]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/netfilter/ipset/ip_set_hash_ipportnet.c
drivers/net/ethernet/silan: remove depends on CONFIG_EXPERIMENTAL
[karo-tx-linux.git] / net / netfilter / ipset / ip_set_hash_ipportnet.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,net 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 /*                      1    SCTP and UDPLITE support added */
30 /*                      2    Range as input support for IPv4 added */
31 #define REVISION_MAX    3 /* nomatch flag support added */
32
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
35 IP_SET_MODULE_DESC("hash:ip,port,net", REVISION_MIN, REVISION_MAX);
36 MODULE_ALIAS("ip_set_hash:ip,port,net");
37
38 /* Type specific function prefix */
39 #define TYPE            hash_ipportnet
40
41 static bool
42 hash_ipportnet_same_set(const struct ip_set *a, const struct ip_set *b);
43
44 #define hash_ipportnet4_same_set        hash_ipportnet_same_set
45 #define hash_ipportnet6_same_set        hash_ipportnet_same_set
46
47 /* The type variant functions: IPv4 */
48
49 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
50  * However this way we have to store internally cidr - 1,
51  * dancing back and forth.
52  */
53 #define IP_SET_HASH_WITH_NETS_PACKED
54
55 /* Member elements without timeout */
56 struct hash_ipportnet4_elem {
57         __be32 ip;
58         __be32 ip2;
59         __be16 port;
60         u8 cidr:7;
61         u8 nomatch:1;
62         u8 proto;
63 };
64
65 /* Member elements with timeout support */
66 struct hash_ipportnet4_telem {
67         __be32 ip;
68         __be32 ip2;
69         __be16 port;
70         u8 cidr:7;
71         u8 nomatch:1;
72         u8 proto;
73         unsigned long timeout;
74 };
75
76 static inline bool
77 hash_ipportnet4_data_equal(const struct hash_ipportnet4_elem *ip1,
78                            const struct hash_ipportnet4_elem *ip2,
79                            u32 *multi)
80 {
81         return ip1->ip == ip2->ip &&
82                ip1->ip2 == ip2->ip2 &&
83                ip1->cidr == ip2->cidr &&
84                ip1->port == ip2->port &&
85                ip1->proto == ip2->proto;
86 }
87
88 static inline bool
89 hash_ipportnet4_data_isnull(const struct hash_ipportnet4_elem *elem)
90 {
91         return elem->proto == 0;
92 }
93
94 static inline void
95 hash_ipportnet4_data_copy(struct hash_ipportnet4_elem *dst,
96                           const struct hash_ipportnet4_elem *src)
97 {
98         memcpy(dst, src, sizeof(*dst));
99 }
100
101 static inline void
102 hash_ipportnet4_data_flags(struct hash_ipportnet4_elem *dst, u32 flags)
103 {
104         dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH);
105 }
106
107 static inline int
108 hash_ipportnet4_data_match(const struct hash_ipportnet4_elem *elem)
109 {
110         return elem->nomatch ? -ENOTEMPTY : 1;
111 }
112
113 static inline void
114 hash_ipportnet4_data_netmask(struct hash_ipportnet4_elem *elem, u8 cidr)
115 {
116         elem->ip2 &= ip_set_netmask(cidr);
117         elem->cidr = cidr - 1;
118 }
119
120 static inline void
121 hash_ipportnet4_data_zero_out(struct hash_ipportnet4_elem *elem)
122 {
123         elem->proto = 0;
124 }
125
126 static bool
127 hash_ipportnet4_data_list(struct sk_buff *skb,
128                           const struct hash_ipportnet4_elem *data)
129 {
130         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
131
132         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
133             nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip2) ||
134             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
135             nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
136             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
137             (flags &&
138              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
139                 goto nla_put_failure;
140         return 0;
141
142 nla_put_failure:
143         return 1;
144 }
145
146 static bool
147 hash_ipportnet4_data_tlist(struct sk_buff *skb,
148                            const struct hash_ipportnet4_elem *data)
149 {
150         const struct hash_ipportnet4_telem *tdata =
151                 (const struct hash_ipportnet4_telem *)data;
152         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
153
154         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, tdata->ip) ||
155             nla_put_ipaddr4(skb, IPSET_ATTR_IP2, tdata->ip2) ||
156             nla_put_net16(skb, IPSET_ATTR_PORT, tdata->port) ||
157             nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
158             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
159             nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
160                           htonl(ip_set_timeout_get(tdata->timeout))) ||
161             (flags &&
162              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
163                 goto nla_put_failure;
164         return 0;
165
166 nla_put_failure:
167         return 1;
168 }
169
170 #define IP_SET_HASH_WITH_PROTO
171 #define IP_SET_HASH_WITH_NETS
172
173 #define PF              4
174 #define HOST_MASK       32
175 #include <linux/netfilter/ipset/ip_set_ahash.h>
176
177 static inline void
178 hash_ipportnet4_data_next(struct ip_set_hash *h,
179                           const struct hash_ipportnet4_elem *d)
180 {
181         h->next.ip = d->ip;
182         h->next.port = d->port;
183         h->next.ip2 = d->ip2;
184 }
185
186 static int
187 hash_ipportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
188                      const struct xt_action_param *par,
189                      enum ipset_adt adt, const struct ip_set_adt_opt *opt)
190 {
191         const struct ip_set_hash *h = set->data;
192         ipset_adtfn adtfn = set->variant->adt[adt];
193         struct hash_ipportnet4_elem data = {
194                 .cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 : HOST_MASK - 1
195         };
196
197         if (adt == IPSET_TEST)
198                 data.cidr = HOST_MASK - 1;
199
200         if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
201                                  &data.port, &data.proto))
202                 return -EINVAL;
203
204         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
205         ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &data.ip2);
206         data.ip2 &= ip_set_netmask(data.cidr + 1);
207
208         return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
209 }
210
211 static int
212 hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
213                      enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
214 {
215         const struct ip_set_hash *h = set->data;
216         ipset_adtfn adtfn = set->variant->adt[adt];
217         struct hash_ipportnet4_elem data = { .cidr = HOST_MASK - 1 };
218         u32 ip, ip_to = 0, p = 0, port, port_to;
219         u32 ip2_from = 0, ip2_to, ip2_last, ip2;
220         u32 timeout = h->timeout;
221         bool with_ports = false;
222         u8 cidr;
223         int ret;
224
225         if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
226                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
227                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
228                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
229                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
230                 return -IPSET_ERR_PROTOCOL;
231
232         if (tb[IPSET_ATTR_LINENO])
233                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
234
235         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
236         if (ret)
237                 return ret;
238
239         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
240         if (ret)
241                 return ret;
242
243         if (tb[IPSET_ATTR_CIDR2]) {
244                 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
245                 if (!cidr || cidr > HOST_MASK)
246                         return -IPSET_ERR_INVALID_CIDR;
247                 data.cidr = cidr - 1;
248         }
249
250         if (tb[IPSET_ATTR_PORT])
251                 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
252         else
253                 return -IPSET_ERR_PROTOCOL;
254
255         if (tb[IPSET_ATTR_PROTO]) {
256                 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
257                 with_ports = ip_set_proto_with_ports(data.proto);
258
259                 if (data.proto == 0)
260                         return -IPSET_ERR_INVALID_PROTO;
261         } else
262                 return -IPSET_ERR_MISSING_PROTO;
263
264         if (!(with_ports || data.proto == IPPROTO_ICMP))
265                 data.port = 0;
266
267         if (tb[IPSET_ATTR_TIMEOUT]) {
268                 if (!with_timeout(h->timeout))
269                         return -IPSET_ERR_TIMEOUT;
270                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
271         }
272
273         if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
274                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
275                 if (cadt_flags & IPSET_FLAG_NOMATCH)
276                         flags |= (cadt_flags << 16);
277         }
278
279         with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
280         if (adt == IPSET_TEST ||
281             !(tb[IPSET_ATTR_CIDR] || tb[IPSET_ATTR_IP_TO] || with_ports ||
282               tb[IPSET_ATTR_IP2_TO])) {
283                 data.ip = htonl(ip);
284                 data.ip2 = htonl(ip2_from & ip_set_hostmask(data.cidr + 1));
285                 ret = adtfn(set, &data, timeout, flags);
286                 return ip_set_eexist(ret, flags) ? 0 : ret;
287         }
288
289         if (tb[IPSET_ATTR_IP_TO]) {
290                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
291                 if (ret)
292                         return ret;
293                 if (ip > ip_to)
294                         swap(ip, ip_to);
295         } else if (tb[IPSET_ATTR_CIDR]) {
296                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
297
298                 if (!cidr || cidr > 32)
299                         return -IPSET_ERR_INVALID_CIDR;
300                 ip_set_mask_from_to(ip, ip_to, cidr);
301         }
302
303         port_to = port = ntohs(data.port);
304         if (tb[IPSET_ATTR_PORT_TO]) {
305                 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
306                 if (port > port_to)
307                         swap(port, port_to);
308         }
309         if (tb[IPSET_ATTR_IP2_TO]) {
310                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
311                 if (ret)
312                         return ret;
313                 if (ip2_from > ip2_to)
314                         swap(ip2_from, ip2_to);
315                 if (ip2_from + UINT_MAX == ip2_to)
316                         return -IPSET_ERR_HASH_RANGE;
317         } else {
318                 ip_set_mask_from_to(ip2_from, ip2_to, data.cidr + 1);
319         }
320
321         if (retried)
322                 ip = ntohl(h->next.ip);
323         for (; !before(ip_to, ip); ip++) {
324                 data.ip = htonl(ip);
325                 p = retried && ip == ntohl(h->next.ip) ? ntohs(h->next.port)
326                                                        : port;
327                 for (; p <= port_to; p++) {
328                         data.port = htons(p);
329                         ip2 = retried
330                               && ip == ntohl(h->next.ip)
331                               && p == ntohs(h->next.port)
332                                 ? ntohl(h->next.ip2) : ip2_from;
333                         while (!after(ip2, ip2_to)) {
334                                 data.ip2 = htonl(ip2);
335                                 ip2_last = ip_set_range_to_cidr(ip2, ip2_to,
336                                                                 &cidr);
337                                 data.cidr = cidr - 1;
338                                 ret = adtfn(set, &data, timeout, flags);
339
340                                 if (ret && !ip_set_eexist(ret, flags))
341                                         return ret;
342                                 else
343                                         ret = 0;
344                                 ip2 = ip2_last + 1;
345                         }
346                 }
347         }
348         return ret;
349 }
350
351 static bool
352 hash_ipportnet_same_set(const struct ip_set *a, const struct ip_set *b)
353 {
354         const struct ip_set_hash *x = a->data;
355         const struct ip_set_hash *y = b->data;
356
357         /* Resizing changes htable_bits, so we ignore it */
358         return x->maxelem == y->maxelem &&
359                x->timeout == y->timeout;
360 }
361
362 /* The type variant functions: IPv6 */
363
364 struct hash_ipportnet6_elem {
365         union nf_inet_addr ip;
366         union nf_inet_addr ip2;
367         __be16 port;
368         u8 cidr:7;
369         u8 nomatch:1;
370         u8 proto;
371 };
372
373 struct hash_ipportnet6_telem {
374         union nf_inet_addr ip;
375         union nf_inet_addr ip2;
376         __be16 port;
377         u8 cidr:7;
378         u8 nomatch:1;
379         u8 proto;
380         unsigned long timeout;
381 };
382
383 static inline bool
384 hash_ipportnet6_data_equal(const struct hash_ipportnet6_elem *ip1,
385                            const struct hash_ipportnet6_elem *ip2,
386                            u32 *multi)
387 {
388         return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
389                ipv6_addr_cmp(&ip1->ip2.in6, &ip2->ip2.in6) == 0 &&
390                ip1->cidr == ip2->cidr &&
391                ip1->port == ip2->port &&
392                ip1->proto == ip2->proto;
393 }
394
395 static inline bool
396 hash_ipportnet6_data_isnull(const struct hash_ipportnet6_elem *elem)
397 {
398         return elem->proto == 0;
399 }
400
401 static inline void
402 hash_ipportnet6_data_copy(struct hash_ipportnet6_elem *dst,
403                           const struct hash_ipportnet6_elem *src)
404 {
405         memcpy(dst, src, sizeof(*dst));
406 }
407
408 static inline void
409 hash_ipportnet6_data_flags(struct hash_ipportnet6_elem *dst, u32 flags)
410 {
411         dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH);
412 }
413
414 static inline int
415 hash_ipportnet6_data_match(const struct hash_ipportnet6_elem *elem)
416 {
417         return elem->nomatch ? -ENOTEMPTY : 1;
418 }
419
420 static inline void
421 hash_ipportnet6_data_zero_out(struct hash_ipportnet6_elem *elem)
422 {
423         elem->proto = 0;
424 }
425
426 static inline void
427 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
428 {
429         ip->ip6[0] &= ip_set_netmask6(prefix)[0];
430         ip->ip6[1] &= ip_set_netmask6(prefix)[1];
431         ip->ip6[2] &= ip_set_netmask6(prefix)[2];
432         ip->ip6[3] &= ip_set_netmask6(prefix)[3];
433 }
434
435 static inline void
436 hash_ipportnet6_data_netmask(struct hash_ipportnet6_elem *elem, u8 cidr)
437 {
438         ip6_netmask(&elem->ip2, cidr);
439         elem->cidr = cidr - 1;
440 }
441
442 static bool
443 hash_ipportnet6_data_list(struct sk_buff *skb,
444                           const struct hash_ipportnet6_elem *data)
445 {
446         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
447
448         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
449             nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
450             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
451             nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
452             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
453             (flags &&
454              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
455                 goto nla_put_failure;
456         return 0;
457
458 nla_put_failure:
459         return 1;
460 }
461
462 static bool
463 hash_ipportnet6_data_tlist(struct sk_buff *skb,
464                            const struct hash_ipportnet6_elem *data)
465 {
466         const struct hash_ipportnet6_telem *e =
467                 (const struct hash_ipportnet6_telem *)data;
468         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
469
470         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
471             nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
472             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
473             nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
474             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
475             nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
476                           htonl(ip_set_timeout_get(e->timeout))) ||
477             (flags &&
478              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
479                 goto nla_put_failure;
480         return 0;
481
482 nla_put_failure:
483         return 1;
484 }
485
486 #undef PF
487 #undef HOST_MASK
488
489 #define PF              6
490 #define HOST_MASK       128
491 #include <linux/netfilter/ipset/ip_set_ahash.h>
492
493 static inline void
494 hash_ipportnet6_data_next(struct ip_set_hash *h,
495                           const struct hash_ipportnet6_elem *d)
496 {
497         h->next.port = d->port;
498 }
499
500 static int
501 hash_ipportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
502                      const struct xt_action_param *par,
503                      enum ipset_adt adt, const struct ip_set_adt_opt *opt)
504 {
505         const struct ip_set_hash *h = set->data;
506         ipset_adtfn adtfn = set->variant->adt[adt];
507         struct hash_ipportnet6_elem data = {
508                 .cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 : HOST_MASK - 1
509         };
510
511         if (adt == IPSET_TEST)
512                 data.cidr = HOST_MASK - 1;
513
514         if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
515                                  &data.port, &data.proto))
516                 return -EINVAL;
517
518         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
519         ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &data.ip2.in6);
520         ip6_netmask(&data.ip2, data.cidr + 1);
521
522         return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
523 }
524
525 static int
526 hash_ipportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
527                      enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
528 {
529         const struct ip_set_hash *h = set->data;
530         ipset_adtfn adtfn = set->variant->adt[adt];
531         struct hash_ipportnet6_elem data = { .cidr = HOST_MASK - 1 };
532         u32 port, port_to;
533         u32 timeout = h->timeout;
534         bool with_ports = false;
535         u8 cidr;
536         int ret;
537
538         if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
539                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
540                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
541                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
542                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
543                      tb[IPSET_ATTR_IP_TO] ||
544                      tb[IPSET_ATTR_CIDR]))
545                 return -IPSET_ERR_PROTOCOL;
546         if (unlikely(tb[IPSET_ATTR_IP_TO]))
547                 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
548
549         if (tb[IPSET_ATTR_LINENO])
550                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
551
552         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
553         if (ret)
554                 return ret;
555
556         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &data.ip2);
557         if (ret)
558                 return ret;
559
560         if (tb[IPSET_ATTR_CIDR2]) {
561                 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
562                 if (!cidr || cidr > HOST_MASK)
563                         return -IPSET_ERR_INVALID_CIDR;
564                 data.cidr = cidr - 1;
565         }
566
567         ip6_netmask(&data.ip2, data.cidr + 1);
568
569         if (tb[IPSET_ATTR_PORT])
570                 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
571         else
572                 return -IPSET_ERR_PROTOCOL;
573
574         if (tb[IPSET_ATTR_PROTO]) {
575                 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
576                 with_ports = ip_set_proto_with_ports(data.proto);
577
578                 if (data.proto == 0)
579                         return -IPSET_ERR_INVALID_PROTO;
580         } else
581                 return -IPSET_ERR_MISSING_PROTO;
582
583         if (!(with_ports || data.proto == IPPROTO_ICMPV6))
584                 data.port = 0;
585
586         if (tb[IPSET_ATTR_TIMEOUT]) {
587                 if (!with_timeout(h->timeout))
588                         return -IPSET_ERR_TIMEOUT;
589                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
590         }
591
592         if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
593                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
594                 if (cadt_flags & IPSET_FLAG_NOMATCH)
595                         flags |= (cadt_flags << 16);
596         }
597
598         if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
599                 ret = adtfn(set, &data, timeout, flags);
600                 return ip_set_eexist(ret, flags) ? 0 : ret;
601         }
602
603         port = ntohs(data.port);
604         port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
605         if (port > port_to)
606                 swap(port, port_to);
607
608         if (retried)
609                 port = ntohs(h->next.port);
610         for (; port <= port_to; port++) {
611                 data.port = htons(port);
612                 ret = adtfn(set, &data, timeout, flags);
613
614                 if (ret && !ip_set_eexist(ret, flags))
615                         return ret;
616                 else
617                         ret = 0;
618         }
619         return ret;
620 }
621
622 /* Create hash:ip type of sets */
623
624 static int
625 hash_ipportnet_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
626 {
627         struct ip_set_hash *h;
628         u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
629         u8 hbits;
630         size_t hsize;
631
632         if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
633                 return -IPSET_ERR_INVALID_FAMILY;
634
635         if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
636                      !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
637                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
638                 return -IPSET_ERR_PROTOCOL;
639
640         if (tb[IPSET_ATTR_HASHSIZE]) {
641                 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
642                 if (hashsize < IPSET_MIMINAL_HASHSIZE)
643                         hashsize = IPSET_MIMINAL_HASHSIZE;
644         }
645
646         if (tb[IPSET_ATTR_MAXELEM])
647                 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
648
649         h = kzalloc(sizeof(*h)
650                     + sizeof(struct ip_set_hash_nets)
651                       * (set->family == NFPROTO_IPV4 ? 32 : 128), GFP_KERNEL);
652         if (!h)
653                 return -ENOMEM;
654
655         h->maxelem = maxelem;
656         get_random_bytes(&h->initval, sizeof(h->initval));
657         h->timeout = IPSET_NO_TIMEOUT;
658
659         hbits = htable_bits(hashsize);
660         hsize = htable_size(hbits);
661         if (hsize == 0) {
662                 kfree(h);
663                 return -ENOMEM;
664         }
665         h->table = ip_set_alloc(hsize);
666         if (!h->table) {
667                 kfree(h);
668                 return -ENOMEM;
669         }
670         h->table->htable_bits = hbits;
671
672         set->data = h;
673
674         if (tb[IPSET_ATTR_TIMEOUT]) {
675                 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
676
677                 set->variant = set->family == NFPROTO_IPV4
678                         ? &hash_ipportnet4_tvariant
679                         : &hash_ipportnet6_tvariant;
680
681                 if (set->family == NFPROTO_IPV4)
682                         hash_ipportnet4_gc_init(set);
683                 else
684                         hash_ipportnet6_gc_init(set);
685         } else {
686                 set->variant = set->family == NFPROTO_IPV4
687                         ? &hash_ipportnet4_variant : &hash_ipportnet6_variant;
688         }
689
690         pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
691                  set->name, jhash_size(h->table->htable_bits),
692                  h->table->htable_bits, h->maxelem, set->data, h->table);
693
694         return 0;
695 }
696
697 static struct ip_set_type hash_ipportnet_type __read_mostly = {
698         .name           = "hash:ip,port,net",
699         .protocol       = IPSET_PROTOCOL,
700         .features       = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
701                           IPSET_TYPE_NOMATCH,
702         .dimension      = IPSET_DIM_THREE,
703         .family         = NFPROTO_UNSPEC,
704         .revision_min   = REVISION_MIN,
705         .revision_max   = REVISION_MAX,
706         .create         = hash_ipportnet_create,
707         .create_policy  = {
708                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
709                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
710                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
711                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
712                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
713         },
714         .adt_policy     = {
715                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
716                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
717                 [IPSET_ATTR_IP2]        = { .type = NLA_NESTED },
718                 [IPSET_ATTR_IP2_TO]     = { .type = NLA_NESTED },
719                 [IPSET_ATTR_PORT]       = { .type = NLA_U16 },
720                 [IPSET_ATTR_PORT_TO]    = { .type = NLA_U16 },
721                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
722                 [IPSET_ATTR_CIDR2]      = { .type = NLA_U8 },
723                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
724                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
725                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
726                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
727         },
728         .me             = THIS_MODULE,
729 };
730
731 static int __init
732 hash_ipportnet_init(void)
733 {
734         return ip_set_type_register(&hash_ipportnet_type);
735 }
736
737 static void __exit
738 hash_ipportnet_fini(void)
739 {
740         ip_set_type_unregister(&hash_ipportnet_type);
741 }
742
743 module_init(hash_ipportnet_init);
744 module_exit(hash_ipportnet_fini);