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.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.
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill doube kfree on module
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>
41 #include <linux/sysctl.h>
43 #include <linux/delay.h>
45 #include <linux/proc_fs.h>
50 #include <net/protocol.h>
51 #include <net/ndisc.h>
52 #include <net/ip6_route.h>
53 #include <net/addrconf.h>
55 #include <linux/if_tunnel.h>
56 #include <linux/rtnetlink.h>
58 #include <asm/uaccess.h>
60 /* Set to 3 to get tracing... */
64 #define ADBG(x) printk x
70 static void addrconf_sysctl_register(struct inet6_dev
*idev
, struct ipv6_devconf
*p
);
71 static void addrconf_sysctl_unregister(struct ipv6_devconf
*p
);
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
=
101 IPV6_DEFAULT_HOPLIMIT
, /* hop limit */
102 IPV6_MIN_MTU
, /* mtu */
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
=
115 IPV6_DEFAULT_HOPLIMIT
, /* hop limit */
116 IPV6_MIN_MTU
, /* mtu */
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
)
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
;
147 case __constant_htonl(0x00020000):
148 type
|= IPV6_ADDR_LINKLOCAL
;
151 case __constant_htonl(0x00050000):
152 type
|= IPV6_ADDR_SITELOCAL
;
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
))
188 enum addrconf_timer_t
195 static void addrconf_mod_timer(struct inet6_ifaddr
*ifp
,
196 enum addrconf_timer_t what
,
199 if (!del_timer(&ifp
->timer
))
204 ifp
->timer
.function
= addrconf_dad_timer
;
207 ifp
->timer
.function
= addrconf_rs_timer
;
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");
228 printk("Freeing alive inet6 device %p\n", idev
);
235 static struct inet6_dev
* ipv6_add_dev(struct net_device
*dev
)
237 struct inet6_dev
*ndev
;
241 if (dev
->mtu
< IPV6_MIN_MTU
)
244 ndev
= kmalloc(sizeof(struct inet6_dev
), GFP_KERNEL
);
247 memset(ndev
, 0, sizeof(struct inet6_dev
));
249 ndev
->lock
= RW_LOCK_UNLOCKED
;
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
) {
260 /* We refer to the device */
263 write_lock_bh(&addrconf_lock
);
265 /* One reference from device */
267 write_unlock_bh(&addrconf_lock
);
270 neigh_sysctl_register(dev
, ndev
->nd_parms
, NET_IPV6
, NET_IPV6_NEIGH
, "ipv6");
271 addrconf_sysctl_register(ndev
, &ndev
->cnf
);
277 static struct inet6_dev
* ipv6_find_idev(struct net_device
*dev
)
279 struct inet6_dev
*idev
;
283 if ((idev
= __in6_dev_get(dev
)) == NULL
) {
284 if ((idev
= ipv6_add_dev(dev
)) == NULL
)
286 if (dev
->flags
&IFF_UP
)
292 static void addrconf_forward_change(struct inet6_dev
*idev
)
294 struct net_device
*dev
;
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
);
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
);
324 printk("Freeing alive inet6 address %p\n", 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
;
340 ifa
= kmalloc(sizeof(struct inet6_ifaddr
), GFP_ATOMIC
);
343 ADBG(("ipv6_add_addr: malloc failed\n"));
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
;
354 ifa
->prefix_len
= pfxlen
;
355 ifa
->flags
= flags
| IFA_F_TENTATIVE
;
357 read_lock(&addrconf_lock
);
359 read_unlock(&addrconf_lock
);
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
;
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
;
384 write_unlock_bh(&idev
->lock
);
385 read_unlock(&addrconf_lock
);
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
;
398 hash
= ipv6_addr_hash(&ifp
->addr
);
402 write_lock_bh(&addrconf_hash_lock
);
403 for (ifap
= &inet6_addr_lst
[hash
]; (ifa
=*ifap
) != NULL
;
404 ifap
= &ifa
->lst_next
) {
406 *ifap
= ifa
->lst_next
;
408 ifa
->lst_next
= NULL
;
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
) {
418 *ifap
= ifa
->if_next
;
424 write_unlock_bh(&idev
->lock
);
426 ipv6_ifa_notify(RTM_DELADDR
, ifp
);
429 addrconf_del_timer(ifp
);
435 * Choose an apropriate source address
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
)
446 struct inet6_ifaddr
*ifp
= NULL
;
447 struct inet6_ifaddr
*match
= NULL
;
448 struct net_device
*dev
= NULL
;
449 struct inet6_dev
*idev
;
453 rt
= (struct rt6_info
*) dst
;
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
468 * search dev and walk through dev addresses
472 if (dev
->flags
& IFF_LOOPBACK
)
475 read_lock(&addrconf_lock
);
476 idev
= __in6_dev_get(dev
);
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
))) {
483 read_unlock_bh(&idev
->lock
);
484 read_unlock(&addrconf_lock
);
488 if (!match
&& !(ifp
->flags
& IFA_F_TENTATIVE
)) {
494 read_unlock_bh(&idev
->lock
);
496 read_unlock(&addrconf_lock
);
499 if (scope
== IFA_LINK
)
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
);
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
))) {
516 read_unlock_bh(&idev
->lock
);
517 goto out_unlock_base
;
520 if (!match
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
526 read_unlock_bh(&idev
->lock
);
531 read_unlock(&addrconf_lock
);
532 read_unlock(&dev_base_lock
);
540 err
= -EADDRNOTAVAIL
;
542 ipv6_addr_copy(saddr
, &ifp
->addr
);
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
);
569 read_unlock_bh(&idev
->lock
);
571 read_unlock(&addrconf_lock
);
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
)))
589 read_unlock_bh(&addrconf_hash_lock
);
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
))) {
608 read_unlock_bh(&addrconf_hash_lock
);
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
);
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
))
638 #ifndef CONFIG_IPV6_NO_PB
639 addrconf_addr_solict_mult_old(addr
, &maddr
);
640 ipv6_dev_mc_inc(dev
, &maddr
);
642 #ifdef CONFIG_IPV6_EUI64
643 addrconf_addr_solict_mult_new(addr
, &maddr
);
644 ipv6_dev_mc_inc(dev
, &maddr
);
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
))
655 #ifndef CONFIG_IPV6_NO_PB
656 addrconf_addr_solict_mult_old(addr
, &maddr
);
657 ipv6_dev_mc_dec(dev
, &maddr
);
659 #ifdef CONFIG_IPV6_EUI64
660 addrconf_addr_solict_mult_new(addr
, &maddr
);
661 ipv6_dev_mc_dec(dev
, &maddr
);
666 #ifdef CONFIG_IPV6_EUI64
667 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
)
672 case ARPHRD_IEEE802_TR
:
673 if (dev
->addr_len
!= ETH_ALEN
)
675 memcpy(eui
, dev
->dev_addr
, 3);
676 memcpy(eui
+ 5, dev
->dev_addr
+3, 3);
685 static int ipv6_inherit_eui64(u8
*eui
, struct inet6_dev
*idev
)
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);
698 read_unlock_bh(&idev
->lock
);
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
;
780 if ((idev
= ipv6_find_idev(dev
)) == NULL
)
783 /* Add default multicast route */
784 addrconf_add_mroute(dev
);
786 /* Add link local route */
787 addrconf_add_lroute(dev
);
791 void addrconf_prefix_rcv(struct net_device
*dev
, u8
*opt
, int len
)
793 struct prefix_info
*pinfo
;
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"));
809 * Validation checks ([ADDRCONF], page 19)
812 addr_type
= ipv6_addr_type(&pinfo
->prefix
);
814 if (addr_type
& (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
))
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");
825 in6_dev
= in6_dev_get(dev
);
827 if (in6_dev
== NULL
) {
828 printk(KERN_DEBUG
"addrconf: device %s not configured\n", dev
->name
);
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
843 if (valid_lft
>= 0x7FFFFFFF/HZ
)
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) {
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
);
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
;
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
);
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
,
894 printk(KERN_DEBUG
"IPv6 addrconf: prefix with wrong length %d\n", pinfo
->prefix_len
);
895 in6_dev_put(in6_dev
);
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);
907 in6_dev_put(in6_dev
);
911 addrconf_dad_start(ifp
);
914 if (ifp
&& valid_lft
== 0) {
922 spin_lock(&ifp
->lock
);
923 ifp
->valid_lft
= valid_lft
;
924 ifp
->prefered_lft
= prefered_lft
;
925 ifp
->tstamp
= jiffies
;
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
);
936 in6_dev_put(in6_dev
);
940 * Set destination address.
941 * Special case for SIT interfaces where we create a new "virtual"
944 int addrconf_set_dstaddr(void *arg
)
946 struct in6_ifreq ireq
;
947 struct net_device
*dev
;
953 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
956 dev
= __dev_get_by_index(ireq
.ifr6_ifindex
);
962 if (dev
->type
== ARPHRD_SIT
) {
965 struct ip_tunnel_parm p
;
967 err
= -EADDRNOTAVAIL
;
968 if (!(ipv6_addr_type(&ireq
.ifr6_addr
) & IPV6_ADDR_COMPATv4
))
971 memset(&p
, 0, sizeof(p
));
972 p
.iph
.daddr
= ireq
.ifr6_addr
.s6_addr32
[3];
976 p
.iph
.protocol
= IPPROTO_IPV6
;
978 ifr
.ifr_ifru
.ifru_data
= (void*)&p
;
980 oldfs
= get_fs(); set_fs(KERNEL_DS
);
981 err
= dev
->do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
986 if ((dev
= __dev_get_by_name(p
.name
)) == NULL
)
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
;
1009 if ((dev
= __dev_get_by_index(ifindex
)) == NULL
)
1012 if (!(dev
->flags
&IFF_UP
))
1015 if ((idev
= addrconf_add_dev(dev
)) == NULL
)
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
);
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
)
1038 if ((idev
= __in6_dev_get(dev
)) == NULL
)
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
)))) {
1046 read_unlock_bh(&idev
->lock
);
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);
1058 read_unlock_bh(&idev
->lock
);
1059 return -EADDRNOTAVAIL
;
1063 int addrconf_add_ifaddr(void *arg
)
1065 struct in6_ifreq ireq
;
1068 if (!capable(CAP_NET_ADMIN
))
1071 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
1075 err
= inet6_addr_add(ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, ireq
.ifr6_prefixlen
);
1080 int addrconf_del_ifaddr(void *arg
)
1082 struct in6_ifreq ireq
;
1085 if (!capable(CAP_NET_ADMIN
))
1088 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
1092 err
= inet6_addr_del(ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, ireq
.ifr6_prefixlen
);
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
;
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);
1113 scope
= IPV6_ADDR_COMPATv4
;
1116 if (addr
.s6_addr32
[3]) {
1117 ifp
= ipv6_add_addr(idev
, &addr
, 128, scope
, IFA_F_PERMANENT
);
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
);
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
;
1135 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
1138 addr
.s6_addr32
[3] = ifa
->ifa_local
;
1140 if (ifa
->ifa_scope
== RT_SCOPE_LINK
)
1142 if (ifa
->ifa_scope
>= RT_SCOPE_HOST
) {
1143 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
1147 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
1152 ifp
= ipv6_add_addr(idev
, &addr
, plen
, flag
,
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
);
1166 static void init_loopback(struct net_device
*dev
)
1168 struct in6_addr addr
;
1169 struct inet6_dev
*idev
;
1170 struct inet6_ifaddr
* ifp
;
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");
1184 ifp
= ipv6_add_addr(idev
, &addr
, 128, IFA_HOST
, IFA_F_PERMANENT
);
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
);
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
);
1200 addrconf_dad_start(ifp
);
1205 static void addrconf_dev_config(struct net_device
*dev
)
1207 struct in6_addr addr
;
1208 struct inet6_dev
* idev
;
1212 if ((dev
->type
!= ARPHRD_ETHER
) &&
1213 (dev
->type
!= ARPHRD_FDDI
) &&
1214 (dev
->type
!= ARPHRD_IEEE802_TR
)) {
1215 /* Alas, we support only Ethernet autoconfiguration. */
1219 idev
= addrconf_add_dev(dev
);
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
);
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
);
1245 static void addrconf_sit_config(struct net_device
*dev
)
1247 struct inet6_dev
*idev
;
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");
1262 sit_add_v4_addrs(idev
);
1264 if (dev
->flags
&IFF_POINTOPOINT
) {
1265 addrconf_add_mroute(dev
);
1266 addrconf_add_lroute(dev
);
1272 int addrconf_notify(struct notifier_block
*this, unsigned long event
,
1275 struct net_device
*dev
;
1277 dev
= (struct net_device
*) data
;
1283 addrconf_sit_config(dev
);
1286 case ARPHRD_LOOPBACK
:
1291 addrconf_dev_config(dev
);
1296 case NETDEV_CHANGEMTU
:
1297 if (dev
->mtu
>= IPV6_MIN_MTU
) {
1298 struct inet6_dev
*idev
;
1300 if ((idev
= __in6_dev_get(dev
)) == NULL
)
1302 idev
->cnf
.mtu6
= dev
->mtu
;
1303 rt6_mtu_change(dev
, dev
->mtu
);
1307 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
1310 case NETDEV_UNREGISTER
:
1312 * Remove all addresses from this interface.
1314 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
1323 static int addrconf_ifdown(struct net_device
*dev
, int how
)
1325 struct inet6_dev
*idev
;
1326 struct inet6_ifaddr
*ifa
, **bifa
;
1332 neigh_ifdown(&nd_tbl
, dev
);
1334 idev
= __in6_dev_get(dev
);
1338 /* Step 1: remove reference to ipv6 device from parent device.
1342 write_lock_bh(&addrconf_lock
);
1343 dev
->ip6_ptr
= NULL
;
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
);
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
;
1373 addrconf_del_timer(ifa
);
1374 write_unlock_bh(&idev
->lock
);
1376 ipv6_ifa_notify(RTM_DELADDR
, ifa
);
1379 write_lock_bh(&idev
->lock
);
1381 write_unlock_bh(&idev
->lock
);
1383 /* Step 4: Discard multicast list */
1386 ipv6_mc_destroy_dev(idev
);
1390 /* Shot the device (if unregistered) */
1393 neigh_parms_release(&nd_tbl
, idev
->nd_parms
);
1394 #ifdef CONFIG_SYSCTL
1395 addrconf_sysctl_unregister(&idev
->cnf
);
1402 static void addrconf_rs_timer(unsigned long data
)
1404 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*) data
;
1406 if (ifp
->idev
->cnf
.forwarding
)
1409 if (ifp
->idev
->if_flags
& IF_RA_RCVD
) {
1411 * Announcement received after solicitation
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
,
1565 struct inet6_ifaddr
*ifp
;
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
) {
1576 for (j
=0; j
<16; j
++) {
1577 sprintf(buffer
+ len
, "%02x",
1578 ifp
->addr
.s6_addr
[j
]);
1582 len
+= sprintf(buffer
+ len
,
1583 " %02x %02x %02x %02x %8s\n",
1584 ifp
->idev
->dev
->ifindex
,
1588 ifp
->idev
->dev
->name
);
1594 if(pos
>offset
+length
) {
1595 read_unlock_bh(&addrconf_hash_lock
);
1599 read_unlock_bh(&addrconf_hash_lock
);
1604 *start
=buffer
+(offset
-begin
);
1605 len
-=(offset
-begin
);
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
;
1625 for (i
=0; i
< IN6_ADDR_HSIZE
; i
++) {
1628 write_lock(&addrconf_hash_lock
);
1629 for (ifp
=inet6_addr_lst
[i
]; ifp
; ifp
=ifp
->lst_next
) {
1632 if (ifp
->flags
& IFA_F_PERMANENT
)
1635 age
= (now
- ifp
->tstamp
) / HZ
;
1637 if (age
> ifp
->valid_lft
) {
1639 write_unlock(&addrconf_hash_lock
);
1642 } else if (age
> ifp
->prefered_lft
) {
1645 spin_lock(&ifp
->lock
);
1646 if (!(ifp
->flags
&IFA_F_DEPRECATED
)) {
1648 ifp
->flags
|= IFA_F_DEPRECATED
;
1650 spin_unlock(&ifp
->lock
);
1654 write_unlock(&addrconf_hash_lock
);
1656 ipv6_ifa_notify(0, ifp
);
1662 write_unlock(&addrconf_hash_lock
);
1665 mod_timer(&addr_chk_timer
, jiffies
+ ADDR_CHECK_FREQUENCY
);
1668 #ifdef CONFIG_RTNETLINK
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
;
1678 if (rta
[IFA_ADDRESS
-1]) {
1679 if (RTA_PAYLOAD(rta
[IFA_ADDRESS
-1]) < sizeof(*pfx
))
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
)))
1686 pfx
= RTA_DATA(rta
[IFA_LOCAL
-1]);
1691 return inet6_addr_del(ifm
->ifa_index
, pfx
, ifm
->ifa_prefixlen
);
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
;
1702 if (rta
[IFA_ADDRESS
-1]) {
1703 if (RTA_PAYLOAD(rta
[IFA_ADDRESS
-1]) < sizeof(*pfx
))
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
)))
1710 pfx
= RTA_DATA(rta
[IFA_LOCAL
-1]);
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
;
1756 skb_trim(skb
, b
- skb
->data
);
1760 static int inet6_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
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
++) {
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
)
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
);
1785 read_unlock_bh(&addrconf_hash_lock
);
1789 cb
->args
[1] = ip_idx
;
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
);
1801 netlink_set_err(rtnl
, 0, RTMGRP_IPV6_IFADDR
, ENOBUFS
);
1804 if (inet6_fill_ifaddr(skb
, ifa
, 0, 0, event
) < 0) {
1806 netlink_set_err(rtnl
, 0, RTMGRP_IPV6_IFADDR
, EINVAL
);
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] =
1820 { inet6_rtm_newaddr
, NULL
, },
1821 { inet6_rtm_deladdr
, NULL
, },
1822 { NULL
, inet6_dump_ifaddr
, },
1825 { inet6_rtm_newroute
, NULL
, },
1826 { inet6_rtm_delroute
, NULL
, },
1827 { inet6_rtm_getroute
, inet6_dump_fib
, },
1832 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
1834 #ifdef CONFIG_RTNETLINK
1835 inet6_ifa_notify(event
? : RTM_NEWADDR
, ifp
);
1839 ip6_rt_addr_add(&ifp
->addr
, ifp
->idev
->dev
);
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
);
1849 #ifdef CONFIG_SYSCTL
1852 int addrconf_sysctl_forward(ctl_table
*ctl
, int write
, struct file
* filp
,
1853 void *buffer
, size_t *lenp
)
1855 int *valp
= ctl
->data
;
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
);
1867 idev
= in6_dev_get(dev
);
1873 ipv6_devconf_dflt
.forwarding
= ipv6_devconf
.forwarding
;
1875 addrconf_forward_change(idev
);
1878 rt6_purge_dflt_routers(0);
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
= {
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
,
1904 {NET_IPV6_MTU
, "mtu",
1905 &ipv6_devconf
.mtu6
, sizeof(int), 0644, NULL
,
1908 {NET_IPV6_ACCEPT_RA
, "accept_ra",
1909 &ipv6_devconf
.accept_ra
, sizeof(int), 0644, NULL
,
1912 {NET_IPV6_ACCEPT_REDIRECTS
, "accept_redirects",
1913 &ipv6_devconf
.accept_redirects
, sizeof(int), 0644, NULL
,
1916 {NET_IPV6_AUTOCONF
, "autoconf",
1917 &ipv6_devconf
.autoconf
, sizeof(int), 0644, NULL
,
1920 {NET_IPV6_DAD_TRANSMITS
, "dad_transmits",
1921 &ipv6_devconf
.dad_transmits
, sizeof(int), 0644, NULL
,
1924 {NET_IPV6_RTR_SOLICITS
, "router_solicitations",
1925 &ipv6_devconf
.rtr_solicits
, sizeof(int), 0644, NULL
,
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
},
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
)
1947 struct net_device
*dev
= idev
? idev
->dev
: NULL
;
1948 struct addrconf_sysctl_table
*t
;
1950 t
= kmalloc(sizeof(*t
), GFP_KERNEL
);
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
;
1959 t
->addrconf_dev
[0].procname
= dev
->name
;
1960 t
->addrconf_dev
[0].ctl_name
= dev
->ifindex
;
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
)
1981 static void addrconf_sysctl_unregister(struct ipv6_devconf
*p
)
1984 struct addrconf_sysctl_table
*t
= p
->sysctl
;
1986 unregister_sysctl_table(t
->sysctl_header
);
1995 * Init / cleanup code
1998 void __init
addrconf_init(void)
2001 struct net_device
*dev
;
2003 /* This takes sense only during module load. */
2005 for (dev
= dev_base
; dev
; dev
= dev
->next
) {
2006 if (!(dev
->flags
&IFF_UP
))
2009 switch (dev
->type
) {
2010 case ARPHRD_LOOPBACK
:
2015 case ARPHRD_IEEE802_TR
:
2016 addrconf_dev_config(dev
);
2019 /* Ignore all other */
2025 #ifdef CONFIG_PROC_FS
2026 proc_net_create("if_inet6", 0, iface_proc_info
);
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
;
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
);
2042 void addrconf_cleanup(void)
2044 struct net_device
*dev
;
2045 struct inet6_dev
*idev
;
2046 struct inet6_ifaddr
*ifa
;
2049 #ifdef CONFIG_RTNETLINK
2050 rtnetlink_links
[PF_INET6
] = NULL
;
2052 #ifdef CONFIG_SYSCTL
2053 addrconf_sysctl_unregister(&ipv6_devconf_dflt
);
2054 addrconf_sysctl_unregister(&ipv6_devconf
);
2063 for (dev
=dev_base
; dev
; dev
=dev
->next
) {
2064 if ((idev
= __in6_dev_get(dev
)) == NULL
)
2066 addrconf_ifdown(dev
, 1);
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
;
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
);
2092 #ifdef CONFIG_PROC_FS
2093 proc_net_remove("if_inet6");