1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2008-2013 Jozsef Kadlecsik <kadlec@netfilter.org> */
4 /* Kernel module implementing an IP set type: the list:set type */
6 #include <linux/module.h>
8 #include <linux/rculist.h>
9 #include <linux/skbuff.h>
10 #include <linux/errno.h>
12 #include <linux/netfilter/ipset/ip_set.h>
13 #include <linux/netfilter/ipset/ip_set_list.h>
15 #define IPSET_TYPE_REV_MIN 0
16 /* 1 Counters support added */
17 /* 2 Comments support added */
18 #define IPSET_TYPE_REV_MAX 3 /* skbinfo support added */
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
22 IP_SET_MODULE_DESC("list:set", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
23 MODULE_ALIAS("ip_set_list:set");
28 struct list_head list;
29 struct ip_set *set; /* Sigh, in order to cleanup reference */
31 } __aligned(__alignof__(u64));
41 u32 size; /* size of set list array */
42 struct timer_list gc; /* garbage collection */
43 struct ip_set *set; /* attached to this ip_set */
44 struct net *net; /* namespace */
45 struct list_head members; /* the set members */
49 list_set_ktest(struct ip_set *set, const struct sk_buff *skb,
50 const struct xt_action_param *par,
51 struct ip_set_adt_opt *opt, const struct ip_set_ext *ext)
53 struct list_set *map = set->data;
54 struct ip_set_ext *mext = &opt->ext;
56 u32 flags = opt->cmdflags;
59 /* Don't lookup sub-counters at all */
60 opt->cmdflags &= ~IPSET_FLAG_MATCH_COUNTERS;
61 if (opt->cmdflags & IPSET_FLAG_SKIP_SUBCOUNTER_UPDATE)
62 opt->cmdflags |= IPSET_FLAG_SKIP_COUNTER_UPDATE;
63 list_for_each_entry_rcu(e, &map->members, list) {
64 ret = ip_set_test(e->id, skb, par, opt);
67 if (ip_set_match_extensions(set, ext, mext, flags, e))
74 list_set_kadd(struct ip_set *set, const struct sk_buff *skb,
75 const struct xt_action_param *par,
76 struct ip_set_adt_opt *opt, const struct ip_set_ext *ext)
78 struct list_set *map = set->data;
82 list_for_each_entry(e, &map->members, list) {
83 if (SET_WITH_TIMEOUT(set) &&
84 ip_set_timeout_expired(ext_timeout(e, set)))
86 ret = ip_set_add(e->id, skb, par, opt);
94 list_set_kdel(struct ip_set *set, const struct sk_buff *skb,
95 const struct xt_action_param *par,
96 struct ip_set_adt_opt *opt, const struct ip_set_ext *ext)
98 struct list_set *map = set->data;
102 list_for_each_entry(e, &map->members, list) {
103 if (SET_WITH_TIMEOUT(set) &&
104 ip_set_timeout_expired(ext_timeout(e, set)))
106 ret = ip_set_del(e->id, skb, par, opt);
114 list_set_kadt(struct ip_set *set, const struct sk_buff *skb,
115 const struct xt_action_param *par,
116 enum ipset_adt adt, struct ip_set_adt_opt *opt)
118 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
124 ret = list_set_ktest(set, skb, par, opt, &ext);
127 ret = list_set_kadd(set, skb, par, opt, &ext);
130 ret = list_set_kdel(set, skb, par, opt, &ext);
140 /* Userspace interfaces: we are protected by the nfnl mutex */
143 __list_set_del_rcu(struct rcu_head * rcu)
145 struct set_elem *e = container_of(rcu, struct set_elem, rcu);
146 struct ip_set *set = e->set;
148 ip_set_ext_destroy(set, e);
153 list_set_del(struct ip_set *set, struct set_elem *e)
155 struct list_set *map = set->data;
158 list_del_rcu(&e->list);
159 ip_set_put_byindex(map->net, e->id);
160 call_rcu(&e->rcu, __list_set_del_rcu);
164 list_set_replace(struct ip_set *set, struct set_elem *e, struct set_elem *old)
166 struct list_set *map = set->data;
168 list_replace_rcu(&old->list, &e->list);
169 ip_set_put_byindex(map->net, old->id);
170 call_rcu(&old->rcu, __list_set_del_rcu);
174 set_cleanup_entries(struct ip_set *set)
176 struct list_set *map = set->data;
177 struct set_elem *e, *n;
179 list_for_each_entry_safe(e, n, &map->members, list)
180 if (ip_set_timeout_expired(ext_timeout(e, set)))
181 list_set_del(set, e);
185 list_set_utest(struct ip_set *set, void *value, const struct ip_set_ext *ext,
186 struct ip_set_ext *mext, u32 flags)
188 struct list_set *map = set->data;
189 struct set_adt_elem *d = value;
190 struct set_elem *e, *next, *prev = NULL;
193 list_for_each_entry(e, &map->members, list) {
194 if (SET_WITH_TIMEOUT(set) &&
195 ip_set_timeout_expired(ext_timeout(e, set)))
197 else if (e->id != d->id) {
202 if (d->before == 0) {
204 } else if (d->before > 0) {
205 next = list_next_entry(e, list);
206 ret = !list_is_last(&e->list, &map->members) &&
207 next->id == d->refid;
209 ret = prev && prev->id == d->refid;
217 list_set_init_extensions(struct ip_set *set, const struct ip_set_ext *ext,
220 if (SET_WITH_COUNTER(set))
221 ip_set_init_counter(ext_counter(e, set), ext);
222 if (SET_WITH_COMMENT(set))
223 ip_set_init_comment(set, ext_comment(e, set), ext);
224 if (SET_WITH_SKBINFO(set))
225 ip_set_init_skbinfo(ext_skbinfo(e, set), ext);
226 /* Update timeout last */
227 if (SET_WITH_TIMEOUT(set))
228 ip_set_timeout_set(ext_timeout(e, set), ext->timeout);
232 list_set_uadd(struct ip_set *set, void *value, const struct ip_set_ext *ext,
233 struct ip_set_ext *mext, u32 flags)
235 struct list_set *map = set->data;
236 struct set_adt_elem *d = value;
237 struct set_elem *e, *n, *prev, *next;
238 bool flag_exist = flags & IPSET_FLAG_EXIST;
240 /* Find where to add the new entry */
241 n = prev = next = NULL;
242 list_for_each_entry(e, &map->members, list) {
243 if (SET_WITH_TIMEOUT(set) &&
244 ip_set_timeout_expired(ext_timeout(e, set)))
246 else if (d->id == e->id)
248 else if (d->before == 0 || e->id != d->refid)
250 else if (d->before > 0)
256 /* If before/after is used on an empty set */
257 if ((d->before > 0 && !next) ||
258 (d->before < 0 && !prev))
259 return -IPSET_ERR_REF_EXIST;
261 /* Re-add already existing element */
264 return -IPSET_ERR_EXIST;
265 /* Update extensions */
266 ip_set_ext_destroy(set, n);
267 list_set_init_extensions(set, ext, n);
269 /* Set is already added to the list */
270 ip_set_put_byindex(map->net, d->id);
274 if (d->before == 0) {
276 n = list_empty(&map->members) ? NULL :
277 list_last_entry(&map->members, struct set_elem, list);
278 } else if (d->before > 0) {
279 /* Insert after next element */
280 if (!list_is_last(&next->list, &map->members))
281 n = list_next_entry(next, list);
283 /* Insert before prev element */
284 if (prev->list.prev != &map->members)
285 n = list_prev_entry(prev, list);
287 /* Can we replace a timed out entry? */
289 !(SET_WITH_TIMEOUT(set) &&
290 ip_set_timeout_expired(ext_timeout(n, set))))
293 e = kzalloc(set->dsize, GFP_ATOMIC);
298 INIT_LIST_HEAD(&e->list);
299 list_set_init_extensions(set, ext, e);
301 list_set_replace(set, e, n);
303 list_add_tail_rcu(&e->list, &next->list);
305 list_add_rcu(&e->list, &prev->list);
307 list_add_tail_rcu(&e->list, &map->members);
314 list_set_udel(struct ip_set *set, void *value, const struct ip_set_ext *ext,
315 struct ip_set_ext *mext, u32 flags)
317 struct list_set *map = set->data;
318 struct set_adt_elem *d = value;
319 struct set_elem *e, *next, *prev = NULL;
321 list_for_each_entry(e, &map->members, list) {
322 if (SET_WITH_TIMEOUT(set) &&
323 ip_set_timeout_expired(ext_timeout(e, set)))
325 else if (e->id != d->id) {
331 next = list_next_entry(e, list);
332 if (list_is_last(&e->list, &map->members) ||
333 next->id != d->refid)
334 return -IPSET_ERR_REF_EXIST;
335 } else if (d->before < 0) {
336 if (!prev || prev->id != d->refid)
337 return -IPSET_ERR_REF_EXIST;
339 list_set_del(set, e);
342 return d->before != 0 ? -IPSET_ERR_REF_EXIST : -IPSET_ERR_EXIST;
346 list_set_uadt(struct ip_set *set, struct nlattr *tb[],
347 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
349 struct list_set *map = set->data;
350 ipset_adtfn adtfn = set->variant->adt[adt];
351 struct set_adt_elem e = { .refid = IPSET_INVALID_ID };
352 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
356 if (tb[IPSET_ATTR_LINENO])
357 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
359 if (unlikely(!tb[IPSET_ATTR_NAME] ||
360 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
361 return -IPSET_ERR_PROTOCOL;
363 ret = ip_set_get_extensions(set, tb, &ext);
366 e.id = ip_set_get_byname(map->net, nla_data(tb[IPSET_ATTR_NAME]), &s);
367 if (e.id == IPSET_INVALID_ID)
368 return -IPSET_ERR_NAME;
369 /* "Loop detection" */
370 if (s->type->features & IPSET_TYPE_NAME) {
371 ret = -IPSET_ERR_LOOP;
375 if (tb[IPSET_ATTR_CADT_FLAGS]) {
376 u32 f = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
378 e.before = f & IPSET_FLAG_BEFORE;
381 if (e.before && !tb[IPSET_ATTR_NAMEREF]) {
382 ret = -IPSET_ERR_BEFORE;
386 if (tb[IPSET_ATTR_NAMEREF]) {
387 e.refid = ip_set_get_byname(map->net,
388 nla_data(tb[IPSET_ATTR_NAMEREF]),
390 if (e.refid == IPSET_INVALID_ID) {
391 ret = -IPSET_ERR_NAMEREF;
397 if (adt != IPSET_TEST && SET_WITH_TIMEOUT(set))
398 set_cleanup_entries(set);
400 ret = adtfn(set, &e, &ext, &ext, flags);
403 if (e.refid != IPSET_INVALID_ID)
404 ip_set_put_byindex(map->net, e.refid);
405 if (adt != IPSET_ADD || ret)
406 ip_set_put_byindex(map->net, e.id);
408 return ip_set_eexist(ret, flags) ? 0 : ret;
412 list_set_flush(struct ip_set *set)
414 struct list_set *map = set->data;
415 struct set_elem *e, *n;
417 list_for_each_entry_safe(e, n, &map->members, list)
418 list_set_del(set, e);
424 list_set_destroy(struct ip_set *set)
426 struct list_set *map = set->data;
427 struct set_elem *e, *n;
429 list_for_each_entry_safe(e, n, &map->members, list) {
431 ip_set_put_byindex(map->net, e->id);
432 ip_set_ext_destroy(set, e);
440 /* Calculate the actual memory size of the set data */
442 list_set_memsize(const struct list_set *map, size_t dsize)
448 list_for_each_entry_rcu(e, &map->members, list)
452 return (sizeof(*map) + n * dsize);
456 list_set_head(struct ip_set *set, struct sk_buff *skb)
458 const struct list_set *map = set->data;
459 struct nlattr *nested;
460 size_t memsize = list_set_memsize(map, set->dsize) + set->ext_size;
462 nested = nla_nest_start(skb, IPSET_ATTR_DATA);
464 goto nla_put_failure;
465 if (nla_put_net32(skb, IPSET_ATTR_SIZE, htonl(map->size)) ||
466 nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref)) ||
467 nla_put_net32(skb, IPSET_ATTR_MEMSIZE, htonl(memsize)) ||
468 nla_put_net32(skb, IPSET_ATTR_ELEMENTS, htonl(set->elements)))
469 goto nla_put_failure;
470 if (unlikely(ip_set_put_flags(skb, set)))
471 goto nla_put_failure;
472 nla_nest_end(skb, nested);
480 list_set_list(const struct ip_set *set,
481 struct sk_buff *skb, struct netlink_callback *cb)
483 const struct list_set *map = set->data;
484 struct nlattr *atd, *nested;
485 u32 i = 0, first = cb->args[IPSET_CB_ARG0];
486 char name[IPSET_MAXNAMELEN];
490 atd = nla_nest_start(skb, IPSET_ATTR_ADT);
495 list_for_each_entry_rcu(e, &map->members, list) {
497 (SET_WITH_TIMEOUT(set) &&
498 ip_set_timeout_expired(ext_timeout(e, set)))) {
502 nested = nla_nest_start(skb, IPSET_ATTR_DATA);
504 goto nla_put_failure;
505 ip_set_name_byindex(map->net, e->id, name);
506 if (nla_put_string(skb, IPSET_ATTR_NAME, name))
507 goto nla_put_failure;
508 if (ip_set_put_extensions(skb, set, e, true))
509 goto nla_put_failure;
510 nla_nest_end(skb, nested);
514 nla_nest_end(skb, atd);
515 /* Set listing finished */
516 cb->args[IPSET_CB_ARG0] = 0;
520 nla_nest_cancel(skb, nested);
521 if (unlikely(i == first)) {
522 nla_nest_cancel(skb, atd);
523 cb->args[IPSET_CB_ARG0] = 0;
526 cb->args[IPSET_CB_ARG0] = i;
527 nla_nest_end(skb, atd);
535 list_set_same_set(const struct ip_set *a, const struct ip_set *b)
537 const struct list_set *x = a->data;
538 const struct list_set *y = b->data;
540 return x->size == y->size &&
541 a->timeout == b->timeout &&
542 a->extensions == b->extensions;
546 list_set_cancel_gc(struct ip_set *set)
548 struct list_set *map = set->data;
550 if (SET_WITH_TIMEOUT(set))
551 del_timer_sync(&map->gc);
554 static const struct ip_set_type_variant set_variant = {
555 .kadt = list_set_kadt,
556 .uadt = list_set_uadt,
558 [IPSET_ADD] = list_set_uadd,
559 [IPSET_DEL] = list_set_udel,
560 [IPSET_TEST] = list_set_utest,
562 .destroy = list_set_destroy,
563 .flush = list_set_flush,
564 .head = list_set_head,
565 .list = list_set_list,
566 .same_set = list_set_same_set,
567 .cancel_gc = list_set_cancel_gc,
571 list_set_gc(struct timer_list *t)
573 struct list_set *map = from_timer(map, t, gc);
574 struct ip_set *set = map->set;
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)(struct timer_list *t))
587 struct list_set *map = set->data;
589 timer_setup(&map->gc, gc, 0);
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);
607 INIT_LIST_HEAD(&map->members);
614 list_set_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
617 u32 size = IP_SET_LIST_DEFAULT_SIZE;
619 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_SIZE) ||
620 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
621 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
622 return -IPSET_ERR_PROTOCOL;
624 if (tb[IPSET_ATTR_SIZE])
625 size = ip_set_get_h32(tb[IPSET_ATTR_SIZE]);
626 if (size < IP_SET_LIST_MIN_SIZE)
627 size = IP_SET_LIST_MIN_SIZE;
629 set->variant = &set_variant;
630 set->dsize = ip_set_elem_len(set, tb, sizeof(struct set_elem),
631 __alignof__(struct set_elem));
632 if (!init_list_set(net, set, size))
634 if (tb[IPSET_ATTR_TIMEOUT]) {
635 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
636 list_set_gc_init(set, list_set_gc);
641 static struct ip_set_type list_set_type __read_mostly = {
643 .protocol = IPSET_PROTOCOL,
644 .features = IPSET_TYPE_NAME | IPSET_DUMP_LAST,
645 .dimension = IPSET_DIM_ONE,
646 .family = NFPROTO_UNSPEC,
647 .revision_min = IPSET_TYPE_REV_MIN,
648 .revision_max = IPSET_TYPE_REV_MAX,
649 .create = list_set_create,
651 [IPSET_ATTR_SIZE] = { .type = NLA_U32 },
652 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
653 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
656 [IPSET_ATTR_NAME] = { .type = NLA_STRING,
657 .len = IPSET_MAXNAMELEN },
658 [IPSET_ATTR_NAMEREF] = { .type = NLA_STRING,
659 .len = IPSET_MAXNAMELEN },
660 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
661 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
662 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
663 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
664 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
665 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
666 .len = IPSET_MAX_COMMENT_SIZE },
667 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
668 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
669 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
677 return ip_set_type_register(&list_set_type);
684 ip_set_type_unregister(&list_set_type);
687 module_init(list_set_init);
688 module_exit(list_set_fini);