2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill double kfree on module
24 * Maciej W. Rozycki : FDDI support
25 * sekiya@USAGI : Don't send too many RS
27 * yoshfuji@USAGI : Fixed interval between DAD
29 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
30 * address validation timer.
31 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
33 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
34 * address on a same interface.
35 * YOSHIFUJI Hideaki @USAGI : ARCnet support
36 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
38 * YOSHIFUJI Hideaki @USAGI : improved source address
39 * selection; consider scope,
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/socket.h>
46 #include <linux/sockios.h>
47 #include <linux/net.h>
48 #include <linux/in6.h>
49 #include <linux/netdevice.h>
50 #include <linux/if_addr.h>
51 #include <linux/if_arp.h>
52 #include <linux/if_arcnet.h>
53 #include <linux/if_infiniband.h>
54 #include <linux/route.h>
55 #include <linux/inetdevice.h>
56 #include <linux/init.h>
58 #include <linux/sysctl.h>
60 #include <linux/capability.h>
61 #include <linux/delay.h>
62 #include <linux/notifier.h>
63 #include <linux/string.h>
69 #include <net/protocol.h>
70 #include <net/ndisc.h>
71 #include <net/ip6_route.h>
72 #include <net/addrconf.h>
75 #include <net/netlink.h>
76 #include <linux/if_tunnel.h>
77 #include <linux/rtnetlink.h>
79 #ifdef CONFIG_IPV6_PRIVACY
80 #include <linux/random.h>
83 #include <asm/uaccess.h>
85 #include <linux/proc_fs.h>
86 #include <linux/seq_file.h>
88 /* Set to 3 to get tracing... */
92 #define ADBG(x) printk x
97 #define INFINITY_LIFE_TIME 0xFFFFFFFF
98 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
101 static void addrconf_sysctl_register(struct inet6_dev
*idev
, struct ipv6_devconf
*p
);
102 static void addrconf_sysctl_unregister(struct ipv6_devconf
*p
);
105 #ifdef CONFIG_IPV6_PRIVACY
106 static int __ipv6_regen_rndid(struct inet6_dev
*idev
);
107 static int __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
);
108 static void ipv6_regen_rndid(unsigned long data
);
110 static int desync_factor
= MAX_DESYNC_FACTOR
* HZ
;
113 static int ipv6_count_addresses(struct inet6_dev
*idev
);
116 * Configured unicast address hash table
118 static struct inet6_ifaddr
*inet6_addr_lst
[IN6_ADDR_HSIZE
];
119 static DEFINE_RWLOCK(addrconf_hash_lock
);
121 static void addrconf_verify(unsigned long);
123 static DEFINE_TIMER(addr_chk_timer
, addrconf_verify
, 0, 0);
124 static DEFINE_SPINLOCK(addrconf_verify_lock
);
126 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
);
127 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
);
129 static int addrconf_ifdown(struct net_device
*dev
, int how
);
131 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
, u32 flags
);
132 static void addrconf_dad_timer(unsigned long data
);
133 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
);
134 static void addrconf_dad_run(struct inet6_dev
*idev
);
135 static void addrconf_rs_timer(unsigned long data
);
136 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
137 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
139 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
140 struct prefix_info
*pinfo
);
141 static int ipv6_chk_same_addr(const struct in6_addr
*addr
, struct net_device
*dev
);
143 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain
);
145 struct ipv6_devconf ipv6_devconf __read_mostly
= {
147 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
148 .mtu6
= IPV6_MIN_MTU
,
150 .accept_redirects
= 1,
152 .force_mld_version
= 0,
154 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
155 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
156 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
157 #ifdef CONFIG_IPV6_PRIVACY
159 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
160 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
161 .regen_max_retry
= REGEN_MAX_RETRY
,
162 .max_desync_factor
= MAX_DESYNC_FACTOR
,
164 .max_addresses
= IPV6_MAX_ADDRESSES
,
165 .accept_ra_defrtr
= 1,
166 .accept_ra_pinfo
= 1,
167 #ifdef CONFIG_IPV6_ROUTER_PREF
168 .accept_ra_rtr_pref
= 1,
169 .rtr_probe_interval
= 60 * HZ
,
170 #ifdef CONFIG_IPV6_ROUTE_INFO
171 .accept_ra_rt_info_max_plen
= 0,
177 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly
= {
179 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
180 .mtu6
= IPV6_MIN_MTU
,
182 .accept_redirects
= 1,
185 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
186 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
187 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
188 #ifdef CONFIG_IPV6_PRIVACY
190 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
191 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
192 .regen_max_retry
= REGEN_MAX_RETRY
,
193 .max_desync_factor
= MAX_DESYNC_FACTOR
,
195 .max_addresses
= IPV6_MAX_ADDRESSES
,
196 .accept_ra_defrtr
= 1,
197 .accept_ra_pinfo
= 1,
198 #ifdef CONFIG_IPV6_ROUTER_PREF
199 .accept_ra_rtr_pref
= 1,
200 .rtr_probe_interval
= 60 * HZ
,
201 #ifdef CONFIG_IPV6_ROUTE_INFO
202 .accept_ra_rt_info_max_plen
= 0,
208 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
210 const struct in6_addr in6addr_any
= IN6ADDR_ANY_INIT
;
212 const struct in6_addr in6addr_loopback
= IN6ADDR_LOOPBACK_INIT
;
214 #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
216 static inline unsigned ipv6_addr_scope2type(unsigned scope
)
219 case IPV6_ADDR_SCOPE_NODELOCAL
:
220 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL
) |
222 case IPV6_ADDR_SCOPE_LINKLOCAL
:
223 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL
) |
224 IPV6_ADDR_LINKLOCAL
);
225 case IPV6_ADDR_SCOPE_SITELOCAL
:
226 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL
) |
227 IPV6_ADDR_SITELOCAL
);
229 return IPV6_ADDR_SCOPE_TYPE(scope
);
232 int __ipv6_addr_type(const struct in6_addr
*addr
)
236 st
= addr
->s6_addr32
[0];
238 /* Consider all addresses with the first three bits different of
239 000 and 111 as unicasts.
241 if ((st
& htonl(0xE0000000)) != htonl(0x00000000) &&
242 (st
& htonl(0xE0000000)) != htonl(0xE0000000))
243 return (IPV6_ADDR_UNICAST
|
244 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL
));
246 if ((st
& htonl(0xFF000000)) == htonl(0xFF000000)) {
248 /* addr-select 3.1 */
249 return (IPV6_ADDR_MULTICAST
|
250 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr
)));
253 if ((st
& htonl(0xFFC00000)) == htonl(0xFE800000))
254 return (IPV6_ADDR_LINKLOCAL
| IPV6_ADDR_UNICAST
|
255 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL
)); /* addr-select 3.1 */
256 if ((st
& htonl(0xFFC00000)) == htonl(0xFEC00000))
257 return (IPV6_ADDR_SITELOCAL
| IPV6_ADDR_UNICAST
|
258 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL
)); /* addr-select 3.1 */
260 if ((addr
->s6_addr32
[0] | addr
->s6_addr32
[1]) == 0) {
261 if (addr
->s6_addr32
[2] == 0) {
262 if (addr
->s6_addr32
[3] == 0)
263 return IPV6_ADDR_ANY
;
265 if (addr
->s6_addr32
[3] == htonl(0x00000001))
266 return (IPV6_ADDR_LOOPBACK
| IPV6_ADDR_UNICAST
|
267 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL
)); /* addr-select 3.4 */
269 return (IPV6_ADDR_COMPATv4
| IPV6_ADDR_UNICAST
|
270 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL
)); /* addr-select 3.3 */
273 if (addr
->s6_addr32
[2] == htonl(0x0000ffff))
274 return (IPV6_ADDR_MAPPED
|
275 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL
)); /* addr-select 3.3 */
278 return (IPV6_ADDR_RESERVED
|
279 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL
)); /* addr-select 3.4 */
282 static void addrconf_del_timer(struct inet6_ifaddr
*ifp
)
284 if (del_timer(&ifp
->timer
))
288 enum addrconf_timer_t
295 static void addrconf_mod_timer(struct inet6_ifaddr
*ifp
,
296 enum addrconf_timer_t what
,
299 if (!del_timer(&ifp
->timer
))
304 ifp
->timer
.function
= addrconf_dad_timer
;
307 ifp
->timer
.function
= addrconf_rs_timer
;
311 ifp
->timer
.expires
= jiffies
+ when
;
312 add_timer(&ifp
->timer
);
315 /* Nobody refers to this device, we may destroy it. */
317 static void in6_dev_finish_destroy_rcu(struct rcu_head
*head
)
319 struct inet6_dev
*idev
= container_of(head
, struct inet6_dev
, rcu
);
323 void in6_dev_finish_destroy(struct inet6_dev
*idev
)
325 struct net_device
*dev
= idev
->dev
;
326 BUG_TRAP(idev
->addr_list
==NULL
);
327 BUG_TRAP(idev
->mc_list
==NULL
);
328 #ifdef NET_REFCNT_DEBUG
329 printk(KERN_DEBUG
"in6_dev_finish_destroy: %s\n", dev
? dev
->name
: "NIL");
333 printk("Freeing alive inet6 device %p\n", idev
);
336 snmp6_free_dev(idev
);
337 call_rcu(&idev
->rcu
, in6_dev_finish_destroy_rcu
);
340 static struct inet6_dev
* ipv6_add_dev(struct net_device
*dev
)
342 struct inet6_dev
*ndev
;
343 struct in6_addr maddr
;
347 if (dev
->mtu
< IPV6_MIN_MTU
)
350 ndev
= kzalloc(sizeof(struct inet6_dev
), GFP_KERNEL
);
355 rwlock_init(&ndev
->lock
);
357 memcpy(&ndev
->cnf
, &ipv6_devconf_dflt
, sizeof(ndev
->cnf
));
358 ndev
->cnf
.mtu6
= dev
->mtu
;
359 ndev
->cnf
.sysctl
= NULL
;
360 ndev
->nd_parms
= neigh_parms_alloc(dev
, &nd_tbl
);
361 if (ndev
->nd_parms
== NULL
) {
365 /* We refer to the device */
368 if (snmp6_alloc_dev(ndev
) < 0) {
370 "%s(): cannot allocate memory for statistics; dev=%s.\n",
371 __FUNCTION__
, dev
->name
));
372 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
374 in6_dev_finish_destroy(ndev
);
378 if (snmp6_register_dev(ndev
) < 0) {
380 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
381 __FUNCTION__
, dev
->name
));
382 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
384 in6_dev_finish_destroy(ndev
);
388 /* One reference from device. We must do this before
389 * we invoke __ipv6_regen_rndid().
393 #ifdef CONFIG_IPV6_PRIVACY
394 init_timer(&ndev
->regen_timer
);
395 ndev
->regen_timer
.function
= ipv6_regen_rndid
;
396 ndev
->regen_timer
.data
= (unsigned long) ndev
;
397 if ((dev
->flags
&IFF_LOOPBACK
) ||
398 dev
->type
== ARPHRD_TUNNEL
||
399 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
400 dev
->type
== ARPHRD_SIT
||
402 dev
->type
== ARPHRD_NONE
) {
404 "%s: Disabled Privacy Extensions\n",
406 ndev
->cnf
.use_tempaddr
= -1;
409 ipv6_regen_rndid((unsigned long) ndev
);
413 if (netif_carrier_ok(dev
))
414 ndev
->if_flags
|= IF_READY
;
417 ipv6_mc_init_dev(ndev
);
418 ndev
->tstamp
= jiffies
;
420 neigh_sysctl_register(dev
, ndev
->nd_parms
, NET_IPV6
,
421 NET_IPV6_NEIGH
, "ipv6",
422 &ndisc_ifinfo_sysctl_change
,
424 addrconf_sysctl_register(ndev
, &ndev
->cnf
);
426 /* protected by rtnl_lock */
427 rcu_assign_pointer(dev
->ip6_ptr
, ndev
);
429 /* Join all-node multicast group */
430 ipv6_addr_all_nodes(&maddr
);
431 ipv6_dev_mc_inc(dev
, &maddr
);
436 static struct inet6_dev
* ipv6_find_idev(struct net_device
*dev
)
438 struct inet6_dev
*idev
;
442 if ((idev
= __in6_dev_get(dev
)) == NULL
) {
443 if ((idev
= ipv6_add_dev(dev
)) == NULL
)
447 if (dev
->flags
&IFF_UP
)
453 static void dev_forward_change(struct inet6_dev
*idev
)
455 struct net_device
*dev
;
456 struct inet6_ifaddr
*ifa
;
457 struct in6_addr addr
;
462 if (dev
&& (dev
->flags
& IFF_MULTICAST
)) {
463 ipv6_addr_all_routers(&addr
);
465 if (idev
->cnf
.forwarding
)
466 ipv6_dev_mc_inc(dev
, &addr
);
468 ipv6_dev_mc_dec(dev
, &addr
);
470 for (ifa
=idev
->addr_list
; ifa
; ifa
=ifa
->if_next
) {
471 if (idev
->cnf
.forwarding
)
472 addrconf_join_anycast(ifa
);
474 addrconf_leave_anycast(ifa
);
479 static void addrconf_forward_change(void)
481 struct net_device
*dev
;
482 struct inet6_dev
*idev
;
484 read_lock(&dev_base_lock
);
485 for (dev
=dev_base
; dev
; dev
=dev
->next
) {
487 idev
= __in6_dev_get(dev
);
489 int changed
= (!idev
->cnf
.forwarding
) ^ (!ipv6_devconf
.forwarding
);
490 idev
->cnf
.forwarding
= ipv6_devconf
.forwarding
;
492 dev_forward_change(idev
);
496 read_unlock(&dev_base_lock
);
500 /* Nobody refers to this ifaddr, destroy it */
502 void inet6_ifa_finish_destroy(struct inet6_ifaddr
*ifp
)
504 BUG_TRAP(ifp
->if_next
==NULL
);
505 BUG_TRAP(ifp
->lst_next
==NULL
);
506 #ifdef NET_REFCNT_DEBUG
507 printk(KERN_DEBUG
"inet6_ifa_finish_destroy\n");
510 in6_dev_put(ifp
->idev
);
512 if (del_timer(&ifp
->timer
))
513 printk("Timer is still running, when freeing ifa=%p\n", ifp
);
516 printk("Freeing alive inet6 address %p\n", ifp
);
519 dst_release(&ifp
->rt
->u
.dst
);
525 ipv6_link_dev_addr(struct inet6_dev
*idev
, struct inet6_ifaddr
*ifp
)
527 struct inet6_ifaddr
*ifa
, **ifap
;
528 int ifp_scope
= ipv6_addr_src_scope(&ifp
->addr
);
531 * Each device address list is sorted in order of scope -
532 * global before linklocal.
534 for (ifap
= &idev
->addr_list
; (ifa
= *ifap
) != NULL
;
535 ifap
= &ifa
->if_next
) {
536 if (ifp_scope
>= ipv6_addr_src_scope(&ifa
->addr
))
540 ifp
->if_next
= *ifap
;
544 /* On success it returns ifp with increased reference count */
546 static struct inet6_ifaddr
*
547 ipv6_add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
, int pfxlen
,
548 int scope
, u32 flags
)
550 struct inet6_ifaddr
*ifa
= NULL
;
557 err
= -ENODEV
; /*XXX*/
561 write_lock(&addrconf_hash_lock
);
563 /* Ignore adding duplicate addresses on an interface */
564 if (ipv6_chk_same_addr(addr
, idev
->dev
)) {
565 ADBG(("ipv6_add_addr: already assigned\n"));
570 ifa
= kzalloc(sizeof(struct inet6_ifaddr
), GFP_ATOMIC
);
573 ADBG(("ipv6_add_addr: malloc failed\n"));
578 rt
= addrconf_dst_alloc(idev
, addr
, 0);
584 ipv6_addr_copy(&ifa
->addr
, addr
);
586 spin_lock_init(&ifa
->lock
);
587 init_timer(&ifa
->timer
);
588 ifa
->timer
.data
= (unsigned long) ifa
;
590 ifa
->prefix_len
= pfxlen
;
591 ifa
->flags
= flags
| IFA_F_TENTATIVE
;
592 ifa
->cstamp
= ifa
->tstamp
= jiffies
;
601 /* Add to big hash table */
602 hash
= ipv6_addr_hash(addr
);
604 ifa
->lst_next
= inet6_addr_lst
[hash
];
605 inet6_addr_lst
[hash
] = ifa
;
607 write_unlock(&addrconf_hash_lock
);
609 write_lock(&idev
->lock
);
610 /* Add to inet6_dev unicast addr list. */
611 ipv6_link_dev_addr(idev
, ifa
);
613 #ifdef CONFIG_IPV6_PRIVACY
614 if (ifa
->flags
&IFA_F_TEMPORARY
) {
615 ifa
->tmp_next
= idev
->tempaddr_list
;
616 idev
->tempaddr_list
= ifa
;
622 write_unlock(&idev
->lock
);
624 rcu_read_unlock_bh();
626 if (likely(err
== 0))
627 atomic_notifier_call_chain(&inet6addr_chain
, NETDEV_UP
, ifa
);
635 write_unlock(&addrconf_hash_lock
);
639 /* This function wants to get referenced ifp and releases it before return */
641 static void ipv6_del_addr(struct inet6_ifaddr
*ifp
)
643 struct inet6_ifaddr
*ifa
, **ifap
;
644 struct inet6_dev
*idev
= ifp
->idev
;
646 int deleted
= 0, onlink
= 0;
647 unsigned long expires
= jiffies
;
649 hash
= ipv6_addr_hash(&ifp
->addr
);
653 write_lock_bh(&addrconf_hash_lock
);
654 for (ifap
= &inet6_addr_lst
[hash
]; (ifa
=*ifap
) != NULL
;
655 ifap
= &ifa
->lst_next
) {
657 *ifap
= ifa
->lst_next
;
659 ifa
->lst_next
= NULL
;
663 write_unlock_bh(&addrconf_hash_lock
);
665 write_lock_bh(&idev
->lock
);
666 #ifdef CONFIG_IPV6_PRIVACY
667 if (ifp
->flags
&IFA_F_TEMPORARY
) {
668 for (ifap
= &idev
->tempaddr_list
; (ifa
=*ifap
) != NULL
;
669 ifap
= &ifa
->tmp_next
) {
671 *ifap
= ifa
->tmp_next
;
673 in6_ifa_put(ifp
->ifpub
);
677 ifa
->tmp_next
= NULL
;
684 for (ifap
= &idev
->addr_list
; (ifa
=*ifap
) != NULL
;) {
686 *ifap
= ifa
->if_next
;
689 if (!(ifp
->flags
& IFA_F_PERMANENT
) || onlink
> 0)
693 } else if (ifp
->flags
& IFA_F_PERMANENT
) {
694 if (ipv6_prefix_equal(&ifa
->addr
, &ifp
->addr
,
696 if (ifa
->flags
& IFA_F_PERMANENT
) {
701 unsigned long lifetime
;
706 spin_lock(&ifa
->lock
);
707 lifetime
= min_t(unsigned long,
708 ifa
->valid_lft
, 0x7fffffffUL
/HZ
);
709 if (time_before(expires
,
710 ifa
->tstamp
+ lifetime
* HZ
))
711 expires
= ifa
->tstamp
+ lifetime
* HZ
;
712 spin_unlock(&ifa
->lock
);
716 ifap
= &ifa
->if_next
;
718 write_unlock_bh(&idev
->lock
);
720 ipv6_ifa_notify(RTM_DELADDR
, ifp
);
722 atomic_notifier_call_chain(&inet6addr_chain
, NETDEV_DOWN
, ifp
);
724 addrconf_del_timer(ifp
);
727 * Purge or update corresponding prefix
729 * 1) we don't purge prefix here if address was not permanent.
730 * prefix is managed by its own lifetime.
731 * 2) if there're no addresses, delete prefix.
732 * 3) if there're still other permanent address(es),
733 * corresponding prefix is still permanent.
734 * 4) otherwise, update prefix lifetime to the
735 * longest valid lifetime among the corresponding
736 * addresses on the device.
737 * Note: subsequent RA will update lifetime.
741 if ((ifp
->flags
& IFA_F_PERMANENT
) && onlink
< 1) {
742 struct in6_addr prefix
;
745 ipv6_addr_prefix(&prefix
, &ifp
->addr
, ifp
->prefix_len
);
746 rt
= rt6_lookup(&prefix
, NULL
, ifp
->idev
->dev
->ifindex
, 1);
748 if (rt
&& ((rt
->rt6i_flags
& (RTF_GATEWAY
| RTF_DEFAULT
)) == 0)) {
752 } else if (!(rt
->rt6i_flags
& RTF_EXPIRES
)) {
753 rt
->rt6i_expires
= expires
;
754 rt
->rt6i_flags
|= RTF_EXPIRES
;
757 dst_release(&rt
->u
.dst
);
763 #ifdef CONFIG_IPV6_PRIVACY
764 static int ipv6_create_tempaddr(struct inet6_ifaddr
*ifp
, struct inet6_ifaddr
*ift
)
766 struct inet6_dev
*idev
= ifp
->idev
;
767 struct in6_addr addr
, *tmpaddr
;
768 unsigned long tmp_prefered_lft
, tmp_valid_lft
, tmp_cstamp
, tmp_tstamp
;
773 write_lock(&idev
->lock
);
775 spin_lock_bh(&ift
->lock
);
776 memcpy(&addr
.s6_addr
[8], &ift
->addr
.s6_addr
[8], 8);
777 spin_unlock_bh(&ift
->lock
);
784 if (idev
->cnf
.use_tempaddr
<= 0) {
785 write_unlock(&idev
->lock
);
787 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
792 spin_lock_bh(&ifp
->lock
);
793 if (ifp
->regen_count
++ >= idev
->cnf
.regen_max_retry
) {
794 idev
->cnf
.use_tempaddr
= -1; /*XXX*/
795 spin_unlock_bh(&ifp
->lock
);
796 write_unlock(&idev
->lock
);
798 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
804 memcpy(addr
.s6_addr
, ifp
->addr
.s6_addr
, 8);
805 if (__ipv6_try_regen_rndid(idev
, tmpaddr
) < 0) {
806 spin_unlock_bh(&ifp
->lock
);
807 write_unlock(&idev
->lock
);
809 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
815 memcpy(&addr
.s6_addr
[8], idev
->rndid
, 8);
816 tmp_valid_lft
= min_t(__u32
,
818 idev
->cnf
.temp_valid_lft
);
819 tmp_prefered_lft
= min_t(__u32
,
821 idev
->cnf
.temp_prefered_lft
- desync_factor
/ HZ
);
822 tmp_plen
= ifp
->prefix_len
;
823 max_addresses
= idev
->cnf
.max_addresses
;
824 tmp_cstamp
= ifp
->cstamp
;
825 tmp_tstamp
= ifp
->tstamp
;
826 spin_unlock_bh(&ifp
->lock
);
828 write_unlock(&idev
->lock
);
829 ift
= !max_addresses
||
830 ipv6_count_addresses(idev
) < max_addresses
?
831 ipv6_add_addr(idev
, &addr
, tmp_plen
,
832 ipv6_addr_type(&addr
)&IPV6_ADDR_SCOPE_MASK
, IFA_F_TEMPORARY
) : NULL
;
833 if (!ift
|| IS_ERR(ift
)) {
837 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
839 write_lock(&idev
->lock
);
843 spin_lock_bh(&ift
->lock
);
845 ift
->valid_lft
= tmp_valid_lft
;
846 ift
->prefered_lft
= tmp_prefered_lft
;
847 ift
->cstamp
= tmp_cstamp
;
848 ift
->tstamp
= tmp_tstamp
;
849 spin_unlock_bh(&ift
->lock
);
851 addrconf_dad_start(ift
, 0);
860 * Choose an appropriate source address (RFC3484)
862 struct ipv6_saddr_score
{
870 #define IPV6_SADDR_SCORE_LOCAL 0x0001
871 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
872 #define IPV6_SADDR_SCORE_HOA 0x0008
873 #define IPV6_SADDR_SCORE_OIF 0x0010
874 #define IPV6_SADDR_SCORE_LABEL 0x0020
875 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
877 static int inline ipv6_saddr_preferred(int type
)
879 if (type
& (IPV6_ADDR_MAPPED
|IPV6_ADDR_COMPATv4
|
880 IPV6_ADDR_LOOPBACK
|IPV6_ADDR_RESERVED
))
885 /* static matching label */
886 static int inline ipv6_saddr_label(const struct in6_addr
*addr
, int type
)
889 * prefix (longest match) label
890 * -----------------------------
899 if (type
& IPV6_ADDR_LOOPBACK
)
901 else if (type
& IPV6_ADDR_COMPATv4
)
903 else if (type
& IPV6_ADDR_MAPPED
)
905 else if (addr
->s6_addr32
[0] == htonl(0x20010000))
907 else if (addr
->s6_addr16
[0] == htons(0x2002))
909 else if ((addr
->s6_addr
[0] & 0xfe) == 0xfc)
914 int ipv6_dev_get_saddr(struct net_device
*daddr_dev
,
915 struct in6_addr
*daddr
, struct in6_addr
*saddr
)
917 struct ipv6_saddr_score hiscore
;
918 struct inet6_ifaddr
*ifa_result
= NULL
;
919 int daddr_type
= __ipv6_addr_type(daddr
);
920 int daddr_scope
= __ipv6_addr_src_scope(daddr_type
);
921 u32 daddr_label
= ipv6_saddr_label(daddr
, daddr_type
);
922 struct net_device
*dev
;
924 memset(&hiscore
, 0, sizeof(hiscore
));
926 read_lock(&dev_base_lock
);
929 for (dev
= dev_base
; dev
; dev
=dev
->next
) {
930 struct inet6_dev
*idev
;
931 struct inet6_ifaddr
*ifa
;
933 /* Rule 0: Candidate Source Address (section 4)
934 * - multicast and link-local destination address,
935 * the set of candidate source address MUST only
936 * include addresses assigned to interfaces
937 * belonging to the same link as the outgoing
939 * (- For site-local destination addresses, the
940 * set of candidate source addresses MUST only
941 * include addresses assigned to interfaces
942 * belonging to the same site as the outgoing
945 if ((daddr_type
& IPV6_ADDR_MULTICAST
||
946 daddr_scope
<= IPV6_ADDR_SCOPE_LINKLOCAL
) &&
947 daddr_dev
&& dev
!= daddr_dev
)
950 idev
= __in6_dev_get(dev
);
954 read_lock_bh(&idev
->lock
);
955 for (ifa
= idev
->addr_list
; ifa
; ifa
= ifa
->if_next
) {
956 struct ipv6_saddr_score score
;
958 score
.addr_type
= __ipv6_addr_type(&ifa
->addr
);
961 * - Tentative Address (RFC2462 section 5.4)
962 * - A tentative address is not considered
963 * "assigned to an interface" in the traditional
965 * - Candidate Source Address (section 4)
966 * - In any case, anycast addresses, multicast
967 * addresses, and the unspecified address MUST
968 * NOT be included in a candidate set.
970 if (ifa
->flags
& IFA_F_TENTATIVE
)
972 if (unlikely(score
.addr_type
== IPV6_ADDR_ANY
||
973 score
.addr_type
& IPV6_ADDR_MULTICAST
)) {
974 LIMIT_NETDEBUG(KERN_DEBUG
975 "ADDRCONF: unspecified / multicast address"
976 "assigned as unicast address on %s",
986 if (ifa_result
== NULL
) {
987 /* record it if the first available entry */
991 /* Rule 1: Prefer same address */
992 if (hiscore
.rule
< 1) {
993 if (ipv6_addr_equal(&ifa_result
->addr
, daddr
))
994 hiscore
.attrs
|= IPV6_SADDR_SCORE_LOCAL
;
997 if (ipv6_addr_equal(&ifa
->addr
, daddr
)) {
998 score
.attrs
|= IPV6_SADDR_SCORE_LOCAL
;
999 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_LOCAL
)) {
1004 if (hiscore
.attrs
& IPV6_SADDR_SCORE_LOCAL
)
1008 /* Rule 2: Prefer appropriate scope */
1009 if (hiscore
.rule
< 2) {
1010 hiscore
.scope
= __ipv6_addr_src_scope(hiscore
.addr_type
);
1013 score
.scope
= __ipv6_addr_src_scope(score
.addr_type
);
1014 if (hiscore
.scope
< score
.scope
) {
1015 if (hiscore
.scope
< daddr_scope
) {
1020 } else if (score
.scope
< hiscore
.scope
) {
1021 if (score
.scope
< daddr_scope
)
1022 break; /* addresses sorted by scope */
1029 /* Rule 3: Avoid deprecated address */
1030 if (hiscore
.rule
< 3) {
1031 if (ipv6_saddr_preferred(hiscore
.addr_type
) ||
1032 !(ifa_result
->flags
& IFA_F_DEPRECATED
))
1033 hiscore
.attrs
|= IPV6_SADDR_SCORE_PREFERRED
;
1036 if (ipv6_saddr_preferred(score
.addr_type
) ||
1037 !(ifa
->flags
& IFA_F_DEPRECATED
)) {
1038 score
.attrs
|= IPV6_SADDR_SCORE_PREFERRED
;
1039 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_PREFERRED
)) {
1044 if (hiscore
.attrs
& IPV6_SADDR_SCORE_PREFERRED
)
1048 /* Rule 4: Prefer home address */
1049 #ifdef CONFIG_IPV6_MIP6
1050 if (hiscore
.rule
< 4) {
1051 if (ifa_result
->flags
& IFA_F_HOMEADDRESS
)
1052 hiscore
.attrs
|= IPV6_SADDR_SCORE_HOA
;
1055 if (ifa
->flags
& IFA_F_HOMEADDRESS
) {
1056 score
.attrs
|= IPV6_SADDR_SCORE_HOA
;
1057 if (!(ifa_result
->flags
& IFA_F_HOMEADDRESS
)) {
1062 if (hiscore
.attrs
& IPV6_SADDR_SCORE_HOA
)
1066 if (hiscore
.rule
< 4)
1070 /* Rule 5: Prefer outgoing interface */
1071 if (hiscore
.rule
< 5) {
1072 if (daddr_dev
== NULL
||
1073 daddr_dev
== ifa_result
->idev
->dev
)
1074 hiscore
.attrs
|= IPV6_SADDR_SCORE_OIF
;
1077 if (daddr_dev
== NULL
||
1078 daddr_dev
== ifa
->idev
->dev
) {
1079 score
.attrs
|= IPV6_SADDR_SCORE_OIF
;
1080 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_OIF
)) {
1085 if (hiscore
.attrs
& IPV6_SADDR_SCORE_OIF
)
1089 /* Rule 6: Prefer matching label */
1090 if (hiscore
.rule
< 6) {
1091 if (ipv6_saddr_label(&ifa_result
->addr
, hiscore
.addr_type
) == daddr_label
)
1092 hiscore
.attrs
|= IPV6_SADDR_SCORE_LABEL
;
1095 if (ipv6_saddr_label(&ifa
->addr
, score
.addr_type
) == daddr_label
) {
1096 score
.attrs
|= IPV6_SADDR_SCORE_LABEL
;
1097 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_LABEL
)) {
1102 if (hiscore
.attrs
& IPV6_SADDR_SCORE_LABEL
)
1106 #ifdef CONFIG_IPV6_PRIVACY
1107 /* Rule 7: Prefer public address
1108 * Note: prefer temprary address if use_tempaddr >= 2
1110 if (hiscore
.rule
< 7) {
1111 if ((!(ifa_result
->flags
& IFA_F_TEMPORARY
)) ^
1112 (ifa_result
->idev
->cnf
.use_tempaddr
>= 2))
1113 hiscore
.attrs
|= IPV6_SADDR_SCORE_PRIVACY
;
1116 if ((!(ifa
->flags
& IFA_F_TEMPORARY
)) ^
1117 (ifa
->idev
->cnf
.use_tempaddr
>= 2)) {
1118 score
.attrs
|= IPV6_SADDR_SCORE_PRIVACY
;
1119 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_PRIVACY
)) {
1124 if (hiscore
.attrs
& IPV6_SADDR_SCORE_PRIVACY
)
1128 if (hiscore
.rule
< 7)
1131 /* Rule 8: Use longest matching prefix */
1132 if (hiscore
.rule
< 8) {
1133 hiscore
.matchlen
= ipv6_addr_diff(&ifa_result
->addr
, daddr
);
1136 score
.matchlen
= ipv6_addr_diff(&ifa
->addr
, daddr
);
1137 if (score
.matchlen
> hiscore
.matchlen
) {
1142 else if (score
.matchlen
< hiscore
.matchlen
)
1146 /* Final Rule: choose first available one */
1150 in6_ifa_put(ifa_result
);
1155 read_unlock_bh(&idev
->lock
);
1158 read_unlock(&dev_base_lock
);
1161 return -EADDRNOTAVAIL
;
1163 ipv6_addr_copy(saddr
, &ifa_result
->addr
);
1164 in6_ifa_put(ifa_result
);
1169 int ipv6_get_saddr(struct dst_entry
*dst
,
1170 struct in6_addr
*daddr
, struct in6_addr
*saddr
)
1172 return ipv6_dev_get_saddr(dst
? ip6_dst_idev(dst
)->dev
: NULL
, daddr
, saddr
);
1176 int ipv6_get_lladdr(struct net_device
*dev
, struct in6_addr
*addr
)
1178 struct inet6_dev
*idev
;
1179 int err
= -EADDRNOTAVAIL
;
1182 if ((idev
= __in6_dev_get(dev
)) != NULL
) {
1183 struct inet6_ifaddr
*ifp
;
1185 read_lock_bh(&idev
->lock
);
1186 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1187 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1188 ipv6_addr_copy(addr
, &ifp
->addr
);
1193 read_unlock_bh(&idev
->lock
);
1199 static int ipv6_count_addresses(struct inet6_dev
*idev
)
1202 struct inet6_ifaddr
*ifp
;
1204 read_lock_bh(&idev
->lock
);
1205 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
)
1207 read_unlock_bh(&idev
->lock
);
1211 int ipv6_chk_addr(struct in6_addr
*addr
, struct net_device
*dev
, int strict
)
1213 struct inet6_ifaddr
* ifp
;
1214 u8 hash
= ipv6_addr_hash(addr
);
1216 read_lock_bh(&addrconf_hash_lock
);
1217 for(ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
=ifp
->lst_next
) {
1218 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
1219 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1220 if (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1221 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
))
1225 read_unlock_bh(&addrconf_hash_lock
);
1230 int ipv6_chk_same_addr(const struct in6_addr
*addr
, struct net_device
*dev
)
1232 struct inet6_ifaddr
* ifp
;
1233 u8 hash
= ipv6_addr_hash(addr
);
1235 for(ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
=ifp
->lst_next
) {
1236 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1237 if (dev
== NULL
|| ifp
->idev
->dev
== dev
)
1244 struct inet6_ifaddr
* ipv6_get_ifaddr(struct in6_addr
*addr
, struct net_device
*dev
, int strict
)
1246 struct inet6_ifaddr
* ifp
;
1247 u8 hash
= ipv6_addr_hash(addr
);
1249 read_lock_bh(&addrconf_hash_lock
);
1250 for(ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
=ifp
->lst_next
) {
1251 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1252 if (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1253 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
)) {
1259 read_unlock_bh(&addrconf_hash_lock
);
1264 int ipv6_rcv_saddr_equal(const struct sock
*sk
, const struct sock
*sk2
)
1266 const struct in6_addr
*sk_rcv_saddr6
= &inet6_sk(sk
)->rcv_saddr
;
1267 const struct in6_addr
*sk2_rcv_saddr6
= inet6_rcv_saddr(sk2
);
1268 __be32 sk_rcv_saddr
= inet_sk(sk
)->rcv_saddr
;
1269 __be32 sk2_rcv_saddr
= inet_rcv_saddr(sk2
);
1270 int sk_ipv6only
= ipv6_only_sock(sk
);
1271 int sk2_ipv6only
= inet_v6_ipv6only(sk2
);
1272 int addr_type
= ipv6_addr_type(sk_rcv_saddr6
);
1273 int addr_type2
= sk2_rcv_saddr6
? ipv6_addr_type(sk2_rcv_saddr6
) : IPV6_ADDR_MAPPED
;
1275 if (!sk2_rcv_saddr
&& !sk_ipv6only
)
1278 if (addr_type2
== IPV6_ADDR_ANY
&&
1279 !(sk2_ipv6only
&& addr_type
== IPV6_ADDR_MAPPED
))
1282 if (addr_type
== IPV6_ADDR_ANY
&&
1283 !(sk_ipv6only
&& addr_type2
== IPV6_ADDR_MAPPED
))
1286 if (sk2_rcv_saddr6
&&
1287 ipv6_addr_equal(sk_rcv_saddr6
, sk2_rcv_saddr6
))
1290 if (addr_type
== IPV6_ADDR_MAPPED
&&
1292 (!sk2_rcv_saddr
|| !sk_rcv_saddr
|| sk_rcv_saddr
== sk2_rcv_saddr
))
1298 /* Gets referenced address, destroys ifaddr */
1300 static void addrconf_dad_stop(struct inet6_ifaddr
*ifp
)
1302 if (ifp
->flags
&IFA_F_PERMANENT
) {
1303 spin_lock_bh(&ifp
->lock
);
1304 addrconf_del_timer(ifp
);
1305 ifp
->flags
|= IFA_F_TENTATIVE
;
1306 spin_unlock_bh(&ifp
->lock
);
1308 #ifdef CONFIG_IPV6_PRIVACY
1309 } else if (ifp
->flags
&IFA_F_TEMPORARY
) {
1310 struct inet6_ifaddr
*ifpub
;
1311 spin_lock_bh(&ifp
->lock
);
1314 in6_ifa_hold(ifpub
);
1315 spin_unlock_bh(&ifp
->lock
);
1316 ipv6_create_tempaddr(ifpub
, ifp
);
1319 spin_unlock_bh(&ifp
->lock
);
1327 void addrconf_dad_failure(struct inet6_ifaddr
*ifp
)
1329 if (net_ratelimit())
1330 printk(KERN_INFO
"%s: duplicate address detected!\n", ifp
->idev
->dev
->name
);
1331 addrconf_dad_stop(ifp
);
1334 /* Join to solicited addr multicast group. */
1336 void addrconf_join_solict(struct net_device
*dev
, struct in6_addr
*addr
)
1338 struct in6_addr maddr
;
1340 if (dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1343 addrconf_addr_solict_mult(addr
, &maddr
);
1344 ipv6_dev_mc_inc(dev
, &maddr
);
1347 void addrconf_leave_solict(struct inet6_dev
*idev
, struct in6_addr
*addr
)
1349 struct in6_addr maddr
;
1351 if (idev
->dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1354 addrconf_addr_solict_mult(addr
, &maddr
);
1355 __ipv6_dev_mc_dec(idev
, &maddr
);
1358 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
)
1360 struct in6_addr addr
;
1361 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1362 if (ipv6_addr_any(&addr
))
1364 ipv6_dev_ac_inc(ifp
->idev
->dev
, &addr
);
1367 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
)
1369 struct in6_addr addr
;
1370 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1371 if (ipv6_addr_any(&addr
))
1373 __ipv6_dev_ac_dec(ifp
->idev
, &addr
);
1376 static int addrconf_ifid_eui48(u8
*eui
, struct net_device
*dev
)
1378 if (dev
->addr_len
!= ETH_ALEN
)
1380 memcpy(eui
, dev
->dev_addr
, 3);
1381 memcpy(eui
+ 5, dev
->dev_addr
+ 3, 3);
1384 * The zSeries OSA network cards can be shared among various
1385 * OS instances, but the OSA cards have only one MAC address.
1386 * This leads to duplicate address conflicts in conjunction
1387 * with IPv6 if more than one instance uses the same card.
1389 * The driver for these cards can deliver a unique 16-bit
1390 * identifier for each instance sharing the same card. It is
1391 * placed instead of 0xFFFE in the interface identifier. The
1392 * "u" bit of the interface identifier is not inverted in this
1393 * case. Hence the resulting interface identifier has local
1394 * scope according to RFC2373.
1397 eui
[3] = (dev
->dev_id
>> 8) & 0xFF;
1398 eui
[4] = dev
->dev_id
& 0xFF;
1407 static int addrconf_ifid_arcnet(u8
*eui
, struct net_device
*dev
)
1409 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1410 if (dev
->addr_len
!= ARCNET_ALEN
)
1413 eui
[7] = *(u8
*)dev
->dev_addr
;
1417 static int addrconf_ifid_infiniband(u8
*eui
, struct net_device
*dev
)
1419 if (dev
->addr_len
!= INFINIBAND_ALEN
)
1421 memcpy(eui
, dev
->dev_addr
+ 12, 8);
1426 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
)
1428 switch (dev
->type
) {
1431 case ARPHRD_IEEE802_TR
:
1432 return addrconf_ifid_eui48(eui
, dev
);
1434 return addrconf_ifid_arcnet(eui
, dev
);
1435 case ARPHRD_INFINIBAND
:
1436 return addrconf_ifid_infiniband(eui
, dev
);
1441 static int ipv6_inherit_eui64(u8
*eui
, struct inet6_dev
*idev
)
1444 struct inet6_ifaddr
*ifp
;
1446 read_lock_bh(&idev
->lock
);
1447 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1448 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1449 memcpy(eui
, ifp
->addr
.s6_addr
+8, 8);
1454 read_unlock_bh(&idev
->lock
);
1458 #ifdef CONFIG_IPV6_PRIVACY
1459 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1460 static int __ipv6_regen_rndid(struct inet6_dev
*idev
)
1463 get_random_bytes(idev
->rndid
, sizeof(idev
->rndid
));
1464 idev
->rndid
[0] &= ~0x02;
1467 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1468 * check if generated address is not inappropriate
1470 * - Reserved subnet anycast (RFC 2526)
1471 * 11111101 11....11 1xxxxxxx
1472 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1473 * 00-00-5E-FE-xx-xx-xx-xx
1475 * - XXX: already assigned to an address on the device
1477 if (idev
->rndid
[0] == 0xfd &&
1478 (idev
->rndid
[1]&idev
->rndid
[2]&idev
->rndid
[3]&idev
->rndid
[4]&idev
->rndid
[5]&idev
->rndid
[6]) == 0xff &&
1479 (idev
->rndid
[7]&0x80))
1481 if ((idev
->rndid
[0]|idev
->rndid
[1]) == 0) {
1482 if (idev
->rndid
[2] == 0x5e && idev
->rndid
[3] == 0xfe)
1484 if ((idev
->rndid
[2]|idev
->rndid
[3]|idev
->rndid
[4]|idev
->rndid
[5]|idev
->rndid
[6]|idev
->rndid
[7]) == 0x00)
1491 static void ipv6_regen_rndid(unsigned long data
)
1493 struct inet6_dev
*idev
= (struct inet6_dev
*) data
;
1494 unsigned long expires
;
1497 write_lock_bh(&idev
->lock
);
1502 if (__ipv6_regen_rndid(idev
) < 0)
1506 idev
->cnf
.temp_prefered_lft
* HZ
-
1507 idev
->cnf
.regen_max_retry
* idev
->cnf
.dad_transmits
* idev
->nd_parms
->retrans_time
- desync_factor
;
1508 if (time_before(expires
, jiffies
)) {
1510 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1515 if (!mod_timer(&idev
->regen_timer
, expires
))
1519 write_unlock_bh(&idev
->lock
);
1520 rcu_read_unlock_bh();
1524 static int __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
) {
1527 if (tmpaddr
&& memcmp(idev
->rndid
, &tmpaddr
->s6_addr
[8], 8) == 0)
1528 ret
= __ipv6_regen_rndid(idev
);
1538 addrconf_prefix_route(struct in6_addr
*pfx
, int plen
, struct net_device
*dev
,
1539 unsigned long expires
, u32 flags
)
1541 struct fib6_config cfg
= {
1542 .fc_table
= RT6_TABLE_PREFIX
,
1543 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
1544 .fc_ifindex
= dev
->ifindex
,
1545 .fc_expires
= expires
,
1547 .fc_flags
= RTF_UP
| flags
,
1550 ipv6_addr_copy(&cfg
.fc_dst
, pfx
);
1552 /* Prevent useless cloning on PtP SIT.
1553 This thing is done here expecting that the whole
1554 class of non-broadcast devices need not cloning.
1556 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1557 if (dev
->type
== ARPHRD_SIT
&& (dev
->flags
& IFF_POINTOPOINT
))
1558 cfg
.fc_flags
|= RTF_NONEXTHOP
;
1561 ip6_route_add(&cfg
);
1564 /* Create "default" multicast route to the interface */
1566 static void addrconf_add_mroute(struct net_device
*dev
)
1568 struct fib6_config cfg
= {
1569 .fc_table
= RT6_TABLE_LOCAL
,
1570 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
1571 .fc_ifindex
= dev
->ifindex
,
1576 ipv6_addr_set(&cfg
.fc_dst
, htonl(0xFF000000), 0, 0, 0);
1578 ip6_route_add(&cfg
);
1581 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1582 static void sit_route_add(struct net_device
*dev
)
1584 struct fib6_config cfg
= {
1585 .fc_table
= RT6_TABLE_MAIN
,
1586 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
1587 .fc_ifindex
= dev
->ifindex
,
1589 .fc_flags
= RTF_UP
| RTF_NONEXTHOP
,
1592 /* prefix length - 96 bits "::d.d.d.d" */
1593 ip6_route_add(&cfg
);
1597 static void addrconf_add_lroute(struct net_device
*dev
)
1599 struct in6_addr addr
;
1601 ipv6_addr_set(&addr
, htonl(0xFE800000), 0, 0, 0);
1602 addrconf_prefix_route(&addr
, 64, dev
, 0, 0);
1605 static struct inet6_dev
*addrconf_add_dev(struct net_device
*dev
)
1607 struct inet6_dev
*idev
;
1611 if ((idev
= ipv6_find_idev(dev
)) == NULL
)
1614 /* Add default multicast route */
1615 addrconf_add_mroute(dev
);
1617 /* Add link local route */
1618 addrconf_add_lroute(dev
);
1622 void addrconf_prefix_rcv(struct net_device
*dev
, u8
*opt
, int len
)
1624 struct prefix_info
*pinfo
;
1628 unsigned long rt_expires
;
1629 struct inet6_dev
*in6_dev
;
1631 pinfo
= (struct prefix_info
*) opt
;
1633 if (len
< sizeof(struct prefix_info
)) {
1634 ADBG(("addrconf: prefix option too short\n"));
1639 * Validation checks ([ADDRCONF], page 19)
1642 addr_type
= ipv6_addr_type(&pinfo
->prefix
);
1644 if (addr_type
& (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
))
1647 valid_lft
= ntohl(pinfo
->valid
);
1648 prefered_lft
= ntohl(pinfo
->prefered
);
1650 if (prefered_lft
> valid_lft
) {
1651 if (net_ratelimit())
1652 printk(KERN_WARNING
"addrconf: prefix option has invalid lifetime\n");
1656 in6_dev
= in6_dev_get(dev
);
1658 if (in6_dev
== NULL
) {
1659 if (net_ratelimit())
1660 printk(KERN_DEBUG
"addrconf: device %s not configured\n", dev
->name
);
1665 * Two things going on here:
1666 * 1) Add routes for on-link prefixes
1667 * 2) Configure prefixes with the auto flag set
1670 /* Avoid arithmetic overflow. Really, we could
1671 save rt_expires in seconds, likely valid_lft,
1672 but it would require division in fib gc, that it
1675 if (valid_lft
>= 0x7FFFFFFF/HZ
)
1676 rt_expires
= 0x7FFFFFFF - (0x7FFFFFFF % HZ
);
1678 rt_expires
= valid_lft
* HZ
;
1681 * We convert this (in jiffies) to clock_t later.
1682 * Avoid arithmetic overflow there as well.
1683 * Overflow can happen only if HZ < USER_HZ.
1685 if (HZ
< USER_HZ
&& rt_expires
> 0x7FFFFFFF / USER_HZ
)
1686 rt_expires
= 0x7FFFFFFF / USER_HZ
;
1688 if (pinfo
->onlink
) {
1689 struct rt6_info
*rt
;
1690 rt
= rt6_lookup(&pinfo
->prefix
, NULL
, dev
->ifindex
, 1);
1692 if (rt
&& ((rt
->rt6i_flags
& (RTF_GATEWAY
| RTF_DEFAULT
)) == 0)) {
1693 if (rt
->rt6i_flags
&RTF_EXPIRES
) {
1694 if (valid_lft
== 0) {
1698 rt
->rt6i_expires
= jiffies
+ rt_expires
;
1701 } else if (valid_lft
) {
1702 addrconf_prefix_route(&pinfo
->prefix
, pinfo
->prefix_len
,
1703 dev
, jiffies_to_clock_t(rt_expires
), RTF_ADDRCONF
|RTF_EXPIRES
|RTF_PREFIX_RT
);
1706 dst_release(&rt
->u
.dst
);
1709 /* Try to figure out our local address for this prefix */
1711 if (pinfo
->autoconf
&& in6_dev
->cnf
.autoconf
) {
1712 struct inet6_ifaddr
* ifp
;
1713 struct in6_addr addr
;
1714 int create
= 0, update_lft
= 0;
1716 if (pinfo
->prefix_len
== 64) {
1717 memcpy(&addr
, &pinfo
->prefix
, 8);
1718 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) &&
1719 ipv6_inherit_eui64(addr
.s6_addr
+ 8, in6_dev
)) {
1720 in6_dev_put(in6_dev
);
1725 if (net_ratelimit())
1726 printk(KERN_DEBUG
"IPv6 addrconf: prefix with wrong length %d\n",
1728 in6_dev_put(in6_dev
);
1733 ifp
= ipv6_get_ifaddr(&addr
, dev
, 1);
1735 if (ifp
== NULL
&& valid_lft
) {
1736 int max_addresses
= in6_dev
->cnf
.max_addresses
;
1738 /* Do not allow to create too much of autoconfigured
1739 * addresses; this would be too easy way to crash kernel.
1741 if (!max_addresses
||
1742 ipv6_count_addresses(in6_dev
) < max_addresses
)
1743 ifp
= ipv6_add_addr(in6_dev
, &addr
, pinfo
->prefix_len
,
1744 addr_type
&IPV6_ADDR_SCOPE_MASK
, 0);
1746 if (!ifp
|| IS_ERR(ifp
)) {
1747 in6_dev_put(in6_dev
);
1751 update_lft
= create
= 1;
1752 ifp
->cstamp
= jiffies
;
1753 addrconf_dad_start(ifp
, RTF_ADDRCONF
|RTF_PREFIX_RT
);
1759 #ifdef CONFIG_IPV6_PRIVACY
1760 struct inet6_ifaddr
*ift
;
1764 /* update lifetime (RFC2462 5.5.3 e) */
1765 spin_lock(&ifp
->lock
);
1767 if (ifp
->valid_lft
> (now
- ifp
->tstamp
) / HZ
)
1768 stored_lft
= ifp
->valid_lft
- (now
- ifp
->tstamp
) / HZ
;
1771 if (!update_lft
&& stored_lft
) {
1772 if (valid_lft
> MIN_VALID_LIFETIME
||
1773 valid_lft
> stored_lft
)
1775 else if (stored_lft
<= MIN_VALID_LIFETIME
) {
1776 /* valid_lft <= stored_lft is always true */
1780 valid_lft
= MIN_VALID_LIFETIME
;
1781 if (valid_lft
< prefered_lft
)
1782 prefered_lft
= valid_lft
;
1788 ifp
->valid_lft
= valid_lft
;
1789 ifp
->prefered_lft
= prefered_lft
;
1792 ifp
->flags
&= ~IFA_F_DEPRECATED
;
1793 spin_unlock(&ifp
->lock
);
1795 if (!(flags
&IFA_F_TENTATIVE
))
1796 ipv6_ifa_notify(0, ifp
);
1798 spin_unlock(&ifp
->lock
);
1800 #ifdef CONFIG_IPV6_PRIVACY
1801 read_lock_bh(&in6_dev
->lock
);
1802 /* update all temporary addresses in the list */
1803 for (ift
=in6_dev
->tempaddr_list
; ift
; ift
=ift
->tmp_next
) {
1805 * When adjusting the lifetimes of an existing
1806 * temporary address, only lower the lifetimes.
1807 * Implementations must not increase the
1808 * lifetimes of an existing temporary address
1809 * when processing a Prefix Information Option.
1811 spin_lock(&ift
->lock
);
1813 if (ift
->valid_lft
> valid_lft
&&
1814 ift
->valid_lft
- valid_lft
> (jiffies
- ift
->tstamp
) / HZ
)
1815 ift
->valid_lft
= valid_lft
+ (jiffies
- ift
->tstamp
) / HZ
;
1816 if (ift
->prefered_lft
> prefered_lft
&&
1817 ift
->prefered_lft
- prefered_lft
> (jiffies
- ift
->tstamp
) / HZ
)
1818 ift
->prefered_lft
= prefered_lft
+ (jiffies
- ift
->tstamp
) / HZ
;
1819 spin_unlock(&ift
->lock
);
1820 if (!(flags
&IFA_F_TENTATIVE
))
1821 ipv6_ifa_notify(0, ift
);
1824 if (create
&& in6_dev
->cnf
.use_tempaddr
> 0) {
1826 * When a new public address is created as described in [ADDRCONF],
1827 * also create a new temporary address.
1829 read_unlock_bh(&in6_dev
->lock
);
1830 ipv6_create_tempaddr(ifp
, NULL
);
1832 read_unlock_bh(&in6_dev
->lock
);
1839 inet6_prefix_notify(RTM_NEWPREFIX
, in6_dev
, pinfo
);
1840 in6_dev_put(in6_dev
);
1844 * Set destination address.
1845 * Special case for SIT interfaces where we create a new "virtual"
1848 int addrconf_set_dstaddr(void __user
*arg
)
1850 struct in6_ifreq ireq
;
1851 struct net_device
*dev
;
1857 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
1860 dev
= __dev_get_by_index(ireq
.ifr6_ifindex
);
1866 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1867 if (dev
->type
== ARPHRD_SIT
) {
1870 struct ip_tunnel_parm p
;
1872 err
= -EADDRNOTAVAIL
;
1873 if (!(ipv6_addr_type(&ireq
.ifr6_addr
) & IPV6_ADDR_COMPATv4
))
1876 memset(&p
, 0, sizeof(p
));
1877 p
.iph
.daddr
= ireq
.ifr6_addr
.s6_addr32
[3];
1881 p
.iph
.protocol
= IPPROTO_IPV6
;
1883 ifr
.ifr_ifru
.ifru_data
= (void __user
*)&p
;
1885 oldfs
= get_fs(); set_fs(KERNEL_DS
);
1886 err
= dev
->do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
1891 if ((dev
= __dev_get_by_name(p
.name
)) == NULL
)
1893 err
= dev_open(dev
);
1904 * Manual configuration of address on an interface
1906 static int inet6_addr_add(int ifindex
, struct in6_addr
*pfx
, int plen
,
1907 __u8 ifa_flags
, __u32 prefered_lft
, __u32 valid_lft
)
1909 struct inet6_ifaddr
*ifp
;
1910 struct inet6_dev
*idev
;
1911 struct net_device
*dev
;
1916 /* check the lifetime */
1917 if (!valid_lft
|| prefered_lft
> valid_lft
)
1920 if ((dev
= __dev_get_by_index(ifindex
)) == NULL
)
1923 if ((idev
= addrconf_add_dev(dev
)) == NULL
)
1926 scope
= ipv6_addr_scope(pfx
);
1928 if (valid_lft
== INFINITY_LIFE_TIME
)
1929 ifa_flags
|= IFA_F_PERMANENT
;
1930 else if (valid_lft
>= 0x7FFFFFFF/HZ
)
1931 valid_lft
= 0x7FFFFFFF/HZ
;
1933 if (prefered_lft
== 0)
1934 ifa_flags
|= IFA_F_DEPRECATED
;
1935 else if ((prefered_lft
>= 0x7FFFFFFF/HZ
) &&
1936 (prefered_lft
!= INFINITY_LIFE_TIME
))
1937 prefered_lft
= 0x7FFFFFFF/HZ
;
1939 ifp
= ipv6_add_addr(idev
, pfx
, plen
, scope
, ifa_flags
);
1942 spin_lock_bh(&ifp
->lock
);
1943 ifp
->valid_lft
= valid_lft
;
1944 ifp
->prefered_lft
= prefered_lft
;
1945 ifp
->tstamp
= jiffies
;
1946 spin_unlock_bh(&ifp
->lock
);
1948 addrconf_dad_start(ifp
, 0);
1954 return PTR_ERR(ifp
);
1957 static int inet6_addr_del(int ifindex
, struct in6_addr
*pfx
, int plen
)
1959 struct inet6_ifaddr
*ifp
;
1960 struct inet6_dev
*idev
;
1961 struct net_device
*dev
;
1963 if ((dev
= __dev_get_by_index(ifindex
)) == NULL
)
1966 if ((idev
= __in6_dev_get(dev
)) == NULL
)
1969 read_lock_bh(&idev
->lock
);
1970 for (ifp
= idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1971 if (ifp
->prefix_len
== plen
&&
1972 ipv6_addr_equal(pfx
, &ifp
->addr
)) {
1974 read_unlock_bh(&idev
->lock
);
1978 /* If the last address is deleted administratively,
1979 disable IPv6 on this interface.
1981 if (idev
->addr_list
== NULL
)
1982 addrconf_ifdown(idev
->dev
, 1);
1986 read_unlock_bh(&idev
->lock
);
1987 return -EADDRNOTAVAIL
;
1991 int addrconf_add_ifaddr(void __user
*arg
)
1993 struct in6_ifreq ireq
;
1996 if (!capable(CAP_NET_ADMIN
))
1999 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2003 err
= inet6_addr_add(ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, ireq
.ifr6_prefixlen
,
2004 IFA_F_PERMANENT
, INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
2009 int addrconf_del_ifaddr(void __user
*arg
)
2011 struct in6_ifreq ireq
;
2014 if (!capable(CAP_NET_ADMIN
))
2017 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2021 err
= inet6_addr_del(ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, ireq
.ifr6_prefixlen
);
2026 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2027 static void sit_add_v4_addrs(struct inet6_dev
*idev
)
2029 struct inet6_ifaddr
* ifp
;
2030 struct in6_addr addr
;
2031 struct net_device
*dev
;
2036 memset(&addr
, 0, sizeof(struct in6_addr
));
2037 memcpy(&addr
.s6_addr32
[3], idev
->dev
->dev_addr
, 4);
2039 if (idev
->dev
->flags
&IFF_POINTOPOINT
) {
2040 addr
.s6_addr32
[0] = htonl(0xfe800000);
2043 scope
= IPV6_ADDR_COMPATv4
;
2046 if (addr
.s6_addr32
[3]) {
2047 ifp
= ipv6_add_addr(idev
, &addr
, 128, scope
, IFA_F_PERMANENT
);
2049 spin_lock_bh(&ifp
->lock
);
2050 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2051 spin_unlock_bh(&ifp
->lock
);
2052 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2058 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
2059 struct in_device
* in_dev
= __in_dev_get_rtnl(dev
);
2060 if (in_dev
&& (dev
->flags
& IFF_UP
)) {
2061 struct in_ifaddr
* ifa
;
2065 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
2068 addr
.s6_addr32
[3] = ifa
->ifa_local
;
2070 if (ifa
->ifa_scope
== RT_SCOPE_LINK
)
2072 if (ifa
->ifa_scope
>= RT_SCOPE_HOST
) {
2073 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2077 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2082 ifp
= ipv6_add_addr(idev
, &addr
, plen
, flag
,
2085 spin_lock_bh(&ifp
->lock
);
2086 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2087 spin_unlock_bh(&ifp
->lock
);
2088 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2097 static void init_loopback(struct net_device
*dev
)
2099 struct inet6_dev
*idev
;
2100 struct inet6_ifaddr
* ifp
;
2106 if ((idev
= ipv6_find_idev(dev
)) == NULL
) {
2107 printk(KERN_DEBUG
"init loopback: add_dev failed\n");
2111 ifp
= ipv6_add_addr(idev
, &in6addr_loopback
, 128, IFA_HOST
, IFA_F_PERMANENT
);
2113 spin_lock_bh(&ifp
->lock
);
2114 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2115 spin_unlock_bh(&ifp
->lock
);
2116 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2121 static void addrconf_add_linklocal(struct inet6_dev
*idev
, struct in6_addr
*addr
)
2123 struct inet6_ifaddr
* ifp
;
2125 ifp
= ipv6_add_addr(idev
, addr
, 64, IFA_LINK
, IFA_F_PERMANENT
);
2127 addrconf_dad_start(ifp
, 0);
2132 static void addrconf_dev_config(struct net_device
*dev
)
2134 struct in6_addr addr
;
2135 struct inet6_dev
* idev
;
2139 if ((dev
->type
!= ARPHRD_ETHER
) &&
2140 (dev
->type
!= ARPHRD_FDDI
) &&
2141 (dev
->type
!= ARPHRD_IEEE802_TR
) &&
2142 (dev
->type
!= ARPHRD_ARCNET
) &&
2143 (dev
->type
!= ARPHRD_INFINIBAND
)) {
2144 /* Alas, we support only Ethernet autoconfiguration. */
2148 idev
= addrconf_add_dev(dev
);
2152 memset(&addr
, 0, sizeof(struct in6_addr
));
2153 addr
.s6_addr32
[0] = htonl(0xFE800000);
2155 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) == 0)
2156 addrconf_add_linklocal(idev
, &addr
);
2159 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2160 static void addrconf_sit_config(struct net_device
*dev
)
2162 struct inet6_dev
*idev
;
2167 * Configure the tunnel with one of our IPv4
2168 * addresses... we should configure all of
2169 * our v4 addrs in the tunnel
2172 if ((idev
= ipv6_find_idev(dev
)) == NULL
) {
2173 printk(KERN_DEBUG
"init sit: add_dev failed\n");
2177 sit_add_v4_addrs(idev
);
2179 if (dev
->flags
&IFF_POINTOPOINT
) {
2180 addrconf_add_mroute(dev
);
2181 addrconf_add_lroute(dev
);
2188 ipv6_inherit_linklocal(struct inet6_dev
*idev
, struct net_device
*link_dev
)
2190 struct in6_addr lladdr
;
2192 if (!ipv6_get_lladdr(link_dev
, &lladdr
)) {
2193 addrconf_add_linklocal(idev
, &lladdr
);
2199 static void ip6_tnl_add_linklocal(struct inet6_dev
*idev
)
2201 struct net_device
*link_dev
;
2203 /* first try to inherit the link-local address from the link device */
2204 if (idev
->dev
->iflink
&&
2205 (link_dev
= __dev_get_by_index(idev
->dev
->iflink
))) {
2206 if (!ipv6_inherit_linklocal(idev
, link_dev
))
2209 /* then try to inherit it from any device */
2210 for (link_dev
= dev_base
; link_dev
; link_dev
= link_dev
->next
) {
2211 if (!ipv6_inherit_linklocal(idev
, link_dev
))
2214 printk(KERN_DEBUG
"init ip6-ip6: add_linklocal failed\n");
2218 * Autoconfigure tunnel with a link-local address so routing protocols,
2219 * DHCPv6, MLD etc. can be run over the virtual link
2222 static void addrconf_ip6_tnl_config(struct net_device
*dev
)
2224 struct inet6_dev
*idev
;
2228 if ((idev
= addrconf_add_dev(dev
)) == NULL
) {
2229 printk(KERN_DEBUG
"init ip6-ip6: add_dev failed\n");
2232 ip6_tnl_add_linklocal(idev
);
2235 static int addrconf_notify(struct notifier_block
*this, unsigned long event
,
2238 struct net_device
*dev
= (struct net_device
*) data
;
2239 struct inet6_dev
*idev
= __in6_dev_get(dev
);
2240 int run_pending
= 0;
2245 if (event
== NETDEV_UP
) {
2246 if (!netif_carrier_ok(dev
)) {
2247 /* device is not ready yet. */
2249 "ADDRCONF(NETDEV_UP): %s: "
2250 "link is not ready\n",
2256 idev
->if_flags
|= IF_READY
;
2258 if (!netif_carrier_ok(dev
)) {
2259 /* device is still not ready. */
2264 if (idev
->if_flags
& IF_READY
) {
2265 /* device is already configured. */
2268 idev
->if_flags
|= IF_READY
;
2272 "ADDRCONF(NETDEV_CHANGE): %s: "
2273 "link becomes ready\n",
2280 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2282 addrconf_sit_config(dev
);
2285 case ARPHRD_TUNNEL6
:
2286 addrconf_ip6_tnl_config(dev
);
2288 case ARPHRD_LOOPBACK
:
2293 addrconf_dev_config(dev
);
2298 addrconf_dad_run(idev
);
2300 /* If the MTU changed during the interface down, when the
2301 interface up, the changed MTU must be reflected in the
2302 idev as well as routers.
2304 if (idev
->cnf
.mtu6
!= dev
->mtu
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2305 rt6_mtu_change(dev
, dev
->mtu
);
2306 idev
->cnf
.mtu6
= dev
->mtu
;
2308 idev
->tstamp
= jiffies
;
2309 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
2310 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2311 stop IPv6 on this interface.
2313 if (dev
->mtu
< IPV6_MIN_MTU
)
2314 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
2318 case NETDEV_CHANGEMTU
:
2319 if ( idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2320 rt6_mtu_change(dev
, dev
->mtu
);
2321 idev
->cnf
.mtu6
= dev
->mtu
;
2325 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2328 case NETDEV_UNREGISTER
:
2330 * Remove all addresses from this interface.
2332 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
2335 case NETDEV_CHANGENAME
:
2336 #ifdef CONFIG_SYSCTL
2338 addrconf_sysctl_unregister(&idev
->cnf
);
2339 neigh_sysctl_unregister(idev
->nd_parms
);
2340 neigh_sysctl_register(dev
, idev
->nd_parms
,
2341 NET_IPV6
, NET_IPV6_NEIGH
, "ipv6",
2342 &ndisc_ifinfo_sysctl_change
,
2344 addrconf_sysctl_register(idev
, &idev
->cnf
);
2354 * addrconf module should be notified of a device going up
2356 static struct notifier_block ipv6_dev_notf
= {
2357 .notifier_call
= addrconf_notify
,
2361 static int addrconf_ifdown(struct net_device
*dev
, int how
)
2363 struct inet6_dev
*idev
;
2364 struct inet6_ifaddr
*ifa
, **bifa
;
2369 if (dev
== &loopback_dev
&& how
== 1)
2373 neigh_ifdown(&nd_tbl
, dev
);
2375 idev
= __in6_dev_get(dev
);
2379 /* Step 1: remove reference to ipv6 device from parent device.
2385 /* protected by rtnl_lock */
2386 rcu_assign_pointer(dev
->ip6_ptr
, NULL
);
2388 /* Step 1.5: remove snmp6 entry */
2389 snmp6_unregister_dev(idev
);
2393 /* Step 2: clear hash table */
2394 for (i
=0; i
<IN6_ADDR_HSIZE
; i
++) {
2395 bifa
= &inet6_addr_lst
[i
];
2397 write_lock_bh(&addrconf_hash_lock
);
2398 while ((ifa
= *bifa
) != NULL
) {
2399 if (ifa
->idev
== idev
) {
2400 *bifa
= ifa
->lst_next
;
2401 ifa
->lst_next
= NULL
;
2402 addrconf_del_timer(ifa
);
2406 bifa
= &ifa
->lst_next
;
2408 write_unlock_bh(&addrconf_hash_lock
);
2411 write_lock_bh(&idev
->lock
);
2413 /* Step 3: clear flags for stateless addrconf */
2415 idev
->if_flags
&= ~(IF_RS_SENT
|IF_RA_RCVD
|IF_READY
);
2417 /* Step 4: clear address list */
2418 #ifdef CONFIG_IPV6_PRIVACY
2419 if (how
== 1 && del_timer(&idev
->regen_timer
))
2422 /* clear tempaddr list */
2423 while ((ifa
= idev
->tempaddr_list
) != NULL
) {
2424 idev
->tempaddr_list
= ifa
->tmp_next
;
2425 ifa
->tmp_next
= NULL
;
2427 write_unlock_bh(&idev
->lock
);
2428 spin_lock_bh(&ifa
->lock
);
2431 in6_ifa_put(ifa
->ifpub
);
2434 spin_unlock_bh(&ifa
->lock
);
2436 write_lock_bh(&idev
->lock
);
2439 while ((ifa
= idev
->addr_list
) != NULL
) {
2440 idev
->addr_list
= ifa
->if_next
;
2441 ifa
->if_next
= NULL
;
2443 addrconf_del_timer(ifa
);
2444 write_unlock_bh(&idev
->lock
);
2446 __ipv6_ifa_notify(RTM_DELADDR
, ifa
);
2449 write_lock_bh(&idev
->lock
);
2451 write_unlock_bh(&idev
->lock
);
2453 /* Step 5: Discard multicast list */
2456 ipv6_mc_destroy_dev(idev
);
2460 /* Step 5: netlink notification of this interface */
2461 idev
->tstamp
= jiffies
;
2462 inet6_ifinfo_notify(RTM_DELLINK
, idev
);
2464 /* Shot the device (if unregistered) */
2467 #ifdef CONFIG_SYSCTL
2468 addrconf_sysctl_unregister(&idev
->cnf
);
2469 neigh_sysctl_unregister(idev
->nd_parms
);
2471 neigh_parms_release(&nd_tbl
, idev
->nd_parms
);
2472 neigh_ifdown(&nd_tbl
, dev
);
2478 static void addrconf_rs_timer(unsigned long data
)
2480 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*) data
;
2482 if (ifp
->idev
->cnf
.forwarding
)
2485 if (ifp
->idev
->if_flags
& IF_RA_RCVD
) {
2487 * Announcement received after solicitation
2493 spin_lock(&ifp
->lock
);
2494 if (ifp
->probes
++ < ifp
->idev
->cnf
.rtr_solicits
) {
2495 struct in6_addr all_routers
;
2497 /* The wait after the last probe can be shorter */
2498 addrconf_mod_timer(ifp
, AC_RS
,
2499 (ifp
->probes
== ifp
->idev
->cnf
.rtr_solicits
) ?
2500 ifp
->idev
->cnf
.rtr_solicit_delay
:
2501 ifp
->idev
->cnf
.rtr_solicit_interval
);
2502 spin_unlock(&ifp
->lock
);
2504 ipv6_addr_all_routers(&all_routers
);
2506 ndisc_send_rs(ifp
->idev
->dev
, &ifp
->addr
, &all_routers
);
2508 spin_unlock(&ifp
->lock
);
2510 * Note: we do not support deprecated "all on-link"
2511 * assumption any longer.
2513 printk(KERN_DEBUG
"%s: no IPv6 routers present\n",
2514 ifp
->idev
->dev
->name
);
2522 * Duplicate Address Detection
2524 static void addrconf_dad_kick(struct inet6_ifaddr
*ifp
)
2526 unsigned long rand_num
;
2527 struct inet6_dev
*idev
= ifp
->idev
;
2529 rand_num
= net_random() % (idev
->cnf
.rtr_solicit_delay
? : 1);
2530 ifp
->probes
= idev
->cnf
.dad_transmits
;
2531 addrconf_mod_timer(ifp
, AC_DAD
, rand_num
);
2534 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
, u32 flags
)
2536 struct inet6_dev
*idev
= ifp
->idev
;
2537 struct net_device
*dev
= idev
->dev
;
2539 addrconf_join_solict(dev
, &ifp
->addr
);
2541 if (ifp
->prefix_len
!= 128 && (ifp
->flags
&IFA_F_PERMANENT
))
2542 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, dev
, 0,
2545 net_srandom(ifp
->addr
.s6_addr32
[3]);
2547 read_lock_bh(&idev
->lock
);
2550 spin_lock_bh(&ifp
->lock
);
2552 if (dev
->flags
&(IFF_NOARP
|IFF_LOOPBACK
) ||
2553 !(ifp
->flags
&IFA_F_TENTATIVE
) ||
2554 ifp
->flags
& IFA_F_NODAD
) {
2555 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2556 spin_unlock_bh(&ifp
->lock
);
2557 read_unlock_bh(&idev
->lock
);
2559 addrconf_dad_completed(ifp
);
2563 if (!(idev
->if_flags
& IF_READY
)) {
2564 spin_unlock_bh(&ifp
->lock
);
2565 read_unlock_bh(&idev
->lock
);
2567 * If the defice is not ready:
2568 * - keep it tentative if it is a permanent address.
2569 * - otherwise, kill it.
2572 addrconf_dad_stop(ifp
);
2575 addrconf_dad_kick(ifp
);
2576 spin_unlock_bh(&ifp
->lock
);
2578 read_unlock_bh(&idev
->lock
);
2581 static void addrconf_dad_timer(unsigned long data
)
2583 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*) data
;
2584 struct inet6_dev
*idev
= ifp
->idev
;
2585 struct in6_addr unspec
;
2586 struct in6_addr mcaddr
;
2588 read_lock_bh(&idev
->lock
);
2590 read_unlock_bh(&idev
->lock
);
2593 spin_lock_bh(&ifp
->lock
);
2594 if (ifp
->probes
== 0) {
2596 * DAD was successful
2599 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2600 spin_unlock_bh(&ifp
->lock
);
2601 read_unlock_bh(&idev
->lock
);
2603 addrconf_dad_completed(ifp
);
2609 addrconf_mod_timer(ifp
, AC_DAD
, ifp
->idev
->nd_parms
->retrans_time
);
2610 spin_unlock_bh(&ifp
->lock
);
2611 read_unlock_bh(&idev
->lock
);
2613 /* send a neighbour solicitation for our addr */
2614 memset(&unspec
, 0, sizeof(unspec
));
2615 addrconf_addr_solict_mult(&ifp
->addr
, &mcaddr
);
2616 ndisc_send_ns(ifp
->idev
->dev
, NULL
, &ifp
->addr
, &mcaddr
, &unspec
);
2621 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
)
2623 struct net_device
* dev
= ifp
->idev
->dev
;
2626 * Configure the address for reception. Now it is valid.
2629 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2631 /* If added prefix is link local and forwarding is off,
2632 start sending router solicitations.
2635 if (ifp
->idev
->cnf
.forwarding
== 0 &&
2636 ifp
->idev
->cnf
.rtr_solicits
> 0 &&
2637 (dev
->flags
&IFF_LOOPBACK
) == 0 &&
2638 (ipv6_addr_type(&ifp
->addr
) & IPV6_ADDR_LINKLOCAL
)) {
2639 struct in6_addr all_routers
;
2641 ipv6_addr_all_routers(&all_routers
);
2644 * If a host as already performed a random delay
2645 * [...] as part of DAD [...] there is no need
2646 * to delay again before sending the first RS
2648 ndisc_send_rs(ifp
->idev
->dev
, &ifp
->addr
, &all_routers
);
2650 spin_lock_bh(&ifp
->lock
);
2652 ifp
->idev
->if_flags
|= IF_RS_SENT
;
2653 addrconf_mod_timer(ifp
, AC_RS
, ifp
->idev
->cnf
.rtr_solicit_interval
);
2654 spin_unlock_bh(&ifp
->lock
);
2658 static void addrconf_dad_run(struct inet6_dev
*idev
) {
2659 struct inet6_ifaddr
*ifp
;
2661 read_lock_bh(&idev
->lock
);
2662 for (ifp
= idev
->addr_list
; ifp
; ifp
= ifp
->if_next
) {
2663 spin_lock_bh(&ifp
->lock
);
2664 if (!(ifp
->flags
& IFA_F_TENTATIVE
)) {
2665 spin_unlock_bh(&ifp
->lock
);
2668 spin_unlock_bh(&ifp
->lock
);
2669 addrconf_dad_kick(ifp
);
2671 read_unlock_bh(&idev
->lock
);
2674 #ifdef CONFIG_PROC_FS
2675 struct if6_iter_state
{
2679 static struct inet6_ifaddr
*if6_get_first(struct seq_file
*seq
)
2681 struct inet6_ifaddr
*ifa
= NULL
;
2682 struct if6_iter_state
*state
= seq
->private;
2684 for (state
->bucket
= 0; state
->bucket
< IN6_ADDR_HSIZE
; ++state
->bucket
) {
2685 ifa
= inet6_addr_lst
[state
->bucket
];
2692 static struct inet6_ifaddr
*if6_get_next(struct seq_file
*seq
, struct inet6_ifaddr
*ifa
)
2694 struct if6_iter_state
*state
= seq
->private;
2696 ifa
= ifa
->lst_next
;
2698 if (!ifa
&& ++state
->bucket
< IN6_ADDR_HSIZE
) {
2699 ifa
= inet6_addr_lst
[state
->bucket
];
2705 static struct inet6_ifaddr
*if6_get_idx(struct seq_file
*seq
, loff_t pos
)
2707 struct inet6_ifaddr
*ifa
= if6_get_first(seq
);
2710 while(pos
&& (ifa
= if6_get_next(seq
, ifa
)) != NULL
)
2712 return pos
? NULL
: ifa
;
2715 static void *if6_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2717 read_lock_bh(&addrconf_hash_lock
);
2718 return if6_get_idx(seq
, *pos
);
2721 static void *if6_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2723 struct inet6_ifaddr
*ifa
;
2725 ifa
= if6_get_next(seq
, v
);
2730 static void if6_seq_stop(struct seq_file
*seq
, void *v
)
2732 read_unlock_bh(&addrconf_hash_lock
);
2735 static int if6_seq_show(struct seq_file
*seq
, void *v
)
2737 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*)v
;
2739 NIP6_SEQFMT
" %02x %02x %02x %02x %8s\n",
2741 ifp
->idev
->dev
->ifindex
,
2745 ifp
->idev
->dev
->name
);
2749 static struct seq_operations if6_seq_ops
= {
2750 .start
= if6_seq_start
,
2751 .next
= if6_seq_next
,
2752 .show
= if6_seq_show
,
2753 .stop
= if6_seq_stop
,
2756 static int if6_seq_open(struct inode
*inode
, struct file
*file
)
2758 struct seq_file
*seq
;
2760 struct if6_iter_state
*s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
2765 rc
= seq_open(file
, &if6_seq_ops
);
2769 seq
= file
->private_data
;
2778 static const struct file_operations if6_fops
= {
2779 .owner
= THIS_MODULE
,
2780 .open
= if6_seq_open
,
2782 .llseek
= seq_lseek
,
2783 .release
= seq_release_private
,
2786 int __init
if6_proc_init(void)
2788 if (!proc_net_fops_create("if_inet6", S_IRUGO
, &if6_fops
))
2793 void if6_proc_exit(void)
2795 proc_net_remove("if_inet6");
2797 #endif /* CONFIG_PROC_FS */
2799 #ifdef CONFIG_IPV6_MIP6
2800 /* Check if address is a home address configured on any interface. */
2801 int ipv6_chk_home_addr(struct in6_addr
*addr
)
2804 struct inet6_ifaddr
* ifp
;
2805 u8 hash
= ipv6_addr_hash(addr
);
2806 read_lock_bh(&addrconf_hash_lock
);
2807 for (ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
= ifp
->lst_next
) {
2808 if (ipv6_addr_cmp(&ifp
->addr
, addr
) == 0 &&
2809 (ifp
->flags
& IFA_F_HOMEADDRESS
)) {
2814 read_unlock_bh(&addrconf_hash_lock
);
2820 * Periodic address status verification
2823 static void addrconf_verify(unsigned long foo
)
2825 struct inet6_ifaddr
*ifp
;
2826 unsigned long now
, next
;
2829 spin_lock_bh(&addrconf_verify_lock
);
2831 next
= now
+ ADDR_CHECK_FREQUENCY
;
2833 del_timer(&addr_chk_timer
);
2835 for (i
=0; i
< IN6_ADDR_HSIZE
; i
++) {
2838 read_lock(&addrconf_hash_lock
);
2839 for (ifp
=inet6_addr_lst
[i
]; ifp
; ifp
=ifp
->lst_next
) {
2841 #ifdef CONFIG_IPV6_PRIVACY
2842 unsigned long regen_advance
;
2845 if (ifp
->flags
& IFA_F_PERMANENT
)
2848 spin_lock(&ifp
->lock
);
2849 age
= (now
- ifp
->tstamp
) / HZ
;
2851 #ifdef CONFIG_IPV6_PRIVACY
2852 regen_advance
= ifp
->idev
->cnf
.regen_max_retry
*
2853 ifp
->idev
->cnf
.dad_transmits
*
2854 ifp
->idev
->nd_parms
->retrans_time
/ HZ
;
2857 if (ifp
->valid_lft
!= INFINITY_LIFE_TIME
&&
2858 age
>= ifp
->valid_lft
) {
2859 spin_unlock(&ifp
->lock
);
2861 read_unlock(&addrconf_hash_lock
);
2864 } else if (ifp
->prefered_lft
== INFINITY_LIFE_TIME
) {
2865 spin_unlock(&ifp
->lock
);
2867 } else if (age
>= ifp
->prefered_lft
) {
2868 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2871 if (!(ifp
->flags
&IFA_F_DEPRECATED
)) {
2873 ifp
->flags
|= IFA_F_DEPRECATED
;
2876 if (time_before(ifp
->tstamp
+ ifp
->valid_lft
* HZ
, next
))
2877 next
= ifp
->tstamp
+ ifp
->valid_lft
* HZ
;
2879 spin_unlock(&ifp
->lock
);
2883 read_unlock(&addrconf_hash_lock
);
2885 ipv6_ifa_notify(0, ifp
);
2889 #ifdef CONFIG_IPV6_PRIVACY
2890 } else if ((ifp
->flags
&IFA_F_TEMPORARY
) &&
2891 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
2892 if (age
>= ifp
->prefered_lft
- regen_advance
) {
2893 struct inet6_ifaddr
*ifpub
= ifp
->ifpub
;
2894 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
2895 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
2896 if (!ifp
->regen_count
&& ifpub
) {
2899 in6_ifa_hold(ifpub
);
2900 spin_unlock(&ifp
->lock
);
2901 read_unlock(&addrconf_hash_lock
);
2902 spin_lock(&ifpub
->lock
);
2903 ifpub
->regen_count
= 0;
2904 spin_unlock(&ifpub
->lock
);
2905 ipv6_create_tempaddr(ifpub
, ifp
);
2910 } else if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
, next
))
2911 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
;
2912 spin_unlock(&ifp
->lock
);
2915 /* ifp->prefered_lft <= ifp->valid_lft */
2916 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
2917 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
2918 spin_unlock(&ifp
->lock
);
2921 read_unlock(&addrconf_hash_lock
);
2924 addr_chk_timer
.expires
= time_before(next
, jiffies
+ HZ
) ? jiffies
+ HZ
: next
;
2925 add_timer(&addr_chk_timer
);
2926 spin_unlock_bh(&addrconf_verify_lock
);
2929 static struct in6_addr
*extract_addr(struct nlattr
*addr
, struct nlattr
*local
)
2931 struct in6_addr
*pfx
= NULL
;
2934 pfx
= nla_data(addr
);
2937 if (pfx
&& nla_memcmp(local
, pfx
, sizeof(*pfx
)))
2940 pfx
= nla_data(local
);
2946 static struct nla_policy ifa_ipv6_policy
[IFA_MAX
+1] __read_mostly
= {
2947 [IFA_ADDRESS
] = { .len
= sizeof(struct in6_addr
) },
2948 [IFA_LOCAL
] = { .len
= sizeof(struct in6_addr
) },
2949 [IFA_CACHEINFO
] = { .len
= sizeof(struct ifa_cacheinfo
) },
2953 inet6_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
2955 struct ifaddrmsg
*ifm
;
2956 struct nlattr
*tb
[IFA_MAX
+1];
2957 struct in6_addr
*pfx
;
2960 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
2964 ifm
= nlmsg_data(nlh
);
2965 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
]);
2969 return inet6_addr_del(ifm
->ifa_index
, pfx
, ifm
->ifa_prefixlen
);
2972 static int inet6_addr_modify(struct inet6_ifaddr
*ifp
, u8 ifa_flags
,
2973 u32 prefered_lft
, u32 valid_lft
)
2975 if (!valid_lft
|| (prefered_lft
> valid_lft
))
2978 if (valid_lft
== INFINITY_LIFE_TIME
)
2979 ifa_flags
|= IFA_F_PERMANENT
;
2980 else if (valid_lft
>= 0x7FFFFFFF/HZ
)
2981 valid_lft
= 0x7FFFFFFF/HZ
;
2983 if (prefered_lft
== 0)
2984 ifa_flags
|= IFA_F_DEPRECATED
;
2985 else if ((prefered_lft
>= 0x7FFFFFFF/HZ
) &&
2986 (prefered_lft
!= INFINITY_LIFE_TIME
))
2987 prefered_lft
= 0x7FFFFFFF/HZ
;
2989 spin_lock_bh(&ifp
->lock
);
2990 ifp
->flags
= (ifp
->flags
& ~(IFA_F_DEPRECATED
| IFA_F_PERMANENT
| IFA_F_NODAD
| IFA_F_HOMEADDRESS
)) | ifa_flags
;
2991 ifp
->tstamp
= jiffies
;
2992 ifp
->valid_lft
= valid_lft
;
2993 ifp
->prefered_lft
= prefered_lft
;
2995 spin_unlock_bh(&ifp
->lock
);
2996 if (!(ifp
->flags
&IFA_F_TENTATIVE
))
2997 ipv6_ifa_notify(0, ifp
);
3005 inet6_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
3007 struct ifaddrmsg
*ifm
;
3008 struct nlattr
*tb
[IFA_MAX
+1];
3009 struct in6_addr
*pfx
;
3010 struct inet6_ifaddr
*ifa
;
3011 struct net_device
*dev
;
3012 u32 valid_lft
= INFINITY_LIFE_TIME
, preferred_lft
= INFINITY_LIFE_TIME
;
3016 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
3020 ifm
= nlmsg_data(nlh
);
3021 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
]);
3025 if (tb
[IFA_CACHEINFO
]) {
3026 struct ifa_cacheinfo
*ci
;
3028 ci
= nla_data(tb
[IFA_CACHEINFO
]);
3029 valid_lft
= ci
->ifa_valid
;
3030 preferred_lft
= ci
->ifa_prefered
;
3032 preferred_lft
= INFINITY_LIFE_TIME
;
3033 valid_lft
= INFINITY_LIFE_TIME
;
3036 dev
= __dev_get_by_index(ifm
->ifa_index
);
3040 /* We ignore other flags so far. */
3041 ifa_flags
= ifm
->ifa_flags
& (IFA_F_NODAD
| IFA_F_HOMEADDRESS
);
3043 ifa
= ipv6_get_ifaddr(pfx
, dev
, 1);
3046 * It would be best to check for !NLM_F_CREATE here but
3047 * userspace alreay relies on not having to provide this.
3049 return inet6_addr_add(ifm
->ifa_index
, pfx
, ifm
->ifa_prefixlen
,
3050 ifa_flags
, preferred_lft
, valid_lft
);
3053 if (nlh
->nlmsg_flags
& NLM_F_EXCL
||
3054 !(nlh
->nlmsg_flags
& NLM_F_REPLACE
))
3057 err
= inet6_addr_modify(ifa
, ifa_flags
, preferred_lft
, valid_lft
);
3064 static void put_ifaddrmsg(struct nlmsghdr
*nlh
, u8 prefixlen
, u8 flags
,
3065 u8 scope
, int ifindex
)
3067 struct ifaddrmsg
*ifm
;
3069 ifm
= nlmsg_data(nlh
);
3070 ifm
->ifa_family
= AF_INET6
;
3071 ifm
->ifa_prefixlen
= prefixlen
;
3072 ifm
->ifa_flags
= flags
;
3073 ifm
->ifa_scope
= scope
;
3074 ifm
->ifa_index
= ifindex
;
3077 static int put_cacheinfo(struct sk_buff
*skb
, unsigned long cstamp
,
3078 unsigned long tstamp
, u32 preferred
, u32 valid
)
3080 struct ifa_cacheinfo ci
;
3082 ci
.cstamp
= (u32
)(TIME_DELTA(cstamp
, INITIAL_JIFFIES
) / HZ
* 100
3083 + TIME_DELTA(cstamp
, INITIAL_JIFFIES
) % HZ
* 100 / HZ
);
3084 ci
.tstamp
= (u32
)(TIME_DELTA(tstamp
, INITIAL_JIFFIES
) / HZ
* 100
3085 + TIME_DELTA(tstamp
, INITIAL_JIFFIES
) % HZ
* 100 / HZ
);
3086 ci
.ifa_prefered
= preferred
;
3087 ci
.ifa_valid
= valid
;
3089 return nla_put(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
3092 static inline int rt_scope(int ifa_scope
)
3094 if (ifa_scope
& IFA_HOST
)
3095 return RT_SCOPE_HOST
;
3096 else if (ifa_scope
& IFA_LINK
)
3097 return RT_SCOPE_LINK
;
3098 else if (ifa_scope
& IFA_SITE
)
3099 return RT_SCOPE_SITE
;
3101 return RT_SCOPE_UNIVERSE
;
3104 static inline int inet6_ifaddr_msgsize(void)
3106 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
3107 + nla_total_size(16) /* IFA_ADDRESS */
3108 + nla_total_size(sizeof(struct ifa_cacheinfo
));
3111 static int inet6_fill_ifaddr(struct sk_buff
*skb
, struct inet6_ifaddr
*ifa
,
3112 u32 pid
, u32 seq
, int event
, unsigned int flags
)
3114 struct nlmsghdr
*nlh
;
3115 u32 preferred
, valid
;
3117 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
3121 put_ifaddrmsg(nlh
, ifa
->prefix_len
, ifa
->flags
, rt_scope(ifa
->scope
),
3122 ifa
->idev
->dev
->ifindex
);
3124 if (!(ifa
->flags
&IFA_F_PERMANENT
)) {
3125 preferred
= ifa
->prefered_lft
;
3126 valid
= ifa
->valid_lft
;
3127 if (preferred
!= INFINITY_LIFE_TIME
) {
3128 long tval
= (jiffies
- ifa
->tstamp
)/HZ
;
3130 if (valid
!= INFINITY_LIFE_TIME
)
3134 preferred
= INFINITY_LIFE_TIME
;
3135 valid
= INFINITY_LIFE_TIME
;
3138 if (nla_put(skb
, IFA_ADDRESS
, 16, &ifa
->addr
) < 0 ||
3139 put_cacheinfo(skb
, ifa
->cstamp
, ifa
->tstamp
, preferred
, valid
) < 0) {
3140 nlmsg_cancel(skb
, nlh
);
3144 return nlmsg_end(skb
, nlh
);
3147 static int inet6_fill_ifmcaddr(struct sk_buff
*skb
, struct ifmcaddr6
*ifmca
,
3148 u32 pid
, u32 seq
, int event
, u16 flags
)
3150 struct nlmsghdr
*nlh
;
3151 u8 scope
= RT_SCOPE_UNIVERSE
;
3152 int ifindex
= ifmca
->idev
->dev
->ifindex
;
3154 if (ipv6_addr_scope(&ifmca
->mca_addr
) & IFA_SITE
)
3155 scope
= RT_SCOPE_SITE
;
3157 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
3161 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
3162 if (nla_put(skb
, IFA_MULTICAST
, 16, &ifmca
->mca_addr
) < 0 ||
3163 put_cacheinfo(skb
, ifmca
->mca_cstamp
, ifmca
->mca_tstamp
,
3164 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
3165 nlmsg_cancel(skb
, nlh
);
3169 return nlmsg_end(skb
, nlh
);
3172 static int inet6_fill_ifacaddr(struct sk_buff
*skb
, struct ifacaddr6
*ifaca
,
3173 u32 pid
, u32 seq
, int event
, unsigned int flags
)
3175 struct nlmsghdr
*nlh
;
3176 u8 scope
= RT_SCOPE_UNIVERSE
;
3177 int ifindex
= ifaca
->aca_idev
->dev
->ifindex
;
3179 if (ipv6_addr_scope(&ifaca
->aca_addr
) & IFA_SITE
)
3180 scope
= RT_SCOPE_SITE
;
3182 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
3186 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
3187 if (nla_put(skb
, IFA_ANYCAST
, 16, &ifaca
->aca_addr
) < 0 ||
3188 put_cacheinfo(skb
, ifaca
->aca_cstamp
, ifaca
->aca_tstamp
,
3189 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
3190 nlmsg_cancel(skb
, nlh
);
3194 return nlmsg_end(skb
, nlh
);
3204 static int inet6_dump_addr(struct sk_buff
*skb
, struct netlink_callback
*cb
,
3205 enum addr_type_t type
)
3208 int s_idx
, s_ip_idx
;
3210 struct net_device
*dev
;
3211 struct inet6_dev
*idev
= NULL
;
3212 struct inet6_ifaddr
*ifa
;
3213 struct ifmcaddr6
*ifmca
;
3214 struct ifacaddr6
*ifaca
;
3216 s_idx
= cb
->args
[0];
3217 s_ip_idx
= ip_idx
= cb
->args
[1];
3218 read_lock(&dev_base_lock
);
3220 for (dev
= dev_base
, idx
= 0; dev
; dev
= dev
->next
, idx
++) {
3226 if ((idev
= in6_dev_get(dev
)) == NULL
)
3228 read_lock_bh(&idev
->lock
);
3231 /* unicast address incl. temp addr */
3232 for (ifa
= idev
->addr_list
; ifa
;
3233 ifa
= ifa
->if_next
, ip_idx
++) {
3234 if (ip_idx
< s_ip_idx
)
3236 if ((err
= inet6_fill_ifaddr(skb
, ifa
,
3237 NETLINK_CB(cb
->skb
).pid
,
3238 cb
->nlh
->nlmsg_seq
, RTM_NEWADDR
,
3243 case MULTICAST_ADDR
:
3244 /* multicast address */
3245 for (ifmca
= idev
->mc_list
; ifmca
;
3246 ifmca
= ifmca
->next
, ip_idx
++) {
3247 if (ip_idx
< s_ip_idx
)
3249 if ((err
= inet6_fill_ifmcaddr(skb
, ifmca
,
3250 NETLINK_CB(cb
->skb
).pid
,
3251 cb
->nlh
->nlmsg_seq
, RTM_GETMULTICAST
,
3257 /* anycast address */
3258 for (ifaca
= idev
->ac_list
; ifaca
;
3259 ifaca
= ifaca
->aca_next
, ip_idx
++) {
3260 if (ip_idx
< s_ip_idx
)
3262 if ((err
= inet6_fill_ifacaddr(skb
, ifaca
,
3263 NETLINK_CB(cb
->skb
).pid
,
3264 cb
->nlh
->nlmsg_seq
, RTM_GETANYCAST
,
3272 read_unlock_bh(&idev
->lock
);
3277 read_unlock_bh(&idev
->lock
);
3280 read_unlock(&dev_base_lock
);
3282 cb
->args
[1] = ip_idx
;
3286 static int inet6_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3288 enum addr_type_t type
= UNICAST_ADDR
;
3289 return inet6_dump_addr(skb
, cb
, type
);
3292 static int inet6_dump_ifmcaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3294 enum addr_type_t type
= MULTICAST_ADDR
;
3295 return inet6_dump_addr(skb
, cb
, type
);
3299 static int inet6_dump_ifacaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3301 enum addr_type_t type
= ANYCAST_ADDR
;
3302 return inet6_dump_addr(skb
, cb
, type
);
3305 static int inet6_rtm_getaddr(struct sk_buff
*in_skb
, struct nlmsghdr
* nlh
,
3308 struct ifaddrmsg
*ifm
;
3309 struct nlattr
*tb
[IFA_MAX
+1];
3310 struct in6_addr
*addr
= NULL
;
3311 struct net_device
*dev
= NULL
;
3312 struct inet6_ifaddr
*ifa
;
3313 struct sk_buff
*skb
;
3316 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
3320 addr
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
]);
3326 ifm
= nlmsg_data(nlh
);
3328 dev
= __dev_get_by_index(ifm
->ifa_index
);
3330 if ((ifa
= ipv6_get_ifaddr(addr
, dev
, 1)) == NULL
) {
3331 err
= -EADDRNOTAVAIL
;
3335 if ((skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL
)) == NULL
) {
3340 err
= inet6_fill_ifaddr(skb
, ifa
, NETLINK_CB(in_skb
).pid
,
3341 nlh
->nlmsg_seq
, RTM_NEWADDR
, 0);
3343 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3344 WARN_ON(err
== -EMSGSIZE
);
3348 err
= rtnl_unicast(skb
, NETLINK_CB(in_skb
).pid
);
3355 static void inet6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
)
3357 struct sk_buff
*skb
;
3360 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC
);
3364 err
= inet6_fill_ifaddr(skb
, ifa
, 0, 0, event
, 0);
3366 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3367 WARN_ON(err
== -EMSGSIZE
);
3371 err
= rtnl_notify(skb
, 0, RTNLGRP_IPV6_IFADDR
, NULL
, GFP_ATOMIC
);
3374 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR
, err
);
3377 static void inline ipv6_store_devconf(struct ipv6_devconf
*cnf
,
3378 __s32
*array
, int bytes
)
3380 BUG_ON(bytes
< (DEVCONF_MAX
* 4));
3382 memset(array
, 0, bytes
);
3383 array
[DEVCONF_FORWARDING
] = cnf
->forwarding
;
3384 array
[DEVCONF_HOPLIMIT
] = cnf
->hop_limit
;
3385 array
[DEVCONF_MTU6
] = cnf
->mtu6
;
3386 array
[DEVCONF_ACCEPT_RA
] = cnf
->accept_ra
;
3387 array
[DEVCONF_ACCEPT_REDIRECTS
] = cnf
->accept_redirects
;
3388 array
[DEVCONF_AUTOCONF
] = cnf
->autoconf
;
3389 array
[DEVCONF_DAD_TRANSMITS
] = cnf
->dad_transmits
;
3390 array
[DEVCONF_RTR_SOLICITS
] = cnf
->rtr_solicits
;
3391 array
[DEVCONF_RTR_SOLICIT_INTERVAL
] = cnf
->rtr_solicit_interval
;
3392 array
[DEVCONF_RTR_SOLICIT_DELAY
] = cnf
->rtr_solicit_delay
;
3393 array
[DEVCONF_FORCE_MLD_VERSION
] = cnf
->force_mld_version
;
3394 #ifdef CONFIG_IPV6_PRIVACY
3395 array
[DEVCONF_USE_TEMPADDR
] = cnf
->use_tempaddr
;
3396 array
[DEVCONF_TEMP_VALID_LFT
] = cnf
->temp_valid_lft
;
3397 array
[DEVCONF_TEMP_PREFERED_LFT
] = cnf
->temp_prefered_lft
;
3398 array
[DEVCONF_REGEN_MAX_RETRY
] = cnf
->regen_max_retry
;
3399 array
[DEVCONF_MAX_DESYNC_FACTOR
] = cnf
->max_desync_factor
;
3401 array
[DEVCONF_MAX_ADDRESSES
] = cnf
->max_addresses
;
3402 array
[DEVCONF_ACCEPT_RA_DEFRTR
] = cnf
->accept_ra_defrtr
;
3403 array
[DEVCONF_ACCEPT_RA_PINFO
] = cnf
->accept_ra_pinfo
;
3404 #ifdef CONFIG_IPV6_ROUTER_PREF
3405 array
[DEVCONF_ACCEPT_RA_RTR_PREF
] = cnf
->accept_ra_rtr_pref
;
3406 array
[DEVCONF_RTR_PROBE_INTERVAL
] = cnf
->rtr_probe_interval
;
3407 #ifdef CONFIG_IPV6_ROUTE_INFO
3408 array
[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN
] = cnf
->accept_ra_rt_info_max_plen
;
3411 array
[DEVCONF_PROXY_NDP
] = cnf
->proxy_ndp
;
3414 static inline size_t inet6_if_nlmsg_size(void)
3416 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
3417 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
3418 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
3419 + nla_total_size(4) /* IFLA_MTU */
3420 + nla_total_size(4) /* IFLA_LINK */
3421 + nla_total_size( /* IFLA_PROTINFO */
3422 nla_total_size(4) /* IFLA_INET6_FLAGS */
3423 + nla_total_size(sizeof(struct ifla_cacheinfo
))
3424 + nla_total_size(DEVCONF_MAX
* 4) /* IFLA_INET6_CONF */
3428 static int inet6_fill_ifinfo(struct sk_buff
*skb
, struct inet6_dev
*idev
,
3429 u32 pid
, u32 seq
, int event
, unsigned int flags
)
3431 struct net_device
*dev
= idev
->dev
;
3432 struct nlattr
*conf
;
3433 struct ifinfomsg
*hdr
;
3434 struct nlmsghdr
*nlh
;
3436 struct ifla_cacheinfo ci
;
3438 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*hdr
), flags
);
3442 hdr
= nlmsg_data(nlh
);
3443 hdr
->ifi_family
= AF_INET6
;
3445 hdr
->ifi_type
= dev
->type
;
3446 hdr
->ifi_index
= dev
->ifindex
;
3447 hdr
->ifi_flags
= dev_get_flags(dev
);
3448 hdr
->ifi_change
= 0;
3450 NLA_PUT_STRING(skb
, IFLA_IFNAME
, dev
->name
);
3453 NLA_PUT(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
);
3455 NLA_PUT_U32(skb
, IFLA_MTU
, dev
->mtu
);
3456 if (dev
->ifindex
!= dev
->iflink
)
3457 NLA_PUT_U32(skb
, IFLA_LINK
, dev
->iflink
);
3459 protoinfo
= nla_nest_start(skb
, IFLA_PROTINFO
);
3460 if (protoinfo
== NULL
)
3461 goto nla_put_failure
;
3463 NLA_PUT_U32(skb
, IFLA_INET6_FLAGS
, idev
->if_flags
);
3465 ci
.max_reasm_len
= IPV6_MAXPLEN
;
3466 ci
.tstamp
= (__u32
)(TIME_DELTA(idev
->tstamp
, INITIAL_JIFFIES
) / HZ
* 100
3467 + TIME_DELTA(idev
->tstamp
, INITIAL_JIFFIES
) % HZ
* 100 / HZ
);
3468 ci
.reachable_time
= idev
->nd_parms
->reachable_time
;
3469 ci
.retrans_time
= idev
->nd_parms
->retrans_time
;
3470 NLA_PUT(skb
, IFLA_INET6_CACHEINFO
, sizeof(ci
), &ci
);
3472 conf
= nla_reserve(skb
, IFLA_INET6_CONF
, DEVCONF_MAX
* sizeof(s32
));
3474 goto nla_put_failure
;
3475 ipv6_store_devconf(&idev
->cnf
, nla_data(conf
), nla_len(conf
));
3477 /* XXX - Statistics/MC not implemented */
3479 nla_nest_end(skb
, protoinfo
);
3480 return nlmsg_end(skb
, nlh
);
3483 nlmsg_cancel(skb
, nlh
);
3487 static int inet6_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3490 int s_idx
= cb
->args
[0];
3491 struct net_device
*dev
;
3492 struct inet6_dev
*idev
;
3494 read_lock(&dev_base_lock
);
3495 for (dev
=dev_base
, idx
=0; dev
; dev
= dev
->next
, idx
++) {
3498 if ((idev
= in6_dev_get(dev
)) == NULL
)
3500 err
= inet6_fill_ifinfo(skb
, idev
, NETLINK_CB(cb
->skb
).pid
,
3501 cb
->nlh
->nlmsg_seq
, RTM_NEWLINK
, NLM_F_MULTI
);
3506 read_unlock(&dev_base_lock
);
3512 void inet6_ifinfo_notify(int event
, struct inet6_dev
*idev
)
3514 struct sk_buff
*skb
;
3517 skb
= nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC
);
3521 err
= inet6_fill_ifinfo(skb
, idev
, 0, 0, event
, 0);
3523 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3524 WARN_ON(err
== -EMSGSIZE
);
3528 err
= rtnl_notify(skb
, 0, RTNLGRP_IPV6_IFADDR
, NULL
, GFP_ATOMIC
);
3531 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR
, err
);
3534 static inline size_t inet6_prefix_nlmsg_size(void)
3536 return NLMSG_ALIGN(sizeof(struct prefixmsg
))
3537 + nla_total_size(sizeof(struct in6_addr
))
3538 + nla_total_size(sizeof(struct prefix_cacheinfo
));
3541 static int inet6_fill_prefix(struct sk_buff
*skb
, struct inet6_dev
*idev
,
3542 struct prefix_info
*pinfo
, u32 pid
, u32 seq
,
3543 int event
, unsigned int flags
)
3545 struct prefixmsg
*pmsg
;
3546 struct nlmsghdr
*nlh
;
3547 struct prefix_cacheinfo ci
;
3549 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*pmsg
), flags
);
3553 pmsg
= nlmsg_data(nlh
);
3554 pmsg
->prefix_family
= AF_INET6
;
3555 pmsg
->prefix_pad1
= 0;
3556 pmsg
->prefix_pad2
= 0;
3557 pmsg
->prefix_ifindex
= idev
->dev
->ifindex
;
3558 pmsg
->prefix_len
= pinfo
->prefix_len
;
3559 pmsg
->prefix_type
= pinfo
->type
;
3560 pmsg
->prefix_pad3
= 0;
3561 pmsg
->prefix_flags
= 0;
3563 pmsg
->prefix_flags
|= IF_PREFIX_ONLINK
;
3564 if (pinfo
->autoconf
)
3565 pmsg
->prefix_flags
|= IF_PREFIX_AUTOCONF
;
3567 NLA_PUT(skb
, PREFIX_ADDRESS
, sizeof(pinfo
->prefix
), &pinfo
->prefix
);
3569 ci
.preferred_time
= ntohl(pinfo
->prefered
);
3570 ci
.valid_time
= ntohl(pinfo
->valid
);
3571 NLA_PUT(skb
, PREFIX_CACHEINFO
, sizeof(ci
), &ci
);
3573 return nlmsg_end(skb
, nlh
);
3576 nlmsg_cancel(skb
, nlh
);
3580 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
3581 struct prefix_info
*pinfo
)
3583 struct sk_buff
*skb
;
3586 skb
= nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC
);
3590 err
= inet6_fill_prefix(skb
, idev
, pinfo
, 0, 0, event
, 0);
3592 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3593 WARN_ON(err
== -EMSGSIZE
);
3597 err
= rtnl_notify(skb
, 0, RTNLGRP_IPV6_PREFIX
, NULL
, GFP_ATOMIC
);
3600 rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX
, err
);
3603 static struct rtnetlink_link inet6_rtnetlink_table
[RTM_NR_MSGTYPES
] = {
3604 [RTM_GETLINK
- RTM_BASE
] = { .dumpit
= inet6_dump_ifinfo
, },
3605 [RTM_NEWADDR
- RTM_BASE
] = { .doit
= inet6_rtm_newaddr
, },
3606 [RTM_DELADDR
- RTM_BASE
] = { .doit
= inet6_rtm_deladdr
, },
3607 [RTM_GETADDR
- RTM_BASE
] = { .doit
= inet6_rtm_getaddr
,
3608 .dumpit
= inet6_dump_ifaddr
, },
3609 [RTM_GETMULTICAST
- RTM_BASE
] = { .dumpit
= inet6_dump_ifmcaddr
, },
3610 [RTM_GETANYCAST
- RTM_BASE
] = { .dumpit
= inet6_dump_ifacaddr
, },
3611 [RTM_NEWROUTE
- RTM_BASE
] = { .doit
= inet6_rtm_newroute
, },
3612 [RTM_DELROUTE
- RTM_BASE
] = { .doit
= inet6_rtm_delroute
, },
3613 [RTM_GETROUTE
- RTM_BASE
] = { .doit
= inet6_rtm_getroute
,
3614 .dumpit
= inet6_dump_fib
, },
3615 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3616 [RTM_GETRULE
- RTM_BASE
] = { .dumpit
= fib6_rules_dump
, },
3620 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
3622 inet6_ifa_notify(event
? : RTM_NEWADDR
, ifp
);
3626 ip6_ins_rt(ifp
->rt
);
3627 if (ifp
->idev
->cnf
.forwarding
)
3628 addrconf_join_anycast(ifp
);
3631 if (ifp
->idev
->cnf
.forwarding
)
3632 addrconf_leave_anycast(ifp
);
3633 addrconf_leave_solict(ifp
->idev
, &ifp
->addr
);
3634 dst_hold(&ifp
->rt
->u
.dst
);
3635 if (ip6_del_rt(ifp
->rt
))
3636 dst_free(&ifp
->rt
->u
.dst
);
3641 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
3644 if (likely(ifp
->idev
->dead
== 0))
3645 __ipv6_ifa_notify(event
, ifp
);
3646 rcu_read_unlock_bh();
3649 #ifdef CONFIG_SYSCTL
3652 int addrconf_sysctl_forward(ctl_table
*ctl
, int write
, struct file
* filp
,
3653 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
3655 int *valp
= ctl
->data
;
3659 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
3661 if (write
&& valp
!= &ipv6_devconf_dflt
.forwarding
) {
3662 if (valp
!= &ipv6_devconf
.forwarding
) {
3663 if ((!*valp
) ^ (!val
)) {
3664 struct inet6_dev
*idev
= (struct inet6_dev
*)ctl
->extra1
;
3667 dev_forward_change(idev
);
3670 ipv6_devconf_dflt
.forwarding
= ipv6_devconf
.forwarding
;
3671 addrconf_forward_change();
3674 rt6_purge_dflt_routers();
3680 static int addrconf_sysctl_forward_strategy(ctl_table
*table
,
3681 int __user
*name
, int nlen
,
3682 void __user
*oldval
,
3683 size_t __user
*oldlenp
,
3684 void __user
*newval
, size_t newlen
)
3686 int *valp
= table
->data
;
3689 if (!newval
|| !newlen
)
3691 if (newlen
!= sizeof(int))
3693 if (get_user(new, (int __user
*)newval
))
3697 if (oldval
&& oldlenp
) {
3699 if (get_user(len
, oldlenp
))
3702 if (len
> table
->maxlen
)
3703 len
= table
->maxlen
;
3704 if (copy_to_user(oldval
, valp
, len
))
3706 if (put_user(len
, oldlenp
))
3711 if (valp
!= &ipv6_devconf_dflt
.forwarding
) {
3712 if (valp
!= &ipv6_devconf
.forwarding
) {
3713 struct inet6_dev
*idev
= (struct inet6_dev
*)table
->extra1
;
3715 if (unlikely(idev
== NULL
))
3717 changed
= (!*valp
) ^ (!new);
3720 dev_forward_change(idev
);
3723 addrconf_forward_change();
3727 rt6_purge_dflt_routers();
3734 static struct addrconf_sysctl_table
3736 struct ctl_table_header
*sysctl_header
;
3737 ctl_table addrconf_vars
[__NET_IPV6_MAX
];
3738 ctl_table addrconf_dev
[2];
3739 ctl_table addrconf_conf_dir
[2];
3740 ctl_table addrconf_proto_dir
[2];
3741 ctl_table addrconf_root_dir
[2];
3742 } addrconf_sysctl __read_mostly
= {
3743 .sysctl_header
= NULL
,
3746 .ctl_name
= NET_IPV6_FORWARDING
,
3747 .procname
= "forwarding",
3748 .data
= &ipv6_devconf
.forwarding
,
3749 .maxlen
= sizeof(int),
3751 .proc_handler
= &addrconf_sysctl_forward
,
3752 .strategy
= &addrconf_sysctl_forward_strategy
,
3755 .ctl_name
= NET_IPV6_HOP_LIMIT
,
3756 .procname
= "hop_limit",
3757 .data
= &ipv6_devconf
.hop_limit
,
3758 .maxlen
= sizeof(int),
3760 .proc_handler
= proc_dointvec
,
3763 .ctl_name
= NET_IPV6_MTU
,
3765 .data
= &ipv6_devconf
.mtu6
,
3766 .maxlen
= sizeof(int),
3768 .proc_handler
= &proc_dointvec
,
3771 .ctl_name
= NET_IPV6_ACCEPT_RA
,
3772 .procname
= "accept_ra",
3773 .data
= &ipv6_devconf
.accept_ra
,
3774 .maxlen
= sizeof(int),
3776 .proc_handler
= &proc_dointvec
,
3779 .ctl_name
= NET_IPV6_ACCEPT_REDIRECTS
,
3780 .procname
= "accept_redirects",
3781 .data
= &ipv6_devconf
.accept_redirects
,
3782 .maxlen
= sizeof(int),
3784 .proc_handler
= &proc_dointvec
,
3787 .ctl_name
= NET_IPV6_AUTOCONF
,
3788 .procname
= "autoconf",
3789 .data
= &ipv6_devconf
.autoconf
,
3790 .maxlen
= sizeof(int),
3792 .proc_handler
= &proc_dointvec
,
3795 .ctl_name
= NET_IPV6_DAD_TRANSMITS
,
3796 .procname
= "dad_transmits",
3797 .data
= &ipv6_devconf
.dad_transmits
,
3798 .maxlen
= sizeof(int),
3800 .proc_handler
= &proc_dointvec
,
3803 .ctl_name
= NET_IPV6_RTR_SOLICITS
,
3804 .procname
= "router_solicitations",
3805 .data
= &ipv6_devconf
.rtr_solicits
,
3806 .maxlen
= sizeof(int),
3808 .proc_handler
= &proc_dointvec
,
3811 .ctl_name
= NET_IPV6_RTR_SOLICIT_INTERVAL
,
3812 .procname
= "router_solicitation_interval",
3813 .data
= &ipv6_devconf
.rtr_solicit_interval
,
3814 .maxlen
= sizeof(int),
3816 .proc_handler
= &proc_dointvec_jiffies
,
3817 .strategy
= &sysctl_jiffies
,
3820 .ctl_name
= NET_IPV6_RTR_SOLICIT_DELAY
,
3821 .procname
= "router_solicitation_delay",
3822 .data
= &ipv6_devconf
.rtr_solicit_delay
,
3823 .maxlen
= sizeof(int),
3825 .proc_handler
= &proc_dointvec_jiffies
,
3826 .strategy
= &sysctl_jiffies
,
3829 .ctl_name
= NET_IPV6_FORCE_MLD_VERSION
,
3830 .procname
= "force_mld_version",
3831 .data
= &ipv6_devconf
.force_mld_version
,
3832 .maxlen
= sizeof(int),
3834 .proc_handler
= &proc_dointvec
,
3836 #ifdef CONFIG_IPV6_PRIVACY
3838 .ctl_name
= NET_IPV6_USE_TEMPADDR
,
3839 .procname
= "use_tempaddr",
3840 .data
= &ipv6_devconf
.use_tempaddr
,
3841 .maxlen
= sizeof(int),
3843 .proc_handler
= &proc_dointvec
,
3846 .ctl_name
= NET_IPV6_TEMP_VALID_LFT
,
3847 .procname
= "temp_valid_lft",
3848 .data
= &ipv6_devconf
.temp_valid_lft
,
3849 .maxlen
= sizeof(int),
3851 .proc_handler
= &proc_dointvec
,
3854 .ctl_name
= NET_IPV6_TEMP_PREFERED_LFT
,
3855 .procname
= "temp_prefered_lft",
3856 .data
= &ipv6_devconf
.temp_prefered_lft
,
3857 .maxlen
= sizeof(int),
3859 .proc_handler
= &proc_dointvec
,
3862 .ctl_name
= NET_IPV6_REGEN_MAX_RETRY
,
3863 .procname
= "regen_max_retry",
3864 .data
= &ipv6_devconf
.regen_max_retry
,
3865 .maxlen
= sizeof(int),
3867 .proc_handler
= &proc_dointvec
,
3870 .ctl_name
= NET_IPV6_MAX_DESYNC_FACTOR
,
3871 .procname
= "max_desync_factor",
3872 .data
= &ipv6_devconf
.max_desync_factor
,
3873 .maxlen
= sizeof(int),
3875 .proc_handler
= &proc_dointvec
,
3879 .ctl_name
= NET_IPV6_MAX_ADDRESSES
,
3880 .procname
= "max_addresses",
3881 .data
= &ipv6_devconf
.max_addresses
,
3882 .maxlen
= sizeof(int),
3884 .proc_handler
= &proc_dointvec
,
3887 .ctl_name
= NET_IPV6_ACCEPT_RA_DEFRTR
,
3888 .procname
= "accept_ra_defrtr",
3889 .data
= &ipv6_devconf
.accept_ra_defrtr
,
3890 .maxlen
= sizeof(int),
3892 .proc_handler
= &proc_dointvec
,
3895 .ctl_name
= NET_IPV6_ACCEPT_RA_PINFO
,
3896 .procname
= "accept_ra_pinfo",
3897 .data
= &ipv6_devconf
.accept_ra_pinfo
,
3898 .maxlen
= sizeof(int),
3900 .proc_handler
= &proc_dointvec
,
3902 #ifdef CONFIG_IPV6_ROUTER_PREF
3904 .ctl_name
= NET_IPV6_ACCEPT_RA_RTR_PREF
,
3905 .procname
= "accept_ra_rtr_pref",
3906 .data
= &ipv6_devconf
.accept_ra_rtr_pref
,
3907 .maxlen
= sizeof(int),
3909 .proc_handler
= &proc_dointvec
,
3912 .ctl_name
= NET_IPV6_RTR_PROBE_INTERVAL
,
3913 .procname
= "router_probe_interval",
3914 .data
= &ipv6_devconf
.rtr_probe_interval
,
3915 .maxlen
= sizeof(int),
3917 .proc_handler
= &proc_dointvec_jiffies
,
3918 .strategy
= &sysctl_jiffies
,
3920 #ifdef CONFIG_IPV6_ROUTE_INFO
3922 .ctl_name
= NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN
,
3923 .procname
= "accept_ra_rt_info_max_plen",
3924 .data
= &ipv6_devconf
.accept_ra_rt_info_max_plen
,
3925 .maxlen
= sizeof(int),
3927 .proc_handler
= &proc_dointvec
,
3932 .ctl_name
= NET_IPV6_PROXY_NDP
,
3933 .procname
= "proxy_ndp",
3934 .data
= &ipv6_devconf
.proxy_ndp
,
3935 .maxlen
= sizeof(int),
3937 .proc_handler
= &proc_dointvec
,
3940 .ctl_name
= 0, /* sentinel */
3945 .ctl_name
= NET_PROTO_CONF_ALL
,
3948 .child
= addrconf_sysctl
.addrconf_vars
,
3951 .ctl_name
= 0, /* sentinel */
3954 .addrconf_conf_dir
= {
3956 .ctl_name
= NET_IPV6_CONF
,
3959 .child
= addrconf_sysctl
.addrconf_dev
,
3962 .ctl_name
= 0, /* sentinel */
3965 .addrconf_proto_dir
= {
3967 .ctl_name
= NET_IPV6
,
3970 .child
= addrconf_sysctl
.addrconf_conf_dir
,
3973 .ctl_name
= 0, /* sentinel */
3976 .addrconf_root_dir
= {
3978 .ctl_name
= CTL_NET
,
3981 .child
= addrconf_sysctl
.addrconf_proto_dir
,
3984 .ctl_name
= 0, /* sentinel */
3989 static void addrconf_sysctl_register(struct inet6_dev
*idev
, struct ipv6_devconf
*p
)
3992 struct net_device
*dev
= idev
? idev
->dev
: NULL
;
3993 struct addrconf_sysctl_table
*t
;
3994 char *dev_name
= NULL
;
3996 t
= kmemdup(&addrconf_sysctl
, sizeof(*t
), GFP_KERNEL
);
3999 for (i
=0; t
->addrconf_vars
[i
].data
; i
++) {
4000 t
->addrconf_vars
[i
].data
+= (char*)p
- (char*)&ipv6_devconf
;
4001 t
->addrconf_vars
[i
].de
= NULL
;
4002 t
->addrconf_vars
[i
].extra1
= idev
; /* embedded; no ref */
4005 dev_name
= dev
->name
;
4006 t
->addrconf_dev
[0].ctl_name
= dev
->ifindex
;
4008 dev_name
= "default";
4009 t
->addrconf_dev
[0].ctl_name
= NET_PROTO_CONF_DEFAULT
;
4013 * Make a copy of dev_name, because '.procname' is regarded as const
4014 * by sysctl and we wouldn't want anyone to change it under our feet
4015 * (see SIOCSIFNAME).
4017 dev_name
= kstrdup(dev_name
, GFP_KERNEL
);
4021 t
->addrconf_dev
[0].procname
= dev_name
;
4023 t
->addrconf_dev
[0].child
= t
->addrconf_vars
;
4024 t
->addrconf_dev
[0].de
= NULL
;
4025 t
->addrconf_conf_dir
[0].child
= t
->addrconf_dev
;
4026 t
->addrconf_conf_dir
[0].de
= NULL
;
4027 t
->addrconf_proto_dir
[0].child
= t
->addrconf_conf_dir
;
4028 t
->addrconf_proto_dir
[0].de
= NULL
;
4029 t
->addrconf_root_dir
[0].child
= t
->addrconf_proto_dir
;
4030 t
->addrconf_root_dir
[0].de
= NULL
;
4032 t
->sysctl_header
= register_sysctl_table(t
->addrconf_root_dir
);
4033 if (t
->sysctl_header
== NULL
)
4048 static void addrconf_sysctl_unregister(struct ipv6_devconf
*p
)
4051 struct addrconf_sysctl_table
*t
= p
->sysctl
;
4053 unregister_sysctl_table(t
->sysctl_header
);
4054 kfree(t
->addrconf_dev
[0].procname
);
4066 int register_inet6addr_notifier(struct notifier_block
*nb
)
4068 return atomic_notifier_chain_register(&inet6addr_chain
, nb
);
4071 int unregister_inet6addr_notifier(struct notifier_block
*nb
)
4073 return atomic_notifier_chain_unregister(&inet6addr_chain
,nb
);
4077 * Init / cleanup code
4080 int __init
addrconf_init(void)
4084 /* The addrconf netdev notifier requires that loopback_dev
4085 * has it's ipv6 private information allocated and setup
4086 * before it can bring up and give link-local addresses
4087 * to other devices which are up.
4089 * Unfortunately, loopback_dev is not necessarily the first
4090 * entry in the global dev_base list of net devices. In fact,
4091 * it is likely to be the very last entry on that list.
4092 * So this causes the notifier registry below to try and
4093 * give link-local addresses to all devices besides loopback_dev
4094 * first, then loopback_dev, which cases all the non-loopback_dev
4095 * devices to fail to get a link-local address.
4097 * So, as a temporary fix, allocate the ipv6 structure for
4098 * loopback_dev first by hand.
4099 * Longer term, all of the dependencies ipv6 has upon the loopback
4100 * device and it being up should be removed.
4103 if (!ipv6_add_dev(&loopback_dev
))
4109 ip6_null_entry
.rt6i_idev
= in6_dev_get(&loopback_dev
);
4111 register_netdevice_notifier(&ipv6_dev_notf
);
4114 rtnetlink_links
[PF_INET6
] = inet6_rtnetlink_table
;
4115 #ifdef CONFIG_SYSCTL
4116 addrconf_sysctl
.sysctl_header
=
4117 register_sysctl_table(addrconf_sysctl
.addrconf_root_dir
);
4118 addrconf_sysctl_register(NULL
, &ipv6_devconf_dflt
);
4124 void __exit
addrconf_cleanup(void)
4126 struct net_device
*dev
;
4127 struct inet6_dev
*idev
;
4128 struct inet6_ifaddr
*ifa
;
4131 unregister_netdevice_notifier(&ipv6_dev_notf
);
4133 rtnetlink_links
[PF_INET6
] = NULL
;
4134 #ifdef CONFIG_SYSCTL
4135 addrconf_sysctl_unregister(&ipv6_devconf_dflt
);
4136 addrconf_sysctl_unregister(&ipv6_devconf
);
4145 for (dev
=dev_base
; dev
; dev
=dev
->next
) {
4146 if ((idev
= __in6_dev_get(dev
)) == NULL
)
4148 addrconf_ifdown(dev
, 1);
4150 addrconf_ifdown(&loopback_dev
, 2);
4156 write_lock_bh(&addrconf_hash_lock
);
4157 for (i
=0; i
< IN6_ADDR_HSIZE
; i
++) {
4158 for (ifa
=inet6_addr_lst
[i
]; ifa
; ) {
4159 struct inet6_ifaddr
*bifa
;
4162 ifa
= ifa
->lst_next
;
4163 printk(KERN_DEBUG
"bug: IPv6 address leakage detected: ifa=%p\n", bifa
);
4164 /* Do not free it; something is wrong.
4165 Now we can investigate it with debugger.
4169 write_unlock_bh(&addrconf_hash_lock
);
4171 del_timer(&addr_chk_timer
);
4175 #ifdef CONFIG_PROC_FS
4176 proc_net_remove("if_inet6");