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