2 * Anycast support for IPv6
3 * Linux INET6 implementation
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/config.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/sched.h>
25 #include <linux/net.h>
26 #include <linux/in6.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/route.h>
30 #include <linux/init.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
38 #include <net/protocol.h>
39 #include <net/if_inet6.h>
40 #include <net/ndisc.h>
41 #include <net/addrconf.h>
42 #include <net/ip6_route.h>
44 #include <net/checksum.h>
46 /* Big ac list lock for all the sockets */
47 static rwlock_t ipv6_sk_ac_lock = RW_LOCK_UNLOCKED;
49 /* XXX ip6_addr_match() and ip6_onlink() really belong in net/core.c */
52 ip6_addr_match(struct in6_addr *addr1, struct in6_addr *addr2, int prefix)
57 if (prefix > 128 || prefix < 0)
65 mask = htonl(~0 << (32 - prefix));
66 if ((addr1->s6_addr32[i] ^ addr2->s6_addr32[i]) & mask)
76 ip6_onlink(struct in6_addr *addr, struct net_device *dev)
78 struct inet6_dev *idev;
79 struct inet6_ifaddr *ifa;
83 read_lock(&addrconf_lock);
84 idev = __in6_dev_get(dev);
86 read_lock_bh(&idev->lock);
87 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
88 onlink = ip6_addr_match(addr, &ifa->addr,
93 read_unlock_bh(&idev->lock);
95 read_unlock(&addrconf_lock);
100 * socket join an anycast group
103 int ipv6_sock_ac_join(struct sock *sk, int ifindex, struct in6_addr *addr)
105 struct ipv6_pinfo *np = inet6_sk(sk);
106 struct net_device *dev = NULL;
107 struct inet6_dev *idev;
108 struct ipv6_ac_socklist *pac;
109 int ishost = !ipv6_devconf.forwarding;
112 if (!capable(CAP_NET_ADMIN))
114 if (ipv6_addr_is_multicast(addr))
116 if (ipv6_chk_addr(addr, NULL, 0))
119 pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL);
122 pac->acl_next = NULL;
123 ipv6_addr_copy(&pac->acl_addr, addr);
128 rt = rt6_lookup(addr, NULL, 0, 0);
132 dst_release(&rt->u.dst);
134 err = -EADDRNOTAVAIL;
137 /* router, no matching interface: just pick one */
139 dev = dev_get_by_flags(IFF_UP, IFF_UP|IFF_LOOPBACK);
142 dev = dev_get_by_index(ifindex);
149 idev = in6_dev_get(dev);
154 err = -EADDRNOTAVAIL;
157 /* reset ishost, now that we have a specific device */
158 ishost = !idev->cnf.forwarding;
161 pac->acl_ifindex = dev->ifindex;
164 * For hosts, allow link-local or matching prefix anycasts.
165 * This obviates the need for propagating anycast routes while
166 * still allowing some non-router anycast participation.
168 if (!ip6_onlink(addr, dev)) {
170 err = -EADDRNOTAVAIL;
175 err = ipv6_dev_ac_inc(dev, addr);
179 write_lock_bh(&ipv6_sk_ac_lock);
180 pac->acl_next = np->ipv6_ac_list;
181 np->ipv6_ac_list = pac;
182 write_unlock_bh(&ipv6_sk_ac_lock);
191 sock_kfree_s(sk, pac, sizeof(*pac));
196 * socket leave an anycast group
198 int ipv6_sock_ac_drop(struct sock *sk, int ifindex, struct in6_addr *addr)
200 struct ipv6_pinfo *np = inet6_sk(sk);
201 struct net_device *dev;
202 struct ipv6_ac_socklist *pac, *prev_pac;
204 write_lock_bh(&ipv6_sk_ac_lock);
206 for (pac = np->ipv6_ac_list; pac; pac = pac->acl_next) {
207 if ((ifindex == 0 || pac->acl_ifindex == ifindex) &&
208 ipv6_addr_cmp(&pac->acl_addr, addr) == 0)
213 write_unlock_bh(&ipv6_sk_ac_lock);
217 prev_pac->acl_next = pac->acl_next;
219 np->ipv6_ac_list = pac->acl_next;
221 write_unlock_bh(&ipv6_sk_ac_lock);
223 dev = dev_get_by_index(pac->acl_ifindex);
225 ipv6_dev_ac_dec(dev, &pac->acl_addr);
228 sock_kfree_s(sk, pac, sizeof(*pac));
232 void ipv6_sock_ac_close(struct sock *sk)
234 struct ipv6_pinfo *np = inet6_sk(sk);
235 struct net_device *dev = NULL;
236 struct ipv6_ac_socklist *pac;
239 write_lock_bh(&ipv6_sk_ac_lock);
240 pac = np->ipv6_ac_list;
241 np->ipv6_ac_list = NULL;
242 write_unlock_bh(&ipv6_sk_ac_lock);
246 struct ipv6_ac_socklist *next = pac->acl_next;
248 if (pac->acl_ifindex != prev_index) {
251 dev = dev_get_by_index(pac->acl_ifindex);
252 prev_index = pac->acl_ifindex;
255 ipv6_dev_ac_dec(dev, &pac->acl_addr);
256 sock_kfree_s(sk, pac, sizeof(*pac));
264 /* The function is not used, which is funny. Apparently, author
265 * supposed to use it to filter out datagrams inside udp/raw but forgot.
267 * It is OK, anycasts are not special comparing to delivery to unicasts.
270 int inet6_ac_check(struct sock *sk, struct in6_addr *addr, int ifindex)
272 struct ipv6_ac_socklist *pac;
273 struct ipv6_pinfo *np = inet6_sk(sk);
277 read_lock(&ipv6_sk_ac_lock);
278 for (pac=np->ipv6_ac_list; pac; pac=pac->acl_next) {
279 if (ifindex && pac->acl_ifindex != ifindex)
281 found = ipv6_addr_cmp(&pac->acl_addr, addr) == 0;
285 read_unlock(&ipv6_sk_ac_lock);
292 static void aca_put(struct ifacaddr6 *ac)
294 if (atomic_dec_and_test(&ac->aca_refcnt)) {
295 in6_dev_put(ac->aca_idev);
296 dst_release(&ac->aca_rt->u.dst);
302 * device anycast group inc (add if not found)
304 int ipv6_dev_ac_inc(struct net_device *dev, struct in6_addr *addr)
306 struct ifacaddr6 *aca;
307 struct inet6_dev *idev;
311 idev = in6_dev_get(dev);
316 write_lock_bh(&idev->lock);
322 for (aca = idev->ac_list; aca; aca = aca->aca_next) {
323 if (ipv6_addr_cmp(&aca->aca_addr, addr) == 0) {
331 * not found: create a new one.
334 aca = kmalloc(sizeof(struct ifacaddr6), GFP_ATOMIC);
341 rt = addrconf_dst_alloc(idev, addr, 1);
348 memset(aca, 0, sizeof(struct ifacaddr6));
350 ipv6_addr_copy(&aca->aca_addr, addr);
351 aca->aca_idev = idev;
354 /* aca_tstamp should be updated upon changes */
355 aca->aca_cstamp = aca->aca_tstamp = jiffies;
356 atomic_set(&aca->aca_refcnt, 2);
357 aca->aca_lock = SPIN_LOCK_UNLOCKED;
359 aca->aca_next = idev->ac_list;
361 write_unlock_bh(&idev->lock);
363 dst_hold(&rt->u.dst);
364 if (ip6_ins_rt(rt, NULL, NULL))
365 dst_release(&rt->u.dst);
367 addrconf_join_solict(dev, &aca->aca_addr);
372 write_unlock_bh(&idev->lock);
378 * device anycast group decrement
380 int __ipv6_dev_ac_dec(struct inet6_dev *idev, struct in6_addr *addr)
382 struct ifacaddr6 *aca, *prev_aca;
384 write_lock_bh(&idev->lock);
386 for (aca = idev->ac_list; aca; aca = aca->aca_next) {
387 if (ipv6_addr_cmp(&aca->aca_addr, addr) == 0)
392 write_unlock_bh(&idev->lock);
395 if (--aca->aca_users > 0) {
396 write_unlock_bh(&idev->lock);
400 prev_aca->aca_next = aca->aca_next;
402 idev->ac_list = aca->aca_next;
403 write_unlock_bh(&idev->lock);
404 addrconf_leave_solict(idev, &aca->aca_addr);
406 dst_hold(&aca->aca_rt->u.dst);
407 if (ip6_del_rt(aca->aca_rt, NULL, NULL))
408 dst_free(&aca->aca_rt->u.dst);
410 dst_release(&aca->aca_rt->u.dst);
416 int ipv6_dev_ac_dec(struct net_device *dev, struct in6_addr *addr)
419 struct inet6_dev *idev = in6_dev_get(dev);
422 ret = __ipv6_dev_ac_dec(idev, addr);
428 * check if the interface has this anycast address
430 static int ipv6_chk_acast_dev(struct net_device *dev, struct in6_addr *addr)
432 struct inet6_dev *idev;
433 struct ifacaddr6 *aca;
435 idev = in6_dev_get(dev);
437 read_lock_bh(&idev->lock);
438 for (aca = idev->ac_list; aca; aca = aca->aca_next)
439 if (ipv6_addr_cmp(&aca->aca_addr, addr) == 0)
441 read_unlock_bh(&idev->lock);
449 * check if given interface (or any, if dev==0) has this anycast address
451 int ipv6_chk_acast_addr(struct net_device *dev, struct in6_addr *addr)
454 return ipv6_chk_acast_dev(dev, addr);
455 read_lock(&dev_base_lock);
456 for (dev=dev_base; dev; dev=dev->next)
457 if (ipv6_chk_acast_dev(dev, addr))
459 read_unlock(&dev_base_lock);
464 #ifdef CONFIG_PROC_FS
465 struct ac6_iter_state {
466 struct net_device *dev;
467 struct inet6_dev *idev;
470 #define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private)
472 static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
474 struct ifacaddr6 *im = NULL;
475 struct ac6_iter_state *state = ac6_seq_private(seq);
477 for (state->dev = dev_base, state->idev = NULL;
479 state->dev = state->dev->next) {
480 struct inet6_dev *idev;
481 idev = in6_dev_get(state->dev);
484 read_lock_bh(&idev->lock);
490 read_unlock_bh(&idev->lock);
495 static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
497 struct ac6_iter_state *state = ac6_seq_private(seq);
501 if (likely(state->idev != NULL)) {
502 read_unlock_bh(&state->idev->lock);
503 in6_dev_put(state->idev);
505 state->dev = state->dev->next;
510 state->idev = in6_dev_get(state->dev);
513 read_lock_bh(&state->idev->lock);
514 im = state->idev->ac_list;
519 static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
521 struct ifacaddr6 *im = ac6_get_first(seq);
523 while (pos && (im = ac6_get_next(seq, im)) != NULL)
525 return pos ? NULL : im;
528 static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
530 read_lock(&dev_base_lock);
531 return ac6_get_idx(seq, *pos);
534 static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
536 struct ifacaddr6 *im;
537 im = ac6_get_next(seq, v);
542 static void ac6_seq_stop(struct seq_file *seq, void *v)
544 struct ac6_iter_state *state = ac6_seq_private(seq);
545 if (likely(state->idev != NULL)) {
546 read_unlock_bh(&state->idev->lock);
547 in6_dev_put(state->idev);
549 read_unlock(&dev_base_lock);
552 static int ac6_seq_show(struct seq_file *seq, void *v)
554 struct ifacaddr6 *im = (struct ifacaddr6 *)v;
555 struct ac6_iter_state *state = ac6_seq_private(seq);
559 "%04x%04x%04x%04x%04x%04x%04x%04x "
561 state->dev->ifindex, state->dev->name,
567 static struct seq_operations ac6_seq_ops = {
568 .start = ac6_seq_start,
569 .next = ac6_seq_next,
570 .stop = ac6_seq_stop,
571 .show = ac6_seq_show,
574 static int ac6_seq_open(struct inode *inode, struct file *file)
576 struct seq_file *seq;
578 struct ac6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
583 rc = seq_open(file, &ac6_seq_ops);
587 seq = file->private_data;
589 memset(s, 0, sizeof(*s));
597 static struct file_operations ac6_seq_fops = {
598 .owner = THIS_MODULE,
599 .open = ac6_seq_open,
602 .release = seq_release_private,
605 int __init ac6_proc_init(void)
607 if (!proc_net_fops_create("anycast6", S_IRUGO, &ac6_seq_fops))
613 void ac6_proc_exit(void)
615 proc_net_remove("anycast6");