xref: /linux/net/ipv6/anycast.c (revision f7c595c9d9f4cce9ec335f0d3c5d875bb547f9d5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Anycast support for IPv6
4  *	Linux INET6 implementation
5  *
6  *	Authors:
7  *	David L Stevens (dlstevens@us.ibm.com)
8  *
9  *	based heavily on net/ipv6/mcast.c
10  */
11 
12 #include <linux/capability.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/random.h>
17 #include <linux/string.h>
18 #include <linux/socket.h>
19 #include <linux/sockios.h>
20 #include <linux/net.h>
21 #include <linux/in6.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_arp.h>
24 #include <linux/route.h>
25 #include <linux/init.h>
26 #include <linux/proc_fs.h>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
29 
30 #include <net/net_namespace.h>
31 #include <net/sock.h>
32 #include <net/snmp.h>
33 
34 #include <net/ipv6.h>
35 #include <net/protocol.h>
36 #include <net/if_inet6.h>
37 #include <net/ndisc.h>
38 #include <net/addrconf.h>
39 #include <net/ip6_route.h>
40 
41 #include <net/checksum.h>
42 
43 #define IN6_ADDR_HSIZE_SHIFT	8
44 #define IN6_ADDR_HSIZE		BIT(IN6_ADDR_HSIZE_SHIFT)
45 /*	anycast address hash table
46  */
47 static struct hlist_head inet6_acaddr_lst[IN6_ADDR_HSIZE];
48 static DEFINE_SPINLOCK(acaddr_hash_lock);
49 
50 #define ac_dereference(a, idev)						\
51 	rcu_dereference_protected(a, lockdep_is_held(&(idev)->lock))
52 
53 static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr);
54 
55 static u32 inet6_acaddr_hash(const struct net *net,
56 			     const struct in6_addr *addr)
57 {
58 	u32 val = __ipv6_addr_jhash(addr, net_hash_mix(net));
59 
60 	return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
61 }
62 
63 /*
64  *	socket join an anycast group
65  */
66 
67 int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
68 {
69 	struct ipv6_pinfo *np = inet6_sk(sk);
70 	struct ipv6_ac_socklist *pac = NULL;
71 	struct net *net = sock_net(sk);
72 	struct net_device *dev = NULL;
73 	struct inet6_dev *idev;
74 	int err = 0, ishost;
75 
76 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
77 		return -EPERM;
78 	if (ipv6_addr_is_multicast(addr))
79 		return -EINVAL;
80 
81 	if (ifindex)
82 		dev = dev_get_by_index(net, ifindex);
83 
84 	if (ipv6_chk_addr_and_flags(net, addr, dev, true, 0, IFA_F_TENTATIVE)) {
85 		err = -EINVAL;
86 		goto error;
87 	}
88 
89 	pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL);
90 	if (!pac) {
91 		err = -ENOMEM;
92 		goto error;
93 	}
94 
95 	pac->acl_next = NULL;
96 	pac->acl_addr = *addr;
97 
98 	ishost = !READ_ONCE(net->ipv6.devconf_all->forwarding);
99 
100 	if (ifindex == 0) {
101 		struct rt6_info *rt;
102 
103 		rcu_read_lock();
104 		rt = rt6_lookup(net, addr, NULL, 0, NULL, 0);
105 		if (rt) {
106 			dev = dst_dev(&rt->dst);
107 			dev_hold(dev);
108 			ip6_rt_put(rt);
109 		} else if (ishost) {
110 			rcu_read_unlock();
111 			err = -EADDRNOTAVAIL;
112 			goto error;
113 		} else {
114 			/* router, no matching interface: just pick one */
115 			dev = dev_get_by_flags_rcu(net, IFF_UP,
116 						   IFF_UP | IFF_LOOPBACK);
117 		}
118 		rcu_read_unlock();
119 	}
120 
121 	if (!dev) {
122 		err = -ENODEV;
123 		goto error;
124 	}
125 
126 	idev = in6_dev_get(dev);
127 	if (!idev) {
128 		if (ifindex)
129 			err = -ENODEV;
130 		else
131 			err = -EADDRNOTAVAIL;
132 		goto error;
133 	}
134 
135 	/* reset ishost, now that we have a specific device */
136 	ishost = !READ_ONCE(idev->cnf.forwarding);
137 
138 	pac->acl_ifindex = dev->ifindex;
139 
140 	/* XXX
141 	 * For hosts, allow link-local or matching prefix anycasts.
142 	 * This obviates the need for propagating anycast routes while
143 	 * still allowing some non-router anycast participation.
144 	 */
145 	if (!ipv6_chk_prefix(addr, dev)) {
146 		if (ishost)
147 			err = -EADDRNOTAVAIL;
148 		if (err)
149 			goto error_idev;
150 	}
151 
152 	err = __ipv6_dev_ac_inc(idev, addr);
153 	if (!err) {
154 		pac->acl_next = np->ipv6_ac_list;
155 		np->ipv6_ac_list = pac;
156 		pac = NULL;
157 	}
158 
159 error_idev:
160 	in6_dev_put(idev);
161 error:
162 	dev_put(dev);
163 
164 	if (pac)
165 		sock_kfree_s(sk, pac, sizeof(*pac));
166 	return err;
167 }
168 
169 /*
170  *	socket leave an anycast group
171  */
172 int ipv6_sock_ac_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
173 {
174 	struct ipv6_ac_socklist *pac, *prev_pac;
175 	struct ipv6_pinfo *np = inet6_sk(sk);
176 	struct net *net = sock_net(sk);
177 	struct net_device *dev;
178 
179 	prev_pac = NULL;
180 	for (pac = np->ipv6_ac_list; pac; pac = pac->acl_next) {
181 		if ((ifindex == 0 || pac->acl_ifindex == ifindex) &&
182 		     ipv6_addr_equal(&pac->acl_addr, addr))
183 			break;
184 		prev_pac = pac;
185 	}
186 	if (!pac)
187 		return -ENOENT;
188 	if (prev_pac)
189 		prev_pac->acl_next = pac->acl_next;
190 	else
191 		np->ipv6_ac_list = pac->acl_next;
192 
193 	dev = dev_get_by_index(net, pac->acl_ifindex);
194 	if (dev) {
195 		ipv6_dev_ac_dec(dev, &pac->acl_addr);
196 		dev_put(dev);
197 	}
198 
199 	sock_kfree_s(sk, pac, sizeof(*pac));
200 	return 0;
201 }
202 
203 void __ipv6_sock_ac_close(struct sock *sk)
204 {
205 	struct ipv6_pinfo *np = inet6_sk(sk);
206 	struct net *net = sock_net(sk);
207 	struct net_device *dev = NULL;
208 	struct ipv6_ac_socklist *pac;
209 	int prev_index = 0;
210 
211 	pac = np->ipv6_ac_list;
212 	np->ipv6_ac_list = NULL;
213 
214 	while (pac) {
215 		struct ipv6_ac_socklist *next = pac->acl_next;
216 
217 		if (pac->acl_ifindex != prev_index) {
218 			dev_put(dev);
219 			dev = dev_get_by_index(net, pac->acl_ifindex);
220 			prev_index = pac->acl_ifindex;
221 		}
222 		if (dev)
223 			ipv6_dev_ac_dec(dev, &pac->acl_addr);
224 		sock_kfree_s(sk, pac, sizeof(*pac));
225 		pac = next;
226 	}
227 
228 	dev_put(dev);
229 }
230 
231 void ipv6_sock_ac_close(struct sock *sk)
232 {
233 	struct ipv6_pinfo *np = inet6_sk(sk);
234 
235 	if (!np->ipv6_ac_list)
236 		return;
237 
238 	__ipv6_sock_ac_close(sk);
239 }
240 
241 static void ipv6_add_acaddr_hash(struct net *net, struct ifacaddr6 *aca)
242 {
243 	unsigned int hash = inet6_acaddr_hash(net, &aca->aca_addr);
244 
245 	spin_lock(&acaddr_hash_lock);
246 	hlist_add_head_rcu(&aca->aca_addr_lst, &inet6_acaddr_lst[hash]);
247 	spin_unlock(&acaddr_hash_lock);
248 }
249 
250 static void ipv6_del_acaddr_hash(struct ifacaddr6 *aca)
251 {
252 	spin_lock(&acaddr_hash_lock);
253 	hlist_del_init_rcu(&aca->aca_addr_lst);
254 	spin_unlock(&acaddr_hash_lock);
255 }
256 
257 static void aca_get(struct ifacaddr6 *aca)
258 {
259 	refcount_inc(&aca->aca_refcnt);
260 }
261 
262 static void aca_free_rcu(struct rcu_head *h)
263 {
264 	struct ifacaddr6 *aca = container_of(h, struct ifacaddr6, rcu);
265 
266 	fib6_info_release(aca->aca_rt);
267 	kfree(aca);
268 }
269 
270 static void aca_put(struct ifacaddr6 *ac)
271 {
272 	if (refcount_dec_and_test(&ac->aca_refcnt))
273 		call_rcu_hurry(&ac->rcu, aca_free_rcu);
274 }
275 
276 static struct ifacaddr6 *aca_alloc(struct fib6_info *f6i,
277 				   const struct in6_addr *addr)
278 {
279 	struct ifacaddr6 *aca;
280 
281 	aca = kzalloc(sizeof(*aca), GFP_ATOMIC);
282 	if (!aca)
283 		return NULL;
284 
285 	aca->aca_addr = *addr;
286 	fib6_info_hold(f6i);
287 	aca->aca_rt = f6i;
288 	INIT_HLIST_NODE(&aca->aca_addr_lst);
289 	aca->aca_users = 1;
290 	/* aca_tstamp should be updated upon changes */
291 	aca->aca_cstamp = aca->aca_tstamp = jiffies;
292 	refcount_set(&aca->aca_refcnt, 1);
293 
294 	return aca;
295 }
296 
297 static void inet6_ifacaddr_notify(struct net_device *dev,
298 				  const struct ifacaddr6 *ifaca, int event)
299 {
300 	struct inet6_fill_args fillargs = {
301 		.event = event,
302 		.netnsid = -1,
303 	};
304 	struct net *net = dev_net(dev);
305 	struct sk_buff *skb;
306 	int err = -ENOMEM;
307 
308 	skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
309 			nla_total_size(sizeof(struct in6_addr)) +
310 			nla_total_size(sizeof(struct ifa_cacheinfo)),
311 			GFP_KERNEL);
312 	if (!skb)
313 		goto error;
314 
315 	err = inet6_fill_ifacaddr(skb, ifaca, &fillargs);
316 	if (err < 0) {
317 		pr_err("Failed to fill in anycast addresses (err %d)\n", err);
318 		nlmsg_free(skb);
319 		goto error;
320 	}
321 
322 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ACADDR, NULL, GFP_KERNEL);
323 	return;
324 error:
325 	rtnl_set_sk_err(net, RTNLGRP_IPV6_ACADDR, err);
326 }
327 
328 /*
329  *	device anycast group inc (add if not found)
330  */
331 int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr)
332 {
333 	struct ifacaddr6 *aca;
334 	struct fib6_info *f6i;
335 	struct net *net;
336 	int err;
337 
338 	write_lock_bh(&idev->lock);
339 	if (idev->dead) {
340 		err = -ENODEV;
341 		goto out;
342 	}
343 
344 	for (aca = ac_dereference(idev->ac_list, idev); aca;
345 	     aca = ac_dereference(aca->aca_next, idev)) {
346 		if (ipv6_addr_equal(&aca->aca_addr, addr)) {
347 			aca->aca_users++;
348 			err = 0;
349 			goto out;
350 		}
351 	}
352 
353 	net = dev_net(idev->dev);
354 	f6i = addrconf_f6i_alloc(net, idev, addr, true, GFP_ATOMIC, NULL);
355 	if (IS_ERR(f6i)) {
356 		err = PTR_ERR(f6i);
357 		goto out;
358 	}
359 	aca = aca_alloc(f6i, addr);
360 	if (!aca) {
361 		fib6_info_release(f6i);
362 		err = -ENOMEM;
363 		goto out;
364 	}
365 
366 	/* Hold this for addrconf_join_solict() below before we unlock,
367 	 * it is already exposed via idev->ac_list.
368 	 */
369 	aca_get(aca);
370 	aca->aca_next = idev->ac_list;
371 	rcu_assign_pointer(idev->ac_list, aca);
372 
373 	write_unlock_bh(&idev->lock);
374 
375 	ipv6_add_acaddr_hash(net, aca);
376 
377 	ip6_ins_rt(net, f6i);
378 
379 	addrconf_join_solict(idev->dev, &aca->aca_addr);
380 
381 	inet6_ifacaddr_notify(idev->dev, aca, RTM_NEWANYCAST);
382 
383 	aca_put(aca);
384 	return 0;
385 out:
386 	write_unlock_bh(&idev->lock);
387 	return err;
388 }
389 
390 /*
391  *	device anycast group decrement
392  */
393 int __ipv6_dev_ac_dec(struct inet6_dev *idev, const struct in6_addr *addr)
394 {
395 	struct ifacaddr6 *aca, *prev_aca;
396 
397 	write_lock_bh(&idev->lock);
398 	prev_aca = NULL;
399 	for (aca = ac_dereference(idev->ac_list, idev); aca;
400 	     aca = ac_dereference(aca->aca_next, idev)) {
401 		if (ipv6_addr_equal(&aca->aca_addr, addr))
402 			break;
403 		prev_aca = aca;
404 	}
405 	if (!aca) {
406 		write_unlock_bh(&idev->lock);
407 		return -ENOENT;
408 	}
409 	if (--aca->aca_users > 0) {
410 		write_unlock_bh(&idev->lock);
411 		return 0;
412 	}
413 	if (prev_aca)
414 		rcu_assign_pointer(prev_aca->aca_next, aca->aca_next);
415 	else
416 		rcu_assign_pointer(idev->ac_list, aca->aca_next);
417 	write_unlock_bh(&idev->lock);
418 	ipv6_del_acaddr_hash(aca);
419 	addrconf_leave_solict(idev, &aca->aca_addr);
420 
421 	ip6_del_rt(dev_net(idev->dev), aca->aca_rt, false);
422 
423 	inet6_ifacaddr_notify(idev->dev, aca, RTM_DELANYCAST);
424 
425 	aca_put(aca);
426 	return 0;
427 }
428 
429 static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr)
430 {
431 	struct inet6_dev *idev = in6_dev_get(dev);
432 	int err;
433 
434 	if (!idev)
435 		return -ENODEV;
436 
437 	err = __ipv6_dev_ac_dec(idev, addr);
438 	in6_dev_put(idev);
439 
440 	return err;
441 }
442 
443 void ipv6_ac_destroy_dev(struct inet6_dev *idev)
444 {
445 	struct ifacaddr6 *aca;
446 
447 	write_lock_bh(&idev->lock);
448 	while ((aca = ac_dereference(idev->ac_list, idev)) != NULL) {
449 		rcu_assign_pointer(idev->ac_list, aca->aca_next);
450 		write_unlock_bh(&idev->lock);
451 
452 		ipv6_del_acaddr_hash(aca);
453 
454 		addrconf_leave_solict(idev, &aca->aca_addr);
455 
456 		ip6_del_rt(dev_net(idev->dev), aca->aca_rt, false);
457 
458 		aca_put(aca);
459 
460 		write_lock_bh(&idev->lock);
461 	}
462 	write_unlock_bh(&idev->lock);
463 }
464 
465 /*
466  *	check if the interface has this anycast address
467  *	called with rcu_read_lock()
468  */
469 static bool ipv6_chk_acast_dev(struct net_device *dev, const struct in6_addr *addr)
470 {
471 	struct inet6_dev *idev;
472 	struct ifacaddr6 *aca;
473 
474 	idev = __in6_dev_get(dev);
475 	if (idev) {
476 		for (aca = rcu_dereference(idev->ac_list); aca;
477 		     aca = rcu_dereference(aca->aca_next))
478 			if (ipv6_addr_equal(&aca->aca_addr, addr))
479 				break;
480 		return aca != NULL;
481 	}
482 	return false;
483 }
484 
485 /*
486  *	check if given interface (or any, if dev==0) has this anycast address
487  */
488 bool ipv6_chk_acast_addr(struct net *net, struct net_device *dev,
489 			 const struct in6_addr *addr)
490 {
491 	struct net_device *nh_dev;
492 	struct ifacaddr6 *aca;
493 	bool found = false;
494 
495 	rcu_read_lock();
496 	if (dev)
497 		found = ipv6_chk_acast_dev(dev, addr);
498 	else {
499 		unsigned int hash = inet6_acaddr_hash(net, addr);
500 
501 		hlist_for_each_entry_rcu(aca, &inet6_acaddr_lst[hash],
502 					 aca_addr_lst) {
503 			nh_dev = fib6_info_nh_dev(aca->aca_rt);
504 			if (!nh_dev || !net_eq(dev_net(nh_dev), net))
505 				continue;
506 			if (ipv6_addr_equal(&aca->aca_addr, addr)) {
507 				found = true;
508 				break;
509 			}
510 		}
511 	}
512 	rcu_read_unlock();
513 	return found;
514 }
515 
516 /*	check if this anycast address is link-local on given interface or
517  *	is global
518  */
519 bool ipv6_chk_acast_addr_src(struct net *net, struct net_device *dev,
520 			     const struct in6_addr *addr)
521 {
522 	return ipv6_chk_acast_addr(net,
523 				   (ipv6_addr_type(addr) & IPV6_ADDR_LINKLOCAL ?
524 				    dev : NULL),
525 				   addr);
526 }
527 
528 #ifdef CONFIG_PROC_FS
529 struct ac6_iter_state {
530 	struct seq_net_private p;
531 	struct net_device *dev;
532 };
533 
534 #define ac6_seq_private(seq)	((struct ac6_iter_state *)(seq)->private)
535 
536 static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
537 {
538 	struct ac6_iter_state *state = ac6_seq_private(seq);
539 	struct net *net = seq_file_net(seq);
540 	struct ifacaddr6 *im = NULL;
541 
542 	for_each_netdev_rcu(net, state->dev) {
543 		struct inet6_dev *idev;
544 
545 		idev = __in6_dev_get(state->dev);
546 		if (!idev)
547 			continue;
548 		im = rcu_dereference(idev->ac_list);
549 		if (im)
550 			break;
551 	}
552 	return im;
553 }
554 
555 static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
556 {
557 	struct ac6_iter_state *state = ac6_seq_private(seq);
558 	struct inet6_dev *idev;
559 
560 	im = rcu_dereference(im->aca_next);
561 	while (!im) {
562 		state->dev = next_net_device_rcu(state->dev);
563 		if (!state->dev)
564 			break;
565 		idev = __in6_dev_get(state->dev);
566 		if (!idev)
567 			continue;
568 		im = rcu_dereference(idev->ac_list);
569 	}
570 	return im;
571 }
572 
573 static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
574 {
575 	struct ifacaddr6 *im = ac6_get_first(seq);
576 	if (im)
577 		while (pos && (im = ac6_get_next(seq, im)) != NULL)
578 			--pos;
579 	return pos ? NULL : im;
580 }
581 
582 static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
583 	__acquires(RCU)
584 {
585 	rcu_read_lock();
586 	return ac6_get_idx(seq, *pos);
587 }
588 
589 static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
590 {
591 	struct ifacaddr6 *im = ac6_get_next(seq, v);
592 
593 	++*pos;
594 	return im;
595 }
596 
597 static void ac6_seq_stop(struct seq_file *seq, void *v)
598 	__releases(RCU)
599 {
600 	rcu_read_unlock();
601 }
602 
603 static int ac6_seq_show(struct seq_file *seq, void *v)
604 {
605 	struct ifacaddr6 *im = (struct ifacaddr6 *)v;
606 	struct ac6_iter_state *state = ac6_seq_private(seq);
607 
608 	seq_printf(seq, "%-4d %-15s %pi6 %5d\n",
609 		   state->dev->ifindex, state->dev->name,
610 		   &im->aca_addr, im->aca_users);
611 	return 0;
612 }
613 
614 static const struct seq_operations ac6_seq_ops = {
615 	.start	=	ac6_seq_start,
616 	.next	=	ac6_seq_next,
617 	.stop	=	ac6_seq_stop,
618 	.show	=	ac6_seq_show,
619 };
620 
621 int __net_init ac6_proc_init(struct net *net)
622 {
623 	if (!proc_create_net("anycast6", 0444, net->proc_net, &ac6_seq_ops,
624 			sizeof(struct ac6_iter_state)))
625 		return -ENOMEM;
626 
627 	return 0;
628 }
629 
630 void ac6_proc_exit(struct net *net)
631 {
632 	remove_proc_entry("anycast6", net->proc_net);
633 }
634 #endif
635 
636 /*	Init / cleanup code
637  */
638 int __init ipv6_anycast_init(void)
639 {
640 	int i;
641 
642 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
643 		INIT_HLIST_HEAD(&inet6_acaddr_lst[i]);
644 	return 0;
645 }
646 
647 void ipv6_anycast_cleanup(void)
648 {
649 	int i;
650 
651 	spin_lock(&acaddr_hash_lock);
652 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
653 		WARN_ON(!hlist_empty(&inet6_acaddr_lst[i]));
654 	spin_unlock(&acaddr_hash_lock);
655 }
656