1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org>
3 * Copyright (C) 2013 Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>
6 /* Kernel module implementing an IP set type: the hash:net type */
8 #include <linux/jhash.h>
9 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/errno.h>
13 #include <linux/random.h>
16 #include <net/netlink.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter/ipset/pfxlen.h>
20 #include <linux/netfilter/ipset/ip_set.h>
21 #include <linux/netfilter/ipset/ip_set_hash.h>
23 #define IPSET_TYPE_REV_MIN 0
24 /* 1 Forceadd support added */
25 #define IPSET_TYPE_REV_MAX 2 /* skbinfo support added */
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
29 IP_SET_MODULE_DESC("hash:net,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
30 MODULE_ALIAS("ip_set_hash:net,net");
32 /* Type specific function prefix */
33 #define HTYPE hash_netnet
34 #define IP_SET_HASH_WITH_NETS
35 #define IPSET_NET_COUNT 2
40 struct hash_netnet4_elem {
53 /* Common functions */
56 hash_netnet4_data_equal(const struct hash_netnet4_elem *ip1,
57 const struct hash_netnet4_elem *ip2,
60 return ip1->ipcmp == ip2->ipcmp &&
61 ip1->ccmp == ip2->ccmp;
65 hash_netnet4_do_data_match(const struct hash_netnet4_elem *elem)
67 return elem->nomatch ? -ENOTEMPTY : 1;
71 hash_netnet4_data_set_flags(struct hash_netnet4_elem *elem, u32 flags)
73 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
77 hash_netnet4_data_reset_flags(struct hash_netnet4_elem *elem, u8 *flags)
79 swap(*flags, elem->nomatch);
83 hash_netnet4_data_reset_elem(struct hash_netnet4_elem *elem,
84 struct hash_netnet4_elem *orig)
86 elem->ip[1] = orig->ip[1];
90 hash_netnet4_data_netmask(struct hash_netnet4_elem *elem, u8 cidr, bool inner)
93 elem->ip[1] &= ip_set_netmask(cidr);
96 elem->ip[0] &= ip_set_netmask(cidr);
102 hash_netnet4_data_list(struct sk_buff *skb,
103 const struct hash_netnet4_elem *data)
105 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
107 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
108 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
109 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
110 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
112 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
113 goto nla_put_failure;
121 hash_netnet4_data_next(struct hash_netnet4_elem *next,
122 const struct hash_netnet4_elem *d)
124 next->ipcmp = d->ipcmp;
127 #define MTYPE hash_netnet4
129 #include "ip_set_hash_gen.h"
132 hash_netnet4_init(struct hash_netnet4_elem *e)
134 e->cidr[0] = HOST_MASK;
135 e->cidr[1] = HOST_MASK;
139 hash_netnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
140 const struct xt_action_param *par,
141 enum ipset_adt adt, struct ip_set_adt_opt *opt)
143 const struct hash_netnet4 *h = set->data;
144 ipset_adtfn adtfn = set->variant->adt[adt];
145 struct hash_netnet4_elem e = { };
146 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
148 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
149 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
150 if (adt == IPSET_TEST)
151 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
153 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
154 ip4addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1]);
155 e.ip[0] &= ip_set_netmask(e.cidr[0]);
156 e.ip[1] &= ip_set_netmask(e.cidr[1]);
158 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
162 hash_netnet4_uadt(struct ip_set *set, struct nlattr *tb[],
163 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
165 const struct hash_netnet4 *h = set->data;
166 ipset_adtfn adtfn = set->variant->adt[adt];
167 struct hash_netnet4_elem e = { };
168 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
169 u32 ip = 0, ip_to = 0;
170 u32 ip2 = 0, ip2_from = 0, ip2_to = 0;
173 if (tb[IPSET_ATTR_LINENO])
174 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
176 hash_netnet4_init(&e);
177 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
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_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
189 ret = ip_set_get_extensions(set, tb, &ext);
193 if (tb[IPSET_ATTR_CIDR]) {
194 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
195 if (!e.cidr[0] || e.cidr[0] > HOST_MASK)
196 return -IPSET_ERR_INVALID_CIDR;
199 if (tb[IPSET_ATTR_CIDR2]) {
200 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
201 if (!e.cidr[1] || e.cidr[1] > HOST_MASK)
202 return -IPSET_ERR_INVALID_CIDR;
205 if (tb[IPSET_ATTR_CADT_FLAGS]) {
206 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
208 if (cadt_flags & IPSET_FLAG_NOMATCH)
209 flags |= (IPSET_FLAG_NOMATCH << 16);
212 if (adt == IPSET_TEST || !(tb[IPSET_ATTR_IP_TO] ||
213 tb[IPSET_ATTR_IP2_TO])) {
214 e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
215 e.ip[1] = htonl(ip2_from & 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;
222 if (tb[IPSET_ATTR_IP_TO]) {
223 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
228 if (unlikely(ip + UINT_MAX == ip_to))
229 return -IPSET_ERR_HASH_RANGE;
231 ip_set_mask_from_to(ip, ip_to, e.cidr[0]);
235 if (tb[IPSET_ATTR_IP2_TO]) {
236 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
239 if (ip2_to < ip2_from)
240 swap(ip2_from, ip2_to);
241 if (unlikely(ip2_from + UINT_MAX == ip2_to))
242 return -IPSET_ERR_HASH_RANGE;
244 ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]);
248 ip = ntohl(h->next.ip[0]);
249 ip2 = ntohl(h->next.ip[1]);
256 ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr[0]);
258 e.ip[1] = htonl(ip2);
259 ip2 = ip_set_range_to_cidr(ip2, ip2_to, &e.cidr[1]);
260 ret = adtfn(set, &e, &ext, &ext, flags);
261 if (ret && !ip_set_eexist(ret, flags))
265 } while (ip2++ < ip2_to);
267 } while (ip++ < ip_to);
273 struct hash_netnet6_elem {
274 union nf_inet_addr ip[2];
283 /* Common functions */
286 hash_netnet6_data_equal(const struct hash_netnet6_elem *ip1,
287 const struct hash_netnet6_elem *ip2,
290 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
291 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
292 ip1->ccmp == ip2->ccmp;
296 hash_netnet6_do_data_match(const struct hash_netnet6_elem *elem)
298 return elem->nomatch ? -ENOTEMPTY : 1;
302 hash_netnet6_data_set_flags(struct hash_netnet6_elem *elem, u32 flags)
304 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
308 hash_netnet6_data_reset_flags(struct hash_netnet6_elem *elem, u8 *flags)
310 swap(*flags, elem->nomatch);
314 hash_netnet6_data_reset_elem(struct hash_netnet6_elem *elem,
315 struct hash_netnet6_elem *orig)
317 elem->ip[1] = orig->ip[1];
321 hash_netnet6_data_netmask(struct hash_netnet6_elem *elem, u8 cidr, bool inner)
324 ip6_netmask(&elem->ip[1], cidr);
325 elem->cidr[1] = cidr;
327 ip6_netmask(&elem->ip[0], cidr);
328 elem->cidr[0] = cidr;
333 hash_netnet6_data_list(struct sk_buff *skb,
334 const struct hash_netnet6_elem *data)
336 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
338 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
339 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
340 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
341 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
343 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
344 goto nla_put_failure;
352 hash_netnet6_data_next(struct hash_netnet6_elem *next,
353 const struct hash_netnet6_elem *d)
360 #define MTYPE hash_netnet6
361 #define HOST_MASK 128
362 #define IP_SET_EMIT_CREATE
363 #include "ip_set_hash_gen.h"
366 hash_netnet6_init(struct hash_netnet6_elem *e)
368 e->cidr[0] = HOST_MASK;
369 e->cidr[1] = HOST_MASK;
373 hash_netnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
374 const struct xt_action_param *par,
375 enum ipset_adt adt, struct ip_set_adt_opt *opt)
377 const struct hash_netnet6 *h = set->data;
378 ipset_adtfn adtfn = set->variant->adt[adt];
379 struct hash_netnet6_elem e = { };
380 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
382 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
383 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
384 if (adt == IPSET_TEST)
385 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
387 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
388 ip6addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1].in6);
389 ip6_netmask(&e.ip[0], e.cidr[0]);
390 ip6_netmask(&e.ip[1], e.cidr[1]);
392 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
396 hash_netnet6_uadt(struct ip_set *set, struct nlattr *tb[],
397 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
399 ipset_adtfn adtfn = set->variant->adt[adt];
400 struct hash_netnet6_elem e = { };
401 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
404 if (tb[IPSET_ATTR_LINENO])
405 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
407 hash_netnet6_init(&e);
408 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
409 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
410 return -IPSET_ERR_PROTOCOL;
411 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
412 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
414 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]);
418 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]);
422 ret = ip_set_get_extensions(set, tb, &ext);
426 if (tb[IPSET_ATTR_CIDR]) {
427 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
428 if (!e.cidr[0] || e.cidr[0] > HOST_MASK)
429 return -IPSET_ERR_INVALID_CIDR;
432 if (tb[IPSET_ATTR_CIDR2]) {
433 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
434 if (!e.cidr[1] || e.cidr[1] > HOST_MASK)
435 return -IPSET_ERR_INVALID_CIDR;
438 ip6_netmask(&e.ip[0], e.cidr[0]);
439 ip6_netmask(&e.ip[1], e.cidr[1]);
441 if (tb[IPSET_ATTR_CADT_FLAGS]) {
442 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
444 if (cadt_flags & IPSET_FLAG_NOMATCH)
445 flags |= (IPSET_FLAG_NOMATCH << 16);
448 ret = adtfn(set, &e, &ext, &ext, flags);
450 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
451 ip_set_eexist(ret, flags) ? 0 : ret;
454 static struct ip_set_type hash_netnet_type __read_mostly = {
455 .name = "hash:net,net",
456 .protocol = IPSET_PROTOCOL,
457 .features = IPSET_TYPE_IP | IPSET_TYPE_IP2 | IPSET_TYPE_NOMATCH,
458 .dimension = IPSET_DIM_TWO,
459 .family = NFPROTO_UNSPEC,
460 .revision_min = IPSET_TYPE_REV_MIN,
461 .revision_max = IPSET_TYPE_REV_MAX,
462 .create = hash_netnet_create,
464 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
465 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
466 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
467 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
468 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
469 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
472 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
473 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
474 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
475 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
476 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
477 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
478 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
479 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
480 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
481 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
482 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
483 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
484 .len = IPSET_MAX_COMMENT_SIZE },
485 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
486 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
487 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
493 hash_netnet_init(void)
495 return ip_set_type_register(&hash_netnet_type);
499 hash_netnet_fini(void)
502 ip_set_type_unregister(&hash_netnet_type);
505 module_init(hash_netnet_init);
506 module_exit(hash_netnet_fini);