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_dad_run(struct inet6_dev *idev);
141 static void addrconf_rs_timer(unsigned long data);
142 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
143 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
145 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
146 struct prefix_info *pinfo);
147 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
149 static struct notifier_block *inet6addr_chain;
151 struct ipv6_devconf ipv6_devconf = {
153 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
154 .mtu6 = IPV6_MIN_MTU,
156 .accept_redirects = 1,
158 .force_mld_version = 0,
160 .rtr_solicits = MAX_RTR_SOLICITATIONS,
161 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
162 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
163 #ifdef CONFIG_IPV6_PRIVACY
165 .temp_valid_lft = TEMP_VALID_LIFETIME,
166 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
167 .regen_max_retry = REGEN_MAX_RETRY,
168 .max_desync_factor = MAX_DESYNC_FACTOR,
170 .max_addresses = IPV6_MAX_ADDRESSES,
173 static struct ipv6_devconf ipv6_devconf_dflt = {
175 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
176 .mtu6 = IPV6_MIN_MTU,
178 .accept_redirects = 1,
181 .rtr_solicits = MAX_RTR_SOLICITATIONS,
182 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
183 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
184 #ifdef CONFIG_IPV6_PRIVACY
186 .temp_valid_lft = TEMP_VALID_LIFETIME,
187 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
188 .regen_max_retry = REGEN_MAX_RETRY,
189 .max_desync_factor = MAX_DESYNC_FACTOR,
191 .max_addresses = IPV6_MAX_ADDRESSES,
194 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
196 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
198 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
200 #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
202 static inline unsigned ipv6_addr_scope2type(unsigned scope)
205 case IPV6_ADDR_SCOPE_NODELOCAL:
206 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
208 case IPV6_ADDR_SCOPE_LINKLOCAL:
209 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
210 IPV6_ADDR_LINKLOCAL);
211 case IPV6_ADDR_SCOPE_SITELOCAL:
212 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
213 IPV6_ADDR_SITELOCAL);
215 return IPV6_ADDR_SCOPE_TYPE(scope);
218 int __ipv6_addr_type(const struct in6_addr *addr)
222 st = addr->s6_addr32[0];
224 /* Consider all addresses with the first three bits different of
225 000 and 111 as unicasts.
227 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
228 (st & htonl(0xE0000000)) != htonl(0xE0000000))
229 return (IPV6_ADDR_UNICAST |
230 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
232 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
234 /* addr-select 3.1 */
235 return (IPV6_ADDR_MULTICAST |
236 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
239 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
240 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
241 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
242 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
243 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
244 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
246 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
247 if (addr->s6_addr32[2] == 0) {
248 if (addr->s6_addr32[3] == 0)
249 return IPV6_ADDR_ANY;
251 if (addr->s6_addr32[3] == htonl(0x00000001))
252 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
253 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
255 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
256 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
259 if (addr->s6_addr32[2] == htonl(0x0000ffff))
260 return (IPV6_ADDR_MAPPED |
261 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
264 return (IPV6_ADDR_RESERVED |
265 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
268 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
270 if (del_timer(&ifp->timer))
274 enum addrconf_timer_t
281 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
282 enum addrconf_timer_t what,
285 if (!del_timer(&ifp->timer))
290 ifp->timer.function = addrconf_dad_timer;
293 ifp->timer.function = addrconf_rs_timer;
297 ifp->timer.expires = jiffies + when;
298 add_timer(&ifp->timer);
301 /* Nobody refers to this device, we may destroy it. */
303 void in6_dev_finish_destroy(struct inet6_dev *idev)
305 struct net_device *dev = idev->dev;
306 BUG_TRAP(idev->addr_list==NULL);
307 BUG_TRAP(idev->mc_list==NULL);
308 #ifdef NET_REFCNT_DEBUG
309 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
313 printk("Freeing alive inet6 device %p\n", idev);
316 snmp6_free_dev(idev);
320 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
322 struct inet6_dev *ndev;
326 if (dev->mtu < IPV6_MIN_MTU)
329 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
332 memset(ndev, 0, sizeof(struct inet6_dev));
334 rwlock_init(&ndev->lock);
336 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
337 ndev->cnf.mtu6 = dev->mtu;
338 ndev->cnf.sysctl = NULL;
339 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
340 if (ndev->nd_parms == NULL) {
344 /* We refer to the device */
347 if (snmp6_alloc_dev(ndev) < 0) {
349 "%s(): cannot allocate memory for statistics; dev=%s.\n",
350 __FUNCTION__, dev->name));
351 neigh_parms_release(&nd_tbl, ndev->nd_parms);
353 in6_dev_finish_destroy(ndev);
357 if (snmp6_register_dev(ndev) < 0) {
359 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
360 __FUNCTION__, dev->name));
361 neigh_parms_release(&nd_tbl, ndev->nd_parms);
363 in6_dev_finish_destroy(ndev);
367 /* One reference from device. We must do this before
368 * we invoke __ipv6_regen_rndid().
372 #ifdef CONFIG_IPV6_PRIVACY
373 get_random_bytes(ndev->rndid, sizeof(ndev->rndid));
374 get_random_bytes(ndev->entropy, sizeof(ndev->entropy));
375 init_timer(&ndev->regen_timer);
376 ndev->regen_timer.function = ipv6_regen_rndid;
377 ndev->regen_timer.data = (unsigned long) ndev;
378 if ((dev->flags&IFF_LOOPBACK) ||
379 dev->type == ARPHRD_TUNNEL ||
380 dev->type == ARPHRD_NONE ||
381 dev->type == ARPHRD_SIT) {
383 "%s: Disabled Privacy Extensions\n",
385 ndev->cnf.use_tempaddr = -1;
388 ipv6_regen_rndid((unsigned long) ndev);
392 if (netif_carrier_ok(dev))
393 ndev->if_flags |= IF_READY;
395 write_lock_bh(&addrconf_lock);
397 write_unlock_bh(&addrconf_lock);
399 ipv6_mc_init_dev(ndev);
400 ndev->tstamp = jiffies;
402 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
403 NET_IPV6_NEIGH, "ipv6",
404 &ndisc_ifinfo_sysctl_change,
406 addrconf_sysctl_register(ndev, &ndev->cnf);
412 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
414 struct inet6_dev *idev;
418 if ((idev = __in6_dev_get(dev)) == NULL) {
419 if ((idev = ipv6_add_dev(dev)) == NULL)
423 if (dev->flags&IFF_UP)
429 static void dev_forward_change(struct inet6_dev *idev)
431 struct net_device *dev;
432 struct inet6_ifaddr *ifa;
433 struct in6_addr addr;
438 if (dev && (dev->flags & IFF_MULTICAST)) {
439 ipv6_addr_all_routers(&addr);
441 if (idev->cnf.forwarding)
442 ipv6_dev_mc_inc(dev, &addr);
444 ipv6_dev_mc_dec(dev, &addr);
446 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
447 if (idev->cnf.forwarding)
448 addrconf_join_anycast(ifa);
450 addrconf_leave_anycast(ifa);
455 static void addrconf_forward_change(void)
457 struct net_device *dev;
458 struct inet6_dev *idev;
460 read_lock(&dev_base_lock);
461 for (dev=dev_base; dev; dev=dev->next) {
462 read_lock(&addrconf_lock);
463 idev = __in6_dev_get(dev);
465 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
466 idev->cnf.forwarding = ipv6_devconf.forwarding;
468 dev_forward_change(idev);
470 read_unlock(&addrconf_lock);
472 read_unlock(&dev_base_lock);
476 /* Nobody refers to this ifaddr, destroy it */
478 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
480 BUG_TRAP(ifp->if_next==NULL);
481 BUG_TRAP(ifp->lst_next==NULL);
482 #ifdef NET_REFCNT_DEBUG
483 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
486 in6_dev_put(ifp->idev);
488 if (del_timer(&ifp->timer))
489 printk("Timer is still running, when freeing ifa=%p\n", ifp);
492 printk("Freeing alive inet6 address %p\n", ifp);
495 dst_release(&ifp->rt->u.dst);
500 /* On success it returns ifp with increased reference count */
502 static struct inet6_ifaddr *
503 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
504 int scope, u32 flags)
506 struct inet6_ifaddr *ifa = NULL;
511 read_lock_bh(&addrconf_lock);
513 err = -ENODEV; /*XXX*/
517 write_lock(&addrconf_hash_lock);
519 /* Ignore adding duplicate addresses on an interface */
520 if (ipv6_chk_same_addr(addr, idev->dev)) {
521 ADBG(("ipv6_add_addr: already assigned\n"));
526 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
529 ADBG(("ipv6_add_addr: malloc failed\n"));
534 rt = addrconf_dst_alloc(idev, addr, 0);
540 memset(ifa, 0, sizeof(struct inet6_ifaddr));
541 ipv6_addr_copy(&ifa->addr, addr);
543 spin_lock_init(&ifa->lock);
544 init_timer(&ifa->timer);
545 ifa->timer.data = (unsigned long) ifa;
547 ifa->prefix_len = pfxlen;
548 ifa->flags = flags | IFA_F_TENTATIVE;
549 ifa->cstamp = ifa->tstamp = jiffies;
556 /* Add to big hash table */
557 hash = ipv6_addr_hash(addr);
559 ifa->lst_next = inet6_addr_lst[hash];
560 inet6_addr_lst[hash] = ifa;
562 write_unlock(&addrconf_hash_lock);
564 write_lock(&idev->lock);
565 /* Add to inet6_dev unicast addr list. */
566 ifa->if_next = idev->addr_list;
567 idev->addr_list = ifa;
569 #ifdef CONFIG_IPV6_PRIVACY
570 if (ifa->flags&IFA_F_TEMPORARY) {
571 ifa->tmp_next = idev->tempaddr_list;
572 idev->tempaddr_list = ifa;
580 write_unlock(&idev->lock);
582 read_unlock_bh(&addrconf_lock);
584 if (likely(err == 0))
585 notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
593 write_unlock(&addrconf_hash_lock);
597 /* This function wants to get referenced ifp and releases it before return */
599 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
601 struct inet6_ifaddr *ifa, **ifap;
602 struct inet6_dev *idev = ifp->idev;
604 int deleted = 0, onlink = 0;
605 unsigned long expires = jiffies;
607 hash = ipv6_addr_hash(&ifp->addr);
611 write_lock_bh(&addrconf_hash_lock);
612 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
613 ifap = &ifa->lst_next) {
615 *ifap = ifa->lst_next;
617 ifa->lst_next = NULL;
621 write_unlock_bh(&addrconf_hash_lock);
623 write_lock_bh(&idev->lock);
624 #ifdef CONFIG_IPV6_PRIVACY
625 if (ifp->flags&IFA_F_TEMPORARY) {
626 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
627 ifap = &ifa->tmp_next) {
629 *ifap = ifa->tmp_next;
631 in6_ifa_put(ifp->ifpub);
635 ifa->tmp_next = NULL;
642 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;
643 ifap = &ifa->if_next) {
645 *ifap = ifa->if_next;
648 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
651 } else if (ifp->flags & IFA_F_PERMANENT) {
652 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
654 if (ifa->flags & IFA_F_PERMANENT) {
659 unsigned long lifetime;
664 spin_lock(&ifa->lock);
665 lifetime = min_t(unsigned long,
666 ifa->valid_lft, 0x7fffffffUL/HZ);
667 if (time_before(expires,
668 ifa->tstamp + lifetime * HZ))
669 expires = ifa->tstamp + lifetime * HZ;
670 spin_unlock(&ifa->lock);
675 write_unlock_bh(&idev->lock);
677 ipv6_ifa_notify(RTM_DELADDR, ifp);
679 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
681 addrconf_del_timer(ifp);
684 * Purge or update corresponding prefix
686 * 1) we don't purge prefix here if address was not permanent.
687 * prefix is managed by its own lifetime.
688 * 2) if there're no addresses, delete prefix.
689 * 3) if there're still other permanent address(es),
690 * corresponding prefix is still permanent.
691 * 4) otherwise, update prefix lifetime to the
692 * longest valid lifetime among the corresponding
693 * addresses on the device.
694 * Note: subsequent RA will update lifetime.
698 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
699 struct in6_addr prefix;
702 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
703 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
705 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
707 ip6_del_rt(rt, NULL, NULL, NULL);
709 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
710 rt->rt6i_expires = expires;
711 rt->rt6i_flags |= RTF_EXPIRES;
714 dst_release(&rt->u.dst);
720 #ifdef CONFIG_IPV6_PRIVACY
721 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
723 struct inet6_dev *idev = ifp->idev;
724 struct in6_addr addr, *tmpaddr;
725 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
730 write_lock(&idev->lock);
732 spin_lock_bh(&ift->lock);
733 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
734 spin_unlock_bh(&ift->lock);
741 if (idev->cnf.use_tempaddr <= 0) {
742 write_unlock(&idev->lock);
744 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
749 spin_lock_bh(&ifp->lock);
750 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
751 idev->cnf.use_tempaddr = -1; /*XXX*/
752 spin_unlock_bh(&ifp->lock);
753 write_unlock(&idev->lock);
755 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
761 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
762 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
763 spin_unlock_bh(&ifp->lock);
764 write_unlock(&idev->lock);
766 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
772 memcpy(&addr.s6_addr[8], idev->rndid, 8);
773 tmp_valid_lft = min_t(__u32,
775 idev->cnf.temp_valid_lft);
776 tmp_prefered_lft = min_t(__u32,
778 idev->cnf.temp_prefered_lft - desync_factor / HZ);
779 tmp_plen = ifp->prefix_len;
780 max_addresses = idev->cnf.max_addresses;
781 tmp_cstamp = ifp->cstamp;
782 tmp_tstamp = ifp->tstamp;
783 spin_unlock_bh(&ifp->lock);
785 write_unlock(&idev->lock);
786 ift = !max_addresses ||
787 ipv6_count_addresses(idev) < max_addresses ?
788 ipv6_add_addr(idev, &addr, tmp_plen,
789 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
790 if (!ift || IS_ERR(ift)) {
794 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
796 write_lock(&idev->lock);
800 spin_lock_bh(&ift->lock);
802 ift->valid_lft = tmp_valid_lft;
803 ift->prefered_lft = tmp_prefered_lft;
804 ift->cstamp = tmp_cstamp;
805 ift->tstamp = tmp_tstamp;
806 spin_unlock_bh(&ift->lock);
808 addrconf_dad_start(ift, 0);
817 * Choose an appropriate source address (RFC3484)
819 struct ipv6_saddr_score {
827 #define IPV6_SADDR_SCORE_LOCAL 0x0001
828 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
829 #define IPV6_SADDR_SCORE_HOA 0x0008
830 #define IPV6_SADDR_SCORE_OIF 0x0010
831 #define IPV6_SADDR_SCORE_LABEL 0x0020
832 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
834 static int inline ipv6_saddr_preferred(int type)
836 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
837 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
842 /* static matching label */
843 static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
846 * prefix (longest match) label
847 * -----------------------------
854 if (type & IPV6_ADDR_LOOPBACK)
856 else if (type & IPV6_ADDR_COMPATv4)
858 else if (type & IPV6_ADDR_MAPPED)
860 else if (addr->s6_addr16[0] == htons(0x2002))
865 int ipv6_dev_get_saddr(struct net_device *daddr_dev,
866 struct in6_addr *daddr, struct in6_addr *saddr)
868 struct ipv6_saddr_score hiscore;
869 struct inet6_ifaddr *ifa_result = NULL;
870 int daddr_type = __ipv6_addr_type(daddr);
871 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
872 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
873 struct net_device *dev;
875 memset(&hiscore, 0, sizeof(hiscore));
877 read_lock(&dev_base_lock);
878 read_lock(&addrconf_lock);
880 for (dev = dev_base; dev; dev=dev->next) {
881 struct inet6_dev *idev;
882 struct inet6_ifaddr *ifa;
884 /* Rule 0: Candidate Source Address (section 4)
885 * - multicast and link-local destination address,
886 * the set of candidate source address MUST only
887 * include addresses assigned to interfaces
888 * belonging to the same link as the outgoing
890 * (- For site-local destination addresses, the
891 * set of candidate source addresses MUST only
892 * include addresses assigned to interfaces
893 * belonging to the same site as the outgoing
896 if ((daddr_type & IPV6_ADDR_MULTICAST ||
897 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
898 daddr_dev && dev != daddr_dev)
901 idev = __in6_dev_get(dev);
905 read_lock_bh(&idev->lock);
906 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
907 struct ipv6_saddr_score score;
909 score.addr_type = __ipv6_addr_type(&ifa->addr);
912 * - Tentative Address (RFC2462 section 5.4)
913 * - A tentative address is not considered
914 * "assigned to an interface" in the traditional
916 * - Candidate Source Address (section 4)
917 * - In any case, anycast addresses, multicast
918 * addresses, and the unspecified address MUST
919 * NOT be included in a candidate set.
921 if (ifa->flags & IFA_F_TENTATIVE)
923 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
924 score.addr_type & IPV6_ADDR_MULTICAST)) {
925 LIMIT_NETDEBUG(KERN_DEBUG
926 "ADDRCONF: unspecified / multicast address"
927 "assigned as unicast address on %s",
937 if (ifa_result == NULL) {
938 /* record it if the first available entry */
942 /* Rule 1: Prefer same address */
943 if (hiscore.rule < 1) {
944 if (ipv6_addr_equal(&ifa_result->addr, daddr))
945 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
948 if (ipv6_addr_equal(&ifa->addr, daddr)) {
949 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
950 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
955 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
959 /* Rule 2: Prefer appropriate scope */
960 if (hiscore.rule < 2) {
961 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
964 score.scope = __ipv6_addr_src_scope(score.addr_type);
965 if (hiscore.scope < score.scope) {
966 if (hiscore.scope < daddr_scope) {
971 } else if (score.scope < hiscore.scope) {
972 if (score.scope < daddr_scope)
980 /* Rule 3: Avoid deprecated address */
981 if (hiscore.rule < 3) {
982 if (ipv6_saddr_preferred(hiscore.addr_type) ||
983 !(ifa_result->flags & IFA_F_DEPRECATED))
984 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
987 if (ipv6_saddr_preferred(score.addr_type) ||
988 !(ifa->flags & IFA_F_DEPRECATED)) {
989 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
990 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
995 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
999 /* Rule 4: Prefer home address -- not implemented yet */
1000 if (hiscore.rule < 4)
1003 /* Rule 5: Prefer outgoing interface */
1004 if (hiscore.rule < 5) {
1005 if (daddr_dev == NULL ||
1006 daddr_dev == ifa_result->idev->dev)
1007 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1010 if (daddr_dev == NULL ||
1011 daddr_dev == ifa->idev->dev) {
1012 score.attrs |= IPV6_SADDR_SCORE_OIF;
1013 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1018 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1022 /* Rule 6: Prefer matching label */
1023 if (hiscore.rule < 6) {
1024 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1025 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1028 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1029 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1030 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1035 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1039 #ifdef CONFIG_IPV6_PRIVACY
1040 /* Rule 7: Prefer public address
1041 * Note: prefer temprary address if use_tempaddr >= 2
1043 if (hiscore.rule < 7) {
1044 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1045 (ifa_result->idev->cnf.use_tempaddr >= 2))
1046 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1049 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1050 (ifa->idev->cnf.use_tempaddr >= 2)) {
1051 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1052 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1057 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1061 /* Rule 8: Use longest matching prefix */
1062 if (hiscore.rule < 8) {
1063 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
1066 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1067 if (score.matchlen > hiscore.matchlen) {
1072 else if (score.matchlen < hiscore.matchlen)
1076 /* Final Rule: choose first available one */
1080 in6_ifa_put(ifa_result);
1085 read_unlock_bh(&idev->lock);
1087 read_unlock(&addrconf_lock);
1088 read_unlock(&dev_base_lock);
1091 return -EADDRNOTAVAIL;
1093 ipv6_addr_copy(saddr, &ifa_result->addr);
1094 in6_ifa_put(ifa_result);
1099 int ipv6_get_saddr(struct dst_entry *dst,
1100 struct in6_addr *daddr, struct in6_addr *saddr)
1102 return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
1106 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1108 struct inet6_dev *idev;
1109 int err = -EADDRNOTAVAIL;
1111 read_lock(&addrconf_lock);
1112 if ((idev = __in6_dev_get(dev)) != NULL) {
1113 struct inet6_ifaddr *ifp;
1115 read_lock_bh(&idev->lock);
1116 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1117 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1118 ipv6_addr_copy(addr, &ifp->addr);
1123 read_unlock_bh(&idev->lock);
1125 read_unlock(&addrconf_lock);
1129 static int ipv6_count_addresses(struct inet6_dev *idev)
1132 struct inet6_ifaddr *ifp;
1134 read_lock_bh(&idev->lock);
1135 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1137 read_unlock_bh(&idev->lock);
1141 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1143 struct inet6_ifaddr * ifp;
1144 u8 hash = ipv6_addr_hash(addr);
1146 read_lock_bh(&addrconf_hash_lock);
1147 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1148 if (ipv6_addr_equal(&ifp->addr, addr) &&
1149 !(ifp->flags&IFA_F_TENTATIVE)) {
1150 if (dev == NULL || ifp->idev->dev == dev ||
1151 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1155 read_unlock_bh(&addrconf_hash_lock);
1160 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1162 struct inet6_ifaddr * ifp;
1163 u8 hash = ipv6_addr_hash(addr);
1165 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1166 if (ipv6_addr_equal(&ifp->addr, addr)) {
1167 if (dev == NULL || ifp->idev->dev == dev)
1174 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1176 struct inet6_ifaddr * ifp;
1177 u8 hash = ipv6_addr_hash(addr);
1179 read_lock_bh(&addrconf_hash_lock);
1180 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1181 if (ipv6_addr_equal(&ifp->addr, addr)) {
1182 if (dev == NULL || ifp->idev->dev == dev ||
1183 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1189 read_unlock_bh(&addrconf_hash_lock);
1194 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1196 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1197 const struct in6_addr *sk2_rcv_saddr6 = tcp_v6_rcv_saddr(sk2);
1198 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1199 u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1200 int sk_ipv6only = ipv6_only_sock(sk);
1201 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1202 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1203 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1205 if (!sk2_rcv_saddr && !sk_ipv6only)
1208 if (addr_type2 == IPV6_ADDR_ANY &&
1209 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1212 if (addr_type == IPV6_ADDR_ANY &&
1213 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1216 if (sk2_rcv_saddr6 &&
1217 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1220 if (addr_type == IPV6_ADDR_MAPPED &&
1222 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1228 /* Gets referenced address, destroys ifaddr */
1230 void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1232 if (ifp->flags&IFA_F_PERMANENT) {
1233 spin_lock_bh(&ifp->lock);
1234 addrconf_del_timer(ifp);
1235 ifp->flags |= IFA_F_TENTATIVE;
1236 spin_unlock_bh(&ifp->lock);
1238 #ifdef CONFIG_IPV6_PRIVACY
1239 } else if (ifp->flags&IFA_F_TEMPORARY) {
1240 struct inet6_ifaddr *ifpub;
1241 spin_lock_bh(&ifp->lock);
1244 in6_ifa_hold(ifpub);
1245 spin_unlock_bh(&ifp->lock);
1246 ipv6_create_tempaddr(ifpub, ifp);
1249 spin_unlock_bh(&ifp->lock);
1257 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1259 if (net_ratelimit())
1260 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1261 addrconf_dad_stop(ifp);
1264 /* Join to solicited addr multicast group. */
1266 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1268 struct in6_addr maddr;
1270 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1273 addrconf_addr_solict_mult(addr, &maddr);
1274 ipv6_dev_mc_inc(dev, &maddr);
1277 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1279 struct in6_addr maddr;
1281 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1284 addrconf_addr_solict_mult(addr, &maddr);
1285 __ipv6_dev_mc_dec(idev, &maddr);
1288 static void addrconf_join_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_inc(ifp->idev->dev, &addr);
1297 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1299 struct in6_addr addr;
1300 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1301 if (ipv6_addr_any(&addr))
1303 __ipv6_dev_ac_dec(ifp->idev, &addr);
1306 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1308 switch (dev->type) {
1311 case ARPHRD_IEEE802_TR:
1312 if (dev->addr_len != ETH_ALEN)
1314 memcpy(eui, dev->dev_addr, 3);
1315 memcpy(eui + 5, dev->dev_addr + 3, 3);
1318 * The zSeries OSA network cards can be shared among various
1319 * OS instances, but the OSA cards have only one MAC address.
1320 * This leads to duplicate address conflicts in conjunction
1321 * with IPv6 if more than one instance uses the same card.
1323 * The driver for these cards can deliver a unique 16-bit
1324 * identifier for each instance sharing the same card. It is
1325 * placed instead of 0xFFFE in the interface identifier. The
1326 * "u" bit of the interface identifier is not inverted in this
1327 * case. Hence the resulting interface identifier has local
1328 * scope according to RFC2373.
1331 eui[3] = (dev->dev_id >> 8) & 0xFF;
1332 eui[4] = dev->dev_id & 0xFF;
1340 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1341 if (dev->addr_len != ARCNET_ALEN)
1344 eui[7] = *(u8*)dev->dev_addr;
1346 case ARPHRD_INFINIBAND:
1347 if (dev->addr_len != INFINIBAND_ALEN)
1349 memcpy(eui, dev->dev_addr + 12, 8);
1356 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1359 struct inet6_ifaddr *ifp;
1361 read_lock_bh(&idev->lock);
1362 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1363 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1364 memcpy(eui, ifp->addr.s6_addr+8, 8);
1369 read_unlock_bh(&idev->lock);
1373 #ifdef CONFIG_IPV6_PRIVACY
1374 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1375 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1377 struct net_device *dev;
1378 struct scatterlist sg[2];
1380 sg_set_buf(&sg[0], idev->entropy, 8);
1381 sg_set_buf(&sg[1], idev->work_eui64, 8);
1385 if (ipv6_generate_eui64(idev->work_eui64, dev)) {
1387 "__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
1389 get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
1392 spin_lock(&md5_tfm_lock);
1393 if (unlikely(md5_tfm == NULL)) {
1394 spin_unlock(&md5_tfm_lock);
1397 crypto_digest_init(md5_tfm);
1398 crypto_digest_update(md5_tfm, sg, 2);
1399 crypto_digest_final(md5_tfm, idev->work_digest);
1400 spin_unlock(&md5_tfm_lock);
1402 memcpy(idev->rndid, &idev->work_digest[0], 8);
1403 idev->rndid[0] &= ~0x02;
1404 memcpy(idev->entropy, &idev->work_digest[8], 8);
1407 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1408 * check if generated address is not inappropriate
1410 * - Reserved subnet anycast (RFC 2526)
1411 * 11111101 11....11 1xxxxxxx
1412 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1413 * 00-00-5E-FE-xx-xx-xx-xx
1415 * - XXX: already assigned to an address on the device
1417 if (idev->rndid[0] == 0xfd &&
1418 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1419 (idev->rndid[7]&0x80))
1421 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1422 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1424 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1431 static void ipv6_regen_rndid(unsigned long data)
1433 struct inet6_dev *idev = (struct inet6_dev *) data;
1434 unsigned long expires;
1436 read_lock_bh(&addrconf_lock);
1437 write_lock_bh(&idev->lock);
1442 if (__ipv6_regen_rndid(idev) < 0)
1446 idev->cnf.temp_prefered_lft * HZ -
1447 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1448 if (time_before(expires, jiffies)) {
1450 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1455 if (!mod_timer(&idev->regen_timer, expires))
1459 write_unlock_bh(&idev->lock);
1460 read_unlock_bh(&addrconf_lock);
1464 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1467 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1468 ret = __ipv6_regen_rndid(idev);
1478 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1479 unsigned long expires, u32 flags)
1481 struct in6_rtmsg rtmsg;
1483 memset(&rtmsg, 0, sizeof(rtmsg));
1484 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1485 rtmsg.rtmsg_dst_len = plen;
1486 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1487 rtmsg.rtmsg_ifindex = dev->ifindex;
1488 rtmsg.rtmsg_info = expires;
1489 rtmsg.rtmsg_flags = RTF_UP|flags;
1490 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1492 /* Prevent useless cloning on PtP SIT.
1493 This thing is done here expecting that the whole
1494 class of non-broadcast devices need not cloning.
1496 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1497 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1499 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1502 /* Create "default" multicast route to the interface */
1504 static void addrconf_add_mroute(struct net_device *dev)
1506 struct in6_rtmsg rtmsg;
1508 memset(&rtmsg, 0, sizeof(rtmsg));
1509 ipv6_addr_set(&rtmsg.rtmsg_dst,
1510 htonl(0xFF000000), 0, 0, 0);
1511 rtmsg.rtmsg_dst_len = 8;
1512 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1513 rtmsg.rtmsg_ifindex = dev->ifindex;
1514 rtmsg.rtmsg_flags = RTF_UP;
1515 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1516 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1519 static void sit_route_add(struct net_device *dev)
1521 struct in6_rtmsg rtmsg;
1523 memset(&rtmsg, 0, sizeof(rtmsg));
1525 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1526 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1528 /* prefix length - 96 bits "::d.d.d.d" */
1529 rtmsg.rtmsg_dst_len = 96;
1530 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1531 rtmsg.rtmsg_ifindex = dev->ifindex;
1533 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1536 static void addrconf_add_lroute(struct net_device *dev)
1538 struct in6_addr addr;
1540 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1541 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1544 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1546 struct inet6_dev *idev;
1550 if ((idev = ipv6_find_idev(dev)) == NULL)
1553 /* Add default multicast route */
1554 addrconf_add_mroute(dev);
1556 /* Add link local route */
1557 addrconf_add_lroute(dev);
1561 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1563 struct prefix_info *pinfo;
1567 unsigned long rt_expires;
1568 struct inet6_dev *in6_dev;
1570 pinfo = (struct prefix_info *) opt;
1572 if (len < sizeof(struct prefix_info)) {
1573 ADBG(("addrconf: prefix option too short\n"));
1578 * Validation checks ([ADDRCONF], page 19)
1581 addr_type = ipv6_addr_type(&pinfo->prefix);
1583 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1586 valid_lft = ntohl(pinfo->valid);
1587 prefered_lft = ntohl(pinfo->prefered);
1589 if (prefered_lft > valid_lft) {
1590 if (net_ratelimit())
1591 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1595 in6_dev = in6_dev_get(dev);
1597 if (in6_dev == NULL) {
1598 if (net_ratelimit())
1599 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1604 * Two things going on here:
1605 * 1) Add routes for on-link prefixes
1606 * 2) Configure prefixes with the auto flag set
1609 /* Avoid arithmetic overflow. Really, we could
1610 save rt_expires in seconds, likely valid_lft,
1611 but it would require division in fib gc, that it
1614 if (valid_lft >= 0x7FFFFFFF/HZ)
1615 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1617 rt_expires = valid_lft * HZ;
1620 * We convert this (in jiffies) to clock_t later.
1621 * Avoid arithmetic overflow there as well.
1622 * Overflow can happen only if HZ < USER_HZ.
1624 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1625 rt_expires = 0x7FFFFFFF / USER_HZ;
1627 if (pinfo->onlink) {
1628 struct rt6_info *rt;
1629 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1631 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1632 if (rt->rt6i_flags&RTF_EXPIRES) {
1633 if (valid_lft == 0) {
1634 ip6_del_rt(rt, NULL, NULL, NULL);
1637 rt->rt6i_expires = jiffies + rt_expires;
1640 } else if (valid_lft) {
1641 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1642 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1645 dst_release(&rt->u.dst);
1648 /* Try to figure out our local address for this prefix */
1650 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1651 struct inet6_ifaddr * ifp;
1652 struct in6_addr addr;
1653 int create = 0, update_lft = 0;
1655 if (pinfo->prefix_len == 64) {
1656 memcpy(&addr, &pinfo->prefix, 8);
1657 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1658 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1659 in6_dev_put(in6_dev);
1664 if (net_ratelimit())
1665 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1667 in6_dev_put(in6_dev);
1672 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1674 if (ifp == NULL && valid_lft) {
1675 int max_addresses = in6_dev->cnf.max_addresses;
1677 /* Do not allow to create too much of autoconfigured
1678 * addresses; this would be too easy way to crash kernel.
1680 if (!max_addresses ||
1681 ipv6_count_addresses(in6_dev) < max_addresses)
1682 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1683 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1685 if (!ifp || IS_ERR(ifp)) {
1686 in6_dev_put(in6_dev);
1690 update_lft = create = 1;
1691 ifp->cstamp = jiffies;
1692 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1698 #ifdef CONFIG_IPV6_PRIVACY
1699 struct inet6_ifaddr *ift;
1703 /* update lifetime (RFC2462 5.5.3 e) */
1704 spin_lock(&ifp->lock);
1706 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1707 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1710 if (!update_lft && stored_lft) {
1711 if (valid_lft > MIN_VALID_LIFETIME ||
1712 valid_lft > stored_lft)
1714 else if (stored_lft <= MIN_VALID_LIFETIME) {
1715 /* valid_lft <= stored_lft is always true */
1719 valid_lft = MIN_VALID_LIFETIME;
1720 if (valid_lft < prefered_lft)
1721 prefered_lft = valid_lft;
1727 ifp->valid_lft = valid_lft;
1728 ifp->prefered_lft = prefered_lft;
1731 ifp->flags &= ~IFA_F_DEPRECATED;
1732 spin_unlock(&ifp->lock);
1734 if (!(flags&IFA_F_TENTATIVE))
1735 ipv6_ifa_notify(0, ifp);
1737 spin_unlock(&ifp->lock);
1739 #ifdef CONFIG_IPV6_PRIVACY
1740 read_lock_bh(&in6_dev->lock);
1741 /* update all temporary addresses in the list */
1742 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1744 * When adjusting the lifetimes of an existing
1745 * temporary address, only lower the lifetimes.
1746 * Implementations must not increase the
1747 * lifetimes of an existing temporary address
1748 * when processing a Prefix Information Option.
1750 spin_lock(&ift->lock);
1752 if (ift->valid_lft > valid_lft &&
1753 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1754 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1755 if (ift->prefered_lft > prefered_lft &&
1756 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1757 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1758 spin_unlock(&ift->lock);
1759 if (!(flags&IFA_F_TENTATIVE))
1760 ipv6_ifa_notify(0, ift);
1763 if (create && in6_dev->cnf.use_tempaddr > 0) {
1765 * When a new public address is created as described in [ADDRCONF],
1766 * also create a new temporary address.
1768 read_unlock_bh(&in6_dev->lock);
1769 ipv6_create_tempaddr(ifp, NULL);
1771 read_unlock_bh(&in6_dev->lock);
1778 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1779 in6_dev_put(in6_dev);
1783 * Set destination address.
1784 * Special case for SIT interfaces where we create a new "virtual"
1787 int addrconf_set_dstaddr(void __user *arg)
1789 struct in6_ifreq ireq;
1790 struct net_device *dev;
1796 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1799 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1805 if (dev->type == ARPHRD_SIT) {
1808 struct ip_tunnel_parm p;
1810 err = -EADDRNOTAVAIL;
1811 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1814 memset(&p, 0, sizeof(p));
1815 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1819 p.iph.protocol = IPPROTO_IPV6;
1821 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1823 oldfs = get_fs(); set_fs(KERNEL_DS);
1824 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1829 if ((dev = __dev_get_by_name(p.name)) == NULL)
1831 err = dev_open(dev);
1841 * Manual configuration of address on an interface
1843 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1845 struct inet6_ifaddr *ifp;
1846 struct inet6_dev *idev;
1847 struct net_device *dev;
1852 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1855 if (!(dev->flags&IFF_UP))
1858 if ((idev = addrconf_add_dev(dev)) == NULL)
1861 scope = ipv6_addr_scope(pfx);
1863 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1865 addrconf_dad_start(ifp, 0);
1870 return PTR_ERR(ifp);
1873 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1875 struct inet6_ifaddr *ifp;
1876 struct inet6_dev *idev;
1877 struct net_device *dev;
1879 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1882 if ((idev = __in6_dev_get(dev)) == NULL)
1885 read_lock_bh(&idev->lock);
1886 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1887 if (ifp->prefix_len == plen &&
1888 ipv6_addr_equal(pfx, &ifp->addr)) {
1890 read_unlock_bh(&idev->lock);
1894 /* If the last address is deleted administratively,
1895 disable IPv6 on this interface.
1897 if (idev->addr_list == NULL)
1898 addrconf_ifdown(idev->dev, 1);
1902 read_unlock_bh(&idev->lock);
1903 return -EADDRNOTAVAIL;
1907 int addrconf_add_ifaddr(void __user *arg)
1909 struct in6_ifreq ireq;
1912 if (!capable(CAP_NET_ADMIN))
1915 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1919 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1924 int addrconf_del_ifaddr(void __user *arg)
1926 struct in6_ifreq ireq;
1929 if (!capable(CAP_NET_ADMIN))
1932 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1936 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1941 static void sit_add_v4_addrs(struct inet6_dev *idev)
1943 struct inet6_ifaddr * ifp;
1944 struct in6_addr addr;
1945 struct net_device *dev;
1950 memset(&addr, 0, sizeof(struct in6_addr));
1951 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1953 if (idev->dev->flags&IFF_POINTOPOINT) {
1954 addr.s6_addr32[0] = htonl(0xfe800000);
1957 scope = IPV6_ADDR_COMPATv4;
1960 if (addr.s6_addr32[3]) {
1961 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1963 spin_lock_bh(&ifp->lock);
1964 ifp->flags &= ~IFA_F_TENTATIVE;
1965 spin_unlock_bh(&ifp->lock);
1966 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1972 for (dev = dev_base; dev != NULL; dev = dev->next) {
1973 struct in_device * in_dev = __in_dev_get_rtnl(dev);
1974 if (in_dev && (dev->flags & IFF_UP)) {
1975 struct in_ifaddr * ifa;
1979 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1982 addr.s6_addr32[3] = ifa->ifa_local;
1984 if (ifa->ifa_scope == RT_SCOPE_LINK)
1986 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1987 if (idev->dev->flags&IFF_POINTOPOINT)
1991 if (idev->dev->flags&IFF_POINTOPOINT)
1996 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1999 spin_lock_bh(&ifp->lock);
2000 ifp->flags &= ~IFA_F_TENTATIVE;
2001 spin_unlock_bh(&ifp->lock);
2002 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2010 static void init_loopback(struct net_device *dev)
2012 struct inet6_dev *idev;
2013 struct inet6_ifaddr * ifp;
2019 if ((idev = ipv6_find_idev(dev)) == NULL) {
2020 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2024 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2026 spin_lock_bh(&ifp->lock);
2027 ifp->flags &= ~IFA_F_TENTATIVE;
2028 spin_unlock_bh(&ifp->lock);
2029 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2034 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2036 struct inet6_ifaddr * ifp;
2038 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2040 addrconf_dad_start(ifp, 0);
2045 static void addrconf_dev_config(struct net_device *dev)
2047 struct in6_addr addr;
2048 struct inet6_dev * idev;
2052 if ((dev->type != ARPHRD_ETHER) &&
2053 (dev->type != ARPHRD_FDDI) &&
2054 (dev->type != ARPHRD_IEEE802_TR) &&
2055 (dev->type != ARPHRD_ARCNET) &&
2056 (dev->type != ARPHRD_INFINIBAND)) {
2057 /* Alas, we support only Ethernet autoconfiguration. */
2061 idev = addrconf_add_dev(dev);
2065 memset(&addr, 0, sizeof(struct in6_addr));
2066 addr.s6_addr32[0] = htonl(0xFE800000);
2068 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2069 addrconf_add_linklocal(idev, &addr);
2072 static void addrconf_sit_config(struct net_device *dev)
2074 struct inet6_dev *idev;
2079 * Configure the tunnel with one of our IPv4
2080 * addresses... we should configure all of
2081 * our v4 addrs in the tunnel
2084 if ((idev = ipv6_find_idev(dev)) == NULL) {
2085 printk(KERN_DEBUG "init sit: add_dev failed\n");
2089 sit_add_v4_addrs(idev);
2091 if (dev->flags&IFF_POINTOPOINT) {
2092 addrconf_add_mroute(dev);
2093 addrconf_add_lroute(dev);
2099 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2101 struct in6_addr lladdr;
2103 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2104 addrconf_add_linklocal(idev, &lladdr);
2110 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2112 struct net_device *link_dev;
2114 /* first try to inherit the link-local address from the link device */
2115 if (idev->dev->iflink &&
2116 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2117 if (!ipv6_inherit_linklocal(idev, link_dev))
2120 /* then try to inherit it from any device */
2121 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2122 if (!ipv6_inherit_linklocal(idev, link_dev))
2125 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2129 * Autoconfigure tunnel with a link-local address so routing protocols,
2130 * DHCPv6, MLD etc. can be run over the virtual link
2133 static void addrconf_ip6_tnl_config(struct net_device *dev)
2135 struct inet6_dev *idev;
2139 if ((idev = addrconf_add_dev(dev)) == NULL) {
2140 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2143 ip6_tnl_add_linklocal(idev);
2144 addrconf_add_mroute(dev);
2147 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2150 struct net_device *dev = (struct net_device *) data;
2151 struct inet6_dev *idev = __in6_dev_get(dev);
2152 int run_pending = 0;
2157 if (event == NETDEV_UP) {
2158 if (!netif_carrier_ok(dev)) {
2159 /* device is not ready yet. */
2161 "ADDRCONF(NETDEV_UP): %s: "
2162 "link is not ready\n",
2167 if (!netif_carrier_ok(dev)) {
2168 /* device is still not ready. */
2173 if (idev->if_flags & IF_READY) {
2174 /* device is already configured. */
2177 idev->if_flags |= IF_READY;
2181 "ADDRCONF(NETDEV_CHANGE): %s: "
2182 "link becomes ready\n",
2190 addrconf_sit_config(dev);
2192 case ARPHRD_TUNNEL6:
2193 addrconf_ip6_tnl_config(dev);
2195 case ARPHRD_LOOPBACK:
2200 addrconf_dev_config(dev);
2205 addrconf_dad_run(idev);
2207 /* If the MTU changed during the interface down, when the
2208 interface up, the changed MTU must be reflected in the
2209 idev as well as routers.
2211 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2212 rt6_mtu_change(dev, dev->mtu);
2213 idev->cnf.mtu6 = dev->mtu;
2215 idev->tstamp = jiffies;
2216 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2217 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2218 stop IPv6 on this interface.
2220 if (dev->mtu < IPV6_MIN_MTU)
2221 addrconf_ifdown(dev, event != NETDEV_DOWN);
2225 case NETDEV_CHANGEMTU:
2226 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2227 rt6_mtu_change(dev, dev->mtu);
2228 idev->cnf.mtu6 = dev->mtu;
2232 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2235 case NETDEV_UNREGISTER:
2237 * Remove all addresses from this interface.
2239 addrconf_ifdown(dev, event != NETDEV_DOWN);
2242 case NETDEV_CHANGENAME:
2243 #ifdef CONFIG_SYSCTL
2245 addrconf_sysctl_unregister(&idev->cnf);
2246 neigh_sysctl_unregister(idev->nd_parms);
2247 neigh_sysctl_register(dev, idev->nd_parms,
2248 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2249 &ndisc_ifinfo_sysctl_change,
2251 addrconf_sysctl_register(idev, &idev->cnf);
2261 * addrconf module should be notified of a device going up
2263 static struct notifier_block ipv6_dev_notf = {
2264 .notifier_call = addrconf_notify,
2268 static int addrconf_ifdown(struct net_device *dev, int how)
2270 struct inet6_dev *idev;
2271 struct inet6_ifaddr *ifa, **bifa;
2276 if (dev == &loopback_dev && how == 1)
2280 neigh_ifdown(&nd_tbl, dev);
2282 idev = __in6_dev_get(dev);
2286 /* Step 1: remove reference to ipv6 device from parent device.
2290 write_lock_bh(&addrconf_lock);
2291 dev->ip6_ptr = NULL;
2293 write_unlock_bh(&addrconf_lock);
2295 /* Step 1.5: remove snmp6 entry */
2296 snmp6_unregister_dev(idev);
2300 /* Step 2: clear hash table */
2301 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2302 bifa = &inet6_addr_lst[i];
2304 write_lock_bh(&addrconf_hash_lock);
2305 while ((ifa = *bifa) != NULL) {
2306 if (ifa->idev == idev) {
2307 *bifa = ifa->lst_next;
2308 ifa->lst_next = NULL;
2309 addrconf_del_timer(ifa);
2313 bifa = &ifa->lst_next;
2315 write_unlock_bh(&addrconf_hash_lock);
2318 write_lock_bh(&idev->lock);
2320 /* Step 3: clear flags for stateless addrconf */
2322 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2324 /* Step 4: clear address list */
2325 #ifdef CONFIG_IPV6_PRIVACY
2326 if (how == 1 && del_timer(&idev->regen_timer))
2329 /* clear tempaddr list */
2330 while ((ifa = idev->tempaddr_list) != NULL) {
2331 idev->tempaddr_list = ifa->tmp_next;
2332 ifa->tmp_next = NULL;
2334 write_unlock_bh(&idev->lock);
2335 spin_lock_bh(&ifa->lock);
2338 in6_ifa_put(ifa->ifpub);
2341 spin_unlock_bh(&ifa->lock);
2343 write_lock_bh(&idev->lock);
2346 while ((ifa = idev->addr_list) != NULL) {
2347 idev->addr_list = ifa->if_next;
2348 ifa->if_next = NULL;
2350 addrconf_del_timer(ifa);
2351 write_unlock_bh(&idev->lock);
2353 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2356 write_lock_bh(&idev->lock);
2358 write_unlock_bh(&idev->lock);
2360 /* Step 5: Discard multicast list */
2363 ipv6_mc_destroy_dev(idev);
2367 /* Step 5: netlink notification of this interface */
2368 idev->tstamp = jiffies;
2369 inet6_ifinfo_notify(RTM_DELLINK, idev);
2371 /* Shot the device (if unregistered) */
2374 #ifdef CONFIG_SYSCTL
2375 addrconf_sysctl_unregister(&idev->cnf);
2376 neigh_sysctl_unregister(idev->nd_parms);
2378 neigh_parms_release(&nd_tbl, idev->nd_parms);
2379 neigh_ifdown(&nd_tbl, dev);
2385 static void addrconf_rs_timer(unsigned long data)
2387 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2389 if (ifp->idev->cnf.forwarding)
2392 if (ifp->idev->if_flags & IF_RA_RCVD) {
2394 * Announcement received after solicitation
2400 spin_lock(&ifp->lock);
2401 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2402 struct in6_addr all_routers;
2404 /* The wait after the last probe can be shorter */
2405 addrconf_mod_timer(ifp, AC_RS,
2406 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2407 ifp->idev->cnf.rtr_solicit_delay :
2408 ifp->idev->cnf.rtr_solicit_interval);
2409 spin_unlock(&ifp->lock);
2411 ipv6_addr_all_routers(&all_routers);
2413 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2415 spin_unlock(&ifp->lock);
2417 * Note: we do not support deprecated "all on-link"
2418 * assumption any longer.
2420 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2421 ifp->idev->dev->name);
2429 * Duplicate Address Detection
2431 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2433 unsigned long rand_num;
2434 struct inet6_dev *idev = ifp->idev;
2436 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2437 ifp->probes = idev->cnf.dad_transmits;
2438 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2441 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2443 struct inet6_dev *idev = ifp->idev;
2444 struct net_device *dev = idev->dev;
2446 addrconf_join_solict(dev, &ifp->addr);
2448 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2449 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2452 net_srandom(ifp->addr.s6_addr32[3]);
2454 read_lock_bh(&idev->lock);
2457 spin_lock_bh(&ifp->lock);
2459 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2460 !(ifp->flags&IFA_F_TENTATIVE)) {
2461 ifp->flags &= ~IFA_F_TENTATIVE;
2462 spin_unlock_bh(&ifp->lock);
2463 read_unlock_bh(&idev->lock);
2465 addrconf_dad_completed(ifp);
2469 if (idev->if_flags & IF_READY)
2470 addrconf_dad_kick(ifp);
2473 * If the defice is not ready:
2474 * - keep it tentative if it is a permanent address.
2475 * - otherwise, kill it.
2478 addrconf_dad_stop(ifp);
2481 spin_unlock_bh(&ifp->lock);
2483 read_unlock_bh(&idev->lock);
2486 static void addrconf_dad_timer(unsigned long data)
2488 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2489 struct inet6_dev *idev = ifp->idev;
2490 struct in6_addr unspec;
2491 struct in6_addr mcaddr;
2493 read_lock_bh(&idev->lock);
2495 read_unlock_bh(&idev->lock);
2498 spin_lock_bh(&ifp->lock);
2499 if (ifp->probes == 0) {
2501 * DAD was successful
2504 ifp->flags &= ~IFA_F_TENTATIVE;
2505 spin_unlock_bh(&ifp->lock);
2506 read_unlock_bh(&idev->lock);
2508 addrconf_dad_completed(ifp);
2514 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2515 spin_unlock_bh(&ifp->lock);
2516 read_unlock_bh(&idev->lock);
2518 /* send a neighbour solicitation for our addr */
2519 memset(&unspec, 0, sizeof(unspec));
2520 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2521 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2526 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2528 struct net_device * dev = ifp->idev->dev;
2531 * Configure the address for reception. Now it is valid.
2534 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2536 /* If added prefix is link local and forwarding is off,
2537 start sending router solicitations.
2540 if (ifp->idev->cnf.forwarding == 0 &&
2541 ifp->idev->cnf.rtr_solicits > 0 &&
2542 (dev->flags&IFF_LOOPBACK) == 0 &&
2543 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2544 struct in6_addr all_routers;
2546 ipv6_addr_all_routers(&all_routers);
2549 * If a host as already performed a random delay
2550 * [...] as part of DAD [...] there is no need
2551 * to delay again before sending the first RS
2553 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2555 spin_lock_bh(&ifp->lock);
2557 ifp->idev->if_flags |= IF_RS_SENT;
2558 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2559 spin_unlock_bh(&ifp->lock);
2563 static void addrconf_dad_run(struct inet6_dev *idev) {
2564 struct inet6_ifaddr *ifp;
2566 read_lock_bh(&idev->lock);
2567 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2568 spin_lock_bh(&ifp->lock);
2569 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2570 spin_unlock_bh(&ifp->lock);
2573 spin_unlock_bh(&ifp->lock);
2574 addrconf_dad_kick(ifp);
2576 read_unlock_bh(&idev->lock);
2579 #ifdef CONFIG_PROC_FS
2580 struct if6_iter_state {
2584 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2586 struct inet6_ifaddr *ifa = NULL;
2587 struct if6_iter_state *state = seq->private;
2589 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2590 ifa = inet6_addr_lst[state->bucket];
2597 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2599 struct if6_iter_state *state = seq->private;
2601 ifa = ifa->lst_next;
2603 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2604 ifa = inet6_addr_lst[state->bucket];
2610 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2612 struct inet6_ifaddr *ifa = if6_get_first(seq);
2615 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2617 return pos ? NULL : ifa;
2620 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2622 read_lock_bh(&addrconf_hash_lock);
2623 return if6_get_idx(seq, *pos);
2626 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2628 struct inet6_ifaddr *ifa;
2630 ifa = if6_get_next(seq, v);
2635 static void if6_seq_stop(struct seq_file *seq, void *v)
2637 read_unlock_bh(&addrconf_hash_lock);
2640 static int if6_seq_show(struct seq_file *seq, void *v)
2642 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2644 "%04x%04x%04x%04x%04x%04x%04x%04x %02x %02x %02x %02x %8s\n",
2646 ifp->idev->dev->ifindex,
2650 ifp->idev->dev->name);
2654 static struct seq_operations if6_seq_ops = {
2655 .start = if6_seq_start,
2656 .next = if6_seq_next,
2657 .show = if6_seq_show,
2658 .stop = if6_seq_stop,
2661 static int if6_seq_open(struct inode *inode, struct file *file)
2663 struct seq_file *seq;
2665 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2669 memset(s, 0, sizeof(*s));
2671 rc = seq_open(file, &if6_seq_ops);
2675 seq = file->private_data;
2684 static struct file_operations if6_fops = {
2685 .owner = THIS_MODULE,
2686 .open = if6_seq_open,
2688 .llseek = seq_lseek,
2689 .release = seq_release_private,
2692 int __init if6_proc_init(void)
2694 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2699 void if6_proc_exit(void)
2701 proc_net_remove("if_inet6");
2703 #endif /* CONFIG_PROC_FS */
2706 * Periodic address status verification
2709 static void addrconf_verify(unsigned long foo)
2711 struct inet6_ifaddr *ifp;
2712 unsigned long now, next;
2715 spin_lock_bh(&addrconf_verify_lock);
2717 next = now + ADDR_CHECK_FREQUENCY;
2719 del_timer(&addr_chk_timer);
2721 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2724 read_lock(&addrconf_hash_lock);
2725 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2727 #ifdef CONFIG_IPV6_PRIVACY
2728 unsigned long regen_advance;
2731 if (ifp->flags & IFA_F_PERMANENT)
2734 spin_lock(&ifp->lock);
2735 age = (now - ifp->tstamp) / HZ;
2737 #ifdef CONFIG_IPV6_PRIVACY
2738 regen_advance = ifp->idev->cnf.regen_max_retry *
2739 ifp->idev->cnf.dad_transmits *
2740 ifp->idev->nd_parms->retrans_time / HZ;
2743 if (age >= ifp->valid_lft) {
2744 spin_unlock(&ifp->lock);
2746 read_unlock(&addrconf_hash_lock);
2749 } else if (age >= ifp->prefered_lft) {
2750 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2753 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2755 ifp->flags |= IFA_F_DEPRECATED;
2758 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2759 next = ifp->tstamp + ifp->valid_lft * HZ;
2761 spin_unlock(&ifp->lock);
2765 read_unlock(&addrconf_hash_lock);
2767 ipv6_ifa_notify(0, ifp);
2771 #ifdef CONFIG_IPV6_PRIVACY
2772 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2773 !(ifp->flags&IFA_F_TENTATIVE)) {
2774 if (age >= ifp->prefered_lft - regen_advance) {
2775 struct inet6_ifaddr *ifpub = ifp->ifpub;
2776 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2777 next = ifp->tstamp + ifp->prefered_lft * HZ;
2778 if (!ifp->regen_count && ifpub) {
2781 in6_ifa_hold(ifpub);
2782 spin_unlock(&ifp->lock);
2783 read_unlock(&addrconf_hash_lock);
2784 ipv6_create_tempaddr(ifpub, ifp);
2789 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2790 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2791 spin_unlock(&ifp->lock);
2794 /* ifp->prefered_lft <= ifp->valid_lft */
2795 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2796 next = ifp->tstamp + ifp->prefered_lft * HZ;
2797 spin_unlock(&ifp->lock);
2800 read_unlock(&addrconf_hash_lock);
2803 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2804 add_timer(&addr_chk_timer);
2805 spin_unlock_bh(&addrconf_verify_lock);
2809 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2811 struct rtattr **rta = arg;
2812 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2813 struct in6_addr *pfx;
2816 if (rta[IFA_ADDRESS-1]) {
2817 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2819 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2821 if (rta[IFA_LOCAL-1]) {
2822 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2824 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2829 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2833 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2835 struct rtattr **rta = arg;
2836 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2837 struct in6_addr *pfx;
2840 if (rta[IFA_ADDRESS-1]) {
2841 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2843 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2845 if (rta[IFA_LOCAL-1]) {
2846 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2848 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2853 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2856 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2857 u32 pid, u32 seq, int event, unsigned int flags)
2859 struct ifaddrmsg *ifm;
2860 struct nlmsghdr *nlh;
2861 struct ifa_cacheinfo ci;
2862 unsigned char *b = skb->tail;
2864 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2865 ifm = NLMSG_DATA(nlh);
2866 ifm->ifa_family = AF_INET6;
2867 ifm->ifa_prefixlen = ifa->prefix_len;
2868 ifm->ifa_flags = ifa->flags;
2869 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2870 if (ifa->scope&IFA_HOST)
2871 ifm->ifa_scope = RT_SCOPE_HOST;
2872 else if (ifa->scope&IFA_LINK)
2873 ifm->ifa_scope = RT_SCOPE_LINK;
2874 else if (ifa->scope&IFA_SITE)
2875 ifm->ifa_scope = RT_SCOPE_SITE;
2876 ifm->ifa_index = ifa->idev->dev->ifindex;
2877 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2878 if (!(ifa->flags&IFA_F_PERMANENT)) {
2879 ci.ifa_prefered = ifa->prefered_lft;
2880 ci.ifa_valid = ifa->valid_lft;
2881 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2882 long tval = (jiffies - ifa->tstamp)/HZ;
2883 ci.ifa_prefered -= tval;
2884 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2885 ci.ifa_valid -= tval;
2888 ci.ifa_prefered = INFINITY_LIFE_TIME;
2889 ci.ifa_valid = INFINITY_LIFE_TIME;
2891 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2892 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2893 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2894 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2895 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2896 nlh->nlmsg_len = skb->tail - b;
2901 skb_trim(skb, b - skb->data);
2905 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2906 u32 pid, u32 seq, int event, u16 flags)
2908 struct ifaddrmsg *ifm;
2909 struct nlmsghdr *nlh;
2910 struct ifa_cacheinfo ci;
2911 unsigned char *b = skb->tail;
2913 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2914 ifm = NLMSG_DATA(nlh);
2915 ifm->ifa_family = AF_INET6;
2916 ifm->ifa_prefixlen = 128;
2917 ifm->ifa_flags = IFA_F_PERMANENT;
2918 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2919 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2920 ifm->ifa_scope = RT_SCOPE_SITE;
2921 ifm->ifa_index = ifmca->idev->dev->ifindex;
2922 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2923 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2924 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2926 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2927 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2929 ci.ifa_prefered = INFINITY_LIFE_TIME;
2930 ci.ifa_valid = INFINITY_LIFE_TIME;
2931 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2932 nlh->nlmsg_len = skb->tail - b;
2937 skb_trim(skb, b - skb->data);
2941 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2942 u32 pid, u32 seq, int event, unsigned int flags)
2944 struct ifaddrmsg *ifm;
2945 struct nlmsghdr *nlh;
2946 struct ifa_cacheinfo ci;
2947 unsigned char *b = skb->tail;
2949 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2950 ifm = NLMSG_DATA(nlh);
2951 ifm->ifa_family = AF_INET6;
2952 ifm->ifa_prefixlen = 128;
2953 ifm->ifa_flags = IFA_F_PERMANENT;
2954 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2955 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2956 ifm->ifa_scope = RT_SCOPE_SITE;
2957 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2958 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2959 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2960 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2962 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2963 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2965 ci.ifa_prefered = INFINITY_LIFE_TIME;
2966 ci.ifa_valid = INFINITY_LIFE_TIME;
2967 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2968 nlh->nlmsg_len = skb->tail - b;
2973 skb_trim(skb, b - skb->data);
2984 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2985 enum addr_type_t type)
2988 int s_idx, s_ip_idx;
2990 struct net_device *dev;
2991 struct inet6_dev *idev = NULL;
2992 struct inet6_ifaddr *ifa;
2993 struct ifmcaddr6 *ifmca;
2994 struct ifacaddr6 *ifaca;
2996 s_idx = cb->args[0];
2997 s_ip_idx = ip_idx = cb->args[1];
2998 read_lock(&dev_base_lock);
3000 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3006 if ((idev = in6_dev_get(dev)) == NULL)
3008 read_lock_bh(&idev->lock);
3011 /* unicast address incl. temp addr */
3012 for (ifa = idev->addr_list; ifa;
3013 ifa = ifa->if_next, ip_idx++) {
3014 if (ip_idx < s_ip_idx)
3016 if ((err = inet6_fill_ifaddr(skb, ifa,
3017 NETLINK_CB(cb->skb).pid,
3018 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3023 case MULTICAST_ADDR:
3024 /* multicast address */
3025 for (ifmca = idev->mc_list; ifmca;
3026 ifmca = ifmca->next, ip_idx++) {
3027 if (ip_idx < s_ip_idx)
3029 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3030 NETLINK_CB(cb->skb).pid,
3031 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3037 /* anycast address */
3038 for (ifaca = idev->ac_list; ifaca;
3039 ifaca = ifaca->aca_next, ip_idx++) {
3040 if (ip_idx < s_ip_idx)
3042 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3043 NETLINK_CB(cb->skb).pid,
3044 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3052 read_unlock_bh(&idev->lock);
3057 read_unlock_bh(&idev->lock);
3060 read_unlock(&dev_base_lock);
3062 cb->args[1] = ip_idx;
3066 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3068 enum addr_type_t type = UNICAST_ADDR;
3069 return inet6_dump_addr(skb, cb, type);
3072 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3074 enum addr_type_t type = MULTICAST_ADDR;
3075 return inet6_dump_addr(skb, cb, type);
3079 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3081 enum addr_type_t type = ANYCAST_ADDR;
3082 return inet6_dump_addr(skb, cb, type);
3085 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3087 struct sk_buff *skb;
3088 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
3090 skb = alloc_skb(size, GFP_ATOMIC);
3092 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
3095 if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
3097 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
3100 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
3101 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
3104 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3105 __s32 *array, int bytes)
3107 memset(array, 0, bytes);
3108 array[DEVCONF_FORWARDING] = cnf->forwarding;
3109 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3110 array[DEVCONF_MTU6] = cnf->mtu6;
3111 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3112 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3113 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3114 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3115 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3116 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3117 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3118 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3119 #ifdef CONFIG_IPV6_PRIVACY
3120 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3121 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3122 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3123 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3124 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3126 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3129 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3130 u32 pid, u32 seq, int event, unsigned int flags)
3132 struct net_device *dev = idev->dev;
3133 __s32 *array = NULL;
3134 struct ifinfomsg *r;
3135 struct nlmsghdr *nlh;
3136 unsigned char *b = skb->tail;
3137 struct rtattr *subattr;
3138 __u32 mtu = dev->mtu;
3139 struct ifla_cacheinfo ci;
3141 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
3142 r = NLMSG_DATA(nlh);
3143 r->ifi_family = AF_INET6;
3145 r->ifi_type = dev->type;
3146 r->ifi_index = dev->ifindex;
3147 r->ifi_flags = dev_get_flags(dev);
3150 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
3153 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3155 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
3156 if (dev->ifindex != dev->iflink)
3157 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
3159 subattr = (struct rtattr*)skb->tail;
3161 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
3163 /* return the device flags */
3164 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
3166 /* return interface cacheinfo */
3167 ci.max_reasm_len = IPV6_MAXPLEN;
3168 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3169 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3170 ci.reachable_time = idev->nd_parms->reachable_time;
3171 ci.retrans_time = idev->nd_parms->retrans_time;
3172 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3174 /* return the device sysctl params */
3175 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
3176 goto rtattr_failure;
3177 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
3178 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
3180 /* XXX - Statistics/MC not implemented */
3181 subattr->rta_len = skb->tail - (u8*)subattr;
3183 nlh->nlmsg_len = skb->tail - b;
3190 skb_trim(skb, b - skb->data);
3194 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3197 int s_idx = cb->args[0];
3198 struct net_device *dev;
3199 struct inet6_dev *idev;
3201 read_lock(&dev_base_lock);
3202 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3205 if ((idev = in6_dev_get(dev)) == NULL)
3207 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3208 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3213 read_unlock(&dev_base_lock);
3219 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3221 struct sk_buff *skb;
3223 int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
3225 skb = alloc_skb(size, GFP_ATOMIC);
3227 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
3230 if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
3232 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
3235 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
3236 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
3239 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3240 struct prefix_info *pinfo, u32 pid, u32 seq,
3241 int event, unsigned int flags)
3243 struct prefixmsg *pmsg;
3244 struct nlmsghdr *nlh;
3245 unsigned char *b = skb->tail;
3246 struct prefix_cacheinfo ci;
3248 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
3249 pmsg = NLMSG_DATA(nlh);
3250 pmsg->prefix_family = AF_INET6;
3251 pmsg->prefix_pad1 = 0;
3252 pmsg->prefix_pad2 = 0;
3253 pmsg->prefix_ifindex = idev->dev->ifindex;
3254 pmsg->prefix_len = pinfo->prefix_len;
3255 pmsg->prefix_type = pinfo->type;
3256 pmsg->prefix_pad3 = 0;
3258 pmsg->prefix_flags = 0;
3260 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3261 if (pinfo->autoconf)
3262 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3264 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3266 ci.preferred_time = ntohl(pinfo->prefered);
3267 ci.valid_time = ntohl(pinfo->valid);
3268 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3270 nlh->nlmsg_len = skb->tail - b;
3275 skb_trim(skb, b - skb->data);
3279 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3280 struct prefix_info *pinfo)
3282 struct sk_buff *skb;
3283 int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
3285 skb = alloc_skb(size, GFP_ATOMIC);
3287 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
3290 if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
3292 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
3295 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
3296 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
3299 static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
3300 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3301 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3302 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
3303 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
3304 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3305 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3306 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3307 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3308 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3309 .dumpit = inet6_dump_fib, },
3312 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3314 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3318 dst_hold(&ifp->rt->u.dst);
3319 if (ip6_ins_rt(ifp->rt, NULL, NULL, NULL))
3320 dst_release(&ifp->rt->u.dst);
3321 if (ifp->idev->cnf.forwarding)
3322 addrconf_join_anycast(ifp);
3325 if (ifp->idev->cnf.forwarding)
3326 addrconf_leave_anycast(ifp);
3327 addrconf_leave_solict(ifp->idev, &ifp->addr);
3328 dst_hold(&ifp->rt->u.dst);
3329 if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
3330 dst_free(&ifp->rt->u.dst);
3332 dst_release(&ifp->rt->u.dst);
3337 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3339 read_lock_bh(&addrconf_lock);
3340 if (likely(ifp->idev->dead == 0))
3341 __ipv6_ifa_notify(event, ifp);
3342 read_unlock_bh(&addrconf_lock);
3345 #ifdef CONFIG_SYSCTL
3348 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3349 void __user *buffer, size_t *lenp, loff_t *ppos)
3351 int *valp = ctl->data;
3355 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3357 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3358 if (valp != &ipv6_devconf.forwarding) {
3359 if ((!*valp) ^ (!val)) {
3360 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3363 dev_forward_change(idev);
3366 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3367 addrconf_forward_change();
3370 rt6_purge_dflt_routers();
3376 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3377 int __user *name, int nlen,
3378 void __user *oldval,
3379 size_t __user *oldlenp,
3380 void __user *newval, size_t newlen,
3383 int *valp = table->data;
3386 if (!newval || !newlen)
3388 if (newlen != sizeof(int))
3390 if (get_user(new, (int __user *)newval))
3394 if (oldval && oldlenp) {
3396 if (get_user(len, oldlenp))
3399 if (len > table->maxlen)
3400 len = table->maxlen;
3401 if (copy_to_user(oldval, valp, len))
3403 if (put_user(len, oldlenp))
3408 if (valp != &ipv6_devconf_dflt.forwarding) {
3409 if (valp != &ipv6_devconf.forwarding) {
3410 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3412 if (unlikely(idev == NULL))
3414 changed = (!*valp) ^ (!new);
3417 dev_forward_change(idev);
3420 addrconf_forward_change();
3424 rt6_purge_dflt_routers();
3431 static struct addrconf_sysctl_table
3433 struct ctl_table_header *sysctl_header;
3434 ctl_table addrconf_vars[__NET_IPV6_MAX];
3435 ctl_table addrconf_dev[2];
3436 ctl_table addrconf_conf_dir[2];
3437 ctl_table addrconf_proto_dir[2];
3438 ctl_table addrconf_root_dir[2];
3439 } addrconf_sysctl = {
3440 .sysctl_header = NULL,
3443 .ctl_name = NET_IPV6_FORWARDING,
3444 .procname = "forwarding",
3445 .data = &ipv6_devconf.forwarding,
3446 .maxlen = sizeof(int),
3448 .proc_handler = &addrconf_sysctl_forward,
3449 .strategy = &addrconf_sysctl_forward_strategy,
3452 .ctl_name = NET_IPV6_HOP_LIMIT,
3453 .procname = "hop_limit",
3454 .data = &ipv6_devconf.hop_limit,
3455 .maxlen = sizeof(int),
3457 .proc_handler = proc_dointvec,
3460 .ctl_name = NET_IPV6_MTU,
3462 .data = &ipv6_devconf.mtu6,
3463 .maxlen = sizeof(int),
3465 .proc_handler = &proc_dointvec,
3468 .ctl_name = NET_IPV6_ACCEPT_RA,
3469 .procname = "accept_ra",
3470 .data = &ipv6_devconf.accept_ra,
3471 .maxlen = sizeof(int),
3473 .proc_handler = &proc_dointvec,
3476 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3477 .procname = "accept_redirects",
3478 .data = &ipv6_devconf.accept_redirects,
3479 .maxlen = sizeof(int),
3481 .proc_handler = &proc_dointvec,
3484 .ctl_name = NET_IPV6_AUTOCONF,
3485 .procname = "autoconf",
3486 .data = &ipv6_devconf.autoconf,
3487 .maxlen = sizeof(int),
3489 .proc_handler = &proc_dointvec,
3492 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3493 .procname = "dad_transmits",
3494 .data = &ipv6_devconf.dad_transmits,
3495 .maxlen = sizeof(int),
3497 .proc_handler = &proc_dointvec,
3500 .ctl_name = NET_IPV6_RTR_SOLICITS,
3501 .procname = "router_solicitations",
3502 .data = &ipv6_devconf.rtr_solicits,
3503 .maxlen = sizeof(int),
3505 .proc_handler = &proc_dointvec,
3508 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3509 .procname = "router_solicitation_interval",
3510 .data = &ipv6_devconf.rtr_solicit_interval,
3511 .maxlen = sizeof(int),
3513 .proc_handler = &proc_dointvec_jiffies,
3514 .strategy = &sysctl_jiffies,
3517 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3518 .procname = "router_solicitation_delay",
3519 .data = &ipv6_devconf.rtr_solicit_delay,
3520 .maxlen = sizeof(int),
3522 .proc_handler = &proc_dointvec_jiffies,
3523 .strategy = &sysctl_jiffies,
3526 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3527 .procname = "force_mld_version",
3528 .data = &ipv6_devconf.force_mld_version,
3529 .maxlen = sizeof(int),
3531 .proc_handler = &proc_dointvec,
3533 #ifdef CONFIG_IPV6_PRIVACY
3535 .ctl_name = NET_IPV6_USE_TEMPADDR,
3536 .procname = "use_tempaddr",
3537 .data = &ipv6_devconf.use_tempaddr,
3538 .maxlen = sizeof(int),
3540 .proc_handler = &proc_dointvec,
3543 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3544 .procname = "temp_valid_lft",
3545 .data = &ipv6_devconf.temp_valid_lft,
3546 .maxlen = sizeof(int),
3548 .proc_handler = &proc_dointvec,
3551 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3552 .procname = "temp_prefered_lft",
3553 .data = &ipv6_devconf.temp_prefered_lft,
3554 .maxlen = sizeof(int),
3556 .proc_handler = &proc_dointvec,
3559 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3560 .procname = "regen_max_retry",
3561 .data = &ipv6_devconf.regen_max_retry,
3562 .maxlen = sizeof(int),
3564 .proc_handler = &proc_dointvec,
3567 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3568 .procname = "max_desync_factor",
3569 .data = &ipv6_devconf.max_desync_factor,
3570 .maxlen = sizeof(int),
3572 .proc_handler = &proc_dointvec,
3576 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3577 .procname = "max_addresses",
3578 .data = &ipv6_devconf.max_addresses,
3579 .maxlen = sizeof(int),
3581 .proc_handler = &proc_dointvec,
3584 .ctl_name = 0, /* sentinel */
3589 .ctl_name = NET_PROTO_CONF_ALL,
3592 .child = addrconf_sysctl.addrconf_vars,
3595 .ctl_name = 0, /* sentinel */
3598 .addrconf_conf_dir = {
3600 .ctl_name = NET_IPV6_CONF,
3603 .child = addrconf_sysctl.addrconf_dev,
3606 .ctl_name = 0, /* sentinel */
3609 .addrconf_proto_dir = {
3611 .ctl_name = NET_IPV6,
3614 .child = addrconf_sysctl.addrconf_conf_dir,
3617 .ctl_name = 0, /* sentinel */
3620 .addrconf_root_dir = {
3622 .ctl_name = CTL_NET,
3625 .child = addrconf_sysctl.addrconf_proto_dir,
3628 .ctl_name = 0, /* sentinel */
3633 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3636 struct net_device *dev = idev ? idev->dev : NULL;
3637 struct addrconf_sysctl_table *t;
3638 char *dev_name = NULL;
3640 t = kmalloc(sizeof(*t), GFP_KERNEL);
3643 memcpy(t, &addrconf_sysctl, sizeof(*t));
3644 for (i=0; t->addrconf_vars[i].data; i++) {
3645 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3646 t->addrconf_vars[i].de = NULL;
3647 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3650 dev_name = dev->name;
3651 t->addrconf_dev[0].ctl_name = dev->ifindex;
3653 dev_name = "default";
3654 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3658 * Make a copy of dev_name, because '.procname' is regarded as const
3659 * by sysctl and we wouldn't want anyone to change it under our feet
3660 * (see SIOCSIFNAME).
3662 dev_name = kstrdup(dev_name, GFP_KERNEL);
3666 t->addrconf_dev[0].procname = dev_name;
3668 t->addrconf_dev[0].child = t->addrconf_vars;
3669 t->addrconf_dev[0].de = NULL;
3670 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3671 t->addrconf_conf_dir[0].de = NULL;
3672 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3673 t->addrconf_proto_dir[0].de = NULL;
3674 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3675 t->addrconf_root_dir[0].de = NULL;
3677 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3678 if (t->sysctl_header == NULL)
3693 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3696 struct addrconf_sysctl_table *t = p->sysctl;
3698 unregister_sysctl_table(t->sysctl_header);
3699 kfree(t->addrconf_dev[0].procname);
3711 int register_inet6addr_notifier(struct notifier_block *nb)
3713 return notifier_chain_register(&inet6addr_chain, nb);
3716 int unregister_inet6addr_notifier(struct notifier_block *nb)
3718 return notifier_chain_unregister(&inet6addr_chain,nb);
3722 * Init / cleanup code
3725 int __init addrconf_init(void)
3729 /* The addrconf netdev notifier requires that loopback_dev
3730 * has it's ipv6 private information allocated and setup
3731 * before it can bring up and give link-local addresses
3732 * to other devices which are up.
3734 * Unfortunately, loopback_dev is not necessarily the first
3735 * entry in the global dev_base list of net devices. In fact,
3736 * it is likely to be the very last entry on that list.
3737 * So this causes the notifier registry below to try and
3738 * give link-local addresses to all devices besides loopback_dev
3739 * first, then loopback_dev, which cases all the non-loopback_dev
3740 * devices to fail to get a link-local address.
3742 * So, as a temporary fix, allocate the ipv6 structure for
3743 * loopback_dev first by hand.
3744 * Longer term, all of the dependencies ipv6 has upon the loopback
3745 * device and it being up should be removed.
3748 if (!ipv6_add_dev(&loopback_dev))
3754 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
3756 register_netdevice_notifier(&ipv6_dev_notf);
3758 #ifdef CONFIG_IPV6_PRIVACY
3759 md5_tfm = crypto_alloc_tfm("md5", 0);
3760 if (unlikely(md5_tfm == NULL))
3762 "failed to load transform for md5\n");
3766 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3767 #ifdef CONFIG_SYSCTL
3768 addrconf_sysctl.sysctl_header =
3769 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3770 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3776 void __exit addrconf_cleanup(void)
3778 struct net_device *dev;
3779 struct inet6_dev *idev;
3780 struct inet6_ifaddr *ifa;
3783 unregister_netdevice_notifier(&ipv6_dev_notf);
3785 rtnetlink_links[PF_INET6] = NULL;
3786 #ifdef CONFIG_SYSCTL
3787 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3788 addrconf_sysctl_unregister(&ipv6_devconf);
3797 for (dev=dev_base; dev; dev=dev->next) {
3798 if ((idev = __in6_dev_get(dev)) == NULL)
3800 addrconf_ifdown(dev, 1);
3802 addrconf_ifdown(&loopback_dev, 2);
3808 write_lock_bh(&addrconf_hash_lock);
3809 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3810 for (ifa=inet6_addr_lst[i]; ifa; ) {
3811 struct inet6_ifaddr *bifa;
3814 ifa = ifa->lst_next;
3815 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3816 /* Do not free it; something is wrong.
3817 Now we can investigate it with debugger.
3821 write_unlock_bh(&addrconf_hash_lock);
3823 del_timer(&addr_chk_timer);
3827 #ifdef CONFIG_IPV6_PRIVACY
3828 crypto_free_tfm(md5_tfm);
3832 #ifdef CONFIG_PROC_FS
3833 proc_net_remove("if_inet6");