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 = 0;
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 = 0;
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);
301 * device anycast group inc (add if not found)
303 int ipv6_dev_ac_inc(struct net_device *dev, struct in6_addr *addr)
305 struct ifacaddr6 *aca;
306 struct inet6_dev *idev;
308 idev = in6_dev_get(dev);
313 write_lock_bh(&idev->lock);
315 write_unlock_bh(&idev->lock);
320 for (aca = idev->ac_list; aca; aca = aca->aca_next) {
321 if (ipv6_addr_cmp(&aca->aca_addr, addr) == 0) {
323 write_unlock_bh(&idev->lock);
330 * not found: create a new one.
333 aca = kmalloc(sizeof(struct ifacaddr6), GFP_ATOMIC);
336 write_unlock_bh(&idev->lock);
341 memset(aca, 0, sizeof(struct ifacaddr6));
343 ipv6_addr_copy(&aca->aca_addr, addr);
344 aca->aca_idev = idev;
346 /* aca_tstamp should be updated upon changes */
347 aca->aca_cstamp = aca->aca_tstamp = jiffies;
348 atomic_set(&aca->aca_refcnt, 2);
349 aca->aca_lock = SPIN_LOCK_UNLOCKED;
351 aca->aca_next = idev->ac_list;
353 write_unlock_bh(&idev->lock);
355 ip6_rt_addr_add(&aca->aca_addr, dev, 1);
357 addrconf_join_solict(dev, &aca->aca_addr);
364 * device anycast group decrement
366 int ipv6_dev_ac_dec(struct net_device *dev, struct in6_addr *addr)
368 struct inet6_dev *idev;
369 struct ifacaddr6 *aca, *prev_aca;
371 idev = in6_dev_get(dev);
375 write_lock_bh(&idev->lock);
377 for (aca = idev->ac_list; aca; aca = aca->aca_next) {
378 if (ipv6_addr_cmp(&aca->aca_addr, addr) == 0)
383 write_unlock_bh(&idev->lock);
387 if (--aca->aca_users > 0) {
388 write_unlock_bh(&idev->lock);
393 prev_aca->aca_next = aca->aca_next;
395 idev->ac_list = aca->aca_next;
396 write_unlock_bh(&idev->lock);
397 addrconf_leave_solict(dev, &aca->aca_addr);
399 ip6_rt_addr_del(&aca->aca_addr, dev);
407 * check if the interface has this anycast address
409 static int ipv6_chk_acast_dev(struct net_device *dev, struct in6_addr *addr)
411 struct inet6_dev *idev;
412 struct ifacaddr6 *aca;
414 idev = in6_dev_get(dev);
416 read_lock_bh(&idev->lock);
417 for (aca = idev->ac_list; aca; aca = aca->aca_next)
418 if (ipv6_addr_cmp(&aca->aca_addr, addr) == 0)
420 read_unlock_bh(&idev->lock);
428 * check if given interface (or any, if dev==0) has this anycast address
430 int ipv6_chk_acast_addr(struct net_device *dev, struct in6_addr *addr)
433 return ipv6_chk_acast_dev(dev, addr);
434 read_lock(&dev_base_lock);
435 for (dev=dev_base; dev; dev=dev->next)
436 if (ipv6_chk_acast_dev(dev, addr))
438 read_unlock(&dev_base_lock);
443 #ifdef CONFIG_PROC_FS
444 struct ac6_iter_state {
445 struct net_device *dev;
446 struct inet6_dev *idev;
449 #define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private)
451 static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
453 struct ifacaddr6 *im = NULL;
454 struct ac6_iter_state *state = ac6_seq_private(seq);
456 for (state->dev = dev_base, state->idev = NULL;
458 state->dev = state->dev->next) {
459 struct inet6_dev *idev;
460 idev = in6_dev_get(state->dev);
463 read_lock_bh(&idev->lock);
469 read_unlock_bh(&idev->lock);
474 static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
476 struct ac6_iter_state *state = ac6_seq_private(seq);
480 if (likely(state->idev != NULL)) {
481 read_unlock_bh(&state->idev->lock);
482 in6_dev_put(state->idev);
484 state->dev = state->dev->next;
489 state->idev = in6_dev_get(state->dev);
492 read_lock_bh(&state->idev->lock);
493 im = state->idev->ac_list;
498 static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
500 struct ifacaddr6 *im = ac6_get_first(seq);
502 while (pos && (im = ac6_get_next(seq, im)) != NULL)
504 return pos ? NULL : im;
507 static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
509 read_lock(&dev_base_lock);
510 return ac6_get_idx(seq, *pos);
513 static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
515 struct ifacaddr6 *im;
516 im = ac6_get_next(seq, v);
521 static void ac6_seq_stop(struct seq_file *seq, void *v)
523 struct ac6_iter_state *state = ac6_seq_private(seq);
524 if (likely(state->idev != NULL)) {
525 read_unlock_bh(&state->idev->lock);
526 in6_dev_put(state->idev);
528 read_unlock(&dev_base_lock);
531 static int ac6_seq_show(struct seq_file *seq, void *v)
533 struct ifacaddr6 *im = (struct ifacaddr6 *)v;
534 struct ac6_iter_state *state = ac6_seq_private(seq);
538 "%04x%04x%04x%04x%04x%04x%04x%04x "
540 state->dev->ifindex, state->dev->name,
546 static struct seq_operations ac6_seq_ops = {
547 .start = ac6_seq_start,
548 .next = ac6_seq_next,
549 .stop = ac6_seq_stop,
550 .show = ac6_seq_show,
553 static int ac6_seq_open(struct inode *inode, struct file *file)
555 struct seq_file *seq;
557 struct ac6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
562 rc = seq_open(file, &ac6_seq_ops);
566 seq = file->private_data;
568 memset(s, 0, sizeof(*s));
576 static struct file_operations ac6_seq_fops = {
577 .owner = THIS_MODULE,
578 .open = ac6_seq_open,
581 .release = seq_release_private,
584 int __init ac6_proc_init(void)
586 if (!proc_net_fops_create("anycast6", S_IRUGO, &ac6_seq_fops))
592 void ac6_proc_exit(void)
594 proc_net_remove("anycast6");