1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org>
3  * Copyright (C) 2013 Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>
4  */
5 
6 /* Kernel module implementing an IP set type: the hash:net type */
7 
8 #include <linux/jhash.h>
9 #include <linux/module.h>
10 #include <linux/ip.h>
11 #include <linux/skbuff.h>
12 #include <linux/errno.h>
13 #include <linux/random.h>
14 #include <net/ip.h>
15 #include <net/ipv6.h>
16 #include <net/netlink.h>
17 
18 #include <linux/netfilter.h>
19 #include <linux/netfilter/ipset/pfxlen.h>
20 #include <linux/netfilter/ipset/ip_set.h>
21 #include <linux/netfilter/ipset/ip_set_hash.h>
22 
23 #define IPSET_TYPE_REV_MIN	0
24 /*				1	   Forceadd support added */
25 /*				2	   skbinfo support added */
26 /*				3	   bucketsize, initval support added */
27 #define IPSET_TYPE_REV_MAX	4	/* bitmask support added */
28 
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
31 IP_SET_MODULE_DESC("hash:net,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
32 MODULE_ALIAS("ip_set_hash:net,net");
33 
34 /* Type specific function prefix */
35 #define HTYPE		hash_netnet
36 #define IP_SET_HASH_WITH_NETS
37 #define IP_SET_HASH_WITH_NETMASK
38 #define IP_SET_HASH_WITH_BITMASK
39 #define IPSET_NET_COUNT 2
40 
41 /* IPv4 variants */
42 
43 /* Member elements  */
44 struct hash_netnet4_elem {
45 	union {
46 		__be32 ip[2];
47 		__be64 ipcmp;
48 	};
49 	u8 nomatch;
50 	u8 padding;
51 	union {
52 		u8 cidr[2];
53 		u16 ccmp;
54 	};
55 };
56 
57 /* Common functions */
58 
59 static bool
60 hash_netnet4_data_equal(const struct hash_netnet4_elem *ip1,
61 			const struct hash_netnet4_elem *ip2,
62 			u32 *multi)
63 {
64 	return ip1->ipcmp == ip2->ipcmp &&
65 	       ip1->ccmp == ip2->ccmp;
66 }
67 
68 static int
69 hash_netnet4_do_data_match(const struct hash_netnet4_elem *elem)
70 {
71 	return elem->nomatch ? -ENOTEMPTY : 1;
72 }
73 
74 static void
75 hash_netnet4_data_set_flags(struct hash_netnet4_elem *elem, u32 flags)
76 {
77 	elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
78 }
79 
80 static void
81 hash_netnet4_data_reset_flags(struct hash_netnet4_elem *elem, u8 *flags)
82 {
83 	swap(*flags, elem->nomatch);
84 }
85 
86 static void
87 hash_netnet4_data_reset_elem(struct hash_netnet4_elem *elem,
88 			     struct hash_netnet4_elem *orig)
89 {
90 	elem->ip[1] = orig->ip[1];
91 }
92 
93 static void
94 hash_netnet4_data_netmask(struct hash_netnet4_elem *elem, u8 cidr, bool inner)
95 {
96 	if (inner) {
97 		elem->ip[1] &= ip_set_netmask(cidr);
98 		elem->cidr[1] = cidr;
99 	} else {
100 		elem->ip[0] &= ip_set_netmask(cidr);
101 		elem->cidr[0] = cidr;
102 	}
103 }
104 
105 static bool
106 hash_netnet4_data_list(struct sk_buff *skb,
107 		       const struct hash_netnet4_elem *data)
108 {
109 	u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
110 
111 	if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
112 	    nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
113 	    nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
114 	    nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
115 	    (flags &&
116 	     nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
117 		goto nla_put_failure;
118 	return false;
119 
120 nla_put_failure:
121 	return true;
122 }
123 
124 static void
125 hash_netnet4_data_next(struct hash_netnet4_elem *next,
126 		       const struct hash_netnet4_elem *d)
127 {
128 	next->ipcmp = d->ipcmp;
129 }
130 
131 #define MTYPE		hash_netnet4
132 #define HOST_MASK	32
133 #include "ip_set_hash_gen.h"
134 
135 static void
136 hash_netnet4_init(struct hash_netnet4_elem *e)
137 {
138 	e->cidr[0] = HOST_MASK;
139 	e->cidr[1] = HOST_MASK;
140 }
141 
142 static int
143 hash_netnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
144 		  const struct xt_action_param *par,
145 		  enum ipset_adt adt, struct ip_set_adt_opt *opt)
146 {
147 	const struct hash_netnet4 *h = set->data;
148 	ipset_adtfn adtfn = set->variant->adt[adt];
149 	struct hash_netnet4_elem e = { };
150 	struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
151 
152 	e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
153 	e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
154 	if (adt == IPSET_TEST)
155 		e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
156 
157 	ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
158 	ip4addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1]);
159 	e.ip[0] &= (ip_set_netmask(e.cidr[0]) & h->bitmask.ip);
160 	e.ip[1] &= (ip_set_netmask(e.cidr[1]) & h->bitmask.ip);
161 
162 	return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
163 }
164 
165 static int
166 hash_netnet4_uadt(struct ip_set *set, struct nlattr *tb[],
167 		  enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
168 {
169 	const struct hash_netnet4 *h = set->data;
170 	ipset_adtfn adtfn = set->variant->adt[adt];
171 	struct hash_netnet4_elem e = { };
172 	struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
173 	u32 ip = 0, ip_to = 0;
174 	u32 ip2 = 0, ip2_from = 0, ip2_to = 0, ipn;
175 	u64 n = 0, m = 0;
176 	int ret;
177 
178 	if (tb[IPSET_ATTR_LINENO])
179 		*lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
180 
181 	hash_netnet4_init(&e);
182 	if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
183 		     !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
184 		return -IPSET_ERR_PROTOCOL;
185 
186 	ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
187 	if (ret)
188 		return ret;
189 
190 	ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from);
191 	if (ret)
192 		return ret;
193 
194 	ret = ip_set_get_extensions(set, tb, &ext);
195 	if (ret)
196 		return ret;
197 
198 	if (tb[IPSET_ATTR_CIDR]) {
199 		e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
200 		if (!e.cidr[0] || e.cidr[0] > HOST_MASK)
201 			return -IPSET_ERR_INVALID_CIDR;
202 	}
203 
204 	if (tb[IPSET_ATTR_CIDR2]) {
205 		e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
206 		if (!e.cidr[1] || e.cidr[1] > HOST_MASK)
207 			return -IPSET_ERR_INVALID_CIDR;
208 	}
209 
210 	if (tb[IPSET_ATTR_CADT_FLAGS]) {
211 		u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
212 
213 		if (cadt_flags & IPSET_FLAG_NOMATCH)
214 			flags |= (IPSET_FLAG_NOMATCH << 16);
215 	}
216 
217 	if (adt == IPSET_TEST || !(tb[IPSET_ATTR_IP_TO] ||
218 				   tb[IPSET_ATTR_IP2_TO])) {
219 		e.ip[0] = htonl(ip & ntohl(h->bitmask.ip) & ip_set_hostmask(e.cidr[0]));
220 		e.ip[1] = htonl(ip2_from & ntohl(h->bitmask.ip) & ip_set_hostmask(e.cidr[1]));
221 		ret = adtfn(set, &e, &ext, &ext, flags);
222 		return ip_set_enomatch(ret, flags, adt, set) ? -ret :
223 		       ip_set_eexist(ret, flags) ? 0 : ret;
224 	}
225 
226 	ip_to = ip;
227 	if (tb[IPSET_ATTR_IP_TO]) {
228 		ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
229 		if (ret)
230 			return ret;
231 		if (ip_to < ip)
232 			swap(ip, ip_to);
233 		if (unlikely(ip + UINT_MAX == ip_to))
234 			return -IPSET_ERR_HASH_RANGE;
235 	} else {
236 		ip_set_mask_from_to(ip, ip_to, e.cidr[0]);
237 	}
238 
239 	ip2_to = ip2_from;
240 	if (tb[IPSET_ATTR_IP2_TO]) {
241 		ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
242 		if (ret)
243 			return ret;
244 		if (ip2_to < ip2_from)
245 			swap(ip2_from, ip2_to);
246 		if (unlikely(ip2_from + UINT_MAX == ip2_to))
247 			return -IPSET_ERR_HASH_RANGE;
248 	} else {
249 		ip_set_mask_from_to(ip2_from, ip2_to, e.cidr[1]);
250 	}
251 	ipn = ip;
252 	do {
253 		ipn = ip_set_range_to_cidr(ipn, ip_to, &e.cidr[0]);
254 		n++;
255 	} while (ipn++ < ip_to);
256 	ipn = ip2_from;
257 	do {
258 		ipn = ip_set_range_to_cidr(ipn, ip2_to, &e.cidr[1]);
259 		m++;
260 	} while (ipn++ < ip2_to);
261 
262 	if (n*m > IPSET_MAX_RANGE)
263 		return -ERANGE;
264 
265 	if (retried) {
266 		ip = ntohl(h->next.ip[0]);
267 		ip2 = ntohl(h->next.ip[1]);
268 	} else {
269 		ip2 = ip2_from;
270 	}
271 
272 	do {
273 		e.ip[0] = htonl(ip);
274 		ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr[0]);
275 		do {
276 			e.ip[1] = htonl(ip2);
277 			ip2 = ip_set_range_to_cidr(ip2, ip2_to, &e.cidr[1]);
278 			ret = adtfn(set, &e, &ext, &ext, flags);
279 			if (ret && !ip_set_eexist(ret, flags))
280 				return ret;
281 
282 			ret = 0;
283 		} while (ip2++ < ip2_to);
284 		ip2 = ip2_from;
285 	} while (ip++ < ip_to);
286 	return ret;
287 }
288 
289 /* IPv6 variants */
290 
291 struct hash_netnet6_elem {
292 	union nf_inet_addr ip[2];
293 	u8 nomatch;
294 	u8 padding;
295 	union {
296 		u8 cidr[2];
297 		u16 ccmp;
298 	};
299 };
300 
301 /* Common functions */
302 
303 static bool
304 hash_netnet6_data_equal(const struct hash_netnet6_elem *ip1,
305 			const struct hash_netnet6_elem *ip2,
306 			u32 *multi)
307 {
308 	return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
309 	       ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
310 	       ip1->ccmp == ip2->ccmp;
311 }
312 
313 static int
314 hash_netnet6_do_data_match(const struct hash_netnet6_elem *elem)
315 {
316 	return elem->nomatch ? -ENOTEMPTY : 1;
317 }
318 
319 static void
320 hash_netnet6_data_set_flags(struct hash_netnet6_elem *elem, u32 flags)
321 {
322 	elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
323 }
324 
325 static void
326 hash_netnet6_data_reset_flags(struct hash_netnet6_elem *elem, u8 *flags)
327 {
328 	swap(*flags, elem->nomatch);
329 }
330 
331 static void
332 hash_netnet6_data_reset_elem(struct hash_netnet6_elem *elem,
333 			     struct hash_netnet6_elem *orig)
334 {
335 	elem->ip[1] = orig->ip[1];
336 }
337 
338 static void
339 hash_netnet6_data_netmask(struct hash_netnet6_elem *elem, u8 cidr, bool inner)
340 {
341 	if (inner) {
342 		ip6_netmask(&elem->ip[1], cidr);
343 		elem->cidr[1] = cidr;
344 	} else {
345 		ip6_netmask(&elem->ip[0], cidr);
346 		elem->cidr[0] = cidr;
347 	}
348 }
349 
350 static bool
351 hash_netnet6_data_list(struct sk_buff *skb,
352 		       const struct hash_netnet6_elem *data)
353 {
354 	u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
355 
356 	if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
357 	    nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
358 	    nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
359 	    nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
360 	    (flags &&
361 	     nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
362 		goto nla_put_failure;
363 	return false;
364 
365 nla_put_failure:
366 	return true;
367 }
368 
369 static void
370 hash_netnet6_data_next(struct hash_netnet6_elem *next,
371 		       const struct hash_netnet6_elem *d)
372 {
373 }
374 
375 #undef MTYPE
376 #undef HOST_MASK
377 
378 #define MTYPE		hash_netnet6
379 #define HOST_MASK	128
380 #define IP_SET_EMIT_CREATE
381 #include "ip_set_hash_gen.h"
382 
383 static void
384 hash_netnet6_init(struct hash_netnet6_elem *e)
385 {
386 	e->cidr[0] = HOST_MASK;
387 	e->cidr[1] = HOST_MASK;
388 }
389 
390 static int
391 hash_netnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
392 		  const struct xt_action_param *par,
393 		  enum ipset_adt adt, struct ip_set_adt_opt *opt)
394 {
395 	const struct hash_netnet6 *h = set->data;
396 	ipset_adtfn adtfn = set->variant->adt[adt];
397 	struct hash_netnet6_elem e = { };
398 	struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
399 
400 	e.cidr[0] = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
401 	e.cidr[1] = INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
402 	if (adt == IPSET_TEST)
403 		e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
404 
405 	ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
406 	ip6addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1].in6);
407 	ip6_netmask(&e.ip[0], e.cidr[0]);
408 	ip6_netmask(&e.ip[1], e.cidr[1]);
409 
410 	nf_inet_addr_mask_inplace(&e.ip[0], &h->bitmask);
411 	nf_inet_addr_mask_inplace(&e.ip[1], &h->bitmask);
412 	if (e.cidr[0] == HOST_MASK && ipv6_addr_any(&e.ip[0].in6))
413 		return -EINVAL;
414 
415 	return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
416 }
417 
418 static int
419 hash_netnet6_uadt(struct ip_set *set, struct nlattr *tb[],
420 		  enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
421 {
422 	ipset_adtfn adtfn = set->variant->adt[adt];
423 	struct hash_netnet6_elem e = { };
424 	struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
425 	const struct hash_netnet6 *h = set->data;
426 	int ret;
427 
428 	if (tb[IPSET_ATTR_LINENO])
429 		*lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
430 
431 	hash_netnet6_init(&e);
432 	if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
433 		     !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
434 		return -IPSET_ERR_PROTOCOL;
435 	if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
436 		return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
437 
438 	ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]);
439 	if (ret)
440 		return ret;
441 
442 	ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]);
443 	if (ret)
444 		return ret;
445 
446 	ret = ip_set_get_extensions(set, tb, &ext);
447 	if (ret)
448 		return ret;
449 
450 	if (tb[IPSET_ATTR_CIDR]) {
451 		e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
452 		if (!e.cidr[0] || e.cidr[0] > HOST_MASK)
453 			return -IPSET_ERR_INVALID_CIDR;
454 	}
455 
456 	if (tb[IPSET_ATTR_CIDR2]) {
457 		e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
458 		if (!e.cidr[1] || e.cidr[1] > HOST_MASK)
459 			return -IPSET_ERR_INVALID_CIDR;
460 	}
461 
462 	ip6_netmask(&e.ip[0], e.cidr[0]);
463 	ip6_netmask(&e.ip[1], e.cidr[1]);
464 
465 	nf_inet_addr_mask_inplace(&e.ip[0], &h->bitmask);
466 	nf_inet_addr_mask_inplace(&e.ip[1], &h->bitmask);
467 	if (e.cidr[0] == HOST_MASK && ipv6_addr_any(&e.ip[0].in6))
468 		return -IPSET_ERR_HASH_ELEM;
469 
470 	if (tb[IPSET_ATTR_CADT_FLAGS]) {
471 		u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
472 
473 		if (cadt_flags & IPSET_FLAG_NOMATCH)
474 			flags |= (IPSET_FLAG_NOMATCH << 16);
475 	}
476 
477 	ret = adtfn(set, &e, &ext, &ext, flags);
478 
479 	return ip_set_enomatch(ret, flags, adt, set) ? -ret :
480 	       ip_set_eexist(ret, flags) ? 0 : ret;
481 }
482 
483 static struct ip_set_type hash_netnet_type __read_mostly = {
484 	.name		= "hash:net,net",
485 	.protocol	= IPSET_PROTOCOL,
486 	.features	= IPSET_TYPE_IP | IPSET_TYPE_IP2 | IPSET_TYPE_NOMATCH,
487 	.dimension	= IPSET_DIM_TWO,
488 	.family		= NFPROTO_UNSPEC,
489 	.revision_min	= IPSET_TYPE_REV_MIN,
490 	.revision_max	= IPSET_TYPE_REV_MAX,
491 	.create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
492 	.create		= hash_netnet_create,
493 	.create_policy	= {
494 		[IPSET_ATTR_HASHSIZE]	= { .type = NLA_U32 },
495 		[IPSET_ATTR_MAXELEM]	= { .type = NLA_U32 },
496 		[IPSET_ATTR_INITVAL]	= { .type = NLA_U32 },
497 		[IPSET_ATTR_BUCKETSIZE]	= { .type = NLA_U8 },
498 		[IPSET_ATTR_RESIZE]	= { .type = NLA_U8  },
499 		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
500 		[IPSET_ATTR_CADT_FLAGS]	= { .type = NLA_U32 },
501 		[IPSET_ATTR_NETMASK]    = { .type = NLA_U8 },
502 		[IPSET_ATTR_BITMASK]	= { .type = NLA_NESTED },
503 	},
504 	.adt_policy	= {
505 		[IPSET_ATTR_IP]		= { .type = NLA_NESTED },
506 		[IPSET_ATTR_IP_TO]	= { .type = NLA_NESTED },
507 		[IPSET_ATTR_IP2]	= { .type = NLA_NESTED },
508 		[IPSET_ATTR_IP2_TO]	= { .type = NLA_NESTED },
509 		[IPSET_ATTR_CIDR]	= { .type = NLA_U8 },
510 		[IPSET_ATTR_CIDR2]	= { .type = NLA_U8 },
511 		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
512 		[IPSET_ATTR_LINENO]	= { .type = NLA_U32 },
513 		[IPSET_ATTR_CADT_FLAGS]	= { .type = NLA_U32 },
514 		[IPSET_ATTR_BYTES]	= { .type = NLA_U64 },
515 		[IPSET_ATTR_PACKETS]	= { .type = NLA_U64 },
516 		[IPSET_ATTR_COMMENT]	= { .type = NLA_NUL_STRING,
517 					    .len  = IPSET_MAX_COMMENT_SIZE },
518 		[IPSET_ATTR_SKBMARK]	= { .type = NLA_U64 },
519 		[IPSET_ATTR_SKBPRIO]	= { .type = NLA_U32 },
520 		[IPSET_ATTR_SKBQUEUE]	= { .type = NLA_U16 },
521 	},
522 	.me		= THIS_MODULE,
523 };
524 
525 static int __init
526 hash_netnet_init(void)
527 {
528 	return ip_set_type_register(&hash_netnet_type);
529 }
530 
531 static void __exit
532 hash_netnet_fini(void)
533 {
534 	rcu_barrier();
535 	ip_set_type_unregister(&hash_netnet_type);
536 }
537 
538 module_init(hash_netnet_init);
539 module_exit(hash_netnet_fini);
540