1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPv6 Address [auto]configuration
4 * Linux INET6 implementation
7 * Pedro Roque <roque@di.fc.ul.pt>
8 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
14 * Janos Farkas : delete timer on ifdown
15 * <chexum@bankinf.banki.hu>
16 * Andi Kleen : kill double kfree on module
18 * Maciej W. Rozycki : FDDI support
19 * sekiya@USAGI : Don't send too many RS
21 * yoshfuji@USAGI : Fixed interval between DAD
23 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
24 * address validation timer.
25 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
27 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
28 * address on a same interface.
29 * YOSHIFUJI Hideaki @USAGI : ARCnet support
30 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
32 * YOSHIFUJI Hideaki @USAGI : improved source address
33 * selection; consider scope,
37 #define pr_fmt(fmt) "IPv6: " fmt
39 #include <linux/errno.h>
40 #include <linux/types.h>
41 #include <linux/kernel.h>
42 #include <linux/sched/signal.h>
43 #include <linux/socket.h>
44 #include <linux/sockios.h>
45 #include <linux/net.h>
46 #include <linux/inet.h>
47 #include <linux/in6.h>
48 #include <linux/netdevice.h>
49 #include <linux/if_addr.h>
50 #include <linux/if_arp.h>
51 #include <linux/if_arcnet.h>
52 #include <linux/if_infiniband.h>
53 #include <linux/route.h>
54 #include <linux/inetdevice.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
58 #include <linux/sysctl.h>
60 #include <linux/capability.h>
61 #include <linux/delay.h>
62 #include <linux/notifier.h>
63 #include <linux/string.h>
64 #include <linux/hash.h>
66 #include <net/net_namespace.h>
70 #include <net/6lowpan.h>
71 #include <net/firewire.h>
73 #include <net/protocol.h>
74 #include <net/ndisc.h>
75 #include <net/ip6_route.h>
76 #include <net/addrconf.h>
79 #include <net/netlink.h>
80 #include <net/pkt_sched.h>
81 #include <net/l3mdev.h>
82 #include <linux/if_tunnel.h>
83 #include <linux/rtnetlink.h>
84 #include <linux/netconf.h>
85 #include <linux/random.h>
86 #include <linux/uaccess.h>
87 #include <asm/unaligned.h>
89 #include <linux/proc_fs.h>
90 #include <linux/seq_file.h>
91 #include <linux/export.h>
92 #include <linux/ioam6.h>
94 #define INFINITY_LIFE_TIME 0xFFFFFFFF
96 #define IPV6_MAX_STRLEN \
97 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
99 static inline u32
cstamp_delta(unsigned long cstamp
)
101 return (cstamp
- INITIAL_JIFFIES
) * 100UL / HZ
;
104 static inline s32
rfc3315_s14_backoff_init(s32 irt
)
106 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
107 u64 tmp
= get_random_u32_inclusive(900000, 1100000) * (u64
)irt
;
108 do_div(tmp
, 1000000);
112 static inline s32
rfc3315_s14_backoff_update(s32 rt
, s32 mrt
)
114 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
115 u64 tmp
= get_random_u32_inclusive(1900000, 2100000) * (u64
)rt
;
116 do_div(tmp
, 1000000);
117 if ((s32
)tmp
> mrt
) {
118 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
119 tmp
= get_random_u32_inclusive(900000, 1100000) * (u64
)mrt
;
120 do_div(tmp
, 1000000);
126 static int addrconf_sysctl_register(struct inet6_dev
*idev
);
127 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
);
129 static inline int addrconf_sysctl_register(struct inet6_dev
*idev
)
134 static inline void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
139 static void ipv6_gen_rnd_iid(struct in6_addr
*addr
);
141 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
);
142 static int ipv6_count_addresses(const struct inet6_dev
*idev
);
143 static int ipv6_generate_stable_address(struct in6_addr
*addr
,
145 const struct inet6_dev
*idev
);
147 #define IN6_ADDR_HSIZE_SHIFT 8
148 #define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT)
150 static void addrconf_verify(struct net
*net
);
151 static void addrconf_verify_rtnl(struct net
*net
);
153 static struct workqueue_struct
*addrconf_wq
;
155 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
);
156 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
);
158 static void addrconf_type_change(struct net_device
*dev
,
159 unsigned long event
);
160 static int addrconf_ifdown(struct net_device
*dev
, bool unregister
);
162 static struct fib6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
164 const struct net_device
*dev
,
165 u32 flags
, u32 noflags
,
168 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
);
169 static void addrconf_dad_work(struct work_struct
*w
);
170 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
, bool bump_id
,
172 static void addrconf_dad_run(struct inet6_dev
*idev
, bool restart
);
173 static void addrconf_rs_timer(struct timer_list
*t
);
174 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
175 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
);
177 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
178 struct prefix_info
*pinfo
);
180 static struct ipv6_devconf ipv6_devconf __read_mostly
= {
182 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
183 .mtu6
= IPV6_MIN_MTU
,
185 .accept_redirects
= 1,
187 .force_mld_version
= 0,
188 .mldv1_unsolicited_report_interval
= 10 * HZ
,
189 .mldv2_unsolicited_report_interval
= HZ
,
191 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
192 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
193 .rtr_solicit_max_interval
= RTR_SOLICITATION_MAX_INTERVAL
,
194 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
196 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
197 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
198 .regen_max_retry
= REGEN_MAX_RETRY
,
199 .max_desync_factor
= MAX_DESYNC_FACTOR
,
200 .max_addresses
= IPV6_MAX_ADDRESSES
,
201 .accept_ra_defrtr
= 1,
202 .ra_defrtr_metric
= IP6_RT_PRIO_USER
,
203 .accept_ra_from_local
= 0,
204 .accept_ra_min_hop_limit
= 1,
205 .accept_ra_min_lft
= 0,
206 .accept_ra_pinfo
= 1,
207 #ifdef CONFIG_IPV6_ROUTER_PREF
208 .accept_ra_rtr_pref
= 1,
209 .rtr_probe_interval
= 60 * HZ
,
210 #ifdef CONFIG_IPV6_ROUTE_INFO
211 .accept_ra_rt_info_min_plen
= 0,
212 .accept_ra_rt_info_max_plen
= 0,
216 .accept_source_route
= 0, /* we do not accept RH0 by default. */
219 .suppress_frag_ndisc
= 1,
222 .initialized
= false,
224 .use_oif_addrs_only
= 0,
225 .ignore_routes_with_linkdown
= 0,
226 .keep_addr_on_down
= 0,
228 #ifdef CONFIG_IPV6_SEG6_HMAC
229 .seg6_require_hmac
= 0,
232 .addr_gen_mode
= IN6_ADDR_GEN_MODE_EUI64
,
234 .rpl_seg_enabled
= 0,
236 .ioam6_id
= IOAM6_DEFAULT_IF_ID
,
237 .ioam6_id_wide
= IOAM6_DEFAULT_IF_ID_WIDE
,
238 .ndisc_evict_nocarrier
= 1,
239 .ra_honor_pio_life
= 0,
242 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly
= {
244 .hop_limit
= IPV6_DEFAULT_HOPLIMIT
,
245 .mtu6
= IPV6_MIN_MTU
,
247 .accept_redirects
= 1,
249 .force_mld_version
= 0,
250 .mldv1_unsolicited_report_interval
= 10 * HZ
,
251 .mldv2_unsolicited_report_interval
= HZ
,
253 .rtr_solicits
= MAX_RTR_SOLICITATIONS
,
254 .rtr_solicit_interval
= RTR_SOLICITATION_INTERVAL
,
255 .rtr_solicit_max_interval
= RTR_SOLICITATION_MAX_INTERVAL
,
256 .rtr_solicit_delay
= MAX_RTR_SOLICITATION_DELAY
,
258 .temp_valid_lft
= TEMP_VALID_LIFETIME
,
259 .temp_prefered_lft
= TEMP_PREFERRED_LIFETIME
,
260 .regen_max_retry
= REGEN_MAX_RETRY
,
261 .max_desync_factor
= MAX_DESYNC_FACTOR
,
262 .max_addresses
= IPV6_MAX_ADDRESSES
,
263 .accept_ra_defrtr
= 1,
264 .ra_defrtr_metric
= IP6_RT_PRIO_USER
,
265 .accept_ra_from_local
= 0,
266 .accept_ra_min_hop_limit
= 1,
267 .accept_ra_min_lft
= 0,
268 .accept_ra_pinfo
= 1,
269 #ifdef CONFIG_IPV6_ROUTER_PREF
270 .accept_ra_rtr_pref
= 1,
271 .rtr_probe_interval
= 60 * HZ
,
272 #ifdef CONFIG_IPV6_ROUTE_INFO
273 .accept_ra_rt_info_min_plen
= 0,
274 .accept_ra_rt_info_max_plen
= 0,
278 .accept_source_route
= 0, /* we do not accept RH0 by default. */
281 .suppress_frag_ndisc
= 1,
284 .initialized
= false,
286 .use_oif_addrs_only
= 0,
287 .ignore_routes_with_linkdown
= 0,
288 .keep_addr_on_down
= 0,
290 #ifdef CONFIG_IPV6_SEG6_HMAC
291 .seg6_require_hmac
= 0,
294 .addr_gen_mode
= IN6_ADDR_GEN_MODE_EUI64
,
296 .rpl_seg_enabled
= 0,
298 .ioam6_id
= IOAM6_DEFAULT_IF_ID
,
299 .ioam6_id_wide
= IOAM6_DEFAULT_IF_ID_WIDE
,
300 .ndisc_evict_nocarrier
= 1,
301 .ra_honor_pio_life
= 0,
304 /* Check if link is ready: is it up and is a valid qdisc available */
305 static inline bool addrconf_link_ready(const struct net_device
*dev
)
307 return netif_oper_up(dev
) && !qdisc_tx_is_noop(dev
);
310 static void addrconf_del_rs_timer(struct inet6_dev
*idev
)
312 if (del_timer(&idev
->rs_timer
))
316 static void addrconf_del_dad_work(struct inet6_ifaddr
*ifp
)
318 if (cancel_delayed_work(&ifp
->dad_work
))
322 static void addrconf_mod_rs_timer(struct inet6_dev
*idev
,
325 if (!mod_timer(&idev
->rs_timer
, jiffies
+ when
))
329 static void addrconf_mod_dad_work(struct inet6_ifaddr
*ifp
,
333 if (mod_delayed_work(addrconf_wq
, &ifp
->dad_work
, delay
))
337 static int snmp6_alloc_dev(struct inet6_dev
*idev
)
341 idev
->stats
.ipv6
= alloc_percpu_gfp(struct ipstats_mib
, GFP_KERNEL_ACCOUNT
);
342 if (!idev
->stats
.ipv6
)
345 for_each_possible_cpu(i
) {
346 struct ipstats_mib
*addrconf_stats
;
347 addrconf_stats
= per_cpu_ptr(idev
->stats
.ipv6
, i
);
348 u64_stats_init(&addrconf_stats
->syncp
);
352 idev
->stats
.icmpv6dev
= kzalloc(sizeof(struct icmpv6_mib_device
),
354 if (!idev
->stats
.icmpv6dev
)
356 idev
->stats
.icmpv6msgdev
= kzalloc(sizeof(struct icmpv6msg_mib_device
),
358 if (!idev
->stats
.icmpv6msgdev
)
364 kfree(idev
->stats
.icmpv6dev
);
366 free_percpu(idev
->stats
.ipv6
);
371 static struct inet6_dev
*ipv6_add_dev(struct net_device
*dev
)
373 struct inet6_dev
*ndev
;
378 if (dev
->mtu
< IPV6_MIN_MTU
&& dev
!= blackhole_netdev
)
379 return ERR_PTR(-EINVAL
);
381 ndev
= kzalloc(sizeof(*ndev
), GFP_KERNEL_ACCOUNT
);
385 rwlock_init(&ndev
->lock
);
387 INIT_LIST_HEAD(&ndev
->addr_list
);
388 timer_setup(&ndev
->rs_timer
, addrconf_rs_timer
, 0);
389 memcpy(&ndev
->cnf
, dev_net(dev
)->ipv6
.devconf_dflt
, sizeof(ndev
->cnf
));
391 if (ndev
->cnf
.stable_secret
.initialized
)
392 ndev
->cnf
.addr_gen_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
394 ndev
->cnf
.mtu6
= dev
->mtu
;
396 ndev
->nd_parms
= neigh_parms_alloc(dev
, &nd_tbl
);
397 if (!ndev
->nd_parms
) {
401 if (ndev
->cnf
.forwarding
)
402 dev_disable_lro(dev
);
403 /* We refer to the device */
404 netdev_hold(dev
, &ndev
->dev_tracker
, GFP_KERNEL
);
406 if (snmp6_alloc_dev(ndev
) < 0) {
407 netdev_dbg(dev
, "%s: cannot allocate memory for statistics\n",
409 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
410 netdev_put(dev
, &ndev
->dev_tracker
);
415 if (dev
!= blackhole_netdev
) {
416 if (snmp6_register_dev(ndev
) < 0) {
417 netdev_dbg(dev
, "%s: cannot create /proc/net/dev_snmp6/%s\n",
418 __func__
, dev
->name
);
422 /* One reference from device. */
423 refcount_set(&ndev
->refcnt
, 1);
425 if (dev
->flags
& (IFF_NOARP
| IFF_LOOPBACK
))
426 ndev
->cnf
.accept_dad
= -1;
428 #if IS_ENABLED(CONFIG_IPV6_SIT)
429 if (dev
->type
== ARPHRD_SIT
&& (dev
->priv_flags
& IFF_ISATAP
)) {
430 pr_info("%s: Disabled Multicast RS\n", dev
->name
);
431 ndev
->cnf
.rtr_solicits
= 0;
435 INIT_LIST_HEAD(&ndev
->tempaddr_list
);
436 ndev
->desync_factor
= U32_MAX
;
437 if ((dev
->flags
&IFF_LOOPBACK
) ||
438 dev
->type
== ARPHRD_TUNNEL
||
439 dev
->type
== ARPHRD_TUNNEL6
||
440 dev
->type
== ARPHRD_SIT
||
441 dev
->type
== ARPHRD_NONE
) {
442 ndev
->cnf
.use_tempaddr
= -1;
445 ndev
->token
= in6addr_any
;
447 if (netif_running(dev
) && addrconf_link_ready(dev
))
448 ndev
->if_flags
|= IF_READY
;
450 ipv6_mc_init_dev(ndev
);
451 ndev
->tstamp
= jiffies
;
452 if (dev
!= blackhole_netdev
) {
453 err
= addrconf_sysctl_register(ndev
);
455 ipv6_mc_destroy_dev(ndev
);
456 snmp6_unregister_dev(ndev
);
460 /* protected by rtnl_lock */
461 rcu_assign_pointer(dev
->ip6_ptr
, ndev
);
463 if (dev
!= blackhole_netdev
) {
464 /* Join interface-local all-node multicast group */
465 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allnodes
);
467 /* Join all-node multicast group */
468 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allnodes
);
470 /* Join all-router multicast group if forwarding is set */
471 if (ndev
->cnf
.forwarding
&& (dev
->flags
& IFF_MULTICAST
))
472 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
477 neigh_parms_release(&nd_tbl
, ndev
->nd_parms
);
479 in6_dev_finish_destroy(ndev
);
483 static struct inet6_dev
*ipv6_find_idev(struct net_device
*dev
)
485 struct inet6_dev
*idev
;
489 idev
= __in6_dev_get(dev
);
491 idev
= ipv6_add_dev(dev
);
496 if (dev
->flags
&IFF_UP
)
501 static int inet6_netconf_msgsize_devconf(int type
)
503 int size
= NLMSG_ALIGN(sizeof(struct netconfmsg
))
504 + nla_total_size(4); /* NETCONFA_IFINDEX */
507 if (type
== NETCONFA_ALL
)
510 if (all
|| type
== NETCONFA_FORWARDING
)
511 size
+= nla_total_size(4);
512 #ifdef CONFIG_IPV6_MROUTE
513 if (all
|| type
== NETCONFA_MC_FORWARDING
)
514 size
+= nla_total_size(4);
516 if (all
|| type
== NETCONFA_PROXY_NEIGH
)
517 size
+= nla_total_size(4);
519 if (all
|| type
== NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
)
520 size
+= nla_total_size(4);
525 static int inet6_netconf_fill_devconf(struct sk_buff
*skb
, int ifindex
,
526 struct ipv6_devconf
*devconf
, u32 portid
,
527 u32 seq
, int event
, unsigned int flags
,
530 struct nlmsghdr
*nlh
;
531 struct netconfmsg
*ncm
;
534 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(struct netconfmsg
),
539 if (type
== NETCONFA_ALL
)
542 ncm
= nlmsg_data(nlh
);
543 ncm
->ncm_family
= AF_INET6
;
545 if (nla_put_s32(skb
, NETCONFA_IFINDEX
, ifindex
) < 0)
546 goto nla_put_failure
;
551 if ((all
|| type
== NETCONFA_FORWARDING
) &&
552 nla_put_s32(skb
, NETCONFA_FORWARDING
, devconf
->forwarding
) < 0)
553 goto nla_put_failure
;
554 #ifdef CONFIG_IPV6_MROUTE
555 if ((all
|| type
== NETCONFA_MC_FORWARDING
) &&
556 nla_put_s32(skb
, NETCONFA_MC_FORWARDING
,
557 atomic_read(&devconf
->mc_forwarding
)) < 0)
558 goto nla_put_failure
;
560 if ((all
|| type
== NETCONFA_PROXY_NEIGH
) &&
561 nla_put_s32(skb
, NETCONFA_PROXY_NEIGH
, devconf
->proxy_ndp
) < 0)
562 goto nla_put_failure
;
564 if ((all
|| type
== NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
) &&
565 nla_put_s32(skb
, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
566 devconf
->ignore_routes_with_linkdown
) < 0)
567 goto nla_put_failure
;
574 nlmsg_cancel(skb
, nlh
);
578 void inet6_netconf_notify_devconf(struct net
*net
, int event
, int type
,
579 int ifindex
, struct ipv6_devconf
*devconf
)
584 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(type
), GFP_KERNEL
);
588 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
, 0, 0,
591 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
592 WARN_ON(err
== -EMSGSIZE
);
596 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_NETCONF
, NULL
, GFP_KERNEL
);
599 rtnl_set_sk_err(net
, RTNLGRP_IPV6_NETCONF
, err
);
602 static const struct nla_policy devconf_ipv6_policy
[NETCONFA_MAX
+1] = {
603 [NETCONFA_IFINDEX
] = { .len
= sizeof(int) },
604 [NETCONFA_FORWARDING
] = { .len
= sizeof(int) },
605 [NETCONFA_PROXY_NEIGH
] = { .len
= sizeof(int) },
606 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
] = { .len
= sizeof(int) },
609 static int inet6_netconf_valid_get_req(struct sk_buff
*skb
,
610 const struct nlmsghdr
*nlh
,
612 struct netlink_ext_ack
*extack
)
616 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(struct netconfmsg
))) {
617 NL_SET_ERR_MSG_MOD(extack
, "Invalid header for netconf get request");
621 if (!netlink_strict_get_check(skb
))
622 return nlmsg_parse_deprecated(nlh
, sizeof(struct netconfmsg
),
624 devconf_ipv6_policy
, extack
);
626 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(struct netconfmsg
),
628 devconf_ipv6_policy
, extack
);
632 for (i
= 0; i
<= NETCONFA_MAX
; i
++) {
637 case NETCONFA_IFINDEX
:
640 NL_SET_ERR_MSG_MOD(extack
, "Unsupported attribute in netconf get request");
648 static int inet6_netconf_get_devconf(struct sk_buff
*in_skb
,
649 struct nlmsghdr
*nlh
,
650 struct netlink_ext_ack
*extack
)
652 struct net
*net
= sock_net(in_skb
->sk
);
653 struct nlattr
*tb
[NETCONFA_MAX
+1];
654 struct inet6_dev
*in6_dev
= NULL
;
655 struct net_device
*dev
= NULL
;
657 struct ipv6_devconf
*devconf
;
661 err
= inet6_netconf_valid_get_req(in_skb
, nlh
, tb
, extack
);
665 if (!tb
[NETCONFA_IFINDEX
])
669 ifindex
= nla_get_s32(tb
[NETCONFA_IFINDEX
]);
671 case NETCONFA_IFINDEX_ALL
:
672 devconf
= net
->ipv6
.devconf_all
;
674 case NETCONFA_IFINDEX_DEFAULT
:
675 devconf
= net
->ipv6
.devconf_dflt
;
678 dev
= dev_get_by_index(net
, ifindex
);
681 in6_dev
= in6_dev_get(dev
);
684 devconf
= &in6_dev
->cnf
;
689 skb
= nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL
), GFP_KERNEL
);
693 err
= inet6_netconf_fill_devconf(skb
, ifindex
, devconf
,
694 NETLINK_CB(in_skb
).portid
,
695 nlh
->nlmsg_seq
, RTM_NEWNETCONF
, 0,
698 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
699 WARN_ON(err
== -EMSGSIZE
);
703 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
706 in6_dev_put(in6_dev
);
711 static int inet6_netconf_dump_devconf(struct sk_buff
*skb
,
712 struct netlink_callback
*cb
)
714 const struct nlmsghdr
*nlh
= cb
->nlh
;
715 struct net
*net
= sock_net(skb
->sk
);
718 struct net_device
*dev
;
719 struct inet6_dev
*idev
;
720 struct hlist_head
*head
;
722 if (cb
->strict_check
) {
723 struct netlink_ext_ack
*extack
= cb
->extack
;
724 struct netconfmsg
*ncm
;
726 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ncm
))) {
727 NL_SET_ERR_MSG_MOD(extack
, "Invalid header for netconf dump request");
731 if (nlmsg_attrlen(nlh
, sizeof(*ncm
))) {
732 NL_SET_ERR_MSG_MOD(extack
, "Invalid data after header in netconf dump request");
738 s_idx
= idx
= cb
->args
[1];
740 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
742 head
= &net
->dev_index_head
[h
];
744 cb
->seq
= atomic_read(&net
->ipv6
.dev_addr_genid
) ^
746 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
749 idev
= __in6_dev_get(dev
);
753 if (inet6_netconf_fill_devconf(skb
, dev
->ifindex
,
755 NETLINK_CB(cb
->skb
).portid
,
763 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
769 if (h
== NETDEV_HASHENTRIES
) {
770 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_ALL
,
771 net
->ipv6
.devconf_all
,
772 NETLINK_CB(cb
->skb
).portid
,
774 RTM_NEWNETCONF
, NLM_F_MULTI
,
780 if (h
== NETDEV_HASHENTRIES
+ 1) {
781 if (inet6_netconf_fill_devconf(skb
, NETCONFA_IFINDEX_DEFAULT
,
782 net
->ipv6
.devconf_dflt
,
783 NETLINK_CB(cb
->skb
).portid
,
785 RTM_NEWNETCONF
, NLM_F_MULTI
,
799 static void dev_forward_change(struct inet6_dev
*idev
)
801 struct net_device
*dev
;
802 struct inet6_ifaddr
*ifa
;
803 LIST_HEAD(tmp_addr_list
);
808 if (idev
->cnf
.forwarding
)
809 dev_disable_lro(dev
);
810 if (dev
->flags
& IFF_MULTICAST
) {
811 if (idev
->cnf
.forwarding
) {
812 ipv6_dev_mc_inc(dev
, &in6addr_linklocal_allrouters
);
813 ipv6_dev_mc_inc(dev
, &in6addr_interfacelocal_allrouters
);
814 ipv6_dev_mc_inc(dev
, &in6addr_sitelocal_allrouters
);
816 ipv6_dev_mc_dec(dev
, &in6addr_linklocal_allrouters
);
817 ipv6_dev_mc_dec(dev
, &in6addr_interfacelocal_allrouters
);
818 ipv6_dev_mc_dec(dev
, &in6addr_sitelocal_allrouters
);
822 read_lock_bh(&idev
->lock
);
823 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
824 if (ifa
->flags
&IFA_F_TENTATIVE
)
826 list_add_tail(&ifa
->if_list_aux
, &tmp_addr_list
);
828 read_unlock_bh(&idev
->lock
);
830 while (!list_empty(&tmp_addr_list
)) {
831 ifa
= list_first_entry(&tmp_addr_list
,
832 struct inet6_ifaddr
, if_list_aux
);
833 list_del(&ifa
->if_list_aux
);
834 if (idev
->cnf
.forwarding
)
835 addrconf_join_anycast(ifa
);
837 addrconf_leave_anycast(ifa
);
840 inet6_netconf_notify_devconf(dev_net(dev
), RTM_NEWNETCONF
,
842 dev
->ifindex
, &idev
->cnf
);
846 static void addrconf_forward_change(struct net
*net
, __s32 newf
)
848 struct net_device
*dev
;
849 struct inet6_dev
*idev
;
851 for_each_netdev(net
, dev
) {
852 idev
= __in6_dev_get(dev
);
854 int changed
= (!idev
->cnf
.forwarding
) ^ (!newf
);
855 idev
->cnf
.forwarding
= newf
;
857 dev_forward_change(idev
);
862 static int addrconf_fixup_forwarding(struct ctl_table
*table
, int *p
, int newf
)
868 return restart_syscall();
870 net
= (struct net
*)table
->extra2
;
874 if (p
== &net
->ipv6
.devconf_dflt
->forwarding
) {
875 if ((!newf
) ^ (!old
))
876 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
878 NETCONFA_IFINDEX_DEFAULT
,
879 net
->ipv6
.devconf_dflt
);
884 if (p
== &net
->ipv6
.devconf_all
->forwarding
) {
885 int old_dflt
= net
->ipv6
.devconf_dflt
->forwarding
;
887 net
->ipv6
.devconf_dflt
->forwarding
= newf
;
888 if ((!newf
) ^ (!old_dflt
))
889 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
891 NETCONFA_IFINDEX_DEFAULT
,
892 net
->ipv6
.devconf_dflt
);
894 addrconf_forward_change(net
, newf
);
895 if ((!newf
) ^ (!old
))
896 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
898 NETCONFA_IFINDEX_ALL
,
899 net
->ipv6
.devconf_all
);
900 } else if ((!newf
) ^ (!old
))
901 dev_forward_change((struct inet6_dev
*)table
->extra1
);
905 rt6_purge_dflt_routers(net
);
909 static void addrconf_linkdown_change(struct net
*net
, __s32 newf
)
911 struct net_device
*dev
;
912 struct inet6_dev
*idev
;
914 for_each_netdev(net
, dev
) {
915 idev
= __in6_dev_get(dev
);
917 int changed
= (!idev
->cnf
.ignore_routes_with_linkdown
) ^ (!newf
);
919 idev
->cnf
.ignore_routes_with_linkdown
= newf
;
921 inet6_netconf_notify_devconf(dev_net(dev
),
923 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
930 static int addrconf_fixup_linkdown(struct ctl_table
*table
, int *p
, int newf
)
936 return restart_syscall();
938 net
= (struct net
*)table
->extra2
;
942 if (p
== &net
->ipv6
.devconf_dflt
->ignore_routes_with_linkdown
) {
943 if ((!newf
) ^ (!old
))
944 inet6_netconf_notify_devconf(net
,
946 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
947 NETCONFA_IFINDEX_DEFAULT
,
948 net
->ipv6
.devconf_dflt
);
953 if (p
== &net
->ipv6
.devconf_all
->ignore_routes_with_linkdown
) {
954 net
->ipv6
.devconf_dflt
->ignore_routes_with_linkdown
= newf
;
955 addrconf_linkdown_change(net
, newf
);
956 if ((!newf
) ^ (!old
))
957 inet6_netconf_notify_devconf(net
,
959 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN
,
960 NETCONFA_IFINDEX_ALL
,
961 net
->ipv6
.devconf_all
);
970 /* Nobody refers to this ifaddr, destroy it */
971 void inet6_ifa_finish_destroy(struct inet6_ifaddr
*ifp
)
973 WARN_ON(!hlist_unhashed(&ifp
->addr_lst
));
975 #ifdef NET_REFCNT_DEBUG
976 pr_debug("%s\n", __func__
);
979 in6_dev_put(ifp
->idev
);
981 if (cancel_delayed_work(&ifp
->dad_work
))
982 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
985 if (ifp
->state
!= INET6_IFADDR_STATE_DEAD
) {
986 pr_warn("Freeing alive inet6 address %p\n", ifp
);
994 ipv6_link_dev_addr(struct inet6_dev
*idev
, struct inet6_ifaddr
*ifp
)
997 int ifp_scope
= ipv6_addr_src_scope(&ifp
->addr
);
1000 * Each device address list is sorted in order of scope -
1001 * global before linklocal.
1003 list_for_each(p
, &idev
->addr_list
) {
1004 struct inet6_ifaddr
*ifa
1005 = list_entry(p
, struct inet6_ifaddr
, if_list
);
1006 if (ifp_scope
>= ipv6_addr_src_scope(&ifa
->addr
))
1010 list_add_tail_rcu(&ifp
->if_list
, p
);
1013 static u32
inet6_addr_hash(const struct net
*net
, const struct in6_addr
*addr
)
1015 u32 val
= ipv6_addr_hash(addr
) ^ net_hash_mix(net
);
1017 return hash_32(val
, IN6_ADDR_HSIZE_SHIFT
);
1020 static bool ipv6_chk_same_addr(struct net
*net
, const struct in6_addr
*addr
,
1021 struct net_device
*dev
, unsigned int hash
)
1023 struct inet6_ifaddr
*ifp
;
1025 hlist_for_each_entry(ifp
, &net
->ipv6
.inet6_addr_lst
[hash
], addr_lst
) {
1026 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
1027 if (!dev
|| ifp
->idev
->dev
== dev
)
1034 static int ipv6_add_addr_hash(struct net_device
*dev
, struct inet6_ifaddr
*ifa
)
1036 struct net
*net
= dev_net(dev
);
1037 unsigned int hash
= inet6_addr_hash(net
, &ifa
->addr
);
1040 spin_lock_bh(&net
->ipv6
.addrconf_hash_lock
);
1042 /* Ignore adding duplicate addresses on an interface */
1043 if (ipv6_chk_same_addr(net
, &ifa
->addr
, dev
, hash
)) {
1044 netdev_dbg(dev
, "ipv6_add_addr: already assigned\n");
1047 hlist_add_head_rcu(&ifa
->addr_lst
, &net
->ipv6
.inet6_addr_lst
[hash
]);
1050 spin_unlock_bh(&net
->ipv6
.addrconf_hash_lock
);
1055 /* On success it returns ifp with increased reference count */
1057 static struct inet6_ifaddr
*
1058 ipv6_add_addr(struct inet6_dev
*idev
, struct ifa6_config
*cfg
,
1059 bool can_block
, struct netlink_ext_ack
*extack
)
1061 gfp_t gfp_flags
= can_block
? GFP_KERNEL
: GFP_ATOMIC
;
1062 int addr_type
= ipv6_addr_type(cfg
->pfx
);
1063 struct net
*net
= dev_net(idev
->dev
);
1064 struct inet6_ifaddr
*ifa
= NULL
;
1065 struct fib6_info
*f6i
= NULL
;
1068 if (addr_type
== IPV6_ADDR_ANY
) {
1069 NL_SET_ERR_MSG_MOD(extack
, "Invalid address");
1070 return ERR_PTR(-EADDRNOTAVAIL
);
1071 } else if (addr_type
& IPV6_ADDR_MULTICAST
&&
1072 !(cfg
->ifa_flags
& IFA_F_MCAUTOJOIN
)) {
1073 NL_SET_ERR_MSG_MOD(extack
, "Cannot assign multicast address without \"IFA_F_MCAUTOJOIN\" flag");
1074 return ERR_PTR(-EADDRNOTAVAIL
);
1075 } else if (!(idev
->dev
->flags
& IFF_LOOPBACK
) &&
1076 !netif_is_l3_master(idev
->dev
) &&
1077 addr_type
& IPV6_ADDR_LOOPBACK
) {
1078 NL_SET_ERR_MSG_MOD(extack
, "Cannot assign loopback address on this device");
1079 return ERR_PTR(-EADDRNOTAVAIL
);
1083 NL_SET_ERR_MSG_MOD(extack
, "device is going away");
1088 if (idev
->cnf
.disable_ipv6
) {
1089 NL_SET_ERR_MSG_MOD(extack
, "IPv6 is disabled on this device");
1094 /* validator notifier needs to be blocking;
1095 * do not call in atomic context
1098 struct in6_validator_info i6vi
= {
1099 .i6vi_addr
= *cfg
->pfx
,
1104 err
= inet6addr_validator_notifier_call_chain(NETDEV_UP
, &i6vi
);
1105 err
= notifier_to_errno(err
);
1110 ifa
= kzalloc(sizeof(*ifa
), gfp_flags
| __GFP_ACCOUNT
);
1116 f6i
= addrconf_f6i_alloc(net
, idev
, cfg
->pfx
, false, gfp_flags
, extack
);
1123 neigh_parms_data_state_setall(idev
->nd_parms
);
1125 ifa
->addr
= *cfg
->pfx
;
1127 ifa
->peer_addr
= *cfg
->peer_pfx
;
1129 spin_lock_init(&ifa
->lock
);
1130 INIT_DELAYED_WORK(&ifa
->dad_work
, addrconf_dad_work
);
1131 INIT_HLIST_NODE(&ifa
->addr_lst
);
1132 ifa
->scope
= cfg
->scope
;
1133 ifa
->prefix_len
= cfg
->plen
;
1134 ifa
->rt_priority
= cfg
->rt_priority
;
1135 ifa
->flags
= cfg
->ifa_flags
;
1136 ifa
->ifa_proto
= cfg
->ifa_proto
;
1137 /* No need to add the TENTATIVE flag for addresses with NODAD */
1138 if (!(cfg
->ifa_flags
& IFA_F_NODAD
))
1139 ifa
->flags
|= IFA_F_TENTATIVE
;
1140 ifa
->valid_lft
= cfg
->valid_lft
;
1141 ifa
->prefered_lft
= cfg
->preferred_lft
;
1142 ifa
->cstamp
= ifa
->tstamp
= jiffies
;
1143 ifa
->tokenized
= false;
1151 refcount_set(&ifa
->refcnt
, 1);
1155 err
= ipv6_add_addr_hash(idev
->dev
, ifa
);
1161 write_lock_bh(&idev
->lock
);
1163 /* Add to inet6_dev unicast addr list. */
1164 ipv6_link_dev_addr(idev
, ifa
);
1166 if (ifa
->flags
&IFA_F_TEMPORARY
) {
1167 list_add(&ifa
->tmp_list
, &idev
->tempaddr_list
);
1172 write_unlock_bh(&idev
->lock
);
1176 inet6addr_notifier_call_chain(NETDEV_UP
, ifa
);
1178 if (unlikely(err
< 0)) {
1179 fib6_info_release(f6i
);
1183 in6_dev_put(ifa
->idev
);
1192 enum cleanup_prefix_rt_t
{
1193 CLEANUP_PREFIX_RT_NOP
, /* no cleanup action for prefix route */
1194 CLEANUP_PREFIX_RT_DEL
, /* delete the prefix route */
1195 CLEANUP_PREFIX_RT_EXPIRE
, /* update the lifetime of the prefix route */
1199 * Check, whether the prefix for ifp would still need a prefix route
1200 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1203 * 1) we don't purge prefix if address was not permanent.
1204 * prefix is managed by its own lifetime.
1205 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1206 * 3) if there are no addresses, delete prefix.
1207 * 4) if there are still other permanent address(es),
1208 * corresponding prefix is still permanent.
1209 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1210 * don't purge the prefix, assume user space is managing it.
1211 * 6) otherwise, update prefix lifetime to the
1212 * longest valid lifetime among the corresponding
1213 * addresses on the device.
1214 * Note: subsequent RA will update lifetime.
1216 static enum cleanup_prefix_rt_t
1217 check_cleanup_prefix_route(struct inet6_ifaddr
*ifp
, unsigned long *expires
)
1219 struct inet6_ifaddr
*ifa
;
1220 struct inet6_dev
*idev
= ifp
->idev
;
1221 unsigned long lifetime
;
1222 enum cleanup_prefix_rt_t action
= CLEANUP_PREFIX_RT_DEL
;
1226 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
1229 if (ifa
->prefix_len
!= ifp
->prefix_len
||
1230 !ipv6_prefix_equal(&ifa
->addr
, &ifp
->addr
,
1233 if (ifa
->flags
& (IFA_F_PERMANENT
| IFA_F_NOPREFIXROUTE
))
1234 return CLEANUP_PREFIX_RT_NOP
;
1236 action
= CLEANUP_PREFIX_RT_EXPIRE
;
1238 spin_lock(&ifa
->lock
);
1240 lifetime
= addrconf_timeout_fixup(ifa
->valid_lft
, HZ
);
1242 * Note: Because this address is
1243 * not permanent, lifetime <
1244 * LONG_MAX / HZ here.
1246 if (time_before(*expires
, ifa
->tstamp
+ lifetime
* HZ
))
1247 *expires
= ifa
->tstamp
+ lifetime
* HZ
;
1248 spin_unlock(&ifa
->lock
);
1255 cleanup_prefix_route(struct inet6_ifaddr
*ifp
, unsigned long expires
,
1256 bool del_rt
, bool del_peer
)
1258 struct fib6_info
*f6i
;
1260 f6i
= addrconf_get_prefix_route(del_peer
? &ifp
->peer_addr
: &ifp
->addr
,
1262 ifp
->idev
->dev
, 0, RTF_DEFAULT
, true);
1265 ip6_del_rt(dev_net(ifp
->idev
->dev
), f6i
, false);
1267 if (!(f6i
->fib6_flags
& RTF_EXPIRES
))
1268 fib6_set_expires(f6i
, expires
);
1269 fib6_info_release(f6i
);
1275 /* This function wants to get referenced ifp and releases it before return */
1277 static void ipv6_del_addr(struct inet6_ifaddr
*ifp
)
1279 enum cleanup_prefix_rt_t action
= CLEANUP_PREFIX_RT_NOP
;
1280 struct net
*net
= dev_net(ifp
->idev
->dev
);
1281 unsigned long expires
;
1286 spin_lock_bh(&ifp
->lock
);
1288 ifp
->state
= INET6_IFADDR_STATE_DEAD
;
1289 spin_unlock_bh(&ifp
->lock
);
1291 if (state
== INET6_IFADDR_STATE_DEAD
)
1294 spin_lock_bh(&net
->ipv6
.addrconf_hash_lock
);
1295 hlist_del_init_rcu(&ifp
->addr_lst
);
1296 spin_unlock_bh(&net
->ipv6
.addrconf_hash_lock
);
1298 write_lock_bh(&ifp
->idev
->lock
);
1300 if (ifp
->flags
&IFA_F_TEMPORARY
) {
1301 list_del(&ifp
->tmp_list
);
1303 in6_ifa_put(ifp
->ifpub
);
1309 if (ifp
->flags
& IFA_F_PERMANENT
&& !(ifp
->flags
& IFA_F_NOPREFIXROUTE
))
1310 action
= check_cleanup_prefix_route(ifp
, &expires
);
1312 list_del_rcu(&ifp
->if_list
);
1315 write_unlock_bh(&ifp
->idev
->lock
);
1317 addrconf_del_dad_work(ifp
);
1319 ipv6_ifa_notify(RTM_DELADDR
, ifp
);
1321 inet6addr_notifier_call_chain(NETDEV_DOWN
, ifp
);
1323 if (action
!= CLEANUP_PREFIX_RT_NOP
) {
1324 cleanup_prefix_route(ifp
, expires
,
1325 action
== CLEANUP_PREFIX_RT_DEL
, false);
1328 /* clean up prefsrc entries */
1329 rt6_remove_prefsrc(ifp
);
1334 static int ipv6_create_tempaddr(struct inet6_ifaddr
*ifp
, bool block
)
1336 struct inet6_dev
*idev
= ifp
->idev
;
1337 unsigned long tmp_tstamp
, age
;
1338 unsigned long regen_advance
;
1339 unsigned long now
= jiffies
;
1340 s32 cnf_temp_preferred_lft
;
1341 struct inet6_ifaddr
*ift
;
1342 struct ifa6_config cfg
;
1343 long max_desync_factor
;
1344 struct in6_addr addr
;
1347 write_lock_bh(&idev
->lock
);
1351 if (idev
->cnf
.use_tempaddr
<= 0) {
1352 write_unlock_bh(&idev
->lock
);
1353 pr_info("%s: use_tempaddr is disabled\n", __func__
);
1358 spin_lock_bh(&ifp
->lock
);
1359 if (ifp
->regen_count
++ >= idev
->cnf
.regen_max_retry
) {
1360 idev
->cnf
.use_tempaddr
= -1; /*XXX*/
1361 spin_unlock_bh(&ifp
->lock
);
1362 write_unlock_bh(&idev
->lock
);
1363 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1370 memcpy(addr
.s6_addr
, ifp
->addr
.s6_addr
, 8);
1371 ipv6_gen_rnd_iid(&addr
);
1373 age
= (now
- ifp
->tstamp
) / HZ
;
1375 regen_advance
= idev
->cnf
.regen_max_retry
*
1376 idev
->cnf
.dad_transmits
*
1377 max(NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
), HZ
/100) / HZ
;
1379 /* recalculate max_desync_factor each time and update
1380 * idev->desync_factor if it's larger
1382 cnf_temp_preferred_lft
= READ_ONCE(idev
->cnf
.temp_prefered_lft
);
1383 max_desync_factor
= min_t(long,
1384 idev
->cnf
.max_desync_factor
,
1385 cnf_temp_preferred_lft
- regen_advance
);
1387 if (unlikely(idev
->desync_factor
> max_desync_factor
)) {
1388 if (max_desync_factor
> 0) {
1389 get_random_bytes(&idev
->desync_factor
,
1390 sizeof(idev
->desync_factor
));
1391 idev
->desync_factor
%= max_desync_factor
;
1393 idev
->desync_factor
= 0;
1397 memset(&cfg
, 0, sizeof(cfg
));
1398 cfg
.valid_lft
= min_t(__u32
, ifp
->valid_lft
,
1399 idev
->cnf
.temp_valid_lft
+ age
);
1400 cfg
.preferred_lft
= cnf_temp_preferred_lft
+ age
- idev
->desync_factor
;
1401 cfg
.preferred_lft
= min_t(__u32
, ifp
->prefered_lft
, cfg
.preferred_lft
);
1402 cfg
.preferred_lft
= min_t(__u32
, cfg
.valid_lft
, cfg
.preferred_lft
);
1404 cfg
.plen
= ifp
->prefix_len
;
1405 tmp_tstamp
= ifp
->tstamp
;
1406 spin_unlock_bh(&ifp
->lock
);
1408 write_unlock_bh(&idev
->lock
);
1412 * A temporary address is created only if this calculated Preferred
1413 * Lifetime is greater than REGEN_ADVANCE time units. In
1414 * particular, an implementation must not create a temporary address
1415 * with a zero Preferred Lifetime.
1417 * Clamp the preferred lifetime to a minimum of regen_advance, unless
1418 * that would exceed valid_lft.
1420 * Use age calculation as in addrconf_verify to avoid unnecessary
1421 * temporary addresses being generated.
1423 age
= (now
- tmp_tstamp
+ ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
1424 if (cfg
.preferred_lft
<= regen_advance
+ age
)
1425 cfg
.preferred_lft
= regen_advance
+ age
+ 1;
1426 if (cfg
.preferred_lft
> cfg
.valid_lft
) {
1433 cfg
.ifa_flags
= IFA_F_TEMPORARY
;
1434 /* set in addrconf_prefix_rcv() */
1435 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
1436 cfg
.ifa_flags
|= IFA_F_OPTIMISTIC
;
1439 cfg
.scope
= ipv6_addr_scope(cfg
.pfx
);
1441 ift
= ipv6_add_addr(idev
, &cfg
, block
, NULL
);
1445 pr_info("%s: retry temporary address regeneration\n", __func__
);
1446 write_lock_bh(&idev
->lock
);
1450 spin_lock_bh(&ift
->lock
);
1453 ift
->tstamp
= tmp_tstamp
;
1454 spin_unlock_bh(&ift
->lock
);
1456 addrconf_dad_start(ift
);
1464 * Choose an appropriate source address (RFC3484)
1467 IPV6_SADDR_RULE_INIT
= 0,
1468 IPV6_SADDR_RULE_LOCAL
,
1469 IPV6_SADDR_RULE_SCOPE
,
1470 IPV6_SADDR_RULE_PREFERRED
,
1471 #ifdef CONFIG_IPV6_MIP6
1472 IPV6_SADDR_RULE_HOA
,
1474 IPV6_SADDR_RULE_OIF
,
1475 IPV6_SADDR_RULE_LABEL
,
1476 IPV6_SADDR_RULE_PRIVACY
,
1477 IPV6_SADDR_RULE_ORCHID
,
1478 IPV6_SADDR_RULE_PREFIX
,
1479 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1480 IPV6_SADDR_RULE_NOT_OPTIMISTIC
,
1485 struct ipv6_saddr_score
{
1488 struct inet6_ifaddr
*ifa
;
1489 DECLARE_BITMAP(scorebits
, IPV6_SADDR_RULE_MAX
);
1494 struct ipv6_saddr_dst
{
1495 const struct in6_addr
*addr
;
1502 static inline int ipv6_saddr_preferred(int type
)
1504 if (type
& (IPV6_ADDR_MAPPED
|IPV6_ADDR_COMPATv4
|IPV6_ADDR_LOOPBACK
))
1509 static bool ipv6_use_optimistic_addr(struct net
*net
,
1510 struct inet6_dev
*idev
)
1512 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1515 if (!net
->ipv6
.devconf_all
->optimistic_dad
&& !idev
->cnf
.optimistic_dad
)
1517 if (!net
->ipv6
.devconf_all
->use_optimistic
&& !idev
->cnf
.use_optimistic
)
1526 static bool ipv6_allow_optimistic_dad(struct net
*net
,
1527 struct inet6_dev
*idev
)
1529 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1532 if (!net
->ipv6
.devconf_all
->optimistic_dad
&& !idev
->cnf
.optimistic_dad
)
1541 static int ipv6_get_saddr_eval(struct net
*net
,
1542 struct ipv6_saddr_score
*score
,
1543 struct ipv6_saddr_dst
*dst
,
1548 if (i
<= score
->rule
) {
1550 case IPV6_SADDR_RULE_SCOPE
:
1551 ret
= score
->scopedist
;
1553 case IPV6_SADDR_RULE_PREFIX
:
1554 ret
= score
->matchlen
;
1557 ret
= !!test_bit(i
, score
->scorebits
);
1563 case IPV6_SADDR_RULE_INIT
:
1564 /* Rule 0: remember if hiscore is not ready yet */
1567 case IPV6_SADDR_RULE_LOCAL
:
1568 /* Rule 1: Prefer same address */
1569 ret
= ipv6_addr_equal(&score
->ifa
->addr
, dst
->addr
);
1571 case IPV6_SADDR_RULE_SCOPE
:
1572 /* Rule 2: Prefer appropriate scope
1577 * ---+--+-+---> scope
1579 * | d is scope of the destination.
1581 * | \ <- smaller scope is better if
1582 * B-15 | \ if scope is enough for destination.
1583 * | ret = B - scope (-1 <= scope >= d <= 15).
1585 * |/ <- greater is better
1586 * -C / if scope is not enough for destination.
1587 * /| ret = scope - C (-1 <= d < scope <= 15).
1589 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1590 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1591 * Assume B = 0 and we get C > 29.
1593 ret
= __ipv6_addr_src_scope(score
->addr_type
);
1594 if (ret
>= dst
->scope
)
1597 ret
-= 128; /* 30 is enough */
1598 score
->scopedist
= ret
;
1600 case IPV6_SADDR_RULE_PREFERRED
:
1602 /* Rule 3: Avoid deprecated and optimistic addresses */
1603 u8 avoid
= IFA_F_DEPRECATED
;
1605 if (!ipv6_use_optimistic_addr(net
, score
->ifa
->idev
))
1606 avoid
|= IFA_F_OPTIMISTIC
;
1607 ret
= ipv6_saddr_preferred(score
->addr_type
) ||
1608 !(score
->ifa
->flags
& avoid
);
1611 #ifdef CONFIG_IPV6_MIP6
1612 case IPV6_SADDR_RULE_HOA
:
1614 /* Rule 4: Prefer home address */
1615 int prefhome
= !(dst
->prefs
& IPV6_PREFER_SRC_COA
);
1616 ret
= !(score
->ifa
->flags
& IFA_F_HOMEADDRESS
) ^ prefhome
;
1620 case IPV6_SADDR_RULE_OIF
:
1621 /* Rule 5: Prefer outgoing interface */
1622 ret
= (!dst
->ifindex
||
1623 dst
->ifindex
== score
->ifa
->idev
->dev
->ifindex
);
1625 case IPV6_SADDR_RULE_LABEL
:
1626 /* Rule 6: Prefer matching label */
1627 ret
= ipv6_addr_label(net
,
1628 &score
->ifa
->addr
, score
->addr_type
,
1629 score
->ifa
->idev
->dev
->ifindex
) == dst
->label
;
1631 case IPV6_SADDR_RULE_PRIVACY
:
1633 /* Rule 7: Prefer public address
1634 * Note: prefer temporary address if use_tempaddr >= 2
1636 int preftmp
= dst
->prefs
& (IPV6_PREFER_SRC_PUBLIC
|IPV6_PREFER_SRC_TMP
) ?
1637 !!(dst
->prefs
& IPV6_PREFER_SRC_TMP
) :
1638 score
->ifa
->idev
->cnf
.use_tempaddr
>= 2;
1639 ret
= (!(score
->ifa
->flags
& IFA_F_TEMPORARY
)) ^ preftmp
;
1642 case IPV6_SADDR_RULE_ORCHID
:
1643 /* Rule 8-: Prefer ORCHID vs ORCHID or
1644 * non-ORCHID vs non-ORCHID
1646 ret
= !(ipv6_addr_orchid(&score
->ifa
->addr
) ^
1647 ipv6_addr_orchid(dst
->addr
));
1649 case IPV6_SADDR_RULE_PREFIX
:
1650 /* Rule 8: Use longest matching prefix */
1651 ret
= ipv6_addr_diff(&score
->ifa
->addr
, dst
->addr
);
1652 if (ret
> score
->ifa
->prefix_len
)
1653 ret
= score
->ifa
->prefix_len
;
1654 score
->matchlen
= ret
;
1656 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1657 case IPV6_SADDR_RULE_NOT_OPTIMISTIC
:
1658 /* Optimistic addresses still have lower precedence than other
1659 * preferred addresses.
1661 ret
= !(score
->ifa
->flags
& IFA_F_OPTIMISTIC
);
1669 __set_bit(i
, score
->scorebits
);
1675 static int __ipv6_dev_get_saddr(struct net
*net
,
1676 struct ipv6_saddr_dst
*dst
,
1677 struct inet6_dev
*idev
,
1678 struct ipv6_saddr_score
*scores
,
1681 struct ipv6_saddr_score
*score
= &scores
[1 - hiscore_idx
], *hiscore
= &scores
[hiscore_idx
];
1683 list_for_each_entry_rcu(score
->ifa
, &idev
->addr_list
, if_list
) {
1687 * - Tentative Address (RFC2462 section 5.4)
1688 * - A tentative address is not considered
1689 * "assigned to an interface" in the traditional
1690 * sense, unless it is also flagged as optimistic.
1691 * - Candidate Source Address (section 4)
1692 * - In any case, anycast addresses, multicast
1693 * addresses, and the unspecified address MUST
1694 * NOT be included in a candidate set.
1696 if ((score
->ifa
->flags
& IFA_F_TENTATIVE
) &&
1697 (!(score
->ifa
->flags
& IFA_F_OPTIMISTIC
)))
1700 score
->addr_type
= __ipv6_addr_type(&score
->ifa
->addr
);
1702 if (unlikely(score
->addr_type
== IPV6_ADDR_ANY
||
1703 score
->addr_type
& IPV6_ADDR_MULTICAST
)) {
1704 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1710 bitmap_zero(score
->scorebits
, IPV6_SADDR_RULE_MAX
);
1712 for (i
= 0; i
< IPV6_SADDR_RULE_MAX
; i
++) {
1713 int minihiscore
, miniscore
;
1715 minihiscore
= ipv6_get_saddr_eval(net
, hiscore
, dst
, i
);
1716 miniscore
= ipv6_get_saddr_eval(net
, score
, dst
, i
);
1718 if (minihiscore
> miniscore
) {
1719 if (i
== IPV6_SADDR_RULE_SCOPE
&&
1720 score
->scopedist
> 0) {
1723 * each remaining entry
1724 * has too small (not enough)
1725 * scope, because ifa entries
1726 * are sorted by their scope
1732 } else if (minihiscore
< miniscore
) {
1733 swap(hiscore
, score
);
1734 hiscore_idx
= 1 - hiscore_idx
;
1736 /* restore our iterator */
1737 score
->ifa
= hiscore
->ifa
;
1747 static int ipv6_get_saddr_master(struct net
*net
,
1748 const struct net_device
*dst_dev
,
1749 const struct net_device
*master
,
1750 struct ipv6_saddr_dst
*dst
,
1751 struct ipv6_saddr_score
*scores
,
1754 struct inet6_dev
*idev
;
1756 idev
= __in6_dev_get(dst_dev
);
1758 hiscore_idx
= __ipv6_dev_get_saddr(net
, dst
, idev
,
1759 scores
, hiscore_idx
);
1761 idev
= __in6_dev_get(master
);
1763 hiscore_idx
= __ipv6_dev_get_saddr(net
, dst
, idev
,
1764 scores
, hiscore_idx
);
1769 int ipv6_dev_get_saddr(struct net
*net
, const struct net_device
*dst_dev
,
1770 const struct in6_addr
*daddr
, unsigned int prefs
,
1771 struct in6_addr
*saddr
)
1773 struct ipv6_saddr_score scores
[2], *hiscore
;
1774 struct ipv6_saddr_dst dst
;
1775 struct inet6_dev
*idev
;
1776 struct net_device
*dev
;
1778 bool use_oif_addr
= false;
1779 int hiscore_idx
= 0;
1782 dst_type
= __ipv6_addr_type(daddr
);
1784 dst
.ifindex
= dst_dev
? dst_dev
->ifindex
: 0;
1785 dst
.scope
= __ipv6_addr_src_scope(dst_type
);
1786 dst
.label
= ipv6_addr_label(net
, daddr
, dst_type
, dst
.ifindex
);
1789 scores
[hiscore_idx
].rule
= -1;
1790 scores
[hiscore_idx
].ifa
= NULL
;
1794 /* Candidate Source Address (section 4)
1795 * - multicast and link-local destination address,
1796 * the set of candidate source address MUST only
1797 * include addresses assigned to interfaces
1798 * belonging to the same link as the outgoing
1800 * (- For site-local destination addresses, the
1801 * set of candidate source addresses MUST only
1802 * include addresses assigned to interfaces
1803 * belonging to the same site as the outgoing
1805 * - "It is RECOMMENDED that the candidate source addresses
1806 * be the set of unicast addresses assigned to the
1807 * interface that will be used to send to the destination
1808 * (the 'outgoing' interface)." (RFC 6724)
1811 idev
= __in6_dev_get(dst_dev
);
1812 if ((dst_type
& IPV6_ADDR_MULTICAST
) ||
1813 dst
.scope
<= IPV6_ADDR_SCOPE_LINKLOCAL
||
1814 (idev
&& idev
->cnf
.use_oif_addrs_only
)) {
1815 use_oif_addr
= true;
1821 hiscore_idx
= __ipv6_dev_get_saddr(net
, &dst
, idev
, scores
, hiscore_idx
);
1823 const struct net_device
*master
;
1826 /* if dst_dev exists and is enslaved to an L3 device, then
1827 * prefer addresses from dst_dev and then the master over
1828 * any other enslaved devices in the L3 domain.
1830 master
= l3mdev_master_dev_rcu(dst_dev
);
1832 master_idx
= master
->ifindex
;
1834 hiscore_idx
= ipv6_get_saddr_master(net
, dst_dev
,
1836 scores
, hiscore_idx
);
1838 if (scores
[hiscore_idx
].ifa
)
1842 for_each_netdev_rcu(net
, dev
) {
1843 /* only consider addresses on devices in the
1846 if (l3mdev_master_ifindex_rcu(dev
) != master_idx
)
1848 idev
= __in6_dev_get(dev
);
1851 hiscore_idx
= __ipv6_dev_get_saddr(net
, &dst
, idev
, scores
, hiscore_idx
);
1856 hiscore
= &scores
[hiscore_idx
];
1858 ret
= -EADDRNOTAVAIL
;
1860 *saddr
= hiscore
->ifa
->addr
;
1865 EXPORT_SYMBOL(ipv6_dev_get_saddr
);
1867 static int __ipv6_get_lladdr(struct inet6_dev
*idev
, struct in6_addr
*addr
,
1870 struct inet6_ifaddr
*ifp
;
1871 int err
= -EADDRNOTAVAIL
;
1873 list_for_each_entry_reverse(ifp
, &idev
->addr_list
, if_list
) {
1874 if (ifp
->scope
> IFA_LINK
)
1876 if (ifp
->scope
== IFA_LINK
&&
1877 !(ifp
->flags
& banned_flags
)) {
1886 int ipv6_get_lladdr(struct net_device
*dev
, struct in6_addr
*addr
,
1889 struct inet6_dev
*idev
;
1890 int err
= -EADDRNOTAVAIL
;
1893 idev
= __in6_dev_get(dev
);
1895 read_lock_bh(&idev
->lock
);
1896 err
= __ipv6_get_lladdr(idev
, addr
, banned_flags
);
1897 read_unlock_bh(&idev
->lock
);
1903 static int ipv6_count_addresses(const struct inet6_dev
*idev
)
1905 const struct inet6_ifaddr
*ifp
;
1909 list_for_each_entry_rcu(ifp
, &idev
->addr_list
, if_list
)
1915 int ipv6_chk_addr(struct net
*net
, const struct in6_addr
*addr
,
1916 const struct net_device
*dev
, int strict
)
1918 return ipv6_chk_addr_and_flags(net
, addr
, dev
, !dev
,
1919 strict
, IFA_F_TENTATIVE
);
1921 EXPORT_SYMBOL(ipv6_chk_addr
);
1923 /* device argument is used to find the L3 domain of interest. If
1924 * skip_dev_check is set, then the ifp device is not checked against
1925 * the passed in dev argument. So the 2 cases for addresses checks are:
1926 * 1. does the address exist in the L3 domain that dev is part of
1927 * (skip_dev_check = true), or
1929 * 2. does the address exist on the specific device
1930 * (skip_dev_check = false)
1932 static struct net_device
*
1933 __ipv6_chk_addr_and_flags(struct net
*net
, const struct in6_addr
*addr
,
1934 const struct net_device
*dev
, bool skip_dev_check
,
1935 int strict
, u32 banned_flags
)
1937 unsigned int hash
= inet6_addr_hash(net
, addr
);
1938 struct net_device
*l3mdev
, *ndev
;
1939 struct inet6_ifaddr
*ifp
;
1944 l3mdev
= l3mdev_master_dev_rcu(dev
);
1948 hlist_for_each_entry_rcu(ifp
, &net
->ipv6
.inet6_addr_lst
[hash
], addr_lst
) {
1949 ndev
= ifp
->idev
->dev
;
1951 if (l3mdev_master_dev_rcu(ndev
) != l3mdev
)
1954 /* Decouple optimistic from tentative for evaluation here.
1955 * Ban optimistic addresses explicitly, when required.
1957 ifp_flags
= (ifp
->flags
&IFA_F_OPTIMISTIC
)
1958 ? (ifp
->flags
&~IFA_F_TENTATIVE
)
1960 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
1961 !(ifp_flags
&banned_flags
) &&
1962 (!dev
|| ndev
== dev
||
1963 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
))) {
1973 int ipv6_chk_addr_and_flags(struct net
*net
, const struct in6_addr
*addr
,
1974 const struct net_device
*dev
, bool skip_dev_check
,
1975 int strict
, u32 banned_flags
)
1977 return __ipv6_chk_addr_and_flags(net
, addr
, dev
, skip_dev_check
,
1978 strict
, banned_flags
) ? 1 : 0;
1980 EXPORT_SYMBOL(ipv6_chk_addr_and_flags
);
1983 /* Compares an address/prefix_len with addresses on device @dev.
1984 * If one is found it returns true.
1986 bool ipv6_chk_custom_prefix(const struct in6_addr
*addr
,
1987 const unsigned int prefix_len
, struct net_device
*dev
)
1989 const struct inet6_ifaddr
*ifa
;
1990 const struct inet6_dev
*idev
;
1994 idev
= __in6_dev_get(dev
);
1996 list_for_each_entry_rcu(ifa
, &idev
->addr_list
, if_list
) {
1997 ret
= ipv6_prefix_equal(addr
, &ifa
->addr
, prefix_len
);
2006 EXPORT_SYMBOL(ipv6_chk_custom_prefix
);
2008 int ipv6_chk_prefix(const struct in6_addr
*addr
, struct net_device
*dev
)
2010 const struct inet6_ifaddr
*ifa
;
2011 const struct inet6_dev
*idev
;
2016 idev
= __in6_dev_get(dev
);
2018 list_for_each_entry_rcu(ifa
, &idev
->addr_list
, if_list
) {
2019 onlink
= ipv6_prefix_equal(addr
, &ifa
->addr
,
2028 EXPORT_SYMBOL(ipv6_chk_prefix
);
2031 * ipv6_dev_find - find the first device with a given source address.
2032 * @net: the net namespace
2033 * @addr: the source address
2034 * @dev: used to find the L3 domain of interest
2036 * The caller should be protected by RCU, or RTNL.
2038 struct net_device
*ipv6_dev_find(struct net
*net
, const struct in6_addr
*addr
,
2039 struct net_device
*dev
)
2041 return __ipv6_chk_addr_and_flags(net
, addr
, dev
, !dev
, 1,
2044 EXPORT_SYMBOL(ipv6_dev_find
);
2046 struct inet6_ifaddr
*ipv6_get_ifaddr(struct net
*net
, const struct in6_addr
*addr
,
2047 struct net_device
*dev
, int strict
)
2049 unsigned int hash
= inet6_addr_hash(net
, addr
);
2050 struct inet6_ifaddr
*ifp
, *result
= NULL
;
2053 hlist_for_each_entry_rcu(ifp
, &net
->ipv6
.inet6_addr_lst
[hash
], addr_lst
) {
2054 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
2055 if (!dev
|| ifp
->idev
->dev
== dev
||
2056 !(ifp
->scope
&(IFA_LINK
|IFA_HOST
) || strict
)) {
2068 /* Gets referenced address, destroys ifaddr */
2070 static void addrconf_dad_stop(struct inet6_ifaddr
*ifp
, int dad_failed
)
2073 ifp
->flags
|= IFA_F_DADFAILED
;
2075 if (ifp
->flags
&IFA_F_TEMPORARY
) {
2076 struct inet6_ifaddr
*ifpub
;
2077 spin_lock_bh(&ifp
->lock
);
2080 in6_ifa_hold(ifpub
);
2081 spin_unlock_bh(&ifp
->lock
);
2082 ipv6_create_tempaddr(ifpub
, true);
2085 spin_unlock_bh(&ifp
->lock
);
2088 } else if (ifp
->flags
&IFA_F_PERMANENT
|| !dad_failed
) {
2089 spin_lock_bh(&ifp
->lock
);
2090 addrconf_del_dad_work(ifp
);
2091 ifp
->flags
|= IFA_F_TENTATIVE
;
2093 ifp
->flags
&= ~IFA_F_OPTIMISTIC
;
2094 spin_unlock_bh(&ifp
->lock
);
2096 ipv6_ifa_notify(0, ifp
);
2103 static int addrconf_dad_end(struct inet6_ifaddr
*ifp
)
2107 spin_lock_bh(&ifp
->lock
);
2108 if (ifp
->state
== INET6_IFADDR_STATE_DAD
) {
2109 ifp
->state
= INET6_IFADDR_STATE_POSTDAD
;
2112 spin_unlock_bh(&ifp
->lock
);
2117 void addrconf_dad_failure(struct sk_buff
*skb
, struct inet6_ifaddr
*ifp
)
2119 struct inet6_dev
*idev
= ifp
->idev
;
2120 struct net
*net
= dev_net(idev
->dev
);
2122 if (addrconf_dad_end(ifp
)) {
2127 net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2128 ifp
->idev
->dev
->name
, &ifp
->addr
, eth_hdr(skb
)->h_source
);
2130 spin_lock_bh(&ifp
->lock
);
2132 if (ifp
->flags
& IFA_F_STABLE_PRIVACY
) {
2133 struct in6_addr new_addr
;
2134 struct inet6_ifaddr
*ifp2
;
2135 int retries
= ifp
->stable_privacy_retry
+ 1;
2136 struct ifa6_config cfg
= {
2138 .plen
= ifp
->prefix_len
,
2139 .ifa_flags
= ifp
->flags
,
2140 .valid_lft
= ifp
->valid_lft
,
2141 .preferred_lft
= ifp
->prefered_lft
,
2142 .scope
= ifp
->scope
,
2145 if (retries
> net
->ipv6
.sysctl
.idgen_retries
) {
2146 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
2147 ifp
->idev
->dev
->name
);
2151 new_addr
= ifp
->addr
;
2152 if (ipv6_generate_stable_address(&new_addr
, retries
,
2156 spin_unlock_bh(&ifp
->lock
);
2158 if (idev
->cnf
.max_addresses
&&
2159 ipv6_count_addresses(idev
) >=
2160 idev
->cnf
.max_addresses
)
2163 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2164 ifp
->idev
->dev
->name
);
2166 ifp2
= ipv6_add_addr(idev
, &cfg
, false, NULL
);
2170 spin_lock_bh(&ifp2
->lock
);
2171 ifp2
->stable_privacy_retry
= retries
;
2172 ifp2
->state
= INET6_IFADDR_STATE_PREDAD
;
2173 spin_unlock_bh(&ifp2
->lock
);
2175 addrconf_mod_dad_work(ifp2
, net
->ipv6
.sysctl
.idgen_delay
);
2178 spin_lock_bh(&ifp
->lock
);
2182 /* transition from _POSTDAD to _ERRDAD */
2183 ifp
->state
= INET6_IFADDR_STATE_ERRDAD
;
2184 spin_unlock_bh(&ifp
->lock
);
2186 addrconf_mod_dad_work(ifp
, 0);
2190 /* Join to solicited addr multicast group.
2191 * caller must hold RTNL */
2192 void addrconf_join_solict(struct net_device
*dev
, const struct in6_addr
*addr
)
2194 struct in6_addr maddr
;
2196 if (dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
2199 addrconf_addr_solict_mult(addr
, &maddr
);
2200 ipv6_dev_mc_inc(dev
, &maddr
);
2203 /* caller must hold RTNL */
2204 void addrconf_leave_solict(struct inet6_dev
*idev
, const struct in6_addr
*addr
)
2206 struct in6_addr maddr
;
2208 if (idev
->dev
->flags
&(IFF_LOOPBACK
|IFF_NOARP
))
2211 addrconf_addr_solict_mult(addr
, &maddr
);
2212 __ipv6_dev_mc_dec(idev
, &maddr
);
2215 /* caller must hold RTNL */
2216 static void addrconf_join_anycast(struct inet6_ifaddr
*ifp
)
2218 struct in6_addr addr
;
2220 if (ifp
->prefix_len
>= 127) /* RFC 6164 */
2222 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
2223 if (ipv6_addr_any(&addr
))
2225 __ipv6_dev_ac_inc(ifp
->idev
, &addr
);
2228 /* caller must hold RTNL */
2229 static void addrconf_leave_anycast(struct inet6_ifaddr
*ifp
)
2231 struct in6_addr addr
;
2233 if (ifp
->prefix_len
>= 127) /* RFC 6164 */
2235 ipv6_addr_prefix(&addr
, &ifp
->addr
, ifp
->prefix_len
);
2236 if (ipv6_addr_any(&addr
))
2238 __ipv6_dev_ac_dec(ifp
->idev
, &addr
);
2241 static int addrconf_ifid_6lowpan(u8
*eui
, struct net_device
*dev
)
2243 switch (dev
->addr_len
) {
2245 memcpy(eui
, dev
->dev_addr
, 3);
2248 memcpy(eui
+ 5, dev
->dev_addr
+ 3, 3);
2250 case EUI64_ADDR_LEN
:
2251 memcpy(eui
, dev
->dev_addr
, EUI64_ADDR_LEN
);
2261 static int addrconf_ifid_ieee1394(u8
*eui
, struct net_device
*dev
)
2263 const union fwnet_hwaddr
*ha
;
2265 if (dev
->addr_len
!= FWNET_ALEN
)
2268 ha
= (const union fwnet_hwaddr
*)dev
->dev_addr
;
2270 memcpy(eui
, &ha
->uc
.uniq_id
, sizeof(ha
->uc
.uniq_id
));
2275 static int addrconf_ifid_arcnet(u8
*eui
, struct net_device
*dev
)
2277 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2278 if (dev
->addr_len
!= ARCNET_ALEN
)
2281 eui
[7] = *(u8
*)dev
->dev_addr
;
2285 static int addrconf_ifid_infiniband(u8
*eui
, struct net_device
*dev
)
2287 if (dev
->addr_len
!= INFINIBAND_ALEN
)
2289 memcpy(eui
, dev
->dev_addr
+ 12, 8);
2294 static int __ipv6_isatap_ifid(u8
*eui
, __be32 addr
)
2298 eui
[0] = (ipv4_is_zeronet(addr
) || ipv4_is_private_10(addr
) ||
2299 ipv4_is_loopback(addr
) || ipv4_is_linklocal_169(addr
) ||
2300 ipv4_is_private_172(addr
) || ipv4_is_test_192(addr
) ||
2301 ipv4_is_anycast_6to4(addr
) || ipv4_is_private_192(addr
) ||
2302 ipv4_is_test_198(addr
) || ipv4_is_multicast(addr
) ||
2303 ipv4_is_lbcast(addr
)) ? 0x00 : 0x02;
2307 memcpy(eui
+ 4, &addr
, 4);
2311 static int addrconf_ifid_sit(u8
*eui
, struct net_device
*dev
)
2313 if (dev
->priv_flags
& IFF_ISATAP
)
2314 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
2318 static int addrconf_ifid_gre(u8
*eui
, struct net_device
*dev
)
2320 return __ipv6_isatap_ifid(eui
, *(__be32
*)dev
->dev_addr
);
2323 static int addrconf_ifid_ip6tnl(u8
*eui
, struct net_device
*dev
)
2325 memcpy(eui
, dev
->perm_addr
, 3);
2326 memcpy(eui
+ 5, dev
->perm_addr
+ 3, 3);
2333 static int ipv6_generate_eui64(u8
*eui
, struct net_device
*dev
)
2335 switch (dev
->type
) {
2338 return addrconf_ifid_eui48(eui
, dev
);
2340 return addrconf_ifid_arcnet(eui
, dev
);
2341 case ARPHRD_INFINIBAND
:
2342 return addrconf_ifid_infiniband(eui
, dev
);
2344 return addrconf_ifid_sit(eui
, dev
);
2347 return addrconf_ifid_gre(eui
, dev
);
2348 case ARPHRD_6LOWPAN
:
2349 return addrconf_ifid_6lowpan(eui
, dev
);
2350 case ARPHRD_IEEE1394
:
2351 return addrconf_ifid_ieee1394(eui
, dev
);
2352 case ARPHRD_TUNNEL6
:
2355 return addrconf_ifid_ip6tnl(eui
, dev
);
2360 static int ipv6_inherit_eui64(u8
*eui
, struct inet6_dev
*idev
)
2363 struct inet6_ifaddr
*ifp
;
2365 read_lock_bh(&idev
->lock
);
2366 list_for_each_entry_reverse(ifp
, &idev
->addr_list
, if_list
) {
2367 if (ifp
->scope
> IFA_LINK
)
2369 if (ifp
->scope
== IFA_LINK
&& !(ifp
->flags
&IFA_F_TENTATIVE
)) {
2370 memcpy(eui
, ifp
->addr
.s6_addr
+8, 8);
2375 read_unlock_bh(&idev
->lock
);
2379 /* Generation of a randomized Interface Identifier
2380 * draft-ietf-6man-rfc4941bis, Section 3.3.1
2383 static void ipv6_gen_rnd_iid(struct in6_addr
*addr
)
2386 get_random_bytes(&addr
->s6_addr
[8], 8);
2388 /* <draft-ietf-6man-rfc4941bis-08.txt>, Section 3.3.1:
2389 * check if generated address is not inappropriate:
2391 * - Reserved IPv6 Interface Identifiers
2392 * - XXX: already assigned to an address on the device
2395 /* Subnet-router anycast: 0000:0000:0000:0000 */
2396 if (!(addr
->s6_addr32
[2] | addr
->s6_addr32
[3]))
2399 /* IANA Ethernet block: 0200:5EFF:FE00:0000-0200:5EFF:FE00:5212
2400 * Proxy Mobile IPv6: 0200:5EFF:FE00:5213
2401 * IANA Ethernet block: 0200:5EFF:FE00:5214-0200:5EFF:FEFF:FFFF
2403 if (ntohl(addr
->s6_addr32
[2]) == 0x02005eff &&
2404 (ntohl(addr
->s6_addr32
[3]) & 0Xff000000) == 0xfe000000)
2407 /* Reserved subnet anycast addresses */
2408 if (ntohl(addr
->s6_addr32
[2]) == 0xfdffffff &&
2409 ntohl(addr
->s6_addr32
[3]) >= 0Xffffff80)
2418 addrconf_prefix_route(struct in6_addr
*pfx
, int plen
, u32 metric
,
2419 struct net_device
*dev
, unsigned long expires
,
2420 u32 flags
, gfp_t gfp_flags
)
2422 struct fib6_config cfg
= {
2423 .fc_table
= l3mdev_fib_table(dev
) ? : RT6_TABLE_PREFIX
,
2424 .fc_metric
= metric
? : IP6_RT_PRIO_ADDRCONF
,
2425 .fc_ifindex
= dev
->ifindex
,
2426 .fc_expires
= expires
,
2428 .fc_flags
= RTF_UP
| flags
,
2429 .fc_nlinfo
.nl_net
= dev_net(dev
),
2430 .fc_protocol
= RTPROT_KERNEL
,
2431 .fc_type
= RTN_UNICAST
,
2436 /* Prevent useless cloning on PtP SIT.
2437 This thing is done here expecting that the whole
2438 class of non-broadcast devices need not cloning.
2440 #if IS_ENABLED(CONFIG_IPV6_SIT)
2441 if (dev
->type
== ARPHRD_SIT
&& (dev
->flags
& IFF_POINTOPOINT
))
2442 cfg
.fc_flags
|= RTF_NONEXTHOP
;
2445 ip6_route_add(&cfg
, gfp_flags
, NULL
);
2449 static struct fib6_info
*addrconf_get_prefix_route(const struct in6_addr
*pfx
,
2451 const struct net_device
*dev
,
2452 u32 flags
, u32 noflags
,
2455 struct fib6_node
*fn
;
2456 struct fib6_info
*rt
= NULL
;
2457 struct fib6_table
*table
;
2458 u32 tb_id
= l3mdev_fib_table(dev
) ? : RT6_TABLE_PREFIX
;
2460 table
= fib6_get_table(dev_net(dev
), tb_id
);
2465 fn
= fib6_locate(&table
->tb6_root
, pfx
, plen
, NULL
, 0, true);
2469 for_each_fib6_node_rt_rcu(fn
) {
2470 /* prefix routes only use builtin fib6_nh */
2474 if (rt
->fib6_nh
->fib_nh_dev
->ifindex
!= dev
->ifindex
)
2476 if (no_gw
&& rt
->fib6_nh
->fib_nh_gw_family
)
2478 if ((rt
->fib6_flags
& flags
) != flags
)
2480 if ((rt
->fib6_flags
& noflags
) != 0)
2482 if (!fib6_info_hold_safe(rt
))
2492 /* Create "default" multicast route to the interface */
2494 static void addrconf_add_mroute(struct net_device
*dev
)
2496 struct fib6_config cfg
= {
2497 .fc_table
= l3mdev_fib_table(dev
) ? : RT6_TABLE_LOCAL
,
2498 .fc_metric
= IP6_RT_PRIO_ADDRCONF
,
2499 .fc_ifindex
= dev
->ifindex
,
2502 .fc_type
= RTN_MULTICAST
,
2503 .fc_nlinfo
.nl_net
= dev_net(dev
),
2504 .fc_protocol
= RTPROT_KERNEL
,
2507 ipv6_addr_set(&cfg
.fc_dst
, htonl(0xFF000000), 0, 0, 0);
2509 ip6_route_add(&cfg
, GFP_KERNEL
, NULL
);
2512 static struct inet6_dev
*addrconf_add_dev(struct net_device
*dev
)
2514 struct inet6_dev
*idev
;
2518 idev
= ipv6_find_idev(dev
);
2522 if (idev
->cnf
.disable_ipv6
)
2523 return ERR_PTR(-EACCES
);
2525 /* Add default multicast route */
2526 if (!(dev
->flags
& IFF_LOOPBACK
) && !netif_is_l3_master(dev
))
2527 addrconf_add_mroute(dev
);
2532 static void manage_tempaddrs(struct inet6_dev
*idev
,
2533 struct inet6_ifaddr
*ifp
,
2534 __u32 valid_lft
, __u32 prefered_lft
,
2535 bool create
, unsigned long now
)
2538 struct inet6_ifaddr
*ift
;
2540 read_lock_bh(&idev
->lock
);
2541 /* update all temporary addresses in the list */
2542 list_for_each_entry(ift
, &idev
->tempaddr_list
, tmp_list
) {
2543 int age
, max_valid
, max_prefered
;
2545 if (ifp
!= ift
->ifpub
)
2548 /* RFC 4941 section 3.3:
2549 * If a received option will extend the lifetime of a public
2550 * address, the lifetimes of temporary addresses should
2551 * be extended, subject to the overall constraint that no
2552 * temporary addresses should ever remain "valid" or "preferred"
2553 * for a time longer than (TEMP_VALID_LIFETIME) or
2554 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2556 age
= (now
- ift
->cstamp
) / HZ
;
2557 max_valid
= idev
->cnf
.temp_valid_lft
- age
;
2561 max_prefered
= idev
->cnf
.temp_prefered_lft
-
2562 idev
->desync_factor
- age
;
2563 if (max_prefered
< 0)
2566 if (valid_lft
> max_valid
)
2567 valid_lft
= max_valid
;
2569 if (prefered_lft
> max_prefered
)
2570 prefered_lft
= max_prefered
;
2572 spin_lock(&ift
->lock
);
2574 ift
->valid_lft
= valid_lft
;
2575 ift
->prefered_lft
= prefered_lft
;
2577 if (prefered_lft
> 0)
2578 ift
->flags
&= ~IFA_F_DEPRECATED
;
2580 spin_unlock(&ift
->lock
);
2581 if (!(flags
&IFA_F_TENTATIVE
))
2582 ipv6_ifa_notify(0, ift
);
2585 /* Also create a temporary address if it's enabled but no temporary
2586 * address currently exists.
2587 * However, we get called with valid_lft == 0, prefered_lft == 0, create == false
2588 * as part of cleanup (ie. deleting the mngtmpaddr).
2589 * We don't want that to result in creating a new temporary ip address.
2591 if (list_empty(&idev
->tempaddr_list
) && (valid_lft
|| prefered_lft
))
2594 if (create
&& idev
->cnf
.use_tempaddr
> 0) {
2595 /* When a new public address is created as described
2596 * in [ADDRCONF], also create a new temporary address.
2598 read_unlock_bh(&idev
->lock
);
2599 ipv6_create_tempaddr(ifp
, false);
2601 read_unlock_bh(&idev
->lock
);
2605 static bool is_addr_mode_generate_stable(struct inet6_dev
*idev
)
2607 return idev
->cnf
.addr_gen_mode
== IN6_ADDR_GEN_MODE_STABLE_PRIVACY
||
2608 idev
->cnf
.addr_gen_mode
== IN6_ADDR_GEN_MODE_RANDOM
;
2611 int addrconf_prefix_rcv_add_addr(struct net
*net
, struct net_device
*dev
,
2612 const struct prefix_info
*pinfo
,
2613 struct inet6_dev
*in6_dev
,
2614 const struct in6_addr
*addr
, int addr_type
,
2615 u32 addr_flags
, bool sllao
, bool tokenized
,
2616 __u32 valid_lft
, u32 prefered_lft
)
2618 struct inet6_ifaddr
*ifp
= ipv6_get_ifaddr(net
, addr
, dev
, 1);
2619 int create
= 0, update_lft
= 0;
2621 if (!ifp
&& valid_lft
) {
2622 int max_addresses
= in6_dev
->cnf
.max_addresses
;
2623 struct ifa6_config cfg
= {
2625 .plen
= pinfo
->prefix_len
,
2626 .ifa_flags
= addr_flags
,
2627 .valid_lft
= valid_lft
,
2628 .preferred_lft
= prefered_lft
,
2629 .scope
= addr_type
& IPV6_ADDR_SCOPE_MASK
,
2630 .ifa_proto
= IFAPROT_KERNEL_RA
2633 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2634 if ((net
->ipv6
.devconf_all
->optimistic_dad
||
2635 in6_dev
->cnf
.optimistic_dad
) &&
2636 !net
->ipv6
.devconf_all
->forwarding
&& sllao
)
2637 cfg
.ifa_flags
|= IFA_F_OPTIMISTIC
;
2640 /* Do not allow to create too much of autoconfigured
2641 * addresses; this would be too easy way to crash kernel.
2643 if (!max_addresses
||
2644 ipv6_count_addresses(in6_dev
) < max_addresses
)
2645 ifp
= ipv6_add_addr(in6_dev
, &cfg
, false, NULL
);
2647 if (IS_ERR_OR_NULL(ifp
))
2651 spin_lock_bh(&ifp
->lock
);
2652 ifp
->flags
|= IFA_F_MANAGETEMPADDR
;
2653 ifp
->cstamp
= jiffies
;
2654 ifp
->tokenized
= tokenized
;
2655 spin_unlock_bh(&ifp
->lock
);
2656 addrconf_dad_start(ifp
);
2664 /* update lifetime (RFC2462 5.5.3 e) */
2665 spin_lock_bh(&ifp
->lock
);
2667 if (ifp
->valid_lft
> (now
- ifp
->tstamp
) / HZ
)
2668 stored_lft
= ifp
->valid_lft
- (now
- ifp
->tstamp
) / HZ
;
2672 /* RFC4862 Section 5.5.3e:
2673 * "Note that the preferred lifetime of the
2674 * corresponding address is always reset to
2675 * the Preferred Lifetime in the received
2676 * Prefix Information option, regardless of
2677 * whether the valid lifetime is also reset or
2680 * So we should always update prefered_lft here.
2682 update_lft
= !create
&& stored_lft
;
2684 if (update_lft
&& !in6_dev
->cnf
.ra_honor_pio_life
) {
2685 const u32 minimum_lft
= min_t(u32
,
2686 stored_lft
, MIN_VALID_LIFETIME
);
2687 valid_lft
= max(valid_lft
, minimum_lft
);
2691 ifp
->valid_lft
= valid_lft
;
2692 ifp
->prefered_lft
= prefered_lft
;
2695 ifp
->flags
&= ~IFA_F_DEPRECATED
;
2696 spin_unlock_bh(&ifp
->lock
);
2698 if (!(flags
&IFA_F_TENTATIVE
))
2699 ipv6_ifa_notify(0, ifp
);
2701 spin_unlock_bh(&ifp
->lock
);
2703 manage_tempaddrs(in6_dev
, ifp
, valid_lft
, prefered_lft
,
2707 addrconf_verify(net
);
2712 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr
);
2714 void addrconf_prefix_rcv(struct net_device
*dev
, u8
*opt
, int len
, bool sllao
)
2716 struct prefix_info
*pinfo
;
2721 struct inet6_dev
*in6_dev
;
2722 struct net
*net
= dev_net(dev
);
2724 pinfo
= (struct prefix_info
*) opt
;
2726 if (len
< sizeof(struct prefix_info
)) {
2727 netdev_dbg(dev
, "addrconf: prefix option too short\n");
2732 * Validation checks ([ADDRCONF], page 19)
2735 addr_type
= ipv6_addr_type(&pinfo
->prefix
);
2737 if (addr_type
& (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
))
2740 valid_lft
= ntohl(pinfo
->valid
);
2741 prefered_lft
= ntohl(pinfo
->prefered
);
2743 if (prefered_lft
> valid_lft
) {
2744 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2748 in6_dev
= in6_dev_get(dev
);
2751 net_dbg_ratelimited("addrconf: device %s not configured\n",
2756 if (valid_lft
!= 0 && valid_lft
< in6_dev
->cnf
.accept_ra_min_lft
)
2760 * Two things going on here:
2761 * 1) Add routes for on-link prefixes
2762 * 2) Configure prefixes with the auto flag set
2765 if (pinfo
->onlink
) {
2766 struct fib6_info
*rt
;
2767 unsigned long rt_expires
;
2769 /* Avoid arithmetic overflow. Really, we could
2770 * save rt_expires in seconds, likely valid_lft,
2771 * but it would require division in fib gc, that it
2775 rt_expires
= addrconf_timeout_fixup(valid_lft
, HZ
);
2777 rt_expires
= addrconf_timeout_fixup(valid_lft
, USER_HZ
);
2779 if (addrconf_finite_timeout(rt_expires
))
2782 rt
= addrconf_get_prefix_route(&pinfo
->prefix
,
2785 RTF_ADDRCONF
| RTF_PREFIX_RT
,
2789 /* Autoconf prefix route */
2790 if (valid_lft
== 0) {
2791 ip6_del_rt(net
, rt
, false);
2793 } else if (addrconf_finite_timeout(rt_expires
)) {
2795 fib6_set_expires(rt
, jiffies
+ rt_expires
);
2797 fib6_clean_expires(rt
);
2799 } else if (valid_lft
) {
2800 clock_t expires
= 0;
2801 int flags
= RTF_ADDRCONF
| RTF_PREFIX_RT
;
2802 if (addrconf_finite_timeout(rt_expires
)) {
2804 flags
|= RTF_EXPIRES
;
2805 expires
= jiffies_to_clock_t(rt_expires
);
2807 addrconf_prefix_route(&pinfo
->prefix
, pinfo
->prefix_len
,
2808 0, dev
, expires
, flags
,
2811 fib6_info_release(rt
);
2814 /* Try to figure out our local address for this prefix */
2816 if (pinfo
->autoconf
&& in6_dev
->cnf
.autoconf
) {
2817 struct in6_addr addr
;
2818 bool tokenized
= false, dev_addr_generated
= false;
2820 if (pinfo
->prefix_len
== 64) {
2821 memcpy(&addr
, &pinfo
->prefix
, 8);
2823 if (!ipv6_addr_any(&in6_dev
->token
)) {
2824 read_lock_bh(&in6_dev
->lock
);
2825 memcpy(addr
.s6_addr
+ 8,
2826 in6_dev
->token
.s6_addr
+ 8, 8);
2827 read_unlock_bh(&in6_dev
->lock
);
2829 } else if (is_addr_mode_generate_stable(in6_dev
) &&
2830 !ipv6_generate_stable_address(&addr
, 0,
2832 addr_flags
|= IFA_F_STABLE_PRIVACY
;
2834 } else if (ipv6_generate_eui64(addr
.s6_addr
+ 8, dev
) &&
2835 ipv6_inherit_eui64(addr
.s6_addr
+ 8, in6_dev
)) {
2838 dev_addr_generated
= true;
2842 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2847 err
= addrconf_prefix_rcv_add_addr(net
, dev
, pinfo
, in6_dev
,
2850 tokenized
, valid_lft
,
2855 /* Ignore error case here because previous prefix add addr was
2856 * successful which will be notified.
2858 ndisc_ops_prefix_rcv_add_addr(net
, dev
, pinfo
, in6_dev
, &addr
,
2859 addr_type
, addr_flags
, sllao
,
2860 tokenized
, valid_lft
,
2862 dev_addr_generated
);
2864 inet6_prefix_notify(RTM_NEWPREFIX
, in6_dev
, pinfo
);
2866 in6_dev_put(in6_dev
);
2869 static int addrconf_set_sit_dstaddr(struct net
*net
, struct net_device
*dev
,
2870 struct in6_ifreq
*ireq
)
2872 struct ip_tunnel_parm p
= { };
2875 if (!(ipv6_addr_type(&ireq
->ifr6_addr
) & IPV6_ADDR_COMPATv4
))
2876 return -EADDRNOTAVAIL
;
2878 p
.iph
.daddr
= ireq
->ifr6_addr
.s6_addr32
[3];
2881 p
.iph
.protocol
= IPPROTO_IPV6
;
2884 if (!dev
->netdev_ops
->ndo_tunnel_ctl
)
2886 err
= dev
->netdev_ops
->ndo_tunnel_ctl(dev
, &p
, SIOCADDTUNNEL
);
2890 dev
= __dev_get_by_name(net
, p
.name
);
2893 return dev_open(dev
, NULL
);
2897 * Set destination address.
2898 * Special case for SIT interfaces where we create a new "virtual"
2901 int addrconf_set_dstaddr(struct net
*net
, void __user
*arg
)
2903 struct net_device
*dev
;
2904 struct in6_ifreq ireq
;
2907 if (!IS_ENABLED(CONFIG_IPV6_SIT
))
2909 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
2913 dev
= __dev_get_by_index(net
, ireq
.ifr6_ifindex
);
2914 if (dev
&& dev
->type
== ARPHRD_SIT
)
2915 err
= addrconf_set_sit_dstaddr(net
, dev
, &ireq
);
2920 static int ipv6_mc_config(struct sock
*sk
, bool join
,
2921 const struct in6_addr
*addr
, int ifindex
)
2929 ret
= ipv6_sock_mc_join(sk
, ifindex
, addr
);
2931 ret
= ipv6_sock_mc_drop(sk
, ifindex
, addr
);
2938 * Manual configuration of address on an interface
2940 static int inet6_addr_add(struct net
*net
, int ifindex
,
2941 struct ifa6_config
*cfg
,
2942 struct netlink_ext_ack
*extack
)
2944 struct inet6_ifaddr
*ifp
;
2945 struct inet6_dev
*idev
;
2946 struct net_device
*dev
;
2947 unsigned long timeout
;
2953 if (cfg
->plen
> 128) {
2954 NL_SET_ERR_MSG_MOD(extack
, "Invalid prefix length");
2958 /* check the lifetime */
2959 if (!cfg
->valid_lft
|| cfg
->preferred_lft
> cfg
->valid_lft
) {
2960 NL_SET_ERR_MSG_MOD(extack
, "address lifetime invalid");
2964 if (cfg
->ifa_flags
& IFA_F_MANAGETEMPADDR
&& cfg
->plen
!= 64) {
2965 NL_SET_ERR_MSG_MOD(extack
, "address with \"mngtmpaddr\" flag must have a prefix length of 64");
2969 dev
= __dev_get_by_index(net
, ifindex
);
2973 idev
= addrconf_add_dev(dev
);
2975 NL_SET_ERR_MSG_MOD(extack
, "IPv6 is disabled on this device");
2976 return PTR_ERR(idev
);
2979 if (cfg
->ifa_flags
& IFA_F_MCAUTOJOIN
) {
2980 int ret
= ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
,
2981 true, cfg
->pfx
, ifindex
);
2984 NL_SET_ERR_MSG_MOD(extack
, "Multicast auto join failed");
2989 cfg
->scope
= ipv6_addr_scope(cfg
->pfx
);
2991 timeout
= addrconf_timeout_fixup(cfg
->valid_lft
, HZ
);
2992 if (addrconf_finite_timeout(timeout
)) {
2993 expires
= jiffies_to_clock_t(timeout
* HZ
);
2994 cfg
->valid_lft
= timeout
;
2995 flags
= RTF_EXPIRES
;
2999 cfg
->ifa_flags
|= IFA_F_PERMANENT
;
3002 timeout
= addrconf_timeout_fixup(cfg
->preferred_lft
, HZ
);
3003 if (addrconf_finite_timeout(timeout
)) {
3005 cfg
->ifa_flags
|= IFA_F_DEPRECATED
;
3006 cfg
->preferred_lft
= timeout
;
3009 ifp
= ipv6_add_addr(idev
, cfg
, true, extack
);
3011 if (!(cfg
->ifa_flags
& IFA_F_NOPREFIXROUTE
)) {
3012 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
,
3013 ifp
->rt_priority
, dev
, expires
,
3017 /* Send a netlink notification if DAD is enabled and
3018 * optimistic flag is not set
3020 if (!(ifp
->flags
& (IFA_F_OPTIMISTIC
| IFA_F_NODAD
)))
3021 ipv6_ifa_notify(0, ifp
);
3023 * Note that section 3.1 of RFC 4429 indicates
3024 * that the Optimistic flag should not be set for
3025 * manually configured addresses
3027 addrconf_dad_start(ifp
);
3028 if (cfg
->ifa_flags
& IFA_F_MANAGETEMPADDR
)
3029 manage_tempaddrs(idev
, ifp
, cfg
->valid_lft
,
3030 cfg
->preferred_lft
, true, jiffies
);
3032 addrconf_verify_rtnl(net
);
3034 } else if (cfg
->ifa_flags
& IFA_F_MCAUTOJOIN
) {
3035 ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
, false,
3039 return PTR_ERR(ifp
);
3042 static int inet6_addr_del(struct net
*net
, int ifindex
, u32 ifa_flags
,
3043 const struct in6_addr
*pfx
, unsigned int plen
,
3044 struct netlink_ext_ack
*extack
)
3046 struct inet6_ifaddr
*ifp
;
3047 struct inet6_dev
*idev
;
3048 struct net_device
*dev
;
3051 NL_SET_ERR_MSG_MOD(extack
, "Invalid prefix length");
3055 dev
= __dev_get_by_index(net
, ifindex
);
3057 NL_SET_ERR_MSG_MOD(extack
, "Unable to find the interface");
3061 idev
= __in6_dev_get(dev
);
3063 NL_SET_ERR_MSG_MOD(extack
, "IPv6 is disabled on this device");
3067 read_lock_bh(&idev
->lock
);
3068 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
3069 if (ifp
->prefix_len
== plen
&&
3070 ipv6_addr_equal(pfx
, &ifp
->addr
)) {
3072 read_unlock_bh(&idev
->lock
);
3074 if (!(ifp
->flags
& IFA_F_TEMPORARY
) &&
3075 (ifa_flags
& IFA_F_MANAGETEMPADDR
))
3076 manage_tempaddrs(idev
, ifp
, 0, 0, false,
3079 addrconf_verify_rtnl(net
);
3080 if (ipv6_addr_is_multicast(pfx
)) {
3081 ipv6_mc_config(net
->ipv6
.mc_autojoin_sk
,
3082 false, pfx
, dev
->ifindex
);
3087 read_unlock_bh(&idev
->lock
);
3089 NL_SET_ERR_MSG_MOD(extack
, "address not found");
3090 return -EADDRNOTAVAIL
;
3094 int addrconf_add_ifaddr(struct net
*net
, void __user
*arg
)
3096 struct ifa6_config cfg
= {
3097 .ifa_flags
= IFA_F_PERMANENT
,
3098 .preferred_lft
= INFINITY_LIFE_TIME
,
3099 .valid_lft
= INFINITY_LIFE_TIME
,
3101 struct in6_ifreq ireq
;
3104 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
3107 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
3110 cfg
.pfx
= &ireq
.ifr6_addr
;
3111 cfg
.plen
= ireq
.ifr6_prefixlen
;
3114 err
= inet6_addr_add(net
, ireq
.ifr6_ifindex
, &cfg
, NULL
);
3119 int addrconf_del_ifaddr(struct net
*net
, void __user
*arg
)
3121 struct in6_ifreq ireq
;
3124 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
3127 if (copy_from_user(&ireq
, arg
, sizeof(struct in6_ifreq
)))
3131 err
= inet6_addr_del(net
, ireq
.ifr6_ifindex
, 0, &ireq
.ifr6_addr
,
3132 ireq
.ifr6_prefixlen
, NULL
);
3137 static void add_addr(struct inet6_dev
*idev
, const struct in6_addr
*addr
,
3138 int plen
, int scope
, u8 proto
)
3140 struct inet6_ifaddr
*ifp
;
3141 struct ifa6_config cfg
= {
3144 .ifa_flags
= IFA_F_PERMANENT
,
3145 .valid_lft
= INFINITY_LIFE_TIME
,
3146 .preferred_lft
= INFINITY_LIFE_TIME
,
3151 ifp
= ipv6_add_addr(idev
, &cfg
, true, NULL
);
3153 spin_lock_bh(&ifp
->lock
);
3154 ifp
->flags
&= ~IFA_F_TENTATIVE
;
3155 spin_unlock_bh(&ifp
->lock
);
3156 rt_genid_bump_ipv6(dev_net(idev
->dev
));
3157 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
3162 #if IS_ENABLED(CONFIG_IPV6_SIT) || IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3163 static void add_v4_addrs(struct inet6_dev
*idev
)
3165 struct in6_addr addr
;
3166 struct net_device
*dev
;
3167 struct net
*net
= dev_net(idev
->dev
);
3168 int scope
, plen
, offset
= 0;
3173 memset(&addr
, 0, sizeof(struct in6_addr
));
3174 /* in case of IP6GRE the dev_addr is an IPv6 and therefore we use only the last 4 bytes */
3175 if (idev
->dev
->addr_len
== sizeof(struct in6_addr
))
3176 offset
= sizeof(struct in6_addr
) - 4;
3177 memcpy(&addr
.s6_addr32
[3], idev
->dev
->dev_addr
+ offset
, 4);
3179 if (!(idev
->dev
->flags
& IFF_POINTOPOINT
) && idev
->dev
->type
== ARPHRD_SIT
) {
3180 scope
= IPV6_ADDR_COMPATv4
;
3182 pflags
|= RTF_NONEXTHOP
;
3184 if (idev
->cnf
.addr_gen_mode
== IN6_ADDR_GEN_MODE_NONE
)
3187 addr
.s6_addr32
[0] = htonl(0xfe800000);
3192 if (addr
.s6_addr32
[3]) {
3193 add_addr(idev
, &addr
, plen
, scope
, IFAPROT_UNSPEC
);
3194 addrconf_prefix_route(&addr
, plen
, 0, idev
->dev
, 0, pflags
,
3199 for_each_netdev(net
, dev
) {
3200 struct in_device
*in_dev
= __in_dev_get_rtnl(dev
);
3201 if (in_dev
&& (dev
->flags
& IFF_UP
)) {
3202 struct in_ifaddr
*ifa
;
3205 in_dev_for_each_ifa_rtnl(ifa
, in_dev
) {
3206 addr
.s6_addr32
[3] = ifa
->ifa_local
;
3208 if (ifa
->ifa_scope
== RT_SCOPE_LINK
)
3210 if (ifa
->ifa_scope
>= RT_SCOPE_HOST
) {
3211 if (idev
->dev
->flags
&IFF_POINTOPOINT
)
3216 add_addr(idev
, &addr
, plen
, flag
,
3218 addrconf_prefix_route(&addr
, plen
, 0, idev
->dev
,
3219 0, pflags
, GFP_KERNEL
);
3226 static void init_loopback(struct net_device
*dev
)
3228 struct inet6_dev
*idev
;
3234 idev
= ipv6_find_idev(dev
);
3236 pr_debug("%s: add_dev failed\n", __func__
);
3240 add_addr(idev
, &in6addr_loopback
, 128, IFA_HOST
, IFAPROT_KERNEL_LO
);
3243 void addrconf_add_linklocal(struct inet6_dev
*idev
,
3244 const struct in6_addr
*addr
, u32 flags
)
3246 struct ifa6_config cfg
= {
3249 .ifa_flags
= flags
| IFA_F_PERMANENT
,
3250 .valid_lft
= INFINITY_LIFE_TIME
,
3251 .preferred_lft
= INFINITY_LIFE_TIME
,
3253 .ifa_proto
= IFAPROT_KERNEL_LL
3255 struct inet6_ifaddr
*ifp
;
3257 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3258 if ((dev_net(idev
->dev
)->ipv6
.devconf_all
->optimistic_dad
||
3259 idev
->cnf
.optimistic_dad
) &&
3260 !dev_net(idev
->dev
)->ipv6
.devconf_all
->forwarding
)
3261 cfg
.ifa_flags
|= IFA_F_OPTIMISTIC
;
3264 ifp
= ipv6_add_addr(idev
, &cfg
, true, NULL
);
3266 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
, 0, idev
->dev
,
3268 addrconf_dad_start(ifp
);
3272 EXPORT_SYMBOL_GPL(addrconf_add_linklocal
);
3274 static bool ipv6_reserved_interfaceid(struct in6_addr address
)
3276 if ((address
.s6_addr32
[2] | address
.s6_addr32
[3]) == 0)
3279 if (address
.s6_addr32
[2] == htonl(0x02005eff) &&
3280 ((address
.s6_addr32
[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3283 if (address
.s6_addr32
[2] == htonl(0xfdffffff) &&
3284 ((address
.s6_addr32
[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3290 static int ipv6_generate_stable_address(struct in6_addr
*address
,
3292 const struct inet6_dev
*idev
)
3294 static DEFINE_SPINLOCK(lock
);
3295 static __u32 digest
[SHA1_DIGEST_WORDS
];
3296 static __u32 workspace
[SHA1_WORKSPACE_WORDS
];
3299 char __data
[SHA1_BLOCK_SIZE
];
3301 struct in6_addr secret
;
3303 unsigned char hwaddr
[MAX_ADDR_LEN
];
3308 struct in6_addr secret
;
3309 struct in6_addr temp
;
3310 struct net
*net
= dev_net(idev
->dev
);
3312 BUILD_BUG_ON(sizeof(data
.__data
) != sizeof(data
));
3314 if (idev
->cnf
.stable_secret
.initialized
)
3315 secret
= idev
->cnf
.stable_secret
.secret
;
3316 else if (net
->ipv6
.devconf_dflt
->stable_secret
.initialized
)
3317 secret
= net
->ipv6
.devconf_dflt
->stable_secret
.secret
;
3322 spin_lock_bh(&lock
);
3325 memset(&data
, 0, sizeof(data
));
3326 memset(workspace
, 0, sizeof(workspace
));
3327 memcpy(data
.hwaddr
, idev
->dev
->perm_addr
, idev
->dev
->addr_len
);
3328 data
.prefix
[0] = address
->s6_addr32
[0];
3329 data
.prefix
[1] = address
->s6_addr32
[1];
3330 data
.secret
= secret
;
3331 data
.dad_count
= dad_count
;
3333 sha1_transform(digest
, data
.__data
, workspace
);
3336 temp
.s6_addr32
[2] = (__force __be32
)digest
[0];
3337 temp
.s6_addr32
[3] = (__force __be32
)digest
[1];
3339 spin_unlock_bh(&lock
);
3341 if (ipv6_reserved_interfaceid(temp
)) {
3343 if (dad_count
> dev_net(idev
->dev
)->ipv6
.sysctl
.idgen_retries
)
3352 static void ipv6_gen_mode_random_init(struct inet6_dev
*idev
)
3354 struct ipv6_stable_secret
*s
= &idev
->cnf
.stable_secret
;
3358 s
= &idev
->cnf
.stable_secret
;
3359 get_random_bytes(&s
->secret
, sizeof(s
->secret
));
3360 s
->initialized
= true;
3363 static void addrconf_addr_gen(struct inet6_dev
*idev
, bool prefix_route
)
3365 struct in6_addr addr
;
3367 /* no link local addresses on L3 master devices */
3368 if (netif_is_l3_master(idev
->dev
))
3371 /* no link local addresses on devices flagged as slaves */
3372 if (idev
->dev
->priv_flags
& IFF_NO_ADDRCONF
)
3375 ipv6_addr_set(&addr
, htonl(0xFE800000), 0, 0, 0);
3377 switch (idev
->cnf
.addr_gen_mode
) {
3378 case IN6_ADDR_GEN_MODE_RANDOM
:
3379 ipv6_gen_mode_random_init(idev
);
3381 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY
:
3382 if (!ipv6_generate_stable_address(&addr
, 0, idev
))
3383 addrconf_add_linklocal(idev
, &addr
,
3384 IFA_F_STABLE_PRIVACY
);
3385 else if (prefix_route
)
3386 addrconf_prefix_route(&addr
, 64, 0, idev
->dev
,
3389 case IN6_ADDR_GEN_MODE_EUI64
:
3390 /* addrconf_add_linklocal also adds a prefix_route and we
3391 * only need to care about prefix routes if ipv6_generate_eui64
3392 * couldn't generate one.
3394 if (ipv6_generate_eui64(addr
.s6_addr
+ 8, idev
->dev
) == 0)
3395 addrconf_add_linklocal(idev
, &addr
, 0);
3396 else if (prefix_route
)
3397 addrconf_prefix_route(&addr
, 64, 0, idev
->dev
,
3400 case IN6_ADDR_GEN_MODE_NONE
:
3402 /* will not add any link local address */
3407 static void addrconf_dev_config(struct net_device
*dev
)
3409 struct inet6_dev
*idev
;
3413 if ((dev
->type
!= ARPHRD_ETHER
) &&
3414 (dev
->type
!= ARPHRD_FDDI
) &&
3415 (dev
->type
!= ARPHRD_ARCNET
) &&
3416 (dev
->type
!= ARPHRD_INFINIBAND
) &&
3417 (dev
->type
!= ARPHRD_IEEE1394
) &&
3418 (dev
->type
!= ARPHRD_TUNNEL6
) &&
3419 (dev
->type
!= ARPHRD_6LOWPAN
) &&
3420 (dev
->type
!= ARPHRD_TUNNEL
) &&
3421 (dev
->type
!= ARPHRD_NONE
) &&
3422 (dev
->type
!= ARPHRD_RAWIP
)) {
3423 /* Alas, we support only Ethernet autoconfiguration. */
3424 idev
= __in6_dev_get(dev
);
3425 if (!IS_ERR_OR_NULL(idev
) && dev
->flags
& IFF_UP
&&
3426 dev
->flags
& IFF_MULTICAST
)
3431 idev
= addrconf_add_dev(dev
);
3435 /* this device type has no EUI support */
3436 if (dev
->type
== ARPHRD_NONE
&&
3437 idev
->cnf
.addr_gen_mode
== IN6_ADDR_GEN_MODE_EUI64
)
3438 idev
->cnf
.addr_gen_mode
= IN6_ADDR_GEN_MODE_RANDOM
;
3440 addrconf_addr_gen(idev
, false);
3443 #if IS_ENABLED(CONFIG_IPV6_SIT)
3444 static void addrconf_sit_config(struct net_device
*dev
)
3446 struct inet6_dev
*idev
;
3451 * Configure the tunnel with one of our IPv4
3452 * addresses... we should configure all of
3453 * our v4 addrs in the tunnel
3456 idev
= ipv6_find_idev(dev
);
3458 pr_debug("%s: add_dev failed\n", __func__
);
3462 if (dev
->priv_flags
& IFF_ISATAP
) {
3463 addrconf_addr_gen(idev
, false);
3469 if (dev
->flags
&IFF_POINTOPOINT
)
3470 addrconf_add_mroute(dev
);
3474 #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3475 static void addrconf_gre_config(struct net_device
*dev
)
3477 struct inet6_dev
*idev
;
3481 idev
= ipv6_find_idev(dev
);
3483 pr_debug("%s: add_dev failed\n", __func__
);
3487 if (dev
->type
== ARPHRD_ETHER
) {
3488 addrconf_addr_gen(idev
, true);
3494 if (dev
->flags
& IFF_POINTOPOINT
)
3495 addrconf_add_mroute(dev
);
3499 static void addrconf_init_auto_addrs(struct net_device
*dev
)
3501 switch (dev
->type
) {
3502 #if IS_ENABLED(CONFIG_IPV6_SIT)
3504 addrconf_sit_config(dev
);
3507 #if IS_ENABLED(CONFIG_NET_IPGRE) || IS_ENABLED(CONFIG_IPV6_GRE)
3510 addrconf_gre_config(dev
);
3513 case ARPHRD_LOOPBACK
:
3518 addrconf_dev_config(dev
);
3523 static int fixup_permanent_addr(struct net
*net
,
3524 struct inet6_dev
*idev
,
3525 struct inet6_ifaddr
*ifp
)
3527 /* !fib6_node means the host route was removed from the
3528 * FIB, for example, if 'lo' device is taken down. In that
3529 * case regenerate the host route.
3531 if (!ifp
->rt
|| !ifp
->rt
->fib6_node
) {
3532 struct fib6_info
*f6i
, *prev
;
3534 f6i
= addrconf_f6i_alloc(net
, idev
, &ifp
->addr
, false,
3537 return PTR_ERR(f6i
);
3539 /* ifp->rt can be accessed outside of rtnl */
3540 spin_lock(&ifp
->lock
);
3543 spin_unlock(&ifp
->lock
);
3545 fib6_info_release(prev
);
3548 if (!(ifp
->flags
& IFA_F_NOPREFIXROUTE
)) {
3549 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
,
3550 ifp
->rt_priority
, idev
->dev
, 0, 0,
3554 if (ifp
->state
== INET6_IFADDR_STATE_PREDAD
)
3555 addrconf_dad_start(ifp
);
3560 static void addrconf_permanent_addr(struct net
*net
, struct net_device
*dev
)
3562 struct inet6_ifaddr
*ifp
, *tmp
;
3563 struct inet6_dev
*idev
;
3565 idev
= __in6_dev_get(dev
);
3569 write_lock_bh(&idev
->lock
);
3571 list_for_each_entry_safe(ifp
, tmp
, &idev
->addr_list
, if_list
) {
3572 if ((ifp
->flags
& IFA_F_PERMANENT
) &&
3573 fixup_permanent_addr(net
, idev
, ifp
) < 0) {
3574 write_unlock_bh(&idev
->lock
);
3577 write_lock_bh(&idev
->lock
);
3579 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3580 idev
->dev
->name
, &ifp
->addr
);
3584 write_unlock_bh(&idev
->lock
);
3587 static int addrconf_notify(struct notifier_block
*this, unsigned long event
,
3590 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3591 struct netdev_notifier_change_info
*change_info
;
3592 struct netdev_notifier_changeupper_info
*info
;
3593 struct inet6_dev
*idev
= __in6_dev_get(dev
);
3594 struct net
*net
= dev_net(dev
);
3595 int run_pending
= 0;
3599 case NETDEV_REGISTER
:
3600 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
) {
3601 idev
= ipv6_add_dev(dev
);
3603 return notifier_from_errno(PTR_ERR(idev
));
3607 case NETDEV_CHANGEMTU
:
3608 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3609 if (dev
->mtu
< IPV6_MIN_MTU
) {
3610 addrconf_ifdown(dev
, dev
!= net
->loopback_dev
);
3615 rt6_mtu_change(dev
, dev
->mtu
);
3616 idev
->cnf
.mtu6
= dev
->mtu
;
3620 /* allocate new idev */
3621 idev
= ipv6_add_dev(dev
);
3625 /* device is still not ready */
3626 if (!(idev
->if_flags
& IF_READY
))
3633 if (idev
&& idev
->cnf
.disable_ipv6
)
3636 if (dev
->priv_flags
& IFF_NO_ADDRCONF
) {
3637 if (event
== NETDEV_UP
&& !IS_ERR_OR_NULL(idev
) &&
3638 dev
->flags
& IFF_UP
&& dev
->flags
& IFF_MULTICAST
)
3643 if (event
== NETDEV_UP
) {
3644 /* restore routes for permanent addresses */
3645 addrconf_permanent_addr(net
, dev
);
3647 if (!addrconf_link_ready(dev
)) {
3648 /* device is not ready yet. */
3649 pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3654 if (!idev
&& dev
->mtu
>= IPV6_MIN_MTU
)
3655 idev
= ipv6_add_dev(dev
);
3657 if (!IS_ERR_OR_NULL(idev
)) {
3658 idev
->if_flags
|= IF_READY
;
3661 } else if (event
== NETDEV_CHANGE
) {
3662 if (!addrconf_link_ready(dev
)) {
3663 /* device is still not ready. */
3664 rt6_sync_down_dev(dev
, event
);
3668 if (!IS_ERR_OR_NULL(idev
)) {
3669 if (idev
->if_flags
& IF_READY
) {
3670 /* device is already configured -
3671 * but resend MLD reports, we might
3672 * have roamed and need to update
3673 * multicast snooping switches
3677 if (change_info
->flags_changed
& IFF_NOARP
)
3678 addrconf_dad_run(idev
, true);
3679 rt6_sync_up(dev
, RTNH_F_LINKDOWN
);
3682 idev
->if_flags
|= IF_READY
;
3685 pr_debug("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3691 addrconf_init_auto_addrs(dev
);
3693 if (!IS_ERR_OR_NULL(idev
)) {
3695 addrconf_dad_run(idev
, false);
3697 /* Device has an address by now */
3698 rt6_sync_up(dev
, RTNH_F_DEAD
);
3701 * If the MTU changed during the interface down,
3702 * when the interface up, the changed MTU must be
3703 * reflected in the idev as well as routers.
3705 if (idev
->cnf
.mtu6
!= dev
->mtu
&&
3706 dev
->mtu
>= IPV6_MIN_MTU
) {
3707 rt6_mtu_change(dev
, dev
->mtu
);
3708 idev
->cnf
.mtu6
= dev
->mtu
;
3710 idev
->tstamp
= jiffies
;
3711 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
3714 * If the changed mtu during down is lower than
3715 * IPV6_MIN_MTU stop IPv6 on this interface.
3717 if (dev
->mtu
< IPV6_MIN_MTU
)
3718 addrconf_ifdown(dev
, dev
!= net
->loopback_dev
);
3723 case NETDEV_UNREGISTER
:
3725 * Remove all addresses from this interface.
3727 addrconf_ifdown(dev
, event
!= NETDEV_DOWN
);
3730 case NETDEV_CHANGENAME
:
3732 snmp6_unregister_dev(idev
);
3733 addrconf_sysctl_unregister(idev
);
3734 err
= addrconf_sysctl_register(idev
);
3736 return notifier_from_errno(err
);
3737 err
= snmp6_register_dev(idev
);
3739 addrconf_sysctl_unregister(idev
);
3740 return notifier_from_errno(err
);
3745 case NETDEV_PRE_TYPE_CHANGE
:
3746 case NETDEV_POST_TYPE_CHANGE
:
3748 addrconf_type_change(dev
, event
);
3751 case NETDEV_CHANGEUPPER
:
3754 /* flush all routes if dev is linked to or unlinked from
3755 * an L3 master device (e.g., VRF)
3757 if (info
->upper_dev
&& netif_is_l3_master(info
->upper_dev
))
3758 addrconf_ifdown(dev
, false);
3765 * addrconf module should be notified of a device going up
3767 static struct notifier_block ipv6_dev_notf
= {
3768 .notifier_call
= addrconf_notify
,
3769 .priority
= ADDRCONF_NOTIFY_PRIORITY
,
3772 static void addrconf_type_change(struct net_device
*dev
, unsigned long event
)
3774 struct inet6_dev
*idev
;
3777 idev
= __in6_dev_get(dev
);
3779 if (event
== NETDEV_POST_TYPE_CHANGE
)
3780 ipv6_mc_remap(idev
);
3781 else if (event
== NETDEV_PRE_TYPE_CHANGE
)
3782 ipv6_mc_unmap(idev
);
3785 static bool addr_is_local(const struct in6_addr
*addr
)
3787 return ipv6_addr_type(addr
) &
3788 (IPV6_ADDR_LINKLOCAL
| IPV6_ADDR_LOOPBACK
);
3791 static int addrconf_ifdown(struct net_device
*dev
, bool unregister
)
3793 unsigned long event
= unregister
? NETDEV_UNREGISTER
: NETDEV_DOWN
;
3794 struct net
*net
= dev_net(dev
);
3795 struct inet6_dev
*idev
;
3796 struct inet6_ifaddr
*ifa
;
3797 LIST_HEAD(tmp_addr_list
);
3798 bool keep_addr
= false;
3804 rt6_disable_ip(dev
, event
);
3806 idev
= __in6_dev_get(dev
);
3811 * Step 1: remove reference to ipv6 device from parent device.
3817 /* protected by rtnl_lock */
3818 RCU_INIT_POINTER(dev
->ip6_ptr
, NULL
);
3820 /* Step 1.5: remove snmp6 entry */
3821 snmp6_unregister_dev(idev
);
3825 /* combine the user config with event to determine if permanent
3826 * addresses are to be removed from address hash table
3828 if (!unregister
&& !idev
->cnf
.disable_ipv6
) {
3829 /* aggregate the system setting and interface setting */
3830 int _keep_addr
= net
->ipv6
.devconf_all
->keep_addr_on_down
;
3833 _keep_addr
= idev
->cnf
.keep_addr_on_down
;
3835 keep_addr
= (_keep_addr
> 0);
3838 /* Step 2: clear hash table */
3839 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
3840 struct hlist_head
*h
= &net
->ipv6
.inet6_addr_lst
[i
];
3842 spin_lock_bh(&net
->ipv6
.addrconf_hash_lock
);
3844 hlist_for_each_entry_rcu(ifa
, h
, addr_lst
) {
3845 if (ifa
->idev
== idev
) {
3846 addrconf_del_dad_work(ifa
);
3847 /* combined flag + permanent flag decide if
3848 * address is retained on a down event
3851 !(ifa
->flags
& IFA_F_PERMANENT
) ||
3852 addr_is_local(&ifa
->addr
)) {
3853 hlist_del_init_rcu(&ifa
->addr_lst
);
3858 spin_unlock_bh(&net
->ipv6
.addrconf_hash_lock
);
3861 write_lock_bh(&idev
->lock
);
3863 addrconf_del_rs_timer(idev
);
3865 /* Step 2: clear flags for stateless addrconf, repeated down
3868 was_ready
= idev
->if_flags
& IF_READY
;
3870 idev
->if_flags
&= ~(IF_RS_SENT
|IF_RA_RCVD
|IF_READY
);
3872 /* Step 3: clear tempaddr list */
3873 while (!list_empty(&idev
->tempaddr_list
)) {
3874 ifa
= list_first_entry(&idev
->tempaddr_list
,
3875 struct inet6_ifaddr
, tmp_list
);
3876 list_del(&ifa
->tmp_list
);
3877 write_unlock_bh(&idev
->lock
);
3878 spin_lock_bh(&ifa
->lock
);
3881 in6_ifa_put(ifa
->ifpub
);
3884 spin_unlock_bh(&ifa
->lock
);
3886 write_lock_bh(&idev
->lock
);
3889 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
)
3890 list_add_tail(&ifa
->if_list_aux
, &tmp_addr_list
);
3891 write_unlock_bh(&idev
->lock
);
3893 while (!list_empty(&tmp_addr_list
)) {
3894 struct fib6_info
*rt
= NULL
;
3897 ifa
= list_first_entry(&tmp_addr_list
,
3898 struct inet6_ifaddr
, if_list_aux
);
3899 list_del(&ifa
->if_list_aux
);
3901 addrconf_del_dad_work(ifa
);
3903 keep
= keep_addr
&& (ifa
->flags
& IFA_F_PERMANENT
) &&
3904 !addr_is_local(&ifa
->addr
);
3906 spin_lock_bh(&ifa
->lock
);
3909 /* set state to skip the notifier below */
3910 state
= INET6_IFADDR_STATE_DEAD
;
3911 ifa
->state
= INET6_IFADDR_STATE_PREDAD
;
3912 if (!(ifa
->flags
& IFA_F_NODAD
))
3913 ifa
->flags
|= IFA_F_TENTATIVE
;
3919 ifa
->state
= INET6_IFADDR_STATE_DEAD
;
3922 spin_unlock_bh(&ifa
->lock
);
3925 ip6_del_rt(net
, rt
, false);
3927 if (state
!= INET6_IFADDR_STATE_DEAD
) {
3928 __ipv6_ifa_notify(RTM_DELADDR
, ifa
);
3929 inet6addr_notifier_call_chain(NETDEV_DOWN
, ifa
);
3931 if (idev
->cnf
.forwarding
)
3932 addrconf_leave_anycast(ifa
);
3933 addrconf_leave_solict(ifa
->idev
, &ifa
->addr
);
3937 write_lock_bh(&idev
->lock
);
3938 list_del_rcu(&ifa
->if_list
);
3939 write_unlock_bh(&idev
->lock
);
3944 /* Step 5: Discard anycast and multicast list */
3946 ipv6_ac_destroy_dev(idev
);
3947 ipv6_mc_destroy_dev(idev
);
3948 } else if (was_ready
) {
3952 idev
->tstamp
= jiffies
;
3955 /* Last: Shot the device (if unregistered) */
3957 addrconf_sysctl_unregister(idev
);
3958 neigh_parms_release(&nd_tbl
, idev
->nd_parms
);
3959 neigh_ifdown(&nd_tbl
, dev
);
3965 static void addrconf_rs_timer(struct timer_list
*t
)
3967 struct inet6_dev
*idev
= from_timer(idev
, t
, rs_timer
);
3968 struct net_device
*dev
= idev
->dev
;
3969 struct in6_addr lladdr
;
3971 write_lock(&idev
->lock
);
3972 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
))
3975 if (!ipv6_accept_ra(idev
))
3978 /* Announcement received after solicitation was sent */
3979 if (idev
->if_flags
& IF_RA_RCVD
)
3982 if (idev
->rs_probes
++ < idev
->cnf
.rtr_solicits
|| idev
->cnf
.rtr_solicits
< 0) {
3983 write_unlock(&idev
->lock
);
3984 if (!ipv6_get_lladdr(dev
, &lladdr
, IFA_F_TENTATIVE
))
3985 ndisc_send_rs(dev
, &lladdr
,
3986 &in6addr_linklocal_allrouters
);
3990 write_lock(&idev
->lock
);
3991 idev
->rs_interval
= rfc3315_s14_backoff_update(
3992 idev
->rs_interval
, idev
->cnf
.rtr_solicit_max_interval
);
3993 /* The wait after the last probe can be shorter */
3994 addrconf_mod_rs_timer(idev
, (idev
->rs_probes
==
3995 idev
->cnf
.rtr_solicits
) ?
3996 idev
->cnf
.rtr_solicit_delay
:
4000 * Note: we do not support deprecated "all on-link"
4001 * assumption any longer.
4003 pr_debug("%s: no IPv6 routers present\n", idev
->dev
->name
);
4007 write_unlock(&idev
->lock
);
4013 * Duplicate Address Detection
4015 static void addrconf_dad_kick(struct inet6_ifaddr
*ifp
)
4017 unsigned long rand_num
;
4018 struct inet6_dev
*idev
= ifp
->idev
;
4021 if (ifp
->flags
& IFA_F_OPTIMISTIC
)
4024 rand_num
= get_random_u32_below(idev
->cnf
.rtr_solicit_delay
? : 1);
4027 if (idev
->cnf
.enhanced_dad
||
4028 dev_net(idev
->dev
)->ipv6
.devconf_all
->enhanced_dad
) {
4030 get_random_bytes(&nonce
, 6);
4033 ifp
->dad_nonce
= nonce
;
4034 ifp
->dad_probes
= idev
->cnf
.dad_transmits
;
4035 addrconf_mod_dad_work(ifp
, rand_num
);
4038 static void addrconf_dad_begin(struct inet6_ifaddr
*ifp
)
4040 struct inet6_dev
*idev
= ifp
->idev
;
4041 struct net_device
*dev
= idev
->dev
;
4042 bool bump_id
, notify
= false;
4045 addrconf_join_solict(dev
, &ifp
->addr
);
4047 read_lock_bh(&idev
->lock
);
4048 spin_lock(&ifp
->lock
);
4049 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
)
4053 if (dev
->flags
&(IFF_NOARP
|IFF_LOOPBACK
) ||
4054 (net
->ipv6
.devconf_all
->accept_dad
< 1 &&
4055 idev
->cnf
.accept_dad
< 1) ||
4056 !(ifp
->flags
&IFA_F_TENTATIVE
) ||
4057 ifp
->flags
& IFA_F_NODAD
) {
4058 bool send_na
= false;
4060 if (ifp
->flags
& IFA_F_TENTATIVE
&&
4061 !(ifp
->flags
& IFA_F_OPTIMISTIC
))
4063 bump_id
= ifp
->flags
& IFA_F_TENTATIVE
;
4064 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
4065 spin_unlock(&ifp
->lock
);
4066 read_unlock_bh(&idev
->lock
);
4068 addrconf_dad_completed(ifp
, bump_id
, send_na
);
4072 if (!(idev
->if_flags
& IF_READY
)) {
4073 spin_unlock(&ifp
->lock
);
4074 read_unlock_bh(&idev
->lock
);
4076 * If the device is not ready:
4077 * - keep it tentative if it is a permanent address.
4078 * - otherwise, kill it.
4081 addrconf_dad_stop(ifp
, 0);
4086 * Optimistic nodes can start receiving
4089 if (ifp
->flags
& IFA_F_OPTIMISTIC
) {
4090 ip6_ins_rt(net
, ifp
->rt
);
4091 if (ipv6_use_optimistic_addr(net
, idev
)) {
4092 /* Because optimistic nodes can use this address,
4093 * notify listeners. If DAD fails, RTM_DELADDR is sent.
4099 addrconf_dad_kick(ifp
);
4101 spin_unlock(&ifp
->lock
);
4102 read_unlock_bh(&idev
->lock
);
4104 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
4107 static void addrconf_dad_start(struct inet6_ifaddr
*ifp
)
4109 bool begin_dad
= false;
4111 spin_lock_bh(&ifp
->lock
);
4112 if (ifp
->state
!= INET6_IFADDR_STATE_DEAD
) {
4113 ifp
->state
= INET6_IFADDR_STATE_PREDAD
;
4116 spin_unlock_bh(&ifp
->lock
);
4119 addrconf_mod_dad_work(ifp
, 0);
4122 static void addrconf_dad_work(struct work_struct
*w
)
4124 struct inet6_ifaddr
*ifp
= container_of(to_delayed_work(w
),
4125 struct inet6_ifaddr
,
4127 struct inet6_dev
*idev
= ifp
->idev
;
4128 bool bump_id
, disable_ipv6
= false;
4129 struct in6_addr mcaddr
;
4135 } action
= DAD_PROCESS
;
4139 spin_lock_bh(&ifp
->lock
);
4140 if (ifp
->state
== INET6_IFADDR_STATE_PREDAD
) {
4142 ifp
->state
= INET6_IFADDR_STATE_DAD
;
4143 } else if (ifp
->state
== INET6_IFADDR_STATE_ERRDAD
) {
4145 ifp
->state
= INET6_IFADDR_STATE_POSTDAD
;
4147 if ((dev_net(idev
->dev
)->ipv6
.devconf_all
->accept_dad
> 1 ||
4148 idev
->cnf
.accept_dad
> 1) &&
4149 !idev
->cnf
.disable_ipv6
&&
4150 !(ifp
->flags
& IFA_F_STABLE_PRIVACY
)) {
4151 struct in6_addr addr
;
4153 addr
.s6_addr32
[0] = htonl(0xfe800000);
4154 addr
.s6_addr32
[1] = 0;
4156 if (!ipv6_generate_eui64(addr
.s6_addr
+ 8, idev
->dev
) &&
4157 ipv6_addr_equal(&ifp
->addr
, &addr
)) {
4158 /* DAD failed for link-local based on MAC */
4159 idev
->cnf
.disable_ipv6
= 1;
4161 pr_info("%s: IPv6 being disabled!\n",
4162 ifp
->idev
->dev
->name
);
4163 disable_ipv6
= true;
4167 spin_unlock_bh(&ifp
->lock
);
4169 if (action
== DAD_BEGIN
) {
4170 addrconf_dad_begin(ifp
);
4172 } else if (action
== DAD_ABORT
) {
4174 addrconf_dad_stop(ifp
, 1);
4176 addrconf_ifdown(idev
->dev
, false);
4180 if (!ifp
->dad_probes
&& addrconf_dad_end(ifp
))
4183 write_lock_bh(&idev
->lock
);
4184 if (idev
->dead
|| !(idev
->if_flags
& IF_READY
)) {
4185 write_unlock_bh(&idev
->lock
);
4189 spin_lock(&ifp
->lock
);
4190 if (ifp
->state
== INET6_IFADDR_STATE_DEAD
) {
4191 spin_unlock(&ifp
->lock
);
4192 write_unlock_bh(&idev
->lock
);
4196 if (ifp
->dad_probes
== 0) {
4197 bool send_na
= false;
4200 * DAD was successful
4203 if (ifp
->flags
& IFA_F_TENTATIVE
&&
4204 !(ifp
->flags
& IFA_F_OPTIMISTIC
))
4206 bump_id
= ifp
->flags
& IFA_F_TENTATIVE
;
4207 ifp
->flags
&= ~(IFA_F_TENTATIVE
|IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
);
4208 spin_unlock(&ifp
->lock
);
4209 write_unlock_bh(&idev
->lock
);
4211 addrconf_dad_completed(ifp
, bump_id
, send_na
);
4217 addrconf_mod_dad_work(ifp
,
4218 max(NEIGH_VAR(ifp
->idev
->nd_parms
, RETRANS_TIME
),
4220 spin_unlock(&ifp
->lock
);
4221 write_unlock_bh(&idev
->lock
);
4223 /* send a neighbour solicitation for our addr */
4224 addrconf_addr_solict_mult(&ifp
->addr
, &mcaddr
);
4225 ndisc_send_ns(ifp
->idev
->dev
, &ifp
->addr
, &mcaddr
, &in6addr_any
,
4232 /* ifp->idev must be at least read locked */
4233 static bool ipv6_lonely_lladdr(struct inet6_ifaddr
*ifp
)
4235 struct inet6_ifaddr
*ifpiter
;
4236 struct inet6_dev
*idev
= ifp
->idev
;
4238 list_for_each_entry_reverse(ifpiter
, &idev
->addr_list
, if_list
) {
4239 if (ifpiter
->scope
> IFA_LINK
)
4241 if (ifp
!= ifpiter
&& ifpiter
->scope
== IFA_LINK
&&
4242 (ifpiter
->flags
& (IFA_F_PERMANENT
|IFA_F_TENTATIVE
|
4243 IFA_F_OPTIMISTIC
|IFA_F_DADFAILED
)) ==
4250 static void addrconf_dad_completed(struct inet6_ifaddr
*ifp
, bool bump_id
,
4253 struct net_device
*dev
= ifp
->idev
->dev
;
4254 struct in6_addr lladdr
;
4255 bool send_rs
, send_mld
;
4257 addrconf_del_dad_work(ifp
);
4260 * Configure the address for reception. Now it is valid.
4263 ipv6_ifa_notify(RTM_NEWADDR
, ifp
);
4265 /* If added prefix is link local and we are prepared to process
4266 router advertisements, start sending router solicitations.
4269 read_lock_bh(&ifp
->idev
->lock
);
4270 send_mld
= ifp
->scope
== IFA_LINK
&& ipv6_lonely_lladdr(ifp
);
4271 send_rs
= send_mld
&&
4272 ipv6_accept_ra(ifp
->idev
) &&
4273 ifp
->idev
->cnf
.rtr_solicits
!= 0 &&
4274 (dev
->flags
& IFF_LOOPBACK
) == 0 &&
4275 (dev
->type
!= ARPHRD_TUNNEL
) &&
4276 !netif_is_team_port(dev
);
4277 read_unlock_bh(&ifp
->idev
->lock
);
4279 /* While dad is in progress mld report's source address is in6_addrany.
4280 * Resend with proper ll now.
4283 ipv6_mc_dad_complete(ifp
->idev
);
4285 /* send unsolicited NA if enabled */
4287 (ifp
->idev
->cnf
.ndisc_notify
||
4288 dev_net(dev
)->ipv6
.devconf_all
->ndisc_notify
)) {
4289 ndisc_send_na(dev
, &in6addr_linklocal_allnodes
, &ifp
->addr
,
4290 /*router=*/ !!ifp
->idev
->cnf
.forwarding
,
4291 /*solicited=*/ false, /*override=*/ true,
4297 * If a host as already performed a random delay
4298 * [...] as part of DAD [...] there is no need
4299 * to delay again before sending the first RS
4301 if (ipv6_get_lladdr(dev
, &lladdr
, IFA_F_TENTATIVE
))
4303 ndisc_send_rs(dev
, &lladdr
, &in6addr_linklocal_allrouters
);
4305 write_lock_bh(&ifp
->idev
->lock
);
4306 spin_lock(&ifp
->lock
);
4307 ifp
->idev
->rs_interval
= rfc3315_s14_backoff_init(
4308 ifp
->idev
->cnf
.rtr_solicit_interval
);
4309 ifp
->idev
->rs_probes
= 1;
4310 ifp
->idev
->if_flags
|= IF_RS_SENT
;
4311 addrconf_mod_rs_timer(ifp
->idev
, ifp
->idev
->rs_interval
);
4312 spin_unlock(&ifp
->lock
);
4313 write_unlock_bh(&ifp
->idev
->lock
);
4317 rt_genid_bump_ipv6(dev_net(dev
));
4319 /* Make sure that a new temporary address will be created
4320 * before this temporary address becomes deprecated.
4322 if (ifp
->flags
& IFA_F_TEMPORARY
)
4323 addrconf_verify_rtnl(dev_net(dev
));
4326 static void addrconf_dad_run(struct inet6_dev
*idev
, bool restart
)
4328 struct inet6_ifaddr
*ifp
;
4330 read_lock_bh(&idev
->lock
);
4331 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
4332 spin_lock(&ifp
->lock
);
4333 if ((ifp
->flags
& IFA_F_TENTATIVE
&&
4334 ifp
->state
== INET6_IFADDR_STATE_DAD
) || restart
) {
4336 ifp
->state
= INET6_IFADDR_STATE_PREDAD
;
4337 addrconf_dad_kick(ifp
);
4339 spin_unlock(&ifp
->lock
);
4341 read_unlock_bh(&idev
->lock
);
4344 #ifdef CONFIG_PROC_FS
4345 struct if6_iter_state
{
4346 struct seq_net_private p
;
4351 static struct inet6_ifaddr
*if6_get_first(struct seq_file
*seq
, loff_t pos
)
4353 struct if6_iter_state
*state
= seq
->private;
4354 struct net
*net
= seq_file_net(seq
);
4355 struct inet6_ifaddr
*ifa
= NULL
;
4358 /* initial bucket if pos is 0 */
4364 for (; state
->bucket
< IN6_ADDR_HSIZE
; ++state
->bucket
) {
4365 hlist_for_each_entry_rcu(ifa
, &net
->ipv6
.inet6_addr_lst
[state
->bucket
],
4367 /* sync with offset */
4368 if (p
< state
->offset
) {
4375 /* prepare for next bucket */
4382 static struct inet6_ifaddr
*if6_get_next(struct seq_file
*seq
,
4383 struct inet6_ifaddr
*ifa
)
4385 struct if6_iter_state
*state
= seq
->private;
4386 struct net
*net
= seq_file_net(seq
);
4388 hlist_for_each_entry_continue_rcu(ifa
, addr_lst
) {
4394 while (++state
->bucket
< IN6_ADDR_HSIZE
) {
4395 hlist_for_each_entry_rcu(ifa
,
4396 &net
->ipv6
.inet6_addr_lst
[state
->bucket
], addr_lst
) {
4404 static void *if6_seq_start(struct seq_file
*seq
, loff_t
*pos
)
4408 return if6_get_first(seq
, *pos
);
4411 static void *if6_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
4413 struct inet6_ifaddr
*ifa
;
4415 ifa
= if6_get_next(seq
, v
);
4420 static void if6_seq_stop(struct seq_file
*seq
, void *v
)
4426 static int if6_seq_show(struct seq_file
*seq
, void *v
)
4428 struct inet6_ifaddr
*ifp
= (struct inet6_ifaddr
*)v
;
4429 seq_printf(seq
, "%pi6 %02x %02x %02x %02x %8s\n",
4431 ifp
->idev
->dev
->ifindex
,
4435 ifp
->idev
->dev
->name
);
4439 static const struct seq_operations if6_seq_ops
= {
4440 .start
= if6_seq_start
,
4441 .next
= if6_seq_next
,
4442 .show
= if6_seq_show
,
4443 .stop
= if6_seq_stop
,
4446 static int __net_init
if6_proc_net_init(struct net
*net
)
4448 if (!proc_create_net("if_inet6", 0444, net
->proc_net
, &if6_seq_ops
,
4449 sizeof(struct if6_iter_state
)))
4454 static void __net_exit
if6_proc_net_exit(struct net
*net
)
4456 remove_proc_entry("if_inet6", net
->proc_net
);
4459 static struct pernet_operations if6_proc_net_ops
= {
4460 .init
= if6_proc_net_init
,
4461 .exit
= if6_proc_net_exit
,
4464 int __init
if6_proc_init(void)
4466 return register_pernet_subsys(&if6_proc_net_ops
);
4469 void if6_proc_exit(void)
4471 unregister_pernet_subsys(&if6_proc_net_ops
);
4473 #endif /* CONFIG_PROC_FS */
4475 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4476 /* Check if address is a home address configured on any interface. */
4477 int ipv6_chk_home_addr(struct net
*net
, const struct in6_addr
*addr
)
4479 unsigned int hash
= inet6_addr_hash(net
, addr
);
4480 struct inet6_ifaddr
*ifp
= NULL
;
4484 hlist_for_each_entry_rcu(ifp
, &net
->ipv6
.inet6_addr_lst
[hash
], addr_lst
) {
4485 if (ipv6_addr_equal(&ifp
->addr
, addr
) &&
4486 (ifp
->flags
& IFA_F_HOMEADDRESS
)) {
4496 /* RFC6554 has some algorithm to avoid loops in segment routing by
4497 * checking if the segments contains any of a local interface address.
4501 * To detect loops in the SRH, a router MUST determine if the SRH
4502 * includes multiple addresses assigned to any interface on that router.
4503 * If such addresses appear more than once and are separated by at least
4504 * one address not assigned to that router.
4506 int ipv6_chk_rpl_srh_loop(struct net
*net
, const struct in6_addr
*segs
,
4507 unsigned char nsegs
)
4509 const struct in6_addr
*addr
;
4510 int i
, ret
= 0, found
= 0;
4511 struct inet6_ifaddr
*ifp
;
4512 bool separated
= false;
4517 for (i
= 0; i
< nsegs
; i
++) {
4519 hash
= inet6_addr_hash(net
, addr
);
4522 hlist_for_each_entry_rcu(ifp
, &net
->ipv6
.inet6_addr_lst
[hash
], addr_lst
) {
4524 if (ipv6_addr_equal(&ifp
->addr
, addr
)) {
4531 if (found
> 1 && separated
) {
4548 * Periodic address status verification
4551 static void addrconf_verify_rtnl(struct net
*net
)
4553 unsigned long now
, next
, next_sec
, next_sched
;
4554 struct inet6_ifaddr
*ifp
;
4561 next
= round_jiffies_up(now
+ ADDR_CHECK_FREQUENCY
);
4563 cancel_delayed_work(&net
->ipv6
.addr_chk_work
);
4565 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++) {
4567 hlist_for_each_entry_rcu_bh(ifp
, &net
->ipv6
.inet6_addr_lst
[i
], addr_lst
) {
4570 /* When setting preferred_lft to a value not zero or
4571 * infinity, while valid_lft is infinity
4572 * IFA_F_PERMANENT has a non-infinity life time.
4574 if ((ifp
->flags
& IFA_F_PERMANENT
) &&
4575 (ifp
->prefered_lft
== INFINITY_LIFE_TIME
))
4578 spin_lock(&ifp
->lock
);
4579 /* We try to batch several events at once. */
4580 age
= (now
- ifp
->tstamp
+ ADDRCONF_TIMER_FUZZ_MINUS
) / HZ
;
4582 if ((ifp
->flags
&IFA_F_TEMPORARY
) &&
4583 !(ifp
->flags
&IFA_F_TENTATIVE
) &&
4584 ifp
->prefered_lft
!= INFINITY_LIFE_TIME
&&
4585 !ifp
->regen_count
&& ifp
->ifpub
) {
4586 /* This is a non-regenerated temporary addr. */
4588 unsigned long regen_advance
= ifp
->idev
->cnf
.regen_max_retry
*
4589 ifp
->idev
->cnf
.dad_transmits
*
4590 max(NEIGH_VAR(ifp
->idev
->nd_parms
, RETRANS_TIME
), HZ
/100) / HZ
;
4592 if (age
+ regen_advance
>= ifp
->prefered_lft
) {
4593 struct inet6_ifaddr
*ifpub
= ifp
->ifpub
;
4594 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
4595 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
4599 in6_ifa_hold(ifpub
);
4600 spin_unlock(&ifp
->lock
);
4602 spin_lock(&ifpub
->lock
);
4603 ifpub
->regen_count
= 0;
4604 spin_unlock(&ifpub
->lock
);
4605 rcu_read_unlock_bh();
4606 ipv6_create_tempaddr(ifpub
, true);
4611 } else if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
, next
))
4612 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
- regen_advance
* HZ
;
4615 if (ifp
->valid_lft
!= INFINITY_LIFE_TIME
&&
4616 age
>= ifp
->valid_lft
) {
4617 spin_unlock(&ifp
->lock
);
4619 rcu_read_unlock_bh();
4623 } else if (ifp
->prefered_lft
== INFINITY_LIFE_TIME
) {
4624 spin_unlock(&ifp
->lock
);
4626 } else if (age
>= ifp
->prefered_lft
) {
4627 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4630 if (!(ifp
->flags
&IFA_F_DEPRECATED
)) {
4632 ifp
->flags
|= IFA_F_DEPRECATED
;
4635 if ((ifp
->valid_lft
!= INFINITY_LIFE_TIME
) &&
4636 (time_before(ifp
->tstamp
+ ifp
->valid_lft
* HZ
, next
)))
4637 next
= ifp
->tstamp
+ ifp
->valid_lft
* HZ
;
4639 spin_unlock(&ifp
->lock
);
4644 ipv6_ifa_notify(0, ifp
);
4649 /* ifp->prefered_lft <= ifp->valid_lft */
4650 if (time_before(ifp
->tstamp
+ ifp
->prefered_lft
* HZ
, next
))
4651 next
= ifp
->tstamp
+ ifp
->prefered_lft
* HZ
;
4652 spin_unlock(&ifp
->lock
);
4657 next_sec
= round_jiffies_up(next
);
4660 /* If rounded timeout is accurate enough, accept it. */
4661 if (time_before(next_sec
, next
+ ADDRCONF_TIMER_FUZZ
))
4662 next_sched
= next_sec
;
4664 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4665 if (time_before(next_sched
, jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
))
4666 next_sched
= jiffies
+ ADDRCONF_TIMER_FUZZ_MAX
;
4668 pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4669 now
, next
, next_sec
, next_sched
);
4670 mod_delayed_work(addrconf_wq
, &net
->ipv6
.addr_chk_work
, next_sched
- now
);
4671 rcu_read_unlock_bh();
4674 static void addrconf_verify_work(struct work_struct
*w
)
4676 struct net
*net
= container_of(to_delayed_work(w
), struct net
,
4677 ipv6
.addr_chk_work
);
4680 addrconf_verify_rtnl(net
);
4684 static void addrconf_verify(struct net
*net
)
4686 mod_delayed_work(addrconf_wq
, &net
->ipv6
.addr_chk_work
, 0);
4689 static struct in6_addr
*extract_addr(struct nlattr
*addr
, struct nlattr
*local
,
4690 struct in6_addr
**peer_pfx
)
4692 struct in6_addr
*pfx
= NULL
;
4697 pfx
= nla_data(addr
);
4700 if (pfx
&& nla_memcmp(local
, pfx
, sizeof(*pfx
)))
4702 pfx
= nla_data(local
);
4708 static const struct nla_policy ifa_ipv6_policy
[IFA_MAX
+1] = {
4709 [IFA_ADDRESS
] = { .len
= sizeof(struct in6_addr
) },
4710 [IFA_LOCAL
] = { .len
= sizeof(struct in6_addr
) },
4711 [IFA_CACHEINFO
] = { .len
= sizeof(struct ifa_cacheinfo
) },
4712 [IFA_FLAGS
] = { .len
= sizeof(u32
) },
4713 [IFA_RT_PRIORITY
] = { .len
= sizeof(u32
) },
4714 [IFA_TARGET_NETNSID
] = { .type
= NLA_S32
},
4715 [IFA_PROTO
] = { .type
= NLA_U8
},
4719 inet6_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4720 struct netlink_ext_ack
*extack
)
4722 struct net
*net
= sock_net(skb
->sk
);
4723 struct ifaddrmsg
*ifm
;
4724 struct nlattr
*tb
[IFA_MAX
+1];
4725 struct in6_addr
*pfx
, *peer_pfx
;
4729 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
4730 ifa_ipv6_policy
, extack
);
4734 ifm
= nlmsg_data(nlh
);
4735 pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer_pfx
);
4739 ifa_flags
= tb
[IFA_FLAGS
] ? nla_get_u32(tb
[IFA_FLAGS
]) : ifm
->ifa_flags
;
4741 /* We ignore other flags so far. */
4742 ifa_flags
&= IFA_F_MANAGETEMPADDR
;
4744 return inet6_addr_del(net
, ifm
->ifa_index
, ifa_flags
, pfx
,
4745 ifm
->ifa_prefixlen
, extack
);
4748 static int modify_prefix_route(struct inet6_ifaddr
*ifp
,
4749 unsigned long expires
, u32 flags
,
4752 struct fib6_info
*f6i
;
4755 f6i
= addrconf_get_prefix_route(modify_peer
? &ifp
->peer_addr
: &ifp
->addr
,
4757 ifp
->idev
->dev
, 0, RTF_DEFAULT
, true);
4761 prio
= ifp
->rt_priority
? : IP6_RT_PRIO_ADDRCONF
;
4762 if (f6i
->fib6_metric
!= prio
) {
4763 /* delete old one */
4764 ip6_del_rt(dev_net(ifp
->idev
->dev
), f6i
, false);
4767 addrconf_prefix_route(modify_peer
? &ifp
->peer_addr
: &ifp
->addr
,
4769 ifp
->rt_priority
, ifp
->idev
->dev
,
4770 expires
, flags
, GFP_KERNEL
);
4773 fib6_clean_expires(f6i
);
4775 fib6_set_expires(f6i
, expires
);
4777 fib6_info_release(f6i
);
4783 static int inet6_addr_modify(struct net
*net
, struct inet6_ifaddr
*ifp
,
4784 struct ifa6_config
*cfg
)
4788 unsigned long timeout
;
4789 bool was_managetempaddr
;
4790 bool had_prefixroute
;
4791 bool new_peer
= false;
4795 if (!cfg
->valid_lft
|| cfg
->preferred_lft
> cfg
->valid_lft
)
4798 if (cfg
->ifa_flags
& IFA_F_MANAGETEMPADDR
&&
4799 (ifp
->flags
& IFA_F_TEMPORARY
|| ifp
->prefix_len
!= 64))
4802 if (!(ifp
->flags
& IFA_F_TENTATIVE
) || ifp
->flags
& IFA_F_DADFAILED
)
4803 cfg
->ifa_flags
&= ~IFA_F_OPTIMISTIC
;
4805 timeout
= addrconf_timeout_fixup(cfg
->valid_lft
, HZ
);
4806 if (addrconf_finite_timeout(timeout
)) {
4807 expires
= jiffies_to_clock_t(timeout
* HZ
);
4808 cfg
->valid_lft
= timeout
;
4809 flags
= RTF_EXPIRES
;
4813 cfg
->ifa_flags
|= IFA_F_PERMANENT
;
4816 timeout
= addrconf_timeout_fixup(cfg
->preferred_lft
, HZ
);
4817 if (addrconf_finite_timeout(timeout
)) {
4819 cfg
->ifa_flags
|= IFA_F_DEPRECATED
;
4820 cfg
->preferred_lft
= timeout
;
4823 if (cfg
->peer_pfx
&&
4824 memcmp(&ifp
->peer_addr
, cfg
->peer_pfx
, sizeof(struct in6_addr
))) {
4825 if (!ipv6_addr_any(&ifp
->peer_addr
))
4826 cleanup_prefix_route(ifp
, expires
, true, true);
4830 spin_lock_bh(&ifp
->lock
);
4831 was_managetempaddr
= ifp
->flags
& IFA_F_MANAGETEMPADDR
;
4832 had_prefixroute
= ifp
->flags
& IFA_F_PERMANENT
&&
4833 !(ifp
->flags
& IFA_F_NOPREFIXROUTE
);
4834 ifp
->flags
&= ~(IFA_F_DEPRECATED
| IFA_F_PERMANENT
| IFA_F_NODAD
|
4835 IFA_F_HOMEADDRESS
| IFA_F_MANAGETEMPADDR
|
4836 IFA_F_NOPREFIXROUTE
);
4837 ifp
->flags
|= cfg
->ifa_flags
;
4838 ifp
->tstamp
= jiffies
;
4839 ifp
->valid_lft
= cfg
->valid_lft
;
4840 ifp
->prefered_lft
= cfg
->preferred_lft
;
4841 ifp
->ifa_proto
= cfg
->ifa_proto
;
4843 if (cfg
->rt_priority
&& cfg
->rt_priority
!= ifp
->rt_priority
)
4844 ifp
->rt_priority
= cfg
->rt_priority
;
4847 ifp
->peer_addr
= *cfg
->peer_pfx
;
4849 spin_unlock_bh(&ifp
->lock
);
4850 if (!(ifp
->flags
&IFA_F_TENTATIVE
))
4851 ipv6_ifa_notify(0, ifp
);
4853 if (!(cfg
->ifa_flags
& IFA_F_NOPREFIXROUTE
)) {
4856 if (had_prefixroute
)
4857 rc
= modify_prefix_route(ifp
, expires
, flags
, false);
4859 /* prefix route could have been deleted; if so restore it */
4860 if (rc
== -ENOENT
) {
4861 addrconf_prefix_route(&ifp
->addr
, ifp
->prefix_len
,
4862 ifp
->rt_priority
, ifp
->idev
->dev
,
4863 expires
, flags
, GFP_KERNEL
);
4866 if (had_prefixroute
&& !ipv6_addr_any(&ifp
->peer_addr
))
4867 rc
= modify_prefix_route(ifp
, expires
, flags
, true);
4869 if (rc
== -ENOENT
&& !ipv6_addr_any(&ifp
->peer_addr
)) {
4870 addrconf_prefix_route(&ifp
->peer_addr
, ifp
->prefix_len
,
4871 ifp
->rt_priority
, ifp
->idev
->dev
,
4872 expires
, flags
, GFP_KERNEL
);
4874 } else if (had_prefixroute
) {
4875 enum cleanup_prefix_rt_t action
;
4876 unsigned long rt_expires
;
4878 write_lock_bh(&ifp
->idev
->lock
);
4879 action
= check_cleanup_prefix_route(ifp
, &rt_expires
);
4880 write_unlock_bh(&ifp
->idev
->lock
);
4882 if (action
!= CLEANUP_PREFIX_RT_NOP
) {
4883 cleanup_prefix_route(ifp
, rt_expires
,
4884 action
== CLEANUP_PREFIX_RT_DEL
, false);
4888 if (was_managetempaddr
|| ifp
->flags
& IFA_F_MANAGETEMPADDR
) {
4889 if (was_managetempaddr
&&
4890 !(ifp
->flags
& IFA_F_MANAGETEMPADDR
)) {
4892 cfg
->preferred_lft
= 0;
4894 manage_tempaddrs(ifp
->idev
, ifp
, cfg
->valid_lft
,
4895 cfg
->preferred_lft
, !was_managetempaddr
,
4899 addrconf_verify_rtnl(net
);
4905 inet6_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
4906 struct netlink_ext_ack
*extack
)
4908 struct net
*net
= sock_net(skb
->sk
);
4909 struct ifaddrmsg
*ifm
;
4910 struct nlattr
*tb
[IFA_MAX
+1];
4911 struct in6_addr
*peer_pfx
;
4912 struct inet6_ifaddr
*ifa
;
4913 struct net_device
*dev
;
4914 struct inet6_dev
*idev
;
4915 struct ifa6_config cfg
;
4918 err
= nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
4919 ifa_ipv6_policy
, extack
);
4923 memset(&cfg
, 0, sizeof(cfg
));
4925 ifm
= nlmsg_data(nlh
);
4926 cfg
.pfx
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer_pfx
);
4930 cfg
.peer_pfx
= peer_pfx
;
4931 cfg
.plen
= ifm
->ifa_prefixlen
;
4932 if (tb
[IFA_RT_PRIORITY
])
4933 cfg
.rt_priority
= nla_get_u32(tb
[IFA_RT_PRIORITY
]);
4936 cfg
.ifa_proto
= nla_get_u8(tb
[IFA_PROTO
]);
4938 cfg
.valid_lft
= INFINITY_LIFE_TIME
;
4939 cfg
.preferred_lft
= INFINITY_LIFE_TIME
;
4941 if (tb
[IFA_CACHEINFO
]) {
4942 struct ifa_cacheinfo
*ci
;
4944 ci
= nla_data(tb
[IFA_CACHEINFO
]);
4945 cfg
.valid_lft
= ci
->ifa_valid
;
4946 cfg
.preferred_lft
= ci
->ifa_prefered
;
4949 dev
= __dev_get_by_index(net
, ifm
->ifa_index
);
4951 NL_SET_ERR_MSG_MOD(extack
, "Unable to find the interface");
4956 cfg
.ifa_flags
= nla_get_u32(tb
[IFA_FLAGS
]);
4958 cfg
.ifa_flags
= ifm
->ifa_flags
;
4960 /* We ignore other flags so far. */
4961 cfg
.ifa_flags
&= IFA_F_NODAD
| IFA_F_HOMEADDRESS
|
4962 IFA_F_MANAGETEMPADDR
| IFA_F_NOPREFIXROUTE
|
4963 IFA_F_MCAUTOJOIN
| IFA_F_OPTIMISTIC
;
4965 idev
= ipv6_find_idev(dev
);
4967 return PTR_ERR(idev
);
4969 if (!ipv6_allow_optimistic_dad(net
, idev
))
4970 cfg
.ifa_flags
&= ~IFA_F_OPTIMISTIC
;
4972 if (cfg
.ifa_flags
& IFA_F_NODAD
&&
4973 cfg
.ifa_flags
& IFA_F_OPTIMISTIC
) {
4974 NL_SET_ERR_MSG(extack
, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4978 ifa
= ipv6_get_ifaddr(net
, cfg
.pfx
, dev
, 1);
4981 * It would be best to check for !NLM_F_CREATE here but
4982 * userspace already relies on not having to provide this.
4984 return inet6_addr_add(net
, ifm
->ifa_index
, &cfg
, extack
);
4987 if (nlh
->nlmsg_flags
& NLM_F_EXCL
||
4988 !(nlh
->nlmsg_flags
& NLM_F_REPLACE
)) {
4989 NL_SET_ERR_MSG_MOD(extack
, "address already assigned");
4992 err
= inet6_addr_modify(net
, ifa
, &cfg
);
5000 static void put_ifaddrmsg(struct nlmsghdr
*nlh
, u8 prefixlen
, u32 flags
,
5001 u8 scope
, int ifindex
)
5003 struct ifaddrmsg
*ifm
;
5005 ifm
= nlmsg_data(nlh
);
5006 ifm
->ifa_family
= AF_INET6
;
5007 ifm
->ifa_prefixlen
= prefixlen
;
5008 ifm
->ifa_flags
= flags
;
5009 ifm
->ifa_scope
= scope
;
5010 ifm
->ifa_index
= ifindex
;
5013 static int put_cacheinfo(struct sk_buff
*skb
, unsigned long cstamp
,
5014 unsigned long tstamp
, u32 preferred
, u32 valid
)
5016 struct ifa_cacheinfo ci
;
5018 ci
.cstamp
= cstamp_delta(cstamp
);
5019 ci
.tstamp
= cstamp_delta(tstamp
);
5020 ci
.ifa_prefered
= preferred
;
5021 ci
.ifa_valid
= valid
;
5023 return nla_put(skb
, IFA_CACHEINFO
, sizeof(ci
), &ci
);
5026 static inline int rt_scope(int ifa_scope
)
5028 if (ifa_scope
& IFA_HOST
)
5029 return RT_SCOPE_HOST
;
5030 else if (ifa_scope
& IFA_LINK
)
5031 return RT_SCOPE_LINK
;
5032 else if (ifa_scope
& IFA_SITE
)
5033 return RT_SCOPE_SITE
;
5035 return RT_SCOPE_UNIVERSE
;
5038 static inline int inet6_ifaddr_msgsize(void)
5040 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
5041 + nla_total_size(16) /* IFA_LOCAL */
5042 + nla_total_size(16) /* IFA_ADDRESS */
5043 + nla_total_size(sizeof(struct ifa_cacheinfo
))
5044 + nla_total_size(4) /* IFA_FLAGS */
5045 + nla_total_size(1) /* IFA_PROTO */
5046 + nla_total_size(4) /* IFA_RT_PRIORITY */;
5055 struct inet6_fill_args
{
5062 enum addr_type_t type
;
5065 static int inet6_fill_ifaddr(struct sk_buff
*skb
, struct inet6_ifaddr
*ifa
,
5066 struct inet6_fill_args
*args
)
5068 struct nlmsghdr
*nlh
;
5069 u32 preferred
, valid
;
5071 nlh
= nlmsg_put(skb
, args
->portid
, args
->seq
, args
->event
,
5072 sizeof(struct ifaddrmsg
), args
->flags
);
5076 put_ifaddrmsg(nlh
, ifa
->prefix_len
, ifa
->flags
, rt_scope(ifa
->scope
),
5077 ifa
->idev
->dev
->ifindex
);
5079 if (args
->netnsid
>= 0 &&
5080 nla_put_s32(skb
, IFA_TARGET_NETNSID
, args
->netnsid
))
5083 spin_lock_bh(&ifa
->lock
);
5084 if (!((ifa
->flags
&IFA_F_PERMANENT
) &&
5085 (ifa
->prefered_lft
== INFINITY_LIFE_TIME
))) {
5086 preferred
= ifa
->prefered_lft
;
5087 valid
= ifa
->valid_lft
;
5088 if (preferred
!= INFINITY_LIFE_TIME
) {
5089 long tval
= (jiffies
- ifa
->tstamp
)/HZ
;
5090 if (preferred
> tval
)
5094 if (valid
!= INFINITY_LIFE_TIME
) {
5102 preferred
= INFINITY_LIFE_TIME
;
5103 valid
= INFINITY_LIFE_TIME
;
5105 spin_unlock_bh(&ifa
->lock
);
5107 if (!ipv6_addr_any(&ifa
->peer_addr
)) {
5108 if (nla_put_in6_addr(skb
, IFA_LOCAL
, &ifa
->addr
) < 0 ||
5109 nla_put_in6_addr(skb
, IFA_ADDRESS
, &ifa
->peer_addr
) < 0)
5112 if (nla_put_in6_addr(skb
, IFA_ADDRESS
, &ifa
->addr
) < 0)
5115 if (ifa
->rt_priority
&&
5116 nla_put_u32(skb
, IFA_RT_PRIORITY
, ifa
->rt_priority
))
5119 if (put_cacheinfo(skb
, ifa
->cstamp
, ifa
->tstamp
, preferred
, valid
) < 0)
5122 if (nla_put_u32(skb
, IFA_FLAGS
, ifa
->flags
) < 0)
5125 if (ifa
->ifa_proto
&&
5126 nla_put_u8(skb
, IFA_PROTO
, ifa
->ifa_proto
))
5129 nlmsg_end(skb
, nlh
);
5133 nlmsg_cancel(skb
, nlh
);
5137 static int inet6_fill_ifmcaddr(struct sk_buff
*skb
, struct ifmcaddr6
*ifmca
,
5138 struct inet6_fill_args
*args
)
5140 struct nlmsghdr
*nlh
;
5141 u8 scope
= RT_SCOPE_UNIVERSE
;
5142 int ifindex
= ifmca
->idev
->dev
->ifindex
;
5144 if (ipv6_addr_scope(&ifmca
->mca_addr
) & IFA_SITE
)
5145 scope
= RT_SCOPE_SITE
;
5147 nlh
= nlmsg_put(skb
, args
->portid
, args
->seq
, args
->event
,
5148 sizeof(struct ifaddrmsg
), args
->flags
);
5152 if (args
->netnsid
>= 0 &&
5153 nla_put_s32(skb
, IFA_TARGET_NETNSID
, args
->netnsid
)) {
5154 nlmsg_cancel(skb
, nlh
);
5158 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
5159 if (nla_put_in6_addr(skb
, IFA_MULTICAST
, &ifmca
->mca_addr
) < 0 ||
5160 put_cacheinfo(skb
, ifmca
->mca_cstamp
, ifmca
->mca_tstamp
,
5161 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
5162 nlmsg_cancel(skb
, nlh
);
5166 nlmsg_end(skb
, nlh
);
5170 static int inet6_fill_ifacaddr(struct sk_buff
*skb
, struct ifacaddr6
*ifaca
,
5171 struct inet6_fill_args
*args
)
5173 struct net_device
*dev
= fib6_info_nh_dev(ifaca
->aca_rt
);
5174 int ifindex
= dev
? dev
->ifindex
: 1;
5175 struct nlmsghdr
*nlh
;
5176 u8 scope
= RT_SCOPE_UNIVERSE
;
5178 if (ipv6_addr_scope(&ifaca
->aca_addr
) & IFA_SITE
)
5179 scope
= RT_SCOPE_SITE
;
5181 nlh
= nlmsg_put(skb
, args
->portid
, args
->seq
, args
->event
,
5182 sizeof(struct ifaddrmsg
), args
->flags
);
5186 if (args
->netnsid
>= 0 &&
5187 nla_put_s32(skb
, IFA_TARGET_NETNSID
, args
->netnsid
)) {
5188 nlmsg_cancel(skb
, nlh
);
5192 put_ifaddrmsg(nlh
, 128, IFA_F_PERMANENT
, scope
, ifindex
);
5193 if (nla_put_in6_addr(skb
, IFA_ANYCAST
, &ifaca
->aca_addr
) < 0 ||
5194 put_cacheinfo(skb
, ifaca
->aca_cstamp
, ifaca
->aca_tstamp
,
5195 INFINITY_LIFE_TIME
, INFINITY_LIFE_TIME
) < 0) {
5196 nlmsg_cancel(skb
, nlh
);
5200 nlmsg_end(skb
, nlh
);
5204 /* called with rcu_read_lock() */
5205 static int in6_dump_addrs(struct inet6_dev
*idev
, struct sk_buff
*skb
,
5206 struct netlink_callback
*cb
, int s_ip_idx
,
5207 struct inet6_fill_args
*fillargs
)
5209 struct ifmcaddr6
*ifmca
;
5210 struct ifacaddr6
*ifaca
;
5214 read_lock_bh(&idev
->lock
);
5215 switch (fillargs
->type
) {
5216 case UNICAST_ADDR
: {
5217 struct inet6_ifaddr
*ifa
;
5218 fillargs
->event
= RTM_NEWADDR
;
5220 /* unicast address incl. temp addr */
5221 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
5222 if (ip_idx
< s_ip_idx
)
5224 err
= inet6_fill_ifaddr(skb
, ifa
, fillargs
);
5227 nl_dump_check_consistent(cb
, nlmsg_hdr(skb
));
5233 case MULTICAST_ADDR
:
5234 read_unlock_bh(&idev
->lock
);
5235 fillargs
->event
= RTM_GETMULTICAST
;
5237 /* multicast address */
5238 for (ifmca
= rtnl_dereference(idev
->mc_list
);
5240 ifmca
= rtnl_dereference(ifmca
->next
), ip_idx
++) {
5241 if (ip_idx
< s_ip_idx
)
5243 err
= inet6_fill_ifmcaddr(skb
, ifmca
, fillargs
);
5247 read_lock_bh(&idev
->lock
);
5250 fillargs
->event
= RTM_GETANYCAST
;
5251 /* anycast address */
5252 for (ifaca
= idev
->ac_list
; ifaca
;
5253 ifaca
= ifaca
->aca_next
, ip_idx
++) {
5254 if (ip_idx
< s_ip_idx
)
5256 err
= inet6_fill_ifacaddr(skb
, ifaca
, fillargs
);
5264 read_unlock_bh(&idev
->lock
);
5265 cb
->args
[2] = ip_idx
;
5269 static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr
*nlh
,
5270 struct inet6_fill_args
*fillargs
,
5271 struct net
**tgt_net
, struct sock
*sk
,
5272 struct netlink_callback
*cb
)
5274 struct netlink_ext_ack
*extack
= cb
->extack
;
5275 struct nlattr
*tb
[IFA_MAX
+1];
5276 struct ifaddrmsg
*ifm
;
5279 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
5280 NL_SET_ERR_MSG_MOD(extack
, "Invalid header for address dump request");
5284 ifm
= nlmsg_data(nlh
);
5285 if (ifm
->ifa_prefixlen
|| ifm
->ifa_flags
|| ifm
->ifa_scope
) {
5286 NL_SET_ERR_MSG_MOD(extack
, "Invalid values in header for address dump request");
5290 fillargs
->ifindex
= ifm
->ifa_index
;
5291 if (fillargs
->ifindex
) {
5292 cb
->answer_flags
|= NLM_F_DUMP_FILTERED
;
5293 fillargs
->flags
|= NLM_F_DUMP_FILTERED
;
5296 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
5297 ifa_ipv6_policy
, extack
);
5301 for (i
= 0; i
<= IFA_MAX
; ++i
) {
5305 if (i
== IFA_TARGET_NETNSID
) {
5308 fillargs
->netnsid
= nla_get_s32(tb
[i
]);
5309 net
= rtnl_get_net_ns_capable(sk
, fillargs
->netnsid
);
5311 fillargs
->netnsid
= -1;
5312 NL_SET_ERR_MSG_MOD(extack
, "Invalid target network namespace id");
5313 return PTR_ERR(net
);
5317 NL_SET_ERR_MSG_MOD(extack
, "Unsupported attribute in dump request");
5325 static int inet6_dump_addr(struct sk_buff
*skb
, struct netlink_callback
*cb
,
5326 enum addr_type_t type
)
5328 const struct nlmsghdr
*nlh
= cb
->nlh
;
5329 struct inet6_fill_args fillargs
= {
5330 .portid
= NETLINK_CB(cb
->skb
).portid
,
5331 .seq
= cb
->nlh
->nlmsg_seq
,
5332 .flags
= NLM_F_MULTI
,
5336 struct net
*tgt_net
= sock_net(skb
->sk
);
5337 int idx
, s_idx
, s_ip_idx
;
5339 struct net_device
*dev
;
5340 struct inet6_dev
*idev
;
5341 struct hlist_head
*head
;
5345 s_idx
= idx
= cb
->args
[1];
5346 s_ip_idx
= cb
->args
[2];
5348 if (cb
->strict_check
) {
5349 err
= inet6_valid_dump_ifaddr_req(nlh
, &fillargs
, &tgt_net
,
5355 if (fillargs
.ifindex
) {
5356 dev
= __dev_get_by_index(tgt_net
, fillargs
.ifindex
);
5361 idev
= __in6_dev_get(dev
);
5363 err
= in6_dump_addrs(idev
, skb
, cb
, s_ip_idx
,
5373 cb
->seq
= atomic_read(&tgt_net
->ipv6
.dev_addr_genid
) ^ tgt_net
->dev_base_seq
;
5374 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
5376 head
= &tgt_net
->dev_index_head
[h
];
5377 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
5380 if (h
> s_h
|| idx
> s_idx
)
5382 idev
= __in6_dev_get(dev
);
5386 if (in6_dump_addrs(idev
, skb
, cb
, s_ip_idx
,
5398 if (fillargs
.netnsid
>= 0)
5401 return skb
->len
? : err
;
5404 static int inet6_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
5406 enum addr_type_t type
= UNICAST_ADDR
;
5408 return inet6_dump_addr(skb
, cb
, type
);
5411 static int inet6_dump_ifmcaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
5413 enum addr_type_t type
= MULTICAST_ADDR
;
5415 return inet6_dump_addr(skb
, cb
, type
);
5419 static int inet6_dump_ifacaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
5421 enum addr_type_t type
= ANYCAST_ADDR
;
5423 return inet6_dump_addr(skb
, cb
, type
);
5426 static int inet6_rtm_valid_getaddr_req(struct sk_buff
*skb
,
5427 const struct nlmsghdr
*nlh
,
5429 struct netlink_ext_ack
*extack
)
5431 struct ifaddrmsg
*ifm
;
5434 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
5435 NL_SET_ERR_MSG_MOD(extack
, "Invalid header for get address request");
5439 if (!netlink_strict_get_check(skb
))
5440 return nlmsg_parse_deprecated(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
5441 ifa_ipv6_policy
, extack
);
5443 ifm
= nlmsg_data(nlh
);
5444 if (ifm
->ifa_prefixlen
|| ifm
->ifa_flags
|| ifm
->ifa_scope
) {
5445 NL_SET_ERR_MSG_MOD(extack
, "Invalid values in header for get address request");
5449 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(*ifm
), tb
, IFA_MAX
,
5450 ifa_ipv6_policy
, extack
);
5454 for (i
= 0; i
<= IFA_MAX
; i
++) {
5459 case IFA_TARGET_NETNSID
:
5464 NL_SET_ERR_MSG_MOD(extack
, "Unsupported attribute in get address request");
5472 static int inet6_rtm_getaddr(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
,
5473 struct netlink_ext_ack
*extack
)
5475 struct net
*tgt_net
= sock_net(in_skb
->sk
);
5476 struct inet6_fill_args fillargs
= {
5477 .portid
= NETLINK_CB(in_skb
).portid
,
5478 .seq
= nlh
->nlmsg_seq
,
5479 .event
= RTM_NEWADDR
,
5483 struct ifaddrmsg
*ifm
;
5484 struct nlattr
*tb
[IFA_MAX
+1];
5485 struct in6_addr
*addr
= NULL
, *peer
;
5486 struct net_device
*dev
= NULL
;
5487 struct inet6_ifaddr
*ifa
;
5488 struct sk_buff
*skb
;
5491 err
= inet6_rtm_valid_getaddr_req(in_skb
, nlh
, tb
, extack
);
5495 if (tb
[IFA_TARGET_NETNSID
]) {
5496 fillargs
.netnsid
= nla_get_s32(tb
[IFA_TARGET_NETNSID
]);
5498 tgt_net
= rtnl_get_net_ns_capable(NETLINK_CB(in_skb
).sk
,
5500 if (IS_ERR(tgt_net
))
5501 return PTR_ERR(tgt_net
);
5504 addr
= extract_addr(tb
[IFA_ADDRESS
], tb
[IFA_LOCAL
], &peer
);
5508 ifm
= nlmsg_data(nlh
);
5510 dev
= dev_get_by_index(tgt_net
, ifm
->ifa_index
);
5512 ifa
= ipv6_get_ifaddr(tgt_net
, addr
, dev
, 1);
5514 err
= -EADDRNOTAVAIL
;
5518 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL
);
5524 err
= inet6_fill_ifaddr(skb
, ifa
, &fillargs
);
5526 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5527 WARN_ON(err
== -EMSGSIZE
);
5531 err
= rtnl_unicast(skb
, tgt_net
, NETLINK_CB(in_skb
).portid
);
5536 if (fillargs
.netnsid
>= 0)
5542 static void inet6_ifa_notify(int event
, struct inet6_ifaddr
*ifa
)
5544 struct sk_buff
*skb
;
5545 struct net
*net
= dev_net(ifa
->idev
->dev
);
5546 struct inet6_fill_args fillargs
= {
5555 skb
= nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC
);
5559 err
= inet6_fill_ifaddr(skb
, ifa
, &fillargs
);
5561 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5562 WARN_ON(err
== -EMSGSIZE
);
5566 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFADDR
, NULL
, GFP_ATOMIC
);
5570 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFADDR
, err
);
5573 static inline void ipv6_store_devconf(struct ipv6_devconf
*cnf
,
5574 __s32
*array
, int bytes
)
5576 BUG_ON(bytes
< (DEVCONF_MAX
* 4));
5578 memset(array
, 0, bytes
);
5579 array
[DEVCONF_FORWARDING
] = cnf
->forwarding
;
5580 array
[DEVCONF_HOPLIMIT
] = cnf
->hop_limit
;
5581 array
[DEVCONF_MTU6
] = cnf
->mtu6
;
5582 array
[DEVCONF_ACCEPT_RA
] = cnf
->accept_ra
;
5583 array
[DEVCONF_ACCEPT_REDIRECTS
] = cnf
->accept_redirects
;
5584 array
[DEVCONF_AUTOCONF
] = cnf
->autoconf
;
5585 array
[DEVCONF_DAD_TRANSMITS
] = cnf
->dad_transmits
;
5586 array
[DEVCONF_RTR_SOLICITS
] = cnf
->rtr_solicits
;
5587 array
[DEVCONF_RTR_SOLICIT_INTERVAL
] =
5588 jiffies_to_msecs(cnf
->rtr_solicit_interval
);
5589 array
[DEVCONF_RTR_SOLICIT_MAX_INTERVAL
] =
5590 jiffies_to_msecs(cnf
->rtr_solicit_max_interval
);
5591 array
[DEVCONF_RTR_SOLICIT_DELAY
] =
5592 jiffies_to_msecs(cnf
->rtr_solicit_delay
);
5593 array
[DEVCONF_FORCE_MLD_VERSION
] = cnf
->force_mld_version
;
5594 array
[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL
] =
5595 jiffies_to_msecs(cnf
->mldv1_unsolicited_report_interval
);
5596 array
[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL
] =
5597 jiffies_to_msecs(cnf
->mldv2_unsolicited_report_interval
);
5598 array
[DEVCONF_USE_TEMPADDR
] = cnf
->use_tempaddr
;
5599 array
[DEVCONF_TEMP_VALID_LFT
] = cnf
->temp_valid_lft
;
5600 array
[DEVCONF_TEMP_PREFERED_LFT
] = cnf
->temp_prefered_lft
;
5601 array
[DEVCONF_REGEN_MAX_RETRY
] = cnf
->regen_max_retry
;
5602 array
[DEVCONF_MAX_DESYNC_FACTOR
] = cnf
->max_desync_factor
;
5603 array
[DEVCONF_MAX_ADDRESSES
] = cnf
->max_addresses
;
5604 array
[DEVCONF_ACCEPT_RA_DEFRTR
] = cnf
->accept_ra_defrtr
;
5605 array
[DEVCONF_RA_DEFRTR_METRIC
] = cnf
->ra_defrtr_metric
;
5606 array
[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT
] = cnf
->accept_ra_min_hop_limit
;
5607 array
[DEVCONF_ACCEPT_RA_PINFO
] = cnf
->accept_ra_pinfo
;
5608 #ifdef CONFIG_IPV6_ROUTER_PREF
5609 array
[DEVCONF_ACCEPT_RA_RTR_PREF
] = cnf
->accept_ra_rtr_pref
;
5610 array
[DEVCONF_RTR_PROBE_INTERVAL
] =
5611 jiffies_to_msecs(cnf
->rtr_probe_interval
);
5612 #ifdef CONFIG_IPV6_ROUTE_INFO
5613 array
[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN
] = cnf
->accept_ra_rt_info_min_plen
;
5614 array
[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN
] = cnf
->accept_ra_rt_info_max_plen
;
5617 array
[DEVCONF_PROXY_NDP
] = cnf
->proxy_ndp
;
5618 array
[DEVCONF_ACCEPT_SOURCE_ROUTE
] = cnf
->accept_source_route
;
5619 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5620 array
[DEVCONF_OPTIMISTIC_DAD
] = cnf
->optimistic_dad
;
5621 array
[DEVCONF_USE_OPTIMISTIC
] = cnf
->use_optimistic
;
5623 #ifdef CONFIG_IPV6_MROUTE
5624 array
[DEVCONF_MC_FORWARDING
] = atomic_read(&cnf
->mc_forwarding
);
5626 array
[DEVCONF_DISABLE_IPV6
] = cnf
->disable_ipv6
;
5627 array
[DEVCONF_ACCEPT_DAD
] = cnf
->accept_dad
;
5628 array
[DEVCONF_FORCE_TLLAO
] = cnf
->force_tllao
;
5629 array
[DEVCONF_NDISC_NOTIFY
] = cnf
->ndisc_notify
;
5630 array
[DEVCONF_SUPPRESS_FRAG_NDISC
] = cnf
->suppress_frag_ndisc
;
5631 array
[DEVCONF_ACCEPT_RA_FROM_LOCAL
] = cnf
->accept_ra_from_local
;
5632 array
[DEVCONF_ACCEPT_RA_MTU
] = cnf
->accept_ra_mtu
;
5633 array
[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN
] = cnf
->ignore_routes_with_linkdown
;
5634 /* we omit DEVCONF_STABLE_SECRET for now */
5635 array
[DEVCONF_USE_OIF_ADDRS_ONLY
] = cnf
->use_oif_addrs_only
;
5636 array
[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST
] = cnf
->drop_unicast_in_l2_multicast
;
5637 array
[DEVCONF_DROP_UNSOLICITED_NA
] = cnf
->drop_unsolicited_na
;
5638 array
[DEVCONF_KEEP_ADDR_ON_DOWN
] = cnf
->keep_addr_on_down
;
5639 array
[DEVCONF_SEG6_ENABLED
] = cnf
->seg6_enabled
;
5640 #ifdef CONFIG_IPV6_SEG6_HMAC
5641 array
[DEVCONF_SEG6_REQUIRE_HMAC
] = cnf
->seg6_require_hmac
;
5643 array
[DEVCONF_ENHANCED_DAD
] = cnf
->enhanced_dad
;
5644 array
[DEVCONF_ADDR_GEN_MODE
] = cnf
->addr_gen_mode
;
5645 array
[DEVCONF_DISABLE_POLICY
] = cnf
->disable_policy
;
5646 array
[DEVCONF_NDISC_TCLASS
] = cnf
->ndisc_tclass
;
5647 array
[DEVCONF_RPL_SEG_ENABLED
] = cnf
->rpl_seg_enabled
;
5648 array
[DEVCONF_IOAM6_ENABLED
] = cnf
->ioam6_enabled
;
5649 array
[DEVCONF_IOAM6_ID
] = cnf
->ioam6_id
;
5650 array
[DEVCONF_IOAM6_ID_WIDE
] = cnf
->ioam6_id_wide
;
5651 array
[DEVCONF_NDISC_EVICT_NOCARRIER
] = cnf
->ndisc_evict_nocarrier
;
5652 array
[DEVCONF_ACCEPT_UNTRACKED_NA
] = cnf
->accept_untracked_na
;
5653 array
[DEVCONF_ACCEPT_RA_MIN_LFT
] = cnf
->accept_ra_min_lft
;
5656 static inline size_t inet6_ifla6_size(void)
5658 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5659 + nla_total_size(sizeof(struct ifla_cacheinfo
))
5660 + nla_total_size(DEVCONF_MAX
* 4) /* IFLA_INET6_CONF */
5661 + nla_total_size(IPSTATS_MIB_MAX
* 8) /* IFLA_INET6_STATS */
5662 + nla_total_size(ICMP6_MIB_MAX
* 8) /* IFLA_INET6_ICMP6STATS */
5663 + nla_total_size(sizeof(struct in6_addr
)) /* IFLA_INET6_TOKEN */
5664 + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5665 + nla_total_size(4) /* IFLA_INET6_RA_MTU */
5669 static inline size_t inet6_if_nlmsg_size(void)
5671 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
5672 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
5673 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
5674 + nla_total_size(4) /* IFLA_MTU */
5675 + nla_total_size(4) /* IFLA_LINK */
5676 + nla_total_size(1) /* IFLA_OPERSTATE */
5677 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5680 static inline void __snmp6_fill_statsdev(u64
*stats
, atomic_long_t
*mib
,
5684 int pad
= bytes
- sizeof(u64
) * ICMP6_MIB_MAX
;
5687 /* Use put_unaligned() because stats may not be aligned for u64. */
5688 put_unaligned(ICMP6_MIB_MAX
, &stats
[0]);
5689 for (i
= 1; i
< ICMP6_MIB_MAX
; i
++)
5690 put_unaligned(atomic_long_read(&mib
[i
]), &stats
[i
]);
5692 memset(&stats
[ICMP6_MIB_MAX
], 0, pad
);
5695 static inline void __snmp6_fill_stats64(u64
*stats
, void __percpu
*mib
,
5696 int bytes
, size_t syncpoff
)
5699 u64 buff
[IPSTATS_MIB_MAX
];
5700 int pad
= bytes
- sizeof(u64
) * IPSTATS_MIB_MAX
;
5704 memset(buff
, 0, sizeof(buff
));
5705 buff
[0] = IPSTATS_MIB_MAX
;
5707 for_each_possible_cpu(c
) {
5708 for (i
= 1; i
< IPSTATS_MIB_MAX
; i
++)
5709 buff
[i
] += snmp_get_cpu_field64(mib
, c
, i
, syncpoff
);
5712 memcpy(stats
, buff
, IPSTATS_MIB_MAX
* sizeof(u64
));
5713 memset(&stats
[IPSTATS_MIB_MAX
], 0, pad
);
5716 static void snmp6_fill_stats(u64
*stats
, struct inet6_dev
*idev
, int attrtype
,
5720 case IFLA_INET6_STATS
:
5721 __snmp6_fill_stats64(stats
, idev
->stats
.ipv6
, bytes
,
5722 offsetof(struct ipstats_mib
, syncp
));
5724 case IFLA_INET6_ICMP6STATS
:
5725 __snmp6_fill_statsdev(stats
, idev
->stats
.icmpv6dev
->mibs
, bytes
);
5730 static int inet6_fill_ifla6_attrs(struct sk_buff
*skb
, struct inet6_dev
*idev
,
5731 u32 ext_filter_mask
)
5734 struct ifla_cacheinfo ci
;
5736 if (nla_put_u32(skb
, IFLA_INET6_FLAGS
, idev
->if_flags
))
5737 goto nla_put_failure
;
5738 ci
.max_reasm_len
= IPV6_MAXPLEN
;
5739 ci
.tstamp
= cstamp_delta(idev
->tstamp
);
5740 ci
.reachable_time
= jiffies_to_msecs(idev
->nd_parms
->reachable_time
);
5741 ci
.retrans_time
= jiffies_to_msecs(NEIGH_VAR(idev
->nd_parms
, RETRANS_TIME
));
5742 if (nla_put(skb
, IFLA_INET6_CACHEINFO
, sizeof(ci
), &ci
))
5743 goto nla_put_failure
;
5744 nla
= nla_reserve(skb
, IFLA_INET6_CONF
, DEVCONF_MAX
* sizeof(s32
));
5746 goto nla_put_failure
;
5747 ipv6_store_devconf(&idev
->cnf
, nla_data(nla
), nla_len(nla
));
5749 /* XXX - MC not implemented */
5751 if (ext_filter_mask
& RTEXT_FILTER_SKIP_STATS
)
5754 nla
= nla_reserve(skb
, IFLA_INET6_STATS
, IPSTATS_MIB_MAX
* sizeof(u64
));
5756 goto nla_put_failure
;
5757 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_STATS
, nla_len(nla
));
5759 nla
= nla_reserve(skb
, IFLA_INET6_ICMP6STATS
, ICMP6_MIB_MAX
* sizeof(u64
));
5761 goto nla_put_failure
;
5762 snmp6_fill_stats(nla_data(nla
), idev
, IFLA_INET6_ICMP6STATS
, nla_len(nla
));
5764 nla
= nla_reserve(skb
, IFLA_INET6_TOKEN
, sizeof(struct in6_addr
));
5766 goto nla_put_failure
;
5767 read_lock_bh(&idev
->lock
);
5768 memcpy(nla_data(nla
), idev
->token
.s6_addr
, nla_len(nla
));
5769 read_unlock_bh(&idev
->lock
);
5771 if (nla_put_u8(skb
, IFLA_INET6_ADDR_GEN_MODE
, idev
->cnf
.addr_gen_mode
))
5772 goto nla_put_failure
;
5775 nla_put_u32(skb
, IFLA_INET6_RA_MTU
, idev
->ra_mtu
))
5776 goto nla_put_failure
;
5784 static size_t inet6_get_link_af_size(const struct net_device
*dev
,
5785 u32 ext_filter_mask
)
5787 if (!__in6_dev_get(dev
))
5790 return inet6_ifla6_size();
5793 static int inet6_fill_link_af(struct sk_buff
*skb
, const struct net_device
*dev
,
5794 u32 ext_filter_mask
)
5796 struct inet6_dev
*idev
= __in6_dev_get(dev
);
5801 if (inet6_fill_ifla6_attrs(skb
, idev
, ext_filter_mask
) < 0)
5807 static int inet6_set_iftoken(struct inet6_dev
*idev
, struct in6_addr
*token
,
5808 struct netlink_ext_ack
*extack
)
5810 struct inet6_ifaddr
*ifp
;
5811 struct net_device
*dev
= idev
->dev
;
5812 bool clear_token
, update_rs
= false;
5813 struct in6_addr ll_addr
;
5820 if (dev
->flags
& IFF_LOOPBACK
) {
5821 NL_SET_ERR_MSG_MOD(extack
, "Device is loopback");
5825 if (dev
->flags
& IFF_NOARP
) {
5826 NL_SET_ERR_MSG_MOD(extack
,
5827 "Device does not do neighbour discovery");
5831 if (!ipv6_accept_ra(idev
)) {
5832 NL_SET_ERR_MSG_MOD(extack
,
5833 "Router advertisement is disabled on device");
5837 if (idev
->cnf
.rtr_solicits
== 0) {
5838 NL_SET_ERR_MSG(extack
,
5839 "Router solicitation is disabled on device");
5843 write_lock_bh(&idev
->lock
);
5845 BUILD_BUG_ON(sizeof(token
->s6_addr
) != 16);
5846 memcpy(idev
->token
.s6_addr
+ 8, token
->s6_addr
+ 8, 8);
5848 write_unlock_bh(&idev
->lock
);
5850 clear_token
= ipv6_addr_any(token
);
5854 if (!idev
->dead
&& (idev
->if_flags
& IF_READY
) &&
5855 !ipv6_get_lladdr(dev
, &ll_addr
, IFA_F_TENTATIVE
|
5856 IFA_F_OPTIMISTIC
)) {
5857 /* If we're not ready, then normal ifup will take care
5858 * of this. Otherwise, we need to request our rs here.
5860 ndisc_send_rs(dev
, &ll_addr
, &in6addr_linklocal_allrouters
);
5865 write_lock_bh(&idev
->lock
);
5868 idev
->if_flags
|= IF_RS_SENT
;
5869 idev
->rs_interval
= rfc3315_s14_backoff_init(
5870 idev
->cnf
.rtr_solicit_interval
);
5871 idev
->rs_probes
= 1;
5872 addrconf_mod_rs_timer(idev
, idev
->rs_interval
);
5875 /* Well, that's kinda nasty ... */
5876 list_for_each_entry(ifp
, &idev
->addr_list
, if_list
) {
5877 spin_lock(&ifp
->lock
);
5878 if (ifp
->tokenized
) {
5880 ifp
->prefered_lft
= 0;
5882 spin_unlock(&ifp
->lock
);
5885 write_unlock_bh(&idev
->lock
);
5886 inet6_ifinfo_notify(RTM_NEWLINK
, idev
);
5887 addrconf_verify_rtnl(dev_net(dev
));
5891 static const struct nla_policy inet6_af_policy
[IFLA_INET6_MAX
+ 1] = {
5892 [IFLA_INET6_ADDR_GEN_MODE
] = { .type
= NLA_U8
},
5893 [IFLA_INET6_TOKEN
] = { .len
= sizeof(struct in6_addr
) },
5894 [IFLA_INET6_RA_MTU
] = { .type
= NLA_REJECT
,
5896 "IFLA_INET6_RA_MTU can not be set" },
5899 static int check_addr_gen_mode(int mode
)
5901 if (mode
!= IN6_ADDR_GEN_MODE_EUI64
&&
5902 mode
!= IN6_ADDR_GEN_MODE_NONE
&&
5903 mode
!= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
&&
5904 mode
!= IN6_ADDR_GEN_MODE_RANDOM
)
5909 static int check_stable_privacy(struct inet6_dev
*idev
, struct net
*net
,
5912 if (mode
== IN6_ADDR_GEN_MODE_STABLE_PRIVACY
&&
5913 !idev
->cnf
.stable_secret
.initialized
&&
5914 !net
->ipv6
.devconf_dflt
->stable_secret
.initialized
)
5919 static int inet6_validate_link_af(const struct net_device
*dev
,
5920 const struct nlattr
*nla
,
5921 struct netlink_ext_ack
*extack
)
5923 struct nlattr
*tb
[IFLA_INET6_MAX
+ 1];
5924 struct inet6_dev
*idev
= NULL
;
5928 idev
= __in6_dev_get(dev
);
5930 return -EAFNOSUPPORT
;
5933 err
= nla_parse_nested_deprecated(tb
, IFLA_INET6_MAX
, nla
,
5934 inet6_af_policy
, extack
);
5938 if (!tb
[IFLA_INET6_TOKEN
] && !tb
[IFLA_INET6_ADDR_GEN_MODE
])
5941 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
5942 u8 mode
= nla_get_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
5944 if (check_addr_gen_mode(mode
) < 0)
5946 if (dev
&& check_stable_privacy(idev
, dev_net(dev
), mode
) < 0)
5953 static int inet6_set_link_af(struct net_device
*dev
, const struct nlattr
*nla
,
5954 struct netlink_ext_ack
*extack
)
5956 struct inet6_dev
*idev
= __in6_dev_get(dev
);
5957 struct nlattr
*tb
[IFLA_INET6_MAX
+ 1];
5961 return -EAFNOSUPPORT
;
5963 if (nla_parse_nested_deprecated(tb
, IFLA_INET6_MAX
, nla
, NULL
, NULL
) < 0)
5966 if (tb
[IFLA_INET6_TOKEN
]) {
5967 err
= inet6_set_iftoken(idev
, nla_data(tb
[IFLA_INET6_TOKEN
]),
5973 if (tb
[IFLA_INET6_ADDR_GEN_MODE
]) {
5974 u8 mode
= nla_get_u8(tb
[IFLA_INET6_ADDR_GEN_MODE
]);
5976 idev
->cnf
.addr_gen_mode
= mode
;
5982 static int inet6_fill_ifinfo(struct sk_buff
*skb
, struct inet6_dev
*idev
,
5983 u32 portid
, u32 seq
, int event
, unsigned int flags
)
5985 struct net_device
*dev
= idev
->dev
;
5986 struct ifinfomsg
*hdr
;
5987 struct nlmsghdr
*nlh
;
5990 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*hdr
), flags
);
5994 hdr
= nlmsg_data(nlh
);
5995 hdr
->ifi_family
= AF_INET6
;
5997 hdr
->ifi_type
= dev
->type
;
5998 hdr
->ifi_index
= dev
->ifindex
;
5999 hdr
->ifi_flags
= dev_get_flags(dev
);
6000 hdr
->ifi_change
= 0;
6002 if (nla_put_string(skb
, IFLA_IFNAME
, dev
->name
) ||
6004 nla_put(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
)) ||
6005 nla_put_u32(skb
, IFLA_MTU
, dev
->mtu
) ||
6006 (dev
->ifindex
!= dev_get_iflink(dev
) &&
6007 nla_put_u32(skb
, IFLA_LINK
, dev_get_iflink(dev
))) ||
6008 nla_put_u8(skb
, IFLA_OPERSTATE
,
6009 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
))
6010 goto nla_put_failure
;
6011 protoinfo
= nla_nest_start_noflag(skb
, IFLA_PROTINFO
);
6013 goto nla_put_failure
;
6015 if (inet6_fill_ifla6_attrs(skb
, idev
, 0) < 0)
6016 goto nla_put_failure
;
6018 nla_nest_end(skb
, protoinfo
);
6019 nlmsg_end(skb
, nlh
);
6023 nlmsg_cancel(skb
, nlh
);
6027 static int inet6_valid_dump_ifinfo(const struct nlmsghdr
*nlh
,
6028 struct netlink_ext_ack
*extack
)
6030 struct ifinfomsg
*ifm
;
6032 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
6033 NL_SET_ERR_MSG_MOD(extack
, "Invalid header for link dump request");
6037 if (nlmsg_attrlen(nlh
, sizeof(*ifm
))) {
6038 NL_SET_ERR_MSG_MOD(extack
, "Invalid data after header");
6042 ifm
= nlmsg_data(nlh
);
6043 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
6044 ifm
->ifi_change
|| ifm
->ifi_index
) {
6045 NL_SET_ERR_MSG_MOD(extack
, "Invalid values in header for dump request");
6052 static int inet6_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
6054 struct net
*net
= sock_net(skb
->sk
);
6057 struct net_device
*dev
;
6058 struct inet6_dev
*idev
;
6059 struct hlist_head
*head
;
6061 /* only requests using strict checking can pass data to
6062 * influence the dump
6064 if (cb
->strict_check
) {
6065 int err
= inet6_valid_dump_ifinfo(cb
->nlh
, cb
->extack
);
6072 s_idx
= cb
->args
[1];
6075 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
6077 head
= &net
->dev_index_head
[h
];
6078 hlist_for_each_entry_rcu(dev
, head
, index_hlist
) {
6081 idev
= __in6_dev_get(dev
);
6084 if (inet6_fill_ifinfo(skb
, idev
,
6085 NETLINK_CB(cb
->skb
).portid
,
6087 RTM_NEWLINK
, NLM_F_MULTI
) < 0)
6101 void inet6_ifinfo_notify(int event
, struct inet6_dev
*idev
)
6103 struct sk_buff
*skb
;
6104 struct net
*net
= dev_net(idev
->dev
);
6107 skb
= nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC
);
6111 err
= inet6_fill_ifinfo(skb
, idev
, 0, 0, event
, 0);
6113 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
6114 WARN_ON(err
== -EMSGSIZE
);
6118 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_IFINFO
, NULL
, GFP_ATOMIC
);
6122 rtnl_set_sk_err(net
, RTNLGRP_IPV6_IFINFO
, err
);
6125 static inline size_t inet6_prefix_nlmsg_size(void)
6127 return NLMSG_ALIGN(sizeof(struct prefixmsg
))
6128 + nla_total_size(sizeof(struct in6_addr
))
6129 + nla_total_size(sizeof(struct prefix_cacheinfo
));
6132 static int inet6_fill_prefix(struct sk_buff
*skb
, struct inet6_dev
*idev
,
6133 struct prefix_info
*pinfo
, u32 portid
, u32 seq
,
6134 int event
, unsigned int flags
)
6136 struct prefixmsg
*pmsg
;
6137 struct nlmsghdr
*nlh
;
6138 struct prefix_cacheinfo ci
;
6140 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*pmsg
), flags
);
6144 pmsg
= nlmsg_data(nlh
);
6145 pmsg
->prefix_family
= AF_INET6
;
6146 pmsg
->prefix_pad1
= 0;
6147 pmsg
->prefix_pad2
= 0;
6148 pmsg
->prefix_ifindex
= idev
->dev
->ifindex
;
6149 pmsg
->prefix_len
= pinfo
->prefix_len
;
6150 pmsg
->prefix_type
= pinfo
->type
;
6151 pmsg
->prefix_pad3
= 0;
6152 pmsg
->prefix_flags
= 0;
6154 pmsg
->prefix_flags
|= IF_PREFIX_ONLINK
;
6155 if (pinfo
->autoconf
)
6156 pmsg
->prefix_flags
|= IF_PREFIX_AUTOCONF
;
6158 if (nla_put(skb
, PREFIX_ADDRESS
, sizeof(pinfo
->prefix
), &pinfo
->prefix
))
6159 goto nla_put_failure
;
6160 ci
.preferred_time
= ntohl(pinfo
->prefered
);
6161 ci
.valid_time
= ntohl(pinfo
->valid
);
6162 if (nla_put(skb
, PREFIX_CACHEINFO
, sizeof(ci
), &ci
))
6163 goto nla_put_failure
;
6164 nlmsg_end(skb
, nlh
);
6168 nlmsg_cancel(skb
, nlh
);
6172 static void inet6_prefix_notify(int event
, struct inet6_dev
*idev
,
6173 struct prefix_info
*pinfo
)
6175 struct sk_buff
*skb
;
6176 struct net
*net
= dev_net(idev
->dev
);
6179 skb
= nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC
);
6183 err
= inet6_fill_prefix(skb
, idev
, pinfo
, 0, 0, event
, 0);
6185 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
6186 WARN_ON(err
== -EMSGSIZE
);
6190 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_PREFIX
, NULL
, GFP_ATOMIC
);
6194 rtnl_set_sk_err(net
, RTNLGRP_IPV6_PREFIX
, err
);
6197 static void __ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
6199 struct net
*net
= dev_net(ifp
->idev
->dev
);
6204 inet6_ifa_notify(event
? : RTM_NEWADDR
, ifp
);
6209 * If the address was optimistic we inserted the route at the
6210 * start of our DAD process, so we don't need to do it again.
6211 * If the device was taken down in the middle of the DAD
6212 * cycle there is a race where we could get here without a
6213 * host route, so nothing to insert. That will be fixed when
6214 * the device is brought up.
6216 if (ifp
->rt
&& !rcu_access_pointer(ifp
->rt
->fib6_node
)) {
6217 ip6_ins_rt(net
, ifp
->rt
);
6218 } else if (!ifp
->rt
&& (ifp
->idev
->dev
->flags
& IFF_UP
)) {
6219 pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
6220 &ifp
->addr
, ifp
->idev
->dev
->name
);
6223 if (ifp
->idev
->cnf
.forwarding
)
6224 addrconf_join_anycast(ifp
);
6225 if (!ipv6_addr_any(&ifp
->peer_addr
))
6226 addrconf_prefix_route(&ifp
->peer_addr
, 128,
6227 ifp
->rt_priority
, ifp
->idev
->dev
,
6231 if (ifp
->idev
->cnf
.forwarding
)
6232 addrconf_leave_anycast(ifp
);
6233 addrconf_leave_solict(ifp
->idev
, &ifp
->addr
);
6234 if (!ipv6_addr_any(&ifp
->peer_addr
)) {
6235 struct fib6_info
*rt
;
6237 rt
= addrconf_get_prefix_route(&ifp
->peer_addr
, 128,
6238 ifp
->idev
->dev
, 0, 0,
6241 ip6_del_rt(net
, rt
, false);
6244 ip6_del_rt(net
, ifp
->rt
, false);
6247 rt_genid_bump_ipv6(net
);
6250 atomic_inc(&net
->ipv6
.dev_addr_genid
);
6253 static void ipv6_ifa_notify(int event
, struct inet6_ifaddr
*ifp
)
6255 if (likely(ifp
->idev
->dead
== 0))
6256 __ipv6_ifa_notify(event
, ifp
);
6259 #ifdef CONFIG_SYSCTL
6261 static int addrconf_sysctl_forward(struct ctl_table
*ctl
, int write
,
6262 void *buffer
, size_t *lenp
, loff_t
*ppos
)
6264 int *valp
= ctl
->data
;
6267 struct ctl_table lctl
;
6271 * ctl->data points to idev->cnf.forwarding, we should
6272 * not modify it until we get the rtnl lock.
6277 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
6280 ret
= addrconf_fixup_forwarding(ctl
, valp
, val
);
6286 static int addrconf_sysctl_mtu(struct ctl_table
*ctl
, int write
,
6287 void *buffer
, size_t *lenp
, loff_t
*ppos
)
6289 struct inet6_dev
*idev
= ctl
->extra1
;
6290 int min_mtu
= IPV6_MIN_MTU
;
6291 struct ctl_table lctl
;
6294 lctl
.extra1
= &min_mtu
;
6295 lctl
.extra2
= idev
? &idev
->dev
->mtu
: NULL
;
6297 return proc_dointvec_minmax(&lctl
, write
, buffer
, lenp
, ppos
);
6300 static void dev_disable_change(struct inet6_dev
*idev
)
6302 struct netdev_notifier_info info
;
6304 if (!idev
|| !idev
->dev
)
6307 netdev_notifier_info_init(&info
, idev
->dev
);
6308 if (idev
->cnf
.disable_ipv6
)
6309 addrconf_notify(NULL
, NETDEV_DOWN
, &info
);
6311 addrconf_notify(NULL
, NETDEV_UP
, &info
);
6314 static void addrconf_disable_change(struct net
*net
, __s32 newf
)
6316 struct net_device
*dev
;
6317 struct inet6_dev
*idev
;
6319 for_each_netdev(net
, dev
) {
6320 idev
= __in6_dev_get(dev
);
6322 int changed
= (!idev
->cnf
.disable_ipv6
) ^ (!newf
);
6323 idev
->cnf
.disable_ipv6
= newf
;
6325 dev_disable_change(idev
);
6330 static int addrconf_disable_ipv6(struct ctl_table
*table
, int *p
, int newf
)
6335 if (!rtnl_trylock())
6336 return restart_syscall();
6338 net
= (struct net
*)table
->extra2
;
6342 if (p
== &net
->ipv6
.devconf_dflt
->disable_ipv6
) {
6347 if (p
== &net
->ipv6
.devconf_all
->disable_ipv6
) {
6348 net
->ipv6
.devconf_dflt
->disable_ipv6
= newf
;
6349 addrconf_disable_change(net
, newf
);
6350 } else if ((!newf
) ^ (!old
))
6351 dev_disable_change((struct inet6_dev
*)table
->extra1
);
6357 static int addrconf_sysctl_disable(struct ctl_table
*ctl
, int write
,
6358 void *buffer
, size_t *lenp
, loff_t
*ppos
)
6360 int *valp
= ctl
->data
;
6363 struct ctl_table lctl
;
6367 * ctl->data points to idev->cnf.disable_ipv6, we should
6368 * not modify it until we get the rtnl lock.
6373 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
6376 ret
= addrconf_disable_ipv6(ctl
, valp
, val
);
6382 static int addrconf_sysctl_proxy_ndp(struct ctl_table
*ctl
, int write
,
6383 void *buffer
, size_t *lenp
, loff_t
*ppos
)
6385 int *valp
= ctl
->data
;
6390 ret
= proc_dointvec(ctl
, write
, buffer
, lenp
, ppos
);
6393 if (write
&& old
!= new) {
6394 struct net
*net
= ctl
->extra2
;
6396 if (!rtnl_trylock())
6397 return restart_syscall();
6399 if (valp
== &net
->ipv6
.devconf_dflt
->proxy_ndp
)
6400 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
6401 NETCONFA_PROXY_NEIGH
,
6402 NETCONFA_IFINDEX_DEFAULT
,
6403 net
->ipv6
.devconf_dflt
);
6404 else if (valp
== &net
->ipv6
.devconf_all
->proxy_ndp
)
6405 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
6406 NETCONFA_PROXY_NEIGH
,
6407 NETCONFA_IFINDEX_ALL
,
6408 net
->ipv6
.devconf_all
);
6410 struct inet6_dev
*idev
= ctl
->extra1
;
6412 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
6413 NETCONFA_PROXY_NEIGH
,
6423 static int addrconf_sysctl_addr_gen_mode(struct ctl_table
*ctl
, int write
,
6424 void *buffer
, size_t *lenp
,
6429 struct inet6_dev
*idev
= (struct inet6_dev
*)ctl
->extra1
;
6430 struct net
*net
= (struct net
*)ctl
->extra2
;
6431 struct ctl_table tmp
= {
6433 .maxlen
= sizeof(new_val
),
6437 if (!rtnl_trylock())
6438 return restart_syscall();
6440 new_val
= *((u32
*)ctl
->data
);
6442 ret
= proc_douintvec(&tmp
, write
, buffer
, lenp
, ppos
);
6447 if (check_addr_gen_mode(new_val
) < 0) {
6453 if (check_stable_privacy(idev
, net
, new_val
) < 0) {
6458 if (idev
->cnf
.addr_gen_mode
!= new_val
) {
6459 idev
->cnf
.addr_gen_mode
= new_val
;
6460 addrconf_init_auto_addrs(idev
->dev
);
6462 } else if (&net
->ipv6
.devconf_all
->addr_gen_mode
== ctl
->data
) {
6463 struct net_device
*dev
;
6465 net
->ipv6
.devconf_dflt
->addr_gen_mode
= new_val
;
6466 for_each_netdev(net
, dev
) {
6467 idev
= __in6_dev_get(dev
);
6469 idev
->cnf
.addr_gen_mode
!= new_val
) {
6470 idev
->cnf
.addr_gen_mode
= new_val
;
6471 addrconf_init_auto_addrs(idev
->dev
);
6476 *((u32
*)ctl
->data
) = new_val
;
6485 static int addrconf_sysctl_stable_secret(struct ctl_table
*ctl
, int write
,
6486 void *buffer
, size_t *lenp
,
6490 struct in6_addr addr
;
6491 char str
[IPV6_MAX_STRLEN
];
6492 struct ctl_table lctl
= *ctl
;
6493 struct net
*net
= ctl
->extra2
;
6494 struct ipv6_stable_secret
*secret
= ctl
->data
;
6496 if (&net
->ipv6
.devconf_all
->stable_secret
== ctl
->data
)
6499 lctl
.maxlen
= IPV6_MAX_STRLEN
;
6502 if (!rtnl_trylock())
6503 return restart_syscall();
6505 if (!write
&& !secret
->initialized
) {
6510 err
= snprintf(str
, sizeof(str
), "%pI6", &secret
->secret
);
6511 if (err
>= sizeof(str
)) {
6516 err
= proc_dostring(&lctl
, write
, buffer
, lenp
, ppos
);
6520 if (in6_pton(str
, -1, addr
.in6_u
.u6_addr8
, -1, NULL
) != 1) {
6525 secret
->initialized
= true;
6526 secret
->secret
= addr
;
6528 if (&net
->ipv6
.devconf_dflt
->stable_secret
== ctl
->data
) {
6529 struct net_device
*dev
;
6531 for_each_netdev(net
, dev
) {
6532 struct inet6_dev
*idev
= __in6_dev_get(dev
);
6535 idev
->cnf
.addr_gen_mode
=
6536 IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
6540 struct inet6_dev
*idev
= ctl
->extra1
;
6542 idev
->cnf
.addr_gen_mode
= IN6_ADDR_GEN_MODE_STABLE_PRIVACY
;
6552 int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table
*ctl
,
6553 int write
, void *buffer
,
6557 int *valp
= ctl
->data
;
6560 struct ctl_table lctl
;
6563 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
6564 * we should not modify it until we get the rtnl lock.
6569 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
6572 ret
= addrconf_fixup_linkdown(ctl
, valp
, val
);
6579 void addrconf_set_nopolicy(struct rt6_info
*rt
, int action
)
6583 rt
->dst
.flags
|= DST_NOPOLICY
;
6585 rt
->dst
.flags
&= ~DST_NOPOLICY
;
6590 void addrconf_disable_policy_idev(struct inet6_dev
*idev
, int val
)
6592 struct inet6_ifaddr
*ifa
;
6594 read_lock_bh(&idev
->lock
);
6595 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
6596 spin_lock(&ifa
->lock
);
6598 /* host routes only use builtin fib6_nh */
6599 struct fib6_nh
*nh
= ifa
->rt
->fib6_nh
;
6603 ifa
->rt
->dst_nopolicy
= val
? true : false;
6604 if (nh
->rt6i_pcpu
) {
6605 for_each_possible_cpu(cpu
) {
6606 struct rt6_info
**rtp
;
6608 rtp
= per_cpu_ptr(nh
->rt6i_pcpu
, cpu
);
6609 addrconf_set_nopolicy(*rtp
, val
);
6614 spin_unlock(&ifa
->lock
);
6616 read_unlock_bh(&idev
->lock
);
6620 int addrconf_disable_policy(struct ctl_table
*ctl
, int *valp
, int val
)
6622 struct inet6_dev
*idev
;
6625 if (!rtnl_trylock())
6626 return restart_syscall();
6630 net
= (struct net
*)ctl
->extra2
;
6631 if (valp
== &net
->ipv6
.devconf_dflt
->disable_policy
) {
6636 if (valp
== &net
->ipv6
.devconf_all
->disable_policy
) {
6637 struct net_device
*dev
;
6639 for_each_netdev(net
, dev
) {
6640 idev
= __in6_dev_get(dev
);
6642 addrconf_disable_policy_idev(idev
, val
);
6645 idev
= (struct inet6_dev
*)ctl
->extra1
;
6646 addrconf_disable_policy_idev(idev
, val
);
6653 static int addrconf_sysctl_disable_policy(struct ctl_table
*ctl
, int write
,
6654 void *buffer
, size_t *lenp
, loff_t
*ppos
)
6656 int *valp
= ctl
->data
;
6659 struct ctl_table lctl
;
6664 ret
= proc_dointvec(&lctl
, write
, buffer
, lenp
, ppos
);
6666 if (write
&& (*valp
!= val
))
6667 ret
= addrconf_disable_policy(ctl
, valp
, val
);
6675 static int minus_one
= -1;
6676 static const int two_five_five
= 255;
6677 static u32 ioam6_if_id_max
= U16_MAX
;
6679 static const struct ctl_table addrconf_sysctl
[] = {
6681 .procname
= "forwarding",
6682 .data
= &ipv6_devconf
.forwarding
,
6683 .maxlen
= sizeof(int),
6685 .proc_handler
= addrconf_sysctl_forward
,
6688 .procname
= "hop_limit",
6689 .data
= &ipv6_devconf
.hop_limit
,
6690 .maxlen
= sizeof(int),
6692 .proc_handler
= proc_dointvec_minmax
,
6693 .extra1
= (void *)SYSCTL_ONE
,
6694 .extra2
= (void *)&two_five_five
,
6698 .data
= &ipv6_devconf
.mtu6
,
6699 .maxlen
= sizeof(int),
6701 .proc_handler
= addrconf_sysctl_mtu
,
6704 .procname
= "accept_ra",
6705 .data
= &ipv6_devconf
.accept_ra
,
6706 .maxlen
= sizeof(int),
6708 .proc_handler
= proc_dointvec
,
6711 .procname
= "accept_redirects",
6712 .data
= &ipv6_devconf
.accept_redirects
,
6713 .maxlen
= sizeof(int),
6715 .proc_handler
= proc_dointvec
,
6718 .procname
= "autoconf",
6719 .data
= &ipv6_devconf
.autoconf
,
6720 .maxlen
= sizeof(int),
6722 .proc_handler
= proc_dointvec
,
6725 .procname
= "dad_transmits",
6726 .data
= &ipv6_devconf
.dad_transmits
,
6727 .maxlen
= sizeof(int),
6729 .proc_handler
= proc_dointvec
,
6732 .procname
= "router_solicitations",
6733 .data
= &ipv6_devconf
.rtr_solicits
,
6734 .maxlen
= sizeof(int),
6736 .proc_handler
= proc_dointvec_minmax
,
6737 .extra1
= &minus_one
,
6740 .procname
= "router_solicitation_interval",
6741 .data
= &ipv6_devconf
.rtr_solicit_interval
,
6742 .maxlen
= sizeof(int),
6744 .proc_handler
= proc_dointvec_jiffies
,
6747 .procname
= "router_solicitation_max_interval",
6748 .data
= &ipv6_devconf
.rtr_solicit_max_interval
,
6749 .maxlen
= sizeof(int),
6751 .proc_handler
= proc_dointvec_jiffies
,
6754 .procname
= "router_solicitation_delay",
6755 .data
= &ipv6_devconf
.rtr_solicit_delay
,
6756 .maxlen
= sizeof(int),
6758 .proc_handler
= proc_dointvec_jiffies
,
6761 .procname
= "force_mld_version",
6762 .data
= &ipv6_devconf
.force_mld_version
,
6763 .maxlen
= sizeof(int),
6765 .proc_handler
= proc_dointvec
,
6768 .procname
= "mldv1_unsolicited_report_interval",
6770 &ipv6_devconf
.mldv1_unsolicited_report_interval
,
6771 .maxlen
= sizeof(int),
6773 .proc_handler
= proc_dointvec_ms_jiffies
,
6776 .procname
= "mldv2_unsolicited_report_interval",
6778 &ipv6_devconf
.mldv2_unsolicited_report_interval
,
6779 .maxlen
= sizeof(int),
6781 .proc_handler
= proc_dointvec_ms_jiffies
,
6784 .procname
= "use_tempaddr",
6785 .data
= &ipv6_devconf
.use_tempaddr
,
6786 .maxlen
= sizeof(int),
6788 .proc_handler
= proc_dointvec
,
6791 .procname
= "temp_valid_lft",
6792 .data
= &ipv6_devconf
.temp_valid_lft
,
6793 .maxlen
= sizeof(int),
6795 .proc_handler
= proc_dointvec
,
6798 .procname
= "temp_prefered_lft",
6799 .data
= &ipv6_devconf
.temp_prefered_lft
,
6800 .maxlen
= sizeof(int),
6802 .proc_handler
= proc_dointvec
,
6805 .procname
= "regen_max_retry",
6806 .data
= &ipv6_devconf
.regen_max_retry
,
6807 .maxlen
= sizeof(int),
6809 .proc_handler
= proc_dointvec
,
6812 .procname
= "max_desync_factor",
6813 .data
= &ipv6_devconf
.max_desync_factor
,
6814 .maxlen
= sizeof(int),
6816 .proc_handler
= proc_dointvec
,
6819 .procname
= "max_addresses",
6820 .data
= &ipv6_devconf
.max_addresses
,
6821 .maxlen
= sizeof(int),
6823 .proc_handler
= proc_dointvec
,
6826 .procname
= "accept_ra_defrtr",
6827 .data
= &ipv6_devconf
.accept_ra_defrtr
,
6828 .maxlen
= sizeof(int),
6830 .proc_handler
= proc_dointvec
,
6833 .procname
= "ra_defrtr_metric",
6834 .data
= &ipv6_devconf
.ra_defrtr_metric
,
6835 .maxlen
= sizeof(u32
),
6837 .proc_handler
= proc_douintvec_minmax
,
6838 .extra1
= (void *)SYSCTL_ONE
,
6841 .procname
= "accept_ra_min_hop_limit",
6842 .data
= &ipv6_devconf
.accept_ra_min_hop_limit
,
6843 .maxlen
= sizeof(int),
6845 .proc_handler
= proc_dointvec
,
6848 .procname
= "accept_ra_min_lft",
6849 .data
= &ipv6_devconf
.accept_ra_min_lft
,
6850 .maxlen
= sizeof(int),
6852 .proc_handler
= proc_dointvec
,
6855 .procname
= "accept_ra_pinfo",
6856 .data
= &ipv6_devconf
.accept_ra_pinfo
,
6857 .maxlen
= sizeof(int),
6859 .proc_handler
= proc_dointvec
,
6862 .procname
= "ra_honor_pio_life",
6863 .data
= &ipv6_devconf
.ra_honor_pio_life
,
6864 .maxlen
= sizeof(u8
),
6866 .proc_handler
= proc_dou8vec_minmax
,
6867 .extra1
= SYSCTL_ZERO
,
6868 .extra2
= SYSCTL_ONE
,
6870 #ifdef CONFIG_IPV6_ROUTER_PREF
6872 .procname
= "accept_ra_rtr_pref",
6873 .data
= &ipv6_devconf
.accept_ra_rtr_pref
,
6874 .maxlen
= sizeof(int),
6876 .proc_handler
= proc_dointvec
,
6879 .procname
= "router_probe_interval",
6880 .data
= &ipv6_devconf
.rtr_probe_interval
,
6881 .maxlen
= sizeof(int),
6883 .proc_handler
= proc_dointvec_jiffies
,
6885 #ifdef CONFIG_IPV6_ROUTE_INFO
6887 .procname
= "accept_ra_rt_info_min_plen",
6888 .data
= &ipv6_devconf
.accept_ra_rt_info_min_plen
,
6889 .maxlen
= sizeof(int),
6891 .proc_handler
= proc_dointvec
,
6894 .procname
= "accept_ra_rt_info_max_plen",
6895 .data
= &ipv6_devconf
.accept_ra_rt_info_max_plen
,
6896 .maxlen
= sizeof(int),
6898 .proc_handler
= proc_dointvec
,
6903 .procname
= "proxy_ndp",
6904 .data
= &ipv6_devconf
.proxy_ndp
,
6905 .maxlen
= sizeof(int),
6907 .proc_handler
= addrconf_sysctl_proxy_ndp
,
6910 .procname
= "accept_source_route",
6911 .data
= &ipv6_devconf
.accept_source_route
,
6912 .maxlen
= sizeof(int),
6914 .proc_handler
= proc_dointvec
,
6916 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
6918 .procname
= "optimistic_dad",
6919 .data
= &ipv6_devconf
.optimistic_dad
,
6920 .maxlen
= sizeof(int),
6922 .proc_handler
= proc_dointvec
,
6925 .procname
= "use_optimistic",
6926 .data
= &ipv6_devconf
.use_optimistic
,
6927 .maxlen
= sizeof(int),
6929 .proc_handler
= proc_dointvec
,
6932 #ifdef CONFIG_IPV6_MROUTE
6934 .procname
= "mc_forwarding",
6935 .data
= &ipv6_devconf
.mc_forwarding
,
6936 .maxlen
= sizeof(int),
6938 .proc_handler
= proc_dointvec
,
6942 .procname
= "disable_ipv6",
6943 .data
= &ipv6_devconf
.disable_ipv6
,
6944 .maxlen
= sizeof(int),
6946 .proc_handler
= addrconf_sysctl_disable
,
6949 .procname
= "accept_dad",
6950 .data
= &ipv6_devconf
.accept_dad
,
6951 .maxlen
= sizeof(int),
6953 .proc_handler
= proc_dointvec
,
6956 .procname
= "force_tllao",
6957 .data
= &ipv6_devconf
.force_tllao
,
6958 .maxlen
= sizeof(int),
6960 .proc_handler
= proc_dointvec
6963 .procname
= "ndisc_notify",
6964 .data
= &ipv6_devconf
.ndisc_notify
,
6965 .maxlen
= sizeof(int),
6967 .proc_handler
= proc_dointvec
6970 .procname
= "suppress_frag_ndisc",
6971 .data
= &ipv6_devconf
.suppress_frag_ndisc
,
6972 .maxlen
= sizeof(int),
6974 .proc_handler
= proc_dointvec
6977 .procname
= "accept_ra_from_local",
6978 .data
= &ipv6_devconf
.accept_ra_from_local
,
6979 .maxlen
= sizeof(int),
6981 .proc_handler
= proc_dointvec
,
6984 .procname
= "accept_ra_mtu",
6985 .data
= &ipv6_devconf
.accept_ra_mtu
,
6986 .maxlen
= sizeof(int),
6988 .proc_handler
= proc_dointvec
,
6991 .procname
= "stable_secret",
6992 .data
= &ipv6_devconf
.stable_secret
,
6993 .maxlen
= IPV6_MAX_STRLEN
,
6995 .proc_handler
= addrconf_sysctl_stable_secret
,
6998 .procname
= "use_oif_addrs_only",
6999 .data
= &ipv6_devconf
.use_oif_addrs_only
,
7000 .maxlen
= sizeof(int),
7002 .proc_handler
= proc_dointvec
,
7005 .procname
= "ignore_routes_with_linkdown",
7006 .data
= &ipv6_devconf
.ignore_routes_with_linkdown
,
7007 .maxlen
= sizeof(int),
7009 .proc_handler
= addrconf_sysctl_ignore_routes_with_linkdown
,
7012 .procname
= "drop_unicast_in_l2_multicast",
7013 .data
= &ipv6_devconf
.drop_unicast_in_l2_multicast
,
7014 .maxlen
= sizeof(int),
7016 .proc_handler
= proc_dointvec
,
7019 .procname
= "drop_unsolicited_na",
7020 .data
= &ipv6_devconf
.drop_unsolicited_na
,
7021 .maxlen
= sizeof(int),
7023 .proc_handler
= proc_dointvec
,
7026 .procname
= "keep_addr_on_down",
7027 .data
= &ipv6_devconf
.keep_addr_on_down
,
7028 .maxlen
= sizeof(int),
7030 .proc_handler
= proc_dointvec
,
7034 .procname
= "seg6_enabled",
7035 .data
= &ipv6_devconf
.seg6_enabled
,
7036 .maxlen
= sizeof(int),
7038 .proc_handler
= proc_dointvec
,
7040 #ifdef CONFIG_IPV6_SEG6_HMAC
7042 .procname
= "seg6_require_hmac",
7043 .data
= &ipv6_devconf
.seg6_require_hmac
,
7044 .maxlen
= sizeof(int),
7046 .proc_handler
= proc_dointvec
,
7050 .procname
= "enhanced_dad",
7051 .data
= &ipv6_devconf
.enhanced_dad
,
7052 .maxlen
= sizeof(int),
7054 .proc_handler
= proc_dointvec
,
7057 .procname
= "addr_gen_mode",
7058 .data
= &ipv6_devconf
.addr_gen_mode
,
7059 .maxlen
= sizeof(int),
7061 .proc_handler
= addrconf_sysctl_addr_gen_mode
,
7064 .procname
= "disable_policy",
7065 .data
= &ipv6_devconf
.disable_policy
,
7066 .maxlen
= sizeof(int),
7068 .proc_handler
= addrconf_sysctl_disable_policy
,
7071 .procname
= "ndisc_tclass",
7072 .data
= &ipv6_devconf
.ndisc_tclass
,
7073 .maxlen
= sizeof(int),
7075 .proc_handler
= proc_dointvec_minmax
,
7076 .extra1
= (void *)SYSCTL_ZERO
,
7077 .extra2
= (void *)&two_five_five
,
7080 .procname
= "rpl_seg_enabled",
7081 .data
= &ipv6_devconf
.rpl_seg_enabled
,
7082 .maxlen
= sizeof(int),
7084 .proc_handler
= proc_dointvec
,
7087 .procname
= "ioam6_enabled",
7088 .data
= &ipv6_devconf
.ioam6_enabled
,
7089 .maxlen
= sizeof(u8
),
7091 .proc_handler
= proc_dou8vec_minmax
,
7092 .extra1
= (void *)SYSCTL_ZERO
,
7093 .extra2
= (void *)SYSCTL_ONE
,
7096 .procname
= "ioam6_id",
7097 .data
= &ipv6_devconf
.ioam6_id
,
7098 .maxlen
= sizeof(u32
),
7100 .proc_handler
= proc_douintvec_minmax
,
7101 .extra1
= (void *)SYSCTL_ZERO
,
7102 .extra2
= (void *)&ioam6_if_id_max
,
7105 .procname
= "ioam6_id_wide",
7106 .data
= &ipv6_devconf
.ioam6_id_wide
,
7107 .maxlen
= sizeof(u32
),
7109 .proc_handler
= proc_douintvec
,
7112 .procname
= "ndisc_evict_nocarrier",
7113 .data
= &ipv6_devconf
.ndisc_evict_nocarrier
,
7114 .maxlen
= sizeof(u8
),
7116 .proc_handler
= proc_dou8vec_minmax
,
7117 .extra1
= (void *)SYSCTL_ZERO
,
7118 .extra2
= (void *)SYSCTL_ONE
,
7121 .procname
= "accept_untracked_na",
7122 .data
= &ipv6_devconf
.accept_untracked_na
,
7123 .maxlen
= sizeof(int),
7125 .proc_handler
= proc_dointvec_minmax
,
7126 .extra1
= SYSCTL_ZERO
,
7127 .extra2
= SYSCTL_TWO
,
7134 static int __addrconf_sysctl_register(struct net
*net
, char *dev_name
,
7135 struct inet6_dev
*idev
, struct ipv6_devconf
*p
)
7138 struct ctl_table
*table
;
7139 char path
[sizeof("net/ipv6/conf/") + IFNAMSIZ
];
7141 table
= kmemdup(addrconf_sysctl
, sizeof(addrconf_sysctl
), GFP_KERNEL_ACCOUNT
);
7145 for (i
= 0; table
[i
].data
; i
++) {
7146 table
[i
].data
+= (char *)p
- (char *)&ipv6_devconf
;
7147 /* If one of these is already set, then it is not safe to
7148 * overwrite either of them: this makes proc_dointvec_minmax
7151 if (!table
[i
].extra1
&& !table
[i
].extra2
) {
7152 table
[i
].extra1
= idev
; /* embedded; no ref */
7153 table
[i
].extra2
= net
;
7157 snprintf(path
, sizeof(path
), "net/ipv6/conf/%s", dev_name
);
7159 p
->sysctl_header
= register_net_sysctl_sz(net
, path
, table
,
7160 ARRAY_SIZE(addrconf_sysctl
));
7161 if (!p
->sysctl_header
)
7164 if (!strcmp(dev_name
, "all"))
7165 ifindex
= NETCONFA_IFINDEX_ALL
;
7166 else if (!strcmp(dev_name
, "default"))
7167 ifindex
= NETCONFA_IFINDEX_DEFAULT
;
7169 ifindex
= idev
->dev
->ifindex
;
7170 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
, NETCONFA_ALL
,
7180 static void __addrconf_sysctl_unregister(struct net
*net
,
7181 struct ipv6_devconf
*p
, int ifindex
)
7183 struct ctl_table
*table
;
7185 if (!p
->sysctl_header
)
7188 table
= p
->sysctl_header
->ctl_table_arg
;
7189 unregister_net_sysctl_table(p
->sysctl_header
);
7190 p
->sysctl_header
= NULL
;
7193 inet6_netconf_notify_devconf(net
, RTM_DELNETCONF
, 0, ifindex
, NULL
);
7196 static int addrconf_sysctl_register(struct inet6_dev
*idev
)
7200 if (!sysctl_dev_name_is_allowed(idev
->dev
->name
))
7203 err
= neigh_sysctl_register(idev
->dev
, idev
->nd_parms
,
7204 &ndisc_ifinfo_sysctl_change
);
7207 err
= __addrconf_sysctl_register(dev_net(idev
->dev
), idev
->dev
->name
,
7210 neigh_sysctl_unregister(idev
->nd_parms
);
7215 static void addrconf_sysctl_unregister(struct inet6_dev
*idev
)
7217 __addrconf_sysctl_unregister(dev_net(idev
->dev
), &idev
->cnf
,
7218 idev
->dev
->ifindex
);
7219 neigh_sysctl_unregister(idev
->nd_parms
);
7225 static int __net_init
addrconf_init_net(struct net
*net
)
7228 struct ipv6_devconf
*all
, *dflt
;
7230 spin_lock_init(&net
->ipv6
.addrconf_hash_lock
);
7231 INIT_DEFERRABLE_WORK(&net
->ipv6
.addr_chk_work
, addrconf_verify_work
);
7232 net
->ipv6
.inet6_addr_lst
= kcalloc(IN6_ADDR_HSIZE
,
7233 sizeof(struct hlist_head
),
7235 if (!net
->ipv6
.inet6_addr_lst
)
7236 goto err_alloc_addr
;
7238 all
= kmemdup(&ipv6_devconf
, sizeof(ipv6_devconf
), GFP_KERNEL
);
7242 dflt
= kmemdup(&ipv6_devconf_dflt
, sizeof(ipv6_devconf_dflt
), GFP_KERNEL
);
7244 goto err_alloc_dflt
;
7246 if (!net_eq(net
, &init_net
)) {
7247 switch (net_inherit_devconf()) {
7248 case 1: /* copy from init_net */
7249 memcpy(all
, init_net
.ipv6
.devconf_all
,
7250 sizeof(ipv6_devconf
));
7251 memcpy(dflt
, init_net
.ipv6
.devconf_dflt
,
7252 sizeof(ipv6_devconf_dflt
));
7254 case 3: /* copy from the current netns */
7255 memcpy(all
, current
->nsproxy
->net_ns
->ipv6
.devconf_all
,
7256 sizeof(ipv6_devconf
));
7258 current
->nsproxy
->net_ns
->ipv6
.devconf_dflt
,
7259 sizeof(ipv6_devconf_dflt
));
7263 /* use compiled values */
7268 /* these will be inherited by all namespaces */
7269 dflt
->autoconf
= ipv6_defaults
.autoconf
;
7270 dflt
->disable_ipv6
= ipv6_defaults
.disable_ipv6
;
7272 dflt
->stable_secret
.initialized
= false;
7273 all
->stable_secret
.initialized
= false;
7275 net
->ipv6
.devconf_all
= all
;
7276 net
->ipv6
.devconf_dflt
= dflt
;
7278 #ifdef CONFIG_SYSCTL
7279 err
= __addrconf_sysctl_register(net
, "all", NULL
, all
);
7283 err
= __addrconf_sysctl_register(net
, "default", NULL
, dflt
);
7289 #ifdef CONFIG_SYSCTL
7291 __addrconf_sysctl_unregister(net
, all
, NETCONFA_IFINDEX_ALL
);
7294 net
->ipv6
.devconf_dflt
= NULL
;
7298 net
->ipv6
.devconf_all
= NULL
;
7300 kfree(net
->ipv6
.inet6_addr_lst
);
7305 static void __net_exit
addrconf_exit_net(struct net
*net
)
7309 #ifdef CONFIG_SYSCTL
7310 __addrconf_sysctl_unregister(net
, net
->ipv6
.devconf_dflt
,
7311 NETCONFA_IFINDEX_DEFAULT
);
7312 __addrconf_sysctl_unregister(net
, net
->ipv6
.devconf_all
,
7313 NETCONFA_IFINDEX_ALL
);
7315 kfree(net
->ipv6
.devconf_dflt
);
7316 net
->ipv6
.devconf_dflt
= NULL
;
7317 kfree(net
->ipv6
.devconf_all
);
7318 net
->ipv6
.devconf_all
= NULL
;
7320 cancel_delayed_work_sync(&net
->ipv6
.addr_chk_work
);
7322 * Check hash table, then free it.
7324 for (i
= 0; i
< IN6_ADDR_HSIZE
; i
++)
7325 WARN_ON_ONCE(!hlist_empty(&net
->ipv6
.inet6_addr_lst
[i
]));
7327 kfree(net
->ipv6
.inet6_addr_lst
);
7328 net
->ipv6
.inet6_addr_lst
= NULL
;
7331 static struct pernet_operations addrconf_ops
= {
7332 .init
= addrconf_init_net
,
7333 .exit
= addrconf_exit_net
,
7336 static struct rtnl_af_ops inet6_ops __read_mostly
= {
7338 .fill_link_af
= inet6_fill_link_af
,
7339 .get_link_af_size
= inet6_get_link_af_size
,
7340 .validate_link_af
= inet6_validate_link_af
,
7341 .set_link_af
= inet6_set_link_af
,
7345 * Init / cleanup code
7348 int __init
addrconf_init(void)
7350 struct inet6_dev
*idev
;
7353 err
= ipv6_addr_label_init();
7355 pr_crit("%s: cannot initialize default policy table: %d\n",
7360 err
= register_pernet_subsys(&addrconf_ops
);
7364 addrconf_wq
= create_workqueue("ipv6_addrconf");
7371 idev
= ipv6_add_dev(blackhole_netdev
);
7374 err
= PTR_ERR(idev
);
7378 ip6_route_init_special_entries();
7380 register_netdevice_notifier(&ipv6_dev_notf
);
7382 addrconf_verify(&init_net
);
7384 rtnl_af_register(&inet6_ops
);
7386 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETLINK
,
7387 NULL
, inet6_dump_ifinfo
, 0);
7391 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_NEWADDR
,
7392 inet6_rtm_newaddr
, NULL
, 0);
7395 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_DELADDR
,
7396 inet6_rtm_deladdr
, NULL
, 0);
7399 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETADDR
,
7400 inet6_rtm_getaddr
, inet6_dump_ifaddr
,
7401 RTNL_FLAG_DOIT_UNLOCKED
);
7404 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETMULTICAST
,
7405 NULL
, inet6_dump_ifmcaddr
, 0);
7408 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETANYCAST
,
7409 NULL
, inet6_dump_ifacaddr
, 0);
7412 err
= rtnl_register_module(THIS_MODULE
, PF_INET6
, RTM_GETNETCONF
,
7413 inet6_netconf_get_devconf
,
7414 inet6_netconf_dump_devconf
,
7415 RTNL_FLAG_DOIT_UNLOCKED
);
7418 err
= ipv6_addr_label_rtnl_register();
7424 rtnl_unregister_all(PF_INET6
);
7425 rtnl_af_unregister(&inet6_ops
);
7426 unregister_netdevice_notifier(&ipv6_dev_notf
);
7428 destroy_workqueue(addrconf_wq
);
7430 unregister_pernet_subsys(&addrconf_ops
);
7432 ipv6_addr_label_cleanup();
7437 void addrconf_cleanup(void)
7439 struct net_device
*dev
;
7441 unregister_netdevice_notifier(&ipv6_dev_notf
);
7442 unregister_pernet_subsys(&addrconf_ops
);
7443 ipv6_addr_label_cleanup();
7445 rtnl_af_unregister(&inet6_ops
);
7449 /* clean dev list */
7450 for_each_netdev(&init_net
, dev
) {
7451 if (__in6_dev_get(dev
) == NULL
)
7453 addrconf_ifdown(dev
, true);
7455 addrconf_ifdown(init_net
.loopback_dev
, true);
7459 destroy_workqueue(addrconf_wq
);