xref: /linux/net/ipv6/addrconf.c (revision c3e67bd82b9b9b1a5118f09a429ccefe2aab0741)
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 			if (scores[hiscore_idx].ifa &&
1906 			    scores[hiscore_idx].scopedist >= 0)
1907 				goto out;
1908 		}
1909 
1910 		for_each_netdev_rcu(net, dev) {
1911 			/* only consider addresses on devices in the
1912 			 * same L3 domain
1913 			 */
1914 			if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1915 				continue;
1916 			idev = __in6_dev_get(dev);
1917 			if (!idev)
1918 				continue;
1919 			hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
1920 		}
1921 	}
1922 
1923 out:
1924 	hiscore = &scores[hiscore_idx];
1925 	if (!hiscore->ifa)
1926 		ret = -EADDRNOTAVAIL;
1927 	else
1928 		*saddr = hiscore->ifa->addr;
1929 
1930 	rcu_read_unlock();
1931 	return ret;
1932 }
1933 EXPORT_SYMBOL(ipv6_dev_get_saddr);
1934 
1935 static int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1936 			      u32 banned_flags)
1937 {
1938 	struct inet6_ifaddr *ifp;
1939 	int err = -EADDRNOTAVAIL;
1940 
1941 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1942 		if (ifp->scope > IFA_LINK)
1943 			break;
1944 		if (ifp->scope == IFA_LINK &&
1945 		    !(ifp->flags & banned_flags)) {
1946 			*addr = ifp->addr;
1947 			err = 0;
1948 			break;
1949 		}
1950 	}
1951 	return err;
1952 }
1953 
1954 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1955 		    u32 banned_flags)
1956 {
1957 	struct inet6_dev *idev;
1958 	int err = -EADDRNOTAVAIL;
1959 
1960 	rcu_read_lock();
1961 	idev = __in6_dev_get(dev);
1962 	if (idev) {
1963 		read_lock_bh(&idev->lock);
1964 		err = __ipv6_get_lladdr(idev, addr, banned_flags);
1965 		read_unlock_bh(&idev->lock);
1966 	}
1967 	rcu_read_unlock();
1968 	return err;
1969 }
1970 
1971 static int ipv6_count_addresses(const struct inet6_dev *idev)
1972 {
1973 	const struct inet6_ifaddr *ifp;
1974 	int cnt = 0;
1975 
1976 	rcu_read_lock();
1977 	list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
1978 		cnt++;
1979 	rcu_read_unlock();
1980 	return cnt;
1981 }
1982 
1983 int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1984 		  const struct net_device *dev, int strict)
1985 {
1986 	return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1987 				       strict, IFA_F_TENTATIVE);
1988 }
1989 EXPORT_SYMBOL(ipv6_chk_addr);
1990 
1991 /* device argument is used to find the L3 domain of interest. If
1992  * skip_dev_check is set, then the ifp device is not checked against
1993  * the passed in dev argument. So the 2 cases for addresses checks are:
1994  *   1. does the address exist in the L3 domain that dev is part of
1995  *      (skip_dev_check = true), or
1996  *
1997  *   2. does the address exist on the specific device
1998  *      (skip_dev_check = false)
1999  */
2000 static struct net_device *
2001 __ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
2002 			  const struct net_device *dev, bool skip_dev_check,
2003 			  int strict, u32 banned_flags)
2004 {
2005 	unsigned int hash = inet6_addr_hash(net, addr);
2006 	struct net_device *l3mdev, *ndev;
2007 	struct inet6_ifaddr *ifp;
2008 	u32 ifp_flags;
2009 
2010 	rcu_read_lock();
2011 
2012 	l3mdev = l3mdev_master_dev_rcu(dev);
2013 	if (skip_dev_check)
2014 		dev = NULL;
2015 
2016 	hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
2017 		ndev = ifp->idev->dev;
2018 
2019 		if (l3mdev_master_dev_rcu(ndev) != l3mdev)
2020 			continue;
2021 
2022 		/* Decouple optimistic from tentative for evaluation here.
2023 		 * Ban optimistic addresses explicitly, when required.
2024 		 */
2025 		ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
2026 			    ? (ifp->flags&~IFA_F_TENTATIVE)
2027 			    : ifp->flags;
2028 		if (ipv6_addr_equal(&ifp->addr, addr) &&
2029 		    !(ifp_flags&banned_flags) &&
2030 		    (!dev || ndev == dev ||
2031 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
2032 			rcu_read_unlock();
2033 			return ndev;
2034 		}
2035 	}
2036 
2037 	rcu_read_unlock();
2038 	return NULL;
2039 }
2040 
2041 int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
2042 			    const struct net_device *dev, bool skip_dev_check,
2043 			    int strict, u32 banned_flags)
2044 {
2045 	return __ipv6_chk_addr_and_flags(net, addr, dev, skip_dev_check,
2046 					 strict, banned_flags) ? 1 : 0;
2047 }
2048 EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
2049 
2050 
2051 /* Compares an address/prefix_len with addresses on device @dev.
2052  * If one is found it returns true.
2053  */
2054 bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
2055 	const unsigned int prefix_len, struct net_device *dev)
2056 {
2057 	const struct inet6_ifaddr *ifa;
2058 	const struct inet6_dev *idev;
2059 	bool ret = false;
2060 
2061 	rcu_read_lock();
2062 	idev = __in6_dev_get(dev);
2063 	if (idev) {
2064 		list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
2065 			ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
2066 			if (ret)
2067 				break;
2068 		}
2069 	}
2070 	rcu_read_unlock();
2071 
2072 	return ret;
2073 }
2074 EXPORT_SYMBOL(ipv6_chk_custom_prefix);
2075 
2076 int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
2077 {
2078 	const struct inet6_ifaddr *ifa;
2079 	const struct inet6_dev *idev;
2080 	int	onlink;
2081 
2082 	onlink = 0;
2083 	rcu_read_lock();
2084 	idev = __in6_dev_get(dev);
2085 	if (idev) {
2086 		list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
2087 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
2088 						   ifa->prefix_len);
2089 			if (onlink)
2090 				break;
2091 		}
2092 	}
2093 	rcu_read_unlock();
2094 	return onlink;
2095 }
2096 EXPORT_SYMBOL(ipv6_chk_prefix);
2097 
2098 /**
2099  * ipv6_dev_find - find the first device with a given source address.
2100  * @net: the net namespace
2101  * @addr: the source address
2102  * @dev: used to find the L3 domain of interest
2103  *
2104  * The caller should be protected by RCU, or RTNL.
2105  */
2106 struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr,
2107 				 struct net_device *dev)
2108 {
2109 	return __ipv6_chk_addr_and_flags(net, addr, dev, !dev, 1,
2110 					 IFA_F_TENTATIVE);
2111 }
2112 EXPORT_SYMBOL(ipv6_dev_find);
2113 
2114 struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
2115 				     struct net_device *dev, int strict)
2116 {
2117 	unsigned int hash = inet6_addr_hash(net, addr);
2118 	struct inet6_ifaddr *ifp, *result = NULL;
2119 
2120 	rcu_read_lock();
2121 	hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
2122 		if (ipv6_addr_equal(&ifp->addr, addr)) {
2123 			if (!dev || ifp->idev->dev == dev ||
2124 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
2125 				if (in6_ifa_hold_safe(ifp)) {
2126 					result = ifp;
2127 					break;
2128 				}
2129 			}
2130 		}
2131 	}
2132 	rcu_read_unlock();
2133 
2134 	return result;
2135 }
2136 
2137 /* Gets referenced address, destroys ifaddr */
2138 
2139 static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
2140 {
2141 	if (dad_failed)
2142 		ifp->flags |= IFA_F_DADFAILED;
2143 
2144 	if (ifp->flags&IFA_F_TEMPORARY) {
2145 		struct inet6_ifaddr *ifpub;
2146 		spin_lock_bh(&ifp->lock);
2147 		ifpub = ifp->ifpub;
2148 		if (ifpub) {
2149 			in6_ifa_hold(ifpub);
2150 			spin_unlock_bh(&ifp->lock);
2151 			ipv6_create_tempaddr(ifpub, true);
2152 			in6_ifa_put(ifpub);
2153 		} else {
2154 			spin_unlock_bh(&ifp->lock);
2155 		}
2156 		ipv6_del_addr(ifp);
2157 	} else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
2158 		spin_lock_bh(&ifp->lock);
2159 		addrconf_del_dad_work(ifp);
2160 		ifp->flags |= IFA_F_TENTATIVE;
2161 		if (dad_failed)
2162 			ifp->flags &= ~IFA_F_OPTIMISTIC;
2163 		spin_unlock_bh(&ifp->lock);
2164 		if (dad_failed)
2165 			ipv6_ifa_notify(0, ifp);
2166 		in6_ifa_put(ifp);
2167 	} else {
2168 		ipv6_del_addr(ifp);
2169 	}
2170 }
2171 
2172 static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2173 {
2174 	int err = -ENOENT;
2175 
2176 	spin_lock_bh(&ifp->lock);
2177 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
2178 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
2179 		err = 0;
2180 	}
2181 	spin_unlock_bh(&ifp->lock);
2182 
2183 	return err;
2184 }
2185 
2186 void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
2187 {
2188 	struct inet6_dev *idev = ifp->idev;
2189 	struct net *net = dev_net(idev->dev);
2190 	int max_addresses;
2191 
2192 	spin_lock_bh(&ifp->lock);
2193 
2194 	if (ifp->state != INET6_IFADDR_STATE_DAD) {
2195 		spin_unlock_bh(&ifp->lock);
2196 		in6_ifa_put(ifp);
2197 		return;
2198 	}
2199 	ifp->state = INET6_IFADDR_STATE_POSTDAD;
2200 
2201 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2202 			     ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
2203 
2204 	if (ifp->flags & IFA_F_STABLE_PRIVACY) {
2205 		bool start_dad = false;
2206 		struct in6_addr new_addr;
2207 		struct inet6_ifaddr *ifp2;
2208 		int retries = ifp->stable_privacy_retry + 1;
2209 		struct ifa6_config cfg = {
2210 			.pfx = &new_addr,
2211 			.plen = ifp->prefix_len,
2212 			.ifa_flags = ifp->flags,
2213 			.valid_lft = ifp->valid_lft,
2214 			.preferred_lft = ifp->prefered_lft,
2215 			.scope = ifp->scope,
2216 		};
2217 
2218 		if (retries > net->ipv6.sysctl.idgen_retries) {
2219 			net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
2220 					     ifp->idev->dev->name);
2221 			goto errdad;
2222 		}
2223 
2224 		new_addr = ifp->addr;
2225 		if (ipv6_generate_stable_address(&new_addr, retries,
2226 						 idev))
2227 			goto errdad;
2228 
2229 		spin_unlock_bh(&ifp->lock);
2230 
2231 		max_addresses = READ_ONCE(idev->cnf.max_addresses);
2232 		if (max_addresses &&
2233 		    ipv6_count_addresses(idev) >= max_addresses)
2234 			goto lock_errdad;
2235 
2236 		net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2237 				     ifp->idev->dev->name);
2238 
2239 		ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
2240 		if (IS_ERR(ifp2))
2241 			goto lock_errdad;
2242 
2243 		spin_lock_bh(&ifp2->lock);
2244 		if (ifp2->state != INET6_IFADDR_STATE_DEAD) {
2245 			ifp2->stable_privacy_retry = retries;
2246 			start_dad = true;
2247 		}
2248 		spin_unlock_bh(&ifp2->lock);
2249 
2250 		if (start_dad)
2251 			addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
2252 		in6_ifa_put(ifp2);
2253 lock_errdad:
2254 		spin_lock_bh(&ifp->lock);
2255 		if (ifp->state != INET6_IFADDR_STATE_POSTDAD) {
2256 			spin_unlock_bh(&ifp->lock);
2257 			in6_ifa_put(ifp);
2258 			return;
2259 		}
2260 	}
2261 
2262 errdad:
2263 	/* transition from _POSTDAD to _ERRDAD */
2264 	ifp->state = INET6_IFADDR_STATE_ERRDAD;
2265 	spin_unlock_bh(&ifp->lock);
2266 
2267 	addrconf_mod_dad_work(ifp, 0);
2268 	in6_ifa_put(ifp);
2269 }
2270 
2271 /* Join to solicited addr multicast group. */
2272 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
2273 {
2274 	struct in6_addr maddr;
2275 
2276 	if (READ_ONCE(dev->flags) & (IFF_LOOPBACK | IFF_NOARP))
2277 		return;
2278 
2279 	addrconf_addr_solict_mult(addr, &maddr);
2280 	ipv6_dev_mc_inc(dev, &maddr);
2281 }
2282 
2283 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
2284 {
2285 	struct in6_addr maddr;
2286 
2287 	if (READ_ONCE(idev->dev->flags) & (IFF_LOOPBACK | IFF_NOARP))
2288 		return;
2289 
2290 	addrconf_addr_solict_mult(addr, &maddr);
2291 	__ipv6_dev_mc_dec(idev, &maddr);
2292 }
2293 
2294 static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
2295 {
2296 	struct in6_addr addr;
2297 
2298 	if (ifp->prefix_len >= 127) /* RFC 6164 */
2299 		return;
2300 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2301 	if (ipv6_addr_any(&addr))
2302 		return;
2303 	__ipv6_dev_ac_inc(ifp->idev, &addr);
2304 }
2305 
2306 static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
2307 {
2308 	struct in6_addr addr;
2309 
2310 	if (ifp->prefix_len >= 127) /* RFC 6164 */
2311 		return;
2312 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2313 	if (ipv6_addr_any(&addr))
2314 		return;
2315 	__ipv6_dev_ac_dec(ifp->idev, &addr);
2316 }
2317 
2318 static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
2319 {
2320 	switch (dev->addr_len) {
2321 	case ETH_ALEN:
2322 		memcpy(eui, dev->dev_addr, 3);
2323 		eui[3] = 0xFF;
2324 		eui[4] = 0xFE;
2325 		memcpy(eui + 5, dev->dev_addr + 3, 3);
2326 		break;
2327 	case EUI64_ADDR_LEN:
2328 		memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2329 		eui[0] ^= 2;
2330 		break;
2331 	default:
2332 		return -1;
2333 	}
2334 
2335 	return 0;
2336 }
2337 
2338 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2339 {
2340 	const union fwnet_hwaddr *ha;
2341 
2342 	if (dev->addr_len != FWNET_ALEN)
2343 		return -1;
2344 
2345 	ha = (const union fwnet_hwaddr *)dev->dev_addr;
2346 
2347 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2348 	eui[0] ^= 2;
2349 	return 0;
2350 }
2351 
2352 static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2353 {
2354 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
2355 	if (dev->addr_len != ARCNET_ALEN)
2356 		return -1;
2357 	memset(eui, 0, 7);
2358 	eui[7] = *(u8 *)dev->dev_addr;
2359 	return 0;
2360 }
2361 
2362 static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2363 {
2364 	if (dev->addr_len != INFINIBAND_ALEN)
2365 		return -1;
2366 	memcpy(eui, dev->dev_addr + 12, 8);
2367 	eui[0] |= 2;
2368 	return 0;
2369 }
2370 
2371 static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
2372 {
2373 	if (addr == 0)
2374 		return -1;
2375 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2376 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2377 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2378 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2379 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2380 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2381 	eui[1] = 0;
2382 	eui[2] = 0x5E;
2383 	eui[3] = 0xFE;
2384 	memcpy(eui + 4, &addr, 4);
2385 	return 0;
2386 }
2387 
2388 static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2389 {
2390 	if (dev->priv_flags & IFF_ISATAP)
2391 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2392 	return -1;
2393 }
2394 
2395 static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2396 {
2397 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2398 }
2399 
2400 static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2401 {
2402 	memcpy(eui, dev->perm_addr, 3);
2403 	memcpy(eui + 5, dev->perm_addr + 3, 3);
2404 	eui[3] = 0xFF;
2405 	eui[4] = 0xFE;
2406 	eui[0] ^= 2;
2407 	return 0;
2408 }
2409 
2410 static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2411 {
2412 	switch (dev->type) {
2413 	case ARPHRD_ETHER:
2414 	case ARPHRD_FDDI:
2415 		return addrconf_ifid_eui48(eui, dev);
2416 	case ARPHRD_ARCNET:
2417 		return addrconf_ifid_arcnet(eui, dev);
2418 	case ARPHRD_INFINIBAND:
2419 		return addrconf_ifid_infiniband(eui, dev);
2420 	case ARPHRD_SIT:
2421 		return addrconf_ifid_sit(eui, dev);
2422 	case ARPHRD_IPGRE:
2423 	case ARPHRD_TUNNEL:
2424 		return addrconf_ifid_gre(eui, dev);
2425 	case ARPHRD_6LOWPAN:
2426 		return addrconf_ifid_6lowpan(eui, dev);
2427 	case ARPHRD_IEEE1394:
2428 		return addrconf_ifid_ieee1394(eui, dev);
2429 	case ARPHRD_TUNNEL6:
2430 	case ARPHRD_IP6GRE:
2431 	case ARPHRD_RAWIP:
2432 		return addrconf_ifid_ip6tnl(eui, dev);
2433 	}
2434 	return -1;
2435 }
2436 
2437 static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2438 {
2439 	int err = -1;
2440 	struct inet6_ifaddr *ifp;
2441 
2442 	read_lock_bh(&idev->lock);
2443 	list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2444 		if (ifp->scope > IFA_LINK)
2445 			break;
2446 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2447 			memcpy(eui, ifp->addr.s6_addr+8, 8);
2448 			err = 0;
2449 			break;
2450 		}
2451 	}
2452 	read_unlock_bh(&idev->lock);
2453 	return err;
2454 }
2455 
2456 /* Generation of a randomized Interface Identifier
2457  * draft-ietf-6man-rfc4941bis, Section 3.3.1
2458  */
2459 
2460 static void ipv6_gen_rnd_iid(struct in6_addr *addr)
2461 {
2462 regen:
2463 	get_random_bytes(&addr->s6_addr[8], 8);
2464 
2465 	/* <draft-ietf-6man-rfc4941bis-08.txt>, Section 3.3.1:
2466 	 * check if generated address is not inappropriate:
2467 	 *
2468 	 * - Reserved IPv6 Interface Identifiers
2469 	 * - XXX: already assigned to an address on the device
2470 	 */
2471 
2472 	/* Subnet-router anycast: 0000:0000:0000:0000 */
2473 	if (!(addr->s6_addr32[2] | addr->s6_addr32[3]))
2474 		goto regen;
2475 
2476 	/* IANA Ethernet block: 0200:5EFF:FE00:0000-0200:5EFF:FE00:5212
2477 	 * Proxy Mobile IPv6:   0200:5EFF:FE00:5213
2478 	 * IANA Ethernet block: 0200:5EFF:FE00:5214-0200:5EFF:FEFF:FFFF
2479 	 */
2480 	if (ntohl(addr->s6_addr32[2]) == 0x02005eff &&
2481 	    (ntohl(addr->s6_addr32[3]) & 0Xff000000) == 0xfe000000)
2482 		goto regen;
2483 
2484 	/* Reserved subnet anycast addresses */
2485 	if (ntohl(addr->s6_addr32[2]) == 0xfdffffff &&
2486 	    ntohl(addr->s6_addr32[3]) >= 0Xffffff80)
2487 		goto regen;
2488 }
2489 
2490 /*
2491  *	Add prefix route.
2492  */
2493 
2494 static void
2495 addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
2496 		      struct net_device *dev, unsigned long expires,
2497 		      u32 flags, gfp_t gfp_flags)
2498 {
2499 	struct fib6_config cfg = {
2500 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
2501 		.fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
2502 		.fc_ifindex = dev->ifindex,
2503 		.fc_expires = expires,
2504 		.fc_dst_len = plen,
2505 		.fc_flags = RTF_UP | flags,
2506 		.fc_nlinfo.nl_net = dev_net(dev),
2507 		.fc_protocol = RTPROT_KERNEL,
2508 		.fc_type = RTN_UNICAST,
2509 	};
2510 
2511 	cfg.fc_dst = *pfx;
2512 
2513 	/* Prevent useless cloning on PtP SIT.
2514 	   This thing is done here expecting that the whole
2515 	   class of non-broadcast devices need not cloning.
2516 	 */
2517 #if IS_ENABLED(CONFIG_IPV6_SIT)
2518 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2519 		cfg.fc_flags |= RTF_NONEXTHOP;
2520 #endif
2521 
2522 	ip6_route_add(&cfg, gfp_flags, NULL);
2523 }
2524 
2525 
2526 static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2527 						  int plen,
2528 						  const struct net_device *dev,
2529 						  u32 flags, u32 noflags,
2530 						  bool no_gw)
2531 {
2532 	struct fib6_node *fn;
2533 	struct fib6_info *rt = NULL;
2534 	struct fib6_table *table;
2535 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
2536 
2537 	table = fib6_get_table(dev_net(dev), tb_id);
2538 	if (!table)
2539 		return NULL;
2540 
2541 	rcu_read_lock();
2542 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
2543 	if (!fn)
2544 		goto out;
2545 
2546 	for_each_fib6_node_rt_rcu(fn) {
2547 		/* prefix routes only use builtin fib6_nh */
2548 		if (rt->nh)
2549 			continue;
2550 
2551 		if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex)
2552 			continue;
2553 		if (no_gw && rt->fib6_nh->fib_nh_gw_family)
2554 			continue;
2555 		if ((rt->fib6_flags & flags) != flags)
2556 			continue;
2557 		if ((rt->fib6_flags & noflags) != 0)
2558 			continue;
2559 		if (!fib6_info_hold_safe(rt))
2560 			continue;
2561 		break;
2562 	}
2563 out:
2564 	rcu_read_unlock();
2565 	return rt;
2566 }
2567 
2568 
2569 /* Create "default" multicast route to the interface */
2570 
2571 static void addrconf_add_mroute(struct net_device *dev)
2572 {
2573 	struct fib6_config cfg = {
2574 		.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
2575 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
2576 		.fc_ifindex = dev->ifindex,
2577 		.fc_dst_len = 8,
2578 		.fc_flags = RTF_UP,
2579 		.fc_type = RTN_MULTICAST,
2580 		.fc_nlinfo.nl_net = dev_net(dev),
2581 		.fc_protocol = RTPROT_KERNEL,
2582 	};
2583 
2584 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2585 
2586 	ip6_route_add(&cfg, GFP_KERNEL, NULL);
2587 }
2588 
2589 static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2590 {
2591 	struct inet6_dev *idev;
2592 
2593 	ASSERT_RTNL();
2594 
2595 	idev = ipv6_find_idev(dev);
2596 	if (IS_ERR(idev))
2597 		return idev;
2598 
2599 	if (idev->cnf.disable_ipv6)
2600 		return ERR_PTR(-EACCES);
2601 
2602 	/* Add default multicast route */
2603 	if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
2604 		addrconf_add_mroute(dev);
2605 
2606 	return idev;
2607 }
2608 
2609 static void delete_tempaddrs(struct inet6_dev *idev,
2610 			     struct inet6_ifaddr *ifp)
2611 {
2612 	struct inet6_ifaddr *ift, *tmp;
2613 
2614 	write_lock_bh(&idev->lock);
2615 	list_for_each_entry_safe(ift, tmp, &idev->tempaddr_list, tmp_list) {
2616 		if (ift->ifpub != ifp)
2617 			continue;
2618 
2619 		in6_ifa_hold(ift);
2620 		write_unlock_bh(&idev->lock);
2621 		ipv6_del_addr(ift);
2622 		write_lock_bh(&idev->lock);
2623 	}
2624 	write_unlock_bh(&idev->lock);
2625 }
2626 
2627 static void manage_tempaddrs(struct inet6_dev *idev,
2628 			     struct inet6_ifaddr *ifp,
2629 			     __u32 valid_lft, __u32 prefered_lft,
2630 			     bool create, unsigned long now)
2631 {
2632 	u32 orig_prefered_lft = prefered_lft;
2633 	struct inet6_ifaddr *ift;
2634 	bool reset_done = false;
2635 	u32 flags;
2636 
2637 	read_lock_bh(&idev->lock);
2638 	/* update all temporary addresses in the list */
2639 	list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2640 		int age, max_valid, max_prefered;
2641 
2642 		if (ifp != ift->ifpub)
2643 			continue;
2644 
2645 		/* RFC 4941 section 3.3:
2646 		 * If a received option will extend the lifetime of a public
2647 		 * address, the lifetimes of temporary addresses should
2648 		 * be extended, subject to the overall constraint that no
2649 		 * temporary addresses should ever remain "valid" or "preferred"
2650 		 * for a time longer than (TEMP_VALID_LIFETIME) or
2651 		 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2652 		 */
2653 		age = (now - ift->cstamp) / HZ;
2654 		max_valid = READ_ONCE(idev->cnf.temp_valid_lft) - age;
2655 		if (max_valid < 0)
2656 			max_valid = 0;
2657 
2658 		max_prefered = READ_ONCE(idev->cnf.temp_prefered_lft) -
2659 			       idev->desync_factor - age;
2660 		if (max_prefered < 0)
2661 			max_prefered = 0;
2662 
2663 		if (valid_lft > max_valid)
2664 			valid_lft = max_valid;
2665 
2666 		if (prefered_lft > max_prefered)
2667 			prefered_lft = max_prefered;
2668 
2669 		spin_lock(&ift->lock);
2670 		/* the first match is the most recent temp address */
2671 		if (!reset_done && orig_prefered_lft > 0) {
2672 			ift->regen_count = 0;
2673 			reset_done = true;
2674 		}
2675 		flags = ift->flags;
2676 		ift->valid_lft = valid_lft;
2677 		ift->prefered_lft = prefered_lft;
2678 		ift->tstamp = now;
2679 		if (prefered_lft > 0)
2680 			ift->flags &= ~IFA_F_DEPRECATED;
2681 
2682 		spin_unlock(&ift->lock);
2683 		if (!(flags&IFA_F_TENTATIVE))
2684 			ipv6_ifa_notify(0, ift);
2685 	}
2686 
2687 	/* Also create a temporary address if it's enabled but no temporary
2688 	 * address currently exists.
2689 	 * However, we get called with valid_lft == 0, prefered_lft == 0, create == false
2690 	 * as part of cleanup (ie. deleting the mngtmpaddr).
2691 	 * We don't want that to result in creating a new temporary ip address.
2692 	 */
2693 	if (list_empty(&idev->tempaddr_list) && (valid_lft || prefered_lft))
2694 		create = true;
2695 
2696 	if (create && READ_ONCE(idev->cnf.use_tempaddr) > 0) {
2697 		/* When a new public address is created as described
2698 		 * in [ADDRCONF], also create a new temporary address.
2699 		 */
2700 		read_unlock_bh(&idev->lock);
2701 		ipv6_create_tempaddr(ifp, false);
2702 	} else {
2703 		read_unlock_bh(&idev->lock);
2704 	}
2705 }
2706 
2707 static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2708 {
2709 	return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2710 	       idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2711 }
2712 
2713 int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2714 				 const struct prefix_info *pinfo,
2715 				 struct inet6_dev *in6_dev,
2716 				 const struct in6_addr *addr, int addr_type,
2717 				 u32 addr_flags, bool sllao, bool tokenized,
2718 				 __u32 valid_lft, u32 prefered_lft)
2719 {
2720 	struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2721 	int create = 0, update_lft = 0;
2722 
2723 	if (!ifp && valid_lft) {
2724 		int max_addresses = READ_ONCE(in6_dev->cnf.max_addresses);
2725 		struct ifa6_config cfg = {
2726 			.pfx = addr,
2727 			.plen = pinfo->prefix_len,
2728 			.ifa_flags = addr_flags,
2729 			.valid_lft = valid_lft,
2730 			.preferred_lft = prefered_lft,
2731 			.scope = addr_type & IPV6_ADDR_SCOPE_MASK,
2732 			.ifa_proto = IFAPROT_KERNEL_RA
2733 		};
2734 
2735 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2736 		if ((READ_ONCE(net->ipv6.devconf_all->optimistic_dad) ||
2737 		     READ_ONCE(in6_dev->cnf.optimistic_dad)) &&
2738 		    !net->ipv6.devconf_all->forwarding && sllao)
2739 			cfg.ifa_flags |= IFA_F_OPTIMISTIC;
2740 #endif
2741 
2742 		/* Do not allow to create too much of autoconfigured
2743 		 * addresses; this would be too easy way to crash kernel.
2744 		 */
2745 		if (!max_addresses ||
2746 		    ipv6_count_addresses(in6_dev) < max_addresses)
2747 			ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
2748 
2749 		if (IS_ERR_OR_NULL(ifp))
2750 			return -1;
2751 
2752 		create = 1;
2753 		spin_lock_bh(&ifp->lock);
2754 		ifp->flags |= IFA_F_MANAGETEMPADDR;
2755 		ifp->cstamp = jiffies;
2756 		ifp->tokenized = tokenized;
2757 		spin_unlock_bh(&ifp->lock);
2758 		addrconf_dad_start(ifp);
2759 	}
2760 
2761 	if (ifp) {
2762 		u32 flags;
2763 		unsigned long now;
2764 		u32 stored_lft;
2765 
2766 		/* update lifetime (RFC2462 5.5.3 e) */
2767 		spin_lock_bh(&ifp->lock);
2768 		now = jiffies;
2769 		if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2770 			stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2771 		else
2772 			stored_lft = 0;
2773 
2774 		/* RFC4862 Section 5.5.3e:
2775 		 * "Note that the preferred lifetime of the
2776 		 *  corresponding address is always reset to
2777 		 *  the Preferred Lifetime in the received
2778 		 *  Prefix Information option, regardless of
2779 		 *  whether the valid lifetime is also reset or
2780 		 *  ignored."
2781 		 *
2782 		 * So we should always update prefered_lft here.
2783 		 */
2784 		update_lft = !create && stored_lft;
2785 
2786 		if (update_lft && !READ_ONCE(in6_dev->cnf.ra_honor_pio_life)) {
2787 			const u32 minimum_lft = min_t(u32,
2788 				stored_lft, MIN_VALID_LIFETIME);
2789 			valid_lft = max(valid_lft, minimum_lft);
2790 		}
2791 
2792 		if (update_lft) {
2793 			ifp->valid_lft = valid_lft;
2794 			ifp->prefered_lft = prefered_lft;
2795 			WRITE_ONCE(ifp->tstamp, now);
2796 			flags = ifp->flags;
2797 			ifp->flags &= ~IFA_F_DEPRECATED;
2798 			spin_unlock_bh(&ifp->lock);
2799 
2800 			if (!(flags&IFA_F_TENTATIVE))
2801 				ipv6_ifa_notify(0, ifp);
2802 		} else
2803 			spin_unlock_bh(&ifp->lock);
2804 
2805 		manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2806 				 create, now);
2807 
2808 		in6_ifa_put(ifp);
2809 		addrconf_verify(net);
2810 	}
2811 
2812 	return 0;
2813 }
2814 EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
2815 
2816 void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2817 {
2818 	struct prefix_info *pinfo;
2819 	struct fib6_table *table;
2820 	__u32 valid_lft;
2821 	__u32 prefered_lft;
2822 	int addr_type, err;
2823 	u32 addr_flags = 0;
2824 	struct inet6_dev *in6_dev;
2825 	struct net *net = dev_net(dev);
2826 	bool ignore_autoconf = false;
2827 
2828 	pinfo = (struct prefix_info *) opt;
2829 
2830 	if (len < sizeof(struct prefix_info)) {
2831 		netdev_dbg(dev, "addrconf: prefix option too short\n");
2832 		return;
2833 	}
2834 
2835 	/*
2836 	 *	Validation checks ([ADDRCONF], page 19)
2837 	 */
2838 
2839 	addr_type = ipv6_addr_type(&pinfo->prefix);
2840 
2841 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2842 		return;
2843 
2844 	valid_lft = ntohl(pinfo->valid);
2845 	prefered_lft = ntohl(pinfo->prefered);
2846 
2847 	if (prefered_lft > valid_lft) {
2848 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
2849 		return;
2850 	}
2851 
2852 	in6_dev = in6_dev_get(dev);
2853 
2854 	if (!in6_dev) {
2855 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2856 				    dev->name);
2857 		return;
2858 	}
2859 
2860 	if (valid_lft != 0 && valid_lft < in6_dev->cnf.accept_ra_min_lft)
2861 		goto put;
2862 
2863 	/*
2864 	 *	Two things going on here:
2865 	 *	1) Add routes for on-link prefixes
2866 	 *	2) Configure prefixes with the auto flag set
2867 	 */
2868 
2869 	if (pinfo->onlink) {
2870 		struct fib6_info *rt;
2871 		unsigned long rt_expires;
2872 
2873 		/* Avoid arithmetic overflow. Really, we could
2874 		 * save rt_expires in seconds, likely valid_lft,
2875 		 * but it would require division in fib gc, that it
2876 		 * not good.
2877 		 */
2878 		if (HZ > USER_HZ)
2879 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2880 		else
2881 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
2882 
2883 		if (addrconf_finite_timeout(rt_expires))
2884 			rt_expires *= HZ;
2885 
2886 		rt = addrconf_get_prefix_route(&pinfo->prefix,
2887 					       pinfo->prefix_len,
2888 					       dev,
2889 					       RTF_ADDRCONF | RTF_PREFIX_RT,
2890 					       RTF_DEFAULT, true);
2891 
2892 		if (rt) {
2893 			/* Autoconf prefix route */
2894 			if (valid_lft == 0) {
2895 				ip6_del_rt_reason(net, rt,
2896 						  RT_DEL_REASON_RA_WITHDRAWN);
2897 				rt = NULL;
2898 			} else {
2899 				table = rt->fib6_table;
2900 				spin_lock_bh(&table->tb6_lock);
2901 
2902 				if (addrconf_finite_timeout(rt_expires)) {
2903 					/* not infinity */
2904 					fib6_set_expires(rt, jiffies + rt_expires);
2905 					fib6_add_gc_list(rt);
2906 				} else {
2907 					fib6_clean_expires(rt);
2908 					fib6_may_remove_gc_list(net, rt);
2909 				}
2910 
2911 				spin_unlock_bh(&table->tb6_lock);
2912 			}
2913 		} else if (valid_lft) {
2914 			clock_t expires = 0;
2915 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2916 			if (addrconf_finite_timeout(rt_expires)) {
2917 				/* not infinity */
2918 				flags |= RTF_EXPIRES;
2919 				expires = jiffies_to_clock_t(rt_expires);
2920 			}
2921 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
2922 					      0, dev, expires, flags,
2923 					      GFP_ATOMIC);
2924 		}
2925 		fib6_info_release(rt);
2926 	}
2927 
2928 	/* Try to figure out our local address for this prefix */
2929 
2930 	ignore_autoconf = READ_ONCE(in6_dev->cnf.ra_honor_pio_pflag) && pinfo->preferpd;
2931 	if (pinfo->autoconf && in6_dev->cnf.autoconf && !ignore_autoconf) {
2932 		struct in6_addr addr;
2933 		bool tokenized = false, dev_addr_generated = false;
2934 
2935 		if (pinfo->prefix_len == 64) {
2936 			memcpy(&addr, &pinfo->prefix, 8);
2937 
2938 			if (!ipv6_addr_any(&in6_dev->token)) {
2939 				read_lock_bh(&in6_dev->lock);
2940 				memcpy(addr.s6_addr + 8,
2941 				       in6_dev->token.s6_addr + 8, 8);
2942 				read_unlock_bh(&in6_dev->lock);
2943 				tokenized = true;
2944 			} else if (is_addr_mode_generate_stable(in6_dev) &&
2945 				   !ipv6_generate_stable_address(&addr, 0,
2946 								 in6_dev)) {
2947 				addr_flags |= IFA_F_STABLE_PRIVACY;
2948 				goto ok;
2949 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2950 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2951 				goto put;
2952 			} else {
2953 				dev_addr_generated = true;
2954 			}
2955 			goto ok;
2956 		}
2957 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2958 				    pinfo->prefix_len);
2959 		goto put;
2960 
2961 ok:
2962 		err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2963 						   &addr, addr_type,
2964 						   addr_flags, sllao,
2965 						   tokenized, valid_lft,
2966 						   prefered_lft);
2967 		if (err)
2968 			goto put;
2969 
2970 		/* Ignore error case here because previous prefix add addr was
2971 		 * successful which will be notified.
2972 		 */
2973 		ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2974 					      addr_type, addr_flags, sllao,
2975 					      tokenized, valid_lft,
2976 					      prefered_lft,
2977 					      dev_addr_generated);
2978 	}
2979 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2980 put:
2981 	in6_dev_put(in6_dev);
2982 }
2983 
2984 static int addrconf_set_sit_dstaddr(struct net *net, struct net_device *dev,
2985 		struct in6_ifreq *ireq)
2986 {
2987 	struct ip_tunnel_parm_kern p = { };
2988 	int err;
2989 
2990 	if (!(ipv6_addr_type(&ireq->ifr6_addr) & IPV6_ADDR_COMPATv4))
2991 		return -EADDRNOTAVAIL;
2992 
2993 	p.iph.daddr = ireq->ifr6_addr.s6_addr32[3];
2994 	p.iph.version = 4;
2995 	p.iph.ihl = 5;
2996 	p.iph.protocol = IPPROTO_IPV6;
2997 	p.iph.ttl = 64;
2998 
2999 	if (!dev->netdev_ops->ndo_tunnel_ctl)
3000 		return -EOPNOTSUPP;
3001 	err = dev->netdev_ops->ndo_tunnel_ctl(dev, &p, SIOCADDTUNNEL);
3002 	if (err)
3003 		return err;
3004 
3005 	dev = __dev_get_by_name(net, p.name);
3006 	if (!dev)
3007 		return -ENOBUFS;
3008 	return dev_open(dev, NULL);
3009 }
3010 
3011 /*
3012  *	Set destination address.
3013  *	Special case for SIT interfaces where we create a new "virtual"
3014  *	device.
3015  */
3016 int addrconf_set_dstaddr(struct net *net, void __user *arg)
3017 {
3018 	struct net_device *dev;
3019 	struct in6_ifreq ireq;
3020 	int err = -ENODEV;
3021 
3022 	if (!IS_ENABLED(CONFIG_IPV6_SIT))
3023 		return -ENODEV;
3024 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3025 		return -EFAULT;
3026 
3027 	rtnl_net_lock(net);
3028 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
3029 	if (dev && dev->type == ARPHRD_SIT)
3030 		err = addrconf_set_sit_dstaddr(net, dev, &ireq);
3031 	rtnl_net_unlock(net);
3032 	return err;
3033 }
3034 
3035 static int ipv6_mc_config(struct sock *sk, bool join,
3036 			  const struct in6_addr *addr, int ifindex)
3037 {
3038 	int ret;
3039 
3040 	ASSERT_RTNL();
3041 
3042 	lock_sock(sk);
3043 	if (join)
3044 		ret = ipv6_sock_mc_join(sk, ifindex, addr);
3045 	else
3046 		ret = ipv6_sock_mc_drop(sk, ifindex, addr);
3047 	release_sock(sk);
3048 
3049 	return ret;
3050 }
3051 
3052 /*
3053  *	Manual configuration of address on an interface
3054  */
3055 static int inet6_addr_add(struct net *net, struct net_device *dev,
3056 			  struct ifa6_config *cfg, clock_t expires, u32 flags,
3057 			  struct netlink_ext_ack *extack)
3058 {
3059 	struct inet6_ifaddr *ifp;
3060 	struct inet6_dev *idev;
3061 
3062 	ASSERT_RTNL_NET(net);
3063 
3064 	if (cfg->plen > 128) {
3065 		NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length");
3066 		return -EINVAL;
3067 	}
3068 
3069 	if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) {
3070 		NL_SET_ERR_MSG_MOD(extack, "address with \"mngtmpaddr\" flag must have a prefix length of 64");
3071 		return -EINVAL;
3072 	}
3073 
3074 	idev = addrconf_add_dev(dev);
3075 	if (IS_ERR(idev)) {
3076 		NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
3077 		return PTR_ERR(idev);
3078 	}
3079 
3080 	if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
3081 		int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
3082 					 true, cfg->pfx, dev->ifindex);
3083 
3084 		if (ret < 0) {
3085 			NL_SET_ERR_MSG_MOD(extack, "Multicast auto join failed");
3086 			return ret;
3087 		}
3088 	}
3089 
3090 	cfg->scope = ipv6_addr_scope(cfg->pfx);
3091 
3092 	ifp = ipv6_add_addr(idev, cfg, true, extack);
3093 	if (!IS_ERR(ifp)) {
3094 		if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
3095 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3096 					      ifp->rt_priority, dev, expires,
3097 					      flags, GFP_KERNEL);
3098 		}
3099 
3100 		/* Send a netlink notification if DAD is enabled and
3101 		 * optimistic flag is not set
3102 		 */
3103 		if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
3104 			ipv6_ifa_notify(0, ifp);
3105 		/*
3106 		 * Note that section 3.1 of RFC 4429 indicates
3107 		 * that the Optimistic flag should not be set for
3108 		 * manually configured addresses
3109 		 */
3110 		addrconf_dad_start(ifp);
3111 		if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
3112 			manage_tempaddrs(idev, ifp, cfg->valid_lft,
3113 					 cfg->preferred_lft, true, jiffies);
3114 		in6_ifa_put(ifp);
3115 		addrconf_verify_rtnl(net);
3116 		return 0;
3117 	} else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
3118 		ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
3119 			       cfg->pfx, dev->ifindex);
3120 	}
3121 
3122 	return PTR_ERR(ifp);
3123 }
3124 
3125 static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
3126 			  const struct in6_addr *pfx, unsigned int plen,
3127 			  struct netlink_ext_ack *extack)
3128 {
3129 	struct inet6_ifaddr *ifp;
3130 	struct inet6_dev *idev;
3131 	struct net_device *dev;
3132 
3133 	if (plen > 128) {
3134 		NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length");
3135 		return -EINVAL;
3136 	}
3137 
3138 	dev = __dev_get_by_index(net, ifindex);
3139 	if (!dev) {
3140 		NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface");
3141 		return -ENODEV;
3142 	}
3143 
3144 	idev = __in6_dev_get_rtnl_net(dev);
3145 	if (!idev) {
3146 		NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
3147 		return -ENXIO;
3148 	}
3149 
3150 	read_lock_bh(&idev->lock);
3151 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
3152 		if (ifp->prefix_len == plen &&
3153 		    ipv6_addr_equal(pfx, &ifp->addr)) {
3154 			in6_ifa_hold(ifp);
3155 			read_unlock_bh(&idev->lock);
3156 
3157 			if (!(ifp->flags & IFA_F_TEMPORARY) &&
3158 			    (ifp->flags & IFA_F_MANAGETEMPADDR))
3159 				delete_tempaddrs(idev, ifp);
3160 
3161 			ipv6_del_addr(ifp);
3162 
3163 			addrconf_verify_rtnl(net);
3164 			if (ipv6_addr_is_multicast(pfx)) {
3165 				ipv6_mc_config(net->ipv6.mc_autojoin_sk,
3166 					       false, pfx, dev->ifindex);
3167 			}
3168 			return 0;
3169 		}
3170 	}
3171 	read_unlock_bh(&idev->lock);
3172 
3173 	NL_SET_ERR_MSG_MOD(extack, "address not found");
3174 	return -EADDRNOTAVAIL;
3175 }
3176 
3177 
3178 int addrconf_add_ifaddr(struct net *net, void __user *arg)
3179 {
3180 	struct ifa6_config cfg = {
3181 		.ifa_flags = IFA_F_PERMANENT,
3182 		.preferred_lft = INFINITY_LIFE_TIME,
3183 		.valid_lft = INFINITY_LIFE_TIME,
3184 	};
3185 	struct net_device *dev;
3186 	struct in6_ifreq ireq;
3187 	int err;
3188 
3189 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3190 		return -EPERM;
3191 
3192 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3193 		return -EFAULT;
3194 
3195 	cfg.pfx = &ireq.ifr6_addr;
3196 	cfg.plen = ireq.ifr6_prefixlen;
3197 
3198 	rtnl_net_lock(net);
3199 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
3200 	if (dev) {
3201 		netdev_lock_ops(dev);
3202 		err = inet6_addr_add(net, dev, &cfg, 0, 0, NULL);
3203 		netdev_unlock_ops(dev);
3204 	} else {
3205 		err = -ENODEV;
3206 	}
3207 	rtnl_net_unlock(net);
3208 	return err;
3209 }
3210 
3211 int addrconf_del_ifaddr(struct net *net, void __user *arg)
3212 {
3213 	struct in6_ifreq ireq;
3214 	int err;
3215 
3216 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
3217 		return -EPERM;
3218 
3219 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3220 		return -EFAULT;
3221 
3222 	rtnl_net_lock(net);
3223 	err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
3224 			     ireq.ifr6_prefixlen, NULL);
3225 	rtnl_net_unlock(net);
3226 	return err;
3227 }
3228 
3229 static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
3230 		     int plen, int scope, u8 proto)
3231 {
3232 	struct inet6_ifaddr *ifp;
3233 	struct ifa6_config cfg = {
3234 		.pfx = addr,
3235 		.plen = plen,
3236 		.ifa_flags = IFA_F_PERMANENT,
3237 		.valid_lft = INFINITY_LIFE_TIME,
3238 		.preferred_lft = INFINITY_LIFE_TIME,
3239 		.scope = scope,
3240 		.ifa_proto = proto
3241 	};
3242 
3243 	ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3244 	if (!IS_ERR(ifp)) {
3245 		spin_lock_bh(&ifp->lock);
3246 		ifp->flags &= ~IFA_F_TENTATIVE;
3247 		spin_unlock_bh(&ifp->lock);
3248 		rt_genid_bump_ipv6(dev_net(idev->dev));
3249 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
3250 		in6_ifa_put(ifp);
3251 	}
3252 }
3253 
3254 #if IS_ENABLED(CONFIG_IPV6_SIT) || IS_ENABLED(CONFIG_NET_IPGRE)
3255 static void add_v4_addrs(struct inet6_dev *idev)
3256 {
3257 	struct in6_addr addr;
3258 	struct net_device *dev;
3259 	struct net *net = dev_net(idev->dev);
3260 	int scope, plen;
3261 	u32 pflags = 0;
3262 
3263 	ASSERT_RTNL();
3264 
3265 	memset(&addr, 0, sizeof(struct in6_addr));
3266 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
3267 
3268 	if (!(idev->dev->flags & IFF_POINTOPOINT) && idev->dev->type == ARPHRD_SIT) {
3269 		scope = IPV6_ADDR_COMPATv4;
3270 		plen = 96;
3271 		pflags |= RTF_NONEXTHOP;
3272 	} else {
3273 		if (idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_NONE)
3274 			return;
3275 
3276 		addr.s6_addr32[0] = htonl(0xfe800000);
3277 		scope = IFA_LINK;
3278 		plen = 64;
3279 	}
3280 
3281 	if (addr.s6_addr32[3]) {
3282 		add_addr(idev, &addr, plen, scope, IFAPROT_UNSPEC);
3283 		addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
3284 				      GFP_KERNEL);
3285 		return;
3286 	}
3287 
3288 	for_each_netdev(net, dev) {
3289 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
3290 		if (in_dev && (dev->flags & IFF_UP)) {
3291 			struct in_ifaddr *ifa;
3292 			int flag = scope;
3293 
3294 			in_dev_for_each_ifa_rtnl(ifa, in_dev) {
3295 				addr.s6_addr32[3] = ifa->ifa_local;
3296 
3297 				if (ifa->ifa_scope == RT_SCOPE_LINK)
3298 					continue;
3299 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
3300 					if (idev->dev->flags&IFF_POINTOPOINT)
3301 						continue;
3302 					flag |= IFA_HOST;
3303 				}
3304 
3305 				add_addr(idev, &addr, plen, flag,
3306 					 IFAPROT_UNSPEC);
3307 				addrconf_prefix_route(&addr, plen, 0, idev->dev,
3308 						      0, pflags, GFP_KERNEL);
3309 			}
3310 		}
3311 	}
3312 }
3313 #endif
3314 
3315 static void init_loopback(struct net_device *dev)
3316 {
3317 	struct inet6_dev  *idev;
3318 
3319 	/* ::1 */
3320 
3321 	ASSERT_RTNL();
3322 
3323 	idev = ipv6_find_idev(dev);
3324 	if (IS_ERR(idev)) {
3325 		pr_debug("%s: add_dev failed\n", __func__);
3326 		return;
3327 	}
3328 
3329 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFAPROT_KERNEL_LO);
3330 }
3331 
3332 void addrconf_add_linklocal(struct inet6_dev *idev,
3333 			    const struct in6_addr *addr, u32 flags)
3334 {
3335 	struct ifa6_config cfg = {
3336 		.pfx = addr,
3337 		.plen = 64,
3338 		.ifa_flags = flags | IFA_F_PERMANENT,
3339 		.valid_lft = INFINITY_LIFE_TIME,
3340 		.preferred_lft = INFINITY_LIFE_TIME,
3341 		.scope = IFA_LINK,
3342 		.ifa_proto = IFAPROT_KERNEL_LL
3343 	};
3344 	struct inet6_ifaddr *ifp;
3345 
3346 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3347 	if ((READ_ONCE(dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad) ||
3348 	     READ_ONCE(idev->cnf.optimistic_dad)) &&
3349 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
3350 		cfg.ifa_flags |= IFA_F_OPTIMISTIC;
3351 #endif
3352 
3353 	ifp = ipv6_add_addr(idev, &cfg, true, NULL);
3354 	if (!IS_ERR(ifp)) {
3355 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
3356 				      0, 0, GFP_ATOMIC);
3357 		addrconf_dad_start(ifp);
3358 		in6_ifa_put(ifp);
3359 	}
3360 }
3361 EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
3362 
3363 static bool ipv6_reserved_interfaceid(struct in6_addr address)
3364 {
3365 	if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3366 		return true;
3367 
3368 	if (address.s6_addr32[2] == htonl(0x02005eff) &&
3369 	    ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3370 		return true;
3371 
3372 	if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3373 	    ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3374 		return true;
3375 
3376 	return false;
3377 }
3378 
3379 static int ipv6_generate_stable_address(struct in6_addr *address,
3380 					u8 dad_count,
3381 					const struct inet6_dev *idev)
3382 {
3383 	static DEFINE_SPINLOCK(lock);
3384 	static struct sha1_ctx sha_ctx;
3385 
3386 	static union {
3387 		u8 __data[SHA1_BLOCK_SIZE];
3388 		struct {
3389 			struct in6_addr secret;
3390 			__be32 prefix[2];
3391 			unsigned char hwaddr[MAX_ADDR_LEN];
3392 			u8 dad_count;
3393 		} __packed;
3394 	} data;
3395 
3396 	struct in6_addr secret;
3397 	struct in6_addr temp;
3398 	struct net *net = dev_net(idev->dev);
3399 
3400 	BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3401 
3402 	if (idev->cnf.stable_secret.initialized)
3403 		secret = idev->cnf.stable_secret.secret;
3404 	else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3405 		secret = net->ipv6.devconf_dflt->stable_secret.secret;
3406 	else
3407 		return -1;
3408 
3409 retry:
3410 	spin_lock_bh(&lock);
3411 
3412 	sha1_init(&sha_ctx);
3413 
3414 	memset(&data, 0, sizeof(data));
3415 	memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
3416 	data.prefix[0] = address->s6_addr32[0];
3417 	data.prefix[1] = address->s6_addr32[1];
3418 	data.secret = secret;
3419 	data.dad_count = dad_count;
3420 
3421 	sha1_update(&sha_ctx, data.__data, sizeof(data));
3422 
3423 	/*
3424 	 * Note that the SHA-1 finalization is omitted here, and the digest is
3425 	 * pulled directly from the internal SHA-1 state (making it incompatible
3426 	 * with standard SHA-1).  Unusual, but technically okay since the data
3427 	 * length is fixed and is a multiple of the SHA-1 block size.
3428 	 */
3429 	temp = *address;
3430 	temp.s6_addr32[2] = (__force __be32)sha_ctx.state.h[0];
3431 	temp.s6_addr32[3] = (__force __be32)sha_ctx.state.h[1];
3432 
3433 	spin_unlock_bh(&lock);
3434 
3435 	if (ipv6_reserved_interfaceid(temp)) {
3436 		dad_count++;
3437 		if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
3438 			return -1;
3439 		goto retry;
3440 	}
3441 
3442 	*address = temp;
3443 	return 0;
3444 }
3445 
3446 static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3447 {
3448 	struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3449 
3450 	if (s->initialized)
3451 		return;
3452 	s = &idev->cnf.stable_secret;
3453 	get_random_bytes(&s->secret, sizeof(s->secret));
3454 	s->initialized = true;
3455 }
3456 
3457 static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3458 {
3459 	struct in6_addr addr;
3460 
3461 	/* no link local addresses on L3 master devices */
3462 	if (netif_is_l3_master(idev->dev))
3463 		return;
3464 
3465 	/* no link local addresses on devices flagged as slaves */
3466 	if (idev->dev->priv_flags & IFF_NO_ADDRCONF)
3467 		return;
3468 
3469 	ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3470 
3471 	switch (idev->cnf.addr_gen_mode) {
3472 	case IN6_ADDR_GEN_MODE_RANDOM:
3473 		ipv6_gen_mode_random_init(idev);
3474 		fallthrough;
3475 	case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
3476 		if (!ipv6_generate_stable_address(&addr, 0, idev))
3477 			addrconf_add_linklocal(idev, &addr,
3478 					       IFA_F_STABLE_PRIVACY);
3479 		else if (prefix_route)
3480 			addrconf_prefix_route(&addr, 64, 0, idev->dev,
3481 					      0, 0, GFP_KERNEL);
3482 		break;
3483 	case IN6_ADDR_GEN_MODE_EUI64:
3484 		/* addrconf_add_linklocal also adds a prefix_route and we
3485 		 * only need to care about prefix routes if ipv6_generate_eui64
3486 		 * couldn't generate one.
3487 		 */
3488 		if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
3489 			addrconf_add_linklocal(idev, &addr, 0);
3490 		else if (prefix_route)
3491 			addrconf_prefix_route(&addr, 64, 0, idev->dev,
3492 					      0, 0, GFP_KERNEL);
3493 		break;
3494 	case IN6_ADDR_GEN_MODE_NONE:
3495 	default:
3496 		/* will not add any link local address */
3497 		break;
3498 	}
3499 }
3500 
3501 static void addrconf_dev_config(struct net_device *dev)
3502 {
3503 	struct inet6_dev *idev;
3504 
3505 	ASSERT_RTNL();
3506 
3507 	if ((dev->type != ARPHRD_ETHER) &&
3508 	    (dev->type != ARPHRD_FDDI) &&
3509 	    (dev->type != ARPHRD_ARCNET) &&
3510 	    (dev->type != ARPHRD_INFINIBAND) &&
3511 	    (dev->type != ARPHRD_IEEE1394) &&
3512 	    (dev->type != ARPHRD_TUNNEL6) &&
3513 	    (dev->type != ARPHRD_6LOWPAN) &&
3514 	    (dev->type != ARPHRD_IP6GRE) &&
3515 	    (dev->type != ARPHRD_TUNNEL) &&
3516 	    (dev->type != ARPHRD_NONE) &&
3517 	    (dev->type != ARPHRD_RAWIP)) {
3518 		/* Alas, we support only Ethernet autoconfiguration. */
3519 		idev = __in6_dev_get(dev);
3520 		if (!IS_ERR_OR_NULL(idev) && dev->flags & IFF_UP &&
3521 		    dev->flags & IFF_MULTICAST)
3522 			ipv6_mc_up(idev);
3523 		return;
3524 	}
3525 
3526 	idev = addrconf_add_dev(dev);
3527 	if (IS_ERR(idev))
3528 		return;
3529 
3530 	/* this device type has no EUI support */
3531 	if (dev->type == ARPHRD_NONE &&
3532 	    idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3533 		WRITE_ONCE(idev->cnf.addr_gen_mode,
3534 			   IN6_ADDR_GEN_MODE_RANDOM);
3535 
3536 	addrconf_addr_gen(idev, false);
3537 }
3538 
3539 #if IS_ENABLED(CONFIG_IPV6_SIT)
3540 static void addrconf_sit_config(struct net_device *dev)
3541 {
3542 	struct inet6_dev *idev;
3543 
3544 	ASSERT_RTNL();
3545 
3546 	/*
3547 	 * Configure the tunnel with one of our IPv4
3548 	 * addresses... we should configure all of
3549 	 * our v4 addrs in the tunnel
3550 	 */
3551 
3552 	idev = ipv6_find_idev(dev);
3553 	if (IS_ERR(idev)) {
3554 		pr_debug("%s: add_dev failed\n", __func__);
3555 		return;
3556 	}
3557 
3558 	if (dev->priv_flags & IFF_ISATAP) {
3559 		addrconf_addr_gen(idev, false);
3560 		return;
3561 	}
3562 
3563 	add_v4_addrs(idev);
3564 
3565 	if (dev->flags&IFF_POINTOPOINT)
3566 		addrconf_add_mroute(dev);
3567 }
3568 #endif
3569 
3570 #if IS_ENABLED(CONFIG_NET_IPGRE)
3571 static void addrconf_gre_config(struct net_device *dev)
3572 {
3573 	struct inet6_dev *idev;
3574 
3575 	ASSERT_RTNL();
3576 
3577 	idev = addrconf_add_dev(dev);
3578 	if (IS_ERR(idev))
3579 		return;
3580 
3581 	/* Generate the IPv6 link-local address using addrconf_addr_gen(),
3582 	 * unless we have an IPv4 GRE device not bound to an IP address and
3583 	 * which is in EUI64 mode (as __ipv6_isatap_ifid() would fail in this
3584 	 * case). Such devices fall back to add_v4_addrs() instead.
3585 	 */
3586 	if (!(*(__be32 *)dev->dev_addr == 0 &&
3587 	      idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)) {
3588 		addrconf_addr_gen(idev, true);
3589 		return;
3590 	}
3591 
3592 	add_v4_addrs(idev);
3593 }
3594 #endif
3595 
3596 static void addrconf_init_auto_addrs(struct net_device *dev)
3597 {
3598 	switch (dev->type) {
3599 #if IS_ENABLED(CONFIG_IPV6_SIT)
3600 	case ARPHRD_SIT:
3601 		addrconf_sit_config(dev);
3602 		break;
3603 #endif
3604 #if IS_ENABLED(CONFIG_NET_IPGRE)
3605 	case ARPHRD_IPGRE:
3606 		addrconf_gre_config(dev);
3607 		break;
3608 #endif
3609 	case ARPHRD_LOOPBACK:
3610 		init_loopback(dev);
3611 		break;
3612 
3613 	default:
3614 		addrconf_dev_config(dev);
3615 		break;
3616 	}
3617 }
3618 
3619 static int fixup_permanent_addr(struct net *net,
3620 				struct inet6_dev *idev,
3621 				struct inet6_ifaddr *ifp)
3622 {
3623 	/* !fib6_node means the host route was removed from the
3624 	 * FIB, for example, if 'lo' device is taken down. In that
3625 	 * case regenerate the host route.
3626 	 */
3627 	if (!ifp->rt || !ifp->rt->fib6_node) {
3628 		struct fib6_info *f6i, *prev;
3629 
3630 		f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3631 					 GFP_KERNEL, NULL);
3632 		if (IS_ERR(f6i))
3633 			return PTR_ERR(f6i);
3634 
3635 		/* ifp->rt can be accessed outside of rtnl */
3636 		spin_lock_bh(&ifp->lock);
3637 		prev = ifp->rt;
3638 		ifp->rt = f6i;
3639 		spin_unlock_bh(&ifp->lock);
3640 
3641 		fib6_info_release(prev);
3642 	}
3643 
3644 	if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3645 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3646 				      ifp->rt_priority, idev->dev, 0, 0,
3647 				      GFP_KERNEL);
3648 	}
3649 
3650 	if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3651 		addrconf_dad_start(ifp);
3652 
3653 	return 0;
3654 }
3655 
3656 static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
3657 {
3658 	struct inet6_ifaddr *ifp;
3659 	LIST_HEAD(tmp_addr_list);
3660 	struct inet6_dev *idev;
3661 
3662 	/* Mutual exclusion with other if_list_aux users. */
3663 	ASSERT_RTNL();
3664 
3665 	idev = __in6_dev_get(dev);
3666 	if (!idev)
3667 		return;
3668 
3669 	write_lock_bh(&idev->lock);
3670 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
3671 		if (ifp->flags & IFA_F_PERMANENT)
3672 			list_add_tail(&ifp->if_list_aux, &tmp_addr_list);
3673 	}
3674 	write_unlock_bh(&idev->lock);
3675 
3676 	while (!list_empty(&tmp_addr_list)) {
3677 		ifp = list_first_entry(&tmp_addr_list,
3678 				       struct inet6_ifaddr, if_list_aux);
3679 		list_del(&ifp->if_list_aux);
3680 
3681 		if (fixup_permanent_addr(net, idev, ifp) < 0) {
3682 			net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3683 					     idev->dev->name, &ifp->addr);
3684 			in6_ifa_hold(ifp);
3685 			ipv6_del_addr(ifp);
3686 		}
3687 	}
3688 }
3689 
3690 static int addrconf_notify(struct notifier_block *this, unsigned long event,
3691 			   void *ptr)
3692 {
3693 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3694 	struct netdev_notifier_change_info *change_info;
3695 	struct netdev_notifier_changeupper_info *info;
3696 	struct inet6_dev *idev = __in6_dev_get(dev);
3697 	struct net *net = dev_net(dev);
3698 	int run_pending = 0;
3699 	int err;
3700 
3701 	switch (event) {
3702 	case NETDEV_REGISTER:
3703 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3704 			idev = ipv6_add_dev(dev);
3705 			if (IS_ERR(idev))
3706 				return notifier_from_errno(PTR_ERR(idev));
3707 		}
3708 		break;
3709 
3710 	case NETDEV_CHANGEMTU:
3711 		/* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3712 		if (dev->mtu < IPV6_MIN_MTU) {
3713 			addrconf_ifdown(dev, dev != net->loopback_dev);
3714 			break;
3715 		}
3716 
3717 		if (idev) {
3718 			rt6_mtu_change(dev, dev->mtu);
3719 			WRITE_ONCE(idev->cnf.mtu6, dev->mtu);
3720 			break;
3721 		}
3722 
3723 		/* allocate new idev */
3724 		idev = ipv6_add_dev(dev);
3725 		if (IS_ERR(idev))
3726 			break;
3727 
3728 		/* device is still not ready */
3729 		if (!(idev->if_flags & IF_READY))
3730 			break;
3731 
3732 		run_pending = 1;
3733 		fallthrough;
3734 	case NETDEV_UP:
3735 	case NETDEV_CHANGE:
3736 		if (idev && idev->cnf.disable_ipv6)
3737 			break;
3738 
3739 		if (dev->priv_flags & IFF_NO_ADDRCONF) {
3740 			if (event == NETDEV_UP && !IS_ERR_OR_NULL(idev) &&
3741 			    dev->flags & IFF_UP && dev->flags & IFF_MULTICAST)
3742 				ipv6_mc_up(idev);
3743 			break;
3744 		}
3745 
3746 		if (event == NETDEV_UP) {
3747 			/* restore routes for permanent addresses */
3748 			addrconf_permanent_addr(net, dev);
3749 
3750 			if (!addrconf_link_ready(dev)) {
3751 				/* device is not ready yet. */
3752 				pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3753 					 dev->name);
3754 				break;
3755 			}
3756 
3757 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
3758 				idev = ipv6_add_dev(dev);
3759 
3760 			if (!IS_ERR_OR_NULL(idev)) {
3761 				idev->if_flags |= IF_READY;
3762 				run_pending = 1;
3763 			}
3764 		} else if (event == NETDEV_CHANGE) {
3765 			if (!addrconf_link_ready(dev)) {
3766 				/* device is still not ready. */
3767 				rt6_sync_down_dev(dev, event);
3768 				break;
3769 			}
3770 
3771 			if (!IS_ERR_OR_NULL(idev)) {
3772 				if (idev->if_flags & IF_READY) {
3773 					/* device is already configured -
3774 					 * but resend MLD reports, we might
3775 					 * have roamed and need to update
3776 					 * multicast snooping switches
3777 					 */
3778 					ipv6_mc_up(idev);
3779 					change_info = ptr;
3780 					if (change_info->flags_changed & IFF_NOARP)
3781 						addrconf_dad_run(idev, true);
3782 					rt6_sync_up(dev, RTNH_F_LINKDOWN);
3783 					break;
3784 				}
3785 				idev->if_flags |= IF_READY;
3786 			}
3787 
3788 			pr_debug("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3789 				 dev->name);
3790 
3791 			run_pending = 1;
3792 		}
3793 
3794 		addrconf_init_auto_addrs(dev);
3795 
3796 		if (!IS_ERR_OR_NULL(idev)) {
3797 			if (run_pending)
3798 				addrconf_dad_run(idev, false);
3799 
3800 			/* Device has an address by now */
3801 			rt6_sync_up(dev, RTNH_F_DEAD);
3802 
3803 			/*
3804 			 * If the MTU changed during the interface down,
3805 			 * when the interface up, the changed MTU must be
3806 			 * reflected in the idev as well as routers.
3807 			 */
3808 			if (idev->cnf.mtu6 != dev->mtu &&
3809 			    dev->mtu >= IPV6_MIN_MTU) {
3810 				rt6_mtu_change(dev, dev->mtu);
3811 				WRITE_ONCE(idev->cnf.mtu6, dev->mtu);
3812 			}
3813 			WRITE_ONCE(idev->tstamp, jiffies);
3814 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
3815 
3816 			/*
3817 			 * If the changed mtu during down is lower than
3818 			 * IPV6_MIN_MTU stop IPv6 on this interface.
3819 			 */
3820 			if (dev->mtu < IPV6_MIN_MTU)
3821 				addrconf_ifdown(dev, dev != net->loopback_dev);
3822 		}
3823 		break;
3824 
3825 	case NETDEV_DOWN:
3826 	case NETDEV_UNREGISTER:
3827 		/*
3828 		 *	Remove all addresses from this interface.
3829 		 */
3830 		addrconf_ifdown(dev, event != NETDEV_DOWN);
3831 		break;
3832 
3833 	case NETDEV_CHANGENAME:
3834 		if (idev) {
3835 			snmp6_unregister_dev(idev);
3836 			addrconf_sysctl_unregister(idev);
3837 			err = addrconf_sysctl_register(idev);
3838 			if (err)
3839 				return notifier_from_errno(err);
3840 			err = snmp6_register_dev(idev);
3841 			if (err) {
3842 				addrconf_sysctl_unregister(idev);
3843 				return notifier_from_errno(err);
3844 			}
3845 		}
3846 		break;
3847 
3848 	case NETDEV_PRE_TYPE_CHANGE:
3849 	case NETDEV_POST_TYPE_CHANGE:
3850 		if (idev)
3851 			addrconf_type_change(dev, event);
3852 		break;
3853 
3854 	case NETDEV_CHANGEUPPER:
3855 		info = ptr;
3856 
3857 		/* flush all routes if dev is linked to or unlinked from
3858 		 * an L3 master device (e.g., VRF)
3859 		 */
3860 		if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3861 			addrconf_ifdown(dev, false);
3862 	}
3863 
3864 	return NOTIFY_OK;
3865 }
3866 
3867 /*
3868  *	addrconf module should be notified of a device going up
3869  */
3870 static struct notifier_block ipv6_dev_notf = {
3871 	.notifier_call = addrconf_notify,
3872 	.priority = ADDRCONF_NOTIFY_PRIORITY,
3873 };
3874 
3875 static void addrconf_type_change(struct net_device *dev, unsigned long event)
3876 {
3877 	struct inet6_dev *idev;
3878 	ASSERT_RTNL();
3879 
3880 	idev = __in6_dev_get(dev);
3881 
3882 	if (event == NETDEV_POST_TYPE_CHANGE)
3883 		ipv6_mc_remap(idev);
3884 	else if (event == NETDEV_PRE_TYPE_CHANGE)
3885 		ipv6_mc_unmap(idev);
3886 }
3887 
3888 static bool addr_is_local(const struct in6_addr *addr)
3889 {
3890 	return ipv6_addr_type(addr) &
3891 		(IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3892 }
3893 
3894 static int addrconf_ifdown(struct net_device *dev, bool unregister)
3895 {
3896 	unsigned long event = unregister ? NETDEV_UNREGISTER : NETDEV_DOWN;
3897 	struct net *net = dev_net(dev);
3898 	struct inet6_dev *idev;
3899 	struct inet6_ifaddr *ifa;
3900 	LIST_HEAD(tmp_addr_list);
3901 	bool keep_addr = false;
3902 	bool was_ready;
3903 	int state, i;
3904 
3905 	ASSERT_RTNL();
3906 
3907 	rt6_disable_ip(dev, event);
3908 
3909 	idev = __in6_dev_get(dev);
3910 	if (!idev)
3911 		return -ENODEV;
3912 
3913 	/*
3914 	 * Step 1: remove reference to ipv6 device from parent device.
3915 	 *	   Do not dev_put!
3916 	 */
3917 	if (unregister) {
3918 		write_lock_bh(&idev->lock);
3919 		WRITE_ONCE(idev->dead, 1);
3920 		write_unlock_bh(&idev->lock);
3921 
3922 		/* protected by rtnl_lock */
3923 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
3924 
3925 		/* Step 1.5: remove snmp6 entry */
3926 		snmp6_unregister_dev(idev);
3927 
3928 	}
3929 
3930 	/* combine the user config with event to determine if permanent
3931 	 * addresses are to be removed from address hash table
3932 	 */
3933 	if (!unregister && !idev->cnf.disable_ipv6) {
3934 		/* aggregate the system setting and interface setting */
3935 		int _keep_addr = READ_ONCE(net->ipv6.devconf_all->keep_addr_on_down);
3936 
3937 		if (!_keep_addr)
3938 			_keep_addr = READ_ONCE(idev->cnf.keep_addr_on_down);
3939 
3940 		keep_addr = (_keep_addr > 0);
3941 	}
3942 
3943 	/* Step 2: clear hash table */
3944 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3945 		struct hlist_head *h = &net->ipv6.inet6_addr_lst[i];
3946 
3947 		spin_lock_bh(&net->ipv6.addrconf_hash_lock);
3948 restart:
3949 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3950 			if (ifa->idev == idev) {
3951 				addrconf_del_dad_work(ifa);
3952 				/* combined flag + permanent flag decide if
3953 				 * address is retained on a down event
3954 				 */
3955 				if (!keep_addr ||
3956 				    !(ifa->flags & IFA_F_PERMANENT) ||
3957 				    addr_is_local(&ifa->addr)) {
3958 					hlist_del_init_rcu(&ifa->addr_lst);
3959 					goto restart;
3960 				}
3961 			}
3962 		}
3963 		spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
3964 	}
3965 
3966 	write_lock_bh(&idev->lock);
3967 
3968 	addrconf_del_rs_timer(idev);
3969 
3970 	/* Step 2: clear flags for stateless addrconf, repeated down
3971 	 *         detection
3972 	 */
3973 	was_ready = idev->if_flags & IF_READY;
3974 	if (!unregister)
3975 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
3976 
3977 	/* Step 3: clear tempaddr list */
3978 	while (!list_empty(&idev->tempaddr_list)) {
3979 		ifa = list_first_entry(&idev->tempaddr_list,
3980 				       struct inet6_ifaddr, tmp_list);
3981 		list_del(&ifa->tmp_list);
3982 		write_unlock_bh(&idev->lock);
3983 		spin_lock_bh(&ifa->lock);
3984 
3985 		if (ifa->ifpub) {
3986 			in6_ifa_put(ifa->ifpub);
3987 			ifa->ifpub = NULL;
3988 		}
3989 		spin_unlock_bh(&ifa->lock);
3990 		in6_ifa_put(ifa);
3991 		write_lock_bh(&idev->lock);
3992 	}
3993 
3994 	list_for_each_entry(ifa, &idev->addr_list, if_list)
3995 		list_add_tail(&ifa->if_list_aux, &tmp_addr_list);
3996 	write_unlock_bh(&idev->lock);
3997 
3998 	while (!list_empty(&tmp_addr_list)) {
3999 		struct fib6_info *rt = NULL;
4000 		bool keep;
4001 
4002 		ifa = list_first_entry(&tmp_addr_list,
4003 				       struct inet6_ifaddr, if_list_aux);
4004 		list_del(&ifa->if_list_aux);
4005 
4006 		addrconf_del_dad_work(ifa);
4007 
4008 		keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
4009 			!addr_is_local(&ifa->addr);
4010 
4011 		spin_lock_bh(&ifa->lock);
4012 
4013 		if (keep) {
4014 			/* set state to skip the notifier below */
4015 			state = INET6_IFADDR_STATE_DEAD;
4016 			ifa->state = INET6_IFADDR_STATE_PREDAD;
4017 			if (!(ifa->flags & IFA_F_NODAD))
4018 				ifa->flags |= IFA_F_TENTATIVE;
4019 
4020 			rt = ifa->rt;
4021 			ifa->rt = NULL;
4022 		} else {
4023 			state = ifa->state;
4024 			ifa->state = INET6_IFADDR_STATE_DEAD;
4025 		}
4026 
4027 		spin_unlock_bh(&ifa->lock);
4028 
4029 		if (rt)
4030 			ip6_del_rt(net, rt, false);
4031 
4032 		if (state != INET6_IFADDR_STATE_DEAD) {
4033 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
4034 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
4035 		} else {
4036 			if (idev->cnf.forwarding)
4037 				addrconf_leave_anycast(ifa);
4038 			addrconf_leave_solict(ifa->idev, &ifa->addr);
4039 		}
4040 
4041 		if (!keep) {
4042 			spin_lock_bh(&net->ipv6.addrconf_hash_lock);
4043 			hlist_del_init_rcu(&ifa->addr_lst);
4044 			spin_unlock_bh(&net->ipv6.addrconf_hash_lock);
4045 
4046 			write_lock_bh(&idev->lock);
4047 			list_del_rcu(&ifa->if_list);
4048 			write_unlock_bh(&idev->lock);
4049 			in6_ifa_put(ifa);
4050 		}
4051 	}
4052 
4053 	/* Step 5: Discard anycast and multicast list */
4054 	if (unregister) {
4055 		ipv6_ac_destroy_dev(idev);
4056 		ipv6_mc_destroy_dev(idev);
4057 	} else if (was_ready) {
4058 		ipv6_mc_down(idev);
4059 	}
4060 
4061 	WRITE_ONCE(idev->tstamp, jiffies);
4062 	idev->ra_mtu = 0;
4063 
4064 	/* Last: Shot the device (if unregistered) */
4065 	if (unregister) {
4066 		struct neigh_table *tbl = nd_table(net);
4067 
4068 		addrconf_sysctl_unregister(idev);
4069 		neigh_parms_release(tbl, idev->nd_parms);
4070 		neigh_ifdown(tbl, dev);
4071 		in6_dev_put(idev);
4072 	}
4073 	return 0;
4074 }
4075 
4076 static void addrconf_rs_timer(struct timer_list *t)
4077 {
4078 	struct inet6_dev *idev = timer_container_of(idev, t, rs_timer);
4079 	struct net_device *dev = idev->dev;
4080 	struct in6_addr lladdr;
4081 	int rtr_solicits;
4082 
4083 	write_lock(&idev->lock);
4084 	if (idev->dead || !(idev->if_flags & IF_READY))
4085 		goto out;
4086 
4087 	if (!ipv6_accept_ra(idev))
4088 		goto out;
4089 
4090 	/* Announcement received after solicitation was sent */
4091 	if (idev->if_flags & IF_RA_RCVD)
4092 		goto out;
4093 
4094 	rtr_solicits = READ_ONCE(idev->cnf.rtr_solicits);
4095 
4096 	if (idev->rs_probes++ < rtr_solicits || rtr_solicits < 0) {
4097 		write_unlock(&idev->lock);
4098 		if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4099 			ndisc_send_rs(dev, &lladdr,
4100 				      &in6addr_linklocal_allrouters);
4101 		else
4102 			goto put;
4103 
4104 		write_lock(&idev->lock);
4105 		idev->rs_interval = rfc3315_s14_backoff_update(
4106 				idev->rs_interval,
4107 				READ_ONCE(idev->cnf.rtr_solicit_max_interval));
4108 		/* The wait after the last probe can be shorter */
4109 		addrconf_mod_rs_timer(idev, (idev->rs_probes ==
4110 					     READ_ONCE(idev->cnf.rtr_solicits)) ?
4111 				      READ_ONCE(idev->cnf.rtr_solicit_delay) :
4112 				      idev->rs_interval);
4113 	} else {
4114 		/*
4115 		 * Note: we do not support deprecated "all on-link"
4116 		 * assumption any longer.
4117 		 */
4118 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
4119 	}
4120 
4121 out:
4122 	write_unlock(&idev->lock);
4123 put:
4124 	in6_dev_put(idev);
4125 }
4126 
4127 /*
4128  *	Duplicate Address Detection
4129  */
4130 static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
4131 {
4132 	struct inet6_dev *idev = ifp->idev;
4133 	unsigned long rand_num;
4134 	u64 nonce;
4135 
4136 	if (ifp->flags & IFA_F_OPTIMISTIC)
4137 		rand_num = 0;
4138 	else
4139 		rand_num = get_random_u32_below(
4140 				READ_ONCE(idev->cnf.rtr_solicit_delay) ? : 1);
4141 
4142 	nonce = 0;
4143 	if (READ_ONCE(idev->cnf.enhanced_dad) ||
4144 	    READ_ONCE(dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad)) {
4145 		do
4146 			get_random_bytes(&nonce, 6);
4147 		while (nonce == 0);
4148 	}
4149 	ifp->dad_nonce = nonce;
4150 	ifp->dad_probes = READ_ONCE(idev->cnf.dad_transmits);
4151 	addrconf_mod_dad_work(ifp, rand_num);
4152 }
4153 
4154 static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
4155 {
4156 	struct inet6_dev *idev = ifp->idev;
4157 	struct net_device *dev = idev->dev;
4158 	bool bump_id, notify = false;
4159 	struct net *net;
4160 
4161 	addrconf_join_solict(dev, &ifp->addr);
4162 
4163 	read_lock_bh(&idev->lock);
4164 	spin_lock(&ifp->lock);
4165 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
4166 		goto out;
4167 
4168 	net = dev_net(dev);
4169 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
4170 	    (READ_ONCE(net->ipv6.devconf_all->accept_dad) < 1 &&
4171 	     READ_ONCE(idev->cnf.accept_dad) < 1) ||
4172 	    !(ifp->flags&IFA_F_TENTATIVE) ||
4173 	    ifp->flags & IFA_F_NODAD) {
4174 		bool send_na = false;
4175 
4176 		if (ifp->flags & IFA_F_TENTATIVE &&
4177 		    !(ifp->flags & IFA_F_OPTIMISTIC))
4178 			send_na = true;
4179 		bump_id = ifp->flags & IFA_F_TENTATIVE;
4180 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4181 		spin_unlock(&ifp->lock);
4182 		read_unlock_bh(&idev->lock);
4183 
4184 		addrconf_dad_completed(ifp, bump_id, send_na);
4185 		return;
4186 	}
4187 
4188 	if (!(idev->if_flags & IF_READY)) {
4189 		spin_unlock(&ifp->lock);
4190 		read_unlock_bh(&idev->lock);
4191 		/*
4192 		 * If the device is not ready:
4193 		 * - keep it tentative if it is a permanent address.
4194 		 * - otherwise, kill it.
4195 		 */
4196 		in6_ifa_hold(ifp);
4197 		addrconf_dad_stop(ifp, 0);
4198 		return;
4199 	}
4200 
4201 	/*
4202 	 * Optimistic nodes can start receiving
4203 	 * Frames right away
4204 	 */
4205 	if (ifp->flags & IFA_F_OPTIMISTIC) {
4206 		ip6_ins_rt(net, ifp->rt);
4207 		if (ipv6_use_optimistic_addr(net, idev)) {
4208 			/* Because optimistic nodes can use this address,
4209 			 * notify listeners. If DAD fails, RTM_DELADDR is sent.
4210 			 */
4211 			notify = true;
4212 		}
4213 	}
4214 
4215 	addrconf_dad_kick(ifp);
4216 out:
4217 	spin_unlock(&ifp->lock);
4218 	read_unlock_bh(&idev->lock);
4219 	if (notify)
4220 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
4221 }
4222 
4223 static void addrconf_dad_start(struct inet6_ifaddr *ifp)
4224 {
4225 	bool begin_dad = false;
4226 
4227 	spin_lock_bh(&ifp->lock);
4228 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4229 		ifp->state = INET6_IFADDR_STATE_PREDAD;
4230 		begin_dad = true;
4231 	}
4232 	spin_unlock_bh(&ifp->lock);
4233 
4234 	if (begin_dad)
4235 		addrconf_mod_dad_work(ifp, 0);
4236 }
4237 
4238 static void addrconf_dad_work(struct work_struct *w)
4239 {
4240 	struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4241 						struct inet6_ifaddr,
4242 						dad_work);
4243 	struct inet6_dev *idev = ifp->idev;
4244 	bool bump_id, disable_ipv6 = false;
4245 	struct in6_addr mcaddr;
4246 	struct net *net;
4247 
4248 	enum {
4249 		DAD_PROCESS,
4250 		DAD_BEGIN,
4251 		DAD_ABORT,
4252 	} action = DAD_PROCESS;
4253 
4254 	net = dev_net(idev->dev);
4255 
4256 	rtnl_net_lock(net);
4257 
4258 	spin_lock_bh(&ifp->lock);
4259 	if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4260 		action = DAD_BEGIN;
4261 		ifp->state = INET6_IFADDR_STATE_DAD;
4262 	} else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4263 		action = DAD_ABORT;
4264 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
4265 
4266 		if ((READ_ONCE(net->ipv6.devconf_all->accept_dad) > 1 ||
4267 		     READ_ONCE(idev->cnf.accept_dad) > 1) &&
4268 		    !idev->cnf.disable_ipv6 &&
4269 		    !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
4270 			struct in6_addr addr;
4271 
4272 			addr.s6_addr32[0] = htonl(0xfe800000);
4273 			addr.s6_addr32[1] = 0;
4274 
4275 			if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
4276 			    ipv6_addr_equal(&ifp->addr, &addr)) {
4277 				/* DAD failed for link-local based on MAC */
4278 				WRITE_ONCE(idev->cnf.disable_ipv6, 1);
4279 
4280 				pr_info("%s: IPv6 being disabled!\n",
4281 					ifp->idev->dev->name);
4282 				disable_ipv6 = true;
4283 			}
4284 		}
4285 	}
4286 	spin_unlock_bh(&ifp->lock);
4287 
4288 	if (action == DAD_BEGIN) {
4289 		addrconf_dad_begin(ifp);
4290 		goto out;
4291 	} else if (action == DAD_ABORT) {
4292 		in6_ifa_hold(ifp);
4293 		addrconf_dad_stop(ifp, 1);
4294 		if (disable_ipv6)
4295 			addrconf_ifdown(idev->dev, false);
4296 		goto out;
4297 	}
4298 
4299 	if (!ifp->dad_probes && addrconf_dad_end(ifp))
4300 		goto out;
4301 
4302 	write_lock_bh(&idev->lock);
4303 	if (idev->dead || !(idev->if_flags & IF_READY)) {
4304 		write_unlock_bh(&idev->lock);
4305 		goto out;
4306 	}
4307 
4308 	spin_lock(&ifp->lock);
4309 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4310 		spin_unlock(&ifp->lock);
4311 		write_unlock_bh(&idev->lock);
4312 		goto out;
4313 	}
4314 
4315 	if (ifp->dad_probes == 0) {
4316 		bool send_na = false;
4317 
4318 		/*
4319 		 * DAD was successful
4320 		 */
4321 
4322 		if (ifp->flags & IFA_F_TENTATIVE &&
4323 		    !(ifp->flags & IFA_F_OPTIMISTIC))
4324 			send_na = true;
4325 		bump_id = ifp->flags & IFA_F_TENTATIVE;
4326 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
4327 		spin_unlock(&ifp->lock);
4328 		write_unlock_bh(&idev->lock);
4329 
4330 		addrconf_dad_completed(ifp, bump_id, send_na);
4331 
4332 		goto out;
4333 	}
4334 
4335 	ifp->dad_probes--;
4336 	addrconf_mod_dad_work(ifp,
4337 			      max(NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME),
4338 				  HZ/100));
4339 	spin_unlock(&ifp->lock);
4340 	write_unlock_bh(&idev->lock);
4341 
4342 	/* send a neighbour solicitation for our addr */
4343 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
4344 	ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4345 		      ifp->dad_nonce);
4346 out:
4347 	in6_ifa_put(ifp);
4348 	rtnl_net_unlock(net);
4349 }
4350 
4351 /* ifp->idev must be at least read locked */
4352 static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
4353 {
4354 	struct inet6_ifaddr *ifpiter;
4355 	struct inet6_dev *idev = ifp->idev;
4356 
4357 	list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4358 		if (ifpiter->scope > IFA_LINK)
4359 			break;
4360 		if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4361 		    (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4362 				       IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4363 		    IFA_F_PERMANENT)
4364 			return false;
4365 	}
4366 	return true;
4367 }
4368 
4369 static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4370 				   bool send_na)
4371 {
4372 	struct net_device *dev = ifp->idev->dev;
4373 	struct in6_addr lladdr;
4374 	bool send_rs, send_mld;
4375 
4376 	addrconf_del_dad_work(ifp);
4377 
4378 	/*
4379 	 *	Configure the address for reception. Now it is valid.
4380 	 */
4381 
4382 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
4383 
4384 	/* If added prefix is link local and we are prepared to process
4385 	   router advertisements, start sending router solicitations.
4386 	 */
4387 
4388 	read_lock_bh(&ifp->idev->lock);
4389 	send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
4390 	send_rs = send_mld &&
4391 		  ipv6_accept_ra(ifp->idev) &&
4392 		  READ_ONCE(ifp->idev->cnf.rtr_solicits) != 0 &&
4393 		  (dev->flags & IFF_LOOPBACK) == 0 &&
4394 		  (dev->type != ARPHRD_TUNNEL) &&
4395 		  !netif_is_team_port(dev);
4396 	read_unlock_bh(&ifp->idev->lock);
4397 
4398 	/* While dad is in progress mld report's source address is in6_addrany.
4399 	 * Resend with proper ll now.
4400 	 */
4401 	if (send_mld)
4402 		ipv6_mc_dad_complete(ifp->idev);
4403 
4404 	/* send unsolicited NA if enabled */
4405 	if (send_na &&
4406 	    (READ_ONCE(ifp->idev->cnf.ndisc_notify) ||
4407 	     READ_ONCE(dev_net(dev)->ipv6.devconf_all->ndisc_notify))) {
4408 		ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4409 			      /*router=*/ !!ifp->idev->cnf.forwarding,
4410 			      /*solicited=*/ false, /*override=*/ true,
4411 			      /*inc_opt=*/ true);
4412 	}
4413 
4414 	if (send_rs) {
4415 		/*
4416 		 *	If a host as already performed a random delay
4417 		 *	[...] as part of DAD [...] there is no need
4418 		 *	to delay again before sending the first RS
4419 		 */
4420 		if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
4421 			return;
4422 		ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
4423 
4424 		write_lock_bh(&ifp->idev->lock);
4425 		spin_lock(&ifp->lock);
4426 		ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4427 			READ_ONCE(ifp->idev->cnf.rtr_solicit_interval));
4428 		ifp->idev->rs_probes = 1;
4429 		ifp->idev->if_flags |= IF_RS_SENT;
4430 		addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
4431 		spin_unlock(&ifp->lock);
4432 		write_unlock_bh(&ifp->idev->lock);
4433 	}
4434 
4435 	if (bump_id)
4436 		rt_genid_bump_ipv6(dev_net(dev));
4437 
4438 	/* Make sure that a new temporary address will be created
4439 	 * before this temporary address becomes deprecated.
4440 	 */
4441 	if (ifp->flags & IFA_F_TEMPORARY)
4442 		addrconf_verify_rtnl(dev_net(dev));
4443 }
4444 
4445 static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
4446 {
4447 	struct inet6_ifaddr *ifp;
4448 
4449 	read_lock_bh(&idev->lock);
4450 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
4451 		spin_lock(&ifp->lock);
4452 		if ((ifp->flags & IFA_F_TENTATIVE &&
4453 		     ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4454 			if (restart)
4455 				ifp->state = INET6_IFADDR_STATE_PREDAD;
4456 			addrconf_dad_kick(ifp);
4457 		}
4458 		spin_unlock(&ifp->lock);
4459 	}
4460 	read_unlock_bh(&idev->lock);
4461 }
4462 
4463 #ifdef CONFIG_PROC_FS
4464 struct if6_iter_state {
4465 	struct seq_net_private p;
4466 	int bucket;
4467 	int offset;
4468 };
4469 
4470 static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
4471 {
4472 	struct if6_iter_state *state = seq->private;
4473 	struct net *net = seq_file_net(seq);
4474 	struct inet6_ifaddr *ifa = NULL;
4475 	int p = 0;
4476 
4477 	/* initial bucket if pos is 0 */
4478 	if (pos == 0) {
4479 		state->bucket = 0;
4480 		state->offset = 0;
4481 	}
4482 
4483 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
4484 		hlist_for_each_entry_rcu(ifa, &net->ipv6.inet6_addr_lst[state->bucket],
4485 					 addr_lst) {
4486 			/* sync with offset */
4487 			if (p < state->offset) {
4488 				p++;
4489 				continue;
4490 			}
4491 			return ifa;
4492 		}
4493 
4494 		/* prepare for next bucket */
4495 		state->offset = 0;
4496 		p = 0;
4497 	}
4498 	return NULL;
4499 }
4500 
4501 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4502 					 struct inet6_ifaddr *ifa)
4503 {
4504 	struct if6_iter_state *state = seq->private;
4505 	struct net *net = seq_file_net(seq);
4506 
4507 	hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
4508 		state->offset++;
4509 		return ifa;
4510 	}
4511 
4512 	state->offset = 0;
4513 	while (++state->bucket < IN6_ADDR_HSIZE) {
4514 		hlist_for_each_entry_rcu(ifa,
4515 				     &net->ipv6.inet6_addr_lst[state->bucket], addr_lst) {
4516 			return ifa;
4517 		}
4518 	}
4519 
4520 	return NULL;
4521 }
4522 
4523 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
4524 	__acquires(rcu)
4525 {
4526 	rcu_read_lock();
4527 	return if6_get_first(seq, *pos);
4528 }
4529 
4530 static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4531 {
4532 	struct inet6_ifaddr *ifa;
4533 
4534 	ifa = if6_get_next(seq, v);
4535 	++*pos;
4536 	return ifa;
4537 }
4538 
4539 static void if6_seq_stop(struct seq_file *seq, void *v)
4540 	__releases(rcu)
4541 {
4542 	rcu_read_unlock();
4543 }
4544 
4545 static int if6_seq_show(struct seq_file *seq, void *v)
4546 {
4547 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
4548 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
4549 		   &ifp->addr,
4550 		   ifp->idev->dev->ifindex,
4551 		   ifp->prefix_len,
4552 		   ifp->scope,
4553 		   (u8) ifp->flags,
4554 		   ifp->idev->dev->name);
4555 	return 0;
4556 }
4557 
4558 static const struct seq_operations if6_seq_ops = {
4559 	.start	= if6_seq_start,
4560 	.next	= if6_seq_next,
4561 	.show	= if6_seq_show,
4562 	.stop	= if6_seq_stop,
4563 };
4564 
4565 static int __net_init if6_proc_net_init(struct net *net)
4566 {
4567 	if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4568 			sizeof(struct if6_iter_state)))
4569 		return -ENOMEM;
4570 	return 0;
4571 }
4572 
4573 static void __net_exit if6_proc_net_exit(struct net *net)
4574 {
4575 	remove_proc_entry("if_inet6", net->proc_net);
4576 }
4577 
4578 static struct pernet_operations if6_proc_net_ops = {
4579 	.init = if6_proc_net_init,
4580 	.exit = if6_proc_net_exit,
4581 };
4582 
4583 int __init if6_proc_init(void)
4584 {
4585 	return register_pernet_subsys(&if6_proc_net_ops);
4586 }
4587 
4588 void if6_proc_exit(void)
4589 {
4590 	unregister_pernet_subsys(&if6_proc_net_ops);
4591 }
4592 #endif	/* CONFIG_PROC_FS */
4593 
4594 #if IS_ENABLED(CONFIG_IPV6_MIP6)
4595 /* Check if address is a home address configured on any interface. */
4596 int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
4597 {
4598 	unsigned int hash = inet6_addr_hash(net, addr);
4599 	struct inet6_ifaddr *ifp = NULL;
4600 	int ret = 0;
4601 
4602 	rcu_read_lock();
4603 	hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4604 		if (ipv6_addr_equal(&ifp->addr, addr) &&
4605 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
4606 			ret = 1;
4607 			break;
4608 		}
4609 	}
4610 	rcu_read_unlock();
4611 	return ret;
4612 }
4613 #endif
4614 
4615 /* RFC6554 has some algorithm to avoid loops in segment routing by
4616  * checking if the segments contains any of a local interface address.
4617  *
4618  * Quote:
4619  *
4620  * To detect loops in the SRH, a router MUST determine if the SRH
4621  * includes multiple addresses assigned to any interface on that router.
4622  * If such addresses appear more than once and are separated by at least
4623  * one address not assigned to that router.
4624  */
4625 int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
4626 			  unsigned char nsegs)
4627 {
4628 	const struct in6_addr *addr;
4629 	int i, ret = 0, found = 0;
4630 	struct inet6_ifaddr *ifp;
4631 	bool separated = false;
4632 	unsigned int hash;
4633 	bool hash_found;
4634 
4635 	rcu_read_lock();
4636 	for (i = 0; i < nsegs; i++) {
4637 		addr = &segs[i];
4638 		hash = inet6_addr_hash(net, addr);
4639 
4640 		hash_found = false;
4641 		hlist_for_each_entry_rcu(ifp, &net->ipv6.inet6_addr_lst[hash], addr_lst) {
4642 
4643 			if (ipv6_addr_equal(&ifp->addr, addr)) {
4644 				hash_found = true;
4645 				break;
4646 			}
4647 		}
4648 
4649 		if (hash_found) {
4650 			if (found > 1 && separated) {
4651 				ret = 1;
4652 				break;
4653 			}
4654 
4655 			separated = false;
4656 			found++;
4657 		} else {
4658 			separated = true;
4659 		}
4660 	}
4661 	rcu_read_unlock();
4662 
4663 	return ret;
4664 }
4665 
4666 /*
4667  *	Periodic address status verification
4668  */
4669 
4670 static void addrconf_verify_rtnl(struct net *net)
4671 {
4672 	unsigned long now, next, next_sec, next_sched;
4673 	struct inet6_ifaddr *ifp;
4674 	int i;
4675 
4676 	ASSERT_RTNL();
4677 
4678 	rcu_read_lock_bh();
4679 	now = jiffies;
4680 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
4681 
4682 	cancel_delayed_work(&net->ipv6.addr_chk_work);
4683 
4684 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
4685 restart:
4686 		hlist_for_each_entry_rcu_bh(ifp, &net->ipv6.inet6_addr_lst[i], addr_lst) {
4687 			unsigned long age;
4688 
4689 			/* When setting preferred_lft to a value not zero or
4690 			 * infinity, while valid_lft is infinity
4691 			 * IFA_F_PERMANENT has a non-infinity life time.
4692 			 */
4693 			if ((ifp->flags & IFA_F_PERMANENT) &&
4694 			    (ifp->prefered_lft == INFINITY_LIFE_TIME))
4695 				continue;
4696 
4697 			spin_lock(&ifp->lock);
4698 			/* We try to batch several events at once. */
4699 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
4700 
4701 			if ((ifp->flags&IFA_F_TEMPORARY) &&
4702 			    !(ifp->flags&IFA_F_TENTATIVE) &&
4703 			    ifp->prefered_lft != INFINITY_LIFE_TIME &&
4704 			    !ifp->regen_count && ifp->ifpub) {
4705 				/* This is a non-regenerated temporary addr. */
4706 
4707 				unsigned long regen_advance = ipv6_get_regen_advance(ifp->idev);
4708 
4709 				if (age + regen_advance >= ifp->prefered_lft) {
4710 					struct inet6_ifaddr *ifpub = ifp->ifpub;
4711 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4712 						next = ifp->tstamp + ifp->prefered_lft * HZ;
4713 
4714 					ifp->regen_count++;
4715 					in6_ifa_hold(ifp);
4716 					in6_ifa_hold(ifpub);
4717 					spin_unlock(&ifp->lock);
4718 
4719 					spin_lock(&ifpub->lock);
4720 					ifpub->regen_count = 0;
4721 					spin_unlock(&ifpub->lock);
4722 					rcu_read_unlock_bh();
4723 					ipv6_create_tempaddr(ifpub, true);
4724 					in6_ifa_put(ifpub);
4725 					in6_ifa_put(ifp);
4726 					rcu_read_lock_bh();
4727 					goto restart;
4728 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4729 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4730 			}
4731 
4732 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4733 			    age >= ifp->valid_lft) {
4734 				spin_unlock(&ifp->lock);
4735 				in6_ifa_hold(ifp);
4736 				rcu_read_unlock_bh();
4737 				ipv6_del_addr(ifp);
4738 				rcu_read_lock_bh();
4739 				goto restart;
4740 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4741 				spin_unlock(&ifp->lock);
4742 				continue;
4743 			} else if (age >= ifp->prefered_lft) {
4744 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
4745 				int deprecate = 0;
4746 
4747 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
4748 					deprecate = 1;
4749 					ifp->flags |= IFA_F_DEPRECATED;
4750 				}
4751 
4752 				if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4753 				    (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
4754 					next = ifp->tstamp + ifp->valid_lft * HZ;
4755 
4756 				spin_unlock(&ifp->lock);
4757 
4758 				if (deprecate) {
4759 					in6_ifa_hold(ifp);
4760 
4761 					ipv6_ifa_notify(0, ifp);
4762 					in6_ifa_put(ifp);
4763 					goto restart;
4764 				}
4765 			} else {
4766 				/* ifp->prefered_lft <= ifp->valid_lft */
4767 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4768 					next = ifp->tstamp + ifp->prefered_lft * HZ;
4769 				spin_unlock(&ifp->lock);
4770 			}
4771 		}
4772 	}
4773 
4774 	next_sec = round_jiffies_up(next);
4775 	next_sched = next;
4776 
4777 	/* If rounded timeout is accurate enough, accept it. */
4778 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4779 		next_sched = next_sec;
4780 
4781 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4782 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4783 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4784 
4785 	pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4786 		 now, next, next_sec, next_sched);
4787 	mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, next_sched - now);
4788 	rcu_read_unlock_bh();
4789 }
4790 
4791 static void addrconf_verify_work(struct work_struct *w)
4792 {
4793 	struct net *net = container_of(to_delayed_work(w), struct net,
4794 				       ipv6.addr_chk_work);
4795 
4796 	rtnl_net_lock(net);
4797 	addrconf_verify_rtnl(net);
4798 	rtnl_net_unlock(net);
4799 }
4800 
4801 static void addrconf_verify(struct net *net)
4802 {
4803 	mod_delayed_work(addrconf_wq, &net->ipv6.addr_chk_work, 0);
4804 }
4805 
4806 static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4807 				     struct in6_addr **peer_pfx)
4808 {
4809 	struct in6_addr *pfx = NULL;
4810 
4811 	*peer_pfx = NULL;
4812 
4813 	if (addr)
4814 		pfx = nla_data(addr);
4815 
4816 	if (local) {
4817 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
4818 			*peer_pfx = pfx;
4819 		pfx = nla_data(local);
4820 	}
4821 
4822 	return pfx;
4823 }
4824 
4825 static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
4826 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
4827 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
4828 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
4829 	[IFA_FLAGS]		= { .len = sizeof(u32) },
4830 	[IFA_RT_PRIORITY]	= { .len = sizeof(u32) },
4831 	[IFA_TARGET_NETNSID]	= { .type = NLA_S32 },
4832 	[IFA_PROTO]		= { .type = NLA_U8 },
4833 };
4834 
4835 static int
4836 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4837 		  struct netlink_ext_ack *extack)
4838 {
4839 	struct net *net = sock_net(skb->sk);
4840 	struct ifaddrmsg *ifm;
4841 	struct nlattr *tb[IFA_MAX+1];
4842 	struct in6_addr *pfx, *peer_pfx;
4843 	u32 ifa_flags;
4844 	int err;
4845 
4846 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4847 				     ifa_ipv6_policy, extack);
4848 	if (err < 0)
4849 		return err;
4850 
4851 	ifm = nlmsg_data(nlh);
4852 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4853 	if (!pfx)
4854 		return -EINVAL;
4855 
4856 	ifa_flags = nla_get_u32_default(tb[IFA_FLAGS], ifm->ifa_flags);
4857 
4858 	/* We ignore other flags so far. */
4859 	ifa_flags &= IFA_F_MANAGETEMPADDR;
4860 
4861 	rtnl_net_lock(net);
4862 	err = inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4863 			     ifm->ifa_prefixlen, extack);
4864 	rtnl_net_unlock(net);
4865 
4866 	return err;
4867 }
4868 
4869 static int modify_prefix_route(struct net *net, struct inet6_ifaddr *ifp,
4870 			       unsigned long expires, u32 flags,
4871 			       bool modify_peer)
4872 {
4873 	struct fib6_table *table;
4874 	struct fib6_info *f6i;
4875 	u32 prio;
4876 
4877 	f6i = addrconf_get_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4878 					ifp->prefix_len,
4879 					ifp->idev->dev, 0, RTF_DEFAULT, true);
4880 	if (!f6i)
4881 		return -ENOENT;
4882 
4883 	prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4884 	if (f6i->fib6_metric != prio) {
4885 		/* delete old one */
4886 		ip6_del_rt(dev_net(ifp->idev->dev), f6i, false);
4887 
4888 		/* add new one */
4889 		addrconf_prefix_route(modify_peer ? &ifp->peer_addr : &ifp->addr,
4890 				      ifp->prefix_len,
4891 				      ifp->rt_priority, ifp->idev->dev,
4892 				      expires, flags, GFP_KERNEL);
4893 		return 0;
4894 	}
4895 	if (f6i != net->ipv6.fib6_null_entry) {
4896 		table = f6i->fib6_table;
4897 		spin_lock_bh(&table->tb6_lock);
4898 
4899 		if (!(flags & RTF_EXPIRES)) {
4900 			fib6_clean_expires(f6i);
4901 			fib6_may_remove_gc_list(net, f6i);
4902 		} else {
4903 			fib6_set_expires(f6i, jiffies + clock_t_to_jiffies(expires));
4904 			fib6_add_gc_list(f6i);
4905 		}
4906 
4907 		spin_unlock_bh(&table->tb6_lock);
4908 	}
4909 	fib6_info_release(f6i);
4910 
4911 	return 0;
4912 }
4913 
4914 static int inet6_addr_modify(struct net *net, struct inet6_ifaddr *ifp,
4915 			     struct ifa6_config *cfg, clock_t expires,
4916 			     u32 flags)
4917 {
4918 	bool was_managetempaddr;
4919 	bool new_peer = false;
4920 	bool had_prefixroute;
4921 
4922 	ASSERT_RTNL_NET(net);
4923 
4924 	if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
4925 	    (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4926 		return -EINVAL;
4927 
4928 	if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
4929 		cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
4930 
4931 	if (cfg->peer_pfx &&
4932 	    memcmp(&ifp->peer_addr, cfg->peer_pfx, sizeof(struct in6_addr))) {
4933 		if (!ipv6_addr_any(&ifp->peer_addr))
4934 			cleanup_prefix_route(ifp, expires, true, true);
4935 		new_peer = true;
4936 	}
4937 
4938 	spin_lock_bh(&ifp->lock);
4939 	was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
4940 	had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4941 			  !(ifp->flags & IFA_F_NOPREFIXROUTE);
4942 	ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
4943 			IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4944 			IFA_F_NOPREFIXROUTE);
4945 	ifp->flags |= cfg->ifa_flags;
4946 	WRITE_ONCE(ifp->tstamp, jiffies);
4947 	WRITE_ONCE(ifp->valid_lft, cfg->valid_lft);
4948 	WRITE_ONCE(ifp->prefered_lft, cfg->preferred_lft);
4949 	WRITE_ONCE(ifp->ifa_proto, cfg->ifa_proto);
4950 
4951 	if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
4952 		WRITE_ONCE(ifp->rt_priority, cfg->rt_priority);
4953 
4954 	if (new_peer)
4955 		ifp->peer_addr = *cfg->peer_pfx;
4956 
4957 	spin_unlock_bh(&ifp->lock);
4958 	if (!(ifp->flags&IFA_F_TENTATIVE))
4959 		ipv6_ifa_notify(0, ifp);
4960 
4961 	if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
4962 		int rc = -ENOENT;
4963 
4964 		if (had_prefixroute)
4965 			rc = modify_prefix_route(net, ifp, expires, flags, false);
4966 
4967 		/* prefix route could have been deleted; if so restore it */
4968 		if (rc == -ENOENT) {
4969 			addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4970 					      ifp->rt_priority, ifp->idev->dev,
4971 					      expires, flags, GFP_KERNEL);
4972 		}
4973 
4974 		if (had_prefixroute && !ipv6_addr_any(&ifp->peer_addr))
4975 			rc = modify_prefix_route(net, ifp, expires, flags, true);
4976 
4977 		if (rc == -ENOENT && !ipv6_addr_any(&ifp->peer_addr)) {
4978 			addrconf_prefix_route(&ifp->peer_addr, ifp->prefix_len,
4979 					      ifp->rt_priority, ifp->idev->dev,
4980 					      expires, flags, GFP_KERNEL);
4981 		}
4982 	} else if (had_prefixroute) {
4983 		enum cleanup_prefix_rt_t action;
4984 		unsigned long rt_expires;
4985 
4986 		write_lock_bh(&ifp->idev->lock);
4987 		action = check_cleanup_prefix_route(ifp, &rt_expires);
4988 		write_unlock_bh(&ifp->idev->lock);
4989 
4990 		if (action != CLEANUP_PREFIX_RT_NOP) {
4991 			cleanup_prefix_route(ifp, rt_expires,
4992 				action == CLEANUP_PREFIX_RT_DEL, false);
4993 		}
4994 	}
4995 
4996 	if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4997 		if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4998 			delete_tempaddrs(ifp->idev, ifp);
4999 		else
5000 			manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
5001 					 cfg->preferred_lft, !was_managetempaddr,
5002 					 jiffies);
5003 	}
5004 
5005 	addrconf_verify_rtnl(net);
5006 
5007 	return 0;
5008 }
5009 
5010 static int
5011 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
5012 		  struct netlink_ext_ack *extack)
5013 {
5014 	struct net *net = sock_net(skb->sk);
5015 	struct nlattr *tb[IFA_MAX+1];
5016 	struct in6_addr *peer_pfx;
5017 	struct inet6_ifaddr *ifa;
5018 	struct net_device *dev;
5019 	struct inet6_dev *idev;
5020 	struct ifa6_config cfg;
5021 	struct ifaddrmsg *ifm;
5022 	unsigned long timeout;
5023 	clock_t expires;
5024 	u32 flags;
5025 	int err;
5026 
5027 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
5028 				     ifa_ipv6_policy, extack);
5029 	if (err < 0)
5030 		return err;
5031 
5032 	memset(&cfg, 0, sizeof(cfg));
5033 
5034 	ifm = nlmsg_data(nlh);
5035 	cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
5036 	if (!cfg.pfx)
5037 		return -EINVAL;
5038 
5039 	cfg.peer_pfx = peer_pfx;
5040 	cfg.plen = ifm->ifa_prefixlen;
5041 	if (tb[IFA_RT_PRIORITY])
5042 		cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
5043 
5044 	if (tb[IFA_PROTO])
5045 		cfg.ifa_proto = nla_get_u8(tb[IFA_PROTO]);
5046 
5047 	cfg.ifa_flags = nla_get_u32_default(tb[IFA_FLAGS], ifm->ifa_flags);
5048 
5049 	/* We ignore other flags so far. */
5050 	cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
5051 			 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
5052 			 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
5053 
5054 	cfg.ifa_flags |= IFA_F_PERMANENT;
5055 	cfg.valid_lft = INFINITY_LIFE_TIME;
5056 	cfg.preferred_lft = INFINITY_LIFE_TIME;
5057 	expires = 0;
5058 	flags = 0;
5059 
5060 	if (tb[IFA_CACHEINFO]) {
5061 		struct ifa_cacheinfo *ci;
5062 
5063 		ci = nla_data(tb[IFA_CACHEINFO]);
5064 		cfg.valid_lft = ci->ifa_valid;
5065 		cfg.preferred_lft = ci->ifa_prefered;
5066 
5067 		if (!cfg.valid_lft || cfg.preferred_lft > cfg.valid_lft) {
5068 			NL_SET_ERR_MSG_MOD(extack, "address lifetime invalid");
5069 			return -EINVAL;
5070 		}
5071 
5072 		timeout = addrconf_timeout_fixup(cfg.valid_lft, HZ);
5073 		if (addrconf_finite_timeout(timeout)) {
5074 			cfg.ifa_flags &= ~IFA_F_PERMANENT;
5075 			cfg.valid_lft = timeout;
5076 			expires = jiffies_to_clock_t(timeout * HZ);
5077 			flags = RTF_EXPIRES;
5078 		}
5079 
5080 		timeout = addrconf_timeout_fixup(cfg.preferred_lft, HZ);
5081 		if (addrconf_finite_timeout(timeout)) {
5082 			if (timeout == 0)
5083 				cfg.ifa_flags |= IFA_F_DEPRECATED;
5084 
5085 			cfg.preferred_lft = timeout;
5086 		}
5087 	}
5088 
5089 	rtnl_net_lock(net);
5090 
5091 	dev =  __dev_get_by_index(net, ifm->ifa_index);
5092 	if (!dev) {
5093 		NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface");
5094 		err = -ENODEV;
5095 		goto unlock_rtnl;
5096 	}
5097 
5098 	netdev_lock_ops(dev);
5099 	idev = ipv6_find_idev(dev);
5100 	if (IS_ERR(idev)) {
5101 		err = PTR_ERR(idev);
5102 		goto unlock;
5103 	}
5104 
5105 	if (!ipv6_allow_optimistic_dad(net, idev))
5106 		cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
5107 
5108 	if (cfg.ifa_flags & IFA_F_NODAD &&
5109 	    cfg.ifa_flags & IFA_F_OPTIMISTIC) {
5110 		NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
5111 		err = -EINVAL;
5112 		goto unlock;
5113 	}
5114 
5115 	ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
5116 	if (!ifa) {
5117 		/*
5118 		 * It would be best to check for !NLM_F_CREATE here but
5119 		 * userspace already relies on not having to provide this.
5120 		 */
5121 		err = inet6_addr_add(net, dev, &cfg, expires, flags, extack);
5122 		goto unlock;
5123 	}
5124 
5125 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
5126 	    !(nlh->nlmsg_flags & NLM_F_REPLACE)) {
5127 		NL_SET_ERR_MSG_MOD(extack, "address already assigned");
5128 		err = -EEXIST;
5129 	} else {
5130 		err = inet6_addr_modify(net, ifa, &cfg, expires, flags);
5131 	}
5132 
5133 	in6_ifa_put(ifa);
5134 unlock:
5135 	netdev_unlock_ops(dev);
5136 unlock_rtnl:
5137 	rtnl_net_unlock(net);
5138 
5139 	return err;
5140 }
5141 
5142 static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
5143 			  u8 scope, int ifindex)
5144 {
5145 	struct ifaddrmsg *ifm;
5146 
5147 	ifm = nlmsg_data(nlh);
5148 	ifm->ifa_family = AF_INET6;
5149 	ifm->ifa_prefixlen = prefixlen;
5150 	ifm->ifa_flags = flags;
5151 	ifm->ifa_scope = scope;
5152 	ifm->ifa_index = ifindex;
5153 }
5154 
5155 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
5156 			 unsigned long tstamp, u32 preferred, u32 valid)
5157 {
5158 	struct ifa_cacheinfo ci;
5159 
5160 	ci.cstamp = cstamp_delta(cstamp);
5161 	ci.tstamp = cstamp_delta(tstamp);
5162 	ci.ifa_prefered = preferred;
5163 	ci.ifa_valid = valid;
5164 
5165 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
5166 }
5167 
5168 static inline int rt_scope(int ifa_scope)
5169 {
5170 	if (ifa_scope & IFA_HOST)
5171 		return RT_SCOPE_HOST;
5172 	else if (ifa_scope & IFA_LINK)
5173 		return RT_SCOPE_LINK;
5174 	else if (ifa_scope & IFA_SITE)
5175 		return RT_SCOPE_SITE;
5176 	else
5177 		return RT_SCOPE_UNIVERSE;
5178 }
5179 
5180 static inline int inet6_ifaddr_msgsize(void)
5181 {
5182 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
5183 	       + nla_total_size(16) /* IFA_LOCAL */
5184 	       + nla_total_size(16) /* IFA_ADDRESS */
5185 	       + nla_total_size(sizeof(struct ifa_cacheinfo))
5186 	       + nla_total_size(4)  /* IFA_FLAGS */
5187 	       + nla_total_size(1)  /* IFA_PROTO */
5188 	       + nla_total_size(4)  /* IFA_RT_PRIORITY */;
5189 }
5190 
5191 static int inet6_fill_ifaddr(struct sk_buff *skb,
5192 			     const struct inet6_ifaddr *ifa,
5193 			     struct inet6_fill_args *args)
5194 {
5195 	struct nlmsghdr *nlh;
5196 	u32 preferred, valid;
5197 	u32 flags, priority;
5198 	u8 proto;
5199 
5200 	nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5201 			sizeof(struct ifaddrmsg), args->flags);
5202 	if (!nlh)
5203 		return -EMSGSIZE;
5204 
5205 	flags = READ_ONCE(ifa->flags);
5206 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
5207 		      ifa->idev->dev->ifindex);
5208 
5209 	if (args->netnsid >= 0 &&
5210 	    nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
5211 		goto error;
5212 
5213 	preferred = READ_ONCE(ifa->prefered_lft);
5214 	valid = READ_ONCE(ifa->valid_lft);
5215 
5216 	if (!((flags & IFA_F_PERMANENT) &&
5217 	      (preferred == INFINITY_LIFE_TIME))) {
5218 		if (preferred != INFINITY_LIFE_TIME) {
5219 			long tval = (jiffies - READ_ONCE(ifa->tstamp)) / HZ;
5220 
5221 			if (preferred > tval)
5222 				preferred -= tval;
5223 			else
5224 				preferred = 0;
5225 			if (valid != INFINITY_LIFE_TIME) {
5226 				if (valid > tval)
5227 					valid -= tval;
5228 				else
5229 					valid = 0;
5230 			}
5231 		}
5232 	} else {
5233 		preferred = INFINITY_LIFE_TIME;
5234 		valid = INFINITY_LIFE_TIME;
5235 	}
5236 
5237 	if (!ipv6_addr_any(&ifa->peer_addr)) {
5238 		if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
5239 		    nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
5240 			goto error;
5241 	} else {
5242 		if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
5243 			goto error;
5244 	}
5245 
5246 	priority = READ_ONCE(ifa->rt_priority);
5247 	if (priority && nla_put_u32(skb, IFA_RT_PRIORITY, priority))
5248 		goto error;
5249 
5250 	if (put_cacheinfo(skb, ifa->cstamp, READ_ONCE(ifa->tstamp),
5251 			  preferred, valid) < 0)
5252 		goto error;
5253 
5254 	if (nla_put_u32(skb, IFA_FLAGS, flags) < 0)
5255 		goto error;
5256 
5257 	proto = READ_ONCE(ifa->ifa_proto);
5258 	if (proto && nla_put_u8(skb, IFA_PROTO, proto))
5259 		goto error;
5260 
5261 	nlmsg_end(skb, nlh);
5262 	return 0;
5263 
5264 error:
5265 	nlmsg_cancel(skb, nlh);
5266 	return -EMSGSIZE;
5267 }
5268 
5269 int inet6_fill_ifmcaddr(struct sk_buff *skb,
5270 			const struct ifmcaddr6 *ifmca,
5271 			struct inet6_fill_args *args)
5272 {
5273 	int ifindex = ifmca->idev->dev->ifindex;
5274 	u8 scope = RT_SCOPE_UNIVERSE;
5275 	struct nlmsghdr *nlh;
5276 
5277 	if (!args->force_rt_scope_universe &&
5278 	    ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
5279 		scope = RT_SCOPE_SITE;
5280 
5281 	nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5282 			sizeof(struct ifaddrmsg), args->flags);
5283 	if (!nlh)
5284 		return -EMSGSIZE;
5285 
5286 	if (args->netnsid >= 0 &&
5287 	    nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5288 		nlmsg_cancel(skb, nlh);
5289 		return -EMSGSIZE;
5290 	}
5291 
5292 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5293 	if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
5294 	    nla_put_u32(skb, IFA_MC_USERS, READ_ONCE(ifmca->mca_users)) < 0 ||
5295 	    put_cacheinfo(skb, ifmca->mca_cstamp, READ_ONCE(ifmca->mca_tstamp),
5296 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
5297 		nlmsg_cancel(skb, nlh);
5298 		return -EMSGSIZE;
5299 	}
5300 
5301 	nlmsg_end(skb, nlh);
5302 	return 0;
5303 }
5304 
5305 int inet6_fill_ifacaddr(struct sk_buff *skb,
5306 			const struct ifacaddr6 *ifaca,
5307 			struct inet6_fill_args *args)
5308 {
5309 	struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
5310 	int ifindex = dev ? dev->ifindex : 1;
5311 	u8 scope = RT_SCOPE_UNIVERSE;
5312 	struct nlmsghdr *nlh;
5313 
5314 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
5315 		scope = RT_SCOPE_SITE;
5316 
5317 	nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
5318 			sizeof(struct ifaddrmsg), args->flags);
5319 	if (!nlh)
5320 		return -EMSGSIZE;
5321 
5322 	if (args->netnsid >= 0 &&
5323 	    nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) {
5324 		nlmsg_cancel(skb, nlh);
5325 		return -EMSGSIZE;
5326 	}
5327 
5328 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
5329 	if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
5330 	    put_cacheinfo(skb, ifaca->aca_cstamp, READ_ONCE(ifaca->aca_tstamp),
5331 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
5332 		nlmsg_cancel(skb, nlh);
5333 		return -EMSGSIZE;
5334 	}
5335 
5336 	nlmsg_end(skb, nlh);
5337 	return 0;
5338 }
5339 
5340 /* called with rcu_read_lock() */
5341 static int in6_dump_addrs(const struct inet6_dev *idev, struct sk_buff *skb,
5342 			  struct netlink_callback *cb, int *s_ip_idx,
5343 			  struct inet6_fill_args *fillargs)
5344 {
5345 	const struct ifmcaddr6 *ifmca;
5346 	const struct ifacaddr6 *ifaca;
5347 	int ip_idx = 0;
5348 	int err = 0;
5349 
5350 	switch (fillargs->type) {
5351 	case UNICAST_ADDR: {
5352 		const struct inet6_ifaddr *ifa;
5353 		fillargs->event = RTM_NEWADDR;
5354 
5355 		/* unicast address incl. temp addr */
5356 		list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
5357 			if (ip_idx < *s_ip_idx)
5358 				goto next;
5359 			err = inet6_fill_ifaddr(skb, ifa, fillargs);
5360 			if (err < 0)
5361 				break;
5362 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5363 next:
5364 			ip_idx++;
5365 		}
5366 		break;
5367 	}
5368 	case MULTICAST_ADDR:
5369 		fillargs->event = RTM_GETMULTICAST;
5370 
5371 		/* multicast address */
5372 		for (ifmca = rcu_dereference(idev->mc_list);
5373 		     ifmca;
5374 		     ifmca = rcu_dereference(ifmca->next), ip_idx++) {
5375 			if (ip_idx < *s_ip_idx)
5376 				continue;
5377 			err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
5378 			if (err < 0)
5379 				break;
5380 		}
5381 		break;
5382 	case ANYCAST_ADDR:
5383 		fillargs->event = RTM_GETANYCAST;
5384 		/* anycast address */
5385 		for (ifaca = rcu_dereference(idev->ac_list); ifaca;
5386 		     ifaca = rcu_dereference(ifaca->aca_next), ip_idx++) {
5387 			if (ip_idx < *s_ip_idx)
5388 				continue;
5389 			err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
5390 			if (err < 0)
5391 				break;
5392 		}
5393 		break;
5394 	default:
5395 		break;
5396 	}
5397 	*s_ip_idx = err ? ip_idx : 0;
5398 	return err;
5399 }
5400 
5401 static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5402 				       struct inet6_fill_args *fillargs,
5403 				       struct net **tgt_net, struct sock *sk,
5404 				       struct netlink_callback *cb)
5405 {
5406 	struct netlink_ext_ack *extack = cb->extack;
5407 	struct nlattr *tb[IFA_MAX+1];
5408 	struct ifaddrmsg *ifm;
5409 	int err, i;
5410 
5411 	ifm = nlmsg_payload(nlh, sizeof(*ifm));
5412 	if (!ifm) {
5413 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5414 		return -EINVAL;
5415 	}
5416 
5417 	if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5418 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5419 		return -EINVAL;
5420 	}
5421 
5422 	fillargs->ifindex = ifm->ifa_index;
5423 	if (fillargs->ifindex) {
5424 		cb->answer_flags |= NLM_F_DUMP_FILTERED;
5425 		fillargs->flags |= NLM_F_DUMP_FILTERED;
5426 	}
5427 
5428 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5429 					    ifa_ipv6_policy, extack);
5430 	if (err < 0)
5431 		return err;
5432 
5433 	for (i = 0; i <= IFA_MAX; ++i) {
5434 		if (!tb[i])
5435 			continue;
5436 
5437 		if (i == IFA_TARGET_NETNSID) {
5438 			struct net *net;
5439 
5440 			fillargs->netnsid = nla_get_s32(tb[i]);
5441 			net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5442 			if (IS_ERR(net)) {
5443 				fillargs->netnsid = -1;
5444 				NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5445 				return PTR_ERR(net);
5446 			}
5447 			*tgt_net = net;
5448 		} else {
5449 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5450 			return -EINVAL;
5451 		}
5452 	}
5453 
5454 	return 0;
5455 }
5456 
5457 static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5458 			   enum addr_type_t type)
5459 {
5460 	struct net *tgt_net = sock_net(skb->sk);
5461 	const struct nlmsghdr *nlh = cb->nlh;
5462 	struct inet6_fill_args fillargs = {
5463 		.portid = NETLINK_CB(cb->skb).portid,
5464 		.seq = cb->nlh->nlmsg_seq,
5465 		.flags = NLM_F_MULTI,
5466 		.netnsid = -1,
5467 		.type = type,
5468 		.force_rt_scope_universe = false,
5469 	};
5470 	struct {
5471 		unsigned long ifindex;
5472 		int ip_idx;
5473 	} *ctx = (void *)cb->ctx;
5474 	struct net_device *dev;
5475 	struct inet6_dev *idev;
5476 	int err = 0;
5477 
5478 	rcu_read_lock();
5479 	if (cb->strict_check) {
5480 		err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
5481 						  skb->sk, cb);
5482 		if (err < 0)
5483 			goto done;
5484 
5485 		err = 0;
5486 		if (fillargs.ifindex) {
5487 			dev = dev_get_by_index_rcu(tgt_net, fillargs.ifindex);
5488 			if (!dev) {
5489 				err = -ENODEV;
5490 				goto done;
5491 			}
5492 			idev = __in6_dev_get(dev);
5493 			if (idev)
5494 				err = in6_dump_addrs(idev, skb, cb,
5495 						     &ctx->ip_idx,
5496 						     &fillargs);
5497 			goto done;
5498 		}
5499 	}
5500 
5501 	cb->seq = inet6_base_seq(tgt_net);
5502 	for_each_netdev_dump(tgt_net, dev, ctx->ifindex) {
5503 		idev = __in6_dev_get(dev);
5504 		if (!idev)
5505 			continue;
5506 		err = in6_dump_addrs(idev, skb, cb, &ctx->ip_idx,
5507 				     &fillargs);
5508 		if (err < 0)
5509 			goto done;
5510 	}
5511 done:
5512 	rcu_read_unlock();
5513 	if (fillargs.netnsid >= 0)
5514 		put_net(tgt_net);
5515 
5516 	return err;
5517 }
5518 
5519 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
5520 {
5521 	enum addr_type_t type = UNICAST_ADDR;
5522 
5523 	return inet6_dump_addr(skb, cb, type);
5524 }
5525 
5526 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
5527 {
5528 	enum addr_type_t type = MULTICAST_ADDR;
5529 
5530 	return inet6_dump_addr(skb, cb, type);
5531 }
5532 
5533 
5534 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
5535 {
5536 	enum addr_type_t type = ANYCAST_ADDR;
5537 
5538 	return inet6_dump_addr(skb, cb, type);
5539 }
5540 
5541 static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
5542 				       const struct nlmsghdr *nlh,
5543 				       struct nlattr **tb,
5544 				       struct netlink_ext_ack *extack)
5545 {
5546 	struct ifaddrmsg *ifm;
5547 	int i, err;
5548 
5549 	ifm = nlmsg_payload(nlh, sizeof(*ifm));
5550 	if (!ifm) {
5551 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
5552 		return -EINVAL;
5553 	}
5554 
5555 	if (!netlink_strict_get_check(skb))
5556 		return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
5557 					      ifa_ipv6_policy, extack);
5558 
5559 	if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5560 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
5561 		return -EINVAL;
5562 	}
5563 
5564 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5565 					    ifa_ipv6_policy, extack);
5566 	if (err)
5567 		return err;
5568 
5569 	for (i = 0; i <= IFA_MAX; i++) {
5570 		if (!tb[i])
5571 			continue;
5572 
5573 		switch (i) {
5574 		case IFA_TARGET_NETNSID:
5575 		case IFA_ADDRESS:
5576 		case IFA_LOCAL:
5577 			break;
5578 		default:
5579 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
5580 			return -EINVAL;
5581 		}
5582 	}
5583 
5584 	return 0;
5585 }
5586 
5587 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5588 			     struct netlink_ext_ack *extack)
5589 {
5590 	struct net *tgt_net = sock_net(in_skb->sk);
5591 	struct inet6_fill_args fillargs = {
5592 		.portid = NETLINK_CB(in_skb).portid,
5593 		.seq = nlh->nlmsg_seq,
5594 		.event = RTM_NEWADDR,
5595 		.flags = 0,
5596 		.netnsid = -1,
5597 		.force_rt_scope_universe = false,
5598 	};
5599 	struct ifaddrmsg *ifm;
5600 	struct nlattr *tb[IFA_MAX+1];
5601 	struct in6_addr *addr = NULL, *peer;
5602 	struct net_device *dev = NULL;
5603 	struct inet6_ifaddr *ifa;
5604 	struct sk_buff *skb;
5605 	int err;
5606 
5607 	err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
5608 	if (err < 0)
5609 		return err;
5610 
5611 	if (tb[IFA_TARGET_NETNSID]) {
5612 		fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
5613 
5614 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
5615 						  fillargs.netnsid);
5616 		if (IS_ERR(tgt_net))
5617 			return PTR_ERR(tgt_net);
5618 	}
5619 
5620 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
5621 	if (!addr) {
5622 		err = -EINVAL;
5623 		goto errout;
5624 	}
5625 	ifm = nlmsg_data(nlh);
5626 	if (ifm->ifa_index)
5627 		dev = dev_get_by_index(tgt_net, ifm->ifa_index);
5628 
5629 	ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
5630 	if (!ifa) {
5631 		err = -EADDRNOTAVAIL;
5632 		goto errout;
5633 	}
5634 
5635 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5636 	if (!skb) {
5637 		err = -ENOBUFS;
5638 		goto errout_ifa;
5639 	}
5640 
5641 	err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5642 	if (err < 0) {
5643 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5644 		WARN_ON(err == -EMSGSIZE);
5645 		kfree_skb(skb);
5646 		goto errout_ifa;
5647 	}
5648 	err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
5649 errout_ifa:
5650 	in6_ifa_put(ifa);
5651 errout:
5652 	dev_put(dev);
5653 	if (fillargs.netnsid >= 0)
5654 		put_net(tgt_net);
5655 
5656 	return err;
5657 }
5658 
5659 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
5660 {
5661 	struct sk_buff *skb;
5662 	struct net *net = dev_net(ifa->idev->dev);
5663 	struct inet6_fill_args fillargs = {
5664 		.portid = 0,
5665 		.seq = 0,
5666 		.event = event,
5667 		.flags = 0,
5668 		.netnsid = -1,
5669 		.force_rt_scope_universe = false,
5670 	};
5671 	int err = -ENOBUFS;
5672 
5673 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
5674 	if (!skb)
5675 		goto errout;
5676 
5677 	err = inet6_fill_ifaddr(skb, ifa, &fillargs);
5678 	if (err < 0) {
5679 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5680 		WARN_ON(err == -EMSGSIZE);
5681 		kfree_skb(skb);
5682 		goto errout;
5683 	}
5684 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
5685 	return;
5686 errout:
5687 	rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
5688 }
5689 
5690 static void ipv6_store_devconf(const struct ipv6_devconf *cnf,
5691 			       __s32 *array, int bytes)
5692 {
5693 	BUG_ON(bytes < (DEVCONF_MAX * 4));
5694 
5695 	memset(array, 0, bytes);
5696 	array[DEVCONF_FORWARDING] = READ_ONCE(cnf->forwarding);
5697 	array[DEVCONF_HOPLIMIT] = READ_ONCE(cnf->hop_limit);
5698 	array[DEVCONF_MTU6] = READ_ONCE(cnf->mtu6);
5699 	array[DEVCONF_ACCEPT_RA] = READ_ONCE(cnf->accept_ra);
5700 	array[DEVCONF_ACCEPT_REDIRECTS] = READ_ONCE(cnf->accept_redirects);
5701 	array[DEVCONF_AUTOCONF] = READ_ONCE(cnf->autoconf);
5702 	array[DEVCONF_DAD_TRANSMITS] = READ_ONCE(cnf->dad_transmits);
5703 	array[DEVCONF_RTR_SOLICITS] = READ_ONCE(cnf->rtr_solicits);
5704 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5705 		jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_interval));
5706 	array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5707 		jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_max_interval));
5708 	array[DEVCONF_RTR_SOLICIT_DELAY] =
5709 		jiffies_to_msecs(READ_ONCE(cnf->rtr_solicit_delay));
5710 	array[DEVCONF_FORCE_MLD_VERSION] = READ_ONCE(cnf->force_mld_version);
5711 	array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5712 		jiffies_to_msecs(READ_ONCE(cnf->mldv1_unsolicited_report_interval));
5713 	array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5714 		jiffies_to_msecs(READ_ONCE(cnf->mldv2_unsolicited_report_interval));
5715 	array[DEVCONF_USE_TEMPADDR] = READ_ONCE(cnf->use_tempaddr);
5716 	array[DEVCONF_TEMP_VALID_LFT] = READ_ONCE(cnf->temp_valid_lft);
5717 	array[DEVCONF_TEMP_PREFERED_LFT] = READ_ONCE(cnf->temp_prefered_lft);
5718 	array[DEVCONF_REGEN_MAX_RETRY] = READ_ONCE(cnf->regen_max_retry);
5719 	array[DEVCONF_MAX_DESYNC_FACTOR] = READ_ONCE(cnf->max_desync_factor);
5720 	array[DEVCONF_MAX_ADDRESSES] = READ_ONCE(cnf->max_addresses);
5721 	array[DEVCONF_ACCEPT_RA_DEFRTR] = READ_ONCE(cnf->accept_ra_defrtr);
5722 	array[DEVCONF_RA_DEFRTR_METRIC] = READ_ONCE(cnf->ra_defrtr_metric);
5723 	array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] =
5724 		READ_ONCE(cnf->accept_ra_min_hop_limit);
5725 	array[DEVCONF_ACCEPT_RA_PINFO] = READ_ONCE(cnf->accept_ra_pinfo);
5726 #ifdef CONFIG_IPV6_ROUTER_PREF
5727 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = READ_ONCE(cnf->accept_ra_rtr_pref);
5728 	array[DEVCONF_RTR_PROBE_INTERVAL] =
5729 		jiffies_to_msecs(READ_ONCE(cnf->rtr_probe_interval));
5730 #ifdef CONFIG_IPV6_ROUTE_INFO
5731 	array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] =
5732 		READ_ONCE(cnf->accept_ra_rt_info_min_plen);
5733 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] =
5734 		READ_ONCE(cnf->accept_ra_rt_info_max_plen);
5735 #endif
5736 #endif
5737 	array[DEVCONF_PROXY_NDP] = READ_ONCE(cnf->proxy_ndp);
5738 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] =
5739 		READ_ONCE(cnf->accept_source_route);
5740 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5741 	array[DEVCONF_OPTIMISTIC_DAD] = READ_ONCE(cnf->optimistic_dad);
5742 	array[DEVCONF_USE_OPTIMISTIC] = READ_ONCE(cnf->use_optimistic);
5743 #endif
5744 #ifdef CONFIG_IPV6_MROUTE
5745 	array[DEVCONF_MC_FORWARDING] = atomic_read(&cnf->mc_forwarding);
5746 #endif
5747 	array[DEVCONF_DISABLE_IPV6] = READ_ONCE(cnf->disable_ipv6);
5748 	array[DEVCONF_ACCEPT_DAD] = READ_ONCE(cnf->accept_dad);
5749 	array[DEVCONF_FORCE_TLLAO] = READ_ONCE(cnf->force_tllao);
5750 	array[DEVCONF_NDISC_NOTIFY] = READ_ONCE(cnf->ndisc_notify);
5751 	array[DEVCONF_SUPPRESS_FRAG_NDISC] =
5752 		READ_ONCE(cnf->suppress_frag_ndisc);
5753 	array[DEVCONF_ACCEPT_RA_FROM_LOCAL] =
5754 		READ_ONCE(cnf->accept_ra_from_local);
5755 	array[DEVCONF_ACCEPT_RA_MTU] = READ_ONCE(cnf->accept_ra_mtu);
5756 	array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] =
5757 		READ_ONCE(cnf->ignore_routes_with_linkdown);
5758 	/* we omit DEVCONF_STABLE_SECRET for now */
5759 	array[DEVCONF_USE_OIF_ADDRS_ONLY] = READ_ONCE(cnf->use_oif_addrs_only);
5760 	array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] =
5761 		READ_ONCE(cnf->drop_unicast_in_l2_multicast);
5762 	array[DEVCONF_DROP_UNSOLICITED_NA] = READ_ONCE(cnf->drop_unsolicited_na);
5763 	array[DEVCONF_KEEP_ADDR_ON_DOWN] = READ_ONCE(cnf->keep_addr_on_down);
5764 	array[DEVCONF_SEG6_ENABLED] = READ_ONCE(cnf->seg6_enabled);
5765 #ifdef CONFIG_IPV6_SEG6_HMAC
5766 	array[DEVCONF_SEG6_REQUIRE_HMAC] = READ_ONCE(cnf->seg6_require_hmac);
5767 #endif
5768 	array[DEVCONF_ENHANCED_DAD] = READ_ONCE(cnf->enhanced_dad);
5769 	array[DEVCONF_ADDR_GEN_MODE] = READ_ONCE(cnf->addr_gen_mode);
5770 	array[DEVCONF_DISABLE_POLICY] = READ_ONCE(cnf->disable_policy);
5771 	array[DEVCONF_NDISC_TCLASS] = READ_ONCE(cnf->ndisc_tclass);
5772 	array[DEVCONF_RPL_SEG_ENABLED] = READ_ONCE(cnf->rpl_seg_enabled);
5773 	array[DEVCONF_IOAM6_ENABLED] = READ_ONCE(cnf->ioam6_enabled);
5774 	array[DEVCONF_IOAM6_ID] = READ_ONCE(cnf->ioam6_id);
5775 	array[DEVCONF_IOAM6_ID_WIDE] = READ_ONCE(cnf->ioam6_id_wide);
5776 	array[DEVCONF_NDISC_EVICT_NOCARRIER] =
5777 		READ_ONCE(cnf->ndisc_evict_nocarrier);
5778 	array[DEVCONF_ACCEPT_UNTRACKED_NA] =
5779 		READ_ONCE(cnf->accept_untracked_na);
5780 	array[DEVCONF_ACCEPT_RA_MIN_LFT] = READ_ONCE(cnf->accept_ra_min_lft);
5781 	array[DEVCONF_FORCE_FORWARDING] = READ_ONCE(cnf->force_forwarding);
5782 }
5783 
5784 static inline size_t inet6_ifla6_size(void)
5785 {
5786 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
5787 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
5788 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5789 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
5790 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5791 	     + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5792 	     + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5793 	     + nla_total_size(4) /* IFLA_INET6_RA_MTU */
5794 	     + 0;
5795 }
5796 
5797 static inline size_t inet6_if_nlmsg_size(void)
5798 {
5799 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5800 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5801 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5802 	       + nla_total_size(4) /* IFLA_MTU */
5803 	       + nla_total_size(4) /* IFLA_LINK */
5804 	       + nla_total_size(1) /* IFLA_OPERSTATE */
5805 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
5806 }
5807 
5808 static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
5809 					int bytes)
5810 {
5811 	int i;
5812 	int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
5813 	BUG_ON(pad < 0);
5814 
5815 	/* Use put_unaligned() because stats may not be aligned for u64. */
5816 	put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5817 	for (i = 1; i < ICMP6_MIB_MAX; i++)
5818 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
5819 
5820 	memset(&stats[ICMP6_MIB_MAX], 0, pad);
5821 }
5822 
5823 static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
5824 					int bytes, size_t syncpoff)
5825 {
5826 	int i, c;
5827 	u64 buff[IPSTATS_MIB_MAX];
5828 	int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5829 
5830 	BUG_ON(pad < 0);
5831 
5832 	memset(buff, 0, sizeof(buff));
5833 	buff[0] = IPSTATS_MIB_MAX;
5834 
5835 	for_each_possible_cpu(c) {
5836 		for (i = 1; i < IPSTATS_MIB_MAX; i++)
5837 			buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5838 	}
5839 
5840 	memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5841 	memset(&stats[IPSTATS_MIB_MAX], 0, pad);
5842 }
5843 
5844 static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5845 			     int bytes)
5846 {
5847 	switch (attrtype) {
5848 	case IFLA_INET6_STATS:
5849 		__snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5850 				     offsetof(struct ipstats_mib, syncp));
5851 		break;
5852 	case IFLA_INET6_ICMP6STATS:
5853 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
5854 		break;
5855 	}
5856 }
5857 
5858 static int inet6_fill_ifla6_stats_attrs(struct sk_buff *skb,
5859 					struct inet6_dev *idev)
5860 {
5861 	struct nlattr *nla;
5862 
5863 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
5864 	if (!nla)
5865 		goto nla_put_failure;
5866 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5867 
5868 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
5869 	if (!nla)
5870 		goto nla_put_failure;
5871 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
5872 
5873 	return 0;
5874 
5875 nla_put_failure:
5876 	return -EMSGSIZE;
5877 }
5878 
5879 static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5880 				  u32 ext_filter_mask)
5881 {
5882 	struct ifla_cacheinfo ci;
5883 	struct nlattr *nla;
5884 	u32 ra_mtu;
5885 
5886 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, READ_ONCE(idev->if_flags)))
5887 		goto nla_put_failure;
5888 	ci.max_reasm_len = IPV6_MAXPLEN;
5889 	ci.tstamp = cstamp_delta(READ_ONCE(idev->tstamp));
5890 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
5891 	ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
5892 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5893 		goto nla_put_failure;
5894 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
5895 	if (!nla)
5896 		goto nla_put_failure;
5897 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
5898 
5899 	/* XXX - MC not implemented */
5900 
5901 	if (!(ext_filter_mask & RTEXT_FILTER_SKIP_STATS)) {
5902 		if (inet6_fill_ifla6_stats_attrs(skb, idev) < 0)
5903 			goto nla_put_failure;
5904 	}
5905 
5906 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
5907 	if (!nla)
5908 		goto nla_put_failure;
5909 	read_lock_bh(&idev->lock);
5910 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5911 	read_unlock_bh(&idev->lock);
5912 
5913 	if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE,
5914 		       READ_ONCE(idev->cnf.addr_gen_mode)))
5915 		goto nla_put_failure;
5916 
5917 	ra_mtu = READ_ONCE(idev->ra_mtu);
5918 	if (ra_mtu && nla_put_u32(skb, IFLA_INET6_RA_MTU, ra_mtu))
5919 		goto nla_put_failure;
5920 
5921 	return 0;
5922 
5923 nla_put_failure:
5924 	return -EMSGSIZE;
5925 }
5926 
5927 static size_t inet6_get_link_af_size(const struct net_device *dev,
5928 				     u32 ext_filter_mask)
5929 {
5930 	if (!__in6_dev_get(dev))
5931 		return 0;
5932 
5933 	return inet6_ifla6_size();
5934 }
5935 
5936 static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5937 			      u32 ext_filter_mask)
5938 {
5939 	struct inet6_dev *idev = __in6_dev_get(dev);
5940 
5941 	if (!idev)
5942 		return -ENODATA;
5943 
5944 	if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
5945 		return -EMSGSIZE;
5946 
5947 	return 0;
5948 }
5949 
5950 static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token,
5951 			     struct netlink_ext_ack *extack)
5952 {
5953 	struct inet6_ifaddr *ifp;
5954 	struct net_device *dev = idev->dev;
5955 	bool clear_token, update_rs = false;
5956 	struct in6_addr ll_addr;
5957 
5958 	ASSERT_RTNL();
5959 
5960 	if (!token)
5961 		return -EINVAL;
5962 
5963 	if (dev->flags & IFF_LOOPBACK) {
5964 		NL_SET_ERR_MSG_MOD(extack, "Device is loopback");
5965 		return -EINVAL;
5966 	}
5967 
5968 	if (dev->flags & IFF_NOARP) {
5969 		NL_SET_ERR_MSG_MOD(extack,
5970 				   "Device does not do neighbour discovery");
5971 		return -EINVAL;
5972 	}
5973 
5974 	if (!ipv6_accept_ra(idev)) {
5975 		NL_SET_ERR_MSG_MOD(extack,
5976 				   "Router advertisement is disabled on device");
5977 		return -EINVAL;
5978 	}
5979 
5980 	if (READ_ONCE(idev->cnf.rtr_solicits) == 0) {
5981 		NL_SET_ERR_MSG(extack,
5982 			       "Router solicitation is disabled on device");
5983 		return -EINVAL;
5984 	}
5985 
5986 	write_lock_bh(&idev->lock);
5987 
5988 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5989 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5990 
5991 	write_unlock_bh(&idev->lock);
5992 
5993 	clear_token = ipv6_addr_any(token);
5994 	if (clear_token)
5995 		goto update_lft;
5996 
5997 	if (!idev->dead && (idev->if_flags & IF_READY) &&
5998 	    !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5999 			     IFA_F_OPTIMISTIC)) {
6000 		/* If we're not ready, then normal ifup will take care
6001 		 * of this. Otherwise, we need to request our rs here.
6002 		 */
6003 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
6004 		update_rs = true;
6005 	}
6006 
6007 update_lft:
6008 	write_lock_bh(&idev->lock);
6009 
6010 	if (update_rs) {
6011 		idev->if_flags |= IF_RS_SENT;
6012 		idev->rs_interval = rfc3315_s14_backoff_init(
6013 			READ_ONCE(idev->cnf.rtr_solicit_interval));
6014 		idev->rs_probes = 1;
6015 		addrconf_mod_rs_timer(idev, idev->rs_interval);
6016 	}
6017 
6018 	/* Well, that's kinda nasty ... */
6019 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
6020 		spin_lock(&ifp->lock);
6021 		if (ifp->tokenized) {
6022 			ifp->valid_lft = 0;
6023 			ifp->prefered_lft = 0;
6024 		}
6025 		spin_unlock(&ifp->lock);
6026 	}
6027 
6028 	write_unlock_bh(&idev->lock);
6029 	inet6_ifinfo_notify(RTM_NEWLINK, idev);
6030 	addrconf_verify_rtnl(dev_net(dev));
6031 	return 0;
6032 }
6033 
6034 static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
6035 	[IFLA_INET6_ADDR_GEN_MODE]	= { .type = NLA_U8 },
6036 	[IFLA_INET6_TOKEN]		= { .len = sizeof(struct in6_addr) },
6037 	[IFLA_INET6_RA_MTU]		= { .type = NLA_REJECT,
6038 					    .reject_message =
6039 						"IFLA_INET6_RA_MTU can not be set" },
6040 };
6041 
6042 static int check_addr_gen_mode(int mode)
6043 {
6044 	if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
6045 	    mode != IN6_ADDR_GEN_MODE_NONE &&
6046 	    mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
6047 	    mode != IN6_ADDR_GEN_MODE_RANDOM)
6048 		return -EINVAL;
6049 	return 1;
6050 }
6051 
6052 static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
6053 				int mode)
6054 {
6055 	if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
6056 	    !idev->cnf.stable_secret.initialized &&
6057 	    !net->ipv6.devconf_dflt->stable_secret.initialized)
6058 		return -EINVAL;
6059 	return 1;
6060 }
6061 
6062 static int inet6_validate_link_af(const struct net_device *dev,
6063 				  const struct nlattr *nla,
6064 				  struct netlink_ext_ack *extack)
6065 {
6066 	struct nlattr *tb[IFLA_INET6_MAX + 1];
6067 	struct inet6_dev *idev = NULL;
6068 	int err;
6069 
6070 	if (dev) {
6071 		idev = __in6_dev_get(dev);
6072 		if (!idev)
6073 			return -EAFNOSUPPORT;
6074 	}
6075 
6076 	err = nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
6077 					  inet6_af_policy, extack);
6078 	if (err)
6079 		return err;
6080 
6081 	if (!tb[IFLA_INET6_TOKEN] && !tb[IFLA_INET6_ADDR_GEN_MODE])
6082 		return -EINVAL;
6083 
6084 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
6085 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
6086 
6087 		if (check_addr_gen_mode(mode) < 0)
6088 			return -EINVAL;
6089 		if (dev && check_stable_privacy(idev, dev_net(dev), mode) < 0)
6090 			return -EINVAL;
6091 	}
6092 
6093 	return 0;
6094 }
6095 
6096 static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla,
6097 			     struct netlink_ext_ack *extack)
6098 {
6099 	struct inet6_dev *idev = __in6_dev_get(dev);
6100 	struct nlattr *tb[IFLA_INET6_MAX + 1];
6101 	int err;
6102 
6103 	if (!idev)
6104 		return -EAFNOSUPPORT;
6105 
6106 	if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
6107 		return -EINVAL;
6108 
6109 	if (tb[IFLA_INET6_TOKEN]) {
6110 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]),
6111 					extack);
6112 		if (err)
6113 			return err;
6114 	}
6115 
6116 	if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
6117 		u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
6118 
6119 		WRITE_ONCE(idev->cnf.addr_gen_mode, mode);
6120 	}
6121 
6122 	return 0;
6123 }
6124 
6125 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
6126 			     u32 portid, u32 seq, int event, unsigned int flags)
6127 {
6128 	struct net_device *dev = idev->dev;
6129 	struct ifinfomsg *hdr;
6130 	struct nlmsghdr *nlh;
6131 	int ifindex, iflink;
6132 	void *protoinfo;
6133 
6134 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
6135 	if (!nlh)
6136 		return -EMSGSIZE;
6137 
6138 	hdr = nlmsg_data(nlh);
6139 	hdr->ifi_family = AF_INET6;
6140 	hdr->__ifi_pad = 0;
6141 	hdr->ifi_type = dev->type;
6142 	ifindex = READ_ONCE(dev->ifindex);
6143 	hdr->ifi_index = ifindex;
6144 	hdr->ifi_flags = netif_get_flags(dev);
6145 	hdr->ifi_change = 0;
6146 
6147 	iflink = dev_get_iflink(dev);
6148 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
6149 	    (dev->addr_len &&
6150 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
6151 	    nla_put_u32(skb, IFLA_MTU, READ_ONCE(dev->mtu)) ||
6152 	    (ifindex != iflink &&
6153 	     nla_put_u32(skb, IFLA_LINK, iflink)) ||
6154 	    nla_put_u8(skb, IFLA_OPERSTATE,
6155 		       netif_running(dev) ? READ_ONCE(dev->operstate) : IF_OPER_DOWN))
6156 		goto nla_put_failure;
6157 	protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
6158 	if (!protoinfo)
6159 		goto nla_put_failure;
6160 
6161 	if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
6162 		goto nla_put_failure;
6163 
6164 	nla_nest_end(skb, protoinfo);
6165 	nlmsg_end(skb, nlh);
6166 	return 0;
6167 
6168 nla_put_failure:
6169 	nlmsg_cancel(skb, nlh);
6170 	return -EMSGSIZE;
6171 }
6172 
6173 static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
6174 				   struct netlink_ext_ack *extack)
6175 {
6176 	struct ifinfomsg *ifm;
6177 
6178 	ifm = nlmsg_payload(nlh, sizeof(*ifm));
6179 	if (!ifm) {
6180 		NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
6181 		return -EINVAL;
6182 	}
6183 
6184 	if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
6185 		NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
6186 		return -EINVAL;
6187 	}
6188 
6189 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
6190 	    ifm->ifi_change || ifm->ifi_index) {
6191 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
6192 		return -EINVAL;
6193 	}
6194 
6195 	return 0;
6196 }
6197 
6198 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
6199 {
6200 	struct net *net = sock_net(skb->sk);
6201 	struct {
6202 		unsigned long ifindex;
6203 	} *ctx = (void *)cb->ctx;
6204 	struct net_device *dev;
6205 	struct inet6_dev *idev;
6206 	int err;
6207 
6208 	/* only requests using strict checking can pass data to
6209 	 * influence the dump
6210 	 */
6211 	if (cb->strict_check) {
6212 		err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
6213 
6214 		if (err < 0)
6215 			return err;
6216 	}
6217 
6218 	err = 0;
6219 	rcu_read_lock();
6220 	for_each_netdev_dump(net, dev, ctx->ifindex) {
6221 		idev = __in6_dev_get(dev);
6222 		if (!idev)
6223 			continue;
6224 		err = inet6_fill_ifinfo(skb, idev,
6225 					NETLINK_CB(cb->skb).portid,
6226 					cb->nlh->nlmsg_seq,
6227 					RTM_NEWLINK, NLM_F_MULTI);
6228 		if (err < 0)
6229 			break;
6230 	}
6231 	rcu_read_unlock();
6232 
6233 	return err;
6234 }
6235 
6236 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
6237 {
6238 	struct sk_buff *skb;
6239 	struct net *net = dev_net(idev->dev);
6240 	int err = -ENOBUFS;
6241 
6242 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
6243 	if (!skb)
6244 		goto errout;
6245 
6246 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
6247 	if (err < 0) {
6248 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
6249 		WARN_ON(err == -EMSGSIZE);
6250 		kfree_skb(skb);
6251 		goto errout;
6252 	}
6253 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
6254 	return;
6255 errout:
6256 	rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
6257 }
6258 
6259 static inline size_t inet6_prefix_nlmsg_size(void)
6260 {
6261 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
6262 	       + nla_total_size(sizeof(struct in6_addr))
6263 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
6264 }
6265 
6266 static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
6267 			     struct prefix_info *pinfo, u32 portid, u32 seq,
6268 			     int event, unsigned int flags)
6269 {
6270 	struct prefixmsg *pmsg;
6271 	struct nlmsghdr *nlh;
6272 	struct prefix_cacheinfo	ci;
6273 
6274 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
6275 	if (!nlh)
6276 		return -EMSGSIZE;
6277 
6278 	pmsg = nlmsg_data(nlh);
6279 	pmsg->prefix_family = AF_INET6;
6280 	pmsg->prefix_pad1 = 0;
6281 	pmsg->prefix_pad2 = 0;
6282 	pmsg->prefix_ifindex = idev->dev->ifindex;
6283 	pmsg->prefix_len = pinfo->prefix_len;
6284 	pmsg->prefix_type = pinfo->type;
6285 	pmsg->prefix_pad3 = 0;
6286 	pmsg->prefix_flags = pinfo->flags;
6287 
6288 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
6289 		goto nla_put_failure;
6290 	ci.preferred_time = ntohl(pinfo->prefered);
6291 	ci.valid_time = ntohl(pinfo->valid);
6292 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
6293 		goto nla_put_failure;
6294 	nlmsg_end(skb, nlh);
6295 	return 0;
6296 
6297 nla_put_failure:
6298 	nlmsg_cancel(skb, nlh);
6299 	return -EMSGSIZE;
6300 }
6301 
6302 static void inet6_prefix_notify(int event, struct inet6_dev *idev,
6303 			 struct prefix_info *pinfo)
6304 {
6305 	struct sk_buff *skb;
6306 	struct net *net = dev_net(idev->dev);
6307 	int err = -ENOBUFS;
6308 
6309 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
6310 	if (!skb)
6311 		goto errout;
6312 
6313 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
6314 	if (err < 0) {
6315 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
6316 		WARN_ON(err == -EMSGSIZE);
6317 		kfree_skb(skb);
6318 		goto errout;
6319 	}
6320 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
6321 	return;
6322 errout:
6323 	rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
6324 }
6325 
6326 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6327 {
6328 	struct net *net = dev_net(ifp->idev->dev);
6329 
6330 	if (event)
6331 		ASSERT_RTNL();
6332 
6333 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
6334 
6335 	switch (event) {
6336 	case RTM_NEWADDR:
6337 		/*
6338 		 * If the address was optimistic we inserted the route at the
6339 		 * start of our DAD process, so we don't need to do it again.
6340 		 * If the device was taken down in the middle of the DAD
6341 		 * cycle there is a race where we could get here without a
6342 		 * host route, so nothing to insert. That will be fixed when
6343 		 * the device is brought up.
6344 		 */
6345 		if (ifp->rt && !rcu_access_pointer(ifp->rt->fib6_node)) {
6346 			ip6_ins_rt(net, ifp->rt);
6347 		} else if (!ifp->rt && (ifp->idev->dev->flags & IFF_UP)) {
6348 			pr_warn("BUG: Address %pI6c on device %s is missing its host route.\n",
6349 				&ifp->addr, ifp->idev->dev->name);
6350 		}
6351 
6352 		if (ifp->idev->cnf.forwarding)
6353 			addrconf_join_anycast(ifp);
6354 		if (!ipv6_addr_any(&ifp->peer_addr))
6355 			addrconf_prefix_route(&ifp->peer_addr, 128,
6356 					      ifp->rt_priority, ifp->idev->dev,
6357 					      0, 0, GFP_ATOMIC);
6358 		break;
6359 	case RTM_DELADDR:
6360 		if (ifp->idev->cnf.forwarding)
6361 			addrconf_leave_anycast(ifp);
6362 		addrconf_leave_solict(ifp->idev, &ifp->addr);
6363 		if (!ipv6_addr_any(&ifp->peer_addr)) {
6364 			struct fib6_info *rt;
6365 
6366 			rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
6367 						       ifp->idev->dev, 0, 0,
6368 						       false);
6369 			if (rt)
6370 				ip6_del_rt(net, rt, false);
6371 		}
6372 		if (ifp->rt) {
6373 			ip6_del_rt(net, ifp->rt, false);
6374 			ifp->rt = NULL;
6375 		}
6376 		rt_genid_bump_ipv6(net);
6377 		break;
6378 	}
6379 	atomic_inc(&net->ipv6.dev_addr_genid);
6380 }
6381 
6382 static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
6383 {
6384 	if (likely(ifp->idev->dead == 0))
6385 		__ipv6_ifa_notify(event, ifp);
6386 }
6387 
6388 #ifdef CONFIG_SYSCTL
6389 
6390 static int addrconf_sysctl_forward(const struct ctl_table *ctl, int write,
6391 		void *buffer, size_t *lenp, loff_t *ppos)
6392 {
6393 	struct ctl_table lctl;
6394 	int *valp = ctl->data;
6395 	int val = *valp;
6396 	int ret;
6397 
6398 	/*
6399 	 * ctl->data points to idev->cnf.forwarding, we should
6400 	 * not modify it until we get the rtnl lock.
6401 	 */
6402 	lctl = *ctl;
6403 	lctl.data = &val;
6404 
6405 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6406 	if (ret)
6407 		return ret;
6408 
6409 	if (write)
6410 		ret = addrconf_fixup_forwarding(ctl, valp, val);
6411 	return ret;
6412 }
6413 
6414 static int addrconf_sysctl_mtu(const struct ctl_table *ctl, int write,
6415 		void *buffer, size_t *lenp, loff_t *ppos)
6416 {
6417 	struct inet6_dev *idev = ctl->extra1;
6418 	int min_mtu = IPV6_MIN_MTU;
6419 	struct ctl_table lctl;
6420 
6421 	lctl = *ctl;
6422 	lctl.extra1 = &min_mtu;
6423 	lctl.extra2 = idev ? &idev->dev->mtu : NULL;
6424 
6425 	return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
6426 }
6427 
6428 static void dev_disable_change(struct inet6_dev *idev)
6429 {
6430 	struct netdev_notifier_info info;
6431 
6432 	if (!idev || !idev->dev)
6433 		return;
6434 
6435 	netdev_notifier_info_init(&info, idev->dev);
6436 	if (idev->cnf.disable_ipv6)
6437 		addrconf_notify(NULL, NETDEV_DOWN, &info);
6438 	else
6439 		addrconf_notify(NULL, NETDEV_UP, &info);
6440 }
6441 
6442 static void addrconf_disable_change(struct net *net, __s32 newf)
6443 {
6444 	struct net_device *dev;
6445 	struct inet6_dev *idev;
6446 
6447 	for_each_netdev(net, dev) {
6448 		idev = __in6_dev_get_rtnl_net(dev);
6449 		if (idev) {
6450 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
6451 
6452 			WRITE_ONCE(idev->cnf.disable_ipv6, newf);
6453 			if (changed)
6454 				dev_disable_change(idev);
6455 		}
6456 	}
6457 }
6458 
6459 static int addrconf_disable_ipv6(const struct ctl_table *table, int *p, int newf)
6460 {
6461 	struct net *net = (struct net *)table->extra2;
6462 	int old;
6463 
6464 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6465 		WRITE_ONCE(*p, newf);
6466 		return 0;
6467 	}
6468 
6469 	if (!rtnl_net_trylock(net))
6470 		return restart_syscall();
6471 
6472 	old = *p;
6473 	WRITE_ONCE(*p, newf);
6474 
6475 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
6476 		WRITE_ONCE(net->ipv6.devconf_dflt->disable_ipv6, newf);
6477 		addrconf_disable_change(net, newf);
6478 	} else if ((!newf) ^ (!old)) {
6479 		dev_disable_change((struct inet6_dev *)table->extra1);
6480 	}
6481 
6482 	rtnl_net_unlock(net);
6483 	return 0;
6484 }
6485 
6486 static int addrconf_sysctl_disable(const struct ctl_table *ctl, int write,
6487 		void *buffer, size_t *lenp, loff_t *ppos)
6488 {
6489 	struct ctl_table lctl;
6490 	int *valp = ctl->data;
6491 	int val = *valp;
6492 	int ret;
6493 
6494 	/*
6495 	 * ctl->data points to idev->cnf.disable_ipv6, we should
6496 	 * not modify it until we get the rtnl lock.
6497 	 */
6498 	lctl = *ctl;
6499 	lctl.data = &val;
6500 
6501 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6502 	if (ret)
6503 		return ret;
6504 
6505 	if (write)
6506 		ret = addrconf_disable_ipv6(ctl, valp, val);
6507 	return ret;
6508 }
6509 
6510 static int addrconf_sysctl_proxy_ndp(const struct ctl_table *ctl, int write,
6511 		void *buffer, size_t *lenp, loff_t *ppos)
6512 {
6513 	struct net *net = ctl->extra2;
6514 	int *valp = ctl->data;
6515 	int old, new;
6516 	int ret;
6517 
6518 	if (write && !rtnl_net_trylock(net))
6519 		return restart_syscall();
6520 
6521 	old = *valp;
6522 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6523 	new = *valp;
6524 
6525 	if (write && old != new) {
6526 		if (valp == &net->ipv6.devconf_dflt->proxy_ndp) {
6527 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6528 						     NETCONFA_PROXY_NEIGH,
6529 						     NETCONFA_IFINDEX_DEFAULT,
6530 						     net->ipv6.devconf_dflt);
6531 		} else if (valp == &net->ipv6.devconf_all->proxy_ndp) {
6532 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6533 						     NETCONFA_PROXY_NEIGH,
6534 						     NETCONFA_IFINDEX_ALL,
6535 						     net->ipv6.devconf_all);
6536 		} else {
6537 			struct inet6_dev *idev = ctl->extra1;
6538 
6539 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6540 						     NETCONFA_PROXY_NEIGH,
6541 						     idev->dev->ifindex,
6542 						     &idev->cnf);
6543 		}
6544 	}
6545 	if (write)
6546 		rtnl_net_unlock(net);
6547 
6548 	return ret;
6549 }
6550 
6551 static int addrconf_sysctl_addr_gen_mode(const struct ctl_table *ctl, int write,
6552 					 void *buffer, size_t *lenp,
6553 					 loff_t *ppos)
6554 {
6555 	int ret = 0;
6556 	u32 new_val;
6557 	struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6558 	struct net *net = (struct net *)ctl->extra2;
6559 	struct ctl_table tmp = {
6560 		.data = &new_val,
6561 		.maxlen = sizeof(new_val),
6562 		.mode = ctl->mode,
6563 	};
6564 
6565 	if (!rtnl_net_trylock(net))
6566 		return restart_syscall();
6567 
6568 	new_val = *((u32 *)ctl->data);
6569 
6570 	ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6571 	if (ret != 0)
6572 		goto out;
6573 
6574 	if (write) {
6575 		if (check_addr_gen_mode(new_val) < 0) {
6576 			ret = -EINVAL;
6577 			goto out;
6578 		}
6579 
6580 		if (idev) {
6581 			if (check_stable_privacy(idev, net, new_val) < 0) {
6582 				ret = -EINVAL;
6583 				goto out;
6584 			}
6585 
6586 			if (idev->cnf.addr_gen_mode != new_val) {
6587 				WRITE_ONCE(idev->cnf.addr_gen_mode, new_val);
6588 				netdev_lock_ops(idev->dev);
6589 				addrconf_init_auto_addrs(idev->dev);
6590 				netdev_unlock_ops(idev->dev);
6591 			}
6592 		} else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6593 			struct net_device *dev;
6594 
6595 			WRITE_ONCE(net->ipv6.devconf_dflt->addr_gen_mode, new_val);
6596 			for_each_netdev(net, dev) {
6597 				idev = __in6_dev_get_rtnl_net(dev);
6598 				if (idev &&
6599 				    idev->cnf.addr_gen_mode != new_val) {
6600 					WRITE_ONCE(idev->cnf.addr_gen_mode,
6601 						  new_val);
6602 					netdev_lock_ops(idev->dev);
6603 					addrconf_init_auto_addrs(idev->dev);
6604 					netdev_unlock_ops(idev->dev);
6605 				}
6606 			}
6607 		}
6608 
6609 		WRITE_ONCE(*((u32 *)ctl->data), new_val);
6610 	}
6611 
6612 out:
6613 	rtnl_net_unlock(net);
6614 
6615 	return ret;
6616 }
6617 
6618 static int addrconf_sysctl_stable_secret(const struct ctl_table *ctl, int write,
6619 					 void *buffer, size_t *lenp,
6620 					 loff_t *ppos)
6621 {
6622 	int err;
6623 	struct in6_addr addr;
6624 	char str[IPV6_MAX_STRLEN];
6625 	struct ctl_table lctl = *ctl;
6626 	struct net *net = ctl->extra2;
6627 	struct ipv6_stable_secret *secret = ctl->data;
6628 
6629 	if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6630 		return -EIO;
6631 
6632 	lctl.maxlen = IPV6_MAX_STRLEN;
6633 	lctl.data = str;
6634 
6635 	if (!rtnl_net_trylock(net))
6636 		return restart_syscall();
6637 
6638 	if (!write && !secret->initialized) {
6639 		err = -EIO;
6640 		goto out;
6641 	}
6642 
6643 	err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
6644 	if (err >= sizeof(str)) {
6645 		err = -EIO;
6646 		goto out;
6647 	}
6648 
6649 	err = proc_dostring(&lctl, write, buffer, lenp, ppos);
6650 	if (err || !write)
6651 		goto out;
6652 
6653 	if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
6654 		err = -EIO;
6655 		goto out;
6656 	}
6657 
6658 	secret->initialized = true;
6659 	secret->secret = addr;
6660 
6661 	if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6662 		struct net_device *dev;
6663 
6664 		for_each_netdev(net, dev) {
6665 			struct inet6_dev *idev = __in6_dev_get_rtnl_net(dev);
6666 
6667 			if (idev) {
6668 				WRITE_ONCE(idev->cnf.addr_gen_mode,
6669 					   IN6_ADDR_GEN_MODE_STABLE_PRIVACY);
6670 			}
6671 		}
6672 	} else {
6673 		struct inet6_dev *idev = ctl->extra1;
6674 
6675 		WRITE_ONCE(idev->cnf.addr_gen_mode,
6676 			   IN6_ADDR_GEN_MODE_STABLE_PRIVACY);
6677 	}
6678 
6679 out:
6680 	rtnl_net_unlock(net);
6681 
6682 	return err;
6683 }
6684 
6685 static
6686 int addrconf_sysctl_ignore_routes_with_linkdown(const struct ctl_table *ctl,
6687 						int write, void *buffer,
6688 						size_t *lenp,
6689 						loff_t *ppos)
6690 {
6691 	struct ctl_table lctl;
6692 	int *valp = ctl->data;
6693 	int val = *valp;
6694 	int ret;
6695 
6696 	/* ctl->data points to idev->cnf.ignore_routes_when_linkdown
6697 	 * we should not modify it until we get the rtnl lock.
6698 	 */
6699 	lctl = *ctl;
6700 	lctl.data = &val;
6701 
6702 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6703 	if (ret)
6704 		return ret;
6705 
6706 	if (write)
6707 		ret = addrconf_fixup_linkdown(ctl, valp, val);
6708 	return ret;
6709 }
6710 
6711 static
6712 void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6713 {
6714 	if (rt) {
6715 		if (action)
6716 			rt->dst.flags |= DST_NOPOLICY;
6717 		else
6718 			rt->dst.flags &= ~DST_NOPOLICY;
6719 	}
6720 }
6721 
6722 static
6723 void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6724 {
6725 	struct inet6_ifaddr *ifa;
6726 
6727 	read_lock_bh(&idev->lock);
6728 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
6729 		spin_lock(&ifa->lock);
6730 		if (ifa->rt) {
6731 			/* host routes only use builtin fib6_nh */
6732 			struct fib6_nh *nh = ifa->rt->fib6_nh;
6733 			int cpu;
6734 
6735 			rcu_read_lock();
6736 			ifa->rt->dst_nopolicy = val ? true : false;
6737 			if (nh->rt6i_pcpu) {
6738 				for_each_possible_cpu(cpu) {
6739 					struct rt6_info **rtp;
6740 
6741 					rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
6742 					addrconf_set_nopolicy(*rtp, val);
6743 				}
6744 			}
6745 			rcu_read_unlock();
6746 		}
6747 		spin_unlock(&ifa->lock);
6748 	}
6749 	read_unlock_bh(&idev->lock);
6750 }
6751 
6752 static
6753 int addrconf_disable_policy(const struct ctl_table *ctl, int *valp, int val)
6754 {
6755 	struct net *net = (struct net *)ctl->extra2;
6756 	struct inet6_dev *idev;
6757 
6758 	if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6759 		WRITE_ONCE(*valp, val);
6760 		return 0;
6761 	}
6762 
6763 	if (!rtnl_net_trylock(net))
6764 		return restart_syscall();
6765 
6766 	WRITE_ONCE(*valp, val);
6767 
6768 	if (valp == &net->ipv6.devconf_all->disable_policy)  {
6769 		struct net_device *dev;
6770 
6771 		for_each_netdev(net, dev) {
6772 			idev = __in6_dev_get_rtnl_net(dev);
6773 			if (idev)
6774 				addrconf_disable_policy_idev(idev, val);
6775 		}
6776 	} else {
6777 		idev = (struct inet6_dev *)ctl->extra1;
6778 		addrconf_disable_policy_idev(idev, val);
6779 	}
6780 
6781 	rtnl_net_unlock(net);
6782 	return 0;
6783 }
6784 
6785 static int addrconf_sysctl_disable_policy(const struct ctl_table *ctl, int write,
6786 				   void *buffer, size_t *lenp, loff_t *ppos)
6787 {
6788 	struct ctl_table lctl;
6789 	int *valp = ctl->data;
6790 	int val = *valp;
6791 	int ret;
6792 
6793 	lctl = *ctl;
6794 	lctl.data = &val;
6795 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6796 	if (ret)
6797 		return ret;
6798 
6799 	if (write && (*valp != val))
6800 		ret = addrconf_disable_policy(ctl, valp, val);
6801 
6802 	return ret;
6803 }
6804 
6805 static void addrconf_force_forward_change(struct net *net, __s32 newf)
6806 {
6807 	struct net_device *dev;
6808 	struct inet6_dev *idev;
6809 
6810 	for_each_netdev(net, dev) {
6811 		idev = __in6_dev_get_rtnl_net(dev);
6812 		if (idev) {
6813 			int changed = (!idev->cnf.force_forwarding) ^ (!newf);
6814 
6815 			WRITE_ONCE(idev->cnf.force_forwarding, newf);
6816 			if (changed)
6817 				inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
6818 							     NETCONFA_FORCE_FORWARDING,
6819 							     dev->ifindex, &idev->cnf);
6820 		}
6821 	}
6822 }
6823 
6824 static int addrconf_sysctl_force_forwarding(const struct ctl_table *ctl, int write,
6825 					    void *buffer, size_t *lenp, loff_t *ppos)
6826 {
6827 	struct inet6_dev *idev = ctl->extra1;
6828 	struct ctl_table tmp_ctl = *ctl;
6829 	struct net *net = ctl->extra2;
6830 	int *valp = ctl->data;
6831 	int new_val = *valp;
6832 	int old_val = *valp;
6833 	int ret;
6834 
6835 	tmp_ctl.extra1 = SYSCTL_ZERO;
6836 	tmp_ctl.extra2 = SYSCTL_ONE;
6837 	tmp_ctl.data = &new_val;
6838 
6839 	ret = proc_douintvec_minmax(&tmp_ctl, write, buffer, lenp, ppos);
6840 
6841 	if (write && old_val != new_val) {
6842 		if (!rtnl_net_trylock(net))
6843 			return restart_syscall();
6844 
6845 		WRITE_ONCE(*valp, new_val);
6846 
6847 		if (valp == &net->ipv6.devconf_dflt->force_forwarding) {
6848 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6849 						     NETCONFA_FORCE_FORWARDING,
6850 						     NETCONFA_IFINDEX_DEFAULT,
6851 						     net->ipv6.devconf_dflt);
6852 		} else if (valp == &net->ipv6.devconf_all->force_forwarding) {
6853 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6854 						     NETCONFA_FORCE_FORWARDING,
6855 						     NETCONFA_IFINDEX_ALL,
6856 						     net->ipv6.devconf_all);
6857 
6858 			addrconf_force_forward_change(net, new_val);
6859 		} else {
6860 			inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6861 						     NETCONFA_FORCE_FORWARDING,
6862 						     idev->dev->ifindex,
6863 						     &idev->cnf);
6864 		}
6865 		rtnl_net_unlock(net);
6866 	}
6867 
6868 	return ret;
6869 }
6870 
6871 static int minus_one = -1;
6872 static const int two_five_five = 255;
6873 static u32 ioam6_if_id_max = U16_MAX;
6874 
6875 static const struct ctl_table addrconf_sysctl[] = {
6876 	{
6877 		.procname	= "forwarding",
6878 		.data		= &ipv6_devconf.forwarding,
6879 		.maxlen		= sizeof(int),
6880 		.mode		= 0644,
6881 		.proc_handler	= addrconf_sysctl_forward,
6882 	},
6883 	{
6884 		.procname	= "hop_limit",
6885 		.data		= &ipv6_devconf.hop_limit,
6886 		.maxlen		= sizeof(int),
6887 		.mode		= 0644,
6888 		.proc_handler	= proc_dointvec_minmax,
6889 		.extra1		= (void *)SYSCTL_ONE,
6890 		.extra2		= (void *)&two_five_five,
6891 	},
6892 	{
6893 		.procname	= "mtu",
6894 		.data		= &ipv6_devconf.mtu6,
6895 		.maxlen		= sizeof(int),
6896 		.mode		= 0644,
6897 		.proc_handler	= addrconf_sysctl_mtu,
6898 	},
6899 	{
6900 		.procname	= "accept_ra",
6901 		.data		= &ipv6_devconf.accept_ra,
6902 		.maxlen		= sizeof(int),
6903 		.mode		= 0644,
6904 		.proc_handler	= proc_dointvec,
6905 	},
6906 	{
6907 		.procname	= "accept_redirects",
6908 		.data		= &ipv6_devconf.accept_redirects,
6909 		.maxlen		= sizeof(int),
6910 		.mode		= 0644,
6911 		.proc_handler	= proc_dointvec,
6912 	},
6913 	{
6914 		.procname	= "autoconf",
6915 		.data		= &ipv6_devconf.autoconf,
6916 		.maxlen		= sizeof(int),
6917 		.mode		= 0644,
6918 		.proc_handler	= proc_dointvec,
6919 	},
6920 	{
6921 		.procname	= "dad_transmits",
6922 		.data		= &ipv6_devconf.dad_transmits,
6923 		.maxlen		= sizeof(int),
6924 		.mode		= 0644,
6925 		.proc_handler	= proc_dointvec,
6926 	},
6927 	{
6928 		.procname	= "router_solicitations",
6929 		.data		= &ipv6_devconf.rtr_solicits,
6930 		.maxlen		= sizeof(int),
6931 		.mode		= 0644,
6932 		.proc_handler	= proc_dointvec_minmax,
6933 		.extra1		= &minus_one,
6934 	},
6935 	{
6936 		.procname	= "router_solicitation_interval",
6937 		.data		= &ipv6_devconf.rtr_solicit_interval,
6938 		.maxlen		= sizeof(int),
6939 		.mode		= 0644,
6940 		.proc_handler	= proc_dointvec_jiffies,
6941 	},
6942 	{
6943 		.procname	= "router_solicitation_max_interval",
6944 		.data		= &ipv6_devconf.rtr_solicit_max_interval,
6945 		.maxlen		= sizeof(int),
6946 		.mode		= 0644,
6947 		.proc_handler	= proc_dointvec_jiffies,
6948 	},
6949 	{
6950 		.procname	= "router_solicitation_delay",
6951 		.data		= &ipv6_devconf.rtr_solicit_delay,
6952 		.maxlen		= sizeof(int),
6953 		.mode		= 0644,
6954 		.proc_handler	= proc_dointvec_jiffies,
6955 	},
6956 	{
6957 		.procname	= "force_mld_version",
6958 		.data		= &ipv6_devconf.force_mld_version,
6959 		.maxlen		= sizeof(int),
6960 		.mode		= 0644,
6961 		.proc_handler	= proc_dointvec,
6962 	},
6963 	{
6964 		.procname	= "mldv1_unsolicited_report_interval",
6965 		.data		=
6966 			&ipv6_devconf.mldv1_unsolicited_report_interval,
6967 		.maxlen		= sizeof(int),
6968 		.mode		= 0644,
6969 		.proc_handler	= proc_dointvec_ms_jiffies,
6970 	},
6971 	{
6972 		.procname	= "mldv2_unsolicited_report_interval",
6973 		.data		=
6974 			&ipv6_devconf.mldv2_unsolicited_report_interval,
6975 		.maxlen		= sizeof(int),
6976 		.mode		= 0644,
6977 		.proc_handler	= proc_dointvec_ms_jiffies,
6978 	},
6979 	{
6980 		.procname	= "use_tempaddr",
6981 		.data		= &ipv6_devconf.use_tempaddr,
6982 		.maxlen		= sizeof(int),
6983 		.mode		= 0644,
6984 		.proc_handler	= proc_dointvec,
6985 	},
6986 	{
6987 		.procname	= "temp_valid_lft",
6988 		.data		= &ipv6_devconf.temp_valid_lft,
6989 		.maxlen		= sizeof(int),
6990 		.mode		= 0644,
6991 		.proc_handler	= proc_dointvec,
6992 	},
6993 	{
6994 		.procname	= "temp_prefered_lft",
6995 		.data		= &ipv6_devconf.temp_prefered_lft,
6996 		.maxlen		= sizeof(int),
6997 		.mode		= 0644,
6998 		.proc_handler	= proc_dointvec,
6999 	},
7000 	{
7001 		.procname       = "regen_min_advance",
7002 		.data           = &ipv6_devconf.regen_min_advance,
7003 		.maxlen         = sizeof(int),
7004 		.mode           = 0644,
7005 		.proc_handler   = proc_dointvec,
7006 	},
7007 	{
7008 		.procname	= "regen_max_retry",
7009 		.data		= &ipv6_devconf.regen_max_retry,
7010 		.maxlen		= sizeof(int),
7011 		.mode		= 0644,
7012 		.proc_handler	= proc_dointvec,
7013 	},
7014 	{
7015 		.procname	= "max_desync_factor",
7016 		.data		= &ipv6_devconf.max_desync_factor,
7017 		.maxlen		= sizeof(int),
7018 		.mode		= 0644,
7019 		.proc_handler	= proc_dointvec,
7020 	},
7021 	{
7022 		.procname	= "max_addresses",
7023 		.data		= &ipv6_devconf.max_addresses,
7024 		.maxlen		= sizeof(int),
7025 		.mode		= 0644,
7026 		.proc_handler	= proc_dointvec,
7027 	},
7028 	{
7029 		.procname	= "accept_ra_defrtr",
7030 		.data		= &ipv6_devconf.accept_ra_defrtr,
7031 		.maxlen		= sizeof(int),
7032 		.mode		= 0644,
7033 		.proc_handler	= proc_dointvec,
7034 	},
7035 	{
7036 		.procname	= "ra_defrtr_metric",
7037 		.data		= &ipv6_devconf.ra_defrtr_metric,
7038 		.maxlen		= sizeof(u32),
7039 		.mode		= 0644,
7040 		.proc_handler	= proc_douintvec_minmax,
7041 		.extra1		= (void *)SYSCTL_ONE,
7042 	},
7043 	{
7044 		.procname	= "accept_ra_min_hop_limit",
7045 		.data		= &ipv6_devconf.accept_ra_min_hop_limit,
7046 		.maxlen		= sizeof(int),
7047 		.mode		= 0644,
7048 		.proc_handler	= proc_dointvec,
7049 	},
7050 	{
7051 		.procname	= "accept_ra_min_lft",
7052 		.data		= &ipv6_devconf.accept_ra_min_lft,
7053 		.maxlen		= sizeof(int),
7054 		.mode		= 0644,
7055 		.proc_handler	= proc_dointvec,
7056 	},
7057 	{
7058 		.procname	= "accept_ra_pinfo",
7059 		.data		= &ipv6_devconf.accept_ra_pinfo,
7060 		.maxlen		= sizeof(int),
7061 		.mode		= 0644,
7062 		.proc_handler	= proc_dointvec,
7063 	},
7064 	{
7065 		.procname	= "ra_honor_pio_life",
7066 		.data		= &ipv6_devconf.ra_honor_pio_life,
7067 		.maxlen		= sizeof(u8),
7068 		.mode		= 0644,
7069 		.proc_handler	= proc_dou8vec_minmax,
7070 		.extra1		= SYSCTL_ZERO,
7071 		.extra2		= SYSCTL_ONE,
7072 	},
7073 	{
7074 		.procname	= "ra_honor_pio_pflag",
7075 		.data		= &ipv6_devconf.ra_honor_pio_pflag,
7076 		.maxlen		= sizeof(u8),
7077 		.mode		= 0644,
7078 		.proc_handler	= proc_dou8vec_minmax,
7079 		.extra1		= SYSCTL_ZERO,
7080 		.extra2		= SYSCTL_ONE,
7081 	},
7082 #ifdef CONFIG_IPV6_ROUTER_PREF
7083 	{
7084 		.procname	= "accept_ra_rtr_pref",
7085 		.data		= &ipv6_devconf.accept_ra_rtr_pref,
7086 		.maxlen		= sizeof(int),
7087 		.mode		= 0644,
7088 		.proc_handler	= proc_dointvec,
7089 	},
7090 	{
7091 		.procname	= "router_probe_interval",
7092 		.data		= &ipv6_devconf.rtr_probe_interval,
7093 		.maxlen		= sizeof(int),
7094 		.mode		= 0644,
7095 		.proc_handler	= proc_dointvec_jiffies,
7096 	},
7097 #ifdef CONFIG_IPV6_ROUTE_INFO
7098 	{
7099 		.procname	= "accept_ra_rt_info_min_plen",
7100 		.data		= &ipv6_devconf.accept_ra_rt_info_min_plen,
7101 		.maxlen		= sizeof(int),
7102 		.mode		= 0644,
7103 		.proc_handler	= proc_dointvec,
7104 	},
7105 	{
7106 		.procname	= "accept_ra_rt_info_max_plen",
7107 		.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
7108 		.maxlen		= sizeof(int),
7109 		.mode		= 0644,
7110 		.proc_handler	= proc_dointvec,
7111 	},
7112 #endif
7113 #endif
7114 	{
7115 		.procname	= "proxy_ndp",
7116 		.data		= &ipv6_devconf.proxy_ndp,
7117 		.maxlen		= sizeof(int),
7118 		.mode		= 0644,
7119 		.proc_handler	= addrconf_sysctl_proxy_ndp,
7120 	},
7121 	{
7122 		.procname	= "accept_source_route",
7123 		.data		= &ipv6_devconf.accept_source_route,
7124 		.maxlen		= sizeof(int),
7125 		.mode		= 0644,
7126 		.proc_handler	= proc_dointvec,
7127 	},
7128 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
7129 	{
7130 		.procname	= "optimistic_dad",
7131 		.data		= &ipv6_devconf.optimistic_dad,
7132 		.maxlen		= sizeof(int),
7133 		.mode		= 0644,
7134 		.proc_handler   = proc_dointvec,
7135 	},
7136 	{
7137 		.procname	= "use_optimistic",
7138 		.data		= &ipv6_devconf.use_optimistic,
7139 		.maxlen		= sizeof(int),
7140 		.mode		= 0644,
7141 		.proc_handler	= proc_dointvec,
7142 	},
7143 #endif
7144 #ifdef CONFIG_IPV6_MROUTE
7145 	{
7146 		.procname	= "mc_forwarding",
7147 		.data		= &ipv6_devconf.mc_forwarding,
7148 		.maxlen		= sizeof(int),
7149 		.mode		= 0444,
7150 		.proc_handler	= proc_dointvec,
7151 	},
7152 #endif
7153 	{
7154 		.procname	= "disable_ipv6",
7155 		.data		= &ipv6_devconf.disable_ipv6,
7156 		.maxlen		= sizeof(int),
7157 		.mode		= 0644,
7158 		.proc_handler	= addrconf_sysctl_disable,
7159 	},
7160 	{
7161 		.procname	= "accept_dad",
7162 		.data		= &ipv6_devconf.accept_dad,
7163 		.maxlen		= sizeof(int),
7164 		.mode		= 0644,
7165 		.proc_handler	= proc_dointvec,
7166 	},
7167 	{
7168 		.procname	= "force_tllao",
7169 		.data		= &ipv6_devconf.force_tllao,
7170 		.maxlen		= sizeof(int),
7171 		.mode		= 0644,
7172 		.proc_handler	= proc_dointvec
7173 	},
7174 	{
7175 		.procname	= "ndisc_notify",
7176 		.data		= &ipv6_devconf.ndisc_notify,
7177 		.maxlen		= sizeof(int),
7178 		.mode		= 0644,
7179 		.proc_handler	= proc_dointvec
7180 	},
7181 	{
7182 		.procname	= "suppress_frag_ndisc",
7183 		.data		= &ipv6_devconf.suppress_frag_ndisc,
7184 		.maxlen		= sizeof(int),
7185 		.mode		= 0644,
7186 		.proc_handler	= proc_dointvec
7187 	},
7188 	{
7189 		.procname	= "accept_ra_from_local",
7190 		.data		= &ipv6_devconf.accept_ra_from_local,
7191 		.maxlen		= sizeof(int),
7192 		.mode		= 0644,
7193 		.proc_handler	= proc_dointvec,
7194 	},
7195 	{
7196 		.procname	= "accept_ra_mtu",
7197 		.data		= &ipv6_devconf.accept_ra_mtu,
7198 		.maxlen		= sizeof(int),
7199 		.mode		= 0644,
7200 		.proc_handler	= proc_dointvec,
7201 	},
7202 	{
7203 		.procname	= "stable_secret",
7204 		.data		= &ipv6_devconf.stable_secret,
7205 		.maxlen		= IPV6_MAX_STRLEN,
7206 		.mode		= 0600,
7207 		.proc_handler	= addrconf_sysctl_stable_secret,
7208 	},
7209 	{
7210 		.procname	= "use_oif_addrs_only",
7211 		.data		= &ipv6_devconf.use_oif_addrs_only,
7212 		.maxlen		= sizeof(int),
7213 		.mode		= 0644,
7214 		.proc_handler	= proc_dointvec,
7215 	},
7216 	{
7217 		.procname	= "ignore_routes_with_linkdown",
7218 		.data		= &ipv6_devconf.ignore_routes_with_linkdown,
7219 		.maxlen		= sizeof(int),
7220 		.mode		= 0644,
7221 		.proc_handler	= addrconf_sysctl_ignore_routes_with_linkdown,
7222 	},
7223 	{
7224 		.procname	= "drop_unicast_in_l2_multicast",
7225 		.data		= &ipv6_devconf.drop_unicast_in_l2_multicast,
7226 		.maxlen		= sizeof(int),
7227 		.mode		= 0644,
7228 		.proc_handler	= proc_dointvec,
7229 	},
7230 	{
7231 		.procname	= "drop_unsolicited_na",
7232 		.data		= &ipv6_devconf.drop_unsolicited_na,
7233 		.maxlen		= sizeof(int),
7234 		.mode		= 0644,
7235 		.proc_handler	= proc_dointvec,
7236 	},
7237 	{
7238 		.procname	= "keep_addr_on_down",
7239 		.data		= &ipv6_devconf.keep_addr_on_down,
7240 		.maxlen		= sizeof(int),
7241 		.mode		= 0644,
7242 		.proc_handler	= proc_dointvec,
7243 
7244 	},
7245 	{
7246 		.procname	= "seg6_enabled",
7247 		.data		= &ipv6_devconf.seg6_enabled,
7248 		.maxlen		= sizeof(int),
7249 		.mode		= 0644,
7250 		.proc_handler	= proc_dointvec,
7251 	},
7252 #ifdef CONFIG_IPV6_SEG6_HMAC
7253 	{
7254 		.procname	= "seg6_require_hmac",
7255 		.data		= &ipv6_devconf.seg6_require_hmac,
7256 		.maxlen		= sizeof(int),
7257 		.mode		= 0644,
7258 		.proc_handler	= proc_dointvec,
7259 	},
7260 #endif
7261 	{
7262 		.procname       = "enhanced_dad",
7263 		.data           = &ipv6_devconf.enhanced_dad,
7264 		.maxlen         = sizeof(int),
7265 		.mode           = 0644,
7266 		.proc_handler   = proc_dointvec,
7267 	},
7268 	{
7269 		.procname	= "addr_gen_mode",
7270 		.data		= &ipv6_devconf.addr_gen_mode,
7271 		.maxlen		= sizeof(int),
7272 		.mode		= 0644,
7273 		.proc_handler	= addrconf_sysctl_addr_gen_mode,
7274 	},
7275 	{
7276 		.procname       = "disable_policy",
7277 		.data           = &ipv6_devconf.disable_policy,
7278 		.maxlen         = sizeof(int),
7279 		.mode           = 0644,
7280 		.proc_handler   = addrconf_sysctl_disable_policy,
7281 	},
7282 	{
7283 		.procname	= "ndisc_tclass",
7284 		.data		= &ipv6_devconf.ndisc_tclass,
7285 		.maxlen		= sizeof(int),
7286 		.mode		= 0644,
7287 		.proc_handler	= proc_dointvec_minmax,
7288 		.extra1		= (void *)SYSCTL_ZERO,
7289 		.extra2		= (void *)&two_five_five,
7290 	},
7291 	{
7292 		.procname	= "rpl_seg_enabled",
7293 		.data		= &ipv6_devconf.rpl_seg_enabled,
7294 		.maxlen		= sizeof(int),
7295 		.mode		= 0644,
7296 		.proc_handler   = proc_dointvec_minmax,
7297 		.extra1         = SYSCTL_ZERO,
7298 		.extra2         = SYSCTL_ONE,
7299 	},
7300 	{
7301 		.procname	= "ioam6_enabled",
7302 		.data		= &ipv6_devconf.ioam6_enabled,
7303 		.maxlen		= sizeof(u8),
7304 		.mode		= 0644,
7305 		.proc_handler	= proc_dou8vec_minmax,
7306 		.extra1		= (void *)SYSCTL_ZERO,
7307 		.extra2		= (void *)SYSCTL_ONE,
7308 	},
7309 	{
7310 		.procname	= "ioam6_id",
7311 		.data		= &ipv6_devconf.ioam6_id,
7312 		.maxlen		= sizeof(u32),
7313 		.mode		= 0644,
7314 		.proc_handler	= proc_douintvec_minmax,
7315 		.extra1		= (void *)SYSCTL_ZERO,
7316 		.extra2		= (void *)&ioam6_if_id_max,
7317 	},
7318 	{
7319 		.procname	= "ioam6_id_wide",
7320 		.data		= &ipv6_devconf.ioam6_id_wide,
7321 		.maxlen		= sizeof(u32),
7322 		.mode		= 0644,
7323 		.proc_handler	= proc_douintvec,
7324 	},
7325 	{
7326 		.procname	= "ndisc_evict_nocarrier",
7327 		.data		= &ipv6_devconf.ndisc_evict_nocarrier,
7328 		.maxlen		= sizeof(u8),
7329 		.mode		= 0644,
7330 		.proc_handler	= proc_dou8vec_minmax,
7331 		.extra1		= (void *)SYSCTL_ZERO,
7332 		.extra2		= (void *)SYSCTL_ONE,
7333 	},
7334 	{
7335 		.procname	= "accept_untracked_na",
7336 		.data		= &ipv6_devconf.accept_untracked_na,
7337 		.maxlen		= sizeof(int),
7338 		.mode		= 0644,
7339 		.proc_handler	= proc_dointvec_minmax,
7340 		.extra1		= SYSCTL_ZERO,
7341 		.extra2		= SYSCTL_TWO,
7342 	},
7343 	{
7344 		.procname	= "force_forwarding",
7345 		.data		= &ipv6_devconf.force_forwarding,
7346 		.maxlen		= sizeof(int),
7347 		.mode		= 0644,
7348 		.proc_handler	= addrconf_sysctl_force_forwarding,
7349 	},
7350 };
7351 
7352 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
7353 		struct inet6_dev *idev, struct ipv6_devconf *p)
7354 {
7355 	size_t table_size = ARRAY_SIZE(addrconf_sysctl);
7356 	int i, ifindex;
7357 	struct ctl_table *table;
7358 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
7359 
7360 	table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL_ACCOUNT);
7361 	if (!table)
7362 		goto out;
7363 
7364 	for (i = 0; i < table_size; i++) {
7365 		table[i].data += (char *)p - (char *)&ipv6_devconf;
7366 		/* If one of these is already set, then it is not safe to
7367 		 * overwrite either of them: this makes proc_dointvec_minmax
7368 		 * usable.
7369 		 */
7370 		if (!table[i].extra1 && !table[i].extra2) {
7371 			table[i].extra1 = idev; /* embedded; no ref */
7372 			table[i].extra2 = net;
7373 		}
7374 	}
7375 
7376 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
7377 
7378 	p->sysctl_header = register_net_sysctl_sz(net, path, table,
7379 						  table_size);
7380 	if (!p->sysctl_header)
7381 		goto free;
7382 
7383 	if (!strcmp(dev_name, "all"))
7384 		ifindex = NETCONFA_IFINDEX_ALL;
7385 	else if (!strcmp(dev_name, "default"))
7386 		ifindex = NETCONFA_IFINDEX_DEFAULT;
7387 	else
7388 		ifindex = idev->dev->ifindex;
7389 	inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
7390 				     ifindex, p);
7391 	return 0;
7392 
7393 free:
7394 	kfree(table);
7395 out:
7396 	return -ENOBUFS;
7397 }
7398 
7399 static void __addrconf_sysctl_unregister(struct net *net,
7400 					 struct ipv6_devconf *p, int ifindex)
7401 {
7402 	const struct ctl_table *table;
7403 
7404 	if (!p->sysctl_header)
7405 		return;
7406 
7407 	table = p->sysctl_header->ctl_table_arg;
7408 	unregister_net_sysctl_table(p->sysctl_header);
7409 	p->sysctl_header = NULL;
7410 	kfree(table);
7411 
7412 	inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
7413 }
7414 
7415 static int addrconf_sysctl_register(struct inet6_dev *idev)
7416 {
7417 	int err;
7418 
7419 	if (!sysctl_dev_name_is_allowed(idev->dev->name))
7420 		return -EINVAL;
7421 
7422 	err = neigh_sysctl_register(idev->dev, idev->nd_parms,
7423 				    &ndisc_ifinfo_sysctl_change);
7424 	if (err)
7425 		return err;
7426 	err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
7427 					 idev, &idev->cnf);
7428 	if (err)
7429 		neigh_sysctl_unregister(idev->nd_parms);
7430 
7431 	return err;
7432 }
7433 
7434 static void addrconf_sysctl_unregister(struct inet6_dev *idev)
7435 {
7436 	__addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
7437 				     idev->dev->ifindex);
7438 	neigh_sysctl_unregister(idev->nd_parms);
7439 }
7440 
7441 
7442 #endif
7443 
7444 static int __net_init addrconf_init_net(struct net *net)
7445 {
7446 	int err = -ENOMEM;
7447 	struct ipv6_devconf *all, *dflt;
7448 
7449 	spin_lock_init(&net->ipv6.addrconf_hash_lock);
7450 	INIT_DEFERRABLE_WORK(&net->ipv6.addr_chk_work, addrconf_verify_work);
7451 	net->ipv6.inet6_addr_lst = kzalloc_objs(struct hlist_head,
7452 						IN6_ADDR_HSIZE);
7453 	if (!net->ipv6.inet6_addr_lst)
7454 		goto err_alloc_addr;
7455 
7456 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
7457 	if (!all)
7458 		goto err_alloc_all;
7459 
7460 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
7461 	if (!dflt)
7462 		goto err_alloc_dflt;
7463 
7464 	if (!net_eq(net, &init_net)) {
7465 		switch (net_inherit_devconf()) {
7466 		case 1:  /* copy from init_net */
7467 			memcpy(all, init_net.ipv6.devconf_all,
7468 			       sizeof(ipv6_devconf));
7469 			memcpy(dflt, init_net.ipv6.devconf_dflt,
7470 			       sizeof(ipv6_devconf_dflt));
7471 			break;
7472 		case 3: /* copy from the current netns */
7473 			memcpy(all, current->nsproxy->net_ns->ipv6.devconf_all,
7474 			       sizeof(ipv6_devconf));
7475 			memcpy(dflt,
7476 			       current->nsproxy->net_ns->ipv6.devconf_dflt,
7477 			       sizeof(ipv6_devconf_dflt));
7478 			break;
7479 		case 0:
7480 		case 2:
7481 			/* use compiled values */
7482 			break;
7483 		}
7484 	}
7485 
7486 	/* these will be inherited by all namespaces */
7487 	dflt->autoconf = ipv6_defaults.autoconf;
7488 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
7489 
7490 	dflt->stable_secret.initialized = false;
7491 	all->stable_secret.initialized = false;
7492 
7493 	net->ipv6.devconf_all = all;
7494 	net->ipv6.devconf_dflt = dflt;
7495 
7496 #ifdef CONFIG_SYSCTL
7497 	err = __addrconf_sysctl_register(net, "all", NULL, all);
7498 	if (err < 0)
7499 		goto err_reg_all;
7500 
7501 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
7502 	if (err < 0)
7503 		goto err_reg_dflt;
7504 #endif
7505 	return 0;
7506 
7507 #ifdef CONFIG_SYSCTL
7508 err_reg_dflt:
7509 	__addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
7510 err_reg_all:
7511 	kfree(dflt);
7512 	net->ipv6.devconf_dflt = NULL;
7513 #endif
7514 err_alloc_dflt:
7515 	kfree(all);
7516 	net->ipv6.devconf_all = NULL;
7517 err_alloc_all:
7518 	kfree(net->ipv6.inet6_addr_lst);
7519 err_alloc_addr:
7520 	return err;
7521 }
7522 
7523 static void __net_exit addrconf_exit_net(struct net *net)
7524 {
7525 	int i;
7526 
7527 #ifdef CONFIG_SYSCTL
7528 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
7529 				     NETCONFA_IFINDEX_DEFAULT);
7530 	__addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
7531 				     NETCONFA_IFINDEX_ALL);
7532 #endif
7533 	kfree(net->ipv6.devconf_dflt);
7534 	net->ipv6.devconf_dflt = NULL;
7535 	kfree(net->ipv6.devconf_all);
7536 	net->ipv6.devconf_all = NULL;
7537 
7538 	cancel_delayed_work_sync(&net->ipv6.addr_chk_work);
7539 	/*
7540 	 *	Check hash table, then free it.
7541 	 */
7542 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
7543 		WARN_ON_ONCE(!hlist_empty(&net->ipv6.inet6_addr_lst[i]));
7544 
7545 	kfree(net->ipv6.inet6_addr_lst);
7546 	net->ipv6.inet6_addr_lst = NULL;
7547 }
7548 
7549 static struct pernet_operations addrconf_ops = {
7550 	.init = addrconf_init_net,
7551 	.exit = addrconf_exit_net,
7552 };
7553 
7554 static struct rtnl_af_ops inet6_ops __read_mostly = {
7555 	.family		  = AF_INET6,
7556 	.fill_link_af	  = inet6_fill_link_af,
7557 	.get_link_af_size = inet6_get_link_af_size,
7558 	.validate_link_af = inet6_validate_link_af,
7559 	.set_link_af	  = inet6_set_link_af,
7560 };
7561 
7562 static const struct rtnl_msg_handler addrconf_rtnl_msg_handlers[] __initconst_or_module = {
7563 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETLINK,
7564 	 .dumpit = inet6_dump_ifinfo, .flags = RTNL_FLAG_DUMP_UNLOCKED},
7565 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_NEWADDR,
7566 	 .doit = inet6_rtm_newaddr, .flags = RTNL_FLAG_DOIT_PERNET},
7567 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_DELADDR,
7568 	 .doit = inet6_rtm_deladdr, .flags = RTNL_FLAG_DOIT_PERNET},
7569 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETADDR,
7570 	 .doit = inet6_rtm_getaddr, .dumpit = inet6_dump_ifaddr,
7571 	 .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
7572 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETMULTICAST,
7573 	 .dumpit = inet6_dump_ifmcaddr,
7574 	 .flags = RTNL_FLAG_DUMP_UNLOCKED},
7575 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETANYCAST,
7576 	 .dumpit = inet6_dump_ifacaddr,
7577 	 .flags = RTNL_FLAG_DUMP_UNLOCKED},
7578 	{.owner = THIS_MODULE, .protocol = PF_INET6, .msgtype = RTM_GETNETCONF,
7579 	 .doit = inet6_netconf_get_devconf, .dumpit = inet6_netconf_dump_devconf,
7580 	 .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED},
7581 };
7582 
7583 /*
7584  *	Init / cleanup code
7585  */
7586 
7587 int __init addrconf_init(void)
7588 {
7589 	struct inet6_dev *idev;
7590 	int err;
7591 
7592 	err = ipv6_addr_label_init();
7593 	if (err < 0) {
7594 		pr_crit("%s: cannot initialize default policy table: %d\n",
7595 			__func__, err);
7596 		goto out;
7597 	}
7598 
7599 	err = register_pernet_subsys(&addrconf_ops);
7600 	if (err < 0)
7601 		goto out_addrlabel;
7602 
7603 	/* All works using addrconf_wq need to lock rtnl. */
7604 	addrconf_wq = create_singlethread_workqueue("ipv6_addrconf");
7605 	if (!addrconf_wq) {
7606 		err = -ENOMEM;
7607 		goto out_nowq;
7608 	}
7609 
7610 	rtnl_net_lock(&init_net);
7611 	idev = ipv6_add_dev(blackhole_netdev);
7612 	rtnl_net_unlock(&init_net);
7613 	if (IS_ERR(idev)) {
7614 		err = PTR_ERR(idev);
7615 		goto errlo;
7616 	}
7617 
7618 	ip6_route_init_special_entries();
7619 
7620 	register_netdevice_notifier(&ipv6_dev_notf);
7621 
7622 	addrconf_verify(&init_net);
7623 
7624 	err = rtnl_af_register(&inet6_ops);
7625 	if (err)
7626 		goto erraf;
7627 
7628 	err = rtnl_register_many(addrconf_rtnl_msg_handlers);
7629 	if (err)
7630 		goto errout;
7631 
7632 	err = ipv6_addr_label_rtnl_register();
7633 	if (err < 0)
7634 		goto errout;
7635 
7636 	return 0;
7637 errout:
7638 	rtnl_unregister_all(PF_INET6);
7639 	rtnl_af_unregister(&inet6_ops);
7640 erraf:
7641 	unregister_netdevice_notifier(&ipv6_dev_notf);
7642 errlo:
7643 	destroy_workqueue(addrconf_wq);
7644 out_nowq:
7645 	unregister_pernet_subsys(&addrconf_ops);
7646 out_addrlabel:
7647 	ipv6_addr_label_cleanup();
7648 out:
7649 	return err;
7650 }
7651 
7652 void addrconf_cleanup(void)
7653 {
7654 	struct net_device *dev;
7655 
7656 	unregister_netdevice_notifier(&ipv6_dev_notf);
7657 	unregister_pernet_subsys(&addrconf_ops);
7658 	ipv6_addr_label_cleanup();
7659 
7660 	rtnl_af_unregister(&inet6_ops);
7661 
7662 	rtnl_net_lock(&init_net);
7663 
7664 	/* clean dev list */
7665 	for_each_netdev(&init_net, dev) {
7666 		if (!__in6_dev_get_rtnl_net(dev))
7667 			continue;
7668 		addrconf_ifdown(dev, true);
7669 	}
7670 	addrconf_ifdown(init_net.loopback_dev, true);
7671 
7672 	rtnl_net_unlock(&init_net);
7673 
7674 	destroy_workqueue(addrconf_wq);
7675 }
7676