xref: /openbmc/linux/net/netfilter/xt_connmark.c (revision b9f78f9f)
1 /* This kernel module matches connection mark values set by the
2  * CONNMARK target
3  *
4  * Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
5  * by Henrik Nordstrom <hno@marasystems.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 #include <linux/module.h>
23 #include <linux/skbuff.h>
24 
25 MODULE_AUTHOR("Henrik Nordstrom <hno@marasytems.com>");
26 MODULE_DESCRIPTION("IP tables connmark match module");
27 MODULE_LICENSE("GPL");
28 MODULE_ALIAS("ipt_connmark");
29 
30 #include <linux/netfilter/x_tables.h>
31 #include <linux/netfilter/xt_connmark.h>
32 #include <net/netfilter/nf_conntrack_compat.h>
33 
34 static int
35 match(const struct sk_buff *skb,
36       const struct net_device *in,
37       const struct net_device *out,
38       const struct xt_match *match,
39       const void *matchinfo,
40       int offset,
41       unsigned int protoff,
42       int *hotdrop)
43 {
44 	const struct xt_connmark_info *info = matchinfo;
45 	u_int32_t ctinfo;
46 	const u_int32_t *ctmark = nf_ct_get_mark(skb, &ctinfo);
47 	if (!ctmark)
48 		return 0;
49 
50 	return (((*ctmark) & info->mask) == info->mark) ^ info->invert;
51 }
52 
53 static int
54 checkentry(const char *tablename,
55 	   const void *ip,
56 	   const struct xt_match *match,
57 	   void *matchinfo,
58 	   unsigned int matchsize,
59 	   unsigned int hook_mask)
60 {
61 	struct xt_connmark_info *cm = (struct xt_connmark_info *)matchinfo;
62 
63 	if (cm->mark > 0xffffffff || cm->mask > 0xffffffff) {
64 		printk(KERN_WARNING "connmark: only support 32bit mark\n");
65 		return 0;
66 	}
67 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
68 	if (nf_ct_l3proto_try_module_get(match->family) < 0) {
69 		printk(KERN_WARNING "can't load nf_conntrack support for "
70 				    "proto=%d\n", match->family);
71 		return 0;
72 	}
73 #endif
74 	return 1;
75 }
76 
77 static void
78 destroy(const struct xt_match *match, void *matchinfo, unsigned int matchsize)
79 {
80 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
81 	nf_ct_l3proto_module_put(match->family);
82 #endif
83 }
84 
85 static struct xt_match connmark_match = {
86 	.name		= "connmark",
87 	.match		= match,
88 	.matchsize	= sizeof(struct xt_connmark_info),
89 	.checkentry	= checkentry,
90 	.destroy	= destroy,
91 	.family		= AF_INET,
92 	.me		= THIS_MODULE
93 };
94 
95 static struct xt_match connmark6_match = {
96 	.name		= "connmark",
97 	.match		= match,
98 	.matchsize	= sizeof(struct xt_connmark_info),
99 	.checkentry	= checkentry,
100 	.destroy	= destroy,
101 	.family		= AF_INET6,
102 	.me		= THIS_MODULE
103 };
104 
105 static int __init init(void)
106 {
107 	int ret;
108 
109 	need_conntrack();
110 
111 	ret = xt_register_match(&connmark_match);
112 	if (ret)
113 		return ret;
114 
115 	ret = xt_register_match(&connmark6_match);
116 	if (ret)
117 		xt_unregister_match(&connmark_match);
118 	return ret;
119 }
120 
121 static void __exit fini(void)
122 {
123 	xt_unregister_match(&connmark6_match);
124 	xt_unregister_match(&connmark_match);
125 }
126 
127 module_init(init);
128 module_exit(fini);
129