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