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