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 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
25 * yoshfuji@USAGI : Fixed interval between DAD
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
36 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
41 #define pr_fmt(fmt) "IPv6: " fmt
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/socket.h>
47 #include <linux/sockios.h>
48 #include <linux/net.h>
49 #include <linux/in6.h>
50 #include <linux/netdevice.h>
51 #include <linux/if_addr.h>
52 #include <linux/if_arp.h>
53 #include <linux/if_arcnet.h>
54 #include <linux/if_infiniband.h>
55 #include <linux/route.h>
56 #include <linux/inetdevice.h>
57 #include <linux/init.h>
58 #include <linux/slab.h>
60 #include <linux/sysctl.h>
62 #include <linux/capability.h>
63 #include <linux/delay.h>
64 #include <linux/notifier.h>
65 #include <linux/string.h>
66 #include <linux/hash.h>
68 #include <net/net_namespace.h>
72 #include <net/af_ieee802154.h>
74 #include <net/protocol.h>
75 #include <net/ndisc.h>
76 #include <net/ip6_route.h>
77 #include <net/addrconf.h>
80 #include <net/netlink.h>
81 #include <net/pkt_sched.h>
82 #include <linux/if_tunnel.h>
83 #include <linux/rtnetlink.h>
84 #include <linux/netconf.h>
86 #ifdef CONFIG_IPV6_PRIVACY
87 #include <linux/random.h>
90 #include <linux/uaccess.h>
91 #include <asm/unaligned.h>
93 #include <linux/proc_fs.h>
94 #include <linux/seq_file.h>
95 #include <linux/export.h>
97 /* Set to 3 to get tracing... */
101 #define ADBG(x) printk x
106 #define INFINITY_LIFE_TIME 0xFFFFFFFF
108 static inline u32
cstamp_delta(unsigned long cstamp
)
110 return (cstamp
- INITIAL_JIFFIES
) * 100UL / HZ
;
114 static void addrconf_sysctl_register(struct inet6_dev
*idev
);
115 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
);
117 static inline void addrconf_sysctl_register(struct inet6_dev
*idev
)
121 static inline void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
126 #ifdef CONFIG_IPV6_PRIVACY
127 static void __ipv6_regen_rndid(struct inet6_dev
*idev
);
128 static void __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
);
129 static void ipv6_regen_rndid(unsigned long data
);
132 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
);
133 static int ipv6_count_addresses(struct inet6_dev
*idev
);
136 * Configured unicast address hash table
138 static struct hlist_head inet6_addr_lst
[IN6_ADDR_HSIZE
];
139 static DEFINE_SPINLOCK(addrconf_hash_lock
);
141 static void addrconf_verify(unsigned long);
143 static DEFINE_TIMER(addr_chk_timer
, addrconf_verify
, 0, 0);
144 static DEFINE_SPINLOCK(addrconf_verify_lock
);
146 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
);
147 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
);
149 static void addrconf_type_change(struct net_device
*dev
,
150 unsigned long event
);
151 static int addrconf_ifdown(struct net_device
*dev
, int how
);
153 static struct rt6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
155 const struct net_device
*dev
,
156 u32 flags
, u32 noflags
);
158 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
);
159 static void addrconf_dad_timer(unsigned long data
);
160 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
);
161 static void addrconf_dad_run(struct inet6_dev
*idev
);
162 static void addrconf_rs_timer(unsigned long data
);
163 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
164 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
166 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
167 struct prefix_info
*pinfo
);
168 static bool ipv6_chk_same_addr(struct net
*net
, const struct in6_addr
*addr
,
169 struct net_device
*dev
);
171 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain
);
173 static struct ipv6_devconf ipv6_devconf __read_mostly
= {
175 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
176 .mtu6
= IPV6_MIN_MTU
,
178 .accept_redirects
= 1,
180 .force_mld_version
= 0,
182 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
183 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
184 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
185 #ifdef CONFIG_IPV6_PRIVACY
187 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
188 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
189 .regen_max_retry
= REGEN_MAX_RETRY
,
190 .max_desync_factor
= MAX_DESYNC_FACTOR
,
192 .max_addresses
= IPV6_MAX_ADDRESSES
,
193 .accept_ra_defrtr
= 1,
194 .accept_ra_pinfo
= 1,
195 #ifdef CONFIG_IPV6_ROUTER_PREF
196 .accept_ra_rtr_pref
= 1,
197 .rtr_probe_interval
= 60 * HZ
,
198 #ifdef CONFIG_IPV6_ROUTE_INFO
199 .accept_ra_rt_info_max_plen
= 0,
203 .accept_source_route
= 0, /* we do not accept RH0 by default. */
208 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly
= {
210 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
211 .mtu6
= IPV6_MIN_MTU
,
213 .accept_redirects
= 1,
216 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
217 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
218 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
219 #ifdef CONFIG_IPV6_PRIVACY
221 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
222 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
223 .regen_max_retry
= REGEN_MAX_RETRY
,
224 .max_desync_factor
= MAX_DESYNC_FACTOR
,
226 .max_addresses
= IPV6_MAX_ADDRESSES
,
227 .accept_ra_defrtr
= 1,
228 .accept_ra_pinfo
= 1,
229 #ifdef CONFIG_IPV6_ROUTER_PREF
230 .accept_ra_rtr_pref
= 1,
231 .rtr_probe_interval
= 60 * HZ
,
232 #ifdef CONFIG_IPV6_ROUTE_INFO
233 .accept_ra_rt_info_max_plen
= 0,
237 .accept_source_route
= 0, /* we do not accept RH0 by default. */
242 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
243 const struct in6_addr in6addr_any
= IN6ADDR_ANY_INIT
;
244 const struct in6_addr in6addr_loopback
= IN6ADDR_LOOPBACK_INIT
;
245 const struct in6_addr in6addr_linklocal_allnodes
= IN6ADDR_LINKLOCAL_ALLNODES_INIT
;
246 const struct in6_addr in6addr_linklocal_allrouters
= IN6ADDR_LINKLOCAL_ALLROUTERS_INIT
;
247 const struct in6_addr in6addr_interfacelocal_allnodes
= IN6ADDR_INTERFACELOCAL_ALLNODES_INIT
;
248 const struct in6_addr in6addr_interfacelocal_allrouters
= IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT
;
249 const struct in6_addr in6addr_sitelocal_allrouters
= IN6ADDR_SITELOCAL_ALLROUTERS_INIT
;
251 /* Check if a valid qdisc is available */
252 static inline bool addrconf_qdisc_ok(const struct net_device
*dev
)
254 return !qdisc_tx_is_noop(dev
);
257 static void addrconf_del_timer(struct inet6_ifaddr
*ifp
)
259 if (del_timer(&ifp
->timer
))
263 enum addrconf_timer_t
{
269 static void addrconf_mod_timer(struct inet6_ifaddr
*ifp
,
270 enum addrconf_timer_t what
,
273 if (!del_timer(&ifp
->timer
))
278 ifp
->timer
.function
= addrconf_dad_timer
;
281 ifp
->timer
.function
= addrconf_rs_timer
;
286 ifp
->timer
.expires
= jiffies
+ when
;
287 add_timer(&ifp
->timer
);
290 static int snmp6_alloc_dev(struct inet6_dev
*idev
)
292 if (snmp_mib_init((void __percpu
**)idev
->stats
.ipv6
,
293 sizeof(struct ipstats_mib
),
294 __alignof__(struct ipstats_mib
)) < 0)
296 idev
->stats
.icmpv6dev
= kzalloc(sizeof(struct icmpv6_mib_device
),
298 if (!idev
->stats
.icmpv6dev
)
300 idev
->stats
.icmpv6msgdev
= kzalloc(sizeof(struct icmpv6msg_mib_device
),
302 if (!idev
->stats
.icmpv6msgdev
)
308 kfree(idev
->stats
.icmpv6dev
);
310 snmp_mib_free((void __percpu
**)idev
->stats
.ipv6
);
315 static void snmp6_free_dev(struct inet6_dev
*idev
)
317 kfree(idev
->stats
.icmpv6msgdev
);
318 kfree(idev
->stats
.icmpv6dev
);
319 snmp_mib_free((void __percpu
**)idev
->stats
.ipv6
);
322 /* Nobody refers to this device, we may destroy it. */
324 void in6_dev_finish_destroy(struct inet6_dev
*idev
)
326 struct net_device
*dev
= idev
->dev
;
328 WARN_ON(!list_empty(&idev
->addr_list
));
329 WARN_ON(idev
->mc_list
!= NULL
);
331 #ifdef NET_REFCNT_DEBUG
332 pr_debug("%s: %s\n", __func__
, dev
? dev
->name
: "NIL");
336 pr_warn("Freeing alive inet6 device %p\n", idev
);
339 snmp6_free_dev(idev
);
340 kfree_rcu(idev
, rcu
);
342 EXPORT_SYMBOL(in6_dev_finish_destroy
);
344 static struct inet6_dev
*ipv6_add_dev(struct net_device
*dev
)
346 struct inet6_dev
*ndev
;
350 if (dev
->mtu
< IPV6_MIN_MTU
)
353 ndev
= kzalloc(sizeof(struct inet6_dev
), GFP_KERNEL
);
358 rwlock_init(&ndev
->lock
);
360 INIT_LIST_HEAD(&ndev
->addr_list
);
362 memcpy(&ndev
->cnf
, dev_net(dev
)->ipv6
.devconf_dflt
, sizeof(ndev
->cnf
));
363 ndev
->cnf
.mtu6
= dev
->mtu
;
364 ndev
->cnf
.sysctl
= NULL
;
365 ndev
->nd_parms
= neigh_parms_alloc(dev
, &nd_tbl
);
366 if (ndev
->nd_parms
== NULL
) {
370 if (ndev
->cnf
.forwarding
)
371 dev_disable_lro(dev
);
372 /* We refer to the device */
375 if (snmp6_alloc_dev(ndev
) < 0) {
377 "%s: cannot allocate memory for statistics; dev=%s.\n",
378 __func__
, dev
->name
));
379 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
385 if (snmp6_register_dev(ndev
) < 0) {
387 "%s: cannot create /proc/net/dev_snmp6/%s\n",
388 __func__
, dev
->name
));
389 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
391 in6_dev_finish_destroy(ndev
);
395 /* One reference from device. We must do this before
396 * we invoke __ipv6_regen_rndid().
400 if (dev
->flags
& (IFF_NOARP
| IFF_LOOPBACK
))
401 ndev
->cnf
.accept_dad
= -1;
403 #if IS_ENABLED(CONFIG_IPV6_SIT)
404 if (dev
->type
== ARPHRD_SIT
&& (dev
->priv_flags
& IFF_ISATAP
)) {
405 pr_info("%s: Disabled Multicast RS\n", dev
->name
);
406 ndev
->cnf
.rtr_solicits
= 0;
410 #ifdef CONFIG_IPV6_PRIVACY
411 INIT_LIST_HEAD(&ndev
->tempaddr_list
);
412 setup_timer(&ndev
->regen_timer
, ipv6_regen_rndid
, (unsigned long)ndev
);
413 if ((dev
->flags
&IFF_LOOPBACK
) ||
414 dev
->type
== ARPHRD_TUNNEL
||
415 dev
->type
== ARPHRD_TUNNEL6
||
416 dev
->type
== ARPHRD_SIT
||
417 dev
->type
== ARPHRD_NONE
) {
418 ndev
->cnf
.use_tempaddr
= -1;
421 ipv6_regen_rndid((unsigned long) ndev
);
425 if (netif_running(dev
) && addrconf_qdisc_ok(dev
))
426 ndev
->if_flags
|= IF_READY
;
428 ipv6_mc_init_dev(ndev
);
429 ndev
->tstamp
= jiffies
;
430 addrconf_sysctl_register(ndev
);
431 /* protected by rtnl_lock */
432 rcu_assign_pointer(dev
->ip6_ptr
, ndev
);
434 /* Join interface-local all-node multicast group */
435 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allnodes
);
437 /* Join all-node multicast group */
438 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allnodes
);
440 /* Join all-router multicast group if forwarding is set */
441 if (ndev
->cnf
.forwarding
&& (dev
->flags
& IFF_MULTICAST
))
442 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
447 static struct inet6_dev
*ipv6_find_idev(struct net_device
*dev
)
449 struct inet6_dev
*idev
;
453 idev
= __in6_dev_get(dev
);
455 idev
= ipv6_add_dev(dev
);
460 if (dev
->flags
&IFF_UP
)
465 static int inet6_netconf_msgsize_devconf(int type
)
467 int size
= NLMSG_ALIGN(sizeof(struct netconfmsg
))
468 + nla_total_size(4); /* NETCONFA_IFINDEX */
470 /* type -1 is used for ALL */
471 if (type
== -1 || type
== NETCONFA_FORWARDING
)
472 size
+= nla_total_size(4);
473 #ifdef CONFIG_IPV6_MROUTE
474 if (type
== -1 || type
== NETCONFA_MC_FORWARDING
)
475 size
+= nla_total_size(4);
481 static int inet6_netconf_fill_devconf(struct sk_buff
*skb
, int ifindex
,
482 struct ipv6_devconf
*devconf
, u32 portid
,
483 u32 seq
, int event
, unsigned int flags
,
486 struct nlmsghdr
*nlh
;
487 struct netconfmsg
*ncm
;
489 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct netconfmsg
),
494 ncm
= nlmsg_data(nlh
);
495 ncm
->ncm_family
= AF_INET6
;
497 if (nla_put_s32(skb
, NETCONFA_IFINDEX
, ifindex
) < 0)
498 goto nla_put_failure
;
500 /* type -1 is used for ALL */
501 if ((type
== -1 || type
== NETCONFA_FORWARDING
) &&
502 nla_put_s32(skb
, NETCONFA_FORWARDING
, devconf
->forwarding
) < 0)
503 goto nla_put_failure
;
504 #ifdef CONFIG_IPV6_MROUTE
505 if ((type
== -1 || type
== NETCONFA_MC_FORWARDING
) &&
506 nla_put_s32(skb
, NETCONFA_MC_FORWARDING
,
507 devconf
->mc_forwarding
) < 0)
508 goto nla_put_failure
;
510 return nlmsg_end(skb
, nlh
);
513 nlmsg_cancel(skb
, nlh
);
517 void inet6_netconf_notify_devconf(struct net
*net
, int type
, int ifindex
,
518 struct ipv6_devconf
*devconf
)
523 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(type
), GFP_ATOMIC
);
527 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
, 0, 0,
528 RTM_NEWNETCONF
, 0, type
);
530 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
531 WARN_ON(err
== -EMSGSIZE
);
535 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_NETCONF
, NULL
, GFP_ATOMIC
);
538 rtnl_set_sk_err(net
, RTNLGRP_IPV6_NETCONF
, err
);
541 static const struct nla_policy devconf_ipv6_policy
[NETCONFA_MAX
+1] = {
542 [NETCONFA_IFINDEX
] = { .len
= sizeof(int) },
543 [NETCONFA_FORWARDING
] = { .len
= sizeof(int) },
546 static int inet6_netconf_get_devconf(struct sk_buff
*in_skb
,
547 struct nlmsghdr
*nlh
)
549 struct net
*net
= sock_net(in_skb
->sk
);
550 struct nlattr
*tb
[NETCONFA_MAX
+1];
551 struct netconfmsg
*ncm
;
553 struct ipv6_devconf
*devconf
;
554 struct inet6_dev
*in6_dev
;
555 struct net_device
*dev
;
559 err
= nlmsg_parse(nlh
, sizeof(*ncm
), tb
, NETCONFA_MAX
,
560 devconf_ipv6_policy
);
565 if (!tb
[NETCONFA_IFINDEX
])
568 ifindex
= nla_get_s32(tb
[NETCONFA_IFINDEX
]);
570 case NETCONFA_IFINDEX_ALL
:
571 devconf
= net
->ipv6
.devconf_all
;
573 case NETCONFA_IFINDEX_DEFAULT
:
574 devconf
= net
->ipv6
.devconf_dflt
;
577 dev
= __dev_get_by_index(net
, ifindex
);
580 in6_dev
= __in6_dev_get(dev
);
583 devconf
= &in6_dev
->cnf
;
588 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC
);
592 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
,
593 NETLINK_CB(in_skb
).portid
,
594 nlh
->nlmsg_seq
, RTM_NEWNETCONF
, 0,
597 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
598 WARN_ON(err
== -EMSGSIZE
);
602 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
607 static int inet6_netconf_dump_devconf(struct sk_buff
*skb
,
608 struct netlink_callback
*cb
)
610 struct net
*net
= sock_net(skb
->sk
);
613 struct net_device
*dev
;
614 struct inet6_dev
*idev
;
615 struct hlist_head
*head
;
618 s_idx
= idx
= cb
->args
[1];
620 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
622 head
= &net
->dev_index_head
[h
];
624 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
627 idev
= __in6_dev_get(dev
);
631 if (inet6_netconf_fill_devconf(skb
, dev
->ifindex
,
633 NETLINK_CB(cb
->skb
).portid
,
646 if (h
== NETDEV_HASHENTRIES
) {
647 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_ALL
,
648 net
->ipv6
.devconf_all
,
649 NETLINK_CB(cb
->skb
).portid
,
651 RTM_NEWNETCONF
, NLM_F_MULTI
,
657 if (h
== NETDEV_HASHENTRIES
+ 1) {
658 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_DEFAULT
,
659 net
->ipv6
.devconf_dflt
,
660 NETLINK_CB(cb
->skb
).portid
,
662 RTM_NEWNETCONF
, NLM_F_MULTI
,
676 static void dev_forward_change(struct inet6_dev
*idev
)
678 struct net_device
*dev
;
679 struct inet6_ifaddr
*ifa
;
684 if (idev
->cnf
.forwarding
)
685 dev_disable_lro(dev
);
686 if (dev
->flags
& IFF_MULTICAST
) {
687 if (idev
->cnf
.forwarding
) {
688 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
689 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allrouters
);
690 ipv6_dev_mc_inc(dev
, &in6addr_sitelocal_allrouters
);
692 ipv6_dev_mc_dec(dev
, &in6addr_linklocal_allrouters
);
693 ipv6_dev_mc_dec(dev
, &in6addr_interfacelocal_allrouters
);
694 ipv6_dev_mc_dec(dev
, &in6addr_sitelocal_allrouters
);
698 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
699 if (ifa
->flags
&IFA_F_TENTATIVE
)
701 if (idev
->cnf
.forwarding
)
702 addrconf_join_anycast(ifa
);
704 addrconf_leave_anycast(ifa
);
706 inet6_netconf_notify_devconf(dev_net(dev
), NETCONFA_FORWARDING
,
707 dev
->ifindex
, &idev
->cnf
);
711 static void addrconf_forward_change(struct net
*net
, __s32 newf
)
713 struct net_device
*dev
;
714 struct inet6_dev
*idev
;
716 for_each_netdev(net
, dev
) {
717 idev
= __in6_dev_get(dev
);
719 int changed
= (!idev
->cnf
.forwarding
) ^ (!newf
);
720 idev
->cnf
.forwarding
= newf
;
722 dev_forward_change(idev
);
727 static int addrconf_fixup_forwarding(struct ctl_table
*table
, int *p
, int newf
)
733 return restart_syscall();
735 net
= (struct net
*)table
->extra2
;
739 if (p
== &net
->ipv6
.devconf_dflt
->forwarding
) {
740 if ((!newf
) ^ (!old
))
741 inet6_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
742 NETCONFA_IFINDEX_DEFAULT
,
743 net
->ipv6
.devconf_dflt
);
748 if (p
== &net
->ipv6
.devconf_all
->forwarding
) {
749 net
->ipv6
.devconf_dflt
->forwarding
= newf
;
750 addrconf_forward_change(net
, newf
);
751 if ((!newf
) ^ (!old
))
752 inet6_netconf_notify_devconf(net
, NETCONFA_FORWARDING
,
753 NETCONFA_IFINDEX_ALL
,
754 net
->ipv6
.devconf_all
);
755 } else if ((!newf
) ^ (!old
))
756 dev_forward_change((struct inet6_dev
*)table
->extra1
);
760 rt6_purge_dflt_routers(net
);
765 /* Nobody refers to this ifaddr, destroy it */
766 void inet6_ifa_finish_destroy(struct inet6_ifaddr
*ifp
)
768 WARN_ON(!hlist_unhashed(&ifp
->addr_lst
));
770 #ifdef NET_REFCNT_DEBUG
771 pr_debug("%s\n", __func__
);
774 in6_dev_put(ifp
->idev
);
776 if (del_timer(&ifp
->timer
))
777 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp
);
779 if (ifp
->state
!= INET6_IFADDR_STATE_DEAD
) {
780 pr_warn("Freeing alive inet6 address %p\n", ifp
);
789 ipv6_link_dev_addr(struct inet6_dev
*idev
, struct inet6_ifaddr
*ifp
)
792 int ifp_scope
= ipv6_addr_src_scope(&ifp
->addr
);
795 * Each device address list is sorted in order of scope -
796 * global before linklocal.
798 list_for_each(p
, &idev
->addr_list
) {
799 struct inet6_ifaddr
*ifa
800 = list_entry(p
, struct inet6_ifaddr
, if_list
);
801 if (ifp_scope
>= ipv6_addr_src_scope(&ifa
->addr
))
805 list_add_tail(&ifp
->if_list
, p
);
808 static u32
inet6_addr_hash(const struct in6_addr
*addr
)
810 return hash_32(ipv6_addr_hash(addr
), IN6_ADDR_HSIZE_SHIFT
);
813 /* On success it returns ifp with increased reference count */
815 static struct inet6_ifaddr
*
816 ipv6_add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
, int pfxlen
,
817 int scope
, u32 flags
)
819 struct inet6_ifaddr
*ifa
= NULL
;
823 int addr_type
= ipv6_addr_type(addr
);
825 if (addr_type
== IPV6_ADDR_ANY
||
826 addr_type
& IPV6_ADDR_MULTICAST
||
827 (!(idev
->dev
->flags
& IFF_LOOPBACK
) &&
828 addr_type
& IPV6_ADDR_LOOPBACK
))
829 return ERR_PTR(-EADDRNOTAVAIL
);
833 err
= -ENODEV
; /*XXX*/
837 if (idev
->cnf
.disable_ipv6
) {
842 spin_lock(&addrconf_hash_lock
);
844 /* Ignore adding duplicate addresses on an interface */
845 if (ipv6_chk_same_addr(dev_net(idev
->dev
), addr
, idev
->dev
)) {
846 ADBG(("ipv6_add_addr: already assigned\n"));
851 ifa
= kzalloc(sizeof(struct inet6_ifaddr
), GFP_ATOMIC
);
854 ADBG(("ipv6_add_addr: malloc failed\n"));
859 rt
= addrconf_dst_alloc(idev
, addr
, false);
867 spin_lock_init(&ifa
->lock
);
868 spin_lock_init(&ifa
->state_lock
);
869 init_timer(&ifa
->timer
);
870 INIT_HLIST_NODE(&ifa
->addr_lst
);
871 ifa
->timer
.data
= (unsigned long) ifa
;
873 ifa
->prefix_len
= pfxlen
;
874 ifa
->flags
= flags
| IFA_F_TENTATIVE
;
875 ifa
->cstamp
= ifa
->tstamp
= jiffies
;
884 /* Add to big hash table */
885 hash
= inet6_addr_hash(addr
);
887 hlist_add_head_rcu(&ifa
->addr_lst
, &inet6_addr_lst
[hash
]);
888 spin_unlock(&addrconf_hash_lock
);
890 write_lock(&idev
->lock
);
891 /* Add to inet6_dev unicast addr list. */
892 ipv6_link_dev_addr(idev
, ifa
);
894 #ifdef CONFIG_IPV6_PRIVACY
895 if (ifa
->flags
&IFA_F_TEMPORARY
) {
896 list_add(&ifa
->tmp_list
, &idev
->tempaddr_list
);
902 write_unlock(&idev
->lock
);
904 rcu_read_unlock_bh();
906 if (likely(err
== 0))
907 atomic_notifier_call_chain(&inet6addr_chain
, NETDEV_UP
, ifa
);
915 spin_unlock(&addrconf_hash_lock
);
919 /* This function wants to get referenced ifp and releases it before return */
921 static void ipv6_del_addr(struct inet6_ifaddr
*ifp
)
923 struct inet6_ifaddr
*ifa
, *ifn
;
924 struct inet6_dev
*idev
= ifp
->idev
;
926 int deleted
= 0, onlink
= 0;
927 unsigned long expires
= jiffies
;
929 spin_lock_bh(&ifp
->state_lock
);
931 ifp
->state
= INET6_IFADDR_STATE_DEAD
;
932 spin_unlock_bh(&ifp
->state_lock
);
934 if (state
== INET6_IFADDR_STATE_DEAD
)
937 spin_lock_bh(&addrconf_hash_lock
);
938 hlist_del_init_rcu(&ifp
->addr_lst
);
939 spin_unlock_bh(&addrconf_hash_lock
);
941 write_lock_bh(&idev
->lock
);
942 #ifdef CONFIG_IPV6_PRIVACY
943 if (ifp
->flags
&IFA_F_TEMPORARY
) {
944 list_del(&ifp
->tmp_list
);
946 in6_ifa_put(ifp
->ifpub
);
953 list_for_each_entry_safe(ifa
, ifn
, &idev
->addr_list
, if_list
) {
955 list_del_init(&ifp
->if_list
);
958 if (!(ifp
->flags
& IFA_F_PERMANENT
) || onlink
> 0)
962 } else if (ifp
->flags
& IFA_F_PERMANENT
) {
963 if (ipv6_prefix_equal(&ifa
->addr
, &ifp
->addr
,
965 if (ifa
->flags
& IFA_F_PERMANENT
) {
970 unsigned long lifetime
;
975 spin_lock(&ifa
->lock
);
977 lifetime
= addrconf_timeout_fixup(ifa
->valid_lft
, HZ
);
979 * Note: Because this address is
980 * not permanent, lifetime <
981 * LONG_MAX / HZ here.
983 if (time_before(expires
,
984 ifa
->tstamp
+ lifetime
* HZ
))
985 expires
= ifa
->tstamp
+ lifetime
* HZ
;
986 spin_unlock(&ifa
->lock
);
991 write_unlock_bh(&idev
->lock
);
993 addrconf_del_timer(ifp
);
995 ipv6_ifa_notify(RTM_DELADDR
, ifp
);
997 atomic_notifier_call_chain(&inet6addr_chain
, NETDEV_DOWN
, ifp
);
1000 * Purge or update corresponding prefix
1002 * 1) we don't purge prefix here if address was not permanent.
1003 * prefix is managed by its own lifetime.
1004 * 2) if there're no addresses, delete prefix.
1005 * 3) if there're still other permanent address(es),
1006 * corresponding prefix is still permanent.
1007 * 4) otherwise, update prefix lifetime to the
1008 * longest valid lifetime among the corresponding
1009 * addresses on the device.
1010 * Note: subsequent RA will update lifetime.
1014 if ((ifp
->flags
& IFA_F_PERMANENT
) && onlink
< 1) {
1015 struct in6_addr prefix
;
1016 struct rt6_info
*rt
;
1018 ipv6_addr_prefix(&prefix
, &ifp
->addr
, ifp
->prefix_len
);
1020 rt
= addrconf_get_prefix_route(&prefix
,
1023 0, RTF_GATEWAY
| RTF_DEFAULT
);
1029 } else if (!(rt
->rt6i_flags
& RTF_EXPIRES
)) {
1030 rt6_set_expires(rt
, expires
);
1036 /* clean up prefsrc entries */
1037 rt6_remove_prefsrc(ifp
);
1042 #ifdef CONFIG_IPV6_PRIVACY
1043 static int ipv6_create_tempaddr(struct inet6_ifaddr
*ifp
, struct inet6_ifaddr
*ift
)
1045 struct inet6_dev
*idev
= ifp
->idev
;
1046 struct in6_addr addr
, *tmpaddr
;
1047 unsigned long tmp_prefered_lft
, tmp_valid_lft
, tmp_tstamp
, age
;
1048 unsigned long regen_advance
;
1053 unsigned long now
= jiffies
;
1055 write_lock(&idev
->lock
);
1057 spin_lock_bh(&ift
->lock
);
1058 memcpy(&addr
.s6_addr
[8], &ift
->addr
.s6_addr
[8], 8);
1059 spin_unlock_bh(&ift
->lock
);
1066 if (idev
->cnf
.use_tempaddr
<= 0) {
1067 write_unlock(&idev
->lock
);
1068 pr_info("%s: use_tempaddr is disabled\n", __func__
);
1073 spin_lock_bh(&ifp
->lock
);
1074 if (ifp
->regen_count
++ >= idev
->cnf
.regen_max_retry
) {
1075 idev
->cnf
.use_tempaddr
= -1; /*XXX*/
1076 spin_unlock_bh(&ifp
->lock
);
1077 write_unlock(&idev
->lock
);
1078 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1085 memcpy(addr
.s6_addr
, ifp
->addr
.s6_addr
, 8);
1086 __ipv6_try_regen_rndid(idev
, tmpaddr
);
1087 memcpy(&addr
.s6_addr
[8], idev
->rndid
, 8);
1088 age
= (now
- ifp
->tstamp
) / HZ
;
1089 tmp_valid_lft
= min_t(__u32
,
1091 idev
->cnf
.temp_valid_lft
+ age
);
1092 tmp_prefered_lft
= min_t(__u32
,
1094 idev
->cnf
.temp_prefered_lft
+ age
-
1095 idev
->cnf
.max_desync_factor
);
1096 tmp_plen
= ifp
->prefix_len
;
1097 max_addresses
= idev
->cnf
.max_addresses
;
1098 tmp_tstamp
= ifp
->tstamp
;
1099 spin_unlock_bh(&ifp
->lock
);
1101 regen_advance
= idev
->cnf
.regen_max_retry
*
1102 idev
->cnf
.dad_transmits
*
1103 idev
->nd_parms
->retrans_time
/ HZ
;
1104 write_unlock(&idev
->lock
);
1106 /* A temporary address is created only if this calculated Preferred
1107 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1108 * an implementation must not create a temporary address with a zero
1109 * Preferred Lifetime.
1111 if (tmp_prefered_lft
<= regen_advance
) {
1118 addr_flags
= IFA_F_TEMPORARY
;
1119 /* set in addrconf_prefix_rcv() */
1120 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
1121 addr_flags
|= IFA_F_OPTIMISTIC
;
1123 ift
= !max_addresses
||
1124 ipv6_count_addresses(idev
) < max_addresses
?
1125 ipv6_add_addr(idev
, &addr
, tmp_plen
,
1126 ipv6_addr_type(&addr
)&IPV6_ADDR_SCOPE_MASK
,
1128 if (IS_ERR_OR_NULL(ift
)) {
1131 pr_info("%s: retry temporary address regeneration\n", __func__
);
1133 write_lock(&idev
->lock
);
1137 spin_lock_bh(&ift
->lock
);
1139 ift
->valid_lft
= tmp_valid_lft
;
1140 ift
->prefered_lft
= tmp_prefered_lft
;
1142 ift
->tstamp
= tmp_tstamp
;
1143 spin_unlock_bh(&ift
->lock
);
1145 addrconf_dad_start(ift
);
1154 * Choose an appropriate source address (RFC3484)
1157 IPV6_SADDR_RULE_INIT
= 0,
1158 IPV6_SADDR_RULE_LOCAL
,
1159 IPV6_SADDR_RULE_SCOPE
,
1160 IPV6_SADDR_RULE_PREFERRED
,
1161 #ifdef CONFIG_IPV6_MIP6
1162 IPV6_SADDR_RULE_HOA
,
1164 IPV6_SADDR_RULE_OIF
,
1165 IPV6_SADDR_RULE_LABEL
,
1166 #ifdef CONFIG_IPV6_PRIVACY
1167 IPV6_SADDR_RULE_PRIVACY
,
1169 IPV6_SADDR_RULE_ORCHID
,
1170 IPV6_SADDR_RULE_PREFIX
,
1174 struct ipv6_saddr_score
{
1177 struct inet6_ifaddr
*ifa
;
1178 DECLARE_BITMAP(scorebits
, IPV6_SADDR_RULE_MAX
);
1183 struct ipv6_saddr_dst
{
1184 const struct in6_addr
*addr
;
1191 static inline int ipv6_saddr_preferred(int type
)
1193 if (type
& (IPV6_ADDR_MAPPED
|IPV6_ADDR_COMPATv4
|IPV6_ADDR_LOOPBACK
))
1198 static int ipv6_get_saddr_eval(struct net
*net
,
1199 struct ipv6_saddr_score
*score
,
1200 struct ipv6_saddr_dst
*dst
,
1205 if (i
<= score
->rule
) {
1207 case IPV6_SADDR_RULE_SCOPE
:
1208 ret
= score
->scopedist
;
1210 case IPV6_SADDR_RULE_PREFIX
:
1211 ret
= score
->matchlen
;
1214 ret
= !!test_bit(i
, score
->scorebits
);
1220 case IPV6_SADDR_RULE_INIT
:
1221 /* Rule 0: remember if hiscore is not ready yet */
1224 case IPV6_SADDR_RULE_LOCAL
:
1225 /* Rule 1: Prefer same address */
1226 ret
= ipv6_addr_equal(&score
->ifa
->addr
, dst
->addr
);
1228 case IPV6_SADDR_RULE_SCOPE
:
1229 /* Rule 2: Prefer appropriate scope
1234 * ---+--+-+---> scope
1236 * | d is scope of the destination.
1238 * | \ <- smaller scope is better if
1239 * B-15 | \ if scope is enough for destinaion.
1240 * | ret = B - scope (-1 <= scope >= d <= 15).
1242 * |/ <- greater is better
1243 * -C / if scope is not enough for destination.
1244 * /| ret = scope - C (-1 <= d < scope <= 15).
1246 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1247 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1248 * Assume B = 0 and we get C > 29.
1250 ret
= __ipv6_addr_src_scope(score
->addr_type
);
1251 if (ret
>= dst
->scope
)
1254 ret
-= 128; /* 30 is enough */
1255 score
->scopedist
= ret
;
1257 case IPV6_SADDR_RULE_PREFERRED
:
1258 /* Rule 3: Avoid deprecated and optimistic addresses */
1259 ret
= ipv6_saddr_preferred(score
->addr_type
) ||
1260 !(score
->ifa
->flags
& (IFA_F_DEPRECATED
|IFA_F_OPTIMISTIC
));
1262 #ifdef CONFIG_IPV6_MIP6
1263 case IPV6_SADDR_RULE_HOA
:
1265 /* Rule 4: Prefer home address */
1266 int prefhome
= !(dst
->prefs
& IPV6_PREFER_SRC_COA
);
1267 ret
= !(score
->ifa
->flags
& IFA_F_HOMEADDRESS
) ^ prefhome
;
1271 case IPV6_SADDR_RULE_OIF
:
1272 /* Rule 5: Prefer outgoing interface */
1273 ret
= (!dst
->ifindex
||
1274 dst
->ifindex
== score
->ifa
->idev
->dev
->ifindex
);
1276 case IPV6_SADDR_RULE_LABEL
:
1277 /* Rule 6: Prefer matching label */
1278 ret
= ipv6_addr_label(net
,
1279 &score
->ifa
->addr
, score
->addr_type
,
1280 score
->ifa
->idev
->dev
->ifindex
) == dst
->label
;
1282 #ifdef CONFIG_IPV6_PRIVACY
1283 case IPV6_SADDR_RULE_PRIVACY
:
1285 /* Rule 7: Prefer public address
1286 * Note: prefer temporary address if use_tempaddr >= 2
1288 int preftmp
= dst
->prefs
& (IPV6_PREFER_SRC_PUBLIC
|IPV6_PREFER_SRC_TMP
) ?
1289 !!(dst
->prefs
& IPV6_PREFER_SRC_TMP
) :
1290 score
->ifa
->idev
->cnf
.use_tempaddr
>= 2;
1291 ret
= (!(score
->ifa
->flags
& IFA_F_TEMPORARY
)) ^ preftmp
;
1295 case IPV6_SADDR_RULE_ORCHID
:
1296 /* Rule 8-: Prefer ORCHID vs ORCHID or
1297 * non-ORCHID vs non-ORCHID
1299 ret
= !(ipv6_addr_orchid(&score
->ifa
->addr
) ^
1300 ipv6_addr_orchid(dst
->addr
));
1302 case IPV6_SADDR_RULE_PREFIX
:
1303 /* Rule 8: Use longest matching prefix */
1304 ret
= ipv6_addr_diff(&score
->ifa
->addr
, dst
->addr
);
1305 if (ret
> score
->ifa
->prefix_len
)
1306 ret
= score
->ifa
->prefix_len
;
1307 score
->matchlen
= ret
;
1314 __set_bit(i
, score
->scorebits
);
1320 int ipv6_dev_get_saddr(struct net
*net
, const struct net_device
*dst_dev
,
1321 const struct in6_addr
*daddr
, unsigned int prefs
,
1322 struct in6_addr
*saddr
)
1324 struct ipv6_saddr_score scores
[2],
1325 *score
= &scores
[0], *hiscore
= &scores
[1];
1326 struct ipv6_saddr_dst dst
;
1327 struct net_device
*dev
;
1330 dst_type
= __ipv6_addr_type(daddr
);
1332 dst
.ifindex
= dst_dev
? dst_dev
->ifindex
: 0;
1333 dst
.scope
= __ipv6_addr_src_scope(dst_type
);
1334 dst
.label
= ipv6_addr_label(net
, daddr
, dst_type
, dst
.ifindex
);
1338 hiscore
->ifa
= NULL
;
1342 for_each_netdev_rcu(net
, dev
) {
1343 struct inet6_dev
*idev
;
1345 /* Candidate Source Address (section 4)
1346 * - multicast and link-local destination address,
1347 * the set of candidate source address MUST only
1348 * include addresses assigned to interfaces
1349 * belonging to the same link as the outgoing
1351 * (- For site-local destination addresses, the
1352 * set of candidate source addresses MUST only
1353 * include addresses assigned to interfaces
1354 * belonging to the same site as the outgoing
1357 if (((dst_type
& IPV6_ADDR_MULTICAST
) ||
1358 dst
.scope
<= IPV6_ADDR_SCOPE_LINKLOCAL
) &&
1359 dst
.ifindex
&& dev
->ifindex
!= dst
.ifindex
)
1362 idev
= __in6_dev_get(dev
);
1366 read_lock_bh(&idev
->lock
);
1367 list_for_each_entry(score
->ifa
, &idev
->addr_list
, if_list
) {
1371 * - Tentative Address (RFC2462 section 5.4)
1372 * - A tentative address is not considered
1373 * "assigned to an interface" in the traditional
1374 * sense, unless it is also flagged as optimistic.
1375 * - Candidate Source Address (section 4)
1376 * - In any case, anycast addresses, multicast
1377 * addresses, and the unspecified address MUST
1378 * NOT be included in a candidate set.
1380 if ((score
->ifa
->flags
& IFA_F_TENTATIVE
) &&
1381 (!(score
->ifa
->flags
& IFA_F_OPTIMISTIC
)))
1384 score
->addr_type
= __ipv6_addr_type(&score
->ifa
->addr
);
1386 if (unlikely(score
->addr_type
== IPV6_ADDR_ANY
||
1387 score
->addr_type
& IPV6_ADDR_MULTICAST
)) {
1388 LIMIT_NETDEBUG(KERN_DEBUG
1389 "ADDRCONF: unspecified / multicast address "
1390 "assigned as unicast address on %s",
1396 bitmap_zero(score
->scorebits
, IPV6_SADDR_RULE_MAX
);
1398 for (i
= 0; i
< IPV6_SADDR_RULE_MAX
; i
++) {
1399 int minihiscore
, miniscore
;
1401 minihiscore
= ipv6_get_saddr_eval(net
, hiscore
, &dst
, i
);
1402 miniscore
= ipv6_get_saddr_eval(net
, score
, &dst
, i
);
1404 if (minihiscore
> miniscore
) {
1405 if (i
== IPV6_SADDR_RULE_SCOPE
&&
1406 score
->scopedist
> 0) {
1409 * each remaining entry
1410 * has too small (not enough)
1411 * scope, because ifa entries
1412 * are sorted by their scope
1418 } else if (minihiscore
< miniscore
) {
1420 in6_ifa_put(hiscore
->ifa
);
1422 in6_ifa_hold(score
->ifa
);
1424 swap(hiscore
, score
);
1426 /* restore our iterator */
1427 score
->ifa
= hiscore
->ifa
;
1434 read_unlock_bh(&idev
->lock
);
1439 return -EADDRNOTAVAIL
;
1441 *saddr
= hiscore
->ifa
->addr
;
1442 in6_ifa_put(hiscore
->ifa
);
1445 EXPORT_SYMBOL(ipv6_dev_get_saddr
);
1447 int ipv6_get_lladdr(struct net_device
*dev
, struct in6_addr
*addr
,
1448 unsigned char banned_flags
)
1450 struct inet6_dev
*idev
;
1451 int err
= -EADDRNOTAVAIL
;
1454 idev
= __in6_dev_get(dev
);
1456 struct inet6_ifaddr
*ifp
;
1458 read_lock_bh(&idev
->lock
);
1459 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
1460 if (ifp
->scope
== IFA_LINK
&&
1461 !(ifp
->flags
& banned_flags
)) {
1467 read_unlock_bh(&idev
->lock
);
1473 static int ipv6_count_addresses(struct inet6_dev
*idev
)
1476 struct inet6_ifaddr
*ifp
;
1478 read_lock_bh(&idev
->lock
);
1479 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
)
1481 read_unlock_bh(&idev
->lock
);
1485 int ipv6_chk_addr(struct net
*net
, const struct in6_addr
*addr
,
1486 struct net_device
*dev
, int strict
)
1488 struct inet6_ifaddr
*ifp
;
1489 unsigned int hash
= inet6_addr_hash(addr
);
1492 hlist_for_each_entry_rcu(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1493 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1495 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
1496 !(ifp
->flags
&IFA_F_TENTATIVE
) &&
1497 (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1498 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
))) {
1499 rcu_read_unlock_bh();
1504 rcu_read_unlock_bh();
1507 EXPORT_SYMBOL(ipv6_chk_addr
);
1509 static bool ipv6_chk_same_addr(struct net
*net
, const struct in6_addr
*addr
,
1510 struct net_device
*dev
)
1512 unsigned int hash
= inet6_addr_hash(addr
);
1513 struct inet6_ifaddr
*ifp
;
1515 hlist_for_each_entry(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1516 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1518 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1519 if (dev
== NULL
|| ifp
->idev
->dev
== dev
)
1526 int ipv6_chk_prefix(const struct in6_addr
*addr
, struct net_device
*dev
)
1528 struct inet6_dev
*idev
;
1529 struct inet6_ifaddr
*ifa
;
1534 idev
= __in6_dev_get(dev
);
1536 read_lock_bh(&idev
->lock
);
1537 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
1538 onlink
= ipv6_prefix_equal(addr
, &ifa
->addr
,
1543 read_unlock_bh(&idev
->lock
);
1548 EXPORT_SYMBOL(ipv6_chk_prefix
);
1550 struct inet6_ifaddr
*ipv6_get_ifaddr(struct net
*net
, const struct in6_addr
*addr
,
1551 struct net_device
*dev
, int strict
)
1553 struct inet6_ifaddr
*ifp
, *result
= NULL
;
1554 unsigned int hash
= inet6_addr_hash(addr
);
1557 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
1558 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
1560 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1561 if (dev
== NULL
|| ifp
->idev
->dev
== dev
||
1562 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
)) {
1569 rcu_read_unlock_bh();
1574 /* Gets referenced address, destroys ifaddr */
1576 static void addrconf_dad_stop(struct inet6_ifaddr
*ifp
, int dad_failed
)
1578 if (ifp
->flags
&IFA_F_PERMANENT
) {
1579 spin_lock_bh(&ifp
->lock
);
1580 addrconf_del_timer(ifp
);
1581 ifp
->flags
|= IFA_F_TENTATIVE
;
1583 ifp
->flags
|= IFA_F_DADFAILED
;
1584 spin_unlock_bh(&ifp
->lock
);
1586 ipv6_ifa_notify(0, ifp
);
1588 #ifdef CONFIG_IPV6_PRIVACY
1589 } else if (ifp
->flags
&IFA_F_TEMPORARY
) {
1590 struct inet6_ifaddr
*ifpub
;
1591 spin_lock_bh(&ifp
->lock
);
1594 in6_ifa_hold(ifpub
);
1595 spin_unlock_bh(&ifp
->lock
);
1596 ipv6_create_tempaddr(ifpub
, ifp
);
1599 spin_unlock_bh(&ifp
->lock
);
1607 static int addrconf_dad_end(struct inet6_ifaddr
*ifp
)
1611 spin_lock(&ifp
->state_lock
);
1612 if (ifp
->state
== INET6_IFADDR_STATE_DAD
) {
1613 ifp
->state
= INET6_IFADDR_STATE_POSTDAD
;
1616 spin_unlock(&ifp
->state_lock
);
1621 void addrconf_dad_failure(struct inet6_ifaddr
*ifp
)
1623 struct inet6_dev
*idev
= ifp
->idev
;
1625 if (addrconf_dad_end(ifp
)) {
1630 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1631 ifp
->idev
->dev
->name
, &ifp
->addr
);
1633 if (idev
->cnf
.accept_dad
> 1 && !idev
->cnf
.disable_ipv6
) {
1634 struct in6_addr addr
;
1636 addr
.s6_addr32
[0] = htonl(0xfe800000);
1637 addr
.s6_addr32
[1] = 0;
1639 if (!ipv6_generate_eui64(addr
.s6_addr
+ 8, idev
->dev
) &&
1640 ipv6_addr_equal(&ifp
->addr
, &addr
)) {
1641 /* DAD failed for link-local based on MAC address */
1642 idev
->cnf
.disable_ipv6
= 1;
1644 pr_info("%s: IPv6 being disabled!\n",
1645 ifp
->idev
->dev
->name
);
1649 addrconf_dad_stop(ifp
, 1);
1652 /* Join to solicited addr multicast group. */
1654 void addrconf_join_solict(struct net_device
*dev
, const struct in6_addr
*addr
)
1656 struct in6_addr maddr
;
1658 if (dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1661 addrconf_addr_solict_mult(addr
, &maddr
);
1662 ipv6_dev_mc_inc(dev
, &maddr
);
1665 void addrconf_leave_solict(struct inet6_dev
*idev
, const struct in6_addr
*addr
)
1667 struct in6_addr maddr
;
1669 if (idev
->dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
1672 addrconf_addr_solict_mult(addr
, &maddr
);
1673 __ipv6_dev_mc_dec(idev
, &maddr
);
1676 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
)
1678 struct in6_addr addr
;
1679 if (ifp
->prefix_len
== 127) /* RFC 6164 */
1681 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1682 if (ipv6_addr_any(&addr
))
1684 ipv6_dev_ac_inc(ifp
->idev
->dev
, &addr
);
1687 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
)
1689 struct in6_addr addr
;
1690 if (ifp
->prefix_len
== 127) /* RFC 6164 */
1692 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
1693 if (ipv6_addr_any(&addr
))
1695 __ipv6_dev_ac_dec(ifp
->idev
, &addr
);
1698 static int addrconf_ifid_eui48(u8
*eui
, struct net_device
*dev
)
1700 if (dev
->addr_len
!= ETH_ALEN
)
1702 memcpy(eui
, dev
->dev_addr
, 3);
1703 memcpy(eui
+ 5, dev
->dev_addr
+ 3, 3);
1706 * The zSeries OSA network cards can be shared among various
1707 * OS instances, but the OSA cards have only one MAC address.
1708 * This leads to duplicate address conflicts in conjunction
1709 * with IPv6 if more than one instance uses the same card.
1711 * The driver for these cards can deliver a unique 16-bit
1712 * identifier for each instance sharing the same card. It is
1713 * placed instead of 0xFFFE in the interface identifier. The
1714 * "u" bit of the interface identifier is not inverted in this
1715 * case. Hence the resulting interface identifier has local
1716 * scope according to RFC2373.
1719 eui
[3] = (dev
->dev_id
>> 8) & 0xFF;
1720 eui
[4] = dev
->dev_id
& 0xFF;
1729 static int addrconf_ifid_eui64(u8
*eui
, struct net_device
*dev
)
1731 if (dev
->addr_len
!= IEEE802154_ADDR_LEN
)
1733 memcpy(eui
, dev
->dev_addr
, 8);
1738 static int addrconf_ifid_arcnet(u8
*eui
, struct net_device
*dev
)
1740 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1741 if (dev
->addr_len
!= ARCNET_ALEN
)
1744 eui
[7] = *(u8
*)dev
->dev_addr
;
1748 static int addrconf_ifid_infiniband(u8
*eui
, struct net_device
*dev
)
1750 if (dev
->addr_len
!= INFINIBAND_ALEN
)
1752 memcpy(eui
, dev
->dev_addr
+ 12, 8);
1757 static int __ipv6_isatap_ifid(u8
*eui
, __be32 addr
)
1761 eui
[0] = (ipv4_is_zeronet(addr
) || ipv4_is_private_10(addr
) ||
1762 ipv4_is_loopback(addr
) || ipv4_is_linklocal_169(addr
) ||
1763 ipv4_is_private_172(addr
) || ipv4_is_test_192(addr
) ||
1764 ipv4_is_anycast_6to4(addr
) || ipv4_is_private_192(addr
) ||
1765 ipv4_is_test_198(addr
) || ipv4_is_multicast(addr
) ||
1766 ipv4_is_lbcast(addr
)) ? 0x00 : 0x02;
1770 memcpy(eui
+ 4, &addr
, 4);
1774 static int addrconf_ifid_sit(u8
*eui
, struct net_device
*dev
)
1776 if (dev
->priv_flags
& IFF_ISATAP
)
1777 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
1781 static int addrconf_ifid_gre(u8
*eui
, struct net_device
*dev
)
1783 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
1786 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
)
1788 switch (dev
->type
) {
1791 return addrconf_ifid_eui48(eui
, dev
);
1793 return addrconf_ifid_arcnet(eui
, dev
);
1794 case ARPHRD_INFINIBAND
:
1795 return addrconf_ifid_infiniband(eui
, dev
);
1797 return addrconf_ifid_sit(eui
, dev
);
1799 return addrconf_ifid_gre(eui
, dev
);
1800 case ARPHRD_IEEE802154
:
1801 return addrconf_ifid_eui64(eui
, dev
);
1806 static int ipv6_inherit_eui64(u8
*eui
, struct inet6_dev
*idev
)
1809 struct inet6_ifaddr
*ifp
;
1811 read_lock_bh(&idev
->lock
);
1812 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
1813 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
1814 memcpy(eui
, ifp
->addr
.s6_addr
+8, 8);
1819 read_unlock_bh(&idev
->lock
);
1823 #ifdef CONFIG_IPV6_PRIVACY
1824 /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1825 static void __ipv6_regen_rndid(struct inet6_dev
*idev
)
1828 get_random_bytes(idev
->rndid
, sizeof(idev
->rndid
));
1829 idev
->rndid
[0] &= ~0x02;
1832 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1833 * check if generated address is not inappropriate
1835 * - Reserved subnet anycast (RFC 2526)
1836 * 11111101 11....11 1xxxxxxx
1837 * - ISATAP (RFC4214) 6.1
1838 * 00-00-5E-FE-xx-xx-xx-xx
1840 * - XXX: already assigned to an address on the device
1842 if (idev
->rndid
[0] == 0xfd &&
1843 (idev
->rndid
[1]&idev
->rndid
[2]&idev
->rndid
[3]&idev
->rndid
[4]&idev
->rndid
[5]&idev
->rndid
[6]) == 0xff &&
1844 (idev
->rndid
[7]&0x80))
1846 if ((idev
->rndid
[0]|idev
->rndid
[1]) == 0) {
1847 if (idev
->rndid
[2] == 0x5e && idev
->rndid
[3] == 0xfe)
1849 if ((idev
->rndid
[2]|idev
->rndid
[3]|idev
->rndid
[4]|idev
->rndid
[5]|idev
->rndid
[6]|idev
->rndid
[7]) == 0x00)
1854 static void ipv6_regen_rndid(unsigned long data
)
1856 struct inet6_dev
*idev
= (struct inet6_dev
*) data
;
1857 unsigned long expires
;
1860 write_lock_bh(&idev
->lock
);
1865 __ipv6_regen_rndid(idev
);
1868 idev
->cnf
.temp_prefered_lft
* HZ
-
1869 idev
->cnf
.regen_max_retry
* idev
->cnf
.dad_transmits
* idev
->nd_parms
->retrans_time
-
1870 idev
->cnf
.max_desync_factor
* HZ
;
1871 if (time_before(expires
, jiffies
)) {
1872 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1873 __func__
, idev
->dev
->name
);
1877 if (!mod_timer(&idev
->regen_timer
, expires
))
1881 write_unlock_bh(&idev
->lock
);
1882 rcu_read_unlock_bh();
1886 static void __ipv6_try_regen_rndid(struct inet6_dev
*idev
, struct in6_addr
*tmpaddr
)
1888 if (tmpaddr
&& memcmp(idev
->rndid
, &tmpaddr
->s6_addr
[8], 8) == 0)
1889 __ipv6_regen_rndid(idev
);
1898 addrconf_prefix_route(struct in6_addr
*pfx
, int plen
, struct net_device
*dev
,
1899 unsigned long expires
, u32 flags
)
1901 struct fib6_config cfg
= {
1902 .fc_table
= RT6_TABLE_PREFIX
,
1903 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
1904 .fc_ifindex
= dev
->ifindex
,
1905 .fc_expires
= expires
,
1907 .fc_flags
= RTF_UP
| flags
,
1908 .fc_nlinfo
.nl_net
= dev_net(dev
),
1909 .fc_protocol
= RTPROT_KERNEL
,
1914 /* Prevent useless cloning on PtP SIT.
1915 This thing is done here expecting that the whole
1916 class of non-broadcast devices need not cloning.
1918 #if IS_ENABLED(CONFIG_IPV6_SIT)
1919 if (dev
->type
== ARPHRD_SIT
&& (dev
->flags
& IFF_POINTOPOINT
))
1920 cfg
.fc_flags
|= RTF_NONEXTHOP
;
1923 ip6_route_add(&cfg
);
1927 static struct rt6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
1929 const struct net_device
*dev
,
1930 u32 flags
, u32 noflags
)
1932 struct fib6_node
*fn
;
1933 struct rt6_info
*rt
= NULL
;
1934 struct fib6_table
*table
;
1936 table
= fib6_get_table(dev_net(dev
), RT6_TABLE_PREFIX
);
1940 read_lock_bh(&table
->tb6_lock
);
1941 fn
= fib6_locate(&table
->tb6_root
, pfx
, plen
, NULL
, 0);
1944 for (rt
= fn
->leaf
; rt
; rt
= rt
->dst
.rt6_next
) {
1945 if (rt
->dst
.dev
->ifindex
!= dev
->ifindex
)
1947 if ((rt
->rt6i_flags
& flags
) != flags
)
1949 if ((rt
->rt6i_flags
& noflags
) != 0)
1955 read_unlock_bh(&table
->tb6_lock
);
1960 /* Create "default" multicast route to the interface */
1962 static void addrconf_add_mroute(struct net_device
*dev
)
1964 struct fib6_config cfg
= {
1965 .fc_table
= RT6_TABLE_LOCAL
,
1966 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
1967 .fc_ifindex
= dev
->ifindex
,
1970 .fc_nlinfo
.nl_net
= dev_net(dev
),
1973 ipv6_addr_set(&cfg
.fc_dst
, htonl(0xFF000000), 0, 0, 0);
1975 ip6_route_add(&cfg
);
1978 #if IS_ENABLED(CONFIG_IPV6_SIT)
1979 static void sit_route_add(struct net_device
*dev
)
1981 struct fib6_config cfg
= {
1982 .fc_table
= RT6_TABLE_MAIN
,
1983 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
1984 .fc_ifindex
= dev
->ifindex
,
1986 .fc_flags
= RTF_UP
| RTF_NONEXTHOP
,
1987 .fc_nlinfo
.nl_net
= dev_net(dev
),
1990 /* prefix length - 96 bits "::d.d.d.d" */
1991 ip6_route_add(&cfg
);
1995 static struct inet6_dev
*addrconf_add_dev(struct net_device
*dev
)
1997 struct inet6_dev
*idev
;
2001 idev
= ipv6_find_idev(dev
);
2003 return ERR_PTR(-ENOBUFS
);
2005 if (idev
->cnf
.disable_ipv6
)
2006 return ERR_PTR(-EACCES
);
2008 /* Add default multicast route */
2009 if (!(dev
->flags
& IFF_LOOPBACK
))
2010 addrconf_add_mroute(dev
);
2015 void addrconf_prefix_rcv(struct net_device
*dev
, u8
*opt
, int len
, bool sllao
)
2017 struct prefix_info
*pinfo
;
2021 struct inet6_dev
*in6_dev
;
2022 struct net
*net
= dev_net(dev
);
2024 pinfo
= (struct prefix_info
*) opt
;
2026 if (len
< sizeof(struct prefix_info
)) {
2027 ADBG(("addrconf: prefix option too short\n"));
2032 * Validation checks ([ADDRCONF], page 19)
2035 addr_type
= ipv6_addr_type(&pinfo
->prefix
);
2037 if (addr_type
& (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
))
2040 valid_lft
= ntohl(pinfo
->valid
);
2041 prefered_lft
= ntohl(pinfo
->prefered
);
2043 if (prefered_lft
> valid_lft
) {
2044 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2048 in6_dev
= in6_dev_get(dev
);
2050 if (in6_dev
== NULL
) {
2051 net_dbg_ratelimited("addrconf: device %s not configured\n",
2057 * Two things going on here:
2058 * 1) Add routes for on-link prefixes
2059 * 2) Configure prefixes with the auto flag set
2062 if (pinfo
->onlink
) {
2063 struct rt6_info
*rt
;
2064 unsigned long rt_expires
;
2066 /* Avoid arithmetic overflow. Really, we could
2067 * save rt_expires in seconds, likely valid_lft,
2068 * but it would require division in fib gc, that it
2072 rt_expires
= addrconf_timeout_fixup(valid_lft
, HZ
);
2074 rt_expires
= addrconf_timeout_fixup(valid_lft
, USER_HZ
);
2076 if (addrconf_finite_timeout(rt_expires
))
2079 rt
= addrconf_get_prefix_route(&pinfo
->prefix
,
2082 RTF_ADDRCONF
| RTF_PREFIX_RT
,
2083 RTF_GATEWAY
| RTF_DEFAULT
);
2086 /* Autoconf prefix route */
2087 if (valid_lft
== 0) {
2090 } else if (addrconf_finite_timeout(rt_expires
)) {
2092 rt6_set_expires(rt
, jiffies
+ rt_expires
);
2094 rt6_clean_expires(rt
);
2096 } else if (valid_lft
) {
2097 clock_t expires
= 0;
2098 int flags
= RTF_ADDRCONF
| RTF_PREFIX_RT
;
2099 if (addrconf_finite_timeout(rt_expires
)) {
2101 flags
|= RTF_EXPIRES
;
2102 expires
= jiffies_to_clock_t(rt_expires
);
2104 addrconf_prefix_route(&pinfo
->prefix
, pinfo
->prefix_len
,
2105 dev
, expires
, flags
);
2110 /* Try to figure out our local address for this prefix */
2112 if (pinfo
->autoconf
&& in6_dev
->cnf
.autoconf
) {
2113 struct inet6_ifaddr
*ifp
;
2114 struct in6_addr addr
;
2115 int create
= 0, update_lft
= 0;
2117 if (pinfo
->prefix_len
== 64) {
2118 memcpy(&addr
, &pinfo
->prefix
, 8);
2119 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) &&
2120 ipv6_inherit_eui64(addr
.s6_addr
+ 8, in6_dev
)) {
2121 in6_dev_put(in6_dev
);
2126 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2128 in6_dev_put(in6_dev
);
2133 ifp
= ipv6_get_ifaddr(net
, &addr
, dev
, 1);
2135 if (ifp
== NULL
&& valid_lft
) {
2136 int max_addresses
= in6_dev
->cnf
.max_addresses
;
2139 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2140 if (in6_dev
->cnf
.optimistic_dad
&&
2141 !net
->ipv6
.devconf_all
->forwarding
&& sllao
)
2142 addr_flags
= IFA_F_OPTIMISTIC
;
2145 /* Do not allow to create too much of autoconfigured
2146 * addresses; this would be too easy way to crash kernel.
2148 if (!max_addresses
||
2149 ipv6_count_addresses(in6_dev
) < max_addresses
)
2150 ifp
= ipv6_add_addr(in6_dev
, &addr
, pinfo
->prefix_len
,
2151 addr_type
&IPV6_ADDR_SCOPE_MASK
,
2154 if (IS_ERR_OR_NULL(ifp
)) {
2155 in6_dev_put(in6_dev
);
2159 update_lft
= create
= 1;
2160 ifp
->cstamp
= jiffies
;
2161 addrconf_dad_start(ifp
);
2167 #ifdef CONFIG_IPV6_PRIVACY
2168 struct inet6_ifaddr
*ift
;
2172 /* update lifetime (RFC2462 5.5.3 e) */
2173 spin_lock(&ifp
->lock
);
2175 if (ifp
->valid_lft
> (now
- ifp
->tstamp
) / HZ
)
2176 stored_lft
= ifp
->valid_lft
- (now
- ifp
->tstamp
) / HZ
;
2179 if (!update_lft
&& stored_lft
) {
2180 if (valid_lft
> MIN_VALID_LIFETIME
||
2181 valid_lft
> stored_lft
)
2183 else if (stored_lft
<= MIN_VALID_LIFETIME
) {
2184 /* valid_lft <= stored_lft is always true */
2186 * RFC 4862 Section 5.5.3e:
2187 * "Note that the preferred lifetime of
2188 * the corresponding address is always
2189 * reset to the Preferred Lifetime in
2190 * the received Prefix Information
2191 * option, regardless of whether the
2192 * valid lifetime is also reset or
2195 * So if the preferred lifetime in
2196 * this advertisement is different
2197 * than what we have stored, but the
2198 * valid lifetime is invalid, just
2199 * reset prefered_lft.
2201 * We must set the valid lifetime
2202 * to the stored lifetime since we'll
2203 * be updating the timestamp below,
2204 * else we'll set it back to the
2207 if (prefered_lft
!= ifp
->prefered_lft
) {
2208 valid_lft
= stored_lft
;
2212 valid_lft
= MIN_VALID_LIFETIME
;
2213 if (valid_lft
< prefered_lft
)
2214 prefered_lft
= valid_lft
;
2220 ifp
->valid_lft
= valid_lft
;
2221 ifp
->prefered_lft
= prefered_lft
;
2224 ifp
->flags
&= ~IFA_F_DEPRECATED
;
2225 spin_unlock(&ifp
->lock
);
2227 if (!(flags
&IFA_F_TENTATIVE
))
2228 ipv6_ifa_notify(0, ifp
);
2230 spin_unlock(&ifp
->lock
);
2232 #ifdef CONFIG_IPV6_PRIVACY
2233 read_lock_bh(&in6_dev
->lock
);
2234 /* update all temporary addresses in the list */
2235 list_for_each_entry(ift
, &in6_dev
->tempaddr_list
,
2237 int age
, max_valid
, max_prefered
;
2239 if (ifp
!= ift
->ifpub
)
2243 * RFC 4941 section 3.3:
2244 * If a received option will extend the lifetime
2245 * of a public address, the lifetimes of
2246 * temporary addresses should be extended,
2247 * subject to the overall constraint that no
2248 * temporary addresses should ever remain
2249 * "valid" or "preferred" for a time longer than
2250 * (TEMP_VALID_LIFETIME) or
2251 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
2254 age
= (now
- ift
->cstamp
) / HZ
;
2255 max_valid
= in6_dev
->cnf
.temp_valid_lft
- age
;
2259 max_prefered
= in6_dev
->cnf
.temp_prefered_lft
-
2260 in6_dev
->cnf
.max_desync_factor
-
2262 if (max_prefered
< 0)
2265 if (valid_lft
> max_valid
)
2266 valid_lft
= max_valid
;
2268 if (prefered_lft
> max_prefered
)
2269 prefered_lft
= max_prefered
;
2271 spin_lock(&ift
->lock
);
2273 ift
->valid_lft
= valid_lft
;
2274 ift
->prefered_lft
= prefered_lft
;
2276 if (prefered_lft
> 0)
2277 ift
->flags
&= ~IFA_F_DEPRECATED
;
2279 spin_unlock(&ift
->lock
);
2280 if (!(flags
&IFA_F_TENTATIVE
))
2281 ipv6_ifa_notify(0, ift
);
2284 if ((create
|| list_empty(&in6_dev
->tempaddr_list
)) && in6_dev
->cnf
.use_tempaddr
> 0) {
2286 * When a new public address is created as
2287 * described in [ADDRCONF], also create a new
2288 * temporary address. Also create a temporary
2289 * address if it's enabled but no temporary
2290 * address currently exists.
2292 read_unlock_bh(&in6_dev
->lock
);
2293 ipv6_create_tempaddr(ifp
, NULL
);
2295 read_unlock_bh(&in6_dev
->lock
);
2302 inet6_prefix_notify(RTM_NEWPREFIX
, in6_dev
, pinfo
);
2303 in6_dev_put(in6_dev
);
2307 * Set destination address.
2308 * Special case for SIT interfaces where we create a new "virtual"
2311 int addrconf_set_dstaddr(struct net
*net
, void __user
*arg
)
2313 struct in6_ifreq ireq
;
2314 struct net_device
*dev
;
2320 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2323 dev
= __dev_get_by_index(net
, ireq
.ifr6_ifindex
);
2329 #if IS_ENABLED(CONFIG_IPV6_SIT)
2330 if (dev
->type
== ARPHRD_SIT
) {
2331 const struct net_device_ops
*ops
= dev
->netdev_ops
;
2333 struct ip_tunnel_parm p
;
2335 err
= -EADDRNOTAVAIL
;
2336 if (!(ipv6_addr_type(&ireq
.ifr6_addr
) & IPV6_ADDR_COMPATv4
))
2339 memset(&p
, 0, sizeof(p
));
2340 p
.iph
.daddr
= ireq
.ifr6_addr
.s6_addr32
[3];
2344 p
.iph
.protocol
= IPPROTO_IPV6
;
2346 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
2348 if (ops
->ndo_do_ioctl
) {
2349 mm_segment_t oldfs
= get_fs();
2352 err
= ops
->ndo_do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
2359 dev
= __dev_get_by_name(net
, p
.name
);
2362 err
= dev_open(dev
);
2373 * Manual configuration of address on an interface
2375 static int inet6_addr_add(struct net
*net
, int ifindex
, const struct in6_addr
*pfx
,
2376 unsigned int plen
, __u8 ifa_flags
, __u32 prefered_lft
,
2379 struct inet6_ifaddr
*ifp
;
2380 struct inet6_dev
*idev
;
2381 struct net_device
*dev
;
2385 unsigned long timeout
;
2392 /* check the lifetime */
2393 if (!valid_lft
|| prefered_lft
> valid_lft
)
2396 dev
= __dev_get_by_index(net
, ifindex
);
2400 idev
= addrconf_add_dev(dev
);
2402 return PTR_ERR(idev
);
2404 scope
= ipv6_addr_scope(pfx
);
2406 timeout
= addrconf_timeout_fixup(valid_lft
, HZ
);
2407 if (addrconf_finite_timeout(timeout
)) {
2408 expires
= jiffies_to_clock_t(timeout
* HZ
);
2409 valid_lft
= timeout
;
2410 flags
= RTF_EXPIRES
;
2414 ifa_flags
|= IFA_F_PERMANENT
;
2417 timeout
= addrconf_timeout_fixup(prefered_lft
, HZ
);
2418 if (addrconf_finite_timeout(timeout
)) {
2420 ifa_flags
|= IFA_F_DEPRECATED
;
2421 prefered_lft
= timeout
;
2424 ifp
= ipv6_add_addr(idev
, pfx
, plen
, scope
, ifa_flags
);
2427 spin_lock_bh(&ifp
->lock
);
2428 ifp
->valid_lft
= valid_lft
;
2429 ifp
->prefered_lft
= prefered_lft
;
2430 ifp
->tstamp
= jiffies
;
2431 spin_unlock_bh(&ifp
->lock
);
2433 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, dev
,
2436 * Note that section 3.1 of RFC 4429 indicates
2437 * that the Optimistic flag should not be set for
2438 * manually configured addresses
2440 addrconf_dad_start(ifp
);
2446 return PTR_ERR(ifp
);
2449 static int inet6_addr_del(struct net
*net
, int ifindex
, const struct in6_addr
*pfx
,
2452 struct inet6_ifaddr
*ifp
;
2453 struct inet6_dev
*idev
;
2454 struct net_device
*dev
;
2459 dev
= __dev_get_by_index(net
, ifindex
);
2463 if ((idev
= __in6_dev_get(dev
)) == NULL
)
2466 read_lock_bh(&idev
->lock
);
2467 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
2468 if (ifp
->prefix_len
== plen
&&
2469 ipv6_addr_equal(pfx
, &ifp
->addr
)) {
2471 read_unlock_bh(&idev
->lock
);
2475 /* If the last address is deleted administratively,
2476 disable IPv6 on this interface.
2478 if (list_empty(&idev
->addr_list
))
2479 addrconf_ifdown(idev
->dev
, 1);
2483 read_unlock_bh(&idev
->lock
);
2484 return -EADDRNOTAVAIL
;
2488 int addrconf_add_ifaddr(struct net
*net
, void __user
*arg
)
2490 struct in6_ifreq ireq
;
2493 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2496 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2500 err
= inet6_addr_add(net
, ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
,
2501 ireq
.ifr6_prefixlen
, IFA_F_PERMANENT
,
2502 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
);
2507 int addrconf_del_ifaddr(struct net
*net
, void __user
*arg
)
2509 struct in6_ifreq ireq
;
2512 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2515 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2519 err
= inet6_addr_del(net
, ireq
.ifr6_ifindex
, &ireq
.ifr6_addr
,
2520 ireq
.ifr6_prefixlen
);
2525 static void add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
,
2526 int plen
, int scope
)
2528 struct inet6_ifaddr
*ifp
;
2530 ifp
= ipv6_add_addr(idev
, addr
, plen
, scope
, IFA_F_PERMANENT
);
2532 spin_lock_bh(&ifp
->lock
);
2533 ifp
->flags
&= ~IFA_F_TENTATIVE
;
2534 spin_unlock_bh(&ifp
->lock
);
2535 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
2540 #if IS_ENABLED(CONFIG_IPV6_SIT)
2541 static void sit_add_v4_addrs(struct inet6_dev
*idev
)
2543 struct in6_addr addr
;
2544 struct net_device
*dev
;
2545 struct net
*net
= dev_net(idev
->dev
);
2550 memset(&addr
, 0, sizeof(struct in6_addr
));
2551 memcpy(&addr
.s6_addr32
[3], idev
->dev
->dev_addr
, 4);
2553 if (idev
->dev
->flags
&IFF_POINTOPOINT
) {
2554 addr
.s6_addr32
[0] = htonl(0xfe800000);
2557 scope
= IPV6_ADDR_COMPATv4
;
2560 if (addr
.s6_addr32
[3]) {
2561 add_addr(idev
, &addr
, 128, scope
);
2565 for_each_netdev(net
, dev
) {
2566 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
2567 if (in_dev
&& (dev
->flags
& IFF_UP
)) {
2568 struct in_ifaddr
*ifa
;
2572 for (ifa
= in_dev
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
2575 addr
.s6_addr32
[3] = ifa
->ifa_local
;
2577 if (ifa
->ifa_scope
== RT_SCOPE_LINK
)
2579 if (ifa
->ifa_scope
>= RT_SCOPE_HOST
) {
2580 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2584 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
2589 add_addr(idev
, &addr
, plen
, flag
);
2596 static void init_loopback(struct net_device
*dev
)
2598 struct inet6_dev
*idev
;
2604 if ((idev
= ipv6_find_idev(dev
)) == NULL
) {
2605 pr_debug("%s: add_dev failed\n", __func__
);
2609 add_addr(idev
, &in6addr_loopback
, 128, IFA_HOST
);
2612 static void addrconf_add_linklocal(struct inet6_dev
*idev
, const struct in6_addr
*addr
)
2614 struct inet6_ifaddr
*ifp
;
2615 u32 addr_flags
= IFA_F_PERMANENT
;
2617 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2618 if (idev
->cnf
.optimistic_dad
&&
2619 !dev_net(idev
->dev
)->ipv6
.devconf_all
->forwarding
)
2620 addr_flags
|= IFA_F_OPTIMISTIC
;
2624 ifp
= ipv6_add_addr(idev
, addr
, 64, IFA_LINK
, addr_flags
);
2626 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, idev
->dev
, 0, 0);
2627 addrconf_dad_start(ifp
);
2632 static void addrconf_dev_config(struct net_device
*dev
)
2634 struct in6_addr addr
;
2635 struct inet6_dev
*idev
;
2639 if ((dev
->type
!= ARPHRD_ETHER
) &&
2640 (dev
->type
!= ARPHRD_FDDI
) &&
2641 (dev
->type
!= ARPHRD_ARCNET
) &&
2642 (dev
->type
!= ARPHRD_INFINIBAND
) &&
2643 (dev
->type
!= ARPHRD_IEEE802154
)) {
2644 /* Alas, we support only Ethernet autoconfiguration. */
2648 idev
= addrconf_add_dev(dev
);
2652 memset(&addr
, 0, sizeof(struct in6_addr
));
2653 addr
.s6_addr32
[0] = htonl(0xFE800000);
2655 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) == 0)
2656 addrconf_add_linklocal(idev
, &addr
);
2659 #if IS_ENABLED(CONFIG_IPV6_SIT)
2660 static void addrconf_sit_config(struct net_device
*dev
)
2662 struct inet6_dev
*idev
;
2667 * Configure the tunnel with one of our IPv4
2668 * addresses... we should configure all of
2669 * our v4 addrs in the tunnel
2672 if ((idev
= ipv6_find_idev(dev
)) == NULL
) {
2673 pr_debug("%s: add_dev failed\n", __func__
);
2677 if (dev
->priv_flags
& IFF_ISATAP
) {
2678 struct in6_addr addr
;
2680 ipv6_addr_set(&addr
, htonl(0xFE800000), 0, 0, 0);
2681 addrconf_prefix_route(&addr
, 64, dev
, 0, 0);
2682 if (!ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
))
2683 addrconf_add_linklocal(idev
, &addr
);
2687 sit_add_v4_addrs(idev
);
2689 if (dev
->flags
&IFF_POINTOPOINT
)
2690 addrconf_add_mroute(dev
);
2696 #if IS_ENABLED(CONFIG_NET_IPGRE)
2697 static void addrconf_gre_config(struct net_device
*dev
)
2699 struct inet6_dev
*idev
;
2700 struct in6_addr addr
;
2702 pr_info("%s(%s)\n", __func__
, dev
->name
);
2706 if ((idev
= ipv6_find_idev(dev
)) == NULL
) {
2707 pr_debug("%s: add_dev failed\n", __func__
);
2711 ipv6_addr_set(&addr
, htonl(0xFE800000), 0, 0, 0);
2712 addrconf_prefix_route(&addr
, 64, dev
, 0, 0);
2714 if (!ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
))
2715 addrconf_add_linklocal(idev
, &addr
);
2720 ipv6_inherit_linklocal(struct inet6_dev
*idev
, struct net_device
*link_dev
)
2722 struct in6_addr lladdr
;
2724 if (!ipv6_get_lladdr(link_dev
, &lladdr
, IFA_F_TENTATIVE
)) {
2725 addrconf_add_linklocal(idev
, &lladdr
);
2731 static void ip6_tnl_add_linklocal(struct inet6_dev
*idev
)
2733 struct net_device
*link_dev
;
2734 struct net
*net
= dev_net(idev
->dev
);
2736 /* first try to inherit the link-local address from the link device */
2737 if (idev
->dev
->iflink
&&
2738 (link_dev
= __dev_get_by_index(net
, idev
->dev
->iflink
))) {
2739 if (!ipv6_inherit_linklocal(idev
, link_dev
))
2742 /* then try to inherit it from any device */
2743 for_each_netdev(net
, link_dev
) {
2744 if (!ipv6_inherit_linklocal(idev
, link_dev
))
2747 pr_debug("init ip6-ip6: add_linklocal failed\n");
2751 * Autoconfigure tunnel with a link-local address so routing protocols,
2752 * DHCPv6, MLD etc. can be run over the virtual link
2755 static void addrconf_ip6_tnl_config(struct net_device
*dev
)
2757 struct inet6_dev
*idev
;
2761 idev
= addrconf_add_dev(dev
);
2763 pr_debug("init ip6-ip6: add_dev failed\n");
2766 ip6_tnl_add_linklocal(idev
);
2769 static int addrconf_notify(struct notifier_block
*this, unsigned long event
,
2772 struct net_device
*dev
= (struct net_device
*) data
;
2773 struct inet6_dev
*idev
= __in6_dev_get(dev
);
2774 int run_pending
= 0;
2778 case NETDEV_REGISTER
:
2779 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2780 idev
= ipv6_add_dev(dev
);
2782 return notifier_from_errno(-ENOMEM
);
2788 if (dev
->flags
& IFF_SLAVE
)
2791 if (event
== NETDEV_UP
) {
2792 if (!addrconf_qdisc_ok(dev
)) {
2793 /* device is not ready yet. */
2794 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
2799 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
)
2800 idev
= ipv6_add_dev(dev
);
2803 idev
->if_flags
|= IF_READY
;
2807 if (!addrconf_qdisc_ok(dev
)) {
2808 /* device is still not ready. */
2813 if (idev
->if_flags
& IF_READY
)
2814 /* device is already configured. */
2816 idev
->if_flags
|= IF_READY
;
2819 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
2825 switch (dev
->type
) {
2826 #if IS_ENABLED(CONFIG_IPV6_SIT)
2828 addrconf_sit_config(dev
);
2831 #if IS_ENABLED(CONFIG_NET_IPGRE)
2833 addrconf_gre_config(dev
);
2836 case ARPHRD_TUNNEL6
:
2837 addrconf_ip6_tnl_config(dev
);
2839 case ARPHRD_LOOPBACK
:
2844 addrconf_dev_config(dev
);
2850 addrconf_dad_run(idev
);
2853 * If the MTU changed during the interface down,
2854 * when the interface up, the changed MTU must be
2855 * reflected in the idev as well as routers.
2857 if (idev
->cnf
.mtu6
!= dev
->mtu
&&
2858 dev
->mtu
>= IPV6_MIN_MTU
) {
2859 rt6_mtu_change(dev
, dev
->mtu
);
2860 idev
->cnf
.mtu6
= dev
->mtu
;
2862 idev
->tstamp
= jiffies
;
2863 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
2866 * If the changed mtu during down is lower than
2867 * IPV6_MIN_MTU stop IPv6 on this interface.
2869 if (dev
->mtu
< IPV6_MIN_MTU
)
2870 addrconf_ifdown(dev
, 1);
2874 case NETDEV_CHANGEMTU
:
2875 if (idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2876 rt6_mtu_change(dev
, dev
->mtu
);
2877 idev
->cnf
.mtu6
= dev
->mtu
;
2881 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
2882 idev
= ipv6_add_dev(dev
);
2888 * MTU falled under IPV6_MIN_MTU.
2889 * Stop IPv6 on this interface.
2893 case NETDEV_UNREGISTER
:
2895 * Remove all addresses from this interface.
2897 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
2900 case NETDEV_CHANGENAME
:
2902 snmp6_unregister_dev(idev
);
2903 addrconf_sysctl_unregister(idev
);
2904 addrconf_sysctl_register(idev
);
2905 err
= snmp6_register_dev(idev
);
2907 return notifier_from_errno(err
);
2911 case NETDEV_PRE_TYPE_CHANGE
:
2912 case NETDEV_POST_TYPE_CHANGE
:
2913 addrconf_type_change(dev
, event
);
2921 * addrconf module should be notified of a device going up
2923 static struct notifier_block ipv6_dev_notf
= {
2924 .notifier_call
= addrconf_notify
,
2927 static void addrconf_type_change(struct net_device
*dev
, unsigned long event
)
2929 struct inet6_dev
*idev
;
2932 idev
= __in6_dev_get(dev
);
2934 if (event
== NETDEV_POST_TYPE_CHANGE
)
2935 ipv6_mc_remap(idev
);
2936 else if (event
== NETDEV_PRE_TYPE_CHANGE
)
2937 ipv6_mc_unmap(idev
);
2940 static int addrconf_ifdown(struct net_device
*dev
, int how
)
2942 struct net
*net
= dev_net(dev
);
2943 struct inet6_dev
*idev
;
2944 struct inet6_ifaddr
*ifa
;
2949 rt6_ifdown(net
, dev
);
2950 neigh_ifdown(&nd_tbl
, dev
);
2952 idev
= __in6_dev_get(dev
);
2957 * Step 1: remove reference to ipv6 device from parent device.
2963 /* protected by rtnl_lock */
2964 RCU_INIT_POINTER(dev
->ip6_ptr
, NULL
);
2966 /* Step 1.5: remove snmp6 entry */
2967 snmp6_unregister_dev(idev
);
2971 /* Step 2: clear hash table */
2972 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
2973 struct hlist_head
*h
= &inet6_addr_lst
[i
];
2975 spin_lock_bh(&addrconf_hash_lock
);
2977 hlist_for_each_entry_rcu(ifa
, h
, addr_lst
) {
2978 if (ifa
->idev
== idev
) {
2979 hlist_del_init_rcu(&ifa
->addr_lst
);
2980 addrconf_del_timer(ifa
);
2984 spin_unlock_bh(&addrconf_hash_lock
);
2987 write_lock_bh(&idev
->lock
);
2989 /* Step 2: clear flags for stateless addrconf */
2991 idev
->if_flags
&= ~(IF_RS_SENT
|IF_RA_RCVD
|IF_READY
);
2993 #ifdef CONFIG_IPV6_PRIVACY
2994 if (how
&& del_timer(&idev
->regen_timer
))
2997 /* Step 3: clear tempaddr list */
2998 while (!list_empty(&idev
->tempaddr_list
)) {
2999 ifa
= list_first_entry(&idev
->tempaddr_list
,
3000 struct inet6_ifaddr
, tmp_list
);
3001 list_del(&ifa
->tmp_list
);
3002 write_unlock_bh(&idev
->lock
);
3003 spin_lock_bh(&ifa
->lock
);
3006 in6_ifa_put(ifa
->ifpub
);
3009 spin_unlock_bh(&ifa
->lock
);
3011 write_lock_bh(&idev
->lock
);
3015 while (!list_empty(&idev
->addr_list
)) {
3016 ifa
= list_first_entry(&idev
->addr_list
,
3017 struct inet6_ifaddr
, if_list
);
3018 addrconf_del_timer(ifa
);
3020 list_del(&ifa
->if_list
);
3022 write_unlock_bh(&idev
->lock
);
3024 spin_lock_bh(&ifa
->state_lock
);
3026 ifa
->state
= INET6_IFADDR_STATE_DEAD
;
3027 spin_unlock_bh(&ifa
->state_lock
);
3029 if (state
!= INET6_IFADDR_STATE_DEAD
) {
3030 __ipv6_ifa_notify(RTM_DELADDR
, ifa
);
3031 atomic_notifier_call_chain(&inet6addr_chain
, NETDEV_DOWN
, ifa
);
3035 write_lock_bh(&idev
->lock
);
3038 write_unlock_bh(&idev
->lock
);
3040 /* Step 5: Discard multicast list */
3042 ipv6_mc_destroy_dev(idev
);
3046 idev
->tstamp
= jiffies
;
3048 /* Last: Shot the device (if unregistered) */
3050 addrconf_sysctl_unregister(idev
);
3051 neigh_parms_release(&nd_tbl
, idev
->nd_parms
);
3052 neigh_ifdown(&nd_tbl
, dev
);
3058 static void addrconf_rs_timer(unsigned long data
)
3060 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*) data
;
3061 struct inet6_dev
*idev
= ifp
->idev
;
3063 read_lock(&idev
->lock
);
3064 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
))
3067 if (!ipv6_accept_ra(idev
))
3070 /* Announcement received after solicitation was sent */
3071 if (idev
->if_flags
& IF_RA_RCVD
)
3074 spin_lock(&ifp
->lock
);
3075 if (ifp
->probes
++ < idev
->cnf
.rtr_solicits
) {
3076 /* The wait after the last probe can be shorter */
3077 addrconf_mod_timer(ifp
, AC_RS
,
3078 (ifp
->probes
== idev
->cnf
.rtr_solicits
) ?
3079 idev
->cnf
.rtr_solicit_delay
:
3080 idev
->cnf
.rtr_solicit_interval
);
3081 spin_unlock(&ifp
->lock
);
3083 ndisc_send_rs(idev
->dev
, &ifp
->addr
, &in6addr_linklocal_allrouters
);
3085 spin_unlock(&ifp
->lock
);
3087 * Note: we do not support deprecated "all on-link"
3088 * assumption any longer.
3090 pr_debug("%s: no IPv6 routers present\n", idev
->dev
->name
);
3094 read_unlock(&idev
->lock
);
3099 * Duplicate Address Detection
3101 static void addrconf_dad_kick(struct inet6_ifaddr
*ifp
)
3103 unsigned long rand_num
;
3104 struct inet6_dev
*idev
= ifp
->idev
;
3106 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
3109 rand_num
= net_random() % (idev
->cnf
.rtr_solicit_delay
? : 1);
3111 ifp
->probes
= idev
->cnf
.dad_transmits
;
3112 addrconf_mod_timer(ifp
, AC_DAD
, rand_num
);
3115 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
)
3117 struct inet6_dev
*idev
= ifp
->idev
;
3118 struct net_device
*dev
= idev
->dev
;
3120 addrconf_join_solict(dev
, &ifp
->addr
);
3122 net_srandom(ifp
->addr
.s6_addr32
[3]);
3124 read_lock_bh(&idev
->lock
);
3125 spin_lock(&ifp
->lock
);
3126 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
)
3129 if (dev
->flags
&(IFF_NOARP
|IFF_LOOPBACK
) ||
3130 idev
->cnf
.accept_dad
< 1 ||
3131 !(ifp
->flags
&IFA_F_TENTATIVE
) ||
3132 ifp
->flags
& IFA_F_NODAD
) {
3133 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
3134 spin_unlock(&ifp
->lock
);
3135 read_unlock_bh(&idev
->lock
);
3137 addrconf_dad_completed(ifp
);
3141 if (!(idev
->if_flags
& IF_READY
)) {
3142 spin_unlock(&ifp
->lock
);
3143 read_unlock_bh(&idev
->lock
);
3145 * If the device is not ready:
3146 * - keep it tentative if it is a permanent address.
3147 * - otherwise, kill it.
3150 addrconf_dad_stop(ifp
, 0);
3155 * Optimistic nodes can start receiving
3158 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
3159 ip6_ins_rt(ifp
->rt
);
3161 addrconf_dad_kick(ifp
);
3163 spin_unlock(&ifp
->lock
);
3164 read_unlock_bh(&idev
->lock
);
3167 static void addrconf_dad_timer(unsigned long data
)
3169 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*) data
;
3170 struct inet6_dev
*idev
= ifp
->idev
;
3171 struct in6_addr mcaddr
;
3173 if (!ifp
->probes
&& addrconf_dad_end(ifp
))
3176 read_lock(&idev
->lock
);
3177 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
)) {
3178 read_unlock(&idev
->lock
);
3182 spin_lock(&ifp
->lock
);
3183 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
) {
3184 spin_unlock(&ifp
->lock
);
3185 read_unlock(&idev
->lock
);
3189 if (ifp
->probes
== 0) {
3191 * DAD was successful
3194 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
3195 spin_unlock(&ifp
->lock
);
3196 read_unlock(&idev
->lock
);
3198 addrconf_dad_completed(ifp
);
3204 addrconf_mod_timer(ifp
, AC_DAD
, ifp
->idev
->nd_parms
->retrans_time
);
3205 spin_unlock(&ifp
->lock
);
3206 read_unlock(&idev
->lock
);
3208 /* send a neighbour solicitation for our addr */
3209 addrconf_addr_solict_mult(&ifp
->addr
, &mcaddr
);
3210 ndisc_send_ns(ifp
->idev
->dev
, NULL
, &ifp
->addr
, &mcaddr
, &in6addr_any
);
3215 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
)
3217 struct net_device
*dev
= ifp
->idev
->dev
;
3220 * Configure the address for reception. Now it is valid.
3223 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
3225 /* If added prefix is link local and we are prepared to process
3226 router advertisements, start sending router solicitations.
3229 if (ipv6_accept_ra(ifp
->idev
) &&
3230 ifp
->idev
->cnf
.rtr_solicits
> 0 &&
3231 (dev
->flags
&IFF_LOOPBACK
) == 0 &&
3232 (ipv6_addr_type(&ifp
->addr
) & IPV6_ADDR_LINKLOCAL
)) {
3234 * If a host as already performed a random delay
3235 * [...] as part of DAD [...] there is no need
3236 * to delay again before sending the first RS
3238 ndisc_send_rs(ifp
->idev
->dev
, &ifp
->addr
, &in6addr_linklocal_allrouters
);
3240 spin_lock_bh(&ifp
->lock
);
3242 ifp
->idev
->if_flags
|= IF_RS_SENT
;
3243 addrconf_mod_timer(ifp
, AC_RS
, ifp
->idev
->cnf
.rtr_solicit_interval
);
3244 spin_unlock_bh(&ifp
->lock
);
3248 static void addrconf_dad_run(struct inet6_dev
*idev
)
3250 struct inet6_ifaddr
*ifp
;
3252 read_lock_bh(&idev
->lock
);
3253 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
3254 spin_lock(&ifp
->lock
);
3255 if (ifp
->flags
& IFA_F_TENTATIVE
&&
3256 ifp
->state
== INET6_IFADDR_STATE_DAD
)
3257 addrconf_dad_kick(ifp
);
3258 spin_unlock(&ifp
->lock
);
3260 read_unlock_bh(&idev
->lock
);
3263 #ifdef CONFIG_PROC_FS
3264 struct if6_iter_state
{
3265 struct seq_net_private p
;
3270 static struct inet6_ifaddr
*if6_get_first(struct seq_file
*seq
, loff_t pos
)
3272 struct inet6_ifaddr
*ifa
= NULL
;
3273 struct if6_iter_state
*state
= seq
->private;
3274 struct net
*net
= seq_file_net(seq
);
3277 /* initial bucket if pos is 0 */
3283 for (; state
->bucket
< IN6_ADDR_HSIZE
; ++state
->bucket
) {
3284 hlist_for_each_entry_rcu_bh(ifa
, &inet6_addr_lst
[state
->bucket
],
3286 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
3288 /* sync with offset */
3289 if (p
< state
->offset
) {
3297 /* prepare for next bucket */
3304 static struct inet6_ifaddr
*if6_get_next(struct seq_file
*seq
,
3305 struct inet6_ifaddr
*ifa
)
3307 struct if6_iter_state
*state
= seq
->private;
3308 struct net
*net
= seq_file_net(seq
);
3310 hlist_for_each_entry_continue_rcu_bh(ifa
, addr_lst
) {
3311 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
3317 while (++state
->bucket
< IN6_ADDR_HSIZE
) {
3319 hlist_for_each_entry_rcu_bh(ifa
,
3320 &inet6_addr_lst
[state
->bucket
], addr_lst
) {
3321 if (!net_eq(dev_net(ifa
->idev
->dev
), net
))
3331 static void *if6_seq_start(struct seq_file
*seq
, loff_t
*pos
)
3335 return if6_get_first(seq
, *pos
);
3338 static void *if6_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
3340 struct inet6_ifaddr
*ifa
;
3342 ifa
= if6_get_next(seq
, v
);
3347 static void if6_seq_stop(struct seq_file
*seq
, void *v
)
3350 rcu_read_unlock_bh();
3353 static int if6_seq_show(struct seq_file
*seq
, void *v
)
3355 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*)v
;
3356 seq_printf(seq
, "%pi6 %02x %02x %02x %02x %8s\n",
3358 ifp
->idev
->dev
->ifindex
,
3362 ifp
->idev
->dev
->name
);
3366 static const struct seq_operations if6_seq_ops
= {
3367 .start
= if6_seq_start
,
3368 .next
= if6_seq_next
,
3369 .show
= if6_seq_show
,
3370 .stop
= if6_seq_stop
,
3373 static int if6_seq_open(struct inode
*inode
, struct file
*file
)
3375 return seq_open_net(inode
, file
, &if6_seq_ops
,
3376 sizeof(struct if6_iter_state
));
3379 static const struct file_operations if6_fops
= {
3380 .owner
= THIS_MODULE
,
3381 .open
= if6_seq_open
,
3383 .llseek
= seq_lseek
,
3384 .release
= seq_release_net
,
3387 static int __net_init
if6_proc_net_init(struct net
*net
)
3389 if (!proc_create("if_inet6", S_IRUGO
, net
->proc_net
, &if6_fops
))
3394 static void __net_exit
if6_proc_net_exit(struct net
*net
)
3396 remove_proc_entry("if_inet6", net
->proc_net
);
3399 static struct pernet_operations if6_proc_net_ops
= {
3400 .init
= if6_proc_net_init
,
3401 .exit
= if6_proc_net_exit
,
3404 int __init
if6_proc_init(void)
3406 return register_pernet_subsys(&if6_proc_net_ops
);
3409 void if6_proc_exit(void)
3411 unregister_pernet_subsys(&if6_proc_net_ops
);
3413 #endif /* CONFIG_PROC_FS */
3415 #if IS_ENABLED(CONFIG_IPV6_MIP6)
3416 /* Check if address is a home address configured on any interface. */
3417 int ipv6_chk_home_addr(struct net
*net
, const struct in6_addr
*addr
)
3420 struct inet6_ifaddr
*ifp
= NULL
;
3421 unsigned int hash
= inet6_addr_hash(addr
);
3424 hlist_for_each_entry_rcu_bh(ifp
, &inet6_addr_lst
[hash
], addr_lst
) {
3425 if (!net_eq(dev_net(ifp
->idev
->dev
), net
))
3427 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
3428 (ifp
->flags
& IFA_F_HOMEADDRESS
)) {
3433 rcu_read_unlock_bh();
3439 * Periodic address status verification
3442 static void addrconf_verify(unsigned long foo
)
3444 unsigned long now
, next
, next_sec
, next_sched
;
3445 struct inet6_ifaddr
*ifp
;
3449 spin_lock(&addrconf_verify_lock
);
3451 next
= round_jiffies_up(now
+ ADDR_CHECK_FREQUENCY
);
3453 del_timer(&addr_chk_timer
);
3455 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
3457 hlist_for_each_entry_rcu_bh(ifp
,
3458 &inet6_addr_lst
[i
], addr_lst
) {
3461 if (ifp
->flags
& IFA_F_PERMANENT
)
3464 spin_lock(&ifp
->lock
);
3465 /* We try to batch several events at once. */
3466 age
= (now
- ifp
->tstamp
+ ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
3468 if (ifp
->valid_lft
!= INFINITY_LIFE_TIME
&&
3469 age
>= ifp
->valid_lft
) {
3470 spin_unlock(&ifp
->lock
);
3474 } else if (ifp
->prefered_lft
== INFINITY_LIFE_TIME
) {
3475 spin_unlock(&ifp
->lock
);
3477 } else if (age
>= ifp
->prefered_lft
) {
3478 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
3481 if (!(ifp
->flags
&IFA_F_DEPRECATED
)) {
3483 ifp
->flags
|= IFA_F_DEPRECATED
;
3486 if (time_before(ifp
->tstamp
+ ifp
->valid_lft
* HZ
, next
))
3487 next
= ifp
->tstamp
+ ifp
->valid_lft
* HZ
;
3489 spin_unlock(&ifp
->lock
);
3494 ipv6_ifa_notify(0, ifp
);
3498 #ifdef CONFIG_IPV6_PRIVACY
3499 } else if ((ifp
->flags
&IFA_F_TEMPORARY
) &&
3500 !(ifp
->flags
&IFA_F_TENTATIVE
)) {
3501 unsigned long regen_advance
= ifp
->idev
->cnf
.regen_max_retry
*
3502 ifp
->idev
->cnf
.dad_transmits
*
3503 ifp
->idev
->nd_parms
->retrans_time
/ HZ
;
3505 if (age
>= ifp
->prefered_lft
- regen_advance
) {
3506 struct inet6_ifaddr
*ifpub
= ifp
->ifpub
;
3507 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
3508 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
3509 if (!ifp
->regen_count
&& ifpub
) {
3512 in6_ifa_hold(ifpub
);
3513 spin_unlock(&ifp
->lock
);
3515 spin_lock(&ifpub
->lock
);
3516 ifpub
->regen_count
= 0;
3517 spin_unlock(&ifpub
->lock
);
3518 ipv6_create_tempaddr(ifpub
, ifp
);
3523 } else if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
, next
))
3524 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
;
3525 spin_unlock(&ifp
->lock
);
3528 /* ifp->prefered_lft <= ifp->valid_lft */
3529 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
3530 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
3531 spin_unlock(&ifp
->lock
);
3536 next_sec
= round_jiffies_up(next
);
3539 /* If rounded timeout is accurate enough, accept it. */
3540 if (time_before(next_sec
, next
+ ADDRCONF_TIMER_FUZZ
))
3541 next_sched
= next_sec
;
3543 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3544 if (time_before(next_sched
, jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
))
3545 next_sched
= jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
;
3547 ADBG((KERN_DEBUG
"now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3548 now
, next
, next_sec
, next_sched
));
3550 addr_chk_timer
.expires
= next_sched
;
3551 add_timer(&addr_chk_timer
);
3552 spin_unlock(&addrconf_verify_lock
);
3553 rcu_read_unlock_bh();
3556 static struct in6_addr
*extract_addr(struct nlattr
*addr
, struct nlattr
*local
)
3558 struct in6_addr
*pfx
= NULL
;
3561 pfx
= nla_data(addr
);
3564 if (pfx
&& nla_memcmp(local
, pfx
, sizeof(*pfx
)))
3567 pfx
= nla_data(local
);
3573 static const struct nla_policy ifa_ipv6_policy
[IFA_MAX
+1] = {
3574 [IFA_ADDRESS
] = { .len
= sizeof(struct in6_addr
) },
3575 [IFA_LOCAL
] = { .len
= sizeof(struct in6_addr
) },
3576 [IFA_CACHEINFO
] = { .len
= sizeof(struct ifa_cacheinfo
) },
3580 inet6_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3582 struct net
*net
= sock_net(skb
->sk
);
3583 struct ifaddrmsg
*ifm
;
3584 struct nlattr
*tb
[IFA_MAX
+1];
3585 struct in6_addr
*pfx
;
3588 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
3592 ifm
= nlmsg_data(nlh
);
3593 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
]);
3597 return inet6_addr_del(net
, ifm
->ifa_index
, pfx
, ifm
->ifa_prefixlen
);
3600 static int inet6_addr_modify(struct inet6_ifaddr
*ifp
, u8 ifa_flags
,
3601 u32 prefered_lft
, u32 valid_lft
)
3605 unsigned long timeout
;
3607 if (!valid_lft
|| (prefered_lft
> valid_lft
))
3610 timeout
= addrconf_timeout_fixup(valid_lft
, HZ
);
3611 if (addrconf_finite_timeout(timeout
)) {
3612 expires
= jiffies_to_clock_t(timeout
* HZ
);
3613 valid_lft
= timeout
;
3614 flags
= RTF_EXPIRES
;
3618 ifa_flags
|= IFA_F_PERMANENT
;
3621 timeout
= addrconf_timeout_fixup(prefered_lft
, HZ
);
3622 if (addrconf_finite_timeout(timeout
)) {
3624 ifa_flags
|= IFA_F_DEPRECATED
;
3625 prefered_lft
= timeout
;
3628 spin_lock_bh(&ifp
->lock
);
3629 ifp
->flags
= (ifp
->flags
& ~(IFA_F_DEPRECATED
| IFA_F_PERMANENT
| IFA_F_NODAD
| IFA_F_HOMEADDRESS
)) | ifa_flags
;
3630 ifp
->tstamp
= jiffies
;
3631 ifp
->valid_lft
= valid_lft
;
3632 ifp
->prefered_lft
= prefered_lft
;
3634 spin_unlock_bh(&ifp
->lock
);
3635 if (!(ifp
->flags
&IFA_F_TENTATIVE
))
3636 ipv6_ifa_notify(0, ifp
);
3638 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, ifp
->idev
->dev
,
3646 inet6_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
3648 struct net
*net
= sock_net(skb
->sk
);
3649 struct ifaddrmsg
*ifm
;
3650 struct nlattr
*tb
[IFA_MAX
+1];
3651 struct in6_addr
*pfx
;
3652 struct inet6_ifaddr
*ifa
;
3653 struct net_device
*dev
;
3654 u32 valid_lft
= INFINITY_LIFE_TIME
, preferred_lft
= INFINITY_LIFE_TIME
;
3658 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
3662 ifm
= nlmsg_data(nlh
);
3663 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
]);
3667 if (tb
[IFA_CACHEINFO
]) {
3668 struct ifa_cacheinfo
*ci
;
3670 ci
= nla_data(tb
[IFA_CACHEINFO
]);
3671 valid_lft
= ci
->ifa_valid
;
3672 preferred_lft
= ci
->ifa_prefered
;
3674 preferred_lft
= INFINITY_LIFE_TIME
;
3675 valid_lft
= INFINITY_LIFE_TIME
;
3678 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
3682 /* We ignore other flags so far. */
3683 ifa_flags
= ifm
->ifa_flags
& (IFA_F_NODAD
| IFA_F_HOMEADDRESS
);
3685 ifa
= ipv6_get_ifaddr(net
, pfx
, dev
, 1);
3688 * It would be best to check for !NLM_F_CREATE here but
3689 * userspace alreay relies on not having to provide this.
3691 return inet6_addr_add(net
, ifm
->ifa_index
, pfx
,
3692 ifm
->ifa_prefixlen
, ifa_flags
,
3693 preferred_lft
, valid_lft
);
3696 if (nlh
->nlmsg_flags
& NLM_F_EXCL
||
3697 !(nlh
->nlmsg_flags
& NLM_F_REPLACE
))
3700 err
= inet6_addr_modify(ifa
, ifa_flags
, preferred_lft
, valid_lft
);
3707 static void put_ifaddrmsg(struct nlmsghdr
*nlh
, u8 prefixlen
, u8 flags
,
3708 u8 scope
, int ifindex
)
3710 struct ifaddrmsg
*ifm
;
3712 ifm
= nlmsg_data(nlh
);
3713 ifm
->ifa_family
= AF_INET6
;
3714 ifm
->ifa_prefixlen
= prefixlen
;
3715 ifm
->ifa_flags
= flags
;
3716 ifm
->ifa_scope
= scope
;
3717 ifm
->ifa_index
= ifindex
;
3720 static int put_cacheinfo(struct sk_buff
*skb
, unsigned long cstamp
,
3721 unsigned long tstamp
, u32 preferred
, u32 valid
)
3723 struct ifa_cacheinfo ci
;
3725 ci
.cstamp
= cstamp_delta(cstamp
);
3726 ci
.tstamp
= cstamp_delta(tstamp
);
3727 ci
.ifa_prefered
= preferred
;
3728 ci
.ifa_valid
= valid
;
3730 return nla_put(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
3733 static inline int rt_scope(int ifa_scope
)
3735 if (ifa_scope
& IFA_HOST
)
3736 return RT_SCOPE_HOST
;
3737 else if (ifa_scope
& IFA_LINK
)
3738 return RT_SCOPE_LINK
;
3739 else if (ifa_scope
& IFA_SITE
)
3740 return RT_SCOPE_SITE
;
3742 return RT_SCOPE_UNIVERSE
;
3745 static inline int inet6_ifaddr_msgsize(void)
3747 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
3748 + nla_total_size(16) /* IFA_ADDRESS */
3749 + nla_total_size(sizeof(struct ifa_cacheinfo
));
3752 static int inet6_fill_ifaddr(struct sk_buff
*skb
, struct inet6_ifaddr
*ifa
,
3753 u32 portid
, u32 seq
, int event
, unsigned int flags
)
3755 struct nlmsghdr
*nlh
;
3756 u32 preferred
, valid
;
3758 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
3762 put_ifaddrmsg(nlh
, ifa
->prefix_len
, ifa
->flags
, rt_scope(ifa
->scope
),
3763 ifa
->idev
->dev
->ifindex
);
3765 if (!(ifa
->flags
&IFA_F_PERMANENT
)) {
3766 preferred
= ifa
->prefered_lft
;
3767 valid
= ifa
->valid_lft
;
3768 if (preferred
!= INFINITY_LIFE_TIME
) {
3769 long tval
= (jiffies
- ifa
->tstamp
)/HZ
;
3770 if (preferred
> tval
)
3774 if (valid
!= INFINITY_LIFE_TIME
) {
3782 preferred
= INFINITY_LIFE_TIME
;
3783 valid
= INFINITY_LIFE_TIME
;
3786 if (nla_put(skb
, IFA_ADDRESS
, 16, &ifa
->addr
) < 0 ||
3787 put_cacheinfo(skb
, ifa
->cstamp
, ifa
->tstamp
, preferred
, valid
) < 0) {
3788 nlmsg_cancel(skb
, nlh
);
3792 return nlmsg_end(skb
, nlh
);
3795 static int inet6_fill_ifmcaddr(struct sk_buff
*skb
, struct ifmcaddr6
*ifmca
,
3796 u32 portid
, u32 seq
, int event
, u16 flags
)
3798 struct nlmsghdr
*nlh
;
3799 u8 scope
= RT_SCOPE_UNIVERSE
;
3800 int ifindex
= ifmca
->idev
->dev
->ifindex
;
3802 if (ipv6_addr_scope(&ifmca
->mca_addr
) & IFA_SITE
)
3803 scope
= RT_SCOPE_SITE
;
3805 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
3809 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
3810 if (nla_put(skb
, IFA_MULTICAST
, 16, &ifmca
->mca_addr
) < 0 ||
3811 put_cacheinfo(skb
, ifmca
->mca_cstamp
, ifmca
->mca_tstamp
,
3812 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
3813 nlmsg_cancel(skb
, nlh
);
3817 return nlmsg_end(skb
, nlh
);
3820 static int inet6_fill_ifacaddr(struct sk_buff
*skb
, struct ifacaddr6
*ifaca
,
3821 u32 portid
, u32 seq
, int event
, unsigned int flags
)
3823 struct nlmsghdr
*nlh
;
3824 u8 scope
= RT_SCOPE_UNIVERSE
;
3825 int ifindex
= ifaca
->aca_idev
->dev
->ifindex
;
3827 if (ipv6_addr_scope(&ifaca
->aca_addr
) & IFA_SITE
)
3828 scope
= RT_SCOPE_SITE
;
3830 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct ifaddrmsg
), flags
);
3834 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
3835 if (nla_put(skb
, IFA_ANYCAST
, 16, &ifaca
->aca_addr
) < 0 ||
3836 put_cacheinfo(skb
, ifaca
->aca_cstamp
, ifaca
->aca_tstamp
,
3837 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
3838 nlmsg_cancel(skb
, nlh
);
3842 return nlmsg_end(skb
, nlh
);
3851 /* called with rcu_read_lock() */
3852 static int in6_dump_addrs(struct inet6_dev
*idev
, struct sk_buff
*skb
,
3853 struct netlink_callback
*cb
, enum addr_type_t type
,
3854 int s_ip_idx
, int *p_ip_idx
)
3856 struct ifmcaddr6
*ifmca
;
3857 struct ifacaddr6
*ifaca
;
3859 int ip_idx
= *p_ip_idx
;
3861 read_lock_bh(&idev
->lock
);
3863 case UNICAST_ADDR
: {
3864 struct inet6_ifaddr
*ifa
;
3866 /* unicast address incl. temp addr */
3867 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
3868 if (++ip_idx
< s_ip_idx
)
3870 err
= inet6_fill_ifaddr(skb
, ifa
,
3871 NETLINK_CB(cb
->skb
).portid
,
3880 case MULTICAST_ADDR
:
3881 /* multicast address */
3882 for (ifmca
= idev
->mc_list
; ifmca
;
3883 ifmca
= ifmca
->next
, ip_idx
++) {
3884 if (ip_idx
< s_ip_idx
)
3886 err
= inet6_fill_ifmcaddr(skb
, ifmca
,
3887 NETLINK_CB(cb
->skb
).portid
,
3896 /* anycast address */
3897 for (ifaca
= idev
->ac_list
; ifaca
;
3898 ifaca
= ifaca
->aca_next
, ip_idx
++) {
3899 if (ip_idx
< s_ip_idx
)
3901 err
= inet6_fill_ifacaddr(skb
, ifaca
,
3902 NETLINK_CB(cb
->skb
).portid
,
3913 read_unlock_bh(&idev
->lock
);
3918 static int inet6_dump_addr(struct sk_buff
*skb
, struct netlink_callback
*cb
,
3919 enum addr_type_t type
)
3921 struct net
*net
= sock_net(skb
->sk
);
3924 int s_idx
, s_ip_idx
;
3925 struct net_device
*dev
;
3926 struct inet6_dev
*idev
;
3927 struct hlist_head
*head
;
3930 s_idx
= idx
= cb
->args
[1];
3931 s_ip_idx
= ip_idx
= cb
->args
[2];
3934 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
3936 head
= &net
->dev_index_head
[h
];
3937 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
3940 if (h
> s_h
|| idx
> s_idx
)
3943 idev
= __in6_dev_get(dev
);
3947 if (in6_dump_addrs(idev
, skb
, cb
, type
,
3948 s_ip_idx
, &ip_idx
) <= 0)
3958 cb
->args
[2] = ip_idx
;
3963 static int inet6_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3965 enum addr_type_t type
= UNICAST_ADDR
;
3967 return inet6_dump_addr(skb
, cb
, type
);
3970 static int inet6_dump_ifmcaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3972 enum addr_type_t type
= MULTICAST_ADDR
;
3974 return inet6_dump_addr(skb
, cb
, type
);
3978 static int inet6_dump_ifacaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3980 enum addr_type_t type
= ANYCAST_ADDR
;
3982 return inet6_dump_addr(skb
, cb
, type
);
3985 static int inet6_rtm_getaddr(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
)
3987 struct net
*net
= sock_net(in_skb
->sk
);
3988 struct ifaddrmsg
*ifm
;
3989 struct nlattr
*tb
[IFA_MAX
+1];
3990 struct in6_addr
*addr
= NULL
;
3991 struct net_device
*dev
= NULL
;
3992 struct inet6_ifaddr
*ifa
;
3993 struct sk_buff
*skb
;
3996 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, ifa_ipv6_policy
);
4000 addr
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
]);
4006 ifm
= nlmsg_data(nlh
);
4008 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
4010 ifa
= ipv6_get_ifaddr(net
, addr
, dev
, 1);
4012 err
= -EADDRNOTAVAIL
;
4016 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL
);
4022 err
= inet6_fill_ifaddr(skb
, ifa
, NETLINK_CB(in_skb
).portid
,
4023 nlh
->nlmsg_seq
, RTM_NEWADDR
, 0);
4025 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4026 WARN_ON(err
== -EMSGSIZE
);
4030 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
4037 static void inet6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
)
4039 struct sk_buff
*skb
;
4040 struct net
*net
= dev_net(ifa
->idev
->dev
);
4043 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC
);
4047 err
= inet6_fill_ifaddr(skb
, ifa
, 0, 0, event
, 0);
4049 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4050 WARN_ON(err
== -EMSGSIZE
);
4054 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFADDR
, NULL
, GFP_ATOMIC
);
4058 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFADDR
, err
);
4061 static inline void ipv6_store_devconf(struct ipv6_devconf
*cnf
,
4062 __s32
*array
, int bytes
)
4064 BUG_ON(bytes
< (DEVCONF_MAX
* 4));
4066 memset(array
, 0, bytes
);
4067 array
[DEVCONF_FORWARDING
] = cnf
->forwarding
;
4068 array
[DEVCONF_HOPLIMIT
] = cnf
->hop_limit
;
4069 array
[DEVCONF_MTU6
] = cnf
->mtu6
;
4070 array
[DEVCONF_ACCEPT_RA
] = cnf
->accept_ra
;
4071 array
[DEVCONF_ACCEPT_REDIRECTS
] = cnf
->accept_redirects
;
4072 array
[DEVCONF_AUTOCONF
] = cnf
->autoconf
;
4073 array
[DEVCONF_DAD_TRANSMITS
] = cnf
->dad_transmits
;
4074 array
[DEVCONF_RTR_SOLICITS
] = cnf
->rtr_solicits
;
4075 array
[DEVCONF_RTR_SOLICIT_INTERVAL
] =
4076 jiffies_to_msecs(cnf
->rtr_solicit_interval
);
4077 array
[DEVCONF_RTR_SOLICIT_DELAY
] =
4078 jiffies_to_msecs(cnf
->rtr_solicit_delay
);
4079 array
[DEVCONF_FORCE_MLD_VERSION
] = cnf
->force_mld_version
;
4080 #ifdef CONFIG_IPV6_PRIVACY
4081 array
[DEVCONF_USE_TEMPADDR
] = cnf
->use_tempaddr
;
4082 array
[DEVCONF_TEMP_VALID_LFT
] = cnf
->temp_valid_lft
;
4083 array
[DEVCONF_TEMP_PREFERED_LFT
] = cnf
->temp_prefered_lft
;
4084 array
[DEVCONF_REGEN_MAX_RETRY
] = cnf
->regen_max_retry
;
4085 array
[DEVCONF_MAX_DESYNC_FACTOR
] = cnf
->max_desync_factor
;
4087 array
[DEVCONF_MAX_ADDRESSES
] = cnf
->max_addresses
;
4088 array
[DEVCONF_ACCEPT_RA_DEFRTR
] = cnf
->accept_ra_defrtr
;
4089 array
[DEVCONF_ACCEPT_RA_PINFO
] = cnf
->accept_ra_pinfo
;
4090 #ifdef CONFIG_IPV6_ROUTER_PREF
4091 array
[DEVCONF_ACCEPT_RA_RTR_PREF
] = cnf
->accept_ra_rtr_pref
;
4092 array
[DEVCONF_RTR_PROBE_INTERVAL
] =
4093 jiffies_to_msecs(cnf
->rtr_probe_interval
);
4094 #ifdef CONFIG_IPV6_ROUTE_INFO
4095 array
[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN
] = cnf
->accept_ra_rt_info_max_plen
;
4098 array
[DEVCONF_PROXY_NDP
] = cnf
->proxy_ndp
;
4099 array
[DEVCONF_ACCEPT_SOURCE_ROUTE
] = cnf
->accept_source_route
;
4100 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4101 array
[DEVCONF_OPTIMISTIC_DAD
] = cnf
->optimistic_dad
;
4103 #ifdef CONFIG_IPV6_MROUTE
4104 array
[DEVCONF_MC_FORWARDING
] = cnf
->mc_forwarding
;
4106 array
[DEVCONF_DISABLE_IPV6
] = cnf
->disable_ipv6
;
4107 array
[DEVCONF_ACCEPT_DAD
] = cnf
->accept_dad
;
4108 array
[DEVCONF_FORCE_TLLAO
] = cnf
->force_tllao
;
4109 array
[DEVCONF_NDISC_NOTIFY
] = cnf
->ndisc_notify
;
4112 static inline size_t inet6_ifla6_size(void)
4114 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4115 + nla_total_size(sizeof(struct ifla_cacheinfo
))
4116 + nla_total_size(DEVCONF_MAX
* 4) /* IFLA_INET6_CONF */
4117 + nla_total_size(IPSTATS_MIB_MAX
* 8) /* IFLA_INET6_STATS */
4118 + nla_total_size(ICMP6_MIB_MAX
* 8); /* IFLA_INET6_ICMP6STATS */
4121 static inline size_t inet6_if_nlmsg_size(void)
4123 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
4124 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
4125 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
4126 + nla_total_size(4) /* IFLA_MTU */
4127 + nla_total_size(4) /* IFLA_LINK */
4128 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4131 static inline void __snmp6_fill_statsdev(u64
*stats
, atomic_long_t
*mib
,
4132 int items
, int bytes
)
4135 int pad
= bytes
- sizeof(u64
) * items
;
4138 /* Use put_unaligned() because stats may not be aligned for u64. */
4139 put_unaligned(items
, &stats
[0]);
4140 for (i
= 1; i
< items
; i
++)
4141 put_unaligned(atomic_long_read(&mib
[i
]), &stats
[i
]);
4143 memset(&stats
[items
], 0, pad
);
4146 static inline void __snmp6_fill_stats64(u64
*stats
, void __percpu
**mib
,
4147 int items
, int bytes
, size_t syncpoff
)
4150 int pad
= bytes
- sizeof(u64
) * items
;
4153 /* Use put_unaligned() because stats may not be aligned for u64. */
4154 put_unaligned(items
, &stats
[0]);
4155 for (i
= 1; i
< items
; i
++)
4156 put_unaligned(snmp_fold_field64(mib
, i
, syncpoff
), &stats
[i
]);
4158 memset(&stats
[items
], 0, pad
);
4161 static void snmp6_fill_stats(u64
*stats
, struct inet6_dev
*idev
, int attrtype
,
4165 case IFLA_INET6_STATS
:
4166 __snmp6_fill_stats64(stats
, (void __percpu
**)idev
->stats
.ipv6
,
4167 IPSTATS_MIB_MAX
, bytes
, offsetof(struct ipstats_mib
, syncp
));
4169 case IFLA_INET6_ICMP6STATS
:
4170 __snmp6_fill_statsdev(stats
, idev
->stats
.icmpv6dev
->mibs
, ICMP6_MIB_MAX
, bytes
);
4175 static int inet6_fill_ifla6_attrs(struct sk_buff
*skb
, struct inet6_dev
*idev
)
4178 struct ifla_cacheinfo ci
;
4180 if (nla_put_u32(skb
, IFLA_INET6_FLAGS
, idev
->if_flags
))
4181 goto nla_put_failure
;
4182 ci
.max_reasm_len
= IPV6_MAXPLEN
;
4183 ci
.tstamp
= cstamp_delta(idev
->tstamp
);
4184 ci
.reachable_time
= jiffies_to_msecs(idev
->nd_parms
->reachable_time
);
4185 ci
.retrans_time
= jiffies_to_msecs(idev
->nd_parms
->retrans_time
);
4186 if (nla_put(skb
, IFLA_INET6_CACHEINFO
, sizeof(ci
), &ci
))
4187 goto nla_put_failure
;
4188 nla
= nla_reserve(skb
, IFLA_INET6_CONF
, DEVCONF_MAX
* sizeof(s32
));
4190 goto nla_put_failure
;
4191 ipv6_store_devconf(&idev
->cnf
, nla_data(nla
), nla_len(nla
));
4193 /* XXX - MC not implemented */
4195 nla
= nla_reserve(skb
, IFLA_INET6_STATS
, IPSTATS_MIB_MAX
* sizeof(u64
));
4197 goto nla_put_failure
;
4198 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_STATS
, nla_len(nla
));
4200 nla
= nla_reserve(skb
, IFLA_INET6_ICMP6STATS
, ICMP6_MIB_MAX
* sizeof(u64
));
4202 goto nla_put_failure
;
4203 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_ICMP6STATS
, nla_len(nla
));
4211 static size_t inet6_get_link_af_size(const struct net_device
*dev
)
4213 if (!__in6_dev_get(dev
))
4216 return inet6_ifla6_size();
4219 static int inet6_fill_link_af(struct sk_buff
*skb
, const struct net_device
*dev
)
4221 struct inet6_dev
*idev
= __in6_dev_get(dev
);
4226 if (inet6_fill_ifla6_attrs(skb
, idev
) < 0)
4232 static int inet6_fill_ifinfo(struct sk_buff
*skb
, struct inet6_dev
*idev
,
4233 u32 portid
, u32 seq
, int event
, unsigned int flags
)
4235 struct net_device
*dev
= idev
->dev
;
4236 struct ifinfomsg
*hdr
;
4237 struct nlmsghdr
*nlh
;
4240 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*hdr
), flags
);
4244 hdr
= nlmsg_data(nlh
);
4245 hdr
->ifi_family
= AF_INET6
;
4247 hdr
->ifi_type
= dev
->type
;
4248 hdr
->ifi_index
= dev
->ifindex
;
4249 hdr
->ifi_flags
= dev_get_flags(dev
);
4250 hdr
->ifi_change
= 0;
4252 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
4254 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
4255 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
4256 (dev
->ifindex
!= dev
->iflink
&&
4257 nla_put_u32(skb
, IFLA_LINK
, dev
->iflink
)))
4258 goto nla_put_failure
;
4259 protoinfo
= nla_nest_start(skb
, IFLA_PROTINFO
);
4260 if (protoinfo
== NULL
)
4261 goto nla_put_failure
;
4263 if (inet6_fill_ifla6_attrs(skb
, idev
) < 0)
4264 goto nla_put_failure
;
4266 nla_nest_end(skb
, protoinfo
);
4267 return nlmsg_end(skb
, nlh
);
4270 nlmsg_cancel(skb
, nlh
);
4274 static int inet6_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
4276 struct net
*net
= sock_net(skb
->sk
);
4279 struct net_device
*dev
;
4280 struct inet6_dev
*idev
;
4281 struct hlist_head
*head
;
4284 s_idx
= cb
->args
[1];
4287 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
4289 head
= &net
->dev_index_head
[h
];
4290 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
4293 idev
= __in6_dev_get(dev
);
4296 if (inet6_fill_ifinfo(skb
, idev
,
4297 NETLINK_CB(cb
->skb
).portid
,
4299 RTM_NEWLINK
, NLM_F_MULTI
) <= 0)
4313 void inet6_ifinfo_notify(int event
, struct inet6_dev
*idev
)
4315 struct sk_buff
*skb
;
4316 struct net
*net
= dev_net(idev
->dev
);
4319 skb
= nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC
);
4323 err
= inet6_fill_ifinfo(skb
, idev
, 0, 0, event
, 0);
4325 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4326 WARN_ON(err
== -EMSGSIZE
);
4330 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFINFO
, NULL
, GFP_ATOMIC
);
4334 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFINFO
, err
);
4337 static inline size_t inet6_prefix_nlmsg_size(void)
4339 return NLMSG_ALIGN(sizeof(struct prefixmsg
))
4340 + nla_total_size(sizeof(struct in6_addr
))
4341 + nla_total_size(sizeof(struct prefix_cacheinfo
));
4344 static int inet6_fill_prefix(struct sk_buff
*skb
, struct inet6_dev
*idev
,
4345 struct prefix_info
*pinfo
, u32 portid
, u32 seq
,
4346 int event
, unsigned int flags
)
4348 struct prefixmsg
*pmsg
;
4349 struct nlmsghdr
*nlh
;
4350 struct prefix_cacheinfo ci
;
4352 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*pmsg
), flags
);
4356 pmsg
= nlmsg_data(nlh
);
4357 pmsg
->prefix_family
= AF_INET6
;
4358 pmsg
->prefix_pad1
= 0;
4359 pmsg
->prefix_pad2
= 0;
4360 pmsg
->prefix_ifindex
= idev
->dev
->ifindex
;
4361 pmsg
->prefix_len
= pinfo
->prefix_len
;
4362 pmsg
->prefix_type
= pinfo
->type
;
4363 pmsg
->prefix_pad3
= 0;
4364 pmsg
->prefix_flags
= 0;
4366 pmsg
->prefix_flags
|= IF_PREFIX_ONLINK
;
4367 if (pinfo
->autoconf
)
4368 pmsg
->prefix_flags
|= IF_PREFIX_AUTOCONF
;
4370 if (nla_put(skb
, PREFIX_ADDRESS
, sizeof(pinfo
->prefix
), &pinfo
->prefix
))
4371 goto nla_put_failure
;
4372 ci
.preferred_time
= ntohl(pinfo
->prefered
);
4373 ci
.valid_time
= ntohl(pinfo
->valid
);
4374 if (nla_put(skb
, PREFIX_CACHEINFO
, sizeof(ci
), &ci
))
4375 goto nla_put_failure
;
4376 return nlmsg_end(skb
, nlh
);
4379 nlmsg_cancel(skb
, nlh
);
4383 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
4384 struct prefix_info
*pinfo
)
4386 struct sk_buff
*skb
;
4387 struct net
*net
= dev_net(idev
->dev
);
4390 skb
= nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC
);
4394 err
= inet6_fill_prefix(skb
, idev
, pinfo
, 0, 0, event
, 0);
4396 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4397 WARN_ON(err
== -EMSGSIZE
);
4401 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_PREFIX
, NULL
, GFP_ATOMIC
);
4405 rtnl_set_sk_err(net
, RTNLGRP_IPV6_PREFIX
, err
);
4408 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
4410 inet6_ifa_notify(event
? : RTM_NEWADDR
, ifp
);
4415 * If the address was optimistic
4416 * we inserted the route at the start of
4417 * our DAD process, so we don't need
4420 if (!(ifp
->rt
->rt6i_node
))
4421 ip6_ins_rt(ifp
->rt
);
4422 if (ifp
->idev
->cnf
.forwarding
)
4423 addrconf_join_anycast(ifp
);
4426 if (ifp
->idev
->cnf
.forwarding
)
4427 addrconf_leave_anycast(ifp
);
4428 addrconf_leave_solict(ifp
->idev
, &ifp
->addr
);
4429 dst_hold(&ifp
->rt
->dst
);
4431 if (ip6_del_rt(ifp
->rt
))
4432 dst_free(&ifp
->rt
->dst
);
4437 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
4440 if (likely(ifp
->idev
->dead
== 0))
4441 __ipv6_ifa_notify(event
, ifp
);
4442 rcu_read_unlock_bh();
4445 #ifdef CONFIG_SYSCTL
4448 int addrconf_sysctl_forward(ctl_table
*ctl
, int write
,
4449 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
4451 int *valp
= ctl
->data
;
4458 * ctl->data points to idev->cnf.forwarding, we should
4459 * not modify it until we get the rtnl lock.
4464 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
4467 ret
= addrconf_fixup_forwarding(ctl
, valp
, val
);
4473 static void dev_disable_change(struct inet6_dev
*idev
)
4475 if (!idev
|| !idev
->dev
)
4478 if (idev
->cnf
.disable_ipv6
)
4479 addrconf_notify(NULL
, NETDEV_DOWN
, idev
->dev
);
4481 addrconf_notify(NULL
, NETDEV_UP
, idev
->dev
);
4484 static void addrconf_disable_change(struct net
*net
, __s32 newf
)
4486 struct net_device
*dev
;
4487 struct inet6_dev
*idev
;
4490 for_each_netdev_rcu(net
, dev
) {
4491 idev
= __in6_dev_get(dev
);
4493 int changed
= (!idev
->cnf
.disable_ipv6
) ^ (!newf
);
4494 idev
->cnf
.disable_ipv6
= newf
;
4496 dev_disable_change(idev
);
4502 static int addrconf_disable_ipv6(struct ctl_table
*table
, int *p
, int newf
)
4507 if (!rtnl_trylock())
4508 return restart_syscall();
4510 net
= (struct net
*)table
->extra2
;
4514 if (p
== &net
->ipv6
.devconf_dflt
->disable_ipv6
) {
4519 if (p
== &net
->ipv6
.devconf_all
->disable_ipv6
) {
4520 net
->ipv6
.devconf_dflt
->disable_ipv6
= newf
;
4521 addrconf_disable_change(net
, newf
);
4522 } else if ((!newf
) ^ (!old
))
4523 dev_disable_change((struct inet6_dev
*)table
->extra1
);
4530 int addrconf_sysctl_disable(ctl_table
*ctl
, int write
,
4531 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
4533 int *valp
= ctl
->data
;
4540 * ctl->data points to idev->cnf.disable_ipv6, we should
4541 * not modify it until we get the rtnl lock.
4546 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
4549 ret
= addrconf_disable_ipv6(ctl
, valp
, val
);
4555 static struct addrconf_sysctl_table
4557 struct ctl_table_header
*sysctl_header
;
4558 ctl_table addrconf_vars
[DEVCONF_MAX
+1];
4559 } addrconf_sysctl __read_mostly
= {
4560 .sysctl_header
= NULL
,
4563 .procname
= "forwarding",
4564 .data
= &ipv6_devconf
.forwarding
,
4565 .maxlen
= sizeof(int),
4567 .proc_handler
= addrconf_sysctl_forward
,
4570 .procname
= "hop_limit",
4571 .data
= &ipv6_devconf
.hop_limit
,
4572 .maxlen
= sizeof(int),
4574 .proc_handler
= proc_dointvec
,
4578 .data
= &ipv6_devconf
.mtu6
,
4579 .maxlen
= sizeof(int),
4581 .proc_handler
= proc_dointvec
,
4584 .procname
= "accept_ra",
4585 .data
= &ipv6_devconf
.accept_ra
,
4586 .maxlen
= sizeof(int),
4588 .proc_handler
= proc_dointvec
,
4591 .procname
= "accept_redirects",
4592 .data
= &ipv6_devconf
.accept_redirects
,
4593 .maxlen
= sizeof(int),
4595 .proc_handler
= proc_dointvec
,
4598 .procname
= "autoconf",
4599 .data
= &ipv6_devconf
.autoconf
,
4600 .maxlen
= sizeof(int),
4602 .proc_handler
= proc_dointvec
,
4605 .procname
= "dad_transmits",
4606 .data
= &ipv6_devconf
.dad_transmits
,
4607 .maxlen
= sizeof(int),
4609 .proc_handler
= proc_dointvec
,
4612 .procname
= "router_solicitations",
4613 .data
= &ipv6_devconf
.rtr_solicits
,
4614 .maxlen
= sizeof(int),
4616 .proc_handler
= proc_dointvec
,
4619 .procname
= "router_solicitation_interval",
4620 .data
= &ipv6_devconf
.rtr_solicit_interval
,
4621 .maxlen
= sizeof(int),
4623 .proc_handler
= proc_dointvec_jiffies
,
4626 .procname
= "router_solicitation_delay",
4627 .data
= &ipv6_devconf
.rtr_solicit_delay
,
4628 .maxlen
= sizeof(int),
4630 .proc_handler
= proc_dointvec_jiffies
,
4633 .procname
= "force_mld_version",
4634 .data
= &ipv6_devconf
.force_mld_version
,
4635 .maxlen
= sizeof(int),
4637 .proc_handler
= proc_dointvec
,
4639 #ifdef CONFIG_IPV6_PRIVACY
4641 .procname
= "use_tempaddr",
4642 .data
= &ipv6_devconf
.use_tempaddr
,
4643 .maxlen
= sizeof(int),
4645 .proc_handler
= proc_dointvec
,
4648 .procname
= "temp_valid_lft",
4649 .data
= &ipv6_devconf
.temp_valid_lft
,
4650 .maxlen
= sizeof(int),
4652 .proc_handler
= proc_dointvec
,
4655 .procname
= "temp_prefered_lft",
4656 .data
= &ipv6_devconf
.temp_prefered_lft
,
4657 .maxlen
= sizeof(int),
4659 .proc_handler
= proc_dointvec
,
4662 .procname
= "regen_max_retry",
4663 .data
= &ipv6_devconf
.regen_max_retry
,
4664 .maxlen
= sizeof(int),
4666 .proc_handler
= proc_dointvec
,
4669 .procname
= "max_desync_factor",
4670 .data
= &ipv6_devconf
.max_desync_factor
,
4671 .maxlen
= sizeof(int),
4673 .proc_handler
= proc_dointvec
,
4677 .procname
= "max_addresses",
4678 .data
= &ipv6_devconf
.max_addresses
,
4679 .maxlen
= sizeof(int),
4681 .proc_handler
= proc_dointvec
,
4684 .procname
= "accept_ra_defrtr",
4685 .data
= &ipv6_devconf
.accept_ra_defrtr
,
4686 .maxlen
= sizeof(int),
4688 .proc_handler
= proc_dointvec
,
4691 .procname
= "accept_ra_pinfo",
4692 .data
= &ipv6_devconf
.accept_ra_pinfo
,
4693 .maxlen
= sizeof(int),
4695 .proc_handler
= proc_dointvec
,
4697 #ifdef CONFIG_IPV6_ROUTER_PREF
4699 .procname
= "accept_ra_rtr_pref",
4700 .data
= &ipv6_devconf
.accept_ra_rtr_pref
,
4701 .maxlen
= sizeof(int),
4703 .proc_handler
= proc_dointvec
,
4706 .procname
= "router_probe_interval",
4707 .data
= &ipv6_devconf
.rtr_probe_interval
,
4708 .maxlen
= sizeof(int),
4710 .proc_handler
= proc_dointvec_jiffies
,
4712 #ifdef CONFIG_IPV6_ROUTE_INFO
4714 .procname
= "accept_ra_rt_info_max_plen",
4715 .data
= &ipv6_devconf
.accept_ra_rt_info_max_plen
,
4716 .maxlen
= sizeof(int),
4718 .proc_handler
= proc_dointvec
,
4723 .procname
= "proxy_ndp",
4724 .data
= &ipv6_devconf
.proxy_ndp
,
4725 .maxlen
= sizeof(int),
4727 .proc_handler
= proc_dointvec
,
4730 .procname
= "accept_source_route",
4731 .data
= &ipv6_devconf
.accept_source_route
,
4732 .maxlen
= sizeof(int),
4734 .proc_handler
= proc_dointvec
,
4736 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4738 .procname
= "optimistic_dad",
4739 .data
= &ipv6_devconf
.optimistic_dad
,
4740 .maxlen
= sizeof(int),
4742 .proc_handler
= proc_dointvec
,
4746 #ifdef CONFIG_IPV6_MROUTE
4748 .procname
= "mc_forwarding",
4749 .data
= &ipv6_devconf
.mc_forwarding
,
4750 .maxlen
= sizeof(int),
4752 .proc_handler
= proc_dointvec
,
4756 .procname
= "disable_ipv6",
4757 .data
= &ipv6_devconf
.disable_ipv6
,
4758 .maxlen
= sizeof(int),
4760 .proc_handler
= addrconf_sysctl_disable
,
4763 .procname
= "accept_dad",
4764 .data
= &ipv6_devconf
.accept_dad
,
4765 .maxlen
= sizeof(int),
4767 .proc_handler
= proc_dointvec
,
4770 .procname
= "force_tllao",
4771 .data
= &ipv6_devconf
.force_tllao
,
4772 .maxlen
= sizeof(int),
4774 .proc_handler
= proc_dointvec
4777 .procname
= "ndisc_notify",
4778 .data
= &ipv6_devconf
.ndisc_notify
,
4779 .maxlen
= sizeof(int),
4781 .proc_handler
= proc_dointvec
4789 static int __addrconf_sysctl_register(struct net
*net
, char *dev_name
,
4790 struct inet6_dev
*idev
, struct ipv6_devconf
*p
)
4793 struct addrconf_sysctl_table
*t
;
4794 char path
[sizeof("net/ipv6/conf/") + IFNAMSIZ
];
4796 t
= kmemdup(&addrconf_sysctl
, sizeof(*t
), GFP_KERNEL
);
4800 for (i
= 0; t
->addrconf_vars
[i
].data
; i
++) {
4801 t
->addrconf_vars
[i
].data
+= (char *)p
- (char *)&ipv6_devconf
;
4802 t
->addrconf_vars
[i
].extra1
= idev
; /* embedded; no ref */
4803 t
->addrconf_vars
[i
].extra2
= net
;
4806 snprintf(path
, sizeof(path
), "net/ipv6/conf/%s", dev_name
);
4808 t
->sysctl_header
= register_net_sysctl(net
, path
, t
->addrconf_vars
);
4809 if (t
->sysctl_header
== NULL
)
4821 static void __addrconf_sysctl_unregister(struct ipv6_devconf
*p
)
4823 struct addrconf_sysctl_table
*t
;
4825 if (p
->sysctl
== NULL
)
4830 unregister_net_sysctl_table(t
->sysctl_header
);
4834 static void addrconf_sysctl_register(struct inet6_dev
*idev
)
4836 neigh_sysctl_register(idev
->dev
, idev
->nd_parms
, "ipv6",
4837 &ndisc_ifinfo_sysctl_change
);
4838 __addrconf_sysctl_register(dev_net(idev
->dev
), idev
->dev
->name
,
4842 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
4844 __addrconf_sysctl_unregister(&idev
->cnf
);
4845 neigh_sysctl_unregister(idev
->nd_parms
);
4851 static int __net_init
addrconf_init_net(struct net
*net
)
4854 struct ipv6_devconf
*all
, *dflt
;
4857 all
= &ipv6_devconf
;
4858 dflt
= &ipv6_devconf_dflt
;
4860 if (!net_eq(net
, &init_net
)) {
4861 all
= kmemdup(all
, sizeof(ipv6_devconf
), GFP_KERNEL
);
4865 dflt
= kmemdup(dflt
, sizeof(ipv6_devconf_dflt
), GFP_KERNEL
);
4867 goto err_alloc_dflt
;
4869 /* these will be inherited by all namespaces */
4870 dflt
->autoconf
= ipv6_defaults
.autoconf
;
4871 dflt
->disable_ipv6
= ipv6_defaults
.disable_ipv6
;
4874 net
->ipv6
.devconf_all
= all
;
4875 net
->ipv6
.devconf_dflt
= dflt
;
4877 #ifdef CONFIG_SYSCTL
4878 err
= __addrconf_sysctl_register(net
, "all", NULL
, all
);
4882 err
= __addrconf_sysctl_register(net
, "default", NULL
, dflt
);
4888 #ifdef CONFIG_SYSCTL
4890 __addrconf_sysctl_unregister(all
);
4900 static void __net_exit
addrconf_exit_net(struct net
*net
)
4902 #ifdef CONFIG_SYSCTL
4903 __addrconf_sysctl_unregister(net
->ipv6
.devconf_dflt
);
4904 __addrconf_sysctl_unregister(net
->ipv6
.devconf_all
);
4906 if (!net_eq(net
, &init_net
)) {
4907 kfree(net
->ipv6
.devconf_dflt
);
4908 kfree(net
->ipv6
.devconf_all
);
4912 static struct pernet_operations addrconf_ops
= {
4913 .init
= addrconf_init_net
,
4914 .exit
= addrconf_exit_net
,
4921 int register_inet6addr_notifier(struct notifier_block
*nb
)
4923 return atomic_notifier_chain_register(&inet6addr_chain
, nb
);
4925 EXPORT_SYMBOL(register_inet6addr_notifier
);
4927 int unregister_inet6addr_notifier(struct notifier_block
*nb
)
4929 return atomic_notifier_chain_unregister(&inet6addr_chain
, nb
);
4931 EXPORT_SYMBOL(unregister_inet6addr_notifier
);
4933 static struct rtnl_af_ops inet6_ops
= {
4935 .fill_link_af
= inet6_fill_link_af
,
4936 .get_link_af_size
= inet6_get_link_af_size
,
4940 * Init / cleanup code
4943 int __init
addrconf_init(void)
4947 err
= ipv6_addr_label_init();
4949 pr_crit("%s: cannot initialize default policy table: %d\n",
4954 err
= register_pernet_subsys(&addrconf_ops
);
4958 /* The addrconf netdev notifier requires that loopback_dev
4959 * has it's ipv6 private information allocated and setup
4960 * before it can bring up and give link-local addresses
4961 * to other devices which are up.
4963 * Unfortunately, loopback_dev is not necessarily the first
4964 * entry in the global dev_base list of net devices. In fact,
4965 * it is likely to be the very last entry on that list.
4966 * So this causes the notifier registry below to try and
4967 * give link-local addresses to all devices besides loopback_dev
4968 * first, then loopback_dev, which cases all the non-loopback_dev
4969 * devices to fail to get a link-local address.
4971 * So, as a temporary fix, allocate the ipv6 structure for
4972 * loopback_dev first by hand.
4973 * Longer term, all of the dependencies ipv6 has upon the loopback
4974 * device and it being up should be removed.
4977 if (!ipv6_add_dev(init_net
.loopback_dev
))
4983 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
4984 INIT_HLIST_HEAD(&inet6_addr_lst
[i
]);
4986 register_netdevice_notifier(&ipv6_dev_notf
);
4990 err
= rtnl_af_register(&inet6_ops
);
4994 err
= __rtnl_register(PF_INET6
, RTM_GETLINK
, NULL
, inet6_dump_ifinfo
,
4999 /* Only the first call to __rtnl_register can fail */
5000 __rtnl_register(PF_INET6
, RTM_NEWADDR
, inet6_rtm_newaddr
, NULL
, NULL
);
5001 __rtnl_register(PF_INET6
, RTM_DELADDR
, inet6_rtm_deladdr
, NULL
, NULL
);
5002 __rtnl_register(PF_INET6
, RTM_GETADDR
, inet6_rtm_getaddr
,
5003 inet6_dump_ifaddr
, NULL
);
5004 __rtnl_register(PF_INET6
, RTM_GETMULTICAST
, NULL
,
5005 inet6_dump_ifmcaddr
, NULL
);
5006 __rtnl_register(PF_INET6
, RTM_GETANYCAST
, NULL
,
5007 inet6_dump_ifacaddr
, NULL
);
5008 __rtnl_register(PF_INET6
, RTM_GETNETCONF
, inet6_netconf_get_devconf
,
5009 inet6_netconf_dump_devconf
, NULL
);
5011 ipv6_addr_label_rtnl_register();
5015 rtnl_af_unregister(&inet6_ops
);
5017 unregister_netdevice_notifier(&ipv6_dev_notf
);
5019 unregister_pernet_subsys(&addrconf_ops
);
5021 ipv6_addr_label_cleanup();
5026 void addrconf_cleanup(void)
5028 struct net_device
*dev
;
5031 unregister_netdevice_notifier(&ipv6_dev_notf
);
5032 unregister_pernet_subsys(&addrconf_ops
);
5033 ipv6_addr_label_cleanup();
5037 __rtnl_af_unregister(&inet6_ops
);
5039 /* clean dev list */
5040 for_each_netdev(&init_net
, dev
) {
5041 if (__in6_dev_get(dev
) == NULL
)
5043 addrconf_ifdown(dev
, 1);
5045 addrconf_ifdown(init_net
.loopback_dev
, 2);
5050 spin_lock_bh(&addrconf_hash_lock
);
5051 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
5052 WARN_ON(!hlist_empty(&inet6_addr_lst
[i
]));
5053 spin_unlock_bh(&addrconf_hash_lock
);
5055 del_timer(&addr_chk_timer
);