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 write_lock_bh(&addrconf_lock);
398 write_unlock_bh(&addrconf_lock);
400 ipv6_mc_init_dev(ndev);
401 ndev->tstamp = jiffies;
403 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
404 NET_IPV6_NEIGH, "ipv6",
405 &ndisc_ifinfo_sysctl_change,
407 addrconf_sysctl_register(ndev, &ndev->cnf);
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 * -----------------------------
856 if (type & IPV6_ADDR_LOOPBACK)
858 else if (type & IPV6_ADDR_COMPATv4)
860 else if (type & IPV6_ADDR_MAPPED)
862 else if (addr->s6_addr16[0] == htons(0x2002))
867 int ipv6_dev_get_saddr(struct net_device *daddr_dev,
868 struct in6_addr *daddr, struct in6_addr *saddr)
870 struct ipv6_saddr_score hiscore;
871 struct inet6_ifaddr *ifa_result = NULL;
872 int daddr_type = __ipv6_addr_type(daddr);
873 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
874 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
875 struct net_device *dev;
877 memset(&hiscore, 0, sizeof(hiscore));
879 read_lock(&dev_base_lock);
880 read_lock(&addrconf_lock);
882 for (dev = dev_base; dev; dev=dev->next) {
883 struct inet6_dev *idev;
884 struct inet6_ifaddr *ifa;
886 /* Rule 0: Candidate Source Address (section 4)
887 * - multicast and link-local destination address,
888 * the set of candidate source address MUST only
889 * include addresses assigned to interfaces
890 * belonging to the same link as the outgoing
892 * (- For site-local destination addresses, the
893 * set of candidate source addresses MUST only
894 * include addresses assigned to interfaces
895 * belonging to the same site as the outgoing
898 if ((daddr_type & IPV6_ADDR_MULTICAST ||
899 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
900 daddr_dev && dev != daddr_dev)
903 idev = __in6_dev_get(dev);
907 read_lock_bh(&idev->lock);
908 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
909 struct ipv6_saddr_score score;
911 score.addr_type = __ipv6_addr_type(&ifa->addr);
914 * - Tentative Address (RFC2462 section 5.4)
915 * - A tentative address is not considered
916 * "assigned to an interface" in the traditional
918 * - Candidate Source Address (section 4)
919 * - In any case, anycast addresses, multicast
920 * addresses, and the unspecified address MUST
921 * NOT be included in a candidate set.
923 if (ifa->flags & IFA_F_TENTATIVE)
925 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
926 score.addr_type & IPV6_ADDR_MULTICAST)) {
927 LIMIT_NETDEBUG(KERN_DEBUG
928 "ADDRCONF: unspecified / multicast address"
929 "assigned as unicast address on %s",
939 if (ifa_result == NULL) {
940 /* record it if the first available entry */
944 /* Rule 1: Prefer same address */
945 if (hiscore.rule < 1) {
946 if (ipv6_addr_equal(&ifa_result->addr, daddr))
947 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
950 if (ipv6_addr_equal(&ifa->addr, daddr)) {
951 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
952 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
957 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
961 /* Rule 2: Prefer appropriate scope */
962 if (hiscore.rule < 2) {
963 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
966 score.scope = __ipv6_addr_src_scope(score.addr_type);
967 if (hiscore.scope < score.scope) {
968 if (hiscore.scope < daddr_scope) {
973 } else if (score.scope < hiscore.scope) {
974 if (score.scope < daddr_scope)
982 /* Rule 3: Avoid deprecated address */
983 if (hiscore.rule < 3) {
984 if (ipv6_saddr_preferred(hiscore.addr_type) ||
985 !(ifa_result->flags & IFA_F_DEPRECATED))
986 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
989 if (ipv6_saddr_preferred(score.addr_type) ||
990 !(ifa->flags & IFA_F_DEPRECATED)) {
991 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
992 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
997 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1001 /* Rule 4: Prefer home address -- not implemented yet */
1002 if (hiscore.rule < 4)
1005 /* Rule 5: Prefer outgoing interface */
1006 if (hiscore.rule < 5) {
1007 if (daddr_dev == NULL ||
1008 daddr_dev == ifa_result->idev->dev)
1009 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1012 if (daddr_dev == NULL ||
1013 daddr_dev == ifa->idev->dev) {
1014 score.attrs |= IPV6_SADDR_SCORE_OIF;
1015 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1020 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1024 /* Rule 6: Prefer matching label */
1025 if (hiscore.rule < 6) {
1026 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1027 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1030 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1031 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1032 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1037 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1041 #ifdef CONFIG_IPV6_PRIVACY
1042 /* Rule 7: Prefer public address
1043 * Note: prefer temprary address if use_tempaddr >= 2
1045 if (hiscore.rule < 7) {
1046 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1047 (ifa_result->idev->cnf.use_tempaddr >= 2))
1048 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1051 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1052 (ifa->idev->cnf.use_tempaddr >= 2)) {
1053 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1054 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1059 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1063 /* Rule 8: Use longest matching prefix */
1064 if (hiscore.rule < 8) {
1065 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
1068 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1069 if (score.matchlen > hiscore.matchlen) {
1074 else if (score.matchlen < hiscore.matchlen)
1078 /* Final Rule: choose first available one */
1082 in6_ifa_put(ifa_result);
1087 read_unlock_bh(&idev->lock);
1089 read_unlock(&addrconf_lock);
1090 read_unlock(&dev_base_lock);
1093 return -EADDRNOTAVAIL;
1095 ipv6_addr_copy(saddr, &ifa_result->addr);
1096 in6_ifa_put(ifa_result);
1101 int ipv6_get_saddr(struct dst_entry *dst,
1102 struct in6_addr *daddr, struct in6_addr *saddr)
1104 return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
1108 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1110 struct inet6_dev *idev;
1111 int err = -EADDRNOTAVAIL;
1113 read_lock(&addrconf_lock);
1114 if ((idev = __in6_dev_get(dev)) != NULL) {
1115 struct inet6_ifaddr *ifp;
1117 read_lock_bh(&idev->lock);
1118 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1119 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1120 ipv6_addr_copy(addr, &ifp->addr);
1125 read_unlock_bh(&idev->lock);
1127 read_unlock(&addrconf_lock);
1131 static int ipv6_count_addresses(struct inet6_dev *idev)
1134 struct inet6_ifaddr *ifp;
1136 read_lock_bh(&idev->lock);
1137 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1139 read_unlock_bh(&idev->lock);
1143 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1145 struct inet6_ifaddr * ifp;
1146 u8 hash = ipv6_addr_hash(addr);
1148 read_lock_bh(&addrconf_hash_lock);
1149 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1150 if (ipv6_addr_equal(&ifp->addr, addr) &&
1151 !(ifp->flags&IFA_F_TENTATIVE)) {
1152 if (dev == NULL || ifp->idev->dev == dev ||
1153 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1157 read_unlock_bh(&addrconf_hash_lock);
1162 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1164 struct inet6_ifaddr * ifp;
1165 u8 hash = ipv6_addr_hash(addr);
1167 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1168 if (ipv6_addr_equal(&ifp->addr, addr)) {
1169 if (dev == NULL || ifp->idev->dev == dev)
1176 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1178 struct inet6_ifaddr * ifp;
1179 u8 hash = ipv6_addr_hash(addr);
1181 read_lock_bh(&addrconf_hash_lock);
1182 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1183 if (ipv6_addr_equal(&ifp->addr, addr)) {
1184 if (dev == NULL || ifp->idev->dev == dev ||
1185 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1191 read_unlock_bh(&addrconf_hash_lock);
1196 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1198 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1199 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
1200 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1201 u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1202 int sk_ipv6only = ipv6_only_sock(sk);
1203 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1204 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1205 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1207 if (!sk2_rcv_saddr && !sk_ipv6only)
1210 if (addr_type2 == IPV6_ADDR_ANY &&
1211 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1214 if (addr_type == IPV6_ADDR_ANY &&
1215 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1218 if (sk2_rcv_saddr6 &&
1219 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1222 if (addr_type == IPV6_ADDR_MAPPED &&
1224 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1230 /* Gets referenced address, destroys ifaddr */
1232 static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1234 if (ifp->flags&IFA_F_PERMANENT) {
1235 spin_lock_bh(&ifp->lock);
1236 addrconf_del_timer(ifp);
1237 ifp->flags |= IFA_F_TENTATIVE;
1238 spin_unlock_bh(&ifp->lock);
1240 #ifdef CONFIG_IPV6_PRIVACY
1241 } else if (ifp->flags&IFA_F_TEMPORARY) {
1242 struct inet6_ifaddr *ifpub;
1243 spin_lock_bh(&ifp->lock);
1246 in6_ifa_hold(ifpub);
1247 spin_unlock_bh(&ifp->lock);
1248 ipv6_create_tempaddr(ifpub, ifp);
1251 spin_unlock_bh(&ifp->lock);
1259 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1261 if (net_ratelimit())
1262 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1263 addrconf_dad_stop(ifp);
1266 /* Join to solicited addr multicast group. */
1268 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1270 struct in6_addr maddr;
1272 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1275 addrconf_addr_solict_mult(addr, &maddr);
1276 ipv6_dev_mc_inc(dev, &maddr);
1279 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1281 struct in6_addr maddr;
1283 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1286 addrconf_addr_solict_mult(addr, &maddr);
1287 __ipv6_dev_mc_dec(idev, &maddr);
1290 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1292 struct in6_addr addr;
1293 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1294 if (ipv6_addr_any(&addr))
1296 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1299 static void addrconf_leave_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_dec(ifp->idev, &addr);
1308 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1310 switch (dev->type) {
1313 case ARPHRD_IEEE802_TR:
1314 if (dev->addr_len != ETH_ALEN)
1316 memcpy(eui, dev->dev_addr, 3);
1317 memcpy(eui + 5, dev->dev_addr + 3, 3);
1320 * The zSeries OSA network cards can be shared among various
1321 * OS instances, but the OSA cards have only one MAC address.
1322 * This leads to duplicate address conflicts in conjunction
1323 * with IPv6 if more than one instance uses the same card.
1325 * The driver for these cards can deliver a unique 16-bit
1326 * identifier for each instance sharing the same card. It is
1327 * placed instead of 0xFFFE in the interface identifier. The
1328 * "u" bit of the interface identifier is not inverted in this
1329 * case. Hence the resulting interface identifier has local
1330 * scope according to RFC2373.
1333 eui[3] = (dev->dev_id >> 8) & 0xFF;
1334 eui[4] = dev->dev_id & 0xFF;
1342 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1343 if (dev->addr_len != ARCNET_ALEN)
1346 eui[7] = *(u8*)dev->dev_addr;
1348 case ARPHRD_INFINIBAND:
1349 if (dev->addr_len != INFINIBAND_ALEN)
1351 memcpy(eui, dev->dev_addr + 12, 8);
1358 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1361 struct inet6_ifaddr *ifp;
1363 read_lock_bh(&idev->lock);
1364 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1365 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1366 memcpy(eui, ifp->addr.s6_addr+8, 8);
1371 read_unlock_bh(&idev->lock);
1375 #ifdef CONFIG_IPV6_PRIVACY
1376 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1377 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1379 struct net_device *dev;
1380 struct scatterlist sg[2];
1382 sg_set_buf(&sg[0], idev->entropy, 8);
1383 sg_set_buf(&sg[1], idev->work_eui64, 8);
1387 if (ipv6_generate_eui64(idev->work_eui64, dev)) {
1389 "__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
1391 get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
1394 spin_lock(&md5_tfm_lock);
1395 if (unlikely(md5_tfm == NULL)) {
1396 spin_unlock(&md5_tfm_lock);
1399 crypto_digest_init(md5_tfm);
1400 crypto_digest_update(md5_tfm, sg, 2);
1401 crypto_digest_final(md5_tfm, idev->work_digest);
1402 spin_unlock(&md5_tfm_lock);
1404 memcpy(idev->rndid, &idev->work_digest[0], 8);
1405 idev->rndid[0] &= ~0x02;
1406 memcpy(idev->entropy, &idev->work_digest[8], 8);
1409 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1410 * check if generated address is not inappropriate
1412 * - Reserved subnet anycast (RFC 2526)
1413 * 11111101 11....11 1xxxxxxx
1414 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1415 * 00-00-5E-FE-xx-xx-xx-xx
1417 * - XXX: already assigned to an address on the device
1419 if (idev->rndid[0] == 0xfd &&
1420 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1421 (idev->rndid[7]&0x80))
1423 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1424 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1426 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1433 static void ipv6_regen_rndid(unsigned long data)
1435 struct inet6_dev *idev = (struct inet6_dev *) data;
1436 unsigned long expires;
1438 read_lock_bh(&addrconf_lock);
1439 write_lock_bh(&idev->lock);
1444 if (__ipv6_regen_rndid(idev) < 0)
1448 idev->cnf.temp_prefered_lft * HZ -
1449 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1450 if (time_before(expires, jiffies)) {
1452 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1457 if (!mod_timer(&idev->regen_timer, expires))
1461 write_unlock_bh(&idev->lock);
1462 read_unlock_bh(&addrconf_lock);
1466 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1469 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1470 ret = __ipv6_regen_rndid(idev);
1480 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1481 unsigned long expires, u32 flags)
1483 struct in6_rtmsg rtmsg;
1485 memset(&rtmsg, 0, sizeof(rtmsg));
1486 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1487 rtmsg.rtmsg_dst_len = plen;
1488 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1489 rtmsg.rtmsg_ifindex = dev->ifindex;
1490 rtmsg.rtmsg_info = expires;
1491 rtmsg.rtmsg_flags = RTF_UP|flags;
1492 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1494 /* Prevent useless cloning on PtP SIT.
1495 This thing is done here expecting that the whole
1496 class of non-broadcast devices need not cloning.
1498 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1499 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1501 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1504 /* Create "default" multicast route to the interface */
1506 static void addrconf_add_mroute(struct net_device *dev)
1508 struct in6_rtmsg rtmsg;
1510 memset(&rtmsg, 0, sizeof(rtmsg));
1511 ipv6_addr_set(&rtmsg.rtmsg_dst,
1512 htonl(0xFF000000), 0, 0, 0);
1513 rtmsg.rtmsg_dst_len = 8;
1514 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1515 rtmsg.rtmsg_ifindex = dev->ifindex;
1516 rtmsg.rtmsg_flags = RTF_UP;
1517 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1518 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1521 static void sit_route_add(struct net_device *dev)
1523 struct in6_rtmsg rtmsg;
1525 memset(&rtmsg, 0, sizeof(rtmsg));
1527 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1528 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1530 /* prefix length - 96 bits "::d.d.d.d" */
1531 rtmsg.rtmsg_dst_len = 96;
1532 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1533 rtmsg.rtmsg_ifindex = dev->ifindex;
1535 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1538 static void addrconf_add_lroute(struct net_device *dev)
1540 struct in6_addr addr;
1542 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1543 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1546 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1548 struct inet6_dev *idev;
1552 if ((idev = ipv6_find_idev(dev)) == NULL)
1555 /* Add default multicast route */
1556 addrconf_add_mroute(dev);
1558 /* Add link local route */
1559 addrconf_add_lroute(dev);
1563 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1565 struct prefix_info *pinfo;
1569 unsigned long rt_expires;
1570 struct inet6_dev *in6_dev;
1572 pinfo = (struct prefix_info *) opt;
1574 if (len < sizeof(struct prefix_info)) {
1575 ADBG(("addrconf: prefix option too short\n"));
1580 * Validation checks ([ADDRCONF], page 19)
1583 addr_type = ipv6_addr_type(&pinfo->prefix);
1585 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1588 valid_lft = ntohl(pinfo->valid);
1589 prefered_lft = ntohl(pinfo->prefered);
1591 if (prefered_lft > valid_lft) {
1592 if (net_ratelimit())
1593 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1597 in6_dev = in6_dev_get(dev);
1599 if (in6_dev == NULL) {
1600 if (net_ratelimit())
1601 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1606 * Two things going on here:
1607 * 1) Add routes for on-link prefixes
1608 * 2) Configure prefixes with the auto flag set
1611 /* Avoid arithmetic overflow. Really, we could
1612 save rt_expires in seconds, likely valid_lft,
1613 but it would require division in fib gc, that it
1616 if (valid_lft >= 0x7FFFFFFF/HZ)
1617 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1619 rt_expires = valid_lft * HZ;
1622 * We convert this (in jiffies) to clock_t later.
1623 * Avoid arithmetic overflow there as well.
1624 * Overflow can happen only if HZ < USER_HZ.
1626 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1627 rt_expires = 0x7FFFFFFF / USER_HZ;
1629 if (pinfo->onlink) {
1630 struct rt6_info *rt;
1631 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1633 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1634 if (rt->rt6i_flags&RTF_EXPIRES) {
1635 if (valid_lft == 0) {
1636 ip6_del_rt(rt, NULL, NULL, NULL);
1639 rt->rt6i_expires = jiffies + rt_expires;
1642 } else if (valid_lft) {
1643 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1644 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1647 dst_release(&rt->u.dst);
1650 /* Try to figure out our local address for this prefix */
1652 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1653 struct inet6_ifaddr * ifp;
1654 struct in6_addr addr;
1655 int create = 0, update_lft = 0;
1657 if (pinfo->prefix_len == 64) {
1658 memcpy(&addr, &pinfo->prefix, 8);
1659 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1660 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1661 in6_dev_put(in6_dev);
1666 if (net_ratelimit())
1667 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1669 in6_dev_put(in6_dev);
1674 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1676 if (ifp == NULL && valid_lft) {
1677 int max_addresses = in6_dev->cnf.max_addresses;
1679 /* Do not allow to create too much of autoconfigured
1680 * addresses; this would be too easy way to crash kernel.
1682 if (!max_addresses ||
1683 ipv6_count_addresses(in6_dev) < max_addresses)
1684 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1685 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1687 if (!ifp || IS_ERR(ifp)) {
1688 in6_dev_put(in6_dev);
1692 update_lft = create = 1;
1693 ifp->cstamp = jiffies;
1694 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1700 #ifdef CONFIG_IPV6_PRIVACY
1701 struct inet6_ifaddr *ift;
1705 /* update lifetime (RFC2462 5.5.3 e) */
1706 spin_lock(&ifp->lock);
1708 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1709 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1712 if (!update_lft && stored_lft) {
1713 if (valid_lft > MIN_VALID_LIFETIME ||
1714 valid_lft > stored_lft)
1716 else if (stored_lft <= MIN_VALID_LIFETIME) {
1717 /* valid_lft <= stored_lft is always true */
1721 valid_lft = MIN_VALID_LIFETIME;
1722 if (valid_lft < prefered_lft)
1723 prefered_lft = valid_lft;
1729 ifp->valid_lft = valid_lft;
1730 ifp->prefered_lft = prefered_lft;
1733 ifp->flags &= ~IFA_F_DEPRECATED;
1734 spin_unlock(&ifp->lock);
1736 if (!(flags&IFA_F_TENTATIVE))
1737 ipv6_ifa_notify(0, ifp);
1739 spin_unlock(&ifp->lock);
1741 #ifdef CONFIG_IPV6_PRIVACY
1742 read_lock_bh(&in6_dev->lock);
1743 /* update all temporary addresses in the list */
1744 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1746 * When adjusting the lifetimes of an existing
1747 * temporary address, only lower the lifetimes.
1748 * Implementations must not increase the
1749 * lifetimes of an existing temporary address
1750 * when processing a Prefix Information Option.
1752 spin_lock(&ift->lock);
1754 if (ift->valid_lft > valid_lft &&
1755 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1756 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1757 if (ift->prefered_lft > prefered_lft &&
1758 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1759 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1760 spin_unlock(&ift->lock);
1761 if (!(flags&IFA_F_TENTATIVE))
1762 ipv6_ifa_notify(0, ift);
1765 if (create && in6_dev->cnf.use_tempaddr > 0) {
1767 * When a new public address is created as described in [ADDRCONF],
1768 * also create a new temporary address.
1770 read_unlock_bh(&in6_dev->lock);
1771 ipv6_create_tempaddr(ifp, NULL);
1773 read_unlock_bh(&in6_dev->lock);
1780 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1781 in6_dev_put(in6_dev);
1785 * Set destination address.
1786 * Special case for SIT interfaces where we create a new "virtual"
1789 int addrconf_set_dstaddr(void __user *arg)
1791 struct in6_ifreq ireq;
1792 struct net_device *dev;
1798 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1801 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1807 if (dev->type == ARPHRD_SIT) {
1810 struct ip_tunnel_parm p;
1812 err = -EADDRNOTAVAIL;
1813 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1816 memset(&p, 0, sizeof(p));
1817 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1821 p.iph.protocol = IPPROTO_IPV6;
1823 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1825 oldfs = get_fs(); set_fs(KERNEL_DS);
1826 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1831 if ((dev = __dev_get_by_name(p.name)) == NULL)
1833 err = dev_open(dev);
1843 * Manual configuration of address on an interface
1845 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1847 struct inet6_ifaddr *ifp;
1848 struct inet6_dev *idev;
1849 struct net_device *dev;
1854 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1857 if (!(dev->flags&IFF_UP))
1860 if ((idev = addrconf_add_dev(dev)) == NULL)
1863 scope = ipv6_addr_scope(pfx);
1865 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1867 addrconf_dad_start(ifp, 0);
1872 return PTR_ERR(ifp);
1875 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1877 struct inet6_ifaddr *ifp;
1878 struct inet6_dev *idev;
1879 struct net_device *dev;
1881 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1884 if ((idev = __in6_dev_get(dev)) == NULL)
1887 read_lock_bh(&idev->lock);
1888 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1889 if (ifp->prefix_len == plen &&
1890 ipv6_addr_equal(pfx, &ifp->addr)) {
1892 read_unlock_bh(&idev->lock);
1896 /* If the last address is deleted administratively,
1897 disable IPv6 on this interface.
1899 if (idev->addr_list == NULL)
1900 addrconf_ifdown(idev->dev, 1);
1904 read_unlock_bh(&idev->lock);
1905 return -EADDRNOTAVAIL;
1909 int addrconf_add_ifaddr(void __user *arg)
1911 struct in6_ifreq ireq;
1914 if (!capable(CAP_NET_ADMIN))
1917 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1921 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1926 int addrconf_del_ifaddr(void __user *arg)
1928 struct in6_ifreq ireq;
1931 if (!capable(CAP_NET_ADMIN))
1934 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1938 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1943 static void sit_add_v4_addrs(struct inet6_dev *idev)
1945 struct inet6_ifaddr * ifp;
1946 struct in6_addr addr;
1947 struct net_device *dev;
1952 memset(&addr, 0, sizeof(struct in6_addr));
1953 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1955 if (idev->dev->flags&IFF_POINTOPOINT) {
1956 addr.s6_addr32[0] = htonl(0xfe800000);
1959 scope = IPV6_ADDR_COMPATv4;
1962 if (addr.s6_addr32[3]) {
1963 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1965 spin_lock_bh(&ifp->lock);
1966 ifp->flags &= ~IFA_F_TENTATIVE;
1967 spin_unlock_bh(&ifp->lock);
1968 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1974 for (dev = dev_base; dev != NULL; dev = dev->next) {
1975 struct in_device * in_dev = __in_dev_get_rtnl(dev);
1976 if (in_dev && (dev->flags & IFF_UP)) {
1977 struct in_ifaddr * ifa;
1981 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1984 addr.s6_addr32[3] = ifa->ifa_local;
1986 if (ifa->ifa_scope == RT_SCOPE_LINK)
1988 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1989 if (idev->dev->flags&IFF_POINTOPOINT)
1993 if (idev->dev->flags&IFF_POINTOPOINT)
1998 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2001 spin_lock_bh(&ifp->lock);
2002 ifp->flags &= ~IFA_F_TENTATIVE;
2003 spin_unlock_bh(&ifp->lock);
2004 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2012 static void init_loopback(struct net_device *dev)
2014 struct inet6_dev *idev;
2015 struct inet6_ifaddr * ifp;
2021 if ((idev = ipv6_find_idev(dev)) == NULL) {
2022 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2026 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2028 spin_lock_bh(&ifp->lock);
2029 ifp->flags &= ~IFA_F_TENTATIVE;
2030 spin_unlock_bh(&ifp->lock);
2031 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2036 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2038 struct inet6_ifaddr * ifp;
2040 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2042 addrconf_dad_start(ifp, 0);
2047 static void addrconf_dev_config(struct net_device *dev)
2049 struct in6_addr addr;
2050 struct inet6_dev * idev;
2054 if ((dev->type != ARPHRD_ETHER) &&
2055 (dev->type != ARPHRD_FDDI) &&
2056 (dev->type != ARPHRD_IEEE802_TR) &&
2057 (dev->type != ARPHRD_ARCNET) &&
2058 (dev->type != ARPHRD_INFINIBAND)) {
2059 /* Alas, we support only Ethernet autoconfiguration. */
2063 idev = addrconf_add_dev(dev);
2067 memset(&addr, 0, sizeof(struct in6_addr));
2068 addr.s6_addr32[0] = htonl(0xFE800000);
2070 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2071 addrconf_add_linklocal(idev, &addr);
2074 static void addrconf_sit_config(struct net_device *dev)
2076 struct inet6_dev *idev;
2081 * Configure the tunnel with one of our IPv4
2082 * addresses... we should configure all of
2083 * our v4 addrs in the tunnel
2086 if ((idev = ipv6_find_idev(dev)) == NULL) {
2087 printk(KERN_DEBUG "init sit: add_dev failed\n");
2091 sit_add_v4_addrs(idev);
2093 if (dev->flags&IFF_POINTOPOINT) {
2094 addrconf_add_mroute(dev);
2095 addrconf_add_lroute(dev);
2101 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2103 struct in6_addr lladdr;
2105 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2106 addrconf_add_linklocal(idev, &lladdr);
2112 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2114 struct net_device *link_dev;
2116 /* first try to inherit the link-local address from the link device */
2117 if (idev->dev->iflink &&
2118 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2119 if (!ipv6_inherit_linklocal(idev, link_dev))
2122 /* then try to inherit it from any device */
2123 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2124 if (!ipv6_inherit_linklocal(idev, link_dev))
2127 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2131 * Autoconfigure tunnel with a link-local address so routing protocols,
2132 * DHCPv6, MLD etc. can be run over the virtual link
2135 static void addrconf_ip6_tnl_config(struct net_device *dev)
2137 struct inet6_dev *idev;
2141 if ((idev = addrconf_add_dev(dev)) == NULL) {
2142 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2145 ip6_tnl_add_linklocal(idev);
2146 addrconf_add_mroute(dev);
2149 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2152 struct net_device *dev = (struct net_device *) data;
2153 struct inet6_dev *idev = __in6_dev_get(dev);
2154 int run_pending = 0;
2159 if (event == NETDEV_UP) {
2160 if (!netif_carrier_ok(dev)) {
2161 /* device is not ready yet. */
2163 "ADDRCONF(NETDEV_UP): %s: "
2164 "link is not ready\n",
2170 idev->if_flags |= IF_READY;
2172 if (!netif_carrier_ok(dev)) {
2173 /* device is still not ready. */
2178 if (idev->if_flags & IF_READY) {
2179 /* device is already configured. */
2182 idev->if_flags |= IF_READY;
2186 "ADDRCONF(NETDEV_CHANGE): %s: "
2187 "link becomes ready\n",
2195 addrconf_sit_config(dev);
2197 case ARPHRD_TUNNEL6:
2198 addrconf_ip6_tnl_config(dev);
2200 case ARPHRD_LOOPBACK:
2205 addrconf_dev_config(dev);
2210 addrconf_dad_run(idev);
2212 /* If the MTU changed during the interface down, when the
2213 interface up, the changed MTU must be reflected in the
2214 idev as well as routers.
2216 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2217 rt6_mtu_change(dev, dev->mtu);
2218 idev->cnf.mtu6 = dev->mtu;
2220 idev->tstamp = jiffies;
2221 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2222 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2223 stop IPv6 on this interface.
2225 if (dev->mtu < IPV6_MIN_MTU)
2226 addrconf_ifdown(dev, event != NETDEV_DOWN);
2230 case NETDEV_CHANGEMTU:
2231 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2232 rt6_mtu_change(dev, dev->mtu);
2233 idev->cnf.mtu6 = dev->mtu;
2237 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2240 case NETDEV_UNREGISTER:
2242 * Remove all addresses from this interface.
2244 addrconf_ifdown(dev, event != NETDEV_DOWN);
2247 case NETDEV_CHANGENAME:
2248 #ifdef CONFIG_SYSCTL
2250 addrconf_sysctl_unregister(&idev->cnf);
2251 neigh_sysctl_unregister(idev->nd_parms);
2252 neigh_sysctl_register(dev, idev->nd_parms,
2253 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2254 &ndisc_ifinfo_sysctl_change,
2256 addrconf_sysctl_register(idev, &idev->cnf);
2266 * addrconf module should be notified of a device going up
2268 static struct notifier_block ipv6_dev_notf = {
2269 .notifier_call = addrconf_notify,
2273 static int addrconf_ifdown(struct net_device *dev, int how)
2275 struct inet6_dev *idev;
2276 struct inet6_ifaddr *ifa, **bifa;
2281 if (dev == &loopback_dev && how == 1)
2285 neigh_ifdown(&nd_tbl, dev);
2287 idev = __in6_dev_get(dev);
2291 /* Step 1: remove reference to ipv6 device from parent device.
2295 write_lock_bh(&addrconf_lock);
2296 dev->ip6_ptr = NULL;
2298 write_unlock_bh(&addrconf_lock);
2300 /* Step 1.5: remove snmp6 entry */
2301 snmp6_unregister_dev(idev);
2305 /* Step 2: clear hash table */
2306 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2307 bifa = &inet6_addr_lst[i];
2309 write_lock_bh(&addrconf_hash_lock);
2310 while ((ifa = *bifa) != NULL) {
2311 if (ifa->idev == idev) {
2312 *bifa = ifa->lst_next;
2313 ifa->lst_next = NULL;
2314 addrconf_del_timer(ifa);
2318 bifa = &ifa->lst_next;
2320 write_unlock_bh(&addrconf_hash_lock);
2323 write_lock_bh(&idev->lock);
2325 /* Step 3: clear flags for stateless addrconf */
2327 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2329 /* Step 4: clear address list */
2330 #ifdef CONFIG_IPV6_PRIVACY
2331 if (how == 1 && del_timer(&idev->regen_timer))
2334 /* clear tempaddr list */
2335 while ((ifa = idev->tempaddr_list) != NULL) {
2336 idev->tempaddr_list = ifa->tmp_next;
2337 ifa->tmp_next = NULL;
2339 write_unlock_bh(&idev->lock);
2340 spin_lock_bh(&ifa->lock);
2343 in6_ifa_put(ifa->ifpub);
2346 spin_unlock_bh(&ifa->lock);
2348 write_lock_bh(&idev->lock);
2351 while ((ifa = idev->addr_list) != NULL) {
2352 idev->addr_list = ifa->if_next;
2353 ifa->if_next = NULL;
2355 addrconf_del_timer(ifa);
2356 write_unlock_bh(&idev->lock);
2358 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2361 write_lock_bh(&idev->lock);
2363 write_unlock_bh(&idev->lock);
2365 /* Step 5: Discard multicast list */
2368 ipv6_mc_destroy_dev(idev);
2372 /* Step 5: netlink notification of this interface */
2373 idev->tstamp = jiffies;
2374 inet6_ifinfo_notify(RTM_DELLINK, idev);
2376 /* Shot the device (if unregistered) */
2379 #ifdef CONFIG_SYSCTL
2380 addrconf_sysctl_unregister(&idev->cnf);
2381 neigh_sysctl_unregister(idev->nd_parms);
2383 neigh_parms_release(&nd_tbl, idev->nd_parms);
2384 neigh_ifdown(&nd_tbl, dev);
2390 static void addrconf_rs_timer(unsigned long data)
2392 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2394 if (ifp->idev->cnf.forwarding)
2397 if (ifp->idev->if_flags & IF_RA_RCVD) {
2399 * Announcement received after solicitation
2405 spin_lock(&ifp->lock);
2406 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2407 struct in6_addr all_routers;
2409 /* The wait after the last probe can be shorter */
2410 addrconf_mod_timer(ifp, AC_RS,
2411 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2412 ifp->idev->cnf.rtr_solicit_delay :
2413 ifp->idev->cnf.rtr_solicit_interval);
2414 spin_unlock(&ifp->lock);
2416 ipv6_addr_all_routers(&all_routers);
2418 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2420 spin_unlock(&ifp->lock);
2422 * Note: we do not support deprecated "all on-link"
2423 * assumption any longer.
2425 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2426 ifp->idev->dev->name);
2434 * Duplicate Address Detection
2436 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2438 unsigned long rand_num;
2439 struct inet6_dev *idev = ifp->idev;
2441 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2442 ifp->probes = idev->cnf.dad_transmits;
2443 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2446 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2448 struct inet6_dev *idev = ifp->idev;
2449 struct net_device *dev = idev->dev;
2451 addrconf_join_solict(dev, &ifp->addr);
2453 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2454 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2457 net_srandom(ifp->addr.s6_addr32[3]);
2459 read_lock_bh(&idev->lock);
2462 spin_lock_bh(&ifp->lock);
2464 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2465 !(ifp->flags&IFA_F_TENTATIVE)) {
2466 ifp->flags &= ~IFA_F_TENTATIVE;
2467 spin_unlock_bh(&ifp->lock);
2468 read_unlock_bh(&idev->lock);
2470 addrconf_dad_completed(ifp);
2474 if (!(idev->if_flags & IF_READY)) {
2475 spin_unlock_bh(&ifp->lock);
2476 read_unlock_bh(&idev->lock);
2478 * If the defice is not ready:
2479 * - keep it tentative if it is a permanent address.
2480 * - otherwise, kill it.
2483 addrconf_dad_stop(ifp);
2486 addrconf_dad_kick(ifp);
2487 spin_unlock_bh(&ifp->lock);
2489 read_unlock_bh(&idev->lock);
2492 static void addrconf_dad_timer(unsigned long data)
2494 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2495 struct inet6_dev *idev = ifp->idev;
2496 struct in6_addr unspec;
2497 struct in6_addr mcaddr;
2499 read_lock_bh(&idev->lock);
2501 read_unlock_bh(&idev->lock);
2504 spin_lock_bh(&ifp->lock);
2505 if (ifp->probes == 0) {
2507 * DAD was successful
2510 ifp->flags &= ~IFA_F_TENTATIVE;
2511 spin_unlock_bh(&ifp->lock);
2512 read_unlock_bh(&idev->lock);
2514 addrconf_dad_completed(ifp);
2520 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2521 spin_unlock_bh(&ifp->lock);
2522 read_unlock_bh(&idev->lock);
2524 /* send a neighbour solicitation for our addr */
2525 memset(&unspec, 0, sizeof(unspec));
2526 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2527 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2532 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2534 struct net_device * dev = ifp->idev->dev;
2537 * Configure the address for reception. Now it is valid.
2540 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2542 /* If added prefix is link local and forwarding is off,
2543 start sending router solicitations.
2546 if (ifp->idev->cnf.forwarding == 0 &&
2547 ifp->idev->cnf.rtr_solicits > 0 &&
2548 (dev->flags&IFF_LOOPBACK) == 0 &&
2549 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2550 struct in6_addr all_routers;
2552 ipv6_addr_all_routers(&all_routers);
2555 * If a host as already performed a random delay
2556 * [...] as part of DAD [...] there is no need
2557 * to delay again before sending the first RS
2559 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2561 spin_lock_bh(&ifp->lock);
2563 ifp->idev->if_flags |= IF_RS_SENT;
2564 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2565 spin_unlock_bh(&ifp->lock);
2569 static void addrconf_dad_run(struct inet6_dev *idev) {
2570 struct inet6_ifaddr *ifp;
2572 read_lock_bh(&idev->lock);
2573 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2574 spin_lock_bh(&ifp->lock);
2575 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2576 spin_unlock_bh(&ifp->lock);
2579 spin_unlock_bh(&ifp->lock);
2580 addrconf_dad_kick(ifp);
2582 read_unlock_bh(&idev->lock);
2585 #ifdef CONFIG_PROC_FS
2586 struct if6_iter_state {
2590 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2592 struct inet6_ifaddr *ifa = NULL;
2593 struct if6_iter_state *state = seq->private;
2595 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2596 ifa = inet6_addr_lst[state->bucket];
2603 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2605 struct if6_iter_state *state = seq->private;
2607 ifa = ifa->lst_next;
2609 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2610 ifa = inet6_addr_lst[state->bucket];
2616 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2618 struct inet6_ifaddr *ifa = if6_get_first(seq);
2621 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2623 return pos ? NULL : ifa;
2626 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2628 read_lock_bh(&addrconf_hash_lock);
2629 return if6_get_idx(seq, *pos);
2632 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2634 struct inet6_ifaddr *ifa;
2636 ifa = if6_get_next(seq, v);
2641 static void if6_seq_stop(struct seq_file *seq, void *v)
2643 read_unlock_bh(&addrconf_hash_lock);
2646 static int if6_seq_show(struct seq_file *seq, void *v)
2648 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2650 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
2652 ifp->idev->dev->ifindex,
2656 ifp->idev->dev->name);
2660 static struct seq_operations if6_seq_ops = {
2661 .start = if6_seq_start,
2662 .next = if6_seq_next,
2663 .show = if6_seq_show,
2664 .stop = if6_seq_stop,
2667 static int if6_seq_open(struct inode *inode, struct file *file)
2669 struct seq_file *seq;
2671 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2675 memset(s, 0, sizeof(*s));
2677 rc = seq_open(file, &if6_seq_ops);
2681 seq = file->private_data;
2690 static struct file_operations if6_fops = {
2691 .owner = THIS_MODULE,
2692 .open = if6_seq_open,
2694 .llseek = seq_lseek,
2695 .release = seq_release_private,
2698 int __init if6_proc_init(void)
2700 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2705 void if6_proc_exit(void)
2707 proc_net_remove("if_inet6");
2709 #endif /* CONFIG_PROC_FS */
2712 * Periodic address status verification
2715 static void addrconf_verify(unsigned long foo)
2717 struct inet6_ifaddr *ifp;
2718 unsigned long now, next;
2721 spin_lock_bh(&addrconf_verify_lock);
2723 next = now + ADDR_CHECK_FREQUENCY;
2725 del_timer(&addr_chk_timer);
2727 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2730 read_lock(&addrconf_hash_lock);
2731 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2733 #ifdef CONFIG_IPV6_PRIVACY
2734 unsigned long regen_advance;
2737 if (ifp->flags & IFA_F_PERMANENT)
2740 spin_lock(&ifp->lock);
2741 age = (now - ifp->tstamp) / HZ;
2743 #ifdef CONFIG_IPV6_PRIVACY
2744 regen_advance = ifp->idev->cnf.regen_max_retry *
2745 ifp->idev->cnf.dad_transmits *
2746 ifp->idev->nd_parms->retrans_time / HZ;
2749 if (age >= ifp->valid_lft) {
2750 spin_unlock(&ifp->lock);
2752 read_unlock(&addrconf_hash_lock);
2755 } else if (age >= ifp->prefered_lft) {
2756 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2759 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2761 ifp->flags |= IFA_F_DEPRECATED;
2764 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2765 next = ifp->tstamp + ifp->valid_lft * HZ;
2767 spin_unlock(&ifp->lock);
2771 read_unlock(&addrconf_hash_lock);
2773 ipv6_ifa_notify(0, ifp);
2777 #ifdef CONFIG_IPV6_PRIVACY
2778 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2779 !(ifp->flags&IFA_F_TENTATIVE)) {
2780 if (age >= ifp->prefered_lft - regen_advance) {
2781 struct inet6_ifaddr *ifpub = ifp->ifpub;
2782 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2783 next = ifp->tstamp + ifp->prefered_lft * HZ;
2784 if (!ifp->regen_count && ifpub) {
2787 in6_ifa_hold(ifpub);
2788 spin_unlock(&ifp->lock);
2789 read_unlock(&addrconf_hash_lock);
2790 spin_lock(&ifpub->lock);
2791 ifpub->regen_count = 0;
2792 spin_unlock(&ifpub->lock);
2793 ipv6_create_tempaddr(ifpub, ifp);
2798 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2799 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2800 spin_unlock(&ifp->lock);
2803 /* ifp->prefered_lft <= ifp->valid_lft */
2804 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2805 next = ifp->tstamp + ifp->prefered_lft * HZ;
2806 spin_unlock(&ifp->lock);
2809 read_unlock(&addrconf_hash_lock);
2812 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2813 add_timer(&addr_chk_timer);
2814 spin_unlock_bh(&addrconf_verify_lock);
2818 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2820 struct rtattr **rta = arg;
2821 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2822 struct in6_addr *pfx;
2825 if (rta[IFA_ADDRESS-1]) {
2826 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2828 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2830 if (rta[IFA_LOCAL-1]) {
2831 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2833 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2838 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2842 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2844 struct rtattr **rta = arg;
2845 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2846 struct in6_addr *pfx;
2849 if (rta[IFA_ADDRESS-1]) {
2850 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2852 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2854 if (rta[IFA_LOCAL-1]) {
2855 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2857 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2862 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2865 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2866 u32 pid, u32 seq, int event, unsigned int flags)
2868 struct ifaddrmsg *ifm;
2869 struct nlmsghdr *nlh;
2870 struct ifa_cacheinfo ci;
2871 unsigned char *b = skb->tail;
2873 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2874 ifm = NLMSG_DATA(nlh);
2875 ifm->ifa_family = AF_INET6;
2876 ifm->ifa_prefixlen = ifa->prefix_len;
2877 ifm->ifa_flags = ifa->flags;
2878 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2879 if (ifa->scope&IFA_HOST)
2880 ifm->ifa_scope = RT_SCOPE_HOST;
2881 else if (ifa->scope&IFA_LINK)
2882 ifm->ifa_scope = RT_SCOPE_LINK;
2883 else if (ifa->scope&IFA_SITE)
2884 ifm->ifa_scope = RT_SCOPE_SITE;
2885 ifm->ifa_index = ifa->idev->dev->ifindex;
2886 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2887 if (!(ifa->flags&IFA_F_PERMANENT)) {
2888 ci.ifa_prefered = ifa->prefered_lft;
2889 ci.ifa_valid = ifa->valid_lft;
2890 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2891 long tval = (jiffies - ifa->tstamp)/HZ;
2892 ci.ifa_prefered -= tval;
2893 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2894 ci.ifa_valid -= tval;
2897 ci.ifa_prefered = INFINITY_LIFE_TIME;
2898 ci.ifa_valid = INFINITY_LIFE_TIME;
2900 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2901 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2902 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2903 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2904 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2905 nlh->nlmsg_len = skb->tail - b;
2910 skb_trim(skb, b - skb->data);
2914 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2915 u32 pid, u32 seq, int event, u16 flags)
2917 struct ifaddrmsg *ifm;
2918 struct nlmsghdr *nlh;
2919 struct ifa_cacheinfo ci;
2920 unsigned char *b = skb->tail;
2922 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2923 ifm = NLMSG_DATA(nlh);
2924 ifm->ifa_family = AF_INET6;
2925 ifm->ifa_prefixlen = 128;
2926 ifm->ifa_flags = IFA_F_PERMANENT;
2927 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2928 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2929 ifm->ifa_scope = RT_SCOPE_SITE;
2930 ifm->ifa_index = ifmca->idev->dev->ifindex;
2931 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2932 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2933 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2935 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2936 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2938 ci.ifa_prefered = INFINITY_LIFE_TIME;
2939 ci.ifa_valid = INFINITY_LIFE_TIME;
2940 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2941 nlh->nlmsg_len = skb->tail - b;
2946 skb_trim(skb, b - skb->data);
2950 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2951 u32 pid, u32 seq, int event, unsigned int flags)
2953 struct ifaddrmsg *ifm;
2954 struct nlmsghdr *nlh;
2955 struct ifa_cacheinfo ci;
2956 unsigned char *b = skb->tail;
2958 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2959 ifm = NLMSG_DATA(nlh);
2960 ifm->ifa_family = AF_INET6;
2961 ifm->ifa_prefixlen = 128;
2962 ifm->ifa_flags = IFA_F_PERMANENT;
2963 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2964 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2965 ifm->ifa_scope = RT_SCOPE_SITE;
2966 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2967 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2968 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2969 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2971 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2972 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2974 ci.ifa_prefered = INFINITY_LIFE_TIME;
2975 ci.ifa_valid = INFINITY_LIFE_TIME;
2976 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2977 nlh->nlmsg_len = skb->tail - b;
2982 skb_trim(skb, b - skb->data);
2993 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2994 enum addr_type_t type)
2997 int s_idx, s_ip_idx;
2999 struct net_device *dev;
3000 struct inet6_dev *idev = NULL;
3001 struct inet6_ifaddr *ifa;
3002 struct ifmcaddr6 *ifmca;
3003 struct ifacaddr6 *ifaca;
3005 s_idx = cb->args[0];
3006 s_ip_idx = ip_idx = cb->args[1];
3007 read_lock(&dev_base_lock);
3009 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3015 if ((idev = in6_dev_get(dev)) == NULL)
3017 read_lock_bh(&idev->lock);
3020 /* unicast address incl. temp addr */
3021 for (ifa = idev->addr_list; ifa;
3022 ifa = ifa->if_next, ip_idx++) {
3023 if (ip_idx < s_ip_idx)
3025 if ((err = inet6_fill_ifaddr(skb, ifa,
3026 NETLINK_CB(cb->skb).pid,
3027 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3032 case MULTICAST_ADDR:
3033 /* multicast address */
3034 for (ifmca = idev->mc_list; ifmca;
3035 ifmca = ifmca->next, ip_idx++) {
3036 if (ip_idx < s_ip_idx)
3038 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3039 NETLINK_CB(cb->skb).pid,
3040 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3046 /* anycast address */
3047 for (ifaca = idev->ac_list; ifaca;
3048 ifaca = ifaca->aca_next, ip_idx++) {
3049 if (ip_idx < s_ip_idx)
3051 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3052 NETLINK_CB(cb->skb).pid,
3053 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3061 read_unlock_bh(&idev->lock);
3066 read_unlock_bh(&idev->lock);
3069 read_unlock(&dev_base_lock);
3071 cb->args[1] = ip_idx;
3075 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3077 enum addr_type_t type = UNICAST_ADDR;
3078 return inet6_dump_addr(skb, cb, type);
3081 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3083 enum addr_type_t type = MULTICAST_ADDR;
3084 return inet6_dump_addr(skb, cb, type);
3088 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3090 enum addr_type_t type = ANYCAST_ADDR;
3091 return inet6_dump_addr(skb, cb, type);
3094 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3096 struct sk_buff *skb;
3097 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
3099 skb = alloc_skb(size, GFP_ATOMIC);
3101 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
3104 if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
3106 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
3109 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
3110 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
3113 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3114 __s32 *array, int bytes)
3116 memset(array, 0, bytes);
3117 array[DEVCONF_FORWARDING] = cnf->forwarding;
3118 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3119 array[DEVCONF_MTU6] = cnf->mtu6;
3120 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3121 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3122 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3123 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3124 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3125 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3126 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3127 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3128 #ifdef CONFIG_IPV6_PRIVACY
3129 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3130 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3131 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3132 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3133 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3135 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3138 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3139 u32 pid, u32 seq, int event, unsigned int flags)
3141 struct net_device *dev = idev->dev;
3142 __s32 *array = NULL;
3143 struct ifinfomsg *r;
3144 struct nlmsghdr *nlh;
3145 unsigned char *b = skb->tail;
3146 struct rtattr *subattr;
3147 __u32 mtu = dev->mtu;
3148 struct ifla_cacheinfo ci;
3150 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
3151 r = NLMSG_DATA(nlh);
3152 r->ifi_family = AF_INET6;
3154 r->ifi_type = dev->type;
3155 r->ifi_index = dev->ifindex;
3156 r->ifi_flags = dev_get_flags(dev);
3159 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
3162 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3164 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
3165 if (dev->ifindex != dev->iflink)
3166 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
3168 subattr = (struct rtattr*)skb->tail;
3170 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
3172 /* return the device flags */
3173 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
3175 /* return interface cacheinfo */
3176 ci.max_reasm_len = IPV6_MAXPLEN;
3177 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3178 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3179 ci.reachable_time = idev->nd_parms->reachable_time;
3180 ci.retrans_time = idev->nd_parms->retrans_time;
3181 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3183 /* return the device sysctl params */
3184 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
3185 goto rtattr_failure;
3186 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
3187 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
3189 /* XXX - Statistics/MC not implemented */
3190 subattr->rta_len = skb->tail - (u8*)subattr;
3192 nlh->nlmsg_len = skb->tail - b;
3199 skb_trim(skb, b - skb->data);
3203 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3206 int s_idx = cb->args[0];
3207 struct net_device *dev;
3208 struct inet6_dev *idev;
3210 read_lock(&dev_base_lock);
3211 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3214 if ((idev = in6_dev_get(dev)) == NULL)
3216 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3217 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3222 read_unlock(&dev_base_lock);
3228 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3230 struct sk_buff *skb;
3232 int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
3234 skb = alloc_skb(size, GFP_ATOMIC);
3236 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
3239 if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
3241 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
3244 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
3245 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
3248 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3249 struct prefix_info *pinfo, u32 pid, u32 seq,
3250 int event, unsigned int flags)
3252 struct prefixmsg *pmsg;
3253 struct nlmsghdr *nlh;
3254 unsigned char *b = skb->tail;
3255 struct prefix_cacheinfo ci;
3257 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
3258 pmsg = NLMSG_DATA(nlh);
3259 pmsg->prefix_family = AF_INET6;
3260 pmsg->prefix_pad1 = 0;
3261 pmsg->prefix_pad2 = 0;
3262 pmsg->prefix_ifindex = idev->dev->ifindex;
3263 pmsg->prefix_len = pinfo->prefix_len;
3264 pmsg->prefix_type = pinfo->type;
3265 pmsg->prefix_pad3 = 0;
3267 pmsg->prefix_flags = 0;
3269 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3270 if (pinfo->autoconf)
3271 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3273 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3275 ci.preferred_time = ntohl(pinfo->prefered);
3276 ci.valid_time = ntohl(pinfo->valid);
3277 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3279 nlh->nlmsg_len = skb->tail - b;
3284 skb_trim(skb, b - skb->data);
3288 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3289 struct prefix_info *pinfo)
3291 struct sk_buff *skb;
3292 int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
3294 skb = alloc_skb(size, GFP_ATOMIC);
3296 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
3299 if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
3301 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
3304 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
3305 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
3308 static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
3309 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3310 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3311 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
3312 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
3313 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3314 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3315 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3316 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3317 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3318 .dumpit = inet6_dump_fib, },
3321 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3323 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3327 ip6_ins_rt(ifp->rt, NULL, NULL, NULL);
3328 if (ifp->idev->cnf.forwarding)
3329 addrconf_join_anycast(ifp);
3332 if (ifp->idev->cnf.forwarding)
3333 addrconf_leave_anycast(ifp);
3334 addrconf_leave_solict(ifp->idev, &ifp->addr);
3335 dst_hold(&ifp->rt->u.dst);
3336 if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
3337 dst_free(&ifp->rt->u.dst);
3342 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3344 read_lock_bh(&addrconf_lock);
3345 if (likely(ifp->idev->dead == 0))
3346 __ipv6_ifa_notify(event, ifp);
3347 read_unlock_bh(&addrconf_lock);
3350 #ifdef CONFIG_SYSCTL
3353 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3354 void __user *buffer, size_t *lenp, loff_t *ppos)
3356 int *valp = ctl->data;
3360 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3362 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3363 if (valp != &ipv6_devconf.forwarding) {
3364 if ((!*valp) ^ (!val)) {
3365 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3368 dev_forward_change(idev);
3371 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3372 addrconf_forward_change();
3375 rt6_purge_dflt_routers();
3381 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3382 int __user *name, int nlen,
3383 void __user *oldval,
3384 size_t __user *oldlenp,
3385 void __user *newval, size_t newlen,
3388 int *valp = table->data;
3391 if (!newval || !newlen)
3393 if (newlen != sizeof(int))
3395 if (get_user(new, (int __user *)newval))
3399 if (oldval && oldlenp) {
3401 if (get_user(len, oldlenp))
3404 if (len > table->maxlen)
3405 len = table->maxlen;
3406 if (copy_to_user(oldval, valp, len))
3408 if (put_user(len, oldlenp))
3413 if (valp != &ipv6_devconf_dflt.forwarding) {
3414 if (valp != &ipv6_devconf.forwarding) {
3415 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3417 if (unlikely(idev == NULL))
3419 changed = (!*valp) ^ (!new);
3422 dev_forward_change(idev);
3425 addrconf_forward_change();
3429 rt6_purge_dflt_routers();
3436 static struct addrconf_sysctl_table
3438 struct ctl_table_header *sysctl_header;
3439 ctl_table addrconf_vars[__NET_IPV6_MAX];
3440 ctl_table addrconf_dev[2];
3441 ctl_table addrconf_conf_dir[2];
3442 ctl_table addrconf_proto_dir[2];
3443 ctl_table addrconf_root_dir[2];
3444 } addrconf_sysctl = {
3445 .sysctl_header = NULL,
3448 .ctl_name = NET_IPV6_FORWARDING,
3449 .procname = "forwarding",
3450 .data = &ipv6_devconf.forwarding,
3451 .maxlen = sizeof(int),
3453 .proc_handler = &addrconf_sysctl_forward,
3454 .strategy = &addrconf_sysctl_forward_strategy,
3457 .ctl_name = NET_IPV6_HOP_LIMIT,
3458 .procname = "hop_limit",
3459 .data = &ipv6_devconf.hop_limit,
3460 .maxlen = sizeof(int),
3462 .proc_handler = proc_dointvec,
3465 .ctl_name = NET_IPV6_MTU,
3467 .data = &ipv6_devconf.mtu6,
3468 .maxlen = sizeof(int),
3470 .proc_handler = &proc_dointvec,
3473 .ctl_name = NET_IPV6_ACCEPT_RA,
3474 .procname = "accept_ra",
3475 .data = &ipv6_devconf.accept_ra,
3476 .maxlen = sizeof(int),
3478 .proc_handler = &proc_dointvec,
3481 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3482 .procname = "accept_redirects",
3483 .data = &ipv6_devconf.accept_redirects,
3484 .maxlen = sizeof(int),
3486 .proc_handler = &proc_dointvec,
3489 .ctl_name = NET_IPV6_AUTOCONF,
3490 .procname = "autoconf",
3491 .data = &ipv6_devconf.autoconf,
3492 .maxlen = sizeof(int),
3494 .proc_handler = &proc_dointvec,
3497 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3498 .procname = "dad_transmits",
3499 .data = &ipv6_devconf.dad_transmits,
3500 .maxlen = sizeof(int),
3502 .proc_handler = &proc_dointvec,
3505 .ctl_name = NET_IPV6_RTR_SOLICITS,
3506 .procname = "router_solicitations",
3507 .data = &ipv6_devconf.rtr_solicits,
3508 .maxlen = sizeof(int),
3510 .proc_handler = &proc_dointvec,
3513 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3514 .procname = "router_solicitation_interval",
3515 .data = &ipv6_devconf.rtr_solicit_interval,
3516 .maxlen = sizeof(int),
3518 .proc_handler = &proc_dointvec_jiffies,
3519 .strategy = &sysctl_jiffies,
3522 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3523 .procname = "router_solicitation_delay",
3524 .data = &ipv6_devconf.rtr_solicit_delay,
3525 .maxlen = sizeof(int),
3527 .proc_handler = &proc_dointvec_jiffies,
3528 .strategy = &sysctl_jiffies,
3531 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3532 .procname = "force_mld_version",
3533 .data = &ipv6_devconf.force_mld_version,
3534 .maxlen = sizeof(int),
3536 .proc_handler = &proc_dointvec,
3538 #ifdef CONFIG_IPV6_PRIVACY
3540 .ctl_name = NET_IPV6_USE_TEMPADDR,
3541 .procname = "use_tempaddr",
3542 .data = &ipv6_devconf.use_tempaddr,
3543 .maxlen = sizeof(int),
3545 .proc_handler = &proc_dointvec,
3548 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3549 .procname = "temp_valid_lft",
3550 .data = &ipv6_devconf.temp_valid_lft,
3551 .maxlen = sizeof(int),
3553 .proc_handler = &proc_dointvec,
3556 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3557 .procname = "temp_prefered_lft",
3558 .data = &ipv6_devconf.temp_prefered_lft,
3559 .maxlen = sizeof(int),
3561 .proc_handler = &proc_dointvec,
3564 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3565 .procname = "regen_max_retry",
3566 .data = &ipv6_devconf.regen_max_retry,
3567 .maxlen = sizeof(int),
3569 .proc_handler = &proc_dointvec,
3572 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3573 .procname = "max_desync_factor",
3574 .data = &ipv6_devconf.max_desync_factor,
3575 .maxlen = sizeof(int),
3577 .proc_handler = &proc_dointvec,
3581 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3582 .procname = "max_addresses",
3583 .data = &ipv6_devconf.max_addresses,
3584 .maxlen = sizeof(int),
3586 .proc_handler = &proc_dointvec,
3589 .ctl_name = 0, /* sentinel */
3594 .ctl_name = NET_PROTO_CONF_ALL,
3597 .child = addrconf_sysctl.addrconf_vars,
3600 .ctl_name = 0, /* sentinel */
3603 .addrconf_conf_dir = {
3605 .ctl_name = NET_IPV6_CONF,
3608 .child = addrconf_sysctl.addrconf_dev,
3611 .ctl_name = 0, /* sentinel */
3614 .addrconf_proto_dir = {
3616 .ctl_name = NET_IPV6,
3619 .child = addrconf_sysctl.addrconf_conf_dir,
3622 .ctl_name = 0, /* sentinel */
3625 .addrconf_root_dir = {
3627 .ctl_name = CTL_NET,
3630 .child = addrconf_sysctl.addrconf_proto_dir,
3633 .ctl_name = 0, /* sentinel */
3638 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3641 struct net_device *dev = idev ? idev->dev : NULL;
3642 struct addrconf_sysctl_table *t;
3643 char *dev_name = NULL;
3645 t = kmalloc(sizeof(*t), GFP_KERNEL);
3648 memcpy(t, &addrconf_sysctl, sizeof(*t));
3649 for (i=0; t->addrconf_vars[i].data; i++) {
3650 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3651 t->addrconf_vars[i].de = NULL;
3652 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3655 dev_name = dev->name;
3656 t->addrconf_dev[0].ctl_name = dev->ifindex;
3658 dev_name = "default";
3659 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3663 * Make a copy of dev_name, because '.procname' is regarded as const
3664 * by sysctl and we wouldn't want anyone to change it under our feet
3665 * (see SIOCSIFNAME).
3667 dev_name = kstrdup(dev_name, GFP_KERNEL);
3671 t->addrconf_dev[0].procname = dev_name;
3673 t->addrconf_dev[0].child = t->addrconf_vars;
3674 t->addrconf_dev[0].de = NULL;
3675 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3676 t->addrconf_conf_dir[0].de = NULL;
3677 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3678 t->addrconf_proto_dir[0].de = NULL;
3679 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3680 t->addrconf_root_dir[0].de = NULL;
3682 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3683 if (t->sysctl_header == NULL)
3698 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3701 struct addrconf_sysctl_table *t = p->sysctl;
3703 unregister_sysctl_table(t->sysctl_header);
3704 kfree(t->addrconf_dev[0].procname);
3716 int register_inet6addr_notifier(struct notifier_block *nb)
3718 return notifier_chain_register(&inet6addr_chain, nb);
3721 int unregister_inet6addr_notifier(struct notifier_block *nb)
3723 return notifier_chain_unregister(&inet6addr_chain,nb);
3727 * Init / cleanup code
3730 int __init addrconf_init(void)
3734 /* The addrconf netdev notifier requires that loopback_dev
3735 * has it's ipv6 private information allocated and setup
3736 * before it can bring up and give link-local addresses
3737 * to other devices which are up.
3739 * Unfortunately, loopback_dev is not necessarily the first
3740 * entry in the global dev_base list of net devices. In fact,
3741 * it is likely to be the very last entry on that list.
3742 * So this causes the notifier registry below to try and
3743 * give link-local addresses to all devices besides loopback_dev
3744 * first, then loopback_dev, which cases all the non-loopback_dev
3745 * devices to fail to get a link-local address.
3747 * So, as a temporary fix, allocate the ipv6 structure for
3748 * loopback_dev first by hand.
3749 * Longer term, all of the dependencies ipv6 has upon the loopback
3750 * device and it being up should be removed.
3753 if (!ipv6_add_dev(&loopback_dev))
3759 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
3761 register_netdevice_notifier(&ipv6_dev_notf);
3763 #ifdef CONFIG_IPV6_PRIVACY
3764 md5_tfm = crypto_alloc_tfm("md5", 0);
3765 if (unlikely(md5_tfm == NULL))
3767 "failed to load transform for md5\n");
3771 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3772 #ifdef CONFIG_SYSCTL
3773 addrconf_sysctl.sysctl_header =
3774 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3775 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3781 void __exit addrconf_cleanup(void)
3783 struct net_device *dev;
3784 struct inet6_dev *idev;
3785 struct inet6_ifaddr *ifa;
3788 unregister_netdevice_notifier(&ipv6_dev_notf);
3790 rtnetlink_links[PF_INET6] = NULL;
3791 #ifdef CONFIG_SYSCTL
3792 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3793 addrconf_sysctl_unregister(&ipv6_devconf);
3802 for (dev=dev_base; dev; dev=dev->next) {
3803 if ((idev = __in6_dev_get(dev)) == NULL)
3805 addrconf_ifdown(dev, 1);
3807 addrconf_ifdown(&loopback_dev, 2);
3813 write_lock_bh(&addrconf_hash_lock);
3814 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3815 for (ifa=inet6_addr_lst[i]; ifa; ) {
3816 struct inet6_ifaddr *bifa;
3819 ifa = ifa->lst_next;
3820 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3821 /* Do not free it; something is wrong.
3822 Now we can investigate it with debugger.
3826 write_unlock_bh(&addrconf_hash_lock);
3828 del_timer(&addr_chk_timer);
3832 #ifdef CONFIG_IPV6_PRIVACY
3833 crypto_free_tfm(md5_tfm);
3837 #ifdef CONFIG_PROC_FS
3838 proc_net_remove("if_inet6");