1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org> */
3
4 /* Kernel module implementing an IP set type: the hash:ip type */
5
6 #include <linux/jhash.h>
7 #include <linux/module.h>
8 #include <linux/ip.h>
9 #include <linux/skbuff.h>
10 #include <linux/errno.h>
11 #include <linux/random.h>
12 #include <net/ip.h>
13 #include <net/ipv6.h>
14 #include <net/netlink.h>
15 #include <net/tcp.h>
16
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>
21
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 */
28
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");
33
34 /* Type specific function prefix */
35 #define HTYPE hash_ip
36 #define IP_SET_HASH_WITH_NETMASK
37
38 /* IPv4 variant */
39
40 /* Member elements */
41 struct hash_ip4_elem {
42 /* Zero valued IP addresses cannot be stored */
43 __be32 ip;
44 };
45
46 /* Common functions */
47
48 static bool
hash_ip4_data_equal(const struct hash_ip4_elem * e1,const struct hash_ip4_elem * e2,u32 * multi)49 hash_ip4_data_equal(const struct hash_ip4_elem *e1,
50 const struct hash_ip4_elem *e2,
51 u32 *multi)
52 {
53 return e1->ip == e2->ip;
54 }
55
56 static bool
hash_ip4_data_list(struct sk_buff * skb,const struct hash_ip4_elem * e)57 hash_ip4_data_list(struct sk_buff *skb, const struct hash_ip4_elem *e)
58 {
59 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, e->ip))
60 goto nla_put_failure;
61 return false;
62
63 nla_put_failure:
64 return true;
65 }
66
67 static void
hash_ip4_data_next(struct hash_ip4_elem * next,const struct hash_ip4_elem * e)68 hash_ip4_data_next(struct hash_ip4_elem *next, const struct hash_ip4_elem *e)
69 {
70 next->ip = e->ip;
71 }
72
73 #define MTYPE hash_ip4
74 #define HOST_MASK 32
75 #include "ip_set_hash_gen.h"
76
77 static int
hash_ip4_kadt(struct ip_set * set,const struct sk_buff * skb,const struct xt_action_param * par,enum ipset_adt adt,struct ip_set_adt_opt * opt)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)
81 {
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);
86 __be32 ip;
87
88 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
89 ip &= ip_set_netmask(h->netmask);
90 if (ip == 0)
91 return -EINVAL;
92
93 e.ip = ip;
94 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
95 }
96
97 static int
hash_ip4_uadt(struct ip_set * set,struct nlattr * tb[],enum ipset_adt adt,u32 * lineno,u32 flags,bool retried)98 hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
99 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
100 {
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;
106 int ret = 0;
107
108 if (tb[IPSET_ATTR_LINENO])
109 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
110
111 if (unlikely(!tb[IPSET_ATTR_IP]))
112 return -IPSET_ERR_PROTOCOL;
113
114 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
115 if (ret)
116 return ret;
117
118 ret = ip_set_get_extensions(set, tb, &ext);
119 if (ret)
120 return ret;
121
122 ip &= ip_set_hostmask(h->netmask);
123 e.ip = htonl(ip);
124 if (e.ip == 0)
125 return -IPSET_ERR_HASH_ELEM;
126
127 if (adt == IPSET_TEST)
128 return adtfn(set, &e, &ext, &ext, flags);
129
130 ip_to = ip;
131 if (tb[IPSET_ATTR_IP_TO]) {
132 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
133 if (ret)
134 return ret;
135 if (ip > ip_to) {
136 if (ip_to == 0)
137 return -IPSET_ERR_HASH_ELEM;
138 swap(ip, ip_to);
139 }
140 } else if (tb[IPSET_ATTR_CIDR]) {
141 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
142
143 if (!cidr || cidr > HOST_MASK)
144 return -IPSET_ERR_INVALID_CIDR;
145 ip_set_mask_from_to(ip, ip_to, cidr);
146 }
147
148 hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
149
150 /* 64bit division is not allowed on 32bit */
151 if (((u64)ip_to - ip + 1) >> (32 - h->netmask) > IPSET_MAX_RANGE)
152 return -ERANGE;
153
154 if (retried)
155 ip = ntohl(h->next.ip);
156 for (; ip <= ip_to;) {
157 e.ip = htonl(ip);
158 ret = adtfn(set, &e, &ext, &ext, flags);
159 if (ret && !ip_set_eexist(ret, flags))
160 return ret;
161
162 ip += hosts;
163 if (ip == 0)
164 return 0;
165
166 ret = 0;
167 }
168 return ret;
169 }
170
171 /* IPv6 variant */
172
173 /* Member elements */
174 struct hash_ip6_elem {
175 union nf_inet_addr ip;
176 };
177
178 /* Common functions */
179
180 static bool
hash_ip6_data_equal(const struct hash_ip6_elem * ip1,const struct hash_ip6_elem * ip2,u32 * multi)181 hash_ip6_data_equal(const struct hash_ip6_elem *ip1,
182 const struct hash_ip6_elem *ip2,
183 u32 *multi)
184 {
185 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6);
186 }
187
188 static void
hash_ip6_netmask(union nf_inet_addr * ip,u8 prefix)189 hash_ip6_netmask(union nf_inet_addr *ip, u8 prefix)
190 {
191 ip6_netmask(ip, prefix);
192 }
193
194 static bool
hash_ip6_data_list(struct sk_buff * skb,const struct hash_ip6_elem * e)195 hash_ip6_data_list(struct sk_buff *skb, const struct hash_ip6_elem *e)
196 {
197 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6))
198 goto nla_put_failure;
199 return false;
200
201 nla_put_failure:
202 return true;
203 }
204
205 static void
hash_ip6_data_next(struct hash_ip6_elem * next,const struct hash_ip6_elem * e)206 hash_ip6_data_next(struct hash_ip6_elem *next, const struct hash_ip6_elem *e)
207 {
208 }
209
210 #undef MTYPE
211 #undef HOST_MASK
212
213 #define MTYPE hash_ip6
214 #define HOST_MASK 128
215
216 #define IP_SET_EMIT_CREATE
217 #include "ip_set_hash_gen.h"
218
219 static int
hash_ip6_kadt(struct ip_set * set,const struct sk_buff * skb,const struct xt_action_param * par,enum ipset_adt adt,struct ip_set_adt_opt * opt)220 hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
221 const struct xt_action_param *par,
222 enum ipset_adt adt, struct ip_set_adt_opt *opt)
223 {
224 const struct hash_ip6 *h = set->data;
225 ipset_adtfn adtfn = set->variant->adt[adt];
226 struct hash_ip6_elem e = { { .all = { 0 } } };
227 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
228
229 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
230 hash_ip6_netmask(&e.ip, h->netmask);
231 if (ipv6_addr_any(&e.ip.in6))
232 return -EINVAL;
233
234 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
235 }
236
237 static int
hash_ip6_uadt(struct ip_set * set,struct nlattr * tb[],enum ipset_adt adt,u32 * lineno,u32 flags,bool retried)238 hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
239 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
240 {
241 const struct hash_ip6 *h = set->data;
242 ipset_adtfn adtfn = set->variant->adt[adt];
243 struct hash_ip6_elem e = { { .all = { 0 } } };
244 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
245 int ret;
246
247 if (tb[IPSET_ATTR_LINENO])
248 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
249
250 if (unlikely(!tb[IPSET_ATTR_IP]))
251 return -IPSET_ERR_PROTOCOL;
252 if (unlikely(tb[IPSET_ATTR_IP_TO]))
253 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
254 if (unlikely(tb[IPSET_ATTR_CIDR])) {
255 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
256
257 if (cidr != HOST_MASK)
258 return -IPSET_ERR_INVALID_CIDR;
259 }
260
261 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
262 if (ret)
263 return ret;
264
265 ret = ip_set_get_extensions(set, tb, &ext);
266 if (ret)
267 return ret;
268
269 hash_ip6_netmask(&e.ip, h->netmask);
270 if (ipv6_addr_any(&e.ip.in6))
271 return -IPSET_ERR_HASH_ELEM;
272
273 ret = adtfn(set, &e, &ext, &ext, flags);
274
275 return ip_set_eexist(ret, flags) ? 0 : ret;
276 }
277
278 static struct ip_set_type hash_ip_type __read_mostly = {
279 .name = "hash:ip",
280 .protocol = IPSET_PROTOCOL,
281 .features = IPSET_TYPE_IP,
282 .dimension = IPSET_DIM_ONE,
283 .family = NFPROTO_UNSPEC,
284 .revision_min = IPSET_TYPE_REV_MIN,
285 .revision_max = IPSET_TYPE_REV_MAX,
286 .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
287 .create = hash_ip_create,
288 .create_policy = {
289 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
290 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
291 [IPSET_ATTR_INITVAL] = { .type = NLA_U32 },
292 [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
293 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
294 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
295 [IPSET_ATTR_NETMASK] = { .type = NLA_U8 },
296 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
297 },
298 .adt_policy = {
299 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
300 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
301 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
302 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
303 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
304 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
305 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
306 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING,
307 .len = IPSET_MAX_COMMENT_SIZE },
308 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
309 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
310 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
311 },
312 .me = THIS_MODULE,
313 };
314
315 static int __init
hash_ip_init(void)316 hash_ip_init(void)
317 {
318 return ip_set_type_register(&hash_ip_type);
319 }
320
321 static void __exit
hash_ip_fini(void)322 hash_ip_fini(void)
323 {
324 rcu_barrier();
325 ip_set_type_unregister(&hash_ip_type);
326 }
327
328 module_init(hash_ip_init);
329 module_exit(hash_ip_fini);
330