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