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, struct ipv6_devconf *p);
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),
260 __alignof__(struct ipstats_mib)) < 0)
262 if (snmp_mib_init((void **)idev->stats.icmpv6,
263 sizeof(struct icmpv6_mib),
264 __alignof__(struct icmpv6_mib)) < 0)
266 if (snmp_mib_init((void **)idev->stats.icmpv6msg,
267 sizeof(struct icmpv6msg_mib),
268 __alignof__(struct icmpv6msg_mib)) < 0)
274 snmp_mib_free((void **)idev->stats.icmpv6);
276 snmp_mib_free((void **)idev->stats.ipv6);
281 static void snmp6_free_dev(struct inet6_dev *idev)
283 snmp_mib_free((void **)idev->stats.icmpv6msg);
284 snmp_mib_free((void **)idev->stats.icmpv6);
285 snmp_mib_free((void **)idev->stats.ipv6);
288 /* Nobody refers to this device, we may destroy it. */
290 static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
292 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
296 void in6_dev_finish_destroy(struct inet6_dev *idev)
298 struct net_device *dev = idev->dev;
299 BUG_TRAP(idev->addr_list==NULL);
300 BUG_TRAP(idev->mc_list==NULL);
301 #ifdef NET_REFCNT_DEBUG
302 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
306 printk("Freeing alive inet6 device %p\n", idev);
309 snmp6_free_dev(idev);
310 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
313 EXPORT_SYMBOL(in6_dev_finish_destroy);
315 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
317 struct inet6_dev *ndev;
318 struct in6_addr maddr;
322 if (dev->mtu < IPV6_MIN_MTU)
325 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
330 rwlock_init(&ndev->lock);
332 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
333 ndev->cnf.mtu6 = dev->mtu;
334 ndev->cnf.sysctl = NULL;
335 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
336 if (ndev->nd_parms == NULL) {
340 /* We refer to the device */
343 if (snmp6_alloc_dev(ndev) < 0) {
345 "%s(): cannot allocate memory for statistics; dev=%s.\n",
346 __FUNCTION__, dev->name));
347 neigh_parms_release(&nd_tbl, ndev->nd_parms);
349 in6_dev_finish_destroy(ndev);
353 if (snmp6_register_dev(ndev) < 0) {
355 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
356 __FUNCTION__, dev->name));
357 neigh_parms_release(&nd_tbl, ndev->nd_parms);
359 in6_dev_finish_destroy(ndev);
363 /* One reference from device. We must do this before
364 * we invoke __ipv6_regen_rndid().
368 #ifdef CONFIG_IPV6_PRIVACY
369 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
370 if ((dev->flags&IFF_LOOPBACK) ||
371 dev->type == ARPHRD_TUNNEL ||
372 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
373 dev->type == ARPHRD_SIT ||
375 dev->type == ARPHRD_NONE) {
377 "%s: Disabled Privacy Extensions\n",
379 ndev->cnf.use_tempaddr = -1;
381 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
383 "%s: Disabled Multicast RS\n",
385 ndev->cnf.rtr_solicits = 0;
389 ipv6_regen_rndid((unsigned long) ndev);
393 if (netif_running(dev) && addrconf_qdisc_ok(dev))
394 ndev->if_flags |= IF_READY;
396 ipv6_mc_init_dev(ndev);
397 ndev->tstamp = jiffies;
399 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
400 NET_IPV6_NEIGH, "ipv6",
401 &ndisc_ifinfo_sysctl_change,
403 addrconf_sysctl_register(ndev, &ndev->cnf);
405 /* protected by rtnl_lock */
406 rcu_assign_pointer(dev->ip6_ptr, ndev);
408 /* Join all-node multicast group */
409 ipv6_addr_all_nodes(&maddr);
410 ipv6_dev_mc_inc(dev, &maddr);
415 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
417 struct inet6_dev *idev;
421 if ((idev = __in6_dev_get(dev)) == NULL) {
422 if ((idev = ipv6_add_dev(dev)) == NULL)
426 if (dev->flags&IFF_UP)
432 static void dev_forward_change(struct inet6_dev *idev)
434 struct net_device *dev;
435 struct inet6_ifaddr *ifa;
436 struct in6_addr addr;
441 if (dev && (dev->flags & IFF_MULTICAST)) {
442 ipv6_addr_all_routers(&addr);
444 if (idev->cnf.forwarding)
445 ipv6_dev_mc_inc(dev, &addr);
447 ipv6_dev_mc_dec(dev, &addr);
449 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
450 if (ifa->flags&IFA_F_TENTATIVE)
452 if (idev->cnf.forwarding)
453 addrconf_join_anycast(ifa);
455 addrconf_leave_anycast(ifa);
460 static void addrconf_forward_change(void)
462 struct net_device *dev;
463 struct inet6_dev *idev;
465 read_lock(&dev_base_lock);
466 for_each_netdev(&init_net, dev) {
468 idev = __in6_dev_get(dev);
470 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
471 idev->cnf.forwarding = ipv6_devconf.forwarding;
473 dev_forward_change(idev);
477 read_unlock(&dev_base_lock);
481 /* Nobody refers to this ifaddr, destroy it */
483 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
485 BUG_TRAP(ifp->if_next==NULL);
486 BUG_TRAP(ifp->lst_next==NULL);
487 #ifdef NET_REFCNT_DEBUG
488 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
491 in6_dev_put(ifp->idev);
493 if (del_timer(&ifp->timer))
494 printk("Timer is still running, when freeing ifa=%p\n", ifp);
497 printk("Freeing alive inet6 address %p\n", ifp);
500 dst_release(&ifp->rt->u.dst);
506 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
508 struct inet6_ifaddr *ifa, **ifap;
509 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
512 * Each device address list is sorted in order of scope -
513 * global before linklocal.
515 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
516 ifap = &ifa->if_next) {
517 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
521 ifp->if_next = *ifap;
525 /* On success it returns ifp with increased reference count */
527 static struct inet6_ifaddr *
528 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
529 int scope, u32 flags)
531 struct inet6_ifaddr *ifa = NULL;
538 err = -ENODEV; /*XXX*/
542 write_lock(&addrconf_hash_lock);
544 /* Ignore adding duplicate addresses on an interface */
545 if (ipv6_chk_same_addr(addr, idev->dev)) {
546 ADBG(("ipv6_add_addr: already assigned\n"));
551 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
554 ADBG(("ipv6_add_addr: malloc failed\n"));
559 rt = addrconf_dst_alloc(idev, addr, 0);
565 ipv6_addr_copy(&ifa->addr, addr);
567 spin_lock_init(&ifa->lock);
568 init_timer(&ifa->timer);
569 ifa->timer.data = (unsigned long) ifa;
571 ifa->prefix_len = pfxlen;
572 ifa->flags = flags | IFA_F_TENTATIVE;
573 ifa->cstamp = ifa->tstamp = jiffies;
578 * part one of RFC 4429, section 3.3
579 * We should not configure an address as
580 * optimistic if we do not yet know the link
581 * layer address of our nexhop router
584 if (rt->rt6i_nexthop == NULL)
585 ifa->flags &= ~IFA_F_OPTIMISTIC;
592 /* Add to big hash table */
593 hash = ipv6_addr_hash(addr);
595 ifa->lst_next = inet6_addr_lst[hash];
596 inet6_addr_lst[hash] = ifa;
598 write_unlock(&addrconf_hash_lock);
600 write_lock(&idev->lock);
601 /* Add to inet6_dev unicast addr list. */
602 ipv6_link_dev_addr(idev, ifa);
604 #ifdef CONFIG_IPV6_PRIVACY
605 if (ifa->flags&IFA_F_TEMPORARY) {
606 ifa->tmp_next = idev->tempaddr_list;
607 idev->tempaddr_list = ifa;
613 write_unlock(&idev->lock);
615 rcu_read_unlock_bh();
617 if (likely(err == 0))
618 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
626 write_unlock(&addrconf_hash_lock);
630 /* This function wants to get referenced ifp and releases it before return */
632 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
634 struct inet6_ifaddr *ifa, **ifap;
635 struct inet6_dev *idev = ifp->idev;
637 int deleted = 0, onlink = 0;
638 unsigned long expires = jiffies;
640 hash = ipv6_addr_hash(&ifp->addr);
644 write_lock_bh(&addrconf_hash_lock);
645 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
646 ifap = &ifa->lst_next) {
648 *ifap = ifa->lst_next;
650 ifa->lst_next = NULL;
654 write_unlock_bh(&addrconf_hash_lock);
656 write_lock_bh(&idev->lock);
657 #ifdef CONFIG_IPV6_PRIVACY
658 if (ifp->flags&IFA_F_TEMPORARY) {
659 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
660 ifap = &ifa->tmp_next) {
662 *ifap = ifa->tmp_next;
664 in6_ifa_put(ifp->ifpub);
668 ifa->tmp_next = NULL;
675 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
677 *ifap = ifa->if_next;
680 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
684 } else if (ifp->flags & IFA_F_PERMANENT) {
685 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
687 if (ifa->flags & IFA_F_PERMANENT) {
692 unsigned long lifetime;
697 spin_lock(&ifa->lock);
698 lifetime = min_t(unsigned long,
699 ifa->valid_lft, 0x7fffffffUL/HZ);
700 if (time_before(expires,
701 ifa->tstamp + lifetime * HZ))
702 expires = ifa->tstamp + lifetime * HZ;
703 spin_unlock(&ifa->lock);
707 ifap = &ifa->if_next;
709 write_unlock_bh(&idev->lock);
711 ipv6_ifa_notify(RTM_DELADDR, ifp);
713 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
715 addrconf_del_timer(ifp);
718 * Purge or update corresponding prefix
720 * 1) we don't purge prefix here if address was not permanent.
721 * prefix is managed by its own lifetime.
722 * 2) if there're no addresses, delete prefix.
723 * 3) if there're still other permanent address(es),
724 * corresponding prefix is still permanent.
725 * 4) otherwise, update prefix lifetime to the
726 * longest valid lifetime among the corresponding
727 * addresses on the device.
728 * Note: subsequent RA will update lifetime.
732 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
733 struct in6_addr prefix;
736 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
737 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
739 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
743 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
744 rt->rt6i_expires = expires;
745 rt->rt6i_flags |= RTF_EXPIRES;
748 dst_release(&rt->u.dst);
754 #ifdef CONFIG_IPV6_PRIVACY
755 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
757 struct inet6_dev *idev = ifp->idev;
758 struct in6_addr addr, *tmpaddr;
759 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
765 write_lock(&idev->lock);
767 spin_lock_bh(&ift->lock);
768 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
769 spin_unlock_bh(&ift->lock);
776 if (idev->cnf.use_tempaddr <= 0) {
777 write_unlock(&idev->lock);
779 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
784 spin_lock_bh(&ifp->lock);
785 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
786 idev->cnf.use_tempaddr = -1; /*XXX*/
787 spin_unlock_bh(&ifp->lock);
788 write_unlock(&idev->lock);
790 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
796 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
797 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
798 spin_unlock_bh(&ifp->lock);
799 write_unlock(&idev->lock);
801 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
807 memcpy(&addr.s6_addr[8], idev->rndid, 8);
808 tmp_valid_lft = min_t(__u32,
810 idev->cnf.temp_valid_lft);
811 tmp_prefered_lft = min_t(__u32,
813 idev->cnf.temp_prefered_lft - desync_factor / HZ);
814 tmp_plen = ifp->prefix_len;
815 max_addresses = idev->cnf.max_addresses;
816 tmp_cstamp = ifp->cstamp;
817 tmp_tstamp = ifp->tstamp;
818 spin_unlock_bh(&ifp->lock);
820 write_unlock(&idev->lock);
822 addr_flags = IFA_F_TEMPORARY;
823 /* set in addrconf_prefix_rcv() */
824 if (ifp->flags & IFA_F_OPTIMISTIC)
825 addr_flags |= IFA_F_OPTIMISTIC;
827 ift = !max_addresses ||
828 ipv6_count_addresses(idev) < max_addresses ?
829 ipv6_add_addr(idev, &addr, tmp_plen,
830 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
832 if (!ift || IS_ERR(ift)) {
836 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
838 write_lock(&idev->lock);
842 spin_lock_bh(&ift->lock);
844 ift->valid_lft = tmp_valid_lft;
845 ift->prefered_lft = tmp_prefered_lft;
846 ift->cstamp = tmp_cstamp;
847 ift->tstamp = tmp_tstamp;
848 spin_unlock_bh(&ift->lock);
850 addrconf_dad_start(ift, 0);
859 * Choose an appropriate source address (RFC3484)
861 struct ipv6_saddr_score {
869 #define IPV6_SADDR_SCORE_LOCAL 0x0001
870 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
871 #define IPV6_SADDR_SCORE_HOA 0x0008
872 #define IPV6_SADDR_SCORE_OIF 0x0010
873 #define IPV6_SADDR_SCORE_LABEL 0x0020
874 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
876 static inline int ipv6_saddr_preferred(int type)
878 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
879 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
884 int ipv6_dev_get_saddr(struct net_device *daddr_dev,
885 struct in6_addr *daddr, struct in6_addr *saddr)
887 struct ipv6_saddr_score hiscore;
888 struct inet6_ifaddr *ifa_result = NULL;
889 int daddr_type = __ipv6_addr_type(daddr);
890 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
891 int daddr_ifindex = daddr_dev ? daddr_dev->ifindex : 0;
892 u32 daddr_label = ipv6_addr_label(daddr, daddr_type, daddr_ifindex);
893 struct net_device *dev;
895 memset(&hiscore, 0, sizeof(hiscore));
897 read_lock(&dev_base_lock);
900 for_each_netdev(&init_net, dev) {
901 struct inet6_dev *idev;
902 struct inet6_ifaddr *ifa;
904 /* Rule 0: Candidate Source Address (section 4)
905 * - multicast and link-local destination address,
906 * the set of candidate source address MUST only
907 * include addresses assigned to interfaces
908 * belonging to the same link as the outgoing
910 * (- For site-local destination addresses, the
911 * set of candidate source addresses MUST only
912 * include addresses assigned to interfaces
913 * belonging to the same site as the outgoing
916 if ((daddr_type & IPV6_ADDR_MULTICAST ||
917 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
918 daddr_dev && dev != daddr_dev)
921 idev = __in6_dev_get(dev);
925 read_lock_bh(&idev->lock);
926 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
927 struct ipv6_saddr_score score;
929 score.addr_type = __ipv6_addr_type(&ifa->addr);
932 * - Tentative Address (RFC2462 section 5.4)
933 * - A tentative address is not considered
934 * "assigned to an interface" in the traditional
935 * sense, unless it is also flagged as optimistic.
936 * - Candidate Source Address (section 4)
937 * - In any case, anycast addresses, multicast
938 * addresses, and the unspecified address MUST
939 * NOT be included in a candidate set.
941 if ((ifa->flags & IFA_F_TENTATIVE) &&
942 (!(ifa->flags & IFA_F_OPTIMISTIC)))
944 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
945 score.addr_type & IPV6_ADDR_MULTICAST)) {
946 LIMIT_NETDEBUG(KERN_DEBUG
947 "ADDRCONF: unspecified / multicast address "
948 "assigned as unicast address on %s",
958 if (ifa_result == NULL) {
959 /* record it if the first available entry */
963 /* Rule 1: Prefer same address */
964 if (hiscore.rule < 1) {
965 if (ipv6_addr_equal(&ifa_result->addr, daddr))
966 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
969 if (ipv6_addr_equal(&ifa->addr, daddr)) {
970 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
971 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
976 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
980 /* Rule 2: Prefer appropriate scope */
981 if (hiscore.rule < 2) {
982 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
985 score.scope = __ipv6_addr_src_scope(score.addr_type);
986 if (hiscore.scope < score.scope) {
987 if (hiscore.scope < daddr_scope) {
992 } else if (score.scope < hiscore.scope) {
993 if (score.scope < daddr_scope)
994 break; /* addresses sorted by scope */
1001 /* Rule 3: Avoid deprecated and optimistic addresses */
1002 if (hiscore.rule < 3) {
1003 if (ipv6_saddr_preferred(hiscore.addr_type) ||
1004 (((ifa_result->flags &
1005 (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0)))
1006 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1009 if (ipv6_saddr_preferred(score.addr_type) ||
1011 (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0))) {
1012 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
1013 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
1018 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
1022 /* Rule 4: Prefer home address */
1023 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
1024 if (hiscore.rule < 4) {
1025 if (ifa_result->flags & IFA_F_HOMEADDRESS)
1026 hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
1029 if (ifa->flags & IFA_F_HOMEADDRESS) {
1030 score.attrs |= IPV6_SADDR_SCORE_HOA;
1031 if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
1036 if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
1040 if (hiscore.rule < 4)
1044 /* Rule 5: Prefer outgoing interface */
1045 if (hiscore.rule < 5) {
1046 if (daddr_dev == NULL ||
1047 daddr_dev == ifa_result->idev->dev)
1048 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1051 if (daddr_dev == NULL ||
1052 daddr_dev == ifa->idev->dev) {
1053 score.attrs |= IPV6_SADDR_SCORE_OIF;
1054 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1059 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1063 /* Rule 6: Prefer matching label */
1064 if (hiscore.rule < 6) {
1065 if (ipv6_addr_label(&ifa_result->addr,
1067 ifa_result->idev->dev->ifindex) == daddr_label)
1068 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1071 if (ipv6_addr_label(&ifa->addr,
1073 ifa->idev->dev->ifindex) == daddr_label) {
1074 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1075 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1080 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1084 #ifdef CONFIG_IPV6_PRIVACY
1085 /* Rule 7: Prefer public address
1086 * Note: prefer temprary address if use_tempaddr >= 2
1088 if (hiscore.rule < 7) {
1089 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1090 (ifa_result->idev->cnf.use_tempaddr >= 2))
1091 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1094 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1095 (ifa->idev->cnf.use_tempaddr >= 2)) {
1096 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1097 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1102 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1106 if (hiscore.rule < 7)
1109 /* Rule 8: Use longest matching prefix */
1110 if (hiscore.rule < 8) {
1111 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
1114 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1115 if (score.matchlen > hiscore.matchlen) {
1120 else if (score.matchlen < hiscore.matchlen)
1124 /* Final Rule: choose first available one */
1128 in6_ifa_put(ifa_result);
1133 read_unlock_bh(&idev->lock);
1136 read_unlock(&dev_base_lock);
1139 return -EADDRNOTAVAIL;
1141 ipv6_addr_copy(saddr, &ifa_result->addr);
1142 in6_ifa_put(ifa_result);
1147 int ipv6_get_saddr(struct dst_entry *dst,
1148 struct in6_addr *daddr, struct in6_addr *saddr)
1150 return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
1153 EXPORT_SYMBOL(ipv6_get_saddr);
1155 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1156 unsigned char banned_flags)
1158 struct inet6_dev *idev;
1159 int err = -EADDRNOTAVAIL;
1162 if ((idev = __in6_dev_get(dev)) != NULL) {
1163 struct inet6_ifaddr *ifp;
1165 read_lock_bh(&idev->lock);
1166 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1167 if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) {
1168 ipv6_addr_copy(addr, &ifp->addr);
1173 read_unlock_bh(&idev->lock);
1179 static int ipv6_count_addresses(struct inet6_dev *idev)
1182 struct inet6_ifaddr *ifp;
1184 read_lock_bh(&idev->lock);
1185 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1187 read_unlock_bh(&idev->lock);
1191 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1193 struct inet6_ifaddr * ifp;
1194 u8 hash = ipv6_addr_hash(addr);
1196 read_lock_bh(&addrconf_hash_lock);
1197 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1198 if (ipv6_addr_equal(&ifp->addr, addr) &&
1199 !(ifp->flags&IFA_F_TENTATIVE)) {
1200 if (dev == NULL || ifp->idev->dev == dev ||
1201 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1205 read_unlock_bh(&addrconf_hash_lock);
1209 EXPORT_SYMBOL(ipv6_chk_addr);
1212 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1214 struct inet6_ifaddr * ifp;
1215 u8 hash = ipv6_addr_hash(addr);
1217 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1218 if (ipv6_addr_equal(&ifp->addr, addr)) {
1219 if (dev == NULL || ifp->idev->dev == dev)
1226 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1228 struct inet6_ifaddr * ifp;
1229 u8 hash = ipv6_addr_hash(addr);
1231 read_lock_bh(&addrconf_hash_lock);
1232 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1233 if (ipv6_addr_equal(&ifp->addr, addr)) {
1234 if (dev == NULL || ifp->idev->dev == dev ||
1235 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1241 read_unlock_bh(&addrconf_hash_lock);
1246 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1248 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1249 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
1250 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1251 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1252 int sk_ipv6only = ipv6_only_sock(sk);
1253 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1254 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1255 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1257 if (!sk2_rcv_saddr && !sk_ipv6only)
1260 if (addr_type2 == IPV6_ADDR_ANY &&
1261 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1264 if (addr_type == IPV6_ADDR_ANY &&
1265 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1268 if (sk2_rcv_saddr6 &&
1269 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1272 if (addr_type == IPV6_ADDR_MAPPED &&
1274 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1280 /* Gets referenced address, destroys ifaddr */
1282 static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1284 if (ifp->flags&IFA_F_PERMANENT) {
1285 spin_lock_bh(&ifp->lock);
1286 addrconf_del_timer(ifp);
1287 ifp->flags |= IFA_F_TENTATIVE;
1288 spin_unlock_bh(&ifp->lock);
1290 #ifdef CONFIG_IPV6_PRIVACY
1291 } else if (ifp->flags&IFA_F_TEMPORARY) {
1292 struct inet6_ifaddr *ifpub;
1293 spin_lock_bh(&ifp->lock);
1296 in6_ifa_hold(ifpub);
1297 spin_unlock_bh(&ifp->lock);
1298 ipv6_create_tempaddr(ifpub, ifp);
1301 spin_unlock_bh(&ifp->lock);
1309 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1311 if (net_ratelimit())
1312 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1313 addrconf_dad_stop(ifp);
1316 /* Join to solicited addr multicast group. */
1318 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1320 struct in6_addr maddr;
1322 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1325 addrconf_addr_solict_mult(addr, &maddr);
1326 ipv6_dev_mc_inc(dev, &maddr);
1329 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1331 struct in6_addr maddr;
1333 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1336 addrconf_addr_solict_mult(addr, &maddr);
1337 __ipv6_dev_mc_dec(idev, &maddr);
1340 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1342 struct in6_addr addr;
1343 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1344 if (ipv6_addr_any(&addr))
1346 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1349 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1351 struct in6_addr addr;
1352 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1353 if (ipv6_addr_any(&addr))
1355 __ipv6_dev_ac_dec(ifp->idev, &addr);
1358 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1360 if (dev->addr_len != ETH_ALEN)
1362 memcpy(eui, dev->dev_addr, 3);
1363 memcpy(eui + 5, dev->dev_addr + 3, 3);
1366 * The zSeries OSA network cards can be shared among various
1367 * OS instances, but the OSA cards have only one MAC address.
1368 * This leads to duplicate address conflicts in conjunction
1369 * with IPv6 if more than one instance uses the same card.
1371 * The driver for these cards can deliver a unique 16-bit
1372 * identifier for each instance sharing the same card. It is
1373 * placed instead of 0xFFFE in the interface identifier. The
1374 * "u" bit of the interface identifier is not inverted in this
1375 * case. Hence the resulting interface identifier has local
1376 * scope according to RFC2373.
1379 eui[3] = (dev->dev_id >> 8) & 0xFF;
1380 eui[4] = dev->dev_id & 0xFF;
1389 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1391 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1392 if (dev->addr_len != ARCNET_ALEN)
1395 eui[7] = *(u8*)dev->dev_addr;
1399 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1401 if (dev->addr_len != INFINIBAND_ALEN)
1403 memcpy(eui, dev->dev_addr + 12, 8);
1408 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1410 switch (dev->type) {
1413 case ARPHRD_IEEE802_TR:
1414 return addrconf_ifid_eui48(eui, dev);
1416 return addrconf_ifid_arcnet(eui, dev);
1417 case ARPHRD_INFINIBAND:
1418 return addrconf_ifid_infiniband(eui, dev);
1420 if (dev->priv_flags & IFF_ISATAP)
1421 return ipv6_isatap_eui64(eui, *(__be32 *)dev->dev_addr);
1426 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1429 struct inet6_ifaddr *ifp;
1431 read_lock_bh(&idev->lock);
1432 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1433 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1434 memcpy(eui, ifp->addr.s6_addr+8, 8);
1439 read_unlock_bh(&idev->lock);
1443 #ifdef CONFIG_IPV6_PRIVACY
1444 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1445 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1448 get_random_bytes(idev->rndid, sizeof(idev->rndid));
1449 idev->rndid[0] &= ~0x02;
1452 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1453 * check if generated address is not inappropriate
1455 * - Reserved subnet anycast (RFC 2526)
1456 * 11111101 11....11 1xxxxxxx
1457 * - ISATAP (RFC4214) 6.1
1458 * 00-00-5E-FE-xx-xx-xx-xx
1460 * - XXX: already assigned to an address on the device
1462 if (idev->rndid[0] == 0xfd &&
1463 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1464 (idev->rndid[7]&0x80))
1466 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1467 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1469 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1476 static void ipv6_regen_rndid(unsigned long data)
1478 struct inet6_dev *idev = (struct inet6_dev *) data;
1479 unsigned long expires;
1482 write_lock_bh(&idev->lock);
1487 if (__ipv6_regen_rndid(idev) < 0)
1491 idev->cnf.temp_prefered_lft * HZ -
1492 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1493 if (time_before(expires, jiffies)) {
1495 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1500 if (!mod_timer(&idev->regen_timer, expires))
1504 write_unlock_bh(&idev->lock);
1505 rcu_read_unlock_bh();
1509 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1512 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1513 ret = __ipv6_regen_rndid(idev);
1523 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1524 unsigned long expires, u32 flags)
1526 struct fib6_config cfg = {
1527 .fc_table = RT6_TABLE_PREFIX,
1528 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1529 .fc_ifindex = dev->ifindex,
1530 .fc_expires = expires,
1532 .fc_flags = RTF_UP | flags,
1535 ipv6_addr_copy(&cfg.fc_dst, pfx);
1537 /* Prevent useless cloning on PtP SIT.
1538 This thing is done here expecting that the whole
1539 class of non-broadcast devices need not cloning.
1541 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1542 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1543 cfg.fc_flags |= RTF_NONEXTHOP;
1546 ip6_route_add(&cfg);
1549 /* Create "default" multicast route to the interface */
1551 static void addrconf_add_mroute(struct net_device *dev)
1553 struct fib6_config cfg = {
1554 .fc_table = RT6_TABLE_LOCAL,
1555 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1556 .fc_ifindex = dev->ifindex,
1561 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1563 ip6_route_add(&cfg);
1566 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1567 static void sit_route_add(struct net_device *dev)
1569 struct fib6_config cfg = {
1570 .fc_table = RT6_TABLE_MAIN,
1571 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1572 .fc_ifindex = dev->ifindex,
1574 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1577 /* prefix length - 96 bits "::d.d.d.d" */
1578 ip6_route_add(&cfg);
1582 static void addrconf_add_lroute(struct net_device *dev)
1584 struct in6_addr addr;
1586 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1587 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1590 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1592 struct inet6_dev *idev;
1596 if ((idev = ipv6_find_idev(dev)) == NULL)
1599 /* Add default multicast route */
1600 addrconf_add_mroute(dev);
1602 /* Add link local route */
1603 addrconf_add_lroute(dev);
1607 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1609 struct prefix_info *pinfo;
1613 unsigned long rt_expires;
1614 struct inet6_dev *in6_dev;
1616 pinfo = (struct prefix_info *) opt;
1618 if (len < sizeof(struct prefix_info)) {
1619 ADBG(("addrconf: prefix option too short\n"));
1624 * Validation checks ([ADDRCONF], page 19)
1627 addr_type = ipv6_addr_type(&pinfo->prefix);
1629 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1632 valid_lft = ntohl(pinfo->valid);
1633 prefered_lft = ntohl(pinfo->prefered);
1635 if (prefered_lft > valid_lft) {
1636 if (net_ratelimit())
1637 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1641 in6_dev = in6_dev_get(dev);
1643 if (in6_dev == NULL) {
1644 if (net_ratelimit())
1645 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1650 * Two things going on here:
1651 * 1) Add routes for on-link prefixes
1652 * 2) Configure prefixes with the auto flag set
1655 /* Avoid arithmetic overflow. Really, we could
1656 save rt_expires in seconds, likely valid_lft,
1657 but it would require division in fib gc, that it
1660 if (valid_lft >= 0x7FFFFFFF/HZ)
1661 rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
1663 rt_expires = valid_lft * HZ;
1666 * We convert this (in jiffies) to clock_t later.
1667 * Avoid arithmetic overflow there as well.
1668 * Overflow can happen only if HZ < USER_HZ.
1670 if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
1671 rt_expires = 0x7FFFFFFF / USER_HZ;
1673 if (pinfo->onlink) {
1674 struct rt6_info *rt;
1675 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1677 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1678 if (rt->rt6i_flags&RTF_EXPIRES) {
1679 if (valid_lft == 0) {
1683 rt->rt6i_expires = jiffies + rt_expires;
1686 } else if (valid_lft) {
1687 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1688 dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1691 dst_release(&rt->u.dst);
1694 /* Try to figure out our local address for this prefix */
1696 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1697 struct inet6_ifaddr * ifp;
1698 struct in6_addr addr;
1699 int create = 0, update_lft = 0;
1701 if (pinfo->prefix_len == 64) {
1702 memcpy(&addr, &pinfo->prefix, 8);
1703 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1704 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1705 in6_dev_put(in6_dev);
1710 if (net_ratelimit())
1711 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1713 in6_dev_put(in6_dev);
1718 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1720 if (ifp == NULL && valid_lft) {
1721 int max_addresses = in6_dev->cnf.max_addresses;
1724 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1725 if (in6_dev->cnf.optimistic_dad &&
1726 !ipv6_devconf.forwarding)
1727 addr_flags = IFA_F_OPTIMISTIC;
1730 /* Do not allow to create too much of autoconfigured
1731 * addresses; this would be too easy way to crash kernel.
1733 if (!max_addresses ||
1734 ipv6_count_addresses(in6_dev) < max_addresses)
1735 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1736 addr_type&IPV6_ADDR_SCOPE_MASK,
1739 if (!ifp || IS_ERR(ifp)) {
1740 in6_dev_put(in6_dev);
1744 update_lft = create = 1;
1745 ifp->cstamp = jiffies;
1746 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1752 #ifdef CONFIG_IPV6_PRIVACY
1753 struct inet6_ifaddr *ift;
1757 /* update lifetime (RFC2462 5.5.3 e) */
1758 spin_lock(&ifp->lock);
1760 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1761 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1764 if (!update_lft && stored_lft) {
1765 if (valid_lft > MIN_VALID_LIFETIME ||
1766 valid_lft > stored_lft)
1768 else if (stored_lft <= MIN_VALID_LIFETIME) {
1769 /* valid_lft <= stored_lft is always true */
1773 valid_lft = MIN_VALID_LIFETIME;
1774 if (valid_lft < prefered_lft)
1775 prefered_lft = valid_lft;
1781 ifp->valid_lft = valid_lft;
1782 ifp->prefered_lft = prefered_lft;
1785 ifp->flags &= ~IFA_F_DEPRECATED;
1786 spin_unlock(&ifp->lock);
1788 if (!(flags&IFA_F_TENTATIVE))
1789 ipv6_ifa_notify(0, ifp);
1791 spin_unlock(&ifp->lock);
1793 #ifdef CONFIG_IPV6_PRIVACY
1794 read_lock_bh(&in6_dev->lock);
1795 /* update all temporary addresses in the list */
1796 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1798 * When adjusting the lifetimes of an existing
1799 * temporary address, only lower the lifetimes.
1800 * Implementations must not increase the
1801 * lifetimes of an existing temporary address
1802 * when processing a Prefix Information Option.
1804 spin_lock(&ift->lock);
1806 if (ift->valid_lft > valid_lft &&
1807 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1808 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1809 if (ift->prefered_lft > prefered_lft &&
1810 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1811 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1812 spin_unlock(&ift->lock);
1813 if (!(flags&IFA_F_TENTATIVE))
1814 ipv6_ifa_notify(0, ift);
1817 if (create && in6_dev->cnf.use_tempaddr > 0) {
1819 * When a new public address is created as described in [ADDRCONF],
1820 * also create a new temporary address.
1822 read_unlock_bh(&in6_dev->lock);
1823 ipv6_create_tempaddr(ifp, NULL);
1825 read_unlock_bh(&in6_dev->lock);
1832 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1833 in6_dev_put(in6_dev);
1837 * Set destination address.
1838 * Special case for SIT interfaces where we create a new "virtual"
1841 int addrconf_set_dstaddr(void __user *arg)
1843 struct in6_ifreq ireq;
1844 struct net_device *dev;
1850 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1853 dev = __dev_get_by_index(&init_net, ireq.ifr6_ifindex);
1859 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1860 if (dev->type == ARPHRD_SIT) {
1863 struct ip_tunnel_parm p;
1865 err = -EADDRNOTAVAIL;
1866 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1869 memset(&p, 0, sizeof(p));
1870 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1874 p.iph.protocol = IPPROTO_IPV6;
1876 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1878 oldfs = get_fs(); set_fs(KERNEL_DS);
1879 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1884 if ((dev = __dev_get_by_name(&init_net, p.name)) == NULL)
1886 err = dev_open(dev);
1897 * Manual configuration of address on an interface
1899 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
1900 __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
1902 struct inet6_ifaddr *ifp;
1903 struct inet6_dev *idev;
1904 struct net_device *dev;
1906 u32 flags = RTF_EXPIRES;
1910 /* check the lifetime */
1911 if (!valid_lft || prefered_lft > valid_lft)
1914 if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
1917 if ((idev = addrconf_add_dev(dev)) == NULL)
1920 scope = ipv6_addr_scope(pfx);
1922 if (valid_lft == INFINITY_LIFE_TIME) {
1923 ifa_flags |= IFA_F_PERMANENT;
1925 } else if (valid_lft >= 0x7FFFFFFF/HZ)
1926 valid_lft = 0x7FFFFFFF/HZ;
1928 if (prefered_lft == 0)
1929 ifa_flags |= IFA_F_DEPRECATED;
1930 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
1931 (prefered_lft != INFINITY_LIFE_TIME))
1932 prefered_lft = 0x7FFFFFFF/HZ;
1934 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
1937 spin_lock_bh(&ifp->lock);
1938 ifp->valid_lft = valid_lft;
1939 ifp->prefered_lft = prefered_lft;
1940 ifp->tstamp = jiffies;
1941 spin_unlock_bh(&ifp->lock);
1943 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
1944 jiffies_to_clock_t(valid_lft * HZ), flags);
1946 * Note that section 3.1 of RFC 4429 indicates
1947 * that the Optimistic flag should not be set for
1948 * manually configured addresses
1950 addrconf_dad_start(ifp, 0);
1956 return PTR_ERR(ifp);
1959 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1961 struct inet6_ifaddr *ifp;
1962 struct inet6_dev *idev;
1963 struct net_device *dev;
1965 if ((dev = __dev_get_by_index(&init_net, ifindex)) == NULL)
1968 if ((idev = __in6_dev_get(dev)) == NULL)
1971 read_lock_bh(&idev->lock);
1972 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1973 if (ifp->prefix_len == plen &&
1974 ipv6_addr_equal(pfx, &ifp->addr)) {
1976 read_unlock_bh(&idev->lock);
1980 /* If the last address is deleted administratively,
1981 disable IPv6 on this interface.
1983 if (idev->addr_list == NULL)
1984 addrconf_ifdown(idev->dev, 1);
1988 read_unlock_bh(&idev->lock);
1989 return -EADDRNOTAVAIL;
1993 int addrconf_add_ifaddr(void __user *arg)
1995 struct in6_ifreq ireq;
1998 if (!capable(CAP_NET_ADMIN))
2001 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2005 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
2006 IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2011 int addrconf_del_ifaddr(void __user *arg)
2013 struct in6_ifreq ireq;
2016 if (!capable(CAP_NET_ADMIN))
2019 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2023 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
2028 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2029 static void sit_add_v4_addrs(struct inet6_dev *idev)
2031 struct inet6_ifaddr * ifp;
2032 struct in6_addr addr;
2033 struct net_device *dev;
2038 memset(&addr, 0, sizeof(struct in6_addr));
2039 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2041 if (idev->dev->flags&IFF_POINTOPOINT) {
2042 addr.s6_addr32[0] = htonl(0xfe800000);
2045 scope = IPV6_ADDR_COMPATv4;
2048 if (addr.s6_addr32[3]) {
2049 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2051 spin_lock_bh(&ifp->lock);
2052 ifp->flags &= ~IFA_F_TENTATIVE;
2053 spin_unlock_bh(&ifp->lock);
2054 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2060 for_each_netdev(&init_net, dev) {
2061 struct in_device * in_dev = __in_dev_get_rtnl(dev);
2062 if (in_dev && (dev->flags & IFF_UP)) {
2063 struct in_ifaddr * ifa;
2067 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2070 addr.s6_addr32[3] = ifa->ifa_local;
2072 if (ifa->ifa_scope == RT_SCOPE_LINK)
2074 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2075 if (idev->dev->flags&IFF_POINTOPOINT)
2079 if (idev->dev->flags&IFF_POINTOPOINT)
2084 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2087 spin_lock_bh(&ifp->lock);
2088 ifp->flags &= ~IFA_F_TENTATIVE;
2089 spin_unlock_bh(&ifp->lock);
2090 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2099 static void init_loopback(struct net_device *dev)
2101 struct inet6_dev *idev;
2102 struct inet6_ifaddr * ifp;
2108 if ((idev = ipv6_find_idev(dev)) == NULL) {
2109 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2113 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2115 spin_lock_bh(&ifp->lock);
2116 ifp->flags &= ~IFA_F_TENTATIVE;
2117 spin_unlock_bh(&ifp->lock);
2118 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2123 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2125 struct inet6_ifaddr * ifp;
2126 u32 addr_flags = IFA_F_PERMANENT;
2128 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2129 if (idev->cnf.optimistic_dad &&
2130 !ipv6_devconf.forwarding)
2131 addr_flags |= IFA_F_OPTIMISTIC;
2135 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
2137 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2138 addrconf_dad_start(ifp, 0);
2143 static void addrconf_dev_config(struct net_device *dev)
2145 struct in6_addr addr;
2146 struct inet6_dev * idev;
2150 if ((dev->type != ARPHRD_ETHER) &&
2151 (dev->type != ARPHRD_FDDI) &&
2152 (dev->type != ARPHRD_IEEE802_TR) &&
2153 (dev->type != ARPHRD_ARCNET) &&
2154 (dev->type != ARPHRD_INFINIBAND)) {
2155 /* Alas, we support only Ethernet autoconfiguration. */
2159 idev = addrconf_add_dev(dev);
2163 memset(&addr, 0, sizeof(struct in6_addr));
2164 addr.s6_addr32[0] = htonl(0xFE800000);
2166 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2167 addrconf_add_linklocal(idev, &addr);
2170 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2171 static void addrconf_sit_config(struct net_device *dev)
2173 struct inet6_dev *idev;
2178 * Configure the tunnel with one of our IPv4
2179 * addresses... we should configure all of
2180 * our v4 addrs in the tunnel
2183 if ((idev = ipv6_find_idev(dev)) == NULL) {
2184 printk(KERN_DEBUG "init sit: add_dev failed\n");
2188 if (dev->priv_flags & IFF_ISATAP) {
2189 struct in6_addr addr;
2191 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2192 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2193 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2194 addrconf_add_linklocal(idev, &addr);
2198 sit_add_v4_addrs(idev);
2200 if (dev->flags&IFF_POINTOPOINT) {
2201 addrconf_add_mroute(dev);
2202 addrconf_add_lroute(dev);
2209 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2211 struct in6_addr lladdr;
2213 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
2214 addrconf_add_linklocal(idev, &lladdr);
2220 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2222 struct net_device *link_dev;
2224 /* first try to inherit the link-local address from the link device */
2225 if (idev->dev->iflink &&
2226 (link_dev = __dev_get_by_index(&init_net, idev->dev->iflink))) {
2227 if (!ipv6_inherit_linklocal(idev, link_dev))
2230 /* then try to inherit it from any device */
2231 for_each_netdev(&init_net, link_dev) {
2232 if (!ipv6_inherit_linklocal(idev, link_dev))
2235 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2239 * Autoconfigure tunnel with a link-local address so routing protocols,
2240 * DHCPv6, MLD etc. can be run over the virtual link
2243 static void addrconf_ip6_tnl_config(struct net_device *dev)
2245 struct inet6_dev *idev;
2249 if ((idev = addrconf_add_dev(dev)) == NULL) {
2250 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2253 ip6_tnl_add_linklocal(idev);
2256 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2259 struct net_device *dev = (struct net_device *) data;
2260 struct inet6_dev *idev = __in6_dev_get(dev);
2261 int run_pending = 0;
2264 if (dev->nd_net != &init_net)
2268 case NETDEV_REGISTER:
2269 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2270 idev = ipv6_add_dev(dev);
2272 return notifier_from_errno(-ENOMEM);
2277 if (dev->flags & IFF_SLAVE)
2280 if (event == NETDEV_UP) {
2281 if (!addrconf_qdisc_ok(dev)) {
2282 /* device is not ready yet. */
2284 "ADDRCONF(NETDEV_UP): %s: "
2285 "link is not ready\n",
2290 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2291 idev = ipv6_add_dev(dev);
2294 idev->if_flags |= IF_READY;
2296 if (!addrconf_qdisc_ok(dev)) {
2297 /* device is still not ready. */
2302 if (idev->if_flags & IF_READY) {
2303 /* device is already configured. */
2306 idev->if_flags |= IF_READY;
2310 "ADDRCONF(NETDEV_CHANGE): %s: "
2311 "link becomes ready\n",
2318 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2320 addrconf_sit_config(dev);
2323 case ARPHRD_TUNNEL6:
2324 addrconf_ip6_tnl_config(dev);
2326 case ARPHRD_LOOPBACK:
2331 addrconf_dev_config(dev);
2336 addrconf_dad_run(idev);
2338 /* If the MTU changed during the interface down, when the
2339 interface up, the changed MTU must be reflected in the
2340 idev as well as routers.
2342 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2343 rt6_mtu_change(dev, dev->mtu);
2344 idev->cnf.mtu6 = dev->mtu;
2346 idev->tstamp = jiffies;
2347 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2348 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2349 stop IPv6 on this interface.
2351 if (dev->mtu < IPV6_MIN_MTU)
2352 addrconf_ifdown(dev, event != NETDEV_DOWN);
2356 case NETDEV_CHANGEMTU:
2357 if (idev && dev->mtu >= IPV6_MIN_MTU) {
2358 rt6_mtu_change(dev, dev->mtu);
2359 idev->cnf.mtu6 = dev->mtu;
2363 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2364 idev = ipv6_add_dev(dev);
2369 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2372 case NETDEV_UNREGISTER:
2374 * Remove all addresses from this interface.
2376 addrconf_ifdown(dev, event != NETDEV_DOWN);
2379 case NETDEV_CHANGENAME:
2381 snmp6_unregister_dev(idev);
2382 #ifdef CONFIG_SYSCTL
2383 addrconf_sysctl_unregister(&idev->cnf);
2384 neigh_sysctl_unregister(idev->nd_parms);
2385 neigh_sysctl_register(dev, idev->nd_parms,
2386 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2387 &ndisc_ifinfo_sysctl_change,
2389 addrconf_sysctl_register(idev, &idev->cnf);
2391 err = snmp6_register_dev(idev);
2393 return notifier_from_errno(err);
2402 * addrconf module should be notified of a device going up
2404 static struct notifier_block ipv6_dev_notf = {
2405 .notifier_call = addrconf_notify,
2409 static int addrconf_ifdown(struct net_device *dev, int how)
2411 struct inet6_dev *idev;
2412 struct inet6_ifaddr *ifa, **bifa;
2417 if (dev == init_net.loopback_dev && how == 1)
2421 neigh_ifdown(&nd_tbl, dev);
2423 idev = __in6_dev_get(dev);
2427 /* Step 1: remove reference to ipv6 device from parent device.
2433 /* protected by rtnl_lock */
2434 rcu_assign_pointer(dev->ip6_ptr, NULL);
2436 /* Step 1.5: remove snmp6 entry */
2437 snmp6_unregister_dev(idev);
2441 /* Step 2: clear hash table */
2442 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2443 bifa = &inet6_addr_lst[i];
2445 write_lock_bh(&addrconf_hash_lock);
2446 while ((ifa = *bifa) != NULL) {
2447 if (ifa->idev == idev) {
2448 *bifa = ifa->lst_next;
2449 ifa->lst_next = NULL;
2450 addrconf_del_timer(ifa);
2454 bifa = &ifa->lst_next;
2456 write_unlock_bh(&addrconf_hash_lock);
2459 write_lock_bh(&idev->lock);
2461 /* Step 3: clear flags for stateless addrconf */
2463 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2465 /* Step 4: clear address list */
2466 #ifdef CONFIG_IPV6_PRIVACY
2467 if (how == 1 && del_timer(&idev->regen_timer))
2470 /* clear tempaddr list */
2471 while ((ifa = idev->tempaddr_list) != NULL) {
2472 idev->tempaddr_list = ifa->tmp_next;
2473 ifa->tmp_next = NULL;
2475 write_unlock_bh(&idev->lock);
2476 spin_lock_bh(&ifa->lock);
2479 in6_ifa_put(ifa->ifpub);
2482 spin_unlock_bh(&ifa->lock);
2484 write_lock_bh(&idev->lock);
2487 while ((ifa = idev->addr_list) != NULL) {
2488 idev->addr_list = ifa->if_next;
2489 ifa->if_next = NULL;
2491 addrconf_del_timer(ifa);
2492 write_unlock_bh(&idev->lock);
2494 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2495 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
2498 write_lock_bh(&idev->lock);
2500 write_unlock_bh(&idev->lock);
2502 /* Step 5: Discard multicast list */
2505 ipv6_mc_destroy_dev(idev);
2509 idev->tstamp = jiffies;
2511 /* Shot the device (if unregistered) */
2514 #ifdef CONFIG_SYSCTL
2515 addrconf_sysctl_unregister(&idev->cnf);
2516 neigh_sysctl_unregister(idev->nd_parms);
2518 neigh_parms_release(&nd_tbl, idev->nd_parms);
2519 neigh_ifdown(&nd_tbl, dev);
2525 static void addrconf_rs_timer(unsigned long data)
2527 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2529 if (ifp->idev->cnf.forwarding)
2532 if (ifp->idev->if_flags & IF_RA_RCVD) {
2534 * Announcement received after solicitation
2540 spin_lock(&ifp->lock);
2541 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2542 struct in6_addr all_routers;
2544 /* The wait after the last probe can be shorter */
2545 addrconf_mod_timer(ifp, AC_RS,
2546 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2547 ifp->idev->cnf.rtr_solicit_delay :
2548 ifp->idev->cnf.rtr_solicit_interval);
2549 spin_unlock(&ifp->lock);
2551 ipv6_addr_all_routers(&all_routers);
2553 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2555 spin_unlock(&ifp->lock);
2557 * Note: we do not support deprecated "all on-link"
2558 * assumption any longer.
2560 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2561 ifp->idev->dev->name);
2569 * Duplicate Address Detection
2571 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2573 unsigned long rand_num;
2574 struct inet6_dev *idev = ifp->idev;
2576 if (ifp->flags & IFA_F_OPTIMISTIC)
2579 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2581 ifp->probes = idev->cnf.dad_transmits;
2582 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2585 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2587 struct inet6_dev *idev = ifp->idev;
2588 struct net_device *dev = idev->dev;
2590 addrconf_join_solict(dev, &ifp->addr);
2592 net_srandom(ifp->addr.s6_addr32[3]);
2594 read_lock_bh(&idev->lock);
2597 spin_lock_bh(&ifp->lock);
2599 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2600 !(ifp->flags&IFA_F_TENTATIVE) ||
2601 ifp->flags & IFA_F_NODAD) {
2602 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
2603 spin_unlock_bh(&ifp->lock);
2604 read_unlock_bh(&idev->lock);
2606 addrconf_dad_completed(ifp);
2610 if (!(idev->if_flags & IF_READY)) {
2611 spin_unlock_bh(&ifp->lock);
2612 read_unlock_bh(&idev->lock);
2614 * If the defice is not ready:
2615 * - keep it tentative if it is a permanent address.
2616 * - otherwise, kill it.
2619 addrconf_dad_stop(ifp);
2624 * Optimistic nodes can start receiving
2627 if(ifp->flags & IFA_F_OPTIMISTIC)
2628 ip6_ins_rt(ifp->rt);
2630 addrconf_dad_kick(ifp);
2631 spin_unlock_bh(&ifp->lock);
2633 read_unlock_bh(&idev->lock);
2636 static void addrconf_dad_timer(unsigned long data)
2638 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2639 struct inet6_dev *idev = ifp->idev;
2640 struct in6_addr unspec;
2641 struct in6_addr mcaddr;
2643 read_lock_bh(&idev->lock);
2645 read_unlock_bh(&idev->lock);
2648 spin_lock_bh(&ifp->lock);
2649 if (ifp->probes == 0) {
2651 * DAD was successful
2654 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
2655 spin_unlock_bh(&ifp->lock);
2656 read_unlock_bh(&idev->lock);
2658 addrconf_dad_completed(ifp);
2664 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2665 spin_unlock_bh(&ifp->lock);
2666 read_unlock_bh(&idev->lock);
2668 /* send a neighbour solicitation for our addr */
2669 memset(&unspec, 0, sizeof(unspec));
2670 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2671 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2676 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2678 struct net_device * dev = ifp->idev->dev;
2681 * Configure the address for reception. Now it is valid.
2684 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2686 /* If added prefix is link local and forwarding is off,
2687 start sending router solicitations.
2690 if (ifp->idev->cnf.forwarding == 0 &&
2691 ifp->idev->cnf.rtr_solicits > 0 &&
2692 (dev->flags&IFF_LOOPBACK) == 0 &&
2693 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2694 struct in6_addr all_routers;
2696 ipv6_addr_all_routers(&all_routers);
2699 * If a host as already performed a random delay
2700 * [...] as part of DAD [...] there is no need
2701 * to delay again before sending the first RS
2703 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2705 spin_lock_bh(&ifp->lock);
2707 ifp->idev->if_flags |= IF_RS_SENT;
2708 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2709 spin_unlock_bh(&ifp->lock);
2713 static void addrconf_dad_run(struct inet6_dev *idev) {
2714 struct inet6_ifaddr *ifp;
2716 read_lock_bh(&idev->lock);
2717 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2718 spin_lock_bh(&ifp->lock);
2719 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2720 spin_unlock_bh(&ifp->lock);
2723 spin_unlock_bh(&ifp->lock);
2724 addrconf_dad_kick(ifp);
2726 read_unlock_bh(&idev->lock);
2729 #ifdef CONFIG_PROC_FS
2730 struct if6_iter_state {
2734 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2736 struct inet6_ifaddr *ifa = NULL;
2737 struct if6_iter_state *state = seq->private;
2739 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2740 ifa = inet6_addr_lst[state->bucket];
2747 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2749 struct if6_iter_state *state = seq->private;
2751 ifa = ifa->lst_next;
2753 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2754 ifa = inet6_addr_lst[state->bucket];
2760 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2762 struct inet6_ifaddr *ifa = if6_get_first(seq);
2765 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2767 return pos ? NULL : ifa;
2770 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2772 read_lock_bh(&addrconf_hash_lock);
2773 return if6_get_idx(seq, *pos);
2776 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2778 struct inet6_ifaddr *ifa;
2780 ifa = if6_get_next(seq, v);
2785 static void if6_seq_stop(struct seq_file *seq, void *v)
2787 read_unlock_bh(&addrconf_hash_lock);
2790 static int if6_seq_show(struct seq_file *seq, void *v)
2792 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2794 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
2796 ifp->idev->dev->ifindex,
2800 ifp->idev->dev->name);
2804 static const struct seq_operations if6_seq_ops = {
2805 .start = if6_seq_start,
2806 .next = if6_seq_next,
2807 .show = if6_seq_show,
2808 .stop = if6_seq_stop,
2811 static int if6_seq_open(struct inode *inode, struct file *file)
2813 return seq_open_private(file, &if6_seq_ops,
2814 sizeof(struct if6_iter_state));
2817 static const struct file_operations if6_fops = {
2818 .owner = THIS_MODULE,
2819 .open = if6_seq_open,
2821 .llseek = seq_lseek,
2822 .release = seq_release_private,
2825 int __init if6_proc_init(void)
2827 if (!proc_net_fops_create(&init_net, "if_inet6", S_IRUGO, &if6_fops))
2832 void if6_proc_exit(void)
2834 proc_net_remove(&init_net, "if_inet6");
2836 #endif /* CONFIG_PROC_FS */
2838 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
2839 /* Check if address is a home address configured on any interface. */
2840 int ipv6_chk_home_addr(struct in6_addr *addr)
2843 struct inet6_ifaddr * ifp;
2844 u8 hash = ipv6_addr_hash(addr);
2845 read_lock_bh(&addrconf_hash_lock);
2846 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
2847 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
2848 (ifp->flags & IFA_F_HOMEADDRESS)) {
2853 read_unlock_bh(&addrconf_hash_lock);
2859 * Periodic address status verification
2862 static void addrconf_verify(unsigned long foo)
2864 struct inet6_ifaddr *ifp;
2865 unsigned long now, next;
2868 spin_lock_bh(&addrconf_verify_lock);
2870 next = now + ADDR_CHECK_FREQUENCY;
2872 del_timer(&addr_chk_timer);
2874 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2877 read_lock(&addrconf_hash_lock);
2878 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2880 #ifdef CONFIG_IPV6_PRIVACY
2881 unsigned long regen_advance;
2884 if (ifp->flags & IFA_F_PERMANENT)
2887 spin_lock(&ifp->lock);
2888 age = (now - ifp->tstamp) / HZ;
2890 #ifdef CONFIG_IPV6_PRIVACY
2891 regen_advance = ifp->idev->cnf.regen_max_retry *
2892 ifp->idev->cnf.dad_transmits *
2893 ifp->idev->nd_parms->retrans_time / HZ;
2896 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
2897 age >= ifp->valid_lft) {
2898 spin_unlock(&ifp->lock);
2900 read_unlock(&addrconf_hash_lock);
2903 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
2904 spin_unlock(&ifp->lock);
2906 } else if (age >= ifp->prefered_lft) {
2907 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2910 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2912 ifp->flags |= IFA_F_DEPRECATED;
2915 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2916 next = ifp->tstamp + ifp->valid_lft * HZ;
2918 spin_unlock(&ifp->lock);
2922 read_unlock(&addrconf_hash_lock);
2924 ipv6_ifa_notify(0, ifp);
2928 #ifdef CONFIG_IPV6_PRIVACY
2929 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2930 !(ifp->flags&IFA_F_TENTATIVE)) {
2931 if (age >= ifp->prefered_lft - regen_advance) {
2932 struct inet6_ifaddr *ifpub = ifp->ifpub;
2933 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2934 next = ifp->tstamp + ifp->prefered_lft * HZ;
2935 if (!ifp->regen_count && ifpub) {
2938 in6_ifa_hold(ifpub);
2939 spin_unlock(&ifp->lock);
2940 read_unlock(&addrconf_hash_lock);
2941 spin_lock(&ifpub->lock);
2942 ifpub->regen_count = 0;
2943 spin_unlock(&ifpub->lock);
2944 ipv6_create_tempaddr(ifpub, ifp);
2949 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2950 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2951 spin_unlock(&ifp->lock);
2954 /* ifp->prefered_lft <= ifp->valid_lft */
2955 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2956 next = ifp->tstamp + ifp->prefered_lft * HZ;
2957 spin_unlock(&ifp->lock);
2960 read_unlock(&addrconf_hash_lock);
2963 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2964 add_timer(&addr_chk_timer);
2965 spin_unlock_bh(&addrconf_verify_lock);
2968 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
2970 struct in6_addr *pfx = NULL;
2973 pfx = nla_data(addr);
2976 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
2979 pfx = nla_data(local);
2985 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
2986 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
2987 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
2988 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
2992 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2994 struct net *net = skb->sk->sk_net;
2995 struct ifaddrmsg *ifm;
2996 struct nlattr *tb[IFA_MAX+1];
2997 struct in6_addr *pfx;
3000 if (net != &init_net)
3003 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3007 ifm = nlmsg_data(nlh);
3008 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3012 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
3015 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3016 u32 prefered_lft, u32 valid_lft)
3018 u32 flags = RTF_EXPIRES;
3020 if (!valid_lft || (prefered_lft > valid_lft))
3023 if (valid_lft == INFINITY_LIFE_TIME) {
3024 ifa_flags |= IFA_F_PERMANENT;
3026 } else if (valid_lft >= 0x7FFFFFFF/HZ)
3027 valid_lft = 0x7FFFFFFF/HZ;
3029 if (prefered_lft == 0)
3030 ifa_flags |= IFA_F_DEPRECATED;
3031 else if ((prefered_lft >= 0x7FFFFFFF/HZ) &&
3032 (prefered_lft != INFINITY_LIFE_TIME))
3033 prefered_lft = 0x7FFFFFFF/HZ;
3035 spin_lock_bh(&ifp->lock);
3036 ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3037 ifp->tstamp = jiffies;
3038 ifp->valid_lft = valid_lft;
3039 ifp->prefered_lft = prefered_lft;
3041 spin_unlock_bh(&ifp->lock);
3042 if (!(ifp->flags&IFA_F_TENTATIVE))
3043 ipv6_ifa_notify(0, ifp);
3045 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3046 jiffies_to_clock_t(valid_lft * HZ), flags);
3053 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3055 struct net *net = skb->sk->sk_net;
3056 struct ifaddrmsg *ifm;
3057 struct nlattr *tb[IFA_MAX+1];
3058 struct in6_addr *pfx;
3059 struct inet6_ifaddr *ifa;
3060 struct net_device *dev;
3061 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3065 if (net != &init_net)
3068 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3072 ifm = nlmsg_data(nlh);
3073 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3077 if (tb[IFA_CACHEINFO]) {
3078 struct ifa_cacheinfo *ci;
3080 ci = nla_data(tb[IFA_CACHEINFO]);
3081 valid_lft = ci->ifa_valid;
3082 preferred_lft = ci->ifa_prefered;
3084 preferred_lft = INFINITY_LIFE_TIME;
3085 valid_lft = INFINITY_LIFE_TIME;
3088 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
3092 /* We ignore other flags so far. */
3093 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
3095 ifa = ipv6_get_ifaddr(pfx, dev, 1);
3098 * It would be best to check for !NLM_F_CREATE here but
3099 * userspace alreay relies on not having to provide this.
3101 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
3102 ifa_flags, preferred_lft, valid_lft);
3105 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3106 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3109 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
3116 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3117 u8 scope, int ifindex)
3119 struct ifaddrmsg *ifm;
3121 ifm = nlmsg_data(nlh);
3122 ifm->ifa_family = AF_INET6;
3123 ifm->ifa_prefixlen = prefixlen;
3124 ifm->ifa_flags = flags;
3125 ifm->ifa_scope = scope;
3126 ifm->ifa_index = ifindex;
3129 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3130 unsigned long tstamp, u32 preferred, u32 valid)
3132 struct ifa_cacheinfo ci;
3134 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3135 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3136 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3137 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3138 ci.ifa_prefered = preferred;
3139 ci.ifa_valid = valid;
3141 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3144 static inline int rt_scope(int ifa_scope)
3146 if (ifa_scope & IFA_HOST)
3147 return RT_SCOPE_HOST;
3148 else if (ifa_scope & IFA_LINK)
3149 return RT_SCOPE_LINK;
3150 else if (ifa_scope & IFA_SITE)
3151 return RT_SCOPE_SITE;
3153 return RT_SCOPE_UNIVERSE;
3156 static inline int inet6_ifaddr_msgsize(void)
3158 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3159 + nla_total_size(16) /* IFA_ADDRESS */
3160 + nla_total_size(sizeof(struct ifa_cacheinfo));
3163 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3164 u32 pid, u32 seq, int event, unsigned int flags)
3166 struct nlmsghdr *nlh;
3167 u32 preferred, valid;
3169 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3173 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3174 ifa->idev->dev->ifindex);
3176 if (!(ifa->flags&IFA_F_PERMANENT)) {
3177 preferred = ifa->prefered_lft;
3178 valid = ifa->valid_lft;
3179 if (preferred != INFINITY_LIFE_TIME) {
3180 long tval = (jiffies - ifa->tstamp)/HZ;
3182 if (valid != INFINITY_LIFE_TIME)
3186 preferred = INFINITY_LIFE_TIME;
3187 valid = INFINITY_LIFE_TIME;
3190 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
3191 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3192 nlmsg_cancel(skb, nlh);
3196 return nlmsg_end(skb, nlh);
3199 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3200 u32 pid, u32 seq, int event, u16 flags)
3202 struct nlmsghdr *nlh;
3203 u8 scope = RT_SCOPE_UNIVERSE;
3204 int ifindex = ifmca->idev->dev->ifindex;
3206 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3207 scope = RT_SCOPE_SITE;
3209 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3213 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3214 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3215 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
3216 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3217 nlmsg_cancel(skb, nlh);
3221 return nlmsg_end(skb, nlh);
3224 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3225 u32 pid, u32 seq, int event, unsigned int flags)
3227 struct nlmsghdr *nlh;
3228 u8 scope = RT_SCOPE_UNIVERSE;
3229 int ifindex = ifaca->aca_idev->dev->ifindex;
3231 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3232 scope = RT_SCOPE_SITE;
3234 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3238 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3239 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3240 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
3241 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3242 nlmsg_cancel(skb, nlh);
3246 return nlmsg_end(skb, nlh);
3256 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3257 enum addr_type_t type)
3260 int s_idx, s_ip_idx;
3262 struct net_device *dev;
3263 struct inet6_dev *idev = NULL;
3264 struct inet6_ifaddr *ifa;
3265 struct ifmcaddr6 *ifmca;
3266 struct ifacaddr6 *ifaca;
3268 s_idx = cb->args[0];
3269 s_ip_idx = ip_idx = cb->args[1];
3272 for_each_netdev(&init_net, dev) {
3278 if ((idev = in6_dev_get(dev)) == NULL)
3280 read_lock_bh(&idev->lock);
3283 /* unicast address incl. temp addr */
3284 for (ifa = idev->addr_list; ifa;
3285 ifa = ifa->if_next, ip_idx++) {
3286 if (ip_idx < s_ip_idx)
3288 if ((err = inet6_fill_ifaddr(skb, ifa,
3289 NETLINK_CB(cb->skb).pid,
3290 cb->nlh->nlmsg_seq, RTM_NEWADDR,
3295 case MULTICAST_ADDR:
3296 /* multicast address */
3297 for (ifmca = idev->mc_list; ifmca;
3298 ifmca = ifmca->next, ip_idx++) {
3299 if (ip_idx < s_ip_idx)
3301 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
3302 NETLINK_CB(cb->skb).pid,
3303 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3309 /* anycast address */
3310 for (ifaca = idev->ac_list; ifaca;
3311 ifaca = ifaca->aca_next, ip_idx++) {
3312 if (ip_idx < s_ip_idx)
3314 if ((err = inet6_fill_ifacaddr(skb, ifaca,
3315 NETLINK_CB(cb->skb).pid,
3316 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3324 read_unlock_bh(&idev->lock);
3331 read_unlock_bh(&idev->lock);
3335 cb->args[1] = ip_idx;
3339 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3341 struct net *net = skb->sk->sk_net;
3342 enum addr_type_t type = UNICAST_ADDR;
3344 if (net != &init_net)
3347 return inet6_dump_addr(skb, cb, type);
3350 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3352 struct net *net = skb->sk->sk_net;
3353 enum addr_type_t type = MULTICAST_ADDR;
3355 if (net != &init_net)
3358 return inet6_dump_addr(skb, cb, type);
3362 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3364 struct net *net = skb->sk->sk_net;
3365 enum addr_type_t type = ANYCAST_ADDR;
3367 if (net != &init_net)
3370 return inet6_dump_addr(skb, cb, type);
3373 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3376 struct net *net = in_skb->sk->sk_net;
3377 struct ifaddrmsg *ifm;
3378 struct nlattr *tb[IFA_MAX+1];
3379 struct in6_addr *addr = NULL;
3380 struct net_device *dev = NULL;
3381 struct inet6_ifaddr *ifa;
3382 struct sk_buff *skb;
3385 if (net != &init_net)
3388 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3392 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3398 ifm = nlmsg_data(nlh);
3400 dev = __dev_get_by_index(&init_net, ifm->ifa_index);
3402 if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
3403 err = -EADDRNOTAVAIL;
3407 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
3412 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3413 nlh->nlmsg_seq, RTM_NEWADDR, 0);
3415 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3416 WARN_ON(err == -EMSGSIZE);
3420 err = rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
3427 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3429 struct sk_buff *skb;
3432 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
3436 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
3438 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3439 WARN_ON(err == -EMSGSIZE);
3443 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3446 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
3449 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
3450 __s32 *array, int bytes)
3452 BUG_ON(bytes < (DEVCONF_MAX * 4));
3454 memset(array, 0, bytes);
3455 array[DEVCONF_FORWARDING] = cnf->forwarding;
3456 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3457 array[DEVCONF_MTU6] = cnf->mtu6;
3458 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3459 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3460 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3461 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3462 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3463 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3464 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3465 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3466 #ifdef CONFIG_IPV6_PRIVACY
3467 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3468 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3469 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3470 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3471 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3473 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3474 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3475 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3476 #ifdef CONFIG_IPV6_ROUTER_PREF
3477 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
3478 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
3479 #ifdef CONFIG_IPV6_ROUTE_INFO
3480 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3483 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
3484 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
3485 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3486 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
3490 static inline size_t inet6_if_nlmsg_size(void)
3492 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3493 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3494 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3495 + nla_total_size(4) /* IFLA_MTU */
3496 + nla_total_size(4) /* IFLA_LINK */
3497 + nla_total_size( /* IFLA_PROTINFO */
3498 nla_total_size(4) /* IFLA_INET6_FLAGS */
3499 + nla_total_size(sizeof(struct ifla_cacheinfo))
3500 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3501 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3502 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
3506 static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
3510 int pad = bytes - sizeof(u64) * items;
3513 /* Use put_unaligned() because stats may not be aligned for u64. */
3514 put_unaligned(items, &stats[0]);
3515 for (i = 1; i < items; i++)
3516 put_unaligned(snmp_fold_field(mib, i), &stats[i]);
3518 memset(&stats[items], 0, pad);
3521 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
3525 case IFLA_INET6_STATS:
3526 __snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
3528 case IFLA_INET6_ICMP6STATS:
3529 __snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
3534 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3535 u32 pid, u32 seq, int event, unsigned int flags)
3537 struct net_device *dev = idev->dev;
3539 struct ifinfomsg *hdr;
3540 struct nlmsghdr *nlh;
3542 struct ifla_cacheinfo ci;
3544 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3548 hdr = nlmsg_data(nlh);
3549 hdr->ifi_family = AF_INET6;
3551 hdr->ifi_type = dev->type;
3552 hdr->ifi_index = dev->ifindex;
3553 hdr->ifi_flags = dev_get_flags(dev);
3554 hdr->ifi_change = 0;
3556 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
3559 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3561 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
3562 if (dev->ifindex != dev->iflink)
3563 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
3565 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3566 if (protoinfo == NULL)
3567 goto nla_put_failure;
3569 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
3571 ci.max_reasm_len = IPV6_MAXPLEN;
3572 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3573 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3574 ci.reachable_time = idev->nd_parms->reachable_time;
3575 ci.retrans_time = idev->nd_parms->retrans_time;
3576 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3578 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3580 goto nla_put_failure;
3581 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
3583 /* XXX - MC not implemented */
3585 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3587 goto nla_put_failure;
3588 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3590 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3592 goto nla_put_failure;
3593 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
3595 nla_nest_end(skb, protoinfo);
3596 return nlmsg_end(skb, nlh);
3599 nlmsg_cancel(skb, nlh);
3603 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3605 struct net *net = skb->sk->sk_net;
3607 int s_idx = cb->args[0];
3608 struct net_device *dev;
3609 struct inet6_dev *idev;
3611 if (net != &init_net)
3614 read_lock(&dev_base_lock);
3616 for_each_netdev(&init_net, dev) {
3619 if ((idev = in6_dev_get(dev)) == NULL)
3621 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3622 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3629 read_unlock(&dev_base_lock);
3635 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3637 struct sk_buff *skb;
3640 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
3644 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
3646 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3647 WARN_ON(err == -EMSGSIZE);
3651 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3654 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_IFADDR, err);
3657 static inline size_t inet6_prefix_nlmsg_size(void)
3659 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3660 + nla_total_size(sizeof(struct in6_addr))
3661 + nla_total_size(sizeof(struct prefix_cacheinfo));
3664 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3665 struct prefix_info *pinfo, u32 pid, u32 seq,
3666 int event, unsigned int flags)
3668 struct prefixmsg *pmsg;
3669 struct nlmsghdr *nlh;
3670 struct prefix_cacheinfo ci;
3672 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3676 pmsg = nlmsg_data(nlh);
3677 pmsg->prefix_family = AF_INET6;
3678 pmsg->prefix_pad1 = 0;
3679 pmsg->prefix_pad2 = 0;
3680 pmsg->prefix_ifindex = idev->dev->ifindex;
3681 pmsg->prefix_len = pinfo->prefix_len;
3682 pmsg->prefix_type = pinfo->type;
3683 pmsg->prefix_pad3 = 0;
3684 pmsg->prefix_flags = 0;
3686 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3687 if (pinfo->autoconf)
3688 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3690 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3692 ci.preferred_time = ntohl(pinfo->prefered);
3693 ci.valid_time = ntohl(pinfo->valid);
3694 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3696 return nlmsg_end(skb, nlh);
3699 nlmsg_cancel(skb, nlh);
3703 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3704 struct prefix_info *pinfo)
3706 struct sk_buff *skb;
3709 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
3713 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
3715 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3716 WARN_ON(err == -EMSGSIZE);
3720 err = rtnl_notify(skb, &init_net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3723 rtnl_set_sk_err(&init_net, RTNLGRP_IPV6_PREFIX, err);
3726 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3728 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3733 * If the address was optimistic
3734 * we inserted the route at the start of
3735 * our DAD process, so we don't need
3738 if (!(ifp->rt->rt6i_node))
3739 ip6_ins_rt(ifp->rt);
3740 if (ifp->idev->cnf.forwarding)
3741 addrconf_join_anycast(ifp);
3744 if (ifp->idev->cnf.forwarding)
3745 addrconf_leave_anycast(ifp);
3746 addrconf_leave_solict(ifp->idev, &ifp->addr);
3747 dst_hold(&ifp->rt->u.dst);
3748 if (ip6_del_rt(ifp->rt))
3749 dst_free(&ifp->rt->u.dst);
3754 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3757 if (likely(ifp->idev->dead == 0))
3758 __ipv6_ifa_notify(event, ifp);
3759 rcu_read_unlock_bh();
3762 #ifdef CONFIG_SYSCTL
3765 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3766 void __user *buffer, size_t *lenp, loff_t *ppos)
3768 int *valp = ctl->data;
3772 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3774 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3775 if (valp != &ipv6_devconf.forwarding) {
3776 if ((!*valp) ^ (!val)) {
3777 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3780 dev_forward_change(idev);
3783 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3784 addrconf_forward_change();
3787 rt6_purge_dflt_routers();
3793 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3794 int __user *name, int nlen,
3795 void __user *oldval,
3796 size_t __user *oldlenp,
3797 void __user *newval, size_t newlen)
3799 int *valp = table->data;
3802 if (!newval || !newlen)
3804 if (newlen != sizeof(int))
3806 if (get_user(new, (int __user *)newval))
3810 if (oldval && oldlenp) {
3812 if (get_user(len, oldlenp))
3815 if (len > table->maxlen)
3816 len = table->maxlen;
3817 if (copy_to_user(oldval, valp, len))
3819 if (put_user(len, oldlenp))
3824 if (valp != &ipv6_devconf_dflt.forwarding) {
3825 if (valp != &ipv6_devconf.forwarding) {
3826 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3828 if (unlikely(idev == NULL))
3830 changed = (!*valp) ^ (!new);
3833 dev_forward_change(idev);
3836 addrconf_forward_change();
3840 rt6_purge_dflt_routers();
3847 static struct addrconf_sysctl_table
3849 struct ctl_table_header *sysctl_header;
3850 ctl_table addrconf_vars[__NET_IPV6_MAX];
3851 ctl_table addrconf_dev[2];
3852 ctl_table addrconf_conf_dir[2];
3853 ctl_table addrconf_proto_dir[2];
3854 ctl_table addrconf_root_dir[2];
3855 } addrconf_sysctl __read_mostly = {
3856 .sysctl_header = NULL,
3859 .ctl_name = NET_IPV6_FORWARDING,
3860 .procname = "forwarding",
3861 .data = &ipv6_devconf.forwarding,
3862 .maxlen = sizeof(int),
3864 .proc_handler = &addrconf_sysctl_forward,
3865 .strategy = &addrconf_sysctl_forward_strategy,
3868 .ctl_name = NET_IPV6_HOP_LIMIT,
3869 .procname = "hop_limit",
3870 .data = &ipv6_devconf.hop_limit,
3871 .maxlen = sizeof(int),
3873 .proc_handler = proc_dointvec,
3876 .ctl_name = NET_IPV6_MTU,
3878 .data = &ipv6_devconf.mtu6,
3879 .maxlen = sizeof(int),
3881 .proc_handler = &proc_dointvec,
3884 .ctl_name = NET_IPV6_ACCEPT_RA,
3885 .procname = "accept_ra",
3886 .data = &ipv6_devconf.accept_ra,
3887 .maxlen = sizeof(int),
3889 .proc_handler = &proc_dointvec,
3892 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3893 .procname = "accept_redirects",
3894 .data = &ipv6_devconf.accept_redirects,
3895 .maxlen = sizeof(int),
3897 .proc_handler = &proc_dointvec,
3900 .ctl_name = NET_IPV6_AUTOCONF,
3901 .procname = "autoconf",
3902 .data = &ipv6_devconf.autoconf,
3903 .maxlen = sizeof(int),
3905 .proc_handler = &proc_dointvec,
3908 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3909 .procname = "dad_transmits",
3910 .data = &ipv6_devconf.dad_transmits,
3911 .maxlen = sizeof(int),
3913 .proc_handler = &proc_dointvec,
3916 .ctl_name = NET_IPV6_RTR_SOLICITS,
3917 .procname = "router_solicitations",
3918 .data = &ipv6_devconf.rtr_solicits,
3919 .maxlen = sizeof(int),
3921 .proc_handler = &proc_dointvec,
3924 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3925 .procname = "router_solicitation_interval",
3926 .data = &ipv6_devconf.rtr_solicit_interval,
3927 .maxlen = sizeof(int),
3929 .proc_handler = &proc_dointvec_jiffies,
3930 .strategy = &sysctl_jiffies,
3933 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3934 .procname = "router_solicitation_delay",
3935 .data = &ipv6_devconf.rtr_solicit_delay,
3936 .maxlen = sizeof(int),
3938 .proc_handler = &proc_dointvec_jiffies,
3939 .strategy = &sysctl_jiffies,
3942 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3943 .procname = "force_mld_version",
3944 .data = &ipv6_devconf.force_mld_version,
3945 .maxlen = sizeof(int),
3947 .proc_handler = &proc_dointvec,
3949 #ifdef CONFIG_IPV6_PRIVACY
3951 .ctl_name = NET_IPV6_USE_TEMPADDR,
3952 .procname = "use_tempaddr",
3953 .data = &ipv6_devconf.use_tempaddr,
3954 .maxlen = sizeof(int),
3956 .proc_handler = &proc_dointvec,
3959 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3960 .procname = "temp_valid_lft",
3961 .data = &ipv6_devconf.temp_valid_lft,
3962 .maxlen = sizeof(int),
3964 .proc_handler = &proc_dointvec,
3967 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3968 .procname = "temp_prefered_lft",
3969 .data = &ipv6_devconf.temp_prefered_lft,
3970 .maxlen = sizeof(int),
3972 .proc_handler = &proc_dointvec,
3975 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3976 .procname = "regen_max_retry",
3977 .data = &ipv6_devconf.regen_max_retry,
3978 .maxlen = sizeof(int),
3980 .proc_handler = &proc_dointvec,
3983 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3984 .procname = "max_desync_factor",
3985 .data = &ipv6_devconf.max_desync_factor,
3986 .maxlen = sizeof(int),
3988 .proc_handler = &proc_dointvec,
3992 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3993 .procname = "max_addresses",
3994 .data = &ipv6_devconf.max_addresses,
3995 .maxlen = sizeof(int),
3997 .proc_handler = &proc_dointvec,
4000 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
4001 .procname = "accept_ra_defrtr",
4002 .data = &ipv6_devconf.accept_ra_defrtr,
4003 .maxlen = sizeof(int),
4005 .proc_handler = &proc_dointvec,
4008 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
4009 .procname = "accept_ra_pinfo",
4010 .data = &ipv6_devconf.accept_ra_pinfo,
4011 .maxlen = sizeof(int),
4013 .proc_handler = &proc_dointvec,
4015 #ifdef CONFIG_IPV6_ROUTER_PREF
4017 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
4018 .procname = "accept_ra_rtr_pref",
4019 .data = &ipv6_devconf.accept_ra_rtr_pref,
4020 .maxlen = sizeof(int),
4022 .proc_handler = &proc_dointvec,
4025 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
4026 .procname = "router_probe_interval",
4027 .data = &ipv6_devconf.rtr_probe_interval,
4028 .maxlen = sizeof(int),
4030 .proc_handler = &proc_dointvec_jiffies,
4031 .strategy = &sysctl_jiffies,
4033 #ifdef CONFIG_IPV6_ROUTE_INFO
4035 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
4036 .procname = "accept_ra_rt_info_max_plen",
4037 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4038 .maxlen = sizeof(int),
4040 .proc_handler = &proc_dointvec,
4045 .ctl_name = NET_IPV6_PROXY_NDP,
4046 .procname = "proxy_ndp",
4047 .data = &ipv6_devconf.proxy_ndp,
4048 .maxlen = sizeof(int),
4050 .proc_handler = &proc_dointvec,
4053 .ctl_name = NET_IPV6_ACCEPT_SOURCE_ROUTE,
4054 .procname = "accept_source_route",
4055 .data = &ipv6_devconf.accept_source_route,
4056 .maxlen = sizeof(int),
4058 .proc_handler = &proc_dointvec,
4060 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4062 .ctl_name = CTL_UNNUMBERED,
4063 .procname = "optimistic_dad",
4064 .data = &ipv6_devconf.optimistic_dad,
4065 .maxlen = sizeof(int),
4067 .proc_handler = &proc_dointvec,
4072 .ctl_name = 0, /* sentinel */
4077 .ctl_name = NET_PROTO_CONF_ALL,
4080 .child = addrconf_sysctl.addrconf_vars,
4083 .ctl_name = 0, /* sentinel */
4086 .addrconf_conf_dir = {
4088 .ctl_name = NET_IPV6_CONF,
4091 .child = addrconf_sysctl.addrconf_dev,
4094 .ctl_name = 0, /* sentinel */
4097 .addrconf_proto_dir = {
4099 .ctl_name = NET_IPV6,
4102 .child = addrconf_sysctl.addrconf_conf_dir,
4105 .ctl_name = 0, /* sentinel */
4108 .addrconf_root_dir = {
4110 .ctl_name = CTL_NET,
4113 .child = addrconf_sysctl.addrconf_proto_dir,
4116 .ctl_name = 0, /* sentinel */
4121 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
4124 struct net_device *dev = idev ? idev->dev : NULL;
4125 struct addrconf_sysctl_table *t;
4126 char *dev_name = NULL;
4128 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
4131 for (i=0; t->addrconf_vars[i].data; i++) {
4132 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
4133 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4136 dev_name = dev->name;
4137 t->addrconf_dev[0].ctl_name = dev->ifindex;
4139 dev_name = "default";
4140 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
4144 * Make a copy of dev_name, because '.procname' is regarded as const
4145 * by sysctl and we wouldn't want anyone to change it under our feet
4146 * (see SIOCSIFNAME).
4148 dev_name = kstrdup(dev_name, GFP_KERNEL);
4152 t->addrconf_dev[0].procname = dev_name;
4154 t->addrconf_dev[0].child = t->addrconf_vars;
4155 t->addrconf_conf_dir[0].child = t->addrconf_dev;
4156 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
4157 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
4159 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir);
4160 if (t->sysctl_header == NULL)
4175 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
4178 struct addrconf_sysctl_table *t = p->sysctl;
4180 unregister_sysctl_table(t->sysctl_header);
4181 kfree(t->addrconf_dev[0].procname);
4193 int register_inet6addr_notifier(struct notifier_block *nb)
4195 return atomic_notifier_chain_register(&inet6addr_chain, nb);
4198 EXPORT_SYMBOL(register_inet6addr_notifier);
4200 int unregister_inet6addr_notifier(struct notifier_block *nb)
4202 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
4205 EXPORT_SYMBOL(unregister_inet6addr_notifier);
4208 * Init / cleanup code
4211 int __init addrconf_init(void)
4215 if ((err = ipv6_addr_label_init()) < 0) {
4216 printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
4221 /* The addrconf netdev notifier requires that loopback_dev
4222 * has it's ipv6 private information allocated and setup
4223 * before it can bring up and give link-local addresses
4224 * to other devices which are up.
4226 * Unfortunately, loopback_dev is not necessarily the first
4227 * entry in the global dev_base list of net devices. In fact,
4228 * it is likely to be the very last entry on that list.
4229 * So this causes the notifier registry below to try and
4230 * give link-local addresses to all devices besides loopback_dev
4231 * first, then loopback_dev, which cases all the non-loopback_dev
4232 * devices to fail to get a link-local address.
4234 * So, as a temporary fix, allocate the ipv6 structure for
4235 * loopback_dev first by hand.
4236 * Longer term, all of the dependencies ipv6 has upon the loopback
4237 * device and it being up should be removed.
4240 if (!ipv6_add_dev(init_net.loopback_dev))
4246 ip6_null_entry.u.dst.dev = init_net.loopback_dev;
4247 ip6_null_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
4248 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
4249 ip6_prohibit_entry.u.dst.dev = init_net.loopback_dev;
4250 ip6_prohibit_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
4251 ip6_blk_hole_entry.u.dst.dev = init_net.loopback_dev;
4252 ip6_blk_hole_entry.rt6i_idev = in6_dev_get(init_net.loopback_dev);
4255 register_netdevice_notifier(&ipv6_dev_notf);
4259 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4263 /* Only the first call to __rtnl_register can fail */
4264 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4265 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4266 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4267 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4268 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4270 ipv6_addr_label_rtnl_register();
4272 #ifdef CONFIG_SYSCTL
4273 addrconf_sysctl.sysctl_header =
4274 register_sysctl_table(addrconf_sysctl.addrconf_root_dir);
4275 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
4280 unregister_netdevice_notifier(&ipv6_dev_notf);
4285 void __exit addrconf_cleanup(void)
4287 struct net_device *dev;
4288 struct inet6_ifaddr *ifa;
4291 unregister_netdevice_notifier(&ipv6_dev_notf);
4293 #ifdef CONFIG_SYSCTL
4294 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
4295 addrconf_sysctl_unregister(&ipv6_devconf);
4304 for_each_netdev(&init_net, dev) {
4305 if (__in6_dev_get(dev) == NULL)
4307 addrconf_ifdown(dev, 1);
4309 addrconf_ifdown(init_net.loopback_dev, 2);
4315 write_lock_bh(&addrconf_hash_lock);
4316 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4317 for (ifa=inet6_addr_lst[i]; ifa; ) {
4318 struct inet6_ifaddr *bifa;
4321 ifa = ifa->lst_next;
4322 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4323 /* Do not free it; something is wrong.
4324 Now we can investigate it with debugger.
4328 write_unlock_bh(&addrconf_hash_lock);
4330 del_timer(&addr_chk_timer);