Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / net / netfilter / ipset / ip_set_hash_netport.c
1 /* Copyright (C) 2003-2013 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,port 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_getport.h>
24 #include <linux/netfilter/ipset/ip_set_hash.h>
25
26 #define IPSET_TYPE_REV_MIN      0
27 /*                              1    SCTP and UDPLITE support added */
28 /*                              2    Range as input support for IPv4 added */
29 /*                              3    nomatch flag support added */
30 /*                              4    Counters support added */
31 /*                              5    Comments support added */
32 /*                              6    Forceadd support added */
33 #define IPSET_TYPE_REV_MAX      7 /* skbinfo support added */
34
35 MODULE_LICENSE("GPL");
36 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
37 IP_SET_MODULE_DESC("hash:net,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
38 MODULE_ALIAS("ip_set_hash:net,port");
39
40 /* Type specific function prefix */
41 #define HTYPE           hash_netport
42 #define IP_SET_HASH_WITH_PROTO
43 #define IP_SET_HASH_WITH_NETS
44
45 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
46  * However this way we have to store internally cidr - 1,
47  * dancing back and forth.
48  */
49 #define IP_SET_HASH_WITH_NETS_PACKED
50
51 /* IPv4 variant */
52
53 /* Member elements */
54 struct hash_netport4_elem {
55         __be32 ip;
56         __be16 port;
57         u8 proto;
58         u8 cidr:7;
59         u8 nomatch:1;
60 };
61
62 /* Common functions */
63
64 static inline bool
65 hash_netport4_data_equal(const struct hash_netport4_elem *ip1,
66                          const struct hash_netport4_elem *ip2,
67                          u32 *multi)
68 {
69         return ip1->ip == ip2->ip &&
70                ip1->port == ip2->port &&
71                ip1->proto == ip2->proto &&
72                ip1->cidr == ip2->cidr;
73 }
74
75 static inline int
76 hash_netport4_do_data_match(const struct hash_netport4_elem *elem)
77 {
78         return elem->nomatch ? -ENOTEMPTY : 1;
79 }
80
81 static inline void
82 hash_netport4_data_set_flags(struct hash_netport4_elem *elem, u32 flags)
83 {
84         elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
85 }
86
87 static inline void
88 hash_netport4_data_reset_flags(struct hash_netport4_elem *elem, u8 *flags)
89 {
90         swap(*flags, elem->nomatch);
91 }
92
93 static inline void
94 hash_netport4_data_netmask(struct hash_netport4_elem *elem, u8 cidr)
95 {
96         elem->ip &= ip_set_netmask(cidr);
97         elem->cidr = cidr - 1;
98 }
99
100 static bool
101 hash_netport4_data_list(struct sk_buff *skb,
102                         const struct hash_netport4_elem *data)
103 {
104         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
105
106         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
107             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
108             nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
109             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
110             (flags &&
111              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
112                 goto nla_put_failure;
113         return 0;
114
115 nla_put_failure:
116         return 1;
117 }
118
119 static inline void
120 hash_netport4_data_next(struct hash_netport4_elem *next,
121                         const struct hash_netport4_elem *d)
122 {
123         next->ip = d->ip;
124         next->port = d->port;
125 }
126
127 #define MTYPE           hash_netport4
128 #define PF              4
129 #define HOST_MASK       32
130 #include "ip_set_hash_gen.h"
131
132 static int
133 hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb,
134                    const struct xt_action_param *par,
135                    enum ipset_adt adt, struct ip_set_adt_opt *opt)
136 {
137         const struct hash_netport *h = set->data;
138         ipset_adtfn adtfn = set->variant->adt[adt];
139         struct hash_netport4_elem e = {
140                 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1,
141         };
142         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
143
144         if (adt == IPSET_TEST)
145                 e.cidr = HOST_MASK - 1;
146
147         if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
148                                  &e.port, &e.proto))
149                 return -EINVAL;
150
151         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
152         e.ip &= ip_set_netmask(e.cidr + 1);
153
154         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
155 }
156
157 static int
158 hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
159                    enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
160 {
161         const struct hash_netport *h = set->data;
162         ipset_adtfn adtfn = set->variant->adt[adt];
163         struct hash_netport4_elem e = { .cidr = HOST_MASK - 1 };
164         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
165         u32 port, port_to, p = 0, ip = 0, ip_to = 0, last;
166         bool with_ports = false;
167         u8 cidr;
168         int ret;
169
170         if (unlikely(!tb[IPSET_ATTR_IP] ||
171                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
172                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
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_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 = cidr - 1;
195         }
196
197         if (tb[IPSET_ATTR_PORT])
198                 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
199         else
200                 return -IPSET_ERR_PROTOCOL;
201
202         if (tb[IPSET_ATTR_PROTO]) {
203                 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
204                 with_ports = ip_set_proto_with_ports(e.proto);
205
206                 if (e.proto == 0)
207                         return -IPSET_ERR_INVALID_PROTO;
208         } else
209                 return -IPSET_ERR_MISSING_PROTO;
210
211         if (!(with_ports || e.proto == IPPROTO_ICMP))
212                 e.port = 0;
213
214         with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
215
216         if (tb[IPSET_ATTR_CADT_FLAGS]) {
217                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
218                 if (cadt_flags & IPSET_FLAG_NOMATCH)
219                         flags |= (IPSET_FLAG_NOMATCH << 16);
220         }
221
222         if (adt == IPSET_TEST || !(with_ports || tb[IPSET_ATTR_IP_TO])) {
223                 e.ip = htonl(ip & ip_set_hostmask(e.cidr + 1));
224                 ret = adtfn(set, &e, &ext, &ext, flags);
225                 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
226                        ip_set_eexist(ret, flags) ? 0 : ret;
227         }
228
229         port = port_to = ntohs(e.port);
230         if (tb[IPSET_ATTR_PORT_TO]) {
231                 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
232                 if (port_to < port)
233                         swap(port, port_to);
234         }
235         if (tb[IPSET_ATTR_IP_TO]) {
236                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
237                 if (ret)
238                         return ret;
239                 if (ip_to < ip)
240                         swap(ip, ip_to);
241                 if (ip + UINT_MAX == ip_to)
242                         return -IPSET_ERR_HASH_RANGE;
243         } else
244                 ip_set_mask_from_to(ip, ip_to, e.cidr + 1);
245
246         if (retried)
247                 ip = ntohl(h->next.ip);
248         while (!after(ip, ip_to)) {
249                 e.ip = htonl(ip);
250                 last = ip_set_range_to_cidr(ip, ip_to, &cidr);
251                 e.cidr = cidr - 1;
252                 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
253                                                        : port;
254                 for (; p <= port_to; p++) {
255                         e.port = htons(p);
256                         ret = adtfn(set, &e, &ext, &ext, flags);
257
258                         if (ret && !ip_set_eexist(ret, flags))
259                                 return ret;
260                         else
261                                 ret = 0;
262                 }
263                 ip = last + 1;
264         }
265         return ret;
266 }
267
268 /* IPv6 variant */
269
270 struct hash_netport6_elem {
271         union nf_inet_addr ip;
272         __be16 port;
273         u8 proto;
274         u8 cidr:7;
275         u8 nomatch:1;
276 };
277
278 /* Common functions */
279
280 static inline bool
281 hash_netport6_data_equal(const struct hash_netport6_elem *ip1,
282                          const struct hash_netport6_elem *ip2,
283                          u32 *multi)
284 {
285         return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
286                ip1->port == ip2->port &&
287                ip1->proto == ip2->proto &&
288                ip1->cidr == ip2->cidr;
289 }
290
291 static inline int
292 hash_netport6_do_data_match(const struct hash_netport6_elem *elem)
293 {
294         return elem->nomatch ? -ENOTEMPTY : 1;
295 }
296
297 static inline void
298 hash_netport6_data_set_flags(struct hash_netport6_elem *elem, u32 flags)
299 {
300         elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
301 }
302
303 static inline void
304 hash_netport6_data_reset_flags(struct hash_netport6_elem *elem, u8 *flags)
305 {
306         swap(*flags, elem->nomatch);
307 }
308
309 static inline void
310 hash_netport6_data_netmask(struct hash_netport6_elem *elem, u8 cidr)
311 {
312         ip6_netmask(&elem->ip, cidr);
313         elem->cidr = cidr - 1;
314 }
315
316 static bool
317 hash_netport6_data_list(struct sk_buff *skb,
318                         const struct hash_netport6_elem *data)
319 {
320         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
321
322         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
323             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
324             nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
325             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
326             (flags &&
327              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
328                 goto nla_put_failure;
329         return 0;
330
331 nla_put_failure:
332         return 1;
333 }
334
335 static inline void
336 hash_netport6_data_next(struct hash_netport4_elem *next,
337                         const struct hash_netport6_elem *d)
338 {
339         next->port = d->port;
340 }
341
342 #undef MTYPE
343 #undef PF
344 #undef HOST_MASK
345
346 #define MTYPE           hash_netport6
347 #define PF              6
348 #define HOST_MASK       128
349 #define IP_SET_EMIT_CREATE
350 #include "ip_set_hash_gen.h"
351
352 static int
353 hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb,
354                    const struct xt_action_param *par,
355                    enum ipset_adt adt, struct ip_set_adt_opt *opt)
356 {
357         const struct hash_netport *h = set->data;
358         ipset_adtfn adtfn = set->variant->adt[adt];
359         struct hash_netport6_elem e = {
360                 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1,
361         };
362         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
363
364         if (adt == IPSET_TEST)
365                 e.cidr = HOST_MASK - 1;
366
367         if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
368                                  &e.port, &e.proto))
369                 return -EINVAL;
370
371         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
372         ip6_netmask(&e.ip, e.cidr + 1);
373
374         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
375 }
376
377 static int
378 hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[],
379                    enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
380 {
381         const struct hash_netport *h = set->data;
382         ipset_adtfn adtfn = set->variant->adt[adt];
383         struct hash_netport6_elem e = { .cidr = HOST_MASK  - 1 };
384         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
385         u32 port, port_to;
386         bool with_ports = false;
387         u8 cidr;
388         int ret;
389
390         if (unlikely(!tb[IPSET_ATTR_IP] ||
391                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
392                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
393                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
394                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
395                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
396                      !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
397                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
398                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
399                      !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
400                 return -IPSET_ERR_PROTOCOL;
401         if (unlikely(tb[IPSET_ATTR_IP_TO]))
402                 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
403
404         if (tb[IPSET_ATTR_LINENO])
405                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
406
407         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip) ||
408               ip_set_get_extensions(set, tb, &ext);
409         if (ret)
410                 return ret;
411
412         if (tb[IPSET_ATTR_CIDR]) {
413                 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
414                 if (!cidr || cidr > HOST_MASK)
415                         return -IPSET_ERR_INVALID_CIDR;
416                 e.cidr = cidr - 1;
417         }
418         ip6_netmask(&e.ip, e.cidr + 1);
419
420         if (tb[IPSET_ATTR_PORT])
421                 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
422         else
423                 return -IPSET_ERR_PROTOCOL;
424
425         if (tb[IPSET_ATTR_PROTO]) {
426                 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
427                 with_ports = ip_set_proto_with_ports(e.proto);
428
429                 if (e.proto == 0)
430                         return -IPSET_ERR_INVALID_PROTO;
431         } else
432                 return -IPSET_ERR_MISSING_PROTO;
433
434         if (!(with_ports || e.proto == IPPROTO_ICMPV6))
435                 e.port = 0;
436
437         if (tb[IPSET_ATTR_CADT_FLAGS]) {
438                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
439                 if (cadt_flags & IPSET_FLAG_NOMATCH)
440                         flags |= (IPSET_FLAG_NOMATCH << 16);
441         }
442
443         if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
444                 ret = adtfn(set, &e, &ext, &ext, flags);
445                 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
446                        ip_set_eexist(ret, flags) ? 0 : ret;
447         }
448
449         port = ntohs(e.port);
450         port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
451         if (port > port_to)
452                 swap(port, port_to);
453
454         if (retried)
455                 port = ntohs(h->next.port);
456         for (; port <= port_to; port++) {
457                 e.port = htons(port);
458                 ret = adtfn(set, &e, &ext, &ext, flags);
459
460                 if (ret && !ip_set_eexist(ret, flags))
461                         return ret;
462                 else
463                         ret = 0;
464         }
465         return ret;
466 }
467
468 static struct ip_set_type hash_netport_type __read_mostly = {
469         .name           = "hash:net,port",
470         .protocol       = IPSET_PROTOCOL,
471         .features       = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_NOMATCH,
472         .dimension      = IPSET_DIM_TWO,
473         .family         = NFPROTO_UNSPEC,
474         .revision_min   = IPSET_TYPE_REV_MIN,
475         .revision_max   = IPSET_TYPE_REV_MAX,
476         .create         = hash_netport_create,
477         .create_policy  = {
478                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
479                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
480                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
481                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
482                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
483                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
484                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
485         },
486         .adt_policy     = {
487                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
488                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
489                 [IPSET_ATTR_PORT]       = { .type = NLA_U16 },
490                 [IPSET_ATTR_PORT_TO]    = { .type = NLA_U16 },
491                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
492                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
493                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
494                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
495                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
496                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
497                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
498                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING },
499                 [IPSET_ATTR_SKBMARK]    = { .type = NLA_U64 },
500                 [IPSET_ATTR_SKBPRIO]    = { .type = NLA_U32 },
501                 [IPSET_ATTR_SKBQUEUE]   = { .type = NLA_U16 },
502         },
503         .me             = THIS_MODULE,
504 };
505
506 static int __init
507 hash_netport_init(void)
508 {
509         return ip_set_type_register(&hash_netport_type);
510 }
511
512 static void __exit
513 hash_netport_fini(void)
514 {
515         ip_set_type_unregister(&hash_netport_type);
516 }
517
518 module_init(hash_netport_init);
519 module_exit(hash_netport_fini);