Merge branch 'spi/merge' of git://git.secretlab.ca/git/linux into spi-fix-grant
[firefly-linux-kernel-4.4.55.git] / net / netfilter / ipset / ip_set_hash_net.c
1 /* Copyright (C) 2003-2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation.
6  */
7
8 /* Kernel module implementing an IP set type: the hash:net type */
9
10 #include <linux/jhash.h>
11 #include <linux/module.h>
12 #include <linux/ip.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
16 #include <net/ip.h>
17 #include <net/ipv6.h>
18 #include <net/netlink.h>
19
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/ipset/pfxlen.h>
22 #include <linux/netfilter/ipset/ip_set.h>
23 #include <linux/netfilter/ipset/ip_set_timeout.h>
24 #include <linux/netfilter/ipset/ip_set_hash.h>
25
26 #define REVISION_MIN    0
27 /*                      1    Range as input support for IPv4 added */
28 #define REVISION_MAX    2 /* nomatch flag support added */
29
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
32 IP_SET_MODULE_DESC("hash:net", REVISION_MIN, REVISION_MAX);
33 MODULE_ALIAS("ip_set_hash:net");
34
35 /* Type specific function prefix */
36 #define TYPE            hash_net
37
38 static bool
39 hash_net_same_set(const struct ip_set *a, const struct ip_set *b);
40
41 #define hash_net4_same_set      hash_net_same_set
42 #define hash_net6_same_set      hash_net_same_set
43
44 /* The type variant functions: IPv4 */
45
46 /* Member elements without timeout */
47 struct hash_net4_elem {
48         __be32 ip;
49         u16 padding0;
50         u8 nomatch;
51         u8 cidr;
52 };
53
54 /* Member elements with timeout support */
55 struct hash_net4_telem {
56         __be32 ip;
57         u16 padding0;
58         u8 nomatch;
59         u8 cidr;
60         unsigned long timeout;
61 };
62
63 static inline bool
64 hash_net4_data_equal(const struct hash_net4_elem *ip1,
65                      const struct hash_net4_elem *ip2,
66                      u32 *multi)
67 {
68         return ip1->ip == ip2->ip &&
69                ip1->cidr == ip2->cidr;
70 }
71
72 static inline bool
73 hash_net4_data_isnull(const struct hash_net4_elem *elem)
74 {
75         return elem->cidr == 0;
76 }
77
78 static inline void
79 hash_net4_data_copy(struct hash_net4_elem *dst,
80                     const struct hash_net4_elem *src)
81 {
82         dst->ip = src->ip;
83         dst->cidr = src->cidr;
84         dst->nomatch = src->nomatch;
85 }
86
87 static inline void
88 hash_net4_data_flags(struct hash_net4_elem *dst, u32 flags)
89 {
90         dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH);
91 }
92
93 static inline void
94 hash_net4_data_reset_flags(struct hash_net4_elem *dst, u32 *flags)
95 {
96         if (dst->nomatch) {
97                 *flags = IPSET_FLAG_NOMATCH;
98                 dst->nomatch = 0;
99         }
100 }
101
102 static inline int
103 hash_net4_data_match(const struct hash_net4_elem *elem)
104 {
105         return elem->nomatch ? -ENOTEMPTY : 1;
106 }
107
108 static inline void
109 hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
110 {
111         elem->ip &= ip_set_netmask(cidr);
112         elem->cidr = cidr;
113 }
114
115 /* Zero CIDR values cannot be stored */
116 static inline void
117 hash_net4_data_zero_out(struct hash_net4_elem *elem)
118 {
119         elem->cidr = 0;
120 }
121
122 static bool
123 hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
124 {
125         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
126
127         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
128             nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
129             (flags &&
130              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
131                 goto nla_put_failure;
132         return 0;
133
134 nla_put_failure:
135         return 1;
136 }
137
138 static bool
139 hash_net4_data_tlist(struct sk_buff *skb, const struct hash_net4_elem *data)
140 {
141         const struct hash_net4_telem *tdata =
142                 (const struct hash_net4_telem *)data;
143         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
144
145         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, tdata->ip) ||
146             nla_put_u8(skb, IPSET_ATTR_CIDR, tdata->cidr) ||
147             nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
148                           htonl(ip_set_timeout_get(tdata->timeout))) ||
149             (flags &&
150              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
151                 goto nla_put_failure;
152         return 0;
153
154 nla_put_failure:
155         return 1;
156 }
157
158 #define IP_SET_HASH_WITH_NETS
159
160 #define PF              4
161 #define HOST_MASK       32
162 #include <linux/netfilter/ipset/ip_set_ahash.h>
163
164 static inline void
165 hash_net4_data_next(struct ip_set_hash *h,
166                     const struct hash_net4_elem *d)
167 {
168         h->next.ip = d->ip;
169 }
170
171 static int
172 hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
173                const struct xt_action_param *par,
174                enum ipset_adt adt, const struct ip_set_adt_opt *opt)
175 {
176         const struct ip_set_hash *h = set->data;
177         ipset_adtfn adtfn = set->variant->adt[adt];
178         struct hash_net4_elem data = {
179                 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
180         };
181
182         if (data.cidr == 0)
183                 return -EINVAL;
184         if (adt == IPSET_TEST)
185                 data.cidr = HOST_MASK;
186
187         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
188         data.ip &= ip_set_netmask(data.cidr);
189
190         return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
191 }
192
193 static int
194 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
195                enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
196 {
197         const struct ip_set_hash *h = set->data;
198         ipset_adtfn adtfn = set->variant->adt[adt];
199         struct hash_net4_elem data = { .cidr = HOST_MASK };
200         u32 timeout = h->timeout;
201         u32 ip = 0, ip_to, last;
202         int ret;
203
204         if (unlikely(!tb[IPSET_ATTR_IP] ||
205                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
206                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
207                 return -IPSET_ERR_PROTOCOL;
208
209         if (tb[IPSET_ATTR_LINENO])
210                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
211
212         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
213         if (ret)
214                 return ret;
215
216         if (tb[IPSET_ATTR_CIDR]) {
217                 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
218                 if (!data.cidr || data.cidr > HOST_MASK)
219                         return -IPSET_ERR_INVALID_CIDR;
220         }
221
222         if (tb[IPSET_ATTR_TIMEOUT]) {
223                 if (!with_timeout(h->timeout))
224                         return -IPSET_ERR_TIMEOUT;
225                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
226         }
227
228         if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
229                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
230                 if (cadt_flags & IPSET_FLAG_NOMATCH)
231                         flags |= (cadt_flags << 16);
232         }
233
234         if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
235                 data.ip = htonl(ip & ip_set_hostmask(data.cidr));
236                 ret = adtfn(set, &data, timeout, flags);
237                 return ip_set_eexist(ret, flags) ? 0 : ret;
238         }
239
240         ip_to = ip;
241         if (tb[IPSET_ATTR_IP_TO]) {
242                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
243                 if (ret)
244                         return ret;
245                 if (ip_to < ip)
246                         swap(ip, ip_to);
247                 if (ip + UINT_MAX == ip_to)
248                         return -IPSET_ERR_HASH_RANGE;
249         }
250         if (retried)
251                 ip = ntohl(h->next.ip);
252         while (!after(ip, ip_to)) {
253                 data.ip = htonl(ip);
254                 last = ip_set_range_to_cidr(ip, ip_to, &data.cidr);
255                 ret = adtfn(set, &data, timeout, flags);
256                 if (ret && !ip_set_eexist(ret, flags))
257                         return ret;
258                 else
259                         ret = 0;
260                 ip = last + 1;
261         }
262         return ret;
263 }
264
265 static bool
266 hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
267 {
268         const struct ip_set_hash *x = a->data;
269         const struct ip_set_hash *y = b->data;
270
271         /* Resizing changes htable_bits, so we ignore it */
272         return x->maxelem == y->maxelem &&
273                x->timeout == y->timeout;
274 }
275
276 /* The type variant functions: IPv6 */
277
278 struct hash_net6_elem {
279         union nf_inet_addr ip;
280         u16 padding0;
281         u8 nomatch;
282         u8 cidr;
283 };
284
285 struct hash_net6_telem {
286         union nf_inet_addr ip;
287         u16 padding0;
288         u8 nomatch;
289         u8 cidr;
290         unsigned long timeout;
291 };
292
293 static inline bool
294 hash_net6_data_equal(const struct hash_net6_elem *ip1,
295                      const struct hash_net6_elem *ip2,
296                      u32 *multi)
297 {
298         return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
299                ip1->cidr == ip2->cidr;
300 }
301
302 static inline bool
303 hash_net6_data_isnull(const struct hash_net6_elem *elem)
304 {
305         return elem->cidr == 0;
306 }
307
308 static inline void
309 hash_net6_data_copy(struct hash_net6_elem *dst,
310                     const struct hash_net6_elem *src)
311 {
312         dst->ip.in6 = src->ip.in6;
313         dst->cidr = src->cidr;
314         dst->nomatch = src->nomatch;
315 }
316
317 static inline void
318 hash_net6_data_flags(struct hash_net6_elem *dst, u32 flags)
319 {
320         dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH);
321 }
322
323 static inline void
324 hash_net6_data_reset_flags(struct hash_net6_elem *dst, u32 *flags)
325 {
326         if (dst->nomatch) {
327                 *flags = IPSET_FLAG_NOMATCH;
328                 dst->nomatch = 0;
329         }
330 }
331
332 static inline int
333 hash_net6_data_match(const struct hash_net6_elem *elem)
334 {
335         return elem->nomatch ? -ENOTEMPTY : 1;
336 }
337
338 static inline void
339 hash_net6_data_zero_out(struct hash_net6_elem *elem)
340 {
341         elem->cidr = 0;
342 }
343
344 static inline void
345 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
346 {
347         ip->ip6[0] &= ip_set_netmask6(prefix)[0];
348         ip->ip6[1] &= ip_set_netmask6(prefix)[1];
349         ip->ip6[2] &= ip_set_netmask6(prefix)[2];
350         ip->ip6[3] &= ip_set_netmask6(prefix)[3];
351 }
352
353 static inline void
354 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
355 {
356         ip6_netmask(&elem->ip, cidr);
357         elem->cidr = cidr;
358 }
359
360 static bool
361 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
362 {
363         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
364
365         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
366             nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
367             (flags &&
368              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
369                 goto nla_put_failure;
370         return 0;
371
372 nla_put_failure:
373         return 1;
374 }
375
376 static bool
377 hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
378 {
379         const struct hash_net6_telem *e =
380                 (const struct hash_net6_telem *)data;
381         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
382
383         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
384             nla_put_u8(skb, IPSET_ATTR_CIDR, e->cidr) ||
385             nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
386                           htonl(ip_set_timeout_get(e->timeout))) ||
387             (flags &&
388              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
389                 goto nla_put_failure;
390         return 0;
391
392 nla_put_failure:
393         return 1;
394 }
395
396 #undef PF
397 #undef HOST_MASK
398
399 #define PF              6
400 #define HOST_MASK       128
401 #include <linux/netfilter/ipset/ip_set_ahash.h>
402
403 static inline void
404 hash_net6_data_next(struct ip_set_hash *h,
405                     const struct hash_net6_elem *d)
406 {
407 }
408
409 static int
410 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
411                const struct xt_action_param *par,
412                enum ipset_adt adt, const struct ip_set_adt_opt *opt)
413 {
414         const struct ip_set_hash *h = set->data;
415         ipset_adtfn adtfn = set->variant->adt[adt];
416         struct hash_net6_elem data = {
417                 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
418         };
419
420         if (data.cidr == 0)
421                 return -EINVAL;
422         if (adt == IPSET_TEST)
423                 data.cidr = HOST_MASK;
424
425         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
426         ip6_netmask(&data.ip, data.cidr);
427
428         return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
429 }
430
431 static int
432 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
433                enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
434 {
435         const struct ip_set_hash *h = set->data;
436         ipset_adtfn adtfn = set->variant->adt[adt];
437         struct hash_net6_elem data = { .cidr = HOST_MASK };
438         u32 timeout = h->timeout;
439         int ret;
440
441         if (unlikely(!tb[IPSET_ATTR_IP] ||
442                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
443                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
444                 return -IPSET_ERR_PROTOCOL;
445         if (unlikely(tb[IPSET_ATTR_IP_TO]))
446                 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
447
448         if (tb[IPSET_ATTR_LINENO])
449                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
450
451         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
452         if (ret)
453                 return ret;
454
455         if (tb[IPSET_ATTR_CIDR])
456                 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
457
458         if (!data.cidr || data.cidr > HOST_MASK)
459                 return -IPSET_ERR_INVALID_CIDR;
460
461         ip6_netmask(&data.ip, data.cidr);
462
463         if (tb[IPSET_ATTR_TIMEOUT]) {
464                 if (!with_timeout(h->timeout))
465                         return -IPSET_ERR_TIMEOUT;
466                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
467         }
468
469         if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
470                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
471                 if (cadt_flags & IPSET_FLAG_NOMATCH)
472                         flags |= (cadt_flags << 16);
473         }
474
475         ret = adtfn(set, &data, timeout, flags);
476
477         return ip_set_eexist(ret, flags) ? 0 : ret;
478 }
479
480 /* Create hash:ip type of sets */
481
482 static int
483 hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
484 {
485         u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
486         struct ip_set_hash *h;
487         u8 hbits;
488         size_t hsize;
489
490         if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
491                 return -IPSET_ERR_INVALID_FAMILY;
492
493         if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
494                      !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
495                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
496                 return -IPSET_ERR_PROTOCOL;
497
498         if (tb[IPSET_ATTR_HASHSIZE]) {
499                 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
500                 if (hashsize < IPSET_MIMINAL_HASHSIZE)
501                         hashsize = IPSET_MIMINAL_HASHSIZE;
502         }
503
504         if (tb[IPSET_ATTR_MAXELEM])
505                 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
506
507         h = kzalloc(sizeof(*h)
508                     + sizeof(struct ip_set_hash_nets)
509                       * (set->family == NFPROTO_IPV4 ? 32 : 128), GFP_KERNEL);
510         if (!h)
511                 return -ENOMEM;
512
513         h->maxelem = maxelem;
514         get_random_bytes(&h->initval, sizeof(h->initval));
515         h->timeout = IPSET_NO_TIMEOUT;
516
517         hbits = htable_bits(hashsize);
518         hsize = htable_size(hbits);
519         if (hsize == 0) {
520                 kfree(h);
521                 return -ENOMEM;
522         }
523         h->table = ip_set_alloc(hsize);
524         if (!h->table) {
525                 kfree(h);
526                 return -ENOMEM;
527         }
528         h->table->htable_bits = hbits;
529
530         set->data = h;
531
532         if (tb[IPSET_ATTR_TIMEOUT]) {
533                 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
534
535                 set->variant = set->family == NFPROTO_IPV4
536                         ? &hash_net4_tvariant : &hash_net6_tvariant;
537
538                 if (set->family == NFPROTO_IPV4)
539                         hash_net4_gc_init(set);
540                 else
541                         hash_net6_gc_init(set);
542         } else {
543                 set->variant = set->family == NFPROTO_IPV4
544                         ? &hash_net4_variant : &hash_net6_variant;
545         }
546
547         pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
548                  set->name, jhash_size(h->table->htable_bits),
549                  h->table->htable_bits, h->maxelem, set->data, h->table);
550
551         return 0;
552 }
553
554 static struct ip_set_type hash_net_type __read_mostly = {
555         .name           = "hash:net",
556         .protocol       = IPSET_PROTOCOL,
557         .features       = IPSET_TYPE_IP | IPSET_TYPE_NOMATCH,
558         .dimension      = IPSET_DIM_ONE,
559         .family         = NFPROTO_UNSPEC,
560         .revision_min   = REVISION_MIN,
561         .revision_max   = REVISION_MAX,
562         .create         = hash_net_create,
563         .create_policy  = {
564                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
565                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
566                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
567                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
568                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
569         },
570         .adt_policy     = {
571                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
572                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
573                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
574                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
575                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
576         },
577         .me             = THIS_MODULE,
578 };
579
580 static int __init
581 hash_net_init(void)
582 {
583         return ip_set_type_register(&hash_net_type);
584 }
585
586 static void __exit
587 hash_net_fini(void)
588 {
589         ip_set_type_unregister(&hash_net_type);
590 }
591
592 module_init(hash_net_init);
593 module_exit(hash_net_fini);