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);
142 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
143 struct prefix_info *pinfo);
144 static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
146 static struct notifier_block *inet6addr_chain;
148 struct ipv6_devconf ipv6_devconf = {
150 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
151 .mtu6 = IPV6_MIN_MTU,
153 .accept_redirects = 1,
155 .force_mld_version = 0,
157 .rtr_solicits = MAX_RTR_SOLICITATIONS,
158 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
159 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
160 #ifdef CONFIG_IPV6_PRIVACY
162 .temp_valid_lft = TEMP_VALID_LIFETIME,
163 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
164 .regen_max_retry = REGEN_MAX_RETRY,
165 .max_desync_factor = MAX_DESYNC_FACTOR,
167 .max_addresses = IPV6_MAX_ADDRESSES,
170 static struct ipv6_devconf ipv6_devconf_dflt = {
172 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
173 .mtu6 = IPV6_MIN_MTU,
175 .accept_redirects = 1,
178 .rtr_solicits = MAX_RTR_SOLICITATIONS,
179 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
180 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
181 #ifdef CONFIG_IPV6_PRIVACY
183 .temp_valid_lft = TEMP_VALID_LIFETIME,
184 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
185 .regen_max_retry = REGEN_MAX_RETRY,
186 .max_desync_factor = MAX_DESYNC_FACTOR,
188 .max_addresses = IPV6_MAX_ADDRESSES,
191 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
192 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
193 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
195 int ipv6_addr_type(const struct in6_addr *addr)
200 st = addr->s6_addr32[0];
202 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
203 type = IPV6_ADDR_MULTICAST;
205 switch((st & htonl(0x00FF0000))) {
206 case __constant_htonl(0x00010000):
207 type |= IPV6_ADDR_LOOPBACK;
210 case __constant_htonl(0x00020000):
211 type |= IPV6_ADDR_LINKLOCAL;
214 case __constant_htonl(0x00050000):
215 type |= IPV6_ADDR_SITELOCAL;
221 type = IPV6_ADDR_UNICAST;
223 /* Consider all addresses with the first three bits different of
224 000 and 111 as finished.
226 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
227 (st & htonl(0xE0000000)) != htonl(0xE0000000))
230 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
231 return (IPV6_ADDR_LINKLOCAL | type);
233 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
234 return (IPV6_ADDR_SITELOCAL | type);
236 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
237 if (addr->s6_addr32[2] == 0) {
238 if (addr->s6_addr32[3] == 0)
239 return IPV6_ADDR_ANY;
241 if (addr->s6_addr32[3] == htonl(0x00000001))
242 return (IPV6_ADDR_LOOPBACK | type);
244 return (IPV6_ADDR_COMPATv4 | type);
247 if (addr->s6_addr32[2] == htonl(0x0000ffff))
248 return IPV6_ADDR_MAPPED;
251 st &= htonl(0xFF000000);
253 return IPV6_ADDR_RESERVED;
254 st &= htonl(0xFE000000);
255 if (st == htonl(0x02000000))
256 return IPV6_ADDR_RESERVED; /* for NSAP */
257 if (st == htonl(0x04000000))
258 return IPV6_ADDR_RESERVED; /* for IPX */
262 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
264 if (del_timer(&ifp->timer))
268 enum addrconf_timer_t
275 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
276 enum addrconf_timer_t what,
279 if (!del_timer(&ifp->timer))
284 ifp->timer.function = addrconf_dad_timer;
287 ifp->timer.function = addrconf_rs_timer;
291 ifp->timer.expires = jiffies + when;
292 add_timer(&ifp->timer);
295 /* Nobody refers to this device, we may destroy it. */
297 void in6_dev_finish_destroy(struct inet6_dev *idev)
299 struct net_device *dev = idev->dev;
300 BUG_TRAP(idev->addr_list==NULL);
301 BUG_TRAP(idev->mc_list==NULL);
302 #ifdef NET_REFCNT_DEBUG
303 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
307 printk("Freeing alive inet6 device %p\n", idev);
310 snmp6_unregister_dev(idev);
315 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
317 struct inet6_dev *ndev;
321 if (dev->mtu < IPV6_MIN_MTU)
324 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
327 memset(ndev, 0, sizeof(struct inet6_dev));
329 ndev->lock = RW_LOCK_UNLOCKED;
331 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
332 ndev->cnf.mtu6 = dev->mtu;
333 ndev->cnf.sysctl = NULL;
334 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
335 if (ndev->nd_parms == NULL) {
340 /* We refer to the device */
343 if (snmp6_register_dev(ndev) < 0) {
345 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
346 __FUNCTION__, dev->name));
347 neigh_parms_release(&nd_tbl, ndev->nd_parms);
349 in6_dev_finish_destroy(ndev);
353 /* One reference from device. We must do this before
354 * we invoke __ipv6_regen_rndid().
358 #ifdef CONFIG_IPV6_PRIVACY
359 get_random_bytes(ndev->rndid, sizeof(ndev->rndid));
360 get_random_bytes(ndev->entropy, sizeof(ndev->entropy));
361 init_timer(&ndev->regen_timer);
362 ndev->regen_timer.function = ipv6_regen_rndid;
363 ndev->regen_timer.data = (unsigned long) ndev;
364 if ((dev->flags&IFF_LOOPBACK) ||
365 dev->type == ARPHRD_TUNNEL ||
366 dev->type == ARPHRD_SIT) {
368 "Disabled Privacy Extensions on device %p(%s)\n",
370 ndev->cnf.use_tempaddr = -1;
373 ipv6_regen_rndid((unsigned long) ndev);
377 write_lock_bh(&addrconf_lock);
379 write_unlock_bh(&addrconf_lock);
381 ipv6_mc_init_dev(ndev);
382 ndev->tstamp = jiffies;
384 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
385 NET_IPV6_NEIGH, "ipv6", &ndisc_ifinfo_sysctl_change);
386 addrconf_sysctl_register(ndev, &ndev->cnf);
392 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
394 struct inet6_dev *idev;
398 if ((idev = __in6_dev_get(dev)) == NULL) {
399 if ((idev = ipv6_add_dev(dev)) == NULL)
402 if (dev->flags&IFF_UP)
407 static void dev_forward_change(struct inet6_dev *idev)
409 struct net_device *dev;
410 struct inet6_ifaddr *ifa;
411 struct in6_addr addr;
416 if (dev && (dev->flags & IFF_MULTICAST)) {
417 ipv6_addr_all_routers(&addr);
419 if (idev->cnf.forwarding)
420 ipv6_dev_mc_inc(dev, &addr);
422 ipv6_dev_mc_dec(dev, &addr);
424 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
425 if (idev->cnf.forwarding)
426 addrconf_join_anycast(ifa);
428 addrconf_leave_anycast(ifa);
433 static void addrconf_forward_change(void)
435 struct net_device *dev;
436 struct inet6_dev *idev;
438 read_lock(&dev_base_lock);
439 for (dev=dev_base; dev; dev=dev->next) {
440 read_lock(&addrconf_lock);
441 idev = __in6_dev_get(dev);
443 int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
444 idev->cnf.forwarding = ipv6_devconf.forwarding;
446 dev_forward_change(idev);
448 read_unlock(&addrconf_lock);
450 read_unlock(&dev_base_lock);
454 /* Nobody refers to this ifaddr, destroy it */
456 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
458 BUG_TRAP(ifp->if_next==NULL);
459 BUG_TRAP(ifp->lst_next==NULL);
460 #ifdef NET_REFCNT_DEBUG
461 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
464 in6_dev_put(ifp->idev);
466 if (del_timer(&ifp->timer))
467 printk("Timer is still running, when freeing ifa=%p\n", ifp);
470 printk("Freeing alive inet6 address %p\n", ifp);
473 dst_release(&ifp->rt->u.dst);
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 = NULL;
488 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
493 /* Ignore adding duplicate addresses on an interface */
494 if (ipv6_chk_same_addr(addr, idev->dev)) {
495 ADBG(("ipv6_add_addr: already assigned\n"));
500 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
503 ADBG(("ipv6_add_addr: malloc failed\n"));
508 rt = addrconf_dst_alloc(idev, addr, 0);
514 memset(ifa, 0, sizeof(struct inet6_ifaddr));
515 ipv6_addr_copy(&ifa->addr, addr);
517 spin_lock_init(&ifa->lock);
518 init_timer(&ifa->timer);
519 ifa->timer.data = (unsigned long) ifa;
521 ifa->prefix_len = pfxlen;
522 ifa->flags = flags | IFA_F_TENTATIVE;
523 ifa->cstamp = ifa->tstamp = jiffies;
525 read_lock(&addrconf_lock);
527 read_unlock(&addrconf_lock);
528 err = -ENODEV; /*XXX*/
538 /* Add to big hash table */
539 hash = ipv6_addr_hash(addr);
541 write_lock_bh(&addrconf_hash_lock);
542 ifa->lst_next = inet6_addr_lst[hash];
543 inet6_addr_lst[hash] = ifa;
545 write_unlock_bh(&addrconf_hash_lock);
547 write_lock_bh(&idev->lock);
548 /* Add to inet6_dev unicast addr list. */
549 ifa->if_next = idev->addr_list;
550 idev->addr_list = ifa;
552 #ifdef CONFIG_IPV6_PRIVACY
553 ifa->regen_count = 0;
554 if (ifa->flags&IFA_F_TEMPORARY) {
555 ifa->tmp_next = idev->tempaddr_list;
556 idev->tempaddr_list = ifa;
559 ifa->tmp_next = NULL;
566 write_unlock_bh(&idev->lock);
567 read_unlock(&addrconf_lock);
569 spin_unlock_bh(&lock);
571 if (unlikely(err == 0))
572 notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
581 /* This function wants to get referenced ifp and releases it before return */
583 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
585 struct inet6_ifaddr *ifa, **ifap;
586 struct inet6_dev *idev = ifp->idev;
589 hash = ipv6_addr_hash(&ifp->addr);
593 write_lock_bh(&addrconf_hash_lock);
594 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
595 ifap = &ifa->lst_next) {
597 *ifap = ifa->lst_next;
599 ifa->lst_next = NULL;
603 write_unlock_bh(&addrconf_hash_lock);
605 write_lock_bh(&idev->lock);
606 #ifdef CONFIG_IPV6_PRIVACY
607 if (ifp->flags&IFA_F_TEMPORARY) {
608 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
609 ifap = &ifa->tmp_next) {
611 *ifap = ifa->tmp_next;
613 in6_ifa_put(ifp->ifpub);
617 ifa->tmp_next = NULL;
624 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;
625 ifap = &ifa->if_next) {
627 *ifap = ifa->if_next;
633 write_unlock_bh(&idev->lock);
635 ipv6_ifa_notify(RTM_DELADDR, ifp);
637 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
639 addrconf_del_timer(ifp);
644 #ifdef CONFIG_IPV6_PRIVACY
645 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
647 struct inet6_dev *idev;
648 struct in6_addr addr, *tmpaddr;
649 unsigned long tmp_prefered_lft, tmp_valid_lft;
655 spin_lock_bh(&ift->lock);
656 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
657 spin_unlock_bh(&ift->lock);
663 spin_lock_bh(&ifp->lock);
667 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
668 write_lock(&idev->lock);
669 if (idev->cnf.use_tempaddr <= 0) {
670 write_unlock(&idev->lock);
671 spin_unlock_bh(&ifp->lock);
673 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
679 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
680 idev->cnf.use_tempaddr = -1; /*XXX*/
681 write_unlock(&idev->lock);
682 spin_unlock_bh(&ifp->lock);
684 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
690 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
691 write_unlock(&idev->lock);
692 spin_unlock_bh(&ifp->lock);
694 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
700 memcpy(&addr.s6_addr[8], idev->rndid, 8);
701 tmp_valid_lft = min_t(__u32,
703 idev->cnf.temp_valid_lft);
704 tmp_prefered_lft = min_t(__u32,
706 idev->cnf.temp_prefered_lft - desync_factor / HZ);
707 tmp_plen = ifp->prefix_len;
708 max_addresses = idev->cnf.max_addresses;
709 write_unlock(&idev->lock);
710 spin_unlock_bh(&ifp->lock);
711 ift = !max_addresses ||
712 ipv6_count_addresses(idev) < max_addresses ?
713 ipv6_add_addr(idev, &addr, tmp_plen,
714 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
715 if (!ift || IS_ERR(ift)) {
719 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
723 spin_lock_bh(&ift->lock);
725 ift->valid_lft = tmp_valid_lft;
726 ift->prefered_lft = tmp_prefered_lft;
727 ift->cstamp = ifp->cstamp;
728 ift->tstamp = ifp->tstamp;
729 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, int onlink)
765 struct inet6_ifaddr *ifp = NULL;
766 struct inet6_ifaddr *match = NULL;
767 struct inet6_dev *idev;
770 int hiscore = -1, score;
773 scope = ipv6_addr_scope(daddr);
779 * search dev and walk through dev addresses
783 if (dev->flags & IFF_LOOPBACK)
786 read_lock(&addrconf_lock);
787 idev = __in6_dev_get(dev);
789 read_lock_bh(&idev->lock);
790 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
791 if (ifp->scope == scope) {
792 if (ifp->flags&IFA_F_TENTATIVE)
794 #ifdef CONFIG_IPV6_PRIVACY
795 score = ipv6_saddr_pref(ifp, idev->cnf.use_tempaddr > 1 ? IFA_F_TEMPORARY : 0);
797 score = ipv6_saddr_pref(ifp, 0);
799 if (score <= hiscore)
808 if (IPV6_GET_SADDR_MAXSCORE(score)) {
809 read_unlock_bh(&idev->lock);
810 read_unlock(&addrconf_lock);
815 read_unlock_bh(&idev->lock);
817 read_unlock(&addrconf_lock);
820 if (scope == IFA_LINK)
824 * dev == NULL or search failed for specified dev
827 read_lock(&dev_base_lock);
828 read_lock(&addrconf_lock);
829 for (dev = dev_base; dev; dev=dev->next) {
830 idev = __in6_dev_get(dev);
832 read_lock_bh(&idev->lock);
833 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
834 if (ifp->scope == scope) {
835 if (ifp->flags&IFA_F_TENTATIVE)
837 #ifdef CONFIG_IPV6_PRIVACY
838 score = ipv6_saddr_pref(ifp, idev->cnf.use_tempaddr > 1 ? IFA_F_TEMPORARY : 0);
840 score = ipv6_saddr_pref(ifp, 0);
842 if (score <= hiscore)
851 if (IPV6_GET_SADDR_MAXSCORE(score)) {
852 read_unlock_bh(&idev->lock);
853 goto out_unlock_base;
857 read_unlock_bh(&idev->lock);
862 read_unlock(&addrconf_lock);
863 read_unlock(&dev_base_lock);
866 err = -EADDRNOTAVAIL;
868 ipv6_addr_copy(saddr, &match->addr);
877 int ipv6_get_saddr(struct dst_entry *dst,
878 struct in6_addr *daddr, struct in6_addr *saddr)
881 struct net_device *dev = NULL;
884 rt = (struct rt6_info *) dst;
888 onlink = (rt && (rt->rt6i_flags & RTF_ALLONLINK));
890 return ipv6_dev_get_saddr(dev, daddr, saddr, onlink);
894 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
896 struct inet6_dev *idev;
897 int err = -EADDRNOTAVAIL;
899 read_lock(&addrconf_lock);
900 if ((idev = __in6_dev_get(dev)) != NULL) {
901 struct inet6_ifaddr *ifp;
903 read_lock_bh(&idev->lock);
904 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
905 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
906 ipv6_addr_copy(addr, &ifp->addr);
911 read_unlock_bh(&idev->lock);
913 read_unlock(&addrconf_lock);
917 static int ipv6_count_addresses(struct inet6_dev *idev)
920 struct inet6_ifaddr *ifp;
922 read_lock_bh(&idev->lock);
923 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
925 read_unlock_bh(&idev->lock);
929 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
931 struct inet6_ifaddr * ifp;
932 u8 hash = ipv6_addr_hash(addr);
934 read_lock_bh(&addrconf_hash_lock);
935 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
936 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
937 !(ifp->flags&IFA_F_TENTATIVE)) {
938 if (dev == NULL || ifp->idev->dev == dev ||
939 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
943 read_unlock_bh(&addrconf_hash_lock);
948 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
950 struct inet6_ifaddr * ifp;
951 u8 hash = ipv6_addr_hash(addr);
953 read_lock_bh(&addrconf_hash_lock);
954 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
955 if (ipv6_addr_cmp(&ifp->addr, addr) == 0) {
956 if (dev == NULL || ifp->idev->dev == dev)
960 read_unlock_bh(&addrconf_hash_lock);
964 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
966 struct inet6_ifaddr * ifp;
967 u8 hash = ipv6_addr_hash(addr);
969 read_lock_bh(&addrconf_hash_lock);
970 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
971 if (ipv6_addr_cmp(&ifp->addr, addr) == 0) {
972 if (dev == NULL || ifp->idev->dev == dev ||
973 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
979 read_unlock_bh(&addrconf_hash_lock);
984 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
986 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
987 const struct in6_addr *sk2_rcv_saddr6 = tcp_v6_rcv_saddr(sk2);
988 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
989 u32 sk2_rcv_saddr = tcp_v4_rcv_saddr(sk2);
990 int sk_ipv6only = ipv6_only_sock(sk);
991 int sk2_ipv6only = tcp_v6_ipv6only(sk2);
992 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
993 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
995 if (!sk2_rcv_saddr && !sk_ipv6only)
998 if (addr_type2 == IPV6_ADDR_ANY &&
999 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
1002 if (addr_type == IPV6_ADDR_ANY &&
1003 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
1006 if (sk2_rcv_saddr6 &&
1007 !ipv6_addr_cmp(sk_rcv_saddr6, sk2_rcv_saddr6))
1010 if (addr_type == IPV6_ADDR_MAPPED &&
1012 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1018 /* Gets referenced address, destroys ifaddr */
1020 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1022 if (net_ratelimit())
1023 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1024 if (ifp->flags&IFA_F_PERMANENT) {
1025 spin_lock_bh(&ifp->lock);
1026 addrconf_del_timer(ifp);
1027 ifp->flags |= IFA_F_TENTATIVE;
1028 spin_unlock_bh(&ifp->lock);
1030 #ifdef CONFIG_IPV6_PRIVACY
1031 } else if (ifp->flags&IFA_F_TEMPORARY) {
1032 struct inet6_ifaddr *ifpub;
1033 spin_lock_bh(&ifp->lock);
1036 in6_ifa_hold(ifpub);
1037 spin_unlock_bh(&ifp->lock);
1038 ipv6_create_tempaddr(ifpub, ifp);
1041 spin_unlock_bh(&ifp->lock);
1050 /* Join to solicited addr multicast group. */
1052 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1054 struct in6_addr maddr;
1056 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1059 addrconf_addr_solict_mult(addr, &maddr);
1060 ipv6_dev_mc_inc(dev, &maddr);
1063 void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
1065 struct in6_addr maddr;
1067 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1070 addrconf_addr_solict_mult(addr, &maddr);
1071 __ipv6_dev_mc_dec(idev, &maddr);
1074 void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1076 struct in6_addr addr;
1077 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1078 if (ipv6_addr_any(&addr))
1080 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1083 void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1085 struct in6_addr addr;
1086 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1087 if (ipv6_addr_any(&addr))
1089 __ipv6_dev_ac_dec(ifp->idev, &addr);
1092 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1094 switch (dev->type) {
1097 case ARPHRD_IEEE802_TR:
1098 if (dev->addr_len != ETH_ALEN)
1100 memcpy(eui, dev->dev_addr, 3);
1101 memcpy(eui + 5, dev->dev_addr+3, 3);
1107 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1108 if (dev->addr_len != ARCNET_ALEN)
1111 eui[7] = *(u8*)dev->dev_addr;
1117 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1120 struct inet6_ifaddr *ifp;
1122 read_lock_bh(&idev->lock);
1123 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1124 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1125 memcpy(eui, ifp->addr.s6_addr+8, 8);
1130 read_unlock_bh(&idev->lock);
1134 #ifdef CONFIG_IPV6_PRIVACY
1135 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1136 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1138 struct net_device *dev;
1139 struct scatterlist sg[2];
1141 sg[0].page = virt_to_page(idev->entropy);
1142 sg[0].offset = offset_in_page(idev->entropy);
1144 sg[1].page = virt_to_page(idev->work_eui64);
1145 sg[1].offset = offset_in_page(idev->work_eui64);
1150 if (ipv6_generate_eui64(idev->work_eui64, dev)) {
1152 "__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
1154 get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
1157 spin_lock(&md5_tfm_lock);
1158 if (unlikely(md5_tfm == NULL)) {
1159 spin_unlock(&md5_tfm_lock);
1162 crypto_digest_init(md5_tfm);
1163 crypto_digest_update(md5_tfm, sg, 2);
1164 crypto_digest_final(md5_tfm, idev->work_digest);
1165 spin_unlock(&md5_tfm_lock);
1167 memcpy(idev->rndid, &idev->work_digest[0], 8);
1168 idev->rndid[0] &= ~0x02;
1169 memcpy(idev->entropy, &idev->work_digest[8], 8);
1172 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1173 * check if generated address is not inappropriate
1175 * - Reserved subnet anycast (RFC 2526)
1176 * 11111101 11....11 1xxxxxxx
1177 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1178 * 00-00-5E-FE-xx-xx-xx-xx
1180 * - XXX: already assigned to an address on the device
1182 if (idev->rndid[0] == 0xfd &&
1183 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1184 (idev->rndid[7]&0x80))
1186 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1187 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1189 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1196 static void ipv6_regen_rndid(unsigned long data)
1198 struct inet6_dev *idev = (struct inet6_dev *) data;
1199 unsigned long expires;
1201 read_lock_bh(&addrconf_lock);
1202 write_lock_bh(&idev->lock);
1207 if (__ipv6_regen_rndid(idev) < 0)
1211 idev->cnf.temp_prefered_lft * HZ -
1212 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1213 if (time_before(expires, jiffies)) {
1215 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1220 if (!mod_timer(&idev->regen_timer, expires))
1224 write_unlock_bh(&idev->lock);
1225 read_unlock_bh(&addrconf_lock);
1229 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1232 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1233 ret = __ipv6_regen_rndid(idev);
1243 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1244 unsigned long expires, unsigned flags)
1246 struct in6_rtmsg rtmsg;
1248 memset(&rtmsg, 0, sizeof(rtmsg));
1249 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1250 rtmsg.rtmsg_dst_len = plen;
1251 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1252 rtmsg.rtmsg_ifindex = dev->ifindex;
1253 rtmsg.rtmsg_info = expires;
1254 rtmsg.rtmsg_flags = RTF_UP|flags;
1255 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1257 /* Prevent useless cloning on PtP SIT.
1258 This thing is done here expecting that the whole
1259 class of non-broadcast devices need not cloning.
1261 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1262 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1264 ip6_route_add(&rtmsg, NULL, NULL);
1267 /* Create "default" multicast route to the interface */
1269 static void addrconf_add_mroute(struct net_device *dev)
1271 struct in6_rtmsg rtmsg;
1273 memset(&rtmsg, 0, sizeof(rtmsg));
1274 ipv6_addr_set(&rtmsg.rtmsg_dst,
1275 htonl(0xFF000000), 0, 0, 0);
1276 rtmsg.rtmsg_dst_len = 8;
1277 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1278 rtmsg.rtmsg_ifindex = dev->ifindex;
1279 rtmsg.rtmsg_flags = RTF_UP;
1280 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1281 ip6_route_add(&rtmsg, NULL, NULL);
1284 static void sit_route_add(struct net_device *dev)
1286 struct in6_rtmsg rtmsg;
1288 memset(&rtmsg, 0, sizeof(rtmsg));
1290 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1291 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1293 /* prefix length - 96 bits "::d.d.d.d" */
1294 rtmsg.rtmsg_dst_len = 96;
1295 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1296 rtmsg.rtmsg_ifindex = dev->ifindex;
1298 ip6_route_add(&rtmsg, NULL, NULL);
1301 static void addrconf_add_lroute(struct net_device *dev)
1303 struct in6_addr addr;
1305 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1306 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1309 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1311 struct inet6_dev *idev;
1315 if ((idev = ipv6_find_idev(dev)) == NULL)
1318 /* Add default multicast route */
1319 addrconf_add_mroute(dev);
1321 /* Add link local route */
1322 addrconf_add_lroute(dev);
1326 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1328 struct prefix_info *pinfo;
1332 unsigned long rt_expires;
1333 struct inet6_dev *in6_dev;
1335 pinfo = (struct prefix_info *) opt;
1337 if (len < sizeof(struct prefix_info)) {
1338 ADBG(("addrconf: prefix option too short\n"));
1343 * Validation checks ([ADDRCONF], page 19)
1346 addr_type = ipv6_addr_type(&pinfo->prefix);
1348 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1351 valid_lft = ntohl(pinfo->valid);
1352 prefered_lft = ntohl(pinfo->prefered);
1354 if (prefered_lft > valid_lft) {
1355 if (net_ratelimit())
1356 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1360 in6_dev = in6_dev_get(dev);
1362 if (in6_dev == NULL) {
1363 if (net_ratelimit())
1364 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1369 * Two things going on here:
1370 * 1) Add routes for on-link prefixes
1371 * 2) Configure prefixes with the auto flag set
1374 /* Avoid arithmetic overflow. Really, we could
1375 save rt_expires in seconds, likely valid_lft,
1376 but it would require division in fib gc, that it
1379 if (valid_lft >= 0x7FFFFFFF/HZ)
1382 rt_expires = jiffies + valid_lft * HZ;
1384 if (pinfo->onlink) {
1385 struct rt6_info *rt;
1386 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1388 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1389 if (rt->rt6i_flags&RTF_EXPIRES) {
1390 if (valid_lft == 0) {
1391 ip6_del_rt(rt, NULL, NULL);
1394 rt->rt6i_expires = rt_expires;
1397 } else if (valid_lft) {
1398 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1399 dev, rt_expires, RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1402 dst_release(&rt->u.dst);
1405 /* Try to figure out our local address for this prefix */
1407 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1408 struct inet6_ifaddr * ifp;
1409 struct in6_addr addr;
1410 int create = 0, update_lft = 0;
1412 if (pinfo->prefix_len == 64) {
1413 memcpy(&addr, &pinfo->prefix, 8);
1414 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1415 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1416 in6_dev_put(in6_dev);
1421 if (net_ratelimit())
1422 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1424 in6_dev_put(in6_dev);
1429 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1431 if (ifp == NULL && valid_lft) {
1432 int max_addresses = in6_dev->cnf.max_addresses;
1434 /* Do not allow to create too much of autoconfigured
1435 * addresses; this would be too easy way to crash kernel.
1437 if (!max_addresses ||
1438 ipv6_count_addresses(in6_dev) < max_addresses)
1439 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1440 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1442 if (!ifp || IS_ERR(ifp)) {
1443 in6_dev_put(in6_dev);
1447 update_lft = create = 1;
1448 ifp->cstamp = jiffies;
1449 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1455 #ifdef CONFIG_IPV6_PRIVACY
1456 struct inet6_ifaddr *ift;
1460 /* update lifetime (RFC2462 5.5.3 e) */
1461 spin_lock(&ifp->lock);
1463 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1464 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1467 if (!update_lft && stored_lft) {
1468 if (valid_lft > MIN_VALID_LIFETIME ||
1469 valid_lft > stored_lft)
1471 else if (stored_lft <= MIN_VALID_LIFETIME) {
1472 /* valid_lft <= stored_lft is always true */
1476 valid_lft = MIN_VALID_LIFETIME;
1477 if (valid_lft < prefered_lft)
1478 prefered_lft = valid_lft;
1484 ifp->valid_lft = valid_lft;
1485 ifp->prefered_lft = prefered_lft;
1488 ifp->flags &= ~IFA_F_DEPRECATED;
1489 spin_unlock(&ifp->lock);
1491 if (!(flags&IFA_F_TENTATIVE))
1492 ipv6_ifa_notify(0, ifp);
1494 spin_unlock(&ifp->lock);
1496 #ifdef CONFIG_IPV6_PRIVACY
1497 read_lock_bh(&in6_dev->lock);
1498 /* update all temporary addresses in the list */
1499 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1501 * When adjusting the lifetimes of an existing
1502 * temporary address, only lower the lifetimes.
1503 * Implementations must not increase the
1504 * lifetimes of an existing temporary address
1505 * when processing a Prefix Information Option.
1507 spin_lock(&ift->lock);
1509 if (ift->valid_lft > valid_lft &&
1510 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1511 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1512 if (ift->prefered_lft > prefered_lft &&
1513 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1514 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1515 spin_unlock(&ift->lock);
1516 if (!(flags&IFA_F_TENTATIVE))
1517 ipv6_ifa_notify(0, ift);
1520 if (create && in6_dev->cnf.use_tempaddr > 0) {
1522 * When a new public address is created as described in [ADDRCONF],
1523 * also create a new temporary address.
1525 read_unlock_bh(&in6_dev->lock);
1526 ipv6_create_tempaddr(ifp, NULL);
1528 read_unlock_bh(&in6_dev->lock);
1535 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1536 in6_dev_put(in6_dev);
1540 * Set destination address.
1541 * Special case for SIT interfaces where we create a new "virtual"
1544 int addrconf_set_dstaddr(void __user *arg)
1546 struct in6_ifreq ireq;
1547 struct net_device *dev;
1553 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1556 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1562 if (dev->type == ARPHRD_SIT) {
1565 struct ip_tunnel_parm p;
1567 err = -EADDRNOTAVAIL;
1568 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1571 memset(&p, 0, sizeof(p));
1572 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1576 p.iph.protocol = IPPROTO_IPV6;
1578 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1580 oldfs = get_fs(); set_fs(KERNEL_DS);
1581 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1586 if ((dev = __dev_get_by_name(p.name)) == NULL)
1588 err = dev_open(dev);
1598 * Manual configuration of address on an interface
1600 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1602 struct inet6_ifaddr *ifp;
1603 struct inet6_dev *idev;
1604 struct net_device *dev;
1609 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1612 if (!(dev->flags&IFF_UP))
1615 if ((idev = addrconf_add_dev(dev)) == NULL)
1618 scope = ipv6_addr_scope(pfx);
1620 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1622 addrconf_dad_start(ifp, 0);
1627 return PTR_ERR(ifp);
1630 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1632 struct inet6_ifaddr *ifp;
1633 struct inet6_dev *idev;
1634 struct net_device *dev;
1636 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1639 if ((idev = __in6_dev_get(dev)) == NULL)
1642 read_lock_bh(&idev->lock);
1643 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1644 if (ifp->prefix_len == plen &&
1645 (!memcmp(pfx, &ifp->addr, sizeof(struct in6_addr)))) {
1647 read_unlock_bh(&idev->lock);
1651 /* If the last address is deleted administratively,
1652 disable IPv6 on this interface.
1654 if (idev->addr_list == NULL)
1655 addrconf_ifdown(idev->dev, 1);
1659 read_unlock_bh(&idev->lock);
1660 return -EADDRNOTAVAIL;
1664 int addrconf_add_ifaddr(void __user *arg)
1666 struct in6_ifreq ireq;
1669 if (!capable(CAP_NET_ADMIN))
1672 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1676 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1681 int addrconf_del_ifaddr(void __user *arg)
1683 struct in6_ifreq ireq;
1686 if (!capable(CAP_NET_ADMIN))
1689 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1693 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1698 static void sit_add_v4_addrs(struct inet6_dev *idev)
1700 struct inet6_ifaddr * ifp;
1701 struct in6_addr addr;
1702 struct net_device *dev;
1707 memset(&addr, 0, sizeof(struct in6_addr));
1708 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1710 if (idev->dev->flags&IFF_POINTOPOINT) {
1711 addr.s6_addr32[0] = htonl(0xfe800000);
1714 scope = IPV6_ADDR_COMPATv4;
1717 if (addr.s6_addr32[3]) {
1718 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1720 spin_lock_bh(&ifp->lock);
1721 ifp->flags &= ~IFA_F_TENTATIVE;
1722 spin_unlock_bh(&ifp->lock);
1723 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1729 for (dev = dev_base; dev != NULL; dev = dev->next) {
1730 struct in_device * in_dev = __in_dev_get(dev);
1731 if (in_dev && (dev->flags & IFF_UP)) {
1732 struct in_ifaddr * ifa;
1736 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1739 addr.s6_addr32[3] = ifa->ifa_local;
1741 if (ifa->ifa_scope == RT_SCOPE_LINK)
1743 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1744 if (idev->dev->flags&IFF_POINTOPOINT)
1748 if (idev->dev->flags&IFF_POINTOPOINT)
1753 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1756 spin_lock_bh(&ifp->lock);
1757 ifp->flags &= ~IFA_F_TENTATIVE;
1758 spin_unlock_bh(&ifp->lock);
1759 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1767 static void init_loopback(struct net_device *dev)
1769 struct inet6_dev *idev;
1770 struct inet6_ifaddr * ifp;
1776 if ((idev = ipv6_find_idev(dev)) == NULL) {
1777 printk(KERN_DEBUG "init loopback: add_dev failed\n");
1781 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
1783 spin_lock_bh(&ifp->lock);
1784 ifp->flags &= ~IFA_F_TENTATIVE;
1785 spin_unlock_bh(&ifp->lock);
1786 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1791 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
1793 struct inet6_ifaddr * ifp;
1795 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
1797 addrconf_dad_start(ifp, 0);
1802 static void addrconf_dev_config(struct net_device *dev)
1804 struct in6_addr addr;
1805 struct inet6_dev * idev;
1809 if ((dev->type != ARPHRD_ETHER) &&
1810 (dev->type != ARPHRD_FDDI) &&
1811 (dev->type != ARPHRD_IEEE802_TR) &&
1812 (dev->type != ARPHRD_ARCNET)) {
1813 /* Alas, we support only Ethernet autoconfiguration. */
1817 idev = addrconf_add_dev(dev);
1821 memset(&addr, 0, sizeof(struct in6_addr));
1822 addr.s6_addr32[0] = htonl(0xFE800000);
1824 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
1825 addrconf_add_linklocal(idev, &addr);
1828 static void addrconf_sit_config(struct net_device *dev)
1830 struct inet6_dev *idev;
1835 * Configure the tunnel with one of our IPv4
1836 * addresses... we should configure all of
1837 * our v4 addrs in the tunnel
1840 if ((idev = ipv6_find_idev(dev)) == NULL) {
1841 printk(KERN_DEBUG "init sit: add_dev failed\n");
1845 sit_add_v4_addrs(idev);
1847 if (dev->flags&IFF_POINTOPOINT) {
1848 addrconf_add_mroute(dev);
1849 addrconf_add_lroute(dev);
1855 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
1857 struct in6_addr lladdr;
1859 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
1860 addrconf_add_linklocal(idev, &lladdr);
1866 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
1868 struct net_device *link_dev;
1870 /* first try to inherit the link-local address from the link device */
1871 if (idev->dev->iflink &&
1872 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
1873 if (!ipv6_inherit_linklocal(idev, link_dev))
1876 /* then try to inherit it from any device */
1877 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
1878 if (!ipv6_inherit_linklocal(idev, link_dev))
1881 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
1885 * Autoconfigure tunnel with a link-local address so routing protocols,
1886 * DHCPv6, MLD etc. can be run over the virtual link
1889 static void addrconf_ip6_tnl_config(struct net_device *dev)
1891 struct inet6_dev *idev;
1895 if ((idev = addrconf_add_dev(dev)) == NULL) {
1896 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
1899 ip6_tnl_add_linklocal(idev);
1900 addrconf_add_mroute(dev);
1903 static int addrconf_notify(struct notifier_block *this, unsigned long event,
1906 struct net_device *dev = (struct net_device *) data;
1907 struct inet6_dev *idev = __in6_dev_get(dev);
1913 addrconf_sit_config(dev);
1915 case ARPHRD_TUNNEL6:
1916 addrconf_ip6_tnl_config(dev);
1918 case ARPHRD_LOOPBACK:
1923 addrconf_dev_config(dev);
1927 /* If the MTU changed during the interface down, when the
1928 interface up, the changed MTU must be reflected in the
1929 idev as well as routers.
1931 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
1932 rt6_mtu_change(dev, dev->mtu);
1933 idev->cnf.mtu6 = dev->mtu;
1935 idev->tstamp = jiffies;
1936 inet6_ifinfo_notify(RTM_NEWLINK, idev);
1937 /* If the changed mtu during down is lower than IPV6_MIN_MTU
1938 stop IPv6 on this interface.
1940 if (dev->mtu < IPV6_MIN_MTU)
1941 addrconf_ifdown(dev, event != NETDEV_DOWN);
1945 case NETDEV_CHANGEMTU:
1946 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
1947 rt6_mtu_change(dev, dev->mtu);
1948 idev->cnf.mtu6 = dev->mtu;
1952 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
1955 case NETDEV_UNREGISTER:
1957 * Remove all addresses from this interface.
1959 addrconf_ifdown(dev, event != NETDEV_DOWN);
1963 case NETDEV_CHANGENAME:
1964 #ifdef CONFIG_SYSCTL
1966 addrconf_sysctl_unregister(&idev->cnf);
1967 neigh_sysctl_unregister(idev->nd_parms);
1968 neigh_sysctl_register(dev, idev->nd_parms, NET_IPV6, NET_IPV6_NEIGH, "ipv6", &ndisc_ifinfo_sysctl_change);
1969 addrconf_sysctl_register(idev, &idev->cnf);
1979 * addrconf module should be notified of a device going up
1981 static struct notifier_block ipv6_dev_notf = {
1982 .notifier_call = addrconf_notify,
1986 static int addrconf_ifdown(struct net_device *dev, int how)
1988 struct inet6_dev *idev;
1989 struct inet6_ifaddr *ifa, **bifa;
1995 neigh_ifdown(&nd_tbl, dev);
1997 idev = __in6_dev_get(dev);
2001 /* Step 1: remove reference to ipv6 device from parent device.
2005 write_lock_bh(&addrconf_lock);
2006 dev->ip6_ptr = NULL;
2008 write_unlock_bh(&addrconf_lock);
2011 /* Step 2: clear hash table */
2012 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2013 bifa = &inet6_addr_lst[i];
2015 write_lock_bh(&addrconf_hash_lock);
2016 while ((ifa = *bifa) != NULL) {
2017 if (ifa->idev == idev) {
2018 *bifa = ifa->lst_next;
2019 ifa->lst_next = NULL;
2020 addrconf_del_timer(ifa);
2024 bifa = &ifa->lst_next;
2026 write_unlock_bh(&addrconf_hash_lock);
2029 write_lock_bh(&idev->lock);
2031 /* Step 3: clear flags for stateless addrconf */
2033 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD);
2035 /* Step 4: clear address list */
2036 #ifdef CONFIG_IPV6_PRIVACY
2037 if (how == 1 && del_timer(&idev->regen_timer))
2040 /* clear tempaddr list */
2041 while ((ifa = idev->tempaddr_list) != NULL) {
2042 idev->tempaddr_list = ifa->tmp_next;
2043 ifa->tmp_next = NULL;
2045 write_unlock_bh(&idev->lock);
2046 spin_lock_bh(&ifa->lock);
2049 in6_ifa_put(ifa->ifpub);
2052 spin_unlock_bh(&ifa->lock);
2054 write_lock_bh(&idev->lock);
2057 while ((ifa = idev->addr_list) != NULL) {
2058 idev->addr_list = ifa->if_next;
2059 ifa->if_next = NULL;
2061 addrconf_del_timer(ifa);
2062 write_unlock_bh(&idev->lock);
2064 ipv6_ifa_notify(RTM_DELADDR, ifa);
2067 write_lock_bh(&idev->lock);
2069 write_unlock_bh(&idev->lock);
2071 /* Step 5: Discard multicast list */
2074 ipv6_mc_destroy_dev(idev);
2078 /* Step 5: netlink notification of this interface */
2079 idev->tstamp = jiffies;
2080 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2082 /* Shot the device (if unregistered) */
2085 #ifdef CONFIG_SYSCTL
2086 addrconf_sysctl_unregister(&idev->cnf);
2087 neigh_sysctl_unregister(idev->nd_parms);
2089 neigh_parms_release(&nd_tbl, idev->nd_parms);
2090 neigh_ifdown(&nd_tbl, dev);
2096 static void addrconf_rs_timer(unsigned long data)
2098 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2100 if (ifp->idev->cnf.forwarding)
2103 if (ifp->idev->if_flags & IF_RA_RCVD) {
2105 * Announcement received after solicitation
2111 spin_lock(&ifp->lock);
2112 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2113 struct in6_addr all_routers;
2115 /* The wait after the last probe can be shorter */
2116 addrconf_mod_timer(ifp, AC_RS,
2117 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2118 ifp->idev->cnf.rtr_solicit_delay :
2119 ifp->idev->cnf.rtr_solicit_interval);
2120 spin_unlock(&ifp->lock);
2122 ipv6_addr_all_routers(&all_routers);
2124 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2126 spin_unlock(&ifp->lock);
2128 * Note: we do not support deprecated "all on-link"
2129 * assumption any longer.
2131 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2132 ifp->idev->dev->name);
2140 * Duplicate Address Detection
2142 static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags)
2144 struct net_device *dev;
2145 unsigned long rand_num;
2147 dev = ifp->idev->dev;
2149 addrconf_join_solict(dev, &ifp->addr);
2151 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2152 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2155 net_srandom(ifp->addr.s6_addr32[3]);
2156 rand_num = net_random() % (ifp->idev->cnf.rtr_solicit_delay ? : 1);
2158 spin_lock_bh(&ifp->lock);
2160 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2161 !(ifp->flags&IFA_F_TENTATIVE)) {
2162 ifp->flags &= ~IFA_F_TENTATIVE;
2163 spin_unlock_bh(&ifp->lock);
2165 addrconf_dad_completed(ifp);
2169 ifp->probes = ifp->idev->cnf.dad_transmits;
2170 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2172 spin_unlock_bh(&ifp->lock);
2175 static void addrconf_dad_timer(unsigned long data)
2177 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2178 struct in6_addr unspec;
2179 struct in6_addr mcaddr;
2181 spin_lock_bh(&ifp->lock);
2182 if (ifp->probes == 0) {
2184 * DAD was successful
2187 ifp->flags &= ~IFA_F_TENTATIVE;
2188 spin_unlock_bh(&ifp->lock);
2190 addrconf_dad_completed(ifp);
2197 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2198 spin_unlock_bh(&ifp->lock);
2200 /* send a neighbour solicitation for our addr */
2201 memset(&unspec, 0, sizeof(unspec));
2202 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2203 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2208 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2210 struct net_device * dev = ifp->idev->dev;
2213 * Configure the address for reception. Now it is valid.
2216 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2218 /* If added prefix is link local and forwarding is off,
2219 start sending router solicitations.
2222 if (ifp->idev->cnf.forwarding == 0 &&
2223 ifp->idev->cnf.rtr_solicits > 0 &&
2224 (dev->flags&IFF_LOOPBACK) == 0 &&
2225 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2226 struct in6_addr all_routers;
2228 ipv6_addr_all_routers(&all_routers);
2231 * If a host as already performed a random delay
2232 * [...] as part of DAD [...] there is no need
2233 * to delay again before sending the first RS
2235 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2237 spin_lock_bh(&ifp->lock);
2239 ifp->idev->if_flags |= IF_RS_SENT;
2240 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2241 spin_unlock_bh(&ifp->lock);
2245 #ifdef CONFIG_PROC_FS
2246 struct if6_iter_state {
2250 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2252 struct inet6_ifaddr *ifa = NULL;
2253 struct if6_iter_state *state = seq->private;
2255 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2256 ifa = inet6_addr_lst[state->bucket];
2263 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2265 struct if6_iter_state *state = seq->private;
2267 ifa = ifa->lst_next;
2269 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2270 ifa = inet6_addr_lst[state->bucket];
2276 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2278 struct inet6_ifaddr *ifa = if6_get_first(seq);
2281 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2283 return pos ? NULL : ifa;
2286 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2288 read_lock_bh(&addrconf_hash_lock);
2289 return if6_get_idx(seq, *pos);
2292 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2294 struct inet6_ifaddr *ifa;
2296 ifa = if6_get_next(seq, v);
2301 static void if6_seq_stop(struct seq_file *seq, void *v)
2303 read_unlock_bh(&addrconf_hash_lock);
2306 static int if6_seq_show(struct seq_file *seq, void *v)
2308 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2310 "%04x%04x%04x%04x%04x%04x%04x%04x %02x %02x %02x %02x %8s\n",
2312 ifp->idev->dev->ifindex,
2316 ifp->idev->dev->name);
2320 static struct seq_operations if6_seq_ops = {
2321 .start = if6_seq_start,
2322 .next = if6_seq_next,
2323 .show = if6_seq_show,
2324 .stop = if6_seq_stop,
2327 static int if6_seq_open(struct inode *inode, struct file *file)
2329 struct seq_file *seq;
2331 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2335 memset(s, 0, sizeof(*s));
2337 rc = seq_open(file, &if6_seq_ops);
2341 seq = file->private_data;
2350 static struct file_operations if6_fops = {
2351 .owner = THIS_MODULE,
2352 .open = if6_seq_open,
2354 .llseek = seq_lseek,
2355 .release = seq_release_private,
2358 int __init if6_proc_init(void)
2360 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2365 void if6_proc_exit(void)
2367 proc_net_remove("if_inet6");
2369 #endif /* CONFIG_PROC_FS */
2372 * Periodic address status verification
2375 static void addrconf_verify(unsigned long foo)
2377 struct inet6_ifaddr *ifp;
2378 unsigned long now, next;
2381 spin_lock_bh(&addrconf_verify_lock);
2383 next = now + ADDR_CHECK_FREQUENCY;
2385 del_timer(&addr_chk_timer);
2387 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2390 write_lock(&addrconf_hash_lock);
2391 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2393 #ifdef CONFIG_IPV6_PRIVACY
2394 unsigned long regen_advance;
2397 if (ifp->flags & IFA_F_PERMANENT)
2400 spin_lock(&ifp->lock);
2401 age = (now - ifp->tstamp) / HZ;
2403 #ifdef CONFIG_IPV6_PRIVACY
2404 regen_advance = ifp->idev->cnf.regen_max_retry *
2405 ifp->idev->cnf.dad_transmits *
2406 ifp->idev->nd_parms->retrans_time / HZ;
2409 if (age >= ifp->valid_lft) {
2410 spin_unlock(&ifp->lock);
2412 write_unlock(&addrconf_hash_lock);
2415 } else if (age >= ifp->prefered_lft) {
2416 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2419 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2421 ifp->flags |= IFA_F_DEPRECATED;
2424 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2425 next = ifp->tstamp + ifp->valid_lft * HZ;
2427 spin_unlock(&ifp->lock);
2431 write_unlock(&addrconf_hash_lock);
2433 ipv6_ifa_notify(0, ifp);
2437 #ifdef CONFIG_IPV6_PRIVACY
2438 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2439 !(ifp->flags&IFA_F_TENTATIVE)) {
2440 if (age >= ifp->prefered_lft - regen_advance) {
2441 struct inet6_ifaddr *ifpub = ifp->ifpub;
2442 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2443 next = ifp->tstamp + ifp->prefered_lft * HZ;
2444 if (!ifp->regen_count && ifpub) {
2447 in6_ifa_hold(ifpub);
2448 spin_unlock(&ifp->lock);
2449 write_unlock(&addrconf_hash_lock);
2450 ipv6_create_tempaddr(ifpub, ifp);
2455 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2456 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2457 spin_unlock(&ifp->lock);
2460 /* ifp->prefered_lft <= ifp->valid_lft */
2461 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2462 next = ifp->tstamp + ifp->prefered_lft * HZ;
2463 spin_unlock(&ifp->lock);
2466 write_unlock(&addrconf_hash_lock);
2469 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2470 add_timer(&addr_chk_timer);
2471 spin_unlock_bh(&addrconf_verify_lock);
2475 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2477 struct rtattr **rta = arg;
2478 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2479 struct in6_addr *pfx;
2482 if (rta[IFA_ADDRESS-1]) {
2483 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2485 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2487 if (rta[IFA_LOCAL-1]) {
2488 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2490 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2495 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2499 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2501 struct rtattr **rta = arg;
2502 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2503 struct in6_addr *pfx;
2506 if (rta[IFA_ADDRESS-1]) {
2507 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2509 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2511 if (rta[IFA_LOCAL-1]) {
2512 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2514 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2519 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2522 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2523 u32 pid, u32 seq, int event)
2525 struct ifaddrmsg *ifm;
2526 struct nlmsghdr *nlh;
2527 struct ifa_cacheinfo ci;
2528 unsigned char *b = skb->tail;
2530 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2531 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2532 ifm = NLMSG_DATA(nlh);
2533 ifm->ifa_family = AF_INET6;
2534 ifm->ifa_prefixlen = ifa->prefix_len;
2535 ifm->ifa_flags = ifa->flags;
2536 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2537 if (ifa->scope&IFA_HOST)
2538 ifm->ifa_scope = RT_SCOPE_HOST;
2539 else if (ifa->scope&IFA_LINK)
2540 ifm->ifa_scope = RT_SCOPE_LINK;
2541 else if (ifa->scope&IFA_SITE)
2542 ifm->ifa_scope = RT_SCOPE_SITE;
2543 ifm->ifa_index = ifa->idev->dev->ifindex;
2544 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2545 if (!(ifa->flags&IFA_F_PERMANENT)) {
2546 ci.ifa_prefered = ifa->prefered_lft;
2547 ci.ifa_valid = ifa->valid_lft;
2548 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2549 long tval = (jiffies - ifa->tstamp)/HZ;
2550 ci.ifa_prefered -= tval;
2551 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2552 ci.ifa_valid -= tval;
2555 ci.ifa_prefered = INFINITY_LIFE_TIME;
2556 ci.ifa_valid = INFINITY_LIFE_TIME;
2558 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2559 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2560 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2561 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2562 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2563 nlh->nlmsg_len = skb->tail - b;
2568 skb_trim(skb, b - skb->data);
2572 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2573 u32 pid, u32 seq, int event)
2575 struct ifaddrmsg *ifm;
2576 struct nlmsghdr *nlh;
2577 struct ifa_cacheinfo ci;
2578 unsigned char *b = skb->tail;
2580 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2581 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2582 ifm = NLMSG_DATA(nlh);
2583 ifm->ifa_family = AF_INET6;
2584 ifm->ifa_prefixlen = 128;
2585 ifm->ifa_flags = IFA_F_PERMANENT;
2586 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2587 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2588 ifm->ifa_scope = RT_SCOPE_SITE;
2589 ifm->ifa_index = ifmca->idev->dev->ifindex;
2590 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2591 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2592 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2594 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2595 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2597 ci.ifa_prefered = INFINITY_LIFE_TIME;
2598 ci.ifa_valid = INFINITY_LIFE_TIME;
2599 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2600 nlh->nlmsg_len = skb->tail - b;
2605 skb_trim(skb, b - skb->data);
2609 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2610 u32 pid, u32 seq, int event)
2612 struct ifaddrmsg *ifm;
2613 struct nlmsghdr *nlh;
2614 struct ifa_cacheinfo ci;
2615 unsigned char *b = skb->tail;
2617 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2618 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2619 ifm = NLMSG_DATA(nlh);
2620 ifm->ifa_family = AF_INET6;
2621 ifm->ifa_prefixlen = 128;
2622 ifm->ifa_flags = IFA_F_PERMANENT;
2623 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2624 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2625 ifm->ifa_scope = RT_SCOPE_SITE;
2626 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2627 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2628 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2629 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2631 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2632 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2634 ci.ifa_prefered = INFINITY_LIFE_TIME;
2635 ci.ifa_valid = INFINITY_LIFE_TIME;
2636 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2637 nlh->nlmsg_len = skb->tail - b;
2642 skb_trim(skb, b - skb->data);
2653 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2654 enum addr_type_t type)
2657 int s_idx, s_ip_idx;
2659 struct net_device *dev;
2660 struct inet6_dev *idev = NULL;
2661 struct inet6_ifaddr *ifa;
2662 struct ifmcaddr6 *ifmca;
2663 struct ifacaddr6 *ifaca;
2665 /* no ipv6 inside a vserver for now */
2666 if (skb->sk && skb->sk->sk_vx_info)
2669 s_idx = cb->args[0];
2670 s_ip_idx = ip_idx = cb->args[1];
2671 read_lock(&dev_base_lock);
2673 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
2679 if ((idev = in6_dev_get(dev)) == NULL)
2681 read_lock_bh(&idev->lock);
2684 /* unicast address */
2685 for (ifa = idev->addr_list; ifa;
2686 ifa = ifa->if_next, ip_idx++) {
2687 if (ip_idx < s_ip_idx)
2689 if ((err = inet6_fill_ifaddr(skb, ifa,
2690 NETLINK_CB(cb->skb).pid,
2691 cb->nlh->nlmsg_seq, RTM_NEWADDR)) <= 0)
2695 #ifdef CONFIG_IPV6_PRIVACY
2696 for (ifa = idev->tempaddr_list; ifa;
2697 ifa = ifa->tmp_next, ip_idx++) {
2698 if (ip_idx < s_ip_idx)
2700 if ((err = inet6_fill_ifaddr(skb, ifa,
2701 NETLINK_CB(cb->skb).pid,
2702 cb->nlh->nlmsg_seq, RTM_NEWADDR)) <= 0)
2707 case MULTICAST_ADDR:
2708 /* multicast address */
2709 for (ifmca = idev->mc_list; ifmca;
2710 ifmca = ifmca->next, ip_idx++) {
2711 if (ip_idx < s_ip_idx)
2713 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
2714 NETLINK_CB(cb->skb).pid,
2715 cb->nlh->nlmsg_seq, RTM_GETMULTICAST)) <= 0)
2720 /* anycast address */
2721 for (ifaca = idev->ac_list; ifaca;
2722 ifaca = ifaca->aca_next, ip_idx++) {
2723 if (ip_idx < s_ip_idx)
2725 if ((err = inet6_fill_ifacaddr(skb, ifaca,
2726 NETLINK_CB(cb->skb).pid,
2727 cb->nlh->nlmsg_seq, RTM_GETANYCAST)) <= 0)
2734 read_unlock_bh(&idev->lock);
2739 read_unlock_bh(&idev->lock);
2742 read_unlock(&dev_base_lock);
2744 cb->args[1] = ip_idx;
2748 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
2750 enum addr_type_t type = UNICAST_ADDR;
2751 return inet6_dump_addr(skb, cb, type);
2754 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
2756 enum addr_type_t type = MULTICAST_ADDR;
2757 return inet6_dump_addr(skb, cb, type);
2761 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
2763 enum addr_type_t type = ANYCAST_ADDR;
2764 return inet6_dump_addr(skb, cb, type);
2767 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
2769 struct sk_buff *skb;
2770 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
2772 skb = alloc_skb(size, GFP_ATOMIC);
2774 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, ENOBUFS);
2777 if (inet6_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
2779 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, EINVAL);
2782 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFADDR;
2783 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFADDR, GFP_ATOMIC);
2786 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
2787 __s32 *array, int bytes)
2789 memset(array, 0, bytes);
2790 array[DEVCONF_FORWARDING] = cnf->forwarding;
2791 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
2792 array[DEVCONF_MTU6] = cnf->mtu6;
2793 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
2794 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
2795 array[DEVCONF_AUTOCONF] = cnf->autoconf;
2796 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
2797 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
2798 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
2799 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
2800 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
2801 #ifdef CONFIG_IPV6_PRIVACY
2802 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
2803 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
2804 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
2805 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
2806 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
2808 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
2811 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
2812 u32 pid, u32 seq, int event)
2814 struct net_device *dev = idev->dev;
2815 __s32 *array = NULL;
2816 struct ifinfomsg *r;
2817 struct nlmsghdr *nlh;
2818 unsigned char *b = skb->tail;
2819 struct rtattr *subattr;
2820 __u32 mtu = dev->mtu;
2821 struct ifla_cacheinfo ci;
2823 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*r));
2824 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2825 r = NLMSG_DATA(nlh);
2826 r->ifi_family = AF_INET6;
2827 r->ifi_type = dev->type;
2828 r->ifi_index = dev->ifindex;
2829 r->ifi_flags = dev->flags;
2831 if (!netif_running(dev) || !netif_carrier_ok(dev))
2832 r->ifi_flags &= ~IFF_RUNNING;
2834 r->ifi_flags |= IFF_RUNNING;
2836 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
2839 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
2841 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
2842 if (dev->ifindex != dev->iflink)
2843 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
2845 subattr = (struct rtattr*)skb->tail;
2847 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
2849 /* return the device flags */
2850 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
2852 /* return interface cacheinfo */
2853 ci.max_reasm_len = IPV6_MAXPLEN;
2854 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
2855 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2856 ci.reachable_time = idev->nd_parms->reachable_time;
2857 ci.retrans_time = idev->nd_parms->retrans_time;
2858 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
2860 /* return the device sysctl params */
2861 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
2862 goto rtattr_failure;
2863 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
2864 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
2866 /* XXX - Statistics/MC not implemented */
2867 subattr->rta_len = skb->tail - (u8*)subattr;
2869 nlh->nlmsg_len = skb->tail - b;
2877 skb_trim(skb, b - skb->data);
2881 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2884 int s_idx = cb->args[0];
2885 struct net_device *dev;
2886 struct inet6_dev *idev;
2888 /* no ipv6 inside a vserver for now */
2889 if (skb->sk && skb->sk->sk_vx_info)
2892 read_lock(&dev_base_lock);
2893 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
2896 if ((idev = in6_dev_get(dev)) == NULL)
2898 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
2899 cb->nlh->nlmsg_seq, RTM_NEWLINK);
2904 read_unlock(&dev_base_lock);
2910 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
2912 struct sk_buff *skb;
2914 int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
2916 skb = alloc_skb(size, GFP_ATOMIC);
2918 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFINFO, ENOBUFS);
2921 if (inet6_fill_ifinfo(skb, idev, 0, 0, event) < 0) {
2923 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFINFO, EINVAL);
2926 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFINFO;
2927 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFINFO, GFP_ATOMIC);
2930 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
2931 struct prefix_info *pinfo, u32 pid, u32 seq, int event)
2933 struct prefixmsg *pmsg;
2934 struct nlmsghdr *nlh;
2935 unsigned char *b = skb->tail;
2936 struct prefix_cacheinfo ci;
2938 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*pmsg));
2941 nlh->nlmsg_flags |= NLM_F_MULTI;
2943 pmsg = NLMSG_DATA(nlh);
2944 pmsg->prefix_family = AF_INET6;
2945 pmsg->prefix_ifindex = idev->dev->ifindex;
2946 pmsg->prefix_len = pinfo->prefix_len;
2947 pmsg->prefix_type = pinfo->type;
2949 pmsg->prefix_flags = 0;
2951 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
2952 if (pinfo->autoconf)
2953 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
2955 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
2957 ci.preferred_time = ntohl(pinfo->prefered);
2958 ci.valid_time = ntohl(pinfo->valid);
2959 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
2961 nlh->nlmsg_len = skb->tail - b;
2966 skb_trim(skb, b - skb->data);
2970 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
2971 struct prefix_info *pinfo)
2973 struct sk_buff *skb;
2974 int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
2976 skb = alloc_skb(size, GFP_ATOMIC);
2978 netlink_set_err(rtnl, 0, RTMGRP_IPV6_PREFIX, ENOBUFS);
2981 if (inet6_fill_prefix(skb, idev, pinfo, 0, 0, event) < 0) {
2983 netlink_set_err(rtnl, 0, RTMGRP_IPV6_PREFIX, EINVAL);
2986 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_PREFIX;
2987 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_PREFIX, GFP_ATOMIC);
2990 static struct rtnetlink_link inet6_rtnetlink_table[RTM_MAX - RTM_BASE + 1] = {
2991 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
2992 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
2993 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
2994 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
2995 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
2996 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
2997 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
2998 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
2999 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3000 .dumpit = inet6_dump_fib, },
3003 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3005 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3009 dst_hold(&ifp->rt->u.dst);
3010 if (ip6_ins_rt(ifp->rt, NULL, NULL))
3011 dst_release(&ifp->rt->u.dst);
3012 if (ifp->idev->cnf.forwarding)
3013 addrconf_join_anycast(ifp);
3016 if (ifp->idev->cnf.forwarding)
3017 addrconf_leave_anycast(ifp);
3018 addrconf_leave_solict(ifp->idev, &ifp->addr);
3019 dst_hold(&ifp->rt->u.dst);
3020 if (ip6_del_rt(ifp->rt, NULL, NULL))
3021 dst_free(&ifp->rt->u.dst);
3023 dst_release(&ifp->rt->u.dst);
3028 #ifdef CONFIG_SYSCTL
3031 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3032 void __user *buffer, size_t *lenp, loff_t *ppos)
3034 int *valp = ctl->data;
3038 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3040 if (write && valp != &ipv6_devconf_dflt.forwarding) {
3041 if (valp != &ipv6_devconf.forwarding) {
3042 if ((!*valp) ^ (!val)) {
3043 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
3046 dev_forward_change(idev);
3049 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3050 addrconf_forward_change();
3053 rt6_purge_dflt_routers(0);
3059 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3060 int __user *name, int nlen,
3061 void __user *oldval,
3062 size_t __user *oldlenp,
3063 void __user *newval, size_t newlen,
3066 int *valp = table->data;
3069 if (!newval || !newlen)
3071 if (newlen != sizeof(int))
3073 if (get_user(new, (int __user *)newval))
3077 if (oldval && oldlenp) {
3079 if (get_user(len, oldlenp))
3082 if (len > table->maxlen)
3083 len = table->maxlen;
3084 if (copy_to_user(oldval, valp, len))
3086 if (put_user(len, oldlenp))
3091 if (valp != &ipv6_devconf_dflt.forwarding) {
3092 if (valp != &ipv6_devconf.forwarding) {
3093 struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
3095 if (unlikely(idev == NULL))
3097 changed = (!*valp) ^ (!new);
3100 dev_forward_change(idev);
3103 addrconf_forward_change();
3107 rt6_purge_dflt_routers(0);
3114 static struct addrconf_sysctl_table
3116 struct ctl_table_header *sysctl_header;
3117 ctl_table addrconf_vars[18];
3118 ctl_table addrconf_dev[2];
3119 ctl_table addrconf_conf_dir[2];
3120 ctl_table addrconf_proto_dir[2];
3121 ctl_table addrconf_root_dir[2];
3122 } addrconf_sysctl = {
3123 .sysctl_header = NULL,
3126 .ctl_name = NET_IPV6_FORWARDING,
3127 .procname = "forwarding",
3128 .data = &ipv6_devconf.forwarding,
3129 .maxlen = sizeof(int),
3131 .proc_handler = &addrconf_sysctl_forward,
3132 .strategy = &addrconf_sysctl_forward_strategy,
3135 .ctl_name = NET_IPV6_HOP_LIMIT,
3136 .procname = "hop_limit",
3137 .data = &ipv6_devconf.hop_limit,
3138 .maxlen = sizeof(int),
3140 .proc_handler = proc_dointvec,
3143 .ctl_name = NET_IPV6_MTU,
3145 .data = &ipv6_devconf.mtu6,
3146 .maxlen = sizeof(int),
3148 .proc_handler = &proc_dointvec,
3151 .ctl_name = NET_IPV6_ACCEPT_RA,
3152 .procname = "accept_ra",
3153 .data = &ipv6_devconf.accept_ra,
3154 .maxlen = sizeof(int),
3156 .proc_handler = &proc_dointvec,
3159 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3160 .procname = "accept_redirects",
3161 .data = &ipv6_devconf.accept_redirects,
3162 .maxlen = sizeof(int),
3164 .proc_handler = &proc_dointvec,
3167 .ctl_name = NET_IPV6_AUTOCONF,
3168 .procname = "autoconf",
3169 .data = &ipv6_devconf.autoconf,
3170 .maxlen = sizeof(int),
3172 .proc_handler = &proc_dointvec,
3175 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3176 .procname = "dad_transmits",
3177 .data = &ipv6_devconf.dad_transmits,
3178 .maxlen = sizeof(int),
3180 .proc_handler = &proc_dointvec,
3183 .ctl_name = NET_IPV6_RTR_SOLICITS,
3184 .procname = "router_solicitations",
3185 .data = &ipv6_devconf.rtr_solicits,
3186 .maxlen = sizeof(int),
3188 .proc_handler = &proc_dointvec,
3191 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3192 .procname = "router_solicitation_interval",
3193 .data = &ipv6_devconf.rtr_solicit_interval,
3194 .maxlen = sizeof(int),
3196 .proc_handler = &proc_dointvec_jiffies,
3197 .strategy = &sysctl_jiffies,
3200 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3201 .procname = "router_solicitation_delay",
3202 .data = &ipv6_devconf.rtr_solicit_delay,
3203 .maxlen = sizeof(int),
3205 .proc_handler = &proc_dointvec_jiffies,
3206 .strategy = &sysctl_jiffies,
3209 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3210 .procname = "force_mld_version",
3211 .data = &ipv6_devconf.force_mld_version,
3212 .maxlen = sizeof(int),
3214 .proc_handler = &proc_dointvec,
3216 #ifdef CONFIG_IPV6_PRIVACY
3218 .ctl_name = NET_IPV6_USE_TEMPADDR,
3219 .procname = "use_tempaddr",
3220 .data = &ipv6_devconf.use_tempaddr,
3221 .maxlen = sizeof(int),
3223 .proc_handler = &proc_dointvec,
3226 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3227 .procname = "temp_valid_lft",
3228 .data = &ipv6_devconf.temp_valid_lft,
3229 .maxlen = sizeof(int),
3231 .proc_handler = &proc_dointvec,
3234 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3235 .procname = "temp_prefered_lft",
3236 .data = &ipv6_devconf.temp_prefered_lft,
3237 .maxlen = sizeof(int),
3239 .proc_handler = &proc_dointvec,
3242 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3243 .procname = "regen_max_retry",
3244 .data = &ipv6_devconf.regen_max_retry,
3245 .maxlen = sizeof(int),
3247 .proc_handler = &proc_dointvec,
3250 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3251 .procname = "max_desync_factor",
3252 .data = &ipv6_devconf.max_desync_factor,
3253 .maxlen = sizeof(int),
3255 .proc_handler = &proc_dointvec,
3259 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3260 .procname = "max_addresses",
3261 .data = &ipv6_devconf.max_addresses,
3262 .maxlen = sizeof(int),
3264 .proc_handler = &proc_dointvec,
3267 .ctl_name = 0, /* sentinel */
3272 .ctl_name = NET_PROTO_CONF_ALL,
3275 .child = addrconf_sysctl.addrconf_vars,
3278 .ctl_name = 0, /* sentinel */
3281 .addrconf_conf_dir = {
3283 .ctl_name = NET_IPV6_CONF,
3286 .child = addrconf_sysctl.addrconf_dev,
3289 .ctl_name = 0, /* sentinel */
3292 .addrconf_proto_dir = {
3294 .ctl_name = NET_IPV6,
3297 .child = addrconf_sysctl.addrconf_conf_dir,
3300 .ctl_name = 0, /* sentinel */
3303 .addrconf_root_dir = {
3305 .ctl_name = CTL_NET,
3308 .child = addrconf_sysctl.addrconf_proto_dir,
3311 .ctl_name = 0, /* sentinel */
3316 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3319 struct net_device *dev = idev ? idev->dev : NULL;
3320 struct addrconf_sysctl_table *t;
3321 char *dev_name = NULL;
3323 t = kmalloc(sizeof(*t), GFP_KERNEL);
3326 memcpy(t, &addrconf_sysctl, sizeof(*t));
3327 for (i=0; t->addrconf_vars[i].data; i++) {
3328 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3329 t->addrconf_vars[i].de = NULL;
3330 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3333 dev_name = dev->name;
3334 t->addrconf_dev[0].ctl_name = dev->ifindex;
3336 dev_name = "default";
3337 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3341 * Make a copy of dev_name, because '.procname' is regarded as const
3342 * by sysctl and we wouldn't want anyone to change it under our feet
3343 * (see SIOCSIFNAME).
3345 dev_name = net_sysctl_strdup(dev_name);
3349 t->addrconf_dev[0].procname = dev_name;
3351 t->addrconf_dev[0].child = t->addrconf_vars;
3352 t->addrconf_dev[0].de = NULL;
3353 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3354 t->addrconf_conf_dir[0].de = NULL;
3355 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3356 t->addrconf_proto_dir[0].de = NULL;
3357 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3358 t->addrconf_root_dir[0].de = NULL;
3360 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3361 if (t->sysctl_header == NULL)
3376 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3379 struct addrconf_sysctl_table *t = p->sysctl;
3381 unregister_sysctl_table(t->sysctl_header);
3382 kfree(t->addrconf_dev[0].procname);
3394 int register_inet6addr_notifier(struct notifier_block *nb)
3396 return notifier_chain_register(&inet6addr_chain, nb);
3399 int unregister_inet6addr_notifier(struct notifier_block *nb)
3401 return notifier_chain_unregister(&inet6addr_chain,nb);
3405 * Init / cleanup code
3408 void __init addrconf_init(void)
3410 register_netdevice_notifier(&ipv6_dev_notf);
3412 #ifdef CONFIG_IPV6_PRIVACY
3413 md5_tfm = crypto_alloc_tfm("md5", 0);
3414 if (unlikely(md5_tfm == NULL))
3416 "failed to load transform for md5\n");
3420 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3421 #ifdef CONFIG_SYSCTL
3422 addrconf_sysctl.sysctl_header =
3423 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3424 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3428 void __exit addrconf_cleanup(void)
3430 struct net_device *dev;
3431 struct inet6_dev *idev;
3432 struct inet6_ifaddr *ifa;
3435 unregister_netdevice_notifier(&ipv6_dev_notf);
3437 rtnetlink_links[PF_INET6] = NULL;
3438 #ifdef CONFIG_SYSCTL
3439 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3440 addrconf_sysctl_unregister(&ipv6_devconf);
3449 for (dev=dev_base; dev; dev=dev->next) {
3450 if ((idev = __in6_dev_get(dev)) == NULL)
3452 addrconf_ifdown(dev, 1);
3459 write_lock_bh(&addrconf_hash_lock);
3460 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3461 for (ifa=inet6_addr_lst[i]; ifa; ) {
3462 struct inet6_ifaddr *bifa;
3465 ifa = ifa->lst_next;
3466 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3467 /* Do not free it; something is wrong.
3468 Now we can investigate it with debugger.
3472 write_unlock_bh(&addrconf_hash_lock);
3474 del_timer(&addr_chk_timer);
3478 #ifdef CONFIG_IPV6_PRIVACY
3479 if (likely(md5_tfm != NULL)) {
3480 crypto_free_tfm(md5_tfm);
3485 #ifdef CONFIG_PROC_FS
3486 proc_net_remove("if_inet6");