netfilter: ipset: Add skbinfo extension kernel support for the list set type.
[deliverable/linux.git] / net / netfilter / xt_set.c
CommitLineData
d956798d
JK
1/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 * Patrick Schaaf <bof@bof.de>
3 * Martin Josefsson <gandalf@wlug.westbo.se>
075e64c0 4 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
d956798d
JK
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11/* Kernel module which implements the set match and SET target
12 * for netfilter/iptables. */
13
14#include <linux/module.h>
15#include <linux/skbuff.h>
d956798d
JK
16
17#include <linux/netfilter/x_tables.h>
18#include <linux/netfilter/xt_set.h>
a73f89a6 19#include <linux/netfilter/ipset/ip_set_timeout.h>
d956798d
JK
20
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
23MODULE_DESCRIPTION("Xtables: IP set match and target module");
24MODULE_ALIAS("xt_SET");
25MODULE_ALIAS("ipt_set");
26MODULE_ALIAS("ip6t_set");
27MODULE_ALIAS("ipt_SET");
28MODULE_ALIAS("ip6t_SET");
29
30static inline int
31match_set(ip_set_id_t index, const struct sk_buff *skb,
b66554cf 32 const struct xt_action_param *par,
075e64c0 33 struct ip_set_adt_opt *opt, int inv)
d956798d 34{
b66554cf 35 if (ip_set_test(index, skb, par, opt))
d956798d
JK
36 inv = !inv;
37 return inv;
38}
39
15b4d93f 40#define ADT_OPT(n, f, d, fs, cfs, t) \
127f5591
JK
41struct ip_set_adt_opt n = { \
42 .family = f, \
43 .dim = d, \
44 .flags = fs, \
45 .cmdflags = cfs, \
075e64c0 46 .ext.timeout = t, \
127f5591 47}
ac8cc925 48
d956798d
JK
49/* Revision 0 interface: backward compatible with netfilter/iptables */
50
51static bool
52set_match_v0(const struct sk_buff *skb, struct xt_action_param *par)
53{
54 const struct xt_set_info_match_v0 *info = par->matchinfo;
ac8cc925
JK
55 ADT_OPT(opt, par->family, info->match_set.u.compat.dim,
56 info->match_set.u.compat.flags, 0, UINT_MAX);
d956798d 57
b66554cf 58 return match_set(info->match_set.index, skb, par, &opt,
d956798d
JK
59 info->match_set.u.compat.flags & IPSET_INV_MATCH);
60}
61
62static void
63compat_flags(struct xt_set_info_v0 *info)
64{
65 u_int8_t i;
66
67 /* Fill out compatibility data according to enum ip_set_kopt */
68 info->u.compat.dim = IPSET_DIM_ZERO;
69 if (info->u.flags[0] & IPSET_MATCH_INV)
70 info->u.compat.flags |= IPSET_INV_MATCH;
71 for (i = 0; i < IPSET_DIM_MAX-1 && info->u.flags[i]; i++) {
72 info->u.compat.dim++;
73 if (info->u.flags[i] & IPSET_SRC)
74 info->u.compat.flags |= (1<<info->u.compat.dim);
75 }
76}
77
78static int
79set_match_v0_checkentry(const struct xt_mtchk_param *par)
80{
81 struct xt_set_info_match_v0 *info = par->matchinfo;
82 ip_set_id_t index;
83
1785e8f4 84 index = ip_set_nfnl_get_byindex(par->net, info->match_set.index);
d956798d
JK
85
86 if (index == IPSET_INVALID_ID) {
b167a37c
JP
87 pr_warn("Cannot find set identified by id %u to match\n",
88 info->match_set.index);
d956798d
JK
89 return -ENOENT;
90 }
91 if (info->match_set.u.flags[IPSET_DIM_MAX-1] != 0) {
b167a37c 92 pr_warn("Protocol error: set match dimension is over the limit!\n");
1785e8f4 93 ip_set_nfnl_put(par->net, info->match_set.index);
d956798d
JK
94 return -ERANGE;
95 }
96
97 /* Fill out compatibility data */
98 compat_flags(&info->match_set);
99
100 return 0;
101}
102
103static void
104set_match_v0_destroy(const struct xt_mtdtor_param *par)
105{
106 struct xt_set_info_match_v0 *info = par->matchinfo;
107
1785e8f4 108 ip_set_nfnl_put(par->net, info->match_set.index);
d956798d
JK
109}
110
bd3129fc
JK
111/* Revision 1 match */
112
113static bool
114set_match_v1(const struct sk_buff *skb, struct xt_action_param *par)
115{
116 const struct xt_set_info_match_v1 *info = par->matchinfo;
117 ADT_OPT(opt, par->family, info->match_set.dim,
118 info->match_set.flags, 0, UINT_MAX);
119
120 if (opt.flags & IPSET_RETURN_NOMATCH)
121 opt.cmdflags |= IPSET_FLAG_RETURN_NOMATCH;
122
123 return match_set(info->match_set.index, skb, par, &opt,
124 info->match_set.flags & IPSET_INV_MATCH);
125}
126
127static int
128set_match_v1_checkentry(const struct xt_mtchk_param *par)
129{
130 struct xt_set_info_match_v1 *info = par->matchinfo;
131 ip_set_id_t index;
132
1785e8f4 133 index = ip_set_nfnl_get_byindex(par->net, info->match_set.index);
bd3129fc
JK
134
135 if (index == IPSET_INVALID_ID) {
b167a37c
JP
136 pr_warn("Cannot find set identified by id %u to match\n",
137 info->match_set.index);
bd3129fc
JK
138 return -ENOENT;
139 }
140 if (info->match_set.dim > IPSET_DIM_MAX) {
b167a37c 141 pr_warn("Protocol error: set match dimension is over the limit!\n");
1785e8f4 142 ip_set_nfnl_put(par->net, info->match_set.index);
bd3129fc
JK
143 return -ERANGE;
144 }
145
146 return 0;
147}
148
149static void
150set_match_v1_destroy(const struct xt_mtdtor_param *par)
151{
152 struct xt_set_info_match_v1 *info = par->matchinfo;
153
1785e8f4 154 ip_set_nfnl_put(par->net, info->match_set.index);
bd3129fc
JK
155}
156
157/* Revision 3 match */
158
159static bool
160match_counter(u64 counter, const struct ip_set_counter_match *info)
161{
162 switch (info->op) {
163 case IPSET_COUNTER_NONE:
164 return true;
165 case IPSET_COUNTER_EQ:
166 return counter == info->value;
167 case IPSET_COUNTER_NE:
168 return counter != info->value;
169 case IPSET_COUNTER_LT:
170 return counter < info->value;
171 case IPSET_COUNTER_GT:
172 return counter > info->value;
173 }
174 return false;
175}
176
177static bool
178set_match_v3(const struct sk_buff *skb, struct xt_action_param *par)
179{
180 const struct xt_set_info_match_v3 *info = par->matchinfo;
181 ADT_OPT(opt, par->family, info->match_set.dim,
182 info->match_set.flags, info->flags, UINT_MAX);
183 int ret;
184
185 if (info->packets.op != IPSET_COUNTER_NONE ||
186 info->bytes.op != IPSET_COUNTER_NONE)
187 opt.cmdflags |= IPSET_FLAG_MATCH_COUNTERS;
188
189 ret = match_set(info->match_set.index, skb, par, &opt,
190 info->match_set.flags & IPSET_INV_MATCH);
191
192 if (!(ret && opt.cmdflags & IPSET_FLAG_MATCH_COUNTERS))
193 return ret;
194
195 if (!match_counter(opt.ext.packets, &info->packets))
196 return 0;
197 return match_counter(opt.ext.bytes, &info->bytes);
198}
199
200#define set_match_v3_checkentry set_match_v1_checkentry
201#define set_match_v3_destroy set_match_v1_destroy
202
203/* Revision 0 interface: backward compatible with netfilter/iptables */
204
d956798d
JK
205static unsigned int
206set_target_v0(struct sk_buff *skb, const struct xt_action_param *par)
207{
208 const struct xt_set_info_target_v0 *info = par->targinfo;
ac8cc925
JK
209 ADT_OPT(add_opt, par->family, info->add_set.u.compat.dim,
210 info->add_set.u.compat.flags, 0, UINT_MAX);
211 ADT_OPT(del_opt, par->family, info->del_set.u.compat.dim,
212 info->del_set.u.compat.flags, 0, UINT_MAX);
d956798d
JK
213
214 if (info->add_set.index != IPSET_INVALID_ID)
b66554cf 215 ip_set_add(info->add_set.index, skb, par, &add_opt);
d956798d 216 if (info->del_set.index != IPSET_INVALID_ID)
b66554cf 217 ip_set_del(info->del_set.index, skb, par, &del_opt);
d956798d
JK
218
219 return XT_CONTINUE;
220}
221
222static int
223set_target_v0_checkentry(const struct xt_tgchk_param *par)
224{
225 struct xt_set_info_target_v0 *info = par->targinfo;
226 ip_set_id_t index;
227
228 if (info->add_set.index != IPSET_INVALID_ID) {
1785e8f4 229 index = ip_set_nfnl_get_byindex(par->net, info->add_set.index);
d956798d 230 if (index == IPSET_INVALID_ID) {
b167a37c
JP
231 pr_warn("Cannot find add_set index %u as target\n",
232 info->add_set.index);
d956798d
JK
233 return -ENOENT;
234 }
235 }
236
237 if (info->del_set.index != IPSET_INVALID_ID) {
1785e8f4 238 index = ip_set_nfnl_get_byindex(par->net, info->del_set.index);
d956798d 239 if (index == IPSET_INVALID_ID) {
b167a37c
JP
240 pr_warn("Cannot find del_set index %u as target\n",
241 info->del_set.index);
eafbd3fd 242 if (info->add_set.index != IPSET_INVALID_ID)
1785e8f4 243 ip_set_nfnl_put(par->net, info->add_set.index);
d956798d
JK
244 return -ENOENT;
245 }
246 }
247 if (info->add_set.u.flags[IPSET_DIM_MAX-1] != 0 ||
248 info->del_set.u.flags[IPSET_DIM_MAX-1] != 0) {
b167a37c 249 pr_warn("Protocol error: SET target dimension is over the limit!\n");
eafbd3fd 250 if (info->add_set.index != IPSET_INVALID_ID)
1785e8f4 251 ip_set_nfnl_put(par->net, info->add_set.index);
eafbd3fd 252 if (info->del_set.index != IPSET_INVALID_ID)
1785e8f4 253 ip_set_nfnl_put(par->net, info->del_set.index);
d956798d
JK
254 return -ERANGE;
255 }
256
257 /* Fill out compatibility data */
258 compat_flags(&info->add_set);
259 compat_flags(&info->del_set);
260
261 return 0;
262}
263
264static void
265set_target_v0_destroy(const struct xt_tgdtor_param *par)
266{
267 const struct xt_set_info_target_v0 *info = par->targinfo;
268
269 if (info->add_set.index != IPSET_INVALID_ID)
1785e8f4 270 ip_set_nfnl_put(par->net, info->add_set.index);
d956798d 271 if (info->del_set.index != IPSET_INVALID_ID)
1785e8f4 272 ip_set_nfnl_put(par->net, info->del_set.index);
d956798d
JK
273}
274
bd3129fc 275/* Revision 1 target */
d956798d
JK
276
277static unsigned int
ac8cc925 278set_target_v1(struct sk_buff *skb, const struct xt_action_param *par)
d956798d 279{
ac8cc925
JK
280 const struct xt_set_info_target_v1 *info = par->targinfo;
281 ADT_OPT(add_opt, par->family, info->add_set.dim,
282 info->add_set.flags, 0, UINT_MAX);
283 ADT_OPT(del_opt, par->family, info->del_set.dim,
284 info->del_set.flags, 0, UINT_MAX);
d956798d
JK
285
286 if (info->add_set.index != IPSET_INVALID_ID)
b66554cf 287 ip_set_add(info->add_set.index, skb, par, &add_opt);
d956798d 288 if (info->del_set.index != IPSET_INVALID_ID)
b66554cf 289 ip_set_del(info->del_set.index, skb, par, &del_opt);
d956798d
JK
290
291 return XT_CONTINUE;
292}
293
294static int
ac8cc925 295set_target_v1_checkentry(const struct xt_tgchk_param *par)
d956798d 296{
ac8cc925 297 const struct xt_set_info_target_v1 *info = par->targinfo;
d956798d
JK
298 ip_set_id_t index;
299
300 if (info->add_set.index != IPSET_INVALID_ID) {
1785e8f4 301 index = ip_set_nfnl_get_byindex(par->net, info->add_set.index);
d956798d 302 if (index == IPSET_INVALID_ID) {
b167a37c
JP
303 pr_warn("Cannot find add_set index %u as target\n",
304 info->add_set.index);
d956798d
JK
305 return -ENOENT;
306 }
307 }
308
309 if (info->del_set.index != IPSET_INVALID_ID) {
1785e8f4 310 index = ip_set_nfnl_get_byindex(par->net, info->del_set.index);
d956798d 311 if (index == IPSET_INVALID_ID) {
b167a37c
JP
312 pr_warn("Cannot find del_set index %u as target\n",
313 info->del_set.index);
eafbd3fd 314 if (info->add_set.index != IPSET_INVALID_ID)
1785e8f4 315 ip_set_nfnl_put(par->net, info->add_set.index);
d956798d
JK
316 return -ENOENT;
317 }
318 }
319 if (info->add_set.dim > IPSET_DIM_MAX ||
eafbd3fd 320 info->del_set.dim > IPSET_DIM_MAX) {
b167a37c 321 pr_warn("Protocol error: SET target dimension is over the limit!\n");
eafbd3fd 322 if (info->add_set.index != IPSET_INVALID_ID)
1785e8f4 323 ip_set_nfnl_put(par->net, info->add_set.index);
eafbd3fd 324 if (info->del_set.index != IPSET_INVALID_ID)
1785e8f4 325 ip_set_nfnl_put(par->net, info->del_set.index);
d956798d
JK
326 return -ERANGE;
327 }
328
329 return 0;
330}
331
332static void
ac8cc925 333set_target_v1_destroy(const struct xt_tgdtor_param *par)
d956798d 334{
ac8cc925 335 const struct xt_set_info_target_v1 *info = par->targinfo;
d956798d
JK
336
337 if (info->add_set.index != IPSET_INVALID_ID)
1785e8f4 338 ip_set_nfnl_put(par->net, info->add_set.index);
d956798d 339 if (info->del_set.index != IPSET_INVALID_ID)
1785e8f4 340 ip_set_nfnl_put(par->net, info->del_set.index);
d956798d
JK
341}
342
ac8cc925
JK
343/* Revision 2 target */
344
345static unsigned int
346set_target_v2(struct sk_buff *skb, const struct xt_action_param *par)
347{
348 const struct xt_set_info_target_v2 *info = par->targinfo;
075e64c0
JK
349 ADT_OPT(add_opt, par->family, info->add_set.dim,
350 info->add_set.flags, info->flags, info->timeout);
ac8cc925
JK
351 ADT_OPT(del_opt, par->family, info->del_set.dim,
352 info->del_set.flags, 0, UINT_MAX);
353
127f5591 354 /* Normalize to fit into jiffies */
075e64c0
JK
355 if (add_opt.ext.timeout != IPSET_NO_TIMEOUT &&
356 add_opt.ext.timeout > UINT_MAX/MSEC_PER_SEC)
357 add_opt.ext.timeout = UINT_MAX/MSEC_PER_SEC;
ac8cc925 358 if (info->add_set.index != IPSET_INVALID_ID)
b66554cf 359 ip_set_add(info->add_set.index, skb, par, &add_opt);
ac8cc925 360 if (info->del_set.index != IPSET_INVALID_ID)
b66554cf 361 ip_set_del(info->del_set.index, skb, par, &del_opt);
ac8cc925
JK
362
363 return XT_CONTINUE;
364}
365
366#define set_target_v2_checkentry set_target_v1_checkentry
367#define set_target_v2_destroy set_target_v1_destroy
368
d956798d
JK
369static struct xt_match set_matches[] __read_mostly = {
370 {
371 .name = "set",
372 .family = NFPROTO_IPV4,
373 .revision = 0,
374 .match = set_match_v0,
375 .matchsize = sizeof(struct xt_set_info_match_v0),
376 .checkentry = set_match_v0_checkentry,
377 .destroy = set_match_v0_destroy,
378 .me = THIS_MODULE
379 },
380 {
381 .name = "set",
382 .family = NFPROTO_IPV4,
383 .revision = 1,
ac8cc925
JK
384 .match = set_match_v1,
385 .matchsize = sizeof(struct xt_set_info_match_v1),
386 .checkentry = set_match_v1_checkentry,
387 .destroy = set_match_v1_destroy,
d956798d
JK
388 .me = THIS_MODULE
389 },
390 {
391 .name = "set",
392 .family = NFPROTO_IPV6,
393 .revision = 1,
ac8cc925
JK
394 .match = set_match_v1,
395 .matchsize = sizeof(struct xt_set_info_match_v1),
396 .checkentry = set_match_v1_checkentry,
397 .destroy = set_match_v1_destroy,
d956798d
JK
398 .me = THIS_MODULE
399 },
3e0304a5
JK
400 /* --return-nomatch flag support */
401 {
402 .name = "set",
403 .family = NFPROTO_IPV4,
404 .revision = 2,
405 .match = set_match_v1,
406 .matchsize = sizeof(struct xt_set_info_match_v1),
407 .checkentry = set_match_v1_checkentry,
408 .destroy = set_match_v1_destroy,
409 .me = THIS_MODULE
410 },
411 {
412 .name = "set",
413 .family = NFPROTO_IPV6,
414 .revision = 2,
415 .match = set_match_v1,
416 .matchsize = sizeof(struct xt_set_info_match_v1),
417 .checkentry = set_match_v1_checkentry,
418 .destroy = set_match_v1_destroy,
419 .me = THIS_MODULE
420 },
6e01781d
JK
421 /* counters support: update, match */
422 {
423 .name = "set",
424 .family = NFPROTO_IPV4,
425 .revision = 3,
426 .match = set_match_v3,
427 .matchsize = sizeof(struct xt_set_info_match_v3),
428 .checkentry = set_match_v3_checkentry,
429 .destroy = set_match_v3_destroy,
430 .me = THIS_MODULE
431 },
432 {
433 .name = "set",
434 .family = NFPROTO_IPV6,
435 .revision = 3,
436 .match = set_match_v3,
437 .matchsize = sizeof(struct xt_set_info_match_v3),
438 .checkentry = set_match_v3_checkentry,
439 .destroy = set_match_v3_destroy,
440 .me = THIS_MODULE
441 },
d956798d
JK
442};
443
444static struct xt_target set_targets[] __read_mostly = {
445 {
446 .name = "SET",
447 .revision = 0,
448 .family = NFPROTO_IPV4,
449 .target = set_target_v0,
450 .targetsize = sizeof(struct xt_set_info_target_v0),
451 .checkentry = set_target_v0_checkentry,
452 .destroy = set_target_v0_destroy,
453 .me = THIS_MODULE
454 },
455 {
456 .name = "SET",
457 .revision = 1,
458 .family = NFPROTO_IPV4,
ac8cc925
JK
459 .target = set_target_v1,
460 .targetsize = sizeof(struct xt_set_info_target_v1),
461 .checkentry = set_target_v1_checkentry,
462 .destroy = set_target_v1_destroy,
d956798d
JK
463 .me = THIS_MODULE
464 },
465 {
466 .name = "SET",
467 .revision = 1,
468 .family = NFPROTO_IPV6,
ac8cc925
JK
469 .target = set_target_v1,
470 .targetsize = sizeof(struct xt_set_info_target_v1),
471 .checkentry = set_target_v1_checkentry,
472 .destroy = set_target_v1_destroy,
473 .me = THIS_MODULE
474 },
3e0304a5 475 /* --timeout and --exist flags support */
ac8cc925
JK
476 {
477 .name = "SET",
478 .revision = 2,
479 .family = NFPROTO_IPV4,
480 .target = set_target_v2,
481 .targetsize = sizeof(struct xt_set_info_target_v2),
482 .checkentry = set_target_v2_checkentry,
483 .destroy = set_target_v2_destroy,
484 .me = THIS_MODULE
485 },
486 {
487 .name = "SET",
488 .revision = 2,
489 .family = NFPROTO_IPV6,
490 .target = set_target_v2,
491 .targetsize = sizeof(struct xt_set_info_target_v2),
492 .checkentry = set_target_v2_checkentry,
493 .destroy = set_target_v2_destroy,
d956798d
JK
494 .me = THIS_MODULE
495 },
496};
497
498static int __init xt_set_init(void)
499{
500 int ret = xt_register_matches(set_matches, ARRAY_SIZE(set_matches));
501
502 if (!ret) {
503 ret = xt_register_targets(set_targets,
504 ARRAY_SIZE(set_targets));
505 if (ret)
506 xt_unregister_matches(set_matches,
507 ARRAY_SIZE(set_matches));
508 }
509 return ret;
510}
511
512static void __exit xt_set_fini(void)
513{
514 xt_unregister_matches(set_matches, ARRAY_SIZE(set_matches));
515 xt_unregister_targets(set_targets, ARRAY_SIZE(set_targets));
516}
517
518module_init(xt_set_init);
519module_exit(xt_set_fini);
This page took 0.223011 seconds and 5 git commands to generate.