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 /* 1 SCTP and UDPLITE support added */
25 /* 2 Range as input support for IPv4 added */
26 /* 3 nomatch flag support added */
27 /* 4 Counters support added */
28 /* 5 Comments support added */
29 /* 6 Forceadd support added */
30 #define IPSET_TYPE_REV_MAX 7 /* skbinfo support added */
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
34 IP_SET_MODULE_DESC("hash:ip,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
35 MODULE_ALIAS("ip_set_hash:ip,port,net");
37 /* Type specific function prefix */
38 #define HTYPE hash_ipportnet
40 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
41 * However this way we have to store internally cidr - 1,
42 * dancing back and forth.
44 #define IP_SET_HASH_WITH_NETS_PACKED
45 #define IP_SET_HASH_WITH_PROTO
46 #define IP_SET_HASH_WITH_NETS
51 struct hash_ipportnet4_elem {
60 /* Common functions */
63 hash_ipportnet4_data_equal(const struct hash_ipportnet4_elem *ip1,
64 const struct hash_ipportnet4_elem *ip2,
67 return ip1->ip == ip2->ip &&
68 ip1->ip2 == ip2->ip2 &&
69 ip1->cidr == ip2->cidr &&
70 ip1->port == ip2->port &&
71 ip1->proto == ip2->proto;
75 hash_ipportnet4_do_data_match(const struct hash_ipportnet4_elem *elem)
77 return elem->nomatch ? -ENOTEMPTY : 1;
81 hash_ipportnet4_data_set_flags(struct hash_ipportnet4_elem *elem, u32 flags)
83 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
87 hash_ipportnet4_data_reset_flags(struct hash_ipportnet4_elem *elem, u8 *flags)
89 swap(*flags, elem->nomatch);
93 hash_ipportnet4_data_netmask(struct hash_ipportnet4_elem *elem, u8 cidr)
95 elem->ip2 &= ip_set_netmask(cidr);
96 elem->cidr = cidr - 1;
100 hash_ipportnet4_data_list(struct sk_buff *skb,
101 const struct hash_ipportnet4_elem *data)
103 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
105 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
106 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip2) ||
107 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
108 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
109 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
111 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
112 goto nla_put_failure;
120 hash_ipportnet4_data_next(struct hash_ipportnet4_elem *next,
121 const struct hash_ipportnet4_elem *d)
124 next->port = d->port;
128 #define MTYPE hash_ipportnet4
130 #include "ip_set_hash_gen.h"
133 hash_ipportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
134 const struct xt_action_param *par,
135 enum ipset_adt adt, struct ip_set_adt_opt *opt)
137 const struct hash_ipportnet4 *h = set->data;
138 ipset_adtfn adtfn = set->variant->adt[adt];
139 struct hash_ipportnet4_elem e = {
140 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
142 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
144 if (adt == IPSET_TEST)
145 e.cidr = HOST_MASK - 1;
147 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
151 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
152 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2);
153 e.ip2 &= ip_set_netmask(e.cidr + 1);
155 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
159 hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
160 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
162 struct hash_ipportnet4 *h = set->data;
163 ipset_adtfn adtfn = set->variant->adt[adt];
164 struct hash_ipportnet4_elem e = { .cidr = HOST_MASK - 1 };
165 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
166 u32 ip = 0, ip_to = 0, p = 0, port, port_to;
167 u32 ip2_from = 0, ip2_to = 0, ip2, i = 0;
168 bool with_ports = false;
172 if (tb[IPSET_ATTR_LINENO])
173 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
175 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
176 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
177 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
178 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
179 return -IPSET_ERR_PROTOCOL;
181 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
185 ret = ip_set_get_extensions(set, tb, &ext);
189 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
193 if (tb[IPSET_ATTR_CIDR2]) {
194 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
195 if (!cidr || cidr > HOST_MASK)
196 return -IPSET_ERR_INVALID_CIDR;
200 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
202 if (tb[IPSET_ATTR_PROTO]) {
203 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
204 with_ports = ip_set_proto_with_ports(e.proto);
207 return -IPSET_ERR_INVALID_PROTO;
209 return -IPSET_ERR_MISSING_PROTO;
212 if (!(with_ports || e.proto == IPPROTO_ICMP))
215 if (tb[IPSET_ATTR_CADT_FLAGS]) {
216 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
218 if (cadt_flags & IPSET_FLAG_NOMATCH)
219 flags |= (IPSET_FLAG_NOMATCH << 16);
222 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
223 if (adt == IPSET_TEST ||
224 !(tb[IPSET_ATTR_CIDR] || tb[IPSET_ATTR_IP_TO] || with_ports ||
225 tb[IPSET_ATTR_IP2_TO])) {
227 e.ip2 = htonl(ip2_from & ip_set_hostmask(e.cidr + 1));
228 ret = adtfn(set, &e, &ext, &ext, flags);
229 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
230 ip_set_eexist(ret, flags) ? 0 : ret;
234 if (tb[IPSET_ATTR_IP_TO]) {
235 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
240 } else if (tb[IPSET_ATTR_CIDR]) {
241 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
243 if (!cidr || cidr > HOST_MASK)
244 return -IPSET_ERR_INVALID_CIDR;
245 ip_set_mask_from_to(ip, ip_to, cidr);
248 port_to = port = ntohs(e.port);
249 if (tb[IPSET_ATTR_PORT_TO]) {
250 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
256 if (tb[IPSET_ATTR_IP2_TO]) {
257 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
260 if (ip2_from > ip2_to)
261 swap(ip2_from, ip2_to);
262 if (ip2_from + UINT_MAX == ip2_to)
263 return -IPSET_ERR_HASH_RANGE;
265 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr + 1);
269 ip = ntohl(h->next.ip);
270 p = ntohs(h->next.port);
271 ip2 = ntohl(h->next.ip2);
276 for (; ip <= ip_to; ip++) {
278 for (; p <= port_to; p++) {
283 ip2 = ip_set_range_to_cidr(ip2, ip2_to, &cidr);
285 if (i > IPSET_MAX_RANGE) {
286 hash_ipportnet4_data_next(&h->next,
290 ret = adtfn(set, &e, &ext, &ext, flags);
292 if (ret && !ip_set_eexist(ret, flags))
296 } while (ip2++ < ip2_to);
306 struct hash_ipportnet6_elem {
307 union nf_inet_addr ip;
308 union nf_inet_addr ip2;
315 /* Common functions */
318 hash_ipportnet6_data_equal(const struct hash_ipportnet6_elem *ip1,
319 const struct hash_ipportnet6_elem *ip2,
322 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
323 ipv6_addr_equal(&ip1->ip2.in6, &ip2->ip2.in6) &&
324 ip1->cidr == ip2->cidr &&
325 ip1->port == ip2->port &&
326 ip1->proto == ip2->proto;
330 hash_ipportnet6_do_data_match(const struct hash_ipportnet6_elem *elem)
332 return elem->nomatch ? -ENOTEMPTY : 1;
336 hash_ipportnet6_data_set_flags(struct hash_ipportnet6_elem *elem, u32 flags)
338 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
342 hash_ipportnet6_data_reset_flags(struct hash_ipportnet6_elem *elem, u8 *flags)
344 swap(*flags, elem->nomatch);
348 hash_ipportnet6_data_netmask(struct hash_ipportnet6_elem *elem, u8 cidr)
350 ip6_netmask(&elem->ip2, cidr);
351 elem->cidr = cidr - 1;
355 hash_ipportnet6_data_list(struct sk_buff *skb,
356 const struct hash_ipportnet6_elem *data)
358 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
360 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
361 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip2.in6) ||
362 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
363 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr + 1) ||
364 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
366 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
367 goto nla_put_failure;
375 hash_ipportnet6_data_next(struct hash_ipportnet6_elem *next,
376 const struct hash_ipportnet6_elem *d)
378 next->port = d->port;
384 #define MTYPE hash_ipportnet6
385 #define HOST_MASK 128
386 #define IP_SET_EMIT_CREATE
387 #include "ip_set_hash_gen.h"
390 hash_ipportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
391 const struct xt_action_param *par,
392 enum ipset_adt adt, struct ip_set_adt_opt *opt)
394 const struct hash_ipportnet6 *h = set->data;
395 ipset_adtfn adtfn = set->variant->adt[adt];
396 struct hash_ipportnet6_elem e = {
397 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
399 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
401 if (adt == IPSET_TEST)
402 e.cidr = HOST_MASK - 1;
404 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
408 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
409 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip2.in6);
410 ip6_netmask(&e.ip2, e.cidr + 1);
412 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
416 hash_ipportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
417 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
419 const struct hash_ipportnet6 *h = set->data;
420 ipset_adtfn adtfn = set->variant->adt[adt];
421 struct hash_ipportnet6_elem e = { .cidr = HOST_MASK - 1 };
422 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
424 bool with_ports = false;
428 if (tb[IPSET_ATTR_LINENO])
429 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
431 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
432 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
433 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
434 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
435 return -IPSET_ERR_PROTOCOL;
436 if (unlikely(tb[IPSET_ATTR_IP_TO]))
437 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
438 if (unlikely(tb[IPSET_ATTR_CIDR])) {
439 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
441 if (cidr != HOST_MASK)
442 return -IPSET_ERR_INVALID_CIDR;
445 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
449 ret = ip_set_get_extensions(set, tb, &ext);
453 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip2);
457 if (tb[IPSET_ATTR_CIDR2]) {
458 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
459 if (!cidr || cidr > HOST_MASK)
460 return -IPSET_ERR_INVALID_CIDR;
464 ip6_netmask(&e.ip2, e.cidr + 1);
466 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
468 if (tb[IPSET_ATTR_PROTO]) {
469 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
470 with_ports = ip_set_proto_with_ports(e.proto);
473 return -IPSET_ERR_INVALID_PROTO;
475 return -IPSET_ERR_MISSING_PROTO;
478 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
481 if (tb[IPSET_ATTR_CADT_FLAGS]) {
482 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
484 if (cadt_flags & IPSET_FLAG_NOMATCH)
485 flags |= (IPSET_FLAG_NOMATCH << 16);
488 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
489 ret = adtfn(set, &e, &ext, &ext, flags);
490 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
491 ip_set_eexist(ret, flags) ? 0 : ret;
494 port = ntohs(e.port);
495 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
500 port = ntohs(h->next.port);
501 for (; port <= port_to; port++) {
502 e.port = htons(port);
503 ret = adtfn(set, &e, &ext, &ext, flags);
505 if (ret && !ip_set_eexist(ret, flags))
513 static struct ip_set_type hash_ipportnet_type __read_mostly = {
514 .name = "hash:ip,port,net",
515 .protocol = IPSET_PROTOCOL,
516 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
518 .dimension = IPSET_DIM_THREE,
519 .family = NFPROTO_UNSPEC,
520 .revision_min = IPSET_TYPE_REV_MIN,
521 .revision_max = IPSET_TYPE_REV_MAX,
522 .create = hash_ipportnet_create,
524 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
525 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
526 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
527 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
528 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
529 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
532 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
533 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
534 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
535 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
536 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
537 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
538 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
539 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
540 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
541 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
542 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
543 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
544 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
545 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
546 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
547 .len = IPSET_MAX_COMMENT_SIZE },
548 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
549 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
550 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
556 hash_ipportnet_init(void)
558 return ip_set_type_register(&hash_ipportnet_type);
562 hash_ipportnet_fini(void)
565 ip_set_type_unregister(&hash_ipportnet_type);
568 module_init(hash_ipportnet_init);
569 module_exit(hash_ipportnet_fini);