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 /* 5 bucketsize, initval support */
28 #define IPSET_TYPE_REV_MAX 6 /* bitmask support */
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
32 IP_SET_MODULE_DESC("hash:ip", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
33 MODULE_ALIAS("ip_set_hash:ip");
35 /* Type specific function prefix */
37 #define IP_SET_HASH_WITH_NETMASK
38 #define IP_SET_HASH_WITH_BITMASK
43 struct hash_ip4_elem {
44 /* Zero valued IP addresses cannot be stored */
48 /* Common functions */
51 hash_ip4_data_equal(const struct hash_ip4_elem *e1,
52 const struct hash_ip4_elem *e2,
55 return e1->ip == e2->ip;
59 hash_ip4_data_list(struct sk_buff *skb, const struct hash_ip4_elem *e)
61 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, e->ip))
70 hash_ip4_data_next(struct hash_ip4_elem *next, const struct hash_ip4_elem *e)
75 #define MTYPE hash_ip4
77 #include "ip_set_hash_gen.h"
80 hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,
81 const struct xt_action_param *par,
82 enum ipset_adt adt, struct ip_set_adt_opt *opt)
84 const struct hash_ip4 *h = set->data;
85 ipset_adtfn adtfn = set->variant->adt[adt];
86 struct hash_ip4_elem e = { 0 };
87 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
90 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
96 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
100 hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
101 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
103 struct hash_ip4 *h = set->data;
104 ipset_adtfn adtfn = set->variant->adt[adt];
105 struct hash_ip4_elem e = { 0 };
106 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
107 u32 ip = 0, ip_to = 0, hosts, i = 0;
110 if (tb[IPSET_ATTR_LINENO])
111 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
113 if (unlikely(!tb[IPSET_ATTR_IP]))
114 return -IPSET_ERR_PROTOCOL;
116 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
120 ret = ip_set_get_extensions(set, tb, &ext);
124 ip &= ntohl(h->bitmask.ip);
127 return -IPSET_ERR_HASH_ELEM;
129 if (adt == IPSET_TEST)
130 return adtfn(set, &e, &ext, &ext, flags);
133 if (tb[IPSET_ATTR_IP_TO]) {
134 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
139 return -IPSET_ERR_HASH_ELEM;
142 } else if (tb[IPSET_ATTR_CIDR]) {
143 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
145 if (!cidr || cidr > HOST_MASK)
146 return -IPSET_ERR_INVALID_CIDR;
147 ip_set_mask_from_to(ip, ip_to, cidr);
150 hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
153 ip = ntohl(h->next.ip);
154 for (; ip <= ip_to; i++) {
156 if (i > IPSET_MAX_RANGE) {
157 hash_ip4_data_next(&h->next, &e);
160 ret = adtfn(set, &e, &ext, &ext, flags);
161 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_data_list(struct sk_buff *skb, const struct hash_ip6_elem *e)
193 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6))
194 goto nla_put_failure;
202 hash_ip6_data_next(struct hash_ip6_elem *next, const struct hash_ip6_elem *e)
209 #define MTYPE hash_ip6
210 #define HOST_MASK 128
212 #define IP_SET_EMIT_CREATE
213 #include "ip_set_hash_gen.h"
216 hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
217 const struct xt_action_param *par,
218 enum ipset_adt adt, struct ip_set_adt_opt *opt)
220 const struct hash_ip6 *h = set->data;
221 ipset_adtfn adtfn = set->variant->adt[adt];
222 struct hash_ip6_elem e = { { .all = { 0 } } };
223 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
225 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
226 nf_inet_addr_mask_inplace(&e.ip, &h->bitmask);
227 if (ipv6_addr_any(&e.ip.in6))
230 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
234 hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
235 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
237 const struct hash_ip6 *h = set->data;
238 ipset_adtfn adtfn = set->variant->adt[adt];
239 struct hash_ip6_elem e = { { .all = { 0 } } };
240 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
243 if (tb[IPSET_ATTR_LINENO])
244 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
246 if (unlikely(!tb[IPSET_ATTR_IP]))
247 return -IPSET_ERR_PROTOCOL;
248 if (unlikely(tb[IPSET_ATTR_IP_TO]))
249 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
250 if (unlikely(tb[IPSET_ATTR_CIDR])) {
251 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
253 if (cidr != HOST_MASK)
254 return -IPSET_ERR_INVALID_CIDR;
257 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
261 ret = ip_set_get_extensions(set, tb, &ext);
265 nf_inet_addr_mask_inplace(&e.ip, &h->bitmask);
266 if (ipv6_addr_any(&e.ip.in6))
267 return -IPSET_ERR_HASH_ELEM;
269 ret = adtfn(set, &e, &ext, &ext, flags);
271 return ip_set_eexist(ret, flags) ? 0 : ret;
274 static struct ip_set_type hash_ip_type __read_mostly = {
276 .protocol = IPSET_PROTOCOL,
277 .features = IPSET_TYPE_IP,
278 .dimension = IPSET_DIM_ONE,
279 .family = NFPROTO_UNSPEC,
280 .revision_min = IPSET_TYPE_REV_MIN,
281 .revision_max = IPSET_TYPE_REV_MAX,
282 .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
283 .create = hash_ip_create,
285 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
286 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
287 [IPSET_ATTR_INITVAL] = { .type = NLA_U32 },
288 [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
289 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
290 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
291 [IPSET_ATTR_NETMASK] = { .type = NLA_U8 },
292 [IPSET_ATTR_BITMASK] = { .type = NLA_NESTED },
293 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
296 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
297 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
298 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
299 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
300 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
301 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
302 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
303 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
304 .len = IPSET_MAX_COMMENT_SIZE },
305 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
306 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
307 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
315 return ip_set_type_register(&hash_ip_type);
322 ip_set_type_unregister(&hash_ip_type);
325 module_init(hash_ip_init);
326 module_exit(hash_ip_fini);