1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org> */
4 /* Kernel module implementing an IP set type: the hash:ip,port,net type */
6 #include <linux/jhash.h>
7 #include <linux/module.h>
9 #include <linux/skbuff.h>
10 #include <linux/errno.h>
11 #include <linux/random.h>
14 #include <net/netlink.h>
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/ipset/pfxlen.h>
19 #include <linux/netfilter/ipset/ip_set.h>
20 #include <linux/netfilter/ipset/ip_set_getport.h>
21 #include <linux/netfilter/ipset/ip_set_hash.h>
23 #define IPSET_TYPE_REV_MIN 0
24 /* 0 Comments support added */
25 /* 1 Forceadd support added */
26 /* 2 skbinfo support added */
27 #define IPSET_TYPE_REV_MAX 3 /* bucketsize, initval support added */
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
31 IP_SET_MODULE_DESC("hash:net,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
32 MODULE_ALIAS("ip_set_hash:net,port,net");
34 /* Type specific function prefix */
35 #define HTYPE hash_netportnet
36 #define IP_SET_HASH_WITH_PROTO
37 #define IP_SET_HASH_WITH_NETS
38 #define IPSET_NET_COUNT 2
39 #define IP_SET_HASH_WITH_NET0
44 struct hash_netportnet4_elem {
59 /* Common functions */
62 hash_netportnet4_data_equal(const struct hash_netportnet4_elem *ip1,
63 const struct hash_netportnet4_elem *ip2,
66 return ip1->ipcmp == ip2->ipcmp &&
67 ip1->ccmp == ip2->ccmp &&
68 ip1->port == ip2->port &&
69 ip1->proto == ip2->proto;
73 hash_netportnet4_do_data_match(const struct hash_netportnet4_elem *elem)
75 return elem->nomatch ? -ENOTEMPTY : 1;
79 hash_netportnet4_data_set_flags(struct hash_netportnet4_elem *elem, u32 flags)
81 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
85 hash_netportnet4_data_reset_flags(struct hash_netportnet4_elem *elem, u8 *flags)
87 swap(*flags, elem->nomatch);
91 hash_netportnet4_data_reset_elem(struct hash_netportnet4_elem *elem,
92 struct hash_netportnet4_elem *orig)
94 elem->ip[1] = orig->ip[1];
98 hash_netportnet4_data_netmask(struct hash_netportnet4_elem *elem,
102 elem->ip[1] &= ip_set_netmask(cidr);
103 elem->cidr[1] = cidr;
105 elem->ip[0] &= ip_set_netmask(cidr);
106 elem->cidr[0] = cidr;
111 hash_netportnet4_data_list(struct sk_buff *skb,
112 const struct hash_netportnet4_elem *data)
114 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
116 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
117 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
118 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
119 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
120 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
121 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
123 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
124 goto nla_put_failure;
132 hash_netportnet4_data_next(struct hash_netportnet4_elem *next,
133 const struct hash_netportnet4_elem *d)
135 next->ipcmp = d->ipcmp;
136 next->port = d->port;
139 #define MTYPE hash_netportnet4
141 #include "ip_set_hash_gen.h"
144 hash_netportnet4_init(struct hash_netportnet4_elem *e)
146 e->cidr[0] = HOST_MASK;
147 e->cidr[1] = HOST_MASK;
151 hash_netportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
152 const struct xt_action_param *par,
153 enum ipset_adt adt, struct ip_set_adt_opt *opt)
155 const struct hash_netportnet4 *h = set->data;
156 ipset_adtfn adtfn = set->variant->adt[adt];
157 struct hash_netportnet4_elem e = { };
158 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
160 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
161 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
162 if (adt == IPSET_TEST)
163 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
165 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
169 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
170 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1]);
171 e.ip[0] &= ip_set_netmask(e.cidr[0]);
172 e.ip[1] &= ip_set_netmask(e.cidr[1]);
174 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
178 hash_netportnet4_range_to_cidr(u32 from, u32 to, u8 *cidr)
180 if (from == 0 && to == UINT_MAX) {
184 return ip_set_range_to_cidr(from, to, cidr);
188 hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
189 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
191 struct hash_netportnet4 *h = set->data;
192 ipset_adtfn adtfn = set->variant->adt[adt];
193 struct hash_netportnet4_elem e = { };
194 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
195 u32 ip = 0, ip_to = 0, p = 0, port, port_to;
196 u32 ip2_from = 0, ip2_to = 0, ip2, i = 0;
197 bool with_ports = false;
200 if (tb[IPSET_ATTR_LINENO])
201 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
203 hash_netportnet4_init(&e);
204 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
205 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
206 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
207 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
208 return -IPSET_ERR_PROTOCOL;
210 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
214 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
218 ret = ip_set_get_extensions(set, tb, &ext);
222 if (tb[IPSET_ATTR_CIDR]) {
223 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
224 if (e.cidr[0] > HOST_MASK)
225 return -IPSET_ERR_INVALID_CIDR;
228 if (tb[IPSET_ATTR_CIDR2]) {
229 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
230 if (e.cidr[1] > HOST_MASK)
231 return -IPSET_ERR_INVALID_CIDR;
234 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
236 if (tb[IPSET_ATTR_PROTO]) {
237 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
238 with_ports = ip_set_proto_with_ports(e.proto);
241 return -IPSET_ERR_INVALID_PROTO;
243 return -IPSET_ERR_MISSING_PROTO;
246 if (!(with_ports || e.proto == IPPROTO_ICMP))
249 if (tb[IPSET_ATTR_CADT_FLAGS]) {
250 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
252 if (cadt_flags & IPSET_FLAG_NOMATCH)
253 flags |= (IPSET_FLAG_NOMATCH << 16);
256 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
257 if (adt == IPSET_TEST ||
258 !(tb[IPSET_ATTR_IP_TO] || with_ports || tb[IPSET_ATTR_IP2_TO])) {
259 e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
260 e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1]));
261 ret = adtfn(set, &e, &ext, &ext, flags);
262 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
263 ip_set_eexist(ret, flags) ? 0 : ret;
267 if (tb[IPSET_ATTR_IP_TO]) {
268 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
273 if (unlikely(ip + UINT_MAX == ip_to))
274 return -IPSET_ERR_HASH_RANGE;
276 ip_set_mask_from_to(ip, ip_to, e.cidr[0]);
279 port_to = port = ntohs(e.port);
280 if (tb[IPSET_ATTR_PORT_TO]) {
281 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
287 if (tb[IPSET_ATTR_IP2_TO]) {
288 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
291 if (ip2_from > ip2_to)
292 swap(ip2_from, ip2_to);
293 if (unlikely(ip2_from + UINT_MAX == ip2_to))
294 return -IPSET_ERR_HASH_RANGE;
296 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]);
300 ip = ntohl(h->next.ip[0]);
301 p = ntohs(h->next.port);
302 ip2 = ntohl(h->next.ip[1]);
310 ip = hash_netportnet4_range_to_cidr(ip, ip_to, &e.cidr[0]);
311 for (; p <= port_to; p++) {
315 e.ip[1] = htonl(ip2);
316 if (i > IPSET_MAX_RANGE) {
317 hash_netportnet4_data_next(&h->next,
321 ip2 = hash_netportnet4_range_to_cidr(ip2,
323 ret = adtfn(set, &e, &ext, &ext, flags);
324 if (ret && !ip_set_eexist(ret, flags))
328 } while (ip2++ < ip2_to);
332 } while (ip++ < ip_to);
338 struct hash_netportnet6_elem {
339 union nf_inet_addr ip[2];
350 /* Common functions */
353 hash_netportnet6_data_equal(const struct hash_netportnet6_elem *ip1,
354 const struct hash_netportnet6_elem *ip2,
357 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
358 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
359 ip1->ccmp == ip2->ccmp &&
360 ip1->port == ip2->port &&
361 ip1->proto == ip2->proto;
365 hash_netportnet6_do_data_match(const struct hash_netportnet6_elem *elem)
367 return elem->nomatch ? -ENOTEMPTY : 1;
371 hash_netportnet6_data_set_flags(struct hash_netportnet6_elem *elem, u32 flags)
373 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
377 hash_netportnet6_data_reset_flags(struct hash_netportnet6_elem *elem, u8 *flags)
379 swap(*flags, elem->nomatch);
383 hash_netportnet6_data_reset_elem(struct hash_netportnet6_elem *elem,
384 struct hash_netportnet6_elem *orig)
386 elem->ip[1] = orig->ip[1];
390 hash_netportnet6_data_netmask(struct hash_netportnet6_elem *elem,
394 ip6_netmask(&elem->ip[1], cidr);
395 elem->cidr[1] = cidr;
397 ip6_netmask(&elem->ip[0], cidr);
398 elem->cidr[0] = cidr;
403 hash_netportnet6_data_list(struct sk_buff *skb,
404 const struct hash_netportnet6_elem *data)
406 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
408 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
409 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
410 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
411 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
412 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
413 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
415 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
416 goto nla_put_failure;
424 hash_netportnet6_data_next(struct hash_netportnet6_elem *next,
425 const struct hash_netportnet6_elem *d)
427 next->port = d->port;
433 #define MTYPE hash_netportnet6
434 #define HOST_MASK 128
435 #define IP_SET_EMIT_CREATE
436 #include "ip_set_hash_gen.h"
439 hash_netportnet6_init(struct hash_netportnet6_elem *e)
441 e->cidr[0] = HOST_MASK;
442 e->cidr[1] = HOST_MASK;
446 hash_netportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
447 const struct xt_action_param *par,
448 enum ipset_adt adt, struct ip_set_adt_opt *opt)
450 const struct hash_netportnet6 *h = set->data;
451 ipset_adtfn adtfn = set->variant->adt[adt];
452 struct hash_netportnet6_elem e = { };
453 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
455 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
456 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
457 if (adt == IPSET_TEST)
458 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
460 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
464 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
465 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1].in6);
466 ip6_netmask(&e.ip[0], e.cidr[0]);
467 ip6_netmask(&e.ip[1], e.cidr[1]);
469 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
473 hash_netportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
474 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
476 const struct hash_netportnet6 *h = set->data;
477 ipset_adtfn adtfn = set->variant->adt[adt];
478 struct hash_netportnet6_elem e = { };
479 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
481 bool with_ports = false;
484 if (tb[IPSET_ATTR_LINENO])
485 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
487 hash_netportnet6_init(&e);
488 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
489 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
490 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
491 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
492 return -IPSET_ERR_PROTOCOL;
493 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
494 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
496 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]);
500 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]);
504 ret = ip_set_get_extensions(set, tb, &ext);
508 if (tb[IPSET_ATTR_CIDR]) {
509 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
510 if (e.cidr[0] > HOST_MASK)
511 return -IPSET_ERR_INVALID_CIDR;
514 if (tb[IPSET_ATTR_CIDR2]) {
515 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
516 if (e.cidr[1] > HOST_MASK)
517 return -IPSET_ERR_INVALID_CIDR;
520 ip6_netmask(&e.ip[0], e.cidr[0]);
521 ip6_netmask(&e.ip[1], e.cidr[1]);
523 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
525 if (tb[IPSET_ATTR_PROTO]) {
526 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
527 with_ports = ip_set_proto_with_ports(e.proto);
530 return -IPSET_ERR_INVALID_PROTO;
532 return -IPSET_ERR_MISSING_PROTO;
535 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
538 if (tb[IPSET_ATTR_CADT_FLAGS]) {
539 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
541 if (cadt_flags & IPSET_FLAG_NOMATCH)
542 flags |= (IPSET_FLAG_NOMATCH << 16);
545 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
546 ret = adtfn(set, &e, &ext, &ext, flags);
547 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
548 ip_set_eexist(ret, flags) ? 0 : ret;
551 port = ntohs(e.port);
552 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
557 port = ntohs(h->next.port);
558 for (; port <= port_to; port++) {
559 e.port = htons(port);
560 ret = adtfn(set, &e, &ext, &ext, flags);
562 if (ret && !ip_set_eexist(ret, flags))
570 static struct ip_set_type hash_netportnet_type __read_mostly = {
571 .name = "hash:net,port,net",
572 .protocol = IPSET_PROTOCOL,
573 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
575 .dimension = IPSET_DIM_THREE,
576 .family = NFPROTO_UNSPEC,
577 .revision_min = IPSET_TYPE_REV_MIN,
578 .revision_max = IPSET_TYPE_REV_MAX,
579 .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
580 .create = hash_netportnet_create,
582 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
583 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
584 [IPSET_ATTR_INITVAL] = { .type = NLA_U32 },
585 [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
586 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
587 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
588 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
591 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
592 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
593 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
594 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
595 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
596 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
597 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
598 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
599 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
600 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
601 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
602 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
603 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
604 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
605 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
606 .len = IPSET_MAX_COMMENT_SIZE },
607 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
608 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
609 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
615 hash_netportnet_init(void)
617 return ip_set_type_register(&hash_netportnet_type);
621 hash_netportnet_fini(void)
624 ip_set_type_unregister(&hash_netportnet_type);
627 module_init(hash_netportnet_init);
628 module_exit(hash_netportnet_fini);