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