2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
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 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
33 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
34 * address on a same interface.
35 * YOSHIFUJI Hideaki @USAGI : ARCnet support
36 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
38 * YOSHIFUJI Hideaki @USAGI : improved source address
39 * selection; consider scope,
43 #include <linux/config.h>
44 #include <linux/errno.h>
45 #include <linux/types.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/sched.h>
49 #include <linux/net.h>
50 #include <linux/in6.h>
51 #include <linux/netdevice.h>
52 #include <linux/if_arp.h>
53 #include <linux/if_arcnet.h>
54 #include <linux/if_infiniband.h>
55 #include <linux/route.h>
56 #include <linux/inetdevice.h>
57 #include <linux/init.h>
59 #include <linux/sysctl.h>
61 #include <linux/capability.h>
62 #include <linux/delay.h>
63 #include <linux/notifier.h>
64 #include <linux/string.h>
70 #include <net/protocol.h>
71 #include <net/ndisc.h>
72 #include <net/ip6_route.h>
73 #include <net/addrconf.h>
76 #include <linux/if_tunnel.h>
77 #include <linux/rtnetlink.h>
79 #ifdef CONFIG_IPV6_PRIVACY
80 #include <linux/random.h>
83 #include <asm/uaccess.h>
85 #include <linux/proc_fs.h>
86 #include <linux/seq_file.h>
88 /* Set to 3 to get tracing... */
92 #define ADBG(x) printk x
97 #define INFINITY_LIFE_TIME 0xFFFFFFFF
98 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
101 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
102 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
105 #ifdef CONFIG_IPV6_PRIVACY
106 static int __ipv6_regen_rndid(struct inet6_dev *idev);
107 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
108 static void ipv6_regen_rndid(unsigned long data);
110 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
113 static int ipv6_count_addresses(struct inet6_dev *idev);
116 * Configured unicast address hash table
118 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
119 static DEFINE_RWLOCK(addrconf_hash_lock);
121 /* Protects inet6 devices */
122 DEFINE_RWLOCK(addrconf_lock);
124 static void addrconf_verify(unsigned long);
126 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
127 static DEFINE_SPINLOCK(addrconf_verify_lock);
129 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
130 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
132 static int addrconf_ifdown(struct net_device *dev, int how);
134 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
135 static void addrconf_dad_timer(unsigned long data);
136 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
137 static void addrconf_dad_run(struct inet6_dev *idev);
138 static void addrconf_rs_timer(unsigned long data);
139 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
140 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
142 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
143 struct prefix_info *pinfo);
144 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
146 static struct notifier_block *inet6addr_chain;
148 struct ipv6_devconf ipv6_devconf = {
150 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
151 .mtu6 = IPV6_MIN_MTU,
153 .accept_redirects = 1,
155 .force_mld_version = 0,
157 .rtr_solicits = MAX_RTR_SOLICITATIONS,
158 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
159 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
160 #ifdef CONFIG_IPV6_PRIVACY
162 .temp_valid_lft = TEMP_VALID_LIFETIME,
163 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
164 .regen_max_retry = REGEN_MAX_RETRY,
165 .max_desync_factor = MAX_DESYNC_FACTOR,
167 .max_addresses = IPV6_MAX_ADDRESSES,
168 .accept_ra_defrtr = 1,
169 .accept_ra_pinfo = 1,
172 static struct ipv6_devconf ipv6_devconf_dflt = {
174 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
175 .mtu6 = IPV6_MIN_MTU,
177 .accept_redirects = 1,
180 .rtr_solicits = MAX_RTR_SOLICITATIONS,
181 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
182 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
183 #ifdef CONFIG_IPV6_PRIVACY
185 .temp_valid_lft = TEMP_VALID_LIFETIME,
186 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
187 .regen_max_retry = REGEN_MAX_RETRY,
188 .max_desync_factor = MAX_DESYNC_FACTOR,
190 .max_addresses = IPV6_MAX_ADDRESSES,
191 .accept_ra_defrtr = 1,
192 .accept_ra_pinfo = 1,
195 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
197 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
199 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
201 #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
203 static inline unsigned ipv6_addr_scope2type(unsigned scope)
206 case IPV6_ADDR_SCOPE_NODELOCAL:
207 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
209 case IPV6_ADDR_SCOPE_LINKLOCAL:
210 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
211 IPV6_ADDR_LINKLOCAL);
212 case IPV6_ADDR_SCOPE_SITELOCAL:
213 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
214 IPV6_ADDR_SITELOCAL);
216 return IPV6_ADDR_SCOPE_TYPE(scope);
219 int __ipv6_addr_type(const struct in6_addr *addr)
223 st = addr->s6_addr32[0];
225 /* Consider all addresses with the first three bits different of
226 000 and 111 as unicasts.
228 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
229 (st & htonl(0xE0000000)) != htonl(0xE0000000))
230 return (IPV6_ADDR_UNICAST |
231 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
233 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
235 /* addr-select 3.1 */
236 return (IPV6_ADDR_MULTICAST |
237 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
240 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
241 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
242 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
243 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
244 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
245 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
247 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
248 if (addr->s6_addr32[2] == 0) {
249 if (addr->s6_addr32[3] == 0)
250 return IPV6_ADDR_ANY;
252 if (addr->s6_addr32[3] == htonl(0x00000001))
253 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
254 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
256 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
257 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
260 if (addr->s6_addr32[2] == htonl(0x0000ffff))
261 return (IPV6_ADDR_MAPPED |
262 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
265 return (IPV6_ADDR_RESERVED |
266 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
269 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
271 if (del_timer(&ifp->timer))
275 enum addrconf_timer_t
282 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
283 enum addrconf_timer_t what,
286 if (!del_timer(&ifp->timer))
291 ifp->timer.function = addrconf_dad_timer;
294 ifp->timer.function = addrconf_rs_timer;
298 ifp->timer.expires = jiffies + when;
299 add_timer(&ifp->timer);
302 /* Nobody refers to this device, we may destroy it. */
304 void in6_dev_finish_destroy(struct inet6_dev *idev)
306 struct net_device *dev = idev->dev;
307 BUG_TRAP(idev->addr_list==NULL);
308 BUG_TRAP(idev->mc_list==NULL);
309 #ifdef NET_REFCNT_DEBUG
310 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
314 printk("Freeing alive inet6 device %p\n", idev);
317 snmp6_free_dev(idev);
321 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
323 struct inet6_dev *ndev;
327 if (dev->mtu < IPV6_MIN_MTU)
330 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
333 memset(ndev, 0, sizeof(struct inet6_dev));
335 rwlock_init(&ndev->lock);
337 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
338 ndev->cnf.mtu6 = dev->mtu;
339 ndev->cnf.sysctl = NULL;
340 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
341 if (ndev->nd_parms == NULL) {
345 /* We refer to the device */
348 if (snmp6_alloc_dev(ndev) < 0) {
350 "%s(): cannot allocate memory for statistics; dev=%s.\n",
351 __FUNCTION__, dev->name));
352 neigh_parms_release(&nd_tbl, ndev->nd_parms);
354 in6_dev_finish_destroy(ndev);
358 if (snmp6_register_dev(ndev) < 0) {
360 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
361 __FUNCTION__, dev->name));
362 neigh_parms_release(&nd_tbl, ndev->nd_parms);
364 in6_dev_finish_destroy(ndev);
368 /* One reference from device. We must do this before
369 * we invoke __ipv6_regen_rndid().
373 #ifdef CONFIG_IPV6_PRIVACY
374 init_timer(&ndev->regen_timer);
375 ndev->regen_timer.function = ipv6_regen_rndid;
376 ndev->regen_timer.data = (unsigned long) ndev;
377 if ((dev->flags&IFF_LOOPBACK) ||
378 dev->type == ARPHRD_TUNNEL ||
379 dev->type == ARPHRD_NONE ||
380 dev->type == ARPHRD_SIT) {
382 "%s: Disabled Privacy Extensions\n",
384 ndev->cnf.use_tempaddr = -1;
387 ipv6_regen_rndid((unsigned long) ndev);
391 if (netif_carrier_ok(dev))
392 ndev->if_flags |= IF_READY;
394 write_lock_bh(&addrconf_lock);
396 write_unlock_bh(&addrconf_lock);
398 ipv6_mc_init_dev(ndev);
399 ndev->tstamp = jiffies;
401 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
402 NET_IPV6_NEIGH, "ipv6",
403 &ndisc_ifinfo_sysctl_change,
405 addrconf_sysctl_register(ndev, &ndev->cnf);
411 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
413 struct inet6_dev *idev;
417 if ((idev = __in6_dev_get(dev)) == NULL) {
418 if ((idev = ipv6_add_dev(dev)) == NULL)
422 if (dev->flags&IFF_UP)
428 static void dev_forward_change(struct inet6_dev *idev)
430 struct net_device *dev;
431 struct inet6_ifaddr *ifa;
432 struct in6_addr addr;
437 if (dev && (dev->flags & IFF_MULTICAST)) {
438 ipv6_addr_all_routers(&addr);
440 if (idev->cnf.forwarding)
441 ipv6_dev_mc_inc(dev, &addr);
443 ipv6_dev_mc_dec(dev, &addr);
445 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
446 if (idev->cnf.forwarding)
447 addrconf_join_anycast(ifa);
449 addrconf_leave_anycast(ifa);
454 static void addrconf_forward_change(void)
456 struct net_device *dev;
457 struct inet6_dev *idev;
459 read_lock(&dev_base_lock);
460 for (dev=dev_base; dev; dev=dev->next) {
461 read_lock(&addrconf_lock);
462 idev = __in6_dev_get(dev);
464 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
465 idev->cnf.forwarding = ipv6_devconf.forwarding;
467 dev_forward_change(idev);
469 read_unlock(&addrconf_lock);
471 read_unlock(&dev_base_lock);
475 /* Nobody refers to this ifaddr, destroy it */
477 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
479 BUG_TRAP(ifp->if_next==NULL);
480 BUG_TRAP(ifp->lst_next==NULL);
481 #ifdef NET_REFCNT_DEBUG
482 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
485 in6_dev_put(ifp->idev);
487 if (del_timer(&ifp->timer))
488 printk("Timer is still running, when freeing ifa=%p\n", ifp);
491 printk("Freeing alive inet6 address %p\n", ifp);
494 dst_release(&ifp->rt->u.dst);
499 /* On success it returns ifp with increased reference count */
501 static struct inet6_ifaddr *
502 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
503 int scope, u32 flags)
505 struct inet6_ifaddr *ifa = NULL;
510 read_lock_bh(&addrconf_lock);
512 err = -ENODEV; /*XXX*/
516 write_lock(&addrconf_hash_lock);
518 /* Ignore adding duplicate addresses on an interface */
519 if (ipv6_chk_same_addr(addr, idev->dev)) {
520 ADBG(("ipv6_add_addr: already assigned\n"));
525 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
528 ADBG(("ipv6_add_addr: malloc failed\n"));
533 rt = addrconf_dst_alloc(idev, addr, 0);
539 memset(ifa, 0, sizeof(struct inet6_ifaddr));
540 ipv6_addr_copy(&ifa->addr, addr);
542 spin_lock_init(&ifa->lock);
543 init_timer(&ifa->timer);
544 ifa->timer.data = (unsigned long) ifa;
546 ifa->prefix_len = pfxlen;
547 ifa->flags = flags | IFA_F_TENTATIVE;
548 ifa->cstamp = ifa->tstamp = jiffies;
555 /* Add to big hash table */
556 hash = ipv6_addr_hash(addr);
558 ifa->lst_next = inet6_addr_lst[hash];
559 inet6_addr_lst[hash] = ifa;
561 write_unlock(&addrconf_hash_lock);
563 write_lock(&idev->lock);
564 /* Add to inet6_dev unicast addr list. */
565 ifa->if_next = idev->addr_list;
566 idev->addr_list = ifa;
568 #ifdef CONFIG_IPV6_PRIVACY
569 if (ifa->flags&IFA_F_TEMPORARY) {
570 ifa->tmp_next = idev->tempaddr_list;
571 idev->tempaddr_list = ifa;
579 write_unlock(&idev->lock);
581 read_unlock_bh(&addrconf_lock);
583 if (likely(err == 0))
584 notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
592 write_unlock(&addrconf_hash_lock);
596 /* This function wants to get referenced ifp and releases it before return */
598 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
600 struct inet6_ifaddr *ifa, **ifap;
601 struct inet6_dev *idev = ifp->idev;
603 int deleted = 0, onlink = 0;
604 unsigned long expires = jiffies;
606 hash = ipv6_addr_hash(&ifp->addr);
610 write_lock_bh(&addrconf_hash_lock);
611 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
612 ifap = &ifa->lst_next) {
614 *ifap = ifa->lst_next;
616 ifa->lst_next = NULL;
620 write_unlock_bh(&addrconf_hash_lock);
622 write_lock_bh(&idev->lock);
623 #ifdef CONFIG_IPV6_PRIVACY
624 if (ifp->flags&IFA_F_TEMPORARY) {
625 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
626 ifap = &ifa->tmp_next) {
628 *ifap = ifa->tmp_next;
630 in6_ifa_put(ifp->ifpub);
634 ifa->tmp_next = NULL;
641 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
643 *ifap = ifa->if_next;
646 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
650 } else if (ifp->flags & IFA_F_PERMANENT) {
651 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
653 if (ifa->flags & IFA_F_PERMANENT) {
658 unsigned long lifetime;
663 spin_lock(&ifa->lock);
664 lifetime = min_t(unsigned long,
665 ifa->valid_lft, 0x7fffffffUL/HZ);
666 if (time_before(expires,
667 ifa->tstamp + lifetime * HZ))
668 expires = ifa->tstamp + lifetime * HZ;
669 spin_unlock(&ifa->lock);
673 ifap = &ifa->if_next;
675 write_unlock_bh(&idev->lock);
677 ipv6_ifa_notify(RTM_DELADDR, ifp);
679 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
681 addrconf_del_timer(ifp);
684 * Purge or update corresponding prefix
686 * 1) we don't purge prefix here if address was not permanent.
687 * prefix is managed by its own lifetime.
688 * 2) if there're no addresses, delete prefix.
689 * 3) if there're still other permanent address(es),
690 * corresponding prefix is still permanent.
691 * 4) otherwise, update prefix lifetime to the
692 * longest valid lifetime among the corresponding
693 * addresses on the device.
694 * Note: subsequent RA will update lifetime.
698 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
699 struct in6_addr prefix;
702 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
703 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
705 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
707 ip6_del_rt(rt, NULL, NULL, NULL);
709 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
710 rt->rt6i_expires = expires;
711 rt->rt6i_flags |= RTF_EXPIRES;
714 dst_release(&rt->u.dst);
720 #ifdef CONFIG_IPV6_PRIVACY
721 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
723 struct inet6_dev *idev = ifp->idev;
724 struct in6_addr addr, *tmpaddr;
725 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
730 write_lock(&idev->lock);
732 spin_lock_bh(&ift->lock);
733 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
734 spin_unlock_bh(&ift->lock);
741 if (idev->cnf.use_tempaddr <= 0) {
742 write_unlock(&idev->lock);
744 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
749 spin_lock_bh(&ifp->lock);
750 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
751 idev->cnf.use_tempaddr = -1; /*XXX*/
752 spin_unlock_bh(&ifp->lock);
753 write_unlock(&idev->lock);
755 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
761 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
762 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
763 spin_unlock_bh(&ifp->lock);
764 write_unlock(&idev->lock);
766 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
772 memcpy(&addr.s6_addr[8], idev->rndid, 8);
773 tmp_valid_lft = min_t(__u32,
775 idev->cnf.temp_valid_lft);
776 tmp_prefered_lft = min_t(__u32,
778 idev->cnf.temp_prefered_lft - desync_factor / HZ);
779 tmp_plen = ifp->prefix_len;
780 max_addresses = idev->cnf.max_addresses;
781 tmp_cstamp = ifp->cstamp;
782 tmp_tstamp = ifp->tstamp;
783 spin_unlock_bh(&ifp->lock);
785 write_unlock(&idev->lock);
786 ift = !max_addresses ||
787 ipv6_count_addresses(idev) < max_addresses ?
788 ipv6_add_addr(idev, &addr, tmp_plen,
789 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
790 if (!ift || IS_ERR(ift)) {
794 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
796 write_lock(&idev->lock);
800 spin_lock_bh(&ift->lock);
802 ift->valid_lft = tmp_valid_lft;
803 ift->prefered_lft = tmp_prefered_lft;
804 ift->cstamp = tmp_cstamp;
805 ift->tstamp = tmp_tstamp;
806 spin_unlock_bh(&ift->lock);
808 addrconf_dad_start(ift, 0);
817 * Choose an appropriate source address (RFC3484)
819 struct ipv6_saddr_score {
827 #define IPV6_SADDR_SCORE_LOCAL 0x0001
828 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
829 #define IPV6_SADDR_SCORE_HOA 0x0008
830 #define IPV6_SADDR_SCORE_OIF 0x0010
831 #define IPV6_SADDR_SCORE_LABEL 0x0020
832 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
834 static int inline ipv6_saddr_preferred(int type)
836 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
837 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
842 /* static matching label */
843 static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
846 * prefix (longest match) label
847 * -----------------------------
854 if (type & IPV6_ADDR_LOOPBACK)
856 else if (type & IPV6_ADDR_COMPATv4)
858 else if (type & IPV6_ADDR_MAPPED)
860 else if (addr->s6_addr16[0] == htons(0x2002))
865 int ipv6_dev_get_saddr(struct net_device *daddr_dev,
866 struct in6_addr *daddr, struct in6_addr *saddr)
868 struct ipv6_saddr_score hiscore;
869 struct inet6_ifaddr *ifa_result = NULL;
870 int daddr_type = __ipv6_addr_type(daddr);
871 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
872 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
873 struct net_device *dev;
875 memset(&hiscore, 0, sizeof(hiscore));
877 read_lock(&dev_base_lock);
878 read_lock(&addrconf_lock);
880 for (dev = dev_base; dev; dev=dev->next) {
881 struct inet6_dev *idev;
882 struct inet6_ifaddr *ifa;
884 /* Rule 0: Candidate Source Address (section 4)
885 * - multicast and link-local destination address,
886 * the set of candidate source address MUST only
887 * include addresses assigned to interfaces
888 * belonging to the same link as the outgoing
890 * (- For site-local destination addresses, the
891 * set of candidate source addresses MUST only
892 * include addresses assigned to interfaces
893 * belonging to the same site as the outgoing
896 if ((daddr_type & IPV6_ADDR_MULTICAST ||
897 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
898 daddr_dev && dev != daddr_dev)
901 idev = __in6_dev_get(dev);
905 read_lock_bh(&idev->lock);
906 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
907 struct ipv6_saddr_score score;
909 score.addr_type = __ipv6_addr_type(&ifa->addr);
912 * - Tentative Address (RFC2462 section 5.4)
913 * - A tentative address is not considered
914 * "assigned to an interface" in the traditional
916 * - Candidate Source Address (section 4)
917 * - In any case, anycast addresses, multicast
918 * addresses, and the unspecified address MUST
919 * NOT be included in a candidate set.
921 if (ifa->flags & IFA_F_TENTATIVE)
923 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
924 score.addr_type & IPV6_ADDR_MULTICAST)) {
925 LIMIT_NETDEBUG(KERN_DEBUG
926 "ADDRCONF: unspecified / multicast address"
927 "assigned as unicast address on %s",
937 if (ifa_result == NULL) {
938 /* record it if the first available entry */
942 /* Rule 1: Prefer same address */
943 if (hiscore.rule < 1) {
944 if (ipv6_addr_equal(&ifa_result->addr, daddr))
945 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
948 if (ipv6_addr_equal(&ifa->addr, daddr)) {
949 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
950 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
955 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
959 /* Rule 2: Prefer appropriate scope */
960 if (hiscore.rule < 2) {
961 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
964 score.scope = __ipv6_addr_src_scope(score.addr_type);
965 if (hiscore.scope < score.scope) {
966 if (hiscore.scope < daddr_scope) {
971 } else if (score.scope < hiscore.scope) {
972 if (score.scope < daddr_scope)
980 /* Rule 3: Avoid deprecated address */
981 if (hiscore.rule < 3) {
982 if (ipv6_saddr_preferred(hiscore.addr_type) ||
983 !(ifa_result->flags & IFA_F_DEPRECATED))
984 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
987 if (ipv6_saddr_preferred(score.addr_type) ||
988 !(ifa->flags & IFA_F_DEPRECATED)) {
989 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
990 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
995 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
999 /* Rule 4: Prefer home address -- not implemented yet */
1000 if (hiscore.rule < 4)
1003 /* Rule 5: Prefer outgoing interface */
1004 if (hiscore.rule < 5) {
1005 if (daddr_dev == NULL ||
1006 daddr_dev == ifa_result->idev->dev)
1007 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1010 if (daddr_dev == NULL ||
1011 daddr_dev == ifa->idev->dev) {
1012 score.attrs |= IPV6_SADDR_SCORE_OIF;
1013 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1018 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1022 /* Rule 6: Prefer matching label */
1023 if (hiscore.rule < 6) {
1024 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1025 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1028 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1029 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1030 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1035 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1039 #ifdef CONFIG_IPV6_PRIVACY
1040 /* Rule 7: Prefer public address
1041 * Note: prefer temprary address if use_tempaddr >= 2
1043 if (hiscore.rule < 7) {
1044 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1045 (ifa_result->idev->cnf.use_tempaddr >= 2))
1046 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1049 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1050 (ifa->idev->cnf.use_tempaddr >= 2)) {
1051 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1052 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1057 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1061 /* Rule 8: Use longest matching prefix */
1062 if (hiscore.rule < 8) {
1063 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
1066 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1067 if (score.matchlen > hiscore.matchlen) {
1072 else if (score.matchlen < hiscore.matchlen)
1076 /* Final Rule: choose first available one */
1080 in6_ifa_put(ifa_result);
1085 read_unlock_bh(&idev->lock);
1087 read_unlock(&addrconf_lock);
1088 read_unlock(&dev_base_lock);
1091 return -EADDRNOTAVAIL;
1093 ipv6_addr_copy(saddr, &ifa_result->addr);
1094 in6_ifa_put(ifa_result);
1099 int ipv6_get_saddr(struct dst_entry *dst,
1100 struct in6_addr *daddr, struct in6_addr *saddr)
1102 return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
1106 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1108 struct inet6_dev *idev;
1109 int err = -EADDRNOTAVAIL;
1111 read_lock(&addrconf_lock);
1112 if ((idev = __in6_dev_get(dev)) != NULL) {
1113 struct inet6_ifaddr *ifp;
1115 read_lock_bh(&idev->lock);
1116 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1117 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1118 ipv6_addr_copy(addr, &ifp->addr);
1123 read_unlock_bh(&idev->lock);
1125 read_unlock(&addrconf_lock);
1129 static int ipv6_count_addresses(struct inet6_dev *idev)
1132 struct inet6_ifaddr *ifp;
1134 read_lock_bh(&idev->lock);
1135 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1137 read_unlock_bh(&idev->lock);
1141 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1143 struct inet6_ifaddr * ifp;
1144 u8 hash = ipv6_addr_hash(addr);
1146 read_lock_bh(&addrconf_hash_lock);
1147 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1148 if (ipv6_addr_equal(&ifp->addr, addr) &&
1149 !(ifp->flags&IFA_F_TENTATIVE)) {
1150 if (dev == NULL || ifp->idev->dev == dev ||
1151 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1155 read_unlock_bh(&addrconf_hash_lock);
1160 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1162 struct inet6_ifaddr * ifp;
1163 u8 hash = ipv6_addr_hash(addr);
1165 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1166 if (ipv6_addr_equal(&ifp->addr, addr)) {
1167 if (dev == NULL || ifp->idev->dev == dev)
1174 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1176 struct inet6_ifaddr * ifp;
1177 u8 hash = ipv6_addr_hash(addr);
1179 read_lock_bh(&addrconf_hash_lock);
1180 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1181 if (ipv6_addr_equal(&ifp->addr, addr)) {
1182 if (dev == NULL || ifp->idev->dev == dev ||
1183 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1189 read_unlock_bh(&addrconf_hash_lock);
1194 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1196 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1197 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
1198 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1199 u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1200 int sk_ipv6only = ipv6_only_sock(sk);
1201 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1202 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1203 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1205 if (!sk2_rcv_saddr && !sk_ipv6only)
1208 if (addr_type2 == IPV6_ADDR_ANY &&
1209 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1212 if (addr_type == IPV6_ADDR_ANY &&
1213 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1216 if (sk2_rcv_saddr6 &&
1217 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1220 if (addr_type == IPV6_ADDR_MAPPED &&
1222 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1228 /* Gets referenced address, destroys ifaddr */
1230 static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1232 if (ifp->flags&IFA_F_PERMANENT) {
1233 spin_lock_bh(&ifp->lock);
1234 addrconf_del_timer(ifp);
1235 ifp->flags |= IFA_F_TENTATIVE;
1236 spin_unlock_bh(&ifp->lock);
1238 #ifdef CONFIG_IPV6_PRIVACY
1239 } else if (ifp->flags&IFA_F_TEMPORARY) {
1240 struct inet6_ifaddr *ifpub;
1241 spin_lock_bh(&ifp->lock);
1244 in6_ifa_hold(ifpub);
1245 spin_unlock_bh(&ifp->lock);
1246 ipv6_create_tempaddr(ifpub, ifp);
1249 spin_unlock_bh(&ifp->lock);
1257 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1259 if (net_ratelimit())
1260 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1261 addrconf_dad_stop(ifp);
1264 /* Join to solicited addr multicast group. */
1266 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1268 struct in6_addr maddr;
1270 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1273 addrconf_addr_solict_mult(addr, &maddr);
1274 ipv6_dev_mc_inc(dev, &maddr);
1277 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1279 struct in6_addr maddr;
1281 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1284 addrconf_addr_solict_mult(addr, &maddr);
1285 __ipv6_dev_mc_dec(idev, &maddr);
1288 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1290 struct in6_addr addr;
1291 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1292 if (ipv6_addr_any(&addr))
1294 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1297 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1299 struct in6_addr addr;
1300 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1301 if (ipv6_addr_any(&addr))
1303 __ipv6_dev_ac_dec(ifp->idev, &addr);
1306 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1308 if (dev->addr_len != ETH_ALEN)
1310 memcpy(eui, dev->dev_addr, 3);
1311 memcpy(eui + 5, dev->dev_addr + 3, 3);
1314 * The zSeries OSA network cards can be shared among various
1315 * OS instances, but the OSA cards have only one MAC address.
1316 * This leads to duplicate address conflicts in conjunction
1317 * with IPv6 if more than one instance uses the same card.
1319 * The driver for these cards can deliver a unique 16-bit
1320 * identifier for each instance sharing the same card. It is
1321 * placed instead of 0xFFFE in the interface identifier. The
1322 * "u" bit of the interface identifier is not inverted in this
1323 * case. Hence the resulting interface identifier has local
1324 * scope according to RFC2373.
1327 eui[3] = (dev->dev_id >> 8) & 0xFF;
1328 eui[4] = dev->dev_id & 0xFF;
1337 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1339 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1340 if (dev->addr_len != ARCNET_ALEN)
1343 eui[7] = *(u8*)dev->dev_addr;
1347 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1349 if (dev->addr_len != INFINIBAND_ALEN)
1351 memcpy(eui, dev->dev_addr + 12, 8);
1356 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1358 switch (dev->type) {
1361 case ARPHRD_IEEE802_TR:
1362 return addrconf_ifid_eui48(eui, dev);
1364 return addrconf_ifid_arcnet(eui, dev);
1365 case ARPHRD_INFINIBAND:
1366 return addrconf_ifid_infiniband(eui, dev);
1371 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1374 struct inet6_ifaddr *ifp;
1376 read_lock_bh(&idev->lock);
1377 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1378 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1379 memcpy(eui, ifp->addr.s6_addr+8, 8);
1384 read_unlock_bh(&idev->lock);
1388 #ifdef CONFIG_IPV6_PRIVACY
1389 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1390 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1393 get_random_bytes(idev->rndid, sizeof(idev->rndid));
1394 idev->rndid[0] &= ~0x02;
1397 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1398 * check if generated address is not inappropriate
1400 * - Reserved subnet anycast (RFC 2526)
1401 * 11111101 11....11 1xxxxxxx
1402 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1403 * 00-00-5E-FE-xx-xx-xx-xx
1405 * - XXX: already assigned to an address on the device
1407 if (idev->rndid[0] == 0xfd &&
1408 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1409 (idev->rndid[7]&0x80))
1411 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1412 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1414 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1421 static void ipv6_regen_rndid(unsigned long data)
1423 struct inet6_dev *idev = (struct inet6_dev *) data;
1424 unsigned long expires;
1426 read_lock_bh(&addrconf_lock);
1427 write_lock_bh(&idev->lock);
1432 if (__ipv6_regen_rndid(idev) < 0)
1436 idev->cnf.temp_prefered_lft * HZ -
1437 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1438 if (time_before(expires, jiffies)) {
1440 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1445 if (!mod_timer(&idev->regen_timer, expires))
1449 write_unlock_bh(&idev->lock);
1450 read_unlock_bh(&addrconf_lock);
1454 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1457 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1458 ret = __ipv6_regen_rndid(idev);
1468 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1469 unsigned long expires, u32 flags)
1471 struct in6_rtmsg rtmsg;
1473 memset(&rtmsg, 0, sizeof(rtmsg));
1474 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1475 rtmsg.rtmsg_dst_len = plen;
1476 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1477 rtmsg.rtmsg_ifindex = dev->ifindex;
1478 rtmsg.rtmsg_info = expires;
1479 rtmsg.rtmsg_flags = RTF_UP|flags;
1480 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1482 /* Prevent useless cloning on PtP SIT.
1483 This thing is done here expecting that the whole
1484 class of non-broadcast devices need not cloning.
1486 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1487 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1489 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1492 /* Create "default" multicast route to the interface */
1494 static void addrconf_add_mroute(struct net_device *dev)
1496 struct in6_rtmsg rtmsg;
1498 memset(&rtmsg, 0, sizeof(rtmsg));
1499 ipv6_addr_set(&rtmsg.rtmsg_dst,
1500 htonl(0xFF000000), 0, 0, 0);
1501 rtmsg.rtmsg_dst_len = 8;
1502 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1503 rtmsg.rtmsg_ifindex = dev->ifindex;
1504 rtmsg.rtmsg_flags = RTF_UP;
1505 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1506 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1509 static void sit_route_add(struct net_device *dev)
1511 struct in6_rtmsg rtmsg;
1513 memset(&rtmsg, 0, sizeof(rtmsg));
1515 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1516 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1518 /* prefix length - 96 bits "::d.d.d.d" */
1519 rtmsg.rtmsg_dst_len = 96;
1520 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1521 rtmsg.rtmsg_ifindex = dev->ifindex;
1523 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1526 static void addrconf_add_lroute(struct net_device *dev)
1528 struct in6_addr addr;
1530 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1531 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1534 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1536 struct inet6_dev *idev;
1540 if ((idev = ipv6_find_idev(dev)) == NULL)
1543 /* Add default multicast route */
1544 addrconf_add_mroute(dev);
1546 /* Add link local route */
1547 addrconf_add_lroute(dev);
1551 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1553 struct prefix_info *pinfo;
1557 unsigned long rt_expires;
1558 struct inet6_dev *in6_dev;
1560 pinfo = (struct prefix_info *) opt;
1562 if (len < sizeof(struct prefix_info)) {
1563 ADBG(("addrconf: prefix option too short\n"));
1568 * Validation checks ([ADDRCONF], page 19)
1571 addr_type = ipv6_addr_type(&pinfo->prefix);
1573 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1576 valid_lft = ntohl(pinfo->valid);
1577 prefered_lft = ntohl(pinfo->prefered);
1579 if (prefered_lft > valid_lft) {
1580 if (net_ratelimit())
1581 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1585 in6_dev = in6_dev_get(dev);
1587 if (in6_dev == NULL) {
1588 if (net_ratelimit())
1589 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1594 * Two things going on here:
1595 * 1) Add routes for on-link prefixes
1596 * 2) Configure prefixes with the auto flag set
1599 /* Avoid arithmetic overflow. Really, we could
1600 save rt_expires in seconds, likely valid_lft,
1601 but it would require division in fib gc, that it
1604 if (valid_lft >= 0x7FFFFFFF/HZ)
1605 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1607 rt_expires = valid_lft * HZ;
1610 * We convert this (in jiffies) to clock_t later.
1611 * Avoid arithmetic overflow there as well.
1612 * Overflow can happen only if HZ < USER_HZ.
1614 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1615 rt_expires = 0x7FFFFFFF / USER_HZ;
1617 if (pinfo->onlink) {
1618 struct rt6_info *rt;
1619 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1621 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1622 if (rt->rt6i_flags&RTF_EXPIRES) {
1623 if (valid_lft == 0) {
1624 ip6_del_rt(rt, NULL, NULL, NULL);
1627 rt->rt6i_expires = jiffies + rt_expires;
1630 } else if (valid_lft) {
1631 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1632 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1635 dst_release(&rt->u.dst);
1638 /* Try to figure out our local address for this prefix */
1640 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1641 struct inet6_ifaddr * ifp;
1642 struct in6_addr addr;
1643 int create = 0, update_lft = 0;
1645 if (pinfo->prefix_len == 64) {
1646 memcpy(&addr, &pinfo->prefix, 8);
1647 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1648 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1649 in6_dev_put(in6_dev);
1654 if (net_ratelimit())
1655 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1657 in6_dev_put(in6_dev);
1662 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1664 if (ifp == NULL && valid_lft) {
1665 int max_addresses = in6_dev->cnf.max_addresses;
1667 /* Do not allow to create too much of autoconfigured
1668 * addresses; this would be too easy way to crash kernel.
1670 if (!max_addresses ||
1671 ipv6_count_addresses(in6_dev) < max_addresses)
1672 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1673 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1675 if (!ifp || IS_ERR(ifp)) {
1676 in6_dev_put(in6_dev);
1680 update_lft = create = 1;
1681 ifp->cstamp = jiffies;
1682 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1688 #ifdef CONFIG_IPV6_PRIVACY
1689 struct inet6_ifaddr *ift;
1693 /* update lifetime (RFC2462 5.5.3 e) */
1694 spin_lock(&ifp->lock);
1696 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1697 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1700 if (!update_lft && stored_lft) {
1701 if (valid_lft > MIN_VALID_LIFETIME ||
1702 valid_lft > stored_lft)
1704 else if (stored_lft <= MIN_VALID_LIFETIME) {
1705 /* valid_lft <= stored_lft is always true */
1709 valid_lft = MIN_VALID_LIFETIME;
1710 if (valid_lft < prefered_lft)
1711 prefered_lft = valid_lft;
1717 ifp->valid_lft = valid_lft;
1718 ifp->prefered_lft = prefered_lft;
1721 ifp->flags &= ~IFA_F_DEPRECATED;
1722 spin_unlock(&ifp->lock);
1724 if (!(flags&IFA_F_TENTATIVE))
1725 ipv6_ifa_notify(0, ifp);
1727 spin_unlock(&ifp->lock);
1729 #ifdef CONFIG_IPV6_PRIVACY
1730 read_lock_bh(&in6_dev->lock);
1731 /* update all temporary addresses in the list */
1732 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1734 * When adjusting the lifetimes of an existing
1735 * temporary address, only lower the lifetimes.
1736 * Implementations must not increase the
1737 * lifetimes of an existing temporary address
1738 * when processing a Prefix Information Option.
1740 spin_lock(&ift->lock);
1742 if (ift->valid_lft > valid_lft &&
1743 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1744 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1745 if (ift->prefered_lft > prefered_lft &&
1746 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1747 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1748 spin_unlock(&ift->lock);
1749 if (!(flags&IFA_F_TENTATIVE))
1750 ipv6_ifa_notify(0, ift);
1753 if (create && in6_dev->cnf.use_tempaddr > 0) {
1755 * When a new public address is created as described in [ADDRCONF],
1756 * also create a new temporary address.
1758 read_unlock_bh(&in6_dev->lock);
1759 ipv6_create_tempaddr(ifp, NULL);
1761 read_unlock_bh(&in6_dev->lock);
1768 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1769 in6_dev_put(in6_dev);
1773 * Set destination address.
1774 * Special case for SIT interfaces where we create a new "virtual"
1777 int addrconf_set_dstaddr(void __user *arg)
1779 struct in6_ifreq ireq;
1780 struct net_device *dev;
1786 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1789 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1795 if (dev->type == ARPHRD_SIT) {
1798 struct ip_tunnel_parm p;
1800 err = -EADDRNOTAVAIL;
1801 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1804 memset(&p, 0, sizeof(p));
1805 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1809 p.iph.protocol = IPPROTO_IPV6;
1811 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1813 oldfs = get_fs(); set_fs(KERNEL_DS);
1814 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1819 if ((dev = __dev_get_by_name(p.name)) == NULL)
1821 err = dev_open(dev);
1831 * Manual configuration of address on an interface
1833 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1835 struct inet6_ifaddr *ifp;
1836 struct inet6_dev *idev;
1837 struct net_device *dev;
1842 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1845 if (!(dev->flags&IFF_UP))
1848 if ((idev = addrconf_add_dev(dev)) == NULL)
1851 scope = ipv6_addr_scope(pfx);
1853 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1855 addrconf_dad_start(ifp, 0);
1860 return PTR_ERR(ifp);
1863 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1865 struct inet6_ifaddr *ifp;
1866 struct inet6_dev *idev;
1867 struct net_device *dev;
1869 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1872 if ((idev = __in6_dev_get(dev)) == NULL)
1875 read_lock_bh(&idev->lock);
1876 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1877 if (ifp->prefix_len == plen &&
1878 ipv6_addr_equal(pfx, &ifp->addr)) {
1880 read_unlock_bh(&idev->lock);
1884 /* If the last address is deleted administratively,
1885 disable IPv6 on this interface.
1887 if (idev->addr_list == NULL)
1888 addrconf_ifdown(idev->dev, 1);
1892 read_unlock_bh(&idev->lock);
1893 return -EADDRNOTAVAIL;
1897 int addrconf_add_ifaddr(void __user *arg)
1899 struct in6_ifreq ireq;
1902 if (!capable(CAP_NET_ADMIN))
1905 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1909 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1914 int addrconf_del_ifaddr(void __user *arg)
1916 struct in6_ifreq ireq;
1919 if (!capable(CAP_NET_ADMIN))
1922 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1926 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1931 static void sit_add_v4_addrs(struct inet6_dev *idev)
1933 struct inet6_ifaddr * ifp;
1934 struct in6_addr addr;
1935 struct net_device *dev;
1940 memset(&addr, 0, sizeof(struct in6_addr));
1941 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1943 if (idev->dev->flags&IFF_POINTOPOINT) {
1944 addr.s6_addr32[0] = htonl(0xfe800000);
1947 scope = IPV6_ADDR_COMPATv4;
1950 if (addr.s6_addr32[3]) {
1951 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1953 spin_lock_bh(&ifp->lock);
1954 ifp->flags &= ~IFA_F_TENTATIVE;
1955 spin_unlock_bh(&ifp->lock);
1956 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1962 for (dev = dev_base; dev != NULL; dev = dev->next) {
1963 struct in_device * in_dev = __in_dev_get_rtnl(dev);
1964 if (in_dev && (dev->flags & IFF_UP)) {
1965 struct in_ifaddr * ifa;
1969 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1972 addr.s6_addr32[3] = ifa->ifa_local;
1974 if (ifa->ifa_scope == RT_SCOPE_LINK)
1976 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1977 if (idev->dev->flags&IFF_POINTOPOINT)
1981 if (idev->dev->flags&IFF_POINTOPOINT)
1986 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1989 spin_lock_bh(&ifp->lock);
1990 ifp->flags &= ~IFA_F_TENTATIVE;
1991 spin_unlock_bh(&ifp->lock);
1992 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2000 static void init_loopback(struct net_device *dev)
2002 struct inet6_dev *idev;
2003 struct inet6_ifaddr * ifp;
2009 if ((idev = ipv6_find_idev(dev)) == NULL) {
2010 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2014 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2016 spin_lock_bh(&ifp->lock);
2017 ifp->flags &= ~IFA_F_TENTATIVE;
2018 spin_unlock_bh(&ifp->lock);
2019 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2024 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2026 struct inet6_ifaddr * ifp;
2028 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2030 addrconf_dad_start(ifp, 0);
2035 static void addrconf_dev_config(struct net_device *dev)
2037 struct in6_addr addr;
2038 struct inet6_dev * idev;
2042 if ((dev->type != ARPHRD_ETHER) &&
2043 (dev->type != ARPHRD_FDDI) &&
2044 (dev->type != ARPHRD_IEEE802_TR) &&
2045 (dev->type != ARPHRD_ARCNET) &&
2046 (dev->type != ARPHRD_INFINIBAND)) {
2047 /* Alas, we support only Ethernet autoconfiguration. */
2051 idev = addrconf_add_dev(dev);
2055 memset(&addr, 0, sizeof(struct in6_addr));
2056 addr.s6_addr32[0] = htonl(0xFE800000);
2058 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2059 addrconf_add_linklocal(idev, &addr);
2062 static void addrconf_sit_config(struct net_device *dev)
2064 struct inet6_dev *idev;
2069 * Configure the tunnel with one of our IPv4
2070 * addresses... we should configure all of
2071 * our v4 addrs in the tunnel
2074 if ((idev = ipv6_find_idev(dev)) == NULL) {
2075 printk(KERN_DEBUG "init sit: add_dev failed\n");
2079 sit_add_v4_addrs(idev);
2081 if (dev->flags&IFF_POINTOPOINT) {
2082 addrconf_add_mroute(dev);
2083 addrconf_add_lroute(dev);
2089 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2091 struct in6_addr lladdr;
2093 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2094 addrconf_add_linklocal(idev, &lladdr);
2100 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2102 struct net_device *link_dev;
2104 /* first try to inherit the link-local address from the link device */
2105 if (idev->dev->iflink &&
2106 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2107 if (!ipv6_inherit_linklocal(idev, link_dev))
2110 /* then try to inherit it from any device */
2111 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2112 if (!ipv6_inherit_linklocal(idev, link_dev))
2115 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2119 * Autoconfigure tunnel with a link-local address so routing protocols,
2120 * DHCPv6, MLD etc. can be run over the virtual link
2123 static void addrconf_ip6_tnl_config(struct net_device *dev)
2125 struct inet6_dev *idev;
2129 if ((idev = addrconf_add_dev(dev)) == NULL) {
2130 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2133 ip6_tnl_add_linklocal(idev);
2136 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2139 struct net_device *dev = (struct net_device *) data;
2140 struct inet6_dev *idev = __in6_dev_get(dev);
2141 int run_pending = 0;
2146 if (event == NETDEV_UP) {
2147 if (!netif_carrier_ok(dev)) {
2148 /* device is not ready yet. */
2150 "ADDRCONF(NETDEV_UP): %s: "
2151 "link is not ready\n",
2157 idev->if_flags |= IF_READY;
2159 if (!netif_carrier_ok(dev)) {
2160 /* device is still not ready. */
2165 if (idev->if_flags & IF_READY) {
2166 /* device is already configured. */
2169 idev->if_flags |= IF_READY;
2173 "ADDRCONF(NETDEV_CHANGE): %s: "
2174 "link becomes ready\n",
2182 addrconf_sit_config(dev);
2184 case ARPHRD_TUNNEL6:
2185 addrconf_ip6_tnl_config(dev);
2187 case ARPHRD_LOOPBACK:
2192 addrconf_dev_config(dev);
2197 addrconf_dad_run(idev);
2199 /* If the MTU changed during the interface down, when the
2200 interface up, the changed MTU must be reflected in the
2201 idev as well as routers.
2203 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2204 rt6_mtu_change(dev, dev->mtu);
2205 idev->cnf.mtu6 = dev->mtu;
2207 idev->tstamp = jiffies;
2208 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2209 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2210 stop IPv6 on this interface.
2212 if (dev->mtu < IPV6_MIN_MTU)
2213 addrconf_ifdown(dev, event != NETDEV_DOWN);
2217 case NETDEV_CHANGEMTU:
2218 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2219 rt6_mtu_change(dev, dev->mtu);
2220 idev->cnf.mtu6 = dev->mtu;
2224 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2227 case NETDEV_UNREGISTER:
2229 * Remove all addresses from this interface.
2231 addrconf_ifdown(dev, event != NETDEV_DOWN);
2234 case NETDEV_CHANGENAME:
2235 #ifdef CONFIG_SYSCTL
2237 addrconf_sysctl_unregister(&idev->cnf);
2238 neigh_sysctl_unregister(idev->nd_parms);
2239 neigh_sysctl_register(dev, idev->nd_parms,
2240 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2241 &ndisc_ifinfo_sysctl_change,
2243 addrconf_sysctl_register(idev, &idev->cnf);
2253 * addrconf module should be notified of a device going up
2255 static struct notifier_block ipv6_dev_notf = {
2256 .notifier_call = addrconf_notify,
2260 static int addrconf_ifdown(struct net_device *dev, int how)
2262 struct inet6_dev *idev;
2263 struct inet6_ifaddr *ifa, **bifa;
2268 if (dev == &loopback_dev && how == 1)
2272 neigh_ifdown(&nd_tbl, dev);
2274 idev = __in6_dev_get(dev);
2278 /* Step 1: remove reference to ipv6 device from parent device.
2282 write_lock_bh(&addrconf_lock);
2283 dev->ip6_ptr = NULL;
2285 write_unlock_bh(&addrconf_lock);
2287 /* Step 1.5: remove snmp6 entry */
2288 snmp6_unregister_dev(idev);
2292 /* Step 2: clear hash table */
2293 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2294 bifa = &inet6_addr_lst[i];
2296 write_lock_bh(&addrconf_hash_lock);
2297 while ((ifa = *bifa) != NULL) {
2298 if (ifa->idev == idev) {
2299 *bifa = ifa->lst_next;
2300 ifa->lst_next = NULL;
2301 addrconf_del_timer(ifa);
2305 bifa = &ifa->lst_next;
2307 write_unlock_bh(&addrconf_hash_lock);
2310 write_lock_bh(&idev->lock);
2312 /* Step 3: clear flags for stateless addrconf */
2314 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2316 /* Step 4: clear address list */
2317 #ifdef CONFIG_IPV6_PRIVACY
2318 if (how == 1 && del_timer(&idev->regen_timer))
2321 /* clear tempaddr list */
2322 while ((ifa = idev->tempaddr_list) != NULL) {
2323 idev->tempaddr_list = ifa->tmp_next;
2324 ifa->tmp_next = NULL;
2326 write_unlock_bh(&idev->lock);
2327 spin_lock_bh(&ifa->lock);
2330 in6_ifa_put(ifa->ifpub);
2333 spin_unlock_bh(&ifa->lock);
2335 write_lock_bh(&idev->lock);
2338 while ((ifa = idev->addr_list) != NULL) {
2339 idev->addr_list = ifa->if_next;
2340 ifa->if_next = NULL;
2342 addrconf_del_timer(ifa);
2343 write_unlock_bh(&idev->lock);
2345 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2348 write_lock_bh(&idev->lock);
2350 write_unlock_bh(&idev->lock);
2352 /* Step 5: Discard multicast list */
2355 ipv6_mc_destroy_dev(idev);
2359 /* Step 5: netlink notification of this interface */
2360 idev->tstamp = jiffies;
2361 inet6_ifinfo_notify(RTM_DELLINK, idev);
2363 /* Shot the device (if unregistered) */
2366 #ifdef CONFIG_SYSCTL
2367 addrconf_sysctl_unregister(&idev->cnf);
2368 neigh_sysctl_unregister(idev->nd_parms);
2370 neigh_parms_release(&nd_tbl, idev->nd_parms);
2371 neigh_ifdown(&nd_tbl, dev);
2377 static void addrconf_rs_timer(unsigned long data)
2379 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2381 if (ifp->idev->cnf.forwarding)
2384 if (ifp->idev->if_flags & IF_RA_RCVD) {
2386 * Announcement received after solicitation
2392 spin_lock(&ifp->lock);
2393 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2394 struct in6_addr all_routers;
2396 /* The wait after the last probe can be shorter */
2397 addrconf_mod_timer(ifp, AC_RS,
2398 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2399 ifp->idev->cnf.rtr_solicit_delay :
2400 ifp->idev->cnf.rtr_solicit_interval);
2401 spin_unlock(&ifp->lock);
2403 ipv6_addr_all_routers(&all_routers);
2405 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2407 spin_unlock(&ifp->lock);
2409 * Note: we do not support deprecated "all on-link"
2410 * assumption any longer.
2412 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2413 ifp->idev->dev->name);
2421 * Duplicate Address Detection
2423 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2425 unsigned long rand_num;
2426 struct inet6_dev *idev = ifp->idev;
2428 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2429 ifp->probes = idev->cnf.dad_transmits;
2430 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2433 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2435 struct inet6_dev *idev = ifp->idev;
2436 struct net_device *dev = idev->dev;
2438 addrconf_join_solict(dev, &ifp->addr);
2440 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2441 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2444 net_srandom(ifp->addr.s6_addr32[3]);
2446 read_lock_bh(&idev->lock);
2449 spin_lock_bh(&ifp->lock);
2451 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2452 !(ifp->flags&IFA_F_TENTATIVE)) {
2453 ifp->flags &= ~IFA_F_TENTATIVE;
2454 spin_unlock_bh(&ifp->lock);
2455 read_unlock_bh(&idev->lock);
2457 addrconf_dad_completed(ifp);
2461 if (!(idev->if_flags & IF_READY)) {
2462 spin_unlock_bh(&ifp->lock);
2463 read_unlock_bh(&idev->lock);
2465 * If the defice is not ready:
2466 * - keep it tentative if it is a permanent address.
2467 * - otherwise, kill it.
2470 addrconf_dad_stop(ifp);
2473 addrconf_dad_kick(ifp);
2474 spin_unlock_bh(&ifp->lock);
2476 read_unlock_bh(&idev->lock);
2479 static void addrconf_dad_timer(unsigned long data)
2481 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2482 struct inet6_dev *idev = ifp->idev;
2483 struct in6_addr unspec;
2484 struct in6_addr mcaddr;
2486 read_lock_bh(&idev->lock);
2488 read_unlock_bh(&idev->lock);
2491 spin_lock_bh(&ifp->lock);
2492 if (ifp->probes == 0) {
2494 * DAD was successful
2497 ifp->flags &= ~IFA_F_TENTATIVE;
2498 spin_unlock_bh(&ifp->lock);
2499 read_unlock_bh(&idev->lock);
2501 addrconf_dad_completed(ifp);
2507 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2508 spin_unlock_bh(&ifp->lock);
2509 read_unlock_bh(&idev->lock);
2511 /* send a neighbour solicitation for our addr */
2512 memset(&unspec, 0, sizeof(unspec));
2513 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2514 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2519 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2521 struct net_device * dev = ifp->idev->dev;
2524 * Configure the address for reception. Now it is valid.
2527 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2529 /* If added prefix is link local and forwarding is off,
2530 start sending router solicitations.
2533 if (ifp->idev->cnf.forwarding == 0 &&
2534 ifp->idev->cnf.rtr_solicits > 0 &&
2535 (dev->flags&IFF_LOOPBACK) == 0 &&
2536 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2537 struct in6_addr all_routers;
2539 ipv6_addr_all_routers(&all_routers);
2542 * If a host as already performed a random delay
2543 * [...] as part of DAD [...] there is no need
2544 * to delay again before sending the first RS
2546 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2548 spin_lock_bh(&ifp->lock);
2550 ifp->idev->if_flags |= IF_RS_SENT;
2551 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2552 spin_unlock_bh(&ifp->lock);
2556 static void addrconf_dad_run(struct inet6_dev *idev) {
2557 struct inet6_ifaddr *ifp;
2559 read_lock_bh(&idev->lock);
2560 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2561 spin_lock_bh(&ifp->lock);
2562 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2563 spin_unlock_bh(&ifp->lock);
2566 spin_unlock_bh(&ifp->lock);
2567 addrconf_dad_kick(ifp);
2569 read_unlock_bh(&idev->lock);
2572 #ifdef CONFIG_PROC_FS
2573 struct if6_iter_state {
2577 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2579 struct inet6_ifaddr *ifa = NULL;
2580 struct if6_iter_state *state = seq->private;
2582 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2583 ifa = inet6_addr_lst[state->bucket];
2590 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2592 struct if6_iter_state *state = seq->private;
2594 ifa = ifa->lst_next;
2596 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2597 ifa = inet6_addr_lst[state->bucket];
2603 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2605 struct inet6_ifaddr *ifa = if6_get_first(seq);
2608 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2610 return pos ? NULL : ifa;
2613 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2615 read_lock_bh(&addrconf_hash_lock);
2616 return if6_get_idx(seq, *pos);
2619 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2621 struct inet6_ifaddr *ifa;
2623 ifa = if6_get_next(seq, v);
2628 static void if6_seq_stop(struct seq_file *seq, void *v)
2630 read_unlock_bh(&addrconf_hash_lock);
2633 static int if6_seq_show(struct seq_file *seq, void *v)
2635 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2637 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
2639 ifp->idev->dev->ifindex,
2643 ifp->idev->dev->name);
2647 static struct seq_operations if6_seq_ops = {
2648 .start = if6_seq_start,
2649 .next = if6_seq_next,
2650 .show = if6_seq_show,
2651 .stop = if6_seq_stop,
2654 static int if6_seq_open(struct inode *inode, struct file *file)
2656 struct seq_file *seq;
2658 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2662 memset(s, 0, sizeof(*s));
2664 rc = seq_open(file, &if6_seq_ops);
2668 seq = file->private_data;
2677 static struct file_operations if6_fops = {
2678 .owner = THIS_MODULE,
2679 .open = if6_seq_open,
2681 .llseek = seq_lseek,
2682 .release = seq_release_private,
2685 int __init if6_proc_init(void)
2687 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2692 void if6_proc_exit(void)
2694 proc_net_remove("if_inet6");
2696 #endif /* CONFIG_PROC_FS */
2699 * Periodic address status verification
2702 static void addrconf_verify(unsigned long foo)
2704 struct inet6_ifaddr *ifp;
2705 unsigned long now, next;
2708 spin_lock_bh(&addrconf_verify_lock);
2710 next = now + ADDR_CHECK_FREQUENCY;
2712 del_timer(&addr_chk_timer);
2714 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2717 read_lock(&addrconf_hash_lock);
2718 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2720 #ifdef CONFIG_IPV6_PRIVACY
2721 unsigned long regen_advance;
2724 if (ifp->flags & IFA_F_PERMANENT)
2727 spin_lock(&ifp->lock);
2728 age = (now - ifp->tstamp) / HZ;
2730 #ifdef CONFIG_IPV6_PRIVACY
2731 regen_advance = ifp->idev->cnf.regen_max_retry *
2732 ifp->idev->cnf.dad_transmits *
2733 ifp->idev->nd_parms->retrans_time / HZ;
2736 if (age >= ifp->valid_lft) {
2737 spin_unlock(&ifp->lock);
2739 read_unlock(&addrconf_hash_lock);
2742 } else if (age >= ifp->prefered_lft) {
2743 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2746 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2748 ifp->flags |= IFA_F_DEPRECATED;
2751 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2752 next = ifp->tstamp + ifp->valid_lft * HZ;
2754 spin_unlock(&ifp->lock);
2758 read_unlock(&addrconf_hash_lock);
2760 ipv6_ifa_notify(0, ifp);
2764 #ifdef CONFIG_IPV6_PRIVACY
2765 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2766 !(ifp->flags&IFA_F_TENTATIVE)) {
2767 if (age >= ifp->prefered_lft - regen_advance) {
2768 struct inet6_ifaddr *ifpub = ifp->ifpub;
2769 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2770 next = ifp->tstamp + ifp->prefered_lft * HZ;
2771 if (!ifp->regen_count && ifpub) {
2774 in6_ifa_hold(ifpub);
2775 spin_unlock(&ifp->lock);
2776 read_unlock(&addrconf_hash_lock);
2777 spin_lock(&ifpub->lock);
2778 ifpub->regen_count = 0;
2779 spin_unlock(&ifpub->lock);
2780 ipv6_create_tempaddr(ifpub, ifp);
2785 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2786 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2787 spin_unlock(&ifp->lock);
2790 /* ifp->prefered_lft <= ifp->valid_lft */
2791 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2792 next = ifp->tstamp + ifp->prefered_lft * HZ;
2793 spin_unlock(&ifp->lock);
2796 read_unlock(&addrconf_hash_lock);
2799 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2800 add_timer(&addr_chk_timer);
2801 spin_unlock_bh(&addrconf_verify_lock);
2805 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2807 struct rtattr **rta = arg;
2808 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2809 struct in6_addr *pfx;
2812 if (rta[IFA_ADDRESS-1]) {
2813 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2815 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2817 if (rta[IFA_LOCAL-1]) {
2818 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2820 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2825 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2829 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2831 struct rtattr **rta = arg;
2832 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2833 struct in6_addr *pfx;
2836 if (rta[IFA_ADDRESS-1]) {
2837 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2839 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2841 if (rta[IFA_LOCAL-1]) {
2842 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2844 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2849 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2852 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2853 u32 pid, u32 seq, int event, unsigned int flags)
2855 struct ifaddrmsg *ifm;
2856 struct nlmsghdr *nlh;
2857 struct ifa_cacheinfo ci;
2858 unsigned char *b = skb->tail;
2860 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2861 ifm = NLMSG_DATA(nlh);
2862 ifm->ifa_family = AF_INET6;
2863 ifm->ifa_prefixlen = ifa->prefix_len;
2864 ifm->ifa_flags = ifa->flags;
2865 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2866 if (ifa->scope&IFA_HOST)
2867 ifm->ifa_scope = RT_SCOPE_HOST;
2868 else if (ifa->scope&IFA_LINK)
2869 ifm->ifa_scope = RT_SCOPE_LINK;
2870 else if (ifa->scope&IFA_SITE)
2871 ifm->ifa_scope = RT_SCOPE_SITE;
2872 ifm->ifa_index = ifa->idev->dev->ifindex;
2873 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2874 if (!(ifa->flags&IFA_F_PERMANENT)) {
2875 ci.ifa_prefered = ifa->prefered_lft;
2876 ci.ifa_valid = ifa->valid_lft;
2877 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2878 long tval = (jiffies - ifa->tstamp)/HZ;
2879 ci.ifa_prefered -= tval;
2880 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2881 ci.ifa_valid -= tval;
2884 ci.ifa_prefered = INFINITY_LIFE_TIME;
2885 ci.ifa_valid = INFINITY_LIFE_TIME;
2887 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2888 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2889 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2890 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2891 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2892 nlh->nlmsg_len = skb->tail - b;
2897 skb_trim(skb, b - skb->data);
2901 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2902 u32 pid, u32 seq, int event, u16 flags)
2904 struct ifaddrmsg *ifm;
2905 struct nlmsghdr *nlh;
2906 struct ifa_cacheinfo ci;
2907 unsigned char *b = skb->tail;
2909 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2910 ifm = NLMSG_DATA(nlh);
2911 ifm->ifa_family = AF_INET6;
2912 ifm->ifa_prefixlen = 128;
2913 ifm->ifa_flags = IFA_F_PERMANENT;
2914 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2915 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2916 ifm->ifa_scope = RT_SCOPE_SITE;
2917 ifm->ifa_index = ifmca->idev->dev->ifindex;
2918 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2919 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2920 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2922 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2923 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2925 ci.ifa_prefered = INFINITY_LIFE_TIME;
2926 ci.ifa_valid = INFINITY_LIFE_TIME;
2927 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2928 nlh->nlmsg_len = skb->tail - b;
2933 skb_trim(skb, b - skb->data);
2937 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2938 u32 pid, u32 seq, int event, unsigned int flags)
2940 struct ifaddrmsg *ifm;
2941 struct nlmsghdr *nlh;
2942 struct ifa_cacheinfo ci;
2943 unsigned char *b = skb->tail;
2945 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2946 ifm = NLMSG_DATA(nlh);
2947 ifm->ifa_family = AF_INET6;
2948 ifm->ifa_prefixlen = 128;
2949 ifm->ifa_flags = IFA_F_PERMANENT;
2950 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2951 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2952 ifm->ifa_scope = RT_SCOPE_SITE;
2953 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2954 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2955 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2956 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2958 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2959 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2961 ci.ifa_prefered = INFINITY_LIFE_TIME;
2962 ci.ifa_valid = INFINITY_LIFE_TIME;
2963 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2964 nlh->nlmsg_len = skb->tail - b;
2969 skb_trim(skb, b - skb->data);
2980 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2981 enum addr_type_t type)
2984 int s_idx, s_ip_idx;
2986 struct net_device *dev;
2987 struct inet6_dev *idev = NULL;
2988 struct inet6_ifaddr *ifa;
2989 struct ifmcaddr6 *ifmca;
2990 struct ifacaddr6 *ifaca;
2992 s_idx = cb->args[0];
2993 s_ip_idx = ip_idx = cb->args[1];
2994 read_lock(&dev_base_lock);
2996 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3002 if ((idev = in6_dev_get(dev)) == NULL)
3004 read_lock_bh(&idev->lock);
3007 /* unicast address incl. temp addr */
3008 for (ifa = idev->addr_list; ifa;
3009 ifa = ifa->if_next, ip_idx++) {
3010 if (ip_idx < s_ip_idx)
3012 if ((err = inet6_fill_ifaddr(skb, ifa,
3013 NETLINK_CB(cb->skb).pid,
3014 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3019 case MULTICAST_ADDR:
3020 /* multicast address */
3021 for (ifmca = idev->mc_list; ifmca;
3022 ifmca = ifmca->next, ip_idx++) {
3023 if (ip_idx < s_ip_idx)
3025 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3026 NETLINK_CB(cb->skb).pid,
3027 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3033 /* anycast address */
3034 for (ifaca = idev->ac_list; ifaca;
3035 ifaca = ifaca->aca_next, ip_idx++) {
3036 if (ip_idx < s_ip_idx)
3038 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3039 NETLINK_CB(cb->skb).pid,
3040 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3048 read_unlock_bh(&idev->lock);
3053 read_unlock_bh(&idev->lock);
3056 read_unlock(&dev_base_lock);
3058 cb->args[1] = ip_idx;
3062 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3064 enum addr_type_t type = UNICAST_ADDR;
3065 return inet6_dump_addr(skb, cb, type);
3068 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3070 enum addr_type_t type = MULTICAST_ADDR;
3071 return inet6_dump_addr(skb, cb, type);
3075 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3077 enum addr_type_t type = ANYCAST_ADDR;
3078 return inet6_dump_addr(skb, cb, type);
3081 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3083 struct sk_buff *skb;
3084 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
3086 skb = alloc_skb(size, GFP_ATOMIC);
3088 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
3091 if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
3093 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
3096 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
3097 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
3100 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3101 __s32 *array, int bytes)
3103 memset(array, 0, bytes);
3104 array[DEVCONF_FORWARDING] = cnf->forwarding;
3105 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3106 array[DEVCONF_MTU6] = cnf->mtu6;
3107 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3108 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3109 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3110 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3111 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3112 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3113 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3114 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3115 #ifdef CONFIG_IPV6_PRIVACY
3116 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3117 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3118 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3119 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3120 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3122 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3123 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3124 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3127 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3128 u32 pid, u32 seq, int event, unsigned int flags)
3130 struct net_device *dev = idev->dev;
3131 __s32 *array = NULL;
3132 struct ifinfomsg *r;
3133 struct nlmsghdr *nlh;
3134 unsigned char *b = skb->tail;
3135 struct rtattr *subattr;
3136 __u32 mtu = dev->mtu;
3137 struct ifla_cacheinfo ci;
3139 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
3140 r = NLMSG_DATA(nlh);
3141 r->ifi_family = AF_INET6;
3143 r->ifi_type = dev->type;
3144 r->ifi_index = dev->ifindex;
3145 r->ifi_flags = dev_get_flags(dev);
3148 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
3151 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3153 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
3154 if (dev->ifindex != dev->iflink)
3155 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
3157 subattr = (struct rtattr*)skb->tail;
3159 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
3161 /* return the device flags */
3162 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
3164 /* return interface cacheinfo */
3165 ci.max_reasm_len = IPV6_MAXPLEN;
3166 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3167 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3168 ci.reachable_time = idev->nd_parms->reachable_time;
3169 ci.retrans_time = idev->nd_parms->retrans_time;
3170 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3172 /* return the device sysctl params */
3173 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
3174 goto rtattr_failure;
3175 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
3176 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
3178 /* XXX - Statistics/MC not implemented */
3179 subattr->rta_len = skb->tail - (u8*)subattr;
3181 nlh->nlmsg_len = skb->tail - b;
3188 skb_trim(skb, b - skb->data);
3192 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3195 int s_idx = cb->args[0];
3196 struct net_device *dev;
3197 struct inet6_dev *idev;
3199 read_lock(&dev_base_lock);
3200 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3203 if ((idev = in6_dev_get(dev)) == NULL)
3205 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3206 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3211 read_unlock(&dev_base_lock);
3217 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3219 struct sk_buff *skb;
3221 int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
3223 skb = alloc_skb(size, GFP_ATOMIC);
3225 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
3228 if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
3230 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
3233 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
3234 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
3237 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3238 struct prefix_info *pinfo, u32 pid, u32 seq,
3239 int event, unsigned int flags)
3241 struct prefixmsg *pmsg;
3242 struct nlmsghdr *nlh;
3243 unsigned char *b = skb->tail;
3244 struct prefix_cacheinfo ci;
3246 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
3247 pmsg = NLMSG_DATA(nlh);
3248 pmsg->prefix_family = AF_INET6;
3249 pmsg->prefix_pad1 = 0;
3250 pmsg->prefix_pad2 = 0;
3251 pmsg->prefix_ifindex = idev->dev->ifindex;
3252 pmsg->prefix_len = pinfo->prefix_len;
3253 pmsg->prefix_type = pinfo->type;
3254 pmsg->prefix_pad3 = 0;
3256 pmsg->prefix_flags = 0;
3258 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3259 if (pinfo->autoconf)
3260 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3262 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3264 ci.preferred_time = ntohl(pinfo->prefered);
3265 ci.valid_time = ntohl(pinfo->valid);
3266 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3268 nlh->nlmsg_len = skb->tail - b;
3273 skb_trim(skb, b - skb->data);
3277 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3278 struct prefix_info *pinfo)
3280 struct sk_buff *skb;
3281 int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
3283 skb = alloc_skb(size, GFP_ATOMIC);
3285 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
3288 if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
3290 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
3293 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
3294 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
3297 static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
3298 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3299 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3300 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
3301 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
3302 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3303 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3304 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3305 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3306 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3307 .dumpit = inet6_dump_fib, },
3310 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3312 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3316 ip6_ins_rt(ifp->rt, NULL, NULL, NULL);
3317 if (ifp->idev->cnf.forwarding)
3318 addrconf_join_anycast(ifp);
3321 if (ifp->idev->cnf.forwarding)
3322 addrconf_leave_anycast(ifp);
3323 addrconf_leave_solict(ifp->idev, &ifp->addr);
3324 dst_hold(&ifp->rt->u.dst);
3325 if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
3326 dst_free(&ifp->rt->u.dst);
3331 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3333 read_lock_bh(&addrconf_lock);
3334 if (likely(ifp->idev->dead == 0))
3335 __ipv6_ifa_notify(event, ifp);
3336 read_unlock_bh(&addrconf_lock);
3339 #ifdef CONFIG_SYSCTL
3342 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3343 void __user *buffer, size_t *lenp, loff_t *ppos)
3345 int *valp = ctl->data;
3349 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3351 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3352 if (valp != &ipv6_devconf.forwarding) {
3353 if ((!*valp) ^ (!val)) {
3354 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3357 dev_forward_change(idev);
3360 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3361 addrconf_forward_change();
3364 rt6_purge_dflt_routers();
3370 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3371 int __user *name, int nlen,
3372 void __user *oldval,
3373 size_t __user *oldlenp,
3374 void __user *newval, size_t newlen,
3377 int *valp = table->data;
3380 if (!newval || !newlen)
3382 if (newlen != sizeof(int))
3384 if (get_user(new, (int __user *)newval))
3388 if (oldval && oldlenp) {
3390 if (get_user(len, oldlenp))
3393 if (len > table->maxlen)
3394 len = table->maxlen;
3395 if (copy_to_user(oldval, valp, len))
3397 if (put_user(len, oldlenp))
3402 if (valp != &ipv6_devconf_dflt.forwarding) {
3403 if (valp != &ipv6_devconf.forwarding) {
3404 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3406 if (unlikely(idev == NULL))
3408 changed = (!*valp) ^ (!new);
3411 dev_forward_change(idev);
3414 addrconf_forward_change();
3418 rt6_purge_dflt_routers();
3425 static struct addrconf_sysctl_table
3427 struct ctl_table_header *sysctl_header;
3428 ctl_table addrconf_vars[__NET_IPV6_MAX];
3429 ctl_table addrconf_dev[2];
3430 ctl_table addrconf_conf_dir[2];
3431 ctl_table addrconf_proto_dir[2];
3432 ctl_table addrconf_root_dir[2];
3433 } addrconf_sysctl = {
3434 .sysctl_header = NULL,
3437 .ctl_name = NET_IPV6_FORWARDING,
3438 .procname = "forwarding",
3439 .data = &ipv6_devconf.forwarding,
3440 .maxlen = sizeof(int),
3442 .proc_handler = &addrconf_sysctl_forward,
3443 .strategy = &addrconf_sysctl_forward_strategy,
3446 .ctl_name = NET_IPV6_HOP_LIMIT,
3447 .procname = "hop_limit",
3448 .data = &ipv6_devconf.hop_limit,
3449 .maxlen = sizeof(int),
3451 .proc_handler = proc_dointvec,
3454 .ctl_name = NET_IPV6_MTU,
3456 .data = &ipv6_devconf.mtu6,
3457 .maxlen = sizeof(int),
3459 .proc_handler = &proc_dointvec,
3462 .ctl_name = NET_IPV6_ACCEPT_RA,
3463 .procname = "accept_ra",
3464 .data = &ipv6_devconf.accept_ra,
3465 .maxlen = sizeof(int),
3467 .proc_handler = &proc_dointvec,
3470 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3471 .procname = "accept_redirects",
3472 .data = &ipv6_devconf.accept_redirects,
3473 .maxlen = sizeof(int),
3475 .proc_handler = &proc_dointvec,
3478 .ctl_name = NET_IPV6_AUTOCONF,
3479 .procname = "autoconf",
3480 .data = &ipv6_devconf.autoconf,
3481 .maxlen = sizeof(int),
3483 .proc_handler = &proc_dointvec,
3486 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3487 .procname = "dad_transmits",
3488 .data = &ipv6_devconf.dad_transmits,
3489 .maxlen = sizeof(int),
3491 .proc_handler = &proc_dointvec,
3494 .ctl_name = NET_IPV6_RTR_SOLICITS,
3495 .procname = "router_solicitations",
3496 .data = &ipv6_devconf.rtr_solicits,
3497 .maxlen = sizeof(int),
3499 .proc_handler = &proc_dointvec,
3502 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3503 .procname = "router_solicitation_interval",
3504 .data = &ipv6_devconf.rtr_solicit_interval,
3505 .maxlen = sizeof(int),
3507 .proc_handler = &proc_dointvec_jiffies,
3508 .strategy = &sysctl_jiffies,
3511 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3512 .procname = "router_solicitation_delay",
3513 .data = &ipv6_devconf.rtr_solicit_delay,
3514 .maxlen = sizeof(int),
3516 .proc_handler = &proc_dointvec_jiffies,
3517 .strategy = &sysctl_jiffies,
3520 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3521 .procname = "force_mld_version",
3522 .data = &ipv6_devconf.force_mld_version,
3523 .maxlen = sizeof(int),
3525 .proc_handler = &proc_dointvec,
3527 #ifdef CONFIG_IPV6_PRIVACY
3529 .ctl_name = NET_IPV6_USE_TEMPADDR,
3530 .procname = "use_tempaddr",
3531 .data = &ipv6_devconf.use_tempaddr,
3532 .maxlen = sizeof(int),
3534 .proc_handler = &proc_dointvec,
3537 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3538 .procname = "temp_valid_lft",
3539 .data = &ipv6_devconf.temp_valid_lft,
3540 .maxlen = sizeof(int),
3542 .proc_handler = &proc_dointvec,
3545 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3546 .procname = "temp_prefered_lft",
3547 .data = &ipv6_devconf.temp_prefered_lft,
3548 .maxlen = sizeof(int),
3550 .proc_handler = &proc_dointvec,
3553 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3554 .procname = "regen_max_retry",
3555 .data = &ipv6_devconf.regen_max_retry,
3556 .maxlen = sizeof(int),
3558 .proc_handler = &proc_dointvec,
3561 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3562 .procname = "max_desync_factor",
3563 .data = &ipv6_devconf.max_desync_factor,
3564 .maxlen = sizeof(int),
3566 .proc_handler = &proc_dointvec,
3570 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3571 .procname = "max_addresses",
3572 .data = &ipv6_devconf.max_addresses,
3573 .maxlen = sizeof(int),
3575 .proc_handler = &proc_dointvec,
3578 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
3579 .procname = "accept_ra_defrtr",
3580 .data = &ipv6_devconf.accept_ra_defrtr,
3581 .maxlen = sizeof(int),
3583 .proc_handler = &proc_dointvec,
3586 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
3587 .procname = "accept_ra_pinfo",
3588 .data = &ipv6_devconf.accept_ra_pinfo,
3589 .maxlen = sizeof(int),
3591 .proc_handler = &proc_dointvec,
3594 .ctl_name = 0, /* sentinel */
3599 .ctl_name = NET_PROTO_CONF_ALL,
3602 .child = addrconf_sysctl.addrconf_vars,
3605 .ctl_name = 0, /* sentinel */
3608 .addrconf_conf_dir = {
3610 .ctl_name = NET_IPV6_CONF,
3613 .child = addrconf_sysctl.addrconf_dev,
3616 .ctl_name = 0, /* sentinel */
3619 .addrconf_proto_dir = {
3621 .ctl_name = NET_IPV6,
3624 .child = addrconf_sysctl.addrconf_conf_dir,
3627 .ctl_name = 0, /* sentinel */
3630 .addrconf_root_dir = {
3632 .ctl_name = CTL_NET,
3635 .child = addrconf_sysctl.addrconf_proto_dir,
3638 .ctl_name = 0, /* sentinel */
3643 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3646 struct net_device *dev = idev ? idev->dev : NULL;
3647 struct addrconf_sysctl_table *t;
3648 char *dev_name = NULL;
3650 t = kmalloc(sizeof(*t), GFP_KERNEL);
3653 memcpy(t, &addrconf_sysctl, sizeof(*t));
3654 for (i=0; t->addrconf_vars[i].data; i++) {
3655 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3656 t->addrconf_vars[i].de = NULL;
3657 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3660 dev_name = dev->name;
3661 t->addrconf_dev[0].ctl_name = dev->ifindex;
3663 dev_name = "default";
3664 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3668 * Make a copy of dev_name, because '.procname' is regarded as const
3669 * by sysctl and we wouldn't want anyone to change it under our feet
3670 * (see SIOCSIFNAME).
3672 dev_name = kstrdup(dev_name, GFP_KERNEL);
3676 t->addrconf_dev[0].procname = dev_name;
3678 t->addrconf_dev[0].child = t->addrconf_vars;
3679 t->addrconf_dev[0].de = NULL;
3680 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3681 t->addrconf_conf_dir[0].de = NULL;
3682 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3683 t->addrconf_proto_dir[0].de = NULL;
3684 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3685 t->addrconf_root_dir[0].de = NULL;
3687 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3688 if (t->sysctl_header == NULL)
3703 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3706 struct addrconf_sysctl_table *t = p->sysctl;
3708 unregister_sysctl_table(t->sysctl_header);
3709 kfree(t->addrconf_dev[0].procname);
3721 int register_inet6addr_notifier(struct notifier_block *nb)
3723 return notifier_chain_register(&inet6addr_chain, nb);
3726 int unregister_inet6addr_notifier(struct notifier_block *nb)
3728 return notifier_chain_unregister(&inet6addr_chain,nb);
3732 * Init / cleanup code
3735 int __init addrconf_init(void)
3739 /* The addrconf netdev notifier requires that loopback_dev
3740 * has it's ipv6 private information allocated and setup
3741 * before it can bring up and give link-local addresses
3742 * to other devices which are up.
3744 * Unfortunately, loopback_dev is not necessarily the first
3745 * entry in the global dev_base list of net devices. In fact,
3746 * it is likely to be the very last entry on that list.
3747 * So this causes the notifier registry below to try and
3748 * give link-local addresses to all devices besides loopback_dev
3749 * first, then loopback_dev, which cases all the non-loopback_dev
3750 * devices to fail to get a link-local address.
3752 * So, as a temporary fix, allocate the ipv6 structure for
3753 * loopback_dev first by hand.
3754 * Longer term, all of the dependencies ipv6 has upon the loopback
3755 * device and it being up should be removed.
3758 if (!ipv6_add_dev(&loopback_dev))
3764 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
3766 register_netdevice_notifier(&ipv6_dev_notf);
3769 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3770 #ifdef CONFIG_SYSCTL
3771 addrconf_sysctl.sysctl_header =
3772 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3773 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3779 void __exit addrconf_cleanup(void)
3781 struct net_device *dev;
3782 struct inet6_dev *idev;
3783 struct inet6_ifaddr *ifa;
3786 unregister_netdevice_notifier(&ipv6_dev_notf);
3788 rtnetlink_links[PF_INET6] = NULL;
3789 #ifdef CONFIG_SYSCTL
3790 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3791 addrconf_sysctl_unregister(&ipv6_devconf);
3800 for (dev=dev_base; dev; dev=dev->next) {
3801 if ((idev = __in6_dev_get(dev)) == NULL)
3803 addrconf_ifdown(dev, 1);
3805 addrconf_ifdown(&loopback_dev, 2);
3811 write_lock_bh(&addrconf_hash_lock);
3812 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3813 for (ifa=inet6_addr_lst[i]; ifa; ) {
3814 struct inet6_ifaddr *bifa;
3817 ifa = ifa->lst_next;
3818 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3819 /* Do not free it; something is wrong.
3820 Now we can investigate it with debugger.
3824 write_unlock_bh(&addrconf_hash_lock);
3826 del_timer(&addr_chk_timer);
3830 #ifdef CONFIG_PROC_FS
3831 proc_net_remove("if_inet6");