Linux 5.1.15
[linux/fpc-iii.git] / net / ipv6 / anycast.c
blobcca3b3603c425123011f76ef3a5cd516c2c5667f
1 /*
2 * Anycast support for IPv6
3 * Linux INET6 implementation
5 * Authors:
6 * David L Stevens (dlstevens@us.ibm.com)
8 * based heavily on net/ipv6/mcast.c
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <linux/capability.h>
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/types.h>
20 #include <linux/random.h>
21 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/net.h>
25 #include <linux/in6.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_arp.h>
28 #include <linux/route.h>
29 #include <linux/init.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/slab.h>
34 #include <net/net_namespace.h>
35 #include <net/sock.h>
36 #include <net/snmp.h>
38 #include <net/ipv6.h>
39 #include <net/protocol.h>
40 #include <net/if_inet6.h>
41 #include <net/ndisc.h>
42 #include <net/addrconf.h>
43 #include <net/ip6_route.h>
45 #include <net/checksum.h>
47 #define IN6_ADDR_HSIZE_SHIFT 8
48 #define IN6_ADDR_HSIZE BIT(IN6_ADDR_HSIZE_SHIFT)
49 /* anycast address hash table
51 static struct hlist_head inet6_acaddr_lst[IN6_ADDR_HSIZE];
52 static DEFINE_SPINLOCK(acaddr_hash_lock);
54 static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr);
56 static u32 inet6_acaddr_hash(struct net *net, const struct in6_addr *addr)
58 u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
60 return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
64 * socket join an anycast group
67 int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
69 struct ipv6_pinfo *np = inet6_sk(sk);
70 struct net_device *dev = NULL;
71 struct inet6_dev *idev;
72 struct ipv6_ac_socklist *pac;
73 struct net *net = sock_net(sk);
74 int ishost = !net->ipv6.devconf_all->forwarding;
75 int err = 0;
77 ASSERT_RTNL();
79 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
80 return -EPERM;
81 if (ipv6_addr_is_multicast(addr))
82 return -EINVAL;
84 if (ifindex)
85 dev = __dev_get_by_index(net, ifindex);
87 if (ipv6_chk_addr_and_flags(net, addr, dev, true, 0, IFA_F_TENTATIVE))
88 return -EINVAL;
90 pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL);
91 if (!pac)
92 return -ENOMEM;
93 pac->acl_next = NULL;
94 pac->acl_addr = *addr;
96 if (ifindex == 0) {
97 struct rt6_info *rt;
99 rt = rt6_lookup(net, addr, NULL, 0, NULL, 0);
100 if (rt) {
101 dev = rt->dst.dev;
102 ip6_rt_put(rt);
103 } else if (ishost) {
104 err = -EADDRNOTAVAIL;
105 goto error;
106 } else {
107 /* router, no matching interface: just pick one */
108 dev = __dev_get_by_flags(net, IFF_UP,
109 IFF_UP | IFF_LOOPBACK);
113 if (!dev) {
114 err = -ENODEV;
115 goto error;
118 idev = __in6_dev_get(dev);
119 if (!idev) {
120 if (ifindex)
121 err = -ENODEV;
122 else
123 err = -EADDRNOTAVAIL;
124 goto error;
126 /* reset ishost, now that we have a specific device */
127 ishost = !idev->cnf.forwarding;
129 pac->acl_ifindex = dev->ifindex;
131 /* XXX
132 * For hosts, allow link-local or matching prefix anycasts.
133 * This obviates the need for propagating anycast routes while
134 * still allowing some non-router anycast participation.
136 if (!ipv6_chk_prefix(addr, dev)) {
137 if (ishost)
138 err = -EADDRNOTAVAIL;
139 if (err)
140 goto error;
143 err = __ipv6_dev_ac_inc(idev, addr);
144 if (!err) {
145 pac->acl_next = np->ipv6_ac_list;
146 np->ipv6_ac_list = pac;
147 pac = NULL;
150 error:
151 if (pac)
152 sock_kfree_s(sk, pac, sizeof(*pac));
153 return err;
157 * socket leave an anycast group
159 int ipv6_sock_ac_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
161 struct ipv6_pinfo *np = inet6_sk(sk);
162 struct net_device *dev;
163 struct ipv6_ac_socklist *pac, *prev_pac;
164 struct net *net = sock_net(sk);
166 ASSERT_RTNL();
168 prev_pac = NULL;
169 for (pac = np->ipv6_ac_list; pac; pac = pac->acl_next) {
170 if ((ifindex == 0 || pac->acl_ifindex == ifindex) &&
171 ipv6_addr_equal(&pac->acl_addr, addr))
172 break;
173 prev_pac = pac;
175 if (!pac)
176 return -ENOENT;
177 if (prev_pac)
178 prev_pac->acl_next = pac->acl_next;
179 else
180 np->ipv6_ac_list = pac->acl_next;
182 dev = __dev_get_by_index(net, pac->acl_ifindex);
183 if (dev)
184 ipv6_dev_ac_dec(dev, &pac->acl_addr);
186 sock_kfree_s(sk, pac, sizeof(*pac));
187 return 0;
190 void ipv6_sock_ac_close(struct sock *sk)
192 struct ipv6_pinfo *np = inet6_sk(sk);
193 struct net_device *dev = NULL;
194 struct ipv6_ac_socklist *pac;
195 struct net *net = sock_net(sk);
196 int prev_index;
198 if (!np->ipv6_ac_list)
199 return;
201 rtnl_lock();
202 pac = np->ipv6_ac_list;
203 np->ipv6_ac_list = NULL;
205 prev_index = 0;
206 while (pac) {
207 struct ipv6_ac_socklist *next = pac->acl_next;
209 if (pac->acl_ifindex != prev_index) {
210 dev = __dev_get_by_index(net, pac->acl_ifindex);
211 prev_index = pac->acl_ifindex;
213 if (dev)
214 ipv6_dev_ac_dec(dev, &pac->acl_addr);
215 sock_kfree_s(sk, pac, sizeof(*pac));
216 pac = next;
218 rtnl_unlock();
221 static void ipv6_add_acaddr_hash(struct net *net, struct ifacaddr6 *aca)
223 unsigned int hash = inet6_acaddr_hash(net, &aca->aca_addr);
225 spin_lock(&acaddr_hash_lock);
226 hlist_add_head_rcu(&aca->aca_addr_lst, &inet6_acaddr_lst[hash]);
227 spin_unlock(&acaddr_hash_lock);
230 static void ipv6_del_acaddr_hash(struct ifacaddr6 *aca)
232 spin_lock(&acaddr_hash_lock);
233 hlist_del_init_rcu(&aca->aca_addr_lst);
234 spin_unlock(&acaddr_hash_lock);
237 static void aca_get(struct ifacaddr6 *aca)
239 refcount_inc(&aca->aca_refcnt);
242 static void aca_free_rcu(struct rcu_head *h)
244 struct ifacaddr6 *aca = container_of(h, struct ifacaddr6, rcu);
246 fib6_info_release(aca->aca_rt);
247 kfree(aca);
250 static void aca_put(struct ifacaddr6 *ac)
252 if (refcount_dec_and_test(&ac->aca_refcnt)) {
253 call_rcu(&ac->rcu, aca_free_rcu);
257 static struct ifacaddr6 *aca_alloc(struct fib6_info *f6i,
258 const struct in6_addr *addr)
260 struct ifacaddr6 *aca;
262 aca = kzalloc(sizeof(*aca), GFP_ATOMIC);
263 if (!aca)
264 return NULL;
266 aca->aca_addr = *addr;
267 fib6_info_hold(f6i);
268 aca->aca_rt = f6i;
269 INIT_HLIST_NODE(&aca->aca_addr_lst);
270 aca->aca_users = 1;
271 /* aca_tstamp should be updated upon changes */
272 aca->aca_cstamp = aca->aca_tstamp = jiffies;
273 refcount_set(&aca->aca_refcnt, 1);
275 return aca;
279 * device anycast group inc (add if not found)
281 int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr)
283 struct ifacaddr6 *aca;
284 struct fib6_info *f6i;
285 struct net *net;
286 int err;
288 ASSERT_RTNL();
290 write_lock_bh(&idev->lock);
291 if (idev->dead) {
292 err = -ENODEV;
293 goto out;
296 for (aca = idev->ac_list; aca; aca = aca->aca_next) {
297 if (ipv6_addr_equal(&aca->aca_addr, addr)) {
298 aca->aca_users++;
299 err = 0;
300 goto out;
304 net = dev_net(idev->dev);
305 f6i = addrconf_f6i_alloc(net, idev, addr, true, GFP_ATOMIC);
306 if (IS_ERR(f6i)) {
307 err = PTR_ERR(f6i);
308 goto out;
310 aca = aca_alloc(f6i, addr);
311 if (!aca) {
312 fib6_info_release(f6i);
313 err = -ENOMEM;
314 goto out;
317 aca->aca_next = idev->ac_list;
318 idev->ac_list = aca;
320 /* Hold this for addrconf_join_solict() below before we unlock,
321 * it is already exposed via idev->ac_list.
323 aca_get(aca);
324 write_unlock_bh(&idev->lock);
326 ipv6_add_acaddr_hash(net, aca);
328 ip6_ins_rt(net, f6i);
330 addrconf_join_solict(idev->dev, &aca->aca_addr);
332 aca_put(aca);
333 return 0;
334 out:
335 write_unlock_bh(&idev->lock);
336 return err;
340 * device anycast group decrement
342 int __ipv6_dev_ac_dec(struct inet6_dev *idev, const struct in6_addr *addr)
344 struct ifacaddr6 *aca, *prev_aca;
346 ASSERT_RTNL();
348 write_lock_bh(&idev->lock);
349 prev_aca = NULL;
350 for (aca = idev->ac_list; aca; aca = aca->aca_next) {
351 if (ipv6_addr_equal(&aca->aca_addr, addr))
352 break;
353 prev_aca = aca;
355 if (!aca) {
356 write_unlock_bh(&idev->lock);
357 return -ENOENT;
359 if (--aca->aca_users > 0) {
360 write_unlock_bh(&idev->lock);
361 return 0;
363 if (prev_aca)
364 prev_aca->aca_next = aca->aca_next;
365 else
366 idev->ac_list = aca->aca_next;
367 write_unlock_bh(&idev->lock);
368 ipv6_del_acaddr_hash(aca);
369 addrconf_leave_solict(idev, &aca->aca_addr);
371 ip6_del_rt(dev_net(idev->dev), aca->aca_rt);
373 aca_put(aca);
374 return 0;
377 /* called with rtnl_lock() */
378 static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr)
380 struct inet6_dev *idev = __in6_dev_get(dev);
382 if (!idev)
383 return -ENODEV;
384 return __ipv6_dev_ac_dec(idev, addr);
387 void ipv6_ac_destroy_dev(struct inet6_dev *idev)
389 struct ifacaddr6 *aca;
391 write_lock_bh(&idev->lock);
392 while ((aca = idev->ac_list) != NULL) {
393 idev->ac_list = aca->aca_next;
394 write_unlock_bh(&idev->lock);
396 ipv6_del_acaddr_hash(aca);
398 addrconf_leave_solict(idev, &aca->aca_addr);
400 ip6_del_rt(dev_net(idev->dev), aca->aca_rt);
402 aca_put(aca);
404 write_lock_bh(&idev->lock);
406 write_unlock_bh(&idev->lock);
410 * check if the interface has this anycast address
411 * called with rcu_read_lock()
413 static bool ipv6_chk_acast_dev(struct net_device *dev, const struct in6_addr *addr)
415 struct inet6_dev *idev;
416 struct ifacaddr6 *aca;
418 idev = __in6_dev_get(dev);
419 if (idev) {
420 read_lock_bh(&idev->lock);
421 for (aca = idev->ac_list; aca; aca = aca->aca_next)
422 if (ipv6_addr_equal(&aca->aca_addr, addr))
423 break;
424 read_unlock_bh(&idev->lock);
425 return aca != NULL;
427 return false;
431 * check if given interface (or any, if dev==0) has this anycast address
433 bool ipv6_chk_acast_addr(struct net *net, struct net_device *dev,
434 const struct in6_addr *addr)
436 struct net_device *nh_dev;
437 struct ifacaddr6 *aca;
438 bool found = false;
440 rcu_read_lock();
441 if (dev)
442 found = ipv6_chk_acast_dev(dev, addr);
443 else {
444 unsigned int hash = inet6_acaddr_hash(net, addr);
446 hlist_for_each_entry_rcu(aca, &inet6_acaddr_lst[hash],
447 aca_addr_lst) {
448 nh_dev = fib6_info_nh_dev(aca->aca_rt);
449 if (!nh_dev || !net_eq(dev_net(nh_dev), net))
450 continue;
451 if (ipv6_addr_equal(&aca->aca_addr, addr)) {
452 found = true;
453 break;
457 rcu_read_unlock();
458 return found;
461 /* check if this anycast address is link-local on given interface or
462 * is global
464 bool ipv6_chk_acast_addr_src(struct net *net, struct net_device *dev,
465 const struct in6_addr *addr)
467 return ipv6_chk_acast_addr(net,
468 (ipv6_addr_type(addr) & IPV6_ADDR_LINKLOCAL ?
469 dev : NULL),
470 addr);
473 #ifdef CONFIG_PROC_FS
474 struct ac6_iter_state {
475 struct seq_net_private p;
476 struct net_device *dev;
477 struct inet6_dev *idev;
480 #define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private)
482 static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
484 struct ifacaddr6 *im = NULL;
485 struct ac6_iter_state *state = ac6_seq_private(seq);
486 struct net *net = seq_file_net(seq);
488 state->idev = NULL;
489 for_each_netdev_rcu(net, state->dev) {
490 struct inet6_dev *idev;
491 idev = __in6_dev_get(state->dev);
492 if (!idev)
493 continue;
494 read_lock_bh(&idev->lock);
495 im = idev->ac_list;
496 if (im) {
497 state->idev = idev;
498 break;
500 read_unlock_bh(&idev->lock);
502 return im;
505 static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
507 struct ac6_iter_state *state = ac6_seq_private(seq);
509 im = im->aca_next;
510 while (!im) {
511 if (likely(state->idev != NULL))
512 read_unlock_bh(&state->idev->lock);
514 state->dev = next_net_device_rcu(state->dev);
515 if (!state->dev) {
516 state->idev = NULL;
517 break;
519 state->idev = __in6_dev_get(state->dev);
520 if (!state->idev)
521 continue;
522 read_lock_bh(&state->idev->lock);
523 im = state->idev->ac_list;
525 return im;
528 static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
530 struct ifacaddr6 *im = ac6_get_first(seq);
531 if (im)
532 while (pos && (im = ac6_get_next(seq, im)) != NULL)
533 --pos;
534 return pos ? NULL : im;
537 static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
538 __acquires(RCU)
540 rcu_read_lock();
541 return ac6_get_idx(seq, *pos);
544 static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
546 struct ifacaddr6 *im = ac6_get_next(seq, v);
548 ++*pos;
549 return im;
552 static void ac6_seq_stop(struct seq_file *seq, void *v)
553 __releases(RCU)
555 struct ac6_iter_state *state = ac6_seq_private(seq);
557 if (likely(state->idev != NULL)) {
558 read_unlock_bh(&state->idev->lock);
559 state->idev = NULL;
561 rcu_read_unlock();
564 static int ac6_seq_show(struct seq_file *seq, void *v)
566 struct ifacaddr6 *im = (struct ifacaddr6 *)v;
567 struct ac6_iter_state *state = ac6_seq_private(seq);
569 seq_printf(seq, "%-4d %-15s %pi6 %5d\n",
570 state->dev->ifindex, state->dev->name,
571 &im->aca_addr, im->aca_users);
572 return 0;
575 static const struct seq_operations ac6_seq_ops = {
576 .start = ac6_seq_start,
577 .next = ac6_seq_next,
578 .stop = ac6_seq_stop,
579 .show = ac6_seq_show,
582 int __net_init ac6_proc_init(struct net *net)
584 if (!proc_create_net("anycast6", 0444, net->proc_net, &ac6_seq_ops,
585 sizeof(struct ac6_iter_state)))
586 return -ENOMEM;
588 return 0;
591 void ac6_proc_exit(struct net *net)
593 remove_proc_entry("anycast6", net->proc_net);
595 #endif
597 /* Init / cleanup code
599 int __init ipv6_anycast_init(void)
601 int i;
603 for (i = 0; i < IN6_ADDR_HSIZE; i++)
604 INIT_HLIST_HEAD(&inet6_acaddr_lst[i]);
605 return 0;
608 void ipv6_anycast_cleanup(void)
610 int i;
612 spin_lock(&acaddr_hash_lock);
613 for (i = 0; i < IN6_ADDR_HSIZE; i++)
614 WARN_ON(!hlist_empty(&inet6_acaddr_lst[i]));
615 spin_unlock(&acaddr_hash_lock);