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-2011 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 implementing an IP set type: the bitmap:ip,mac type */
13 #include <linux/module.h>
15 #include <linux/etherdevice.h>
16 #include <linux/skbuff.h>
17 #include <linux/errno.h>
18 #include <linux/if_ether.h>
19 #include <linux/netlink.h>
20 #include <linux/jiffies.h>
21 #include <linux/timer.h>
22 #include <net/netlink.h>
24 #include <linux/netfilter/ipset/pfxlen.h>
25 #include <linux/netfilter/ipset/ip_set.h>
26 #include <linux/netfilter/ipset/ip_set_timeout.h>
27 #include <linux/netfilter/ipset/ip_set_bitmap.h>
29 #define REVISION_MIN 0
30 #define REVISION_MAX 0
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
34 IP_SET_MODULE_DESC("bitmap:ip,mac", REVISION_MIN, REVISION_MAX);
35 MODULE_ALIAS("ip_set_bitmap:ip,mac");
38 MAC_EMPTY, /* element is not set */
39 MAC_FILLED, /* element is set with MAC */
40 MAC_UNSET, /* element is set, without MAC */
45 void *members; /* the set members */
46 u32 first_ip; /* host byte order, included in range */
47 u32 last_ip; /* host byte order, included in range */
48 u32 timeout; /* timeout value */
49 struct timer_list gc; /* garbage collector */
50 size_t dsize; /* size of element */
53 /* ADT structure for generic function args */
55 u32 id; /* id in array */
56 unsigned char *ether; /* ethernet address */
59 /* Member element without and with timeout */
62 unsigned char ether[ETH_ALEN];
64 } __attribute__ ((aligned));
67 unsigned char ether[ETH_ALEN];
69 unsigned long timeout;
70 } __attribute__ ((aligned));
73 bitmap_ipmac_elem(const struct bitmap_ipmac *map, u32 id)
75 return (void *)((char *)map->members + id * map->dsize);
79 bitmap_timeout(const struct bitmap_ipmac *map, u32 id)
81 const struct ipmac_telem *elem = bitmap_ipmac_elem(map, id);
83 return ip_set_timeout_test(elem->timeout);
87 bitmap_expired(const struct bitmap_ipmac *map, u32 id)
89 const struct ipmac_telem *elem = bitmap_ipmac_elem(map, id);
91 return ip_set_timeout_expired(elem->timeout);
95 bitmap_ipmac_exist(const struct ipmac_telem *elem)
97 return elem->match == MAC_UNSET ||
98 (elem->match == MAC_FILLED &&
99 !ip_set_timeout_expired(elem->timeout));
105 bitmap_ipmac_test(struct ip_set *set, void *value, u32 timeout, u32 flags)
107 const struct bitmap_ipmac *map = set->data;
108 const struct ipmac *data = value;
109 const struct ipmac_elem *elem = bitmap_ipmac_elem(map, data->id);
111 switch (elem->match) {
113 /* Trigger kernel to fill out the ethernet address */
116 return data->ether == NULL ||
117 ether_addr_equal(data->ether, elem->ether);
123 bitmap_ipmac_add(struct ip_set *set, void *value, u32 timeout, u32 flags)
125 struct bitmap_ipmac *map = set->data;
126 const struct ipmac *data = value;
127 struct ipmac_elem *elem = bitmap_ipmac_elem(map, data->id);
129 switch (elem->match) {
132 /* Already added without ethernet address */
133 return -IPSET_ERR_EXIST;
134 /* Fill the MAC address */
135 memcpy(elem->ether, data->ether, ETH_ALEN);
136 elem->match = MAC_FILLED;
139 return -IPSET_ERR_EXIST;
142 memcpy(elem->ether, data->ether, ETH_ALEN);
143 elem->match = MAC_FILLED;
145 elem->match = MAC_UNSET;
152 bitmap_ipmac_del(struct ip_set *set, void *value, u32 timeout, u32 flags)
154 struct bitmap_ipmac *map = set->data;
155 const struct ipmac *data = value;
156 struct ipmac_elem *elem = bitmap_ipmac_elem(map, data->id);
158 if (elem->match == MAC_EMPTY)
159 return -IPSET_ERR_EXIST;
161 elem->match = MAC_EMPTY;
167 bitmap_ipmac_list(const struct ip_set *set,
168 struct sk_buff *skb, struct netlink_callback *cb)
170 const struct bitmap_ipmac *map = set->data;
171 const struct ipmac_elem *elem;
172 struct nlattr *atd, *nested;
173 u32 id, first = cb->args[2];
174 u32 last = map->last_ip - map->first_ip;
176 atd = ipset_nest_start(skb, IPSET_ATTR_ADT);
179 for (; cb->args[2] <= last; cb->args[2]++) {
181 elem = bitmap_ipmac_elem(map, id);
182 if (elem->match == MAC_EMPTY)
184 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
187 nla_nest_cancel(skb, atd);
190 goto nla_put_failure;
192 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP,
193 htonl(map->first_ip + id)) ||
194 (elem->match == MAC_FILLED &&
195 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN,
197 goto nla_put_failure;
198 ipset_nest_end(skb, nested);
200 ipset_nest_end(skb, atd);
201 /* Set listing finished */
207 nla_nest_cancel(skb, nested);
208 ipset_nest_end(skb, atd);
209 if (unlikely(id == first)) {
216 /* Timeout variant */
219 bitmap_ipmac_ttest(struct ip_set *set, void *value, u32 timeout, u32 flags)
221 const struct bitmap_ipmac *map = set->data;
222 const struct ipmac *data = value;
223 const struct ipmac_elem *elem = bitmap_ipmac_elem(map, data->id);
225 switch (elem->match) {
227 /* Trigger kernel to fill out the ethernet address */
230 return (data->ether == NULL ||
231 ether_addr_equal(data->ether, elem->ether)) &&
232 !bitmap_expired(map, data->id);
238 bitmap_ipmac_tadd(struct ip_set *set, void *value, u32 timeout, u32 flags)
240 struct bitmap_ipmac *map = set->data;
241 const struct ipmac *data = value;
242 struct ipmac_telem *elem = bitmap_ipmac_elem(map, data->id);
243 bool flag_exist = flags & IPSET_FLAG_EXIST;
245 switch (elem->match) {
247 if (!(data->ether || flag_exist))
248 /* Already added without ethernet address */
249 return -IPSET_ERR_EXIST;
250 /* Fill the MAC address and activate the timer */
251 memcpy(elem->ether, data->ether, ETH_ALEN);
252 elem->match = MAC_FILLED;
253 if (timeout == map->timeout)
254 /* Timeout was not specified, get stored one */
255 timeout = elem->timeout;
256 elem->timeout = ip_set_timeout_set(timeout);
259 if (!(bitmap_expired(map, data->id) || flag_exist))
260 return -IPSET_ERR_EXIST;
264 memcpy(elem->ether, data->ether, ETH_ALEN);
265 elem->match = MAC_FILLED;
267 elem->match = MAC_UNSET;
268 /* If MAC is unset yet, we store plain timeout value
269 * because the timer is not activated yet
270 * and we can reuse it later when MAC is filled out,
271 * possibly by the kernel */
272 elem->timeout = data->ether ? ip_set_timeout_set(timeout)
281 bitmap_ipmac_tdel(struct ip_set *set, void *value, u32 timeout, u32 flags)
283 struct bitmap_ipmac *map = set->data;
284 const struct ipmac *data = value;
285 struct ipmac_telem *elem = bitmap_ipmac_elem(map, data->id);
287 if (elem->match == MAC_EMPTY || bitmap_expired(map, data->id))
288 return -IPSET_ERR_EXIST;
290 elem->match = MAC_EMPTY;
296 bitmap_ipmac_tlist(const struct ip_set *set,
297 struct sk_buff *skb, struct netlink_callback *cb)
299 const struct bitmap_ipmac *map = set->data;
300 const struct ipmac_telem *elem;
301 struct nlattr *atd, *nested;
302 u32 id, first = cb->args[2];
303 u32 timeout, last = map->last_ip - map->first_ip;
305 atd = ipset_nest_start(skb, IPSET_ATTR_ADT);
308 for (; cb->args[2] <= last; cb->args[2]++) {
310 elem = bitmap_ipmac_elem(map, id);
311 if (!bitmap_ipmac_exist(elem))
313 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
316 nla_nest_cancel(skb, atd);
319 goto nla_put_failure;
321 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP,
322 htonl(map->first_ip + id)) ||
323 (elem->match == MAC_FILLED &&
324 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN,
326 goto nla_put_failure;
327 timeout = elem->match == MAC_UNSET ? elem->timeout
328 : ip_set_timeout_get(elem->timeout);
329 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT, htonl(timeout)))
330 goto nla_put_failure;
331 ipset_nest_end(skb, nested);
333 ipset_nest_end(skb, atd);
334 /* Set listing finished */
340 nla_nest_cancel(skb, nested);
341 ipset_nest_end(skb, atd);
346 bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
347 const struct xt_action_param *par,
348 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
350 struct bitmap_ipmac *map = set->data;
351 ipset_adtfn adtfn = set->variant->adt[adt];
354 /* MAC can be src only */
355 if (!(opt->flags & IPSET_DIM_TWO_SRC))
358 data.id = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
359 if (data.id < map->first_ip || data.id > map->last_ip)
360 return -IPSET_ERR_BITMAP_RANGE;
362 /* Backward compatibility: we don't check the second flag */
363 if (skb_mac_header(skb) < skb->head ||
364 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
367 data.id -= map->first_ip;
368 data.ether = eth_hdr(skb)->h_source;
370 return adtfn(set, &data, opt_timeout(opt, map), opt->cmdflags);
374 bitmap_ipmac_uadt(struct ip_set *set, struct nlattr *tb[],
375 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
377 const struct bitmap_ipmac *map = set->data;
378 ipset_adtfn adtfn = set->variant->adt[adt];
380 u32 timeout = map->timeout;
383 if (unlikely(!tb[IPSET_ATTR_IP] ||
384 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
385 return -IPSET_ERR_PROTOCOL;
387 if (tb[IPSET_ATTR_LINENO])
388 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
390 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &data.id);
394 if (data.id < map->first_ip || data.id > map->last_ip)
395 return -IPSET_ERR_BITMAP_RANGE;
397 if (tb[IPSET_ATTR_ETHER])
398 data.ether = nla_data(tb[IPSET_ATTR_ETHER]);
402 if (tb[IPSET_ATTR_TIMEOUT]) {
403 if (!with_timeout(map->timeout))
404 return -IPSET_ERR_TIMEOUT;
405 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
408 data.id -= map->first_ip;
410 ret = adtfn(set, &data, timeout, flags);
412 return ip_set_eexist(ret, flags) ? 0 : ret;
416 bitmap_ipmac_destroy(struct ip_set *set)
418 struct bitmap_ipmac *map = set->data;
420 if (with_timeout(map->timeout))
421 del_timer_sync(&map->gc);
423 ip_set_free(map->members);
430 bitmap_ipmac_flush(struct ip_set *set)
432 struct bitmap_ipmac *map = set->data;
434 memset(map->members, 0,
435 (map->last_ip - map->first_ip + 1) * map->dsize);
439 bitmap_ipmac_head(struct ip_set *set, struct sk_buff *skb)
441 const struct bitmap_ipmac *map = set->data;
442 struct nlattr *nested;
444 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
446 goto nla_put_failure;
447 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, htonl(map->first_ip)) ||
448 nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip)) ||
449 nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1)) ||
450 nla_put_net32(skb, IPSET_ATTR_MEMSIZE,
452 ((map->last_ip - map->first_ip + 1) *
454 (with_timeout(map->timeout) &&
455 nla_put_net32(skb, IPSET_ATTR_TIMEOUT, htonl(map->timeout))))
456 goto nla_put_failure;
457 ipset_nest_end(skb, nested);
465 bitmap_ipmac_same_set(const struct ip_set *a, const struct ip_set *b)
467 const struct bitmap_ipmac *x = a->data;
468 const struct bitmap_ipmac *y = b->data;
470 return x->first_ip == y->first_ip &&
471 x->last_ip == y->last_ip &&
472 x->timeout == y->timeout;
475 static const struct ip_set_type_variant bitmap_ipmac = {
476 .kadt = bitmap_ipmac_kadt,
477 .uadt = bitmap_ipmac_uadt,
479 [IPSET_ADD] = bitmap_ipmac_add,
480 [IPSET_DEL] = bitmap_ipmac_del,
481 [IPSET_TEST] = bitmap_ipmac_test,
483 .destroy = bitmap_ipmac_destroy,
484 .flush = bitmap_ipmac_flush,
485 .head = bitmap_ipmac_head,
486 .list = bitmap_ipmac_list,
487 .same_set = bitmap_ipmac_same_set,
490 static const struct ip_set_type_variant bitmap_tipmac = {
491 .kadt = bitmap_ipmac_kadt,
492 .uadt = bitmap_ipmac_uadt,
494 [IPSET_ADD] = bitmap_ipmac_tadd,
495 [IPSET_DEL] = bitmap_ipmac_tdel,
496 [IPSET_TEST] = bitmap_ipmac_ttest,
498 .destroy = bitmap_ipmac_destroy,
499 .flush = bitmap_ipmac_flush,
500 .head = bitmap_ipmac_head,
501 .list = bitmap_ipmac_tlist,
502 .same_set = bitmap_ipmac_same_set,
506 bitmap_ipmac_gc(unsigned long ul_set)
508 struct ip_set *set = (struct ip_set *) ul_set;
509 struct bitmap_ipmac *map = set->data;
510 struct ipmac_telem *elem;
511 u32 id, last = map->last_ip - map->first_ip;
513 /* We run parallel with other readers (test element)
514 * but adding/deleting new entries is locked out */
515 read_lock_bh(&set->lock);
516 for (id = 0; id <= last; id++) {
517 elem = bitmap_ipmac_elem(map, id);
518 if (elem->match == MAC_FILLED &&
519 ip_set_timeout_expired(elem->timeout))
520 elem->match = MAC_EMPTY;
522 read_unlock_bh(&set->lock);
524 map->gc.expires = jiffies + IPSET_GC_PERIOD(map->timeout) * HZ;
529 bitmap_ipmac_gc_init(struct ip_set *set)
531 struct bitmap_ipmac *map = set->data;
533 init_timer(&map->gc);
534 map->gc.data = (unsigned long) set;
535 map->gc.function = bitmap_ipmac_gc;
536 map->gc.expires = jiffies + IPSET_GC_PERIOD(map->timeout) * HZ;
540 /* Create bitmap:ip,mac type of sets */
543 init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
544 u32 first_ip, u32 last_ip)
546 map->members = ip_set_alloc((last_ip - first_ip + 1) * map->dsize);
549 map->first_ip = first_ip;
550 map->last_ip = last_ip;
551 map->timeout = IPSET_NO_TIMEOUT;
554 set->family = NFPROTO_IPV4;
560 bitmap_ipmac_create(struct ip_set *set, struct nlattr *tb[],
563 u32 first_ip, last_ip;
565 struct bitmap_ipmac *map;
568 if (unlikely(!tb[IPSET_ATTR_IP] ||
569 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
570 return -IPSET_ERR_PROTOCOL;
572 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
576 if (tb[IPSET_ATTR_IP_TO]) {
577 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
580 if (first_ip > last_ip) {
586 } else if (tb[IPSET_ATTR_CIDR]) {
587 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
590 return -IPSET_ERR_INVALID_CIDR;
591 ip_set_mask_from_to(first_ip, last_ip, cidr);
593 return -IPSET_ERR_PROTOCOL;
595 elements = (u64)last_ip - first_ip + 1;
597 if (elements > IPSET_BITMAP_MAX_RANGE + 1)
598 return -IPSET_ERR_BITMAP_RANGE_SIZE;
600 map = kzalloc(sizeof(*map), GFP_KERNEL);
604 if (tb[IPSET_ATTR_TIMEOUT]) {
605 map->dsize = sizeof(struct ipmac_telem);
607 if (!init_map_ipmac(set, map, first_ip, last_ip)) {
612 map->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
614 set->variant = &bitmap_tipmac;
616 bitmap_ipmac_gc_init(set);
618 map->dsize = sizeof(struct ipmac_elem);
620 if (!init_map_ipmac(set, map, first_ip, last_ip)) {
624 set->variant = &bitmap_ipmac;
630 static struct ip_set_type bitmap_ipmac_type = {
631 .name = "bitmap:ip,mac",
632 .protocol = IPSET_PROTOCOL,
633 .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
634 .dimension = IPSET_DIM_TWO,
635 .family = NFPROTO_IPV4,
636 .revision_min = REVISION_MIN,
637 .revision_max = REVISION_MAX,
638 .create = bitmap_ipmac_create,
640 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
641 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
642 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
643 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
646 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
647 [IPSET_ATTR_ETHER] = { .type = NLA_BINARY,
649 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
650 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
656 bitmap_ipmac_init(void)
658 return ip_set_type_register(&bitmap_ipmac_type);
662 bitmap_ipmac_fini(void)
664 ip_set_type_unregister(&bitmap_ipmac_type);
667 module_init(bitmap_ipmac_init);
668 module_exit(bitmap_ipmac_fini);