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 int addrconf_ifdown(struct net_device *dev, int how);
133 static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags);
134 static void addrconf_dad_timer(unsigned long data);
135 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
136 static void addrconf_rs_timer(unsigned long data);
137 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
139 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
140 struct prefix_info *pinfo);
141 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
143 static struct notifier_block *inet6addr_chain;
145 struct ipv6_devconf ipv6_devconf = {
147 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
148 .mtu6 = IPV6_MIN_MTU,
150 .accept_redirects = 1,
152 .force_mld_version = 0,
154 .rtr_solicits = MAX_RTR_SOLICITATIONS,
155 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
156 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
157 #ifdef CONFIG_IPV6_PRIVACY
159 .temp_valid_lft = TEMP_VALID_LIFETIME,
160 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
161 .regen_max_retry = REGEN_MAX_RETRY,
162 .max_desync_factor = MAX_DESYNC_FACTOR,
164 .max_addresses = IPV6_MAX_ADDRESSES,
167 static struct ipv6_devconf ipv6_devconf_dflt = {
169 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
170 .mtu6 = IPV6_MIN_MTU,
172 .accept_redirects = 1,
175 .rtr_solicits = MAX_RTR_SOLICITATIONS,
176 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
177 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
178 #ifdef CONFIG_IPV6_PRIVACY
180 .temp_valid_lft = TEMP_VALID_LIFETIME,
181 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
182 .regen_max_retry = REGEN_MAX_RETRY,
183 .max_desync_factor = MAX_DESYNC_FACTOR,
185 .max_addresses = IPV6_MAX_ADDRESSES,
188 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
189 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
190 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
192 int ipv6_addr_type(const struct in6_addr *addr)
197 st = addr->s6_addr32[0];
199 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
200 type = IPV6_ADDR_MULTICAST;
202 switch((st & htonl(0x00FF0000))) {
203 case __constant_htonl(0x00010000):
204 type |= IPV6_ADDR_LOOPBACK;
207 case __constant_htonl(0x00020000):
208 type |= IPV6_ADDR_LINKLOCAL;
211 case __constant_htonl(0x00050000):
212 type |= IPV6_ADDR_SITELOCAL;
218 type = IPV6_ADDR_UNICAST;
220 /* Consider all addresses with the first three bits different of
221 000 and 111 as finished.
223 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
224 (st & htonl(0xE0000000)) != htonl(0xE0000000))
227 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
228 return (IPV6_ADDR_LINKLOCAL | type);
230 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
231 return (IPV6_ADDR_SITELOCAL | type);
233 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
234 if (addr->s6_addr32[2] == 0) {
235 if (addr->s6_addr32[3] == 0)
236 return IPV6_ADDR_ANY;
238 if (addr->s6_addr32[3] == htonl(0x00000001))
239 return (IPV6_ADDR_LOOPBACK | type);
241 return (IPV6_ADDR_COMPATv4 | type);
244 if (addr->s6_addr32[2] == htonl(0x0000ffff))
245 return IPV6_ADDR_MAPPED;
248 st &= htonl(0xFF000000);
250 return IPV6_ADDR_RESERVED;
251 st &= htonl(0xFE000000);
252 if (st == htonl(0x02000000))
253 return IPV6_ADDR_RESERVED; /* for NSAP */
254 if (st == htonl(0x04000000))
255 return IPV6_ADDR_RESERVED; /* for IPX */
259 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
261 if (del_timer(&ifp->timer))
265 enum addrconf_timer_t
272 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
273 enum addrconf_timer_t what,
276 if (!del_timer(&ifp->timer))
281 ifp->timer.function = addrconf_dad_timer;
284 ifp->timer.function = addrconf_rs_timer;
288 ifp->timer.expires = jiffies + when;
289 add_timer(&ifp->timer);
292 /* Nobody refers to this device, we may destroy it. */
294 void in6_dev_finish_destroy(struct inet6_dev *idev)
296 struct net_device *dev = idev->dev;
297 BUG_TRAP(idev->addr_list==NULL);
298 BUG_TRAP(idev->mc_list==NULL);
299 #ifdef NET_REFCNT_DEBUG
300 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
304 printk("Freeing alive inet6 device %p\n", idev);
307 snmp6_unregister_dev(idev);
312 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
314 struct inet6_dev *ndev;
318 if (dev->mtu < IPV6_MIN_MTU)
321 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
324 memset(ndev, 0, sizeof(struct inet6_dev));
326 ndev->lock = RW_LOCK_UNLOCKED;
328 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
329 ndev->cnf.mtu6 = dev->mtu;
330 ndev->cnf.sysctl = NULL;
331 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
332 if (ndev->nd_parms == NULL) {
337 /* We refer to the device */
340 if (snmp6_register_dev(ndev) < 0) {
342 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
343 __FUNCTION__, dev->name));
344 neigh_parms_release(&nd_tbl, ndev->nd_parms);
346 in6_dev_finish_destroy(ndev);
350 /* One reference from device. We must do this before
351 * we invoke __ipv6_regen_rndid().
355 #ifdef CONFIG_IPV6_PRIVACY
356 get_random_bytes(ndev->rndid, sizeof(ndev->rndid));
357 get_random_bytes(ndev->entropy, sizeof(ndev->entropy));
358 init_timer(&ndev->regen_timer);
359 ndev->regen_timer.function = ipv6_regen_rndid;
360 ndev->regen_timer.data = (unsigned long) ndev;
361 if ((dev->flags&IFF_LOOPBACK) ||
362 dev->type == ARPHRD_TUNNEL ||
363 dev->type == ARPHRD_SIT) {
365 "Disabled Privacy Extensions on device %p(%s)\n",
367 ndev->cnf.use_tempaddr = -1;
370 ipv6_regen_rndid((unsigned long) ndev);
374 write_lock_bh(&addrconf_lock);
376 write_unlock_bh(&addrconf_lock);
378 ipv6_mc_init_dev(ndev);
379 ndev->tstamp = jiffies;
381 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
382 NET_IPV6_NEIGH, "ipv6", &ndisc_ifinfo_sysctl_change);
383 addrconf_sysctl_register(ndev, &ndev->cnf);
389 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
391 struct inet6_dev *idev;
395 if ((idev = __in6_dev_get(dev)) == NULL) {
396 if ((idev = ipv6_add_dev(dev)) == NULL)
399 if (dev->flags&IFF_UP)
404 static void dev_forward_change(struct inet6_dev *idev)
406 struct net_device *dev;
407 struct inet6_ifaddr *ifa;
408 struct in6_addr addr;
413 if (dev && (dev->flags & IFF_MULTICAST)) {
414 ipv6_addr_all_routers(&addr);
416 if (idev->cnf.forwarding)
417 ipv6_dev_mc_inc(dev, &addr);
419 ipv6_dev_mc_dec(dev, &addr);
421 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
422 ipv6_addr_prefix(&addr, &ifa->addr, ifa->prefix_len);
423 if (ipv6_addr_any(&addr))
425 if (idev->cnf.forwarding)
426 ipv6_dev_ac_inc(idev->dev, &addr);
428 ipv6_dev_ac_dec(idev->dev, &addr);
433 static void addrconf_forward_change(struct inet6_dev *idev)
435 struct net_device *dev;
438 dev_forward_change(idev);
442 read_lock(&dev_base_lock);
443 for (dev=dev_base; dev; dev=dev->next) {
444 read_lock(&addrconf_lock);
445 idev = __in6_dev_get(dev);
447 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);
479 /* On success it returns ifp with increased reference count */
481 static struct inet6_ifaddr *
482 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
483 int scope, unsigned flags)
485 struct inet6_ifaddr *ifa;
487 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
491 /* Ignore adding duplicate addresses on an interface */
492 if (ipv6_chk_same_addr(addr, idev->dev)) {
493 spin_unlock_bh(&lock);
494 ADBG(("ipv6_add_addr: already assigned\n"));
495 return ERR_PTR(-EEXIST);
498 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
501 spin_unlock_bh(&lock);
502 ADBG(("ipv6_add_addr: malloc failed\n"));
503 return ERR_PTR(-ENOBUFS);
506 memset(ifa, 0, sizeof(struct inet6_ifaddr));
507 ipv6_addr_copy(&ifa->addr, addr);
509 spin_lock_init(&ifa->lock);
510 init_timer(&ifa->timer);
511 ifa->timer.data = (unsigned long) ifa;
513 ifa->prefix_len = pfxlen;
514 ifa->flags = flags | IFA_F_TENTATIVE;
515 ifa->cstamp = ifa->tstamp = jiffies;
517 read_lock(&addrconf_lock);
519 read_unlock(&addrconf_lock);
520 spin_unlock_bh(&lock);
522 return ERR_PTR(-ENODEV); /*XXX*/
531 /* Add to big hash table */
532 hash = ipv6_addr_hash(addr);
534 write_lock_bh(&addrconf_hash_lock);
535 ifa->lst_next = inet6_addr_lst[hash];
536 inet6_addr_lst[hash] = ifa;
538 write_unlock_bh(&addrconf_hash_lock);
540 write_lock_bh(&idev->lock);
541 /* Add to inet6_dev unicast addr list. */
542 ifa->if_next = idev->addr_list;
543 idev->addr_list = ifa;
545 #ifdef CONFIG_IPV6_PRIVACY
546 ifa->regen_count = 0;
547 if (ifa->flags&IFA_F_TEMPORARY) {
548 ifa->tmp_next = idev->tempaddr_list;
549 idev->tempaddr_list = ifa;
552 ifa->tmp_next = NULL;
557 write_unlock_bh(&idev->lock);
558 read_unlock(&addrconf_lock);
559 spin_unlock_bh(&lock);
561 notifier_call_chain(&inet6addr_chain,NETDEV_UP,ifa);
566 /* This function wants to get referenced ifp and releases it before return */
568 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
570 struct inet6_ifaddr *ifa, **ifap;
571 struct inet6_dev *idev = ifp->idev;
574 hash = ipv6_addr_hash(&ifp->addr);
578 write_lock_bh(&addrconf_hash_lock);
579 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
580 ifap = &ifa->lst_next) {
582 *ifap = ifa->lst_next;
584 ifa->lst_next = NULL;
588 write_unlock_bh(&addrconf_hash_lock);
590 write_lock_bh(&idev->lock);
591 #ifdef CONFIG_IPV6_PRIVACY
592 if (ifp->flags&IFA_F_TEMPORARY) {
593 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
594 ifap = &ifa->tmp_next) {
596 *ifap = ifa->tmp_next;
598 in6_ifa_put(ifp->ifpub);
602 ifa->tmp_next = NULL;
609 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;
610 ifap = &ifa->if_next) {
612 *ifap = ifa->if_next;
618 write_unlock_bh(&idev->lock);
620 ipv6_ifa_notify(RTM_DELADDR, ifp);
622 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
624 addrconf_del_timer(ifp);
629 #ifdef CONFIG_IPV6_PRIVACY
630 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
632 struct inet6_dev *idev;
633 struct in6_addr addr, *tmpaddr;
634 unsigned long tmp_prefered_lft, tmp_valid_lft;
640 spin_lock_bh(&ift->lock);
641 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
642 spin_unlock_bh(&ift->lock);
648 spin_lock_bh(&ifp->lock);
652 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
653 write_lock(&idev->lock);
654 if (idev->cnf.use_tempaddr <= 0) {
655 write_unlock(&idev->lock);
656 spin_unlock_bh(&ifp->lock);
658 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
664 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
665 idev->cnf.use_tempaddr = -1; /*XXX*/
666 write_unlock(&idev->lock);
667 spin_unlock_bh(&ifp->lock);
669 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
675 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
676 write_unlock(&idev->lock);
677 spin_unlock_bh(&ifp->lock);
679 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
685 memcpy(&addr.s6_addr[8], idev->rndid, 8);
686 tmp_valid_lft = min_t(__u32,
688 idev->cnf.temp_valid_lft);
689 tmp_prefered_lft = min_t(__u32,
691 idev->cnf.temp_prefered_lft - desync_factor / HZ);
692 tmp_plen = ifp->prefix_len;
693 max_addresses = idev->cnf.max_addresses;
694 write_unlock(&idev->lock);
695 spin_unlock_bh(&ifp->lock);
696 ift = !max_addresses ||
697 ipv6_count_addresses(idev) < max_addresses ?
698 ipv6_add_addr(idev, &addr, tmp_plen,
699 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : 0;
700 if (!ift || IS_ERR(ift)) {
704 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
708 spin_lock_bh(&ift->lock);
710 ift->valid_lft = tmp_valid_lft;
711 ift->prefered_lft = tmp_prefered_lft;
712 ift->cstamp = ifp->cstamp;
713 ift->tstamp = ifp->tstamp;
714 spin_unlock_bh(&ift->lock);
715 addrconf_dad_start(ift, 0);
724 * Choose an appropriate source address
726 * i) get an address with an appropriate scope
727 * ii) see if there is a specific route for the destination and use
728 * an address of the attached interface
729 * iii) don't use deprecated addresses
731 static int inline ipv6_saddr_pref(const struct inet6_ifaddr *ifp, u8 invpref)
734 pref = ifp->flags&IFA_F_DEPRECATED ? 0 : 2;
735 #ifdef CONFIG_IPV6_PRIVACY
736 pref |= (ifp->flags^invpref)&IFA_F_TEMPORARY ? 0 : 1;
741 #ifdef CONFIG_IPV6_PRIVACY
742 #define IPV6_GET_SADDR_MAXSCORE(score) ((score) == 3)
744 #define IPV6_GET_SADDR_MAXSCORE(score) (score)
747 int ipv6_dev_get_saddr(struct net_device *dev,
748 struct in6_addr *daddr, struct in6_addr *saddr, int onlink)
750 struct inet6_ifaddr *ifp = NULL;
751 struct inet6_ifaddr *match = NULL;
752 struct inet6_dev *idev;
755 int hiscore = -1, score;
758 scope = ipv6_addr_scope(daddr);
764 * search dev and walk through dev addresses
768 if (dev->flags & IFF_LOOPBACK)
771 read_lock(&addrconf_lock);
772 idev = __in6_dev_get(dev);
774 read_lock_bh(&idev->lock);
775 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
776 if (ifp->scope == scope) {
777 if (ifp->flags&IFA_F_TENTATIVE)
779 #ifdef CONFIG_IPV6_PRIVACY
780 score = ipv6_saddr_pref(ifp, idev->cnf.use_tempaddr > 1 ? IFA_F_TEMPORARY : 0);
782 score = ipv6_saddr_pref(ifp, 0);
784 if (score <= hiscore)
793 if (IPV6_GET_SADDR_MAXSCORE(score)) {
794 read_unlock_bh(&idev->lock);
795 read_unlock(&addrconf_lock);
800 read_unlock_bh(&idev->lock);
802 read_unlock(&addrconf_lock);
805 if (scope == IFA_LINK)
809 * dev == NULL or search failed for specified dev
812 read_lock(&dev_base_lock);
813 read_lock(&addrconf_lock);
814 for (dev = dev_base; dev; dev=dev->next) {
815 idev = __in6_dev_get(dev);
817 read_lock_bh(&idev->lock);
818 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
819 if (ifp->scope == scope) {
820 if (ifp->flags&IFA_F_TENTATIVE)
822 #ifdef CONFIG_IPV6_PRIVACY
823 score = ipv6_saddr_pref(ifp, idev->cnf.use_tempaddr > 1 ? IFA_F_TEMPORARY : 0);
825 score = ipv6_saddr_pref(ifp, 0);
827 if (score <= hiscore)
836 if (IPV6_GET_SADDR_MAXSCORE(score)) {
837 read_unlock_bh(&idev->lock);
838 goto out_unlock_base;
842 read_unlock_bh(&idev->lock);
847 read_unlock(&addrconf_lock);
848 read_unlock(&dev_base_lock);
851 err = -EADDRNOTAVAIL;
853 ipv6_addr_copy(saddr, &match->addr);
862 int ipv6_get_saddr(struct dst_entry *dst,
863 struct in6_addr *daddr, struct in6_addr *saddr)
866 struct net_device *dev = NULL;
869 rt = (struct rt6_info *) dst;
873 onlink = (rt && (rt->rt6i_flags & RTF_ALLONLINK));
875 return ipv6_dev_get_saddr(dev, daddr, saddr, onlink);
879 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
881 struct inet6_dev *idev;
882 int err = -EADDRNOTAVAIL;
884 read_lock(&addrconf_lock);
885 if ((idev = __in6_dev_get(dev)) != NULL) {
886 struct inet6_ifaddr *ifp;
888 read_lock_bh(&idev->lock);
889 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
890 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
891 ipv6_addr_copy(addr, &ifp->addr);
896 read_unlock_bh(&idev->lock);
898 read_unlock(&addrconf_lock);
902 static int ipv6_count_addresses(struct inet6_dev *idev)
905 struct inet6_ifaddr *ifp;
907 read_lock_bh(&idev->lock);
908 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
910 read_unlock_bh(&idev->lock);
914 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
916 struct inet6_ifaddr * ifp;
917 u8 hash = ipv6_addr_hash(addr);
919 read_lock_bh(&addrconf_hash_lock);
920 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
921 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
922 !(ifp->flags&IFA_F_TENTATIVE)) {
923 if (dev == NULL || ifp->idev->dev == dev ||
924 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
928 read_unlock_bh(&addrconf_hash_lock);
933 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
935 struct inet6_ifaddr * ifp;
936 u8 hash = ipv6_addr_hash(addr);
938 read_lock_bh(&addrconf_hash_lock);
939 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
940 if (ipv6_addr_cmp(&ifp->addr, addr) == 0) {
941 if (dev == NULL || ifp->idev->dev == dev)
945 read_unlock_bh(&addrconf_hash_lock);
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_cmp(&ifp->addr, addr) == 0) {
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_cmp(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 net_device *dev, struct in6_addr *addr)
1050 struct in6_addr maddr;
1052 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1055 addrconf_addr_solict_mult(addr, &maddr);
1056 ipv6_dev_mc_dec(dev, &maddr);
1060 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1062 switch (dev->type) {
1065 case ARPHRD_IEEE802_TR:
1066 if (dev->addr_len != ETH_ALEN)
1068 memcpy(eui, dev->dev_addr, 3);
1069 memcpy(eui + 5, dev->dev_addr+3, 3);
1075 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1076 if (dev->addr_len != ARCNET_ALEN)
1079 eui[7] = *(u8*)dev->dev_addr;
1085 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1088 struct inet6_ifaddr *ifp;
1090 read_lock_bh(&idev->lock);
1091 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1092 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1093 memcpy(eui, ifp->addr.s6_addr+8, 8);
1098 read_unlock_bh(&idev->lock);
1102 #ifdef CONFIG_IPV6_PRIVACY
1103 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1104 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1106 struct net_device *dev;
1107 struct scatterlist sg[2];
1109 sg[0].page = virt_to_page(idev->entropy);
1110 sg[0].offset = offset_in_page(idev->entropy);
1112 sg[1].page = virt_to_page(idev->work_eui64);
1113 sg[1].offset = offset_in_page(idev->work_eui64);
1118 if (ipv6_generate_eui64(idev->work_eui64, dev)) {
1120 "__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
1122 get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
1125 spin_lock(&md5_tfm_lock);
1126 if (unlikely(md5_tfm == NULL)) {
1127 spin_unlock(&md5_tfm_lock);
1130 crypto_digest_init(md5_tfm);
1131 crypto_digest_update(md5_tfm, sg, 2);
1132 crypto_digest_final(md5_tfm, idev->work_digest);
1133 spin_unlock(&md5_tfm_lock);
1135 memcpy(idev->rndid, &idev->work_digest[0], 8);
1136 idev->rndid[0] &= ~0x02;
1137 memcpy(idev->entropy, &idev->work_digest[8], 8);
1140 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1141 * check if generated address is not inappropriate
1143 * - Reserved subnet anycast (RFC 2526)
1144 * 11111101 11....11 1xxxxxxx
1145 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1146 * 00-00-5E-FE-xx-xx-xx-xx
1148 * - XXX: already assigned to an address on the device
1150 if (idev->rndid[0] == 0xfd &&
1151 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1152 (idev->rndid[7]&0x80))
1154 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1155 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1157 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1164 static void ipv6_regen_rndid(unsigned long data)
1166 struct inet6_dev *idev = (struct inet6_dev *) data;
1167 unsigned long expires;
1169 read_lock_bh(&addrconf_lock);
1170 write_lock_bh(&idev->lock);
1175 if (__ipv6_regen_rndid(idev) < 0)
1179 idev->cnf.temp_prefered_lft * HZ -
1180 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1181 if (time_before(expires, jiffies)) {
1183 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1188 if (!mod_timer(&idev->regen_timer, expires))
1192 write_unlock_bh(&idev->lock);
1193 read_unlock_bh(&addrconf_lock);
1197 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1200 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1201 ret = __ipv6_regen_rndid(idev);
1211 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1212 unsigned long expires, unsigned flags)
1214 struct in6_rtmsg rtmsg;
1216 memset(&rtmsg, 0, sizeof(rtmsg));
1217 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1218 rtmsg.rtmsg_dst_len = plen;
1219 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1220 rtmsg.rtmsg_ifindex = dev->ifindex;
1221 rtmsg.rtmsg_info = expires;
1222 rtmsg.rtmsg_flags = RTF_UP|flags;
1223 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1225 /* Prevent useless cloning on PtP SIT.
1226 This thing is done here expecting that the whole
1227 class of non-broadcast devices need not cloning.
1229 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1230 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1232 ip6_route_add(&rtmsg, NULL, NULL);
1235 /* Create "default" multicast route to the interface */
1237 static void addrconf_add_mroute(struct net_device *dev)
1239 struct in6_rtmsg rtmsg;
1241 memset(&rtmsg, 0, sizeof(rtmsg));
1242 ipv6_addr_set(&rtmsg.rtmsg_dst,
1243 htonl(0xFF000000), 0, 0, 0);
1244 rtmsg.rtmsg_dst_len = 8;
1245 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1246 rtmsg.rtmsg_ifindex = dev->ifindex;
1247 rtmsg.rtmsg_flags = RTF_UP;
1248 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1249 ip6_route_add(&rtmsg, NULL, NULL);
1252 static void sit_route_add(struct net_device *dev)
1254 struct in6_rtmsg rtmsg;
1256 memset(&rtmsg, 0, sizeof(rtmsg));
1258 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1259 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1261 /* prefix length - 96 bits "::d.d.d.d" */
1262 rtmsg.rtmsg_dst_len = 96;
1263 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1264 rtmsg.rtmsg_ifindex = dev->ifindex;
1266 ip6_route_add(&rtmsg, NULL, NULL);
1269 static void addrconf_add_lroute(struct net_device *dev)
1271 struct in6_addr addr;
1273 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1274 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1277 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1279 struct inet6_dev *idev;
1283 if ((idev = ipv6_find_idev(dev)) == NULL)
1286 /* Add default multicast route */
1287 addrconf_add_mroute(dev);
1289 /* Add link local route */
1290 addrconf_add_lroute(dev);
1294 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1296 struct prefix_info *pinfo;
1300 unsigned long rt_expires;
1301 struct inet6_dev *in6_dev;
1303 pinfo = (struct prefix_info *) opt;
1305 if (len < sizeof(struct prefix_info)) {
1306 ADBG(("addrconf: prefix option too short\n"));
1311 * Validation checks ([ADDRCONF], page 19)
1314 addr_type = ipv6_addr_type(&pinfo->prefix);
1316 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1319 valid_lft = ntohl(pinfo->valid);
1320 prefered_lft = ntohl(pinfo->prefered);
1322 if (prefered_lft > valid_lft) {
1323 if (net_ratelimit())
1324 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1328 in6_dev = in6_dev_get(dev);
1330 if (in6_dev == NULL) {
1331 if (net_ratelimit())
1332 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1337 * Two things going on here:
1338 * 1) Add routes for on-link prefixes
1339 * 2) Configure prefixes with the auto flag set
1342 /* Avoid arithmetic overflow. Really, we could
1343 save rt_expires in seconds, likely valid_lft,
1344 but it would require division in fib gc, that it
1347 if (valid_lft >= 0x7FFFFFFF/HZ)
1350 rt_expires = jiffies + valid_lft * HZ;
1352 if (pinfo->onlink) {
1353 struct rt6_info *rt;
1354 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1356 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1357 if (rt->rt6i_flags&RTF_EXPIRES) {
1358 if (valid_lft == 0) {
1359 ip6_del_rt(rt, NULL, NULL);
1362 rt->rt6i_expires = rt_expires;
1365 } else if (valid_lft) {
1366 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1367 dev, rt_expires, RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1370 dst_release(&rt->u.dst);
1373 /* Try to figure out our local address for this prefix */
1375 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1376 struct inet6_ifaddr * ifp;
1377 struct in6_addr addr;
1378 int create = 0, update_lft = 0;
1380 if (pinfo->prefix_len == 64) {
1381 memcpy(&addr, &pinfo->prefix, 8);
1382 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1383 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1384 in6_dev_put(in6_dev);
1389 if (net_ratelimit())
1390 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1392 in6_dev_put(in6_dev);
1397 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1399 if (ifp == NULL && valid_lft) {
1400 int max_addresses = in6_dev->cnf.max_addresses;
1402 /* Do not allow to create too much of autoconfigured
1403 * addresses; this would be too easy way to crash kernel.
1405 if (!max_addresses ||
1406 ipv6_count_addresses(in6_dev) < max_addresses)
1407 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1408 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1410 if (!ifp || IS_ERR(ifp)) {
1411 in6_dev_put(in6_dev);
1415 update_lft = create = 1;
1416 ifp->cstamp = jiffies;
1417 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1423 #ifdef CONFIG_IPV6_PRIVACY
1424 struct inet6_ifaddr *ift;
1428 /* update lifetime (RFC2462 5.5.3 e) */
1429 spin_lock(&ifp->lock);
1431 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1432 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1435 if (!update_lft && stored_lft) {
1436 if (valid_lft > MIN_VALID_LIFETIME ||
1437 valid_lft > stored_lft)
1439 else if (stored_lft <= MIN_VALID_LIFETIME) {
1440 /* valid_lft <= stored_lft is always true */
1444 valid_lft = MIN_VALID_LIFETIME;
1445 if (valid_lft < prefered_lft)
1446 prefered_lft = valid_lft;
1452 ifp->valid_lft = valid_lft;
1453 ifp->prefered_lft = prefered_lft;
1456 ifp->flags &= ~IFA_F_DEPRECATED;
1457 spin_unlock(&ifp->lock);
1459 if (!(flags&IFA_F_TENTATIVE))
1460 ipv6_ifa_notify((flags&IFA_F_DEPRECATED) ?
1461 0 : RTM_NEWADDR, ifp);
1463 spin_unlock(&ifp->lock);
1465 #ifdef CONFIG_IPV6_PRIVACY
1466 read_lock_bh(&in6_dev->lock);
1467 /* update all temporary addresses in the list */
1468 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1470 * When adjusting the lifetimes of an existing
1471 * temporary address, only lower the lifetimes.
1472 * Implementations must not increase the
1473 * lifetimes of an existing temporary address
1474 * when processing a Prefix Information Option.
1476 spin_lock(&ift->lock);
1478 if (ift->valid_lft > valid_lft &&
1479 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1480 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1481 if (ift->prefered_lft > prefered_lft &&
1482 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1483 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1484 spin_unlock(&ift->lock);
1485 if (!(flags&IFA_F_TENTATIVE))
1486 ipv6_ifa_notify(0, ift);
1489 if (create && in6_dev->cnf.use_tempaddr > 0) {
1491 * When a new public address is created as described in [ADDRCONF],
1492 * also create a new temporary address.
1494 read_unlock_bh(&in6_dev->lock);
1495 ipv6_create_tempaddr(ifp, NULL);
1497 read_unlock_bh(&in6_dev->lock);
1504 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1505 in6_dev_put(in6_dev);
1509 * Set destination address.
1510 * Special case for SIT interfaces where we create a new "virtual"
1513 int addrconf_set_dstaddr(void *arg)
1515 struct in6_ifreq ireq;
1516 struct net_device *dev;
1522 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1525 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1531 if (dev->type == ARPHRD_SIT) {
1534 struct ip_tunnel_parm p;
1536 err = -EADDRNOTAVAIL;
1537 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1540 memset(&p, 0, sizeof(p));
1541 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1545 p.iph.protocol = IPPROTO_IPV6;
1547 ifr.ifr_ifru.ifru_data = (void*)&p;
1549 oldfs = get_fs(); set_fs(KERNEL_DS);
1550 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1555 if ((dev = __dev_get_by_name(p.name)) == NULL)
1557 err = dev_open(dev);
1567 * Manual configuration of address on an interface
1569 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1571 struct inet6_ifaddr *ifp;
1572 struct inet6_dev *idev;
1573 struct net_device *dev;
1578 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1581 if (!(dev->flags&IFF_UP))
1584 if ((idev = addrconf_add_dev(dev)) == NULL)
1587 scope = ipv6_addr_scope(pfx);
1589 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1591 addrconf_dad_start(ifp, 0);
1596 return PTR_ERR(ifp);
1599 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1601 struct inet6_ifaddr *ifp;
1602 struct inet6_dev *idev;
1603 struct net_device *dev;
1605 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1608 if ((idev = __in6_dev_get(dev)) == NULL)
1611 read_lock_bh(&idev->lock);
1612 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1613 if (ifp->prefix_len == plen &&
1614 (!memcmp(pfx, &ifp->addr, sizeof(struct in6_addr)))) {
1616 read_unlock_bh(&idev->lock);
1620 /* If the last address is deleted administratively,
1621 disable IPv6 on this interface.
1623 if (idev->addr_list == NULL)
1624 addrconf_ifdown(idev->dev, 1);
1628 read_unlock_bh(&idev->lock);
1629 return -EADDRNOTAVAIL;
1633 int addrconf_add_ifaddr(void *arg)
1635 struct in6_ifreq ireq;
1638 if (!capable(CAP_NET_ADMIN))
1641 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1645 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1650 int addrconf_del_ifaddr(void *arg)
1652 struct in6_ifreq ireq;
1655 if (!capable(CAP_NET_ADMIN))
1658 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1662 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1667 static void sit_add_v4_addrs(struct inet6_dev *idev)
1669 struct inet6_ifaddr * ifp;
1670 struct in6_addr addr;
1671 struct net_device *dev;
1676 memset(&addr, 0, sizeof(struct in6_addr));
1677 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1679 if (idev->dev->flags&IFF_POINTOPOINT) {
1680 addr.s6_addr32[0] = htonl(0xfe800000);
1683 scope = IPV6_ADDR_COMPATv4;
1686 if (addr.s6_addr32[3]) {
1687 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1689 spin_lock_bh(&ifp->lock);
1690 ifp->flags &= ~IFA_F_TENTATIVE;
1691 spin_unlock_bh(&ifp->lock);
1692 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1698 for (dev = dev_base; dev != NULL; dev = dev->next) {
1699 struct in_device * in_dev = __in_dev_get(dev);
1700 if (in_dev && (dev->flags & IFF_UP)) {
1701 struct in_ifaddr * ifa;
1705 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1708 addr.s6_addr32[3] = ifa->ifa_local;
1710 if (ifa->ifa_scope == RT_SCOPE_LINK)
1712 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1713 if (idev->dev->flags&IFF_POINTOPOINT)
1717 if (idev->dev->flags&IFF_POINTOPOINT)
1722 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1725 spin_lock_bh(&ifp->lock);
1726 ifp->flags &= ~IFA_F_TENTATIVE;
1727 spin_unlock_bh(&ifp->lock);
1728 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1736 static void init_loopback(struct net_device *dev)
1738 struct inet6_dev *idev;
1739 struct inet6_ifaddr * ifp;
1745 if ((idev = ipv6_find_idev(dev)) == NULL) {
1746 printk(KERN_DEBUG "init loopback: add_dev failed\n");
1750 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
1752 spin_lock_bh(&ifp->lock);
1753 ifp->flags &= ~IFA_F_TENTATIVE;
1754 spin_unlock_bh(&ifp->lock);
1755 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1760 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
1762 struct inet6_ifaddr * ifp;
1764 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
1766 addrconf_dad_start(ifp, 0);
1771 static void addrconf_dev_config(struct net_device *dev)
1773 struct in6_addr addr;
1774 struct inet6_dev * idev;
1778 if ((dev->type != ARPHRD_ETHER) &&
1779 (dev->type != ARPHRD_FDDI) &&
1780 (dev->type != ARPHRD_IEEE802_TR) &&
1781 (dev->type != ARPHRD_ARCNET)) {
1782 /* Alas, we support only Ethernet autoconfiguration. */
1786 idev = addrconf_add_dev(dev);
1790 memset(&addr, 0, sizeof(struct in6_addr));
1791 addr.s6_addr32[0] = htonl(0xFE800000);
1793 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
1794 addrconf_add_linklocal(idev, &addr);
1797 static void addrconf_sit_config(struct net_device *dev)
1799 struct inet6_dev *idev;
1804 * Configure the tunnel with one of our IPv4
1805 * addresses... we should configure all of
1806 * our v4 addrs in the tunnel
1809 if ((idev = ipv6_find_idev(dev)) == NULL) {
1810 printk(KERN_DEBUG "init sit: add_dev failed\n");
1814 sit_add_v4_addrs(idev);
1816 if (dev->flags&IFF_POINTOPOINT) {
1817 addrconf_add_mroute(dev);
1818 addrconf_add_lroute(dev);
1824 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
1826 struct in6_addr lladdr;
1828 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
1829 addrconf_add_linklocal(idev, &lladdr);
1835 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
1837 struct net_device *link_dev;
1839 /* first try to inherit the link-local address from the link device */
1840 if (idev->dev->iflink &&
1841 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
1842 if (!ipv6_inherit_linklocal(idev, link_dev))
1845 /* then try to inherit it from any device */
1846 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
1847 if (!ipv6_inherit_linklocal(idev, link_dev))
1850 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
1854 * Autoconfigure tunnel with a link-local address so routing protocols,
1855 * DHCPv6, MLD etc. can be run over the virtual link
1858 static void addrconf_ip6_tnl_config(struct net_device *dev)
1860 struct inet6_dev *idev;
1864 if ((idev = addrconf_add_dev(dev)) == NULL) {
1865 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
1868 ip6_tnl_add_linklocal(idev);
1869 addrconf_add_mroute(dev);
1872 static int addrconf_notify(struct notifier_block *this, unsigned long event,
1875 struct net_device *dev = (struct net_device *) data;
1876 struct inet6_dev *idev = __in6_dev_get(dev);
1882 addrconf_sit_config(dev);
1884 case ARPHRD_TUNNEL6:
1885 addrconf_ip6_tnl_config(dev);
1887 case ARPHRD_LOOPBACK:
1892 addrconf_dev_config(dev);
1896 /* If the MTU changed during the interface down, when the
1897 interface up, the changed MTU must be reflected in the
1898 idev as well as routers.
1900 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
1901 rt6_mtu_change(dev, dev->mtu);
1902 idev->cnf.mtu6 = dev->mtu;
1904 idev->tstamp = jiffies;
1905 inet6_ifinfo_notify(RTM_NEWLINK, idev);
1906 /* If the changed mtu during down is lower than IPV6_MIN_MTU
1907 stop IPv6 on this interface.
1909 if (dev->mtu < IPV6_MIN_MTU)
1910 addrconf_ifdown(dev, event != NETDEV_DOWN);
1914 case NETDEV_CHANGEMTU:
1915 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
1916 rt6_mtu_change(dev, dev->mtu);
1917 idev->cnf.mtu6 = dev->mtu;
1921 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
1924 case NETDEV_UNREGISTER:
1926 * Remove all addresses from this interface.
1928 addrconf_ifdown(dev, event != NETDEV_DOWN);
1932 case NETDEV_CHANGENAME:
1933 #ifdef CONFIG_SYSCTL
1935 addrconf_sysctl_unregister(&idev->cnf);
1936 neigh_sysctl_unregister(idev->nd_parms);
1937 neigh_sysctl_register(dev, idev->nd_parms, NET_IPV6, NET_IPV6_NEIGH, "ipv6", &ndisc_ifinfo_sysctl_change);
1938 addrconf_sysctl_register(idev, &idev->cnf);
1948 * addrconf module should be notified of a device going up
1950 static struct notifier_block ipv6_dev_notf = {
1951 .notifier_call = addrconf_notify,
1955 static int addrconf_ifdown(struct net_device *dev, int how)
1957 struct inet6_dev *idev;
1958 struct inet6_ifaddr *ifa, **bifa;
1964 neigh_ifdown(&nd_tbl, dev);
1966 idev = __in6_dev_get(dev);
1970 /* Step 1: remove reference to ipv6 device from parent device.
1974 write_lock_bh(&addrconf_lock);
1975 dev->ip6_ptr = NULL;
1977 write_unlock_bh(&addrconf_lock);
1980 /* Step 2: clear hash table */
1981 for (i=0; i<IN6_ADDR_HSIZE; i++) {
1982 bifa = &inet6_addr_lst[i];
1984 write_lock_bh(&addrconf_hash_lock);
1985 while ((ifa = *bifa) != NULL) {
1986 if (ifa->idev == idev) {
1987 *bifa = ifa->lst_next;
1988 ifa->lst_next = NULL;
1989 addrconf_del_timer(ifa);
1993 bifa = &ifa->lst_next;
1995 write_unlock_bh(&addrconf_hash_lock);
1998 write_lock_bh(&idev->lock);
2000 /* Step 3: clear flags for stateless addrconf */
2002 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD);
2004 /* Step 4: clear address list */
2005 #ifdef CONFIG_IPV6_PRIVACY
2006 if (how == 1 && del_timer(&idev->regen_timer))
2009 /* clear tempaddr list */
2010 while ((ifa = idev->tempaddr_list) != NULL) {
2011 idev->tempaddr_list = ifa->tmp_next;
2012 ifa->tmp_next = NULL;
2014 write_unlock_bh(&idev->lock);
2015 spin_lock_bh(&ifa->lock);
2018 in6_ifa_put(ifa->ifpub);
2021 spin_unlock_bh(&ifa->lock);
2023 write_lock_bh(&idev->lock);
2026 while ((ifa = idev->addr_list) != NULL) {
2027 idev->addr_list = ifa->if_next;
2028 ifa->if_next = NULL;
2030 addrconf_del_timer(ifa);
2031 write_unlock_bh(&idev->lock);
2033 ipv6_ifa_notify(RTM_DELADDR, ifa);
2036 write_lock_bh(&idev->lock);
2038 write_unlock_bh(&idev->lock);
2040 /* Step 5: Discard multicast list */
2043 ipv6_mc_destroy_dev(idev);
2047 /* Step 5: netlink notification of this interface */
2048 idev->tstamp = jiffies;
2049 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2051 /* Shot the device (if unregistered) */
2054 #ifdef CONFIG_SYSCTL
2055 addrconf_sysctl_unregister(&idev->cnf);
2056 neigh_sysctl_unregister(idev->nd_parms);
2058 neigh_parms_release(&nd_tbl, idev->nd_parms);
2064 static void addrconf_rs_timer(unsigned long data)
2066 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2068 if (ifp->idev->cnf.forwarding)
2071 if (ifp->idev->if_flags & IF_RA_RCVD) {
2073 * Announcement received after solicitation
2079 spin_lock(&ifp->lock);
2080 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2081 struct in6_addr all_routers;
2083 /* The wait after the last probe can be shorter */
2084 addrconf_mod_timer(ifp, AC_RS,
2085 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2086 ifp->idev->cnf.rtr_solicit_delay :
2087 ifp->idev->cnf.rtr_solicit_interval);
2088 spin_unlock(&ifp->lock);
2090 ipv6_addr_all_routers(&all_routers);
2092 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2094 struct in6_rtmsg rtmsg;
2096 spin_unlock(&ifp->lock);
2098 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2099 ifp->idev->dev->name);
2101 memset(&rtmsg, 0, sizeof(struct in6_rtmsg));
2102 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
2103 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
2104 rtmsg.rtmsg_flags = (RTF_ALLONLINK | RTF_DEFAULT | RTF_UP);
2106 rtmsg.rtmsg_ifindex = ifp->idev->dev->ifindex;
2108 ip6_route_add(&rtmsg, NULL, NULL);
2116 * Duplicate Address Detection
2118 static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags)
2120 struct net_device *dev;
2121 unsigned long rand_num;
2123 dev = ifp->idev->dev;
2125 addrconf_join_solict(dev, &ifp->addr);
2127 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2128 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2131 net_srandom(ifp->addr.s6_addr32[3]);
2132 rand_num = net_random() % (ifp->idev->cnf.rtr_solicit_delay ? : 1);
2134 spin_lock_bh(&ifp->lock);
2136 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2137 !(ifp->flags&IFA_F_TENTATIVE)) {
2138 ifp->flags &= ~IFA_F_TENTATIVE;
2139 spin_unlock_bh(&ifp->lock);
2141 addrconf_dad_completed(ifp);
2145 ifp->probes = ifp->idev->cnf.dad_transmits;
2146 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2148 spin_unlock_bh(&ifp->lock);
2151 static void addrconf_dad_timer(unsigned long data)
2153 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2154 struct in6_addr unspec;
2155 struct in6_addr mcaddr;
2157 spin_lock_bh(&ifp->lock);
2158 if (ifp->probes == 0) {
2160 * DAD was successful
2163 ifp->flags &= ~IFA_F_TENTATIVE;
2164 spin_unlock_bh(&ifp->lock);
2166 addrconf_dad_completed(ifp);
2173 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2174 spin_unlock_bh(&ifp->lock);
2176 /* send a neighbour solicitation for our addr */
2177 memset(&unspec, 0, sizeof(unspec));
2178 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2179 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2184 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2186 struct net_device * dev = ifp->idev->dev;
2189 * Configure the address for reception. Now it is valid.
2192 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2194 /* If added prefix is link local and forwarding is off,
2195 start sending router solicitations.
2198 if (ifp->idev->cnf.forwarding == 0 &&
2199 ifp->idev->cnf.rtr_solicits > 0 &&
2200 (dev->flags&IFF_LOOPBACK) == 0 &&
2201 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2202 struct in6_addr all_routers;
2204 ipv6_addr_all_routers(&all_routers);
2207 * If a host as already performed a random delay
2208 * [...] as part of DAD [...] there is no need
2209 * to delay again before sending the first RS
2211 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2213 spin_lock_bh(&ifp->lock);
2215 ifp->idev->if_flags |= IF_RS_SENT;
2216 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2217 spin_unlock_bh(&ifp->lock);
2220 if (ifp->idev->cnf.forwarding) {
2221 struct in6_addr addr;
2223 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2224 if (!ipv6_addr_any(&addr))
2225 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
2229 #ifdef CONFIG_PROC_FS
2230 struct if6_iter_state {
2234 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2236 struct inet6_ifaddr *ifa = NULL;
2237 struct if6_iter_state *state = seq->private;
2239 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2240 ifa = inet6_addr_lst[state->bucket];
2247 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2249 struct if6_iter_state *state = seq->private;
2251 ifa = ifa->lst_next;
2253 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2254 ifa = inet6_addr_lst[state->bucket];
2260 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2262 struct inet6_ifaddr *ifa = if6_get_first(seq);
2265 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2267 return pos ? NULL : ifa;
2270 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2272 read_lock_bh(&addrconf_hash_lock);
2273 return if6_get_idx(seq, *pos);
2276 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2278 struct inet6_ifaddr *ifa;
2280 ifa = if6_get_next(seq, v);
2285 static void if6_seq_stop(struct seq_file *seq, void *v)
2287 read_unlock_bh(&addrconf_hash_lock);
2290 static int if6_seq_show(struct seq_file *seq, void *v)
2292 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2294 "%04x%04x%04x%04x%04x%04x%04x%04x %02x %02x %02x %02x %8s\n",
2296 ifp->idev->dev->ifindex,
2300 ifp->idev->dev->name);
2304 static struct seq_operations if6_seq_ops = {
2305 .start = if6_seq_start,
2306 .next = if6_seq_next,
2307 .show = if6_seq_show,
2308 .stop = if6_seq_stop,
2311 static int if6_seq_open(struct inode *inode, struct file *file)
2313 struct seq_file *seq;
2315 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2319 memset(s, 0, sizeof(*s));
2321 rc = seq_open(file, &if6_seq_ops);
2325 seq = file->private_data;
2334 static struct file_operations if6_fops = {
2335 .owner = THIS_MODULE,
2336 .open = if6_seq_open,
2338 .llseek = seq_lseek,
2339 .release = seq_release_private,
2342 int __init if6_proc_init(void)
2344 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2349 void if6_proc_exit(void)
2351 proc_net_remove("if_inet6");
2353 #endif /* CONFIG_PROC_FS */
2356 * Periodic address status verification
2359 static void addrconf_verify(unsigned long foo)
2361 struct inet6_ifaddr *ifp;
2362 unsigned long now, next;
2365 spin_lock_bh(&addrconf_verify_lock);
2367 next = now + ADDR_CHECK_FREQUENCY;
2369 del_timer(&addr_chk_timer);
2371 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2374 write_lock(&addrconf_hash_lock);
2375 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2377 #ifdef CONFIG_IPV6_PRIVACY
2378 unsigned long regen_advance;
2381 if (ifp->flags & IFA_F_PERMANENT)
2384 spin_lock(&ifp->lock);
2385 age = (now - ifp->tstamp) / HZ;
2387 #ifdef CONFIG_IPV6_PRIVACY
2388 regen_advance = ifp->idev->cnf.regen_max_retry *
2389 ifp->idev->cnf.dad_transmits *
2390 ifp->idev->nd_parms->retrans_time / HZ;
2393 if (age >= ifp->valid_lft) {
2394 spin_unlock(&ifp->lock);
2396 write_unlock(&addrconf_hash_lock);
2399 } else if (age >= ifp->prefered_lft) {
2400 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2403 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2405 ifp->flags |= IFA_F_DEPRECATED;
2408 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2409 next = ifp->tstamp + ifp->valid_lft * HZ;
2411 spin_unlock(&ifp->lock);
2415 write_unlock(&addrconf_hash_lock);
2417 ipv6_ifa_notify(0, ifp);
2421 #ifdef CONFIG_IPV6_PRIVACY
2422 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2423 !(ifp->flags&IFA_F_TENTATIVE)) {
2424 if (age >= ifp->prefered_lft - regen_advance) {
2425 struct inet6_ifaddr *ifpub = ifp->ifpub;
2426 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2427 next = ifp->tstamp + ifp->prefered_lft * HZ;
2428 if (!ifp->regen_count && ifpub) {
2431 in6_ifa_hold(ifpub);
2432 spin_unlock(&ifp->lock);
2433 write_unlock(&addrconf_hash_lock);
2434 ipv6_create_tempaddr(ifpub, ifp);
2439 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2440 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2441 spin_unlock(&ifp->lock);
2444 /* ifp->prefered_lft <= ifp->valid_lft */
2445 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2446 next = ifp->tstamp + ifp->prefered_lft * HZ;
2447 spin_unlock(&ifp->lock);
2450 write_unlock(&addrconf_hash_lock);
2453 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2454 add_timer(&addr_chk_timer);
2455 spin_unlock_bh(&addrconf_verify_lock);
2459 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2461 struct rtattr **rta = arg;
2462 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2463 struct in6_addr *pfx;
2466 if (rta[IFA_ADDRESS-1]) {
2467 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2469 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2471 if (rta[IFA_LOCAL-1]) {
2472 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2474 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2479 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2483 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2485 struct rtattr **rta = arg;
2486 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2487 struct in6_addr *pfx;
2490 if (rta[IFA_ADDRESS-1]) {
2491 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2493 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2495 if (rta[IFA_LOCAL-1]) {
2496 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2498 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2503 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2506 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2507 u32 pid, u32 seq, int event)
2509 struct ifaddrmsg *ifm;
2510 struct nlmsghdr *nlh;
2511 struct ifa_cacheinfo ci;
2512 unsigned char *b = skb->tail;
2514 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2515 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2516 ifm = NLMSG_DATA(nlh);
2517 ifm->ifa_family = AF_INET6;
2518 ifm->ifa_prefixlen = ifa->prefix_len;
2519 ifm->ifa_flags = ifa->flags;
2520 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2521 if (ifa->scope&IFA_HOST)
2522 ifm->ifa_scope = RT_SCOPE_HOST;
2523 else if (ifa->scope&IFA_LINK)
2524 ifm->ifa_scope = RT_SCOPE_LINK;
2525 else if (ifa->scope&IFA_SITE)
2526 ifm->ifa_scope = RT_SCOPE_SITE;
2527 ifm->ifa_index = ifa->idev->dev->ifindex;
2528 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2529 if (!(ifa->flags&IFA_F_PERMANENT)) {
2530 ci.ifa_prefered = ifa->prefered_lft;
2531 ci.ifa_valid = ifa->valid_lft;
2532 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2533 long tval = (jiffies - ifa->tstamp)/HZ;
2534 ci.ifa_prefered -= tval;
2535 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2536 ci.ifa_valid -= tval;
2539 ci.ifa_prefered = INFINITY_LIFE_TIME;
2540 ci.ifa_valid = INFINITY_LIFE_TIME;
2542 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2543 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2544 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2545 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2546 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2547 nlh->nlmsg_len = skb->tail - b;
2552 skb_trim(skb, b - skb->data);
2556 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2557 u32 pid, u32 seq, int event)
2559 struct ifaddrmsg *ifm;
2560 struct nlmsghdr *nlh;
2561 struct ifa_cacheinfo ci;
2562 unsigned char *b = skb->tail;
2564 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2565 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2566 ifm = NLMSG_DATA(nlh);
2567 ifm->ifa_family = AF_INET6;
2568 ifm->ifa_prefixlen = 128;
2569 ifm->ifa_flags = IFA_F_PERMANENT;
2570 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2571 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2572 ifm->ifa_scope = RT_SCOPE_SITE;
2573 ifm->ifa_index = ifmca->idev->dev->ifindex;
2574 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2575 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2576 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2578 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2579 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2581 ci.ifa_prefered = INFINITY_LIFE_TIME;
2582 ci.ifa_valid = INFINITY_LIFE_TIME;
2583 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2584 nlh->nlmsg_len = skb->tail - b;
2589 skb_trim(skb, b - skb->data);
2593 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2594 u32 pid, u32 seq, int event)
2596 struct ifaddrmsg *ifm;
2597 struct nlmsghdr *nlh;
2598 struct ifa_cacheinfo ci;
2599 unsigned char *b = skb->tail;
2601 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2602 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2603 ifm = NLMSG_DATA(nlh);
2604 ifm->ifa_family = AF_INET6;
2605 ifm->ifa_prefixlen = 128;
2606 ifm->ifa_flags = IFA_F_PERMANENT;
2607 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2608 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2609 ifm->ifa_scope = RT_SCOPE_SITE;
2610 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2611 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2612 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2613 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2615 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2616 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2618 ci.ifa_prefered = INFINITY_LIFE_TIME;
2619 ci.ifa_valid = INFINITY_LIFE_TIME;
2620 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2621 nlh->nlmsg_len = skb->tail - b;
2626 skb_trim(skb, b - skb->data);
2637 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2638 enum addr_type_t type)
2641 int s_idx, s_ip_idx;
2643 struct net_device *dev;
2644 struct inet6_dev *idev = NULL;
2645 struct inet6_ifaddr *ifa;
2646 struct ifmcaddr6 *ifmca;
2647 struct ifacaddr6 *ifaca;
2649 s_idx = cb->args[0];
2650 s_ip_idx = ip_idx = cb->args[1];
2651 read_lock(&dev_base_lock);
2653 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
2659 if ((idev = in6_dev_get(dev)) == NULL)
2661 read_lock_bh(&idev->lock);
2664 /* unicast address */
2665 for (ifa = idev->addr_list; ifa;
2666 ifa = ifa->if_next, ip_idx++) {
2667 if (ip_idx < s_ip_idx)
2669 if ((err = inet6_fill_ifaddr(skb, ifa,
2670 NETLINK_CB(cb->skb).pid,
2671 cb->nlh->nlmsg_seq, RTM_NEWADDR)) <= 0)
2675 #ifdef CONFIG_IPV6_PRIVACY
2676 for (ifa = idev->tempaddr_list; ifa;
2677 ifa = ifa->tmp_next, ip_idx++) {
2678 if (ip_idx < s_ip_idx)
2680 if ((err = inet6_fill_ifaddr(skb, ifa,
2681 NETLINK_CB(cb->skb).pid,
2682 cb->nlh->nlmsg_seq, RTM_NEWADDR)) <= 0)
2687 case MULTICAST_ADDR:
2688 /* multicast address */
2689 for (ifmca = idev->mc_list; ifmca;
2690 ifmca = ifmca->next, ip_idx++) {
2691 if (ip_idx < s_ip_idx)
2693 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
2694 NETLINK_CB(cb->skb).pid,
2695 cb->nlh->nlmsg_seq, RTM_GETMULTICAST)) <= 0)
2700 /* anycast address */
2701 for (ifaca = idev->ac_list; ifaca;
2702 ifaca = ifaca->aca_next, ip_idx++) {
2703 if (ip_idx < s_ip_idx)
2705 if ((err = inet6_fill_ifacaddr(skb, ifaca,
2706 NETLINK_CB(cb->skb).pid,
2707 cb->nlh->nlmsg_seq, RTM_GETANYCAST)) <= 0)
2714 read_unlock_bh(&idev->lock);
2719 read_unlock_bh(&idev->lock);
2722 read_unlock(&dev_base_lock);
2724 cb->args[1] = ip_idx;
2728 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
2730 enum addr_type_t type = UNICAST_ADDR;
2731 return inet6_dump_addr(skb, cb, type);
2734 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
2736 enum addr_type_t type = MULTICAST_ADDR;
2737 return inet6_dump_addr(skb, cb, type);
2741 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
2743 enum addr_type_t type = ANYCAST_ADDR;
2744 return inet6_dump_addr(skb, cb, type);
2747 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
2749 struct sk_buff *skb;
2750 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
2752 skb = alloc_skb(size, GFP_ATOMIC);
2754 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, ENOBUFS);
2757 if (inet6_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
2759 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, EINVAL);
2762 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFADDR;
2763 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFADDR, GFP_ATOMIC);
2766 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
2767 __s32 *array, int bytes)
2769 memset(array, 0, bytes);
2770 array[DEVCONF_FORWARDING] = cnf->forwarding;
2771 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
2772 array[DEVCONF_MTU6] = cnf->mtu6;
2773 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
2774 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
2775 array[DEVCONF_AUTOCONF] = cnf->autoconf;
2776 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
2777 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
2778 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
2779 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
2780 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
2781 #ifdef CONFIG_IPV6_PRIVACY
2782 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
2783 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
2784 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
2785 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
2786 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
2788 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
2791 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
2792 u32 pid, u32 seq, int event)
2794 struct net_device *dev = idev->dev;
2795 __s32 *array = NULL;
2796 struct ifinfomsg *r;
2797 struct nlmsghdr *nlh;
2798 unsigned char *b = skb->tail;
2799 struct rtattr *subattr;
2800 __u32 mtu = dev->mtu;
2801 struct ifla_cacheinfo ci;
2803 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*r));
2804 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2805 r = NLMSG_DATA(nlh);
2806 r->ifi_family = AF_INET6;
2807 r->ifi_type = dev->type;
2808 r->ifi_index = dev->ifindex;
2809 r->ifi_flags = dev->flags;
2811 if (!netif_running(dev) || !netif_carrier_ok(dev))
2812 r->ifi_flags &= ~IFF_RUNNING;
2814 r->ifi_flags |= IFF_RUNNING;
2816 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
2819 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
2821 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
2822 if (dev->ifindex != dev->iflink)
2823 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
2825 subattr = (struct rtattr*)skb->tail;
2827 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
2829 /* return the device flags */
2830 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
2832 /* return interface cacheinfo */
2833 ci.max_reasm_len = IPV6_MAXPLEN;
2834 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
2835 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2836 ci.reachable_time = idev->nd_parms->reachable_time;
2837 ci.retrans_time = idev->nd_parms->retrans_time;
2838 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
2840 /* return the device sysctl params */
2841 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
2842 goto rtattr_failure;
2843 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
2844 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
2846 /* XXX - Statistics/MC not implemented */
2847 subattr->rta_len = skb->tail - (u8*)subattr;
2849 nlh->nlmsg_len = skb->tail - b;
2857 skb_trim(skb, b - skb->data);
2861 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2864 int s_idx = cb->args[0];
2865 struct net_device *dev;
2866 struct inet6_dev *idev;
2868 read_lock(&dev_base_lock);
2869 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
2872 if ((idev = in6_dev_get(dev)) == NULL)
2874 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
2875 cb->nlh->nlmsg_seq, RTM_NEWLINK);
2880 read_unlock(&dev_base_lock);
2886 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
2888 struct sk_buff *skb;
2890 int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
2892 skb = alloc_skb(size, GFP_ATOMIC);
2894 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFINFO, ENOBUFS);
2897 if (inet6_fill_ifinfo(skb, idev, 0, 0, event) < 0) {
2899 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFINFO, EINVAL);
2902 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFINFO;
2903 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFINFO, GFP_ATOMIC);
2906 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
2907 struct prefix_info *pinfo, u32 pid, u32 seq, int event)
2909 struct prefixmsg *pmsg;
2910 struct nlmsghdr *nlh;
2911 unsigned char *b = skb->tail;
2912 struct prefix_cacheinfo ci;
2914 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*pmsg));
2917 nlh->nlmsg_flags |= NLM_F_MULTI;
2919 pmsg = NLMSG_DATA(nlh);
2920 pmsg->prefix_family = AF_INET6;
2921 pmsg->prefix_ifindex = idev->dev->ifindex;
2922 pmsg->prefix_len = pinfo->prefix_len;
2923 pmsg->prefix_type = pinfo->type;
2925 pmsg->prefix_flags = 0;
2927 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
2928 if (pinfo->autoconf)
2929 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
2931 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
2933 ci.preferred_time = ntohl(pinfo->prefered);
2934 ci.valid_time = ntohl(pinfo->valid);
2935 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
2937 nlh->nlmsg_len = skb->tail - b;
2942 skb_trim(skb, b - skb->data);
2946 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
2947 struct prefix_info *pinfo)
2949 struct sk_buff *skb;
2950 int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
2952 skb = alloc_skb(size, GFP_ATOMIC);
2954 netlink_set_err(rtnl, 0, RTMGRP_IPV6_PREFIX, ENOBUFS);
2957 if (inet6_fill_prefix(skb, idev, pinfo, 0, 0, event) < 0) {
2959 netlink_set_err(rtnl, 0, RTMGRP_IPV6_PREFIX, EINVAL);
2962 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_PREFIX;
2963 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_PREFIX, GFP_ATOMIC);
2966 static struct rtnetlink_link inet6_rtnetlink_table[RTM_MAX - RTM_BASE + 1] = {
2967 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
2968 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
2969 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
2970 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
2971 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
2972 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
2973 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
2974 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
2975 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
2976 .dumpit = inet6_dump_fib, },
2979 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
2981 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
2985 ip6_rt_addr_add(&ifp->addr, ifp->idev->dev, 0);
2988 addrconf_leave_solict(ifp->idev->dev, &ifp->addr);
2989 if (ifp->idev->cnf.forwarding) {
2990 struct in6_addr addr;
2992 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2993 if (!ipv6_addr_any(&addr))
2994 ipv6_dev_ac_dec(ifp->idev->dev, &addr);
2996 if (!ipv6_chk_addr(&ifp->addr, ifp->idev->dev, 1))
2997 ip6_rt_addr_del(&ifp->addr, ifp->idev->dev);
3002 #ifdef CONFIG_SYSCTL
3005 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3006 void *buffer, size_t *lenp)
3008 int *valp = ctl->data;
3012 ret = proc_dointvec(ctl, write, filp, buffer, lenp);
3014 if (write && *valp != val && valp != &ipv6_devconf_dflt.forwarding) {
3015 struct inet6_dev *idev = NULL;
3017 if (valp != &ipv6_devconf.forwarding) {
3018 idev = (struct inet6_dev *)ctl->extra1;
3022 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3024 addrconf_forward_change(idev);
3027 rt6_purge_dflt_routers(0);
3033 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3034 int *name, int nlen,
3035 void *oldval, size_t *oldlenp,
3036 void *newval, size_t newlen,
3039 int *valp = table->data;
3042 if (!newval || !newlen)
3044 if (newlen != sizeof(int))
3046 if (get_user(new, (int *)newval))
3050 if (oldval && oldlenp) {
3052 if (get_user(len, oldlenp))
3055 if (len > table->maxlen)
3056 len = table->maxlen;
3057 if (copy_to_user(oldval, valp, len))
3059 if (put_user(len, oldlenp))
3064 if (valp != &ipv6_devconf_dflt.forwarding) {
3065 struct inet6_dev *idev;
3066 if (valp != &ipv6_devconf.forwarding) {
3067 idev = (struct inet6_dev *)table->extra1;
3068 if (unlikely(idev == NULL))
3073 addrconf_forward_change(idev);
3076 rt6_purge_dflt_routers(0);
3083 static struct addrconf_sysctl_table
3085 struct ctl_table_header *sysctl_header;
3086 ctl_table addrconf_vars[18];
3087 ctl_table addrconf_dev[2];
3088 ctl_table addrconf_conf_dir[2];
3089 ctl_table addrconf_proto_dir[2];
3090 ctl_table addrconf_root_dir[2];
3091 } addrconf_sysctl = {
3092 .sysctl_header = NULL,
3095 .ctl_name = NET_IPV6_FORWARDING,
3096 .procname = "forwarding",
3097 .data = &ipv6_devconf.forwarding,
3098 .maxlen = sizeof(int),
3100 .proc_handler = &addrconf_sysctl_forward,
3101 .strategy = &addrconf_sysctl_forward_strategy,
3104 .ctl_name = NET_IPV6_HOP_LIMIT,
3105 .procname = "hop_limit",
3106 .data = &ipv6_devconf.hop_limit,
3107 .maxlen = sizeof(int),
3109 .proc_handler = proc_dointvec,
3112 .ctl_name = NET_IPV6_MTU,
3114 .data = &ipv6_devconf.mtu6,
3115 .maxlen = sizeof(int),
3117 .proc_handler = &proc_dointvec,
3120 .ctl_name = NET_IPV6_ACCEPT_RA,
3121 .procname = "accept_ra",
3122 .data = &ipv6_devconf.accept_ra,
3123 .maxlen = sizeof(int),
3125 .proc_handler = &proc_dointvec,
3128 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3129 .procname = "accept_redirects",
3130 .data = &ipv6_devconf.accept_redirects,
3131 .maxlen = sizeof(int),
3133 .proc_handler = &proc_dointvec,
3136 .ctl_name = NET_IPV6_AUTOCONF,
3137 .procname = "autoconf",
3138 .data = &ipv6_devconf.autoconf,
3139 .maxlen = sizeof(int),
3141 .proc_handler = &proc_dointvec,
3144 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3145 .procname = "dad_transmits",
3146 .data = &ipv6_devconf.dad_transmits,
3147 .maxlen = sizeof(int),
3149 .proc_handler = &proc_dointvec,
3152 .ctl_name = NET_IPV6_RTR_SOLICITS,
3153 .procname = "router_solicitations",
3154 .data = &ipv6_devconf.rtr_solicits,
3155 .maxlen = sizeof(int),
3157 .proc_handler = &proc_dointvec,
3160 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3161 .procname = "router_solicitation_interval",
3162 .data = &ipv6_devconf.rtr_solicit_interval,
3163 .maxlen = sizeof(int),
3165 .proc_handler = &proc_dointvec_jiffies,
3166 .strategy = &sysctl_jiffies,
3169 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3170 .procname = "router_solicitation_delay",
3171 .data = &ipv6_devconf.rtr_solicit_delay,
3172 .maxlen = sizeof(int),
3174 .proc_handler = &proc_dointvec_jiffies,
3175 .strategy = &sysctl_jiffies,
3178 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3179 .procname = "force_mld_version",
3180 .data = &ipv6_devconf.force_mld_version,
3181 .maxlen = sizeof(int),
3183 .proc_handler = &proc_dointvec,
3185 #ifdef CONFIG_IPV6_PRIVACY
3187 .ctl_name = NET_IPV6_USE_TEMPADDR,
3188 .procname = "use_tempaddr",
3189 .data = &ipv6_devconf.use_tempaddr,
3190 .maxlen = sizeof(int),
3192 .proc_handler = &proc_dointvec,
3195 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3196 .procname = "temp_valid_lft",
3197 .data = &ipv6_devconf.temp_valid_lft,
3198 .maxlen = sizeof(int),
3200 .proc_handler = &proc_dointvec,
3203 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3204 .procname = "temp_prefered_lft",
3205 .data = &ipv6_devconf.temp_prefered_lft,
3206 .maxlen = sizeof(int),
3208 .proc_handler = &proc_dointvec,
3211 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3212 .procname = "regen_max_retry",
3213 .data = &ipv6_devconf.regen_max_retry,
3214 .maxlen = sizeof(int),
3216 .proc_handler = &proc_dointvec,
3219 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3220 .procname = "max_desync_factor",
3221 .data = &ipv6_devconf.max_desync_factor,
3222 .maxlen = sizeof(int),
3224 .proc_handler = &proc_dointvec,
3228 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3229 .procname = "max_addresses",
3230 .data = &ipv6_devconf.max_addresses,
3231 .maxlen = sizeof(int),
3233 .proc_handler = &proc_dointvec,
3236 .ctl_name = 0, /* sentinel */
3241 .ctl_name = NET_PROTO_CONF_ALL,
3244 .child = addrconf_sysctl.addrconf_vars,
3247 .ctl_name = 0, /* sentinel */
3250 .addrconf_conf_dir = {
3252 .ctl_name = NET_IPV6_CONF,
3255 .child = addrconf_sysctl.addrconf_dev,
3258 .ctl_name = 0, /* sentinel */
3261 .addrconf_proto_dir = {
3263 .ctl_name = NET_IPV6,
3266 .child = addrconf_sysctl.addrconf_conf_dir,
3269 .ctl_name = 0, /* sentinel */
3272 .addrconf_root_dir = {
3274 .ctl_name = CTL_NET,
3277 .child = addrconf_sysctl.addrconf_proto_dir,
3280 .ctl_name = 0, /* sentinel */
3285 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3288 struct net_device *dev = idev ? idev->dev : NULL;
3289 struct addrconf_sysctl_table *t;
3290 char *dev_name = NULL;
3292 t = kmalloc(sizeof(*t), GFP_KERNEL);
3295 memcpy(t, &addrconf_sysctl, sizeof(*t));
3296 for (i=0; t->addrconf_vars[i].data; i++) {
3297 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3298 t->addrconf_vars[i].de = NULL;
3299 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3302 dev_name = dev->name;
3303 t->addrconf_dev[0].ctl_name = dev->ifindex;
3305 dev_name = "default";
3306 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3310 * Make a copy of dev_name, because '.procname' is regarded as const
3311 * by sysctl and we wouldn't want anyone to change it under our feet
3312 * (see SIOCSIFNAME).
3314 dev_name = net_sysctl_strdup(dev_name);
3318 t->addrconf_dev[0].procname = dev_name;
3320 t->addrconf_dev[0].child = t->addrconf_vars;
3321 t->addrconf_dev[0].de = NULL;
3322 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3323 t->addrconf_conf_dir[0].de = NULL;
3324 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3325 t->addrconf_proto_dir[0].de = NULL;
3326 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3327 t->addrconf_root_dir[0].de = NULL;
3329 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3330 if (t->sysctl_header == NULL)
3345 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3348 struct addrconf_sysctl_table *t = p->sysctl;
3350 unregister_sysctl_table(t->sysctl_header);
3351 kfree(t->addrconf_dev[0].procname);
3363 int register_inet6addr_notifier(struct notifier_block *nb)
3365 return notifier_chain_register(&inet6addr_chain, nb);
3368 int unregister_inet6addr_notifier(struct notifier_block *nb)
3370 return notifier_chain_unregister(&inet6addr_chain,nb);
3374 * Init / cleanup code
3377 void __init addrconf_init(void)
3379 register_netdevice_notifier(&ipv6_dev_notf);
3381 #ifdef CONFIG_IPV6_PRIVACY
3382 md5_tfm = crypto_alloc_tfm("md5", 0);
3383 if (unlikely(md5_tfm == NULL))
3385 "failed to load transform for md5\n");
3389 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3390 #ifdef CONFIG_SYSCTL
3391 addrconf_sysctl.sysctl_header =
3392 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3393 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3397 void __exit addrconf_cleanup(void)
3399 struct net_device *dev;
3400 struct inet6_dev *idev;
3401 struct inet6_ifaddr *ifa;
3404 unregister_netdevice_notifier(&ipv6_dev_notf);
3406 rtnetlink_links[PF_INET6] = NULL;
3407 #ifdef CONFIG_SYSCTL
3408 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3409 addrconf_sysctl_unregister(&ipv6_devconf);
3418 for (dev=dev_base; dev; dev=dev->next) {
3419 if ((idev = __in6_dev_get(dev)) == NULL)
3421 addrconf_ifdown(dev, 1);
3428 write_lock_bh(&addrconf_hash_lock);
3429 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3430 for (ifa=inet6_addr_lst[i]; ifa; ) {
3431 struct inet6_ifaddr *bifa;
3434 ifa = ifa->lst_next;
3435 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3436 /* Do not free it; something is wrong.
3437 Now we can investigate it with debugger.
3441 write_unlock_bh(&addrconf_hash_lock);
3443 del_timer(&addr_chk_timer);
3447 #ifdef CONFIG_IPV6_PRIVACY
3448 if (likely(md5_tfm != NULL)) {
3449 crypto_free_tfm(md5_tfm);
3454 #ifdef CONFIG_PROC_FS
3455 proc_net_remove("if_inet6");