ALSA: hda - Fix build without CONFIG_PM
[firefly-linux-kernel-4.4.55.git] / net / netfilter / ipset / ip_set_hash_ipportip.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:ip,port,ip 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 #include <net/tcp.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_timeout.h>
25 #include <linux/netfilter/ipset/ip_set_getport.h>
26 #include <linux/netfilter/ipset/ip_set_hash.h>
27
28 #define REVISION_MIN    0
29 #define REVISION_MAX    1 /* SCTP and UDPLITE support added */
30
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
33 IP_SET_MODULE_DESC("hash:ip,port,ip", REVISION_MIN, REVISION_MAX);
34 MODULE_ALIAS("ip_set_hash:ip,port,ip");
35
36 /* Type specific function prefix */
37 #define TYPE            hash_ipportip
38
39 static bool
40 hash_ipportip_same_set(const struct ip_set *a, const struct ip_set *b);
41
42 #define hash_ipportip4_same_set hash_ipportip_same_set
43 #define hash_ipportip6_same_set hash_ipportip_same_set
44
45 /* The type variant functions: IPv4 */
46
47 /* Member elements without timeout */
48 struct hash_ipportip4_elem {
49         __be32 ip;
50         __be32 ip2;
51         __be16 port;
52         u8 proto;
53         u8 padding;
54 };
55
56 /* Member elements with timeout support */
57 struct hash_ipportip4_telem {
58         __be32 ip;
59         __be32 ip2;
60         __be16 port;
61         u8 proto;
62         u8 padding;
63         unsigned long timeout;
64 };
65
66 static inline bool
67 hash_ipportip4_data_equal(const struct hash_ipportip4_elem *ip1,
68                           const struct hash_ipportip4_elem *ip2,
69                           u32 *multi)
70 {
71         return ip1->ip == ip2->ip &&
72                ip1->ip2 == ip2->ip2 &&
73                ip1->port == ip2->port &&
74                ip1->proto == ip2->proto;
75 }
76
77 static inline bool
78 hash_ipportip4_data_isnull(const struct hash_ipportip4_elem *elem)
79 {
80         return elem->proto == 0;
81 }
82
83 static inline void
84 hash_ipportip4_data_copy(struct hash_ipportip4_elem *dst,
85                          const struct hash_ipportip4_elem *src)
86 {
87         memcpy(dst, src, sizeof(*dst));
88 }
89
90 static inline void
91 hash_ipportip4_data_zero_out(struct hash_ipportip4_elem *elem)
92 {
93         elem->proto = 0;
94 }
95
96 static bool
97 hash_ipportip4_data_list(struct sk_buff *skb,
98                        const struct hash_ipportip4_elem *data)
99 {
100         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
101             nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip2) ||
102             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
103             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
104                 goto nla_put_failure;
105         return 0;
106
107 nla_put_failure:
108         return 1;
109 }
110
111 static bool
112 hash_ipportip4_data_tlist(struct sk_buff *skb,
113                         const struct hash_ipportip4_elem *data)
114 {
115         const struct hash_ipportip4_telem *tdata =
116                 (const struct hash_ipportip4_telem *)data;
117
118         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, tdata->ip) ||
119             nla_put_ipaddr4(skb, IPSET_ATTR_IP2, tdata->ip2) ||
120             nla_put_net16(skb, IPSET_ATTR_PORT, tdata->port) ||
121             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
122             nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
123                           htonl(ip_set_timeout_get(tdata->timeout))))
124                 goto nla_put_failure;
125         return 0;
126
127 nla_put_failure:
128         return 1;
129 }
130
131 #define PF              4
132 #define HOST_MASK       32
133 #include <linux/netfilter/ipset/ip_set_ahash.h>
134
135 static inline void
136 hash_ipportip4_data_next(struct ip_set_hash *h,
137                          const struct hash_ipportip4_elem *d)
138 {
139         h->next.ip = d->ip;
140         h->next.port = d->port;
141 }
142
143 static int
144 hash_ipportip4_kadt(struct ip_set *set, const struct sk_buff *skb,
145                     const struct xt_action_param *par,
146                     enum ipset_adt adt, const struct ip_set_adt_opt *opt)
147 {
148         const struct ip_set_hash *h = set->data;
149         ipset_adtfn adtfn = set->variant->adt[adt];
150         struct hash_ipportip4_elem data = { };
151
152         if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
153                                  &data.port, &data.proto))
154                 return -EINVAL;
155
156         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
157         ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &data.ip2);
158
159         return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
160 }
161
162 static int
163 hash_ipportip4_uadt(struct ip_set *set, struct nlattr *tb[],
164                     enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
165 {
166         const struct ip_set_hash *h = set->data;
167         ipset_adtfn adtfn = set->variant->adt[adt];
168         struct hash_ipportip4_elem data = { };
169         u32 ip, ip_to = 0, p = 0, port, port_to;
170         u32 timeout = h->timeout;
171         bool with_ports = false;
172         int ret;
173
174         if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
175                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
176                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
177                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
178                 return -IPSET_ERR_PROTOCOL;
179
180         if (tb[IPSET_ATTR_LINENO])
181                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
182
183         ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
184         if (ret)
185                 return ret;
186
187         ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP2], &data.ip2);
188         if (ret)
189                 return ret;
190
191         if (tb[IPSET_ATTR_PORT])
192                 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
193         else
194                 return -IPSET_ERR_PROTOCOL;
195
196         if (tb[IPSET_ATTR_PROTO]) {
197                 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
198                 with_ports = ip_set_proto_with_ports(data.proto);
199
200                 if (data.proto == 0)
201                         return -IPSET_ERR_INVALID_PROTO;
202         } else
203                 return -IPSET_ERR_MISSING_PROTO;
204
205         if (!(with_ports || data.proto == IPPROTO_ICMP))
206                 data.port = 0;
207
208         if (tb[IPSET_ATTR_TIMEOUT]) {
209                 if (!with_timeout(h->timeout))
210                         return -IPSET_ERR_TIMEOUT;
211                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
212         }
213
214         if (adt == IPSET_TEST ||
215             !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR] ||
216               tb[IPSET_ATTR_PORT_TO])) {
217                 ret = adtfn(set, &data, timeout, flags);
218                 return ip_set_eexist(ret, flags) ? 0 : ret;
219         }
220
221         ip = ntohl(data.ip);
222         if (tb[IPSET_ATTR_IP_TO]) {
223                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
224                 if (ret)
225                         return ret;
226                 if (ip > ip_to)
227                         swap(ip, ip_to);
228         } else if (tb[IPSET_ATTR_CIDR]) {
229                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
230
231                 if (!cidr || cidr > 32)
232                         return -IPSET_ERR_INVALID_CIDR;
233                 ip_set_mask_from_to(ip, ip_to, cidr);
234         } else
235                 ip_to = ip;
236
237         port_to = port = ntohs(data.port);
238         if (with_ports && tb[IPSET_ATTR_PORT_TO]) {
239                 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
240                 if (port > port_to)
241                         swap(port, port_to);
242         }
243
244         if (retried)
245                 ip = ntohl(h->next.ip);
246         for (; !before(ip_to, ip); ip++) {
247                 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
248                                                        : port;
249                 for (; p <= port_to; p++) {
250                         data.ip = htonl(ip);
251                         data.port = htons(p);
252                         ret = adtfn(set, &data, timeout, flags);
253
254                         if (ret && !ip_set_eexist(ret, flags))
255                                 return ret;
256                         else
257                                 ret = 0;
258                 }
259         }
260         return ret;
261 }
262
263 static bool
264 hash_ipportip_same_set(const struct ip_set *a, const struct ip_set *b)
265 {
266         const struct ip_set_hash *x = a->data;
267         const struct ip_set_hash *y = b->data;
268
269         /* Resizing changes htable_bits, so we ignore it */
270         return x->maxelem == y->maxelem &&
271                x->timeout == y->timeout;
272 }
273
274 /* The type variant functions: IPv6 */
275
276 struct hash_ipportip6_elem {
277         union nf_inet_addr ip;
278         union nf_inet_addr ip2;
279         __be16 port;
280         u8 proto;
281         u8 padding;
282 };
283
284 struct hash_ipportip6_telem {
285         union nf_inet_addr ip;
286         union nf_inet_addr ip2;
287         __be16 port;
288         u8 proto;
289         u8 padding;
290         unsigned long timeout;
291 };
292
293 static inline bool
294 hash_ipportip6_data_equal(const struct hash_ipportip6_elem *ip1,
295                           const struct hash_ipportip6_elem *ip2,
296                           u32 *multi)
297 {
298         return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
299                ipv6_addr_cmp(&ip1->ip2.in6, &ip2->ip2.in6) == 0 &&
300                ip1->port == ip2->port &&
301                ip1->proto == ip2->proto;
302 }
303
304 static inline bool
305 hash_ipportip6_data_isnull(const struct hash_ipportip6_elem *elem)
306 {
307         return elem->proto == 0;
308 }
309
310 static inline void
311 hash_ipportip6_data_copy(struct hash_ipportip6_elem *dst,
312                          const struct hash_ipportip6_elem *src)
313 {
314         memcpy(dst, src, sizeof(*dst));
315 }
316
317 static inline void
318 hash_ipportip6_data_zero_out(struct hash_ipportip6_elem *elem)
319 {
320         elem->proto = 0;
321 }
322
323 static bool
324 hash_ipportip6_data_list(struct sk_buff *skb,
325                          const struct hash_ipportip6_elem *data)
326 {
327         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
328             nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
329             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
330             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto))
331                 goto nla_put_failure;
332         return 0;
333
334 nla_put_failure:
335         return 1;
336 }
337
338 static bool
339 hash_ipportip6_data_tlist(struct sk_buff *skb,
340                           const struct hash_ipportip6_elem *data)
341 {
342         const struct hash_ipportip6_telem *e =
343                 (const struct hash_ipportip6_telem *)data;
344
345         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
346             nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
347             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
348             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
349             nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
350                           htonl(ip_set_timeout_get(e->timeout))))
351                 goto nla_put_failure;
352         return 0;
353
354 nla_put_failure:
355         return 1;
356 }
357
358 #undef PF
359 #undef HOST_MASK
360
361 #define PF              6
362 #define HOST_MASK       128
363 #include <linux/netfilter/ipset/ip_set_ahash.h>
364
365 static inline void
366 hash_ipportip6_data_next(struct ip_set_hash *h,
367                          const struct hash_ipportip6_elem *d)
368 {
369         h->next.port = d->port;
370 }
371
372 static int
373 hash_ipportip6_kadt(struct ip_set *set, const struct sk_buff *skb,
374                     const struct xt_action_param *par,
375                     enum ipset_adt adt, const struct ip_set_adt_opt *opt)
376 {
377         const struct ip_set_hash *h = set->data;
378         ipset_adtfn adtfn = set->variant->adt[adt];
379         struct hash_ipportip6_elem data = { };
380
381         if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
382                                  &data.port, &data.proto))
383                 return -EINVAL;
384
385         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
386         ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &data.ip2.in6);
387
388         return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
389 }
390
391 static int
392 hash_ipportip6_uadt(struct ip_set *set, struct nlattr *tb[],
393                     enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
394 {
395         const struct ip_set_hash *h = set->data;
396         ipset_adtfn adtfn = set->variant->adt[adt];
397         struct hash_ipportip6_elem data = { };
398         u32 port, port_to;
399         u32 timeout = h->timeout;
400         bool with_ports = false;
401         int ret;
402
403         if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
404                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
405                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
406                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
407                      tb[IPSET_ATTR_IP_TO] ||
408                      tb[IPSET_ATTR_CIDR]))
409                 return -IPSET_ERR_PROTOCOL;
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], &data.ip);
415         if (ret)
416                 return ret;
417
418         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &data.ip2);
419         if (ret)
420                 return ret;
421
422         if (tb[IPSET_ATTR_PORT])
423                 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
424         else
425                 return -IPSET_ERR_PROTOCOL;
426
427         if (tb[IPSET_ATTR_PROTO]) {
428                 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
429                 with_ports = ip_set_proto_with_ports(data.proto);
430
431                 if (data.proto == 0)
432                         return -IPSET_ERR_INVALID_PROTO;
433         } else
434                 return -IPSET_ERR_MISSING_PROTO;
435
436         if (!(with_ports || data.proto == IPPROTO_ICMPV6))
437                 data.port = 0;
438
439         if (tb[IPSET_ATTR_TIMEOUT]) {
440                 if (!with_timeout(h->timeout))
441                         return -IPSET_ERR_TIMEOUT;
442                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
443         }
444
445         if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
446                 ret = adtfn(set, &data, timeout, flags);
447                 return ip_set_eexist(ret, flags) ? 0 : ret;
448         }
449
450         port = ntohs(data.port);
451         port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
452         if (port > port_to)
453                 swap(port, port_to);
454
455         if (retried)
456                 port = ntohs(h->next.port);
457         for (; port <= port_to; port++) {
458                 data.port = htons(port);
459                 ret = adtfn(set, &data, timeout, flags);
460
461                 if (ret && !ip_set_eexist(ret, flags))
462                         return ret;
463                 else
464                         ret = 0;
465         }
466         return ret;
467 }
468
469 /* Create hash:ip type of sets */
470
471 static int
472 hash_ipportip_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
473 {
474         struct ip_set_hash *h;
475         u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
476         u8 hbits;
477         size_t hsize;
478
479         if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
480                 return -IPSET_ERR_INVALID_FAMILY;
481
482         if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
483                      !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
484                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
485                 return -IPSET_ERR_PROTOCOL;
486
487         if (tb[IPSET_ATTR_HASHSIZE]) {
488                 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
489                 if (hashsize < IPSET_MIMINAL_HASHSIZE)
490                         hashsize = IPSET_MIMINAL_HASHSIZE;
491         }
492
493         if (tb[IPSET_ATTR_MAXELEM])
494                 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
495
496         h = kzalloc(sizeof(*h), GFP_KERNEL);
497         if (!h)
498                 return -ENOMEM;
499
500         h->maxelem = maxelem;
501         get_random_bytes(&h->initval, sizeof(h->initval));
502         h->timeout = IPSET_NO_TIMEOUT;
503
504         hbits = htable_bits(hashsize);
505         hsize = htable_size(hbits);
506         if (hsize == 0) {
507                 kfree(h);
508                 return -ENOMEM;
509         }
510         h->table = ip_set_alloc(hsize);
511         if (!h->table) {
512                 kfree(h);
513                 return -ENOMEM;
514         }
515         h->table->htable_bits = hbits;
516
517         set->data = h;
518
519         if (tb[IPSET_ATTR_TIMEOUT]) {
520                 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
521
522                 set->variant = set->family == NFPROTO_IPV4
523                         ? &hash_ipportip4_tvariant : &hash_ipportip6_tvariant;
524
525                 if (set->family == NFPROTO_IPV4)
526                         hash_ipportip4_gc_init(set);
527                 else
528                         hash_ipportip6_gc_init(set);
529         } else {
530                 set->variant = set->family == NFPROTO_IPV4
531                         ? &hash_ipportip4_variant : &hash_ipportip6_variant;
532         }
533
534         pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
535                  set->name, jhash_size(h->table->htable_bits),
536                  h->table->htable_bits, h->maxelem, set->data, h->table);
537
538         return 0;
539 }
540
541 static struct ip_set_type hash_ipportip_type __read_mostly = {
542         .name           = "hash:ip,port,ip",
543         .protocol       = IPSET_PROTOCOL,
544         .features       = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2,
545         .dimension      = IPSET_DIM_THREE,
546         .family         = NFPROTO_UNSPEC,
547         .revision_min   = REVISION_MIN,
548         .revision_max   = REVISION_MAX,
549         .create         = hash_ipportip_create,
550         .create_policy  = {
551                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
552                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
553                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
554                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
555                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
556         },
557         .adt_policy     = {
558                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
559                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
560                 [IPSET_ATTR_IP2]        = { .type = NLA_NESTED },
561                 [IPSET_ATTR_PORT]       = { .type = NLA_U16 },
562                 [IPSET_ATTR_PORT_TO]    = { .type = NLA_U16 },
563                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
564                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
565                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
566                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
567         },
568         .me             = THIS_MODULE,
569 };
570
571 static int __init
572 hash_ipportip_init(void)
573 {
574         return ip_set_type_register(&hash_ipportip_type);
575 }
576
577 static void __exit
578 hash_ipportip_fini(void)
579 {
580         ip_set_type_unregister(&hash_ipportip_type);
581 }
582
583 module_init(hash_ipportip_init);
584 module_exit(hash_ipportip_fini);