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-2013 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_bitmap.h>
28 #define REVISION_MIN 0
29 #define REVISION_MAX 1 /* Counter support added */
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
33 IP_SET_MODULE_DESC("bitmap:ip,mac", REVISION_MIN, REVISION_MAX);
34 MODULE_ALIAS("ip_set_bitmap:ip,mac");
36 #define MTYPE bitmap_ipmac
37 #define IP_SET_BITMAP_STORED_TIMEOUT
40 MAC_UNSET, /* element is set, without MAC */
41 MAC_FILLED, /* element is set with MAC */
46 void *members; /* the set members */
47 void *extensions; /* MAC + data extensions */
48 u32 first_ip; /* host byte order, included in range */
49 u32 last_ip; /* host byte order, included in range */
50 u32 elements; /* number of max elements in the set */
51 u32 timeout; /* timeout value */
52 struct timer_list gc; /* garbage collector */
53 size_t memsize; /* members size */
54 size_t dsize; /* size of element */
55 size_t offset[IPSET_OFFSET_MAX]; /* Offsets to extensions */
58 /* ADT structure for generic function args */
59 struct bitmap_ipmac_adt_elem {
64 struct bitmap_ipmac_elem {
65 unsigned char ether[ETH_ALEN];
67 } __attribute__ ((aligned));
70 ip_to_id(const struct bitmap_ipmac *m, u32 ip)
72 return ip - m->first_ip;
75 static inline struct bitmap_ipmac_elem *
76 get_elem(void *extensions, u16 id, size_t dsize)
78 return (struct bitmap_ipmac_elem *)(extensions + id * dsize);
81 /* Common functions */
84 bitmap_ipmac_do_test(const struct bitmap_ipmac_adt_elem *e,
85 const struct bitmap_ipmac *map)
87 const struct bitmap_ipmac_elem *elem;
89 if (!test_bit(e->id, map->members))
91 elem = get_elem(map->extensions, e->id, map->dsize);
92 if (elem->filled == MAC_FILLED)
93 return e->ether == NULL ||
94 ether_addr_equal(e->ether, elem->ether);
95 /* Trigger kernel to fill out the ethernet address */
100 bitmap_ipmac_gc_test(u16 id, const struct bitmap_ipmac *map)
102 const struct bitmap_ipmac_elem *elem;
104 if (!test_bit(id, map->members))
106 elem = get_elem(map->extensions, id, map->dsize);
107 /* Timer not started for the incomplete elements */
108 return elem->filled == MAC_FILLED;
112 bitmap_ipmac_is_filled(const struct bitmap_ipmac_elem *elem)
114 return elem->filled == MAC_FILLED;
118 bitmap_ipmac_add_timeout(unsigned long *timeout,
119 const struct bitmap_ipmac_adt_elem *e,
120 const struct ip_set_ext *ext,
121 struct bitmap_ipmac *map, int mode)
123 u32 t = ext->timeout;
125 if (mode == IPSET_ADD_START_STORED_TIMEOUT) {
126 if (t == map->timeout)
127 /* Timeout was not specified, get stored one */
129 ip_set_timeout_set(timeout, t);
131 /* If MAC is unset yet, we store plain timeout value
132 * because the timer is not activated yet
133 * and we can reuse it later when MAC is filled out,
134 * possibly by the kernel */
136 ip_set_timeout_set(timeout, t);
144 bitmap_ipmac_do_add(const struct bitmap_ipmac_adt_elem *e,
145 struct bitmap_ipmac *map, u32 flags)
147 struct bitmap_ipmac_elem *elem;
149 elem = get_elem(map->extensions, e->id, map->dsize);
150 if (test_and_set_bit(e->id, map->members)) {
151 if (elem->filled == MAC_FILLED) {
152 if (e->ether && (flags & IPSET_FLAG_EXIST))
153 memcpy(elem->ether, e->ether, ETH_ALEN);
154 return IPSET_ADD_FAILED;
155 } else if (!e->ether)
156 /* Already added without ethernet address */
157 return IPSET_ADD_FAILED;
158 /* Fill the MAC address and trigger the timer activation */
159 memcpy(elem->ether, e->ether, ETH_ALEN);
160 elem->filled = MAC_FILLED;
161 return IPSET_ADD_START_STORED_TIMEOUT;
162 } else if (e->ether) {
163 /* We can store MAC too */
164 memcpy(elem->ether, e->ether, ETH_ALEN);
165 elem->filled = MAC_FILLED;
168 elem->filled = MAC_UNSET;
169 /* MAC is not stored yet, don't start timer */
170 return IPSET_ADD_STORE_PLAIN_TIMEOUT;
175 bitmap_ipmac_do_del(const struct bitmap_ipmac_adt_elem *e,
176 struct bitmap_ipmac *map)
178 return !test_and_clear_bit(e->id, map->members);
181 static inline unsigned long
182 ip_set_timeout_stored(struct bitmap_ipmac *map, u32 id, unsigned long *timeout)
184 const struct bitmap_ipmac_elem *elem =
185 get_elem(map->extensions, id, map->dsize);
187 return elem->filled == MAC_FILLED ? ip_set_timeout_get(timeout) :
192 bitmap_ipmac_do_list(struct sk_buff *skb, const struct bitmap_ipmac *map,
195 const struct bitmap_ipmac_elem *elem =
196 get_elem(map->extensions, id, map->dsize);
198 return nla_put_ipaddr4(skb, IPSET_ATTR_IP,
199 htonl(map->first_ip + id)) ||
200 (elem->filled == MAC_FILLED &&
201 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, elem->ether));
205 bitmap_ipmac_do_head(struct sk_buff *skb, const struct bitmap_ipmac *map)
207 return nla_put_ipaddr4(skb, IPSET_ATTR_IP, htonl(map->first_ip)) ||
208 nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip));
212 bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
213 const struct xt_action_param *par,
214 enum ipset_adt adt, struct ip_set_adt_opt *opt)
216 struct bitmap_ipmac *map = set->data;
217 ipset_adtfn adtfn = set->variant->adt[adt];
218 struct bitmap_ipmac_adt_elem e = {};
219 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, map);
222 /* MAC can be src only */
223 if (!(opt->flags & IPSET_DIM_TWO_SRC))
226 ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
227 if (ip < map->first_ip || ip > map->last_ip)
228 return -IPSET_ERR_BITMAP_RANGE;
230 /* Backward compatibility: we don't check the second flag */
231 if (skb_mac_header(skb) < skb->head ||
232 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
235 e.id = ip_to_id(map, ip);
236 e.ether = eth_hdr(skb)->h_source;
238 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
242 bitmap_ipmac_uadt(struct ip_set *set, struct nlattr *tb[],
243 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
245 const struct bitmap_ipmac *map = set->data;
246 ipset_adtfn adtfn = set->variant->adt[adt];
247 struct bitmap_ipmac_adt_elem e = {};
248 struct ip_set_ext ext = IP_SET_INIT_UEXT(map);
252 if (unlikely(!tb[IPSET_ATTR_IP] ||
253 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
254 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
255 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
256 return -IPSET_ERR_PROTOCOL;
258 if (tb[IPSET_ATTR_LINENO])
259 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
261 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
262 ip_set_get_extensions(set, tb, &ext);
266 if (ip < map->first_ip || ip > map->last_ip)
267 return -IPSET_ERR_BITMAP_RANGE;
269 e.id = ip_to_id(map, ip);
270 if (tb[IPSET_ATTR_ETHER])
271 e.ether = nla_data(tb[IPSET_ATTR_ETHER]);
275 ret = adtfn(set, &e, &ext, &ext, flags);
277 return ip_set_eexist(ret, flags) ? 0 : ret;
281 bitmap_ipmac_same_set(const struct ip_set *a, const struct ip_set *b)
283 const struct bitmap_ipmac *x = a->data;
284 const struct bitmap_ipmac *y = b->data;
286 return x->first_ip == y->first_ip &&
287 x->last_ip == y->last_ip &&
288 x->timeout == y->timeout &&
289 a->extensions == b->extensions;
294 /* Timeout variant */
296 struct bitmap_ipmact_elem {
298 unsigned char ether[ETH_ALEN];
299 unsigned char filled;
300 } __attribute__ ((aligned));
301 unsigned long timeout;
304 /* Plain variant with counter */
306 struct bitmap_ipmacc_elem {
308 unsigned char ether[ETH_ALEN];
309 unsigned char filled;
310 } __attribute__ ((aligned));
311 struct ip_set_counter counter;
314 /* Timeout variant with counter */
316 struct bitmap_ipmacct_elem {
318 unsigned char ether[ETH_ALEN];
319 unsigned char filled;
320 } __attribute__ ((aligned));
321 unsigned long timeout;
322 struct ip_set_counter counter;
325 #include "ip_set_bitmap_gen.h"
327 /* Create bitmap:ip,mac type of sets */
330 init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
331 u32 first_ip, u32 last_ip, u32 elements)
333 map->members = ip_set_alloc((last_ip - first_ip + 1) * map->dsize);
337 map->extensions = ip_set_alloc(map->dsize * elements);
338 if (!map->extensions) {
343 map->first_ip = first_ip;
344 map->last_ip = last_ip;
345 map->elements = elements;
346 map->timeout = IPSET_NO_TIMEOUT;
349 set->family = NFPROTO_IPV4;
355 bitmap_ipmac_create(struct ip_set *set, struct nlattr *tb[],
358 u32 first_ip, last_ip, cadt_flags = 0;
360 struct bitmap_ipmac *map;
363 if (unlikely(!tb[IPSET_ATTR_IP] ||
364 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
365 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
366 return -IPSET_ERR_PROTOCOL;
368 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
372 if (tb[IPSET_ATTR_IP_TO]) {
373 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
376 if (first_ip > last_ip) {
382 } else if (tb[IPSET_ATTR_CIDR]) {
383 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
386 return -IPSET_ERR_INVALID_CIDR;
387 ip_set_mask_from_to(first_ip, last_ip, cidr);
389 return -IPSET_ERR_PROTOCOL;
391 elements = (u64)last_ip - first_ip + 1;
393 if (elements > IPSET_BITMAP_MAX_RANGE + 1)
394 return -IPSET_ERR_BITMAP_RANGE_SIZE;
396 map = kzalloc(sizeof(*map), GFP_KERNEL);
400 map->memsize = bitmap_bytes(0, elements - 1);
401 set->variant = &bitmap_ipmac;
402 if (tb[IPSET_ATTR_CADT_FLAGS])
403 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
404 if (cadt_flags & IPSET_FLAG_WITH_COUNTERS) {
405 set->extensions |= IPSET_EXT_COUNTER;
406 if (tb[IPSET_ATTR_TIMEOUT]) {
407 map->dsize = sizeof(struct bitmap_ipmacct_elem);
408 map->offset[IPSET_OFFSET_TIMEOUT] =
409 offsetof(struct bitmap_ipmacct_elem, timeout);
410 map->offset[IPSET_OFFSET_COUNTER] =
411 offsetof(struct bitmap_ipmacct_elem, counter);
413 if (!init_map_ipmac(set, map, first_ip, last_ip,
418 map->timeout = ip_set_timeout_uget(
419 tb[IPSET_ATTR_TIMEOUT]);
420 set->extensions |= IPSET_EXT_TIMEOUT;
421 bitmap_ipmac_gc_init(set, bitmap_ipmac_gc);
423 map->dsize = sizeof(struct bitmap_ipmacc_elem);
424 map->offset[IPSET_OFFSET_COUNTER] =
425 offsetof(struct bitmap_ipmacc_elem, counter);
427 if (!init_map_ipmac(set, map, first_ip, last_ip,
433 } else if (tb[IPSET_ATTR_TIMEOUT]) {
434 map->dsize = sizeof(struct bitmap_ipmact_elem);
435 map->offset[IPSET_OFFSET_TIMEOUT] =
436 offsetof(struct bitmap_ipmact_elem, timeout);
438 if (!init_map_ipmac(set, map, first_ip, last_ip, elements)) {
442 map->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
443 set->extensions |= IPSET_EXT_TIMEOUT;
444 bitmap_ipmac_gc_init(set, bitmap_ipmac_gc);
446 map->dsize = sizeof(struct bitmap_ipmac_elem);
448 if (!init_map_ipmac(set, map, first_ip, last_ip, elements)) {
452 set->variant = &bitmap_ipmac;
457 static struct ip_set_type bitmap_ipmac_type = {
458 .name = "bitmap:ip,mac",
459 .protocol = IPSET_PROTOCOL,
460 .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
461 .dimension = IPSET_DIM_TWO,
462 .family = NFPROTO_IPV4,
463 .revision_min = REVISION_MIN,
464 .revision_max = REVISION_MAX,
465 .create = bitmap_ipmac_create,
467 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
468 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
469 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
470 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
471 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
474 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
475 [IPSET_ATTR_ETHER] = { .type = NLA_BINARY,
477 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
478 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
479 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
480 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
486 bitmap_ipmac_init(void)
488 return ip_set_type_register(&bitmap_ipmac_type);
492 bitmap_ipmac_fini(void)
494 ip_set_type_unregister(&bitmap_ipmac_type);
497 module_init(bitmap_ipmac_init);
498 module_exit(bitmap_ipmac_fini);