Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-2.6-microblaze.git] / net / netfilter / ipset / ip_set_hash_netnet.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org>
3  * Copyright (C) 2013 Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>
4  */
5
6 /* Kernel module implementing an IP set type: the hash:net type */
7
8 #include <linux/jhash.h>
9 #include <linux/module.h>
10 #include <linux/ip.h>
11 #include <linux/skbuff.h>
12 #include <linux/errno.h>
13 #include <linux/random.h>
14 #include <net/ip.h>
15 #include <net/ipv6.h>
16 #include <net/netlink.h>
17
18 #include <linux/netfilter.h>
19 #include <linux/netfilter/ipset/pfxlen.h>
20 #include <linux/netfilter/ipset/ip_set.h>
21 #include <linux/netfilter/ipset/ip_set_hash.h>
22
23 #define IPSET_TYPE_REV_MIN      0
24 /*                              1          Forceadd support added */
25 /*                              2          skbinfo support added */
26 #define IPSET_TYPE_REV_MAX      3       /* bucketsize, initval support added */
27
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
30 IP_SET_MODULE_DESC("hash:net,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
31 MODULE_ALIAS("ip_set_hash:net,net");
32
33 /* Type specific function prefix */
34 #define HTYPE           hash_netnet
35 #define IP_SET_HASH_WITH_NETS
36 #define IPSET_NET_COUNT 2
37
38 /* IPv4 variants */
39
40 /* Member elements  */
41 struct hash_netnet4_elem {
42         union {
43                 __be32 ip[2];
44                 __be64 ipcmp;
45         };
46         u8 nomatch;
47         u8 padding;
48         union {
49                 u8 cidr[2];
50                 u16 ccmp;
51         };
52 };
53
54 /* Common functions */
55
56 static bool
57 hash_netnet4_data_equal(const struct hash_netnet4_elem *ip1,
58                         const struct hash_netnet4_elem *ip2,
59                         u32 *multi)
60 {
61         return ip1->ipcmp == ip2->ipcmp &&
62                ip1->ccmp == ip2->ccmp;
63 }
64
65 static int
66 hash_netnet4_do_data_match(const struct hash_netnet4_elem *elem)
67 {
68         return elem->nomatch ? -ENOTEMPTY : 1;
69 }
70
71 static void
72 hash_netnet4_data_set_flags(struct hash_netnet4_elem *elem, u32 flags)
73 {
74         elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
75 }
76
77 static void
78 hash_netnet4_data_reset_flags(struct hash_netnet4_elem *elem, u8 *flags)
79 {
80         swap(*flags, elem->nomatch);
81 }
82
83 static void
84 hash_netnet4_data_reset_elem(struct hash_netnet4_elem *elem,
85                              struct hash_netnet4_elem *orig)
86 {
87         elem->ip[1] = orig->ip[1];
88 }
89
90 static void
91 hash_netnet4_data_netmask(struct hash_netnet4_elem *elem, u8 cidr, bool inner)
92 {
93         if (inner) {
94                 elem->ip[1] &= ip_set_netmask(cidr);
95                 elem->cidr[1] = cidr;
96         } else {
97                 elem->ip[0] &= ip_set_netmask(cidr);
98                 elem->cidr[0] = cidr;
99         }
100 }
101
102 static bool
103 hash_netnet4_data_list(struct sk_buff *skb,
104                        const struct hash_netnet4_elem *data)
105 {
106         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
107
108         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
109             nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
110             nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
111             nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
112             (flags &&
113              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
114                 goto nla_put_failure;
115         return false;
116
117 nla_put_failure:
118         return true;
119 }
120
121 static void
122 hash_netnet4_data_next(struct hash_netnet4_elem *next,
123                        const struct hash_netnet4_elem *d)
124 {
125         next->ipcmp = d->ipcmp;
126 }
127
128 #define MTYPE           hash_netnet4
129 #define HOST_MASK       32
130 #include "ip_set_hash_gen.h"
131
132 static void
133 hash_netnet4_init(struct hash_netnet4_elem *e)
134 {
135         e->cidr[0] = HOST_MASK;
136         e->cidr[1] = HOST_MASK;
137 }
138
139 static int
140 hash_netnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
141                   const struct xt_action_param *par,
142                   enum ipset_adt adt, struct ip_set_adt_opt *opt)
143 {
144         const struct hash_netnet4 *h = set->data;
145         ipset_adtfn adtfn = set->variant->adt[adt];
146         struct hash_netnet4_elem e = { };
147         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
148
149         e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
150         e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
151         if (adt == IPSET_TEST)
152                 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
153
154         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
155         ip4addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1]);
156         e.ip[0] &= ip_set_netmask(e.cidr[0]);
157         e.ip[1] &= ip_set_netmask(e.cidr[1]);
158
159         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
160 }
161
162 static int
163 hash_netnet4_uadt(struct ip_set *set, struct nlattr *tb[],
164                   enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
165 {
166         const struct hash_netnet4 *h = set->data;
167         ipset_adtfn adtfn = set->variant->adt[adt];
168         struct hash_netnet4_elem e = { };
169         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
170         u32 ip = 0, ip_to = 0;
171         u32 ip2 = 0, ip2_from = 0, ip2_to = 0;
172         int ret;
173
174         if (tb[IPSET_ATTR_LINENO])
175                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
176
177         hash_netnet4_init(&e);
178         if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
179                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
180                 return -IPSET_ERR_PROTOCOL;
181
182         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
183         if (ret)
184                 return ret;
185
186         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
187         if (ret)
188                 return ret;
189
190         ret = ip_set_get_extensions(set, tb, &ext);
191         if (ret)
192                 return ret;
193
194         if (tb[IPSET_ATTR_CIDR]) {
195                 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
196                 if (!e.cidr[0] || e.cidr[0] > HOST_MASK)
197                         return -IPSET_ERR_INVALID_CIDR;
198         }
199
200         if (tb[IPSET_ATTR_CIDR2]) {
201                 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
202                 if (!e.cidr[1] || e.cidr[1] > HOST_MASK)
203                         return -IPSET_ERR_INVALID_CIDR;
204         }
205
206         if (tb[IPSET_ATTR_CADT_FLAGS]) {
207                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
208
209                 if (cadt_flags & IPSET_FLAG_NOMATCH)
210                         flags |= (IPSET_FLAG_NOMATCH << 16);
211         }
212
213         if (adt == IPSET_TEST || !(tb[IPSET_ATTR_IP_TO] ||
214                                    tb[IPSET_ATTR_IP2_TO])) {
215                 e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
216                 e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1]));
217                 ret = adtfn(set, &e, &ext, &ext, flags);
218                 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
219                        ip_set_eexist(ret, flags) ? 0 : ret;
220         }
221
222         ip_to = ip;
223         if (tb[IPSET_ATTR_IP_TO]) {
224                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
225                 if (ret)
226                         return ret;
227                 if (ip_to < ip)
228                         swap(ip, ip_to);
229                 if (unlikely(ip + UINT_MAX == ip_to))
230                         return -IPSET_ERR_HASH_RANGE;
231         } else {
232                 ip_set_mask_from_to(ip, ip_to, e.cidr[0]);
233         }
234
235         ip2_to = ip2_from;
236         if (tb[IPSET_ATTR_IP2_TO]) {
237                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
238                 if (ret)
239                         return ret;
240                 if (ip2_to < ip2_from)
241                         swap(ip2_from, ip2_to);
242                 if (unlikely(ip2_from + UINT_MAX == ip2_to))
243                         return -IPSET_ERR_HASH_RANGE;
244         } else {
245                 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]);
246         }
247
248         if (retried) {
249                 ip = ntohl(h->next.ip[0]);
250                 ip2 = ntohl(h->next.ip[1]);
251         } else {
252                 ip2 = ip2_from;
253         }
254
255         do {
256                 e.ip[0] = htonl(ip);
257                 ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr[0]);
258                 do {
259                         e.ip[1] = htonl(ip2);
260                         ip2 = ip_set_range_to_cidr(ip2, ip2_to, &e.cidr[1]);
261                         ret = adtfn(set, &e, &ext, &ext, flags);
262                         if (ret && !ip_set_eexist(ret, flags))
263                                 return ret;
264
265                         ret = 0;
266                 } while (ip2++ < ip2_to);
267                 ip2 = ip2_from;
268         } while (ip++ < ip_to);
269         return ret;
270 }
271
272 /* IPv6 variants */
273
274 struct hash_netnet6_elem {
275         union nf_inet_addr ip[2];
276         u8 nomatch;
277         u8 padding;
278         union {
279                 u8 cidr[2];
280                 u16 ccmp;
281         };
282 };
283
284 /* Common functions */
285
286 static bool
287 hash_netnet6_data_equal(const struct hash_netnet6_elem *ip1,
288                         const struct hash_netnet6_elem *ip2,
289                         u32 *multi)
290 {
291         return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
292                ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
293                ip1->ccmp == ip2->ccmp;
294 }
295
296 static int
297 hash_netnet6_do_data_match(const struct hash_netnet6_elem *elem)
298 {
299         return elem->nomatch ? -ENOTEMPTY : 1;
300 }
301
302 static void
303 hash_netnet6_data_set_flags(struct hash_netnet6_elem *elem, u32 flags)
304 {
305         elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
306 }
307
308 static void
309 hash_netnet6_data_reset_flags(struct hash_netnet6_elem *elem, u8 *flags)
310 {
311         swap(*flags, elem->nomatch);
312 }
313
314 static void
315 hash_netnet6_data_reset_elem(struct hash_netnet6_elem *elem,
316                              struct hash_netnet6_elem *orig)
317 {
318         elem->ip[1] = orig->ip[1];
319 }
320
321 static void
322 hash_netnet6_data_netmask(struct hash_netnet6_elem *elem, u8 cidr, bool inner)
323 {
324         if (inner) {
325                 ip6_netmask(&elem->ip[1], cidr);
326                 elem->cidr[1] = cidr;
327         } else {
328                 ip6_netmask(&elem->ip[0], cidr);
329                 elem->cidr[0] = cidr;
330         }
331 }
332
333 static bool
334 hash_netnet6_data_list(struct sk_buff *skb,
335                        const struct hash_netnet6_elem *data)
336 {
337         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
338
339         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
340             nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
341             nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
342             nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
343             (flags &&
344              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
345                 goto nla_put_failure;
346         return false;
347
348 nla_put_failure:
349         return true;
350 }
351
352 static void
353 hash_netnet6_data_next(struct hash_netnet6_elem *next,
354                        const struct hash_netnet6_elem *d)
355 {
356 }
357
358 #undef MTYPE
359 #undef HOST_MASK
360
361 #define MTYPE           hash_netnet6
362 #define HOST_MASK       128
363 #define IP_SET_EMIT_CREATE
364 #include "ip_set_hash_gen.h"
365
366 static void
367 hash_netnet6_init(struct hash_netnet6_elem *e)
368 {
369         e->cidr[0] = HOST_MASK;
370         e->cidr[1] = HOST_MASK;
371 }
372
373 static int
374 hash_netnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
375                   const struct xt_action_param *par,
376                   enum ipset_adt adt, struct ip_set_adt_opt *opt)
377 {
378         const struct hash_netnet6 *h = set->data;
379         ipset_adtfn adtfn = set->variant->adt[adt];
380         struct hash_netnet6_elem e = { };
381         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
382
383         e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
384         e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
385         if (adt == IPSET_TEST)
386                 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
387
388         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
389         ip6addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1].in6);
390         ip6_netmask(&e.ip[0], e.cidr[0]);
391         ip6_netmask(&e.ip[1], e.cidr[1]);
392
393         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
394 }
395
396 static int
397 hash_netnet6_uadt(struct ip_set *set, struct nlattr *tb[],
398                   enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
399 {
400         ipset_adtfn adtfn = set->variant->adt[adt];
401         struct hash_netnet6_elem e = { };
402         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
403         int ret;
404
405         if (tb[IPSET_ATTR_LINENO])
406                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
407
408         hash_netnet6_init(&e);
409         if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
410                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
411                 return -IPSET_ERR_PROTOCOL;
412         if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
413                 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
414
415         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]);
416         if (ret)
417                 return ret;
418
419         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]);
420         if (ret)
421                 return ret;
422
423         ret = ip_set_get_extensions(set, tb, &ext);
424         if (ret)
425                 return ret;
426
427         if (tb[IPSET_ATTR_CIDR]) {
428                 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
429                 if (!e.cidr[0] || e.cidr[0] > HOST_MASK)
430                         return -IPSET_ERR_INVALID_CIDR;
431         }
432
433         if (tb[IPSET_ATTR_CIDR2]) {
434                 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
435                 if (!e.cidr[1] || e.cidr[1] > HOST_MASK)
436                         return -IPSET_ERR_INVALID_CIDR;
437         }
438
439         ip6_netmask(&e.ip[0], e.cidr[0]);
440         ip6_netmask(&e.ip[1], e.cidr[1]);
441
442         if (tb[IPSET_ATTR_CADT_FLAGS]) {
443                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
444
445                 if (cadt_flags & IPSET_FLAG_NOMATCH)
446                         flags |= (IPSET_FLAG_NOMATCH << 16);
447         }
448
449         ret = adtfn(set, &e, &ext, &ext, flags);
450
451         return ip_set_enomatch(ret, flags, adt, set) ? -ret :
452                ip_set_eexist(ret, flags) ? 0 : ret;
453 }
454
455 static struct ip_set_type hash_netnet_type __read_mostly = {
456         .name           = "hash:net,net",
457         .protocol       = IPSET_PROTOCOL,
458         .features       = IPSET_TYPE_IP | IPSET_TYPE_IP2 | IPSET_TYPE_NOMATCH,
459         .dimension      = IPSET_DIM_TWO,
460         .family         = NFPROTO_UNSPEC,
461         .revision_min   = IPSET_TYPE_REV_MIN,
462         .revision_max   = IPSET_TYPE_REV_MAX,
463         .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
464         .create         = hash_netnet_create,
465         .create_policy  = {
466                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
467                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
468                 [IPSET_ATTR_INITVAL]    = { .type = NLA_U32 },
469                 [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
470                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
471                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
472                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
473         },
474         .adt_policy     = {
475                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
476                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
477                 [IPSET_ATTR_IP2]        = { .type = NLA_NESTED },
478                 [IPSET_ATTR_IP2_TO]     = { .type = NLA_NESTED },
479                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
480                 [IPSET_ATTR_CIDR2]      = { .type = NLA_U8 },
481                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
482                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
483                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
484                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
485                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
486                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING,
487                                             .len  = IPSET_MAX_COMMENT_SIZE },
488                 [IPSET_ATTR_SKBMARK]    = { .type = NLA_U64 },
489                 [IPSET_ATTR_SKBPRIO]    = { .type = NLA_U32 },
490                 [IPSET_ATTR_SKBQUEUE]   = { .type = NLA_U16 },
491         },
492         .me             = THIS_MODULE,
493 };
494
495 static int __init
496 hash_netnet_init(void)
497 {
498         return ip_set_type_register(&hash_netnet_type);
499 }
500
501 static void __exit
502 hash_netnet_fini(void)
503 {
504         rcu_barrier();
505         ip_set_type_unregister(&hash_netnet_type);
506 }
507
508 module_init(hash_netnet_init);
509 module_exit(hash_netnet_fini);