2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill double kfree on module
24 * Maciej W. Rozycki : FDDI support
25 * sekiya@USAGI : Don't send too many RS
27 * yoshfuji@USAGI : Fixed interval between DAD
29 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
30 * address validation timer.
31 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
33 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
34 * address on a same interface.
35 * YOSHIFUJI Hideaki @USAGI : ARCnet support
36 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
38 * YOSHIFUJI Hideaki @USAGI : improved source address
39 * selection; consider scope,
43 #include <linux/config.h>
44 #include <linux/errno.h>
45 #include <linux/types.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/sched.h>
49 #include <linux/net.h>
50 #include <linux/in6.h>
51 #include <linux/netdevice.h>
52 #include <linux/if_arp.h>
53 #include <linux/if_arcnet.h>
54 #include <linux/if_infiniband.h>
55 #include <linux/route.h>
56 #include <linux/inetdevice.h>
57 #include <linux/init.h>
59 #include <linux/sysctl.h>
61 #include <linux/capability.h>
62 #include <linux/delay.h>
63 #include <linux/notifier.h>
64 #include <linux/string.h>
70 #include <net/protocol.h>
71 #include <net/ndisc.h>
72 #include <net/ip6_route.h>
73 #include <net/addrconf.h>
76 #include <linux/if_tunnel.h>
77 #include <linux/rtnetlink.h>
79 #ifdef CONFIG_IPV6_PRIVACY
80 #include <linux/random.h>
81 #include <linux/crypto.h>
82 #include <linux/scatterlist.h>
85 #include <asm/uaccess.h>
87 #include <linux/proc_fs.h>
88 #include <linux/seq_file.h>
90 /* Set to 3 to get tracing... */
94 #define ADBG(x) printk x
99 #define INFINITY_LIFE_TIME 0xFFFFFFFF
100 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
103 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
104 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
107 #ifdef CONFIG_IPV6_PRIVACY
108 static int __ipv6_regen_rndid(struct inet6_dev *idev);
109 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
110 static void ipv6_regen_rndid(unsigned long data);
112 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
113 static struct crypto_tfm *md5_tfm;
114 static DEFINE_SPINLOCK(md5_tfm_lock);
117 static int ipv6_count_addresses(struct inet6_dev *idev);
120 * Configured unicast address hash table
122 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
123 static DEFINE_RWLOCK(addrconf_hash_lock);
125 /* Protects inet6 devices */
126 DEFINE_RWLOCK(addrconf_lock);
128 static void addrconf_verify(unsigned long);
130 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
131 static DEFINE_SPINLOCK(addrconf_verify_lock);
133 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
134 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
136 static int addrconf_ifdown(struct net_device *dev, int how);
138 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
139 static void addrconf_dad_timer(unsigned long data);
140 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
141 static void addrconf_dad_run(struct inet6_dev *idev);
142 static void addrconf_rs_timer(unsigned long data);
143 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
144 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
146 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
147 struct prefix_info *pinfo);
148 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
150 static struct notifier_block *inet6addr_chain;
152 struct ipv6_devconf ipv6_devconf = {
154 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
155 .mtu6 = IPV6_MIN_MTU,
157 .accept_redirects = 1,
159 .force_mld_version = 0,
161 .rtr_solicits = MAX_RTR_SOLICITATIONS,
162 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
163 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
164 #ifdef CONFIG_IPV6_PRIVACY
166 .temp_valid_lft = TEMP_VALID_LIFETIME,
167 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
168 .regen_max_retry = REGEN_MAX_RETRY,
169 .max_desync_factor = MAX_DESYNC_FACTOR,
171 .max_addresses = IPV6_MAX_ADDRESSES,
174 static struct ipv6_devconf ipv6_devconf_dflt = {
176 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
177 .mtu6 = IPV6_MIN_MTU,
179 .accept_redirects = 1,
182 .rtr_solicits = MAX_RTR_SOLICITATIONS,
183 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
184 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
185 #ifdef CONFIG_IPV6_PRIVACY
187 .temp_valid_lft = TEMP_VALID_LIFETIME,
188 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
189 .regen_max_retry = REGEN_MAX_RETRY,
190 .max_desync_factor = MAX_DESYNC_FACTOR,
192 .max_addresses = IPV6_MAX_ADDRESSES,
195 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
197 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
199 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
201 #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
203 static inline unsigned ipv6_addr_scope2type(unsigned scope)
206 case IPV6_ADDR_SCOPE_NODELOCAL:
207 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
209 case IPV6_ADDR_SCOPE_LINKLOCAL:
210 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
211 IPV6_ADDR_LINKLOCAL);
212 case IPV6_ADDR_SCOPE_SITELOCAL:
213 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
214 IPV6_ADDR_SITELOCAL);
216 return IPV6_ADDR_SCOPE_TYPE(scope);
219 int __ipv6_addr_type(const struct in6_addr *addr)
223 st = addr->s6_addr32[0];
225 /* Consider all addresses with the first three bits different of
226 000 and 111 as unicasts.
228 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
229 (st & htonl(0xE0000000)) != htonl(0xE0000000))
230 return (IPV6_ADDR_UNICAST |
231 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
233 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
235 /* addr-select 3.1 */
236 return (IPV6_ADDR_MULTICAST |
237 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
240 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
241 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
242 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
243 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
244 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
245 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
247 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
248 if (addr->s6_addr32[2] == 0) {
249 if (addr->s6_addr32[3] == 0)
250 return IPV6_ADDR_ANY;
252 if (addr->s6_addr32[3] == htonl(0x00000001))
253 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
254 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
256 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
257 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
260 if (addr->s6_addr32[2] == htonl(0x0000ffff))
261 return (IPV6_ADDR_MAPPED |
262 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
265 return (IPV6_ADDR_RESERVED |
266 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
269 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
271 if (del_timer(&ifp->timer))
275 enum addrconf_timer_t
282 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
283 enum addrconf_timer_t what,
286 if (!del_timer(&ifp->timer))
291 ifp->timer.function = addrconf_dad_timer;
294 ifp->timer.function = addrconf_rs_timer;
298 ifp->timer.expires = jiffies + when;
299 add_timer(&ifp->timer);
302 /* Nobody refers to this device, we may destroy it. */
304 void in6_dev_finish_destroy(struct inet6_dev *idev)
306 struct net_device *dev = idev->dev;
307 BUG_TRAP(idev->addr_list==NULL);
308 BUG_TRAP(idev->mc_list==NULL);
309 #ifdef NET_REFCNT_DEBUG
310 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
314 printk("Freeing alive inet6 device %p\n", idev);
317 snmp6_free_dev(idev);
321 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
323 struct inet6_dev *ndev;
324 struct in6_addr maddr;
328 if (dev->mtu < IPV6_MIN_MTU)
331 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
334 memset(ndev, 0, sizeof(struct inet6_dev));
336 rwlock_init(&ndev->lock);
338 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
339 ndev->cnf.mtu6 = dev->mtu;
340 ndev->cnf.sysctl = NULL;
341 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
342 if (ndev->nd_parms == NULL) {
346 /* We refer to the device */
349 if (snmp6_alloc_dev(ndev) < 0) {
351 "%s(): cannot allocate memory for statistics; dev=%s.\n",
352 __FUNCTION__, dev->name));
353 neigh_parms_release(&nd_tbl, ndev->nd_parms);
355 in6_dev_finish_destroy(ndev);
359 if (snmp6_register_dev(ndev) < 0) {
361 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
362 __FUNCTION__, dev->name));
363 neigh_parms_release(&nd_tbl, ndev->nd_parms);
365 in6_dev_finish_destroy(ndev);
369 /* One reference from device. We must do this before
370 * we invoke __ipv6_regen_rndid().
374 #ifdef CONFIG_IPV6_PRIVACY
375 get_random_bytes(ndev->rndid, sizeof(ndev->rndid));
376 get_random_bytes(ndev->entropy, sizeof(ndev->entropy));
377 init_timer(&ndev->regen_timer);
378 ndev->regen_timer.function = ipv6_regen_rndid;
379 ndev->regen_timer.data = (unsigned long) ndev;
380 if ((dev->flags&IFF_LOOPBACK) ||
381 dev->type == ARPHRD_TUNNEL ||
382 dev->type == ARPHRD_NONE ||
383 dev->type == ARPHRD_SIT) {
385 "%s: Disabled Privacy Extensions\n",
387 ndev->cnf.use_tempaddr = -1;
390 ipv6_regen_rndid((unsigned long) ndev);
394 if (netif_carrier_ok(dev))
395 ndev->if_flags |= IF_READY;
397 ipv6_mc_init_dev(ndev);
398 ndev->tstamp = jiffies;
400 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
401 NET_IPV6_NEIGH, "ipv6",
402 &ndisc_ifinfo_sysctl_change,
404 addrconf_sysctl_register(ndev, &ndev->cnf);
406 write_lock_bh(&addrconf_lock);
408 write_unlock_bh(&addrconf_lock);
410 /* Join all-node multicast group */
411 ipv6_addr_all_nodes(&maddr);
412 ipv6_dev_mc_inc(dev, &maddr);
417 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
419 struct inet6_dev *idev;
423 if ((idev = __in6_dev_get(dev)) == NULL) {
424 if ((idev = ipv6_add_dev(dev)) == NULL)
428 if (dev->flags&IFF_UP)
434 static void dev_forward_change(struct inet6_dev *idev)
436 struct net_device *dev;
437 struct inet6_ifaddr *ifa;
438 struct in6_addr addr;
443 if (dev && (dev->flags & IFF_MULTICAST)) {
444 ipv6_addr_all_routers(&addr);
446 if (idev->cnf.forwarding)
447 ipv6_dev_mc_inc(dev, &addr);
449 ipv6_dev_mc_dec(dev, &addr);
451 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
452 if (idev->cnf.forwarding)
453 addrconf_join_anycast(ifa);
455 addrconf_leave_anycast(ifa);
460 static void addrconf_forward_change(void)
462 struct net_device *dev;
463 struct inet6_dev *idev;
465 read_lock(&dev_base_lock);
466 for (dev=dev_base; dev; dev=dev->next) {
467 read_lock(&addrconf_lock);
468 idev = __in6_dev_get(dev);
470 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
471 idev->cnf.forwarding = ipv6_devconf.forwarding;
473 dev_forward_change(idev);
475 read_unlock(&addrconf_lock);
477 read_unlock(&dev_base_lock);
481 /* Nobody refers to this ifaddr, destroy it */
483 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
485 BUG_TRAP(ifp->if_next==NULL);
486 BUG_TRAP(ifp->lst_next==NULL);
487 #ifdef NET_REFCNT_DEBUG
488 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
491 in6_dev_put(ifp->idev);
493 if (del_timer(&ifp->timer))
494 printk("Timer is still running, when freeing ifa=%p\n", ifp);
497 printk("Freeing alive inet6 address %p\n", ifp);
500 dst_release(&ifp->rt->u.dst);
505 /* On success it returns ifp with increased reference count */
507 static struct inet6_ifaddr *
508 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
509 int scope, u32 flags)
511 struct inet6_ifaddr *ifa = NULL;
516 read_lock_bh(&addrconf_lock);
518 err = -ENODEV; /*XXX*/
522 write_lock(&addrconf_hash_lock);
524 /* Ignore adding duplicate addresses on an interface */
525 if (ipv6_chk_same_addr(addr, idev->dev)) {
526 ADBG(("ipv6_add_addr: already assigned\n"));
531 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
534 ADBG(("ipv6_add_addr: malloc failed\n"));
539 rt = addrconf_dst_alloc(idev, addr, 0);
545 memset(ifa, 0, sizeof(struct inet6_ifaddr));
546 ipv6_addr_copy(&ifa->addr, addr);
548 spin_lock_init(&ifa->lock);
549 init_timer(&ifa->timer);
550 ifa->timer.data = (unsigned long) ifa;
552 ifa->prefix_len = pfxlen;
553 ifa->flags = flags | IFA_F_TENTATIVE;
554 ifa->cstamp = ifa->tstamp = jiffies;
561 /* Add to big hash table */
562 hash = ipv6_addr_hash(addr);
564 ifa->lst_next = inet6_addr_lst[hash];
565 inet6_addr_lst[hash] = ifa;
567 write_unlock(&addrconf_hash_lock);
569 write_lock(&idev->lock);
570 /* Add to inet6_dev unicast addr list. */
571 ifa->if_next = idev->addr_list;
572 idev->addr_list = ifa;
574 #ifdef CONFIG_IPV6_PRIVACY
575 if (ifa->flags&IFA_F_TEMPORARY) {
576 ifa->tmp_next = idev->tempaddr_list;
577 idev->tempaddr_list = ifa;
585 write_unlock(&idev->lock);
587 read_unlock_bh(&addrconf_lock);
589 if (likely(err == 0))
590 notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
598 write_unlock(&addrconf_hash_lock);
602 /* This function wants to get referenced ifp and releases it before return */
604 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
606 struct inet6_ifaddr *ifa, **ifap;
607 struct inet6_dev *idev = ifp->idev;
609 int deleted = 0, onlink = 0;
610 unsigned long expires = jiffies;
612 hash = ipv6_addr_hash(&ifp->addr);
616 write_lock_bh(&addrconf_hash_lock);
617 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
618 ifap = &ifa->lst_next) {
620 *ifap = ifa->lst_next;
622 ifa->lst_next = NULL;
626 write_unlock_bh(&addrconf_hash_lock);
628 write_lock_bh(&idev->lock);
629 #ifdef CONFIG_IPV6_PRIVACY
630 if (ifp->flags&IFA_F_TEMPORARY) {
631 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
632 ifap = &ifa->tmp_next) {
634 *ifap = ifa->tmp_next;
636 in6_ifa_put(ifp->ifpub);
640 ifa->tmp_next = NULL;
647 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
649 *ifap = ifa->if_next;
652 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
656 } else if (ifp->flags & IFA_F_PERMANENT) {
657 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
659 if (ifa->flags & IFA_F_PERMANENT) {
664 unsigned long lifetime;
669 spin_lock(&ifa->lock);
670 lifetime = min_t(unsigned long,
671 ifa->valid_lft, 0x7fffffffUL/HZ);
672 if (time_before(expires,
673 ifa->tstamp + lifetime * HZ))
674 expires = ifa->tstamp + lifetime * HZ;
675 spin_unlock(&ifa->lock);
679 ifap = &ifa->if_next;
681 write_unlock_bh(&idev->lock);
683 ipv6_ifa_notify(RTM_DELADDR, ifp);
685 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
687 addrconf_del_timer(ifp);
690 * Purge or update corresponding prefix
692 * 1) we don't purge prefix here if address was not permanent.
693 * prefix is managed by its own lifetime.
694 * 2) if there're no addresses, delete prefix.
695 * 3) if there're still other permanent address(es),
696 * corresponding prefix is still permanent.
697 * 4) otherwise, update prefix lifetime to the
698 * longest valid lifetime among the corresponding
699 * addresses on the device.
700 * Note: subsequent RA will update lifetime.
704 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
705 struct in6_addr prefix;
708 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
709 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
711 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
713 ip6_del_rt(rt, NULL, NULL, NULL);
715 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
716 rt->rt6i_expires = expires;
717 rt->rt6i_flags |= RTF_EXPIRES;
720 dst_release(&rt->u.dst);
726 #ifdef CONFIG_IPV6_PRIVACY
727 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
729 struct inet6_dev *idev = ifp->idev;
730 struct in6_addr addr, *tmpaddr;
731 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
736 write_lock(&idev->lock);
738 spin_lock_bh(&ift->lock);
739 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
740 spin_unlock_bh(&ift->lock);
747 if (idev->cnf.use_tempaddr <= 0) {
748 write_unlock(&idev->lock);
750 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
755 spin_lock_bh(&ifp->lock);
756 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
757 idev->cnf.use_tempaddr = -1; /*XXX*/
758 spin_unlock_bh(&ifp->lock);
759 write_unlock(&idev->lock);
761 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
767 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
768 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
769 spin_unlock_bh(&ifp->lock);
770 write_unlock(&idev->lock);
772 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
778 memcpy(&addr.s6_addr[8], idev->rndid, 8);
779 tmp_valid_lft = min_t(__u32,
781 idev->cnf.temp_valid_lft);
782 tmp_prefered_lft = min_t(__u32,
784 idev->cnf.temp_prefered_lft - desync_factor / HZ);
785 tmp_plen = ifp->prefix_len;
786 max_addresses = idev->cnf.max_addresses;
787 tmp_cstamp = ifp->cstamp;
788 tmp_tstamp = ifp->tstamp;
789 spin_unlock_bh(&ifp->lock);
791 write_unlock(&idev->lock);
792 ift = !max_addresses ||
793 ipv6_count_addresses(idev) < max_addresses ?
794 ipv6_add_addr(idev, &addr, tmp_plen,
795 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
796 if (!ift || IS_ERR(ift)) {
800 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
802 write_lock(&idev->lock);
806 spin_lock_bh(&ift->lock);
808 ift->valid_lft = tmp_valid_lft;
809 ift->prefered_lft = tmp_prefered_lft;
810 ift->cstamp = tmp_cstamp;
811 ift->tstamp = tmp_tstamp;
812 spin_unlock_bh(&ift->lock);
814 addrconf_dad_start(ift, 0);
823 * Choose an appropriate source address (RFC3484)
825 struct ipv6_saddr_score {
833 #define IPV6_SADDR_SCORE_LOCAL 0x0001
834 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
835 #define IPV6_SADDR_SCORE_HOA 0x0008
836 #define IPV6_SADDR_SCORE_OIF 0x0010
837 #define IPV6_SADDR_SCORE_LABEL 0x0020
838 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
840 static int inline ipv6_saddr_preferred(int type)
842 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
843 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
848 /* static matching label */
849 static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
852 * prefix (longest match) label
853 * -----------------------------
862 if (type & IPV6_ADDR_LOOPBACK)
864 else if (type & IPV6_ADDR_COMPATv4)
866 else if (type & IPV6_ADDR_MAPPED)
868 else if (addr->s6_addr32[0] == htonl(0x20010000))
870 else if (addr->s6_addr16[0] == htons(0x2002))
872 else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
877 int ipv6_dev_get_saddr(struct net_device *daddr_dev,
878 struct in6_addr *daddr, struct in6_addr *saddr)
880 struct ipv6_saddr_score hiscore;
881 struct inet6_ifaddr *ifa_result = NULL;
882 int daddr_type = __ipv6_addr_type(daddr);
883 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
884 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
885 struct net_device *dev;
887 memset(&hiscore, 0, sizeof(hiscore));
889 read_lock(&dev_base_lock);
890 read_lock(&addrconf_lock);
892 for (dev = dev_base; dev; dev=dev->next) {
893 struct inet6_dev *idev;
894 struct inet6_ifaddr *ifa;
896 /* Rule 0: Candidate Source Address (section 4)
897 * - multicast and link-local destination address,
898 * the set of candidate source address MUST only
899 * include addresses assigned to interfaces
900 * belonging to the same link as the outgoing
902 * (- For site-local destination addresses, the
903 * set of candidate source addresses MUST only
904 * include addresses assigned to interfaces
905 * belonging to the same site as the outgoing
908 if ((daddr_type & IPV6_ADDR_MULTICAST ||
909 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
910 daddr_dev && dev != daddr_dev)
913 idev = __in6_dev_get(dev);
917 read_lock_bh(&idev->lock);
918 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
919 struct ipv6_saddr_score score;
921 score.addr_type = __ipv6_addr_type(&ifa->addr);
924 * - Tentative Address (RFC2462 section 5.4)
925 * - A tentative address is not considered
926 * "assigned to an interface" in the traditional
928 * - Candidate Source Address (section 4)
929 * - In any case, anycast addresses, multicast
930 * addresses, and the unspecified address MUST
931 * NOT be included in a candidate set.
933 if (ifa->flags & IFA_F_TENTATIVE)
935 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
936 score.addr_type & IPV6_ADDR_MULTICAST)) {
937 LIMIT_NETDEBUG(KERN_DEBUG
938 "ADDRCONF: unspecified / multicast address"
939 "assigned as unicast address on %s",
949 if (ifa_result == NULL) {
950 /* record it if the first available entry */
954 /* Rule 1: Prefer same address */
955 if (hiscore.rule < 1) {
956 if (ipv6_addr_equal(&ifa_result->addr, daddr))
957 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
960 if (ipv6_addr_equal(&ifa->addr, daddr)) {
961 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
962 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
967 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
971 /* Rule 2: Prefer appropriate scope */
972 if (hiscore.rule < 2) {
973 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
976 score.scope = __ipv6_addr_src_scope(score.addr_type);
977 if (hiscore.scope < score.scope) {
978 if (hiscore.scope < daddr_scope) {
983 } else if (score.scope < hiscore.scope) {
984 if (score.scope < daddr_scope)
992 /* Rule 3: Avoid deprecated address */
993 if (hiscore.rule < 3) {
994 if (ipv6_saddr_preferred(hiscore.addr_type) ||
995 !(ifa_result->flags & IFA_F_DEPRECATED))
996 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
999 if (ipv6_saddr_preferred(score.addr_type) ||
1000 !(ifa->flags & IFA_F_DEPRECATED)) {
1001 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1002 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1007 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1011 /* Rule 4: Prefer home address -- not implemented yet */
1012 if (hiscore.rule < 4)
1015 /* Rule 5: Prefer outgoing interface */
1016 if (hiscore.rule < 5) {
1017 if (daddr_dev == NULL ||
1018 daddr_dev == ifa_result->idev->dev)
1019 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1022 if (daddr_dev == NULL ||
1023 daddr_dev == ifa->idev->dev) {
1024 score.attrs |= IPV6_SADDR_SCORE_OIF;
1025 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1030 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1034 /* Rule 6: Prefer matching label */
1035 if (hiscore.rule < 6) {
1036 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1037 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1040 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1041 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1042 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1047 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1051 #ifdef CONFIG_IPV6_PRIVACY
1052 /* Rule 7: Prefer public address
1053 * Note: prefer temprary address if use_tempaddr >= 2
1055 if (hiscore.rule < 7) {
1056 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1057 (ifa_result->idev->cnf.use_tempaddr >= 2))
1058 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1061 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1062 (ifa->idev->cnf.use_tempaddr >= 2)) {
1063 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1064 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1069 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1073 if (hiscore.rule < 7)
1076 /* Rule 8: Use longest matching prefix */
1077 if (hiscore.rule < 8) {
1078 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
1081 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1082 if (score.matchlen > hiscore.matchlen) {
1087 else if (score.matchlen < hiscore.matchlen)
1091 /* Final Rule: choose first available one */
1095 in6_ifa_put(ifa_result);
1100 read_unlock_bh(&idev->lock);
1102 read_unlock(&addrconf_lock);
1103 read_unlock(&dev_base_lock);
1106 return -EADDRNOTAVAIL;
1108 ipv6_addr_copy(saddr, &ifa_result->addr);
1109 in6_ifa_put(ifa_result);
1114 int ipv6_get_saddr(struct dst_entry *dst,
1115 struct in6_addr *daddr, struct in6_addr *saddr)
1117 return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
1121 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1123 struct inet6_dev *idev;
1124 int err = -EADDRNOTAVAIL;
1126 read_lock(&addrconf_lock);
1127 if ((idev = __in6_dev_get(dev)) != NULL) {
1128 struct inet6_ifaddr *ifp;
1130 read_lock_bh(&idev->lock);
1131 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1132 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1133 ipv6_addr_copy(addr, &ifp->addr);
1138 read_unlock_bh(&idev->lock);
1140 read_unlock(&addrconf_lock);
1144 static int ipv6_count_addresses(struct inet6_dev *idev)
1147 struct inet6_ifaddr *ifp;
1149 read_lock_bh(&idev->lock);
1150 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1152 read_unlock_bh(&idev->lock);
1156 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1158 struct inet6_ifaddr * ifp;
1159 u8 hash = ipv6_addr_hash(addr);
1161 read_lock_bh(&addrconf_hash_lock);
1162 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1163 if (ipv6_addr_equal(&ifp->addr, addr) &&
1164 !(ifp->flags&IFA_F_TENTATIVE)) {
1165 if (dev == NULL || ifp->idev->dev == dev ||
1166 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1170 read_unlock_bh(&addrconf_hash_lock);
1175 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1177 struct inet6_ifaddr * ifp;
1178 u8 hash = ipv6_addr_hash(addr);
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)
1189 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1191 struct inet6_ifaddr * ifp;
1192 u8 hash = ipv6_addr_hash(addr);
1194 read_lock_bh(&addrconf_hash_lock);
1195 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1196 if (ipv6_addr_equal(&ifp->addr, addr)) {
1197 if (dev == NULL || ifp->idev->dev == dev ||
1198 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1204 read_unlock_bh(&addrconf_hash_lock);
1209 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1211 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1212 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
1213 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1214 u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1215 int sk_ipv6only = ipv6_only_sock(sk);
1216 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1217 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1218 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1220 if (!sk2_rcv_saddr && !sk_ipv6only)
1223 if (addr_type2 == IPV6_ADDR_ANY &&
1224 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1227 if (addr_type == IPV6_ADDR_ANY &&
1228 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1231 if (sk2_rcv_saddr6 &&
1232 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1235 if (addr_type == IPV6_ADDR_MAPPED &&
1237 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1243 /* Gets referenced address, destroys ifaddr */
1245 static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1247 if (ifp->flags&IFA_F_PERMANENT) {
1248 spin_lock_bh(&ifp->lock);
1249 addrconf_del_timer(ifp);
1250 ifp->flags |= IFA_F_TENTATIVE;
1251 spin_unlock_bh(&ifp->lock);
1253 #ifdef CONFIG_IPV6_PRIVACY
1254 } else if (ifp->flags&IFA_F_TEMPORARY) {
1255 struct inet6_ifaddr *ifpub;
1256 spin_lock_bh(&ifp->lock);
1259 in6_ifa_hold(ifpub);
1260 spin_unlock_bh(&ifp->lock);
1261 ipv6_create_tempaddr(ifpub, ifp);
1264 spin_unlock_bh(&ifp->lock);
1272 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1274 if (net_ratelimit())
1275 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1276 addrconf_dad_stop(ifp);
1279 /* Join to solicited addr multicast group. */
1281 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1283 struct in6_addr maddr;
1285 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1288 addrconf_addr_solict_mult(addr, &maddr);
1289 ipv6_dev_mc_inc(dev, &maddr);
1292 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1294 struct in6_addr maddr;
1296 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1299 addrconf_addr_solict_mult(addr, &maddr);
1300 __ipv6_dev_mc_dec(idev, &maddr);
1303 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1305 struct in6_addr addr;
1306 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1307 if (ipv6_addr_any(&addr))
1309 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1312 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1314 struct in6_addr addr;
1315 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1316 if (ipv6_addr_any(&addr))
1318 __ipv6_dev_ac_dec(ifp->idev, &addr);
1321 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1323 switch (dev->type) {
1326 case ARPHRD_IEEE802_TR:
1327 if (dev->addr_len != ETH_ALEN)
1329 memcpy(eui, dev->dev_addr, 3);
1330 memcpy(eui + 5, dev->dev_addr + 3, 3);
1333 * The zSeries OSA network cards can be shared among various
1334 * OS instances, but the OSA cards have only one MAC address.
1335 * This leads to duplicate address conflicts in conjunction
1336 * with IPv6 if more than one instance uses the same card.
1338 * The driver for these cards can deliver a unique 16-bit
1339 * identifier for each instance sharing the same card. It is
1340 * placed instead of 0xFFFE in the interface identifier. The
1341 * "u" bit of the interface identifier is not inverted in this
1342 * case. Hence the resulting interface identifier has local
1343 * scope according to RFC2373.
1346 eui[3] = (dev->dev_id >> 8) & 0xFF;
1347 eui[4] = dev->dev_id & 0xFF;
1355 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1356 if (dev->addr_len != ARCNET_ALEN)
1359 eui[7] = *(u8*)dev->dev_addr;
1361 case ARPHRD_INFINIBAND:
1362 if (dev->addr_len != INFINIBAND_ALEN)
1364 memcpy(eui, dev->dev_addr + 12, 8);
1371 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1374 struct inet6_ifaddr *ifp;
1376 read_lock_bh(&idev->lock);
1377 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1378 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1379 memcpy(eui, ifp->addr.s6_addr+8, 8);
1384 read_unlock_bh(&idev->lock);
1388 #ifdef CONFIG_IPV6_PRIVACY
1389 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1390 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1392 struct net_device *dev;
1393 struct scatterlist sg[2];
1395 sg_set_buf(&sg[0], idev->entropy, 8);
1396 sg_set_buf(&sg[1], idev->work_eui64, 8);
1400 if (ipv6_generate_eui64(idev->work_eui64, dev)) {
1402 "__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
1404 get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
1407 spin_lock(&md5_tfm_lock);
1408 if (unlikely(md5_tfm == NULL)) {
1409 spin_unlock(&md5_tfm_lock);
1412 crypto_digest_init(md5_tfm);
1413 crypto_digest_update(md5_tfm, sg, 2);
1414 crypto_digest_final(md5_tfm, idev->work_digest);
1415 spin_unlock(&md5_tfm_lock);
1417 memcpy(idev->rndid, &idev->work_digest[0], 8);
1418 idev->rndid[0] &= ~0x02;
1419 memcpy(idev->entropy, &idev->work_digest[8], 8);
1422 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1423 * check if generated address is not inappropriate
1425 * - Reserved subnet anycast (RFC 2526)
1426 * 11111101 11....11 1xxxxxxx
1427 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1428 * 00-00-5E-FE-xx-xx-xx-xx
1430 * - XXX: already assigned to an address on the device
1432 if (idev->rndid[0] == 0xfd &&
1433 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1434 (idev->rndid[7]&0x80))
1436 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1437 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1439 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1446 static void ipv6_regen_rndid(unsigned long data)
1448 struct inet6_dev *idev = (struct inet6_dev *) data;
1449 unsigned long expires;
1451 read_lock_bh(&addrconf_lock);
1452 write_lock_bh(&idev->lock);
1457 if (__ipv6_regen_rndid(idev) < 0)
1461 idev->cnf.temp_prefered_lft * HZ -
1462 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1463 if (time_before(expires, jiffies)) {
1465 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1470 if (!mod_timer(&idev->regen_timer, expires))
1474 write_unlock_bh(&idev->lock);
1475 read_unlock_bh(&addrconf_lock);
1479 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1482 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1483 ret = __ipv6_regen_rndid(idev);
1493 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1494 unsigned long expires, u32 flags)
1496 struct in6_rtmsg rtmsg;
1498 memset(&rtmsg, 0, sizeof(rtmsg));
1499 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1500 rtmsg.rtmsg_dst_len = plen;
1501 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1502 rtmsg.rtmsg_ifindex = dev->ifindex;
1503 rtmsg.rtmsg_info = expires;
1504 rtmsg.rtmsg_flags = RTF_UP|flags;
1505 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1507 /* Prevent useless cloning on PtP SIT.
1508 This thing is done here expecting that the whole
1509 class of non-broadcast devices need not cloning.
1511 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1512 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1514 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1517 /* Create "default" multicast route to the interface */
1519 static void addrconf_add_mroute(struct net_device *dev)
1521 struct in6_rtmsg rtmsg;
1523 memset(&rtmsg, 0, sizeof(rtmsg));
1524 ipv6_addr_set(&rtmsg.rtmsg_dst,
1525 htonl(0xFF000000), 0, 0, 0);
1526 rtmsg.rtmsg_dst_len = 8;
1527 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1528 rtmsg.rtmsg_ifindex = dev->ifindex;
1529 rtmsg.rtmsg_flags = RTF_UP;
1530 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1531 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1534 static void sit_route_add(struct net_device *dev)
1536 struct in6_rtmsg rtmsg;
1538 memset(&rtmsg, 0, sizeof(rtmsg));
1540 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1541 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1543 /* prefix length - 96 bits "::d.d.d.d" */
1544 rtmsg.rtmsg_dst_len = 96;
1545 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1546 rtmsg.rtmsg_ifindex = dev->ifindex;
1548 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1551 static void addrconf_add_lroute(struct net_device *dev)
1553 struct in6_addr addr;
1555 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1556 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1559 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1561 struct inet6_dev *idev;
1565 if ((idev = ipv6_find_idev(dev)) == NULL)
1568 /* Add default multicast route */
1569 addrconf_add_mroute(dev);
1571 /* Add link local route */
1572 addrconf_add_lroute(dev);
1576 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1578 struct prefix_info *pinfo;
1582 unsigned long rt_expires;
1583 struct inet6_dev *in6_dev;
1585 pinfo = (struct prefix_info *) opt;
1587 if (len < sizeof(struct prefix_info)) {
1588 ADBG(("addrconf: prefix option too short\n"));
1593 * Validation checks ([ADDRCONF], page 19)
1596 addr_type = ipv6_addr_type(&pinfo->prefix);
1598 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1601 valid_lft = ntohl(pinfo->valid);
1602 prefered_lft = ntohl(pinfo->prefered);
1604 if (prefered_lft > valid_lft) {
1605 if (net_ratelimit())
1606 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1610 in6_dev = in6_dev_get(dev);
1612 if (in6_dev == NULL) {
1613 if (net_ratelimit())
1614 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1619 * Two things going on here:
1620 * 1) Add routes for on-link prefixes
1621 * 2) Configure prefixes with the auto flag set
1624 /* Avoid arithmetic overflow. Really, we could
1625 save rt_expires in seconds, likely valid_lft,
1626 but it would require division in fib gc, that it
1629 if (valid_lft >= 0x7FFFFFFF/HZ)
1630 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1632 rt_expires = valid_lft * HZ;
1635 * We convert this (in jiffies) to clock_t later.
1636 * Avoid arithmetic overflow there as well.
1637 * Overflow can happen only if HZ < USER_HZ.
1639 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1640 rt_expires = 0x7FFFFFFF / USER_HZ;
1642 if (pinfo->onlink) {
1643 struct rt6_info *rt;
1644 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1646 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1647 if (rt->rt6i_flags&RTF_EXPIRES) {
1648 if (valid_lft == 0) {
1649 ip6_del_rt(rt, NULL, NULL, NULL);
1652 rt->rt6i_expires = jiffies + rt_expires;
1655 } else if (valid_lft) {
1656 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1657 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1660 dst_release(&rt->u.dst);
1663 /* Try to figure out our local address for this prefix */
1665 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1666 struct inet6_ifaddr * ifp;
1667 struct in6_addr addr;
1668 int create = 0, update_lft = 0;
1670 if (pinfo->prefix_len == 64) {
1671 memcpy(&addr, &pinfo->prefix, 8);
1672 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1673 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1674 in6_dev_put(in6_dev);
1679 if (net_ratelimit())
1680 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1682 in6_dev_put(in6_dev);
1687 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1689 if (ifp == NULL && valid_lft) {
1690 int max_addresses = in6_dev->cnf.max_addresses;
1692 /* Do not allow to create too much of autoconfigured
1693 * addresses; this would be too easy way to crash kernel.
1695 if (!max_addresses ||
1696 ipv6_count_addresses(in6_dev) < max_addresses)
1697 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1698 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1700 if (!ifp || IS_ERR(ifp)) {
1701 in6_dev_put(in6_dev);
1705 update_lft = create = 1;
1706 ifp->cstamp = jiffies;
1707 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1713 #ifdef CONFIG_IPV6_PRIVACY
1714 struct inet6_ifaddr *ift;
1718 /* update lifetime (RFC2462 5.5.3 e) */
1719 spin_lock(&ifp->lock);
1721 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1722 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1725 if (!update_lft && stored_lft) {
1726 if (valid_lft > MIN_VALID_LIFETIME ||
1727 valid_lft > stored_lft)
1729 else if (stored_lft <= MIN_VALID_LIFETIME) {
1730 /* valid_lft <= stored_lft is always true */
1734 valid_lft = MIN_VALID_LIFETIME;
1735 if (valid_lft < prefered_lft)
1736 prefered_lft = valid_lft;
1742 ifp->valid_lft = valid_lft;
1743 ifp->prefered_lft = prefered_lft;
1746 ifp->flags &= ~IFA_F_DEPRECATED;
1747 spin_unlock(&ifp->lock);
1749 if (!(flags&IFA_F_TENTATIVE))
1750 ipv6_ifa_notify(0, ifp);
1752 spin_unlock(&ifp->lock);
1754 #ifdef CONFIG_IPV6_PRIVACY
1755 read_lock_bh(&in6_dev->lock);
1756 /* update all temporary addresses in the list */
1757 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1759 * When adjusting the lifetimes of an existing
1760 * temporary address, only lower the lifetimes.
1761 * Implementations must not increase the
1762 * lifetimes of an existing temporary address
1763 * when processing a Prefix Information Option.
1765 spin_lock(&ift->lock);
1767 if (ift->valid_lft > valid_lft &&
1768 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1769 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1770 if (ift->prefered_lft > prefered_lft &&
1771 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1772 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1773 spin_unlock(&ift->lock);
1774 if (!(flags&IFA_F_TENTATIVE))
1775 ipv6_ifa_notify(0, ift);
1778 if (create && in6_dev->cnf.use_tempaddr > 0) {
1780 * When a new public address is created as described in [ADDRCONF],
1781 * also create a new temporary address.
1783 read_unlock_bh(&in6_dev->lock);
1784 ipv6_create_tempaddr(ifp, NULL);
1786 read_unlock_bh(&in6_dev->lock);
1793 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1794 in6_dev_put(in6_dev);
1798 * Set destination address.
1799 * Special case for SIT interfaces where we create a new "virtual"
1802 int addrconf_set_dstaddr(void __user *arg)
1804 struct in6_ifreq ireq;
1805 struct net_device *dev;
1811 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1814 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1820 if (dev->type == ARPHRD_SIT) {
1823 struct ip_tunnel_parm p;
1825 err = -EADDRNOTAVAIL;
1826 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1829 memset(&p, 0, sizeof(p));
1830 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1834 p.iph.protocol = IPPROTO_IPV6;
1836 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1838 oldfs = get_fs(); set_fs(KERNEL_DS);
1839 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1844 if ((dev = __dev_get_by_name(p.name)) == NULL)
1846 err = dev_open(dev);
1856 * Manual configuration of address on an interface
1858 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1860 struct inet6_ifaddr *ifp;
1861 struct inet6_dev *idev;
1862 struct net_device *dev;
1867 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1870 if (!(dev->flags&IFF_UP))
1873 if ((idev = addrconf_add_dev(dev)) == NULL)
1876 scope = ipv6_addr_scope(pfx);
1878 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1880 addrconf_dad_start(ifp, 0);
1885 return PTR_ERR(ifp);
1888 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1890 struct inet6_ifaddr *ifp;
1891 struct inet6_dev *idev;
1892 struct net_device *dev;
1894 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1897 if ((idev = __in6_dev_get(dev)) == NULL)
1900 read_lock_bh(&idev->lock);
1901 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1902 if (ifp->prefix_len == plen &&
1903 ipv6_addr_equal(pfx, &ifp->addr)) {
1905 read_unlock_bh(&idev->lock);
1909 /* If the last address is deleted administratively,
1910 disable IPv6 on this interface.
1912 if (idev->addr_list == NULL)
1913 addrconf_ifdown(idev->dev, 1);
1917 read_unlock_bh(&idev->lock);
1918 return -EADDRNOTAVAIL;
1922 int addrconf_add_ifaddr(void __user *arg)
1924 struct in6_ifreq ireq;
1927 if (!capable(CAP_NET_ADMIN))
1930 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1934 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1939 int addrconf_del_ifaddr(void __user *arg)
1941 struct in6_ifreq ireq;
1944 if (!capable(CAP_NET_ADMIN))
1947 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1951 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1956 static void sit_add_v4_addrs(struct inet6_dev *idev)
1958 struct inet6_ifaddr * ifp;
1959 struct in6_addr addr;
1960 struct net_device *dev;
1965 memset(&addr, 0, sizeof(struct in6_addr));
1966 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1968 if (idev->dev->flags&IFF_POINTOPOINT) {
1969 addr.s6_addr32[0] = htonl(0xfe800000);
1972 scope = IPV6_ADDR_COMPATv4;
1975 if (addr.s6_addr32[3]) {
1976 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1978 spin_lock_bh(&ifp->lock);
1979 ifp->flags &= ~IFA_F_TENTATIVE;
1980 spin_unlock_bh(&ifp->lock);
1981 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1987 for (dev = dev_base; dev != NULL; dev = dev->next) {
1988 struct in_device * in_dev = __in_dev_get_rtnl(dev);
1989 if (in_dev && (dev->flags & IFF_UP)) {
1990 struct in_ifaddr * ifa;
1994 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1997 addr.s6_addr32[3] = ifa->ifa_local;
1999 if (ifa->ifa_scope == RT_SCOPE_LINK)
2001 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2002 if (idev->dev->flags&IFF_POINTOPOINT)
2006 if (idev->dev->flags&IFF_POINTOPOINT)
2011 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2014 spin_lock_bh(&ifp->lock);
2015 ifp->flags &= ~IFA_F_TENTATIVE;
2016 spin_unlock_bh(&ifp->lock);
2017 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2025 static void init_loopback(struct net_device *dev)
2027 struct inet6_dev *idev;
2028 struct inet6_ifaddr * ifp;
2034 if ((idev = ipv6_find_idev(dev)) == NULL) {
2035 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2039 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2041 spin_lock_bh(&ifp->lock);
2042 ifp->flags &= ~IFA_F_TENTATIVE;
2043 spin_unlock_bh(&ifp->lock);
2044 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2049 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2051 struct inet6_ifaddr * ifp;
2053 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2055 addrconf_dad_start(ifp, 0);
2060 static void addrconf_dev_config(struct net_device *dev)
2062 struct in6_addr addr;
2063 struct inet6_dev * idev;
2067 if ((dev->type != ARPHRD_ETHER) &&
2068 (dev->type != ARPHRD_FDDI) &&
2069 (dev->type != ARPHRD_IEEE802_TR) &&
2070 (dev->type != ARPHRD_ARCNET) &&
2071 (dev->type != ARPHRD_INFINIBAND)) {
2072 /* Alas, we support only Ethernet autoconfiguration. */
2076 idev = addrconf_add_dev(dev);
2080 memset(&addr, 0, sizeof(struct in6_addr));
2081 addr.s6_addr32[0] = htonl(0xFE800000);
2083 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2084 addrconf_add_linklocal(idev, &addr);
2087 static void addrconf_sit_config(struct net_device *dev)
2089 struct inet6_dev *idev;
2094 * Configure the tunnel with one of our IPv4
2095 * addresses... we should configure all of
2096 * our v4 addrs in the tunnel
2099 if ((idev = ipv6_find_idev(dev)) == NULL) {
2100 printk(KERN_DEBUG "init sit: add_dev failed\n");
2104 sit_add_v4_addrs(idev);
2106 if (dev->flags&IFF_POINTOPOINT) {
2107 addrconf_add_mroute(dev);
2108 addrconf_add_lroute(dev);
2114 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2116 struct in6_addr lladdr;
2118 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2119 addrconf_add_linklocal(idev, &lladdr);
2125 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2127 struct net_device *link_dev;
2129 /* first try to inherit the link-local address from the link device */
2130 if (idev->dev->iflink &&
2131 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2132 if (!ipv6_inherit_linklocal(idev, link_dev))
2135 /* then try to inherit it from any device */
2136 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2137 if (!ipv6_inherit_linklocal(idev, link_dev))
2140 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2144 * Autoconfigure tunnel with a link-local address so routing protocols,
2145 * DHCPv6, MLD etc. can be run over the virtual link
2148 static void addrconf_ip6_tnl_config(struct net_device *dev)
2150 struct inet6_dev *idev;
2154 if ((idev = addrconf_add_dev(dev)) == NULL) {
2155 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2158 ip6_tnl_add_linklocal(idev);
2159 addrconf_add_mroute(dev);
2162 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2165 struct net_device *dev = (struct net_device *) data;
2166 struct inet6_dev *idev = __in6_dev_get(dev);
2167 int run_pending = 0;
2172 if (event == NETDEV_UP) {
2173 if (!netif_carrier_ok(dev)) {
2174 /* device is not ready yet. */
2176 "ADDRCONF(NETDEV_UP): %s: "
2177 "link is not ready\n",
2183 idev->if_flags |= IF_READY;
2185 if (!netif_carrier_ok(dev)) {
2186 /* device is still not ready. */
2191 if (idev->if_flags & IF_READY) {
2192 /* device is already configured. */
2195 idev->if_flags |= IF_READY;
2199 "ADDRCONF(NETDEV_CHANGE): %s: "
2200 "link becomes ready\n",
2208 addrconf_sit_config(dev);
2210 case ARPHRD_TUNNEL6:
2211 addrconf_ip6_tnl_config(dev);
2213 case ARPHRD_LOOPBACK:
2218 addrconf_dev_config(dev);
2223 addrconf_dad_run(idev);
2225 /* If the MTU changed during the interface down, when the
2226 interface up, the changed MTU must be reflected in the
2227 idev as well as routers.
2229 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2230 rt6_mtu_change(dev, dev->mtu);
2231 idev->cnf.mtu6 = dev->mtu;
2233 idev->tstamp = jiffies;
2234 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2235 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2236 stop IPv6 on this interface.
2238 if (dev->mtu < IPV6_MIN_MTU)
2239 addrconf_ifdown(dev, event != NETDEV_DOWN);
2243 case NETDEV_CHANGEMTU:
2244 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2245 rt6_mtu_change(dev, dev->mtu);
2246 idev->cnf.mtu6 = dev->mtu;
2250 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2253 case NETDEV_UNREGISTER:
2255 * Remove all addresses from this interface.
2257 addrconf_ifdown(dev, event != NETDEV_DOWN);
2260 case NETDEV_CHANGENAME:
2261 #ifdef CONFIG_SYSCTL
2263 addrconf_sysctl_unregister(&idev->cnf);
2264 neigh_sysctl_unregister(idev->nd_parms);
2265 neigh_sysctl_register(dev, idev->nd_parms,
2266 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2267 &ndisc_ifinfo_sysctl_change,
2269 addrconf_sysctl_register(idev, &idev->cnf);
2279 * addrconf module should be notified of a device going up
2281 static struct notifier_block ipv6_dev_notf = {
2282 .notifier_call = addrconf_notify,
2286 static int addrconf_ifdown(struct net_device *dev, int how)
2288 struct inet6_dev *idev;
2289 struct inet6_ifaddr *ifa, **bifa;
2294 if (dev == &loopback_dev && how == 1)
2298 neigh_ifdown(&nd_tbl, dev);
2300 idev = __in6_dev_get(dev);
2304 /* Step 1: remove reference to ipv6 device from parent device.
2308 write_lock_bh(&addrconf_lock);
2309 dev->ip6_ptr = NULL;
2311 write_unlock_bh(&addrconf_lock);
2313 /* Step 1.5: remove snmp6 entry */
2314 snmp6_unregister_dev(idev);
2318 /* Step 2: clear hash table */
2319 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2320 bifa = &inet6_addr_lst[i];
2322 write_lock_bh(&addrconf_hash_lock);
2323 while ((ifa = *bifa) != NULL) {
2324 if (ifa->idev == idev) {
2325 *bifa = ifa->lst_next;
2326 ifa->lst_next = NULL;
2327 addrconf_del_timer(ifa);
2331 bifa = &ifa->lst_next;
2333 write_unlock_bh(&addrconf_hash_lock);
2336 write_lock_bh(&idev->lock);
2338 /* Step 3: clear flags for stateless addrconf */
2340 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2342 /* Step 4: clear address list */
2343 #ifdef CONFIG_IPV6_PRIVACY
2344 if (how == 1 && del_timer(&idev->regen_timer))
2347 /* clear tempaddr list */
2348 while ((ifa = idev->tempaddr_list) != NULL) {
2349 idev->tempaddr_list = ifa->tmp_next;
2350 ifa->tmp_next = NULL;
2352 write_unlock_bh(&idev->lock);
2353 spin_lock_bh(&ifa->lock);
2356 in6_ifa_put(ifa->ifpub);
2359 spin_unlock_bh(&ifa->lock);
2361 write_lock_bh(&idev->lock);
2364 while ((ifa = idev->addr_list) != NULL) {
2365 idev->addr_list = ifa->if_next;
2366 ifa->if_next = NULL;
2368 addrconf_del_timer(ifa);
2369 write_unlock_bh(&idev->lock);
2371 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2374 write_lock_bh(&idev->lock);
2376 write_unlock_bh(&idev->lock);
2378 /* Step 5: Discard multicast list */
2381 ipv6_mc_destroy_dev(idev);
2385 /* Step 5: netlink notification of this interface */
2386 idev->tstamp = jiffies;
2387 inet6_ifinfo_notify(RTM_DELLINK, idev);
2389 /* Shot the device (if unregistered) */
2392 #ifdef CONFIG_SYSCTL
2393 addrconf_sysctl_unregister(&idev->cnf);
2394 neigh_sysctl_unregister(idev->nd_parms);
2396 neigh_parms_release(&nd_tbl, idev->nd_parms);
2397 neigh_ifdown(&nd_tbl, dev);
2403 static void addrconf_rs_timer(unsigned long data)
2405 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2407 if (ifp->idev->cnf.forwarding)
2410 if (ifp->idev->if_flags & IF_RA_RCVD) {
2412 * Announcement received after solicitation
2418 spin_lock(&ifp->lock);
2419 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2420 struct in6_addr all_routers;
2422 /* The wait after the last probe can be shorter */
2423 addrconf_mod_timer(ifp, AC_RS,
2424 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2425 ifp->idev->cnf.rtr_solicit_delay :
2426 ifp->idev->cnf.rtr_solicit_interval);
2427 spin_unlock(&ifp->lock);
2429 ipv6_addr_all_routers(&all_routers);
2431 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2433 spin_unlock(&ifp->lock);
2435 * Note: we do not support deprecated "all on-link"
2436 * assumption any longer.
2438 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2439 ifp->idev->dev->name);
2447 * Duplicate Address Detection
2449 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2451 unsigned long rand_num;
2452 struct inet6_dev *idev = ifp->idev;
2454 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2455 ifp->probes = idev->cnf.dad_transmits;
2456 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2459 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2461 struct inet6_dev *idev = ifp->idev;
2462 struct net_device *dev = idev->dev;
2464 addrconf_join_solict(dev, &ifp->addr);
2466 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2467 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2470 net_srandom(ifp->addr.s6_addr32[3]);
2472 read_lock_bh(&idev->lock);
2475 spin_lock_bh(&ifp->lock);
2477 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2478 !(ifp->flags&IFA_F_TENTATIVE)) {
2479 ifp->flags &= ~IFA_F_TENTATIVE;
2480 spin_unlock_bh(&ifp->lock);
2481 read_unlock_bh(&idev->lock);
2483 addrconf_dad_completed(ifp);
2487 if (!(idev->if_flags & IF_READY)) {
2488 spin_unlock_bh(&ifp->lock);
2489 read_unlock_bh(&idev->lock);
2491 * If the defice is not ready:
2492 * - keep it tentative if it is a permanent address.
2493 * - otherwise, kill it.
2496 addrconf_dad_stop(ifp);
2499 addrconf_dad_kick(ifp);
2500 spin_unlock_bh(&ifp->lock);
2502 read_unlock_bh(&idev->lock);
2505 static void addrconf_dad_timer(unsigned long data)
2507 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2508 struct inet6_dev *idev = ifp->idev;
2509 struct in6_addr unspec;
2510 struct in6_addr mcaddr;
2512 read_lock_bh(&idev->lock);
2514 read_unlock_bh(&idev->lock);
2517 spin_lock_bh(&ifp->lock);
2518 if (ifp->probes == 0) {
2520 * DAD was successful
2523 ifp->flags &= ~IFA_F_TENTATIVE;
2524 spin_unlock_bh(&ifp->lock);
2525 read_unlock_bh(&idev->lock);
2527 addrconf_dad_completed(ifp);
2533 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2534 spin_unlock_bh(&ifp->lock);
2535 read_unlock_bh(&idev->lock);
2537 /* send a neighbour solicitation for our addr */
2538 memset(&unspec, 0, sizeof(unspec));
2539 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2540 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2545 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2547 struct net_device * dev = ifp->idev->dev;
2550 * Configure the address for reception. Now it is valid.
2553 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2555 /* If added prefix is link local and forwarding is off,
2556 start sending router solicitations.
2559 if (ifp->idev->cnf.forwarding == 0 &&
2560 ifp->idev->cnf.rtr_solicits > 0 &&
2561 (dev->flags&IFF_LOOPBACK) == 0 &&
2562 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2563 struct in6_addr all_routers;
2565 ipv6_addr_all_routers(&all_routers);
2568 * If a host as already performed a random delay
2569 * [...] as part of DAD [...] there is no need
2570 * to delay again before sending the first RS
2572 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2574 spin_lock_bh(&ifp->lock);
2576 ifp->idev->if_flags |= IF_RS_SENT;
2577 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2578 spin_unlock_bh(&ifp->lock);
2582 static void addrconf_dad_run(struct inet6_dev *idev) {
2583 struct inet6_ifaddr *ifp;
2585 read_lock_bh(&idev->lock);
2586 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2587 spin_lock_bh(&ifp->lock);
2588 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2589 spin_unlock_bh(&ifp->lock);
2592 spin_unlock_bh(&ifp->lock);
2593 addrconf_dad_kick(ifp);
2595 read_unlock_bh(&idev->lock);
2598 #ifdef CONFIG_PROC_FS
2599 struct if6_iter_state {
2603 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2605 struct inet6_ifaddr *ifa = NULL;
2606 struct if6_iter_state *state = seq->private;
2608 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2609 ifa = inet6_addr_lst[state->bucket];
2616 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2618 struct if6_iter_state *state = seq->private;
2620 ifa = ifa->lst_next;
2622 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2623 ifa = inet6_addr_lst[state->bucket];
2629 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2631 struct inet6_ifaddr *ifa = if6_get_first(seq);
2634 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2636 return pos ? NULL : ifa;
2639 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2641 read_lock_bh(&addrconf_hash_lock);
2642 return if6_get_idx(seq, *pos);
2645 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2647 struct inet6_ifaddr *ifa;
2649 ifa = if6_get_next(seq, v);
2654 static void if6_seq_stop(struct seq_file *seq, void *v)
2656 read_unlock_bh(&addrconf_hash_lock);
2659 static int if6_seq_show(struct seq_file *seq, void *v)
2661 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2663 /* no ipv6 inside a vserver for now */
2664 if (vx_check(0, VX_ADMIN|VX_WATCH))
2666 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
2668 ifp->idev->dev->ifindex,
2672 ifp->idev->dev->name);
2676 static struct seq_operations if6_seq_ops = {
2677 .start = if6_seq_start,
2678 .next = if6_seq_next,
2679 .show = if6_seq_show,
2680 .stop = if6_seq_stop,
2683 static int if6_seq_open(struct inode *inode, struct file *file)
2685 struct seq_file *seq;
2687 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2691 memset(s, 0, sizeof(*s));
2693 rc = seq_open(file, &if6_seq_ops);
2697 seq = file->private_data;
2706 static struct file_operations if6_fops = {
2707 .owner = THIS_MODULE,
2708 .open = if6_seq_open,
2710 .llseek = seq_lseek,
2711 .release = seq_release_private,
2714 int __init if6_proc_init(void)
2716 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2721 void if6_proc_exit(void)
2723 proc_net_remove("if_inet6");
2725 #endif /* CONFIG_PROC_FS */
2728 * Periodic address status verification
2731 static void addrconf_verify(unsigned long foo)
2733 struct inet6_ifaddr *ifp;
2734 unsigned long now, next;
2737 spin_lock_bh(&addrconf_verify_lock);
2739 next = now + ADDR_CHECK_FREQUENCY;
2741 del_timer(&addr_chk_timer);
2743 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2746 read_lock(&addrconf_hash_lock);
2747 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2749 #ifdef CONFIG_IPV6_PRIVACY
2750 unsigned long regen_advance;
2753 if (ifp->flags & IFA_F_PERMANENT)
2756 spin_lock(&ifp->lock);
2757 age = (now - ifp->tstamp) / HZ;
2759 #ifdef CONFIG_IPV6_PRIVACY
2760 regen_advance = ifp->idev->cnf.regen_max_retry *
2761 ifp->idev->cnf.dad_transmits *
2762 ifp->idev->nd_parms->retrans_time / HZ;
2765 if (age >= ifp->valid_lft) {
2766 spin_unlock(&ifp->lock);
2768 read_unlock(&addrconf_hash_lock);
2771 } else if (age >= ifp->prefered_lft) {
2772 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2775 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2777 ifp->flags |= IFA_F_DEPRECATED;
2780 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2781 next = ifp->tstamp + ifp->valid_lft * HZ;
2783 spin_unlock(&ifp->lock);
2787 read_unlock(&addrconf_hash_lock);
2789 ipv6_ifa_notify(0, ifp);
2793 #ifdef CONFIG_IPV6_PRIVACY
2794 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2795 !(ifp->flags&IFA_F_TENTATIVE)) {
2796 if (age >= ifp->prefered_lft - regen_advance) {
2797 struct inet6_ifaddr *ifpub = ifp->ifpub;
2798 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2799 next = ifp->tstamp + ifp->prefered_lft * HZ;
2800 if (!ifp->regen_count && ifpub) {
2803 in6_ifa_hold(ifpub);
2804 spin_unlock(&ifp->lock);
2805 read_unlock(&addrconf_hash_lock);
2806 spin_lock(&ifpub->lock);
2807 ifpub->regen_count = 0;
2808 spin_unlock(&ifpub->lock);
2809 ipv6_create_tempaddr(ifpub, ifp);
2814 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2815 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2816 spin_unlock(&ifp->lock);
2819 /* ifp->prefered_lft <= ifp->valid_lft */
2820 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2821 next = ifp->tstamp + ifp->prefered_lft * HZ;
2822 spin_unlock(&ifp->lock);
2825 read_unlock(&addrconf_hash_lock);
2828 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2829 add_timer(&addr_chk_timer);
2830 spin_unlock_bh(&addrconf_verify_lock);
2834 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2836 struct rtattr **rta = arg;
2837 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2838 struct in6_addr *pfx;
2841 if (rta[IFA_ADDRESS-1]) {
2842 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2844 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2846 if (rta[IFA_LOCAL-1]) {
2847 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2849 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2854 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2858 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2860 struct rtattr **rta = arg;
2861 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2862 struct in6_addr *pfx;
2865 if (rta[IFA_ADDRESS-1]) {
2866 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2868 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2870 if (rta[IFA_LOCAL-1]) {
2871 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2873 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2878 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2881 /* Maximum length of ifa_cacheinfo attributes */
2882 #define INET6_IFADDR_RTA_SPACE \
2883 RTA_SPACE(16) /* IFA_ADDRESS */ + \
2884 RTA_SPACE(sizeof(struct ifa_cacheinfo)) /* CACHEINFO */
2886 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2887 u32 pid, u32 seq, int event, unsigned int flags)
2889 struct ifaddrmsg *ifm;
2890 struct nlmsghdr *nlh;
2891 struct ifa_cacheinfo ci;
2892 unsigned char *b = skb->tail;
2894 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2895 ifm = NLMSG_DATA(nlh);
2896 ifm->ifa_family = AF_INET6;
2897 ifm->ifa_prefixlen = ifa->prefix_len;
2898 ifm->ifa_flags = ifa->flags;
2899 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2900 if (ifa->scope&IFA_HOST)
2901 ifm->ifa_scope = RT_SCOPE_HOST;
2902 else if (ifa->scope&IFA_LINK)
2903 ifm->ifa_scope = RT_SCOPE_LINK;
2904 else if (ifa->scope&IFA_SITE)
2905 ifm->ifa_scope = RT_SCOPE_SITE;
2906 ifm->ifa_index = ifa->idev->dev->ifindex;
2907 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2908 if (!(ifa->flags&IFA_F_PERMANENT)) {
2909 ci.ifa_prefered = ifa->prefered_lft;
2910 ci.ifa_valid = ifa->valid_lft;
2911 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2912 long tval = (jiffies - ifa->tstamp)/HZ;
2913 ci.ifa_prefered -= tval;
2914 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2915 ci.ifa_valid -= tval;
2918 ci.ifa_prefered = INFINITY_LIFE_TIME;
2919 ci.ifa_valid = INFINITY_LIFE_TIME;
2921 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2922 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2923 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2924 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2925 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2926 nlh->nlmsg_len = skb->tail - b;
2931 skb_trim(skb, b - skb->data);
2935 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2936 u32 pid, u32 seq, int event, u16 flags)
2938 struct ifaddrmsg *ifm;
2939 struct nlmsghdr *nlh;
2940 struct ifa_cacheinfo ci;
2941 unsigned char *b = skb->tail;
2943 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2944 ifm = NLMSG_DATA(nlh);
2945 ifm->ifa_family = AF_INET6;
2946 ifm->ifa_prefixlen = 128;
2947 ifm->ifa_flags = IFA_F_PERMANENT;
2948 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2949 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2950 ifm->ifa_scope = RT_SCOPE_SITE;
2951 ifm->ifa_index = ifmca->idev->dev->ifindex;
2952 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2953 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2954 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2956 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2957 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2959 ci.ifa_prefered = INFINITY_LIFE_TIME;
2960 ci.ifa_valid = INFINITY_LIFE_TIME;
2961 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2962 nlh->nlmsg_len = skb->tail - b;
2967 skb_trim(skb, b - skb->data);
2971 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2972 u32 pid, u32 seq, int event, unsigned int flags)
2974 struct ifaddrmsg *ifm;
2975 struct nlmsghdr *nlh;
2976 struct ifa_cacheinfo ci;
2977 unsigned char *b = skb->tail;
2979 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2980 ifm = NLMSG_DATA(nlh);
2981 ifm->ifa_family = AF_INET6;
2982 ifm->ifa_prefixlen = 128;
2983 ifm->ifa_flags = IFA_F_PERMANENT;
2984 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2985 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2986 ifm->ifa_scope = RT_SCOPE_SITE;
2987 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2988 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2989 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2990 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2992 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2993 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2995 ci.ifa_prefered = INFINITY_LIFE_TIME;
2996 ci.ifa_valid = INFINITY_LIFE_TIME;
2997 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2998 nlh->nlmsg_len = skb->tail - b;
3003 skb_trim(skb, b - skb->data);
3014 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3015 enum addr_type_t type)
3018 int s_idx, s_ip_idx;
3020 struct net_device *dev;
3021 struct inet6_dev *idev = NULL;
3022 struct inet6_ifaddr *ifa;
3023 struct ifmcaddr6 *ifmca;
3024 struct ifacaddr6 *ifaca;
3026 /* no ipv6 inside a vserver for now */
3027 if (skb->sk && skb->sk->sk_vx_info)
3030 s_idx = cb->args[0];
3031 s_ip_idx = ip_idx = cb->args[1];
3032 read_lock(&dev_base_lock);
3034 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
3040 if ((idev = in6_dev_get(dev)) == NULL)
3042 read_lock_bh(&idev->lock);
3045 /* unicast address incl. temp addr */
3046 for (ifa = idev->addr_list; ifa;
3047 ifa = ifa->if_next, ip_idx++) {
3048 if (ip_idx < s_ip_idx)
3050 if ((err = inet6_fill_ifaddr(skb, ifa,
3051 NETLINK_CB(cb->skb).pid,
3052 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3057 case MULTICAST_ADDR:
3058 /* multicast address */
3059 for (ifmca = idev->mc_list; ifmca;
3060 ifmca = ifmca->next, ip_idx++) {
3061 if (ip_idx < s_ip_idx)
3063 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3064 NETLINK_CB(cb->skb).pid,
3065 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3071 /* anycast address */
3072 for (ifaca = idev->ac_list; ifaca;
3073 ifaca = ifaca->aca_next, ip_idx++) {
3074 if (ip_idx < s_ip_idx)
3076 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3077 NETLINK_CB(cb->skb).pid,
3078 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3086 read_unlock_bh(&idev->lock);
3091 read_unlock_bh(&idev->lock);
3094 read_unlock(&dev_base_lock);
3096 cb->args[1] = ip_idx;
3100 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3102 enum addr_type_t type = UNICAST_ADDR;
3103 return inet6_dump_addr(skb, cb, type);
3106 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3108 enum addr_type_t type = MULTICAST_ADDR;
3109 return inet6_dump_addr(skb, cb, type);
3113 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3115 enum addr_type_t type = ANYCAST_ADDR;
3116 return inet6_dump_addr(skb, cb, type);
3119 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3121 struct sk_buff *skb;
3122 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg) + INET6_IFADDR_RTA_SPACE);
3124 skb = alloc_skb(size, GFP_ATOMIC);
3126 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
3129 if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
3131 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
3134 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
3135 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
3138 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3139 __s32 *array, int bytes)
3141 memset(array, 0, bytes);
3142 array[DEVCONF_FORWARDING] = cnf->forwarding;
3143 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3144 array[DEVCONF_MTU6] = cnf->mtu6;
3145 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3146 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3147 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3148 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3149 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3150 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3151 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3152 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3153 #ifdef CONFIG_IPV6_PRIVACY
3154 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3155 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3156 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3157 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3158 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3160 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3163 /* Maximum length of ifinfomsg attributes */
3164 #define INET6_IFINFO_RTA_SPACE \
3165 RTA_SPACE(IFNAMSIZ) /* IFNAME */ + \
3166 RTA_SPACE(MAX_ADDR_LEN) /* ADDRESS */ + \
3167 RTA_SPACE(sizeof(u32)) /* MTU */ + \
3168 RTA_SPACE(sizeof(int)) /* LINK */ + \
3169 RTA_SPACE(0) /* PROTINFO */ + \
3170 RTA_SPACE(sizeof(u32)) /* FLAGS */ + \
3171 RTA_SPACE(sizeof(struct ifla_cacheinfo)) /* CACHEINFO */ + \
3172 RTA_SPACE(sizeof(__s32[DEVCONF_MAX])) /* CONF */
3174 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3175 u32 pid, u32 seq, int event, unsigned int flags)
3177 struct net_device *dev = idev->dev;
3178 __s32 *array = NULL;
3179 struct ifinfomsg *r;
3180 struct nlmsghdr *nlh;
3181 unsigned char *b = skb->tail;
3182 struct rtattr *subattr;
3183 __u32 mtu = dev->mtu;
3184 struct ifla_cacheinfo ci;
3186 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
3187 r = NLMSG_DATA(nlh);
3188 r->ifi_family = AF_INET6;
3190 r->ifi_type = dev->type;
3191 r->ifi_index = dev->ifindex;
3192 r->ifi_flags = dev_get_flags(dev);
3195 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
3198 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3200 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
3201 if (dev->ifindex != dev->iflink)
3202 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
3204 subattr = (struct rtattr*)skb->tail;
3206 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
3208 /* return the device flags */
3209 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
3211 /* return interface cacheinfo */
3212 ci.max_reasm_len = IPV6_MAXPLEN;
3213 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3214 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3215 ci.reachable_time = idev->nd_parms->reachable_time;
3216 ci.retrans_time = idev->nd_parms->retrans_time;
3217 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3219 /* return the device sysctl params */
3220 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
3221 goto rtattr_failure;
3222 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
3223 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
3225 /* XXX - Statistics/MC not implemented */
3226 subattr->rta_len = skb->tail - (u8*)subattr;
3228 nlh->nlmsg_len = skb->tail - b;
3235 skb_trim(skb, b - skb->data);
3239 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3242 int s_idx = cb->args[0];
3243 struct net_device *dev;
3244 struct inet6_dev *idev;
3246 /* no ipv6 inside a vserver for now */
3247 if (skb->sk && skb->sk->sk_vx_info)
3250 read_lock(&dev_base_lock);
3251 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3254 if ((idev = in6_dev_get(dev)) == NULL)
3256 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3257 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3262 read_unlock(&dev_base_lock);
3268 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3270 struct sk_buff *skb;
3271 int size = NLMSG_SPACE(sizeof(struct ifinfomsg) + INET6_IFINFO_RTA_SPACE);
3273 skb = alloc_skb(size, GFP_ATOMIC);
3275 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
3278 if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
3280 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
3283 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
3284 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
3287 /* Maximum length of prefix_cacheinfo attributes */
3288 #define INET6_PREFIX_RTA_SPACE \
3289 RTA_SPACE(sizeof(((struct prefix_info *)NULL)->prefix)) /* ADDRESS */ + \
3290 RTA_SPACE(sizeof(struct prefix_cacheinfo)) /* CACHEINFO */
3292 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3293 struct prefix_info *pinfo, u32 pid, u32 seq,
3294 int event, unsigned int flags)
3296 struct prefixmsg *pmsg;
3297 struct nlmsghdr *nlh;
3298 unsigned char *b = skb->tail;
3299 struct prefix_cacheinfo ci;
3301 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
3302 pmsg = NLMSG_DATA(nlh);
3303 pmsg->prefix_family = AF_INET6;
3304 pmsg->prefix_pad1 = 0;
3305 pmsg->prefix_pad2 = 0;
3306 pmsg->prefix_ifindex = idev->dev->ifindex;
3307 pmsg->prefix_len = pinfo->prefix_len;
3308 pmsg->prefix_type = pinfo->type;
3309 pmsg->prefix_pad3 = 0;
3311 pmsg->prefix_flags = 0;
3313 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3314 if (pinfo->autoconf)
3315 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3317 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3319 ci.preferred_time = ntohl(pinfo->prefered);
3320 ci.valid_time = ntohl(pinfo->valid);
3321 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3323 nlh->nlmsg_len = skb->tail - b;
3328 skb_trim(skb, b - skb->data);
3332 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3333 struct prefix_info *pinfo)
3335 struct sk_buff *skb;
3336 int size = NLMSG_SPACE(sizeof(struct prefixmsg) + INET6_PREFIX_RTA_SPACE);
3338 skb = alloc_skb(size, GFP_ATOMIC);
3340 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
3343 if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
3345 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
3348 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
3349 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
3352 static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
3353 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3354 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3355 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
3356 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
3357 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3358 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3359 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3360 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3361 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3362 .dumpit = inet6_dump_fib, },
3365 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3367 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3371 ip6_ins_rt(ifp->rt, NULL, NULL, NULL);
3372 if (ifp->idev->cnf.forwarding)
3373 addrconf_join_anycast(ifp);
3376 if (ifp->idev->cnf.forwarding)
3377 addrconf_leave_anycast(ifp);
3378 addrconf_leave_solict(ifp->idev, &ifp->addr);
3379 dst_hold(&ifp->rt->u.dst);
3380 if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
3381 dst_free(&ifp->rt->u.dst);
3386 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3388 read_lock_bh(&addrconf_lock);
3389 if (likely(ifp->idev->dead == 0))
3390 __ipv6_ifa_notify(event, ifp);
3391 read_unlock_bh(&addrconf_lock);
3394 #ifdef CONFIG_SYSCTL
3397 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3398 void __user *buffer, size_t *lenp, loff_t *ppos)
3400 int *valp = ctl->data;
3404 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3406 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3407 if (valp != &ipv6_devconf.forwarding) {
3408 if ((!*valp) ^ (!val)) {
3409 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3412 dev_forward_change(idev);
3415 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3416 addrconf_forward_change();
3419 rt6_purge_dflt_routers();
3425 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3426 int __user *name, int nlen,
3427 void __user *oldval,
3428 size_t __user *oldlenp,
3429 void __user *newval, size_t newlen,
3432 int *valp = table->data;
3435 if (!newval || !newlen)
3437 if (newlen != sizeof(int))
3439 if (get_user(new, (int __user *)newval))
3443 if (oldval && oldlenp) {
3445 if (get_user(len, oldlenp))
3448 if (len > table->maxlen)
3449 len = table->maxlen;
3450 if (copy_to_user(oldval, valp, len))
3452 if (put_user(len, oldlenp))
3457 if (valp != &ipv6_devconf_dflt.forwarding) {
3458 if (valp != &ipv6_devconf.forwarding) {
3459 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3461 if (unlikely(idev == NULL))
3463 changed = (!*valp) ^ (!new);
3466 dev_forward_change(idev);
3469 addrconf_forward_change();
3473 rt6_purge_dflt_routers();
3480 static struct addrconf_sysctl_table
3482 struct ctl_table_header *sysctl_header;
3483 ctl_table addrconf_vars[__NET_IPV6_MAX];
3484 ctl_table addrconf_dev[2];
3485 ctl_table addrconf_conf_dir[2];
3486 ctl_table addrconf_proto_dir[2];
3487 ctl_table addrconf_root_dir[2];
3488 } addrconf_sysctl = {
3489 .sysctl_header = NULL,
3492 .ctl_name = NET_IPV6_FORWARDING,
3493 .procname = "forwarding",
3494 .data = &ipv6_devconf.forwarding,
3495 .maxlen = sizeof(int),
3497 .proc_handler = &addrconf_sysctl_forward,
3498 .strategy = &addrconf_sysctl_forward_strategy,
3501 .ctl_name = NET_IPV6_HOP_LIMIT,
3502 .procname = "hop_limit",
3503 .data = &ipv6_devconf.hop_limit,
3504 .maxlen = sizeof(int),
3506 .proc_handler = proc_dointvec,
3509 .ctl_name = NET_IPV6_MTU,
3511 .data = &ipv6_devconf.mtu6,
3512 .maxlen = sizeof(int),
3514 .proc_handler = &proc_dointvec,
3517 .ctl_name = NET_IPV6_ACCEPT_RA,
3518 .procname = "accept_ra",
3519 .data = &ipv6_devconf.accept_ra,
3520 .maxlen = sizeof(int),
3522 .proc_handler = &proc_dointvec,
3525 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3526 .procname = "accept_redirects",
3527 .data = &ipv6_devconf.accept_redirects,
3528 .maxlen = sizeof(int),
3530 .proc_handler = &proc_dointvec,
3533 .ctl_name = NET_IPV6_AUTOCONF,
3534 .procname = "autoconf",
3535 .data = &ipv6_devconf.autoconf,
3536 .maxlen = sizeof(int),
3538 .proc_handler = &proc_dointvec,
3541 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3542 .procname = "dad_transmits",
3543 .data = &ipv6_devconf.dad_transmits,
3544 .maxlen = sizeof(int),
3546 .proc_handler = &proc_dointvec,
3549 .ctl_name = NET_IPV6_RTR_SOLICITS,
3550 .procname = "router_solicitations",
3551 .data = &ipv6_devconf.rtr_solicits,
3552 .maxlen = sizeof(int),
3554 .proc_handler = &proc_dointvec,
3557 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3558 .procname = "router_solicitation_interval",
3559 .data = &ipv6_devconf.rtr_solicit_interval,
3560 .maxlen = sizeof(int),
3562 .proc_handler = &proc_dointvec_jiffies,
3563 .strategy = &sysctl_jiffies,
3566 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3567 .procname = "router_solicitation_delay",
3568 .data = &ipv6_devconf.rtr_solicit_delay,
3569 .maxlen = sizeof(int),
3571 .proc_handler = &proc_dointvec_jiffies,
3572 .strategy = &sysctl_jiffies,
3575 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3576 .procname = "force_mld_version",
3577 .data = &ipv6_devconf.force_mld_version,
3578 .maxlen = sizeof(int),
3580 .proc_handler = &proc_dointvec,
3582 #ifdef CONFIG_IPV6_PRIVACY
3584 .ctl_name = NET_IPV6_USE_TEMPADDR,
3585 .procname = "use_tempaddr",
3586 .data = &ipv6_devconf.use_tempaddr,
3587 .maxlen = sizeof(int),
3589 .proc_handler = &proc_dointvec,
3592 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3593 .procname = "temp_valid_lft",
3594 .data = &ipv6_devconf.temp_valid_lft,
3595 .maxlen = sizeof(int),
3597 .proc_handler = &proc_dointvec,
3600 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3601 .procname = "temp_prefered_lft",
3602 .data = &ipv6_devconf.temp_prefered_lft,
3603 .maxlen = sizeof(int),
3605 .proc_handler = &proc_dointvec,
3608 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3609 .procname = "regen_max_retry",
3610 .data = &ipv6_devconf.regen_max_retry,
3611 .maxlen = sizeof(int),
3613 .proc_handler = &proc_dointvec,
3616 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3617 .procname = "max_desync_factor",
3618 .data = &ipv6_devconf.max_desync_factor,
3619 .maxlen = sizeof(int),
3621 .proc_handler = &proc_dointvec,
3625 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3626 .procname = "max_addresses",
3627 .data = &ipv6_devconf.max_addresses,
3628 .maxlen = sizeof(int),
3630 .proc_handler = &proc_dointvec,
3633 .ctl_name = 0, /* sentinel */
3638 .ctl_name = NET_PROTO_CONF_ALL,
3641 .child = addrconf_sysctl.addrconf_vars,
3644 .ctl_name = 0, /* sentinel */
3647 .addrconf_conf_dir = {
3649 .ctl_name = NET_IPV6_CONF,
3652 .child = addrconf_sysctl.addrconf_dev,
3655 .ctl_name = 0, /* sentinel */
3658 .addrconf_proto_dir = {
3660 .ctl_name = NET_IPV6,
3663 .child = addrconf_sysctl.addrconf_conf_dir,
3666 .ctl_name = 0, /* sentinel */
3669 .addrconf_root_dir = {
3671 .ctl_name = CTL_NET,
3674 .child = addrconf_sysctl.addrconf_proto_dir,
3677 .ctl_name = 0, /* sentinel */
3682 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3685 struct net_device *dev = idev ? idev->dev : NULL;
3686 struct addrconf_sysctl_table *t;
3687 char *dev_name = NULL;
3689 t = kmalloc(sizeof(*t), GFP_KERNEL);
3692 memcpy(t, &addrconf_sysctl, sizeof(*t));
3693 for (i=0; t->addrconf_vars[i].data; i++) {
3694 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3695 t->addrconf_vars[i].de = NULL;
3696 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3699 dev_name = dev->name;
3700 t->addrconf_dev[0].ctl_name = dev->ifindex;
3702 dev_name = "default";
3703 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3707 * Make a copy of dev_name, because '.procname' is regarded as const
3708 * by sysctl and we wouldn't want anyone to change it under our feet
3709 * (see SIOCSIFNAME).
3711 dev_name = kstrdup(dev_name, GFP_KERNEL);
3715 t->addrconf_dev[0].procname = dev_name;
3717 t->addrconf_dev[0].child = t->addrconf_vars;
3718 t->addrconf_dev[0].de = NULL;
3719 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3720 t->addrconf_conf_dir[0].de = NULL;
3721 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3722 t->addrconf_proto_dir[0].de = NULL;
3723 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3724 t->addrconf_root_dir[0].de = NULL;
3726 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3727 if (t->sysctl_header == NULL)
3742 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3745 struct addrconf_sysctl_table *t = p->sysctl;
3747 unregister_sysctl_table(t->sysctl_header);
3748 kfree(t->addrconf_dev[0].procname);
3760 int register_inet6addr_notifier(struct notifier_block *nb)
3762 return notifier_chain_register(&inet6addr_chain, nb);
3765 int unregister_inet6addr_notifier(struct notifier_block *nb)
3767 return notifier_chain_unregister(&inet6addr_chain,nb);
3771 * Init / cleanup code
3774 int __init addrconf_init(void)
3778 /* The addrconf netdev notifier requires that loopback_dev
3779 * has it's ipv6 private information allocated and setup
3780 * before it can bring up and give link-local addresses
3781 * to other devices which are up.
3783 * Unfortunately, loopback_dev is not necessarily the first
3784 * entry in the global dev_base list of net devices. In fact,
3785 * it is likely to be the very last entry on that list.
3786 * So this causes the notifier registry below to try and
3787 * give link-local addresses to all devices besides loopback_dev
3788 * first, then loopback_dev, which cases all the non-loopback_dev
3789 * devices to fail to get a link-local address.
3791 * So, as a temporary fix, allocate the ipv6 structure for
3792 * loopback_dev first by hand.
3793 * Longer term, all of the dependencies ipv6 has upon the loopback
3794 * device and it being up should be removed.
3797 if (!ipv6_add_dev(&loopback_dev))
3803 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
3805 register_netdevice_notifier(&ipv6_dev_notf);
3807 #ifdef CONFIG_IPV6_PRIVACY
3808 md5_tfm = crypto_alloc_tfm("md5", 0);
3809 if (unlikely(md5_tfm == NULL))
3811 "failed to load transform for md5\n");
3815 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3816 #ifdef CONFIG_SYSCTL
3817 addrconf_sysctl.sysctl_header =
3818 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3819 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3825 void __exit addrconf_cleanup(void)
3827 struct net_device *dev;
3828 struct inet6_dev *idev;
3829 struct inet6_ifaddr *ifa;
3832 unregister_netdevice_notifier(&ipv6_dev_notf);
3834 rtnetlink_links[PF_INET6] = NULL;
3835 #ifdef CONFIG_SYSCTL
3836 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3837 addrconf_sysctl_unregister(&ipv6_devconf);
3846 for (dev=dev_base; dev; dev=dev->next) {
3847 if ((idev = __in6_dev_get(dev)) == NULL)
3849 addrconf_ifdown(dev, 1);
3851 addrconf_ifdown(&loopback_dev, 2);
3857 write_lock_bh(&addrconf_hash_lock);
3858 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3859 for (ifa=inet6_addr_lst[i]; ifa; ) {
3860 struct inet6_ifaddr *bifa;
3863 ifa = ifa->lst_next;
3864 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3865 /* Do not free it; something is wrong.
3866 Now we can investigate it with debugger.
3870 write_unlock_bh(&addrconf_hash_lock);
3872 del_timer(&addr_chk_timer);
3876 #ifdef CONFIG_IPV6_PRIVACY
3877 crypto_free_tfm(md5_tfm);
3881 #ifdef CONFIG_PROC_FS
3882 proc_net_remove("if_inet6");