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