Merge branch 'work.mount0' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-block.git] / net / netfilter / xt_state.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/* Kernel module to match connection tracking information. */
3
4/* (C) 1999-2001 Paul `Rusty' Russell
2e4e6a17 5 * (C) 2002-2005 Netfilter Core Team <coreteam@netfilter.org>
1da177e4
LT
6 */
7
8#include <linux/module.h>
9#include <linux/skbuff.h>
587aa641 10#include <net/netfilter/nf_conntrack.h>
2e4e6a17
HW
11#include <linux/netfilter/x_tables.h>
12#include <linux/netfilter/xt_state.h>
1da177e4
LT
13
14MODULE_LICENSE("GPL");
15MODULE_AUTHOR("Rusty Russell <rusty@rustcorp.com.au>");
2e4e6a17
HW
16MODULE_DESCRIPTION("ip[6]_tables connection tracking state match module");
17MODULE_ALIAS("ipt_state");
18MODULE_ALIAS("ip6t_state");
1da177e4 19
1d93a9cb 20static bool
62fc8051 21state_mt(const struct sk_buff *skb, struct xt_action_param *par)
1da177e4 22{
f7108a20 23 const struct xt_state_info *sinfo = par->matchinfo;
1da177e4
LT
24 enum ip_conntrack_info ctinfo;
25 unsigned int statebit;
5bfddbd4 26 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
1da177e4 27
cc41c84b
FW
28 if (ct)
29 statebit = XT_STATE_BIT(ctinfo);
30 else if (ctinfo == IP_CT_UNTRACKED)
31 statebit = XT_STATE_UNTRACKED;
32 else
2e4e6a17 33 statebit = XT_STATE_INVALID;
cc41c84b 34
1da177e4
LT
35 return (sinfo->statemask & statebit);
36}
37
b0f38452 38static int state_mt_check(const struct xt_mtchk_param *par)
b9f78f9f 39{
4a5a5c73
JE
40 int ret;
41
ecb2421b 42 ret = nf_ct_netns_get(par->net, par->family);
f95c74e3 43 if (ret < 0)
b2606644
FW
44 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
45 par->family);
f95c74e3 46 return ret;
b9f78f9f
PNA
47}
48
6be3d859 49static void state_mt_destroy(const struct xt_mtdtor_param *par)
b9f78f9f 50{
ecb2421b 51 nf_ct_netns_put(par->net, par->family);
b9f78f9f
PNA
52}
53
b4467288
JE
54static struct xt_match state_mt_reg __read_mostly = {
55 .name = "state",
56 .family = NFPROTO_UNSPEC,
57 .checkentry = state_mt_check,
58 .match = state_mt,
59 .destroy = state_mt_destroy,
60 .matchsize = sizeof(struct xt_state_info),
61 .me = THIS_MODULE,
1da177e4
LT
62};
63
d3c5ee6d 64static int __init state_mt_init(void)
1da177e4 65{
b4467288 66 return xt_register_match(&state_mt_reg);
1da177e4
LT
67}
68
d3c5ee6d 69static void __exit state_mt_exit(void)
1da177e4 70{
b4467288 71 xt_unregister_match(&state_mt_reg);
1da177e4
LT
72}
73
d3c5ee6d
JE
74module_init(state_mt_init);
75module_exit(state_mt_exit);