- Linus: more PageDirty / swapcache handling
[davej-history.git] / net / ipv6 / addrconf.c
blobd342d564217f6ea5fce1fe0a19e34615da732716
1 /*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * $Id: addrconf.c,v 1.59 2000/11/28 11:39:43 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.
18 * Changes:
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill doube kfree on module
23 * unload.
24 * Maciej W. Rozycki : FDDI support
27 #include <linux/config.h>
28 #include <linux/errno.h>
29 #include <linux/types.h>
30 #include <linux/socket.h>
31 #include <linux/sockios.h>
32 #include <linux/sched.h>
33 #include <linux/net.h>
34 #include <linux/in6.h>
35 #include <linux/netdevice.h>
36 #include <linux/if_arp.h>
37 #include <linux/route.h>
38 #include <linux/inetdevice.h>
39 #include <linux/init.h>
40 #ifdef CONFIG_SYSCTL
41 #include <linux/sysctl.h>
42 #endif
43 #include <linux/delay.h>
45 #include <linux/proc_fs.h>
46 #include <net/sock.h>
47 #include <net/snmp.h>
49 #include <net/ipv6.h>
50 #include <net/protocol.h>
51 #include <net/ndisc.h>
52 #include <net/ip6_route.h>
53 #include <net/addrconf.h>
54 #include <net/ip.h>
55 #include <linux/if_tunnel.h>
56 #include <linux/rtnetlink.h>
58 #include <asm/uaccess.h>
60 /* Set to 3 to get tracing... */
61 #define ACONF_DEBUG 2
63 #if ACONF_DEBUG >= 3
64 #define ADBG(x) printk x
65 #else
66 #define ADBG(x)
67 #endif
69 #ifdef CONFIG_SYSCTL
70 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
71 static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
72 #endif
74 int inet6_dev_count;
75 int inet6_ifa_count;
78 * Configured unicast address hash table
80 static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];
81 static rwlock_t addrconf_hash_lock = RW_LOCK_UNLOCKED;
83 /* Protects inet6 devices */
84 rwlock_t addrconf_lock = RW_LOCK_UNLOCKED;
86 void addrconf_verify(unsigned long);
88 static struct timer_list addr_chk_timer = { function: addrconf_verify };
90 static int addrconf_ifdown(struct net_device *dev, int how);
92 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
93 static void addrconf_dad_timer(unsigned long data);
94 static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
95 static void addrconf_rs_timer(unsigned long data);
96 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
98 struct ipv6_devconf ipv6_devconf =
100 0, /* forwarding */
101 IPV6_DEFAULT_HOPLIMIT, /* hop limit */
102 IPV6_MIN_MTU, /* mtu */
103 1, /* accept RAs */
104 1, /* accept redirects */
105 1, /* autoconfiguration */
106 1, /* dad transmits */
107 MAX_RTR_SOLICITATIONS, /* router solicits */
108 RTR_SOLICITATION_INTERVAL, /* rtr solicit interval */
109 MAX_RTR_SOLICITATION_DELAY, /* rtr solicit delay */
112 static struct ipv6_devconf ipv6_devconf_dflt =
114 0, /* forwarding */
115 IPV6_DEFAULT_HOPLIMIT, /* hop limit */
116 IPV6_MIN_MTU, /* mtu */
117 1, /* accept RAs */
118 1, /* accept redirects */
119 1, /* autoconfiguration */
120 1, /* dad transmits */
121 MAX_RTR_SOLICITATIONS, /* router solicits */
122 RTR_SOLICITATION_INTERVAL, /* rtr solicit interval */
123 MAX_RTR_SOLICITATION_DELAY, /* rtr solicit delay */
126 int ipv6_addr_type(struct in6_addr *addr)
128 u32 st;
130 st = addr->s6_addr32[0];
132 /* Consider all addresses with the first three bits different of
133 000 and 111 as unicasts.
135 if ((st & __constant_htonl(0xE0000000)) != __constant_htonl(0x00000000) &&
136 (st & __constant_htonl(0xE0000000)) != __constant_htonl(0xE0000000))
137 return IPV6_ADDR_UNICAST;
139 if ((st & __constant_htonl(0xFF000000)) == __constant_htonl(0xFF000000)) {
140 int type = IPV6_ADDR_MULTICAST;
142 switch((st & __constant_htonl(0x00FF0000))) {
143 case __constant_htonl(0x00010000):
144 type |= IPV6_ADDR_LOOPBACK;
145 break;
147 case __constant_htonl(0x00020000):
148 type |= IPV6_ADDR_LINKLOCAL;
149 break;
151 case __constant_htonl(0x00050000):
152 type |= IPV6_ADDR_SITELOCAL;
153 break;
155 return type;
158 if ((st & __constant_htonl(0xFFC00000)) == __constant_htonl(0xFE800000))
159 return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST);
161 if ((st & __constant_htonl(0xFFC00000)) == __constant_htonl(0xFEC00000))
162 return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST);
164 if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
165 if (addr->s6_addr32[2] == 0) {
166 if (addr->in6_u.u6_addr32[3] == 0)
167 return IPV6_ADDR_ANY;
169 if (addr->s6_addr32[3] == __constant_htonl(0x00000001))
170 return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST);
172 return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST);
175 if (addr->s6_addr32[2] == __constant_htonl(0x0000ffff))
176 return IPV6_ADDR_MAPPED;
179 return IPV6_ADDR_RESERVED;
182 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
184 if (del_timer(&ifp->timer))
185 __in6_ifa_put(ifp);
188 enum addrconf_timer_t
190 AC_NONE,
191 AC_DAD,
192 AC_RS,
195 static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
196 enum addrconf_timer_t what,
197 unsigned long when)
199 if (!del_timer(&ifp->timer))
200 in6_ifa_hold(ifp);
202 switch (what) {
203 case AC_DAD:
204 ifp->timer.function = addrconf_dad_timer;
205 break;
206 case AC_RS:
207 ifp->timer.function = addrconf_rs_timer;
208 break;
209 default:
211 ifp->timer.expires = jiffies + when;
212 add_timer(&ifp->timer);
216 /* Nobody refers to this device, we may destroy it. */
218 void in6_dev_finish_destroy(struct inet6_dev *idev)
220 struct net_device *dev = idev->dev;
221 BUG_TRAP(idev->addr_list==NULL);
222 BUG_TRAP(idev->mc_list==NULL);
223 #ifdef NET_REFCNT_DEBUG
224 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
225 #endif
226 dev_put(dev);
227 if (!idev->dead) {
228 printk("Freeing alive inet6 device %p\n", idev);
229 return;
231 inet6_dev_count--;
232 kfree(idev);
235 static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
237 struct inet6_dev *ndev;
239 ASSERT_RTNL();
241 if (dev->mtu < IPV6_MIN_MTU)
242 return NULL;
244 ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
246 if (ndev) {
247 memset(ndev, 0, sizeof(struct inet6_dev));
249 ndev->lock = RW_LOCK_UNLOCKED;
250 ndev->dev = dev;
251 memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
252 ndev->cnf.mtu6 = dev->mtu;
253 ndev->cnf.sysctl = NULL;
254 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
255 if (ndev->nd_parms == NULL) {
256 kfree(ndev);
257 return NULL;
259 inet6_dev_count++;
260 /* We refer to the device */
261 dev_hold(dev);
263 write_lock_bh(&addrconf_lock);
264 dev->ip6_ptr = ndev;
265 /* One reference from device */
266 in6_dev_hold(ndev);
267 write_unlock_bh(&addrconf_lock);
269 #ifdef CONFIG_SYSCTL
270 neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6, NET_IPV6_NEIGH, "ipv6");
271 addrconf_sysctl_register(ndev, &ndev->cnf);
272 #endif
274 return ndev;
277 static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
279 struct inet6_dev *idev;
281 ASSERT_RTNL();
283 if ((idev = __in6_dev_get(dev)) == NULL) {
284 if ((idev = ipv6_add_dev(dev)) == NULL)
285 return NULL;
286 if (dev->flags&IFF_UP)
287 ipv6_mc_up(idev);
289 return idev;
292 static void addrconf_forward_change(struct inet6_dev *idev)
294 struct net_device *dev;
296 if (idev)
297 return;
299 read_lock(&dev_base_lock);
300 for (dev=dev_base; dev; dev=dev->next) {
301 read_lock(&addrconf_lock);
302 idev = __in6_dev_get(dev);
303 if (idev)
304 idev->cnf.forwarding = ipv6_devconf.forwarding;
305 read_unlock(&addrconf_lock);
307 read_unlock(&dev_base_lock);
310 /* Nobody refers to this ifaddr, destroy it */
312 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
314 BUG_TRAP(ifp->if_next==NULL);
315 BUG_TRAP(ifp->lst_next==NULL);
316 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
318 in6_dev_put(ifp->idev);
320 if (del_timer(&ifp->timer))
321 printk("Timer is still running, when freeing ifa=%p\n", ifp);
323 if (!ifp->dead) {
324 printk("Freeing alive inet6 address %p\n", ifp);
325 return;
327 inet6_ifa_count--;
328 kfree(ifp);
331 /* On success it returns ifp with increased reference count */
333 static struct inet6_ifaddr *
334 ipv6_add_addr(struct inet6_dev *idev, struct in6_addr *addr, int pfxlen,
335 int scope, unsigned flags)
337 struct inet6_ifaddr *ifa;
338 int hash;
340 ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
342 if (ifa == NULL) {
343 ADBG(("ipv6_add_addr: malloc failed\n"));
344 return NULL;
347 memset(ifa, 0, sizeof(struct inet6_ifaddr));
348 ipv6_addr_copy(&ifa->addr, addr);
350 spin_lock_init(&ifa->lock);
351 init_timer(&ifa->timer);
352 ifa->timer.data = (unsigned long) ifa;
353 ifa->scope = scope;
354 ifa->prefix_len = pfxlen;
355 ifa->flags = flags | IFA_F_TENTATIVE;
357 read_lock(&addrconf_lock);
358 if (idev->dead) {
359 read_unlock(&addrconf_lock);
360 kfree(ifa);
361 return NULL;
364 inet6_ifa_count++;
365 ifa->idev = idev;
366 in6_dev_hold(idev);
367 /* For caller */
368 in6_ifa_hold(ifa);
370 /* Add to big hash table */
371 hash = ipv6_addr_hash(addr);
373 write_lock_bh(&addrconf_hash_lock);
374 ifa->lst_next = inet6_addr_lst[hash];
375 inet6_addr_lst[hash] = ifa;
376 in6_ifa_hold(ifa);
377 write_unlock_bh(&addrconf_hash_lock);
379 write_lock_bh(&idev->lock);
380 /* Add to inet6_dev unicast addr list. */
381 ifa->if_next = idev->addr_list;
382 idev->addr_list = ifa;
383 in6_ifa_hold(ifa);
384 write_unlock_bh(&idev->lock);
385 read_unlock(&addrconf_lock);
387 return ifa;
390 /* This function wants to get referenced ifp and releases it before return */
392 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
394 struct inet6_ifaddr *ifa, **ifap;
395 struct inet6_dev *idev = ifp->idev;
396 int hash;
398 hash = ipv6_addr_hash(&ifp->addr);
400 ifp->dead = 1;
402 write_lock_bh(&addrconf_hash_lock);
403 for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
404 ifap = &ifa->lst_next) {
405 if (ifa == ifp) {
406 *ifap = ifa->lst_next;
407 __in6_ifa_put(ifp);
408 ifa->lst_next = NULL;
409 break;
412 write_unlock_bh(&addrconf_hash_lock);
414 write_lock_bh(&idev->lock);
415 for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;
416 ifap = &ifa->if_next) {
417 if (ifa == ifp) {
418 *ifap = ifa->if_next;
419 __in6_ifa_put(ifp);
420 ifa->if_next = NULL;
421 break;
424 write_unlock_bh(&idev->lock);
426 ipv6_ifa_notify(RTM_DELADDR, ifp);
429 addrconf_del_timer(ifp);
431 in6_ifa_put(ifp);
435 * Choose an apropriate source address
436 * should do:
437 * i) get an address with an apropriate scope
438 * ii) see if there is a specific route for the destination and use
439 * an address of the attached interface
440 * iii) don't use deprecated addresses
442 int ipv6_get_saddr(struct dst_entry *dst,
443 struct in6_addr *daddr, struct in6_addr *saddr)
445 int scope;
446 struct inet6_ifaddr *ifp = NULL;
447 struct inet6_ifaddr *match = NULL;
448 struct net_device *dev = NULL;
449 struct inet6_dev *idev;
450 struct rt6_info *rt;
451 int err;
453 rt = (struct rt6_info *) dst;
454 if (rt)
455 dev = rt->rt6i_dev;
457 scope = ipv6_addr_scope(daddr);
458 if (rt && (rt->rt6i_flags & RTF_ALLONLINK)) {
460 * route for the "all destinations on link" rule
461 * when no routers are present
463 scope = IFA_LINK;
467 * known dev
468 * search dev and walk through dev addresses
471 if (dev) {
472 if (dev->flags & IFF_LOOPBACK)
473 scope = IFA_HOST;
475 read_lock(&addrconf_lock);
476 idev = __in6_dev_get(dev);
477 if (idev) {
478 read_lock_bh(&idev->lock);
479 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
480 if (ifp->scope == scope) {
481 if (!(ifp->flags & (IFA_F_DEPRECATED|IFA_F_TENTATIVE))) {
482 in6_ifa_hold(ifp);
483 read_unlock_bh(&idev->lock);
484 read_unlock(&addrconf_lock);
485 goto out;
488 if (!match && !(ifp->flags & IFA_F_TENTATIVE)) {
489 match = ifp;
490 in6_ifa_hold(ifp);
494 read_unlock_bh(&idev->lock);
496 read_unlock(&addrconf_lock);
499 if (scope == IFA_LINK)
500 goto out;
503 * dev == NULL or search failed for specified dev
506 read_lock(&dev_base_lock);
507 read_lock(&addrconf_lock);
508 for (dev = dev_base; dev; dev=dev->next) {
509 idev = __in6_dev_get(dev);
510 if (idev) {
511 read_lock_bh(&idev->lock);
512 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
513 if (ifp->scope == scope) {
514 if (!(ifp->flags&(IFA_F_DEPRECATED|IFA_F_TENTATIVE))) {
515 in6_ifa_hold(ifp);
516 read_unlock_bh(&idev->lock);
517 goto out_unlock_base;
520 if (!match && !(ifp->flags&IFA_F_TENTATIVE)) {
521 match = ifp;
522 in6_ifa_hold(ifp);
526 read_unlock_bh(&idev->lock);
530 out_unlock_base:
531 read_unlock(&addrconf_lock);
532 read_unlock(&dev_base_lock);
534 out:
535 if (ifp == NULL) {
536 ifp = match;
537 match = NULL;
540 err = -EADDRNOTAVAIL;
541 if (ifp) {
542 ipv6_addr_copy(saddr, &ifp->addr);
543 err = 0;
544 in6_ifa_put(ifp);
546 if (match)
547 in6_ifa_put(match);
549 return err;
552 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
554 struct inet6_dev *idev;
555 int err = -EADDRNOTAVAIL;
557 read_lock(&addrconf_lock);
558 if ((idev = __in6_dev_get(dev)) != NULL) {
559 struct inet6_ifaddr *ifp;
561 read_lock_bh(&idev->lock);
562 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
563 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
564 ipv6_addr_copy(addr, &ifp->addr);
565 err = 0;
566 break;
569 read_unlock_bh(&idev->lock);
571 read_unlock(&addrconf_lock);
572 return err;
575 int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev)
577 struct inet6_ifaddr * ifp;
578 u8 hash = ipv6_addr_hash(addr);
580 read_lock_bh(&addrconf_hash_lock);
581 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
582 if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
583 !(ifp->flags&IFA_F_TENTATIVE)) {
584 if (dev == NULL || ifp->idev->dev == dev ||
585 !(ifp->scope&(IFA_LINK|IFA_HOST)))
586 break;
589 read_unlock_bh(&addrconf_hash_lock);
590 return ifp != NULL;
593 struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev)
595 struct inet6_ifaddr * ifp;
596 u8 hash = ipv6_addr_hash(addr);
598 read_lock_bh(&addrconf_hash_lock);
599 for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
600 if (ipv6_addr_cmp(&ifp->addr, addr) == 0) {
601 if (dev == NULL || ifp->idev->dev == dev ||
602 !(ifp->scope&(IFA_LINK|IFA_HOST))) {
603 in6_ifa_hold(ifp);
604 break;
608 read_unlock_bh(&addrconf_hash_lock);
610 return ifp;
613 /* Gets referenced address, destroys ifaddr */
615 void addrconf_dad_failure(struct inet6_ifaddr *ifp)
617 printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
618 if (ifp->flags&IFA_F_PERMANENT) {
619 spin_lock_bh(&ifp->lock);
620 addrconf_del_timer(ifp);
621 ifp->flags |= IFA_F_TENTATIVE;
622 spin_unlock_bh(&ifp->lock);
623 in6_ifa_put(ifp);
624 } else
625 ipv6_del_addr(ifp);
629 /* Join to solicited addr multicast group. */
631 static void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
633 struct in6_addr maddr;
635 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
636 return;
638 #ifndef CONFIG_IPV6_NO_PB
639 addrconf_addr_solict_mult_old(addr, &maddr);
640 ipv6_dev_mc_inc(dev, &maddr);
641 #endif
642 #ifdef CONFIG_IPV6_EUI64
643 addrconf_addr_solict_mult_new(addr, &maddr);
644 ipv6_dev_mc_inc(dev, &maddr);
645 #endif
648 static void addrconf_leave_solict(struct net_device *dev, struct in6_addr *addr)
650 struct in6_addr maddr;
652 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
653 return;
655 #ifndef CONFIG_IPV6_NO_PB
656 addrconf_addr_solict_mult_old(addr, &maddr);
657 ipv6_dev_mc_dec(dev, &maddr);
658 #endif
659 #ifdef CONFIG_IPV6_EUI64
660 addrconf_addr_solict_mult_new(addr, &maddr);
661 ipv6_dev_mc_dec(dev, &maddr);
662 #endif
666 #ifdef CONFIG_IPV6_EUI64
667 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
669 switch (dev->type) {
670 case ARPHRD_ETHER:
671 case ARPHRD_FDDI:
672 case ARPHRD_IEEE802_TR:
673 if (dev->addr_len != ETH_ALEN)
674 return -1;
675 memcpy(eui, dev->dev_addr, 3);
676 memcpy(eui + 5, dev->dev_addr+3, 3);
677 eui[3] = 0xFF;
678 eui[4] = 0xFE;
679 eui[0] ^= 2;
680 return 0;
682 return -1;
685 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
687 int err = -1;
688 struct inet6_ifaddr *ifp;
690 read_lock_bh(&idev->lock);
691 for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
692 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
693 memcpy(eui, ifp->addr.s6_addr+8, 8);
694 err = 0;
695 break;
698 read_unlock_bh(&idev->lock);
699 return err;
701 #endif
704 * Add prefix route.
707 static void
708 addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
709 unsigned long expires, unsigned flags)
711 struct in6_rtmsg rtmsg;
713 memset(&rtmsg, 0, sizeof(rtmsg));
714 memcpy(&rtmsg.rtmsg_dst, pfx, sizeof(struct in6_addr));
715 rtmsg.rtmsg_dst_len = plen;
716 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
717 rtmsg.rtmsg_ifindex = dev->ifindex;
718 rtmsg.rtmsg_info = expires;
719 rtmsg.rtmsg_flags = RTF_UP|flags;
720 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
722 /* Prevent useless cloning on PtP SIT.
723 This thing is done here expecting that the whole
724 class of non-broadcast devices need not cloning.
726 if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
727 rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
729 ip6_route_add(&rtmsg);
732 /* Create "default" multicast route to the interface */
734 static void addrconf_add_mroute(struct net_device *dev)
736 struct in6_rtmsg rtmsg;
738 memset(&rtmsg, 0, sizeof(rtmsg));
739 ipv6_addr_set(&rtmsg.rtmsg_dst,
740 __constant_htonl(0xFF000000), 0, 0, 0);
741 rtmsg.rtmsg_dst_len = 8;
742 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
743 rtmsg.rtmsg_ifindex = dev->ifindex;
744 rtmsg.rtmsg_flags = RTF_UP|RTF_ADDRCONF;
745 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
746 ip6_route_add(&rtmsg);
749 static void sit_route_add(struct net_device *dev)
751 struct in6_rtmsg rtmsg;
753 memset(&rtmsg, 0, sizeof(rtmsg));
755 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
756 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
758 /* prefix length - 96 bytes "::d.d.d.d" */
759 rtmsg.rtmsg_dst_len = 96;
760 rtmsg.rtmsg_flags = RTF_UP|RTF_NONEXTHOP;
761 rtmsg.rtmsg_ifindex = dev->ifindex;
763 ip6_route_add(&rtmsg);
766 static void addrconf_add_lroute(struct net_device *dev)
768 struct in6_addr addr;
770 ipv6_addr_set(&addr, __constant_htonl(0xFE800000), 0, 0, 0);
771 addrconf_prefix_route(&addr, 10, dev, 0, RTF_ADDRCONF);
774 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
776 struct inet6_dev *idev;
778 ASSERT_RTNL();
780 if ((idev = ipv6_find_idev(dev)) == NULL)
781 return NULL;
783 /* Add default multicast route */
784 addrconf_add_mroute(dev);
786 /* Add link local route */
787 addrconf_add_lroute(dev);
788 return idev;
791 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
793 struct prefix_info *pinfo;
794 struct rt6_info *rt;
795 __u32 valid_lft;
796 __u32 prefered_lft;
797 int addr_type;
798 unsigned long rt_expires;
799 struct inet6_dev *in6_dev;
801 pinfo = (struct prefix_info *) opt;
803 if (len < sizeof(struct prefix_info)) {
804 ADBG(("addrconf: prefix option too short\n"));
805 return;
809 * Validation checks ([ADDRCONF], page 19)
812 addr_type = ipv6_addr_type(&pinfo->prefix);
814 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
815 return;
817 valid_lft = ntohl(pinfo->valid);
818 prefered_lft = ntohl(pinfo->prefered);
820 if (prefered_lft > valid_lft) {
821 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
822 return;
825 in6_dev = in6_dev_get(dev);
827 if (in6_dev == NULL) {
828 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
829 return;
833 * Two things going on here:
834 * 1) Add routes for on-link prefixes
835 * 2) Configure prefixes with the auto flag set
838 /* Avoid arithemtic overflow. Really, we could
839 save rt_expires in seconds, likely valid_lft,
840 but it would require division in fib gc, that it
841 not good.
843 if (valid_lft >= 0x7FFFFFFF/HZ)
844 rt_expires = 0;
845 else
846 rt_expires = jiffies + valid_lft * HZ;
848 rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
850 if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
851 if (rt->rt6i_flags&RTF_EXPIRES) {
852 if (pinfo->onlink == 0 || valid_lft == 0) {
853 ip6_del_rt(rt);
854 rt = NULL;
855 } else {
856 rt->rt6i_expires = rt_expires;
859 } else if (pinfo->onlink && valid_lft) {
860 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
861 dev, rt_expires, RTF_ADDRCONF|RTF_EXPIRES);
863 if (rt)
864 dst_release(&rt->u.dst);
866 /* Try to figure out our local address for this prefix */
868 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
869 struct inet6_ifaddr * ifp;
870 struct in6_addr addr;
871 int plen;
873 plen = pinfo->prefix_len >> 3;
875 #ifdef CONFIG_IPV6_EUI64
876 if (pinfo->prefix_len == 64) {
877 memcpy(&addr, &pinfo->prefix, 8);
878 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
879 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
880 in6_dev_put(in6_dev);
881 return;
883 goto ok;
885 #endif
886 #ifndef CONFIG_IPV6_NO_PB
887 if (pinfo->prefix_len == ((sizeof(struct in6_addr) - dev->addr_len)<<3)) {
888 memcpy(&addr, &pinfo->prefix, plen);
889 memcpy(addr.s6_addr + plen, dev->dev_addr,
890 dev->addr_len);
891 goto ok;
893 #endif
894 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n", pinfo->prefix_len);
895 in6_dev_put(in6_dev);
896 return;
900 ifp = ipv6_get_ifaddr(&addr, dev);
902 if (ifp == NULL && valid_lft) {
903 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
904 addr_type&IPV6_ADDR_SCOPE_MASK, 0);
906 if (ifp == NULL) {
907 in6_dev_put(in6_dev);
908 return;
911 addrconf_dad_start(ifp);
914 if (ifp && valid_lft == 0) {
915 ipv6_del_addr(ifp);
916 ifp = NULL;
919 if (ifp) {
920 int flags;
922 spin_lock(&ifp->lock);
923 ifp->valid_lft = valid_lft;
924 ifp->prefered_lft = prefered_lft;
925 ifp->tstamp = jiffies;
926 flags = ifp->flags;
927 ifp->flags &= ~IFA_F_DEPRECATED;
928 spin_unlock(&ifp->lock);
930 if (!(flags&IFA_F_TENTATIVE))
931 ipv6_ifa_notify((flags&IFA_F_DEPRECATED) ?
932 0 : RTM_NEWADDR, ifp);
933 in6_ifa_put(ifp);
936 in6_dev_put(in6_dev);
940 * Set destination address.
941 * Special case for SIT interfaces where we create a new "virtual"
942 * device.
944 int addrconf_set_dstaddr(void *arg)
946 struct in6_ifreq ireq;
947 struct net_device *dev;
948 int err = -EINVAL;
950 rtnl_lock();
952 err = -EFAULT;
953 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
954 goto err_exit;
956 dev = __dev_get_by_index(ireq.ifr6_ifindex);
958 err = -ENODEV;
959 if (dev == NULL)
960 goto err_exit;
962 if (dev->type == ARPHRD_SIT) {
963 struct ifreq ifr;
964 mm_segment_t oldfs;
965 struct ip_tunnel_parm p;
967 err = -EADDRNOTAVAIL;
968 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
969 goto err_exit;
971 memset(&p, 0, sizeof(p));
972 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
973 p.iph.saddr = 0;
974 p.iph.version = 4;
975 p.iph.ihl = 5;
976 p.iph.protocol = IPPROTO_IPV6;
977 p.iph.ttl = 64;
978 ifr.ifr_ifru.ifru_data = (void*)&p;
980 oldfs = get_fs(); set_fs(KERNEL_DS);
981 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
982 set_fs(oldfs);
984 if (err == 0) {
985 err = -ENOBUFS;
986 if ((dev = __dev_get_by_name(p.name)) == NULL)
987 goto err_exit;
988 err = dev_open(dev);
992 err_exit:
993 rtnl_unlock();
994 return err;
998 * Manual configuration of address on an interface
1000 static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
1002 struct inet6_ifaddr *ifp;
1003 struct inet6_dev *idev;
1004 struct net_device *dev;
1005 int scope;
1007 ASSERT_RTNL();
1009 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1010 return -ENODEV;
1012 if (!(dev->flags&IFF_UP))
1013 return -ENETDOWN;
1015 if ((idev = addrconf_add_dev(dev)) == NULL)
1016 return -ENOBUFS;
1018 scope = ipv6_addr_scope(pfx);
1020 if ((ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT)) != NULL) {
1021 addrconf_dad_start(ifp);
1022 in6_ifa_put(ifp);
1023 return 0;
1026 return -ENOBUFS;
1029 static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
1031 struct inet6_ifaddr *ifp;
1032 struct inet6_dev *idev;
1033 struct net_device *dev;
1035 if ((dev = __dev_get_by_index(ifindex)) == NULL)
1036 return -ENODEV;
1038 if ((idev = __in6_dev_get(dev)) == NULL)
1039 return -ENXIO;
1041 read_lock_bh(&idev->lock);
1042 for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
1043 if (ifp->prefix_len == plen &&
1044 (!memcmp(pfx, &ifp->addr, sizeof(struct in6_addr)))) {
1045 in6_ifa_hold(ifp);
1046 read_unlock_bh(&idev->lock);
1048 ipv6_del_addr(ifp);
1050 /* If the last address is deleted administratively,
1051 disable IPv6 on this interface.
1053 if (idev->addr_list == NULL)
1054 addrconf_ifdown(idev->dev, 1);
1055 return 0;
1058 read_unlock_bh(&idev->lock);
1059 return -EADDRNOTAVAIL;
1063 int addrconf_add_ifaddr(void *arg)
1065 struct in6_ifreq ireq;
1066 int err;
1068 if (!capable(CAP_NET_ADMIN))
1069 return -EPERM;
1071 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1072 return -EFAULT;
1074 rtnl_lock();
1075 err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1076 rtnl_unlock();
1077 return err;
1080 int addrconf_del_ifaddr(void *arg)
1082 struct in6_ifreq ireq;
1083 int err;
1085 if (!capable(CAP_NET_ADMIN))
1086 return -EPERM;
1088 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
1089 return -EFAULT;
1091 rtnl_lock();
1092 err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
1093 rtnl_unlock();
1094 return err;
1097 static void sit_add_v4_addrs(struct inet6_dev *idev)
1099 struct inet6_ifaddr * ifp;
1100 struct in6_addr addr;
1101 struct net_device *dev;
1102 int scope;
1104 ASSERT_RTNL();
1106 memset(&addr, 0, sizeof(struct in6_addr));
1107 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
1109 if (idev->dev->flags&IFF_POINTOPOINT) {
1110 addr.s6_addr32[0] = __constant_htonl(0xfe800000);
1111 scope = IFA_LINK;
1112 } else {
1113 scope = IPV6_ADDR_COMPATv4;
1116 if (addr.s6_addr32[3]) {
1117 ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
1118 if (ifp) {
1119 spin_lock_bh(&ifp->lock);
1120 ifp->flags &= ~IFA_F_TENTATIVE;
1121 spin_unlock_bh(&ifp->lock);
1122 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1123 in6_ifa_put(ifp);
1125 return;
1128 for (dev = dev_base; dev != NULL; dev = dev->next) {
1129 struct in_device * in_dev = __in_dev_get(dev);
1130 if (in_dev && (dev->flags & IFF_UP)) {
1131 struct in_ifaddr * ifa;
1133 int flag = scope;
1135 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1136 int plen;
1138 addr.s6_addr32[3] = ifa->ifa_local;
1140 if (ifa->ifa_scope == RT_SCOPE_LINK)
1141 continue;
1142 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
1143 if (idev->dev->flags&IFF_POINTOPOINT)
1144 continue;
1145 flag |= IFA_HOST;
1147 if (idev->dev->flags&IFF_POINTOPOINT)
1148 plen = 10;
1149 else
1150 plen = 96;
1152 ifp = ipv6_add_addr(idev, &addr, plen, flag,
1153 IFA_F_PERMANENT);
1154 if (ifp) {
1155 spin_lock_bh(&ifp->lock);
1156 ifp->flags &= ~IFA_F_TENTATIVE;
1157 spin_unlock_bh(&ifp->lock);
1158 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1159 in6_ifa_put(ifp);
1166 static void init_loopback(struct net_device *dev)
1168 struct in6_addr addr;
1169 struct inet6_dev *idev;
1170 struct inet6_ifaddr * ifp;
1172 /* ::1 */
1174 ASSERT_RTNL();
1176 memset(&addr, 0, sizeof(struct in6_addr));
1177 addr.s6_addr[15] = 1;
1179 if ((idev = ipv6_find_idev(dev)) == NULL) {
1180 printk(KERN_DEBUG "init loopback: add_dev failed\n");
1181 return;
1184 ifp = ipv6_add_addr(idev, &addr, 128, IFA_HOST, IFA_F_PERMANENT);
1185 if (ifp) {
1186 spin_lock_bh(&ifp->lock);
1187 ifp->flags &= ~IFA_F_TENTATIVE;
1188 spin_unlock_bh(&ifp->lock);
1189 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1190 in6_ifa_put(ifp);
1194 static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
1196 struct inet6_ifaddr * ifp;
1198 ifp = ipv6_add_addr(idev, addr, 10, IFA_LINK, IFA_F_PERMANENT);
1199 if (ifp) {
1200 addrconf_dad_start(ifp);
1201 in6_ifa_put(ifp);
1205 static void addrconf_dev_config(struct net_device *dev)
1207 struct in6_addr addr;
1208 struct inet6_dev * idev;
1210 ASSERT_RTNL();
1212 if ((dev->type != ARPHRD_ETHER) &&
1213 (dev->type != ARPHRD_FDDI) &&
1214 (dev->type != ARPHRD_IEEE802_TR)) {
1215 /* Alas, we support only Ethernet autoconfiguration. */
1216 return;
1219 idev = addrconf_add_dev(dev);
1220 if (idev == NULL)
1221 return;
1223 #ifdef CONFIG_IPV6_EUI64
1224 memset(&addr, 0, sizeof(struct in6_addr));
1226 addr.s6_addr[0] = 0xFE;
1227 addr.s6_addr[1] = 0x80;
1229 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
1230 addrconf_add_linklocal(idev, &addr);
1231 #endif
1233 #ifndef CONFIG_IPV6_NO_PB
1234 memset(&addr, 0, sizeof(struct in6_addr));
1236 addr.s6_addr[0] = 0xFE;
1237 addr.s6_addr[1] = 0x80;
1239 memcpy(addr.s6_addr + (sizeof(struct in6_addr) - dev->addr_len),
1240 dev->dev_addr, dev->addr_len);
1241 addrconf_add_linklocal(idev, &addr);
1242 #endif
1245 static void addrconf_sit_config(struct net_device *dev)
1247 struct inet6_dev *idev;
1249 ASSERT_RTNL();
1252 * Configure the tunnel with one of our IPv4
1253 * addresses... we should configure all of
1254 * our v4 addrs in the tunnel
1257 if ((idev = ipv6_find_idev(dev)) == NULL) {
1258 printk(KERN_DEBUG "init sit: add_dev failed\n");
1259 return;
1262 sit_add_v4_addrs(idev);
1264 if (dev->flags&IFF_POINTOPOINT) {
1265 addrconf_add_mroute(dev);
1266 addrconf_add_lroute(dev);
1267 } else
1268 sit_route_add(dev);
1272 int addrconf_notify(struct notifier_block *this, unsigned long event,
1273 void * data)
1275 struct net_device *dev;
1277 dev = (struct net_device *) data;
1279 switch(event) {
1280 case NETDEV_UP:
1281 switch(dev->type) {
1282 case ARPHRD_SIT:
1283 addrconf_sit_config(dev);
1284 break;
1286 case ARPHRD_LOOPBACK:
1287 init_loopback(dev);
1288 break;
1290 default:
1291 addrconf_dev_config(dev);
1292 break;
1294 break;
1296 case NETDEV_CHANGEMTU:
1297 if (dev->mtu >= IPV6_MIN_MTU) {
1298 struct inet6_dev *idev;
1300 if ((idev = __in6_dev_get(dev)) == NULL)
1301 break;
1302 idev->cnf.mtu6 = dev->mtu;
1303 rt6_mtu_change(dev, dev->mtu);
1304 break;
1307 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
1309 case NETDEV_DOWN:
1310 case NETDEV_UNREGISTER:
1312 * Remove all addresses from this interface.
1314 addrconf_ifdown(dev, event != NETDEV_DOWN);
1315 break;
1316 case NETDEV_CHANGE:
1317 break;
1320 return NOTIFY_OK;
1323 static int addrconf_ifdown(struct net_device *dev, int how)
1325 struct inet6_dev *idev;
1326 struct inet6_ifaddr *ifa, **bifa;
1327 int i;
1329 ASSERT_RTNL();
1331 rt6_ifdown(dev);
1332 neigh_ifdown(&nd_tbl, dev);
1334 idev = __in6_dev_get(dev);
1335 if (idev == NULL)
1336 return -ENODEV;
1338 /* Step 1: remove reference to ipv6 device from parent device.
1339 Do not dev_put!
1341 if (how == 1) {
1342 write_lock_bh(&addrconf_lock);
1343 dev->ip6_ptr = NULL;
1344 idev->dead = 1;
1345 write_unlock_bh(&addrconf_lock);
1348 /* Step 2: clear hash table */
1349 for (i=0; i<IN6_ADDR_HSIZE; i++) {
1350 bifa = &inet6_addr_lst[i];
1352 write_lock_bh(&addrconf_hash_lock);
1353 while ((ifa = *bifa) != NULL) {
1354 if (ifa->idev == idev) {
1355 *bifa = ifa->lst_next;
1356 ifa->lst_next = NULL;
1357 addrconf_del_timer(ifa);
1358 in6_ifa_put(ifa);
1359 continue;
1361 bifa = &ifa->lst_next;
1363 write_unlock_bh(&addrconf_hash_lock);
1366 /* Step 3: clear address list */
1368 write_lock_bh(&idev->lock);
1369 while ((ifa = idev->addr_list) != NULL) {
1370 idev->addr_list = ifa->if_next;
1371 ifa->if_next = NULL;
1372 ifa->dead = 1;
1373 addrconf_del_timer(ifa);
1374 write_unlock_bh(&idev->lock);
1376 ipv6_ifa_notify(RTM_DELADDR, ifa);
1377 in6_ifa_put(ifa);
1379 write_lock_bh(&idev->lock);
1381 write_unlock_bh(&idev->lock);
1383 /* Step 4: Discard multicast list */
1385 if (how == 1)
1386 ipv6_mc_destroy_dev(idev);
1387 else
1388 ipv6_mc_down(idev);
1390 /* Shot the device (if unregistered) */
1392 if (how == 1) {
1393 neigh_parms_release(&nd_tbl, idev->nd_parms);
1394 #ifdef CONFIG_SYSCTL
1395 addrconf_sysctl_unregister(&idev->cnf);
1396 #endif
1397 in6_dev_put(idev);
1399 return 0;
1402 static void addrconf_rs_timer(unsigned long data)
1404 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
1406 if (ifp->idev->cnf.forwarding)
1407 goto out;
1409 if (ifp->idev->if_flags & IF_RA_RCVD) {
1411 * Announcement received after solicitation
1412 * was sent
1414 goto out;
1417 spin_lock(&ifp->lock);
1418 if (ifp->probes++ <= ifp->idev->cnf.rtr_solicits) {
1419 struct in6_addr all_routers;
1421 addrconf_mod_timer(ifp, AC_RS,
1422 ifp->idev->cnf.rtr_solicit_interval);
1423 spin_unlock(&ifp->lock);
1425 ipv6_addr_all_routers(&all_routers);
1427 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
1428 } else {
1429 struct in6_rtmsg rtmsg;
1431 spin_unlock(&ifp->lock);
1433 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
1434 ifp->idev->dev->name);
1436 memset(&rtmsg, 0, sizeof(struct in6_rtmsg));
1437 rtmsg.rtmsg_type = RTMSG_NEWROUTE;
1438 rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
1439 rtmsg.rtmsg_flags = (RTF_ALLONLINK | RTF_ADDRCONF |
1440 RTF_DEFAULT | RTF_UP);
1442 rtmsg.rtmsg_ifindex = ifp->idev->dev->ifindex;
1444 ip6_route_add(&rtmsg);
1447 out:
1448 in6_ifa_put(ifp);
1452 * Duplicate Address Detection
1454 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
1456 struct net_device *dev;
1457 unsigned long rand_num;
1459 dev = ifp->idev->dev;
1461 addrconf_join_solict(dev, &ifp->addr);
1463 if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
1464 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0, RTF_ADDRCONF);
1466 net_srandom(ifp->addr.s6_addr32[3]);
1467 rand_num = net_random() % (ifp->idev->cnf.rtr_solicit_delay ? : 1);
1469 spin_lock_bh(&ifp->lock);
1471 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
1472 !(ifp->flags&IFA_F_TENTATIVE)) {
1473 ifp->flags &= ~IFA_F_TENTATIVE;
1474 spin_unlock_bh(&ifp->lock);
1476 addrconf_dad_completed(ifp);
1477 return;
1480 ifp->probes = ifp->idev->cnf.dad_transmits;
1481 addrconf_mod_timer(ifp, AC_DAD, rand_num);
1483 spin_unlock_bh(&ifp->lock);
1486 static void addrconf_dad_timer(unsigned long data)
1488 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
1489 struct in6_addr unspec;
1490 struct in6_addr mcaddr;
1492 spin_lock_bh(&ifp->lock);
1493 if (ifp->probes == 0) {
1495 * DAD was successful
1498 ifp->flags &= ~IFA_F_TENTATIVE;
1499 spin_unlock_bh(&ifp->lock);
1501 addrconf_dad_completed(ifp);
1503 in6_ifa_put(ifp);
1504 return;
1507 ifp->probes--;
1508 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->cnf.rtr_solicit_interval);
1509 spin_unlock_bh(&ifp->lock);
1511 /* send a neighbour solicitation for our addr */
1512 memset(&unspec, 0, sizeof(unspec));
1513 #ifdef CONFIG_IPV6_EUI64
1514 addrconf_addr_solict_mult_new(&ifp->addr, &mcaddr);
1515 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
1516 #endif
1517 #ifndef CONFIG_IPV6_NO_PB
1518 addrconf_addr_solict_mult_old(&ifp->addr, &mcaddr);
1519 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
1520 #endif
1522 in6_ifa_put(ifp);
1525 static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
1527 struct net_device * dev = ifp->idev->dev;
1530 * Configure the address for reception. Now it is valid.
1533 ipv6_ifa_notify(RTM_NEWADDR, ifp);
1535 /* If added prefix is link local and forwarding is off,
1536 start sending router solicitations.
1539 if (ifp->idev->cnf.forwarding == 0 &&
1540 (dev->flags&IFF_LOOPBACK) == 0 &&
1541 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
1542 struct in6_addr all_routers;
1544 ipv6_addr_all_routers(&all_routers);
1547 * If a host as already performed a random delay
1548 * [...] as part of DAD [...] there is no need
1549 * to delay again before sending the first RS
1551 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
1553 spin_lock_bh(&ifp->lock);
1554 ifp->probes = 1;
1555 ifp->idev->if_flags |= IF_RS_SENT;
1556 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
1557 spin_unlock_bh(&ifp->lock);
1561 #ifdef CONFIG_PROC_FS
1562 static int iface_proc_info(char *buffer, char **start, off_t offset,
1563 int length)
1565 struct inet6_ifaddr *ifp;
1566 int i;
1567 int len = 0;
1568 off_t pos=0;
1569 off_t begin=0;
1571 for (i=0; i < IN6_ADDR_HSIZE; i++) {
1572 read_lock_bh(&addrconf_hash_lock);
1573 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
1574 int j;
1576 for (j=0; j<16; j++) {
1577 sprintf(buffer + len, "%02x",
1578 ifp->addr.s6_addr[j]);
1579 len += 2;
1582 len += sprintf(buffer + len,
1583 " %02x %02x %02x %02x %8s\n",
1584 ifp->idev->dev->ifindex,
1585 ifp->prefix_len,
1586 ifp->scope,
1587 ifp->flags,
1588 ifp->idev->dev->name);
1589 pos=begin+len;
1590 if(pos<offset) {
1591 len=0;
1592 begin=pos;
1594 if(pos>offset+length) {
1595 read_unlock_bh(&addrconf_hash_lock);
1596 goto done;
1599 read_unlock_bh(&addrconf_hash_lock);
1602 done:
1604 *start=buffer+(offset-begin);
1605 len-=(offset-begin);
1606 if(len>length)
1607 len=length;
1608 if(len<0)
1609 len=0;
1610 return len;
1613 #endif /* CONFIG_PROC_FS */
1616 * Periodic address status verification
1619 void addrconf_verify(unsigned long foo)
1621 struct inet6_ifaddr *ifp;
1622 unsigned long now = jiffies;
1623 int i;
1625 for (i=0; i < IN6_ADDR_HSIZE; i++) {
1627 restart:
1628 write_lock(&addrconf_hash_lock);
1629 for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
1630 unsigned long age;
1632 if (ifp->flags & IFA_F_PERMANENT)
1633 continue;
1635 age = (now - ifp->tstamp) / HZ;
1637 if (age > ifp->valid_lft) {
1638 in6_ifa_hold(ifp);
1639 write_unlock(&addrconf_hash_lock);
1640 ipv6_del_addr(ifp);
1641 goto restart;
1642 } else if (age > ifp->prefered_lft) {
1643 int deprecate = 0;
1645 spin_lock(&ifp->lock);
1646 if (!(ifp->flags&IFA_F_DEPRECATED)) {
1647 deprecate = 1;
1648 ifp->flags |= IFA_F_DEPRECATED;
1650 spin_unlock(&ifp->lock);
1652 if (deprecate) {
1653 in6_ifa_hold(ifp);
1654 write_unlock(&addrconf_hash_lock);
1656 ipv6_ifa_notify(0, ifp);
1657 in6_ifa_put(ifp);
1658 goto restart;
1662 write_unlock(&addrconf_hash_lock);
1665 mod_timer(&addr_chk_timer, jiffies + ADDR_CHECK_FREQUENCY);
1668 #ifdef CONFIG_RTNETLINK
1670 static int
1671 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1673 struct rtattr **rta = arg;
1674 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
1675 struct in6_addr *pfx;
1677 pfx = NULL;
1678 if (rta[IFA_ADDRESS-1]) {
1679 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
1680 return -EINVAL;
1681 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
1683 if (rta[IFA_LOCAL-1]) {
1684 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
1685 return -EINVAL;
1686 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
1688 if (pfx == NULL)
1689 return -EINVAL;
1691 return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
1694 static int
1695 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1697 struct rtattr **rta = arg;
1698 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
1699 struct in6_addr *pfx;
1701 pfx = NULL;
1702 if (rta[IFA_ADDRESS-1]) {
1703 if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
1704 return -EINVAL;
1705 pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
1707 if (rta[IFA_LOCAL-1]) {
1708 if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
1709 return -EINVAL;
1710 pfx = RTA_DATA(rta[IFA_LOCAL-1]);
1712 if (pfx == NULL)
1713 return -EINVAL;
1715 return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
1718 static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
1719 u32 pid, u32 seq, int event)
1721 struct ifaddrmsg *ifm;
1722 struct nlmsghdr *nlh;
1723 struct ifa_cacheinfo ci;
1724 unsigned char *b = skb->tail;
1726 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
1727 ifm = NLMSG_DATA(nlh);
1728 ifm->ifa_family = AF_INET6;
1729 ifm->ifa_prefixlen = ifa->prefix_len;
1730 ifm->ifa_flags = ifa->flags;
1731 ifm->ifa_scope = RT_SCOPE_UNIVERSE;
1732 if (ifa->scope&IFA_HOST)
1733 ifm->ifa_scope = RT_SCOPE_HOST;
1734 else if (ifa->scope&IFA_LINK)
1735 ifm->ifa_scope = RT_SCOPE_LINK;
1736 else if (ifa->scope&IFA_SITE)
1737 ifm->ifa_scope = RT_SCOPE_SITE;
1738 ifm->ifa_index = ifa->idev->dev->ifindex;
1739 RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
1740 if (!(ifa->flags&IFA_F_PERMANENT)) {
1741 ci.ifa_prefered = ifa->prefered_lft;
1742 ci.ifa_valid = ifa->valid_lft;
1743 if (ci.ifa_prefered != 0xFFFFFFFF) {
1744 long tval = (jiffies - ifa->tstamp)/HZ;
1745 ci.ifa_prefered -= tval;
1746 if (ci.ifa_valid != 0xFFFFFFFF)
1747 ci.ifa_valid -= tval;
1749 RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
1751 nlh->nlmsg_len = skb->tail - b;
1752 return skb->len;
1754 nlmsg_failure:
1755 rtattr_failure:
1756 skb_trim(skb, b - skb->data);
1757 return -1;
1760 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
1762 int idx, ip_idx;
1763 int s_idx, s_ip_idx;
1764 struct inet6_ifaddr *ifa;
1766 s_idx = cb->args[0];
1767 s_ip_idx = ip_idx = cb->args[1];
1769 for (idx=0; idx < IN6_ADDR_HSIZE; idx++) {
1770 if (idx < s_idx)
1771 continue;
1772 if (idx > s_idx)
1773 s_ip_idx = 0;
1774 read_lock_bh(&addrconf_hash_lock);
1775 for (ifa=inet6_addr_lst[idx], ip_idx = 0; ifa;
1776 ifa = ifa->lst_next, ip_idx++) {
1777 if (ip_idx < s_ip_idx)
1778 continue;
1779 if (inet6_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
1780 cb->nlh->nlmsg_seq, RTM_NEWADDR) <= 0) {
1781 read_unlock_bh(&addrconf_hash_lock);
1782 goto done;
1785 read_unlock_bh(&addrconf_hash_lock);
1787 done:
1788 cb->args[0] = idx;
1789 cb->args[1] = ip_idx;
1791 return skb->len;
1794 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
1796 struct sk_buff *skb;
1797 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
1799 skb = alloc_skb(size, GFP_ATOMIC);
1800 if (!skb) {
1801 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, ENOBUFS);
1802 return;
1804 if (inet6_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
1805 kfree_skb(skb);
1806 netlink_set_err(rtnl, 0, RTMGRP_IPV6_IFADDR, EINVAL);
1807 return;
1809 NETLINK_CB(skb).dst_groups = RTMGRP_IPV6_IFADDR;
1810 netlink_broadcast(rtnl, skb, 0, RTMGRP_IPV6_IFADDR, GFP_ATOMIC);
1813 static struct rtnetlink_link inet6_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
1815 { NULL, NULL, },
1816 { NULL, NULL, },
1817 { NULL, NULL, },
1818 { NULL, NULL, },
1820 { inet6_rtm_newaddr, NULL, },
1821 { inet6_rtm_deladdr, NULL, },
1822 { NULL, inet6_dump_ifaddr, },
1823 { NULL, NULL, },
1825 { inet6_rtm_newroute, NULL, },
1826 { inet6_rtm_delroute, NULL, },
1827 { inet6_rtm_getroute, inet6_dump_fib, },
1828 { NULL, NULL, },
1830 #endif
1832 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
1834 #ifdef CONFIG_RTNETLINK
1835 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
1836 #endif
1837 switch (event) {
1838 case RTM_NEWADDR:
1839 ip6_rt_addr_add(&ifp->addr, ifp->idev->dev);
1840 break;
1841 case RTM_DELADDR:
1842 addrconf_leave_solict(ifp->idev->dev, &ifp->addr);
1843 if (!ipv6_chk_addr(&ifp->addr, ifp->idev->dev))
1844 ip6_rt_addr_del(&ifp->addr, ifp->idev->dev);
1845 break;
1849 #ifdef CONFIG_SYSCTL
1851 static
1852 int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
1853 void *buffer, size_t *lenp)
1855 int *valp = ctl->data;
1856 int val = *valp;
1857 int ret;
1859 ret = proc_dointvec(ctl, write, filp, buffer, lenp);
1861 if (write && *valp != val && valp != &ipv6_devconf_dflt.forwarding) {
1862 struct inet6_dev *idev = NULL;
1864 if (valp != &ipv6_devconf.forwarding) {
1865 struct net_device *dev = dev_get_by_index(ctl->ctl_name);
1866 if (dev) {
1867 idev = in6_dev_get(dev);
1868 dev_put(dev);
1870 if (idev == NULL)
1871 return ret;
1872 } else
1873 ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
1875 addrconf_forward_change(idev);
1877 if (*valp)
1878 rt6_purge_dflt_routers(0);
1879 if (idev)
1880 in6_dev_put(idev);
1883 return ret;
1886 static struct addrconf_sysctl_table
1888 struct ctl_table_header *sysctl_header;
1889 ctl_table addrconf_vars[11];
1890 ctl_table addrconf_dev[2];
1891 ctl_table addrconf_conf_dir[2];
1892 ctl_table addrconf_proto_dir[2];
1893 ctl_table addrconf_root_dir[2];
1894 } addrconf_sysctl = {
1895 NULL,
1896 {{NET_IPV6_FORWARDING, "forwarding",
1897 &ipv6_devconf.forwarding, sizeof(int), 0644, NULL,
1898 &addrconf_sysctl_forward},
1900 {NET_IPV6_HOP_LIMIT, "hop_limit",
1901 &ipv6_devconf.hop_limit, sizeof(int), 0644, NULL,
1902 &proc_dointvec},
1904 {NET_IPV6_MTU, "mtu",
1905 &ipv6_devconf.mtu6, sizeof(int), 0644, NULL,
1906 &proc_dointvec},
1908 {NET_IPV6_ACCEPT_RA, "accept_ra",
1909 &ipv6_devconf.accept_ra, sizeof(int), 0644, NULL,
1910 &proc_dointvec},
1912 {NET_IPV6_ACCEPT_REDIRECTS, "accept_redirects",
1913 &ipv6_devconf.accept_redirects, sizeof(int), 0644, NULL,
1914 &proc_dointvec},
1916 {NET_IPV6_AUTOCONF, "autoconf",
1917 &ipv6_devconf.autoconf, sizeof(int), 0644, NULL,
1918 &proc_dointvec},
1920 {NET_IPV6_DAD_TRANSMITS, "dad_transmits",
1921 &ipv6_devconf.dad_transmits, sizeof(int), 0644, NULL,
1922 &proc_dointvec},
1924 {NET_IPV6_RTR_SOLICITS, "router_solicitations",
1925 &ipv6_devconf.rtr_solicits, sizeof(int), 0644, NULL,
1926 &proc_dointvec},
1928 {NET_IPV6_RTR_SOLICIT_INTERVAL, "router_solicitation_interval",
1929 &ipv6_devconf.rtr_solicit_interval, sizeof(int), 0644, NULL,
1930 &proc_dointvec_jiffies},
1932 {NET_IPV6_RTR_SOLICIT_DELAY, "router_solicitation_delay",
1933 &ipv6_devconf.rtr_solicit_delay, sizeof(int), 0644, NULL,
1934 &proc_dointvec_jiffies},
1936 {0}},
1938 {{NET_PROTO_CONF_ALL, "all", NULL, 0, 0555, addrconf_sysctl.addrconf_vars},{0}},
1939 {{NET_IPV6_CONF, "conf", NULL, 0, 0555, addrconf_sysctl.addrconf_dev},{0}},
1940 {{NET_IPV6, "ipv6", NULL, 0, 0555, addrconf_sysctl.addrconf_conf_dir},{0}},
1941 {{CTL_NET, "net", NULL, 0, 0555, addrconf_sysctl.addrconf_proto_dir},{0}}
1944 static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
1946 int i;
1947 struct net_device *dev = idev ? idev->dev : NULL;
1948 struct addrconf_sysctl_table *t;
1950 t = kmalloc(sizeof(*t), GFP_KERNEL);
1951 if (t == NULL)
1952 return;
1953 memcpy(t, &addrconf_sysctl, sizeof(*t));
1954 for (i=0; i<sizeof(t->addrconf_vars)/sizeof(t->addrconf_vars[0])-1; i++) {
1955 t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
1956 t->addrconf_vars[i].de = NULL;
1958 if (dev) {
1959 t->addrconf_dev[0].procname = dev->name;
1960 t->addrconf_dev[0].ctl_name = dev->ifindex;
1961 } else {
1962 t->addrconf_dev[0].procname = "default";
1963 t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
1965 t->addrconf_dev[0].child = t->addrconf_vars;
1966 t->addrconf_dev[0].de = NULL;
1967 t->addrconf_conf_dir[0].child = t->addrconf_dev;
1968 t->addrconf_conf_dir[0].de = NULL;
1969 t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
1970 t->addrconf_proto_dir[0].de = NULL;
1971 t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
1972 t->addrconf_root_dir[0].de = NULL;
1974 t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
1975 if (t->sysctl_header == NULL)
1976 kfree(t);
1977 else
1978 p->sysctl = t;
1981 static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
1983 if (p->sysctl) {
1984 struct addrconf_sysctl_table *t = p->sysctl;
1985 p->sysctl = NULL;
1986 unregister_sysctl_table(t->sysctl_header);
1987 kfree(t);
1992 #endif
1995 * Init / cleanup code
1998 void __init addrconf_init(void)
2000 #ifdef MODULE
2001 struct net_device *dev;
2003 /* This takes sense only during module load. */
2004 rtnl_lock();
2005 for (dev = dev_base; dev; dev = dev->next) {
2006 if (!(dev->flags&IFF_UP))
2007 continue;
2009 switch (dev->type) {
2010 case ARPHRD_LOOPBACK:
2011 init_loopback(dev);
2012 break;
2013 case ARPHRD_ETHER:
2014 case ARPHRD_FDDI:
2015 case ARPHRD_IEEE802_TR:
2016 addrconf_dev_config(dev);
2017 break;
2018 default:
2019 /* Ignore all other */
2022 rtnl_unlock();
2023 #endif
2025 #ifdef CONFIG_PROC_FS
2026 proc_net_create("if_inet6", 0, iface_proc_info);
2027 #endif
2029 addr_chk_timer.expires = jiffies + ADDR_CHECK_FREQUENCY;
2030 add_timer(&addr_chk_timer);
2031 #ifdef CONFIG_RTNETLINK
2032 rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
2033 #endif
2034 #ifdef CONFIG_SYSCTL
2035 addrconf_sysctl.sysctl_header =
2036 register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
2037 addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
2038 #endif
2041 #ifdef MODULE
2042 void addrconf_cleanup(void)
2044 struct net_device *dev;
2045 struct inet6_dev *idev;
2046 struct inet6_ifaddr *ifa;
2047 int i;
2049 #ifdef CONFIG_RTNETLINK
2050 rtnetlink_links[PF_INET6] = NULL;
2051 #endif
2052 #ifdef CONFIG_SYSCTL
2053 addrconf_sysctl_unregister(&ipv6_devconf_dflt);
2054 addrconf_sysctl_unregister(&ipv6_devconf);
2055 #endif
2057 rtnl_lock();
2060 * clean dev list.
2063 for (dev=dev_base; dev; dev=dev->next) {
2064 if ((idev = __in6_dev_get(dev)) == NULL)
2065 continue;
2066 addrconf_ifdown(dev, 1);
2070 * Check hash table.
2073 write_lock_bh(&addrconf_hash_lock);
2074 for (i=0; i < IN6_ADDR_HSIZE; i++) {
2075 for (ifa=inet6_addr_lst[i]; ifa; ) {
2076 struct inet6_ifaddr *bifa;
2078 bifa = ifa;
2079 ifa = ifa->lst_next;
2080 printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
2081 /* Do not free it; something is wrong.
2082 Now we can investigate it with debugger.
2086 write_unlock_bh(&addrconf_hash_lock);
2088 del_timer(&addr_chk_timer);
2090 rtnl_unlock();
2092 #ifdef CONFIG_PROC_FS
2093 proc_net_remove("if_inet6");
2094 #endif
2096 #endif /* MODULE */