1 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 * Patrick Schaaf <bof@bof.de>
3 * Martin Josefsson <gandalf@wlug.westbo.se>
4 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
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.
11 /* Kernel module which implements the set match and SET target
12 * for netfilter/iptables. */
14 #include <linux/module.h>
15 #include <linux/skbuff.h>
17 #include <linux/netfilter/x_tables.h>
18 #include <linux/netfilter/xt_set.h>
19 #include <linux/netfilter/ipset/ip_set_timeout.h>
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
23 MODULE_DESCRIPTION("Xtables: IP set match and target module");
24 MODULE_ALIAS("xt_SET");
25 MODULE_ALIAS("ipt_set");
26 MODULE_ALIAS("ip6t_set");
27 MODULE_ALIAS("ipt_SET");
28 MODULE_ALIAS("ip6t_SET");
31 match_set(ip_set_id_t index
, const struct sk_buff
*skb
,
32 const struct xt_action_param
*par
,
33 struct ip_set_adt_opt
*opt
, int inv
)
35 if (ip_set_test(index
, skb
, par
, opt
))
40 #define ADT_OPT(n, f, d, fs, cfs, t) \
41 struct ip_set_adt_opt n = { \
49 /* Revision 0 interface: backward compatible with netfilter/iptables */
52 set_match_v0(const struct sk_buff
*skb
, struct xt_action_param
*par
)
54 const struct xt_set_info_match_v0
*info
= par
->matchinfo
;
55 ADT_OPT(opt
, par
->family
, info
->match_set
.u
.compat
.dim
,
56 info
->match_set
.u
.compat
.flags
, 0, UINT_MAX
);
58 return match_set(info
->match_set
.index
, skb
, par
, &opt
,
59 info
->match_set
.u
.compat
.flags
& IPSET_INV_MATCH
);
63 compat_flags(struct xt_set_info_v0
*info
)
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
++) {
73 if (info
->u
.flags
[i
] & IPSET_SRC
)
74 info
->u
.compat
.flags
|= (1<<info
->u
.compat
.dim
);
79 set_match_v0_checkentry(const struct xt_mtchk_param
*par
)
81 struct xt_set_info_match_v0
*info
= par
->matchinfo
;
84 index
= ip_set_nfnl_get_byindex(par
->net
, info
->match_set
.index
);
86 if (index
== IPSET_INVALID_ID
) {
87 pr_warning("Cannot find set identified by id %u to match\n",
88 info
->match_set
.index
);
91 if (info
->match_set
.u
.flags
[IPSET_DIM_MAX
-1] != 0) {
92 pr_warning("Protocol error: set match dimension "
93 "is over the limit!\n");
94 ip_set_nfnl_put(par
->net
, info
->match_set
.index
);
98 /* Fill out compatibility data */
99 compat_flags(&info
->match_set
);
105 set_match_v0_destroy(const struct xt_mtdtor_param
*par
)
107 struct xt_set_info_match_v0
*info
= par
->matchinfo
;
109 ip_set_nfnl_put(par
->net
, info
->match_set
.index
);
112 /* Revision 1 match */
115 set_match_v1(const struct sk_buff
*skb
, struct xt_action_param
*par
)
117 const struct xt_set_info_match_v1
*info
= par
->matchinfo
;
118 ADT_OPT(opt
, par
->family
, info
->match_set
.dim
,
119 info
->match_set
.flags
, 0, UINT_MAX
);
121 if (opt
.flags
& IPSET_RETURN_NOMATCH
)
122 opt
.cmdflags
|= IPSET_FLAG_RETURN_NOMATCH
;
124 return match_set(info
->match_set
.index
, skb
, par
, &opt
,
125 info
->match_set
.flags
& IPSET_INV_MATCH
);
129 set_match_v1_checkentry(const struct xt_mtchk_param
*par
)
131 struct xt_set_info_match_v1
*info
= par
->matchinfo
;
134 index
= ip_set_nfnl_get_byindex(par
->net
, info
->match_set
.index
);
136 if (index
== IPSET_INVALID_ID
) {
137 pr_warning("Cannot find set identified by id %u to match\n",
138 info
->match_set
.index
);
141 if (info
->match_set
.dim
> IPSET_DIM_MAX
) {
142 pr_warning("Protocol error: set match dimension "
143 "is over the limit!\n");
144 ip_set_nfnl_put(par
->net
, info
->match_set
.index
);
152 set_match_v1_destroy(const struct xt_mtdtor_param
*par
)
154 struct xt_set_info_match_v1
*info
= par
->matchinfo
;
156 ip_set_nfnl_put(par
->net
, info
->match_set
.index
);
159 /* Revision 3 match */
162 match_counter(u64 counter
, const struct ip_set_counter_match
*info
)
165 case IPSET_COUNTER_NONE
:
167 case IPSET_COUNTER_EQ
:
168 return counter
== info
->value
;
169 case IPSET_COUNTER_NE
:
170 return counter
!= info
->value
;
171 case IPSET_COUNTER_LT
:
172 return counter
< info
->value
;
173 case IPSET_COUNTER_GT
:
174 return counter
> info
->value
;
180 set_match_v3(const struct sk_buff
*skb
, struct xt_action_param
*par
)
182 const struct xt_set_info_match_v3
*info
= par
->matchinfo
;
183 ADT_OPT(opt
, par
->family
, info
->match_set
.dim
,
184 info
->match_set
.flags
, info
->flags
, UINT_MAX
);
187 if (info
->packets
.op
!= IPSET_COUNTER_NONE
||
188 info
->bytes
.op
!= IPSET_COUNTER_NONE
)
189 opt
.cmdflags
|= IPSET_FLAG_MATCH_COUNTERS
;
191 ret
= match_set(info
->match_set
.index
, skb
, par
, &opt
,
192 info
->match_set
.flags
& IPSET_INV_MATCH
);
194 if (!(ret
&& opt
.cmdflags
& IPSET_FLAG_MATCH_COUNTERS
))
197 if (!match_counter(opt
.ext
.packets
, &info
->packets
))
199 return match_counter(opt
.ext
.bytes
, &info
->bytes
);
202 #define set_match_v3_checkentry set_match_v1_checkentry
203 #define set_match_v3_destroy set_match_v1_destroy
205 /* Revision 0 interface: backward compatible with netfilter/iptables */
208 set_target_v0(struct sk_buff
*skb
, const struct xt_action_param
*par
)
210 const struct xt_set_info_target_v0
*info
= par
->targinfo
;
211 ADT_OPT(add_opt
, par
->family
, info
->add_set
.u
.compat
.dim
,
212 info
->add_set
.u
.compat
.flags
, 0, UINT_MAX
);
213 ADT_OPT(del_opt
, par
->family
, info
->del_set
.u
.compat
.dim
,
214 info
->del_set
.u
.compat
.flags
, 0, UINT_MAX
);
216 if (info
->add_set
.index
!= IPSET_INVALID_ID
)
217 ip_set_add(info
->add_set
.index
, skb
, par
, &add_opt
);
218 if (info
->del_set
.index
!= IPSET_INVALID_ID
)
219 ip_set_del(info
->del_set
.index
, skb
, par
, &del_opt
);
225 set_target_v0_checkentry(const struct xt_tgchk_param
*par
)
227 struct xt_set_info_target_v0
*info
= par
->targinfo
;
230 if (info
->add_set
.index
!= IPSET_INVALID_ID
) {
231 index
= ip_set_nfnl_get_byindex(par
->net
, info
->add_set
.index
);
232 if (index
== IPSET_INVALID_ID
) {
233 pr_warning("Cannot find add_set index %u as target\n",
234 info
->add_set
.index
);
239 if (info
->del_set
.index
!= IPSET_INVALID_ID
) {
240 index
= ip_set_nfnl_get_byindex(par
->net
, info
->del_set
.index
);
241 if (index
== IPSET_INVALID_ID
) {
242 pr_warning("Cannot find del_set index %u as target\n",
243 info
->del_set
.index
);
244 if (info
->add_set
.index
!= IPSET_INVALID_ID
)
245 ip_set_nfnl_put(par
->net
, info
->add_set
.index
);
249 if (info
->add_set
.u
.flags
[IPSET_DIM_MAX
-1] != 0 ||
250 info
->del_set
.u
.flags
[IPSET_DIM_MAX
-1] != 0) {
251 pr_warning("Protocol error: SET target dimension "
252 "is over the limit!\n");
253 if (info
->add_set
.index
!= IPSET_INVALID_ID
)
254 ip_set_nfnl_put(par
->net
, info
->add_set
.index
);
255 if (info
->del_set
.index
!= IPSET_INVALID_ID
)
256 ip_set_nfnl_put(par
->net
, info
->del_set
.index
);
260 /* Fill out compatibility data */
261 compat_flags(&info
->add_set
);
262 compat_flags(&info
->del_set
);
268 set_target_v0_destroy(const struct xt_tgdtor_param
*par
)
270 const struct xt_set_info_target_v0
*info
= par
->targinfo
;
272 if (info
->add_set
.index
!= IPSET_INVALID_ID
)
273 ip_set_nfnl_put(par
->net
, info
->add_set
.index
);
274 if (info
->del_set
.index
!= IPSET_INVALID_ID
)
275 ip_set_nfnl_put(par
->net
, info
->del_set
.index
);
278 /* Revision 1 target */
281 set_target_v1(struct sk_buff
*skb
, const struct xt_action_param
*par
)
283 const struct xt_set_info_target_v1
*info
= par
->targinfo
;
284 ADT_OPT(add_opt
, par
->family
, info
->add_set
.dim
,
285 info
->add_set
.flags
, 0, UINT_MAX
);
286 ADT_OPT(del_opt
, par
->family
, info
->del_set
.dim
,
287 info
->del_set
.flags
, 0, UINT_MAX
);
289 if (info
->add_set
.index
!= IPSET_INVALID_ID
)
290 ip_set_add(info
->add_set
.index
, skb
, par
, &add_opt
);
291 if (info
->del_set
.index
!= IPSET_INVALID_ID
)
292 ip_set_del(info
->del_set
.index
, skb
, par
, &del_opt
);
298 set_target_v1_checkentry(const struct xt_tgchk_param
*par
)
300 const struct xt_set_info_target_v1
*info
= par
->targinfo
;
303 if (info
->add_set
.index
!= IPSET_INVALID_ID
) {
304 index
= ip_set_nfnl_get_byindex(par
->net
, info
->add_set
.index
);
305 if (index
== IPSET_INVALID_ID
) {
306 pr_warning("Cannot find add_set index %u as target\n",
307 info
->add_set
.index
);
312 if (info
->del_set
.index
!= IPSET_INVALID_ID
) {
313 index
= ip_set_nfnl_get_byindex(par
->net
, info
->del_set
.index
);
314 if (index
== IPSET_INVALID_ID
) {
315 pr_warning("Cannot find del_set index %u as target\n",
316 info
->del_set
.index
);
317 if (info
->add_set
.index
!= IPSET_INVALID_ID
)
318 ip_set_nfnl_put(par
->net
, info
->add_set
.index
);
322 if (info
->add_set
.dim
> IPSET_DIM_MAX
||
323 info
->del_set
.dim
> IPSET_DIM_MAX
) {
324 pr_warning("Protocol error: SET target dimension "
325 "is over the limit!\n");
326 if (info
->add_set
.index
!= IPSET_INVALID_ID
)
327 ip_set_nfnl_put(par
->net
, info
->add_set
.index
);
328 if (info
->del_set
.index
!= IPSET_INVALID_ID
)
329 ip_set_nfnl_put(par
->net
, info
->del_set
.index
);
337 set_target_v1_destroy(const struct xt_tgdtor_param
*par
)
339 const struct xt_set_info_target_v1
*info
= par
->targinfo
;
341 if (info
->add_set
.index
!= IPSET_INVALID_ID
)
342 ip_set_nfnl_put(par
->net
, info
->add_set
.index
);
343 if (info
->del_set
.index
!= IPSET_INVALID_ID
)
344 ip_set_nfnl_put(par
->net
, info
->del_set
.index
);
347 /* Revision 2 target */
350 set_target_v2(struct sk_buff
*skb
, const struct xt_action_param
*par
)
352 const struct xt_set_info_target_v2
*info
= par
->targinfo
;
353 ADT_OPT(add_opt
, par
->family
, info
->add_set
.dim
,
354 info
->add_set
.flags
, info
->flags
, info
->timeout
);
355 ADT_OPT(del_opt
, par
->family
, info
->del_set
.dim
,
356 info
->del_set
.flags
, 0, UINT_MAX
);
358 /* Normalize to fit into jiffies */
359 if (add_opt
.ext
.timeout
!= IPSET_NO_TIMEOUT
&&
360 add_opt
.ext
.timeout
> UINT_MAX
/MSEC_PER_SEC
)
361 add_opt
.ext
.timeout
= UINT_MAX
/MSEC_PER_SEC
;
362 if (info
->add_set
.index
!= IPSET_INVALID_ID
)
363 ip_set_add(info
->add_set
.index
, skb
, par
, &add_opt
);
364 if (info
->del_set
.index
!= IPSET_INVALID_ID
)
365 ip_set_del(info
->del_set
.index
, skb
, par
, &del_opt
);
370 #define set_target_v2_checkentry set_target_v1_checkentry
371 #define set_target_v2_destroy set_target_v1_destroy
373 static struct xt_match set_matches
[] __read_mostly
= {
376 .family
= NFPROTO_IPV4
,
378 .match
= set_match_v0
,
379 .matchsize
= sizeof(struct xt_set_info_match_v0
),
380 .checkentry
= set_match_v0_checkentry
,
381 .destroy
= set_match_v0_destroy
,
386 .family
= NFPROTO_IPV4
,
388 .match
= set_match_v1
,
389 .matchsize
= sizeof(struct xt_set_info_match_v1
),
390 .checkentry
= set_match_v1_checkentry
,
391 .destroy
= set_match_v1_destroy
,
396 .family
= NFPROTO_IPV6
,
398 .match
= set_match_v1
,
399 .matchsize
= sizeof(struct xt_set_info_match_v1
),
400 .checkentry
= set_match_v1_checkentry
,
401 .destroy
= set_match_v1_destroy
,
404 /* --return-nomatch flag support */
407 .family
= NFPROTO_IPV4
,
409 .match
= set_match_v1
,
410 .matchsize
= sizeof(struct xt_set_info_match_v1
),
411 .checkentry
= set_match_v1_checkentry
,
412 .destroy
= set_match_v1_destroy
,
417 .family
= NFPROTO_IPV6
,
419 .match
= set_match_v1
,
420 .matchsize
= sizeof(struct xt_set_info_match_v1
),
421 .checkentry
= set_match_v1_checkentry
,
422 .destroy
= set_match_v1_destroy
,
425 /* counters support: update, match */
428 .family
= NFPROTO_IPV4
,
430 .match
= set_match_v3
,
431 .matchsize
= sizeof(struct xt_set_info_match_v3
),
432 .checkentry
= set_match_v3_checkentry
,
433 .destroy
= set_match_v3_destroy
,
438 .family
= NFPROTO_IPV6
,
440 .match
= set_match_v3
,
441 .matchsize
= sizeof(struct xt_set_info_match_v3
),
442 .checkentry
= set_match_v3_checkentry
,
443 .destroy
= set_match_v3_destroy
,
448 static struct xt_target set_targets
[] __read_mostly
= {
452 .family
= NFPROTO_IPV4
,
453 .target
= set_target_v0
,
454 .targetsize
= sizeof(struct xt_set_info_target_v0
),
455 .checkentry
= set_target_v0_checkentry
,
456 .destroy
= set_target_v0_destroy
,
462 .family
= NFPROTO_IPV4
,
463 .target
= set_target_v1
,
464 .targetsize
= sizeof(struct xt_set_info_target_v1
),
465 .checkentry
= set_target_v1_checkentry
,
466 .destroy
= set_target_v1_destroy
,
472 .family
= NFPROTO_IPV6
,
473 .target
= set_target_v1
,
474 .targetsize
= sizeof(struct xt_set_info_target_v1
),
475 .checkentry
= set_target_v1_checkentry
,
476 .destroy
= set_target_v1_destroy
,
479 /* --timeout and --exist flags support */
483 .family
= NFPROTO_IPV4
,
484 .target
= set_target_v2
,
485 .targetsize
= sizeof(struct xt_set_info_target_v2
),
486 .checkentry
= set_target_v2_checkentry
,
487 .destroy
= set_target_v2_destroy
,
493 .family
= NFPROTO_IPV6
,
494 .target
= set_target_v2
,
495 .targetsize
= sizeof(struct xt_set_info_target_v2
),
496 .checkentry
= set_target_v2_checkentry
,
497 .destroy
= set_target_v2_destroy
,
502 static int __init
xt_set_init(void)
504 int ret
= xt_register_matches(set_matches
, ARRAY_SIZE(set_matches
));
507 ret
= xt_register_targets(set_targets
,
508 ARRAY_SIZE(set_targets
));
510 xt_unregister_matches(set_matches
,
511 ARRAY_SIZE(set_matches
));
516 static void __exit
xt_set_fini(void)
518 xt_unregister_matches(set_matches
, ARRAY_SIZE(set_matches
));
519 xt_unregister_targets(set_targets
, ARRAY_SIZE(set_targets
));
522 module_init(xt_set_init
);
523 module_exit(xt_set_fini
);