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,
170 static struct ipv6_devconf ipv6_devconf_dflt = {
172 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
173 .mtu6 = IPV6_MIN_MTU,
175 .accept_redirects = 1,
178 .rtr_solicits = MAX_RTR_SOLICITATIONS,
179 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
180 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
181 #ifdef CONFIG_IPV6_PRIVACY
183 .temp_valid_lft = TEMP_VALID_LIFETIME,
184 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
185 .regen_max_retry = REGEN_MAX_RETRY,
186 .max_desync_factor = MAX_DESYNC_FACTOR,
188 .max_addresses = IPV6_MAX_ADDRESSES,
191 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
193 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
195 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
197 #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
199 static inline unsigned ipv6_addr_scope2type(unsigned scope)
202 case IPV6_ADDR_SCOPE_NODELOCAL:
203 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
205 case IPV6_ADDR_SCOPE_LINKLOCAL:
206 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
207 IPV6_ADDR_LINKLOCAL);
208 case IPV6_ADDR_SCOPE_SITELOCAL:
209 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
210 IPV6_ADDR_SITELOCAL);
212 return IPV6_ADDR_SCOPE_TYPE(scope);
215 int __ipv6_addr_type(const struct in6_addr *addr)
219 st = addr->s6_addr32[0];
221 /* Consider all addresses with the first three bits different of
222 000 and 111 as unicasts.
224 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
225 (st & htonl(0xE0000000)) != htonl(0xE0000000))
226 return (IPV6_ADDR_UNICAST |
227 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
229 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
231 /* addr-select 3.1 */
232 return (IPV6_ADDR_MULTICAST |
233 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
236 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
237 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
238 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
239 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
240 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
241 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
243 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
244 if (addr->s6_addr32[2] == 0) {
245 if (addr->s6_addr32[3] == 0)
246 return IPV6_ADDR_ANY;
248 if (addr->s6_addr32[3] == htonl(0x00000001))
249 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
250 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
252 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
253 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
256 if (addr->s6_addr32[2] == htonl(0x0000ffff))
257 return (IPV6_ADDR_MAPPED |
258 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
261 return (IPV6_ADDR_RESERVED |
262 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
265 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
267 if (del_timer(&ifp->timer))
271 enum addrconf_timer_t
278 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
279 enum addrconf_timer_t what,
282 if (!del_timer(&ifp->timer))
287 ifp->timer.function = addrconf_dad_timer;
290 ifp->timer.function = addrconf_rs_timer;
294 ifp->timer.expires = jiffies + when;
295 add_timer(&ifp->timer);
298 /* Nobody refers to this device, we may destroy it. */
300 void in6_dev_finish_destroy(struct inet6_dev *idev)
302 struct net_device *dev = idev->dev;
303 BUG_TRAP(idev->addr_list==NULL);
304 BUG_TRAP(idev->mc_list==NULL);
305 #ifdef NET_REFCNT_DEBUG
306 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
310 printk("Freeing alive inet6 device %p\n", idev);
313 snmp6_free_dev(idev);
317 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
319 struct inet6_dev *ndev;
323 if (dev->mtu < IPV6_MIN_MTU)
326 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
329 memset(ndev, 0, sizeof(struct inet6_dev));
331 rwlock_init(&ndev->lock);
333 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
334 ndev->cnf.mtu6 = dev->mtu;
335 ndev->cnf.sysctl = NULL;
336 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
337 if (ndev->nd_parms == NULL) {
341 /* We refer to the device */
344 if (snmp6_alloc_dev(ndev) < 0) {
346 "%s(): cannot allocate memory for statistics; dev=%s.\n",
347 __FUNCTION__, dev->name));
348 neigh_parms_release(&nd_tbl, ndev->nd_parms);
350 in6_dev_finish_destroy(ndev);
354 if (snmp6_register_dev(ndev) < 0) {
356 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
357 __FUNCTION__, dev->name));
358 neigh_parms_release(&nd_tbl, ndev->nd_parms);
360 in6_dev_finish_destroy(ndev);
364 /* One reference from device. We must do this before
365 * we invoke __ipv6_regen_rndid().
369 #ifdef CONFIG_IPV6_PRIVACY
370 init_timer(&ndev->regen_timer);
371 ndev->regen_timer.function = ipv6_regen_rndid;
372 ndev->regen_timer.data = (unsigned long) ndev;
373 if ((dev->flags&IFF_LOOPBACK) ||
374 dev->type == ARPHRD_TUNNEL ||
375 dev->type == ARPHRD_NONE ||
376 dev->type == ARPHRD_SIT) {
378 "%s: Disabled Privacy Extensions\n",
380 ndev->cnf.use_tempaddr = -1;
383 ipv6_regen_rndid((unsigned long) ndev);
387 if (netif_carrier_ok(dev))
388 ndev->if_flags |= IF_READY;
390 write_lock_bh(&addrconf_lock);
392 write_unlock_bh(&addrconf_lock);
394 ipv6_mc_init_dev(ndev);
395 ndev->tstamp = jiffies;
397 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
398 NET_IPV6_NEIGH, "ipv6",
399 &ndisc_ifinfo_sysctl_change,
401 addrconf_sysctl_register(ndev, &ndev->cnf);
407 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
409 struct inet6_dev *idev;
413 if ((idev = __in6_dev_get(dev)) == NULL) {
414 if ((idev = ipv6_add_dev(dev)) == NULL)
418 if (dev->flags&IFF_UP)
424 static void dev_forward_change(struct inet6_dev *idev)
426 struct net_device *dev;
427 struct inet6_ifaddr *ifa;
428 struct in6_addr addr;
433 if (dev && (dev->flags & IFF_MULTICAST)) {
434 ipv6_addr_all_routers(&addr);
436 if (idev->cnf.forwarding)
437 ipv6_dev_mc_inc(dev, &addr);
439 ipv6_dev_mc_dec(dev, &addr);
441 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
442 if (idev->cnf.forwarding)
443 addrconf_join_anycast(ifa);
445 addrconf_leave_anycast(ifa);
450 static void addrconf_forward_change(void)
452 struct net_device *dev;
453 struct inet6_dev *idev;
455 read_lock(&dev_base_lock);
456 for (dev=dev_base; dev; dev=dev->next) {
457 read_lock(&addrconf_lock);
458 idev = __in6_dev_get(dev);
460 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
461 idev->cnf.forwarding = ipv6_devconf.forwarding;
463 dev_forward_change(idev);
465 read_unlock(&addrconf_lock);
467 read_unlock(&dev_base_lock);
471 /* Nobody refers to this ifaddr, destroy it */
473 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
475 BUG_TRAP(ifp->if_next==NULL);
476 BUG_TRAP(ifp->lst_next==NULL);
477 #ifdef NET_REFCNT_DEBUG
478 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
481 in6_dev_put(ifp->idev);
483 if (del_timer(&ifp->timer))
484 printk("Timer is still running, when freeing ifa=%p\n", ifp);
487 printk("Freeing alive inet6 address %p\n", ifp);
490 dst_release(&ifp->rt->u.dst);
495 /* On success it returns ifp with increased reference count */
497 static struct inet6_ifaddr *
498 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
499 int scope, u32 flags)
501 struct inet6_ifaddr *ifa = NULL;
506 read_lock_bh(&addrconf_lock);
508 err = -ENODEV; /*XXX*/
512 write_lock(&addrconf_hash_lock);
514 /* Ignore adding duplicate addresses on an interface */
515 if (ipv6_chk_same_addr(addr, idev->dev)) {
516 ADBG(("ipv6_add_addr: already assigned\n"));
521 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
524 ADBG(("ipv6_add_addr: malloc failed\n"));
529 rt = addrconf_dst_alloc(idev, addr, 0);
535 memset(ifa, 0, sizeof(struct inet6_ifaddr));
536 ipv6_addr_copy(&ifa->addr, addr);
538 spin_lock_init(&ifa->lock);
539 init_timer(&ifa->timer);
540 ifa->timer.data = (unsigned long) ifa;
542 ifa->prefix_len = pfxlen;
543 ifa->flags = flags | IFA_F_TENTATIVE;
544 ifa->cstamp = ifa->tstamp = jiffies;
551 /* Add to big hash table */
552 hash = ipv6_addr_hash(addr);
554 ifa->lst_next = inet6_addr_lst[hash];
555 inet6_addr_lst[hash] = ifa;
557 write_unlock(&addrconf_hash_lock);
559 write_lock(&idev->lock);
560 /* Add to inet6_dev unicast addr list. */
561 ifa->if_next = idev->addr_list;
562 idev->addr_list = ifa;
564 #ifdef CONFIG_IPV6_PRIVACY
565 if (ifa->flags&IFA_F_TEMPORARY) {
566 ifa->tmp_next = idev->tempaddr_list;
567 idev->tempaddr_list = ifa;
575 write_unlock(&idev->lock);
577 read_unlock_bh(&addrconf_lock);
579 if (likely(err == 0))
580 notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
588 write_unlock(&addrconf_hash_lock);
592 /* This function wants to get referenced ifp and releases it before return */
594 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
596 struct inet6_ifaddr *ifa, **ifap;
597 struct inet6_dev *idev = ifp->idev;
599 int deleted = 0, onlink = 0;
600 unsigned long expires = jiffies;
602 hash = ipv6_addr_hash(&ifp->addr);
606 write_lock_bh(&addrconf_hash_lock);
607 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
608 ifap = &ifa->lst_next) {
610 *ifap = ifa->lst_next;
612 ifa->lst_next = NULL;
616 write_unlock_bh(&addrconf_hash_lock);
618 write_lock_bh(&idev->lock);
619 #ifdef CONFIG_IPV6_PRIVACY
620 if (ifp->flags&IFA_F_TEMPORARY) {
621 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
622 ifap = &ifa->tmp_next) {
624 *ifap = ifa->tmp_next;
626 in6_ifa_put(ifp->ifpub);
630 ifa->tmp_next = NULL;
637 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
639 *ifap = ifa->if_next;
642 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
646 } else if (ifp->flags & IFA_F_PERMANENT) {
647 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
649 if (ifa->flags & IFA_F_PERMANENT) {
654 unsigned long lifetime;
659 spin_lock(&ifa->lock);
660 lifetime = min_t(unsigned long,
661 ifa->valid_lft, 0x7fffffffUL/HZ);
662 if (time_before(expires,
663 ifa->tstamp + lifetime * HZ))
664 expires = ifa->tstamp + lifetime * HZ;
665 spin_unlock(&ifa->lock);
669 ifap = &ifa->if_next;
671 write_unlock_bh(&idev->lock);
673 ipv6_ifa_notify(RTM_DELADDR, ifp);
675 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
677 addrconf_del_timer(ifp);
680 * Purge or update corresponding prefix
682 * 1) we don't purge prefix here if address was not permanent.
683 * prefix is managed by its own lifetime.
684 * 2) if there're no addresses, delete prefix.
685 * 3) if there're still other permanent address(es),
686 * corresponding prefix is still permanent.
687 * 4) otherwise, update prefix lifetime to the
688 * longest valid lifetime among the corresponding
689 * addresses on the device.
690 * Note: subsequent RA will update lifetime.
694 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
695 struct in6_addr prefix;
698 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
699 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
701 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
703 ip6_del_rt(rt, NULL, NULL, NULL);
705 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
706 rt->rt6i_expires = expires;
707 rt->rt6i_flags |= RTF_EXPIRES;
710 dst_release(&rt->u.dst);
716 #ifdef CONFIG_IPV6_PRIVACY
717 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
719 struct inet6_dev *idev = ifp->idev;
720 struct in6_addr addr, *tmpaddr;
721 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
726 write_lock(&idev->lock);
728 spin_lock_bh(&ift->lock);
729 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
730 spin_unlock_bh(&ift->lock);
737 if (idev->cnf.use_tempaddr <= 0) {
738 write_unlock(&idev->lock);
740 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
745 spin_lock_bh(&ifp->lock);
746 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
747 idev->cnf.use_tempaddr = -1; /*XXX*/
748 spin_unlock_bh(&ifp->lock);
749 write_unlock(&idev->lock);
751 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
757 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
758 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
759 spin_unlock_bh(&ifp->lock);
760 write_unlock(&idev->lock);
762 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
768 memcpy(&addr.s6_addr[8], idev->rndid, 8);
769 tmp_valid_lft = min_t(__u32,
771 idev->cnf.temp_valid_lft);
772 tmp_prefered_lft = min_t(__u32,
774 idev->cnf.temp_prefered_lft - desync_factor / HZ);
775 tmp_plen = ifp->prefix_len;
776 max_addresses = idev->cnf.max_addresses;
777 tmp_cstamp = ifp->cstamp;
778 tmp_tstamp = ifp->tstamp;
779 spin_unlock_bh(&ifp->lock);
781 write_unlock(&idev->lock);
782 ift = !max_addresses ||
783 ipv6_count_addresses(idev) < max_addresses ?
784 ipv6_add_addr(idev, &addr, tmp_plen,
785 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
786 if (!ift || IS_ERR(ift)) {
790 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
792 write_lock(&idev->lock);
796 spin_lock_bh(&ift->lock);
798 ift->valid_lft = tmp_valid_lft;
799 ift->prefered_lft = tmp_prefered_lft;
800 ift->cstamp = tmp_cstamp;
801 ift->tstamp = tmp_tstamp;
802 spin_unlock_bh(&ift->lock);
804 addrconf_dad_start(ift, 0);
813 * Choose an appropriate source address (RFC3484)
815 struct ipv6_saddr_score {
823 #define IPV6_SADDR_SCORE_LOCAL 0x0001
824 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
825 #define IPV6_SADDR_SCORE_HOA 0x0008
826 #define IPV6_SADDR_SCORE_OIF 0x0010
827 #define IPV6_SADDR_SCORE_LABEL 0x0020
828 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
830 static int inline ipv6_saddr_preferred(int type)
832 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
833 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
838 /* static matching label */
839 static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
842 * prefix (longest match) label
843 * -----------------------------
850 if (type & IPV6_ADDR_LOOPBACK)
852 else if (type & IPV6_ADDR_COMPATv4)
854 else if (type & IPV6_ADDR_MAPPED)
856 else if (addr->s6_addr16[0] == htons(0x2002))
861 int ipv6_dev_get_saddr(struct net_device *daddr_dev,
862 struct in6_addr *daddr, struct in6_addr *saddr)
864 struct ipv6_saddr_score hiscore;
865 struct inet6_ifaddr *ifa_result = NULL;
866 int daddr_type = __ipv6_addr_type(daddr);
867 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
868 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
869 struct net_device *dev;
871 memset(&hiscore, 0, sizeof(hiscore));
873 read_lock(&dev_base_lock);
874 read_lock(&addrconf_lock);
876 for (dev = dev_base; dev; dev=dev->next) {
877 struct inet6_dev *idev;
878 struct inet6_ifaddr *ifa;
880 /* Rule 0: Candidate Source Address (section 4)
881 * - multicast and link-local destination address,
882 * the set of candidate source address MUST only
883 * include addresses assigned to interfaces
884 * belonging to the same link as the outgoing
886 * (- For site-local destination addresses, the
887 * set of candidate source addresses MUST only
888 * include addresses assigned to interfaces
889 * belonging to the same site as the outgoing
892 if ((daddr_type & IPV6_ADDR_MULTICAST ||
893 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
894 daddr_dev && dev != daddr_dev)
897 idev = __in6_dev_get(dev);
901 read_lock_bh(&idev->lock);
902 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
903 struct ipv6_saddr_score score;
905 score.addr_type = __ipv6_addr_type(&ifa->addr);
908 * - Tentative Address (RFC2462 section 5.4)
909 * - A tentative address is not considered
910 * "assigned to an interface" in the traditional
912 * - Candidate Source Address (section 4)
913 * - In any case, anycast addresses, multicast
914 * addresses, and the unspecified address MUST
915 * NOT be included in a candidate set.
917 if (ifa->flags & IFA_F_TENTATIVE)
919 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
920 score.addr_type & IPV6_ADDR_MULTICAST)) {
921 LIMIT_NETDEBUG(KERN_DEBUG
922 "ADDRCONF: unspecified / multicast address"
923 "assigned as unicast address on %s",
933 if (ifa_result == NULL) {
934 /* record it if the first available entry */
938 /* Rule 1: Prefer same address */
939 if (hiscore.rule < 1) {
940 if (ipv6_addr_equal(&ifa_result->addr, daddr))
941 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
944 if (ipv6_addr_equal(&ifa->addr, daddr)) {
945 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
946 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
951 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
955 /* Rule 2: Prefer appropriate scope */
956 if (hiscore.rule < 2) {
957 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
960 score.scope = __ipv6_addr_src_scope(score.addr_type);
961 if (hiscore.scope < score.scope) {
962 if (hiscore.scope < daddr_scope) {
967 } else if (score.scope < hiscore.scope) {
968 if (score.scope < daddr_scope)
976 /* Rule 3: Avoid deprecated address */
977 if (hiscore.rule < 3) {
978 if (ipv6_saddr_preferred(hiscore.addr_type) ||
979 !(ifa_result->flags & IFA_F_DEPRECATED))
980 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
983 if (ipv6_saddr_preferred(score.addr_type) ||
984 !(ifa->flags & IFA_F_DEPRECATED)) {
985 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
986 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
991 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
995 /* Rule 4: Prefer home address -- not implemented yet */
996 if (hiscore.rule < 4)
999 /* Rule 5: Prefer outgoing interface */
1000 if (hiscore.rule < 5) {
1001 if (daddr_dev == NULL ||
1002 daddr_dev == ifa_result->idev->dev)
1003 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1006 if (daddr_dev == NULL ||
1007 daddr_dev == ifa->idev->dev) {
1008 score.attrs |= IPV6_SADDR_SCORE_OIF;
1009 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1014 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1018 /* Rule 6: Prefer matching label */
1019 if (hiscore.rule < 6) {
1020 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1021 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1024 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1025 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1026 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1031 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1035 #ifdef CONFIG_IPV6_PRIVACY
1036 /* Rule 7: Prefer public address
1037 * Note: prefer temprary address if use_tempaddr >= 2
1039 if (hiscore.rule < 7) {
1040 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1041 (ifa_result->idev->cnf.use_tempaddr >= 2))
1042 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1045 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1046 (ifa->idev->cnf.use_tempaddr >= 2)) {
1047 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1048 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1053 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1057 /* Rule 8: Use longest matching prefix */
1058 if (hiscore.rule < 8) {
1059 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
1062 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1063 if (score.matchlen > hiscore.matchlen) {
1068 else if (score.matchlen < hiscore.matchlen)
1072 /* Final Rule: choose first available one */
1076 in6_ifa_put(ifa_result);
1081 read_unlock_bh(&idev->lock);
1083 read_unlock(&addrconf_lock);
1084 read_unlock(&dev_base_lock);
1087 return -EADDRNOTAVAIL;
1089 ipv6_addr_copy(saddr, &ifa_result->addr);
1090 in6_ifa_put(ifa_result);
1095 int ipv6_get_saddr(struct dst_entry *dst,
1096 struct in6_addr *daddr, struct in6_addr *saddr)
1098 return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
1102 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1104 struct inet6_dev *idev;
1105 int err = -EADDRNOTAVAIL;
1107 read_lock(&addrconf_lock);
1108 if ((idev = __in6_dev_get(dev)) != NULL) {
1109 struct inet6_ifaddr *ifp;
1111 read_lock_bh(&idev->lock);
1112 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1113 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1114 ipv6_addr_copy(addr, &ifp->addr);
1119 read_unlock_bh(&idev->lock);
1121 read_unlock(&addrconf_lock);
1125 static int ipv6_count_addresses(struct inet6_dev *idev)
1128 struct inet6_ifaddr *ifp;
1130 read_lock_bh(&idev->lock);
1131 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1133 read_unlock_bh(&idev->lock);
1137 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1139 struct inet6_ifaddr * ifp;
1140 u8 hash = ipv6_addr_hash(addr);
1142 read_lock_bh(&addrconf_hash_lock);
1143 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1144 if (ipv6_addr_equal(&ifp->addr, addr) &&
1145 !(ifp->flags&IFA_F_TENTATIVE)) {
1146 if (dev == NULL || ifp->idev->dev == dev ||
1147 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1151 read_unlock_bh(&addrconf_hash_lock);
1156 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1158 struct inet6_ifaddr * ifp;
1159 u8 hash = ipv6_addr_hash(addr);
1161 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1162 if (ipv6_addr_equal(&ifp->addr, addr)) {
1163 if (dev == NULL || ifp->idev->dev == dev)
1170 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1172 struct inet6_ifaddr * ifp;
1173 u8 hash = ipv6_addr_hash(addr);
1175 read_lock_bh(&addrconf_hash_lock);
1176 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1177 if (ipv6_addr_equal(&ifp->addr, addr)) {
1178 if (dev == NULL || ifp->idev->dev == dev ||
1179 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1185 read_unlock_bh(&addrconf_hash_lock);
1190 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1192 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1193 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
1194 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1195 u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1196 int sk_ipv6only = ipv6_only_sock(sk);
1197 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1198 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1199 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1201 if (!sk2_rcv_saddr && !sk_ipv6only)
1204 if (addr_type2 == IPV6_ADDR_ANY &&
1205 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1208 if (addr_type == IPV6_ADDR_ANY &&
1209 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1212 if (sk2_rcv_saddr6 &&
1213 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1216 if (addr_type == IPV6_ADDR_MAPPED &&
1218 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1224 /* Gets referenced address, destroys ifaddr */
1226 static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1228 if (ifp->flags&IFA_F_PERMANENT) {
1229 spin_lock_bh(&ifp->lock);
1230 addrconf_del_timer(ifp);
1231 ifp->flags |= IFA_F_TENTATIVE;
1232 spin_unlock_bh(&ifp->lock);
1234 #ifdef CONFIG_IPV6_PRIVACY
1235 } else if (ifp->flags&IFA_F_TEMPORARY) {
1236 struct inet6_ifaddr *ifpub;
1237 spin_lock_bh(&ifp->lock);
1240 in6_ifa_hold(ifpub);
1241 spin_unlock_bh(&ifp->lock);
1242 ipv6_create_tempaddr(ifpub, ifp);
1245 spin_unlock_bh(&ifp->lock);
1253 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1255 if (net_ratelimit())
1256 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1257 addrconf_dad_stop(ifp);
1260 /* Join to solicited addr multicast group. */
1262 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1264 struct in6_addr maddr;
1266 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1269 addrconf_addr_solict_mult(addr, &maddr);
1270 ipv6_dev_mc_inc(dev, &maddr);
1273 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1275 struct in6_addr maddr;
1277 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1280 addrconf_addr_solict_mult(addr, &maddr);
1281 __ipv6_dev_mc_dec(idev, &maddr);
1284 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1286 struct in6_addr addr;
1287 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1288 if (ipv6_addr_any(&addr))
1290 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1293 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1295 struct in6_addr addr;
1296 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1297 if (ipv6_addr_any(&addr))
1299 __ipv6_dev_ac_dec(ifp->idev, &addr);
1302 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1304 switch (dev->type) {
1307 case ARPHRD_IEEE802_TR:
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;
1336 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1337 if (dev->addr_len != ARCNET_ALEN)
1340 eui[7] = *(u8*)dev->dev_addr;
1342 case ARPHRD_INFINIBAND:
1343 if (dev->addr_len != INFINIBAND_ALEN)
1345 memcpy(eui, dev->dev_addr + 12, 8);
1352 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1355 struct inet6_ifaddr *ifp;
1357 read_lock_bh(&idev->lock);
1358 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1359 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1360 memcpy(eui, ifp->addr.s6_addr+8, 8);
1365 read_unlock_bh(&idev->lock);
1369 #ifdef CONFIG_IPV6_PRIVACY
1370 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1371 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1374 get_random_bytes(idev->rndid, sizeof(idev->rndid));
1375 idev->rndid[0] &= ~0x02;
1378 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1379 * check if generated address is not inappropriate
1381 * - Reserved subnet anycast (RFC 2526)
1382 * 11111101 11....11 1xxxxxxx
1383 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1384 * 00-00-5E-FE-xx-xx-xx-xx
1386 * - XXX: already assigned to an address on the device
1388 if (idev->rndid[0] == 0xfd &&
1389 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1390 (idev->rndid[7]&0x80))
1392 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1393 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1395 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1402 static void ipv6_regen_rndid(unsigned long data)
1404 struct inet6_dev *idev = (struct inet6_dev *) data;
1405 unsigned long expires;
1407 read_lock_bh(&addrconf_lock);
1408 write_lock_bh(&idev->lock);
1413 if (__ipv6_regen_rndid(idev) < 0)
1417 idev->cnf.temp_prefered_lft * HZ -
1418 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1419 if (time_before(expires, jiffies)) {
1421 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1426 if (!mod_timer(&idev->regen_timer, expires))
1430 write_unlock_bh(&idev->lock);
1431 read_unlock_bh(&addrconf_lock);
1435 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1438 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1439 ret = __ipv6_regen_rndid(idev);
1449 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1450 unsigned long expires, u32 flags)
1452 struct in6_rtmsg rtmsg;
1454 memset(&rtmsg, 0, sizeof(rtmsg));
1455 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1456 rtmsg.rtmsg_dst_len = plen;
1457 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1458 rtmsg.rtmsg_ifindex = dev->ifindex;
1459 rtmsg.rtmsg_info = expires;
1460 rtmsg.rtmsg_flags = RTF_UP|flags;
1461 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1463 /* Prevent useless cloning on PtP SIT.
1464 This thing is done here expecting that the whole
1465 class of non-broadcast devices need not cloning.
1467 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1468 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1470 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1473 /* Create "default" multicast route to the interface */
1475 static void addrconf_add_mroute(struct net_device *dev)
1477 struct in6_rtmsg rtmsg;
1479 memset(&rtmsg, 0, sizeof(rtmsg));
1480 ipv6_addr_set(&rtmsg.rtmsg_dst,
1481 htonl(0xFF000000), 0, 0, 0);
1482 rtmsg.rtmsg_dst_len = 8;
1483 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1484 rtmsg.rtmsg_ifindex = dev->ifindex;
1485 rtmsg.rtmsg_flags = RTF_UP;
1486 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1487 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1490 static void sit_route_add(struct net_device *dev)
1492 struct in6_rtmsg rtmsg;
1494 memset(&rtmsg, 0, sizeof(rtmsg));
1496 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1497 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1499 /* prefix length - 96 bits "::d.d.d.d" */
1500 rtmsg.rtmsg_dst_len = 96;
1501 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1502 rtmsg.rtmsg_ifindex = dev->ifindex;
1504 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1507 static void addrconf_add_lroute(struct net_device *dev)
1509 struct in6_addr addr;
1511 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1512 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1515 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1517 struct inet6_dev *idev;
1521 if ((idev = ipv6_find_idev(dev)) == NULL)
1524 /* Add default multicast route */
1525 addrconf_add_mroute(dev);
1527 /* Add link local route */
1528 addrconf_add_lroute(dev);
1532 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1534 struct prefix_info *pinfo;
1538 unsigned long rt_expires;
1539 struct inet6_dev *in6_dev;
1541 pinfo = (struct prefix_info *) opt;
1543 if (len < sizeof(struct prefix_info)) {
1544 ADBG(("addrconf: prefix option too short\n"));
1549 * Validation checks ([ADDRCONF], page 19)
1552 addr_type = ipv6_addr_type(&pinfo->prefix);
1554 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1557 valid_lft = ntohl(pinfo->valid);
1558 prefered_lft = ntohl(pinfo->prefered);
1560 if (prefered_lft > valid_lft) {
1561 if (net_ratelimit())
1562 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1566 in6_dev = in6_dev_get(dev);
1568 if (in6_dev == NULL) {
1569 if (net_ratelimit())
1570 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1575 * Two things going on here:
1576 * 1) Add routes for on-link prefixes
1577 * 2) Configure prefixes with the auto flag set
1580 /* Avoid arithmetic overflow. Really, we could
1581 save rt_expires in seconds, likely valid_lft,
1582 but it would require division in fib gc, that it
1585 if (valid_lft >= 0x7FFFFFFF/HZ)
1586 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1588 rt_expires = valid_lft * HZ;
1591 * We convert this (in jiffies) to clock_t later.
1592 * Avoid arithmetic overflow there as well.
1593 * Overflow can happen only if HZ < USER_HZ.
1595 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1596 rt_expires = 0x7FFFFFFF / USER_HZ;
1598 if (pinfo->onlink) {
1599 struct rt6_info *rt;
1600 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1602 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1603 if (rt->rt6i_flags&RTF_EXPIRES) {
1604 if (valid_lft == 0) {
1605 ip6_del_rt(rt, NULL, NULL, NULL);
1608 rt->rt6i_expires = jiffies + rt_expires;
1611 } else if (valid_lft) {
1612 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1613 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1616 dst_release(&rt->u.dst);
1619 /* Try to figure out our local address for this prefix */
1621 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1622 struct inet6_ifaddr * ifp;
1623 struct in6_addr addr;
1624 int create = 0, update_lft = 0;
1626 if (pinfo->prefix_len == 64) {
1627 memcpy(&addr, &pinfo->prefix, 8);
1628 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1629 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1630 in6_dev_put(in6_dev);
1635 if (net_ratelimit())
1636 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1638 in6_dev_put(in6_dev);
1643 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1645 if (ifp == NULL && valid_lft) {
1646 int max_addresses = in6_dev->cnf.max_addresses;
1648 /* Do not allow to create too much of autoconfigured
1649 * addresses; this would be too easy way to crash kernel.
1651 if (!max_addresses ||
1652 ipv6_count_addresses(in6_dev) < max_addresses)
1653 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1654 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1656 if (!ifp || IS_ERR(ifp)) {
1657 in6_dev_put(in6_dev);
1661 update_lft = create = 1;
1662 ifp->cstamp = jiffies;
1663 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1669 #ifdef CONFIG_IPV6_PRIVACY
1670 struct inet6_ifaddr *ift;
1674 /* update lifetime (RFC2462 5.5.3 e) */
1675 spin_lock(&ifp->lock);
1677 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1678 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1681 if (!update_lft && stored_lft) {
1682 if (valid_lft > MIN_VALID_LIFETIME ||
1683 valid_lft > stored_lft)
1685 else if (stored_lft <= MIN_VALID_LIFETIME) {
1686 /* valid_lft <= stored_lft is always true */
1690 valid_lft = MIN_VALID_LIFETIME;
1691 if (valid_lft < prefered_lft)
1692 prefered_lft = valid_lft;
1698 ifp->valid_lft = valid_lft;
1699 ifp->prefered_lft = prefered_lft;
1702 ifp->flags &= ~IFA_F_DEPRECATED;
1703 spin_unlock(&ifp->lock);
1705 if (!(flags&IFA_F_TENTATIVE))
1706 ipv6_ifa_notify(0, ifp);
1708 spin_unlock(&ifp->lock);
1710 #ifdef CONFIG_IPV6_PRIVACY
1711 read_lock_bh(&in6_dev->lock);
1712 /* update all temporary addresses in the list */
1713 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1715 * When adjusting the lifetimes of an existing
1716 * temporary address, only lower the lifetimes.
1717 * Implementations must not increase the
1718 * lifetimes of an existing temporary address
1719 * when processing a Prefix Information Option.
1721 spin_lock(&ift->lock);
1723 if (ift->valid_lft > valid_lft &&
1724 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1725 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1726 if (ift->prefered_lft > prefered_lft &&
1727 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1728 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1729 spin_unlock(&ift->lock);
1730 if (!(flags&IFA_F_TENTATIVE))
1731 ipv6_ifa_notify(0, ift);
1734 if (create && in6_dev->cnf.use_tempaddr > 0) {
1736 * When a new public address is created as described in [ADDRCONF],
1737 * also create a new temporary address.
1739 read_unlock_bh(&in6_dev->lock);
1740 ipv6_create_tempaddr(ifp, NULL);
1742 read_unlock_bh(&in6_dev->lock);
1749 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1750 in6_dev_put(in6_dev);
1754 * Set destination address.
1755 * Special case for SIT interfaces where we create a new "virtual"
1758 int addrconf_set_dstaddr(void __user *arg)
1760 struct in6_ifreq ireq;
1761 struct net_device *dev;
1767 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1770 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1776 if (dev->type == ARPHRD_SIT) {
1779 struct ip_tunnel_parm p;
1781 err = -EADDRNOTAVAIL;
1782 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1785 memset(&p, 0, sizeof(p));
1786 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1790 p.iph.protocol = IPPROTO_IPV6;
1792 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1794 oldfs = get_fs(); set_fs(KERNEL_DS);
1795 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1800 if ((dev = __dev_get_by_name(p.name)) == NULL)
1802 err = dev_open(dev);
1812 * Manual configuration of address on an interface
1814 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1816 struct inet6_ifaddr *ifp;
1817 struct inet6_dev *idev;
1818 struct net_device *dev;
1823 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1826 if (!(dev->flags&IFF_UP))
1829 if ((idev = addrconf_add_dev(dev)) == NULL)
1832 scope = ipv6_addr_scope(pfx);
1834 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1836 addrconf_dad_start(ifp, 0);
1841 return PTR_ERR(ifp);
1844 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1846 struct inet6_ifaddr *ifp;
1847 struct inet6_dev *idev;
1848 struct net_device *dev;
1850 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1853 if ((idev = __in6_dev_get(dev)) == NULL)
1856 read_lock_bh(&idev->lock);
1857 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1858 if (ifp->prefix_len == plen &&
1859 ipv6_addr_equal(pfx, &ifp->addr)) {
1861 read_unlock_bh(&idev->lock);
1865 /* If the last address is deleted administratively,
1866 disable IPv6 on this interface.
1868 if (idev->addr_list == NULL)
1869 addrconf_ifdown(idev->dev, 1);
1873 read_unlock_bh(&idev->lock);
1874 return -EADDRNOTAVAIL;
1878 int addrconf_add_ifaddr(void __user *arg)
1880 struct in6_ifreq ireq;
1883 if (!capable(CAP_NET_ADMIN))
1886 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1890 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1895 int addrconf_del_ifaddr(void __user *arg)
1897 struct in6_ifreq ireq;
1900 if (!capable(CAP_NET_ADMIN))
1903 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1907 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1912 static void sit_add_v4_addrs(struct inet6_dev *idev)
1914 struct inet6_ifaddr * ifp;
1915 struct in6_addr addr;
1916 struct net_device *dev;
1921 memset(&addr, 0, sizeof(struct in6_addr));
1922 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1924 if (idev->dev->flags&IFF_POINTOPOINT) {
1925 addr.s6_addr32[0] = htonl(0xfe800000);
1928 scope = IPV6_ADDR_COMPATv4;
1931 if (addr.s6_addr32[3]) {
1932 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1934 spin_lock_bh(&ifp->lock);
1935 ifp->flags &= ~IFA_F_TENTATIVE;
1936 spin_unlock_bh(&ifp->lock);
1937 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1943 for (dev = dev_base; dev != NULL; dev = dev->next) {
1944 struct in_device * in_dev = __in_dev_get_rtnl(dev);
1945 if (in_dev && (dev->flags & IFF_UP)) {
1946 struct in_ifaddr * ifa;
1950 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1953 addr.s6_addr32[3] = ifa->ifa_local;
1955 if (ifa->ifa_scope == RT_SCOPE_LINK)
1957 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1958 if (idev->dev->flags&IFF_POINTOPOINT)
1962 if (idev->dev->flags&IFF_POINTOPOINT)
1967 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1970 spin_lock_bh(&ifp->lock);
1971 ifp->flags &= ~IFA_F_TENTATIVE;
1972 spin_unlock_bh(&ifp->lock);
1973 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1981 static void init_loopback(struct net_device *dev)
1983 struct inet6_dev *idev;
1984 struct inet6_ifaddr * ifp;
1990 if ((idev = ipv6_find_idev(dev)) == NULL) {
1991 printk(KERN_DEBUG "init loopback: add_dev failed\n");
1995 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
1997 spin_lock_bh(&ifp->lock);
1998 ifp->flags &= ~IFA_F_TENTATIVE;
1999 spin_unlock_bh(&ifp->lock);
2000 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2005 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2007 struct inet6_ifaddr * ifp;
2009 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2011 addrconf_dad_start(ifp, 0);
2016 static void addrconf_dev_config(struct net_device *dev)
2018 struct in6_addr addr;
2019 struct inet6_dev * idev;
2023 if ((dev->type != ARPHRD_ETHER) &&
2024 (dev->type != ARPHRD_FDDI) &&
2025 (dev->type != ARPHRD_IEEE802_TR) &&
2026 (dev->type != ARPHRD_ARCNET) &&
2027 (dev->type != ARPHRD_INFINIBAND)) {
2028 /* Alas, we support only Ethernet autoconfiguration. */
2032 idev = addrconf_add_dev(dev);
2036 memset(&addr, 0, sizeof(struct in6_addr));
2037 addr.s6_addr32[0] = htonl(0xFE800000);
2039 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2040 addrconf_add_linklocal(idev, &addr);
2043 static void addrconf_sit_config(struct net_device *dev)
2045 struct inet6_dev *idev;
2050 * Configure the tunnel with one of our IPv4
2051 * addresses... we should configure all of
2052 * our v4 addrs in the tunnel
2055 if ((idev = ipv6_find_idev(dev)) == NULL) {
2056 printk(KERN_DEBUG "init sit: add_dev failed\n");
2060 sit_add_v4_addrs(idev);
2062 if (dev->flags&IFF_POINTOPOINT) {
2063 addrconf_add_mroute(dev);
2064 addrconf_add_lroute(dev);
2070 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2072 struct in6_addr lladdr;
2074 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2075 addrconf_add_linklocal(idev, &lladdr);
2081 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2083 struct net_device *link_dev;
2085 /* first try to inherit the link-local address from the link device */
2086 if (idev->dev->iflink &&
2087 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2088 if (!ipv6_inherit_linklocal(idev, link_dev))
2091 /* then try to inherit it from any device */
2092 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2093 if (!ipv6_inherit_linklocal(idev, link_dev))
2096 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2100 * Autoconfigure tunnel with a link-local address so routing protocols,
2101 * DHCPv6, MLD etc. can be run over the virtual link
2104 static void addrconf_ip6_tnl_config(struct net_device *dev)
2106 struct inet6_dev *idev;
2110 if ((idev = addrconf_add_dev(dev)) == NULL) {
2111 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2114 ip6_tnl_add_linklocal(idev);
2117 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2120 struct net_device *dev = (struct net_device *) data;
2121 struct inet6_dev *idev = __in6_dev_get(dev);
2122 int run_pending = 0;
2127 if (event == NETDEV_UP) {
2128 if (!netif_carrier_ok(dev)) {
2129 /* device is not ready yet. */
2131 "ADDRCONF(NETDEV_UP): %s: "
2132 "link is not ready\n",
2138 idev->if_flags |= IF_READY;
2140 if (!netif_carrier_ok(dev)) {
2141 /* device is still not ready. */
2146 if (idev->if_flags & IF_READY) {
2147 /* device is already configured. */
2150 idev->if_flags |= IF_READY;
2154 "ADDRCONF(NETDEV_CHANGE): %s: "
2155 "link becomes ready\n",
2163 addrconf_sit_config(dev);
2165 case ARPHRD_TUNNEL6:
2166 addrconf_ip6_tnl_config(dev);
2168 case ARPHRD_LOOPBACK:
2173 addrconf_dev_config(dev);
2178 addrconf_dad_run(idev);
2180 /* If the MTU changed during the interface down, when the
2181 interface up, the changed MTU must be reflected in the
2182 idev as well as routers.
2184 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2185 rt6_mtu_change(dev, dev->mtu);
2186 idev->cnf.mtu6 = dev->mtu;
2188 idev->tstamp = jiffies;
2189 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2190 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2191 stop IPv6 on this interface.
2193 if (dev->mtu < IPV6_MIN_MTU)
2194 addrconf_ifdown(dev, event != NETDEV_DOWN);
2198 case NETDEV_CHANGEMTU:
2199 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2200 rt6_mtu_change(dev, dev->mtu);
2201 idev->cnf.mtu6 = dev->mtu;
2205 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2208 case NETDEV_UNREGISTER:
2210 * Remove all addresses from this interface.
2212 addrconf_ifdown(dev, event != NETDEV_DOWN);
2215 case NETDEV_CHANGENAME:
2216 #ifdef CONFIG_SYSCTL
2218 addrconf_sysctl_unregister(&idev->cnf);
2219 neigh_sysctl_unregister(idev->nd_parms);
2220 neigh_sysctl_register(dev, idev->nd_parms,
2221 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2222 &ndisc_ifinfo_sysctl_change,
2224 addrconf_sysctl_register(idev, &idev->cnf);
2234 * addrconf module should be notified of a device going up
2236 static struct notifier_block ipv6_dev_notf = {
2237 .notifier_call = addrconf_notify,
2241 static int addrconf_ifdown(struct net_device *dev, int how)
2243 struct inet6_dev *idev;
2244 struct inet6_ifaddr *ifa, **bifa;
2249 if (dev == &loopback_dev && how == 1)
2253 neigh_ifdown(&nd_tbl, dev);
2255 idev = __in6_dev_get(dev);
2259 /* Step 1: remove reference to ipv6 device from parent device.
2263 write_lock_bh(&addrconf_lock);
2264 dev->ip6_ptr = NULL;
2266 write_unlock_bh(&addrconf_lock);
2268 /* Step 1.5: remove snmp6 entry */
2269 snmp6_unregister_dev(idev);
2273 /* Step 2: clear hash table */
2274 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2275 bifa = &inet6_addr_lst[i];
2277 write_lock_bh(&addrconf_hash_lock);
2278 while ((ifa = *bifa) != NULL) {
2279 if (ifa->idev == idev) {
2280 *bifa = ifa->lst_next;
2281 ifa->lst_next = NULL;
2282 addrconf_del_timer(ifa);
2286 bifa = &ifa->lst_next;
2288 write_unlock_bh(&addrconf_hash_lock);
2291 write_lock_bh(&idev->lock);
2293 /* Step 3: clear flags for stateless addrconf */
2295 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2297 /* Step 4: clear address list */
2298 #ifdef CONFIG_IPV6_PRIVACY
2299 if (how == 1 && del_timer(&idev->regen_timer))
2302 /* clear tempaddr list */
2303 while ((ifa = idev->tempaddr_list) != NULL) {
2304 idev->tempaddr_list = ifa->tmp_next;
2305 ifa->tmp_next = NULL;
2307 write_unlock_bh(&idev->lock);
2308 spin_lock_bh(&ifa->lock);
2311 in6_ifa_put(ifa->ifpub);
2314 spin_unlock_bh(&ifa->lock);
2316 write_lock_bh(&idev->lock);
2319 while ((ifa = idev->addr_list) != NULL) {
2320 idev->addr_list = ifa->if_next;
2321 ifa->if_next = NULL;
2323 addrconf_del_timer(ifa);
2324 write_unlock_bh(&idev->lock);
2326 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2329 write_lock_bh(&idev->lock);
2331 write_unlock_bh(&idev->lock);
2333 /* Step 5: Discard multicast list */
2336 ipv6_mc_destroy_dev(idev);
2340 /* Step 5: netlink notification of this interface */
2341 idev->tstamp = jiffies;
2342 inet6_ifinfo_notify(RTM_DELLINK, idev);
2344 /* Shot the device (if unregistered) */
2347 #ifdef CONFIG_SYSCTL
2348 addrconf_sysctl_unregister(&idev->cnf);
2349 neigh_sysctl_unregister(idev->nd_parms);
2351 neigh_parms_release(&nd_tbl, idev->nd_parms);
2352 neigh_ifdown(&nd_tbl, dev);
2358 static void addrconf_rs_timer(unsigned long data)
2360 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2362 if (ifp->idev->cnf.forwarding)
2365 if (ifp->idev->if_flags & IF_RA_RCVD) {
2367 * Announcement received after solicitation
2373 spin_lock(&ifp->lock);
2374 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2375 struct in6_addr all_routers;
2377 /* The wait after the last probe can be shorter */
2378 addrconf_mod_timer(ifp, AC_RS,
2379 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2380 ifp->idev->cnf.rtr_solicit_delay :
2381 ifp->idev->cnf.rtr_solicit_interval);
2382 spin_unlock(&ifp->lock);
2384 ipv6_addr_all_routers(&all_routers);
2386 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2388 spin_unlock(&ifp->lock);
2390 * Note: we do not support deprecated "all on-link"
2391 * assumption any longer.
2393 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2394 ifp->idev->dev->name);
2402 * Duplicate Address Detection
2404 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2406 unsigned long rand_num;
2407 struct inet6_dev *idev = ifp->idev;
2409 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2410 ifp->probes = idev->cnf.dad_transmits;
2411 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2414 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2416 struct inet6_dev *idev = ifp->idev;
2417 struct net_device *dev = idev->dev;
2419 addrconf_join_solict(dev, &ifp->addr);
2421 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2422 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2425 net_srandom(ifp->addr.s6_addr32[3]);
2427 read_lock_bh(&idev->lock);
2430 spin_lock_bh(&ifp->lock);
2432 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2433 !(ifp->flags&IFA_F_TENTATIVE)) {
2434 ifp->flags &= ~IFA_F_TENTATIVE;
2435 spin_unlock_bh(&ifp->lock);
2436 read_unlock_bh(&idev->lock);
2438 addrconf_dad_completed(ifp);
2442 if (!(idev->if_flags & IF_READY)) {
2443 spin_unlock_bh(&ifp->lock);
2444 read_unlock_bh(&idev->lock);
2446 * If the defice is not ready:
2447 * - keep it tentative if it is a permanent address.
2448 * - otherwise, kill it.
2451 addrconf_dad_stop(ifp);
2454 addrconf_dad_kick(ifp);
2455 spin_unlock_bh(&ifp->lock);
2457 read_unlock_bh(&idev->lock);
2460 static void addrconf_dad_timer(unsigned long data)
2462 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2463 struct inet6_dev *idev = ifp->idev;
2464 struct in6_addr unspec;
2465 struct in6_addr mcaddr;
2467 read_lock_bh(&idev->lock);
2469 read_unlock_bh(&idev->lock);
2472 spin_lock_bh(&ifp->lock);
2473 if (ifp->probes == 0) {
2475 * DAD was successful
2478 ifp->flags &= ~IFA_F_TENTATIVE;
2479 spin_unlock_bh(&ifp->lock);
2480 read_unlock_bh(&idev->lock);
2482 addrconf_dad_completed(ifp);
2488 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2489 spin_unlock_bh(&ifp->lock);
2490 read_unlock_bh(&idev->lock);
2492 /* send a neighbour solicitation for our addr */
2493 memset(&unspec, 0, sizeof(unspec));
2494 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2495 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2500 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2502 struct net_device * dev = ifp->idev->dev;
2505 * Configure the address for reception. Now it is valid.
2508 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2510 /* If added prefix is link local and forwarding is off,
2511 start sending router solicitations.
2514 if (ifp->idev->cnf.forwarding == 0 &&
2515 ifp->idev->cnf.rtr_solicits > 0 &&
2516 (dev->flags&IFF_LOOPBACK) == 0 &&
2517 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2518 struct in6_addr all_routers;
2520 ipv6_addr_all_routers(&all_routers);
2523 * If a host as already performed a random delay
2524 * [...] as part of DAD [...] there is no need
2525 * to delay again before sending the first RS
2527 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2529 spin_lock_bh(&ifp->lock);
2531 ifp->idev->if_flags |= IF_RS_SENT;
2532 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2533 spin_unlock_bh(&ifp->lock);
2537 static void addrconf_dad_run(struct inet6_dev *idev) {
2538 struct inet6_ifaddr *ifp;
2540 read_lock_bh(&idev->lock);
2541 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2542 spin_lock_bh(&ifp->lock);
2543 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2544 spin_unlock_bh(&ifp->lock);
2547 spin_unlock_bh(&ifp->lock);
2548 addrconf_dad_kick(ifp);
2550 read_unlock_bh(&idev->lock);
2553 #ifdef CONFIG_PROC_FS
2554 struct if6_iter_state {
2558 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2560 struct inet6_ifaddr *ifa = NULL;
2561 struct if6_iter_state *state = seq->private;
2563 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2564 ifa = inet6_addr_lst[state->bucket];
2571 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2573 struct if6_iter_state *state = seq->private;
2575 ifa = ifa->lst_next;
2577 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2578 ifa = inet6_addr_lst[state->bucket];
2584 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2586 struct inet6_ifaddr *ifa = if6_get_first(seq);
2589 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2591 return pos ? NULL : ifa;
2594 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2596 read_lock_bh(&addrconf_hash_lock);
2597 return if6_get_idx(seq, *pos);
2600 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2602 struct inet6_ifaddr *ifa;
2604 ifa = if6_get_next(seq, v);
2609 static void if6_seq_stop(struct seq_file *seq, void *v)
2611 read_unlock_bh(&addrconf_hash_lock);
2614 static int if6_seq_show(struct seq_file *seq, void *v)
2616 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2618 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
2620 ifp->idev->dev->ifindex,
2624 ifp->idev->dev->name);
2628 static struct seq_operations if6_seq_ops = {
2629 .start = if6_seq_start,
2630 .next = if6_seq_next,
2631 .show = if6_seq_show,
2632 .stop = if6_seq_stop,
2635 static int if6_seq_open(struct inode *inode, struct file *file)
2637 struct seq_file *seq;
2639 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2643 memset(s, 0, sizeof(*s));
2645 rc = seq_open(file, &if6_seq_ops);
2649 seq = file->private_data;
2658 static struct file_operations if6_fops = {
2659 .owner = THIS_MODULE,
2660 .open = if6_seq_open,
2662 .llseek = seq_lseek,
2663 .release = seq_release_private,
2666 int __init if6_proc_init(void)
2668 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2673 void if6_proc_exit(void)
2675 proc_net_remove("if_inet6");
2677 #endif /* CONFIG_PROC_FS */
2680 * Periodic address status verification
2683 static void addrconf_verify(unsigned long foo)
2685 struct inet6_ifaddr *ifp;
2686 unsigned long now, next;
2689 spin_lock_bh(&addrconf_verify_lock);
2691 next = now + ADDR_CHECK_FREQUENCY;
2693 del_timer(&addr_chk_timer);
2695 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2698 read_lock(&addrconf_hash_lock);
2699 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2701 #ifdef CONFIG_IPV6_PRIVACY
2702 unsigned long regen_advance;
2705 if (ifp->flags & IFA_F_PERMANENT)
2708 spin_lock(&ifp->lock);
2709 age = (now - ifp->tstamp) / HZ;
2711 #ifdef CONFIG_IPV6_PRIVACY
2712 regen_advance = ifp->idev->cnf.regen_max_retry *
2713 ifp->idev->cnf.dad_transmits *
2714 ifp->idev->nd_parms->retrans_time / HZ;
2717 if (age >= ifp->valid_lft) {
2718 spin_unlock(&ifp->lock);
2720 read_unlock(&addrconf_hash_lock);
2723 } else if (age >= ifp->prefered_lft) {
2724 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2727 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2729 ifp->flags |= IFA_F_DEPRECATED;
2732 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2733 next = ifp->tstamp + ifp->valid_lft * HZ;
2735 spin_unlock(&ifp->lock);
2739 read_unlock(&addrconf_hash_lock);
2741 ipv6_ifa_notify(0, ifp);
2745 #ifdef CONFIG_IPV6_PRIVACY
2746 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2747 !(ifp->flags&IFA_F_TENTATIVE)) {
2748 if (age >= ifp->prefered_lft - regen_advance) {
2749 struct inet6_ifaddr *ifpub = ifp->ifpub;
2750 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2751 next = ifp->tstamp + ifp->prefered_lft * HZ;
2752 if (!ifp->regen_count && ifpub) {
2755 in6_ifa_hold(ifpub);
2756 spin_unlock(&ifp->lock);
2757 read_unlock(&addrconf_hash_lock);
2758 spin_lock(&ifpub->lock);
2759 ifpub->regen_count = 0;
2760 spin_unlock(&ifpub->lock);
2761 ipv6_create_tempaddr(ifpub, ifp);
2766 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2767 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2768 spin_unlock(&ifp->lock);
2771 /* ifp->prefered_lft <= ifp->valid_lft */
2772 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2773 next = ifp->tstamp + ifp->prefered_lft * HZ;
2774 spin_unlock(&ifp->lock);
2777 read_unlock(&addrconf_hash_lock);
2780 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2781 add_timer(&addr_chk_timer);
2782 spin_unlock_bh(&addrconf_verify_lock);
2786 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2788 struct rtattr **rta = arg;
2789 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2790 struct in6_addr *pfx;
2793 if (rta[IFA_ADDRESS-1]) {
2794 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2796 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2798 if (rta[IFA_LOCAL-1]) {
2799 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2801 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2806 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2810 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2812 struct rtattr **rta = arg;
2813 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2814 struct in6_addr *pfx;
2817 if (rta[IFA_ADDRESS-1]) {
2818 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2820 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2822 if (rta[IFA_LOCAL-1]) {
2823 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2825 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2830 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2833 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2834 u32 pid, u32 seq, int event, unsigned int flags)
2836 struct ifaddrmsg *ifm;
2837 struct nlmsghdr *nlh;
2838 struct ifa_cacheinfo ci;
2839 unsigned char *b = skb->tail;
2841 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2842 ifm = NLMSG_DATA(nlh);
2843 ifm->ifa_family = AF_INET6;
2844 ifm->ifa_prefixlen = ifa->prefix_len;
2845 ifm->ifa_flags = ifa->flags;
2846 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2847 if (ifa->scope&IFA_HOST)
2848 ifm->ifa_scope = RT_SCOPE_HOST;
2849 else if (ifa->scope&IFA_LINK)
2850 ifm->ifa_scope = RT_SCOPE_LINK;
2851 else if (ifa->scope&IFA_SITE)
2852 ifm->ifa_scope = RT_SCOPE_SITE;
2853 ifm->ifa_index = ifa->idev->dev->ifindex;
2854 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2855 if (!(ifa->flags&IFA_F_PERMANENT)) {
2856 ci.ifa_prefered = ifa->prefered_lft;
2857 ci.ifa_valid = ifa->valid_lft;
2858 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2859 long tval = (jiffies - ifa->tstamp)/HZ;
2860 ci.ifa_prefered -= tval;
2861 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2862 ci.ifa_valid -= tval;
2865 ci.ifa_prefered = INFINITY_LIFE_TIME;
2866 ci.ifa_valid = INFINITY_LIFE_TIME;
2868 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2869 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2870 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2871 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2872 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2873 nlh->nlmsg_len = skb->tail - b;
2878 skb_trim(skb, b - skb->data);
2882 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2883 u32 pid, u32 seq, int event, u16 flags)
2885 struct ifaddrmsg *ifm;
2886 struct nlmsghdr *nlh;
2887 struct ifa_cacheinfo ci;
2888 unsigned char *b = skb->tail;
2890 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2891 ifm = NLMSG_DATA(nlh);
2892 ifm->ifa_family = AF_INET6;
2893 ifm->ifa_prefixlen = 128;
2894 ifm->ifa_flags = IFA_F_PERMANENT;
2895 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2896 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2897 ifm->ifa_scope = RT_SCOPE_SITE;
2898 ifm->ifa_index = ifmca->idev->dev->ifindex;
2899 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2900 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2901 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2903 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2904 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2906 ci.ifa_prefered = INFINITY_LIFE_TIME;
2907 ci.ifa_valid = INFINITY_LIFE_TIME;
2908 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2909 nlh->nlmsg_len = skb->tail - b;
2914 skb_trim(skb, b - skb->data);
2918 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2919 u32 pid, u32 seq, int event, unsigned int flags)
2921 struct ifaddrmsg *ifm;
2922 struct nlmsghdr *nlh;
2923 struct ifa_cacheinfo ci;
2924 unsigned char *b = skb->tail;
2926 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2927 ifm = NLMSG_DATA(nlh);
2928 ifm->ifa_family = AF_INET6;
2929 ifm->ifa_prefixlen = 128;
2930 ifm->ifa_flags = IFA_F_PERMANENT;
2931 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2932 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2933 ifm->ifa_scope = RT_SCOPE_SITE;
2934 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2935 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2936 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2937 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2939 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2940 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2942 ci.ifa_prefered = INFINITY_LIFE_TIME;
2943 ci.ifa_valid = INFINITY_LIFE_TIME;
2944 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2945 nlh->nlmsg_len = skb->tail - b;
2950 skb_trim(skb, b - skb->data);
2961 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2962 enum addr_type_t type)
2965 int s_idx, s_ip_idx;
2967 struct net_device *dev;
2968 struct inet6_dev *idev = NULL;
2969 struct inet6_ifaddr *ifa;
2970 struct ifmcaddr6 *ifmca;
2971 struct ifacaddr6 *ifaca;
2973 s_idx = cb->args[0];
2974 s_ip_idx = ip_idx = cb->args[1];
2975 read_lock(&dev_base_lock);
2977 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
2983 if ((idev = in6_dev_get(dev)) == NULL)
2985 read_lock_bh(&idev->lock);
2988 /* unicast address incl. temp addr */
2989 for (ifa = idev->addr_list; ifa;
2990 ifa = ifa->if_next, ip_idx++) {
2991 if (ip_idx < s_ip_idx)
2993 if ((err = inet6_fill_ifaddr(skb, ifa,
2994 NETLINK_CB(cb->skb).pid,
2995 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3000 case MULTICAST_ADDR:
3001 /* multicast address */
3002 for (ifmca = idev->mc_list; ifmca;
3003 ifmca = ifmca->next, ip_idx++) {
3004 if (ip_idx < s_ip_idx)
3006 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3007 NETLINK_CB(cb->skb).pid,
3008 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3014 /* anycast address */
3015 for (ifaca = idev->ac_list; ifaca;
3016 ifaca = ifaca->aca_next, ip_idx++) {
3017 if (ip_idx < s_ip_idx)
3019 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3020 NETLINK_CB(cb->skb).pid,
3021 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3029 read_unlock_bh(&idev->lock);
3034 read_unlock_bh(&idev->lock);
3037 read_unlock(&dev_base_lock);
3039 cb->args[1] = ip_idx;
3043 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3045 enum addr_type_t type = UNICAST_ADDR;
3046 return inet6_dump_addr(skb, cb, type);
3049 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3051 enum addr_type_t type = MULTICAST_ADDR;
3052 return inet6_dump_addr(skb, cb, type);
3056 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3058 enum addr_type_t type = ANYCAST_ADDR;
3059 return inet6_dump_addr(skb, cb, type);
3062 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3064 struct sk_buff *skb;
3065 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
3067 skb = alloc_skb(size, GFP_ATOMIC);
3069 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
3072 if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
3074 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
3077 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
3078 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
3081 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3082 __s32 *array, int bytes)
3084 memset(array, 0, bytes);
3085 array[DEVCONF_FORWARDING] = cnf->forwarding;
3086 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3087 array[DEVCONF_MTU6] = cnf->mtu6;
3088 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3089 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3090 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3091 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3092 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3093 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3094 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3095 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3096 #ifdef CONFIG_IPV6_PRIVACY
3097 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3098 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3099 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3100 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3101 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3103 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3106 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3107 u32 pid, u32 seq, int event, unsigned int flags)
3109 struct net_device *dev = idev->dev;
3110 __s32 *array = NULL;
3111 struct ifinfomsg *r;
3112 struct nlmsghdr *nlh;
3113 unsigned char *b = skb->tail;
3114 struct rtattr *subattr;
3115 __u32 mtu = dev->mtu;
3116 struct ifla_cacheinfo ci;
3118 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
3119 r = NLMSG_DATA(nlh);
3120 r->ifi_family = AF_INET6;
3122 r->ifi_type = dev->type;
3123 r->ifi_index = dev->ifindex;
3124 r->ifi_flags = dev_get_flags(dev);
3127 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
3130 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3132 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
3133 if (dev->ifindex != dev->iflink)
3134 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
3136 subattr = (struct rtattr*)skb->tail;
3138 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
3140 /* return the device flags */
3141 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
3143 /* return interface cacheinfo */
3144 ci.max_reasm_len = IPV6_MAXPLEN;
3145 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3146 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3147 ci.reachable_time = idev->nd_parms->reachable_time;
3148 ci.retrans_time = idev->nd_parms->retrans_time;
3149 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3151 /* return the device sysctl params */
3152 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
3153 goto rtattr_failure;
3154 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
3155 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
3157 /* XXX - Statistics/MC not implemented */
3158 subattr->rta_len = skb->tail - (u8*)subattr;
3160 nlh->nlmsg_len = skb->tail - b;
3167 skb_trim(skb, b - skb->data);
3171 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3174 int s_idx = cb->args[0];
3175 struct net_device *dev;
3176 struct inet6_dev *idev;
3178 read_lock(&dev_base_lock);
3179 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3182 if ((idev = in6_dev_get(dev)) == NULL)
3184 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3185 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3190 read_unlock(&dev_base_lock);
3196 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3198 struct sk_buff *skb;
3200 int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
3202 skb = alloc_skb(size, GFP_ATOMIC);
3204 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
3207 if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
3209 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
3212 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
3213 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
3216 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3217 struct prefix_info *pinfo, u32 pid, u32 seq,
3218 int event, unsigned int flags)
3220 struct prefixmsg *pmsg;
3221 struct nlmsghdr *nlh;
3222 unsigned char *b = skb->tail;
3223 struct prefix_cacheinfo ci;
3225 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
3226 pmsg = NLMSG_DATA(nlh);
3227 pmsg->prefix_family = AF_INET6;
3228 pmsg->prefix_pad1 = 0;
3229 pmsg->prefix_pad2 = 0;
3230 pmsg->prefix_ifindex = idev->dev->ifindex;
3231 pmsg->prefix_len = pinfo->prefix_len;
3232 pmsg->prefix_type = pinfo->type;
3233 pmsg->prefix_pad3 = 0;
3235 pmsg->prefix_flags = 0;
3237 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3238 if (pinfo->autoconf)
3239 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3241 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3243 ci.preferred_time = ntohl(pinfo->prefered);
3244 ci.valid_time = ntohl(pinfo->valid);
3245 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3247 nlh->nlmsg_len = skb->tail - b;
3252 skb_trim(skb, b - skb->data);
3256 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3257 struct prefix_info *pinfo)
3259 struct sk_buff *skb;
3260 int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
3262 skb = alloc_skb(size, GFP_ATOMIC);
3264 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
3267 if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
3269 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
3272 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
3273 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
3276 static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
3277 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3278 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3279 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
3280 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
3281 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3282 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3283 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3284 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3285 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3286 .dumpit = inet6_dump_fib, },
3289 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3291 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3295 ip6_ins_rt(ifp->rt, NULL, NULL, NULL);
3296 if (ifp->idev->cnf.forwarding)
3297 addrconf_join_anycast(ifp);
3300 if (ifp->idev->cnf.forwarding)
3301 addrconf_leave_anycast(ifp);
3302 addrconf_leave_solict(ifp->idev, &ifp->addr);
3303 dst_hold(&ifp->rt->u.dst);
3304 if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
3305 dst_free(&ifp->rt->u.dst);
3310 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3312 read_lock_bh(&addrconf_lock);
3313 if (likely(ifp->idev->dead == 0))
3314 __ipv6_ifa_notify(event, ifp);
3315 read_unlock_bh(&addrconf_lock);
3318 #ifdef CONFIG_SYSCTL
3321 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3322 void __user *buffer, size_t *lenp, loff_t *ppos)
3324 int *valp = ctl->data;
3328 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3330 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3331 if (valp != &ipv6_devconf.forwarding) {
3332 if ((!*valp) ^ (!val)) {
3333 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3336 dev_forward_change(idev);
3339 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3340 addrconf_forward_change();
3343 rt6_purge_dflt_routers();
3349 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3350 int __user *name, int nlen,
3351 void __user *oldval,
3352 size_t __user *oldlenp,
3353 void __user *newval, size_t newlen,
3356 int *valp = table->data;
3359 if (!newval || !newlen)
3361 if (newlen != sizeof(int))
3363 if (get_user(new, (int __user *)newval))
3367 if (oldval && oldlenp) {
3369 if (get_user(len, oldlenp))
3372 if (len > table->maxlen)
3373 len = table->maxlen;
3374 if (copy_to_user(oldval, valp, len))
3376 if (put_user(len, oldlenp))
3381 if (valp != &ipv6_devconf_dflt.forwarding) {
3382 if (valp != &ipv6_devconf.forwarding) {
3383 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3385 if (unlikely(idev == NULL))
3387 changed = (!*valp) ^ (!new);
3390 dev_forward_change(idev);
3393 addrconf_forward_change();
3397 rt6_purge_dflt_routers();
3404 static struct addrconf_sysctl_table
3406 struct ctl_table_header *sysctl_header;
3407 ctl_table addrconf_vars[__NET_IPV6_MAX];
3408 ctl_table addrconf_dev[2];
3409 ctl_table addrconf_conf_dir[2];
3410 ctl_table addrconf_proto_dir[2];
3411 ctl_table addrconf_root_dir[2];
3412 } addrconf_sysctl = {
3413 .sysctl_header = NULL,
3416 .ctl_name = NET_IPV6_FORWARDING,
3417 .procname = "forwarding",
3418 .data = &ipv6_devconf.forwarding,
3419 .maxlen = sizeof(int),
3421 .proc_handler = &addrconf_sysctl_forward,
3422 .strategy = &addrconf_sysctl_forward_strategy,
3425 .ctl_name = NET_IPV6_HOP_LIMIT,
3426 .procname = "hop_limit",
3427 .data = &ipv6_devconf.hop_limit,
3428 .maxlen = sizeof(int),
3430 .proc_handler = proc_dointvec,
3433 .ctl_name = NET_IPV6_MTU,
3435 .data = &ipv6_devconf.mtu6,
3436 .maxlen = sizeof(int),
3438 .proc_handler = &proc_dointvec,
3441 .ctl_name = NET_IPV6_ACCEPT_RA,
3442 .procname = "accept_ra",
3443 .data = &ipv6_devconf.accept_ra,
3444 .maxlen = sizeof(int),
3446 .proc_handler = &proc_dointvec,
3449 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3450 .procname = "accept_redirects",
3451 .data = &ipv6_devconf.accept_redirects,
3452 .maxlen = sizeof(int),
3454 .proc_handler = &proc_dointvec,
3457 .ctl_name = NET_IPV6_AUTOCONF,
3458 .procname = "autoconf",
3459 .data = &ipv6_devconf.autoconf,
3460 .maxlen = sizeof(int),
3462 .proc_handler = &proc_dointvec,
3465 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3466 .procname = "dad_transmits",
3467 .data = &ipv6_devconf.dad_transmits,
3468 .maxlen = sizeof(int),
3470 .proc_handler = &proc_dointvec,
3473 .ctl_name = NET_IPV6_RTR_SOLICITS,
3474 .procname = "router_solicitations",
3475 .data = &ipv6_devconf.rtr_solicits,
3476 .maxlen = sizeof(int),
3478 .proc_handler = &proc_dointvec,
3481 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3482 .procname = "router_solicitation_interval",
3483 .data = &ipv6_devconf.rtr_solicit_interval,
3484 .maxlen = sizeof(int),
3486 .proc_handler = &proc_dointvec_jiffies,
3487 .strategy = &sysctl_jiffies,
3490 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3491 .procname = "router_solicitation_delay",
3492 .data = &ipv6_devconf.rtr_solicit_delay,
3493 .maxlen = sizeof(int),
3495 .proc_handler = &proc_dointvec_jiffies,
3496 .strategy = &sysctl_jiffies,
3499 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3500 .procname = "force_mld_version",
3501 .data = &ipv6_devconf.force_mld_version,
3502 .maxlen = sizeof(int),
3504 .proc_handler = &proc_dointvec,
3506 #ifdef CONFIG_IPV6_PRIVACY
3508 .ctl_name = NET_IPV6_USE_TEMPADDR,
3509 .procname = "use_tempaddr",
3510 .data = &ipv6_devconf.use_tempaddr,
3511 .maxlen = sizeof(int),
3513 .proc_handler = &proc_dointvec,
3516 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3517 .procname = "temp_valid_lft",
3518 .data = &ipv6_devconf.temp_valid_lft,
3519 .maxlen = sizeof(int),
3521 .proc_handler = &proc_dointvec,
3524 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3525 .procname = "temp_prefered_lft",
3526 .data = &ipv6_devconf.temp_prefered_lft,
3527 .maxlen = sizeof(int),
3529 .proc_handler = &proc_dointvec,
3532 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3533 .procname = "regen_max_retry",
3534 .data = &ipv6_devconf.regen_max_retry,
3535 .maxlen = sizeof(int),
3537 .proc_handler = &proc_dointvec,
3540 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3541 .procname = "max_desync_factor",
3542 .data = &ipv6_devconf.max_desync_factor,
3543 .maxlen = sizeof(int),
3545 .proc_handler = &proc_dointvec,
3549 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3550 .procname = "max_addresses",
3551 .data = &ipv6_devconf.max_addresses,
3552 .maxlen = sizeof(int),
3554 .proc_handler = &proc_dointvec,
3557 .ctl_name = 0, /* sentinel */
3562 .ctl_name = NET_PROTO_CONF_ALL,
3565 .child = addrconf_sysctl.addrconf_vars,
3568 .ctl_name = 0, /* sentinel */
3571 .addrconf_conf_dir = {
3573 .ctl_name = NET_IPV6_CONF,
3576 .child = addrconf_sysctl.addrconf_dev,
3579 .ctl_name = 0, /* sentinel */
3582 .addrconf_proto_dir = {
3584 .ctl_name = NET_IPV6,
3587 .child = addrconf_sysctl.addrconf_conf_dir,
3590 .ctl_name = 0, /* sentinel */
3593 .addrconf_root_dir = {
3595 .ctl_name = CTL_NET,
3598 .child = addrconf_sysctl.addrconf_proto_dir,
3601 .ctl_name = 0, /* sentinel */
3606 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3609 struct net_device *dev = idev ? idev->dev : NULL;
3610 struct addrconf_sysctl_table *t;
3611 char *dev_name = NULL;
3613 t = kmalloc(sizeof(*t), GFP_KERNEL);
3616 memcpy(t, &addrconf_sysctl, sizeof(*t));
3617 for (i=0; t->addrconf_vars[i].data; i++) {
3618 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3619 t->addrconf_vars[i].de = NULL;
3620 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3623 dev_name = dev->name;
3624 t->addrconf_dev[0].ctl_name = dev->ifindex;
3626 dev_name = "default";
3627 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3631 * Make a copy of dev_name, because '.procname' is regarded as const
3632 * by sysctl and we wouldn't want anyone to change it under our feet
3633 * (see SIOCSIFNAME).
3635 dev_name = kstrdup(dev_name, GFP_KERNEL);
3639 t->addrconf_dev[0].procname = dev_name;
3641 t->addrconf_dev[0].child = t->addrconf_vars;
3642 t->addrconf_dev[0].de = NULL;
3643 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3644 t->addrconf_conf_dir[0].de = NULL;
3645 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3646 t->addrconf_proto_dir[0].de = NULL;
3647 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3648 t->addrconf_root_dir[0].de = NULL;
3650 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3651 if (t->sysctl_header == NULL)
3666 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3669 struct addrconf_sysctl_table *t = p->sysctl;
3671 unregister_sysctl_table(t->sysctl_header);
3672 kfree(t->addrconf_dev[0].procname);
3684 int register_inet6addr_notifier(struct notifier_block *nb)
3686 return notifier_chain_register(&inet6addr_chain, nb);
3689 int unregister_inet6addr_notifier(struct notifier_block *nb)
3691 return notifier_chain_unregister(&inet6addr_chain,nb);
3695 * Init / cleanup code
3698 int __init addrconf_init(void)
3702 /* The addrconf netdev notifier requires that loopback_dev
3703 * has it's ipv6 private information allocated and setup
3704 * before it can bring up and give link-local addresses
3705 * to other devices which are up.
3707 * Unfortunately, loopback_dev is not necessarily the first
3708 * entry in the global dev_base list of net devices. In fact,
3709 * it is likely to be the very last entry on that list.
3710 * So this causes the notifier registry below to try and
3711 * give link-local addresses to all devices besides loopback_dev
3712 * first, then loopback_dev, which cases all the non-loopback_dev
3713 * devices to fail to get a link-local address.
3715 * So, as a temporary fix, allocate the ipv6 structure for
3716 * loopback_dev first by hand.
3717 * Longer term, all of the dependencies ipv6 has upon the loopback
3718 * device and it being up should be removed.
3721 if (!ipv6_add_dev(&loopback_dev))
3727 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
3729 register_netdevice_notifier(&ipv6_dev_notf);
3732 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3733 #ifdef CONFIG_SYSCTL
3734 addrconf_sysctl.sysctl_header =
3735 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3736 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3742 void __exit addrconf_cleanup(void)
3744 struct net_device *dev;
3745 struct inet6_dev *idev;
3746 struct inet6_ifaddr *ifa;
3749 unregister_netdevice_notifier(&ipv6_dev_notf);
3751 rtnetlink_links[PF_INET6] = NULL;
3752 #ifdef CONFIG_SYSCTL
3753 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3754 addrconf_sysctl_unregister(&ipv6_devconf);
3763 for (dev=dev_base; dev; dev=dev->next) {
3764 if ((idev = __in6_dev_get(dev)) == NULL)
3766 addrconf_ifdown(dev, 1);
3768 addrconf_ifdown(&loopback_dev, 2);
3774 write_lock_bh(&addrconf_hash_lock);
3775 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3776 for (ifa=inet6_addr_lst[i]; ifa; ) {
3777 struct inet6_ifaddr *bifa;
3780 ifa = ifa->lst_next;
3781 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3782 /* Do not free it; something is wrong.
3783 Now we can investigate it with debugger.
3787 write_unlock_bh(&addrconf_hash_lock);
3789 del_timer(&addr_chk_timer);
3793 #ifdef CONFIG_PROC_FS
3794 proc_net_remove("if_inet6");