1 /* Copyright (C) 2008-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation.
8 /* Kernel module implementing an IP set type: the list:set type */
10 #include <linux/module.h>
12 #include <linux/rculist.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
16 #include <linux/netfilter/ipset/ip_set.h>
17 #include <linux/netfilter/ipset/ip_set_list.h>
19 #define IPSET_TYPE_REV_MIN 0
20 /* 1 Counters support added */
21 /* 2 Comments support added */
22 #define IPSET_TYPE_REV_MAX 3 /* skbinfo support added */
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
26 IP_SET_MODULE_DESC("list:set", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
27 MODULE_ALIAS("ip_set_list:set");
32 struct list_head list;
33 struct ip_set *set; /* Sigh, in order to cleanup reference */
35 } __aligned(__alignof__(u64));
45 u32 size; /* size of set list array */
46 struct timer_list gc; /* garbage collection */
47 struct net *net; /* namespace */
48 struct list_head members; /* the set members */
52 list_set_ktest(struct ip_set *set, const struct sk_buff *skb,
53 const struct xt_action_param *par,
54 struct ip_set_adt_opt *opt, const struct ip_set_ext *ext)
56 struct list_set *map = set->data;
58 u32 cmdflags = opt->cmdflags;
61 /* Don't lookup sub-counters at all */
62 opt->cmdflags &= ~IPSET_FLAG_MATCH_COUNTERS;
63 if (opt->cmdflags & IPSET_FLAG_SKIP_SUBCOUNTER_UPDATE)
64 opt->cmdflags &= ~IPSET_FLAG_SKIP_COUNTER_UPDATE;
65 list_for_each_entry_rcu(e, &map->members, list) {
66 if (SET_WITH_TIMEOUT(set) &&
67 ip_set_timeout_expired(ext_timeout(e, set)))
69 ret = ip_set_test(e->id, skb, par, opt);
71 if (SET_WITH_COUNTER(set))
72 ip_set_update_counter(ext_counter(e, set),
75 if (SET_WITH_SKBINFO(set))
76 ip_set_get_skbinfo(ext_skbinfo(e, set),
86 list_set_kadd(struct ip_set *set, const struct sk_buff *skb,
87 const struct xt_action_param *par,
88 struct ip_set_adt_opt *opt, const struct ip_set_ext *ext)
90 struct list_set *map = set->data;
94 list_for_each_entry(e, &map->members, list) {
95 if (SET_WITH_TIMEOUT(set) &&
96 ip_set_timeout_expired(ext_timeout(e, set)))
98 ret = ip_set_add(e->id, skb, par, opt);
106 list_set_kdel(struct ip_set *set, const struct sk_buff *skb,
107 const struct xt_action_param *par,
108 struct ip_set_adt_opt *opt, const struct ip_set_ext *ext)
110 struct list_set *map = set->data;
114 list_for_each_entry(e, &map->members, list) {
115 if (SET_WITH_TIMEOUT(set) &&
116 ip_set_timeout_expired(ext_timeout(e, set)))
118 ret = ip_set_del(e->id, skb, par, opt);
126 list_set_kadt(struct ip_set *set, const struct sk_buff *skb,
127 const struct xt_action_param *par,
128 enum ipset_adt adt, struct ip_set_adt_opt *opt)
130 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
136 ret = list_set_ktest(set, skb, par, opt, &ext);
139 ret = list_set_kadd(set, skb, par, opt, &ext);
142 ret = list_set_kdel(set, skb, par, opt, &ext);
152 /* Userspace interfaces: we are protected by the nfnl mutex */
155 __list_set_del_rcu(struct rcu_head * rcu)
157 struct set_elem *e = container_of(rcu, struct set_elem, rcu);
158 struct ip_set *set = e->set;
159 struct list_set *map = set->data;
161 ip_set_put_byindex(map->net, e->id);
162 ip_set_ext_destroy(set, e);
167 list_set_del(struct ip_set *set, struct set_elem *e)
170 list_del_rcu(&e->list);
171 call_rcu(&e->rcu, __list_set_del_rcu);
175 list_set_replace(struct set_elem *e, struct set_elem *old)
177 list_replace_rcu(&old->list, &e->list);
178 call_rcu(&old->rcu, __list_set_del_rcu);
182 set_cleanup_entries(struct ip_set *set)
184 struct list_set *map = set->data;
185 struct set_elem *e, *n;
187 list_for_each_entry_safe(e, n, &map->members, list)
188 if (ip_set_timeout_expired(ext_timeout(e, set)))
189 list_set_del(set, e);
193 list_set_utest(struct ip_set *set, void *value, const struct ip_set_ext *ext,
194 struct ip_set_ext *mext, u32 flags)
196 struct list_set *map = set->data;
197 struct set_adt_elem *d = value;
198 struct set_elem *e, *next, *prev = NULL;
201 list_for_each_entry(e, &map->members, list) {
202 if (SET_WITH_TIMEOUT(set) &&
203 ip_set_timeout_expired(ext_timeout(e, set)))
205 else if (e->id != d->id) {
210 if (d->before == 0) {
212 } else if (d->before > 0) {
213 next = list_next_entry(e, list);
214 ret = !list_is_last(&e->list, &map->members) &&
215 next->id == d->refid;
217 ret = prev && prev->id == d->refid;
225 list_set_init_extensions(struct ip_set *set, const struct ip_set_ext *ext,
228 if (SET_WITH_COUNTER(set))
229 ip_set_init_counter(ext_counter(e, set), ext);
230 if (SET_WITH_COMMENT(set))
231 ip_set_init_comment(set, ext_comment(e, set), ext);
232 if (SET_WITH_SKBINFO(set))
233 ip_set_init_skbinfo(ext_skbinfo(e, set), ext);
234 /* Update timeout last */
235 if (SET_WITH_TIMEOUT(set))
236 ip_set_timeout_set(ext_timeout(e, set), ext->timeout);
240 list_set_uadd(struct ip_set *set, void *value, const struct ip_set_ext *ext,
241 struct ip_set_ext *mext, u32 flags)
243 struct list_set *map = set->data;
244 struct set_adt_elem *d = value;
245 struct set_elem *e, *n, *prev, *next;
246 bool flag_exist = flags & IPSET_FLAG_EXIST;
248 /* Find where to add the new entry */
249 n = prev = next = NULL;
250 list_for_each_entry(e, &map->members, list) {
251 if (SET_WITH_TIMEOUT(set) &&
252 ip_set_timeout_expired(ext_timeout(e, set)))
254 else if (d->id == e->id)
256 else if (d->before == 0 || e->id != d->refid)
258 else if (d->before > 0)
263 /* Re-add already existing element */
265 if ((d->before > 0 && !next) ||
266 (d->before < 0 && !prev))
267 return -IPSET_ERR_REF_EXIST;
269 return -IPSET_ERR_EXIST;
270 /* Update extensions */
271 ip_set_ext_destroy(set, n);
272 list_set_init_extensions(set, ext, n);
274 /* Set is already added to the list */
275 ip_set_put_byindex(map->net, d->id);
279 if (d->before == 0) {
281 n = list_empty(&map->members) ? NULL :
282 list_last_entry(&map->members, struct set_elem, list);
283 } else if (d->before > 0) {
284 /* Insert after next element */
285 if (!list_is_last(&next->list, &map->members))
286 n = list_next_entry(next, list);
288 /* Insert before prev element */
289 if (prev->list.prev != &map->members)
290 n = list_prev_entry(prev, list);
292 /* Can we replace a timed out entry? */
294 !(SET_WITH_TIMEOUT(set) &&
295 ip_set_timeout_expired(ext_timeout(n, set))))
298 e = kzalloc(set->dsize, GFP_ATOMIC);
303 INIT_LIST_HEAD(&e->list);
304 list_set_init_extensions(set, ext, e);
306 list_set_replace(e, n);
308 list_add_tail_rcu(&e->list, &next->list);
310 list_add_rcu(&e->list, &prev->list);
312 list_add_tail_rcu(&e->list, &map->members);
319 list_set_udel(struct ip_set *set, void *value, const struct ip_set_ext *ext,
320 struct ip_set_ext *mext, u32 flags)
322 struct list_set *map = set->data;
323 struct set_adt_elem *d = value;
324 struct set_elem *e, *next, *prev = NULL;
326 list_for_each_entry(e, &map->members, list) {
327 if (SET_WITH_TIMEOUT(set) &&
328 ip_set_timeout_expired(ext_timeout(e, set)))
330 else if (e->id != d->id) {
336 next = list_next_entry(e, list);
337 if (list_is_last(&e->list, &map->members) ||
338 next->id != d->refid)
339 return -IPSET_ERR_REF_EXIST;
340 } else if (d->before < 0) {
341 if (!prev || prev->id != d->refid)
342 return -IPSET_ERR_REF_EXIST;
344 list_set_del(set, e);
347 return d->before != 0 ? -IPSET_ERR_REF_EXIST : -IPSET_ERR_EXIST;
351 list_set_uadt(struct ip_set *set, struct nlattr *tb[],
352 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
354 struct list_set *map = set->data;
355 ipset_adtfn adtfn = set->variant->adt[adt];
356 struct set_adt_elem e = { .refid = IPSET_INVALID_ID };
357 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
361 if (tb[IPSET_ATTR_LINENO])
362 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
364 if (unlikely(!tb[IPSET_ATTR_NAME] ||
365 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
366 return -IPSET_ERR_PROTOCOL;
368 ret = ip_set_get_extensions(set, tb, &ext);
371 e.id = ip_set_get_byname(map->net, nla_data(tb[IPSET_ATTR_NAME]), &s);
372 if (e.id == IPSET_INVALID_ID)
373 return -IPSET_ERR_NAME;
374 /* "Loop detection" */
375 if (s->type->features & IPSET_TYPE_NAME) {
376 ret = -IPSET_ERR_LOOP;
380 if (tb[IPSET_ATTR_CADT_FLAGS]) {
381 u32 f = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
383 e.before = f & IPSET_FLAG_BEFORE;
386 if (e.before && !tb[IPSET_ATTR_NAMEREF]) {
387 ret = -IPSET_ERR_BEFORE;
391 if (tb[IPSET_ATTR_NAMEREF]) {
392 e.refid = ip_set_get_byname(map->net,
393 nla_data(tb[IPSET_ATTR_NAMEREF]),
395 if (e.refid == IPSET_INVALID_ID) {
396 ret = -IPSET_ERR_NAMEREF;
402 if (adt != IPSET_TEST && SET_WITH_TIMEOUT(set))
403 set_cleanup_entries(set);
405 ret = adtfn(set, &e, &ext, &ext, flags);
408 if (e.refid != IPSET_INVALID_ID)
409 ip_set_put_byindex(map->net, e.refid);
410 if (adt != IPSET_ADD || ret)
411 ip_set_put_byindex(map->net, e.id);
413 return ip_set_eexist(ret, flags) ? 0 : ret;
417 list_set_flush(struct ip_set *set)
419 struct list_set *map = set->data;
420 struct set_elem *e, *n;
422 list_for_each_entry_safe(e, n, &map->members, list)
423 list_set_del(set, e);
429 list_set_destroy(struct ip_set *set)
431 struct list_set *map = set->data;
432 struct set_elem *e, *n;
434 if (SET_WITH_TIMEOUT(set))
435 del_timer_sync(&map->gc);
437 list_for_each_entry_safe(e, n, &map->members, list) {
439 ip_set_put_byindex(map->net, e->id);
440 ip_set_ext_destroy(set, e);
448 /* Calculate the actual memory size of the set data */
450 list_set_memsize(const struct list_set *map, size_t dsize)
457 list_for_each_entry_rcu(e, &map->members, list)
461 memsize = sizeof(*map) + n * dsize;
467 list_set_head(struct ip_set *set, struct sk_buff *skb)
469 const struct list_set *map = set->data;
470 struct nlattr *nested;
471 size_t memsize = list_set_memsize(map, set->dsize) + set->ext_size;
473 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
475 goto nla_put_failure;
476 if (nla_put_net32(skb, IPSET_ATTR_SIZE, htonl(map->size)) ||
477 nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref)) ||
478 nla_put_net32(skb, IPSET_ATTR_MEMSIZE, htonl(memsize)) ||
479 nla_put_net32(skb, IPSET_ATTR_ELEMENTS, htonl(set->elements)))
480 goto nla_put_failure;
481 if (unlikely(ip_set_put_flags(skb, set)))
482 goto nla_put_failure;
483 ipset_nest_end(skb, nested);
491 list_set_list(const struct ip_set *set,
492 struct sk_buff *skb, struct netlink_callback *cb)
494 const struct list_set *map = set->data;
495 struct nlattr *atd, *nested;
496 u32 i = 0, first = cb->args[IPSET_CB_ARG0];
500 atd = ipset_nest_start(skb, IPSET_ATTR_ADT);
505 list_for_each_entry_rcu(e, &map->members, list) {
507 (SET_WITH_TIMEOUT(set) &&
508 ip_set_timeout_expired(ext_timeout(e, set)))) {
512 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
514 goto nla_put_failure;
515 if (nla_put_string(skb, IPSET_ATTR_NAME,
516 ip_set_name_byindex(map->net, e->id)))
517 goto nla_put_failure;
518 if (ip_set_put_extensions(skb, set, e, true))
519 goto nla_put_failure;
520 ipset_nest_end(skb, nested);
524 ipset_nest_end(skb, atd);
525 /* Set listing finished */
526 cb->args[IPSET_CB_ARG0] = 0;
530 nla_nest_cancel(skb, nested);
531 if (unlikely(i == first)) {
532 nla_nest_cancel(skb, atd);
533 cb->args[IPSET_CB_ARG0] = 0;
536 cb->args[IPSET_CB_ARG0] = i;
538 ipset_nest_end(skb, atd);
545 list_set_same_set(const struct ip_set *a, const struct ip_set *b)
547 const struct list_set *x = a->data;
548 const struct list_set *y = b->data;
550 return x->size == y->size &&
551 a->timeout == b->timeout &&
552 a->extensions == b->extensions;
555 static const struct ip_set_type_variant set_variant = {
556 .kadt = list_set_kadt,
557 .uadt = list_set_uadt,
559 [IPSET_ADD] = list_set_uadd,
560 [IPSET_DEL] = list_set_udel,
561 [IPSET_TEST] = list_set_utest,
563 .destroy = list_set_destroy,
564 .flush = list_set_flush,
565 .head = list_set_head,
566 .list = list_set_list,
567 .same_set = list_set_same_set,
571 list_set_gc(unsigned long ul_set)
573 struct ip_set *set = (struct ip_set *)ul_set;
574 struct list_set *map = set->data;
576 spin_lock_bh(&set->lock);
577 set_cleanup_entries(set);
578 spin_unlock_bh(&set->lock);
580 map->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
585 list_set_gc_init(struct ip_set *set, void (*gc)(unsigned long ul_set))
587 struct list_set *map = set->data;
589 setup_timer(&map->gc, gc, (unsigned long)set);
590 mod_timer(&map->gc, jiffies + IPSET_GC_PERIOD(set->timeout) * HZ);
593 /* Create list:set type of sets */
596 init_list_set(struct net *net, struct ip_set *set, u32 size)
598 struct list_set *map;
600 map = kzalloc(sizeof(*map), GFP_KERNEL);
606 INIT_LIST_HEAD(&map->members);
613 list_set_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
616 u32 size = IP_SET_LIST_DEFAULT_SIZE;
618 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_SIZE) ||
619 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
620 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
621 return -IPSET_ERR_PROTOCOL;
623 if (tb[IPSET_ATTR_SIZE])
624 size = ip_set_get_h32(tb[IPSET_ATTR_SIZE]);
625 if (size < IP_SET_LIST_MIN_SIZE)
626 size = IP_SET_LIST_MIN_SIZE;
628 set->variant = &set_variant;
629 set->dsize = ip_set_elem_len(set, tb, sizeof(struct set_elem),
630 __alignof__(struct set_elem));
631 if (!init_list_set(net, set, size))
633 if (tb[IPSET_ATTR_TIMEOUT]) {
634 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
635 list_set_gc_init(set, list_set_gc);
640 static struct ip_set_type list_set_type __read_mostly = {
642 .protocol = IPSET_PROTOCOL,
643 .features = IPSET_TYPE_NAME | IPSET_DUMP_LAST,
644 .dimension = IPSET_DIM_ONE,
645 .family = NFPROTO_UNSPEC,
646 .revision_min = IPSET_TYPE_REV_MIN,
647 .revision_max = IPSET_TYPE_REV_MAX,
648 .create = list_set_create,
650 [IPSET_ATTR_SIZE] = { .type = NLA_U32 },
651 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
652 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
655 [IPSET_ATTR_NAME] = { .type = NLA_STRING,
656 .len = IPSET_MAXNAMELEN },
657 [IPSET_ATTR_NAMEREF] = { .type = NLA_STRING,
658 .len = IPSET_MAXNAMELEN },
659 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
660 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
661 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
662 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
663 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
664 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
665 .len = IPSET_MAX_COMMENT_SIZE },
666 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
667 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
668 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
676 return ip_set_type_register(&list_set_type);
683 ip_set_type_unregister(&list_set_type);
686 module_init(list_set_init);
687 module_exit(list_set_fini);