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 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, i = 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]);
259 e.ip[1] = htonl(ip2);
260 if (i > IPSET_MAX_RANGE) {
261 hash_netnet4_data_next(&h->next, &e);
264 ip2 = ip_set_range_to_cidr(ip2, ip2_to, &e.cidr[1]);
265 ret = adtfn(set, &e, &ext, &ext, flags);
266 if (ret && !ip_set_eexist(ret, flags))
270 } while (ip2++ < ip2_to);
272 } while (ip++ < ip_to);
278 struct hash_netnet6_elem {
279 union nf_inet_addr ip[2];
288 /* Common functions */
291 hash_netnet6_data_equal(const struct hash_netnet6_elem *ip1,
292 const struct hash_netnet6_elem *ip2,
295 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
296 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
297 ip1->ccmp == ip2->ccmp;
301 hash_netnet6_do_data_match(const struct hash_netnet6_elem *elem)
303 return elem->nomatch ? -ENOTEMPTY : 1;
307 hash_netnet6_data_set_flags(struct hash_netnet6_elem *elem, u32 flags)
309 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
313 hash_netnet6_data_reset_flags(struct hash_netnet6_elem *elem, u8 *flags)
315 swap(*flags, elem->nomatch);
319 hash_netnet6_data_reset_elem(struct hash_netnet6_elem *elem,
320 struct hash_netnet6_elem *orig)
322 elem->ip[1] = orig->ip[1];
326 hash_netnet6_data_netmask(struct hash_netnet6_elem *elem, u8 cidr, bool inner)
329 ip6_netmask(&elem->ip[1], cidr);
330 elem->cidr[1] = cidr;
332 ip6_netmask(&elem->ip[0], cidr);
333 elem->cidr[0] = cidr;
338 hash_netnet6_data_list(struct sk_buff *skb,
339 const struct hash_netnet6_elem *data)
341 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
343 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
344 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
345 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
346 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
348 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
349 goto nla_put_failure;
357 hash_netnet6_data_next(struct hash_netnet6_elem *next,
358 const struct hash_netnet6_elem *d)
365 #define MTYPE hash_netnet6
366 #define HOST_MASK 128
367 #define IP_SET_EMIT_CREATE
368 #include "ip_set_hash_gen.h"
371 hash_netnet6_init(struct hash_netnet6_elem *e)
373 e->cidr[0] = HOST_MASK;
374 e->cidr[1] = HOST_MASK;
378 hash_netnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
379 const struct xt_action_param *par,
380 enum ipset_adt adt, struct ip_set_adt_opt *opt)
382 const struct hash_netnet6 *h = set->data;
383 ipset_adtfn adtfn = set->variant->adt[adt];
384 struct hash_netnet6_elem e = { };
385 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
387 e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
388 e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
389 if (adt == IPSET_TEST)
390 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
392 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
393 ip6addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1].in6);
394 ip6_netmask(&e.ip[0], e.cidr[0]);
395 ip6_netmask(&e.ip[1], e.cidr[1]);
397 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
401 hash_netnet6_uadt(struct ip_set *set, struct nlattr *tb[],
402 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
404 ipset_adtfn adtfn = set->variant->adt[adt];
405 struct hash_netnet6_elem e = { };
406 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
409 if (tb[IPSET_ATTR_LINENO])
410 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
412 hash_netnet6_init(&e);
413 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
414 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
415 return -IPSET_ERR_PROTOCOL;
416 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
417 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
419 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]);
423 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]);
427 ret = ip_set_get_extensions(set, tb, &ext);
431 if (tb[IPSET_ATTR_CIDR]) {
432 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
433 if (!e.cidr[0] || e.cidr[0] > HOST_MASK)
434 return -IPSET_ERR_INVALID_CIDR;
437 if (tb[IPSET_ATTR_CIDR2]) {
438 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
439 if (!e.cidr[1] || e.cidr[1] > HOST_MASK)
440 return -IPSET_ERR_INVALID_CIDR;
443 ip6_netmask(&e.ip[0], e.cidr[0]);
444 ip6_netmask(&e.ip[1], e.cidr[1]);
446 if (tb[IPSET_ATTR_CADT_FLAGS]) {
447 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
449 if (cadt_flags & IPSET_FLAG_NOMATCH)
450 flags |= (IPSET_FLAG_NOMATCH << 16);
453 ret = adtfn(set, &e, &ext, &ext, flags);
455 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
456 ip_set_eexist(ret, flags) ? 0 : ret;
459 static struct ip_set_type hash_netnet_type __read_mostly = {
460 .name = "hash:net,net",
461 .protocol = IPSET_PROTOCOL,
462 .features = IPSET_TYPE_IP | IPSET_TYPE_IP2 | 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_netnet_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_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_IP2] = { .type = NLA_NESTED },
480 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
481 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
482 [IPSET_ATTR_CIDR2] = { .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_netnet_init(void)
500 return ip_set_type_register(&hash_netnet_type);
504 hash_netnet_fini(void)
507 ip_set_type_unregister(&hash_netnet_type);
510 module_init(hash_netnet_init);
511 module_exit(hash_netnet_fini);