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