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 #define IPSET_TYPE_REV_MAX 7 /* skbinfo support added */
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
33 IP_SET_MODULE_DESC("hash:net,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34 MODULE_ALIAS("ip_set_hash:net,port");
36 /* Type specific function prefix */
37 #define HTYPE hash_netport
38 #define IP_SET_HASH_WITH_PROTO
39 #define IP_SET_HASH_WITH_NETS
41 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
42 * However this way we have to store internally cidr - 1,
43 * dancing back and forth.
45 #define IP_SET_HASH_WITH_NETS_PACKED
50 struct hash_netport4_elem {
58 /* Common functions */
61 hash_netport4_data_equal(const struct hash_netport4_elem *ip1,
62 const struct hash_netport4_elem *ip2,
65 return ip1->ip == ip2->ip &&
66 ip1->port == ip2->port &&
67 ip1->proto == ip2->proto &&
68 ip1->cidr == ip2->cidr;
72 hash_netport4_do_data_match(const struct hash_netport4_elem *elem)
74 return elem->nomatch ? -ENOTEMPTY : 1;
78 hash_netport4_data_set_flags(struct hash_netport4_elem *elem, u32 flags)
80 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
84 hash_netport4_data_reset_flags(struct hash_netport4_elem *elem, u8 *flags)
86 swap(*flags, elem->nomatch);
90 hash_netport4_data_netmask(struct hash_netport4_elem *elem, u8 cidr)
92 elem->ip &= ip_set_netmask(cidr);
93 elem->cidr = cidr - 1;
97 hash_netport4_data_list(struct sk_buff *skb,
98 const struct hash_netport4_elem *data)
100 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
102 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
103 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
104 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
105 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
107 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
108 goto nla_put_failure;
116 hash_netport4_data_next(struct hash_netport4_elem *next,
117 const struct hash_netport4_elem *d)
120 next->port = d->port;
123 #define MTYPE hash_netport4
125 #include "ip_set_hash_gen.h"
128 hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb,
129 const struct xt_action_param *par,
130 enum ipset_adt adt, struct ip_set_adt_opt *opt)
132 const struct hash_netport4 *h = set->data;
133 ipset_adtfn adtfn = set->variant->adt[adt];
134 struct hash_netport4_elem e = {
135 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
137 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
139 if (adt == IPSET_TEST)
140 e.cidr = HOST_MASK - 1;
142 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
146 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
147 e.ip &= ip_set_netmask(e.cidr + 1);
149 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
153 hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
154 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
156 struct hash_netport4 *h = set->data;
157 ipset_adtfn adtfn = set->variant->adt[adt];
158 struct hash_netport4_elem e = { .cidr = HOST_MASK - 1 };
159 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
160 u32 port, port_to, p = 0, ip = 0, ip_to = 0, i = 0;
161 bool with_ports = false;
165 if (tb[IPSET_ATTR_LINENO])
166 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
168 if (unlikely(!tb[IPSET_ATTR_IP] ||
169 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
170 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
171 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
172 return -IPSET_ERR_PROTOCOL;
174 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
178 ret = ip_set_get_extensions(set, tb, &ext);
182 if (tb[IPSET_ATTR_CIDR]) {
183 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
184 if (!cidr || cidr > HOST_MASK)
185 return -IPSET_ERR_INVALID_CIDR;
189 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
191 if (tb[IPSET_ATTR_PROTO]) {
192 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
193 with_ports = ip_set_proto_with_ports(e.proto);
196 return -IPSET_ERR_INVALID_PROTO;
198 return -IPSET_ERR_MISSING_PROTO;
201 if (!(with_ports || e.proto == IPPROTO_ICMP))
204 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
206 if (tb[IPSET_ATTR_CADT_FLAGS]) {
207 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
209 if (cadt_flags & IPSET_FLAG_NOMATCH)
210 flags |= (IPSET_FLAG_NOMATCH << 16);
213 if (adt == IPSET_TEST || !(with_ports || tb[IPSET_ATTR_IP_TO])) {
214 e.ip = htonl(ip & ip_set_hostmask(e.cidr + 1));
215 ret = adtfn(set, &e, &ext, &ext, flags);
216 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
217 ip_set_eexist(ret, flags) ? 0 : ret;
220 port = port_to = ntohs(e.port);
221 if (tb[IPSET_ATTR_PORT_TO]) {
222 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
226 if (tb[IPSET_ATTR_IP_TO]) {
227 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
232 if (ip + UINT_MAX == ip_to)
233 return -IPSET_ERR_HASH_RANGE;
235 ip_set_mask_from_to(ip, ip_to, e.cidr + 1);
239 ip = ntohl(h->next.ip);
240 p = ntohs(h->next.port);
246 ip = ip_set_range_to_cidr(ip, ip_to, &cidr);
248 for (; p <= port_to; p++, i++) {
250 if (i > IPSET_MAX_RANGE) {
251 hash_netport4_data_next(&h->next, &e);
254 ret = adtfn(set, &e, &ext, &ext, flags);
255 if (ret && !ip_set_eexist(ret, flags))
261 } while (ip++ < ip_to);
267 struct hash_netport6_elem {
268 union nf_inet_addr ip;
275 /* Common functions */
278 hash_netport6_data_equal(const struct hash_netport6_elem *ip1,
279 const struct hash_netport6_elem *ip2,
282 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
283 ip1->port == ip2->port &&
284 ip1->proto == ip2->proto &&
285 ip1->cidr == ip2->cidr;
289 hash_netport6_do_data_match(const struct hash_netport6_elem *elem)
291 return elem->nomatch ? -ENOTEMPTY : 1;
295 hash_netport6_data_set_flags(struct hash_netport6_elem *elem, u32 flags)
297 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
301 hash_netport6_data_reset_flags(struct hash_netport6_elem *elem, u8 *flags)
303 swap(*flags, elem->nomatch);
307 hash_netport6_data_netmask(struct hash_netport6_elem *elem, u8 cidr)
309 ip6_netmask(&elem->ip, cidr);
310 elem->cidr = cidr - 1;
314 hash_netport6_data_list(struct sk_buff *skb,
315 const struct hash_netport6_elem *data)
317 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
319 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
320 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
321 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
322 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
324 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
325 goto nla_put_failure;
333 hash_netport6_data_next(struct hash_netport6_elem *next,
334 const struct hash_netport6_elem *d)
336 next->port = d->port;
342 #define MTYPE hash_netport6
343 #define HOST_MASK 128
344 #define IP_SET_EMIT_CREATE
345 #include "ip_set_hash_gen.h"
348 hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb,
349 const struct xt_action_param *par,
350 enum ipset_adt adt, struct ip_set_adt_opt *opt)
352 const struct hash_netport6 *h = set->data;
353 ipset_adtfn adtfn = set->variant->adt[adt];
354 struct hash_netport6_elem e = {
355 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
357 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
359 if (adt == IPSET_TEST)
360 e.cidr = HOST_MASK - 1;
362 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
366 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
367 ip6_netmask(&e.ip, e.cidr + 1);
369 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
373 hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[],
374 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
376 const struct hash_netport6 *h = set->data;
377 ipset_adtfn adtfn = set->variant->adt[adt];
378 struct hash_netport6_elem e = { .cidr = HOST_MASK - 1 };
379 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
381 bool with_ports = false;
385 if (tb[IPSET_ATTR_LINENO])
386 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
388 if (unlikely(!tb[IPSET_ATTR_IP] ||
389 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
390 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
391 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
392 return -IPSET_ERR_PROTOCOL;
393 if (unlikely(tb[IPSET_ATTR_IP_TO]))
394 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
396 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
400 ret = ip_set_get_extensions(set, tb, &ext);
404 if (tb[IPSET_ATTR_CIDR]) {
405 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
406 if (!cidr || cidr > HOST_MASK)
407 return -IPSET_ERR_INVALID_CIDR;
410 ip6_netmask(&e.ip, e.cidr + 1);
412 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
414 if (tb[IPSET_ATTR_PROTO]) {
415 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
416 with_ports = ip_set_proto_with_ports(e.proto);
419 return -IPSET_ERR_INVALID_PROTO;
421 return -IPSET_ERR_MISSING_PROTO;
424 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
427 if (tb[IPSET_ATTR_CADT_FLAGS]) {
428 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
430 if (cadt_flags & IPSET_FLAG_NOMATCH)
431 flags |= (IPSET_FLAG_NOMATCH << 16);
434 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
435 ret = adtfn(set, &e, &ext, &ext, flags);
436 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
437 ip_set_eexist(ret, flags) ? 0 : ret;
440 port = ntohs(e.port);
441 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
446 port = ntohs(h->next.port);
447 for (; port <= port_to; port++) {
448 e.port = htons(port);
449 ret = adtfn(set, &e, &ext, &ext, flags);
451 if (ret && !ip_set_eexist(ret, flags))
459 static struct ip_set_type hash_netport_type __read_mostly = {
460 .name = "hash:net,port",
461 .protocol = IPSET_PROTOCOL,
462 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_NOMATCH,
463 .dimension = IPSET_DIM_TWO,
464 .family = NFPROTO_UNSPEC,
465 .revision_min = IPSET_TYPE_REV_MIN,
466 .revision_max = IPSET_TYPE_REV_MAX,
467 .create = hash_netport_create,
469 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
470 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
471 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
472 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
473 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
474 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
475 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
478 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
479 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
480 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
481 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
482 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
483 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
484 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
485 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
486 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
487 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
488 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
489 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
490 .len = IPSET_MAX_COMMENT_SIZE },
491 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
492 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
493 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
499 hash_netport_init(void)
501 return ip_set_type_register(&hash_netport_type);
505 hash_netport_fini(void)
508 ip_set_type_unregister(&hash_netport_type);
511 module_init(hash_netport_init);
512 module_exit(hash_netport_fini);