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 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_hash.h>
21 #define IPSET_TYPE_REV_MIN 0
22 /* 1 Range as input support for IPv4 added */
23 /* 2 nomatch flag support added */
24 /* 3 Counters support added */
25 /* 4 Comments support added */
26 /* 5 Forceadd support added */
27 #define IPSET_TYPE_REV_MAX 6 /* skbinfo mapping support added */
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
31 IP_SET_MODULE_DESC("hash:net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
32 MODULE_ALIAS("ip_set_hash:net");
34 /* Type specific function prefix */
35 #define HTYPE hash_net
36 #define IP_SET_HASH_WITH_NETS
41 struct hash_net4_elem {
48 /* Common functions */
51 hash_net4_data_equal(const struct hash_net4_elem *ip1,
52 const struct hash_net4_elem *ip2,
55 return ip1->ip == ip2->ip &&
56 ip1->cidr == ip2->cidr;
60 hash_net4_do_data_match(const struct hash_net4_elem *elem)
62 return elem->nomatch ? -ENOTEMPTY : 1;
66 hash_net4_data_set_flags(struct hash_net4_elem *elem, u32 flags)
68 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
72 hash_net4_data_reset_flags(struct hash_net4_elem *elem, u8 *flags)
74 swap(*flags, elem->nomatch);
78 hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
80 elem->ip &= ip_set_netmask(cidr);
85 hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
87 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
89 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
90 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
92 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
101 hash_net4_data_next(struct hash_net4_elem *next,
102 const struct hash_net4_elem *d)
107 #define MTYPE hash_net4
109 #include "ip_set_hash_gen.h"
112 hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
113 const struct xt_action_param *par,
114 enum ipset_adt adt, struct ip_set_adt_opt *opt)
116 const struct hash_net4 *h = set->data;
117 ipset_adtfn adtfn = set->variant->adt[adt];
118 struct hash_net4_elem e = {
119 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
121 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
125 if (adt == IPSET_TEST)
128 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
129 e.ip &= ip_set_netmask(e.cidr);
131 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
135 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
136 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
138 struct hash_net4 *h = set->data;
139 ipset_adtfn adtfn = set->variant->adt[adt];
140 struct hash_net4_elem e = { .cidr = HOST_MASK };
141 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
142 u32 ip = 0, ip_to = 0, i = 0;
145 if (tb[IPSET_ATTR_LINENO])
146 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
148 if (unlikely(!tb[IPSET_ATTR_IP] ||
149 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
150 return -IPSET_ERR_PROTOCOL;
152 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
156 ret = ip_set_get_extensions(set, tb, &ext);
160 if (tb[IPSET_ATTR_CIDR]) {
161 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
162 if (!e.cidr || e.cidr > HOST_MASK)
163 return -IPSET_ERR_INVALID_CIDR;
166 if (tb[IPSET_ATTR_CADT_FLAGS]) {
167 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
169 if (cadt_flags & IPSET_FLAG_NOMATCH)
170 flags |= (IPSET_FLAG_NOMATCH << 16);
173 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
174 e.ip = htonl(ip & ip_set_hostmask(e.cidr));
175 ret = adtfn(set, &e, &ext, &ext, flags);
176 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
177 ip_set_eexist(ret, flags) ? 0 : ret;
181 if (tb[IPSET_ATTR_IP_TO]) {
182 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
187 if (ip + UINT_MAX == ip_to)
188 return -IPSET_ERR_HASH_RANGE;
192 ip = ntohl(h->next.ip);
196 if (i > IPSET_MAX_RANGE) {
197 hash_net4_data_next(&h->next, &e);
200 ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
201 ret = adtfn(set, &e, &ext, &ext, flags);
202 if (ret && !ip_set_eexist(ret, flags))
206 } while (ip++ < ip_to);
212 struct hash_net6_elem {
213 union nf_inet_addr ip;
219 /* Common functions */
222 hash_net6_data_equal(const struct hash_net6_elem *ip1,
223 const struct hash_net6_elem *ip2,
226 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
227 ip1->cidr == ip2->cidr;
231 hash_net6_do_data_match(const struct hash_net6_elem *elem)
233 return elem->nomatch ? -ENOTEMPTY : 1;
237 hash_net6_data_set_flags(struct hash_net6_elem *elem, u32 flags)
239 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
243 hash_net6_data_reset_flags(struct hash_net6_elem *elem, u8 *flags)
245 swap(*flags, elem->nomatch);
249 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
251 ip6_netmask(&elem->ip, cidr);
256 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
258 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
260 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
261 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
263 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
264 goto nla_put_failure;
272 hash_net6_data_next(struct hash_net6_elem *next,
273 const struct hash_net6_elem *d)
280 #define MTYPE hash_net6
281 #define HOST_MASK 128
282 #define IP_SET_EMIT_CREATE
283 #include "ip_set_hash_gen.h"
286 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
287 const struct xt_action_param *par,
288 enum ipset_adt adt, struct ip_set_adt_opt *opt)
290 const struct hash_net6 *h = set->data;
291 ipset_adtfn adtfn = set->variant->adt[adt];
292 struct hash_net6_elem e = {
293 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
295 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
299 if (adt == IPSET_TEST)
302 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
303 ip6_netmask(&e.ip, e.cidr);
305 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
309 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
310 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
312 ipset_adtfn adtfn = set->variant->adt[adt];
313 struct hash_net6_elem e = { .cidr = HOST_MASK };
314 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
317 if (tb[IPSET_ATTR_LINENO])
318 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
320 if (unlikely(!tb[IPSET_ATTR_IP] ||
321 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
322 return -IPSET_ERR_PROTOCOL;
323 if (unlikely(tb[IPSET_ATTR_IP_TO]))
324 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
326 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
330 ret = ip_set_get_extensions(set, tb, &ext);
334 if (tb[IPSET_ATTR_CIDR]) {
335 e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
336 if (!e.cidr || e.cidr > HOST_MASK)
337 return -IPSET_ERR_INVALID_CIDR;
340 ip6_netmask(&e.ip, e.cidr);
342 if (tb[IPSET_ATTR_CADT_FLAGS]) {
343 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
345 if (cadt_flags & IPSET_FLAG_NOMATCH)
346 flags |= (IPSET_FLAG_NOMATCH << 16);
349 ret = adtfn(set, &e, &ext, &ext, flags);
351 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
352 ip_set_eexist(ret, flags) ? 0 : ret;
355 static struct ip_set_type hash_net_type __read_mostly = {
357 .protocol = IPSET_PROTOCOL,
358 .features = IPSET_TYPE_IP | IPSET_TYPE_NOMATCH,
359 .dimension = IPSET_DIM_ONE,
360 .family = NFPROTO_UNSPEC,
361 .revision_min = IPSET_TYPE_REV_MIN,
362 .revision_max = IPSET_TYPE_REV_MAX,
363 .create = hash_net_create,
365 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
366 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
367 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
368 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
369 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
370 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
373 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
374 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
375 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
376 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
377 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
378 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
379 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
380 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
381 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
382 .len = IPSET_MAX_COMMENT_SIZE },
383 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
384 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
385 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
393 return ip_set_type_register(&hash_net_type);
400 ip_set_type_unregister(&hash_net_type);
403 module_init(hash_net_init);
404 module_exit(hash_net_fini);