2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill double kfree on module
24 * Maciej W. Rozycki : FDDI support
25 * sekiya@USAGI : Don't send too many RS
27 * yoshfuji@USAGI : Fixed interval between DAD
29 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
30 * address validation timer.
31 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
33 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
34 * address on a same interface.
35 * YOSHIFUJI Hideaki @USAGI : ARCnet support
36 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
38 * YOSHIFUJI Hideaki @USAGI : improved source address
39 * selection; consider scope,
43 #include <linux/config.h>
44 #include <linux/errno.h>
45 #include <linux/types.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/sched.h>
49 #include <linux/net.h>
50 #include <linux/in6.h>
51 #include <linux/netdevice.h>
52 #include <linux/if_arp.h>
53 #include <linux/if_arcnet.h>
54 #include <linux/route.h>
55 #include <linux/inetdevice.h>
56 #include <linux/init.h>
58 #include <linux/sysctl.h>
60 #include <linux/delay.h>
61 #include <linux/notifier.h>
67 #include <net/protocol.h>
68 #include <net/ndisc.h>
69 #include <net/ip6_route.h>
70 #include <net/addrconf.h>
73 #include <linux/if_tunnel.h>
74 #include <linux/rtnetlink.h>
76 #ifdef CONFIG_IPV6_PRIVACY
77 #include <linux/random.h>
78 #include <linux/crypto.h>
79 #include <asm/scatterlist.h>
82 #include <asm/uaccess.h>
84 #include <linux/proc_fs.h>
85 #include <linux/seq_file.h>
87 /* Set to 3 to get tracing... */
91 #define ADBG(x) printk x
96 #define INFINITY_LIFE_TIME 0xFFFFFFFF
97 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
100 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
101 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
107 #ifdef CONFIG_IPV6_PRIVACY
108 static int __ipv6_regen_rndid(struct inet6_dev *idev);
109 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
110 static void ipv6_regen_rndid(unsigned long data);
112 static int desync_factor = MAX_DESYNC_FACTOR * HZ;
113 static struct crypto_tfm *md5_tfm;
114 static spinlock_t md5_tfm_lock = SPIN_LOCK_UNLOCKED;
117 static int ipv6_count_addresses(struct inet6_dev *idev);
120 * Configured unicast address hash table
122 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
123 static rwlock_t addrconf_hash_lock = RW_LOCK_UNLOCKED;
125 /* Protects inet6 devices */
126 rwlock_t addrconf_lock = RW_LOCK_UNLOCKED;
128 static void addrconf_verify(unsigned long);
130 static struct timer_list addr_chk_timer =
131 TIMER_INITIALIZER(addrconf_verify, 0, 0);
132 static spinlock_t addrconf_verify_lock = SPIN_LOCK_UNLOCKED;
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 static u32 ipv6_addrselect_label_lookup(const struct in6_addr *addr, int ifindex);
150 struct ipv6_devconf ipv6_devconf = {
152 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
153 .mtu6 = IPV6_MIN_MTU,
155 .accept_redirects = 1,
157 .force_mld_version = 1,
159 .rtr_solicits = MAX_RTR_SOLICITATIONS,
160 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
161 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
162 #ifdef CONFIG_IPV6_PRIVACY
164 .temp_valid_lft = TEMP_VALID_LIFETIME,
165 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
166 .regen_max_retry = REGEN_MAX_RETRY,
167 .max_desync_factor = MAX_DESYNC_FACTOR,
169 .max_addresses = IPV6_MAX_ADDRESSES,
172 static struct ipv6_devconf ipv6_devconf_dflt = {
174 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
175 .mtu6 = IPV6_MIN_MTU,
177 .accept_redirects = 1,
180 .rtr_solicits = MAX_RTR_SOLICITATIONS,
181 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
182 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
183 #ifdef CONFIG_IPV6_PRIVACY
185 .temp_valid_lft = TEMP_VALID_LIFETIME,
186 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
187 .regen_max_retry = REGEN_MAX_RETRY,
188 .max_desync_factor = MAX_DESYNC_FACTOR,
190 .max_addresses = IPV6_MAX_ADDRESSES,
193 /* IPv6 Wildcard Address and Loopback Address defined by RFC3493 */
194 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
195 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
197 int __ipv6_addr_type(const struct in6_addr *addr)
202 st = addr->s6_addr32[0];
204 /* Consider all addresses with the first three bits different of
205 000 and 111 as unicasts.
207 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
208 (st & htonl(0xE0000000)) != htonl(0xE0000000))
209 return (IPV6_ADDR_UNICAST |
210 IPV6_ADDR_SCOPE_GLOBAL<<16);
212 if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
214 /* addr-select 3.1 */
215 int type = IPV6_ADDR_MC_SCOPE(addr)<<16;
218 case IPV6_ADDR_SCOPE_NODELOCAL<<16:
219 type |= IPV6_ADDR_LOOPBACK;
222 case IPV6_ADDR_SCOPE_LINKLOCAL<<16:
223 type |= IPV6_ADDR_LINKLOCAL;
226 case IPV6_ADDR_SCOPE_SITELOCAL<<16:
227 type |= IPV6_ADDR_SITELOCAL;
230 type |= IPV6_ADDR_MULTICAST;
234 type = IPV6_ADDR_UNICAST;
236 /* Consider all addresses with the first three bits different of
237 000 and 111 as finished.
239 if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
240 (st & htonl(0xE0000000)) != htonl(0xE0000000))
243 if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
244 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
245 IPV6_ADDR_SCOPE_LINKLOCAL<<16); /* addr-select 3.1 */
247 if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
248 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
249 IPV6_ADDR_SCOPE_SITELOCAL<<16); /* addr-select 3.1 */
251 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
252 if (addr->s6_addr32[2] == 0) {
253 if (addr->s6_addr32[3] == 0)
254 return IPV6_ADDR_ANY;
256 if (addr->s6_addr32[3] == htonl(0x00000001))
257 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
258 IPV6_ADDR_SCOPE_LINKLOCAL<<16); /* addr-select 3.4 */
260 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
261 IPV6_ADDR_SCOPE_GLOBAL<<16); /* addr-select 3.3 */
264 if (addr->s6_addr32[2] == htonl(0x0000ffff))
265 return (IPV6_ADDR_MAPPED |
266 IPV6_ADDR_SCOPE_GLOBAL<<16); /* addr-select 3.3 */
269 return (IPV6_ADDR_RESERVED |
270 IPV6_ADDR_SCOPE_GLOBAL<<16); /* addr-select 3.4 */
273 /* find 1st bit in difference between the 2 addrs */
274 static inline int addr_diff(const void *__a1, const void *__a2, int addrlen)
276 /* find 1st bit in difference between the 2 addrs.
277 * bit may be an invalid value,
278 * but if it is >= plen, the value is ignored in any case.
280 const u32 *a1 = __a1;
281 const u32 *a2 = __a2;
285 for (i = 0; i < addrlen; i++) {
286 u32 xb = a1[i] ^ a2[i];
290 while ((xb & (1 << j)) == 0)
292 return (i * 32 + 31 - j);
298 static inline int ipv6_addr_diff(const struct in6_addr *a1, const struct in6_addr *a2)
300 return addr_diff(a1->s6_addr, a2->s6_addr, sizeof(struct in6_addr));
303 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
305 if (del_timer(&ifp->timer))
309 enum addrconf_timer_t
316 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
317 enum addrconf_timer_t what,
320 if (!del_timer(&ifp->timer))
325 ifp->timer.function = addrconf_dad_timer;
328 ifp->timer.function = addrconf_rs_timer;
332 ifp->timer.expires = jiffies + when;
333 add_timer(&ifp->timer);
336 /* Nobody refers to this device, we may destroy it. */
338 void in6_dev_finish_destroy(struct inet6_dev *idev)
340 struct net_device *dev = idev->dev;
341 BUG_TRAP(idev->addr_list==NULL);
342 BUG_TRAP(idev->mc_list==NULL);
343 #ifdef NET_REFCNT_DEBUG
344 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
348 printk("Freeing alive inet6 device %p\n", idev);
351 snmp6_unregister_dev(idev);
356 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
358 struct inet6_dev *ndev;
362 if (dev->mtu < IPV6_MIN_MTU)
365 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
368 memset(ndev, 0, sizeof(struct inet6_dev));
370 ndev->lock = RW_LOCK_UNLOCKED;
372 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
373 ndev->cnf.mtu6 = dev->mtu;
374 ndev->cnf.sysctl = NULL;
375 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
376 if (ndev->nd_parms == NULL) {
381 /* We refer to the device */
384 if (snmp6_register_dev(ndev) < 0) {
386 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
387 __FUNCTION__, dev->name));
388 neigh_parms_release(&nd_tbl, ndev->nd_parms);
390 in6_dev_finish_destroy(ndev);
394 /* One reference from device. We must do this before
395 * we invoke __ipv6_regen_rndid().
399 #ifdef CONFIG_IPV6_PRIVACY
400 get_random_bytes(ndev->rndid, sizeof(ndev->rndid));
401 get_random_bytes(ndev->entropy, sizeof(ndev->entropy));
402 init_timer(&ndev->regen_timer);
403 ndev->regen_timer.function = ipv6_regen_rndid;
404 ndev->regen_timer.data = (unsigned long) ndev;
405 if ((dev->flags&IFF_LOOPBACK) ||
406 dev->type == ARPHRD_TUNNEL ||
407 dev->type == ARPHRD_SIT) {
409 "Disabled Privacy Extensions on device %p(%s)\n",
411 ndev->cnf.use_tempaddr = -1;
414 ipv6_regen_rndid((unsigned long) ndev);
418 write_lock_bh(&addrconf_lock);
420 write_unlock_bh(&addrconf_lock);
422 ipv6_mc_init_dev(ndev);
423 ndev->tstamp = jiffies;
425 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
426 NET_IPV6_NEIGH, "ipv6", &ndisc_ifinfo_sysctl_change);
427 addrconf_sysctl_register(ndev, &ndev->cnf);
433 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
435 struct inet6_dev *idev;
439 if ((idev = __in6_dev_get(dev)) == NULL) {
440 if ((idev = ipv6_add_dev(dev)) == NULL)
443 if (dev->flags&IFF_UP)
448 static void dev_forward_change(struct inet6_dev *idev)
450 struct net_device *dev;
451 struct inet6_ifaddr *ifa;
452 struct in6_addr addr;
457 if (dev && (dev->flags & IFF_MULTICAST)) {
458 ipv6_addr_all_routers(&addr);
460 if (idev->cnf.forwarding)
461 ipv6_dev_mc_inc(dev, &addr);
463 ipv6_dev_mc_dec(dev, &addr);
465 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
466 ipv6_addr_prefix(&addr, &ifa->addr, ifa->prefix_len);
467 if (ipv6_addr_any(&addr))
469 if (idev->cnf.forwarding)
470 ipv6_dev_ac_inc(idev->dev, &addr);
472 ipv6_dev_ac_dec(idev->dev, &addr);
477 static void addrconf_forward_change(struct inet6_dev *idev)
479 struct net_device *dev;
482 dev_forward_change(idev);
486 read_lock(&dev_base_lock);
487 for (dev=dev_base; dev; dev=dev->next) {
488 read_lock(&addrconf_lock);
489 idev = __in6_dev_get(dev);
491 idev->cnf.forwarding = ipv6_devconf.forwarding;
492 dev_forward_change(idev);
494 read_unlock(&addrconf_lock);
496 read_unlock(&dev_base_lock);
500 /* Nobody refers to this ifaddr, destroy it */
502 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
504 BUG_TRAP(ifp->if_next==NULL);
505 BUG_TRAP(ifp->lst_next==NULL);
506 #ifdef NET_REFCNT_DEBUG
507 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
510 in6_dev_put(ifp->idev);
512 if (del_timer(&ifp->timer))
513 printk("Timer is still running, when freeing ifa=%p\n", ifp);
516 printk("Freeing alive inet6 address %p\n", ifp);
519 dst_release(&ifp->rt->u.dst);
525 /* On success it returns ifp with increased reference count */
527 static struct inet6_ifaddr *
528 ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
529 int scope, unsigned flags)
531 struct inet6_ifaddr *ifa = NULL;
534 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
539 /* Ignore adding duplicate addresses on an interface */
540 if (ipv6_chk_same_addr(addr, idev->dev)) {
541 ADBG(("ipv6_add_addr: already assigned\n"));
546 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
549 ADBG(("ipv6_add_addr: malloc failed\n"));
554 rt = addrconf_dst_alloc(idev, addr, 0);
560 memset(ifa, 0, sizeof(struct inet6_ifaddr));
561 ipv6_addr_copy(&ifa->addr, addr);
563 spin_lock_init(&ifa->lock);
564 init_timer(&ifa->timer);
565 ifa->timer.data = (unsigned long) ifa;
567 ifa->prefix_len = pfxlen;
568 ifa->flags = flags | IFA_F_TENTATIVE;
569 ifa->cstamp = ifa->tstamp = jiffies;
571 read_lock(&addrconf_lock);
573 read_unlock(&addrconf_lock);
574 err = -ENODEV; /*XXX*/
584 /* Add to big hash table */
585 hash = ipv6_addr_hash(addr);
587 write_lock_bh(&addrconf_hash_lock);
588 ifa->lst_next = inet6_addr_lst[hash];
589 inet6_addr_lst[hash] = ifa;
591 write_unlock_bh(&addrconf_hash_lock);
593 write_lock_bh(&idev->lock);
594 /* Add to inet6_dev unicast addr list. */
595 ifa->if_next = idev->addr_list;
596 idev->addr_list = ifa;
598 #ifdef CONFIG_IPV6_PRIVACY
599 ifa->regen_count = 0;
600 if (ifa->flags&IFA_F_TEMPORARY) {
601 ifa->tmp_next = idev->tempaddr_list;
602 idev->tempaddr_list = ifa;
605 ifa->tmp_next = NULL;
612 write_unlock_bh(&idev->lock);
613 read_unlock(&addrconf_lock);
615 spin_unlock_bh(&lock);
617 if (unlikely(err == 0))
618 notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
627 /* This function wants to get referenced ifp and releases it before return */
629 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
631 struct inet6_ifaddr *ifa, **ifap;
632 struct inet6_dev *idev = ifp->idev;
635 hash = ipv6_addr_hash(&ifp->addr);
639 write_lock_bh(&addrconf_hash_lock);
640 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
641 ifap = &ifa->lst_next) {
643 *ifap = ifa->lst_next;
645 ifa->lst_next = NULL;
649 write_unlock_bh(&addrconf_hash_lock);
651 write_lock_bh(&idev->lock);
652 #ifdef CONFIG_IPV6_PRIVACY
653 if (ifp->flags&IFA_F_TEMPORARY) {
654 for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
655 ifap = &ifa->tmp_next) {
657 *ifap = ifa->tmp_next;
659 in6_ifa_put(ifp->ifpub);
663 ifa->tmp_next = NULL;
670 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;
671 ifap = &ifa->if_next) {
673 *ifap = ifa->if_next;
679 write_unlock_bh(&idev->lock);
681 ipv6_ifa_notify(RTM_DELADDR, ifp);
683 notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
685 addrconf_del_timer(ifp);
690 #ifdef CONFIG_IPV6_PRIVACY
691 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
693 struct inet6_dev *idev;
694 struct in6_addr addr, *tmpaddr;
695 unsigned long tmp_prefered_lft, tmp_valid_lft;
701 spin_lock_bh(&ift->lock);
702 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
703 spin_unlock_bh(&ift->lock);
709 spin_lock_bh(&ifp->lock);
713 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
714 write_lock(&idev->lock);
715 if (idev->cnf.use_tempaddr <= 0) {
716 write_unlock(&idev->lock);
717 spin_unlock_bh(&ifp->lock);
719 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
725 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
726 idev->cnf.use_tempaddr = -1; /*XXX*/
727 write_unlock(&idev->lock);
728 spin_unlock_bh(&ifp->lock);
730 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
736 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
737 write_unlock(&idev->lock);
738 spin_unlock_bh(&ifp->lock);
740 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
746 memcpy(&addr.s6_addr[8], idev->rndid, 8);
747 tmp_valid_lft = min_t(__u32,
749 idev->cnf.temp_valid_lft);
750 tmp_prefered_lft = min_t(__u32,
752 idev->cnf.temp_prefered_lft - desync_factor / HZ);
753 tmp_plen = ifp->prefix_len;
754 max_addresses = idev->cnf.max_addresses;
755 write_unlock(&idev->lock);
756 spin_unlock_bh(&ifp->lock);
757 ift = !max_addresses ||
758 ipv6_count_addresses(idev) < max_addresses ?
759 ipv6_add_addr(idev, &addr, tmp_plen,
760 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
761 if (!ift || IS_ERR(ift)) {
765 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
769 spin_lock_bh(&ift->lock);
771 ift->valid_lft = tmp_valid_lft;
772 ift->prefered_lft = tmp_prefered_lft;
773 ift->cstamp = ifp->cstamp;
774 ift->tstamp = ifp->tstamp;
775 spin_unlock_bh(&ift->lock);
776 addrconf_dad_start(ift, 0);
786 * Choose an appropriate source address
787 * draft-ietf-ipv6-default-addr-select-09.txt
789 * i) get an address with an appropriate scope
790 * ii) see if there is a specific route for the destination and use
791 * an address of the attached interface
792 * iii) don't use deprecated addresses
794 #define IPV6_SADDRSELECT_SELF 0x01
795 #define IPV6_SADDRSELECT_PREFERRED 0x02
796 #define IPV6_SADDRSELECT_HOME 0x04
797 #define IPV6_SADDRSELECT_PUBLIC 0x08
798 #define IPV6_SADDRSELECT_INTERFACE 0x10
799 #define IPV6_SADDRSELECT_LABEL 0x20
801 struct addrselect_attrs {
802 struct inet6_ifaddr *ifp;
808 static int __inline__ ipv6_addrselect_preferred(int type)
810 /* section 3.3, 3.4 */
811 if (type&(IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
812 IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
817 int ipv6_dev_get_saddr(struct net_device *daddr_dev,
818 struct in6_addr *daddr, struct in6_addr *saddr)
820 int daddr_type, daddr_scope;
822 struct inet6_ifaddr *ifp0, *ifp = NULL;
823 struct net_device *dev;
824 struct inet6_dev *idev;
828 struct addrselect_attrs candidate = {NULL,0,0,0};
829 #if defined(CONFIG_IPV6_PRIVACY)
833 #ifdef CONFIG_IPV6_PRIVACY
834 if (ipv6_devconf.use_tempaddr > 1)
835 invpref |= IPV6_SADDRSELECT_PUBLIC;
838 daddr_type = __ipv6_addr_type(daddr);
839 daddr_scope = __ipv6_addr_src_scope(daddr_type);
840 daddr_label = ipv6_addrselect_label_lookup(daddr,
841 daddr_dev?daddr_dev->ifindex:0);
843 read_lock(&dev_base_lock);
844 read_lock(&addrconf_lock);
845 for (dev = dev_base; dev; dev=dev->next) {
846 /* Rule 0: Candidate Source Address (section 4)
847 * - multicast and link-local destination address,
848 * the set of candidate source address MUST only
849 * include addresses assigned to interfaces
850 * belonging to the same link as the outgoing
852 * (- For site-local destination addresses, the
853 * set of candidate source addresses MUST only
854 * include addresses assigned to interfaces
855 * belonging to the same site as the outgoing
858 if ((daddr_type&IPV6_ADDR_MULTICAST ||
859 daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
860 daddr_dev && dev != daddr_dev)
863 idev = __in6_dev_get(dev);
867 read_lock_bh(&idev->lock);
868 ifp0 = idev->addr_list;
869 for (ifp=ifp0; ifp; ifp=ifp->if_next) {
870 struct addrselect_attrs temp = {NULL,0,0,0};
874 /* Rule 0: Candidate Source Address (section 4)
875 * - In any case, anycast addresses, multicast
876 * addresses, and the unspecified address MUST
877 * NOT be included in a candidate set.
879 addr_type = __ipv6_addr_type(&ifp->addr);
880 if (addr_type == IPV6_ADDR_ANY ||
881 addr_type&IPV6_ADDR_MULTICAST)
884 /* Rule 1: Prefer same address */
885 if (ipv6_addr_cmp(&ifp->addr, daddr) == 0)
886 temp.flags |= IPV6_SADDRSELECT_SELF;
887 if ((temp.flags^candidate.flags)&IPV6_SADDRSELECT_SELF) {
888 update = temp.flags&IPV6_SADDRSELECT_SELF;
894 /* Rule 2: Prefer appropriate scope */
895 temp.scope = __ipv6_addr_src_scope(addr_type);
897 update = temp.scope - candidate.scope;
899 update = candidate.scope < daddr_scope ? 1 : -1;
900 } else if (update < 0) {
901 update = temp.scope < daddr_scope ? -1 : 1;
908 /* Rule 3: Avoid deprecated address */
909 if (ipv6_addrselect_preferred(addr_type) ||
910 !(ifp->flags & IFA_F_DEPRECATED))
911 temp.flags |= IPV6_SADDRSELECT_PREFERRED;
912 if (!update && (temp.flags^candidate.flags)&IPV6_SADDRSELECT_PREFERRED) {
913 update = temp.flags&IPV6_SADDRSELECT_PREFERRED;
919 /* XXX: Rule 4: Prefer home address */
921 /* Rule 5: Prefer outgoing interface */
922 if (daddr_dev == NULL || daddr_dev == dev)
923 temp.flags |= IPV6_SADDRSELECT_INTERFACE;
924 if (!update && (temp.flags^candidate.flags)&IPV6_SADDRSELECT_INTERFACE) {
925 update = temp.flags&IPV6_SADDRSELECT_INTERFACE;
931 /* XXX: Rule 6: Prefer matching label */
932 if (ipv6_addrselect_label_lookup(&ifp->addr, dev->ifindex) == daddr_label)
933 temp.flags |= IPV6_SADDRSELECT_LABEL;
934 if (!update && (temp.flags^candidate.flags)&IPV6_SADDRSELECT_LABEL) {
935 update = temp.flags&IPV6_SADDRSELECT_LABEL;
941 /* XXX: Rule 7: Prefer public address */
942 #ifdef CONFIG_IPV6_PRIVACY
943 if (!(ifp->flags & IFA_F_TEMPORARY))
944 temp.flags |= IPV6_SADDRSELECT_PUBLIC;
945 if (!update && (temp.flags^candidate.flags)&IPV6_SADDRSELECT_PUBLIC) {
946 update = (temp.flags^invpref)&IPV6_SADDRSELECT_PUBLIC;
953 /* Rule 8: Use longest matching prefix */
954 temp.matchlen = ipv6_addr_diff(&ifp->addr, daddr);
956 update = temp.matchlen - candidate.matchlen;
963 if (!update && candidate.ifp) {
967 /* update candidate */
971 in6_ifa_put(candidate.ifp);
974 read_unlock_bh(&idev->lock);
976 read_unlock(&addrconf_lock);
977 read_unlock(&dev_base_lock);
980 ipv6_addr_copy(saddr, &candidate.ifp->addr);
981 in6_ifa_put(candidate.ifp);
984 err = -EADDRNOTAVAIL;
989 int ipv6_get_saddr(struct dst_entry *dst,
990 struct in6_addr *daddr, struct in6_addr *saddr)
992 return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_dev : NULL,
996 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
998 struct inet6_dev *idev;
999 int err = -EADDRNOTAVAIL;
1001 read_lock(&addrconf_lock);
1002 if ((idev = __in6_dev_get(dev)) != NULL) {
1003 struct inet6_ifaddr *ifp;
1005 read_lock_bh(&idev->lock);
1006 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1007 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1008 ipv6_addr_copy(addr, &ifp->addr);
1013 read_unlock_bh(&idev->lock);
1015 read_unlock(&addrconf_lock);
1019 static int ipv6_count_addresses(struct inet6_dev *idev)
1022 struct inet6_ifaddr *ifp;
1024 read_lock_bh(&idev->lock);
1025 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
1027 read_unlock_bh(&idev->lock);
1031 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
1033 struct inet6_ifaddr * ifp;
1034 u8 hash = ipv6_addr_hash(addr);
1036 read_lock_bh(&addrconf_hash_lock);
1037 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1038 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
1039 !(ifp->flags&IFA_F_TENTATIVE)) {
1040 if (dev == NULL || ifp->idev->dev == dev ||
1041 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
1045 read_unlock_bh(&addrconf_hash_lock);
1050 int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
1052 struct inet6_ifaddr * ifp;
1053 u8 hash = ipv6_addr_hash(addr);
1055 read_lock_bh(&addrconf_hash_lock);
1056 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1057 if (ipv6_addr_cmp(&ifp->addr, addr) == 0) {
1058 if (dev == NULL || ifp->idev->dev == dev)
1062 read_unlock_bh(&addrconf_hash_lock);
1066 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
1068 struct inet6_ifaddr * ifp;
1069 u8 hash = ipv6_addr_hash(addr);
1071 read_lock_bh(&addrconf_hash_lock);
1072 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1073 if (ipv6_addr_cmp(&ifp->addr, addr) == 0) {
1074 if (dev == NULL || ifp->idev->dev == dev ||
1075 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1081 read_unlock_bh(&addrconf_hash_lock);
1086 /* address selection: default policy label */
1087 /* XXX: user level configuration */
1088 static struct ipv6_addrselect_label {
1089 struct in6_addr addr;
1093 } ipv6_addrselect_label_table[] = {
1094 /* ::1/128, label = 0 */
1096 .addr = IN6ADDR_LOOPBACK_INIT,
1100 /* ::/0, label = 1 */
1102 .addr = IN6ADDR_ANY_INIT,
1106 /* 2002::/16, label = 2 */
1108 .addr = {{{ 0x20, 0x02 }}},
1112 /* ::/96, label = 3 */
1117 /* ::ffff:0:0/96, label = 4 */
1119 .addr = {{{ [10] = 0xff, [11] = 0xff }}},
1125 .label = 0xffffffff,
1129 static u32 ipv6_addrselect_label_lookup(const struct in6_addr *addr,
1132 struct ipv6_addrselect_label *p;
1133 int plen, matchlen = -1;
1134 u32 label = 0xffffffff;
1136 for (p = ipv6_addrselect_label_table;
1137 p->label != 0xffffffff;
1139 if (ifindex && p->ifindex && ifindex != p->ifindex)
1141 plen = ipv6_addr_diff(addr, &p->addr);
1142 if (plen < p->plen || plen < matchlen)
1150 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
1152 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
1153 const struct in6_addr *sk2_rcv_saddr6 = tcp_v6_rcv_saddr(sk2);
1154 u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
1155 u32 sk2_rcv_saddr = tcp_v4_rcv_saddr(sk2);
1156 int sk_ipv6only = ipv6_only_sock(sk);
1157 int sk2_ipv6only = tcp_v6_ipv6only(sk2);
1158 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
1159 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
1161 if (!sk2_rcv_saddr && !sk_ipv6only)
1164 if (addr_type2 == IPV6_ADDR_ANY &&
1165 !(sk2_ipv6only && (addr_type & IPV6_ADDR_MAPPED)))
1168 if (addr_type == IPV6_ADDR_ANY &&
1169 !(sk_ipv6only && (addr_type2 & IPV6_ADDR_MAPPED)))
1172 if (sk2_rcv_saddr6 &&
1173 !ipv6_addr_cmp(sk_rcv_saddr6, sk2_rcv_saddr6))
1176 if ((addr_type & IPV6_ADDR_MAPPED) &&
1178 (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
1184 /* Gets referenced address, destroys ifaddr */
1186 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1188 if (net_ratelimit())
1189 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
1190 if (ifp->flags&IFA_F_PERMANENT) {
1191 spin_lock_bh(&ifp->lock);
1192 addrconf_del_timer(ifp);
1193 ifp->flags |= IFA_F_TENTATIVE;
1194 spin_unlock_bh(&ifp->lock);
1196 #ifdef CONFIG_IPV6_PRIVACY
1197 } else if (ifp->flags&IFA_F_TEMPORARY) {
1198 struct inet6_ifaddr *ifpub;
1199 spin_lock_bh(&ifp->lock);
1202 in6_ifa_hold(ifpub);
1203 spin_unlock_bh(&ifp->lock);
1204 ipv6_create_tempaddr(ifpub, ifp);
1207 spin_unlock_bh(&ifp->lock);
1216 /* Join to solicited addr multicast group. */
1218 void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
1220 struct in6_addr maddr;
1222 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1225 addrconf_addr_solict_mult(addr, &maddr);
1226 ipv6_dev_mc_inc(dev, &maddr);
1229 void addrconf_leave_solict(struct net_device *dev, struct in6_addr *addr)
1231 struct in6_addr maddr;
1233 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1236 addrconf_addr_solict_mult(addr, &maddr);
1237 ipv6_dev_mc_dec(dev, &maddr);
1241 int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1243 switch (dev->type) {
1246 case ARPHRD_IEEE802_TR:
1247 if (dev->addr_len != ETH_ALEN)
1249 memcpy(eui, dev->dev_addr, 3);
1250 memcpy(eui + 5, dev->dev_addr+3, 3);
1256 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1257 if (dev->addr_len != ARCNET_ALEN)
1260 eui[7] = *(u8*)dev->dev_addr;
1266 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1269 struct inet6_ifaddr *ifp;
1271 read_lock_bh(&idev->lock);
1272 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
1273 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1274 memcpy(eui, ifp->addr.s6_addr+8, 8);
1279 read_unlock_bh(&idev->lock);
1283 #ifdef CONFIG_IPV6_PRIVACY
1284 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1285 static int __ipv6_regen_rndid(struct inet6_dev *idev)
1287 struct net_device *dev;
1288 struct scatterlist sg[2];
1290 sg[0].page = virt_to_page(idev->entropy);
1291 sg[0].offset = offset_in_page(idev->entropy);
1293 sg[1].page = virt_to_page(idev->work_eui64);
1294 sg[1].offset = offset_in_page(idev->work_eui64);
1299 if (dev->generate_eui64(idev->work_eui64, dev)) {
1301 "__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
1303 get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
1306 spin_lock(&md5_tfm_lock);
1307 if (unlikely(md5_tfm == NULL)) {
1308 spin_unlock(&md5_tfm_lock);
1311 crypto_digest_init(md5_tfm);
1312 crypto_digest_update(md5_tfm, sg, 2);
1313 crypto_digest_final(md5_tfm, idev->work_digest);
1314 spin_unlock(&md5_tfm_lock);
1316 memcpy(idev->rndid, &idev->work_digest[0], 8);
1317 idev->rndid[0] &= ~0x02;
1318 memcpy(idev->entropy, &idev->work_digest[8], 8);
1321 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1322 * check if generated address is not inappropriate
1324 * - Reserved subnet anycast (RFC 2526)
1325 * 11111101 11....11 1xxxxxxx
1326 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1327 * 00-00-5E-FE-xx-xx-xx-xx
1329 * - XXX: already assigned to an address on the device
1331 if (idev->rndid[0] == 0xfd &&
1332 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1333 (idev->rndid[7]&0x80))
1335 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1336 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1338 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1345 static void ipv6_regen_rndid(unsigned long data)
1347 struct inet6_dev *idev = (struct inet6_dev *) data;
1348 unsigned long expires;
1350 read_lock_bh(&addrconf_lock);
1351 write_lock_bh(&idev->lock);
1356 if (__ipv6_regen_rndid(idev) < 0)
1360 idev->cnf.temp_prefered_lft * HZ -
1361 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
1362 if (time_before(expires, jiffies)) {
1364 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1369 if (!mod_timer(&idev->regen_timer, expires))
1373 write_unlock_bh(&idev->lock);
1374 read_unlock_bh(&addrconf_lock);
1378 static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1381 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1382 ret = __ipv6_regen_rndid(idev);
1392 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1393 unsigned long expires, unsigned flags)
1395 struct in6_rtmsg rtmsg;
1397 memset(&rtmsg, 0, sizeof(rtmsg));
1398 ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
1399 rtmsg.rtmsg_dst_len = plen;
1400 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1401 rtmsg.rtmsg_ifindex = dev->ifindex;
1402 rtmsg.rtmsg_info = expires;
1403 rtmsg.rtmsg_flags = RTF_UP|flags;
1404 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1406 /* Prevent useless cloning on PtP SIT.
1407 This thing is done here expecting that the whole
1408 class of non-broadcast devices need not cloning.
1410 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
1411 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
1413 ip6_route_add(&rtmsg, NULL, NULL);
1416 /* Create "default" multicast route to the interface */
1418 static void addrconf_add_mroute(struct net_device *dev)
1420 struct in6_rtmsg rtmsg;
1422 memset(&rtmsg, 0, sizeof(rtmsg));
1423 ipv6_addr_set(&rtmsg.rtmsg_dst,
1424 htonl(0xFF000000), 0, 0, 0);
1425 rtmsg.rtmsg_dst_len = 8;
1426 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1427 rtmsg.rtmsg_ifindex = dev->ifindex;
1428 rtmsg.rtmsg_flags = RTF_UP;
1429 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1430 ip6_route_add(&rtmsg, NULL, NULL);
1433 static void sit_route_add(struct net_device *dev)
1435 struct in6_rtmsg rtmsg;
1437 memset(&rtmsg, 0, sizeof(rtmsg));
1439 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1440 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1442 /* prefix length - 96 bits "::d.d.d.d" */
1443 rtmsg.rtmsg_dst_len = 96;
1444 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
1445 rtmsg.rtmsg_ifindex = dev->ifindex;
1447 ip6_route_add(&rtmsg, NULL, NULL);
1450 static void addrconf_add_lroute(struct net_device *dev)
1452 struct in6_addr addr;
1454 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1455 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1458 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1460 struct inet6_dev *idev;
1464 if ((idev = ipv6_find_idev(dev)) == NULL)
1467 /* Add default multicast route */
1468 addrconf_add_mroute(dev);
1470 /* Add link local route */
1471 addrconf_add_lroute(dev);
1475 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
1477 struct prefix_info *pinfo;
1481 unsigned long rt_expires;
1482 struct inet6_dev *in6_dev;
1484 pinfo = (struct prefix_info *) opt;
1486 if (len < sizeof(struct prefix_info)) {
1487 ADBG(("addrconf: prefix option too short\n"));
1492 * Validation checks ([ADDRCONF], page 19)
1495 addr_type = ipv6_addr_type(&pinfo->prefix);
1497 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1500 valid_lft = ntohl(pinfo->valid);
1501 prefered_lft = ntohl(pinfo->prefered);
1503 if (prefered_lft > valid_lft) {
1504 if (net_ratelimit())
1505 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1509 in6_dev = in6_dev_get(dev);
1511 if (in6_dev == NULL) {
1512 if (net_ratelimit())
1513 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1518 * Two things going on here:
1519 * 1) Add routes for on-link prefixes
1520 * 2) Configure prefixes with the auto flag set
1523 /* Avoid arithmetic overflow. Really, we could
1524 save rt_expires in seconds, likely valid_lft,
1525 but it would require division in fib gc, that it
1528 if (valid_lft >= 0x7FFFFFFF/HZ)
1531 rt_expires = jiffies + valid_lft * HZ;
1533 if (pinfo->onlink) {
1534 struct rt6_info *rt;
1535 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
1537 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
1538 if (rt->rt6i_flags&RTF_EXPIRES) {
1539 if (valid_lft == 0) {
1540 ip6_del_rt(rt, NULL, NULL);
1543 rt->rt6i_expires = rt_expires;
1546 } else if (valid_lft) {
1547 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
1548 dev, rt_expires, RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
1551 dst_release(&rt->u.dst);
1554 /* Try to figure out our local address for this prefix */
1556 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1557 struct inet6_ifaddr * ifp;
1558 struct in6_addr addr;
1559 int create = 0, update_lft = 0;
1561 if (pinfo->prefix_len == 64) {
1562 memcpy(&addr, &pinfo->prefix, 8);
1563 if (dev->generate_eui64(addr.s6_addr + 8, dev) &&
1564 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1565 in6_dev_put(in6_dev);
1570 if (net_ratelimit())
1571 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1573 in6_dev_put(in6_dev);
1578 ifp = ipv6_get_ifaddr(&addr, dev, 1);
1580 if (ifp == NULL && valid_lft) {
1581 int max_addresses = in6_dev->cnf.max_addresses;
1583 /* Do not allow to create too much of autoconfigured
1584 * addresses; this would be too easy way to crash kernel.
1586 if (!max_addresses ||
1587 ipv6_count_addresses(in6_dev) < max_addresses)
1588 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
1589 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
1591 if (!ifp || IS_ERR(ifp)) {
1592 in6_dev_put(in6_dev);
1596 update_lft = create = 1;
1597 ifp->cstamp = jiffies;
1598 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1604 #ifdef CONFIG_IPV6_PRIVACY
1605 struct inet6_ifaddr *ift;
1609 /* update lifetime (RFC2462 5.5.3 e) */
1610 spin_lock(&ifp->lock);
1612 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1613 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1616 if (!update_lft && stored_lft) {
1617 if (valid_lft > MIN_VALID_LIFETIME ||
1618 valid_lft > stored_lft)
1620 else if (stored_lft <= MIN_VALID_LIFETIME) {
1621 /* valid_lft <= stored_lft is always true */
1625 valid_lft = MIN_VALID_LIFETIME;
1626 if (valid_lft < prefered_lft)
1627 prefered_lft = valid_lft;
1633 ifp->valid_lft = valid_lft;
1634 ifp->prefered_lft = prefered_lft;
1637 ifp->flags &= ~IFA_F_DEPRECATED;
1638 spin_unlock(&ifp->lock);
1640 if (!(flags&IFA_F_TENTATIVE))
1641 ipv6_ifa_notify(0, ifp);
1643 spin_unlock(&ifp->lock);
1645 #ifdef CONFIG_IPV6_PRIVACY
1646 read_lock_bh(&in6_dev->lock);
1647 /* update all temporary addresses in the list */
1648 for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
1650 * When adjusting the lifetimes of an existing
1651 * temporary address, only lower the lifetimes.
1652 * Implementations must not increase the
1653 * lifetimes of an existing temporary address
1654 * when processing a Prefix Information Option.
1656 spin_lock(&ift->lock);
1658 if (ift->valid_lft > valid_lft &&
1659 ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
1660 ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
1661 if (ift->prefered_lft > prefered_lft &&
1662 ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
1663 ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
1664 spin_unlock(&ift->lock);
1665 if (!(flags&IFA_F_TENTATIVE))
1666 ipv6_ifa_notify(0, ift);
1669 if (create && in6_dev->cnf.use_tempaddr > 0) {
1671 * When a new public address is created as described in [ADDRCONF],
1672 * also create a new temporary address.
1674 read_unlock_bh(&in6_dev->lock);
1675 ipv6_create_tempaddr(ifp, NULL);
1677 read_unlock_bh(&in6_dev->lock);
1684 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
1685 in6_dev_put(in6_dev);
1689 * Set destination address.
1690 * Special case for SIT interfaces where we create a new "virtual"
1693 int addrconf_set_dstaddr(void __user *arg)
1695 struct in6_ifreq ireq;
1696 struct net_device *dev;
1702 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1705 dev = __dev_get_by_index(ireq.ifr6_ifindex);
1711 if (dev->type == ARPHRD_SIT) {
1714 struct ip_tunnel_parm p;
1716 err = -EADDRNOTAVAIL;
1717 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
1720 memset(&p, 0, sizeof(p));
1721 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
1725 p.iph.protocol = IPPROTO_IPV6;
1727 ifr.ifr_ifru.ifru_data = (void __user *)&p;
1729 oldfs = get_fs(); set_fs(KERNEL_DS);
1730 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
1735 if ((dev = __dev_get_by_name(p.name)) == NULL)
1737 err = dev_open(dev);
1747 * Manual configuration of address on an interface
1749 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1751 struct inet6_ifaddr *ifp;
1752 struct inet6_dev *idev;
1753 struct net_device *dev;
1758 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1761 if (!(dev->flags&IFF_UP))
1764 if ((idev = addrconf_add_dev(dev)) == NULL)
1767 scope = ipv6_addr_scope(pfx);
1769 ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
1771 addrconf_dad_start(ifp, 0);
1776 return PTR_ERR(ifp);
1779 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1781 struct inet6_ifaddr *ifp;
1782 struct inet6_dev *idev;
1783 struct net_device *dev;
1785 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1788 if ((idev = __in6_dev_get(dev)) == NULL)
1791 read_lock_bh(&idev->lock);
1792 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1793 if (ifp->prefix_len == plen &&
1794 (!memcmp(pfx, &ifp->addr, sizeof(struct in6_addr)))) {
1796 read_unlock_bh(&idev->lock);
1800 /* If the last address is deleted administratively,
1801 disable IPv6 on this interface.
1803 if (idev->addr_list == NULL)
1804 addrconf_ifdown(idev->dev, 1);
1808 read_unlock_bh(&idev->lock);
1809 return -EADDRNOTAVAIL;
1813 int addrconf_add_ifaddr(void __user *arg)
1815 struct in6_ifreq ireq;
1818 if (!capable(CAP_NET_ADMIN))
1821 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1825 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1830 int addrconf_del_ifaddr(void __user *arg)
1832 struct in6_ifreq ireq;
1835 if (!capable(CAP_NET_ADMIN))
1838 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1842 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1847 static void sit_add_v4_addrs(struct inet6_dev *idev)
1849 struct inet6_ifaddr * ifp;
1850 struct in6_addr addr;
1851 struct net_device *dev;
1856 memset(&addr, 0, sizeof(struct in6_addr));
1857 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1859 if (idev->dev->flags&IFF_POINTOPOINT) {
1860 addr.s6_addr32[0] = htonl(0xfe800000);
1863 scope = IPV6_ADDR_COMPATv4;
1866 if (addr.s6_addr32[3]) {
1867 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1869 spin_lock_bh(&ifp->lock);
1870 ifp->flags &= ~IFA_F_TENTATIVE;
1871 spin_unlock_bh(&ifp->lock);
1872 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1878 for (dev = dev_base; dev != NULL; dev = dev->next) {
1879 struct in_device * in_dev = __in_dev_get(dev);
1880 if (in_dev && (dev->flags & IFF_UP)) {
1881 struct in_ifaddr * ifa;
1885 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1888 addr.s6_addr32[3] = ifa->ifa_local;
1890 if (ifa->ifa_scope == RT_SCOPE_LINK)
1892 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1893 if (idev->dev->flags&IFF_POINTOPOINT)
1897 if (idev->dev->flags&IFF_POINTOPOINT)
1902 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1905 spin_lock_bh(&ifp->lock);
1906 ifp->flags &= ~IFA_F_TENTATIVE;
1907 spin_unlock_bh(&ifp->lock);
1908 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1916 static void init_loopback(struct net_device *dev)
1918 struct inet6_dev *idev;
1919 struct inet6_ifaddr * ifp;
1925 if ((idev = ipv6_find_idev(dev)) == NULL) {
1926 printk(KERN_DEBUG "init loopback: add_dev failed\n");
1930 ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
1932 spin_lock_bh(&ifp->lock);
1933 ifp->flags &= ~IFA_F_TENTATIVE;
1934 spin_unlock_bh(&ifp->lock);
1935 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1940 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
1942 struct inet6_ifaddr * ifp;
1944 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
1946 addrconf_dad_start(ifp, 0);
1951 static void addrconf_dev_config(struct net_device *dev)
1953 struct in6_addr addr;
1954 struct inet6_dev * idev;
1958 if ((dev->type != ARPHRD_ETHER) &&
1959 (dev->type != ARPHRD_FDDI) &&
1960 (dev->type != ARPHRD_IEEE802_TR) &&
1961 (dev->type != ARPHRD_ARCNET)) {
1962 /* Alas, we support only Ethernet autoconfiguration. */
1966 if (!dev->generate_eui64)
1967 dev->generate_eui64 = ipv6_generate_eui64;
1969 idev = addrconf_add_dev(dev);
1973 memset(&addr, 0, sizeof(struct in6_addr));
1974 addr.s6_addr32[0] = htonl(0xFE800000);
1975 if (dev->generate_eui64(addr.s6_addr + 8, dev) == 0)
1976 addrconf_add_linklocal(idev, &addr);
1979 static void addrconf_sit_config(struct net_device *dev)
1981 struct inet6_dev *idev;
1986 * Configure the tunnel with one of our IPv4
1987 * addresses... we should configure all of
1988 * our v4 addrs in the tunnel
1991 if ((idev = ipv6_find_idev(dev)) == NULL) {
1992 printk(KERN_DEBUG "init sit: add_dev failed\n");
1996 sit_add_v4_addrs(idev);
1998 if (dev->flags&IFF_POINTOPOINT) {
1999 addrconf_add_mroute(dev);
2000 addrconf_add_lroute(dev);
2006 ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2008 struct in6_addr lladdr;
2010 if (!ipv6_get_lladdr(link_dev, &lladdr)) {
2011 addrconf_add_linklocal(idev, &lladdr);
2017 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2019 struct net_device *link_dev;
2021 /* first try to inherit the link-local address from the link device */
2022 if (idev->dev->iflink &&
2023 (link_dev = __dev_get_by_index(idev->dev->iflink))) {
2024 if (!ipv6_inherit_linklocal(idev, link_dev))
2027 /* then try to inherit it from any device */
2028 for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
2029 if (!ipv6_inherit_linklocal(idev, link_dev))
2032 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2036 * Autoconfigure tunnel with a link-local address so routing protocols,
2037 * DHCPv6, MLD etc. can be run over the virtual link
2040 static void addrconf_ip6_tnl_config(struct net_device *dev)
2042 struct inet6_dev *idev;
2046 if ((idev = addrconf_add_dev(dev)) == NULL) {
2047 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2050 ip6_tnl_add_linklocal(idev);
2051 addrconf_add_mroute(dev);
2054 static int addrconf_notify(struct notifier_block *this, unsigned long event,
2057 struct net_device *dev = (struct net_device *) data;
2058 struct inet6_dev *idev = __in6_dev_get(dev);
2064 addrconf_sit_config(dev);
2066 case ARPHRD_TUNNEL6:
2067 addrconf_ip6_tnl_config(dev);
2069 case ARPHRD_LOOPBACK:
2074 addrconf_dev_config(dev);
2078 /* If the MTU changed during the interface down, when the
2079 interface up, the changed MTU must be reflected in the
2080 idev as well as routers.
2082 if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
2083 rt6_mtu_change(dev, dev->mtu);
2084 idev->cnf.mtu6 = dev->mtu;
2086 idev->tstamp = jiffies;
2087 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2088 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2089 stop IPv6 on this interface.
2091 if (dev->mtu < IPV6_MIN_MTU)
2092 addrconf_ifdown(dev, event != NETDEV_DOWN);
2096 case NETDEV_CHANGEMTU:
2097 if ( idev && dev->mtu >= IPV6_MIN_MTU) {
2098 rt6_mtu_change(dev, dev->mtu);
2099 idev->cnf.mtu6 = dev->mtu;
2103 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2106 case NETDEV_UNREGISTER:
2108 * Remove all addresses from this interface.
2110 addrconf_ifdown(dev, event != NETDEV_DOWN);
2114 case NETDEV_CHANGENAME:
2115 #ifdef CONFIG_SYSCTL
2117 addrconf_sysctl_unregister(&idev->cnf);
2118 neigh_sysctl_unregister(idev->nd_parms);
2119 neigh_sysctl_register(dev, idev->nd_parms, NET_IPV6, NET_IPV6_NEIGH, "ipv6", &ndisc_ifinfo_sysctl_change);
2120 addrconf_sysctl_register(idev, &idev->cnf);
2130 * addrconf module should be notified of a device going up
2132 static struct notifier_block ipv6_dev_notf = {
2133 .notifier_call = addrconf_notify,
2137 static int addrconf_ifdown(struct net_device *dev, int how)
2139 struct inet6_dev *idev;
2140 struct inet6_ifaddr *ifa, **bifa;
2146 neigh_ifdown(&nd_tbl, dev);
2148 idev = __in6_dev_get(dev);
2152 /* Step 1: remove reference to ipv6 device from parent device.
2156 write_lock_bh(&addrconf_lock);
2157 dev->ip6_ptr = NULL;
2159 write_unlock_bh(&addrconf_lock);
2162 /* Step 2: clear hash table */
2163 for (i=0; i<IN6_ADDR_HSIZE; i++) {
2164 bifa = &inet6_addr_lst[i];
2166 write_lock_bh(&addrconf_hash_lock);
2167 while ((ifa = *bifa) != NULL) {
2168 if (ifa->idev == idev) {
2169 *bifa = ifa->lst_next;
2170 ifa->lst_next = NULL;
2171 addrconf_del_timer(ifa);
2175 bifa = &ifa->lst_next;
2177 write_unlock_bh(&addrconf_hash_lock);
2180 write_lock_bh(&idev->lock);
2182 /* Step 3: clear flags for stateless addrconf */
2184 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD);
2186 /* Step 4: clear address list */
2187 #ifdef CONFIG_IPV6_PRIVACY
2188 if (how == 1 && del_timer(&idev->regen_timer))
2191 /* clear tempaddr list */
2192 while ((ifa = idev->tempaddr_list) != NULL) {
2193 idev->tempaddr_list = ifa->tmp_next;
2194 ifa->tmp_next = NULL;
2196 write_unlock_bh(&idev->lock);
2197 spin_lock_bh(&ifa->lock);
2200 in6_ifa_put(ifa->ifpub);
2203 spin_unlock_bh(&ifa->lock);
2205 write_lock_bh(&idev->lock);
2208 while ((ifa = idev->addr_list) != NULL) {
2209 idev->addr_list = ifa->if_next;
2210 ifa->if_next = NULL;
2212 addrconf_del_timer(ifa);
2213 write_unlock_bh(&idev->lock);
2215 ipv6_ifa_notify(RTM_DELADDR, ifa);
2218 write_lock_bh(&idev->lock);
2220 write_unlock_bh(&idev->lock);
2222 /* Step 5: Discard multicast list */
2225 ipv6_mc_destroy_dev(idev);
2229 /* Step 5: netlink notification of this interface */
2230 idev->tstamp = jiffies;
2231 inet6_ifinfo_notify(RTM_NEWLINK, idev);
2233 /* Shot the device (if unregistered) */
2236 #ifdef CONFIG_SYSCTL
2237 addrconf_sysctl_unregister(&idev->cnf);
2238 neigh_sysctl_unregister(idev->nd_parms);
2240 neigh_parms_release(&nd_tbl, idev->nd_parms);
2246 static void addrconf_rs_timer(unsigned long data)
2248 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2250 if (ifp->idev->cnf.forwarding)
2253 if (ifp->idev->if_flags & IF_RA_RCVD) {
2255 * Announcement received after solicitation
2261 spin_lock(&ifp->lock);
2262 if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
2263 struct in6_addr all_routers;
2265 /* The wait after the last probe can be shorter */
2266 addrconf_mod_timer(ifp, AC_RS,
2267 (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
2268 ifp->idev->cnf.rtr_solicit_delay :
2269 ifp->idev->cnf.rtr_solicit_interval);
2270 spin_unlock(&ifp->lock);
2272 ipv6_addr_all_routers(&all_routers);
2274 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2276 struct in6_rtmsg rtmsg;
2278 spin_unlock(&ifp->lock);
2280 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
2281 ifp->idev->dev->name);
2283 memset(&rtmsg, 0, sizeof(struct in6_rtmsg));
2284 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
2285 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
2286 rtmsg.rtmsg_flags = (RTF_ALLONLINK | RTF_DEFAULT | RTF_UP);
2288 rtmsg.rtmsg_ifindex = ifp->idev->dev->ifindex;
2290 ip6_route_add(&rtmsg, NULL, NULL);
2298 * Duplicate Address Detection
2300 static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags)
2302 struct net_device *dev;
2303 unsigned long rand_num;
2305 dev = ifp->idev->dev;
2307 addrconf_join_solict(dev, &ifp->addr);
2309 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
2310 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
2313 net_srandom(ifp->addr.s6_addr32[3]);
2314 rand_num = net_random() % (ifp->idev->cnf.rtr_solicit_delay ? : 1);
2316 spin_lock_bh(&ifp->lock);
2318 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
2319 !(ifp->flags&IFA_F_TENTATIVE)) {
2320 ifp->flags &= ~IFA_F_TENTATIVE;
2321 spin_unlock_bh(&ifp->lock);
2323 addrconf_dad_completed(ifp);
2327 ifp->probes = ifp->idev->cnf.dad_transmits;
2328 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2330 spin_unlock_bh(&ifp->lock);
2333 static void addrconf_dad_timer(unsigned long data)
2335 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
2336 struct in6_addr unspec;
2337 struct in6_addr mcaddr;
2339 spin_lock_bh(&ifp->lock);
2340 if (ifp->probes == 0) {
2342 * DAD was successful
2345 ifp->flags &= ~IFA_F_TENTATIVE;
2346 spin_unlock_bh(&ifp->lock);
2348 addrconf_dad_completed(ifp);
2355 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
2356 spin_unlock_bh(&ifp->lock);
2358 /* send a neighbour solicitation for our addr */
2359 memset(&unspec, 0, sizeof(unspec));
2360 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2361 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
2366 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
2368 struct net_device * dev = ifp->idev->dev;
2371 * Configure the address for reception. Now it is valid.
2374 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2376 /* If added prefix is link local and forwarding is off,
2377 start sending router solicitations.
2380 if (ifp->idev->cnf.forwarding == 0 &&
2381 ifp->idev->cnf.rtr_solicits > 0 &&
2382 (dev->flags&IFF_LOOPBACK) == 0 &&
2383 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
2384 struct in6_addr all_routers;
2386 ipv6_addr_all_routers(&all_routers);
2389 * If a host as already performed a random delay
2390 * [...] as part of DAD [...] there is no need
2391 * to delay again before sending the first RS
2393 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
2395 spin_lock_bh(&ifp->lock);
2397 ifp->idev->if_flags |= IF_RS_SENT;
2398 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
2399 spin_unlock_bh(&ifp->lock);
2402 if (ifp->idev->cnf.forwarding) {
2403 struct in6_addr addr;
2405 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2406 if (!ipv6_addr_any(&addr))
2407 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
2411 #ifdef CONFIG_PROC_FS
2412 struct if6_iter_state {
2416 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
2418 struct inet6_ifaddr *ifa = NULL;
2419 struct if6_iter_state *state = seq->private;
2421 for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
2422 ifa = inet6_addr_lst[state->bucket];
2429 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
2431 struct if6_iter_state *state = seq->private;
2433 ifa = ifa->lst_next;
2435 if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
2436 ifa = inet6_addr_lst[state->bucket];
2442 static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
2444 struct inet6_ifaddr *ifa = if6_get_first(seq);
2447 while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
2449 return pos ? NULL : ifa;
2452 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2454 read_lock_bh(&addrconf_hash_lock);
2455 return if6_get_idx(seq, *pos);
2458 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2460 struct inet6_ifaddr *ifa;
2462 ifa = if6_get_next(seq, v);
2467 static void if6_seq_stop(struct seq_file *seq, void *v)
2469 read_unlock_bh(&addrconf_hash_lock);
2472 static int if6_seq_show(struct seq_file *seq, void *v)
2474 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2476 "%04x%04x%04x%04x%04x%04x%04x%04x %02x %02x %02x %02x %8s\n",
2478 ifp->idev->dev->ifindex,
2482 ifp->idev->dev->name);
2486 static struct seq_operations if6_seq_ops = {
2487 .start = if6_seq_start,
2488 .next = if6_seq_next,
2489 .show = if6_seq_show,
2490 .stop = if6_seq_stop,
2493 static int if6_seq_open(struct inode *inode, struct file *file)
2495 struct seq_file *seq;
2497 struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2501 memset(s, 0, sizeof(*s));
2503 rc = seq_open(file, &if6_seq_ops);
2507 seq = file->private_data;
2516 static struct file_operations if6_fops = {
2517 .owner = THIS_MODULE,
2518 .open = if6_seq_open,
2520 .llseek = seq_lseek,
2521 .release = seq_release_private,
2524 int __init if6_proc_init(void)
2526 if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
2531 void if6_proc_exit(void)
2533 proc_net_remove("if_inet6");
2535 #endif /* CONFIG_PROC_FS */
2538 * Periodic address status verification
2541 static void addrconf_verify(unsigned long foo)
2543 struct inet6_ifaddr *ifp;
2544 unsigned long now, next;
2547 spin_lock_bh(&addrconf_verify_lock);
2549 next = now + ADDR_CHECK_FREQUENCY;
2551 del_timer(&addr_chk_timer);
2553 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2556 write_lock(&addrconf_hash_lock);
2557 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
2559 #ifdef CONFIG_IPV6_PRIVACY
2560 unsigned long regen_advance;
2563 if (ifp->flags & IFA_F_PERMANENT)
2566 spin_lock(&ifp->lock);
2567 age = (now - ifp->tstamp) / HZ;
2569 #ifdef CONFIG_IPV6_PRIVACY
2570 regen_advance = ifp->idev->cnf.regen_max_retry *
2571 ifp->idev->cnf.dad_transmits *
2572 ifp->idev->nd_parms->retrans_time / HZ;
2575 if (age >= ifp->valid_lft) {
2576 spin_unlock(&ifp->lock);
2578 write_unlock(&addrconf_hash_lock);
2581 } else if (age >= ifp->prefered_lft) {
2582 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2585 if (!(ifp->flags&IFA_F_DEPRECATED)) {
2587 ifp->flags |= IFA_F_DEPRECATED;
2590 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
2591 next = ifp->tstamp + ifp->valid_lft * HZ;
2593 spin_unlock(&ifp->lock);
2597 write_unlock(&addrconf_hash_lock);
2599 ipv6_ifa_notify(0, ifp);
2603 #ifdef CONFIG_IPV6_PRIVACY
2604 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
2605 !(ifp->flags&IFA_F_TENTATIVE) &&
2606 age >= ifp->prefered_lft - regen_advance) {
2607 struct inet6_ifaddr *ifpub = ifp->ifpub;
2608 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2609 next = ifp->tstamp + ifp->prefered_lft * HZ;
2610 if (!ifp->regen_count && ifpub) {
2613 in6_ifa_hold(ifpub);
2614 spin_unlock(&ifp->lock);
2615 write_unlock(&addrconf_hash_lock);
2616 ipv6_create_tempaddr(ifpub, ifp);
2621 spin_unlock(&ifp->lock);
2625 /* ifp->prefered_lft <= ifp->valid_lft */
2626 #ifdef CONFIG_IPV6_PRIVACY
2627 if (ifp->flags&IFA_F_TEMPORARY) {
2628 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
2629 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
2632 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
2633 next = ifp->tstamp + ifp->prefered_lft * HZ;
2634 spin_unlock(&ifp->lock);
2637 write_unlock(&addrconf_hash_lock);
2640 addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
2641 add_timer(&addr_chk_timer);
2642 spin_unlock_bh(&addrconf_verify_lock);
2646 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2648 struct rtattr **rta = arg;
2649 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2650 struct in6_addr *pfx;
2653 if (rta[IFA_ADDRESS-1]) {
2654 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2656 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2658 if (rta[IFA_LOCAL-1]) {
2659 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2661 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2666 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2670 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
2672 struct rtattr **rta = arg;
2673 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
2674 struct in6_addr *pfx;
2677 if (rta[IFA_ADDRESS-1]) {
2678 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
2680 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
2682 if (rta[IFA_LOCAL-1]) {
2683 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
2685 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
2691 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
2694 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2695 u32 pid, u32 seq, int event)
2697 struct ifaddrmsg *ifm;
2698 struct nlmsghdr *nlh;
2699 struct ifa_cacheinfo ci;
2700 unsigned char *b = skb->tail;
2702 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2703 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2704 ifm = NLMSG_DATA(nlh);
2705 ifm->ifa_family = AF_INET6;
2706 ifm->ifa_prefixlen = ifa->prefix_len;
2707 ifm->ifa_flags = ifa->flags;
2708 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2709 if (ifa->scope&IFA_HOST)
2710 ifm->ifa_scope = RT_SCOPE_HOST;
2711 else if (ifa->scope&IFA_LINK)
2712 ifm->ifa_scope = RT_SCOPE_LINK;
2713 else if (ifa->scope&IFA_SITE)
2714 ifm->ifa_scope = RT_SCOPE_SITE;
2715 ifm->ifa_index = ifa->idev->dev->ifindex;
2716 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
2717 if (!(ifa->flags&IFA_F_PERMANENT)) {
2718 ci.ifa_prefered = ifa->prefered_lft;
2719 ci.ifa_valid = ifa->valid_lft;
2720 if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
2721 long tval = (jiffies - ifa->tstamp)/HZ;
2722 ci.ifa_prefered -= tval;
2723 if (ci.ifa_valid != INFINITY_LIFE_TIME)
2724 ci.ifa_valid -= tval;
2727 ci.ifa_prefered = INFINITY_LIFE_TIME;
2728 ci.ifa_valid = INFINITY_LIFE_TIME;
2730 ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
2731 + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2732 ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
2733 + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
2734 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2735 nlh->nlmsg_len = skb->tail - b;
2740 skb_trim(skb, b - skb->data);
2744 static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2745 u32 pid, u32 seq, int event)
2747 struct ifaddrmsg *ifm;
2748 struct nlmsghdr *nlh;
2749 struct ifa_cacheinfo ci;
2750 unsigned char *b = skb->tail;
2752 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2753 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2754 ifm = NLMSG_DATA(nlh);
2755 ifm->ifa_family = AF_INET6;
2756 ifm->ifa_prefixlen = 128;
2757 ifm->ifa_flags = IFA_F_PERMANENT;
2758 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2759 if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
2760 ifm->ifa_scope = RT_SCOPE_SITE;
2761 ifm->ifa_index = ifmca->idev->dev->ifindex;
2762 RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
2763 ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
2764 * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
2766 ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
2767 * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
2769 ci.ifa_prefered = INFINITY_LIFE_TIME;
2770 ci.ifa_valid = INFINITY_LIFE_TIME;
2771 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2772 nlh->nlmsg_len = skb->tail - b;
2777 skb_trim(skb, b - skb->data);
2781 static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2782 u32 pid, u32 seq, int event)
2784 struct ifaddrmsg *ifm;
2785 struct nlmsghdr *nlh;
2786 struct ifa_cacheinfo ci;
2787 unsigned char *b = skb->tail;
2789 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
2790 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2791 ifm = NLMSG_DATA(nlh);
2792 ifm->ifa_family = AF_INET6;
2793 ifm->ifa_prefixlen = 128;
2794 ifm->ifa_flags = IFA_F_PERMANENT;
2795 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
2796 if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
2797 ifm->ifa_scope = RT_SCOPE_SITE;
2798 ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
2799 RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
2800 ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
2801 * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
2803 ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
2804 * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
2806 ci.ifa_prefered = INFINITY_LIFE_TIME;
2807 ci.ifa_valid = INFINITY_LIFE_TIME;
2808 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
2809 nlh->nlmsg_len = skb->tail - b;
2814 skb_trim(skb, b - skb->data);
2825 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
2826 enum addr_type_t type)
2829 int s_idx, s_ip_idx;
2831 struct net_device *dev;
2832 struct inet6_dev *idev = NULL;
2833 struct inet6_ifaddr *ifa;
2834 struct ifmcaddr6 *ifmca;
2835 struct ifacaddr6 *ifaca;
2837 s_idx = cb->args[0];
2838 s_ip_idx = ip_idx = cb->args[1];
2839 read_lock(&dev_base_lock);
2841 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
2847 if ((idev = in6_dev_get(dev)) == NULL)
2849 read_lock_bh(&idev->lock);
2852 /* unicast address */
2853 for (ifa = idev->addr_list; ifa;
2854 ifa = ifa->if_next, ip_idx++) {
2855 if (ip_idx < s_ip_idx)
2857 if ((err = inet6_fill_ifaddr(skb, ifa,
2858 NETLINK_CB(cb->skb).pid,
2859 cb->nlh->nlmsg_seq, RTM_NEWADDR)) <= 0)
2863 #ifdef CONFIG_IPV6_PRIVACY
2864 for (ifa = idev->tempaddr_list; ifa;
2865 ifa = ifa->tmp_next, ip_idx++) {
2866 if (ip_idx < s_ip_idx)
2868 if ((err = inet6_fill_ifaddr(skb, ifa,
2869 NETLINK_CB(cb->skb).pid,
2870 cb->nlh->nlmsg_seq, RTM_NEWADDR)) <= 0)
2875 case MULTICAST_ADDR:
2876 /* multicast address */
2877 for (ifmca = idev->mc_list; ifmca;
2878 ifmca = ifmca->next, ip_idx++) {
2879 if (ip_idx < s_ip_idx)
2881 if ((err = inet6_fill_ifmcaddr(skb, ifmca,
2882 NETLINK_CB(cb->skb).pid,
2883 cb->nlh->nlmsg_seq, RTM_GETMULTICAST)) <= 0)
2888 /* anycast address */
2889 for (ifaca = idev->ac_list; ifaca;
2890 ifaca = ifaca->aca_next, ip_idx++) {
2891 if (ip_idx < s_ip_idx)
2893 if ((err = inet6_fill_ifacaddr(skb, ifaca,
2894 NETLINK_CB(cb->skb).pid,
2895 cb->nlh->nlmsg_seq, RTM_GETANYCAST)) <= 0)
2902 read_unlock_bh(&idev->lock);
2907 read_unlock_bh(&idev->lock);
2910 read_unlock(&dev_base_lock);
2912 cb->args[1] = ip_idx;
2916 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
2918 enum addr_type_t type = UNICAST_ADDR;
2919 return inet6_dump_addr(skb, cb, type);
2922 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
2924 enum addr_type_t type = MULTICAST_ADDR;
2925 return inet6_dump_addr(skb, cb, type);
2929 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
2931 enum addr_type_t type = ANYCAST_ADDR;
2932 return inet6_dump_addr(skb, cb, type);
2935 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
2937 struct sk_buff *skb;
2938 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
2940 skb = alloc_skb(size, GFP_ATOMIC);
2942 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, ENOBUFS);
2945 if (inet6_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
2947 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, EINVAL);
2950 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFADDR;
2951 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFADDR, GFP_ATOMIC);
2954 static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
2955 __s32 *array, int bytes)
2957 memset(array, 0, bytes);
2958 array[DEVCONF_FORWARDING] = cnf->forwarding;
2959 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
2960 array[DEVCONF_MTU6] = cnf->mtu6;
2961 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
2962 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
2963 array[DEVCONF_AUTOCONF] = cnf->autoconf;
2964 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
2965 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
2966 array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
2967 array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
2968 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
2969 #ifdef CONFIG_IPV6_PRIVACY
2970 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
2971 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
2972 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
2973 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
2974 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
2976 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
2979 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
2980 u32 pid, u32 seq, int event)
2982 struct net_device *dev = idev->dev;
2983 __s32 *array = NULL;
2984 struct ifinfomsg *r;
2985 struct nlmsghdr *nlh;
2986 unsigned char *b = skb->tail;
2987 struct rtattr *subattr;
2988 __u32 mtu = dev->mtu;
2989 struct ifla_cacheinfo ci;
2991 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*r));
2992 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
2993 r = NLMSG_DATA(nlh);
2994 r->ifi_family = AF_INET6;
2995 r->ifi_type = dev->type;
2996 r->ifi_index = dev->ifindex;
2997 r->ifi_flags = dev->flags;
2999 if (!netif_running(dev) || !netif_carrier_ok(dev))
3000 r->ifi_flags &= ~IFF_RUNNING;
3002 r->ifi_flags |= IFF_RUNNING;
3004 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
3007 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
3009 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
3010 if (dev->ifindex != dev->iflink)
3011 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
3013 subattr = (struct rtattr*)skb->tail;
3015 RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
3017 /* return the device flags */
3018 RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
3020 /* return interface cacheinfo */
3021 ci.max_reasm_len = IPV6_MAXPLEN;
3022 ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
3023 + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
3024 ci.reachable_time = idev->nd_parms->reachable_time;
3025 ci.retrans_time = idev->nd_parms->retrans_time;
3026 RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
3028 /* return the device sysctl params */
3029 if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
3030 goto rtattr_failure;
3031 ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
3032 RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
3034 /* XXX - Statistics/MC not implemented */
3035 subattr->rta_len = skb->tail - (u8*)subattr;
3037 nlh->nlmsg_len = skb->tail - b;
3045 skb_trim(skb, b - skb->data);
3049 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
3052 int s_idx = cb->args[0];
3053 struct net_device *dev;
3054 struct inet6_dev *idev;
3056 read_lock(&dev_base_lock);
3057 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
3060 if ((idev = in6_dev_get(dev)) == NULL)
3062 err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3063 cb->nlh->nlmsg_seq, RTM_NEWLINK);
3068 read_unlock(&dev_base_lock);
3074 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
3076 struct sk_buff *skb;
3078 int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
3080 skb = alloc_skb(size, GFP_ATOMIC);
3082 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFINFO, ENOBUFS);
3085 if (inet6_fill_ifinfo(skb, idev, 0, 0, event) < 0) {
3087 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFINFO, EINVAL);
3090 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFINFO;
3091 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFINFO, GFP_ATOMIC);
3094 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3095 struct prefix_info *pinfo, u32 pid, u32 seq, int event)
3097 struct prefixmsg *pmsg;
3098 struct nlmsghdr *nlh;
3099 unsigned char *b = skb->tail;
3100 struct prefix_cacheinfo ci;
3102 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*pmsg));
3105 nlh->nlmsg_flags |= NLM_F_MULTI;
3107 pmsg = NLMSG_DATA(nlh);
3108 pmsg->prefix_family = AF_INET6;
3109 pmsg->prefix_ifindex = idev->dev->ifindex;
3110 pmsg->prefix_len = pinfo->prefix_len;
3111 pmsg->prefix_type = pinfo->type;
3113 pmsg->prefix_flags = 0;
3115 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
3116 if (pinfo->autoconf)
3117 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
3119 RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
3121 ci.preferred_time = ntohl(pinfo->prefered);
3122 ci.valid_time = ntohl(pinfo->valid);
3123 RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
3125 nlh->nlmsg_len = skb->tail - b;
3130 skb_trim(skb, b - skb->data);
3134 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
3135 struct prefix_info *pinfo)
3137 struct sk_buff *skb;
3138 int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
3140 skb = alloc_skb(size, GFP_ATOMIC);
3142 netlink_set_err(rtnl, 0, RTMGRP_IPV6_PREFIX, ENOBUFS);
3145 if (inet6_fill_prefix(skb, idev, pinfo, 0, 0, event) < 0) {
3147 netlink_set_err(rtnl, 0, RTMGRP_IPV6_PREFIX, EINVAL);
3150 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_PREFIX;
3151 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_PREFIX, GFP_ATOMIC);
3154 static struct rtnetlink_link inet6_rtnetlink_table[RTM_MAX - RTM_BASE + 1] = {
3155 [RTM_GETLINK - RTM_BASE] = { .dumpit = inet6_dump_ifinfo, },
3156 [RTM_NEWADDR - RTM_BASE] = { .doit = inet6_rtm_newaddr, },
3157 [RTM_DELADDR - RTM_BASE] = { .doit = inet6_rtm_deladdr, },
3158 [RTM_GETADDR - RTM_BASE] = { .dumpit = inet6_dump_ifaddr, },
3159 [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
3160 [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
3161 [RTM_NEWROUTE - RTM_BASE] = { .doit = inet6_rtm_newroute, },
3162 [RTM_DELROUTE - RTM_BASE] = { .doit = inet6_rtm_delroute, },
3163 [RTM_GETROUTE - RTM_BASE] = { .doit = inet6_rtm_getroute,
3164 .dumpit = inet6_dump_fib, },
3167 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
3169 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
3173 dst_hold(&ifp->rt->u.dst);
3174 if (ip6_ins_rt(ifp->rt, NULL, NULL))
3175 dst_release(&ifp->rt->u.dst);
3178 addrconf_leave_solict(ifp->idev->dev, &ifp->addr);
3179 if (ifp->idev->cnf.forwarding) {
3180 struct in6_addr addr;
3182 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
3183 if (!ipv6_addr_any(&addr))
3184 ipv6_dev_ac_dec(ifp->idev->dev, &addr);
3186 dst_hold(&ifp->rt->u.dst);
3187 if (ip6_del_rt(ifp->rt, NULL, NULL))
3188 dst_free(&ifp->rt->u.dst);
3190 dst_release(&ifp->rt->u.dst);
3195 #ifdef CONFIG_SYSCTL
3198 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
3199 void __user *buffer, size_t *lenp, loff_t *ppos)
3201 int *valp = ctl->data;
3205 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
3207 if (write && *valp != val && valp != &ipv6_devconf_dflt.forwarding) {
3208 struct inet6_dev *idev = NULL;
3210 if (valp != &ipv6_devconf.forwarding) {
3211 idev = (struct inet6_dev *)ctl->extra1;
3215 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
3217 addrconf_forward_change(idev);
3220 rt6_purge_dflt_routers(0);
3226 static int addrconf_sysctl_forward_strategy(ctl_table *table,
3227 int __user *name, int nlen,
3228 void __user *oldval,
3229 size_t __user *oldlenp,
3230 void __user *newval, size_t newlen,
3233 int *valp = table->data;
3236 if (!newval || !newlen)
3238 if (newlen != sizeof(int))
3240 if (get_user(new, (int __user *)newval))
3244 if (oldval && oldlenp) {
3246 if (get_user(len, oldlenp))
3249 if (len > table->maxlen)
3250 len = table->maxlen;
3251 if (copy_to_user(oldval, valp, len))
3253 if (put_user(len, oldlenp))
3258 if (valp != &ipv6_devconf_dflt.forwarding) {
3259 struct inet6_dev *idev;
3260 if (valp != &ipv6_devconf.forwarding) {
3261 idev = (struct inet6_dev *)table->extra1;
3262 if (unlikely(idev == NULL))
3267 addrconf_forward_change(idev);
3270 rt6_purge_dflt_routers(0);
3277 static struct addrconf_sysctl_table
3279 struct ctl_table_header *sysctl_header;
3280 ctl_table addrconf_vars[18];
3281 ctl_table addrconf_dev[2];
3282 ctl_table addrconf_conf_dir[2];
3283 ctl_table addrconf_proto_dir[2];
3284 ctl_table addrconf_root_dir[2];
3285 } addrconf_sysctl = {
3286 .sysctl_header = NULL,
3289 .ctl_name = NET_IPV6_FORWARDING,
3290 .procname = "forwarding",
3291 .data = &ipv6_devconf.forwarding,
3292 .maxlen = sizeof(int),
3294 .proc_handler = &addrconf_sysctl_forward,
3295 .strategy = &addrconf_sysctl_forward_strategy,
3298 .ctl_name = NET_IPV6_HOP_LIMIT,
3299 .procname = "hop_limit",
3300 .data = &ipv6_devconf.hop_limit,
3301 .maxlen = sizeof(int),
3303 .proc_handler = proc_dointvec,
3306 .ctl_name = NET_IPV6_MTU,
3308 .data = &ipv6_devconf.mtu6,
3309 .maxlen = sizeof(int),
3311 .proc_handler = &proc_dointvec,
3314 .ctl_name = NET_IPV6_ACCEPT_RA,
3315 .procname = "accept_ra",
3316 .data = &ipv6_devconf.accept_ra,
3317 .maxlen = sizeof(int),
3319 .proc_handler = &proc_dointvec,
3322 .ctl_name = NET_IPV6_ACCEPT_REDIRECTS,
3323 .procname = "accept_redirects",
3324 .data = &ipv6_devconf.accept_redirects,
3325 .maxlen = sizeof(int),
3327 .proc_handler = &proc_dointvec,
3330 .ctl_name = NET_IPV6_AUTOCONF,
3331 .procname = "autoconf",
3332 .data = &ipv6_devconf.autoconf,
3333 .maxlen = sizeof(int),
3335 .proc_handler = &proc_dointvec,
3338 .ctl_name = NET_IPV6_DAD_TRANSMITS,
3339 .procname = "dad_transmits",
3340 .data = &ipv6_devconf.dad_transmits,
3341 .maxlen = sizeof(int),
3343 .proc_handler = &proc_dointvec,
3346 .ctl_name = NET_IPV6_RTR_SOLICITS,
3347 .procname = "router_solicitations",
3348 .data = &ipv6_devconf.rtr_solicits,
3349 .maxlen = sizeof(int),
3351 .proc_handler = &proc_dointvec,
3354 .ctl_name = NET_IPV6_RTR_SOLICIT_INTERVAL,
3355 .procname = "router_solicitation_interval",
3356 .data = &ipv6_devconf.rtr_solicit_interval,
3357 .maxlen = sizeof(int),
3359 .proc_handler = &proc_dointvec_jiffies,
3360 .strategy = &sysctl_jiffies,
3363 .ctl_name = NET_IPV6_RTR_SOLICIT_DELAY,
3364 .procname = "router_solicitation_delay",
3365 .data = &ipv6_devconf.rtr_solicit_delay,
3366 .maxlen = sizeof(int),
3368 .proc_handler = &proc_dointvec_jiffies,
3369 .strategy = &sysctl_jiffies,
3372 .ctl_name = NET_IPV6_FORCE_MLD_VERSION,
3373 .procname = "force_mld_version",
3374 .data = &ipv6_devconf.force_mld_version,
3375 .maxlen = sizeof(int),
3377 .proc_handler = &proc_dointvec,
3379 #ifdef CONFIG_IPV6_PRIVACY
3381 .ctl_name = NET_IPV6_USE_TEMPADDR,
3382 .procname = "use_tempaddr",
3383 .data = &ipv6_devconf.use_tempaddr,
3384 .maxlen = sizeof(int),
3386 .proc_handler = &proc_dointvec,
3389 .ctl_name = NET_IPV6_TEMP_VALID_LFT,
3390 .procname = "temp_valid_lft",
3391 .data = &ipv6_devconf.temp_valid_lft,
3392 .maxlen = sizeof(int),
3394 .proc_handler = &proc_dointvec,
3397 .ctl_name = NET_IPV6_TEMP_PREFERED_LFT,
3398 .procname = "temp_prefered_lft",
3399 .data = &ipv6_devconf.temp_prefered_lft,
3400 .maxlen = sizeof(int),
3402 .proc_handler = &proc_dointvec,
3405 .ctl_name = NET_IPV6_REGEN_MAX_RETRY,
3406 .procname = "regen_max_retry",
3407 .data = &ipv6_devconf.regen_max_retry,
3408 .maxlen = sizeof(int),
3410 .proc_handler = &proc_dointvec,
3413 .ctl_name = NET_IPV6_MAX_DESYNC_FACTOR,
3414 .procname = "max_desync_factor",
3415 .data = &ipv6_devconf.max_desync_factor,
3416 .maxlen = sizeof(int),
3418 .proc_handler = &proc_dointvec,
3422 .ctl_name = NET_IPV6_MAX_ADDRESSES,
3423 .procname = "max_addresses",
3424 .data = &ipv6_devconf.max_addresses,
3425 .maxlen = sizeof(int),
3427 .proc_handler = &proc_dointvec,
3430 .ctl_name = 0, /* sentinel */
3435 .ctl_name = NET_PROTO_CONF_ALL,
3438 .child = addrconf_sysctl.addrconf_vars,
3441 .ctl_name = 0, /* sentinel */
3444 .addrconf_conf_dir = {
3446 .ctl_name = NET_IPV6_CONF,
3449 .child = addrconf_sysctl.addrconf_dev,
3452 .ctl_name = 0, /* sentinel */
3455 .addrconf_proto_dir = {
3457 .ctl_name = NET_IPV6,
3460 .child = addrconf_sysctl.addrconf_conf_dir,
3463 .ctl_name = 0, /* sentinel */
3466 .addrconf_root_dir = {
3468 .ctl_name = CTL_NET,
3471 .child = addrconf_sysctl.addrconf_proto_dir,
3474 .ctl_name = 0, /* sentinel */
3479 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
3482 struct net_device *dev = idev ? idev->dev : NULL;
3483 struct addrconf_sysctl_table *t;
3484 char *dev_name = NULL;
3486 t = kmalloc(sizeof(*t), GFP_KERNEL);
3489 memcpy(t, &addrconf_sysctl, sizeof(*t));
3490 for (i=0; t->addrconf_vars[i].data; i++) {
3491 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
3492 t->addrconf_vars[i].de = NULL;
3493 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
3496 dev_name = dev->name;
3497 t->addrconf_dev[0].ctl_name = dev->ifindex;
3499 dev_name = "default";
3500 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
3504 * Make a copy of dev_name, because '.procname' is regarded as const
3505 * by sysctl and we wouldn't want anyone to change it under our feet
3506 * (see SIOCSIFNAME).
3508 dev_name = net_sysctl_strdup(dev_name);
3512 t->addrconf_dev[0].procname = dev_name;
3514 t->addrconf_dev[0].child = t->addrconf_vars;
3515 t->addrconf_dev[0].de = NULL;
3516 t->addrconf_conf_dir[0].child = t->addrconf_dev;
3517 t->addrconf_conf_dir[0].de = NULL;
3518 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
3519 t->addrconf_proto_dir[0].de = NULL;
3520 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
3521 t->addrconf_root_dir[0].de = NULL;
3523 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
3524 if (t->sysctl_header == NULL)
3539 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
3542 struct addrconf_sysctl_table *t = p->sysctl;
3544 unregister_sysctl_table(t->sysctl_header);
3545 kfree(t->addrconf_dev[0].procname);
3557 int register_inet6addr_notifier(struct notifier_block *nb)
3559 return notifier_chain_register(&inet6addr_chain, nb);
3562 int unregister_inet6addr_notifier(struct notifier_block *nb)
3564 return notifier_chain_unregister(&inet6addr_chain,nb);
3568 * Init / cleanup code
3571 void __init addrconf_init(void)
3573 register_netdevice_notifier(&ipv6_dev_notf);
3575 #ifdef CONFIG_IPV6_PRIVACY
3576 struct crypto_tfm *tfm = crypto_alloc_tfm("md5", 0);
3577 if (likely(tfm != NULL)) {
3578 spin_lock(&md5_tfm_lock);
3579 if (likely(md5_tfm == NULL)) {
3581 spin_unlock(&md5_tfm_lock);
3583 spin_unlock(&md5_tfm_lock);
3584 crypto_free_tfm(tfm);
3588 "failed to load transform for md5\n");
3593 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
3594 #ifdef CONFIG_SYSCTL
3595 addrconf_sysctl.sysctl_header =
3596 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
3597 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
3601 void __exit addrconf_cleanup(void)
3603 struct net_device *dev;
3604 struct inet6_dev *idev;
3605 struct inet6_ifaddr *ifa;
3607 #ifdef CONFIG_IPV6_PRIVACY
3608 struct crypto_tfm *tfm;
3611 unregister_netdevice_notifier(&ipv6_dev_notf);
3613 rtnetlink_links[PF_INET6] = NULL;
3614 #ifdef CONFIG_SYSCTL
3615 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
3616 addrconf_sysctl_unregister(&ipv6_devconf);
3625 for (dev=dev_base; dev; dev=dev->next) {
3626 if ((idev = __in6_dev_get(dev)) == NULL)
3628 addrconf_ifdown(dev, 1);
3635 write_lock_bh(&addrconf_hash_lock);
3636 for (i=0; i < IN6_ADDR_HSIZE; i++) {
3637 for (ifa=inet6_addr_lst[i]; ifa; ) {
3638 struct inet6_ifaddr *bifa;
3641 ifa = ifa->lst_next;
3642 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
3643 /* Do not free it; something is wrong.
3644 Now we can investigate it with debugger.
3648 write_unlock_bh(&addrconf_hash_lock);
3650 del_timer(&addr_chk_timer);
3654 #ifdef CONFIG_IPV6_PRIVACY
3655 spin_lock(&md5_tfm_lock);
3658 spin_unlock(&md5_tfm_lock);
3660 crypto_free_tfm(tfm);
3663 #ifdef CONFIG_PROC_FS
3664 proc_net_remove("if_inet6");