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