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