• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, i = 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 
259 	if (retried)
260 		ip = ntohl(h->next.ip);
261 	do {
262 		i++;
263 		e.ip = htonl(ip);
264 		if (i > IPSET_MAX_RANGE) {
265 			hash_netiface4_data_next(&h->next, &e);
266 			return -ERANGE;
267 		}
268 		ip = ip_set_range_to_cidr(ip, ip_to, &e.cidr);
269 		ret = adtfn(set, &e, &ext, &ext, flags);
270 
271 		if (ret && !ip_set_eexist(ret, flags))
272 			return ret;
273 
274 		ret = 0;
275 	} while (ip++ < ip_to);
276 	return ret;
277 }
278 
279 /* IPv6 variant */
280 
281 struct hash_netiface6_elem_hashed {
282 	union nf_inet_addr ip;
283 	u8 physdev;
284 	u8 cidr;
285 	u8 nomatch;
286 	u8 elem;
287 };
288 
289 struct hash_netiface6_elem {
290 	union nf_inet_addr ip;
291 	u8 physdev;
292 	u8 cidr;
293 	u8 nomatch;
294 	u8 elem;
295 	u8 wildcard;
296 	char iface[IFNAMSIZ];
297 };
298 
299 /* Common functions */
300 
301 static bool
hash_netiface6_data_equal(const struct hash_netiface6_elem * ip1,const struct hash_netiface6_elem * ip2,u32 * multi)302 hash_netiface6_data_equal(const struct hash_netiface6_elem *ip1,
303 			  const struct hash_netiface6_elem *ip2,
304 			  u32 *multi)
305 {
306 	return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
307 	       ip1->cidr == ip2->cidr &&
308 	       (++*multi) &&
309 	       ip1->physdev == ip2->physdev &&
310 	       (ip1->wildcard ?
311 		strncmp(ip1->iface, ip2->iface, strlen(ip1->iface)) == 0 :
312 		strcmp(ip1->iface, ip2->iface) == 0);
313 }
314 
315 static int
hash_netiface6_do_data_match(const struct hash_netiface6_elem * elem)316 hash_netiface6_do_data_match(const struct hash_netiface6_elem *elem)
317 {
318 	return elem->nomatch ? -ENOTEMPTY : 1;
319 }
320 
321 static void
hash_netiface6_data_set_flags(struct hash_netiface6_elem * elem,u32 flags)322 hash_netiface6_data_set_flags(struct hash_netiface6_elem *elem, u32 flags)
323 {
324 	elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
325 }
326 
327 static void
hash_netiface6_data_reset_flags(struct hash_netiface6_elem * elem,u8 * flags)328 hash_netiface6_data_reset_flags(struct hash_netiface6_elem *elem, u8 *flags)
329 {
330 	swap(*flags, elem->nomatch);
331 }
332 
333 static void
hash_netiface6_data_netmask(struct hash_netiface6_elem * elem,u8 cidr)334 hash_netiface6_data_netmask(struct hash_netiface6_elem *elem, u8 cidr)
335 {
336 	ip6_netmask(&elem->ip, cidr);
337 	elem->cidr = cidr;
338 }
339 
340 static bool
hash_netiface6_data_list(struct sk_buff * skb,const struct hash_netiface6_elem * data)341 hash_netiface6_data_list(struct sk_buff *skb,
342 			 const struct hash_netiface6_elem *data)
343 {
344 	u32 flags = (data->physdev ? IPSET_FLAG_PHYSDEV : 0) |
345 		    (data->wildcard ? IPSET_FLAG_IFACE_WILDCARD : 0);
346 
347 	if (data->nomatch)
348 		flags |= IPSET_FLAG_NOMATCH;
349 	if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
350 	    nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
351 	    nla_put_string(skb, IPSET_ATTR_IFACE, data->iface) ||
352 	    (flags &&
353 	     nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
354 		goto nla_put_failure;
355 	return false;
356 
357 nla_put_failure:
358 	return true;
359 }
360 
361 static void
hash_netiface6_data_next(struct hash_netiface6_elem * next,const struct hash_netiface6_elem * d)362 hash_netiface6_data_next(struct hash_netiface6_elem *next,
363 			 const struct hash_netiface6_elem *d)
364 {
365 }
366 
367 #undef MTYPE
368 #undef HOST_MASK
369 
370 #define MTYPE		hash_netiface6
371 #define HOST_MASK	128
372 #define HKEY_DATALEN	sizeof(struct hash_netiface6_elem_hashed)
373 #define IP_SET_EMIT_CREATE
374 #include "ip_set_hash_gen.h"
375 
376 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)377 hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
378 		    const struct xt_action_param *par,
379 		    enum ipset_adt adt, struct ip_set_adt_opt *opt)
380 {
381 	struct hash_netiface6 *h = set->data;
382 	ipset_adtfn adtfn = set->variant->adt[adt];
383 	struct hash_netiface6_elem e = {
384 		.cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
385 		.elem = 1,
386 	};
387 	struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
388 
389 	if (adt == IPSET_TEST)
390 		e.cidr = HOST_MASK;
391 
392 	ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
393 	ip6_netmask(&e.ip, e.cidr);
394 
395 	if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
396 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
397 		const char *eiface = SRCDIR ? get_physindev_name(skb) :
398 					      get_physoutdev_name(skb);
399 
400 		if (!eiface)
401 			return -EINVAL;
402 		STRLCPY(e.iface, eiface);
403 		e.physdev = 1;
404 #endif
405 	} else {
406 		STRLCPY(e.iface, SRCDIR ? IFACE(in) : IFACE(out));
407 	}
408 
409 	if (strlen(e.iface) == 0)
410 		return -EINVAL;
411 
412 	return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
413 }
414 
415 static int
hash_netiface6_uadt(struct ip_set * set,struct nlattr * tb[],enum ipset_adt adt,u32 * lineno,u32 flags,bool retried)416 hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],
417 		    enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
418 {
419 	ipset_adtfn adtfn = set->variant->adt[adt];
420 	struct hash_netiface6_elem e = { .cidr = HOST_MASK, .elem = 1 };
421 	struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
422 	int ret;
423 
424 	if (tb[IPSET_ATTR_LINENO])
425 		*lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
426 
427 	if (unlikely(!tb[IPSET_ATTR_IP] ||
428 		     !tb[IPSET_ATTR_IFACE] ||
429 		     !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
430 		return -IPSET_ERR_PROTOCOL;
431 	if (unlikely(tb[IPSET_ATTR_IP_TO]))
432 		return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
433 
434 	ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
435 	if (ret)
436 		return ret;
437 
438 	ret = ip_set_get_extensions(set, tb, &ext);
439 	if (ret)
440 		return ret;
441 
442 	if (tb[IPSET_ATTR_CIDR]) {
443 		e.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
444 		if (e.cidr > HOST_MASK)
445 			return -IPSET_ERR_INVALID_CIDR;
446 	}
447 
448 	ip6_netmask(&e.ip, e.cidr);
449 
450 	nla_strlcpy(e.iface, tb[IPSET_ATTR_IFACE], IFNAMSIZ);
451 
452 	if (tb[IPSET_ATTR_CADT_FLAGS]) {
453 		u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
454 
455 		if (cadt_flags & IPSET_FLAG_PHYSDEV)
456 			e.physdev = 1;
457 		if (cadt_flags & IPSET_FLAG_NOMATCH)
458 			flags |= (IPSET_FLAG_NOMATCH << 16);
459 		if (cadt_flags & IPSET_FLAG_IFACE_WILDCARD)
460 			e.wildcard = 1;
461 	}
462 
463 	ret = adtfn(set, &e, &ext, &ext, flags);
464 
465 	return ip_set_enomatch(ret, flags, adt, set) ? -ret :
466 	       ip_set_eexist(ret, flags) ? 0 : ret;
467 }
468 
469 static struct ip_set_type hash_netiface_type __read_mostly = {
470 	.name		= "hash:net,iface",
471 	.protocol	= IPSET_PROTOCOL,
472 	.features	= IPSET_TYPE_IP | IPSET_TYPE_IFACE |
473 			  IPSET_TYPE_NOMATCH,
474 	.dimension	= IPSET_DIM_TWO,
475 	.family		= NFPROTO_UNSPEC,
476 	.revision_min	= IPSET_TYPE_REV_MIN,
477 	.revision_max	= IPSET_TYPE_REV_MAX,
478 	.create		= hash_netiface_create,
479 	.create_policy	= {
480 		[IPSET_ATTR_HASHSIZE]	= { .type = NLA_U32 },
481 		[IPSET_ATTR_MAXELEM]	= { .type = NLA_U32 },
482 		[IPSET_ATTR_PROBES]	= { .type = NLA_U8 },
483 		[IPSET_ATTR_RESIZE]	= { .type = NLA_U8  },
484 		[IPSET_ATTR_PROTO]	= { .type = NLA_U8 },
485 		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
486 		[IPSET_ATTR_CADT_FLAGS]	= { .type = NLA_U32 },
487 	},
488 	.adt_policy	= {
489 		[IPSET_ATTR_IP]		= { .type = NLA_NESTED },
490 		[IPSET_ATTR_IP_TO]	= { .type = NLA_NESTED },
491 		[IPSET_ATTR_IFACE]	= { .type = NLA_NUL_STRING,
492 					    .len  = IFNAMSIZ - 1 },
493 		[IPSET_ATTR_CADT_FLAGS]	= { .type = NLA_U32 },
494 		[IPSET_ATTR_CIDR]	= { .type = NLA_U8 },
495 		[IPSET_ATTR_TIMEOUT]	= { .type = NLA_U32 },
496 		[IPSET_ATTR_LINENO]	= { .type = NLA_U32 },
497 		[IPSET_ATTR_BYTES]	= { .type = NLA_U64 },
498 		[IPSET_ATTR_PACKETS]	= { .type = NLA_U64 },
499 		[IPSET_ATTR_COMMENT]	= { .type = NLA_NUL_STRING,
500 					    .len  = IPSET_MAX_COMMENT_SIZE },
501 		[IPSET_ATTR_SKBMARK]	= { .type = NLA_U64 },
502 		[IPSET_ATTR_SKBPRIO]	= { .type = NLA_U32 },
503 		[IPSET_ATTR_SKBQUEUE]	= { .type = NLA_U16 },
504 	},
505 	.me		= THIS_MODULE,
506 };
507 
508 static int __init
hash_netiface_init(void)509 hash_netiface_init(void)
510 {
511 	return ip_set_type_register(&hash_netiface_type);
512 }
513 
514 static void __exit
hash_netiface_fini(void)515 hash_netiface_fini(void)
516 {
517 	rcu_barrier();
518 	ip_set_type_unregister(&hash_netiface_type);
519 }
520 
521 module_init(hash_netiface_init);
522 module_exit(hash_netiface_fini);
523