Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Kernel module to match connection tracking information. */
0003 
0004 /* (C) 1999-2001 Paul `Rusty' Russell
0005  * (C) 2002-2005 Netfilter Core Team <coreteam@netfilter.org>
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/skbuff.h>
0010 #include <net/netfilter/nf_conntrack.h>
0011 #include <linux/netfilter/x_tables.h>
0012 #include <linux/netfilter/xt_state.h>
0013 
0014 MODULE_LICENSE("GPL");
0015 MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
0016 MODULE_DESCRIPTION("ip[6]_tables connection tracking state match module");
0017 MODULE_ALIAS("ipt_state");
0018 MODULE_ALIAS("ip6t_state");
0019 
0020 static bool
0021 state_mt(const struct sk_buff *skb, struct xt_action_param *par)
0022 {
0023     const struct xt_state_info *sinfo = par->matchinfo;
0024     enum ip_conntrack_info ctinfo;
0025     unsigned int statebit;
0026     struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
0027 
0028     if (ct)
0029         statebit = XT_STATE_BIT(ctinfo);
0030     else if (ctinfo == IP_CT_UNTRACKED)
0031         statebit = XT_STATE_UNTRACKED;
0032     else
0033         statebit = XT_STATE_INVALID;
0034 
0035     return (sinfo->statemask & statebit);
0036 }
0037 
0038 static int state_mt_check(const struct xt_mtchk_param *par)
0039 {
0040     int ret;
0041 
0042     ret = nf_ct_netns_get(par->net, par->family);
0043     if (ret < 0)
0044         pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
0045                     par->family);
0046     return ret;
0047 }
0048 
0049 static void state_mt_destroy(const struct xt_mtdtor_param *par)
0050 {
0051     nf_ct_netns_put(par->net, par->family);
0052 }
0053 
0054 static struct xt_match state_mt_reg __read_mostly = {
0055     .name       = "state",
0056     .family     = NFPROTO_UNSPEC,
0057     .checkentry = state_mt_check,
0058     .match      = state_mt,
0059     .destroy    = state_mt_destroy,
0060     .matchsize  = sizeof(struct xt_state_info),
0061     .me         = THIS_MODULE,
0062 };
0063 
0064 static int __init state_mt_init(void)
0065 {
0066     return xt_register_match(&state_mt_reg);
0067 }
0068 
0069 static void __exit state_mt_exit(void)
0070 {
0071     xt_unregister_match(&state_mt_reg);
0072 }
0073 
0074 module_init(state_mt_init);
0075 module_exit(state_mt_exit);