2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
20 * Janos Farkas : delete timer on ifdown
21 * <chexum@bankinf.banki.hu>
22 * Andi Kleen : kill double kfree on module
24 * Maciej W. Rozycki : FDDI support
25 * sekiya@USAGI : Don't send too many RS
27 * yoshfuji@USAGI : Fixed interval between DAD
29 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
30 * address validation timer.
31 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
33 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
34 * address on a same interface.
35 * YOSHIFUJI Hideaki @USAGI : ARCnet support
36 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
38 * YOSHIFUJI Hideaki @USAGI : improved source address
39 * selection; consider scope,
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/socket.h>
46 #include <linux/sockios.h>
47 #include <linux/net.h>
48 #include <linux/in6.h>
49 #include <linux/netdevice.h>
50 #include <linux/if_addr.h>
51 #include <linux/if_arp.h>
52 #include <linux/if_arcnet.h>
53 #include <linux/if_infiniband.h>
54 #include <linux/route.h>
55 #include <linux/inetdevice.h>
56 #include <linux/init.h>
58 #include <linux/sysctl.h>
60 #include <linux/capability.h>
61 #include <linux/delay.h>
62 #include <linux/notifier.h>
63 #include <linux/string.h>
69 #include <net/protocol.h>
70 #include <net/ndisc.h>
71 #include <net/ip6_route.h>
72 #include <net/addrconf.h>
75 #include <net/netlink.h>
76 #include <linux/if_tunnel.h>
77 #include <linux/rtnetlink.h>
79 #ifdef CONFIG_IPV6_PRIVACY
80 #include <linux/random.h>
83 #include <asm/uaccess.h>
85 #include <linux/proc_fs.h>
86 #include <linux/seq_file.h>
88 /* Set to 3 to get tracing... */
92 #define ADBG(x) printk x
97 #define INFINITY_LIFE_TIME 0xFFFFFFFF
98 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
101 static void addrconf_sysctl_register(struct inet6_dev
*idev
, struct ipv6_devconf
*p
);
102 static void addrconf_sysctl_unregister(struct ipv6_devconf
*p
);
105 #ifdef CONFIG_IPV6_PRIVACY
106 static int __ipv6_regen_rndid(struct inet6_dev
*idev
);
107 static int __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
);
108 static void ipv6_regen_rndid(unsigned long data
);
110 static int desync_factor
= MAX_DESYNC_FACTOR
* HZ
;
113 static int ipv6_count_addresses(struct inet6_dev
*idev
);
116 * Configured unicast address hash table
118 static struct inet6_ifaddr
*inet6_addr_lst
[IN6_ADDR_HSIZE
];
119 static DEFINE_RWLOCK(addrconf_hash_lock
);
121 static void addrconf_verify(unsigned long);
123 static DEFINE_TIMER(addr_chk_timer
, addrconf_verify
, 0, 0);
124 static DEFINE_SPINLOCK(addrconf_verify_lock
);
126 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
);
127 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
);
129 static int addrconf_ifdown(struct net_device
*dev
, int how
);
131 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
, u32 flags
);
132 static void addrconf_dad_timer(unsigned long data
);
133 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
);
134 static void addrconf_dad_run(struct inet6_dev
*idev
);
135 static void addrconf_rs_timer(unsigned long data
);
136 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
137 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
139 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
140 struct prefix_info
*pinfo
);
141 static int ipv6_chk_same_addr(const struct in6_addr
*addr
, struct net_device
*dev
);
143 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain
);
145 struct ipv6_devconf ipv6_devconf __read_mostly
= {
147 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
148 .mtu6
= IPV6_MIN_MTU
,
150 .accept_redirects
= 1,
152 .force_mld_version
= 0,
154 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
155 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
156 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
157 #ifdef CONFIG_IPV6_PRIVACY
159 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
160 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
161 .regen_max_retry
= REGEN_MAX_RETRY
,
162 .max_desync_factor
= MAX_DESYNC_FACTOR
,
164 .max_addresses
= IPV6_MAX_ADDRESSES
,
165 .accept_ra_defrtr
= 1,
166 .accept_ra_pinfo
= 1,
167 #ifdef CONFIG_IPV6_ROUTER_PREF
168 .accept_ra_rtr_pref
= 1,
169 .rtr_probe_interval
= 60 * HZ
,
170 #ifdef CONFIG_IPV6_ROUTE_INFO
171 .accept_ra_rt_info_max_plen
= 0,
175 .accept_source_route
= 0, /* we do not accept RH0 by default. */
178 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly
= {
180 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
181 .mtu6
= IPV6_MIN_MTU
,
183 .accept_redirects
= 1,
186 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
187 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
188 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
189 #ifdef CONFIG_IPV6_PRIVACY
191 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
192 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
193 .regen_max_retry
= REGEN_MAX_RETRY
,
194 .max_desync_factor
= MAX_DESYNC_FACTOR
,
196 .max_addresses
= IPV6_MAX_ADDRESSES
,
197 .accept_ra_defrtr
= 1,
198 .accept_ra_pinfo
= 1,
199 #ifdef CONFIG_IPV6_ROUTER_PREF
200 .accept_ra_rtr_pref
= 1,
201 .rtr_probe_interval
= 60 * HZ
,
202 #ifdef CONFIG_IPV6_ROUTE_INFO
203 .accept_ra_rt_info_max_plen
= 0,
207 .accept_source_route
= 0, /* we do not accept RH0 by default. */
210 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
212 const struct in6_addr in6addr_any
= IN6ADDR_ANY_INIT
;
214 const struct in6_addr in6addr_loopback
= IN6ADDR_LOOPBACK_INIT
;
216 static void addrconf_del_timer(struct inet6_ifaddr
*ifp
)
218 if (del_timer(&ifp
->timer
))
222 enum addrconf_timer_t
229 static void addrconf_mod_timer(struct inet6_ifaddr
*ifp
,
230 enum addrconf_timer_t what
,
233 if (!del_timer(&ifp
->timer
))
238 ifp
->timer
.function
= addrconf_dad_timer
;
241 ifp
->timer
.function
= addrconf_rs_timer
;
245 ifp
->timer
.expires
= jiffies
+ when
;
246 add_timer(&ifp
->timer
);
249 /* Nobody refers to this device, we may destroy it. */
251 static void in6_dev_finish_destroy_rcu(struct rcu_head
*head
)
253 struct inet6_dev
*idev
= container_of(head
, struct inet6_dev
, rcu
);
257 void in6_dev_finish_destroy(struct inet6_dev
*idev
)
259 struct net_device
*dev
= idev
->dev
;
260 BUG_TRAP(idev
->addr_list
==NULL
);
261 BUG_TRAP(idev
->mc_list
==NULL
);
262 #ifdef NET_REFCNT_DEBUG
263 printk(KERN_DEBUG
"in6_dev_finish_destroy: %s\n", dev
? dev
->name
: "NIL");
267 printk("Freeing alive inet6 device %p\n", idev
);
270 snmp6_free_dev(idev
);
271 call_rcu(&idev
->rcu
, in6_dev_finish_destroy_rcu
);
274 static struct inet6_dev
* ipv6_add_dev(struct net_device
*dev
)
276 struct inet6_dev
*ndev
;
277 struct in6_addr maddr
;
281 if (dev
->mtu
< IPV6_MIN_MTU
)
284 ndev
= kzalloc(sizeof(struct inet6_dev
), GFP_KERNEL
);
289 rwlock_init(&ndev
->lock
);
291 memcpy(&ndev
->cnf
, &ipv6_devconf_dflt
, sizeof(ndev
->cnf
));
292 ndev
->cnf
.mtu6
= dev
->mtu
;
293 ndev
->cnf
.sysctl
= NULL
;
294 ndev
->nd_parms
= neigh_parms_alloc(dev
, &nd_tbl
);
295 if (ndev
->nd_parms
== NULL
) {
299 /* We refer to the device */
302 if (snmp6_alloc_dev(ndev
) < 0) {
304 "%s(): cannot allocate memory for statistics; dev=%s.\n",
305 __FUNCTION__
, dev
->name
));
306 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
308 in6_dev_finish_destroy(ndev
);
312 if (snmp6_register_dev(ndev
) < 0) {
314 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
315 __FUNCTION__
, dev
->name
));
316 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
318 in6_dev_finish_destroy(ndev
);
322 /* One reference from device. We must do this before
323 * we invoke __ipv6_regen_rndid().
327 #ifdef CONFIG_IPV6_PRIVACY
328 init_timer(&ndev
->regen_timer
);
329 ndev
->regen_timer
.function
= ipv6_regen_rndid
;
330 ndev
->regen_timer
.data
= (unsigned long) ndev
;
331 if ((dev
->flags
&IFF_LOOPBACK
) ||
332 dev
->type
== ARPHRD_TUNNEL
||
333 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
334 dev
->type
== ARPHRD_SIT
||
336 dev
->type
== ARPHRD_NONE
) {
338 "%s: Disabled Privacy Extensions\n",
340 ndev
->cnf
.use_tempaddr
= -1;
343 ipv6_regen_rndid((unsigned long) ndev
);
347 if (netif_running(dev
) && netif_carrier_ok(dev
))
348 ndev
->if_flags
|= IF_READY
;
350 ipv6_mc_init_dev(ndev
);
351 ndev
->tstamp
= jiffies
;
353 neigh_sysctl_register(dev
, ndev
->nd_parms
, NET_IPV6
,
354 NET_IPV6_NEIGH
, "ipv6",
355 &ndisc_ifinfo_sysctl_change
,
357 addrconf_sysctl_register(ndev
, &ndev
->cnf
);
359 /* protected by rtnl_lock */
360 rcu_assign_pointer(dev
->ip6_ptr
, ndev
);
362 /* Join all-node multicast group */
363 ipv6_addr_all_nodes(&maddr
);
364 ipv6_dev_mc_inc(dev
, &maddr
);
369 static struct inet6_dev
* ipv6_find_idev(struct net_device
*dev
)
371 struct inet6_dev
*idev
;
375 if ((idev
= __in6_dev_get(dev
)) == NULL
) {
376 if ((idev
= ipv6_add_dev(dev
)) == NULL
)
380 if (dev
->flags
&IFF_UP
)
386 static void dev_forward_change(struct inet6_dev
*idev
)
388 struct net_device
*dev
;
389 struct inet6_ifaddr
*ifa
;
390 struct in6_addr addr
;
395 if (dev
&& (dev
->flags
& IFF_MULTICAST
)) {
396 ipv6_addr_all_routers(&addr
);
398 if (idev
->cnf
.forwarding
)
399 ipv6_dev_mc_inc(dev
, &addr
);
401 ipv6_dev_mc_dec(dev
, &addr
);
403 for (ifa
=idev
->addr_list
; ifa
; ifa
=ifa
->if_next
) {
404 if (ifa
->flags
&IFA_F_TENTATIVE
)
406 if (idev
->cnf
.forwarding
)
407 addrconf_join_anycast(ifa
);
409 addrconf_leave_anycast(ifa
);
414 static void addrconf_forward_change(void)
416 struct net_device
*dev
;
417 struct inet6_dev
*idev
;
419 read_lock(&dev_base_lock
);
420 for (dev
=dev_base
; dev
; dev
=dev
->next
) {
422 idev
= __in6_dev_get(dev
);
424 int changed
= (!idev
->cnf
.forwarding
) ^ (!ipv6_devconf
.forwarding
);
425 idev
->cnf
.forwarding
= ipv6_devconf
.forwarding
;
427 dev_forward_change(idev
);
431 read_unlock(&dev_base_lock
);
435 /* Nobody refers to this ifaddr, destroy it */
437 void inet6_ifa_finish_destroy(struct inet6_ifaddr
*ifp
)
439 BUG_TRAP(ifp
->if_next
==NULL
);
440 BUG_TRAP(ifp
->lst_next
==NULL
);
441 #ifdef NET_REFCNT_DEBUG
442 printk(KERN_DEBUG
"inet6_ifa_finish_destroy\n");
445 in6_dev_put(ifp
->idev
);
447 if (del_timer(&ifp
->timer
))
448 printk("Timer is still running, when freeing ifa=%p\n", ifp
);
451 printk("Freeing alive inet6 address %p\n", ifp
);
454 dst_release(&ifp
->rt
->u
.dst
);
460 ipv6_link_dev_addr(struct inet6_dev
*idev
, struct inet6_ifaddr
*ifp
)
462 struct inet6_ifaddr
*ifa
, **ifap
;
463 int ifp_scope
= ipv6_addr_src_scope(&ifp
->addr
);
466 * Each device address list is sorted in order of scope -
467 * global before linklocal.
469 for (ifap
= &idev
->addr_list
; (ifa
= *ifap
) != NULL
;
470 ifap
= &ifa
->if_next
) {
471 if (ifp_scope
>= ipv6_addr_src_scope(&ifa
->addr
))
475 ifp
->if_next
= *ifap
;
479 /* On success it returns ifp with increased reference count */
481 static struct inet6_ifaddr
*
482 ipv6_add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
, int pfxlen
,
483 int scope
, u32 flags
)
485 struct inet6_ifaddr
*ifa
= NULL
;
492 err
= -ENODEV
; /*XXX*/
496 write_lock(&addrconf_hash_lock
);
498 /* Ignore adding duplicate addresses on an interface */
499 if (ipv6_chk_same_addr(addr
, idev
->dev
)) {
500 ADBG(("ipv6_add_addr: already assigned\n"));
505 ifa
= kzalloc(sizeof(struct inet6_ifaddr
), GFP_ATOMIC
);
508 ADBG(("ipv6_add_addr: malloc failed\n"));
513 rt
= addrconf_dst_alloc(idev
, addr
, 0);
519 ipv6_addr_copy(&ifa
->addr
, addr
);
521 spin_lock_init(&ifa
->lock
);
522 init_timer(&ifa
->timer
);
523 ifa
->timer
.data
= (unsigned long) ifa
;
525 ifa
->prefix_len
= pfxlen
;
526 ifa
->flags
= flags
| IFA_F_TENTATIVE
;
527 ifa
->cstamp
= ifa
->tstamp
= jiffies
;
536 /* Add to big hash table */
537 hash
= ipv6_addr_hash(addr
);
539 ifa
->lst_next
= inet6_addr_lst
[hash
];
540 inet6_addr_lst
[hash
] = ifa
;
542 write_unlock(&addrconf_hash_lock
);
544 write_lock(&idev
->lock
);
545 /* Add to inet6_dev unicast addr list. */
546 ipv6_link_dev_addr(idev
, ifa
);
548 #ifdef CONFIG_IPV6_PRIVACY
549 if (ifa
->flags
&IFA_F_TEMPORARY
) {
550 ifa
->tmp_next
= idev
->tempaddr_list
;
551 idev
->tempaddr_list
= ifa
;
557 write_unlock(&idev
->lock
);
559 rcu_read_unlock_bh();
561 if (likely(err
== 0))
562 atomic_notifier_call_chain(&inet6addr_chain
, NETDEV_UP
, ifa
);
570 write_unlock(&addrconf_hash_lock
);
574 /* This function wants to get referenced ifp and releases it before return */
576 static void ipv6_del_addr(struct inet6_ifaddr
*ifp
)
578 struct inet6_ifaddr
*ifa
, **ifap
;
579 struct inet6_dev
*idev
= ifp
->idev
;
581 int deleted
= 0, onlink
= 0;
582 unsigned long expires
= jiffies
;
584 hash
= ipv6_addr_hash(&ifp
->addr
);
588 write_lock_bh(&addrconf_hash_lock
);
589 for (ifap
= &inet6_addr_lst
[hash
]; (ifa
=*ifap
) != NULL
;
590 ifap
= &ifa
->lst_next
) {
592 *ifap
= ifa
->lst_next
;
594 ifa
->lst_next
= NULL
;
598 write_unlock_bh(&addrconf_hash_lock
);
600 write_lock_bh(&idev
->lock
);
601 #ifdef CONFIG_IPV6_PRIVACY
602 if (ifp
->flags
&IFA_F_TEMPORARY
) {
603 for (ifap
= &idev
->tempaddr_list
; (ifa
=*ifap
) != NULL
;
604 ifap
= &ifa
->tmp_next
) {
606 *ifap
= ifa
->tmp_next
;
608 in6_ifa_put(ifp
->ifpub
);
612 ifa
->tmp_next
= NULL
;
619 for (ifap
= &idev
->addr_list
; (ifa
=*ifap
) != NULL
;) {
621 *ifap
= ifa
->if_next
;
624 if (!(ifp
->flags
& IFA_F_PERMANENT
) || onlink
> 0)
628 } else if (ifp
->flags
& IFA_F_PERMANENT
) {
629 if (ipv6_prefix_equal(&ifa
->addr
, &ifp
->addr
,
631 if (ifa
->flags
& IFA_F_PERMANENT
) {
636 unsigned long lifetime
;
641 spin_lock(&ifa
->lock
);
642 lifetime
= min_t(unsigned long,
643 ifa
->valid_lft
, 0x7fffffffUL
/HZ
);
644 if (time_before(expires
,
645 ifa
->tstamp
+ lifetime
* HZ
))
646 expires
= ifa
->tstamp
+ lifetime
* HZ
;
647 spin_unlock(&ifa
->lock
);
651 ifap
= &ifa
->if_next
;
653 write_unlock_bh(&idev
->lock
);
655 ipv6_ifa_notify(RTM_DELADDR
, ifp
);
657 atomic_notifier_call_chain(&inet6addr_chain
, NETDEV_DOWN
, ifp
);
659 addrconf_del_timer(ifp
);
662 * Purge or update corresponding prefix
664 * 1) we don't purge prefix here if address was not permanent.
665 * prefix is managed by its own lifetime.
666 * 2) if there're no addresses, delete prefix.
667 * 3) if there're still other permanent address(es),
668 * corresponding prefix is still permanent.
669 * 4) otherwise, update prefix lifetime to the
670 * longest valid lifetime among the corresponding
671 * addresses on the device.
672 * Note: subsequent RA will update lifetime.
676 if ((ifp
->flags
& IFA_F_PERMANENT
) && onlink
< 1) {
677 struct in6_addr prefix
;
680 ipv6_addr_prefix(&prefix
, &ifp
->addr
, ifp
->prefix_len
);
681 rt
= rt6_lookup(&prefix
, NULL
, ifp
->idev
->dev
->ifindex
, 1);
683 if (rt
&& ((rt
->rt6i_flags
& (RTF_GATEWAY
| RTF_DEFAULT
)) == 0)) {
687 } else if (!(rt
->rt6i_flags
& RTF_EXPIRES
)) {
688 rt
->rt6i_expires
= expires
;
689 rt
->rt6i_flags
|= RTF_EXPIRES
;
692 dst_release(&rt
->u
.dst
);
698 #ifdef CONFIG_IPV6_PRIVACY
699 static int ipv6_create_tempaddr(struct inet6_ifaddr
*ifp
, struct inet6_ifaddr
*ift
)
701 struct inet6_dev
*idev
= ifp
->idev
;
702 struct in6_addr addr
, *tmpaddr
;
703 unsigned long tmp_prefered_lft
, tmp_valid_lft
, tmp_cstamp
, tmp_tstamp
;
708 write_lock(&idev
->lock
);
710 spin_lock_bh(&ift
->lock
);
711 memcpy(&addr
.s6_addr
[8], &ift
->addr
.s6_addr
[8], 8);
712 spin_unlock_bh(&ift
->lock
);
719 if (idev
->cnf
.use_tempaddr
<= 0) {
720 write_unlock(&idev
->lock
);
722 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
727 spin_lock_bh(&ifp
->lock
);
728 if (ifp
->regen_count
++ >= idev
->cnf
.regen_max_retry
) {
729 idev
->cnf
.use_tempaddr
= -1; /*XXX*/
730 spin_unlock_bh(&ifp
->lock
);
731 write_unlock(&idev
->lock
);
733 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
739 memcpy(addr
.s6_addr
, ifp
->addr
.s6_addr
, 8);
740 if (__ipv6_try_regen_rndid(idev
, tmpaddr
) < 0) {
741 spin_unlock_bh(&ifp
->lock
);
742 write_unlock(&idev
->lock
);
744 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
750 memcpy(&addr
.s6_addr
[8], idev
->rndid
, 8);
751 tmp_valid_lft
= min_t(__u32
,
753 idev
->cnf
.temp_valid_lft
);
754 tmp_prefered_lft
= min_t(__u32
,
756 idev
->cnf
.temp_prefered_lft
- desync_factor
/ HZ
);
757 tmp_plen
= ifp
->prefix_len
;
758 max_addresses
= idev
->cnf
.max_addresses
;
759 tmp_cstamp
= ifp
->cstamp
;
760 tmp_tstamp
= ifp
->tstamp
;
761 spin_unlock_bh(&ifp
->lock
);
763 write_unlock(&idev
->lock
);
764 ift
= !max_addresses
||
765 ipv6_count_addresses(idev
) < max_addresses
?
766 ipv6_add_addr(idev
, &addr
, tmp_plen
,
767 ipv6_addr_type(&addr
)&IPV6_ADDR_SCOPE_MASK
, IFA_F_TEMPORARY
) : NULL
;
768 if (!ift
|| IS_ERR(ift
)) {
772 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
774 write_lock(&idev
->lock
);
778 spin_lock_bh(&ift
->lock
);
780 ift
->valid_lft
= tmp_valid_lft
;
781 ift
->prefered_lft
= tmp_prefered_lft
;
782 ift
->cstamp
= tmp_cstamp
;
783 ift
->tstamp
= tmp_tstamp
;
784 spin_unlock_bh(&ift
->lock
);
786 addrconf_dad_start(ift
, 0);
795 * Choose an appropriate source address (RFC3484)
797 struct ipv6_saddr_score
{
805 #define IPV6_SADDR_SCORE_LOCAL 0x0001
806 #define IPV6_SADDR_SCORE_PREFERRED 0x0004
807 #define IPV6_SADDR_SCORE_HOA 0x0008
808 #define IPV6_SADDR_SCORE_OIF 0x0010
809 #define IPV6_SADDR_SCORE_LABEL 0x0020
810 #define IPV6_SADDR_SCORE_PRIVACY 0x0040
812 static inline int ipv6_saddr_preferred(int type
)
814 if (type
& (IPV6_ADDR_MAPPED
|IPV6_ADDR_COMPATv4
|
815 IPV6_ADDR_LOOPBACK
|IPV6_ADDR_RESERVED
))
820 /* static matching label */
821 static inline int ipv6_saddr_label(const struct in6_addr
*addr
, int type
)
824 * prefix (longest match) label
825 * -----------------------------
834 if (type
& IPV6_ADDR_LOOPBACK
)
836 else if (type
& IPV6_ADDR_COMPATv4
)
838 else if (type
& IPV6_ADDR_MAPPED
)
840 else if (addr
->s6_addr32
[0] == htonl(0x20010000))
842 else if (addr
->s6_addr16
[0] == htons(0x2002))
844 else if ((addr
->s6_addr
[0] & 0xfe) == 0xfc)
849 int ipv6_dev_get_saddr(struct net_device
*daddr_dev
,
850 struct in6_addr
*daddr
, struct in6_addr
*saddr
)
852 struct ipv6_saddr_score hiscore
;
853 struct inet6_ifaddr
*ifa_result
= NULL
;
854 int daddr_type
= __ipv6_addr_type(daddr
);
855 int daddr_scope
= __ipv6_addr_src_scope(daddr_type
);
856 u32 daddr_label
= ipv6_saddr_label(daddr
, daddr_type
);
857 struct net_device
*dev
;
859 memset(&hiscore
, 0, sizeof(hiscore
));
861 read_lock(&dev_base_lock
);
864 for (dev
= dev_base
; dev
; dev
=dev
->next
) {
865 struct inet6_dev
*idev
;
866 struct inet6_ifaddr
*ifa
;
868 /* Rule 0: Candidate Source Address (section 4)
869 * - multicast and link-local destination address,
870 * the set of candidate source address MUST only
871 * include addresses assigned to interfaces
872 * belonging to the same link as the outgoing
874 * (- For site-local destination addresses, the
875 * set of candidate source addresses MUST only
876 * include addresses assigned to interfaces
877 * belonging to the same site as the outgoing
880 if ((daddr_type
& IPV6_ADDR_MULTICAST
||
881 daddr_scope
<= IPV6_ADDR_SCOPE_LINKLOCAL
) &&
882 daddr_dev
&& dev
!= daddr_dev
)
885 idev
= __in6_dev_get(dev
);
889 read_lock_bh(&idev
->lock
);
890 for (ifa
= idev
->addr_list
; ifa
; ifa
= ifa
->if_next
) {
891 struct ipv6_saddr_score score
;
893 score
.addr_type
= __ipv6_addr_type(&ifa
->addr
);
896 * - Tentative Address (RFC2462 section 5.4)
897 * - A tentative address is not considered
898 * "assigned to an interface" in the traditional
900 * - Candidate Source Address (section 4)
901 * - In any case, anycast addresses, multicast
902 * addresses, and the unspecified address MUST
903 * NOT be included in a candidate set.
905 if (ifa
->flags
& IFA_F_TENTATIVE
)
907 if (unlikely(score
.addr_type
== IPV6_ADDR_ANY
||
908 score
.addr_type
& IPV6_ADDR_MULTICAST
)) {
909 LIMIT_NETDEBUG(KERN_DEBUG
910 "ADDRCONF: unspecified / multicast address"
911 "assigned as unicast address on %s",
921 if (ifa_result
== NULL
) {
922 /* record it if the first available entry */
926 /* Rule 1: Prefer same address */
927 if (hiscore
.rule
< 1) {
928 if (ipv6_addr_equal(&ifa_result
->addr
, daddr
))
929 hiscore
.attrs
|= IPV6_SADDR_SCORE_LOCAL
;
932 if (ipv6_addr_equal(&ifa
->addr
, daddr
)) {
933 score
.attrs
|= IPV6_SADDR_SCORE_LOCAL
;
934 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_LOCAL
)) {
939 if (hiscore
.attrs
& IPV6_SADDR_SCORE_LOCAL
)
943 /* Rule 2: Prefer appropriate scope */
944 if (hiscore
.rule
< 2) {
945 hiscore
.scope
= __ipv6_addr_src_scope(hiscore
.addr_type
);
948 score
.scope
= __ipv6_addr_src_scope(score
.addr_type
);
949 if (hiscore
.scope
< score
.scope
) {
950 if (hiscore
.scope
< daddr_scope
) {
955 } else if (score
.scope
< hiscore
.scope
) {
956 if (score
.scope
< daddr_scope
)
957 break; /* addresses sorted by scope */
964 /* Rule 3: Avoid deprecated address */
965 if (hiscore
.rule
< 3) {
966 if (ipv6_saddr_preferred(hiscore
.addr_type
) ||
967 !(ifa_result
->flags
& IFA_F_DEPRECATED
))
968 hiscore
.attrs
|= IPV6_SADDR_SCORE_PREFERRED
;
971 if (ipv6_saddr_preferred(score
.addr_type
) ||
972 !(ifa
->flags
& IFA_F_DEPRECATED
)) {
973 score
.attrs
|= IPV6_SADDR_SCORE_PREFERRED
;
974 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_PREFERRED
)) {
979 if (hiscore
.attrs
& IPV6_SADDR_SCORE_PREFERRED
)
983 /* Rule 4: Prefer home address */
984 #ifdef CONFIG_IPV6_MIP6
985 if (hiscore
.rule
< 4) {
986 if (ifa_result
->flags
& IFA_F_HOMEADDRESS
)
987 hiscore
.attrs
|= IPV6_SADDR_SCORE_HOA
;
990 if (ifa
->flags
& IFA_F_HOMEADDRESS
) {
991 score
.attrs
|= IPV6_SADDR_SCORE_HOA
;
992 if (!(ifa_result
->flags
& IFA_F_HOMEADDRESS
)) {
997 if (hiscore
.attrs
& IPV6_SADDR_SCORE_HOA
)
1001 if (hiscore
.rule
< 4)
1005 /* Rule 5: Prefer outgoing interface */
1006 if (hiscore
.rule
< 5) {
1007 if (daddr_dev
== NULL
||
1008 daddr_dev
== ifa_result
->idev
->dev
)
1009 hiscore
.attrs
|= IPV6_SADDR_SCORE_OIF
;
1012 if (daddr_dev
== NULL
||
1013 daddr_dev
== ifa
->idev
->dev
) {
1014 score
.attrs
|= IPV6_SADDR_SCORE_OIF
;
1015 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_OIF
)) {
1020 if (hiscore
.attrs
& IPV6_SADDR_SCORE_OIF
)
1024 /* Rule 6: Prefer matching label */
1025 if (hiscore
.rule
< 6) {
1026 if (ipv6_saddr_label(&ifa_result
->addr
, hiscore
.addr_type
) == daddr_label
)
1027 hiscore
.attrs
|= IPV6_SADDR_SCORE_LABEL
;
1030 if (ipv6_saddr_label(&ifa
->addr
, score
.addr_type
) == daddr_label
) {
1031 score
.attrs
|= IPV6_SADDR_SCORE_LABEL
;
1032 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_LABEL
)) {
1037 if (hiscore
.attrs
& IPV6_SADDR_SCORE_LABEL
)
1041 #ifdef CONFIG_IPV6_PRIVACY
1042 /* Rule 7: Prefer public address
1043 * Note: prefer temprary address if use_tempaddr >= 2
1045 if (hiscore
.rule
< 7) {
1046 if ((!(ifa_result
->flags
& IFA_F_TEMPORARY
)) ^
1047 (ifa_result
->idev
->cnf
.use_tempaddr
>= 2))
1048 hiscore
.attrs
|= IPV6_SADDR_SCORE_PRIVACY
;
1051 if ((!(ifa
->flags
& IFA_F_TEMPORARY
)) ^
1052 (ifa
->idev
->cnf
.use_tempaddr
>= 2)) {
1053 score
.attrs
|= IPV6_SADDR_SCORE_PRIVACY
;
1054 if (!(hiscore
.attrs
& IPV6_SADDR_SCORE_PRIVACY
)) {
1059 if (hiscore
.attrs
& IPV6_SADDR_SCORE_PRIVACY
)
1063 if (hiscore
.rule
< 7)
1066 /* Rule 8: Use longest matching prefix */
1067 if (hiscore
.rule
< 8) {
1068 hiscore
.matchlen
= ipv6_addr_diff(&ifa_result
->addr
, daddr
);
1071 score
.matchlen
= ipv6_addr_diff(&ifa
->addr
, daddr
);
1072 if (score
.matchlen
> hiscore
.matchlen
) {
1077 else if (score
.matchlen
< hiscore
.matchlen
)
1081 /* Final Rule: choose first available one */
1085 in6_ifa_put(ifa_result
);
1090 read_unlock_bh(&idev
->lock
);
1093 read_unlock(&dev_base_lock
);
1096 return -EADDRNOTAVAIL
;
1098 ipv6_addr_copy(saddr
, &ifa_result
->addr
);
1099 in6_ifa_put(ifa_result
);
1104 int ipv6_get_saddr(struct dst_entry
*dst
,
1105 struct in6_addr
*daddr
, struct in6_addr
*saddr
)
1107 return ipv6_dev_get_saddr(dst
? ip6_dst_idev(dst
)->dev
: NULL
, daddr
, saddr
);
1111 int ipv6_get_lladdr(struct net_device
*dev
, struct in6_addr
*addr
)
1113 struct inet6_dev
*idev
;
1114 int err
= -EADDRNOTAVAIL
;
1117 if ((idev
= __in6_dev_get(dev
)) != NULL
) {
1118 struct inet6_ifaddr
*ifp
;
1120 read_lock_bh(&idev
->lock
);
1121 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1122 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1123 ipv6_addr_copy(addr
, &ifp
->addr
);
1128 read_unlock_bh(&idev
->lock
);
1134 static int ipv6_count_addresses(struct inet6_dev
*idev
)
1137 struct inet6_ifaddr
*ifp
;
1139 read_lock_bh(&idev
->lock
);
1140 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
)
1142 read_unlock_bh(&idev
->lock
);
1146 int ipv6_chk_addr(struct in6_addr
*addr
, struct net_device
*dev
, int strict
)
1148 struct inet6_ifaddr
* ifp
;
1149 u8 hash
= ipv6_addr_hash(addr
);
1151 read_lock_bh(&addrconf_hash_lock
);
1152 for(ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
=ifp
->lst_next
) {
1153 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
1154 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1155 if (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1156 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
))
1160 read_unlock_bh(&addrconf_hash_lock
);
1165 int ipv6_chk_same_addr(const struct in6_addr
*addr
, struct net_device
*dev
)
1167 struct inet6_ifaddr
* ifp
;
1168 u8 hash
= ipv6_addr_hash(addr
);
1170 for(ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
=ifp
->lst_next
) {
1171 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1172 if (dev
== NULL
|| ifp
->idev
->dev
== dev
)
1179 struct inet6_ifaddr
* ipv6_get_ifaddr(struct in6_addr
*addr
, struct net_device
*dev
, int strict
)
1181 struct inet6_ifaddr
* ifp
;
1182 u8 hash
= ipv6_addr_hash(addr
);
1184 read_lock_bh(&addrconf_hash_lock
);
1185 for(ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
=ifp
->lst_next
) {
1186 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1187 if (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1188 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
)) {
1194 read_unlock_bh(&addrconf_hash_lock
);
1199 int ipv6_rcv_saddr_equal(const struct sock
*sk
, const struct sock
*sk2
)
1201 const struct in6_addr
*sk_rcv_saddr6
= &inet6_sk(sk
)->rcv_saddr
;
1202 const struct in6_addr
*sk2_rcv_saddr6
= inet6_rcv_saddr(sk2
);
1203 __be32 sk_rcv_saddr
= inet_sk(sk
)->rcv_saddr
;
1204 __be32 sk2_rcv_saddr
= inet_rcv_saddr(sk2
);
1205 int sk_ipv6only
= ipv6_only_sock(sk
);
1206 int sk2_ipv6only
= inet_v6_ipv6only(sk2
);
1207 int addr_type
= ipv6_addr_type(sk_rcv_saddr6
);
1208 int addr_type2
= sk2_rcv_saddr6
? ipv6_addr_type(sk2_rcv_saddr6
) : IPV6_ADDR_MAPPED
;
1210 if (!sk2_rcv_saddr
&& !sk_ipv6only
)
1213 if (addr_type2
== IPV6_ADDR_ANY
&&
1214 !(sk2_ipv6only
&& addr_type
== IPV6_ADDR_MAPPED
))
1217 if (addr_type
== IPV6_ADDR_ANY
&&
1218 !(sk_ipv6only
&& addr_type2
== IPV6_ADDR_MAPPED
))
1221 if (sk2_rcv_saddr6
&&
1222 ipv6_addr_equal(sk_rcv_saddr6
, sk2_rcv_saddr6
))
1225 if (addr_type
== IPV6_ADDR_MAPPED
&&
1227 (!sk2_rcv_saddr
|| !sk_rcv_saddr
|| sk_rcv_saddr
== sk2_rcv_saddr
))
1233 /* Gets referenced address, destroys ifaddr */
1235 static void addrconf_dad_stop(struct inet6_ifaddr
*ifp
)
1237 if (ifp
->flags
&IFA_F_PERMANENT
) {
1238 spin_lock_bh(&ifp
->lock
);
1239 addrconf_del_timer(ifp
);
1240 ifp
->flags
|= IFA_F_TENTATIVE
;
1241 spin_unlock_bh(&ifp
->lock
);
1243 #ifdef CONFIG_IPV6_PRIVACY
1244 } else if (ifp
->flags
&IFA_F_TEMPORARY
) {
1245 struct inet6_ifaddr
*ifpub
;
1246 spin_lock_bh(&ifp
->lock
);
1249 in6_ifa_hold(ifpub
);
1250 spin_unlock_bh(&ifp
->lock
);
1251 ipv6_create_tempaddr(ifpub
, ifp
);
1254 spin_unlock_bh(&ifp
->lock
);
1262 void addrconf_dad_failure(struct inet6_ifaddr
*ifp
)
1264 if (net_ratelimit())
1265 printk(KERN_INFO
"%s: duplicate address detected!\n", ifp
->idev
->dev
->name
);
1266 addrconf_dad_stop(ifp
);
1269 /* Join to solicited addr multicast group. */
1271 void addrconf_join_solict(struct net_device
*dev
, struct in6_addr
*addr
)
1273 struct in6_addr maddr
;
1275 if (dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1278 addrconf_addr_solict_mult(addr
, &maddr
);
1279 ipv6_dev_mc_inc(dev
, &maddr
);
1282 void addrconf_leave_solict(struct inet6_dev
*idev
, struct in6_addr
*addr
)
1284 struct in6_addr maddr
;
1286 if (idev
->dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1289 addrconf_addr_solict_mult(addr
, &maddr
);
1290 __ipv6_dev_mc_dec(idev
, &maddr
);
1293 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
)
1295 struct in6_addr addr
;
1296 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1297 if (ipv6_addr_any(&addr
))
1299 ipv6_dev_ac_inc(ifp
->idev
->dev
, &addr
);
1302 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
)
1304 struct in6_addr addr
;
1305 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1306 if (ipv6_addr_any(&addr
))
1308 __ipv6_dev_ac_dec(ifp
->idev
, &addr
);
1311 static int addrconf_ifid_eui48(u8
*eui
, struct net_device
*dev
)
1313 if (dev
->addr_len
!= ETH_ALEN
)
1315 memcpy(eui
, dev
->dev_addr
, 3);
1316 memcpy(eui
+ 5, dev
->dev_addr
+ 3, 3);
1319 * The zSeries OSA network cards can be shared among various
1320 * OS instances, but the OSA cards have only one MAC address.
1321 * This leads to duplicate address conflicts in conjunction
1322 * with IPv6 if more than one instance uses the same card.
1324 * The driver for these cards can deliver a unique 16-bit
1325 * identifier for each instance sharing the same card. It is
1326 * placed instead of 0xFFFE in the interface identifier. The
1327 * "u" bit of the interface identifier is not inverted in this
1328 * case. Hence the resulting interface identifier has local
1329 * scope according to RFC2373.
1332 eui
[3] = (dev
->dev_id
>> 8) & 0xFF;
1333 eui
[4] = dev
->dev_id
& 0xFF;
1342 static int addrconf_ifid_arcnet(u8
*eui
, struct net_device
*dev
)
1344 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1345 if (dev
->addr_len
!= ARCNET_ALEN
)
1348 eui
[7] = *(u8
*)dev
->dev_addr
;
1352 static int addrconf_ifid_infiniband(u8
*eui
, struct net_device
*dev
)
1354 if (dev
->addr_len
!= INFINIBAND_ALEN
)
1356 memcpy(eui
, dev
->dev_addr
+ 12, 8);
1361 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
)
1363 switch (dev
->type
) {
1366 case ARPHRD_IEEE802_TR
:
1367 return addrconf_ifid_eui48(eui
, dev
);
1369 return addrconf_ifid_arcnet(eui
, dev
);
1370 case ARPHRD_INFINIBAND
:
1371 return addrconf_ifid_infiniband(eui
, dev
);
1376 static int ipv6_inherit_eui64(u8
*eui
, struct inet6_dev
*idev
)
1379 struct inet6_ifaddr
*ifp
;
1381 read_lock_bh(&idev
->lock
);
1382 for (ifp
=idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1383 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1384 memcpy(eui
, ifp
->addr
.s6_addr
+8, 8);
1389 read_unlock_bh(&idev
->lock
);
1393 #ifdef CONFIG_IPV6_PRIVACY
1394 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1395 static int __ipv6_regen_rndid(struct inet6_dev
*idev
)
1398 get_random_bytes(idev
->rndid
, sizeof(idev
->rndid
));
1399 idev
->rndid
[0] &= ~0x02;
1402 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1403 * check if generated address is not inappropriate
1405 * - Reserved subnet anycast (RFC 2526)
1406 * 11111101 11....11 1xxxxxxx
1407 * - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
1408 * 00-00-5E-FE-xx-xx-xx-xx
1410 * - XXX: already assigned to an address on the device
1412 if (idev
->rndid
[0] == 0xfd &&
1413 (idev
->rndid
[1]&idev
->rndid
[2]&idev
->rndid
[3]&idev
->rndid
[4]&idev
->rndid
[5]&idev
->rndid
[6]) == 0xff &&
1414 (idev
->rndid
[7]&0x80))
1416 if ((idev
->rndid
[0]|idev
->rndid
[1]) == 0) {
1417 if (idev
->rndid
[2] == 0x5e && idev
->rndid
[3] == 0xfe)
1419 if ((idev
->rndid
[2]|idev
->rndid
[3]|idev
->rndid
[4]|idev
->rndid
[5]|idev
->rndid
[6]|idev
->rndid
[7]) == 0x00)
1426 static void ipv6_regen_rndid(unsigned long data
)
1428 struct inet6_dev
*idev
= (struct inet6_dev
*) data
;
1429 unsigned long expires
;
1432 write_lock_bh(&idev
->lock
);
1437 if (__ipv6_regen_rndid(idev
) < 0)
1441 idev
->cnf
.temp_prefered_lft
* HZ
-
1442 idev
->cnf
.regen_max_retry
* idev
->cnf
.dad_transmits
* idev
->nd_parms
->retrans_time
- desync_factor
;
1443 if (time_before(expires
, jiffies
)) {
1445 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1450 if (!mod_timer(&idev
->regen_timer
, expires
))
1454 write_unlock_bh(&idev
->lock
);
1455 rcu_read_unlock_bh();
1459 static int __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
) {
1462 if (tmpaddr
&& memcmp(idev
->rndid
, &tmpaddr
->s6_addr
[8], 8) == 0)
1463 ret
= __ipv6_regen_rndid(idev
);
1473 addrconf_prefix_route(struct in6_addr
*pfx
, int plen
, struct net_device
*dev
,
1474 unsigned long expires
, u32 flags
)
1476 struct fib6_config cfg
= {
1477 .fc_table
= RT6_TABLE_PREFIX
,
1478 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
1479 .fc_ifindex
= dev
->ifindex
,
1480 .fc_expires
= expires
,
1482 .fc_flags
= RTF_UP
| flags
,
1485 ipv6_addr_copy(&cfg
.fc_dst
, pfx
);
1487 /* Prevent useless cloning on PtP SIT.
1488 This thing is done here expecting that the whole
1489 class of non-broadcast devices need not cloning.
1491 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1492 if (dev
->type
== ARPHRD_SIT
&& (dev
->flags
& IFF_POINTOPOINT
))
1493 cfg
.fc_flags
|= RTF_NONEXTHOP
;
1496 ip6_route_add(&cfg
);
1499 /* Create "default" multicast route to the interface */
1501 static void addrconf_add_mroute(struct net_device
*dev
)
1503 struct fib6_config cfg
= {
1504 .fc_table
= RT6_TABLE_LOCAL
,
1505 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
1506 .fc_ifindex
= dev
->ifindex
,
1511 ipv6_addr_set(&cfg
.fc_dst
, htonl(0xFF000000), 0, 0, 0);
1513 ip6_route_add(&cfg
);
1516 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1517 static void sit_route_add(struct net_device
*dev
)
1519 struct fib6_config cfg
= {
1520 .fc_table
= RT6_TABLE_MAIN
,
1521 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
1522 .fc_ifindex
= dev
->ifindex
,
1524 .fc_flags
= RTF_UP
| RTF_NONEXTHOP
,
1527 /* prefix length - 96 bits "::d.d.d.d" */
1528 ip6_route_add(&cfg
);
1532 static void addrconf_add_lroute(struct net_device
*dev
)
1534 struct in6_addr addr
;
1536 ipv6_addr_set(&addr
, htonl(0xFE800000), 0, 0, 0);
1537 addrconf_prefix_route(&addr
, 64, dev
, 0, 0);
1540 static struct inet6_dev
*addrconf_add_dev(struct net_device
*dev
)
1542 struct inet6_dev
*idev
;
1546 if ((idev
= ipv6_find_idev(dev
)) == NULL
)
1549 /* Add default multicast route */
1550 addrconf_add_mroute(dev
);
1552 /* Add link local route */
1553 addrconf_add_lroute(dev
);
1557 void addrconf_prefix_rcv(struct net_device
*dev
, u8
*opt
, int len
)
1559 struct prefix_info
*pinfo
;
1563 unsigned long rt_expires
;
1564 struct inet6_dev
*in6_dev
;
1566 pinfo
= (struct prefix_info
*) opt
;
1568 if (len
< sizeof(struct prefix_info
)) {
1569 ADBG(("addrconf: prefix option too short\n"));
1574 * Validation checks ([ADDRCONF], page 19)
1577 addr_type
= ipv6_addr_type(&pinfo
->prefix
);
1579 if (addr_type
& (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
))
1582 valid_lft
= ntohl(pinfo
->valid
);
1583 prefered_lft
= ntohl(pinfo
->prefered
);
1585 if (prefered_lft
> valid_lft
) {
1586 if (net_ratelimit())
1587 printk(KERN_WARNING
"addrconf: prefix option has invalid lifetime\n");
1591 in6_dev
= in6_dev_get(dev
);
1593 if (in6_dev
== NULL
) {
1594 if (net_ratelimit())
1595 printk(KERN_DEBUG
"addrconf: device %s not configured\n", dev
->name
);
1600 * Two things going on here:
1601 * 1) Add routes for on-link prefixes
1602 * 2) Configure prefixes with the auto flag set
1605 /* Avoid arithmetic overflow. Really, we could
1606 save rt_expires in seconds, likely valid_lft,
1607 but it would require division in fib gc, that it
1610 if (valid_lft
>= 0x7FFFFFFF/HZ
)
1611 rt_expires
= 0x7FFFFFFF - (0x7FFFFFFF % HZ
);
1613 rt_expires
= valid_lft
* HZ
;
1616 * We convert this (in jiffies) to clock_t later.
1617 * Avoid arithmetic overflow there as well.
1618 * Overflow can happen only if HZ < USER_HZ.
1620 if (HZ
< USER_HZ
&& rt_expires
> 0x7FFFFFFF / USER_HZ
)
1621 rt_expires
= 0x7FFFFFFF / USER_HZ
;
1623 if (pinfo
->onlink
) {
1624 struct rt6_info
*rt
;
1625 rt
= rt6_lookup(&pinfo
->prefix
, NULL
, dev
->ifindex
, 1);
1627 if (rt
&& ((rt
->rt6i_flags
& (RTF_GATEWAY
| RTF_DEFAULT
)) == 0)) {
1628 if (rt
->rt6i_flags
&RTF_EXPIRES
) {
1629 if (valid_lft
== 0) {
1633 rt
->rt6i_expires
= jiffies
+ rt_expires
;
1636 } else if (valid_lft
) {
1637 addrconf_prefix_route(&pinfo
->prefix
, pinfo
->prefix_len
,
1638 dev
, jiffies_to_clock_t(rt_expires
), RTF_ADDRCONF
|RTF_EXPIRES
|RTF_PREFIX_RT
);
1641 dst_release(&rt
->u
.dst
);
1644 /* Try to figure out our local address for this prefix */
1646 if (pinfo
->autoconf
&& in6_dev
->cnf
.autoconf
) {
1647 struct inet6_ifaddr
* ifp
;
1648 struct in6_addr addr
;
1649 int create
= 0, update_lft
= 0;
1651 if (pinfo
->prefix_len
== 64) {
1652 memcpy(&addr
, &pinfo
->prefix
, 8);
1653 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) &&
1654 ipv6_inherit_eui64(addr
.s6_addr
+ 8, in6_dev
)) {
1655 in6_dev_put(in6_dev
);
1660 if (net_ratelimit())
1661 printk(KERN_DEBUG
"IPv6 addrconf: prefix with wrong length %d\n",
1663 in6_dev_put(in6_dev
);
1668 ifp
= ipv6_get_ifaddr(&addr
, dev
, 1);
1670 if (ifp
== NULL
&& valid_lft
) {
1671 int max_addresses
= in6_dev
->cnf
.max_addresses
;
1673 /* Do not allow to create too much of autoconfigured
1674 * addresses; this would be too easy way to crash kernel.
1676 if (!max_addresses
||
1677 ipv6_count_addresses(in6_dev
) < max_addresses
)
1678 ifp
= ipv6_add_addr(in6_dev
, &addr
, pinfo
->prefix_len
,
1679 addr_type
&IPV6_ADDR_SCOPE_MASK
, 0);
1681 if (!ifp
|| IS_ERR(ifp
)) {
1682 in6_dev_put(in6_dev
);
1686 update_lft
= create
= 1;
1687 ifp
->cstamp
= jiffies
;
1688 addrconf_dad_start(ifp
, RTF_ADDRCONF
|RTF_PREFIX_RT
);
1694 #ifdef CONFIG_IPV6_PRIVACY
1695 struct inet6_ifaddr
*ift
;
1699 /* update lifetime (RFC2462 5.5.3 e) */
1700 spin_lock(&ifp
->lock
);
1702 if (ifp
->valid_lft
> (now
- ifp
->tstamp
) / HZ
)
1703 stored_lft
= ifp
->valid_lft
- (now
- ifp
->tstamp
) / HZ
;
1706 if (!update_lft
&& stored_lft
) {
1707 if (valid_lft
> MIN_VALID_LIFETIME
||
1708 valid_lft
> stored_lft
)
1710 else if (stored_lft
<= MIN_VALID_LIFETIME
) {
1711 /* valid_lft <= stored_lft is always true */
1715 valid_lft
= MIN_VALID_LIFETIME
;
1716 if (valid_lft
< prefered_lft
)
1717 prefered_lft
= valid_lft
;
1723 ifp
->valid_lft
= valid_lft
;
1724 ifp
->prefered_lft
= prefered_lft
;
1727 ifp
->flags
&= ~IFA_F_DEPRECATED
;
1728 spin_unlock(&ifp
->lock
);
1730 if (!(flags
&IFA_F_TENTATIVE
))
1731 ipv6_ifa_notify(0, ifp
);
1733 spin_unlock(&ifp
->lock
);
1735 #ifdef CONFIG_IPV6_PRIVACY
1736 read_lock_bh(&in6_dev
->lock
);
1737 /* update all temporary addresses in the list */
1738 for (ift
=in6_dev
->tempaddr_list
; ift
; ift
=ift
->tmp_next
) {
1740 * When adjusting the lifetimes of an existing
1741 * temporary address, only lower the lifetimes.
1742 * Implementations must not increase the
1743 * lifetimes of an existing temporary address
1744 * when processing a Prefix Information Option.
1746 spin_lock(&ift
->lock
);
1748 if (ift
->valid_lft
> valid_lft
&&
1749 ift
->valid_lft
- valid_lft
> (jiffies
- ift
->tstamp
) / HZ
)
1750 ift
->valid_lft
= valid_lft
+ (jiffies
- ift
->tstamp
) / HZ
;
1751 if (ift
->prefered_lft
> prefered_lft
&&
1752 ift
->prefered_lft
- prefered_lft
> (jiffies
- ift
->tstamp
) / HZ
)
1753 ift
->prefered_lft
= prefered_lft
+ (jiffies
- ift
->tstamp
) / HZ
;
1754 spin_unlock(&ift
->lock
);
1755 if (!(flags
&IFA_F_TENTATIVE
))
1756 ipv6_ifa_notify(0, ift
);
1759 if (create
&& in6_dev
->cnf
.use_tempaddr
> 0) {
1761 * When a new public address is created as described in [ADDRCONF],
1762 * also create a new temporary address.
1764 read_unlock_bh(&in6_dev
->lock
);
1765 ipv6_create_tempaddr(ifp
, NULL
);
1767 read_unlock_bh(&in6_dev
->lock
);
1774 inet6_prefix_notify(RTM_NEWPREFIX
, in6_dev
, pinfo
);
1775 in6_dev_put(in6_dev
);
1779 * Set destination address.
1780 * Special case for SIT interfaces where we create a new "virtual"
1783 int addrconf_set_dstaddr(void __user
*arg
)
1785 struct in6_ifreq ireq
;
1786 struct net_device
*dev
;
1792 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
1795 dev
= __dev_get_by_index(ireq
.ifr6_ifindex
);
1801 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1802 if (dev
->type
== ARPHRD_SIT
) {
1805 struct ip_tunnel_parm p
;
1807 err
= -EADDRNOTAVAIL
;
1808 if (!(ipv6_addr_type(&ireq
.ifr6_addr
) & IPV6_ADDR_COMPATv4
))
1811 memset(&p
, 0, sizeof(p
));
1812 p
.iph
.daddr
= ireq
.ifr6_addr
.s6_addr32
[3];
1816 p
.iph
.protocol
= IPPROTO_IPV6
;
1818 ifr
.ifr_ifru
.ifru_data
= (void __user
*)&p
;
1820 oldfs
= get_fs(); set_fs(KERNEL_DS
);
1821 err
= dev
->do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
1826 if ((dev
= __dev_get_by_name(p
.name
)) == NULL
)
1828 err
= dev_open(dev
);
1839 * Manual configuration of address on an interface
1841 static int inet6_addr_add(int ifindex
, struct in6_addr
*pfx
, int plen
,
1842 __u8 ifa_flags
, __u32 prefered_lft
, __u32 valid_lft
)
1844 struct inet6_ifaddr
*ifp
;
1845 struct inet6_dev
*idev
;
1846 struct net_device
*dev
;
1848 u32 flags
= RTF_EXPIRES
;
1852 /* check the lifetime */
1853 if (!valid_lft
|| prefered_lft
> valid_lft
)
1856 if ((dev
= __dev_get_by_index(ifindex
)) == NULL
)
1859 if ((idev
= addrconf_add_dev(dev
)) == NULL
)
1862 scope
= ipv6_addr_scope(pfx
);
1864 if (valid_lft
== INFINITY_LIFE_TIME
) {
1865 ifa_flags
|= IFA_F_PERMANENT
;
1867 } else if (valid_lft
>= 0x7FFFFFFF/HZ
)
1868 valid_lft
= 0x7FFFFFFF/HZ
;
1870 if (prefered_lft
== 0)
1871 ifa_flags
|= IFA_F_DEPRECATED
;
1872 else if ((prefered_lft
>= 0x7FFFFFFF/HZ
) &&
1873 (prefered_lft
!= INFINITY_LIFE_TIME
))
1874 prefered_lft
= 0x7FFFFFFF/HZ
;
1876 ifp
= ipv6_add_addr(idev
, pfx
, plen
, scope
, ifa_flags
);
1879 spin_lock_bh(&ifp
->lock
);
1880 ifp
->valid_lft
= valid_lft
;
1881 ifp
->prefered_lft
= prefered_lft
;
1882 ifp
->tstamp
= jiffies
;
1883 spin_unlock_bh(&ifp
->lock
);
1885 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, dev
,
1886 jiffies_to_clock_t(valid_lft
* HZ
), flags
);
1887 addrconf_dad_start(ifp
, 0);
1893 return PTR_ERR(ifp
);
1896 static int inet6_addr_del(int ifindex
, struct in6_addr
*pfx
, int plen
)
1898 struct inet6_ifaddr
*ifp
;
1899 struct inet6_dev
*idev
;
1900 struct net_device
*dev
;
1902 if ((dev
= __dev_get_by_index(ifindex
)) == NULL
)
1905 if ((idev
= __in6_dev_get(dev
)) == NULL
)
1908 read_lock_bh(&idev
->lock
);
1909 for (ifp
= idev
->addr_list
; ifp
; ifp
=ifp
->if_next
) {
1910 if (ifp
->prefix_len
== plen
&&
1911 ipv6_addr_equal(pfx
, &ifp
->addr
)) {
1913 read_unlock_bh(&idev
->lock
);
1917 /* If the last address is deleted administratively,
1918 disable IPv6 on this interface.
1920 if (idev
->addr_list
== NULL
)
1921 addrconf_ifdown(idev
->dev
, 1);
1925 read_unlock_bh(&idev
->lock
);
1926 return -EADDRNOTAVAIL
;
1930 int addrconf_add_ifaddr(void __user
*arg
)
1932 struct in6_ifreq ireq
;
1935 if (!capable(CAP_NET_ADMIN
))
1938 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
1942 err
= inet6_addr_add(ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, ireq
.ifr6_prefixlen
,
1943 IFA_F_PERMANENT
, INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
1948 int addrconf_del_ifaddr(void __user
*arg
)
1950 struct in6_ifreq ireq
;
1953 if (!capable(CAP_NET_ADMIN
))
1956 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
1960 err
= inet6_addr_del(ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
, ireq
.ifr6_prefixlen
);
1965 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
1966 static void sit_add_v4_addrs(struct inet6_dev
*idev
)
1968 struct inet6_ifaddr
* ifp
;
1969 struct in6_addr addr
;
1970 struct net_device
*dev
;
1975 memset(&addr
, 0, sizeof(struct in6_addr
));
1976 memcpy(&addr
.s6_addr32
[3], idev
->dev
->dev_addr
, 4);
1978 if (idev
->dev
->flags
&IFF_POINTOPOINT
) {
1979 addr
.s6_addr32
[0] = htonl(0xfe800000);
1982 scope
= IPV6_ADDR_COMPATv4
;
1985 if (addr
.s6_addr32
[3]) {
1986 ifp
= ipv6_add_addr(idev
, &addr
, 128, scope
, IFA_F_PERMANENT
);
1988 spin_lock_bh(&ifp
->lock
);
1989 ifp
->flags
&= ~IFA_F_TENTATIVE
;
1990 spin_unlock_bh(&ifp
->lock
);
1991 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
1997 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
1998 struct in_device
* in_dev
= __in_dev_get_rtnl(dev
);
1999 if (in_dev
&& (dev
->flags
& IFF_UP
)) {
2000 struct in_ifaddr
* ifa
;
2004 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
2007 addr
.s6_addr32
[3] = ifa
->ifa_local
;
2009 if (ifa
->ifa_scope
== RT_SCOPE_LINK
)
2011 if (ifa
->ifa_scope
>= RT_SCOPE_HOST
) {
2012 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2016 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2021 ifp
= ipv6_add_addr(idev
, &addr
, plen
, flag
,
2024 spin_lock_bh(&ifp
->lock
);
2025 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2026 spin_unlock_bh(&ifp
->lock
);
2027 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2036 static void init_loopback(struct net_device
*dev
)
2038 struct inet6_dev
*idev
;
2039 struct inet6_ifaddr
* ifp
;
2045 if ((idev
= ipv6_find_idev(dev
)) == NULL
) {
2046 printk(KERN_DEBUG
"init loopback: add_dev failed\n");
2050 ifp
= ipv6_add_addr(idev
, &in6addr_loopback
, 128, IFA_HOST
, IFA_F_PERMANENT
);
2052 spin_lock_bh(&ifp
->lock
);
2053 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2054 spin_unlock_bh(&ifp
->lock
);
2055 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2060 static void addrconf_add_linklocal(struct inet6_dev
*idev
, struct in6_addr
*addr
)
2062 struct inet6_ifaddr
* ifp
;
2064 ifp
= ipv6_add_addr(idev
, addr
, 64, IFA_LINK
, IFA_F_PERMANENT
);
2066 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, idev
->dev
, 0, 0);
2067 addrconf_dad_start(ifp
, 0);
2072 static void addrconf_dev_config(struct net_device
*dev
)
2074 struct in6_addr addr
;
2075 struct inet6_dev
* idev
;
2079 if ((dev
->type
!= ARPHRD_ETHER
) &&
2080 (dev
->type
!= ARPHRD_FDDI
) &&
2081 (dev
->type
!= ARPHRD_IEEE802_TR
) &&
2082 (dev
->type
!= ARPHRD_ARCNET
) &&
2083 (dev
->type
!= ARPHRD_INFINIBAND
)) {
2084 /* Alas, we support only Ethernet autoconfiguration. */
2088 idev
= addrconf_add_dev(dev
);
2092 memset(&addr
, 0, sizeof(struct in6_addr
));
2093 addr
.s6_addr32
[0] = htonl(0xFE800000);
2095 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) == 0)
2096 addrconf_add_linklocal(idev
, &addr
);
2099 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2100 static void addrconf_sit_config(struct net_device
*dev
)
2102 struct inet6_dev
*idev
;
2107 * Configure the tunnel with one of our IPv4
2108 * addresses... we should configure all of
2109 * our v4 addrs in the tunnel
2112 if ((idev
= ipv6_find_idev(dev
)) == NULL
) {
2113 printk(KERN_DEBUG
"init sit: add_dev failed\n");
2117 sit_add_v4_addrs(idev
);
2119 if (dev
->flags
&IFF_POINTOPOINT
) {
2120 addrconf_add_mroute(dev
);
2121 addrconf_add_lroute(dev
);
2128 ipv6_inherit_linklocal(struct inet6_dev
*idev
, struct net_device
*link_dev
)
2130 struct in6_addr lladdr
;
2132 if (!ipv6_get_lladdr(link_dev
, &lladdr
)) {
2133 addrconf_add_linklocal(idev
, &lladdr
);
2139 static void ip6_tnl_add_linklocal(struct inet6_dev
*idev
)
2141 struct net_device
*link_dev
;
2143 /* first try to inherit the link-local address from the link device */
2144 if (idev
->dev
->iflink
&&
2145 (link_dev
= __dev_get_by_index(idev
->dev
->iflink
))) {
2146 if (!ipv6_inherit_linklocal(idev
, link_dev
))
2149 /* then try to inherit it from any device */
2150 for (link_dev
= dev_base
; link_dev
; link_dev
= link_dev
->next
) {
2151 if (!ipv6_inherit_linklocal(idev
, link_dev
))
2154 printk(KERN_DEBUG
"init ip6-ip6: add_linklocal failed\n");
2158 * Autoconfigure tunnel with a link-local address so routing protocols,
2159 * DHCPv6, MLD etc. can be run over the virtual link
2162 static void addrconf_ip6_tnl_config(struct net_device
*dev
)
2164 struct inet6_dev
*idev
;
2168 if ((idev
= addrconf_add_dev(dev
)) == NULL
) {
2169 printk(KERN_DEBUG
"init ip6-ip6: add_dev failed\n");
2172 ip6_tnl_add_linklocal(idev
);
2175 static int addrconf_notify(struct notifier_block
*this, unsigned long event
,
2178 struct net_device
*dev
= (struct net_device
*) data
;
2179 struct inet6_dev
*idev
= __in6_dev_get(dev
);
2180 int run_pending
= 0;
2183 case NETDEV_REGISTER
:
2185 idev
= ipv6_add_dev(dev
);
2187 printk(KERN_WARNING
"IPv6: add_dev failed for %s\n",
2193 if (event
== NETDEV_UP
) {
2194 if (!netif_carrier_ok(dev
)) {
2195 /* device is not ready yet. */
2197 "ADDRCONF(NETDEV_UP): %s: "
2198 "link is not ready\n",
2204 idev
->if_flags
|= IF_READY
;
2206 if (!netif_carrier_ok(dev
)) {
2207 /* device is still not ready. */
2212 if (idev
->if_flags
& IF_READY
) {
2213 /* device is already configured. */
2216 idev
->if_flags
|= IF_READY
;
2220 "ADDRCONF(NETDEV_CHANGE): %s: "
2221 "link becomes ready\n",
2228 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
2230 addrconf_sit_config(dev
);
2233 case ARPHRD_TUNNEL6
:
2234 addrconf_ip6_tnl_config(dev
);
2236 case ARPHRD_LOOPBACK
:
2241 addrconf_dev_config(dev
);
2246 addrconf_dad_run(idev
);
2248 /* If the MTU changed during the interface down, when the
2249 interface up, the changed MTU must be reflected in the
2250 idev as well as routers.
2252 if (idev
->cnf
.mtu6
!= dev
->mtu
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2253 rt6_mtu_change(dev
, dev
->mtu
);
2254 idev
->cnf
.mtu6
= dev
->mtu
;
2256 idev
->tstamp
= jiffies
;
2257 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
2258 /* If the changed mtu during down is lower than IPV6_MIN_MTU
2259 stop IPv6 on this interface.
2261 if (dev
->mtu
< IPV6_MIN_MTU
)
2262 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
2266 case NETDEV_CHANGEMTU
:
2267 if ( idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2268 rt6_mtu_change(dev
, dev
->mtu
);
2269 idev
->cnf
.mtu6
= dev
->mtu
;
2273 /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
2276 case NETDEV_UNREGISTER
:
2278 * Remove all addresses from this interface.
2280 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
2283 case NETDEV_CHANGENAME
:
2284 #ifdef CONFIG_SYSCTL
2286 addrconf_sysctl_unregister(&idev
->cnf
);
2287 neigh_sysctl_unregister(idev
->nd_parms
);
2288 neigh_sysctl_register(dev
, idev
->nd_parms
,
2289 NET_IPV6
, NET_IPV6_NEIGH
, "ipv6",
2290 &ndisc_ifinfo_sysctl_change
,
2292 addrconf_sysctl_register(idev
, &idev
->cnf
);
2302 * addrconf module should be notified of a device going up
2304 static struct notifier_block ipv6_dev_notf
= {
2305 .notifier_call
= addrconf_notify
,
2309 static int addrconf_ifdown(struct net_device
*dev
, int how
)
2311 struct inet6_dev
*idev
;
2312 struct inet6_ifaddr
*ifa
, **bifa
;
2317 if (dev
== &loopback_dev
&& how
== 1)
2321 neigh_ifdown(&nd_tbl
, dev
);
2323 idev
= __in6_dev_get(dev
);
2327 /* Step 1: remove reference to ipv6 device from parent device.
2333 /* protected by rtnl_lock */
2334 rcu_assign_pointer(dev
->ip6_ptr
, NULL
);
2336 /* Step 1.5: remove snmp6 entry */
2337 snmp6_unregister_dev(idev
);
2341 /* Step 2: clear hash table */
2342 for (i
=0; i
<IN6_ADDR_HSIZE
; i
++) {
2343 bifa
= &inet6_addr_lst
[i
];
2345 write_lock_bh(&addrconf_hash_lock
);
2346 while ((ifa
= *bifa
) != NULL
) {
2347 if (ifa
->idev
== idev
) {
2348 *bifa
= ifa
->lst_next
;
2349 ifa
->lst_next
= NULL
;
2350 addrconf_del_timer(ifa
);
2354 bifa
= &ifa
->lst_next
;
2356 write_unlock_bh(&addrconf_hash_lock
);
2359 write_lock_bh(&idev
->lock
);
2361 /* Step 3: clear flags for stateless addrconf */
2363 idev
->if_flags
&= ~(IF_RS_SENT
|IF_RA_RCVD
|IF_READY
);
2365 /* Step 4: clear address list */
2366 #ifdef CONFIG_IPV6_PRIVACY
2367 if (how
== 1 && del_timer(&idev
->regen_timer
))
2370 /* clear tempaddr list */
2371 while ((ifa
= idev
->tempaddr_list
) != NULL
) {
2372 idev
->tempaddr_list
= ifa
->tmp_next
;
2373 ifa
->tmp_next
= NULL
;
2375 write_unlock_bh(&idev
->lock
);
2376 spin_lock_bh(&ifa
->lock
);
2379 in6_ifa_put(ifa
->ifpub
);
2382 spin_unlock_bh(&ifa
->lock
);
2384 write_lock_bh(&idev
->lock
);
2387 while ((ifa
= idev
->addr_list
) != NULL
) {
2388 idev
->addr_list
= ifa
->if_next
;
2389 ifa
->if_next
= NULL
;
2391 addrconf_del_timer(ifa
);
2392 write_unlock_bh(&idev
->lock
);
2394 __ipv6_ifa_notify(RTM_DELADDR
, ifa
);
2397 write_lock_bh(&idev
->lock
);
2399 write_unlock_bh(&idev
->lock
);
2401 /* Step 5: Discard multicast list */
2404 ipv6_mc_destroy_dev(idev
);
2408 /* Step 5: netlink notification of this interface */
2409 idev
->tstamp
= jiffies
;
2410 inet6_ifinfo_notify(RTM_DELLINK
, idev
);
2412 /* Shot the device (if unregistered) */
2415 #ifdef CONFIG_SYSCTL
2416 addrconf_sysctl_unregister(&idev
->cnf
);
2417 neigh_sysctl_unregister(idev
->nd_parms
);
2419 neigh_parms_release(&nd_tbl
, idev
->nd_parms
);
2420 neigh_ifdown(&nd_tbl
, dev
);
2426 static void addrconf_rs_timer(unsigned long data
)
2428 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*) data
;
2430 if (ifp
->idev
->cnf
.forwarding
)
2433 if (ifp
->idev
->if_flags
& IF_RA_RCVD
) {
2435 * Announcement received after solicitation
2441 spin_lock(&ifp
->lock
);
2442 if (ifp
->probes
++ < ifp
->idev
->cnf
.rtr_solicits
) {
2443 struct in6_addr all_routers
;
2445 /* The wait after the last probe can be shorter */
2446 addrconf_mod_timer(ifp
, AC_RS
,
2447 (ifp
->probes
== ifp
->idev
->cnf
.rtr_solicits
) ?
2448 ifp
->idev
->cnf
.rtr_solicit_delay
:
2449 ifp
->idev
->cnf
.rtr_solicit_interval
);
2450 spin_unlock(&ifp
->lock
);
2452 ipv6_addr_all_routers(&all_routers
);
2454 ndisc_send_rs(ifp
->idev
->dev
, &ifp
->addr
, &all_routers
);
2456 spin_unlock(&ifp
->lock
);
2458 * Note: we do not support deprecated "all on-link"
2459 * assumption any longer.
2461 printk(KERN_DEBUG
"%s: no IPv6 routers present\n",
2462 ifp
->idev
->dev
->name
);
2470 * Duplicate Address Detection
2472 static void addrconf_dad_kick(struct inet6_ifaddr
*ifp
)
2474 unsigned long rand_num
;
2475 struct inet6_dev
*idev
= ifp
->idev
;
2477 rand_num
= net_random() % (idev
->cnf
.rtr_solicit_delay
? : 1);
2478 ifp
->probes
= idev
->cnf
.dad_transmits
;
2479 addrconf_mod_timer(ifp
, AC_DAD
, rand_num
);
2482 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
, u32 flags
)
2484 struct inet6_dev
*idev
= ifp
->idev
;
2485 struct net_device
*dev
= idev
->dev
;
2487 addrconf_join_solict(dev
, &ifp
->addr
);
2489 net_srandom(ifp
->addr
.s6_addr32
[3]);
2491 read_lock_bh(&idev
->lock
);
2494 spin_lock_bh(&ifp
->lock
);
2496 if (dev
->flags
&(IFF_NOARP
|IFF_LOOPBACK
) ||
2497 !(ifp
->flags
&IFA_F_TENTATIVE
) ||
2498 ifp
->flags
& IFA_F_NODAD
) {
2499 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2500 spin_unlock_bh(&ifp
->lock
);
2501 read_unlock_bh(&idev
->lock
);
2503 addrconf_dad_completed(ifp
);
2507 if (!(idev
->if_flags
& IF_READY
)) {
2508 spin_unlock_bh(&ifp
->lock
);
2509 read_unlock_bh(&idev
->lock
);
2511 * If the defice is not ready:
2512 * - keep it tentative if it is a permanent address.
2513 * - otherwise, kill it.
2516 addrconf_dad_stop(ifp
);
2519 addrconf_dad_kick(ifp
);
2520 spin_unlock_bh(&ifp
->lock
);
2522 read_unlock_bh(&idev
->lock
);
2525 static void addrconf_dad_timer(unsigned long data
)
2527 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*) data
;
2528 struct inet6_dev
*idev
= ifp
->idev
;
2529 struct in6_addr unspec
;
2530 struct in6_addr mcaddr
;
2532 read_lock_bh(&idev
->lock
);
2534 read_unlock_bh(&idev
->lock
);
2537 spin_lock_bh(&ifp
->lock
);
2538 if (ifp
->probes
== 0) {
2540 * DAD was successful
2543 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2544 spin_unlock_bh(&ifp
->lock
);
2545 read_unlock_bh(&idev
->lock
);
2547 addrconf_dad_completed(ifp
);
2553 addrconf_mod_timer(ifp
, AC_DAD
, ifp
->idev
->nd_parms
->retrans_time
);
2554 spin_unlock_bh(&ifp
->lock
);
2555 read_unlock_bh(&idev
->lock
);
2557 /* send a neighbour solicitation for our addr */
2558 memset(&unspec
, 0, sizeof(unspec
));
2559 addrconf_addr_solict_mult(&ifp
->addr
, &mcaddr
);
2560 ndisc_send_ns(ifp
->idev
->dev
, NULL
, &ifp
->addr
, &mcaddr
, &unspec
);
2565 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
)
2567 struct net_device
* dev
= ifp
->idev
->dev
;
2570 * Configure the address for reception. Now it is valid.
2573 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2575 /* If added prefix is link local and forwarding is off,
2576 start sending router solicitations.
2579 if (ifp
->idev
->cnf
.forwarding
== 0 &&
2580 ifp
->idev
->cnf
.rtr_solicits
> 0 &&
2581 (dev
->flags
&IFF_LOOPBACK
) == 0 &&
2582 (ipv6_addr_type(&ifp
->addr
) & IPV6_ADDR_LINKLOCAL
)) {
2583 struct in6_addr all_routers
;
2585 ipv6_addr_all_routers(&all_routers
);
2588 * If a host as already performed a random delay
2589 * [...] as part of DAD [...] there is no need
2590 * to delay again before sending the first RS
2592 ndisc_send_rs(ifp
->idev
->dev
, &ifp
->addr
, &all_routers
);
2594 spin_lock_bh(&ifp
->lock
);
2596 ifp
->idev
->if_flags
|= IF_RS_SENT
;
2597 addrconf_mod_timer(ifp
, AC_RS
, ifp
->idev
->cnf
.rtr_solicit_interval
);
2598 spin_unlock_bh(&ifp
->lock
);
2602 static void addrconf_dad_run(struct inet6_dev
*idev
) {
2603 struct inet6_ifaddr
*ifp
;
2605 read_lock_bh(&idev
->lock
);
2606 for (ifp
= idev
->addr_list
; ifp
; ifp
= ifp
->if_next
) {
2607 spin_lock_bh(&ifp
->lock
);
2608 if (!(ifp
->flags
& IFA_F_TENTATIVE
)) {
2609 spin_unlock_bh(&ifp
->lock
);
2612 spin_unlock_bh(&ifp
->lock
);
2613 addrconf_dad_kick(ifp
);
2615 read_unlock_bh(&idev
->lock
);
2618 #ifdef CONFIG_PROC_FS
2619 struct if6_iter_state
{
2623 static struct inet6_ifaddr
*if6_get_first(struct seq_file
*seq
)
2625 struct inet6_ifaddr
*ifa
= NULL
;
2626 struct if6_iter_state
*state
= seq
->private;
2628 for (state
->bucket
= 0; state
->bucket
< IN6_ADDR_HSIZE
; ++state
->bucket
) {
2629 ifa
= inet6_addr_lst
[state
->bucket
];
2636 static struct inet6_ifaddr
*if6_get_next(struct seq_file
*seq
, struct inet6_ifaddr
*ifa
)
2638 struct if6_iter_state
*state
= seq
->private;
2640 ifa
= ifa
->lst_next
;
2642 if (!ifa
&& ++state
->bucket
< IN6_ADDR_HSIZE
) {
2643 ifa
= inet6_addr_lst
[state
->bucket
];
2649 static struct inet6_ifaddr
*if6_get_idx(struct seq_file
*seq
, loff_t pos
)
2651 struct inet6_ifaddr
*ifa
= if6_get_first(seq
);
2654 while(pos
&& (ifa
= if6_get_next(seq
, ifa
)) != NULL
)
2656 return pos
? NULL
: ifa
;
2659 static void *if6_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2661 read_lock_bh(&addrconf_hash_lock
);
2662 return if6_get_idx(seq
, *pos
);
2665 static void *if6_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2667 struct inet6_ifaddr
*ifa
;
2669 ifa
= if6_get_next(seq
, v
);
2674 static void if6_seq_stop(struct seq_file
*seq
, void *v
)
2676 read_unlock_bh(&addrconf_hash_lock
);
2679 static int if6_seq_show(struct seq_file
*seq
, void *v
)
2681 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*)v
;
2683 NIP6_SEQFMT
" %02x %02x %02x %02x %8s\n",
2685 ifp
->idev
->dev
->ifindex
,
2689 ifp
->idev
->dev
->name
);
2693 static struct seq_operations if6_seq_ops
= {
2694 .start
= if6_seq_start
,
2695 .next
= if6_seq_next
,
2696 .show
= if6_seq_show
,
2697 .stop
= if6_seq_stop
,
2700 static int if6_seq_open(struct inode
*inode
, struct file
*file
)
2702 struct seq_file
*seq
;
2704 struct if6_iter_state
*s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
2709 rc
= seq_open(file
, &if6_seq_ops
);
2713 seq
= file
->private_data
;
2722 static const struct file_operations if6_fops
= {
2723 .owner
= THIS_MODULE
,
2724 .open
= if6_seq_open
,
2726 .llseek
= seq_lseek
,
2727 .release
= seq_release_private
,
2730 int __init
if6_proc_init(void)
2732 if (!proc_net_fops_create("if_inet6", S_IRUGO
, &if6_fops
))
2737 void if6_proc_exit(void)
2739 proc_net_remove("if_inet6");
2741 #endif /* CONFIG_PROC_FS */
2743 #ifdef CONFIG_IPV6_MIP6
2744 /* Check if address is a home address configured on any interface. */
2745 int ipv6_chk_home_addr(struct in6_addr
*addr
)
2748 struct inet6_ifaddr
* ifp
;
2749 u8 hash
= ipv6_addr_hash(addr
);
2750 read_lock_bh(&addrconf_hash_lock
);
2751 for (ifp
= inet6_addr_lst
[hash
]; ifp
; ifp
= ifp
->lst_next
) {
2752 if (ipv6_addr_cmp(&ifp
->addr
, addr
) == 0 &&
2753 (ifp
->flags
& IFA_F_HOMEADDRESS
)) {
2758 read_unlock_bh(&addrconf_hash_lock
);
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
);
2873 static struct in6_addr
*extract_addr(struct nlattr
*addr
, struct nlattr
*local
)
2875 struct in6_addr
*pfx
= NULL
;
2878 pfx
= nla_data(addr
);
2881 if (pfx
&& nla_memcmp(local
, pfx
, sizeof(*pfx
)))
2884 pfx
= nla_data(local
);
2890 static struct nla_policy ifa_ipv6_policy
[IFA_MAX
+1] __read_mostly
= {
2891 [IFA_ADDRESS
] = { .len
= sizeof(struct in6_addr
) },
2892 [IFA_LOCAL
] = { .len
= sizeof(struct in6_addr
) },
2893 [IFA_CACHEINFO
] = { .len
= sizeof(struct ifa_cacheinfo
) },
2897 inet6_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
2899 struct ifaddrmsg
*ifm
;
2900 struct nlattr
*tb
[IFA_MAX
+1];
2901 struct in6_addr
*pfx
;
2904 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
2908 ifm
= nlmsg_data(nlh
);
2909 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
]);
2913 return inet6_addr_del(ifm
->ifa_index
, pfx
, ifm
->ifa_prefixlen
);
2916 static int inet6_addr_modify(struct inet6_ifaddr
*ifp
, u8 ifa_flags
,
2917 u32 prefered_lft
, u32 valid_lft
)
2919 u32 flags
= RTF_EXPIRES
;
2921 if (!valid_lft
|| (prefered_lft
> valid_lft
))
2924 if (valid_lft
== INFINITY_LIFE_TIME
) {
2925 ifa_flags
|= IFA_F_PERMANENT
;
2927 } else if (valid_lft
>= 0x7FFFFFFF/HZ
)
2928 valid_lft
= 0x7FFFFFFF/HZ
;
2930 if (prefered_lft
== 0)
2931 ifa_flags
|= IFA_F_DEPRECATED
;
2932 else if ((prefered_lft
>= 0x7FFFFFFF/HZ
) &&
2933 (prefered_lft
!= INFINITY_LIFE_TIME
))
2934 prefered_lft
= 0x7FFFFFFF/HZ
;
2936 spin_lock_bh(&ifp
->lock
);
2937 ifp
->flags
= (ifp
->flags
& ~(IFA_F_DEPRECATED
| IFA_F_PERMANENT
| IFA_F_NODAD
| IFA_F_HOMEADDRESS
)) | ifa_flags
;
2938 ifp
->tstamp
= jiffies
;
2939 ifp
->valid_lft
= valid_lft
;
2940 ifp
->prefered_lft
= prefered_lft
;
2942 spin_unlock_bh(&ifp
->lock
);
2943 if (!(ifp
->flags
&IFA_F_TENTATIVE
))
2944 ipv6_ifa_notify(0, ifp
);
2946 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, ifp
->idev
->dev
,
2947 jiffies_to_clock_t(valid_lft
* HZ
), flags
);
2954 inet6_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
2956 struct ifaddrmsg
*ifm
;
2957 struct nlattr
*tb
[IFA_MAX
+1];
2958 struct in6_addr
*pfx
;
2959 struct inet6_ifaddr
*ifa
;
2960 struct net_device
*dev
;
2961 u32 valid_lft
= INFINITY_LIFE_TIME
, preferred_lft
= INFINITY_LIFE_TIME
;
2965 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
2969 ifm
= nlmsg_data(nlh
);
2970 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
]);
2974 if (tb
[IFA_CACHEINFO
]) {
2975 struct ifa_cacheinfo
*ci
;
2977 ci
= nla_data(tb
[IFA_CACHEINFO
]);
2978 valid_lft
= ci
->ifa_valid
;
2979 preferred_lft
= ci
->ifa_prefered
;
2981 preferred_lft
= INFINITY_LIFE_TIME
;
2982 valid_lft
= INFINITY_LIFE_TIME
;
2985 dev
= __dev_get_by_index(ifm
->ifa_index
);
2989 /* We ignore other flags so far. */
2990 ifa_flags
= ifm
->ifa_flags
& (IFA_F_NODAD
| IFA_F_HOMEADDRESS
);
2992 ifa
= ipv6_get_ifaddr(pfx
, dev
, 1);
2995 * It would be best to check for !NLM_F_CREATE here but
2996 * userspace alreay relies on not having to provide this.
2998 return inet6_addr_add(ifm
->ifa_index
, pfx
, ifm
->ifa_prefixlen
,
2999 ifa_flags
, preferred_lft
, valid_lft
);
3002 if (nlh
->nlmsg_flags
& NLM_F_EXCL
||
3003 !(nlh
->nlmsg_flags
& NLM_F_REPLACE
))
3006 err
= inet6_addr_modify(ifa
, ifa_flags
, preferred_lft
, valid_lft
);
3013 static void put_ifaddrmsg(struct nlmsghdr
*nlh
, u8 prefixlen
, u8 flags
,
3014 u8 scope
, int ifindex
)
3016 struct ifaddrmsg
*ifm
;
3018 ifm
= nlmsg_data(nlh
);
3019 ifm
->ifa_family
= AF_INET6
;
3020 ifm
->ifa_prefixlen
= prefixlen
;
3021 ifm
->ifa_flags
= flags
;
3022 ifm
->ifa_scope
= scope
;
3023 ifm
->ifa_index
= ifindex
;
3026 static int put_cacheinfo(struct sk_buff
*skb
, unsigned long cstamp
,
3027 unsigned long tstamp
, u32 preferred
, u32 valid
)
3029 struct ifa_cacheinfo ci
;
3031 ci
.cstamp
= (u32
)(TIME_DELTA(cstamp
, INITIAL_JIFFIES
) / HZ
* 100
3032 + TIME_DELTA(cstamp
, INITIAL_JIFFIES
) % HZ
* 100 / HZ
);
3033 ci
.tstamp
= (u32
)(TIME_DELTA(tstamp
, INITIAL_JIFFIES
) / HZ
* 100
3034 + TIME_DELTA(tstamp
, INITIAL_JIFFIES
) % HZ
* 100 / HZ
);
3035 ci
.ifa_prefered
= preferred
;
3036 ci
.ifa_valid
= valid
;
3038 return nla_put(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
3041 static inline int rt_scope(int ifa_scope
)
3043 if (ifa_scope
& IFA_HOST
)
3044 return RT_SCOPE_HOST
;
3045 else if (ifa_scope
& IFA_LINK
)
3046 return RT_SCOPE_LINK
;
3047 else if (ifa_scope
& IFA_SITE
)
3048 return RT_SCOPE_SITE
;
3050 return RT_SCOPE_UNIVERSE
;
3053 static inline int inet6_ifaddr_msgsize(void)
3055 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
3056 + nla_total_size(16) /* IFA_ADDRESS */
3057 + nla_total_size(sizeof(struct ifa_cacheinfo
));
3060 static int inet6_fill_ifaddr(struct sk_buff
*skb
, struct inet6_ifaddr
*ifa
,
3061 u32 pid
, u32 seq
, int event
, unsigned int flags
)
3063 struct nlmsghdr
*nlh
;
3064 u32 preferred
, valid
;
3066 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
3070 put_ifaddrmsg(nlh
, ifa
->prefix_len
, ifa
->flags
, rt_scope(ifa
->scope
),
3071 ifa
->idev
->dev
->ifindex
);
3073 if (!(ifa
->flags
&IFA_F_PERMANENT
)) {
3074 preferred
= ifa
->prefered_lft
;
3075 valid
= ifa
->valid_lft
;
3076 if (preferred
!= INFINITY_LIFE_TIME
) {
3077 long tval
= (jiffies
- ifa
->tstamp
)/HZ
;
3079 if (valid
!= INFINITY_LIFE_TIME
)
3083 preferred
= INFINITY_LIFE_TIME
;
3084 valid
= INFINITY_LIFE_TIME
;
3087 if (nla_put(skb
, IFA_ADDRESS
, 16, &ifa
->addr
) < 0 ||
3088 put_cacheinfo(skb
, ifa
->cstamp
, ifa
->tstamp
, preferred
, valid
) < 0) {
3089 nlmsg_cancel(skb
, nlh
);
3093 return nlmsg_end(skb
, nlh
);
3096 static int inet6_fill_ifmcaddr(struct sk_buff
*skb
, struct ifmcaddr6
*ifmca
,
3097 u32 pid
, u32 seq
, int event
, u16 flags
)
3099 struct nlmsghdr
*nlh
;
3100 u8 scope
= RT_SCOPE_UNIVERSE
;
3101 int ifindex
= ifmca
->idev
->dev
->ifindex
;
3103 if (ipv6_addr_scope(&ifmca
->mca_addr
) & IFA_SITE
)
3104 scope
= RT_SCOPE_SITE
;
3106 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
3110 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
3111 if (nla_put(skb
, IFA_MULTICAST
, 16, &ifmca
->mca_addr
) < 0 ||
3112 put_cacheinfo(skb
, ifmca
->mca_cstamp
, ifmca
->mca_tstamp
,
3113 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
3114 nlmsg_cancel(skb
, nlh
);
3118 return nlmsg_end(skb
, nlh
);
3121 static int inet6_fill_ifacaddr(struct sk_buff
*skb
, struct ifacaddr6
*ifaca
,
3122 u32 pid
, u32 seq
, int event
, unsigned int flags
)
3124 struct nlmsghdr
*nlh
;
3125 u8 scope
= RT_SCOPE_UNIVERSE
;
3126 int ifindex
= ifaca
->aca_idev
->dev
->ifindex
;
3128 if (ipv6_addr_scope(&ifaca
->aca_addr
) & IFA_SITE
)
3129 scope
= RT_SCOPE_SITE
;
3131 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
3135 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
3136 if (nla_put(skb
, IFA_ANYCAST
, 16, &ifaca
->aca_addr
) < 0 ||
3137 put_cacheinfo(skb
, ifaca
->aca_cstamp
, ifaca
->aca_tstamp
,
3138 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
3139 nlmsg_cancel(skb
, nlh
);
3143 return nlmsg_end(skb
, nlh
);
3153 static int inet6_dump_addr(struct sk_buff
*skb
, struct netlink_callback
*cb
,
3154 enum addr_type_t type
)
3157 int s_idx
, s_ip_idx
;
3159 struct net_device
*dev
;
3160 struct inet6_dev
*idev
= NULL
;
3161 struct inet6_ifaddr
*ifa
;
3162 struct ifmcaddr6
*ifmca
;
3163 struct ifacaddr6
*ifaca
;
3165 s_idx
= cb
->args
[0];
3166 s_ip_idx
= ip_idx
= cb
->args
[1];
3167 read_lock(&dev_base_lock
);
3169 for (dev
= dev_base
, idx
= 0; dev
; dev
= dev
->next
, idx
++) {
3175 if ((idev
= in6_dev_get(dev
)) == NULL
)
3177 read_lock_bh(&idev
->lock
);
3180 /* unicast address incl. temp addr */
3181 for (ifa
= idev
->addr_list
; ifa
;
3182 ifa
= ifa
->if_next
, ip_idx
++) {
3183 if (ip_idx
< s_ip_idx
)
3185 if ((err
= inet6_fill_ifaddr(skb
, ifa
,
3186 NETLINK_CB(cb
->skb
).pid
,
3187 cb
->nlh
->nlmsg_seq
, RTM_NEWADDR
,
3192 case MULTICAST_ADDR
:
3193 /* multicast address */
3194 for (ifmca
= idev
->mc_list
; ifmca
;
3195 ifmca
= ifmca
->next
, ip_idx
++) {
3196 if (ip_idx
< s_ip_idx
)
3198 if ((err
= inet6_fill_ifmcaddr(skb
, ifmca
,
3199 NETLINK_CB(cb
->skb
).pid
,
3200 cb
->nlh
->nlmsg_seq
, RTM_GETMULTICAST
,
3206 /* anycast address */
3207 for (ifaca
= idev
->ac_list
; ifaca
;
3208 ifaca
= ifaca
->aca_next
, ip_idx
++) {
3209 if (ip_idx
< s_ip_idx
)
3211 if ((err
= inet6_fill_ifacaddr(skb
, ifaca
,
3212 NETLINK_CB(cb
->skb
).pid
,
3213 cb
->nlh
->nlmsg_seq
, RTM_GETANYCAST
,
3221 read_unlock_bh(&idev
->lock
);
3226 read_unlock_bh(&idev
->lock
);
3229 read_unlock(&dev_base_lock
);
3231 cb
->args
[1] = ip_idx
;
3235 static int inet6_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3237 enum addr_type_t type
= UNICAST_ADDR
;
3238 return inet6_dump_addr(skb
, cb
, type
);
3241 static int inet6_dump_ifmcaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3243 enum addr_type_t type
= MULTICAST_ADDR
;
3244 return inet6_dump_addr(skb
, cb
, type
);
3248 static int inet6_dump_ifacaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3250 enum addr_type_t type
= ANYCAST_ADDR
;
3251 return inet6_dump_addr(skb
, cb
, type
);
3254 static int inet6_rtm_getaddr(struct sk_buff
*in_skb
, struct nlmsghdr
* nlh
,
3257 struct ifaddrmsg
*ifm
;
3258 struct nlattr
*tb
[IFA_MAX
+1];
3259 struct in6_addr
*addr
= NULL
;
3260 struct net_device
*dev
= NULL
;
3261 struct inet6_ifaddr
*ifa
;
3262 struct sk_buff
*skb
;
3265 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
3269 addr
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
]);
3275 ifm
= nlmsg_data(nlh
);
3277 dev
= __dev_get_by_index(ifm
->ifa_index
);
3279 if ((ifa
= ipv6_get_ifaddr(addr
, dev
, 1)) == NULL
) {
3280 err
= -EADDRNOTAVAIL
;
3284 if ((skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL
)) == NULL
) {
3289 err
= inet6_fill_ifaddr(skb
, ifa
, NETLINK_CB(in_skb
).pid
,
3290 nlh
->nlmsg_seq
, RTM_NEWADDR
, 0);
3292 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3293 WARN_ON(err
== -EMSGSIZE
);
3297 err
= rtnl_unicast(skb
, NETLINK_CB(in_skb
).pid
);
3304 static void inet6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
)
3306 struct sk_buff
*skb
;
3309 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC
);
3313 err
= inet6_fill_ifaddr(skb
, ifa
, 0, 0, event
, 0);
3315 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3316 WARN_ON(err
== -EMSGSIZE
);
3320 err
= rtnl_notify(skb
, 0, RTNLGRP_IPV6_IFADDR
, NULL
, GFP_ATOMIC
);
3323 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR
, err
);
3326 static inline void ipv6_store_devconf(struct ipv6_devconf
*cnf
,
3327 __s32
*array
, int bytes
)
3329 BUG_ON(bytes
< (DEVCONF_MAX
* 4));
3331 memset(array
, 0, bytes
);
3332 array
[DEVCONF_FORWARDING
] = cnf
->forwarding
;
3333 array
[DEVCONF_HOPLIMIT
] = cnf
->hop_limit
;
3334 array
[DEVCONF_MTU6
] = cnf
->mtu6
;
3335 array
[DEVCONF_ACCEPT_RA
] = cnf
->accept_ra
;
3336 array
[DEVCONF_ACCEPT_REDIRECTS
] = cnf
->accept_redirects
;
3337 array
[DEVCONF_AUTOCONF
] = cnf
->autoconf
;
3338 array
[DEVCONF_DAD_TRANSMITS
] = cnf
->dad_transmits
;
3339 array
[DEVCONF_RTR_SOLICITS
] = cnf
->rtr_solicits
;
3340 array
[DEVCONF_RTR_SOLICIT_INTERVAL
] = cnf
->rtr_solicit_interval
;
3341 array
[DEVCONF_RTR_SOLICIT_DELAY
] = cnf
->rtr_solicit_delay
;
3342 array
[DEVCONF_FORCE_MLD_VERSION
] = cnf
->force_mld_version
;
3343 #ifdef CONFIG_IPV6_PRIVACY
3344 array
[DEVCONF_USE_TEMPADDR
] = cnf
->use_tempaddr
;
3345 array
[DEVCONF_TEMP_VALID_LFT
] = cnf
->temp_valid_lft
;
3346 array
[DEVCONF_TEMP_PREFERED_LFT
] = cnf
->temp_prefered_lft
;
3347 array
[DEVCONF_REGEN_MAX_RETRY
] = cnf
->regen_max_retry
;
3348 array
[DEVCONF_MAX_DESYNC_FACTOR
] = cnf
->max_desync_factor
;
3350 array
[DEVCONF_MAX_ADDRESSES
] = cnf
->max_addresses
;
3351 array
[DEVCONF_ACCEPT_RA_DEFRTR
] = cnf
->accept_ra_defrtr
;
3352 array
[DEVCONF_ACCEPT_RA_PINFO
] = cnf
->accept_ra_pinfo
;
3353 #ifdef CONFIG_IPV6_ROUTER_PREF
3354 array
[DEVCONF_ACCEPT_RA_RTR_PREF
] = cnf
->accept_ra_rtr_pref
;
3355 array
[DEVCONF_RTR_PROBE_INTERVAL
] = cnf
->rtr_probe_interval
;
3356 #ifdef CONFIG_IPV6_ROUTE_INFO
3357 array
[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN
] = cnf
->accept_ra_rt_info_max_plen
;
3360 array
[DEVCONF_PROXY_NDP
] = cnf
->proxy_ndp
;
3361 array
[DEVCONF_ACCEPT_SOURCE_ROUTE
] = cnf
->accept_source_route
;
3364 static inline size_t inet6_if_nlmsg_size(void)
3366 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
3367 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
3368 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
3369 + nla_total_size(4) /* IFLA_MTU */
3370 + nla_total_size(4) /* IFLA_LINK */
3371 + nla_total_size( /* IFLA_PROTINFO */
3372 nla_total_size(4) /* IFLA_INET6_FLAGS */
3373 + nla_total_size(sizeof(struct ifla_cacheinfo
))
3374 + nla_total_size(DEVCONF_MAX
* 4) /* IFLA_INET6_CONF */
3378 static int inet6_fill_ifinfo(struct sk_buff
*skb
, struct inet6_dev
*idev
,
3379 u32 pid
, u32 seq
, int event
, unsigned int flags
)
3381 struct net_device
*dev
= idev
->dev
;
3382 struct nlattr
*conf
;
3383 struct ifinfomsg
*hdr
;
3384 struct nlmsghdr
*nlh
;
3386 struct ifla_cacheinfo ci
;
3388 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*hdr
), flags
);
3392 hdr
= nlmsg_data(nlh
);
3393 hdr
->ifi_family
= AF_INET6
;
3395 hdr
->ifi_type
= dev
->type
;
3396 hdr
->ifi_index
= dev
->ifindex
;
3397 hdr
->ifi_flags
= dev_get_flags(dev
);
3398 hdr
->ifi_change
= 0;
3400 NLA_PUT_STRING(skb
, IFLA_IFNAME
, dev
->name
);
3403 NLA_PUT(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
);
3405 NLA_PUT_U32(skb
, IFLA_MTU
, dev
->mtu
);
3406 if (dev
->ifindex
!= dev
->iflink
)
3407 NLA_PUT_U32(skb
, IFLA_LINK
, dev
->iflink
);
3409 protoinfo
= nla_nest_start(skb
, IFLA_PROTINFO
);
3410 if (protoinfo
== NULL
)
3411 goto nla_put_failure
;
3413 NLA_PUT_U32(skb
, IFLA_INET6_FLAGS
, idev
->if_flags
);
3415 ci
.max_reasm_len
= IPV6_MAXPLEN
;
3416 ci
.tstamp
= (__u32
)(TIME_DELTA(idev
->tstamp
, INITIAL_JIFFIES
) / HZ
* 100
3417 + TIME_DELTA(idev
->tstamp
, INITIAL_JIFFIES
) % HZ
* 100 / HZ
);
3418 ci
.reachable_time
= idev
->nd_parms
->reachable_time
;
3419 ci
.retrans_time
= idev
->nd_parms
->retrans_time
;
3420 NLA_PUT(skb
, IFLA_INET6_CACHEINFO
, sizeof(ci
), &ci
);
3422 conf
= nla_reserve(skb
, IFLA_INET6_CONF
, DEVCONF_MAX
* sizeof(s32
));
3424 goto nla_put_failure
;
3425 ipv6_store_devconf(&idev
->cnf
, nla_data(conf
), nla_len(conf
));
3427 /* XXX - Statistics/MC not implemented */
3429 nla_nest_end(skb
, protoinfo
);
3430 return nlmsg_end(skb
, nlh
);
3433 nlmsg_cancel(skb
, nlh
);
3437 static int inet6_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3440 int s_idx
= cb
->args
[0];
3441 struct net_device
*dev
;
3442 struct inet6_dev
*idev
;
3444 read_lock(&dev_base_lock
);
3445 for (dev
=dev_base
, idx
=0; dev
; dev
= dev
->next
, idx
++) {
3448 if ((idev
= in6_dev_get(dev
)) == NULL
)
3450 err
= inet6_fill_ifinfo(skb
, idev
, NETLINK_CB(cb
->skb
).pid
,
3451 cb
->nlh
->nlmsg_seq
, RTM_NEWLINK
, NLM_F_MULTI
);
3456 read_unlock(&dev_base_lock
);
3462 void inet6_ifinfo_notify(int event
, struct inet6_dev
*idev
)
3464 struct sk_buff
*skb
;
3467 skb
= nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC
);
3471 err
= inet6_fill_ifinfo(skb
, idev
, 0, 0, event
, 0);
3473 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
3474 WARN_ON(err
== -EMSGSIZE
);
3478 err
= rtnl_notify(skb
, 0, RTNLGRP_IPV6_IFADDR
, NULL
, GFP_ATOMIC
);
3481 rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR
, err
);
3484 static inline size_t inet6_prefix_nlmsg_size(void)
3486 return NLMSG_ALIGN(sizeof(struct prefixmsg
))
3487 + nla_total_size(sizeof(struct in6_addr
))
3488 + nla_total_size(sizeof(struct prefix_cacheinfo
));
3491 static int inet6_fill_prefix(struct sk_buff
*skb
, struct inet6_dev
*idev
,
3492 struct prefix_info
*pinfo
, u32 pid
, u32 seq
,
3493 int event
, unsigned int flags
)
3495 struct prefixmsg
*pmsg
;
3496 struct nlmsghdr
*nlh
;
3497 struct prefix_cacheinfo ci
;
3499 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*pmsg
), flags
);
3503 pmsg
= nlmsg_data(nlh
);
3504 pmsg
->prefix_family
= AF_INET6
;
3505 pmsg
->prefix_pad1
= 0;
3506 pmsg
->prefix_pad2
= 0;
3507 pmsg
->prefix_ifindex
= idev
->dev
->ifindex
;
3508 pmsg
->prefix_len
= pinfo
->prefix_len
;
3509 pmsg
->prefix_type
= pinfo
->type
;
3510 pmsg
->prefix_pad3
= 0;
3511 pmsg
->prefix_flags
= 0;
3513 pmsg
->prefix_flags
|= IF_PREFIX_ONLINK
;
3514 if (pinfo
->autoconf
)
3515 pmsg
->prefix_flags
|= IF_PREFIX_AUTOCONF
;
3517 NLA_PUT(skb
, PREFIX_ADDRESS
, sizeof(pinfo
->prefix
), &pinfo
->prefix
);
3519 ci
.preferred_time
= ntohl(pinfo
->prefered
);
3520 ci
.valid_time
= ntohl(pinfo
->valid
);
3521 NLA_PUT(skb
, PREFIX_CACHEINFO
, sizeof(ci
), &ci
);
3523 return nlmsg_end(skb
, nlh
);
3526 nlmsg_cancel(skb
, nlh
);
3530 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
3531 struct prefix_info
*pinfo
)
3533 struct sk_buff
*skb
;
3536 skb
= nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC
);
3540 err
= inet6_fill_prefix(skb
, idev
, pinfo
, 0, 0, event
, 0);
3542 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
3543 WARN_ON(err
== -EMSGSIZE
);
3547 err
= rtnl_notify(skb
, 0, RTNLGRP_IPV6_PREFIX
, NULL
, GFP_ATOMIC
);
3550 rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX
, err
);
3553 static struct rtnetlink_link inet6_rtnetlink_table
[RTM_NR_MSGTYPES
] = {
3554 [RTM_GETLINK
- RTM_BASE
] = { .dumpit
= inet6_dump_ifinfo
, },
3555 [RTM_NEWADDR
- RTM_BASE
] = { .doit
= inet6_rtm_newaddr
, },
3556 [RTM_DELADDR
- RTM_BASE
] = { .doit
= inet6_rtm_deladdr
, },
3557 [RTM_GETADDR
- RTM_BASE
] = { .doit
= inet6_rtm_getaddr
,
3558 .dumpit
= inet6_dump_ifaddr
, },
3559 [RTM_GETMULTICAST
- RTM_BASE
] = { .dumpit
= inet6_dump_ifmcaddr
, },
3560 [RTM_GETANYCAST
- RTM_BASE
] = { .dumpit
= inet6_dump_ifacaddr
, },
3561 [RTM_NEWROUTE
- RTM_BASE
] = { .doit
= inet6_rtm_newroute
, },
3562 [RTM_DELROUTE
- RTM_BASE
] = { .doit
= inet6_rtm_delroute
, },
3563 [RTM_GETROUTE
- RTM_BASE
] = { .doit
= inet6_rtm_getroute
,
3564 .dumpit
= inet6_dump_fib
, },
3565 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3566 [RTM_GETRULE
- RTM_BASE
] = { .dumpit
= fib6_rules_dump
, },
3570 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
3572 inet6_ifa_notify(event
? : RTM_NEWADDR
, ifp
);
3576 ip6_ins_rt(ifp
->rt
);
3577 if (ifp
->idev
->cnf
.forwarding
)
3578 addrconf_join_anycast(ifp
);
3581 if (ifp
->idev
->cnf
.forwarding
)
3582 addrconf_leave_anycast(ifp
);
3583 addrconf_leave_solict(ifp
->idev
, &ifp
->addr
);
3584 dst_hold(&ifp
->rt
->u
.dst
);
3585 if (ip6_del_rt(ifp
->rt
))
3586 dst_free(&ifp
->rt
->u
.dst
);
3591 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
3594 if (likely(ifp
->idev
->dead
== 0))
3595 __ipv6_ifa_notify(event
, ifp
);
3596 rcu_read_unlock_bh();
3599 #ifdef CONFIG_SYSCTL
3602 int addrconf_sysctl_forward(ctl_table
*ctl
, int write
, struct file
* filp
,
3603 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
3605 int *valp
= ctl
->data
;
3609 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
3611 if (write
&& valp
!= &ipv6_devconf_dflt
.forwarding
) {
3612 if (valp
!= &ipv6_devconf
.forwarding
) {
3613 if ((!*valp
) ^ (!val
)) {
3614 struct inet6_dev
*idev
= (struct inet6_dev
*)ctl
->extra1
;
3617 dev_forward_change(idev
);
3620 ipv6_devconf_dflt
.forwarding
= ipv6_devconf
.forwarding
;
3621 addrconf_forward_change();
3624 rt6_purge_dflt_routers();
3630 static int addrconf_sysctl_forward_strategy(ctl_table
*table
,
3631 int __user
*name
, int nlen
,
3632 void __user
*oldval
,
3633 size_t __user
*oldlenp
,
3634 void __user
*newval
, size_t newlen
)
3636 int *valp
= table
->data
;
3639 if (!newval
|| !newlen
)
3641 if (newlen
!= sizeof(int))
3643 if (get_user(new, (int __user
*)newval
))
3647 if (oldval
&& oldlenp
) {
3649 if (get_user(len
, oldlenp
))
3652 if (len
> table
->maxlen
)
3653 len
= table
->maxlen
;
3654 if (copy_to_user(oldval
, valp
, len
))
3656 if (put_user(len
, oldlenp
))
3661 if (valp
!= &ipv6_devconf_dflt
.forwarding
) {
3662 if (valp
!= &ipv6_devconf
.forwarding
) {
3663 struct inet6_dev
*idev
= (struct inet6_dev
*)table
->extra1
;
3665 if (unlikely(idev
== NULL
))
3667 changed
= (!*valp
) ^ (!new);
3670 dev_forward_change(idev
);
3673 addrconf_forward_change();
3677 rt6_purge_dflt_routers();
3684 static struct addrconf_sysctl_table
3686 struct ctl_table_header
*sysctl_header
;
3687 ctl_table addrconf_vars
[__NET_IPV6_MAX
];
3688 ctl_table addrconf_dev
[2];
3689 ctl_table addrconf_conf_dir
[2];
3690 ctl_table addrconf_proto_dir
[2];
3691 ctl_table addrconf_root_dir
[2];
3692 } addrconf_sysctl __read_mostly
= {
3693 .sysctl_header
= NULL
,
3696 .ctl_name
= NET_IPV6_FORWARDING
,
3697 .procname
= "forwarding",
3698 .data
= &ipv6_devconf
.forwarding
,
3699 .maxlen
= sizeof(int),
3701 .proc_handler
= &addrconf_sysctl_forward
,
3702 .strategy
= &addrconf_sysctl_forward_strategy
,
3705 .ctl_name
= NET_IPV6_HOP_LIMIT
,
3706 .procname
= "hop_limit",
3707 .data
= &ipv6_devconf
.hop_limit
,
3708 .maxlen
= sizeof(int),
3710 .proc_handler
= proc_dointvec
,
3713 .ctl_name
= NET_IPV6_MTU
,
3715 .data
= &ipv6_devconf
.mtu6
,
3716 .maxlen
= sizeof(int),
3718 .proc_handler
= &proc_dointvec
,
3721 .ctl_name
= NET_IPV6_ACCEPT_RA
,
3722 .procname
= "accept_ra",
3723 .data
= &ipv6_devconf
.accept_ra
,
3724 .maxlen
= sizeof(int),
3726 .proc_handler
= &proc_dointvec
,
3729 .ctl_name
= NET_IPV6_ACCEPT_REDIRECTS
,
3730 .procname
= "accept_redirects",
3731 .data
= &ipv6_devconf
.accept_redirects
,
3732 .maxlen
= sizeof(int),
3734 .proc_handler
= &proc_dointvec
,
3737 .ctl_name
= NET_IPV6_AUTOCONF
,
3738 .procname
= "autoconf",
3739 .data
= &ipv6_devconf
.autoconf
,
3740 .maxlen
= sizeof(int),
3742 .proc_handler
= &proc_dointvec
,
3745 .ctl_name
= NET_IPV6_DAD_TRANSMITS
,
3746 .procname
= "dad_transmits",
3747 .data
= &ipv6_devconf
.dad_transmits
,
3748 .maxlen
= sizeof(int),
3750 .proc_handler
= &proc_dointvec
,
3753 .ctl_name
= NET_IPV6_RTR_SOLICITS
,
3754 .procname
= "router_solicitations",
3755 .data
= &ipv6_devconf
.rtr_solicits
,
3756 .maxlen
= sizeof(int),
3758 .proc_handler
= &proc_dointvec
,
3761 .ctl_name
= NET_IPV6_RTR_SOLICIT_INTERVAL
,
3762 .procname
= "router_solicitation_interval",
3763 .data
= &ipv6_devconf
.rtr_solicit_interval
,
3764 .maxlen
= sizeof(int),
3766 .proc_handler
= &proc_dointvec_jiffies
,
3767 .strategy
= &sysctl_jiffies
,
3770 .ctl_name
= NET_IPV6_RTR_SOLICIT_DELAY
,
3771 .procname
= "router_solicitation_delay",
3772 .data
= &ipv6_devconf
.rtr_solicit_delay
,
3773 .maxlen
= sizeof(int),
3775 .proc_handler
= &proc_dointvec_jiffies
,
3776 .strategy
= &sysctl_jiffies
,
3779 .ctl_name
= NET_IPV6_FORCE_MLD_VERSION
,
3780 .procname
= "force_mld_version",
3781 .data
= &ipv6_devconf
.force_mld_version
,
3782 .maxlen
= sizeof(int),
3784 .proc_handler
= &proc_dointvec
,
3786 #ifdef CONFIG_IPV6_PRIVACY
3788 .ctl_name
= NET_IPV6_USE_TEMPADDR
,
3789 .procname
= "use_tempaddr",
3790 .data
= &ipv6_devconf
.use_tempaddr
,
3791 .maxlen
= sizeof(int),
3793 .proc_handler
= &proc_dointvec
,
3796 .ctl_name
= NET_IPV6_TEMP_VALID_LFT
,
3797 .procname
= "temp_valid_lft",
3798 .data
= &ipv6_devconf
.temp_valid_lft
,
3799 .maxlen
= sizeof(int),
3801 .proc_handler
= &proc_dointvec
,
3804 .ctl_name
= NET_IPV6_TEMP_PREFERED_LFT
,
3805 .procname
= "temp_prefered_lft",
3806 .data
= &ipv6_devconf
.temp_prefered_lft
,
3807 .maxlen
= sizeof(int),
3809 .proc_handler
= &proc_dointvec
,
3812 .ctl_name
= NET_IPV6_REGEN_MAX_RETRY
,
3813 .procname
= "regen_max_retry",
3814 .data
= &ipv6_devconf
.regen_max_retry
,
3815 .maxlen
= sizeof(int),
3817 .proc_handler
= &proc_dointvec
,
3820 .ctl_name
= NET_IPV6_MAX_DESYNC_FACTOR
,
3821 .procname
= "max_desync_factor",
3822 .data
= &ipv6_devconf
.max_desync_factor
,
3823 .maxlen
= sizeof(int),
3825 .proc_handler
= &proc_dointvec
,
3829 .ctl_name
= NET_IPV6_MAX_ADDRESSES
,
3830 .procname
= "max_addresses",
3831 .data
= &ipv6_devconf
.max_addresses
,
3832 .maxlen
= sizeof(int),
3834 .proc_handler
= &proc_dointvec
,
3837 .ctl_name
= NET_IPV6_ACCEPT_RA_DEFRTR
,
3838 .procname
= "accept_ra_defrtr",
3839 .data
= &ipv6_devconf
.accept_ra_defrtr
,
3840 .maxlen
= sizeof(int),
3842 .proc_handler
= &proc_dointvec
,
3845 .ctl_name
= NET_IPV6_ACCEPT_RA_PINFO
,
3846 .procname
= "accept_ra_pinfo",
3847 .data
= &ipv6_devconf
.accept_ra_pinfo
,
3848 .maxlen
= sizeof(int),
3850 .proc_handler
= &proc_dointvec
,
3852 #ifdef CONFIG_IPV6_ROUTER_PREF
3854 .ctl_name
= NET_IPV6_ACCEPT_RA_RTR_PREF
,
3855 .procname
= "accept_ra_rtr_pref",
3856 .data
= &ipv6_devconf
.accept_ra_rtr_pref
,
3857 .maxlen
= sizeof(int),
3859 .proc_handler
= &proc_dointvec
,
3862 .ctl_name
= NET_IPV6_RTR_PROBE_INTERVAL
,
3863 .procname
= "router_probe_interval",
3864 .data
= &ipv6_devconf
.rtr_probe_interval
,
3865 .maxlen
= sizeof(int),
3867 .proc_handler
= &proc_dointvec_jiffies
,
3868 .strategy
= &sysctl_jiffies
,
3870 #ifdef CONFIG_IPV6_ROUTE_INFO
3872 .ctl_name
= NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN
,
3873 .procname
= "accept_ra_rt_info_max_plen",
3874 .data
= &ipv6_devconf
.accept_ra_rt_info_max_plen
,
3875 .maxlen
= sizeof(int),
3877 .proc_handler
= &proc_dointvec
,
3882 .ctl_name
= NET_IPV6_PROXY_NDP
,
3883 .procname
= "proxy_ndp",
3884 .data
= &ipv6_devconf
.proxy_ndp
,
3885 .maxlen
= sizeof(int),
3887 .proc_handler
= &proc_dointvec
,
3890 .ctl_name
= NET_IPV6_ACCEPT_SOURCE_ROUTE
,
3891 .procname
= "accept_source_route",
3892 .data
= &ipv6_devconf
.accept_source_route
,
3893 .maxlen
= sizeof(int),
3895 .proc_handler
= &proc_dointvec
,
3898 .ctl_name
= 0, /* sentinel */
3903 .ctl_name
= NET_PROTO_CONF_ALL
,
3906 .child
= addrconf_sysctl
.addrconf_vars
,
3909 .ctl_name
= 0, /* sentinel */
3912 .addrconf_conf_dir
= {
3914 .ctl_name
= NET_IPV6_CONF
,
3917 .child
= addrconf_sysctl
.addrconf_dev
,
3920 .ctl_name
= 0, /* sentinel */
3923 .addrconf_proto_dir
= {
3925 .ctl_name
= NET_IPV6
,
3928 .child
= addrconf_sysctl
.addrconf_conf_dir
,
3931 .ctl_name
= 0, /* sentinel */
3934 .addrconf_root_dir
= {
3936 .ctl_name
= CTL_NET
,
3939 .child
= addrconf_sysctl
.addrconf_proto_dir
,
3942 .ctl_name
= 0, /* sentinel */
3947 static void addrconf_sysctl_register(struct inet6_dev
*idev
, struct ipv6_devconf
*p
)
3950 struct net_device
*dev
= idev
? idev
->dev
: NULL
;
3951 struct addrconf_sysctl_table
*t
;
3952 char *dev_name
= NULL
;
3954 t
= kmemdup(&addrconf_sysctl
, sizeof(*t
), GFP_KERNEL
);
3957 for (i
=0; t
->addrconf_vars
[i
].data
; i
++) {
3958 t
->addrconf_vars
[i
].data
+= (char*)p
- (char*)&ipv6_devconf
;
3959 t
->addrconf_vars
[i
].extra1
= idev
; /* embedded; no ref */
3962 dev_name
= dev
->name
;
3963 t
->addrconf_dev
[0].ctl_name
= dev
->ifindex
;
3965 dev_name
= "default";
3966 t
->addrconf_dev
[0].ctl_name
= NET_PROTO_CONF_DEFAULT
;
3970 * Make a copy of dev_name, because '.procname' is regarded as const
3971 * by sysctl and we wouldn't want anyone to change it under our feet
3972 * (see SIOCSIFNAME).
3974 dev_name
= kstrdup(dev_name
, GFP_KERNEL
);
3978 t
->addrconf_dev
[0].procname
= dev_name
;
3980 t
->addrconf_dev
[0].child
= t
->addrconf_vars
;
3981 t
->addrconf_conf_dir
[0].child
= t
->addrconf_dev
;
3982 t
->addrconf_proto_dir
[0].child
= t
->addrconf_conf_dir
;
3983 t
->addrconf_root_dir
[0].child
= t
->addrconf_proto_dir
;
3985 t
->sysctl_header
= register_sysctl_table(t
->addrconf_root_dir
);
3986 if (t
->sysctl_header
== NULL
)
4001 static void addrconf_sysctl_unregister(struct ipv6_devconf
*p
)
4004 struct addrconf_sysctl_table
*t
= p
->sysctl
;
4006 unregister_sysctl_table(t
->sysctl_header
);
4007 kfree(t
->addrconf_dev
[0].procname
);
4019 int register_inet6addr_notifier(struct notifier_block
*nb
)
4021 return atomic_notifier_chain_register(&inet6addr_chain
, nb
);
4024 int unregister_inet6addr_notifier(struct notifier_block
*nb
)
4026 return atomic_notifier_chain_unregister(&inet6addr_chain
,nb
);
4030 * Init / cleanup code
4033 int __init
addrconf_init(void)
4037 /* The addrconf netdev notifier requires that loopback_dev
4038 * has it's ipv6 private information allocated and setup
4039 * before it can bring up and give link-local addresses
4040 * to other devices which are up.
4042 * Unfortunately, loopback_dev is not necessarily the first
4043 * entry in the global dev_base list of net devices. In fact,
4044 * it is likely to be the very last entry on that list.
4045 * So this causes the notifier registry below to try and
4046 * give link-local addresses to all devices besides loopback_dev
4047 * first, then loopback_dev, which cases all the non-loopback_dev
4048 * devices to fail to get a link-local address.
4050 * So, as a temporary fix, allocate the ipv6 structure for
4051 * loopback_dev first by hand.
4052 * Longer term, all of the dependencies ipv6 has upon the loopback
4053 * device and it being up should be removed.
4056 if (!ipv6_add_dev(&loopback_dev
))
4062 ip6_null_entry
.rt6i_idev
= in6_dev_get(&loopback_dev
);
4064 register_netdevice_notifier(&ipv6_dev_notf
);
4067 rtnetlink_links
[PF_INET6
] = inet6_rtnetlink_table
;
4068 #ifdef CONFIG_SYSCTL
4069 addrconf_sysctl
.sysctl_header
=
4070 register_sysctl_table(addrconf_sysctl
.addrconf_root_dir
);
4071 addrconf_sysctl_register(NULL
, &ipv6_devconf_dflt
);
4077 void __exit
addrconf_cleanup(void)
4079 struct net_device
*dev
;
4080 struct inet6_dev
*idev
;
4081 struct inet6_ifaddr
*ifa
;
4084 unregister_netdevice_notifier(&ipv6_dev_notf
);
4086 rtnetlink_links
[PF_INET6
] = NULL
;
4087 #ifdef CONFIG_SYSCTL
4088 addrconf_sysctl_unregister(&ipv6_devconf_dflt
);
4089 addrconf_sysctl_unregister(&ipv6_devconf
);
4098 for (dev
=dev_base
; dev
; dev
=dev
->next
) {
4099 if ((idev
= __in6_dev_get(dev
)) == NULL
)
4101 addrconf_ifdown(dev
, 1);
4103 addrconf_ifdown(&loopback_dev
, 2);
4109 write_lock_bh(&addrconf_hash_lock
);
4110 for (i
=0; i
< IN6_ADDR_HSIZE
; i
++) {
4111 for (ifa
=inet6_addr_lst
[i
]; ifa
; ) {
4112 struct inet6_ifaddr
*bifa
;
4115 ifa
= ifa
->lst_next
;
4116 printk(KERN_DEBUG
"bug: IPv6 address leakage detected: ifa=%p\n", bifa
);
4117 /* Do not free it; something is wrong.
4118 Now we can investigate it with debugger.
4122 write_unlock_bh(&addrconf_hash_lock
);
4124 del_timer(&addr_chk_timer
);
4128 #ifdef CONFIG_PROC_FS
4129 proc_net_remove("if_inet6");