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