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