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
39 #define IP_SET_BITMAP_STORED_TIMEOUT
42 MAC_UNSET, /* element is set, without MAC */
43 MAC_FILLED, /* element is set with MAC */
48 void *members; /* the set members */
49 void *extensions; /* MAC + data extensions */
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 */
57 /* ADT structure for generic function args */
58 struct bitmap_ipmac_adt_elem {
63 struct bitmap_ipmac_elem {
64 unsigned char ether[ETH_ALEN];
66 } __attribute__ ((aligned));
69 ip_to_id(const struct bitmap_ipmac *m, u32 ip)
71 return ip - m->first_ip;
74 static inline struct bitmap_ipmac_elem *
75 get_elem(void *extensions, u16 id, size_t dsize)
77 return (struct bitmap_ipmac_elem *)(extensions + id * dsize);
80 /* Common functions */
83 bitmap_ipmac_do_test(const struct bitmap_ipmac_adt_elem *e,
84 const struct bitmap_ipmac *map, size_t dsize)
86 const struct bitmap_ipmac_elem *elem;
88 if (!test_bit(e->id, map->members))
90 elem = get_elem(map->extensions, e->id, dsize);
91 if (elem->filled == MAC_FILLED)
92 return e->ether == NULL ||
93 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_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 */
135 ip_set_timeout_set(timeout, t);
143 bitmap_ipmac_do_add(const struct bitmap_ipmac_adt_elem *e,
144 struct bitmap_ipmac *map, u32 flags, size_t dsize)
146 struct bitmap_ipmac_elem *elem;
148 elem = get_elem(map->extensions, e->id, dsize);
149 if (test_and_set_bit(e->id, map->members)) {
150 if (elem->filled == MAC_FILLED) {
151 if (e->ether && (flags & IPSET_FLAG_EXIST))
152 memcpy(elem->ether, e->ether, ETH_ALEN);
153 return IPSET_ADD_FAILED;
154 } else if (!e->ether)
155 /* Already added without ethernet address */
156 return IPSET_ADD_FAILED;
157 /* Fill the MAC address and trigger the timer activation */
158 memcpy(elem->ether, e->ether, ETH_ALEN);
159 elem->filled = MAC_FILLED;
160 return IPSET_ADD_START_STORED_TIMEOUT;
161 } else if (e->ether) {
162 /* We can store MAC too */
163 memcpy(elem->ether, e->ether, ETH_ALEN);
164 elem->filled = MAC_FILLED;
167 elem->filled = MAC_UNSET;
168 /* MAC is not stored yet, don't start timer */
169 return IPSET_ADD_STORE_PLAIN_TIMEOUT;
174 bitmap_ipmac_do_del(const struct bitmap_ipmac_adt_elem *e,
175 struct bitmap_ipmac *map)
177 return !test_and_clear_bit(e->id, map->members);
181 bitmap_ipmac_do_list(struct sk_buff *skb, const struct bitmap_ipmac *map,
182 u32 id, size_t dsize)
184 const struct bitmap_ipmac_elem *elem =
185 get_elem(map->extensions, id, dsize);
187 return nla_put_ipaddr4(skb, IPSET_ATTR_IP,
188 htonl(map->first_ip + id)) ||
189 (elem->filled == MAC_FILLED &&
190 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, elem->ether));
194 bitmap_ipmac_do_head(struct sk_buff *skb, const struct bitmap_ipmac *map)
196 return nla_put_ipaddr4(skb, IPSET_ATTR_IP, htonl(map->first_ip)) ||
197 nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip));
201 bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
202 const struct xt_action_param *par,
203 enum ipset_adt adt, struct ip_set_adt_opt *opt)
205 struct bitmap_ipmac *map = set->data;
206 ipset_adtfn adtfn = set->variant->adt[adt];
207 struct bitmap_ipmac_adt_elem e = { .id = 0 };
208 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
211 /* MAC can be src only */
212 if (!(opt->flags & IPSET_DIM_TWO_SRC))
215 ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
216 if (ip < map->first_ip || ip > map->last_ip)
217 return -IPSET_ERR_BITMAP_RANGE;
219 /* Backward compatibility: we don't check the second flag */
220 if (skb_mac_header(skb) < skb->head ||
221 (skb_mac_header(skb) + ETH_HLEN) > skb->data)
224 e.id = ip_to_id(map, ip);
225 e.ether = eth_hdr(skb)->h_source;
227 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
231 bitmap_ipmac_uadt(struct ip_set *set, struct nlattr *tb[],
232 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
234 const struct bitmap_ipmac *map = set->data;
235 ipset_adtfn adtfn = set->variant->adt[adt];
236 struct bitmap_ipmac_adt_elem e = { .id = 0 };
237 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
241 if (unlikely(!tb[IPSET_ATTR_IP] ||
242 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
243 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
244 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
245 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
246 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
247 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
248 return -IPSET_ERR_PROTOCOL;
250 if (tb[IPSET_ATTR_LINENO])
251 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
253 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
254 ip_set_get_extensions(set, tb, &ext);
258 if (ip < map->first_ip || ip > map->last_ip)
259 return -IPSET_ERR_BITMAP_RANGE;
261 e.id = ip_to_id(map, ip);
262 if (tb[IPSET_ATTR_ETHER])
263 e.ether = nla_data(tb[IPSET_ATTR_ETHER]);
267 ret = adtfn(set, &e, &ext, &ext, flags);
269 return ip_set_eexist(ret, flags) ? 0 : ret;
273 bitmap_ipmac_same_set(const struct ip_set *a, const struct ip_set *b)
275 const struct bitmap_ipmac *x = a->data;
276 const struct bitmap_ipmac *y = b->data;
278 return x->first_ip == y->first_ip &&
279 x->last_ip == y->last_ip &&
280 a->timeout == b->timeout &&
281 a->extensions == b->extensions;
286 #include "ip_set_bitmap_gen.h"
288 /* Create bitmap:ip,mac type of sets */
291 init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
292 u32 first_ip, u32 last_ip, u32 elements)
294 map->members = ip_set_alloc(map->memsize);
298 map->extensions = ip_set_alloc(set->dsize * elements);
299 if (!map->extensions) {
304 map->first_ip = first_ip;
305 map->last_ip = last_ip;
306 map->elements = elements;
307 set->timeout = IPSET_NO_TIMEOUT;
310 set->family = NFPROTO_IPV4;
316 bitmap_ipmac_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
319 u32 first_ip = 0, last_ip = 0;
321 struct bitmap_ipmac *map;
324 if (unlikely(!tb[IPSET_ATTR_IP] ||
325 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
326 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
327 return -IPSET_ERR_PROTOCOL;
329 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
333 if (tb[IPSET_ATTR_IP_TO]) {
334 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
337 if (first_ip > last_ip) {
343 } else if (tb[IPSET_ATTR_CIDR]) {
344 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
347 return -IPSET_ERR_INVALID_CIDR;
348 ip_set_mask_from_to(first_ip, last_ip, cidr);
350 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 map = kzalloc(sizeof(*map), GFP_KERNEL);
361 map->memsize = bitmap_bytes(0, elements - 1);
362 set->variant = &bitmap_ipmac;
363 set->dsize = ip_set_elem_len(set, tb,
364 sizeof(struct bitmap_ipmac_elem));
365 if (!init_map_ipmac(set, map, first_ip, last_ip, elements)) {
369 if (tb[IPSET_ATTR_TIMEOUT]) {
370 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
371 bitmap_ipmac_gc_init(set, bitmap_ipmac_gc);
376 static struct ip_set_type bitmap_ipmac_type = {
377 .name = "bitmap:ip,mac",
378 .protocol = IPSET_PROTOCOL,
379 .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
380 .dimension = IPSET_DIM_TWO,
381 .family = NFPROTO_IPV4,
382 .revision_min = IPSET_TYPE_REV_MIN,
383 .revision_max = IPSET_TYPE_REV_MAX,
384 .create = bitmap_ipmac_create,
386 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
387 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
388 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
389 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
390 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
393 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
394 [IPSET_ATTR_ETHER] = { .type = NLA_BINARY,
396 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
397 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
398 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
399 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
400 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
401 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
402 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
403 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
409 bitmap_ipmac_init(void)
411 return ip_set_type_register(&bitmap_ipmac_type);
415 bitmap_ipmac_fini(void)
417 ip_set_type_unregister(&bitmap_ipmac_type);
420 module_init(bitmap_ipmac_init);
421 module_exit(bitmap_ipmac_fini);