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