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