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>
81 #include <linux/crypto.h>
82 #include <linux/scatterlist.h>
85 #include <asm/uaccess.h>
87 #include <linux/proc_fs.h>
88 #include <linux/seq_file.h>
90 /* Set to 3 to get tracing... */
94 #define ADBG(x) printk x
99 #define INFINITY_LIFE_TIME 0xFFFFFFFF
100 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
103 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
104 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
107 #ifdef CONFIG_IPV6_PRIVACY
108 static int __ipv6_regen_rndid(struct inet6_dev *idev);
109 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
110 static void ipv6_regen_rndid(unsigned long data);
112 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
113 static struct crypto_tfm *md5_tfm;
114 static DEFINE_SPINLOCK(md5_tfm_lock);
117 static int ipv6_count_addresses(struct inet6_dev *idev);
120 * Configured unicast address hash table
122 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
123 static DEFINE_RWLOCK(addrconf_hash_lock);
125 /* Protects inet6 devices */
126 DEFINE_RWLOCK(addrconf_lock);
128 static void addrconf_verify(unsigned long);
130 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
131 static DEFINE_SPINLOCK(addrconf_verify_lock);
133 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
134 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
136 static int addrconf_ifdown(struct net_device *dev, int how);
138 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
139 static void addrconf_dad_timer(unsigned long data);
140 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
141 static void addrconf_dad_run(struct inet6_dev *idev);
142 static void addrconf_rs_timer(unsigned long data);
143 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
144 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
146 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
147 struct prefix_info *pinfo);
148 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
150 static struct notifier_block *inet6addr_chain;
152 struct ipv6_devconf ipv6_devconf = {
154 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
155 .mtu6 = IPV6_MIN_MTU,
157 .accept_redirects = 1,
159 .force_mld_version = 0,
161 .rtr_solicits = MAX_RTR_SOLICITATIONS,
162 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
163 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
164 #ifdef CONFIG_IPV6_PRIVACY
166 .temp_valid_lft = TEMP_VALID_LIFETIME,
167 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
168 .regen_max_retry = REGEN_MAX_RETRY,
169 .max_desync_factor = MAX_DESYNC_FACTOR,
171 .max_addresses = IPV6_MAX_ADDRESSES,
174 static struct ipv6_devconf ipv6_devconf_dflt = {
176 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
177 .mtu6 = IPV6_MIN_MTU,
179 .accept_redirects = 1,
182 .rtr_solicits = MAX_RTR_SOLICITATIONS,
183 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
184 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
185 #ifdef CONFIG_IPV6_PRIVACY
187 .temp_valid_lft = TEMP_VALID_LIFETIME,
188 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
189 .regen_max_retry = REGEN_MAX_RETRY,
190 .max_desync_factor = MAX_DESYNC_FACTOR,
192 .max_addresses = IPV6_MAX_ADDRESSES,
195 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
197 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
199 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
201 #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
203 static inline unsigned ipv6_addr_scope2type(unsigned scope)
206 case IPV6_ADDR_SCOPE_NODELOCAL:
207 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
209 case IPV6_ADDR_SCOPE_LINKLOCAL:
210 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
211 IPV6_ADDR_LINKLOCAL);
212 case IPV6_ADDR_SCOPE_SITELOCAL:
213 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
214 IPV6_ADDR_SITELOCAL);
216 return IPV6_ADDR_SCOPE_TYPE(scope);
219 int __ipv6_addr_type(const struct in6_addr *addr)
223 st = addr->s6_addr32[0];
225 /* Consider all addresses with the first three bits different of
226 000 and 111 as unicasts.
228 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
229 (st & htonl(0xE0000000)) != htonl(0xE0000000))
230 return (IPV6_ADDR_UNICAST |
231 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
233 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
235 /* addr-select 3.1 */
236 return (IPV6_ADDR_MULTICAST |
237 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
240 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
241 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
242 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
243 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
244 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
245 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
247 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
248 if (addr->s6_addr32[2] == 0) {
249 if (addr->s6_addr32[3] == 0)
250 return IPV6_ADDR_ANY;
252 if (addr->s6_addr32[3] == htonl(0x00000001))
253 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
254 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
256 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
257 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
260 if (addr->s6_addr32[2] == htonl(0x0000ffff))
261 return (IPV6_ADDR_MAPPED |
262 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
265 return (IPV6_ADDR_RESERVED |
266 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
269 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
271 if (del_timer(&ifp->timer))
275 enum addrconf_timer_t
282 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
283 enum addrconf_timer_t what,
286 if (!del_timer(&ifp->timer))
291 ifp->timer.function = addrconf_dad_timer;
294 ifp->timer.function = addrconf_rs_timer;
298 ifp->timer.expires = jiffies + when;
299 add_timer(&ifp->timer);
302 /* Nobody refers to this device, we may destroy it. */
304 void in6_dev_finish_destroy(struct inet6_dev *idev)
306 struct net_device *dev = idev->dev;
307 BUG_TRAP(idev->addr_list==NULL);
308 BUG_TRAP(idev->mc_list==NULL);
309 #ifdef NET_REFCNT_DEBUG
310 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
314 printk("Freeing alive inet6 device %p\n", idev);
317 snmp6_free_dev(idev);
321 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
323 struct inet6_dev *ndev;
327 if (dev->mtu < IPV6_MIN_MTU)
330 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
333 memset(ndev, 0, sizeof(struct inet6_dev));
335 rwlock_init(&ndev->lock);
337 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
338 ndev->cnf.mtu6 = dev->mtu;
339 ndev->cnf.sysctl = NULL;
340 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
341 if (ndev->nd_parms == NULL) {
345 /* We refer to the device */
348 if (snmp6_alloc_dev(ndev) < 0) {
350 "%s(): cannot allocate memory for statistics; dev=%s.\n",
351 __FUNCTION__, dev->name));
352 neigh_parms_release(&nd_tbl, ndev->nd_parms);
354 in6_dev_finish_destroy(ndev);
358 if (snmp6_register_dev(ndev) < 0) {
360 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
361 __FUNCTION__, dev->name));
362 neigh_parms_release(&nd_tbl, ndev->nd_parms);
364 in6_dev_finish_destroy(ndev);
368 /* One reference from device. We must do this before
369 * we invoke __ipv6_regen_rndid().
373 #ifdef CONFIG_IPV6_PRIVACY
374 get_random_bytes(ndev->rndid, sizeof(ndev->rndid));
375 get_random_bytes(ndev->entropy, sizeof(ndev->entropy));
376 init_timer(&ndev->regen_timer);
377 ndev->regen_timer.function = ipv6_regen_rndid;
378 ndev->regen_timer.data = (unsigned long) ndev;
379 if ((dev->flags&IFF_LOOPBACK) ||
380 dev->type == ARPHRD_TUNNEL ||
381 dev->type == ARPHRD_NONE ||
382 dev->type == ARPHRD_SIT) {
384 "%s: Disabled Privacy Extensions\n",
386 ndev->cnf.use_tempaddr = -1;
389 ipv6_regen_rndid((unsigned long) ndev);
393 if (netif_carrier_ok(dev))
394 ndev->if_flags |= IF_READY;
396 ipv6_mc_init_dev(ndev);
397 ndev->tstamp = jiffies;
399 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
400 NET_IPV6_NEIGH, "ipv6",
401 &ndisc_ifinfo_sysctl_change,
403 addrconf_sysctl_register(ndev, &ndev->cnf);
405 write_lock_bh(&addrconf_lock);
407 write_unlock_bh(&addrconf_lock);
413 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
415 struct inet6_dev *idev;
419 if ((idev = __in6_dev_get(dev)) == NULL) {
420 if ((idev = ipv6_add_dev(dev)) == NULL)
424 if (dev->flags&IFF_UP)
430 static void dev_forward_change(struct inet6_dev *idev)
432 struct net_device *dev;
433 struct inet6_ifaddr *ifa;
434 struct in6_addr addr;
439 if (dev && (dev->flags & IFF_MULTICAST)) {
440 ipv6_addr_all_routers(&addr);
442 if (idev->cnf.forwarding)
443 ipv6_dev_mc_inc(dev, &addr);
445 ipv6_dev_mc_dec(dev, &addr);
447 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
448 if (idev->cnf.forwarding)
449 addrconf_join_anycast(ifa);
451 addrconf_leave_anycast(ifa);
456 static void addrconf_forward_change(void)
458 struct net_device *dev;
459 struct inet6_dev *idev;
461 read_lock(&dev_base_lock);
462 for (dev=dev_base; dev; dev=dev->next) {
463 read_lock(&addrconf_lock);
464 idev = __in6_dev_get(dev);
466 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
467 idev->cnf.forwarding = ipv6_devconf.forwarding;
469 dev_forward_change(idev);
471 read_unlock(&addrconf_lock);
473 read_unlock(&dev_base_lock);
477 /* Nobody refers to this ifaddr, destroy it */
479 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
481 BUG_TRAP(ifp->if_next==NULL);
482 BUG_TRAP(ifp->lst_next==NULL);
483 #ifdef NET_REFCNT_DEBUG
484 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
487 in6_dev_put(ifp->idev);
489 if (del_timer(&ifp->timer))
490 printk("Timer is still running, when freeing ifa=%p\n", ifp);
493 printk("Freeing alive inet6 address %p\n", ifp);
496 dst_release(&ifp->rt->u.dst);
501 /* On success it returns ifp with increased reference count */
503 static struct inet6_ifaddr *
504 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
505 int scope, u32 flags)
507 struct inet6_ifaddr *ifa = NULL;
512 read_lock_bh(&addrconf_lock);
514 err = -ENODEV; /*XXX*/
518 write_lock(&addrconf_hash_lock);
520 /* Ignore adding duplicate addresses on an interface */
521 if (ipv6_chk_same_addr(addr, idev->dev)) {
522 ADBG(("ipv6_add_addr: already assigned\n"));
527 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
530 ADBG(("ipv6_add_addr: malloc failed\n"));
535 rt = addrconf_dst_alloc(idev, addr, 0);
541 memset(ifa, 0, sizeof(struct inet6_ifaddr));
542 ipv6_addr_copy(&ifa->addr, addr);
544 spin_lock_init(&ifa->lock);
545 init_timer(&ifa->timer);
546 ifa->timer.data = (unsigned long) ifa;
548 ifa->prefix_len = pfxlen;
549 ifa->flags = flags | IFA_F_TENTATIVE;
550 ifa->cstamp = ifa->tstamp = jiffies;
557 /* Add to big hash table */
558 hash = ipv6_addr_hash(addr);
560 ifa->lst_next = inet6_addr_lst[hash];
561 inet6_addr_lst[hash] = ifa;
563 write_unlock(&addrconf_hash_lock);
565 write_lock(&idev->lock);
566 /* Add to inet6_dev unicast addr list. */
567 ifa->if_next = idev->addr_list;
568 idev->addr_list = ifa;
570 #ifdef CONFIG_IPV6_PRIVACY
571 if (ifa->flags&IFA_F_TEMPORARY) {
572 ifa->tmp_next = idev->tempaddr_list;
573 idev->tempaddr_list = ifa;
581 write_unlock(&idev->lock);
583 read_unlock_bh(&addrconf_lock);
585 if (likely(err == 0))
586 notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
594 write_unlock(&addrconf_hash_lock);
598 /* This function wants to get referenced ifp and releases it before return */
600 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
602 struct inet6_ifaddr *ifa, **ifap;
603 struct inet6_dev *idev = ifp->idev;
605 int deleted = 0, onlink = 0;
606 unsigned long expires = jiffies;
608 hash = ipv6_addr_hash(&ifp->addr);
612 write_lock_bh(&addrconf_hash_lock);
613 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
614 ifap = &ifa->lst_next) {
616 *ifap = ifa->lst_next;
618 ifa->lst_next = NULL;
622 write_unlock_bh(&addrconf_hash_lock);
624 write_lock_bh(&idev->lock);
625 #ifdef CONFIG_IPV6_PRIVACY
626 if (ifp->flags&IFA_F_TEMPORARY) {
627 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
628 ifap = &ifa->tmp_next) {
630 *ifap = ifa->tmp_next;
632 in6_ifa_put(ifp->ifpub);
636 ifa->tmp_next = NULL;
643 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
645 *ifap = ifa->if_next;
648 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
652 } else if (ifp->flags & IFA_F_PERMANENT) {
653 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
655 if (ifa->flags & IFA_F_PERMANENT) {
660 unsigned long lifetime;
665 spin_lock(&ifa->lock);
666 lifetime = min_t(unsigned long,
667 ifa->valid_lft, 0x7fffffffUL/HZ);
668 if (time_before(expires,
669 ifa->tstamp + lifetime * HZ))
670 expires = ifa->tstamp + lifetime * HZ;
671 spin_unlock(&ifa->lock);
675 ifap = &ifa->if_next;
677 write_unlock_bh(&idev->lock);
679 ipv6_ifa_notify(RTM_DELADDR, ifp);
681 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
683 addrconf_del_timer(ifp);
686 * Purge or update corresponding prefix
688 * 1) we don't purge prefix here if address was not permanent.
689 * prefix is managed by its own lifetime.
690 * 2) if there're no addresses, delete prefix.
691 * 3) if there're still other permanent address(es),
692 * corresponding prefix is still permanent.
693 * 4) otherwise, update prefix lifetime to the
694 * longest valid lifetime among the corresponding
695 * addresses on the device.
696 * Note: subsequent RA will update lifetime.
700 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
701 struct in6_addr prefix;
704 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
705 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
707 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
709 ip6_del_rt(rt, NULL, NULL, NULL);
711 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
712 rt->rt6i_expires = expires;
713 rt->rt6i_flags |= RTF_EXPIRES;
716 dst_release(&rt->u.dst);
722 #ifdef CONFIG_IPV6_PRIVACY
723 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
725 struct inet6_dev *idev = ifp->idev;
726 struct in6_addr addr, *tmpaddr;
727 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
732 write_lock(&idev->lock);
734 spin_lock_bh(&ift->lock);
735 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
736 spin_unlock_bh(&ift->lock);
743 if (idev->cnf.use_tempaddr <= 0) {
744 write_unlock(&idev->lock);
746 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
751 spin_lock_bh(&ifp->lock);
752 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
753 idev->cnf.use_tempaddr = -1; /*XXX*/
754 spin_unlock_bh(&ifp->lock);
755 write_unlock(&idev->lock);
757 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
763 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
764 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
765 spin_unlock_bh(&ifp->lock);
766 write_unlock(&idev->lock);
768 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
774 memcpy(&addr.s6_addr[8], idev->rndid, 8);
775 tmp_valid_lft = min_t(__u32,
777 idev->cnf.temp_valid_lft);
778 tmp_prefered_lft = min_t(__u32,
780 idev->cnf.temp_prefered_lft - desync_factor / HZ);
781 tmp_plen = ifp->prefix_len;
782 max_addresses = idev->cnf.max_addresses;
783 tmp_cstamp = ifp->cstamp;
784 tmp_tstamp = ifp->tstamp;
785 spin_unlock_bh(&ifp->lock);
787 write_unlock(&idev->lock);
788 ift = !max_addresses ||
789 ipv6_count_addresses(idev) < max_addresses ?
790 ipv6_add_addr(idev, &addr, tmp_plen,
791 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
792 if (!ift || IS_ERR(ift)) {
796 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
798 write_lock(&idev->lock);
802 spin_lock_bh(&ift->lock);
804 ift->valid_lft = tmp_valid_lft;
805 ift->prefered_lft = tmp_prefered_lft;
806 ift->cstamp = tmp_cstamp;
807 ift->tstamp = tmp_tstamp;
808 spin_unlock_bh(&ift->lock);
810 addrconf_dad_start(ift, 0);
819 * Choose an appropriate source address (RFC3484)
821 struct ipv6_saddr_score {
829 #define IPV6_SADDR_SCORE_LOCAL 0x0001
830 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
831 #define IPV6_SADDR_SCORE_HOA 0x0008
832 #define IPV6_SADDR_SCORE_OIF 0x0010
833 #define IPV6_SADDR_SCORE_LABEL 0x0020
834 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
836 static int inline ipv6_saddr_preferred(int type)
838 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
839 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
844 /* static matching label */
845 static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
848 * prefix (longest match) label
849 * -----------------------------
858 if (type & IPV6_ADDR_LOOPBACK)
860 else if (type & IPV6_ADDR_COMPATv4)
862 else if (type & IPV6_ADDR_MAPPED)
864 else if (addr->s6_addr32[0] == htonl(0x20010000))
866 else if (addr->s6_addr16[0] == htons(0x2002))
868 else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
873 int ipv6_dev_get_saddr(struct net_device *daddr_dev,
874 struct in6_addr *daddr, struct in6_addr *saddr)
876 struct ipv6_saddr_score hiscore;
877 struct inet6_ifaddr *ifa_result = NULL;
878 int daddr_type = __ipv6_addr_type(daddr);
879 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
880 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
881 struct net_device *dev;
883 memset(&hiscore, 0, sizeof(hiscore));
885 read_lock(&dev_base_lock);
886 read_lock(&addrconf_lock);
888 for (dev = dev_base; dev; dev=dev->next) {
889 struct inet6_dev *idev;
890 struct inet6_ifaddr *ifa;
892 /* Rule 0: Candidate Source Address (section 4)
893 * - multicast and link-local destination address,
894 * the set of candidate source address MUST only
895 * include addresses assigned to interfaces
896 * belonging to the same link as the outgoing
898 * (- For site-local destination addresses, the
899 * set of candidate source addresses MUST only
900 * include addresses assigned to interfaces
901 * belonging to the same site as the outgoing
904 if ((daddr_type & IPV6_ADDR_MULTICAST ||
905 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
906 daddr_dev && dev != daddr_dev)
909 idev = __in6_dev_get(dev);
913 read_lock_bh(&idev->lock);
914 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
915 struct ipv6_saddr_score score;
917 score.addr_type = __ipv6_addr_type(&ifa->addr);
920 * - Tentative Address (RFC2462 section 5.4)
921 * - A tentative address is not considered
922 * "assigned to an interface" in the traditional
924 * - Candidate Source Address (section 4)
925 * - In any case, anycast addresses, multicast
926 * addresses, and the unspecified address MUST
927 * NOT be included in a candidate set.
929 if (ifa->flags & IFA_F_TENTATIVE)
931 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
932 score.addr_type & IPV6_ADDR_MULTICAST)) {
933 LIMIT_NETDEBUG(KERN_DEBUG
934 "ADDRCONF: unspecified / multicast address"
935 "assigned as unicast address on %s",
945 if (ifa_result == NULL) {
946 /* record it if the first available entry */
950 /* Rule 1: Prefer same address */
951 if (hiscore.rule < 1) {
952 if (ipv6_addr_equal(&ifa_result->addr, daddr))
953 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
956 if (ipv6_addr_equal(&ifa->addr, daddr)) {
957 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
958 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
963 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
967 /* Rule 2: Prefer appropriate scope */
968 if (hiscore.rule < 2) {
969 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
972 score.scope = __ipv6_addr_src_scope(score.addr_type);
973 if (hiscore.scope < score.scope) {
974 if (hiscore.scope < daddr_scope) {
979 } else if (score.scope < hiscore.scope) {
980 if (score.scope < daddr_scope)
988 /* Rule 3: Avoid deprecated address */
989 if (hiscore.rule < 3) {
990 if (ipv6_saddr_preferred(hiscore.addr_type) ||
991 !(ifa_result->flags & IFA_F_DEPRECATED))
992 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
995 if (ipv6_saddr_preferred(score.addr_type) ||
996 !(ifa->flags & IFA_F_DEPRECATED)) {
997 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
998 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1003 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1007 /* Rule 4: Prefer home address -- not implemented yet */
1008 if (hiscore.rule < 4)
1011 /* Rule 5: Prefer outgoing interface */
1012 if (hiscore.rule < 5) {
1013 if (daddr_dev == NULL ||
1014 daddr_dev == ifa_result->idev->dev)
1015 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1018 if (daddr_dev == NULL ||
1019 daddr_dev == ifa->idev->dev) {
1020 score.attrs |= IPV6_SADDR_SCORE_OIF;
1021 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1026 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1030 /* Rule 6: Prefer matching label */
1031 if (hiscore.rule < 6) {
1032 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1033 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1036 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1037 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1038 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1043 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1047 #ifdef CONFIG_IPV6_PRIVACY
1048 /* Rule 7: Prefer public address
1049 * Note: prefer temprary address if use_tempaddr >= 2
1051 if (hiscore.rule < 7) {
1052 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1053 (ifa_result->idev->cnf.use_tempaddr >= 2))
1054 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1057 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1058 (ifa->idev->cnf.use_tempaddr >= 2)) {
1059 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1060 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1065 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1069 if (hiscore.rule < 7)
1072 /* Rule 8: Use longest matching prefix */
1073 if (hiscore.rule < 8) {
1074 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
1077 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1078 if (score.matchlen > hiscore.matchlen) {
1083 else if (score.matchlen < hiscore.matchlen)
1087 /* Final Rule: choose first available one */
1091 in6_ifa_put(ifa_result);
1096 read_unlock_bh(&idev->lock);
1098 read_unlock(&addrconf_lock);
1099 read_unlock(&dev_base_lock);
1102 return -EADDRNOTAVAIL;
1104 ipv6_addr_copy(saddr, &ifa_result->addr);
1105 in6_ifa_put(ifa_result);
1110 int ipv6_get_saddr(struct dst_entry *dst,
1111 struct in6_addr *daddr, struct in6_addr *saddr)
1113 return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
1117 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1119 struct inet6_dev *idev;
1120 int err = -EADDRNOTAVAIL;
1122 read_lock(&addrconf_lock);
1123 if ((idev = __in6_dev_get(dev)) != NULL) {
1124 struct inet6_ifaddr *ifp;
1126 read_lock_bh(&idev->lock);
1127 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1128 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1129 ipv6_addr_copy(addr, &ifp->addr);
1134 read_unlock_bh(&idev->lock);
1136 read_unlock(&addrconf_lock);
1140 static int ipv6_count_addresses(struct inet6_dev *idev)
1143 struct inet6_ifaddr *ifp;
1145 read_lock_bh(&idev->lock);
1146 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1148 read_unlock_bh(&idev->lock);
1152 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1154 struct inet6_ifaddr * ifp;
1155 u8 hash = ipv6_addr_hash(addr);
1157 read_lock_bh(&addrconf_hash_lock);
1158 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1159 if (ipv6_addr_equal(&ifp->addr, addr) &&
1160 !(ifp->flags&IFA_F_TENTATIVE)) {
1161 if (dev == NULL || ifp->idev->dev == dev ||
1162 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1166 read_unlock_bh(&addrconf_hash_lock);
1171 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1173 struct inet6_ifaddr * ifp;
1174 u8 hash = ipv6_addr_hash(addr);
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)
1185 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1187 struct inet6_ifaddr * ifp;
1188 u8 hash = ipv6_addr_hash(addr);
1190 read_lock_bh(&addrconf_hash_lock);
1191 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1192 if (ipv6_addr_equal(&ifp->addr, addr)) {
1193 if (dev == NULL || ifp->idev->dev == dev ||
1194 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1200 read_unlock_bh(&addrconf_hash_lock);
1205 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1207 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1208 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
1209 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1210 u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1211 int sk_ipv6only = ipv6_only_sock(sk);
1212 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1213 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1214 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1216 if (!sk2_rcv_saddr && !sk_ipv6only)
1219 if (addr_type2 == IPV6_ADDR_ANY &&
1220 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1223 if (addr_type == IPV6_ADDR_ANY &&
1224 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1227 if (sk2_rcv_saddr6 &&
1228 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1231 if (addr_type == IPV6_ADDR_MAPPED &&
1233 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1239 /* Gets referenced address, destroys ifaddr */
1241 static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1243 if (ifp->flags&IFA_F_PERMANENT) {
1244 spin_lock_bh(&ifp->lock);
1245 addrconf_del_timer(ifp);
1246 ifp->flags |= IFA_F_TENTATIVE;
1247 spin_unlock_bh(&ifp->lock);
1249 #ifdef CONFIG_IPV6_PRIVACY
1250 } else if (ifp->flags&IFA_F_TEMPORARY) {
1251 struct inet6_ifaddr *ifpub;
1252 spin_lock_bh(&ifp->lock);
1255 in6_ifa_hold(ifpub);
1256 spin_unlock_bh(&ifp->lock);
1257 ipv6_create_tempaddr(ifpub, ifp);
1260 spin_unlock_bh(&ifp->lock);
1268 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1270 if (net_ratelimit())
1271 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1272 addrconf_dad_stop(ifp);
1275 /* Join to solicited addr multicast group. */
1277 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1279 struct in6_addr maddr;
1281 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1284 addrconf_addr_solict_mult(addr, &maddr);
1285 ipv6_dev_mc_inc(dev, &maddr);
1288 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1290 struct in6_addr maddr;
1292 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1295 addrconf_addr_solict_mult(addr, &maddr);
1296 __ipv6_dev_mc_dec(idev, &maddr);
1299 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1301 struct in6_addr addr;
1302 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1303 if (ipv6_addr_any(&addr))
1305 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1308 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1310 struct in6_addr addr;
1311 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1312 if (ipv6_addr_any(&addr))
1314 __ipv6_dev_ac_dec(ifp->idev, &addr);
1317 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1319 switch (dev->type) {
1322 case ARPHRD_IEEE802_TR:
1323 if (dev->addr_len != ETH_ALEN)
1325 memcpy(eui, dev->dev_addr, 3);
1326 memcpy(eui + 5, dev->dev_addr + 3, 3);
1329 * The zSeries OSA network cards can be shared among various
1330 * OS instances, but the OSA cards have only one MAC address.
1331 * This leads to duplicate address conflicts in conjunction
1332 * with IPv6 if more than one instance uses the same card.
1334 * The driver for these cards can deliver a unique 16-bit
1335 * identifier for each instance sharing the same card. It is
1336 * placed instead of 0xFFFE in the interface identifier. The
1337 * "u" bit of the interface identifier is not inverted in this
1338 * case. Hence the resulting interface identifier has local
1339 * scope according to RFC2373.
1342 eui[3] = (dev->dev_id >> 8) & 0xFF;
1343 eui[4] = dev->dev_id & 0xFF;
1351 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1352 if (dev->addr_len != ARCNET_ALEN)
1355 eui[7] = *(u8*)dev->dev_addr;
1357 case ARPHRD_INFINIBAND:
1358 if (dev->addr_len != INFINIBAND_ALEN)
1360 memcpy(eui, dev->dev_addr + 12, 8);
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)
1388 struct net_device *dev;
1389 struct scatterlist sg[2];
1391 sg_set_buf(&sg[0], idev->entropy, 8);
1392 sg_set_buf(&sg[1], idev->work_eui64, 8);
1396 if (ipv6_generate_eui64(idev->work_eui64, dev)) {
1398 "__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
1400 get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
1403 spin_lock(&md5_tfm_lock);
1404 if (unlikely(md5_tfm == NULL)) {
1405 spin_unlock(&md5_tfm_lock);
1408 crypto_digest_init(md5_tfm);
1409 crypto_digest_update(md5_tfm, sg, 2);
1410 crypto_digest_final(md5_tfm, idev->work_digest);
1411 spin_unlock(&md5_tfm_lock);
1413 memcpy(idev->rndid, &idev->work_digest[0], 8);
1414 idev->rndid[0] &= ~0x02;
1415 memcpy(idev->entropy, &idev->work_digest[8], 8);
1418 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1419 * check if generated address is not inappropriate
1421 * - Reserved subnet anycast (RFC 2526)
1422 * 11111101 11....11 1xxxxxxx
1423 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1424 * 00-00-5E-FE-xx-xx-xx-xx
1426 * - XXX: already assigned to an address on the device
1428 if (idev->rndid[0] == 0xfd &&
1429 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1430 (idev->rndid[7]&0x80))
1432 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1433 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1435 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1442 static void ipv6_regen_rndid(unsigned long data)
1444 struct inet6_dev *idev = (struct inet6_dev *) data;
1445 unsigned long expires;
1447 read_lock_bh(&addrconf_lock);
1448 write_lock_bh(&idev->lock);
1453 if (__ipv6_regen_rndid(idev) < 0)
1457 idev->cnf.temp_prefered_lft * HZ -
1458 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1459 if (time_before(expires, jiffies)) {
1461 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1466 if (!mod_timer(&idev->regen_timer, expires))
1470 write_unlock_bh(&idev->lock);
1471 read_unlock_bh(&addrconf_lock);
1475 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1478 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1479 ret = __ipv6_regen_rndid(idev);
1489 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1490 unsigned long expires, u32 flags)
1492 struct in6_rtmsg rtmsg;
1494 memset(&rtmsg, 0, sizeof(rtmsg));
1495 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1496 rtmsg.rtmsg_dst_len = plen;
1497 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1498 rtmsg.rtmsg_ifindex = dev->ifindex;
1499 rtmsg.rtmsg_info = expires;
1500 rtmsg.rtmsg_flags = RTF_UP|flags;
1501 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1503 /* Prevent useless cloning on PtP SIT.
1504 This thing is done here expecting that the whole
1505 class of non-broadcast devices need not cloning.
1507 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1508 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1510 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1513 /* Create "default" multicast route to the interface */
1515 static void addrconf_add_mroute(struct net_device *dev)
1517 struct in6_rtmsg rtmsg;
1519 memset(&rtmsg, 0, sizeof(rtmsg));
1520 ipv6_addr_set(&rtmsg.rtmsg_dst,
1521 htonl(0xFF000000), 0, 0, 0);
1522 rtmsg.rtmsg_dst_len = 8;
1523 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1524 rtmsg.rtmsg_ifindex = dev->ifindex;
1525 rtmsg.rtmsg_flags = RTF_UP;
1526 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1527 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1530 static void sit_route_add(struct net_device *dev)
1532 struct in6_rtmsg rtmsg;
1534 memset(&rtmsg, 0, sizeof(rtmsg));
1536 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1537 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1539 /* prefix length - 96 bits "::d.d.d.d" */
1540 rtmsg.rtmsg_dst_len = 96;
1541 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1542 rtmsg.rtmsg_ifindex = dev->ifindex;
1544 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1547 static void addrconf_add_lroute(struct net_device *dev)
1549 struct in6_addr addr;
1551 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1552 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1555 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1557 struct inet6_dev *idev;
1561 if ((idev = ipv6_find_idev(dev)) == NULL)
1564 /* Add default multicast route */
1565 addrconf_add_mroute(dev);
1567 /* Add link local route */
1568 addrconf_add_lroute(dev);
1572 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1574 struct prefix_info *pinfo;
1578 unsigned long rt_expires;
1579 struct inet6_dev *in6_dev;
1581 pinfo = (struct prefix_info *) opt;
1583 if (len < sizeof(struct prefix_info)) {
1584 ADBG(("addrconf: prefix option too short\n"));
1589 * Validation checks ([ADDRCONF], page 19)
1592 addr_type = ipv6_addr_type(&pinfo->prefix);
1594 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1597 valid_lft = ntohl(pinfo->valid);
1598 prefered_lft = ntohl(pinfo->prefered);
1600 if (prefered_lft > valid_lft) {
1601 if (net_ratelimit())
1602 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1606 in6_dev = in6_dev_get(dev);
1608 if (in6_dev == NULL) {
1609 if (net_ratelimit())
1610 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1615 * Two things going on here:
1616 * 1) Add routes for on-link prefixes
1617 * 2) Configure prefixes with the auto flag set
1620 /* Avoid arithmetic overflow. Really, we could
1621 save rt_expires in seconds, likely valid_lft,
1622 but it would require division in fib gc, that it
1625 if (valid_lft >= 0x7FFFFFFF/HZ)
1626 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1628 rt_expires = valid_lft * HZ;
1631 * We convert this (in jiffies) to clock_t later.
1632 * Avoid arithmetic overflow there as well.
1633 * Overflow can happen only if HZ < USER_HZ.
1635 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1636 rt_expires = 0x7FFFFFFF / USER_HZ;
1638 if (pinfo->onlink) {
1639 struct rt6_info *rt;
1640 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1642 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1643 if (rt->rt6i_flags&RTF_EXPIRES) {
1644 if (valid_lft == 0) {
1645 ip6_del_rt(rt, NULL, NULL, NULL);
1648 rt->rt6i_expires = jiffies + rt_expires;
1651 } else if (valid_lft) {
1652 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1653 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1656 dst_release(&rt->u.dst);
1659 /* Try to figure out our local address for this prefix */
1661 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1662 struct inet6_ifaddr * ifp;
1663 struct in6_addr addr;
1664 int create = 0, update_lft = 0;
1666 if (pinfo->prefix_len == 64) {
1667 memcpy(&addr, &pinfo->prefix, 8);
1668 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1669 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1670 in6_dev_put(in6_dev);
1675 if (net_ratelimit())
1676 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1678 in6_dev_put(in6_dev);
1683 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1685 if (ifp == NULL && valid_lft) {
1686 int max_addresses = in6_dev->cnf.max_addresses;
1688 /* Do not allow to create too much of autoconfigured
1689 * addresses; this would be too easy way to crash kernel.
1691 if (!max_addresses ||
1692 ipv6_count_addresses(in6_dev) < max_addresses)
1693 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1694 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1696 if (!ifp || IS_ERR(ifp)) {
1697 in6_dev_put(in6_dev);
1701 update_lft = create = 1;
1702 ifp->cstamp = jiffies;
1703 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1709 #ifdef CONFIG_IPV6_PRIVACY
1710 struct inet6_ifaddr *ift;
1714 /* update lifetime (RFC2462 5.5.3 e) */
1715 spin_lock(&ifp->lock);
1717 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1718 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1721 if (!update_lft && stored_lft) {
1722 if (valid_lft > MIN_VALID_LIFETIME ||
1723 valid_lft > stored_lft)
1725 else if (stored_lft <= MIN_VALID_LIFETIME) {
1726 /* valid_lft <= stored_lft is always true */
1730 valid_lft = MIN_VALID_LIFETIME;
1731 if (valid_lft < prefered_lft)
1732 prefered_lft = valid_lft;
1738 ifp->valid_lft = valid_lft;
1739 ifp->prefered_lft = prefered_lft;
1742 ifp->flags &= ~IFA_F_DEPRECATED;
1743 spin_unlock(&ifp->lock);
1745 if (!(flags&IFA_F_TENTATIVE))
1746 ipv6_ifa_notify(0, ifp);
1748 spin_unlock(&ifp->lock);
1750 #ifdef CONFIG_IPV6_PRIVACY
1751 read_lock_bh(&in6_dev->lock);
1752 /* update all temporary addresses in the list */
1753 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1755 * When adjusting the lifetimes of an existing
1756 * temporary address, only lower the lifetimes.
1757 * Implementations must not increase the
1758 * lifetimes of an existing temporary address
1759 * when processing a Prefix Information Option.
1761 spin_lock(&ift->lock);
1763 if (ift->valid_lft > valid_lft &&
1764 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1765 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1766 if (ift->prefered_lft > prefered_lft &&
1767 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1768 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1769 spin_unlock(&ift->lock);
1770 if (!(flags&IFA_F_TENTATIVE))
1771 ipv6_ifa_notify(0, ift);
1774 if (create && in6_dev->cnf.use_tempaddr > 0) {
1776 * When a new public address is created as described in [ADDRCONF],
1777 * also create a new temporary address.
1779 read_unlock_bh(&in6_dev->lock);
1780 ipv6_create_tempaddr(ifp, NULL);
1782 read_unlock_bh(&in6_dev->lock);
1789 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1790 in6_dev_put(in6_dev);
1794 * Set destination address.
1795 * Special case for SIT interfaces where we create a new "virtual"
1798 int addrconf_set_dstaddr(void __user *arg)
1800 struct in6_ifreq ireq;
1801 struct net_device *dev;
1807 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1810 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1816 if (dev->type == ARPHRD_SIT) {
1819 struct ip_tunnel_parm p;
1821 err = -EADDRNOTAVAIL;
1822 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1825 memset(&p, 0, sizeof(p));
1826 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1830 p.iph.protocol = IPPROTO_IPV6;
1832 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1834 oldfs = get_fs(); set_fs(KERNEL_DS);
1835 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1840 if ((dev = __dev_get_by_name(p.name)) == NULL)
1842 err = dev_open(dev);
1852 * Manual configuration of address on an interface
1854 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1856 struct inet6_ifaddr *ifp;
1857 struct inet6_dev *idev;
1858 struct net_device *dev;
1863 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1866 if (!(dev->flags&IFF_UP))
1869 if ((idev = addrconf_add_dev(dev)) == NULL)
1872 scope = ipv6_addr_scope(pfx);
1874 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1876 addrconf_dad_start(ifp, 0);
1881 return PTR_ERR(ifp);
1884 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1886 struct inet6_ifaddr *ifp;
1887 struct inet6_dev *idev;
1888 struct net_device *dev;
1890 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1893 if ((idev = __in6_dev_get(dev)) == NULL)
1896 read_lock_bh(&idev->lock);
1897 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1898 if (ifp->prefix_len == plen &&
1899 ipv6_addr_equal(pfx, &ifp->addr)) {
1901 read_unlock_bh(&idev->lock);
1905 /* If the last address is deleted administratively,
1906 disable IPv6 on this interface.
1908 if (idev->addr_list == NULL)
1909 addrconf_ifdown(idev->dev, 1);
1913 read_unlock_bh(&idev->lock);
1914 return -EADDRNOTAVAIL;
1918 int addrconf_add_ifaddr(void __user *arg)
1920 struct in6_ifreq ireq;
1923 if (!capable(CAP_NET_ADMIN))
1926 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1930 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1935 int addrconf_del_ifaddr(void __user *arg)
1937 struct in6_ifreq ireq;
1940 if (!capable(CAP_NET_ADMIN))
1943 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1947 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1952 static void sit_add_v4_addrs(struct inet6_dev *idev)
1954 struct inet6_ifaddr * ifp;
1955 struct in6_addr addr;
1956 struct net_device *dev;
1961 memset(&addr, 0, sizeof(struct in6_addr));
1962 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1964 if (idev->dev->flags&IFF_POINTOPOINT) {
1965 addr.s6_addr32[0] = htonl(0xfe800000);
1968 scope = IPV6_ADDR_COMPATv4;
1971 if (addr.s6_addr32[3]) {
1972 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1974 spin_lock_bh(&ifp->lock);
1975 ifp->flags &= ~IFA_F_TENTATIVE;
1976 spin_unlock_bh(&ifp->lock);
1977 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1983 for (dev = dev_base; dev != NULL; dev = dev->next) {
1984 struct in_device * in_dev = __in_dev_get_rtnl(dev);
1985 if (in_dev && (dev->flags & IFF_UP)) {
1986 struct in_ifaddr * ifa;
1990 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1993 addr.s6_addr32[3] = ifa->ifa_local;
1995 if (ifa->ifa_scope == RT_SCOPE_LINK)
1997 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1998 if (idev->dev->flags&IFF_POINTOPOINT)
2002 if (idev->dev->flags&IFF_POINTOPOINT)
2007 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2010 spin_lock_bh(&ifp->lock);
2011 ifp->flags &= ~IFA_F_TENTATIVE;
2012 spin_unlock_bh(&ifp->lock);
2013 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2021 static void init_loopback(struct net_device *dev)
2023 struct inet6_dev *idev;
2024 struct inet6_ifaddr * ifp;
2030 if ((idev = ipv6_find_idev(dev)) == NULL) {
2031 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2035 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2037 spin_lock_bh(&ifp->lock);
2038 ifp->flags &= ~IFA_F_TENTATIVE;
2039 spin_unlock_bh(&ifp->lock);
2040 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2045 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2047 struct inet6_ifaddr * ifp;
2049 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2051 addrconf_dad_start(ifp, 0);
2056 static void addrconf_dev_config(struct net_device *dev)
2058 struct in6_addr addr;
2059 struct inet6_dev * idev;
2063 if ((dev->type != ARPHRD_ETHER) &&
2064 (dev->type != ARPHRD_FDDI) &&
2065 (dev->type != ARPHRD_IEEE802_TR) &&
2066 (dev->type != ARPHRD_ARCNET) &&
2067 (dev->type != ARPHRD_INFINIBAND)) {
2068 /* Alas, we support only Ethernet autoconfiguration. */
2072 idev = addrconf_add_dev(dev);
2076 memset(&addr, 0, sizeof(struct in6_addr));
2077 addr.s6_addr32[0] = htonl(0xFE800000);
2079 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2080 addrconf_add_linklocal(idev, &addr);
2083 static void addrconf_sit_config(struct net_device *dev)
2085 struct inet6_dev *idev;
2090 * Configure the tunnel with one of our IPv4
2091 * addresses... we should configure all of
2092 * our v4 addrs in the tunnel
2095 if ((idev = ipv6_find_idev(dev)) == NULL) {
2096 printk(KERN_DEBUG "init sit: add_dev failed\n");
2100 sit_add_v4_addrs(idev);
2102 if (dev->flags&IFF_POINTOPOINT) {
2103 addrconf_add_mroute(dev);
2104 addrconf_add_lroute(dev);
2110 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2112 struct in6_addr lladdr;
2114 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2115 addrconf_add_linklocal(idev, &lladdr);
2121 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2123 struct net_device *link_dev;
2125 /* first try to inherit the link-local address from the link device */
2126 if (idev->dev->iflink &&
2127 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2128 if (!ipv6_inherit_linklocal(idev, link_dev))
2131 /* then try to inherit it from any device */
2132 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2133 if (!ipv6_inherit_linklocal(idev, link_dev))
2136 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2140 * Autoconfigure tunnel with a link-local address so routing protocols,
2141 * DHCPv6, MLD etc. can be run over the virtual link
2144 static void addrconf_ip6_tnl_config(struct net_device *dev)
2146 struct inet6_dev *idev;
2150 if ((idev = addrconf_add_dev(dev)) == NULL) {
2151 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2154 ip6_tnl_add_linklocal(idev);
2155 addrconf_add_mroute(dev);
2158 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2161 struct net_device *dev = (struct net_device *) data;
2162 struct inet6_dev *idev = __in6_dev_get(dev);
2163 int run_pending = 0;
2168 if (event == NETDEV_UP) {
2169 if (!netif_carrier_ok(dev)) {
2170 /* device is not ready yet. */
2172 "ADDRCONF(NETDEV_UP): %s: "
2173 "link is not ready\n",
2179 idev->if_flags |= IF_READY;
2181 if (!netif_carrier_ok(dev)) {
2182 /* device is still not ready. */
2187 if (idev->if_flags & IF_READY) {
2188 /* device is already configured. */
2191 idev->if_flags |= IF_READY;
2195 "ADDRCONF(NETDEV_CHANGE): %s: "
2196 "link becomes ready\n",
2204 addrconf_sit_config(dev);
2206 case ARPHRD_TUNNEL6:
2207 addrconf_ip6_tnl_config(dev);
2209 case ARPHRD_LOOPBACK:
2214 addrconf_dev_config(dev);
2219 addrconf_dad_run(idev);
2221 /* If the MTU changed during the interface down, when the
2222 interface up, the changed MTU must be reflected in the
2223 idev as well as routers.
2225 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2226 rt6_mtu_change(dev, dev->mtu);
2227 idev->cnf.mtu6 = dev->mtu;
2229 idev->tstamp = jiffies;
2230 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2231 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2232 stop IPv6 on this interface.
2234 if (dev->mtu < IPV6_MIN_MTU)
2235 addrconf_ifdown(dev, event != NETDEV_DOWN);
2239 case NETDEV_CHANGEMTU:
2240 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2241 rt6_mtu_change(dev, dev->mtu);
2242 idev->cnf.mtu6 = dev->mtu;
2246 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2249 case NETDEV_UNREGISTER:
2251 * Remove all addresses from this interface.
2253 addrconf_ifdown(dev, event != NETDEV_DOWN);
2256 case NETDEV_CHANGENAME:
2257 #ifdef CONFIG_SYSCTL
2259 addrconf_sysctl_unregister(&idev->cnf);
2260 neigh_sysctl_unregister(idev->nd_parms);
2261 neigh_sysctl_register(dev, idev->nd_parms,
2262 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2263 &ndisc_ifinfo_sysctl_change,
2265 addrconf_sysctl_register(idev, &idev->cnf);
2275 * addrconf module should be notified of a device going up
2277 static struct notifier_block ipv6_dev_notf = {
2278 .notifier_call = addrconf_notify,
2282 static int addrconf_ifdown(struct net_device *dev, int how)
2284 struct inet6_dev *idev;
2285 struct inet6_ifaddr *ifa, **bifa;
2290 if (dev == &loopback_dev && how == 1)
2294 neigh_ifdown(&nd_tbl, dev);
2296 idev = __in6_dev_get(dev);
2300 /* Step 1: remove reference to ipv6 device from parent device.
2304 write_lock_bh(&addrconf_lock);
2305 dev->ip6_ptr = NULL;
2307 write_unlock_bh(&addrconf_lock);
2309 /* Step 1.5: remove snmp6 entry */
2310 snmp6_unregister_dev(idev);
2314 /* Step 2: clear hash table */
2315 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2316 bifa = &inet6_addr_lst[i];
2318 write_lock_bh(&addrconf_hash_lock);
2319 while ((ifa = *bifa) != NULL) {
2320 if (ifa->idev == idev) {
2321 *bifa = ifa->lst_next;
2322 ifa->lst_next = NULL;
2323 addrconf_del_timer(ifa);
2327 bifa = &ifa->lst_next;
2329 write_unlock_bh(&addrconf_hash_lock);
2332 write_lock_bh(&idev->lock);
2334 /* Step 3: clear flags for stateless addrconf */
2336 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2338 /* Step 4: clear address list */
2339 #ifdef CONFIG_IPV6_PRIVACY
2340 if (how == 1 && del_timer(&idev->regen_timer))
2343 /* clear tempaddr list */
2344 while ((ifa = idev->tempaddr_list) != NULL) {
2345 idev->tempaddr_list = ifa->tmp_next;
2346 ifa->tmp_next = NULL;
2348 write_unlock_bh(&idev->lock);
2349 spin_lock_bh(&ifa->lock);
2352 in6_ifa_put(ifa->ifpub);
2355 spin_unlock_bh(&ifa->lock);
2357 write_lock_bh(&idev->lock);
2360 while ((ifa = idev->addr_list) != NULL) {
2361 idev->addr_list = ifa->if_next;
2362 ifa->if_next = NULL;
2364 addrconf_del_timer(ifa);
2365 write_unlock_bh(&idev->lock);
2367 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2370 write_lock_bh(&idev->lock);
2372 write_unlock_bh(&idev->lock);
2374 /* Step 5: Discard multicast list */
2377 ipv6_mc_destroy_dev(idev);
2381 /* Step 5: netlink notification of this interface */
2382 idev->tstamp = jiffies;
2383 inet6_ifinfo_notify(RTM_DELLINK, idev);
2385 /* Shot the device (if unregistered) */
2388 #ifdef CONFIG_SYSCTL
2389 addrconf_sysctl_unregister(&idev->cnf);
2390 neigh_sysctl_unregister(idev->nd_parms);
2392 neigh_parms_release(&nd_tbl, idev->nd_parms);
2393 neigh_ifdown(&nd_tbl, dev);
2399 static void addrconf_rs_timer(unsigned long data)
2401 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2403 if (ifp->idev->cnf.forwarding)
2406 if (ifp->idev->if_flags & IF_RA_RCVD) {
2408 * Announcement received after solicitation
2414 spin_lock(&ifp->lock);
2415 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2416 struct in6_addr all_routers;
2418 /* The wait after the last probe can be shorter */
2419 addrconf_mod_timer(ifp, AC_RS,
2420 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2421 ifp->idev->cnf.rtr_solicit_delay :
2422 ifp->idev->cnf.rtr_solicit_interval);
2423 spin_unlock(&ifp->lock);
2425 ipv6_addr_all_routers(&all_routers);
2427 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2429 spin_unlock(&ifp->lock);
2431 * Note: we do not support deprecated "all on-link"
2432 * assumption any longer.
2434 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2435 ifp->idev->dev->name);
2443 * Duplicate Address Detection
2445 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2447 unsigned long rand_num;
2448 struct inet6_dev *idev = ifp->idev;
2450 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2451 ifp->probes = idev->cnf.dad_transmits;
2452 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2455 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2457 struct inet6_dev *idev = ifp->idev;
2458 struct net_device *dev = idev->dev;
2460 addrconf_join_solict(dev, &ifp->addr);
2462 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2463 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2466 net_srandom(ifp->addr.s6_addr32[3]);
2468 read_lock_bh(&idev->lock);
2471 spin_lock_bh(&ifp->lock);
2473 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2474 !(ifp->flags&IFA_F_TENTATIVE)) {
2475 ifp->flags &= ~IFA_F_TENTATIVE;
2476 spin_unlock_bh(&ifp->lock);
2477 read_unlock_bh(&idev->lock);
2479 addrconf_dad_completed(ifp);
2483 if (!(idev->if_flags & IF_READY)) {
2484 spin_unlock_bh(&ifp->lock);
2485 read_unlock_bh(&idev->lock);
2487 * If the defice is not ready:
2488 * - keep it tentative if it is a permanent address.
2489 * - otherwise, kill it.
2492 addrconf_dad_stop(ifp);
2495 addrconf_dad_kick(ifp);
2496 spin_unlock_bh(&ifp->lock);
2498 read_unlock_bh(&idev->lock);
2501 static void addrconf_dad_timer(unsigned long data)
2503 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2504 struct inet6_dev *idev = ifp->idev;
2505 struct in6_addr unspec;
2506 struct in6_addr mcaddr;
2508 read_lock_bh(&idev->lock);
2510 read_unlock_bh(&idev->lock);
2513 spin_lock_bh(&ifp->lock);
2514 if (ifp->probes == 0) {
2516 * DAD was successful
2519 ifp->flags &= ~IFA_F_TENTATIVE;
2520 spin_unlock_bh(&ifp->lock);
2521 read_unlock_bh(&idev->lock);
2523 addrconf_dad_completed(ifp);
2529 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2530 spin_unlock_bh(&ifp->lock);
2531 read_unlock_bh(&idev->lock);
2533 /* send a neighbour solicitation for our addr */
2534 memset(&unspec, 0, sizeof(unspec));
2535 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2536 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2541 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2543 struct net_device * dev = ifp->idev->dev;
2546 * Configure the address for reception. Now it is valid.
2549 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2551 /* If added prefix is link local and forwarding is off,
2552 start sending router solicitations.
2555 if (ifp->idev->cnf.forwarding == 0 &&
2556 ifp->idev->cnf.rtr_solicits > 0 &&
2557 (dev->flags&IFF_LOOPBACK) == 0 &&
2558 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2559 struct in6_addr all_routers;
2561 ipv6_addr_all_routers(&all_routers);
2564 * If a host as already performed a random delay
2565 * [...] as part of DAD [...] there is no need
2566 * to delay again before sending the first RS
2568 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2570 spin_lock_bh(&ifp->lock);
2572 ifp->idev->if_flags |= IF_RS_SENT;
2573 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2574 spin_unlock_bh(&ifp->lock);
2578 static void addrconf_dad_run(struct inet6_dev *idev) {
2579 struct inet6_ifaddr *ifp;
2581 read_lock_bh(&idev->lock);
2582 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2583 spin_lock_bh(&ifp->lock);
2584 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2585 spin_unlock_bh(&ifp->lock);
2588 spin_unlock_bh(&ifp->lock);
2589 addrconf_dad_kick(ifp);
2591 read_unlock_bh(&idev->lock);
2594 #ifdef CONFIG_PROC_FS
2595 struct if6_iter_state {
2599 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2601 struct inet6_ifaddr *ifa = NULL;
2602 struct if6_iter_state *state = seq->private;
2604 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2605 ifa = inet6_addr_lst[state->bucket];
2612 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2614 struct if6_iter_state *state = seq->private;
2616 ifa = ifa->lst_next;
2618 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2619 ifa = inet6_addr_lst[state->bucket];
2625 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2627 struct inet6_ifaddr *ifa = if6_get_first(seq);
2630 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2632 return pos ? NULL : ifa;
2635 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2637 read_lock_bh(&addrconf_hash_lock);
2638 return if6_get_idx(seq, *pos);
2641 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2643 struct inet6_ifaddr *ifa;
2645 ifa = if6_get_next(seq, v);
2650 static void if6_seq_stop(struct seq_file *seq, void *v)
2652 read_unlock_bh(&addrconf_hash_lock);
2655 static int if6_seq_show(struct seq_file *seq, void *v)
2657 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2659 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
2661 ifp->idev->dev->ifindex,
2665 ifp->idev->dev->name);
2669 static struct seq_operations if6_seq_ops = {
2670 .start = if6_seq_start,
2671 .next = if6_seq_next,
2672 .show = if6_seq_show,
2673 .stop = if6_seq_stop,
2676 static int if6_seq_open(struct inode *inode, struct file *file)
2678 struct seq_file *seq;
2680 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2684 memset(s, 0, sizeof(*s));
2686 rc = seq_open(file, &if6_seq_ops);
2690 seq = file->private_data;
2699 static struct file_operations if6_fops = {
2700 .owner = THIS_MODULE,
2701 .open = if6_seq_open,
2703 .llseek = seq_lseek,
2704 .release = seq_release_private,
2707 int __init if6_proc_init(void)
2709 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2714 void if6_proc_exit(void)
2716 proc_net_remove("if_inet6");
2718 #endif /* CONFIG_PROC_FS */
2721 * Periodic address status verification
2724 static void addrconf_verify(unsigned long foo)
2726 struct inet6_ifaddr *ifp;
2727 unsigned long now, next;
2730 spin_lock_bh(&addrconf_verify_lock);
2732 next = now + ADDR_CHECK_FREQUENCY;
2734 del_timer(&addr_chk_timer);
2736 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2739 read_lock(&addrconf_hash_lock);
2740 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2742 #ifdef CONFIG_IPV6_PRIVACY
2743 unsigned long regen_advance;
2746 if (ifp->flags & IFA_F_PERMANENT)
2749 spin_lock(&ifp->lock);
2750 age = (now - ifp->tstamp) / HZ;
2752 #ifdef CONFIG_IPV6_PRIVACY
2753 regen_advance = ifp->idev->cnf.regen_max_retry *
2754 ifp->idev->cnf.dad_transmits *
2755 ifp->idev->nd_parms->retrans_time / HZ;
2758 if (age >= ifp->valid_lft) {
2759 spin_unlock(&ifp->lock);
2761 read_unlock(&addrconf_hash_lock);
2764 } else if (age >= ifp->prefered_lft) {
2765 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2768 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2770 ifp->flags |= IFA_F_DEPRECATED;
2773 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2774 next = ifp->tstamp + ifp->valid_lft * HZ;
2776 spin_unlock(&ifp->lock);
2780 read_unlock(&addrconf_hash_lock);
2782 ipv6_ifa_notify(0, ifp);
2786 #ifdef CONFIG_IPV6_PRIVACY
2787 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2788 !(ifp->flags&IFA_F_TENTATIVE)) {
2789 if (age >= ifp->prefered_lft - regen_advance) {
2790 struct inet6_ifaddr *ifpub = ifp->ifpub;
2791 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2792 next = ifp->tstamp + ifp->prefered_lft * HZ;
2793 if (!ifp->regen_count && ifpub) {
2796 in6_ifa_hold(ifpub);
2797 spin_unlock(&ifp->lock);
2798 read_unlock(&addrconf_hash_lock);
2799 spin_lock(&ifpub->lock);
2800 ifpub->regen_count = 0;
2801 spin_unlock(&ifpub->lock);
2802 ipv6_create_tempaddr(ifpub, ifp);
2807 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2808 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2809 spin_unlock(&ifp->lock);
2812 /* ifp->prefered_lft <= ifp->valid_lft */
2813 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2814 next = ifp->tstamp + ifp->prefered_lft * HZ;
2815 spin_unlock(&ifp->lock);
2818 read_unlock(&addrconf_hash_lock);
2821 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2822 add_timer(&addr_chk_timer);
2823 spin_unlock_bh(&addrconf_verify_lock);
2827 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2829 struct rtattr **rta = arg;
2830 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2831 struct in6_addr *pfx;
2834 if (rta[IFA_ADDRESS-1]) {
2835 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2837 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2839 if (rta[IFA_LOCAL-1]) {
2840 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2842 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2847 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2851 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2853 struct rtattr **rta = arg;
2854 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2855 struct in6_addr *pfx;
2858 if (rta[IFA_ADDRESS-1]) {
2859 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2861 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2863 if (rta[IFA_LOCAL-1]) {
2864 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2866 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2871 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2874 /* Maximum length of ifa_cacheinfo attributes */
2875 #define INET6_IFADDR_RTA_SPACE \
2876 RTA_SPACE(16) /* IFA_ADDRESS */ + \
2877 RTA_SPACE(sizeof(struct ifa_cacheinfo)) /* CACHEINFO */
2879 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2880 u32 pid, u32 seq, int event, unsigned int flags)
2882 struct ifaddrmsg *ifm;
2883 struct nlmsghdr *nlh;
2884 struct ifa_cacheinfo ci;
2885 unsigned char *b = skb->tail;
2887 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2888 ifm = NLMSG_DATA(nlh);
2889 ifm->ifa_family = AF_INET6;
2890 ifm->ifa_prefixlen = ifa->prefix_len;
2891 ifm->ifa_flags = ifa->flags;
2892 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2893 if (ifa->scope&IFA_HOST)
2894 ifm->ifa_scope = RT_SCOPE_HOST;
2895 else if (ifa->scope&IFA_LINK)
2896 ifm->ifa_scope = RT_SCOPE_LINK;
2897 else if (ifa->scope&IFA_SITE)
2898 ifm->ifa_scope = RT_SCOPE_SITE;
2899 ifm->ifa_index = ifa->idev->dev->ifindex;
2900 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2901 if (!(ifa->flags&IFA_F_PERMANENT)) {
2902 ci.ifa_prefered = ifa->prefered_lft;
2903 ci.ifa_valid = ifa->valid_lft;
2904 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2905 long tval = (jiffies - ifa->tstamp)/HZ;
2906 ci.ifa_prefered -= tval;
2907 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2908 ci.ifa_valid -= tval;
2911 ci.ifa_prefered = INFINITY_LIFE_TIME;
2912 ci.ifa_valid = INFINITY_LIFE_TIME;
2914 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2915 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2916 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2917 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2918 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2919 nlh->nlmsg_len = skb->tail - b;
2924 skb_trim(skb, b - skb->data);
2928 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2929 u32 pid, u32 seq, int event, u16 flags)
2931 struct ifaddrmsg *ifm;
2932 struct nlmsghdr *nlh;
2933 struct ifa_cacheinfo ci;
2934 unsigned char *b = skb->tail;
2936 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2937 ifm = NLMSG_DATA(nlh);
2938 ifm->ifa_family = AF_INET6;
2939 ifm->ifa_prefixlen = 128;
2940 ifm->ifa_flags = IFA_F_PERMANENT;
2941 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2942 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2943 ifm->ifa_scope = RT_SCOPE_SITE;
2944 ifm->ifa_index = ifmca->idev->dev->ifindex;
2945 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2946 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2947 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2949 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2950 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2952 ci.ifa_prefered = INFINITY_LIFE_TIME;
2953 ci.ifa_valid = INFINITY_LIFE_TIME;
2954 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2955 nlh->nlmsg_len = skb->tail - b;
2960 skb_trim(skb, b - skb->data);
2964 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2965 u32 pid, u32 seq, int event, unsigned int flags)
2967 struct ifaddrmsg *ifm;
2968 struct nlmsghdr *nlh;
2969 struct ifa_cacheinfo ci;
2970 unsigned char *b = skb->tail;
2972 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2973 ifm = NLMSG_DATA(nlh);
2974 ifm->ifa_family = AF_INET6;
2975 ifm->ifa_prefixlen = 128;
2976 ifm->ifa_flags = IFA_F_PERMANENT;
2977 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2978 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2979 ifm->ifa_scope = RT_SCOPE_SITE;
2980 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2981 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2982 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2983 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2985 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2986 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2988 ci.ifa_prefered = INFINITY_LIFE_TIME;
2989 ci.ifa_valid = INFINITY_LIFE_TIME;
2990 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2991 nlh->nlmsg_len = skb->tail - b;
2996 skb_trim(skb, b - skb->data);
3007 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3008 enum addr_type_t type)
3011 int s_idx, s_ip_idx;
3013 struct net_device *dev;
3014 struct inet6_dev *idev = NULL;
3015 struct inet6_ifaddr *ifa;
3016 struct ifmcaddr6 *ifmca;
3017 struct ifacaddr6 *ifaca;
3019 s_idx = cb->args[0];
3020 s_ip_idx = ip_idx = cb->args[1];
3021 read_lock(&dev_base_lock);
3023 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3029 if ((idev = in6_dev_get(dev)) == NULL)
3031 read_lock_bh(&idev->lock);
3034 /* unicast address incl. temp addr */
3035 for (ifa = idev->addr_list; ifa;
3036 ifa = ifa->if_next, ip_idx++) {
3037 if (ip_idx < s_ip_idx)
3039 if ((err = inet6_fill_ifaddr(skb, ifa,
3040 NETLINK_CB(cb->skb).pid,
3041 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3046 case MULTICAST_ADDR:
3047 /* multicast address */
3048 for (ifmca = idev->mc_list; ifmca;
3049 ifmca = ifmca->next, ip_idx++) {
3050 if (ip_idx < s_ip_idx)
3052 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3053 NETLINK_CB(cb->skb).pid,
3054 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3060 /* anycast address */
3061 for (ifaca = idev->ac_list; ifaca;
3062 ifaca = ifaca->aca_next, ip_idx++) {
3063 if (ip_idx < s_ip_idx)
3065 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3066 NETLINK_CB(cb->skb).pid,
3067 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3075 read_unlock_bh(&idev->lock);
3080 read_unlock_bh(&idev->lock);
3083 read_unlock(&dev_base_lock);
3085 cb->args[1] = ip_idx;
3089 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3091 enum addr_type_t type = UNICAST_ADDR;
3092 return inet6_dump_addr(skb, cb, type);
3095 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3097 enum addr_type_t type = MULTICAST_ADDR;
3098 return inet6_dump_addr(skb, cb, type);
3102 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3104 enum addr_type_t type = ANYCAST_ADDR;
3105 return inet6_dump_addr(skb, cb, type);
3108 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3110 struct sk_buff *skb;
3111 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + INET6_IFADDR_RTA_SPACE);
3113 skb = alloc_skb(size, GFP_ATOMIC);
3115 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
3118 if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
3120 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
3123 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
3124 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
3127 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3128 __s32 *array, int bytes)
3130 memset(array, 0, bytes);
3131 array[DEVCONF_FORWARDING] = cnf->forwarding;
3132 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3133 array[DEVCONF_MTU6] = cnf->mtu6;
3134 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3135 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3136 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3137 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3138 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3139 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3140 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3141 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3142 #ifdef CONFIG_IPV6_PRIVACY
3143 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3144 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3145 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3146 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3147 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3149 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3152 /* Maximum length of ifinfomsg attributes */
3153 #define INET6_IFINFO_RTA_SPACE \
3154 RTA_SPACE(IFNAMSIZ) /* IFNAME */ + \
3155 RTA_SPACE(MAX_ADDR_LEN) /* ADDRESS */ + \
3156 RTA_SPACE(sizeof(u32)) /* MTU */ + \
3157 RTA_SPACE(sizeof(int)) /* LINK */ + \
3158 RTA_SPACE(0) /* PROTINFO */ + \
3159 RTA_SPACE(sizeof(u32)) /* FLAGS */ + \
3160 RTA_SPACE(sizeof(struct ifla_cacheinfo)) /* CACHEINFO */ + \
3161 RTA_SPACE(sizeof(__s32[DEVCONF_MAX])) /* CONF */
3163 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3164 u32 pid, u32 seq, int event, unsigned int flags)
3166 struct net_device *dev = idev->dev;
3167 __s32 *array = NULL;
3168 struct ifinfomsg *r;
3169 struct nlmsghdr *nlh;
3170 unsigned char *b = skb->tail;
3171 struct rtattr *subattr;
3172 __u32 mtu = dev->mtu;
3173 struct ifla_cacheinfo ci;
3175 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
3176 r = NLMSG_DATA(nlh);
3177 r->ifi_family = AF_INET6;
3179 r->ifi_type = dev->type;
3180 r->ifi_index = dev->ifindex;
3181 r->ifi_flags = dev_get_flags(dev);
3184 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
3187 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3189 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
3190 if (dev->ifindex != dev->iflink)
3191 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
3193 subattr = (struct rtattr*)skb->tail;
3195 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
3197 /* return the device flags */
3198 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
3200 /* return interface cacheinfo */
3201 ci.max_reasm_len = IPV6_MAXPLEN;
3202 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3203 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3204 ci.reachable_time = idev->nd_parms->reachable_time;
3205 ci.retrans_time = idev->nd_parms->retrans_time;
3206 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3208 /* return the device sysctl params */
3209 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
3210 goto rtattr_failure;
3211 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
3212 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
3214 /* XXX - Statistics/MC not implemented */
3215 subattr->rta_len = skb->tail - (u8*)subattr;
3217 nlh->nlmsg_len = skb->tail - b;
3224 skb_trim(skb, b - skb->data);
3228 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3231 int s_idx = cb->args[0];
3232 struct net_device *dev;
3233 struct inet6_dev *idev;
3235 read_lock(&dev_base_lock);
3236 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3239 if ((idev = in6_dev_get(dev)) == NULL)
3241 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3242 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3247 read_unlock(&dev_base_lock);
3253 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3255 struct sk_buff *skb;
3256 int size = NLMSG_SPACE(sizeof(struct ifinfomsg) + INET6_IFINFO_RTA_SPACE);
3258 skb = alloc_skb(size, GFP_ATOMIC);
3260 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
3263 if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
3265 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
3268 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
3269 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
3272 /* Maximum length of prefix_cacheinfo attributes */
3273 #define INET6_PREFIX_RTA_SPACE \
3274 RTA_SPACE(sizeof(((struct prefix_info *)NULL)->prefix)) /* ADDRESS */ + \
3275 RTA_SPACE(sizeof(struct prefix_cacheinfo)) /* CACHEINFO */
3277 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3278 struct prefix_info *pinfo, u32 pid, u32 seq,
3279 int event, unsigned int flags)
3281 struct prefixmsg *pmsg;
3282 struct nlmsghdr *nlh;
3283 unsigned char *b = skb->tail;
3284 struct prefix_cacheinfo ci;
3286 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
3287 pmsg = NLMSG_DATA(nlh);
3288 pmsg->prefix_family = AF_INET6;
3289 pmsg->prefix_pad1 = 0;
3290 pmsg->prefix_pad2 = 0;
3291 pmsg->prefix_ifindex = idev->dev->ifindex;
3292 pmsg->prefix_len = pinfo->prefix_len;
3293 pmsg->prefix_type = pinfo->type;
3294 pmsg->prefix_pad3 = 0;
3296 pmsg->prefix_flags = 0;
3298 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3299 if (pinfo->autoconf)
3300 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3302 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3304 ci.preferred_time = ntohl(pinfo->prefered);
3305 ci.valid_time = ntohl(pinfo->valid);
3306 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3308 nlh->nlmsg_len = skb->tail - b;
3313 skb_trim(skb, b - skb->data);
3317 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3318 struct prefix_info *pinfo)
3320 struct sk_buff *skb;
3321 int size = NLMSG_SPACE(sizeof(struct prefixmsg) + INET6_PREFIX_RTA_SPACE);
3323 skb = alloc_skb(size, GFP_ATOMIC);
3325 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
3328 if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
3330 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
3333 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
3334 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
3337 static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
3338 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3339 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3340 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
3341 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
3342 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3343 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3344 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3345 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3346 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3347 .dumpit = inet6_dump_fib, },
3350 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3352 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3356 ip6_ins_rt(ifp->rt, NULL, NULL, NULL);
3357 if (ifp->idev->cnf.forwarding)
3358 addrconf_join_anycast(ifp);
3361 if (ifp->idev->cnf.forwarding)
3362 addrconf_leave_anycast(ifp);
3363 addrconf_leave_solict(ifp->idev, &ifp->addr);
3364 dst_hold(&ifp->rt->u.dst);
3365 if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
3366 dst_free(&ifp->rt->u.dst);
3371 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3373 read_lock_bh(&addrconf_lock);
3374 if (likely(ifp->idev->dead == 0))
3375 __ipv6_ifa_notify(event, ifp);
3376 read_unlock_bh(&addrconf_lock);
3379 #ifdef CONFIG_SYSCTL
3382 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3383 void __user *buffer, size_t *lenp, loff_t *ppos)
3385 int *valp = ctl->data;
3389 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3391 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3392 if (valp != &ipv6_devconf.forwarding) {
3393 if ((!*valp) ^ (!val)) {
3394 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3397 dev_forward_change(idev);
3400 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3401 addrconf_forward_change();
3404 rt6_purge_dflt_routers();
3410 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3411 int __user *name, int nlen,
3412 void __user *oldval,
3413 size_t __user *oldlenp,
3414 void __user *newval, size_t newlen,
3417 int *valp = table->data;
3420 if (!newval || !newlen)
3422 if (newlen != sizeof(int))
3424 if (get_user(new, (int __user *)newval))
3428 if (oldval && oldlenp) {
3430 if (get_user(len, oldlenp))
3433 if (len > table->maxlen)
3434 len = table->maxlen;
3435 if (copy_to_user(oldval, valp, len))
3437 if (put_user(len, oldlenp))
3442 if (valp != &ipv6_devconf_dflt.forwarding) {
3443 if (valp != &ipv6_devconf.forwarding) {
3444 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3446 if (unlikely(idev == NULL))
3448 changed = (!*valp) ^ (!new);
3451 dev_forward_change(idev);
3454 addrconf_forward_change();
3458 rt6_purge_dflt_routers();
3465 static struct addrconf_sysctl_table
3467 struct ctl_table_header *sysctl_header;
3468 ctl_table addrconf_vars[__NET_IPV6_MAX];
3469 ctl_table addrconf_dev[2];
3470 ctl_table addrconf_conf_dir[2];
3471 ctl_table addrconf_proto_dir[2];
3472 ctl_table addrconf_root_dir[2];
3473 } addrconf_sysctl = {
3474 .sysctl_header = NULL,
3477 .ctl_name = NET_IPV6_FORWARDING,
3478 .procname = "forwarding",
3479 .data = &ipv6_devconf.forwarding,
3480 .maxlen = sizeof(int),
3482 .proc_handler = &addrconf_sysctl_forward,
3483 .strategy = &addrconf_sysctl_forward_strategy,
3486 .ctl_name = NET_IPV6_HOP_LIMIT,
3487 .procname = "hop_limit",
3488 .data = &ipv6_devconf.hop_limit,
3489 .maxlen = sizeof(int),
3491 .proc_handler = proc_dointvec,
3494 .ctl_name = NET_IPV6_MTU,
3496 .data = &ipv6_devconf.mtu6,
3497 .maxlen = sizeof(int),
3499 .proc_handler = &proc_dointvec,
3502 .ctl_name = NET_IPV6_ACCEPT_RA,
3503 .procname = "accept_ra",
3504 .data = &ipv6_devconf.accept_ra,
3505 .maxlen = sizeof(int),
3507 .proc_handler = &proc_dointvec,
3510 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3511 .procname = "accept_redirects",
3512 .data = &ipv6_devconf.accept_redirects,
3513 .maxlen = sizeof(int),
3515 .proc_handler = &proc_dointvec,
3518 .ctl_name = NET_IPV6_AUTOCONF,
3519 .procname = "autoconf",
3520 .data = &ipv6_devconf.autoconf,
3521 .maxlen = sizeof(int),
3523 .proc_handler = &proc_dointvec,
3526 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3527 .procname = "dad_transmits",
3528 .data = &ipv6_devconf.dad_transmits,
3529 .maxlen = sizeof(int),
3531 .proc_handler = &proc_dointvec,
3534 .ctl_name = NET_IPV6_RTR_SOLICITS,
3535 .procname = "router_solicitations",
3536 .data = &ipv6_devconf.rtr_solicits,
3537 .maxlen = sizeof(int),
3539 .proc_handler = &proc_dointvec,
3542 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3543 .procname = "router_solicitation_interval",
3544 .data = &ipv6_devconf.rtr_solicit_interval,
3545 .maxlen = sizeof(int),
3547 .proc_handler = &proc_dointvec_jiffies,
3548 .strategy = &sysctl_jiffies,
3551 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3552 .procname = "router_solicitation_delay",
3553 .data = &ipv6_devconf.rtr_solicit_delay,
3554 .maxlen = sizeof(int),
3556 .proc_handler = &proc_dointvec_jiffies,
3557 .strategy = &sysctl_jiffies,
3560 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3561 .procname = "force_mld_version",
3562 .data = &ipv6_devconf.force_mld_version,
3563 .maxlen = sizeof(int),
3565 .proc_handler = &proc_dointvec,
3567 #ifdef CONFIG_IPV6_PRIVACY
3569 .ctl_name = NET_IPV6_USE_TEMPADDR,
3570 .procname = "use_tempaddr",
3571 .data = &ipv6_devconf.use_tempaddr,
3572 .maxlen = sizeof(int),
3574 .proc_handler = &proc_dointvec,
3577 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3578 .procname = "temp_valid_lft",
3579 .data = &ipv6_devconf.temp_valid_lft,
3580 .maxlen = sizeof(int),
3582 .proc_handler = &proc_dointvec,
3585 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3586 .procname = "temp_prefered_lft",
3587 .data = &ipv6_devconf.temp_prefered_lft,
3588 .maxlen = sizeof(int),
3590 .proc_handler = &proc_dointvec,
3593 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3594 .procname = "regen_max_retry",
3595 .data = &ipv6_devconf.regen_max_retry,
3596 .maxlen = sizeof(int),
3598 .proc_handler = &proc_dointvec,
3601 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3602 .procname = "max_desync_factor",
3603 .data = &ipv6_devconf.max_desync_factor,
3604 .maxlen = sizeof(int),
3606 .proc_handler = &proc_dointvec,
3610 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3611 .procname = "max_addresses",
3612 .data = &ipv6_devconf.max_addresses,
3613 .maxlen = sizeof(int),
3615 .proc_handler = &proc_dointvec,
3618 .ctl_name = 0, /* sentinel */
3623 .ctl_name = NET_PROTO_CONF_ALL,
3626 .child = addrconf_sysctl.addrconf_vars,
3629 .ctl_name = 0, /* sentinel */
3632 .addrconf_conf_dir = {
3634 .ctl_name = NET_IPV6_CONF,
3637 .child = addrconf_sysctl.addrconf_dev,
3640 .ctl_name = 0, /* sentinel */
3643 .addrconf_proto_dir = {
3645 .ctl_name = NET_IPV6,
3648 .child = addrconf_sysctl.addrconf_conf_dir,
3651 .ctl_name = 0, /* sentinel */
3654 .addrconf_root_dir = {
3656 .ctl_name = CTL_NET,
3659 .child = addrconf_sysctl.addrconf_proto_dir,
3662 .ctl_name = 0, /* sentinel */
3667 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3670 struct net_device *dev = idev ? idev->dev : NULL;
3671 struct addrconf_sysctl_table *t;
3672 char *dev_name = NULL;
3674 t = kmalloc(sizeof(*t), GFP_KERNEL);
3677 memcpy(t, &addrconf_sysctl, sizeof(*t));
3678 for (i=0; t->addrconf_vars[i].data; i++) {
3679 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3680 t->addrconf_vars[i].de = NULL;
3681 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3684 dev_name = dev->name;
3685 t->addrconf_dev[0].ctl_name = dev->ifindex;
3687 dev_name = "default";
3688 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3692 * Make a copy of dev_name, because '.procname' is regarded as const
3693 * by sysctl and we wouldn't want anyone to change it under our feet
3694 * (see SIOCSIFNAME).
3696 dev_name = kstrdup(dev_name, GFP_KERNEL);
3700 t->addrconf_dev[0].procname = dev_name;
3702 t->addrconf_dev[0].child = t->addrconf_vars;
3703 t->addrconf_dev[0].de = NULL;
3704 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3705 t->addrconf_conf_dir[0].de = NULL;
3706 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3707 t->addrconf_proto_dir[0].de = NULL;
3708 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3709 t->addrconf_root_dir[0].de = NULL;
3711 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3712 if (t->sysctl_header == NULL)
3727 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3730 struct addrconf_sysctl_table *t = p->sysctl;
3732 unregister_sysctl_table(t->sysctl_header);
3733 kfree(t->addrconf_dev[0].procname);
3745 int register_inet6addr_notifier(struct notifier_block *nb)
3747 return notifier_chain_register(&inet6addr_chain, nb);
3750 int unregister_inet6addr_notifier(struct notifier_block *nb)
3752 return notifier_chain_unregister(&inet6addr_chain,nb);
3756 * Init / cleanup code
3759 int __init addrconf_init(void)
3763 /* The addrconf netdev notifier requires that loopback_dev
3764 * has it's ipv6 private information allocated and setup
3765 * before it can bring up and give link-local addresses
3766 * to other devices which are up.
3768 * Unfortunately, loopback_dev is not necessarily the first
3769 * entry in the global dev_base list of net devices. In fact,
3770 * it is likely to be the very last entry on that list.
3771 * So this causes the notifier registry below to try and
3772 * give link-local addresses to all devices besides loopback_dev
3773 * first, then loopback_dev, which cases all the non-loopback_dev
3774 * devices to fail to get a link-local address.
3776 * So, as a temporary fix, allocate the ipv6 structure for
3777 * loopback_dev first by hand.
3778 * Longer term, all of the dependencies ipv6 has upon the loopback
3779 * device and it being up should be removed.
3782 if (!ipv6_add_dev(&loopback_dev))
3788 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
3790 register_netdevice_notifier(&ipv6_dev_notf);
3792 #ifdef CONFIG_IPV6_PRIVACY
3793 md5_tfm = crypto_alloc_tfm("md5", 0);
3794 if (unlikely(md5_tfm == NULL))
3796 "failed to load transform for md5\n");
3800 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3801 #ifdef CONFIG_SYSCTL
3802 addrconf_sysctl.sysctl_header =
3803 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3804 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3810 void __exit addrconf_cleanup(void)
3812 struct net_device *dev;
3813 struct inet6_dev *idev;
3814 struct inet6_ifaddr *ifa;
3817 unregister_netdevice_notifier(&ipv6_dev_notf);
3819 rtnetlink_links[PF_INET6] = NULL;
3820 #ifdef CONFIG_SYSCTL
3821 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3822 addrconf_sysctl_unregister(&ipv6_devconf);
3831 for (dev=dev_base; dev; dev=dev->next) {
3832 if ((idev = __in6_dev_get(dev)) == NULL)
3834 addrconf_ifdown(dev, 1);
3836 addrconf_ifdown(&loopback_dev, 2);
3842 write_lock_bh(&addrconf_hash_lock);
3843 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3844 for (ifa=inet6_addr_lst[i]; ifa; ) {
3845 struct inet6_ifaddr *bifa;
3848 ifa = ifa->lst_next;
3849 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3850 /* Do not free it; something is wrong.
3851 Now we can investigate it with debugger.
3855 write_unlock_bh(&addrconf_hash_lock);
3857 del_timer(&addr_chk_timer);
3861 #ifdef CONFIG_IPV6_PRIVACY
3862 crypto_free_tfm(md5_tfm);
3866 #ifdef CONFIG_PROC_FS
3867 proc_net_remove("if_inet6");