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 ether_addr_equal(data->ether, elem->ether);
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 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP,
190 htonl(map->first_ip + id)) ||
191 (elem->match == MAC_FILLED &&
192 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN,
194 goto nla_put_failure;
195 ipset_nest_end(skb, nested);
197 ipset_nest_end(skb, atd);
198 /* Set listing finished */
204 nla_nest_cancel(skb, nested);
205 ipset_nest_end(skb, atd);
206 if (unlikely(id == first)) {
213 /* Timeout variant */
216 bitmap_ipmac_ttest(struct ip_set *set, void *value, u32 timeout, u32 flags)
218 const struct bitmap_ipmac *map = set->data;
219 const struct ipmac *data = value;
220 const struct ipmac_elem *elem = bitmap_ipmac_elem(map, data->id);
222 switch (elem->match) {
224 /* Trigger kernel to fill out the ethernet address */
227 return (data->ether == NULL ||
228 ether_addr_equal(data->ether, elem->ether)) &&
229 !bitmap_expired(map, data->id);
235 bitmap_ipmac_tadd(struct ip_set *set, void *value, u32 timeout, u32 flags)
237 struct bitmap_ipmac *map = set->data;
238 const struct ipmac *data = value;
239 struct ipmac_telem *elem = bitmap_ipmac_elem(map, data->id);
240 bool flag_exist = flags & IPSET_FLAG_EXIST;
242 switch (elem->match) {
244 if (!(data->ether || flag_exist))
245 /* Already added without ethernet address */
246 return -IPSET_ERR_EXIST;
247 /* Fill the MAC address and activate the timer */
248 memcpy(elem->ether, data->ether, ETH_ALEN);
249 elem->match = MAC_FILLED;
250 if (timeout == map->timeout)
251 /* Timeout was not specified, get stored one */
252 timeout = elem->timeout;
253 elem->timeout = ip_set_timeout_set(timeout);
256 if (!(bitmap_expired(map, data->id) || flag_exist))
257 return -IPSET_ERR_EXIST;
261 memcpy(elem->ether, data->ether, ETH_ALEN);
262 elem->match = MAC_FILLED;
264 elem->match = MAC_UNSET;
265 /* If MAC is unset yet, we store plain timeout value
266 * because the timer is not activated yet
267 * and we can reuse it later when MAC is filled out,
268 * possibly by the kernel */
269 elem->timeout = data->ether ? ip_set_timeout_set(timeout)
278 bitmap_ipmac_tdel(struct ip_set *set, void *value, u32 timeout, u32 flags)
280 struct bitmap_ipmac *map = set->data;
281 const struct ipmac *data = value;
282 struct ipmac_telem *elem = bitmap_ipmac_elem(map, data->id);
284 if (elem->match == MAC_EMPTY || bitmap_expired(map, data->id))
285 return -IPSET_ERR_EXIST;
287 elem->match = MAC_EMPTY;
293 bitmap_ipmac_tlist(const struct ip_set *set,
294 struct sk_buff *skb, struct netlink_callback *cb)
296 const struct bitmap_ipmac *map = set->data;
297 const struct ipmac_telem *elem;
298 struct nlattr *atd, *nested;
299 u32 id, first = cb->args[2];
300 u32 timeout, last = map->last_ip - map->first_ip;
302 atd = ipset_nest_start(skb, IPSET_ATTR_ADT);
305 for (; cb->args[2] <= last; cb->args[2]++) {
307 elem = bitmap_ipmac_elem(map, id);
308 if (!bitmap_ipmac_exist(elem))
310 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
313 nla_nest_cancel(skb, atd);
316 goto nla_put_failure;
318 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP,
319 htonl(map->first_ip + id)) ||
320 (elem->match == MAC_FILLED &&
321 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN,
323 goto nla_put_failure;
324 timeout = elem->match == MAC_UNSET ? elem->timeout
325 : ip_set_timeout_get(elem->timeout);
326 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT, htonl(timeout)))
327 goto nla_put_failure;
328 ipset_nest_end(skb, nested);
330 ipset_nest_end(skb, atd);
331 /* Set listing finished */
337 nla_nest_cancel(skb, nested);
338 ipset_nest_end(skb, atd);
343 bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
344 const struct xt_action_param *par,
345 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
347 struct bitmap_ipmac *map = set->data;
348 ipset_adtfn adtfn = set->variant->adt[adt];
351 /* MAC can be src only */
352 if (!(opt->flags & IPSET_DIM_TWO_SRC))
355 data.id = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
356 if (data.id < map->first_ip || data.id > map->last_ip)
357 return -IPSET_ERR_BITMAP_RANGE;
359 /* Backward compatibility: we don't check the second flag */
360 if (skb_mac_header(skb) < skb->head ||
361 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
364 data.id -= map->first_ip;
365 data.ether = eth_hdr(skb)->h_source;
367 return adtfn(set, &data, opt_timeout(opt, map), opt->cmdflags);
371 bitmap_ipmac_uadt(struct ip_set *set, struct nlattr *tb[],
372 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
374 const struct bitmap_ipmac *map = set->data;
375 ipset_adtfn adtfn = set->variant->adt[adt];
377 u32 timeout = map->timeout;
380 if (unlikely(!tb[IPSET_ATTR_IP] ||
381 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
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_hostipaddr4(tb[IPSET_ATTR_IP], &data.id);
391 if (data.id < map->first_ip || data.id > map->last_ip)
392 return -IPSET_ERR_BITMAP_RANGE;
394 if (tb[IPSET_ATTR_ETHER])
395 data.ether = nla_data(tb[IPSET_ATTR_ETHER]);
399 if (tb[IPSET_ATTR_TIMEOUT]) {
400 if (!with_timeout(map->timeout))
401 return -IPSET_ERR_TIMEOUT;
402 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
405 data.id -= map->first_ip;
407 ret = adtfn(set, &data, timeout, flags);
409 return ip_set_eexist(ret, flags) ? 0 : ret;
413 bitmap_ipmac_destroy(struct ip_set *set)
415 struct bitmap_ipmac *map = set->data;
417 if (with_timeout(map->timeout))
418 del_timer_sync(&map->gc);
420 ip_set_free(map->members);
427 bitmap_ipmac_flush(struct ip_set *set)
429 struct bitmap_ipmac *map = set->data;
431 memset(map->members, 0,
432 (map->last_ip - map->first_ip + 1) * map->dsize);
436 bitmap_ipmac_head(struct ip_set *set, struct sk_buff *skb)
438 const struct bitmap_ipmac *map = set->data;
439 struct nlattr *nested;
441 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
443 goto nla_put_failure;
444 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, htonl(map->first_ip)) ||
445 nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip)) ||
446 nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1)) ||
447 nla_put_net32(skb, IPSET_ATTR_MEMSIZE,
449 ((map->last_ip - map->first_ip + 1) *
451 (with_timeout(map->timeout) &&
452 nla_put_net32(skb, IPSET_ATTR_TIMEOUT, htonl(map->timeout))))
453 goto nla_put_failure;
454 ipset_nest_end(skb, nested);
462 bitmap_ipmac_same_set(const struct ip_set *a, const struct ip_set *b)
464 const struct bitmap_ipmac *x = a->data;
465 const struct bitmap_ipmac *y = b->data;
467 return x->first_ip == y->first_ip &&
468 x->last_ip == y->last_ip &&
469 x->timeout == y->timeout;
472 static const struct ip_set_type_variant bitmap_ipmac = {
473 .kadt = bitmap_ipmac_kadt,
474 .uadt = bitmap_ipmac_uadt,
476 [IPSET_ADD] = bitmap_ipmac_add,
477 [IPSET_DEL] = bitmap_ipmac_del,
478 [IPSET_TEST] = bitmap_ipmac_test,
480 .destroy = bitmap_ipmac_destroy,
481 .flush = bitmap_ipmac_flush,
482 .head = bitmap_ipmac_head,
483 .list = bitmap_ipmac_list,
484 .same_set = bitmap_ipmac_same_set,
487 static const struct ip_set_type_variant bitmap_tipmac = {
488 .kadt = bitmap_ipmac_kadt,
489 .uadt = bitmap_ipmac_uadt,
491 [IPSET_ADD] = bitmap_ipmac_tadd,
492 [IPSET_DEL] = bitmap_ipmac_tdel,
493 [IPSET_TEST] = bitmap_ipmac_ttest,
495 .destroy = bitmap_ipmac_destroy,
496 .flush = bitmap_ipmac_flush,
497 .head = bitmap_ipmac_head,
498 .list = bitmap_ipmac_tlist,
499 .same_set = bitmap_ipmac_same_set,
503 bitmap_ipmac_gc(unsigned long ul_set)
505 struct ip_set *set = (struct ip_set *) ul_set;
506 struct bitmap_ipmac *map = set->data;
507 struct ipmac_telem *elem;
508 u32 id, last = map->last_ip - map->first_ip;
510 /* We run parallel with other readers (test element)
511 * but adding/deleting new entries is locked out */
512 read_lock_bh(&set->lock);
513 for (id = 0; id <= last; id++) {
514 elem = bitmap_ipmac_elem(map, id);
515 if (elem->match == MAC_FILLED &&
516 ip_set_timeout_expired(elem->timeout))
517 elem->match = MAC_EMPTY;
519 read_unlock_bh(&set->lock);
521 map->gc.expires = jiffies + IPSET_GC_PERIOD(map->timeout) * HZ;
526 bitmap_ipmac_gc_init(struct ip_set *set)
528 struct bitmap_ipmac *map = set->data;
530 init_timer(&map->gc);
531 map->gc.data = (unsigned long) set;
532 map->gc.function = bitmap_ipmac_gc;
533 map->gc.expires = jiffies + IPSET_GC_PERIOD(map->timeout) * HZ;
537 /* Create bitmap:ip,mac type of sets */
540 init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
541 u32 first_ip, u32 last_ip)
543 map->members = ip_set_alloc((last_ip - first_ip + 1) * map->dsize);
546 map->first_ip = first_ip;
547 map->last_ip = last_ip;
548 map->timeout = IPSET_NO_TIMEOUT;
551 set->family = NFPROTO_IPV4;
557 bitmap_ipmac_create(struct ip_set *set, struct nlattr *tb[],
560 u32 first_ip, last_ip, elements;
561 struct bitmap_ipmac *map;
564 if (unlikely(!tb[IPSET_ATTR_IP] ||
565 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
566 return -IPSET_ERR_PROTOCOL;
568 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
572 if (tb[IPSET_ATTR_IP_TO]) {
573 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
576 if (first_ip > last_ip) {
582 } else if (tb[IPSET_ATTR_CIDR]) {
583 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
586 return -IPSET_ERR_INVALID_CIDR;
587 ip_set_mask_from_to(first_ip, last_ip, cidr);
589 return -IPSET_ERR_PROTOCOL;
591 elements = last_ip - first_ip + 1;
593 if (elements > IPSET_BITMAP_MAX_RANGE + 1)
594 return -IPSET_ERR_BITMAP_RANGE_SIZE;
596 map = kzalloc(sizeof(*map), GFP_KERNEL);
600 if (tb[IPSET_ATTR_TIMEOUT]) {
601 map->dsize = sizeof(struct ipmac_telem);
603 if (!init_map_ipmac(set, map, first_ip, last_ip)) {
608 map->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
610 set->variant = &bitmap_tipmac;
612 bitmap_ipmac_gc_init(set);
614 map->dsize = sizeof(struct ipmac_elem);
616 if (!init_map_ipmac(set, map, first_ip, last_ip)) {
620 set->variant = &bitmap_ipmac;
626 static struct ip_set_type bitmap_ipmac_type = {
627 .name = "bitmap:ip,mac",
628 .protocol = IPSET_PROTOCOL,
629 .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
630 .dimension = IPSET_DIM_TWO,
631 .family = NFPROTO_IPV4,
634 .create = bitmap_ipmac_create,
636 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
637 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
638 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
639 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
642 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
643 [IPSET_ATTR_ETHER] = { .type = NLA_BINARY,
645 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
646 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
652 bitmap_ipmac_init(void)
654 return ip_set_type_register(&bitmap_ipmac_type);
658 bitmap_ipmac_fini(void)
660 ip_set_type_unregister(&bitmap_ipmac_type);
663 module_init(bitmap_ipmac_init);
664 module_exit(bitmap_ipmac_fini);