1 /* Copyright (C) 2008-2011 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_timeout.h>
17 #include <linux/netfilter/ipset/ip_set_list.h>
19 #define REVISION_MIN 0
20 #define REVISION_MAX 0
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
24 IP_SET_MODULE_DESC("list:set", REVISION_MIN, REVISION_MAX);
25 MODULE_ALIAS("ip_set_list:set");
27 /* Member elements without and with timeout */
34 unsigned long timeout;
39 size_t dsize; /* element size */
40 u32 size; /* size of set list array */
41 u32 timeout; /* timeout value */
42 struct timer_list gc; /* garbage collection */
43 struct set_elem members[0]; /* the set members */
46 static inline struct set_elem *
47 list_set_elem(const struct list_set *map, u32 id)
49 return (struct set_elem *)((void *)map->members + id * map->dsize);
52 static inline struct set_telem *
53 list_set_telem(const struct list_set *map, u32 id)
55 return (struct set_telem *)((void *)map->members + id * map->dsize);
59 list_set_timeout(const struct list_set *map, u32 id)
61 const struct set_telem *elem = list_set_telem(map, id);
63 return ip_set_timeout_test(elem->timeout);
67 list_set_expired(const struct list_set *map, u32 id)
69 const struct set_telem *elem = list_set_telem(map, id);
71 return ip_set_timeout_expired(elem->timeout);
74 /* Set list without and with timeout */
77 list_set_kadt(struct ip_set *set, const struct sk_buff *skb,
78 const struct xt_action_param *par,
79 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
81 struct list_set *map = set->data;
82 struct set_elem *elem;
86 for (i = 0; i < map->size; i++) {
87 elem = list_set_elem(map, i);
88 if (elem->id == IPSET_INVALID_ID)
90 if (with_timeout(map->timeout) && list_set_expired(map, i))
94 ret = ip_set_test(elem->id, skb, par, opt);
99 ret = ip_set_add(elem->id, skb, par, opt);
104 ret = ip_set_del(elem->id, skb, par, opt);
116 id_eq(const struct list_set *map, u32 i, ip_set_id_t id)
118 const struct set_elem *elem;
121 elem = list_set_elem(map, i);
122 return elem->id == id;
129 id_eq_timeout(const struct list_set *map, u32 i, ip_set_id_t id)
131 const struct set_elem *elem;
134 elem = list_set_elem(map, i);
135 return !!(elem->id == id &&
136 !(with_timeout(map->timeout) &&
137 list_set_expired(map, i)));
144 list_elem_add(struct list_set *map, u32 i, ip_set_id_t id)
148 for (; i < map->size; i++) {
149 e = list_set_elem(map, i);
151 if (e->id == IPSET_INVALID_ID)
157 list_elem_tadd(struct list_set *map, u32 i, ip_set_id_t id,
158 unsigned long timeout)
162 for (; i < map->size; i++) {
163 e = list_set_telem(map, i);
165 swap(e->timeout, timeout);
166 if (e->id == IPSET_INVALID_ID)
172 list_set_add(struct list_set *map, u32 i, ip_set_id_t id,
173 unsigned long timeout)
175 const struct set_elem *e = list_set_elem(map, i);
177 if (i == map->size - 1 && e->id != IPSET_INVALID_ID)
178 /* Last element replaced: e.g. add new,before,last */
179 ip_set_put_byindex(e->id);
180 if (with_timeout(map->timeout))
181 list_elem_tadd(map, i, id, ip_set_timeout_set(timeout));
183 list_elem_add(map, i, id);
189 list_set_del(struct list_set *map, u32 i)
191 struct set_elem *a = list_set_elem(map, i), *b;
193 ip_set_put_byindex(a->id);
195 for (; i < map->size - 1; i++) {
196 b = list_set_elem(map, i + 1);
198 if (with_timeout(map->timeout))
199 ((struct set_telem *)a)->timeout =
200 ((struct set_telem *)b)->timeout;
202 if (a->id == IPSET_INVALID_ID)
206 a->id = IPSET_INVALID_ID;
211 cleanup_entries(struct list_set *map)
216 for (i = 0; i < map->size; i++) {
217 e = list_set_telem(map, i);
218 if (e->id != IPSET_INVALID_ID && list_set_expired(map, i))
219 list_set_del(map, i);
224 list_set_uadt(struct ip_set *set, struct nlattr *tb[],
225 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
227 struct list_set *map = set->data;
228 bool with_timeout = with_timeout(map->timeout);
229 bool flag_exist = flags & IPSET_FLAG_EXIST;
231 u32 timeout = map->timeout;
232 ip_set_id_t id, refid = IPSET_INVALID_ID;
233 const struct set_elem *elem;
238 if (unlikely(!tb[IPSET_ATTR_NAME] ||
239 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
240 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
241 return -IPSET_ERR_PROTOCOL;
243 if (tb[IPSET_ATTR_LINENO])
244 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
246 id = ip_set_get_byname(nla_data(tb[IPSET_ATTR_NAME]), &s);
247 if (id == IPSET_INVALID_ID)
248 return -IPSET_ERR_NAME;
249 /* "Loop detection" */
250 if (s->type->features & IPSET_TYPE_NAME) {
251 ret = -IPSET_ERR_LOOP;
255 if (tb[IPSET_ATTR_CADT_FLAGS]) {
256 u32 f = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
257 before = f & IPSET_FLAG_BEFORE;
260 if (before && !tb[IPSET_ATTR_NAMEREF]) {
261 ret = -IPSET_ERR_BEFORE;
265 if (tb[IPSET_ATTR_NAMEREF]) {
266 refid = ip_set_get_byname(nla_data(tb[IPSET_ATTR_NAMEREF]),
268 if (refid == IPSET_INVALID_ID) {
269 ret = -IPSET_ERR_NAMEREF;
275 if (tb[IPSET_ATTR_TIMEOUT]) {
277 ret = -IPSET_ERR_TIMEOUT;
280 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
282 if (with_timeout && adt != IPSET_TEST)
283 cleanup_entries(map);
287 for (i = 0; i < map->size && !ret; i++) {
288 elem = list_set_elem(map, i);
289 if (elem->id == IPSET_INVALID_ID ||
290 (before != 0 && i + 1 >= map->size))
292 else if (with_timeout && list_set_expired(map, i))
294 else if (before > 0 && elem->id == id)
295 ret = id_eq_timeout(map, i + 1, refid);
296 else if (before < 0 && elem->id == refid)
297 ret = id_eq_timeout(map, i + 1, id);
298 else if (before == 0 && elem->id == id)
303 for (i = 0; i < map->size; i++) {
304 elem = list_set_elem(map, i);
307 if (!(with_timeout && flag_exist)) {
308 ret = -IPSET_ERR_EXIST;
311 struct set_telem *e = list_set_telem(map, i);
314 !id_eq(map, i + 1, refid)) ||
316 (i == 0 || !id_eq(map, i - 1, refid)))) {
317 ret = -IPSET_ERR_EXIST;
320 e->timeout = ip_set_timeout_set(timeout);
321 ip_set_put_byindex(id);
326 ret = -IPSET_ERR_LIST_FULL;
327 for (i = 0; i < map->size && ret == -IPSET_ERR_LIST_FULL; i++) {
328 elem = list_set_elem(map, i);
329 if (elem->id == IPSET_INVALID_ID)
330 ret = before != 0 ? -IPSET_ERR_REF_EXIST
331 : list_set_add(map, i, id, timeout);
332 else if (elem->id != refid)
335 ret = list_set_add(map, i, id, timeout);
336 else if (i + 1 < map->size)
337 ret = list_set_add(map, i + 1, id, timeout);
341 ret = -IPSET_ERR_EXIST;
342 for (i = 0; i < map->size && ret == -IPSET_ERR_EXIST; i++) {
343 elem = list_set_elem(map, i);
344 if (elem->id == IPSET_INVALID_ID) {
345 ret = before != 0 ? -IPSET_ERR_REF_EXIST
348 } else if (elem->id == id &&
350 (before > 0 && id_eq(map, i + 1, refid))))
351 ret = list_set_del(map, i);
352 else if (elem->id == refid &&
353 before < 0 && id_eq(map, i + 1, id))
354 ret = list_set_del(map, i + 1);
362 if (refid != IPSET_INVALID_ID)
363 ip_set_put_byindex(refid);
364 if (adt != IPSET_ADD || ret)
365 ip_set_put_byindex(id);
367 return ip_set_eexist(ret, flags) ? 0 : ret;
371 list_set_flush(struct ip_set *set)
373 struct list_set *map = set->data;
374 struct set_elem *elem;
377 for (i = 0; i < map->size; i++) {
378 elem = list_set_elem(map, i);
379 if (elem->id != IPSET_INVALID_ID) {
380 ip_set_put_byindex(elem->id);
381 elem->id = IPSET_INVALID_ID;
387 list_set_destroy(struct ip_set *set)
389 struct list_set *map = set->data;
391 if (with_timeout(map->timeout))
392 del_timer_sync(&map->gc);
400 list_set_head(struct ip_set *set, struct sk_buff *skb)
402 const struct list_set *map = set->data;
403 struct nlattr *nested;
405 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
407 goto nla_put_failure;
408 if (nla_put_net32(skb, IPSET_ATTR_SIZE, htonl(map->size)) ||
409 (with_timeout(map->timeout) &&
410 nla_put_net32(skb, IPSET_ATTR_TIMEOUT, htonl(map->timeout))) ||
411 nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1)) ||
412 nla_put_net32(skb, IPSET_ATTR_MEMSIZE,
413 htonl(sizeof(*map) + map->size * map->dsize)))
414 goto nla_put_failure;
415 ipset_nest_end(skb, nested);
423 list_set_list(const struct ip_set *set,
424 struct sk_buff *skb, struct netlink_callback *cb)
426 const struct list_set *map = set->data;
427 struct nlattr *atd, *nested;
428 u32 i, first = cb->args[2];
429 const struct set_elem *e;
431 atd = ipset_nest_start(skb, IPSET_ATTR_ADT);
434 for (; cb->args[2] < map->size; cb->args[2]++) {
436 e = list_set_elem(map, i);
437 if (e->id == IPSET_INVALID_ID)
439 if (with_timeout(map->timeout) && list_set_expired(map, i))
441 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
444 nla_nest_cancel(skb, atd);
447 goto nla_put_failure;
449 if (nla_put_string(skb, IPSET_ATTR_NAME,
450 ip_set_name_byindex(e->id)))
451 goto nla_put_failure;
452 if (with_timeout(map->timeout)) {
453 const struct set_telem *te =
454 (const struct set_telem *) e;
455 __be32 to = htonl(ip_set_timeout_get(te->timeout));
456 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT, to))
457 goto nla_put_failure;
459 ipset_nest_end(skb, nested);
462 ipset_nest_end(skb, atd);
463 /* Set listing finished */
468 nla_nest_cancel(skb, nested);
469 ipset_nest_end(skb, atd);
470 if (unlikely(i == first)) {
478 list_set_same_set(const struct ip_set *a, const struct ip_set *b)
480 const struct list_set *x = a->data;
481 const struct list_set *y = b->data;
483 return x->size == y->size &&
484 x->timeout == y->timeout;
487 static const struct ip_set_type_variant list_set = {
488 .kadt = list_set_kadt,
489 .uadt = list_set_uadt,
490 .destroy = list_set_destroy,
491 .flush = list_set_flush,
492 .head = list_set_head,
493 .list = list_set_list,
494 .same_set = list_set_same_set,
498 list_set_gc(unsigned long ul_set)
500 struct ip_set *set = (struct ip_set *) ul_set;
501 struct list_set *map = set->data;
503 write_lock_bh(&set->lock);
504 cleanup_entries(map);
505 write_unlock_bh(&set->lock);
507 map->gc.expires = jiffies + IPSET_GC_PERIOD(map->timeout) * HZ;
512 list_set_gc_init(struct ip_set *set)
514 struct list_set *map = set->data;
516 init_timer(&map->gc);
517 map->gc.data = (unsigned long) set;
518 map->gc.function = list_set_gc;
519 map->gc.expires = jiffies + IPSET_GC_PERIOD(map->timeout) * HZ;
523 /* Create list:set type of sets */
526 init_list_set(struct ip_set *set, u32 size, size_t dsize,
527 unsigned long timeout)
529 struct list_set *map;
533 map = kzalloc(sizeof(*map) + size * dsize, GFP_KERNEL);
539 map->timeout = timeout;
542 for (i = 0; i < size; i++) {
543 e = list_set_elem(map, i);
544 e->id = IPSET_INVALID_ID;
551 list_set_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
553 u32 size = IP_SET_LIST_DEFAULT_SIZE;
555 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_SIZE) ||
556 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
557 return -IPSET_ERR_PROTOCOL;
559 if (tb[IPSET_ATTR_SIZE])
560 size = ip_set_get_h32(tb[IPSET_ATTR_SIZE]);
561 if (size < IP_SET_LIST_MIN_SIZE)
562 size = IP_SET_LIST_MIN_SIZE;
564 if (tb[IPSET_ATTR_TIMEOUT]) {
565 if (!init_list_set(set, size, sizeof(struct set_telem),
566 ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT])))
569 list_set_gc_init(set);
571 if (!init_list_set(set, size, sizeof(struct set_elem),
575 set->variant = &list_set;
579 static struct ip_set_type list_set_type __read_mostly = {
581 .protocol = IPSET_PROTOCOL,
582 .features = IPSET_TYPE_NAME | IPSET_DUMP_LAST,
583 .dimension = IPSET_DIM_ONE,
584 .family = NFPROTO_UNSPEC,
585 .revision_min = REVISION_MIN,
586 .revision_max = REVISION_MAX,
587 .create = list_set_create,
589 [IPSET_ATTR_SIZE] = { .type = NLA_U32 },
590 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
593 [IPSET_ATTR_NAME] = { .type = NLA_STRING,
594 .len = IPSET_MAXNAMELEN },
595 [IPSET_ATTR_NAMEREF] = { .type = NLA_STRING,
596 .len = IPSET_MAXNAMELEN },
597 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
598 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
599 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
607 return ip_set_type_register(&list_set_type);
613 ip_set_type_unregister(&list_set_type);
616 module_init(list_set_init);
617 module_exit(list_set_fini);