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/skbuff.h>
13 #include <linux/errno.h>
15 #include <linux/netfilter/ipset/ip_set.h>
16 #include <linux/netfilter/ipset/ip_set_list.h>
18 #define IPSET_TYPE_REV_MIN 0
19 /* 1 Counters support added */
20 #define IPSET_TYPE_REV_MAX 2 /* Comments support added */
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
24 IP_SET_MODULE_DESC("list:set", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
25 MODULE_ALIAS("ip_set_list:set");
40 u32 size; /* size of set list array */
41 struct timer_list gc; /* garbage collection */
42 struct net *net; /* namespace */
43 struct set_elem members[0]; /* the set members */
46 #define list_set_elem(set, map, id) \
47 (struct set_elem *)((void *)(map)->members + (id) * (set)->dsize)
50 list_set_ktest(struct ip_set *set, const struct sk_buff *skb,
51 const struct xt_action_param *par,
52 struct ip_set_adt_opt *opt, const struct ip_set_ext *ext)
54 struct list_set *map = set->data;
56 u32 i, cmdflags = 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 for (i = 0; i < map->size; i++) {
64 e = list_set_elem(set, map, i);
65 if (e->id == IPSET_INVALID_ID)
67 if (SET_WITH_TIMEOUT(set) &&
68 ip_set_timeout_expired(ext_timeout(e, set)))
70 ret = ip_set_test(e->id, skb, par, opt);
72 if (SET_WITH_COUNTER(set))
73 ip_set_update_counter(ext_counter(e, set),
83 list_set_kadd(struct ip_set *set, const struct sk_buff *skb,
84 const struct xt_action_param *par,
85 struct ip_set_adt_opt *opt, const struct ip_set_ext *ext)
87 struct list_set *map = set->data;
92 for (i = 0; i < map->size; i++) {
93 e = list_set_elem(set, map, i);
94 if (e->id == IPSET_INVALID_ID)
96 if (SET_WITH_TIMEOUT(set) &&
97 ip_set_timeout_expired(ext_timeout(e, set)))
99 ret = ip_set_add(e->id, skb, par, opt);
107 list_set_kdel(struct ip_set *set, const struct sk_buff *skb,
108 const struct xt_action_param *par,
109 struct ip_set_adt_opt *opt, const struct ip_set_ext *ext)
111 struct list_set *map = set->data;
116 for (i = 0; i < map->size; i++) {
117 e = list_set_elem(set, map, i);
118 if (e->id == IPSET_INVALID_ID)
120 if (SET_WITH_TIMEOUT(set) &&
121 ip_set_timeout_expired(ext_timeout(e, set)))
123 ret = ip_set_del(e->id, skb, par, opt);
131 list_set_kadt(struct ip_set *set, const struct sk_buff *skb,
132 const struct xt_action_param *par,
133 enum ipset_adt adt, struct ip_set_adt_opt *opt)
135 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
139 return list_set_ktest(set, skb, par, opt, &ext);
141 return list_set_kadd(set, skb, par, opt, &ext);
143 return list_set_kdel(set, skb, par, opt, &ext);
151 id_eq(const struct ip_set *set, u32 i, ip_set_id_t id)
153 const struct list_set *map = set->data;
154 const struct set_elem *e;
159 e = list_set_elem(set, map, i);
160 return !!(e->id == id &&
161 !(SET_WITH_TIMEOUT(set) &&
162 ip_set_timeout_expired(ext_timeout(e, set))));
166 list_set_add(struct ip_set *set, u32 i, struct set_adt_elem *d,
167 const struct ip_set_ext *ext)
169 struct list_set *map = set->data;
170 struct set_elem *e = list_set_elem(set, map, i);
172 if (e->id != IPSET_INVALID_ID) {
173 if (i == map->size - 1) {
174 /* Last element replaced: e.g. add new,before,last */
175 ip_set_put_byindex(map->net, e->id);
176 ip_set_ext_destroy(set, e);
178 struct set_elem *x = list_set_elem(set, map,
181 /* Last element pushed off */
182 if (x->id != IPSET_INVALID_ID) {
183 ip_set_put_byindex(map->net, x->id);
184 ip_set_ext_destroy(set, x);
186 memmove(list_set_elem(set, map, i + 1), e,
187 set->dsize * (map->size - (i + 1)));
188 /* Extensions must be initialized to zero */
189 memset(e, 0, set->dsize);
194 if (SET_WITH_TIMEOUT(set))
195 ip_set_timeout_set(ext_timeout(e, set), ext->timeout);
196 if (SET_WITH_COUNTER(set))
197 ip_set_init_counter(ext_counter(e, set), ext);
198 if (SET_WITH_COMMENT(set))
199 ip_set_init_comment(ext_comment(e, set), ext);
204 list_set_del(struct ip_set *set, u32 i)
206 struct list_set *map = set->data;
207 struct set_elem *e = list_set_elem(set, map, i);
209 ip_set_put_byindex(map->net, e->id);
210 ip_set_ext_destroy(set, e);
212 if (i < map->size - 1)
213 memmove(e, list_set_elem(set, map, i + 1),
214 set->dsize * (map->size - (i + 1)));
217 e = list_set_elem(set, map, map->size - 1);
218 e->id = IPSET_INVALID_ID;
223 set_cleanup_entries(struct ip_set *set)
225 struct list_set *map = set->data;
229 while (i < map->size) {
230 e = list_set_elem(set, map, i);
231 if (e->id != IPSET_INVALID_ID &&
232 ip_set_timeout_expired(ext_timeout(e, set)))
233 list_set_del(set, i);
234 /* Check element moved to position i in next loop */
241 list_set_utest(struct ip_set *set, void *value, const struct ip_set_ext *ext,
242 struct ip_set_ext *mext, u32 flags)
244 struct list_set *map = set->data;
245 struct set_adt_elem *d = value;
250 for (i = 0; i < map->size; i++) {
251 e = list_set_elem(set, map, i);
252 if (e->id == IPSET_INVALID_ID)
254 else if (SET_WITH_TIMEOUT(set) &&
255 ip_set_timeout_expired(ext_timeout(e, set)))
257 else if (e->id != d->id)
262 else if (d->before > 0)
263 ret = id_eq(set, i + 1, d->refid);
265 ret = i > 0 && id_eq(set, i - 1, d->refid);
273 list_set_uadd(struct ip_set *set, void *value, const struct ip_set_ext *ext,
274 struct ip_set_ext *mext, u32 flags)
276 struct list_set *map = set->data;
277 struct set_adt_elem *d = value;
279 bool flag_exist = flags & IPSET_FLAG_EXIST;
282 if (SET_WITH_TIMEOUT(set))
283 set_cleanup_entries(set);
285 /* Check already added element */
286 for (i = 0; i < map->size; i++) {
287 e = list_set_elem(set, map, i);
288 if (e->id == IPSET_INVALID_ID)
290 else if (e->id != d->id)
293 if ((d->before > 1 && !id_eq(set, i + 1, d->refid)) ||
295 (i == 0 || !id_eq(set, i - 1, d->refid))))
296 /* Before/after doesn't match */
297 return -IPSET_ERR_REF_EXIST;
300 return -IPSET_ERR_EXIST;
301 /* Update extensions */
302 ip_set_ext_destroy(set, e);
304 if (SET_WITH_TIMEOUT(set))
305 ip_set_timeout_set(ext_timeout(e, set), ext->timeout);
306 if (SET_WITH_COUNTER(set))
307 ip_set_init_counter(ext_counter(e, set), ext);
308 if (SET_WITH_COMMENT(set))
309 ip_set_init_comment(ext_comment(e, set), ext);
310 /* Set is already added to the list */
311 ip_set_put_byindex(map->net, d->id);
315 ret = -IPSET_ERR_LIST_FULL;
316 for (i = 0; i < map->size && ret == -IPSET_ERR_LIST_FULL; i++) {
317 e = list_set_elem(set, map, i);
318 if (e->id == IPSET_INVALID_ID)
319 ret = d->before != 0 ? -IPSET_ERR_REF_EXIST
320 : list_set_add(set, i, d, ext);
321 else if (e->id != d->refid)
323 else if (d->before > 0)
324 ret = list_set_add(set, i, d, ext);
325 else if (i + 1 < map->size)
326 ret = list_set_add(set, i + 1, d, ext);
333 list_set_udel(struct ip_set *set, void *value, const struct ip_set_ext *ext,
334 struct ip_set_ext *mext, u32 flags)
336 struct list_set *map = set->data;
337 struct set_adt_elem *d = value;
341 for (i = 0; i < map->size; i++) {
342 e = list_set_elem(set, map, i);
343 if (e->id == IPSET_INVALID_ID)
344 return d->before != 0 ? -IPSET_ERR_REF_EXIST
346 else if (SET_WITH_TIMEOUT(set) &&
347 ip_set_timeout_expired(ext_timeout(e, set)))
349 else if (e->id != d->id)
353 return list_set_del(set, i);
354 else if (d->before > 0) {
355 if (!id_eq(set, i + 1, d->refid))
356 return -IPSET_ERR_REF_EXIST;
357 return list_set_del(set, i);
358 } else if (i == 0 || !id_eq(set, i - 1, d->refid))
359 return -IPSET_ERR_REF_EXIST;
361 return list_set_del(set, i);
363 return -IPSET_ERR_EXIST;
367 list_set_uadt(struct ip_set *set, struct nlattr *tb[],
368 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
370 struct list_set *map = set->data;
371 ipset_adtfn adtfn = set->variant->adt[adt];
372 struct set_adt_elem e = { .refid = IPSET_INVALID_ID };
373 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
377 if (unlikely(!tb[IPSET_ATTR_NAME] ||
378 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
379 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
380 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
381 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
382 return -IPSET_ERR_PROTOCOL;
384 if (tb[IPSET_ATTR_LINENO])
385 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
387 ret = ip_set_get_extensions(set, tb, &ext);
390 e.id = ip_set_get_byname(map->net, nla_data(tb[IPSET_ATTR_NAME]), &s);
391 if (e.id == IPSET_INVALID_ID)
392 return -IPSET_ERR_NAME;
393 /* "Loop detection" */
394 if (s->type->features & IPSET_TYPE_NAME) {
395 ret = -IPSET_ERR_LOOP;
399 if (tb[IPSET_ATTR_CADT_FLAGS]) {
400 u32 f = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
401 e.before = f & IPSET_FLAG_BEFORE;
404 if (e.before && !tb[IPSET_ATTR_NAMEREF]) {
405 ret = -IPSET_ERR_BEFORE;
409 if (tb[IPSET_ATTR_NAMEREF]) {
410 e.refid = ip_set_get_byname(map->net,
411 nla_data(tb[IPSET_ATTR_NAMEREF]),
413 if (e.refid == IPSET_INVALID_ID) {
414 ret = -IPSET_ERR_NAMEREF;
420 if (adt != IPSET_TEST && SET_WITH_TIMEOUT(set))
421 set_cleanup_entries(set);
423 ret = adtfn(set, &e, &ext, &ext, flags);
426 if (e.refid != IPSET_INVALID_ID)
427 ip_set_put_byindex(map->net, e.refid);
428 if (adt != IPSET_ADD || ret)
429 ip_set_put_byindex(map->net, e.id);
431 return ip_set_eexist(ret, flags) ? 0 : ret;
435 list_set_flush(struct ip_set *set)
437 struct list_set *map = set->data;
441 for (i = 0; i < map->size; i++) {
442 e = list_set_elem(set, map, i);
443 if (e->id != IPSET_INVALID_ID) {
444 ip_set_put_byindex(map->net, e->id);
445 ip_set_ext_destroy(set, e);
446 e->id = IPSET_INVALID_ID;
452 list_set_destroy(struct ip_set *set)
454 struct list_set *map = set->data;
456 if (SET_WITH_TIMEOUT(set))
457 del_timer_sync(&map->gc);
465 list_set_head(struct ip_set *set, struct sk_buff *skb)
467 const struct list_set *map = set->data;
468 struct nlattr *nested;
470 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
472 goto nla_put_failure;
473 if (nla_put_net32(skb, IPSET_ATTR_SIZE, htonl(map->size)) ||
474 nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1)) ||
475 nla_put_net32(skb, IPSET_ATTR_MEMSIZE,
476 htonl(sizeof(*map) + map->size * set->dsize)))
477 goto nla_put_failure;
478 if (unlikely(ip_set_put_flags(skb, set)))
479 goto nla_put_failure;
480 ipset_nest_end(skb, nested);
488 list_set_list(const struct ip_set *set,
489 struct sk_buff *skb, struct netlink_callback *cb)
491 const struct list_set *map = set->data;
492 struct nlattr *atd, *nested;
493 u32 i, first = cb->args[2];
494 const struct set_elem *e;
496 atd = ipset_nest_start(skb, IPSET_ATTR_ADT);
499 for (; cb->args[2] < map->size; cb->args[2]++) {
501 e = list_set_elem(set, map, i);
502 if (e->id == IPSET_INVALID_ID)
504 if (SET_WITH_TIMEOUT(set) &&
505 ip_set_timeout_expired(ext_timeout(e, set)))
507 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
510 nla_nest_cancel(skb, atd);
513 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);
523 ipset_nest_end(skb, atd);
524 /* Set listing finished */
529 nla_nest_cancel(skb, nested);
530 if (unlikely(i == first)) {
534 ipset_nest_end(skb, atd);
539 list_set_same_set(const struct ip_set *a, const struct ip_set *b)
541 const struct list_set *x = a->data;
542 const struct list_set *y = b->data;
544 return x->size == y->size &&
545 a->timeout == b->timeout &&
546 a->extensions == b->extensions;
549 static const struct ip_set_type_variant set_variant = {
550 .kadt = list_set_kadt,
551 .uadt = list_set_uadt,
553 [IPSET_ADD] = list_set_uadd,
554 [IPSET_DEL] = list_set_udel,
555 [IPSET_TEST] = list_set_utest,
557 .destroy = list_set_destroy,
558 .flush = list_set_flush,
559 .head = list_set_head,
560 .list = list_set_list,
561 .same_set = list_set_same_set,
565 list_set_gc(unsigned long ul_set)
567 struct ip_set *set = (struct ip_set *) ul_set;
568 struct list_set *map = set->data;
570 write_lock_bh(&set->lock);
571 set_cleanup_entries(set);
572 write_unlock_bh(&set->lock);
574 map->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
579 list_set_gc_init(struct ip_set *set, void (*gc)(unsigned long ul_set))
581 struct list_set *map = set->data;
583 init_timer(&map->gc);
584 map->gc.data = (unsigned long) set;
585 map->gc.function = gc;
586 map->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
590 /* Create list:set type of sets */
593 init_list_set(struct net *net, struct ip_set *set, u32 size)
595 struct list_set *map;
599 map = kzalloc(sizeof(*map) + size * set->dsize, GFP_KERNEL);
607 for (i = 0; i < size; i++) {
608 e = list_set_elem(set, map, i);
609 e->id = IPSET_INVALID_ID;
616 list_set_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
619 u32 size = IP_SET_LIST_DEFAULT_SIZE;
621 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_SIZE) ||
622 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
623 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
624 return -IPSET_ERR_PROTOCOL;
626 if (tb[IPSET_ATTR_SIZE])
627 size = ip_set_get_h32(tb[IPSET_ATTR_SIZE]);
628 if (size < IP_SET_LIST_MIN_SIZE)
629 size = IP_SET_LIST_MIN_SIZE;
631 set->variant = &set_variant;
632 set->dsize = ip_set_elem_len(set, tb, sizeof(struct set_elem));
633 if (!init_list_set(net, set, size))
635 if (tb[IPSET_ATTR_TIMEOUT]) {
636 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
637 list_set_gc_init(set, list_set_gc);
642 static struct ip_set_type list_set_type __read_mostly = {
644 .protocol = IPSET_PROTOCOL,
645 .features = IPSET_TYPE_NAME | IPSET_DUMP_LAST,
646 .dimension = IPSET_DIM_ONE,
647 .family = NFPROTO_UNSPEC,
648 .revision_min = IPSET_TYPE_REV_MIN,
649 .revision_max = IPSET_TYPE_REV_MAX,
650 .create = list_set_create,
652 [IPSET_ATTR_SIZE] = { .type = NLA_U32 },
653 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
654 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
657 [IPSET_ATTR_NAME] = { .type = NLA_STRING,
658 .len = IPSET_MAXNAMELEN },
659 [IPSET_ATTR_NAMEREF] = { .type = NLA_STRING,
660 .len = IPSET_MAXNAMELEN },
661 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
662 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
663 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
664 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
665 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
666 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
674 return ip_set_type_register(&list_set_type);
680 ip_set_type_unregister(&list_set_type);
683 module_init(list_set_init);
684 module_exit(list_set_fini);