Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
d956798d JK |
2 | /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu> |
3 | * Patrick Schaaf <bof@bof.de> | |
4 | * Martin Josefsson <gandalf@wlug.westbo.se> | |
fe03d474 | 5 | * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org> |
d956798d JK |
6 | */ |
7 | ||
8 | /* Kernel module which implements the set match and SET target | |
ca0f6a5c JK |
9 | * for netfilter/iptables. |
10 | */ | |
d956798d JK |
11 | |
12 | #include <linux/module.h> | |
13 | #include <linux/skbuff.h> | |
d956798d JK |
14 | |
15 | #include <linux/netfilter/x_tables.h> | |
a9756e6f | 16 | #include <linux/netfilter/ipset/ip_set.h> |
a9756e6f | 17 | #include <uapi/linux/netfilter/xt_set.h> |
d956798d JK |
18 | |
19 | MODULE_LICENSE("GPL"); | |
fe03d474 | 20 | MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>"); |
d956798d JK |
21 | MODULE_DESCRIPTION("Xtables: IP set match and target module"); |
22 | MODULE_ALIAS("xt_SET"); | |
23 | MODULE_ALIAS("ipt_set"); | |
24 | MODULE_ALIAS("ip6t_set"); | |
25 | MODULE_ALIAS("ipt_SET"); | |
26 | MODULE_ALIAS("ip6t_SET"); | |
27 | ||
28 | static inline int | |
29 | match_set(ip_set_id_t index, const struct sk_buff *skb, | |
b66554cf | 30 | const struct xt_action_param *par, |
075e64c0 | 31 | struct ip_set_adt_opt *opt, int inv) |
d956798d | 32 | { |
b66554cf | 33 | if (ip_set_test(index, skb, par, opt)) |
d956798d JK |
34 | inv = !inv; |
35 | return inv; | |
36 | } | |
37 | ||
4750005a JK |
38 | #define ADT_OPT(n, f, d, fs, cfs, t, p, b, po, bo) \ |
39 | struct ip_set_adt_opt n = { \ | |
40 | .family = f, \ | |
41 | .dim = d, \ | |
42 | .flags = fs, \ | |
43 | .cmdflags = cfs, \ | |
44 | .ext.timeout = t, \ | |
45 | .ext.packets = p, \ | |
46 | .ext.bytes = b, \ | |
47 | .ext.packets_op = po, \ | |
48 | .ext.bytes_op = bo, \ | |
127f5591 | 49 | } |
ac8cc925 | 50 | |
d956798d JK |
51 | /* Revision 0 interface: backward compatible with netfilter/iptables */ |
52 | ||
53 | static bool | |
54 | set_match_v0(const struct sk_buff *skb, struct xt_action_param *par) | |
55 | { | |
56 | const struct xt_set_info_match_v0 *info = par->matchinfo; | |
ca0f6a5c | 57 | |
613dbd95 | 58 | ADT_OPT(opt, xt_family(par), info->match_set.u.compat.dim, |
4750005a JK |
59 | info->match_set.u.compat.flags, 0, UINT_MAX, |
60 | 0, 0, 0, 0); | |
d956798d | 61 | |
b66554cf | 62 | return match_set(info->match_set.index, skb, par, &opt, |
d956798d JK |
63 | info->match_set.u.compat.flags & IPSET_INV_MATCH); |
64 | } | |
65 | ||
66 | static void | |
67 | compat_flags(struct xt_set_info_v0 *info) | |
68 | { | |
69 | u_int8_t i; | |
70 | ||
71 | /* Fill out compatibility data according to enum ip_set_kopt */ | |
72 | info->u.compat.dim = IPSET_DIM_ZERO; | |
73 | if (info->u.flags[0] & IPSET_MATCH_INV) | |
74 | info->u.compat.flags |= IPSET_INV_MATCH; | |
ca0f6a5c | 75 | for (i = 0; i < IPSET_DIM_MAX - 1 && info->u.flags[i]; i++) { |
d956798d JK |
76 | info->u.compat.dim++; |
77 | if (info->u.flags[i] & IPSET_SRC) | |
ca0f6a5c | 78 | info->u.compat.flags |= (1 << info->u.compat.dim); |
d956798d JK |
79 | } |
80 | } | |
81 | ||
82 | static int | |
83 | set_match_v0_checkentry(const struct xt_mtchk_param *par) | |
84 | { | |
85 | struct xt_set_info_match_v0 *info = par->matchinfo; | |
86 | ip_set_id_t index; | |
87 | ||
1785e8f4 | 88 | index = ip_set_nfnl_get_byindex(par->net, info->match_set.index); |
d956798d JK |
89 | |
90 | if (index == IPSET_INVALID_ID) { | |
c82b31c5 FW |
91 | pr_info_ratelimited("Cannot find set identified by id %u to match\n", |
92 | info->match_set.index); | |
d956798d JK |
93 | return -ENOENT; |
94 | } | |
ca0f6a5c | 95 | if (info->match_set.u.flags[IPSET_DIM_MAX - 1] != 0) { |
c82b31c5 | 96 | pr_info_ratelimited("set match dimension is over the limit!\n"); |
1785e8f4 | 97 | ip_set_nfnl_put(par->net, info->match_set.index); |
d956798d JK |
98 | return -ERANGE; |
99 | } | |
100 | ||
101 | /* Fill out compatibility data */ | |
102 | compat_flags(&info->match_set); | |
103 | ||
104 | return 0; | |
105 | } | |
106 | ||
107 | static void | |
108 | set_match_v0_destroy(const struct xt_mtdtor_param *par) | |
109 | { | |
110 | struct xt_set_info_match_v0 *info = par->matchinfo; | |
111 | ||
1785e8f4 | 112 | ip_set_nfnl_put(par->net, info->match_set.index); |
d956798d JK |
113 | } |
114 | ||
bd3129fc JK |
115 | /* Revision 1 match */ |
116 | ||
117 | static bool | |
118 | set_match_v1(const struct sk_buff *skb, struct xt_action_param *par) | |
119 | { | |
120 | const struct xt_set_info_match_v1 *info = par->matchinfo; | |
ca0f6a5c | 121 | |
613dbd95 | 122 | ADT_OPT(opt, xt_family(par), info->match_set.dim, |
4750005a JK |
123 | info->match_set.flags, 0, UINT_MAX, |
124 | 0, 0, 0, 0); | |
bd3129fc JK |
125 | |
126 | if (opt.flags & IPSET_RETURN_NOMATCH) | |
127 | opt.cmdflags |= IPSET_FLAG_RETURN_NOMATCH; | |
128 | ||
129 | return match_set(info->match_set.index, skb, par, &opt, | |
130 | info->match_set.flags & IPSET_INV_MATCH); | |
131 | } | |
132 | ||
133 | static int | |
134 | set_match_v1_checkentry(const struct xt_mtchk_param *par) | |
135 | { | |
136 | struct xt_set_info_match_v1 *info = par->matchinfo; | |
137 | ip_set_id_t index; | |
138 | ||
1785e8f4 | 139 | index = ip_set_nfnl_get_byindex(par->net, info->match_set.index); |
bd3129fc JK |
140 | |
141 | if (index == IPSET_INVALID_ID) { | |
c82b31c5 FW |
142 | pr_info_ratelimited("Cannot find set identified by id %u to match\n", |
143 | info->match_set.index); | |
bd3129fc JK |
144 | return -ENOENT; |
145 | } | |
146 | if (info->match_set.dim > IPSET_DIM_MAX) { | |
c82b31c5 | 147 | pr_info_ratelimited("set match dimension is over the limit!\n"); |
1785e8f4 | 148 | ip_set_nfnl_put(par->net, info->match_set.index); |
bd3129fc JK |
149 | return -ERANGE; |
150 | } | |
151 | ||
152 | return 0; | |
153 | } | |
154 | ||
155 | static void | |
156 | set_match_v1_destroy(const struct xt_mtdtor_param *par) | |
157 | { | |
158 | struct xt_set_info_match_v1 *info = par->matchinfo; | |
159 | ||
1785e8f4 | 160 | ip_set_nfnl_put(par->net, info->match_set.index); |
bd3129fc JK |
161 | } |
162 | ||
163 | /* Revision 3 match */ | |
164 | ||
bd3129fc JK |
165 | static bool |
166 | set_match_v3(const struct sk_buff *skb, struct xt_action_param *par) | |
167 | { | |
168 | const struct xt_set_info_match_v3 *info = par->matchinfo; | |
ca0f6a5c | 169 | |
613dbd95 | 170 | ADT_OPT(opt, xt_family(par), info->match_set.dim, |
4750005a JK |
171 | info->match_set.flags, info->flags, UINT_MAX, |
172 | info->packets.value, info->bytes.value, | |
173 | info->packets.op, info->bytes.op); | |
bd3129fc JK |
174 | |
175 | if (info->packets.op != IPSET_COUNTER_NONE || | |
176 | info->bytes.op != IPSET_COUNTER_NONE) | |
177 | opt.cmdflags |= IPSET_FLAG_MATCH_COUNTERS; | |
178 | ||
4750005a JK |
179 | return match_set(info->match_set.index, skb, par, &opt, |
180 | info->match_set.flags & IPSET_INV_MATCH); | |
bd3129fc JK |
181 | } |
182 | ||
183 | #define set_match_v3_checkentry set_match_v1_checkentry | |
184 | #define set_match_v3_destroy set_match_v1_destroy | |
185 | ||
a51b9199 JK |
186 | /* Revision 4 match */ |
187 | ||
a51b9199 JK |
188 | static bool |
189 | set_match_v4(const struct sk_buff *skb, struct xt_action_param *par) | |
190 | { | |
191 | const struct xt_set_info_match_v4 *info = par->matchinfo; | |
ca0f6a5c | 192 | |
613dbd95 | 193 | ADT_OPT(opt, xt_family(par), info->match_set.dim, |
4750005a JK |
194 | info->match_set.flags, info->flags, UINT_MAX, |
195 | info->packets.value, info->bytes.value, | |
196 | info->packets.op, info->bytes.op); | |
a51b9199 JK |
197 | |
198 | if (info->packets.op != IPSET_COUNTER_NONE || | |
199 | info->bytes.op != IPSET_COUNTER_NONE) | |
200 | opt.cmdflags |= IPSET_FLAG_MATCH_COUNTERS; | |
201 | ||
4750005a JK |
202 | return match_set(info->match_set.index, skb, par, &opt, |
203 | info->match_set.flags & IPSET_INV_MATCH); | |
a51b9199 JK |
204 | } |
205 | ||
206 | #define set_match_v4_checkentry set_match_v1_checkentry | |
207 | #define set_match_v4_destroy set_match_v1_destroy | |
208 | ||
bd3129fc JK |
209 | /* Revision 0 interface: backward compatible with netfilter/iptables */ |
210 | ||
d956798d JK |
211 | static unsigned int |
212 | set_target_v0(struct sk_buff *skb, const struct xt_action_param *par) | |
213 | { | |
214 | const struct xt_set_info_target_v0 *info = par->targinfo; | |
ca0f6a5c | 215 | |
613dbd95 | 216 | ADT_OPT(add_opt, xt_family(par), info->add_set.u.compat.dim, |
4750005a JK |
217 | info->add_set.u.compat.flags, 0, UINT_MAX, |
218 | 0, 0, 0, 0); | |
613dbd95 | 219 | ADT_OPT(del_opt, xt_family(par), info->del_set.u.compat.dim, |
4750005a JK |
220 | info->del_set.u.compat.flags, 0, UINT_MAX, |
221 | 0, 0, 0, 0); | |
d956798d JK |
222 | |
223 | if (info->add_set.index != IPSET_INVALID_ID) | |
b66554cf | 224 | ip_set_add(info->add_set.index, skb, par, &add_opt); |
d956798d | 225 | if (info->del_set.index != IPSET_INVALID_ID) |
b66554cf | 226 | ip_set_del(info->del_set.index, skb, par, &del_opt); |
d956798d JK |
227 | |
228 | return XT_CONTINUE; | |
229 | } | |
230 | ||
231 | static int | |
232 | set_target_v0_checkentry(const struct xt_tgchk_param *par) | |
233 | { | |
234 | struct xt_set_info_target_v0 *info = par->targinfo; | |
235 | ip_set_id_t index; | |
236 | ||
237 | if (info->add_set.index != IPSET_INVALID_ID) { | |
1785e8f4 | 238 | index = ip_set_nfnl_get_byindex(par->net, info->add_set.index); |
d956798d | 239 | if (index == IPSET_INVALID_ID) { |
c82b31c5 FW |
240 | pr_info_ratelimited("Cannot find add_set index %u as target\n", |
241 | info->add_set.index); | |
d956798d JK |
242 | return -ENOENT; |
243 | } | |
244 | } | |
245 | ||
246 | if (info->del_set.index != IPSET_INVALID_ID) { | |
1785e8f4 | 247 | index = ip_set_nfnl_get_byindex(par->net, info->del_set.index); |
d956798d | 248 | if (index == IPSET_INVALID_ID) { |
c82b31c5 FW |
249 | pr_info_ratelimited("Cannot find del_set index %u as target\n", |
250 | info->del_set.index); | |
eafbd3fd | 251 | if (info->add_set.index != IPSET_INVALID_ID) |
1785e8f4 | 252 | ip_set_nfnl_put(par->net, info->add_set.index); |
d956798d JK |
253 | return -ENOENT; |
254 | } | |
255 | } | |
ca0f6a5c JK |
256 | if (info->add_set.u.flags[IPSET_DIM_MAX - 1] != 0 || |
257 | info->del_set.u.flags[IPSET_DIM_MAX - 1] != 0) { | |
c82b31c5 | 258 | pr_info_ratelimited("SET target dimension over the limit!\n"); |
eafbd3fd | 259 | if (info->add_set.index != IPSET_INVALID_ID) |
1785e8f4 | 260 | ip_set_nfnl_put(par->net, info->add_set.index); |
eafbd3fd | 261 | if (info->del_set.index != IPSET_INVALID_ID) |
1785e8f4 | 262 | ip_set_nfnl_put(par->net, info->del_set.index); |
d956798d JK |
263 | return -ERANGE; |
264 | } | |
265 | ||
266 | /* Fill out compatibility data */ | |
267 | compat_flags(&info->add_set); | |
268 | compat_flags(&info->del_set); | |
269 | ||
270 | return 0; | |
271 | } | |
272 | ||
273 | static void | |
274 | set_target_v0_destroy(const struct xt_tgdtor_param *par) | |
275 | { | |
276 | const struct xt_set_info_target_v0 *info = par->targinfo; | |
277 | ||
278 | if (info->add_set.index != IPSET_INVALID_ID) | |
1785e8f4 | 279 | ip_set_nfnl_put(par->net, info->add_set.index); |
d956798d | 280 | if (info->del_set.index != IPSET_INVALID_ID) |
1785e8f4 | 281 | ip_set_nfnl_put(par->net, info->del_set.index); |
d956798d JK |
282 | } |
283 | ||
bd3129fc | 284 | /* Revision 1 target */ |
d956798d JK |
285 | |
286 | static unsigned int | |
ac8cc925 | 287 | set_target_v1(struct sk_buff *skb, const struct xt_action_param *par) |
d956798d | 288 | { |
ac8cc925 | 289 | const struct xt_set_info_target_v1 *info = par->targinfo; |
ca0f6a5c | 290 | |
613dbd95 | 291 | ADT_OPT(add_opt, xt_family(par), info->add_set.dim, |
4750005a JK |
292 | info->add_set.flags, 0, UINT_MAX, |
293 | 0, 0, 0, 0); | |
613dbd95 | 294 | ADT_OPT(del_opt, xt_family(par), info->del_set.dim, |
4750005a JK |
295 | info->del_set.flags, 0, UINT_MAX, |
296 | 0, 0, 0, 0); | |
d956798d JK |
297 | |
298 | if (info->add_set.index != IPSET_INVALID_ID) | |
b66554cf | 299 | ip_set_add(info->add_set.index, skb, par, &add_opt); |
d956798d | 300 | if (info->del_set.index != IPSET_INVALID_ID) |
b66554cf | 301 | ip_set_del(info->del_set.index, skb, par, &del_opt); |
d956798d JK |
302 | |
303 | return XT_CONTINUE; | |
304 | } | |
305 | ||
306 | static int | |
ac8cc925 | 307 | set_target_v1_checkentry(const struct xt_tgchk_param *par) |
d956798d | 308 | { |
ac8cc925 | 309 | const struct xt_set_info_target_v1 *info = par->targinfo; |
d956798d JK |
310 | ip_set_id_t index; |
311 | ||
312 | if (info->add_set.index != IPSET_INVALID_ID) { | |
1785e8f4 | 313 | index = ip_set_nfnl_get_byindex(par->net, info->add_set.index); |
d956798d | 314 | if (index == IPSET_INVALID_ID) { |
c82b31c5 FW |
315 | pr_info_ratelimited("Cannot find add_set index %u as target\n", |
316 | info->add_set.index); | |
d956798d JK |
317 | return -ENOENT; |
318 | } | |
319 | } | |
320 | ||
321 | if (info->del_set.index != IPSET_INVALID_ID) { | |
1785e8f4 | 322 | index = ip_set_nfnl_get_byindex(par->net, info->del_set.index); |
d956798d | 323 | if (index == IPSET_INVALID_ID) { |
c82b31c5 FW |
324 | pr_info_ratelimited("Cannot find del_set index %u as target\n", |
325 | info->del_set.index); | |
eafbd3fd | 326 | if (info->add_set.index != IPSET_INVALID_ID) |
1785e8f4 | 327 | ip_set_nfnl_put(par->net, info->add_set.index); |
d956798d JK |
328 | return -ENOENT; |
329 | } | |
330 | } | |
331 | if (info->add_set.dim > IPSET_DIM_MAX || | |
eafbd3fd | 332 | info->del_set.dim > IPSET_DIM_MAX) { |
c82b31c5 | 333 | pr_info_ratelimited("SET target dimension over the limit!\n"); |
eafbd3fd | 334 | if (info->add_set.index != IPSET_INVALID_ID) |
1785e8f4 | 335 | ip_set_nfnl_put(par->net, info->add_set.index); |
eafbd3fd | 336 | if (info->del_set.index != IPSET_INVALID_ID) |
1785e8f4 | 337 | ip_set_nfnl_put(par->net, info->del_set.index); |
d956798d JK |
338 | return -ERANGE; |
339 | } | |
340 | ||
341 | return 0; | |
342 | } | |
343 | ||
344 | static void | |
ac8cc925 | 345 | set_target_v1_destroy(const struct xt_tgdtor_param *par) |
d956798d | 346 | { |
ac8cc925 | 347 | const struct xt_set_info_target_v1 *info = par->targinfo; |
d956798d JK |
348 | |
349 | if (info->add_set.index != IPSET_INVALID_ID) | |
1785e8f4 | 350 | ip_set_nfnl_put(par->net, info->add_set.index); |
d956798d | 351 | if (info->del_set.index != IPSET_INVALID_ID) |
1785e8f4 | 352 | ip_set_nfnl_put(par->net, info->del_set.index); |
d956798d JK |
353 | } |
354 | ||
ac8cc925 JK |
355 | /* Revision 2 target */ |
356 | ||
357 | static unsigned int | |
358 | set_target_v2(struct sk_buff *skb, const struct xt_action_param *par) | |
359 | { | |
360 | const struct xt_set_info_target_v2 *info = par->targinfo; | |
ca0f6a5c | 361 | |
613dbd95 | 362 | ADT_OPT(add_opt, xt_family(par), info->add_set.dim, |
4750005a JK |
363 | info->add_set.flags, info->flags, info->timeout, |
364 | 0, 0, 0, 0); | |
613dbd95 | 365 | ADT_OPT(del_opt, xt_family(par), info->del_set.dim, |
4750005a JK |
366 | info->del_set.flags, 0, UINT_MAX, |
367 | 0, 0, 0, 0); | |
ac8cc925 | 368 | |
127f5591 | 369 | /* Normalize to fit into jiffies */ |
075e64c0 | 370 | if (add_opt.ext.timeout != IPSET_NO_TIMEOUT && |
30a2e107 JK |
371 | add_opt.ext.timeout > IPSET_MAX_TIMEOUT) |
372 | add_opt.ext.timeout = IPSET_MAX_TIMEOUT; | |
ac8cc925 | 373 | if (info->add_set.index != IPSET_INVALID_ID) |
b66554cf | 374 | ip_set_add(info->add_set.index, skb, par, &add_opt); |
ac8cc925 | 375 | if (info->del_set.index != IPSET_INVALID_ID) |
b66554cf | 376 | ip_set_del(info->del_set.index, skb, par, &del_opt); |
ac8cc925 JK |
377 | |
378 | return XT_CONTINUE; | |
379 | } | |
380 | ||
381 | #define set_target_v2_checkentry set_target_v1_checkentry | |
382 | #define set_target_v2_destroy set_target_v1_destroy | |
383 | ||
76cea410 AD |
384 | /* Revision 3 target */ |
385 | ||
bec810d9 JK |
386 | #define MOPT(opt, member) ((opt).ext.skbinfo.member) |
387 | ||
76cea410 AD |
388 | static unsigned int |
389 | set_target_v3(struct sk_buff *skb, const struct xt_action_param *par) | |
390 | { | |
391 | const struct xt_set_info_target_v3 *info = par->targinfo; | |
ca0f6a5c JK |
392 | int ret; |
393 | ||
613dbd95 | 394 | ADT_OPT(add_opt, xt_family(par), info->add_set.dim, |
4750005a JK |
395 | info->add_set.flags, info->flags, info->timeout, |
396 | 0, 0, 0, 0); | |
613dbd95 | 397 | ADT_OPT(del_opt, xt_family(par), info->del_set.dim, |
4750005a JK |
398 | info->del_set.flags, 0, UINT_MAX, |
399 | 0, 0, 0, 0); | |
613dbd95 | 400 | ADT_OPT(map_opt, xt_family(par), info->map_set.dim, |
4750005a JK |
401 | info->map_set.flags, 0, UINT_MAX, |
402 | 0, 0, 0, 0); | |
76cea410 | 403 | |
76cea410 AD |
404 | /* Normalize to fit into jiffies */ |
405 | if (add_opt.ext.timeout != IPSET_NO_TIMEOUT && | |
30a2e107 JK |
406 | add_opt.ext.timeout > IPSET_MAX_TIMEOUT) |
407 | add_opt.ext.timeout = IPSET_MAX_TIMEOUT; | |
76cea410 AD |
408 | if (info->add_set.index != IPSET_INVALID_ID) |
409 | ip_set_add(info->add_set.index, skb, par, &add_opt); | |
410 | if (info->del_set.index != IPSET_INVALID_ID) | |
411 | ip_set_del(info->del_set.index, skb, par, &del_opt); | |
412 | if (info->map_set.index != IPSET_INVALID_ID) { | |
413 | map_opt.cmdflags |= info->flags & (IPSET_FLAG_MAP_SKBMARK | | |
414 | IPSET_FLAG_MAP_SKBPRIO | | |
415 | IPSET_FLAG_MAP_SKBQUEUE); | |
416 | ret = match_set(info->map_set.index, skb, par, &map_opt, | |
417 | info->map_set.flags & IPSET_INV_MATCH); | |
418 | if (!ret) | |
419 | return XT_CONTINUE; | |
420 | if (map_opt.cmdflags & IPSET_FLAG_MAP_SKBMARK) | |
bec810d9 JK |
421 | skb->mark = (skb->mark & ~MOPT(map_opt,skbmarkmask)) |
422 | ^ MOPT(map_opt, skbmark); | |
76cea410 | 423 | if (map_opt.cmdflags & IPSET_FLAG_MAP_SKBPRIO) |
bec810d9 | 424 | skb->priority = MOPT(map_opt, skbprio); |
76cea410 AD |
425 | if ((map_opt.cmdflags & IPSET_FLAG_MAP_SKBQUEUE) && |
426 | skb->dev && | |
bec810d9 JK |
427 | skb->dev->real_num_tx_queues > MOPT(map_opt, skbqueue)) |
428 | skb_set_queue_mapping(skb, MOPT(map_opt, skbqueue)); | |
76cea410 AD |
429 | } |
430 | return XT_CONTINUE; | |
431 | } | |
432 | ||
76cea410 AD |
433 | static int |
434 | set_target_v3_checkentry(const struct xt_tgchk_param *par) | |
435 | { | |
436 | const struct xt_set_info_target_v3 *info = par->targinfo; | |
437 | ip_set_id_t index; | |
b1732e16 | 438 | int ret = 0; |
76cea410 AD |
439 | |
440 | if (info->add_set.index != IPSET_INVALID_ID) { | |
441 | index = ip_set_nfnl_get_byindex(par->net, | |
442 | info->add_set.index); | |
443 | if (index == IPSET_INVALID_ID) { | |
c82b31c5 FW |
444 | pr_info_ratelimited("Cannot find add_set index %u as target\n", |
445 | info->add_set.index); | |
76cea410 AD |
446 | return -ENOENT; |
447 | } | |
448 | } | |
449 | ||
450 | if (info->del_set.index != IPSET_INVALID_ID) { | |
451 | index = ip_set_nfnl_get_byindex(par->net, | |
452 | info->del_set.index); | |
453 | if (index == IPSET_INVALID_ID) { | |
c82b31c5 FW |
454 | pr_info_ratelimited("Cannot find del_set index %u as target\n", |
455 | info->del_set.index); | |
b1732e16 JK |
456 | ret = -ENOENT; |
457 | goto cleanup_add; | |
76cea410 AD |
458 | } |
459 | } | |
460 | ||
461 | if (info->map_set.index != IPSET_INVALID_ID) { | |
462 | if (strncmp(par->table, "mangle", 7)) { | |
c82b31c5 | 463 | pr_info_ratelimited("--map-set only usable from mangle table\n"); |
b1732e16 JK |
464 | ret = -EINVAL; |
465 | goto cleanup_del; | |
76cea410 AD |
466 | } |
467 | if (((info->flags & IPSET_FLAG_MAP_SKBPRIO) | | |
468 | (info->flags & IPSET_FLAG_MAP_SKBQUEUE)) && | |
9dcceb13 | 469 | (par->hook_mask & ~(1 << NF_INET_FORWARD | |
76cea410 AD |
470 | 1 << NF_INET_LOCAL_OUT | |
471 | 1 << NF_INET_POST_ROUTING))) { | |
c82b31c5 | 472 | pr_info_ratelimited("mapping of prio or/and queue is allowed only from OUTPUT/FORWARD/POSTROUTING chains\n"); |
b1732e16 JK |
473 | ret = -EINVAL; |
474 | goto cleanup_del; | |
76cea410 AD |
475 | } |
476 | index = ip_set_nfnl_get_byindex(par->net, | |
477 | info->map_set.index); | |
478 | if (index == IPSET_INVALID_ID) { | |
c82b31c5 FW |
479 | pr_info_ratelimited("Cannot find map_set index %u as target\n", |
480 | info->map_set.index); | |
b1732e16 JK |
481 | ret = -ENOENT; |
482 | goto cleanup_del; | |
76cea410 AD |
483 | } |
484 | } | |
485 | ||
486 | if (info->add_set.dim > IPSET_DIM_MAX || | |
487 | info->del_set.dim > IPSET_DIM_MAX || | |
488 | info->map_set.dim > IPSET_DIM_MAX) { | |
c82b31c5 | 489 | pr_info_ratelimited("SET target dimension over the limit!\n"); |
b1732e16 JK |
490 | ret = -ERANGE; |
491 | goto cleanup_mark; | |
76cea410 AD |
492 | } |
493 | ||
494 | return 0; | |
b1732e16 JK |
495 | cleanup_mark: |
496 | if (info->map_set.index != IPSET_INVALID_ID) | |
497 | ip_set_nfnl_put(par->net, info->map_set.index); | |
498 | cleanup_del: | |
499 | if (info->del_set.index != IPSET_INVALID_ID) | |
500 | ip_set_nfnl_put(par->net, info->del_set.index); | |
501 | cleanup_add: | |
502 | if (info->add_set.index != IPSET_INVALID_ID) | |
503 | ip_set_nfnl_put(par->net, info->add_set.index); | |
504 | return ret; | |
76cea410 AD |
505 | } |
506 | ||
507 | static void | |
508 | set_target_v3_destroy(const struct xt_tgdtor_param *par) | |
509 | { | |
510 | const struct xt_set_info_target_v3 *info = par->targinfo; | |
511 | ||
512 | if (info->add_set.index != IPSET_INVALID_ID) | |
513 | ip_set_nfnl_put(par->net, info->add_set.index); | |
514 | if (info->del_set.index != IPSET_INVALID_ID) | |
515 | ip_set_nfnl_put(par->net, info->del_set.index); | |
516 | if (info->map_set.index != IPSET_INVALID_ID) | |
517 | ip_set_nfnl_put(par->net, info->map_set.index); | |
518 | } | |
519 | ||
d956798d JK |
520 | static struct xt_match set_matches[] __read_mostly = { |
521 | { | |
522 | .name = "set", | |
523 | .family = NFPROTO_IPV4, | |
524 | .revision = 0, | |
525 | .match = set_match_v0, | |
526 | .matchsize = sizeof(struct xt_set_info_match_v0), | |
527 | .checkentry = set_match_v0_checkentry, | |
528 | .destroy = set_match_v0_destroy, | |
529 | .me = THIS_MODULE | |
530 | }, | |
531 | { | |
532 | .name = "set", | |
533 | .family = NFPROTO_IPV4, | |
534 | .revision = 1, | |
ac8cc925 JK |
535 | .match = set_match_v1, |
536 | .matchsize = sizeof(struct xt_set_info_match_v1), | |
537 | .checkentry = set_match_v1_checkentry, | |
538 | .destroy = set_match_v1_destroy, | |
d956798d JK |
539 | .me = THIS_MODULE |
540 | }, | |
541 | { | |
542 | .name = "set", | |
543 | .family = NFPROTO_IPV6, | |
544 | .revision = 1, | |
ac8cc925 JK |
545 | .match = set_match_v1, |
546 | .matchsize = sizeof(struct xt_set_info_match_v1), | |
547 | .checkentry = set_match_v1_checkentry, | |
548 | .destroy = set_match_v1_destroy, | |
d956798d JK |
549 | .me = THIS_MODULE |
550 | }, | |
3e0304a5 JK |
551 | /* --return-nomatch flag support */ |
552 | { | |
553 | .name = "set", | |
554 | .family = NFPROTO_IPV4, | |
555 | .revision = 2, | |
556 | .match = set_match_v1, | |
557 | .matchsize = sizeof(struct xt_set_info_match_v1), | |
558 | .checkentry = set_match_v1_checkentry, | |
559 | .destroy = set_match_v1_destroy, | |
560 | .me = THIS_MODULE | |
561 | }, | |
562 | { | |
563 | .name = "set", | |
564 | .family = NFPROTO_IPV6, | |
565 | .revision = 2, | |
566 | .match = set_match_v1, | |
567 | .matchsize = sizeof(struct xt_set_info_match_v1), | |
568 | .checkentry = set_match_v1_checkentry, | |
569 | .destroy = set_match_v1_destroy, | |
570 | .me = THIS_MODULE | |
571 | }, | |
6e01781d JK |
572 | /* counters support: update, match */ |
573 | { | |
574 | .name = "set", | |
575 | .family = NFPROTO_IPV4, | |
576 | .revision = 3, | |
577 | .match = set_match_v3, | |
578 | .matchsize = sizeof(struct xt_set_info_match_v3), | |
579 | .checkentry = set_match_v3_checkentry, | |
580 | .destroy = set_match_v3_destroy, | |
581 | .me = THIS_MODULE | |
582 | }, | |
583 | { | |
584 | .name = "set", | |
585 | .family = NFPROTO_IPV6, | |
586 | .revision = 3, | |
587 | .match = set_match_v3, | |
588 | .matchsize = sizeof(struct xt_set_info_match_v3), | |
589 | .checkentry = set_match_v3_checkentry, | |
590 | .destroy = set_match_v3_destroy, | |
591 | .me = THIS_MODULE | |
592 | }, | |
a51b9199 JK |
593 | /* new revision for counters support: update, match */ |
594 | { | |
595 | .name = "set", | |
596 | .family = NFPROTO_IPV4, | |
597 | .revision = 4, | |
598 | .match = set_match_v4, | |
599 | .matchsize = sizeof(struct xt_set_info_match_v4), | |
600 | .checkentry = set_match_v4_checkentry, | |
601 | .destroy = set_match_v4_destroy, | |
602 | .me = THIS_MODULE | |
603 | }, | |
604 | { | |
605 | .name = "set", | |
606 | .family = NFPROTO_IPV6, | |
607 | .revision = 4, | |
608 | .match = set_match_v4, | |
609 | .matchsize = sizeof(struct xt_set_info_match_v4), | |
610 | .checkentry = set_match_v4_checkentry, | |
611 | .destroy = set_match_v4_destroy, | |
612 | .me = THIS_MODULE | |
613 | }, | |
d956798d JK |
614 | }; |
615 | ||
616 | static struct xt_target set_targets[] __read_mostly = { | |
617 | { | |
618 | .name = "SET", | |
619 | .revision = 0, | |
620 | .family = NFPROTO_IPV4, | |
621 | .target = set_target_v0, | |
622 | .targetsize = sizeof(struct xt_set_info_target_v0), | |
623 | .checkentry = set_target_v0_checkentry, | |
624 | .destroy = set_target_v0_destroy, | |
625 | .me = THIS_MODULE | |
626 | }, | |
627 | { | |
628 | .name = "SET", | |
629 | .revision = 1, | |
630 | .family = NFPROTO_IPV4, | |
ac8cc925 JK |
631 | .target = set_target_v1, |
632 | .targetsize = sizeof(struct xt_set_info_target_v1), | |
633 | .checkentry = set_target_v1_checkentry, | |
634 | .destroy = set_target_v1_destroy, | |
d956798d JK |
635 | .me = THIS_MODULE |
636 | }, | |
637 | { | |
638 | .name = "SET", | |
639 | .revision = 1, | |
640 | .family = NFPROTO_IPV6, | |
ac8cc925 JK |
641 | .target = set_target_v1, |
642 | .targetsize = sizeof(struct xt_set_info_target_v1), | |
643 | .checkentry = set_target_v1_checkentry, | |
644 | .destroy = set_target_v1_destroy, | |
645 | .me = THIS_MODULE | |
646 | }, | |
3e0304a5 | 647 | /* --timeout and --exist flags support */ |
ac8cc925 JK |
648 | { |
649 | .name = "SET", | |
650 | .revision = 2, | |
651 | .family = NFPROTO_IPV4, | |
652 | .target = set_target_v2, | |
653 | .targetsize = sizeof(struct xt_set_info_target_v2), | |
654 | .checkentry = set_target_v2_checkentry, | |
655 | .destroy = set_target_v2_destroy, | |
656 | .me = THIS_MODULE | |
657 | }, | |
658 | { | |
659 | .name = "SET", | |
660 | .revision = 2, | |
661 | .family = NFPROTO_IPV6, | |
662 | .target = set_target_v2, | |
663 | .targetsize = sizeof(struct xt_set_info_target_v2), | |
664 | .checkentry = set_target_v2_checkentry, | |
665 | .destroy = set_target_v2_destroy, | |
d956798d JK |
666 | .me = THIS_MODULE |
667 | }, | |
76cea410 AD |
668 | /* --map-set support */ |
669 | { | |
670 | .name = "SET", | |
671 | .revision = 3, | |
672 | .family = NFPROTO_IPV4, | |
673 | .target = set_target_v3, | |
674 | .targetsize = sizeof(struct xt_set_info_target_v3), | |
675 | .checkentry = set_target_v3_checkentry, | |
676 | .destroy = set_target_v3_destroy, | |
677 | .me = THIS_MODULE | |
678 | }, | |
679 | { | |
680 | .name = "SET", | |
681 | .revision = 3, | |
682 | .family = NFPROTO_IPV6, | |
683 | .target = set_target_v3, | |
684 | .targetsize = sizeof(struct xt_set_info_target_v3), | |
685 | .checkentry = set_target_v3_checkentry, | |
686 | .destroy = set_target_v3_destroy, | |
687 | .me = THIS_MODULE | |
688 | }, | |
d956798d JK |
689 | }; |
690 | ||
691 | static int __init xt_set_init(void) | |
692 | { | |
693 | int ret = xt_register_matches(set_matches, ARRAY_SIZE(set_matches)); | |
694 | ||
695 | if (!ret) { | |
696 | ret = xt_register_targets(set_targets, | |
697 | ARRAY_SIZE(set_targets)); | |
698 | if (ret) | |
699 | xt_unregister_matches(set_matches, | |
700 | ARRAY_SIZE(set_matches)); | |
701 | } | |
702 | return ret; | |
703 | } | |
704 | ||
705 | static void __exit xt_set_fini(void) | |
706 | { | |
707 | xt_unregister_matches(set_matches, ARRAY_SIZE(set_matches)); | |
708 | xt_unregister_targets(set_targets, ARRAY_SIZE(set_targets)); | |
709 | } | |
710 | ||
711 | module_init(xt_set_init); | |
712 | module_exit(xt_set_fini); |