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:ip 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>
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/ipset/pfxlen.h>
19 #include <linux/netfilter/ipset/ip_set.h>
20 #include <linux/netfilter/ipset/ip_set_hash.h>
22 #define IPSET_TYPE_REV_MIN 0
23 /* 1 Counters support */
24 /* 2 Comments support */
25 /* 3 Forceadd support */
26 /* 4 skbinfo support */
27 #define IPSET_TYPE_REV_MAX 5 /* bucketsize, initval support */
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
31 IP_SET_MODULE_DESC("hash:ip", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
32 MODULE_ALIAS("ip_set_hash:ip");
34 /* Type specific function prefix */
36 #define IP_SET_HASH_WITH_NETMASK
41 struct hash_ip4_elem {
42 /* Zero valued IP addresses cannot be stored */
46 /* Common functions */
49 hash_ip4_data_equal(const struct hash_ip4_elem *e1,
50 const struct hash_ip4_elem *e2,
53 return e1->ip == e2->ip;
57 hash_ip4_data_list(struct sk_buff *skb, const struct hash_ip4_elem *e)
59 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, e->ip))
68 hash_ip4_data_next(struct hash_ip4_elem *next, const struct hash_ip4_elem *e)
73 #define MTYPE hash_ip4
75 #include "ip_set_hash_gen.h"
78 hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,
79 const struct xt_action_param *par,
80 enum ipset_adt adt, struct ip_set_adt_opt *opt)
82 const struct hash_ip4 *h = set->data;
83 ipset_adtfn adtfn = set->variant->adt[adt];
84 struct hash_ip4_elem e = { 0 };
85 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
88 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
89 ip &= ip_set_netmask(h->netmask);
94 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
98 hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
99 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
101 const struct hash_ip4 *h = set->data;
102 ipset_adtfn adtfn = set->variant->adt[adt];
103 struct hash_ip4_elem e = { 0 };
104 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
105 u32 ip = 0, ip_to = 0, hosts;
108 if (tb[IPSET_ATTR_LINENO])
109 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
111 if (unlikely(!tb[IPSET_ATTR_IP]))
112 return -IPSET_ERR_PROTOCOL;
114 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
118 ret = ip_set_get_extensions(set, tb, &ext);
122 ip &= ip_set_hostmask(h->netmask);
125 return -IPSET_ERR_HASH_ELEM;
127 if (adt == IPSET_TEST)
128 return adtfn(set, &e, &ext, &ext, flags);
131 if (tb[IPSET_ATTR_IP_TO]) {
132 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
137 return -IPSET_ERR_HASH_ELEM;
140 } else if (tb[IPSET_ATTR_CIDR]) {
141 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
143 if (!cidr || cidr > HOST_MASK)
144 return -IPSET_ERR_INVALID_CIDR;
145 ip_set_mask_from_to(ip, ip_to, cidr);
148 hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
150 /* 64bit division is not allowed on 32bit */
151 if (((u64)ip_to - ip + 1) >> (32 - h->netmask) > IPSET_MAX_RANGE)
155 ip = ntohl(h->next.ip);
158 for (; ip <= ip_to;) {
159 ret = adtfn(set, &e, &ext, &ext, flags);
160 if (ret && !ip_set_eexist(ret, flags))
175 /* Member elements */
176 struct hash_ip6_elem {
177 union nf_inet_addr ip;
180 /* Common functions */
183 hash_ip6_data_equal(const struct hash_ip6_elem *ip1,
184 const struct hash_ip6_elem *ip2,
187 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6);
191 hash_ip6_netmask(union nf_inet_addr *ip, u8 prefix)
193 ip6_netmask(ip, prefix);
197 hash_ip6_data_list(struct sk_buff *skb, const struct hash_ip6_elem *e)
199 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6))
200 goto nla_put_failure;
208 hash_ip6_data_next(struct hash_ip6_elem *next, const struct hash_ip6_elem *e)
215 #define MTYPE hash_ip6
216 #define HOST_MASK 128
218 #define IP_SET_EMIT_CREATE
219 #include "ip_set_hash_gen.h"
222 hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
223 const struct xt_action_param *par,
224 enum ipset_adt adt, struct ip_set_adt_opt *opt)
226 const struct hash_ip6 *h = set->data;
227 ipset_adtfn adtfn = set->variant->adt[adt];
228 struct hash_ip6_elem e = { { .all = { 0 } } };
229 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
231 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
232 hash_ip6_netmask(&e.ip, h->netmask);
233 if (ipv6_addr_any(&e.ip.in6))
236 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
240 hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
241 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
243 const struct hash_ip6 *h = set->data;
244 ipset_adtfn adtfn = set->variant->adt[adt];
245 struct hash_ip6_elem e = { { .all = { 0 } } };
246 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
249 if (tb[IPSET_ATTR_LINENO])
250 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
252 if (unlikely(!tb[IPSET_ATTR_IP]))
253 return -IPSET_ERR_PROTOCOL;
254 if (unlikely(tb[IPSET_ATTR_IP_TO]))
255 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
256 if (unlikely(tb[IPSET_ATTR_CIDR])) {
257 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
259 if (cidr != HOST_MASK)
260 return -IPSET_ERR_INVALID_CIDR;
263 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
267 ret = ip_set_get_extensions(set, tb, &ext);
271 hash_ip6_netmask(&e.ip, h->netmask);
272 if (ipv6_addr_any(&e.ip.in6))
273 return -IPSET_ERR_HASH_ELEM;
275 ret = adtfn(set, &e, &ext, &ext, flags);
277 return ip_set_eexist(ret, flags) ? 0 : ret;
280 static struct ip_set_type hash_ip_type __read_mostly = {
282 .protocol = IPSET_PROTOCOL,
283 .features = IPSET_TYPE_IP,
284 .dimension = IPSET_DIM_ONE,
285 .family = NFPROTO_UNSPEC,
286 .revision_min = IPSET_TYPE_REV_MIN,
287 .revision_max = IPSET_TYPE_REV_MAX,
288 .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
289 .create = hash_ip_create,
291 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
292 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
293 [IPSET_ATTR_INITVAL] = { .type = NLA_U32 },
294 [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
295 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
296 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
297 [IPSET_ATTR_NETMASK] = { .type = NLA_U8 },
298 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
301 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
302 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
303 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
304 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
305 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
306 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
307 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
308 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
309 .len = IPSET_MAX_COMMENT_SIZE },
310 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
311 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
312 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
320 return ip_set_type_register(&hash_ip_type);
327 ip_set_type_unregister(&hash_ip_type);
330 module_init(hash_ip_init);
331 module_exit(hash_ip_fini);