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 <linux/if_tunnel.h>
77 #include <linux/rtnetlink.h>
79 #ifdef CONFIG_IPV6_PRIVACY
80 #include <linux/random.h>
83 #include <asm/uaccess.h>
85 #include <linux/proc_fs.h>
86 #include <linux/seq_file.h>
88 /* Set to 3 to get tracing... */
92 #define ADBG(x) printk x
97 #define INFINITY_LIFE_TIME 0xFFFFFFFF
98 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
101 static void addrconf_sysctl_register(struct inet6_dev
*idev
, struct ipv6_devconf
*p
);
102 static void addrconf_sysctl_unregister(struct ipv6_devconf
*p
);
105 #ifdef CONFIG_IPV6_PRIVACY
106 static int __ipv6_regen_rndid(struct inet6_dev
*idev
);
107 static int __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
);
108 static void ipv6_regen_rndid(unsigned long data
);
110 static int desync_factor
= MAX_DESYNC_FACTOR
* HZ
;
113 static int ipv6_count_addresses(struct inet6_dev
*idev
);
116 * Configured unicast address hash table
118 static struct inet6_ifaddr
*inet6_addr_lst
[IN6_ADDR_HSIZE
];
119 static DEFINE_RWLOCK(addrconf_hash_lock
);
121 /* Protects inet6 devices */
122 DEFINE_RWLOCK(addrconf_lock
);
124 static void addrconf_verify(unsigned long);
126 static DEFINE_TIMER(addr_chk_timer
, addrconf_verify
, 0, 0);
127 static DEFINE_SPINLOCK(addrconf_verify_lock
);
129 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
);
130 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
);
132 static int addrconf_ifdown(struct net_device
*dev
, int how
);
134 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
, u32 flags
);
135 static void addrconf_dad_timer(unsigned long data
);
136 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
);
137 static void addrconf_dad_run(struct inet6_dev
*idev
);
138 static void addrconf_rs_timer(unsigned long data
);
139 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
140 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
142 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
143 struct prefix_info
*pinfo
);
144 static int ipv6_chk_same_addr(const struct in6_addr
*addr
, struct net_device
*dev
);
146 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain
);
148 struct ipv6_devconf ipv6_devconf
= {
150 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
151 .mtu6
= IPV6_MIN_MTU
,
153 .accept_redirects
= 1,
155 .force_mld_version
= 0,
157 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
158 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
159 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
160 #ifdef CONFIG_IPV6_PRIVACY
162 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
163 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
164 .regen_max_retry
= REGEN_MAX_RETRY
,
165 .max_desync_factor
= MAX_DESYNC_FACTOR
,
167 .max_addresses
= IPV6_MAX_ADDRESSES
,
168 .accept_ra_defrtr
= 1,
169 .accept_ra_pinfo
= 1,
170 #ifdef CONFIG_IPV6_ROUTER_PREF
171 .accept_ra_rtr_pref
= 1,
172 .rtr_probe_interval
= 60 * HZ
,
173 #ifdef CONFIG_IPV6_ROUTE_INFO
174 .accept_ra_rt_info_max_plen
= 0,
179 static struct ipv6_devconf ipv6_devconf_dflt
= {
181 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
182 .mtu6
= IPV6_MIN_MTU
,
184 .accept_redirects
= 1,
187 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
188 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
189 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
190 #ifdef CONFIG_IPV6_PRIVACY
192 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
193 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
194 .regen_max_retry
= REGEN_MAX_RETRY
,
195 .max_desync_factor
= MAX_DESYNC_FACTOR
,
197 .max_addresses
= IPV6_MAX_ADDRESSES
,
198 .accept_ra_defrtr
= 1,
199 .accept_ra_pinfo
= 1,
200 #ifdef CONFIG_IPV6_ROUTER_PREF
201 .accept_ra_rtr_pref
= 1,
202 .rtr_probe_interval
= 60 * HZ
,
203 #ifdef CONFIG_IPV6_ROUTE_INFO
204 .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 void in6_dev_finish_destroy(struct inet6_dev
*idev
)
320 struct net_device
*dev
= idev
->dev
;
321 BUG_TRAP(idev
->addr_list
==NULL
);
322 BUG_TRAP(idev
->mc_list
==NULL
);
323 #ifdef NET_REFCNT_DEBUG
324 printk(KERN_DEBUG
"in6_dev_finish_destroy: %s\n", dev
? dev
->name
: "NIL");
328 printk("Freeing alive inet6 device %p\n", idev
);
331 snmp6_free_dev(idev
);
335 static struct inet6_dev
* ipv6_add_dev(struct net_device
*dev
)
337 struct inet6_dev
*ndev
;
341 if (dev
->mtu
< IPV6_MIN_MTU
)
344 ndev
= kzalloc(sizeof(struct inet6_dev
), GFP_KERNEL
);
349 rwlock_init(&ndev
->lock
);
351 memcpy(&ndev
->cnf
, &ipv6_devconf_dflt
, sizeof(ndev
->cnf
));
352 ndev
->cnf
.mtu6
= dev
->mtu
;
353 ndev
->cnf
.sysctl
= NULL
;
354 ndev
->nd_parms
= neigh_parms_alloc(dev
, &nd_tbl
);
355 if (ndev
->nd_parms
== NULL
) {
359 /* We refer to the device */
362 if (snmp6_alloc_dev(ndev
) < 0) {
364 "%s(): cannot allocate memory for statistics; dev=%s.\n",
365 __FUNCTION__
, dev
->name
));
366 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
368 in6_dev_finish_destroy(ndev
);
372 if (snmp6_register_dev(ndev
) < 0) {
374 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
375 __FUNCTION__
, dev
->name
));
376 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
378 in6_dev_finish_destroy(ndev
);
382 /* One reference from device. We must do this before
383 * we invoke __ipv6_regen_rndid().
387 #ifdef CONFIG_IPV6_PRIVACY
388 init_timer(&ndev
->regen_timer
);
389 ndev
->regen_timer
.function
= ipv6_regen_rndid
;
390 ndev
->regen_timer
.data
= (unsigned long) ndev
;
391 if ((dev
->flags
&IFF_LOOPBACK
) ||
392 dev
->type
== ARPHRD_TUNNEL
||
393 dev
->type
== ARPHRD_NONE
||
394 dev
->type
== ARPHRD_SIT
) {
396 "%s: Disabled Privacy Extensions\n",
398 ndev
->cnf
.use_tempaddr
= -1;
401 ipv6_regen_rndid((unsigned long) ndev
);
405 if (netif_carrier_ok(dev
))
406 ndev
->if_flags
|= IF_READY
;
408 write_lock_bh(&addrconf_lock
);
410 write_unlock_bh(&addrconf_lock
);
412 ipv6_mc_init_dev(ndev
);
413 ndev
->tstamp
= jiffies
;
415 neigh_sysctl_register(dev
, ndev
->nd_parms
, NET_IPV6
,
416 NET_IPV6_NEIGH
, "ipv6",
417 &ndisc_ifinfo_sysctl_change
,
419 addrconf_sysctl_register(ndev
, &ndev
->cnf
);
424 static struct inet6_dev
* ipv6_find_idev(struct net_device
*dev
)
426 struct inet6_dev
*idev
;
430 if ((idev
= __in6_dev_get(dev
)) == NULL
) {
431 if ((idev
= ipv6_add_dev(dev
)) == NULL
)
435 if (dev
->flags
&IFF_UP
)
441 static void dev_forward_change(struct inet6_dev
*idev
)
443 struct net_device
*dev
;
444 struct inet6_ifaddr
*ifa
;
445 struct in6_addr addr
;
450 if (dev
&& (dev
->flags
& IFF_MULTICAST
)) {
451 ipv6_addr_all_routers(&addr
);
453 if (idev
->cnf
.forwarding
)
454 ipv6_dev_mc_inc(dev
, &addr
);
456 ipv6_dev_mc_dec(dev
, &addr
);
458 for (ifa
=idev
->addr_list
; ifa
; ifa
=ifa
->if_next
) {
459 if (idev
->cnf
.forwarding
)
460 addrconf_join_anycast(ifa
);
462 addrconf_leave_anycast(ifa
);
467 static void addrconf_forward_change(void)
469 struct net_device
*dev
;
470 struct inet6_dev
*idev
;
472 read_lock(&dev_base_lock
);
473 for (dev
=dev_base
; dev
; dev
=dev
->next
) {
474 read_lock(&addrconf_lock
);
475 idev
= __in6_dev_get(dev
);
477 int changed
= (!idev
->cnf
.forwarding
) ^ (!ipv6_devconf
.forwarding
);
478 idev
->cnf
.forwarding
= ipv6_devconf
.forwarding
;
480 dev_forward_change(idev
);
482 read_unlock(&addrconf_lock
);
484 read_unlock(&dev_base_lock
);
488 /* Nobody refers to this ifaddr, destroy it */
490 void inet6_ifa_finish_destroy(struct inet6_ifaddr
*ifp
)
492 BUG_TRAP(ifp
->if_next
==NULL
);
493 BUG_TRAP(ifp
->lst_next
==NULL
);
494 #ifdef NET_REFCNT_DEBUG
495 printk(KERN_DEBUG
"inet6_ifa_finish_destroy\n");
498 in6_dev_put(ifp
->idev
);
500 if (del_timer(&ifp
->timer
))
501 printk("Timer is still running, when freeing ifa=%p\n", ifp
);
504 printk("Freeing alive inet6 address %p\n", ifp
);
507 dst_release(&ifp
->rt
->u
.dst
);
513 ipv6_link_dev_addr(struct inet6_dev
*idev
, struct inet6_ifaddr
*ifp
)
515 struct inet6_ifaddr
*ifa
, **ifap
;
516 int ifp_scope
= ipv6_addr_src_scope(&ifp
->addr
);
519 * Each device address list is sorted in order of scope -
520 * global before linklocal.
522 for (ifap
= &idev
->addr_list
; (ifa
= *ifap
) != NULL
;
523 ifap
= &ifa
->if_next
) {
524 if (ifp_scope
>= ipv6_addr_src_scope(&ifa
->addr
))
528 ifp
->if_next
= *ifap
;
532 /* On success it returns ifp with increased reference count */
534 static struct inet6_ifaddr
*
535 ipv6_add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
, int pfxlen
,
536 int scope
, u32 flags
)
538 struct inet6_ifaddr
*ifa
= NULL
;
543 read_lock_bh(&addrconf_lock
);
545 err
= -ENODEV
; /*XXX*/
549 write_lock(&addrconf_hash_lock
);
551 /* Ignore adding duplicate addresses on an interface */
552 if (ipv6_chk_same_addr(addr
, idev
->dev
)) {
553 ADBG(("ipv6_add_addr: already assigned\n"));
558 ifa
= kzalloc(sizeof(struct inet6_ifaddr
), GFP_ATOMIC
);
561 ADBG(("ipv6_add_addr: malloc failed\n"));
566 rt
= addrconf_dst_alloc(idev
, addr
, 0);
572 ipv6_addr_copy(&ifa
->addr
, addr
);
574 spin_lock_init(&ifa
->lock
);
575 init_timer(&ifa
->timer
);
576 ifa
->timer
.data
= (unsigned long) ifa
;
578 ifa
->prefix_len
= pfxlen
;
579 ifa
->flags
= flags
| IFA_F_TENTATIVE
;
580 ifa
->cstamp
= ifa
->tstamp
= jiffies
;
589 /* Add to big hash table */
590 hash
= ipv6_addr_hash(addr
);
592 ifa
->lst_next
= inet6_addr_lst
[hash
];
593 inet6_addr_lst
[hash
] = ifa
;
595 write_unlock(&addrconf_hash_lock
);
597 write_lock(&idev
->lock
);
598 /* Add to inet6_dev unicast addr list. */
599 ipv6_link_dev_addr(idev
, ifa
);
601 #ifdef CONFIG_IPV6_PRIVACY
602 if (ifa
->flags
&IFA_F_TEMPORARY
) {
603 ifa
->tmp_next
= idev
->tempaddr_list
;
604 idev
->tempaddr_list
= ifa
;
610 write_unlock(&idev
->lock
);
612 read_unlock_bh(&addrconf_lock
);
614 if (likely(err
== 0))
615 atomic_notifier_call_chain(&inet6addr_chain
, NETDEV_UP
, ifa
);
623 write_unlock(&addrconf_hash_lock
);
627 /* This function wants to get referenced ifp and releases it before return */
629 static void ipv6_del_addr(struct inet6_ifaddr
*ifp
)
631 struct inet6_ifaddr
*ifa
, **ifap
;
632 struct inet6_dev
*idev
= ifp
->idev
;
634 int deleted
= 0, onlink
= 0;
635 unsigned long expires
= jiffies
;
637 hash
= ipv6_addr_hash(&ifp
->addr
);
641 write_lock_bh(&addrconf_hash_lock
);
642 for (ifap
= &inet6_addr_lst
[hash
]; (ifa
=*ifap
) != NULL
;
643 ifap
= &ifa
->lst_next
) {
645 *ifap
= ifa
->lst_next
;
647 ifa
->lst_next
= NULL
;
651 write_unlock_bh(&addrconf_hash_lock
);
653 write_lock_bh(&idev
->lock
);
654 #ifdef CONFIG_IPV6_PRIVACY
655 if (ifp
->flags
&IFA_F_TEMPORARY
) {
656 for (ifap
= &idev
->tempaddr_list
; (ifa
=*ifap
) != NULL
;
657 ifap
= &ifa
->tmp_next
) {
659 *ifap
= ifa
->tmp_next
;
661 in6_ifa_put(ifp
->ifpub
);
665 ifa
->tmp_next
= NULL
;
672 for (ifap
= &idev
->addr_list
; (ifa
=*ifap
) != NULL
;) {
674 *ifap
= ifa
->if_next
;
677 if (!(ifp
->flags
& IFA_F_PERMANENT
) || onlink
> 0)
681 } else if (ifp
->flags
& IFA_F_PERMANENT
) {
682 if (ipv6_prefix_equal(&ifa
->addr
, &ifp
->addr
,
684 if (ifa
->flags
& IFA_F_PERMANENT
) {
689 unsigned long lifetime
;
694 spin_lock(&ifa
->lock
);
695 lifetime
= min_t(unsigned long,
696 ifa
->valid_lft
, 0x7fffffffUL
/HZ
);
697 if (time_before(expires
,
698 ifa
->tstamp
+ lifetime
* HZ
))
699 expires
= ifa
->tstamp
+ lifetime
* HZ
;
700 spin_unlock(&ifa
->lock
);
704 ifap
= &ifa
->if_next
;
706 write_unlock_bh(&idev
->lock
);
708 ipv6_ifa_notify(RTM_DELADDR
, ifp
);
710 atomic_notifier_call_chain(&inet6addr_chain
, NETDEV_DOWN
, ifp
);
712 addrconf_del_timer(ifp
);
715 * Purge or update corresponding prefix
717 * 1) we don't purge prefix here if address was not permanent.
718 * prefix is managed by its own lifetime.
719 * 2) if there're no addresses, delete prefix.
720 * 3) if there're still other permanent address(es),
721 * corresponding prefix is still permanent.
722 * 4) otherwise, update prefix lifetime to the
723 * longest valid lifetime among the corresponding
724 * addresses on the device.
725 * Note: subsequent RA will update lifetime.
729 if ((ifp
->flags
& IFA_F_PERMANENT
) && onlink
< 1) {
730 struct in6_addr prefix
;
733 ipv6_addr_prefix(&prefix
, &ifp
->addr
, ifp
->prefix_len
);
734 rt
= rt6_lookup(&prefix
, NULL
, ifp
->idev
->dev
->ifindex
, 1);
736 if (rt
&& ((rt
->rt6i_flags
& (RTF_GATEWAY
| RTF_DEFAULT
)) == 0)) {
738 ip6_del_rt(rt
, NULL
, NULL
, NULL
);
740 } else if (!(rt
->rt6i_flags
& RTF_EXPIRES
)) {
741 rt
->rt6i_expires
= expires
;
742 rt
->rt6i_flags
|= RTF_EXPIRES
;
745 dst_release(&rt
->u
.dst
);
751 #ifdef CONFIG_IPV6_PRIVACY
752 static int ipv6_create_tempaddr(struct inet6_ifaddr
*ifp
, struct inet6_ifaddr
*ift
)
754 struct inet6_dev
*idev
= ifp
->idev
;
755 struct in6_addr addr
, *tmpaddr
;
756 unsigned long tmp_prefered_lft
, tmp_valid_lft
, tmp_cstamp
, tmp_tstamp
;
761 write_lock(&idev
->lock
);
763 spin_lock_bh(&ift
->lock
);
764 memcpy(&addr
.s6_addr
[8], &ift
->addr
.s6_addr
[8], 8);
765 spin_unlock_bh(&ift
->lock
);
772 if (idev
->cnf
.use_tempaddr
<= 0) {
773 write_unlock(&idev
->lock
);
775 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
780 spin_lock_bh(&ifp
->lock
);
781 if (ifp
->regen_count
++ >= idev
->cnf
.regen_max_retry
) {
782 idev
->cnf
.use_tempaddr
= -1; /*XXX*/
783 spin_unlock_bh(&ifp
->lock
);
784 write_unlock(&idev
->lock
);
786 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
792 memcpy(addr
.s6_addr
, ifp
->addr
.s6_addr
, 8);
793 if (__ipv6_try_regen_rndid(idev
, tmpaddr
) < 0) {
794 spin_unlock_bh(&ifp
->lock
);
795 write_unlock(&idev
->lock
);
797 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
803 memcpy(&addr
.s6_addr
[8], idev
->rndid
, 8);
804 tmp_valid_lft
= min_t(__u32
,
806 idev
->cnf
.temp_valid_lft
);
807 tmp_prefered_lft
= min_t(__u32
,
809 idev
->cnf
.temp_prefered_lft
- desync_factor
/ HZ
);
810 tmp_plen
= ifp
->prefix_len
;
811 max_addresses
= idev
->cnf
.max_addresses
;
812 tmp_cstamp
= ifp
->cstamp
;
813 tmp_tstamp
= ifp
->tstamp
;
814 spin_unlock_bh(&ifp
->lock
);
816 write_unlock(&idev
->lock
);
817 ift
= !max_addresses
||
818 ipv6_count_addresses(idev
) < max_addresses
?
819 ipv6_add_addr(idev
, &addr
, tmp_plen
,
820 ipv6_addr_type(&addr
)&IPV6_ADDR_SCOPE_MASK
, IFA_F_TEMPORARY
) : NULL
;
821 if (!ift
|| IS_ERR(ift
)) {
825 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
827 write_lock(&idev
->lock
);
831 spin_lock_bh(&ift
->lock
);
833 ift
->valid_lft
= tmp_valid_lft
;
834 ift
->prefered_lft
= tmp_prefered_lft
;
835 ift
->cstamp
= tmp_cstamp
;
836 ift
->tstamp
= tmp_tstamp
;
837 spin_unlock_bh(&ift
->lock
);
839 addrconf_dad_start(ift
, 0);
848 * Choose an appropriate source address (RFC3484)
850 struct ipv6_saddr_score
{
858 #define IPV6_SADDR_SCORE_LOCAL 0x0001
859 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
860 #define IPV6_SADDR_SCORE_HOA 0x0008
861 #define IPV6_SADDR_SCORE_OIF 0x0010
862 #define IPV6_SADDR_SCORE_LABEL 0x0020
863 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
865 static int inline ipv6_saddr_preferred(int type
)
867 if (type
& (IPV6_ADDR_MAPPED
|IPV6_ADDR_COMPATv4
|
868 IPV6_ADDR_LOOPBACK
|IPV6_ADDR_RESERVED
))
873 /* static matching label */
874 static int inline ipv6_saddr_label(const struct in6_addr
*addr
, int type
)
877 * prefix (longest match) label
878 * -----------------------------
887 if (type
& IPV6_ADDR_LOOPBACK
)
889 else if (type
& IPV6_ADDR_COMPATv4
)
891 else if (type
& IPV6_ADDR_MAPPED
)
893 else if (addr
->s6_addr32
[0] == htonl(0x20010000))
895 else if (addr
->s6_addr16
[0] == htons(0x2002))
897 else if ((addr
->s6_addr
[0] & 0xfe) == 0xfc)
902 int ipv6_dev_get_saddr(struct net_device
*daddr_dev
,
903 struct in6_addr
*daddr
, struct in6_addr
*saddr
)
905 struct ipv6_saddr_score hiscore
;
906 struct inet6_ifaddr
*ifa_result
= NULL
;
907 int daddr_type
= __ipv6_addr_type(daddr
);
908 int daddr_scope
= __ipv6_addr_src_scope(daddr_type
);
909 u32 daddr_label
= ipv6_saddr_label(daddr
, daddr_type
);
910 struct net_device
*dev
;
912 memset(&hiscore
, 0, sizeof(hiscore
));
914 read_lock(&dev_base_lock
);
915 read_lock(&addrconf_lock
);
917 for (dev
= dev_base
; dev
; dev
=dev
->next
) {
918 struct inet6_dev
*idev
;
919 struct inet6_ifaddr
*ifa
;
921 /* Rule 0: Candidate Source Address (section 4)
922 * - multicast and link-local destination address,
923 * the set of candidate source address MUST only
924 * include addresses assigned to interfaces
925 * belonging to the same link as the outgoing
927 * (- For site-local destination addresses, the
928 * set of candidate source addresses MUST only
929 * include addresses assigned to interfaces
930 * belonging to the same site as the outgoing
933 if ((daddr_type
& IPV6_ADDR_MULTICAST
||
934 daddr_scope
<= IPV6_ADDR_SCOPE_LINKLOCAL
) &&
935 daddr_dev
&& dev
!= daddr_dev
)
938 idev
= __in6_dev_get(dev
);
942 read_lock_bh(&idev
->lock
);
943 for (ifa
= idev
->addr_list
; ifa
; ifa
= ifa
->if_next
) {
944 struct ipv6_saddr_score score
;
946 score
.addr_type
= __ipv6_addr_type(&ifa
->addr
);
949 * - Tentative Address (RFC2462 section 5.4)
950 * - A tentative address is not considered
951 * "assigned to an interface" in the traditional
953 * - Candidate Source Address (section 4)
954 * - In any case, anycast addresses, multicast
955 * addresses, and the unspecified address MUST
956 * NOT be included in a candidate set.
958 if (ifa
->flags
& IFA_F_TENTATIVE
)
960 if (unlikely(score
.addr_type
== IPV6_ADDR_ANY
||
961 score
.addr_type
& IPV6_ADDR_MULTICAST
)) {
962 LIMIT_NETDEBUG(KERN_DEBUG
963 "ADDRCONF: unspecified / multicast address"
964 "assigned as unicast address on %s",
974 if (ifa_result
== NULL
) {
975 /* record it if the first available entry */
979 /* Rule 1: Prefer same address */
980 if (hiscore
.rule
< 1) {
981 if (ipv6_addr_equal(&ifa_result
->addr
, daddr
))
982 hiscore
.attrs
|= IPV6_SADDR_SCORE_LOCAL
;
985 if (ipv6_addr_equal(&ifa
->addr
, daddr
)) {
986 score
.attrs
|= IPV6_SADDR_SCORE_LOCAL
;
987 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_LOCAL
)) {
992 if (hiscore
.attrs
& IPV6_SADDR_SCORE_LOCAL
)
996 /* Rule 2: Prefer appropriate scope */
997 if (hiscore
.rule
< 2) {
998 hiscore
.scope
= __ipv6_addr_src_scope(hiscore
.addr_type
);
1001 score
.scope
= __ipv6_addr_src_scope(score
.addr_type
);
1002 if (hiscore
.scope
< score
.scope
) {
1003 if (hiscore
.scope
< daddr_scope
) {
1008 } else if (score
.scope
< hiscore
.scope
) {
1009 if (score
.scope
< daddr_scope
)
1010 break; /* addresses sorted by scope */
1017 /* Rule 3: Avoid deprecated address */
1018 if (hiscore
.rule
< 3) {
1019 if (ipv6_saddr_preferred(hiscore
.addr_type
) ||
1020 !(ifa_result
->flags
& IFA_F_DEPRECATED
))
1021 hiscore
.attrs
|= IPV6_SADDR_SCORE_PREFERRED
;
1024 if (ipv6_saddr_preferred(score
.addr_type
) ||
1025 !(ifa
->flags
& IFA_F_DEPRECATED
)) {
1026 score
.attrs
|= IPV6_SADDR_SCORE_PREFERRED
;
1027 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_PREFERRED
)) {
1032 if (hiscore
.attrs
& IPV6_SADDR_SCORE_PREFERRED
)
1036 /* Rule 4: Prefer home address -- not implemented yet */
1037 if (hiscore
.rule
< 4)
1040 /* Rule 5: Prefer outgoing interface */
1041 if (hiscore
.rule
< 5) {
1042 if (daddr_dev
== NULL
||
1043 daddr_dev
== ifa_result
->idev
->dev
)
1044 hiscore
.attrs
|= IPV6_SADDR_SCORE_OIF
;
1047 if (daddr_dev
== NULL
||
1048 daddr_dev
== ifa
->idev
->dev
) {
1049 score
.attrs
|= IPV6_SADDR_SCORE_OIF
;
1050 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_OIF
)) {
1055 if (hiscore
.attrs
& IPV6_SADDR_SCORE_OIF
)
1059 /* Rule 6: Prefer matching label */
1060 if (hiscore
.rule
< 6) {
1061 if (ipv6_saddr_label(&ifa_result
->addr
, hiscore
.addr_type
) == daddr_label
)
1062 hiscore
.attrs
|= IPV6_SADDR_SCORE_LABEL
;
1065 if (ipv6_saddr_label(&ifa
->addr
, score
.addr_type
) == daddr_label
) {
1066 score
.attrs
|= IPV6_SADDR_SCORE_LABEL
;
1067 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_LABEL
)) {
1072 if (hiscore
.attrs
& IPV6_SADDR_SCORE_LABEL
)
1076 #ifdef CONFIG_IPV6_PRIVACY
1077 /* Rule 7: Prefer public address
1078 * Note: prefer temprary address if use_tempaddr >= 2
1080 if (hiscore
.rule
< 7) {
1081 if ((!(ifa_result
->flags
& IFA_F_TEMPORARY
)) ^
1082 (ifa_result
->idev
->cnf
.use_tempaddr
>= 2))
1083 hiscore
.attrs
|= IPV6_SADDR_SCORE_PRIVACY
;
1086 if ((!(ifa
->flags
& IFA_F_TEMPORARY
)) ^
1087 (ifa
->idev
->cnf
.use_tempaddr
>= 2)) {
1088 score
.attrs
|= IPV6_SADDR_SCORE_PRIVACY
;
1089 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_PRIVACY
)) {
1094 if (hiscore
.attrs
& IPV6_SADDR_SCORE_PRIVACY
)
1098 if (hiscore
.rule
< 7)
1101 /* Rule 8: Use longest matching prefix */
1102 if (hiscore
.rule
< 8) {
1103 hiscore
.matchlen
= ipv6_addr_diff(&ifa_result
->addr
, daddr
);
1106 score
.matchlen
= ipv6_addr_diff(&ifa
->addr
, daddr
);
1107 if (score
.matchlen
> hiscore
.matchlen
) {
1112 else if (score
.matchlen
< hiscore
.matchlen
)
1116 /* Final Rule: choose first available one */
1120 in6_ifa_put(ifa_result
);
1125 read_unlock_bh(&idev
->lock
);
1127 read_unlock(&addrconf_lock
);
1128 read_unlock(&dev_base_lock
);
1131 return -EADDRNOTAVAIL
;
1133 ipv6_addr_copy(saddr
, &ifa_result
->addr
);
1134 in6_ifa_put(ifa_result
);
1139 int ipv6_get_saddr(struct dst_entry
*dst
,
1140 struct in6_addr
*daddr
, struct in6_addr
*saddr
)
1142 return ipv6_dev_get_saddr(dst
? ((struct rt6_info
*)dst
)->rt6i_idev
->dev
: NULL
, daddr
, saddr
);
1146 int ipv6_get_lladdr(struct net_device
*dev
, struct in6_addr
*addr
)
1148 struct inet6_dev
*idev
;
1149 int err
= -EADDRNOTAVAIL
;
1151 read_lock(&addrconf_lock
);
1152 if ((idev
= __in6_dev_get(dev
)) != NULL
) {
1153 struct inet6_ifaddr
*ifp
;
1155 read_lock_bh(&idev
->lock
);
1156 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1157 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1158 ipv6_addr_copy(addr
, &ifp
->addr
);
1163 read_unlock_bh(&idev
->lock
);
1165 read_unlock(&addrconf_lock
);
1169 static int ipv6_count_addresses(struct inet6_dev
*idev
)
1172 struct inet6_ifaddr
*ifp
;
1174 read_lock_bh(&idev
->lock
);
1175 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
)
1177 read_unlock_bh(&idev
->lock
);
1181 int ipv6_chk_addr(struct in6_addr
*addr
, struct net_device
*dev
, int strict
)
1183 struct inet6_ifaddr
* ifp
;
1184 u8 hash
= ipv6_addr_hash(addr
);
1186 read_lock_bh(&addrconf_hash_lock
);
1187 for(ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
=ifp
->lst_next
) {
1188 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
1189 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1190 if (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1191 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
))
1195 read_unlock_bh(&addrconf_hash_lock
);
1200 int ipv6_chk_same_addr(const struct in6_addr
*addr
, struct net_device
*dev
)
1202 struct inet6_ifaddr
* ifp
;
1203 u8 hash
= ipv6_addr_hash(addr
);
1205 for(ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
=ifp
->lst_next
) {
1206 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1207 if (dev
== NULL
|| ifp
->idev
->dev
== dev
)
1214 struct inet6_ifaddr
* ipv6_get_ifaddr(struct in6_addr
*addr
, struct net_device
*dev
, int strict
)
1216 struct inet6_ifaddr
* ifp
;
1217 u8 hash
= ipv6_addr_hash(addr
);
1219 read_lock_bh(&addrconf_hash_lock
);
1220 for(ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
=ifp
->lst_next
) {
1221 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1222 if (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1223 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
)) {
1229 read_unlock_bh(&addrconf_hash_lock
);
1234 int ipv6_rcv_saddr_equal(const struct sock
*sk
, const struct sock
*sk2
)
1236 const struct in6_addr
*sk_rcv_saddr6
= &inet6_sk(sk
)->rcv_saddr
;
1237 const struct in6_addr
*sk2_rcv_saddr6
= inet6_rcv_saddr(sk2
);
1238 u32 sk_rcv_saddr
= inet_sk(sk
)->rcv_saddr
;
1239 u32 sk2_rcv_saddr
= inet_rcv_saddr(sk2
);
1240 int sk_ipv6only
= ipv6_only_sock(sk
);
1241 int sk2_ipv6only
= inet_v6_ipv6only(sk2
);
1242 int addr_type
= ipv6_addr_type(sk_rcv_saddr6
);
1243 int addr_type2
= sk2_rcv_saddr6
? ipv6_addr_type(sk2_rcv_saddr6
) : IPV6_ADDR_MAPPED
;
1245 if (!sk2_rcv_saddr
&& !sk_ipv6only
)
1248 if (addr_type2
== IPV6_ADDR_ANY
&&
1249 !(sk2_ipv6only
&& addr_type
== IPV6_ADDR_MAPPED
))
1252 if (addr_type
== IPV6_ADDR_ANY
&&
1253 !(sk_ipv6only
&& addr_type2
== IPV6_ADDR_MAPPED
))
1256 if (sk2_rcv_saddr6
&&
1257 ipv6_addr_equal(sk_rcv_saddr6
, sk2_rcv_saddr6
))
1260 if (addr_type
== IPV6_ADDR_MAPPED
&&
1262 (!sk2_rcv_saddr
|| !sk_rcv_saddr
|| sk_rcv_saddr
== sk2_rcv_saddr
))
1268 /* Gets referenced address, destroys ifaddr */
1270 static void addrconf_dad_stop(struct inet6_ifaddr
*ifp
)
1272 if (ifp
->flags
&IFA_F_PERMANENT
) {
1273 spin_lock_bh(&ifp
->lock
);
1274 addrconf_del_timer(ifp
);
1275 ifp
->flags
|= IFA_F_TENTATIVE
;
1276 spin_unlock_bh(&ifp
->lock
);
1278 #ifdef CONFIG_IPV6_PRIVACY
1279 } else if (ifp
->flags
&IFA_F_TEMPORARY
) {
1280 struct inet6_ifaddr
*ifpub
;
1281 spin_lock_bh(&ifp
->lock
);
1284 in6_ifa_hold(ifpub
);
1285 spin_unlock_bh(&ifp
->lock
);
1286 ipv6_create_tempaddr(ifpub
, ifp
);
1289 spin_unlock_bh(&ifp
->lock
);
1297 void addrconf_dad_failure(struct inet6_ifaddr
*ifp
)
1299 if (net_ratelimit())
1300 printk(KERN_INFO
"%s: duplicate address detected!\n", ifp
->idev
->dev
->name
);
1301 addrconf_dad_stop(ifp
);
1304 /* Join to solicited addr multicast group. */
1306 void addrconf_join_solict(struct net_device
*dev
, struct in6_addr
*addr
)
1308 struct in6_addr maddr
;
1310 if (dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1313 addrconf_addr_solict_mult(addr
, &maddr
);
1314 ipv6_dev_mc_inc(dev
, &maddr
);
1317 void addrconf_leave_solict(struct inet6_dev
*idev
, struct in6_addr
*addr
)
1319 struct in6_addr maddr
;
1321 if (idev
->dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1324 addrconf_addr_solict_mult(addr
, &maddr
);
1325 __ipv6_dev_mc_dec(idev
, &maddr
);
1328 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
)
1330 struct in6_addr addr
;
1331 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1332 if (ipv6_addr_any(&addr
))
1334 ipv6_dev_ac_inc(ifp
->idev
->dev
, &addr
);
1337 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
)
1339 struct in6_addr addr
;
1340 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1341 if (ipv6_addr_any(&addr
))
1343 __ipv6_dev_ac_dec(ifp
->idev
, &addr
);
1346 static int addrconf_ifid_eui48(u8
*eui
, struct net_device
*dev
)
1348 if (dev
->addr_len
!= ETH_ALEN
)
1350 memcpy(eui
, dev
->dev_addr
, 3);
1351 memcpy(eui
+ 5, dev
->dev_addr
+ 3, 3);
1354 * The zSeries OSA network cards can be shared among various
1355 * OS instances, but the OSA cards have only one MAC address.
1356 * This leads to duplicate address conflicts in conjunction
1357 * with IPv6 if more than one instance uses the same card.
1359 * The driver for these cards can deliver a unique 16-bit
1360 * identifier for each instance sharing the same card. It is
1361 * placed instead of 0xFFFE in the interface identifier. The
1362 * "u" bit of the interface identifier is not inverted in this
1363 * case. Hence the resulting interface identifier has local
1364 * scope according to RFC2373.
1367 eui
[3] = (dev
->dev_id
>> 8) & 0xFF;
1368 eui
[4] = dev
->dev_id
& 0xFF;
1377 static int addrconf_ifid_arcnet(u8
*eui
, struct net_device
*dev
)
1379 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1380 if (dev
->addr_len
!= ARCNET_ALEN
)
1383 eui
[7] = *(u8
*)dev
->dev_addr
;
1387 static int addrconf_ifid_infiniband(u8
*eui
, struct net_device
*dev
)
1389 if (dev
->addr_len
!= INFINIBAND_ALEN
)
1391 memcpy(eui
, dev
->dev_addr
+ 12, 8);
1396 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
)
1398 switch (dev
->type
) {
1401 case ARPHRD_IEEE802_TR
:
1402 return addrconf_ifid_eui48(eui
, dev
);
1404 return addrconf_ifid_arcnet(eui
, dev
);
1405 case ARPHRD_INFINIBAND
:
1406 return addrconf_ifid_infiniband(eui
, dev
);
1411 static int ipv6_inherit_eui64(u8
*eui
, struct inet6_dev
*idev
)
1414 struct inet6_ifaddr
*ifp
;
1416 read_lock_bh(&idev
->lock
);
1417 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1418 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1419 memcpy(eui
, ifp
->addr
.s6_addr
+8, 8);
1424 read_unlock_bh(&idev
->lock
);
1428 #ifdef CONFIG_IPV6_PRIVACY
1429 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1430 static int __ipv6_regen_rndid(struct inet6_dev
*idev
)
1433 get_random_bytes(idev
->rndid
, sizeof(idev
->rndid
));
1434 idev
->rndid
[0] &= ~0x02;
1437 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1438 * check if generated address is not inappropriate
1440 * - Reserved subnet anycast (RFC 2526)
1441 * 11111101 11....11 1xxxxxxx
1442 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1443 * 00-00-5E-FE-xx-xx-xx-xx
1445 * - XXX: already assigned to an address on the device
1447 if (idev
->rndid
[0] == 0xfd &&
1448 (idev
->rndid
[1]&idev
->rndid
[2]&idev
->rndid
[3]&idev
->rndid
[4]&idev
->rndid
[5]&idev
->rndid
[6]) == 0xff &&
1449 (idev
->rndid
[7]&0x80))
1451 if ((idev
->rndid
[0]|idev
->rndid
[1]) == 0) {
1452 if (idev
->rndid
[2] == 0x5e && idev
->rndid
[3] == 0xfe)
1454 if ((idev
->rndid
[2]|idev
->rndid
[3]|idev
->rndid
[4]|idev
->rndid
[5]|idev
->rndid
[6]|idev
->rndid
[7]) == 0x00)
1461 static void ipv6_regen_rndid(unsigned long data
)
1463 struct inet6_dev
*idev
= (struct inet6_dev
*) data
;
1464 unsigned long expires
;
1466 read_lock_bh(&addrconf_lock
);
1467 write_lock_bh(&idev
->lock
);
1472 if (__ipv6_regen_rndid(idev
) < 0)
1476 idev
->cnf
.temp_prefered_lft
* HZ
-
1477 idev
->cnf
.regen_max_retry
* idev
->cnf
.dad_transmits
* idev
->nd_parms
->retrans_time
- desync_factor
;
1478 if (time_before(expires
, jiffies
)) {
1480 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1485 if (!mod_timer(&idev
->regen_timer
, expires
))
1489 write_unlock_bh(&idev
->lock
);
1490 read_unlock_bh(&addrconf_lock
);
1494 static int __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
) {
1497 if (tmpaddr
&& memcmp(idev
->rndid
, &tmpaddr
->s6_addr
[8], 8) == 0)
1498 ret
= __ipv6_regen_rndid(idev
);
1508 addrconf_prefix_route(struct in6_addr
*pfx
, int plen
, struct net_device
*dev
,
1509 unsigned long expires
, u32 flags
)
1511 struct in6_rtmsg rtmsg
;
1513 memset(&rtmsg
, 0, sizeof(rtmsg
));
1514 ipv6_addr_copy(&rtmsg
.rtmsg_dst
, pfx
);
1515 rtmsg
.rtmsg_dst_len
= plen
;
1516 rtmsg
.rtmsg_metric
= IP6_RT_PRIO_ADDRCONF
;
1517 rtmsg
.rtmsg_ifindex
= dev
->ifindex
;
1518 rtmsg
.rtmsg_info
= expires
;
1519 rtmsg
.rtmsg_flags
= RTF_UP
|flags
;
1520 rtmsg
.rtmsg_type
= RTMSG_NEWROUTE
;
1522 /* Prevent useless cloning on PtP SIT.
1523 This thing is done here expecting that the whole
1524 class of non-broadcast devices need not cloning.
1526 if (dev
->type
== ARPHRD_SIT
&& (dev
->flags
&IFF_POINTOPOINT
))
1527 rtmsg
.rtmsg_flags
|= RTF_NONEXTHOP
;
1529 ip6_route_add(&rtmsg
, NULL
, NULL
, NULL
, RT6_TABLE_PREFIX
);
1532 /* Create "default" multicast route to the interface */
1534 static void addrconf_add_mroute(struct net_device
*dev
)
1536 struct in6_rtmsg rtmsg
;
1538 memset(&rtmsg
, 0, sizeof(rtmsg
));
1539 ipv6_addr_set(&rtmsg
.rtmsg_dst
,
1540 htonl(0xFF000000), 0, 0, 0);
1541 rtmsg
.rtmsg_dst_len
= 8;
1542 rtmsg
.rtmsg_metric
= IP6_RT_PRIO_ADDRCONF
;
1543 rtmsg
.rtmsg_ifindex
= dev
->ifindex
;
1544 rtmsg
.rtmsg_flags
= RTF_UP
;
1545 rtmsg
.rtmsg_type
= RTMSG_NEWROUTE
;
1546 ip6_route_add(&rtmsg
, NULL
, NULL
, NULL
, RT6_TABLE_LOCAL
);
1549 static void sit_route_add(struct net_device
*dev
)
1551 struct in6_rtmsg rtmsg
;
1553 memset(&rtmsg
, 0, sizeof(rtmsg
));
1555 rtmsg
.rtmsg_type
= RTMSG_NEWROUTE
;
1556 rtmsg
.rtmsg_metric
= IP6_RT_PRIO_ADDRCONF
;
1558 /* prefix length - 96 bits "::d.d.d.d" */
1559 rtmsg
.rtmsg_dst_len
= 96;
1560 rtmsg
.rtmsg_flags
= RTF_UP
|RTF_NONEXTHOP
;
1561 rtmsg
.rtmsg_ifindex
= dev
->ifindex
;
1563 ip6_route_add(&rtmsg
, NULL
, NULL
, NULL
, RT6_TABLE_MAIN
);
1566 static void addrconf_add_lroute(struct net_device
*dev
)
1568 struct in6_addr addr
;
1570 ipv6_addr_set(&addr
, htonl(0xFE800000), 0, 0, 0);
1571 addrconf_prefix_route(&addr
, 64, dev
, 0, 0);
1574 static struct inet6_dev
*addrconf_add_dev(struct net_device
*dev
)
1576 struct inet6_dev
*idev
;
1580 if ((idev
= ipv6_find_idev(dev
)) == NULL
)
1583 /* Add default multicast route */
1584 addrconf_add_mroute(dev
);
1586 /* Add link local route */
1587 addrconf_add_lroute(dev
);
1591 void addrconf_prefix_rcv(struct net_device
*dev
, u8
*opt
, int len
)
1593 struct prefix_info
*pinfo
;
1597 unsigned long rt_expires
;
1598 struct inet6_dev
*in6_dev
;
1600 pinfo
= (struct prefix_info
*) opt
;
1602 if (len
< sizeof(struct prefix_info
)) {
1603 ADBG(("addrconf: prefix option too short\n"));
1608 * Validation checks ([ADDRCONF], page 19)
1611 addr_type
= ipv6_addr_type(&pinfo
->prefix
);
1613 if (addr_type
& (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
))
1616 valid_lft
= ntohl(pinfo
->valid
);
1617 prefered_lft
= ntohl(pinfo
->prefered
);
1619 if (prefered_lft
> valid_lft
) {
1620 if (net_ratelimit())
1621 printk(KERN_WARNING
"addrconf: prefix option has invalid lifetime\n");
1625 in6_dev
= in6_dev_get(dev
);
1627 if (in6_dev
== NULL
) {
1628 if (net_ratelimit())
1629 printk(KERN_DEBUG
"addrconf: device %s not configured\n", dev
->name
);
1634 * Two things going on here:
1635 * 1) Add routes for on-link prefixes
1636 * 2) Configure prefixes with the auto flag set
1639 /* Avoid arithmetic overflow. Really, we could
1640 save rt_expires in seconds, likely valid_lft,
1641 but it would require division in fib gc, that it
1644 if (valid_lft
>= 0x7FFFFFFF/HZ
)
1645 rt_expires
= 0x7FFFFFFF - (0x7FFFFFFF % HZ
);
1647 rt_expires
= valid_lft
* HZ
;
1650 * We convert this (in jiffies) to clock_t later.
1651 * Avoid arithmetic overflow there as well.
1652 * Overflow can happen only if HZ < USER_HZ.
1654 if (HZ
< USER_HZ
&& rt_expires
> 0x7FFFFFFF / USER_HZ
)
1655 rt_expires
= 0x7FFFFFFF / USER_HZ
;
1657 if (pinfo
->onlink
) {
1658 struct rt6_info
*rt
;
1659 rt
= rt6_lookup(&pinfo
->prefix
, NULL
, dev
->ifindex
, 1);
1661 if (rt
&& ((rt
->rt6i_flags
& (RTF_GATEWAY
| RTF_DEFAULT
)) == 0)) {
1662 if (rt
->rt6i_flags
&RTF_EXPIRES
) {
1663 if (valid_lft
== 0) {
1664 ip6_del_rt(rt
, NULL
, NULL
, NULL
);
1667 rt
->rt6i_expires
= jiffies
+ rt_expires
;
1670 } else if (valid_lft
) {
1671 addrconf_prefix_route(&pinfo
->prefix
, pinfo
->prefix_len
,
1672 dev
, jiffies_to_clock_t(rt_expires
), RTF_ADDRCONF
|RTF_EXPIRES
|RTF_PREFIX_RT
);
1675 dst_release(&rt
->u
.dst
);
1678 /* Try to figure out our local address for this prefix */
1680 if (pinfo
->autoconf
&& in6_dev
->cnf
.autoconf
) {
1681 struct inet6_ifaddr
* ifp
;
1682 struct in6_addr addr
;
1683 int create
= 0, update_lft
= 0;
1685 if (pinfo
->prefix_len
== 64) {
1686 memcpy(&addr
, &pinfo
->prefix
, 8);
1687 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) &&
1688 ipv6_inherit_eui64(addr
.s6_addr
+ 8, in6_dev
)) {
1689 in6_dev_put(in6_dev
);
1694 if (net_ratelimit())
1695 printk(KERN_DEBUG
"IPv6 addrconf: prefix with wrong length %d\n",
1697 in6_dev_put(in6_dev
);
1702 ifp
= ipv6_get_ifaddr(&addr
, dev
, 1);
1704 if (ifp
== NULL
&& valid_lft
) {
1705 int max_addresses
= in6_dev
->cnf
.max_addresses
;
1707 /* Do not allow to create too much of autoconfigured
1708 * addresses; this would be too easy way to crash kernel.
1710 if (!max_addresses
||
1711 ipv6_count_addresses(in6_dev
) < max_addresses
)
1712 ifp
= ipv6_add_addr(in6_dev
, &addr
, pinfo
->prefix_len
,
1713 addr_type
&IPV6_ADDR_SCOPE_MASK
, 0);
1715 if (!ifp
|| IS_ERR(ifp
)) {
1716 in6_dev_put(in6_dev
);
1720 update_lft
= create
= 1;
1721 ifp
->cstamp
= jiffies
;
1722 addrconf_dad_start(ifp
, RTF_ADDRCONF
|RTF_PREFIX_RT
);
1728 #ifdef CONFIG_IPV6_PRIVACY
1729 struct inet6_ifaddr
*ift
;
1733 /* update lifetime (RFC2462 5.5.3 e) */
1734 spin_lock(&ifp
->lock
);
1736 if (ifp
->valid_lft
> (now
- ifp
->tstamp
) / HZ
)
1737 stored_lft
= ifp
->valid_lft
- (now
- ifp
->tstamp
) / HZ
;
1740 if (!update_lft
&& stored_lft
) {
1741 if (valid_lft
> MIN_VALID_LIFETIME
||
1742 valid_lft
> stored_lft
)
1744 else if (stored_lft
<= MIN_VALID_LIFETIME
) {
1745 /* valid_lft <= stored_lft is always true */
1749 valid_lft
= MIN_VALID_LIFETIME
;
1750 if (valid_lft
< prefered_lft
)
1751 prefered_lft
= valid_lft
;
1757 ifp
->valid_lft
= valid_lft
;
1758 ifp
->prefered_lft
= prefered_lft
;
1761 ifp
->flags
&= ~IFA_F_DEPRECATED
;
1762 spin_unlock(&ifp
->lock
);
1764 if (!(flags
&IFA_F_TENTATIVE
))
1765 ipv6_ifa_notify(0, ifp
);
1767 spin_unlock(&ifp
->lock
);
1769 #ifdef CONFIG_IPV6_PRIVACY
1770 read_lock_bh(&in6_dev
->lock
);
1771 /* update all temporary addresses in the list */
1772 for (ift
=in6_dev
->tempaddr_list
; ift
; ift
=ift
->tmp_next
) {
1774 * When adjusting the lifetimes of an existing
1775 * temporary address, only lower the lifetimes.
1776 * Implementations must not increase the
1777 * lifetimes of an existing temporary address
1778 * when processing a Prefix Information Option.
1780 spin_lock(&ift
->lock
);
1782 if (ift
->valid_lft
> valid_lft
&&
1783 ift
->valid_lft
- valid_lft
> (jiffies
- ift
->tstamp
) / HZ
)
1784 ift
->valid_lft
= valid_lft
+ (jiffies
- ift
->tstamp
) / HZ
;
1785 if (ift
->prefered_lft
> prefered_lft
&&
1786 ift
->prefered_lft
- prefered_lft
> (jiffies
- ift
->tstamp
) / HZ
)
1787 ift
->prefered_lft
= prefered_lft
+ (jiffies
- ift
->tstamp
) / HZ
;
1788 spin_unlock(&ift
->lock
);
1789 if (!(flags
&IFA_F_TENTATIVE
))
1790 ipv6_ifa_notify(0, ift
);
1793 if (create
&& in6_dev
->cnf
.use_tempaddr
> 0) {
1795 * When a new public address is created as described in [ADDRCONF],
1796 * also create a new temporary address.
1798 read_unlock_bh(&in6_dev
->lock
);
1799 ipv6_create_tempaddr(ifp
, NULL
);
1801 read_unlock_bh(&in6_dev
->lock
);
1808 inet6_prefix_notify(RTM_NEWPREFIX
, in6_dev
, pinfo
);
1809 in6_dev_put(in6_dev
);
1813 * Set destination address.
1814 * Special case for SIT interfaces where we create a new "virtual"
1817 int addrconf_set_dstaddr(void __user
*arg
)
1819 struct in6_ifreq ireq
;
1820 struct net_device
*dev
;
1826 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
1829 dev
= __dev_get_by_index(ireq
.ifr6_ifindex
);
1835 if (dev
->type
== ARPHRD_SIT
) {
1838 struct ip_tunnel_parm p
;
1840 err
= -EADDRNOTAVAIL
;
1841 if (!(ipv6_addr_type(&ireq
.ifr6_addr
) & IPV6_ADDR_COMPATv4
))
1844 memset(&p
, 0, sizeof(p
));
1845 p
.iph
.daddr
= ireq
.ifr6_addr
.s6_addr32
[3];
1849 p
.iph
.protocol
= IPPROTO_IPV6
;
1851 ifr
.ifr_ifru
.ifru_data
= (void __user
*)&p
;
1853 oldfs
= get_fs(); set_fs(KERNEL_DS
);
1854 err
= dev
->do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
1859 if ((dev
= __dev_get_by_name(p
.name
)) == NULL
)
1861 err
= dev_open(dev
);
1871 * Manual configuration of address on an interface
1873 static int inet6_addr_add(int ifindex
, struct in6_addr
*pfx
, int plen
,
1874 __u32 prefered_lft
, __u32 valid_lft
)
1876 struct inet6_ifaddr
*ifp
;
1877 struct inet6_dev
*idev
;
1878 struct net_device
*dev
;
1884 /* check the lifetime */
1885 if (!valid_lft
|| prefered_lft
> valid_lft
)
1888 if ((dev
= __dev_get_by_index(ifindex
)) == NULL
)
1891 if (!(dev
->flags
&IFF_UP
))
1894 if ((idev
= addrconf_add_dev(dev
)) == NULL
)
1897 scope
= ipv6_addr_scope(pfx
);
1899 if (valid_lft
== INFINITY_LIFE_TIME
)
1900 ifa_flags
|= IFA_F_PERMANENT
;
1901 else if (valid_lft
>= 0x7FFFFFFF/HZ
)
1902 valid_lft
= 0x7FFFFFFF/HZ
;
1904 if (prefered_lft
== 0)
1905 ifa_flags
|= IFA_F_DEPRECATED
;
1906 else if ((prefered_lft
>= 0x7FFFFFFF/HZ
) &&
1907 (prefered_lft
!= INFINITY_LIFE_TIME
))
1908 prefered_lft
= 0x7FFFFFFF/HZ
;
1910 ifp
= ipv6_add_addr(idev
, pfx
, plen
, scope
, ifa_flags
);
1913 spin_lock_bh(&ifp
->lock
);
1914 ifp
->valid_lft
= valid_lft
;
1915 ifp
->prefered_lft
= prefered_lft
;
1916 ifp
->tstamp
= jiffies
;
1917 spin_unlock_bh(&ifp
->lock
);
1919 addrconf_dad_start(ifp
, 0);
1925 return PTR_ERR(ifp
);
1928 static int inet6_addr_del(int ifindex
, struct in6_addr
*pfx
, int plen
)
1930 struct inet6_ifaddr
*ifp
;
1931 struct inet6_dev
*idev
;
1932 struct net_device
*dev
;
1934 if ((dev
= __dev_get_by_index(ifindex
)) == NULL
)
1937 if ((idev
= __in6_dev_get(dev
)) == NULL
)
1940 read_lock_bh(&idev
->lock
);
1941 for (ifp
= idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1942 if (ifp
->prefix_len
== plen
&&
1943 ipv6_addr_equal(pfx
, &ifp
->addr
)) {
1945 read_unlock_bh(&idev
->lock
);
1949 /* If the last address is deleted administratively,
1950 disable IPv6 on this interface.
1952 if (idev
->addr_list
== NULL
)
1953 addrconf_ifdown(idev
->dev
, 1);
1957 read_unlock_bh(&idev
->lock
);
1958 return -EADDRNOTAVAIL
;
1962 int addrconf_add_ifaddr(void __user
*arg
)
1964 struct in6_ifreq ireq
;
1967 if (!capable(CAP_NET_ADMIN
))
1970 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
1974 err
= inet6_addr_add(ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, ireq
.ifr6_prefixlen
,
1975 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
1980 int addrconf_del_ifaddr(void __user
*arg
)
1982 struct in6_ifreq ireq
;
1985 if (!capable(CAP_NET_ADMIN
))
1988 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
1992 err
= inet6_addr_del(ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, ireq
.ifr6_prefixlen
);
1997 static void sit_add_v4_addrs(struct inet6_dev
*idev
)
1999 struct inet6_ifaddr
* ifp
;
2000 struct in6_addr addr
;
2001 struct net_device
*dev
;
2006 memset(&addr
, 0, sizeof(struct in6_addr
));
2007 memcpy(&addr
.s6_addr32
[3], idev
->dev
->dev_addr
, 4);
2009 if (idev
->dev
->flags
&IFF_POINTOPOINT
) {
2010 addr
.s6_addr32
[0] = htonl(0xfe800000);
2013 scope
= IPV6_ADDR_COMPATv4
;
2016 if (addr
.s6_addr32
[3]) {
2017 ifp
= ipv6_add_addr(idev
, &addr
, 128, scope
, IFA_F_PERMANENT
);
2019 spin_lock_bh(&ifp
->lock
);
2020 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2021 spin_unlock_bh(&ifp
->lock
);
2022 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2028 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
2029 struct in_device
* in_dev
= __in_dev_get_rtnl(dev
);
2030 if (in_dev
&& (dev
->flags
& IFF_UP
)) {
2031 struct in_ifaddr
* ifa
;
2035 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
2038 addr
.s6_addr32
[3] = ifa
->ifa_local
;
2040 if (ifa
->ifa_scope
== RT_SCOPE_LINK
)
2042 if (ifa
->ifa_scope
>= RT_SCOPE_HOST
) {
2043 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2047 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2052 ifp
= ipv6_add_addr(idev
, &addr
, plen
, flag
,
2055 spin_lock_bh(&ifp
->lock
);
2056 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2057 spin_unlock_bh(&ifp
->lock
);
2058 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2066 static void init_loopback(struct net_device
*dev
)
2068 struct inet6_dev
*idev
;
2069 struct inet6_ifaddr
* ifp
;
2075 if ((idev
= ipv6_find_idev(dev
)) == NULL
) {
2076 printk(KERN_DEBUG
"init loopback: add_dev failed\n");
2080 ifp
= ipv6_add_addr(idev
, &in6addr_loopback
, 128, IFA_HOST
, IFA_F_PERMANENT
);
2082 spin_lock_bh(&ifp
->lock
);
2083 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2084 spin_unlock_bh(&ifp
->lock
);
2085 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2090 static void addrconf_add_linklocal(struct inet6_dev
*idev
, struct in6_addr
*addr
)
2092 struct inet6_ifaddr
* ifp
;
2094 ifp
= ipv6_add_addr(idev
, addr
, 64, IFA_LINK
, IFA_F_PERMANENT
);
2096 addrconf_dad_start(ifp
, 0);
2101 static void addrconf_dev_config(struct net_device
*dev
)
2103 struct in6_addr addr
;
2104 struct inet6_dev
* idev
;
2108 if ((dev
->type
!= ARPHRD_ETHER
) &&
2109 (dev
->type
!= ARPHRD_FDDI
) &&
2110 (dev
->type
!= ARPHRD_IEEE802_TR
) &&
2111 (dev
->type
!= ARPHRD_ARCNET
) &&
2112 (dev
->type
!= ARPHRD_INFINIBAND
)) {
2113 /* Alas, we support only Ethernet autoconfiguration. */
2117 idev
= addrconf_add_dev(dev
);
2121 memset(&addr
, 0, sizeof(struct in6_addr
));
2122 addr
.s6_addr32
[0] = htonl(0xFE800000);
2124 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) == 0)
2125 addrconf_add_linklocal(idev
, &addr
);
2128 static void addrconf_sit_config(struct net_device
*dev
)
2130 struct inet6_dev
*idev
;
2135 * Configure the tunnel with one of our IPv4
2136 * addresses... we should configure all of
2137 * our v4 addrs in the tunnel
2140 if ((idev
= ipv6_find_idev(dev
)) == NULL
) {
2141 printk(KERN_DEBUG
"init sit: add_dev failed\n");
2145 sit_add_v4_addrs(idev
);
2147 if (dev
->flags
&IFF_POINTOPOINT
) {
2148 addrconf_add_mroute(dev
);
2149 addrconf_add_lroute(dev
);
2155 ipv6_inherit_linklocal(struct inet6_dev
*idev
, struct net_device
*link_dev
)
2157 struct in6_addr lladdr
;
2159 if (!ipv6_get_lladdr(link_dev
, &lladdr
)) {
2160 addrconf_add_linklocal(idev
, &lladdr
);
2166 static void ip6_tnl_add_linklocal(struct inet6_dev
*idev
)
2168 struct net_device
*link_dev
;
2170 /* first try to inherit the link-local address from the link device */
2171 if (idev
->dev
->iflink
&&
2172 (link_dev
= __dev_get_by_index(idev
->dev
->iflink
))) {
2173 if (!ipv6_inherit_linklocal(idev
, link_dev
))
2176 /* then try to inherit it from any device */
2177 for (link_dev
= dev_base
; link_dev
; link_dev
= link_dev
->next
) {
2178 if (!ipv6_inherit_linklocal(idev
, link_dev
))
2181 printk(KERN_DEBUG
"init ip6-ip6: add_linklocal failed\n");
2185 * Autoconfigure tunnel with a link-local address so routing protocols,
2186 * DHCPv6, MLD etc. can be run over the virtual link
2189 static void addrconf_ip6_tnl_config(struct net_device
*dev
)
2191 struct inet6_dev
*idev
;
2195 if ((idev
= addrconf_add_dev(dev
)) == NULL
) {
2196 printk(KERN_DEBUG
"init ip6-ip6: add_dev failed\n");
2199 ip6_tnl_add_linklocal(idev
);
2202 static int addrconf_notify(struct notifier_block
*this, unsigned long event
,
2205 struct net_device
*dev
= (struct net_device
*) data
;
2206 struct inet6_dev
*idev
= __in6_dev_get(dev
);
2207 int run_pending
= 0;
2212 if (event
== NETDEV_UP
) {
2213 if (!netif_carrier_ok(dev
)) {
2214 /* device is not ready yet. */
2216 "ADDRCONF(NETDEV_UP): %s: "
2217 "link is not ready\n",
2223 idev
->if_flags
|= IF_READY
;
2225 if (!netif_carrier_ok(dev
)) {
2226 /* device is still not ready. */
2231 if (idev
->if_flags
& IF_READY
) {
2232 /* device is already configured. */
2235 idev
->if_flags
|= IF_READY
;
2239 "ADDRCONF(NETDEV_CHANGE): %s: "
2240 "link becomes ready\n",
2248 addrconf_sit_config(dev
);
2250 case ARPHRD_TUNNEL6
:
2251 addrconf_ip6_tnl_config(dev
);
2253 case ARPHRD_LOOPBACK
:
2258 addrconf_dev_config(dev
);
2263 addrconf_dad_run(idev
);
2265 /* If the MTU changed during the interface down, when the
2266 interface up, the changed MTU must be reflected in the
2267 idev as well as routers.
2269 if (idev
->cnf
.mtu6
!= dev
->mtu
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2270 rt6_mtu_change(dev
, dev
->mtu
);
2271 idev
->cnf
.mtu6
= dev
->mtu
;
2273 idev
->tstamp
= jiffies
;
2274 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
2275 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2276 stop IPv6 on this interface.
2278 if (dev
->mtu
< IPV6_MIN_MTU
)
2279 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
2283 case NETDEV_CHANGEMTU
:
2284 if ( idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2285 rt6_mtu_change(dev
, dev
->mtu
);
2286 idev
->cnf
.mtu6
= dev
->mtu
;
2290 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2293 case NETDEV_UNREGISTER
:
2295 * Remove all addresses from this interface.
2297 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
2300 case NETDEV_CHANGENAME
:
2301 #ifdef CONFIG_SYSCTL
2303 addrconf_sysctl_unregister(&idev
->cnf
);
2304 neigh_sysctl_unregister(idev
->nd_parms
);
2305 neigh_sysctl_register(dev
, idev
->nd_parms
,
2306 NET_IPV6
, NET_IPV6_NEIGH
, "ipv6",
2307 &ndisc_ifinfo_sysctl_change
,
2309 addrconf_sysctl_register(idev
, &idev
->cnf
);
2319 * addrconf module should be notified of a device going up
2321 static struct notifier_block ipv6_dev_notf
= {
2322 .notifier_call
= addrconf_notify
,
2326 static int addrconf_ifdown(struct net_device
*dev
, int how
)
2328 struct inet6_dev
*idev
;
2329 struct inet6_ifaddr
*ifa
, **bifa
;
2334 if (dev
== &loopback_dev
&& how
== 1)
2338 neigh_ifdown(&nd_tbl
, dev
);
2340 idev
= __in6_dev_get(dev
);
2344 /* Step 1: remove reference to ipv6 device from parent device.
2348 write_lock_bh(&addrconf_lock
);
2349 dev
->ip6_ptr
= NULL
;
2351 write_unlock_bh(&addrconf_lock
);
2353 /* Step 1.5: remove snmp6 entry */
2354 snmp6_unregister_dev(idev
);
2358 /* Step 2: clear hash table */
2359 for (i
=0; i
<IN6_ADDR_HSIZE
; i
++) {
2360 bifa
= &inet6_addr_lst
[i
];
2362 write_lock_bh(&addrconf_hash_lock
);
2363 while ((ifa
= *bifa
) != NULL
) {
2364 if (ifa
->idev
== idev
) {
2365 *bifa
= ifa
->lst_next
;
2366 ifa
->lst_next
= NULL
;
2367 addrconf_del_timer(ifa
);
2371 bifa
= &ifa
->lst_next
;
2373 write_unlock_bh(&addrconf_hash_lock
);
2376 write_lock_bh(&idev
->lock
);
2378 /* Step 3: clear flags for stateless addrconf */
2380 idev
->if_flags
&= ~(IF_RS_SENT
|IF_RA_RCVD
|IF_READY
);
2382 /* Step 4: clear address list */
2383 #ifdef CONFIG_IPV6_PRIVACY
2384 if (how
== 1 && del_timer(&idev
->regen_timer
))
2387 /* clear tempaddr list */
2388 while ((ifa
= idev
->tempaddr_list
) != NULL
) {
2389 idev
->tempaddr_list
= ifa
->tmp_next
;
2390 ifa
->tmp_next
= NULL
;
2392 write_unlock_bh(&idev
->lock
);
2393 spin_lock_bh(&ifa
->lock
);
2396 in6_ifa_put(ifa
->ifpub
);
2399 spin_unlock_bh(&ifa
->lock
);
2401 write_lock_bh(&idev
->lock
);
2404 while ((ifa
= idev
->addr_list
) != NULL
) {
2405 idev
->addr_list
= ifa
->if_next
;
2406 ifa
->if_next
= NULL
;
2408 addrconf_del_timer(ifa
);
2409 write_unlock_bh(&idev
->lock
);
2411 __ipv6_ifa_notify(RTM_DELADDR
, ifa
);
2414 write_lock_bh(&idev
->lock
);
2416 write_unlock_bh(&idev
->lock
);
2418 /* Step 5: Discard multicast list */
2421 ipv6_mc_destroy_dev(idev
);
2425 /* Step 5: netlink notification of this interface */
2426 idev
->tstamp
= jiffies
;
2427 inet6_ifinfo_notify(RTM_DELLINK
, idev
);
2429 /* Shot the device (if unregistered) */
2432 #ifdef CONFIG_SYSCTL
2433 addrconf_sysctl_unregister(&idev
->cnf
);
2434 neigh_sysctl_unregister(idev
->nd_parms
);
2436 neigh_parms_release(&nd_tbl
, idev
->nd_parms
);
2437 neigh_ifdown(&nd_tbl
, dev
);
2443 static void addrconf_rs_timer(unsigned long data
)
2445 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*) data
;
2447 if (ifp
->idev
->cnf
.forwarding
)
2450 if (ifp
->idev
->if_flags
& IF_RA_RCVD
) {
2452 * Announcement received after solicitation
2458 spin_lock(&ifp
->lock
);
2459 if (ifp
->probes
++ < ifp
->idev
->cnf
.rtr_solicits
) {
2460 struct in6_addr all_routers
;
2462 /* The wait after the last probe can be shorter */
2463 addrconf_mod_timer(ifp
, AC_RS
,
2464 (ifp
->probes
== ifp
->idev
->cnf
.rtr_solicits
) ?
2465 ifp
->idev
->cnf
.rtr_solicit_delay
:
2466 ifp
->idev
->cnf
.rtr_solicit_interval
);
2467 spin_unlock(&ifp
->lock
);
2469 ipv6_addr_all_routers(&all_routers
);
2471 ndisc_send_rs(ifp
->idev
->dev
, &ifp
->addr
, &all_routers
);
2473 spin_unlock(&ifp
->lock
);
2475 * Note: we do not support deprecated "all on-link"
2476 * assumption any longer.
2478 printk(KERN_DEBUG
"%s: no IPv6 routers present\n",
2479 ifp
->idev
->dev
->name
);
2487 * Duplicate Address Detection
2489 static void addrconf_dad_kick(struct inet6_ifaddr
*ifp
)
2491 unsigned long rand_num
;
2492 struct inet6_dev
*idev
= ifp
->idev
;
2494 rand_num
= net_random() % (idev
->cnf
.rtr_solicit_delay
? : 1);
2495 ifp
->probes
= idev
->cnf
.dad_transmits
;
2496 addrconf_mod_timer(ifp
, AC_DAD
, rand_num
);
2499 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
, u32 flags
)
2501 struct inet6_dev
*idev
= ifp
->idev
;
2502 struct net_device
*dev
= idev
->dev
;
2504 addrconf_join_solict(dev
, &ifp
->addr
);
2506 if (ifp
->prefix_len
!= 128 && (ifp
->flags
&IFA_F_PERMANENT
))
2507 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, dev
, 0,
2510 net_srandom(ifp
->addr
.s6_addr32
[3]);
2512 read_lock_bh(&idev
->lock
);
2515 spin_lock_bh(&ifp
->lock
);
2517 if (dev
->flags
&(IFF_NOARP
|IFF_LOOPBACK
) ||
2518 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
2519 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2520 spin_unlock_bh(&ifp
->lock
);
2521 read_unlock_bh(&idev
->lock
);
2523 addrconf_dad_completed(ifp
);
2527 if (!(idev
->if_flags
& IF_READY
)) {
2528 spin_unlock_bh(&ifp
->lock
);
2529 read_unlock_bh(&idev
->lock
);
2531 * If the defice is not ready:
2532 * - keep it tentative if it is a permanent address.
2533 * - otherwise, kill it.
2536 addrconf_dad_stop(ifp
);
2539 addrconf_dad_kick(ifp
);
2540 spin_unlock_bh(&ifp
->lock
);
2542 read_unlock_bh(&idev
->lock
);
2545 static void addrconf_dad_timer(unsigned long data
)
2547 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*) data
;
2548 struct inet6_dev
*idev
= ifp
->idev
;
2549 struct in6_addr unspec
;
2550 struct in6_addr mcaddr
;
2552 read_lock_bh(&idev
->lock
);
2554 read_unlock_bh(&idev
->lock
);
2557 spin_lock_bh(&ifp
->lock
);
2558 if (ifp
->probes
== 0) {
2560 * DAD was successful
2563 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2564 spin_unlock_bh(&ifp
->lock
);
2565 read_unlock_bh(&idev
->lock
);
2567 addrconf_dad_completed(ifp
);
2573 addrconf_mod_timer(ifp
, AC_DAD
, ifp
->idev
->nd_parms
->retrans_time
);
2574 spin_unlock_bh(&ifp
->lock
);
2575 read_unlock_bh(&idev
->lock
);
2577 /* send a neighbour solicitation for our addr */
2578 memset(&unspec
, 0, sizeof(unspec
));
2579 addrconf_addr_solict_mult(&ifp
->addr
, &mcaddr
);
2580 ndisc_send_ns(ifp
->idev
->dev
, NULL
, &ifp
->addr
, &mcaddr
, &unspec
);
2585 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
)
2587 struct net_device
* dev
= ifp
->idev
->dev
;
2590 * Configure the address for reception. Now it is valid.
2593 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2595 /* If added prefix is link local and forwarding is off,
2596 start sending router solicitations.
2599 if (ifp
->idev
->cnf
.forwarding
== 0 &&
2600 ifp
->idev
->cnf
.rtr_solicits
> 0 &&
2601 (dev
->flags
&IFF_LOOPBACK
) == 0 &&
2602 (ipv6_addr_type(&ifp
->addr
) & IPV6_ADDR_LINKLOCAL
)) {
2603 struct in6_addr all_routers
;
2605 ipv6_addr_all_routers(&all_routers
);
2608 * If a host as already performed a random delay
2609 * [...] as part of DAD [...] there is no need
2610 * to delay again before sending the first RS
2612 ndisc_send_rs(ifp
->idev
->dev
, &ifp
->addr
, &all_routers
);
2614 spin_lock_bh(&ifp
->lock
);
2616 ifp
->idev
->if_flags
|= IF_RS_SENT
;
2617 addrconf_mod_timer(ifp
, AC_RS
, ifp
->idev
->cnf
.rtr_solicit_interval
);
2618 spin_unlock_bh(&ifp
->lock
);
2622 static void addrconf_dad_run(struct inet6_dev
*idev
) {
2623 struct inet6_ifaddr
*ifp
;
2625 read_lock_bh(&idev
->lock
);
2626 for (ifp
= idev
->addr_list
; ifp
; ifp
= ifp
->if_next
) {
2627 spin_lock_bh(&ifp
->lock
);
2628 if (!(ifp
->flags
& IFA_F_TENTATIVE
)) {
2629 spin_unlock_bh(&ifp
->lock
);
2632 spin_unlock_bh(&ifp
->lock
);
2633 addrconf_dad_kick(ifp
);
2635 read_unlock_bh(&idev
->lock
);
2638 #ifdef CONFIG_PROC_FS
2639 struct if6_iter_state
{
2643 static struct inet6_ifaddr
*if6_get_first(struct seq_file
*seq
)
2645 struct inet6_ifaddr
*ifa
= NULL
;
2646 struct if6_iter_state
*state
= seq
->private;
2648 for (state
->bucket
= 0; state
->bucket
< IN6_ADDR_HSIZE
; ++state
->bucket
) {
2649 ifa
= inet6_addr_lst
[state
->bucket
];
2656 static struct inet6_ifaddr
*if6_get_next(struct seq_file
*seq
, struct inet6_ifaddr
*ifa
)
2658 struct if6_iter_state
*state
= seq
->private;
2660 ifa
= ifa
->lst_next
;
2662 if (!ifa
&& ++state
->bucket
< IN6_ADDR_HSIZE
) {
2663 ifa
= inet6_addr_lst
[state
->bucket
];
2669 static struct inet6_ifaddr
*if6_get_idx(struct seq_file
*seq
, loff_t pos
)
2671 struct inet6_ifaddr
*ifa
= if6_get_first(seq
);
2674 while(pos
&& (ifa
= if6_get_next(seq
, ifa
)) != NULL
)
2676 return pos
? NULL
: ifa
;
2679 static void *if6_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2681 read_lock_bh(&addrconf_hash_lock
);
2682 return if6_get_idx(seq
, *pos
);
2685 static void *if6_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2687 struct inet6_ifaddr
*ifa
;
2689 ifa
= if6_get_next(seq
, v
);
2694 static void if6_seq_stop(struct seq_file
*seq
, void *v
)
2696 read_unlock_bh(&addrconf_hash_lock
);
2699 static int if6_seq_show(struct seq_file
*seq
, void *v
)
2701 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*)v
;
2703 NIP6_SEQFMT
" %02x %02x %02x %02x %8s\n",
2705 ifp
->idev
->dev
->ifindex
,
2709 ifp
->idev
->dev
->name
);
2713 static struct seq_operations if6_seq_ops
= {
2714 .start
= if6_seq_start
,
2715 .next
= if6_seq_next
,
2716 .show
= if6_seq_show
,
2717 .stop
= if6_seq_stop
,
2720 static int if6_seq_open(struct inode
*inode
, struct file
*file
)
2722 struct seq_file
*seq
;
2724 struct if6_iter_state
*s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
2729 rc
= seq_open(file
, &if6_seq_ops
);
2733 seq
= file
->private_data
;
2742 static struct file_operations if6_fops
= {
2743 .owner
= THIS_MODULE
,
2744 .open
= if6_seq_open
,
2746 .llseek
= seq_lseek
,
2747 .release
= seq_release_private
,
2750 int __init
if6_proc_init(void)
2752 if (!proc_net_fops_create("if_inet6", S_IRUGO
, &if6_fops
))
2757 void if6_proc_exit(void)
2759 proc_net_remove("if_inet6");
2761 #endif /* CONFIG_PROC_FS */
2764 * Periodic address status verification
2767 static void addrconf_verify(unsigned long foo
)
2769 struct inet6_ifaddr
*ifp
;
2770 unsigned long now
, next
;
2773 spin_lock_bh(&addrconf_verify_lock
);
2775 next
= now
+ ADDR_CHECK_FREQUENCY
;
2777 del_timer(&addr_chk_timer
);
2779 for (i
=0; i
< IN6_ADDR_HSIZE
; i
++) {
2782 read_lock(&addrconf_hash_lock
);
2783 for (ifp
=inet6_addr_lst
[i
]; ifp
; ifp
=ifp
->lst_next
) {
2785 #ifdef CONFIG_IPV6_PRIVACY
2786 unsigned long regen_advance
;
2789 if (ifp
->flags
& IFA_F_PERMANENT
)
2792 spin_lock(&ifp
->lock
);
2793 age
= (now
- ifp
->tstamp
) / HZ
;
2795 #ifdef CONFIG_IPV6_PRIVACY
2796 regen_advance
= ifp
->idev
->cnf
.regen_max_retry
*
2797 ifp
->idev
->cnf
.dad_transmits
*
2798 ifp
->idev
->nd_parms
->retrans_time
/ HZ
;
2801 if (ifp
->valid_lft
!= INFINITY_LIFE_TIME
&&
2802 age
>= ifp
->valid_lft
) {
2803 spin_unlock(&ifp
->lock
);
2805 read_unlock(&addrconf_hash_lock
);
2808 } else if (ifp
->prefered_lft
== INFINITY_LIFE_TIME
) {
2809 spin_unlock(&ifp
->lock
);
2811 } else if (age
>= ifp
->prefered_lft
) {
2812 /* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
2815 if (!(ifp
->flags
&IFA_F_DEPRECATED
)) {
2817 ifp
->flags
|= IFA_F_DEPRECATED
;
2820 if (time_before(ifp
->tstamp
+ ifp
->valid_lft
* HZ
, next
))
2821 next
= ifp
->tstamp
+ ifp
->valid_lft
* HZ
;
2823 spin_unlock(&ifp
->lock
);
2827 read_unlock(&addrconf_hash_lock
);
2829 ipv6_ifa_notify(0, ifp
);
2833 #ifdef CONFIG_IPV6_PRIVACY
2834 } else if ((ifp
->flags
&IFA_F_TEMPORARY
) &&
2835 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
2836 if (age
>= ifp
->prefered_lft
- regen_advance
) {
2837 struct inet6_ifaddr
*ifpub
= ifp
->ifpub
;
2838 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
2839 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
2840 if (!ifp
->regen_count
&& ifpub
) {
2843 in6_ifa_hold(ifpub
);
2844 spin_unlock(&ifp
->lock
);
2845 read_unlock(&addrconf_hash_lock
);
2846 spin_lock(&ifpub
->lock
);
2847 ifpub
->regen_count
= 0;
2848 spin_unlock(&ifpub
->lock
);
2849 ipv6_create_tempaddr(ifpub
, ifp
);
2854 } else if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
, next
))
2855 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
;
2856 spin_unlock(&ifp
->lock
);
2859 /* ifp->prefered_lft <= ifp->valid_lft */
2860 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
2861 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
2862 spin_unlock(&ifp
->lock
);
2865 read_unlock(&addrconf_hash_lock
);
2868 addr_chk_timer
.expires
= time_before(next
, jiffies
+ HZ
) ? jiffies
+ HZ
: next
;
2869 add_timer(&addr_chk_timer
);
2870 spin_unlock_bh(&addrconf_verify_lock
);
2874 inet6_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
2876 struct rtattr
**rta
= arg
;
2877 struct ifaddrmsg
*ifm
= NLMSG_DATA(nlh
);
2878 struct in6_addr
*pfx
;
2881 if (rta
[IFA_ADDRESS
-1]) {
2882 if (RTA_PAYLOAD(rta
[IFA_ADDRESS
-1]) < sizeof(*pfx
))
2884 pfx
= RTA_DATA(rta
[IFA_ADDRESS
-1]);
2886 if (rta
[IFA_LOCAL
-1]) {
2887 if (RTA_PAYLOAD(rta
[IFA_LOCAL
-1]) < sizeof(*pfx
) ||
2888 (pfx
&& memcmp(pfx
, RTA_DATA(rta
[IFA_LOCAL
-1]), sizeof(*pfx
))))
2890 pfx
= RTA_DATA(rta
[IFA_LOCAL
-1]);
2895 return inet6_addr_del(ifm
->ifa_index
, pfx
, ifm
->ifa_prefixlen
);
2899 inet6_addr_modify(int ifindex
, struct in6_addr
*pfx
,
2900 __u32 prefered_lft
, __u32 valid_lft
)
2902 struct inet6_ifaddr
*ifp
= NULL
;
2903 struct net_device
*dev
;
2906 if ((dev
= __dev_get_by_index(ifindex
)) == NULL
)
2909 if (!(dev
->flags
&IFF_UP
))
2912 if (!valid_lft
|| (prefered_lft
> valid_lft
))
2915 ifp
= ipv6_get_ifaddr(pfx
, dev
, 1);
2919 if (valid_lft
== INFINITY_LIFE_TIME
)
2920 ifa_flags
= IFA_F_PERMANENT
;
2921 else if (valid_lft
>= 0x7FFFFFFF/HZ
)
2922 valid_lft
= 0x7FFFFFFF/HZ
;
2924 if (prefered_lft
== 0)
2925 ifa_flags
= IFA_F_DEPRECATED
;
2926 else if ((prefered_lft
>= 0x7FFFFFFF/HZ
) &&
2927 (prefered_lft
!= INFINITY_LIFE_TIME
))
2928 prefered_lft
= 0x7FFFFFFF/HZ
;
2930 spin_lock_bh(&ifp
->lock
);
2931 ifp
->flags
= (ifp
->flags
& ~(IFA_F_DEPRECATED
|IFA_F_PERMANENT
)) | ifa_flags
;
2933 ifp
->tstamp
= jiffies
;
2934 ifp
->valid_lft
= valid_lft
;
2935 ifp
->prefered_lft
= prefered_lft
;
2937 spin_unlock_bh(&ifp
->lock
);
2938 if (!(ifp
->flags
&IFA_F_TENTATIVE
))
2939 ipv6_ifa_notify(0, ifp
);
2948 inet6_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
2950 struct rtattr
**rta
= arg
;
2951 struct ifaddrmsg
*ifm
= NLMSG_DATA(nlh
);
2952 struct in6_addr
*pfx
;
2953 __u32 valid_lft
= INFINITY_LIFE_TIME
, prefered_lft
= INFINITY_LIFE_TIME
;
2956 if (rta
[IFA_ADDRESS
-1]) {
2957 if (RTA_PAYLOAD(rta
[IFA_ADDRESS
-1]) < sizeof(*pfx
))
2959 pfx
= RTA_DATA(rta
[IFA_ADDRESS
-1]);
2961 if (rta
[IFA_LOCAL
-1]) {
2962 if (RTA_PAYLOAD(rta
[IFA_LOCAL
-1]) < sizeof(*pfx
) ||
2963 (pfx
&& memcmp(pfx
, RTA_DATA(rta
[IFA_LOCAL
-1]), sizeof(*pfx
))))
2965 pfx
= RTA_DATA(rta
[IFA_LOCAL
-1]);
2970 if (rta
[IFA_CACHEINFO
-1]) {
2971 struct ifa_cacheinfo
*ci
;
2972 if (RTA_PAYLOAD(rta
[IFA_CACHEINFO
-1]) < sizeof(*ci
))
2974 ci
= RTA_DATA(rta
[IFA_CACHEINFO
-1]);
2975 valid_lft
= ci
->ifa_valid
;
2976 prefered_lft
= ci
->ifa_prefered
;
2979 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
2981 ret
= inet6_addr_modify(ifm
->ifa_index
, pfx
,
2982 prefered_lft
, valid_lft
);
2983 if (ret
== 0 || !(nlh
->nlmsg_flags
& NLM_F_CREATE
))
2987 return inet6_addr_add(ifm
->ifa_index
, pfx
, ifm
->ifa_prefixlen
,
2988 prefered_lft
, valid_lft
);
2992 /* Maximum length of ifa_cacheinfo attributes */
2993 #define INET6_IFADDR_RTA_SPACE \
2994 RTA_SPACE(16) /* IFA_ADDRESS */ + \
2995 RTA_SPACE(sizeof(struct ifa_cacheinfo)) /* CACHEINFO */
2997 static int inet6_fill_ifaddr(struct sk_buff
*skb
, struct inet6_ifaddr
*ifa
,
2998 u32 pid
, u32 seq
, int event
, unsigned int flags
)
3000 struct ifaddrmsg
*ifm
;
3001 struct nlmsghdr
*nlh
;
3002 struct ifa_cacheinfo ci
;
3003 unsigned char *b
= skb
->tail
;
3005 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*ifm
), flags
);
3006 ifm
= NLMSG_DATA(nlh
);
3007 ifm
->ifa_family
= AF_INET6
;
3008 ifm
->ifa_prefixlen
= ifa
->prefix_len
;
3009 ifm
->ifa_flags
= ifa
->flags
;
3010 ifm
->ifa_scope
= RT_SCOPE_UNIVERSE
;
3011 if (ifa
->scope
&IFA_HOST
)
3012 ifm
->ifa_scope
= RT_SCOPE_HOST
;
3013 else if (ifa
->scope
&IFA_LINK
)
3014 ifm
->ifa_scope
= RT_SCOPE_LINK
;
3015 else if (ifa
->scope
&IFA_SITE
)
3016 ifm
->ifa_scope
= RT_SCOPE_SITE
;
3017 ifm
->ifa_index
= ifa
->idev
->dev
->ifindex
;
3018 RTA_PUT(skb
, IFA_ADDRESS
, 16, &ifa
->addr
);
3019 if (!(ifa
->flags
&IFA_F_PERMANENT
)) {
3020 ci
.ifa_prefered
= ifa
->prefered_lft
;
3021 ci
.ifa_valid
= ifa
->valid_lft
;
3022 if (ci
.ifa_prefered
!= INFINITY_LIFE_TIME
) {
3023 long tval
= (jiffies
- ifa
->tstamp
)/HZ
;
3024 ci
.ifa_prefered
-= tval
;
3025 if (ci
.ifa_valid
!= INFINITY_LIFE_TIME
)
3026 ci
.ifa_valid
-= tval
;
3029 ci
.ifa_prefered
= INFINITY_LIFE_TIME
;
3030 ci
.ifa_valid
= INFINITY_LIFE_TIME
;
3032 ci
.cstamp
= (__u32
)(TIME_DELTA(ifa
->cstamp
, INITIAL_JIFFIES
) / HZ
* 100
3033 + TIME_DELTA(ifa
->cstamp
, INITIAL_JIFFIES
) % HZ
* 100 / HZ
);
3034 ci
.tstamp
= (__u32
)(TIME_DELTA(ifa
->tstamp
, INITIAL_JIFFIES
) / HZ
* 100
3035 + TIME_DELTA(ifa
->tstamp
, INITIAL_JIFFIES
) % HZ
* 100 / HZ
);
3036 RTA_PUT(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
3037 nlh
->nlmsg_len
= skb
->tail
- b
;
3042 skb_trim(skb
, b
- skb
->data
);
3046 static int inet6_fill_ifmcaddr(struct sk_buff
*skb
, struct ifmcaddr6
*ifmca
,
3047 u32 pid
, u32 seq
, int event
, u16 flags
)
3049 struct ifaddrmsg
*ifm
;
3050 struct nlmsghdr
*nlh
;
3051 struct ifa_cacheinfo ci
;
3052 unsigned char *b
= skb
->tail
;
3054 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*ifm
), flags
);
3055 ifm
= NLMSG_DATA(nlh
);
3056 ifm
->ifa_family
= AF_INET6
;
3057 ifm
->ifa_prefixlen
= 128;
3058 ifm
->ifa_flags
= IFA_F_PERMANENT
;
3059 ifm
->ifa_scope
= RT_SCOPE_UNIVERSE
;
3060 if (ipv6_addr_scope(&ifmca
->mca_addr
)&IFA_SITE
)
3061 ifm
->ifa_scope
= RT_SCOPE_SITE
;
3062 ifm
->ifa_index
= ifmca
->idev
->dev
->ifindex
;
3063 RTA_PUT(skb
, IFA_MULTICAST
, 16, &ifmca
->mca_addr
);
3064 ci
.cstamp
= (__u32
)(TIME_DELTA(ifmca
->mca_cstamp
, INITIAL_JIFFIES
) / HZ
3065 * 100 + TIME_DELTA(ifmca
->mca_cstamp
, INITIAL_JIFFIES
) % HZ
3067 ci
.tstamp
= (__u32
)(TIME_DELTA(ifmca
->mca_tstamp
, INITIAL_JIFFIES
) / HZ
3068 * 100 + TIME_DELTA(ifmca
->mca_tstamp
, INITIAL_JIFFIES
) % HZ
3070 ci
.ifa_prefered
= INFINITY_LIFE_TIME
;
3071 ci
.ifa_valid
= INFINITY_LIFE_TIME
;
3072 RTA_PUT(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
3073 nlh
->nlmsg_len
= skb
->tail
- b
;
3078 skb_trim(skb
, b
- skb
->data
);
3082 static int inet6_fill_ifacaddr(struct sk_buff
*skb
, struct ifacaddr6
*ifaca
,
3083 u32 pid
, u32 seq
, int event
, unsigned int flags
)
3085 struct ifaddrmsg
*ifm
;
3086 struct nlmsghdr
*nlh
;
3087 struct ifa_cacheinfo ci
;
3088 unsigned char *b
= skb
->tail
;
3090 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*ifm
), flags
);
3091 ifm
= NLMSG_DATA(nlh
);
3092 ifm
->ifa_family
= AF_INET6
;
3093 ifm
->ifa_prefixlen
= 128;
3094 ifm
->ifa_flags
= IFA_F_PERMANENT
;
3095 ifm
->ifa_scope
= RT_SCOPE_UNIVERSE
;
3096 if (ipv6_addr_scope(&ifaca
->aca_addr
)&IFA_SITE
)
3097 ifm
->ifa_scope
= RT_SCOPE_SITE
;
3098 ifm
->ifa_index
= ifaca
->aca_idev
->dev
->ifindex
;
3099 RTA_PUT(skb
, IFA_ANYCAST
, 16, &ifaca
->aca_addr
);
3100 ci
.cstamp
= (__u32
)(TIME_DELTA(ifaca
->aca_cstamp
, INITIAL_JIFFIES
) / HZ
3101 * 100 + TIME_DELTA(ifaca
->aca_cstamp
, INITIAL_JIFFIES
) % HZ
3103 ci
.tstamp
= (__u32
)(TIME_DELTA(ifaca
->aca_tstamp
, INITIAL_JIFFIES
) / HZ
3104 * 100 + TIME_DELTA(ifaca
->aca_tstamp
, INITIAL_JIFFIES
) % HZ
3106 ci
.ifa_prefered
= INFINITY_LIFE_TIME
;
3107 ci
.ifa_valid
= INFINITY_LIFE_TIME
;
3108 RTA_PUT(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
3109 nlh
->nlmsg_len
= skb
->tail
- b
;
3114 skb_trim(skb
, b
- skb
->data
);
3125 static int inet6_dump_addr(struct sk_buff
*skb
, struct netlink_callback
*cb
,
3126 enum addr_type_t type
)
3129 int s_idx
, s_ip_idx
;
3131 struct net_device
*dev
;
3132 struct inet6_dev
*idev
= NULL
;
3133 struct inet6_ifaddr
*ifa
;
3134 struct ifmcaddr6
*ifmca
;
3135 struct ifacaddr6
*ifaca
;
3137 s_idx
= cb
->args
[0];
3138 s_ip_idx
= ip_idx
= cb
->args
[1];
3139 read_lock(&dev_base_lock
);
3141 for (dev
= dev_base
, idx
= 0; dev
; dev
= dev
->next
, idx
++) {
3147 if ((idev
= in6_dev_get(dev
)) == NULL
)
3149 read_lock_bh(&idev
->lock
);
3152 /* unicast address incl. temp addr */
3153 for (ifa
= idev
->addr_list
; ifa
;
3154 ifa
= ifa
->if_next
, ip_idx
++) {
3155 if (ip_idx
< s_ip_idx
)
3157 if ((err
= inet6_fill_ifaddr(skb
, ifa
,
3158 NETLINK_CB(cb
->skb
).pid
,
3159 cb
->nlh
->nlmsg_seq
, RTM_NEWADDR
,
3164 case MULTICAST_ADDR
:
3165 /* multicast address */
3166 for (ifmca
= idev
->mc_list
; ifmca
;
3167 ifmca
= ifmca
->next
, ip_idx
++) {
3168 if (ip_idx
< s_ip_idx
)
3170 if ((err
= inet6_fill_ifmcaddr(skb
, ifmca
,
3171 NETLINK_CB(cb
->skb
).pid
,
3172 cb
->nlh
->nlmsg_seq
, RTM_GETMULTICAST
,
3178 /* anycast address */
3179 for (ifaca
= idev
->ac_list
; ifaca
;
3180 ifaca
= ifaca
->aca_next
, ip_idx
++) {
3181 if (ip_idx
< s_ip_idx
)
3183 if ((err
= inet6_fill_ifacaddr(skb
, ifaca
,
3184 NETLINK_CB(cb
->skb
).pid
,
3185 cb
->nlh
->nlmsg_seq
, RTM_GETANYCAST
,
3193 read_unlock_bh(&idev
->lock
);
3198 read_unlock_bh(&idev
->lock
);
3201 read_unlock(&dev_base_lock
);
3203 cb
->args
[1] = ip_idx
;
3207 static int inet6_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3209 enum addr_type_t type
= UNICAST_ADDR
;
3210 return inet6_dump_addr(skb
, cb
, type
);
3213 static int inet6_dump_ifmcaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3215 enum addr_type_t type
= MULTICAST_ADDR
;
3216 return inet6_dump_addr(skb
, cb
, type
);
3220 static int inet6_dump_ifacaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3222 enum addr_type_t type
= ANYCAST_ADDR
;
3223 return inet6_dump_addr(skb
, cb
, type
);
3226 static int inet6_rtm_getaddr(struct sk_buff
*in_skb
,
3227 struct nlmsghdr
* nlh
, void *arg
)
3229 struct rtattr
**rta
= arg
;
3230 struct ifaddrmsg
*ifm
= NLMSG_DATA(nlh
);
3231 struct in6_addr
*addr
= NULL
;
3232 struct net_device
*dev
= NULL
;
3233 struct inet6_ifaddr
*ifa
;
3234 struct sk_buff
*skb
;
3235 int size
= NLMSG_SPACE(sizeof(struct ifaddrmsg
) + INET6_IFADDR_RTA_SPACE
);
3238 if (rta
[IFA_ADDRESS
-1]) {
3239 if (RTA_PAYLOAD(rta
[IFA_ADDRESS
-1]) < sizeof(*addr
))
3241 addr
= RTA_DATA(rta
[IFA_ADDRESS
-1]);
3243 if (rta
[IFA_LOCAL
-1]) {
3244 if (RTA_PAYLOAD(rta
[IFA_LOCAL
-1]) < sizeof(*addr
) ||
3245 (addr
&& memcmp(addr
, RTA_DATA(rta
[IFA_LOCAL
-1]), sizeof(*addr
))))
3247 addr
= RTA_DATA(rta
[IFA_LOCAL
-1]);
3253 dev
= __dev_get_by_index(ifm
->ifa_index
);
3255 if ((ifa
= ipv6_get_ifaddr(addr
, dev
, 1)) == NULL
)
3256 return -EADDRNOTAVAIL
;
3258 if ((skb
= alloc_skb(size
, GFP_KERNEL
)) == NULL
) {
3263 NETLINK_CB(skb
).dst_pid
= NETLINK_CB(in_skb
).pid
;
3264 err
= inet6_fill_ifaddr(skb
, ifa
, NETLINK_CB(in_skb
).pid
,
3265 nlh
->nlmsg_seq
, RTM_NEWADDR
, 0);
3271 err
= netlink_unicast(rtnl
, skb
, NETLINK_CB(in_skb
).pid
, MSG_DONTWAIT
);
3282 static void inet6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
)
3284 struct sk_buff
*skb
;
3285 int size
= NLMSG_SPACE(sizeof(struct ifaddrmsg
) + INET6_IFADDR_RTA_SPACE
);
3287 skb
= alloc_skb(size
, GFP_ATOMIC
);
3289 netlink_set_err(rtnl
, 0, RTNLGRP_IPV6_IFADDR
, ENOBUFS
);
3292 if (inet6_fill_ifaddr(skb
, ifa
, current
->pid
, 0, event
, 0) < 0) {
3294 netlink_set_err(rtnl
, 0, RTNLGRP_IPV6_IFADDR
, EINVAL
);
3297 NETLINK_CB(skb
).dst_group
= RTNLGRP_IPV6_IFADDR
;
3298 netlink_broadcast(rtnl
, skb
, 0, RTNLGRP_IPV6_IFADDR
, GFP_ATOMIC
);
3301 static void inline ipv6_store_devconf(struct ipv6_devconf
*cnf
,
3302 __s32
*array
, int bytes
)
3304 memset(array
, 0, bytes
);
3305 array
[DEVCONF_FORWARDING
] = cnf
->forwarding
;
3306 array
[DEVCONF_HOPLIMIT
] = cnf
->hop_limit
;
3307 array
[DEVCONF_MTU6
] = cnf
->mtu6
;
3308 array
[DEVCONF_ACCEPT_RA
] = cnf
->accept_ra
;
3309 array
[DEVCONF_ACCEPT_REDIRECTS
] = cnf
->accept_redirects
;
3310 array
[DEVCONF_AUTOCONF
] = cnf
->autoconf
;
3311 array
[DEVCONF_DAD_TRANSMITS
] = cnf
->dad_transmits
;
3312 array
[DEVCONF_RTR_SOLICITS
] = cnf
->rtr_solicits
;
3313 array
[DEVCONF_RTR_SOLICIT_INTERVAL
] = cnf
->rtr_solicit_interval
;
3314 array
[DEVCONF_RTR_SOLICIT_DELAY
] = cnf
->rtr_solicit_delay
;
3315 array
[DEVCONF_FORCE_MLD_VERSION
] = cnf
->force_mld_version
;
3316 #ifdef CONFIG_IPV6_PRIVACY
3317 array
[DEVCONF_USE_TEMPADDR
] = cnf
->use_tempaddr
;
3318 array
[DEVCONF_TEMP_VALID_LFT
] = cnf
->temp_valid_lft
;
3319 array
[DEVCONF_TEMP_PREFERED_LFT
] = cnf
->temp_prefered_lft
;
3320 array
[DEVCONF_REGEN_MAX_RETRY
] = cnf
->regen_max_retry
;
3321 array
[DEVCONF_MAX_DESYNC_FACTOR
] = cnf
->max_desync_factor
;
3323 array
[DEVCONF_MAX_ADDRESSES
] = cnf
->max_addresses
;
3324 array
[DEVCONF_ACCEPT_RA_DEFRTR
] = cnf
->accept_ra_defrtr
;
3325 array
[DEVCONF_ACCEPT_RA_PINFO
] = cnf
->accept_ra_pinfo
;
3326 #ifdef CONFIG_IPV6_ROUTER_PREF
3327 array
[DEVCONF_ACCEPT_RA_RTR_PREF
] = cnf
->accept_ra_rtr_pref
;
3328 array
[DEVCONF_RTR_PROBE_INTERVAL
] = cnf
->rtr_probe_interval
;
3329 #ifdef CONFIV_IPV6_ROUTE_INFO
3330 array
[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN
] = cnf
->accept_ra_rt_info_max_plen
;
3335 /* Maximum length of ifinfomsg attributes */
3336 #define INET6_IFINFO_RTA_SPACE \
3337 RTA_SPACE(IFNAMSIZ) /* IFNAME */ + \
3338 RTA_SPACE(MAX_ADDR_LEN) /* ADDRESS */ + \
3339 RTA_SPACE(sizeof(u32)) /* MTU */ + \
3340 RTA_SPACE(sizeof(int)) /* LINK */ + \
3341 RTA_SPACE(0) /* PROTINFO */ + \
3342 RTA_SPACE(sizeof(u32)) /* FLAGS */ + \
3343 RTA_SPACE(sizeof(struct ifla_cacheinfo)) /* CACHEINFO */ + \
3344 RTA_SPACE(sizeof(__s32[DEVCONF_MAX])) /* CONF */
3346 static int inet6_fill_ifinfo(struct sk_buff
*skb
, struct inet6_dev
*idev
,
3347 u32 pid
, u32 seq
, int event
, unsigned int flags
)
3349 struct net_device
*dev
= idev
->dev
;
3350 __s32
*array
= NULL
;
3351 struct ifinfomsg
*r
;
3352 struct nlmsghdr
*nlh
;
3353 unsigned char *b
= skb
->tail
;
3354 struct rtattr
*subattr
;
3355 __u32 mtu
= dev
->mtu
;
3356 struct ifla_cacheinfo ci
;
3358 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*r
), flags
);
3359 r
= NLMSG_DATA(nlh
);
3360 r
->ifi_family
= AF_INET6
;
3362 r
->ifi_type
= dev
->type
;
3363 r
->ifi_index
= dev
->ifindex
;
3364 r
->ifi_flags
= dev_get_flags(dev
);
3367 RTA_PUT(skb
, IFLA_IFNAME
, strlen(dev
->name
)+1, dev
->name
);
3370 RTA_PUT(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
);
3372 RTA_PUT(skb
, IFLA_MTU
, sizeof(mtu
), &mtu
);
3373 if (dev
->ifindex
!= dev
->iflink
)
3374 RTA_PUT(skb
, IFLA_LINK
, sizeof(int), &dev
->iflink
);
3376 subattr
= (struct rtattr
*)skb
->tail
;
3378 RTA_PUT(skb
, IFLA_PROTINFO
, 0, NULL
);
3380 /* return the device flags */
3381 RTA_PUT(skb
, IFLA_INET6_FLAGS
, sizeof(__u32
), &idev
->if_flags
);
3383 /* return interface cacheinfo */
3384 ci
.max_reasm_len
= IPV6_MAXPLEN
;
3385 ci
.tstamp
= (__u32
)(TIME_DELTA(idev
->tstamp
, INITIAL_JIFFIES
) / HZ
* 100
3386 + TIME_DELTA(idev
->tstamp
, INITIAL_JIFFIES
) % HZ
* 100 / HZ
);
3387 ci
.reachable_time
= idev
->nd_parms
->reachable_time
;
3388 ci
.retrans_time
= idev
->nd_parms
->retrans_time
;
3389 RTA_PUT(skb
, IFLA_INET6_CACHEINFO
, sizeof(ci
), &ci
);
3391 /* return the device sysctl params */
3392 if ((array
= kmalloc(DEVCONF_MAX
* sizeof(*array
), GFP_ATOMIC
)) == NULL
)
3393 goto rtattr_failure
;
3394 ipv6_store_devconf(&idev
->cnf
, array
, DEVCONF_MAX
* sizeof(*array
));
3395 RTA_PUT(skb
, IFLA_INET6_CONF
, DEVCONF_MAX
* sizeof(*array
), array
);
3397 /* XXX - Statistics/MC not implemented */
3398 subattr
->rta_len
= skb
->tail
- (u8
*)subattr
;
3400 nlh
->nlmsg_len
= skb
->tail
- b
;
3407 skb_trim(skb
, b
- skb
->data
);
3411 static int inet6_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3414 int s_idx
= cb
->args
[0];
3415 struct net_device
*dev
;
3416 struct inet6_dev
*idev
;
3418 read_lock(&dev_base_lock
);
3419 for (dev
=dev_base
, idx
=0; dev
; dev
= dev
->next
, idx
++) {
3422 if ((idev
= in6_dev_get(dev
)) == NULL
)
3424 err
= inet6_fill_ifinfo(skb
, idev
, NETLINK_CB(cb
->skb
).pid
,
3425 cb
->nlh
->nlmsg_seq
, RTM_NEWLINK
, NLM_F_MULTI
);
3430 read_unlock(&dev_base_lock
);
3436 void inet6_ifinfo_notify(int event
, struct inet6_dev
*idev
)
3438 struct sk_buff
*skb
;
3439 int size
= NLMSG_SPACE(sizeof(struct ifinfomsg
) + INET6_IFINFO_RTA_SPACE
);
3441 skb
= alloc_skb(size
, GFP_ATOMIC
);
3443 netlink_set_err(rtnl
, 0, RTNLGRP_IPV6_IFINFO
, ENOBUFS
);
3446 if (inet6_fill_ifinfo(skb
, idev
, current
->pid
, 0, event
, 0) < 0) {
3448 netlink_set_err(rtnl
, 0, RTNLGRP_IPV6_IFINFO
, EINVAL
);
3451 NETLINK_CB(skb
).dst_group
= RTNLGRP_IPV6_IFINFO
;
3452 netlink_broadcast(rtnl
, skb
, 0, RTNLGRP_IPV6_IFINFO
, GFP_ATOMIC
);
3455 /* Maximum length of prefix_cacheinfo attributes */
3456 #define INET6_PREFIX_RTA_SPACE \
3457 RTA_SPACE(sizeof(((struct prefix_info *)NULL)->prefix)) /* ADDRESS */ + \
3458 RTA_SPACE(sizeof(struct prefix_cacheinfo)) /* CACHEINFO */
3460 static int inet6_fill_prefix(struct sk_buff
*skb
, struct inet6_dev
*idev
,
3461 struct prefix_info
*pinfo
, u32 pid
, u32 seq
,
3462 int event
, unsigned int flags
)
3464 struct prefixmsg
*pmsg
;
3465 struct nlmsghdr
*nlh
;
3466 unsigned char *b
= skb
->tail
;
3467 struct prefix_cacheinfo ci
;
3469 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*pmsg
), flags
);
3470 pmsg
= NLMSG_DATA(nlh
);
3471 pmsg
->prefix_family
= AF_INET6
;
3472 pmsg
->prefix_pad1
= 0;
3473 pmsg
->prefix_pad2
= 0;
3474 pmsg
->prefix_ifindex
= idev
->dev
->ifindex
;
3475 pmsg
->prefix_len
= pinfo
->prefix_len
;
3476 pmsg
->prefix_type
= pinfo
->type
;
3477 pmsg
->prefix_pad3
= 0;
3479 pmsg
->prefix_flags
= 0;
3481 pmsg
->prefix_flags
|= IF_PREFIX_ONLINK
;
3482 if (pinfo
->autoconf
)
3483 pmsg
->prefix_flags
|= IF_PREFIX_AUTOCONF
;
3485 RTA_PUT(skb
, PREFIX_ADDRESS
, sizeof(pinfo
->prefix
), &pinfo
->prefix
);
3487 ci
.preferred_time
= ntohl(pinfo
->prefered
);
3488 ci
.valid_time
= ntohl(pinfo
->valid
);
3489 RTA_PUT(skb
, PREFIX_CACHEINFO
, sizeof(ci
), &ci
);
3491 nlh
->nlmsg_len
= skb
->tail
- b
;
3496 skb_trim(skb
, b
- skb
->data
);
3500 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
3501 struct prefix_info
*pinfo
)
3503 struct sk_buff
*skb
;
3504 int size
= NLMSG_SPACE(sizeof(struct prefixmsg
) + INET6_PREFIX_RTA_SPACE
);
3506 skb
= alloc_skb(size
, GFP_ATOMIC
);
3508 netlink_set_err(rtnl
, 0, RTNLGRP_IPV6_PREFIX
, ENOBUFS
);
3511 if (inet6_fill_prefix(skb
, idev
, pinfo
, current
->pid
, 0, event
, 0) < 0) {
3513 netlink_set_err(rtnl
, 0, RTNLGRP_IPV6_PREFIX
, EINVAL
);
3516 NETLINK_CB(skb
).dst_group
= RTNLGRP_IPV6_PREFIX
;
3517 netlink_broadcast(rtnl
, skb
, 0, RTNLGRP_IPV6_PREFIX
, GFP_ATOMIC
);
3520 static struct rtnetlink_link inet6_rtnetlink_table
[RTM_NR_MSGTYPES
] = {
3521 [RTM_GETLINK
- RTM_BASE
] = { .dumpit
= inet6_dump_ifinfo
, },
3522 [RTM_NEWADDR
- RTM_BASE
] = { .doit
= inet6_rtm_newaddr
, },
3523 [RTM_DELADDR
- RTM_BASE
] = { .doit
= inet6_rtm_deladdr
, },
3524 [RTM_GETADDR
- RTM_BASE
] = { .doit
= inet6_rtm_getaddr
,
3525 .dumpit
= inet6_dump_ifaddr
, },
3526 [RTM_GETMULTICAST
- RTM_BASE
] = { .dumpit
= inet6_dump_ifmcaddr
, },
3527 [RTM_GETANYCAST
- RTM_BASE
] = { .dumpit
= inet6_dump_ifacaddr
, },
3528 [RTM_NEWROUTE
- RTM_BASE
] = { .doit
= inet6_rtm_newroute
, },
3529 [RTM_DELROUTE
- RTM_BASE
] = { .doit
= inet6_rtm_delroute
, },
3530 [RTM_GETROUTE
- RTM_BASE
] = { .doit
= inet6_rtm_getroute
,
3531 .dumpit
= inet6_dump_fib
, },
3532 [RTM_GETRULE
- RTM_BASE
] = { .dumpit
= fib6_rules_dump
, },
3535 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
3537 inet6_ifa_notify(event
? : RTM_NEWADDR
, ifp
);
3541 ip6_ins_rt(ifp
->rt
, NULL
, NULL
, NULL
);
3542 if (ifp
->idev
->cnf
.forwarding
)
3543 addrconf_join_anycast(ifp
);
3546 if (ifp
->idev
->cnf
.forwarding
)
3547 addrconf_leave_anycast(ifp
);
3548 addrconf_leave_solict(ifp
->idev
, &ifp
->addr
);
3549 dst_hold(&ifp
->rt
->u
.dst
);
3550 if (ip6_del_rt(ifp
->rt
, NULL
, NULL
, NULL
))
3551 dst_free(&ifp
->rt
->u
.dst
);
3556 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
3558 read_lock_bh(&addrconf_lock
);
3559 if (likely(ifp
->idev
->dead
== 0))
3560 __ipv6_ifa_notify(event
, ifp
);
3561 read_unlock_bh(&addrconf_lock
);
3564 #ifdef CONFIG_SYSCTL
3567 int addrconf_sysctl_forward(ctl_table
*ctl
, int write
, struct file
* filp
,
3568 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
3570 int *valp
= ctl
->data
;
3574 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
3576 if (write
&& valp
!= &ipv6_devconf_dflt
.forwarding
) {
3577 if (valp
!= &ipv6_devconf
.forwarding
) {
3578 if ((!*valp
) ^ (!val
)) {
3579 struct inet6_dev
*idev
= (struct inet6_dev
*)ctl
->extra1
;
3582 dev_forward_change(idev
);
3585 ipv6_devconf_dflt
.forwarding
= ipv6_devconf
.forwarding
;
3586 addrconf_forward_change();
3589 rt6_purge_dflt_routers();
3595 static int addrconf_sysctl_forward_strategy(ctl_table
*table
,
3596 int __user
*name
, int nlen
,
3597 void __user
*oldval
,
3598 size_t __user
*oldlenp
,
3599 void __user
*newval
, size_t newlen
,
3602 int *valp
= table
->data
;
3605 if (!newval
|| !newlen
)
3607 if (newlen
!= sizeof(int))
3609 if (get_user(new, (int __user
*)newval
))
3613 if (oldval
&& oldlenp
) {
3615 if (get_user(len
, oldlenp
))
3618 if (len
> table
->maxlen
)
3619 len
= table
->maxlen
;
3620 if (copy_to_user(oldval
, valp
, len
))
3622 if (put_user(len
, oldlenp
))
3627 if (valp
!= &ipv6_devconf_dflt
.forwarding
) {
3628 if (valp
!= &ipv6_devconf
.forwarding
) {
3629 struct inet6_dev
*idev
= (struct inet6_dev
*)table
->extra1
;
3631 if (unlikely(idev
== NULL
))
3633 changed
= (!*valp
) ^ (!new);
3636 dev_forward_change(idev
);
3639 addrconf_forward_change();
3643 rt6_purge_dflt_routers();
3650 static struct addrconf_sysctl_table
3652 struct ctl_table_header
*sysctl_header
;
3653 ctl_table addrconf_vars
[__NET_IPV6_MAX
];
3654 ctl_table addrconf_dev
[2];
3655 ctl_table addrconf_conf_dir
[2];
3656 ctl_table addrconf_proto_dir
[2];
3657 ctl_table addrconf_root_dir
[2];
3658 } addrconf_sysctl
= {
3659 .sysctl_header
= NULL
,
3662 .ctl_name
= NET_IPV6_FORWARDING
,
3663 .procname
= "forwarding",
3664 .data
= &ipv6_devconf
.forwarding
,
3665 .maxlen
= sizeof(int),
3667 .proc_handler
= &addrconf_sysctl_forward
,
3668 .strategy
= &addrconf_sysctl_forward_strategy
,
3671 .ctl_name
= NET_IPV6_HOP_LIMIT
,
3672 .procname
= "hop_limit",
3673 .data
= &ipv6_devconf
.hop_limit
,
3674 .maxlen
= sizeof(int),
3676 .proc_handler
= proc_dointvec
,
3679 .ctl_name
= NET_IPV6_MTU
,
3681 .data
= &ipv6_devconf
.mtu6
,
3682 .maxlen
= sizeof(int),
3684 .proc_handler
= &proc_dointvec
,
3687 .ctl_name
= NET_IPV6_ACCEPT_RA
,
3688 .procname
= "accept_ra",
3689 .data
= &ipv6_devconf
.accept_ra
,
3690 .maxlen
= sizeof(int),
3692 .proc_handler
= &proc_dointvec
,
3695 .ctl_name
= NET_IPV6_ACCEPT_REDIRECTS
,
3696 .procname
= "accept_redirects",
3697 .data
= &ipv6_devconf
.accept_redirects
,
3698 .maxlen
= sizeof(int),
3700 .proc_handler
= &proc_dointvec
,
3703 .ctl_name
= NET_IPV6_AUTOCONF
,
3704 .procname
= "autoconf",
3705 .data
= &ipv6_devconf
.autoconf
,
3706 .maxlen
= sizeof(int),
3708 .proc_handler
= &proc_dointvec
,
3711 .ctl_name
= NET_IPV6_DAD_TRANSMITS
,
3712 .procname
= "dad_transmits",
3713 .data
= &ipv6_devconf
.dad_transmits
,
3714 .maxlen
= sizeof(int),
3716 .proc_handler
= &proc_dointvec
,
3719 .ctl_name
= NET_IPV6_RTR_SOLICITS
,
3720 .procname
= "router_solicitations",
3721 .data
= &ipv6_devconf
.rtr_solicits
,
3722 .maxlen
= sizeof(int),
3724 .proc_handler
= &proc_dointvec
,
3727 .ctl_name
= NET_IPV6_RTR_SOLICIT_INTERVAL
,
3728 .procname
= "router_solicitation_interval",
3729 .data
= &ipv6_devconf
.rtr_solicit_interval
,
3730 .maxlen
= sizeof(int),
3732 .proc_handler
= &proc_dointvec_jiffies
,
3733 .strategy
= &sysctl_jiffies
,
3736 .ctl_name
= NET_IPV6_RTR_SOLICIT_DELAY
,
3737 .procname
= "router_solicitation_delay",
3738 .data
= &ipv6_devconf
.rtr_solicit_delay
,
3739 .maxlen
= sizeof(int),
3741 .proc_handler
= &proc_dointvec_jiffies
,
3742 .strategy
= &sysctl_jiffies
,
3745 .ctl_name
= NET_IPV6_FORCE_MLD_VERSION
,
3746 .procname
= "force_mld_version",
3747 .data
= &ipv6_devconf
.force_mld_version
,
3748 .maxlen
= sizeof(int),
3750 .proc_handler
= &proc_dointvec
,
3752 #ifdef CONFIG_IPV6_PRIVACY
3754 .ctl_name
= NET_IPV6_USE_TEMPADDR
,
3755 .procname
= "use_tempaddr",
3756 .data
= &ipv6_devconf
.use_tempaddr
,
3757 .maxlen
= sizeof(int),
3759 .proc_handler
= &proc_dointvec
,
3762 .ctl_name
= NET_IPV6_TEMP_VALID_LFT
,
3763 .procname
= "temp_valid_lft",
3764 .data
= &ipv6_devconf
.temp_valid_lft
,
3765 .maxlen
= sizeof(int),
3767 .proc_handler
= &proc_dointvec
,
3770 .ctl_name
= NET_IPV6_TEMP_PREFERED_LFT
,
3771 .procname
= "temp_prefered_lft",
3772 .data
= &ipv6_devconf
.temp_prefered_lft
,
3773 .maxlen
= sizeof(int),
3775 .proc_handler
= &proc_dointvec
,
3778 .ctl_name
= NET_IPV6_REGEN_MAX_RETRY
,
3779 .procname
= "regen_max_retry",
3780 .data
= &ipv6_devconf
.regen_max_retry
,
3781 .maxlen
= sizeof(int),
3783 .proc_handler
= &proc_dointvec
,
3786 .ctl_name
= NET_IPV6_MAX_DESYNC_FACTOR
,
3787 .procname
= "max_desync_factor",
3788 .data
= &ipv6_devconf
.max_desync_factor
,
3789 .maxlen
= sizeof(int),
3791 .proc_handler
= &proc_dointvec
,
3795 .ctl_name
= NET_IPV6_MAX_ADDRESSES
,
3796 .procname
= "max_addresses",
3797 .data
= &ipv6_devconf
.max_addresses
,
3798 .maxlen
= sizeof(int),
3800 .proc_handler
= &proc_dointvec
,
3803 .ctl_name
= NET_IPV6_ACCEPT_RA_DEFRTR
,
3804 .procname
= "accept_ra_defrtr",
3805 .data
= &ipv6_devconf
.accept_ra_defrtr
,
3806 .maxlen
= sizeof(int),
3808 .proc_handler
= &proc_dointvec
,
3811 .ctl_name
= NET_IPV6_ACCEPT_RA_PINFO
,
3812 .procname
= "accept_ra_pinfo",
3813 .data
= &ipv6_devconf
.accept_ra_pinfo
,
3814 .maxlen
= sizeof(int),
3816 .proc_handler
= &proc_dointvec
,
3818 #ifdef CONFIG_IPV6_ROUTER_PREF
3820 .ctl_name
= NET_IPV6_ACCEPT_RA_RTR_PREF
,
3821 .procname
= "accept_ra_rtr_pref",
3822 .data
= &ipv6_devconf
.accept_ra_rtr_pref
,
3823 .maxlen
= sizeof(int),
3825 .proc_handler
= &proc_dointvec
,
3828 .ctl_name
= NET_IPV6_RTR_PROBE_INTERVAL
,
3829 .procname
= "router_probe_interval",
3830 .data
= &ipv6_devconf
.rtr_probe_interval
,
3831 .maxlen
= sizeof(int),
3833 .proc_handler
= &proc_dointvec_jiffies
,
3834 .strategy
= &sysctl_jiffies
,
3836 #ifdef CONFIV_IPV6_ROUTE_INFO
3838 .ctl_name
= NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN
,
3839 .procname
= "accept_ra_rt_info_max_plen",
3840 .data
= &ipv6_devconf
.accept_ra_rt_info_max_plen
,
3841 .maxlen
= sizeof(int),
3843 .proc_handler
= &proc_dointvec
,
3848 .ctl_name
= 0, /* sentinel */
3853 .ctl_name
= NET_PROTO_CONF_ALL
,
3856 .child
= addrconf_sysctl
.addrconf_vars
,
3859 .ctl_name
= 0, /* sentinel */
3862 .addrconf_conf_dir
= {
3864 .ctl_name
= NET_IPV6_CONF
,
3867 .child
= addrconf_sysctl
.addrconf_dev
,
3870 .ctl_name
= 0, /* sentinel */
3873 .addrconf_proto_dir
= {
3875 .ctl_name
= NET_IPV6
,
3878 .child
= addrconf_sysctl
.addrconf_conf_dir
,
3881 .ctl_name
= 0, /* sentinel */
3884 .addrconf_root_dir
= {
3886 .ctl_name
= CTL_NET
,
3889 .child
= addrconf_sysctl
.addrconf_proto_dir
,
3892 .ctl_name
= 0, /* sentinel */
3897 static void addrconf_sysctl_register(struct inet6_dev
*idev
, struct ipv6_devconf
*p
)
3900 struct net_device
*dev
= idev
? idev
->dev
: NULL
;
3901 struct addrconf_sysctl_table
*t
;
3902 char *dev_name
= NULL
;
3904 t
= kmalloc(sizeof(*t
), GFP_KERNEL
);
3907 memcpy(t
, &addrconf_sysctl
, sizeof(*t
));
3908 for (i
=0; t
->addrconf_vars
[i
].data
; i
++) {
3909 t
->addrconf_vars
[i
].data
+= (char*)p
- (char*)&ipv6_devconf
;
3910 t
->addrconf_vars
[i
].de
= NULL
;
3911 t
->addrconf_vars
[i
].extra1
= idev
; /* embedded; no ref */
3914 dev_name
= dev
->name
;
3915 t
->addrconf_dev
[0].ctl_name
= dev
->ifindex
;
3917 dev_name
= "default";
3918 t
->addrconf_dev
[0].ctl_name
= NET_PROTO_CONF_DEFAULT
;
3922 * Make a copy of dev_name, because '.procname' is regarded as const
3923 * by sysctl and we wouldn't want anyone to change it under our feet
3924 * (see SIOCSIFNAME).
3926 dev_name
= kstrdup(dev_name
, GFP_KERNEL
);
3930 t
->addrconf_dev
[0].procname
= dev_name
;
3932 t
->addrconf_dev
[0].child
= t
->addrconf_vars
;
3933 t
->addrconf_dev
[0].de
= NULL
;
3934 t
->addrconf_conf_dir
[0].child
= t
->addrconf_dev
;
3935 t
->addrconf_conf_dir
[0].de
= NULL
;
3936 t
->addrconf_proto_dir
[0].child
= t
->addrconf_conf_dir
;
3937 t
->addrconf_proto_dir
[0].de
= NULL
;
3938 t
->addrconf_root_dir
[0].child
= t
->addrconf_proto_dir
;
3939 t
->addrconf_root_dir
[0].de
= NULL
;
3941 t
->sysctl_header
= register_sysctl_table(t
->addrconf_root_dir
, 0);
3942 if (t
->sysctl_header
== NULL
)
3957 static void addrconf_sysctl_unregister(struct ipv6_devconf
*p
)
3960 struct addrconf_sysctl_table
*t
= p
->sysctl
;
3962 unregister_sysctl_table(t
->sysctl_header
);
3963 kfree(t
->addrconf_dev
[0].procname
);
3975 int register_inet6addr_notifier(struct notifier_block
*nb
)
3977 return atomic_notifier_chain_register(&inet6addr_chain
, nb
);
3980 int unregister_inet6addr_notifier(struct notifier_block
*nb
)
3982 return atomic_notifier_chain_unregister(&inet6addr_chain
,nb
);
3986 * Init / cleanup code
3989 int __init
addrconf_init(void)
3993 /* The addrconf netdev notifier requires that loopback_dev
3994 * has it's ipv6 private information allocated and setup
3995 * before it can bring up and give link-local addresses
3996 * to other devices which are up.
3998 * Unfortunately, loopback_dev is not necessarily the first
3999 * entry in the global dev_base list of net devices. In fact,
4000 * it is likely to be the very last entry on that list.
4001 * So this causes the notifier registry below to try and
4002 * give link-local addresses to all devices besides loopback_dev
4003 * first, then loopback_dev, which cases all the non-loopback_dev
4004 * devices to fail to get a link-local address.
4006 * So, as a temporary fix, allocate the ipv6 structure for
4007 * loopback_dev first by hand.
4008 * Longer term, all of the dependencies ipv6 has upon the loopback
4009 * device and it being up should be removed.
4012 if (!ipv6_add_dev(&loopback_dev
))
4018 ip6_null_entry
.rt6i_idev
= in6_dev_get(&loopback_dev
);
4020 register_netdevice_notifier(&ipv6_dev_notf
);
4023 rtnetlink_links
[PF_INET6
] = inet6_rtnetlink_table
;
4024 #ifdef CONFIG_SYSCTL
4025 addrconf_sysctl
.sysctl_header
=
4026 register_sysctl_table(addrconf_sysctl
.addrconf_root_dir
, 0);
4027 addrconf_sysctl_register(NULL
, &ipv6_devconf_dflt
);
4033 void __exit
addrconf_cleanup(void)
4035 struct net_device
*dev
;
4036 struct inet6_dev
*idev
;
4037 struct inet6_ifaddr
*ifa
;
4040 unregister_netdevice_notifier(&ipv6_dev_notf
);
4042 rtnetlink_links
[PF_INET6
] = NULL
;
4043 #ifdef CONFIG_SYSCTL
4044 addrconf_sysctl_unregister(&ipv6_devconf_dflt
);
4045 addrconf_sysctl_unregister(&ipv6_devconf
);
4054 for (dev
=dev_base
; dev
; dev
=dev
->next
) {
4055 if ((idev
= __in6_dev_get(dev
)) == NULL
)
4057 addrconf_ifdown(dev
, 1);
4059 addrconf_ifdown(&loopback_dev
, 2);
4065 write_lock_bh(&addrconf_hash_lock
);
4066 for (i
=0; i
< IN6_ADDR_HSIZE
; i
++) {
4067 for (ifa
=inet6_addr_lst
[i
]; ifa
; ) {
4068 struct inet6_ifaddr
*bifa
;
4071 ifa
= ifa
->lst_next
;
4072 printk(KERN_DEBUG
"bug: IPv6 address leakage detected: ifa=%p\n", bifa
);
4073 /* Do not free it; something is wrong.
4074 Now we can investigate it with debugger.
4078 write_unlock_bh(&addrconf_hash_lock
);
4080 del_timer(&addr_chk_timer
);
4084 #ifdef CONFIG_PROC_FS
4085 proc_net_remove("if_inet6");