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