]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/netfilter/xt_connmark.c
[NETFILTER]: xt_MARK target, revision 2
[mirror_ubuntu-zesty-kernel.git] / net / netfilter / xt_connmark.c
CommitLineData
1da177e4
LT
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>
587aa641
PM
24#include <net/netfilter/nf_conntrack.h>
25#include <linux/netfilter/x_tables.h>
26#include <linux/netfilter/xt_connmark.h>
1da177e4 27
3a4fa0a2 28MODULE_AUTHOR("Henrik Nordstrom <hno@marasystems.com>");
1da177e4
LT
29MODULE_DESCRIPTION("IP tables connmark match module");
30MODULE_LICENSE("GPL");
2e4e6a17 31MODULE_ALIAS("ipt_connmark");
73aaf935 32MODULE_ALIAS("ip6t_connmark");
1da177e4 33
1d93a9cb 34static bool
d3c5ee6d
JE
35connmark_mt(const struct sk_buff *skb, const struct net_device *in,
36 const struct net_device *out, const struct xt_match *match,
37 const void *matchinfo, int offset, unsigned int protoff,
38 bool *hotdrop)
1da177e4 39{
2e4e6a17 40 const struct xt_connmark_info *info = matchinfo;
a47362a2 41 const struct nf_conn *ct;
587aa641
PM
42 enum ip_conntrack_info ctinfo;
43
44 ct = nf_ct_get(skb, &ctinfo);
45 if (!ct)
1d93a9cb 46 return false;
1da177e4 47
7c4e36bc 48 return ((ct->mark & info->mask) == info->mark) ^ info->invert;
1da177e4
LT
49}
50
ccb79bdc 51static bool
d3c5ee6d
JE
52connmark_mt_check(const char *tablename, const void *ip,
53 const struct xt_match *match, void *matchinfo,
54 unsigned int hook_mask)
1da177e4 55{
a47362a2 56 const struct xt_connmark_info *cm = matchinfo;
1da177e4 57
bf3a46aa
HW
58 if (cm->mark > 0xffffffff || cm->mask > 0xffffffff) {
59 printk(KERN_WARNING "connmark: only support 32bit mark\n");
ccb79bdc 60 return false;
bf3a46aa 61 }
b9f78f9f 62 if (nf_ct_l3proto_try_module_get(match->family) < 0) {
fe0b9294 63 printk(KERN_WARNING "can't load conntrack support for "
df54aae0 64 "proto=%u\n", match->family);
ccb79bdc 65 return false;
b9f78f9f 66 }
ccb79bdc 67 return true;
1da177e4
LT
68}
69
b9f78f9f 70static void
d3c5ee6d 71connmark_mt_destroy(const struct xt_match *match, void *matchinfo)
b9f78f9f 72{
b9f78f9f 73 nf_ct_l3proto_module_put(match->family);
b9f78f9f
PNA
74}
75
f1eda053
PM
76#ifdef CONFIG_COMPAT
77struct compat_xt_connmark_info {
78 compat_ulong_t mark, mask;
79 u_int8_t invert;
80 u_int8_t __pad1;
81 u_int16_t __pad2;
82};
83
d3c5ee6d 84static void connmark_mt_compat_from_user(void *dst, void *src)
f1eda053 85{
a47362a2 86 const struct compat_xt_connmark_info *cm = src;
f1eda053
PM
87 struct xt_connmark_info m = {
88 .mark = cm->mark,
89 .mask = cm->mask,
90 .invert = cm->invert,
91 };
92 memcpy(dst, &m, sizeof(m));
93}
94
d3c5ee6d 95static int connmark_mt_compat_to_user(void __user *dst, void *src)
f1eda053 96{
a47362a2 97 const struct xt_connmark_info *m = src;
f1eda053
PM
98 struct compat_xt_connmark_info cm = {
99 .mark = m->mark,
100 .mask = m->mask,
101 .invert = m->invert,
102 };
103 return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
104}
105#endif /* CONFIG_COMPAT */
106
d3c5ee6d 107static struct xt_match connmark_mt_reg[] __read_mostly = {
4470bbc7
PM
108 {
109 .name = "connmark",
110 .family = AF_INET,
d3c5ee6d
JE
111 .checkentry = connmark_mt_check,
112 .match = connmark_mt,
113 .destroy = connmark_mt_destroy,
4470bbc7 114 .matchsize = sizeof(struct xt_connmark_info),
f1eda053
PM
115#ifdef CONFIG_COMPAT
116 .compatsize = sizeof(struct compat_xt_connmark_info),
d3c5ee6d
JE
117 .compat_from_user = connmark_mt_compat_from_user,
118 .compat_to_user = connmark_mt_compat_to_user,
f1eda053 119#endif
4470bbc7
PM
120 .me = THIS_MODULE
121 },
122 {
123 .name = "connmark",
124 .family = AF_INET6,
d3c5ee6d
JE
125 .checkentry = connmark_mt_check,
126 .match = connmark_mt,
127 .destroy = connmark_mt_destroy,
4470bbc7 128 .matchsize = sizeof(struct xt_connmark_info),
34f4c429
PM
129#ifdef CONFIG_COMPAT
130 .compatsize = sizeof(struct compat_xt_connmark_info),
131 .compat_from_user = connmark_mt_compat_from_user,
132 .compat_to_user = connmark_mt_compat_to_user,
133#endif
4470bbc7
PM
134 .me = THIS_MODULE
135 },
1da177e4
LT
136};
137
d3c5ee6d 138static int __init connmark_mt_init(void)
1da177e4 139{
d3c5ee6d
JE
140 return xt_register_matches(connmark_mt_reg,
141 ARRAY_SIZE(connmark_mt_reg));
1da177e4
LT
142}
143
d3c5ee6d 144static void __exit connmark_mt_exit(void)
1da177e4 145{
d3c5ee6d 146 xt_unregister_matches(connmark_mt_reg, ARRAY_SIZE(connmark_mt_reg));
1da177e4
LT
147}
148
d3c5ee6d
JE
149module_init(connmark_mt_init);
150module_exit(connmark_mt_exit);