2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill double kfree on module
24 * Maciej W. Rozycki : FDDI support
25 * sekiya@USAGI : Don't send too many RS
27 * yoshfuji@USAGI : Fixed interval between DAD
29 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
30 * address validation timer.
31 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
33 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
34 * address on a same interface.
35 * YOSHIFUJI Hideaki @USAGI : ARCnet support
36 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
38 * YOSHIFUJI Hideaki @USAGI : improved source address
39 * selection; consider scope,
43 #include <linux/config.h>
44 #include <linux/errno.h>
45 #include <linux/types.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/sched.h>
49 #include <linux/net.h>
50 #include <linux/in6.h>
51 #include <linux/netdevice.h>
52 #include <linux/if_arp.h>
53 #include <linux/if_arcnet.h>
54 #include <linux/if_infiniband.h>
55 #include <linux/route.h>
56 #include <linux/inetdevice.h>
57 #include <linux/init.h>
59 #include <linux/sysctl.h>
61 #include <linux/delay.h>
62 #include <linux/notifier.h>
63 #include <linux/string.h>
69 #include <net/protocol.h>
70 #include <net/ndisc.h>
71 #include <net/ip6_route.h>
72 #include <net/addrconf.h>
75 #include <linux/if_tunnel.h>
76 #include <linux/rtnetlink.h>
78 #ifdef CONFIG_IPV6_PRIVACY
79 #include <linux/random.h>
80 #include <linux/crypto.h>
81 #include <linux/scatterlist.h>
84 #include <asm/uaccess.h>
86 #include <linux/proc_fs.h>
87 #include <linux/seq_file.h>
89 /* Set to 3 to get tracing... */
93 #define ADBG(x) printk x
98 #define INFINITY_LIFE_TIME 0xFFFFFFFF
99 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
102 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
103 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
106 #ifdef CONFIG_IPV6_PRIVACY
107 static int __ipv6_regen_rndid(struct inet6_dev *idev);
108 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
109 static void ipv6_regen_rndid(unsigned long data);
111 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
112 static struct crypto_tfm *md5_tfm;
113 static DEFINE_SPINLOCK(md5_tfm_lock);
116 static int ipv6_count_addresses(struct inet6_dev *idev);
119 * Configured unicast address hash table
121 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
122 static DEFINE_RWLOCK(addrconf_hash_lock);
124 /* Protects inet6 devices */
125 DEFINE_RWLOCK(addrconf_lock);
127 static void addrconf_verify(unsigned long);
129 static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
130 static DEFINE_SPINLOCK(addrconf_verify_lock);
132 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
133 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
135 static int addrconf_ifdown(struct net_device *dev, int how);
137 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
138 static void addrconf_dad_timer(unsigned long data);
139 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
140 static void addrconf_rs_timer(unsigned long data);
141 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
142 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
144 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
145 struct prefix_info *pinfo);
146 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
148 static struct notifier_block *inet6addr_chain;
150 struct ipv6_devconf ipv6_devconf = {
152 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
153 .mtu6 = IPV6_MIN_MTU,
155 .accept_redirects = 1,
157 .force_mld_version = 0,
159 .rtr_solicits = MAX_RTR_SOLICITATIONS,
160 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
161 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
162 #ifdef CONFIG_IPV6_PRIVACY
164 .temp_valid_lft = TEMP_VALID_LIFETIME,
165 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
166 .regen_max_retry = REGEN_MAX_RETRY,
167 .max_desync_factor = MAX_DESYNC_FACTOR,
169 .max_addresses = IPV6_MAX_ADDRESSES,
172 static struct ipv6_devconf ipv6_devconf_dflt = {
174 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
175 .mtu6 = IPV6_MIN_MTU,
177 .accept_redirects = 1,
180 .rtr_solicits = MAX_RTR_SOLICITATIONS,
181 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
182 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
183 #ifdef CONFIG_IPV6_PRIVACY
185 .temp_valid_lft = TEMP_VALID_LIFETIME,
186 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
187 .regen_max_retry = REGEN_MAX_RETRY,
188 .max_desync_factor = MAX_DESYNC_FACTOR,
190 .max_addresses = IPV6_MAX_ADDRESSES,
193 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
195 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
197 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
199 #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
201 static inline unsigned ipv6_addr_scope2type(unsigned scope)
204 case IPV6_ADDR_SCOPE_NODELOCAL:
205 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
207 case IPV6_ADDR_SCOPE_LINKLOCAL:
208 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
209 IPV6_ADDR_LINKLOCAL);
210 case IPV6_ADDR_SCOPE_SITELOCAL:
211 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
212 IPV6_ADDR_SITELOCAL);
214 return IPV6_ADDR_SCOPE_TYPE(scope);
217 int __ipv6_addr_type(const struct in6_addr *addr)
221 st = addr->s6_addr32[0];
223 /* Consider all addresses with the first three bits different of
224 000 and 111 as unicasts.
226 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
227 (st & htonl(0xE0000000)) != htonl(0xE0000000))
228 return (IPV6_ADDR_UNICAST |
229 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
231 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
233 /* addr-select 3.1 */
234 return (IPV6_ADDR_MULTICAST |
235 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
238 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
239 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
240 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.1 */
241 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
242 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
243 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL)); /* addr-select 3.1 */
245 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
246 if (addr->s6_addr32[2] == 0) {
247 if (addr->s6_addr32[3] == 0)
248 return IPV6_ADDR_ANY;
250 if (addr->s6_addr32[3] == htonl(0x00000001))
251 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
252 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL)); /* addr-select 3.4 */
254 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
255 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
258 if (addr->s6_addr32[2] == htonl(0x0000ffff))
259 return (IPV6_ADDR_MAPPED |
260 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.3 */
263 return (IPV6_ADDR_RESERVED |
264 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL)); /* addr-select 3.4 */
267 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
269 if (del_timer(&ifp->timer))
273 enum addrconf_timer_t
280 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
281 enum addrconf_timer_t what,
284 if (!del_timer(&ifp->timer))
289 ifp->timer.function = addrconf_dad_timer;
292 ifp->timer.function = addrconf_rs_timer;
296 ifp->timer.expires = jiffies + when;
297 add_timer(&ifp->timer);
300 /* Nobody refers to this device, we may destroy it. */
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);
319 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
321 struct inet6_dev *ndev;
325 if (dev->mtu < IPV6_MIN_MTU)
328 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
331 memset(ndev, 0, sizeof(struct inet6_dev));
333 rwlock_init(&ndev->lock);
335 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
336 ndev->cnf.mtu6 = dev->mtu;
337 ndev->cnf.sysctl = NULL;
338 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
339 if (ndev->nd_parms == NULL) {
343 /* We refer to the device */
346 if (snmp6_alloc_dev(ndev) < 0) {
348 "%s(): cannot allocate memory for statistics; dev=%s.\n",
349 __FUNCTION__, dev->name));
350 neigh_parms_release(&nd_tbl, ndev->nd_parms);
352 in6_dev_finish_destroy(ndev);
356 if (snmp6_register_dev(ndev) < 0) {
358 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
359 __FUNCTION__, dev->name));
360 neigh_parms_release(&nd_tbl, ndev->nd_parms);
362 in6_dev_finish_destroy(ndev);
366 /* One reference from device. We must do this before
367 * we invoke __ipv6_regen_rndid().
371 #ifdef CONFIG_IPV6_PRIVACY
372 get_random_bytes(ndev->rndid, sizeof(ndev->rndid));
373 get_random_bytes(ndev->entropy, sizeof(ndev->entropy));
374 init_timer(&ndev->regen_timer);
375 ndev->regen_timer.function = ipv6_regen_rndid;
376 ndev->regen_timer.data = (unsigned long) ndev;
377 if ((dev->flags&IFF_LOOPBACK) ||
378 dev->type == ARPHRD_TUNNEL ||
379 dev->type == ARPHRD_NONE ||
380 dev->type == ARPHRD_SIT) {
382 "Disabled Privacy Extensions on device %p(%s)\n",
384 ndev->cnf.use_tempaddr = -1;
387 ipv6_regen_rndid((unsigned long) ndev);
391 write_lock_bh(&addrconf_lock);
393 write_unlock_bh(&addrconf_lock);
395 ipv6_mc_init_dev(ndev);
396 ndev->tstamp = jiffies;
398 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
399 NET_IPV6_NEIGH, "ipv6",
400 &ndisc_ifinfo_sysctl_change,
402 addrconf_sysctl_register(ndev, &ndev->cnf);
408 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
410 struct inet6_dev *idev;
414 if ((idev = __in6_dev_get(dev)) == NULL) {
415 if ((idev = ipv6_add_dev(dev)) == NULL)
418 if (dev->flags&IFF_UP)
424 static void dev_forward_change(struct inet6_dev *idev)
426 struct net_device *dev;
427 struct inet6_ifaddr *ifa;
428 struct in6_addr addr;
433 if (dev && (dev->flags & IFF_MULTICAST)) {
434 ipv6_addr_all_routers(&addr);
436 if (idev->cnf.forwarding)
437 ipv6_dev_mc_inc(dev, &addr);
439 ipv6_dev_mc_dec(dev, &addr);
441 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
442 if (idev->cnf.forwarding)
443 addrconf_join_anycast(ifa);
445 addrconf_leave_anycast(ifa);
450 static void addrconf_forward_change(void)
452 struct net_device *dev;
453 struct inet6_dev *idev;
455 read_lock(&dev_base_lock);
456 for (dev=dev_base; dev; dev=dev->next) {
457 read_lock(&addrconf_lock);
458 idev = __in6_dev_get(dev);
460 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
461 idev->cnf.forwarding = ipv6_devconf.forwarding;
463 dev_forward_change(idev);
465 read_unlock(&addrconf_lock);
467 read_unlock(&dev_base_lock);
471 /* Nobody refers to this ifaddr, destroy it */
473 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
475 BUG_TRAP(ifp->if_next==NULL);
476 BUG_TRAP(ifp->lst_next==NULL);
477 #ifdef NET_REFCNT_DEBUG
478 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
481 in6_dev_put(ifp->idev);
483 if (del_timer(&ifp->timer))
484 printk("Timer is still running, when freeing ifa=%p\n", ifp);
487 printk("Freeing alive inet6 address %p\n", ifp);
490 dst_release(&ifp->rt->u.dst);
495 /* On success it returns ifp with increased reference count */
497 static struct inet6_ifaddr *
498 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
499 int scope, u32 flags)
501 struct inet6_ifaddr *ifa = NULL;
506 read_lock_bh(&addrconf_lock);
508 err = -ENODEV; /*XXX*/
512 write_lock(&addrconf_hash_lock);
514 /* Ignore adding duplicate addresses on an interface */
515 if (ipv6_chk_same_addr(addr, idev->dev)) {
516 ADBG(("ipv6_add_addr: already assigned\n"));
521 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
524 ADBG(("ipv6_add_addr: malloc failed\n"));
529 rt = addrconf_dst_alloc(idev, addr, 0);
535 memset(ifa, 0, sizeof(struct inet6_ifaddr));
536 ipv6_addr_copy(&ifa->addr, addr);
538 spin_lock_init(&ifa->lock);
539 init_timer(&ifa->timer);
540 ifa->timer.data = (unsigned long) ifa;
542 ifa->prefix_len = pfxlen;
543 ifa->flags = flags | IFA_F_TENTATIVE;
544 ifa->cstamp = ifa->tstamp = jiffies;
551 /* Add to big hash table */
552 hash = ipv6_addr_hash(addr);
554 ifa->lst_next = inet6_addr_lst[hash];
555 inet6_addr_lst[hash] = ifa;
557 write_unlock(&addrconf_hash_lock);
559 write_lock(&idev->lock);
560 /* Add to inet6_dev unicast addr list. */
561 ifa->if_next = idev->addr_list;
562 idev->addr_list = ifa;
564 #ifdef CONFIG_IPV6_PRIVACY
565 if (ifa->flags&IFA_F_TEMPORARY) {
566 ifa->tmp_next = idev->tempaddr_list;
567 idev->tempaddr_list = ifa;
575 write_unlock(&idev->lock);
577 read_unlock_bh(&addrconf_lock);
579 if (likely(err == 0))
580 notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
588 write_unlock(&addrconf_hash_lock);
592 /* This function wants to get referenced ifp and releases it before return */
594 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
596 struct inet6_ifaddr *ifa, **ifap;
597 struct inet6_dev *idev = ifp->idev;
599 int deleted = 0, onlink = 0;
600 unsigned long expires = jiffies;
602 hash = ipv6_addr_hash(&ifp->addr);
606 write_lock_bh(&addrconf_hash_lock);
607 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
608 ifap = &ifa->lst_next) {
610 *ifap = ifa->lst_next;
612 ifa->lst_next = NULL;
616 write_unlock_bh(&addrconf_hash_lock);
618 write_lock_bh(&idev->lock);
619 #ifdef CONFIG_IPV6_PRIVACY
620 if (ifp->flags&IFA_F_TEMPORARY) {
621 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
622 ifap = &ifa->tmp_next) {
624 *ifap = ifa->tmp_next;
626 in6_ifa_put(ifp->ifpub);
630 ifa->tmp_next = NULL;
637 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;
638 ifap = &ifa->if_next) {
640 *ifap = ifa->if_next;
643 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
646 } else if (ifp->flags & IFA_F_PERMANENT) {
647 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
649 if (ifa->flags & IFA_F_PERMANENT) {
654 unsigned long lifetime;
659 spin_lock(&ifa->lock);
660 lifetime = min_t(unsigned long,
661 ifa->valid_lft, 0x7fffffffUL/HZ);
662 if (time_before(expires,
663 ifa->tstamp + lifetime * HZ))
664 expires = ifa->tstamp + lifetime * HZ;
665 spin_unlock(&ifa->lock);
670 write_unlock_bh(&idev->lock);
672 ipv6_ifa_notify(RTM_DELADDR, ifp);
674 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
676 addrconf_del_timer(ifp);
679 * Purge or update corresponding prefix
681 * 1) we don't purge prefix here if address was not permanent.
682 * prefix is managed by its own lifetime.
683 * 2) if there're no addresses, delete prefix.
684 * 3) if there're still other permanent address(es),
685 * corresponding prefix is still permanent.
686 * 4) otherwise, update prefix lifetime to the
687 * longest valid lifetime among the corresponding
688 * addresses on the device.
689 * Note: subsequent RA will update lifetime.
693 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
694 struct in6_addr prefix;
697 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
698 rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
700 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
702 ip6_del_rt(rt, NULL, NULL, NULL);
704 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
705 rt->rt6i_expires = expires;
706 rt->rt6i_flags |= RTF_EXPIRES;
709 dst_release(&rt->u.dst);
715 #ifdef CONFIG_IPV6_PRIVACY
716 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
718 struct inet6_dev *idev = ifp->idev;
719 struct in6_addr addr, *tmpaddr;
720 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
725 write_lock(&idev->lock);
727 spin_lock_bh(&ift->lock);
728 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
729 spin_unlock_bh(&ift->lock);
736 if (idev->cnf.use_tempaddr <= 0) {
737 write_unlock(&idev->lock);
739 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
744 spin_lock_bh(&ifp->lock);
745 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
746 idev->cnf.use_tempaddr = -1; /*XXX*/
747 spin_unlock_bh(&ifp->lock);
748 write_unlock(&idev->lock);
750 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
756 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
757 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
758 spin_unlock_bh(&ifp->lock);
759 write_unlock(&idev->lock);
761 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
767 memcpy(&addr.s6_addr[8], idev->rndid, 8);
768 tmp_valid_lft = min_t(__u32,
770 idev->cnf.temp_valid_lft);
771 tmp_prefered_lft = min_t(__u32,
773 idev->cnf.temp_prefered_lft - desync_factor / HZ);
774 tmp_plen = ifp->prefix_len;
775 max_addresses = idev->cnf.max_addresses;
776 tmp_cstamp = ifp->cstamp;
777 tmp_tstamp = ifp->tstamp;
778 spin_unlock_bh(&ifp->lock);
780 write_unlock(&idev->lock);
781 ift = !max_addresses ||
782 ipv6_count_addresses(idev) < max_addresses ?
783 ipv6_add_addr(idev, &addr, tmp_plen,
784 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
785 if (!ift || IS_ERR(ift)) {
789 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
791 write_lock(&idev->lock);
795 spin_lock_bh(&ift->lock);
797 ift->valid_lft = tmp_valid_lft;
798 ift->prefered_lft = tmp_prefered_lft;
799 ift->cstamp = tmp_cstamp;
800 ift->tstamp = tmp_tstamp;
801 spin_unlock_bh(&ift->lock);
803 addrconf_dad_start(ift, 0);
812 * Choose an appropriate source address (RFC3484)
814 struct ipv6_saddr_score {
822 #define IPV6_SADDR_SCORE_LOCAL 0x0001
823 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
824 #define IPV6_SADDR_SCORE_HOA 0x0008
825 #define IPV6_SADDR_SCORE_OIF 0x0010
826 #define IPV6_SADDR_SCORE_LABEL 0x0020
827 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
829 static int inline ipv6_saddr_preferred(int type)
831 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
832 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
837 /* static matching label */
838 static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
841 * prefix (longest match) label
842 * -----------------------------
849 if (type & IPV6_ADDR_LOOPBACK)
851 else if (type & IPV6_ADDR_COMPATv4)
853 else if (type & IPV6_ADDR_MAPPED)
855 else if (addr->s6_addr16[0] == htons(0x2002))
860 int ipv6_dev_get_saddr(struct net_device *daddr_dev,
861 struct in6_addr *daddr, struct in6_addr *saddr)
863 struct ipv6_saddr_score hiscore;
864 struct inet6_ifaddr *ifa_result = NULL;
865 int daddr_type = __ipv6_addr_type(daddr);
866 int daddr_scope = __ipv6_addr_src_scope(daddr_type);
867 u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
868 struct net_device *dev;
870 memset(&hiscore, 0, sizeof(hiscore));
872 read_lock(&dev_base_lock);
873 read_lock(&addrconf_lock);
875 for (dev = dev_base; dev; dev=dev->next) {
876 struct inet6_dev *idev;
877 struct inet6_ifaddr *ifa;
879 /* Rule 0: Candidate Source Address (section 4)
880 * - multicast and link-local destination address,
881 * the set of candidate source address MUST only
882 * include addresses assigned to interfaces
883 * belonging to the same link as the outgoing
885 * (- For site-local destination addresses, the
886 * set of candidate source addresses MUST only
887 * include addresses assigned to interfaces
888 * belonging to the same site as the outgoing
891 if ((daddr_type & IPV6_ADDR_MULTICAST ||
892 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
893 daddr_dev && dev != daddr_dev)
896 idev = __in6_dev_get(dev);
900 read_lock_bh(&idev->lock);
901 for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
902 struct ipv6_saddr_score score;
904 score.addr_type = __ipv6_addr_type(&ifa->addr);
906 /* Rule 0: Candidate Source Address (section 4)
907 * - In any case, anycast addresses, multicast
908 * addresses, and the unspecified address MUST
909 * NOT be included in a candidate set.
911 if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
912 score.addr_type & IPV6_ADDR_MULTICAST)) {
913 LIMIT_NETDEBUG(KERN_DEBUG
914 "ADDRCONF: unspecified / multicast address"
915 "assigned as unicast address on %s",
925 if (ifa_result == NULL) {
926 /* record it if the first available entry */
930 /* Rule 1: Prefer same address */
931 if (hiscore.rule < 1) {
932 if (ipv6_addr_equal(&ifa_result->addr, daddr))
933 hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
936 if (ipv6_addr_equal(&ifa->addr, daddr)) {
937 score.attrs |= IPV6_SADDR_SCORE_LOCAL;
938 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
943 if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
947 /* Rule 2: Prefer appropriate scope */
948 if (hiscore.rule < 2) {
949 hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
952 score.scope = __ipv6_addr_src_scope(score.addr_type);
953 if (hiscore.scope < score.scope) {
954 if (hiscore.scope < daddr_scope) {
959 } else if (score.scope < hiscore.scope) {
960 if (score.scope < daddr_scope)
968 /* Rule 3: Avoid deprecated address */
969 if (hiscore.rule < 3) {
970 if (ipv6_saddr_preferred(hiscore.addr_type) ||
971 !(ifa_result->flags & IFA_F_DEPRECATED))
972 hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
975 if (ipv6_saddr_preferred(score.addr_type) ||
976 !(ifa->flags & IFA_F_DEPRECATED)) {
977 score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
978 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
983 if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
987 /* Rule 4: Prefer home address -- not implemented yet */
989 /* Rule 5: Prefer outgoing interface */
990 if (hiscore.rule < 5) {
991 if (daddr_dev == NULL ||
992 daddr_dev == ifa_result->idev->dev)
993 hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
996 if (daddr_dev == NULL ||
997 daddr_dev == ifa->idev->dev) {
998 score.attrs |= IPV6_SADDR_SCORE_OIF;
999 if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1004 if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1008 /* Rule 6: Prefer matching label */
1009 if (hiscore.rule < 6) {
1010 if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1011 hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1014 if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1015 score.attrs |= IPV6_SADDR_SCORE_LABEL;
1016 if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1021 if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1025 #ifdef CONFIG_IPV6_PRIVACY
1026 /* Rule 7: Prefer public address
1027 * Note: prefer temprary address if use_tempaddr >= 2
1029 if (hiscore.rule < 7) {
1030 if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1031 (ifa_result->idev->cnf.use_tempaddr >= 2))
1032 hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1035 if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1036 (ifa->idev->cnf.use_tempaddr >= 2)) {
1037 score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1038 if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1043 if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1047 /* Rule 8: Use longest matching prefix */
1048 if (hiscore.rule < 8) {
1049 hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
1052 score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1053 if (score.matchlen > hiscore.matchlen) {
1058 else if (score.matchlen < hiscore.matchlen)
1062 /* Final Rule: choose first available one */
1066 in6_ifa_put(ifa_result);
1071 read_unlock_bh(&idev->lock);
1073 read_unlock(&addrconf_lock);
1074 read_unlock(&dev_base_lock);
1077 return -EADDRNOTAVAIL;
1079 ipv6_addr_copy(saddr, &ifa_result->addr);
1080 in6_ifa_put(ifa_result);
1085 int ipv6_get_saddr(struct dst_entry *dst,
1086 struct in6_addr *daddr, struct in6_addr *saddr)
1088 return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
1092 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
1094 struct inet6_dev *idev;
1095 int err = -EADDRNOTAVAIL;
1097 read_lock(&addrconf_lock);
1098 if ((idev = __in6_dev_get(dev)) != NULL) {
1099 struct inet6_ifaddr *ifp;
1101 read_lock_bh(&idev->lock);
1102 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1103 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1104 ipv6_addr_copy(addr, &ifp->addr);
1109 read_unlock_bh(&idev->lock);
1111 read_unlock(&addrconf_lock);
1115 static int ipv6_count_addresses(struct inet6_dev *idev)
1118 struct inet6_ifaddr *ifp;
1120 read_lock_bh(&idev->lock);
1121 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1123 read_unlock_bh(&idev->lock);
1127 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1129 struct inet6_ifaddr * ifp;
1130 u8 hash = ipv6_addr_hash(addr);
1132 read_lock_bh(&addrconf_hash_lock);
1133 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1134 if (ipv6_addr_equal(&ifp->addr, addr) &&
1135 !(ifp->flags&IFA_F_TENTATIVE)) {
1136 if (dev == NULL || ifp->idev->dev == dev ||
1137 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1141 read_unlock_bh(&addrconf_hash_lock);
1146 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1148 struct inet6_ifaddr * ifp;
1149 u8 hash = ipv6_addr_hash(addr);
1151 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1152 if (ipv6_addr_equal(&ifp->addr, addr)) {
1153 if (dev == NULL || ifp->idev->dev == dev)
1160 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1162 struct inet6_ifaddr * ifp;
1163 u8 hash = ipv6_addr_hash(addr);
1165 read_lock_bh(&addrconf_hash_lock);
1166 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1167 if (ipv6_addr_equal(&ifp->addr, addr)) {
1168 if (dev == NULL || ifp->idev->dev == dev ||
1169 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1175 read_unlock_bh(&addrconf_hash_lock);
1180 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1182 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1183 const struct in6_addr *sk2_rcv_saddr6 = tcp_v6_rcv_saddr(sk2);
1184 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1185 u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
1186 int sk_ipv6only = ipv6_only_sock(sk);
1187 int sk2_ipv6only = inet_v6_ipv6only(sk2);
1188 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1189 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1191 if (!sk2_rcv_saddr && !sk_ipv6only)
1194 if (addr_type2 == IPV6_ADDR_ANY &&
1195 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1198 if (addr_type == IPV6_ADDR_ANY &&
1199 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1202 if (sk2_rcv_saddr6 &&
1203 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
1206 if (addr_type == IPV6_ADDR_MAPPED &&
1208 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1214 /* Gets referenced address, destroys ifaddr */
1216 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1218 if (net_ratelimit())
1219 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1220 if (ifp->flags&IFA_F_PERMANENT) {
1221 spin_lock_bh(&ifp->lock);
1222 addrconf_del_timer(ifp);
1223 ifp->flags |= IFA_F_TENTATIVE;
1224 spin_unlock_bh(&ifp->lock);
1226 #ifdef CONFIG_IPV6_PRIVACY
1227 } else if (ifp->flags&IFA_F_TEMPORARY) {
1228 struct inet6_ifaddr *ifpub;
1229 spin_lock_bh(&ifp->lock);
1232 in6_ifa_hold(ifpub);
1233 spin_unlock_bh(&ifp->lock);
1234 ipv6_create_tempaddr(ifpub, ifp);
1237 spin_unlock_bh(&ifp->lock);
1246 /* Join to solicited addr multicast group. */
1248 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1250 struct in6_addr maddr;
1252 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1255 addrconf_addr_solict_mult(addr, &maddr);
1256 ipv6_dev_mc_inc(dev, &maddr);
1259 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1261 struct in6_addr maddr;
1263 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1266 addrconf_addr_solict_mult(addr, &maddr);
1267 __ipv6_dev_mc_dec(idev, &maddr);
1270 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1272 struct in6_addr addr;
1273 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1274 if (ipv6_addr_any(&addr))
1276 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1279 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1281 struct in6_addr addr;
1282 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1283 if (ipv6_addr_any(&addr))
1285 __ipv6_dev_ac_dec(ifp->idev, &addr);
1288 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1290 switch (dev->type) {
1293 case ARPHRD_IEEE802_TR:
1294 if (dev->addr_len != ETH_ALEN)
1296 memcpy(eui, dev->dev_addr, 3);
1297 memcpy(eui + 5, dev->dev_addr + 3, 3);
1300 * The zSeries OSA network cards can be shared among various
1301 * OS instances, but the OSA cards have only one MAC address.
1302 * This leads to duplicate address conflicts in conjunction
1303 * with IPv6 if more than one instance uses the same card.
1305 * The driver for these cards can deliver a unique 16-bit
1306 * identifier for each instance sharing the same card. It is
1307 * placed instead of 0xFFFE in the interface identifier. The
1308 * "u" bit of the interface identifier is not inverted in this
1309 * case. Hence the resulting interface identifier has local
1310 * scope according to RFC2373.
1313 eui[3] = (dev->dev_id >> 8) & 0xFF;
1314 eui[4] = dev->dev_id & 0xFF;
1322 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1323 if (dev->addr_len != ARCNET_ALEN)
1326 eui[7] = *(u8*)dev->dev_addr;
1328 case ARPHRD_INFINIBAND:
1329 if (dev->addr_len != INFINIBAND_ALEN)
1331 memcpy(eui, dev->dev_addr + 12, 8);
1338 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1341 struct inet6_ifaddr *ifp;
1343 read_lock_bh(&idev->lock);
1344 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1345 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1346 memcpy(eui, ifp->addr.s6_addr+8, 8);
1351 read_unlock_bh(&idev->lock);
1355 #ifdef CONFIG_IPV6_PRIVACY
1356 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1357 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1359 struct net_device *dev;
1360 struct scatterlist sg[2];
1362 sg_set_buf(&sg[0], idev->entropy, 8);
1363 sg_set_buf(&sg[1], idev->work_eui64, 8);
1367 if (ipv6_generate_eui64(idev->work_eui64, dev)) {
1369 "__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
1371 get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
1374 spin_lock(&md5_tfm_lock);
1375 if (unlikely(md5_tfm == NULL)) {
1376 spin_unlock(&md5_tfm_lock);
1379 crypto_digest_init(md5_tfm);
1380 crypto_digest_update(md5_tfm, sg, 2);
1381 crypto_digest_final(md5_tfm, idev->work_digest);
1382 spin_unlock(&md5_tfm_lock);
1384 memcpy(idev->rndid, &idev->work_digest[0], 8);
1385 idev->rndid[0] &= ~0x02;
1386 memcpy(idev->entropy, &idev->work_digest[8], 8);
1389 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1390 * check if generated address is not inappropriate
1392 * - Reserved subnet anycast (RFC 2526)
1393 * 11111101 11....11 1xxxxxxx
1394 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1395 * 00-00-5E-FE-xx-xx-xx-xx
1397 * - XXX: already assigned to an address on the device
1399 if (idev->rndid[0] == 0xfd &&
1400 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1401 (idev->rndid[7]&0x80))
1403 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1404 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1406 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1413 static void ipv6_regen_rndid(unsigned long data)
1415 struct inet6_dev *idev = (struct inet6_dev *) data;
1416 unsigned long expires;
1418 read_lock_bh(&addrconf_lock);
1419 write_lock_bh(&idev->lock);
1424 if (__ipv6_regen_rndid(idev) < 0)
1428 idev->cnf.temp_prefered_lft * HZ -
1429 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1430 if (time_before(expires, jiffies)) {
1432 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1437 if (!mod_timer(&idev->regen_timer, expires))
1441 write_unlock_bh(&idev->lock);
1442 read_unlock_bh(&addrconf_lock);
1446 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1449 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1450 ret = __ipv6_regen_rndid(idev);
1460 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1461 unsigned long expires, u32 flags)
1463 struct in6_rtmsg rtmsg;
1465 memset(&rtmsg, 0, sizeof(rtmsg));
1466 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1467 rtmsg.rtmsg_dst_len = plen;
1468 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1469 rtmsg.rtmsg_ifindex = dev->ifindex;
1470 rtmsg.rtmsg_info = expires;
1471 rtmsg.rtmsg_flags = RTF_UP|flags;
1472 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1474 /* Prevent useless cloning on PtP SIT.
1475 This thing is done here expecting that the whole
1476 class of non-broadcast devices need not cloning.
1478 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1479 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1481 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1484 /* Create "default" multicast route to the interface */
1486 static void addrconf_add_mroute(struct net_device *dev)
1488 struct in6_rtmsg rtmsg;
1490 memset(&rtmsg, 0, sizeof(rtmsg));
1491 ipv6_addr_set(&rtmsg.rtmsg_dst,
1492 htonl(0xFF000000), 0, 0, 0);
1493 rtmsg.rtmsg_dst_len = 8;
1494 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1495 rtmsg.rtmsg_ifindex = dev->ifindex;
1496 rtmsg.rtmsg_flags = RTF_UP;
1497 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1498 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1501 static void sit_route_add(struct net_device *dev)
1503 struct in6_rtmsg rtmsg;
1505 memset(&rtmsg, 0, sizeof(rtmsg));
1507 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1508 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1510 /* prefix length - 96 bits "::d.d.d.d" */
1511 rtmsg.rtmsg_dst_len = 96;
1512 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1513 rtmsg.rtmsg_ifindex = dev->ifindex;
1515 ip6_route_add(&rtmsg, NULL, NULL, NULL);
1518 static void addrconf_add_lroute(struct net_device *dev)
1520 struct in6_addr addr;
1522 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1523 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1526 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1528 struct inet6_dev *idev;
1532 if ((idev = ipv6_find_idev(dev)) == NULL)
1535 /* Add default multicast route */
1536 addrconf_add_mroute(dev);
1538 /* Add link local route */
1539 addrconf_add_lroute(dev);
1543 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1545 struct prefix_info *pinfo;
1549 unsigned long rt_expires;
1550 struct inet6_dev *in6_dev;
1552 pinfo = (struct prefix_info *) opt;
1554 if (len < sizeof(struct prefix_info)) {
1555 ADBG(("addrconf: prefix option too short\n"));
1560 * Validation checks ([ADDRCONF], page 19)
1563 addr_type = ipv6_addr_type(&pinfo->prefix);
1565 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1568 valid_lft = ntohl(pinfo->valid);
1569 prefered_lft = ntohl(pinfo->prefered);
1571 if (prefered_lft > valid_lft) {
1572 if (net_ratelimit())
1573 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1577 in6_dev = in6_dev_get(dev);
1579 if (in6_dev == NULL) {
1580 if (net_ratelimit())
1581 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1586 * Two things going on here:
1587 * 1) Add routes for on-link prefixes
1588 * 2) Configure prefixes with the auto flag set
1591 /* Avoid arithmetic overflow. Really, we could
1592 save rt_expires in seconds, likely valid_lft,
1593 but it would require division in fib gc, that it
1596 if (valid_lft >= 0x7FFFFFFF/HZ)
1599 rt_expires = jiffies + valid_lft * HZ;
1601 if (pinfo->onlink) {
1602 struct rt6_info *rt;
1603 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1605 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1606 if (rt->rt6i_flags&RTF_EXPIRES) {
1607 if (valid_lft == 0) {
1608 ip6_del_rt(rt, NULL, NULL, NULL);
1611 rt->rt6i_expires = rt_expires;
1614 } else if (valid_lft) {
1615 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1616 dev, rt_expires, RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1619 dst_release(&rt->u.dst);
1622 /* Try to figure out our local address for this prefix */
1624 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1625 struct inet6_ifaddr * ifp;
1626 struct in6_addr addr;
1627 int create = 0, update_lft = 0;
1629 if (pinfo->prefix_len == 64) {
1630 memcpy(&addr, &pinfo->prefix, 8);
1631 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1632 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1633 in6_dev_put(in6_dev);
1638 if (net_ratelimit())
1639 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1641 in6_dev_put(in6_dev);
1646 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1648 if (ifp == NULL && valid_lft) {
1649 int max_addresses = in6_dev->cnf.max_addresses;
1651 /* Do not allow to create too much of autoconfigured
1652 * addresses; this would be too easy way to crash kernel.
1654 if (!max_addresses ||
1655 ipv6_count_addresses(in6_dev) < max_addresses)
1656 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1657 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1659 if (!ifp || IS_ERR(ifp)) {
1660 in6_dev_put(in6_dev);
1664 update_lft = create = 1;
1665 ifp->cstamp = jiffies;
1666 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1672 #ifdef CONFIG_IPV6_PRIVACY
1673 struct inet6_ifaddr *ift;
1677 /* update lifetime (RFC2462 5.5.3 e) */
1678 spin_lock(&ifp->lock);
1680 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1681 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1684 if (!update_lft && stored_lft) {
1685 if (valid_lft > MIN_VALID_LIFETIME ||
1686 valid_lft > stored_lft)
1688 else if (stored_lft <= MIN_VALID_LIFETIME) {
1689 /* valid_lft <= stored_lft is always true */
1693 valid_lft = MIN_VALID_LIFETIME;
1694 if (valid_lft < prefered_lft)
1695 prefered_lft = valid_lft;
1701 ifp->valid_lft = valid_lft;
1702 ifp->prefered_lft = prefered_lft;
1705 ifp->flags &= ~IFA_F_DEPRECATED;
1706 spin_unlock(&ifp->lock);
1708 if (!(flags&IFA_F_TENTATIVE))
1709 ipv6_ifa_notify(0, ifp);
1711 spin_unlock(&ifp->lock);
1713 #ifdef CONFIG_IPV6_PRIVACY
1714 read_lock_bh(&in6_dev->lock);
1715 /* update all temporary addresses in the list */
1716 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1718 * When adjusting the lifetimes of an existing
1719 * temporary address, only lower the lifetimes.
1720 * Implementations must not increase the
1721 * lifetimes of an existing temporary address
1722 * when processing a Prefix Information Option.
1724 spin_lock(&ift->lock);
1726 if (ift->valid_lft > valid_lft &&
1727 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1728 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1729 if (ift->prefered_lft > prefered_lft &&
1730 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1731 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1732 spin_unlock(&ift->lock);
1733 if (!(flags&IFA_F_TENTATIVE))
1734 ipv6_ifa_notify(0, ift);
1737 if (create && in6_dev->cnf.use_tempaddr > 0) {
1739 * When a new public address is created as described in [ADDRCONF],
1740 * also create a new temporary address.
1742 read_unlock_bh(&in6_dev->lock);
1743 ipv6_create_tempaddr(ifp, NULL);
1745 read_unlock_bh(&in6_dev->lock);
1752 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1753 in6_dev_put(in6_dev);
1757 * Set destination address.
1758 * Special case for SIT interfaces where we create a new "virtual"
1761 int addrconf_set_dstaddr(void __user *arg)
1763 struct in6_ifreq ireq;
1764 struct net_device *dev;
1770 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1773 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1779 if (dev->type == ARPHRD_SIT) {
1782 struct ip_tunnel_parm p;
1784 err = -EADDRNOTAVAIL;
1785 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1788 memset(&p, 0, sizeof(p));
1789 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1793 p.iph.protocol = IPPROTO_IPV6;
1795 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1797 oldfs = get_fs(); set_fs(KERNEL_DS);
1798 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1803 if ((dev = __dev_get_by_name(p.name)) == NULL)
1805 err = dev_open(dev);
1815 * Manual configuration of address on an interface
1817 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1819 struct inet6_ifaddr *ifp;
1820 struct inet6_dev *idev;
1821 struct net_device *dev;
1826 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1829 if (!(dev->flags&IFF_UP))
1832 if ((idev = addrconf_add_dev(dev)) == NULL)
1835 scope = ipv6_addr_scope(pfx);
1837 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1839 addrconf_dad_start(ifp, 0);
1844 return PTR_ERR(ifp);
1847 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1849 struct inet6_ifaddr *ifp;
1850 struct inet6_dev *idev;
1851 struct net_device *dev;
1853 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1856 if ((idev = __in6_dev_get(dev)) == NULL)
1859 read_lock_bh(&idev->lock);
1860 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1861 if (ifp->prefix_len == plen &&
1862 ipv6_addr_equal(pfx, &ifp->addr)) {
1864 read_unlock_bh(&idev->lock);
1868 /* If the last address is deleted administratively,
1869 disable IPv6 on this interface.
1871 if (idev->addr_list == NULL)
1872 addrconf_ifdown(idev->dev, 1);
1876 read_unlock_bh(&idev->lock);
1877 return -EADDRNOTAVAIL;
1881 int addrconf_add_ifaddr(void __user *arg)
1883 struct in6_ifreq ireq;
1886 if (!capable(CAP_NET_ADMIN))
1889 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1893 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1898 int addrconf_del_ifaddr(void __user *arg)
1900 struct in6_ifreq ireq;
1903 if (!capable(CAP_NET_ADMIN))
1906 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1910 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1915 static void sit_add_v4_addrs(struct inet6_dev *idev)
1917 struct inet6_ifaddr * ifp;
1918 struct in6_addr addr;
1919 struct net_device *dev;
1924 memset(&addr, 0, sizeof(struct in6_addr));
1925 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1927 if (idev->dev->flags&IFF_POINTOPOINT) {
1928 addr.s6_addr32[0] = htonl(0xfe800000);
1931 scope = IPV6_ADDR_COMPATv4;
1934 if (addr.s6_addr32[3]) {
1935 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1937 spin_lock_bh(&ifp->lock);
1938 ifp->flags &= ~IFA_F_TENTATIVE;
1939 spin_unlock_bh(&ifp->lock);
1940 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1946 for (dev = dev_base; dev != NULL; dev = dev->next) {
1947 struct in_device * in_dev = __in_dev_get_rtnl(dev);
1948 if (in_dev && (dev->flags & IFF_UP)) {
1949 struct in_ifaddr * ifa;
1953 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1956 addr.s6_addr32[3] = ifa->ifa_local;
1958 if (ifa->ifa_scope == RT_SCOPE_LINK)
1960 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1961 if (idev->dev->flags&IFF_POINTOPOINT)
1965 if (idev->dev->flags&IFF_POINTOPOINT)
1970 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1973 spin_lock_bh(&ifp->lock);
1974 ifp->flags &= ~IFA_F_TENTATIVE;
1975 spin_unlock_bh(&ifp->lock);
1976 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1984 static void init_loopback(struct net_device *dev)
1986 struct inet6_dev *idev;
1987 struct inet6_ifaddr * ifp;
1993 if ((idev = ipv6_find_idev(dev)) == NULL) {
1994 printk(KERN_DEBUG "init loopback: add_dev failed\n");
1998 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
2000 spin_lock_bh(&ifp->lock);
2001 ifp->flags &= ~IFA_F_TENTATIVE;
2002 spin_unlock_bh(&ifp->lock);
2003 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2008 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
2010 struct inet6_ifaddr * ifp;
2012 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
2014 addrconf_dad_start(ifp, 0);
2019 static void addrconf_dev_config(struct net_device *dev)
2021 struct in6_addr addr;
2022 struct inet6_dev * idev;
2026 if ((dev->type != ARPHRD_ETHER) &&
2027 (dev->type != ARPHRD_FDDI) &&
2028 (dev->type != ARPHRD_IEEE802_TR) &&
2029 (dev->type != ARPHRD_ARCNET) &&
2030 (dev->type != ARPHRD_INFINIBAND)) {
2031 /* Alas, we support only Ethernet autoconfiguration. */
2035 idev = addrconf_add_dev(dev);
2039 memset(&addr, 0, sizeof(struct in6_addr));
2040 addr.s6_addr32[0] = htonl(0xFE800000);
2042 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2043 addrconf_add_linklocal(idev, &addr);
2046 static void addrconf_sit_config(struct net_device *dev)
2048 struct inet6_dev *idev;
2053 * Configure the tunnel with one of our IPv4
2054 * addresses... we should configure all of
2055 * our v4 addrs in the tunnel
2058 if ((idev = ipv6_find_idev(dev)) == NULL) {
2059 printk(KERN_DEBUG "init sit: add_dev failed\n");
2063 sit_add_v4_addrs(idev);
2065 if (dev->flags&IFF_POINTOPOINT) {
2066 addrconf_add_mroute(dev);
2067 addrconf_add_lroute(dev);
2073 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2075 struct in6_addr lladdr;
2077 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2078 addrconf_add_linklocal(idev, &lladdr);
2084 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2086 struct net_device *link_dev;
2088 /* first try to inherit the link-local address from the link device */
2089 if (idev->dev->iflink &&
2090 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2091 if (!ipv6_inherit_linklocal(idev, link_dev))
2094 /* then try to inherit it from any device */
2095 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2096 if (!ipv6_inherit_linklocal(idev, link_dev))
2099 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2103 * Autoconfigure tunnel with a link-local address so routing protocols,
2104 * DHCPv6, MLD etc. can be run over the virtual link
2107 static void addrconf_ip6_tnl_config(struct net_device *dev)
2109 struct inet6_dev *idev;
2113 if ((idev = addrconf_add_dev(dev)) == NULL) {
2114 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2117 ip6_tnl_add_linklocal(idev);
2118 addrconf_add_mroute(dev);
2121 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2124 struct net_device *dev = (struct net_device *) data;
2125 struct inet6_dev *idev = __in6_dev_get(dev);
2131 addrconf_sit_config(dev);
2133 case ARPHRD_TUNNEL6:
2134 addrconf_ip6_tnl_config(dev);
2136 case ARPHRD_LOOPBACK:
2141 addrconf_dev_config(dev);
2145 /* If the MTU changed during the interface down, when the
2146 interface up, the changed MTU must be reflected in the
2147 idev as well as routers.
2149 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2150 rt6_mtu_change(dev, dev->mtu);
2151 idev->cnf.mtu6 = dev->mtu;
2153 idev->tstamp = jiffies;
2154 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2155 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2156 stop IPv6 on this interface.
2158 if (dev->mtu < IPV6_MIN_MTU)
2159 addrconf_ifdown(dev, event != NETDEV_DOWN);
2163 case NETDEV_CHANGEMTU:
2164 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2165 rt6_mtu_change(dev, dev->mtu);
2166 idev->cnf.mtu6 = dev->mtu;
2170 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2173 case NETDEV_UNREGISTER:
2175 * Remove all addresses from this interface.
2177 addrconf_ifdown(dev, event != NETDEV_DOWN);
2181 case NETDEV_CHANGENAME:
2182 #ifdef CONFIG_SYSCTL
2184 addrconf_sysctl_unregister(&idev->cnf);
2185 neigh_sysctl_unregister(idev->nd_parms);
2186 neigh_sysctl_register(dev, idev->nd_parms,
2187 NET_IPV6, NET_IPV6_NEIGH, "ipv6",
2188 &ndisc_ifinfo_sysctl_change,
2190 addrconf_sysctl_register(idev, &idev->cnf);
2200 * addrconf module should be notified of a device going up
2202 static struct notifier_block ipv6_dev_notf = {
2203 .notifier_call = addrconf_notify,
2207 static int addrconf_ifdown(struct net_device *dev, int how)
2209 struct inet6_dev *idev;
2210 struct inet6_ifaddr *ifa, **bifa;
2215 if (dev == &loopback_dev && how == 1)
2219 neigh_ifdown(&nd_tbl, dev);
2221 idev = __in6_dev_get(dev);
2225 /* Step 1: remove reference to ipv6 device from parent device.
2229 write_lock_bh(&addrconf_lock);
2230 dev->ip6_ptr = NULL;
2232 write_unlock_bh(&addrconf_lock);
2234 /* Step 1.5: remove snmp6 entry */
2235 snmp6_unregister_dev(idev);
2239 /* Step 2: clear hash table */
2240 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2241 bifa = &inet6_addr_lst[i];
2243 write_lock_bh(&addrconf_hash_lock);
2244 while ((ifa = *bifa) != NULL) {
2245 if (ifa->idev == idev) {
2246 *bifa = ifa->lst_next;
2247 ifa->lst_next = NULL;
2248 addrconf_del_timer(ifa);
2252 bifa = &ifa->lst_next;
2254 write_unlock_bh(&addrconf_hash_lock);
2257 write_lock_bh(&idev->lock);
2259 /* Step 3: clear flags for stateless addrconf */
2261 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD);
2263 /* Step 4: clear address list */
2264 #ifdef CONFIG_IPV6_PRIVACY
2265 if (how == 1 && del_timer(&idev->regen_timer))
2268 /* clear tempaddr list */
2269 while ((ifa = idev->tempaddr_list) != NULL) {
2270 idev->tempaddr_list = ifa->tmp_next;
2271 ifa->tmp_next = NULL;
2273 write_unlock_bh(&idev->lock);
2274 spin_lock_bh(&ifa->lock);
2277 in6_ifa_put(ifa->ifpub);
2280 spin_unlock_bh(&ifa->lock);
2282 write_lock_bh(&idev->lock);
2285 while ((ifa = idev->addr_list) != NULL) {
2286 idev->addr_list = ifa->if_next;
2287 ifa->if_next = NULL;
2289 addrconf_del_timer(ifa);
2290 write_unlock_bh(&idev->lock);
2292 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2295 write_lock_bh(&idev->lock);
2297 write_unlock_bh(&idev->lock);
2299 /* Step 5: Discard multicast list */
2302 ipv6_mc_destroy_dev(idev);
2306 /* Step 5: netlink notification of this interface */
2307 idev->tstamp = jiffies;
2308 inet6_ifinfo_notify(RTM_DELLINK, idev);
2310 /* Shot the device (if unregistered) */
2313 #ifdef CONFIG_SYSCTL
2314 addrconf_sysctl_unregister(&idev->cnf);
2315 neigh_sysctl_unregister(idev->nd_parms);
2317 neigh_parms_release(&nd_tbl, idev->nd_parms);
2318 neigh_ifdown(&nd_tbl, dev);
2324 static void addrconf_rs_timer(unsigned long data)
2326 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2328 if (ifp->idev->cnf.forwarding)
2331 if (ifp->idev->if_flags & IF_RA_RCVD) {
2333 * Announcement received after solicitation
2339 spin_lock(&ifp->lock);
2340 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2341 struct in6_addr all_routers;
2343 /* The wait after the last probe can be shorter */
2344 addrconf_mod_timer(ifp, AC_RS,
2345 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2346 ifp->idev->cnf.rtr_solicit_delay :
2347 ifp->idev->cnf.rtr_solicit_interval);
2348 spin_unlock(&ifp->lock);
2350 ipv6_addr_all_routers(&all_routers);
2352 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2354 spin_unlock(&ifp->lock);
2356 * Note: we do not support deprecated "all on-link"
2357 * assumption any longer.
2359 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2360 ifp->idev->dev->name);
2368 * Duplicate Address Detection
2370 static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
2372 struct inet6_dev *idev = ifp->idev;
2373 struct net_device *dev = idev->dev;
2374 unsigned long rand_num;
2376 addrconf_join_solict(dev, &ifp->addr);
2378 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2379 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2382 net_srandom(ifp->addr.s6_addr32[3]);
2383 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2385 read_lock_bh(&idev->lock);
2388 spin_lock_bh(&ifp->lock);
2390 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2391 !(ifp->flags&IFA_F_TENTATIVE)) {
2392 ifp->flags &= ~IFA_F_TENTATIVE;
2393 spin_unlock_bh(&ifp->lock);
2394 read_unlock_bh(&idev->lock);
2396 addrconf_dad_completed(ifp);
2400 ifp->probes = idev->cnf.dad_transmits;
2401 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2403 spin_unlock_bh(&ifp->lock);
2405 read_unlock_bh(&idev->lock);
2408 static void addrconf_dad_timer(unsigned long data)
2410 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2411 struct inet6_dev *idev = ifp->idev;
2412 struct in6_addr unspec;
2413 struct in6_addr mcaddr;
2415 read_lock_bh(&idev->lock);
2417 read_unlock_bh(&idev->lock);
2420 spin_lock_bh(&ifp->lock);
2421 if (ifp->probes == 0) {
2423 * DAD was successful
2426 ifp->flags &= ~IFA_F_TENTATIVE;
2427 spin_unlock_bh(&ifp->lock);
2428 read_unlock_bh(&idev->lock);
2430 addrconf_dad_completed(ifp);
2436 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2437 spin_unlock_bh(&ifp->lock);
2438 read_unlock_bh(&idev->lock);
2440 /* send a neighbour solicitation for our addr */
2441 memset(&unspec, 0, sizeof(unspec));
2442 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2443 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2448 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2450 struct net_device * dev = ifp->idev->dev;
2453 * Configure the address for reception. Now it is valid.
2456 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2458 /* If added prefix is link local and forwarding is off,
2459 start sending router solicitations.
2462 if (ifp->idev->cnf.forwarding == 0 &&
2463 ifp->idev->cnf.rtr_solicits > 0 &&
2464 (dev->flags&IFF_LOOPBACK) == 0 &&
2465 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2466 struct in6_addr all_routers;
2468 ipv6_addr_all_routers(&all_routers);
2471 * If a host as already performed a random delay
2472 * [...] as part of DAD [...] there is no need
2473 * to delay again before sending the first RS
2475 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2477 spin_lock_bh(&ifp->lock);
2479 ifp->idev->if_flags |= IF_RS_SENT;
2480 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2481 spin_unlock_bh(&ifp->lock);
2485 #ifdef CONFIG_PROC_FS
2486 struct if6_iter_state {
2490 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2492 struct inet6_ifaddr *ifa = NULL;
2493 struct if6_iter_state *state = seq->private;
2495 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2496 ifa = inet6_addr_lst[state->bucket];
2503 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2505 struct if6_iter_state *state = seq->private;
2507 ifa = ifa->lst_next;
2509 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2510 ifa = inet6_addr_lst[state->bucket];
2516 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2518 struct inet6_ifaddr *ifa = if6_get_first(seq);
2521 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2523 return pos ? NULL : ifa;
2526 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2528 read_lock_bh(&addrconf_hash_lock);
2529 return if6_get_idx(seq, *pos);
2532 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2534 struct inet6_ifaddr *ifa;
2536 ifa = if6_get_next(seq, v);
2541 static void if6_seq_stop(struct seq_file *seq, void *v)
2543 read_unlock_bh(&addrconf_hash_lock);
2546 static int if6_seq_show(struct seq_file *seq, void *v)
2548 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2550 "%04x%04x%04x%04x%04x%04x%04x%04x %02x %02x %02x %02x %8s\n",
2552 ifp->idev->dev->ifindex,
2556 ifp->idev->dev->name);
2560 static struct seq_operations if6_seq_ops = {
2561 .start = if6_seq_start,
2562 .next = if6_seq_next,
2563 .show = if6_seq_show,
2564 .stop = if6_seq_stop,
2567 static int if6_seq_open(struct inode *inode, struct file *file)
2569 struct seq_file *seq;
2571 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2575 memset(s, 0, sizeof(*s));
2577 rc = seq_open(file, &if6_seq_ops);
2581 seq = file->private_data;
2590 static struct file_operations if6_fops = {
2591 .owner = THIS_MODULE,
2592 .open = if6_seq_open,
2594 .llseek = seq_lseek,
2595 .release = seq_release_private,
2598 int __init if6_proc_init(void)
2600 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2605 void if6_proc_exit(void)
2607 proc_net_remove("if_inet6");
2609 #endif /* CONFIG_PROC_FS */
2612 * Periodic address status verification
2615 static void addrconf_verify(unsigned long foo)
2617 struct inet6_ifaddr *ifp;
2618 unsigned long now, next;
2621 spin_lock_bh(&addrconf_verify_lock);
2623 next = now + ADDR_CHECK_FREQUENCY;
2625 del_timer(&addr_chk_timer);
2627 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2630 read_lock(&addrconf_hash_lock);
2631 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2633 #ifdef CONFIG_IPV6_PRIVACY
2634 unsigned long regen_advance;
2637 if (ifp->flags & IFA_F_PERMANENT)
2640 spin_lock(&ifp->lock);
2641 age = (now - ifp->tstamp) / HZ;
2643 #ifdef CONFIG_IPV6_PRIVACY
2644 regen_advance = ifp->idev->cnf.regen_max_retry *
2645 ifp->idev->cnf.dad_transmits *
2646 ifp->idev->nd_parms->retrans_time / HZ;
2649 if (age >= ifp->valid_lft) {
2650 spin_unlock(&ifp->lock);
2652 read_unlock(&addrconf_hash_lock);
2655 } else if (age >= ifp->prefered_lft) {
2656 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2659 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2661 ifp->flags |= IFA_F_DEPRECATED;
2664 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2665 next = ifp->tstamp + ifp->valid_lft * HZ;
2667 spin_unlock(&ifp->lock);
2671 read_unlock(&addrconf_hash_lock);
2673 ipv6_ifa_notify(0, ifp);
2677 #ifdef CONFIG_IPV6_PRIVACY
2678 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2679 !(ifp->flags&IFA_F_TENTATIVE)) {
2680 if (age >= ifp->prefered_lft - regen_advance) {
2681 struct inet6_ifaddr *ifpub = ifp->ifpub;
2682 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2683 next = ifp->tstamp + ifp->prefered_lft * HZ;
2684 if (!ifp->regen_count && ifpub) {
2687 in6_ifa_hold(ifpub);
2688 spin_unlock(&ifp->lock);
2689 read_unlock(&addrconf_hash_lock);
2690 ipv6_create_tempaddr(ifpub, ifp);
2695 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2696 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2697 spin_unlock(&ifp->lock);
2700 /* ifp->prefered_lft <= ifp->valid_lft */
2701 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2702 next = ifp->tstamp + ifp->prefered_lft * HZ;
2703 spin_unlock(&ifp->lock);
2706 read_unlock(&addrconf_hash_lock);
2709 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2710 add_timer(&addr_chk_timer);
2711 spin_unlock_bh(&addrconf_verify_lock);
2715 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2717 struct rtattr **rta = arg;
2718 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2719 struct in6_addr *pfx;
2722 if (rta[IFA_ADDRESS-1]) {
2723 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2725 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2727 if (rta[IFA_LOCAL-1]) {
2728 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2730 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2735 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2739 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2741 struct rtattr **rta = arg;
2742 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2743 struct in6_addr *pfx;
2746 if (rta[IFA_ADDRESS-1]) {
2747 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2749 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2751 if (rta[IFA_LOCAL-1]) {
2752 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2754 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2759 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2762 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2763 u32 pid, u32 seq, int event, unsigned int flags)
2765 struct ifaddrmsg *ifm;
2766 struct nlmsghdr *nlh;
2767 struct ifa_cacheinfo ci;
2768 unsigned char *b = skb->tail;
2770 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2771 ifm = NLMSG_DATA(nlh);
2772 ifm->ifa_family = AF_INET6;
2773 ifm->ifa_prefixlen = ifa->prefix_len;
2774 ifm->ifa_flags = ifa->flags;
2775 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2776 if (ifa->scope&IFA_HOST)
2777 ifm->ifa_scope = RT_SCOPE_HOST;
2778 else if (ifa->scope&IFA_LINK)
2779 ifm->ifa_scope = RT_SCOPE_LINK;
2780 else if (ifa->scope&IFA_SITE)
2781 ifm->ifa_scope = RT_SCOPE_SITE;
2782 ifm->ifa_index = ifa->idev->dev->ifindex;
2783 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2784 if (!(ifa->flags&IFA_F_PERMANENT)) {
2785 ci.ifa_prefered = ifa->prefered_lft;
2786 ci.ifa_valid = ifa->valid_lft;
2787 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2788 long tval = (jiffies - ifa->tstamp)/HZ;
2789 ci.ifa_prefered -= tval;
2790 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2791 ci.ifa_valid -= tval;
2794 ci.ifa_prefered = INFINITY_LIFE_TIME;
2795 ci.ifa_valid = INFINITY_LIFE_TIME;
2797 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2798 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2799 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2800 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2801 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2802 nlh->nlmsg_len = skb->tail - b;
2807 skb_trim(skb, b - skb->data);
2811 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2812 u32 pid, u32 seq, int event, u16 flags)
2814 struct ifaddrmsg *ifm;
2815 struct nlmsghdr *nlh;
2816 struct ifa_cacheinfo ci;
2817 unsigned char *b = skb->tail;
2819 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2820 ifm = NLMSG_DATA(nlh);
2821 ifm->ifa_family = AF_INET6;
2822 ifm->ifa_prefixlen = 128;
2823 ifm->ifa_flags = IFA_F_PERMANENT;
2824 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2825 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2826 ifm->ifa_scope = RT_SCOPE_SITE;
2827 ifm->ifa_index = ifmca->idev->dev->ifindex;
2828 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2829 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2830 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2832 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2833 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2835 ci.ifa_prefered = INFINITY_LIFE_TIME;
2836 ci.ifa_valid = INFINITY_LIFE_TIME;
2837 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2838 nlh->nlmsg_len = skb->tail - b;
2843 skb_trim(skb, b - skb->data);
2847 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2848 u32 pid, u32 seq, int event, unsigned int flags)
2850 struct ifaddrmsg *ifm;
2851 struct nlmsghdr *nlh;
2852 struct ifa_cacheinfo ci;
2853 unsigned char *b = skb->tail;
2855 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
2856 ifm = NLMSG_DATA(nlh);
2857 ifm->ifa_family = AF_INET6;
2858 ifm->ifa_prefixlen = 128;
2859 ifm->ifa_flags = IFA_F_PERMANENT;
2860 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2861 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2862 ifm->ifa_scope = RT_SCOPE_SITE;
2863 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2864 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2865 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2866 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2868 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2869 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2871 ci.ifa_prefered = INFINITY_LIFE_TIME;
2872 ci.ifa_valid = INFINITY_LIFE_TIME;
2873 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2874 nlh->nlmsg_len = skb->tail - b;
2879 skb_trim(skb, b - skb->data);
2890 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2891 enum addr_type_t type)
2894 int s_idx, s_ip_idx;
2896 struct net_device *dev;
2897 struct inet6_dev *idev = NULL;
2898 struct inet6_ifaddr *ifa;
2899 struct ifmcaddr6 *ifmca;
2900 struct ifacaddr6 *ifaca;
2902 s_idx = cb->args[0];
2903 s_ip_idx = ip_idx = cb->args[1];
2904 read_lock(&dev_base_lock);
2906 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
2912 if ((idev = in6_dev_get(dev)) == NULL)
2914 read_lock_bh(&idev->lock);
2917 /* unicast address incl. temp addr */
2918 for (ifa = idev->addr_list; ifa;
2919 ifa = ifa->if_next, ip_idx++) {
2920 if (ip_idx < s_ip_idx)
2922 if ((err = inet6_fill_ifaddr(skb, ifa,
2923 NETLINK_CB(cb->skb).pid,
2924 cb->nlh->nlmsg_seq, RTM_NEWADDR,
2929 case MULTICAST_ADDR:
2930 /* multicast address */
2931 for (ifmca = idev->mc_list; ifmca;
2932 ifmca = ifmca->next, ip_idx++) {
2933 if (ip_idx < s_ip_idx)
2935 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
2936 NETLINK_CB(cb->skb).pid,
2937 cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
2943 /* anycast address */
2944 for (ifaca = idev->ac_list; ifaca;
2945 ifaca = ifaca->aca_next, ip_idx++) {
2946 if (ip_idx < s_ip_idx)
2948 if ((err = inet6_fill_ifacaddr(skb, ifaca,
2949 NETLINK_CB(cb->skb).pid,
2950 cb->nlh->nlmsg_seq, RTM_GETANYCAST,
2958 read_unlock_bh(&idev->lock);
2963 read_unlock_bh(&idev->lock);
2966 read_unlock(&dev_base_lock);
2968 cb->args[1] = ip_idx;
2972 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
2974 enum addr_type_t type = UNICAST_ADDR;
2975 return inet6_dump_addr(skb, cb, type);
2978 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
2980 enum addr_type_t type = MULTICAST_ADDR;
2981 return inet6_dump_addr(skb, cb, type);
2985 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
2987 enum addr_type_t type = ANYCAST_ADDR;
2988 return inet6_dump_addr(skb, cb, type);
2991 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
2993 struct sk_buff *skb;
2994 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
2996 skb = alloc_skb(size, GFP_ATOMIC);
2998 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
3001 if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
3003 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
3006 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
3007 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
3010 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
3011 __s32 *array, int bytes)
3013 memset(array, 0, bytes);
3014 array[DEVCONF_FORWARDING] = cnf->forwarding;
3015 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3016 array[DEVCONF_MTU6] = cnf->mtu6;
3017 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3018 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3019 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3020 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3021 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
3022 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
3023 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
3024 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3025 #ifdef CONFIG_IPV6_PRIVACY
3026 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3027 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3028 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3029 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3030 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3032 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
3035 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3036 u32 pid, u32 seq, int event, unsigned int flags)
3038 struct net_device *dev = idev->dev;
3039 __s32 *array = NULL;
3040 struct ifinfomsg *r;
3041 struct nlmsghdr *nlh;
3042 unsigned char *b = skb->tail;
3043 struct rtattr *subattr;
3044 __u32 mtu = dev->mtu;
3045 struct ifla_cacheinfo ci;
3047 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
3048 r = NLMSG_DATA(nlh);
3049 r->ifi_family = AF_INET6;
3051 r->ifi_type = dev->type;
3052 r->ifi_index = dev->ifindex;
3053 r->ifi_flags = dev_get_flags(dev);
3056 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
3059 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3061 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
3062 if (dev->ifindex != dev->iflink)
3063 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
3065 subattr = (struct rtattr*)skb->tail;
3067 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
3069 /* return the device flags */
3070 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
3072 /* return interface cacheinfo */
3073 ci.max_reasm_len = IPV6_MAXPLEN;
3074 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3075 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3076 ci.reachable_time = idev->nd_parms->reachable_time;
3077 ci.retrans_time = idev->nd_parms->retrans_time;
3078 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3080 /* return the device sysctl params */
3081 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
3082 goto rtattr_failure;
3083 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
3084 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
3086 /* XXX - Statistics/MC not implemented */
3087 subattr->rta_len = skb->tail - (u8*)subattr;
3089 nlh->nlmsg_len = skb->tail - b;
3096 skb_trim(skb, b - skb->data);
3100 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3103 int s_idx = cb->args[0];
3104 struct net_device *dev;
3105 struct inet6_dev *idev;
3107 read_lock(&dev_base_lock);
3108 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3111 if ((idev = in6_dev_get(dev)) == NULL)
3113 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3114 cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
3119 read_unlock(&dev_base_lock);
3125 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3127 struct sk_buff *skb;
3129 int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
3131 skb = alloc_skb(size, GFP_ATOMIC);
3133 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
3136 if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
3138 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
3141 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
3142 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
3145 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3146 struct prefix_info *pinfo, u32 pid, u32 seq,
3147 int event, unsigned int flags)
3149 struct prefixmsg *pmsg;
3150 struct nlmsghdr *nlh;
3151 unsigned char *b = skb->tail;
3152 struct prefix_cacheinfo ci;
3154 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
3155 pmsg = NLMSG_DATA(nlh);
3156 pmsg->prefix_family = AF_INET6;
3157 pmsg->prefix_pad1 = 0;
3158 pmsg->prefix_pad2 = 0;
3159 pmsg->prefix_ifindex = idev->dev->ifindex;
3160 pmsg->prefix_len = pinfo->prefix_len;
3161 pmsg->prefix_type = pinfo->type;
3162 pmsg->prefix_pad3 = 0;
3164 pmsg->prefix_flags = 0;
3166 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3167 if (pinfo->autoconf)
3168 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3170 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3172 ci.preferred_time = ntohl(pinfo->prefered);
3173 ci.valid_time = ntohl(pinfo->valid);
3174 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3176 nlh->nlmsg_len = skb->tail - b;
3181 skb_trim(skb, b - skb->data);
3185 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3186 struct prefix_info *pinfo)
3188 struct sk_buff *skb;
3189 int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
3191 skb = alloc_skb(size, GFP_ATOMIC);
3193 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
3196 if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
3198 netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
3201 NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
3202 netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
3205 static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
3206 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3207 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3208 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
3209 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
3210 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3211 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3212 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3213 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3214 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3215 .dumpit = inet6_dump_fib, },
3218 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3220 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3224 dst_hold(&ifp->rt->u.dst);
3225 if (ip6_ins_rt(ifp->rt, NULL, NULL, NULL))
3226 dst_release(&ifp->rt->u.dst);
3227 if (ifp->idev->cnf.forwarding)
3228 addrconf_join_anycast(ifp);
3231 if (ifp->idev->cnf.forwarding)
3232 addrconf_leave_anycast(ifp);
3233 addrconf_leave_solict(ifp->idev, &ifp->addr);
3234 dst_hold(&ifp->rt->u.dst);
3235 if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
3236 dst_free(&ifp->rt->u.dst);
3238 dst_release(&ifp->rt->u.dst);
3243 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3245 read_lock_bh(&addrconf_lock);
3246 if (likely(ifp->idev->dead == 0))
3247 __ipv6_ifa_notify(event, ifp);
3248 read_unlock_bh(&addrconf_lock);
3251 #ifdef CONFIG_SYSCTL
3254 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3255 void __user *buffer, size_t *lenp, loff_t *ppos)
3257 int *valp = ctl->data;
3261 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3263 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3264 if (valp != &ipv6_devconf.forwarding) {
3265 if ((!*valp) ^ (!val)) {
3266 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3269 dev_forward_change(idev);
3272 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3273 addrconf_forward_change();
3276 rt6_purge_dflt_routers();
3282 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3283 int __user *name, int nlen,
3284 void __user *oldval,
3285 size_t __user *oldlenp,
3286 void __user *newval, size_t newlen,
3289 int *valp = table->data;
3292 if (!newval || !newlen)
3294 if (newlen != sizeof(int))
3296 if (get_user(new, (int __user *)newval))
3300 if (oldval && oldlenp) {
3302 if (get_user(len, oldlenp))
3305 if (len > table->maxlen)
3306 len = table->maxlen;
3307 if (copy_to_user(oldval, valp, len))
3309 if (put_user(len, oldlenp))
3314 if (valp != &ipv6_devconf_dflt.forwarding) {
3315 if (valp != &ipv6_devconf.forwarding) {
3316 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3318 if (unlikely(idev == NULL))
3320 changed = (!*valp) ^ (!new);
3323 dev_forward_change(idev);
3326 addrconf_forward_change();
3330 rt6_purge_dflt_routers();
3337 static struct addrconf_sysctl_table
3339 struct ctl_table_header *sysctl_header;
3340 ctl_table addrconf_vars[__NET_IPV6_MAX];
3341 ctl_table addrconf_dev[2];
3342 ctl_table addrconf_conf_dir[2];
3343 ctl_table addrconf_proto_dir[2];
3344 ctl_table addrconf_root_dir[2];
3345 } addrconf_sysctl = {
3346 .sysctl_header = NULL,
3349 .ctl_name = NET_IPV6_FORWARDING,
3350 .procname = "forwarding",
3351 .data = &ipv6_devconf.forwarding,
3352 .maxlen = sizeof(int),
3354 .proc_handler = &addrconf_sysctl_forward,
3355 .strategy = &addrconf_sysctl_forward_strategy,
3358 .ctl_name = NET_IPV6_HOP_LIMIT,
3359 .procname = "hop_limit",
3360 .data = &ipv6_devconf.hop_limit,
3361 .maxlen = sizeof(int),
3363 .proc_handler = proc_dointvec,
3366 .ctl_name = NET_IPV6_MTU,
3368 .data = &ipv6_devconf.mtu6,
3369 .maxlen = sizeof(int),
3371 .proc_handler = &proc_dointvec,
3374 .ctl_name = NET_IPV6_ACCEPT_RA,
3375 .procname = "accept_ra",
3376 .data = &ipv6_devconf.accept_ra,
3377 .maxlen = sizeof(int),
3379 .proc_handler = &proc_dointvec,
3382 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3383 .procname = "accept_redirects",
3384 .data = &ipv6_devconf.accept_redirects,
3385 .maxlen = sizeof(int),
3387 .proc_handler = &proc_dointvec,
3390 .ctl_name = NET_IPV6_AUTOCONF,
3391 .procname = "autoconf",
3392 .data = &ipv6_devconf.autoconf,
3393 .maxlen = sizeof(int),
3395 .proc_handler = &proc_dointvec,
3398 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3399 .procname = "dad_transmits",
3400 .data = &ipv6_devconf.dad_transmits,
3401 .maxlen = sizeof(int),
3403 .proc_handler = &proc_dointvec,
3406 .ctl_name = NET_IPV6_RTR_SOLICITS,
3407 .procname = "router_solicitations",
3408 .data = &ipv6_devconf.rtr_solicits,
3409 .maxlen = sizeof(int),
3411 .proc_handler = &proc_dointvec,
3414 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3415 .procname = "router_solicitation_interval",
3416 .data = &ipv6_devconf.rtr_solicit_interval,
3417 .maxlen = sizeof(int),
3419 .proc_handler = &proc_dointvec_jiffies,
3420 .strategy = &sysctl_jiffies,
3423 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3424 .procname = "router_solicitation_delay",
3425 .data = &ipv6_devconf.rtr_solicit_delay,
3426 .maxlen = sizeof(int),
3428 .proc_handler = &proc_dointvec_jiffies,
3429 .strategy = &sysctl_jiffies,
3432 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3433 .procname = "force_mld_version",
3434 .data = &ipv6_devconf.force_mld_version,
3435 .maxlen = sizeof(int),
3437 .proc_handler = &proc_dointvec,
3439 #ifdef CONFIG_IPV6_PRIVACY
3441 .ctl_name = NET_IPV6_USE_TEMPADDR,
3442 .procname = "use_tempaddr",
3443 .data = &ipv6_devconf.use_tempaddr,
3444 .maxlen = sizeof(int),
3446 .proc_handler = &proc_dointvec,
3449 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3450 .procname = "temp_valid_lft",
3451 .data = &ipv6_devconf.temp_valid_lft,
3452 .maxlen = sizeof(int),
3454 .proc_handler = &proc_dointvec,
3457 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3458 .procname = "temp_prefered_lft",
3459 .data = &ipv6_devconf.temp_prefered_lft,
3460 .maxlen = sizeof(int),
3462 .proc_handler = &proc_dointvec,
3465 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3466 .procname = "regen_max_retry",
3467 .data = &ipv6_devconf.regen_max_retry,
3468 .maxlen = sizeof(int),
3470 .proc_handler = &proc_dointvec,
3473 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3474 .procname = "max_desync_factor",
3475 .data = &ipv6_devconf.max_desync_factor,
3476 .maxlen = sizeof(int),
3478 .proc_handler = &proc_dointvec,
3482 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3483 .procname = "max_addresses",
3484 .data = &ipv6_devconf.max_addresses,
3485 .maxlen = sizeof(int),
3487 .proc_handler = &proc_dointvec,
3490 .ctl_name = 0, /* sentinel */
3495 .ctl_name = NET_PROTO_CONF_ALL,
3498 .child = addrconf_sysctl.addrconf_vars,
3501 .ctl_name = 0, /* sentinel */
3504 .addrconf_conf_dir = {
3506 .ctl_name = NET_IPV6_CONF,
3509 .child = addrconf_sysctl.addrconf_dev,
3512 .ctl_name = 0, /* sentinel */
3515 .addrconf_proto_dir = {
3517 .ctl_name = NET_IPV6,
3520 .child = addrconf_sysctl.addrconf_conf_dir,
3523 .ctl_name = 0, /* sentinel */
3526 .addrconf_root_dir = {
3528 .ctl_name = CTL_NET,
3531 .child = addrconf_sysctl.addrconf_proto_dir,
3534 .ctl_name = 0, /* sentinel */
3539 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3542 struct net_device *dev = idev ? idev->dev : NULL;
3543 struct addrconf_sysctl_table *t;
3544 char *dev_name = NULL;
3546 t = kmalloc(sizeof(*t), GFP_KERNEL);
3549 memcpy(t, &addrconf_sysctl, sizeof(*t));
3550 for (i=0; t->addrconf_vars[i].data; i++) {
3551 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3552 t->addrconf_vars[i].de = NULL;
3553 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3556 dev_name = dev->name;
3557 t->addrconf_dev[0].ctl_name = dev->ifindex;
3559 dev_name = "default";
3560 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3564 * Make a copy of dev_name, because '.procname' is regarded as const
3565 * by sysctl and we wouldn't want anyone to change it under our feet
3566 * (see SIOCSIFNAME).
3568 dev_name = kstrdup(dev_name, GFP_KERNEL);
3572 t->addrconf_dev[0].procname = dev_name;
3574 t->addrconf_dev[0].child = t->addrconf_vars;
3575 t->addrconf_dev[0].de = NULL;
3576 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3577 t->addrconf_conf_dir[0].de = NULL;
3578 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3579 t->addrconf_proto_dir[0].de = NULL;
3580 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3581 t->addrconf_root_dir[0].de = NULL;
3583 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3584 if (t->sysctl_header == NULL)
3599 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3602 struct addrconf_sysctl_table *t = p->sysctl;
3604 unregister_sysctl_table(t->sysctl_header);
3605 kfree(t->addrconf_dev[0].procname);
3617 int register_inet6addr_notifier(struct notifier_block *nb)
3619 return notifier_chain_register(&inet6addr_chain, nb);
3622 int unregister_inet6addr_notifier(struct notifier_block *nb)
3624 return notifier_chain_unregister(&inet6addr_chain,nb);
3628 * Init / cleanup code
3631 int __init addrconf_init(void)
3635 /* The addrconf netdev notifier requires that loopback_dev
3636 * has it's ipv6 private information allocated and setup
3637 * before it can bring up and give link-local addresses
3638 * to other devices which are up.
3640 * Unfortunately, loopback_dev is not necessarily the first
3641 * entry in the global dev_base list of net devices. In fact,
3642 * it is likely to be the very last entry on that list.
3643 * So this causes the notifier registry below to try and
3644 * give link-local addresses to all devices besides loopback_dev
3645 * first, then loopback_dev, which cases all the non-loopback_dev
3646 * devices to fail to get a link-local address.
3648 * So, as a temporary fix, allocate the ipv6 structure for
3649 * loopback_dev first by hand.
3650 * Longer term, all of the dependencies ipv6 has upon the loopback
3651 * device and it being up should be removed.
3654 if (!ipv6_add_dev(&loopback_dev))
3660 ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
3662 register_netdevice_notifier(&ipv6_dev_notf);
3664 #ifdef CONFIG_IPV6_PRIVACY
3665 md5_tfm = crypto_alloc_tfm("md5", 0);
3666 if (unlikely(md5_tfm == NULL))
3668 "failed to load transform for md5\n");
3672 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3673 #ifdef CONFIG_SYSCTL
3674 addrconf_sysctl.sysctl_header =
3675 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3676 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3682 void __exit addrconf_cleanup(void)
3684 struct net_device *dev;
3685 struct inet6_dev *idev;
3686 struct inet6_ifaddr *ifa;
3689 unregister_netdevice_notifier(&ipv6_dev_notf);
3691 rtnetlink_links[PF_INET6] = NULL;
3692 #ifdef CONFIG_SYSCTL
3693 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3694 addrconf_sysctl_unregister(&ipv6_devconf);
3703 for (dev=dev_base; dev; dev=dev->next) {
3704 if ((idev = __in6_dev_get(dev)) == NULL)
3706 addrconf_ifdown(dev, 1);
3708 addrconf_ifdown(&loopback_dev, 2);
3714 write_lock_bh(&addrconf_hash_lock);
3715 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3716 for (ifa=inet6_addr_lst[i]; ifa; ) {
3717 struct inet6_ifaddr *bifa;
3720 ifa = ifa->lst_next;
3721 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3722 /* Do not free it; something is wrong.
3723 Now we can investigate it with debugger.
3727 write_unlock_bh(&addrconf_hash_lock);
3729 del_timer(&addr_chk_timer);
3733 #ifdef CONFIG_IPV6_PRIVACY
3734 crypto_free_tfm(md5_tfm);
3738 #ifdef CONFIG_PROC_FS
3739 proc_net_remove("if_inet6");