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 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
25 * yoshfuji@USAGI : Fixed interval between DAD
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
36 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
41 #include <linux/errno.h>
42 #include <linux/types.h>
43 #include <linux/socket.h>
44 #include <linux/sockios.h>
45 #include <linux/net.h>
46 #include <linux/in6.h>
47 #include <linux/netdevice.h>
48 #include <linux/if_addr.h>
49 #include <linux/if_arp.h>
50 #include <linux/if_arcnet.h>
51 #include <linux/if_infiniband.h>
52 #include <linux/route.h>
53 #include <linux/inetdevice.h>
54 #include <linux/init.h>
56 #include <linux/sysctl.h>
58 #include <linux/capability.h>
59 #include <linux/delay.h>
60 #include <linux/notifier.h>
61 #include <linux/string.h>
63 #include <net/net_namespace.h>
68 #include <net/protocol.h>
69 #include <net/ndisc.h>
70 #include <net/ip6_route.h>
71 #include <net/addrconf.h>
74 #include <net/netlink.h>
75 #include <net/pkt_sched.h>
76 #include <linux/if_tunnel.h>
77 #include <linux/rtnetlink.h>
79 #ifdef CONFIG_IPV6_PRIVACY
80 #include <linux/random.h>
83 #include <asm/uaccess.h>
84 #include <asm/unaligned.h>
86 #include <linux/proc_fs.h>
87 #include <linux/seq_file.h>
89 /* Set to 3 to get tracing... */
93 #define ADBG(x) printk x
98 #define INFINITY_LIFE_TIME 0xFFFFFFFF
99 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
102 static void addrconf_sysctl_register(struct inet6_dev *idev);
103 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
105 static inline void addrconf_sysctl_register(struct inet6_dev *idev)
109 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
114 #ifdef CONFIG_IPV6_PRIVACY
115 static int __ipv6_regen_rndid(struct inet6_dev *idev);
116 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
117 static void ipv6_regen_rndid(unsigned long data);
119 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
122 static int ipv6_count_addresses(struct inet6_dev *idev);
125 * Configured unicast address hash table
127 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
128 static DEFINE_RWLOCK(addrconf_hash_lock);
130 static void addrconf_verify(unsigned long);
132 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
133 static DEFINE_SPINLOCK(addrconf_verify_lock);
135 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
136 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
138 static int addrconf_ifdown(struct net_device *dev, int how);
140 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
141 static void addrconf_dad_timer(unsigned long data);
142 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
143 static void addrconf_dad_run(struct inet6_dev *idev);
144 static void addrconf_rs_timer(unsigned long data);
145 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
146 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
148 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
149 struct prefix_info *pinfo);
150 static int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
151 struct net_device *dev);
153 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
155 struct ipv6_devconf ipv6_devconf __read_mostly = {
157 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
158 .mtu6 = IPV6_MIN_MTU,
160 .accept_redirects = 1,
162 .force_mld_version = 0,
164 .rtr_solicits = MAX_RTR_SOLICITATIONS,
165 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
166 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
167 #ifdef CONFIG_IPV6_PRIVACY
169 .temp_valid_lft = TEMP_VALID_LIFETIME,
170 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
171 .regen_max_retry = REGEN_MAX_RETRY,
172 .max_desync_factor = MAX_DESYNC_FACTOR,
174 .max_addresses = IPV6_MAX_ADDRESSES,
175 .accept_ra_defrtr = 1,
176 .accept_ra_pinfo = 1,
177 #ifdef CONFIG_IPV6_ROUTER_PREF
178 .accept_ra_rtr_pref = 1,
179 .rtr_probe_interval = 60 * HZ,
180 #ifdef CONFIG_IPV6_ROUTE_INFO
181 .accept_ra_rt_info_max_plen = 0,
185 .accept_source_route = 0, /* we do not accept RH0 by default. */
188 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
190 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
191 .mtu6 = IPV6_MIN_MTU,
193 .accept_redirects = 1,
196 .rtr_solicits = MAX_RTR_SOLICITATIONS,
197 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
198 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
199 #ifdef CONFIG_IPV6_PRIVACY
201 .temp_valid_lft = TEMP_VALID_LIFETIME,
202 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
203 .regen_max_retry = REGEN_MAX_RETRY,
204 .max_desync_factor = MAX_DESYNC_FACTOR,
206 .max_addresses = IPV6_MAX_ADDRESSES,
207 .accept_ra_defrtr = 1,
208 .accept_ra_pinfo = 1,
209 #ifdef CONFIG_IPV6_ROUTER_PREF
210 .accept_ra_rtr_pref = 1,
211 .rtr_probe_interval = 60 * HZ,
212 #ifdef CONFIG_IPV6_ROUTE_INFO
213 .accept_ra_rt_info_max_plen = 0,
217 .accept_source_route = 0, /* we do not accept RH0 by default. */
220 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
221 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
222 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
223 const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
224 const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
226 /* Check if a valid qdisc is available */
227 static inline int addrconf_qdisc_ok(struct net_device *dev)
229 return (dev->qdisc != &noop_qdisc);
232 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
234 if (del_timer(&ifp->timer))
238 enum addrconf_timer_t
245 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
246 enum addrconf_timer_t what,
249 if (!del_timer(&ifp->timer))
254 ifp->timer.function = addrconf_dad_timer;
257 ifp->timer.function = addrconf_rs_timer;
261 ifp->timer.expires = jiffies + when;
262 add_timer(&ifp->timer);
265 static int snmp6_alloc_dev(struct inet6_dev *idev)
267 if (snmp_mib_init((void **)idev->stats.ipv6,
268 sizeof(struct ipstats_mib)) < 0)
270 if (snmp_mib_init((void **)idev->stats.icmpv6,
271 sizeof(struct icmpv6_mib)) < 0)
273 if (snmp_mib_init((void **)idev->stats.icmpv6msg,
274 sizeof(struct icmpv6msg_mib)) < 0)
280 snmp_mib_free((void **)idev->stats.icmpv6);
282 snmp_mib_free((void **)idev->stats.ipv6);
287 static void snmp6_free_dev(struct inet6_dev *idev)
289 snmp_mib_free((void **)idev->stats.icmpv6msg);
290 snmp_mib_free((void **)idev->stats.icmpv6);
291 snmp_mib_free((void **)idev->stats.ipv6);
294 /* Nobody refers to this device, we may destroy it. */
296 static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
298 struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
302 void in6_dev_finish_destroy(struct inet6_dev *idev)
304 struct net_device *dev = idev->dev;
305 BUG_TRAP(idev->addr_list==NULL);
306 BUG_TRAP(idev->mc_list==NULL);
307 #ifdef NET_REFCNT_DEBUG
308 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
312 printk("Freeing alive inet6 device %p\n", idev);
315 snmp6_free_dev(idev);
316 call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
319 EXPORT_SYMBOL(in6_dev_finish_destroy);
321 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
323 struct inet6_dev *ndev;
327 if (dev->mtu < IPV6_MIN_MTU)
330 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
335 rwlock_init(&ndev->lock);
337 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
338 ndev->cnf.mtu6 = dev->mtu;
339 ndev->cnf.sysctl = NULL;
340 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
341 if (ndev->nd_parms == NULL) {
345 /* We refer to the device */
348 if (snmp6_alloc_dev(ndev) < 0) {
350 "%s(): cannot allocate memory for statistics; dev=%s.\n",
351 __func__, dev->name));
352 neigh_parms_release(&nd_tbl, ndev->nd_parms);
354 in6_dev_finish_destroy(ndev);
358 if (snmp6_register_dev(ndev) < 0) {
360 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
361 __func__, dev->name));
362 neigh_parms_release(&nd_tbl, ndev->nd_parms);
364 in6_dev_finish_destroy(ndev);
368 /* One reference from device. We must do this before
369 * we invoke __ipv6_regen_rndid().
373 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
374 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
376 "%s: Disabled Multicast RS\n",
378 ndev->cnf.rtr_solicits = 0;
382 #ifdef CONFIG_IPV6_PRIVACY
383 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
384 if ((dev->flags&IFF_LOOPBACK) ||
385 dev->type == ARPHRD_TUNNEL ||
386 dev->type == ARPHRD_TUNNEL6 ||
387 dev->type == ARPHRD_SIT ||
388 dev->type == ARPHRD_NONE) {
390 "%s: Disabled Privacy Extensions\n",
392 ndev->cnf.use_tempaddr = -1;
395 ipv6_regen_rndid((unsigned long) ndev);
399 if (netif_running(dev) && addrconf_qdisc_ok(dev))
400 ndev->if_flags |= IF_READY;
402 ipv6_mc_init_dev(ndev);
403 ndev->tstamp = jiffies;
404 addrconf_sysctl_register(ndev);
405 /* protected by rtnl_lock */
406 rcu_assign_pointer(dev->ip6_ptr, ndev);
408 /* Join all-node multicast group */
409 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
414 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
416 struct inet6_dev *idev;
420 if ((idev = __in6_dev_get(dev)) == NULL) {
421 if ((idev = ipv6_add_dev(dev)) == NULL)
425 if (dev->flags&IFF_UP)
431 static void dev_forward_change(struct inet6_dev *idev)
433 struct net_device *dev;
434 struct inet6_ifaddr *ifa;
439 if (dev && (dev->flags & IFF_MULTICAST)) {
440 if (idev->cnf.forwarding)
441 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
443 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
445 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
446 if (ifa->flags&IFA_F_TENTATIVE)
448 if (idev->cnf.forwarding)
449 addrconf_join_anycast(ifa);
451 addrconf_leave_anycast(ifa);
456 static void addrconf_forward_change(struct net *net, __s32 newf)
458 struct net_device *dev;
459 struct inet6_dev *idev;
461 read_lock(&dev_base_lock);
462 for_each_netdev(net, dev) {
464 idev = __in6_dev_get(dev);
466 int changed = (!idev->cnf.forwarding) ^ (!newf);
467 idev->cnf.forwarding = newf;
469 dev_forward_change(idev);
473 read_unlock(&dev_base_lock);
476 static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
480 net = (struct net *)table->extra2;
481 if (p == &net->ipv6.devconf_dflt->forwarding)
484 if (p == &net->ipv6.devconf_all->forwarding) {
485 __s32 newf = net->ipv6.devconf_all->forwarding;
486 net->ipv6.devconf_dflt->forwarding = newf;
487 addrconf_forward_change(net, newf);
488 } else if ((!*p) ^ (!old))
489 dev_forward_change((struct inet6_dev *)table->extra1);
492 rt6_purge_dflt_routers(net);
496 /* Nobody refers to this ifaddr, destroy it */
498 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
500 BUG_TRAP(ifp->if_next==NULL);
501 BUG_TRAP(ifp->lst_next==NULL);
502 #ifdef NET_REFCNT_DEBUG
503 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
506 in6_dev_put(ifp->idev);
508 if (del_timer(&ifp->timer))
509 printk("Timer is still running, when freeing ifa=%p\n", ifp);
512 printk("Freeing alive inet6 address %p\n", ifp);
515 dst_release(&ifp->rt->u.dst);
521 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
523 struct inet6_ifaddr *ifa, **ifap;
524 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
527 * Each device address list is sorted in order of scope -
528 * global before linklocal.
530 for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
531 ifap = &ifa->if_next) {
532 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
536 ifp->if_next = *ifap;
541 * Hash function taken from net_alias.c
543 static u8 ipv6_addr_hash(const struct in6_addr *addr)
548 * We perform the hash function over the last 64 bits of the address
549 * This will include the IEEE address token on links that support it.
552 word = (__force u32)(addr->s6_addr32[2] ^ addr->s6_addr32[3]);
553 word ^= (word >> 16);
556 return ((word ^ (word >> 4)) & 0x0f);
559 /* On success it returns ifp with increased reference count */
561 static struct inet6_ifaddr *
562 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
563 int scope, u32 flags)
565 struct inet6_ifaddr *ifa = NULL;
572 err = -ENODEV; /*XXX*/
576 write_lock(&addrconf_hash_lock);
578 /* Ignore adding duplicate addresses on an interface */
579 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
580 ADBG(("ipv6_add_addr: already assigned\n"));
585 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
588 ADBG(("ipv6_add_addr: malloc failed\n"));
593 rt = addrconf_dst_alloc(idev, addr, 0);
599 ipv6_addr_copy(&ifa->addr, addr);
601 spin_lock_init(&ifa->lock);
602 init_timer(&ifa->timer);
603 ifa->timer.data = (unsigned long) ifa;
605 ifa->prefix_len = pfxlen;
606 ifa->flags = flags | IFA_F_TENTATIVE;
607 ifa->cstamp = ifa->tstamp = jiffies;
612 * part one of RFC 4429, section 3.3
613 * We should not configure an address as
614 * optimistic if we do not yet know the link
615 * layer address of our nexhop router
618 if (rt->rt6i_nexthop == NULL)
619 ifa->flags &= ~IFA_F_OPTIMISTIC;
626 /* Add to big hash table */
627 hash = ipv6_addr_hash(addr);
629 ifa->lst_next = inet6_addr_lst[hash];
630 inet6_addr_lst[hash] = ifa;
632 write_unlock(&addrconf_hash_lock);
634 write_lock(&idev->lock);
635 /* Add to inet6_dev unicast addr list. */
636 ipv6_link_dev_addr(idev, ifa);
638 #ifdef CONFIG_IPV6_PRIVACY
639 if (ifa->flags&IFA_F_TEMPORARY) {
640 ifa->tmp_next = idev->tempaddr_list;
641 idev->tempaddr_list = ifa;
647 write_unlock(&idev->lock);
649 rcu_read_unlock_bh();
651 if (likely(err == 0))
652 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
660 write_unlock(&addrconf_hash_lock);
664 /* This function wants to get referenced ifp and releases it before return */
666 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
668 struct inet6_ifaddr *ifa, **ifap;
669 struct inet6_dev *idev = ifp->idev;
671 int deleted = 0, onlink = 0;
672 unsigned long expires = jiffies;
674 hash = ipv6_addr_hash(&ifp->addr);
678 write_lock_bh(&addrconf_hash_lock);
679 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
680 ifap = &ifa->lst_next) {
682 *ifap = ifa->lst_next;
684 ifa->lst_next = NULL;
688 write_unlock_bh(&addrconf_hash_lock);
690 write_lock_bh(&idev->lock);
691 #ifdef CONFIG_IPV6_PRIVACY
692 if (ifp->flags&IFA_F_TEMPORARY) {
693 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
694 ifap = &ifa->tmp_next) {
696 *ifap = ifa->tmp_next;
698 in6_ifa_put(ifp->ifpub);
702 ifa->tmp_next = NULL;
709 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
711 *ifap = ifa->if_next;
714 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
718 } else if (ifp->flags & IFA_F_PERMANENT) {
719 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
721 if (ifa->flags & IFA_F_PERMANENT) {
726 unsigned long lifetime;
731 spin_lock(&ifa->lock);
733 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
735 * Note: Because this address is
736 * not permanent, lifetime <
737 * LONG_MAX / HZ here.
739 if (time_before(expires,
740 ifa->tstamp + lifetime * HZ))
741 expires = ifa->tstamp + lifetime * HZ;
742 spin_unlock(&ifa->lock);
746 ifap = &ifa->if_next;
748 write_unlock_bh(&idev->lock);
750 ipv6_ifa_notify(RTM_DELADDR, ifp);
752 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
754 addrconf_del_timer(ifp);
757 * Purge or update corresponding prefix
759 * 1) we don't purge prefix here if address was not permanent.
760 * prefix is managed by its own lifetime.
761 * 2) if there're no addresses, delete prefix.
762 * 3) if there're still other permanent address(es),
763 * corresponding prefix is still permanent.
764 * 4) otherwise, update prefix lifetime to the
765 * longest valid lifetime among the corresponding
766 * addresses on the device.
767 * Note: subsequent RA will update lifetime.
771 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
772 struct in6_addr prefix;
774 struct net *net = dev_net(ifp->idev->dev);
775 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
776 rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1);
778 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
782 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
783 rt->rt6i_expires = expires;
784 rt->rt6i_flags |= RTF_EXPIRES;
787 dst_release(&rt->u.dst);
793 #ifdef CONFIG_IPV6_PRIVACY
794 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
796 struct inet6_dev *idev = ifp->idev;
797 struct in6_addr addr, *tmpaddr;
798 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
799 unsigned long regen_advance;
805 write_lock(&idev->lock);
807 spin_lock_bh(&ift->lock);
808 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
809 spin_unlock_bh(&ift->lock);
816 if (idev->cnf.use_tempaddr <= 0) {
817 write_unlock(&idev->lock);
819 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
824 spin_lock_bh(&ifp->lock);
825 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
826 idev->cnf.use_tempaddr = -1; /*XXX*/
827 spin_unlock_bh(&ifp->lock);
828 write_unlock(&idev->lock);
830 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
836 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
837 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
838 spin_unlock_bh(&ifp->lock);
839 write_unlock(&idev->lock);
841 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
847 memcpy(&addr.s6_addr[8], idev->rndid, 8);
848 tmp_valid_lft = min_t(__u32,
850 idev->cnf.temp_valid_lft);
851 tmp_prefered_lft = min_t(__u32,
853 idev->cnf.temp_prefered_lft - desync_factor / HZ);
854 tmp_plen = ifp->prefix_len;
855 max_addresses = idev->cnf.max_addresses;
856 tmp_cstamp = ifp->cstamp;
857 tmp_tstamp = ifp->tstamp;
858 spin_unlock_bh(&ifp->lock);
860 regen_advance = idev->cnf.regen_max_retry *
861 idev->cnf.dad_transmits *
862 idev->nd_parms->retrans_time / HZ;
863 write_unlock(&idev->lock);
865 /* A temporary address is created only if this calculated Preferred
866 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
867 * an implementation must not create a temporary address with a zero
868 * Preferred Lifetime.
870 if (tmp_prefered_lft <= regen_advance) {
877 addr_flags = IFA_F_TEMPORARY;
878 /* set in addrconf_prefix_rcv() */
879 if (ifp->flags & IFA_F_OPTIMISTIC)
880 addr_flags |= IFA_F_OPTIMISTIC;
882 ift = !max_addresses ||
883 ipv6_count_addresses(idev) < max_addresses ?
884 ipv6_add_addr(idev, &addr, tmp_plen,
885 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
887 if (!ift || IS_ERR(ift)) {
891 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
893 write_lock(&idev->lock);
897 spin_lock_bh(&ift->lock);
899 ift->valid_lft = tmp_valid_lft;
900 ift->prefered_lft = tmp_prefered_lft;
901 ift->cstamp = tmp_cstamp;
902 ift->tstamp = tmp_tstamp;
903 spin_unlock_bh(&ift->lock);
905 addrconf_dad_start(ift, 0);
914 * Choose an appropriate source address (RFC3484)
917 IPV6_SADDR_RULE_INIT = 0,
918 IPV6_SADDR_RULE_LOCAL,
919 IPV6_SADDR_RULE_SCOPE,
920 IPV6_SADDR_RULE_PREFERRED,
921 #ifdef CONFIG_IPV6_MIP6
925 IPV6_SADDR_RULE_LABEL,
926 #ifdef CONFIG_IPV6_PRIVACY
927 IPV6_SADDR_RULE_PRIVACY,
929 IPV6_SADDR_RULE_ORCHID,
930 IPV6_SADDR_RULE_PREFIX,
934 struct ipv6_saddr_score {
937 struct inet6_ifaddr *ifa;
938 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
943 struct ipv6_saddr_dst {
944 const struct in6_addr *addr;
951 static inline int ipv6_saddr_preferred(int type)
953 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
954 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
959 static int ipv6_get_saddr_eval(struct ipv6_saddr_score *score,
960 struct ipv6_saddr_dst *dst,
965 if (i <= score->rule) {
967 case IPV6_SADDR_RULE_SCOPE:
968 ret = score->scopedist;
970 case IPV6_SADDR_RULE_PREFIX:
971 ret = score->matchlen;
974 ret = !!test_bit(i, score->scorebits);
980 case IPV6_SADDR_RULE_INIT:
981 /* Rule 0: remember if hiscore is not ready yet */
984 case IPV6_SADDR_RULE_LOCAL:
985 /* Rule 1: Prefer same address */
986 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
988 case IPV6_SADDR_RULE_SCOPE:
989 /* Rule 2: Prefer appropriate scope
994 * ---+--+-+---> scope
996 * | d is scope of the destination.
998 * | \ <- smaller scope is better if
999 * B-15 | \ if scope is enough for destinaion.
1000 * | ret = B - scope (-1 <= scope >= d <= 15).
1002 * |/ <- greater is better
1003 * -C / if scope is not enough for destination.
1004 * /| ret = scope - C (-1 <= d < scope <= 15).
1006 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1007 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1008 * Assume B = 0 and we get C > 29.
1010 ret = __ipv6_addr_src_scope(score->addr_type);
1011 if (ret >= dst->scope)
1014 ret -= 128; /* 30 is enough */
1015 score->scopedist = ret;
1017 case IPV6_SADDR_RULE_PREFERRED:
1018 /* Rule 3: Avoid deprecated and optimistic addresses */
1019 ret = ipv6_saddr_preferred(score->addr_type) ||
1020 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1022 #ifdef CONFIG_IPV6_MIP6
1023 case IPV6_SADDR_RULE_HOA:
1025 /* Rule 4: Prefer home address */
1026 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1027 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1031 case IPV6_SADDR_RULE_OIF:
1032 /* Rule 5: Prefer outgoing interface */
1033 ret = (!dst->ifindex ||
1034 dst->ifindex == score->ifa->idev->dev->ifindex);
1036 case IPV6_SADDR_RULE_LABEL:
1037 /* Rule 6: Prefer matching label */
1038 ret = ipv6_addr_label(&score->ifa->addr, score->addr_type,
1039 score->ifa->idev->dev->ifindex) == dst->label;
1041 #ifdef CONFIG_IPV6_PRIVACY
1042 case IPV6_SADDR_RULE_PRIVACY:
1044 /* Rule 7: Prefer public address
1045 * Note: prefer temprary address if use_tempaddr >= 2
1047 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1048 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1049 score->ifa->idev->cnf.use_tempaddr >= 2;
1050 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1054 case IPV6_SADDR_RULE_ORCHID:
1055 /* Rule 8-: Prefer ORCHID vs ORCHID or
1056 * non-ORCHID vs non-ORCHID
1058 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1059 ipv6_addr_orchid(dst->addr));
1061 case IPV6_SADDR_RULE_PREFIX:
1062 /* Rule 8: Use longest matching prefix */
1063 score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr,
1071 __set_bit(i, score->scorebits);
1077 int ipv6_dev_get_saddr(struct net_device *dst_dev,
1078 const struct in6_addr *daddr, unsigned int prefs,
1079 struct in6_addr *saddr)
1081 struct ipv6_saddr_score scores[2],
1082 *score = &scores[0], *hiscore = &scores[1];
1083 struct net *net = dev_net(dst_dev);
1084 struct ipv6_saddr_dst dst;
1085 struct net_device *dev;
1088 dst_type = __ipv6_addr_type(daddr);
1090 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1091 dst.scope = __ipv6_addr_src_scope(dst_type);
1092 dst.label = ipv6_addr_label(daddr, dst_type, dst.ifindex);
1096 hiscore->ifa = NULL;
1098 read_lock(&dev_base_lock);
1101 for_each_netdev(net, dev) {
1102 struct inet6_dev *idev;
1104 /* Candidate Source Address (section 4)
1105 * - multicast and link-local destination address,
1106 * the set of candidate source address MUST only
1107 * include addresses assigned to interfaces
1108 * belonging to the same link as the outgoing
1110 * (- For site-local destination addresses, the
1111 * set of candidate source addresses MUST only
1112 * include addresses assigned to interfaces
1113 * belonging to the same site as the outgoing
1116 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1117 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1118 dst.ifindex && dev->ifindex != dst.ifindex)
1121 idev = __in6_dev_get(dev);
1125 read_lock_bh(&idev->lock);
1126 for (score->ifa = idev->addr_list; score->ifa; score->ifa = score->ifa->if_next) {
1130 * - Tentative Address (RFC2462 section 5.4)
1131 * - A tentative address is not considered
1132 * "assigned to an interface" in the traditional
1133 * sense, unless it is also flagged as optimistic.
1134 * - Candidate Source Address (section 4)
1135 * - In any case, anycast addresses, multicast
1136 * addresses, and the unspecified address MUST
1137 * NOT be included in a candidate set.
1139 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1140 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1143 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1145 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1146 score->addr_type & IPV6_ADDR_MULTICAST)) {
1147 LIMIT_NETDEBUG(KERN_DEBUG
1148 "ADDRCONF: unspecified / multicast address "
1149 "assigned as unicast address on %s",
1155 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1157 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1158 int minihiscore, miniscore;
1160 minihiscore = ipv6_get_saddr_eval(hiscore, &dst, i);
1161 miniscore = ipv6_get_saddr_eval(score, &dst, i);
1163 if (minihiscore > miniscore) {
1164 if (i == IPV6_SADDR_RULE_SCOPE &&
1165 score->scopedist > 0) {
1168 * each remaining entry
1169 * has too small (not enough)
1170 * scope, because ifa entries
1171 * are sorted by their scope
1177 } else if (minihiscore < miniscore) {
1178 struct ipv6_saddr_score *tmp;
1181 in6_ifa_put(hiscore->ifa);
1183 in6_ifa_hold(score->ifa);
1189 /* restore our iterator */
1190 score->ifa = hiscore->ifa;
1197 read_unlock_bh(&idev->lock);
1200 read_unlock(&dev_base_lock);
1203 return -EADDRNOTAVAIL;
1205 ipv6_addr_copy(saddr, &hiscore->ifa->addr);
1206 in6_ifa_put(hiscore->ifa);
1210 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1212 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1213 unsigned char banned_flags)
1215 struct inet6_dev *idev;
1216 int err = -EADDRNOTAVAIL;
1219 if ((idev = __in6_dev_get(dev)) != NULL) {
1220 struct inet6_ifaddr *ifp;
1222 read_lock_bh(&idev->lock);
1223 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1224 if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) {
1225 ipv6_addr_copy(addr, &ifp->addr);
1230 read_unlock_bh(&idev->lock);
1236 static int ipv6_count_addresses(struct inet6_dev *idev)
1239 struct inet6_ifaddr *ifp;
1241 read_lock_bh(&idev->lock);
1242 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1244 read_unlock_bh(&idev->lock);
1248 int ipv6_chk_addr(struct net *net, struct in6_addr *addr,
1249 struct net_device *dev, int strict)
1251 struct inet6_ifaddr * ifp;
1252 u8 hash = ipv6_addr_hash(addr);
1254 read_lock_bh(&addrconf_hash_lock);
1255 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1256 if (!net_eq(dev_net(ifp->idev->dev), net))
1258 if (ipv6_addr_equal(&ifp->addr, addr) &&
1259 !(ifp->flags&IFA_F_TENTATIVE)) {
1260 if (dev == NULL || ifp->idev->dev == dev ||
1261 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1265 read_unlock_bh(&addrconf_hash_lock);
1268 EXPORT_SYMBOL(ipv6_chk_addr);
1271 int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1272 struct net_device *dev)
1274 struct inet6_ifaddr * ifp;
1275 u8 hash = ipv6_addr_hash(addr);
1277 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1278 if (!net_eq(dev_net(ifp->idev->dev), net))
1280 if (ipv6_addr_equal(&ifp->addr, addr)) {
1281 if (dev == NULL || ifp->idev->dev == dev)
1288 int ipv6_chk_prefix(struct in6_addr *addr, struct net_device *dev)
1290 struct inet6_dev *idev;
1291 struct inet6_ifaddr *ifa;
1296 idev = __in6_dev_get(dev);
1298 read_lock_bh(&idev->lock);
1299 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
1300 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1305 read_unlock_bh(&idev->lock);
1311 EXPORT_SYMBOL(ipv6_chk_prefix);
1313 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
1314 struct net_device *dev, int strict)
1316 struct inet6_ifaddr * ifp;
1317 u8 hash = ipv6_addr_hash(addr);
1319 read_lock_bh(&addrconf_hash_lock);
1320 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1321 if (!net_eq(dev_net(ifp->idev->dev), net))
1323 if (ipv6_addr_equal(&ifp->addr, addr)) {
1324 if (dev == NULL || ifp->idev->dev == dev ||
1325 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1331 read_unlock_bh(&addrconf_hash_lock);
1336 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1338 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1339 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
1340 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1341 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1342 int sk_ipv6only = ipv6_only_sock(sk);
1343 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1344 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1345 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1347 if (!sk2_rcv_saddr && !sk_ipv6only)
1350 if (addr_type2 == IPV6_ADDR_ANY &&
1351 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1354 if (addr_type == IPV6_ADDR_ANY &&
1355 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1358 if (sk2_rcv_saddr6 &&
1359 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1362 if (addr_type == IPV6_ADDR_MAPPED &&
1364 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1370 /* Gets referenced address, destroys ifaddr */
1372 static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
1374 if (ifp->flags&IFA_F_PERMANENT) {
1375 spin_lock_bh(&ifp->lock);
1376 addrconf_del_timer(ifp);
1377 ifp->flags |= IFA_F_TENTATIVE;
1378 spin_unlock_bh(&ifp->lock);
1380 #ifdef CONFIG_IPV6_PRIVACY
1381 } else if (ifp->flags&IFA_F_TEMPORARY) {
1382 struct inet6_ifaddr *ifpub;
1383 spin_lock_bh(&ifp->lock);
1386 in6_ifa_hold(ifpub);
1387 spin_unlock_bh(&ifp->lock);
1388 ipv6_create_tempaddr(ifpub, ifp);
1391 spin_unlock_bh(&ifp->lock);
1399 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1401 if (net_ratelimit())
1402 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1403 addrconf_dad_stop(ifp);
1406 /* Join to solicited addr multicast group. */
1408 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1410 struct in6_addr maddr;
1412 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1415 addrconf_addr_solict_mult(addr, &maddr);
1416 ipv6_dev_mc_inc(dev, &maddr);
1419 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1421 struct in6_addr maddr;
1423 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1426 addrconf_addr_solict_mult(addr, &maddr);
1427 __ipv6_dev_mc_dec(idev, &maddr);
1430 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1432 struct in6_addr addr;
1433 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1434 if (ipv6_addr_any(&addr))
1436 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1439 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1441 struct in6_addr addr;
1442 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1443 if (ipv6_addr_any(&addr))
1445 __ipv6_dev_ac_dec(ifp->idev, &addr);
1448 static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1450 if (dev->addr_len != ETH_ALEN)
1452 memcpy(eui, dev->dev_addr, 3);
1453 memcpy(eui + 5, dev->dev_addr + 3, 3);
1456 * The zSeries OSA network cards can be shared among various
1457 * OS instances, but the OSA cards have only one MAC address.
1458 * This leads to duplicate address conflicts in conjunction
1459 * with IPv6 if more than one instance uses the same card.
1461 * The driver for these cards can deliver a unique 16-bit
1462 * identifier for each instance sharing the same card. It is
1463 * placed instead of 0xFFFE in the interface identifier. The
1464 * "u" bit of the interface identifier is not inverted in this
1465 * case. Hence the resulting interface identifier has local
1466 * scope according to RFC2373.
1469 eui[3] = (dev->dev_id >> 8) & 0xFF;
1470 eui[4] = dev->dev_id & 0xFF;
1479 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1481 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1482 if (dev->addr_len != ARCNET_ALEN)
1485 eui[7] = *(u8*)dev->dev_addr;
1489 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1491 if (dev->addr_len != INFINIBAND_ALEN)
1493 memcpy(eui, dev->dev_addr + 12, 8);
1498 int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1500 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1501 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1502 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1503 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1504 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1505 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1509 memcpy(eui + 4, &addr, 4);
1512 EXPORT_SYMBOL(__ipv6_isatap_ifid);
1514 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1516 if (dev->priv_flags & IFF_ISATAP)
1517 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1521 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1523 switch (dev->type) {
1526 case ARPHRD_IEEE802_TR:
1527 return addrconf_ifid_eui48(eui, dev);
1529 return addrconf_ifid_arcnet(eui, dev);
1530 case ARPHRD_INFINIBAND:
1531 return addrconf_ifid_infiniband(eui, dev);
1533 return addrconf_ifid_sit(eui, dev);
1538 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1541 struct inet6_ifaddr *ifp;
1543 read_lock_bh(&idev->lock);
1544 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1545 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1546 memcpy(eui, ifp->addr.s6_addr+8, 8);
1551 read_unlock_bh(&idev->lock);
1555 #ifdef CONFIG_IPV6_PRIVACY
1556 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1557 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1560 get_random_bytes(idev->rndid, sizeof(idev->rndid));
1561 idev->rndid[0] &= ~0x02;
1564 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1565 * check if generated address is not inappropriate
1567 * - Reserved subnet anycast (RFC 2526)
1568 * 11111101 11....11 1xxxxxxx
1569 * - ISATAP (RFC4214) 6.1
1570 * 00-00-5E-FE-xx-xx-xx-xx
1572 * - XXX: already assigned to an address on the device
1574 if (idev->rndid[0] == 0xfd &&
1575 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1576 (idev->rndid[7]&0x80))
1578 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1579 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1581 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1588 static void ipv6_regen_rndid(unsigned long data)
1590 struct inet6_dev *idev = (struct inet6_dev *) data;
1591 unsigned long expires;
1594 write_lock_bh(&idev->lock);
1599 if (__ipv6_regen_rndid(idev) < 0)
1603 idev->cnf.temp_prefered_lft * HZ -
1604 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1605 if (time_before(expires, jiffies)) {
1607 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1612 if (!mod_timer(&idev->regen_timer, expires))
1616 write_unlock_bh(&idev->lock);
1617 rcu_read_unlock_bh();
1621 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1624 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1625 ret = __ipv6_regen_rndid(idev);
1635 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1636 unsigned long expires, u32 flags)
1638 struct fib6_config cfg = {
1639 .fc_table = RT6_TABLE_PREFIX,
1640 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1641 .fc_ifindex = dev->ifindex,
1642 .fc_expires = expires,
1644 .fc_flags = RTF_UP | flags,
1645 .fc_nlinfo.nl_net = dev_net(dev),
1648 ipv6_addr_copy(&cfg.fc_dst, pfx);
1650 /* Prevent useless cloning on PtP SIT.
1651 This thing is done here expecting that the whole
1652 class of non-broadcast devices need not cloning.
1654 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1655 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1656 cfg.fc_flags |= RTF_NONEXTHOP;
1659 ip6_route_add(&cfg);
1662 /* Create "default" multicast route to the interface */
1664 static void addrconf_add_mroute(struct net_device *dev)
1666 struct fib6_config cfg = {
1667 .fc_table = RT6_TABLE_LOCAL,
1668 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1669 .fc_ifindex = dev->ifindex,
1672 .fc_nlinfo.nl_net = dev_net(dev),
1675 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1677 ip6_route_add(&cfg);
1680 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1681 static void sit_route_add(struct net_device *dev)
1683 struct fib6_config cfg = {
1684 .fc_table = RT6_TABLE_MAIN,
1685 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1686 .fc_ifindex = dev->ifindex,
1688 .fc_flags = RTF_UP | RTF_NONEXTHOP,
1689 .fc_nlinfo.nl_net = dev_net(dev),
1692 /* prefix length - 96 bits "::d.d.d.d" */
1693 ip6_route_add(&cfg);
1697 static void addrconf_add_lroute(struct net_device *dev)
1699 struct in6_addr addr;
1701 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1702 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1705 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1707 struct inet6_dev *idev;
1711 if ((idev = ipv6_find_idev(dev)) == NULL)
1714 /* Add default multicast route */
1715 addrconf_add_mroute(dev);
1717 /* Add link local route */
1718 addrconf_add_lroute(dev);
1722 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1724 struct prefix_info *pinfo;
1728 struct inet6_dev *in6_dev;
1730 pinfo = (struct prefix_info *) opt;
1732 if (len < sizeof(struct prefix_info)) {
1733 ADBG(("addrconf: prefix option too short\n"));
1738 * Validation checks ([ADDRCONF], page 19)
1741 addr_type = ipv6_addr_type(&pinfo->prefix);
1743 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1746 valid_lft = ntohl(pinfo->valid);
1747 prefered_lft = ntohl(pinfo->prefered);
1749 if (prefered_lft > valid_lft) {
1750 if (net_ratelimit())
1751 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1755 in6_dev = in6_dev_get(dev);
1757 if (in6_dev == NULL) {
1758 if (net_ratelimit())
1759 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1764 * Two things going on here:
1765 * 1) Add routes for on-link prefixes
1766 * 2) Configure prefixes with the auto flag set
1769 if (pinfo->onlink) {
1770 struct rt6_info *rt;
1771 unsigned long rt_expires;
1773 /* Avoid arithmetic overflow. Really, we could
1774 * save rt_expires in seconds, likely valid_lft,
1775 * but it would require division in fib gc, that it
1779 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
1781 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
1783 if (addrconf_finite_timeout(rt_expires))
1786 rt = rt6_lookup(dev_net(dev), &pinfo->prefix, NULL,
1789 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1790 /* Autoconf prefix route */
1791 if (valid_lft == 0) {
1794 } else if (addrconf_finite_timeout(rt_expires)) {
1796 rt->rt6i_expires = jiffies + rt_expires;
1797 rt->rt6i_flags |= RTF_EXPIRES;
1799 rt->rt6i_flags &= ~RTF_EXPIRES;
1800 rt->rt6i_expires = 0;
1802 } else if (valid_lft) {
1803 clock_t expires = 0;
1804 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
1805 if (addrconf_finite_timeout(rt_expires)) {
1807 flags |= RTF_EXPIRES;
1808 expires = jiffies_to_clock_t(rt_expires);
1810 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1811 dev, expires, flags);
1814 dst_release(&rt->u.dst);
1817 /* Try to figure out our local address for this prefix */
1819 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1820 struct inet6_ifaddr * ifp;
1821 struct in6_addr addr;
1822 int create = 0, update_lft = 0;
1824 if (pinfo->prefix_len == 64) {
1825 memcpy(&addr, &pinfo->prefix, 8);
1826 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1827 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1828 in6_dev_put(in6_dev);
1833 if (net_ratelimit())
1834 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1836 in6_dev_put(in6_dev);
1841 ifp = ipv6_get_ifaddr(dev_net(dev), &addr, dev, 1);
1843 if (ifp == NULL && valid_lft) {
1844 int max_addresses = in6_dev->cnf.max_addresses;
1847 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1848 if (in6_dev->cnf.optimistic_dad &&
1849 !ipv6_devconf.forwarding)
1850 addr_flags = IFA_F_OPTIMISTIC;
1853 /* Do not allow to create too much of autoconfigured
1854 * addresses; this would be too easy way to crash kernel.
1856 if (!max_addresses ||
1857 ipv6_count_addresses(in6_dev) < max_addresses)
1858 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1859 addr_type&IPV6_ADDR_SCOPE_MASK,
1862 if (!ifp || IS_ERR(ifp)) {
1863 in6_dev_put(in6_dev);
1867 update_lft = create = 1;
1868 ifp->cstamp = jiffies;
1869 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1875 #ifdef CONFIG_IPV6_PRIVACY
1876 struct inet6_ifaddr *ift;
1880 /* update lifetime (RFC2462 5.5.3 e) */
1881 spin_lock(&ifp->lock);
1883 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1884 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1887 if (!update_lft && stored_lft) {
1888 if (valid_lft > MIN_VALID_LIFETIME ||
1889 valid_lft > stored_lft)
1891 else if (stored_lft <= MIN_VALID_LIFETIME) {
1892 /* valid_lft <= stored_lft is always true */
1896 valid_lft = MIN_VALID_LIFETIME;
1897 if (valid_lft < prefered_lft)
1898 prefered_lft = valid_lft;
1904 ifp->valid_lft = valid_lft;
1905 ifp->prefered_lft = prefered_lft;
1908 ifp->flags &= ~IFA_F_DEPRECATED;
1909 spin_unlock(&ifp->lock);
1911 if (!(flags&IFA_F_TENTATIVE))
1912 ipv6_ifa_notify(0, ifp);
1914 spin_unlock(&ifp->lock);
1916 #ifdef CONFIG_IPV6_PRIVACY
1917 read_lock_bh(&in6_dev->lock);
1918 /* update all temporary addresses in the list */
1919 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1921 * When adjusting the lifetimes of an existing
1922 * temporary address, only lower the lifetimes.
1923 * Implementations must not increase the
1924 * lifetimes of an existing temporary address
1925 * when processing a Prefix Information Option.
1927 if (ifp != ift->ifpub)
1930 spin_lock(&ift->lock);
1932 if (ift->valid_lft > valid_lft &&
1933 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1934 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1935 if (ift->prefered_lft > prefered_lft &&
1936 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1937 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1938 spin_unlock(&ift->lock);
1939 if (!(flags&IFA_F_TENTATIVE))
1940 ipv6_ifa_notify(0, ift);
1943 if (create && in6_dev->cnf.use_tempaddr > 0) {
1945 * When a new public address is created as described in [ADDRCONF],
1946 * also create a new temporary address.
1948 read_unlock_bh(&in6_dev->lock);
1949 ipv6_create_tempaddr(ifp, NULL);
1951 read_unlock_bh(&in6_dev->lock);
1958 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1959 in6_dev_put(in6_dev);
1963 * Set destination address.
1964 * Special case for SIT interfaces where we create a new "virtual"
1967 int addrconf_set_dstaddr(struct net *net, void __user *arg)
1969 struct in6_ifreq ireq;
1970 struct net_device *dev;
1976 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1979 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
1985 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1986 if (dev->type == ARPHRD_SIT) {
1989 struct ip_tunnel_parm p;
1991 err = -EADDRNOTAVAIL;
1992 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1995 memset(&p, 0, sizeof(p));
1996 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2000 p.iph.protocol = IPPROTO_IPV6;
2002 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
2004 oldfs = get_fs(); set_fs(KERNEL_DS);
2005 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2010 dev = __dev_get_by_name(net, p.name);
2013 err = dev_open(dev);
2024 * Manual configuration of address on an interface
2026 static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx,
2027 unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2030 struct inet6_ifaddr *ifp;
2031 struct inet6_dev *idev;
2032 struct net_device *dev;
2036 unsigned long timeout;
2043 /* check the lifetime */
2044 if (!valid_lft || prefered_lft > valid_lft)
2047 dev = __dev_get_by_index(net, ifindex);
2051 if ((idev = addrconf_add_dev(dev)) == NULL)
2054 scope = ipv6_addr_scope(pfx);
2056 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2057 if (addrconf_finite_timeout(timeout)) {
2058 expires = jiffies_to_clock_t(timeout * HZ);
2059 valid_lft = timeout;
2060 flags = RTF_EXPIRES;
2064 ifa_flags |= IFA_F_PERMANENT;
2067 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2068 if (addrconf_finite_timeout(timeout)) {
2070 ifa_flags |= IFA_F_DEPRECATED;
2071 prefered_lft = timeout;
2074 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
2077 spin_lock_bh(&ifp->lock);
2078 ifp->valid_lft = valid_lft;
2079 ifp->prefered_lft = prefered_lft;
2080 ifp->tstamp = jiffies;
2081 spin_unlock_bh(&ifp->lock);
2083 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2086 * Note that section 3.1 of RFC 4429 indicates
2087 * that the Optimistic flag should not be set for
2088 * manually configured addresses
2090 addrconf_dad_start(ifp, 0);
2096 return PTR_ERR(ifp);
2099 static int inet6_addr_del(struct net *net, int ifindex, struct in6_addr *pfx,
2102 struct inet6_ifaddr *ifp;
2103 struct inet6_dev *idev;
2104 struct net_device *dev;
2109 dev = __dev_get_by_index(net, ifindex);
2113 if ((idev = __in6_dev_get(dev)) == NULL)
2116 read_lock_bh(&idev->lock);
2117 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
2118 if (ifp->prefix_len == plen &&
2119 ipv6_addr_equal(pfx, &ifp->addr)) {
2121 read_unlock_bh(&idev->lock);
2125 /* If the last address is deleted administratively,
2126 disable IPv6 on this interface.
2128 if (idev->addr_list == NULL)
2129 addrconf_ifdown(idev->dev, 1);
2133 read_unlock_bh(&idev->lock);
2134 return -EADDRNOTAVAIL;
2138 int addrconf_add_ifaddr(struct net *net, void __user *arg)
2140 struct in6_ifreq ireq;
2143 if (!capable(CAP_NET_ADMIN))
2146 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2150 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2151 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2152 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2157 int addrconf_del_ifaddr(struct net *net, void __user *arg)
2159 struct in6_ifreq ireq;
2162 if (!capable(CAP_NET_ADMIN))
2165 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2169 err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2170 ireq.ifr6_prefixlen);
2175 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2176 static void sit_add_v4_addrs(struct inet6_dev *idev)
2178 struct inet6_ifaddr * ifp;
2179 struct in6_addr addr;
2180 struct net_device *dev;
2181 struct net *net = dev_net(idev->dev);
2186 memset(&addr, 0, sizeof(struct in6_addr));
2187 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2189 if (idev->dev->flags&IFF_POINTOPOINT) {
2190 addr.s6_addr32[0] = htonl(0xfe800000);
2193 scope = IPV6_ADDR_COMPATv4;
2196 if (addr.s6_addr32[3]) {
2197 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
2199 spin_lock_bh(&ifp->lock);
2200 ifp->flags &= ~IFA_F_TENTATIVE;
2201 spin_unlock_bh(&ifp->lock);
2202 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2208 for_each_netdev(net, dev) {
2209 struct in_device * in_dev = __in_dev_get_rtnl(dev);
2210 if (in_dev && (dev->flags & IFF_UP)) {
2211 struct in_ifaddr * ifa;
2215 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2218 addr.s6_addr32[3] = ifa->ifa_local;
2220 if (ifa->ifa_scope == RT_SCOPE_LINK)
2222 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2223 if (idev->dev->flags&IFF_POINTOPOINT)
2227 if (idev->dev->flags&IFF_POINTOPOINT)
2232 ifp = ipv6_add_addr(idev, &addr, plen, flag,
2235 spin_lock_bh(&ifp->lock);
2236 ifp->flags &= ~IFA_F_TENTATIVE;
2237 spin_unlock_bh(&ifp->lock);
2238 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2247 static void init_loopback(struct net_device *dev)
2249 struct inet6_dev *idev;
2250 struct inet6_ifaddr * ifp;
2256 if ((idev = ipv6_find_idev(dev)) == NULL) {
2257 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2261 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2263 spin_lock_bh(&ifp->lock);
2264 ifp->flags &= ~IFA_F_TENTATIVE;
2265 spin_unlock_bh(&ifp->lock);
2266 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2271 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2273 struct inet6_ifaddr * ifp;
2274 u32 addr_flags = IFA_F_PERMANENT;
2276 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2277 if (idev->cnf.optimistic_dad &&
2278 !ipv6_devconf.forwarding)
2279 addr_flags |= IFA_F_OPTIMISTIC;
2283 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
2285 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2286 addrconf_dad_start(ifp, 0);
2291 static void addrconf_dev_config(struct net_device *dev)
2293 struct in6_addr addr;
2294 struct inet6_dev * idev;
2298 if ((dev->type != ARPHRD_ETHER) &&
2299 (dev->type != ARPHRD_FDDI) &&
2300 (dev->type != ARPHRD_IEEE802_TR) &&
2301 (dev->type != ARPHRD_ARCNET) &&
2302 (dev->type != ARPHRD_INFINIBAND)) {
2303 /* Alas, we support only Ethernet autoconfiguration. */
2307 idev = addrconf_add_dev(dev);
2311 memset(&addr, 0, sizeof(struct in6_addr));
2312 addr.s6_addr32[0] = htonl(0xFE800000);
2314 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2315 addrconf_add_linklocal(idev, &addr);
2318 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2319 static void addrconf_sit_config(struct net_device *dev)
2321 struct inet6_dev *idev;
2326 * Configure the tunnel with one of our IPv4
2327 * addresses... we should configure all of
2328 * our v4 addrs in the tunnel
2331 if ((idev = ipv6_find_idev(dev)) == NULL) {
2332 printk(KERN_DEBUG "init sit: add_dev failed\n");
2336 if (dev->priv_flags & IFF_ISATAP) {
2337 struct in6_addr addr;
2339 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2340 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2341 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2342 addrconf_add_linklocal(idev, &addr);
2346 sit_add_v4_addrs(idev);
2348 if (dev->flags&IFF_POINTOPOINT) {
2349 addrconf_add_mroute(dev);
2350 addrconf_add_lroute(dev);
2357 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2359 struct in6_addr lladdr;
2361 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
2362 addrconf_add_linklocal(idev, &lladdr);
2368 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2370 struct net_device *link_dev;
2371 struct net *net = dev_net(idev->dev);
2373 /* first try to inherit the link-local address from the link device */
2374 if (idev->dev->iflink &&
2375 (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
2376 if (!ipv6_inherit_linklocal(idev, link_dev))
2379 /* then try to inherit it from any device */
2380 for_each_netdev(net, link_dev) {
2381 if (!ipv6_inherit_linklocal(idev, link_dev))
2384 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2388 * Autoconfigure tunnel with a link-local address so routing protocols,
2389 * DHCPv6, MLD etc. can be run over the virtual link
2392 static void addrconf_ip6_tnl_config(struct net_device *dev)
2394 struct inet6_dev *idev;
2398 if ((idev = addrconf_add_dev(dev)) == NULL) {
2399 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2402 ip6_tnl_add_linklocal(idev);
2405 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2408 struct net_device *dev = (struct net_device *) data;
2409 struct inet6_dev *idev = __in6_dev_get(dev);
2410 int run_pending = 0;
2414 case NETDEV_REGISTER:
2415 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2416 idev = ipv6_add_dev(dev);
2418 return notifier_from_errno(-ENOMEM);
2423 if (dev->flags & IFF_SLAVE)
2426 if (event == NETDEV_UP) {
2427 if (!addrconf_qdisc_ok(dev)) {
2428 /* device is not ready yet. */
2430 "ADDRCONF(NETDEV_UP): %s: "
2431 "link is not ready\n",
2436 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2437 idev = ipv6_add_dev(dev);
2440 idev->if_flags |= IF_READY;
2442 if (!addrconf_qdisc_ok(dev)) {
2443 /* device is still not ready. */
2448 if (idev->if_flags & IF_READY) {
2449 /* device is already configured. */
2452 idev->if_flags |= IF_READY;
2456 "ADDRCONF(NETDEV_CHANGE): %s: "
2457 "link becomes ready\n",
2464 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2466 addrconf_sit_config(dev);
2469 case ARPHRD_TUNNEL6:
2470 addrconf_ip6_tnl_config(dev);
2472 case ARPHRD_LOOPBACK:
2477 addrconf_dev_config(dev);
2482 addrconf_dad_run(idev);
2484 /* If the MTU changed during the interface down, when the
2485 interface up, the changed MTU must be reflected in the
2486 idev as well as routers.
2488 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2489 rt6_mtu_change(dev, dev->mtu);
2490 idev->cnf.mtu6 = dev->mtu;
2492 idev->tstamp = jiffies;
2493 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2494 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2495 stop IPv6 on this interface.
2497 if (dev->mtu < IPV6_MIN_MTU)
2498 addrconf_ifdown(dev, event != NETDEV_DOWN);
2502 case NETDEV_CHANGEMTU:
2503 if (idev && dev->mtu >= IPV6_MIN_MTU) {
2504 rt6_mtu_change(dev, dev->mtu);
2505 idev->cnf.mtu6 = dev->mtu;
2509 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2510 idev = ipv6_add_dev(dev);
2515 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2518 case NETDEV_UNREGISTER:
2520 * Remove all addresses from this interface.
2522 addrconf_ifdown(dev, event != NETDEV_DOWN);
2525 case NETDEV_CHANGENAME:
2527 snmp6_unregister_dev(idev);
2528 addrconf_sysctl_unregister(idev);
2529 addrconf_sysctl_register(idev);
2530 err = snmp6_register_dev(idev);
2532 return notifier_from_errno(err);
2541 * addrconf module should be notified of a device going up
2543 static struct notifier_block ipv6_dev_notf = {
2544 .notifier_call = addrconf_notify,
2548 static int addrconf_ifdown(struct net_device *dev, int how)
2550 struct inet6_dev *idev;
2551 struct inet6_ifaddr *ifa, **bifa;
2552 struct net *net = dev_net(dev);
2557 if ((dev->flags & IFF_LOOPBACK) && how == 1)
2560 rt6_ifdown(net, dev);
2561 neigh_ifdown(&nd_tbl, dev);
2563 idev = __in6_dev_get(dev);
2567 /* Step 1: remove reference to ipv6 device from parent device.
2573 /* protected by rtnl_lock */
2574 rcu_assign_pointer(dev->ip6_ptr, NULL);
2576 /* Step 1.5: remove snmp6 entry */
2577 snmp6_unregister_dev(idev);
2581 /* Step 2: clear hash table */
2582 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2583 bifa = &inet6_addr_lst[i];
2585 write_lock_bh(&addrconf_hash_lock);
2586 while ((ifa = *bifa) != NULL) {
2587 if (ifa->idev == idev) {
2588 *bifa = ifa->lst_next;
2589 ifa->lst_next = NULL;
2590 addrconf_del_timer(ifa);
2594 bifa = &ifa->lst_next;
2596 write_unlock_bh(&addrconf_hash_lock);
2599 write_lock_bh(&idev->lock);
2601 /* Step 3: clear flags for stateless addrconf */
2603 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2605 /* Step 4: clear address list */
2606 #ifdef CONFIG_IPV6_PRIVACY
2607 if (how && del_timer(&idev->regen_timer))
2610 /* clear tempaddr list */
2611 while ((ifa = idev->tempaddr_list) != NULL) {
2612 idev->tempaddr_list = ifa->tmp_next;
2613 ifa->tmp_next = NULL;
2615 write_unlock_bh(&idev->lock);
2616 spin_lock_bh(&ifa->lock);
2619 in6_ifa_put(ifa->ifpub);
2622 spin_unlock_bh(&ifa->lock);
2624 write_lock_bh(&idev->lock);
2627 while ((ifa = idev->addr_list) != NULL) {
2628 idev->addr_list = ifa->if_next;
2629 ifa->if_next = NULL;
2631 addrconf_del_timer(ifa);
2632 write_unlock_bh(&idev->lock);
2634 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2635 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
2638 write_lock_bh(&idev->lock);
2640 write_unlock_bh(&idev->lock);
2642 /* Step 5: Discard multicast list */
2645 ipv6_mc_destroy_dev(idev);
2649 idev->tstamp = jiffies;
2651 /* Shot the device (if unregistered) */
2654 addrconf_sysctl_unregister(idev);
2655 neigh_parms_release(&nd_tbl, idev->nd_parms);
2656 neigh_ifdown(&nd_tbl, dev);
2662 static void addrconf_rs_timer(unsigned long data)
2664 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2666 if (ifp->idev->cnf.forwarding)
2669 if (ifp->idev->if_flags & IF_RA_RCVD) {
2671 * Announcement received after solicitation
2677 spin_lock(&ifp->lock);
2678 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2679 /* The wait after the last probe can be shorter */
2680 addrconf_mod_timer(ifp, AC_RS,
2681 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2682 ifp->idev->cnf.rtr_solicit_delay :
2683 ifp->idev->cnf.rtr_solicit_interval);
2684 spin_unlock(&ifp->lock);
2686 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
2688 spin_unlock(&ifp->lock);
2690 * Note: we do not support deprecated "all on-link"
2691 * assumption any longer.
2693 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2694 ifp->idev->dev->name);
2702 * Duplicate Address Detection
2704 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2706 unsigned long rand_num;
2707 struct inet6_dev *idev = ifp->idev;
2709 if (ifp->flags & IFA_F_OPTIMISTIC)
2712 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2714 ifp->probes = idev->cnf.dad_transmits;
2715 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2718 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2720 struct inet6_dev *idev = ifp->idev;
2721 struct net_device *dev = idev->dev;
2723 addrconf_join_solict(dev, &ifp->addr);
2725 net_srandom(ifp->addr.s6_addr32[3]);
2727 read_lock_bh(&idev->lock);
2730 spin_lock_bh(&ifp->lock);
2732 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2733 !(ifp->flags&IFA_F_TENTATIVE) ||
2734 ifp->flags & IFA_F_NODAD) {
2735 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
2736 spin_unlock_bh(&ifp->lock);
2737 read_unlock_bh(&idev->lock);
2739 addrconf_dad_completed(ifp);
2743 if (!(idev->if_flags & IF_READY)) {
2744 spin_unlock_bh(&ifp->lock);
2745 read_unlock_bh(&idev->lock);
2747 * If the defice is not ready:
2748 * - keep it tentative if it is a permanent address.
2749 * - otherwise, kill it.
2752 addrconf_dad_stop(ifp);
2757 * Optimistic nodes can start receiving
2760 if(ifp->flags & IFA_F_OPTIMISTIC)
2761 ip6_ins_rt(ifp->rt);
2763 addrconf_dad_kick(ifp);
2764 spin_unlock_bh(&ifp->lock);
2766 read_unlock_bh(&idev->lock);
2769 static void addrconf_dad_timer(unsigned long data)
2771 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2772 struct inet6_dev *idev = ifp->idev;
2773 struct in6_addr mcaddr;
2775 read_lock_bh(&idev->lock);
2777 read_unlock_bh(&idev->lock);
2780 spin_lock_bh(&ifp->lock);
2781 if (ifp->probes == 0) {
2783 * DAD was successful
2786 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
2787 spin_unlock_bh(&ifp->lock);
2788 read_unlock_bh(&idev->lock);
2790 addrconf_dad_completed(ifp);
2796 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2797 spin_unlock_bh(&ifp->lock);
2798 read_unlock_bh(&idev->lock);
2800 /* send a neighbour solicitation for our addr */
2801 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2802 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
2807 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2809 struct net_device * dev = ifp->idev->dev;
2812 * Configure the address for reception. Now it is valid.
2815 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2817 /* If added prefix is link local and forwarding is off,
2818 start sending router solicitations.
2821 if (ifp->idev->cnf.forwarding == 0 &&
2822 ifp->idev->cnf.rtr_solicits > 0 &&
2823 (dev->flags&IFF_LOOPBACK) == 0 &&
2824 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2826 * If a host as already performed a random delay
2827 * [...] as part of DAD [...] there is no need
2828 * to delay again before sending the first RS
2830 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
2832 spin_lock_bh(&ifp->lock);
2834 ifp->idev->if_flags |= IF_RS_SENT;
2835 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2836 spin_unlock_bh(&ifp->lock);
2840 static void addrconf_dad_run(struct inet6_dev *idev) {
2841 struct inet6_ifaddr *ifp;
2843 read_lock_bh(&idev->lock);
2844 for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2845 spin_lock_bh(&ifp->lock);
2846 if (!(ifp->flags & IFA_F_TENTATIVE)) {
2847 spin_unlock_bh(&ifp->lock);
2850 spin_unlock_bh(&ifp->lock);
2851 addrconf_dad_kick(ifp);
2853 read_unlock_bh(&idev->lock);
2856 #ifdef CONFIG_PROC_FS
2857 struct if6_iter_state {
2858 struct seq_net_private p;
2862 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2864 struct inet6_ifaddr *ifa = NULL;
2865 struct if6_iter_state *state = seq->private;
2866 struct net *net = seq_file_net(seq);
2868 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2869 ifa = inet6_addr_lst[state->bucket];
2871 while (ifa && !net_eq(dev_net(ifa->idev->dev), net))
2872 ifa = ifa->lst_next;
2879 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2881 struct if6_iter_state *state = seq->private;
2882 struct net *net = seq_file_net(seq);
2884 ifa = ifa->lst_next;
2887 if (!net_eq(dev_net(ifa->idev->dev), net)) {
2888 ifa = ifa->lst_next;
2893 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2894 ifa = inet6_addr_lst[state->bucket];
2901 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2903 struct inet6_ifaddr *ifa = if6_get_first(seq);
2906 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2908 return pos ? NULL : ifa;
2911 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2912 __acquires(addrconf_hash_lock)
2914 read_lock_bh(&addrconf_hash_lock);
2915 return if6_get_idx(seq, *pos);
2918 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2920 struct inet6_ifaddr *ifa;
2922 ifa = if6_get_next(seq, v);
2927 static void if6_seq_stop(struct seq_file *seq, void *v)
2928 __releases(addrconf_hash_lock)
2930 read_unlock_bh(&addrconf_hash_lock);
2933 static int if6_seq_show(struct seq_file *seq, void *v)
2935 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2937 NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
2939 ifp->idev->dev->ifindex,
2943 ifp->idev->dev->name);
2947 static const struct seq_operations if6_seq_ops = {
2948 .start = if6_seq_start,
2949 .next = if6_seq_next,
2950 .show = if6_seq_show,
2951 .stop = if6_seq_stop,
2954 static int if6_seq_open(struct inode *inode, struct file *file)
2956 return seq_open_net(inode, file, &if6_seq_ops,
2957 sizeof(struct if6_iter_state));
2960 static const struct file_operations if6_fops = {
2961 .owner = THIS_MODULE,
2962 .open = if6_seq_open,
2964 .llseek = seq_lseek,
2965 .release = seq_release_net,
2968 static int if6_proc_net_init(struct net *net)
2970 if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
2975 static void if6_proc_net_exit(struct net *net)
2977 proc_net_remove(net, "if_inet6");
2980 static struct pernet_operations if6_proc_net_ops = {
2981 .init = if6_proc_net_init,
2982 .exit = if6_proc_net_exit,
2985 int __init if6_proc_init(void)
2987 return register_pernet_subsys(&if6_proc_net_ops);
2990 void if6_proc_exit(void)
2992 unregister_pernet_subsys(&if6_proc_net_ops);
2994 #endif /* CONFIG_PROC_FS */
2996 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
2997 /* Check if address is a home address configured on any interface. */
2998 int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr)
3001 struct inet6_ifaddr * ifp;
3002 u8 hash = ipv6_addr_hash(addr);
3003 read_lock_bh(&addrconf_hash_lock);
3004 for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
3005 if (!net_eq(dev_net(ifp->idev->dev), net))
3007 if (ipv6_addr_equal(&ifp->addr, addr) &&
3008 (ifp->flags & IFA_F_HOMEADDRESS)) {
3013 read_unlock_bh(&addrconf_hash_lock);
3019 * Periodic address status verification
3022 static void addrconf_verify(unsigned long foo)
3024 struct inet6_ifaddr *ifp;
3025 unsigned long now, next;
3028 spin_lock_bh(&addrconf_verify_lock);
3030 next = now + ADDR_CHECK_FREQUENCY;
3032 del_timer(&addr_chk_timer);
3034 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3037 read_lock(&addrconf_hash_lock);
3038 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
3040 #ifdef CONFIG_IPV6_PRIVACY
3041 unsigned long regen_advance;
3044 if (ifp->flags & IFA_F_PERMANENT)
3047 spin_lock(&ifp->lock);
3048 age = (now - ifp->tstamp) / HZ;
3050 #ifdef CONFIG_IPV6_PRIVACY
3051 regen_advance = ifp->idev->cnf.regen_max_retry *
3052 ifp->idev->cnf.dad_transmits *
3053 ifp->idev->nd_parms->retrans_time / HZ;
3056 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3057 age >= ifp->valid_lft) {
3058 spin_unlock(&ifp->lock);
3060 read_unlock(&addrconf_hash_lock);
3063 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3064 spin_unlock(&ifp->lock);
3066 } else if (age >= ifp->prefered_lft) {
3067 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
3070 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3072 ifp->flags |= IFA_F_DEPRECATED;
3075 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3076 next = ifp->tstamp + ifp->valid_lft * HZ;
3078 spin_unlock(&ifp->lock);
3082 read_unlock(&addrconf_hash_lock);
3084 ipv6_ifa_notify(0, ifp);
3088 #ifdef CONFIG_IPV6_PRIVACY
3089 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3090 !(ifp->flags&IFA_F_TENTATIVE)) {
3091 if (age >= ifp->prefered_lft - regen_advance) {
3092 struct inet6_ifaddr *ifpub = ifp->ifpub;
3093 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3094 next = ifp->tstamp + ifp->prefered_lft * HZ;
3095 if (!ifp->regen_count && ifpub) {
3098 in6_ifa_hold(ifpub);
3099 spin_unlock(&ifp->lock);
3100 read_unlock(&addrconf_hash_lock);
3101 spin_lock(&ifpub->lock);
3102 ifpub->regen_count = 0;
3103 spin_unlock(&ifpub->lock);
3104 ipv6_create_tempaddr(ifpub, ifp);
3109 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3110 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3111 spin_unlock(&ifp->lock);
3114 /* ifp->prefered_lft <= ifp->valid_lft */
3115 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3116 next = ifp->tstamp + ifp->prefered_lft * HZ;
3117 spin_unlock(&ifp->lock);
3120 read_unlock(&addrconf_hash_lock);
3123 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
3124 add_timer(&addr_chk_timer);
3125 spin_unlock_bh(&addrconf_verify_lock);
3128 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3130 struct in6_addr *pfx = NULL;
3133 pfx = nla_data(addr);
3136 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3139 pfx = nla_data(local);
3145 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3146 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3147 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3148 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3152 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3154 struct net *net = sock_net(skb->sk);
3155 struct ifaddrmsg *ifm;
3156 struct nlattr *tb[IFA_MAX+1];
3157 struct in6_addr *pfx;
3160 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3164 ifm = nlmsg_data(nlh);
3165 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3169 return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
3172 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3173 u32 prefered_lft, u32 valid_lft)
3177 unsigned long timeout;
3179 if (!valid_lft || (prefered_lft > valid_lft))
3182 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3183 if (addrconf_finite_timeout(timeout)) {
3184 expires = jiffies_to_clock_t(timeout * HZ);
3185 valid_lft = timeout;
3186 flags = RTF_EXPIRES;
3190 ifa_flags |= IFA_F_PERMANENT;
3193 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3194 if (addrconf_finite_timeout(timeout)) {
3196 ifa_flags |= IFA_F_DEPRECATED;
3197 prefered_lft = timeout;
3200 spin_lock_bh(&ifp->lock);
3201 ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3202 ifp->tstamp = jiffies;
3203 ifp->valid_lft = valid_lft;
3204 ifp->prefered_lft = prefered_lft;
3206 spin_unlock_bh(&ifp->lock);
3207 if (!(ifp->flags&IFA_F_TENTATIVE))
3208 ipv6_ifa_notify(0, ifp);
3210 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3218 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3220 struct net *net = sock_net(skb->sk);
3221 struct ifaddrmsg *ifm;
3222 struct nlattr *tb[IFA_MAX+1];
3223 struct in6_addr *pfx;
3224 struct inet6_ifaddr *ifa;
3225 struct net_device *dev;
3226 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3230 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3234 ifm = nlmsg_data(nlh);
3235 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3239 if (tb[IFA_CACHEINFO]) {
3240 struct ifa_cacheinfo *ci;
3242 ci = nla_data(tb[IFA_CACHEINFO]);
3243 valid_lft = ci->ifa_valid;
3244 preferred_lft = ci->ifa_prefered;
3246 preferred_lft = INFINITY_LIFE_TIME;
3247 valid_lft = INFINITY_LIFE_TIME;
3250 dev = __dev_get_by_index(net, ifm->ifa_index);
3254 /* We ignore other flags so far. */
3255 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
3257 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
3260 * It would be best to check for !NLM_F_CREATE here but
3261 * userspace alreay relies on not having to provide this.
3263 return inet6_addr_add(net, ifm->ifa_index, pfx,
3264 ifm->ifa_prefixlen, ifa_flags,
3265 preferred_lft, valid_lft);
3268 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3269 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3272 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
3279 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3280 u8 scope, int ifindex)
3282 struct ifaddrmsg *ifm;
3284 ifm = nlmsg_data(nlh);
3285 ifm->ifa_family = AF_INET6;
3286 ifm->ifa_prefixlen = prefixlen;
3287 ifm->ifa_flags = flags;
3288 ifm->ifa_scope = scope;
3289 ifm->ifa_index = ifindex;
3292 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3293 unsigned long tstamp, u32 preferred, u32 valid)
3295 struct ifa_cacheinfo ci;
3297 ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
3298 + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3299 ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
3300 + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3301 ci.ifa_prefered = preferred;
3302 ci.ifa_valid = valid;
3304 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3307 static inline int rt_scope(int ifa_scope)
3309 if (ifa_scope & IFA_HOST)
3310 return RT_SCOPE_HOST;
3311 else if (ifa_scope & IFA_LINK)
3312 return RT_SCOPE_LINK;
3313 else if (ifa_scope & IFA_SITE)
3314 return RT_SCOPE_SITE;
3316 return RT_SCOPE_UNIVERSE;
3319 static inline int inet6_ifaddr_msgsize(void)
3321 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3322 + nla_total_size(16) /* IFA_ADDRESS */
3323 + nla_total_size(sizeof(struct ifa_cacheinfo));
3326 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3327 u32 pid, u32 seq, int event, unsigned int flags)
3329 struct nlmsghdr *nlh;
3330 u32 preferred, valid;
3332 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3336 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3337 ifa->idev->dev->ifindex);
3339 if (!(ifa->flags&IFA_F_PERMANENT)) {
3340 preferred = ifa->prefered_lft;
3341 valid = ifa->valid_lft;
3342 if (preferred != INFINITY_LIFE_TIME) {
3343 long tval = (jiffies - ifa->tstamp)/HZ;
3345 if (valid != INFINITY_LIFE_TIME)
3349 preferred = INFINITY_LIFE_TIME;
3350 valid = INFINITY_LIFE_TIME;
3353 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
3354 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3355 nlmsg_cancel(skb, nlh);
3359 return nlmsg_end(skb, nlh);
3362 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3363 u32 pid, u32 seq, int event, u16 flags)
3365 struct nlmsghdr *nlh;
3366 u8 scope = RT_SCOPE_UNIVERSE;
3367 int ifindex = ifmca->idev->dev->ifindex;
3369 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3370 scope = RT_SCOPE_SITE;
3372 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3376 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3377 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3378 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
3379 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3380 nlmsg_cancel(skb, nlh);
3384 return nlmsg_end(skb, nlh);
3387 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3388 u32 pid, u32 seq, int event, unsigned int flags)
3390 struct nlmsghdr *nlh;
3391 u8 scope = RT_SCOPE_UNIVERSE;
3392 int ifindex = ifaca->aca_idev->dev->ifindex;
3394 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3395 scope = RT_SCOPE_SITE;
3397 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3401 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
3402 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3403 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
3404 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3405 nlmsg_cancel(skb, nlh);
3409 return nlmsg_end(skb, nlh);
3419 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3420 enum addr_type_t type)
3423 int s_idx, s_ip_idx;
3425 struct net_device *dev;
3426 struct inet6_dev *idev = NULL;
3427 struct inet6_ifaddr *ifa;
3428 struct ifmcaddr6 *ifmca;
3429 struct ifacaddr6 *ifaca;
3430 struct net *net = sock_net(skb->sk);
3432 s_idx = cb->args[0];
3433 s_ip_idx = ip_idx = cb->args[1];
3436 for_each_netdev(net, dev) {
3442 if ((idev = in6_dev_get(dev)) == NULL)
3444 read_lock_bh(&idev->lock);
3447 /* unicast address incl. temp addr */
3448 for (ifa = idev->addr_list; ifa;
3449 ifa = ifa->if_next, ip_idx++) {
3450 if (ip_idx < s_ip_idx)
3452 err = inet6_fill_ifaddr(skb, ifa,
3453 NETLINK_CB(cb->skb).pid,
3459 case MULTICAST_ADDR:
3460 /* multicast address */
3461 for (ifmca = idev->mc_list; ifmca;
3462 ifmca = ifmca->next, ip_idx++) {
3463 if (ip_idx < s_ip_idx)
3465 err = inet6_fill_ifmcaddr(skb, ifmca,
3466 NETLINK_CB(cb->skb).pid,
3473 /* anycast address */
3474 for (ifaca = idev->ac_list; ifaca;
3475 ifaca = ifaca->aca_next, ip_idx++) {
3476 if (ip_idx < s_ip_idx)
3478 err = inet6_fill_ifacaddr(skb, ifaca,
3479 NETLINK_CB(cb->skb).pid,
3488 read_unlock_bh(&idev->lock);
3497 cb->args[1] = ip_idx;
3501 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3503 enum addr_type_t type = UNICAST_ADDR;
3505 return inet6_dump_addr(skb, cb, type);
3508 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3510 enum addr_type_t type = MULTICAST_ADDR;
3512 return inet6_dump_addr(skb, cb, type);
3516 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3518 enum addr_type_t type = ANYCAST_ADDR;
3520 return inet6_dump_addr(skb, cb, type);
3523 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3526 struct net *net = sock_net(in_skb->sk);
3527 struct ifaddrmsg *ifm;
3528 struct nlattr *tb[IFA_MAX+1];
3529 struct in6_addr *addr = NULL;
3530 struct net_device *dev = NULL;
3531 struct inet6_ifaddr *ifa;
3532 struct sk_buff *skb;
3535 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3539 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3545 ifm = nlmsg_data(nlh);
3547 dev = __dev_get_by_index(net, ifm->ifa_index);
3549 if ((ifa = ipv6_get_ifaddr(net, addr, dev, 1)) == NULL) {
3550 err = -EADDRNOTAVAIL;
3554 if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
3559 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3560 nlh->nlmsg_seq, RTM_NEWADDR, 0);
3562 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3563 WARN_ON(err == -EMSGSIZE);
3567 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
3574 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3576 struct sk_buff *skb;
3577 struct net *net = dev_net(ifa->idev->dev);
3580 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
3584 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
3586 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3587 WARN_ON(err == -EMSGSIZE);
3591 err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3594 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
3597 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
3598 __s32 *array, int bytes)
3600 BUG_ON(bytes < (DEVCONF_MAX * 4));
3602 memset(array, 0, bytes);
3603 array[DEVCONF_FORWARDING] = cnf->forwarding;
3604 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3605 array[DEVCONF_MTU6] = cnf->mtu6;
3606 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3607 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3608 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3609 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3610 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3611 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3612 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3613 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3614 #ifdef CONFIG_IPV6_PRIVACY
3615 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3616 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3617 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3618 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3619 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3621 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3622 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3623 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3624 #ifdef CONFIG_IPV6_ROUTER_PREF
3625 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
3626 array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
3627 #ifdef CONFIG_IPV6_ROUTE_INFO
3628 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3631 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
3632 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
3633 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3634 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
3636 #ifdef CONFIG_IPV6_MROUTE
3637 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
3641 static inline size_t inet6_if_nlmsg_size(void)
3643 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3644 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3645 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3646 + nla_total_size(4) /* IFLA_MTU */
3647 + nla_total_size(4) /* IFLA_LINK */
3648 + nla_total_size( /* IFLA_PROTINFO */
3649 nla_total_size(4) /* IFLA_INET6_FLAGS */
3650 + nla_total_size(sizeof(struct ifla_cacheinfo))
3651 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3652 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3653 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
3657 static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
3661 int pad = bytes - sizeof(u64) * items;
3664 /* Use put_unaligned() because stats may not be aligned for u64. */
3665 put_unaligned(items, &stats[0]);
3666 for (i = 1; i < items; i++)
3667 put_unaligned(snmp_fold_field(mib, i), &stats[i]);
3669 memset(&stats[items], 0, pad);
3672 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
3676 case IFLA_INET6_STATS:
3677 __snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
3679 case IFLA_INET6_ICMP6STATS:
3680 __snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
3685 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3686 u32 pid, u32 seq, int event, unsigned int flags)
3688 struct net_device *dev = idev->dev;
3690 struct ifinfomsg *hdr;
3691 struct nlmsghdr *nlh;
3693 struct ifla_cacheinfo ci;
3695 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
3699 hdr = nlmsg_data(nlh);
3700 hdr->ifi_family = AF_INET6;
3702 hdr->ifi_type = dev->type;
3703 hdr->ifi_index = dev->ifindex;
3704 hdr->ifi_flags = dev_get_flags(dev);
3705 hdr->ifi_change = 0;
3707 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
3710 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3712 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
3713 if (dev->ifindex != dev->iflink)
3714 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
3716 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
3717 if (protoinfo == NULL)
3718 goto nla_put_failure;
3720 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
3722 ci.max_reasm_len = IPV6_MAXPLEN;
3723 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3724 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3725 ci.reachable_time = idev->nd_parms->reachable_time;
3726 ci.retrans_time = idev->nd_parms->retrans_time;
3727 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3729 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3731 goto nla_put_failure;
3732 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
3734 /* XXX - MC not implemented */
3736 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3738 goto nla_put_failure;
3739 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3741 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3743 goto nla_put_failure;
3744 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
3746 nla_nest_end(skb, protoinfo);
3747 return nlmsg_end(skb, nlh);
3750 nlmsg_cancel(skb, nlh);
3754 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3756 struct net *net = sock_net(skb->sk);
3758 int s_idx = cb->args[0];
3759 struct net_device *dev;
3760 struct inet6_dev *idev;
3762 read_lock(&dev_base_lock);
3764 for_each_netdev(net, dev) {
3767 if ((idev = in6_dev_get(dev)) == NULL)
3769 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3770 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3777 read_unlock(&dev_base_lock);
3783 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3785 struct sk_buff *skb;
3786 struct net *net = dev_net(idev->dev);
3789 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
3793 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
3795 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3796 WARN_ON(err == -EMSGSIZE);
3800 err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3803 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
3806 static inline size_t inet6_prefix_nlmsg_size(void)
3808 return NLMSG_ALIGN(sizeof(struct prefixmsg))
3809 + nla_total_size(sizeof(struct in6_addr))
3810 + nla_total_size(sizeof(struct prefix_cacheinfo));
3813 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3814 struct prefix_info *pinfo, u32 pid, u32 seq,
3815 int event, unsigned int flags)
3817 struct prefixmsg *pmsg;
3818 struct nlmsghdr *nlh;
3819 struct prefix_cacheinfo ci;
3821 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
3825 pmsg = nlmsg_data(nlh);
3826 pmsg->prefix_family = AF_INET6;
3827 pmsg->prefix_pad1 = 0;
3828 pmsg->prefix_pad2 = 0;
3829 pmsg->prefix_ifindex = idev->dev->ifindex;
3830 pmsg->prefix_len = pinfo->prefix_len;
3831 pmsg->prefix_type = pinfo->type;
3832 pmsg->prefix_pad3 = 0;
3833 pmsg->prefix_flags = 0;
3835 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3836 if (pinfo->autoconf)
3837 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3839 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3841 ci.preferred_time = ntohl(pinfo->prefered);
3842 ci.valid_time = ntohl(pinfo->valid);
3843 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3845 return nlmsg_end(skb, nlh);
3848 nlmsg_cancel(skb, nlh);
3852 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3853 struct prefix_info *pinfo)
3855 struct sk_buff *skb;
3856 struct net *net = dev_net(idev->dev);
3859 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
3863 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
3865 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3866 WARN_ON(err == -EMSGSIZE);
3870 err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3873 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
3876 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3878 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3883 * If the address was optimistic
3884 * we inserted the route at the start of
3885 * our DAD process, so we don't need
3888 if (!(ifp->rt->rt6i_node))
3889 ip6_ins_rt(ifp->rt);
3890 if (ifp->idev->cnf.forwarding)
3891 addrconf_join_anycast(ifp);
3894 if (ifp->idev->cnf.forwarding)
3895 addrconf_leave_anycast(ifp);
3896 addrconf_leave_solict(ifp->idev, &ifp->addr);
3897 dst_hold(&ifp->rt->u.dst);
3898 if (ip6_del_rt(ifp->rt))
3899 dst_free(&ifp->rt->u.dst);
3904 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3907 if (likely(ifp->idev->dead == 0))
3908 __ipv6_ifa_notify(event, ifp);
3909 rcu_read_unlock_bh();
3912 #ifdef CONFIG_SYSCTL
3915 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3916 void __user *buffer, size_t *lenp, loff_t *ppos)
3918 int *valp = ctl->data;
3922 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3925 addrconf_fixup_forwarding(ctl, valp, val);
3929 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3930 int __user *name, int nlen,
3931 void __user *oldval,
3932 size_t __user *oldlenp,
3933 void __user *newval, size_t newlen)
3935 int *valp = table->data;
3939 if (!newval || !newlen)
3941 if (newlen != sizeof(int))
3943 if (get_user(new, (int __user *)newval))
3947 if (oldval && oldlenp) {
3949 if (get_user(len, oldlenp))
3952 if (len > table->maxlen)
3953 len = table->maxlen;
3954 if (copy_to_user(oldval, valp, len))
3956 if (put_user(len, oldlenp))
3962 addrconf_fixup_forwarding(table, valp, val);
3966 static struct addrconf_sysctl_table
3968 struct ctl_table_header *sysctl_header;
3969 ctl_table addrconf_vars[DEVCONF_MAX+1];
3971 } addrconf_sysctl __read_mostly = {
3972 .sysctl_header = NULL,
3975 .ctl_name = NET_IPV6_FORWARDING,
3976 .procname = "forwarding",
3977 .data = &ipv6_devconf.forwarding,
3978 .maxlen = sizeof(int),
3980 .proc_handler = &addrconf_sysctl_forward,
3981 .strategy = &addrconf_sysctl_forward_strategy,
3984 .ctl_name = NET_IPV6_HOP_LIMIT,
3985 .procname = "hop_limit",
3986 .data = &ipv6_devconf.hop_limit,
3987 .maxlen = sizeof(int),
3989 .proc_handler = proc_dointvec,
3992 .ctl_name = NET_IPV6_MTU,
3994 .data = &ipv6_devconf.mtu6,
3995 .maxlen = sizeof(int),
3997 .proc_handler = &proc_dointvec,
4000 .ctl_name = NET_IPV6_ACCEPT_RA,
4001 .procname = "accept_ra",
4002 .data = &ipv6_devconf.accept_ra,
4003 .maxlen = sizeof(int),
4005 .proc_handler = &proc_dointvec,
4008 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
4009 .procname = "accept_redirects",
4010 .data = &ipv6_devconf.accept_redirects,
4011 .maxlen = sizeof(int),
4013 .proc_handler = &proc_dointvec,
4016 .ctl_name = NET_IPV6_AUTOCONF,
4017 .procname = "autoconf",
4018 .data = &ipv6_devconf.autoconf,
4019 .maxlen = sizeof(int),
4021 .proc_handler = &proc_dointvec,
4024 .ctl_name = NET_IPV6_DAD_TRANSMITS,
4025 .procname = "dad_transmits",
4026 .data = &ipv6_devconf.dad_transmits,
4027 .maxlen = sizeof(int),
4029 .proc_handler = &proc_dointvec,
4032 .ctl_name = NET_IPV6_RTR_SOLICITS,
4033 .procname = "router_solicitations",
4034 .data = &ipv6_devconf.rtr_solicits,
4035 .maxlen = sizeof(int),
4037 .proc_handler = &proc_dointvec,
4040 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
4041 .procname = "router_solicitation_interval",
4042 .data = &ipv6_devconf.rtr_solicit_interval,
4043 .maxlen = sizeof(int),
4045 .proc_handler = &proc_dointvec_jiffies,
4046 .strategy = &sysctl_jiffies,
4049 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
4050 .procname = "router_solicitation_delay",
4051 .data = &ipv6_devconf.rtr_solicit_delay,
4052 .maxlen = sizeof(int),
4054 .proc_handler = &proc_dointvec_jiffies,
4055 .strategy = &sysctl_jiffies,
4058 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
4059 .procname = "force_mld_version",
4060 .data = &ipv6_devconf.force_mld_version,
4061 .maxlen = sizeof(int),
4063 .proc_handler = &proc_dointvec,
4065 #ifdef CONFIG_IPV6_PRIVACY
4067 .ctl_name = NET_IPV6_USE_TEMPADDR,
4068 .procname = "use_tempaddr",
4069 .data = &ipv6_devconf.use_tempaddr,
4070 .maxlen = sizeof(int),
4072 .proc_handler = &proc_dointvec,
4075 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
4076 .procname = "temp_valid_lft",
4077 .data = &ipv6_devconf.temp_valid_lft,
4078 .maxlen = sizeof(int),
4080 .proc_handler = &proc_dointvec,
4083 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
4084 .procname = "temp_prefered_lft",
4085 .data = &ipv6_devconf.temp_prefered_lft,
4086 .maxlen = sizeof(int),
4088 .proc_handler = &proc_dointvec,
4091 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
4092 .procname = "regen_max_retry",
4093 .data = &ipv6_devconf.regen_max_retry,
4094 .maxlen = sizeof(int),
4096 .proc_handler = &proc_dointvec,
4099 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
4100 .procname = "max_desync_factor",
4101 .data = &ipv6_devconf.max_desync_factor,
4102 .maxlen = sizeof(int),
4104 .proc_handler = &proc_dointvec,
4108 .ctl_name = NET_IPV6_MAX_ADDRESSES,
4109 .procname = "max_addresses",
4110 .data = &ipv6_devconf.max_addresses,
4111 .maxlen = sizeof(int),
4113 .proc_handler = &proc_dointvec,
4116 .ctl_name = NET_IPV6_ACCEPT_RA_DEFRTR,
4117 .procname = "accept_ra_defrtr",
4118 .data = &ipv6_devconf.accept_ra_defrtr,
4119 .maxlen = sizeof(int),
4121 .proc_handler = &proc_dointvec,
4124 .ctl_name = NET_IPV6_ACCEPT_RA_PINFO,
4125 .procname = "accept_ra_pinfo",
4126 .data = &ipv6_devconf.accept_ra_pinfo,
4127 .maxlen = sizeof(int),
4129 .proc_handler = &proc_dointvec,
4131 #ifdef CONFIG_IPV6_ROUTER_PREF
4133 .ctl_name = NET_IPV6_ACCEPT_RA_RTR_PREF,
4134 .procname = "accept_ra_rtr_pref",
4135 .data = &ipv6_devconf.accept_ra_rtr_pref,
4136 .maxlen = sizeof(int),
4138 .proc_handler = &proc_dointvec,
4141 .ctl_name = NET_IPV6_RTR_PROBE_INTERVAL,
4142 .procname = "router_probe_interval",
4143 .data = &ipv6_devconf.rtr_probe_interval,
4144 .maxlen = sizeof(int),
4146 .proc_handler = &proc_dointvec_jiffies,
4147 .strategy = &sysctl_jiffies,
4149 #ifdef CONFIG_IPV6_ROUTE_INFO
4151 .ctl_name = NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
4152 .procname = "accept_ra_rt_info_max_plen",
4153 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4154 .maxlen = sizeof(int),
4156 .proc_handler = &proc_dointvec,
4161 .ctl_name = NET_IPV6_PROXY_NDP,
4162 .procname = "proxy_ndp",
4163 .data = &ipv6_devconf.proxy_ndp,
4164 .maxlen = sizeof(int),
4166 .proc_handler = &proc_dointvec,
4169 .ctl_name = NET_IPV6_ACCEPT_SOURCE_ROUTE,
4170 .procname = "accept_source_route",
4171 .data = &ipv6_devconf.accept_source_route,
4172 .maxlen = sizeof(int),
4174 .proc_handler = &proc_dointvec,
4176 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4178 .ctl_name = CTL_UNNUMBERED,
4179 .procname = "optimistic_dad",
4180 .data = &ipv6_devconf.optimistic_dad,
4181 .maxlen = sizeof(int),
4183 .proc_handler = &proc_dointvec,
4187 #ifdef CONFIG_IPV6_MROUTE
4189 .ctl_name = CTL_UNNUMBERED,
4190 .procname = "mc_forwarding",
4191 .data = &ipv6_devconf.mc_forwarding,
4192 .maxlen = sizeof(int),
4194 .proc_handler = &proc_dointvec,
4198 .ctl_name = 0, /* sentinel */
4203 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4204 int ctl_name, struct inet6_dev *idev, struct ipv6_devconf *p)
4207 struct addrconf_sysctl_table *t;
4209 #define ADDRCONF_CTL_PATH_DEV 3
4211 struct ctl_path addrconf_ctl_path[] = {
4212 { .procname = "net", .ctl_name = CTL_NET, },
4213 { .procname = "ipv6", .ctl_name = NET_IPV6, },
4214 { .procname = "conf", .ctl_name = NET_IPV6_CONF, },
4215 { /* to be set */ },
4220 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
4224 for (i=0; t->addrconf_vars[i].data; i++) {
4225 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
4226 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4227 t->addrconf_vars[i].extra2 = net;
4231 * Make a copy of dev_name, because '.procname' is regarded as const
4232 * by sysctl and we wouldn't want anyone to change it under our feet
4233 * (see SIOCSIFNAME).
4235 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
4239 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
4240 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name;
4242 t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
4244 if (t->sysctl_header == NULL)
4258 static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4260 struct addrconf_sysctl_table *t;
4262 if (p->sysctl == NULL)
4267 unregister_sysctl_table(t->sysctl_header);
4272 static void addrconf_sysctl_register(struct inet6_dev *idev)
4274 neigh_sysctl_register(idev->dev, idev->nd_parms, NET_IPV6,
4275 NET_IPV6_NEIGH, "ipv6",
4276 &ndisc_ifinfo_sysctl_change,
4277 ndisc_ifinfo_sysctl_strategy);
4278 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
4279 idev->dev->ifindex, idev, &idev->cnf);
4282 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
4284 __addrconf_sysctl_unregister(&idev->cnf);
4285 neigh_sysctl_unregister(idev->nd_parms);
4291 static int addrconf_init_net(struct net *net)
4294 struct ipv6_devconf *all, *dflt;
4297 all = &ipv6_devconf;
4298 dflt = &ipv6_devconf_dflt;
4300 if (net != &init_net) {
4301 all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);
4305 dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4307 goto err_alloc_dflt;
4310 net->ipv6.devconf_all = all;
4311 net->ipv6.devconf_dflt = dflt;
4313 #ifdef CONFIG_SYSCTL
4314 err = __addrconf_sysctl_register(net, "all", NET_PROTO_CONF_ALL,
4319 err = __addrconf_sysctl_register(net, "default", NET_PROTO_CONF_DEFAULT,
4326 #ifdef CONFIG_SYSCTL
4328 __addrconf_sysctl_unregister(all);
4338 static void addrconf_exit_net(struct net *net)
4340 #ifdef CONFIG_SYSCTL
4341 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4342 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
4344 if (net != &init_net) {
4345 kfree(net->ipv6.devconf_dflt);
4346 kfree(net->ipv6.devconf_all);
4350 static struct pernet_operations addrconf_ops = {
4351 .init = addrconf_init_net,
4352 .exit = addrconf_exit_net,
4359 int register_inet6addr_notifier(struct notifier_block *nb)
4361 return atomic_notifier_chain_register(&inet6addr_chain, nb);
4364 EXPORT_SYMBOL(register_inet6addr_notifier);
4366 int unregister_inet6addr_notifier(struct notifier_block *nb)
4368 return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
4371 EXPORT_SYMBOL(unregister_inet6addr_notifier);
4373 static void addrconf_net_exit(struct net *net)
4375 struct net_device *dev;
4378 /* clean dev list */
4379 for_each_netdev(net, dev) {
4380 if (__in6_dev_get(dev) == NULL)
4382 addrconf_ifdown(dev, 1);
4384 addrconf_ifdown(net->loopback_dev, 2);
4388 static struct pernet_operations addrconf_net_ops = {
4389 .exit = addrconf_net_exit,
4393 * Init / cleanup code
4396 int __init addrconf_init(void)
4400 if ((err = ipv6_addr_label_init()) < 0) {
4401 printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
4406 register_pernet_subsys(&addrconf_ops);
4408 /* The addrconf netdev notifier requires that loopback_dev
4409 * has it's ipv6 private information allocated and setup
4410 * before it can bring up and give link-local addresses
4411 * to other devices which are up.
4413 * Unfortunately, loopback_dev is not necessarily the first
4414 * entry in the global dev_base list of net devices. In fact,
4415 * it is likely to be the very last entry on that list.
4416 * So this causes the notifier registry below to try and
4417 * give link-local addresses to all devices besides loopback_dev
4418 * first, then loopback_dev, which cases all the non-loopback_dev
4419 * devices to fail to get a link-local address.
4421 * So, as a temporary fix, allocate the ipv6 structure for
4422 * loopback_dev first by hand.
4423 * Longer term, all of the dependencies ipv6 has upon the loopback
4424 * device and it being up should be removed.
4427 if (!ipv6_add_dev(init_net.loopback_dev))
4433 err = register_pernet_device(&addrconf_net_ops);
4437 register_netdevice_notifier(&ipv6_dev_notf);
4441 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4445 /* Only the first call to __rtnl_register can fail */
4446 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4447 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4448 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4449 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4450 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4452 ipv6_addr_label_rtnl_register();
4456 unregister_netdevice_notifier(&ipv6_dev_notf);
4458 unregister_pernet_subsys(&addrconf_ops);
4463 void addrconf_cleanup(void)
4465 struct inet6_ifaddr *ifa;
4468 unregister_netdevice_notifier(&ipv6_dev_notf);
4469 unregister_pernet_device(&addrconf_net_ops);
4471 unregister_pernet_subsys(&addrconf_ops);
4478 write_lock_bh(&addrconf_hash_lock);
4479 for (i=0; i < IN6_ADDR_HSIZE; i++) {
4480 for (ifa=inet6_addr_lst[i]; ifa; ) {
4481 struct inet6_ifaddr *bifa;
4484 ifa = ifa->lst_next;
4485 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
4486 /* Do not free it; something is wrong.
4487 Now we can investigate it with debugger.
4491 write_unlock_bh(&addrconf_hash_lock);
4493 del_timer(&addr_chk_timer);
4496 unregister_pernet_subsys(&addrconf_net_ops);