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:net,port 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>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/ipset/pfxlen.h>
18 #include <linux/netfilter/ipset/ip_set.h>
19 #include <linux/netfilter/ipset/ip_set_getport.h>
20 #include <linux/netfilter/ipset/ip_set_hash.h>
22 #define IPSET_TYPE_REV_MIN 0
23 /* 1 SCTP and UDPLITE support added */
24 /* 2 Range as input support for IPv4 added */
25 /* 3 nomatch flag support added */
26 /* 4 Counters support added */
27 /* 5 Comments support added */
28 /* 6 Forceadd support added */
29 /* 7 skbinfo support added */
30 #define IPSET_TYPE_REV_MAX 8 /* bucketsize, initval support added */
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
34 IP_SET_MODULE_DESC("hash:net,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
35 MODULE_ALIAS("ip_set_hash:net,port");
37 /* Type specific function prefix */
38 #define HTYPE hash_netport
39 #define IP_SET_HASH_WITH_PROTO
40 #define IP_SET_HASH_WITH_NETS
42 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
43 * However this way we have to store internally cidr - 1,
44 * dancing back and forth.
46 #define IP_SET_HASH_WITH_NETS_PACKED
51 struct hash_netport4_elem {
59 /* Common functions */
62 hash_netport4_data_equal(const struct hash_netport4_elem *ip1,
63 const struct hash_netport4_elem *ip2,
66 return ip1->ip == ip2->ip &&
67 ip1->port == ip2->port &&
68 ip1->proto == ip2->proto &&
69 ip1->cidr == ip2->cidr;
73 hash_netport4_do_data_match(const struct hash_netport4_elem *elem)
75 return elem->nomatch ? -ENOTEMPTY : 1;
79 hash_netport4_data_set_flags(struct hash_netport4_elem *elem, u32 flags)
81 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
85 hash_netport4_data_reset_flags(struct hash_netport4_elem *elem, u8 *flags)
87 swap(*flags, elem->nomatch);
91 hash_netport4_data_netmask(struct hash_netport4_elem *elem, u8 cidr)
93 elem->ip &= ip_set_netmask(cidr);
94 elem->cidr = cidr - 1;
98 hash_netport4_data_list(struct sk_buff *skb,
99 const struct hash_netport4_elem *data)
101 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
103 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
104 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
105 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
106 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
108 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
109 goto nla_put_failure;
117 hash_netport4_data_next(struct hash_netport4_elem *next,
118 const struct hash_netport4_elem *d)
121 next->port = d->port;
124 #define MTYPE hash_netport4
126 #include "ip_set_hash_gen.h"
129 hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb,
130 const struct xt_action_param *par,
131 enum ipset_adt adt, struct ip_set_adt_opt *opt)
133 const struct hash_netport4 *h = set->data;
134 ipset_adtfn adtfn = set->variant->adt[adt];
135 struct hash_netport4_elem e = {
136 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
138 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
140 if (adt == IPSET_TEST)
141 e.cidr = HOST_MASK - 1;
143 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
147 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
148 e.ip &= ip_set_netmask(e.cidr + 1);
150 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
154 hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
155 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
157 const struct hash_netport4 *h = set->data;
158 ipset_adtfn adtfn = set->variant->adt[adt];
159 struct hash_netport4_elem e = { .cidr = HOST_MASK - 1 };
160 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
161 u32 port, port_to, p = 0, ip = 0, ip_to = 0;
162 bool with_ports = false;
166 if (tb[IPSET_ATTR_LINENO])
167 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
169 if (unlikely(!tb[IPSET_ATTR_IP] ||
170 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
171 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
172 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
173 return -IPSET_ERR_PROTOCOL;
175 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
179 ret = ip_set_get_extensions(set, tb, &ext);
183 if (tb[IPSET_ATTR_CIDR]) {
184 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
185 if (!cidr || cidr > HOST_MASK)
186 return -IPSET_ERR_INVALID_CIDR;
190 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
192 if (tb[IPSET_ATTR_PROTO]) {
193 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
194 with_ports = ip_set_proto_with_ports(e.proto);
197 return -IPSET_ERR_INVALID_PROTO;
199 return -IPSET_ERR_MISSING_PROTO;
202 if (!(with_ports || e.proto == IPPROTO_ICMP))
205 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
207 if (tb[IPSET_ATTR_CADT_FLAGS]) {
208 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
210 if (cadt_flags & IPSET_FLAG_NOMATCH)
211 flags |= (IPSET_FLAG_NOMATCH << 16);
214 if (adt == IPSET_TEST || !(with_ports || tb[IPSET_ATTR_IP_TO])) {
215 e.ip = htonl(ip & ip_set_hostmask(e.cidr + 1));
216 ret = adtfn(set, &e, &ext, &ext, flags);
217 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
218 ip_set_eexist(ret, flags) ? 0 : ret;
221 port = port_to = ntohs(e.port);
222 if (tb[IPSET_ATTR_PORT_TO]) {
223 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
227 if (tb[IPSET_ATTR_IP_TO]) {
228 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
233 if (ip + UINT_MAX == ip_to)
234 return -IPSET_ERR_HASH_RANGE;
236 ip_set_mask_from_to(ip, ip_to, e.cidr + 1);
240 ip = ntohl(h->next.ip);
241 p = ntohs(h->next.port);
247 ip = ip_set_range_to_cidr(ip, ip_to, &cidr);
249 for (; p <= port_to; p++) {
251 ret = adtfn(set, &e, &ext, &ext, flags);
252 if (ret && !ip_set_eexist(ret, flags))
258 } while (ip++ < ip_to);
264 struct hash_netport6_elem {
265 union nf_inet_addr ip;
272 /* Common functions */
275 hash_netport6_data_equal(const struct hash_netport6_elem *ip1,
276 const struct hash_netport6_elem *ip2,
279 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
280 ip1->port == ip2->port &&
281 ip1->proto == ip2->proto &&
282 ip1->cidr == ip2->cidr;
286 hash_netport6_do_data_match(const struct hash_netport6_elem *elem)
288 return elem->nomatch ? -ENOTEMPTY : 1;
292 hash_netport6_data_set_flags(struct hash_netport6_elem *elem, u32 flags)
294 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
298 hash_netport6_data_reset_flags(struct hash_netport6_elem *elem, u8 *flags)
300 swap(*flags, elem->nomatch);
304 hash_netport6_data_netmask(struct hash_netport6_elem *elem, u8 cidr)
306 ip6_netmask(&elem->ip, cidr);
307 elem->cidr = cidr - 1;
311 hash_netport6_data_list(struct sk_buff *skb,
312 const struct hash_netport6_elem *data)
314 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
316 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
317 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
318 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
319 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
321 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
322 goto nla_put_failure;
330 hash_netport6_data_next(struct hash_netport6_elem *next,
331 const struct hash_netport6_elem *d)
333 next->port = d->port;
339 #define MTYPE hash_netport6
340 #define HOST_MASK 128
341 #define IP_SET_EMIT_CREATE
342 #include "ip_set_hash_gen.h"
345 hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb,
346 const struct xt_action_param *par,
347 enum ipset_adt adt, struct ip_set_adt_opt *opt)
349 const struct hash_netport6 *h = set->data;
350 ipset_adtfn adtfn = set->variant->adt[adt];
351 struct hash_netport6_elem e = {
352 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
354 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
356 if (adt == IPSET_TEST)
357 e.cidr = HOST_MASK - 1;
359 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
363 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
364 ip6_netmask(&e.ip, e.cidr + 1);
366 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
370 hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[],
371 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
373 const struct hash_netport6 *h = set->data;
374 ipset_adtfn adtfn = set->variant->adt[adt];
375 struct hash_netport6_elem e = { .cidr = HOST_MASK - 1 };
376 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
378 bool with_ports = false;
382 if (tb[IPSET_ATTR_LINENO])
383 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
385 if (unlikely(!tb[IPSET_ATTR_IP] ||
386 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
387 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
388 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
389 return -IPSET_ERR_PROTOCOL;
390 if (unlikely(tb[IPSET_ATTR_IP_TO]))
391 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
393 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
397 ret = ip_set_get_extensions(set, tb, &ext);
401 if (tb[IPSET_ATTR_CIDR]) {
402 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
403 if (!cidr || cidr > HOST_MASK)
404 return -IPSET_ERR_INVALID_CIDR;
407 ip6_netmask(&e.ip, e.cidr + 1);
409 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
411 if (tb[IPSET_ATTR_PROTO]) {
412 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
413 with_ports = ip_set_proto_with_ports(e.proto);
416 return -IPSET_ERR_INVALID_PROTO;
418 return -IPSET_ERR_MISSING_PROTO;
421 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
424 if (tb[IPSET_ATTR_CADT_FLAGS]) {
425 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
427 if (cadt_flags & IPSET_FLAG_NOMATCH)
428 flags |= (IPSET_FLAG_NOMATCH << 16);
431 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
432 ret = adtfn(set, &e, &ext, &ext, flags);
433 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
434 ip_set_eexist(ret, flags) ? 0 : ret;
437 port = ntohs(e.port);
438 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
443 port = ntohs(h->next.port);
444 for (; port <= port_to; port++) {
445 e.port = htons(port);
446 ret = adtfn(set, &e, &ext, &ext, flags);
448 if (ret && !ip_set_eexist(ret, flags))
456 static struct ip_set_type hash_netport_type __read_mostly = {
457 .name = "hash:net,port",
458 .protocol = IPSET_PROTOCOL,
459 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_NOMATCH,
460 .dimension = IPSET_DIM_TWO,
461 .family = NFPROTO_UNSPEC,
462 .revision_min = IPSET_TYPE_REV_MIN,
463 .revision_max = IPSET_TYPE_REV_MAX,
464 .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
465 .create = hash_netport_create,
467 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
468 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
469 [IPSET_ATTR_INITVAL] = { .type = NLA_U32 },
470 [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
471 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
472 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
473 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
474 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
477 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
478 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
479 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
480 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
481 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
482 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
483 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
484 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
485 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
486 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
487 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
488 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
489 .len = IPSET_MAX_COMMENT_SIZE },
490 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
491 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
492 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
498 hash_netport_init(void)
500 return ip_set_type_register(&hash_netport_type);
504 hash_netport_fini(void)
507 ip_set_type_unregister(&hash_netport_type);
510 module_init(hash_netport_init);
511 module_exit(hash_netport_fini);