xref: /OK3568_Linux_fs/kernel/net/netfilter/ipset/ip_set_hash_netiface.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2011-2013 Jozsef Kadlecsik <kadlec@netfilter.org> */
3 
4 /* Kernel module implementing an IP set type: the hash:net,iface 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 
16 #include <linux/netfilter.h>
17 #include <linux/netfilter_bridge.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    nomatch flag support added */
24 /*				2    /0 support added */
25 /*				3    Counters support added */
26 /*				4    Comments support added */
27 /*				5    Forceadd support added */
28 /*				6    skbinfo support added */
29 #define IPSET_TYPE_REV_MAX	7 /* interface wildcard support added */
30 
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
33 IP_SET_MODULE_DESC("hash:net,iface", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34 MODULE_ALIAS("ip_set_hash:net,iface");
35 
36 /* Type specific function prefix */
37 #define HTYPE		hash_netiface
38 #define IP_SET_HASH_WITH_NETS
39 #define IP_SET_HASH_WITH_MULTI
40 #define IP_SET_HASH_WITH_NET0
41 
42 #define STRLCPY(a, b)	strlcpy(a, b, IFNAMSIZ)
43 
44 /* IPv4 variant */
45 
46 struct hash_netiface4_elem_hashed {
47 	__be32 ip;
48 	u8 physdev;
49 	u8 cidr;
50 	u8 nomatch;
51 	u8 elem;
52 };
53 
54 /* Member elements */
55 struct hash_netiface4_elem {
56 	__be32 ip;
57 	u8 physdev;
58 	u8 cidr;
59 	u8 nomatch;
60 	u8 elem;
61 	u8 wildcard;
62 	char iface[IFNAMSIZ];
63 };
64 
65 /* Common functions */
66 
67 static bool
hash_netiface4_data_equal(const struct hash_netiface4_elem * ip1,const struct hash_netiface4_elem * ip2,u32 * multi)68 hash_netiface4_data_equal(const struct hash_netiface4_elem *ip1,
69 			  const struct hash_netiface4_elem *ip2,
70 			  u32 *multi)
71 {
72 	return ip1->ip == ip2->ip &&
73 	       ip1->cidr == ip2->cidr &&
74 	       (++*multi) &&
75 	       ip1->physdev == ip2->physdev &&
76 	       (ip1->wildcard ?
77 		strncmp(ip1->iface, ip2->iface, strlen(ip1->iface)) == 0 :
78 		strcmp(ip1->iface, ip2->iface) == 0);
79 }
80 
81 static int
hash_netiface4_do_data_match(const struct hash_netiface4_elem * elem)82 hash_netiface4_do_data_match(const struct hash_netiface4_elem *elem)
83 {
84 	return elem->nomatch ? -ENOTEMPTY : 1;
85 }
86 
87 static void
hash_netiface4_data_set_flags(struct hash_netiface4_elem * elem,u32 flags)88 hash_netiface4_data_set_flags(struct hash_netiface4_elem *elem, u32 flags)
89 {
90 	elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
91 }
92 
93 static void
hash_netiface4_data_reset_flags(struct hash_netiface4_elem * elem,u8 * flags)94 hash_netiface4_data_reset_flags(struct hash_netiface4_elem *elem, u8 *flags)
95 {
96 	swap(*flags, elem->nomatch);
97 }
98 
99 static void
hash_netiface4_data_netmask(struct hash_netiface4_elem * elem,u8 cidr)100 hash_netiface4_data_netmask(struct hash_netiface4_elem *elem, u8 cidr)
101 {
102 	elem->ip &= ip_set_netmask(cidr);
103 	elem->cidr = cidr;
104 }
105 
106 static bool
hash_netiface4_data_list(struct sk_buff * skb,const struct hash_netiface4_elem * data)107 hash_netiface4_data_list(struct sk_buff *skb,
108 			 const struct hash_netiface4_elem *data)
109 {
110 	u32 flags = (data->physdev ? IPSET_FLAG_PHYSDEV : 0) |
111 		    (data->wildcard ? IPSET_FLAG_IFACE_WILDCARD : 0);
112 
113 	if (data->nomatch)
114 		flags |= IPSET_FLAG_NOMATCH;
115 	if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
116 	    nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
117 	    nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
118 	    (flags &&
119 	     nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
120 		goto nla_put_failure;
121 	return false;
122 
123 nla_put_failure:
124 	return true;
125 }
126 
127 static void
hash_netiface4_data_next(struct hash_netiface4_elem * next,const struct hash_netiface4_elem * d)128 hash_netiface4_data_next(struct hash_netiface4_elem *next,
129 			 const struct hash_netiface4_elem *d)
130 {
131 	next->ip = d->ip;
132 }
133 
134 #define MTYPE		hash_netiface4
135 #define HOST_MASK	32
136 #define HKEY_DATALEN	sizeof(struct hash_netiface4_elem_hashed)
137 #include "ip_set_hash_gen.h"
138 
139 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
get_physindev_name(const struct sk_buff * skb)140 static const char *get_physindev_name(const struct sk_buff *skb)
141 {
142 	struct net_device *dev = nf_bridge_get_physindev(skb);
143 
144 	return dev ? dev->name : NULL;
145 }
146 
get_physoutdev_name(const struct sk_buff * skb)147 static const char *get_physoutdev_name(const struct sk_buff *skb)
148 {
149 	struct net_device *dev = nf_bridge_get_physoutdev(skb);
150 
151 	return dev ? dev->name : NULL;
152 }
153 #endif
154 
155 static int
hash_netiface4_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)156 hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
157 		    const struct xt_action_param *par,
158 		    enum ipset_adt adt, struct ip_set_adt_opt *opt)
159 {
160 	struct hash_netiface4 *h = set->data;
161 	ipset_adtfn adtfn = set->variant->adt[adt];
162 	struct hash_netiface4_elem e = {
163 		.cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
164 		.elem = 1,
165 	};
166 	struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
167 
168 	if (adt == IPSET_TEST)
169 		e.cidr = HOST_MASK;
170 
171 	ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
172 	e.ip &= ip_set_netmask(e.cidr);
173 
174 #define IFACE(dir)	(par->state->dir ? par->state->dir->name : "")
175 #define SRCDIR		(opt->flags & IPSET_DIM_TWO_SRC)
176 
177 	if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
178 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
179 		const char *eiface = SRCDIR ? get_physindev_name(skb) :
180 					      get_physoutdev_name(skb);
181 
182 		if (!eiface)
183 			return -EINVAL;
184 		STRLCPY(e.iface, eiface);
185 		e.physdev = 1;
186 #endif
187 	} else {
188 		STRLCPY(e.iface, SRCDIR ? IFACE(in) : IFACE(out));
189 	}
190 
191 	if (strlen(e.iface) == 0)
192 		return -EINVAL;
193 	return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
194 }
195 
196 static int
hash_netiface4_uadt(struct ip_set * set,struct nlattr * tb[],enum ipset_adt adt,u32 * lineno,u32 flags,bool retried)197 hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
198 		    enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
199 {
200 	struct hash_netiface4 *h = set->data;
201 	ipset_adtfn adtfn = set->variant->adt[adt];
202 	struct hash_netiface4_elem e = { .cidr = HOST_MASK, .elem = 1 };
203 	struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
204 	u32 ip = 0, ip_to = 0, ipn, n = 0;
205 	int ret;
206 
207 	if (tb[IPSET_ATTR_LINENO])
208 		*lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
209 
210 	if (unlikely(!tb[IPSET_ATTR_IP] ||
211 		     !tb[IPSET_ATTR_IFACE] ||
212 		     !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
213 		return -IPSET_ERR_PROTOCOL;
214 
215 	ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
216 	if (ret)
217 		return ret;
218 
219 	ret = ip_set_get_extensions(set, tb, &ext);
220 	if (ret)
221 		return ret;
222 
223 	if (tb[IPSET_ATTR_CIDR]) {
224 		e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
225 		if (e.cidr > HOST_MASK)
226 			return -IPSET_ERR_INVALID_CIDR;
227 	}
228 	nla_strlcpy(e.iface, tb[IPSET_ATTR_IFACE], IFNAMSIZ);
229 
230 	if (tb[IPSET_ATTR_CADT_FLAGS]) {
231 		u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
232 
233 		if (cadt_flags & IPSET_FLAG_PHYSDEV)
234 			e.physdev = 1;
235 		if (cadt_flags & IPSET_FLAG_NOMATCH)
236 			flags |= (IPSET_FLAG_NOMATCH << 16);
237 		if (cadt_flags & IPSET_FLAG_IFACE_WILDCARD)
238 			e.wildcard = 1;
239 	}
240 	if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
241 		e.ip = htonl(ip & ip_set_hostmask(e.cidr));
242 		ret = adtfn(set, &e, &ext, &ext, flags);
243 		return ip_set_enomatch(ret, flags, adt, set) ? -ret :
244 		       ip_set_eexist(ret, flags) ? 0 : ret;
245 	}
246 
247 	if (tb[IPSET_ATTR_IP_TO]) {
248 		ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
249 		if (ret)
250 			return ret;
251 		if (ip_to < ip)
252 			swap(ip, ip_to);
253 		if (ip + UINT_MAX == ip_to)
254 			return -IPSET_ERR_HASH_RANGE;
255 	} else {
256 		ip_set_mask_from_to(ip, ip_to, e.cidr);
257 	}
258 	ipn = ip;
259 	do {
260 		ipn = ip_set_range_to_cidr(ipn, ip_to, &e.cidr);
261 		n++;
262 	} while (ipn++ < ip_to);
263 
264 	if (n > IPSET_MAX_RANGE)
265 		return -ERANGE;
266 
267 	if (retried)
268 		ip = ntohl(h->next.ip);
269 	do {
270 		e.ip = htonl(ip);
271 		ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
272 		ret = adtfn(set, &e, &ext, &ext, flags);
273 
274 		if (ret && !ip_set_eexist(ret, flags))
275 			return ret;
276 
277 		ret = 0;
278 	} while (ip++ < ip_to);
279 	return ret;
280 }
281 
282 /* IPv6 variant */
283 
284 struct hash_netiface6_elem_hashed {
285 	union nf_inet_addr ip;
286 	u8 physdev;
287 	u8 cidr;
288 	u8 nomatch;
289 	u8 elem;
290 };
291 
292 struct hash_netiface6_elem {
293 	union nf_inet_addr ip;
294 	u8 physdev;
295 	u8 cidr;
296 	u8 nomatch;
297 	u8 elem;
298 	u8 wildcard;
299 	char iface[IFNAMSIZ];
300 };
301 
302 /* Common functions */
303 
304 static bool
hash_netiface6_data_equal(const struct hash_netiface6_elem * ip1,const struct hash_netiface6_elem * ip2,u32 * multi)305 hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1,
306 			  const struct hash_netiface6_elem *ip2,
307 			  u32 *multi)
308 {
309 	return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
310 	       ip1->cidr == ip2->cidr &&
311 	       (++*multi) &&
312 	       ip1->physdev == ip2->physdev &&
313 	       (ip1->wildcard ?
314 		strncmp(ip1->iface, ip2->iface, strlen(ip1->iface)) == 0 :
315 		strcmp(ip1->iface, ip2->iface) == 0);
316 }
317 
318 static int
hash_netiface6_do_data_match(const struct hash_netiface6_elem * elem)319 hash_netiface6_do_data_match(const struct hash_netiface6_elem *elem)
320 {
321 	return elem->nomatch ? -ENOTEMPTY : 1;
322 }
323 
324 static void
hash_netiface6_data_set_flags(struct hash_netiface6_elem * elem,u32 flags)325 hash_netiface6_data_set_flags(struct hash_netiface6_elem *elem, u32 flags)
326 {
327 	elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
328 }
329 
330 static void
hash_netiface6_data_reset_flags(struct hash_netiface6_elem * elem,u8 * flags)331 hash_netiface6_data_reset_flags(struct hash_netiface6_elem *elem, u8 *flags)
332 {
333 	swap(*flags, elem->nomatch);
334 }
335 
336 static void
hash_netiface6_data_netmask(struct hash_netiface6_elem * elem,u8 cidr)337 hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr)
338 {
339 	ip6_netmask(&elem->ip, cidr);
340 	elem->cidr = cidr;
341 }
342 
343 static bool
hash_netiface6_data_list(struct sk_buff * skb,const struct hash_netiface6_elem * data)344 hash_netiface6_data_list(struct sk_buff *skb,
345 			 const struct hash_netiface6_elem *data)
346 {
347 	u32 flags = (data->physdev ? IPSET_FLAG_PHYSDEV : 0) |
348 		    (data->wildcard ? IPSET_FLAG_IFACE_WILDCARD : 0);
349 
350 	if (data->nomatch)
351 		flags |= IPSET_FLAG_NOMATCH;
352 	if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
353 	    nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
354 	    nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
355 	    (flags &&
356 	     nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
357 		goto nla_put_failure;
358 	return false;
359 
360 nla_put_failure:
361 	return true;
362 }
363 
364 static void
hash_netiface6_data_next(struct hash_netiface6_elem * next,const struct hash_netiface6_elem * d)365 hash_netiface6_data_next(struct hash_netiface6_elem *next,
366 			 const struct hash_netiface6_elem *d)
367 {
368 }
369 
370 #undef MTYPE
371 #undef HOST_MASK
372 
373 #define MTYPE		hash_netiface6
374 #define HOST_MASK	128
375 #define HKEY_DATALEN	sizeof(struct hash_netiface6_elem_hashed)
376 #define IP_SET_EMIT_CREATE
377 #include "ip_set_hash_gen.h"
378 
379 static int
hash_netiface6_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)380 hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
381 		    const struct xt_action_param *par,
382 		    enum ipset_adt adt, struct ip_set_adt_opt *opt)
383 {
384 	struct hash_netiface6 *h = set->data;
385 	ipset_adtfn adtfn = set->variant->adt[adt];
386 	struct hash_netiface6_elem e = {
387 		.cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
388 		.elem = 1,
389 	};
390 	struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
391 
392 	if (adt == IPSET_TEST)
393 		e.cidr = HOST_MASK;
394 
395 	ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
396 	ip6_netmask(&e.ip, e.cidr);
397 
398 	if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
399 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
400 		const char *eiface = SRCDIR ? get_physindev_name(skb) :
401 					      get_physoutdev_name(skb);
402 
403 		if (!eiface)
404 			return -EINVAL;
405 		STRLCPY(e.iface, eiface);
406 		e.physdev = 1;
407 #endif
408 	} else {
409 		STRLCPY(e.iface, SRCDIR ? IFACE(in) : IFACE(out));
410 	}
411 
412 	if (strlen(e.iface) == 0)
413 		return -EINVAL;
414 
415 	return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
416 }
417 
418 static int
hash_netiface6_uadt(struct ip_set * set,struct nlattr * tb[],enum ipset_adt adt,u32 * lineno,u32 flags,bool retried)419 hash_netiface6_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_netiface6_elem e = { .cidr = HOST_MASK, .elem = 1 };
424 	struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
425 	int ret;
426 
427 	if (tb[IPSET_ATTR_LINENO])
428 		*lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
429 
430 	if (unlikely(!tb[IPSET_ATTR_IP] ||
431 		     !tb[IPSET_ATTR_IFACE] ||
432 		     !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
433 		return -IPSET_ERR_PROTOCOL;
434 	if (unlikely(tb[IPSET_ATTR_IP_TO]))
435 		return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
436 
437 	ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
438 	if (ret)
439 		return ret;
440 
441 	ret = ip_set_get_extensions(set, tb, &ext);
442 	if (ret)
443 		return ret;
444 
445 	if (tb[IPSET_ATTR_CIDR]) {
446 		e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
447 		if (e.cidr > HOST_MASK)
448 			return -IPSET_ERR_INVALID_CIDR;
449 	}
450 
451 	ip6_netmask(&e.ip, e.cidr);
452 
453 	nla_strlcpy(e.iface, tb[IPSET_ATTR_IFACE], IFNAMSIZ);
454 
455 	if (tb[IPSET_ATTR_CADT_FLAGS]) {
456 		u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
457 
458 		if (cadt_flags & IPSET_FLAG_PHYSDEV)
459 			e.physdev = 1;
460 		if (cadt_flags & IPSET_FLAG_NOMATCH)
461 			flags |= (IPSET_FLAG_NOMATCH << 16);
462 		if (cadt_flags & IPSET_FLAG_IFACE_WILDCARD)
463 			e.wildcard = 1;
464 	}
465 
466 	ret = adtfn(set, &e, &ext, &ext, flags);
467 
468 	return ip_set_enomatch(ret, flags, adt, set) ? -ret :
469 	       ip_set_eexist(ret, flags) ? 0 : ret;
470 }
471 
472 static struct ip_set_type hash_netiface_type __read_mostly = {
473 	.name		= "hash:net,iface",
474 	.protocol	= IPSET_PROTOCOL,
475 	.features	= IPSET_TYPE_IP | IPSET_TYPE_IFACE |
476 			  IPSET_TYPE_NOMATCH,
477 	.dimension	= IPSET_DIM_TWO,
478 	.family		= NFPROTO_UNSPEC,
479 	.revision_min	= IPSET_TYPE_REV_MIN,
480 	.revision_max	= IPSET_TYPE_REV_MAX,
481 	.create		= hash_netiface_create,
482 	.create_policy	= {
483 		[IPSET_ATTR_HASHSIZE]	= { .type = NLA_U32 },
484 		[IPSET_ATTR_MAXELEM]	= { .type = NLA_U32 },
485 		[IPSET_ATTR_PROBES]	= { .type = NLA_U8 },
486 		[IPSET_ATTR_RESIZE]	= { .type = NLA_U8  },
487 		[IPSET_ATTR_PROTO]	= { .type = NLA_U8 },
488 		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
489 		[IPSET_ATTR_CADT_FLAGS]	= { .type = NLA_U32 },
490 	},
491 	.adt_policy	= {
492 		[IPSET_ATTR_IP]		= { .type = NLA_NESTED },
493 		[IPSET_ATTR_IP_TO]	= { .type = NLA_NESTED },
494 		[IPSET_ATTR_IFACE]	= { .type = NLA_NUL_STRING,
495 					    .len  = IFNAMSIZ - 1 },
496 		[IPSET_ATTR_CADT_FLAGS]	= { .type = NLA_U32 },
497 		[IPSET_ATTR_CIDR]	= { .type = NLA_U8 },
498 		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
499 		[IPSET_ATTR_LINENO]	= { .type = NLA_U32 },
500 		[IPSET_ATTR_BYTES]	= { .type = NLA_U64 },
501 		[IPSET_ATTR_PACKETS]	= { .type = NLA_U64 },
502 		[IPSET_ATTR_COMMENT]	= { .type = NLA_NUL_STRING,
503 					    .len  = IPSET_MAX_COMMENT_SIZE },
504 		[IPSET_ATTR_SKBMARK]	= { .type = NLA_U64 },
505 		[IPSET_ATTR_SKBPRIO]	= { .type = NLA_U32 },
506 		[IPSET_ATTR_SKBQUEUE]	= { .type = NLA_U16 },
507 	},
508 	.me		= THIS_MODULE,
509 };
510 
511 static int __init
hash_netiface_init(void)512 hash_netiface_init(void)
513 {
514 	return ip_set_type_register(&hash_netiface_type);
515 }
516 
517 static void __exit
hash_netiface_fini(void)518 hash_netiface_fini(void)
519 {
520 	rcu_barrier();
521 	ip_set_type_unregister(&hash_netiface_type);
522 }
523 
524 module_init(hash_netiface_init);
525 module_exit(hash_netiface_fini);
526