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/delay.h>
62 #include <linux/notifier.h>
63 #include <linux/string.h>
69 #include <net/protocol.h>
70 #include <net/ndisc.h>
71 #include <net/ip6_route.h>
72 #include <net/addrconf.h>
75 #include <linux/if_tunnel.h>
76 #include <linux/rtnetlink.h>
78 #ifdef CONFIG_IPV6_PRIVACY
79 #include <linux/random.h>
80 #include <linux/crypto.h>
81 #include <linux/scatterlist.h>
84 #include <asm/uaccess.h>
86 #include <linux/proc_fs.h>
87 #include <linux/seq_file.h>
89 /* Set to 3 to get tracing... */
93 #define ADBG(x) printk x
98 #define INFINITY_LIFE_TIME 0xFFFFFFFF
99 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
102 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
103 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
106 #ifdef CONFIG_IPV6_PRIVACY
107 static int __ipv6_regen_rndid(struct inet6_dev *idev);
108 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
109 static void ipv6_regen_rndid(unsigned long data);
111 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
112 static struct crypto_tfm *md5_tfm;
113 static DEFINE_SPINLOCK(md5_tfm_lock);
116 static int ipv6_count_addresses(struct inet6_dev *idev);
119 * Configured unicast address hash table
121 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
122 static DEFINE_RWLOCK(addrconf_hash_lock);
124 /* Protects inet6 devices */
125 DEFINE_RWLOCK(addrconf_lock);
127 static void addrconf_verify(unsigned long);
129 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
130 static DEFINE_SPINLOCK(addrconf_verify_lock);
132 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
133 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
135 static int addrconf_ifdown(struct net_device *dev, int how);
137 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
138 static void addrconf_dad_timer(unsigned long data);
139 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
140 static void addrconf_rs_timer(unsigned long data);
141 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
142 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
144 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
145 struct prefix_info *pinfo);
146 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
148 static struct notifier_block *inet6addr_chain;
150 struct ipv6_devconf ipv6_devconf = {
152 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
153 .mtu6 = IPV6_MIN_MTU,
155 .accept_redirects = 1,
157 .force_mld_version = 0,
159 .rtr_solicits = MAX_RTR_SOLICITATIONS,
160 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
161 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
162 #ifdef CONFIG_IPV6_PRIVACY
164 .temp_valid_lft = TEMP_VALID_LIFETIME,
165 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
166 .regen_max_retry = REGEN_MAX_RETRY,
167 .max_desync_factor = MAX_DESYNC_FACTOR,
169 .max_addresses = IPV6_MAX_ADDRESSES,
172 static struct ipv6_devconf ipv6_devconf_dflt = {
174 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
175 .mtu6 = IPV6_MIN_MTU,
177 .accept_redirects = 1,
180 .rtr_solicits = MAX_RTR_SOLICITATIONS,
181 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
182 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
183 #ifdef CONFIG_IPV6_PRIVACY
185 .temp_valid_lft = TEMP_VALID_LIFETIME,
186 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
187 .regen_max_retry = REGEN_MAX_RETRY,
188 .max_desync_factor = MAX_DESYNC_FACTOR,
190 .max_addresses = IPV6_MAX_ADDRESSES,
193 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
195 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
197 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
199 #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
201 static inline unsigned ipv6_addr_scope2type(unsigned scope)
204 case IPV6_ADDR_SCOPE_NODELOCAL:
205 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
207 case IPV6_ADDR_SCOPE_LINKLOCAL:
208 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
209 IPV6_ADDR_LINKLOCAL);
210 case IPV6_ADDR_SCOPE_SITELOCAL:
211 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
212 IPV6_ADDR_SITELOCAL);
214 return IPV6_ADDR_SCOPE_TYPE(scope);
217 int __ipv6_addr_type(const struct in6_addr *addr)
221 st = addr->s6_addr32[0];
223 /* Consider all addresses with the first three bits different of
224 000 and 111 as unicasts.
226 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
227 (st & htonl(0xE0000000)) != htonl(0xE0000000))
228 return (IPV6_ADDR_UNICAST |
229 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
231 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
233 /* addr-select 3.1 */
234 return (IPV6_ADDR_MULTICAST |
235 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
238 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
239 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
240 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
241 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
242 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
243 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
245 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
246 if (addr->s6_addr32[2] == 0) {
247 if (addr->s6_addr32[3] == 0)
248 return IPV6_ADDR_ANY;
250 if (addr->s6_addr32[3] == htonl(0x00000001))
251 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
252 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
254 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
255 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
258 if (addr->s6_addr32[2] == htonl(0x0000ffff))
259 return (IPV6_ADDR_MAPPED |
260 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
263 return (IPV6_ADDR_RESERVED |
264 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
267 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
269 if (del_timer(&ifp->timer))
273 enum addrconf_timer_t
280 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
281 enum addrconf_timer_t what,
284 if (!del_timer(&ifp->timer))
289 ifp->timer.function = addrconf_dad_timer;
292 ifp->timer.function = addrconf_rs_timer;
296 ifp->timer.expires = jiffies + when;
297 add_timer(&ifp->timer);
300 /* Nobody refers to this device, we may destroy it. */
302 void in6_dev_finish_destroy(struct inet6_dev *idev)
304 struct net_device *dev = idev->dev;
305 BUG_TRAP(idev->addr_list==NULL);
306 BUG_TRAP(idev->mc_list==NULL);
307 #ifdef NET_REFCNT_DEBUG
308 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
312 printk("Freeing alive inet6 device %p\n", idev);
315 snmp6_free_dev(idev);
319 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
321 struct inet6_dev *ndev;
325 if (dev->mtu < IPV6_MIN_MTU)
328 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
331 memset(ndev, 0, sizeof(struct inet6_dev));
333 rwlock_init(&ndev->lock);
335 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
336 ndev->cnf.mtu6 = dev->mtu;
337 ndev->cnf.sysctl = NULL;
338 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
339 if (ndev->nd_parms == NULL) {
343 /* We refer to the device */
346 if (snmp6_alloc_dev(ndev) < 0) {
348 "%s(): cannot allocate memory for statistics; dev=%s.\n",
349 __FUNCTION__, dev->name));
350 neigh_parms_release(&nd_tbl, ndev->nd_parms);
352 in6_dev_finish_destroy(ndev);
356 if (snmp6_register_dev(ndev) < 0) {
358 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
359 __FUNCTION__, dev->name));
360 neigh_parms_release(&nd_tbl, ndev->nd_parms);
362 in6_dev_finish_destroy(ndev);
366 /* One reference from device. We must do this before
367 * we invoke __ipv6_regen_rndid().
371 #ifdef CONFIG_IPV6_PRIVACY
372 get_random_bytes(ndev->rndid, sizeof(ndev->rndid));
373 get_random_bytes(ndev->entropy, sizeof(ndev->entropy));
374 init_timer(&ndev->regen_timer);
375 ndev->regen_timer.function = ipv6_regen_rndid;
376 ndev->regen_timer.data = (unsigned long) ndev;
377 if ((dev->flags&IFF_LOOPBACK) ||
378 dev->type == ARPHRD_TUNNEL ||
379 dev->type == ARPHRD_NONE ||
380 dev->type == ARPHRD_SIT) {
382 "%s: Disabled Privacy Extensions\n",
384 ndev->cnf.use_tempaddr = -1;
387 ipv6_regen_rndid((unsigned long) ndev);
391 if (netif_carrier_ok(dev))
392 ndev->if_flags |= IF_READY;
394 write_lock_bh(&addrconf_lock);
396 write_unlock_bh(&addrconf_lock);
398 ipv6_mc_init_dev(ndev);
399 ndev->tstamp = jiffies;
401 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
402 NET_IPV6_NEIGH, "ipv6",
403 &ndisc_ifinfo_sysctl_change,
405 addrconf_sysctl_register(ndev, &ndev->cnf);
411 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
413 struct inet6_dev *idev;
417 if ((idev = __in6_dev_get(dev)) == NULL) {
418 if ((idev = ipv6_add_dev(dev)) == NULL)
421 if (dev->flags&IFF_UP)
427 static void dev_forward_change(struct inet6_dev *idev)
429 struct net_device *dev;
430 struct inet6_ifaddr *ifa;
431 struct in6_addr addr;
436 if (dev && (dev->flags & IFF_MULTICAST)) {
437 ipv6_addr_all_routers(&addr);
439 if (idev->cnf.forwarding)
440 ipv6_dev_mc_inc(dev, &addr);
442 ipv6_dev_mc_dec(dev, &addr);
444 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
445 if (idev->cnf.forwarding)
446 addrconf_join_anycast(ifa);
448 addrconf_leave_anycast(ifa);
453 static void addrconf_forward_change(void)
455 struct net_device *dev;
456 struct inet6_dev *idev;
458 read_lock(&dev_base_lock);
459 for (dev=dev_base; dev; dev=dev->next) {
460 read_lock(&addrconf_lock);
461 idev = __in6_dev_get(dev);
463 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
464 idev->cnf.forwarding = ipv6_devconf.forwarding;
466 dev_forward_change(idev);
468 read_unlock(&addrconf_lock);
470 read_unlock(&dev_base_lock);
474 /* Nobody refers to this ifaddr, destroy it */
476 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
478 BUG_TRAP(ifp->if_next==NULL);
479 BUG_TRAP(ifp->lst_next==NULL);
480 #ifdef NET_REFCNT_DEBUG
481 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
484 in6_dev_put(ifp->idev);
486 if (del_timer(&ifp->timer))
487 printk("Timer is still running, when freeing ifa=%p\n", ifp);
490 printk("Freeing alive inet6 address %p\n", ifp);
493 dst_release(&ifp->rt->u.dst);
498 /* On success it returns ifp with increased reference count */
500 static struct inet6_ifaddr *
501 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
502 int scope, u32 flags)
504 struct inet6_ifaddr *ifa = NULL;
509 read_lock_bh(&addrconf_lock);
511 err = -ENODEV; /*XXX*/
515 write_lock(&addrconf_hash_lock);
517 /* Ignore adding duplicate addresses on an interface */
518 if (ipv6_chk_same_addr(addr, idev->dev)) {
519 ADBG(("ipv6_add_addr: already assigned\n"));
524 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
527 ADBG(("ipv6_add_addr: malloc failed\n"));
532 rt = addrconf_dst_alloc(idev, addr, 0);
538 memset(ifa, 0, sizeof(struct inet6_ifaddr));
539 ipv6_addr_copy(&ifa->addr, addr);
541 spin_lock_init(&ifa->lock);
542 init_timer(&ifa->timer);
543 ifa->timer.data = (unsigned long) ifa;
545 ifa->prefix_len = pfxlen;
546 ifa->flags = flags | IFA_F_TENTATIVE;
547 ifa->cstamp = ifa->tstamp = jiffies;
554 /* Add to big hash table */
555 hash = ipv6_addr_hash(addr);
557 ifa->lst_next = inet6_addr_lst[hash];
558 inet6_addr_lst[hash] = ifa;
560 write_unlock(&addrconf_hash_lock);
562 write_lock(&idev->lock);
563 /* Add to inet6_dev unicast addr list. */
564 ifa->if_next = idev->addr_list;
565 idev->addr_list = ifa;
567 #ifdef CONFIG_IPV6_PRIVACY
568 if (ifa->flags&IFA_F_TEMPORARY) {
569 ifa->tmp_next = idev->tempaddr_list;
570 idev->tempaddr_list = ifa;
578 write_unlock(&idev->lock);
580 read_unlock_bh(&addrconf_lock);
582 if (likely(err == 0))
583 notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
591 write_unlock(&addrconf_hash_lock);
595 /* This function wants to get referenced ifp and releases it before return */
597 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
599 struct inet6_ifaddr *ifa, **ifap;
600 struct inet6_dev *idev = ifp->idev;
602 int deleted = 0, onlink = 0;
603 unsigned long expires = jiffies;
605 hash = ipv6_addr_hash(&ifp->addr);
609 write_lock_bh(&addrconf_hash_lock);
610 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
611 ifap = &ifa->lst_next) {
613 *ifap = ifa->lst_next;
615 ifa->lst_next = NULL;
619 write_unlock_bh(&addrconf_hash_lock);
621 write_lock_bh(&idev->lock);
622 #ifdef CONFIG_IPV6_PRIVACY
623 if (ifp->flags&IFA_F_TEMPORARY) {
624 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
625 ifap = &ifa->tmp_next) {
627 *ifap = ifa->tmp_next;
629 in6_ifa_put(ifp->ifpub);
633 ifa->tmp_next = NULL;
640 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;
641 ifap = &ifa->if_next) {
643 *ifap = ifa->if_next;
646 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
649 } else if (ifp->flags & IFA_F_PERMANENT) {
650 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
652 if (ifa->flags & IFA_F_PERMANENT) {
657 unsigned long lifetime;
662 spin_lock(&ifa->lock);
663 lifetime = min_t(unsigned long,
664 ifa->valid_lft, 0x7fffffffUL/HZ);
665 if (time_before(expires,
666 ifa->tstamp + lifetime * HZ))
667 expires = ifa->tstamp + lifetime * HZ;
668 spin_unlock(&ifa->lock);
673 write_unlock_bh(&idev->lock);
675 ipv6_ifa_notify(RTM_DELADDR, ifp);
677 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
679 addrconf_del_timer(ifp);
682 * Purge or update corresponding prefix
684 * 1) we don't purge prefix here if address was not permanent.
685 * prefix is managed by its own lifetime.
686 * 2) if there're no addresses, delete prefix.
687 * 3) if there're still other permanent address(es),
688 * corresponding prefix is still permanent.
689 * 4) otherwise, update prefix lifetime to the
690 * longest valid lifetime among the corresponding
691 * addresses on the device.
692 * Note: subsequent RA will update lifetime.
696 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
697 struct in6_addr prefix;
700 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
701 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
703 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
705 ip6_del_rt(rt, NULL, NULL, NULL);
707 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
708 rt->rt6i_expires = expires;
709 rt->rt6i_flags |= RTF_EXPIRES;
712 dst_release(&rt->u.dst);
718 #ifdef CONFIG_IPV6_PRIVACY
719 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
721 struct inet6_dev *idev = ifp->idev;
722 struct in6_addr addr, *tmpaddr;
723 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
728 write_lock(&idev->lock);
730 spin_lock_bh(&ift->lock);
731 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
732 spin_unlock_bh(&ift->lock);
739 if (idev->cnf.use_tempaddr <= 0) {
740 write_unlock(&idev->lock);
742 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
747 spin_lock_bh(&ifp->lock);
748 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
749 idev->cnf.use_tempaddr = -1; /*XXX*/
750 spin_unlock_bh(&ifp->lock);
751 write_unlock(&idev->lock);
753 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
759 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
760 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
761 spin_unlock_bh(&ifp->lock);
762 write_unlock(&idev->lock);
764 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
770 memcpy(&addr.s6_addr[8], idev->rndid, 8);
771 tmp_valid_lft = min_t(__u32,
773 idev->cnf.temp_valid_lft);
774 tmp_prefered_lft = min_t(__u32,
776 idev->cnf.temp_prefered_lft - desync_factor / HZ);
777 tmp_plen = ifp->prefix_len;
778 max_addresses = idev->cnf.max_addresses;
779 tmp_cstamp = ifp->cstamp;
780 tmp_tstamp = ifp->tstamp;
781 spin_unlock_bh(&ifp->lock);
783 write_unlock(&idev->lock);
784 ift = !max_addresses ||
785 ipv6_count_addresses(idev) < max_addresses ?
786 ipv6_add_addr(idev, &addr, tmp_plen,
787 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
788 if (!ift || IS_ERR(ift)) {
792 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
794 write_lock(&idev->lock);
798 spin_lock_bh(&ift->lock);
800 ift->valid_lft = tmp_valid_lft;
801 ift->prefered_lft = tmp_prefered_lft;
802 ift->cstamp = tmp_cstamp;
803 ift->tstamp = tmp_tstamp;
804 spin_unlock_bh(&ift->lock);
806 addrconf_dad_start(ift, 0);
815 * Choose an appropriate source address (RFC3484)
817 struct ipv6_saddr_score {
825 #define IPV6_SADDR_SCORE_LOCAL 0x0001
826 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
827 #define IPV6_SADDR_SCORE_HOA 0x0008
828 #define IPV6_SADDR_SCORE_OIF 0x0010
829 #define IPV6_SADDR_SCORE_LABEL 0x0020
830 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
832 static int inline ipv6_saddr_preferred(int type)
834 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
835 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
840 /* static matching label */
841 static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
844 * prefix (longest match) label
845 * -----------------------------
852 if (type & IPV6_ADDR_LOOPBACK)
854 else if (type & IPV6_ADDR_COMPATv4)
856 else if (type & IPV6_ADDR_MAPPED)
858 else if (addr->s6_addr16[0] == htons(0x2002))
863 int ipv6_dev_get_saddr(struct net_device *daddr_dev,
864 struct in6_addr *daddr, struct in6_addr *saddr)
866 struct ipv6_saddr_score hiscore;
867 struct inet6_ifaddr *ifa_result = NULL;
868 int daddr_type = __ipv6_addr_type(daddr);
869 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
870 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
871 struct net_device *dev;
873 memset(&hiscore, 0, sizeof(hiscore));
875 read_lock(&dev_base_lock);
876 read_lock(&addrconf_lock);
878 for (dev = dev_base; dev; dev=dev->next) {
879 struct inet6_dev *idev;
880 struct inet6_ifaddr *ifa;
882 /* Rule 0: Candidate Source Address (section 4)
883 * - multicast and link-local destination address,
884 * the set of candidate source address MUST only
885 * include addresses assigned to interfaces
886 * belonging to the same link as the outgoing
888 * (- For site-local destination addresses, the
889 * set of candidate source addresses MUST only
890 * include addresses assigned to interfaces
891 * belonging to the same site as the outgoing
894 if ((daddr_type & IPV6_ADDR_MULTICAST ||
895 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
896 daddr_dev && dev != daddr_dev)
899 idev = __in6_dev_get(dev);
903 read_lock_bh(&idev->lock);
904 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
905 struct ipv6_saddr_score score;
907 score.addr_type = __ipv6_addr_type(&ifa->addr);
909 /* Rule 0: Candidate Source Address (section 4)
910 * - In any case, anycast addresses, multicast
911 * addresses, and the unspecified address MUST
912 * NOT be included in a candidate set.
914 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
915 score.addr_type & IPV6_ADDR_MULTICAST)) {
916 LIMIT_NETDEBUG(KERN_DEBUG
917 "ADDRCONF: unspecified / multicast address"
918 "assigned as unicast address on %s",
928 if (ifa_result == NULL) {
929 /* record it if the first available entry */
933 /* Rule 1: Prefer same address */
934 if (hiscore.rule < 1) {
935 if (ipv6_addr_equal(&ifa_result->addr, daddr))
936 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
939 if (ipv6_addr_equal(&ifa->addr, daddr)) {
940 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
941 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
946 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
950 /* Rule 2: Prefer appropriate scope */
951 if (hiscore.rule < 2) {
952 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
955 score.scope = __ipv6_addr_src_scope(score.addr_type);
956 if (hiscore.scope < score.scope) {
957 if (hiscore.scope < daddr_scope) {
962 } else if (score.scope < hiscore.scope) {
963 if (score.scope < daddr_scope)
971 /* Rule 3: Avoid deprecated address */
972 if (hiscore.rule < 3) {
973 if (ipv6_saddr_preferred(hiscore.addr_type) ||
974 !(ifa_result->flags & IFA_F_DEPRECATED))
975 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
978 if (ipv6_saddr_preferred(score.addr_type) ||
979 !(ifa->flags & IFA_F_DEPRECATED)) {
980 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
981 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
986 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
990 /* Rule 4: Prefer home address -- not implemented yet */
991 if (hiscore.rule < 4)
994 /* Rule 5: Prefer outgoing interface */
995 if (hiscore.rule < 5) {
996 if (daddr_dev == NULL ||
997 daddr_dev == ifa_result->idev->dev)
998 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1001 if (daddr_dev == NULL ||
1002 daddr_dev == ifa->idev->dev) {
1003 score.attrs |= IPV6_SADDR_SCORE_OIF;
1004 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1009 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1013 /* Rule 6: Prefer matching label */
1014 if (hiscore.rule < 6) {
1015 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1016 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1019 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1020 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1021 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1026 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1030 #ifdef CONFIG_IPV6_PRIVACY
1031 /* Rule 7: Prefer public address
1032 * Note: prefer temprary address if use_tempaddr >= 2
1034 if (hiscore.rule < 7) {
1035 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1036 (ifa_result->idev->cnf.use_tempaddr >= 2))
1037 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1040 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1041 (ifa->idev->cnf.use_tempaddr >= 2)) {
1042 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1043 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1048 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1052 /* Rule 8: Use longest matching prefix */
1053 if (hiscore.rule < 8) {
1054 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
1057 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1058 if (score.matchlen > hiscore.matchlen) {
1063 else if (score.matchlen < hiscore.matchlen)
1067 /* Final Rule: choose first available one */
1071 in6_ifa_put(ifa_result);
1076 read_unlock_bh(&idev->lock);
1078 read_unlock(&addrconf_lock);
1079 read_unlock(&dev_base_lock);
1082 return -EADDRNOTAVAIL;
1084 ipv6_addr_copy(saddr, &ifa_result->addr);
1085 in6_ifa_put(ifa_result);
1090 int ipv6_get_saddr(struct dst_entry *dst,
1091 struct in6_addr *daddr, struct in6_addr *saddr)
1093 return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
1097 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1099 struct inet6_dev *idev;
1100 int err = -EADDRNOTAVAIL;
1102 read_lock(&addrconf_lock);
1103 if ((idev = __in6_dev_get(dev)) != NULL) {
1104 struct inet6_ifaddr *ifp;
1106 read_lock_bh(&idev->lock);
1107 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1108 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1109 ipv6_addr_copy(addr, &ifp->addr);
1114 read_unlock_bh(&idev->lock);
1116 read_unlock(&addrconf_lock);
1120 static int ipv6_count_addresses(struct inet6_dev *idev)
1123 struct inet6_ifaddr *ifp;
1125 read_lock_bh(&idev->lock);
1126 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1128 read_unlock_bh(&idev->lock);
1132 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1134 struct inet6_ifaddr * ifp;
1135 u8 hash = ipv6_addr_hash(addr);
1137 read_lock_bh(&addrconf_hash_lock);
1138 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1139 if (ipv6_addr_equal(&ifp->addr, addr) &&
1140 !(ifp->flags&IFA_F_TENTATIVE)) {
1141 if (dev == NULL || ifp->idev->dev == dev ||
1142 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1146 read_unlock_bh(&addrconf_hash_lock);
1151 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1153 struct inet6_ifaddr * ifp;
1154 u8 hash = ipv6_addr_hash(addr);
1156 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1157 if (ipv6_addr_equal(&ifp->addr, addr)) {
1158 if (dev == NULL || ifp->idev->dev == dev)
1165 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1167 struct inet6_ifaddr * ifp;
1168 u8 hash = ipv6_addr_hash(addr);
1170 read_lock_bh(&addrconf_hash_lock);
1171 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1172 if (ipv6_addr_equal(&ifp->addr, addr)) {
1173 if (dev == NULL || ifp->idev->dev == dev ||
1174 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1180 read_unlock_bh(&addrconf_hash_lock);
1185 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1187 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1188 const struct in6_addr *sk2_rcv_saddr6 = tcp_v6_rcv_saddr(sk2);
1189 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1190 u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1191 int sk_ipv6only = ipv6_only_sock(sk);
1192 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1193 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1194 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1196 if (!sk2_rcv_saddr && !sk_ipv6only)
1199 if (addr_type2 == IPV6_ADDR_ANY &&
1200 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1203 if (addr_type == IPV6_ADDR_ANY &&
1204 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1207 if (sk2_rcv_saddr6 &&
1208 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1211 if (addr_type == IPV6_ADDR_MAPPED &&
1213 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1219 /* Gets referenced address, destroys ifaddr */
1221 void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1223 if (ifp->flags&IFA_F_PERMANENT) {
1224 spin_lock_bh(&ifp->lock);
1225 addrconf_del_timer(ifp);
1226 ifp->flags |= IFA_F_TENTATIVE;
1227 spin_unlock_bh(&ifp->lock);
1229 #ifdef CONFIG_IPV6_PRIVACY
1230 } else if (ifp->flags&IFA_F_TEMPORARY) {
1231 struct inet6_ifaddr *ifpub;
1232 spin_lock_bh(&ifp->lock);
1235 in6_ifa_hold(ifpub);
1236 spin_unlock_bh(&ifp->lock);
1237 ipv6_create_tempaddr(ifpub, ifp);
1240 spin_unlock_bh(&ifp->lock);
1248 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1250 if (net_ratelimit())
1251 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1252 addrconf_dad_stop(ifp);
1255 /* Join to solicited addr multicast group. */
1257 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1259 struct in6_addr maddr;
1261 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1264 addrconf_addr_solict_mult(addr, &maddr);
1265 ipv6_dev_mc_inc(dev, &maddr);
1268 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1270 struct in6_addr maddr;
1272 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1275 addrconf_addr_solict_mult(addr, &maddr);
1276 __ipv6_dev_mc_dec(idev, &maddr);
1279 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1281 struct in6_addr addr;
1282 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1283 if (ipv6_addr_any(&addr))
1285 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1288 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1290 struct in6_addr addr;
1291 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1292 if (ipv6_addr_any(&addr))
1294 __ipv6_dev_ac_dec(ifp->idev, &addr);
1297 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1299 switch (dev->type) {
1302 case ARPHRD_IEEE802_TR:
1303 if (dev->addr_len != ETH_ALEN)
1305 memcpy(eui, dev->dev_addr, 3);
1306 memcpy(eui + 5, dev->dev_addr + 3, 3);
1309 * The zSeries OSA network cards can be shared among various
1310 * OS instances, but the OSA cards have only one MAC address.
1311 * This leads to duplicate address conflicts in conjunction
1312 * with IPv6 if more than one instance uses the same card.
1314 * The driver for these cards can deliver a unique 16-bit
1315 * identifier for each instance sharing the same card. It is
1316 * placed instead of 0xFFFE in the interface identifier. The
1317 * "u" bit of the interface identifier is not inverted in this
1318 * case. Hence the resulting interface identifier has local
1319 * scope according to RFC2373.
1322 eui[3] = (dev->dev_id >> 8) & 0xFF;
1323 eui[4] = dev->dev_id & 0xFF;
1331 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1332 if (dev->addr_len != ARCNET_ALEN)
1335 eui[7] = *(u8*)dev->dev_addr;
1337 case ARPHRD_INFINIBAND:
1338 if (dev->addr_len != INFINIBAND_ALEN)
1340 memcpy(eui, dev->dev_addr + 12, 8);
1347 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1350 struct inet6_ifaddr *ifp;
1352 read_lock_bh(&idev->lock);
1353 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1354 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1355 memcpy(eui, ifp->addr.s6_addr+8, 8);
1360 read_unlock_bh(&idev->lock);
1364 #ifdef CONFIG_IPV6_PRIVACY
1365 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1366 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1368 struct net_device *dev;
1369 struct scatterlist sg[2];
1371 sg_set_buf(&sg[0], idev->entropy, 8);
1372 sg_set_buf(&sg[1], idev->work_eui64, 8);
1376 if (ipv6_generate_eui64(idev->work_eui64, dev)) {
1378 "__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
1380 get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
1383 spin_lock(&md5_tfm_lock);
1384 if (unlikely(md5_tfm == NULL)) {
1385 spin_unlock(&md5_tfm_lock);
1388 crypto_digest_init(md5_tfm);
1389 crypto_digest_update(md5_tfm, sg, 2);
1390 crypto_digest_final(md5_tfm, idev->work_digest);
1391 spin_unlock(&md5_tfm_lock);
1393 memcpy(idev->rndid, &idev->work_digest[0], 8);
1394 idev->rndid[0] &= ~0x02;
1395 memcpy(idev->entropy, &idev->work_digest[8], 8);
1398 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1399 * check if generated address is not inappropriate
1401 * - Reserved subnet anycast (RFC 2526)
1402 * 11111101 11....11 1xxxxxxx
1403 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1404 * 00-00-5E-FE-xx-xx-xx-xx
1406 * - XXX: already assigned to an address on the device
1408 if (idev->rndid[0] == 0xfd &&
1409 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1410 (idev->rndid[7]&0x80))
1412 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1413 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1415 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1422 static void ipv6_regen_rndid(unsigned long data)
1424 struct inet6_dev *idev = (struct inet6_dev *) data;
1425 unsigned long expires;
1427 read_lock_bh(&addrconf_lock);
1428 write_lock_bh(&idev->lock);
1433 if (__ipv6_regen_rndid(idev) < 0)
1437 idev->cnf.temp_prefered_lft * HZ -
1438 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1439 if (time_before(expires, jiffies)) {
1441 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1446 if (!mod_timer(&idev->regen_timer, expires))
1450 write_unlock_bh(&idev->lock);
1451 read_unlock_bh(&addrconf_lock);
1455 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1458 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1459 ret = __ipv6_regen_rndid(idev);
1469 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1470 unsigned long expires, u32 flags)
1472 struct in6_rtmsg rtmsg;
1474 memset(&rtmsg, 0, sizeof(rtmsg));
1475 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1476 rtmsg.rtmsg_dst_len = plen;
1477 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1478 rtmsg.rtmsg_ifindex = dev->ifindex;
1479 rtmsg.rtmsg_info = expires;
1480 rtmsg.rtmsg_flags = RTF_UP|flags;
1481 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1483 /* Prevent useless cloning on PtP SIT.
1484 This thing is done here expecting that the whole
1485 class of non-broadcast devices need not cloning.
1487 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1488 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1490 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1493 /* Create "default" multicast route to the interface */
1495 static void addrconf_add_mroute(struct net_device *dev)
1497 struct in6_rtmsg rtmsg;
1499 memset(&rtmsg, 0, sizeof(rtmsg));
1500 ipv6_addr_set(&rtmsg.rtmsg_dst,
1501 htonl(0xFF000000), 0, 0, 0);
1502 rtmsg.rtmsg_dst_len = 8;
1503 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1504 rtmsg.rtmsg_ifindex = dev->ifindex;
1505 rtmsg.rtmsg_flags = RTF_UP;
1506 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1507 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1510 static void sit_route_add(struct net_device *dev)
1512 struct in6_rtmsg rtmsg;
1514 memset(&rtmsg, 0, sizeof(rtmsg));
1516 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1517 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1519 /* prefix length - 96 bits "::d.d.d.d" */
1520 rtmsg.rtmsg_dst_len = 96;
1521 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1522 rtmsg.rtmsg_ifindex = dev->ifindex;
1524 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1527 static void addrconf_add_lroute(struct net_device *dev)
1529 struct in6_addr addr;
1531 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1532 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1535 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1537 struct inet6_dev *idev;
1541 if ((idev = ipv6_find_idev(dev)) == NULL)
1544 /* Add default multicast route */
1545 addrconf_add_mroute(dev);
1547 /* Add link local route */
1548 addrconf_add_lroute(dev);
1552 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1554 struct prefix_info *pinfo;
1558 unsigned long rt_expires;
1559 struct inet6_dev *in6_dev;
1561 pinfo = (struct prefix_info *) opt;
1563 if (len < sizeof(struct prefix_info)) {
1564 ADBG(("addrconf: prefix option too short\n"));
1569 * Validation checks ([ADDRCONF], page 19)
1572 addr_type = ipv6_addr_type(&pinfo->prefix);
1574 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1577 valid_lft = ntohl(pinfo->valid);
1578 prefered_lft = ntohl(pinfo->prefered);
1580 if (prefered_lft > valid_lft) {
1581 if (net_ratelimit())
1582 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1586 in6_dev = in6_dev_get(dev);
1588 if (in6_dev == NULL) {
1589 if (net_ratelimit())
1590 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1595 * Two things going on here:
1596 * 1) Add routes for on-link prefixes
1597 * 2) Configure prefixes with the auto flag set
1600 /* Avoid arithmetic overflow. Really, we could
1601 save rt_expires in seconds, likely valid_lft,
1602 but it would require division in fib gc, that it
1605 if (valid_lft >= 0x7FFFFFFF/HZ)
1606 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1608 rt_expires = valid_lft * HZ;
1611 * We convert this (in jiffies) to clock_t later.
1612 * Avoid arithmetic overflow there as well.
1613 * Overflow can happen only if HZ < USER_HZ.
1615 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1616 rt_expires = 0x7FFFFFFF / USER_HZ;
1618 if (pinfo->onlink) {
1619 struct rt6_info *rt;
1620 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1622 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1623 if (rt->rt6i_flags&RTF_EXPIRES) {
1624 if (valid_lft == 0) {
1625 ip6_del_rt(rt, NULL, NULL, NULL);
1628 rt->rt6i_expires = jiffies + rt_expires;
1631 } else if (valid_lft) {
1632 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1633 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1636 dst_release(&rt->u.dst);
1639 /* Try to figure out our local address for this prefix */
1641 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1642 struct inet6_ifaddr * ifp;
1643 struct in6_addr addr;
1644 int create = 0, update_lft = 0;
1646 if (pinfo->prefix_len == 64) {
1647 memcpy(&addr, &pinfo->prefix, 8);
1648 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1649 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1650 in6_dev_put(in6_dev);
1655 if (net_ratelimit())
1656 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1658 in6_dev_put(in6_dev);
1663 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1665 if (ifp == NULL && valid_lft) {
1666 int max_addresses = in6_dev->cnf.max_addresses;
1668 /* Do not allow to create too much of autoconfigured
1669 * addresses; this would be too easy way to crash kernel.
1671 if (!max_addresses ||
1672 ipv6_count_addresses(in6_dev) < max_addresses)
1673 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1674 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1676 if (!ifp || IS_ERR(ifp)) {
1677 in6_dev_put(in6_dev);
1681 update_lft = create = 1;
1682 ifp->cstamp = jiffies;
1683 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1689 #ifdef CONFIG_IPV6_PRIVACY
1690 struct inet6_ifaddr *ift;
1694 /* update lifetime (RFC2462 5.5.3 e) */
1695 spin_lock(&ifp->lock);
1697 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1698 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1701 if (!update_lft && stored_lft) {
1702 if (valid_lft > MIN_VALID_LIFETIME ||
1703 valid_lft > stored_lft)
1705 else if (stored_lft <= MIN_VALID_LIFETIME) {
1706 /* valid_lft <= stored_lft is always true */
1710 valid_lft = MIN_VALID_LIFETIME;
1711 if (valid_lft < prefered_lft)
1712 prefered_lft = valid_lft;
1718 ifp->valid_lft = valid_lft;
1719 ifp->prefered_lft = prefered_lft;
1722 ifp->flags &= ~IFA_F_DEPRECATED;
1723 spin_unlock(&ifp->lock);
1725 if (!(flags&IFA_F_TENTATIVE))
1726 ipv6_ifa_notify(0, ifp);
1728 spin_unlock(&ifp->lock);
1730 #ifdef CONFIG_IPV6_PRIVACY
1731 read_lock_bh(&in6_dev->lock);
1732 /* update all temporary addresses in the list */
1733 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1735 * When adjusting the lifetimes of an existing
1736 * temporary address, only lower the lifetimes.
1737 * Implementations must not increase the
1738 * lifetimes of an existing temporary address
1739 * when processing a Prefix Information Option.
1741 spin_lock(&ift->lock);
1743 if (ift->valid_lft > valid_lft &&
1744 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1745 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1746 if (ift->prefered_lft > prefered_lft &&
1747 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1748 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1749 spin_unlock(&ift->lock);
1750 if (!(flags&IFA_F_TENTATIVE))
1751 ipv6_ifa_notify(0, ift);
1754 if (create && in6_dev->cnf.use_tempaddr > 0) {
1756 * When a new public address is created as described in [ADDRCONF],
1757 * also create a new temporary address.
1759 read_unlock_bh(&in6_dev->lock);
1760 ipv6_create_tempaddr(ifp, NULL);
1762 read_unlock_bh(&in6_dev->lock);
1769 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1770 in6_dev_put(in6_dev);
1774 * Set destination address.
1775 * Special case for SIT interfaces where we create a new "virtual"
1778 int addrconf_set_dstaddr(void __user *arg)
1780 struct in6_ifreq ireq;
1781 struct net_device *dev;
1787 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1790 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1796 if (dev->type == ARPHRD_SIT) {
1799 struct ip_tunnel_parm p;
1801 err = -EADDRNOTAVAIL;
1802 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1805 memset(&p, 0, sizeof(p));
1806 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1810 p.iph.protocol = IPPROTO_IPV6;
1812 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1814 oldfs = get_fs(); set_fs(KERNEL_DS);
1815 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1820 if ((dev = __dev_get_by_name(p.name)) == NULL)
1822 err = dev_open(dev);
1832 * Manual configuration of address on an interface
1834 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1836 struct inet6_ifaddr *ifp;
1837 struct inet6_dev *idev;
1838 struct net_device *dev;
1843 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1846 if (!(dev->flags&IFF_UP))
1849 if ((idev = addrconf_add_dev(dev)) == NULL)
1852 scope = ipv6_addr_scope(pfx);
1854 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1856 addrconf_dad_start(ifp, 0);
1861 return PTR_ERR(ifp);
1864 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1866 struct inet6_ifaddr *ifp;
1867 struct inet6_dev *idev;
1868 struct net_device *dev;
1870 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1873 if ((idev = __in6_dev_get(dev)) == NULL)
1876 read_lock_bh(&idev->lock);
1877 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1878 if (ifp->prefix_len == plen &&
1879 ipv6_addr_equal(pfx, &ifp->addr)) {
1881 read_unlock_bh(&idev->lock);
1885 /* If the last address is deleted administratively,
1886 disable IPv6 on this interface.
1888 if (idev->addr_list == NULL)
1889 addrconf_ifdown(idev->dev, 1);
1893 read_unlock_bh(&idev->lock);
1894 return -EADDRNOTAVAIL;
1898 int addrconf_add_ifaddr(void __user *arg)
1900 struct in6_ifreq ireq;
1903 if (!capable(CAP_NET_ADMIN))
1906 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1910 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1915 int addrconf_del_ifaddr(void __user *arg)
1917 struct in6_ifreq ireq;
1920 if (!capable(CAP_NET_ADMIN))
1923 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1927 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1932 static void sit_add_v4_addrs(struct inet6_dev *idev)
1934 struct inet6_ifaddr * ifp;
1935 struct in6_addr addr;
1936 struct net_device *dev;
1941 memset(&addr, 0, sizeof(struct in6_addr));
1942 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1944 if (idev->dev->flags&IFF_POINTOPOINT) {
1945 addr.s6_addr32[0] = htonl(0xfe800000);
1948 scope = IPV6_ADDR_COMPATv4;
1951 if (addr.s6_addr32[3]) {
1952 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1954 spin_lock_bh(&ifp->lock);
1955 ifp->flags &= ~IFA_F_TENTATIVE;
1956 spin_unlock_bh(&ifp->lock);
1957 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1963 for (dev = dev_base; dev != NULL; dev = dev->next) {
1964 struct in_device * in_dev = __in_dev_get_rtnl(dev);
1965 if (in_dev && (dev->flags & IFF_UP)) {
1966 struct in_ifaddr * ifa;
1970 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1973 addr.s6_addr32[3] = ifa->ifa_local;
1975 if (ifa->ifa_scope == RT_SCOPE_LINK)
1977 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1978 if (idev->dev->flags&IFF_POINTOPOINT)
1982 if (idev->dev->flags&IFF_POINTOPOINT)
1987 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1990 spin_lock_bh(&ifp->lock);
1991 ifp->flags &= ~IFA_F_TENTATIVE;
1992 spin_unlock_bh(&ifp->lock);
1993 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2001 static void init_loopback(struct net_device *dev)
2003 struct inet6_dev *idev;
2004 struct inet6_ifaddr * ifp;
2010 if ((idev = ipv6_find_idev(dev)) == NULL) {
2011 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2015 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2017 spin_lock_bh(&ifp->lock);
2018 ifp->flags &= ~IFA_F_TENTATIVE;
2019 spin_unlock_bh(&ifp->lock);
2020 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2025 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2027 struct inet6_ifaddr * ifp;
2029 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2031 addrconf_dad_start(ifp, 0);
2036 static void addrconf_dev_config(struct net_device *dev)
2038 struct in6_addr addr;
2039 struct inet6_dev * idev;
2043 if ((dev->type != ARPHRD_ETHER) &&
2044 (dev->type != ARPHRD_FDDI) &&
2045 (dev->type != ARPHRD_IEEE802_TR) &&
2046 (dev->type != ARPHRD_ARCNET) &&
2047 (dev->type != ARPHRD_INFINIBAND)) {
2048 /* Alas, we support only Ethernet autoconfiguration. */
2052 idev = addrconf_add_dev(dev);
2056 memset(&addr, 0, sizeof(struct in6_addr));
2057 addr.s6_addr32[0] = htonl(0xFE800000);
2059 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2060 addrconf_add_linklocal(idev, &addr);
2063 static void addrconf_sit_config(struct net_device *dev)
2065 struct inet6_dev *idev;
2070 * Configure the tunnel with one of our IPv4
2071 * addresses... we should configure all of
2072 * our v4 addrs in the tunnel
2075 if ((idev = ipv6_find_idev(dev)) == NULL) {
2076 printk(KERN_DEBUG "init sit: add_dev failed\n");
2080 sit_add_v4_addrs(idev);
2082 if (dev->flags&IFF_POINTOPOINT) {
2083 addrconf_add_mroute(dev);
2084 addrconf_add_lroute(dev);
2090 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2092 struct in6_addr lladdr;
2094 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2095 addrconf_add_linklocal(idev, &lladdr);
2101 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2103 struct net_device *link_dev;
2105 /* first try to inherit the link-local address from the link device */
2106 if (idev->dev->iflink &&
2107 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2108 if (!ipv6_inherit_linklocal(idev, link_dev))
2111 /* then try to inherit it from any device */
2112 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2113 if (!ipv6_inherit_linklocal(idev, link_dev))
2116 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2120 * Autoconfigure tunnel with a link-local address so routing protocols,
2121 * DHCPv6, MLD etc. can be run over the virtual link
2124 static void addrconf_ip6_tnl_config(struct net_device *dev)
2126 struct inet6_dev *idev;
2130 if ((idev = addrconf_add_dev(dev)) == NULL) {
2131 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2134 ip6_tnl_add_linklocal(idev);
2135 addrconf_add_mroute(dev);
2138 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2141 struct net_device *dev = (struct net_device *) data;
2142 struct inet6_dev *idev = __in6_dev_get(dev);
2147 if (event == NETDEV_UP) {
2148 if (!netif_carrier_ok(dev)) {
2149 /* device is not ready yet. */
2151 "ADDRCONF(NETDEV_UP): %s: "
2152 "link is not ready\n",
2157 if (!netif_carrier_ok(dev)) {
2158 /* device is still not ready. */
2163 if (idev->if_flags & IF_READY) {
2164 /* device is already configured. */
2167 idev->if_flags |= IF_READY;
2171 "ADDRCONF(NETDEV_CHANGE): %s: "
2172 "link becomes ready\n",
2179 addrconf_sit_config(dev);
2181 case ARPHRD_TUNNEL6:
2182 addrconf_ip6_tnl_config(dev);
2184 case ARPHRD_LOOPBACK:
2189 addrconf_dev_config(dev);
2193 /* If the MTU changed during the interface down, when the
2194 interface up, the changed MTU must be reflected in the
2195 idev as well as routers.
2197 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2198 rt6_mtu_change(dev, dev->mtu);
2199 idev->cnf.mtu6 = dev->mtu;
2201 idev->tstamp = jiffies;
2202 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2203 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2204 stop IPv6 on this interface.
2206 if (dev->mtu < IPV6_MIN_MTU)
2207 addrconf_ifdown(dev, event != NETDEV_DOWN);
2211 case NETDEV_CHANGEMTU:
2212 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2213 rt6_mtu_change(dev, dev->mtu);
2214 idev->cnf.mtu6 = dev->mtu;
2218 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2221 case NETDEV_UNREGISTER:
2223 * Remove all addresses from this interface.
2225 addrconf_ifdown(dev, event != NETDEV_DOWN);
2228 case NETDEV_CHANGENAME:
2229 #ifdef CONFIG_SYSCTL
2231 addrconf_sysctl_unregister(&idev->cnf);
2232 neigh_sysctl_unregister(idev->nd_parms);
2233 neigh_sysctl_register(dev, idev->nd_parms,
2234 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2235 &ndisc_ifinfo_sysctl_change,
2237 addrconf_sysctl_register(idev, &idev->cnf);
2247 * addrconf module should be notified of a device going up
2249 static struct notifier_block ipv6_dev_notf = {
2250 .notifier_call = addrconf_notify,
2254 static int addrconf_ifdown(struct net_device *dev, int how)
2256 struct inet6_dev *idev;
2257 struct inet6_ifaddr *ifa, **bifa;
2262 if (dev == &loopback_dev && how == 1)
2266 neigh_ifdown(&nd_tbl, dev);
2268 idev = __in6_dev_get(dev);
2272 /* Step 1: remove reference to ipv6 device from parent device.
2276 write_lock_bh(&addrconf_lock);
2277 dev->ip6_ptr = NULL;
2279 write_unlock_bh(&addrconf_lock);
2281 /* Step 1.5: remove snmp6 entry */
2282 snmp6_unregister_dev(idev);
2286 /* Step 2: clear hash table */
2287 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2288 bifa = &inet6_addr_lst[i];
2290 write_lock_bh(&addrconf_hash_lock);
2291 while ((ifa = *bifa) != NULL) {
2292 if (ifa->idev == idev) {
2293 *bifa = ifa->lst_next;
2294 ifa->lst_next = NULL;
2295 addrconf_del_timer(ifa);
2299 bifa = &ifa->lst_next;
2301 write_unlock_bh(&addrconf_hash_lock);
2304 write_lock_bh(&idev->lock);
2306 /* Step 3: clear flags for stateless addrconf */
2308 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2310 /* Step 4: clear address list */
2311 #ifdef CONFIG_IPV6_PRIVACY
2312 if (how == 1 && del_timer(&idev->regen_timer))
2315 /* clear tempaddr list */
2316 while ((ifa = idev->tempaddr_list) != NULL) {
2317 idev->tempaddr_list = ifa->tmp_next;
2318 ifa->tmp_next = NULL;
2320 write_unlock_bh(&idev->lock);
2321 spin_lock_bh(&ifa->lock);
2324 in6_ifa_put(ifa->ifpub);
2327 spin_unlock_bh(&ifa->lock);
2329 write_lock_bh(&idev->lock);
2332 while ((ifa = idev->addr_list) != NULL) {
2333 idev->addr_list = ifa->if_next;
2334 ifa->if_next = NULL;
2336 addrconf_del_timer(ifa);
2337 write_unlock_bh(&idev->lock);
2339 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2342 write_lock_bh(&idev->lock);
2344 write_unlock_bh(&idev->lock);
2346 /* Step 5: Discard multicast list */
2349 ipv6_mc_destroy_dev(idev);
2353 /* Step 5: netlink notification of this interface */
2354 idev->tstamp = jiffies;
2355 inet6_ifinfo_notify(RTM_DELLINK, idev);
2357 /* Shot the device (if unregistered) */
2360 #ifdef CONFIG_SYSCTL
2361 addrconf_sysctl_unregister(&idev->cnf);
2362 neigh_sysctl_unregister(idev->nd_parms);
2364 neigh_parms_release(&nd_tbl, idev->nd_parms);
2365 neigh_ifdown(&nd_tbl, dev);
2371 static void addrconf_rs_timer(unsigned long data)
2373 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2375 if (ifp->idev->cnf.forwarding)
2378 if (ifp->idev->if_flags & IF_RA_RCVD) {
2380 * Announcement received after solicitation
2386 spin_lock(&ifp->lock);
2387 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2388 struct in6_addr all_routers;
2390 /* The wait after the last probe can be shorter */
2391 addrconf_mod_timer(ifp, AC_RS,
2392 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2393 ifp->idev->cnf.rtr_solicit_delay :
2394 ifp->idev->cnf.rtr_solicit_interval);
2395 spin_unlock(&ifp->lock);
2397 ipv6_addr_all_routers(&all_routers);
2399 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2401 spin_unlock(&ifp->lock);
2403 * Note: we do not support deprecated "all on-link"
2404 * assumption any longer.
2406 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2407 ifp->idev->dev->name);
2415 * Duplicate Address Detection
2417 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2419 unsigned long rand_num;
2420 struct inet6_dev *idev = ifp->idev;
2422 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2423 ifp->probes = idev->cnf.dad_transmits;
2424 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2427 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2429 struct inet6_dev *idev = ifp->idev;
2430 struct net_device *dev = idev->dev;
2432 addrconf_join_solict(dev, &ifp->addr);
2434 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2435 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2438 net_srandom(ifp->addr.s6_addr32[3]);
2440 read_lock_bh(&idev->lock);
2443 spin_lock_bh(&ifp->lock);
2445 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2446 !(ifp->flags&IFA_F_TENTATIVE)) {
2447 ifp->flags &= ~IFA_F_TENTATIVE;
2448 spin_unlock_bh(&ifp->lock);
2449 read_unlock_bh(&idev->lock);
2451 addrconf_dad_completed(ifp);
2455 if (idev->if_flags & IF_READY)
2456 addrconf_dad_kick(ifp);
2459 * If the defice is not ready:
2460 * - keep it tentative if it is a permanent address.
2461 * - otherwise, kill it.
2464 addrconf_dad_stop(ifp);
2467 spin_unlock_bh(&ifp->lock);
2469 read_unlock_bh(&idev->lock);
2472 static void addrconf_dad_timer(unsigned long data)
2474 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2475 struct inet6_dev *idev = ifp->idev;
2476 struct in6_addr unspec;
2477 struct in6_addr mcaddr;
2479 read_lock_bh(&idev->lock);
2481 read_unlock_bh(&idev->lock);
2484 spin_lock_bh(&ifp->lock);
2485 if (ifp->probes == 0) {
2487 * DAD was successful
2490 ifp->flags &= ~IFA_F_TENTATIVE;
2491 spin_unlock_bh(&ifp->lock);
2492 read_unlock_bh(&idev->lock);
2494 addrconf_dad_completed(ifp);
2500 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2501 spin_unlock_bh(&ifp->lock);
2502 read_unlock_bh(&idev->lock);
2504 /* send a neighbour solicitation for our addr */
2505 memset(&unspec, 0, sizeof(unspec));
2506 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2507 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2512 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2514 struct net_device * dev = ifp->idev->dev;
2517 * Configure the address for reception. Now it is valid.
2520 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2522 /* If added prefix is link local and forwarding is off,
2523 start sending router solicitations.
2526 if (ifp->idev->cnf.forwarding == 0 &&
2527 ifp->idev->cnf.rtr_solicits > 0 &&
2528 (dev->flags&IFF_LOOPBACK) == 0 &&
2529 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2530 struct in6_addr all_routers;
2532 ipv6_addr_all_routers(&all_routers);
2535 * If a host as already performed a random delay
2536 * [...] as part of DAD [...] there is no need
2537 * to delay again before sending the first RS
2539 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2541 spin_lock_bh(&ifp->lock);
2543 ifp->idev->if_flags |= IF_RS_SENT;
2544 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2545 spin_unlock_bh(&ifp->lock);
2549 #ifdef CONFIG_PROC_FS
2550 struct if6_iter_state {
2554 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2556 struct inet6_ifaddr *ifa = NULL;
2557 struct if6_iter_state *state = seq->private;
2559 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2560 ifa = inet6_addr_lst[state->bucket];
2567 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2569 struct if6_iter_state *state = seq->private;
2571 ifa = ifa->lst_next;
2573 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2574 ifa = inet6_addr_lst[state->bucket];
2580 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2582 struct inet6_ifaddr *ifa = if6_get_first(seq);
2585 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2587 return pos ? NULL : ifa;
2590 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2592 read_lock_bh(&addrconf_hash_lock);
2593 return if6_get_idx(seq, *pos);
2596 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2598 struct inet6_ifaddr *ifa;
2600 ifa = if6_get_next(seq, v);
2605 static void if6_seq_stop(struct seq_file *seq, void *v)
2607 read_unlock_bh(&addrconf_hash_lock);
2610 static int if6_seq_show(struct seq_file *seq, void *v)
2612 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2614 "%04x%04x%04x%04x%04x%04x%04x%04x %02x %02x %02x %02x %8s\n",
2616 ifp->idev->dev->ifindex,
2620 ifp->idev->dev->name);
2624 static struct seq_operations if6_seq_ops = {
2625 .start = if6_seq_start,
2626 .next = if6_seq_next,
2627 .show = if6_seq_show,
2628 .stop = if6_seq_stop,
2631 static int if6_seq_open(struct inode *inode, struct file *file)
2633 struct seq_file *seq;
2635 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2639 memset(s, 0, sizeof(*s));
2641 rc = seq_open(file, &if6_seq_ops);
2645 seq = file->private_data;
2654 static struct file_operations if6_fops = {
2655 .owner = THIS_MODULE,
2656 .open = if6_seq_open,
2658 .llseek = seq_lseek,
2659 .release = seq_release_private,
2662 int __init if6_proc_init(void)
2664 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2669 void if6_proc_exit(void)
2671 proc_net_remove("if_inet6");
2673 #endif /* CONFIG_PROC_FS */
2676 * Periodic address status verification
2679 static void addrconf_verify(unsigned long foo)
2681 struct inet6_ifaddr *ifp;
2682 unsigned long now, next;
2685 spin_lock_bh(&addrconf_verify_lock);
2687 next = now + ADDR_CHECK_FREQUENCY;
2689 del_timer(&addr_chk_timer);
2691 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2694 read_lock(&addrconf_hash_lock);
2695 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2697 #ifdef CONFIG_IPV6_PRIVACY
2698 unsigned long regen_advance;
2701 if (ifp->flags & IFA_F_PERMANENT)
2704 spin_lock(&ifp->lock);
2705 age = (now - ifp->tstamp) / HZ;
2707 #ifdef CONFIG_IPV6_PRIVACY
2708 regen_advance = ifp->idev->cnf.regen_max_retry *
2709 ifp->idev->cnf.dad_transmits *
2710 ifp->idev->nd_parms->retrans_time / HZ;
2713 if (age >= ifp->valid_lft) {
2714 spin_unlock(&ifp->lock);
2716 read_unlock(&addrconf_hash_lock);
2719 } else if (age >= ifp->prefered_lft) {
2720 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2723 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2725 ifp->flags |= IFA_F_DEPRECATED;
2728 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2729 next = ifp->tstamp + ifp->valid_lft * HZ;
2731 spin_unlock(&ifp->lock);
2735 read_unlock(&addrconf_hash_lock);
2737 ipv6_ifa_notify(0, ifp);
2741 #ifdef CONFIG_IPV6_PRIVACY
2742 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2743 !(ifp->flags&IFA_F_TENTATIVE)) {
2744 if (age >= ifp->prefered_lft - regen_advance) {
2745 struct inet6_ifaddr *ifpub = ifp->ifpub;
2746 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2747 next = ifp->tstamp + ifp->prefered_lft * HZ;
2748 if (!ifp->regen_count && ifpub) {
2751 in6_ifa_hold(ifpub);
2752 spin_unlock(&ifp->lock);
2753 read_unlock(&addrconf_hash_lock);
2754 ipv6_create_tempaddr(ifpub, ifp);
2759 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2760 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2761 spin_unlock(&ifp->lock);
2764 /* ifp->prefered_lft <= ifp->valid_lft */
2765 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2766 next = ifp->tstamp + ifp->prefered_lft * HZ;
2767 spin_unlock(&ifp->lock);
2770 read_unlock(&addrconf_hash_lock);
2773 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2774 add_timer(&addr_chk_timer);
2775 spin_unlock_bh(&addrconf_verify_lock);
2779 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2781 struct rtattr **rta = arg;
2782 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2783 struct in6_addr *pfx;
2786 if (rta[IFA_ADDRESS-1]) {
2787 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2789 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2791 if (rta[IFA_LOCAL-1]) {
2792 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2794 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2799 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2803 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2805 struct rtattr **rta = arg;
2806 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2807 struct in6_addr *pfx;
2810 if (rta[IFA_ADDRESS-1]) {
2811 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2813 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2815 if (rta[IFA_LOCAL-1]) {
2816 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2818 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2823 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2826 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2827 u32 pid, u32 seq, int event, unsigned int flags)
2829 struct ifaddrmsg *ifm;
2830 struct nlmsghdr *nlh;
2831 struct ifa_cacheinfo ci;
2832 unsigned char *b = skb->tail;
2834 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2835 ifm = NLMSG_DATA(nlh);
2836 ifm->ifa_family = AF_INET6;
2837 ifm->ifa_prefixlen = ifa->prefix_len;
2838 ifm->ifa_flags = ifa->flags;
2839 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2840 if (ifa->scope&IFA_HOST)
2841 ifm->ifa_scope = RT_SCOPE_HOST;
2842 else if (ifa->scope&IFA_LINK)
2843 ifm->ifa_scope = RT_SCOPE_LINK;
2844 else if (ifa->scope&IFA_SITE)
2845 ifm->ifa_scope = RT_SCOPE_SITE;
2846 ifm->ifa_index = ifa->idev->dev->ifindex;
2847 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2848 if (!(ifa->flags&IFA_F_PERMANENT)) {
2849 ci.ifa_prefered = ifa->prefered_lft;
2850 ci.ifa_valid = ifa->valid_lft;
2851 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2852 long tval = (jiffies - ifa->tstamp)/HZ;
2853 ci.ifa_prefered -= tval;
2854 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2855 ci.ifa_valid -= tval;
2858 ci.ifa_prefered = INFINITY_LIFE_TIME;
2859 ci.ifa_valid = INFINITY_LIFE_TIME;
2861 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2862 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2863 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2864 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2865 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2866 nlh->nlmsg_len = skb->tail - b;
2871 skb_trim(skb, b - skb->data);
2875 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2876 u32 pid, u32 seq, int event, u16 flags)
2878 struct ifaddrmsg *ifm;
2879 struct nlmsghdr *nlh;
2880 struct ifa_cacheinfo ci;
2881 unsigned char *b = skb->tail;
2883 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2884 ifm = NLMSG_DATA(nlh);
2885 ifm->ifa_family = AF_INET6;
2886 ifm->ifa_prefixlen = 128;
2887 ifm->ifa_flags = IFA_F_PERMANENT;
2888 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2889 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2890 ifm->ifa_scope = RT_SCOPE_SITE;
2891 ifm->ifa_index = ifmca->idev->dev->ifindex;
2892 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2893 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2894 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2896 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2897 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2899 ci.ifa_prefered = INFINITY_LIFE_TIME;
2900 ci.ifa_valid = INFINITY_LIFE_TIME;
2901 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2902 nlh->nlmsg_len = skb->tail - b;
2907 skb_trim(skb, b - skb->data);
2911 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2912 u32 pid, u32 seq, int event, unsigned int flags)
2914 struct ifaddrmsg *ifm;
2915 struct nlmsghdr *nlh;
2916 struct ifa_cacheinfo ci;
2917 unsigned char *b = skb->tail;
2919 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2920 ifm = NLMSG_DATA(nlh);
2921 ifm->ifa_family = AF_INET6;
2922 ifm->ifa_prefixlen = 128;
2923 ifm->ifa_flags = IFA_F_PERMANENT;
2924 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2925 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2926 ifm->ifa_scope = RT_SCOPE_SITE;
2927 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2928 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2929 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2930 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2932 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2933 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2935 ci.ifa_prefered = INFINITY_LIFE_TIME;
2936 ci.ifa_valid = INFINITY_LIFE_TIME;
2937 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2938 nlh->nlmsg_len = skb->tail - b;
2943 skb_trim(skb, b - skb->data);
2954 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2955 enum addr_type_t type)
2958 int s_idx, s_ip_idx;
2960 struct net_device *dev;
2961 struct inet6_dev *idev = NULL;
2962 struct inet6_ifaddr *ifa;
2963 struct ifmcaddr6 *ifmca;
2964 struct ifacaddr6 *ifaca;
2966 s_idx = cb->args[0];
2967 s_ip_idx = ip_idx = cb->args[1];
2968 read_lock(&dev_base_lock);
2970 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
2976 if ((idev = in6_dev_get(dev)) == NULL)
2978 read_lock_bh(&idev->lock);
2981 /* unicast address incl. temp addr */
2982 for (ifa = idev->addr_list; ifa;
2983 ifa = ifa->if_next, ip_idx++) {
2984 if (ip_idx < s_ip_idx)
2986 if ((err = inet6_fill_ifaddr(skb, ifa,
2987 NETLINK_CB(cb->skb).pid,
2988 cb->nlh->nlmsg_seq, RTM_NEWADDR,
2993 case MULTICAST_ADDR:
2994 /* multicast address */
2995 for (ifmca = idev->mc_list; ifmca;
2996 ifmca = ifmca->next, ip_idx++) {
2997 if (ip_idx < s_ip_idx)
2999 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3000 NETLINK_CB(cb->skb).pid,
3001 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3007 /* anycast address */
3008 for (ifaca = idev->ac_list; ifaca;
3009 ifaca = ifaca->aca_next, ip_idx++) {
3010 if (ip_idx < s_ip_idx)
3012 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3013 NETLINK_CB(cb->skb).pid,
3014 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3022 read_unlock_bh(&idev->lock);
3027 read_unlock_bh(&idev->lock);
3030 read_unlock(&dev_base_lock);
3032 cb->args[1] = ip_idx;
3036 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3038 enum addr_type_t type = UNICAST_ADDR;
3039 return inet6_dump_addr(skb, cb, type);
3042 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3044 enum addr_type_t type = MULTICAST_ADDR;
3045 return inet6_dump_addr(skb, cb, type);
3049 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3051 enum addr_type_t type = ANYCAST_ADDR;
3052 return inet6_dump_addr(skb, cb, type);
3055 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3057 struct sk_buff *skb;
3058 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
3060 skb = alloc_skb(size, GFP_ATOMIC);
3062 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
3065 if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
3067 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
3070 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
3071 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
3074 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3075 __s32 *array, int bytes)
3077 memset(array, 0, bytes);
3078 array[DEVCONF_FORWARDING] = cnf->forwarding;
3079 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3080 array[DEVCONF_MTU6] = cnf->mtu6;
3081 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3082 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3083 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3084 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3085 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3086 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3087 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3088 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3089 #ifdef CONFIG_IPV6_PRIVACY
3090 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3091 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3092 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3093 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3094 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3096 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3099 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3100 u32 pid, u32 seq, int event, unsigned int flags)
3102 struct net_device *dev = idev->dev;
3103 __s32 *array = NULL;
3104 struct ifinfomsg *r;
3105 struct nlmsghdr *nlh;
3106 unsigned char *b = skb->tail;
3107 struct rtattr *subattr;
3108 __u32 mtu = dev->mtu;
3109 struct ifla_cacheinfo ci;
3111 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
3112 r = NLMSG_DATA(nlh);
3113 r->ifi_family = AF_INET6;
3115 r->ifi_type = dev->type;
3116 r->ifi_index = dev->ifindex;
3117 r->ifi_flags = dev_get_flags(dev);
3120 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
3123 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3125 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
3126 if (dev->ifindex != dev->iflink)
3127 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
3129 subattr = (struct rtattr*)skb->tail;
3131 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
3133 /* return the device flags */
3134 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
3136 /* return interface cacheinfo */
3137 ci.max_reasm_len = IPV6_MAXPLEN;
3138 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3139 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3140 ci.reachable_time = idev->nd_parms->reachable_time;
3141 ci.retrans_time = idev->nd_parms->retrans_time;
3142 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3144 /* return the device sysctl params */
3145 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
3146 goto rtattr_failure;
3147 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
3148 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
3150 /* XXX - Statistics/MC not implemented */
3151 subattr->rta_len = skb->tail - (u8*)subattr;
3153 nlh->nlmsg_len = skb->tail - b;
3160 skb_trim(skb, b - skb->data);
3164 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3167 int s_idx = cb->args[0];
3168 struct net_device *dev;
3169 struct inet6_dev *idev;
3171 read_lock(&dev_base_lock);
3172 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3175 if ((idev = in6_dev_get(dev)) == NULL)
3177 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3178 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3183 read_unlock(&dev_base_lock);
3189 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3191 struct sk_buff *skb;
3193 int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
3195 skb = alloc_skb(size, GFP_ATOMIC);
3197 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
3200 if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
3202 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
3205 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
3206 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
3209 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3210 struct prefix_info *pinfo, u32 pid, u32 seq,
3211 int event, unsigned int flags)
3213 struct prefixmsg *pmsg;
3214 struct nlmsghdr *nlh;
3215 unsigned char *b = skb->tail;
3216 struct prefix_cacheinfo ci;
3218 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
3219 pmsg = NLMSG_DATA(nlh);
3220 pmsg->prefix_family = AF_INET6;
3221 pmsg->prefix_pad1 = 0;
3222 pmsg->prefix_pad2 = 0;
3223 pmsg->prefix_ifindex = idev->dev->ifindex;
3224 pmsg->prefix_len = pinfo->prefix_len;
3225 pmsg->prefix_type = pinfo->type;
3226 pmsg->prefix_pad3 = 0;
3228 pmsg->prefix_flags = 0;
3230 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3231 if (pinfo->autoconf)
3232 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3234 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3236 ci.preferred_time = ntohl(pinfo->prefered);
3237 ci.valid_time = ntohl(pinfo->valid);
3238 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3240 nlh->nlmsg_len = skb->tail - b;
3245 skb_trim(skb, b - skb->data);
3249 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3250 struct prefix_info *pinfo)
3252 struct sk_buff *skb;
3253 int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
3255 skb = alloc_skb(size, GFP_ATOMIC);
3257 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
3260 if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
3262 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
3265 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
3266 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
3269 static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
3270 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3271 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3272 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
3273 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
3274 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3275 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3276 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3277 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3278 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3279 .dumpit = inet6_dump_fib, },
3282 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3284 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3288 dst_hold(&ifp->rt->u.dst);
3289 if (ip6_ins_rt(ifp->rt, NULL, NULL, NULL))
3290 dst_release(&ifp->rt->u.dst);
3291 if (ifp->idev->cnf.forwarding)
3292 addrconf_join_anycast(ifp);
3295 if (ifp->idev->cnf.forwarding)
3296 addrconf_leave_anycast(ifp);
3297 addrconf_leave_solict(ifp->idev, &ifp->addr);
3298 dst_hold(&ifp->rt->u.dst);
3299 if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
3300 dst_free(&ifp->rt->u.dst);
3302 dst_release(&ifp->rt->u.dst);
3307 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3309 read_lock_bh(&addrconf_lock);
3310 if (likely(ifp->idev->dead == 0))
3311 __ipv6_ifa_notify(event, ifp);
3312 read_unlock_bh(&addrconf_lock);
3315 #ifdef CONFIG_SYSCTL
3318 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3319 void __user *buffer, size_t *lenp, loff_t *ppos)
3321 int *valp = ctl->data;
3325 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3327 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3328 if (valp != &ipv6_devconf.forwarding) {
3329 if ((!*valp) ^ (!val)) {
3330 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3333 dev_forward_change(idev);
3336 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3337 addrconf_forward_change();
3340 rt6_purge_dflt_routers();
3346 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3347 int __user *name, int nlen,
3348 void __user *oldval,
3349 size_t __user *oldlenp,
3350 void __user *newval, size_t newlen,
3353 int *valp = table->data;
3356 if (!newval || !newlen)
3358 if (newlen != sizeof(int))
3360 if (get_user(new, (int __user *)newval))
3364 if (oldval && oldlenp) {
3366 if (get_user(len, oldlenp))
3369 if (len > table->maxlen)
3370 len = table->maxlen;
3371 if (copy_to_user(oldval, valp, len))
3373 if (put_user(len, oldlenp))
3378 if (valp != &ipv6_devconf_dflt.forwarding) {
3379 if (valp != &ipv6_devconf.forwarding) {
3380 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3382 if (unlikely(idev == NULL))
3384 changed = (!*valp) ^ (!new);
3387 dev_forward_change(idev);
3390 addrconf_forward_change();
3394 rt6_purge_dflt_routers();
3401 static struct addrconf_sysctl_table
3403 struct ctl_table_header *sysctl_header;
3404 ctl_table addrconf_vars[__NET_IPV6_MAX];
3405 ctl_table addrconf_dev[2];
3406 ctl_table addrconf_conf_dir[2];
3407 ctl_table addrconf_proto_dir[2];
3408 ctl_table addrconf_root_dir[2];
3409 } addrconf_sysctl = {
3410 .sysctl_header = NULL,
3413 .ctl_name = NET_IPV6_FORWARDING,
3414 .procname = "forwarding",
3415 .data = &ipv6_devconf.forwarding,
3416 .maxlen = sizeof(int),
3418 .proc_handler = &addrconf_sysctl_forward,
3419 .strategy = &addrconf_sysctl_forward_strategy,
3422 .ctl_name = NET_IPV6_HOP_LIMIT,
3423 .procname = "hop_limit",
3424 .data = &ipv6_devconf.hop_limit,
3425 .maxlen = sizeof(int),
3427 .proc_handler = proc_dointvec,
3430 .ctl_name = NET_IPV6_MTU,
3432 .data = &ipv6_devconf.mtu6,
3433 .maxlen = sizeof(int),
3435 .proc_handler = &proc_dointvec,
3438 .ctl_name = NET_IPV6_ACCEPT_RA,
3439 .procname = "accept_ra",
3440 .data = &ipv6_devconf.accept_ra,
3441 .maxlen = sizeof(int),
3443 .proc_handler = &proc_dointvec,
3446 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3447 .procname = "accept_redirects",
3448 .data = &ipv6_devconf.accept_redirects,
3449 .maxlen = sizeof(int),
3451 .proc_handler = &proc_dointvec,
3454 .ctl_name = NET_IPV6_AUTOCONF,
3455 .procname = "autoconf",
3456 .data = &ipv6_devconf.autoconf,
3457 .maxlen = sizeof(int),
3459 .proc_handler = &proc_dointvec,
3462 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3463 .procname = "dad_transmits",
3464 .data = &ipv6_devconf.dad_transmits,
3465 .maxlen = sizeof(int),
3467 .proc_handler = &proc_dointvec,
3470 .ctl_name = NET_IPV6_RTR_SOLICITS,
3471 .procname = "router_solicitations",
3472 .data = &ipv6_devconf.rtr_solicits,
3473 .maxlen = sizeof(int),
3475 .proc_handler = &proc_dointvec,
3478 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3479 .procname = "router_solicitation_interval",
3480 .data = &ipv6_devconf.rtr_solicit_interval,
3481 .maxlen = sizeof(int),
3483 .proc_handler = &proc_dointvec_jiffies,
3484 .strategy = &sysctl_jiffies,
3487 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3488 .procname = "router_solicitation_delay",
3489 .data = &ipv6_devconf.rtr_solicit_delay,
3490 .maxlen = sizeof(int),
3492 .proc_handler = &proc_dointvec_jiffies,
3493 .strategy = &sysctl_jiffies,
3496 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3497 .procname = "force_mld_version",
3498 .data = &ipv6_devconf.force_mld_version,
3499 .maxlen = sizeof(int),
3501 .proc_handler = &proc_dointvec,
3503 #ifdef CONFIG_IPV6_PRIVACY
3505 .ctl_name = NET_IPV6_USE_TEMPADDR,
3506 .procname = "use_tempaddr",
3507 .data = &ipv6_devconf.use_tempaddr,
3508 .maxlen = sizeof(int),
3510 .proc_handler = &proc_dointvec,
3513 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3514 .procname = "temp_valid_lft",
3515 .data = &ipv6_devconf.temp_valid_lft,
3516 .maxlen = sizeof(int),
3518 .proc_handler = &proc_dointvec,
3521 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3522 .procname = "temp_prefered_lft",
3523 .data = &ipv6_devconf.temp_prefered_lft,
3524 .maxlen = sizeof(int),
3526 .proc_handler = &proc_dointvec,
3529 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3530 .procname = "regen_max_retry",
3531 .data = &ipv6_devconf.regen_max_retry,
3532 .maxlen = sizeof(int),
3534 .proc_handler = &proc_dointvec,
3537 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3538 .procname = "max_desync_factor",
3539 .data = &ipv6_devconf.max_desync_factor,
3540 .maxlen = sizeof(int),
3542 .proc_handler = &proc_dointvec,
3546 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3547 .procname = "max_addresses",
3548 .data = &ipv6_devconf.max_addresses,
3549 .maxlen = sizeof(int),
3551 .proc_handler = &proc_dointvec,
3554 .ctl_name = 0, /* sentinel */
3559 .ctl_name = NET_PROTO_CONF_ALL,
3562 .child = addrconf_sysctl.addrconf_vars,
3565 .ctl_name = 0, /* sentinel */
3568 .addrconf_conf_dir = {
3570 .ctl_name = NET_IPV6_CONF,
3573 .child = addrconf_sysctl.addrconf_dev,
3576 .ctl_name = 0, /* sentinel */
3579 .addrconf_proto_dir = {
3581 .ctl_name = NET_IPV6,
3584 .child = addrconf_sysctl.addrconf_conf_dir,
3587 .ctl_name = 0, /* sentinel */
3590 .addrconf_root_dir = {
3592 .ctl_name = CTL_NET,
3595 .child = addrconf_sysctl.addrconf_proto_dir,
3598 .ctl_name = 0, /* sentinel */
3603 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3606 struct net_device *dev = idev ? idev->dev : NULL;
3607 struct addrconf_sysctl_table *t;
3608 char *dev_name = NULL;
3610 t = kmalloc(sizeof(*t), GFP_KERNEL);
3613 memcpy(t, &addrconf_sysctl, sizeof(*t));
3614 for (i=0; t->addrconf_vars[i].data; i++) {
3615 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3616 t->addrconf_vars[i].de = NULL;
3617 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3620 dev_name = dev->name;
3621 t->addrconf_dev[0].ctl_name = dev->ifindex;
3623 dev_name = "default";
3624 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3628 * Make a copy of dev_name, because '.procname' is regarded as const
3629 * by sysctl and we wouldn't want anyone to change it under our feet
3630 * (see SIOCSIFNAME).
3632 dev_name = kstrdup(dev_name, GFP_KERNEL);
3636 t->addrconf_dev[0].procname = dev_name;
3638 t->addrconf_dev[0].child = t->addrconf_vars;
3639 t->addrconf_dev[0].de = NULL;
3640 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3641 t->addrconf_conf_dir[0].de = NULL;
3642 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3643 t->addrconf_proto_dir[0].de = NULL;
3644 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3645 t->addrconf_root_dir[0].de = NULL;
3647 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3648 if (t->sysctl_header == NULL)
3663 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3666 struct addrconf_sysctl_table *t = p->sysctl;
3668 unregister_sysctl_table(t->sysctl_header);
3669 kfree(t->addrconf_dev[0].procname);
3681 int register_inet6addr_notifier(struct notifier_block *nb)
3683 return notifier_chain_register(&inet6addr_chain, nb);
3686 int unregister_inet6addr_notifier(struct notifier_block *nb)
3688 return notifier_chain_unregister(&inet6addr_chain,nb);
3692 * Init / cleanup code
3695 int __init addrconf_init(void)
3699 /* The addrconf netdev notifier requires that loopback_dev
3700 * has it's ipv6 private information allocated and setup
3701 * before it can bring up and give link-local addresses
3702 * to other devices which are up.
3704 * Unfortunately, loopback_dev is not necessarily the first
3705 * entry in the global dev_base list of net devices. In fact,
3706 * it is likely to be the very last entry on that list.
3707 * So this causes the notifier registry below to try and
3708 * give link-local addresses to all devices besides loopback_dev
3709 * first, then loopback_dev, which cases all the non-loopback_dev
3710 * devices to fail to get a link-local address.
3712 * So, as a temporary fix, allocate the ipv6 structure for
3713 * loopback_dev first by hand.
3714 * Longer term, all of the dependencies ipv6 has upon the loopback
3715 * device and it being up should be removed.
3718 if (!ipv6_add_dev(&loopback_dev))
3724 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
3726 register_netdevice_notifier(&ipv6_dev_notf);
3728 #ifdef CONFIG_IPV6_PRIVACY
3729 md5_tfm = crypto_alloc_tfm("md5", 0);
3730 if (unlikely(md5_tfm == NULL))
3732 "failed to load transform for md5\n");
3736 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3737 #ifdef CONFIG_SYSCTL
3738 addrconf_sysctl.sysctl_header =
3739 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3740 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3746 void __exit addrconf_cleanup(void)
3748 struct net_device *dev;
3749 struct inet6_dev *idev;
3750 struct inet6_ifaddr *ifa;
3753 unregister_netdevice_notifier(&ipv6_dev_notf);
3755 rtnetlink_links[PF_INET6] = NULL;
3756 #ifdef CONFIG_SYSCTL
3757 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3758 addrconf_sysctl_unregister(&ipv6_devconf);
3767 for (dev=dev_base; dev; dev=dev->next) {
3768 if ((idev = __in6_dev_get(dev)) == NULL)
3770 addrconf_ifdown(dev, 1);
3772 addrconf_ifdown(&loopback_dev, 2);
3778 write_lock_bh(&addrconf_hash_lock);
3779 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3780 for (ifa=inet6_addr_lst[i]; ifa; ) {
3781 struct inet6_ifaddr *bifa;
3784 ifa = ifa->lst_next;
3785 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3786 /* Do not free it; something is wrong.
3787 Now we can investigate it with debugger.
3791 write_unlock_bh(&addrconf_hash_lock);
3793 del_timer(&addr_chk_timer);
3797 #ifdef CONFIG_IPV6_PRIVACY
3798 crypto_free_tfm(md5_tfm);
3802 #ifdef CONFIG_PROC_FS
3803 proc_net_remove("if_inet6");