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