perf bpf: Move perf_event_output() from stdio.h to bpf.h
[linux/fpc-iii.git] / net / ipv6 / anycast.c
blob94999058e11029b637b6ab8201f8706599e49284
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 unsigned int hash = inet6_acaddr_hash(net, addr);
437 struct net_device *nh_dev;
438 struct ifacaddr6 *aca;
439 bool found = false;
441 rcu_read_lock();
442 if (dev)
443 found = ipv6_chk_acast_dev(dev, addr);
444 else
445 hlist_for_each_entry_rcu(aca, &inet6_acaddr_lst[hash],
446 aca_addr_lst) {
447 nh_dev = fib6_info_nh_dev(aca->aca_rt);
448 if (!nh_dev || !net_eq(dev_net(nh_dev), net))
449 continue;
450 if (ipv6_addr_equal(&aca->aca_addr, addr)) {
451 found = true;
452 break;
455 rcu_read_unlock();
456 return found;
459 /* check if this anycast address is link-local on given interface or
460 * is global
462 bool ipv6_chk_acast_addr_src(struct net *net, struct net_device *dev,
463 const struct in6_addr *addr)
465 return ipv6_chk_acast_addr(net,
466 (ipv6_addr_type(addr) & IPV6_ADDR_LINKLOCAL ?
467 dev : NULL),
468 addr);
471 #ifdef CONFIG_PROC_FS
472 struct ac6_iter_state {
473 struct seq_net_private p;
474 struct net_device *dev;
475 struct inet6_dev *idev;
478 #define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private)
480 static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
482 struct ifacaddr6 *im = NULL;
483 struct ac6_iter_state *state = ac6_seq_private(seq);
484 struct net *net = seq_file_net(seq);
486 state->idev = NULL;
487 for_each_netdev_rcu(net, state->dev) {
488 struct inet6_dev *idev;
489 idev = __in6_dev_get(state->dev);
490 if (!idev)
491 continue;
492 read_lock_bh(&idev->lock);
493 im = idev->ac_list;
494 if (im) {
495 state->idev = idev;
496 break;
498 read_unlock_bh(&idev->lock);
500 return im;
503 static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
505 struct ac6_iter_state *state = ac6_seq_private(seq);
507 im = im->aca_next;
508 while (!im) {
509 if (likely(state->idev != NULL))
510 read_unlock_bh(&state->idev->lock);
512 state->dev = next_net_device_rcu(state->dev);
513 if (!state->dev) {
514 state->idev = NULL;
515 break;
517 state->idev = __in6_dev_get(state->dev);
518 if (!state->idev)
519 continue;
520 read_lock_bh(&state->idev->lock);
521 im = state->idev->ac_list;
523 return im;
526 static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
528 struct ifacaddr6 *im = ac6_get_first(seq);
529 if (im)
530 while (pos && (im = ac6_get_next(seq, im)) != NULL)
531 --pos;
532 return pos ? NULL : im;
535 static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
536 __acquires(RCU)
538 rcu_read_lock();
539 return ac6_get_idx(seq, *pos);
542 static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
544 struct ifacaddr6 *im = ac6_get_next(seq, v);
546 ++*pos;
547 return im;
550 static void ac6_seq_stop(struct seq_file *seq, void *v)
551 __releases(RCU)
553 struct ac6_iter_state *state = ac6_seq_private(seq);
555 if (likely(state->idev != NULL)) {
556 read_unlock_bh(&state->idev->lock);
557 state->idev = NULL;
559 rcu_read_unlock();
562 static int ac6_seq_show(struct seq_file *seq, void *v)
564 struct ifacaddr6 *im = (struct ifacaddr6 *)v;
565 struct ac6_iter_state *state = ac6_seq_private(seq);
567 seq_printf(seq, "%-4d %-15s %pi6 %5d\n",
568 state->dev->ifindex, state->dev->name,
569 &im->aca_addr, im->aca_users);
570 return 0;
573 static const struct seq_operations ac6_seq_ops = {
574 .start = ac6_seq_start,
575 .next = ac6_seq_next,
576 .stop = ac6_seq_stop,
577 .show = ac6_seq_show,
580 int __net_init ac6_proc_init(struct net *net)
582 if (!proc_create_net("anycast6", 0444, net->proc_net, &ac6_seq_ops,
583 sizeof(struct ac6_iter_state)))
584 return -ENOMEM;
586 return 0;
589 void ac6_proc_exit(struct net *net)
591 remove_proc_entry("anycast6", net->proc_net);
593 #endif
595 /* Init / cleanup code
597 int __init ipv6_anycast_init(void)
599 int i;
601 for (i = 0; i < IN6_ADDR_HSIZE; i++)
602 INIT_HLIST_HEAD(&inet6_acaddr_lst[i]);
603 return 0;
606 void ipv6_anycast_cleanup(void)
608 int i;
610 spin_lock(&acaddr_hash_lock);
611 for (i = 0; i < IN6_ADDR_HSIZE; i++)
612 WARN_ON(!hlist_empty(&inet6_acaddr_lst[i]));
613 spin_unlock(&acaddr_hash_lock);