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/errno.h>
44 #include <linux/types.h>
45 #include <linux/socket.h>
46 #include <linux/sockios.h>
47 #include <linux/net.h>
48 #include <linux/in6.h>
49 #include <linux/netdevice.h>
50 #include <linux/if_addr.h>
51 #include <linux/if_arp.h>
52 #include <linux/if_arcnet.h>
53 #include <linux/if_infiniband.h>
54 #include <linux/route.h>
55 #include <linux/inetdevice.h>
56 #include <linux/init.h>
58 #include <linux/sysctl.h>
60 #include <linux/capability.h>
61 #include <linux/delay.h>
62 #include <linux/notifier.h>
63 #include <linux/string.h>
65 #include <net/net_namespace.h>
70 #include <net/protocol.h>
71 #include <net/ndisc.h>
72 #include <net/ip6_route.h>
73 #include <net/addrconf.h>
76 #include <net/netlink.h>
77 #include <net/pkt_sched.h>
78 #include <linux/if_tunnel.h>
79 #include <linux/rtnetlink.h>
81 #ifdef CONFIG_IPV6_PRIVACY
82 #include <linux/random.h>
85 #include <asm/uaccess.h>
86 #include <asm/unaligned.h>
88 #include <linux/proc_fs.h>
89 #include <linux/seq_file.h>
91 /* Set to 3 to get tracing... */
95 #define ADBG(x) printk x
100 #define INFINITY_LIFE_TIME 0xFFFFFFFF
101 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
104 static void addrconf_sysctl_register(struct inet6_dev *idev);
105 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
108 #ifdef CONFIG_IPV6_PRIVACY
109 static int __ipv6_regen_rndid(struct inet6_dev *idev);
110 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
111 static void ipv6_regen_rndid(unsigned long data);
113 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
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 static void addrconf_verify(unsigned long);
126 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
127 static DEFINE_SPINLOCK(addrconf_verify_lock);
129 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
130 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
132 static int addrconf_ifdown(struct net_device *dev, int how);
134 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
135 static void addrconf_dad_timer(unsigned long data);
136 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
137 static void addrconf_dad_run(struct inet6_dev *idev);
138 static void addrconf_rs_timer(unsigned long data);
139 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
140 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
142 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
143 struct prefix_info *pinfo);
144 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
146 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
148 struct ipv6_devconf ipv6_devconf __read_mostly = {
150 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
151 .mtu6 = IPV6_MIN_MTU,
153 .accept_redirects = 1,
155 .force_mld_version = 0,
157 .rtr_solicits = MAX_RTR_SOLICITATIONS,
158 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
159 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
160 #ifdef CONFIG_IPV6_PRIVACY
162 .temp_valid_lft = TEMP_VALID_LIFETIME,
163 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
164 .regen_max_retry = REGEN_MAX_RETRY,
165 .max_desync_factor = MAX_DESYNC_FACTOR,
167 .max_addresses = IPV6_MAX_ADDRESSES,
168 .accept_ra_defrtr = 1,
169 .accept_ra_pinfo = 1,
170 #ifdef CONFIG_IPV6_ROUTER_PREF
171 .accept_ra_rtr_pref = 1,
172 .rtr_probe_interval = 60 * HZ,
173 #ifdef CONFIG_IPV6_ROUTE_INFO
174 .accept_ra_rt_info_max_plen = 0,
178 .accept_source_route = 0, /* we do not accept RH0 by default. */
181 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
183 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
184 .mtu6 = IPV6_MIN_MTU,
186 .accept_redirects = 1,
189 .rtr_solicits = MAX_RTR_SOLICITATIONS,
190 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
191 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
192 #ifdef CONFIG_IPV6_PRIVACY
194 .temp_valid_lft = TEMP_VALID_LIFETIME,
195 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
196 .regen_max_retry = REGEN_MAX_RETRY,
197 .max_desync_factor = MAX_DESYNC_FACTOR,
199 .max_addresses = IPV6_MAX_ADDRESSES,
200 .accept_ra_defrtr = 1,
201 .accept_ra_pinfo = 1,
202 #ifdef CONFIG_IPV6_ROUTER_PREF
203 .accept_ra_rtr_pref = 1,
204 .rtr_probe_interval = 60 * HZ,
205 #ifdef CONFIG_IPV6_ROUTE_INFO
206 .accept_ra_rt_info_max_plen = 0,
210 .accept_source_route = 0, /* we do not accept RH0 by default. */
213 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
214 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
215 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
217 /* Check if a valid qdisc is available */
218 static inline int addrconf_qdisc_ok(struct net_device *dev)
220 return (dev->qdisc != &noop_qdisc);
223 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
225 if (del_timer(&ifp->timer))
229 enum addrconf_timer_t
236 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
237 enum addrconf_timer_t what,
240 if (!del_timer(&ifp->timer))
245 ifp->timer.function = addrconf_dad_timer;
248 ifp->timer.function = addrconf_rs_timer;
252 ifp->timer.expires = jiffies + when;
253 add_timer(&ifp->timer);
256 static int snmp6_alloc_dev(struct inet6_dev *idev)
258 if (snmp_mib_init((void **)idev->stats.ipv6,
259 sizeof(struct ipstats_mib)) < 0)
261 if (snmp_mib_init((void **)idev->stats.icmpv6,
262 sizeof(struct icmpv6_mib)) < 0)
264 if (snmp_mib_init((void **)idev->stats.icmpv6msg,
265 sizeof(struct icmpv6msg_mib)) < 0)
271 snmp_mib_free((void **)idev->stats.icmpv6);
273 snmp_mib_free((void **)idev->stats.ipv6);
278 static void snmp6_free_dev(struct inet6_dev *idev)
280 snmp_mib_free((void **)idev->stats.icmpv6msg);
281 snmp_mib_free((void **)idev->stats.icmpv6);
282 snmp_mib_free((void **)idev->stats.ipv6);
285 /* Nobody refers to this device, we may destroy it. */
287 static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
289 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
293 void in6_dev_finish_destroy(struct inet6_dev *idev)
295 struct net_device *dev = idev->dev;
296 BUG_TRAP(idev->addr_list==NULL);
297 BUG_TRAP(idev->mc_list==NULL);
298 #ifdef NET_REFCNT_DEBUG
299 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
303 printk("Freeing alive inet6 device %p\n", idev);
306 snmp6_free_dev(idev);
307 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
310 EXPORT_SYMBOL(in6_dev_finish_destroy);
312 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
314 struct inet6_dev *ndev;
315 struct in6_addr maddr;
319 if (dev->mtu < IPV6_MIN_MTU)
322 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
327 rwlock_init(&ndev->lock);
329 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
330 ndev->cnf.mtu6 = dev->mtu;
331 ndev->cnf.sysctl = NULL;
332 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
333 if (ndev->nd_parms == NULL) {
337 /* We refer to the device */
340 if (snmp6_alloc_dev(ndev) < 0) {
342 "%s(): cannot allocate memory for statistics; dev=%s.\n",
343 __FUNCTION__, dev->name));
344 neigh_parms_release(&nd_tbl, ndev->nd_parms);
346 in6_dev_finish_destroy(ndev);
350 if (snmp6_register_dev(ndev) < 0) {
352 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
353 __FUNCTION__, dev->name));
354 neigh_parms_release(&nd_tbl, ndev->nd_parms);
356 in6_dev_finish_destroy(ndev);
360 /* One reference from device. We must do this before
361 * we invoke __ipv6_regen_rndid().
365 #ifdef CONFIG_IPV6_PRIVACY
366 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
367 if ((dev->flags&IFF_LOOPBACK) ||
368 dev->type == ARPHRD_TUNNEL ||
369 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
370 dev->type == ARPHRD_SIT ||
372 dev->type == ARPHRD_NONE) {
374 "%s: Disabled Privacy Extensions\n",
376 ndev->cnf.use_tempaddr = -1;
378 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
380 "%s: Disabled Multicast RS\n",
382 ndev->cnf.rtr_solicits = 0;
386 ipv6_regen_rndid((unsigned long) ndev);
390 if (netif_running(dev) && addrconf_qdisc_ok(dev))
391 ndev->if_flags |= IF_READY;
393 ipv6_mc_init_dev(ndev);
394 ndev->tstamp = jiffies;
396 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
397 NET_IPV6_NEIGH, "ipv6",
398 &ndisc_ifinfo_sysctl_change,
400 addrconf_sysctl_register(ndev);
402 /* protected by rtnl_lock */
403 rcu_assign_pointer(dev->ip6_ptr, ndev);
405 /* Join all-node multicast group */
406 ipv6_addr_all_nodes(&maddr);
407 ipv6_dev_mc_inc(dev, &maddr);
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 (ifa->flags&IFA_F_TENTATIVE)
449 if (idev->cnf.forwarding)
450 addrconf_join_anycast(ifa);
452 addrconf_leave_anycast(ifa);
457 static void addrconf_forward_change(void)
459 struct net_device *dev;
460 struct inet6_dev *idev;
462 read_lock(&dev_base_lock);
463 for_each_netdev(&init_net, dev) {
465 idev = __in6_dev_get(dev);
467 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
468 idev->cnf.forwarding = ipv6_devconf.forwarding;
470 dev_forward_change(idev);
474 read_unlock(&dev_base_lock);
477 static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
479 if (p == &ipv6_devconf_dflt.forwarding)
482 if (p == &ipv6_devconf.forwarding) {
483 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
484 addrconf_forward_change();
485 } else if ((!*p) ^ (!old))
486 dev_forward_change((struct inet6_dev *)table->extra1);
489 rt6_purge_dflt_routers();
493 /* Nobody refers to this ifaddr, destroy it */
495 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
497 BUG_TRAP(ifp->if_next==NULL);
498 BUG_TRAP(ifp->lst_next==NULL);
499 #ifdef NET_REFCNT_DEBUG
500 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
503 in6_dev_put(ifp->idev);
505 if (del_timer(&ifp->timer))
506 printk("Timer is still running, when freeing ifa=%p\n", ifp);
509 printk("Freeing alive inet6 address %p\n", ifp);
512 dst_release(&ifp->rt->u.dst);
518 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
520 struct inet6_ifaddr *ifa, **ifap;
521 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
524 * Each device address list is sorted in order of scope -
525 * global before linklocal.
527 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
528 ifap = &ifa->if_next) {
529 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
533 ifp->if_next = *ifap;
537 /* On success it returns ifp with increased reference count */
539 static struct inet6_ifaddr *
540 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
541 int scope, u32 flags)
543 struct inet6_ifaddr *ifa = NULL;
550 err = -ENODEV; /*XXX*/
554 write_lock(&addrconf_hash_lock);
556 /* Ignore adding duplicate addresses on an interface */
557 if (ipv6_chk_same_addr(addr, idev->dev)) {
558 ADBG(("ipv6_add_addr: already assigned\n"));
563 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
566 ADBG(("ipv6_add_addr: malloc failed\n"));
571 rt = addrconf_dst_alloc(idev, addr, 0);
577 ipv6_addr_copy(&ifa->addr, addr);
579 spin_lock_init(&ifa->lock);
580 init_timer(&ifa->timer);
581 ifa->timer.data = (unsigned long) ifa;
583 ifa->prefix_len = pfxlen;
584 ifa->flags = flags | IFA_F_TENTATIVE;
585 ifa->cstamp = ifa->tstamp = jiffies;
590 * part one of RFC 4429, section 3.3
591 * We should not configure an address as
592 * optimistic if we do not yet know the link
593 * layer address of our nexhop router
596 if (rt->rt6i_nexthop == NULL)
597 ifa->flags &= ~IFA_F_OPTIMISTIC;
604 /* Add to big hash table */
605 hash = ipv6_addr_hash(addr);
607 ifa->lst_next = inet6_addr_lst[hash];
608 inet6_addr_lst[hash] = ifa;
610 write_unlock(&addrconf_hash_lock);
612 write_lock(&idev->lock);
613 /* Add to inet6_dev unicast addr list. */
614 ipv6_link_dev_addr(idev, ifa);
616 #ifdef CONFIG_IPV6_PRIVACY
617 if (ifa->flags&IFA_F_TEMPORARY) {
618 ifa->tmp_next = idev->tempaddr_list;
619 idev->tempaddr_list = ifa;
625 write_unlock(&idev->lock);
627 rcu_read_unlock_bh();
629 if (likely(err == 0))
630 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
638 write_unlock(&addrconf_hash_lock);
642 /* This function wants to get referenced ifp and releases it before return */
644 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
646 struct inet6_ifaddr *ifa, **ifap;
647 struct inet6_dev *idev = ifp->idev;
649 int deleted = 0, onlink = 0;
650 unsigned long expires = jiffies;
652 hash = ipv6_addr_hash(&ifp->addr);
656 write_lock_bh(&addrconf_hash_lock);
657 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
658 ifap = &ifa->lst_next) {
660 *ifap = ifa->lst_next;
662 ifa->lst_next = NULL;
666 write_unlock_bh(&addrconf_hash_lock);
668 write_lock_bh(&idev->lock);
669 #ifdef CONFIG_IPV6_PRIVACY
670 if (ifp->flags&IFA_F_TEMPORARY) {
671 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
672 ifap = &ifa->tmp_next) {
674 *ifap = ifa->tmp_next;
676 in6_ifa_put(ifp->ifpub);
680 ifa->tmp_next = NULL;
687 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
689 *ifap = ifa->if_next;
692 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
696 } else if (ifp->flags & IFA_F_PERMANENT) {
697 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
699 if (ifa->flags & IFA_F_PERMANENT) {
704 unsigned long lifetime;
709 spin_lock(&ifa->lock);
710 lifetime = min_t(unsigned long,
711 ifa->valid_lft, 0x7fffffffUL/HZ);
712 if (time_before(expires,
713 ifa->tstamp + lifetime * HZ))
714 expires = ifa->tstamp + lifetime * HZ;
715 spin_unlock(&ifa->lock);
719 ifap = &ifa->if_next;
721 write_unlock_bh(&idev->lock);
723 ipv6_ifa_notify(RTM_DELADDR, ifp);
725 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
727 addrconf_del_timer(ifp);
730 * Purge or update corresponding prefix
732 * 1) we don't purge prefix here if address was not permanent.
733 * prefix is managed by its own lifetime.
734 * 2) if there're no addresses, delete prefix.
735 * 3) if there're still other permanent address(es),
736 * corresponding prefix is still permanent.
737 * 4) otherwise, update prefix lifetime to the
738 * longest valid lifetime among the corresponding
739 * addresses on the device.
740 * Note: subsequent RA will update lifetime.
744 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
745 struct in6_addr prefix;
748 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
749 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
751 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
755 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
756 rt->rt6i_expires = expires;
757 rt->rt6i_flags |= RTF_EXPIRES;
760 dst_release(&rt->u.dst);
766 #ifdef CONFIG_IPV6_PRIVACY
767 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
769 struct inet6_dev *idev = ifp->idev;
770 struct in6_addr addr, *tmpaddr;
771 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
777 write_lock(&idev->lock);
779 spin_lock_bh(&ift->lock);
780 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
781 spin_unlock_bh(&ift->lock);
788 if (idev->cnf.use_tempaddr <= 0) {
789 write_unlock(&idev->lock);
791 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
796 spin_lock_bh(&ifp->lock);
797 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
798 idev->cnf.use_tempaddr = -1; /*XXX*/
799 spin_unlock_bh(&ifp->lock);
800 write_unlock(&idev->lock);
802 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
808 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
809 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
810 spin_unlock_bh(&ifp->lock);
811 write_unlock(&idev->lock);
813 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
819 memcpy(&addr.s6_addr[8], idev->rndid, 8);
820 tmp_valid_lft = min_t(__u32,
822 idev->cnf.temp_valid_lft);
823 tmp_prefered_lft = min_t(__u32,
825 idev->cnf.temp_prefered_lft - desync_factor / HZ);
826 tmp_plen = ifp->prefix_len;
827 max_addresses = idev->cnf.max_addresses;
828 tmp_cstamp = ifp->cstamp;
829 tmp_tstamp = ifp->tstamp;
830 spin_unlock_bh(&ifp->lock);
832 write_unlock(&idev->lock);
834 addr_flags = IFA_F_TEMPORARY;
835 /* set in addrconf_prefix_rcv() */
836 if (ifp->flags & IFA_F_OPTIMISTIC)
837 addr_flags |= IFA_F_OPTIMISTIC;
839 ift = !max_addresses ||
840 ipv6_count_addresses(idev) < max_addresses ?
841 ipv6_add_addr(idev, &addr, tmp_plen,
842 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
844 if (!ift || IS_ERR(ift)) {
848 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
850 write_lock(&idev->lock);
854 spin_lock_bh(&ift->lock);
856 ift->valid_lft = tmp_valid_lft;
857 ift->prefered_lft = tmp_prefered_lft;
858 ift->cstamp = tmp_cstamp;
859 ift->tstamp = tmp_tstamp;
860 spin_unlock_bh(&ift->lock);
862 addrconf_dad_start(ift, 0);
871 * Choose an appropriate source address (RFC3484)
873 struct ipv6_saddr_score {
881 #define IPV6_SADDR_SCORE_LOCAL 0x0001
882 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
883 #define IPV6_SADDR_SCORE_HOA 0x0008
884 #define IPV6_SADDR_SCORE_OIF 0x0010
885 #define IPV6_SADDR_SCORE_LABEL 0x0020
886 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
888 static inline int ipv6_saddr_preferred(int type)
890 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
891 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
896 int ipv6_dev_get_saddr(struct net_device *daddr_dev,
897 struct in6_addr *daddr, struct in6_addr *saddr)
899 struct ipv6_saddr_score hiscore;
900 struct inet6_ifaddr *ifa_result = NULL;
901 int daddr_type = __ipv6_addr_type(daddr);
902 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
903 int daddr_ifindex = daddr_dev ? daddr_dev->ifindex : 0;
904 u32 daddr_label = ipv6_addr_label(daddr, daddr_type, daddr_ifindex);
905 struct net_device *dev;
907 memset(&hiscore, 0, sizeof(hiscore));
909 read_lock(&dev_base_lock);
912 for_each_netdev(&init_net, dev) {
913 struct inet6_dev *idev;
914 struct inet6_ifaddr *ifa;
916 /* Rule 0: Candidate Source Address (section 4)
917 * - multicast and link-local destination address,
918 * the set of candidate source address MUST only
919 * include addresses assigned to interfaces
920 * belonging to the same link as the outgoing
922 * (- For site-local destination addresses, the
923 * set of candidate source addresses MUST only
924 * include addresses assigned to interfaces
925 * belonging to the same site as the outgoing
928 if ((daddr_type & IPV6_ADDR_MULTICAST ||
929 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
930 daddr_dev && dev != daddr_dev)
933 idev = __in6_dev_get(dev);
937 read_lock_bh(&idev->lock);
938 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
939 struct ipv6_saddr_score score;
941 score.addr_type = __ipv6_addr_type(&ifa->addr);
944 * - Tentative Address (RFC2462 section 5.4)
945 * - A tentative address is not considered
946 * "assigned to an interface" in the traditional
947 * sense, unless it is also flagged as optimistic.
948 * - Candidate Source Address (section 4)
949 * - In any case, anycast addresses, multicast
950 * addresses, and the unspecified address MUST
951 * NOT be included in a candidate set.
953 if ((ifa->flags & IFA_F_TENTATIVE) &&
954 (!(ifa->flags & IFA_F_OPTIMISTIC)))
956 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
957 score.addr_type & IPV6_ADDR_MULTICAST)) {
958 LIMIT_NETDEBUG(KERN_DEBUG
959 "ADDRCONF: unspecified / multicast address "
960 "assigned as unicast address on %s",
970 if (ifa_result == NULL) {
971 /* record it if the first available entry */
975 /* Rule 1: Prefer same address */
976 if (hiscore.rule < 1) {
977 if (ipv6_addr_equal(&ifa_result->addr, daddr))
978 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
981 if (ipv6_addr_equal(&ifa->addr, daddr)) {
982 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
983 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
988 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
992 /* Rule 2: Prefer appropriate scope */
993 if (hiscore.rule < 2) {
994 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
997 score.scope = __ipv6_addr_src_scope(score.addr_type);
998 if (hiscore.scope < score.scope) {
999 if (hiscore.scope < daddr_scope) {
1004 } else if (score.scope < hiscore.scope) {
1005 if (score.scope < daddr_scope)
1006 break; /* addresses sorted by scope */
1013 /* Rule 3: Avoid deprecated and optimistic addresses */
1014 if (hiscore.rule < 3) {
1015 if (ipv6_saddr_preferred(hiscore.addr_type) ||
1016 (((ifa_result->flags &
1017 (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0)))
1018 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1021 if (ipv6_saddr_preferred(score.addr_type) ||
1023 (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0))) {
1024 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1025 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1030 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1034 /* Rule 4: Prefer home address */
1035 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
1036 if (hiscore.rule < 4) {
1037 if (ifa_result->flags & IFA_F_HOMEADDRESS)
1038 hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
1041 if (ifa->flags & IFA_F_HOMEADDRESS) {
1042 score.attrs |= IPV6_SADDR_SCORE_HOA;
1043 if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
1048 if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
1052 if (hiscore.rule < 4)
1056 /* Rule 5: Prefer outgoing interface */
1057 if (hiscore.rule < 5) {
1058 if (daddr_dev == NULL ||
1059 daddr_dev == ifa_result->idev->dev)
1060 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1063 if (daddr_dev == NULL ||
1064 daddr_dev == ifa->idev->dev) {
1065 score.attrs |= IPV6_SADDR_SCORE_OIF;
1066 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1071 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1075 /* Rule 6: Prefer matching label */
1076 if (hiscore.rule < 6) {
1077 if (ipv6_addr_label(&ifa_result->addr,
1079 ifa_result->idev->dev->ifindex) == daddr_label)
1080 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1083 if (ipv6_addr_label(&ifa->addr,
1085 ifa->idev->dev->ifindex) == daddr_label) {
1086 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1087 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1092 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1096 #ifdef CONFIG_IPV6_PRIVACY
1097 /* Rule 7: Prefer public address
1098 * Note: prefer temprary address if use_tempaddr >= 2
1100 if (hiscore.rule < 7) {
1101 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1102 (ifa_result->idev->cnf.use_tempaddr >= 2))
1103 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1106 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1107 (ifa->idev->cnf.use_tempaddr >= 2)) {
1108 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1109 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1114 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1118 if (hiscore.rule < 7)
1121 /* Rule 8: Use longest matching prefix */
1122 if (hiscore.rule < 8) {
1123 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
1126 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1127 if (score.matchlen > hiscore.matchlen) {
1132 else if (score.matchlen < hiscore.matchlen)
1136 /* Final Rule: choose first available one */
1140 in6_ifa_put(ifa_result);
1145 read_unlock_bh(&idev->lock);
1148 read_unlock(&dev_base_lock);
1151 return -EADDRNOTAVAIL;
1153 ipv6_addr_copy(saddr, &ifa_result->addr);
1154 in6_ifa_put(ifa_result);
1159 int ipv6_get_saddr(struct dst_entry *dst,
1160 struct in6_addr *daddr, struct in6_addr *saddr)
1162 return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
1165 EXPORT_SYMBOL(ipv6_get_saddr);
1167 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1168 unsigned char banned_flags)
1170 struct inet6_dev *idev;
1171 int err = -EADDRNOTAVAIL;
1174 if ((idev = __in6_dev_get(dev)) != NULL) {
1175 struct inet6_ifaddr *ifp;
1177 read_lock_bh(&idev->lock);
1178 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1179 if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) {
1180 ipv6_addr_copy(addr, &ifp->addr);
1185 read_unlock_bh(&idev->lock);
1191 static int ipv6_count_addresses(struct inet6_dev *idev)
1194 struct inet6_ifaddr *ifp;
1196 read_lock_bh(&idev->lock);
1197 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1199 read_unlock_bh(&idev->lock);
1203 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1205 struct inet6_ifaddr * ifp;
1206 u8 hash = ipv6_addr_hash(addr);
1208 read_lock_bh(&addrconf_hash_lock);
1209 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1210 if (ipv6_addr_equal(&ifp->addr, addr) &&
1211 !(ifp->flags&IFA_F_TENTATIVE)) {
1212 if (dev == NULL || ifp->idev->dev == dev ||
1213 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1217 read_unlock_bh(&addrconf_hash_lock);
1221 EXPORT_SYMBOL(ipv6_chk_addr);
1224 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1226 struct inet6_ifaddr * ifp;
1227 u8 hash = ipv6_addr_hash(addr);
1229 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1230 if (ipv6_addr_equal(&ifp->addr, addr)) {
1231 if (dev == NULL || ifp->idev->dev == dev)
1238 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1240 struct inet6_ifaddr * ifp;
1241 u8 hash = ipv6_addr_hash(addr);
1243 read_lock_bh(&addrconf_hash_lock);
1244 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1245 if (ipv6_addr_equal(&ifp->addr, addr)) {
1246 if (dev == NULL || ifp->idev->dev == dev ||
1247 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1253 read_unlock_bh(&addrconf_hash_lock);
1258 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1260 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1261 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
1262 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1263 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1264 int sk_ipv6only = ipv6_only_sock(sk);
1265 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1266 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1267 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1269 if (!sk2_rcv_saddr && !sk_ipv6only)
1272 if (addr_type2 == IPV6_ADDR_ANY &&
1273 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1276 if (addr_type == IPV6_ADDR_ANY &&
1277 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1280 if (sk2_rcv_saddr6 &&
1281 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1284 if (addr_type == IPV6_ADDR_MAPPED &&
1286 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1292 /* Gets referenced address, destroys ifaddr */
1294 static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1296 if (ifp->flags&IFA_F_PERMANENT) {
1297 spin_lock_bh(&ifp->lock);
1298 addrconf_del_timer(ifp);
1299 ifp->flags |= IFA_F_TENTATIVE;
1300 spin_unlock_bh(&ifp->lock);
1302 #ifdef CONFIG_IPV6_PRIVACY
1303 } else if (ifp->flags&IFA_F_TEMPORARY) {
1304 struct inet6_ifaddr *ifpub;
1305 spin_lock_bh(&ifp->lock);
1308 in6_ifa_hold(ifpub);
1309 spin_unlock_bh(&ifp->lock);
1310 ipv6_create_tempaddr(ifpub, ifp);
1313 spin_unlock_bh(&ifp->lock);
1321 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1323 if (net_ratelimit())
1324 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1325 addrconf_dad_stop(ifp);
1328 /* Join to solicited addr multicast group. */
1330 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1332 struct in6_addr maddr;
1334 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1337 addrconf_addr_solict_mult(addr, &maddr);
1338 ipv6_dev_mc_inc(dev, &maddr);
1341 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1343 struct in6_addr maddr;
1345 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1348 addrconf_addr_solict_mult(addr, &maddr);
1349 __ipv6_dev_mc_dec(idev, &maddr);
1352 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1354 struct in6_addr addr;
1355 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1356 if (ipv6_addr_any(&addr))
1358 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1361 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1363 struct in6_addr addr;
1364 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1365 if (ipv6_addr_any(&addr))
1367 __ipv6_dev_ac_dec(ifp->idev, &addr);
1370 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1372 if (dev->addr_len != ETH_ALEN)
1374 memcpy(eui, dev->dev_addr, 3);
1375 memcpy(eui + 5, dev->dev_addr + 3, 3);
1378 * The zSeries OSA network cards can be shared among various
1379 * OS instances, but the OSA cards have only one MAC address.
1380 * This leads to duplicate address conflicts in conjunction
1381 * with IPv6 if more than one instance uses the same card.
1383 * The driver for these cards can deliver a unique 16-bit
1384 * identifier for each instance sharing the same card. It is
1385 * placed instead of 0xFFFE in the interface identifier. The
1386 * "u" bit of the interface identifier is not inverted in this
1387 * case. Hence the resulting interface identifier has local
1388 * scope according to RFC2373.
1391 eui[3] = (dev->dev_id >> 8) & 0xFF;
1392 eui[4] = dev->dev_id & 0xFF;
1401 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1403 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1404 if (dev->addr_len != ARCNET_ALEN)
1407 eui[7] = *(u8*)dev->dev_addr;
1411 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1413 if (dev->addr_len != INFINIBAND_ALEN)
1415 memcpy(eui, dev->dev_addr + 12, 8);
1420 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1422 switch (dev->type) {
1425 case ARPHRD_IEEE802_TR:
1426 return addrconf_ifid_eui48(eui, dev);
1428 return addrconf_ifid_arcnet(eui, dev);
1429 case ARPHRD_INFINIBAND:
1430 return addrconf_ifid_infiniband(eui, dev);
1432 if (dev->priv_flags & IFF_ISATAP)
1433 return ipv6_isatap_eui64(eui, *(__be32 *)dev->dev_addr);
1438 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1441 struct inet6_ifaddr *ifp;
1443 read_lock_bh(&idev->lock);
1444 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1445 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1446 memcpy(eui, ifp->addr.s6_addr+8, 8);
1451 read_unlock_bh(&idev->lock);
1455 #ifdef CONFIG_IPV6_PRIVACY
1456 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1457 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1460 get_random_bytes(idev->rndid, sizeof(idev->rndid));
1461 idev->rndid[0] &= ~0x02;
1464 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1465 * check if generated address is not inappropriate
1467 * - Reserved subnet anycast (RFC 2526)
1468 * 11111101 11....11 1xxxxxxx
1469 * - ISATAP (RFC4214) 6.1
1470 * 00-00-5E-FE-xx-xx-xx-xx
1472 * - XXX: already assigned to an address on the device
1474 if (idev->rndid[0] == 0xfd &&
1475 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1476 (idev->rndid[7]&0x80))
1478 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1479 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1481 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1488 static void ipv6_regen_rndid(unsigned long data)
1490 struct inet6_dev *idev = (struct inet6_dev *) data;
1491 unsigned long expires;
1494 write_lock_bh(&idev->lock);
1499 if (__ipv6_regen_rndid(idev) < 0)
1503 idev->cnf.temp_prefered_lft * HZ -
1504 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1505 if (time_before(expires, jiffies)) {
1507 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1512 if (!mod_timer(&idev->regen_timer, expires))
1516 write_unlock_bh(&idev->lock);
1517 rcu_read_unlock_bh();
1521 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1524 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1525 ret = __ipv6_regen_rndid(idev);
1535 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1536 unsigned long expires, u32 flags)
1538 struct fib6_config cfg = {
1539 .fc_table = RT6_TABLE_PREFIX,
1540 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1541 .fc_ifindex = dev->ifindex,
1542 .fc_expires = expires,
1544 .fc_flags = RTF_UP | flags,
1547 ipv6_addr_copy(&cfg.fc_dst, pfx);
1549 /* Prevent useless cloning on PtP SIT.
1550 This thing is done here expecting that the whole
1551 class of non-broadcast devices need not cloning.
1553 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1554 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1555 cfg.fc_flags |= RTF_NONEXTHOP;
1558 ip6_route_add(&cfg);
1561 /* Create "default" multicast route to the interface */
1563 static void addrconf_add_mroute(struct net_device *dev)
1565 struct fib6_config cfg = {
1566 .fc_table = RT6_TABLE_LOCAL,
1567 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1568 .fc_ifindex = dev->ifindex,
1573 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1575 ip6_route_add(&cfg);
1578 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1579 static void sit_route_add(struct net_device *dev)
1581 struct fib6_config cfg = {
1582 .fc_table = RT6_TABLE_MAIN,
1583 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1584 .fc_ifindex = dev->ifindex,
1586 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1589 /* prefix length - 96 bits "::d.d.d.d" */
1590 ip6_route_add(&cfg);
1594 static void addrconf_add_lroute(struct net_device *dev)
1596 struct in6_addr addr;
1598 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1599 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1602 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1604 struct inet6_dev *idev;
1608 if ((idev = ipv6_find_idev(dev)) == NULL)
1611 /* Add default multicast route */
1612 addrconf_add_mroute(dev);
1614 /* Add link local route */
1615 addrconf_add_lroute(dev);
1619 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1621 struct prefix_info *pinfo;
1625 unsigned long rt_expires;
1626 struct inet6_dev *in6_dev;
1628 pinfo = (struct prefix_info *) opt;
1630 if (len < sizeof(struct prefix_info)) {
1631 ADBG(("addrconf: prefix option too short\n"));
1636 * Validation checks ([ADDRCONF], page 19)
1639 addr_type = ipv6_addr_type(&pinfo->prefix);
1641 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1644 valid_lft = ntohl(pinfo->valid);
1645 prefered_lft = ntohl(pinfo->prefered);
1647 if (prefered_lft > valid_lft) {
1648 if (net_ratelimit())
1649 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1653 in6_dev = in6_dev_get(dev);
1655 if (in6_dev == NULL) {
1656 if (net_ratelimit())
1657 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1662 * Two things going on here:
1663 * 1) Add routes for on-link prefixes
1664 * 2) Configure prefixes with the auto flag set
1667 /* Avoid arithmetic overflow. Really, we could
1668 save rt_expires in seconds, likely valid_lft,
1669 but it would require division in fib gc, that it
1672 if (valid_lft >= 0x7FFFFFFF/HZ)
1673 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1675 rt_expires = valid_lft * HZ;
1678 * We convert this (in jiffies) to clock_t later.
1679 * Avoid arithmetic overflow there as well.
1680 * Overflow can happen only if HZ < USER_HZ.
1682 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1683 rt_expires = 0x7FFFFFFF / USER_HZ;
1685 if (pinfo->onlink) {
1686 struct rt6_info *rt;
1687 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1689 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1690 if (rt->rt6i_flags&RTF_EXPIRES) {
1691 if (valid_lft == 0) {
1695 rt->rt6i_expires = jiffies + rt_expires;
1698 } else if (valid_lft) {
1699 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1700 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1703 dst_release(&rt->u.dst);
1706 /* Try to figure out our local address for this prefix */
1708 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1709 struct inet6_ifaddr * ifp;
1710 struct in6_addr addr;
1711 int create = 0, update_lft = 0;
1713 if (pinfo->prefix_len == 64) {
1714 memcpy(&addr, &pinfo->prefix, 8);
1715 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1716 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1717 in6_dev_put(in6_dev);
1722 if (net_ratelimit())
1723 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1725 in6_dev_put(in6_dev);
1730 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1732 if (ifp == NULL && valid_lft) {
1733 int max_addresses = in6_dev->cnf.max_addresses;
1736 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1737 if (in6_dev->cnf.optimistic_dad &&
1738 !ipv6_devconf.forwarding)
1739 addr_flags = IFA_F_OPTIMISTIC;
1742 /* Do not allow to create too much of autoconfigured
1743 * addresses; this would be too easy way to crash kernel.
1745 if (!max_addresses ||
1746 ipv6_count_addresses(in6_dev) < max_addresses)
1747 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1748 addr_type&IPV6_ADDR_SCOPE_MASK,
1751 if (!ifp || IS_ERR(ifp)) {
1752 in6_dev_put(in6_dev);
1756 update_lft = create = 1;
1757 ifp->cstamp = jiffies;
1758 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1764 #ifdef CONFIG_IPV6_PRIVACY
1765 struct inet6_ifaddr *ift;
1769 /* update lifetime (RFC2462 5.5.3 e) */
1770 spin_lock(&ifp->lock);
1772 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1773 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1776 if (!update_lft && stored_lft) {
1777 if (valid_lft > MIN_VALID_LIFETIME ||
1778 valid_lft > stored_lft)
1780 else if (stored_lft <= MIN_VALID_LIFETIME) {
1781 /* valid_lft <= stored_lft is always true */
1785 valid_lft = MIN_VALID_LIFETIME;
1786 if (valid_lft < prefered_lft)
1787 prefered_lft = valid_lft;
1793 ifp->valid_lft = valid_lft;
1794 ifp->prefered_lft = prefered_lft;
1797 ifp->flags &= ~IFA_F_DEPRECATED;
1798 spin_unlock(&ifp->lock);
1800 if (!(flags&IFA_F_TENTATIVE))
1801 ipv6_ifa_notify(0, ifp);
1803 spin_unlock(&ifp->lock);
1805 #ifdef CONFIG_IPV6_PRIVACY
1806 read_lock_bh(&in6_dev->lock);
1807 /* update all temporary addresses in the list */
1808 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1810 * When adjusting the lifetimes of an existing
1811 * temporary address, only lower the lifetimes.
1812 * Implementations must not increase the
1813 * lifetimes of an existing temporary address
1814 * when processing a Prefix Information Option.
1816 spin_lock(&ift->lock);
1818 if (ift->valid_lft > valid_lft &&
1819 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1820 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1821 if (ift->prefered_lft > prefered_lft &&
1822 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1823 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1824 spin_unlock(&ift->lock);
1825 if (!(flags&IFA_F_TENTATIVE))
1826 ipv6_ifa_notify(0, ift);
1829 if (create && in6_dev->cnf.use_tempaddr > 0) {
1831 * When a new public address is created as described in [ADDRCONF],
1832 * also create a new temporary address.
1834 read_unlock_bh(&in6_dev->lock);
1835 ipv6_create_tempaddr(ifp, NULL);
1837 read_unlock_bh(&in6_dev->lock);
1844 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1845 in6_dev_put(in6_dev);
1849 * Set destination address.
1850 * Special case for SIT interfaces where we create a new "virtual"
1853 int addrconf_set_dstaddr(void __user *arg)
1855 struct in6_ifreq ireq;
1856 struct net_device *dev;
1862 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1865 dev = __dev_get_by_index(&init_net, ireq.ifr6_ifindex);
1871 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1872 if (dev->type == ARPHRD_SIT) {
1875 struct ip_tunnel_parm p;
1877 err = -EADDRNOTAVAIL;
1878 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1881 memset(&p, 0, sizeof(p));
1882 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1886 p.iph.protocol = IPPROTO_IPV6;
1888 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1890 oldfs = get_fs(); set_fs(KERNEL_DS);
1891 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1896 if ((dev = __dev_get_by_name(&init_net, p.name)) == NULL)
1898 err = dev_open(dev);
1909 * Manual configuration of address on an interface
1911 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
1912 __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
1914 struct inet6_ifaddr *ifp;
1915 struct inet6_dev *idev;
1916 struct net_device *dev;
1918 u32 flags = RTF_EXPIRES;
1922 /* check the lifetime */
1923 if (!valid_lft || prefered_lft > valid_lft)
1926 if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
1929 if ((idev = addrconf_add_dev(dev)) == NULL)
1932 scope = ipv6_addr_scope(pfx);
1934 if (valid_lft == INFINITY_LIFE_TIME) {
1935 ifa_flags |= IFA_F_PERMANENT;
1937 } else if (valid_lft >= 0x7FFFFFFF/HZ)
1938 valid_lft = 0x7FFFFFFF/HZ;
1940 if (prefered_lft == 0)
1941 ifa_flags |= IFA_F_DEPRECATED;
1942 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1943 (prefered_lft != INFINITY_LIFE_TIME))
1944 prefered_lft = 0x7FFFFFFF/HZ;
1946 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1949 spin_lock_bh(&ifp->lock);
1950 ifp->valid_lft = valid_lft;
1951 ifp->prefered_lft = prefered_lft;
1952 ifp->tstamp = jiffies;
1953 spin_unlock_bh(&ifp->lock);
1955 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
1956 jiffies_to_clock_t(valid_lft * HZ), flags);
1958 * Note that section 3.1 of RFC 4429 indicates
1959 * that the Optimistic flag should not be set for
1960 * manually configured addresses
1962 addrconf_dad_start(ifp, 0);
1968 return PTR_ERR(ifp);
1971 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1973 struct inet6_ifaddr *ifp;
1974 struct inet6_dev *idev;
1975 struct net_device *dev;
1977 if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
1980 if ((idev = __in6_dev_get(dev)) == NULL)
1983 read_lock_bh(&idev->lock);
1984 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1985 if (ifp->prefix_len == plen &&
1986 ipv6_addr_equal(pfx, &ifp->addr)) {
1988 read_unlock_bh(&idev->lock);
1992 /* If the last address is deleted administratively,
1993 disable IPv6 on this interface.
1995 if (idev->addr_list == NULL)
1996 addrconf_ifdown(idev->dev, 1);
2000 read_unlock_bh(&idev->lock);
2001 return -EADDRNOTAVAIL;
2005 int addrconf_add_ifaddr(void __user *arg)
2007 struct in6_ifreq ireq;
2010 if (!capable(CAP_NET_ADMIN))
2013 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2017 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
2018 IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2023 int addrconf_del_ifaddr(void __user *arg)
2025 struct in6_ifreq ireq;
2028 if (!capable(CAP_NET_ADMIN))
2031 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2035 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
2040 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2041 static void sit_add_v4_addrs(struct inet6_dev *idev)
2043 struct inet6_ifaddr * ifp;
2044 struct in6_addr addr;
2045 struct net_device *dev;
2050 memset(&addr, 0, sizeof(struct in6_addr));
2051 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2053 if (idev->dev->flags&IFF_POINTOPOINT) {
2054 addr.s6_addr32[0] = htonl(0xfe800000);
2057 scope = IPV6_ADDR_COMPATv4;
2060 if (addr.s6_addr32[3]) {
2061 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2063 spin_lock_bh(&ifp->lock);
2064 ifp->flags &= ~IFA_F_TENTATIVE;
2065 spin_unlock_bh(&ifp->lock);
2066 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2072 for_each_netdev(&init_net, dev) {
2073 struct in_device * in_dev = __in_dev_get_rtnl(dev);
2074 if (in_dev && (dev->flags & IFF_UP)) {
2075 struct in_ifaddr * ifa;
2079 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2082 addr.s6_addr32[3] = ifa->ifa_local;
2084 if (ifa->ifa_scope == RT_SCOPE_LINK)
2086 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2087 if (idev->dev->flags&IFF_POINTOPOINT)
2091 if (idev->dev->flags&IFF_POINTOPOINT)
2096 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2099 spin_lock_bh(&ifp->lock);
2100 ifp->flags &= ~IFA_F_TENTATIVE;
2101 spin_unlock_bh(&ifp->lock);
2102 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2111 static void init_loopback(struct net_device *dev)
2113 struct inet6_dev *idev;
2114 struct inet6_ifaddr * ifp;
2120 if ((idev = ipv6_find_idev(dev)) == NULL) {
2121 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2125 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2127 spin_lock_bh(&ifp->lock);
2128 ifp->flags &= ~IFA_F_TENTATIVE;
2129 spin_unlock_bh(&ifp->lock);
2130 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2135 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2137 struct inet6_ifaddr * ifp;
2138 u32 addr_flags = IFA_F_PERMANENT;
2140 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2141 if (idev->cnf.optimistic_dad &&
2142 !ipv6_devconf.forwarding)
2143 addr_flags |= IFA_F_OPTIMISTIC;
2147 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
2149 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2150 addrconf_dad_start(ifp, 0);
2155 static void addrconf_dev_config(struct net_device *dev)
2157 struct in6_addr addr;
2158 struct inet6_dev * idev;
2162 if ((dev->type != ARPHRD_ETHER) &&
2163 (dev->type != ARPHRD_FDDI) &&
2164 (dev->type != ARPHRD_IEEE802_TR) &&
2165 (dev->type != ARPHRD_ARCNET) &&
2166 (dev->type != ARPHRD_INFINIBAND)) {
2167 /* Alas, we support only Ethernet autoconfiguration. */
2171 idev = addrconf_add_dev(dev);
2175 memset(&addr, 0, sizeof(struct in6_addr));
2176 addr.s6_addr32[0] = htonl(0xFE800000);
2178 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2179 addrconf_add_linklocal(idev, &addr);
2182 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2183 static void addrconf_sit_config(struct net_device *dev)
2185 struct inet6_dev *idev;
2190 * Configure the tunnel with one of our IPv4
2191 * addresses... we should configure all of
2192 * our v4 addrs in the tunnel
2195 if ((idev = ipv6_find_idev(dev)) == NULL) {
2196 printk(KERN_DEBUG "init sit: add_dev failed\n");
2200 if (dev->priv_flags & IFF_ISATAP) {
2201 struct in6_addr addr;
2203 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2204 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2205 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2206 addrconf_add_linklocal(idev, &addr);
2210 sit_add_v4_addrs(idev);
2212 if (dev->flags&IFF_POINTOPOINT) {
2213 addrconf_add_mroute(dev);
2214 addrconf_add_lroute(dev);
2221 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2223 struct in6_addr lladdr;
2225 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
2226 addrconf_add_linklocal(idev, &lladdr);
2232 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2234 struct net_device *link_dev;
2236 /* first try to inherit the link-local address from the link device */
2237 if (idev->dev->iflink &&
2238 (link_dev = __dev_get_by_index(&init_net, idev->dev->iflink))) {
2239 if (!ipv6_inherit_linklocal(idev, link_dev))
2242 /* then try to inherit it from any device */
2243 for_each_netdev(&init_net, link_dev) {
2244 if (!ipv6_inherit_linklocal(idev, link_dev))
2247 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2251 * Autoconfigure tunnel with a link-local address so routing protocols,
2252 * DHCPv6, MLD etc. can be run over the virtual link
2255 static void addrconf_ip6_tnl_config(struct net_device *dev)
2257 struct inet6_dev *idev;
2261 if ((idev = addrconf_add_dev(dev)) == NULL) {
2262 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2265 ip6_tnl_add_linklocal(idev);
2268 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2271 struct net_device *dev = (struct net_device *) data;
2272 struct inet6_dev *idev = __in6_dev_get(dev);
2273 int run_pending = 0;
2276 if (dev->nd_net != &init_net)
2280 case NETDEV_REGISTER:
2281 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2282 idev = ipv6_add_dev(dev);
2284 return notifier_from_errno(-ENOMEM);
2289 if (dev->flags & IFF_SLAVE)
2292 if (event == NETDEV_UP) {
2293 if (!addrconf_qdisc_ok(dev)) {
2294 /* device is not ready yet. */
2296 "ADDRCONF(NETDEV_UP): %s: "
2297 "link is not ready\n",
2302 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2303 idev = ipv6_add_dev(dev);
2306 idev->if_flags |= IF_READY;
2308 if (!addrconf_qdisc_ok(dev)) {
2309 /* device is still not ready. */
2314 if (idev->if_flags & IF_READY) {
2315 /* device is already configured. */
2318 idev->if_flags |= IF_READY;
2322 "ADDRCONF(NETDEV_CHANGE): %s: "
2323 "link becomes ready\n",
2330 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2332 addrconf_sit_config(dev);
2335 case ARPHRD_TUNNEL6:
2336 addrconf_ip6_tnl_config(dev);
2338 case ARPHRD_LOOPBACK:
2343 addrconf_dev_config(dev);
2348 addrconf_dad_run(idev);
2350 /* If the MTU changed during the interface down, when the
2351 interface up, the changed MTU must be reflected in the
2352 idev as well as routers.
2354 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2355 rt6_mtu_change(dev, dev->mtu);
2356 idev->cnf.mtu6 = dev->mtu;
2358 idev->tstamp = jiffies;
2359 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2360 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2361 stop IPv6 on this interface.
2363 if (dev->mtu < IPV6_MIN_MTU)
2364 addrconf_ifdown(dev, event != NETDEV_DOWN);
2368 case NETDEV_CHANGEMTU:
2369 if (idev && dev->mtu >= IPV6_MIN_MTU) {
2370 rt6_mtu_change(dev, dev->mtu);
2371 idev->cnf.mtu6 = dev->mtu;
2375 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2376 idev = ipv6_add_dev(dev);
2381 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2384 case NETDEV_UNREGISTER:
2386 * Remove all addresses from this interface.
2388 addrconf_ifdown(dev, event != NETDEV_DOWN);
2391 case NETDEV_CHANGENAME:
2393 snmp6_unregister_dev(idev);
2394 #ifdef CONFIG_SYSCTL
2395 addrconf_sysctl_unregister(&idev->cnf);
2396 neigh_sysctl_unregister(idev->nd_parms);
2397 neigh_sysctl_register(dev, idev->nd_parms,
2398 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2399 &ndisc_ifinfo_sysctl_change,
2401 addrconf_sysctl_register(idev);
2403 err = snmp6_register_dev(idev);
2405 return notifier_from_errno(err);
2414 * addrconf module should be notified of a device going up
2416 static struct notifier_block ipv6_dev_notf = {
2417 .notifier_call = addrconf_notify,
2421 static int addrconf_ifdown(struct net_device *dev, int how)
2423 struct inet6_dev *idev;
2424 struct inet6_ifaddr *ifa, **bifa;
2429 if (dev == init_net.loopback_dev && how == 1)
2433 neigh_ifdown(&nd_tbl, dev);
2435 idev = __in6_dev_get(dev);
2439 /* Step 1: remove reference to ipv6 device from parent device.
2445 /* protected by rtnl_lock */
2446 rcu_assign_pointer(dev->ip6_ptr, NULL);
2448 /* Step 1.5: remove snmp6 entry */
2449 snmp6_unregister_dev(idev);
2453 /* Step 2: clear hash table */
2454 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2455 bifa = &inet6_addr_lst[i];
2457 write_lock_bh(&addrconf_hash_lock);
2458 while ((ifa = *bifa) != NULL) {
2459 if (ifa->idev == idev) {
2460 *bifa = ifa->lst_next;
2461 ifa->lst_next = NULL;
2462 addrconf_del_timer(ifa);
2466 bifa = &ifa->lst_next;
2468 write_unlock_bh(&addrconf_hash_lock);
2471 write_lock_bh(&idev->lock);
2473 /* Step 3: clear flags for stateless addrconf */
2475 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2477 /* Step 4: clear address list */
2478 #ifdef CONFIG_IPV6_PRIVACY
2479 if (how == 1 && del_timer(&idev->regen_timer))
2482 /* clear tempaddr list */
2483 while ((ifa = idev->tempaddr_list) != NULL) {
2484 idev->tempaddr_list = ifa->tmp_next;
2485 ifa->tmp_next = NULL;
2487 write_unlock_bh(&idev->lock);
2488 spin_lock_bh(&ifa->lock);
2491 in6_ifa_put(ifa->ifpub);
2494 spin_unlock_bh(&ifa->lock);
2496 write_lock_bh(&idev->lock);
2499 while ((ifa = idev->addr_list) != NULL) {
2500 idev->addr_list = ifa->if_next;
2501 ifa->if_next = NULL;
2503 addrconf_del_timer(ifa);
2504 write_unlock_bh(&idev->lock);
2506 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2507 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
2510 write_lock_bh(&idev->lock);
2512 write_unlock_bh(&idev->lock);
2514 /* Step 5: Discard multicast list */
2517 ipv6_mc_destroy_dev(idev);
2521 idev->tstamp = jiffies;
2523 /* Shot the device (if unregistered) */
2526 #ifdef CONFIG_SYSCTL
2527 addrconf_sysctl_unregister(&idev->cnf);
2528 neigh_sysctl_unregister(idev->nd_parms);
2530 neigh_parms_release(&nd_tbl, idev->nd_parms);
2531 neigh_ifdown(&nd_tbl, dev);
2537 static void addrconf_rs_timer(unsigned long data)
2539 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2541 if (ifp->idev->cnf.forwarding)
2544 if (ifp->idev->if_flags & IF_RA_RCVD) {
2546 * Announcement received after solicitation
2552 spin_lock(&ifp->lock);
2553 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2554 struct in6_addr all_routers;
2556 /* The wait after the last probe can be shorter */
2557 addrconf_mod_timer(ifp, AC_RS,
2558 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2559 ifp->idev->cnf.rtr_solicit_delay :
2560 ifp->idev->cnf.rtr_solicit_interval);
2561 spin_unlock(&ifp->lock);
2563 ipv6_addr_all_routers(&all_routers);
2565 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2567 spin_unlock(&ifp->lock);
2569 * Note: we do not support deprecated "all on-link"
2570 * assumption any longer.
2572 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2573 ifp->idev->dev->name);
2581 * Duplicate Address Detection
2583 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2585 unsigned long rand_num;
2586 struct inet6_dev *idev = ifp->idev;
2588 if (ifp->flags & IFA_F_OPTIMISTIC)
2591 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2593 ifp->probes = idev->cnf.dad_transmits;
2594 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2597 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2599 struct inet6_dev *idev = ifp->idev;
2600 struct net_device *dev = idev->dev;
2602 addrconf_join_solict(dev, &ifp->addr);
2604 net_srandom(ifp->addr.s6_addr32[3]);
2606 read_lock_bh(&idev->lock);
2609 spin_lock_bh(&ifp->lock);
2611 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2612 !(ifp->flags&IFA_F_TENTATIVE) ||
2613 ifp->flags & IFA_F_NODAD) {
2614 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
2615 spin_unlock_bh(&ifp->lock);
2616 read_unlock_bh(&idev->lock);
2618 addrconf_dad_completed(ifp);
2622 if (!(idev->if_flags & IF_READY)) {
2623 spin_unlock_bh(&ifp->lock);
2624 read_unlock_bh(&idev->lock);
2626 * If the defice is not ready:
2627 * - keep it tentative if it is a permanent address.
2628 * - otherwise, kill it.
2631 addrconf_dad_stop(ifp);
2636 * Optimistic nodes can start receiving
2639 if(ifp->flags & IFA_F_OPTIMISTIC)
2640 ip6_ins_rt(ifp->rt);
2642 addrconf_dad_kick(ifp);
2643 spin_unlock_bh(&ifp->lock);
2645 read_unlock_bh(&idev->lock);
2648 static void addrconf_dad_timer(unsigned long data)
2650 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2651 struct inet6_dev *idev = ifp->idev;
2652 struct in6_addr unspec;
2653 struct in6_addr mcaddr;
2655 read_lock_bh(&idev->lock);
2657 read_unlock_bh(&idev->lock);
2660 spin_lock_bh(&ifp->lock);
2661 if (ifp->probes == 0) {
2663 * DAD was successful
2666 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
2667 spin_unlock_bh(&ifp->lock);
2668 read_unlock_bh(&idev->lock);
2670 addrconf_dad_completed(ifp);
2676 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2677 spin_unlock_bh(&ifp->lock);
2678 read_unlock_bh(&idev->lock);
2680 /* send a neighbour solicitation for our addr */
2681 memset(&unspec, 0, sizeof(unspec));
2682 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2683 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2688 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2690 struct net_device * dev = ifp->idev->dev;
2693 * Configure the address for reception. Now it is valid.
2696 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2698 /* If added prefix is link local and forwarding is off,
2699 start sending router solicitations.
2702 if (ifp->idev->cnf.forwarding == 0 &&
2703 ifp->idev->cnf.rtr_solicits > 0 &&
2704 (dev->flags&IFF_LOOPBACK) == 0 &&
2705 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2706 struct in6_addr all_routers;
2708 ipv6_addr_all_routers(&all_routers);
2711 * If a host as already performed a random delay
2712 * [...] as part of DAD [...] there is no need
2713 * to delay again before sending the first RS
2715 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2717 spin_lock_bh(&ifp->lock);
2719 ifp->idev->if_flags |= IF_RS_SENT;
2720 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2721 spin_unlock_bh(&ifp->lock);
2725 static void addrconf_dad_run(struct inet6_dev *idev) {
2726 struct inet6_ifaddr *ifp;
2728 read_lock_bh(&idev->lock);
2729 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2730 spin_lock_bh(&ifp->lock);
2731 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2732 spin_unlock_bh(&ifp->lock);
2735 spin_unlock_bh(&ifp->lock);
2736 addrconf_dad_kick(ifp);
2738 read_unlock_bh(&idev->lock);
2741 #ifdef CONFIG_PROC_FS
2742 struct if6_iter_state {
2746 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2748 struct inet6_ifaddr *ifa = NULL;
2749 struct if6_iter_state *state = seq->private;
2751 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2752 ifa = inet6_addr_lst[state->bucket];
2759 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2761 struct if6_iter_state *state = seq->private;
2763 ifa = ifa->lst_next;
2765 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2766 ifa = inet6_addr_lst[state->bucket];
2772 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2774 struct inet6_ifaddr *ifa = if6_get_first(seq);
2777 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2779 return pos ? NULL : ifa;
2782 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2784 read_lock_bh(&addrconf_hash_lock);
2785 return if6_get_idx(seq, *pos);
2788 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2790 struct inet6_ifaddr *ifa;
2792 ifa = if6_get_next(seq, v);
2797 static void if6_seq_stop(struct seq_file *seq, void *v)
2799 read_unlock_bh(&addrconf_hash_lock);
2802 static int if6_seq_show(struct seq_file *seq, void *v)
2804 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2806 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
2808 ifp->idev->dev->ifindex,
2812 ifp->idev->dev->name);
2816 static const struct seq_operations if6_seq_ops = {
2817 .start = if6_seq_start,
2818 .next = if6_seq_next,
2819 .show = if6_seq_show,
2820 .stop = if6_seq_stop,
2823 static int if6_seq_open(struct inode *inode, struct file *file)
2825 return seq_open_private(file, &if6_seq_ops,
2826 sizeof(struct if6_iter_state));
2829 static const struct file_operations if6_fops = {
2830 .owner = THIS_MODULE,
2831 .open = if6_seq_open,
2833 .llseek = seq_lseek,
2834 .release = seq_release_private,
2837 int __init if6_proc_init(void)
2839 if (!proc_net_fops_create(&init_net, "if_inet6", S_IRUGO, &if6_fops))
2844 void if6_proc_exit(void)
2846 proc_net_remove(&init_net, "if_inet6");
2848 #endif /* CONFIG_PROC_FS */
2850 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
2851 /* Check if address is a home address configured on any interface. */
2852 int ipv6_chk_home_addr(struct in6_addr *addr)
2855 struct inet6_ifaddr * ifp;
2856 u8 hash = ipv6_addr_hash(addr);
2857 read_lock_bh(&addrconf_hash_lock);
2858 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
2859 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
2860 (ifp->flags & IFA_F_HOMEADDRESS)) {
2865 read_unlock_bh(&addrconf_hash_lock);
2871 * Periodic address status verification
2874 static void addrconf_verify(unsigned long foo)
2876 struct inet6_ifaddr *ifp;
2877 unsigned long now, next;
2880 spin_lock_bh(&addrconf_verify_lock);
2882 next = now + ADDR_CHECK_FREQUENCY;
2884 del_timer(&addr_chk_timer);
2886 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2889 read_lock(&addrconf_hash_lock);
2890 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2892 #ifdef CONFIG_IPV6_PRIVACY
2893 unsigned long regen_advance;
2896 if (ifp->flags & IFA_F_PERMANENT)
2899 spin_lock(&ifp->lock);
2900 age = (now - ifp->tstamp) / HZ;
2902 #ifdef CONFIG_IPV6_PRIVACY
2903 regen_advance = ifp->idev->cnf.regen_max_retry *
2904 ifp->idev->cnf.dad_transmits *
2905 ifp->idev->nd_parms->retrans_time / HZ;
2908 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2909 age >= ifp->valid_lft) {
2910 spin_unlock(&ifp->lock);
2912 read_unlock(&addrconf_hash_lock);
2915 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2916 spin_unlock(&ifp->lock);
2918 } else if (age >= ifp->prefered_lft) {
2919 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2922 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2924 ifp->flags |= IFA_F_DEPRECATED;
2927 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2928 next = ifp->tstamp + ifp->valid_lft * HZ;
2930 spin_unlock(&ifp->lock);
2934 read_unlock(&addrconf_hash_lock);
2936 ipv6_ifa_notify(0, ifp);
2940 #ifdef CONFIG_IPV6_PRIVACY
2941 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2942 !(ifp->flags&IFA_F_TENTATIVE)) {
2943 if (age >= ifp->prefered_lft - regen_advance) {
2944 struct inet6_ifaddr *ifpub = ifp->ifpub;
2945 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2946 next = ifp->tstamp + ifp->prefered_lft * HZ;
2947 if (!ifp->regen_count && ifpub) {
2950 in6_ifa_hold(ifpub);
2951 spin_unlock(&ifp->lock);
2952 read_unlock(&addrconf_hash_lock);
2953 spin_lock(&ifpub->lock);
2954 ifpub->regen_count = 0;
2955 spin_unlock(&ifpub->lock);
2956 ipv6_create_tempaddr(ifpub, ifp);
2961 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2962 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2963 spin_unlock(&ifp->lock);
2966 /* ifp->prefered_lft <= ifp->valid_lft */
2967 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2968 next = ifp->tstamp + ifp->prefered_lft * HZ;
2969 spin_unlock(&ifp->lock);
2972 read_unlock(&addrconf_hash_lock);
2975 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2976 add_timer(&addr_chk_timer);
2977 spin_unlock_bh(&addrconf_verify_lock);
2980 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2982 struct in6_addr *pfx = NULL;
2985 pfx = nla_data(addr);
2988 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2991 pfx = nla_data(local);
2997 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
2998 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
2999 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3000 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3004 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3006 struct net *net = skb->sk->sk_net;
3007 struct ifaddrmsg *ifm;
3008 struct nlattr *tb[IFA_MAX+1];
3009 struct in6_addr *pfx;
3012 if (net != &init_net)
3015 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3019 ifm = nlmsg_data(nlh);
3020 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3024 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
3027 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3028 u32 prefered_lft, u32 valid_lft)
3030 u32 flags = RTF_EXPIRES;
3032 if (!valid_lft || (prefered_lft > valid_lft))
3035 if (valid_lft == INFINITY_LIFE_TIME) {
3036 ifa_flags |= IFA_F_PERMANENT;
3038 } else if (valid_lft >= 0x7FFFFFFF/HZ)
3039 valid_lft = 0x7FFFFFFF/HZ;
3041 if (prefered_lft == 0)
3042 ifa_flags |= IFA_F_DEPRECATED;
3043 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
3044 (prefered_lft != INFINITY_LIFE_TIME))
3045 prefered_lft = 0x7FFFFFFF/HZ;
3047 spin_lock_bh(&ifp->lock);
3048 ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3049 ifp->tstamp = jiffies;
3050 ifp->valid_lft = valid_lft;
3051 ifp->prefered_lft = prefered_lft;
3053 spin_unlock_bh(&ifp->lock);
3054 if (!(ifp->flags&IFA_F_TENTATIVE))
3055 ipv6_ifa_notify(0, ifp);
3057 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3058 jiffies_to_clock_t(valid_lft * HZ), flags);
3065 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3067 struct net *net = skb->sk->sk_net;
3068 struct ifaddrmsg *ifm;
3069 struct nlattr *tb[IFA_MAX+1];
3070 struct in6_addr *pfx;
3071 struct inet6_ifaddr *ifa;
3072 struct net_device *dev;
3073 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3077 if (net != &init_net)
3080 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3084 ifm = nlmsg_data(nlh);
3085 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3089 if (tb[IFA_CACHEINFO]) {
3090 struct ifa_cacheinfo *ci;
3092 ci = nla_data(tb[IFA_CACHEINFO]);
3093 valid_lft = ci->ifa_valid;
3094 preferred_lft = ci->ifa_prefered;
3096 preferred_lft = INFINITY_LIFE_TIME;
3097 valid_lft = INFINITY_LIFE_TIME;
3100 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
3104 /* We ignore other flags so far. */
3105 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
3107 ifa = ipv6_get_ifaddr(pfx, dev, 1);
3110 * It would be best to check for !NLM_F_CREATE here but
3111 * userspace alreay relies on not having to provide this.
3113 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
3114 ifa_flags, preferred_lft, valid_lft);
3117 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3118 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3121 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
3128 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3129 u8 scope, int ifindex)
3131 struct ifaddrmsg *ifm;
3133 ifm = nlmsg_data(nlh);
3134 ifm->ifa_family = AF_INET6;
3135 ifm->ifa_prefixlen = prefixlen;
3136 ifm->ifa_flags = flags;
3137 ifm->ifa_scope = scope;
3138 ifm->ifa_index = ifindex;
3141 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3142 unsigned long tstamp, u32 preferred, u32 valid)
3144 struct ifa_cacheinfo ci;
3146 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3147 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3148 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3149 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3150 ci.ifa_prefered = preferred;
3151 ci.ifa_valid = valid;
3153 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3156 static inline int rt_scope(int ifa_scope)
3158 if (ifa_scope & IFA_HOST)
3159 return RT_SCOPE_HOST;
3160 else if (ifa_scope & IFA_LINK)
3161 return RT_SCOPE_LINK;
3162 else if (ifa_scope & IFA_SITE)
3163 return RT_SCOPE_SITE;
3165 return RT_SCOPE_UNIVERSE;
3168 static inline int inet6_ifaddr_msgsize(void)
3170 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3171 + nla_total_size(16) /* IFA_ADDRESS */
3172 + nla_total_size(sizeof(struct ifa_cacheinfo));
3175 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3176 u32 pid, u32 seq, int event, unsigned int flags)
3178 struct nlmsghdr *nlh;
3179 u32 preferred, valid;
3181 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3185 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3186 ifa->idev->dev->ifindex);
3188 if (!(ifa->flags&IFA_F_PERMANENT)) {
3189 preferred = ifa->prefered_lft;
3190 valid = ifa->valid_lft;
3191 if (preferred != INFINITY_LIFE_TIME) {
3192 long tval = (jiffies - ifa->tstamp)/HZ;
3194 if (valid != INFINITY_LIFE_TIME)
3198 preferred = INFINITY_LIFE_TIME;
3199 valid = INFINITY_LIFE_TIME;
3202 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
3203 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3204 nlmsg_cancel(skb, nlh);
3208 return nlmsg_end(skb, nlh);
3211 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3212 u32 pid, u32 seq, int event, u16 flags)
3214 struct nlmsghdr *nlh;
3215 u8 scope = RT_SCOPE_UNIVERSE;
3216 int ifindex = ifmca->idev->dev->ifindex;
3218 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3219 scope = RT_SCOPE_SITE;
3221 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3225 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3226 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3227 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
3228 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3229 nlmsg_cancel(skb, nlh);
3233 return nlmsg_end(skb, nlh);
3236 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3237 u32 pid, u32 seq, int event, unsigned int flags)
3239 struct nlmsghdr *nlh;
3240 u8 scope = RT_SCOPE_UNIVERSE;
3241 int ifindex = ifaca->aca_idev->dev->ifindex;
3243 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3244 scope = RT_SCOPE_SITE;
3246 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3250 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3251 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3252 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
3253 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3254 nlmsg_cancel(skb, nlh);
3258 return nlmsg_end(skb, nlh);
3268 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3269 enum addr_type_t type)
3272 int s_idx, s_ip_idx;
3274 struct net_device *dev;
3275 struct inet6_dev *idev = NULL;
3276 struct inet6_ifaddr *ifa;
3277 struct ifmcaddr6 *ifmca;
3278 struct ifacaddr6 *ifaca;
3280 s_idx = cb->args[0];
3281 s_ip_idx = ip_idx = cb->args[1];
3284 for_each_netdev(&init_net, dev) {
3290 if ((idev = in6_dev_get(dev)) == NULL)
3292 read_lock_bh(&idev->lock);
3295 /* unicast address incl. temp addr */
3296 for (ifa = idev->addr_list; ifa;
3297 ifa = ifa->if_next, ip_idx++) {
3298 if (ip_idx < s_ip_idx)
3300 if ((err = inet6_fill_ifaddr(skb, ifa,
3301 NETLINK_CB(cb->skb).pid,
3302 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3307 case MULTICAST_ADDR:
3308 /* multicast address */
3309 for (ifmca = idev->mc_list; ifmca;
3310 ifmca = ifmca->next, ip_idx++) {
3311 if (ip_idx < s_ip_idx)
3313 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3314 NETLINK_CB(cb->skb).pid,
3315 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3321 /* anycast address */
3322 for (ifaca = idev->ac_list; ifaca;
3323 ifaca = ifaca->aca_next, ip_idx++) {
3324 if (ip_idx < s_ip_idx)
3326 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3327 NETLINK_CB(cb->skb).pid,
3328 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3336 read_unlock_bh(&idev->lock);
3343 read_unlock_bh(&idev->lock);
3347 cb->args[1] = ip_idx;
3351 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3353 struct net *net = skb->sk->sk_net;
3354 enum addr_type_t type = UNICAST_ADDR;
3356 if (net != &init_net)
3359 return inet6_dump_addr(skb, cb, type);
3362 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3364 struct net *net = skb->sk->sk_net;
3365 enum addr_type_t type = MULTICAST_ADDR;
3367 if (net != &init_net)
3370 return inet6_dump_addr(skb, cb, type);
3374 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3376 struct net *net = skb->sk->sk_net;
3377 enum addr_type_t type = ANYCAST_ADDR;
3379 if (net != &init_net)
3382 return inet6_dump_addr(skb, cb, type);
3385 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3388 struct net *net = in_skb->sk->sk_net;
3389 struct ifaddrmsg *ifm;
3390 struct nlattr *tb[IFA_MAX+1];
3391 struct in6_addr *addr = NULL;
3392 struct net_device *dev = NULL;
3393 struct inet6_ifaddr *ifa;
3394 struct sk_buff *skb;
3397 if (net != &init_net)
3400 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3404 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3410 ifm = nlmsg_data(nlh);
3412 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
3414 if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
3415 err = -EADDRNOTAVAIL;
3419 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
3424 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3425 nlh->nlmsg_seq, RTM_NEWADDR, 0);
3427 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3428 WARN_ON(err == -EMSGSIZE);
3432 err = rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
3439 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3441 struct sk_buff *skb;
3444 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
3448 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
3450 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3451 WARN_ON(err == -EMSGSIZE);
3455 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3458 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
3461 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
3462 __s32 *array, int bytes)
3464 BUG_ON(bytes < (DEVCONF_MAX * 4));
3466 memset(array, 0, bytes);
3467 array[DEVCONF_FORWARDING] = cnf->forwarding;
3468 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3469 array[DEVCONF_MTU6] = cnf->mtu6;
3470 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3471 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3472 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3473 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3474 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3475 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3476 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3477 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3478 #ifdef CONFIG_IPV6_PRIVACY
3479 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3480 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3481 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3482 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3483 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3485 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3486 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3487 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3488 #ifdef CONFIG_IPV6_ROUTER_PREF
3489 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
3490 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
3491 #ifdef CONFIG_IPV6_ROUTE_INFO
3492 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3495 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
3496 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
3497 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3498 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
3502 static inline size_t inet6_if_nlmsg_size(void)
3504 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3505 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3506 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3507 + nla_total_size(4) /* IFLA_MTU */
3508 + nla_total_size(4) /* IFLA_LINK */
3509 + nla_total_size( /* IFLA_PROTINFO */
3510 nla_total_size(4) /* IFLA_INET6_FLAGS */
3511 + nla_total_size(sizeof(struct ifla_cacheinfo))
3512 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3513 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3514 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
3518 static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
3522 int pad = bytes - sizeof(u64) * items;
3525 /* Use put_unaligned() because stats may not be aligned for u64. */
3526 put_unaligned(items, &stats[0]);
3527 for (i = 1; i < items; i++)
3528 put_unaligned(snmp_fold_field(mib, i), &stats[i]);
3530 memset(&stats[items], 0, pad);
3533 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
3537 case IFLA_INET6_STATS:
3538 __snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
3540 case IFLA_INET6_ICMP6STATS:
3541 __snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
3546 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3547 u32 pid, u32 seq, int event, unsigned int flags)
3549 struct net_device *dev = idev->dev;
3551 struct ifinfomsg *hdr;
3552 struct nlmsghdr *nlh;
3554 struct ifla_cacheinfo ci;
3556 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3560 hdr = nlmsg_data(nlh);
3561 hdr->ifi_family = AF_INET6;
3563 hdr->ifi_type = dev->type;
3564 hdr->ifi_index = dev->ifindex;
3565 hdr->ifi_flags = dev_get_flags(dev);
3566 hdr->ifi_change = 0;
3568 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
3571 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3573 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
3574 if (dev->ifindex != dev->iflink)
3575 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
3577 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3578 if (protoinfo == NULL)
3579 goto nla_put_failure;
3581 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
3583 ci.max_reasm_len = IPV6_MAXPLEN;
3584 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3585 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3586 ci.reachable_time = idev->nd_parms->reachable_time;
3587 ci.retrans_time = idev->nd_parms->retrans_time;
3588 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3590 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3592 goto nla_put_failure;
3593 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
3595 /* XXX - MC not implemented */
3597 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3599 goto nla_put_failure;
3600 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3602 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3604 goto nla_put_failure;
3605 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
3607 nla_nest_end(skb, protoinfo);
3608 return nlmsg_end(skb, nlh);
3611 nlmsg_cancel(skb, nlh);
3615 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3617 struct net *net = skb->sk->sk_net;
3619 int s_idx = cb->args[0];
3620 struct net_device *dev;
3621 struct inet6_dev *idev;
3623 if (net != &init_net)
3626 read_lock(&dev_base_lock);
3628 for_each_netdev(&init_net, dev) {
3631 if ((idev = in6_dev_get(dev)) == NULL)
3633 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3634 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3641 read_unlock(&dev_base_lock);
3647 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3649 struct sk_buff *skb;
3652 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
3656 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
3658 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3659 WARN_ON(err == -EMSGSIZE);
3663 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3666 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
3669 static inline size_t inet6_prefix_nlmsg_size(void)
3671 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3672 + nla_total_size(sizeof(struct in6_addr))
3673 + nla_total_size(sizeof(struct prefix_cacheinfo));
3676 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3677 struct prefix_info *pinfo, u32 pid, u32 seq,
3678 int event, unsigned int flags)
3680 struct prefixmsg *pmsg;
3681 struct nlmsghdr *nlh;
3682 struct prefix_cacheinfo ci;
3684 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3688 pmsg = nlmsg_data(nlh);
3689 pmsg->prefix_family = AF_INET6;
3690 pmsg->prefix_pad1 = 0;
3691 pmsg->prefix_pad2 = 0;
3692 pmsg->prefix_ifindex = idev->dev->ifindex;
3693 pmsg->prefix_len = pinfo->prefix_len;
3694 pmsg->prefix_type = pinfo->type;
3695 pmsg->prefix_pad3 = 0;
3696 pmsg->prefix_flags = 0;
3698 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3699 if (pinfo->autoconf)
3700 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3702 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3704 ci.preferred_time = ntohl(pinfo->prefered);
3705 ci.valid_time = ntohl(pinfo->valid);
3706 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3708 return nlmsg_end(skb, nlh);
3711 nlmsg_cancel(skb, nlh);
3715 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3716 struct prefix_info *pinfo)
3718 struct sk_buff *skb;
3721 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
3725 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
3727 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3728 WARN_ON(err == -EMSGSIZE);
3732 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3735 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_PREFIX, err);
3738 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3740 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3745 * If the address was optimistic
3746 * we inserted the route at the start of
3747 * our DAD process, so we don't need
3750 if (!(ifp->rt->rt6i_node))
3751 ip6_ins_rt(ifp->rt);
3752 if (ifp->idev->cnf.forwarding)
3753 addrconf_join_anycast(ifp);
3756 if (ifp->idev->cnf.forwarding)
3757 addrconf_leave_anycast(ifp);
3758 addrconf_leave_solict(ifp->idev, &ifp->addr);
3759 dst_hold(&ifp->rt->u.dst);
3760 if (ip6_del_rt(ifp->rt))
3761 dst_free(&ifp->rt->u.dst);
3766 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3769 if (likely(ifp->idev->dead == 0))
3770 __ipv6_ifa_notify(event, ifp);
3771 rcu_read_unlock_bh();
3774 #ifdef CONFIG_SYSCTL
3777 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3778 void __user *buffer, size_t *lenp, loff_t *ppos)
3780 int *valp = ctl->data;
3784 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3787 addrconf_fixup_forwarding(ctl, valp, val);
3791 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3792 int __user *name, int nlen,
3793 void __user *oldval,
3794 size_t __user *oldlenp,
3795 void __user *newval, size_t newlen)
3797 int *valp = table->data;
3801 if (!newval || !newlen)
3803 if (newlen != sizeof(int))
3805 if (get_user(new, (int __user *)newval))
3809 if (oldval && oldlenp) {
3811 if (get_user(len, oldlenp))
3814 if (len > table->maxlen)
3815 len = table->maxlen;
3816 if (copy_to_user(oldval, valp, len))
3818 if (put_user(len, oldlenp))
3824 addrconf_fixup_forwarding(table, valp, val);
3828 static struct addrconf_sysctl_table
3830 struct ctl_table_header *sysctl_header;
3831 ctl_table addrconf_vars[__NET_IPV6_MAX];
3833 } addrconf_sysctl __read_mostly = {
3834 .sysctl_header = NULL,
3837 .ctl_name = NET_IPV6_FORWARDING,
3838 .procname = "forwarding",
3839 .data = &ipv6_devconf.forwarding,
3840 .maxlen = sizeof(int),
3842 .proc_handler = &addrconf_sysctl_forward,
3843 .strategy = &addrconf_sysctl_forward_strategy,
3846 .ctl_name = NET_IPV6_HOP_LIMIT,
3847 .procname = "hop_limit",
3848 .data = &ipv6_devconf.hop_limit,
3849 .maxlen = sizeof(int),
3851 .proc_handler = proc_dointvec,
3854 .ctl_name = NET_IPV6_MTU,
3856 .data = &ipv6_devconf.mtu6,
3857 .maxlen = sizeof(int),
3859 .proc_handler = &proc_dointvec,
3862 .ctl_name = NET_IPV6_ACCEPT_RA,
3863 .procname = "accept_ra",
3864 .data = &ipv6_devconf.accept_ra,
3865 .maxlen = sizeof(int),
3867 .proc_handler = &proc_dointvec,
3870 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3871 .procname = "accept_redirects",
3872 .data = &ipv6_devconf.accept_redirects,
3873 .maxlen = sizeof(int),
3875 .proc_handler = &proc_dointvec,
3878 .ctl_name = NET_IPV6_AUTOCONF,
3879 .procname = "autoconf",
3880 .data = &ipv6_devconf.autoconf,
3881 .maxlen = sizeof(int),
3883 .proc_handler = &proc_dointvec,
3886 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3887 .procname = "dad_transmits",
3888 .data = &ipv6_devconf.dad_transmits,
3889 .maxlen = sizeof(int),
3891 .proc_handler = &proc_dointvec,
3894 .ctl_name = NET_IPV6_RTR_SOLICITS,
3895 .procname = "router_solicitations",
3896 .data = &ipv6_devconf.rtr_solicits,
3897 .maxlen = sizeof(int),
3899 .proc_handler = &proc_dointvec,
3902 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3903 .procname = "router_solicitation_interval",
3904 .data = &ipv6_devconf.rtr_solicit_interval,
3905 .maxlen = sizeof(int),
3907 .proc_handler = &proc_dointvec_jiffies,
3908 .strategy = &sysctl_jiffies,
3911 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3912 .procname = "router_solicitation_delay",
3913 .data = &ipv6_devconf.rtr_solicit_delay,
3914 .maxlen = sizeof(int),
3916 .proc_handler = &proc_dointvec_jiffies,
3917 .strategy = &sysctl_jiffies,
3920 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3921 .procname = "force_mld_version",
3922 .data = &ipv6_devconf.force_mld_version,
3923 .maxlen = sizeof(int),
3925 .proc_handler = &proc_dointvec,
3927 #ifdef CONFIG_IPV6_PRIVACY
3929 .ctl_name = NET_IPV6_USE_TEMPADDR,
3930 .procname = "use_tempaddr",
3931 .data = &ipv6_devconf.use_tempaddr,
3932 .maxlen = sizeof(int),
3934 .proc_handler = &proc_dointvec,
3937 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3938 .procname = "temp_valid_lft",
3939 .data = &ipv6_devconf.temp_valid_lft,
3940 .maxlen = sizeof(int),
3942 .proc_handler = &proc_dointvec,
3945 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3946 .procname = "temp_prefered_lft",
3947 .data = &ipv6_devconf.temp_prefered_lft,
3948 .maxlen = sizeof(int),
3950 .proc_handler = &proc_dointvec,
3953 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3954 .procname = "regen_max_retry",
3955 .data = &ipv6_devconf.regen_max_retry,
3956 .maxlen = sizeof(int),
3958 .proc_handler = &proc_dointvec,
3961 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3962 .procname = "max_desync_factor",
3963 .data = &ipv6_devconf.max_desync_factor,
3964 .maxlen = sizeof(int),
3966 .proc_handler = &proc_dointvec,
3970 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3971 .procname = "max_addresses",
3972 .data = &ipv6_devconf.max_addresses,
3973 .maxlen = sizeof(int),
3975 .proc_handler = &proc_dointvec,
3978 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
3979 .procname = "accept_ra_defrtr",
3980 .data = &ipv6_devconf.accept_ra_defrtr,
3981 .maxlen = sizeof(int),
3983 .proc_handler = &proc_dointvec,
3986 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
3987 .procname = "accept_ra_pinfo",
3988 .data = &ipv6_devconf.accept_ra_pinfo,
3989 .maxlen = sizeof(int),
3991 .proc_handler = &proc_dointvec,
3993 #ifdef CONFIG_IPV6_ROUTER_PREF
3995 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
3996 .procname = "accept_ra_rtr_pref",
3997 .data = &ipv6_devconf.accept_ra_rtr_pref,
3998 .maxlen = sizeof(int),
4000 .proc_handler = &proc_dointvec,
4003 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
4004 .procname = "router_probe_interval",
4005 .data = &ipv6_devconf.rtr_probe_interval,
4006 .maxlen = sizeof(int),
4008 .proc_handler = &proc_dointvec_jiffies,
4009 .strategy = &sysctl_jiffies,
4011 #ifdef CONFIG_IPV6_ROUTE_INFO
4013 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
4014 .procname = "accept_ra_rt_info_max_plen",
4015 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4016 .maxlen = sizeof(int),
4018 .proc_handler = &proc_dointvec,
4023 .ctl_name = NET_IPV6_PROXY_NDP,
4024 .procname = "proxy_ndp",
4025 .data = &ipv6_devconf.proxy_ndp,
4026 .maxlen = sizeof(int),
4028 .proc_handler = &proc_dointvec,
4031 .ctl_name = NET_IPV6_ACCEPT_SOURCE_ROUTE,
4032 .procname = "accept_source_route",
4033 .data = &ipv6_devconf.accept_source_route,
4034 .maxlen = sizeof(int),
4036 .proc_handler = &proc_dointvec,
4038 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4040 .ctl_name = CTL_UNNUMBERED,
4041 .procname = "optimistic_dad",
4042 .data = &ipv6_devconf.optimistic_dad,
4043 .maxlen = sizeof(int),
4045 .proc_handler = &proc_dointvec,
4050 .ctl_name = 0, /* sentinel */
4055 static void __addrconf_sysctl_register(char *dev_name, int ctl_name,
4056 struct inet6_dev *idev, struct ipv6_devconf *p)
4059 struct addrconf_sysctl_table *t;
4061 #define ADDRCONF_CTL_PATH_DEV 3
4063 struct ctl_path addrconf_ctl_path[] = {
4064 { .procname = "net", .ctl_name = CTL_NET, },
4065 { .procname = "ipv6", .ctl_name = NET_IPV6, },
4066 { .procname = "conf", .ctl_name = NET_IPV6_CONF, },
4067 { /* to be set */ },
4072 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
4076 for (i=0; t->addrconf_vars[i].data; i++) {
4077 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
4078 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4082 * Make a copy of dev_name, because '.procname' is regarded as const
4083 * by sysctl and we wouldn't want anyone to change it under our feet
4084 * (see SIOCSIFNAME).
4086 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
4090 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
4091 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name;
4093 t->sysctl_header = register_sysctl_paths(addrconf_ctl_path,
4095 if (t->sysctl_header == NULL)
4109 static void addrconf_sysctl_register(struct inet6_dev *idev)
4111 __addrconf_sysctl_register(idev->dev->name, idev->dev->ifindex,
4115 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
4118 struct addrconf_sysctl_table *t = p->sysctl;
4120 unregister_sysctl_table(t->sysctl_header);
4133 int register_inet6addr_notifier(struct notifier_block *nb)
4135 return atomic_notifier_chain_register(&inet6addr_chain, nb);
4138 EXPORT_SYMBOL(register_inet6addr_notifier);
4140 int unregister_inet6addr_notifier(struct notifier_block *nb)
4142 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
4145 EXPORT_SYMBOL(unregister_inet6addr_notifier);
4148 * Init / cleanup code
4151 int __init addrconf_init(void)
4155 if ((err = ipv6_addr_label_init()) < 0) {
4156 printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
4161 /* The addrconf netdev notifier requires that loopback_dev
4162 * has it's ipv6 private information allocated and setup
4163 * before it can bring up and give link-local addresses
4164 * to other devices which are up.
4166 * Unfortunately, loopback_dev is not necessarily the first
4167 * entry in the global dev_base list of net devices. In fact,
4168 * it is likely to be the very last entry on that list.
4169 * So this causes the notifier registry below to try and
4170 * give link-local addresses to all devices besides loopback_dev
4171 * first, then loopback_dev, which cases all the non-loopback_dev
4172 * devices to fail to get a link-local address.
4174 * So, as a temporary fix, allocate the ipv6 structure for
4175 * loopback_dev first by hand.
4176 * Longer term, all of the dependencies ipv6 has upon the loopback
4177 * device and it being up should be removed.
4180 if (!ipv6_add_dev(init_net.loopback_dev))
4186 ip6_null_entry.u.dst.dev = init_net.loopback_dev;
4187 ip6_null_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
4188 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
4189 ip6_prohibit_entry.u.dst.dev = init_net.loopback_dev;
4190 ip6_prohibit_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
4191 ip6_blk_hole_entry.u.dst.dev = init_net.loopback_dev;
4192 ip6_blk_hole_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
4195 register_netdevice_notifier(&ipv6_dev_notf);
4199 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4203 /* Only the first call to __rtnl_register can fail */
4204 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4205 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4206 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4207 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4208 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4210 ipv6_addr_label_rtnl_register();
4212 #ifdef CONFIG_SYSCTL
4213 __addrconf_sysctl_register("all", NET_PROTO_CONF_ALL,
4214 NULL, &ipv6_devconf);
4215 __addrconf_sysctl_register("default", NET_PROTO_CONF_DEFAULT,
4216 NULL, &ipv6_devconf_dflt);
4221 unregister_netdevice_notifier(&ipv6_dev_notf);
4226 void __exit addrconf_cleanup(void)
4228 struct net_device *dev;
4229 struct inet6_ifaddr *ifa;
4232 unregister_netdevice_notifier(&ipv6_dev_notf);
4234 #ifdef CONFIG_SYSCTL
4235 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
4236 addrconf_sysctl_unregister(&ipv6_devconf);
4245 for_each_netdev(&init_net, dev) {
4246 if (__in6_dev_get(dev) == NULL)
4248 addrconf_ifdown(dev, 1);
4250 addrconf_ifdown(init_net.loopback_dev, 2);
4256 write_lock_bh(&addrconf_hash_lock);
4257 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4258 for (ifa=inet6_addr_lst[i]; ifa; ) {
4259 struct inet6_ifaddr *bifa;
4262 ifa = ifa->lst_next;
4263 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4264 /* Do not free it; something is wrong.
4265 Now we can investigate it with debugger.
4269 write_unlock_bh(&addrconf_hash_lock);
4271 del_timer(&addr_chk_timer);