2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <pedro_m@yahoo.com>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill double kfree on module
24 * Maciej W. Rozycki : FDDI support
25 * sekiya@USAGI : Don't send too many RS
27 * yoshfuji@USAGI : Fixed interval between DAD
29 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
30 * address validation timer.
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
36 #include <linux/config.h>
37 #include <linux/errno.h>
38 #include <linux/types.h>
39 #include <linux/socket.h>
40 #include <linux/sockios.h>
41 #include <linux/sched.h>
42 #include <linux/net.h>
43 #include <linux/in6.h>
44 #include <linux/netdevice.h>
45 #include <linux/if_arp.h>
46 #include <linux/if_arcnet.h>
47 #include <linux/route.h>
48 #include <linux/inetdevice.h>
49 #include <linux/init.h>
51 #include <linux/sysctl.h>
53 #include <linux/delay.h>
54 #include <linux/notifier.h>
56 #include <linux/proc_fs.h>
61 #include <net/protocol.h>
62 #include <net/ndisc.h>
63 #include <net/ip6_route.h>
64 #include <net/addrconf.h>
66 #include <linux/if_tunnel.h>
67 #include <linux/rtnetlink.h>
69 #include <asm/uaccess.h>
71 #define IPV6_MAX_ADDRESSES 16
73 /* Set to 3 to get tracing... */
77 #define ADBG(x) printk x
83 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
84 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
91 * Configured unicast address hash table
93 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
94 static rwlock_t addrconf_hash_lock = RW_LOCK_UNLOCKED;
96 /* Protects inet6 devices */
97 rwlock_t addrconf_lock = RW_LOCK_UNLOCKED;
99 void addrconf_verify(unsigned long);
101 static struct timer_list addr_chk_timer = { function: addrconf_verify };
102 static spinlock_t addrconf_verify_lock = SPIN_LOCK_UNLOCKED;
104 static int addrconf_ifdown(struct net_device *dev, int how);
106 static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags);
107 static void addrconf_dad_timer(unsigned long data);
108 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
109 static void addrconf_rs_timer(unsigned long data);
110 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
112 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
114 static struct notifier_block *inet6addr_chain;
116 struct ipv6_devconf ipv6_devconf =
119 IPV6_DEFAULT_HOPLIMIT, /* hop limit */
120 IPV6_MIN_MTU, /* mtu */
122 1, /* accept redirects */
123 1, /* autoconfiguration */
124 1, /* dad transmits */
125 MAX_RTR_SOLICITATIONS, /* router solicits */
126 RTR_SOLICITATION_INTERVAL, /* rtr solicit interval */
127 MAX_RTR_SOLICITATION_DELAY, /* rtr solicit delay */
130 static struct ipv6_devconf ipv6_devconf_dflt =
133 IPV6_DEFAULT_HOPLIMIT, /* hop limit */
134 IPV6_MIN_MTU, /* mtu */
136 1, /* accept redirects */
137 1, /* autoconfiguration */
138 1, /* dad transmits */
139 MAX_RTR_SOLICITATIONS, /* router solicits */
140 RTR_SOLICITATION_INTERVAL, /* rtr solicit interval */
141 MAX_RTR_SOLICITATION_DELAY, /* rtr solicit delay */
144 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
145 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
146 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
148 int ipv6_addr_type(const struct in6_addr *addr)
153 st = addr->s6_addr32[0];
155 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
156 type = IPV6_ADDR_MULTICAST;
158 switch((st & htonl(0x00FF0000))) {
159 case __constant_htonl(0x00010000):
160 type |= IPV6_ADDR_LOOPBACK;
163 case __constant_htonl(0x00020000):
164 type |= IPV6_ADDR_LINKLOCAL;
167 case __constant_htonl(0x00050000):
168 type |= IPV6_ADDR_SITELOCAL;
173 /* check for reserved anycast addresses */
175 if ((st & htonl(0xE0000000)) &&
176 ((addr->s6_addr32[2] == htonl(0xFDFFFFFF) &&
177 (addr->s6_addr32[3] | htonl(0x7F)) == (u32)~0) ||
178 (addr->s6_addr32[2] == 0 && addr->s6_addr32[3] == 0)))
179 type = IPV6_ADDR_ANYCAST;
181 type = IPV6_ADDR_UNICAST;
183 /* Consider all addresses with the first three bits different of
184 000 and 111 as finished.
186 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
187 (st & htonl(0xE0000000)) != htonl(0xE0000000))
190 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
191 return (IPV6_ADDR_LINKLOCAL | type);
193 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
194 return (IPV6_ADDR_SITELOCAL | type);
196 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
197 if (addr->s6_addr32[2] == 0) {
198 if (addr->in6_u.u6_addr32[3] == 0)
199 return IPV6_ADDR_ANY;
201 if (addr->s6_addr32[3] == htonl(0x00000001))
202 return (IPV6_ADDR_LOOPBACK | type);
204 return (IPV6_ADDR_COMPATv4 | type);
207 if (addr->s6_addr32[2] == htonl(0x0000ffff))
208 return IPV6_ADDR_MAPPED;
211 st &= htonl(0xFF000000);
213 return IPV6_ADDR_RESERVED;
214 st &= htonl(0xFE000000);
215 if (st == htonl(0x02000000))
216 return IPV6_ADDR_RESERVED; /* for NSAP */
217 if (st == htonl(0x04000000))
218 return IPV6_ADDR_RESERVED; /* for IPX */
222 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
224 if (del_timer(&ifp->timer))
228 enum addrconf_timer_t
235 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
236 enum addrconf_timer_t what,
239 if (!del_timer(&ifp->timer))
244 ifp->timer.function = addrconf_dad_timer;
247 ifp->timer.function = addrconf_rs_timer;
251 ifp->timer.expires = jiffies + when;
252 add_timer(&ifp->timer);
255 /* Nobody refers to this device, we may destroy it. */
257 void in6_dev_finish_destroy(struct inet6_dev *idev)
259 struct net_device *dev = idev->dev;
260 BUG_TRAP(idev->addr_list==NULL);
261 BUG_TRAP(idev->mc_list==NULL);
262 #ifdef NET_REFCNT_DEBUG
263 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
267 printk("Freeing alive inet6 device %p\n", idev);
274 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
276 struct inet6_dev *ndev;
280 if (dev->mtu < IPV6_MIN_MTU)
283 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
286 memset(ndev, 0, sizeof(struct inet6_dev));
288 ndev->lock = RW_LOCK_UNLOCKED;
290 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
291 ndev->cnf.mtu6 = dev->mtu;
292 ndev->cnf.sysctl = NULL;
293 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
294 if (ndev->nd_parms == NULL) {
299 /* We refer to the device */
302 write_lock_bh(&addrconf_lock);
304 /* One reference from device */
306 write_unlock_bh(&addrconf_lock);
308 ipv6_mc_init_dev(ndev);
311 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6, NET_IPV6_NEIGH, "ipv6");
312 addrconf_sysctl_register(ndev, &ndev->cnf);
318 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
320 struct inet6_dev *idev;
324 if ((idev = __in6_dev_get(dev)) == NULL) {
325 if ((idev = ipv6_add_dev(dev)) == NULL)
328 if (dev->flags&IFF_UP)
333 void ipv6_addr_prefix(struct in6_addr *prefix,
334 struct in6_addr *addr, int prefix_len)
339 memset(prefix, 0, sizeof(*prefix));
343 if (prefix_len > 128)
346 ncopy = prefix_len / 32;
348 case 4: prefix->s6_addr32[3] = addr->s6_addr32[3];
349 case 3: prefix->s6_addr32[2] = addr->s6_addr32[2];
350 case 2: prefix->s6_addr32[1] = addr->s6_addr32[1];
351 case 1: prefix->s6_addr32[0] = addr->s6_addr32[0];
354 nbits = prefix_len % 32;
358 mask = ~((1 << (32 - nbits)) - 1);
361 prefix->s6_addr32[ncopy] = addr->s6_addr32[ncopy] & mask;
365 static void dev_forward_change(struct inet6_dev *idev)
367 struct net_device *dev;
368 struct inet6_ifaddr *ifa;
369 struct in6_addr addr;
374 if (dev && (dev->flags & IFF_MULTICAST)) {
375 ipv6_addr_all_routers(&addr);
377 if (idev->cnf.forwarding)
378 ipv6_dev_mc_inc(dev, &addr);
380 ipv6_dev_mc_dec(dev, &addr);
382 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
383 ipv6_addr_prefix(&addr, &ifa->addr, ifa->prefix_len);
384 if (ipv6_addr_any(&addr))
386 if (idev->cnf.forwarding)
387 ipv6_dev_ac_inc(idev->dev, &addr);
389 ipv6_dev_ac_dec(idev->dev, &addr);
394 static void addrconf_forward_change(struct inet6_dev *idev)
396 struct net_device *dev;
399 dev_forward_change(idev);
403 read_lock(&dev_base_lock);
404 for (dev=dev_base; dev; dev=dev->next) {
405 read_lock(&addrconf_lock);
406 idev = __in6_dev_get(dev);
408 idev->cnf.forwarding = ipv6_devconf.forwarding;
409 dev_forward_change(idev);
411 read_unlock(&addrconf_lock);
413 read_unlock(&dev_base_lock);
417 /* Nobody refers to this ifaddr, destroy it */
419 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
421 BUG_TRAP(ifp->if_next==NULL);
422 BUG_TRAP(ifp->lst_next==NULL);
423 #ifdef NET_REFCNT_DEBUG
424 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
427 in6_dev_put(ifp->idev);
429 if (del_timer(&ifp->timer))
430 printk("Timer is still running, when freeing ifa=%p\n", ifp);
433 printk("Freeing alive inet6 address %p\n", ifp);
440 /* On success it returns ifp with increased reference count */
442 static struct inet6_ifaddr *
443 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
444 int scope, unsigned flags)
446 struct inet6_ifaddr *ifa;
448 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
452 /* Ignore adding duplicate addresses on an interface */
453 if (ipv6_chk_same_addr(addr, idev->dev)) {
454 spin_unlock_bh(&lock);
455 ADBG(("ipv6_add_addr: already assigned\n"));
456 return ERR_PTR(-EEXIST);
459 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
462 spin_unlock_bh(&lock);
463 ADBG(("ipv6_add_addr: malloc failed\n"));
464 return ERR_PTR(-ENOBUFS);
467 memset(ifa, 0, sizeof(struct inet6_ifaddr));
468 ipv6_addr_copy(&ifa->addr, addr);
470 spin_lock_init(&ifa->lock);
471 init_timer(&ifa->timer);
472 ifa->timer.data = (unsigned long) ifa;
474 ifa->prefix_len = pfxlen;
475 ifa->flags = flags | IFA_F_TENTATIVE;
477 read_lock(&addrconf_lock);
479 read_unlock(&addrconf_lock);
480 spin_unlock_bh(&lock);
482 return ERR_PTR(-ENODEV); /*XXX*/
491 /* Add to big hash table */
492 hash = ipv6_addr_hash(addr);
494 write_lock_bh(&addrconf_hash_lock);
495 ifa->lst_next = inet6_addr_lst[hash];
496 inet6_addr_lst[hash] = ifa;
498 write_unlock_bh(&addrconf_hash_lock);
500 write_lock_bh(&idev->lock);
501 /* Add to inet6_dev unicast addr list. */
502 ifa->if_next = idev->addr_list;
503 idev->addr_list = ifa;
505 write_unlock_bh(&idev->lock);
506 read_unlock(&addrconf_lock);
507 spin_unlock_bh(&lock);
509 notifier_call_chain(&inet6addr_chain,NETDEV_UP,ifa);
514 /* This function wants to get referenced ifp and releases it before return */
516 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
518 struct inet6_ifaddr *ifa, **ifap;
519 struct inet6_dev *idev = ifp->idev;
522 hash = ipv6_addr_hash(&ifp->addr);
526 write_lock_bh(&addrconf_hash_lock);
527 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
528 ifap = &ifa->lst_next) {
530 *ifap = ifa->lst_next;
532 ifa->lst_next = NULL;
536 write_unlock_bh(&addrconf_hash_lock);
538 write_lock_bh(&idev->lock);
539 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;
540 ifap = &ifa->if_next) {
542 *ifap = ifa->if_next;
548 write_unlock_bh(&idev->lock);
550 ipv6_ifa_notify(RTM_DELADDR, ifp);
552 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
554 addrconf_del_timer(ifp);
560 * Choose an apropriate source address
562 * i) get an address with an apropriate scope
563 * ii) see if there is a specific route for the destination and use
564 * an address of the attached interface
565 * iii) don't use deprecated addresses
567 int ipv6_dev_get_saddr(struct net_device *dev,
568 struct in6_addr *daddr, struct in6_addr *saddr, int onlink)
570 struct inet6_ifaddr *ifp = NULL;
571 struct inet6_ifaddr *match = NULL;
572 struct inet6_dev *idev;
578 scope = ipv6_addr_scope(daddr);
584 * search dev and walk through dev addresses
588 if (dev->flags & IFF_LOOPBACK)
591 read_lock(&addrconf_lock);
592 idev = __in6_dev_get(dev);
594 read_lock_bh(&idev->lock);
595 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
596 if (ifp->scope == scope) {
597 if (!(ifp->flags & (IFA_F_DEPRECATED|IFA_F_TENTATIVE))) {
599 read_unlock_bh(&idev->lock);
600 read_unlock(&addrconf_lock);
604 if (!match && !(ifp->flags & IFA_F_TENTATIVE)) {
610 read_unlock_bh(&idev->lock);
612 read_unlock(&addrconf_lock);
615 if (scope == IFA_LINK)
619 * dev == NULL or search failed for specified dev
622 read_lock(&dev_base_lock);
623 read_lock(&addrconf_lock);
624 for (dev = dev_base; dev; dev=dev->next) {
625 idev = __in6_dev_get(dev);
627 read_lock_bh(&idev->lock);
628 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
629 if (ifp->scope == scope) {
630 if (!(ifp->flags&(IFA_F_DEPRECATED|IFA_F_TENTATIVE))) {
632 read_unlock_bh(&idev->lock);
633 goto out_unlock_base;
636 if (!match && !(ifp->flags&IFA_F_TENTATIVE)) {
642 read_unlock_bh(&idev->lock);
647 read_unlock(&addrconf_lock);
648 read_unlock(&dev_base_lock);
656 err = -EADDRNOTAVAIL;
658 ipv6_addr_copy(saddr, &ifp->addr);
669 int ipv6_get_saddr(struct dst_entry *dst,
670 struct in6_addr *daddr, struct in6_addr *saddr)
673 struct net_device *dev = NULL;
676 rt = (struct rt6_info *) dst;
680 onlink = (rt && (rt->rt6i_flags & RTF_ALLONLINK));
682 return ipv6_dev_get_saddr(dev, daddr, saddr, onlink);
686 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
688 struct inet6_dev *idev;
689 int err = -EADDRNOTAVAIL;
691 read_lock(&addrconf_lock);
692 if ((idev = __in6_dev_get(dev)) != NULL) {
693 struct inet6_ifaddr *ifp;
695 read_lock_bh(&idev->lock);
696 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
697 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
698 ipv6_addr_copy(addr, &ifp->addr);
703 read_unlock_bh(&idev->lock);
705 read_unlock(&addrconf_lock);
709 int ipv6_count_addresses(struct inet6_dev *idev)
712 struct inet6_ifaddr *ifp;
714 read_lock_bh(&idev->lock);
715 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
717 read_unlock_bh(&idev->lock);
721 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev)
723 struct inet6_ifaddr * ifp;
724 u8 hash = ipv6_addr_hash(addr);
726 read_lock_bh(&addrconf_hash_lock);
727 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
728 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
729 !(ifp->flags&IFA_F_TENTATIVE)) {
730 if (dev == NULL || ifp->idev->dev == dev ||
731 !(ifp->scope&(IFA_LINK|IFA_HOST)))
735 read_unlock_bh(&addrconf_hash_lock);
740 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
742 struct inet6_ifaddr * ifp;
743 u8 hash = ipv6_addr_hash(addr);
745 read_lock_bh(&addrconf_hash_lock);
746 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
747 if (ipv6_addr_cmp(&ifp->addr, addr) == 0) {
748 if (dev == NULL || ifp->idev->dev == dev)
752 read_unlock_bh(&addrconf_hash_lock);
756 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev)
758 struct inet6_ifaddr * ifp;
759 u8 hash = ipv6_addr_hash(addr);
761 read_lock_bh(&addrconf_hash_lock);
762 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
763 if (ipv6_addr_cmp(&ifp->addr, addr) == 0) {
764 if (dev == NULL || ifp->idev->dev == dev ||
765 !(ifp->scope&(IFA_LINK|IFA_HOST))) {
771 read_unlock_bh(&addrconf_hash_lock);
776 /* Gets referenced address, destroys ifaddr */
778 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
781 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
782 if (ifp->flags&IFA_F_PERMANENT) {
783 spin_lock_bh(&ifp->lock);
784 addrconf_del_timer(ifp);
785 ifp->flags |= IFA_F_TENTATIVE;
786 spin_unlock_bh(&ifp->lock);
793 /* Join to solicited addr multicast group. */
795 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
797 struct in6_addr maddr;
799 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
802 addrconf_addr_solict_mult(addr, &maddr);
803 ipv6_dev_mc_inc(dev, &maddr);
806 void addrconf_leave_solict(struct net_device *dev, struct in6_addr *addr)
808 struct in6_addr maddr;
810 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
813 addrconf_addr_solict_mult(addr, &maddr);
814 ipv6_dev_mc_dec(dev, &maddr);
818 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
823 case ARPHRD_IEEE802_TR:
824 if (dev->addr_len != ETH_ALEN)
826 memcpy(eui, dev->dev_addr, 3);
827 memcpy(eui + 5, dev->dev_addr+3, 3);
833 /* XXX: inherit EUI-64 fro mother interface -- yoshfuji */
834 if (dev->addr_len != ARCNET_ALEN)
837 eui[7] = *(u8*)dev->dev_addr;
843 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
846 struct inet6_ifaddr *ifp;
848 read_lock_bh(&idev->lock);
849 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
850 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
851 memcpy(eui, ifp->addr.s6_addr+8, 8);
856 read_unlock_bh(&idev->lock);
865 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
866 unsigned long expires, unsigned flags)
868 struct in6_rtmsg rtmsg;
870 memset(&rtmsg, 0, sizeof(rtmsg));
871 memcpy(&rtmsg.rtmsg_dst, pfx, sizeof(struct in6_addr));
872 rtmsg.rtmsg_dst_len = plen;
873 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
874 rtmsg.rtmsg_ifindex = dev->ifindex;
875 rtmsg.rtmsg_info = expires;
876 rtmsg.rtmsg_flags = RTF_UP|flags;
877 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
879 /* Prevent useless cloning on PtP SIT.
880 This thing is done here expecting that the whole
881 class of non-broadcast devices need not cloning.
883 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
884 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
886 ip6_route_add(&rtmsg, NULL, NULL);
889 /* Create "default" multicast route to the interface */
891 static void addrconf_add_mroute(struct net_device *dev)
893 struct in6_rtmsg rtmsg;
895 memset(&rtmsg, 0, sizeof(rtmsg));
896 ipv6_addr_set(&rtmsg.rtmsg_dst,
897 htonl(0xFF000000), 0, 0, 0);
898 rtmsg.rtmsg_dst_len = 8;
899 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
900 rtmsg.rtmsg_ifindex = dev->ifindex;
901 rtmsg.rtmsg_flags = RTF_UP;
902 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
903 ip6_route_add(&rtmsg, NULL, NULL);
906 static void sit_route_add(struct net_device *dev)
908 struct in6_rtmsg rtmsg;
910 memset(&rtmsg, 0, sizeof(rtmsg));
912 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
913 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
915 /* prefix length - 96 bits "::d.d.d.d" */
916 rtmsg.rtmsg_dst_len = 96;
917 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
918 rtmsg.rtmsg_ifindex = dev->ifindex;
920 ip6_route_add(&rtmsg, NULL, NULL);
923 static void addrconf_add_lroute(struct net_device *dev)
925 struct in6_addr addr;
927 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
928 addrconf_prefix_route(&addr, 64, dev, 0, 0);
931 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
933 struct inet6_dev *idev;
937 if ((idev = ipv6_find_idev(dev)) == NULL)
940 /* Add default multicast route */
941 addrconf_add_mroute(dev);
943 /* Add link local route */
944 addrconf_add_lroute(dev);
948 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
950 struct prefix_info *pinfo;
955 unsigned long rt_expires;
956 struct inet6_dev *in6_dev;
958 pinfo = (struct prefix_info *) opt;
960 if (len < sizeof(struct prefix_info)) {
961 ADBG(("addrconf: prefix option too short\n"));
966 * Validation checks ([ADDRCONF], page 19)
969 addr_type = ipv6_addr_type(&pinfo->prefix);
971 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
974 valid_lft = ntohl(pinfo->valid);
975 prefered_lft = ntohl(pinfo->prefered);
977 if (prefered_lft > valid_lft) {
979 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
983 in6_dev = in6_dev_get(dev);
985 if (in6_dev == NULL) {
987 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
992 * Two things going on here:
993 * 1) Add routes for on-link prefixes
994 * 2) Configure prefixes with the auto flag set
997 /* Avoid arithemtic overflow. Really, we could
998 save rt_expires in seconds, likely valid_lft,
999 but it would require division in fib gc, that it
1002 if (valid_lft >= 0x7FFFFFFF/HZ)
1005 rt_expires = jiffies + valid_lft * HZ;
1007 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1009 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1010 if (rt->rt6i_flags&RTF_EXPIRES) {
1011 if (pinfo->onlink == 0 || valid_lft == 0) {
1012 ip6_del_rt(rt, NULL, NULL);
1015 rt->rt6i_expires = rt_expires;
1018 } else if (pinfo->onlink && valid_lft) {
1019 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1020 dev, rt_expires, RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1023 dst_release(&rt->u.dst);
1025 /* Try to figure out our local address for this prefix */
1027 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1028 struct inet6_ifaddr * ifp;
1029 struct in6_addr addr;
1032 plen = pinfo->prefix_len >> 3;
1034 if (pinfo->prefix_len == 64) {
1035 memcpy(&addr, &pinfo->prefix, 8);
1036 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1037 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1038 in6_dev_put(in6_dev);
1043 if (net_ratelimit())
1044 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1046 in6_dev_put(in6_dev);
1051 ifp = ipv6_get_ifaddr(&addr, dev);
1053 if (ifp == NULL && valid_lft) {
1054 /* Do not allow to create too much of autoconfigured
1055 * addresses; this would be too easy way to crash kernel.
1057 if (ipv6_count_addresses(in6_dev) < IPV6_MAX_ADDRESSES)
1058 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1059 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1062 in6_dev_put(in6_dev);
1066 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1069 if (ifp && valid_lft == 0) {
1077 spin_lock(&ifp->lock);
1078 ifp->valid_lft = valid_lft;
1079 ifp->prefered_lft = prefered_lft;
1080 ifp->tstamp = jiffies;
1082 ifp->flags &= ~IFA_F_DEPRECATED;
1083 spin_unlock(&ifp->lock);
1085 if (!(flags&IFA_F_TENTATIVE))
1086 ipv6_ifa_notify((flags&IFA_F_DEPRECATED) ?
1087 0 : RTM_NEWADDR, ifp);
1092 in6_dev_put(in6_dev);
1096 * Set destination address.
1097 * Special case for SIT interfaces where we create a new "virtual"
1100 int addrconf_set_dstaddr(void *arg)
1102 struct in6_ifreq ireq;
1103 struct net_device *dev;
1109 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1112 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1118 if (dev->type == ARPHRD_SIT) {
1121 struct ip_tunnel_parm p;
1123 err = -EADDRNOTAVAIL;
1124 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1127 memset(&p, 0, sizeof(p));
1128 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1132 p.iph.protocol = IPPROTO_IPV6;
1134 ifr.ifr_ifru.ifru_data = (void*)&p;
1136 oldfs = get_fs(); set_fs(KERNEL_DS);
1137 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1142 if ((dev = __dev_get_by_name(p.name)) == NULL)
1144 err = dev_open(dev);
1154 * Manual configuration of address on an interface
1156 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1158 struct inet6_ifaddr *ifp;
1159 struct inet6_dev *idev;
1160 struct net_device *dev;
1165 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1168 if (!(dev->flags&IFF_UP))
1171 if ((idev = addrconf_add_dev(dev)) == NULL)
1174 scope = ipv6_addr_scope(pfx);
1176 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1178 addrconf_dad_start(ifp, 0);
1183 return PTR_ERR(ifp);
1186 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1188 struct inet6_ifaddr *ifp;
1189 struct inet6_dev *idev;
1190 struct net_device *dev;
1192 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1195 if ((idev = __in6_dev_get(dev)) == NULL)
1198 read_lock_bh(&idev->lock);
1199 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1200 if (ifp->prefix_len == plen &&
1201 (!memcmp(pfx, &ifp->addr, sizeof(struct in6_addr)))) {
1203 read_unlock_bh(&idev->lock);
1207 /* If the last address is deleted administratively,
1208 disable IPv6 on this interface.
1210 if (idev->addr_list == NULL)
1211 addrconf_ifdown(idev->dev, 1);
1215 read_unlock_bh(&idev->lock);
1216 return -EADDRNOTAVAIL;
1220 int addrconf_add_ifaddr(void *arg)
1222 struct in6_ifreq ireq;
1225 if (!capable(CAP_NET_ADMIN))
1228 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1232 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1237 int addrconf_del_ifaddr(void *arg)
1239 struct in6_ifreq ireq;
1242 if (!capable(CAP_NET_ADMIN))
1245 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1249 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1254 static void sit_add_v4_addrs(struct inet6_dev *idev)
1256 struct inet6_ifaddr * ifp;
1257 struct in6_addr addr;
1258 struct net_device *dev;
1263 memset(&addr, 0, sizeof(struct in6_addr));
1264 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1266 if (idev->dev->flags&IFF_POINTOPOINT) {
1267 addr.s6_addr32[0] = htonl(0xfe800000);
1270 scope = IPV6_ADDR_COMPATv4;
1273 if (addr.s6_addr32[3]) {
1274 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1276 spin_lock_bh(&ifp->lock);
1277 ifp->flags &= ~IFA_F_TENTATIVE;
1278 spin_unlock_bh(&ifp->lock);
1279 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1285 for (dev = dev_base; dev != NULL; dev = dev->next) {
1286 struct in_device * in_dev = __in_dev_get(dev);
1287 if (in_dev && (dev->flags & IFF_UP)) {
1288 struct in_ifaddr * ifa;
1292 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1295 addr.s6_addr32[3] = ifa->ifa_local;
1297 if (ifa->ifa_scope == RT_SCOPE_LINK)
1299 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1300 if (idev->dev->flags&IFF_POINTOPOINT)
1304 if (idev->dev->flags&IFF_POINTOPOINT)
1309 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1312 spin_lock_bh(&ifp->lock);
1313 ifp->flags &= ~IFA_F_TENTATIVE;
1314 spin_unlock_bh(&ifp->lock);
1315 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1323 static void init_loopback(struct net_device *dev)
1325 struct inet6_dev *idev;
1326 struct inet6_ifaddr * ifp;
1332 if ((idev = ipv6_find_idev(dev)) == NULL) {
1333 printk(KERN_DEBUG "init loopback: add_dev failed\n");
1337 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
1339 spin_lock_bh(&ifp->lock);
1340 ifp->flags &= ~IFA_F_TENTATIVE;
1341 spin_unlock_bh(&ifp->lock);
1342 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1347 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
1349 struct inet6_ifaddr * ifp;
1351 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
1353 addrconf_dad_start(ifp, 0);
1358 static void addrconf_dev_config(struct net_device *dev)
1360 struct in6_addr addr;
1361 struct inet6_dev * idev;
1365 if ((dev->type != ARPHRD_ETHER) &&
1366 (dev->type != ARPHRD_FDDI) &&
1367 (dev->type != ARPHRD_IEEE802_TR) &&
1368 (dev->type != ARPHRD_ARCNET)) {
1369 /* Alas, we support only Ethernet autoconfiguration. */
1373 idev = addrconf_add_dev(dev);
1377 memset(&addr, 0, sizeof(struct in6_addr));
1378 addr.s6_addr32[0] = htonl(0xFE800000);
1380 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
1381 addrconf_add_linklocal(idev, &addr);
1384 static void addrconf_sit_config(struct net_device *dev)
1386 struct inet6_dev *idev;
1391 * Configure the tunnel with one of our IPv4
1392 * addresses... we should configure all of
1393 * our v4 addrs in the tunnel
1396 if ((idev = ipv6_find_idev(dev)) == NULL) {
1397 printk(KERN_DEBUG "init sit: add_dev failed\n");
1401 sit_add_v4_addrs(idev);
1403 if (dev->flags&IFF_POINTOPOINT) {
1404 addrconf_add_mroute(dev);
1405 addrconf_add_lroute(dev);
1411 int addrconf_notify(struct notifier_block *this, unsigned long event,
1414 struct net_device *dev = (struct net_device *) data;
1415 struct inet6_dev *idev = __in6_dev_get(dev);
1421 addrconf_sit_config(dev);
1424 case ARPHRD_LOOPBACK:
1429 addrconf_dev_config(dev);
1433 /* If the MTU changed during the interface down, when the
1434 interface up, the changed MTU must be reflected in the
1435 idev as well as routers.
1437 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
1438 rt6_mtu_change(dev, dev->mtu);
1439 idev->cnf.mtu6 = dev->mtu;
1441 /* If the changed mtu during down is lower than IPV6_MIN_MTU
1442 stop IPv6 on this interface.
1444 if (dev->mtu < IPV6_MIN_MTU)
1445 addrconf_ifdown(dev, event != NETDEV_DOWN);
1449 case NETDEV_CHANGEMTU:
1450 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
1451 rt6_mtu_change(dev, dev->mtu);
1452 idev->cnf.mtu6 = dev->mtu;
1456 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
1459 case NETDEV_UNREGISTER:
1461 * Remove all addresses from this interface.
1463 addrconf_ifdown(dev, event != NETDEV_DOWN);
1472 static int addrconf_ifdown(struct net_device *dev, int how)
1474 struct inet6_dev *idev;
1475 struct inet6_ifaddr *ifa, **bifa;
1481 neigh_ifdown(&nd_tbl, dev);
1483 idev = __in6_dev_get(dev);
1487 /* Step 1: remove reference to ipv6 device from parent device.
1491 write_lock_bh(&addrconf_lock);
1492 dev->ip6_ptr = NULL;
1494 write_unlock_bh(&addrconf_lock);
1497 /* Step 2: clear hash table */
1498 for (i=0; i<IN6_ADDR_HSIZE; i++) {
1499 bifa = &inet6_addr_lst[i];
1501 write_lock_bh(&addrconf_hash_lock);
1502 while ((ifa = *bifa) != NULL) {
1503 if (ifa->idev == idev) {
1504 *bifa = ifa->lst_next;
1505 ifa->lst_next = NULL;
1506 addrconf_del_timer(ifa);
1510 bifa = &ifa->lst_next;
1512 write_unlock_bh(&addrconf_hash_lock);
1515 /* Step 3: clear address list */
1517 write_lock_bh(&idev->lock);
1518 while ((ifa = idev->addr_list) != NULL) {
1519 idev->addr_list = ifa->if_next;
1520 ifa->if_next = NULL;
1522 addrconf_del_timer(ifa);
1523 write_unlock_bh(&idev->lock);
1525 ipv6_ifa_notify(RTM_DELADDR, ifa);
1528 write_lock_bh(&idev->lock);
1530 write_unlock_bh(&idev->lock);
1532 /* Step 4: Discard multicast list */
1535 ipv6_mc_destroy_dev(idev);
1539 /* Shot the device (if unregistered) */
1542 neigh_parms_release(&nd_tbl, idev->nd_parms);
1543 #ifdef CONFIG_SYSCTL
1544 addrconf_sysctl_unregister(&idev->cnf);
1551 static void addrconf_rs_timer(unsigned long data)
1553 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
1555 if (ifp->idev->cnf.forwarding)
1558 if (ifp->idev->if_flags & IF_RA_RCVD) {
1560 * Announcement received after solicitation
1566 spin_lock(&ifp->lock);
1567 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
1568 struct in6_addr all_routers;
1570 /* The wait after the last probe can be shorter */
1571 addrconf_mod_timer(ifp, AC_RS,
1572 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
1573 ifp->idev->cnf.rtr_solicit_delay :
1574 ifp->idev->cnf.rtr_solicit_interval);
1575 spin_unlock(&ifp->lock);
1577 ipv6_addr_all_routers(&all_routers);
1579 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
1581 struct in6_rtmsg rtmsg;
1583 spin_unlock(&ifp->lock);
1585 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
1586 ifp->idev->dev->name);
1588 memset(&rtmsg, 0, sizeof(struct in6_rtmsg));
1589 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1590 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1591 rtmsg.rtmsg_flags = (RTF_ALLONLINK | RTF_DEFAULT | RTF_UP);
1593 rtmsg.rtmsg_ifindex = ifp->idev->dev->ifindex;
1595 ip6_route_add(&rtmsg, NULL, NULL);
1603 * Duplicate Address Detection
1605 static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags)
1607 struct net_device *dev;
1608 unsigned long rand_num;
1610 dev = ifp->idev->dev;
1612 addrconf_join_solict(dev, &ifp->addr);
1614 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
1615 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0, flags);
1617 net_srandom(ifp->addr.s6_addr32[3]);
1618 rand_num = net_random() % (ifp->idev->cnf.rtr_solicit_delay ? : 1);
1620 spin_lock_bh(&ifp->lock);
1622 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
1623 !(ifp->flags&IFA_F_TENTATIVE)) {
1624 ifp->flags &= ~IFA_F_TENTATIVE;
1625 spin_unlock_bh(&ifp->lock);
1627 addrconf_dad_completed(ifp);
1631 ifp->probes = ifp->idev->cnf.dad_transmits;
1632 addrconf_mod_timer(ifp, AC_DAD, rand_num);
1634 spin_unlock_bh(&ifp->lock);
1637 static void addrconf_dad_timer(unsigned long data)
1639 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
1640 struct in6_addr unspec;
1641 struct in6_addr mcaddr;
1643 spin_lock_bh(&ifp->lock);
1644 if (ifp->probes == 0) {
1646 * DAD was successful
1649 ifp->flags &= ~IFA_F_TENTATIVE;
1650 spin_unlock_bh(&ifp->lock);
1652 addrconf_dad_completed(ifp);
1659 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
1660 spin_unlock_bh(&ifp->lock);
1662 /* send a neighbour solicitation for our addr */
1663 memset(&unspec, 0, sizeof(unspec));
1664 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
1665 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
1670 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
1672 struct net_device * dev = ifp->idev->dev;
1675 * Configure the address for reception. Now it is valid.
1678 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1680 /* If added prefix is link local and forwarding is off,
1681 start sending router solicitations.
1684 if (ifp->idev->cnf.forwarding == 0 &&
1685 (dev->flags&IFF_LOOPBACK) == 0 &&
1686 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
1687 struct in6_addr all_routers;
1689 ipv6_addr_all_routers(&all_routers);
1692 * If a host as already performed a random delay
1693 * [...] as part of DAD [...] there is no need
1694 * to delay again before sending the first RS
1696 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
1698 spin_lock_bh(&ifp->lock);
1700 ifp->idev->if_flags |= IF_RS_SENT;
1701 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
1702 spin_unlock_bh(&ifp->lock);
1705 if (ifp->idev->cnf.forwarding) {
1706 struct in6_addr addr;
1708 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1709 if (!ipv6_addr_any(&addr))
1710 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1714 #ifdef CONFIG_PROC_FS
1715 static int iface_proc_info(char *buffer, char **start, off_t offset,
1718 struct inet6_ifaddr *ifp;
1724 for (i=0; i < IN6_ADDR_HSIZE; i++) {
1725 read_lock_bh(&addrconf_hash_lock);
1726 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
1729 for (j=0; j<16; j++) {
1730 sprintf(buffer + len, "%02x",
1731 ifp->addr.s6_addr[j]);
1735 len += sprintf(buffer + len,
1736 " %02x %02x %02x %02x %8s\n",
1737 ifp->idev->dev->ifindex,
1741 ifp->idev->dev->name);
1747 if(pos>offset+length) {
1748 read_unlock_bh(&addrconf_hash_lock);
1752 read_unlock_bh(&addrconf_hash_lock);
1757 *start=buffer+(offset-begin);
1758 len-=(offset-begin);
1766 #endif /* CONFIG_PROC_FS */
1769 * Periodic address status verification
1772 void addrconf_verify(unsigned long foo)
1774 struct inet6_ifaddr *ifp;
1775 unsigned long now, next;
1778 spin_lock_bh(&addrconf_verify_lock);
1780 next = now + ADDR_CHECK_FREQUENCY;
1782 del_timer(&addr_chk_timer);
1784 for (i=0; i < IN6_ADDR_HSIZE; i++) {
1787 write_lock(&addrconf_hash_lock);
1788 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
1791 if (ifp->flags & IFA_F_PERMANENT)
1794 spin_lock(&ifp->lock);
1795 age = (now - ifp->tstamp) / HZ;
1797 if (age >= ifp->valid_lft) {
1798 spin_unlock(&ifp->lock);
1800 write_unlock(&addrconf_hash_lock);
1803 } else if (age >= ifp->prefered_lft) {
1804 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
1807 if (!(ifp->flags&IFA_F_DEPRECATED)) {
1809 ifp->flags |= IFA_F_DEPRECATED;
1812 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
1813 next = ifp->tstamp + ifp->valid_lft * HZ;
1815 spin_unlock(&ifp->lock);
1819 write_unlock(&addrconf_hash_lock);
1821 ipv6_ifa_notify(0, ifp);
1826 /* ifp->prefered_lft <= ifp->valid_lft */
1827 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
1828 next = ifp->tstamp + ifp->prefered_lft * HZ;
1829 spin_unlock(&ifp->lock);
1832 write_unlock(&addrconf_hash_lock);
1835 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
1836 add_timer(&addr_chk_timer);
1837 spin_unlock_bh(&addrconf_verify_lock);
1841 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1843 struct rtattr **rta = arg;
1844 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
1845 struct in6_addr *pfx;
1848 if (rta[IFA_ADDRESS-1]) {
1849 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
1851 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
1853 if (rta[IFA_LOCAL-1]) {
1854 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
1856 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
1861 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
1865 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1867 struct rtattr **rta = arg;
1868 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
1869 struct in6_addr *pfx;
1872 if (rta[IFA_ADDRESS-1]) {
1873 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
1875 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
1877 if (rta[IFA_LOCAL-1]) {
1878 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
1880 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
1885 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
1888 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
1889 u32 pid, u32 seq, int event)
1891 struct ifaddrmsg *ifm;
1892 struct nlmsghdr *nlh;
1893 struct ifa_cacheinfo ci;
1894 unsigned char *b = skb->tail;
1896 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
1897 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
1898 ifm = NLMSG_DATA(nlh);
1899 ifm->ifa_family = AF_INET6;
1900 ifm->ifa_prefixlen = ifa->prefix_len;
1901 ifm->ifa_flags = ifa->flags;
1902 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
1903 if (ifa->scope&IFA_HOST)
1904 ifm->ifa_scope = RT_SCOPE_HOST;
1905 else if (ifa->scope&IFA_LINK)
1906 ifm->ifa_scope = RT_SCOPE_LINK;
1907 else if (ifa->scope&IFA_SITE)
1908 ifm->ifa_scope = RT_SCOPE_SITE;
1909 ifm->ifa_index = ifa->idev->dev->ifindex;
1910 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
1911 if (!(ifa->flags&IFA_F_PERMANENT)) {
1912 ci.ifa_prefered = ifa->prefered_lft;
1913 ci.ifa_valid = ifa->valid_lft;
1914 if (ci.ifa_prefered != 0xFFFFFFFF) {
1915 long tval = (jiffies - ifa->tstamp)/HZ;
1916 ci.ifa_prefered -= tval;
1917 if (ci.ifa_valid != 0xFFFFFFFF)
1918 ci.ifa_valid -= tval;
1920 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
1922 nlh->nlmsg_len = skb->tail - b;
1927 skb_trim(skb, b - skb->data);
1931 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1934 int s_idx, s_ip_idx;
1935 struct inet6_ifaddr *ifa;
1937 s_idx = cb->args[0];
1938 s_ip_idx = ip_idx = cb->args[1];
1940 for (idx=0; idx < IN6_ADDR_HSIZE; idx++) {
1945 read_lock_bh(&addrconf_hash_lock);
1946 for (ifa=inet6_addr_lst[idx], ip_idx = 0; ifa;
1947 ifa = ifa->lst_next, ip_idx++) {
1948 if (ip_idx < s_ip_idx)
1950 if (inet6_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1951 cb->nlh->nlmsg_seq, RTM_NEWADDR) <= 0) {
1952 read_unlock_bh(&addrconf_hash_lock);
1956 read_unlock_bh(&addrconf_hash_lock);
1960 cb->args[1] = ip_idx;
1965 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
1967 struct sk_buff *skb;
1968 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
1970 skb = alloc_skb(size, GFP_ATOMIC);
1972 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, ENOBUFS);
1975 if (inet6_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
1977 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, EINVAL);
1980 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFADDR;
1981 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFADDR, GFP_ATOMIC);
1984 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
1985 __s32 *array, int bytes)
1987 memset(array, 0, bytes);
1988 array[DEVCONF_FORWARDING] = cnf->forwarding;
1989 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
1990 array[DEVCONF_MTU6] = cnf->mtu6;
1991 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
1992 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
1993 array[DEVCONF_AUTOCONF] = cnf->autoconf;
1994 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
1995 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
1996 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
1997 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
1998 #ifdef CONFIG_IPV6_PRIVACY
1999 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
2000 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
2001 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
2002 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
2003 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
2007 static int inet6_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
2008 struct inet6_dev *idev,
2009 int type, u32 pid, u32 seq)
2011 __s32 *array = NULL;
2012 struct ifinfomsg *r;
2013 struct nlmsghdr *nlh;
2014 unsigned char *b = skb->tail;
2015 struct rtattr *subattr;
2017 nlh = NLMSG_PUT(skb, pid, seq, type, sizeof(*r));
2018 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2019 r = NLMSG_DATA(nlh);
2020 r->ifi_family = AF_INET6;
2021 r->ifi_type = dev->type;
2022 r->ifi_index = dev->ifindex;
2023 r->ifi_flags = dev->flags;
2025 if (!netif_running(dev) || !netif_carrier_ok(dev))
2026 r->ifi_flags &= ~IFF_RUNNING;
2028 r->ifi_flags |= IFF_RUNNING;
2030 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
2032 subattr = (struct rtattr*)skb->tail;
2034 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
2036 /* return the device flags */
2037 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
2039 /* return the device sysctl params */
2040 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
2041 goto rtattr_failure;
2042 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
2043 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
2045 /* XXX - Statistics/MC not implemented */
2046 subattr->rta_len = skb->tail - (u8*)subattr;
2048 nlh->nlmsg_len = skb->tail - b;
2056 skb_trim(skb, b - skb->data);
2060 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2063 int s_idx = cb->args[0];
2064 struct net_device *dev;
2065 struct inet6_dev *idev;
2067 read_lock(&dev_base_lock);
2068 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
2071 if ((idev = in6_dev_get(dev)) == NULL)
2073 err = inet6_fill_ifinfo(skb, dev, idev, RTM_NEWLINK,
2074 NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq);
2079 read_unlock(&dev_base_lock);
2085 static struct rtnetlink_link inet6_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
2089 { NULL, inet6_dump_ifinfo, },
2092 { inet6_rtm_newaddr, NULL, },
2093 { inet6_rtm_deladdr, NULL, },
2094 { NULL, inet6_dump_ifaddr, },
2097 { inet6_rtm_newroute, NULL, },
2098 { inet6_rtm_delroute, NULL, },
2099 { inet6_rtm_getroute, inet6_dump_fib, },
2103 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
2105 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
2109 ip6_rt_addr_add(&ifp->addr, ifp->idev->dev);
2112 addrconf_leave_solict(ifp->idev->dev, &ifp->addr);
2113 if (ifp->idev->cnf.forwarding) {
2114 struct in6_addr addr;
2116 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2117 if (!ipv6_addr_any(&addr))
2118 ipv6_dev_ac_dec(ifp->idev->dev, &addr);
2120 if (!ipv6_chk_addr(&ifp->addr, NULL))
2121 ip6_rt_addr_del(&ifp->addr, ifp->idev->dev);
2126 #ifdef CONFIG_SYSCTL
2129 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
2130 void *buffer, size_t *lenp)
2132 int *valp = ctl->data;
2136 ret = proc_dointvec(ctl, write, filp, buffer, lenp);
2138 if (write && *valp != val && valp != &ipv6_devconf_dflt.forwarding) {
2139 struct inet6_dev *idev = NULL;
2141 if (valp != &ipv6_devconf.forwarding) {
2142 idev = (struct inet6_dev *)ctl->extra1;
2146 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
2148 addrconf_forward_change(idev);
2151 rt6_purge_dflt_routers(0);
2157 static struct addrconf_sysctl_table
2159 struct ctl_table_header *sysctl_header;
2160 ctl_table addrconf_vars[11];
2161 ctl_table addrconf_dev[2];
2162 ctl_table addrconf_conf_dir[2];
2163 ctl_table addrconf_proto_dir[2];
2164 ctl_table addrconf_root_dir[2];
2165 } addrconf_sysctl = {
2167 {{NET_IPV6_FORWARDING, "forwarding",
2168 &ipv6_devconf.forwarding, sizeof(int), 0644, NULL,
2169 &addrconf_sysctl_forward},
2171 {NET_IPV6_HOP_LIMIT, "hop_limit",
2172 &ipv6_devconf.hop_limit, sizeof(int), 0644, NULL,
2175 {NET_IPV6_MTU, "mtu",
2176 &ipv6_devconf.mtu6, sizeof(int), 0644, NULL,
2179 {NET_IPV6_ACCEPT_RA, "accept_ra",
2180 &ipv6_devconf.accept_ra, sizeof(int), 0644, NULL,
2183 {NET_IPV6_ACCEPT_REDIRECTS, "accept_redirects",
2184 &ipv6_devconf.accept_redirects, sizeof(int), 0644, NULL,
2187 {NET_IPV6_AUTOCONF, "autoconf",
2188 &ipv6_devconf.autoconf, sizeof(int), 0644, NULL,
2191 {NET_IPV6_DAD_TRANSMITS, "dad_transmits",
2192 &ipv6_devconf.dad_transmits, sizeof(int), 0644, NULL,
2195 {NET_IPV6_RTR_SOLICITS, "router_solicitations",
2196 &ipv6_devconf.rtr_solicits, sizeof(int), 0644, NULL,
2199 {NET_IPV6_RTR_SOLICIT_INTERVAL, "router_solicitation_interval",
2200 &ipv6_devconf.rtr_solicit_interval, sizeof(int), 0644, NULL,
2201 &proc_dointvec_jiffies},
2203 {NET_IPV6_RTR_SOLICIT_DELAY, "router_solicitation_delay",
2204 &ipv6_devconf.rtr_solicit_delay, sizeof(int), 0644, NULL,
2205 &proc_dointvec_jiffies},
2209 {{NET_PROTO_CONF_ALL, "all", NULL, 0, 0555, addrconf_sysctl.addrconf_vars},{0}},
2210 {{NET_IPV6_CONF, "conf", NULL, 0, 0555, addrconf_sysctl.addrconf_dev},{0}},
2211 {{NET_IPV6, "ipv6", NULL, 0, 0555, addrconf_sysctl.addrconf_conf_dir},{0}},
2212 {{CTL_NET, "net", NULL, 0, 0555, addrconf_sysctl.addrconf_proto_dir},{0}}
2215 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
2218 struct net_device *dev = idev ? idev->dev : NULL;
2219 struct addrconf_sysctl_table *t;
2221 t = kmalloc(sizeof(*t), GFP_KERNEL);
2224 memcpy(t, &addrconf_sysctl, sizeof(*t));
2225 for (i=0; i<sizeof(t->addrconf_vars)/sizeof(t->addrconf_vars[0])-1; i++) {
2226 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
2227 t->addrconf_vars[i].de = NULL;
2228 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
2231 t->addrconf_dev[0].procname = dev->name;
2232 t->addrconf_dev[0].ctl_name = dev->ifindex;
2234 t->addrconf_dev[0].procname = "default";
2235 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
2237 t->addrconf_dev[0].child = t->addrconf_vars;
2238 t->addrconf_dev[0].de = NULL;
2239 t->addrconf_conf_dir[0].child = t->addrconf_dev;
2240 t->addrconf_conf_dir[0].de = NULL;
2241 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
2242 t->addrconf_proto_dir[0].de = NULL;
2243 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
2244 t->addrconf_root_dir[0].de = NULL;
2246 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
2247 if (t->sysctl_header == NULL)
2253 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
2256 struct addrconf_sysctl_table *t = p->sysctl;
2258 unregister_sysctl_table(t->sysctl_header);
2270 int register_inet6addr_notifier(struct notifier_block *nb)
2272 return notifier_chain_register(&inet6addr_chain, nb);
2275 int unregister_inet6addr_notifier(struct notifier_block *nb)
2277 return notifier_chain_unregister(&inet6addr_chain,nb);
2281 * Init / cleanup code
2284 void __init addrconf_init(void)
2287 struct net_device *dev;
2289 /* This takes sense only during module load. */
2291 for (dev = dev_base; dev; dev = dev->next) {
2292 if (!(dev->flags&IFF_UP))
2295 switch (dev->type) {
2296 case ARPHRD_LOOPBACK:
2301 case ARPHRD_IEEE802_TR:
2303 addrconf_dev_config(dev);
2306 /* Ignore all other */
2312 #ifdef CONFIG_PROC_FS
2313 proc_net_create("if_inet6", 0, iface_proc_info);
2317 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
2318 #ifdef CONFIG_SYSCTL
2319 addrconf_sysctl.sysctl_header =
2320 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
2321 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
2326 void addrconf_cleanup(void)
2328 struct net_device *dev;
2329 struct inet6_dev *idev;
2330 struct inet6_ifaddr *ifa;
2333 rtnetlink_links[PF_INET6] = NULL;
2334 #ifdef CONFIG_SYSCTL
2335 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
2336 addrconf_sysctl_unregister(&ipv6_devconf);
2345 for (dev=dev_base; dev; dev=dev->next) {
2346 if ((idev = __in6_dev_get(dev)) == NULL)
2348 addrconf_ifdown(dev, 1);
2355 write_lock_bh(&addrconf_hash_lock);
2356 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2357 for (ifa=inet6_addr_lst[i]; ifa; ) {
2358 struct inet6_ifaddr *bifa;
2361 ifa = ifa->lst_next;
2362 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
2363 /* Do not free it; something is wrong.
2364 Now we can investigate it with debugger.
2368 write_unlock_bh(&addrconf_hash_lock);
2370 del_timer(&addr_chk_timer);
2374 #ifdef CONFIG_PROC_FS
2375 proc_net_remove("if_inet6");