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