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 #define IPSET_TYPE_REV_MAX 2 /* skbinfo support added */
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
30 IP_SET_MODULE_DESC("hash:net,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
31 MODULE_ALIAS("ip_set_hash:net,port,net");
33 /* Type specific function prefix */
34 #define HTYPE hash_netportnet
35 #define IP_SET_HASH_WITH_PROTO
36 #define IP_SET_HASH_WITH_NETS
37 #define IPSET_NET_COUNT 2
42 struct hash_netportnet4_elem {
57 /* Common functions */
60 hash_netportnet4_data_equal(const struct hash_netportnet4_elem *ip1,
61 const struct hash_netportnet4_elem *ip2,
64 return ip1->ipcmp == ip2->ipcmp &&
65 ip1->ccmp == ip2->ccmp &&
66 ip1->port == ip2->port &&
67 ip1->proto == ip2->proto;
71 hash_netportnet4_do_data_match(const struct hash_netportnet4_elem *elem)
73 return elem->nomatch ? -ENOTEMPTY : 1;
77 hash_netportnet4_data_set_flags(struct hash_netportnet4_elem *elem, u32 flags)
79 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
83 hash_netportnet4_data_reset_flags(struct hash_netportnet4_elem *elem, u8 *flags)
85 swap(*flags, elem->nomatch);
89 hash_netportnet4_data_reset_elem(struct hash_netportnet4_elem *elem,
90 struct hash_netportnet4_elem *orig)
92 elem->ip[1] = orig->ip[1];
96 hash_netportnet4_data_netmask(struct hash_netportnet4_elem *elem,
100 elem->ip[1] &= ip_set_netmask(cidr);
101 elem->cidr[1] = cidr;
103 elem->ip[0] &= ip_set_netmask(cidr);
104 elem->cidr[0] = cidr;
109 hash_netportnet4_data_list(struct sk_buff *skb,
110 const struct hash_netportnet4_elem *data)
112 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
114 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
115 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
116 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
117 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
118 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
119 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
121 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
122 goto nla_put_failure;
130 hash_netportnet4_data_next(struct hash_netportnet4_elem *next,
131 const struct hash_netportnet4_elem *d)
133 next->ipcmp = d->ipcmp;
134 next->port = d->port;
137 #define MTYPE hash_netportnet4
139 #include "ip_set_hash_gen.h"
142 hash_netportnet4_init(struct hash_netportnet4_elem *e)
144 e->cidr[0] = HOST_MASK;
145 e->cidr[1] = HOST_MASK;
149 hash_netportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
150 const struct xt_action_param *par,
151 enum ipset_adt adt, struct ip_set_adt_opt *opt)
153 const struct hash_netportnet4 *h = set->data;
154 ipset_adtfn adtfn = set->variant->adt[adt];
155 struct hash_netportnet4_elem e = { };
156 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
158 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
159 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
160 if (adt == IPSET_TEST)
161 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
163 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
167 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
168 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1]);
169 e.ip[0] &= ip_set_netmask(e.cidr[0]);
170 e.ip[1] &= ip_set_netmask(e.cidr[1]);
172 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
176 hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
177 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
179 const struct hash_netportnet4 *h = set->data;
180 ipset_adtfn adtfn = set->variant->adt[adt];
181 struct hash_netportnet4_elem e = { };
182 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
183 u32 ip = 0, ip_to = 0, p = 0, port, port_to;
184 u32 ip2_from = 0, ip2_to = 0, ip2;
185 bool with_ports = false;
188 if (tb[IPSET_ATTR_LINENO])
189 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
191 hash_netportnet4_init(&e);
192 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
193 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
194 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
195 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
196 return -IPSET_ERR_PROTOCOL;
198 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
202 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
206 ret = ip_set_get_extensions(set, tb, &ext);
210 if (tb[IPSET_ATTR_CIDR]) {
211 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
212 if (e.cidr[0] > HOST_MASK)
213 return -IPSET_ERR_INVALID_CIDR;
216 if (tb[IPSET_ATTR_CIDR2]) {
217 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
218 if (e.cidr[1] > HOST_MASK)
219 return -IPSET_ERR_INVALID_CIDR;
222 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
224 if (tb[IPSET_ATTR_PROTO]) {
225 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
226 with_ports = ip_set_proto_with_ports(e.proto);
229 return -IPSET_ERR_INVALID_PROTO;
231 return -IPSET_ERR_MISSING_PROTO;
234 if (!(with_ports || e.proto == IPPROTO_ICMP))
237 if (tb[IPSET_ATTR_CADT_FLAGS]) {
238 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
240 if (cadt_flags & IPSET_FLAG_NOMATCH)
241 flags |= (IPSET_FLAG_NOMATCH << 16);
244 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
245 if (adt == IPSET_TEST ||
246 !(tb[IPSET_ATTR_IP_TO] || with_ports || tb[IPSET_ATTR_IP2_TO])) {
247 e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
248 e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1]));
249 ret = adtfn(set, &e, &ext, &ext, flags);
250 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
251 ip_set_eexist(ret, flags) ? 0 : ret;
255 if (tb[IPSET_ATTR_IP_TO]) {
256 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
261 if (unlikely(ip + UINT_MAX == ip_to))
262 return -IPSET_ERR_HASH_RANGE;
264 ip_set_mask_from_to(ip, ip_to, e.cidr[0]);
267 port_to = port = ntohs(e.port);
268 if (tb[IPSET_ATTR_PORT_TO]) {
269 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
275 if (tb[IPSET_ATTR_IP2_TO]) {
276 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
279 if (ip2_from > ip2_to)
280 swap(ip2_from, ip2_to);
281 if (unlikely(ip2_from + UINT_MAX == ip2_to))
282 return -IPSET_ERR_HASH_RANGE;
284 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]);
288 ip = ntohl(h->next.ip[0]);
289 p = ntohs(h->next.port);
290 ip2 = ntohl(h->next.ip[1]);
298 ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr[0]);
299 for (; p <= port_to; p++) {
302 e.ip[1] = htonl(ip2);
303 ip2 = ip_set_range_to_cidr(ip2, ip2_to,
305 ret = adtfn(set, &e, &ext, &ext, flags);
306 if (ret && !ip_set_eexist(ret, flags))
310 } while (ip2++ < ip2_to);
314 } while (ip++ < ip_to);
320 struct hash_netportnet6_elem {
321 union nf_inet_addr ip[2];
332 /* Common functions */
335 hash_netportnet6_data_equal(const struct hash_netportnet6_elem *ip1,
336 const struct hash_netportnet6_elem *ip2,
339 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
340 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
341 ip1->ccmp == ip2->ccmp &&
342 ip1->port == ip2->port &&
343 ip1->proto == ip2->proto;
347 hash_netportnet6_do_data_match(const struct hash_netportnet6_elem *elem)
349 return elem->nomatch ? -ENOTEMPTY : 1;
353 hash_netportnet6_data_set_flags(struct hash_netportnet6_elem *elem, u32 flags)
355 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
359 hash_netportnet6_data_reset_flags(struct hash_netportnet6_elem *elem, u8 *flags)
361 swap(*flags, elem->nomatch);
365 hash_netportnet6_data_reset_elem(struct hash_netportnet6_elem *elem,
366 struct hash_netportnet6_elem *orig)
368 elem->ip[1] = orig->ip[1];
372 hash_netportnet6_data_netmask(struct hash_netportnet6_elem *elem,
376 ip6_netmask(&elem->ip[1], cidr);
377 elem->cidr[1] = cidr;
379 ip6_netmask(&elem->ip[0], cidr);
380 elem->cidr[0] = cidr;
385 hash_netportnet6_data_list(struct sk_buff *skb,
386 const struct hash_netportnet6_elem *data)
388 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
390 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
391 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
392 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
393 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
394 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
395 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
397 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
398 goto nla_put_failure;
406 hash_netportnet6_data_next(struct hash_netportnet6_elem *next,
407 const struct hash_netportnet6_elem *d)
409 next->port = d->port;
415 #define MTYPE hash_netportnet6
416 #define HOST_MASK 128
417 #define IP_SET_EMIT_CREATE
418 #include "ip_set_hash_gen.h"
421 hash_netportnet6_init(struct hash_netportnet6_elem *e)
423 e->cidr[0] = HOST_MASK;
424 e->cidr[1] = HOST_MASK;
428 hash_netportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
429 const struct xt_action_param *par,
430 enum ipset_adt adt, struct ip_set_adt_opt *opt)
432 const struct hash_netportnet6 *h = set->data;
433 ipset_adtfn adtfn = set->variant->adt[adt];
434 struct hash_netportnet6_elem e = { };
435 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
437 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
438 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
439 if (adt == IPSET_TEST)
440 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
442 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
446 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
447 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1].in6);
448 ip6_netmask(&e.ip[0], e.cidr[0]);
449 ip6_netmask(&e.ip[1], e.cidr[1]);
451 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
455 hash_netportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
456 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
458 const struct hash_netportnet6 *h = set->data;
459 ipset_adtfn adtfn = set->variant->adt[adt];
460 struct hash_netportnet6_elem e = { };
461 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
463 bool with_ports = false;
466 if (tb[IPSET_ATTR_LINENO])
467 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
469 hash_netportnet6_init(&e);
470 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
471 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
472 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
473 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
474 return -IPSET_ERR_PROTOCOL;
475 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
476 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
478 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]);
482 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]);
486 ret = ip_set_get_extensions(set, tb, &ext);
490 if (tb[IPSET_ATTR_CIDR]) {
491 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
492 if (e.cidr[0] > HOST_MASK)
493 return -IPSET_ERR_INVALID_CIDR;
496 if (tb[IPSET_ATTR_CIDR2]) {
497 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
498 if (e.cidr[1] > HOST_MASK)
499 return -IPSET_ERR_INVALID_CIDR;
502 ip6_netmask(&e.ip[0], e.cidr[0]);
503 ip6_netmask(&e.ip[1], e.cidr[1]);
505 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
507 if (tb[IPSET_ATTR_PROTO]) {
508 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
509 with_ports = ip_set_proto_with_ports(e.proto);
512 return -IPSET_ERR_INVALID_PROTO;
514 return -IPSET_ERR_MISSING_PROTO;
517 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
520 if (tb[IPSET_ATTR_CADT_FLAGS]) {
521 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
523 if (cadt_flags & IPSET_FLAG_NOMATCH)
524 flags |= (IPSET_FLAG_NOMATCH << 16);
527 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
528 ret = adtfn(set, &e, &ext, &ext, flags);
529 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
530 ip_set_eexist(ret, flags) ? 0 : ret;
533 port = ntohs(e.port);
534 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
539 port = ntohs(h->next.port);
540 for (; port <= port_to; port++) {
541 e.port = htons(port);
542 ret = adtfn(set, &e, &ext, &ext, flags);
544 if (ret && !ip_set_eexist(ret, flags))
552 static struct ip_set_type hash_netportnet_type __read_mostly = {
553 .name = "hash:net,port,net",
554 .protocol = IPSET_PROTOCOL,
555 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
557 .dimension = IPSET_DIM_THREE,
558 .family = NFPROTO_UNSPEC,
559 .revision_min = IPSET_TYPE_REV_MIN,
560 .revision_max = IPSET_TYPE_REV_MAX,
561 .create = hash_netportnet_create,
563 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
564 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
565 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
566 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
567 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
568 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
571 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
572 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
573 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
574 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
575 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
576 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
577 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
578 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
579 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
580 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
581 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
582 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
583 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
584 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
585 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
586 .len = IPSET_MAX_COMMENT_SIZE },
587 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
588 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
589 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
595 hash_netportnet_init(void)
597 return ip_set_type_register(&hash_netportnet_type);
601 hash_netportnet_fini(void)
604 ip_set_type_unregister(&hash_netportnet_type);
607 module_init(hash_netportnet_init);
608 module_exit(hash_netportnet_fini);