GNU Linux-libre 4.19.264-gnu1
[releases.git] / 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         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));
58 };
59
60 /* ADT structure for generic function args */
61 struct bitmap_ipmac_adt_elem {
62         unsigned char ether[ETH_ALEN] __aligned(2);
63         u16 id;
64         u16 add_mac;
65 };
66
67 struct bitmap_ipmac_elem {
68         unsigned char ether[ETH_ALEN];
69         unsigned char filled;
70 } __aligned(__alignof__(u64));
71
72 static inline u32
73 ip_to_id(const struct bitmap_ipmac *m, u32 ip)
74 {
75         return ip - m->first_ip;
76 }
77
78 #define get_elem(extensions, id, dsize)         \
79         (struct bitmap_ipmac_elem *)(extensions + (id) * (dsize))
80
81 #define get_const_elem(extensions, id, dsize)   \
82         (const struct bitmap_ipmac_elem *)(extensions + (id) * (dsize))
83
84 /* Common functions */
85
86 static inline int
87 bitmap_ipmac_do_test(const struct bitmap_ipmac_adt_elem *e,
88                      const struct bitmap_ipmac *map, size_t dsize)
89 {
90         const struct bitmap_ipmac_elem *elem;
91
92         if (!test_bit(e->id, map->members))
93                 return 0;
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 */
98         return -EAGAIN;
99 }
100
101 static inline int
102 bitmap_ipmac_gc_test(u16 id, const struct bitmap_ipmac *map, size_t dsize)
103 {
104         const struct bitmap_ipmac_elem *elem;
105
106         if (!test_bit(id, map->members))
107                 return 0;
108         elem = get_const_elem(map->extensions, id, dsize);
109         /* Timer not started for the incomplete elements */
110         return elem->filled == MAC_FILLED;
111 }
112
113 static inline int
114 bitmap_ipmac_is_filled(const struct bitmap_ipmac_elem *elem)
115 {
116         return elem->filled == MAC_FILLED;
117 }
118
119 static inline int
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)
124 {
125         u32 t = ext->timeout;
126
127         if (mode == IPSET_ADD_START_STORED_TIMEOUT) {
128                 if (t == set->timeout)
129                         /* Timeout was not specified, get stored one */
130                         t = *timeout;
131                 ip_set_timeout_set(timeout, t);
132         } else {
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
137                  */
138                 if (e->add_mac)
139                         ip_set_timeout_set(timeout, t);
140                 else
141                         *timeout = t;
142         }
143         return 0;
144 }
145
146 static inline int
147 bitmap_ipmac_do_add(const struct bitmap_ipmac_adt_elem *e,
148                     struct bitmap_ipmac *map, u32 flags, size_t dsize)
149 {
150         struct bitmap_ipmac_elem *elem;
151
152         elem = get_elem(map->extensions, e->id, dsize);
153         if (test_bit(e->id, map->members)) {
154                 if (elem->filled == MAC_FILLED) {
155                         if (e->add_mac &&
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);
162                         }
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;
177                 return 0;
178         }
179         elem->filled = MAC_UNSET;
180         /* MAC is not stored yet, don't start timer */
181         return IPSET_ADD_STORE_PLAIN_TIMEOUT;
182 }
183
184 static inline int
185 bitmap_ipmac_do_del(const struct bitmap_ipmac_adt_elem *e,
186                     struct bitmap_ipmac *map)
187 {
188         return !test_and_clear_bit(e->id, map->members);
189 }
190
191 static inline int
192 bitmap_ipmac_do_list(struct sk_buff *skb, const struct bitmap_ipmac *map,
193                      u32 id, size_t dsize)
194 {
195         const struct bitmap_ipmac_elem *elem =
196                 get_const_elem(map->extensions, id, dsize);
197
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));
202 }
203
204 static inline int
205 bitmap_ipmac_do_head(struct sk_buff *skb, const struct bitmap_ipmac *map)
206 {
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));
209 }
210
211 static int
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)
215 {
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);
220         u32 ip;
221
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;
225
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)
229                 return -EINVAL;
230
231         e.id = ip_to_id(map, ip);
232
233         if (opt->flags & IPSET_DIM_TWO_SRC)
234                 ether_addr_copy(e.ether, eth_hdr(skb)->h_source);
235         else
236                 ether_addr_copy(e.ether, eth_hdr(skb)->h_dest);
237
238         if (is_zero_ether_addr(e.ether))
239                 return -EINVAL;
240
241         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
242 }
243
244 static int
245 bitmap_ipmac_uadt(struct ip_set *set, struct nlattr *tb[],
246                   enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
247 {
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);
252         u32 ip = 0;
253         int ret = 0;
254
255         if (tb[IPSET_ATTR_LINENO])
256                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
257
258         if (unlikely(!tb[IPSET_ATTR_IP]))
259                 return -IPSET_ERR_PROTOCOL;
260
261         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
262         if (ret)
263                 return ret;
264
265         ret = ip_set_get_extensions(set, tb, &ext);
266         if (ret)
267                 return ret;
268
269         if (ip < map->first_ip || ip > map->last_ip)
270                 return -IPSET_ERR_BITMAP_RANGE;
271
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);
277                 e.add_mac = 1;
278         }
279         ret = adtfn(set, &e, &ext, &ext, flags);
280
281         return ip_set_eexist(ret, flags) ? 0 : ret;
282 }
283
284 static bool
285 bitmap_ipmac_same_set(const struct ip_set *a, const struct ip_set *b)
286 {
287         const struct bitmap_ipmac *x = a->data;
288         const struct bitmap_ipmac *y = b->data;
289
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;
294 }
295
296 /* Plain variant */
297
298 #include "ip_set_bitmap_gen.h"
299
300 /* Create bitmap:ip,mac type of sets */
301
302 static bool
303 init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
304                u32 first_ip, u32 last_ip, u32 elements)
305 {
306         map->members = bitmap_zalloc(elements, GFP_KERNEL | __GFP_NOWARN);
307         if (!map->members)
308                 return false;
309         map->first_ip = first_ip;
310         map->last_ip = last_ip;
311         map->elements = elements;
312         set->timeout = IPSET_NO_TIMEOUT;
313
314         map->set = set;
315         set->data = map;
316         set->family = NFPROTO_IPV4;
317
318         return true;
319 }
320
321 static int
322 bitmap_ipmac_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
323                     u32 flags)
324 {
325         u32 first_ip = 0, last_ip = 0;
326         u64 elements;
327         struct bitmap_ipmac *map;
328         int ret;
329
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;
334
335         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
336         if (ret)
337                 return ret;
338
339         if (tb[IPSET_ATTR_IP_TO]) {
340                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
341                 if (ret)
342                         return ret;
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]);
347
348                 if (cidr >= HOST_MASK)
349                         return -IPSET_ERR_INVALID_CIDR;
350                 ip_set_mask_from_to(first_ip, last_ip, cidr);
351         } else {
352                 return -IPSET_ERR_PROTOCOL;
353         }
354
355         elements = (u64)last_ip - first_ip + 1;
356
357         if (elements > IPSET_BITMAP_MAX_RANGE + 1)
358                 return -IPSET_ERR_BITMAP_RANGE_SIZE;
359
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);
364         if (!map)
365                 return -ENOMEM;
366
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)) {
370                 kfree(map);
371                 return -ENOMEM;
372         }
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);
376         }
377         return 0;
378 }
379
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,
389         .create_policy  = {
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 },
395         },
396         .adt_policy     = {
397                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
398                 [IPSET_ATTR_ETHER]      = { .type = NLA_BINARY,
399                                             .len  = ETH_ALEN },
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 },
409         },
410         .me             = THIS_MODULE,
411 };
412
413 static int __init
414 bitmap_ipmac_init(void)
415 {
416         return ip_set_type_register(&bitmap_ipmac_type);
417 }
418
419 static void __exit
420 bitmap_ipmac_fini(void)
421 {
422         rcu_barrier();
423         ip_set_type_unregister(&bitmap_ipmac_type);
424 }
425
426 module_init(bitmap_ipmac_init);
427 module_exit(bitmap_ipmac_fini);