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