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 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
31 MODULE_DESCRIPTION("bitmap:ip,mac type of IP sets");
32 MODULE_ALIAS("ip_set_bitmap:ip,mac");
35 MAC_EMPTY, /* element is not set */
36 MAC_FILLED, /* element is set with MAC */
37 MAC_UNSET, /* element is set, without MAC */
42 void *members; /* the set members */
43 u32 first_ip; /* host byte order, included in range */
44 u32 last_ip; /* host byte order, included in range */
45 u32 timeout; /* timeout value */
46 struct timer_list gc; /* garbage collector */
47 size_t dsize; /* size of element */
50 /* ADT structure for generic function args */
52 u32 id; /* id in array */
53 unsigned char *ether; /* ethernet address */
56 /* Member element without and with timeout */
59 unsigned char ether[ETH_ALEN];
61 } __attribute__ ((aligned));
64 unsigned char ether[ETH_ALEN];
66 unsigned long timeout;
67 } __attribute__ ((aligned));
70 bitmap_ipmac_elem(const struct bitmap_ipmac *map, u32 id)
72 return (void *)((char *)map->members + id * map->dsize);
76 bitmap_timeout(const struct bitmap_ipmac *map, u32 id)
78 const struct ipmac_telem *elem = bitmap_ipmac_elem(map, id);
80 return ip_set_timeout_test(elem->timeout);
84 bitmap_expired(const struct bitmap_ipmac *map, u32 id)
86 const struct ipmac_telem *elem = bitmap_ipmac_elem(map, id);
88 return ip_set_timeout_expired(elem->timeout);
92 bitmap_ipmac_exist(const struct ipmac_telem *elem)
94 return elem->match == MAC_UNSET ||
95 (elem->match == MAC_FILLED &&
96 !ip_set_timeout_expired(elem->timeout));
102 bitmap_ipmac_test(struct ip_set *set, void *value, u32 timeout, u32 flags)
104 const struct bitmap_ipmac *map = set->data;
105 const struct ipmac *data = value;
106 const struct ipmac_elem *elem = bitmap_ipmac_elem(map, data->id);
108 switch (elem->match) {
110 /* Trigger kernel to fill out the ethernet address */
113 return data->ether == NULL ||
114 compare_ether_addr(data->ether, elem->ether) == 0;
120 bitmap_ipmac_add(struct ip_set *set, void *value, u32 timeout, u32 flags)
122 struct bitmap_ipmac *map = set->data;
123 const struct ipmac *data = value;
124 struct ipmac_elem *elem = bitmap_ipmac_elem(map, data->id);
126 switch (elem->match) {
129 /* Already added without ethernet address */
130 return -IPSET_ERR_EXIST;
131 /* Fill the MAC address */
132 memcpy(elem->ether, data->ether, ETH_ALEN);
133 elem->match = MAC_FILLED;
136 return -IPSET_ERR_EXIST;
139 memcpy(elem->ether, data->ether, ETH_ALEN);
140 elem->match = MAC_FILLED;
142 elem->match = MAC_UNSET;
149 bitmap_ipmac_del(struct ip_set *set, void *value, u32 timeout, u32 flags)
151 struct bitmap_ipmac *map = set->data;
152 const struct ipmac *data = value;
153 struct ipmac_elem *elem = bitmap_ipmac_elem(map, data->id);
155 if (elem->match == MAC_EMPTY)
156 return -IPSET_ERR_EXIST;
158 elem->match = MAC_EMPTY;
164 bitmap_ipmac_list(const struct ip_set *set,
165 struct sk_buff *skb, struct netlink_callback *cb)
167 const struct bitmap_ipmac *map = set->data;
168 const struct ipmac_elem *elem;
169 struct nlattr *atd, *nested;
170 u32 id, first = cb->args[2];
171 u32 last = map->last_ip - map->first_ip;
173 atd = ipset_nest_start(skb, IPSET_ATTR_ADT);
176 for (; cb->args[2] <= last; cb->args[2]++) {
178 elem = bitmap_ipmac_elem(map, id);
179 if (elem->match == MAC_EMPTY)
181 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
184 nla_nest_cancel(skb, atd);
187 goto nla_put_failure;
189 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP,
190 htonl(map->first_ip + id));
191 if (elem->match == MAC_FILLED)
192 NLA_PUT(skb, IPSET_ATTR_ETHER, ETH_ALEN,
194 ipset_nest_end(skb, nested);
196 ipset_nest_end(skb, atd);
197 /* Set listing finished */
203 nla_nest_cancel(skb, nested);
204 ipset_nest_end(skb, atd);
205 if (unlikely(id == first)) {
212 /* Timeout variant */
215 bitmap_ipmac_ttest(struct ip_set *set, void *value, u32 timeout, u32 flags)
217 const struct bitmap_ipmac *map = set->data;
218 const struct ipmac *data = value;
219 const struct ipmac_elem *elem = bitmap_ipmac_elem(map, data->id);
221 switch (elem->match) {
223 /* Trigger kernel to fill out the ethernet address */
226 return (data->ether == NULL ||
227 compare_ether_addr(data->ether, elem->ether) == 0) &&
228 !bitmap_expired(map, data->id);
234 bitmap_ipmac_tadd(struct ip_set *set, void *value, u32 timeout, u32 flags)
236 struct bitmap_ipmac *map = set->data;
237 const struct ipmac *data = value;
238 struct ipmac_telem *elem = bitmap_ipmac_elem(map, data->id);
239 bool flag_exist = flags & IPSET_FLAG_EXIST;
241 switch (elem->match) {
243 if (!(data->ether || flag_exist))
244 /* Already added without ethernet address */
245 return -IPSET_ERR_EXIST;
246 /* Fill the MAC address and activate the timer */
247 memcpy(elem->ether, data->ether, ETH_ALEN);
248 elem->match = MAC_FILLED;
249 if (timeout == map->timeout)
250 /* Timeout was not specified, get stored one */
251 timeout = elem->timeout;
252 elem->timeout = ip_set_timeout_set(timeout);
255 if (!(bitmap_expired(map, data->id) || flag_exist))
256 return -IPSET_ERR_EXIST;
260 memcpy(elem->ether, data->ether, ETH_ALEN);
261 elem->match = MAC_FILLED;
263 elem->match = MAC_UNSET;
264 /* If MAC is unset yet, we store plain timeout value
265 * because the timer is not activated yet
266 * and we can reuse it later when MAC is filled out,
267 * possibly by the kernel */
268 elem->timeout = data->ether ? ip_set_timeout_set(timeout)
277 bitmap_ipmac_tdel(struct ip_set *set, void *value, u32 timeout, u32 flags)
279 struct bitmap_ipmac *map = set->data;
280 const struct ipmac *data = value;
281 struct ipmac_telem *elem = bitmap_ipmac_elem(map, data->id);
283 if (elem->match == MAC_EMPTY || bitmap_expired(map, data->id))
284 return -IPSET_ERR_EXIST;
286 elem->match = MAC_EMPTY;
292 bitmap_ipmac_tlist(const struct ip_set *set,
293 struct sk_buff *skb, struct netlink_callback *cb)
295 const struct bitmap_ipmac *map = set->data;
296 const struct ipmac_telem *elem;
297 struct nlattr *atd, *nested;
298 u32 id, first = cb->args[2];
299 u32 timeout, last = map->last_ip - map->first_ip;
301 atd = ipset_nest_start(skb, IPSET_ATTR_ADT);
304 for (; cb->args[2] <= last; cb->args[2]++) {
306 elem = bitmap_ipmac_elem(map, id);
307 if (!bitmap_ipmac_exist(elem))
309 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
312 nla_nest_cancel(skb, atd);
315 goto nla_put_failure;
317 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP,
318 htonl(map->first_ip + id));
319 if (elem->match == MAC_FILLED)
320 NLA_PUT(skb, IPSET_ATTR_ETHER, ETH_ALEN,
322 timeout = elem->match == MAC_UNSET ? elem->timeout
323 : ip_set_timeout_get(elem->timeout);
324 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT, htonl(timeout));
325 ipset_nest_end(skb, nested);
327 ipset_nest_end(skb, atd);
328 /* Set listing finished */
334 nla_nest_cancel(skb, nested);
335 ipset_nest_end(skb, atd);
340 bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
341 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
343 struct bitmap_ipmac *map = set->data;
344 ipset_adtfn adtfn = set->variant->adt[adt];
347 /* MAC can be src only */
348 if (!(opt->flags & IPSET_DIM_TWO_SRC))
351 data.id = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
352 if (data.id < map->first_ip || data.id > map->last_ip)
353 return -IPSET_ERR_BITMAP_RANGE;
355 /* Backward compatibility: we don't check the second flag */
356 if (skb_mac_header(skb) < skb->head ||
357 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
360 data.id -= map->first_ip;
361 data.ether = eth_hdr(skb)->h_source;
363 return adtfn(set, &data, opt_timeout(opt, map), opt->cmdflags);
367 bitmap_ipmac_uadt(struct ip_set *set, struct nlattr *tb[],
368 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
370 const struct bitmap_ipmac *map = set->data;
371 ipset_adtfn adtfn = set->variant->adt[adt];
373 u32 timeout = map->timeout;
376 if (unlikely(!tb[IPSET_ATTR_IP] ||
377 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
378 return -IPSET_ERR_PROTOCOL;
380 if (tb[IPSET_ATTR_LINENO])
381 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
383 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &data.id);
387 if (data.id < map->first_ip || data.id > map->last_ip)
388 return -IPSET_ERR_BITMAP_RANGE;
390 if (tb[IPSET_ATTR_ETHER])
391 data.ether = nla_data(tb[IPSET_ATTR_ETHER]);
395 if (tb[IPSET_ATTR_TIMEOUT]) {
396 if (!with_timeout(map->timeout))
397 return -IPSET_ERR_TIMEOUT;
398 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
401 data.id -= map->first_ip;
403 ret = adtfn(set, &data, timeout, flags);
405 return ip_set_eexist(ret, flags) ? 0 : ret;
409 bitmap_ipmac_destroy(struct ip_set *set)
411 struct bitmap_ipmac *map = set->data;
413 if (with_timeout(map->timeout))
414 del_timer_sync(&map->gc);
416 ip_set_free(map->members);
423 bitmap_ipmac_flush(struct ip_set *set)
425 struct bitmap_ipmac *map = set->data;
427 memset(map->members, 0,
428 (map->last_ip - map->first_ip + 1) * map->dsize);
432 bitmap_ipmac_head(struct ip_set *set, struct sk_buff *skb)
434 const struct bitmap_ipmac *map = set->data;
435 struct nlattr *nested;
437 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
439 goto nla_put_failure;
440 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, htonl(map->first_ip));
441 NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip));
442 NLA_PUT_NET32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1));
443 NLA_PUT_NET32(skb, IPSET_ATTR_MEMSIZE,
445 + (map->last_ip - map->first_ip + 1) * map->dsize));
446 if (with_timeout(map->timeout))
447 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT, htonl(map->timeout));
448 ipset_nest_end(skb, nested);
456 bitmap_ipmac_same_set(const struct ip_set *a, const struct ip_set *b)
458 const struct bitmap_ipmac *x = a->data;
459 const struct bitmap_ipmac *y = b->data;
461 return x->first_ip == y->first_ip &&
462 x->last_ip == y->last_ip &&
463 x->timeout == y->timeout;
466 static const struct ip_set_type_variant bitmap_ipmac = {
467 .kadt = bitmap_ipmac_kadt,
468 .uadt = bitmap_ipmac_uadt,
470 [IPSET_ADD] = bitmap_ipmac_add,
471 [IPSET_DEL] = bitmap_ipmac_del,
472 [IPSET_TEST] = bitmap_ipmac_test,
474 .destroy = bitmap_ipmac_destroy,
475 .flush = bitmap_ipmac_flush,
476 .head = bitmap_ipmac_head,
477 .list = bitmap_ipmac_list,
478 .same_set = bitmap_ipmac_same_set,
481 static const struct ip_set_type_variant bitmap_tipmac = {
482 .kadt = bitmap_ipmac_kadt,
483 .uadt = bitmap_ipmac_uadt,
485 [IPSET_ADD] = bitmap_ipmac_tadd,
486 [IPSET_DEL] = bitmap_ipmac_tdel,
487 [IPSET_TEST] = bitmap_ipmac_ttest,
489 .destroy = bitmap_ipmac_destroy,
490 .flush = bitmap_ipmac_flush,
491 .head = bitmap_ipmac_head,
492 .list = bitmap_ipmac_tlist,
493 .same_set = bitmap_ipmac_same_set,
497 bitmap_ipmac_gc(unsigned long ul_set)
499 struct ip_set *set = (struct ip_set *) ul_set;
500 struct bitmap_ipmac *map = set->data;
501 struct ipmac_telem *elem;
502 u32 id, last = map->last_ip - map->first_ip;
504 /* We run parallel with other readers (test element)
505 * but adding/deleting new entries is locked out */
506 read_lock_bh(&set->lock);
507 for (id = 0; id <= last; id++) {
508 elem = bitmap_ipmac_elem(map, id);
509 if (elem->match == MAC_FILLED &&
510 ip_set_timeout_expired(elem->timeout))
511 elem->match = MAC_EMPTY;
513 read_unlock_bh(&set->lock);
515 map->gc.expires = jiffies + IPSET_GC_PERIOD(map->timeout) * HZ;
520 bitmap_ipmac_gc_init(struct ip_set *set)
522 struct bitmap_ipmac *map = set->data;
524 init_timer(&map->gc);
525 map->gc.data = (unsigned long) set;
526 map->gc.function = bitmap_ipmac_gc;
527 map->gc.expires = jiffies + IPSET_GC_PERIOD(map->timeout) * HZ;
531 /* Create bitmap:ip,mac type of sets */
534 init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
535 u32 first_ip, u32 last_ip)
537 map->members = ip_set_alloc((last_ip - first_ip + 1) * map->dsize);
540 map->first_ip = first_ip;
541 map->last_ip = last_ip;
542 map->timeout = IPSET_NO_TIMEOUT;
545 set->family = AF_INET;
551 bitmap_ipmac_create(struct ip_set *set, struct nlattr *tb[],
554 u32 first_ip, last_ip, elements;
555 struct bitmap_ipmac *map;
558 if (unlikely(!tb[IPSET_ATTR_IP] ||
559 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
560 return -IPSET_ERR_PROTOCOL;
562 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
566 if (tb[IPSET_ATTR_IP_TO]) {
567 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
570 if (first_ip > last_ip) {
576 } else if (tb[IPSET_ATTR_CIDR]) {
577 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
580 return -IPSET_ERR_INVALID_CIDR;
581 ip_set_mask_from_to(first_ip, last_ip, cidr);
583 return -IPSET_ERR_PROTOCOL;
585 elements = last_ip - first_ip + 1;
587 if (elements > IPSET_BITMAP_MAX_RANGE + 1)
588 return -IPSET_ERR_BITMAP_RANGE_SIZE;
590 map = kzalloc(sizeof(*map), GFP_KERNEL);
594 if (tb[IPSET_ATTR_TIMEOUT]) {
595 map->dsize = sizeof(struct ipmac_telem);
597 if (!init_map_ipmac(set, map, first_ip, last_ip)) {
602 map->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
604 set->variant = &bitmap_tipmac;
606 bitmap_ipmac_gc_init(set);
608 map->dsize = sizeof(struct ipmac_elem);
610 if (!init_map_ipmac(set, map, first_ip, last_ip)) {
614 set->variant = &bitmap_ipmac;
620 static struct ip_set_type bitmap_ipmac_type = {
621 .name = "bitmap:ip,mac",
622 .protocol = IPSET_PROTOCOL,
623 .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
624 .dimension = IPSET_DIM_TWO,
628 .create = bitmap_ipmac_create,
630 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
631 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
632 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
633 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
636 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
637 [IPSET_ATTR_ETHER] = { .type = NLA_BINARY, .len = ETH_ALEN },
638 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
639 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
645 bitmap_ipmac_init(void)
647 return ip_set_type_register(&bitmap_ipmac_type);
651 bitmap_ipmac_fini(void)
653 ip_set_type_unregister(&bitmap_ipmac_type);
656 module_init(bitmap_ipmac_init);
657 module_exit(bitmap_ipmac_fini);