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