These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / net / netfilter / ipset / ip_set_bitmap_ipmac.c
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>
5  *
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.
9  */
10
11 /* Kernel module implementing an IP set type: the bitmap:ip,mac type */
12
13 #include <linux/module.h>
14 #include <linux/ip.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>
23
24 #include <linux/netfilter/ipset/pfxlen.h>
25 #include <linux/netfilter/ipset/ip_set.h>
26 #include <linux/netfilter/ipset/ip_set_bitmap.h>
27
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 */
32
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");
37
38 #define MTYPE           bitmap_ipmac
39 #define HOST_MASK       32
40 #define IP_SET_BITMAP_STORED_TIMEOUT
41
42 enum {
43         MAC_UNSET,              /* element is set, without MAC */
44         MAC_FILLED,             /* element is set with MAC */
45 };
46
47 /* Type structure */
48 struct bitmap_ipmac {
49         void *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         unsigned char extensions[0]     /* MAC + data extensions */
56                 __aligned(__alignof__(u64));
57 };
58
59 /* ADT structure for generic function args */
60 struct bitmap_ipmac_adt_elem {
61         unsigned char ether[ETH_ALEN] __aligned(2);
62         u16 id;
63         u16 add_mac;
64 };
65
66 struct bitmap_ipmac_elem {
67         unsigned char ether[ETH_ALEN];
68         unsigned char filled;
69 } __aligned(__alignof__(u64));
70
71 static inline u32
72 ip_to_id(const struct bitmap_ipmac *m, u32 ip)
73 {
74         return ip - m->first_ip;
75 }
76
77 #define get_elem(extensions, id, dsize)         \
78         (struct bitmap_ipmac_elem *)(extensions + (id) * (dsize))
79
80 #define get_const_elem(extensions, id, dsize)   \
81         (const struct bitmap_ipmac_elem *)(extensions + (id) * (dsize))
82
83 /* Common functions */
84
85 static inline int
86 bitmap_ipmac_do_test(const struct bitmap_ipmac_adt_elem *e,
87                      const struct bitmap_ipmac *map, size_t dsize)
88 {
89         const struct bitmap_ipmac_elem *elem;
90
91         if (!test_bit(e->id, map->members))
92                 return 0;
93         elem = get_const_elem(map->extensions, e->id, dsize);
94         if (e->add_mac && elem->filled == MAC_FILLED)
95                 return ether_addr_equal(e->ether, elem->ether);
96         /* Trigger kernel to fill out the ethernet address */
97         return -EAGAIN;
98 }
99
100 static inline int
101 bitmap_ipmac_gc_test(u16 id, const struct bitmap_ipmac *map, size_t dsize)
102 {
103         const struct bitmap_ipmac_elem *elem;
104
105         if (!test_bit(id, map->members))
106                 return 0;
107         elem = get_const_elem(map->extensions, id, dsize);
108         /* Timer not started for the incomplete elements */
109         return elem->filled == MAC_FILLED;
110 }
111
112 static inline int
113 bitmap_ipmac_is_filled(const struct bitmap_ipmac_elem *elem)
114 {
115         return elem->filled == MAC_FILLED;
116 }
117
118 static inline int
119 bitmap_ipmac_add_timeout(unsigned long *timeout,
120                          const struct bitmap_ipmac_adt_elem *e,
121                          const struct ip_set_ext *ext, struct ip_set *set,
122                          struct bitmap_ipmac *map, int mode)
123 {
124         u32 t = ext->timeout;
125
126         if (mode == IPSET_ADD_START_STORED_TIMEOUT) {
127                 if (t == set->timeout)
128                         /* Timeout was not specified, get stored one */
129                         t = *timeout;
130                 ip_set_timeout_set(timeout, t);
131         } else {
132                 /* If MAC is unset yet, we store plain timeout value
133                  * because the timer is not activated yet
134                  * and we can reuse it later when MAC is filled out,
135                  * possibly by the kernel
136                  */
137                 if (e->add_mac)
138                         ip_set_timeout_set(timeout, t);
139                 else
140                         *timeout = t;
141         }
142         return 0;
143 }
144
145 static inline int
146 bitmap_ipmac_do_add(const struct bitmap_ipmac_adt_elem *e,
147                     struct bitmap_ipmac *map, u32 flags, size_t dsize)
148 {
149         struct bitmap_ipmac_elem *elem;
150
151         elem = get_elem(map->extensions, e->id, dsize);
152         if (test_bit(e->id, map->members)) {
153                 if (elem->filled == MAC_FILLED) {
154                         if (e->add_mac &&
155                             (flags & IPSET_FLAG_EXIST) &&
156                             !ether_addr_equal(e->ether, elem->ether)) {
157                                 /* memcpy isn't atomic */
158                                 clear_bit(e->id, map->members);
159                                 smp_mb__after_atomic();
160                                 ether_addr_copy(elem->ether, e->ether);
161                         }
162                         return IPSET_ADD_FAILED;
163                 } else if (!e->add_mac)
164                         /* Already added without ethernet address */
165                         return IPSET_ADD_FAILED;
166                 /* Fill the MAC address and trigger the timer activation */
167                 clear_bit(e->id, map->members);
168                 smp_mb__after_atomic();
169                 ether_addr_copy(elem->ether, e->ether);
170                 elem->filled = MAC_FILLED;
171                 return IPSET_ADD_START_STORED_TIMEOUT;
172         } else if (e->add_mac) {
173                 /* We can store MAC too */
174                 ether_addr_copy(elem->ether, e->ether);
175                 elem->filled = MAC_FILLED;
176                 return 0;
177         }
178         elem->filled = MAC_UNSET;
179         /* MAC is not stored yet, don't start timer */
180         return IPSET_ADD_STORE_PLAIN_TIMEOUT;
181 }
182
183 static inline int
184 bitmap_ipmac_do_del(const struct bitmap_ipmac_adt_elem *e,
185                     struct bitmap_ipmac *map)
186 {
187         return !test_and_clear_bit(e->id, map->members);
188 }
189
190 static inline int
191 bitmap_ipmac_do_list(struct sk_buff *skb, const struct bitmap_ipmac *map,
192                      u32 id, size_t dsize)
193 {
194         const struct bitmap_ipmac_elem *elem =
195                 get_const_elem(map->extensions, id, dsize);
196
197         return nla_put_ipaddr4(skb, IPSET_ATTR_IP,
198                                htonl(map->first_ip + id)) ||
199                (elem->filled == MAC_FILLED &&
200                 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, elem->ether));
201 }
202
203 static inline int
204 bitmap_ipmac_do_head(struct sk_buff *skb, const struct bitmap_ipmac *map)
205 {
206         return nla_put_ipaddr4(skb, IPSET_ATTR_IP, htonl(map->first_ip)) ||
207                nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip));
208 }
209
210 static int
211 bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
212                   const struct xt_action_param *par,
213                   enum ipset_adt adt, struct ip_set_adt_opt *opt)
214 {
215         struct bitmap_ipmac *map = set->data;
216         ipset_adtfn adtfn = set->variant->adt[adt];
217         struct bitmap_ipmac_adt_elem e = { .id = 0, .add_mac = 1 };
218         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
219         u32 ip;
220
221         /* MAC can be src only */
222         if (!(opt->flags & IPSET_DIM_TWO_SRC))
223                 return 0;
224
225         ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
226         if (ip < map->first_ip || ip > map->last_ip)
227                 return -IPSET_ERR_BITMAP_RANGE;
228
229         /* Backward compatibility: we don't check the second flag */
230         if (skb_mac_header(skb) < skb->head ||
231             (skb_mac_header(skb) + ETH_HLEN) > skb->data)
232                 return -EINVAL;
233
234         e.id = ip_to_id(map, ip);
235         memcpy(e.ether, eth_hdr(skb)->h_source, ETH_ALEN);
236
237         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
238 }
239
240 static int
241 bitmap_ipmac_uadt(struct ip_set *set, struct nlattr *tb[],
242                   enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
243 {
244         const struct bitmap_ipmac *map = set->data;
245         ipset_adtfn adtfn = set->variant->adt[adt];
246         struct bitmap_ipmac_adt_elem e = { .id = 0 };
247         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
248         u32 ip = 0;
249         int ret = 0;
250
251         if (tb[IPSET_ATTR_LINENO])
252                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
253
254         if (unlikely(!tb[IPSET_ATTR_IP]))
255                 return -IPSET_ERR_PROTOCOL;
256
257         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
258         if (ret)
259                 return ret;
260
261         ret = ip_set_get_extensions(set, tb, &ext);
262         if (ret)
263                 return ret;
264
265         if (ip < map->first_ip || ip > map->last_ip)
266                 return -IPSET_ERR_BITMAP_RANGE;
267
268         e.id = ip_to_id(map, ip);
269         if (tb[IPSET_ATTR_ETHER]) {
270                 memcpy(e.ether, nla_data(tb[IPSET_ATTR_ETHER]), ETH_ALEN);
271                 e.add_mac = 1;
272         }
273         ret = adtfn(set, &e, &ext, &ext, flags);
274
275         return ip_set_eexist(ret, flags) ? 0 : ret;
276 }
277
278 static bool
279 bitmap_ipmac_same_set(const struct ip_set *a, const struct ip_set *b)
280 {
281         const struct bitmap_ipmac *x = a->data;
282         const struct bitmap_ipmac *y = b->data;
283
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;
288 }
289
290 /* Plain variant */
291
292 #include "ip_set_bitmap_gen.h"
293
294 /* Create bitmap:ip,mac type of sets */
295
296 static bool
297 init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
298                u32 first_ip, u32 last_ip, u32 elements)
299 {
300         map->members = ip_set_alloc(map->memsize);
301         if (!map->members)
302                 return false;
303         map->first_ip = first_ip;
304         map->last_ip = last_ip;
305         map->elements = elements;
306         set->timeout = IPSET_NO_TIMEOUT;
307
308         set->data = map;
309         set->family = NFPROTO_IPV4;
310
311         return true;
312 }
313
314 static int
315 bitmap_ipmac_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
316                     u32 flags)
317 {
318         u32 first_ip = 0, last_ip = 0;
319         u64 elements;
320         struct bitmap_ipmac *map;
321         int ret;
322
323         if (unlikely(!tb[IPSET_ATTR_IP] ||
324                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
325                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
326                 return -IPSET_ERR_PROTOCOL;
327
328         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
329         if (ret)
330                 return ret;
331
332         if (tb[IPSET_ATTR_IP_TO]) {
333                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
334                 if (ret)
335                         return ret;
336                 if (first_ip > last_ip) {
337                         u32 tmp = first_ip;
338
339                         first_ip = last_ip;
340                         last_ip = tmp;
341                 }
342         } else if (tb[IPSET_ATTR_CIDR]) {
343                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
344
345                 if (cidr >= HOST_MASK)
346                         return -IPSET_ERR_INVALID_CIDR;
347                 ip_set_mask_from_to(first_ip, last_ip, cidr);
348         } else {
349                 return -IPSET_ERR_PROTOCOL;
350         }
351
352         elements = (u64)last_ip - first_ip + 1;
353
354         if (elements > IPSET_BITMAP_MAX_RANGE + 1)
355                 return -IPSET_ERR_BITMAP_RANGE_SIZE;
356
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);
361         if (!map)
362                 return -ENOMEM;
363
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)) {
367                 kfree(map);
368                 return -ENOMEM;
369         }
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);
373         }
374         return 0;
375 }
376
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,
386         .create_policy  = {
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 },
392         },
393         .adt_policy     = {
394                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
395                 [IPSET_ATTR_ETHER]      = { .type = NLA_BINARY,
396                                             .len  = ETH_ALEN },
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 },
406         },
407         .me             = THIS_MODULE,
408 };
409
410 static int __init
411 bitmap_ipmac_init(void)
412 {
413         return ip_set_type_register(&bitmap_ipmac_type);
414 }
415
416 static void __exit
417 bitmap_ipmac_fini(void)
418 {
419         rcu_barrier();
420         ip_set_type_unregister(&bitmap_ipmac_type);
421 }
422
423 module_init(bitmap_ipmac_init);
424 module_exit(bitmap_ipmac_fini);