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/sched.h>
48 #include <linux/net.h>
49 #include <linux/in6.h>
50 #include <linux/netdevice.h>
51 #include <linux/if_addr.h>
52 #include <linux/if_arp.h>
53 #include <linux/if_arcnet.h>
54 #include <linux/if_infiniband.h>
55 #include <linux/route.h>
56 #include <linux/inetdevice.h>
57 #include <linux/init.h>
59 #include <linux/sysctl.h>
61 #include <linux/capability.h>
62 #include <linux/delay.h>
63 #include <linux/notifier.h>
64 #include <linux/string.h>
70 #include <net/protocol.h>
71 #include <net/ndisc.h>
72 #include <net/ip6_route.h>
73 #include <net/addrconf.h>
76 #include <net/netlink.h>
77 #include <linux/if_tunnel.h>
78 #include <linux/rtnetlink.h>
80 #ifdef CONFIG_IPV6_PRIVACY
81 #include <linux/random.h>
84 #include <asm/uaccess.h>
86 #include <linux/proc_fs.h>
87 #include <linux/seq_file.h>
89 /* Set to 3 to get tracing... */
93 #define ADBG(x) printk x
98 #define INFINITY_LIFE_TIME 0xFFFFFFFF
99 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
102 static void addrconf_sysctl_register(struct inet6_dev
*idev
, struct ipv6_devconf
*p
);
103 static void addrconf_sysctl_unregister(struct ipv6_devconf
*p
);
106 #ifdef CONFIG_IPV6_PRIVACY
107 static int __ipv6_regen_rndid(struct inet6_dev
*idev
);
108 static int __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
);
109 static void ipv6_regen_rndid(unsigned long data
);
111 static int desync_factor
= MAX_DESYNC_FACTOR
* HZ
;
114 static int ipv6_count_addresses(struct inet6_dev
*idev
);
117 * Configured unicast address hash table
119 static struct inet6_ifaddr
*inet6_addr_lst
[IN6_ADDR_HSIZE
];
120 static DEFINE_RWLOCK(addrconf_hash_lock
);
122 static void addrconf_verify(unsigned long);
124 static DEFINE_TIMER(addr_chk_timer
, addrconf_verify
, 0, 0);
125 static DEFINE_SPINLOCK(addrconf_verify_lock
);
127 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
);
128 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
);
130 static int addrconf_ifdown(struct net_device
*dev
, int how
);
132 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
, u32 flags
);
133 static void addrconf_dad_timer(unsigned long data
);
134 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
);
135 static void addrconf_dad_run(struct inet6_dev
*idev
);
136 static void addrconf_rs_timer(unsigned long data
);
137 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
138 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
140 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
141 struct prefix_info
*pinfo
);
142 static int ipv6_chk_same_addr(const struct in6_addr
*addr
, struct net_device
*dev
);
144 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain
);
146 struct ipv6_devconf ipv6_devconf __read_mostly
= {
148 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
149 .mtu6
= IPV6_MIN_MTU
,
151 .accept_redirects
= 1,
153 .force_mld_version
= 0,
155 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
156 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
157 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
158 #ifdef CONFIG_IPV6_PRIVACY
160 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
161 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
162 .regen_max_retry
= REGEN_MAX_RETRY
,
163 .max_desync_factor
= MAX_DESYNC_FACTOR
,
165 .max_addresses
= IPV6_MAX_ADDRESSES
,
166 .accept_ra_defrtr
= 1,
167 .accept_ra_pinfo
= 1,
168 #ifdef CONFIG_IPV6_ROUTER_PREF
169 .accept_ra_rtr_pref
= 1,
170 .rtr_probe_interval
= 60 * HZ
,
171 #ifdef CONFIG_IPV6_ROUTE_INFO
172 .accept_ra_rt_info_max_plen
= 0,
178 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly
= {
180 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
181 .mtu6
= IPV6_MIN_MTU
,
183 .accept_redirects
= 1,
186 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
187 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
188 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
189 #ifdef CONFIG_IPV6_PRIVACY
191 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
192 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
193 .regen_max_retry
= REGEN_MAX_RETRY
,
194 .max_desync_factor
= MAX_DESYNC_FACTOR
,
196 .max_addresses
= IPV6_MAX_ADDRESSES
,
197 .accept_ra_defrtr
= 1,
198 .accept_ra_pinfo
= 1,
199 #ifdef CONFIG_IPV6_ROUTER_PREF
200 .accept_ra_rtr_pref
= 1,
201 .rtr_probe_interval
= 60 * HZ
,
202 #ifdef CONFIG_IPV6_ROUTE_INFO
203 .accept_ra_rt_info_max_plen
= 0,
209 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
211 const struct in6_addr in6addr_any
= IN6ADDR_ANY_INIT
;
213 const struct in6_addr in6addr_loopback
= IN6ADDR_LOOPBACK_INIT
;
215 #define IPV6_ADDR_SCOPE_TYPE(scope) ((scope) << 16)
217 static inline unsigned ipv6_addr_scope2type(unsigned scope
)
220 case IPV6_ADDR_SCOPE_NODELOCAL
:
221 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL
) |
223 case IPV6_ADDR_SCOPE_LINKLOCAL
:
224 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL
) |
225 IPV6_ADDR_LINKLOCAL
);
226 case IPV6_ADDR_SCOPE_SITELOCAL
:
227 return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL
) |
228 IPV6_ADDR_SITELOCAL
);
230 return IPV6_ADDR_SCOPE_TYPE(scope
);
233 int __ipv6_addr_type(const struct in6_addr
*addr
)
237 st
= addr
->s6_addr32
[0];
239 /* Consider all addresses with the first three bits different of
240 000 and 111 as unicasts.
242 if ((st
& htonl(0xE0000000)) != htonl(0x00000000) &&
243 (st
& htonl(0xE0000000)) != htonl(0xE0000000))
244 return (IPV6_ADDR_UNICAST
|
245 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL
));
247 if ((st
& htonl(0xFF000000)) == htonl(0xFF000000)) {
249 /* addr-select 3.1 */
250 return (IPV6_ADDR_MULTICAST
|
251 ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr
)));
254 if ((st
& htonl(0xFFC00000)) == htonl(0xFE800000))
255 return (IPV6_ADDR_LINKLOCAL
| IPV6_ADDR_UNICAST
|
256 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL
)); /* addr-select 3.1 */
257 if ((st
& htonl(0xFFC00000)) == htonl(0xFEC00000))
258 return (IPV6_ADDR_SITELOCAL
| IPV6_ADDR_UNICAST
|
259 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL
)); /* addr-select 3.1 */
261 if ((addr
->s6_addr32
[0] | addr
->s6_addr32
[1]) == 0) {
262 if (addr
->s6_addr32
[2] == 0) {
263 if (addr
->s6_addr32
[3] == 0)
264 return IPV6_ADDR_ANY
;
266 if (addr
->s6_addr32
[3] == htonl(0x00000001))
267 return (IPV6_ADDR_LOOPBACK
| IPV6_ADDR_UNICAST
|
268 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL
)); /* addr-select 3.4 */
270 return (IPV6_ADDR_COMPATv4
| IPV6_ADDR_UNICAST
|
271 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL
)); /* addr-select 3.3 */
274 if (addr
->s6_addr32
[2] == htonl(0x0000ffff))
275 return (IPV6_ADDR_MAPPED
|
276 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL
)); /* addr-select 3.3 */
279 return (IPV6_ADDR_RESERVED
|
280 IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL
)); /* addr-select 3.4 */
283 static void addrconf_del_timer(struct inet6_ifaddr
*ifp
)
285 if (del_timer(&ifp
->timer
))
289 enum addrconf_timer_t
296 static void addrconf_mod_timer(struct inet6_ifaddr
*ifp
,
297 enum addrconf_timer_t what
,
300 if (!del_timer(&ifp
->timer
))
305 ifp
->timer
.function
= addrconf_dad_timer
;
308 ifp
->timer
.function
= addrconf_rs_timer
;
312 ifp
->timer
.expires
= jiffies
+ when
;
313 add_timer(&ifp
->timer
);
316 /* Nobody refers to this device, we may destroy it. */
318 static void in6_dev_finish_destroy_rcu(struct rcu_head
*head
)
320 struct inet6_dev
*idev
= container_of(head
, struct inet6_dev
, rcu
);
324 void in6_dev_finish_destroy(struct inet6_dev
*idev
)
326 struct net_device
*dev
= idev
->dev
;
327 BUG_TRAP(idev
->addr_list
==NULL
);
328 BUG_TRAP(idev
->mc_list
==NULL
);
329 #ifdef NET_REFCNT_DEBUG
330 printk(KERN_DEBUG
"in6_dev_finish_destroy: %s\n", dev
? dev
->name
: "NIL");
334 printk("Freeing alive inet6 device %p\n", idev
);
337 snmp6_free_dev(idev
);
338 call_rcu(&idev
->rcu
, in6_dev_finish_destroy_rcu
);
341 static struct inet6_dev
* ipv6_add_dev(struct net_device
*dev
)
343 struct inet6_dev
*ndev
;
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 dev
->type
== ARPHRD_NONE
||
400 dev
->type
== ARPHRD_SIT
) {
402 "%s: Disabled Privacy Extensions\n",
404 ndev
->cnf
.use_tempaddr
= -1;
407 ipv6_regen_rndid((unsigned long) ndev
);
411 if (netif_carrier_ok(dev
))
412 ndev
->if_flags
|= IF_READY
;
414 /* protected by rtnl_lock */
415 rcu_assign_pointer(dev
->ip6_ptr
, ndev
);
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
);
429 static struct inet6_dev
* ipv6_find_idev(struct net_device
*dev
)
431 struct inet6_dev
*idev
;
435 if ((idev
= __in6_dev_get(dev
)) == NULL
) {
436 if ((idev
= ipv6_add_dev(dev
)) == NULL
)
440 if (dev
->flags
&IFF_UP
)
446 static void dev_forward_change(struct inet6_dev
*idev
)
448 struct net_device
*dev
;
449 struct inet6_ifaddr
*ifa
;
450 struct in6_addr addr
;
455 if (dev
&& (dev
->flags
& IFF_MULTICAST
)) {
456 ipv6_addr_all_routers(&addr
);
458 if (idev
->cnf
.forwarding
)
459 ipv6_dev_mc_inc(dev
, &addr
);
461 ipv6_dev_mc_dec(dev
, &addr
);
463 for (ifa
=idev
->addr_list
; ifa
; ifa
=ifa
->if_next
) {
464 if (idev
->cnf
.forwarding
)
465 addrconf_join_anycast(ifa
);
467 addrconf_leave_anycast(ifa
);
472 static void addrconf_forward_change(void)
474 struct net_device
*dev
;
475 struct inet6_dev
*idev
;
477 read_lock(&dev_base_lock
);
478 for (dev
=dev_base
; dev
; dev
=dev
->next
) {
480 idev
= __in6_dev_get(dev
);
482 int changed
= (!idev
->cnf
.forwarding
) ^ (!ipv6_devconf
.forwarding
);
483 idev
->cnf
.forwarding
= ipv6_devconf
.forwarding
;
485 dev_forward_change(idev
);
489 read_unlock(&dev_base_lock
);
493 /* Nobody refers to this ifaddr, destroy it */
495 void inet6_ifa_finish_destroy(struct inet6_ifaddr
*ifp
)
497 BUG_TRAP(ifp
->if_next
==NULL
);
498 BUG_TRAP(ifp
->lst_next
==NULL
);
499 #ifdef NET_REFCNT_DEBUG
500 printk(KERN_DEBUG
"inet6_ifa_finish_destroy\n");
503 in6_dev_put(ifp
->idev
);
505 if (del_timer(&ifp
->timer
))
506 printk("Timer is still running, when freeing ifa=%p\n", ifp
);
509 printk("Freeing alive inet6 address %p\n", ifp
);
512 dst_release(&ifp
->rt
->u
.dst
);
518 ipv6_link_dev_addr(struct inet6_dev
*idev
, struct inet6_ifaddr
*ifp
)
520 struct inet6_ifaddr
*ifa
, **ifap
;
521 int ifp_scope
= ipv6_addr_src_scope(&ifp
->addr
);
524 * Each device address list is sorted in order of scope -
525 * global before linklocal.
527 for (ifap
= &idev
->addr_list
; (ifa
= *ifap
) != NULL
;
528 ifap
= &ifa
->if_next
) {
529 if (ifp_scope
>= ipv6_addr_src_scope(&ifa
->addr
))
533 ifp
->if_next
= *ifap
;
537 /* On success it returns ifp with increased reference count */
539 static struct inet6_ifaddr
*
540 ipv6_add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
, int pfxlen
,
541 int scope
, u32 flags
)
543 struct inet6_ifaddr
*ifa
= NULL
;
550 err
= -ENODEV
; /*XXX*/
554 write_lock(&addrconf_hash_lock
);
556 /* Ignore adding duplicate addresses on an interface */
557 if (ipv6_chk_same_addr(addr
, idev
->dev
)) {
558 ADBG(("ipv6_add_addr: already assigned\n"));
563 ifa
= kzalloc(sizeof(struct inet6_ifaddr
), GFP_ATOMIC
);
566 ADBG(("ipv6_add_addr: malloc failed\n"));
571 rt
= addrconf_dst_alloc(idev
, addr
, 0);
577 ipv6_addr_copy(&ifa
->addr
, addr
);
579 spin_lock_init(&ifa
->lock
);
580 init_timer(&ifa
->timer
);
581 ifa
->timer
.data
= (unsigned long) ifa
;
583 ifa
->prefix_len
= pfxlen
;
584 ifa
->flags
= flags
| IFA_F_TENTATIVE
;
585 ifa
->cstamp
= ifa
->tstamp
= jiffies
;
594 /* Add to big hash table */
595 hash
= ipv6_addr_hash(addr
);
597 ifa
->lst_next
= inet6_addr_lst
[hash
];
598 inet6_addr_lst
[hash
] = ifa
;
600 write_unlock(&addrconf_hash_lock
);
602 write_lock(&idev
->lock
);
603 /* Add to inet6_dev unicast addr list. */
604 ipv6_link_dev_addr(idev
, ifa
);
606 #ifdef CONFIG_IPV6_PRIVACY
607 if (ifa
->flags
&IFA_F_TEMPORARY
) {
608 ifa
->tmp_next
= idev
->tempaddr_list
;
609 idev
->tempaddr_list
= ifa
;
615 write_unlock(&idev
->lock
);
617 rcu_read_unlock_bh();
619 if (likely(err
== 0))
620 atomic_notifier_call_chain(&inet6addr_chain
, NETDEV_UP
, ifa
);
628 write_unlock(&addrconf_hash_lock
);
632 /* This function wants to get referenced ifp and releases it before return */
634 static void ipv6_del_addr(struct inet6_ifaddr
*ifp
)
636 struct inet6_ifaddr
*ifa
, **ifap
;
637 struct inet6_dev
*idev
= ifp
->idev
;
639 int deleted
= 0, onlink
= 0;
640 unsigned long expires
= jiffies
;
642 hash
= ipv6_addr_hash(&ifp
->addr
);
646 write_lock_bh(&addrconf_hash_lock
);
647 for (ifap
= &inet6_addr_lst
[hash
]; (ifa
=*ifap
) != NULL
;
648 ifap
= &ifa
->lst_next
) {
650 *ifap
= ifa
->lst_next
;
652 ifa
->lst_next
= NULL
;
656 write_unlock_bh(&addrconf_hash_lock
);
658 write_lock_bh(&idev
->lock
);
659 #ifdef CONFIG_IPV6_PRIVACY
660 if (ifp
->flags
&IFA_F_TEMPORARY
) {
661 for (ifap
= &idev
->tempaddr_list
; (ifa
=*ifap
) != NULL
;
662 ifap
= &ifa
->tmp_next
) {
664 *ifap
= ifa
->tmp_next
;
666 in6_ifa_put(ifp
->ifpub
);
670 ifa
->tmp_next
= NULL
;
677 for (ifap
= &idev
->addr_list
; (ifa
=*ifap
) != NULL
;) {
679 *ifap
= ifa
->if_next
;
682 if (!(ifp
->flags
& IFA_F_PERMANENT
) || onlink
> 0)
686 } else if (ifp
->flags
& IFA_F_PERMANENT
) {
687 if (ipv6_prefix_equal(&ifa
->addr
, &ifp
->addr
,
689 if (ifa
->flags
& IFA_F_PERMANENT
) {
694 unsigned long lifetime
;
699 spin_lock(&ifa
->lock
);
700 lifetime
= min_t(unsigned long,
701 ifa
->valid_lft
, 0x7fffffffUL
/HZ
);
702 if (time_before(expires
,
703 ifa
->tstamp
+ lifetime
* HZ
))
704 expires
= ifa
->tstamp
+ lifetime
* HZ
;
705 spin_unlock(&ifa
->lock
);
709 ifap
= &ifa
->if_next
;
711 write_unlock_bh(&idev
->lock
);
713 ipv6_ifa_notify(RTM_DELADDR
, ifp
);
715 atomic_notifier_call_chain(&inet6addr_chain
, NETDEV_DOWN
, ifp
);
717 addrconf_del_timer(ifp
);
720 * Purge or update corresponding prefix
722 * 1) we don't purge prefix here if address was not permanent.
723 * prefix is managed by its own lifetime.
724 * 2) if there're no addresses, delete prefix.
725 * 3) if there're still other permanent address(es),
726 * corresponding prefix is still permanent.
727 * 4) otherwise, update prefix lifetime to the
728 * longest valid lifetime among the corresponding
729 * addresses on the device.
730 * Note: subsequent RA will update lifetime.
734 if ((ifp
->flags
& IFA_F_PERMANENT
) && onlink
< 1) {
735 struct in6_addr prefix
;
738 ipv6_addr_prefix(&prefix
, &ifp
->addr
, ifp
->prefix_len
);
739 rt
= rt6_lookup(&prefix
, NULL
, ifp
->idev
->dev
->ifindex
, 1);
741 if (rt
&& ((rt
->rt6i_flags
& (RTF_GATEWAY
| RTF_DEFAULT
)) == 0)) {
745 } else if (!(rt
->rt6i_flags
& RTF_EXPIRES
)) {
746 rt
->rt6i_expires
= expires
;
747 rt
->rt6i_flags
|= RTF_EXPIRES
;
750 dst_release(&rt
->u
.dst
);
756 #ifdef CONFIG_IPV6_PRIVACY
757 static int ipv6_create_tempaddr(struct inet6_ifaddr
*ifp
, struct inet6_ifaddr
*ift
)
759 struct inet6_dev
*idev
= ifp
->idev
;
760 struct in6_addr addr
, *tmpaddr
;
761 unsigned long tmp_prefered_lft
, tmp_valid_lft
, tmp_cstamp
, tmp_tstamp
;
766 write_lock(&idev
->lock
);
768 spin_lock_bh(&ift
->lock
);
769 memcpy(&addr
.s6_addr
[8], &ift
->addr
.s6_addr
[8], 8);
770 spin_unlock_bh(&ift
->lock
);
777 if (idev
->cnf
.use_tempaddr
<= 0) {
778 write_unlock(&idev
->lock
);
780 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
785 spin_lock_bh(&ifp
->lock
);
786 if (ifp
->regen_count
++ >= idev
->cnf
.regen_max_retry
) {
787 idev
->cnf
.use_tempaddr
= -1; /*XXX*/
788 spin_unlock_bh(&ifp
->lock
);
789 write_unlock(&idev
->lock
);
791 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
797 memcpy(addr
.s6_addr
, ifp
->addr
.s6_addr
, 8);
798 if (__ipv6_try_regen_rndid(idev
, tmpaddr
) < 0) {
799 spin_unlock_bh(&ifp
->lock
);
800 write_unlock(&idev
->lock
);
802 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
808 memcpy(&addr
.s6_addr
[8], idev
->rndid
, 8);
809 tmp_valid_lft
= min_t(__u32
,
811 idev
->cnf
.temp_valid_lft
);
812 tmp_prefered_lft
= min_t(__u32
,
814 idev
->cnf
.temp_prefered_lft
- desync_factor
/ HZ
);
815 tmp_plen
= ifp
->prefix_len
;
816 max_addresses
= idev
->cnf
.max_addresses
;
817 tmp_cstamp
= ifp
->cstamp
;
818 tmp_tstamp
= ifp
->tstamp
;
819 spin_unlock_bh(&ifp
->lock
);
821 write_unlock(&idev
->lock
);
822 ift
= !max_addresses
||
823 ipv6_count_addresses(idev
) < max_addresses
?
824 ipv6_add_addr(idev
, &addr
, tmp_plen
,
825 ipv6_addr_type(&addr
)&IPV6_ADDR_SCOPE_MASK
, IFA_F_TEMPORARY
) : NULL
;
826 if (!ift
|| IS_ERR(ift
)) {
830 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
832 write_lock(&idev
->lock
);
836 spin_lock_bh(&ift
->lock
);
838 ift
->valid_lft
= tmp_valid_lft
;
839 ift
->prefered_lft
= tmp_prefered_lft
;
840 ift
->cstamp
= tmp_cstamp
;
841 ift
->tstamp
= tmp_tstamp
;
842 spin_unlock_bh(&ift
->lock
);
844 addrconf_dad_start(ift
, 0);
853 * Choose an appropriate source address (RFC3484)
855 struct ipv6_saddr_score
{
863 #define IPV6_SADDR_SCORE_LOCAL 0x0001
864 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
865 #define IPV6_SADDR_SCORE_HOA 0x0008
866 #define IPV6_SADDR_SCORE_OIF 0x0010
867 #define IPV6_SADDR_SCORE_LABEL 0x0020
868 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
870 static int inline ipv6_saddr_preferred(int type
)
872 if (type
& (IPV6_ADDR_MAPPED
|IPV6_ADDR_COMPATv4
|
873 IPV6_ADDR_LOOPBACK
|IPV6_ADDR_RESERVED
))
878 /* static matching label */
879 static int inline ipv6_saddr_label(const struct in6_addr
*addr
, int type
)
882 * prefix (longest match) label
883 * -----------------------------
892 if (type
& IPV6_ADDR_LOOPBACK
)
894 else if (type
& IPV6_ADDR_COMPATv4
)
896 else if (type
& IPV6_ADDR_MAPPED
)
898 else if (addr
->s6_addr32
[0] == htonl(0x20010000))
900 else if (addr
->s6_addr16
[0] == htons(0x2002))
902 else if ((addr
->s6_addr
[0] & 0xfe) == 0xfc)
907 int ipv6_dev_get_saddr(struct net_device
*daddr_dev
,
908 struct in6_addr
*daddr
, struct in6_addr
*saddr
)
910 struct ipv6_saddr_score hiscore
;
911 struct inet6_ifaddr
*ifa_result
= NULL
;
912 int daddr_type
= __ipv6_addr_type(daddr
);
913 int daddr_scope
= __ipv6_addr_src_scope(daddr_type
);
914 u32 daddr_label
= ipv6_saddr_label(daddr
, daddr_type
);
915 struct net_device
*dev
;
917 memset(&hiscore
, 0, sizeof(hiscore
));
919 read_lock(&dev_base_lock
);
922 for (dev
= dev_base
; dev
; dev
=dev
->next
) {
923 struct inet6_dev
*idev
;
924 struct inet6_ifaddr
*ifa
;
926 /* Rule 0: Candidate Source Address (section 4)
927 * - multicast and link-local destination address,
928 * the set of candidate source address MUST only
929 * include addresses assigned to interfaces
930 * belonging to the same link as the outgoing
932 * (- For site-local destination addresses, the
933 * set of candidate source addresses MUST only
934 * include addresses assigned to interfaces
935 * belonging to the same site as the outgoing
938 if ((daddr_type
& IPV6_ADDR_MULTICAST
||
939 daddr_scope
<= IPV6_ADDR_SCOPE_LINKLOCAL
) &&
940 daddr_dev
&& dev
!= daddr_dev
)
943 idev
= __in6_dev_get(dev
);
947 read_lock_bh(&idev
->lock
);
948 for (ifa
= idev
->addr_list
; ifa
; ifa
= ifa
->if_next
) {
949 struct ipv6_saddr_score score
;
951 score
.addr_type
= __ipv6_addr_type(&ifa
->addr
);
954 * - Tentative Address (RFC2462 section 5.4)
955 * - A tentative address is not considered
956 * "assigned to an interface" in the traditional
958 * - Candidate Source Address (section 4)
959 * - In any case, anycast addresses, multicast
960 * addresses, and the unspecified address MUST
961 * NOT be included in a candidate set.
963 if (ifa
->flags
& IFA_F_TENTATIVE
)
965 if (unlikely(score
.addr_type
== IPV6_ADDR_ANY
||
966 score
.addr_type
& IPV6_ADDR_MULTICAST
)) {
967 LIMIT_NETDEBUG(KERN_DEBUG
968 "ADDRCONF: unspecified / multicast address"
969 "assigned as unicast address on %s",
979 if (ifa_result
== NULL
) {
980 /* record it if the first available entry */
984 /* Rule 1: Prefer same address */
985 if (hiscore
.rule
< 1) {
986 if (ipv6_addr_equal(&ifa_result
->addr
, daddr
))
987 hiscore
.attrs
|= IPV6_SADDR_SCORE_LOCAL
;
990 if (ipv6_addr_equal(&ifa
->addr
, daddr
)) {
991 score
.attrs
|= IPV6_SADDR_SCORE_LOCAL
;
992 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_LOCAL
)) {
997 if (hiscore
.attrs
& IPV6_SADDR_SCORE_LOCAL
)
1001 /* Rule 2: Prefer appropriate scope */
1002 if (hiscore
.rule
< 2) {
1003 hiscore
.scope
= __ipv6_addr_src_scope(hiscore
.addr_type
);
1006 score
.scope
= __ipv6_addr_src_scope(score
.addr_type
);
1007 if (hiscore
.scope
< score
.scope
) {
1008 if (hiscore
.scope
< daddr_scope
) {
1013 } else if (score
.scope
< hiscore
.scope
) {
1014 if (score
.scope
< daddr_scope
)
1015 break; /* addresses sorted by scope */
1022 /* Rule 3: Avoid deprecated address */
1023 if (hiscore
.rule
< 3) {
1024 if (ipv6_saddr_preferred(hiscore
.addr_type
) ||
1025 !(ifa_result
->flags
& IFA_F_DEPRECATED
))
1026 hiscore
.attrs
|= IPV6_SADDR_SCORE_PREFERRED
;
1029 if (ipv6_saddr_preferred(score
.addr_type
) ||
1030 !(ifa
->flags
& IFA_F_DEPRECATED
)) {
1031 score
.attrs
|= IPV6_SADDR_SCORE_PREFERRED
;
1032 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_PREFERRED
)) {
1037 if (hiscore
.attrs
& IPV6_SADDR_SCORE_PREFERRED
)
1041 /* Rule 4: Prefer home address -- not implemented yet */
1042 if (hiscore
.rule
< 4)
1045 /* Rule 5: Prefer outgoing interface */
1046 if (hiscore
.rule
< 5) {
1047 if (daddr_dev
== NULL
||
1048 daddr_dev
== ifa_result
->idev
->dev
)
1049 hiscore
.attrs
|= IPV6_SADDR_SCORE_OIF
;
1052 if (daddr_dev
== NULL
||
1053 daddr_dev
== ifa
->idev
->dev
) {
1054 score
.attrs
|= IPV6_SADDR_SCORE_OIF
;
1055 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_OIF
)) {
1060 if (hiscore
.attrs
& IPV6_SADDR_SCORE_OIF
)
1064 /* Rule 6: Prefer matching label */
1065 if (hiscore
.rule
< 6) {
1066 if (ipv6_saddr_label(&ifa_result
->addr
, hiscore
.addr_type
) == daddr_label
)
1067 hiscore
.attrs
|= IPV6_SADDR_SCORE_LABEL
;
1070 if (ipv6_saddr_label(&ifa
->addr
, score
.addr_type
) == daddr_label
) {
1071 score
.attrs
|= IPV6_SADDR_SCORE_LABEL
;
1072 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_LABEL
)) {
1077 if (hiscore
.attrs
& IPV6_SADDR_SCORE_LABEL
)
1081 #ifdef CONFIG_IPV6_PRIVACY
1082 /* Rule 7: Prefer public address
1083 * Note: prefer temprary address if use_tempaddr >= 2
1085 if (hiscore
.rule
< 7) {
1086 if ((!(ifa_result
->flags
& IFA_F_TEMPORARY
)) ^
1087 (ifa_result
->idev
->cnf
.use_tempaddr
>= 2))
1088 hiscore
.attrs
|= IPV6_SADDR_SCORE_PRIVACY
;
1091 if ((!(ifa
->flags
& IFA_F_TEMPORARY
)) ^
1092 (ifa
->idev
->cnf
.use_tempaddr
>= 2)) {
1093 score
.attrs
|= IPV6_SADDR_SCORE_PRIVACY
;
1094 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_PRIVACY
)) {
1099 if (hiscore
.attrs
& IPV6_SADDR_SCORE_PRIVACY
)
1103 if (hiscore
.rule
< 7)
1106 /* Rule 8: Use longest matching prefix */
1107 if (hiscore
.rule
< 8) {
1108 hiscore
.matchlen
= ipv6_addr_diff(&ifa_result
->addr
, daddr
);
1111 score
.matchlen
= ipv6_addr_diff(&ifa
->addr
, daddr
);
1112 if (score
.matchlen
> hiscore
.matchlen
) {
1117 else if (score
.matchlen
< hiscore
.matchlen
)
1121 /* Final Rule: choose first available one */
1125 in6_ifa_put(ifa_result
);
1130 read_unlock_bh(&idev
->lock
);
1133 read_unlock(&dev_base_lock
);
1136 return -EADDRNOTAVAIL
;
1138 ipv6_addr_copy(saddr
, &ifa_result
->addr
);
1139 in6_ifa_put(ifa_result
);
1144 int ipv6_get_saddr(struct dst_entry
*dst
,
1145 struct in6_addr
*daddr
, struct in6_addr
*saddr
)
1147 return ipv6_dev_get_saddr(dst
? ((struct rt6_info
*)dst
)->rt6i_idev
->dev
: NULL
, daddr
, saddr
);
1151 int ipv6_get_lladdr(struct net_device
*dev
, struct in6_addr
*addr
)
1153 struct inet6_dev
*idev
;
1154 int err
= -EADDRNOTAVAIL
;
1157 if ((idev
= __in6_dev_get(dev
)) != NULL
) {
1158 struct inet6_ifaddr
*ifp
;
1160 read_lock_bh(&idev
->lock
);
1161 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1162 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1163 ipv6_addr_copy(addr
, &ifp
->addr
);
1168 read_unlock_bh(&idev
->lock
);
1174 static int ipv6_count_addresses(struct inet6_dev
*idev
)
1177 struct inet6_ifaddr
*ifp
;
1179 read_lock_bh(&idev
->lock
);
1180 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
)
1182 read_unlock_bh(&idev
->lock
);
1186 int ipv6_chk_addr(struct in6_addr
*addr
, struct net_device
*dev
, int strict
)
1188 struct inet6_ifaddr
* ifp
;
1189 u8 hash
= ipv6_addr_hash(addr
);
1191 read_lock_bh(&addrconf_hash_lock
);
1192 for(ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
=ifp
->lst_next
) {
1193 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
1194 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1195 if (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1196 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
))
1200 read_unlock_bh(&addrconf_hash_lock
);
1205 int ipv6_chk_same_addr(const struct in6_addr
*addr
, struct net_device
*dev
)
1207 struct inet6_ifaddr
* ifp
;
1208 u8 hash
= ipv6_addr_hash(addr
);
1210 for(ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
=ifp
->lst_next
) {
1211 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1212 if (dev
== NULL
|| ifp
->idev
->dev
== dev
)
1219 struct inet6_ifaddr
* ipv6_get_ifaddr(struct in6_addr
*addr
, struct net_device
*dev
, int strict
)
1221 struct inet6_ifaddr
* ifp
;
1222 u8 hash
= ipv6_addr_hash(addr
);
1224 read_lock_bh(&addrconf_hash_lock
);
1225 for(ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
=ifp
->lst_next
) {
1226 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1227 if (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1228 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
)) {
1234 read_unlock_bh(&addrconf_hash_lock
);
1239 int ipv6_rcv_saddr_equal(const struct sock
*sk
, const struct sock
*sk2
)
1241 const struct in6_addr
*sk_rcv_saddr6
= &inet6_sk(sk
)->rcv_saddr
;
1242 const struct in6_addr
*sk2_rcv_saddr6
= inet6_rcv_saddr(sk2
);
1243 u32 sk_rcv_saddr
= inet_sk(sk
)->rcv_saddr
;
1244 u32 sk2_rcv_saddr
= inet_rcv_saddr(sk2
);
1245 int sk_ipv6only
= ipv6_only_sock(sk
);
1246 int sk2_ipv6only
= inet_v6_ipv6only(sk2
);
1247 int addr_type
= ipv6_addr_type(sk_rcv_saddr6
);
1248 int addr_type2
= sk2_rcv_saddr6
? ipv6_addr_type(sk2_rcv_saddr6
) : IPV6_ADDR_MAPPED
;
1250 if (!sk2_rcv_saddr
&& !sk_ipv6only
)
1253 if (addr_type2
== IPV6_ADDR_ANY
&&
1254 !(sk2_ipv6only
&& addr_type
== IPV6_ADDR_MAPPED
))
1257 if (addr_type
== IPV6_ADDR_ANY
&&
1258 !(sk_ipv6only
&& addr_type2
== IPV6_ADDR_MAPPED
))
1261 if (sk2_rcv_saddr6
&&
1262 ipv6_addr_equal(sk_rcv_saddr6
, sk2_rcv_saddr6
))
1265 if (addr_type
== IPV6_ADDR_MAPPED
&&
1267 (!sk2_rcv_saddr
|| !sk_rcv_saddr
|| sk_rcv_saddr
== sk2_rcv_saddr
))
1273 /* Gets referenced address, destroys ifaddr */
1275 static void addrconf_dad_stop(struct inet6_ifaddr
*ifp
)
1277 if (ifp
->flags
&IFA_F_PERMANENT
) {
1278 spin_lock_bh(&ifp
->lock
);
1279 addrconf_del_timer(ifp
);
1280 ifp
->flags
|= IFA_F_TENTATIVE
;
1281 spin_unlock_bh(&ifp
->lock
);
1283 #ifdef CONFIG_IPV6_PRIVACY
1284 } else if (ifp
->flags
&IFA_F_TEMPORARY
) {
1285 struct inet6_ifaddr
*ifpub
;
1286 spin_lock_bh(&ifp
->lock
);
1289 in6_ifa_hold(ifpub
);
1290 spin_unlock_bh(&ifp
->lock
);
1291 ipv6_create_tempaddr(ifpub
, ifp
);
1294 spin_unlock_bh(&ifp
->lock
);
1302 void addrconf_dad_failure(struct inet6_ifaddr
*ifp
)
1304 if (net_ratelimit())
1305 printk(KERN_INFO
"%s: duplicate address detected!\n", ifp
->idev
->dev
->name
);
1306 addrconf_dad_stop(ifp
);
1309 /* Join to solicited addr multicast group. */
1311 void addrconf_join_solict(struct net_device
*dev
, struct in6_addr
*addr
)
1313 struct in6_addr maddr
;
1315 if (dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1318 addrconf_addr_solict_mult(addr
, &maddr
);
1319 ipv6_dev_mc_inc(dev
, &maddr
);
1322 void addrconf_leave_solict(struct inet6_dev
*idev
, struct in6_addr
*addr
)
1324 struct in6_addr maddr
;
1326 if (idev
->dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1329 addrconf_addr_solict_mult(addr
, &maddr
);
1330 __ipv6_dev_mc_dec(idev
, &maddr
);
1333 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
)
1335 struct in6_addr addr
;
1336 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1337 if (ipv6_addr_any(&addr
))
1339 ipv6_dev_ac_inc(ifp
->idev
->dev
, &addr
);
1342 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
)
1344 struct in6_addr addr
;
1345 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1346 if (ipv6_addr_any(&addr
))
1348 __ipv6_dev_ac_dec(ifp
->idev
, &addr
);
1351 static int addrconf_ifid_eui48(u8
*eui
, struct net_device
*dev
)
1353 if (dev
->addr_len
!= ETH_ALEN
)
1355 memcpy(eui
, dev
->dev_addr
, 3);
1356 memcpy(eui
+ 5, dev
->dev_addr
+ 3, 3);
1359 * The zSeries OSA network cards can be shared among various
1360 * OS instances, but the OSA cards have only one MAC address.
1361 * This leads to duplicate address conflicts in conjunction
1362 * with IPv6 if more than one instance uses the same card.
1364 * The driver for these cards can deliver a unique 16-bit
1365 * identifier for each instance sharing the same card. It is
1366 * placed instead of 0xFFFE in the interface identifier. The
1367 * "u" bit of the interface identifier is not inverted in this
1368 * case. Hence the resulting interface identifier has local
1369 * scope according to RFC2373.
1372 eui
[3] = (dev
->dev_id
>> 8) & 0xFF;
1373 eui
[4] = dev
->dev_id
& 0xFF;
1382 static int addrconf_ifid_arcnet(u8
*eui
, struct net_device
*dev
)
1384 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1385 if (dev
->addr_len
!= ARCNET_ALEN
)
1388 eui
[7] = *(u8
*)dev
->dev_addr
;
1392 static int addrconf_ifid_infiniband(u8
*eui
, struct net_device
*dev
)
1394 if (dev
->addr_len
!= INFINIBAND_ALEN
)
1396 memcpy(eui
, dev
->dev_addr
+ 12, 8);
1401 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
)
1403 switch (dev
->type
) {
1406 case ARPHRD_IEEE802_TR
:
1407 return addrconf_ifid_eui48(eui
, dev
);
1409 return addrconf_ifid_arcnet(eui
, dev
);
1410 case ARPHRD_INFINIBAND
:
1411 return addrconf_ifid_infiniband(eui
, dev
);
1416 static int ipv6_inherit_eui64(u8
*eui
, struct inet6_dev
*idev
)
1419 struct inet6_ifaddr
*ifp
;
1421 read_lock_bh(&idev
->lock
);
1422 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1423 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1424 memcpy(eui
, ifp
->addr
.s6_addr
+8, 8);
1429 read_unlock_bh(&idev
->lock
);
1433 #ifdef CONFIG_IPV6_PRIVACY
1434 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1435 static int __ipv6_regen_rndid(struct inet6_dev
*idev
)
1438 get_random_bytes(idev
->rndid
, sizeof(idev
->rndid
));
1439 idev
->rndid
[0] &= ~0x02;
1442 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1443 * check if generated address is not inappropriate
1445 * - Reserved subnet anycast (RFC 2526)
1446 * 11111101 11....11 1xxxxxxx
1447 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1448 * 00-00-5E-FE-xx-xx-xx-xx
1450 * - XXX: already assigned to an address on the device
1452 if (idev
->rndid
[0] == 0xfd &&
1453 (idev
->rndid
[1]&idev
->rndid
[2]&idev
->rndid
[3]&idev
->rndid
[4]&idev
->rndid
[5]&idev
->rndid
[6]) == 0xff &&
1454 (idev
->rndid
[7]&0x80))
1456 if ((idev
->rndid
[0]|idev
->rndid
[1]) == 0) {
1457 if (idev
->rndid
[2] == 0x5e && idev
->rndid
[3] == 0xfe)
1459 if ((idev
->rndid
[2]|idev
->rndid
[3]|idev
->rndid
[4]|idev
->rndid
[5]|idev
->rndid
[6]|idev
->rndid
[7]) == 0x00)
1466 static void ipv6_regen_rndid(unsigned long data
)
1468 struct inet6_dev
*idev
= (struct inet6_dev
*) data
;
1469 unsigned long expires
;
1472 write_lock_bh(&idev
->lock
);
1477 if (__ipv6_regen_rndid(idev
) < 0)
1481 idev
->cnf
.temp_prefered_lft
* HZ
-
1482 idev
->cnf
.regen_max_retry
* idev
->cnf
.dad_transmits
* idev
->nd_parms
->retrans_time
- desync_factor
;
1483 if (time_before(expires
, jiffies
)) {
1485 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1490 if (!mod_timer(&idev
->regen_timer
, expires
))
1494 write_unlock_bh(&idev
->lock
);
1495 rcu_read_unlock_bh();
1499 static int __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
) {
1502 if (tmpaddr
&& memcmp(idev
->rndid
, &tmpaddr
->s6_addr
[8], 8) == 0)
1503 ret
= __ipv6_regen_rndid(idev
);
1513 addrconf_prefix_route(struct in6_addr
*pfx
, int plen
, struct net_device
*dev
,
1514 unsigned long expires
, u32 flags
)
1516 struct fib6_config cfg
= {
1517 .fc_table
= RT6_TABLE_PREFIX
,
1518 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
1519 .fc_ifindex
= dev
->ifindex
,
1520 .fc_expires
= expires
,
1522 .fc_flags
= RTF_UP
| flags
,
1525 ipv6_addr_copy(&cfg
.fc_dst
, pfx
);
1527 /* Prevent useless cloning on PtP SIT.
1528 This thing is done here expecting that the whole
1529 class of non-broadcast devices need not cloning.
1531 if (dev
->type
== ARPHRD_SIT
&& (dev
->flags
& IFF_POINTOPOINT
))
1532 cfg
.fc_flags
|= RTF_NONEXTHOP
;
1534 ip6_route_add(&cfg
);
1537 /* Create "default" multicast route to the interface */
1539 static void addrconf_add_mroute(struct net_device
*dev
)
1541 struct fib6_config cfg
= {
1542 .fc_table
= RT6_TABLE_LOCAL
,
1543 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
1544 .fc_ifindex
= dev
->ifindex
,
1549 ipv6_addr_set(&cfg
.fc_dst
, htonl(0xFF000000), 0, 0, 0);
1551 ip6_route_add(&cfg
);
1554 static void sit_route_add(struct net_device
*dev
)
1556 struct fib6_config cfg
= {
1557 .fc_table
= RT6_TABLE_MAIN
,
1558 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
1559 .fc_ifindex
= dev
->ifindex
,
1561 .fc_flags
= RTF_UP
| RTF_NONEXTHOP
,
1564 /* prefix length - 96 bits "::d.d.d.d" */
1565 ip6_route_add(&cfg
);
1568 static void addrconf_add_lroute(struct net_device
*dev
)
1570 struct in6_addr addr
;
1572 ipv6_addr_set(&addr
, htonl(0xFE800000), 0, 0, 0);
1573 addrconf_prefix_route(&addr
, 64, dev
, 0, 0);
1576 static struct inet6_dev
*addrconf_add_dev(struct net_device
*dev
)
1578 struct inet6_dev
*idev
;
1582 if ((idev
= ipv6_find_idev(dev
)) == NULL
)
1585 /* Add default multicast route */
1586 addrconf_add_mroute(dev
);
1588 /* Add link local route */
1589 addrconf_add_lroute(dev
);
1593 void addrconf_prefix_rcv(struct net_device
*dev
, u8
*opt
, int len
)
1595 struct prefix_info
*pinfo
;
1599 unsigned long rt_expires
;
1600 struct inet6_dev
*in6_dev
;
1602 pinfo
= (struct prefix_info
*) opt
;
1604 if (len
< sizeof(struct prefix_info
)) {
1605 ADBG(("addrconf: prefix option too short\n"));
1610 * Validation checks ([ADDRCONF], page 19)
1613 addr_type
= ipv6_addr_type(&pinfo
->prefix
);
1615 if (addr_type
& (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
))
1618 valid_lft
= ntohl(pinfo
->valid
);
1619 prefered_lft
= ntohl(pinfo
->prefered
);
1621 if (prefered_lft
> valid_lft
) {
1622 if (net_ratelimit())
1623 printk(KERN_WARNING
"addrconf: prefix option has invalid lifetime\n");
1627 in6_dev
= in6_dev_get(dev
);
1629 if (in6_dev
== NULL
) {
1630 if (net_ratelimit())
1631 printk(KERN_DEBUG
"addrconf: device %s not configured\n", dev
->name
);
1636 * Two things going on here:
1637 * 1) Add routes for on-link prefixes
1638 * 2) Configure prefixes with the auto flag set
1641 /* Avoid arithmetic overflow. Really, we could
1642 save rt_expires in seconds, likely valid_lft,
1643 but it would require division in fib gc, that it
1646 if (valid_lft
>= 0x7FFFFFFF/HZ
)
1647 rt_expires
= 0x7FFFFFFF - (0x7FFFFFFF % HZ
);
1649 rt_expires
= valid_lft
* HZ
;
1652 * We convert this (in jiffies) to clock_t later.
1653 * Avoid arithmetic overflow there as well.
1654 * Overflow can happen only if HZ < USER_HZ.
1656 if (HZ
< USER_HZ
&& rt_expires
> 0x7FFFFFFF / USER_HZ
)
1657 rt_expires
= 0x7FFFFFFF / USER_HZ
;
1659 if (pinfo
->onlink
) {
1660 struct rt6_info
*rt
;
1661 rt
= rt6_lookup(&pinfo
->prefix
, NULL
, dev
->ifindex
, 1);
1663 if (rt
&& ((rt
->rt6i_flags
& (RTF_GATEWAY
| RTF_DEFAULT
)) == 0)) {
1664 if (rt
->rt6i_flags
&RTF_EXPIRES
) {
1665 if (valid_lft
== 0) {
1669 rt
->rt6i_expires
= jiffies
+ rt_expires
;
1672 } else if (valid_lft
) {
1673 addrconf_prefix_route(&pinfo
->prefix
, pinfo
->prefix_len
,
1674 dev
, jiffies_to_clock_t(rt_expires
), RTF_ADDRCONF
|RTF_EXPIRES
|RTF_PREFIX_RT
);
1677 dst_release(&rt
->u
.dst
);
1680 /* Try to figure out our local address for this prefix */
1682 if (pinfo
->autoconf
&& in6_dev
->cnf
.autoconf
) {
1683 struct inet6_ifaddr
* ifp
;
1684 struct in6_addr addr
;
1685 int create
= 0, update_lft
= 0;
1687 if (pinfo
->prefix_len
== 64) {
1688 memcpy(&addr
, &pinfo
->prefix
, 8);
1689 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) &&
1690 ipv6_inherit_eui64(addr
.s6_addr
+ 8, in6_dev
)) {
1691 in6_dev_put(in6_dev
);
1696 if (net_ratelimit())
1697 printk(KERN_DEBUG
"IPv6 addrconf: prefix with wrong length %d\n",
1699 in6_dev_put(in6_dev
);
1704 ifp
= ipv6_get_ifaddr(&addr
, dev
, 1);
1706 if (ifp
== NULL
&& valid_lft
) {
1707 int max_addresses
= in6_dev
->cnf
.max_addresses
;
1709 /* Do not allow to create too much of autoconfigured
1710 * addresses; this would be too easy way to crash kernel.
1712 if (!max_addresses
||
1713 ipv6_count_addresses(in6_dev
) < max_addresses
)
1714 ifp
= ipv6_add_addr(in6_dev
, &addr
, pinfo
->prefix_len
,
1715 addr_type
&IPV6_ADDR_SCOPE_MASK
, 0);
1717 if (!ifp
|| IS_ERR(ifp
)) {
1718 in6_dev_put(in6_dev
);
1722 update_lft
= create
= 1;
1723 ifp
->cstamp
= jiffies
;
1724 addrconf_dad_start(ifp
, RTF_ADDRCONF
|RTF_PREFIX_RT
);
1730 #ifdef CONFIG_IPV6_PRIVACY
1731 struct inet6_ifaddr
*ift
;
1735 /* update lifetime (RFC2462 5.5.3 e) */
1736 spin_lock(&ifp
->lock
);
1738 if (ifp
->valid_lft
> (now
- ifp
->tstamp
) / HZ
)
1739 stored_lft
= ifp
->valid_lft
- (now
- ifp
->tstamp
) / HZ
;
1742 if (!update_lft
&& stored_lft
) {
1743 if (valid_lft
> MIN_VALID_LIFETIME
||
1744 valid_lft
> stored_lft
)
1746 else if (stored_lft
<= MIN_VALID_LIFETIME
) {
1747 /* valid_lft <= stored_lft is always true */
1751 valid_lft
= MIN_VALID_LIFETIME
;
1752 if (valid_lft
< prefered_lft
)
1753 prefered_lft
= valid_lft
;
1759 ifp
->valid_lft
= valid_lft
;
1760 ifp
->prefered_lft
= prefered_lft
;
1763 ifp
->flags
&= ~IFA_F_DEPRECATED
;
1764 spin_unlock(&ifp
->lock
);
1766 if (!(flags
&IFA_F_TENTATIVE
))
1767 ipv6_ifa_notify(0, ifp
);
1769 spin_unlock(&ifp
->lock
);
1771 #ifdef CONFIG_IPV6_PRIVACY
1772 read_lock_bh(&in6_dev
->lock
);
1773 /* update all temporary addresses in the list */
1774 for (ift
=in6_dev
->tempaddr_list
; ift
; ift
=ift
->tmp_next
) {
1776 * When adjusting the lifetimes of an existing
1777 * temporary address, only lower the lifetimes.
1778 * Implementations must not increase the
1779 * lifetimes of an existing temporary address
1780 * when processing a Prefix Information Option.
1782 spin_lock(&ift
->lock
);
1784 if (ift
->valid_lft
> valid_lft
&&
1785 ift
->valid_lft
- valid_lft
> (jiffies
- ift
->tstamp
) / HZ
)
1786 ift
->valid_lft
= valid_lft
+ (jiffies
- ift
->tstamp
) / HZ
;
1787 if (ift
->prefered_lft
> prefered_lft
&&
1788 ift
->prefered_lft
- prefered_lft
> (jiffies
- ift
->tstamp
) / HZ
)
1789 ift
->prefered_lft
= prefered_lft
+ (jiffies
- ift
->tstamp
) / HZ
;
1790 spin_unlock(&ift
->lock
);
1791 if (!(flags
&IFA_F_TENTATIVE
))
1792 ipv6_ifa_notify(0, ift
);
1795 if (create
&& in6_dev
->cnf
.use_tempaddr
> 0) {
1797 * When a new public address is created as described in [ADDRCONF],
1798 * also create a new temporary address.
1800 read_unlock_bh(&in6_dev
->lock
);
1801 ipv6_create_tempaddr(ifp
, NULL
);
1803 read_unlock_bh(&in6_dev
->lock
);
1810 inet6_prefix_notify(RTM_NEWPREFIX
, in6_dev
, pinfo
);
1811 in6_dev_put(in6_dev
);
1815 * Set destination address.
1816 * Special case for SIT interfaces where we create a new "virtual"
1819 int addrconf_set_dstaddr(void __user
*arg
)
1821 struct in6_ifreq ireq
;
1822 struct net_device
*dev
;
1828 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
1831 dev
= __dev_get_by_index(ireq
.ifr6_ifindex
);
1837 if (dev
->type
== ARPHRD_SIT
) {
1840 struct ip_tunnel_parm p
;
1842 err
= -EADDRNOTAVAIL
;
1843 if (!(ipv6_addr_type(&ireq
.ifr6_addr
) & IPV6_ADDR_COMPATv4
))
1846 memset(&p
, 0, sizeof(p
));
1847 p
.iph
.daddr
= ireq
.ifr6_addr
.s6_addr32
[3];
1851 p
.iph
.protocol
= IPPROTO_IPV6
;
1853 ifr
.ifr_ifru
.ifru_data
= (void __user
*)&p
;
1855 oldfs
= get_fs(); set_fs(KERNEL_DS
);
1856 err
= dev
->do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
1861 if ((dev
= __dev_get_by_name(p
.name
)) == NULL
)
1863 err
= dev_open(dev
);
1873 * Manual configuration of address on an interface
1875 static int inet6_addr_add(int ifindex
, struct in6_addr
*pfx
, int plen
,
1876 __u32 prefered_lft
, __u32 valid_lft
)
1878 struct inet6_ifaddr
*ifp
;
1879 struct inet6_dev
*idev
;
1880 struct net_device
*dev
;
1886 /* check the lifetime */
1887 if (!valid_lft
|| prefered_lft
> valid_lft
)
1890 if ((dev
= __dev_get_by_index(ifindex
)) == NULL
)
1893 if ((idev
= addrconf_add_dev(dev
)) == NULL
)
1896 scope
= ipv6_addr_scope(pfx
);
1898 if (valid_lft
== INFINITY_LIFE_TIME
)
1899 ifa_flags
|= IFA_F_PERMANENT
;
1900 else if (valid_lft
>= 0x7FFFFFFF/HZ
)
1901 valid_lft
= 0x7FFFFFFF/HZ
;
1903 if (prefered_lft
== 0)
1904 ifa_flags
|= IFA_F_DEPRECATED
;
1905 else if ((prefered_lft
>= 0x7FFFFFFF/HZ
) &&
1906 (prefered_lft
!= INFINITY_LIFE_TIME
))
1907 prefered_lft
= 0x7FFFFFFF/HZ
;
1909 ifp
= ipv6_add_addr(idev
, pfx
, plen
, scope
, ifa_flags
);
1912 spin_lock_bh(&ifp
->lock
);
1913 ifp
->valid_lft
= valid_lft
;
1914 ifp
->prefered_lft
= prefered_lft
;
1915 ifp
->tstamp
= jiffies
;
1916 spin_unlock_bh(&ifp
->lock
);
1918 addrconf_dad_start(ifp
, 0);
1924 return PTR_ERR(ifp
);
1927 static int inet6_addr_del(int ifindex
, struct in6_addr
*pfx
, int plen
)
1929 struct inet6_ifaddr
*ifp
;
1930 struct inet6_dev
*idev
;
1931 struct net_device
*dev
;
1933 if ((dev
= __dev_get_by_index(ifindex
)) == NULL
)
1936 if ((idev
= __in6_dev_get(dev
)) == NULL
)
1939 read_lock_bh(&idev
->lock
);
1940 for (ifp
= idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1941 if (ifp
->prefix_len
== plen
&&
1942 ipv6_addr_equal(pfx
, &ifp
->addr
)) {
1944 read_unlock_bh(&idev
->lock
);
1948 /* If the last address is deleted administratively,
1949 disable IPv6 on this interface.
1951 if (idev
->addr_list
== NULL
)
1952 addrconf_ifdown(idev
->dev
, 1);
1956 read_unlock_bh(&idev
->lock
);
1957 return -EADDRNOTAVAIL
;
1961 int addrconf_add_ifaddr(void __user
*arg
)
1963 struct in6_ifreq ireq
;
1966 if (!capable(CAP_NET_ADMIN
))
1969 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
1973 err
= inet6_addr_add(ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, ireq
.ifr6_prefixlen
,
1974 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
1979 int addrconf_del_ifaddr(void __user
*arg
)
1981 struct in6_ifreq ireq
;
1984 if (!capable(CAP_NET_ADMIN
))
1987 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
1991 err
= inet6_addr_del(ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, ireq
.ifr6_prefixlen
);
1996 static void sit_add_v4_addrs(struct inet6_dev
*idev
)
1998 struct inet6_ifaddr
* ifp
;
1999 struct in6_addr addr
;
2000 struct net_device
*dev
;
2005 memset(&addr
, 0, sizeof(struct in6_addr
));
2006 memcpy(&addr
.s6_addr32
[3], idev
->dev
->dev_addr
, 4);
2008 if (idev
->dev
->flags
&IFF_POINTOPOINT
) {
2009 addr
.s6_addr32
[0] = htonl(0xfe800000);
2012 scope
= IPV6_ADDR_COMPATv4
;
2015 if (addr
.s6_addr32
[3]) {
2016 ifp
= ipv6_add_addr(idev
, &addr
, 128, scope
, IFA_F_PERMANENT
);
2018 spin_lock_bh(&ifp
->lock
);
2019 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2020 spin_unlock_bh(&ifp
->lock
);
2021 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2027 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
2028 struct in_device
* in_dev
= __in_dev_get_rtnl(dev
);
2029 if (in_dev
&& (dev
->flags
& IFF_UP
)) {
2030 struct in_ifaddr
* ifa
;
2034 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
2037 addr
.s6_addr32
[3] = ifa
->ifa_local
;
2039 if (ifa
->ifa_scope
== RT_SCOPE_LINK
)
2041 if (ifa
->ifa_scope
>= RT_SCOPE_HOST
) {
2042 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2046 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2051 ifp
= ipv6_add_addr(idev
, &addr
, plen
, flag
,
2054 spin_lock_bh(&ifp
->lock
);
2055 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2056 spin_unlock_bh(&ifp
->lock
);
2057 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2065 static void init_loopback(struct net_device
*dev
)
2067 struct inet6_dev
*idev
;
2068 struct inet6_ifaddr
* ifp
;
2074 if ((idev
= ipv6_find_idev(dev
)) == NULL
) {
2075 printk(KERN_DEBUG
"init loopback: add_dev failed\n");
2079 ifp
= ipv6_add_addr(idev
, &in6addr_loopback
, 128, IFA_HOST
, IFA_F_PERMANENT
);
2081 spin_lock_bh(&ifp
->lock
);
2082 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2083 spin_unlock_bh(&ifp
->lock
);
2084 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2089 static void addrconf_add_linklocal(struct inet6_dev
*idev
, struct in6_addr
*addr
)
2091 struct inet6_ifaddr
* ifp
;
2093 ifp
= ipv6_add_addr(idev
, addr
, 64, IFA_LINK
, IFA_F_PERMANENT
);
2095 addrconf_dad_start(ifp
, 0);
2100 static void addrconf_dev_config(struct net_device
*dev
)
2102 struct in6_addr addr
;
2103 struct inet6_dev
* idev
;
2107 if ((dev
->type
!= ARPHRD_ETHER
) &&
2108 (dev
->type
!= ARPHRD_FDDI
) &&
2109 (dev
->type
!= ARPHRD_IEEE802_TR
) &&
2110 (dev
->type
!= ARPHRD_ARCNET
) &&
2111 (dev
->type
!= ARPHRD_INFINIBAND
)) {
2112 /* Alas, we support only Ethernet autoconfiguration. */
2116 idev
= addrconf_add_dev(dev
);
2120 memset(&addr
, 0, sizeof(struct in6_addr
));
2121 addr
.s6_addr32
[0] = htonl(0xFE800000);
2123 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) == 0)
2124 addrconf_add_linklocal(idev
, &addr
);
2127 static void addrconf_sit_config(struct net_device
*dev
)
2129 struct inet6_dev
*idev
;
2134 * Configure the tunnel with one of our IPv4
2135 * addresses... we should configure all of
2136 * our v4 addrs in the tunnel
2139 if ((idev
= ipv6_find_idev(dev
)) == NULL
) {
2140 printk(KERN_DEBUG
"init sit: add_dev failed\n");
2144 sit_add_v4_addrs(idev
);
2146 if (dev
->flags
&IFF_POINTOPOINT
) {
2147 addrconf_add_mroute(dev
);
2148 addrconf_add_lroute(dev
);
2154 ipv6_inherit_linklocal(struct inet6_dev
*idev
, struct net_device
*link_dev
)
2156 struct in6_addr lladdr
;
2158 if (!ipv6_get_lladdr(link_dev
, &lladdr
)) {
2159 addrconf_add_linklocal(idev
, &lladdr
);
2165 static void ip6_tnl_add_linklocal(struct inet6_dev
*idev
)
2167 struct net_device
*link_dev
;
2169 /* first try to inherit the link-local address from the link device */
2170 if (idev
->dev
->iflink
&&
2171 (link_dev
= __dev_get_by_index(idev
->dev
->iflink
))) {
2172 if (!ipv6_inherit_linklocal(idev
, link_dev
))
2175 /* then try to inherit it from any device */
2176 for (link_dev
= dev_base
; link_dev
; link_dev
= link_dev
->next
) {
2177 if (!ipv6_inherit_linklocal(idev
, link_dev
))
2180 printk(KERN_DEBUG
"init ip6-ip6: add_linklocal failed\n");
2184 * Autoconfigure tunnel with a link-local address so routing protocols,
2185 * DHCPv6, MLD etc. can be run over the virtual link
2188 static void addrconf_ip6_tnl_config(struct net_device
*dev
)
2190 struct inet6_dev
*idev
;
2194 if ((idev
= addrconf_add_dev(dev
)) == NULL
) {
2195 printk(KERN_DEBUG
"init ip6-ip6: add_dev failed\n");
2198 ip6_tnl_add_linklocal(idev
);
2201 static int addrconf_notify(struct notifier_block
*this, unsigned long event
,
2204 struct net_device
*dev
= (struct net_device
*) data
;
2205 struct inet6_dev
*idev
= __in6_dev_get(dev
);
2206 int run_pending
= 0;
2211 if (event
== NETDEV_UP
) {
2212 if (!netif_carrier_ok(dev
)) {
2213 /* device is not ready yet. */
2215 "ADDRCONF(NETDEV_UP): %s: "
2216 "link is not ready\n",
2222 idev
->if_flags
|= IF_READY
;
2224 if (!netif_carrier_ok(dev
)) {
2225 /* device is still not ready. */
2230 if (idev
->if_flags
& IF_READY
) {
2231 /* device is already configured. */
2234 idev
->if_flags
|= IF_READY
;
2238 "ADDRCONF(NETDEV_CHANGE): %s: "
2239 "link becomes ready\n",
2247 addrconf_sit_config(dev
);
2249 case ARPHRD_TUNNEL6
:
2250 addrconf_ip6_tnl_config(dev
);
2252 case ARPHRD_LOOPBACK
:
2257 addrconf_dev_config(dev
);
2262 addrconf_dad_run(idev
);
2264 /* If the MTU changed during the interface down, when the
2265 interface up, the changed MTU must be reflected in the
2266 idev as well as routers.
2268 if (idev
->cnf
.mtu6
!= dev
->mtu
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2269 rt6_mtu_change(dev
, dev
->mtu
);
2270 idev
->cnf
.mtu6
= dev
->mtu
;
2272 idev
->tstamp
= jiffies
;
2273 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
2274 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2275 stop IPv6 on this interface.
2277 if (dev
->mtu
< IPV6_MIN_MTU
)
2278 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
2282 case NETDEV_CHANGEMTU
:
2283 if ( idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2284 rt6_mtu_change(dev
, dev
->mtu
);
2285 idev
->cnf
.mtu6
= dev
->mtu
;
2289 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2292 case NETDEV_UNREGISTER
:
2294 * Remove all addresses from this interface.
2296 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
2299 case NETDEV_CHANGENAME
:
2300 #ifdef CONFIG_SYSCTL
2302 addrconf_sysctl_unregister(&idev
->cnf
);
2303 neigh_sysctl_unregister(idev
->nd_parms
);
2304 neigh_sysctl_register(dev
, idev
->nd_parms
,
2305 NET_IPV6
, NET_IPV6_NEIGH
, "ipv6",
2306 &ndisc_ifinfo_sysctl_change
,
2308 addrconf_sysctl_register(idev
, &idev
->cnf
);
2318 * addrconf module should be notified of a device going up
2320 static struct notifier_block ipv6_dev_notf
= {
2321 .notifier_call
= addrconf_notify
,
2325 static int addrconf_ifdown(struct net_device
*dev
, int how
)
2327 struct inet6_dev
*idev
;
2328 struct inet6_ifaddr
*ifa
, **bifa
;
2333 if (dev
== &loopback_dev
&& how
== 1)
2337 neigh_ifdown(&nd_tbl
, dev
);
2339 idev
= __in6_dev_get(dev
);
2343 /* Step 1: remove reference to ipv6 device from parent device.
2349 /* protected by rtnl_lock */
2350 rcu_assign_pointer(dev
->ip6_ptr
, NULL
);
2352 /* Step 1.5: remove snmp6 entry */
2353 snmp6_unregister_dev(idev
);
2357 /* Step 2: clear hash table */
2358 for (i
=0; i
<IN6_ADDR_HSIZE
; i
++) {
2359 bifa
= &inet6_addr_lst
[i
];
2361 write_lock_bh(&addrconf_hash_lock
);
2362 while ((ifa
= *bifa
) != NULL
) {
2363 if (ifa
->idev
== idev
) {
2364 *bifa
= ifa
->lst_next
;
2365 ifa
->lst_next
= NULL
;
2366 addrconf_del_timer(ifa
);
2370 bifa
= &ifa
->lst_next
;
2372 write_unlock_bh(&addrconf_hash_lock
);
2375 write_lock_bh(&idev
->lock
);
2377 /* Step 3: clear flags for stateless addrconf */
2379 idev
->if_flags
&= ~(IF_RS_SENT
|IF_RA_RCVD
|IF_READY
);
2381 /* Step 4: clear address list */
2382 #ifdef CONFIG_IPV6_PRIVACY
2383 if (how
== 1 && del_timer(&idev
->regen_timer
))
2386 /* clear tempaddr list */
2387 while ((ifa
= idev
->tempaddr_list
) != NULL
) {
2388 idev
->tempaddr_list
= ifa
->tmp_next
;
2389 ifa
->tmp_next
= NULL
;
2391 write_unlock_bh(&idev
->lock
);
2392 spin_lock_bh(&ifa
->lock
);
2395 in6_ifa_put(ifa
->ifpub
);
2398 spin_unlock_bh(&ifa
->lock
);
2400 write_lock_bh(&idev
->lock
);
2403 while ((ifa
= idev
->addr_list
) != NULL
) {
2404 idev
->addr_list
= ifa
->if_next
;
2405 ifa
->if_next
= NULL
;
2407 addrconf_del_timer(ifa
);
2408 write_unlock_bh(&idev
->lock
);
2410 __ipv6_ifa_notify(RTM_DELADDR
, ifa
);
2413 write_lock_bh(&idev
->lock
);
2415 write_unlock_bh(&idev
->lock
);
2417 /* Step 5: Discard multicast list */
2420 ipv6_mc_destroy_dev(idev
);
2424 /* Step 5: netlink notification of this interface */
2425 idev
->tstamp
= jiffies
;
2426 inet6_ifinfo_notify(RTM_DELLINK
, idev
);
2428 /* Shot the device (if unregistered) */
2431 #ifdef CONFIG_SYSCTL
2432 addrconf_sysctl_unregister(&idev
->cnf
);
2433 neigh_sysctl_unregister(idev
->nd_parms
);
2435 neigh_parms_release(&nd_tbl
, idev
->nd_parms
);
2436 neigh_ifdown(&nd_tbl
, dev
);
2442 static void addrconf_rs_timer(unsigned long data
)
2444 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*) data
;
2446 if (ifp
->idev
->cnf
.forwarding
)
2449 if (ifp
->idev
->if_flags
& IF_RA_RCVD
) {
2451 * Announcement received after solicitation
2457 spin_lock(&ifp
->lock
);
2458 if (ifp
->probes
++ < ifp
->idev
->cnf
.rtr_solicits
) {
2459 struct in6_addr all_routers
;
2461 /* The wait after the last probe can be shorter */
2462 addrconf_mod_timer(ifp
, AC_RS
,
2463 (ifp
->probes
== ifp
->idev
->cnf
.rtr_solicits
) ?
2464 ifp
->idev
->cnf
.rtr_solicit_delay
:
2465 ifp
->idev
->cnf
.rtr_solicit_interval
);
2466 spin_unlock(&ifp
->lock
);
2468 ipv6_addr_all_routers(&all_routers
);
2470 ndisc_send_rs(ifp
->idev
->dev
, &ifp
->addr
, &all_routers
);
2472 spin_unlock(&ifp
->lock
);
2474 * Note: we do not support deprecated "all on-link"
2475 * assumption any longer.
2477 printk(KERN_DEBUG
"%s: no IPv6 routers present\n",
2478 ifp
->idev
->dev
->name
);
2486 * Duplicate Address Detection
2488 static void addrconf_dad_kick(struct inet6_ifaddr
*ifp
)
2490 unsigned long rand_num
;
2491 struct inet6_dev
*idev
= ifp
->idev
;
2493 rand_num
= net_random() % (idev
->cnf
.rtr_solicit_delay
? : 1);
2494 ifp
->probes
= idev
->cnf
.dad_transmits
;
2495 addrconf_mod_timer(ifp
, AC_DAD
, rand_num
);
2498 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
, u32 flags
)
2500 struct inet6_dev
*idev
= ifp
->idev
;
2501 struct net_device
*dev
= idev
->dev
;
2503 addrconf_join_solict(dev
, &ifp
->addr
);
2505 if (ifp
->prefix_len
!= 128 && (ifp
->flags
&IFA_F_PERMANENT
))
2506 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, dev
, 0,
2509 net_srandom(ifp
->addr
.s6_addr32
[3]);
2511 read_lock_bh(&idev
->lock
);
2514 spin_lock_bh(&ifp
->lock
);
2516 if (dev
->flags
&(IFF_NOARP
|IFF_LOOPBACK
) ||
2517 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
2518 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2519 spin_unlock_bh(&ifp
->lock
);
2520 read_unlock_bh(&idev
->lock
);
2522 addrconf_dad_completed(ifp
);
2526 if (!(idev
->if_flags
& IF_READY
)) {
2527 spin_unlock_bh(&ifp
->lock
);
2528 read_unlock_bh(&idev
->lock
);
2530 * If the defice is not ready:
2531 * - keep it tentative if it is a permanent address.
2532 * - otherwise, kill it.
2535 addrconf_dad_stop(ifp
);
2538 addrconf_dad_kick(ifp
);
2539 spin_unlock_bh(&ifp
->lock
);
2541 read_unlock_bh(&idev
->lock
);
2544 static void addrconf_dad_timer(unsigned long data
)
2546 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*) data
;
2547 struct inet6_dev
*idev
= ifp
->idev
;
2548 struct in6_addr unspec
;
2549 struct in6_addr mcaddr
;
2551 read_lock_bh(&idev
->lock
);
2553 read_unlock_bh(&idev
->lock
);
2556 spin_lock_bh(&ifp
->lock
);
2557 if (ifp
->probes
== 0) {
2559 * DAD was successful
2562 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2563 spin_unlock_bh(&ifp
->lock
);
2564 read_unlock_bh(&idev
->lock
);
2566 addrconf_dad_completed(ifp
);
2572 addrconf_mod_timer(ifp
, AC_DAD
, ifp
->idev
->nd_parms
->retrans_time
);
2573 spin_unlock_bh(&ifp
->lock
);
2574 read_unlock_bh(&idev
->lock
);
2576 /* send a neighbour solicitation for our addr */
2577 memset(&unspec
, 0, sizeof(unspec
));
2578 addrconf_addr_solict_mult(&ifp
->addr
, &mcaddr
);
2579 ndisc_send_ns(ifp
->idev
->dev
, NULL
, &ifp
->addr
, &mcaddr
, &unspec
);
2584 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
)
2586 struct net_device
* dev
= ifp
->idev
->dev
;
2589 * Configure the address for reception. Now it is valid.
2592 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2594 /* If added prefix is link local and forwarding is off,
2595 start sending router solicitations.
2598 if (ifp
->idev
->cnf
.forwarding
== 0 &&
2599 ifp
->idev
->cnf
.rtr_solicits
> 0 &&
2600 (dev
->flags
&IFF_LOOPBACK
) == 0 &&
2601 (ipv6_addr_type(&ifp
->addr
) & IPV6_ADDR_LINKLOCAL
)) {
2602 struct in6_addr all_routers
;
2604 ipv6_addr_all_routers(&all_routers
);
2607 * If a host as already performed a random delay
2608 * [...] as part of DAD [...] there is no need
2609 * to delay again before sending the first RS
2611 ndisc_send_rs(ifp
->idev
->dev
, &ifp
->addr
, &all_routers
);
2613 spin_lock_bh(&ifp
->lock
);
2615 ifp
->idev
->if_flags
|= IF_RS_SENT
;
2616 addrconf_mod_timer(ifp
, AC_RS
, ifp
->idev
->cnf
.rtr_solicit_interval
);
2617 spin_unlock_bh(&ifp
->lock
);
2621 static void addrconf_dad_run(struct inet6_dev
*idev
) {
2622 struct inet6_ifaddr
*ifp
;
2624 read_lock_bh(&idev
->lock
);
2625 for (ifp
= idev
->addr_list
; ifp
; ifp
= ifp
->if_next
) {
2626 spin_lock_bh(&ifp
->lock
);
2627 if (!(ifp
->flags
& IFA_F_TENTATIVE
)) {
2628 spin_unlock_bh(&ifp
->lock
);
2631 spin_unlock_bh(&ifp
->lock
);
2632 addrconf_dad_kick(ifp
);
2634 read_unlock_bh(&idev
->lock
);
2637 #ifdef CONFIG_PROC_FS
2638 struct if6_iter_state
{
2642 static struct inet6_ifaddr
*if6_get_first(struct seq_file
*seq
)
2644 struct inet6_ifaddr
*ifa
= NULL
;
2645 struct if6_iter_state
*state
= seq
->private;
2647 for (state
->bucket
= 0; state
->bucket
< IN6_ADDR_HSIZE
; ++state
->bucket
) {
2648 ifa
= inet6_addr_lst
[state
->bucket
];
2655 static struct inet6_ifaddr
*if6_get_next(struct seq_file
*seq
, struct inet6_ifaddr
*ifa
)
2657 struct if6_iter_state
*state
= seq
->private;
2659 ifa
= ifa
->lst_next
;
2661 if (!ifa
&& ++state
->bucket
< IN6_ADDR_HSIZE
) {
2662 ifa
= inet6_addr_lst
[state
->bucket
];
2668 static struct inet6_ifaddr
*if6_get_idx(struct seq_file
*seq
, loff_t pos
)
2670 struct inet6_ifaddr
*ifa
= if6_get_first(seq
);
2673 while(pos
&& (ifa
= if6_get_next(seq
, ifa
)) != NULL
)
2675 return pos
? NULL
: ifa
;
2678 static void *if6_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2680 read_lock_bh(&addrconf_hash_lock
);
2681 return if6_get_idx(seq
, *pos
);
2684 static void *if6_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2686 struct inet6_ifaddr
*ifa
;
2688 ifa
= if6_get_next(seq
, v
);
2693 static void if6_seq_stop(struct seq_file
*seq
, void *v
)
2695 read_unlock_bh(&addrconf_hash_lock
);
2698 static int if6_seq_show(struct seq_file
*seq
, void *v
)
2700 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*)v
;
2702 NIP6_SEQFMT
" %02x %02x %02x %02x %8s\n",
2704 ifp
->idev
->dev
->ifindex
,
2708 ifp
->idev
->dev
->name
);
2712 static struct seq_operations if6_seq_ops
= {
2713 .start
= if6_seq_start
,
2714 .next
= if6_seq_next
,
2715 .show
= if6_seq_show
,
2716 .stop
= if6_seq_stop
,
2719 static int if6_seq_open(struct inode
*inode
, struct file
*file
)
2721 struct seq_file
*seq
;
2723 struct if6_iter_state
*s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
2728 rc
= seq_open(file
, &if6_seq_ops
);
2732 seq
= file
->private_data
;
2741 static struct file_operations if6_fops
= {
2742 .owner
= THIS_MODULE
,
2743 .open
= if6_seq_open
,
2745 .llseek
= seq_lseek
,
2746 .release
= seq_release_private
,
2749 int __init
if6_proc_init(void)
2751 if (!proc_net_fops_create("if_inet6", S_IRUGO
, &if6_fops
))
2756 void if6_proc_exit(void)
2758 proc_net_remove("if_inet6");
2760 #endif /* CONFIG_PROC_FS */
2763 * Periodic address status verification
2766 static void addrconf_verify(unsigned long foo
)
2768 struct inet6_ifaddr
*ifp
;
2769 unsigned long now
, next
;
2772 spin_lock_bh(&addrconf_verify_lock
);
2774 next
= now
+ ADDR_CHECK_FREQUENCY
;
2776 del_timer(&addr_chk_timer
);
2778 for (i
=0; i
< IN6_ADDR_HSIZE
; i
++) {
2781 read_lock(&addrconf_hash_lock
);
2782 for (ifp
=inet6_addr_lst
[i
]; ifp
; ifp
=ifp
->lst_next
) {
2784 #ifdef CONFIG_IPV6_PRIVACY
2785 unsigned long regen_advance
;
2788 if (ifp
->flags
& IFA_F_PERMANENT
)
2791 spin_lock(&ifp
->lock
);
2792 age
= (now
- ifp
->tstamp
) / HZ
;
2794 #ifdef CONFIG_IPV6_PRIVACY
2795 regen_advance
= ifp
->idev
->cnf
.regen_max_retry
*
2796 ifp
->idev
->cnf
.dad_transmits
*
2797 ifp
->idev
->nd_parms
->retrans_time
/ HZ
;
2800 if (ifp
->valid_lft
!= INFINITY_LIFE_TIME
&&
2801 age
>= ifp
->valid_lft
) {
2802 spin_unlock(&ifp
->lock
);
2804 read_unlock(&addrconf_hash_lock
);
2807 } else if (ifp
->prefered_lft
== INFINITY_LIFE_TIME
) {
2808 spin_unlock(&ifp
->lock
);
2810 } else if (age
>= ifp
->prefered_lft
) {
2811 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2814 if (!(ifp
->flags
&IFA_F_DEPRECATED
)) {
2816 ifp
->flags
|= IFA_F_DEPRECATED
;
2819 if (time_before(ifp
->tstamp
+ ifp
->valid_lft
* HZ
, next
))
2820 next
= ifp
->tstamp
+ ifp
->valid_lft
* HZ
;
2822 spin_unlock(&ifp
->lock
);
2826 read_unlock(&addrconf_hash_lock
);
2828 ipv6_ifa_notify(0, ifp
);
2832 #ifdef CONFIG_IPV6_PRIVACY
2833 } else if ((ifp
->flags
&IFA_F_TEMPORARY
) &&
2834 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
2835 if (age
>= ifp
->prefered_lft
- regen_advance
) {
2836 struct inet6_ifaddr
*ifpub
= ifp
->ifpub
;
2837 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
2838 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
2839 if (!ifp
->regen_count
&& ifpub
) {
2842 in6_ifa_hold(ifpub
);
2843 spin_unlock(&ifp
->lock
);
2844 read_unlock(&addrconf_hash_lock
);
2845 spin_lock(&ifpub
->lock
);
2846 ifpub
->regen_count
= 0;
2847 spin_unlock(&ifpub
->lock
);
2848 ipv6_create_tempaddr(ifpub
, ifp
);
2853 } else if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
, next
))
2854 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
;
2855 spin_unlock(&ifp
->lock
);
2858 /* ifp->prefered_lft <= ifp->valid_lft */
2859 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
2860 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
2861 spin_unlock(&ifp
->lock
);
2864 read_unlock(&addrconf_hash_lock
);
2867 addr_chk_timer
.expires
= time_before(next
, jiffies
+ HZ
) ? jiffies
+ HZ
: next
;
2868 add_timer(&addr_chk_timer
);
2869 spin_unlock_bh(&addrconf_verify_lock
);
2872 static struct in6_addr
*extract_addr(struct nlattr
*addr
, struct nlattr
*local
)
2874 struct in6_addr
*pfx
= NULL
;
2877 pfx
= nla_data(addr
);
2880 if (pfx
&& nla_memcmp(local
, pfx
, sizeof(*pfx
)))
2883 pfx
= nla_data(local
);
2889 static struct nla_policy ifa_ipv6_policy
[IFA_MAX
+1] __read_mostly
= {
2890 [IFA_ADDRESS
] = { .len
= sizeof(struct in6_addr
) },
2891 [IFA_LOCAL
] = { .len
= sizeof(struct in6_addr
) },
2892 [IFA_CACHEINFO
] = { .len
= sizeof(struct ifa_cacheinfo
) },
2896 inet6_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
2898 struct ifaddrmsg
*ifm
;
2899 struct nlattr
*tb
[IFA_MAX
+1];
2900 struct in6_addr
*pfx
;
2903 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
2907 ifm
= nlmsg_data(nlh
);
2908 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
]);
2912 return inet6_addr_del(ifm
->ifa_index
, pfx
, ifm
->ifa_prefixlen
);
2915 static int inet6_addr_modify(struct inet6_ifaddr
*ifp
, u32 prefered_lft
,
2920 if (!valid_lft
|| (prefered_lft
> valid_lft
))
2923 if (valid_lft
== INFINITY_LIFE_TIME
)
2924 ifa_flags
= IFA_F_PERMANENT
;
2925 else if (valid_lft
>= 0x7FFFFFFF/HZ
)
2926 valid_lft
= 0x7FFFFFFF/HZ
;
2928 if (prefered_lft
== 0)
2929 ifa_flags
= IFA_F_DEPRECATED
;
2930 else if ((prefered_lft
>= 0x7FFFFFFF/HZ
) &&
2931 (prefered_lft
!= INFINITY_LIFE_TIME
))
2932 prefered_lft
= 0x7FFFFFFF/HZ
;
2934 spin_lock_bh(&ifp
->lock
);
2935 ifp
->flags
= (ifp
->flags
& ~(IFA_F_DEPRECATED
|IFA_F_PERMANENT
)) | ifa_flags
;
2937 ifp
->tstamp
= jiffies
;
2938 ifp
->valid_lft
= valid_lft
;
2939 ifp
->prefered_lft
= prefered_lft
;
2941 spin_unlock_bh(&ifp
->lock
);
2942 if (!(ifp
->flags
&IFA_F_TENTATIVE
))
2943 ipv6_ifa_notify(0, ifp
);
2951 inet6_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
2953 struct ifaddrmsg
*ifm
;
2954 struct nlattr
*tb
[IFA_MAX
+1];
2955 struct in6_addr
*pfx
;
2956 struct inet6_ifaddr
*ifa
;
2957 struct net_device
*dev
;
2958 u32 valid_lft
, preferred_lft
;
2961 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
2965 ifm
= nlmsg_data(nlh
);
2966 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
]);
2970 if (tb
[IFA_CACHEINFO
]) {
2971 struct ifa_cacheinfo
*ci
;
2973 ci
= nla_data(tb
[IFA_CACHEINFO
]);
2974 valid_lft
= ci
->ifa_valid
;
2975 preferred_lft
= ci
->ifa_prefered
;
2977 preferred_lft
= INFINITY_LIFE_TIME
;
2978 valid_lft
= INFINITY_LIFE_TIME
;
2981 dev
= __dev_get_by_index(ifm
->ifa_index
);
2985 ifa
= ipv6_get_ifaddr(pfx
, dev
, 1);
2988 * It would be best to check for !NLM_F_CREATE here but
2989 * userspace alreay relies on not having to provide this.
2991 return inet6_addr_add(ifm
->ifa_index
, pfx
, ifm
->ifa_prefixlen
,
2992 preferred_lft
, valid_lft
);
2995 if (nlh
->nlmsg_flags
& NLM_F_EXCL
||
2996 !(nlh
->nlmsg_flags
& NLM_F_REPLACE
))
2999 err
= inet6_addr_modify(ifa
, preferred_lft
, valid_lft
);
3006 static void put_ifaddrmsg(struct nlmsghdr
*nlh
, u8 prefixlen
, u8 flags
,
3007 u8 scope
, int ifindex
)
3009 struct ifaddrmsg
*ifm
;
3011 ifm
= nlmsg_data(nlh
);
3012 ifm
->ifa_family
= AF_INET6
;
3013 ifm
->ifa_prefixlen
= prefixlen
;
3014 ifm
->ifa_flags
= flags
;
3015 ifm
->ifa_scope
= scope
;
3016 ifm
->ifa_index
= ifindex
;
3019 static int put_cacheinfo(struct sk_buff
*skb
, unsigned long cstamp
,
3020 unsigned long tstamp
, u32 preferred
, u32 valid
)
3022 struct ifa_cacheinfo ci
;
3024 ci
.cstamp
= (u32
)(TIME_DELTA(cstamp
, INITIAL_JIFFIES
) / HZ
* 100
3025 + TIME_DELTA(cstamp
, INITIAL_JIFFIES
) % HZ
* 100 / HZ
);
3026 ci
.tstamp
= (u32
)(TIME_DELTA(tstamp
, INITIAL_JIFFIES
) / HZ
* 100
3027 + TIME_DELTA(tstamp
, INITIAL_JIFFIES
) % HZ
* 100 / HZ
);
3028 ci
.ifa_prefered
= preferred
;
3029 ci
.ifa_valid
= valid
;
3031 return nla_put(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
3034 static inline int rt_scope(int ifa_scope
)
3036 if (ifa_scope
& IFA_HOST
)
3037 return RT_SCOPE_HOST
;
3038 else if (ifa_scope
& IFA_LINK
)
3039 return RT_SCOPE_LINK
;
3040 else if (ifa_scope
& IFA_SITE
)
3041 return RT_SCOPE_SITE
;
3043 return RT_SCOPE_UNIVERSE
;
3046 static inline int inet6_ifaddr_msgsize(void)
3048 return nlmsg_total_size(sizeof(struct ifaddrmsg
) +
3049 nla_total_size(16) +
3050 nla_total_size(sizeof(struct ifa_cacheinfo
)) +
3054 static int inet6_fill_ifaddr(struct sk_buff
*skb
, struct inet6_ifaddr
*ifa
,
3055 u32 pid
, u32 seq
, int event
, unsigned int flags
)
3057 struct nlmsghdr
*nlh
;
3058 u32 preferred
, valid
;
3060 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
3064 put_ifaddrmsg(nlh
, ifa
->prefix_len
, ifa
->flags
, rt_scope(ifa
->scope
),
3065 ifa
->idev
->dev
->ifindex
);
3067 if (!(ifa
->flags
&IFA_F_PERMANENT
)) {
3068 preferred
= ifa
->prefered_lft
;
3069 valid
= ifa
->valid_lft
;
3070 if (preferred
!= INFINITY_LIFE_TIME
) {
3071 long tval
= (jiffies
- ifa
->tstamp
)/HZ
;
3073 if (valid
!= INFINITY_LIFE_TIME
)
3077 preferred
= INFINITY_LIFE_TIME
;
3078 valid
= INFINITY_LIFE_TIME
;
3081 if (nla_put(skb
, IFA_ADDRESS
, 16, &ifa
->addr
) < 0 ||
3082 put_cacheinfo(skb
, ifa
->cstamp
, ifa
->tstamp
, preferred
, valid
) < 0)
3083 return nlmsg_cancel(skb
, nlh
);
3085 return nlmsg_end(skb
, nlh
);
3088 static int inet6_fill_ifmcaddr(struct sk_buff
*skb
, struct ifmcaddr6
*ifmca
,
3089 u32 pid
, u32 seq
, int event
, u16 flags
)
3091 struct nlmsghdr
*nlh
;
3092 u8 scope
= RT_SCOPE_UNIVERSE
;
3093 int ifindex
= ifmca
->idev
->dev
->ifindex
;
3095 if (ipv6_addr_scope(&ifmca
->mca_addr
) & IFA_SITE
)
3096 scope
= RT_SCOPE_SITE
;
3098 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
3102 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
3103 if (nla_put(skb
, IFA_MULTICAST
, 16, &ifmca
->mca_addr
) < 0 ||
3104 put_cacheinfo(skb
, ifmca
->mca_cstamp
, ifmca
->mca_tstamp
,
3105 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0)
3106 return nlmsg_cancel(skb
, nlh
);
3108 return nlmsg_end(skb
, nlh
);
3111 static int inet6_fill_ifacaddr(struct sk_buff
*skb
, struct ifacaddr6
*ifaca
,
3112 u32 pid
, u32 seq
, int event
, unsigned int flags
)
3114 struct nlmsghdr
*nlh
;
3115 u8 scope
= RT_SCOPE_UNIVERSE
;
3116 int ifindex
= ifaca
->aca_idev
->dev
->ifindex
;
3118 if (ipv6_addr_scope(&ifaca
->aca_addr
) & IFA_SITE
)
3119 scope
= RT_SCOPE_SITE
;
3121 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
3125 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
3126 if (nla_put(skb
, IFA_ANYCAST
, 16, &ifaca
->aca_addr
) < 0 ||
3127 put_cacheinfo(skb
, ifaca
->aca_cstamp
, ifaca
->aca_tstamp
,
3128 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0)
3129 return nlmsg_cancel(skb
, nlh
);
3131 return nlmsg_end(skb
, nlh
);
3141 static int inet6_dump_addr(struct sk_buff
*skb
, struct netlink_callback
*cb
,
3142 enum addr_type_t type
)
3145 int s_idx
, s_ip_idx
;
3147 struct net_device
*dev
;
3148 struct inet6_dev
*idev
= NULL
;
3149 struct inet6_ifaddr
*ifa
;
3150 struct ifmcaddr6
*ifmca
;
3151 struct ifacaddr6
*ifaca
;
3153 s_idx
= cb
->args
[0];
3154 s_ip_idx
= ip_idx
= cb
->args
[1];
3155 read_lock(&dev_base_lock
);
3157 for (dev
= dev_base
, idx
= 0; dev
; dev
= dev
->next
, idx
++) {
3163 if ((idev
= in6_dev_get(dev
)) == NULL
)
3165 read_lock_bh(&idev
->lock
);
3168 /* unicast address incl. temp addr */
3169 for (ifa
= idev
->addr_list
; ifa
;
3170 ifa
= ifa
->if_next
, ip_idx
++) {
3171 if (ip_idx
< s_ip_idx
)
3173 if ((err
= inet6_fill_ifaddr(skb
, ifa
,
3174 NETLINK_CB(cb
->skb
).pid
,
3175 cb
->nlh
->nlmsg_seq
, RTM_NEWADDR
,
3180 case MULTICAST_ADDR
:
3181 /* multicast address */
3182 for (ifmca
= idev
->mc_list
; ifmca
;
3183 ifmca
= ifmca
->next
, ip_idx
++) {
3184 if (ip_idx
< s_ip_idx
)
3186 if ((err
= inet6_fill_ifmcaddr(skb
, ifmca
,
3187 NETLINK_CB(cb
->skb
).pid
,
3188 cb
->nlh
->nlmsg_seq
, RTM_GETMULTICAST
,
3194 /* anycast address */
3195 for (ifaca
= idev
->ac_list
; ifaca
;
3196 ifaca
= ifaca
->aca_next
, ip_idx
++) {
3197 if (ip_idx
< s_ip_idx
)
3199 if ((err
= inet6_fill_ifacaddr(skb
, ifaca
,
3200 NETLINK_CB(cb
->skb
).pid
,
3201 cb
->nlh
->nlmsg_seq
, RTM_GETANYCAST
,
3209 read_unlock_bh(&idev
->lock
);
3214 read_unlock_bh(&idev
->lock
);
3217 read_unlock(&dev_base_lock
);
3219 cb
->args
[1] = ip_idx
;
3223 static int inet6_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3225 enum addr_type_t type
= UNICAST_ADDR
;
3226 return inet6_dump_addr(skb
, cb
, type
);
3229 static int inet6_dump_ifmcaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3231 enum addr_type_t type
= MULTICAST_ADDR
;
3232 return inet6_dump_addr(skb
, cb
, type
);
3236 static int inet6_dump_ifacaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3238 enum addr_type_t type
= ANYCAST_ADDR
;
3239 return inet6_dump_addr(skb
, cb
, type
);
3242 static int inet6_rtm_getaddr(struct sk_buff
*in_skb
, struct nlmsghdr
* nlh
,
3245 struct ifaddrmsg
*ifm
;
3246 struct nlattr
*tb
[IFA_MAX
+1];
3247 struct in6_addr
*addr
= NULL
;
3248 struct net_device
*dev
= NULL
;
3249 struct inet6_ifaddr
*ifa
;
3250 struct sk_buff
*skb
;
3253 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
3257 addr
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
]);
3263 ifm
= nlmsg_data(nlh
);
3265 dev
= __dev_get_by_index(ifm
->ifa_index
);
3267 if ((ifa
= ipv6_get_ifaddr(addr
, dev
, 1)) == NULL
) {
3268 err
= -EADDRNOTAVAIL
;
3272 if ((skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL
)) == NULL
) {
3277 err
= inet6_fill_ifaddr(skb
, ifa
, NETLINK_CB(in_skb
).pid
,
3278 nlh
->nlmsg_seq
, RTM_NEWADDR
, 0);
3284 err
= rtnl_unicast(skb
, NETLINK_CB(in_skb
).pid
);
3291 static void inet6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
)
3293 struct sk_buff
*skb
;
3296 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC
);
3300 err
= inet6_fill_ifaddr(skb
, ifa
, 0, 0, event
, 0);
3306 err
= rtnl_notify(skb
, 0, RTNLGRP_IPV6_IFADDR
, NULL
, GFP_ATOMIC
);
3309 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR
, err
);
3312 static void inline ipv6_store_devconf(struct ipv6_devconf
*cnf
,
3313 __s32
*array
, int bytes
)
3315 memset(array
, 0, bytes
);
3316 array
[DEVCONF_FORWARDING
] = cnf
->forwarding
;
3317 array
[DEVCONF_HOPLIMIT
] = cnf
->hop_limit
;
3318 array
[DEVCONF_MTU6
] = cnf
->mtu6
;
3319 array
[DEVCONF_ACCEPT_RA
] = cnf
->accept_ra
;
3320 array
[DEVCONF_ACCEPT_REDIRECTS
] = cnf
->accept_redirects
;
3321 array
[DEVCONF_AUTOCONF
] = cnf
->autoconf
;
3322 array
[DEVCONF_DAD_TRANSMITS
] = cnf
->dad_transmits
;
3323 array
[DEVCONF_RTR_SOLICITS
] = cnf
->rtr_solicits
;
3324 array
[DEVCONF_RTR_SOLICIT_INTERVAL
] = cnf
->rtr_solicit_interval
;
3325 array
[DEVCONF_RTR_SOLICIT_DELAY
] = cnf
->rtr_solicit_delay
;
3326 array
[DEVCONF_FORCE_MLD_VERSION
] = cnf
->force_mld_version
;
3327 #ifdef CONFIG_IPV6_PRIVACY
3328 array
[DEVCONF_USE_TEMPADDR
] = cnf
->use_tempaddr
;
3329 array
[DEVCONF_TEMP_VALID_LFT
] = cnf
->temp_valid_lft
;
3330 array
[DEVCONF_TEMP_PREFERED_LFT
] = cnf
->temp_prefered_lft
;
3331 array
[DEVCONF_REGEN_MAX_RETRY
] = cnf
->regen_max_retry
;
3332 array
[DEVCONF_MAX_DESYNC_FACTOR
] = cnf
->max_desync_factor
;
3334 array
[DEVCONF_MAX_ADDRESSES
] = cnf
->max_addresses
;
3335 array
[DEVCONF_ACCEPT_RA_DEFRTR
] = cnf
->accept_ra_defrtr
;
3336 array
[DEVCONF_ACCEPT_RA_PINFO
] = cnf
->accept_ra_pinfo
;
3337 #ifdef CONFIG_IPV6_ROUTER_PREF
3338 array
[DEVCONF_ACCEPT_RA_RTR_PREF
] = cnf
->accept_ra_rtr_pref
;
3339 array
[DEVCONF_RTR_PROBE_INTERVAL
] = cnf
->rtr_probe_interval
;
3340 #ifdef CONFIV_IPV6_ROUTE_INFO
3341 array
[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN
] = cnf
->accept_ra_rt_info_max_plen
;
3344 array
[DEVCONF_PROXY_NDP
] = cnf
->proxy_ndp
;
3347 /* Maximum length of ifinfomsg attributes */
3348 #define INET6_IFINFO_RTA_SPACE \
3349 RTA_SPACE(IFNAMSIZ) /* IFNAME */ + \
3350 RTA_SPACE(MAX_ADDR_LEN) /* ADDRESS */ + \
3351 RTA_SPACE(sizeof(u32)) /* MTU */ + \
3352 RTA_SPACE(sizeof(int)) /* LINK */ + \
3353 RTA_SPACE(0) /* PROTINFO */ + \
3354 RTA_SPACE(sizeof(u32)) /* FLAGS */ + \
3355 RTA_SPACE(sizeof(struct ifla_cacheinfo)) /* CACHEINFO */ + \
3356 RTA_SPACE(sizeof(__s32[DEVCONF_MAX])) /* CONF */
3358 static int inet6_fill_ifinfo(struct sk_buff
*skb
, struct inet6_dev
*idev
,
3359 u32 pid
, u32 seq
, int event
, unsigned int flags
)
3361 struct net_device
*dev
= idev
->dev
;
3362 __s32
*array
= NULL
;
3363 struct ifinfomsg
*r
;
3364 struct nlmsghdr
*nlh
;
3365 unsigned char *b
= skb
->tail
;
3366 struct rtattr
*subattr
;
3367 __u32 mtu
= dev
->mtu
;
3368 struct ifla_cacheinfo ci
;
3370 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*r
), flags
);
3371 r
= NLMSG_DATA(nlh
);
3372 r
->ifi_family
= AF_INET6
;
3374 r
->ifi_type
= dev
->type
;
3375 r
->ifi_index
= dev
->ifindex
;
3376 r
->ifi_flags
= dev_get_flags(dev
);
3379 RTA_PUT(skb
, IFLA_IFNAME
, strlen(dev
->name
)+1, dev
->name
);
3382 RTA_PUT(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
);
3384 RTA_PUT(skb
, IFLA_MTU
, sizeof(mtu
), &mtu
);
3385 if (dev
->ifindex
!= dev
->iflink
)
3386 RTA_PUT(skb
, IFLA_LINK
, sizeof(int), &dev
->iflink
);
3388 subattr
= (struct rtattr
*)skb
->tail
;
3390 RTA_PUT(skb
, IFLA_PROTINFO
, 0, NULL
);
3392 /* return the device flags */
3393 RTA_PUT(skb
, IFLA_INET6_FLAGS
, sizeof(__u32
), &idev
->if_flags
);
3395 /* return interface cacheinfo */
3396 ci
.max_reasm_len
= IPV6_MAXPLEN
;
3397 ci
.tstamp
= (__u32
)(TIME_DELTA(idev
->tstamp
, INITIAL_JIFFIES
) / HZ
* 100
3398 + TIME_DELTA(idev
->tstamp
, INITIAL_JIFFIES
) % HZ
* 100 / HZ
);
3399 ci
.reachable_time
= idev
->nd_parms
->reachable_time
;
3400 ci
.retrans_time
= idev
->nd_parms
->retrans_time
;
3401 RTA_PUT(skb
, IFLA_INET6_CACHEINFO
, sizeof(ci
), &ci
);
3403 /* return the device sysctl params */
3404 if ((array
= kmalloc(DEVCONF_MAX
* sizeof(*array
), GFP_ATOMIC
)) == NULL
)
3405 goto rtattr_failure
;
3406 ipv6_store_devconf(&idev
->cnf
, array
, DEVCONF_MAX
* sizeof(*array
));
3407 RTA_PUT(skb
, IFLA_INET6_CONF
, DEVCONF_MAX
* sizeof(*array
), array
);
3409 /* XXX - Statistics/MC not implemented */
3410 subattr
->rta_len
= skb
->tail
- (u8
*)subattr
;
3412 nlh
->nlmsg_len
= skb
->tail
- b
;
3419 skb_trim(skb
, b
- skb
->data
);
3423 static int inet6_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3426 int s_idx
= cb
->args
[0];
3427 struct net_device
*dev
;
3428 struct inet6_dev
*idev
;
3430 read_lock(&dev_base_lock
);
3431 for (dev
=dev_base
, idx
=0; dev
; dev
= dev
->next
, idx
++) {
3434 if ((idev
= in6_dev_get(dev
)) == NULL
)
3436 err
= inet6_fill_ifinfo(skb
, idev
, NETLINK_CB(cb
->skb
).pid
,
3437 cb
->nlh
->nlmsg_seq
, RTM_NEWLINK
, NLM_F_MULTI
);
3442 read_unlock(&dev_base_lock
);
3448 void inet6_ifinfo_notify(int event
, struct inet6_dev
*idev
)
3450 struct sk_buff
*skb
;
3451 int payload
= sizeof(struct ifinfomsg
) + INET6_IFINFO_RTA_SPACE
;
3454 skb
= nlmsg_new(nlmsg_total_size(payload
), GFP_ATOMIC
);
3458 err
= inet6_fill_ifinfo(skb
, idev
, 0, 0, event
, 0);
3464 err
= rtnl_notify(skb
, 0, RTNLGRP_IPV6_IFADDR
, NULL
, GFP_ATOMIC
);
3467 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR
, err
);
3470 /* Maximum length of prefix_cacheinfo attributes */
3471 #define INET6_PREFIX_RTA_SPACE \
3472 RTA_SPACE(sizeof(((struct prefix_info *)NULL)->prefix)) /* ADDRESS */ + \
3473 RTA_SPACE(sizeof(struct prefix_cacheinfo)) /* CACHEINFO */
3475 static int inet6_fill_prefix(struct sk_buff
*skb
, struct inet6_dev
*idev
,
3476 struct prefix_info
*pinfo
, u32 pid
, u32 seq
,
3477 int event
, unsigned int flags
)
3479 struct prefixmsg
*pmsg
;
3480 struct nlmsghdr
*nlh
;
3481 unsigned char *b
= skb
->tail
;
3482 struct prefix_cacheinfo ci
;
3484 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*pmsg
), flags
);
3485 pmsg
= NLMSG_DATA(nlh
);
3486 pmsg
->prefix_family
= AF_INET6
;
3487 pmsg
->prefix_pad1
= 0;
3488 pmsg
->prefix_pad2
= 0;
3489 pmsg
->prefix_ifindex
= idev
->dev
->ifindex
;
3490 pmsg
->prefix_len
= pinfo
->prefix_len
;
3491 pmsg
->prefix_type
= pinfo
->type
;
3492 pmsg
->prefix_pad3
= 0;
3494 pmsg
->prefix_flags
= 0;
3496 pmsg
->prefix_flags
|= IF_PREFIX_ONLINK
;
3497 if (pinfo
->autoconf
)
3498 pmsg
->prefix_flags
|= IF_PREFIX_AUTOCONF
;
3500 RTA_PUT(skb
, PREFIX_ADDRESS
, sizeof(pinfo
->prefix
), &pinfo
->prefix
);
3502 ci
.preferred_time
= ntohl(pinfo
->prefered
);
3503 ci
.valid_time
= ntohl(pinfo
->valid
);
3504 RTA_PUT(skb
, PREFIX_CACHEINFO
, sizeof(ci
), &ci
);
3506 nlh
->nlmsg_len
= skb
->tail
- b
;
3511 skb_trim(skb
, b
- skb
->data
);
3515 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
3516 struct prefix_info
*pinfo
)
3518 struct sk_buff
*skb
;
3519 int payload
= sizeof(struct prefixmsg
) + INET6_PREFIX_RTA_SPACE
;
3522 skb
= nlmsg_new(nlmsg_total_size(payload
), GFP_ATOMIC
);
3526 err
= inet6_fill_prefix(skb
, idev
, pinfo
, 0, 0, event
, 0);
3532 err
= rtnl_notify(skb
, 0, RTNLGRP_IPV6_PREFIX
, NULL
, GFP_ATOMIC
);
3535 rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX
, err
);
3538 static struct rtnetlink_link inet6_rtnetlink_table
[RTM_NR_MSGTYPES
] = {
3539 [RTM_GETLINK
- RTM_BASE
] = { .dumpit
= inet6_dump_ifinfo
, },
3540 [RTM_NEWADDR
- RTM_BASE
] = { .doit
= inet6_rtm_newaddr
, },
3541 [RTM_DELADDR
- RTM_BASE
] = { .doit
= inet6_rtm_deladdr
, },
3542 [RTM_GETADDR
- RTM_BASE
] = { .doit
= inet6_rtm_getaddr
,
3543 .dumpit
= inet6_dump_ifaddr
, },
3544 [RTM_GETMULTICAST
- RTM_BASE
] = { .dumpit
= inet6_dump_ifmcaddr
, },
3545 [RTM_GETANYCAST
- RTM_BASE
] = { .dumpit
= inet6_dump_ifacaddr
, },
3546 [RTM_NEWROUTE
- RTM_BASE
] = { .doit
= inet6_rtm_newroute
, },
3547 [RTM_DELROUTE
- RTM_BASE
] = { .doit
= inet6_rtm_delroute
, },
3548 [RTM_GETROUTE
- RTM_BASE
] = { .doit
= inet6_rtm_getroute
,
3549 .dumpit
= inet6_dump_fib
, },
3550 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3551 [RTM_GETRULE
- RTM_BASE
] = { .dumpit
= fib6_rules_dump
, },
3555 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
3557 inet6_ifa_notify(event
? : RTM_NEWADDR
, ifp
);
3561 ip6_ins_rt(ifp
->rt
);
3562 if (ifp
->idev
->cnf
.forwarding
)
3563 addrconf_join_anycast(ifp
);
3566 if (ifp
->idev
->cnf
.forwarding
)
3567 addrconf_leave_anycast(ifp
);
3568 addrconf_leave_solict(ifp
->idev
, &ifp
->addr
);
3569 dst_hold(&ifp
->rt
->u
.dst
);
3570 if (ip6_del_rt(ifp
->rt
))
3571 dst_free(&ifp
->rt
->u
.dst
);
3576 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
3579 if (likely(ifp
->idev
->dead
== 0))
3580 __ipv6_ifa_notify(event
, ifp
);
3581 rcu_read_unlock_bh();
3584 #ifdef CONFIG_SYSCTL
3587 int addrconf_sysctl_forward(ctl_table
*ctl
, int write
, struct file
* filp
,
3588 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
3590 int *valp
= ctl
->data
;
3594 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
3596 if (write
&& valp
!= &ipv6_devconf_dflt
.forwarding
) {
3597 if (valp
!= &ipv6_devconf
.forwarding
) {
3598 if ((!*valp
) ^ (!val
)) {
3599 struct inet6_dev
*idev
= (struct inet6_dev
*)ctl
->extra1
;
3602 dev_forward_change(idev
);
3605 ipv6_devconf_dflt
.forwarding
= ipv6_devconf
.forwarding
;
3606 addrconf_forward_change();
3609 rt6_purge_dflt_routers();
3615 static int addrconf_sysctl_forward_strategy(ctl_table
*table
,
3616 int __user
*name
, int nlen
,
3617 void __user
*oldval
,
3618 size_t __user
*oldlenp
,
3619 void __user
*newval
, size_t newlen
,
3622 int *valp
= table
->data
;
3625 if (!newval
|| !newlen
)
3627 if (newlen
!= sizeof(int))
3629 if (get_user(new, (int __user
*)newval
))
3633 if (oldval
&& oldlenp
) {
3635 if (get_user(len
, oldlenp
))
3638 if (len
> table
->maxlen
)
3639 len
= table
->maxlen
;
3640 if (copy_to_user(oldval
, valp
, len
))
3642 if (put_user(len
, oldlenp
))
3647 if (valp
!= &ipv6_devconf_dflt
.forwarding
) {
3648 if (valp
!= &ipv6_devconf
.forwarding
) {
3649 struct inet6_dev
*idev
= (struct inet6_dev
*)table
->extra1
;
3651 if (unlikely(idev
== NULL
))
3653 changed
= (!*valp
) ^ (!new);
3656 dev_forward_change(idev
);
3659 addrconf_forward_change();
3663 rt6_purge_dflt_routers();
3670 static struct addrconf_sysctl_table
3672 struct ctl_table_header
*sysctl_header
;
3673 ctl_table addrconf_vars
[__NET_IPV6_MAX
];
3674 ctl_table addrconf_dev
[2];
3675 ctl_table addrconf_conf_dir
[2];
3676 ctl_table addrconf_proto_dir
[2];
3677 ctl_table addrconf_root_dir
[2];
3678 } addrconf_sysctl __read_mostly
= {
3679 .sysctl_header
= NULL
,
3682 .ctl_name
= NET_IPV6_FORWARDING
,
3683 .procname
= "forwarding",
3684 .data
= &ipv6_devconf
.forwarding
,
3685 .maxlen
= sizeof(int),
3687 .proc_handler
= &addrconf_sysctl_forward
,
3688 .strategy
= &addrconf_sysctl_forward_strategy
,
3691 .ctl_name
= NET_IPV6_HOP_LIMIT
,
3692 .procname
= "hop_limit",
3693 .data
= &ipv6_devconf
.hop_limit
,
3694 .maxlen
= sizeof(int),
3696 .proc_handler
= proc_dointvec
,
3699 .ctl_name
= NET_IPV6_MTU
,
3701 .data
= &ipv6_devconf
.mtu6
,
3702 .maxlen
= sizeof(int),
3704 .proc_handler
= &proc_dointvec
,
3707 .ctl_name
= NET_IPV6_ACCEPT_RA
,
3708 .procname
= "accept_ra",
3709 .data
= &ipv6_devconf
.accept_ra
,
3710 .maxlen
= sizeof(int),
3712 .proc_handler
= &proc_dointvec
,
3715 .ctl_name
= NET_IPV6_ACCEPT_REDIRECTS
,
3716 .procname
= "accept_redirects",
3717 .data
= &ipv6_devconf
.accept_redirects
,
3718 .maxlen
= sizeof(int),
3720 .proc_handler
= &proc_dointvec
,
3723 .ctl_name
= NET_IPV6_AUTOCONF
,
3724 .procname
= "autoconf",
3725 .data
= &ipv6_devconf
.autoconf
,
3726 .maxlen
= sizeof(int),
3728 .proc_handler
= &proc_dointvec
,
3731 .ctl_name
= NET_IPV6_DAD_TRANSMITS
,
3732 .procname
= "dad_transmits",
3733 .data
= &ipv6_devconf
.dad_transmits
,
3734 .maxlen
= sizeof(int),
3736 .proc_handler
= &proc_dointvec
,
3739 .ctl_name
= NET_IPV6_RTR_SOLICITS
,
3740 .procname
= "router_solicitations",
3741 .data
= &ipv6_devconf
.rtr_solicits
,
3742 .maxlen
= sizeof(int),
3744 .proc_handler
= &proc_dointvec
,
3747 .ctl_name
= NET_IPV6_RTR_SOLICIT_INTERVAL
,
3748 .procname
= "router_solicitation_interval",
3749 .data
= &ipv6_devconf
.rtr_solicit_interval
,
3750 .maxlen
= sizeof(int),
3752 .proc_handler
= &proc_dointvec_jiffies
,
3753 .strategy
= &sysctl_jiffies
,
3756 .ctl_name
= NET_IPV6_RTR_SOLICIT_DELAY
,
3757 .procname
= "router_solicitation_delay",
3758 .data
= &ipv6_devconf
.rtr_solicit_delay
,
3759 .maxlen
= sizeof(int),
3761 .proc_handler
= &proc_dointvec_jiffies
,
3762 .strategy
= &sysctl_jiffies
,
3765 .ctl_name
= NET_IPV6_FORCE_MLD_VERSION
,
3766 .procname
= "force_mld_version",
3767 .data
= &ipv6_devconf
.force_mld_version
,
3768 .maxlen
= sizeof(int),
3770 .proc_handler
= &proc_dointvec
,
3772 #ifdef CONFIG_IPV6_PRIVACY
3774 .ctl_name
= NET_IPV6_USE_TEMPADDR
,
3775 .procname
= "use_tempaddr",
3776 .data
= &ipv6_devconf
.use_tempaddr
,
3777 .maxlen
= sizeof(int),
3779 .proc_handler
= &proc_dointvec
,
3782 .ctl_name
= NET_IPV6_TEMP_VALID_LFT
,
3783 .procname
= "temp_valid_lft",
3784 .data
= &ipv6_devconf
.temp_valid_lft
,
3785 .maxlen
= sizeof(int),
3787 .proc_handler
= &proc_dointvec
,
3790 .ctl_name
= NET_IPV6_TEMP_PREFERED_LFT
,
3791 .procname
= "temp_prefered_lft",
3792 .data
= &ipv6_devconf
.temp_prefered_lft
,
3793 .maxlen
= sizeof(int),
3795 .proc_handler
= &proc_dointvec
,
3798 .ctl_name
= NET_IPV6_REGEN_MAX_RETRY
,
3799 .procname
= "regen_max_retry",
3800 .data
= &ipv6_devconf
.regen_max_retry
,
3801 .maxlen
= sizeof(int),
3803 .proc_handler
= &proc_dointvec
,
3806 .ctl_name
= NET_IPV6_MAX_DESYNC_FACTOR
,
3807 .procname
= "max_desync_factor",
3808 .data
= &ipv6_devconf
.max_desync_factor
,
3809 .maxlen
= sizeof(int),
3811 .proc_handler
= &proc_dointvec
,
3815 .ctl_name
= NET_IPV6_MAX_ADDRESSES
,
3816 .procname
= "max_addresses",
3817 .data
= &ipv6_devconf
.max_addresses
,
3818 .maxlen
= sizeof(int),
3820 .proc_handler
= &proc_dointvec
,
3823 .ctl_name
= NET_IPV6_ACCEPT_RA_DEFRTR
,
3824 .procname
= "accept_ra_defrtr",
3825 .data
= &ipv6_devconf
.accept_ra_defrtr
,
3826 .maxlen
= sizeof(int),
3828 .proc_handler
= &proc_dointvec
,
3831 .ctl_name
= NET_IPV6_ACCEPT_RA_PINFO
,
3832 .procname
= "accept_ra_pinfo",
3833 .data
= &ipv6_devconf
.accept_ra_pinfo
,
3834 .maxlen
= sizeof(int),
3836 .proc_handler
= &proc_dointvec
,
3838 #ifdef CONFIG_IPV6_ROUTER_PREF
3840 .ctl_name
= NET_IPV6_ACCEPT_RA_RTR_PREF
,
3841 .procname
= "accept_ra_rtr_pref",
3842 .data
= &ipv6_devconf
.accept_ra_rtr_pref
,
3843 .maxlen
= sizeof(int),
3845 .proc_handler
= &proc_dointvec
,
3848 .ctl_name
= NET_IPV6_RTR_PROBE_INTERVAL
,
3849 .procname
= "router_probe_interval",
3850 .data
= &ipv6_devconf
.rtr_probe_interval
,
3851 .maxlen
= sizeof(int),
3853 .proc_handler
= &proc_dointvec_jiffies
,
3854 .strategy
= &sysctl_jiffies
,
3856 #ifdef CONFIV_IPV6_ROUTE_INFO
3858 .ctl_name
= NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN
,
3859 .procname
= "accept_ra_rt_info_max_plen",
3860 .data
= &ipv6_devconf
.accept_ra_rt_info_max_plen
,
3861 .maxlen
= sizeof(int),
3863 .proc_handler
= &proc_dointvec
,
3868 .ctl_name
= NET_IPV6_PROXY_NDP
,
3869 .procname
= "proxy_ndp",
3870 .data
= &ipv6_devconf
.proxy_ndp
,
3871 .maxlen
= sizeof(int),
3873 .proc_handler
= &proc_dointvec
,
3876 .ctl_name
= 0, /* sentinel */
3881 .ctl_name
= NET_PROTO_CONF_ALL
,
3884 .child
= addrconf_sysctl
.addrconf_vars
,
3887 .ctl_name
= 0, /* sentinel */
3890 .addrconf_conf_dir
= {
3892 .ctl_name
= NET_IPV6_CONF
,
3895 .child
= addrconf_sysctl
.addrconf_dev
,
3898 .ctl_name
= 0, /* sentinel */
3901 .addrconf_proto_dir
= {
3903 .ctl_name
= NET_IPV6
,
3906 .child
= addrconf_sysctl
.addrconf_conf_dir
,
3909 .ctl_name
= 0, /* sentinel */
3912 .addrconf_root_dir
= {
3914 .ctl_name
= CTL_NET
,
3917 .child
= addrconf_sysctl
.addrconf_proto_dir
,
3920 .ctl_name
= 0, /* sentinel */
3925 static void addrconf_sysctl_register(struct inet6_dev
*idev
, struct ipv6_devconf
*p
)
3928 struct net_device
*dev
= idev
? idev
->dev
: NULL
;
3929 struct addrconf_sysctl_table
*t
;
3930 char *dev_name
= NULL
;
3932 t
= kmalloc(sizeof(*t
), GFP_KERNEL
);
3935 memcpy(t
, &addrconf_sysctl
, sizeof(*t
));
3936 for (i
=0; t
->addrconf_vars
[i
].data
; i
++) {
3937 t
->addrconf_vars
[i
].data
+= (char*)p
- (char*)&ipv6_devconf
;
3938 t
->addrconf_vars
[i
].de
= NULL
;
3939 t
->addrconf_vars
[i
].extra1
= idev
; /* embedded; no ref */
3942 dev_name
= dev
->name
;
3943 t
->addrconf_dev
[0].ctl_name
= dev
->ifindex
;
3945 dev_name
= "default";
3946 t
->addrconf_dev
[0].ctl_name
= NET_PROTO_CONF_DEFAULT
;
3950 * Make a copy of dev_name, because '.procname' is regarded as const
3951 * by sysctl and we wouldn't want anyone to change it under our feet
3952 * (see SIOCSIFNAME).
3954 dev_name
= kstrdup(dev_name
, GFP_KERNEL
);
3958 t
->addrconf_dev
[0].procname
= dev_name
;
3960 t
->addrconf_dev
[0].child
= t
->addrconf_vars
;
3961 t
->addrconf_dev
[0].de
= NULL
;
3962 t
->addrconf_conf_dir
[0].child
= t
->addrconf_dev
;
3963 t
->addrconf_conf_dir
[0].de
= NULL
;
3964 t
->addrconf_proto_dir
[0].child
= t
->addrconf_conf_dir
;
3965 t
->addrconf_proto_dir
[0].de
= NULL
;
3966 t
->addrconf_root_dir
[0].child
= t
->addrconf_proto_dir
;
3967 t
->addrconf_root_dir
[0].de
= NULL
;
3969 t
->sysctl_header
= register_sysctl_table(t
->addrconf_root_dir
, 0);
3970 if (t
->sysctl_header
== NULL
)
3985 static void addrconf_sysctl_unregister(struct ipv6_devconf
*p
)
3988 struct addrconf_sysctl_table
*t
= p
->sysctl
;
3990 unregister_sysctl_table(t
->sysctl_header
);
3991 kfree(t
->addrconf_dev
[0].procname
);
4003 int register_inet6addr_notifier(struct notifier_block
*nb
)
4005 return atomic_notifier_chain_register(&inet6addr_chain
, nb
);
4008 int unregister_inet6addr_notifier(struct notifier_block
*nb
)
4010 return atomic_notifier_chain_unregister(&inet6addr_chain
,nb
);
4014 * Init / cleanup code
4017 int __init
addrconf_init(void)
4021 /* The addrconf netdev notifier requires that loopback_dev
4022 * has it's ipv6 private information allocated and setup
4023 * before it can bring up and give link-local addresses
4024 * to other devices which are up.
4026 * Unfortunately, loopback_dev is not necessarily the first
4027 * entry in the global dev_base list of net devices. In fact,
4028 * it is likely to be the very last entry on that list.
4029 * So this causes the notifier registry below to try and
4030 * give link-local addresses to all devices besides loopback_dev
4031 * first, then loopback_dev, which cases all the non-loopback_dev
4032 * devices to fail to get a link-local address.
4034 * So, as a temporary fix, allocate the ipv6 structure for
4035 * loopback_dev first by hand.
4036 * Longer term, all of the dependencies ipv6 has upon the loopback
4037 * device and it being up should be removed.
4040 if (!ipv6_add_dev(&loopback_dev
))
4046 ip6_null_entry
.rt6i_idev
= in6_dev_get(&loopback_dev
);
4048 register_netdevice_notifier(&ipv6_dev_notf
);
4051 rtnetlink_links
[PF_INET6
] = inet6_rtnetlink_table
;
4052 #ifdef CONFIG_SYSCTL
4053 addrconf_sysctl
.sysctl_header
=
4054 register_sysctl_table(addrconf_sysctl
.addrconf_root_dir
, 0);
4055 addrconf_sysctl_register(NULL
, &ipv6_devconf_dflt
);
4061 void __exit
addrconf_cleanup(void)
4063 struct net_device
*dev
;
4064 struct inet6_dev
*idev
;
4065 struct inet6_ifaddr
*ifa
;
4068 unregister_netdevice_notifier(&ipv6_dev_notf
);
4070 rtnetlink_links
[PF_INET6
] = NULL
;
4071 #ifdef CONFIG_SYSCTL
4072 addrconf_sysctl_unregister(&ipv6_devconf_dflt
);
4073 addrconf_sysctl_unregister(&ipv6_devconf
);
4082 for (dev
=dev_base
; dev
; dev
=dev
->next
) {
4083 if ((idev
= __in6_dev_get(dev
)) == NULL
)
4085 addrconf_ifdown(dev
, 1);
4087 addrconf_ifdown(&loopback_dev
, 2);
4093 write_lock_bh(&addrconf_hash_lock
);
4094 for (i
=0; i
< IN6_ADDR_HSIZE
; i
++) {
4095 for (ifa
=inet6_addr_lst
[i
]; ifa
; ) {
4096 struct inet6_ifaddr
*bifa
;
4099 ifa
= ifa
->lst_next
;
4100 printk(KERN_DEBUG
"bug: IPv6 address leakage detected: ifa=%p\n", bifa
);
4101 /* Do not free it; something is wrong.
4102 Now we can investigate it with debugger.
4106 write_unlock_bh(&addrconf_hash_lock
);
4108 del_timer(&addr_chk_timer
);
4112 #ifdef CONFIG_PROC_FS
4113 proc_net_remove("if_inet6");