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