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 addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1304 if (dev->addr_len != ETH_ALEN)
1306 memcpy(eui, dev->dev_addr, 3);
1307 memcpy(eui + 5, dev->dev_addr + 3, 3);
1310 * The zSeries OSA network cards can be shared among various
1311 * OS instances, but the OSA cards have only one MAC address.
1312 * This leads to duplicate address conflicts in conjunction
1313 * with IPv6 if more than one instance uses the same card.
1315 * The driver for these cards can deliver a unique 16-bit
1316 * identifier for each instance sharing the same card. It is
1317 * placed instead of 0xFFFE in the interface identifier. The
1318 * "u" bit of the interface identifier is not inverted in this
1319 * case. Hence the resulting interface identifier has local
1320 * scope according to RFC2373.
1323 eui[3] = (dev->dev_id >> 8) & 0xFF;
1324 eui[4] = dev->dev_id & 0xFF;
1333 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1335 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1336 if (dev->addr_len != ARCNET_ALEN)
1339 eui[7] = *(u8*)dev->dev_addr;
1343 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1345 if (dev->addr_len != INFINIBAND_ALEN)
1347 memcpy(eui, dev->dev_addr + 12, 8);
1352 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1354 switch (dev->type) {
1357 case ARPHRD_IEEE802_TR:
1358 return addrconf_ifid_eui48(eui, dev);
1360 return addrconf_ifid_arcnet(eui, dev);
1361 case ARPHRD_INFINIBAND:
1362 return addrconf_ifid_infiniband(eui, dev);
1367 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1370 struct inet6_ifaddr *ifp;
1372 read_lock_bh(&idev->lock);
1373 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1374 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1375 memcpy(eui, ifp->addr.s6_addr+8, 8);
1380 read_unlock_bh(&idev->lock);
1384 #ifdef CONFIG_IPV6_PRIVACY
1385 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1386 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1389 get_random_bytes(idev->rndid, sizeof(idev->rndid));
1390 idev->rndid[0] &= ~0x02;
1393 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1394 * check if generated address is not inappropriate
1396 * - Reserved subnet anycast (RFC 2526)
1397 * 11111101 11....11 1xxxxxxx
1398 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1399 * 00-00-5E-FE-xx-xx-xx-xx
1401 * - XXX: already assigned to an address on the device
1403 if (idev->rndid[0] == 0xfd &&
1404 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1405 (idev->rndid[7]&0x80))
1407 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1408 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1410 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1417 static void ipv6_regen_rndid(unsigned long data)
1419 struct inet6_dev *idev = (struct inet6_dev *) data;
1420 unsigned long expires;
1422 read_lock_bh(&addrconf_lock);
1423 write_lock_bh(&idev->lock);
1428 if (__ipv6_regen_rndid(idev) < 0)
1432 idev->cnf.temp_prefered_lft * HZ -
1433 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1434 if (time_before(expires, jiffies)) {
1436 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1441 if (!mod_timer(&idev->regen_timer, expires))
1445 write_unlock_bh(&idev->lock);
1446 read_unlock_bh(&addrconf_lock);
1450 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1453 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1454 ret = __ipv6_regen_rndid(idev);
1464 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1465 unsigned long expires, u32 flags)
1467 struct in6_rtmsg rtmsg;
1469 memset(&rtmsg, 0, sizeof(rtmsg));
1470 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1471 rtmsg.rtmsg_dst_len = plen;
1472 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1473 rtmsg.rtmsg_ifindex = dev->ifindex;
1474 rtmsg.rtmsg_info = expires;
1475 rtmsg.rtmsg_flags = RTF_UP|flags;
1476 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1478 /* Prevent useless cloning on PtP SIT.
1479 This thing is done here expecting that the whole
1480 class of non-broadcast devices need not cloning.
1482 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1483 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1485 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1488 /* Create "default" multicast route to the interface */
1490 static void addrconf_add_mroute(struct net_device *dev)
1492 struct in6_rtmsg rtmsg;
1494 memset(&rtmsg, 0, sizeof(rtmsg));
1495 ipv6_addr_set(&rtmsg.rtmsg_dst,
1496 htonl(0xFF000000), 0, 0, 0);
1497 rtmsg.rtmsg_dst_len = 8;
1498 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1499 rtmsg.rtmsg_ifindex = dev->ifindex;
1500 rtmsg.rtmsg_flags = RTF_UP;
1501 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1502 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1505 static void sit_route_add(struct net_device *dev)
1507 struct in6_rtmsg rtmsg;
1509 memset(&rtmsg, 0, sizeof(rtmsg));
1511 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1512 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1514 /* prefix length - 96 bits "::d.d.d.d" */
1515 rtmsg.rtmsg_dst_len = 96;
1516 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1517 rtmsg.rtmsg_ifindex = dev->ifindex;
1519 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1522 static void addrconf_add_lroute(struct net_device *dev)
1524 struct in6_addr addr;
1526 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1527 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1530 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1532 struct inet6_dev *idev;
1536 if ((idev = ipv6_find_idev(dev)) == NULL)
1539 /* Add default multicast route */
1540 addrconf_add_mroute(dev);
1542 /* Add link local route */
1543 addrconf_add_lroute(dev);
1547 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1549 struct prefix_info *pinfo;
1553 unsigned long rt_expires;
1554 struct inet6_dev *in6_dev;
1556 pinfo = (struct prefix_info *) opt;
1558 if (len < sizeof(struct prefix_info)) {
1559 ADBG(("addrconf: prefix option too short\n"));
1564 * Validation checks ([ADDRCONF], page 19)
1567 addr_type = ipv6_addr_type(&pinfo->prefix);
1569 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1572 valid_lft = ntohl(pinfo->valid);
1573 prefered_lft = ntohl(pinfo->prefered);
1575 if (prefered_lft > valid_lft) {
1576 if (net_ratelimit())
1577 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1581 in6_dev = in6_dev_get(dev);
1583 if (in6_dev == NULL) {
1584 if (net_ratelimit())
1585 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1590 * Two things going on here:
1591 * 1) Add routes for on-link prefixes
1592 * 2) Configure prefixes with the auto flag set
1595 /* Avoid arithmetic overflow. Really, we could
1596 save rt_expires in seconds, likely valid_lft,
1597 but it would require division in fib gc, that it
1600 if (valid_lft >= 0x7FFFFFFF/HZ)
1601 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1603 rt_expires = valid_lft * HZ;
1606 * We convert this (in jiffies) to clock_t later.
1607 * Avoid arithmetic overflow there as well.
1608 * Overflow can happen only if HZ < USER_HZ.
1610 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1611 rt_expires = 0x7FFFFFFF / USER_HZ;
1613 if (pinfo->onlink) {
1614 struct rt6_info *rt;
1615 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1617 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1618 if (rt->rt6i_flags&RTF_EXPIRES) {
1619 if (valid_lft == 0) {
1620 ip6_del_rt(rt, NULL, NULL, NULL);
1623 rt->rt6i_expires = jiffies + rt_expires;
1626 } else if (valid_lft) {
1627 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1628 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1631 dst_release(&rt->u.dst);
1634 /* Try to figure out our local address for this prefix */
1636 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1637 struct inet6_ifaddr * ifp;
1638 struct in6_addr addr;
1639 int create = 0, update_lft = 0;
1641 if (pinfo->prefix_len == 64) {
1642 memcpy(&addr, &pinfo->prefix, 8);
1643 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1644 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1645 in6_dev_put(in6_dev);
1650 if (net_ratelimit())
1651 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1653 in6_dev_put(in6_dev);
1658 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1660 if (ifp == NULL && valid_lft) {
1661 int max_addresses = in6_dev->cnf.max_addresses;
1663 /* Do not allow to create too much of autoconfigured
1664 * addresses; this would be too easy way to crash kernel.
1666 if (!max_addresses ||
1667 ipv6_count_addresses(in6_dev) < max_addresses)
1668 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1669 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1671 if (!ifp || IS_ERR(ifp)) {
1672 in6_dev_put(in6_dev);
1676 update_lft = create = 1;
1677 ifp->cstamp = jiffies;
1678 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1684 #ifdef CONFIG_IPV6_PRIVACY
1685 struct inet6_ifaddr *ift;
1689 /* update lifetime (RFC2462 5.5.3 e) */
1690 spin_lock(&ifp->lock);
1692 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1693 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1696 if (!update_lft && stored_lft) {
1697 if (valid_lft > MIN_VALID_LIFETIME ||
1698 valid_lft > stored_lft)
1700 else if (stored_lft <= MIN_VALID_LIFETIME) {
1701 /* valid_lft <= stored_lft is always true */
1705 valid_lft = MIN_VALID_LIFETIME;
1706 if (valid_lft < prefered_lft)
1707 prefered_lft = valid_lft;
1713 ifp->valid_lft = valid_lft;
1714 ifp->prefered_lft = prefered_lft;
1717 ifp->flags &= ~IFA_F_DEPRECATED;
1718 spin_unlock(&ifp->lock);
1720 if (!(flags&IFA_F_TENTATIVE))
1721 ipv6_ifa_notify(0, ifp);
1723 spin_unlock(&ifp->lock);
1725 #ifdef CONFIG_IPV6_PRIVACY
1726 read_lock_bh(&in6_dev->lock);
1727 /* update all temporary addresses in the list */
1728 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1730 * When adjusting the lifetimes of an existing
1731 * temporary address, only lower the lifetimes.
1732 * Implementations must not increase the
1733 * lifetimes of an existing temporary address
1734 * when processing a Prefix Information Option.
1736 spin_lock(&ift->lock);
1738 if (ift->valid_lft > valid_lft &&
1739 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1740 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1741 if (ift->prefered_lft > prefered_lft &&
1742 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1743 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1744 spin_unlock(&ift->lock);
1745 if (!(flags&IFA_F_TENTATIVE))
1746 ipv6_ifa_notify(0, ift);
1749 if (create && in6_dev->cnf.use_tempaddr > 0) {
1751 * When a new public address is created as described in [ADDRCONF],
1752 * also create a new temporary address.
1754 read_unlock_bh(&in6_dev->lock);
1755 ipv6_create_tempaddr(ifp, NULL);
1757 read_unlock_bh(&in6_dev->lock);
1764 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1765 in6_dev_put(in6_dev);
1769 * Set destination address.
1770 * Special case for SIT interfaces where we create a new "virtual"
1773 int addrconf_set_dstaddr(void __user *arg)
1775 struct in6_ifreq ireq;
1776 struct net_device *dev;
1782 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1785 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1791 if (dev->type == ARPHRD_SIT) {
1794 struct ip_tunnel_parm p;
1796 err = -EADDRNOTAVAIL;
1797 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1800 memset(&p, 0, sizeof(p));
1801 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1805 p.iph.protocol = IPPROTO_IPV6;
1807 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1809 oldfs = get_fs(); set_fs(KERNEL_DS);
1810 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1815 if ((dev = __dev_get_by_name(p.name)) == NULL)
1817 err = dev_open(dev);
1827 * Manual configuration of address on an interface
1829 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1831 struct inet6_ifaddr *ifp;
1832 struct inet6_dev *idev;
1833 struct net_device *dev;
1838 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1841 if (!(dev->flags&IFF_UP))
1844 if ((idev = addrconf_add_dev(dev)) == NULL)
1847 scope = ipv6_addr_scope(pfx);
1849 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1851 addrconf_dad_start(ifp, 0);
1856 return PTR_ERR(ifp);
1859 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1861 struct inet6_ifaddr *ifp;
1862 struct inet6_dev *idev;
1863 struct net_device *dev;
1865 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1868 if ((idev = __in6_dev_get(dev)) == NULL)
1871 read_lock_bh(&idev->lock);
1872 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1873 if (ifp->prefix_len == plen &&
1874 ipv6_addr_equal(pfx, &ifp->addr)) {
1876 read_unlock_bh(&idev->lock);
1880 /* If the last address is deleted administratively,
1881 disable IPv6 on this interface.
1883 if (idev->addr_list == NULL)
1884 addrconf_ifdown(idev->dev, 1);
1888 read_unlock_bh(&idev->lock);
1889 return -EADDRNOTAVAIL;
1893 int addrconf_add_ifaddr(void __user *arg)
1895 struct in6_ifreq ireq;
1898 if (!capable(CAP_NET_ADMIN))
1901 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1905 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1910 int addrconf_del_ifaddr(void __user *arg)
1912 struct in6_ifreq ireq;
1915 if (!capable(CAP_NET_ADMIN))
1918 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1922 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1927 static void sit_add_v4_addrs(struct inet6_dev *idev)
1929 struct inet6_ifaddr * ifp;
1930 struct in6_addr addr;
1931 struct net_device *dev;
1936 memset(&addr, 0, sizeof(struct in6_addr));
1937 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1939 if (idev->dev->flags&IFF_POINTOPOINT) {
1940 addr.s6_addr32[0] = htonl(0xfe800000);
1943 scope = IPV6_ADDR_COMPATv4;
1946 if (addr.s6_addr32[3]) {
1947 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1949 spin_lock_bh(&ifp->lock);
1950 ifp->flags &= ~IFA_F_TENTATIVE;
1951 spin_unlock_bh(&ifp->lock);
1952 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1958 for (dev = dev_base; dev != NULL; dev = dev->next) {
1959 struct in_device * in_dev = __in_dev_get_rtnl(dev);
1960 if (in_dev && (dev->flags & IFF_UP)) {
1961 struct in_ifaddr * ifa;
1965 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1968 addr.s6_addr32[3] = ifa->ifa_local;
1970 if (ifa->ifa_scope == RT_SCOPE_LINK)
1972 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1973 if (idev->dev->flags&IFF_POINTOPOINT)
1977 if (idev->dev->flags&IFF_POINTOPOINT)
1982 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1985 spin_lock_bh(&ifp->lock);
1986 ifp->flags &= ~IFA_F_TENTATIVE;
1987 spin_unlock_bh(&ifp->lock);
1988 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1996 static void init_loopback(struct net_device *dev)
1998 struct inet6_dev *idev;
1999 struct inet6_ifaddr * ifp;
2005 if ((idev = ipv6_find_idev(dev)) == NULL) {
2006 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2010 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2012 spin_lock_bh(&ifp->lock);
2013 ifp->flags &= ~IFA_F_TENTATIVE;
2014 spin_unlock_bh(&ifp->lock);
2015 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2020 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2022 struct inet6_ifaddr * ifp;
2024 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2026 addrconf_dad_start(ifp, 0);
2031 static void addrconf_dev_config(struct net_device *dev)
2033 struct in6_addr addr;
2034 struct inet6_dev * idev;
2038 if ((dev->type != ARPHRD_ETHER) &&
2039 (dev->type != ARPHRD_FDDI) &&
2040 (dev->type != ARPHRD_IEEE802_TR) &&
2041 (dev->type != ARPHRD_ARCNET) &&
2042 (dev->type != ARPHRD_INFINIBAND)) {
2043 /* Alas, we support only Ethernet autoconfiguration. */
2047 idev = addrconf_add_dev(dev);
2051 memset(&addr, 0, sizeof(struct in6_addr));
2052 addr.s6_addr32[0] = htonl(0xFE800000);
2054 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2055 addrconf_add_linklocal(idev, &addr);
2058 static void addrconf_sit_config(struct net_device *dev)
2060 struct inet6_dev *idev;
2065 * Configure the tunnel with one of our IPv4
2066 * addresses... we should configure all of
2067 * our v4 addrs in the tunnel
2070 if ((idev = ipv6_find_idev(dev)) == NULL) {
2071 printk(KERN_DEBUG "init sit: add_dev failed\n");
2075 sit_add_v4_addrs(idev);
2077 if (dev->flags&IFF_POINTOPOINT) {
2078 addrconf_add_mroute(dev);
2079 addrconf_add_lroute(dev);
2085 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2087 struct in6_addr lladdr;
2089 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2090 addrconf_add_linklocal(idev, &lladdr);
2096 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2098 struct net_device *link_dev;
2100 /* first try to inherit the link-local address from the link device */
2101 if (idev->dev->iflink &&
2102 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2103 if (!ipv6_inherit_linklocal(idev, link_dev))
2106 /* then try to inherit it from any device */
2107 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2108 if (!ipv6_inherit_linklocal(idev, link_dev))
2111 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2115 * Autoconfigure tunnel with a link-local address so routing protocols,
2116 * DHCPv6, MLD etc. can be run over the virtual link
2119 static void addrconf_ip6_tnl_config(struct net_device *dev)
2121 struct inet6_dev *idev;
2125 if ((idev = addrconf_add_dev(dev)) == NULL) {
2126 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2129 ip6_tnl_add_linklocal(idev);
2132 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2135 struct net_device *dev = (struct net_device *) data;
2136 struct inet6_dev *idev = __in6_dev_get(dev);
2137 int run_pending = 0;
2142 if (event == NETDEV_UP) {
2143 if (!netif_carrier_ok(dev)) {
2144 /* device is not ready yet. */
2146 "ADDRCONF(NETDEV_UP): %s: "
2147 "link is not ready\n",
2153 idev->if_flags |= IF_READY;
2155 if (!netif_carrier_ok(dev)) {
2156 /* device is still not ready. */
2161 if (idev->if_flags & IF_READY) {
2162 /* device is already configured. */
2165 idev->if_flags |= IF_READY;
2169 "ADDRCONF(NETDEV_CHANGE): %s: "
2170 "link becomes ready\n",
2178 addrconf_sit_config(dev);
2180 case ARPHRD_TUNNEL6:
2181 addrconf_ip6_tnl_config(dev);
2183 case ARPHRD_LOOPBACK:
2188 addrconf_dev_config(dev);
2193 addrconf_dad_run(idev);
2195 /* If the MTU changed during the interface down, when the
2196 interface up, the changed MTU must be reflected in the
2197 idev as well as routers.
2199 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2200 rt6_mtu_change(dev, dev->mtu);
2201 idev->cnf.mtu6 = dev->mtu;
2203 idev->tstamp = jiffies;
2204 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2205 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2206 stop IPv6 on this interface.
2208 if (dev->mtu < IPV6_MIN_MTU)
2209 addrconf_ifdown(dev, event != NETDEV_DOWN);
2213 case NETDEV_CHANGEMTU:
2214 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2215 rt6_mtu_change(dev, dev->mtu);
2216 idev->cnf.mtu6 = dev->mtu;
2220 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2223 case NETDEV_UNREGISTER:
2225 * Remove all addresses from this interface.
2227 addrconf_ifdown(dev, event != NETDEV_DOWN);
2230 case NETDEV_CHANGENAME:
2231 #ifdef CONFIG_SYSCTL
2233 addrconf_sysctl_unregister(&idev->cnf);
2234 neigh_sysctl_unregister(idev->nd_parms);
2235 neigh_sysctl_register(dev, idev->nd_parms,
2236 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2237 &ndisc_ifinfo_sysctl_change,
2239 addrconf_sysctl_register(idev, &idev->cnf);
2249 * addrconf module should be notified of a device going up
2251 static struct notifier_block ipv6_dev_notf = {
2252 .notifier_call = addrconf_notify,
2256 static int addrconf_ifdown(struct net_device *dev, int how)
2258 struct inet6_dev *idev;
2259 struct inet6_ifaddr *ifa, **bifa;
2264 if (dev == &loopback_dev && how == 1)
2268 neigh_ifdown(&nd_tbl, dev);
2270 idev = __in6_dev_get(dev);
2274 /* Step 1: remove reference to ipv6 device from parent device.
2278 write_lock_bh(&addrconf_lock);
2279 dev->ip6_ptr = NULL;
2281 write_unlock_bh(&addrconf_lock);
2283 /* Step 1.5: remove snmp6 entry */
2284 snmp6_unregister_dev(idev);
2288 /* Step 2: clear hash table */
2289 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2290 bifa = &inet6_addr_lst[i];
2292 write_lock_bh(&addrconf_hash_lock);
2293 while ((ifa = *bifa) != NULL) {
2294 if (ifa->idev == idev) {
2295 *bifa = ifa->lst_next;
2296 ifa->lst_next = NULL;
2297 addrconf_del_timer(ifa);
2301 bifa = &ifa->lst_next;
2303 write_unlock_bh(&addrconf_hash_lock);
2306 write_lock_bh(&idev->lock);
2308 /* Step 3: clear flags for stateless addrconf */
2310 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2312 /* Step 4: clear address list */
2313 #ifdef CONFIG_IPV6_PRIVACY
2314 if (how == 1 && del_timer(&idev->regen_timer))
2317 /* clear tempaddr list */
2318 while ((ifa = idev->tempaddr_list) != NULL) {
2319 idev->tempaddr_list = ifa->tmp_next;
2320 ifa->tmp_next = NULL;
2322 write_unlock_bh(&idev->lock);
2323 spin_lock_bh(&ifa->lock);
2326 in6_ifa_put(ifa->ifpub);
2329 spin_unlock_bh(&ifa->lock);
2331 write_lock_bh(&idev->lock);
2334 while ((ifa = idev->addr_list) != NULL) {
2335 idev->addr_list = ifa->if_next;
2336 ifa->if_next = NULL;
2338 addrconf_del_timer(ifa);
2339 write_unlock_bh(&idev->lock);
2341 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2344 write_lock_bh(&idev->lock);
2346 write_unlock_bh(&idev->lock);
2348 /* Step 5: Discard multicast list */
2351 ipv6_mc_destroy_dev(idev);
2355 /* Step 5: netlink notification of this interface */
2356 idev->tstamp = jiffies;
2357 inet6_ifinfo_notify(RTM_DELLINK, idev);
2359 /* Shot the device (if unregistered) */
2362 #ifdef CONFIG_SYSCTL
2363 addrconf_sysctl_unregister(&idev->cnf);
2364 neigh_sysctl_unregister(idev->nd_parms);
2366 neigh_parms_release(&nd_tbl, idev->nd_parms);
2367 neigh_ifdown(&nd_tbl, dev);
2373 static void addrconf_rs_timer(unsigned long data)
2375 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2377 if (ifp->idev->cnf.forwarding)
2380 if (ifp->idev->if_flags & IF_RA_RCVD) {
2382 * Announcement received after solicitation
2388 spin_lock(&ifp->lock);
2389 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2390 struct in6_addr all_routers;
2392 /* The wait after the last probe can be shorter */
2393 addrconf_mod_timer(ifp, AC_RS,
2394 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2395 ifp->idev->cnf.rtr_solicit_delay :
2396 ifp->idev->cnf.rtr_solicit_interval);
2397 spin_unlock(&ifp->lock);
2399 ipv6_addr_all_routers(&all_routers);
2401 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2403 spin_unlock(&ifp->lock);
2405 * Note: we do not support deprecated "all on-link"
2406 * assumption any longer.
2408 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2409 ifp->idev->dev->name);
2417 * Duplicate Address Detection
2419 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2421 unsigned long rand_num;
2422 struct inet6_dev *idev = ifp->idev;
2424 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2425 ifp->probes = idev->cnf.dad_transmits;
2426 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2429 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2431 struct inet6_dev *idev = ifp->idev;
2432 struct net_device *dev = idev->dev;
2434 addrconf_join_solict(dev, &ifp->addr);
2436 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2437 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2440 net_srandom(ifp->addr.s6_addr32[3]);
2442 read_lock_bh(&idev->lock);
2445 spin_lock_bh(&ifp->lock);
2447 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2448 !(ifp->flags&IFA_F_TENTATIVE)) {
2449 ifp->flags &= ~IFA_F_TENTATIVE;
2450 spin_unlock_bh(&ifp->lock);
2451 read_unlock_bh(&idev->lock);
2453 addrconf_dad_completed(ifp);
2457 if (!(idev->if_flags & IF_READY)) {
2458 spin_unlock_bh(&ifp->lock);
2459 read_unlock_bh(&idev->lock);
2461 * If the defice is not ready:
2462 * - keep it tentative if it is a permanent address.
2463 * - otherwise, kill it.
2466 addrconf_dad_stop(ifp);
2469 addrconf_dad_kick(ifp);
2470 spin_unlock_bh(&ifp->lock);
2472 read_unlock_bh(&idev->lock);
2475 static void addrconf_dad_timer(unsigned long data)
2477 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2478 struct inet6_dev *idev = ifp->idev;
2479 struct in6_addr unspec;
2480 struct in6_addr mcaddr;
2482 read_lock_bh(&idev->lock);
2484 read_unlock_bh(&idev->lock);
2487 spin_lock_bh(&ifp->lock);
2488 if (ifp->probes == 0) {
2490 * DAD was successful
2493 ifp->flags &= ~IFA_F_TENTATIVE;
2494 spin_unlock_bh(&ifp->lock);
2495 read_unlock_bh(&idev->lock);
2497 addrconf_dad_completed(ifp);
2503 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2504 spin_unlock_bh(&ifp->lock);
2505 read_unlock_bh(&idev->lock);
2507 /* send a neighbour solicitation for our addr */
2508 memset(&unspec, 0, sizeof(unspec));
2509 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2510 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2515 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2517 struct net_device * dev = ifp->idev->dev;
2520 * Configure the address for reception. Now it is valid.
2523 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2525 /* If added prefix is link local and forwarding is off,
2526 start sending router solicitations.
2529 if (ifp->idev->cnf.forwarding == 0 &&
2530 ifp->idev->cnf.rtr_solicits > 0 &&
2531 (dev->flags&IFF_LOOPBACK) == 0 &&
2532 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2533 struct in6_addr all_routers;
2535 ipv6_addr_all_routers(&all_routers);
2538 * If a host as already performed a random delay
2539 * [...] as part of DAD [...] there is no need
2540 * to delay again before sending the first RS
2542 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2544 spin_lock_bh(&ifp->lock);
2546 ifp->idev->if_flags |= IF_RS_SENT;
2547 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2548 spin_unlock_bh(&ifp->lock);
2552 static void addrconf_dad_run(struct inet6_dev *idev) {
2553 struct inet6_ifaddr *ifp;
2555 read_lock_bh(&idev->lock);
2556 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2557 spin_lock_bh(&ifp->lock);
2558 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2559 spin_unlock_bh(&ifp->lock);
2562 spin_unlock_bh(&ifp->lock);
2563 addrconf_dad_kick(ifp);
2565 read_unlock_bh(&idev->lock);
2568 #ifdef CONFIG_PROC_FS
2569 struct if6_iter_state {
2573 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2575 struct inet6_ifaddr *ifa = NULL;
2576 struct if6_iter_state *state = seq->private;
2578 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2579 ifa = inet6_addr_lst[state->bucket];
2586 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2588 struct if6_iter_state *state = seq->private;
2590 ifa = ifa->lst_next;
2592 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2593 ifa = inet6_addr_lst[state->bucket];
2599 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2601 struct inet6_ifaddr *ifa = if6_get_first(seq);
2604 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2606 return pos ? NULL : ifa;
2609 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2611 read_lock_bh(&addrconf_hash_lock);
2612 return if6_get_idx(seq, *pos);
2615 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2617 struct inet6_ifaddr *ifa;
2619 ifa = if6_get_next(seq, v);
2624 static void if6_seq_stop(struct seq_file *seq, void *v)
2626 read_unlock_bh(&addrconf_hash_lock);
2629 static int if6_seq_show(struct seq_file *seq, void *v)
2631 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2633 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
2635 ifp->idev->dev->ifindex,
2639 ifp->idev->dev->name);
2643 static struct seq_operations if6_seq_ops = {
2644 .start = if6_seq_start,
2645 .next = if6_seq_next,
2646 .show = if6_seq_show,
2647 .stop = if6_seq_stop,
2650 static int if6_seq_open(struct inode *inode, struct file *file)
2652 struct seq_file *seq;
2654 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2658 memset(s, 0, sizeof(*s));
2660 rc = seq_open(file, &if6_seq_ops);
2664 seq = file->private_data;
2673 static struct file_operations if6_fops = {
2674 .owner = THIS_MODULE,
2675 .open = if6_seq_open,
2677 .llseek = seq_lseek,
2678 .release = seq_release_private,
2681 int __init if6_proc_init(void)
2683 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2688 void if6_proc_exit(void)
2690 proc_net_remove("if_inet6");
2692 #endif /* CONFIG_PROC_FS */
2695 * Periodic address status verification
2698 static void addrconf_verify(unsigned long foo)
2700 struct inet6_ifaddr *ifp;
2701 unsigned long now, next;
2704 spin_lock_bh(&addrconf_verify_lock);
2706 next = now + ADDR_CHECK_FREQUENCY;
2708 del_timer(&addr_chk_timer);
2710 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2713 read_lock(&addrconf_hash_lock);
2714 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2716 #ifdef CONFIG_IPV6_PRIVACY
2717 unsigned long regen_advance;
2720 if (ifp->flags & IFA_F_PERMANENT)
2723 spin_lock(&ifp->lock);
2724 age = (now - ifp->tstamp) / HZ;
2726 #ifdef CONFIG_IPV6_PRIVACY
2727 regen_advance = ifp->idev->cnf.regen_max_retry *
2728 ifp->idev->cnf.dad_transmits *
2729 ifp->idev->nd_parms->retrans_time / HZ;
2732 if (age >= ifp->valid_lft) {
2733 spin_unlock(&ifp->lock);
2735 read_unlock(&addrconf_hash_lock);
2738 } else if (age >= ifp->prefered_lft) {
2739 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2742 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2744 ifp->flags |= IFA_F_DEPRECATED;
2747 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2748 next = ifp->tstamp + ifp->valid_lft * HZ;
2750 spin_unlock(&ifp->lock);
2754 read_unlock(&addrconf_hash_lock);
2756 ipv6_ifa_notify(0, ifp);
2760 #ifdef CONFIG_IPV6_PRIVACY
2761 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2762 !(ifp->flags&IFA_F_TENTATIVE)) {
2763 if (age >= ifp->prefered_lft - regen_advance) {
2764 struct inet6_ifaddr *ifpub = ifp->ifpub;
2765 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2766 next = ifp->tstamp + ifp->prefered_lft * HZ;
2767 if (!ifp->regen_count && ifpub) {
2770 in6_ifa_hold(ifpub);
2771 spin_unlock(&ifp->lock);
2772 read_unlock(&addrconf_hash_lock);
2773 spin_lock(&ifpub->lock);
2774 ifpub->regen_count = 0;
2775 spin_unlock(&ifpub->lock);
2776 ipv6_create_tempaddr(ifpub, ifp);
2781 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2782 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2783 spin_unlock(&ifp->lock);
2786 /* ifp->prefered_lft <= ifp->valid_lft */
2787 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2788 next = ifp->tstamp + ifp->prefered_lft * HZ;
2789 spin_unlock(&ifp->lock);
2792 read_unlock(&addrconf_hash_lock);
2795 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2796 add_timer(&addr_chk_timer);
2797 spin_unlock_bh(&addrconf_verify_lock);
2801 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2803 struct rtattr **rta = arg;
2804 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2805 struct in6_addr *pfx;
2808 if (rta[IFA_ADDRESS-1]) {
2809 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2811 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2813 if (rta[IFA_LOCAL-1]) {
2814 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2816 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2821 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2825 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2827 struct rtattr **rta = arg;
2828 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2829 struct in6_addr *pfx;
2832 if (rta[IFA_ADDRESS-1]) {
2833 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2835 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2837 if (rta[IFA_LOCAL-1]) {
2838 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2840 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2845 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2848 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2849 u32 pid, u32 seq, int event, unsigned int flags)
2851 struct ifaddrmsg *ifm;
2852 struct nlmsghdr *nlh;
2853 struct ifa_cacheinfo ci;
2854 unsigned char *b = skb->tail;
2856 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2857 ifm = NLMSG_DATA(nlh);
2858 ifm->ifa_family = AF_INET6;
2859 ifm->ifa_prefixlen = ifa->prefix_len;
2860 ifm->ifa_flags = ifa->flags;
2861 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2862 if (ifa->scope&IFA_HOST)
2863 ifm->ifa_scope = RT_SCOPE_HOST;
2864 else if (ifa->scope&IFA_LINK)
2865 ifm->ifa_scope = RT_SCOPE_LINK;
2866 else if (ifa->scope&IFA_SITE)
2867 ifm->ifa_scope = RT_SCOPE_SITE;
2868 ifm->ifa_index = ifa->idev->dev->ifindex;
2869 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2870 if (!(ifa->flags&IFA_F_PERMANENT)) {
2871 ci.ifa_prefered = ifa->prefered_lft;
2872 ci.ifa_valid = ifa->valid_lft;
2873 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2874 long tval = (jiffies - ifa->tstamp)/HZ;
2875 ci.ifa_prefered -= tval;
2876 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2877 ci.ifa_valid -= tval;
2880 ci.ifa_prefered = INFINITY_LIFE_TIME;
2881 ci.ifa_valid = INFINITY_LIFE_TIME;
2883 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2884 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2885 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2886 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2887 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2888 nlh->nlmsg_len = skb->tail - b;
2893 skb_trim(skb, b - skb->data);
2897 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2898 u32 pid, u32 seq, int event, u16 flags)
2900 struct ifaddrmsg *ifm;
2901 struct nlmsghdr *nlh;
2902 struct ifa_cacheinfo ci;
2903 unsigned char *b = skb->tail;
2905 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2906 ifm = NLMSG_DATA(nlh);
2907 ifm->ifa_family = AF_INET6;
2908 ifm->ifa_prefixlen = 128;
2909 ifm->ifa_flags = IFA_F_PERMANENT;
2910 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2911 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2912 ifm->ifa_scope = RT_SCOPE_SITE;
2913 ifm->ifa_index = ifmca->idev->dev->ifindex;
2914 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2915 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2916 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2918 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2919 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2921 ci.ifa_prefered = INFINITY_LIFE_TIME;
2922 ci.ifa_valid = INFINITY_LIFE_TIME;
2923 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2924 nlh->nlmsg_len = skb->tail - b;
2929 skb_trim(skb, b - skb->data);
2933 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2934 u32 pid, u32 seq, int event, unsigned int flags)
2936 struct ifaddrmsg *ifm;
2937 struct nlmsghdr *nlh;
2938 struct ifa_cacheinfo ci;
2939 unsigned char *b = skb->tail;
2941 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2942 ifm = NLMSG_DATA(nlh);
2943 ifm->ifa_family = AF_INET6;
2944 ifm->ifa_prefixlen = 128;
2945 ifm->ifa_flags = IFA_F_PERMANENT;
2946 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2947 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2948 ifm->ifa_scope = RT_SCOPE_SITE;
2949 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2950 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2951 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2952 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2954 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2955 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2957 ci.ifa_prefered = INFINITY_LIFE_TIME;
2958 ci.ifa_valid = INFINITY_LIFE_TIME;
2959 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2960 nlh->nlmsg_len = skb->tail - b;
2965 skb_trim(skb, b - skb->data);
2976 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2977 enum addr_type_t type)
2980 int s_idx, s_ip_idx;
2982 struct net_device *dev;
2983 struct inet6_dev *idev = NULL;
2984 struct inet6_ifaddr *ifa;
2985 struct ifmcaddr6 *ifmca;
2986 struct ifacaddr6 *ifaca;
2988 s_idx = cb->args[0];
2989 s_ip_idx = ip_idx = cb->args[1];
2990 read_lock(&dev_base_lock);
2992 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
2998 if ((idev = in6_dev_get(dev)) == NULL)
3000 read_lock_bh(&idev->lock);
3003 /* unicast address incl. temp addr */
3004 for (ifa = idev->addr_list; ifa;
3005 ifa = ifa->if_next, ip_idx++) {
3006 if (ip_idx < s_ip_idx)
3008 if ((err = inet6_fill_ifaddr(skb, ifa,
3009 NETLINK_CB(cb->skb).pid,
3010 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3015 case MULTICAST_ADDR:
3016 /* multicast address */
3017 for (ifmca = idev->mc_list; ifmca;
3018 ifmca = ifmca->next, ip_idx++) {
3019 if (ip_idx < s_ip_idx)
3021 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3022 NETLINK_CB(cb->skb).pid,
3023 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3029 /* anycast address */
3030 for (ifaca = idev->ac_list; ifaca;
3031 ifaca = ifaca->aca_next, ip_idx++) {
3032 if (ip_idx < s_ip_idx)
3034 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3035 NETLINK_CB(cb->skb).pid,
3036 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3044 read_unlock_bh(&idev->lock);
3049 read_unlock_bh(&idev->lock);
3052 read_unlock(&dev_base_lock);
3054 cb->args[1] = ip_idx;
3058 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3060 enum addr_type_t type = UNICAST_ADDR;
3061 return inet6_dump_addr(skb, cb, type);
3064 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3066 enum addr_type_t type = MULTICAST_ADDR;
3067 return inet6_dump_addr(skb, cb, type);
3071 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3073 enum addr_type_t type = ANYCAST_ADDR;
3074 return inet6_dump_addr(skb, cb, type);
3077 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3079 struct sk_buff *skb;
3080 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
3082 skb = alloc_skb(size, GFP_ATOMIC);
3084 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
3087 if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
3089 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
3092 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
3093 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
3096 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3097 __s32 *array, int bytes)
3099 memset(array, 0, bytes);
3100 array[DEVCONF_FORWARDING] = cnf->forwarding;
3101 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3102 array[DEVCONF_MTU6] = cnf->mtu6;
3103 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3104 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3105 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3106 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3107 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3108 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3109 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3110 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3111 #ifdef CONFIG_IPV6_PRIVACY
3112 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3113 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3114 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3115 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3116 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3118 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3121 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3122 u32 pid, u32 seq, int event, unsigned int flags)
3124 struct net_device *dev = idev->dev;
3125 __s32 *array = NULL;
3126 struct ifinfomsg *r;
3127 struct nlmsghdr *nlh;
3128 unsigned char *b = skb->tail;
3129 struct rtattr *subattr;
3130 __u32 mtu = dev->mtu;
3131 struct ifla_cacheinfo ci;
3133 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
3134 r = NLMSG_DATA(nlh);
3135 r->ifi_family = AF_INET6;
3137 r->ifi_type = dev->type;
3138 r->ifi_index = dev->ifindex;
3139 r->ifi_flags = dev_get_flags(dev);
3142 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
3145 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3147 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
3148 if (dev->ifindex != dev->iflink)
3149 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
3151 subattr = (struct rtattr*)skb->tail;
3153 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
3155 /* return the device flags */
3156 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
3158 /* return interface cacheinfo */
3159 ci.max_reasm_len = IPV6_MAXPLEN;
3160 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3161 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3162 ci.reachable_time = idev->nd_parms->reachable_time;
3163 ci.retrans_time = idev->nd_parms->retrans_time;
3164 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3166 /* return the device sysctl params */
3167 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
3168 goto rtattr_failure;
3169 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
3170 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
3172 /* XXX - Statistics/MC not implemented */
3173 subattr->rta_len = skb->tail - (u8*)subattr;
3175 nlh->nlmsg_len = skb->tail - b;
3182 skb_trim(skb, b - skb->data);
3186 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3189 int s_idx = cb->args[0];
3190 struct net_device *dev;
3191 struct inet6_dev *idev;
3193 read_lock(&dev_base_lock);
3194 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3197 if ((idev = in6_dev_get(dev)) == NULL)
3199 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3200 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3205 read_unlock(&dev_base_lock);
3211 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3213 struct sk_buff *skb;
3215 int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
3217 skb = alloc_skb(size, GFP_ATOMIC);
3219 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
3222 if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
3224 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
3227 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
3228 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
3231 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3232 struct prefix_info *pinfo, u32 pid, u32 seq,
3233 int event, unsigned int flags)
3235 struct prefixmsg *pmsg;
3236 struct nlmsghdr *nlh;
3237 unsigned char *b = skb->tail;
3238 struct prefix_cacheinfo ci;
3240 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
3241 pmsg = NLMSG_DATA(nlh);
3242 pmsg->prefix_family = AF_INET6;
3243 pmsg->prefix_pad1 = 0;
3244 pmsg->prefix_pad2 = 0;
3245 pmsg->prefix_ifindex = idev->dev->ifindex;
3246 pmsg->prefix_len = pinfo->prefix_len;
3247 pmsg->prefix_type = pinfo->type;
3248 pmsg->prefix_pad3 = 0;
3250 pmsg->prefix_flags = 0;
3252 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3253 if (pinfo->autoconf)
3254 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3256 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3258 ci.preferred_time = ntohl(pinfo->prefered);
3259 ci.valid_time = ntohl(pinfo->valid);
3260 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3262 nlh->nlmsg_len = skb->tail - b;
3267 skb_trim(skb, b - skb->data);
3271 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3272 struct prefix_info *pinfo)
3274 struct sk_buff *skb;
3275 int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
3277 skb = alloc_skb(size, GFP_ATOMIC);
3279 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
3282 if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
3284 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
3287 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
3288 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
3291 static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
3292 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3293 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3294 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
3295 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
3296 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3297 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3298 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3299 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3300 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3301 .dumpit = inet6_dump_fib, },
3304 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3306 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3310 ip6_ins_rt(ifp->rt, NULL, NULL, NULL);
3311 if (ifp->idev->cnf.forwarding)
3312 addrconf_join_anycast(ifp);
3315 if (ifp->idev->cnf.forwarding)
3316 addrconf_leave_anycast(ifp);
3317 addrconf_leave_solict(ifp->idev, &ifp->addr);
3318 dst_hold(&ifp->rt->u.dst);
3319 if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
3320 dst_free(&ifp->rt->u.dst);
3325 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3327 read_lock_bh(&addrconf_lock);
3328 if (likely(ifp->idev->dead == 0))
3329 __ipv6_ifa_notify(event, ifp);
3330 read_unlock_bh(&addrconf_lock);
3333 #ifdef CONFIG_SYSCTL
3336 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3337 void __user *buffer, size_t *lenp, loff_t *ppos)
3339 int *valp = ctl->data;
3343 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3345 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3346 if (valp != &ipv6_devconf.forwarding) {
3347 if ((!*valp) ^ (!val)) {
3348 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3351 dev_forward_change(idev);
3354 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3355 addrconf_forward_change();
3358 rt6_purge_dflt_routers();
3364 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3365 int __user *name, int nlen,
3366 void __user *oldval,
3367 size_t __user *oldlenp,
3368 void __user *newval, size_t newlen,
3371 int *valp = table->data;
3374 if (!newval || !newlen)
3376 if (newlen != sizeof(int))
3378 if (get_user(new, (int __user *)newval))
3382 if (oldval && oldlenp) {
3384 if (get_user(len, oldlenp))
3387 if (len > table->maxlen)
3388 len = table->maxlen;
3389 if (copy_to_user(oldval, valp, len))
3391 if (put_user(len, oldlenp))
3396 if (valp != &ipv6_devconf_dflt.forwarding) {
3397 if (valp != &ipv6_devconf.forwarding) {
3398 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3400 if (unlikely(idev == NULL))
3402 changed = (!*valp) ^ (!new);
3405 dev_forward_change(idev);
3408 addrconf_forward_change();
3412 rt6_purge_dflt_routers();
3419 static struct addrconf_sysctl_table
3421 struct ctl_table_header *sysctl_header;
3422 ctl_table addrconf_vars[__NET_IPV6_MAX];
3423 ctl_table addrconf_dev[2];
3424 ctl_table addrconf_conf_dir[2];
3425 ctl_table addrconf_proto_dir[2];
3426 ctl_table addrconf_root_dir[2];
3427 } addrconf_sysctl = {
3428 .sysctl_header = NULL,
3431 .ctl_name = NET_IPV6_FORWARDING,
3432 .procname = "forwarding",
3433 .data = &ipv6_devconf.forwarding,
3434 .maxlen = sizeof(int),
3436 .proc_handler = &addrconf_sysctl_forward,
3437 .strategy = &addrconf_sysctl_forward_strategy,
3440 .ctl_name = NET_IPV6_HOP_LIMIT,
3441 .procname = "hop_limit",
3442 .data = &ipv6_devconf.hop_limit,
3443 .maxlen = sizeof(int),
3445 .proc_handler = proc_dointvec,
3448 .ctl_name = NET_IPV6_MTU,
3450 .data = &ipv6_devconf.mtu6,
3451 .maxlen = sizeof(int),
3453 .proc_handler = &proc_dointvec,
3456 .ctl_name = NET_IPV6_ACCEPT_RA,
3457 .procname = "accept_ra",
3458 .data = &ipv6_devconf.accept_ra,
3459 .maxlen = sizeof(int),
3461 .proc_handler = &proc_dointvec,
3464 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3465 .procname = "accept_redirects",
3466 .data = &ipv6_devconf.accept_redirects,
3467 .maxlen = sizeof(int),
3469 .proc_handler = &proc_dointvec,
3472 .ctl_name = NET_IPV6_AUTOCONF,
3473 .procname = "autoconf",
3474 .data = &ipv6_devconf.autoconf,
3475 .maxlen = sizeof(int),
3477 .proc_handler = &proc_dointvec,
3480 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3481 .procname = "dad_transmits",
3482 .data = &ipv6_devconf.dad_transmits,
3483 .maxlen = sizeof(int),
3485 .proc_handler = &proc_dointvec,
3488 .ctl_name = NET_IPV6_RTR_SOLICITS,
3489 .procname = "router_solicitations",
3490 .data = &ipv6_devconf.rtr_solicits,
3491 .maxlen = sizeof(int),
3493 .proc_handler = &proc_dointvec,
3496 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3497 .procname = "router_solicitation_interval",
3498 .data = &ipv6_devconf.rtr_solicit_interval,
3499 .maxlen = sizeof(int),
3501 .proc_handler = &proc_dointvec_jiffies,
3502 .strategy = &sysctl_jiffies,
3505 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3506 .procname = "router_solicitation_delay",
3507 .data = &ipv6_devconf.rtr_solicit_delay,
3508 .maxlen = sizeof(int),
3510 .proc_handler = &proc_dointvec_jiffies,
3511 .strategy = &sysctl_jiffies,
3514 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3515 .procname = "force_mld_version",
3516 .data = &ipv6_devconf.force_mld_version,
3517 .maxlen = sizeof(int),
3519 .proc_handler = &proc_dointvec,
3521 #ifdef CONFIG_IPV6_PRIVACY
3523 .ctl_name = NET_IPV6_USE_TEMPADDR,
3524 .procname = "use_tempaddr",
3525 .data = &ipv6_devconf.use_tempaddr,
3526 .maxlen = sizeof(int),
3528 .proc_handler = &proc_dointvec,
3531 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3532 .procname = "temp_valid_lft",
3533 .data = &ipv6_devconf.temp_valid_lft,
3534 .maxlen = sizeof(int),
3536 .proc_handler = &proc_dointvec,
3539 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3540 .procname = "temp_prefered_lft",
3541 .data = &ipv6_devconf.temp_prefered_lft,
3542 .maxlen = sizeof(int),
3544 .proc_handler = &proc_dointvec,
3547 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3548 .procname = "regen_max_retry",
3549 .data = &ipv6_devconf.regen_max_retry,
3550 .maxlen = sizeof(int),
3552 .proc_handler = &proc_dointvec,
3555 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3556 .procname = "max_desync_factor",
3557 .data = &ipv6_devconf.max_desync_factor,
3558 .maxlen = sizeof(int),
3560 .proc_handler = &proc_dointvec,
3564 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3565 .procname = "max_addresses",
3566 .data = &ipv6_devconf.max_addresses,
3567 .maxlen = sizeof(int),
3569 .proc_handler = &proc_dointvec,
3572 .ctl_name = 0, /* sentinel */
3577 .ctl_name = NET_PROTO_CONF_ALL,
3580 .child = addrconf_sysctl.addrconf_vars,
3583 .ctl_name = 0, /* sentinel */
3586 .addrconf_conf_dir = {
3588 .ctl_name = NET_IPV6_CONF,
3591 .child = addrconf_sysctl.addrconf_dev,
3594 .ctl_name = 0, /* sentinel */
3597 .addrconf_proto_dir = {
3599 .ctl_name = NET_IPV6,
3602 .child = addrconf_sysctl.addrconf_conf_dir,
3605 .ctl_name = 0, /* sentinel */
3608 .addrconf_root_dir = {
3610 .ctl_name = CTL_NET,
3613 .child = addrconf_sysctl.addrconf_proto_dir,
3616 .ctl_name = 0, /* sentinel */
3621 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3624 struct net_device *dev = idev ? idev->dev : NULL;
3625 struct addrconf_sysctl_table *t;
3626 char *dev_name = NULL;
3628 t = kmalloc(sizeof(*t), GFP_KERNEL);
3631 memcpy(t, &addrconf_sysctl, sizeof(*t));
3632 for (i=0; t->addrconf_vars[i].data; i++) {
3633 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3634 t->addrconf_vars[i].de = NULL;
3635 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3638 dev_name = dev->name;
3639 t->addrconf_dev[0].ctl_name = dev->ifindex;
3641 dev_name = "default";
3642 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3646 * Make a copy of dev_name, because '.procname' is regarded as const
3647 * by sysctl and we wouldn't want anyone to change it under our feet
3648 * (see SIOCSIFNAME).
3650 dev_name = kstrdup(dev_name, GFP_KERNEL);
3654 t->addrconf_dev[0].procname = dev_name;
3656 t->addrconf_dev[0].child = t->addrconf_vars;
3657 t->addrconf_dev[0].de = NULL;
3658 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3659 t->addrconf_conf_dir[0].de = NULL;
3660 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3661 t->addrconf_proto_dir[0].de = NULL;
3662 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3663 t->addrconf_root_dir[0].de = NULL;
3665 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3666 if (t->sysctl_header == NULL)
3681 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3684 struct addrconf_sysctl_table *t = p->sysctl;
3686 unregister_sysctl_table(t->sysctl_header);
3687 kfree(t->addrconf_dev[0].procname);
3699 int register_inet6addr_notifier(struct notifier_block *nb)
3701 return notifier_chain_register(&inet6addr_chain, nb);
3704 int unregister_inet6addr_notifier(struct notifier_block *nb)
3706 return notifier_chain_unregister(&inet6addr_chain,nb);
3710 * Init / cleanup code
3713 int __init addrconf_init(void)
3717 /* The addrconf netdev notifier requires that loopback_dev
3718 * has it's ipv6 private information allocated and setup
3719 * before it can bring up and give link-local addresses
3720 * to other devices which are up.
3722 * Unfortunately, loopback_dev is not necessarily the first
3723 * entry in the global dev_base list of net devices. In fact,
3724 * it is likely to be the very last entry on that list.
3725 * So this causes the notifier registry below to try and
3726 * give link-local addresses to all devices besides loopback_dev
3727 * first, then loopback_dev, which cases all the non-loopback_dev
3728 * devices to fail to get a link-local address.
3730 * So, as a temporary fix, allocate the ipv6 structure for
3731 * loopback_dev first by hand.
3732 * Longer term, all of the dependencies ipv6 has upon the loopback
3733 * device and it being up should be removed.
3736 if (!ipv6_add_dev(&loopback_dev))
3742 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
3744 register_netdevice_notifier(&ipv6_dev_notf);
3747 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3748 #ifdef CONFIG_SYSCTL
3749 addrconf_sysctl.sysctl_header =
3750 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3751 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3757 void __exit addrconf_cleanup(void)
3759 struct net_device *dev;
3760 struct inet6_dev *idev;
3761 struct inet6_ifaddr *ifa;
3764 unregister_netdevice_notifier(&ipv6_dev_notf);
3766 rtnetlink_links[PF_INET6] = NULL;
3767 #ifdef CONFIG_SYSCTL
3768 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3769 addrconf_sysctl_unregister(&ipv6_devconf);
3778 for (dev=dev_base; dev; dev=dev->next) {
3779 if ((idev = __in6_dev_get(dev)) == NULL)
3781 addrconf_ifdown(dev, 1);
3783 addrconf_ifdown(&loopback_dev, 2);
3789 write_lock_bh(&addrconf_hash_lock);
3790 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3791 for (ifa=inet6_addr_lst[i]; ifa; ) {
3792 struct inet6_ifaddr *bifa;
3795 ifa = ifa->lst_next;
3796 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3797 /* Do not free it; something is wrong.
3798 Now we can investigate it with debugger.
3802 write_unlock_bh(&addrconf_hash_lock);
3804 del_timer(&addr_chk_timer);
3808 #ifdef CONFIG_PROC_FS
3809 proc_net_remove("if_inet6");