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 #define IPSET_TYPE_REV_MAX 4 /* skbinfo support */
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
30 IP_SET_MODULE_DESC("hash:ip", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
31 MODULE_ALIAS("ip_set_hash:ip");
33 /* Type specific function prefix */
35 #define IP_SET_HASH_WITH_NETMASK
40 struct hash_ip4_elem {
41 /* Zero valued IP addresses cannot be stored */
45 /* Common functions */
48 hash_ip4_data_equal(const struct hash_ip4_elem *e1,
49 const struct hash_ip4_elem *e2,
52 return e1->ip == e2->ip;
56 hash_ip4_data_list(struct sk_buff *skb, const struct hash_ip4_elem *e)
58 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, e->ip))
67 hash_ip4_data_next(struct hash_ip4_elem *next, const struct hash_ip4_elem *e)
72 #define MTYPE hash_ip4
74 #include "ip_set_hash_gen.h"
77 hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,
78 const struct xt_action_param *par,
79 enum ipset_adt adt, struct ip_set_adt_opt *opt)
81 const struct hash_ip4 *h = set->data;
82 ipset_adtfn adtfn = set->variant->adt[adt];
83 struct hash_ip4_elem e = { 0 };
84 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
87 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
88 ip &= ip_set_netmask(h->netmask);
93 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
97 hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
98 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
100 const struct hash_ip4 *h = set->data;
101 ipset_adtfn adtfn = set->variant->adt[adt];
102 struct hash_ip4_elem e = { 0 };
103 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
104 u32 ip = 0, ip_to = 0, hosts;
107 if (tb[IPSET_ATTR_LINENO])
108 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
110 if (unlikely(!tb[IPSET_ATTR_IP]))
111 return -IPSET_ERR_PROTOCOL;
113 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
117 ret = ip_set_get_extensions(set, tb, &ext);
121 ip &= ip_set_hostmask(h->netmask);
124 return -IPSET_ERR_HASH_ELEM;
126 if (adt == IPSET_TEST)
127 return adtfn(set, &e, &ext, &ext, flags);
130 if (tb[IPSET_ATTR_IP_TO]) {
131 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
136 } else if (tb[IPSET_ATTR_CIDR]) {
137 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
139 if (!cidr || cidr > HOST_MASK)
140 return -IPSET_ERR_INVALID_CIDR;
141 ip_set_mask_from_to(ip, ip_to, cidr);
144 hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
147 ip = ntohl(h->next.ip);
150 for (; ip <= ip_to;) {
151 ret = adtfn(set, &e, &ext, &ext, flags);
152 if (ret && !ip_set_eexist(ret, flags))
167 /* Member elements */
168 struct hash_ip6_elem {
169 union nf_inet_addr ip;
172 /* Common functions */
175 hash_ip6_data_equal(const struct hash_ip6_elem *ip1,
176 const struct hash_ip6_elem *ip2,
179 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6);
183 hash_ip6_netmask(union nf_inet_addr *ip, u8 prefix)
185 ip6_netmask(ip, prefix);
189 hash_ip6_data_list(struct sk_buff *skb, const struct hash_ip6_elem *e)
191 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6))
192 goto nla_put_failure;
200 hash_ip6_data_next(struct hash_ip6_elem *next, const struct hash_ip6_elem *e)
207 #define MTYPE hash_ip6
208 #define HOST_MASK 128
210 #define IP_SET_EMIT_CREATE
211 #include "ip_set_hash_gen.h"
214 hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
215 const struct xt_action_param *par,
216 enum ipset_adt adt, struct ip_set_adt_opt *opt)
218 const struct hash_ip6 *h = set->data;
219 ipset_adtfn adtfn = set->variant->adt[adt];
220 struct hash_ip6_elem e = { { .all = { 0 } } };
221 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
223 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
224 hash_ip6_netmask(&e.ip, h->netmask);
225 if (ipv6_addr_any(&e.ip.in6))
228 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
232 hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
233 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
235 const struct hash_ip6 *h = set->data;
236 ipset_adtfn adtfn = set->variant->adt[adt];
237 struct hash_ip6_elem e = { { .all = { 0 } } };
238 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
241 if (tb[IPSET_ATTR_LINENO])
242 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
244 if (unlikely(!tb[IPSET_ATTR_IP]))
245 return -IPSET_ERR_PROTOCOL;
246 if (unlikely(tb[IPSET_ATTR_IP_TO]))
247 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
248 if (unlikely(tb[IPSET_ATTR_CIDR])) {
249 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
251 if (cidr != HOST_MASK)
252 return -IPSET_ERR_INVALID_CIDR;
255 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
259 ret = ip_set_get_extensions(set, tb, &ext);
263 hash_ip6_netmask(&e.ip, h->netmask);
264 if (ipv6_addr_any(&e.ip.in6))
265 return -IPSET_ERR_HASH_ELEM;
267 ret = adtfn(set, &e, &ext, &ext, flags);
269 return ip_set_eexist(ret, flags) ? 0 : ret;
272 static struct ip_set_type hash_ip_type __read_mostly = {
274 .protocol = IPSET_PROTOCOL,
275 .features = IPSET_TYPE_IP,
276 .dimension = IPSET_DIM_ONE,
277 .family = NFPROTO_UNSPEC,
278 .revision_min = IPSET_TYPE_REV_MIN,
279 .revision_max = IPSET_TYPE_REV_MAX,
280 .create = hash_ip_create,
282 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
283 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
284 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
285 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
286 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
287 [IPSET_ATTR_NETMASK] = { .type = NLA_U8 },
288 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
291 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
292 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
293 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
294 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
295 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
296 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
297 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
298 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
299 .len = IPSET_MAX_COMMENT_SIZE },
300 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
301 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
302 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
310 return ip_set_type_register(&hash_ip_type);
317 ip_set_type_unregister(&hash_ip_type);
320 module_init(hash_ip_init);
321 module_exit(hash_ip_fini);