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 unsigned long *members; /* the set members */
50 u32 first_ip; /* host byte order, included in range */
51 u32 last_ip; /* host byte order, included in range */
52 u32 elements; /* number of max elements in the set */
53 size_t memsize; /* members size */
54 struct timer_list gc; /* garbage collector */
55 struct ip_set *set; /* attached to this ip_set */
56 unsigned char extensions[0] /* MAC + data extensions */
57 __aligned(__alignof__(u64));
60 /* ADT structure for generic function args */
61 struct bitmap_ipmac_adt_elem {
62 unsigned char ether[ETH_ALEN] __aligned(2);
67 struct bitmap_ipmac_elem {
68 unsigned char ether[ETH_ALEN];
70 } __aligned(__alignof__(u64));
73 ip_to_id(const struct bitmap_ipmac *m, u32 ip)
75 return ip - m->first_ip;
78 #define get_elem(extensions, id, dsize) \
79 (struct bitmap_ipmac_elem *)(extensions + (id) * (dsize))
81 #define get_const_elem(extensions, id, dsize) \
82 (const struct bitmap_ipmac_elem *)(extensions + (id) * (dsize))
84 /* Common functions */
87 bitmap_ipmac_do_test(const struct bitmap_ipmac_adt_elem *e,
88 const struct bitmap_ipmac *map, size_t dsize)
90 const struct bitmap_ipmac_elem *elem;
92 if (!test_bit(e->id, map->members))
94 elem = get_const_elem(map->extensions, e->id, dsize);
95 if (e->add_mac && elem->filled == MAC_FILLED)
96 return ether_addr_equal(e->ether, elem->ether);
97 /* Trigger kernel to fill out the ethernet address */
102 bitmap_ipmac_gc_test(u16 id, const struct bitmap_ipmac *map, size_t dsize)
104 const struct bitmap_ipmac_elem *elem;
106 if (!test_bit(id, map->members))
108 elem = get_const_elem(map->extensions, id, dsize);
109 /* Timer not started for the incomplete elements */
110 return elem->filled == MAC_FILLED;
114 bitmap_ipmac_is_filled(const struct bitmap_ipmac_elem *elem)
116 return elem->filled == MAC_FILLED;
120 bitmap_ipmac_add_timeout(unsigned long *timeout,
121 const struct bitmap_ipmac_adt_elem *e,
122 const struct ip_set_ext *ext, struct ip_set *set,
123 struct bitmap_ipmac *map, int mode)
125 u32 t = ext->timeout;
127 if (mode == IPSET_ADD_START_STORED_TIMEOUT) {
128 if (t == set->timeout)
129 /* Timeout was not specified, get stored one */
131 ip_set_timeout_set(timeout, t);
133 /* If MAC is unset yet, we store plain timeout value
134 * because the timer is not activated yet
135 * and we can reuse it later when MAC is filled out,
136 * possibly by the kernel
139 ip_set_timeout_set(timeout, t);
147 bitmap_ipmac_do_add(const struct bitmap_ipmac_adt_elem *e,
148 struct bitmap_ipmac *map, u32 flags, size_t dsize)
150 struct bitmap_ipmac_elem *elem;
152 elem = get_elem(map->extensions, e->id, dsize);
153 if (test_bit(e->id, map->members)) {
154 if (elem->filled == MAC_FILLED) {
156 (flags & IPSET_FLAG_EXIST) &&
157 !ether_addr_equal(e->ether, elem->ether)) {
158 /* memcpy isn't atomic */
159 clear_bit(e->id, map->members);
160 smp_mb__after_atomic();
161 ether_addr_copy(elem->ether, e->ether);
163 return IPSET_ADD_FAILED;
164 } else if (!e->add_mac)
165 /* Already added without ethernet address */
166 return IPSET_ADD_FAILED;
167 /* Fill the MAC address and trigger the timer activation */
168 clear_bit(e->id, map->members);
169 smp_mb__after_atomic();
170 ether_addr_copy(elem->ether, e->ether);
171 elem->filled = MAC_FILLED;
172 return IPSET_ADD_START_STORED_TIMEOUT;
173 } else if (e->add_mac) {
174 /* We can store MAC too */
175 ether_addr_copy(elem->ether, e->ether);
176 elem->filled = MAC_FILLED;
179 elem->filled = MAC_UNSET;
180 /* MAC is not stored yet, don't start timer */
181 return IPSET_ADD_STORE_PLAIN_TIMEOUT;
185 bitmap_ipmac_do_del(const struct bitmap_ipmac_adt_elem *e,
186 struct bitmap_ipmac *map)
188 return !test_and_clear_bit(e->id, map->members);
192 bitmap_ipmac_do_list(struct sk_buff *skb, const struct bitmap_ipmac *map,
193 u32 id, size_t dsize)
195 const struct bitmap_ipmac_elem *elem =
196 get_const_elem(map->extensions, id, 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 = { .id = 0, .add_mac = 1 };
219 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
222 ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
223 if (ip < map->first_ip || ip > map->last_ip)
224 return -IPSET_ERR_BITMAP_RANGE;
226 /* Backward compatibility: we don't check the second flag */
227 if (skb_mac_header(skb) < skb->head ||
228 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
231 e.id = ip_to_id(map, ip);
233 if (opt->flags & IPSET_DIM_TWO_SRC)
234 ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
236 ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
238 if (is_zero_ether_addr(e.ether))
241 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
245 bitmap_ipmac_uadt(struct ip_set *set, struct nlattr *tb[],
246 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
248 const struct bitmap_ipmac *map = set->data;
249 ipset_adtfn adtfn = set->variant->adt[adt];
250 struct bitmap_ipmac_adt_elem e = { .id = 0 };
251 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
255 if (tb[IPSET_ATTR_LINENO])
256 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
258 if (unlikely(!tb[IPSET_ATTR_IP]))
259 return -IPSET_ERR_PROTOCOL;
261 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
265 ret = ip_set_get_extensions(set, tb, &ext);
269 if (ip < map->first_ip || ip > map->last_ip)
270 return -IPSET_ERR_BITMAP_RANGE;
272 e.id = ip_to_id(map, ip);
273 if (tb[IPSET_ATTR_ETHER]) {
274 if (nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN)
275 return -IPSET_ERR_PROTOCOL;
276 memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
279 ret = adtfn(set, &e, &ext, &ext, flags);
281 return ip_set_eexist(ret, flags) ? 0 : ret;
285 bitmap_ipmac_same_set(const struct ip_set *a, const struct ip_set *b)
287 const struct bitmap_ipmac *x = a->data;
288 const struct bitmap_ipmac *y = b->data;
290 return x->first_ip == y->first_ip &&
291 x->last_ip == y->last_ip &&
292 a->timeout == b->timeout &&
293 a->extensions == b->extensions;
298 #include "ip_set_bitmap_gen.h"
300 /* Create bitmap:ip,mac type of sets */
303 init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
304 u32 first_ip, u32 last_ip, u32 elements)
306 map->members = bitmap_zalloc(elements, GFP_KERNEL | __GFP_NOWARN);
309 map->first_ip = first_ip;
310 map->last_ip = last_ip;
311 map->elements = elements;
312 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)
344 swap(first_ip, last_ip);
345 } else if (tb[IPSET_ATTR_CIDR]) {
346 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
348 if (cidr >= HOST_MASK)
349 return -IPSET_ERR_INVALID_CIDR;
350 ip_set_mask_from_to(first_ip, last_ip, cidr);
352 return -IPSET_ERR_PROTOCOL;
355 elements = (u64)last_ip - first_ip + 1;
357 if (elements > IPSET_BITMAP_MAX_RANGE + 1)
358 return -IPSET_ERR_BITMAP_RANGE_SIZE;
360 set->dsize = ip_set_elem_len(set, tb,
361 sizeof(struct bitmap_ipmac_elem),
362 __alignof__(struct bitmap_ipmac_elem));
363 map = ip_set_alloc(sizeof(*map) + elements * set->dsize);
367 map->memsize = BITS_TO_LONGS(elements) * sizeof(unsigned long);
368 set->variant = &bitmap_ipmac;
369 if (!init_map_ipmac(set, map, first_ip, last_ip, elements)) {
373 if (tb[IPSET_ATTR_TIMEOUT]) {
374 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
375 bitmap_ipmac_gc_init(set, bitmap_ipmac_gc);
380 static struct ip_set_type bitmap_ipmac_type = {
381 .name = "bitmap:ip,mac",
382 .protocol = IPSET_PROTOCOL,
383 .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
384 .dimension = IPSET_DIM_TWO,
385 .family = NFPROTO_IPV4,
386 .revision_min = IPSET_TYPE_REV_MIN,
387 .revision_max = IPSET_TYPE_REV_MAX,
388 .create = bitmap_ipmac_create,
390 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
391 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
392 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
393 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
394 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
397 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
398 [IPSET_ATTR_ETHER] = { .type = NLA_BINARY,
400 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
401 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
402 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
403 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
404 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
405 .len = IPSET_MAX_COMMENT_SIZE },
406 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
407 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
408 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
414 bitmap_ipmac_init(void)
416 return ip_set_type_register(&bitmap_ipmac_type);
420 bitmap_ipmac_fini(void)
423 ip_set_type_unregister(&bitmap_ipmac_type);
426 module_init(bitmap_ipmac_init);
427 module_exit(bitmap_ipmac_fini);