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 IPSET_TYPE_REV_MIN 0
29 /* 1 Counter support added */
30 /* 2 Comment support added */
31 #define IPSET_TYPE_REV_MAX 3 /* skbinfo support added */
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
35 IP_SET_MODULE_DESC("bitmap:ip,mac", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
36 MODULE_ALIAS("ip_set_bitmap:ip,mac");
38 #define MTYPE bitmap_ipmac
40 #define IP_SET_BITMAP_STORED_TIMEOUT
43 MAC_UNSET, /* element is set, without MAC */
44 MAC_FILLED, /* element is set with MAC */
49 void *members; /* the set members */
50 void *extensions; /* MAC + data extensions */
51 u32 first_ip; /* host byte order, included in range */
52 u32 last_ip; /* host byte order, included in range */
53 u32 elements; /* number of max elements in the set */
54 size_t memsize; /* members size */
55 struct timer_list gc; /* garbage collector */
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, size_t dsize)
87 const struct bitmap_ipmac_elem *elem;
89 if (!test_bit(e->id, map->members))
91 elem = get_elem(map->extensions, e->id, dsize);
92 if (elem->filled == MAC_FILLED)
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, size_t dsize)
102 const struct bitmap_ipmac_elem *elem;
104 if (!test_bit(id, map->members))
106 elem = get_elem(map->extensions, id, 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, struct ip_set *set,
121 struct bitmap_ipmac *map, int mode)
123 u32 t = ext->timeout;
125 if (mode == IPSET_ADD_START_STORED_TIMEOUT) {
126 if (t == set->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
137 ip_set_timeout_set(timeout, t);
145 bitmap_ipmac_do_add(const struct bitmap_ipmac_adt_elem *e,
146 struct bitmap_ipmac *map, u32 flags, size_t dsize)
148 struct bitmap_ipmac_elem *elem;
150 elem = get_elem(map->extensions, e->id, dsize);
151 if (test_bit(e->id, map->members)) {
152 if (elem->filled == MAC_FILLED) {
154 (flags & IPSET_FLAG_EXIST) &&
155 !ether_addr_equal(e->ether, elem->ether)) {
156 /* memcpy isn't atomic */
157 clear_bit(e->id, map->members);
158 smp_mb__after_atomic();
159 ether_addr_copy(elem->ether, e->ether);
161 return IPSET_ADD_FAILED;
162 } else if (!e->ether)
163 /* Already added without ethernet address */
164 return IPSET_ADD_FAILED;
165 /* Fill the MAC address and trigger the timer activation */
166 clear_bit(e->id, map->members);
167 smp_mb__after_atomic();
168 ether_addr_copy(elem->ether, e->ether);
169 elem->filled = MAC_FILLED;
170 return IPSET_ADD_START_STORED_TIMEOUT;
171 } else if (e->ether) {
172 /* We can store MAC too */
173 ether_addr_copy(elem->ether, e->ether);
174 elem->filled = MAC_FILLED;
177 elem->filled = MAC_UNSET;
178 /* MAC is not stored yet, don't start timer */
179 return IPSET_ADD_STORE_PLAIN_TIMEOUT;
183 bitmap_ipmac_do_del(const struct bitmap_ipmac_adt_elem *e,
184 struct bitmap_ipmac *map)
186 return !test_and_clear_bit(e->id, map->members);
190 bitmap_ipmac_do_list(struct sk_buff *skb, const struct bitmap_ipmac *map,
191 u32 id, size_t dsize)
193 const struct bitmap_ipmac_elem *elem =
194 get_elem(map->extensions, id, dsize);
196 return nla_put_ipaddr4(skb, IPSET_ATTR_IP,
197 htonl(map->first_ip + id)) ||
198 (elem->filled == MAC_FILLED &&
199 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, elem->ether));
203 bitmap_ipmac_do_head(struct sk_buff *skb, const struct bitmap_ipmac *map)
205 return nla_put_ipaddr4(skb, IPSET_ATTR_IP, htonl(map->first_ip)) ||
206 nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip));
210 bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
211 const struct xt_action_param *par,
212 enum ipset_adt adt, struct ip_set_adt_opt *opt)
214 struct bitmap_ipmac *map = set->data;
215 ipset_adtfn adtfn = set->variant->adt[adt];
216 struct bitmap_ipmac_adt_elem e = { .id = 0 };
217 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
220 /* MAC can be src only */
221 if (!(opt->flags & IPSET_DIM_TWO_SRC))
224 ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
225 if (ip < map->first_ip || ip > map->last_ip)
226 return -IPSET_ERR_BITMAP_RANGE;
228 /* Backward compatibility: we don't check the second flag */
229 if (skb_mac_header(skb) < skb->head ||
230 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
233 e.id = ip_to_id(map, ip);
234 e.ether = eth_hdr(skb)->h_source;
236 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
240 bitmap_ipmac_uadt(struct ip_set *set, struct nlattr *tb[],
241 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
243 const struct bitmap_ipmac *map = set->data;
244 ipset_adtfn adtfn = set->variant->adt[adt];
245 struct bitmap_ipmac_adt_elem e = { .id = 0 };
246 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
250 if (tb[IPSET_ATTR_LINENO])
251 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
253 if (unlikely(!tb[IPSET_ATTR_IP]))
254 return -IPSET_ERR_PROTOCOL;
256 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
260 ret = ip_set_get_extensions(set, tb, &ext);
264 if (ip < map->first_ip || ip > map->last_ip)
265 return -IPSET_ERR_BITMAP_RANGE;
267 e.id = ip_to_id(map, ip);
268 if (tb[IPSET_ATTR_ETHER])
269 e.ether = nla_data(tb[IPSET_ATTR_ETHER]);
273 ret = adtfn(set, &e, &ext, &ext, flags);
275 return ip_set_eexist(ret, flags) ? 0 : ret;
279 bitmap_ipmac_same_set(const struct ip_set *a, const struct ip_set *b)
281 const struct bitmap_ipmac *x = a->data;
282 const struct bitmap_ipmac *y = b->data;
284 return x->first_ip == y->first_ip &&
285 x->last_ip == y->last_ip &&
286 a->timeout == b->timeout &&
287 a->extensions == b->extensions;
292 #include "ip_set_bitmap_gen.h"
294 /* Create bitmap:ip,mac type of sets */
297 init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
298 u32 first_ip, u32 last_ip, u32 elements)
300 map->members = ip_set_alloc(map->memsize);
304 map->extensions = ip_set_alloc(set->dsize * elements);
305 if (!map->extensions) {
310 map->first_ip = first_ip;
311 map->last_ip = last_ip;
312 map->elements = elements;
313 set->timeout = IPSET_NO_TIMEOUT;
316 set->family = NFPROTO_IPV4;
322 bitmap_ipmac_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
325 u32 first_ip = 0, last_ip = 0;
327 struct bitmap_ipmac *map;
330 if (unlikely(!tb[IPSET_ATTR_IP] ||
331 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
332 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
333 return -IPSET_ERR_PROTOCOL;
335 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
339 if (tb[IPSET_ATTR_IP_TO]) {
340 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
343 if (first_ip > last_ip) {
349 } else if (tb[IPSET_ATTR_CIDR]) {
350 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
352 if (cidr >= HOST_MASK)
353 return -IPSET_ERR_INVALID_CIDR;
354 ip_set_mask_from_to(first_ip, last_ip, cidr);
356 return -IPSET_ERR_PROTOCOL;
359 elements = (u64)last_ip - first_ip + 1;
361 if (elements > IPSET_BITMAP_MAX_RANGE + 1)
362 return -IPSET_ERR_BITMAP_RANGE_SIZE;
364 map = kzalloc(sizeof(*map), GFP_KERNEL);
368 map->memsize = bitmap_bytes(0, elements - 1);
369 set->variant = &bitmap_ipmac;
370 set->dsize = ip_set_elem_len(set, tb,
371 sizeof(struct bitmap_ipmac_elem));
372 if (!init_map_ipmac(set, map, first_ip, last_ip, elements)) {
376 if (tb[IPSET_ATTR_TIMEOUT]) {
377 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
378 bitmap_ipmac_gc_init(set, bitmap_ipmac_gc);
383 static struct ip_set_type bitmap_ipmac_type = {
384 .name = "bitmap:ip,mac",
385 .protocol = IPSET_PROTOCOL,
386 .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
387 .dimension = IPSET_DIM_TWO,
388 .family = NFPROTO_IPV4,
389 .revision_min = IPSET_TYPE_REV_MIN,
390 .revision_max = IPSET_TYPE_REV_MAX,
391 .create = bitmap_ipmac_create,
393 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
394 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
395 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
396 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
397 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
400 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
401 [IPSET_ATTR_ETHER] = { .type = NLA_BINARY,
403 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
404 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
405 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
406 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
407 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
408 .len = IPSET_MAX_COMMENT_SIZE },
409 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
410 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
411 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
417 bitmap_ipmac_init(void)
419 return ip_set_type_register(&bitmap_ipmac_type);
423 bitmap_ipmac_fini(void)
426 ip_set_type_unregister(&bitmap_ipmac_type);
429 module_init(bitmap_ipmac_init);
430 module_exit(bitmap_ipmac_fini);