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