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