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
40 #include <linux/config.h>
41 #include <linux/errno.h>
42 #include <linux/types.h>
43 #include <linux/socket.h>
44 #include <linux/sockios.h>
45 #include <linux/sched.h>
46 #include <linux/net.h>
47 #include <linux/in6.h>
48 #include <linux/netdevice.h>
49 #include <linux/if_arp.h>
50 #include <linux/if_arcnet.h>
51 #include <linux/route.h>
52 #include <linux/inetdevice.h>
53 #include <linux/init.h>
55 #include <linux/sysctl.h>
57 #include <linux/delay.h>
58 #include <linux/notifier.h>
64 #include <net/protocol.h>
65 #include <net/ndisc.h>
66 #include <net/ip6_route.h>
67 #include <net/addrconf.h>
70 #include <linux/if_tunnel.h>
71 #include <linux/rtnetlink.h>
73 #ifdef CONFIG_IPV6_PRIVACY
74 #include <linux/random.h>
75 #include <linux/crypto.h>
76 #include <asm/scatterlist.h>
79 #include <asm/uaccess.h>
81 #include <linux/proc_fs.h>
82 #include <linux/seq_file.h>
84 /* Set to 3 to get tracing... */
88 #define ADBG(x) printk x
93 #define INFINITY_LIFE_TIME 0xFFFFFFFF
94 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
97 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
98 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
104 #ifdef CONFIG_IPV6_PRIVACY
105 static int __ipv6_regen_rndid(struct inet6_dev *idev);
106 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
107 static void ipv6_regen_rndid(unsigned long data);
109 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
110 static struct crypto_tfm *md5_tfm;
111 static spinlock_t md5_tfm_lock = SPIN_LOCK_UNLOCKED;
114 static int ipv6_count_addresses(struct inet6_dev *idev);
117 * Configured unicast address hash table
119 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
120 static rwlock_t addrconf_hash_lock = RW_LOCK_UNLOCKED;
122 /* Protects inet6 devices */
123 rwlock_t addrconf_lock = RW_LOCK_UNLOCKED;
125 static void addrconf_verify(unsigned long);
127 static struct timer_list addr_chk_timer =
128 TIMER_INITIALIZER(addrconf_verify, 0, 0);
129 static spinlock_t addrconf_verify_lock = SPIN_LOCK_UNLOCKED;
131 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
132 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
134 static int addrconf_ifdown(struct net_device *dev, int how);
136 static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags);
137 static void addrconf_dad_timer(unsigned long data);
138 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
139 static void addrconf_rs_timer(unsigned long data);
140 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
141 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
143 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
144 struct prefix_info *pinfo);
145 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
147 static struct notifier_block *inet6addr_chain;
149 struct ipv6_devconf ipv6_devconf = {
151 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
152 .mtu6 = IPV6_MIN_MTU,
154 .accept_redirects = 1,
156 .force_mld_version = 0,
158 .rtr_solicits = MAX_RTR_SOLICITATIONS,
159 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
160 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
161 #ifdef CONFIG_IPV6_PRIVACY
163 .temp_valid_lft = TEMP_VALID_LIFETIME,
164 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
165 .regen_max_retry = REGEN_MAX_RETRY,
166 .max_desync_factor = MAX_DESYNC_FACTOR,
168 .max_addresses = IPV6_MAX_ADDRESSES,
171 static struct ipv6_devconf ipv6_devconf_dflt = {
173 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
174 .mtu6 = IPV6_MIN_MTU,
176 .accept_redirects = 1,
179 .rtr_solicits = MAX_RTR_SOLICITATIONS,
180 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
181 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
182 #ifdef CONFIG_IPV6_PRIVACY
184 .temp_valid_lft = TEMP_VALID_LIFETIME,
185 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
186 .regen_max_retry = REGEN_MAX_RETRY,
187 .max_desync_factor = MAX_DESYNC_FACTOR,
189 .max_addresses = IPV6_MAX_ADDRESSES,
192 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
193 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
194 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
196 int ipv6_addr_type(const struct in6_addr *addr)
201 st = addr->s6_addr32[0];
203 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
204 type = IPV6_ADDR_MULTICAST;
206 switch((st & htonl(0x00FF0000))) {
207 case __constant_htonl(0x00010000):
208 type |= IPV6_ADDR_LOOPBACK;
211 case __constant_htonl(0x00020000):
212 type |= IPV6_ADDR_LINKLOCAL;
215 case __constant_htonl(0x00050000):
216 type |= IPV6_ADDR_SITELOCAL;
222 type = IPV6_ADDR_UNICAST;
224 /* Consider all addresses with the first three bits different of
225 000 and 111 as finished.
227 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
228 (st & htonl(0xE0000000)) != htonl(0xE0000000))
231 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
232 return (IPV6_ADDR_LINKLOCAL | type);
234 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
235 return (IPV6_ADDR_SITELOCAL | type);
237 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
238 if (addr->s6_addr32[2] == 0) {
239 if (addr->s6_addr32[3] == 0)
240 return IPV6_ADDR_ANY;
242 if (addr->s6_addr32[3] == htonl(0x00000001))
243 return (IPV6_ADDR_LOOPBACK | type);
245 return (IPV6_ADDR_COMPATv4 | type);
248 if (addr->s6_addr32[2] == htonl(0x0000ffff))
249 return IPV6_ADDR_MAPPED;
252 st &= htonl(0xFF000000);
254 return IPV6_ADDR_RESERVED;
255 st &= htonl(0xFE000000);
256 if (st == htonl(0x02000000))
257 return IPV6_ADDR_RESERVED; /* for NSAP */
258 if (st == htonl(0x04000000))
259 return IPV6_ADDR_RESERVED; /* for IPX */
263 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
265 if (del_timer(&ifp->timer))
269 enum addrconf_timer_t
276 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
277 enum addrconf_timer_t what,
280 if (!del_timer(&ifp->timer))
285 ifp->timer.function = addrconf_dad_timer;
288 ifp->timer.function = addrconf_rs_timer;
292 ifp->timer.expires = jiffies + when;
293 add_timer(&ifp->timer);
296 /* Nobody refers to this device, we may destroy it. */
298 void in6_dev_finish_destroy(struct inet6_dev *idev)
300 struct net_device *dev = idev->dev;
301 BUG_TRAP(idev->addr_list==NULL);
302 BUG_TRAP(idev->mc_list==NULL);
303 #ifdef NET_REFCNT_DEBUG
304 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
308 printk("Freeing alive inet6 device %p\n", idev);
311 snmp6_unregister_dev(idev);
316 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
318 struct inet6_dev *ndev;
322 if (dev->mtu < IPV6_MIN_MTU)
325 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
328 memset(ndev, 0, sizeof(struct inet6_dev));
330 rwlock_init(&ndev->lock);
332 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
333 ndev->cnf.mtu6 = dev->mtu;
334 ndev->cnf.sysctl = NULL;
335 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
336 if (ndev->nd_parms == NULL) {
341 /* We refer to the device */
344 if (snmp6_register_dev(ndev) < 0) {
346 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
347 __FUNCTION__, dev->name));
348 neigh_parms_release(&nd_tbl, ndev->nd_parms);
350 in6_dev_finish_destroy(ndev);
354 /* One reference from device. We must do this before
355 * we invoke __ipv6_regen_rndid().
359 #ifdef CONFIG_IPV6_PRIVACY
360 get_random_bytes(ndev->rndid, sizeof(ndev->rndid));
361 get_random_bytes(ndev->entropy, sizeof(ndev->entropy));
362 init_timer(&ndev->regen_timer);
363 ndev->regen_timer.function = ipv6_regen_rndid;
364 ndev->regen_timer.data = (unsigned long) ndev;
365 if ((dev->flags&IFF_LOOPBACK) ||
366 dev->type == ARPHRD_TUNNEL ||
367 dev->type == ARPHRD_SIT) {
369 "Disabled Privacy Extensions on device %p(%s)\n",
371 ndev->cnf.use_tempaddr = -1;
374 ipv6_regen_rndid((unsigned long) ndev);
378 write_lock_bh(&addrconf_lock);
380 write_unlock_bh(&addrconf_lock);
382 ipv6_mc_init_dev(ndev);
383 ndev->tstamp = jiffies;
385 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
386 NET_IPV6_NEIGH, "ipv6", &ndisc_ifinfo_sysctl_change);
387 addrconf_sysctl_register(ndev, &ndev->cnf);
393 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
395 struct inet6_dev *idev;
399 if ((idev = __in6_dev_get(dev)) == NULL) {
400 if ((idev = ipv6_add_dev(dev)) == NULL)
403 if (dev->flags&IFF_UP)
409 static void dev_forward_change(struct inet6_dev *idev)
411 struct net_device *dev;
412 struct inet6_ifaddr *ifa;
413 struct in6_addr addr;
418 if (dev && (dev->flags & IFF_MULTICAST)) {
419 ipv6_addr_all_routers(&addr);
421 if (idev->cnf.forwarding)
422 ipv6_dev_mc_inc(dev, &addr);
424 ipv6_dev_mc_dec(dev, &addr);
426 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
427 if (idev->cnf.forwarding)
428 addrconf_join_anycast(ifa);
430 addrconf_leave_anycast(ifa);
435 static void addrconf_forward_change(void)
437 struct net_device *dev;
438 struct inet6_dev *idev;
440 read_lock(&dev_base_lock);
441 for (dev=dev_base; dev; dev=dev->next) {
442 read_lock(&addrconf_lock);
443 idev = __in6_dev_get(dev);
445 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
446 idev->cnf.forwarding = ipv6_devconf.forwarding;
448 dev_forward_change(idev);
450 read_unlock(&addrconf_lock);
452 read_unlock(&dev_base_lock);
456 /* Nobody refers to this ifaddr, destroy it */
458 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
460 BUG_TRAP(ifp->if_next==NULL);
461 BUG_TRAP(ifp->lst_next==NULL);
462 #ifdef NET_REFCNT_DEBUG
463 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
466 in6_dev_put(ifp->idev);
468 if (del_timer(&ifp->timer))
469 printk("Timer is still running, when freeing ifa=%p\n", ifp);
472 printk("Freeing alive inet6 address %p\n", ifp);
475 dst_release(&ifp->rt->u.dst);
481 /* On success it returns ifp with increased reference count */
483 static struct inet6_ifaddr *
484 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
485 int scope, unsigned flags)
487 struct inet6_ifaddr *ifa = NULL;
492 read_lock_bh(&addrconf_lock);
494 err = -ENODEV; /*XXX*/
498 write_lock(&addrconf_hash_lock);
500 /* Ignore adding duplicate addresses on an interface */
501 if (ipv6_chk_same_addr(addr, idev->dev)) {
502 ADBG(("ipv6_add_addr: already assigned\n"));
507 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
510 ADBG(("ipv6_add_addr: malloc failed\n"));
515 rt = addrconf_dst_alloc(idev, addr, 0);
521 memset(ifa, 0, sizeof(struct inet6_ifaddr));
522 ipv6_addr_copy(&ifa->addr, addr);
524 spin_lock_init(&ifa->lock);
525 init_timer(&ifa->timer);
526 ifa->timer.data = (unsigned long) ifa;
528 ifa->prefix_len = pfxlen;
529 ifa->flags = flags | IFA_F_TENTATIVE;
530 ifa->cstamp = ifa->tstamp = jiffies;
538 /* Add to big hash table */
539 hash = ipv6_addr_hash(addr);
541 ifa->lst_next = inet6_addr_lst[hash];
542 inet6_addr_lst[hash] = ifa;
544 write_unlock(&addrconf_hash_lock);
546 write_lock(&idev->lock);
547 /* Add to inet6_dev unicast addr list. */
548 ifa->if_next = idev->addr_list;
549 idev->addr_list = ifa;
551 #ifdef CONFIG_IPV6_PRIVACY
552 if (ifa->flags&IFA_F_TEMPORARY) {
553 ifa->tmp_next = idev->tempaddr_list;
554 idev->tempaddr_list = ifa;
562 write_unlock(&idev->lock);
564 read_unlock_bh(&addrconf_lock);
566 if (unlikely(err == 0))
567 notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
575 write_unlock(&addrconf_hash_lock);
579 /* This function wants to get referenced ifp and releases it before return */
581 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
583 struct inet6_ifaddr *ifa, **ifap;
584 struct inet6_dev *idev = ifp->idev;
587 hash = ipv6_addr_hash(&ifp->addr);
591 write_lock_bh(&addrconf_hash_lock);
592 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
593 ifap = &ifa->lst_next) {
595 *ifap = ifa->lst_next;
597 ifa->lst_next = NULL;
601 write_unlock_bh(&addrconf_hash_lock);
603 write_lock_bh(&idev->lock);
604 #ifdef CONFIG_IPV6_PRIVACY
605 if (ifp->flags&IFA_F_TEMPORARY) {
606 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
607 ifap = &ifa->tmp_next) {
609 *ifap = ifa->tmp_next;
611 in6_ifa_put(ifp->ifpub);
615 ifa->tmp_next = NULL;
622 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;
623 ifap = &ifa->if_next) {
625 *ifap = ifa->if_next;
631 write_unlock_bh(&idev->lock);
633 ipv6_ifa_notify(RTM_DELADDR, ifp);
635 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
637 addrconf_del_timer(ifp);
642 #ifdef CONFIG_IPV6_PRIVACY
643 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
645 struct inet6_dev *idev = ifp->idev;
646 struct in6_addr addr, *tmpaddr;
647 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
652 write_lock(&idev->lock);
654 spin_lock_bh(&ift->lock);
655 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
656 spin_unlock_bh(&ift->lock);
663 if (idev->cnf.use_tempaddr <= 0) {
664 write_unlock(&idev->lock);
666 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
671 spin_lock_bh(&ifp->lock);
672 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
673 idev->cnf.use_tempaddr = -1; /*XXX*/
674 spin_unlock_bh(&ifp->lock);
675 write_unlock(&idev->lock);
677 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
683 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
684 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
685 spin_unlock_bh(&ifp->lock);
686 write_unlock(&idev->lock);
688 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
694 memcpy(&addr.s6_addr[8], idev->rndid, 8);
695 tmp_valid_lft = min_t(__u32,
697 idev->cnf.temp_valid_lft);
698 tmp_prefered_lft = min_t(__u32,
700 idev->cnf.temp_prefered_lft - desync_factor / HZ);
701 tmp_plen = ifp->prefix_len;
702 max_addresses = idev->cnf.max_addresses;
703 tmp_cstamp = ifp->cstamp;
704 tmp_tstamp = ifp->tstamp;
705 spin_unlock_bh(&ifp->lock);
707 write_unlock(&idev->lock);
708 ift = !max_addresses ||
709 ipv6_count_addresses(idev) < max_addresses ?
710 ipv6_add_addr(idev, &addr, tmp_plen,
711 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
712 if (!ift || IS_ERR(ift)) {
716 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
718 write_lock(&idev->lock);
722 spin_lock_bh(&ift->lock);
724 ift->valid_lft = tmp_valid_lft;
725 ift->prefered_lft = tmp_prefered_lft;
726 ift->cstamp = tmp_cstamp;
727 ift->tstamp = tmp_tstamp;
728 spin_unlock_bh(&ift->lock);
730 addrconf_dad_start(ift, 0);
739 * Choose an appropriate source address
741 * i) get an address with an appropriate scope
742 * ii) see if there is a specific route for the destination and use
743 * an address of the attached interface
744 * iii) don't use deprecated addresses
746 static int inline ipv6_saddr_pref(const struct inet6_ifaddr *ifp, u8 invpref)
749 pref = ifp->flags&IFA_F_DEPRECATED ? 0 : 2;
750 #ifdef CONFIG_IPV6_PRIVACY
751 pref |= (ifp->flags^invpref)&IFA_F_TEMPORARY ? 0 : 1;
756 #ifdef CONFIG_IPV6_PRIVACY
757 #define IPV6_GET_SADDR_MAXSCORE(score) ((score) == 3)
759 #define IPV6_GET_SADDR_MAXSCORE(score) (score)
762 int ipv6_dev_get_saddr(struct net_device *dev,
763 struct in6_addr *daddr, struct in6_addr *saddr)
765 struct inet6_ifaddr *ifp = NULL;
766 struct inet6_ifaddr *match = NULL;
767 struct inet6_dev *idev;
770 int hiscore = -1, score;
772 scope = ipv6_addr_scope(daddr);
776 * search dev and walk through dev addresses
780 if (dev->flags & IFF_LOOPBACK)
783 read_lock(&addrconf_lock);
784 idev = __in6_dev_get(dev);
786 read_lock_bh(&idev->lock);
787 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
788 if (ifp->scope == scope) {
789 if (ifp->flags&IFA_F_TENTATIVE)
791 #ifdef CONFIG_IPV6_PRIVACY
792 score = ipv6_saddr_pref(ifp, idev->cnf.use_tempaddr > 1 ? IFA_F_TEMPORARY : 0);
794 score = ipv6_saddr_pref(ifp, 0);
796 if (score <= hiscore)
805 if (IPV6_GET_SADDR_MAXSCORE(score)) {
806 read_unlock_bh(&idev->lock);
807 read_unlock(&addrconf_lock);
812 read_unlock_bh(&idev->lock);
814 read_unlock(&addrconf_lock);
817 if (scope == IFA_LINK)
821 * dev == NULL or search failed for specified dev
824 read_lock(&dev_base_lock);
825 read_lock(&addrconf_lock);
826 for (dev = dev_base; dev; dev=dev->next) {
827 idev = __in6_dev_get(dev);
829 read_lock_bh(&idev->lock);
830 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
831 if (ifp->scope == scope) {
832 if (ifp->flags&IFA_F_TENTATIVE)
834 #ifdef CONFIG_IPV6_PRIVACY
835 score = ipv6_saddr_pref(ifp, idev->cnf.use_tempaddr > 1 ? IFA_F_TEMPORARY : 0);
837 score = ipv6_saddr_pref(ifp, 0);
839 if (score <= hiscore)
848 if (IPV6_GET_SADDR_MAXSCORE(score)) {
849 read_unlock_bh(&idev->lock);
850 goto out_unlock_base;
854 read_unlock_bh(&idev->lock);
859 read_unlock(&addrconf_lock);
860 read_unlock(&dev_base_lock);
863 err = -EADDRNOTAVAIL;
865 ipv6_addr_copy(saddr, &match->addr);
874 int ipv6_get_saddr(struct dst_entry *dst,
875 struct in6_addr *daddr, struct in6_addr *saddr)
877 return ipv6_dev_get_saddr(dst ? dst->dev : NULL, daddr, saddr);
881 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
883 struct inet6_dev *idev;
884 int err = -EADDRNOTAVAIL;
886 read_lock(&addrconf_lock);
887 if ((idev = __in6_dev_get(dev)) != NULL) {
888 struct inet6_ifaddr *ifp;
890 read_lock_bh(&idev->lock);
891 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
892 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
893 ipv6_addr_copy(addr, &ifp->addr);
898 read_unlock_bh(&idev->lock);
900 read_unlock(&addrconf_lock);
904 static int ipv6_count_addresses(struct inet6_dev *idev)
907 struct inet6_ifaddr *ifp;
909 read_lock_bh(&idev->lock);
910 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
912 read_unlock_bh(&idev->lock);
916 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
918 struct inet6_ifaddr * ifp;
919 u8 hash = ipv6_addr_hash(addr);
921 read_lock_bh(&addrconf_hash_lock);
922 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
923 if (ipv6_addr_equal(&ifp->addr, addr) &&
924 !(ifp->flags&IFA_F_TENTATIVE)) {
925 if (dev == NULL || ifp->idev->dev == dev ||
926 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
930 read_unlock_bh(&addrconf_hash_lock);
935 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
937 struct inet6_ifaddr * ifp;
938 u8 hash = ipv6_addr_hash(addr);
940 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
941 if (ipv6_addr_equal(&ifp->addr, addr)) {
942 if (dev == NULL || ifp->idev->dev == dev)
949 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
951 struct inet6_ifaddr * ifp;
952 u8 hash = ipv6_addr_hash(addr);
954 read_lock_bh(&addrconf_hash_lock);
955 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
956 if (ipv6_addr_equal(&ifp->addr, addr)) {
957 if (dev == NULL || ifp->idev->dev == dev ||
958 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
964 read_unlock_bh(&addrconf_hash_lock);
969 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
971 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
972 const struct in6_addr *sk2_rcv_saddr6 = tcp_v6_rcv_saddr(sk2);
973 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
974 u32 sk2_rcv_saddr = tcp_v4_rcv_saddr(sk2);
975 int sk_ipv6only = ipv6_only_sock(sk);
976 int sk2_ipv6only = tcp_v6_ipv6only(sk2);
977 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
978 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
980 if (!sk2_rcv_saddr && !sk_ipv6only)
983 if (addr_type2 == IPV6_ADDR_ANY &&
984 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
987 if (addr_type == IPV6_ADDR_ANY &&
988 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
991 if (sk2_rcv_saddr6 &&
992 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
995 if (addr_type == IPV6_ADDR_MAPPED &&
997 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1003 /* Gets referenced address, destroys ifaddr */
1005 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1007 if (net_ratelimit())
1008 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1009 if (ifp->flags&IFA_F_PERMANENT) {
1010 spin_lock_bh(&ifp->lock);
1011 addrconf_del_timer(ifp);
1012 ifp->flags |= IFA_F_TENTATIVE;
1013 spin_unlock_bh(&ifp->lock);
1015 #ifdef CONFIG_IPV6_PRIVACY
1016 } else if (ifp->flags&IFA_F_TEMPORARY) {
1017 struct inet6_ifaddr *ifpub;
1018 spin_lock_bh(&ifp->lock);
1021 in6_ifa_hold(ifpub);
1022 spin_unlock_bh(&ifp->lock);
1023 ipv6_create_tempaddr(ifpub, ifp);
1026 spin_unlock_bh(&ifp->lock);
1035 /* Join to solicited addr multicast group. */
1037 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1039 struct in6_addr maddr;
1041 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1044 addrconf_addr_solict_mult(addr, &maddr);
1045 ipv6_dev_mc_inc(dev, &maddr);
1048 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1050 struct in6_addr maddr;
1052 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1055 addrconf_addr_solict_mult(addr, &maddr);
1056 __ipv6_dev_mc_dec(idev, &maddr);
1059 void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1061 struct in6_addr addr;
1062 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1063 if (ipv6_addr_any(&addr))
1065 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1068 void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1070 struct in6_addr addr;
1071 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1072 if (ipv6_addr_any(&addr))
1074 __ipv6_dev_ac_dec(ifp->idev, &addr);
1077 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1079 switch (dev->type) {
1082 case ARPHRD_IEEE802_TR:
1083 if (dev->addr_len != ETH_ALEN)
1085 memcpy(eui, dev->dev_addr, 3);
1086 memcpy(eui + 5, dev->dev_addr+3, 3);
1092 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1093 if (dev->addr_len != ARCNET_ALEN)
1096 eui[7] = *(u8*)dev->dev_addr;
1102 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1105 struct inet6_ifaddr *ifp;
1107 read_lock_bh(&idev->lock);
1108 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1109 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1110 memcpy(eui, ifp->addr.s6_addr+8, 8);
1115 read_unlock_bh(&idev->lock);
1119 #ifdef CONFIG_IPV6_PRIVACY
1120 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1121 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1123 struct net_device *dev;
1124 struct scatterlist sg[2];
1126 sg[0].page = virt_to_page(idev->entropy);
1127 sg[0].offset = offset_in_page(idev->entropy);
1129 sg[1].page = virt_to_page(idev->work_eui64);
1130 sg[1].offset = offset_in_page(idev->work_eui64);
1135 if (ipv6_generate_eui64(idev->work_eui64, dev)) {
1137 "__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
1139 get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
1142 spin_lock(&md5_tfm_lock);
1143 if (unlikely(md5_tfm == NULL)) {
1144 spin_unlock(&md5_tfm_lock);
1147 crypto_digest_init(md5_tfm);
1148 crypto_digest_update(md5_tfm, sg, 2);
1149 crypto_digest_final(md5_tfm, idev->work_digest);
1150 spin_unlock(&md5_tfm_lock);
1152 memcpy(idev->rndid, &idev->work_digest[0], 8);
1153 idev->rndid[0] &= ~0x02;
1154 memcpy(idev->entropy, &idev->work_digest[8], 8);
1157 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1158 * check if generated address is not inappropriate
1160 * - Reserved subnet anycast (RFC 2526)
1161 * 11111101 11....11 1xxxxxxx
1162 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1163 * 00-00-5E-FE-xx-xx-xx-xx
1165 * - XXX: already assigned to an address on the device
1167 if (idev->rndid[0] == 0xfd &&
1168 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1169 (idev->rndid[7]&0x80))
1171 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1172 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1174 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1181 static void ipv6_regen_rndid(unsigned long data)
1183 struct inet6_dev *idev = (struct inet6_dev *) data;
1184 unsigned long expires;
1186 read_lock_bh(&addrconf_lock);
1187 write_lock_bh(&idev->lock);
1192 if (__ipv6_regen_rndid(idev) < 0)
1196 idev->cnf.temp_prefered_lft * HZ -
1197 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1198 if (time_before(expires, jiffies)) {
1200 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1205 if (!mod_timer(&idev->regen_timer, expires))
1209 write_unlock_bh(&idev->lock);
1210 read_unlock_bh(&addrconf_lock);
1214 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1217 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1218 ret = __ipv6_regen_rndid(idev);
1228 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1229 unsigned long expires, unsigned flags)
1231 struct in6_rtmsg rtmsg;
1233 memset(&rtmsg, 0, sizeof(rtmsg));
1234 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1235 rtmsg.rtmsg_dst_len = plen;
1236 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1237 rtmsg.rtmsg_ifindex = dev->ifindex;
1238 rtmsg.rtmsg_info = expires;
1239 rtmsg.rtmsg_flags = RTF_UP|flags;
1240 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1242 /* Prevent useless cloning on PtP SIT.
1243 This thing is done here expecting that the whole
1244 class of non-broadcast devices need not cloning.
1246 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1247 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1249 ip6_route_add(&rtmsg, NULL, NULL);
1252 /* Create "default" multicast route to the interface */
1254 static void addrconf_add_mroute(struct net_device *dev)
1256 struct in6_rtmsg rtmsg;
1258 memset(&rtmsg, 0, sizeof(rtmsg));
1259 ipv6_addr_set(&rtmsg.rtmsg_dst,
1260 htonl(0xFF000000), 0, 0, 0);
1261 rtmsg.rtmsg_dst_len = 8;
1262 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1263 rtmsg.rtmsg_ifindex = dev->ifindex;
1264 rtmsg.rtmsg_flags = RTF_UP;
1265 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1266 ip6_route_add(&rtmsg, NULL, NULL);
1269 static void sit_route_add(struct net_device *dev)
1271 struct in6_rtmsg rtmsg;
1273 memset(&rtmsg, 0, sizeof(rtmsg));
1275 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1276 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1278 /* prefix length - 96 bits "::d.d.d.d" */
1279 rtmsg.rtmsg_dst_len = 96;
1280 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1281 rtmsg.rtmsg_ifindex = dev->ifindex;
1283 ip6_route_add(&rtmsg, NULL, NULL);
1286 static void addrconf_add_lroute(struct net_device *dev)
1288 struct in6_addr addr;
1290 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1291 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1294 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1296 struct inet6_dev *idev;
1300 if ((idev = ipv6_find_idev(dev)) == NULL)
1303 /* Add default multicast route */
1304 addrconf_add_mroute(dev);
1306 /* Add link local route */
1307 addrconf_add_lroute(dev);
1311 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1313 struct prefix_info *pinfo;
1317 unsigned long rt_expires;
1318 struct inet6_dev *in6_dev;
1320 pinfo = (struct prefix_info *) opt;
1322 if (len < sizeof(struct prefix_info)) {
1323 ADBG(("addrconf: prefix option too short\n"));
1328 * Validation checks ([ADDRCONF], page 19)
1331 addr_type = ipv6_addr_type(&pinfo->prefix);
1333 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1336 valid_lft = ntohl(pinfo->valid);
1337 prefered_lft = ntohl(pinfo->prefered);
1339 if (prefered_lft > valid_lft) {
1340 if (net_ratelimit())
1341 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1345 in6_dev = in6_dev_get(dev);
1347 if (in6_dev == NULL) {
1348 if (net_ratelimit())
1349 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1354 * Two things going on here:
1355 * 1) Add routes for on-link prefixes
1356 * 2) Configure prefixes with the auto flag set
1359 /* Avoid arithmetic overflow. Really, we could
1360 save rt_expires in seconds, likely valid_lft,
1361 but it would require division in fib gc, that it
1364 if (valid_lft >= 0x7FFFFFFF/HZ)
1367 rt_expires = jiffies + valid_lft * HZ;
1369 if (pinfo->onlink) {
1370 struct rt6_info *rt;
1371 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1373 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1374 if (rt->rt6i_flags&RTF_EXPIRES) {
1375 if (valid_lft == 0) {
1376 ip6_del_rt(rt, NULL, NULL);
1379 rt->rt6i_expires = rt_expires;
1382 } else if (valid_lft) {
1383 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1384 dev, rt_expires, RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1387 dst_release(&rt->u.dst);
1390 /* Try to figure out our local address for this prefix */
1392 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1393 struct inet6_ifaddr * ifp;
1394 struct in6_addr addr;
1395 int create = 0, update_lft = 0;
1397 if (pinfo->prefix_len == 64) {
1398 memcpy(&addr, &pinfo->prefix, 8);
1399 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1400 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1401 in6_dev_put(in6_dev);
1406 if (net_ratelimit())
1407 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1409 in6_dev_put(in6_dev);
1414 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1416 if (ifp == NULL && valid_lft) {
1417 int max_addresses = in6_dev->cnf.max_addresses;
1419 /* Do not allow to create too much of autoconfigured
1420 * addresses; this would be too easy way to crash kernel.
1422 if (!max_addresses ||
1423 ipv6_count_addresses(in6_dev) < max_addresses)
1424 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1425 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1427 if (!ifp || IS_ERR(ifp)) {
1428 in6_dev_put(in6_dev);
1432 update_lft = create = 1;
1433 ifp->cstamp = jiffies;
1434 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1440 #ifdef CONFIG_IPV6_PRIVACY
1441 struct inet6_ifaddr *ift;
1445 /* update lifetime (RFC2462 5.5.3 e) */
1446 spin_lock(&ifp->lock);
1448 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1449 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1452 if (!update_lft && stored_lft) {
1453 if (valid_lft > MIN_VALID_LIFETIME ||
1454 valid_lft > stored_lft)
1456 else if (stored_lft <= MIN_VALID_LIFETIME) {
1457 /* valid_lft <= stored_lft is always true */
1461 valid_lft = MIN_VALID_LIFETIME;
1462 if (valid_lft < prefered_lft)
1463 prefered_lft = valid_lft;
1469 ifp->valid_lft = valid_lft;
1470 ifp->prefered_lft = prefered_lft;
1473 ifp->flags &= ~IFA_F_DEPRECATED;
1474 spin_unlock(&ifp->lock);
1476 if (!(flags&IFA_F_TENTATIVE))
1477 ipv6_ifa_notify(0, ifp);
1479 spin_unlock(&ifp->lock);
1481 #ifdef CONFIG_IPV6_PRIVACY
1482 read_lock_bh(&in6_dev->lock);
1483 /* update all temporary addresses in the list */
1484 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1486 * When adjusting the lifetimes of an existing
1487 * temporary address, only lower the lifetimes.
1488 * Implementations must not increase the
1489 * lifetimes of an existing temporary address
1490 * when processing a Prefix Information Option.
1492 spin_lock(&ift->lock);
1494 if (ift->valid_lft > valid_lft &&
1495 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1496 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1497 if (ift->prefered_lft > prefered_lft &&
1498 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1499 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1500 spin_unlock(&ift->lock);
1501 if (!(flags&IFA_F_TENTATIVE))
1502 ipv6_ifa_notify(0, ift);
1505 if (create && in6_dev->cnf.use_tempaddr > 0) {
1507 * When a new public address is created as described in [ADDRCONF],
1508 * also create a new temporary address.
1510 read_unlock_bh(&in6_dev->lock);
1511 ipv6_create_tempaddr(ifp, NULL);
1513 read_unlock_bh(&in6_dev->lock);
1520 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1521 in6_dev_put(in6_dev);
1525 * Set destination address.
1526 * Special case for SIT interfaces where we create a new "virtual"
1529 int addrconf_set_dstaddr(void __user *arg)
1531 struct in6_ifreq ireq;
1532 struct net_device *dev;
1538 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1541 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1547 if (dev->type == ARPHRD_SIT) {
1550 struct ip_tunnel_parm p;
1552 err = -EADDRNOTAVAIL;
1553 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1556 memset(&p, 0, sizeof(p));
1557 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1561 p.iph.protocol = IPPROTO_IPV6;
1563 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1565 oldfs = get_fs(); set_fs(KERNEL_DS);
1566 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1571 if ((dev = __dev_get_by_name(p.name)) == NULL)
1573 err = dev_open(dev);
1583 * Manual configuration of address on an interface
1585 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1587 struct inet6_ifaddr *ifp;
1588 struct inet6_dev *idev;
1589 struct net_device *dev;
1594 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1597 if (!(dev->flags&IFF_UP))
1600 if ((idev = addrconf_add_dev(dev)) == NULL)
1603 scope = ipv6_addr_scope(pfx);
1605 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1607 addrconf_dad_start(ifp, 0);
1612 return PTR_ERR(ifp);
1615 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1617 struct inet6_ifaddr *ifp;
1618 struct inet6_dev *idev;
1619 struct net_device *dev;
1621 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1624 if ((idev = __in6_dev_get(dev)) == NULL)
1627 read_lock_bh(&idev->lock);
1628 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1629 if (ifp->prefix_len == plen &&
1630 ipv6_addr_equal(pfx, &ifp->addr)) {
1632 read_unlock_bh(&idev->lock);
1636 /* If the last address is deleted administratively,
1637 disable IPv6 on this interface.
1639 if (idev->addr_list == NULL)
1640 addrconf_ifdown(idev->dev, 1);
1644 read_unlock_bh(&idev->lock);
1645 return -EADDRNOTAVAIL;
1649 int addrconf_add_ifaddr(void __user *arg)
1651 struct in6_ifreq ireq;
1654 if (!capable(CAP_NET_ADMIN))
1657 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1661 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1666 int addrconf_del_ifaddr(void __user *arg)
1668 struct in6_ifreq ireq;
1671 if (!capable(CAP_NET_ADMIN))
1674 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1678 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1683 static void sit_add_v4_addrs(struct inet6_dev *idev)
1685 struct inet6_ifaddr * ifp;
1686 struct in6_addr addr;
1687 struct net_device *dev;
1692 memset(&addr, 0, sizeof(struct in6_addr));
1693 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1695 if (idev->dev->flags&IFF_POINTOPOINT) {
1696 addr.s6_addr32[0] = htonl(0xfe800000);
1699 scope = IPV6_ADDR_COMPATv4;
1702 if (addr.s6_addr32[3]) {
1703 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1705 spin_lock_bh(&ifp->lock);
1706 ifp->flags &= ~IFA_F_TENTATIVE;
1707 spin_unlock_bh(&ifp->lock);
1708 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1714 for (dev = dev_base; dev != NULL; dev = dev->next) {
1715 struct in_device * in_dev = __in_dev_get(dev);
1716 if (in_dev && (dev->flags & IFF_UP)) {
1717 struct in_ifaddr * ifa;
1721 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1724 addr.s6_addr32[3] = ifa->ifa_local;
1726 if (ifa->ifa_scope == RT_SCOPE_LINK)
1728 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1729 if (idev->dev->flags&IFF_POINTOPOINT)
1733 if (idev->dev->flags&IFF_POINTOPOINT)
1738 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1741 spin_lock_bh(&ifp->lock);
1742 ifp->flags &= ~IFA_F_TENTATIVE;
1743 spin_unlock_bh(&ifp->lock);
1744 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1752 static void init_loopback(struct net_device *dev)
1754 struct inet6_dev *idev;
1755 struct inet6_ifaddr * ifp;
1761 if ((idev = ipv6_find_idev(dev)) == NULL) {
1762 printk(KERN_DEBUG "init loopback: add_dev failed\n");
1766 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
1768 spin_lock_bh(&ifp->lock);
1769 ifp->flags &= ~IFA_F_TENTATIVE;
1770 spin_unlock_bh(&ifp->lock);
1771 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1776 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
1778 struct inet6_ifaddr * ifp;
1780 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
1782 addrconf_dad_start(ifp, 0);
1787 static void addrconf_dev_config(struct net_device *dev)
1789 struct in6_addr addr;
1790 struct inet6_dev * idev;
1794 if ((dev->type != ARPHRD_ETHER) &&
1795 (dev->type != ARPHRD_FDDI) &&
1796 (dev->type != ARPHRD_IEEE802_TR) &&
1797 (dev->type != ARPHRD_ARCNET)) {
1798 /* Alas, we support only Ethernet autoconfiguration. */
1802 idev = addrconf_add_dev(dev);
1806 memset(&addr, 0, sizeof(struct in6_addr));
1807 addr.s6_addr32[0] = htonl(0xFE800000);
1809 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
1810 addrconf_add_linklocal(idev, &addr);
1813 static void addrconf_sit_config(struct net_device *dev)
1815 struct inet6_dev *idev;
1820 * Configure the tunnel with one of our IPv4
1821 * addresses... we should configure all of
1822 * our v4 addrs in the tunnel
1825 if ((idev = ipv6_find_idev(dev)) == NULL) {
1826 printk(KERN_DEBUG "init sit: add_dev failed\n");
1830 sit_add_v4_addrs(idev);
1832 if (dev->flags&IFF_POINTOPOINT) {
1833 addrconf_add_mroute(dev);
1834 addrconf_add_lroute(dev);
1840 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
1842 struct in6_addr lladdr;
1844 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
1845 addrconf_add_linklocal(idev, &lladdr);
1851 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
1853 struct net_device *link_dev;
1855 /* first try to inherit the link-local address from the link device */
1856 if (idev->dev->iflink &&
1857 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
1858 if (!ipv6_inherit_linklocal(idev, link_dev))
1861 /* then try to inherit it from any device */
1862 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
1863 if (!ipv6_inherit_linklocal(idev, link_dev))
1866 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
1870 * Autoconfigure tunnel with a link-local address so routing protocols,
1871 * DHCPv6, MLD etc. can be run over the virtual link
1874 static void addrconf_ip6_tnl_config(struct net_device *dev)
1876 struct inet6_dev *idev;
1880 if ((idev = addrconf_add_dev(dev)) == NULL) {
1881 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
1884 ip6_tnl_add_linklocal(idev);
1885 addrconf_add_mroute(dev);
1888 static int addrconf_notify(struct notifier_block *this, unsigned long event,
1891 struct net_device *dev = (struct net_device *) data;
1892 struct inet6_dev *idev = __in6_dev_get(dev);
1898 addrconf_sit_config(dev);
1900 case ARPHRD_TUNNEL6:
1901 addrconf_ip6_tnl_config(dev);
1903 case ARPHRD_LOOPBACK:
1908 addrconf_dev_config(dev);
1912 /* If the MTU changed during the interface down, when the
1913 interface up, the changed MTU must be reflected in the
1914 idev as well as routers.
1916 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
1917 rt6_mtu_change(dev, dev->mtu);
1918 idev->cnf.mtu6 = dev->mtu;
1920 idev->tstamp = jiffies;
1921 inet6_ifinfo_notify(RTM_NEWLINK, idev);
1922 /* If the changed mtu during down is lower than IPV6_MIN_MTU
1923 stop IPv6 on this interface.
1925 if (dev->mtu < IPV6_MIN_MTU)
1926 addrconf_ifdown(dev, event != NETDEV_DOWN);
1930 case NETDEV_CHANGEMTU:
1931 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
1932 rt6_mtu_change(dev, dev->mtu);
1933 idev->cnf.mtu6 = dev->mtu;
1937 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
1940 case NETDEV_UNREGISTER:
1942 * Remove all addresses from this interface.
1944 addrconf_ifdown(dev, event != NETDEV_DOWN);
1948 case NETDEV_CHANGENAME:
1949 #ifdef CONFIG_SYSCTL
1951 addrconf_sysctl_unregister(&idev->cnf);
1952 neigh_sysctl_unregister(idev->nd_parms);
1953 neigh_sysctl_register(dev, idev->nd_parms, NET_IPV6, NET_IPV6_NEIGH, "ipv6", &ndisc_ifinfo_sysctl_change);
1954 addrconf_sysctl_register(idev, &idev->cnf);
1964 * addrconf module should be notified of a device going up
1966 static struct notifier_block ipv6_dev_notf = {
1967 .notifier_call = addrconf_notify,
1971 static int addrconf_ifdown(struct net_device *dev, int how)
1973 struct inet6_dev *idev;
1974 struct inet6_ifaddr *ifa, **bifa;
1980 neigh_ifdown(&nd_tbl, dev);
1982 idev = __in6_dev_get(dev);
1986 /* Step 1: remove reference to ipv6 device from parent device.
1990 write_lock_bh(&addrconf_lock);
1991 dev->ip6_ptr = NULL;
1993 write_unlock_bh(&addrconf_lock);
1996 /* Step 2: clear hash table */
1997 for (i=0; i<IN6_ADDR_HSIZE; i++) {
1998 bifa = &inet6_addr_lst[i];
2000 write_lock_bh(&addrconf_hash_lock);
2001 while ((ifa = *bifa) != NULL) {
2002 if (ifa->idev == idev) {
2003 *bifa = ifa->lst_next;
2004 ifa->lst_next = NULL;
2005 addrconf_del_timer(ifa);
2009 bifa = &ifa->lst_next;
2011 write_unlock_bh(&addrconf_hash_lock);
2014 write_lock_bh(&idev->lock);
2016 /* Step 3: clear flags for stateless addrconf */
2018 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD);
2020 /* Step 4: clear address list */
2021 #ifdef CONFIG_IPV6_PRIVACY
2022 if (how == 1 && del_timer(&idev->regen_timer))
2025 /* clear tempaddr list */
2026 while ((ifa = idev->tempaddr_list) != NULL) {
2027 idev->tempaddr_list = ifa->tmp_next;
2028 ifa->tmp_next = NULL;
2030 write_unlock_bh(&idev->lock);
2031 spin_lock_bh(&ifa->lock);
2034 in6_ifa_put(ifa->ifpub);
2037 spin_unlock_bh(&ifa->lock);
2039 write_lock_bh(&idev->lock);
2042 while ((ifa = idev->addr_list) != NULL) {
2043 idev->addr_list = ifa->if_next;
2044 ifa->if_next = NULL;
2046 addrconf_del_timer(ifa);
2047 write_unlock_bh(&idev->lock);
2049 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2052 write_lock_bh(&idev->lock);
2054 write_unlock_bh(&idev->lock);
2056 /* Step 5: Discard multicast list */
2059 ipv6_mc_destroy_dev(idev);
2063 /* Step 5: netlink notification of this interface */
2064 idev->tstamp = jiffies;
2065 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2067 /* Shot the device (if unregistered) */
2070 #ifdef CONFIG_SYSCTL
2071 addrconf_sysctl_unregister(&idev->cnf);
2072 neigh_sysctl_unregister(idev->nd_parms);
2074 neigh_parms_release(&nd_tbl, idev->nd_parms);
2075 neigh_ifdown(&nd_tbl, dev);
2081 static void addrconf_rs_timer(unsigned long data)
2083 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2085 if (ifp->idev->cnf.forwarding)
2088 if (ifp->idev->if_flags & IF_RA_RCVD) {
2090 * Announcement received after solicitation
2096 spin_lock(&ifp->lock);
2097 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2098 struct in6_addr all_routers;
2100 /* The wait after the last probe can be shorter */
2101 addrconf_mod_timer(ifp, AC_RS,
2102 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2103 ifp->idev->cnf.rtr_solicit_delay :
2104 ifp->idev->cnf.rtr_solicit_interval);
2105 spin_unlock(&ifp->lock);
2107 ipv6_addr_all_routers(&all_routers);
2109 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2111 spin_unlock(&ifp->lock);
2113 * Note: we do not support deprecated "all on-link"
2114 * assumption any longer.
2116 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2117 ifp->idev->dev->name);
2125 * Duplicate Address Detection
2127 static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags)
2129 struct inet6_dev *idev = ifp->idev;
2130 struct net_device *dev = idev->dev;
2131 unsigned long rand_num;
2133 addrconf_join_solict(dev, &ifp->addr);
2135 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2136 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2139 net_srandom(ifp->addr.s6_addr32[3]);
2140 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2142 read_lock_bh(&idev->lock);
2145 spin_lock_bh(&ifp->lock);
2147 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2148 !(ifp->flags&IFA_F_TENTATIVE)) {
2149 ifp->flags &= ~IFA_F_TENTATIVE;
2150 spin_unlock_bh(&ifp->lock);
2151 read_unlock_bh(&idev->lock);
2153 addrconf_dad_completed(ifp);
2157 ifp->probes = idev->cnf.dad_transmits;
2158 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2160 spin_unlock_bh(&ifp->lock);
2162 read_unlock_bh(&idev->lock);
2165 static void addrconf_dad_timer(unsigned long data)
2167 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2168 struct inet6_dev *idev = ifp->idev;
2169 struct in6_addr unspec;
2170 struct in6_addr mcaddr;
2172 read_lock_bh(&idev->lock);
2174 read_unlock_bh(&idev->lock);
2177 spin_lock_bh(&ifp->lock);
2178 if (ifp->probes == 0) {
2180 * DAD was successful
2183 ifp->flags &= ~IFA_F_TENTATIVE;
2184 spin_unlock_bh(&ifp->lock);
2185 read_unlock_bh(&idev->lock);
2187 addrconf_dad_completed(ifp);
2193 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2194 spin_unlock_bh(&ifp->lock);
2195 read_unlock_bh(&idev->lock);
2197 /* send a neighbour solicitation for our addr */
2198 memset(&unspec, 0, sizeof(unspec));
2199 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2200 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2205 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2207 struct net_device * dev = ifp->idev->dev;
2210 * Configure the address for reception. Now it is valid.
2213 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2215 /* If added prefix is link local and forwarding is off,
2216 start sending router solicitations.
2219 if (ifp->idev->cnf.forwarding == 0 &&
2220 ifp->idev->cnf.rtr_solicits > 0 &&
2221 (dev->flags&IFF_LOOPBACK) == 0 &&
2222 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2223 struct in6_addr all_routers;
2225 ipv6_addr_all_routers(&all_routers);
2228 * If a host as already performed a random delay
2229 * [...] as part of DAD [...] there is no need
2230 * to delay again before sending the first RS
2232 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2234 spin_lock_bh(&ifp->lock);
2236 ifp->idev->if_flags |= IF_RS_SENT;
2237 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2238 spin_unlock_bh(&ifp->lock);
2242 #ifdef CONFIG_PROC_FS
2243 struct if6_iter_state {
2247 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2249 struct inet6_ifaddr *ifa = NULL;
2250 struct if6_iter_state *state = seq->private;
2252 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2253 ifa = inet6_addr_lst[state->bucket];
2260 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2262 struct if6_iter_state *state = seq->private;
2264 ifa = ifa->lst_next;
2266 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2267 ifa = inet6_addr_lst[state->bucket];
2273 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2275 struct inet6_ifaddr *ifa = if6_get_first(seq);
2278 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2280 return pos ? NULL : ifa;
2283 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2285 read_lock_bh(&addrconf_hash_lock);
2286 return if6_get_idx(seq, *pos);
2289 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2291 struct inet6_ifaddr *ifa;
2293 ifa = if6_get_next(seq, v);
2298 static void if6_seq_stop(struct seq_file *seq, void *v)
2300 read_unlock_bh(&addrconf_hash_lock);
2303 static int if6_seq_show(struct seq_file *seq, void *v)
2305 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2307 "%04x%04x%04x%04x%04x%04x%04x%04x %02x %02x %02x %02x %8s\n",
2309 ifp->idev->dev->ifindex,
2313 ifp->idev->dev->name);
2317 static struct seq_operations if6_seq_ops = {
2318 .start = if6_seq_start,
2319 .next = if6_seq_next,
2320 .show = if6_seq_show,
2321 .stop = if6_seq_stop,
2324 static int if6_seq_open(struct inode *inode, struct file *file)
2326 struct seq_file *seq;
2328 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2332 memset(s, 0, sizeof(*s));
2334 rc = seq_open(file, &if6_seq_ops);
2338 seq = file->private_data;
2347 static struct file_operations if6_fops = {
2348 .owner = THIS_MODULE,
2349 .open = if6_seq_open,
2351 .llseek = seq_lseek,
2352 .release = seq_release_private,
2355 int __init if6_proc_init(void)
2357 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2362 void if6_proc_exit(void)
2364 proc_net_remove("if_inet6");
2366 #endif /* CONFIG_PROC_FS */
2369 * Periodic address status verification
2372 static void addrconf_verify(unsigned long foo)
2374 struct inet6_ifaddr *ifp;
2375 unsigned long now, next;
2378 spin_lock_bh(&addrconf_verify_lock);
2380 next = now + ADDR_CHECK_FREQUENCY;
2382 del_timer(&addr_chk_timer);
2384 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2387 write_lock(&addrconf_hash_lock);
2388 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2390 #ifdef CONFIG_IPV6_PRIVACY
2391 unsigned long regen_advance;
2394 if (ifp->flags & IFA_F_PERMANENT)
2397 spin_lock(&ifp->lock);
2398 age = (now - ifp->tstamp) / HZ;
2400 #ifdef CONFIG_IPV6_PRIVACY
2401 regen_advance = ifp->idev->cnf.regen_max_retry *
2402 ifp->idev->cnf.dad_transmits *
2403 ifp->idev->nd_parms->retrans_time / HZ;
2406 if (age >= ifp->valid_lft) {
2407 spin_unlock(&ifp->lock);
2409 write_unlock(&addrconf_hash_lock);
2412 } else if (age >= ifp->prefered_lft) {
2413 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2416 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2418 ifp->flags |= IFA_F_DEPRECATED;
2421 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2422 next = ifp->tstamp + ifp->valid_lft * HZ;
2424 spin_unlock(&ifp->lock);
2428 write_unlock(&addrconf_hash_lock);
2430 ipv6_ifa_notify(0, ifp);
2434 #ifdef CONFIG_IPV6_PRIVACY
2435 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2436 !(ifp->flags&IFA_F_TENTATIVE)) {
2437 if (age >= ifp->prefered_lft - regen_advance) {
2438 struct inet6_ifaddr *ifpub = ifp->ifpub;
2439 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2440 next = ifp->tstamp + ifp->prefered_lft * HZ;
2441 if (!ifp->regen_count && ifpub) {
2444 in6_ifa_hold(ifpub);
2445 spin_unlock(&ifp->lock);
2446 write_unlock(&addrconf_hash_lock);
2447 ipv6_create_tempaddr(ifpub, ifp);
2452 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2453 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2454 spin_unlock(&ifp->lock);
2457 /* ifp->prefered_lft <= ifp->valid_lft */
2458 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2459 next = ifp->tstamp + ifp->prefered_lft * HZ;
2460 spin_unlock(&ifp->lock);
2463 write_unlock(&addrconf_hash_lock);
2466 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2467 add_timer(&addr_chk_timer);
2468 spin_unlock_bh(&addrconf_verify_lock);
2472 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2474 struct rtattr **rta = arg;
2475 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2476 struct in6_addr *pfx;
2479 if (rta[IFA_ADDRESS-1]) {
2480 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2482 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2484 if (rta[IFA_LOCAL-1]) {
2485 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2487 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2492 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2496 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2498 struct rtattr **rta = arg;
2499 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2500 struct in6_addr *pfx;
2503 if (rta[IFA_ADDRESS-1]) {
2504 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2506 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2508 if (rta[IFA_LOCAL-1]) {
2509 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2511 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2516 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2519 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2520 u32 pid, u32 seq, int event)
2522 struct ifaddrmsg *ifm;
2523 struct nlmsghdr *nlh;
2524 struct ifa_cacheinfo ci;
2525 unsigned char *b = skb->tail;
2527 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2528 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2529 ifm = NLMSG_DATA(nlh);
2530 ifm->ifa_family = AF_INET6;
2531 ifm->ifa_prefixlen = ifa->prefix_len;
2532 ifm->ifa_flags = ifa->flags;
2533 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2534 if (ifa->scope&IFA_HOST)
2535 ifm->ifa_scope = RT_SCOPE_HOST;
2536 else if (ifa->scope&IFA_LINK)
2537 ifm->ifa_scope = RT_SCOPE_LINK;
2538 else if (ifa->scope&IFA_SITE)
2539 ifm->ifa_scope = RT_SCOPE_SITE;
2540 ifm->ifa_index = ifa->idev->dev->ifindex;
2541 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2542 if (!(ifa->flags&IFA_F_PERMANENT)) {
2543 ci.ifa_prefered = ifa->prefered_lft;
2544 ci.ifa_valid = ifa->valid_lft;
2545 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2546 long tval = (jiffies - ifa->tstamp)/HZ;
2547 ci.ifa_prefered -= tval;
2548 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2549 ci.ifa_valid -= tval;
2552 ci.ifa_prefered = INFINITY_LIFE_TIME;
2553 ci.ifa_valid = INFINITY_LIFE_TIME;
2555 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2556 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2557 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2558 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2559 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2560 nlh->nlmsg_len = skb->tail - b;
2565 skb_trim(skb, b - skb->data);
2569 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2570 u32 pid, u32 seq, int event)
2572 struct ifaddrmsg *ifm;
2573 struct nlmsghdr *nlh;
2574 struct ifa_cacheinfo ci;
2575 unsigned char *b = skb->tail;
2577 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2578 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2579 ifm = NLMSG_DATA(nlh);
2580 ifm->ifa_family = AF_INET6;
2581 ifm->ifa_prefixlen = 128;
2582 ifm->ifa_flags = IFA_F_PERMANENT;
2583 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2584 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2585 ifm->ifa_scope = RT_SCOPE_SITE;
2586 ifm->ifa_index = ifmca->idev->dev->ifindex;
2587 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2588 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2589 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2591 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2592 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2594 ci.ifa_prefered = INFINITY_LIFE_TIME;
2595 ci.ifa_valid = INFINITY_LIFE_TIME;
2596 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2597 nlh->nlmsg_len = skb->tail - b;
2602 skb_trim(skb, b - skb->data);
2606 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2607 u32 pid, u32 seq, int event)
2609 struct ifaddrmsg *ifm;
2610 struct nlmsghdr *nlh;
2611 struct ifa_cacheinfo ci;
2612 unsigned char *b = skb->tail;
2614 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2615 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2616 ifm = NLMSG_DATA(nlh);
2617 ifm->ifa_family = AF_INET6;
2618 ifm->ifa_prefixlen = 128;
2619 ifm->ifa_flags = IFA_F_PERMANENT;
2620 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2621 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2622 ifm->ifa_scope = RT_SCOPE_SITE;
2623 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2624 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2625 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2626 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2628 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2629 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2631 ci.ifa_prefered = INFINITY_LIFE_TIME;
2632 ci.ifa_valid = INFINITY_LIFE_TIME;
2633 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2634 nlh->nlmsg_len = skb->tail - b;
2639 skb_trim(skb, b - skb->data);
2650 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2651 enum addr_type_t type)
2654 int s_idx, s_ip_idx;
2656 struct net_device *dev;
2657 struct inet6_dev *idev = NULL;
2658 struct inet6_ifaddr *ifa;
2659 struct ifmcaddr6 *ifmca;
2660 struct ifacaddr6 *ifaca;
2662 /* no ipv6 inside a vserver for now */
2663 if (skb->sk && skb->sk->sk_vx_info)
2666 s_idx = cb->args[0];
2667 s_ip_idx = ip_idx = cb->args[1];
2668 read_lock(&dev_base_lock);
2670 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
2676 if ((idev = in6_dev_get(dev)) == NULL)
2678 read_lock_bh(&idev->lock);
2681 /* unicast address */
2682 for (ifa = idev->addr_list; ifa;
2683 ifa = ifa->if_next, ip_idx++) {
2684 if (ip_idx < s_ip_idx)
2686 if ((err = inet6_fill_ifaddr(skb, ifa,
2687 NETLINK_CB(cb->skb).pid,
2688 cb->nlh->nlmsg_seq, RTM_NEWADDR)) <= 0)
2692 #ifdef CONFIG_IPV6_PRIVACY
2693 for (ifa = idev->tempaddr_list; ifa;
2694 ifa = ifa->tmp_next, ip_idx++) {
2695 if (ip_idx < s_ip_idx)
2697 if ((err = inet6_fill_ifaddr(skb, ifa,
2698 NETLINK_CB(cb->skb).pid,
2699 cb->nlh->nlmsg_seq, RTM_NEWADDR)) <= 0)
2704 case MULTICAST_ADDR:
2705 /* multicast address */
2706 for (ifmca = idev->mc_list; ifmca;
2707 ifmca = ifmca->next, ip_idx++) {
2708 if (ip_idx < s_ip_idx)
2710 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
2711 NETLINK_CB(cb->skb).pid,
2712 cb->nlh->nlmsg_seq, RTM_GETMULTICAST)) <= 0)
2717 /* anycast address */
2718 for (ifaca = idev->ac_list; ifaca;
2719 ifaca = ifaca->aca_next, ip_idx++) {
2720 if (ip_idx < s_ip_idx)
2722 if ((err = inet6_fill_ifacaddr(skb, ifaca,
2723 NETLINK_CB(cb->skb).pid,
2724 cb->nlh->nlmsg_seq, RTM_GETANYCAST)) <= 0)
2731 read_unlock_bh(&idev->lock);
2736 read_unlock_bh(&idev->lock);
2739 read_unlock(&dev_base_lock);
2741 cb->args[1] = ip_idx;
2745 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
2747 enum addr_type_t type = UNICAST_ADDR;
2748 return inet6_dump_addr(skb, cb, type);
2751 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
2753 enum addr_type_t type = MULTICAST_ADDR;
2754 return inet6_dump_addr(skb, cb, type);
2758 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
2760 enum addr_type_t type = ANYCAST_ADDR;
2761 return inet6_dump_addr(skb, cb, type);
2764 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
2766 struct sk_buff *skb;
2767 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
2769 skb = alloc_skb(size, GFP_ATOMIC);
2771 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, ENOBUFS);
2774 if (inet6_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
2776 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, EINVAL);
2779 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFADDR;
2780 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFADDR, GFP_ATOMIC);
2783 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
2784 __s32 *array, int bytes)
2786 memset(array, 0, bytes);
2787 array[DEVCONF_FORWARDING] = cnf->forwarding;
2788 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
2789 array[DEVCONF_MTU6] = cnf->mtu6;
2790 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
2791 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
2792 array[DEVCONF_AUTOCONF] = cnf->autoconf;
2793 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
2794 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
2795 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
2796 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
2797 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
2798 #ifdef CONFIG_IPV6_PRIVACY
2799 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
2800 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
2801 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
2802 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
2803 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
2805 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
2808 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
2809 u32 pid, u32 seq, int event)
2811 struct net_device *dev = idev->dev;
2812 __s32 *array = NULL;
2813 struct ifinfomsg *r;
2814 struct nlmsghdr *nlh;
2815 unsigned char *b = skb->tail;
2816 struct rtattr *subattr;
2817 __u32 mtu = dev->mtu;
2818 struct ifla_cacheinfo ci;
2820 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*r));
2821 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2822 r = NLMSG_DATA(nlh);
2823 r->ifi_family = AF_INET6;
2824 r->ifi_type = dev->type;
2825 r->ifi_index = dev->ifindex;
2826 r->ifi_flags = dev->flags;
2828 if (!netif_running(dev) || !netif_carrier_ok(dev))
2829 r->ifi_flags &= ~IFF_RUNNING;
2831 r->ifi_flags |= IFF_RUNNING;
2833 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
2836 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
2838 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
2839 if (dev->ifindex != dev->iflink)
2840 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
2842 subattr = (struct rtattr*)skb->tail;
2844 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
2846 /* return the device flags */
2847 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
2849 /* return interface cacheinfo */
2850 ci.max_reasm_len = IPV6_MAXPLEN;
2851 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
2852 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2853 ci.reachable_time = idev->nd_parms->reachable_time;
2854 ci.retrans_time = idev->nd_parms->retrans_time;
2855 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
2857 /* return the device sysctl params */
2858 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
2859 goto rtattr_failure;
2860 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
2861 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
2863 /* XXX - Statistics/MC not implemented */
2864 subattr->rta_len = skb->tail - (u8*)subattr;
2866 nlh->nlmsg_len = skb->tail - b;
2874 skb_trim(skb, b - skb->data);
2878 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2881 int s_idx = cb->args[0];
2882 struct net_device *dev;
2883 struct inet6_dev *idev;
2885 /* no ipv6 inside a vserver for now */
2886 if (skb->sk && skb->sk->sk_vx_info)
2889 read_lock(&dev_base_lock);
2890 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
2893 if ((idev = in6_dev_get(dev)) == NULL)
2895 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
2896 cb->nlh->nlmsg_seq, RTM_NEWLINK);
2901 read_unlock(&dev_base_lock);
2907 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
2909 struct sk_buff *skb;
2911 int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
2913 skb = alloc_skb(size, GFP_ATOMIC);
2915 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFINFO, ENOBUFS);
2918 if (inet6_fill_ifinfo(skb, idev, 0, 0, event) < 0) {
2920 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFINFO, EINVAL);
2923 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFINFO;
2924 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFINFO, GFP_ATOMIC);
2927 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
2928 struct prefix_info *pinfo, u32 pid, u32 seq, int event)
2930 struct prefixmsg *pmsg;
2931 struct nlmsghdr *nlh;
2932 unsigned char *b = skb->tail;
2933 struct prefix_cacheinfo ci;
2935 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*pmsg));
2938 nlh->nlmsg_flags |= NLM_F_MULTI;
2940 pmsg = NLMSG_DATA(nlh);
2941 pmsg->prefix_family = AF_INET6;
2942 pmsg->prefix_ifindex = idev->dev->ifindex;
2943 pmsg->prefix_len = pinfo->prefix_len;
2944 pmsg->prefix_type = pinfo->type;
2946 pmsg->prefix_flags = 0;
2948 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
2949 if (pinfo->autoconf)
2950 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
2952 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
2954 ci.preferred_time = ntohl(pinfo->prefered);
2955 ci.valid_time = ntohl(pinfo->valid);
2956 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
2958 nlh->nlmsg_len = skb->tail - b;
2963 skb_trim(skb, b - skb->data);
2967 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
2968 struct prefix_info *pinfo)
2970 struct sk_buff *skb;
2971 int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
2973 skb = alloc_skb(size, GFP_ATOMIC);
2975 netlink_set_err(rtnl, 0, RTMGRP_IPV6_PREFIX, ENOBUFS);
2978 if (inet6_fill_prefix(skb, idev, pinfo, 0, 0, event) < 0) {
2980 netlink_set_err(rtnl, 0, RTMGRP_IPV6_PREFIX, EINVAL);
2983 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_PREFIX;
2984 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_PREFIX, GFP_ATOMIC);
2987 static struct rtnetlink_link inet6_rtnetlink_table[RTM_MAX - RTM_BASE + 1] = {
2988 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
2989 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
2990 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
2991 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
2992 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
2993 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
2994 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
2995 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
2996 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
2997 .dumpit = inet6_dump_fib, },
3000 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3002 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3006 dst_hold(&ifp->rt->u.dst);
3007 if (ip6_ins_rt(ifp->rt, NULL, NULL))
3008 dst_release(&ifp->rt->u.dst);
3009 if (ifp->idev->cnf.forwarding)
3010 addrconf_join_anycast(ifp);
3013 if (ifp->idev->cnf.forwarding)
3014 addrconf_leave_anycast(ifp);
3015 addrconf_leave_solict(ifp->idev, &ifp->addr);
3016 dst_hold(&ifp->rt->u.dst);
3017 if (ip6_del_rt(ifp->rt, NULL, NULL))
3018 dst_free(&ifp->rt->u.dst);
3020 dst_release(&ifp->rt->u.dst);
3025 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3027 read_lock_bh(&addrconf_lock);
3028 if (likely(ifp->idev->dead == 0))
3029 __ipv6_ifa_notify(event, ifp);
3030 read_unlock_bh(&addrconf_lock);
3033 #ifdef CONFIG_SYSCTL
3036 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3037 void __user *buffer, size_t *lenp, loff_t *ppos)
3039 int *valp = ctl->data;
3043 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3045 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3046 if (valp != &ipv6_devconf.forwarding) {
3047 if ((!*valp) ^ (!val)) {
3048 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3051 dev_forward_change(idev);
3054 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3055 addrconf_forward_change();
3058 rt6_purge_dflt_routers();
3064 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3065 int __user *name, int nlen,
3066 void __user *oldval,
3067 size_t __user *oldlenp,
3068 void __user *newval, size_t newlen,
3071 int *valp = table->data;
3074 if (!newval || !newlen)
3076 if (newlen != sizeof(int))
3078 if (get_user(new, (int __user *)newval))
3082 if (oldval && oldlenp) {
3084 if (get_user(len, oldlenp))
3087 if (len > table->maxlen)
3088 len = table->maxlen;
3089 if (copy_to_user(oldval, valp, len))
3091 if (put_user(len, oldlenp))
3096 if (valp != &ipv6_devconf_dflt.forwarding) {
3097 if (valp != &ipv6_devconf.forwarding) {
3098 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3100 if (unlikely(idev == NULL))
3102 changed = (!*valp) ^ (!new);
3105 dev_forward_change(idev);
3108 addrconf_forward_change();
3112 rt6_purge_dflt_routers();
3119 static struct addrconf_sysctl_table
3121 struct ctl_table_header *sysctl_header;
3122 ctl_table addrconf_vars[18];
3123 ctl_table addrconf_dev[2];
3124 ctl_table addrconf_conf_dir[2];
3125 ctl_table addrconf_proto_dir[2];
3126 ctl_table addrconf_root_dir[2];
3127 } addrconf_sysctl = {
3128 .sysctl_header = NULL,
3131 .ctl_name = NET_IPV6_FORWARDING,
3132 .procname = "forwarding",
3133 .data = &ipv6_devconf.forwarding,
3134 .maxlen = sizeof(int),
3136 .proc_handler = &addrconf_sysctl_forward,
3137 .strategy = &addrconf_sysctl_forward_strategy,
3140 .ctl_name = NET_IPV6_HOP_LIMIT,
3141 .procname = "hop_limit",
3142 .data = &ipv6_devconf.hop_limit,
3143 .maxlen = sizeof(int),
3145 .proc_handler = proc_dointvec,
3148 .ctl_name = NET_IPV6_MTU,
3150 .data = &ipv6_devconf.mtu6,
3151 .maxlen = sizeof(int),
3153 .proc_handler = &proc_dointvec,
3156 .ctl_name = NET_IPV6_ACCEPT_RA,
3157 .procname = "accept_ra",
3158 .data = &ipv6_devconf.accept_ra,
3159 .maxlen = sizeof(int),
3161 .proc_handler = &proc_dointvec,
3164 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3165 .procname = "accept_redirects",
3166 .data = &ipv6_devconf.accept_redirects,
3167 .maxlen = sizeof(int),
3169 .proc_handler = &proc_dointvec,
3172 .ctl_name = NET_IPV6_AUTOCONF,
3173 .procname = "autoconf",
3174 .data = &ipv6_devconf.autoconf,
3175 .maxlen = sizeof(int),
3177 .proc_handler = &proc_dointvec,
3180 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3181 .procname = "dad_transmits",
3182 .data = &ipv6_devconf.dad_transmits,
3183 .maxlen = sizeof(int),
3185 .proc_handler = &proc_dointvec,
3188 .ctl_name = NET_IPV6_RTR_SOLICITS,
3189 .procname = "router_solicitations",
3190 .data = &ipv6_devconf.rtr_solicits,
3191 .maxlen = sizeof(int),
3193 .proc_handler = &proc_dointvec,
3196 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3197 .procname = "router_solicitation_interval",
3198 .data = &ipv6_devconf.rtr_solicit_interval,
3199 .maxlen = sizeof(int),
3201 .proc_handler = &proc_dointvec_jiffies,
3202 .strategy = &sysctl_jiffies,
3205 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3206 .procname = "router_solicitation_delay",
3207 .data = &ipv6_devconf.rtr_solicit_delay,
3208 .maxlen = sizeof(int),
3210 .proc_handler = &proc_dointvec_jiffies,
3211 .strategy = &sysctl_jiffies,
3214 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3215 .procname = "force_mld_version",
3216 .data = &ipv6_devconf.force_mld_version,
3217 .maxlen = sizeof(int),
3219 .proc_handler = &proc_dointvec,
3221 #ifdef CONFIG_IPV6_PRIVACY
3223 .ctl_name = NET_IPV6_USE_TEMPADDR,
3224 .procname = "use_tempaddr",
3225 .data = &ipv6_devconf.use_tempaddr,
3226 .maxlen = sizeof(int),
3228 .proc_handler = &proc_dointvec,
3231 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3232 .procname = "temp_valid_lft",
3233 .data = &ipv6_devconf.temp_valid_lft,
3234 .maxlen = sizeof(int),
3236 .proc_handler = &proc_dointvec,
3239 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3240 .procname = "temp_prefered_lft",
3241 .data = &ipv6_devconf.temp_prefered_lft,
3242 .maxlen = sizeof(int),
3244 .proc_handler = &proc_dointvec,
3247 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3248 .procname = "regen_max_retry",
3249 .data = &ipv6_devconf.regen_max_retry,
3250 .maxlen = sizeof(int),
3252 .proc_handler = &proc_dointvec,
3255 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3256 .procname = "max_desync_factor",
3257 .data = &ipv6_devconf.max_desync_factor,
3258 .maxlen = sizeof(int),
3260 .proc_handler = &proc_dointvec,
3264 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3265 .procname = "max_addresses",
3266 .data = &ipv6_devconf.max_addresses,
3267 .maxlen = sizeof(int),
3269 .proc_handler = &proc_dointvec,
3272 .ctl_name = 0, /* sentinel */
3277 .ctl_name = NET_PROTO_CONF_ALL,
3280 .child = addrconf_sysctl.addrconf_vars,
3283 .ctl_name = 0, /* sentinel */
3286 .addrconf_conf_dir = {
3288 .ctl_name = NET_IPV6_CONF,
3291 .child = addrconf_sysctl.addrconf_dev,
3294 .ctl_name = 0, /* sentinel */
3297 .addrconf_proto_dir = {
3299 .ctl_name = NET_IPV6,
3302 .child = addrconf_sysctl.addrconf_conf_dir,
3305 .ctl_name = 0, /* sentinel */
3308 .addrconf_root_dir = {
3310 .ctl_name = CTL_NET,
3313 .child = addrconf_sysctl.addrconf_proto_dir,
3316 .ctl_name = 0, /* sentinel */
3321 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3324 struct net_device *dev = idev ? idev->dev : NULL;
3325 struct addrconf_sysctl_table *t;
3326 char *dev_name = NULL;
3328 t = kmalloc(sizeof(*t), GFP_KERNEL);
3331 memcpy(t, &addrconf_sysctl, sizeof(*t));
3332 for (i=0; t->addrconf_vars[i].data; i++) {
3333 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3334 t->addrconf_vars[i].de = NULL;
3335 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3338 dev_name = dev->name;
3339 t->addrconf_dev[0].ctl_name = dev->ifindex;
3341 dev_name = "default";
3342 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3346 * Make a copy of dev_name, because '.procname' is regarded as const
3347 * by sysctl and we wouldn't want anyone to change it under our feet
3348 * (see SIOCSIFNAME).
3350 dev_name = net_sysctl_strdup(dev_name);
3354 t->addrconf_dev[0].procname = dev_name;
3356 t->addrconf_dev[0].child = t->addrconf_vars;
3357 t->addrconf_dev[0].de = NULL;
3358 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3359 t->addrconf_conf_dir[0].de = NULL;
3360 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3361 t->addrconf_proto_dir[0].de = NULL;
3362 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3363 t->addrconf_root_dir[0].de = NULL;
3365 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3366 if (t->sysctl_header == NULL)
3381 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3384 struct addrconf_sysctl_table *t = p->sysctl;
3386 unregister_sysctl_table(t->sysctl_header);
3387 kfree(t->addrconf_dev[0].procname);
3399 int register_inet6addr_notifier(struct notifier_block *nb)
3401 return notifier_chain_register(&inet6addr_chain, nb);
3404 int unregister_inet6addr_notifier(struct notifier_block *nb)
3406 return notifier_chain_unregister(&inet6addr_chain,nb);
3410 * Init / cleanup code
3413 void __init addrconf_init(void)
3415 /* The addrconf netdev notifier requires that loopback_dev
3416 * has it's ipv6 private information allocated and setup
3417 * before it can bring up and give link-local addresses
3418 * to other devices which are up.
3420 * Unfortunately, loopback_dev is not necessarily the first
3421 * entry in the global dev_base list of net devices. In fact,
3422 * it is likely to be the very last entry on that list.
3423 * So this causes the notifier registry below to try and
3424 * give link-local addresses to all devices besides loopback_dev
3425 * first, then loopback_dev, which cases all the non-loopback_dev
3426 * devices to fail to get a link-local address.
3428 * So, as a temporary fix, register loopback_dev first by hand.
3429 * Longer term, all of the dependencies ipv6 has upon the loopback
3430 * device and it being up should be removed.
3433 addrconf_notify(&ipv6_dev_notf, NETDEV_REGISTER, &loopback_dev);
3434 if (loopback_dev.flags & IFF_UP)
3435 addrconf_notify(&ipv6_dev_notf, NETDEV_UP, &loopback_dev);
3438 register_netdevice_notifier(&ipv6_dev_notf);
3440 #ifdef CONFIG_IPV6_PRIVACY
3441 md5_tfm = crypto_alloc_tfm("md5", 0);
3442 if (unlikely(md5_tfm == NULL))
3444 "failed to load transform for md5\n");
3448 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3449 #ifdef CONFIG_SYSCTL
3450 addrconf_sysctl.sysctl_header =
3451 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3452 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3456 void __exit addrconf_cleanup(void)
3458 struct net_device *dev;
3459 struct inet6_dev *idev;
3460 struct inet6_ifaddr *ifa;
3463 unregister_netdevice_notifier(&ipv6_dev_notf);
3465 rtnetlink_links[PF_INET6] = NULL;
3466 #ifdef CONFIG_SYSCTL
3467 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3468 addrconf_sysctl_unregister(&ipv6_devconf);
3477 for (dev=dev_base; dev; dev=dev->next) {
3478 if ((idev = __in6_dev_get(dev)) == NULL)
3480 addrconf_ifdown(dev, 1);
3487 write_lock_bh(&addrconf_hash_lock);
3488 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3489 for (ifa=inet6_addr_lst[i]; ifa; ) {
3490 struct inet6_ifaddr *bifa;
3493 ifa = ifa->lst_next;
3494 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3495 /* Do not free it; something is wrong.
3496 Now we can investigate it with debugger.
3500 write_unlock_bh(&addrconf_hash_lock);
3502 del_timer(&addr_chk_timer);
3506 #ifdef CONFIG_IPV6_PRIVACY
3507 if (likely(md5_tfm != NULL)) {
3508 crypto_free_tfm(md5_tfm);
3513 #ifdef CONFIG_PROC_FS
3514 proc_net_remove("if_inet6");