]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* This kernel module is used to modify the connection mark values, or |
2 | * to optionally restore the skb nfmark from the connection mark | |
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 | #include <linux/module.h> | |
22 | #include <linux/skbuff.h> | |
23 | #include <linux/ip.h> | |
24 | #include <net/checksum.h> | |
25 | ||
26 | MODULE_AUTHOR("Henrik Nordstrom <hno@marasytems.com>"); | |
27 | MODULE_DESCRIPTION("IP tables CONNMARK matching module"); | |
28 | MODULE_LICENSE("GPL"); | |
2e4e6a17 | 29 | MODULE_ALIAS("ipt_CONNMARK"); |
1da177e4 | 30 | |
2e4e6a17 HW |
31 | #include <linux/netfilter/x_tables.h> |
32 | #include <linux/netfilter/xt_CONNMARK.h> | |
f6180121 | 33 | #include <net/netfilter/nf_conntrack_ecache.h> |
1da177e4 LT |
34 | |
35 | static unsigned int | |
36 | target(struct sk_buff **pskb, | |
37 | const struct net_device *in, | |
38 | const struct net_device *out, | |
39 | unsigned int hooknum, | |
c4986734 | 40 | const struct xt_target *target, |
fe1cb108 | 41 | const void *targinfo) |
1da177e4 | 42 | { |
2e4e6a17 | 43 | const struct xt_connmark_target_info *markinfo = targinfo; |
587aa641 PM |
44 | struct nf_conn *ct; |
45 | enum ip_conntrack_info ctinfo; | |
bf3a46aa | 46 | u_int32_t diff; |
82e91ffe | 47 | u_int32_t mark; |
bf3a46aa | 48 | u_int32_t newmark; |
1da177e4 | 49 | |
587aa641 PM |
50 | ct = nf_ct_get(*pskb, &ctinfo); |
51 | if (ct) { | |
90528e6f PM |
52 | switch(markinfo->mode) { |
53 | case XT_CONNMARK_SET: | |
587aa641 PM |
54 | newmark = (ct->mark & ~markinfo->mask) | markinfo->mark; |
55 | if (newmark != ct->mark) { | |
56 | ct->mark = newmark; | |
90528e6f | 57 | nf_conntrack_event_cache(IPCT_MARK, *pskb); |
2822b0d9 | 58 | } |
90528e6f PM |
59 | break; |
60 | case XT_CONNMARK_SAVE: | |
587aa641 | 61 | newmark = (ct->mark & ~markinfo->mask) | |
82e91ffe | 62 | ((*pskb)->mark & markinfo->mask); |
587aa641 PM |
63 | if (ct->mark != newmark) { |
64 | ct->mark = newmark; | |
90528e6f | 65 | nf_conntrack_event_cache(IPCT_MARK, *pskb); |
90528e6f PM |
66 | } |
67 | break; | |
68 | case XT_CONNMARK_RESTORE: | |
82e91ffe | 69 | mark = (*pskb)->mark; |
587aa641 | 70 | diff = (ct->mark ^ mark) & markinfo->mask; |
2822b0d9 | 71 | (*pskb)->mark = mark ^ diff; |
90528e6f | 72 | break; |
2521c12c | 73 | } |
1da177e4 LT |
74 | } |
75 | ||
2e4e6a17 | 76 | return XT_CONTINUE; |
1da177e4 LT |
77 | } |
78 | ||
e1931b78 | 79 | static bool |
1da177e4 | 80 | checkentry(const char *tablename, |
2e4e6a17 | 81 | const void *entry, |
c4986734 | 82 | const struct xt_target *target, |
1da177e4 | 83 | void *targinfo, |
1da177e4 LT |
84 | unsigned int hook_mask) |
85 | { | |
a47362a2 | 86 | const struct xt_connmark_target_info *matchinfo = targinfo; |
1da177e4 | 87 | |
11078c37 YK |
88 | if (nf_ct_l3proto_try_module_get(target->family) < 0) { |
89 | printk(KERN_WARNING "can't load conntrack support for " | |
90 | "proto=%d\n", target->family); | |
e1931b78 | 91 | return false; |
11078c37 | 92 | } |
2e4e6a17 | 93 | if (matchinfo->mode == XT_CONNMARK_RESTORE) { |
90528e6f PM |
94 | if (strcmp(tablename, "mangle") != 0) { |
95 | printk(KERN_WARNING "CONNMARK: restore can only be " | |
96 | "called from \"mangle\" table, not \"%s\"\n", | |
97 | tablename); | |
e1931b78 | 98 | return false; |
90528e6f | 99 | } |
1da177e4 | 100 | } |
bf3a46aa HW |
101 | if (matchinfo->mark > 0xffffffff || matchinfo->mask > 0xffffffff) { |
102 | printk(KERN_WARNING "CONNMARK: Only supports 32bit mark\n"); | |
e1931b78 | 103 | return false; |
bf3a46aa | 104 | } |
e1931b78 | 105 | return true; |
1da177e4 LT |
106 | } |
107 | ||
11078c37 YK |
108 | static void |
109 | destroy(const struct xt_target *target, void *targinfo) | |
110 | { | |
111 | nf_ct_l3proto_module_put(target->family); | |
112 | } | |
113 | ||
7ce975b9 PM |
114 | #ifdef CONFIG_COMPAT |
115 | struct compat_xt_connmark_target_info { | |
116 | compat_ulong_t mark, mask; | |
117 | u_int8_t mode; | |
118 | u_int8_t __pad1; | |
119 | u_int16_t __pad2; | |
120 | }; | |
121 | ||
122 | static void compat_from_user(void *dst, void *src) | |
123 | { | |
a47362a2 | 124 | const struct compat_xt_connmark_target_info *cm = src; |
7ce975b9 PM |
125 | struct xt_connmark_target_info m = { |
126 | .mark = cm->mark, | |
127 | .mask = cm->mask, | |
128 | .mode = cm->mode, | |
129 | }; | |
130 | memcpy(dst, &m, sizeof(m)); | |
131 | } | |
132 | ||
133 | static int compat_to_user(void __user *dst, void *src) | |
134 | { | |
a47362a2 | 135 | const struct xt_connmark_target_info *m = src; |
7ce975b9 PM |
136 | struct compat_xt_connmark_target_info cm = { |
137 | .mark = m->mark, | |
138 | .mask = m->mask, | |
139 | .mode = m->mode, | |
140 | }; | |
141 | return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0; | |
142 | } | |
143 | #endif /* CONFIG_COMPAT */ | |
144 | ||
9f15c530 | 145 | static struct xt_target xt_connmark_target[] __read_mostly = { |
4470bbc7 PM |
146 | { |
147 | .name = "CONNMARK", | |
148 | .family = AF_INET, | |
149 | .checkentry = checkentry, | |
11078c37 | 150 | .destroy = destroy, |
4470bbc7 PM |
151 | .target = target, |
152 | .targetsize = sizeof(struct xt_connmark_target_info), | |
7ce975b9 PM |
153 | #ifdef CONFIG_COMPAT |
154 | .compatsize = sizeof(struct compat_xt_connmark_target_info), | |
155 | .compat_from_user = compat_from_user, | |
156 | .compat_to_user = compat_to_user, | |
157 | #endif | |
4470bbc7 PM |
158 | .me = THIS_MODULE |
159 | }, | |
160 | { | |
161 | .name = "CONNMARK", | |
162 | .family = AF_INET6, | |
163 | .checkentry = checkentry, | |
11078c37 | 164 | .destroy = destroy, |
4470bbc7 PM |
165 | .target = target, |
166 | .targetsize = sizeof(struct xt_connmark_target_info), | |
167 | .me = THIS_MODULE | |
168 | }, | |
1da177e4 LT |
169 | }; |
170 | ||
65b4b4e8 | 171 | static int __init xt_connmark_init(void) |
1da177e4 | 172 | { |
4470bbc7 PM |
173 | return xt_register_targets(xt_connmark_target, |
174 | ARRAY_SIZE(xt_connmark_target)); | |
1da177e4 LT |
175 | } |
176 | ||
65b4b4e8 | 177 | static void __exit xt_connmark_fini(void) |
1da177e4 | 178 | { |
4470bbc7 PM |
179 | xt_unregister_targets(xt_connmark_target, |
180 | ARRAY_SIZE(xt_connmark_target)); | |
1da177e4 LT |
181 | } |
182 | ||
65b4b4e8 AM |
183 | module_init(xt_connmark_init); |
184 | module_exit(xt_connmark_fini); |