xref: /linux/net/ipv6/addrconf.c (revision b026cc5ce2f1049486fd4a12b97311e65c51cdf8)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	IPv6 Address [auto]configuration
4  *	Linux INET6 implementation
5  *
6  *	Authors:
7  *	Pedro Roque		<roque@di.fc.ul.pt>
8  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
9  */
10 
11 /*
12  *	Changes:
13  *
14  *	Janos Farkas			:	delete timer on ifdown
15  *	<chexum@bankinf.banki.hu>
16  *	Andi Kleen			:	kill double kfree on module
17  *						unload.
18  *	Maciej W. Rozycki		:	FDDI support
19  *	sekiya@USAGI			:	Don't send too many RS
20  *						packets.
21  *	yoshfuji@USAGI			:       Fixed interval between DAD
22  *						packets.
23  *	YOSHIFUJI Hideaki @USAGI	:	improved accuracy of
24  *						address validation timer.
25  *	YOSHIFUJI Hideaki @USAGI	:	Privacy Extensions (RFC3041)
26  *						support.
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
31  *						seq_file.
32  *	YOSHIFUJI Hideaki @USAGI	:	improved source address
33  *						selection; consider scope,
34  *						status etc.
35  */
36 
37 #define pr_fmt(fmt) "IPv6: " fmt
38 
39 #include <crypto/sha1.h>
40 #include <linux/errno.h>
41 #include <linux/types.h>
42 #include <linux/kernel.h>
43 #include <linux/sched/signal.h>
44 #include <linux/socket.h>
45 #include <linux/sockios.h>
46 #include <linux/net.h>
47 #include <linux/inet.h>
48 #include <linux/in6.h>
49 #include <linux/netdevice.h>
50 #include <linux/if_addr.h>
51 #include <linux/if_arp.h>
52 #include <linux/if_arcnet.h>
53 #include <linux/if_infiniband.h>
54 #include <linux/route.h>
55 #include <linux/inetdevice.h>
56 #include <linux/init.h>
57 #include <linux/slab.h>
58 #ifdef CONFIG_SYSCTL
59 #include <linux/sysctl.h>
60 #endif
61 #include <linux/capability.h>
62 #include <linux/delay.h>
63 #include <linux/notifier.h>
64 #include <linux/string.h>
65 #include <linux/hash.h>
66 
67 #include <net/ip_tunnels.h>
68 #include <net/net_namespace.h>
69 #include <net/sock.h>
70 #include <net/snmp.h>
71 
72 #include <net/6lowpan.h>
73 #include <net/firewire.h>
74 #include <net/ipv6.h>
75 #include <net/protocol.h>
76 #include <net/ndisc.h>
77 #include <net/ip6_route.h>
78 #include <net/addrconf.h>
79 #include <net/tcp.h>
80 #include <net/ip.h>
81 #include <net/netlink.h>
82 #include <net/pkt_sched.h>
83 #include <net/l3mdev.h>
84 #include <net/netdev_lock.h>
85 #include <linux/if_tunnel.h>
86 #include <linux/rtnetlink.h>
87 #include <linux/netconf.h>
88 #include <linux/random.h>
89 #include <linux/uaccess.h>
90 #include <linux/unaligned.h>
91 
92 #include <linux/proc_fs.h>
93 #include <linux/seq_file.h>
94 #include <linux/export.h>
95 #include <linux/ioam6.h>
96 
97 #define IPV6_MAX_STRLEN \
98 	sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
99 
100 static inline u32 cstamp_delta(unsigned long cstamp)
101 {
102 	return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
103 }
104 
105 static inline s32 rfc3315_s14_backoff_init(s32 irt)
106 {
107 	/* multiply 'initial retransmission time' by 0.9 .. 1.1 */
108 	u64 tmp = get_random_u32_inclusive(900000, 1100000) * (u64)irt;
109 	do_div(tmp, 1000000);
110 	return (s32)tmp;
111 }
112 
113 static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
114 {
115 	/* multiply 'retransmission timeout' by 1.9 .. 2.1 */
116 	u64 tmp = get_random_u32_inclusive(1900000, 2100000) * (u64)rt;
117 	do_div(tmp, 1000000);
118 	if ((s32)tmp > mrt) {
119 		/* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
120 		tmp = get_random_u32_inclusive(900000, 1100000) * (u64)mrt;
121 		do_div(tmp, 1000000);
122 	}
123 	return (s32)tmp;
124 }
125 
126 #ifdef CONFIG_SYSCTL
127 static int addrconf_sysctl_register(struct inet6_dev *idev);
128 static void addrconf_sysctl_unregister(struct inet6_dev *idev);
129 #else
130 static inline int addrconf_sysctl_register(struct inet6_dev *idev)
131 {
132 	return 0;
133 }
134 
135 static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
136 {
137 }
138 #endif
139 
140 static void ipv6_gen_rnd_iid(struct in6_addr *addr);
141 
142 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
143 static int ipv6_count_addresses(const struct inet6_dev *idev);
144 static int ipv6_generate_stable_address(struct in6_addr *addr,
145 					u8 dad_count,
146 					const struct inet6_dev *idev);
147 
148 #define IN6_ADDR_HSIZE_SHIFT	8
149 #define IN6_ADDR_HSIZE		(1 << IN6_ADDR_HSIZE_SHIFT)
150 
151 static void addrconf_verify(struct net *net);
152 static void addrconf_verify_rtnl(struct net *net);
153 
154 static struct workqueue_struct *addrconf_wq;
155 
156 static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
157 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
158 
159 static void addrconf_type_change(struct net_device *dev,
160 				 unsigned long event);
161 static int addrconf_ifdown(struct net_device *dev, bool unregister);
162 
163 static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
164 						  int plen,
165 						  const struct net_device *dev,
166 						  u32 flags, u32 noflags,
167 						  bool no_gw);
168 
169 static void addrconf_dad_start(struct inet6_ifaddr *ifp);
170 static void addrconf_dad_work(struct work_struct *w);
171 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
172 				   bool send_na);
173 static void addrconf_dad_run(struct inet6_dev *idev, bool restart);
174 static void addrconf_rs_timer(struct timer_list *t);
175 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
176 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
177 
178 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
179 				struct prefix_info *pinfo);
180 
181 static struct ipv6_devconf ipv6_devconf __read_mostly = {
182 	.forwarding		= 0,
183 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
184 	.mtu6			= IPV6_MIN_MTU,
185 	.accept_ra		= 1,
186 	.accept_redirects	= 1,
187 	.autoconf		= 1,
188 	.force_mld_version	= 0,
189 	.mldv1_unsolicited_report_interval = 10 * HZ,
190 	.mldv2_unsolicited_report_interval = HZ,
191 	.dad_transmits		= 1,
192 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
193 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
194 	.rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
195 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
196 	.use_tempaddr		= 0,
197 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
198 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
199 	.regen_min_advance	= REGEN_MIN_ADVANCE,
200 	.regen_max_retry	= REGEN_MAX_RETRY,
201 	.max_desync_factor	= MAX_DESYNC_FACTOR,
202 	.max_addresses		= IPV6_MAX_ADDRESSES,
203 	.accept_ra_defrtr	= 1,
204 	.ra_defrtr_metric	= IP6_RT_PRIO_USER,
205 	.accept_ra_from_local	= 0,
206 	.accept_ra_min_hop_limit= 1,
207 	.accept_ra_min_lft	= 0,
208 	.accept_ra_pinfo	= 1,
209 #ifdef CONFIG_IPV6_ROUTER_PREF
210 	.accept_ra_rtr_pref	= 1,
211 	.rtr_probe_interval	= 60 * HZ,
212 #ifdef CONFIG_IPV6_ROUTE_INFO
213 	.accept_ra_rt_info_min_plen = 0,
214 	.accept_ra_rt_info_max_plen = 0,
215 #endif
216 #endif
217 	.proxy_ndp		= 0,
218 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
219 	.disable_ipv6		= 0,
220 	.accept_dad		= 0,
221 	.suppress_frag_ndisc	= 1,
222 	.accept_ra_mtu		= 1,
223 	.stable_secret		= {
224 		.initialized = false,
225 	},
226 	.use_oif_addrs_only	= 0,
227 	.ignore_routes_with_linkdown = 0,
228 	.keep_addr_on_down	= 0,
229 	.seg6_enabled		= 0,
230 #ifdef CONFIG_IPV6_SEG6_HMAC
231 	.seg6_require_hmac	= 0,
232 #endif
233 	.enhanced_dad           = 1,
234 	.addr_gen_mode		= IN6_ADDR_GEN_MODE_EUI64,
235 	.disable_policy		= 0,
236 	.rpl_seg_enabled	= 0,
237 	.ioam6_enabled		= 0,
238 	.ioam6_id               = IOAM6_DEFAULT_IF_ID,
239 	.ioam6_id_wide		= IOAM6_DEFAULT_IF_ID_WIDE,
240 	.ndisc_evict_nocarrier	= 1,
241 	.ra_honor_pio_life	= 0,
242 	.ra_honor_pio_pflag	= 0,
243 	.force_forwarding	= 0,
244 };
245 
246 static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
247 	.forwarding		= 0,
248 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
249 	.mtu6			= IPV6_MIN_MTU,
250 	.accept_ra		= 1,
251 	.accept_redirects	= 1,
252 	.autoconf		= 1,
253 	.force_mld_version	= 0,
254 	.mldv1_unsolicited_report_interval = 10 * HZ,
255 	.mldv2_unsolicited_report_interval = HZ,
256 	.dad_transmits		= 1,
257 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
258 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
259 	.rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
260 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
261 	.use_tempaddr		= 0,
262 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
263 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
264 	.regen_min_advance	= REGEN_MIN_ADVANCE,
265 	.regen_max_retry	= REGEN_MAX_RETRY,
266 	.max_desync_factor	= MAX_DESYNC_FACTOR,
267 	.max_addresses		= IPV6_MAX_ADDRESSES,
268 	.accept_ra_defrtr	= 1,
269 	.ra_defrtr_metric	= IP6_RT_PRIO_USER,
270 	.accept_ra_from_local	= 0,
271 	.accept_ra_min_hop_limit= 1,
272 	.accept_ra_min_lft	= 0,
273 	.accept_ra_pinfo	= 1,
274 #ifdef CONFIG_IPV6_ROUTER_PREF
275 	.accept_ra_rtr_pref	= 1,
276 	.rtr_probe_interval	= 60 * HZ,
277 #ifdef CONFIG_IPV6_ROUTE_INFO
278 	.accept_ra_rt_info_min_plen = 0,
279 	.accept_ra_rt_info_max_plen = 0,
280 #endif
281 #endif
282 	.proxy_ndp		= 0,
283 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
284 	.disable_ipv6		= 0,
285 	.accept_dad		= 1,
286 	.suppress_frag_ndisc	= 1,
287 	.accept_ra_mtu		= 1,
288 	.stable_secret		= {
289 		.initialized = false,
290 	},
291 	.use_oif_addrs_only	= 0,
292 	.ignore_routes_with_linkdown = 0,
293 	.keep_addr_on_down	= 0,
294 	.seg6_enabled		= 0,
295 #ifdef CONFIG_IPV6_SEG6_HMAC
296 	.seg6_require_hmac	= 0,
297 #endif
298 	.enhanced_dad           = 1,
299 	.addr_gen_mode		= IN6_ADDR_GEN_MODE_EUI64,
300 	.disable_policy		= 0,
301 	.rpl_seg_enabled	= 0,
302 	.ioam6_enabled		= 0,
303 	.ioam6_id               = IOAM6_DEFAULT_IF_ID,
304 	.ioam6_id_wide		= IOAM6_DEFAULT_IF_ID_WIDE,
305 	.ndisc_evict_nocarrier	= 1,
306 	.ra_honor_pio_life	= 0,
307 	.ra_honor_pio_pflag	= 0,
308 	.force_forwarding	= 0,
309 };
310 
311 /* Check if link is ready: is it up and is a valid qdisc available */
312 static inline bool addrconf_link_ready(const struct net_device *dev)
313 {
314 	return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
315 }
316 
317 static void addrconf_del_rs_timer(struct inet6_dev *idev)
318 {
319 	if (timer_delete(&idev->rs_timer))
320 		__in6_dev_put(idev);
321 }
322 
323 static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
324 {
325 	if (cancel_delayed_work(&ifp->dad_work))
326 		__in6_ifa_put(ifp);
327 }
328 
329 static void addrconf_mod_rs_timer(struct inet6_dev *idev,
330 				  unsigned long when)
331 {
332 	if (!mod_timer(&idev->rs_timer, jiffies + when))
333 		in6_dev_hold(idev);
334 }
335 
336 static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
337 				   unsigned long delay)
338 {
339 	in6_ifa_hold(ifp);
340 	if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
341 		in6_ifa_put(ifp);
342 }
343 
344 static int snmp6_alloc_dev(struct inet6_dev *idev)
345 {
346 	int i;
347 
348 	idev->stats.ipv6 = alloc_percpu_gfp(struct ipstats_mib, GFP_KERNEL_ACCOUNT);
349 	if (!idev->stats.ipv6)
350 		goto err_ip;
351 
352 	for_each_possible_cpu(i) {
353 		struct ipstats_mib *addrconf_stats;
354 		addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
355 		u64_stats_init(&addrconf_stats->syncp);
356 	}
357 
358 
359 	idev->stats.icmpv6dev = kzalloc_obj(struct icmpv6_mib_device);
360 	if (!idev->stats.icmpv6dev)
361 		goto err_icmp;
362 	idev->stats.icmpv6msgdev = kzalloc_obj(struct icmpv6msg_mib_device,
363 					       GFP_KERNEL_ACCOUNT);
364 	if (!idev->stats.icmpv6msgdev)
365 		goto err_icmpmsg;
366 
367 	return 0;
368 
369 err_icmpmsg:
370 	kfree(idev->stats.icmpv6dev);
371 err_icmp:
372 	free_percpu(idev->stats.ipv6);
373 err_ip:
374 	return -ENOMEM;
375 }
376 
377 static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
378 {
379 	struct net *net = dev_net(dev);
380 	struct neigh_table *tbl;
381 	struct inet6_dev *ndev;
382 	int err = -ENOMEM;
383 
384 	ASSERT_RTNL();
385 	netdev_assert_locked_ops_compat(dev);
386 
387 	if (dev->mtu < IPV6_MIN_MTU && dev != blackhole_netdev)
388 		return ERR_PTR(-EINVAL);
389 
390 	ndev = kzalloc_obj(*ndev, GFP_KERNEL_ACCOUNT);
391 	if (!ndev)
392 		return ERR_PTR(err);
393 
394 	rwlock_init(&ndev->lock);
395 	ndev->dev = dev;
396 	INIT_LIST_HEAD(&ndev->addr_list);
397 	timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0);
398 	memcpy(&ndev->cnf, net->ipv6.devconf_dflt, sizeof(ndev->cnf));
399 
400 	if (ndev->cnf.stable_secret.initialized)
401 		ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
402 
403 	tbl = nd_table(net);
404 	ndev->cnf.mtu6 = dev->mtu;
405 	ndev->ra_mtu = 0;
406 	ndev->nd_parms = neigh_parms_alloc(dev, tbl);
407 	if (!ndev->nd_parms) {
408 		kfree(ndev);
409 		return ERR_PTR(err);
410 	}
411 	if (ndev->cnf.forwarding)
412 		netif_disable_lro(dev);
413 	/* We refer to the device */
414 	netdev_hold(dev, &ndev->dev_tracker, GFP_KERNEL);
415 
416 	if (snmp6_alloc_dev(ndev) < 0) {
417 		netdev_dbg(dev, "%s: cannot allocate memory for statistics\n",
418 			   __func__);
419 		neigh_parms_release(tbl, ndev->nd_parms);
420 		netdev_put(dev, &ndev->dev_tracker);
421 		kfree(ndev);
422 		return ERR_PTR(err);
423 	}
424 
425 	if (dev != blackhole_netdev) {
426 		if (snmp6_register_dev(ndev) < 0) {
427 			netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n",
428 				   __func__, dev->name);
429 			goto err_release;
430 		}
431 	}
432 	/* One reference from device. */
433 	refcount_set(&ndev->refcnt, 1);
434 
435 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
436 		ndev->cnf.accept_dad = -1;
437 
438 #if IS_ENABLED(CONFIG_IPV6_SIT)
439 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
440 		pr_info("%s: Disabled Multicast RS\n", dev->name);
441 		ndev->cnf.rtr_solicits = 0;
442 	}
443 #endif
444 
445 	INIT_LIST_HEAD(&ndev->tempaddr_list);
446 	ndev->desync_factor = U32_MAX;
447 	if ((dev->flags&IFF_LOOPBACK) ||
448 	    dev->type == ARPHRD_TUNNEL ||
449 	    dev->type == ARPHRD_TUNNEL6 ||
450 	    dev->type == ARPHRD_SIT ||
451 	    dev->type == ARPHRD_NONE) {
452 		ndev->cnf.use_tempaddr = -1;
453 	}
454 
455 	ndev->token = in6addr_any;
456 
457 	if (netif_running(dev) && addrconf_link_ready(dev))
458 		ndev->if_flags |= IF_READY;
459 
460 	ipv6_mc_init_dev(ndev);
461 	ndev->tstamp = jiffies;
462 	if (dev != blackhole_netdev) {
463 		err = addrconf_sysctl_register(ndev);
464 		if (err) {
465 			ipv6_mc_destroy_dev(ndev);
466 			snmp6_unregister_dev(ndev);
467 			goto err_release;
468 		}
469 	}
470 	/* protected by rtnl_lock */
471 	rcu_assign_pointer(dev->ip6_ptr, ndev);
472 
473 	if (dev != blackhole_netdev) {
474 		/* Join interface-local all-node multicast group */
475 		ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
476 
477 		/* Join all-node multicast group */
478 		ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
479 
480 		/* Join all-router multicast group if forwarding is set */
481 		if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
482 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
483 	}
484 	return ndev;
485 
486 err_release:
487 	neigh_parms_release(tbl, ndev->nd_parms);
488 	ndev->dead = 1;
489 	in6_dev_finish_destroy(ndev);
490 	return ERR_PTR(err);
491 }
492 
493 static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
494 {
495 	struct inet6_dev *idev;
496 
497 	ASSERT_RTNL();
498 
499 	idev = __in6_dev_get(dev);
500 	if (!idev) {
501 		idev = ipv6_add_dev(dev);
502 		if (IS_ERR(idev))
503 			return idev;
504 	}
505 
506 	if (dev->flags&IFF_UP)
507 		ipv6_mc_up(idev);
508 	return idev;
509 }
510 
511 static int inet6_netconf_msgsize_devconf(int type)
512 {
513 	int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
514 		    + nla_total_size(4);	/* NETCONFA_IFINDEX */
515 	bool all = false;
516 
517 	if (type == NETCONFA_ALL)
518 		all = true;
519 
520 	if (all || type == NETCONFA_FORWARDING)
521 		size += nla_total_size(4);
522 #ifdef CONFIG_IPV6_MROUTE
523 	if (all || type == NETCONFA_MC_FORWARDING)
524 		size += nla_total_size(4);
525 #endif
526 	if (all || type == NETCONFA_PROXY_NEIGH)
527 		size += nla_total_size(4);
528 
529 	if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
530 		size += nla_total_size(4);
531 
532 	return size;
533 }
534 
535 static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
536 				      struct ipv6_devconf *devconf, u32 portid,
537 				      u32 seq, int event, unsigned int flags,
538 				      int type)
539 {
540 	struct nlmsghdr  *nlh;
541 	struct netconfmsg *ncm;
542 	bool all = false;
543 
544 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
545 			flags);
546 	if (!nlh)
547 		return -EMSGSIZE;
548 
549 	if (type == NETCONFA_ALL)
550 		all = true;
551 
552 	ncm = nlmsg_data(nlh);
553 	ncm->ncm_family = AF_INET6;
554 
555 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
556 		goto nla_put_failure;
557 
558 	if (!devconf)
559 		goto out;
560 
561 	if ((all || type == NETCONFA_FORWARDING) &&
562 	    nla_put_s32(skb, NETCONFA_FORWARDING,
563 			READ_ONCE(devconf->forwarding)) < 0)
564 		goto nla_put_failure;
565 #ifdef CONFIG_IPV6_MROUTE
566 	if ((all || type == NETCONFA_MC_FORWARDING) &&
567 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
568 			atomic_read(&devconf->mc_forwarding)) < 0)
569 		goto nla_put_failure;
570 #endif
571 	if ((all || type == NETCONFA_PROXY_NEIGH) &&
572 	    nla_put_s32(skb, NETCONFA_PROXY_NEIGH,
573 			READ_ONCE(devconf->proxy_ndp)) < 0)
574 		goto nla_put_failure;
575 
576 	if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
577 	    nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
578 			READ_ONCE(devconf->ignore_routes_with_linkdown)) < 0)
579 		goto nla_put_failure;
580 
581 out:
582 	nlmsg_end(skb, nlh);
583 	return 0;
584 
585 nla_put_failure:
586 	nlmsg_cancel(skb, nlh);
587 	return -EMSGSIZE;
588 }
589 
590 void inet6_netconf_notify_devconf(struct net *net, int event, int type,
591 				  int ifindex, struct ipv6_devconf *devconf)
592 {
593 	struct sk_buff *skb;
594 	int err = -ENOBUFS;
595 
596 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
597 	if (!skb)
598 		goto errout;
599 
600 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
601 					 event, 0, type);
602 	if (err < 0) {
603 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
604 		WARN_ON(err == -EMSGSIZE);
605 		kfree_skb(skb);
606 		goto errout;
607 	}
608 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
609 	return;
610 errout:
611 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
612 }
613 
614 static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
615 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
616 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
617 	[NETCONFA_PROXY_NEIGH]	= { .len = sizeof(int) },
618 	[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]	= { .len = sizeof(int) },
619 };
620 
621 static int inet6_netconf_valid_get_req(struct sk_buff *skb,
622 				       const struct nlmsghdr *nlh,
623 				       struct nlattr **tb,
624 				       struct netlink_ext_ack *extack)
625 {
626 	int i, err;
627 
628 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
629 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request");
630 		return -EINVAL;
631 	}
632 
633 	if (!netlink_strict_get_check(skb))
634 		return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
635 					      tb, NETCONFA_MAX,
636 					      devconf_ipv6_policy, extack);
637 
638 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
639 					    tb, NETCONFA_MAX,
640 					    devconf_ipv6_policy, extack);
641 	if (err)
642 		return err;
643 
644 	for (i = 0; i <= NETCONFA_MAX; i++) {
645 		if (!tb[i])
646 			continue;
647 
648 		switch (i) {
649 		case NETCONFA_IFINDEX:
650 			break;
651 		default:
652 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
653 			return -EINVAL;
654 		}
655 	}
656 
657 	return 0;
658 }
659 
660 static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
661 				     struct nlmsghdr *nlh,
662 				     struct netlink_ext_ack *extack)
663 {
664 	struct net *net = sock_net(in_skb->sk);
665 	struct nlattr *tb[NETCONFA_MAX+1];
666 	struct inet6_dev *in6_dev = NULL;
667 	struct net_device *dev = NULL;
668 	struct sk_buff *skb;
669 	struct ipv6_devconf *devconf;
670 	int ifindex;
671 	int err;
672 
673 	err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack);
674 	if (err < 0)
675 		return err;
676 
677 	if (!tb[NETCONFA_IFINDEX])
678 		return -EINVAL;
679 
680 	err = -EINVAL;
681 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
682 	switch (ifindex) {
683 	case NETCONFA_IFINDEX_ALL:
684 		devconf = net->ipv6.devconf_all;
685 		break;
686 	case NETCONFA_IFINDEX_DEFAULT:
687 		devconf = net->ipv6.devconf_dflt;
688 		break;
689 	default:
690 		dev = dev_get_by_index(net, ifindex);
691 		if (!dev)
692 			return -EINVAL;
693 		in6_dev = in6_dev_get(dev);
694 		if (!in6_dev)
695 			goto errout;
696 		devconf = &in6_dev->cnf;
697 		break;
698 	}
699 
700 	err = -ENOBUFS;
701 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
702 	if (!skb)
703 		goto errout;
704 
705 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
706 					 NETLINK_CB(in_skb).portid,
707 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
708 					 NETCONFA_ALL);
709 	if (err < 0) {
710 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
711 		WARN_ON(err == -EMSGSIZE);
712 		kfree_skb(skb);
713 		goto errout;
714 	}
715 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
716 errout:
717 	if (in6_dev)
718 		in6_dev_put(in6_dev);
719 	dev_put(dev);
720 	return err;
721 }
722 
723 /* Combine dev_addr_genid and dev_base_seq to detect changes.
724  */
725 static u32 inet6_base_seq(const struct net *net)
726 {
727 	u32 res = atomic_read(&net->ipv6.dev_addr_genid) +
728 		  READ_ONCE(net->dev_base_seq);
729 
730 	/* Must not return 0 (see nl_dump_check_consistent()).
731 	 * Chose a value far away from 0.
732 	 */
733 	if (!res)
734 		res = 0x80000000;
735 	return res;
736 }
737 
738 static int inet6_netconf_dump_devconf(struct sk_buff *skb,
739 				      struct netlink_callback *cb)
740 {
741 	const struct nlmsghdr *nlh = cb->nlh;
742 	struct net *net = sock_net(skb->sk);
743 	struct {
744 		unsigned long ifindex;
745 		unsigned int all_default;
746 	} *ctx = (void *)cb->ctx;
747 	struct net_device *dev;
748 	struct inet6_dev *idev;
749 	int err = 0;
750 
751 	if (cb->strict_check) {
752 		struct netlink_ext_ack *extack = cb->extack;
753 		struct netconfmsg *ncm;
754 
755 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
756 			NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
757 			return -EINVAL;
758 		}
759 
760 		if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
761 			NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
762 			return -EINVAL;
763 		}
764 	}
765 
766 	rcu_read_lock();
767 	for_each_netdev_dump(net, dev, ctx->ifindex) {
768 		idev = __in6_dev_get(dev);
769 		if (!idev)
770 			continue;
771 		err = inet6_netconf_fill_devconf(skb, dev->ifindex,
772 					         &idev->cnf,
773 						 NETLINK_CB(cb->skb).portid,
774 						 nlh->nlmsg_seq,
775 						 RTM_NEWNETCONF,
776 						 NLM_F_MULTI,
777 						 NETCONFA_ALL);
778 		if (err < 0)
779 			goto done;
780 	}
781 	if (ctx->all_default == 0) {
782 		err = inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
783 						 net->ipv6.devconf_all,
784 						 NETLINK_CB(cb->skb).portid,
785 						 nlh->nlmsg_seq,
786 						 RTM_NEWNETCONF, NLM_F_MULTI,
787 						 NETCONFA_ALL);
788 		if (err < 0)
789 			goto done;
790 		ctx->all_default++;
791 	}
792 	if (ctx->all_default == 1) {
793 		err = inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
794 						 net->ipv6.devconf_dflt,
795 						 NETLINK_CB(cb->skb).portid,
796 						 nlh->nlmsg_seq,
797 						 RTM_NEWNETCONF, NLM_F_MULTI,
798 						 NETCONFA_ALL);
799 		if (err < 0)
800 			goto done;
801 		ctx->all_default++;
802 	}
803 done:
804 	rcu_read_unlock();
805 	return err;
806 }
807 
808 #ifdef CONFIG_SYSCTL
809 static void dev_forward_change(struct inet6_dev *idev)
810 {
811 	struct net_device *dev;
812 	struct inet6_ifaddr *ifa;
813 	LIST_HEAD(tmp_addr_list);
814 
815 	if (!idev)
816 		return;
817 	dev = idev->dev;
818 	if (idev->cnf.forwarding)
819 		dev_disable_lro(dev);
820 	if (dev->flags & IFF_MULTICAST) {
821 		if (idev->cnf.forwarding) {
822 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
823 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
824 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
825 		} else {
826 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
827 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
828 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
829 		}
830 	}
831 
832 	read_lock_bh(&idev->lock);
833 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
834 		if (ifa->flags&IFA_F_TENTATIVE)
835 			continue;
836 		list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
837 	}
838 	read_unlock_bh(&idev->lock);
839 
840 	while (!list_empty(&tmp_addr_list)) {
841 		ifa = list_first_entry(&tmp_addr_list,
842 				       struct inet6_ifaddr, if_list_aux);
843 		list_del(&ifa->if_list_aux);
844 		if (idev->cnf.forwarding)
845 			addrconf_join_anycast(ifa);
846 		else
847 			addrconf_leave_anycast(ifa);
848 	}
849 
850 	inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
851 				     NETCONFA_FORWARDING,
852 				     dev->ifindex, &idev->cnf);
853 }
854 
855 
856 static void addrconf_forward_change(struct net *net, __s32 newf)
857 {
858 	struct net_device *dev;
859 	struct inet6_dev *idev;
860 
861 	for_each_netdev(net, dev) {
862 		idev = __in6_dev_get_rtnl_net(dev);
863 		if (idev) {
864 			int changed = (!idev->cnf.forwarding) ^ (!newf);
865 			/* Disabling all.forwarding sets 0 to force_forwarding for all interfaces */
866 			if (newf == 0)
867 				WRITE_ONCE(idev->cnf.force_forwarding, 0);
868 
869 			WRITE_ONCE(idev->cnf.forwarding, newf);
870 			if (changed)
871 				dev_forward_change(idev);
872 		}
873 	}
874 }
875 
876 static int addrconf_fixup_forwarding(const struct ctl_table *table, int *p, int newf)
877 {
878 	struct net *net = (struct net *)table->extra2;
879 	int old;
880 
881 	if (!rtnl_net_trylock(net))
882 		return restart_syscall();
883 
884 	old = *p;
885 	WRITE_ONCE(*p, newf);
886 
887 	if (p == &net->ipv6.devconf_dflt->forwarding) {
888 		if ((!newf) ^ (!old))
889 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
890 						     NETCONFA_FORWARDING,
891 						     NETCONFA_IFINDEX_DEFAULT,
892 						     net->ipv6.devconf_dflt);
893 		rtnl_net_unlock(net);
894 		return 0;
895 	}
896 
897 	if (p == &net->ipv6.devconf_all->forwarding) {
898 		int old_dflt = net->ipv6.devconf_dflt->forwarding;
899 
900 		WRITE_ONCE(net->ipv6.devconf_dflt->forwarding, newf);
901 		if ((!newf) ^ (!old_dflt))
902 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
903 						     NETCONFA_FORWARDING,
904 						     NETCONFA_IFINDEX_DEFAULT,
905 						     net->ipv6.devconf_dflt);
906 
907 		addrconf_forward_change(net, newf);
908 		if ((!newf) ^ (!old))
909 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
910 						     NETCONFA_FORWARDING,
911 						     NETCONFA_IFINDEX_ALL,
912 						     net->ipv6.devconf_all);
913 	} else if ((!newf) ^ (!old))
914 		dev_forward_change((struct inet6_dev *)table->extra1);
915 	rtnl_net_unlock(net);
916 
917 	if (newf)
918 		rt6_purge_dflt_routers(net);
919 	return 0;
920 }
921 
922 static void addrconf_linkdown_change(struct net *net, __s32 newf)
923 {
924 	struct net_device *dev;
925 	struct inet6_dev *idev;
926 
927 	for_each_netdev(net, dev) {
928 		idev = __in6_dev_get_rtnl_net(dev);
929 		if (idev) {
930 			int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
931 
932 			WRITE_ONCE(idev->cnf.ignore_routes_with_linkdown, newf);
933 			if (changed)
934 				inet6_netconf_notify_devconf(dev_net(dev),
935 							     RTM_NEWNETCONF,
936 							     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
937 							     dev->ifindex,
938 							     &idev->cnf);
939 		}
940 	}
941 }
942 
943 static int addrconf_fixup_linkdown(const struct ctl_table *table, int *p, int newf)
944 {
945 	struct net *net = (struct net *)table->extra2;
946 	int old;
947 
948 	if (!rtnl_net_trylock(net))
949 		return restart_syscall();
950 
951 	old = *p;
952 	WRITE_ONCE(*p, newf);
953 
954 	if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
955 		if ((!newf) ^ (!old))
956 			inet6_netconf_notify_devconf(net,
957 						     RTM_NEWNETCONF,
958 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
959 						     NETCONFA_IFINDEX_DEFAULT,
960 						     net->ipv6.devconf_dflt);
961 	} else if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
962 		WRITE_ONCE(net->ipv6.devconf_dflt->ignore_routes_with_linkdown, newf);
963 		addrconf_linkdown_change(net, newf);
964 		if ((!newf) ^ (!old))
965 			inet6_netconf_notify_devconf(net,
966 						     RTM_NEWNETCONF,
967 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
968 						     NETCONFA_IFINDEX_ALL,
969 						     net->ipv6.devconf_all);
970 	} else {
971 		if (!newf ^ !old) {
972 			struct inet6_dev *idev = table->extra1;
973 
974 			inet6_netconf_notify_devconf(net,
975 						     RTM_NEWNETCONF,
976 						     NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
977 						     idev->dev->ifindex,
978 						     &idev->cnf);
979 		}
980 	}
981 
982 	rtnl_net_unlock(net);
983 
984 	return 0;
985 }
986 
987 #endif
988 
989 /* Nobody refers to this ifaddr, destroy it */
990 void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
991 {
992 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
993 
994 #ifdef NET_REFCNT_DEBUG
995 	pr_debug("%s\n", __func__);
996 #endif
997 
998 	in6_dev_put(ifp->idev);
999 
1000 	if (cancel_delayed_work(&ifp->dad_work))
1001 		pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
1002 			  ifp);
1003 
1004 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
1005 		pr_warn("Freeing alive inet6 address %p\n", ifp);
1006 		return;
1007 	}
1008 
1009 	kfree_rcu(ifp, rcu);
1010 }
1011 
1012 static void
1013 ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
1014 {
1015 	struct list_head *p;
1016 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
1017 
1018 	/*
1019 	 * Each device address list is sorted in order of scope -
1020 	 * global before linklocal.
1021 	 */
1022 	list_for_each(p, &idev->addr_list) {
1023 		struct inet6_ifaddr *ifa
1024 			= list_entry(p, struct inet6_ifaddr, if_list);
1025 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
1026 			break;
1027 	}
1028 
1029 	list_add_tail_rcu(&ifp->if_list, p);
1030 }
1031 
1032 static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
1033 {
1034 	u32 val = __ipv6_addr_jhash(addr, net_hash_mix(net));
1035 
1036 	return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
1037 }
1038 
1039 static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1040 			       struct net_device *dev, unsigned int hash)
1041 {
1042 	struct inet6_ifaddr *ifp;
1043 
1044 	hlist_for_each_entry(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
1045 		if (ipv6_addr_equal(&ifp->addr, addr)) {
1046 			if (!dev || ifp->idev->dev == dev)
1047 				return true;
1048 		}
1049 	}
1050 	return false;
1051 }
1052 
1053 static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
1054 {
1055 	struct net *net = dev_net(dev);
1056 	unsigned int hash = inet6_addr_hash(net, &ifa->addr);
1057 	int err = 0;
1058 
1059 	spin_lock_bh(&net->ipv6.addrconf_hash_lock);
1060 
1061 	/* Ignore adding duplicate addresses on an interface */
1062 	if (ipv6_chk_same_addr(net, &ifa->addr, dev, hash)) {
1063 		netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
1064 		err = -EEXIST;
1065 	} else {
1066 		hlist_add_head_rcu(&ifa->addr_lst, &net->ipv6.inet6_addr_lst[hash]);
1067 	}
1068 
1069 	spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
1070 
1071 	return err;
1072 }
1073 
1074 /* On success it returns ifp with increased reference count */
1075 
1076 static struct inet6_ifaddr *
1077 ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
1078 	      bool can_block, struct netlink_ext_ack *extack)
1079 {
1080 	gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
1081 	int addr_type = ipv6_addr_type(cfg->pfx);
1082 	struct net *net = dev_net(idev->dev);
1083 	struct inet6_ifaddr *ifa = NULL;
1084 	struct fib6_info *f6i = NULL;
1085 	int err = 0;
1086 
1087 	if (addr_type == IPV6_ADDR_ANY) {
1088 		NL_SET_ERR_MSG_MOD(extack, "Invalid address");
1089 		return ERR_PTR(-EADDRNOTAVAIL);
1090 	} else if (addr_type & IPV6_ADDR_MULTICAST &&
1091 		   !(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) {
1092 		NL_SET_ERR_MSG_MOD(extack, "Cannot assign multicast address without \"IFA_F_MCAUTOJOIN\" flag");
1093 		return ERR_PTR(-EADDRNOTAVAIL);
1094 	} else if (!(idev->dev->flags & IFF_LOOPBACK) &&
1095 		   !netif_is_l3_master(idev->dev) &&
1096 		   addr_type & IPV6_ADDR_LOOPBACK) {
1097 		NL_SET_ERR_MSG_MOD(extack, "Cannot assign loopback address on this device");
1098 		return ERR_PTR(-EADDRNOTAVAIL);
1099 	}
1100 
1101 	if (idev->dead) {
1102 		NL_SET_ERR_MSG_MOD(extack, "device is going away");
1103 		err = -ENODEV;
1104 		goto out;
1105 	}
1106 
1107 	if (idev->cnf.disable_ipv6) {
1108 		NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
1109 		err = -EACCES;
1110 		goto out;
1111 	}
1112 
1113 	/* validator notifier needs to be blocking;
1114 	 * do not call in atomic context
1115 	 */
1116 	if (can_block) {
1117 		struct in6_validator_info i6vi = {
1118 			.i6vi_addr = *cfg->pfx,
1119 			.i6vi_dev = idev,
1120 			.extack = extack,
1121 		};
1122 
1123 		err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
1124 		err = notifier_to_errno(err);
1125 		if (err < 0)
1126 			goto out;
1127 	}
1128 
1129 	ifa = kzalloc_obj(*ifa, gfp_flags | __GFP_ACCOUNT);
1130 	if (!ifa) {
1131 		err = -ENOBUFS;
1132 		goto out;
1133 	}
1134 
1135 	f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags, extack);
1136 	if (IS_ERR(f6i)) {
1137 		err = PTR_ERR(f6i);
1138 		f6i = NULL;
1139 		goto out;
1140 	}
1141 
1142 	neigh_parms_data_state_setall(idev->nd_parms);
1143 
1144 	ifa->addr = *cfg->pfx;
1145 	if (cfg->peer_pfx)
1146 		ifa->peer_addr = *cfg->peer_pfx;
1147 
1148 	spin_lock_init(&ifa->lock);
1149 	INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
1150 	INIT_HLIST_NODE(&ifa->addr_lst);
1151 	ifa->scope = cfg->scope;
1152 	ifa->prefix_len = cfg->plen;
1153 	ifa->rt_priority = cfg->rt_priority;
1154 	ifa->flags = cfg->ifa_flags;
1155 	ifa->ifa_proto = cfg->ifa_proto;
1156 	/* No need to add the TENTATIVE flag for addresses with NODAD */
1157 	if (!(cfg->ifa_flags & IFA_F_NODAD))
1158 		ifa->flags |= IFA_F_TENTATIVE;
1159 	ifa->valid_lft = cfg->valid_lft;
1160 	ifa->prefered_lft = cfg->preferred_lft;
1161 	ifa->tokenized = false;
1162 
1163 	ifa->rt = f6i;
1164 	ifa->ifpub = cfg->ifpub;
1165 	if (cfg->ifpub) {
1166 		ifa->cstamp = cfg->cstamp;
1167 		ifa->tstamp = cfg->tstamp;
1168 	} else {
1169 		unsigned long now = jiffies;
1170 
1171 		ifa->cstamp = now;
1172 		ifa->tstamp = now;
1173 	}
1174 
1175 	ifa->idev = idev;
1176 	in6_dev_hold(idev);
1177 
1178 	/* For caller */
1179 	refcount_set(&ifa->refcnt, 1);
1180 
1181 	rcu_read_lock();
1182 
1183 	write_lock_bh(&idev->lock);
1184 
1185 	if (idev->dead)
1186 		err = -ENODEV;
1187 	else
1188 		err = ipv6_add_addr_hash(idev->dev, ifa);
1189 	if (err < 0) {
1190 		write_unlock_bh(&idev->lock);
1191 		rcu_read_unlock();
1192 		goto out;
1193 	}
1194 
1195 	/* Add to inet6_dev unicast addr list. */
1196 	ipv6_link_dev_addr(idev, ifa);
1197 
1198 	if (ifa->flags&IFA_F_TEMPORARY) {
1199 		/* manage_tempaddrs() relies on addresses being added to the head */
1200 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
1201 		in6_ifa_hold(ifa);
1202 	}
1203 
1204 	in6_ifa_hold(ifa);
1205 	write_unlock_bh(&idev->lock);
1206 
1207 	rcu_read_unlock();
1208 
1209 	inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1210 out:
1211 	if (unlikely(err < 0)) {
1212 		fib6_info_release(f6i);
1213 
1214 		if (ifa) {
1215 			if (ifa->idev)
1216 				in6_dev_put(ifa->idev);
1217 			kfree(ifa);
1218 		}
1219 		ifa = ERR_PTR(err);
1220 	}
1221 
1222 	return ifa;
1223 }
1224 
1225 enum cleanup_prefix_rt_t {
1226 	CLEANUP_PREFIX_RT_NOP,    /* no cleanup action for prefix route */
1227 	CLEANUP_PREFIX_RT_DEL,    /* delete the prefix route */
1228 	CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1229 };
1230 
1231 /*
1232  * Check, whether the prefix for ifp would still need a prefix route
1233  * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1234  * constants.
1235  *
1236  * 1) we don't purge prefix if address was not permanent.
1237  *    prefix is managed by its own lifetime.
1238  * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1239  * 3) if there are no addresses, delete prefix.
1240  * 4) if there are still other permanent address(es),
1241  *    corresponding prefix is still permanent.
1242  * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1243  *    don't purge the prefix, assume user space is managing it.
1244  * 6) otherwise, update prefix lifetime to the
1245  *    longest valid lifetime among the corresponding
1246  *    addresses on the device.
1247  *    Note: subsequent RA will update lifetime.
1248  **/
1249 static enum cleanup_prefix_rt_t
1250 check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1251 {
1252 	struct inet6_ifaddr *ifa;
1253 	struct inet6_dev *idev = ifp->idev;
1254 	unsigned long lifetime;
1255 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1256 
1257 	*expires = jiffies;
1258 
1259 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1260 		if (ifa == ifp)
1261 			continue;
1262 		if (ifa->prefix_len != ifp->prefix_len ||
1263 		    !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1264 				       ifp->prefix_len))
1265 			continue;
1266 		if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1267 			return CLEANUP_PREFIX_RT_NOP;
1268 
1269 		action = CLEANUP_PREFIX_RT_EXPIRE;
1270 
1271 		spin_lock(&ifa->lock);
1272 
1273 		lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1274 		/*
1275 		 * Note: Because this address is
1276 		 * not permanent, lifetime <
1277 		 * LONG_MAX / HZ here.
1278 		 */
1279 		if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1280 			*expires = ifa->tstamp + lifetime * HZ;
1281 		spin_unlock(&ifa->lock);
1282 	}
1283 
1284 	return action;
1285 }
1286 
1287 static void
1288 cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires,
1289 		     bool del_rt, bool del_peer)
1290 {
1291 	struct net *net = dev_net(ifp->idev->dev);
1292 	struct fib6_table *table;
1293 	struct fib6_info *f6i;
1294 
1295 	f6i = addrconf_get_prefix_route(del_peer ? &ifp->peer_addr : &ifp->addr,
1296 					ifp->prefix_len,
1297 					ifp->idev->dev, 0, RTF_DEFAULT, true);
1298 	if (f6i) {
1299 		if (del_rt)
1300 			ip6_del_rt(net, f6i, false);
1301 		else {
1302 			if (f6i != net->ipv6.fib6_null_entry &&
1303 			    !(f6i->fib6_flags & RTF_EXPIRES)) {
1304 				table = f6i->fib6_table;
1305 				spin_lock_bh(&table->tb6_lock);
1306 
1307 				fib6_set_expires(f6i, expires);
1308 				fib6_add_gc_list(f6i);
1309 
1310 				spin_unlock_bh(&table->tb6_lock);
1311 			}
1312 			fib6_info_release(f6i);
1313 		}
1314 	}
1315 }
1316 
1317 
1318 /* This function wants to get referenced ifp and releases it before return */
1319 
1320 static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1321 {
1322 	enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1323 	struct net *net = dev_net(ifp->idev->dev);
1324 	unsigned long expires;
1325 	int state;
1326 
1327 	ASSERT_RTNL();
1328 
1329 	spin_lock_bh(&ifp->lock);
1330 	state = ifp->state;
1331 	ifp->state = INET6_IFADDR_STATE_DEAD;
1332 	spin_unlock_bh(&ifp->lock);
1333 
1334 	if (state == INET6_IFADDR_STATE_DEAD)
1335 		goto out;
1336 
1337 	spin_lock_bh(&net->ipv6.addrconf_hash_lock);
1338 	hlist_del_init_rcu(&ifp->addr_lst);
1339 	spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
1340 
1341 	write_lock_bh(&ifp->idev->lock);
1342 
1343 	if (ifp->flags&IFA_F_TEMPORARY) {
1344 		list_del(&ifp->tmp_list);
1345 		if (ifp->ifpub) {
1346 			in6_ifa_put(ifp->ifpub);
1347 			ifp->ifpub = NULL;
1348 		}
1349 		__in6_ifa_put(ifp);
1350 	}
1351 
1352 	if (!(ifp->flags & IFA_F_NOPREFIXROUTE))
1353 		action = check_cleanup_prefix_route(ifp, &expires);
1354 
1355 	list_del_rcu(&ifp->if_list);
1356 	__in6_ifa_put(ifp);
1357 
1358 	write_unlock_bh(&ifp->idev->lock);
1359 
1360 	addrconf_del_dad_work(ifp);
1361 
1362 	ipv6_ifa_notify(RTM_DELADDR, ifp);
1363 
1364 	inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
1365 
1366 	if (action != CLEANUP_PREFIX_RT_NOP) {
1367 		cleanup_prefix_route(ifp, expires,
1368 			action == CLEANUP_PREFIX_RT_DEL, false);
1369 	}
1370 
1371 	/* clean up prefsrc entries */
1372 	rt6_remove_prefsrc(ifp);
1373 out:
1374 	in6_ifa_put(ifp);
1375 }
1376 
1377 static unsigned long ipv6_get_regen_advance(const struct inet6_dev *idev)
1378 {
1379 	return READ_ONCE(idev->cnf.regen_min_advance) +
1380 		READ_ONCE(idev->cnf.regen_max_retry) *
1381 		READ_ONCE(idev->cnf.dad_transmits) *
1382 		max(NEIGH_VAR(idev->nd_parms, RETRANS_TIME), HZ/100) / HZ;
1383 }
1384 
1385 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, bool block)
1386 {
1387 	struct inet6_dev *idev = ifp->idev;
1388 	unsigned long tmp_tstamp, age;
1389 	unsigned long regen_advance;
1390 	unsigned long now = jiffies;
1391 	u32 if_public_preferred_lft;
1392 	s32 cnf_temp_preferred_lft;
1393 	struct inet6_ifaddr *ift;
1394 	struct ifa6_config cfg;
1395 	long max_desync_factor;
1396 	struct in6_addr addr;
1397 	int ret = 0;
1398 
1399 	write_lock_bh(&idev->lock);
1400 
1401 retry:
1402 	in6_dev_hold(idev);
1403 	if (READ_ONCE(idev->cnf.use_tempaddr) <= 0) {
1404 		write_unlock_bh(&idev->lock);
1405 		pr_info("%s: use_tempaddr is disabled\n", __func__);
1406 		in6_dev_put(idev);
1407 		ret = -1;
1408 		goto out;
1409 	}
1410 	spin_lock_bh(&ifp->lock);
1411 	if (ifp->regen_count++ >= READ_ONCE(idev->cnf.regen_max_retry)) {
1412 		WRITE_ONCE(idev->cnf.use_tempaddr, -1);	/*XXX*/
1413 		spin_unlock_bh(&ifp->lock);
1414 		write_unlock_bh(&idev->lock);
1415 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1416 			__func__);
1417 		in6_dev_put(idev);
1418 		ret = -1;
1419 		goto out;
1420 	}
1421 	in6_ifa_hold(ifp);
1422 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1423 	ipv6_gen_rnd_iid(&addr);
1424 
1425 	age = (now - ifp->tstamp) / HZ;
1426 
1427 	regen_advance = ipv6_get_regen_advance(idev);
1428 
1429 	/* recalculate max_desync_factor each time and update
1430 	 * idev->desync_factor if it's larger
1431 	 */
1432 	cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
1433 	max_desync_factor = min_t(long,
1434 				  READ_ONCE(idev->cnf.max_desync_factor),
1435 				  cnf_temp_preferred_lft - regen_advance);
1436 
1437 	if (unlikely(idev->desync_factor > max_desync_factor)) {
1438 		if (max_desync_factor > 0) {
1439 			get_random_bytes(&idev->desync_factor,
1440 					 sizeof(idev->desync_factor));
1441 			idev->desync_factor %= max_desync_factor;
1442 		} else {
1443 			idev->desync_factor = 0;
1444 		}
1445 	}
1446 
1447 	if_public_preferred_lft = ifp->prefered_lft;
1448 
1449 	memset(&cfg, 0, sizeof(cfg));
1450 	cfg.valid_lft = min_t(__u32, ifp->valid_lft,
1451 			      READ_ONCE(idev->cnf.temp_valid_lft) + age);
1452 	cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
1453 	cfg.preferred_lft = min_t(__u32, if_public_preferred_lft, cfg.preferred_lft);
1454 	cfg.preferred_lft = min_t(__u32, cfg.valid_lft, cfg.preferred_lft);
1455 
1456 	cfg.plen = ifp->prefix_len;
1457 	tmp_tstamp = ifp->tstamp;
1458 	spin_unlock_bh(&ifp->lock);
1459 
1460 	write_unlock_bh(&idev->lock);
1461 
1462 	/* From RFC 4941:
1463 	 *
1464 	 *     A temporary address is created only if this calculated Preferred
1465 	 *     Lifetime is greater than REGEN_ADVANCE time units.  In
1466 	 *     particular, an implementation must not create a temporary address
1467 	 *     with a zero Preferred Lifetime.
1468 	 *
1469 	 *     ...
1470 	 *
1471 	 *     When creating a temporary address, the lifetime values MUST be
1472 	 *     derived from the corresponding prefix as follows:
1473 	 *
1474 	 *     ...
1475 	 *
1476 	 *     *  Its Preferred Lifetime is the lower of the Preferred Lifetime
1477 	 *        of the public address or TEMP_PREFERRED_LIFETIME -
1478 	 *        DESYNC_FACTOR.
1479 	 *
1480 	 * To comply with the RFC's requirements, clamp the preferred lifetime
1481 	 * to a minimum of regen_advance, unless that would exceed valid_lft or
1482 	 * ifp->prefered_lft.
1483 	 *
1484 	 * Use age calculation as in addrconf_verify to avoid unnecessary
1485 	 * temporary addresses being generated.
1486 	 */
1487 	age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1488 	if (cfg.preferred_lft <= regen_advance + age) {
1489 		cfg.preferred_lft = regen_advance + age + 1;
1490 		if (cfg.preferred_lft <= regen_advance + age ||
1491 		    cfg.preferred_lft > cfg.valid_lft ||
1492 		    cfg.preferred_lft > if_public_preferred_lft) {
1493 			in6_ifa_put(ifp);
1494 			in6_dev_put(idev);
1495 			ret = -1;
1496 			goto out;
1497 		}
1498 	}
1499 
1500 	cfg.ifa_flags = IFA_F_TEMPORARY;
1501 	/* set in addrconf_prefix_rcv() */
1502 	if (ifp->flags & IFA_F_OPTIMISTIC)
1503 		cfg.ifa_flags |= IFA_F_OPTIMISTIC;
1504 
1505 	cfg.pfx = &addr;
1506 	cfg.scope = ipv6_addr_scope(cfg.pfx);
1507 	cfg.ifpub = ifp;
1508 	cfg.cstamp = now;
1509 	cfg.tstamp = tmp_tstamp;
1510 
1511 	ift = ipv6_add_addr(idev, &cfg, block, NULL);
1512 	if (IS_ERR(ift)) {
1513 		in6_ifa_put(ifp);
1514 		in6_dev_put(idev);
1515 		pr_info("%s: retry temporary address regeneration\n", __func__);
1516 		write_lock_bh(&idev->lock);
1517 		goto retry;
1518 	}
1519 
1520 	addrconf_dad_start(ift);
1521 	in6_ifa_put(ift);
1522 	in6_dev_put(idev);
1523 out:
1524 	return ret;
1525 }
1526 
1527 /*
1528  *	Choose an appropriate source address (RFC3484)
1529  */
1530 enum {
1531 	IPV6_SADDR_RULE_INIT = 0,
1532 	IPV6_SADDR_RULE_LOCAL,
1533 	IPV6_SADDR_RULE_SCOPE,
1534 	IPV6_SADDR_RULE_PREFERRED,
1535 #ifdef CONFIG_IPV6_MIP6
1536 	IPV6_SADDR_RULE_HOA,
1537 #endif
1538 	IPV6_SADDR_RULE_OIF,
1539 	IPV6_SADDR_RULE_LABEL,
1540 	IPV6_SADDR_RULE_PRIVACY,
1541 	IPV6_SADDR_RULE_ORCHID,
1542 	IPV6_SADDR_RULE_PREFIX,
1543 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1544 	IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1545 #endif
1546 	IPV6_SADDR_RULE_MAX
1547 };
1548 
1549 struct ipv6_saddr_score {
1550 	int			rule;
1551 	int			addr_type;
1552 	struct inet6_ifaddr	*ifa;
1553 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1554 	int			scopedist;
1555 	int			matchlen;
1556 };
1557 
1558 struct ipv6_saddr_dst {
1559 	const struct in6_addr *addr;
1560 	int ifindex;
1561 	int scope;
1562 	int label;
1563 	unsigned int prefs;
1564 };
1565 
1566 static inline int ipv6_saddr_preferred(int type)
1567 {
1568 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1569 		return 1;
1570 	return 0;
1571 }
1572 
1573 static bool ipv6_use_optimistic_addr(const struct net *net,
1574 				     const struct inet6_dev *idev)
1575 {
1576 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1577 	if (!idev)
1578 		return false;
1579 	if (!READ_ONCE(net->ipv6.devconf_all->optimistic_dad) &&
1580 	    !READ_ONCE(idev->cnf.optimistic_dad))
1581 		return false;
1582 	if (!READ_ONCE(net->ipv6.devconf_all->use_optimistic) &&
1583 	    !READ_ONCE(idev->cnf.use_optimistic))
1584 		return false;
1585 
1586 	return true;
1587 #else
1588 	return false;
1589 #endif
1590 }
1591 
1592 static bool ipv6_allow_optimistic_dad(const struct net *net,
1593 				      const struct inet6_dev *idev)
1594 {
1595 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1596 	if (!idev)
1597 		return false;
1598 	if (!READ_ONCE(net->ipv6.devconf_all->optimistic_dad) &&
1599 	    !READ_ONCE(idev->cnf.optimistic_dad))
1600 		return false;
1601 
1602 	return true;
1603 #else
1604 	return false;
1605 #endif
1606 }
1607 
1608 static int ipv6_get_saddr_eval(struct net *net,
1609 			       struct ipv6_saddr_score *score,
1610 			       struct ipv6_saddr_dst *dst,
1611 			       int i)
1612 {
1613 	int ret;
1614 
1615 	if (i <= score->rule) {
1616 		switch (i) {
1617 		case IPV6_SADDR_RULE_SCOPE:
1618 			ret = score->scopedist;
1619 			break;
1620 		case IPV6_SADDR_RULE_PREFIX:
1621 			ret = score->matchlen;
1622 			break;
1623 		default:
1624 			ret = !!test_bit(i, score->scorebits);
1625 		}
1626 		goto out;
1627 	}
1628 
1629 	switch (i) {
1630 	case IPV6_SADDR_RULE_INIT:
1631 		/* Rule 0: remember if hiscore is not ready yet */
1632 		ret = !!score->ifa;
1633 		break;
1634 	case IPV6_SADDR_RULE_LOCAL:
1635 		/* Rule 1: Prefer same address */
1636 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1637 		break;
1638 	case IPV6_SADDR_RULE_SCOPE:
1639 		/* Rule 2: Prefer appropriate scope
1640 		 *
1641 		 *      ret
1642 		 *       ^
1643 		 *    -1 |  d 15
1644 		 *    ---+--+-+---> scope
1645 		 *       |
1646 		 *       |             d is scope of the destination.
1647 		 *  B-d  |  \
1648 		 *       |   \      <- smaller scope is better if
1649 		 *  B-15 |    \        if scope is enough for destination.
1650 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1651 		 * d-C-1 | /
1652 		 *       |/         <- greater is better
1653 		 *   -C  /             if scope is not enough for destination.
1654 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1655 		 *
1656 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1657 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1658 		 * Assume B = 0 and we get C > 29.
1659 		 */
1660 		ret = __ipv6_addr_src_scope(score->addr_type);
1661 		if (ret >= dst->scope)
1662 			ret = -ret;
1663 		else
1664 			ret -= 128;	/* 30 is enough */
1665 		score->scopedist = ret;
1666 		break;
1667 	case IPV6_SADDR_RULE_PREFERRED:
1668 	    {
1669 		/* Rule 3: Avoid deprecated and optimistic addresses */
1670 		u8 avoid = IFA_F_DEPRECATED;
1671 
1672 		if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
1673 			avoid |= IFA_F_OPTIMISTIC;
1674 		ret = ipv6_saddr_preferred(score->addr_type) ||
1675 		      !(score->ifa->flags & avoid);
1676 		break;
1677 	    }
1678 #ifdef CONFIG_IPV6_MIP6
1679 	case IPV6_SADDR_RULE_HOA:
1680 	    {
1681 		/* Rule 4: Prefer home address */
1682 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1683 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1684 		break;
1685 	    }
1686 #endif
1687 	case IPV6_SADDR_RULE_OIF:
1688 		/* Rule 5: Prefer outgoing interface */
1689 		ret = (!dst->ifindex ||
1690 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1691 		break;
1692 	case IPV6_SADDR_RULE_LABEL:
1693 		/* Rule 6: Prefer matching label */
1694 		ret = ipv6_addr_label(net,
1695 				      &score->ifa->addr, score->addr_type,
1696 				      score->ifa->idev->dev->ifindex) == dst->label;
1697 		break;
1698 	case IPV6_SADDR_RULE_PRIVACY:
1699 	    {
1700 		/* Rule 7: Prefer public address
1701 		 * Note: prefer temporary address if use_tempaddr >= 2
1702 		 */
1703 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1704 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1705 				READ_ONCE(score->ifa->idev->cnf.use_tempaddr) >= 2;
1706 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1707 		break;
1708 	    }
1709 	case IPV6_SADDR_RULE_ORCHID:
1710 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1711 		 *	    non-ORCHID vs non-ORCHID
1712 		 */
1713 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1714 			ipv6_addr_orchid(dst->addr));
1715 		break;
1716 	case IPV6_SADDR_RULE_PREFIX:
1717 		/* Rule 8: Use longest matching prefix */
1718 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1719 		if (ret > score->ifa->prefix_len)
1720 			ret = score->ifa->prefix_len;
1721 		score->matchlen = ret;
1722 		break;
1723 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1724 	case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1725 		/* Optimistic addresses still have lower precedence than other
1726 		 * preferred addresses.
1727 		 */
1728 		ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1729 		break;
1730 #endif
1731 	default:
1732 		ret = 0;
1733 	}
1734 
1735 	if (ret)
1736 		__set_bit(i, score->scorebits);
1737 	score->rule = i;
1738 out:
1739 	return ret;
1740 }
1741 
1742 static int __ipv6_dev_get_saddr(struct net *net,
1743 				struct ipv6_saddr_dst *dst,
1744 				struct inet6_dev *idev,
1745 				struct ipv6_saddr_score *scores,
1746 				int hiscore_idx)
1747 {
1748 	struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
1749 
1750 	list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
1751 		int i;
1752 
1753 		/*
1754 		 * - Tentative Address (RFC2462 section 5.4)
1755 		 *  - A tentative address is not considered
1756 		 *    "assigned to an interface" in the traditional
1757 		 *    sense, unless it is also flagged as optimistic.
1758 		 * - Candidate Source Address (section 4)
1759 		 *  - In any case, anycast addresses, multicast
1760 		 *    addresses, and the unspecified address MUST
1761 		 *    NOT be included in a candidate set.
1762 		 */
1763 		if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1764 		    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1765 			continue;
1766 
1767 		score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1768 
1769 		if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1770 			     score->addr_type & IPV6_ADDR_MULTICAST)) {
1771 			net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1772 					    idev->dev->name);
1773 			continue;
1774 		}
1775 
1776 		score->rule = -1;
1777 		bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1778 
1779 		for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1780 			int minihiscore, miniscore;
1781 
1782 			minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1783 			miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1784 
1785 			if (minihiscore > miniscore) {
1786 				if (i == IPV6_SADDR_RULE_SCOPE &&
1787 				    score->scopedist > 0) {
1788 					/*
1789 					 * special case:
1790 					 * each remaining entry
1791 					 * has too small (not enough)
1792 					 * scope, because ifa entries
1793 					 * are sorted by their scope
1794 					 * values.
1795 					 */
1796 					goto out;
1797 				}
1798 				break;
1799 			} else if (minihiscore < miniscore) {
1800 				swap(hiscore, score);
1801 				hiscore_idx = 1 - hiscore_idx;
1802 
1803 				/* restore our iterator */
1804 				score->ifa = hiscore->ifa;
1805 
1806 				break;
1807 			}
1808 		}
1809 	}
1810 out:
1811 	return hiscore_idx;
1812 }
1813 
1814 static int ipv6_get_saddr_master(struct net *net,
1815 				 const struct net_device *dst_dev,
1816 				 const struct net_device *master,
1817 				 struct ipv6_saddr_dst *dst,
1818 				 struct ipv6_saddr_score *scores,
1819 				 int hiscore_idx)
1820 {
1821 	struct inet6_dev *idev;
1822 
1823 	idev = __in6_dev_get(dst_dev);
1824 	if (idev)
1825 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1826 						   scores, hiscore_idx);
1827 
1828 	idev = __in6_dev_get(master);
1829 	if (idev)
1830 		hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1831 						   scores, hiscore_idx);
1832 
1833 	return hiscore_idx;
1834 }
1835 
1836 int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
1837 		       const struct in6_addr *daddr, unsigned int prefs,
1838 		       struct in6_addr *saddr)
1839 {
1840 	struct ipv6_saddr_score scores[2], *hiscore;
1841 	struct ipv6_saddr_dst dst;
1842 	struct inet6_dev *idev;
1843 	struct net_device *dev;
1844 	int dst_type;
1845 	bool use_oif_addr = false;
1846 	int hiscore_idx = 0;
1847 	int ret = 0;
1848 
1849 	dst_type = __ipv6_addr_type(daddr);
1850 	dst.addr = daddr;
1851 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1852 	dst.scope = __ipv6_addr_src_scope(dst_type);
1853 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
1854 	dst.prefs = prefs;
1855 
1856 	scores[hiscore_idx].rule = -1;
1857 	scores[hiscore_idx].ifa = NULL;
1858 
1859 	rcu_read_lock();
1860 
1861 	/* Candidate Source Address (section 4)
1862 	 *  - multicast and link-local destination address,
1863 	 *    the set of candidate source address MUST only
1864 	 *    include addresses assigned to interfaces
1865 	 *    belonging to the same link as the outgoing
1866 	 *    interface.
1867 	 * (- For site-local destination addresses, the
1868 	 *    set of candidate source addresses MUST only
1869 	 *    include addresses assigned to interfaces
1870 	 *    belonging to the same site as the outgoing
1871 	 *    interface.)
1872 	 *  - "It is RECOMMENDED that the candidate source addresses
1873 	 *    be the set of unicast addresses assigned to the
1874 	 *    interface that will be used to send to the destination
1875 	 *    (the 'outgoing' interface)." (RFC 6724)
1876 	 */
1877 	if (dst_dev) {
1878 		idev = __in6_dev_get(dst_dev);
1879 		if ((dst_type & IPV6_ADDR_MULTICAST) ||
1880 		    dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1881 		    (idev && READ_ONCE(idev->cnf.use_oif_addrs_only))) {
1882 			use_oif_addr = true;
1883 		}
1884 	}
1885 
1886 	if (use_oif_addr) {
1887 		if (idev)
1888 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1889 	} else {
1890 		const struct net_device *master;
1891 		int master_idx = 0;
1892 
1893 		/* if dst_dev exists and is enslaved to an L3 device, then
1894 		 * prefer addresses from dst_dev and then the master over
1895 		 * any other enslaved devices in the L3 domain.
1896 		 */
1897 		master = l3mdev_master_dev_rcu(dst_dev);
1898 		if (master) {
1899 			master_idx = master->ifindex;
1900 
1901 			hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1902 							    master, &dst,
1903 							    scores, hiscore_idx);
1904 
1905 			hiscore = &scores[hiscore_idx];
1906 			if (hiscore->ifa &&
1907 			    __ipv6_addr_src_scope(hiscore->addr_type) >=
1908 			    dst.scope)
1909 				goto out;
1910 		}
1911 
1912 		for_each_netdev_rcu(net, dev) {
1913 			/* only consider addresses on devices in the
1914 			 * same L3 domain
1915 			 */
1916 			if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1917 				continue;
1918 			idev = __in6_dev_get(dev);
1919 			if (!idev)
1920 				continue;
1921 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1922 		}
1923 	}
1924 
1925 out:
1926 	hiscore = &scores[hiscore_idx];
1927 	if (!hiscore->ifa)
1928 		ret = -EADDRNOTAVAIL;
1929 	else
1930 		*saddr = hiscore->ifa->addr;
1931 
1932 	rcu_read_unlock();
1933 	return ret;
1934 }
1935 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1936 
1937 static int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1938 			      u32 banned_flags)
1939 {
1940 	struct inet6_ifaddr *ifp;
1941 	int err = -EADDRNOTAVAIL;
1942 
1943 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1944 		if (ifp->scope > IFA_LINK)
1945 			break;
1946 		if (ifp->scope == IFA_LINK &&
1947 		    !(ifp->flags & banned_flags)) {
1948 			*addr = ifp->addr;
1949 			err = 0;
1950 			break;
1951 		}
1952 	}
1953 	return err;
1954 }
1955 
1956 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1957 		    u32 banned_flags)
1958 {
1959 	struct inet6_dev *idev;
1960 	int err = -EADDRNOTAVAIL;
1961 
1962 	rcu_read_lock();
1963 	idev = __in6_dev_get(dev);
1964 	if (idev) {
1965 		read_lock_bh(&idev->lock);
1966 		err = __ipv6_get_lladdr(idev, addr, banned_flags);
1967 		read_unlock_bh(&idev->lock);
1968 	}
1969 	rcu_read_unlock();
1970 	return err;
1971 }
1972 
1973 static int ipv6_count_addresses(const struct inet6_dev *idev)
1974 {
1975 	const struct inet6_ifaddr *ifp;
1976 	int cnt = 0;
1977 
1978 	rcu_read_lock();
1979 	list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
1980 		cnt++;
1981 	rcu_read_unlock();
1982 	return cnt;
1983 }
1984 
1985 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1986 		  const struct net_device *dev, int strict)
1987 {
1988 	return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1989 				       strict, IFA_F_TENTATIVE);
1990 }
1991 EXPORT_SYMBOL(ipv6_chk_addr);
1992 
1993 /* device argument is used to find the L3 domain of interest. If
1994  * skip_dev_check is set, then the ifp device is not checked against
1995  * the passed in dev argument. So the 2 cases for addresses checks are:
1996  *   1. does the address exist in the L3 domain that dev is part of
1997  *      (skip_dev_check = true), or
1998  *
1999  *   2. does the address exist on the specific device
2000  *      (skip_dev_check = false)
2001  */
2002 static struct net_device *
2003 __ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
2004 			  const struct net_device *dev, bool skip_dev_check,
2005 			  int strict, u32 banned_flags)
2006 {
2007 	unsigned int hash = inet6_addr_hash(net, addr);
2008 	struct net_device *l3mdev, *ndev;
2009 	struct inet6_ifaddr *ifp;
2010 	u32 ifp_flags;
2011 
2012 	rcu_read_lock();
2013 
2014 	l3mdev = l3mdev_master_dev_rcu(dev);
2015 	if (skip_dev_check)
2016 		dev = NULL;
2017 
2018 	hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
2019 		ndev = ifp->idev->dev;
2020 
2021 		if (l3mdev_master_dev_rcu(ndev) != l3mdev)
2022 			continue;
2023 
2024 		/* Decouple optimistic from tentative for evaluation here.
2025 		 * Ban optimistic addresses explicitly, when required.
2026 		 */
2027 		ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
2028 			    ? (ifp->flags&~IFA_F_TENTATIVE)
2029 			    : ifp->flags;
2030 		if (ipv6_addr_equal(&ifp->addr, addr) &&
2031 		    !(ifp_flags&banned_flags) &&
2032 		    (!dev || ndev == dev ||
2033 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
2034 			rcu_read_unlock();
2035 			return ndev;
2036 		}
2037 	}
2038 
2039 	rcu_read_unlock();
2040 	return NULL;
2041 }
2042 
2043 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
2044 			    const struct net_device *dev, bool skip_dev_check,
2045 			    int strict, u32 banned_flags)
2046 {
2047 	return __ipv6_chk_addr_and_flags(net, addr, dev, skip_dev_check,
2048 					 strict, banned_flags) ? 1 : 0;
2049 }
2050 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
2051 
2052 
2053 /* Compares an address/prefix_len with addresses on device @dev.
2054  * If one is found it returns true.
2055  */
2056 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
2057 	const unsigned int prefix_len, struct net_device *dev)
2058 {
2059 	const struct inet6_ifaddr *ifa;
2060 	const struct inet6_dev *idev;
2061 	bool ret = false;
2062 
2063 	rcu_read_lock();
2064 	idev = __in6_dev_get(dev);
2065 	if (idev) {
2066 		list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
2067 			ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
2068 			if (ret)
2069 				break;
2070 		}
2071 	}
2072 	rcu_read_unlock();
2073 
2074 	return ret;
2075 }
2076 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
2077 
2078 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
2079 {
2080 	const struct inet6_ifaddr *ifa;
2081 	const struct inet6_dev *idev;
2082 	int	onlink;
2083 
2084 	onlink = 0;
2085 	rcu_read_lock();
2086 	idev = __in6_dev_get(dev);
2087 	if (idev) {
2088 		list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
2089 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
2090 						   ifa->prefix_len);
2091 			if (onlink)
2092 				break;
2093 		}
2094 	}
2095 	rcu_read_unlock();
2096 	return onlink;
2097 }
2098 EXPORT_SYMBOL(ipv6_chk_prefix);
2099 
2100 /**
2101  * ipv6_dev_find - find the first device with a given source address.
2102  * @net: the net namespace
2103  * @addr: the source address
2104  * @dev: used to find the L3 domain of interest
2105  *
2106  * The caller should be protected by RCU, or RTNL.
2107  */
2108 struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr,
2109 				 struct net_device *dev)
2110 {
2111 	return __ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1,
2112 					 IFA_F_TENTATIVE);
2113 }
2114 EXPORT_SYMBOL(ipv6_dev_find);
2115 
2116 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
2117 				     struct net_device *dev, int strict)
2118 {
2119 	unsigned int hash = inet6_addr_hash(net, addr);
2120 	struct inet6_ifaddr *ifp, *result = NULL;
2121 
2122 	rcu_read_lock();
2123 	hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
2124 		if (ipv6_addr_equal(&ifp->addr, addr)) {
2125 			if (!dev || ifp->idev->dev == dev ||
2126 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
2127 				if (in6_ifa_hold_safe(ifp)) {
2128 					result = ifp;
2129 					break;
2130 				}
2131 			}
2132 		}
2133 	}
2134 	rcu_read_unlock();
2135 
2136 	return result;
2137 }
2138 
2139 /* Gets referenced address, destroys ifaddr */
2140 
2141 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
2142 {
2143 	if (dad_failed)
2144 		ifp->flags |= IFA_F_DADFAILED;
2145 
2146 	if (ifp->flags&IFA_F_TEMPORARY) {
2147 		struct inet6_ifaddr *ifpub;
2148 		spin_lock_bh(&ifp->lock);
2149 		ifpub = ifp->ifpub;
2150 		if (ifpub) {
2151 			in6_ifa_hold(ifpub);
2152 			spin_unlock_bh(&ifp->lock);
2153 			ipv6_create_tempaddr(ifpub, true);
2154 			in6_ifa_put(ifpub);
2155 		} else {
2156 			spin_unlock_bh(&ifp->lock);
2157 		}
2158 		ipv6_del_addr(ifp);
2159 	} else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
2160 		spin_lock_bh(&ifp->lock);
2161 		addrconf_del_dad_work(ifp);
2162 		ifp->flags |= IFA_F_TENTATIVE;
2163 		if (dad_failed)
2164 			ifp->flags &= ~IFA_F_OPTIMISTIC;
2165 		spin_unlock_bh(&ifp->lock);
2166 		if (dad_failed)
2167 			ipv6_ifa_notify(0, ifp);
2168 		in6_ifa_put(ifp);
2169 	} else {
2170 		ipv6_del_addr(ifp);
2171 	}
2172 }
2173 
2174 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2175 {
2176 	int err = -ENOENT;
2177 
2178 	spin_lock_bh(&ifp->lock);
2179 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
2180 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
2181 		err = 0;
2182 	}
2183 	spin_unlock_bh(&ifp->lock);
2184 
2185 	return err;
2186 }
2187 
2188 void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
2189 {
2190 	struct inet6_dev *idev = ifp->idev;
2191 	struct net *net = dev_net(idev->dev);
2192 	int max_addresses;
2193 
2194 	spin_lock_bh(&ifp->lock);
2195 
2196 	if (ifp->state != INET6_IFADDR_STATE_DAD) {
2197 		spin_unlock_bh(&ifp->lock);
2198 		in6_ifa_put(ifp);
2199 		return;
2200 	}
2201 	ifp->state = INET6_IFADDR_STATE_POSTDAD;
2202 
2203 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2204 			     ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
2205 
2206 	if (ifp->flags & IFA_F_STABLE_PRIVACY) {
2207 		bool start_dad = false;
2208 		struct in6_addr new_addr;
2209 		struct inet6_ifaddr *ifp2;
2210 		int retries = ifp->stable_privacy_retry + 1;
2211 		struct ifa6_config cfg = {
2212 			.pfx = &new_addr,
2213 			.plen = ifp->prefix_len,
2214 			.ifa_flags = ifp->flags,
2215 			.valid_lft = ifp->valid_lft,
2216 			.preferred_lft = ifp->prefered_lft,
2217 			.scope = ifp->scope,
2218 		};
2219 
2220 		if (retries > net->ipv6.sysctl.idgen_retries) {
2221 			net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
2222 					     ifp->idev->dev->name);
2223 			goto errdad;
2224 		}
2225 
2226 		new_addr = ifp->addr;
2227 		if (ipv6_generate_stable_address(&new_addr, retries,
2228 						 idev))
2229 			goto errdad;
2230 
2231 		spin_unlock_bh(&ifp->lock);
2232 
2233 		max_addresses = READ_ONCE(idev->cnf.max_addresses);
2234 		if (max_addresses &&
2235 		    ipv6_count_addresses(idev) >= max_addresses)
2236 			goto lock_errdad;
2237 
2238 		net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2239 				     ifp->idev->dev->name);
2240 
2241 		ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
2242 		if (IS_ERR(ifp2))
2243 			goto lock_errdad;
2244 
2245 		spin_lock_bh(&ifp2->lock);
2246 		if (ifp2->state != INET6_IFADDR_STATE_DEAD) {
2247 			ifp2->stable_privacy_retry = retries;
2248 			start_dad = true;
2249 		}
2250 		spin_unlock_bh(&ifp2->lock);
2251 
2252 		if (start_dad)
2253 			addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
2254 		in6_ifa_put(ifp2);
2255 lock_errdad:
2256 		spin_lock_bh(&ifp->lock);
2257 		if (ifp->state != INET6_IFADDR_STATE_POSTDAD) {
2258 			spin_unlock_bh(&ifp->lock);
2259 			in6_ifa_put(ifp);
2260 			return;
2261 		}
2262 	}
2263 
2264 errdad:
2265 	/* transition from _POSTDAD to _ERRDAD */
2266 	ifp->state = INET6_IFADDR_STATE_ERRDAD;
2267 	spin_unlock_bh(&ifp->lock);
2268 
2269 	addrconf_mod_dad_work(ifp, 0);
2270 	in6_ifa_put(ifp);
2271 }
2272 
2273 /* Join to solicited addr multicast group. */
2274 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
2275 {
2276 	struct in6_addr maddr;
2277 
2278 	if (READ_ONCE(dev->flags) & (IFF_LOOPBACK | IFF_NOARP))
2279 		return;
2280 
2281 	addrconf_addr_solict_mult(addr, &maddr);
2282 	ipv6_dev_mc_inc(dev, &maddr);
2283 }
2284 
2285 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
2286 {
2287 	struct in6_addr maddr;
2288 
2289 	if (READ_ONCE(idev->dev->flags) & (IFF_LOOPBACK | IFF_NOARP))
2290 		return;
2291 
2292 	addrconf_addr_solict_mult(addr, &maddr);
2293 	__ipv6_dev_mc_dec(idev, &maddr);
2294 }
2295 
2296 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
2297 {
2298 	struct in6_addr addr;
2299 
2300 	if (ifp->prefix_len >= 127) /* RFC 6164 */
2301 		return;
2302 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2303 	if (ipv6_addr_any(&addr))
2304 		return;
2305 	__ipv6_dev_ac_inc(ifp->idev, &addr);
2306 }
2307 
2308 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
2309 {
2310 	struct in6_addr addr;
2311 
2312 	if (ifp->prefix_len >= 127) /* RFC 6164 */
2313 		return;
2314 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2315 	if (ipv6_addr_any(&addr))
2316 		return;
2317 	__ipv6_dev_ac_dec(ifp->idev, &addr);
2318 }
2319 
2320 static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
2321 {
2322 	switch (dev->addr_len) {
2323 	case ETH_ALEN:
2324 		memcpy(eui, dev->dev_addr, 3);
2325 		eui[3] = 0xFF;
2326 		eui[4] = 0xFE;
2327 		memcpy(eui + 5, dev->dev_addr + 3, 3);
2328 		break;
2329 	case EUI64_ADDR_LEN:
2330 		memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2331 		eui[0] ^= 2;
2332 		break;
2333 	default:
2334 		return -1;
2335 	}
2336 
2337 	return 0;
2338 }
2339 
2340 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2341 {
2342 	const union fwnet_hwaddr *ha;
2343 
2344 	if (dev->addr_len != FWNET_ALEN)
2345 		return -1;
2346 
2347 	ha = (const union fwnet_hwaddr *)dev->dev_addr;
2348 
2349 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2350 	eui[0] ^= 2;
2351 	return 0;
2352 }
2353 
2354 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2355 {
2356 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
2357 	if (dev->addr_len != ARCNET_ALEN)
2358 		return -1;
2359 	memset(eui, 0, 7);
2360 	eui[7] = *(u8 *)dev->dev_addr;
2361 	return 0;
2362 }
2363 
2364 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2365 {
2366 	if (dev->addr_len != INFINIBAND_ALEN)
2367 		return -1;
2368 	memcpy(eui, dev->dev_addr + 12, 8);
2369 	eui[0] |= 2;
2370 	return 0;
2371 }
2372 
2373 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2374 {
2375 	if (addr == 0)
2376 		return -1;
2377 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2378 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2379 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2380 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2381 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2382 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2383 	eui[1] = 0;
2384 	eui[2] = 0x5E;
2385 	eui[3] = 0xFE;
2386 	memcpy(eui + 4, &addr, 4);
2387 	return 0;
2388 }
2389 
2390 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2391 {
2392 	if (dev->priv_flags & IFF_ISATAP)
2393 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2394 	return -1;
2395 }
2396 
2397 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2398 {
2399 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2400 }
2401 
2402 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2403 {
2404 	memcpy(eui, dev->perm_addr, 3);
2405 	memcpy(eui + 5, dev->perm_addr + 3, 3);
2406 	eui[3] = 0xFF;
2407 	eui[4] = 0xFE;
2408 	eui[0] ^= 2;
2409 	return 0;
2410 }
2411 
2412 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2413 {
2414 	switch (dev->type) {
2415 	case ARPHRD_ETHER:
2416 	case ARPHRD_FDDI:
2417 		return addrconf_ifid_eui48(eui, dev);
2418 	case ARPHRD_ARCNET:
2419 		return addrconf_ifid_arcnet(eui, dev);
2420 	case ARPHRD_INFINIBAND:
2421 		return addrconf_ifid_infiniband(eui, dev);
2422 	case ARPHRD_SIT:
2423 		return addrconf_ifid_sit(eui, dev);
2424 	case ARPHRD_IPGRE:
2425 	case ARPHRD_TUNNEL:
2426 		return addrconf_ifid_gre(eui, dev);
2427 	case ARPHRD_6LOWPAN:
2428 		return addrconf_ifid_6lowpan(eui, dev);
2429 	case ARPHRD_IEEE1394:
2430 		return addrconf_ifid_ieee1394(eui, dev);
2431 	case ARPHRD_TUNNEL6:
2432 	case ARPHRD_IP6GRE:
2433 	case ARPHRD_RAWIP:
2434 		return addrconf_ifid_ip6tnl(eui, dev);
2435 	}
2436 	return -1;
2437 }
2438 
2439 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2440 {
2441 	int err = -1;
2442 	struct inet6_ifaddr *ifp;
2443 
2444 	read_lock_bh(&idev->lock);
2445 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2446 		if (ifp->scope > IFA_LINK)
2447 			break;
2448 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2449 			memcpy(eui, ifp->addr.s6_addr+8, 8);
2450 			err = 0;
2451 			break;
2452 		}
2453 	}
2454 	read_unlock_bh(&idev->lock);
2455 	return err;
2456 }
2457 
2458 /* Generation of a randomized Interface Identifier
2459  * draft-ietf-6man-rfc4941bis, Section 3.3.1
2460  */
2461 
2462 static void ipv6_gen_rnd_iid(struct in6_addr *addr)
2463 {
2464 regen:
2465 	get_random_bytes(&addr->s6_addr[8], 8);
2466 
2467 	/* <draft-ietf-6man-rfc4941bis-08.txt>, Section 3.3.1:
2468 	 * check if generated address is not inappropriate:
2469 	 *
2470 	 * - Reserved IPv6 Interface Identifiers
2471 	 * - XXX: already assigned to an address on the device
2472 	 */
2473 
2474 	/* Subnet-router anycast: 0000:0000:0000:0000 */
2475 	if (!(addr->s6_addr32[2] | addr->s6_addr32[3]))
2476 		goto regen;
2477 
2478 	/* IANA Ethernet block: 0200:5EFF:FE00:0000-0200:5EFF:FE00:5212
2479 	 * Proxy Mobile IPv6:   0200:5EFF:FE00:5213
2480 	 * IANA Ethernet block: 0200:5EFF:FE00:5214-0200:5EFF:FEFF:FFFF
2481 	 */
2482 	if (ntohl(addr->s6_addr32[2]) == 0x02005eff &&
2483 	    (ntohl(addr->s6_addr32[3]) & 0Xff000000) == 0xfe000000)
2484 		goto regen;
2485 
2486 	/* Reserved subnet anycast addresses */
2487 	if (ntohl(addr->s6_addr32[2]) == 0xfdffffff &&
2488 	    ntohl(addr->s6_addr32[3]) >= 0Xffffff80)
2489 		goto regen;
2490 }
2491 
2492 /*
2493  *	Add prefix route.
2494  */
2495 
2496 static void
2497 addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
2498 		      struct net_device *dev, unsigned long expires,
2499 		      u32 flags, gfp_t gfp_flags)
2500 {
2501 	struct fib6_config cfg = {
2502 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
2503 		.fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
2504 		.fc_ifindex = dev->ifindex,
2505 		.fc_expires = expires,
2506 		.fc_dst_len = plen,
2507 		.fc_flags = RTF_UP | flags,
2508 		.fc_nlinfo.nl_net = dev_net(dev),
2509 		.fc_protocol = RTPROT_KERNEL,
2510 		.fc_type = RTN_UNICAST,
2511 	};
2512 
2513 	cfg.fc_dst = *pfx;
2514 
2515 	/* Prevent useless cloning on PtP SIT.
2516 	   This thing is done here expecting that the whole
2517 	   class of non-broadcast devices need not cloning.
2518 	 */
2519 #if IS_ENABLED(CONFIG_IPV6_SIT)
2520 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2521 		cfg.fc_flags |= RTF_NONEXTHOP;
2522 #endif
2523 
2524 	ip6_route_add(&cfg, gfp_flags, NULL);
2525 }
2526 
2527 
2528 static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2529 						  int plen,
2530 						  const struct net_device *dev,
2531 						  u32 flags, u32 noflags,
2532 						  bool no_gw)
2533 {
2534 	struct fib6_node *fn;
2535 	struct fib6_info *rt = NULL;
2536 	struct fib6_table *table;
2537 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
2538 
2539 	table = fib6_get_table(dev_net(dev), tb_id);
2540 	if (!table)
2541 		return NULL;
2542 
2543 	rcu_read_lock();
2544 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
2545 	if (!fn)
2546 		goto out;
2547 
2548 	for_each_fib6_node_rt_rcu(fn) {
2549 		/* prefix routes only use builtin fib6_nh */
2550 		if (rt->nh)
2551 			continue;
2552 
2553 		if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex)
2554 			continue;
2555 		if (no_gw && rt->fib6_nh->fib_nh_gw_family)
2556 			continue;
2557 		if ((rt->fib6_flags & flags) != flags)
2558 			continue;
2559 		if ((rt->fib6_flags & noflags) != 0)
2560 			continue;
2561 		if (!fib6_info_hold_safe(rt))
2562 			continue;
2563 		break;
2564 	}
2565 out:
2566 	rcu_read_unlock();
2567 	return rt;
2568 }
2569 
2570 
2571 /* Create "default" multicast route to the interface */
2572 
2573 static void addrconf_add_mroute(struct net_device *dev)
2574 {
2575 	struct fib6_config cfg = {
2576 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
2577 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
2578 		.fc_ifindex = dev->ifindex,
2579 		.fc_dst_len = 8,
2580 		.fc_flags = RTF_UP,
2581 		.fc_type = RTN_MULTICAST,
2582 		.fc_nlinfo.nl_net = dev_net(dev),
2583 		.fc_protocol = RTPROT_KERNEL,
2584 	};
2585 
2586 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2587 
2588 	ip6_route_add(&cfg, GFP_KERNEL, NULL);
2589 }
2590 
2591 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2592 {
2593 	struct inet6_dev *idev;
2594 
2595 	ASSERT_RTNL();
2596 
2597 	idev = ipv6_find_idev(dev);
2598 	if (IS_ERR(idev))
2599 		return idev;
2600 
2601 	if (idev->cnf.disable_ipv6)
2602 		return ERR_PTR(-EACCES);
2603 
2604 	/* Add default multicast route */
2605 	if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
2606 		addrconf_add_mroute(dev);
2607 
2608 	return idev;
2609 }
2610 
2611 static void delete_tempaddrs(struct inet6_dev *idev,
2612 			     struct inet6_ifaddr *ifp)
2613 {
2614 	struct inet6_ifaddr *ift, *tmp;
2615 
2616 	write_lock_bh(&idev->lock);
2617 	list_for_each_entry_safe(ift, tmp, &idev->tempaddr_list, tmp_list) {
2618 		if (ift->ifpub != ifp)
2619 			continue;
2620 
2621 		in6_ifa_hold(ift);
2622 		write_unlock_bh(&idev->lock);
2623 		ipv6_del_addr(ift);
2624 		write_lock_bh(&idev->lock);
2625 	}
2626 	write_unlock_bh(&idev->lock);
2627 }
2628 
2629 static void manage_tempaddrs(struct inet6_dev *idev,
2630 			     struct inet6_ifaddr *ifp,
2631 			     __u32 valid_lft, __u32 prefered_lft,
2632 			     bool create, unsigned long now)
2633 {
2634 	u32 orig_prefered_lft = prefered_lft;
2635 	struct inet6_ifaddr *ift;
2636 	bool reset_done = false;
2637 	u32 flags;
2638 
2639 	read_lock_bh(&idev->lock);
2640 	/* update all temporary addresses in the list */
2641 	list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2642 		int age, max_valid, max_prefered;
2643 
2644 		if (ifp != ift->ifpub)
2645 			continue;
2646 
2647 		/* RFC 4941 section 3.3:
2648 		 * If a received option will extend the lifetime of a public
2649 		 * address, the lifetimes of temporary addresses should
2650 		 * be extended, subject to the overall constraint that no
2651 		 * temporary addresses should ever remain "valid" or "preferred"
2652 		 * for a time longer than (TEMP_VALID_LIFETIME) or
2653 		 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2654 		 */
2655 		age = (now - ift->cstamp) / HZ;
2656 		max_valid = READ_ONCE(idev->cnf.temp_valid_lft) - age;
2657 		if (max_valid < 0)
2658 			max_valid = 0;
2659 
2660 		max_prefered = READ_ONCE(idev->cnf.temp_prefered_lft) -
2661 			       idev->desync_factor - age;
2662 		if (max_prefered < 0)
2663 			max_prefered = 0;
2664 
2665 		if (valid_lft > max_valid)
2666 			valid_lft = max_valid;
2667 
2668 		if (prefered_lft > max_prefered)
2669 			prefered_lft = max_prefered;
2670 
2671 		spin_lock(&ift->lock);
2672 		/* the first match is the most recent temp address */
2673 		if (!reset_done && orig_prefered_lft > 0) {
2674 			ift->regen_count = 0;
2675 			reset_done = true;
2676 		}
2677 		flags = ift->flags;
2678 		ift->valid_lft = valid_lft;
2679 		ift->prefered_lft = prefered_lft;
2680 		ift->tstamp = now;
2681 		if (prefered_lft > 0)
2682 			ift->flags &= ~IFA_F_DEPRECATED;
2683 
2684 		spin_unlock(&ift->lock);
2685 		if (!(flags&IFA_F_TENTATIVE))
2686 			ipv6_ifa_notify(0, ift);
2687 	}
2688 
2689 	/* Also create a temporary address if it's enabled but no temporary
2690 	 * address currently exists.
2691 	 * However, we get called with valid_lft == 0, prefered_lft == 0, create == false
2692 	 * as part of cleanup (ie. deleting the mngtmpaddr).
2693 	 * We don't want that to result in creating a new temporary ip address.
2694 	 */
2695 	if (list_empty(&idev->tempaddr_list) && (valid_lft || prefered_lft))
2696 		create = true;
2697 
2698 	if (create && READ_ONCE(idev->cnf.use_tempaddr) > 0) {
2699 		/* When a new public address is created as described
2700 		 * in [ADDRCONF], also create a new temporary address.
2701 		 */
2702 		read_unlock_bh(&idev->lock);
2703 		ipv6_create_tempaddr(ifp, false);
2704 	} else {
2705 		read_unlock_bh(&idev->lock);
2706 	}
2707 }
2708 
2709 static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2710 {
2711 	return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2712 	       idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2713 }
2714 
2715 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2716 				 const struct prefix_info *pinfo,
2717 				 struct inet6_dev *in6_dev,
2718 				 const struct in6_addr *addr, int addr_type,
2719 				 u32 addr_flags, bool sllao, bool tokenized,
2720 				 __u32 valid_lft, u32 prefered_lft)
2721 {
2722 	struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2723 	int create = 0, update_lft = 0;
2724 
2725 	if (!ifp && valid_lft) {
2726 		int max_addresses = READ_ONCE(in6_dev->cnf.max_addresses);
2727 		struct ifa6_config cfg = {
2728 			.pfx = addr,
2729 			.plen = pinfo->prefix_len,
2730 			.ifa_flags = addr_flags,
2731 			.valid_lft = valid_lft,
2732 			.preferred_lft = prefered_lft,
2733 			.scope = addr_type & IPV6_ADDR_SCOPE_MASK,
2734 			.ifa_proto = IFAPROT_KERNEL_RA
2735 		};
2736 
2737 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2738 		if ((READ_ONCE(net->ipv6.devconf_all->optimistic_dad) ||
2739 		     READ_ONCE(in6_dev->cnf.optimistic_dad)) &&
2740 		    !net->ipv6.devconf_all->forwarding && sllao)
2741 			cfg.ifa_flags |= IFA_F_OPTIMISTIC;
2742 #endif
2743 
2744 		/* Do not allow to create too much of autoconfigured
2745 		 * addresses; this would be too easy way to crash kernel.
2746 		 */
2747 		if (!max_addresses ||
2748 		    ipv6_count_addresses(in6_dev) < max_addresses)
2749 			ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
2750 
2751 		if (IS_ERR_OR_NULL(ifp))
2752 			return -1;
2753 
2754 		create = 1;
2755 		spin_lock_bh(&ifp->lock);
2756 		ifp->flags |= IFA_F_MANAGETEMPADDR;
2757 		ifp->cstamp = jiffies;
2758 		ifp->tokenized = tokenized;
2759 		spin_unlock_bh(&ifp->lock);
2760 		addrconf_dad_start(ifp);
2761 	}
2762 
2763 	if (ifp) {
2764 		u32 flags;
2765 		unsigned long now;
2766 		u32 stored_lft;
2767 
2768 		/* update lifetime (RFC2462 5.5.3 e) */
2769 		spin_lock_bh(&ifp->lock);
2770 		now = jiffies;
2771 		if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2772 			stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2773 		else
2774 			stored_lft = 0;
2775 
2776 		/* RFC4862 Section 5.5.3e:
2777 		 * "Note that the preferred lifetime of the
2778 		 *  corresponding address is always reset to
2779 		 *  the Preferred Lifetime in the received
2780 		 *  Prefix Information option, regardless of
2781 		 *  whether the valid lifetime is also reset or
2782 		 *  ignored."
2783 		 *
2784 		 * So we should always update prefered_lft here.
2785 		 */
2786 		update_lft = !create && stored_lft;
2787 
2788 		if (update_lft && !READ_ONCE(in6_dev->cnf.ra_honor_pio_life)) {
2789 			const u32 minimum_lft = min_t(u32,
2790 				stored_lft, MIN_VALID_LIFETIME);
2791 			valid_lft = max(valid_lft, minimum_lft);
2792 		}
2793 
2794 		if (update_lft) {
2795 			ifp->valid_lft = valid_lft;
2796 			ifp->prefered_lft = prefered_lft;
2797 			WRITE_ONCE(ifp->tstamp, now);
2798 			flags = ifp->flags;
2799 			ifp->flags &= ~IFA_F_DEPRECATED;
2800 			spin_unlock_bh(&ifp->lock);
2801 
2802 			if (!(flags&IFA_F_TENTATIVE))
2803 				ipv6_ifa_notify(0, ifp);
2804 		} else
2805 			spin_unlock_bh(&ifp->lock);
2806 
2807 		manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2808 				 create, now);
2809 
2810 		in6_ifa_put(ifp);
2811 		addrconf_verify(net);
2812 	}
2813 
2814 	return 0;
2815 }
2816 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
2817 
2818 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2819 {
2820 	struct prefix_info *pinfo;
2821 	struct fib6_table *table;
2822 	__u32 valid_lft;
2823 	__u32 prefered_lft;
2824 	int addr_type, err;
2825 	u32 addr_flags = 0;
2826 	struct inet6_dev *in6_dev;
2827 	struct net *net = dev_net(dev);
2828 	bool ignore_autoconf = false;
2829 
2830 	pinfo = (struct prefix_info *) opt;
2831 
2832 	if (len < sizeof(struct prefix_info)) {
2833 		netdev_dbg(dev, "addrconf: prefix option too short\n");
2834 		return;
2835 	}
2836 
2837 	/*
2838 	 *	Validation checks ([ADDRCONF], page 19)
2839 	 */
2840 
2841 	addr_type = ipv6_addr_type(&pinfo->prefix);
2842 
2843 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2844 		return;
2845 
2846 	valid_lft = ntohl(pinfo->valid);
2847 	prefered_lft = ntohl(pinfo->prefered);
2848 
2849 	if (prefered_lft > valid_lft) {
2850 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2851 		return;
2852 	}
2853 
2854 	in6_dev = in6_dev_get(dev);
2855 
2856 	if (!in6_dev) {
2857 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2858 				    dev->name);
2859 		return;
2860 	}
2861 
2862 	if (valid_lft != 0 && valid_lft < in6_dev->cnf.accept_ra_min_lft)
2863 		goto put;
2864 
2865 	/*
2866 	 *	Two things going on here:
2867 	 *	1) Add routes for on-link prefixes
2868 	 *	2) Configure prefixes with the auto flag set
2869 	 */
2870 
2871 	if (pinfo->onlink) {
2872 		struct fib6_info *rt;
2873 		unsigned long rt_expires;
2874 
2875 		/* Avoid arithmetic overflow. Really, we could
2876 		 * save rt_expires in seconds, likely valid_lft,
2877 		 * but it would require division in fib gc, that it
2878 		 * not good.
2879 		 */
2880 		if (HZ > USER_HZ)
2881 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2882 		else
2883 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2884 
2885 		if (addrconf_finite_timeout(rt_expires))
2886 			rt_expires *= HZ;
2887 
2888 		rt = addrconf_get_prefix_route(&pinfo->prefix,
2889 					       pinfo->prefix_len,
2890 					       dev,
2891 					       RTF_ADDRCONF | RTF_PREFIX_RT,
2892 					       RTF_DEFAULT, true);
2893 
2894 		if (rt) {
2895 			/* Autoconf prefix route */
2896 			if (valid_lft == 0) {
2897 				ip6_del_rt_reason(net, rt,
2898 						  RT_DEL_REASON_RA_WITHDRAWN);
2899 				rt = NULL;
2900 			} else {
2901 				table = rt->fib6_table;
2902 				spin_lock_bh(&table->tb6_lock);
2903 
2904 				if (addrconf_finite_timeout(rt_expires)) {
2905 					/* not infinity */
2906 					fib6_set_expires(rt, jiffies + rt_expires);
2907 					fib6_add_gc_list(rt);
2908 				} else {
2909 					fib6_clean_expires(rt);
2910 					fib6_may_remove_gc_list(net, rt);
2911 				}
2912 
2913 				spin_unlock_bh(&table->tb6_lock);
2914 			}
2915 		} else if (valid_lft) {
2916 			clock_t expires = 0;
2917 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2918 			if (addrconf_finite_timeout(rt_expires)) {
2919 				/* not infinity */
2920 				flags |= RTF_EXPIRES;
2921 				expires = jiffies_to_clock_t(rt_expires);
2922 			}
2923 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2924 					      0, dev, expires, flags,
2925 					      GFP_ATOMIC);
2926 		}
2927 		fib6_info_release(rt);
2928 	}
2929 
2930 	/* Try to figure out our local address for this prefix */
2931 
2932 	ignore_autoconf = READ_ONCE(in6_dev->cnf.ra_honor_pio_pflag) && pinfo->preferpd;
2933 	if (pinfo->autoconf && in6_dev->cnf.autoconf && !ignore_autoconf) {
2934 		struct in6_addr addr;
2935 		bool tokenized = false, dev_addr_generated = false;
2936 
2937 		if (pinfo->prefix_len == 64) {
2938 			memcpy(&addr, &pinfo->prefix, 8);
2939 
2940 			if (!ipv6_addr_any(&in6_dev->token)) {
2941 				read_lock_bh(&in6_dev->lock);
2942 				memcpy(addr.s6_addr + 8,
2943 				       in6_dev->token.s6_addr + 8, 8);
2944 				read_unlock_bh(&in6_dev->lock);
2945 				tokenized = true;
2946 			} else if (is_addr_mode_generate_stable(in6_dev) &&
2947 				   !ipv6_generate_stable_address(&addr, 0,
2948 								 in6_dev)) {
2949 				addr_flags |= IFA_F_STABLE_PRIVACY;
2950 				goto ok;
2951 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2952 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2953 				goto put;
2954 			} else {
2955 				dev_addr_generated = true;
2956 			}
2957 			goto ok;
2958 		}
2959 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2960 				    pinfo->prefix_len);
2961 		goto put;
2962 
2963 ok:
2964 		err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2965 						   &addr, addr_type,
2966 						   addr_flags, sllao,
2967 						   tokenized, valid_lft,
2968 						   prefered_lft);
2969 		if (err)
2970 			goto put;
2971 
2972 		/* Ignore error case here because previous prefix add addr was
2973 		 * successful which will be notified.
2974 		 */
2975 		ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2976 					      addr_type, addr_flags, sllao,
2977 					      tokenized, valid_lft,
2978 					      prefered_lft,
2979 					      dev_addr_generated);
2980 	}
2981 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2982 put:
2983 	in6_dev_put(in6_dev);
2984 }
2985 
2986 static int addrconf_set_sit_dstaddr(struct net *net, struct net_device *dev,
2987 		struct in6_ifreq *ireq)
2988 {
2989 	struct ip_tunnel_parm_kern p = { };
2990 	int err;
2991 
2992 	if (!(ipv6_addr_type(&ireq->ifr6_addr) & IPV6_ADDR_COMPATv4))
2993 		return -EADDRNOTAVAIL;
2994 
2995 	p.iph.daddr = ireq->ifr6_addr.s6_addr32[3];
2996 	p.iph.version = 4;
2997 	p.iph.ihl = 5;
2998 	p.iph.protocol = IPPROTO_IPV6;
2999 	p.iph.ttl = 64;
3000 
3001 	if (!dev->netdev_ops->ndo_tunnel_ctl)
3002 		return -EOPNOTSUPP;
3003 	err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, SIOCADDTUNNEL);
3004 	if (err)
3005 		return err;
3006 
3007 	dev = __dev_get_by_name(net, p.name);
3008 	if (!dev)
3009 		return -ENOBUFS;
3010 	return dev_open(dev, NULL);
3011 }
3012 
3013 /*
3014  *	Set destination address.
3015  *	Special case for SIT interfaces where we create a new "virtual"
3016  *	device.
3017  */
3018 int addrconf_set_dstaddr(struct net *net, void __user *arg)
3019 {
3020 	struct net_device *dev;
3021 	struct in6_ifreq ireq;
3022 	int err = -ENODEV;
3023 
3024 	if (!IS_ENABLED(CONFIG_IPV6_SIT))
3025 		return -ENODEV;
3026 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3027 		return -EFAULT;
3028 
3029 	rtnl_net_lock(net);
3030 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
3031 	if (dev && dev->type == ARPHRD_SIT)
3032 		err = addrconf_set_sit_dstaddr(net, dev, &ireq);
3033 	rtnl_net_unlock(net);
3034 	return err;
3035 }
3036 
3037 static int ipv6_mc_config(struct sock *sk, bool join,
3038 			  const struct in6_addr *addr, int ifindex)
3039 {
3040 	int ret;
3041 
3042 	ASSERT_RTNL();
3043 
3044 	lock_sock(sk);
3045 	if (join)
3046 		ret = ipv6_sock_mc_join(sk, ifindex, addr);
3047 	else
3048 		ret = ipv6_sock_mc_drop(sk, ifindex, addr);
3049 	release_sock(sk);
3050 
3051 	return ret;
3052 }
3053 
3054 /*
3055  *	Manual configuration of address on an interface
3056  */
3057 static int inet6_addr_add(struct net *net, struct net_device *dev,
3058 			  struct ifa6_config *cfg, clock_t expires, u32 flags,
3059 			  struct netlink_ext_ack *extack)
3060 {
3061 	struct inet6_ifaddr *ifp;
3062 	struct inet6_dev *idev;
3063 
3064 	ASSERT_RTNL_NET(net);
3065 
3066 	if (cfg->plen > 128) {
3067 		NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length");
3068 		return -EINVAL;
3069 	}
3070 
3071 	if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) {
3072 		NL_SET_ERR_MSG_MOD(extack, "address with \"mngtmpaddr\" flag must have a prefix length of 64");
3073 		return -EINVAL;
3074 	}
3075 
3076 	idev = addrconf_add_dev(dev);
3077 	if (IS_ERR(idev)) {
3078 		NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
3079 		return PTR_ERR(idev);
3080 	}
3081 
3082 	if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
3083 		int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
3084 					 true, cfg->pfx, dev->ifindex);
3085 
3086 		if (ret < 0) {
3087 			NL_SET_ERR_MSG_MOD(extack, "Multicast auto join failed");
3088 			return ret;
3089 		}
3090 	}
3091 
3092 	cfg->scope = ipv6_addr_scope(cfg->pfx);
3093 
3094 	ifp = ipv6_add_addr(idev, cfg, true, extack);
3095 	if (!IS_ERR(ifp)) {
3096 		if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
3097 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3098 					      ifp->rt_priority, dev, expires,
3099 					      flags, GFP_KERNEL);
3100 		}
3101 
3102 		/* Send a netlink notification if DAD is enabled and
3103 		 * optimistic flag is not set
3104 		 */
3105 		if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
3106 			ipv6_ifa_notify(0, ifp);
3107 		/*
3108 		 * Note that section 3.1 of RFC 4429 indicates
3109 		 * that the Optimistic flag should not be set for
3110 		 * manually configured addresses
3111 		 */
3112 		addrconf_dad_start(ifp);
3113 		if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
3114 			manage_tempaddrs(idev, ifp, cfg->valid_lft,
3115 					 cfg->preferred_lft, true, jiffies);
3116 		in6_ifa_put(ifp);
3117 		addrconf_verify_rtnl(net);
3118 		return 0;
3119 	} else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
3120 		ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
3121 			       cfg->pfx, dev->ifindex);
3122 	}
3123 
3124 	return PTR_ERR(ifp);
3125 }
3126 
3127 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
3128 			  const struct in6_addr *pfx, unsigned int plen,
3129 			  struct netlink_ext_ack *extack)
3130 {
3131 	struct inet6_ifaddr *ifp;
3132 	struct inet6_dev *idev;
3133 	struct net_device *dev;
3134 
3135 	if (plen > 128) {
3136 		NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length");
3137 		return -EINVAL;
3138 	}
3139 
3140 	dev = __dev_get_by_index(net, ifindex);
3141 	if (!dev) {
3142 		NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface");
3143 		return -ENODEV;
3144 	}
3145 
3146 	idev = __in6_dev_get_rtnl_net(dev);
3147 	if (!idev) {
3148 		NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
3149 		return -ENXIO;
3150 	}
3151 
3152 	read_lock_bh(&idev->lock);
3153 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
3154 		if (ifp->prefix_len == plen &&
3155 		    ipv6_addr_equal(pfx, &ifp->addr)) {
3156 			in6_ifa_hold(ifp);
3157 			read_unlock_bh(&idev->lock);
3158 
3159 			if (!(ifp->flags & IFA_F_TEMPORARY) &&
3160 			    (ifp->flags & IFA_F_MANAGETEMPADDR))
3161 				delete_tempaddrs(idev, ifp);
3162 
3163 			ipv6_del_addr(ifp);
3164 
3165 			addrconf_verify_rtnl(net);
3166 			if (ipv6_addr_is_multicast(pfx)) {
3167 				ipv6_mc_config(net->ipv6.mc_autojoin_sk,
3168 					       false, pfx, dev->ifindex);
3169 			}
3170 			return 0;
3171 		}
3172 	}
3173 	read_unlock_bh(&idev->lock);
3174 
3175 	NL_SET_ERR_MSG_MOD(extack, "address not found");
3176 	return -EADDRNOTAVAIL;
3177 }
3178 
3179 
3180 int addrconf_add_ifaddr(struct net *net, void __user *arg)
3181 {
3182 	struct ifa6_config cfg = {
3183 		.ifa_flags = IFA_F_PERMANENT,
3184 		.preferred_lft = INFINITY_LIFE_TIME,
3185 		.valid_lft = INFINITY_LIFE_TIME,
3186 	};
3187 	struct net_device *dev;
3188 	struct in6_ifreq ireq;
3189 	int err;
3190 
3191 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3192 		return -EPERM;
3193 
3194 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3195 		return -EFAULT;
3196 
3197 	cfg.pfx = &ireq.ifr6_addr;
3198 	cfg.plen = ireq.ifr6_prefixlen;
3199 
3200 	rtnl_net_lock(net);
3201 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
3202 	if (dev) {
3203 		netdev_lock_ops(dev);
3204 		err = inet6_addr_add(net, dev, &cfg, 0, 0, NULL);
3205 		netdev_unlock_ops(dev);
3206 	} else {
3207 		err = -ENODEV;
3208 	}
3209 	rtnl_net_unlock(net);
3210 	return err;
3211 }
3212 
3213 int addrconf_del_ifaddr(struct net *net, void __user *arg)
3214 {
3215 	struct in6_ifreq ireq;
3216 	int err;
3217 
3218 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3219 		return -EPERM;
3220 
3221 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3222 		return -EFAULT;
3223 
3224 	rtnl_net_lock(net);
3225 	err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
3226 			     ireq.ifr6_prefixlen, NULL);
3227 	rtnl_net_unlock(net);
3228 	return err;
3229 }
3230 
3231 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
3232 		     int plen, int scope, u8 proto)
3233 {
3234 	struct inet6_ifaddr *ifp;
3235 	struct ifa6_config cfg = {
3236 		.pfx = addr,
3237 		.plen = plen,
3238 		.ifa_flags = IFA_F_PERMANENT,
3239 		.valid_lft = INFINITY_LIFE_TIME,
3240 		.preferred_lft = INFINITY_LIFE_TIME,
3241 		.scope = scope,
3242 		.ifa_proto = proto
3243 	};
3244 
3245 	ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3246 	if (!IS_ERR(ifp)) {
3247 		spin_lock_bh(&ifp->lock);
3248 		ifp->flags &= ~IFA_F_TENTATIVE;
3249 		spin_unlock_bh(&ifp->lock);
3250 		rt_genid_bump_ipv6(dev_net(idev->dev));
3251 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
3252 		in6_ifa_put(ifp);
3253 	}
3254 }
3255 
3256 #if IS_ENABLED(CONFIG_IPV6_SIT) || IS_ENABLED(CONFIG_NET_IPGRE)
3257 static void add_v4_addrs(struct inet6_dev *idev)
3258 {
3259 	struct in6_addr addr;
3260 	struct net_device *dev;
3261 	struct net *net = dev_net(idev->dev);
3262 	int scope, plen;
3263 	u32 pflags = 0;
3264 
3265 	ASSERT_RTNL();
3266 
3267 	memset(&addr, 0, sizeof(struct in6_addr));
3268 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
3269 
3270 	if (!(idev->dev->flags & IFF_POINTOPOINT) && idev->dev->type == ARPHRD_SIT) {
3271 		scope = IPV6_ADDR_COMPATv4;
3272 		plen = 96;
3273 		pflags |= RTF_NONEXTHOP;
3274 	} else {
3275 		if (idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_NONE)
3276 			return;
3277 
3278 		addr.s6_addr32[0] = htonl(0xfe800000);
3279 		scope = IFA_LINK;
3280 		plen = 64;
3281 	}
3282 
3283 	if (addr.s6_addr32[3]) {
3284 		add_addr(idev, &addr, plen, scope, IFAPROT_UNSPEC);
3285 		addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
3286 				      GFP_KERNEL);
3287 		return;
3288 	}
3289 
3290 	for_each_netdev(net, dev) {
3291 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
3292 		if (in_dev && (dev->flags & IFF_UP)) {
3293 			struct in_ifaddr *ifa;
3294 			int flag = scope;
3295 
3296 			in_dev_for_each_ifa_rtnl(ifa, in_dev) {
3297 				addr.s6_addr32[3] = ifa->ifa_local;
3298 
3299 				if (ifa->ifa_scope == RT_SCOPE_LINK)
3300 					continue;
3301 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
3302 					if (idev->dev->flags&IFF_POINTOPOINT)
3303 						continue;
3304 					flag |= IFA_HOST;
3305 				}
3306 
3307 				add_addr(idev, &addr, plen, flag,
3308 					 IFAPROT_UNSPEC);
3309 				addrconf_prefix_route(&addr, plen, 0, idev->dev,
3310 						      0, pflags, GFP_KERNEL);
3311 			}
3312 		}
3313 	}
3314 }
3315 #endif
3316 
3317 static void init_loopback(struct net_device *dev)
3318 {
3319 	struct inet6_dev  *idev;
3320 
3321 	/* ::1 */
3322 
3323 	ASSERT_RTNL();
3324 
3325 	idev = ipv6_find_idev(dev);
3326 	if (IS_ERR(idev)) {
3327 		pr_debug("%s: add_dev failed\n", __func__);
3328 		return;
3329 	}
3330 
3331 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFAPROT_KERNEL_LO);
3332 }
3333 
3334 void addrconf_add_linklocal(struct inet6_dev *idev,
3335 			    const struct in6_addr *addr, u32 flags)
3336 {
3337 	struct ifa6_config cfg = {
3338 		.pfx = addr,
3339 		.plen = 64,
3340 		.ifa_flags = flags | IFA_F_PERMANENT,
3341 		.valid_lft = INFINITY_LIFE_TIME,
3342 		.preferred_lft = INFINITY_LIFE_TIME,
3343 		.scope = IFA_LINK,
3344 		.ifa_proto = IFAPROT_KERNEL_LL
3345 	};
3346 	struct inet6_ifaddr *ifp;
3347 
3348 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3349 	if ((READ_ONCE(dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad) ||
3350 	     READ_ONCE(idev->cnf.optimistic_dad)) &&
3351 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3352 		cfg.ifa_flags |= IFA_F_OPTIMISTIC;
3353 #endif
3354 
3355 	ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3356 	if (!IS_ERR(ifp)) {
3357 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
3358 				      0, 0, GFP_ATOMIC);
3359 		addrconf_dad_start(ifp);
3360 		in6_ifa_put(ifp);
3361 	}
3362 }
3363 EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
3364 
3365 static bool ipv6_reserved_interfaceid(struct in6_addr address)
3366 {
3367 	if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3368 		return true;
3369 
3370 	if (address.s6_addr32[2] == htonl(0x02005eff) &&
3371 	    ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3372 		return true;
3373 
3374 	if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3375 	    ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3376 		return true;
3377 
3378 	return false;
3379 }
3380 
3381 static int ipv6_generate_stable_address(struct in6_addr *address,
3382 					u8 dad_count,
3383 					const struct inet6_dev *idev)
3384 {
3385 	static DEFINE_SPINLOCK(lock);
3386 	static struct sha1_ctx sha_ctx;
3387 
3388 	static union {
3389 		u8 __data[SHA1_BLOCK_SIZE];
3390 		struct {
3391 			struct in6_addr secret;
3392 			__be32 prefix[2];
3393 			unsigned char hwaddr[MAX_ADDR_LEN];
3394 			u8 dad_count;
3395 		} __packed;
3396 	} data;
3397 
3398 	struct in6_addr secret;
3399 	struct in6_addr temp;
3400 	struct net *net = dev_net(idev->dev);
3401 
3402 	BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3403 
3404 	if (idev->cnf.stable_secret.initialized)
3405 		secret = idev->cnf.stable_secret.secret;
3406 	else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3407 		secret = net->ipv6.devconf_dflt->stable_secret.secret;
3408 	else
3409 		return -1;
3410 
3411 retry:
3412 	spin_lock_bh(&lock);
3413 
3414 	sha1_init(&sha_ctx);
3415 
3416 	memset(&data, 0, sizeof(data));
3417 	memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3418 	data.prefix[0] = address->s6_addr32[0];
3419 	data.prefix[1] = address->s6_addr32[1];
3420 	data.secret = secret;
3421 	data.dad_count = dad_count;
3422 
3423 	sha1_update(&sha_ctx, data.__data, sizeof(data));
3424 
3425 	/*
3426 	 * Note that the SHA-1 finalization is omitted here, and the digest is
3427 	 * pulled directly from the internal SHA-1 state (making it incompatible
3428 	 * with standard SHA-1).  Unusual, but technically okay since the data
3429 	 * length is fixed and is a multiple of the SHA-1 block size.
3430 	 */
3431 	temp = *address;
3432 	temp.s6_addr32[2] = (__force __be32)sha_ctx.state.h[0];
3433 	temp.s6_addr32[3] = (__force __be32)sha_ctx.state.h[1];
3434 
3435 	spin_unlock_bh(&lock);
3436 
3437 	if (ipv6_reserved_interfaceid(temp)) {
3438 		dad_count++;
3439 		if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3440 			return -1;
3441 		goto retry;
3442 	}
3443 
3444 	*address = temp;
3445 	return 0;
3446 }
3447 
3448 static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3449 {
3450 	struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3451 
3452 	if (s->initialized)
3453 		return;
3454 	s = &idev->cnf.stable_secret;
3455 	get_random_bytes(&s->secret, sizeof(s->secret));
3456 	s->initialized = true;
3457 }
3458 
3459 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3460 {
3461 	struct in6_addr addr;
3462 
3463 	/* no link local addresses on L3 master devices */
3464 	if (netif_is_l3_master(idev->dev))
3465 		return;
3466 
3467 	/* no link local addresses on devices flagged as slaves */
3468 	if (idev->dev->priv_flags & IFF_NO_ADDRCONF)
3469 		return;
3470 
3471 	ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3472 
3473 	switch (idev->cnf.addr_gen_mode) {
3474 	case IN6_ADDR_GEN_MODE_RANDOM:
3475 		ipv6_gen_mode_random_init(idev);
3476 		fallthrough;
3477 	case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3478 		if (!ipv6_generate_stable_address(&addr, 0, idev))
3479 			addrconf_add_linklocal(idev, &addr,
3480 					       IFA_F_STABLE_PRIVACY);
3481 		else if (prefix_route)
3482 			addrconf_prefix_route(&addr, 64, 0, idev->dev,
3483 					      0, 0, GFP_KERNEL);
3484 		break;
3485 	case IN6_ADDR_GEN_MODE_EUI64:
3486 		/* addrconf_add_linklocal also adds a prefix_route and we
3487 		 * only need to care about prefix routes if ipv6_generate_eui64
3488 		 * couldn't generate one.
3489 		 */
3490 		if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3491 			addrconf_add_linklocal(idev, &addr, 0);
3492 		else if (prefix_route)
3493 			addrconf_prefix_route(&addr, 64, 0, idev->dev,
3494 					      0, 0, GFP_KERNEL);
3495 		break;
3496 	case IN6_ADDR_GEN_MODE_NONE:
3497 	default:
3498 		/* will not add any link local address */
3499 		break;
3500 	}
3501 }
3502 
3503 static void addrconf_dev_config(struct net_device *dev)
3504 {
3505 	struct inet6_dev *idev;
3506 
3507 	ASSERT_RTNL();
3508 
3509 	if ((dev->type != ARPHRD_ETHER) &&
3510 	    (dev->type != ARPHRD_FDDI) &&
3511 	    (dev->type != ARPHRD_ARCNET) &&
3512 	    (dev->type != ARPHRD_INFINIBAND) &&
3513 	    (dev->type != ARPHRD_IEEE1394) &&
3514 	    (dev->type != ARPHRD_TUNNEL6) &&
3515 	    (dev->type != ARPHRD_6LOWPAN) &&
3516 	    (dev->type != ARPHRD_IP6GRE) &&
3517 	    (dev->type != ARPHRD_TUNNEL) &&
3518 	    (dev->type != ARPHRD_NONE) &&
3519 	    (dev->type != ARPHRD_RAWIP)) {
3520 		/* Alas, we support only Ethernet autoconfiguration. */
3521 		idev = __in6_dev_get(dev);
3522 		if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP &&
3523 		    dev->flags & IFF_MULTICAST)
3524 			ipv6_mc_up(idev);
3525 		return;
3526 	}
3527 
3528 	idev = addrconf_add_dev(dev);
3529 	if (IS_ERR(idev))
3530 		return;
3531 
3532 	/* this device type has no EUI support */
3533 	if (dev->type == ARPHRD_NONE &&
3534 	    idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3535 		WRITE_ONCE(idev->cnf.addr_gen_mode,
3536 			   IN6_ADDR_GEN_MODE_RANDOM);
3537 
3538 	addrconf_addr_gen(idev, false);
3539 }
3540 
3541 #if IS_ENABLED(CONFIG_IPV6_SIT)
3542 static void addrconf_sit_config(struct net_device *dev)
3543 {
3544 	struct inet6_dev *idev;
3545 
3546 	ASSERT_RTNL();
3547 
3548 	/*
3549 	 * Configure the tunnel with one of our IPv4
3550 	 * addresses... we should configure all of
3551 	 * our v4 addrs in the tunnel
3552 	 */
3553 
3554 	idev = ipv6_find_idev(dev);
3555 	if (IS_ERR(idev)) {
3556 		pr_debug("%s: add_dev failed\n", __func__);
3557 		return;
3558 	}
3559 
3560 	if (dev->priv_flags & IFF_ISATAP) {
3561 		addrconf_addr_gen(idev, false);
3562 		return;
3563 	}
3564 
3565 	add_v4_addrs(idev);
3566 
3567 	if (dev->flags&IFF_POINTOPOINT)
3568 		addrconf_add_mroute(dev);
3569 }
3570 #endif
3571 
3572 #if IS_ENABLED(CONFIG_NET_IPGRE)
3573 static void addrconf_gre_config(struct net_device *dev)
3574 {
3575 	struct inet6_dev *idev;
3576 
3577 	ASSERT_RTNL();
3578 
3579 	idev = addrconf_add_dev(dev);
3580 	if (IS_ERR(idev))
3581 		return;
3582 
3583 	/* Generate the IPv6 link-local address using addrconf_addr_gen(),
3584 	 * unless we have an IPv4 GRE device not bound to an IP address and
3585 	 * which is in EUI64 mode (as __ipv6_isatap_ifid() would fail in this
3586 	 * case). Such devices fall back to add_v4_addrs() instead.
3587 	 */
3588 	if (!(*(__be32 *)dev->dev_addr == 0 &&
3589 	      idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)) {
3590 		addrconf_addr_gen(idev, true);
3591 		return;
3592 	}
3593 
3594 	add_v4_addrs(idev);
3595 }
3596 #endif
3597 
3598 static void addrconf_init_auto_addrs(struct net_device *dev)
3599 {
3600 	switch (dev->type) {
3601 #if IS_ENABLED(CONFIG_IPV6_SIT)
3602 	case ARPHRD_SIT:
3603 		addrconf_sit_config(dev);
3604 		break;
3605 #endif
3606 #if IS_ENABLED(CONFIG_NET_IPGRE)
3607 	case ARPHRD_IPGRE:
3608 		addrconf_gre_config(dev);
3609 		break;
3610 #endif
3611 	case ARPHRD_LOOPBACK:
3612 		init_loopback(dev);
3613 		break;
3614 
3615 	default:
3616 		addrconf_dev_config(dev);
3617 		break;
3618 	}
3619 }
3620 
3621 static int fixup_permanent_addr(struct net *net,
3622 				struct inet6_dev *idev,
3623 				struct inet6_ifaddr *ifp)
3624 {
3625 	/* !fib6_node means the host route was removed from the
3626 	 * FIB, for example, if 'lo' device is taken down. In that
3627 	 * case regenerate the host route.
3628 	 */
3629 	if (!ifp->rt || !ifp->rt->fib6_node) {
3630 		struct fib6_info *f6i, *prev;
3631 
3632 		f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3633 					 GFP_KERNEL, NULL);
3634 		if (IS_ERR(f6i))
3635 			return PTR_ERR(f6i);
3636 
3637 		/* ifp->rt can be accessed outside of rtnl */
3638 		spin_lock_bh(&ifp->lock);
3639 		prev = ifp->rt;
3640 		ifp->rt = f6i;
3641 		spin_unlock_bh(&ifp->lock);
3642 
3643 		fib6_info_release(prev);
3644 	}
3645 
3646 	if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3647 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3648 				      ifp->rt_priority, idev->dev, 0, 0,
3649 				      GFP_KERNEL);
3650 	}
3651 
3652 	if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3653 		addrconf_dad_start(ifp);
3654 
3655 	return 0;
3656 }
3657 
3658 static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
3659 {
3660 	struct inet6_ifaddr *ifp;
3661 	LIST_HEAD(tmp_addr_list);
3662 	struct inet6_dev *idev;
3663 
3664 	/* Mutual exclusion with other if_list_aux users. */
3665 	ASSERT_RTNL();
3666 
3667 	idev = __in6_dev_get(dev);
3668 	if (!idev)
3669 		return;
3670 
3671 	write_lock_bh(&idev->lock);
3672 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
3673 		if (ifp->flags & IFA_F_PERMANENT)
3674 			list_add_tail(&ifp->if_list_aux, &tmp_addr_list);
3675 	}
3676 	write_unlock_bh(&idev->lock);
3677 
3678 	while (!list_empty(&tmp_addr_list)) {
3679 		ifp = list_first_entry(&tmp_addr_list,
3680 				       struct inet6_ifaddr, if_list_aux);
3681 		list_del(&ifp->if_list_aux);
3682 
3683 		if (fixup_permanent_addr(net, idev, ifp) < 0) {
3684 			net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3685 					     idev->dev->name, &ifp->addr);
3686 			in6_ifa_hold(ifp);
3687 			ipv6_del_addr(ifp);
3688 		}
3689 	}
3690 }
3691 
3692 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3693 			   void *ptr)
3694 {
3695 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3696 	struct netdev_notifier_change_info *change_info;
3697 	struct netdev_notifier_changeupper_info *info;
3698 	struct inet6_dev *idev = __in6_dev_get(dev);
3699 	struct net *net = dev_net(dev);
3700 	int run_pending = 0;
3701 	int err;
3702 
3703 	switch (event) {
3704 	case NETDEV_REGISTER:
3705 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3706 			idev = ipv6_add_dev(dev);
3707 			if (IS_ERR(idev))
3708 				return notifier_from_errno(PTR_ERR(idev));
3709 		}
3710 		break;
3711 
3712 	case NETDEV_CHANGEMTU:
3713 		/* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3714 		if (dev->mtu < IPV6_MIN_MTU) {
3715 			addrconf_ifdown(dev, dev != net->loopback_dev);
3716 			break;
3717 		}
3718 
3719 		if (idev) {
3720 			rt6_mtu_change(dev, dev->mtu);
3721 			WRITE_ONCE(idev->cnf.mtu6, dev->mtu);
3722 			break;
3723 		}
3724 
3725 		/* allocate new idev */
3726 		idev = ipv6_add_dev(dev);
3727 		if (IS_ERR(idev))
3728 			break;
3729 
3730 		/* device is still not ready */
3731 		if (!(idev->if_flags & IF_READY))
3732 			break;
3733 
3734 		run_pending = 1;
3735 		fallthrough;
3736 	case NETDEV_UP:
3737 	case NETDEV_CHANGE:
3738 		if (idev && idev->cnf.disable_ipv6)
3739 			break;
3740 
3741 		if (dev->priv_flags & IFF_NO_ADDRCONF) {
3742 			if (event == NETDEV_UP && !IS_ERR_OR_NULL(idev) &&
3743 			    dev->flags & IFF_UP && dev->flags & IFF_MULTICAST)
3744 				ipv6_mc_up(idev);
3745 			break;
3746 		}
3747 
3748 		if (event == NETDEV_UP) {
3749 			/* restore routes for permanent addresses */
3750 			addrconf_permanent_addr(net, dev);
3751 
3752 			if (!addrconf_link_ready(dev)) {
3753 				/* device is not ready yet. */
3754 				pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3755 					 dev->name);
3756 				break;
3757 			}
3758 
3759 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
3760 				idev = ipv6_add_dev(dev);
3761 
3762 			if (!IS_ERR_OR_NULL(idev)) {
3763 				idev->if_flags |= IF_READY;
3764 				run_pending = 1;
3765 			}
3766 		} else if (event == NETDEV_CHANGE) {
3767 			if (!addrconf_link_ready(dev)) {
3768 				/* device is still not ready. */
3769 				rt6_sync_down_dev(dev, event);
3770 				break;
3771 			}
3772 
3773 			if (!IS_ERR_OR_NULL(idev)) {
3774 				if (idev->if_flags & IF_READY) {
3775 					/* device is already configured -
3776 					 * but resend MLD reports, we might
3777 					 * have roamed and need to update
3778 					 * multicast snooping switches
3779 					 */
3780 					ipv6_mc_up(idev);
3781 					change_info = ptr;
3782 					if (change_info->flags_changed & IFF_NOARP)
3783 						addrconf_dad_run(idev, true);
3784 					rt6_sync_up(dev, RTNH_F_LINKDOWN);
3785 					break;
3786 				}
3787 				idev->if_flags |= IF_READY;
3788 			}
3789 
3790 			pr_debug("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3791 				 dev->name);
3792 
3793 			run_pending = 1;
3794 		}
3795 
3796 		addrconf_init_auto_addrs(dev);
3797 
3798 		if (!IS_ERR_OR_NULL(idev)) {
3799 			if (run_pending)
3800 				addrconf_dad_run(idev, false);
3801 
3802 			/* Device has an address by now */
3803 			rt6_sync_up(dev, RTNH_F_DEAD);
3804 
3805 			/*
3806 			 * If the MTU changed during the interface down,
3807 			 * when the interface up, the changed MTU must be
3808 			 * reflected in the idev as well as routers.
3809 			 */
3810 			if (idev->cnf.mtu6 != dev->mtu &&
3811 			    dev->mtu >= IPV6_MIN_MTU) {
3812 				rt6_mtu_change(dev, dev->mtu);
3813 				WRITE_ONCE(idev->cnf.mtu6, dev->mtu);
3814 			}
3815 			WRITE_ONCE(idev->tstamp, jiffies);
3816 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
3817 
3818 			/*
3819 			 * If the changed mtu during down is lower than
3820 			 * IPV6_MIN_MTU stop IPv6 on this interface.
3821 			 */
3822 			if (dev->mtu < IPV6_MIN_MTU)
3823 				addrconf_ifdown(dev, dev != net->loopback_dev);
3824 		}
3825 		break;
3826 
3827 	case NETDEV_DOWN:
3828 	case NETDEV_UNREGISTER:
3829 		/*
3830 		 *	Remove all addresses from this interface.
3831 		 */
3832 		addrconf_ifdown(dev, event != NETDEV_DOWN);
3833 		break;
3834 
3835 	case NETDEV_CHANGENAME:
3836 		if (idev) {
3837 			snmp6_unregister_dev(idev);
3838 			addrconf_sysctl_unregister(idev);
3839 			err = addrconf_sysctl_register(idev);
3840 			if (err)
3841 				return notifier_from_errno(err);
3842 			err = snmp6_register_dev(idev);
3843 			if (err) {
3844 				addrconf_sysctl_unregister(idev);
3845 				return notifier_from_errno(err);
3846 			}
3847 		}
3848 		break;
3849 
3850 	case NETDEV_PRE_TYPE_CHANGE:
3851 	case NETDEV_POST_TYPE_CHANGE:
3852 		if (idev)
3853 			addrconf_type_change(dev, event);
3854 		break;
3855 
3856 	case NETDEV_CHANGEUPPER:
3857 		info = ptr;
3858 
3859 		/* flush all routes if dev is linked to or unlinked from
3860 		 * an L3 master device (e.g., VRF)
3861 		 */
3862 		if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3863 			addrconf_ifdown(dev, false);
3864 	}
3865 
3866 	return NOTIFY_OK;
3867 }
3868 
3869 /*
3870  *	addrconf module should be notified of a device going up
3871  */
3872 static struct notifier_block ipv6_dev_notf = {
3873 	.notifier_call = addrconf_notify,
3874 	.priority = ADDRCONF_NOTIFY_PRIORITY,
3875 };
3876 
3877 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3878 {
3879 	struct inet6_dev *idev;
3880 	ASSERT_RTNL();
3881 
3882 	idev = __in6_dev_get(dev);
3883 
3884 	if (event == NETDEV_POST_TYPE_CHANGE)
3885 		ipv6_mc_remap(idev);
3886 	else if (event == NETDEV_PRE_TYPE_CHANGE)
3887 		ipv6_mc_unmap(idev);
3888 }
3889 
3890 static bool addr_is_local(const struct in6_addr *addr)
3891 {
3892 	return ipv6_addr_type(addr) &
3893 		(IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3894 }
3895 
3896 static int addrconf_ifdown(struct net_device *dev, bool unregister)
3897 {
3898 	unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN;
3899 	struct net *net = dev_net(dev);
3900 	struct inet6_dev *idev;
3901 	struct inet6_ifaddr *ifa;
3902 	LIST_HEAD(tmp_addr_list);
3903 	bool keep_addr = false;
3904 	bool was_ready;
3905 	int state, i;
3906 
3907 	ASSERT_RTNL();
3908 
3909 	rt6_disable_ip(dev, event);
3910 
3911 	idev = __in6_dev_get(dev);
3912 	if (!idev)
3913 		return -ENODEV;
3914 
3915 	/*
3916 	 * Step 1: remove reference to ipv6 device from parent device.
3917 	 *	   Do not dev_put!
3918 	 */
3919 	if (unregister) {
3920 		write_lock_bh(&idev->lock);
3921 		WRITE_ONCE(idev->dead, 1);
3922 		write_unlock_bh(&idev->lock);
3923 
3924 		/* protected by rtnl_lock */
3925 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3926 
3927 		/* Step 1.5: remove snmp6 entry */
3928 		snmp6_unregister_dev(idev);
3929 
3930 	}
3931 
3932 	/* combine the user config with event to determine if permanent
3933 	 * addresses are to be removed from address hash table
3934 	 */
3935 	if (!unregister && !idev->cnf.disable_ipv6) {
3936 		/* aggregate the system setting and interface setting */
3937 		int _keep_addr = READ_ONCE(net->ipv6.devconf_all->keep_addr_on_down);
3938 
3939 		if (!_keep_addr)
3940 			_keep_addr = READ_ONCE(idev->cnf.keep_addr_on_down);
3941 
3942 		keep_addr = (_keep_addr > 0);
3943 	}
3944 
3945 	/* Step 2: clear hash table */
3946 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3947 		struct hlist_head *h = &net->ipv6.inet6_addr_lst[i];
3948 
3949 		spin_lock_bh(&net->ipv6.addrconf_hash_lock);
3950 restart:
3951 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3952 			if (ifa->idev == idev) {
3953 				addrconf_del_dad_work(ifa);
3954 				/* combined flag + permanent flag decide if
3955 				 * address is retained on a down event
3956 				 */
3957 				if (!keep_addr ||
3958 				    !(ifa->flags & IFA_F_PERMANENT) ||
3959 				    addr_is_local(&ifa->addr)) {
3960 					hlist_del_init_rcu(&ifa->addr_lst);
3961 					goto restart;
3962 				}
3963 			}
3964 		}
3965 		spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
3966 	}
3967 
3968 	write_lock_bh(&idev->lock);
3969 
3970 	addrconf_del_rs_timer(idev);
3971 
3972 	/* Step 2: clear flags for stateless addrconf, repeated down
3973 	 *         detection
3974 	 */
3975 	was_ready = idev->if_flags & IF_READY;
3976 	if (!unregister)
3977 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3978 
3979 	/* Step 3: clear tempaddr list */
3980 	while (!list_empty(&idev->tempaddr_list)) {
3981 		ifa = list_first_entry(&idev->tempaddr_list,
3982 				       struct inet6_ifaddr, tmp_list);
3983 		list_del(&ifa->tmp_list);
3984 		write_unlock_bh(&idev->lock);
3985 		spin_lock_bh(&ifa->lock);
3986 
3987 		if (ifa->ifpub) {
3988 			in6_ifa_put(ifa->ifpub);
3989 			ifa->ifpub = NULL;
3990 		}
3991 		spin_unlock_bh(&ifa->lock);
3992 		in6_ifa_put(ifa);
3993 		write_lock_bh(&idev->lock);
3994 	}
3995 
3996 	list_for_each_entry(ifa, &idev->addr_list, if_list)
3997 		list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
3998 	write_unlock_bh(&idev->lock);
3999 
4000 	while (!list_empty(&tmp_addr_list)) {
4001 		struct fib6_info *rt = NULL;
4002 		bool keep;
4003 
4004 		ifa = list_first_entry(&tmp_addr_list,
4005 				       struct inet6_ifaddr, if_list_aux);
4006 		list_del(&ifa->if_list_aux);
4007 
4008 		addrconf_del_dad_work(ifa);
4009 
4010 		keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
4011 			!addr_is_local(&ifa->addr);
4012 
4013 		spin_lock_bh(&ifa->lock);
4014 
4015 		if (keep) {
4016 			/* set state to skip the notifier below */
4017 			state = INET6_IFADDR_STATE_DEAD;
4018 			ifa->state = INET6_IFADDR_STATE_PREDAD;
4019 			if (!(ifa->flags & IFA_F_NODAD))
4020 				ifa->flags |= IFA_F_TENTATIVE;
4021 
4022 			rt = ifa->rt;
4023 			ifa->rt = NULL;
4024 		} else {
4025 			state = ifa->state;
4026 			ifa->state = INET6_IFADDR_STATE_DEAD;
4027 		}
4028 
4029 		spin_unlock_bh(&ifa->lock);
4030 
4031 		if (rt)
4032 			ip6_del_rt(net, rt, false);
4033 
4034 		if (state != INET6_IFADDR_STATE_DEAD) {
4035 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
4036 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
4037 		} else {
4038 			if (idev->cnf.forwarding)
4039 				addrconf_leave_anycast(ifa);
4040 			addrconf_leave_solict(ifa->idev, &ifa->addr);
4041 		}
4042 
4043 		if (!keep) {
4044 			spin_lock_bh(&net->ipv6.addrconf_hash_lock);
4045 			hlist_del_init_rcu(&ifa->addr_lst);
4046 			spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
4047 
4048 			write_lock_bh(&idev->lock);
4049 			list_del_rcu(&ifa->if_list);
4050 			write_unlock_bh(&idev->lock);
4051 			in6_ifa_put(ifa);
4052 		}
4053 	}
4054 
4055 	/* Step 5: Discard anycast and multicast list */
4056 	if (unregister) {
4057 		ipv6_ac_destroy_dev(idev);
4058 		ipv6_mc_destroy_dev(idev);
4059 	} else if (was_ready) {
4060 		ipv6_mc_down(idev);
4061 	}
4062 
4063 	WRITE_ONCE(idev->tstamp, jiffies);
4064 	idev->ra_mtu = 0;
4065 
4066 	/* Last: Shot the device (if unregistered) */
4067 	if (unregister) {
4068 		struct neigh_table *tbl = nd_table(net);
4069 
4070 		addrconf_sysctl_unregister(idev);
4071 		neigh_parms_release(tbl, idev->nd_parms);
4072 		neigh_ifdown(tbl, dev);
4073 		in6_dev_put(idev);
4074 	}
4075 	return 0;
4076 }
4077 
4078 static void addrconf_rs_timer(struct timer_list *t)
4079 {
4080 	struct inet6_dev *idev = timer_container_of(idev, t, rs_timer);
4081 	struct net_device *dev = idev->dev;
4082 	struct in6_addr lladdr;
4083 	int rtr_solicits;
4084 
4085 	write_lock(&idev->lock);
4086 	if (idev->dead || !(idev->if_flags & IF_READY))
4087 		goto out;
4088 
4089 	if (!ipv6_accept_ra(idev))
4090 		goto out;
4091 
4092 	/* Announcement received after solicitation was sent */
4093 	if (idev->if_flags & IF_RA_RCVD)
4094 		goto out;
4095 
4096 	rtr_solicits = READ_ONCE(idev->cnf.rtr_solicits);
4097 
4098 	if (idev->rs_probes++ < rtr_solicits || rtr_solicits < 0) {
4099 		write_unlock(&idev->lock);
4100 		if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4101 			ndisc_send_rs(dev, &lladdr,
4102 				      &in6addr_linklocal_allrouters);
4103 		else
4104 			goto put;
4105 
4106 		write_lock(&idev->lock);
4107 		idev->rs_interval = rfc3315_s14_backoff_update(
4108 				idev->rs_interval,
4109 				READ_ONCE(idev->cnf.rtr_solicit_max_interval));
4110 		/* The wait after the last probe can be shorter */
4111 		addrconf_mod_rs_timer(idev, (idev->rs_probes ==
4112 					     READ_ONCE(idev->cnf.rtr_solicits)) ?
4113 				      READ_ONCE(idev->cnf.rtr_solicit_delay) :
4114 				      idev->rs_interval);
4115 	} else {
4116 		/*
4117 		 * Note: we do not support deprecated "all on-link"
4118 		 * assumption any longer.
4119 		 */
4120 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
4121 	}
4122 
4123 out:
4124 	write_unlock(&idev->lock);
4125 put:
4126 	in6_dev_put(idev);
4127 }
4128 
4129 /*
4130  *	Duplicate Address Detection
4131  */
4132 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
4133 {
4134 	struct inet6_dev *idev = ifp->idev;
4135 	unsigned long rand_num;
4136 	u64 nonce;
4137 
4138 	if (ifp->flags & IFA_F_OPTIMISTIC)
4139 		rand_num = 0;
4140 	else
4141 		rand_num = get_random_u32_below(
4142 				READ_ONCE(idev->cnf.rtr_solicit_delay) ? : 1);
4143 
4144 	nonce = 0;
4145 	if (READ_ONCE(idev->cnf.enhanced_dad) ||
4146 	    READ_ONCE(dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad)) {
4147 		do
4148 			get_random_bytes(&nonce, 6);
4149 		while (nonce == 0);
4150 	}
4151 	ifp->dad_nonce = nonce;
4152 	ifp->dad_probes = READ_ONCE(idev->cnf.dad_transmits);
4153 	addrconf_mod_dad_work(ifp, rand_num);
4154 }
4155 
4156 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
4157 {
4158 	struct inet6_dev *idev = ifp->idev;
4159 	struct net_device *dev = idev->dev;
4160 	bool bump_id, notify = false;
4161 	struct net *net;
4162 
4163 	addrconf_join_solict(dev, &ifp->addr);
4164 
4165 	read_lock_bh(&idev->lock);
4166 	spin_lock(&ifp->lock);
4167 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
4168 		goto out;
4169 
4170 	net = dev_net(dev);
4171 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
4172 	    (READ_ONCE(net->ipv6.devconf_all->accept_dad) < 1 &&
4173 	     READ_ONCE(idev->cnf.accept_dad) < 1) ||
4174 	    !(ifp->flags&IFA_F_TENTATIVE) ||
4175 	    ifp->flags & IFA_F_NODAD) {
4176 		bool send_na = false;
4177 
4178 		if (ifp->flags & IFA_F_TENTATIVE &&
4179 		    !(ifp->flags & IFA_F_OPTIMISTIC))
4180 			send_na = true;
4181 		bump_id = ifp->flags & IFA_F_TENTATIVE;
4182 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4183 		spin_unlock(&ifp->lock);
4184 		read_unlock_bh(&idev->lock);
4185 
4186 		addrconf_dad_completed(ifp, bump_id, send_na);
4187 		return;
4188 	}
4189 
4190 	if (!(idev->if_flags & IF_READY)) {
4191 		spin_unlock(&ifp->lock);
4192 		read_unlock_bh(&idev->lock);
4193 		/*
4194 		 * If the device is not ready:
4195 		 * - keep it tentative if it is a permanent address.
4196 		 * - otherwise, kill it.
4197 		 */
4198 		in6_ifa_hold(ifp);
4199 		addrconf_dad_stop(ifp, 0);
4200 		return;
4201 	}
4202 
4203 	/*
4204 	 * Optimistic nodes can start receiving
4205 	 * Frames right away
4206 	 */
4207 	if (ifp->flags & IFA_F_OPTIMISTIC) {
4208 		ip6_ins_rt(net, ifp->rt);
4209 		if (ipv6_use_optimistic_addr(net, idev)) {
4210 			/* Because optimistic nodes can use this address,
4211 			 * notify listeners. If DAD fails, RTM_DELADDR is sent.
4212 			 */
4213 			notify = true;
4214 		}
4215 	}
4216 
4217 	addrconf_dad_kick(ifp);
4218 out:
4219 	spin_unlock(&ifp->lock);
4220 	read_unlock_bh(&idev->lock);
4221 	if (notify)
4222 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
4223 }
4224 
4225 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
4226 {
4227 	bool begin_dad = false;
4228 
4229 	spin_lock_bh(&ifp->lock);
4230 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4231 		ifp->state = INET6_IFADDR_STATE_PREDAD;
4232 		begin_dad = true;
4233 	}
4234 	spin_unlock_bh(&ifp->lock);
4235 
4236 	if (begin_dad)
4237 		addrconf_mod_dad_work(ifp, 0);
4238 }
4239 
4240 static void addrconf_dad_work(struct work_struct *w)
4241 {
4242 	struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4243 						struct inet6_ifaddr,
4244 						dad_work);
4245 	struct inet6_dev *idev = ifp->idev;
4246 	bool bump_id, disable_ipv6 = false;
4247 	struct in6_addr mcaddr;
4248 	struct net *net;
4249 
4250 	enum {
4251 		DAD_PROCESS,
4252 		DAD_BEGIN,
4253 		DAD_ABORT,
4254 	} action = DAD_PROCESS;
4255 
4256 	net = dev_net(idev->dev);
4257 
4258 	rtnl_net_lock(net);
4259 
4260 	spin_lock_bh(&ifp->lock);
4261 	if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4262 		action = DAD_BEGIN;
4263 		ifp->state = INET6_IFADDR_STATE_DAD;
4264 	} else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4265 		action = DAD_ABORT;
4266 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
4267 
4268 		if ((READ_ONCE(net->ipv6.devconf_all->accept_dad) > 1 ||
4269 		     READ_ONCE(idev->cnf.accept_dad) > 1) &&
4270 		    !idev->cnf.disable_ipv6 &&
4271 		    !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
4272 			struct in6_addr addr;
4273 
4274 			addr.s6_addr32[0] = htonl(0xfe800000);
4275 			addr.s6_addr32[1] = 0;
4276 
4277 			if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
4278 			    ipv6_addr_equal(&ifp->addr, &addr)) {
4279 				/* DAD failed for link-local based on MAC */
4280 				WRITE_ONCE(idev->cnf.disable_ipv6, 1);
4281 
4282 				pr_info("%s: IPv6 being disabled!\n",
4283 					ifp->idev->dev->name);
4284 				disable_ipv6 = true;
4285 			}
4286 		}
4287 	}
4288 	spin_unlock_bh(&ifp->lock);
4289 
4290 	if (action == DAD_BEGIN) {
4291 		addrconf_dad_begin(ifp);
4292 		goto out;
4293 	} else if (action == DAD_ABORT) {
4294 		in6_ifa_hold(ifp);
4295 		addrconf_dad_stop(ifp, 1);
4296 		if (disable_ipv6)
4297 			addrconf_ifdown(idev->dev, false);
4298 		goto out;
4299 	}
4300 
4301 	if (!ifp->dad_probes && addrconf_dad_end(ifp))
4302 		goto out;
4303 
4304 	write_lock_bh(&idev->lock);
4305 	if (idev->dead || !(idev->if_flags & IF_READY)) {
4306 		write_unlock_bh(&idev->lock);
4307 		goto out;
4308 	}
4309 
4310 	spin_lock(&ifp->lock);
4311 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4312 		spin_unlock(&ifp->lock);
4313 		write_unlock_bh(&idev->lock);
4314 		goto out;
4315 	}
4316 
4317 	if (ifp->dad_probes == 0) {
4318 		bool send_na = false;
4319 
4320 		/*
4321 		 * DAD was successful
4322 		 */
4323 
4324 		if (ifp->flags & IFA_F_TENTATIVE &&
4325 		    !(ifp->flags & IFA_F_OPTIMISTIC))
4326 			send_na = true;
4327 		bump_id = ifp->flags & IFA_F_TENTATIVE;
4328 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4329 		spin_unlock(&ifp->lock);
4330 		write_unlock_bh(&idev->lock);
4331 
4332 		addrconf_dad_completed(ifp, bump_id, send_na);
4333 
4334 		goto out;
4335 	}
4336 
4337 	ifp->dad_probes--;
4338 	addrconf_mod_dad_work(ifp,
4339 			      max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME),
4340 				  HZ/100));
4341 	spin_unlock(&ifp->lock);
4342 	write_unlock_bh(&idev->lock);
4343 
4344 	/* send a neighbour solicitation for our addr */
4345 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
4346 	ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4347 		      ifp->dad_nonce);
4348 out:
4349 	in6_ifa_put(ifp);
4350 	rtnl_net_unlock(net);
4351 }
4352 
4353 /* ifp->idev must be at least read locked */
4354 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
4355 {
4356 	struct inet6_ifaddr *ifpiter;
4357 	struct inet6_dev *idev = ifp->idev;
4358 
4359 	list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4360 		if (ifpiter->scope > IFA_LINK)
4361 			break;
4362 		if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4363 		    (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4364 				       IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4365 		    IFA_F_PERMANENT)
4366 			return false;
4367 	}
4368 	return true;
4369 }
4370 
4371 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4372 				   bool send_na)
4373 {
4374 	struct net_device *dev = ifp->idev->dev;
4375 	struct in6_addr lladdr;
4376 	bool send_rs, send_mld;
4377 
4378 	addrconf_del_dad_work(ifp);
4379 
4380 	/*
4381 	 *	Configure the address for reception. Now it is valid.
4382 	 */
4383 
4384 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
4385 
4386 	/* If added prefix is link local and we are prepared to process
4387 	   router advertisements, start sending router solicitations.
4388 	 */
4389 
4390 	read_lock_bh(&ifp->idev->lock);
4391 	send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4392 	send_rs = send_mld &&
4393 		  ipv6_accept_ra(ifp->idev) &&
4394 		  READ_ONCE(ifp->idev->cnf.rtr_solicits) != 0 &&
4395 		  (dev->flags & IFF_LOOPBACK) == 0 &&
4396 		  (dev->type != ARPHRD_TUNNEL) &&
4397 		  !netif_is_team_port(dev);
4398 	read_unlock_bh(&ifp->idev->lock);
4399 
4400 	/* While dad is in progress mld report's source address is in6_addrany.
4401 	 * Resend with proper ll now.
4402 	 */
4403 	if (send_mld)
4404 		ipv6_mc_dad_complete(ifp->idev);
4405 
4406 	/* send unsolicited NA if enabled */
4407 	if (send_na &&
4408 	    (READ_ONCE(ifp->idev->cnf.ndisc_notify) ||
4409 	     READ_ONCE(dev_net(dev)->ipv6.devconf_all->ndisc_notify))) {
4410 		ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4411 			      /*router=*/ !!ifp->idev->cnf.forwarding,
4412 			      /*solicited=*/ false, /*override=*/ true,
4413 			      /*inc_opt=*/ true);
4414 	}
4415 
4416 	if (send_rs) {
4417 		/*
4418 		 *	If a host as already performed a random delay
4419 		 *	[...] as part of DAD [...] there is no need
4420 		 *	to delay again before sending the first RS
4421 		 */
4422 		if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4423 			return;
4424 		ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
4425 
4426 		write_lock_bh(&ifp->idev->lock);
4427 		spin_lock(&ifp->lock);
4428 		ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4429 			READ_ONCE(ifp->idev->cnf.rtr_solicit_interval));
4430 		ifp->idev->rs_probes = 1;
4431 		ifp->idev->if_flags |= IF_RS_SENT;
4432 		addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4433 		spin_unlock(&ifp->lock);
4434 		write_unlock_bh(&ifp->idev->lock);
4435 	}
4436 
4437 	if (bump_id)
4438 		rt_genid_bump_ipv6(dev_net(dev));
4439 
4440 	/* Make sure that a new temporary address will be created
4441 	 * before this temporary address becomes deprecated.
4442 	 */
4443 	if (ifp->flags & IFA_F_TEMPORARY)
4444 		addrconf_verify_rtnl(dev_net(dev));
4445 }
4446 
4447 static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
4448 {
4449 	struct inet6_ifaddr *ifp;
4450 
4451 	read_lock_bh(&idev->lock);
4452 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
4453 		spin_lock(&ifp->lock);
4454 		if ((ifp->flags & IFA_F_TENTATIVE &&
4455 		     ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4456 			if (restart)
4457 				ifp->state = INET6_IFADDR_STATE_PREDAD;
4458 			addrconf_dad_kick(ifp);
4459 		}
4460 		spin_unlock(&ifp->lock);
4461 	}
4462 	read_unlock_bh(&idev->lock);
4463 }
4464 
4465 #ifdef CONFIG_PROC_FS
4466 struct if6_iter_state {
4467 	struct seq_net_private p;
4468 	int bucket;
4469 	int offset;
4470 };
4471 
4472 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4473 {
4474 	struct if6_iter_state *state = seq->private;
4475 	struct net *net = seq_file_net(seq);
4476 	struct inet6_ifaddr *ifa = NULL;
4477 	int p = 0;
4478 
4479 	/* initial bucket if pos is 0 */
4480 	if (pos == 0) {
4481 		state->bucket = 0;
4482 		state->offset = 0;
4483 	}
4484 
4485 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4486 		hlist_for_each_entry_rcu(ifa, &net->ipv6.inet6_addr_lst[state->bucket],
4487 					 addr_lst) {
4488 			/* sync with offset */
4489 			if (p < state->offset) {
4490 				p++;
4491 				continue;
4492 			}
4493 			return ifa;
4494 		}
4495 
4496 		/* prepare for next bucket */
4497 		state->offset = 0;
4498 		p = 0;
4499 	}
4500 	return NULL;
4501 }
4502 
4503 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4504 					 struct inet6_ifaddr *ifa)
4505 {
4506 	struct if6_iter_state *state = seq->private;
4507 	struct net *net = seq_file_net(seq);
4508 
4509 	hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
4510 		state->offset++;
4511 		return ifa;
4512 	}
4513 
4514 	state->offset = 0;
4515 	while (++state->bucket < IN6_ADDR_HSIZE) {
4516 		hlist_for_each_entry_rcu(ifa,
4517 				     &net->ipv6.inet6_addr_lst[state->bucket], addr_lst) {
4518 			return ifa;
4519 		}
4520 	}
4521 
4522 	return NULL;
4523 }
4524 
4525 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4526 	__acquires(rcu)
4527 {
4528 	rcu_read_lock();
4529 	return if6_get_first(seq, *pos);
4530 }
4531 
4532 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4533 {
4534 	struct inet6_ifaddr *ifa;
4535 
4536 	ifa = if6_get_next(seq, v);
4537 	++*pos;
4538 	return ifa;
4539 }
4540 
4541 static void if6_seq_stop(struct seq_file *seq, void *v)
4542 	__releases(rcu)
4543 {
4544 	rcu_read_unlock();
4545 }
4546 
4547 static int if6_seq_show(struct seq_file *seq, void *v)
4548 {
4549 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4550 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4551 		   &ifp->addr,
4552 		   ifp->idev->dev->ifindex,
4553 		   ifp->prefix_len,
4554 		   ifp->scope,
4555 		   (u8) ifp->flags,
4556 		   ifp->idev->dev->name);
4557 	return 0;
4558 }
4559 
4560 static const struct seq_operations if6_seq_ops = {
4561 	.start	= if6_seq_start,
4562 	.next	= if6_seq_next,
4563 	.show	= if6_seq_show,
4564 	.stop	= if6_seq_stop,
4565 };
4566 
4567 static int __net_init if6_proc_net_init(struct net *net)
4568 {
4569 	if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4570 			sizeof(struct if6_iter_state)))
4571 		return -ENOMEM;
4572 	return 0;
4573 }
4574 
4575 static void __net_exit if6_proc_net_exit(struct net *net)
4576 {
4577 	remove_proc_entry("if_inet6", net->proc_net);
4578 }
4579 
4580 static struct pernet_operations if6_proc_net_ops = {
4581 	.init = if6_proc_net_init,
4582 	.exit = if6_proc_net_exit,
4583 };
4584 
4585 int __init if6_proc_init(void)
4586 {
4587 	return register_pernet_subsys(&if6_proc_net_ops);
4588 }
4589 
4590 void if6_proc_exit(void)
4591 {
4592 	unregister_pernet_subsys(&if6_proc_net_ops);
4593 }
4594 #endif	/* CONFIG_PROC_FS */
4595 
4596 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4597 /* Check if address is a home address configured on any interface. */
4598 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4599 {
4600 	unsigned int hash = inet6_addr_hash(net, addr);
4601 	struct inet6_ifaddr *ifp = NULL;
4602 	int ret = 0;
4603 
4604 	rcu_read_lock();
4605 	hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4606 		if (ipv6_addr_equal(&ifp->addr, addr) &&
4607 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
4608 			ret = 1;
4609 			break;
4610 		}
4611 	}
4612 	rcu_read_unlock();
4613 	return ret;
4614 }
4615 #endif
4616 
4617 /* RFC6554 has some algorithm to avoid loops in segment routing by
4618  * checking if the segments contains any of a local interface address.
4619  *
4620  * Quote:
4621  *
4622  * To detect loops in the SRH, a router MUST determine if the SRH
4623  * includes multiple addresses assigned to any interface on that router.
4624  * If such addresses appear more than once and are separated by at least
4625  * one address not assigned to that router.
4626  */
4627 int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
4628 			  unsigned char nsegs)
4629 {
4630 	const struct in6_addr *addr;
4631 	int i, ret = 0, found = 0;
4632 	struct inet6_ifaddr *ifp;
4633 	bool separated = false;
4634 	unsigned int hash;
4635 	bool hash_found;
4636 
4637 	rcu_read_lock();
4638 	for (i = 0; i < nsegs; i++) {
4639 		addr = &segs[i];
4640 		hash = inet6_addr_hash(net, addr);
4641 
4642 		hash_found = false;
4643 		hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4644 
4645 			if (ipv6_addr_equal(&ifp->addr, addr)) {
4646 				hash_found = true;
4647 				break;
4648 			}
4649 		}
4650 
4651 		if (hash_found) {
4652 			if (found > 1 && separated) {
4653 				ret = 1;
4654 				break;
4655 			}
4656 
4657 			separated = false;
4658 			found++;
4659 		} else {
4660 			separated = true;
4661 		}
4662 	}
4663 	rcu_read_unlock();
4664 
4665 	return ret;
4666 }
4667 
4668 /*
4669  *	Periodic address status verification
4670  */
4671 
4672 static void addrconf_verify_rtnl(struct net *net)
4673 {
4674 	unsigned long now, next, next_sec, next_sched;
4675 	struct inet6_ifaddr *ifp;
4676 	int i;
4677 
4678 	ASSERT_RTNL();
4679 
4680 	rcu_read_lock_bh();
4681 	now = jiffies;
4682 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4683 
4684 	cancel_delayed_work(&net->ipv6.addr_chk_work);
4685 
4686 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4687 restart:
4688 		hlist_for_each_entry_rcu_bh(ifp, &net->ipv6.inet6_addr_lst[i], addr_lst) {
4689 			unsigned long age;
4690 
4691 			/* When setting preferred_lft to a value not zero or
4692 			 * infinity, while valid_lft is infinity
4693 			 * IFA_F_PERMANENT has a non-infinity life time.
4694 			 */
4695 			if ((ifp->flags & IFA_F_PERMANENT) &&
4696 			    (ifp->prefered_lft == INFINITY_LIFE_TIME))
4697 				continue;
4698 
4699 			spin_lock(&ifp->lock);
4700 			/* We try to batch several events at once. */
4701 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4702 
4703 			if ((ifp->flags&IFA_F_TEMPORARY) &&
4704 			    !(ifp->flags&IFA_F_TENTATIVE) &&
4705 			    ifp->prefered_lft != INFINITY_LIFE_TIME &&
4706 			    !ifp->regen_count && ifp->ifpub) {
4707 				/* This is a non-regenerated temporary addr. */
4708 
4709 				unsigned long regen_advance = ipv6_get_regen_advance(ifp->idev);
4710 
4711 				if (age + regen_advance >= ifp->prefered_lft) {
4712 					struct inet6_ifaddr *ifpub = ifp->ifpub;
4713 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4714 						next = ifp->tstamp + ifp->prefered_lft * HZ;
4715 
4716 					ifp->regen_count++;
4717 					in6_ifa_hold(ifp);
4718 					in6_ifa_hold(ifpub);
4719 					spin_unlock(&ifp->lock);
4720 
4721 					spin_lock(&ifpub->lock);
4722 					ifpub->regen_count = 0;
4723 					spin_unlock(&ifpub->lock);
4724 					rcu_read_unlock_bh();
4725 					ipv6_create_tempaddr(ifpub, true);
4726 					in6_ifa_put(ifpub);
4727 					in6_ifa_put(ifp);
4728 					rcu_read_lock_bh();
4729 					goto restart;
4730 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4731 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4732 			}
4733 
4734 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4735 			    age >= ifp->valid_lft) {
4736 				spin_unlock(&ifp->lock);
4737 				in6_ifa_hold(ifp);
4738 				rcu_read_unlock_bh();
4739 				ipv6_del_addr(ifp);
4740 				rcu_read_lock_bh();
4741 				goto restart;
4742 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4743 				spin_unlock(&ifp->lock);
4744 				continue;
4745 			} else if (age >= ifp->prefered_lft) {
4746 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4747 				int deprecate = 0;
4748 
4749 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
4750 					deprecate = 1;
4751 					ifp->flags |= IFA_F_DEPRECATED;
4752 				}
4753 
4754 				if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4755 				    (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4756 					next = ifp->tstamp + ifp->valid_lft * HZ;
4757 
4758 				spin_unlock(&ifp->lock);
4759 
4760 				if (deprecate) {
4761 					in6_ifa_hold(ifp);
4762 
4763 					ipv6_ifa_notify(0, ifp);
4764 					in6_ifa_put(ifp);
4765 					goto restart;
4766 				}
4767 			} else {
4768 				/* ifp->prefered_lft <= ifp->valid_lft */
4769 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4770 					next = ifp->tstamp + ifp->prefered_lft * HZ;
4771 				spin_unlock(&ifp->lock);
4772 			}
4773 		}
4774 	}
4775 
4776 	next_sec = round_jiffies_up(next);
4777 	next_sched = next;
4778 
4779 	/* If rounded timeout is accurate enough, accept it. */
4780 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4781 		next_sched = next_sec;
4782 
4783 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4784 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4785 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4786 
4787 	pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4788 		 now, next, next_sec, next_sched);
4789 	mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, next_sched - now);
4790 	rcu_read_unlock_bh();
4791 }
4792 
4793 static void addrconf_verify_work(struct work_struct *w)
4794 {
4795 	struct net *net = container_of(to_delayed_work(w), struct net,
4796 				       ipv6.addr_chk_work);
4797 
4798 	rtnl_net_lock(net);
4799 	addrconf_verify_rtnl(net);
4800 	rtnl_net_unlock(net);
4801 }
4802 
4803 static void addrconf_verify(struct net *net)
4804 {
4805 	mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, 0);
4806 }
4807 
4808 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4809 				     struct in6_addr **peer_pfx)
4810 {
4811 	struct in6_addr *pfx = NULL;
4812 
4813 	*peer_pfx = NULL;
4814 
4815 	if (addr)
4816 		pfx = nla_data(addr);
4817 
4818 	if (local) {
4819 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4820 			*peer_pfx = pfx;
4821 		pfx = nla_data(local);
4822 	}
4823 
4824 	return pfx;
4825 }
4826 
4827 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4828 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
4829 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
4830 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
4831 	[IFA_FLAGS]		= { .len = sizeof(u32) },
4832 	[IFA_RT_PRIORITY]	= { .len = sizeof(u32) },
4833 	[IFA_TARGET_NETNSID]	= { .type = NLA_S32 },
4834 	[IFA_PROTO]		= { .type = NLA_U8 },
4835 };
4836 
4837 static int
4838 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4839 		  struct netlink_ext_ack *extack)
4840 {
4841 	struct net *net = sock_net(skb->sk);
4842 	struct ifaddrmsg *ifm;
4843 	struct nlattr *tb[IFA_MAX+1];
4844 	struct in6_addr *pfx, *peer_pfx;
4845 	u32 ifa_flags;
4846 	int err;
4847 
4848 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4849 				     ifa_ipv6_policy, extack);
4850 	if (err < 0)
4851 		return err;
4852 
4853 	ifm = nlmsg_data(nlh);
4854 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4855 	if (!pfx)
4856 		return -EINVAL;
4857 
4858 	ifa_flags = nla_get_u32_default(tb[IFA_FLAGS], ifm->ifa_flags);
4859 
4860 	/* We ignore other flags so far. */
4861 	ifa_flags &= IFA_F_MANAGETEMPADDR;
4862 
4863 	rtnl_net_lock(net);
4864 	err = inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4865 			     ifm->ifa_prefixlen, extack);
4866 	rtnl_net_unlock(net);
4867 
4868 	return err;
4869 }
4870 
4871 static int modify_prefix_route(struct net *net, struct inet6_ifaddr *ifp,
4872 			       unsigned long expires, u32 flags,
4873 			       bool modify_peer)
4874 {
4875 	struct fib6_table *table;
4876 	struct fib6_info *f6i;
4877 	u32 prio;
4878 
4879 	f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4880 					ifp->prefix_len,
4881 					ifp->idev->dev, 0, RTF_DEFAULT, true);
4882 	if (!f6i)
4883 		return -ENOENT;
4884 
4885 	prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4886 	if (f6i->fib6_metric != prio) {
4887 		/* delete old one */
4888 		ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
4889 
4890 		/* add new one */
4891 		addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4892 				      ifp->prefix_len,
4893 				      ifp->rt_priority, ifp->idev->dev,
4894 				      expires, flags, GFP_KERNEL);
4895 		return 0;
4896 	}
4897 	if (f6i != net->ipv6.fib6_null_entry) {
4898 		table = f6i->fib6_table;
4899 		spin_lock_bh(&table->tb6_lock);
4900 
4901 		if (!(flags & RTF_EXPIRES)) {
4902 			fib6_clean_expires(f6i);
4903 			fib6_may_remove_gc_list(net, f6i);
4904 		} else {
4905 			fib6_set_expires(f6i, jiffies + clock_t_to_jiffies(expires));
4906 			fib6_add_gc_list(f6i);
4907 		}
4908 
4909 		spin_unlock_bh(&table->tb6_lock);
4910 	}
4911 	fib6_info_release(f6i);
4912 
4913 	return 0;
4914 }
4915 
4916 static int inet6_addr_modify(struct net *net, struct inet6_ifaddr *ifp,
4917 			     struct ifa6_config *cfg, clock_t expires,
4918 			     u32 flags)
4919 {
4920 	bool was_managetempaddr;
4921 	bool new_peer = false;
4922 	bool had_prefixroute;
4923 
4924 	ASSERT_RTNL_NET(net);
4925 
4926 	if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
4927 	    (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4928 		return -EINVAL;
4929 
4930 	if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
4931 		cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
4932 
4933 	if (cfg->peer_pfx &&
4934 	    memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) {
4935 		if (!ipv6_addr_any(&ifp->peer_addr))
4936 			cleanup_prefix_route(ifp, expires, true, true);
4937 		new_peer = true;
4938 	}
4939 
4940 	spin_lock_bh(&ifp->lock);
4941 	was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4942 	had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4943 			  !(ifp->flags & IFA_F_NOPREFIXROUTE);
4944 	ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4945 			IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4946 			IFA_F_NOPREFIXROUTE);
4947 	ifp->flags |= cfg->ifa_flags;
4948 	WRITE_ONCE(ifp->tstamp, jiffies);
4949 	WRITE_ONCE(ifp->valid_lft, cfg->valid_lft);
4950 	WRITE_ONCE(ifp->prefered_lft, cfg->preferred_lft);
4951 	WRITE_ONCE(ifp->ifa_proto, cfg->ifa_proto);
4952 
4953 	if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
4954 		WRITE_ONCE(ifp->rt_priority, cfg->rt_priority);
4955 
4956 	if (new_peer)
4957 		ifp->peer_addr = *cfg->peer_pfx;
4958 
4959 	spin_unlock_bh(&ifp->lock);
4960 	if (!(ifp->flags&IFA_F_TENTATIVE))
4961 		ipv6_ifa_notify(0, ifp);
4962 
4963 	if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
4964 		int rc = -ENOENT;
4965 
4966 		if (had_prefixroute)
4967 			rc = modify_prefix_route(net, ifp, expires, flags, false);
4968 
4969 		/* prefix route could have been deleted; if so restore it */
4970 		if (rc == -ENOENT) {
4971 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4972 					      ifp->rt_priority, ifp->idev->dev,
4973 					      expires, flags, GFP_KERNEL);
4974 		}
4975 
4976 		if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr))
4977 			rc = modify_prefix_route(net, ifp, expires, flags, true);
4978 
4979 		if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) {
4980 			addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len,
4981 					      ifp->rt_priority, ifp->idev->dev,
4982 					      expires, flags, GFP_KERNEL);
4983 		}
4984 	} else if (had_prefixroute) {
4985 		enum cleanup_prefix_rt_t action;
4986 		unsigned long rt_expires;
4987 
4988 		write_lock_bh(&ifp->idev->lock);
4989 		action = check_cleanup_prefix_route(ifp, &rt_expires);
4990 		write_unlock_bh(&ifp->idev->lock);
4991 
4992 		if (action != CLEANUP_PREFIX_RT_NOP) {
4993 			cleanup_prefix_route(ifp, rt_expires,
4994 				action == CLEANUP_PREFIX_RT_DEL, false);
4995 		}
4996 	}
4997 
4998 	if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4999 		if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
5000 			delete_tempaddrs(ifp->idev, ifp);
5001 		else
5002 			manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
5003 					 cfg->preferred_lft, !was_managetempaddr,
5004 					 jiffies);
5005 	}
5006 
5007 	addrconf_verify_rtnl(net);
5008 
5009 	return 0;
5010 }
5011 
5012 static int
5013 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
5014 		  struct netlink_ext_ack *extack)
5015 {
5016 	struct net *net = sock_net(skb->sk);
5017 	struct nlattr *tb[IFA_MAX+1];
5018 	struct in6_addr *peer_pfx;
5019 	struct inet6_ifaddr *ifa;
5020 	struct net_device *dev;
5021 	struct inet6_dev *idev;
5022 	struct ifa6_config cfg;
5023 	struct ifaddrmsg *ifm;
5024 	unsigned long timeout;
5025 	clock_t expires;
5026 	u32 flags;
5027 	int err;
5028 
5029 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
5030 				     ifa_ipv6_policy, extack);
5031 	if (err < 0)
5032 		return err;
5033 
5034 	memset(&cfg, 0, sizeof(cfg));
5035 
5036 	ifm = nlmsg_data(nlh);
5037 	cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
5038 	if (!cfg.pfx)
5039 		return -EINVAL;
5040 
5041 	cfg.peer_pfx = peer_pfx;
5042 	cfg.plen = ifm->ifa_prefixlen;
5043 	if (tb[IFA_RT_PRIORITY])
5044 		cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
5045 
5046 	if (tb[IFA_PROTO])
5047 		cfg.ifa_proto = nla_get_u8(tb[IFA_PROTO]);
5048 
5049 	cfg.ifa_flags = nla_get_u32_default(tb[IFA_FLAGS], ifm->ifa_flags);
5050 
5051 	/* We ignore other flags so far. */
5052 	cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
5053 			 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
5054 			 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
5055 
5056 	cfg.ifa_flags |= IFA_F_PERMANENT;
5057 	cfg.valid_lft = INFINITY_LIFE_TIME;
5058 	cfg.preferred_lft = INFINITY_LIFE_TIME;
5059 	expires = 0;
5060 	flags = 0;
5061 
5062 	if (tb[IFA_CACHEINFO]) {
5063 		struct ifa_cacheinfo *ci;
5064 
5065 		ci = nla_data(tb[IFA_CACHEINFO]);
5066 		cfg.valid_lft = ci->ifa_valid;
5067 		cfg.preferred_lft = ci->ifa_prefered;
5068 
5069 		if (!cfg.valid_lft || cfg.preferred_lft > cfg.valid_lft) {
5070 			NL_SET_ERR_MSG_MOD(extack, "address lifetime invalid");
5071 			return -EINVAL;
5072 		}
5073 
5074 		timeout = addrconf_timeout_fixup(cfg.valid_lft, HZ);
5075 		if (addrconf_finite_timeout(timeout)) {
5076 			cfg.ifa_flags &= ~IFA_F_PERMANENT;
5077 			cfg.valid_lft = timeout;
5078 			expires = jiffies_to_clock_t(timeout * HZ);
5079 			flags = RTF_EXPIRES;
5080 		}
5081 
5082 		timeout = addrconf_timeout_fixup(cfg.preferred_lft, HZ);
5083 		if (addrconf_finite_timeout(timeout)) {
5084 			if (timeout == 0)
5085 				cfg.ifa_flags |= IFA_F_DEPRECATED;
5086 
5087 			cfg.preferred_lft = timeout;
5088 		}
5089 	}
5090 
5091 	rtnl_net_lock(net);
5092 
5093 	dev =  __dev_get_by_index(net, ifm->ifa_index);
5094 	if (!dev) {
5095 		NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface");
5096 		err = -ENODEV;
5097 		goto unlock_rtnl;
5098 	}
5099 
5100 	netdev_lock_ops(dev);
5101 	idev = ipv6_find_idev(dev);
5102 	if (IS_ERR(idev)) {
5103 		err = PTR_ERR(idev);
5104 		goto unlock;
5105 	}
5106 
5107 	if (!ipv6_allow_optimistic_dad(net, idev))
5108 		cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
5109 
5110 	if (cfg.ifa_flags & IFA_F_NODAD &&
5111 	    cfg.ifa_flags & IFA_F_OPTIMISTIC) {
5112 		NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
5113 		err = -EINVAL;
5114 		goto unlock;
5115 	}
5116 
5117 	ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
5118 	if (!ifa) {
5119 		/*
5120 		 * It would be best to check for !NLM_F_CREATE here but
5121 		 * userspace already relies on not having to provide this.
5122 		 */
5123 		err = inet6_addr_add(net, dev, &cfg, expires, flags, extack);
5124 		goto unlock;
5125 	}
5126 
5127 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
5128 	    !(nlh->nlmsg_flags & NLM_F_REPLACE)) {
5129 		NL_SET_ERR_MSG_MOD(extack, "address already assigned");
5130 		err = -EEXIST;
5131 	} else {
5132 		err = inet6_addr_modify(net, ifa, &cfg, expires, flags);
5133 	}
5134 
5135 	in6_ifa_put(ifa);
5136 unlock:
5137 	netdev_unlock_ops(dev);
5138 unlock_rtnl:
5139 	rtnl_net_unlock(net);
5140 
5141 	return err;
5142 }
5143 
5144 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
5145 			  u8 scope, int ifindex)
5146 {
5147 	struct ifaddrmsg *ifm;
5148 
5149 	ifm = nlmsg_data(nlh);
5150 	ifm->ifa_family = AF_INET6;
5151 	ifm->ifa_prefixlen = prefixlen;
5152 	ifm->ifa_flags = flags;
5153 	ifm->ifa_scope = scope;
5154 	ifm->ifa_index = ifindex;
5155 }
5156 
5157 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
5158 			 unsigned long tstamp, u32 preferred, u32 valid)
5159 {
5160 	struct ifa_cacheinfo ci;
5161 
5162 	ci.cstamp = cstamp_delta(cstamp);
5163 	ci.tstamp = cstamp_delta(tstamp);
5164 	ci.ifa_prefered = preferred;
5165 	ci.ifa_valid = valid;
5166 
5167 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
5168 }
5169 
5170 static inline int rt_scope(int ifa_scope)
5171 {
5172 	if (ifa_scope & IFA_HOST)
5173 		return RT_SCOPE_HOST;
5174 	else if (ifa_scope & IFA_LINK)
5175 		return RT_SCOPE_LINK;
5176 	else if (ifa_scope & IFA_SITE)
5177 		return RT_SCOPE_SITE;
5178 	else
5179 		return RT_SCOPE_UNIVERSE;
5180 }
5181 
5182 static inline int inet6_ifaddr_msgsize(void)
5183 {
5184 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
5185 	       + nla_total_size(16) /* IFA_LOCAL */
5186 	       + nla_total_size(16) /* IFA_ADDRESS */
5187 	       + nla_total_size(sizeof(struct ifa_cacheinfo))
5188 	       + nla_total_size(4)  /* IFA_FLAGS */
5189 	       + nla_total_size(1)  /* IFA_PROTO */
5190 	       + nla_total_size(4)  /* IFA_RT_PRIORITY */;
5191 }
5192 
5193 static int inet6_fill_ifaddr(struct sk_buff *skb,
5194 			     const struct inet6_ifaddr *ifa,
5195 			     struct inet6_fill_args *args)
5196 {
5197 	struct nlmsghdr *nlh;
5198 	u32 preferred, valid;
5199 	u32 flags, priority;
5200 	u8 proto;
5201 
5202 	nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5203 			sizeof(struct ifaddrmsg), args->flags);
5204 	if (!nlh)
5205 		return -EMSGSIZE;
5206 
5207 	flags = READ_ONCE(ifa->flags);
5208 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
5209 		      ifa->idev->dev->ifindex);
5210 
5211 	if (args->netnsid >= 0 &&
5212 	    nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
5213 		goto error;
5214 
5215 	preferred = READ_ONCE(ifa->prefered_lft);
5216 	valid = READ_ONCE(ifa->valid_lft);
5217 
5218 	if (!((flags & IFA_F_PERMANENT) &&
5219 	      (preferred == INFINITY_LIFE_TIME))) {
5220 		if (preferred != INFINITY_LIFE_TIME) {
5221 			long tval = (jiffies - READ_ONCE(ifa->tstamp)) / HZ;
5222 
5223 			if (preferred > tval)
5224 				preferred -= tval;
5225 			else
5226 				preferred = 0;
5227 			if (valid != INFINITY_LIFE_TIME) {
5228 				if (valid > tval)
5229 					valid -= tval;
5230 				else
5231 					valid = 0;
5232 			}
5233 		}
5234 	} else {
5235 		preferred = INFINITY_LIFE_TIME;
5236 		valid = INFINITY_LIFE_TIME;
5237 	}
5238 
5239 	if (!ipv6_addr_any(&ifa->peer_addr)) {
5240 		if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
5241 		    nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
5242 			goto error;
5243 	} else {
5244 		if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
5245 			goto error;
5246 	}
5247 
5248 	priority = READ_ONCE(ifa->rt_priority);
5249 	if (priority && nla_put_u32(skb, IFA_RT_PRIORITY, priority))
5250 		goto error;
5251 
5252 	if (put_cacheinfo(skb, ifa->cstamp, READ_ONCE(ifa->tstamp),
5253 			  preferred, valid) < 0)
5254 		goto error;
5255 
5256 	if (nla_put_u32(skb, IFA_FLAGS, flags) < 0)
5257 		goto error;
5258 
5259 	proto = READ_ONCE(ifa->ifa_proto);
5260 	if (proto && nla_put_u8(skb, IFA_PROTO, proto))
5261 		goto error;
5262 
5263 	nlmsg_end(skb, nlh);
5264 	return 0;
5265 
5266 error:
5267 	nlmsg_cancel(skb, nlh);
5268 	return -EMSGSIZE;
5269 }
5270 
5271 int inet6_fill_ifmcaddr(struct sk_buff *skb,
5272 			const struct ifmcaddr6 *ifmca,
5273 			struct inet6_fill_args *args)
5274 {
5275 	int ifindex = ifmca->idev->dev->ifindex;
5276 	u8 scope = RT_SCOPE_UNIVERSE;
5277 	struct nlmsghdr *nlh;
5278 
5279 	if (!args->force_rt_scope_universe &&
5280 	    ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
5281 		scope = RT_SCOPE_SITE;
5282 
5283 	nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5284 			sizeof(struct ifaddrmsg), args->flags);
5285 	if (!nlh)
5286 		return -EMSGSIZE;
5287 
5288 	if (args->netnsid >= 0 &&
5289 	    nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5290 		nlmsg_cancel(skb, nlh);
5291 		return -EMSGSIZE;
5292 	}
5293 
5294 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5295 	if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
5296 	    nla_put_u32(skb, IFA_MC_USERS, READ_ONCE(ifmca->mca_users)) < 0 ||
5297 	    put_cacheinfo(skb, ifmca->mca_cstamp, READ_ONCE(ifmca->mca_tstamp),
5298 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
5299 		nlmsg_cancel(skb, nlh);
5300 		return -EMSGSIZE;
5301 	}
5302 
5303 	nlmsg_end(skb, nlh);
5304 	return 0;
5305 }
5306 
5307 int inet6_fill_ifacaddr(struct sk_buff *skb,
5308 			const struct ifacaddr6 *ifaca,
5309 			struct inet6_fill_args *args)
5310 {
5311 	struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
5312 	int ifindex = dev ? dev->ifindex : 1;
5313 	u8 scope = RT_SCOPE_UNIVERSE;
5314 	struct nlmsghdr *nlh;
5315 
5316 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
5317 		scope = RT_SCOPE_SITE;
5318 
5319 	nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5320 			sizeof(struct ifaddrmsg), args->flags);
5321 	if (!nlh)
5322 		return -EMSGSIZE;
5323 
5324 	if (args->netnsid >= 0 &&
5325 	    nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5326 		nlmsg_cancel(skb, nlh);
5327 		return -EMSGSIZE;
5328 	}
5329 
5330 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5331 	if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
5332 	    put_cacheinfo(skb, ifaca->aca_cstamp, READ_ONCE(ifaca->aca_tstamp),
5333 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
5334 		nlmsg_cancel(skb, nlh);
5335 		return -EMSGSIZE;
5336 	}
5337 
5338 	nlmsg_end(skb, nlh);
5339 	return 0;
5340 }
5341 
5342 /* called with rcu_read_lock() */
5343 static int in6_dump_addrs(const struct inet6_dev *idev, struct sk_buff *skb,
5344 			  struct netlink_callback *cb, int *s_ip_idx,
5345 			  struct inet6_fill_args *fillargs)
5346 {
5347 	const struct ifmcaddr6 *ifmca;
5348 	const struct ifacaddr6 *ifaca;
5349 	int ip_idx = 0;
5350 	int err = 0;
5351 
5352 	switch (fillargs->type) {
5353 	case UNICAST_ADDR: {
5354 		const struct inet6_ifaddr *ifa;
5355 		fillargs->event = RTM_NEWADDR;
5356 
5357 		/* unicast address incl. temp addr */
5358 		list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
5359 			if (ip_idx < *s_ip_idx)
5360 				goto next;
5361 			err = inet6_fill_ifaddr(skb, ifa, fillargs);
5362 			if (err < 0)
5363 				break;
5364 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5365 next:
5366 			ip_idx++;
5367 		}
5368 		break;
5369 	}
5370 	case MULTICAST_ADDR:
5371 		fillargs->event = RTM_GETMULTICAST;
5372 
5373 		/* multicast address */
5374 		for (ifmca = rcu_dereference(idev->mc_list);
5375 		     ifmca;
5376 		     ifmca = rcu_dereference(ifmca->next), ip_idx++) {
5377 			if (ip_idx < *s_ip_idx)
5378 				continue;
5379 			err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
5380 			if (err < 0)
5381 				break;
5382 		}
5383 		break;
5384 	case ANYCAST_ADDR:
5385 		fillargs->event = RTM_GETANYCAST;
5386 		/* anycast address */
5387 		for (ifaca = rcu_dereference(idev->ac_list); ifaca;
5388 		     ifaca = rcu_dereference(ifaca->aca_next), ip_idx++) {
5389 			if (ip_idx < *s_ip_idx)
5390 				continue;
5391 			err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
5392 			if (err < 0)
5393 				break;
5394 		}
5395 		break;
5396 	default:
5397 		break;
5398 	}
5399 	*s_ip_idx = err ? ip_idx : 0;
5400 	return err;
5401 }
5402 
5403 static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5404 				       struct inet6_fill_args *fillargs,
5405 				       struct net **tgt_net, struct sock *sk,
5406 				       struct netlink_callback *cb)
5407 {
5408 	struct netlink_ext_ack *extack = cb->extack;
5409 	struct nlattr *tb[IFA_MAX+1];
5410 	struct ifaddrmsg *ifm;
5411 	int err, i;
5412 
5413 	ifm = nlmsg_payload(nlh, sizeof(*ifm));
5414 	if (!ifm) {
5415 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5416 		return -EINVAL;
5417 	}
5418 
5419 	if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5420 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5421 		return -EINVAL;
5422 	}
5423 
5424 	fillargs->ifindex = ifm->ifa_index;
5425 	if (fillargs->ifindex) {
5426 		cb->answer_flags |= NLM_F_DUMP_FILTERED;
5427 		fillargs->flags |= NLM_F_DUMP_FILTERED;
5428 	}
5429 
5430 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5431 					    ifa_ipv6_policy, extack);
5432 	if (err < 0)
5433 		return err;
5434 
5435 	for (i = 0; i <= IFA_MAX; ++i) {
5436 		if (!tb[i])
5437 			continue;
5438 
5439 		if (i == IFA_TARGET_NETNSID) {
5440 			struct net *net;
5441 
5442 			fillargs->netnsid = nla_get_s32(tb[i]);
5443 			net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5444 			if (IS_ERR(net)) {
5445 				fillargs->netnsid = -1;
5446 				NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5447 				return PTR_ERR(net);
5448 			}
5449 			*tgt_net = net;
5450 		} else {
5451 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5452 			return -EINVAL;
5453 		}
5454 	}
5455 
5456 	return 0;
5457 }
5458 
5459 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5460 			   enum addr_type_t type)
5461 {
5462 	struct net *tgt_net = sock_net(skb->sk);
5463 	const struct nlmsghdr *nlh = cb->nlh;
5464 	struct inet6_fill_args fillargs = {
5465 		.portid = NETLINK_CB(cb->skb).portid,
5466 		.seq = cb->nlh->nlmsg_seq,
5467 		.flags = NLM_F_MULTI,
5468 		.netnsid = -1,
5469 		.type = type,
5470 		.force_rt_scope_universe = false,
5471 	};
5472 	struct {
5473 		unsigned long ifindex;
5474 		int ip_idx;
5475 	} *ctx = (void *)cb->ctx;
5476 	struct net_device *dev;
5477 	struct inet6_dev *idev;
5478 	int err = 0;
5479 
5480 	rcu_read_lock();
5481 	if (cb->strict_check) {
5482 		err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
5483 						  skb->sk, cb);
5484 		if (err < 0)
5485 			goto done;
5486 
5487 		err = 0;
5488 		if (fillargs.ifindex) {
5489 			dev = dev_get_by_index_rcu(tgt_net, fillargs.ifindex);
5490 			if (!dev) {
5491 				err = -ENODEV;
5492 				goto done;
5493 			}
5494 			idev = __in6_dev_get(dev);
5495 			if (idev)
5496 				err = in6_dump_addrs(idev, skb, cb,
5497 						     &ctx->ip_idx,
5498 						     &fillargs);
5499 			goto done;
5500 		}
5501 	}
5502 
5503 	cb->seq = inet6_base_seq(tgt_net);
5504 	for_each_netdev_dump(tgt_net, dev, ctx->ifindex) {
5505 		idev = __in6_dev_get(dev);
5506 		if (!idev)
5507 			continue;
5508 		err = in6_dump_addrs(idev, skb, cb, &ctx->ip_idx,
5509 				     &fillargs);
5510 		if (err < 0)
5511 			goto done;
5512 	}
5513 done:
5514 	rcu_read_unlock();
5515 	if (fillargs.netnsid >= 0)
5516 		put_net(tgt_net);
5517 
5518 	return err;
5519 }
5520 
5521 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
5522 {
5523 	enum addr_type_t type = UNICAST_ADDR;
5524 
5525 	return inet6_dump_addr(skb, cb, type);
5526 }
5527 
5528 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
5529 {
5530 	enum addr_type_t type = MULTICAST_ADDR;
5531 
5532 	return inet6_dump_addr(skb, cb, type);
5533 }
5534 
5535 
5536 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
5537 {
5538 	enum addr_type_t type = ANYCAST_ADDR;
5539 
5540 	return inet6_dump_addr(skb, cb, type);
5541 }
5542 
5543 static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
5544 				       const struct nlmsghdr *nlh,
5545 				       struct nlattr **tb,
5546 				       struct netlink_ext_ack *extack)
5547 {
5548 	struct ifaddrmsg *ifm;
5549 	int i, err;
5550 
5551 	ifm = nlmsg_payload(nlh, sizeof(*ifm));
5552 	if (!ifm) {
5553 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
5554 		return -EINVAL;
5555 	}
5556 
5557 	if (!netlink_strict_get_check(skb))
5558 		return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
5559 					      ifa_ipv6_policy, extack);
5560 
5561 	if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5562 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
5563 		return -EINVAL;
5564 	}
5565 
5566 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5567 					    ifa_ipv6_policy, extack);
5568 	if (err)
5569 		return err;
5570 
5571 	for (i = 0; i <= IFA_MAX; i++) {
5572 		if (!tb[i])
5573 			continue;
5574 
5575 		switch (i) {
5576 		case IFA_TARGET_NETNSID:
5577 		case IFA_ADDRESS:
5578 		case IFA_LOCAL:
5579 			break;
5580 		default:
5581 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
5582 			return -EINVAL;
5583 		}
5584 	}
5585 
5586 	return 0;
5587 }
5588 
5589 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5590 			     struct netlink_ext_ack *extack)
5591 {
5592 	struct net *tgt_net = sock_net(in_skb->sk);
5593 	struct inet6_fill_args fillargs = {
5594 		.portid = NETLINK_CB(in_skb).portid,
5595 		.seq = nlh->nlmsg_seq,
5596 		.event = RTM_NEWADDR,
5597 		.flags = 0,
5598 		.netnsid = -1,
5599 		.force_rt_scope_universe = false,
5600 	};
5601 	struct ifaddrmsg *ifm;
5602 	struct nlattr *tb[IFA_MAX+1];
5603 	struct in6_addr *addr = NULL, *peer;
5604 	struct net_device *dev = NULL;
5605 	struct inet6_ifaddr *ifa;
5606 	struct sk_buff *skb;
5607 	int err;
5608 
5609 	err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
5610 	if (err < 0)
5611 		return err;
5612 
5613 	if (tb[IFA_TARGET_NETNSID]) {
5614 		fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
5615 
5616 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
5617 						  fillargs.netnsid);
5618 		if (IS_ERR(tgt_net))
5619 			return PTR_ERR(tgt_net);
5620 	}
5621 
5622 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
5623 	if (!addr) {
5624 		err = -EINVAL;
5625 		goto errout;
5626 	}
5627 	ifm = nlmsg_data(nlh);
5628 	if (ifm->ifa_index)
5629 		dev = dev_get_by_index(tgt_net, ifm->ifa_index);
5630 
5631 	ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
5632 	if (!ifa) {
5633 		err = -EADDRNOTAVAIL;
5634 		goto errout;
5635 	}
5636 
5637 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5638 	if (!skb) {
5639 		err = -ENOBUFS;
5640 		goto errout_ifa;
5641 	}
5642 
5643 	err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5644 	if (err < 0) {
5645 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5646 		WARN_ON(err == -EMSGSIZE);
5647 		kfree_skb(skb);
5648 		goto errout_ifa;
5649 	}
5650 	err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
5651 errout_ifa:
5652 	in6_ifa_put(ifa);
5653 errout:
5654 	dev_put(dev);
5655 	if (fillargs.netnsid >= 0)
5656 		put_net(tgt_net);
5657 
5658 	return err;
5659 }
5660 
5661 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
5662 {
5663 	struct sk_buff *skb;
5664 	struct net *net = dev_net(ifa->idev->dev);
5665 	struct inet6_fill_args fillargs = {
5666 		.portid = 0,
5667 		.seq = 0,
5668 		.event = event,
5669 		.flags = 0,
5670 		.netnsid = -1,
5671 		.force_rt_scope_universe = false,
5672 	};
5673 	int err = -ENOBUFS;
5674 
5675 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
5676 	if (!skb)
5677 		goto errout;
5678 
5679 	err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5680 	if (err < 0) {
5681 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5682 		WARN_ON(err == -EMSGSIZE);
5683 		kfree_skb(skb);
5684 		goto errout;
5685 	}
5686 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
5687 	return;
5688 errout:
5689 	rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
5690 }
5691 
5692 static void ipv6_store_devconf(const struct ipv6_devconf *cnf,
5693 			       __s32 *array, int bytes)
5694 {
5695 	BUG_ON(bytes < (DEVCONF_MAX * 4));
5696 
5697 	memset(array, 0, bytes);
5698 	array[DEVCONF_FORWARDING] = READ_ONCE(cnf->forwarding);
5699 	array[DEVCONF_HOPLIMIT] = READ_ONCE(cnf->hop_limit);
5700 	array[DEVCONF_MTU6] = READ_ONCE(cnf->mtu6);
5701 	array[DEVCONF_ACCEPT_RA] = READ_ONCE(cnf->accept_ra);
5702 	array[DEVCONF_ACCEPT_REDIRECTS] = READ_ONCE(cnf->accept_redirects);
5703 	array[DEVCONF_AUTOCONF] = READ_ONCE(cnf->autoconf);
5704 	array[DEVCONF_DAD_TRANSMITS] = READ_ONCE(cnf->dad_transmits);
5705 	array[DEVCONF_RTR_SOLICITS] = READ_ONCE(cnf->rtr_solicits);
5706 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5707 		jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_interval));
5708 	array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5709 		jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_max_interval));
5710 	array[DEVCONF_RTR_SOLICIT_DELAY] =
5711 		jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_delay));
5712 	array[DEVCONF_FORCE_MLD_VERSION] = READ_ONCE(cnf->force_mld_version);
5713 	array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5714 		jiffies_to_msecs(READ_ONCE(cnf->mldv1_unsolicited_report_interval));
5715 	array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5716 		jiffies_to_msecs(READ_ONCE(cnf->mldv2_unsolicited_report_interval));
5717 	array[DEVCONF_USE_TEMPADDR] = READ_ONCE(cnf->use_tempaddr);
5718 	array[DEVCONF_TEMP_VALID_LFT] = READ_ONCE(cnf->temp_valid_lft);
5719 	array[DEVCONF_TEMP_PREFERED_LFT] = READ_ONCE(cnf->temp_prefered_lft);
5720 	array[DEVCONF_REGEN_MAX_RETRY] = READ_ONCE(cnf->regen_max_retry);
5721 	array[DEVCONF_MAX_DESYNC_FACTOR] = READ_ONCE(cnf->max_desync_factor);
5722 	array[DEVCONF_MAX_ADDRESSES] = READ_ONCE(cnf->max_addresses);
5723 	array[DEVCONF_ACCEPT_RA_DEFRTR] = READ_ONCE(cnf->accept_ra_defrtr);
5724 	array[DEVCONF_RA_DEFRTR_METRIC] = READ_ONCE(cnf->ra_defrtr_metric);
5725 	array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] =
5726 		READ_ONCE(cnf->accept_ra_min_hop_limit);
5727 	array[DEVCONF_ACCEPT_RA_PINFO] = READ_ONCE(cnf->accept_ra_pinfo);
5728 #ifdef CONFIG_IPV6_ROUTER_PREF
5729 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = READ_ONCE(cnf->accept_ra_rtr_pref);
5730 	array[DEVCONF_RTR_PROBE_INTERVAL] =
5731 		jiffies_to_msecs(READ_ONCE(cnf->rtr_probe_interval));
5732 #ifdef CONFIG_IPV6_ROUTE_INFO
5733 	array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] =
5734 		READ_ONCE(cnf->accept_ra_rt_info_min_plen);
5735 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] =
5736 		READ_ONCE(cnf->accept_ra_rt_info_max_plen);
5737 #endif
5738 #endif
5739 	array[DEVCONF_PROXY_NDP] = READ_ONCE(cnf->proxy_ndp);
5740 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] =
5741 		READ_ONCE(cnf->accept_source_route);
5742 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5743 	array[DEVCONF_OPTIMISTIC_DAD] = READ_ONCE(cnf->optimistic_dad);
5744 	array[DEVCONF_USE_OPTIMISTIC] = READ_ONCE(cnf->use_optimistic);
5745 #endif
5746 #ifdef CONFIG_IPV6_MROUTE
5747 	array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding);
5748 #endif
5749 	array[DEVCONF_DISABLE_IPV6] = READ_ONCE(cnf->disable_ipv6);
5750 	array[DEVCONF_ACCEPT_DAD] = READ_ONCE(cnf->accept_dad);
5751 	array[DEVCONF_FORCE_TLLAO] = READ_ONCE(cnf->force_tllao);
5752 	array[DEVCONF_NDISC_NOTIFY] = READ_ONCE(cnf->ndisc_notify);
5753 	array[DEVCONF_SUPPRESS_FRAG_NDISC] =
5754 		READ_ONCE(cnf->suppress_frag_ndisc);
5755 	array[DEVCONF_ACCEPT_RA_FROM_LOCAL] =
5756 		READ_ONCE(cnf->accept_ra_from_local);
5757 	array[DEVCONF_ACCEPT_RA_MTU] = READ_ONCE(cnf->accept_ra_mtu);
5758 	array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] =
5759 		READ_ONCE(cnf->ignore_routes_with_linkdown);
5760 	/* we omit DEVCONF_STABLE_SECRET for now */
5761 	array[DEVCONF_USE_OIF_ADDRS_ONLY] = READ_ONCE(cnf->use_oif_addrs_only);
5762 	array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] =
5763 		READ_ONCE(cnf->drop_unicast_in_l2_multicast);
5764 	array[DEVCONF_DROP_UNSOLICITED_NA] = READ_ONCE(cnf->drop_unsolicited_na);
5765 	array[DEVCONF_KEEP_ADDR_ON_DOWN] = READ_ONCE(cnf->keep_addr_on_down);
5766 	array[DEVCONF_SEG6_ENABLED] = READ_ONCE(cnf->seg6_enabled);
5767 #ifdef CONFIG_IPV6_SEG6_HMAC
5768 	array[DEVCONF_SEG6_REQUIRE_HMAC] = READ_ONCE(cnf->seg6_require_hmac);
5769 #endif
5770 	array[DEVCONF_ENHANCED_DAD] = READ_ONCE(cnf->enhanced_dad);
5771 	array[DEVCONF_ADDR_GEN_MODE] = READ_ONCE(cnf->addr_gen_mode);
5772 	array[DEVCONF_DISABLE_POLICY] = READ_ONCE(cnf->disable_policy);
5773 	array[DEVCONF_NDISC_TCLASS] = READ_ONCE(cnf->ndisc_tclass);
5774 	array[DEVCONF_RPL_SEG_ENABLED] = READ_ONCE(cnf->rpl_seg_enabled);
5775 	array[DEVCONF_IOAM6_ENABLED] = READ_ONCE(cnf->ioam6_enabled);
5776 	array[DEVCONF_IOAM6_ID] = READ_ONCE(cnf->ioam6_id);
5777 	array[DEVCONF_IOAM6_ID_WIDE] = READ_ONCE(cnf->ioam6_id_wide);
5778 	array[DEVCONF_NDISC_EVICT_NOCARRIER] =
5779 		READ_ONCE(cnf->ndisc_evict_nocarrier);
5780 	array[DEVCONF_ACCEPT_UNTRACKED_NA] =
5781 		READ_ONCE(cnf->accept_untracked_na);
5782 	array[DEVCONF_ACCEPT_RA_MIN_LFT] = READ_ONCE(cnf->accept_ra_min_lft);
5783 	array[DEVCONF_FORCE_FORWARDING] = READ_ONCE(cnf->force_forwarding);
5784 }
5785 
5786 static inline size_t inet6_ifla6_size(void)
5787 {
5788 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
5789 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
5790 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5791 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5792 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5793 	     + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5794 	     + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5795 	     + nla_total_size(4) /* IFLA_INET6_RA_MTU */
5796 	     + 0;
5797 }
5798 
5799 static inline size_t inet6_if_nlmsg_size(void)
5800 {
5801 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5802 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5803 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5804 	       + nla_total_size(4) /* IFLA_MTU */
5805 	       + nla_total_size(4) /* IFLA_LINK */
5806 	       + nla_total_size(1) /* IFLA_OPERSTATE */
5807 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5808 }
5809 
5810 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5811 					int bytes)
5812 {
5813 	int i;
5814 	int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
5815 	BUG_ON(pad < 0);
5816 
5817 	/* Use put_unaligned() because stats may not be aligned for u64. */
5818 	put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5819 	for (i = 1; i < ICMP6_MIB_MAX; i++)
5820 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5821 
5822 	memset(&stats[ICMP6_MIB_MAX], 0, pad);
5823 }
5824 
5825 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5826 					int bytes, size_t syncpoff)
5827 {
5828 	int i, c;
5829 	u64 buff[IPSTATS_MIB_MAX];
5830 	int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5831 
5832 	BUG_ON(pad < 0);
5833 
5834 	memset(buff, 0, sizeof(buff));
5835 	buff[0] = IPSTATS_MIB_MAX;
5836 
5837 	for_each_possible_cpu(c) {
5838 		for (i = 1; i < IPSTATS_MIB_MAX; i++)
5839 			buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5840 	}
5841 
5842 	memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5843 	memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5844 }
5845 
5846 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5847 			     int bytes)
5848 {
5849 	switch (attrtype) {
5850 	case IFLA_INET6_STATS:
5851 		__snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5852 				     offsetof(struct ipstats_mib, syncp));
5853 		break;
5854 	case IFLA_INET6_ICMP6STATS:
5855 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5856 		break;
5857 	}
5858 }
5859 
5860 static int inet6_fill_ifla6_stats_attrs(struct sk_buff *skb,
5861 					struct inet6_dev *idev)
5862 {
5863 	struct nlattr *nla;
5864 
5865 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5866 	if (!nla)
5867 		goto nla_put_failure;
5868 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5869 
5870 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5871 	if (!nla)
5872 		goto nla_put_failure;
5873 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5874 
5875 	return 0;
5876 
5877 nla_put_failure:
5878 	return -EMSGSIZE;
5879 }
5880 
5881 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5882 				  u32 ext_filter_mask)
5883 {
5884 	struct ifla_cacheinfo ci;
5885 	struct nlattr *nla;
5886 	u32 ra_mtu;
5887 
5888 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, READ_ONCE(idev->if_flags)))
5889 		goto nla_put_failure;
5890 	ci.max_reasm_len = IPV6_MAXPLEN;
5891 	ci.tstamp = cstamp_delta(READ_ONCE(idev->tstamp));
5892 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5893 	ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5894 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5895 		goto nla_put_failure;
5896 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5897 	if (!nla)
5898 		goto nla_put_failure;
5899 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5900 
5901 	/* XXX - MC not implemented */
5902 
5903 	if (!(ext_filter_mask & RTEXT_FILTER_SKIP_STATS)) {
5904 		if (inet6_fill_ifla6_stats_attrs(skb, idev) < 0)
5905 			goto nla_put_failure;
5906 	}
5907 
5908 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5909 	if (!nla)
5910 		goto nla_put_failure;
5911 	read_lock_bh(&idev->lock);
5912 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5913 	read_unlock_bh(&idev->lock);
5914 
5915 	if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE,
5916 		       READ_ONCE(idev->cnf.addr_gen_mode)))
5917 		goto nla_put_failure;
5918 
5919 	ra_mtu = READ_ONCE(idev->ra_mtu);
5920 	if (ra_mtu && nla_put_u32(skb, IFLA_INET6_RA_MTU, ra_mtu))
5921 		goto nla_put_failure;
5922 
5923 	return 0;
5924 
5925 nla_put_failure:
5926 	return -EMSGSIZE;
5927 }
5928 
5929 static size_t inet6_get_link_af_size(const struct net_device *dev,
5930 				     u32 ext_filter_mask)
5931 {
5932 	if (!__in6_dev_get(dev))
5933 		return 0;
5934 
5935 	return inet6_ifla6_size();
5936 }
5937 
5938 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5939 			      u32 ext_filter_mask)
5940 {
5941 	struct inet6_dev *idev = __in6_dev_get(dev);
5942 
5943 	if (!idev)
5944 		return -ENODATA;
5945 
5946 	if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5947 		return -EMSGSIZE;
5948 
5949 	return 0;
5950 }
5951 
5952 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token,
5953 			     struct netlink_ext_ack *extack)
5954 {
5955 	struct inet6_ifaddr *ifp;
5956 	struct net_device *dev = idev->dev;
5957 	bool clear_token, update_rs = false;
5958 	struct in6_addr ll_addr;
5959 
5960 	ASSERT_RTNL();
5961 
5962 	if (!token)
5963 		return -EINVAL;
5964 
5965 	if (dev->flags & IFF_LOOPBACK) {
5966 		NL_SET_ERR_MSG_MOD(extack, "Device is loopback");
5967 		return -EINVAL;
5968 	}
5969 
5970 	if (dev->flags & IFF_NOARP) {
5971 		NL_SET_ERR_MSG_MOD(extack,
5972 				   "Device does not do neighbour discovery");
5973 		return -EINVAL;
5974 	}
5975 
5976 	if (!ipv6_accept_ra(idev)) {
5977 		NL_SET_ERR_MSG_MOD(extack,
5978 				   "Router advertisement is disabled on device");
5979 		return -EINVAL;
5980 	}
5981 
5982 	if (READ_ONCE(idev->cnf.rtr_solicits) == 0) {
5983 		NL_SET_ERR_MSG(extack,
5984 			       "Router solicitation is disabled on device");
5985 		return -EINVAL;
5986 	}
5987 
5988 	write_lock_bh(&idev->lock);
5989 
5990 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5991 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5992 
5993 	write_unlock_bh(&idev->lock);
5994 
5995 	clear_token = ipv6_addr_any(token);
5996 	if (clear_token)
5997 		goto update_lft;
5998 
5999 	if (!idev->dead && (idev->if_flags & IF_READY) &&
6000 	    !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
6001 			     IFA_F_OPTIMISTIC)) {
6002 		/* If we're not ready, then normal ifup will take care
6003 		 * of this. Otherwise, we need to request our rs here.
6004 		 */
6005 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
6006 		update_rs = true;
6007 	}
6008 
6009 update_lft:
6010 	write_lock_bh(&idev->lock);
6011 
6012 	if (update_rs) {
6013 		idev->if_flags |= IF_RS_SENT;
6014 		idev->rs_interval = rfc3315_s14_backoff_init(
6015 			READ_ONCE(idev->cnf.rtr_solicit_interval));
6016 		idev->rs_probes = 1;
6017 		addrconf_mod_rs_timer(idev, idev->rs_interval);
6018 	}
6019 
6020 	/* Well, that's kinda nasty ... */
6021 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
6022 		spin_lock(&ifp->lock);
6023 		if (ifp->tokenized) {
6024 			ifp->valid_lft = 0;
6025 			ifp->prefered_lft = 0;
6026 		}
6027 		spin_unlock(&ifp->lock);
6028 	}
6029 
6030 	write_unlock_bh(&idev->lock);
6031 	inet6_ifinfo_notify(RTM_NEWLINK, idev);
6032 	addrconf_verify_rtnl(dev_net(dev));
6033 	return 0;
6034 }
6035 
6036 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
6037 	[IFLA_INET6_ADDR_GEN_MODE]	= { .type = NLA_U8 },
6038 	[IFLA_INET6_TOKEN]		= { .len = sizeof(struct in6_addr) },
6039 	[IFLA_INET6_RA_MTU]		= { .type = NLA_REJECT,
6040 					    .reject_message =
6041 						"IFLA_INET6_RA_MTU can not be set" },
6042 };
6043 
6044 static int check_addr_gen_mode(int mode)
6045 {
6046 	if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
6047 	    mode != IN6_ADDR_GEN_MODE_NONE &&
6048 	    mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
6049 	    mode != IN6_ADDR_GEN_MODE_RANDOM)
6050 		return -EINVAL;
6051 	return 1;
6052 }
6053 
6054 static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
6055 				int mode)
6056 {
6057 	if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
6058 	    !idev->cnf.stable_secret.initialized &&
6059 	    !net->ipv6.devconf_dflt->stable_secret.initialized)
6060 		return -EINVAL;
6061 	return 1;
6062 }
6063 
6064 static int inet6_validate_link_af(const struct net_device *dev,
6065 				  const struct nlattr *nla,
6066 				  struct netlink_ext_ack *extack)
6067 {
6068 	struct nlattr *tb[IFLA_INET6_MAX + 1];
6069 	struct inet6_dev *idev = NULL;
6070 	int err;
6071 
6072 	if (dev) {
6073 		idev = __in6_dev_get(dev);
6074 		if (!idev)
6075 			return -EAFNOSUPPORT;
6076 	}
6077 
6078 	err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
6079 					  inet6_af_policy, extack);
6080 	if (err)
6081 		return err;
6082 
6083 	if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
6084 		return -EINVAL;
6085 
6086 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
6087 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
6088 
6089 		if (check_addr_gen_mode(mode) < 0)
6090 			return -EINVAL;
6091 		if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
6092 			return -EINVAL;
6093 	}
6094 
6095 	return 0;
6096 }
6097 
6098 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla,
6099 			     struct netlink_ext_ack *extack)
6100 {
6101 	struct inet6_dev *idev = __in6_dev_get(dev);
6102 	struct nlattr *tb[IFLA_INET6_MAX + 1];
6103 	int err;
6104 
6105 	if (!idev)
6106 		return -EAFNOSUPPORT;
6107 
6108 	if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
6109 		return -EINVAL;
6110 
6111 	if (tb[IFLA_INET6_TOKEN]) {
6112 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]),
6113 					extack);
6114 		if (err)
6115 			return err;
6116 	}
6117 
6118 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
6119 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
6120 
6121 		WRITE_ONCE(idev->cnf.addr_gen_mode, mode);
6122 	}
6123 
6124 	return 0;
6125 }
6126 
6127 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
6128 			     u32 portid, u32 seq, int event, unsigned int flags)
6129 {
6130 	struct net_device *dev = idev->dev;
6131 	struct ifinfomsg *hdr;
6132 	struct nlmsghdr *nlh;
6133 	int ifindex, iflink;
6134 	void *protoinfo;
6135 
6136 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
6137 	if (!nlh)
6138 		return -EMSGSIZE;
6139 
6140 	hdr = nlmsg_data(nlh);
6141 	hdr->ifi_family = AF_INET6;
6142 	hdr->__ifi_pad = 0;
6143 	hdr->ifi_type = dev->type;
6144 	ifindex = READ_ONCE(dev->ifindex);
6145 	hdr->ifi_index = ifindex;
6146 	hdr->ifi_flags = netif_get_flags(dev);
6147 	hdr->ifi_change = 0;
6148 
6149 	iflink = dev_get_iflink(dev);
6150 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
6151 	    (dev->addr_len &&
6152 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
6153 	    nla_put_u32(skb, IFLA_MTU, READ_ONCE(dev->mtu)) ||
6154 	    (ifindex != iflink &&
6155 	     nla_put_u32(skb, IFLA_LINK, iflink)) ||
6156 	    nla_put_u8(skb, IFLA_OPERSTATE,
6157 		       netif_running(dev) ? READ_ONCE(dev->operstate) : IF_OPER_DOWN))
6158 		goto nla_put_failure;
6159 	protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
6160 	if (!protoinfo)
6161 		goto nla_put_failure;
6162 
6163 	if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
6164 		goto nla_put_failure;
6165 
6166 	nla_nest_end(skb, protoinfo);
6167 	nlmsg_end(skb, nlh);
6168 	return 0;
6169 
6170 nla_put_failure:
6171 	nlmsg_cancel(skb, nlh);
6172 	return -EMSGSIZE;
6173 }
6174 
6175 static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
6176 				   struct netlink_ext_ack *extack)
6177 {
6178 	struct ifinfomsg *ifm;
6179 
6180 	ifm = nlmsg_payload(nlh, sizeof(*ifm));
6181 	if (!ifm) {
6182 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
6183 		return -EINVAL;
6184 	}
6185 
6186 	if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
6187 		NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
6188 		return -EINVAL;
6189 	}
6190 
6191 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
6192 	    ifm->ifi_change || ifm->ifi_index) {
6193 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
6194 		return -EINVAL;
6195 	}
6196 
6197 	return 0;
6198 }
6199 
6200 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
6201 {
6202 	struct net *net = sock_net(skb->sk);
6203 	struct {
6204 		unsigned long ifindex;
6205 	} *ctx = (void *)cb->ctx;
6206 	struct net_device *dev;
6207 	struct inet6_dev *idev;
6208 	int err;
6209 
6210 	/* only requests using strict checking can pass data to
6211 	 * influence the dump
6212 	 */
6213 	if (cb->strict_check) {
6214 		err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
6215 
6216 		if (err < 0)
6217 			return err;
6218 	}
6219 
6220 	err = 0;
6221 	rcu_read_lock();
6222 	for_each_netdev_dump(net, dev, ctx->ifindex) {
6223 		idev = __in6_dev_get(dev);
6224 		if (!idev)
6225 			continue;
6226 		err = inet6_fill_ifinfo(skb, idev,
6227 					NETLINK_CB(cb->skb).portid,
6228 					cb->nlh->nlmsg_seq,
6229 					RTM_NEWLINK, NLM_F_MULTI);
6230 		if (err < 0)
6231 			break;
6232 	}
6233 	rcu_read_unlock();
6234 
6235 	return err;
6236 }
6237 
6238 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
6239 {
6240 	struct sk_buff *skb;
6241 	struct net *net = dev_net(idev->dev);
6242 	int err = -ENOBUFS;
6243 
6244 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
6245 	if (!skb)
6246 		goto errout;
6247 
6248 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
6249 	if (err < 0) {
6250 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
6251 		WARN_ON(err == -EMSGSIZE);
6252 		kfree_skb(skb);
6253 		goto errout;
6254 	}
6255 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
6256 	return;
6257 errout:
6258 	rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
6259 }
6260 
6261 static inline size_t inet6_prefix_nlmsg_size(void)
6262 {
6263 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
6264 	       + nla_total_size(sizeof(struct in6_addr))
6265 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
6266 }
6267 
6268 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
6269 			     struct prefix_info *pinfo, u32 portid, u32 seq,
6270 			     int event, unsigned int flags)
6271 {
6272 	struct prefixmsg *pmsg;
6273 	struct nlmsghdr *nlh;
6274 	struct prefix_cacheinfo	ci;
6275 
6276 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
6277 	if (!nlh)
6278 		return -EMSGSIZE;
6279 
6280 	pmsg = nlmsg_data(nlh);
6281 	pmsg->prefix_family = AF_INET6;
6282 	pmsg->prefix_pad1 = 0;
6283 	pmsg->prefix_pad2 = 0;
6284 	pmsg->prefix_ifindex = idev->dev->ifindex;
6285 	pmsg->prefix_len = pinfo->prefix_len;
6286 	pmsg->prefix_type = pinfo->type;
6287 	pmsg->prefix_pad3 = 0;
6288 	pmsg->prefix_flags = pinfo->flags;
6289 
6290 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
6291 		goto nla_put_failure;
6292 	ci.preferred_time = ntohl(pinfo->prefered);
6293 	ci.valid_time = ntohl(pinfo->valid);
6294 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
6295 		goto nla_put_failure;
6296 	nlmsg_end(skb, nlh);
6297 	return 0;
6298 
6299 nla_put_failure:
6300 	nlmsg_cancel(skb, nlh);
6301 	return -EMSGSIZE;
6302 }
6303 
6304 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
6305 			 struct prefix_info *pinfo)
6306 {
6307 	struct sk_buff *skb;
6308 	struct net *net = dev_net(idev->dev);
6309 	int err = -ENOBUFS;
6310 
6311 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
6312 	if (!skb)
6313 		goto errout;
6314 
6315 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
6316 	if (err < 0) {
6317 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
6318 		WARN_ON(err == -EMSGSIZE);
6319 		kfree_skb(skb);
6320 		goto errout;
6321 	}
6322 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
6323 	return;
6324 errout:
6325 	rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
6326 }
6327 
6328 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6329 {
6330 	struct net *net = dev_net(ifp->idev->dev);
6331 
6332 	if (event)
6333 		ASSERT_RTNL();
6334 
6335 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
6336 
6337 	switch (event) {
6338 	case RTM_NEWADDR:
6339 		/*
6340 		 * If the address was optimistic we inserted the route at the
6341 		 * start of our DAD process, so we don't need to do it again.
6342 		 * If the device was taken down in the middle of the DAD
6343 		 * cycle there is a race where we could get here without a
6344 		 * host route, so nothing to insert. That will be fixed when
6345 		 * the device is brought up.
6346 		 */
6347 		if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) {
6348 			ip6_ins_rt(net, ifp->rt);
6349 		} else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
6350 			pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
6351 				&ifp->addr, ifp->idev->dev->name);
6352 		}
6353 
6354 		if (ifp->idev->cnf.forwarding)
6355 			addrconf_join_anycast(ifp);
6356 		if (!ipv6_addr_any(&ifp->peer_addr))
6357 			addrconf_prefix_route(&ifp->peer_addr, 128,
6358 					      ifp->rt_priority, ifp->idev->dev,
6359 					      0, 0, GFP_ATOMIC);
6360 		break;
6361 	case RTM_DELADDR:
6362 		if (ifp->idev->cnf.forwarding)
6363 			addrconf_leave_anycast(ifp);
6364 		addrconf_leave_solict(ifp->idev, &ifp->addr);
6365 		if (!ipv6_addr_any(&ifp->peer_addr)) {
6366 			struct fib6_info *rt;
6367 
6368 			rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
6369 						       ifp->idev->dev, 0, 0,
6370 						       false);
6371 			if (rt)
6372 				ip6_del_rt(net, rt, false);
6373 		}
6374 		if (ifp->rt) {
6375 			ip6_del_rt(net, ifp->rt, false);
6376 			ifp->rt = NULL;
6377 		}
6378 		rt_genid_bump_ipv6(net);
6379 		break;
6380 	}
6381 	atomic_inc(&net->ipv6.dev_addr_genid);
6382 }
6383 
6384 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6385 {
6386 	if (likely(ifp->idev->dead == 0))
6387 		__ipv6_ifa_notify(event, ifp);
6388 }
6389 
6390 #ifdef CONFIG_SYSCTL
6391 
6392 static int addrconf_sysctl_forward(const struct ctl_table *ctl, int write,
6393 		void *buffer, size_t *lenp, loff_t *ppos)
6394 {
6395 	struct ctl_table lctl;
6396 	int *valp = ctl->data;
6397 	int val = *valp;
6398 	int ret;
6399 
6400 	/*
6401 	 * ctl->data points to idev->cnf.forwarding, we should
6402 	 * not modify it until we get the rtnl lock.
6403 	 */
6404 	lctl = *ctl;
6405 	lctl.data = &val;
6406 
6407 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6408 	if (ret)
6409 		return ret;
6410 
6411 	if (write)
6412 		ret = addrconf_fixup_forwarding(ctl, valp, val);
6413 	return ret;
6414 }
6415 
6416 static int addrconf_sysctl_mtu(const struct ctl_table *ctl, int write,
6417 		void *buffer, size_t *lenp, loff_t *ppos)
6418 {
6419 	struct inet6_dev *idev = ctl->extra1;
6420 	int min_mtu = IPV6_MIN_MTU;
6421 	struct ctl_table lctl;
6422 
6423 	lctl = *ctl;
6424 	lctl.extra1 = &min_mtu;
6425 	lctl.extra2 = idev ? &idev->dev->mtu : NULL;
6426 
6427 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
6428 }
6429 
6430 static void dev_disable_change(struct inet6_dev *idev)
6431 {
6432 	struct netdev_notifier_info info;
6433 
6434 	if (!idev || !idev->dev)
6435 		return;
6436 
6437 	netdev_notifier_info_init(&info, idev->dev);
6438 	if (idev->cnf.disable_ipv6)
6439 		addrconf_notify(NULL, NETDEV_DOWN, &info);
6440 	else
6441 		addrconf_notify(NULL, NETDEV_UP, &info);
6442 }
6443 
6444 static void addrconf_disable_change(struct net *net, __s32 newf)
6445 {
6446 	struct net_device *dev;
6447 	struct inet6_dev *idev;
6448 
6449 	for_each_netdev(net, dev) {
6450 		idev = __in6_dev_get_rtnl_net(dev);
6451 		if (idev) {
6452 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
6453 
6454 			WRITE_ONCE(idev->cnf.disable_ipv6, newf);
6455 			if (changed)
6456 				dev_disable_change(idev);
6457 		}
6458 	}
6459 }
6460 
6461 static int addrconf_disable_ipv6(const struct ctl_table *table, int *p, int newf)
6462 {
6463 	struct net *net = (struct net *)table->extra2;
6464 	int old;
6465 
6466 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6467 		WRITE_ONCE(*p, newf);
6468 		return 0;
6469 	}
6470 
6471 	if (!rtnl_net_trylock(net))
6472 		return restart_syscall();
6473 
6474 	old = *p;
6475 	WRITE_ONCE(*p, newf);
6476 
6477 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
6478 		WRITE_ONCE(net->ipv6.devconf_dflt->disable_ipv6, newf);
6479 		addrconf_disable_change(net, newf);
6480 	} else if ((!newf) ^ (!old)) {
6481 		dev_disable_change((struct inet6_dev *)table->extra1);
6482 	}
6483 
6484 	rtnl_net_unlock(net);
6485 	return 0;
6486 }
6487 
6488 static int addrconf_sysctl_disable(const struct ctl_table *ctl, int write,
6489 		void *buffer, size_t *lenp, loff_t *ppos)
6490 {
6491 	struct ctl_table lctl;
6492 	int *valp = ctl->data;
6493 	int val = *valp;
6494 	int ret;
6495 
6496 	/*
6497 	 * ctl->data points to idev->cnf.disable_ipv6, we should
6498 	 * not modify it until we get the rtnl lock.
6499 	 */
6500 	lctl = *ctl;
6501 	lctl.data = &val;
6502 
6503 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6504 	if (ret)
6505 		return ret;
6506 
6507 	if (write)
6508 		ret = addrconf_disable_ipv6(ctl, valp, val);
6509 	return ret;
6510 }
6511 
6512 static int addrconf_sysctl_proxy_ndp(const struct ctl_table *ctl, int write,
6513 		void *buffer, size_t *lenp, loff_t *ppos)
6514 {
6515 	struct net *net = ctl->extra2;
6516 	int *valp = ctl->data;
6517 	int old, new;
6518 	int ret;
6519 
6520 	if (write && !rtnl_net_trylock(net))
6521 		return restart_syscall();
6522 
6523 	old = *valp;
6524 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6525 	new = *valp;
6526 
6527 	if (write && old != new) {
6528 		if (valp == &net->ipv6.devconf_dflt->proxy_ndp) {
6529 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6530 						     NETCONFA_PROXY_NEIGH,
6531 						     NETCONFA_IFINDEX_DEFAULT,
6532 						     net->ipv6.devconf_dflt);
6533 		} else if (valp == &net->ipv6.devconf_all->proxy_ndp) {
6534 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6535 						     NETCONFA_PROXY_NEIGH,
6536 						     NETCONFA_IFINDEX_ALL,
6537 						     net->ipv6.devconf_all);
6538 		} else {
6539 			struct inet6_dev *idev = ctl->extra1;
6540 
6541 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6542 						     NETCONFA_PROXY_NEIGH,
6543 						     idev->dev->ifindex,
6544 						     &idev->cnf);
6545 		}
6546 	}
6547 	if (write)
6548 		rtnl_net_unlock(net);
6549 
6550 	return ret;
6551 }
6552 
6553 static int addrconf_sysctl_addr_gen_mode(const struct ctl_table *ctl, int write,
6554 					 void *buffer, size_t *lenp,
6555 					 loff_t *ppos)
6556 {
6557 	int ret = 0;
6558 	u32 new_val;
6559 	struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6560 	struct net *net = (struct net *)ctl->extra2;
6561 	struct ctl_table tmp = {
6562 		.data = &new_val,
6563 		.maxlen = sizeof(new_val),
6564 		.mode = ctl->mode,
6565 	};
6566 
6567 	if (!rtnl_net_trylock(net))
6568 		return restart_syscall();
6569 
6570 	new_val = *((u32 *)ctl->data);
6571 
6572 	ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6573 	if (ret != 0)
6574 		goto out;
6575 
6576 	if (write) {
6577 		if (check_addr_gen_mode(new_val) < 0) {
6578 			ret = -EINVAL;
6579 			goto out;
6580 		}
6581 
6582 		if (idev) {
6583 			if (check_stable_privacy(idev, net, new_val) < 0) {
6584 				ret = -EINVAL;
6585 				goto out;
6586 			}
6587 
6588 			if (idev->cnf.addr_gen_mode != new_val) {
6589 				WRITE_ONCE(idev->cnf.addr_gen_mode, new_val);
6590 				netdev_lock_ops(idev->dev);
6591 				addrconf_init_auto_addrs(idev->dev);
6592 				netdev_unlock_ops(idev->dev);
6593 			}
6594 		} else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6595 			struct net_device *dev;
6596 
6597 			WRITE_ONCE(net->ipv6.devconf_dflt->addr_gen_mode, new_val);
6598 			for_each_netdev(net, dev) {
6599 				idev = __in6_dev_get_rtnl_net(dev);
6600 				if (idev &&
6601 				    idev->cnf.addr_gen_mode != new_val) {
6602 					WRITE_ONCE(idev->cnf.addr_gen_mode,
6603 						  new_val);
6604 					netdev_lock_ops(idev->dev);
6605 					addrconf_init_auto_addrs(idev->dev);
6606 					netdev_unlock_ops(idev->dev);
6607 				}
6608 			}
6609 		}
6610 
6611 		WRITE_ONCE(*((u32 *)ctl->data), new_val);
6612 	}
6613 
6614 out:
6615 	rtnl_net_unlock(net);
6616 
6617 	return ret;
6618 }
6619 
6620 static int addrconf_sysctl_stable_secret(const struct ctl_table *ctl, int write,
6621 					 void *buffer, size_t *lenp,
6622 					 loff_t *ppos)
6623 {
6624 	int err;
6625 	struct in6_addr addr;
6626 	char str[IPV6_MAX_STRLEN];
6627 	struct ctl_table lctl = *ctl;
6628 	struct net *net = ctl->extra2;
6629 	struct ipv6_stable_secret *secret = ctl->data;
6630 
6631 	if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6632 		return -EIO;
6633 
6634 	lctl.maxlen = IPV6_MAX_STRLEN;
6635 	lctl.data = str;
6636 
6637 	if (!rtnl_net_trylock(net))
6638 		return restart_syscall();
6639 
6640 	if (!write && !secret->initialized) {
6641 		err = -EIO;
6642 		goto out;
6643 	}
6644 
6645 	err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
6646 	if (err >= sizeof(str)) {
6647 		err = -EIO;
6648 		goto out;
6649 	}
6650 
6651 	err = proc_dostring(&lctl, write, buffer, lenp, ppos);
6652 	if (err || !write)
6653 		goto out;
6654 
6655 	if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
6656 		err = -EIO;
6657 		goto out;
6658 	}
6659 
6660 	secret->initialized = true;
6661 	secret->secret = addr;
6662 
6663 	if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6664 		struct net_device *dev;
6665 
6666 		for_each_netdev(net, dev) {
6667 			struct inet6_dev *idev = __in6_dev_get_rtnl_net(dev);
6668 
6669 			if (idev) {
6670 				WRITE_ONCE(idev->cnf.addr_gen_mode,
6671 					   IN6_ADDR_GEN_MODE_STABLE_PRIVACY);
6672 			}
6673 		}
6674 	} else {
6675 		struct inet6_dev *idev = ctl->extra1;
6676 
6677 		WRITE_ONCE(idev->cnf.addr_gen_mode,
6678 			   IN6_ADDR_GEN_MODE_STABLE_PRIVACY);
6679 	}
6680 
6681 out:
6682 	rtnl_net_unlock(net);
6683 
6684 	return err;
6685 }
6686 
6687 static
6688 int addrconf_sysctl_ignore_routes_with_linkdown(const struct ctl_table *ctl,
6689 						int write, void *buffer,
6690 						size_t *lenp,
6691 						loff_t *ppos)
6692 {
6693 	struct ctl_table lctl;
6694 	int *valp = ctl->data;
6695 	int val = *valp;
6696 	int ret;
6697 
6698 	/* ctl->data points to idev->cnf.ignore_routes_when_linkdown
6699 	 * we should not modify it until we get the rtnl lock.
6700 	 */
6701 	lctl = *ctl;
6702 	lctl.data = &val;
6703 
6704 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6705 	if (ret)
6706 		return ret;
6707 
6708 	if (write)
6709 		ret = addrconf_fixup_linkdown(ctl, valp, val);
6710 	return ret;
6711 }
6712 
6713 static
6714 void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6715 {
6716 	if (rt) {
6717 		if (action)
6718 			rt->dst.flags |= DST_NOPOLICY;
6719 		else
6720 			rt->dst.flags &= ~DST_NOPOLICY;
6721 	}
6722 }
6723 
6724 static
6725 void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6726 {
6727 	struct inet6_ifaddr *ifa;
6728 
6729 	read_lock_bh(&idev->lock);
6730 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
6731 		spin_lock(&ifa->lock);
6732 		if (ifa->rt) {
6733 			/* host routes only use builtin fib6_nh */
6734 			struct fib6_nh *nh = ifa->rt->fib6_nh;
6735 			int cpu;
6736 
6737 			rcu_read_lock();
6738 			ifa->rt->dst_nopolicy = val ? true : false;
6739 			if (nh->rt6i_pcpu) {
6740 				for_each_possible_cpu(cpu) {
6741 					struct rt6_info **rtp;
6742 
6743 					rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
6744 					addrconf_set_nopolicy(*rtp, val);
6745 				}
6746 			}
6747 			rcu_read_unlock();
6748 		}
6749 		spin_unlock(&ifa->lock);
6750 	}
6751 	read_unlock_bh(&idev->lock);
6752 }
6753 
6754 static
6755 int addrconf_disable_policy(const struct ctl_table *ctl, int *valp, int val)
6756 {
6757 	struct net *net = (struct net *)ctl->extra2;
6758 	struct inet6_dev *idev;
6759 
6760 	if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6761 		WRITE_ONCE(*valp, val);
6762 		return 0;
6763 	}
6764 
6765 	if (!rtnl_net_trylock(net))
6766 		return restart_syscall();
6767 
6768 	WRITE_ONCE(*valp, val);
6769 
6770 	if (valp == &net->ipv6.devconf_all->disable_policy)  {
6771 		struct net_device *dev;
6772 
6773 		for_each_netdev(net, dev) {
6774 			idev = __in6_dev_get_rtnl_net(dev);
6775 			if (idev)
6776 				addrconf_disable_policy_idev(idev, val);
6777 		}
6778 	} else {
6779 		idev = (struct inet6_dev *)ctl->extra1;
6780 		addrconf_disable_policy_idev(idev, val);
6781 	}
6782 
6783 	rtnl_net_unlock(net);
6784 	return 0;
6785 }
6786 
6787 static int addrconf_sysctl_disable_policy(const struct ctl_table *ctl, int write,
6788 				   void *buffer, size_t *lenp, loff_t *ppos)
6789 {
6790 	struct ctl_table lctl;
6791 	int *valp = ctl->data;
6792 	int val = *valp;
6793 	int ret;
6794 
6795 	lctl = *ctl;
6796 	lctl.data = &val;
6797 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6798 	if (ret)
6799 		return ret;
6800 
6801 	if (write && (*valp != val))
6802 		ret = addrconf_disable_policy(ctl, valp, val);
6803 
6804 	return ret;
6805 }
6806 
6807 static void addrconf_force_forward_change(struct net *net, __s32 newf)
6808 {
6809 	struct net_device *dev;
6810 	struct inet6_dev *idev;
6811 
6812 	for_each_netdev(net, dev) {
6813 		idev = __in6_dev_get_rtnl_net(dev);
6814 		if (idev) {
6815 			int changed = (!idev->cnf.force_forwarding) ^ (!newf);
6816 
6817 			WRITE_ONCE(idev->cnf.force_forwarding, newf);
6818 			if (changed)
6819 				inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
6820 							     NETCONFA_FORCE_FORWARDING,
6821 							     dev->ifindex, &idev->cnf);
6822 		}
6823 	}
6824 }
6825 
6826 static int addrconf_sysctl_force_forwarding(const struct ctl_table *ctl, int write,
6827 					    void *buffer, size_t *lenp, loff_t *ppos)
6828 {
6829 	struct inet6_dev *idev = ctl->extra1;
6830 	struct ctl_table tmp_ctl = *ctl;
6831 	struct net *net = ctl->extra2;
6832 	int *valp = ctl->data;
6833 	int new_val = *valp;
6834 	int old_val = *valp;
6835 	int ret;
6836 
6837 	tmp_ctl.extra1 = SYSCTL_ZERO;
6838 	tmp_ctl.extra2 = SYSCTL_ONE;
6839 	tmp_ctl.data = &new_val;
6840 
6841 	ret = proc_douintvec_minmax(&tmp_ctl, write, buffer, lenp, ppos);
6842 
6843 	if (write && old_val != new_val) {
6844 		if (!rtnl_net_trylock(net))
6845 			return restart_syscall();
6846 
6847 		WRITE_ONCE(*valp, new_val);
6848 
6849 		if (valp == &net->ipv6.devconf_dflt->force_forwarding) {
6850 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6851 						     NETCONFA_FORCE_FORWARDING,
6852 						     NETCONFA_IFINDEX_DEFAULT,
6853 						     net->ipv6.devconf_dflt);
6854 		} else if (valp == &net->ipv6.devconf_all->force_forwarding) {
6855 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6856 						     NETCONFA_FORCE_FORWARDING,
6857 						     NETCONFA_IFINDEX_ALL,
6858 						     net->ipv6.devconf_all);
6859 
6860 			addrconf_force_forward_change(net, new_val);
6861 		} else {
6862 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6863 						     NETCONFA_FORCE_FORWARDING,
6864 						     idev->dev->ifindex,
6865 						     &idev->cnf);
6866 		}
6867 		rtnl_net_unlock(net);
6868 	}
6869 
6870 	return ret;
6871 }
6872 
6873 static int minus_one = -1;
6874 static const int two_five_five = 255;
6875 static u32 ioam6_if_id_max = U16_MAX;
6876 
6877 static const struct ctl_table addrconf_sysctl[] = {
6878 	{
6879 		.procname	= "forwarding",
6880 		.data		= &ipv6_devconf.forwarding,
6881 		.maxlen		= sizeof(int),
6882 		.mode		= 0644,
6883 		.proc_handler	= addrconf_sysctl_forward,
6884 	},
6885 	{
6886 		.procname	= "hop_limit",
6887 		.data		= &ipv6_devconf.hop_limit,
6888 		.maxlen		= sizeof(int),
6889 		.mode		= 0644,
6890 		.proc_handler	= proc_dointvec_minmax,
6891 		.extra1		= (void *)SYSCTL_ONE,
6892 		.extra2		= (void *)&two_five_five,
6893 	},
6894 	{
6895 		.procname	= "mtu",
6896 		.data		= &ipv6_devconf.mtu6,
6897 		.maxlen		= sizeof(int),
6898 		.mode		= 0644,
6899 		.proc_handler	= addrconf_sysctl_mtu,
6900 	},
6901 	{
6902 		.procname	= "accept_ra",
6903 		.data		= &ipv6_devconf.accept_ra,
6904 		.maxlen		= sizeof(int),
6905 		.mode		= 0644,
6906 		.proc_handler	= proc_dointvec,
6907 	},
6908 	{
6909 		.procname	= "accept_redirects",
6910 		.data		= &ipv6_devconf.accept_redirects,
6911 		.maxlen		= sizeof(int),
6912 		.mode		= 0644,
6913 		.proc_handler	= proc_dointvec,
6914 	},
6915 	{
6916 		.procname	= "autoconf",
6917 		.data		= &ipv6_devconf.autoconf,
6918 		.maxlen		= sizeof(int),
6919 		.mode		= 0644,
6920 		.proc_handler	= proc_dointvec,
6921 	},
6922 	{
6923 		.procname	= "dad_transmits",
6924 		.data		= &ipv6_devconf.dad_transmits,
6925 		.maxlen		= sizeof(int),
6926 		.mode		= 0644,
6927 		.proc_handler	= proc_dointvec,
6928 	},
6929 	{
6930 		.procname	= "router_solicitations",
6931 		.data		= &ipv6_devconf.rtr_solicits,
6932 		.maxlen		= sizeof(int),
6933 		.mode		= 0644,
6934 		.proc_handler	= proc_dointvec_minmax,
6935 		.extra1		= &minus_one,
6936 	},
6937 	{
6938 		.procname	= "router_solicitation_interval",
6939 		.data		= &ipv6_devconf.rtr_solicit_interval,
6940 		.maxlen		= sizeof(int),
6941 		.mode		= 0644,
6942 		.proc_handler	= proc_dointvec_jiffies,
6943 	},
6944 	{
6945 		.procname	= "router_solicitation_max_interval",
6946 		.data		= &ipv6_devconf.rtr_solicit_max_interval,
6947 		.maxlen		= sizeof(int),
6948 		.mode		= 0644,
6949 		.proc_handler	= proc_dointvec_jiffies,
6950 	},
6951 	{
6952 		.procname	= "router_solicitation_delay",
6953 		.data		= &ipv6_devconf.rtr_solicit_delay,
6954 		.maxlen		= sizeof(int),
6955 		.mode		= 0644,
6956 		.proc_handler	= proc_dointvec_jiffies,
6957 	},
6958 	{
6959 		.procname	= "force_mld_version",
6960 		.data		= &ipv6_devconf.force_mld_version,
6961 		.maxlen		= sizeof(int),
6962 		.mode		= 0644,
6963 		.proc_handler	= proc_dointvec,
6964 	},
6965 	{
6966 		.procname	= "mldv1_unsolicited_report_interval",
6967 		.data		=
6968 			&ipv6_devconf.mldv1_unsolicited_report_interval,
6969 		.maxlen		= sizeof(int),
6970 		.mode		= 0644,
6971 		.proc_handler	= proc_dointvec_ms_jiffies,
6972 	},
6973 	{
6974 		.procname	= "mldv2_unsolicited_report_interval",
6975 		.data		=
6976 			&ipv6_devconf.mldv2_unsolicited_report_interval,
6977 		.maxlen		= sizeof(int),
6978 		.mode		= 0644,
6979 		.proc_handler	= proc_dointvec_ms_jiffies,
6980 	},
6981 	{
6982 		.procname	= "use_tempaddr",
6983 		.data		= &ipv6_devconf.use_tempaddr,
6984 		.maxlen		= sizeof(int),
6985 		.mode		= 0644,
6986 		.proc_handler	= proc_dointvec,
6987 	},
6988 	{
6989 		.procname	= "temp_valid_lft",
6990 		.data		= &ipv6_devconf.temp_valid_lft,
6991 		.maxlen		= sizeof(int),
6992 		.mode		= 0644,
6993 		.proc_handler	= proc_dointvec,
6994 	},
6995 	{
6996 		.procname	= "temp_prefered_lft",
6997 		.data		= &ipv6_devconf.temp_prefered_lft,
6998 		.maxlen		= sizeof(int),
6999 		.mode		= 0644,
7000 		.proc_handler	= proc_dointvec,
7001 	},
7002 	{
7003 		.procname       = "regen_min_advance",
7004 		.data           = &ipv6_devconf.regen_min_advance,
7005 		.maxlen         = sizeof(int),
7006 		.mode           = 0644,
7007 		.proc_handler   = proc_dointvec,
7008 	},
7009 	{
7010 		.procname	= "regen_max_retry",
7011 		.data		= &ipv6_devconf.regen_max_retry,
7012 		.maxlen		= sizeof(int),
7013 		.mode		= 0644,
7014 		.proc_handler	= proc_dointvec,
7015 	},
7016 	{
7017 		.procname	= "max_desync_factor",
7018 		.data		= &ipv6_devconf.max_desync_factor,
7019 		.maxlen		= sizeof(int),
7020 		.mode		= 0644,
7021 		.proc_handler	= proc_dointvec,
7022 	},
7023 	{
7024 		.procname	= "max_addresses",
7025 		.data		= &ipv6_devconf.max_addresses,
7026 		.maxlen		= sizeof(int),
7027 		.mode		= 0644,
7028 		.proc_handler	= proc_dointvec,
7029 	},
7030 	{
7031 		.procname	= "accept_ra_defrtr",
7032 		.data		= &ipv6_devconf.accept_ra_defrtr,
7033 		.maxlen		= sizeof(int),
7034 		.mode		= 0644,
7035 		.proc_handler	= proc_dointvec,
7036 	},
7037 	{
7038 		.procname	= "ra_defrtr_metric",
7039 		.data		= &ipv6_devconf.ra_defrtr_metric,
7040 		.maxlen		= sizeof(u32),
7041 		.mode		= 0644,
7042 		.proc_handler	= proc_douintvec_minmax,
7043 		.extra1		= (void *)SYSCTL_ONE,
7044 	},
7045 	{
7046 		.procname	= "accept_ra_min_hop_limit",
7047 		.data		= &ipv6_devconf.accept_ra_min_hop_limit,
7048 		.maxlen		= sizeof(int),
7049 		.mode		= 0644,
7050 		.proc_handler	= proc_dointvec,
7051 	},
7052 	{
7053 		.procname	= "accept_ra_min_lft",
7054 		.data		= &ipv6_devconf.accept_ra_min_lft,
7055 		.maxlen		= sizeof(int),
7056 		.mode		= 0644,
7057 		.proc_handler	= proc_dointvec,
7058 	},
7059 	{
7060 		.procname	= "accept_ra_pinfo",
7061 		.data		= &ipv6_devconf.accept_ra_pinfo,
7062 		.maxlen		= sizeof(int),
7063 		.mode		= 0644,
7064 		.proc_handler	= proc_dointvec,
7065 	},
7066 	{
7067 		.procname	= "ra_honor_pio_life",
7068 		.data		= &ipv6_devconf.ra_honor_pio_life,
7069 		.maxlen		= sizeof(u8),
7070 		.mode		= 0644,
7071 		.proc_handler	= proc_dou8vec_minmax,
7072 		.extra1		= SYSCTL_ZERO,
7073 		.extra2		= SYSCTL_ONE,
7074 	},
7075 	{
7076 		.procname	= "ra_honor_pio_pflag",
7077 		.data		= &ipv6_devconf.ra_honor_pio_pflag,
7078 		.maxlen		= sizeof(u8),
7079 		.mode		= 0644,
7080 		.proc_handler	= proc_dou8vec_minmax,
7081 		.extra1		= SYSCTL_ZERO,
7082 		.extra2		= SYSCTL_ONE,
7083 	},
7084 #ifdef CONFIG_IPV6_ROUTER_PREF
7085 	{
7086 		.procname	= "accept_ra_rtr_pref",
7087 		.data		= &ipv6_devconf.accept_ra_rtr_pref,
7088 		.maxlen		= sizeof(int),
7089 		.mode		= 0644,
7090 		.proc_handler	= proc_dointvec,
7091 	},
7092 	{
7093 		.procname	= "router_probe_interval",
7094 		.data		= &ipv6_devconf.rtr_probe_interval,
7095 		.maxlen		= sizeof(int),
7096 		.mode		= 0644,
7097 		.proc_handler	= proc_dointvec_jiffies,
7098 	},
7099 #ifdef CONFIG_IPV6_ROUTE_INFO
7100 	{
7101 		.procname	= "accept_ra_rt_info_min_plen",
7102 		.data		= &ipv6_devconf.accept_ra_rt_info_min_plen,
7103 		.maxlen		= sizeof(int),
7104 		.mode		= 0644,
7105 		.proc_handler	= proc_dointvec,
7106 	},
7107 	{
7108 		.procname	= "accept_ra_rt_info_max_plen",
7109 		.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
7110 		.maxlen		= sizeof(int),
7111 		.mode		= 0644,
7112 		.proc_handler	= proc_dointvec,
7113 	},
7114 #endif
7115 #endif
7116 	{
7117 		.procname	= "proxy_ndp",
7118 		.data		= &ipv6_devconf.proxy_ndp,
7119 		.maxlen		= sizeof(int),
7120 		.mode		= 0644,
7121 		.proc_handler	= addrconf_sysctl_proxy_ndp,
7122 	},
7123 	{
7124 		.procname	= "accept_source_route",
7125 		.data		= &ipv6_devconf.accept_source_route,
7126 		.maxlen		= sizeof(int),
7127 		.mode		= 0644,
7128 		.proc_handler	= proc_dointvec,
7129 	},
7130 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
7131 	{
7132 		.procname	= "optimistic_dad",
7133 		.data		= &ipv6_devconf.optimistic_dad,
7134 		.maxlen		= sizeof(int),
7135 		.mode		= 0644,
7136 		.proc_handler   = proc_dointvec,
7137 	},
7138 	{
7139 		.procname	= "use_optimistic",
7140 		.data		= &ipv6_devconf.use_optimistic,
7141 		.maxlen		= sizeof(int),
7142 		.mode		= 0644,
7143 		.proc_handler	= proc_dointvec,
7144 	},
7145 #endif
7146 #ifdef CONFIG_IPV6_MROUTE
7147 	{
7148 		.procname	= "mc_forwarding",
7149 		.data		= &ipv6_devconf.mc_forwarding,
7150 		.maxlen		= sizeof(int),
7151 		.mode		= 0444,
7152 		.proc_handler	= proc_dointvec,
7153 	},
7154 #endif
7155 	{
7156 		.procname	= "disable_ipv6",
7157 		.data		= &ipv6_devconf.disable_ipv6,
7158 		.maxlen		= sizeof(int),
7159 		.mode		= 0644,
7160 		.proc_handler	= addrconf_sysctl_disable,
7161 	},
7162 	{
7163 		.procname	= "accept_dad",
7164 		.data		= &ipv6_devconf.accept_dad,
7165 		.maxlen		= sizeof(int),
7166 		.mode		= 0644,
7167 		.proc_handler	= proc_dointvec,
7168 	},
7169 	{
7170 		.procname	= "force_tllao",
7171 		.data		= &ipv6_devconf.force_tllao,
7172 		.maxlen		= sizeof(int),
7173 		.mode		= 0644,
7174 		.proc_handler	= proc_dointvec
7175 	},
7176 	{
7177 		.procname	= "ndisc_notify",
7178 		.data		= &ipv6_devconf.ndisc_notify,
7179 		.maxlen		= sizeof(int),
7180 		.mode		= 0644,
7181 		.proc_handler	= proc_dointvec
7182 	},
7183 	{
7184 		.procname	= "suppress_frag_ndisc",
7185 		.data		= &ipv6_devconf.suppress_frag_ndisc,
7186 		.maxlen		= sizeof(int),
7187 		.mode		= 0644,
7188 		.proc_handler	= proc_dointvec
7189 	},
7190 	{
7191 		.procname	= "accept_ra_from_local",
7192 		.data		= &ipv6_devconf.accept_ra_from_local,
7193 		.maxlen		= sizeof(int),
7194 		.mode		= 0644,
7195 		.proc_handler	= proc_dointvec,
7196 	},
7197 	{
7198 		.procname	= "accept_ra_mtu",
7199 		.data		= &ipv6_devconf.accept_ra_mtu,
7200 		.maxlen		= sizeof(int),
7201 		.mode		= 0644,
7202 		.proc_handler	= proc_dointvec,
7203 	},
7204 	{
7205 		.procname	= "stable_secret",
7206 		.data		= &ipv6_devconf.stable_secret,
7207 		.maxlen		= IPV6_MAX_STRLEN,
7208 		.mode		= 0600,
7209 		.proc_handler	= addrconf_sysctl_stable_secret,
7210 	},
7211 	{
7212 		.procname	= "use_oif_addrs_only",
7213 		.data		= &ipv6_devconf.use_oif_addrs_only,
7214 		.maxlen		= sizeof(int),
7215 		.mode		= 0644,
7216 		.proc_handler	= proc_dointvec,
7217 	},
7218 	{
7219 		.procname	= "ignore_routes_with_linkdown",
7220 		.data		= &ipv6_devconf.ignore_routes_with_linkdown,
7221 		.maxlen		= sizeof(int),
7222 		.mode		= 0644,
7223 		.proc_handler	= addrconf_sysctl_ignore_routes_with_linkdown,
7224 	},
7225 	{
7226 		.procname	= "drop_unicast_in_l2_multicast",
7227 		.data		= &ipv6_devconf.drop_unicast_in_l2_multicast,
7228 		.maxlen		= sizeof(int),
7229 		.mode		= 0644,
7230 		.proc_handler	= proc_dointvec,
7231 	},
7232 	{
7233 		.procname	= "drop_unsolicited_na",
7234 		.data		= &ipv6_devconf.drop_unsolicited_na,
7235 		.maxlen		= sizeof(int),
7236 		.mode		= 0644,
7237 		.proc_handler	= proc_dointvec,
7238 	},
7239 	{
7240 		.procname	= "keep_addr_on_down",
7241 		.data		= &ipv6_devconf.keep_addr_on_down,
7242 		.maxlen		= sizeof(int),
7243 		.mode		= 0644,
7244 		.proc_handler	= proc_dointvec,
7245 
7246 	},
7247 	{
7248 		.procname	= "seg6_enabled",
7249 		.data		= &ipv6_devconf.seg6_enabled,
7250 		.maxlen		= sizeof(int),
7251 		.mode		= 0644,
7252 		.proc_handler	= proc_dointvec,
7253 	},
7254 #ifdef CONFIG_IPV6_SEG6_HMAC
7255 	{
7256 		.procname	= "seg6_require_hmac",
7257 		.data		= &ipv6_devconf.seg6_require_hmac,
7258 		.maxlen		= sizeof(int),
7259 		.mode		= 0644,
7260 		.proc_handler	= proc_dointvec,
7261 	},
7262 #endif
7263 	{
7264 		.procname       = "enhanced_dad",
7265 		.data           = &ipv6_devconf.enhanced_dad,
7266 		.maxlen         = sizeof(int),
7267 		.mode           = 0644,
7268 		.proc_handler   = proc_dointvec,
7269 	},
7270 	{
7271 		.procname	= "addr_gen_mode",
7272 		.data		= &ipv6_devconf.addr_gen_mode,
7273 		.maxlen		= sizeof(int),
7274 		.mode		= 0644,
7275 		.proc_handler	= addrconf_sysctl_addr_gen_mode,
7276 	},
7277 	{
7278 		.procname       = "disable_policy",
7279 		.data           = &ipv6_devconf.disable_policy,
7280 		.maxlen         = sizeof(int),
7281 		.mode           = 0644,
7282 		.proc_handler   = addrconf_sysctl_disable_policy,
7283 	},
7284 	{
7285 		.procname	= "ndisc_tclass",
7286 		.data		= &ipv6_devconf.ndisc_tclass,
7287 		.maxlen		= sizeof(int),
7288 		.mode		= 0644,
7289 		.proc_handler	= proc_dointvec_minmax,
7290 		.extra1		= (void *)SYSCTL_ZERO,
7291 		.extra2		= (void *)&two_five_five,
7292 	},
7293 	{
7294 		.procname	= "rpl_seg_enabled",
7295 		.data		= &ipv6_devconf.rpl_seg_enabled,
7296 		.maxlen		= sizeof(int),
7297 		.mode		= 0644,
7298 		.proc_handler   = proc_dointvec_minmax,
7299 		.extra1         = SYSCTL_ZERO,
7300 		.extra2         = SYSCTL_ONE,
7301 	},
7302 	{
7303 		.procname	= "ioam6_enabled",
7304 		.data		= &ipv6_devconf.ioam6_enabled,
7305 		.maxlen		= sizeof(u8),
7306 		.mode		= 0644,
7307 		.proc_handler	= proc_dou8vec_minmax,
7308 		.extra1		= (void *)SYSCTL_ZERO,
7309 		.extra2		= (void *)SYSCTL_ONE,
7310 	},
7311 	{
7312 		.procname	= "ioam6_id",
7313 		.data		= &ipv6_devconf.ioam6_id,
7314 		.maxlen		= sizeof(u32),
7315 		.mode		= 0644,
7316 		.proc_handler	= proc_douintvec_minmax,
7317 		.extra1		= (void *)SYSCTL_ZERO,
7318 		.extra2		= (void *)&ioam6_if_id_max,
7319 	},
7320 	{
7321 		.procname	= "ioam6_id_wide",
7322 		.data		= &ipv6_devconf.ioam6_id_wide,
7323 		.maxlen		= sizeof(u32),
7324 		.mode		= 0644,
7325 		.proc_handler	= proc_douintvec,
7326 	},
7327 	{
7328 		.procname	= "ndisc_evict_nocarrier",
7329 		.data		= &ipv6_devconf.ndisc_evict_nocarrier,
7330 		.maxlen		= sizeof(u8),
7331 		.mode		= 0644,
7332 		.proc_handler	= proc_dou8vec_minmax,
7333 		.extra1		= (void *)SYSCTL_ZERO,
7334 		.extra2		= (void *)SYSCTL_ONE,
7335 	},
7336 	{
7337 		.procname	= "accept_untracked_na",
7338 		.data		= &ipv6_devconf.accept_untracked_na,
7339 		.maxlen		= sizeof(int),
7340 		.mode		= 0644,
7341 		.proc_handler	= proc_dointvec_minmax,
7342 		.extra1		= SYSCTL_ZERO,
7343 		.extra2		= SYSCTL_TWO,
7344 	},
7345 	{
7346 		.procname	= "force_forwarding",
7347 		.data		= &ipv6_devconf.force_forwarding,
7348 		.maxlen		= sizeof(int),
7349 		.mode		= 0644,
7350 		.proc_handler	= addrconf_sysctl_force_forwarding,
7351 	},
7352 };
7353 
7354 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
7355 		struct inet6_dev *idev, struct ipv6_devconf *p)
7356 {
7357 	size_t table_size = ARRAY_SIZE(addrconf_sysctl);
7358 	int i, ifindex;
7359 	struct ctl_table *table;
7360 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
7361 
7362 	table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL_ACCOUNT);
7363 	if (!table)
7364 		goto out;
7365 
7366 	for (i = 0; i < table_size; i++) {
7367 		table[i].data += (char *)p - (char *)&ipv6_devconf;
7368 		/* If one of these is already set, then it is not safe to
7369 		 * overwrite either of them: this makes proc_dointvec_minmax
7370 		 * usable.
7371 		 */
7372 		if (!table[i].extra1 && !table[i].extra2) {
7373 			table[i].extra1 = idev; /* embedded; no ref */
7374 			table[i].extra2 = net;
7375 		}
7376 	}
7377 
7378 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
7379 
7380 	p->sysctl_header = register_net_sysctl_sz(net, path, table,
7381 						  table_size);
7382 	if (!p->sysctl_header)
7383 		goto free;
7384 
7385 	if (!strcmp(dev_name, "all"))
7386 		ifindex = NETCONFA_IFINDEX_ALL;
7387 	else if (!strcmp(dev_name, "default"))
7388 		ifindex = NETCONFA_IFINDEX_DEFAULT;
7389 	else
7390 		ifindex = idev->dev->ifindex;
7391 	inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
7392 				     ifindex, p);
7393 	return 0;
7394 
7395 free:
7396 	kfree(table);
7397 out:
7398 	return -ENOBUFS;
7399 }
7400 
7401 static void __addrconf_sysctl_unregister(struct net *net,
7402 					 struct ipv6_devconf *p, int ifindex)
7403 {
7404 	const struct ctl_table *table;
7405 
7406 	if (!p->sysctl_header)
7407 		return;
7408 
7409 	table = p->sysctl_header->ctl_table_arg;
7410 	unregister_net_sysctl_table(p->sysctl_header);
7411 	p->sysctl_header = NULL;
7412 	kfree(table);
7413 
7414 	inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
7415 }
7416 
7417 static int addrconf_sysctl_register(struct inet6_dev *idev)
7418 {
7419 	int err;
7420 
7421 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
7422 		return -EINVAL;
7423 
7424 	err = neigh_sysctl_register(idev->dev, idev->nd_parms,
7425 				    &ndisc_ifinfo_sysctl_change);
7426 	if (err)
7427 		return err;
7428 	err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
7429 					 idev, &idev->cnf);
7430 	if (err)
7431 		neigh_sysctl_unregister(idev->nd_parms);
7432 
7433 	return err;
7434 }
7435 
7436 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
7437 {
7438 	__addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
7439 				     idev->dev->ifindex);
7440 	neigh_sysctl_unregister(idev->nd_parms);
7441 }
7442 
7443 
7444 #endif
7445 
7446 static int __net_init addrconf_init_net(struct net *net)
7447 {
7448 	int err = -ENOMEM;
7449 	struct ipv6_devconf *all, *dflt;
7450 
7451 	spin_lock_init(&net->ipv6.addrconf_hash_lock);
7452 	INIT_DEFERRABLE_WORK(&net->ipv6.addr_chk_work, addrconf_verify_work);
7453 	net->ipv6.inet6_addr_lst = kzalloc_objs(struct hlist_head,
7454 						IN6_ADDR_HSIZE);
7455 	if (!net->ipv6.inet6_addr_lst)
7456 		goto err_alloc_addr;
7457 
7458 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
7459 	if (!all)
7460 		goto err_alloc_all;
7461 
7462 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
7463 	if (!dflt)
7464 		goto err_alloc_dflt;
7465 
7466 	if (!net_eq(net, &init_net)) {
7467 		switch (net_inherit_devconf()) {
7468 		case 1:  /* copy from init_net */
7469 			memcpy(all, init_net.ipv6.devconf_all,
7470 			       sizeof(ipv6_devconf));
7471 			memcpy(dflt, init_net.ipv6.devconf_dflt,
7472 			       sizeof(ipv6_devconf_dflt));
7473 			break;
7474 		case 3: /* copy from the current netns */
7475 			memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all,
7476 			       sizeof(ipv6_devconf));
7477 			memcpy(dflt,
7478 			       current->nsproxy->net_ns->ipv6.devconf_dflt,
7479 			       sizeof(ipv6_devconf_dflt));
7480 			break;
7481 		case 0:
7482 		case 2:
7483 			/* use compiled values */
7484 			break;
7485 		}
7486 	}
7487 
7488 	/* these will be inherited by all namespaces */
7489 	dflt->autoconf = ipv6_defaults.autoconf;
7490 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
7491 
7492 	dflt->stable_secret.initialized = false;
7493 	all->stable_secret.initialized = false;
7494 
7495 	net->ipv6.devconf_all = all;
7496 	net->ipv6.devconf_dflt = dflt;
7497 
7498 #ifdef CONFIG_SYSCTL
7499 	err = __addrconf_sysctl_register(net, "all", NULL, all);
7500 	if (err < 0)
7501 		goto err_reg_all;
7502 
7503 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
7504 	if (err < 0)
7505 		goto err_reg_dflt;
7506 #endif
7507 	return 0;
7508 
7509 #ifdef CONFIG_SYSCTL
7510 err_reg_dflt:
7511 	__addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
7512 err_reg_all:
7513 	kfree(dflt);
7514 	net->ipv6.devconf_dflt = NULL;
7515 #endif
7516 err_alloc_dflt:
7517 	kfree(all);
7518 	net->ipv6.devconf_all = NULL;
7519 err_alloc_all:
7520 	kfree(net->ipv6.inet6_addr_lst);
7521 err_alloc_addr:
7522 	return err;
7523 }
7524 
7525 static void __net_exit addrconf_exit_net(struct net *net)
7526 {
7527 	int i;
7528 
7529 #ifdef CONFIG_SYSCTL
7530 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
7531 				     NETCONFA_IFINDEX_DEFAULT);
7532 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
7533 				     NETCONFA_IFINDEX_ALL);
7534 #endif
7535 	kfree(net->ipv6.devconf_dflt);
7536 	net->ipv6.devconf_dflt = NULL;
7537 	kfree(net->ipv6.devconf_all);
7538 	net->ipv6.devconf_all = NULL;
7539 
7540 	cancel_delayed_work_sync(&net->ipv6.addr_chk_work);
7541 	/*
7542 	 *	Check hash table, then free it.
7543 	 */
7544 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
7545 		WARN_ON_ONCE(!hlist_empty(&net->ipv6.inet6_addr_lst[i]));
7546 
7547 	kfree(net->ipv6.inet6_addr_lst);
7548 	net->ipv6.inet6_addr_lst = NULL;
7549 }
7550 
7551 static struct pernet_operations addrconf_ops = {
7552 	.init = addrconf_init_net,
7553 	.exit = addrconf_exit_net,
7554 };
7555 
7556 static struct rtnl_af_ops inet6_ops __read_mostly = {
7557 	.family		  = AF_INET6,
7558 	.fill_link_af	  = inet6_fill_link_af,
7559 	.get_link_af_size = inet6_get_link_af_size,
7560 	.validate_link_af = inet6_validate_link_af,
7561 	.set_link_af	  = inet6_set_link_af,
7562 };
7563 
7564 static const struct rtnl_msg_handler addrconf_rtnl_msg_handlers[] __initconst_or_module = {
7565 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETLINK,
7566 	 .dumpit = inet6_dump_ifinfo, .flags = RTNL_FLAG_DUMP_UNLOCKED},
7567 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_NEWADDR,
7568 	 .doit = inet6_rtm_newaddr, .flags = RTNL_FLAG_DOIT_PERNET},
7569 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_DELADDR,
7570 	 .doit = inet6_rtm_deladdr, .flags = RTNL_FLAG_DOIT_PERNET},
7571 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETADDR,
7572 	 .doit = inet6_rtm_getaddr, .dumpit = inet6_dump_ifaddr,
7573 	 .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
7574 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETMULTICAST,
7575 	 .dumpit = inet6_dump_ifmcaddr,
7576 	 .flags = RTNL_FLAG_DUMP_UNLOCKED},
7577 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETANYCAST,
7578 	 .dumpit = inet6_dump_ifacaddr,
7579 	 .flags = RTNL_FLAG_DUMP_UNLOCKED},
7580 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETNETCONF,
7581 	 .doit = inet6_netconf_get_devconf, .dumpit = inet6_netconf_dump_devconf,
7582 	 .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
7583 };
7584 
7585 /*
7586  *	Init / cleanup code
7587  */
7588 
7589 int __init addrconf_init(void)
7590 {
7591 	struct inet6_dev *idev;
7592 	int err;
7593 
7594 	err = ipv6_addr_label_init();
7595 	if (err < 0) {
7596 		pr_crit("%s: cannot initialize default policy table: %d\n",
7597 			__func__, err);
7598 		goto out;
7599 	}
7600 
7601 	err = register_pernet_subsys(&addrconf_ops);
7602 	if (err < 0)
7603 		goto out_addrlabel;
7604 
7605 	/* All works using addrconf_wq need to lock rtnl. */
7606 	addrconf_wq = create_singlethread_workqueue("ipv6_addrconf");
7607 	if (!addrconf_wq) {
7608 		err = -ENOMEM;
7609 		goto out_nowq;
7610 	}
7611 
7612 	rtnl_net_lock(&init_net);
7613 	idev = ipv6_add_dev(blackhole_netdev);
7614 	rtnl_net_unlock(&init_net);
7615 	if (IS_ERR(idev)) {
7616 		err = PTR_ERR(idev);
7617 		goto errlo;
7618 	}
7619 
7620 	ip6_route_init_special_entries();
7621 
7622 	register_netdevice_notifier(&ipv6_dev_notf);
7623 
7624 	addrconf_verify(&init_net);
7625 
7626 	err = rtnl_af_register(&inet6_ops);
7627 	if (err)
7628 		goto erraf;
7629 
7630 	err = rtnl_register_many(addrconf_rtnl_msg_handlers);
7631 	if (err)
7632 		goto errout;
7633 
7634 	err = ipv6_addr_label_rtnl_register();
7635 	if (err < 0)
7636 		goto errout;
7637 
7638 	return 0;
7639 errout:
7640 	rtnl_unregister_all(PF_INET6);
7641 	rtnl_af_unregister(&inet6_ops);
7642 erraf:
7643 	unregister_netdevice_notifier(&ipv6_dev_notf);
7644 errlo:
7645 	destroy_workqueue(addrconf_wq);
7646 out_nowq:
7647 	unregister_pernet_subsys(&addrconf_ops);
7648 out_addrlabel:
7649 	ipv6_addr_label_cleanup();
7650 out:
7651 	return err;
7652 }
7653 
7654 void addrconf_cleanup(void)
7655 {
7656 	struct net_device *dev;
7657 
7658 	unregister_netdevice_notifier(&ipv6_dev_notf);
7659 	unregister_pernet_subsys(&addrconf_ops);
7660 	ipv6_addr_label_cleanup();
7661 
7662 	rtnl_af_unregister(&inet6_ops);
7663 
7664 	rtnl_net_lock(&init_net);
7665 
7666 	/* clean dev list */
7667 	for_each_netdev(&init_net, dev) {
7668 		if (!__in6_dev_get_rtnl_net(dev))
7669 			continue;
7670 		addrconf_ifdown(dev, true);
7671 	}
7672 	addrconf_ifdown(init_net.loopback_dev, true);
7673 
7674 	rtnl_net_unlock(&init_net);
7675 
7676 	destroy_workqueue(addrconf_wq);
7677 }
7678