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