1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3 * Patrick Schaaf <bof@bof.de>
4 * Martin Josefsson <gandalf@wlug.westbo.se>
5 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
8 /* Kernel module implementing an IP set type: the bitmap:ip,mac type */
10 #include <linux/module.h>
12 #include <linux/etherdevice.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
15 #include <linux/if_ether.h>
16 #include <linux/netlink.h>
17 #include <linux/jiffies.h>
18 #include <linux/timer.h>
19 #include <net/netlink.h>
21 #include <linux/netfilter/ipset/pfxlen.h>
22 #include <linux/netfilter/ipset/ip_set.h>
23 #include <linux/netfilter/ipset/ip_set_bitmap.h>
25 #define IPSET_TYPE_REV_MIN 0
26 /* 1 Counter support added */
27 /* 2 Comment support added */
28 #define IPSET_TYPE_REV_MAX 3 /* skbinfo support added */
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
32 IP_SET_MODULE_DESC("bitmap:ip,mac", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
33 MODULE_ALIAS("ip_set_bitmap:ip,mac");
35 #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 u32 first_ip; /* host byte order, included in range */
48 u32 last_ip; /* host byte order, included in range */
49 u32 elements; /* number of max elements in the set */
50 size_t memsize; /* members size */
51 struct timer_list gc; /* garbage collector */
52 struct ip_set *set; /* attached to this ip_set */
53 unsigned char extensions[0] /* MAC + data extensions */
54 __aligned(__alignof__(u64));
57 /* ADT structure for generic function args */
58 struct bitmap_ipmac_adt_elem {
59 unsigned char ether[ETH_ALEN] __aligned(2);
64 struct bitmap_ipmac_elem {
65 unsigned char ether[ETH_ALEN];
67 } __aligned(__alignof__(u64));
70 ip_to_id(const struct bitmap_ipmac *m, u32 ip)
72 return ip - m->first_ip;
75 #define get_elem(extensions, id, dsize) \
76 (struct bitmap_ipmac_elem *)(extensions + (id) * (dsize))
78 #define get_const_elem(extensions, id, dsize) \
79 (const 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_const_elem(map->extensions, e->id, dsize);
92 if (e->add_mac && elem->filled == MAC_FILLED)
93 return ether_addr_equal(e->ether, elem->ether);
94 /* Trigger kernel to fill out the ethernet address */
99 bitmap_ipmac_gc_test(u16 id, const struct bitmap_ipmac *map, size_t dsize)
101 const struct bitmap_ipmac_elem *elem;
103 if (!test_bit(id, map->members))
105 elem = get_const_elem(map->extensions, id, dsize);
106 /* Timer not started for the incomplete elements */
107 return elem->filled == MAC_FILLED;
111 bitmap_ipmac_is_filled(const struct bitmap_ipmac_elem *elem)
113 return elem->filled == MAC_FILLED;
117 bitmap_ipmac_add_timeout(unsigned long *timeout,
118 const struct bitmap_ipmac_adt_elem *e,
119 const struct ip_set_ext *ext, struct ip_set *set,
120 struct bitmap_ipmac *map, int mode)
122 u32 t = ext->timeout;
124 if (mode == IPSET_ADD_START_STORED_TIMEOUT) {
125 if (t == set->timeout)
126 /* Timeout was not specified, get stored one */
128 ip_set_timeout_set(timeout, t);
130 /* If MAC is unset yet, we store plain timeout value
131 * because the timer is not activated yet
132 * and we can reuse it later when MAC is filled out,
133 * 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, size_t dsize)
147 struct bitmap_ipmac_elem *elem;
149 elem = get_elem(map->extensions, e->id, dsize);
150 if (test_bit(e->id, map->members)) {
151 if (elem->filled == MAC_FILLED) {
153 (flags & IPSET_FLAG_EXIST) &&
154 !ether_addr_equal(e->ether, elem->ether)) {
155 /* memcpy isn't atomic */
156 clear_bit(e->id, map->members);
157 smp_mb__after_atomic();
158 ether_addr_copy(elem->ether, e->ether);
160 return IPSET_ADD_FAILED;
161 } else if (!e->add_mac)
162 /* Already added without ethernet address */
163 return IPSET_ADD_FAILED;
164 /* Fill the MAC address and trigger the timer activation */
165 clear_bit(e->id, map->members);
166 smp_mb__after_atomic();
167 ether_addr_copy(elem->ether, e->ether);
168 elem->filled = MAC_FILLED;
169 return IPSET_ADD_START_STORED_TIMEOUT;
170 } else if (e->add_mac) {
171 /* We can store MAC too */
172 ether_addr_copy(elem->ether, e->ether);
173 elem->filled = MAC_FILLED;
176 elem->filled = MAC_UNSET;
177 /* MAC is not stored yet, don't start timer */
178 return IPSET_ADD_STORE_PLAIN_TIMEOUT;
182 bitmap_ipmac_do_del(const struct bitmap_ipmac_adt_elem *e,
183 struct bitmap_ipmac *map)
185 return !test_and_clear_bit(e->id, map->members);
189 bitmap_ipmac_do_list(struct sk_buff *skb, const struct bitmap_ipmac *map,
190 u32 id, size_t dsize)
192 const struct bitmap_ipmac_elem *elem =
193 get_const_elem(map->extensions, id, dsize);
195 return nla_put_ipaddr4(skb, IPSET_ATTR_IP,
196 htonl(map->first_ip + id)) ||
197 (elem->filled == MAC_FILLED &&
198 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, elem->ether));
202 bitmap_ipmac_do_head(struct sk_buff *skb, const struct bitmap_ipmac *map)
204 return nla_put_ipaddr4(skb, IPSET_ATTR_IP, htonl(map->first_ip)) ||
205 nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip));
209 bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
210 const struct xt_action_param *par,
211 enum ipset_adt adt, struct ip_set_adt_opt *opt)
213 struct bitmap_ipmac *map = set->data;
214 ipset_adtfn adtfn = set->variant->adt[adt];
215 struct bitmap_ipmac_adt_elem e = { .id = 0, .add_mac = 1 };
216 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
219 ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
220 if (ip < map->first_ip || ip > map->last_ip)
221 return -IPSET_ERR_BITMAP_RANGE;
223 /* Backward compatibility: we don't check the second flag */
224 if (skb_mac_header(skb) < skb->head ||
225 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
228 e.id = ip_to_id(map, ip);
230 if (opt->flags & IPSET_DIM_ONE_SRC)
231 ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
233 ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
235 if (is_zero_ether_addr(e.ether))
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 = { .id = 0 };
248 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
252 if (tb[IPSET_ATTR_LINENO])
253 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
255 if (unlikely(!tb[IPSET_ATTR_IP]))
256 return -IPSET_ERR_PROTOCOL;
258 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
262 ret = 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 if (nla_len(tb[IPSET_ATTR_ETHER]) != ETH_ALEN)
272 return -IPSET_ERR_PROTOCOL;
273 memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
276 ret = adtfn(set, &e, &ext, &ext, flags);
278 return ip_set_eexist(ret, flags) ? 0 : ret;
282 bitmap_ipmac_same_set(const struct ip_set *a, const struct ip_set *b)
284 const struct bitmap_ipmac *x = a->data;
285 const struct bitmap_ipmac *y = b->data;
287 return x->first_ip == y->first_ip &&
288 x->last_ip == y->last_ip &&
289 a->timeout == b->timeout &&
290 a->extensions == b->extensions;
295 #include "ip_set_bitmap_gen.h"
297 /* Create bitmap:ip,mac type of sets */
300 init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
301 u32 first_ip, u32 last_ip, u32 elements)
303 map->members = ip_set_alloc(map->memsize);
306 map->first_ip = first_ip;
307 map->last_ip = last_ip;
308 map->elements = elements;
309 set->timeout = IPSET_NO_TIMEOUT;
313 set->family = NFPROTO_IPV4;
319 bitmap_ipmac_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
322 u32 first_ip = 0, last_ip = 0;
324 struct bitmap_ipmac *map;
327 if (unlikely(!tb[IPSET_ATTR_IP] ||
328 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
329 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
330 return -IPSET_ERR_PROTOCOL;
332 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
336 if (tb[IPSET_ATTR_IP_TO]) {
337 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
340 if (first_ip > last_ip)
341 swap(first_ip, last_ip);
342 } else if (tb[IPSET_ATTR_CIDR]) {
343 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
345 if (cidr >= HOST_MASK)
346 return -IPSET_ERR_INVALID_CIDR;
347 ip_set_mask_from_to(first_ip, last_ip, cidr);
349 return -IPSET_ERR_PROTOCOL;
352 elements = (u64)last_ip - first_ip + 1;
354 if (elements > IPSET_BITMAP_MAX_RANGE + 1)
355 return -IPSET_ERR_BITMAP_RANGE_SIZE;
357 set->dsize = ip_set_elem_len(set, tb,
358 sizeof(struct bitmap_ipmac_elem),
359 __alignof__(struct bitmap_ipmac_elem));
360 map = ip_set_alloc(sizeof(*map) + elements * set->dsize);
364 map->memsize = bitmap_bytes(0, elements - 1);
365 set->variant = &bitmap_ipmac;
366 if (!init_map_ipmac(set, map, first_ip, last_ip, elements)) {
370 if (tb[IPSET_ATTR_TIMEOUT]) {
371 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
372 bitmap_ipmac_gc_init(set, bitmap_ipmac_gc);
377 static struct ip_set_type bitmap_ipmac_type = {
378 .name = "bitmap:ip,mac",
379 .protocol = IPSET_PROTOCOL,
380 .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
381 .dimension = IPSET_DIM_TWO,
382 .family = NFPROTO_IPV4,
383 .revision_min = IPSET_TYPE_REV_MIN,
384 .revision_max = IPSET_TYPE_REV_MAX,
385 .create = bitmap_ipmac_create,
387 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
388 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
389 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
390 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
391 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
394 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
395 [IPSET_ATTR_ETHER] = { .type = NLA_BINARY,
397 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
398 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
399 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
400 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
401 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
402 .len = IPSET_MAX_COMMENT_SIZE },
403 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
404 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
405 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
411 bitmap_ipmac_init(void)
413 return ip_set_type_register(&bitmap_ipmac_type);
417 bitmap_ipmac_fini(void)
420 ip_set_type_unregister(&bitmap_ipmac_type);
423 module_init(bitmap_ipmac_init);
424 module_exit(bitmap_ipmac_fini);