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