spi: mediatek: single device does not require cs_gpios
[firefly-linux-kernel-4.4.55.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         void *members;          /* the set members */
50         void *extensions;       /* MAC + data extensions */
51         u32 first_ip;           /* host byte order, included in range */
52         u32 last_ip;            /* host byte order, included in range */
53         u32 elements;           /* number of max elements in the set */
54         size_t memsize;         /* members size */
55         struct timer_list gc;   /* garbage collector */
56 };
57
58 /* ADT structure for generic function args */
59 struct bitmap_ipmac_adt_elem {
60         u16 id;
61         unsigned char *ether;
62 };
63
64 struct bitmap_ipmac_elem {
65         unsigned char ether[ETH_ALEN];
66         unsigned char filled;
67 } __attribute__ ((aligned));
68
69 static inline u32
70 ip_to_id(const struct bitmap_ipmac *m, u32 ip)
71 {
72         return ip - m->first_ip;
73 }
74
75 static inline struct bitmap_ipmac_elem *
76 get_elem(void *extensions, u16 id, size_t dsize)
77 {
78         return (struct bitmap_ipmac_elem *)(extensions + id * dsize);
79 }
80
81 /* Common functions */
82
83 static inline int
84 bitmap_ipmac_do_test(const struct bitmap_ipmac_adt_elem *e,
85                      const struct bitmap_ipmac *map, size_t dsize)
86 {
87         const struct bitmap_ipmac_elem *elem;
88
89         if (!test_bit(e->id, map->members))
90                 return 0;
91         elem = get_elem(map->extensions, e->id, dsize);
92         if (elem->filled == MAC_FILLED)
93                 return !e->ether ||
94                        ether_addr_equal(e->ether, elem->ether);
95         /* Trigger kernel to fill out the ethernet address */
96         return -EAGAIN;
97 }
98
99 static inline int
100 bitmap_ipmac_gc_test(u16 id, const struct bitmap_ipmac *map, size_t dsize)
101 {
102         const struct bitmap_ipmac_elem *elem;
103
104         if (!test_bit(id, map->members))
105                 return 0;
106         elem = get_elem(map->extensions, id, dsize);
107         /* Timer not started for the incomplete elements */
108         return elem->filled == MAC_FILLED;
109 }
110
111 static inline int
112 bitmap_ipmac_is_filled(const struct bitmap_ipmac_elem *elem)
113 {
114         return elem->filled == MAC_FILLED;
115 }
116
117 static inline int
118 bitmap_ipmac_add_timeout(unsigned long *timeout,
119                          const struct bitmap_ipmac_adt_elem *e,
120                          const struct ip_set_ext *ext, struct ip_set *set,
121                          struct bitmap_ipmac *map, int mode)
122 {
123         u32 t = ext->timeout;
124
125         if (mode == IPSET_ADD_START_STORED_TIMEOUT) {
126                 if (t == set->timeout)
127                         /* Timeout was not specified, get stored one */
128                         t = *timeout;
129                 ip_set_timeout_set(timeout, t);
130         } else {
131                 /* If MAC is unset yet, we store plain timeout value
132                  * because the timer is not activated yet
133                  * and we can reuse it later when MAC is filled out,
134                  * possibly by the kernel
135                  */
136                 if (e->ether)
137                         ip_set_timeout_set(timeout, t);
138                 else
139                         *timeout = t;
140         }
141         return 0;
142 }
143
144 static inline int
145 bitmap_ipmac_do_add(const struct bitmap_ipmac_adt_elem *e,
146                     struct bitmap_ipmac *map, u32 flags, size_t dsize)
147 {
148         struct bitmap_ipmac_elem *elem;
149
150         elem = get_elem(map->extensions, e->id, dsize);
151         if (test_bit(e->id, map->members)) {
152                 if (elem->filled == MAC_FILLED) {
153                         if (e->ether &&
154                             (flags & IPSET_FLAG_EXIST) &&
155                             !ether_addr_equal(e->ether, elem->ether)) {
156                                 /* memcpy isn't atomic */
157                                 clear_bit(e->id, map->members);
158                                 smp_mb__after_atomic();
159                                 ether_addr_copy(elem->ether, e->ether);
160                         }
161                         return IPSET_ADD_FAILED;
162                 } else if (!e->ether)
163                         /* Already added without ethernet address */
164                         return IPSET_ADD_FAILED;
165                 /* Fill the MAC address and trigger the timer activation */
166                 clear_bit(e->id, map->members);
167                 smp_mb__after_atomic();
168                 ether_addr_copy(elem->ether, e->ether);
169                 elem->filled = MAC_FILLED;
170                 return IPSET_ADD_START_STORED_TIMEOUT;
171         } else if (e->ether) {
172                 /* We can store MAC too */
173                 ether_addr_copy(elem->ether, e->ether);
174                 elem->filled = MAC_FILLED;
175                 return 0;
176         }
177         elem->filled = MAC_UNSET;
178         /* MAC is not stored yet, don't start timer */
179         return IPSET_ADD_STORE_PLAIN_TIMEOUT;
180 }
181
182 static inline int
183 bitmap_ipmac_do_del(const struct bitmap_ipmac_adt_elem *e,
184                     struct bitmap_ipmac *map)
185 {
186         return !test_and_clear_bit(e->id, map->members);
187 }
188
189 static inline int
190 bitmap_ipmac_do_list(struct sk_buff *skb, const struct bitmap_ipmac *map,
191                      u32 id, size_t dsize)
192 {
193         const struct bitmap_ipmac_elem *elem =
194                 get_elem(map->extensions, id, dsize);
195
196         return nla_put_ipaddr4(skb, IPSET_ATTR_IP,
197                                htonl(map->first_ip + id)) ||
198                (elem->filled == MAC_FILLED &&
199                 nla_put(skb, IPSET_ATTR_ETHER, ETH_ALEN, elem->ether));
200 }
201
202 static inline int
203 bitmap_ipmac_do_head(struct sk_buff *skb, const struct bitmap_ipmac *map)
204 {
205         return nla_put_ipaddr4(skb, IPSET_ATTR_IP, htonl(map->first_ip)) ||
206                nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip));
207 }
208
209 static int
210 bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
211                   const struct xt_action_param *par,
212                   enum ipset_adt adt, struct ip_set_adt_opt *opt)
213 {
214         struct bitmap_ipmac *map = set->data;
215         ipset_adtfn adtfn = set->variant->adt[adt];
216         struct bitmap_ipmac_adt_elem e = { .id = 0 };
217         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
218         u32 ip;
219
220         /* MAC can be src only */
221         if (!(opt->flags & IPSET_DIM_TWO_SRC))
222                 return 0;
223
224         ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
225         if (ip < map->first_ip || ip > map->last_ip)
226                 return -IPSET_ERR_BITMAP_RANGE;
227
228         /* Backward compatibility: we don't check the second flag */
229         if (skb_mac_header(skb) < skb->head ||
230             (skb_mac_header(skb) + ETH_HLEN) > skb->data)
231                 return -EINVAL;
232
233         e.id = ip_to_id(map, ip);
234         e.ether = eth_hdr(skb)->h_source;
235
236         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
237 }
238
239 static int
240 bitmap_ipmac_uadt(struct ip_set *set, struct nlattr *tb[],
241                   enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
242 {
243         const struct bitmap_ipmac *map = set->data;
244         ipset_adtfn adtfn = set->variant->adt[adt];
245         struct bitmap_ipmac_adt_elem e = { .id = 0 };
246         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
247         u32 ip = 0;
248         int ret = 0;
249
250         if (tb[IPSET_ATTR_LINENO])
251                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
252
253         if (unlikely(!tb[IPSET_ATTR_IP]))
254                 return -IPSET_ERR_PROTOCOL;
255
256         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
257         if (ret)
258                 return ret;
259
260         ret = ip_set_get_extensions(set, tb, &ext);
261         if (ret)
262                 return ret;
263
264         if (ip < map->first_ip || ip > map->last_ip)
265                 return -IPSET_ERR_BITMAP_RANGE;
266
267         e.id = ip_to_id(map, ip);
268         if (tb[IPSET_ATTR_ETHER])
269                 e.ether = nla_data(tb[IPSET_ATTR_ETHER]);
270         else
271                 e.ether = NULL;
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         if (set->dsize) {
304                 map->extensions = ip_set_alloc(set->dsize * elements);
305                 if (!map->extensions) {
306                         kfree(map->members);
307                         return false;
308                 }
309         }
310         map->first_ip = first_ip;
311         map->last_ip = last_ip;
312         map->elements = elements;
313         set->timeout = IPSET_NO_TIMEOUT;
314
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                         u32 tmp = first_ip;
345
346                         first_ip = last_ip;
347                         last_ip = tmp;
348                 }
349         } else if (tb[IPSET_ATTR_CIDR]) {
350                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
351
352                 if (cidr >= HOST_MASK)
353                         return -IPSET_ERR_INVALID_CIDR;
354                 ip_set_mask_from_to(first_ip, last_ip, cidr);
355         } else {
356                 return -IPSET_ERR_PROTOCOL;
357         }
358
359         elements = (u64)last_ip - first_ip + 1;
360
361         if (elements > IPSET_BITMAP_MAX_RANGE + 1)
362                 return -IPSET_ERR_BITMAP_RANGE_SIZE;
363
364         map = kzalloc(sizeof(*map), GFP_KERNEL);
365         if (!map)
366                 return -ENOMEM;
367
368         map->memsize = bitmap_bytes(0, elements - 1);
369         set->variant = &bitmap_ipmac;
370         set->dsize = ip_set_elem_len(set, tb,
371                                      sizeof(struct bitmap_ipmac_elem));
372         if (!init_map_ipmac(set, map, first_ip, last_ip, elements)) {
373                 kfree(map);
374                 return -ENOMEM;
375         }
376         if (tb[IPSET_ATTR_TIMEOUT]) {
377                 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
378                 bitmap_ipmac_gc_init(set, bitmap_ipmac_gc);
379         }
380         return 0;
381 }
382
383 static struct ip_set_type bitmap_ipmac_type = {
384         .name           = "bitmap:ip,mac",
385         .protocol       = IPSET_PROTOCOL,
386         .features       = IPSET_TYPE_IP | IPSET_TYPE_MAC,
387         .dimension      = IPSET_DIM_TWO,
388         .family         = NFPROTO_IPV4,
389         .revision_min   = IPSET_TYPE_REV_MIN,
390         .revision_max   = IPSET_TYPE_REV_MAX,
391         .create         = bitmap_ipmac_create,
392         .create_policy  = {
393                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
394                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
395                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
396                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
397                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
398         },
399         .adt_policy     = {
400                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
401                 [IPSET_ATTR_ETHER]      = { .type = NLA_BINARY,
402                                             .len  = ETH_ALEN },
403                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
404                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
405                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
406                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
407                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING,
408                                             .len  = IPSET_MAX_COMMENT_SIZE },
409                 [IPSET_ATTR_SKBMARK]    = { .type = NLA_U64 },
410                 [IPSET_ATTR_SKBPRIO]    = { .type = NLA_U32 },
411                 [IPSET_ATTR_SKBQUEUE]   = { .type = NLA_U16 },
412         },
413         .me             = THIS_MODULE,
414 };
415
416 static int __init
417 bitmap_ipmac_init(void)
418 {
419         return ip_set_type_register(&bitmap_ipmac_type);
420 }
421
422 static void __exit
423 bitmap_ipmac_fini(void)
424 {
425         rcu_barrier();
426         ip_set_type_unregister(&bitmap_ipmac_type);
427 }
428
429 module_init(bitmap_ipmac_init);
430 module_exit(bitmap_ipmac_fini);