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