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