xref: /openbmc/linux/net/ipv6/route.c (revision 7d9e5f422150ed00de744e02a80734d74cc9704d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Linux INET6 implementation
4  *	FIB front-end.
5  *
6  *	Authors:
7  *	Pedro Roque		<roque@di.fc.ul.pt>
8  */
9 
10 /*	Changes:
11  *
12  *	YOSHIFUJI Hideaki @USAGI
13  *		reworked default router selection.
14  *		- respect outgoing interface
15  *		- select from (probably) reachable routers (i.e.
16  *		routers in REACHABLE, STALE, DELAY or PROBE states).
17  *		- always select the same router if it is (probably)
18  *		reachable.  otherwise, round-robin the list.
19  *	Ville Nuorvala
20  *		Fixed routing subtrees.
21  */
22 
23 #define pr_fmt(fmt) "IPv6: " fmt
24 
25 #include <linux/capability.h>
26 #include <linux/errno.h>
27 #include <linux/export.h>
28 #include <linux/types.h>
29 #include <linux/times.h>
30 #include <linux/socket.h>
31 #include <linux/sockios.h>
32 #include <linux/net.h>
33 #include <linux/route.h>
34 #include <linux/netdevice.h>
35 #include <linux/in6.h>
36 #include <linux/mroute6.h>
37 #include <linux/init.h>
38 #include <linux/if_arp.h>
39 #include <linux/proc_fs.h>
40 #include <linux/seq_file.h>
41 #include <linux/nsproxy.h>
42 #include <linux/slab.h>
43 #include <linux/jhash.h>
44 #include <net/net_namespace.h>
45 #include <net/snmp.h>
46 #include <net/ipv6.h>
47 #include <net/ip6_fib.h>
48 #include <net/ip6_route.h>
49 #include <net/ndisc.h>
50 #include <net/addrconf.h>
51 #include <net/tcp.h>
52 #include <linux/rtnetlink.h>
53 #include <net/dst.h>
54 #include <net/dst_metadata.h>
55 #include <net/xfrm.h>
56 #include <net/netevent.h>
57 #include <net/netlink.h>
58 #include <net/rtnh.h>
59 #include <net/lwtunnel.h>
60 #include <net/ip_tunnels.h>
61 #include <net/l3mdev.h>
62 #include <net/ip.h>
63 #include <linux/uaccess.h>
64 
65 #ifdef CONFIG_SYSCTL
66 #include <linux/sysctl.h>
67 #endif
68 
69 static int ip6_rt_type_to_error(u8 fib6_type);
70 
71 #define CREATE_TRACE_POINTS
72 #include <trace/events/fib6.h>
73 EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
74 #undef CREATE_TRACE_POINTS
75 
76 enum rt6_nud_state {
77 	RT6_NUD_FAIL_HARD = -3,
78 	RT6_NUD_FAIL_PROBE = -2,
79 	RT6_NUD_FAIL_DO_RR = -1,
80 	RT6_NUD_SUCCEED = 1
81 };
82 
83 static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
84 static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
85 static unsigned int	 ip6_mtu(const struct dst_entry *dst);
86 static struct dst_entry *ip6_negative_advice(struct dst_entry *);
87 static void		ip6_dst_destroy(struct dst_entry *);
88 static void		ip6_dst_ifdown(struct dst_entry *,
89 				       struct net_device *dev, int how);
90 static int		 ip6_dst_gc(struct dst_ops *ops);
91 
92 static int		ip6_pkt_discard(struct sk_buff *skb);
93 static int		ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
94 static int		ip6_pkt_prohibit(struct sk_buff *skb);
95 static int		ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
96 static void		ip6_link_failure(struct sk_buff *skb);
97 static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
98 					   struct sk_buff *skb, u32 mtu);
99 static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
100 					struct sk_buff *skb);
101 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
102 			   int strict);
103 static size_t rt6_nlmsg_size(struct fib6_info *f6i);
104 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
105 			 struct fib6_info *rt, struct dst_entry *dst,
106 			 struct in6_addr *dest, struct in6_addr *src,
107 			 int iif, int type, u32 portid, u32 seq,
108 			 unsigned int flags);
109 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
110 					   const struct in6_addr *daddr,
111 					   const struct in6_addr *saddr);
112 
113 #ifdef CONFIG_IPV6_ROUTE_INFO
114 static struct fib6_info *rt6_add_route_info(struct net *net,
115 					   const struct in6_addr *prefix, int prefixlen,
116 					   const struct in6_addr *gwaddr,
117 					   struct net_device *dev,
118 					   unsigned int pref);
119 static struct fib6_info *rt6_get_route_info(struct net *net,
120 					   const struct in6_addr *prefix, int prefixlen,
121 					   const struct in6_addr *gwaddr,
122 					   struct net_device *dev);
123 #endif
124 
125 struct uncached_list {
126 	spinlock_t		lock;
127 	struct list_head	head;
128 };
129 
130 static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
131 
132 void rt6_uncached_list_add(struct rt6_info *rt)
133 {
134 	struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
135 
136 	rt->rt6i_uncached_list = ul;
137 
138 	spin_lock_bh(&ul->lock);
139 	list_add_tail(&rt->rt6i_uncached, &ul->head);
140 	spin_unlock_bh(&ul->lock);
141 }
142 
143 void rt6_uncached_list_del(struct rt6_info *rt)
144 {
145 	if (!list_empty(&rt->rt6i_uncached)) {
146 		struct uncached_list *ul = rt->rt6i_uncached_list;
147 		struct net *net = dev_net(rt->dst.dev);
148 
149 		spin_lock_bh(&ul->lock);
150 		list_del(&rt->rt6i_uncached);
151 		atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
152 		spin_unlock_bh(&ul->lock);
153 	}
154 }
155 
156 static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
157 {
158 	struct net_device *loopback_dev = net->loopback_dev;
159 	int cpu;
160 
161 	if (dev == loopback_dev)
162 		return;
163 
164 	for_each_possible_cpu(cpu) {
165 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
166 		struct rt6_info *rt;
167 
168 		spin_lock_bh(&ul->lock);
169 		list_for_each_entry(rt, &ul->head, rt6i_uncached) {
170 			struct inet6_dev *rt_idev = rt->rt6i_idev;
171 			struct net_device *rt_dev = rt->dst.dev;
172 
173 			if (rt_idev->dev == dev) {
174 				rt->rt6i_idev = in6_dev_get(loopback_dev);
175 				in6_dev_put(rt_idev);
176 			}
177 
178 			if (rt_dev == dev) {
179 				rt->dst.dev = loopback_dev;
180 				dev_hold(rt->dst.dev);
181 				dev_put(rt_dev);
182 			}
183 		}
184 		spin_unlock_bh(&ul->lock);
185 	}
186 }
187 
188 static inline const void *choose_neigh_daddr(const struct in6_addr *p,
189 					     struct sk_buff *skb,
190 					     const void *daddr)
191 {
192 	if (!ipv6_addr_any(p))
193 		return (const void *) p;
194 	else if (skb)
195 		return &ipv6_hdr(skb)->daddr;
196 	return daddr;
197 }
198 
199 struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
200 				   struct net_device *dev,
201 				   struct sk_buff *skb,
202 				   const void *daddr)
203 {
204 	struct neighbour *n;
205 
206 	daddr = choose_neigh_daddr(gw, skb, daddr);
207 	n = __ipv6_neigh_lookup(dev, daddr);
208 	if (n)
209 		return n;
210 
211 	n = neigh_create(&nd_tbl, daddr, dev);
212 	return IS_ERR(n) ? NULL : n;
213 }
214 
215 static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
216 					      struct sk_buff *skb,
217 					      const void *daddr)
218 {
219 	const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
220 
221 	return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr);
222 }
223 
224 static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
225 {
226 	struct net_device *dev = dst->dev;
227 	struct rt6_info *rt = (struct rt6_info *)dst;
228 
229 	daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
230 	if (!daddr)
231 		return;
232 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
233 		return;
234 	if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
235 		return;
236 	__ipv6_confirm_neigh(dev, daddr);
237 }
238 
239 static struct dst_ops ip6_dst_ops_template = {
240 	.family			=	AF_INET6,
241 	.gc			=	ip6_dst_gc,
242 	.gc_thresh		=	1024,
243 	.check			=	ip6_dst_check,
244 	.default_advmss		=	ip6_default_advmss,
245 	.mtu			=	ip6_mtu,
246 	.cow_metrics		=	dst_cow_metrics_generic,
247 	.destroy		=	ip6_dst_destroy,
248 	.ifdown			=	ip6_dst_ifdown,
249 	.negative_advice	=	ip6_negative_advice,
250 	.link_failure		=	ip6_link_failure,
251 	.update_pmtu		=	ip6_rt_update_pmtu,
252 	.redirect		=	rt6_do_redirect,
253 	.local_out		=	__ip6_local_out,
254 	.neigh_lookup		=	ip6_dst_neigh_lookup,
255 	.confirm_neigh		=	ip6_confirm_neigh,
256 };
257 
258 static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
259 {
260 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
261 
262 	return mtu ? : dst->dev->mtu;
263 }
264 
265 static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
266 					 struct sk_buff *skb, u32 mtu)
267 {
268 }
269 
270 static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
271 				      struct sk_buff *skb)
272 {
273 }
274 
275 static struct dst_ops ip6_dst_blackhole_ops = {
276 	.family			=	AF_INET6,
277 	.destroy		=	ip6_dst_destroy,
278 	.check			=	ip6_dst_check,
279 	.mtu			=	ip6_blackhole_mtu,
280 	.default_advmss		=	ip6_default_advmss,
281 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
282 	.redirect		=	ip6_rt_blackhole_redirect,
283 	.cow_metrics		=	dst_cow_metrics_generic,
284 	.neigh_lookup		=	ip6_dst_neigh_lookup,
285 };
286 
287 static const u32 ip6_template_metrics[RTAX_MAX] = {
288 	[RTAX_HOPLIMIT - 1] = 0,
289 };
290 
291 static const struct fib6_info fib6_null_entry_template = {
292 	.fib6_flags	= (RTF_REJECT | RTF_NONEXTHOP),
293 	.fib6_protocol  = RTPROT_KERNEL,
294 	.fib6_metric	= ~(u32)0,
295 	.fib6_ref	= REFCOUNT_INIT(1),
296 	.fib6_type	= RTN_UNREACHABLE,
297 	.fib6_metrics	= (struct dst_metrics *)&dst_default_metrics,
298 };
299 
300 static const struct rt6_info ip6_null_entry_template = {
301 	.dst = {
302 		.__refcnt	= ATOMIC_INIT(1),
303 		.__use		= 1,
304 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
305 		.error		= -ENETUNREACH,
306 		.input		= ip6_pkt_discard,
307 		.output		= ip6_pkt_discard_out,
308 	},
309 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
310 };
311 
312 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
313 
314 static const struct rt6_info ip6_prohibit_entry_template = {
315 	.dst = {
316 		.__refcnt	= ATOMIC_INIT(1),
317 		.__use		= 1,
318 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
319 		.error		= -EACCES,
320 		.input		= ip6_pkt_prohibit,
321 		.output		= ip6_pkt_prohibit_out,
322 	},
323 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
324 };
325 
326 static const struct rt6_info ip6_blk_hole_entry_template = {
327 	.dst = {
328 		.__refcnt	= ATOMIC_INIT(1),
329 		.__use		= 1,
330 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
331 		.error		= -EINVAL,
332 		.input		= dst_discard,
333 		.output		= dst_discard_out,
334 	},
335 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
336 };
337 
338 #endif
339 
340 static void rt6_info_init(struct rt6_info *rt)
341 {
342 	struct dst_entry *dst = &rt->dst;
343 
344 	memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
345 	INIT_LIST_HEAD(&rt->rt6i_uncached);
346 }
347 
348 /* allocate dst with ip6_dst_ops */
349 struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
350 			       int flags)
351 {
352 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
353 					1, DST_OBSOLETE_FORCE_CHK, flags);
354 
355 	if (rt) {
356 		rt6_info_init(rt);
357 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
358 	}
359 
360 	return rt;
361 }
362 EXPORT_SYMBOL(ip6_dst_alloc);
363 
364 static void ip6_dst_destroy(struct dst_entry *dst)
365 {
366 	struct rt6_info *rt = (struct rt6_info *)dst;
367 	struct fib6_info *from;
368 	struct inet6_dev *idev;
369 
370 	ip_dst_metrics_put(dst);
371 	rt6_uncached_list_del(rt);
372 
373 	idev = rt->rt6i_idev;
374 	if (idev) {
375 		rt->rt6i_idev = NULL;
376 		in6_dev_put(idev);
377 	}
378 
379 	from = xchg((__force struct fib6_info **)&rt->from, NULL);
380 	fib6_info_release(from);
381 }
382 
383 static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
384 			   int how)
385 {
386 	struct rt6_info *rt = (struct rt6_info *)dst;
387 	struct inet6_dev *idev = rt->rt6i_idev;
388 	struct net_device *loopback_dev =
389 		dev_net(dev)->loopback_dev;
390 
391 	if (idev && idev->dev != loopback_dev) {
392 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
393 		if (loopback_idev) {
394 			rt->rt6i_idev = loopback_idev;
395 			in6_dev_put(idev);
396 		}
397 	}
398 }
399 
400 static bool __rt6_check_expired(const struct rt6_info *rt)
401 {
402 	if (rt->rt6i_flags & RTF_EXPIRES)
403 		return time_after(jiffies, rt->dst.expires);
404 	else
405 		return false;
406 }
407 
408 static bool rt6_check_expired(const struct rt6_info *rt)
409 {
410 	struct fib6_info *from;
411 
412 	from = rcu_dereference(rt->from);
413 
414 	if (rt->rt6i_flags & RTF_EXPIRES) {
415 		if (time_after(jiffies, rt->dst.expires))
416 			return true;
417 	} else if (from) {
418 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
419 			fib6_check_expired(from);
420 	}
421 	return false;
422 }
423 
424 void fib6_select_path(const struct net *net, struct fib6_result *res,
425 		      struct flowi6 *fl6, int oif, bool have_oif_match,
426 		      const struct sk_buff *skb, int strict)
427 {
428 	struct fib6_info *sibling, *next_sibling;
429 	struct fib6_info *match = res->f6i;
430 
431 	if ((!match->fib6_nsiblings && !match->nh) || have_oif_match)
432 		goto out;
433 
434 	/* We might have already computed the hash for ICMPv6 errors. In such
435 	 * case it will always be non-zero. Otherwise now is the time to do it.
436 	 */
437 	if (!fl6->mp_hash &&
438 	    (!match->nh || nexthop_is_multipath(match->nh)))
439 		fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
440 
441 	if (unlikely(match->nh)) {
442 		nexthop_path_fib6_result(res, fl6->mp_hash);
443 		return;
444 	}
445 
446 	if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
447 		goto out;
448 
449 	list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
450 				 fib6_siblings) {
451 		const struct fib6_nh *nh = sibling->fib6_nh;
452 		int nh_upper_bound;
453 
454 		nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
455 		if (fl6->mp_hash > nh_upper_bound)
456 			continue;
457 		if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
458 			break;
459 		match = sibling;
460 		break;
461 	}
462 
463 out:
464 	res->f6i = match;
465 	res->nh = match->fib6_nh;
466 }
467 
468 /*
469  *	Route lookup. rcu_read_lock() should be held.
470  */
471 
472 static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
473 			       const struct in6_addr *saddr, int oif, int flags)
474 {
475 	const struct net_device *dev;
476 
477 	if (nh->fib_nh_flags & RTNH_F_DEAD)
478 		return false;
479 
480 	dev = nh->fib_nh_dev;
481 	if (oif) {
482 		if (dev->ifindex == oif)
483 			return true;
484 	} else {
485 		if (ipv6_chk_addr(net, saddr, dev,
486 				  flags & RT6_LOOKUP_F_IFACE))
487 			return true;
488 	}
489 
490 	return false;
491 }
492 
493 struct fib6_nh_dm_arg {
494 	struct net		*net;
495 	const struct in6_addr	*saddr;
496 	int			oif;
497 	int			flags;
498 	struct fib6_nh		*nh;
499 };
500 
501 static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
502 {
503 	struct fib6_nh_dm_arg *arg = _arg;
504 
505 	arg->nh = nh;
506 	return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
507 				  arg->flags);
508 }
509 
510 /* returns fib6_nh from nexthop or NULL */
511 static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
512 					struct fib6_result *res,
513 					const struct in6_addr *saddr,
514 					int oif, int flags)
515 {
516 	struct fib6_nh_dm_arg arg = {
517 		.net   = net,
518 		.saddr = saddr,
519 		.oif   = oif,
520 		.flags = flags,
521 	};
522 
523 	if (nexthop_is_blackhole(nh))
524 		return NULL;
525 
526 	if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
527 		return arg.nh;
528 
529 	return NULL;
530 }
531 
532 static void rt6_device_match(struct net *net, struct fib6_result *res,
533 			     const struct in6_addr *saddr, int oif, int flags)
534 {
535 	struct fib6_info *f6i = res->f6i;
536 	struct fib6_info *spf6i;
537 	struct fib6_nh *nh;
538 
539 	if (!oif && ipv6_addr_any(saddr)) {
540 		if (unlikely(f6i->nh)) {
541 			nh = nexthop_fib6_nh(f6i->nh);
542 			if (nexthop_is_blackhole(f6i->nh))
543 				goto out_blackhole;
544 		} else {
545 			nh = f6i->fib6_nh;
546 		}
547 		if (!(nh->fib_nh_flags & RTNH_F_DEAD))
548 			goto out;
549 	}
550 
551 	for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
552 		bool matched = false;
553 
554 		if (unlikely(spf6i->nh)) {
555 			nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
556 					      oif, flags);
557 			if (nh)
558 				matched = true;
559 		} else {
560 			nh = spf6i->fib6_nh;
561 			if (__rt6_device_match(net, nh, saddr, oif, flags))
562 				matched = true;
563 		}
564 		if (matched) {
565 			res->f6i = spf6i;
566 			goto out;
567 		}
568 	}
569 
570 	if (oif && flags & RT6_LOOKUP_F_IFACE) {
571 		res->f6i = net->ipv6.fib6_null_entry;
572 		nh = res->f6i->fib6_nh;
573 		goto out;
574 	}
575 
576 	if (unlikely(f6i->nh)) {
577 		nh = nexthop_fib6_nh(f6i->nh);
578 		if (nexthop_is_blackhole(f6i->nh))
579 			goto out_blackhole;
580 	} else {
581 		nh = f6i->fib6_nh;
582 	}
583 
584 	if (nh->fib_nh_flags & RTNH_F_DEAD) {
585 		res->f6i = net->ipv6.fib6_null_entry;
586 		nh = res->f6i->fib6_nh;
587 	}
588 out:
589 	res->nh = nh;
590 	res->fib6_type = res->f6i->fib6_type;
591 	res->fib6_flags = res->f6i->fib6_flags;
592 	return;
593 
594 out_blackhole:
595 	res->fib6_flags |= RTF_REJECT;
596 	res->fib6_type = RTN_BLACKHOLE;
597 	res->nh = nh;
598 }
599 
600 #ifdef CONFIG_IPV6_ROUTER_PREF
601 struct __rt6_probe_work {
602 	struct work_struct work;
603 	struct in6_addr target;
604 	struct net_device *dev;
605 };
606 
607 static void rt6_probe_deferred(struct work_struct *w)
608 {
609 	struct in6_addr mcaddr;
610 	struct __rt6_probe_work *work =
611 		container_of(w, struct __rt6_probe_work, work);
612 
613 	addrconf_addr_solict_mult(&work->target, &mcaddr);
614 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
615 	dev_put(work->dev);
616 	kfree(work);
617 }
618 
619 static void rt6_probe(struct fib6_nh *fib6_nh)
620 {
621 	struct __rt6_probe_work *work = NULL;
622 	const struct in6_addr *nh_gw;
623 	struct neighbour *neigh;
624 	struct net_device *dev;
625 	struct inet6_dev *idev;
626 
627 	/*
628 	 * Okay, this does not seem to be appropriate
629 	 * for now, however, we need to check if it
630 	 * is really so; aka Router Reachability Probing.
631 	 *
632 	 * Router Reachability Probe MUST be rate-limited
633 	 * to no more than one per minute.
634 	 */
635 	if (fib6_nh->fib_nh_gw_family)
636 		return;
637 
638 	nh_gw = &fib6_nh->fib_nh_gw6;
639 	dev = fib6_nh->fib_nh_dev;
640 	rcu_read_lock_bh();
641 	idev = __in6_dev_get(dev);
642 	neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
643 	if (neigh) {
644 		if (neigh->nud_state & NUD_VALID)
645 			goto out;
646 
647 		write_lock(&neigh->lock);
648 		if (!(neigh->nud_state & NUD_VALID) &&
649 		    time_after(jiffies,
650 			       neigh->updated + idev->cnf.rtr_probe_interval)) {
651 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
652 			if (work)
653 				__neigh_set_probe_once(neigh);
654 		}
655 		write_unlock(&neigh->lock);
656 	} else if (time_after(jiffies, fib6_nh->last_probe +
657 				       idev->cnf.rtr_probe_interval)) {
658 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
659 	}
660 
661 	if (work) {
662 		fib6_nh->last_probe = jiffies;
663 		INIT_WORK(&work->work, rt6_probe_deferred);
664 		work->target = *nh_gw;
665 		dev_hold(dev);
666 		work->dev = dev;
667 		schedule_work(&work->work);
668 	}
669 
670 out:
671 	rcu_read_unlock_bh();
672 }
673 #else
674 static inline void rt6_probe(struct fib6_nh *fib6_nh)
675 {
676 }
677 #endif
678 
679 /*
680  * Default Router Selection (RFC 2461 6.3.6)
681  */
682 static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
683 {
684 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
685 	struct neighbour *neigh;
686 
687 	rcu_read_lock_bh();
688 	neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
689 					  &fib6_nh->fib_nh_gw6);
690 	if (neigh) {
691 		read_lock(&neigh->lock);
692 		if (neigh->nud_state & NUD_VALID)
693 			ret = RT6_NUD_SUCCEED;
694 #ifdef CONFIG_IPV6_ROUTER_PREF
695 		else if (!(neigh->nud_state & NUD_FAILED))
696 			ret = RT6_NUD_SUCCEED;
697 		else
698 			ret = RT6_NUD_FAIL_PROBE;
699 #endif
700 		read_unlock(&neigh->lock);
701 	} else {
702 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
703 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
704 	}
705 	rcu_read_unlock_bh();
706 
707 	return ret;
708 }
709 
710 static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
711 			   int strict)
712 {
713 	int m = 0;
714 
715 	if (!oif || nh->fib_nh_dev->ifindex == oif)
716 		m = 2;
717 
718 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
719 		return RT6_NUD_FAIL_HARD;
720 #ifdef CONFIG_IPV6_ROUTER_PREF
721 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
722 #endif
723 	if ((strict & RT6_LOOKUP_F_REACHABLE) &&
724 	    !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
725 		int n = rt6_check_neigh(nh);
726 		if (n < 0)
727 			return n;
728 	}
729 	return m;
730 }
731 
732 static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
733 		       int oif, int strict, int *mpri, bool *do_rr)
734 {
735 	bool match_do_rr = false;
736 	bool rc = false;
737 	int m;
738 
739 	if (nh->fib_nh_flags & RTNH_F_DEAD)
740 		goto out;
741 
742 	if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
743 	    nh->fib_nh_flags & RTNH_F_LINKDOWN &&
744 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
745 		goto out;
746 
747 	m = rt6_score_route(nh, fib6_flags, oif, strict);
748 	if (m == RT6_NUD_FAIL_DO_RR) {
749 		match_do_rr = true;
750 		m = 0; /* lowest valid score */
751 	} else if (m == RT6_NUD_FAIL_HARD) {
752 		goto out;
753 	}
754 
755 	if (strict & RT6_LOOKUP_F_REACHABLE)
756 		rt6_probe(nh);
757 
758 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
759 	if (m > *mpri) {
760 		*do_rr = match_do_rr;
761 		*mpri = m;
762 		rc = true;
763 	}
764 out:
765 	return rc;
766 }
767 
768 struct fib6_nh_frl_arg {
769 	u32		flags;
770 	int		oif;
771 	int		strict;
772 	int		*mpri;
773 	bool		*do_rr;
774 	struct fib6_nh	*nh;
775 };
776 
777 static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
778 {
779 	struct fib6_nh_frl_arg *arg = _arg;
780 
781 	arg->nh = nh;
782 	return find_match(nh, arg->flags, arg->oif, arg->strict,
783 			  arg->mpri, arg->do_rr);
784 }
785 
786 static void __find_rr_leaf(struct fib6_info *f6i_start,
787 			   struct fib6_info *nomatch, u32 metric,
788 			   struct fib6_result *res, struct fib6_info **cont,
789 			   int oif, int strict, bool *do_rr, int *mpri)
790 {
791 	struct fib6_info *f6i;
792 
793 	for (f6i = f6i_start;
794 	     f6i && f6i != nomatch;
795 	     f6i = rcu_dereference(f6i->fib6_next)) {
796 		bool matched = false;
797 		struct fib6_nh *nh;
798 
799 		if (cont && f6i->fib6_metric != metric) {
800 			*cont = f6i;
801 			return;
802 		}
803 
804 		if (fib6_check_expired(f6i))
805 			continue;
806 
807 		if (unlikely(f6i->nh)) {
808 			struct fib6_nh_frl_arg arg = {
809 				.flags  = f6i->fib6_flags,
810 				.oif    = oif,
811 				.strict = strict,
812 				.mpri   = mpri,
813 				.do_rr  = do_rr
814 			};
815 
816 			if (nexthop_is_blackhole(f6i->nh)) {
817 				res->fib6_flags = RTF_REJECT;
818 				res->fib6_type = RTN_BLACKHOLE;
819 				res->f6i = f6i;
820 				res->nh = nexthop_fib6_nh(f6i->nh);
821 				return;
822 			}
823 			if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
824 						     &arg)) {
825 				matched = true;
826 				nh = arg.nh;
827 			}
828 		} else {
829 			nh = f6i->fib6_nh;
830 			if (find_match(nh, f6i->fib6_flags, oif, strict,
831 				       mpri, do_rr))
832 				matched = true;
833 		}
834 		if (matched) {
835 			res->f6i = f6i;
836 			res->nh = nh;
837 			res->fib6_flags = f6i->fib6_flags;
838 			res->fib6_type = f6i->fib6_type;
839 		}
840 	}
841 }
842 
843 static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
844 			 struct fib6_info *rr_head, int oif, int strict,
845 			 bool *do_rr, struct fib6_result *res)
846 {
847 	u32 metric = rr_head->fib6_metric;
848 	struct fib6_info *cont = NULL;
849 	int mpri = -1;
850 
851 	__find_rr_leaf(rr_head, NULL, metric, res, &cont,
852 		       oif, strict, do_rr, &mpri);
853 
854 	__find_rr_leaf(leaf, rr_head, metric, res, &cont,
855 		       oif, strict, do_rr, &mpri);
856 
857 	if (res->f6i || !cont)
858 		return;
859 
860 	__find_rr_leaf(cont, NULL, metric, res, NULL,
861 		       oif, strict, do_rr, &mpri);
862 }
863 
864 static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
865 		       struct fib6_result *res, int strict)
866 {
867 	struct fib6_info *leaf = rcu_dereference(fn->leaf);
868 	struct fib6_info *rt0;
869 	bool do_rr = false;
870 	int key_plen;
871 
872 	/* make sure this function or its helpers sets f6i */
873 	res->f6i = NULL;
874 
875 	if (!leaf || leaf == net->ipv6.fib6_null_entry)
876 		goto out;
877 
878 	rt0 = rcu_dereference(fn->rr_ptr);
879 	if (!rt0)
880 		rt0 = leaf;
881 
882 	/* Double check to make sure fn is not an intermediate node
883 	 * and fn->leaf does not points to its child's leaf
884 	 * (This might happen if all routes under fn are deleted from
885 	 * the tree and fib6_repair_tree() is called on the node.)
886 	 */
887 	key_plen = rt0->fib6_dst.plen;
888 #ifdef CONFIG_IPV6_SUBTREES
889 	if (rt0->fib6_src.plen)
890 		key_plen = rt0->fib6_src.plen;
891 #endif
892 	if (fn->fn_bit != key_plen)
893 		goto out;
894 
895 	find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
896 	if (do_rr) {
897 		struct fib6_info *next = rcu_dereference(rt0->fib6_next);
898 
899 		/* no entries matched; do round-robin */
900 		if (!next || next->fib6_metric != rt0->fib6_metric)
901 			next = leaf;
902 
903 		if (next != rt0) {
904 			spin_lock_bh(&leaf->fib6_table->tb6_lock);
905 			/* make sure next is not being deleted from the tree */
906 			if (next->fib6_node)
907 				rcu_assign_pointer(fn->rr_ptr, next);
908 			spin_unlock_bh(&leaf->fib6_table->tb6_lock);
909 		}
910 	}
911 
912 out:
913 	if (!res->f6i) {
914 		res->f6i = net->ipv6.fib6_null_entry;
915 		res->nh = res->f6i->fib6_nh;
916 		res->fib6_flags = res->f6i->fib6_flags;
917 		res->fib6_type = res->f6i->fib6_type;
918 	}
919 }
920 
921 static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
922 {
923 	return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
924 	       res->nh->fib_nh_gw_family;
925 }
926 
927 #ifdef CONFIG_IPV6_ROUTE_INFO
928 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
929 		  const struct in6_addr *gwaddr)
930 {
931 	struct net *net = dev_net(dev);
932 	struct route_info *rinfo = (struct route_info *) opt;
933 	struct in6_addr prefix_buf, *prefix;
934 	unsigned int pref;
935 	unsigned long lifetime;
936 	struct fib6_info *rt;
937 
938 	if (len < sizeof(struct route_info)) {
939 		return -EINVAL;
940 	}
941 
942 	/* Sanity check for prefix_len and length */
943 	if (rinfo->length > 3) {
944 		return -EINVAL;
945 	} else if (rinfo->prefix_len > 128) {
946 		return -EINVAL;
947 	} else if (rinfo->prefix_len > 64) {
948 		if (rinfo->length < 2) {
949 			return -EINVAL;
950 		}
951 	} else if (rinfo->prefix_len > 0) {
952 		if (rinfo->length < 1) {
953 			return -EINVAL;
954 		}
955 	}
956 
957 	pref = rinfo->route_pref;
958 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
959 		return -EINVAL;
960 
961 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
962 
963 	if (rinfo->length == 3)
964 		prefix = (struct in6_addr *)rinfo->prefix;
965 	else {
966 		/* this function is safe */
967 		ipv6_addr_prefix(&prefix_buf,
968 				 (struct in6_addr *)rinfo->prefix,
969 				 rinfo->prefix_len);
970 		prefix = &prefix_buf;
971 	}
972 
973 	if (rinfo->prefix_len == 0)
974 		rt = rt6_get_dflt_router(net, gwaddr, dev);
975 	else
976 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
977 					gwaddr, dev);
978 
979 	if (rt && !lifetime) {
980 		ip6_del_rt(net, rt);
981 		rt = NULL;
982 	}
983 
984 	if (!rt && lifetime)
985 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
986 					dev, pref);
987 	else if (rt)
988 		rt->fib6_flags = RTF_ROUTEINFO |
989 				 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
990 
991 	if (rt) {
992 		if (!addrconf_finite_timeout(lifetime))
993 			fib6_clean_expires(rt);
994 		else
995 			fib6_set_expires(rt, jiffies + HZ * lifetime);
996 
997 		fib6_info_release(rt);
998 	}
999 	return 0;
1000 }
1001 #endif
1002 
1003 /*
1004  *	Misc support functions
1005  */
1006 
1007 /* called with rcu_lock held */
1008 static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
1009 {
1010 	struct net_device *dev = res->nh->fib_nh_dev;
1011 
1012 	if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
1013 		/* for copies of local routes, dst->dev needs to be the
1014 		 * device if it is a master device, the master device if
1015 		 * device is enslaved, and the loopback as the default
1016 		 */
1017 		if (netif_is_l3_slave(dev) &&
1018 		    !rt6_need_strict(&res->f6i->fib6_dst.addr))
1019 			dev = l3mdev_master_dev_rcu(dev);
1020 		else if (!netif_is_l3_master(dev))
1021 			dev = dev_net(dev)->loopback_dev;
1022 		/* last case is netif_is_l3_master(dev) is true in which
1023 		 * case we want dev returned to be dev
1024 		 */
1025 	}
1026 
1027 	return dev;
1028 }
1029 
1030 static const int fib6_prop[RTN_MAX + 1] = {
1031 	[RTN_UNSPEC]	= 0,
1032 	[RTN_UNICAST]	= 0,
1033 	[RTN_LOCAL]	= 0,
1034 	[RTN_BROADCAST]	= 0,
1035 	[RTN_ANYCAST]	= 0,
1036 	[RTN_MULTICAST]	= 0,
1037 	[RTN_BLACKHOLE]	= -EINVAL,
1038 	[RTN_UNREACHABLE] = -EHOSTUNREACH,
1039 	[RTN_PROHIBIT]	= -EACCES,
1040 	[RTN_THROW]	= -EAGAIN,
1041 	[RTN_NAT]	= -EINVAL,
1042 	[RTN_XRESOLVE]	= -EINVAL,
1043 };
1044 
1045 static int ip6_rt_type_to_error(u8 fib6_type)
1046 {
1047 	return fib6_prop[fib6_type];
1048 }
1049 
1050 static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
1051 {
1052 	unsigned short flags = 0;
1053 
1054 	if (rt->dst_nocount)
1055 		flags |= DST_NOCOUNT;
1056 	if (rt->dst_nopolicy)
1057 		flags |= DST_NOPOLICY;
1058 	if (rt->dst_host)
1059 		flags |= DST_HOST;
1060 
1061 	return flags;
1062 }
1063 
1064 static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
1065 {
1066 	rt->dst.error = ip6_rt_type_to_error(fib6_type);
1067 
1068 	switch (fib6_type) {
1069 	case RTN_BLACKHOLE:
1070 		rt->dst.output = dst_discard_out;
1071 		rt->dst.input = dst_discard;
1072 		break;
1073 	case RTN_PROHIBIT:
1074 		rt->dst.output = ip6_pkt_prohibit_out;
1075 		rt->dst.input = ip6_pkt_prohibit;
1076 		break;
1077 	case RTN_THROW:
1078 	case RTN_UNREACHABLE:
1079 	default:
1080 		rt->dst.output = ip6_pkt_discard_out;
1081 		rt->dst.input = ip6_pkt_discard;
1082 		break;
1083 	}
1084 }
1085 
1086 static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
1087 {
1088 	struct fib6_info *f6i = res->f6i;
1089 
1090 	if (res->fib6_flags & RTF_REJECT) {
1091 		ip6_rt_init_dst_reject(rt, res->fib6_type);
1092 		return;
1093 	}
1094 
1095 	rt->dst.error = 0;
1096 	rt->dst.output = ip6_output;
1097 
1098 	if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
1099 		rt->dst.input = ip6_input;
1100 	} else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
1101 		rt->dst.input = ip6_mc_input;
1102 	} else {
1103 		rt->dst.input = ip6_forward;
1104 	}
1105 
1106 	if (res->nh->fib_nh_lws) {
1107 		rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
1108 		lwtunnel_set_redirect(&rt->dst);
1109 	}
1110 
1111 	rt->dst.lastuse = jiffies;
1112 }
1113 
1114 /* Caller must already hold reference to @from */
1115 static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1116 {
1117 	rt->rt6i_flags &= ~RTF_EXPIRES;
1118 	rcu_assign_pointer(rt->from, from);
1119 	ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
1120 }
1121 
1122 /* Caller must already hold reference to f6i in result */
1123 static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
1124 {
1125 	const struct fib6_nh *nh = res->nh;
1126 	const struct net_device *dev = nh->fib_nh_dev;
1127 	struct fib6_info *f6i = res->f6i;
1128 
1129 	ip6_rt_init_dst(rt, res);
1130 
1131 	rt->rt6i_dst = f6i->fib6_dst;
1132 	rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
1133 	rt->rt6i_flags = res->fib6_flags;
1134 	if (nh->fib_nh_gw_family) {
1135 		rt->rt6i_gateway = nh->fib_nh_gw6;
1136 		rt->rt6i_flags |= RTF_GATEWAY;
1137 	}
1138 	rt6_set_from(rt, f6i);
1139 #ifdef CONFIG_IPV6_SUBTREES
1140 	rt->rt6i_src = f6i->fib6_src;
1141 #endif
1142 }
1143 
1144 static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1145 					struct in6_addr *saddr)
1146 {
1147 	struct fib6_node *pn, *sn;
1148 	while (1) {
1149 		if (fn->fn_flags & RTN_TL_ROOT)
1150 			return NULL;
1151 		pn = rcu_dereference(fn->parent);
1152 		sn = FIB6_SUBTREE(pn);
1153 		if (sn && sn != fn)
1154 			fn = fib6_node_lookup(sn, NULL, saddr);
1155 		else
1156 			fn = pn;
1157 		if (fn->fn_flags & RTN_RTINFO)
1158 			return fn;
1159 	}
1160 }
1161 
1162 static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1163 {
1164 	struct rt6_info *rt = *prt;
1165 
1166 	if (dst_hold_safe(&rt->dst))
1167 		return true;
1168 	if (net) {
1169 		rt = net->ipv6.ip6_null_entry;
1170 		dst_hold(&rt->dst);
1171 	} else {
1172 		rt = NULL;
1173 	}
1174 	*prt = rt;
1175 	return false;
1176 }
1177 
1178 /* called with rcu_lock held */
1179 static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
1180 {
1181 	struct net_device *dev = res->nh->fib_nh_dev;
1182 	struct fib6_info *f6i = res->f6i;
1183 	unsigned short flags;
1184 	struct rt6_info *nrt;
1185 
1186 	if (!fib6_info_hold_safe(f6i))
1187 		goto fallback;
1188 
1189 	flags = fib6_info_dst_flags(f6i);
1190 	nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
1191 	if (!nrt) {
1192 		fib6_info_release(f6i);
1193 		goto fallback;
1194 	}
1195 
1196 	ip6_rt_copy_init(nrt, res);
1197 	return nrt;
1198 
1199 fallback:
1200 	nrt = dev_net(dev)->ipv6.ip6_null_entry;
1201 	dst_hold(&nrt->dst);
1202 	return nrt;
1203 }
1204 
1205 static struct rt6_info *ip6_pol_route_lookup(struct net *net,
1206 					     struct fib6_table *table,
1207 					     struct flowi6 *fl6,
1208 					     const struct sk_buff *skb,
1209 					     int flags)
1210 {
1211 	struct fib6_result res = {};
1212 	struct fib6_node *fn;
1213 	struct rt6_info *rt;
1214 
1215 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1216 		flags &= ~RT6_LOOKUP_F_IFACE;
1217 
1218 	rcu_read_lock();
1219 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1220 restart:
1221 	res.f6i = rcu_dereference(fn->leaf);
1222 	if (!res.f6i)
1223 		res.f6i = net->ipv6.fib6_null_entry;
1224 	else
1225 		rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
1226 				 flags);
1227 
1228 	if (res.f6i == net->ipv6.fib6_null_entry) {
1229 		fn = fib6_backtrack(fn, &fl6->saddr);
1230 		if (fn)
1231 			goto restart;
1232 
1233 		rt = net->ipv6.ip6_null_entry;
1234 		dst_hold(&rt->dst);
1235 		goto out;
1236 	} else if (res.fib6_flags & RTF_REJECT) {
1237 		goto do_create;
1238 	}
1239 
1240 	fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1241 			 fl6->flowi6_oif != 0, skb, flags);
1242 
1243 	/* Search through exception table */
1244 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
1245 	if (rt) {
1246 		if (ip6_hold_safe(net, &rt))
1247 			dst_use_noref(&rt->dst, jiffies);
1248 	} else {
1249 do_create:
1250 		rt = ip6_create_rt_rcu(&res);
1251 	}
1252 
1253 out:
1254 	trace_fib6_table_lookup(net, &res, table, fl6);
1255 
1256 	rcu_read_unlock();
1257 
1258 	return rt;
1259 }
1260 
1261 struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1262 				   const struct sk_buff *skb, int flags)
1263 {
1264 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1265 }
1266 EXPORT_SYMBOL_GPL(ip6_route_lookup);
1267 
1268 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1269 			    const struct in6_addr *saddr, int oif,
1270 			    const struct sk_buff *skb, int strict)
1271 {
1272 	struct flowi6 fl6 = {
1273 		.flowi6_oif = oif,
1274 		.daddr = *daddr,
1275 	};
1276 	struct dst_entry *dst;
1277 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1278 
1279 	if (saddr) {
1280 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1281 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1282 	}
1283 
1284 	dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1285 	if (dst->error == 0)
1286 		return (struct rt6_info *) dst;
1287 
1288 	dst_release(dst);
1289 
1290 	return NULL;
1291 }
1292 EXPORT_SYMBOL(rt6_lookup);
1293 
1294 /* ip6_ins_rt is called with FREE table->tb6_lock.
1295  * It takes new route entry, the addition fails by any reason the
1296  * route is released.
1297  * Caller must hold dst before calling it.
1298  */
1299 
1300 static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1301 			struct netlink_ext_ack *extack)
1302 {
1303 	int err;
1304 	struct fib6_table *table;
1305 
1306 	table = rt->fib6_table;
1307 	spin_lock_bh(&table->tb6_lock);
1308 	err = fib6_add(&table->tb6_root, rt, info, extack);
1309 	spin_unlock_bh(&table->tb6_lock);
1310 
1311 	return err;
1312 }
1313 
1314 int ip6_ins_rt(struct net *net, struct fib6_info *rt)
1315 {
1316 	struct nl_info info = {	.nl_net = net, };
1317 
1318 	return __ip6_ins_rt(rt, &info, NULL);
1319 }
1320 
1321 static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
1322 					   const struct in6_addr *daddr,
1323 					   const struct in6_addr *saddr)
1324 {
1325 	struct fib6_info *f6i = res->f6i;
1326 	struct net_device *dev;
1327 	struct rt6_info *rt;
1328 
1329 	/*
1330 	 *	Clone the route.
1331 	 */
1332 
1333 	if (!fib6_info_hold_safe(f6i))
1334 		return NULL;
1335 
1336 	dev = ip6_rt_get_dev_rcu(res);
1337 	rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1338 	if (!rt) {
1339 		fib6_info_release(f6i);
1340 		return NULL;
1341 	}
1342 
1343 	ip6_rt_copy_init(rt, res);
1344 	rt->rt6i_flags |= RTF_CACHE;
1345 	rt->dst.flags |= DST_HOST;
1346 	rt->rt6i_dst.addr = *daddr;
1347 	rt->rt6i_dst.plen = 128;
1348 
1349 	if (!rt6_is_gw_or_nonexthop(res)) {
1350 		if (f6i->fib6_dst.plen != 128 &&
1351 		    ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
1352 			rt->rt6i_flags |= RTF_ANYCAST;
1353 #ifdef CONFIG_IPV6_SUBTREES
1354 		if (rt->rt6i_src.plen && saddr) {
1355 			rt->rt6i_src.addr = *saddr;
1356 			rt->rt6i_src.plen = 128;
1357 		}
1358 #endif
1359 	}
1360 
1361 	return rt;
1362 }
1363 
1364 static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
1365 {
1366 	struct fib6_info *f6i = res->f6i;
1367 	unsigned short flags = fib6_info_dst_flags(f6i);
1368 	struct net_device *dev;
1369 	struct rt6_info *pcpu_rt;
1370 
1371 	if (!fib6_info_hold_safe(f6i))
1372 		return NULL;
1373 
1374 	rcu_read_lock();
1375 	dev = ip6_rt_get_dev_rcu(res);
1376 	pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
1377 	rcu_read_unlock();
1378 	if (!pcpu_rt) {
1379 		fib6_info_release(f6i);
1380 		return NULL;
1381 	}
1382 	ip6_rt_copy_init(pcpu_rt, res);
1383 	pcpu_rt->rt6i_flags |= RTF_PCPU;
1384 	return pcpu_rt;
1385 }
1386 
1387 /* It should be called with rcu_read_lock() acquired */
1388 static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
1389 {
1390 	struct rt6_info *pcpu_rt;
1391 
1392 	pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
1393 
1394 	return pcpu_rt;
1395 }
1396 
1397 static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1398 					    const struct fib6_result *res)
1399 {
1400 	struct rt6_info *pcpu_rt, *prev, **p;
1401 
1402 	pcpu_rt = ip6_rt_pcpu_alloc(res);
1403 	if (!pcpu_rt)
1404 		return NULL;
1405 
1406 	p = this_cpu_ptr(res->nh->rt6i_pcpu);
1407 	prev = cmpxchg(p, NULL, pcpu_rt);
1408 	BUG_ON(prev);
1409 
1410 	if (res->f6i->fib6_destroying) {
1411 		struct fib6_info *from;
1412 
1413 		from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
1414 		fib6_info_release(from);
1415 	}
1416 
1417 	return pcpu_rt;
1418 }
1419 
1420 /* exception hash table implementation
1421  */
1422 static DEFINE_SPINLOCK(rt6_exception_lock);
1423 
1424 /* Remove rt6_ex from hash table and free the memory
1425  * Caller must hold rt6_exception_lock
1426  */
1427 static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
1428 				 struct rt6_exception *rt6_ex)
1429 {
1430 	struct fib6_info *from;
1431 	struct net *net;
1432 
1433 	if (!bucket || !rt6_ex)
1434 		return;
1435 
1436 	net = dev_net(rt6_ex->rt6i->dst.dev);
1437 	net->ipv6.rt6_stats->fib_rt_cache--;
1438 
1439 	/* purge completely the exception to allow releasing the held resources:
1440 	 * some [sk] cache may keep the dst around for unlimited time
1441 	 */
1442 	from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1443 	fib6_info_release(from);
1444 	dst_dev_put(&rt6_ex->rt6i->dst);
1445 
1446 	hlist_del_rcu(&rt6_ex->hlist);
1447 	dst_release(&rt6_ex->rt6i->dst);
1448 	kfree_rcu(rt6_ex, rcu);
1449 	WARN_ON_ONCE(!bucket->depth);
1450 	bucket->depth--;
1451 }
1452 
1453 /* Remove oldest rt6_ex in bucket and free the memory
1454  * Caller must hold rt6_exception_lock
1455  */
1456 static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
1457 {
1458 	struct rt6_exception *rt6_ex, *oldest = NULL;
1459 
1460 	if (!bucket)
1461 		return;
1462 
1463 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1464 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
1465 			oldest = rt6_ex;
1466 	}
1467 	rt6_remove_exception(bucket, oldest);
1468 }
1469 
1470 static u32 rt6_exception_hash(const struct in6_addr *dst,
1471 			      const struct in6_addr *src)
1472 {
1473 	static u32 seed __read_mostly;
1474 	u32 val;
1475 
1476 	net_get_random_once(&seed, sizeof(seed));
1477 	val = jhash(dst, sizeof(*dst), seed);
1478 
1479 #ifdef CONFIG_IPV6_SUBTREES
1480 	if (src)
1481 		val = jhash(src, sizeof(*src), val);
1482 #endif
1483 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
1484 }
1485 
1486 /* Helper function to find the cached rt in the hash table
1487  * and update bucket pointer to point to the bucket for this
1488  * (daddr, saddr) pair
1489  * Caller must hold rt6_exception_lock
1490  */
1491 static struct rt6_exception *
1492 __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
1493 			      const struct in6_addr *daddr,
1494 			      const struct in6_addr *saddr)
1495 {
1496 	struct rt6_exception *rt6_ex;
1497 	u32 hval;
1498 
1499 	if (!(*bucket) || !daddr)
1500 		return NULL;
1501 
1502 	hval = rt6_exception_hash(daddr, saddr);
1503 	*bucket += hval;
1504 
1505 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
1506 		struct rt6_info *rt6 = rt6_ex->rt6i;
1507 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1508 
1509 #ifdef CONFIG_IPV6_SUBTREES
1510 		if (matched && saddr)
1511 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1512 #endif
1513 		if (matched)
1514 			return rt6_ex;
1515 	}
1516 	return NULL;
1517 }
1518 
1519 /* Helper function to find the cached rt in the hash table
1520  * and update bucket pointer to point to the bucket for this
1521  * (daddr, saddr) pair
1522  * Caller must hold rcu_read_lock()
1523  */
1524 static struct rt6_exception *
1525 __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
1526 			 const struct in6_addr *daddr,
1527 			 const struct in6_addr *saddr)
1528 {
1529 	struct rt6_exception *rt6_ex;
1530 	u32 hval;
1531 
1532 	WARN_ON_ONCE(!rcu_read_lock_held());
1533 
1534 	if (!(*bucket) || !daddr)
1535 		return NULL;
1536 
1537 	hval = rt6_exception_hash(daddr, saddr);
1538 	*bucket += hval;
1539 
1540 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
1541 		struct rt6_info *rt6 = rt6_ex->rt6i;
1542 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
1543 
1544 #ifdef CONFIG_IPV6_SUBTREES
1545 		if (matched && saddr)
1546 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
1547 #endif
1548 		if (matched)
1549 			return rt6_ex;
1550 	}
1551 	return NULL;
1552 }
1553 
1554 static unsigned int fib6_mtu(const struct fib6_result *res)
1555 {
1556 	const struct fib6_nh *nh = res->nh;
1557 	unsigned int mtu;
1558 
1559 	if (res->f6i->fib6_pmtu) {
1560 		mtu = res->f6i->fib6_pmtu;
1561 	} else {
1562 		struct net_device *dev = nh->fib_nh_dev;
1563 		struct inet6_dev *idev;
1564 
1565 		rcu_read_lock();
1566 		idev = __in6_dev_get(dev);
1567 		mtu = idev->cnf.mtu6;
1568 		rcu_read_unlock();
1569 	}
1570 
1571 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1572 
1573 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1574 }
1575 
1576 #define FIB6_EXCEPTION_BUCKET_FLUSHED  0x1UL
1577 
1578 /* used when the flushed bit is not relevant, only access to the bucket
1579  * (ie., all bucket users except rt6_insert_exception);
1580  *
1581  * called under rcu lock; sometimes called with rt6_exception_lock held
1582  */
1583 static
1584 struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1585 						       spinlock_t *lock)
1586 {
1587 	struct rt6_exception_bucket *bucket;
1588 
1589 	if (lock)
1590 		bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1591 						   lockdep_is_held(lock));
1592 	else
1593 		bucket = rcu_dereference(nh->rt6i_exception_bucket);
1594 
1595 	/* remove bucket flushed bit if set */
1596 	if (bucket) {
1597 		unsigned long p = (unsigned long)bucket;
1598 
1599 		p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1600 		bucket = (struct rt6_exception_bucket *)p;
1601 	}
1602 
1603 	return bucket;
1604 }
1605 
1606 static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1607 {
1608 	unsigned long p = (unsigned long)bucket;
1609 
1610 	return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1611 }
1612 
1613 /* called with rt6_exception_lock held */
1614 static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1615 					      spinlock_t *lock)
1616 {
1617 	struct rt6_exception_bucket *bucket;
1618 	unsigned long p;
1619 
1620 	bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1621 					   lockdep_is_held(lock));
1622 
1623 	p = (unsigned long)bucket;
1624 	p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1625 	bucket = (struct rt6_exception_bucket *)p;
1626 	rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1627 }
1628 
1629 static int rt6_insert_exception(struct rt6_info *nrt,
1630 				const struct fib6_result *res)
1631 {
1632 	struct net *net = dev_net(nrt->dst.dev);
1633 	struct rt6_exception_bucket *bucket;
1634 	struct fib6_info *f6i = res->f6i;
1635 	struct in6_addr *src_key = NULL;
1636 	struct rt6_exception *rt6_ex;
1637 	struct fib6_nh *nh = res->nh;
1638 	int err = 0;
1639 
1640 	spin_lock_bh(&rt6_exception_lock);
1641 
1642 	bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1643 					  lockdep_is_held(&rt6_exception_lock));
1644 	if (!bucket) {
1645 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
1646 				 GFP_ATOMIC);
1647 		if (!bucket) {
1648 			err = -ENOMEM;
1649 			goto out;
1650 		}
1651 		rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1652 	} else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1653 		err = -EINVAL;
1654 		goto out;
1655 	}
1656 
1657 #ifdef CONFIG_IPV6_SUBTREES
1658 	/* fib6_src.plen != 0 indicates f6i is in subtree
1659 	 * and exception table is indexed by a hash of
1660 	 * both fib6_dst and fib6_src.
1661 	 * Otherwise, the exception table is indexed by
1662 	 * a hash of only fib6_dst.
1663 	 */
1664 	if (f6i->fib6_src.plen)
1665 		src_key = &nrt->rt6i_src.addr;
1666 #endif
1667 	/* rt6_mtu_change() might lower mtu on f6i.
1668 	 * Only insert this exception route if its mtu
1669 	 * is less than f6i's mtu value.
1670 	 */
1671 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1672 		err = -EINVAL;
1673 		goto out;
1674 	}
1675 
1676 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
1677 					       src_key);
1678 	if (rt6_ex)
1679 		rt6_remove_exception(bucket, rt6_ex);
1680 
1681 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
1682 	if (!rt6_ex) {
1683 		err = -ENOMEM;
1684 		goto out;
1685 	}
1686 	rt6_ex->rt6i = nrt;
1687 	rt6_ex->stamp = jiffies;
1688 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
1689 	bucket->depth++;
1690 	net->ipv6.rt6_stats->fib_rt_cache++;
1691 
1692 	if (bucket->depth > FIB6_MAX_DEPTH)
1693 		rt6_exception_remove_oldest(bucket);
1694 
1695 out:
1696 	spin_unlock_bh(&rt6_exception_lock);
1697 
1698 	/* Update fn->fn_sernum to invalidate all cached dst */
1699 	if (!err) {
1700 		spin_lock_bh(&f6i->fib6_table->tb6_lock);
1701 		fib6_update_sernum(net, f6i);
1702 		spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1703 		fib6_force_start_gc(net);
1704 	}
1705 
1706 	return err;
1707 }
1708 
1709 static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
1710 {
1711 	struct rt6_exception_bucket *bucket;
1712 	struct rt6_exception *rt6_ex;
1713 	struct hlist_node *tmp;
1714 	int i;
1715 
1716 	spin_lock_bh(&rt6_exception_lock);
1717 
1718 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1719 	if (!bucket)
1720 		goto out;
1721 
1722 	/* Prevent rt6_insert_exception() to recreate the bucket list */
1723 	if (!from)
1724 		fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1725 
1726 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1727 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1728 			if (!from ||
1729 			    rcu_access_pointer(rt6_ex->rt6i->from) == from)
1730 				rt6_remove_exception(bucket, rt6_ex);
1731 		}
1732 		WARN_ON_ONCE(!from && bucket->depth);
1733 		bucket++;
1734 	}
1735 out:
1736 	spin_unlock_bh(&rt6_exception_lock);
1737 }
1738 
1739 static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1740 {
1741 	struct fib6_info *f6i = arg;
1742 
1743 	fib6_nh_flush_exceptions(nh, f6i);
1744 
1745 	return 0;
1746 }
1747 
1748 void rt6_flush_exceptions(struct fib6_info *f6i)
1749 {
1750 	if (f6i->nh)
1751 		nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1752 					 f6i);
1753 	else
1754 		fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1755 }
1756 
1757 /* Find cached rt in the hash table inside passed in rt
1758  * Caller has to hold rcu_read_lock()
1759  */
1760 static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1761 					   const struct in6_addr *daddr,
1762 					   const struct in6_addr *saddr)
1763 {
1764 	const struct in6_addr *src_key = NULL;
1765 	struct rt6_exception_bucket *bucket;
1766 	struct rt6_exception *rt6_ex;
1767 	struct rt6_info *ret = NULL;
1768 
1769 #ifdef CONFIG_IPV6_SUBTREES
1770 	/* fib6i_src.plen != 0 indicates f6i is in subtree
1771 	 * and exception table is indexed by a hash of
1772 	 * both fib6_dst and fib6_src.
1773 	 * However, the src addr used to create the hash
1774 	 * might not be exactly the passed in saddr which
1775 	 * is a /128 addr from the flow.
1776 	 * So we need to use f6i->fib6_src to redo lookup
1777 	 * if the passed in saddr does not find anything.
1778 	 * (See the logic in ip6_rt_cache_alloc() on how
1779 	 * rt->rt6i_src is updated.)
1780 	 */
1781 	if (res->f6i->fib6_src.plen)
1782 		src_key = saddr;
1783 find_ex:
1784 #endif
1785 	bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
1786 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
1787 
1788 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
1789 		ret = rt6_ex->rt6i;
1790 
1791 #ifdef CONFIG_IPV6_SUBTREES
1792 	/* Use fib6_src as src_key and redo lookup */
1793 	if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1794 		src_key = &res->f6i->fib6_src.addr;
1795 		goto find_ex;
1796 	}
1797 #endif
1798 
1799 	return ret;
1800 }
1801 
1802 /* Remove the passed in cached rt from the hash table that contains it */
1803 static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1804 				    const struct rt6_info *rt)
1805 {
1806 	const struct in6_addr *src_key = NULL;
1807 	struct rt6_exception_bucket *bucket;
1808 	struct rt6_exception *rt6_ex;
1809 	int err;
1810 
1811 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
1812 		return -ENOENT;
1813 
1814 	spin_lock_bh(&rt6_exception_lock);
1815 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1816 
1817 #ifdef CONFIG_IPV6_SUBTREES
1818 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
1819 	 * and exception table is indexed by a hash of
1820 	 * both rt6i_dst and rt6i_src.
1821 	 * Otherwise, the exception table is indexed by
1822 	 * a hash of only rt6i_dst.
1823 	 */
1824 	if (plen)
1825 		src_key = &rt->rt6i_src.addr;
1826 #endif
1827 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
1828 					       &rt->rt6i_dst.addr,
1829 					       src_key);
1830 	if (rt6_ex) {
1831 		rt6_remove_exception(bucket, rt6_ex);
1832 		err = 0;
1833 	} else {
1834 		err = -ENOENT;
1835 	}
1836 
1837 	spin_unlock_bh(&rt6_exception_lock);
1838 	return err;
1839 }
1840 
1841 struct fib6_nh_excptn_arg {
1842 	struct rt6_info	*rt;
1843 	int		plen;
1844 };
1845 
1846 static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1847 {
1848 	struct fib6_nh_excptn_arg *arg = _arg;
1849 	int err;
1850 
1851 	err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1852 	if (err == 0)
1853 		return 1;
1854 
1855 	return 0;
1856 }
1857 
1858 static int rt6_remove_exception_rt(struct rt6_info *rt)
1859 {
1860 	struct fib6_info *from;
1861 
1862 	from = rcu_dereference(rt->from);
1863 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1864 		return -EINVAL;
1865 
1866 	if (from->nh) {
1867 		struct fib6_nh_excptn_arg arg = {
1868 			.rt = rt,
1869 			.plen = from->fib6_src.plen
1870 		};
1871 		int rc;
1872 
1873 		/* rc = 1 means an entry was found */
1874 		rc = nexthop_for_each_fib6_nh(from->nh,
1875 					      rt6_nh_remove_exception_rt,
1876 					      &arg);
1877 		return rc ? 0 : -ENOENT;
1878 	}
1879 
1880 	return fib6_nh_remove_exception(from->fib6_nh,
1881 					from->fib6_src.plen, rt);
1882 }
1883 
1884 /* Find rt6_ex which contains the passed in rt cache and
1885  * refresh its stamp
1886  */
1887 static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1888 				     const struct rt6_info *rt)
1889 {
1890 	const struct in6_addr *src_key = NULL;
1891 	struct rt6_exception_bucket *bucket;
1892 	struct rt6_exception *rt6_ex;
1893 
1894 	bucket = fib6_nh_get_excptn_bucket(nh, NULL);
1895 #ifdef CONFIG_IPV6_SUBTREES
1896 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
1897 	 * and exception table is indexed by a hash of
1898 	 * both rt6i_dst and rt6i_src.
1899 	 * Otherwise, the exception table is indexed by
1900 	 * a hash of only rt6i_dst.
1901 	 */
1902 	if (plen)
1903 		src_key = &rt->rt6i_src.addr;
1904 #endif
1905 	rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
1906 	if (rt6_ex)
1907 		rt6_ex->stamp = jiffies;
1908 }
1909 
1910 struct fib6_nh_match_arg {
1911 	const struct net_device *dev;
1912 	const struct in6_addr	*gw;
1913 	struct fib6_nh		*match;
1914 };
1915 
1916 /* determine if fib6_nh has given device and gateway */
1917 static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1918 {
1919 	struct fib6_nh_match_arg *arg = _arg;
1920 
1921 	if (arg->dev != nh->fib_nh_dev ||
1922 	    (arg->gw && !nh->fib_nh_gw_family) ||
1923 	    (!arg->gw && nh->fib_nh_gw_family) ||
1924 	    (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1925 		return 0;
1926 
1927 	arg->match = nh;
1928 
1929 	/* found a match, break the loop */
1930 	return 1;
1931 }
1932 
1933 static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1934 {
1935 	struct fib6_info *from;
1936 	struct fib6_nh *fib6_nh;
1937 
1938 	rcu_read_lock();
1939 
1940 	from = rcu_dereference(rt->from);
1941 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1942 		goto unlock;
1943 
1944 	if (from->nh) {
1945 		struct fib6_nh_match_arg arg = {
1946 			.dev = rt->dst.dev,
1947 			.gw = &rt->rt6i_gateway,
1948 		};
1949 
1950 		nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1951 
1952 		if (!arg.match)
1953 			return;
1954 		fib6_nh = arg.match;
1955 	} else {
1956 		fib6_nh = from->fib6_nh;
1957 	}
1958 	fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
1959 unlock:
1960 	rcu_read_unlock();
1961 }
1962 
1963 static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1964 					 struct rt6_info *rt, int mtu)
1965 {
1966 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1967 	 * lowest MTU in the path: always allow updating the route PMTU to
1968 	 * reflect PMTU decreases.
1969 	 *
1970 	 * If the new MTU is higher, and the route PMTU is equal to the local
1971 	 * MTU, this means the old MTU is the lowest in the path, so allow
1972 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1973 	 * handle this.
1974 	 */
1975 
1976 	if (dst_mtu(&rt->dst) >= mtu)
1977 		return true;
1978 
1979 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1980 		return true;
1981 
1982 	return false;
1983 }
1984 
1985 static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
1986 				       const struct fib6_nh *nh, int mtu)
1987 {
1988 	struct rt6_exception_bucket *bucket;
1989 	struct rt6_exception *rt6_ex;
1990 	int i;
1991 
1992 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1993 	if (!bucket)
1994 		return;
1995 
1996 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1997 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1998 			struct rt6_info *entry = rt6_ex->rt6i;
1999 
2000 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2001 			 * route), the metrics of its rt->from have already
2002 			 * been updated.
2003 			 */
2004 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
2005 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
2006 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
2007 		}
2008 		bucket++;
2009 	}
2010 }
2011 
2012 #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
2013 
2014 static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2015 					    const struct in6_addr *gateway)
2016 {
2017 	struct rt6_exception_bucket *bucket;
2018 	struct rt6_exception *rt6_ex;
2019 	struct hlist_node *tmp;
2020 	int i;
2021 
2022 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2023 		return;
2024 
2025 	spin_lock_bh(&rt6_exception_lock);
2026 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2027 	if (bucket) {
2028 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2029 			hlist_for_each_entry_safe(rt6_ex, tmp,
2030 						  &bucket->chain, hlist) {
2031 				struct rt6_info *entry = rt6_ex->rt6i;
2032 
2033 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2034 				    RTF_CACHE_GATEWAY &&
2035 				    ipv6_addr_equal(gateway,
2036 						    &entry->rt6i_gateway)) {
2037 					rt6_remove_exception(bucket, rt6_ex);
2038 				}
2039 			}
2040 			bucket++;
2041 		}
2042 	}
2043 
2044 	spin_unlock_bh(&rt6_exception_lock);
2045 }
2046 
2047 static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2048 				      struct rt6_exception *rt6_ex,
2049 				      struct fib6_gc_args *gc_args,
2050 				      unsigned long now)
2051 {
2052 	struct rt6_info *rt = rt6_ex->rt6i;
2053 
2054 	/* we are pruning and obsoleting aged-out and non gateway exceptions
2055 	 * even if others have still references to them, so that on next
2056 	 * dst_check() such references can be dropped.
2057 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
2058 	 * expired, independently from their aging, as per RFC 8201 section 4
2059 	 */
2060 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
2061 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2062 			RT6_TRACE("aging clone %p\n", rt);
2063 			rt6_remove_exception(bucket, rt6_ex);
2064 			return;
2065 		}
2066 	} else if (time_after(jiffies, rt->dst.expires)) {
2067 		RT6_TRACE("purging expired route %p\n", rt);
2068 		rt6_remove_exception(bucket, rt6_ex);
2069 		return;
2070 	}
2071 
2072 	if (rt->rt6i_flags & RTF_GATEWAY) {
2073 		struct neighbour *neigh;
2074 		__u8 neigh_flags = 0;
2075 
2076 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
2077 		if (neigh)
2078 			neigh_flags = neigh->flags;
2079 
2080 		if (!(neigh_flags & NTF_ROUTER)) {
2081 			RT6_TRACE("purging route %p via non-router but gateway\n",
2082 				  rt);
2083 			rt6_remove_exception(bucket, rt6_ex);
2084 			return;
2085 		}
2086 	}
2087 
2088 	gc_args->more++;
2089 }
2090 
2091 static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
2092 				   struct fib6_gc_args *gc_args,
2093 				   unsigned long now)
2094 {
2095 	struct rt6_exception_bucket *bucket;
2096 	struct rt6_exception *rt6_ex;
2097 	struct hlist_node *tmp;
2098 	int i;
2099 
2100 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2101 		return;
2102 
2103 	rcu_read_lock_bh();
2104 	spin_lock(&rt6_exception_lock);
2105 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2106 	if (bucket) {
2107 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2108 			hlist_for_each_entry_safe(rt6_ex, tmp,
2109 						  &bucket->chain, hlist) {
2110 				rt6_age_examine_exception(bucket, rt6_ex,
2111 							  gc_args, now);
2112 			}
2113 			bucket++;
2114 		}
2115 	}
2116 	spin_unlock(&rt6_exception_lock);
2117 	rcu_read_unlock_bh();
2118 }
2119 
2120 struct fib6_nh_age_excptn_arg {
2121 	struct fib6_gc_args	*gc_args;
2122 	unsigned long		now;
2123 };
2124 
2125 static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2126 {
2127 	struct fib6_nh_age_excptn_arg *arg = _arg;
2128 
2129 	fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2130 	return 0;
2131 }
2132 
2133 void rt6_age_exceptions(struct fib6_info *f6i,
2134 			struct fib6_gc_args *gc_args,
2135 			unsigned long now)
2136 {
2137 	if (f6i->nh) {
2138 		struct fib6_nh_age_excptn_arg arg = {
2139 			.gc_args = gc_args,
2140 			.now = now
2141 		};
2142 
2143 		nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2144 					 &arg);
2145 	} else {
2146 		fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2147 	}
2148 }
2149 
2150 /* must be called with rcu lock held */
2151 int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2152 		      struct flowi6 *fl6, struct fib6_result *res, int strict)
2153 {
2154 	struct fib6_node *fn, *saved_fn;
2155 
2156 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2157 	saved_fn = fn;
2158 
2159 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2160 		oif = 0;
2161 
2162 redo_rt6_select:
2163 	rt6_select(net, fn, oif, res, strict);
2164 	if (res->f6i == net->ipv6.fib6_null_entry) {
2165 		fn = fib6_backtrack(fn, &fl6->saddr);
2166 		if (fn)
2167 			goto redo_rt6_select;
2168 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
2169 			/* also consider unreachable route */
2170 			strict &= ~RT6_LOOKUP_F_REACHABLE;
2171 			fn = saved_fn;
2172 			goto redo_rt6_select;
2173 		}
2174 	}
2175 
2176 	trace_fib6_table_lookup(net, res, table, fl6);
2177 
2178 	return 0;
2179 }
2180 
2181 struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
2182 			       int oif, struct flowi6 *fl6,
2183 			       const struct sk_buff *skb, int flags)
2184 {
2185 	struct fib6_result res = {};
2186 	struct rt6_info *rt = NULL;
2187 	int strict = 0;
2188 
2189 	WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
2190 		     !rcu_read_lock_held());
2191 
2192 	strict |= flags & RT6_LOOKUP_F_IFACE;
2193 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
2194 	if (net->ipv6.devconf_all->forwarding == 0)
2195 		strict |= RT6_LOOKUP_F_REACHABLE;
2196 
2197 	rcu_read_lock();
2198 
2199 	fib6_table_lookup(net, table, oif, fl6, &res, strict);
2200 	if (res.f6i == net->ipv6.fib6_null_entry)
2201 		goto out;
2202 
2203 	fib6_select_path(net, &res, fl6, oif, false, skb, strict);
2204 
2205 	/*Search through exception table */
2206 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
2207 	if (rt) {
2208 		goto out;
2209 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
2210 			    !res.nh->fib_nh_gw_family)) {
2211 		/* Create a RTF_CACHE clone which will not be
2212 		 * owned by the fib6 tree.  It is for the special case where
2213 		 * the daddr in the skb during the neighbor look-up is different
2214 		 * from the fl6->daddr used to look-up route here.
2215 		 */
2216 		rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
2217 
2218 		if (rt) {
2219 			/* 1 refcnt is taken during ip6_rt_cache_alloc().
2220 			 * As rt6_uncached_list_add() does not consume refcnt,
2221 			 * this refcnt is always returned to the caller even
2222 			 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
2223 			 */
2224 			rt6_uncached_list_add(rt);
2225 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
2226 			rcu_read_unlock();
2227 
2228 			return rt;
2229 		}
2230 	} else {
2231 		/* Get a percpu copy */
2232 		local_bh_disable();
2233 		rt = rt6_get_pcpu_route(&res);
2234 
2235 		if (!rt)
2236 			rt = rt6_make_pcpu_route(net, &res);
2237 
2238 		local_bh_enable();
2239 	}
2240 out:
2241 	if (!rt)
2242 		rt = net->ipv6.ip6_null_entry;
2243 	if (!(flags & RT6_LOOKUP_F_DST_NOREF))
2244 		ip6_hold_safe(net, &rt);
2245 	rcu_read_unlock();
2246 
2247 	return rt;
2248 }
2249 EXPORT_SYMBOL_GPL(ip6_pol_route);
2250 
2251 static struct rt6_info *ip6_pol_route_input(struct net *net,
2252 					    struct fib6_table *table,
2253 					    struct flowi6 *fl6,
2254 					    const struct sk_buff *skb,
2255 					    int flags)
2256 {
2257 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
2258 }
2259 
2260 struct dst_entry *ip6_route_input_lookup(struct net *net,
2261 					 struct net_device *dev,
2262 					 struct flowi6 *fl6,
2263 					 const struct sk_buff *skb,
2264 					 int flags)
2265 {
2266 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
2267 		flags |= RT6_LOOKUP_F_IFACE;
2268 
2269 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
2270 }
2271 EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
2272 
2273 static void ip6_multipath_l3_keys(const struct sk_buff *skb,
2274 				  struct flow_keys *keys,
2275 				  struct flow_keys *flkeys)
2276 {
2277 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
2278 	const struct ipv6hdr *key_iph = outer_iph;
2279 	struct flow_keys *_flkeys = flkeys;
2280 	const struct ipv6hdr *inner_iph;
2281 	const struct icmp6hdr *icmph;
2282 	struct ipv6hdr _inner_iph;
2283 	struct icmp6hdr _icmph;
2284 
2285 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
2286 		goto out;
2287 
2288 	icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2289 				   sizeof(_icmph), &_icmph);
2290 	if (!icmph)
2291 		goto out;
2292 
2293 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
2294 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
2295 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
2296 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
2297 		goto out;
2298 
2299 	inner_iph = skb_header_pointer(skb,
2300 				       skb_transport_offset(skb) + sizeof(*icmph),
2301 				       sizeof(_inner_iph), &_inner_iph);
2302 	if (!inner_iph)
2303 		goto out;
2304 
2305 	key_iph = inner_iph;
2306 	_flkeys = NULL;
2307 out:
2308 	if (_flkeys) {
2309 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
2310 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
2311 		keys->tags.flow_label = _flkeys->tags.flow_label;
2312 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
2313 	} else {
2314 		keys->addrs.v6addrs.src = key_iph->saddr;
2315 		keys->addrs.v6addrs.dst = key_iph->daddr;
2316 		keys->tags.flow_label = ip6_flowlabel(key_iph);
2317 		keys->basic.ip_proto = key_iph->nexthdr;
2318 	}
2319 }
2320 
2321 /* if skb is set it will be used and fl6 can be NULL */
2322 u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2323 		       const struct sk_buff *skb, struct flow_keys *flkeys)
2324 {
2325 	struct flow_keys hash_keys;
2326 	u32 mhash;
2327 
2328 	switch (ip6_multipath_hash_policy(net)) {
2329 	case 0:
2330 		memset(&hash_keys, 0, sizeof(hash_keys));
2331 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2332 		if (skb) {
2333 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2334 		} else {
2335 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2336 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2337 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2338 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2339 		}
2340 		break;
2341 	case 1:
2342 		if (skb) {
2343 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2344 			struct flow_keys keys;
2345 
2346 			/* short-circuit if we already have L4 hash present */
2347 			if (skb->l4_hash)
2348 				return skb_get_hash_raw(skb) >> 1;
2349 
2350 			memset(&hash_keys, 0, sizeof(hash_keys));
2351 
2352                         if (!flkeys) {
2353 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2354 				flkeys = &keys;
2355 			}
2356 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2357 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2358 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2359 			hash_keys.ports.src = flkeys->ports.src;
2360 			hash_keys.ports.dst = flkeys->ports.dst;
2361 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2362 		} else {
2363 			memset(&hash_keys, 0, sizeof(hash_keys));
2364 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2365 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2366 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2367 			hash_keys.ports.src = fl6->fl6_sport;
2368 			hash_keys.ports.dst = fl6->fl6_dport;
2369 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2370 		}
2371 		break;
2372 	}
2373 	mhash = flow_hash_from_keys(&hash_keys);
2374 
2375 	return mhash >> 1;
2376 }
2377 
2378 /* Called with rcu held */
2379 void ip6_route_input(struct sk_buff *skb)
2380 {
2381 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2382 	struct net *net = dev_net(skb->dev);
2383 	int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
2384 	struct ip_tunnel_info *tun_info;
2385 	struct flowi6 fl6 = {
2386 		.flowi6_iif = skb->dev->ifindex,
2387 		.daddr = iph->daddr,
2388 		.saddr = iph->saddr,
2389 		.flowlabel = ip6_flowinfo(iph),
2390 		.flowi6_mark = skb->mark,
2391 		.flowi6_proto = iph->nexthdr,
2392 	};
2393 	struct flow_keys *flkeys = NULL, _flkeys;
2394 
2395 	tun_info = skb_tunnel_info(skb);
2396 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2397 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
2398 
2399 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
2400 		flkeys = &_flkeys;
2401 
2402 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2403 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
2404 	skb_dst_drop(skb);
2405 	skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
2406 						      &fl6, skb, flags));
2407 }
2408 
2409 static struct rt6_info *ip6_pol_route_output(struct net *net,
2410 					     struct fib6_table *table,
2411 					     struct flowi6 *fl6,
2412 					     const struct sk_buff *skb,
2413 					     int flags)
2414 {
2415 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2416 }
2417 
2418 struct dst_entry *ip6_route_output_flags_noref(struct net *net,
2419 					       const struct sock *sk,
2420 					       struct flowi6 *fl6, int flags)
2421 {
2422 	bool any_src;
2423 
2424 	if (ipv6_addr_type(&fl6->daddr) &
2425 	    (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
2426 		struct dst_entry *dst;
2427 
2428 		/* This function does not take refcnt on the dst */
2429 		dst = l3mdev_link_scope_lookup(net, fl6);
2430 		if (dst)
2431 			return dst;
2432 	}
2433 
2434 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
2435 
2436 	flags |= RT6_LOOKUP_F_DST_NOREF;
2437 	any_src = ipv6_addr_any(&fl6->saddr);
2438 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2439 	    (fl6->flowi6_oif && any_src))
2440 		flags |= RT6_LOOKUP_F_IFACE;
2441 
2442 	if (!any_src)
2443 		flags |= RT6_LOOKUP_F_HAS_SADDR;
2444 	else if (sk)
2445 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2446 
2447 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
2448 }
2449 EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
2450 
2451 struct dst_entry *ip6_route_output_flags(struct net *net,
2452 					 const struct sock *sk,
2453 					 struct flowi6 *fl6,
2454 					 int flags)
2455 {
2456         struct dst_entry *dst;
2457         struct rt6_info *rt6;
2458 
2459         rcu_read_lock();
2460         dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
2461         rt6 = (struct rt6_info *)dst;
2462         /* For dst cached in uncached_list, refcnt is already taken. */
2463         if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
2464                 dst = &net->ipv6.ip6_null_entry->dst;
2465                 dst_hold(dst);
2466         }
2467         rcu_read_unlock();
2468 
2469         return dst;
2470 }
2471 EXPORT_SYMBOL_GPL(ip6_route_output_flags);
2472 
2473 struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
2474 {
2475 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
2476 	struct net_device *loopback_dev = net->loopback_dev;
2477 	struct dst_entry *new = NULL;
2478 
2479 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
2480 		       DST_OBSOLETE_DEAD, 0);
2481 	if (rt) {
2482 		rt6_info_init(rt);
2483 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
2484 
2485 		new = &rt->dst;
2486 		new->__use = 1;
2487 		new->input = dst_discard;
2488 		new->output = dst_discard_out;
2489 
2490 		dst_copy_metrics(new, &ort->dst);
2491 
2492 		rt->rt6i_idev = in6_dev_get(loopback_dev);
2493 		rt->rt6i_gateway = ort->rt6i_gateway;
2494 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
2495 
2496 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
2497 #ifdef CONFIG_IPV6_SUBTREES
2498 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
2499 #endif
2500 	}
2501 
2502 	dst_release(dst_orig);
2503 	return new ? new : ERR_PTR(-ENOMEM);
2504 }
2505 
2506 /*
2507  *	Destination cache support functions
2508  */
2509 
2510 static bool fib6_check(struct fib6_info *f6i, u32 cookie)
2511 {
2512 	u32 rt_cookie = 0;
2513 
2514 	if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
2515 		return false;
2516 
2517 	if (fib6_check_expired(f6i))
2518 		return false;
2519 
2520 	return true;
2521 }
2522 
2523 static struct dst_entry *rt6_check(struct rt6_info *rt,
2524 				   struct fib6_info *from,
2525 				   u32 cookie)
2526 {
2527 	u32 rt_cookie = 0;
2528 
2529 	if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
2530 	    rt_cookie != cookie)
2531 		return NULL;
2532 
2533 	if (rt6_check_expired(rt))
2534 		return NULL;
2535 
2536 	return &rt->dst;
2537 }
2538 
2539 static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2540 					    struct fib6_info *from,
2541 					    u32 cookie)
2542 {
2543 	if (!__rt6_check_expired(rt) &&
2544 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2545 	    fib6_check(from, cookie))
2546 		return &rt->dst;
2547 	else
2548 		return NULL;
2549 }
2550 
2551 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
2552 {
2553 	struct dst_entry *dst_ret;
2554 	struct fib6_info *from;
2555 	struct rt6_info *rt;
2556 
2557 	rt = container_of(dst, struct rt6_info, dst);
2558 
2559 	rcu_read_lock();
2560 
2561 	/* All IPV6 dsts are created with ->obsolete set to the value
2562 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
2563 	 * into this function always.
2564 	 */
2565 
2566 	from = rcu_dereference(rt->from);
2567 
2568 	if (from && (rt->rt6i_flags & RTF_PCPU ||
2569 	    unlikely(!list_empty(&rt->rt6i_uncached))))
2570 		dst_ret = rt6_dst_from_check(rt, from, cookie);
2571 	else
2572 		dst_ret = rt6_check(rt, from, cookie);
2573 
2574 	rcu_read_unlock();
2575 
2576 	return dst_ret;
2577 }
2578 
2579 static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
2580 {
2581 	struct rt6_info *rt = (struct rt6_info *) dst;
2582 
2583 	if (rt) {
2584 		if (rt->rt6i_flags & RTF_CACHE) {
2585 			rcu_read_lock();
2586 			if (rt6_check_expired(rt)) {
2587 				rt6_remove_exception_rt(rt);
2588 				dst = NULL;
2589 			}
2590 			rcu_read_unlock();
2591 		} else {
2592 			dst_release(dst);
2593 			dst = NULL;
2594 		}
2595 	}
2596 	return dst;
2597 }
2598 
2599 static void ip6_link_failure(struct sk_buff *skb)
2600 {
2601 	struct rt6_info *rt;
2602 
2603 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
2604 
2605 	rt = (struct rt6_info *) skb_dst(skb);
2606 	if (rt) {
2607 		rcu_read_lock();
2608 		if (rt->rt6i_flags & RTF_CACHE) {
2609 			rt6_remove_exception_rt(rt);
2610 		} else {
2611 			struct fib6_info *from;
2612 			struct fib6_node *fn;
2613 
2614 			from = rcu_dereference(rt->from);
2615 			if (from) {
2616 				fn = rcu_dereference(from->fib6_node);
2617 				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2618 					fn->fn_sernum = -1;
2619 			}
2620 		}
2621 		rcu_read_unlock();
2622 	}
2623 }
2624 
2625 static void rt6_update_expires(struct rt6_info *rt0, int timeout)
2626 {
2627 	if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2628 		struct fib6_info *from;
2629 
2630 		rcu_read_lock();
2631 		from = rcu_dereference(rt0->from);
2632 		if (from)
2633 			rt0->dst.expires = from->expires;
2634 		rcu_read_unlock();
2635 	}
2636 
2637 	dst_set_expires(&rt0->dst, timeout);
2638 	rt0->rt6i_flags |= RTF_EXPIRES;
2639 }
2640 
2641 static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
2642 {
2643 	struct net *net = dev_net(rt->dst.dev);
2644 
2645 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
2646 	rt->rt6i_flags |= RTF_MODIFIED;
2647 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
2648 }
2649 
2650 static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
2651 {
2652 	return !(rt->rt6i_flags & RTF_CACHE) &&
2653 		(rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
2654 }
2655 
2656 static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2657 				 const struct ipv6hdr *iph, u32 mtu)
2658 {
2659 	const struct in6_addr *daddr, *saddr;
2660 	struct rt6_info *rt6 = (struct rt6_info *)dst;
2661 
2662 	if (dst_metric_locked(dst, RTAX_MTU))
2663 		return;
2664 
2665 	if (iph) {
2666 		daddr = &iph->daddr;
2667 		saddr = &iph->saddr;
2668 	} else if (sk) {
2669 		daddr = &sk->sk_v6_daddr;
2670 		saddr = &inet6_sk(sk)->saddr;
2671 	} else {
2672 		daddr = NULL;
2673 		saddr = NULL;
2674 	}
2675 	dst_confirm_neigh(dst, daddr);
2676 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
2677 	if (mtu >= dst_mtu(dst))
2678 		return;
2679 
2680 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
2681 		rt6_do_update_pmtu(rt6, mtu);
2682 		/* update rt6_ex->stamp for cache */
2683 		if (rt6->rt6i_flags & RTF_CACHE)
2684 			rt6_update_exception_stamp_rt(rt6);
2685 	} else if (daddr) {
2686 		struct fib6_result res = {};
2687 		struct rt6_info *nrt6;
2688 
2689 		rcu_read_lock();
2690 		res.f6i = rcu_dereference(rt6->from);
2691 		if (!res.f6i) {
2692 			rcu_read_unlock();
2693 			return;
2694 		}
2695 		res.fib6_flags = res.f6i->fib6_flags;
2696 		res.fib6_type = res.f6i->fib6_type;
2697 
2698 		if (res.f6i->nh) {
2699 			struct fib6_nh_match_arg arg = {
2700 				.dev = dst->dev,
2701 				.gw = &rt6->rt6i_gateway,
2702 			};
2703 
2704 			nexthop_for_each_fib6_nh(res.f6i->nh,
2705 						 fib6_nh_find_match, &arg);
2706 
2707 			/* fib6_info uses a nexthop that does not have fib6_nh
2708 			 * using the dst->dev + gw. Should be impossible.
2709 			 */
2710 			if (!arg.match) {
2711 				rcu_read_unlock();
2712 				return;
2713 			}
2714 
2715 			res.nh = arg.match;
2716 		} else {
2717 			res.nh = res.f6i->fib6_nh;
2718 		}
2719 
2720 		nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
2721 		if (nrt6) {
2722 			rt6_do_update_pmtu(nrt6, mtu);
2723 			if (rt6_insert_exception(nrt6, &res))
2724 				dst_release_immediate(&nrt6->dst);
2725 		}
2726 		rcu_read_unlock();
2727 	}
2728 }
2729 
2730 static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2731 			       struct sk_buff *skb, u32 mtu)
2732 {
2733 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
2734 }
2735 
2736 void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2737 		     int oif, u32 mark, kuid_t uid)
2738 {
2739 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2740 	struct dst_entry *dst;
2741 	struct flowi6 fl6 = {
2742 		.flowi6_oif = oif,
2743 		.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2744 		.daddr = iph->daddr,
2745 		.saddr = iph->saddr,
2746 		.flowlabel = ip6_flowinfo(iph),
2747 		.flowi6_uid = uid,
2748 	};
2749 
2750 	dst = ip6_route_output(net, NULL, &fl6);
2751 	if (!dst->error)
2752 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
2753 	dst_release(dst);
2754 }
2755 EXPORT_SYMBOL_GPL(ip6_update_pmtu);
2756 
2757 void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
2758 {
2759 	int oif = sk->sk_bound_dev_if;
2760 	struct dst_entry *dst;
2761 
2762 	if (!oif && skb->dev)
2763 		oif = l3mdev_master_ifindex(skb->dev);
2764 
2765 	ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
2766 
2767 	dst = __sk_dst_get(sk);
2768 	if (!dst || !dst->obsolete ||
2769 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
2770 		return;
2771 
2772 	bh_lock_sock(sk);
2773 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
2774 		ip6_datagram_dst_update(sk, false);
2775 	bh_unlock_sock(sk);
2776 }
2777 EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
2778 
2779 void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
2780 			   const struct flowi6 *fl6)
2781 {
2782 #ifdef CONFIG_IPV6_SUBTREES
2783 	struct ipv6_pinfo *np = inet6_sk(sk);
2784 #endif
2785 
2786 	ip6_dst_store(sk, dst,
2787 		      ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
2788 		      &sk->sk_v6_daddr : NULL,
2789 #ifdef CONFIG_IPV6_SUBTREES
2790 		      ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
2791 		      &np->saddr :
2792 #endif
2793 		      NULL);
2794 }
2795 
2796 static bool ip6_redirect_nh_match(const struct fib6_result *res,
2797 				  struct flowi6 *fl6,
2798 				  const struct in6_addr *gw,
2799 				  struct rt6_info **ret)
2800 {
2801 	const struct fib6_nh *nh = res->nh;
2802 
2803 	if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
2804 	    fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
2805 		return false;
2806 
2807 	/* rt_cache's gateway might be different from its 'parent'
2808 	 * in the case of an ip redirect.
2809 	 * So we keep searching in the exception table if the gateway
2810 	 * is different.
2811 	 */
2812 	if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
2813 		struct rt6_info *rt_cache;
2814 
2815 		rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
2816 		if (rt_cache &&
2817 		    ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
2818 			*ret = rt_cache;
2819 			return true;
2820 		}
2821 		return false;
2822 	}
2823 	return true;
2824 }
2825 
2826 struct fib6_nh_rd_arg {
2827 	struct fib6_result	*res;
2828 	struct flowi6		*fl6;
2829 	const struct in6_addr	*gw;
2830 	struct rt6_info		**ret;
2831 };
2832 
2833 static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2834 {
2835 	struct fib6_nh_rd_arg *arg = _arg;
2836 
2837 	arg->res->nh = nh;
2838 	return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2839 }
2840 
2841 /* Handle redirects */
2842 struct ip6rd_flowi {
2843 	struct flowi6 fl6;
2844 	struct in6_addr gateway;
2845 };
2846 
2847 static struct rt6_info *__ip6_route_redirect(struct net *net,
2848 					     struct fib6_table *table,
2849 					     struct flowi6 *fl6,
2850 					     const struct sk_buff *skb,
2851 					     int flags)
2852 {
2853 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
2854 	struct rt6_info *ret = NULL;
2855 	struct fib6_result res = {};
2856 	struct fib6_nh_rd_arg arg = {
2857 		.res = &res,
2858 		.fl6 = fl6,
2859 		.gw  = &rdfl->gateway,
2860 		.ret = &ret
2861 	};
2862 	struct fib6_info *rt;
2863 	struct fib6_node *fn;
2864 
2865 	/* l3mdev_update_flow overrides oif if the device is enslaved; in
2866 	 * this case we must match on the real ingress device, so reset it
2867 	 */
2868 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2869 		fl6->flowi6_oif = skb->dev->ifindex;
2870 
2871 	/* Get the "current" route for this destination and
2872 	 * check if the redirect has come from appropriate router.
2873 	 *
2874 	 * RFC 4861 specifies that redirects should only be
2875 	 * accepted if they come from the nexthop to the target.
2876 	 * Due to the way the routes are chosen, this notion
2877 	 * is a bit fuzzy and one might need to check all possible
2878 	 * routes.
2879 	 */
2880 
2881 	rcu_read_lock();
2882 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2883 restart:
2884 	for_each_fib6_node_rt_rcu(fn) {
2885 		res.f6i = rt;
2886 		if (fib6_check_expired(rt))
2887 			continue;
2888 		if (rt->fib6_flags & RTF_REJECT)
2889 			break;
2890 		if (unlikely(rt->nh)) {
2891 			if (nexthop_is_blackhole(rt->nh))
2892 				continue;
2893 			/* on match, res->nh is filled in and potentially ret */
2894 			if (nexthop_for_each_fib6_nh(rt->nh,
2895 						     fib6_nh_redirect_match,
2896 						     &arg))
2897 				goto out;
2898 		} else {
2899 			res.nh = rt->fib6_nh;
2900 			if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2901 						  &ret))
2902 				goto out;
2903 		}
2904 	}
2905 
2906 	if (!rt)
2907 		rt = net->ipv6.fib6_null_entry;
2908 	else if (rt->fib6_flags & RTF_REJECT) {
2909 		ret = net->ipv6.ip6_null_entry;
2910 		goto out;
2911 	}
2912 
2913 	if (rt == net->ipv6.fib6_null_entry) {
2914 		fn = fib6_backtrack(fn, &fl6->saddr);
2915 		if (fn)
2916 			goto restart;
2917 	}
2918 
2919 	res.f6i = rt;
2920 	res.nh = rt->fib6_nh;
2921 out:
2922 	if (ret) {
2923 		ip6_hold_safe(net, &ret);
2924 	} else {
2925 		res.fib6_flags = res.f6i->fib6_flags;
2926 		res.fib6_type = res.f6i->fib6_type;
2927 		ret = ip6_create_rt_rcu(&res);
2928 	}
2929 
2930 	rcu_read_unlock();
2931 
2932 	trace_fib6_table_lookup(net, &res, table, fl6);
2933 	return ret;
2934 };
2935 
2936 static struct dst_entry *ip6_route_redirect(struct net *net,
2937 					    const struct flowi6 *fl6,
2938 					    const struct sk_buff *skb,
2939 					    const struct in6_addr *gateway)
2940 {
2941 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2942 	struct ip6rd_flowi rdfl;
2943 
2944 	rdfl.fl6 = *fl6;
2945 	rdfl.gateway = *gateway;
2946 
2947 	return fib6_rule_lookup(net, &rdfl.fl6, skb,
2948 				flags, __ip6_route_redirect);
2949 }
2950 
2951 void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2952 		  kuid_t uid)
2953 {
2954 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
2955 	struct dst_entry *dst;
2956 	struct flowi6 fl6 = {
2957 		.flowi6_iif = LOOPBACK_IFINDEX,
2958 		.flowi6_oif = oif,
2959 		.flowi6_mark = mark,
2960 		.daddr = iph->daddr,
2961 		.saddr = iph->saddr,
2962 		.flowlabel = ip6_flowinfo(iph),
2963 		.flowi6_uid = uid,
2964 	};
2965 
2966 	dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
2967 	rt6_do_redirect(dst, NULL, skb);
2968 	dst_release(dst);
2969 }
2970 EXPORT_SYMBOL_GPL(ip6_redirect);
2971 
2972 void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
2973 {
2974 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2975 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2976 	struct dst_entry *dst;
2977 	struct flowi6 fl6 = {
2978 		.flowi6_iif = LOOPBACK_IFINDEX,
2979 		.flowi6_oif = oif,
2980 		.daddr = msg->dest,
2981 		.saddr = iph->daddr,
2982 		.flowi6_uid = sock_net_uid(net, NULL),
2983 	};
2984 
2985 	dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
2986 	rt6_do_redirect(dst, NULL, skb);
2987 	dst_release(dst);
2988 }
2989 
2990 void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
2991 {
2992 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2993 		     sk->sk_uid);
2994 }
2995 EXPORT_SYMBOL_GPL(ip6_sk_redirect);
2996 
2997 static unsigned int ip6_default_advmss(const struct dst_entry *dst)
2998 {
2999 	struct net_device *dev = dst->dev;
3000 	unsigned int mtu = dst_mtu(dst);
3001 	struct net *net = dev_net(dev);
3002 
3003 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
3004 
3005 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
3006 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
3007 
3008 	/*
3009 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
3010 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
3011 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
3012 	 * rely only on pmtu discovery"
3013 	 */
3014 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
3015 		mtu = IPV6_MAXPLEN;
3016 	return mtu;
3017 }
3018 
3019 static unsigned int ip6_mtu(const struct dst_entry *dst)
3020 {
3021 	struct inet6_dev *idev;
3022 	unsigned int mtu;
3023 
3024 	mtu = dst_metric_raw(dst, RTAX_MTU);
3025 	if (mtu)
3026 		goto out;
3027 
3028 	mtu = IPV6_MIN_MTU;
3029 
3030 	rcu_read_lock();
3031 	idev = __in6_dev_get(dst->dev);
3032 	if (idev)
3033 		mtu = idev->cnf.mtu6;
3034 	rcu_read_unlock();
3035 
3036 out:
3037 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3038 
3039 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
3040 }
3041 
3042 /* MTU selection:
3043  * 1. mtu on route is locked - use it
3044  * 2. mtu from nexthop exception
3045  * 3. mtu from egress device
3046  *
3047  * based on ip6_dst_mtu_forward and exception logic of
3048  * rt6_find_cached_rt; called with rcu_read_lock
3049  */
3050 u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3051 		      const struct in6_addr *daddr,
3052 		      const struct in6_addr *saddr)
3053 {
3054 	const struct fib6_nh *nh = res->nh;
3055 	struct fib6_info *f6i = res->f6i;
3056 	struct inet6_dev *idev;
3057 	struct rt6_info *rt;
3058 	u32 mtu = 0;
3059 
3060 	if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3061 		mtu = f6i->fib6_pmtu;
3062 		if (mtu)
3063 			goto out;
3064 	}
3065 
3066 	rt = rt6_find_cached_rt(res, daddr, saddr);
3067 	if (unlikely(rt)) {
3068 		mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3069 	} else {
3070 		struct net_device *dev = nh->fib_nh_dev;
3071 
3072 		mtu = IPV6_MIN_MTU;
3073 		idev = __in6_dev_get(dev);
3074 		if (idev && idev->cnf.mtu6 > mtu)
3075 			mtu = idev->cnf.mtu6;
3076 	}
3077 
3078 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3079 out:
3080 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
3081 }
3082 
3083 struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
3084 				  struct flowi6 *fl6)
3085 {
3086 	struct dst_entry *dst;
3087 	struct rt6_info *rt;
3088 	struct inet6_dev *idev = in6_dev_get(dev);
3089 	struct net *net = dev_net(dev);
3090 
3091 	if (unlikely(!idev))
3092 		return ERR_PTR(-ENODEV);
3093 
3094 	rt = ip6_dst_alloc(net, dev, 0);
3095 	if (unlikely(!rt)) {
3096 		in6_dev_put(idev);
3097 		dst = ERR_PTR(-ENOMEM);
3098 		goto out;
3099 	}
3100 
3101 	rt->dst.flags |= DST_HOST;
3102 	rt->dst.input = ip6_input;
3103 	rt->dst.output  = ip6_output;
3104 	rt->rt6i_gateway  = fl6->daddr;
3105 	rt->rt6i_dst.addr = fl6->daddr;
3106 	rt->rt6i_dst.plen = 128;
3107 	rt->rt6i_idev     = idev;
3108 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
3109 
3110 	/* Add this dst into uncached_list so that rt6_disable_ip() can
3111 	 * do proper release of the net_device
3112 	 */
3113 	rt6_uncached_list_add(rt);
3114 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
3115 
3116 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
3117 
3118 out:
3119 	return dst;
3120 }
3121 
3122 static int ip6_dst_gc(struct dst_ops *ops)
3123 {
3124 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
3125 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
3126 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
3127 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
3128 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
3129 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
3130 	int entries;
3131 
3132 	entries = dst_entries_get_fast(ops);
3133 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
3134 	    entries <= rt_max_size)
3135 		goto out;
3136 
3137 	net->ipv6.ip6_rt_gc_expire++;
3138 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
3139 	entries = dst_entries_get_slow(ops);
3140 	if (entries < ops->gc_thresh)
3141 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
3142 out:
3143 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
3144 	return entries > rt_max_size;
3145 }
3146 
3147 static struct rt6_info *ip6_nh_lookup_table(struct net *net,
3148 					    struct fib6_config *cfg,
3149 					    const struct in6_addr *gw_addr,
3150 					    u32 tbid, int flags)
3151 {
3152 	struct flowi6 fl6 = {
3153 		.flowi6_oif = cfg->fc_ifindex,
3154 		.daddr = *gw_addr,
3155 		.saddr = cfg->fc_prefsrc,
3156 	};
3157 	struct fib6_table *table;
3158 	struct rt6_info *rt;
3159 
3160 	table = fib6_get_table(net, tbid);
3161 	if (!table)
3162 		return NULL;
3163 
3164 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
3165 		flags |= RT6_LOOKUP_F_HAS_SADDR;
3166 
3167 	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
3168 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
3169 
3170 	/* if table lookup failed, fall back to full lookup */
3171 	if (rt == net->ipv6.ip6_null_entry) {
3172 		ip6_rt_put(rt);
3173 		rt = NULL;
3174 	}
3175 
3176 	return rt;
3177 }
3178 
3179 static int ip6_route_check_nh_onlink(struct net *net,
3180 				     struct fib6_config *cfg,
3181 				     const struct net_device *dev,
3182 				     struct netlink_ext_ack *extack)
3183 {
3184 	u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
3185 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
3186 	u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
3187 	struct fib6_info *from;
3188 	struct rt6_info *grt;
3189 	int err;
3190 
3191 	err = 0;
3192 	grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
3193 	if (grt) {
3194 		rcu_read_lock();
3195 		from = rcu_dereference(grt->from);
3196 		if (!grt->dst.error &&
3197 		    /* ignore match if it is the default route */
3198 		    from && !ipv6_addr_any(&from->fib6_dst.addr) &&
3199 		    (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
3200 			NL_SET_ERR_MSG(extack,
3201 				       "Nexthop has invalid gateway or device mismatch");
3202 			err = -EINVAL;
3203 		}
3204 		rcu_read_unlock();
3205 
3206 		ip6_rt_put(grt);
3207 	}
3208 
3209 	return err;
3210 }
3211 
3212 static int ip6_route_check_nh(struct net *net,
3213 			      struct fib6_config *cfg,
3214 			      struct net_device **_dev,
3215 			      struct inet6_dev **idev)
3216 {
3217 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
3218 	struct net_device *dev = _dev ? *_dev : NULL;
3219 	struct rt6_info *grt = NULL;
3220 	int err = -EHOSTUNREACH;
3221 
3222 	if (cfg->fc_table) {
3223 		int flags = RT6_LOOKUP_F_IFACE;
3224 
3225 		grt = ip6_nh_lookup_table(net, cfg, gw_addr,
3226 					  cfg->fc_table, flags);
3227 		if (grt) {
3228 			if (grt->rt6i_flags & RTF_GATEWAY ||
3229 			    (dev && dev != grt->dst.dev)) {
3230 				ip6_rt_put(grt);
3231 				grt = NULL;
3232 			}
3233 		}
3234 	}
3235 
3236 	if (!grt)
3237 		grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
3238 
3239 	if (!grt)
3240 		goto out;
3241 
3242 	if (dev) {
3243 		if (dev != grt->dst.dev) {
3244 			ip6_rt_put(grt);
3245 			goto out;
3246 		}
3247 	} else {
3248 		*_dev = dev = grt->dst.dev;
3249 		*idev = grt->rt6i_idev;
3250 		dev_hold(dev);
3251 		in6_dev_hold(grt->rt6i_idev);
3252 	}
3253 
3254 	if (!(grt->rt6i_flags & RTF_GATEWAY))
3255 		err = 0;
3256 
3257 	ip6_rt_put(grt);
3258 
3259 out:
3260 	return err;
3261 }
3262 
3263 static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
3264 			   struct net_device **_dev, struct inet6_dev **idev,
3265 			   struct netlink_ext_ack *extack)
3266 {
3267 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
3268 	int gwa_type = ipv6_addr_type(gw_addr);
3269 	bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
3270 	const struct net_device *dev = *_dev;
3271 	bool need_addr_check = !dev;
3272 	int err = -EINVAL;
3273 
3274 	/* if gw_addr is local we will fail to detect this in case
3275 	 * address is still TENTATIVE (DAD in progress). rt6_lookup()
3276 	 * will return already-added prefix route via interface that
3277 	 * prefix route was assigned to, which might be non-loopback.
3278 	 */
3279 	if (dev &&
3280 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3281 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3282 		goto out;
3283 	}
3284 
3285 	if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
3286 		/* IPv6 strictly inhibits using not link-local
3287 		 * addresses as nexthop address.
3288 		 * Otherwise, router will not able to send redirects.
3289 		 * It is very good, but in some (rare!) circumstances
3290 		 * (SIT, PtP, NBMA NOARP links) it is handy to allow
3291 		 * some exceptions. --ANK
3292 		 * We allow IPv4-mapped nexthops to support RFC4798-type
3293 		 * addressing
3294 		 */
3295 		if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
3296 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
3297 			goto out;
3298 		}
3299 
3300 		if (cfg->fc_flags & RTNH_F_ONLINK)
3301 			err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
3302 		else
3303 			err = ip6_route_check_nh(net, cfg, _dev, idev);
3304 
3305 		if (err)
3306 			goto out;
3307 	}
3308 
3309 	/* reload in case device was changed */
3310 	dev = *_dev;
3311 
3312 	err = -EINVAL;
3313 	if (!dev) {
3314 		NL_SET_ERR_MSG(extack, "Egress device not specified");
3315 		goto out;
3316 	} else if (dev->flags & IFF_LOOPBACK) {
3317 		NL_SET_ERR_MSG(extack,
3318 			       "Egress device can not be loopback device for this route");
3319 		goto out;
3320 	}
3321 
3322 	/* if we did not check gw_addr above, do so now that the
3323 	 * egress device has been resolved.
3324 	 */
3325 	if (need_addr_check &&
3326 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3327 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3328 		goto out;
3329 	}
3330 
3331 	err = 0;
3332 out:
3333 	return err;
3334 }
3335 
3336 static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
3337 {
3338 	if ((flags & RTF_REJECT) ||
3339 	    (dev && (dev->flags & IFF_LOOPBACK) &&
3340 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
3341 	     !(flags & RTF_LOCAL)))
3342 		return true;
3343 
3344 	return false;
3345 }
3346 
3347 int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
3348 		 struct fib6_config *cfg, gfp_t gfp_flags,
3349 		 struct netlink_ext_ack *extack)
3350 {
3351 	struct net_device *dev = NULL;
3352 	struct inet6_dev *idev = NULL;
3353 	int addr_type;
3354 	int err;
3355 
3356 	fib6_nh->fib_nh_family = AF_INET6;
3357 
3358 	err = -ENODEV;
3359 	if (cfg->fc_ifindex) {
3360 		dev = dev_get_by_index(net, cfg->fc_ifindex);
3361 		if (!dev)
3362 			goto out;
3363 		idev = in6_dev_get(dev);
3364 		if (!idev)
3365 			goto out;
3366 	}
3367 
3368 	if (cfg->fc_flags & RTNH_F_ONLINK) {
3369 		if (!dev) {
3370 			NL_SET_ERR_MSG(extack,
3371 				       "Nexthop device required for onlink");
3372 			goto out;
3373 		}
3374 
3375 		if (!(dev->flags & IFF_UP)) {
3376 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3377 			err = -ENETDOWN;
3378 			goto out;
3379 		}
3380 
3381 		fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
3382 	}
3383 
3384 	fib6_nh->fib_nh_weight = 1;
3385 
3386 	/* We cannot add true routes via loopback here,
3387 	 * they would result in kernel looping; promote them to reject routes
3388 	 */
3389 	addr_type = ipv6_addr_type(&cfg->fc_dst);
3390 	if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
3391 		/* hold loopback dev/idev if we haven't done so. */
3392 		if (dev != net->loopback_dev) {
3393 			if (dev) {
3394 				dev_put(dev);
3395 				in6_dev_put(idev);
3396 			}
3397 			dev = net->loopback_dev;
3398 			dev_hold(dev);
3399 			idev = in6_dev_get(dev);
3400 			if (!idev) {
3401 				err = -ENODEV;
3402 				goto out;
3403 			}
3404 		}
3405 		goto pcpu_alloc;
3406 	}
3407 
3408 	if (cfg->fc_flags & RTF_GATEWAY) {
3409 		err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3410 		if (err)
3411 			goto out;
3412 
3413 		fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3414 		fib6_nh->fib_nh_gw_family = AF_INET6;
3415 	}
3416 
3417 	err = -ENODEV;
3418 	if (!dev)
3419 		goto out;
3420 
3421 	if (idev->cnf.disable_ipv6) {
3422 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3423 		err = -EACCES;
3424 		goto out;
3425 	}
3426 
3427 	if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3428 		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3429 		err = -ENETDOWN;
3430 		goto out;
3431 	}
3432 
3433 	if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3434 	    !netif_carrier_ok(dev))
3435 		fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
3436 
3437 	err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
3438 				 cfg->fc_encap_type, cfg, gfp_flags, extack);
3439 	if (err)
3440 		goto out;
3441 
3442 pcpu_alloc:
3443 	fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3444 	if (!fib6_nh->rt6i_pcpu) {
3445 		err = -ENOMEM;
3446 		goto out;
3447 	}
3448 
3449 	fib6_nh->fib_nh_dev = dev;
3450 	fib6_nh->fib_nh_oif = dev->ifindex;
3451 	err = 0;
3452 out:
3453 	if (idev)
3454 		in6_dev_put(idev);
3455 
3456 	if (err) {
3457 		lwtstate_put(fib6_nh->fib_nh_lws);
3458 		fib6_nh->fib_nh_lws = NULL;
3459 		if (dev)
3460 			dev_put(dev);
3461 	}
3462 
3463 	return err;
3464 }
3465 
3466 void fib6_nh_release(struct fib6_nh *fib6_nh)
3467 {
3468 	struct rt6_exception_bucket *bucket;
3469 
3470 	rcu_read_lock();
3471 
3472 	fib6_nh_flush_exceptions(fib6_nh, NULL);
3473 	bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3474 	if (bucket) {
3475 		rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3476 		kfree(bucket);
3477 	}
3478 
3479 	rcu_read_unlock();
3480 
3481 	if (fib6_nh->rt6i_pcpu) {
3482 		int cpu;
3483 
3484 		for_each_possible_cpu(cpu) {
3485 			struct rt6_info **ppcpu_rt;
3486 			struct rt6_info *pcpu_rt;
3487 
3488 			ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3489 			pcpu_rt = *ppcpu_rt;
3490 			if (pcpu_rt) {
3491 				dst_dev_put(&pcpu_rt->dst);
3492 				dst_release(&pcpu_rt->dst);
3493 				*ppcpu_rt = NULL;
3494 			}
3495 		}
3496 
3497 		free_percpu(fib6_nh->rt6i_pcpu);
3498 	}
3499 
3500 	fib_nh_common_release(&fib6_nh->nh_common);
3501 }
3502 
3503 static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3504 					      gfp_t gfp_flags,
3505 					      struct netlink_ext_ack *extack)
3506 {
3507 	struct net *net = cfg->fc_nlinfo.nl_net;
3508 	struct fib6_info *rt = NULL;
3509 	struct nexthop *nh = NULL;
3510 	struct fib6_table *table;
3511 	struct fib6_nh *fib6_nh;
3512 	int err = -EINVAL;
3513 	int addr_type;
3514 
3515 	/* RTF_PCPU is an internal flag; can not be set by userspace */
3516 	if (cfg->fc_flags & RTF_PCPU) {
3517 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3518 		goto out;
3519 	}
3520 
3521 	/* RTF_CACHE is an internal flag; can not be set by userspace */
3522 	if (cfg->fc_flags & RTF_CACHE) {
3523 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
3524 		goto out;
3525 	}
3526 
3527 	if (cfg->fc_type > RTN_MAX) {
3528 		NL_SET_ERR_MSG(extack, "Invalid route type");
3529 		goto out;
3530 	}
3531 
3532 	if (cfg->fc_dst_len > 128) {
3533 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
3534 		goto out;
3535 	}
3536 	if (cfg->fc_src_len > 128) {
3537 		NL_SET_ERR_MSG(extack, "Invalid source address length");
3538 		goto out;
3539 	}
3540 #ifndef CONFIG_IPV6_SUBTREES
3541 	if (cfg->fc_src_len) {
3542 		NL_SET_ERR_MSG(extack,
3543 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
3544 		goto out;
3545 	}
3546 #endif
3547 	if (cfg->fc_nh_id) {
3548 		nh = nexthop_find_by_id(net, cfg->fc_nh_id);
3549 		if (!nh) {
3550 			NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
3551 			goto out;
3552 		}
3553 		err = fib6_check_nexthop(nh, cfg, extack);
3554 		if (err)
3555 			goto out;
3556 	}
3557 
3558 	err = -ENOBUFS;
3559 	if (cfg->fc_nlinfo.nlh &&
3560 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3561 		table = fib6_get_table(net, cfg->fc_table);
3562 		if (!table) {
3563 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3564 			table = fib6_new_table(net, cfg->fc_table);
3565 		}
3566 	} else {
3567 		table = fib6_new_table(net, cfg->fc_table);
3568 	}
3569 
3570 	if (!table)
3571 		goto out;
3572 
3573 	err = -ENOMEM;
3574 	rt = fib6_info_alloc(gfp_flags, !nh);
3575 	if (!rt)
3576 		goto out;
3577 
3578 	rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3579 					       extack);
3580 	if (IS_ERR(rt->fib6_metrics)) {
3581 		err = PTR_ERR(rt->fib6_metrics);
3582 		/* Do not leave garbage there. */
3583 		rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3584 		goto out;
3585 	}
3586 
3587 	if (cfg->fc_flags & RTF_ADDRCONF)
3588 		rt->dst_nocount = true;
3589 
3590 	if (cfg->fc_flags & RTF_EXPIRES)
3591 		fib6_set_expires(rt, jiffies +
3592 				clock_t_to_jiffies(cfg->fc_expires));
3593 	else
3594 		fib6_clean_expires(rt);
3595 
3596 	if (cfg->fc_protocol == RTPROT_UNSPEC)
3597 		cfg->fc_protocol = RTPROT_BOOT;
3598 	rt->fib6_protocol = cfg->fc_protocol;
3599 
3600 	rt->fib6_table = table;
3601 	rt->fib6_metric = cfg->fc_metric;
3602 	rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
3603 	rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
3604 
3605 	ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
3606 	rt->fib6_dst.plen = cfg->fc_dst_len;
3607 	if (rt->fib6_dst.plen == 128)
3608 		rt->dst_host = true;
3609 
3610 #ifdef CONFIG_IPV6_SUBTREES
3611 	ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
3612 	rt->fib6_src.plen = cfg->fc_src_len;
3613 #endif
3614 	if (nh) {
3615 		if (!nexthop_get(nh)) {
3616 			NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3617 			goto out;
3618 		}
3619 		if (rt->fib6_src.plen) {
3620 			NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
3621 			goto out;
3622 		}
3623 		rt->nh = nh;
3624 		fib6_nh = nexthop_fib6_nh(rt->nh);
3625 	} else {
3626 		err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
3627 		if (err)
3628 			goto out;
3629 
3630 		fib6_nh = rt->fib6_nh;
3631 
3632 		/* We cannot add true routes via loopback here, they would
3633 		 * result in kernel looping; promote them to reject routes
3634 		 */
3635 		addr_type = ipv6_addr_type(&cfg->fc_dst);
3636 		if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3637 				   addr_type))
3638 			rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3639 	}
3640 
3641 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3642 		struct net_device *dev = fib6_nh->fib_nh_dev;
3643 
3644 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3645 			NL_SET_ERR_MSG(extack, "Invalid source address");
3646 			err = -EINVAL;
3647 			goto out;
3648 		}
3649 		rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
3650 		rt->fib6_prefsrc.plen = 128;
3651 	} else
3652 		rt->fib6_prefsrc.plen = 0;
3653 
3654 	return rt;
3655 out:
3656 	fib6_info_release(rt);
3657 	return ERR_PTR(err);
3658 }
3659 
3660 int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3661 		  struct netlink_ext_ack *extack)
3662 {
3663 	struct fib6_info *rt;
3664 	int err;
3665 
3666 	rt = ip6_route_info_create(cfg, gfp_flags, extack);
3667 	if (IS_ERR(rt))
3668 		return PTR_ERR(rt);
3669 
3670 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
3671 	fib6_info_release(rt);
3672 
3673 	return err;
3674 }
3675 
3676 static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
3677 {
3678 	struct net *net = info->nl_net;
3679 	struct fib6_table *table;
3680 	int err;
3681 
3682 	if (rt == net->ipv6.fib6_null_entry) {
3683 		err = -ENOENT;
3684 		goto out;
3685 	}
3686 
3687 	table = rt->fib6_table;
3688 	spin_lock_bh(&table->tb6_lock);
3689 	err = fib6_del(rt, info);
3690 	spin_unlock_bh(&table->tb6_lock);
3691 
3692 out:
3693 	fib6_info_release(rt);
3694 	return err;
3695 }
3696 
3697 int ip6_del_rt(struct net *net, struct fib6_info *rt)
3698 {
3699 	struct nl_info info = { .nl_net = net };
3700 
3701 	return __ip6_del_rt(rt, &info);
3702 }
3703 
3704 static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
3705 {
3706 	struct nl_info *info = &cfg->fc_nlinfo;
3707 	struct net *net = info->nl_net;
3708 	struct sk_buff *skb = NULL;
3709 	struct fib6_table *table;
3710 	int err = -ENOENT;
3711 
3712 	if (rt == net->ipv6.fib6_null_entry)
3713 		goto out_put;
3714 	table = rt->fib6_table;
3715 	spin_lock_bh(&table->tb6_lock);
3716 
3717 	if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
3718 		struct fib6_info *sibling, *next_sibling;
3719 
3720 		/* prefer to send a single notification with all hops */
3721 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
3722 		if (skb) {
3723 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
3724 
3725 			if (rt6_fill_node(net, skb, rt, NULL,
3726 					  NULL, NULL, 0, RTM_DELROUTE,
3727 					  info->portid, seq, 0) < 0) {
3728 				kfree_skb(skb);
3729 				skb = NULL;
3730 			} else
3731 				info->skip_notify = 1;
3732 		}
3733 
3734 		info->skip_notify_kernel = 1;
3735 		call_fib6_multipath_entry_notifiers(net,
3736 						    FIB_EVENT_ENTRY_DEL,
3737 						    rt,
3738 						    rt->fib6_nsiblings,
3739 						    NULL);
3740 		list_for_each_entry_safe(sibling, next_sibling,
3741 					 &rt->fib6_siblings,
3742 					 fib6_siblings) {
3743 			err = fib6_del(sibling, info);
3744 			if (err)
3745 				goto out_unlock;
3746 		}
3747 	}
3748 
3749 	err = fib6_del(rt, info);
3750 out_unlock:
3751 	spin_unlock_bh(&table->tb6_lock);
3752 out_put:
3753 	fib6_info_release(rt);
3754 
3755 	if (skb) {
3756 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
3757 			    info->nlh, gfp_any());
3758 	}
3759 	return err;
3760 }
3761 
3762 static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
3763 {
3764 	int rc = -ESRCH;
3765 
3766 	if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
3767 		goto out;
3768 
3769 	if (cfg->fc_flags & RTF_GATEWAY &&
3770 	    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
3771 		goto out;
3772 
3773 	rc = rt6_remove_exception_rt(rt);
3774 out:
3775 	return rc;
3776 }
3777 
3778 static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3779 			     struct fib6_nh *nh)
3780 {
3781 	struct fib6_result res = {
3782 		.f6i = rt,
3783 		.nh = nh,
3784 	};
3785 	struct rt6_info *rt_cache;
3786 
3787 	rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3788 	if (rt_cache)
3789 		return __ip6_del_cached_rt(rt_cache, cfg);
3790 
3791 	return 0;
3792 }
3793 
3794 struct fib6_nh_del_cached_rt_arg {
3795 	struct fib6_config *cfg;
3796 	struct fib6_info *f6i;
3797 };
3798 
3799 static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
3800 {
3801 	struct fib6_nh_del_cached_rt_arg *arg = _arg;
3802 	int rc;
3803 
3804 	rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
3805 	return rc != -ESRCH ? rc : 0;
3806 }
3807 
3808 static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
3809 {
3810 	struct fib6_nh_del_cached_rt_arg arg = {
3811 		.cfg = cfg,
3812 		.f6i = f6i
3813 	};
3814 
3815 	return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
3816 }
3817 
3818 static int ip6_route_del(struct fib6_config *cfg,
3819 			 struct netlink_ext_ack *extack)
3820 {
3821 	struct fib6_table *table;
3822 	struct fib6_info *rt;
3823 	struct fib6_node *fn;
3824 	int err = -ESRCH;
3825 
3826 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3827 	if (!table) {
3828 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
3829 		return err;
3830 	}
3831 
3832 	rcu_read_lock();
3833 
3834 	fn = fib6_locate(&table->tb6_root,
3835 			 &cfg->fc_dst, cfg->fc_dst_len,
3836 			 &cfg->fc_src, cfg->fc_src_len,
3837 			 !(cfg->fc_flags & RTF_CACHE));
3838 
3839 	if (fn) {
3840 		for_each_fib6_node_rt_rcu(fn) {
3841 			struct fib6_nh *nh;
3842 
3843 			if (rt->nh && rt->nh->id != cfg->fc_nh_id)
3844 				continue;
3845 
3846 			if (cfg->fc_flags & RTF_CACHE) {
3847 				int rc = 0;
3848 
3849 				if (rt->nh) {
3850 					rc = ip6_del_cached_rt_nh(cfg, rt);
3851 				} else if (cfg->fc_nh_id) {
3852 					continue;
3853 				} else {
3854 					nh = rt->fib6_nh;
3855 					rc = ip6_del_cached_rt(cfg, rt, nh);
3856 				}
3857 				if (rc != -ESRCH) {
3858 					rcu_read_unlock();
3859 					return rc;
3860 				}
3861 				continue;
3862 			}
3863 
3864 			if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
3865 				continue;
3866 			if (cfg->fc_protocol &&
3867 			    cfg->fc_protocol != rt->fib6_protocol)
3868 				continue;
3869 
3870 			if (rt->nh) {
3871 				if (!fib6_info_hold_safe(rt))
3872 					continue;
3873 				rcu_read_unlock();
3874 
3875 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3876 			}
3877 			if (cfg->fc_nh_id)
3878 				continue;
3879 
3880 			nh = rt->fib6_nh;
3881 			if (cfg->fc_ifindex &&
3882 			    (!nh->fib_nh_dev ||
3883 			     nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
3884 				continue;
3885 			if (cfg->fc_flags & RTF_GATEWAY &&
3886 			    !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
3887 				continue;
3888 			if (!fib6_info_hold_safe(rt))
3889 				continue;
3890 			rcu_read_unlock();
3891 
3892 			/* if gateway was specified only delete the one hop */
3893 			if (cfg->fc_flags & RTF_GATEWAY)
3894 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
3895 
3896 			return __ip6_del_rt_siblings(rt, cfg);
3897 		}
3898 	}
3899 	rcu_read_unlock();
3900 
3901 	return err;
3902 }
3903 
3904 static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3905 {
3906 	struct netevent_redirect netevent;
3907 	struct rt6_info *rt, *nrt = NULL;
3908 	struct fib6_result res = {};
3909 	struct ndisc_options ndopts;
3910 	struct inet6_dev *in6_dev;
3911 	struct neighbour *neigh;
3912 	struct rd_msg *msg;
3913 	int optlen, on_link;
3914 	u8 *lladdr;
3915 
3916 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
3917 	optlen -= sizeof(*msg);
3918 
3919 	if (optlen < 0) {
3920 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3921 		return;
3922 	}
3923 
3924 	msg = (struct rd_msg *)icmp6_hdr(skb);
3925 
3926 	if (ipv6_addr_is_multicast(&msg->dest)) {
3927 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3928 		return;
3929 	}
3930 
3931 	on_link = 0;
3932 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
3933 		on_link = 1;
3934 	} else if (ipv6_addr_type(&msg->target) !=
3935 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
3936 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
3937 		return;
3938 	}
3939 
3940 	in6_dev = __in6_dev_get(skb->dev);
3941 	if (!in6_dev)
3942 		return;
3943 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3944 		return;
3945 
3946 	/* RFC2461 8.1:
3947 	 *	The IP source address of the Redirect MUST be the same as the current
3948 	 *	first-hop router for the specified ICMP Destination Address.
3949 	 */
3950 
3951 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
3952 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3953 		return;
3954 	}
3955 
3956 	lladdr = NULL;
3957 	if (ndopts.nd_opts_tgt_lladdr) {
3958 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3959 					     skb->dev);
3960 		if (!lladdr) {
3961 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3962 			return;
3963 		}
3964 	}
3965 
3966 	rt = (struct rt6_info *) dst;
3967 	if (rt->rt6i_flags & RTF_REJECT) {
3968 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
3969 		return;
3970 	}
3971 
3972 	/* Redirect received -> path was valid.
3973 	 * Look, redirects are sent only in response to data packets,
3974 	 * so that this nexthop apparently is reachable. --ANK
3975 	 */
3976 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
3977 
3978 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
3979 	if (!neigh)
3980 		return;
3981 
3982 	/*
3983 	 *	We have finally decided to accept it.
3984 	 */
3985 
3986 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
3987 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
3988 		     NEIGH_UPDATE_F_OVERRIDE|
3989 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
3990 				     NEIGH_UPDATE_F_ISROUTER)),
3991 		     NDISC_REDIRECT, &ndopts);
3992 
3993 	rcu_read_lock();
3994 	res.f6i = rcu_dereference(rt->from);
3995 	if (!res.f6i)
3996 		goto out;
3997 
3998 	if (res.f6i->nh) {
3999 		struct fib6_nh_match_arg arg = {
4000 			.dev = dst->dev,
4001 			.gw = &rt->rt6i_gateway,
4002 		};
4003 
4004 		nexthop_for_each_fib6_nh(res.f6i->nh,
4005 					 fib6_nh_find_match, &arg);
4006 
4007 		/* fib6_info uses a nexthop that does not have fib6_nh
4008 		 * using the dst->dev. Should be impossible
4009 		 */
4010 		if (!arg.match)
4011 			goto out;
4012 		res.nh = arg.match;
4013 	} else {
4014 		res.nh = res.f6i->fib6_nh;
4015 	}
4016 
4017 	res.fib6_flags = res.f6i->fib6_flags;
4018 	res.fib6_type = res.f6i->fib6_type;
4019 	nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
4020 	if (!nrt)
4021 		goto out;
4022 
4023 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
4024 	if (on_link)
4025 		nrt->rt6i_flags &= ~RTF_GATEWAY;
4026 
4027 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
4028 
4029 	/* rt6_insert_exception() will take care of duplicated exceptions */
4030 	if (rt6_insert_exception(nrt, &res)) {
4031 		dst_release_immediate(&nrt->dst);
4032 		goto out;
4033 	}
4034 
4035 	netevent.old = &rt->dst;
4036 	netevent.new = &nrt->dst;
4037 	netevent.daddr = &msg->dest;
4038 	netevent.neigh = neigh;
4039 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
4040 
4041 out:
4042 	rcu_read_unlock();
4043 	neigh_release(neigh);
4044 }
4045 
4046 #ifdef CONFIG_IPV6_ROUTE_INFO
4047 static struct fib6_info *rt6_get_route_info(struct net *net,
4048 					   const struct in6_addr *prefix, int prefixlen,
4049 					   const struct in6_addr *gwaddr,
4050 					   struct net_device *dev)
4051 {
4052 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4053 	int ifindex = dev->ifindex;
4054 	struct fib6_node *fn;
4055 	struct fib6_info *rt = NULL;
4056 	struct fib6_table *table;
4057 
4058 	table = fib6_get_table(net, tb_id);
4059 	if (!table)
4060 		return NULL;
4061 
4062 	rcu_read_lock();
4063 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
4064 	if (!fn)
4065 		goto out;
4066 
4067 	for_each_fib6_node_rt_rcu(fn) {
4068 		/* these routes do not use nexthops */
4069 		if (rt->nh)
4070 			continue;
4071 		if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
4072 			continue;
4073 		if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
4074 		    !rt->fib6_nh->fib_nh_gw_family)
4075 			continue;
4076 		if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
4077 			continue;
4078 		if (!fib6_info_hold_safe(rt))
4079 			continue;
4080 		break;
4081 	}
4082 out:
4083 	rcu_read_unlock();
4084 	return rt;
4085 }
4086 
4087 static struct fib6_info *rt6_add_route_info(struct net *net,
4088 					   const struct in6_addr *prefix, int prefixlen,
4089 					   const struct in6_addr *gwaddr,
4090 					   struct net_device *dev,
4091 					   unsigned int pref)
4092 {
4093 	struct fib6_config cfg = {
4094 		.fc_metric	= IP6_RT_PRIO_USER,
4095 		.fc_ifindex	= dev->ifindex,
4096 		.fc_dst_len	= prefixlen,
4097 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
4098 				  RTF_UP | RTF_PREF(pref),
4099 		.fc_protocol = RTPROT_RA,
4100 		.fc_type = RTN_UNICAST,
4101 		.fc_nlinfo.portid = 0,
4102 		.fc_nlinfo.nlh = NULL,
4103 		.fc_nlinfo.nl_net = net,
4104 	};
4105 
4106 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
4107 	cfg.fc_dst = *prefix;
4108 	cfg.fc_gateway = *gwaddr;
4109 
4110 	/* We should treat it as a default route if prefix length is 0. */
4111 	if (!prefixlen)
4112 		cfg.fc_flags |= RTF_DEFAULT;
4113 
4114 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
4115 
4116 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
4117 }
4118 #endif
4119 
4120 struct fib6_info *rt6_get_dflt_router(struct net *net,
4121 				     const struct in6_addr *addr,
4122 				     struct net_device *dev)
4123 {
4124 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
4125 	struct fib6_info *rt;
4126 	struct fib6_table *table;
4127 
4128 	table = fib6_get_table(net, tb_id);
4129 	if (!table)
4130 		return NULL;
4131 
4132 	rcu_read_lock();
4133 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
4134 		struct fib6_nh *nh;
4135 
4136 		/* RA routes do not use nexthops */
4137 		if (rt->nh)
4138 			continue;
4139 
4140 		nh = rt->fib6_nh;
4141 		if (dev == nh->fib_nh_dev &&
4142 		    ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
4143 		    ipv6_addr_equal(&nh->fib_nh_gw6, addr))
4144 			break;
4145 	}
4146 	if (rt && !fib6_info_hold_safe(rt))
4147 		rt = NULL;
4148 	rcu_read_unlock();
4149 	return rt;
4150 }
4151 
4152 struct fib6_info *rt6_add_dflt_router(struct net *net,
4153 				     const struct in6_addr *gwaddr,
4154 				     struct net_device *dev,
4155 				     unsigned int pref)
4156 {
4157 	struct fib6_config cfg = {
4158 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
4159 		.fc_metric	= IP6_RT_PRIO_USER,
4160 		.fc_ifindex	= dev->ifindex,
4161 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
4162 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
4163 		.fc_protocol = RTPROT_RA,
4164 		.fc_type = RTN_UNICAST,
4165 		.fc_nlinfo.portid = 0,
4166 		.fc_nlinfo.nlh = NULL,
4167 		.fc_nlinfo.nl_net = net,
4168 	};
4169 
4170 	cfg.fc_gateway = *gwaddr;
4171 
4172 	if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
4173 		struct fib6_table *table;
4174 
4175 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
4176 		if (table)
4177 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4178 	}
4179 
4180 	return rt6_get_dflt_router(net, gwaddr, dev);
4181 }
4182 
4183 static void __rt6_purge_dflt_routers(struct net *net,
4184 				     struct fib6_table *table)
4185 {
4186 	struct fib6_info *rt;
4187 
4188 restart:
4189 	rcu_read_lock();
4190 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
4191 		struct net_device *dev = fib6_info_nh_dev(rt);
4192 		struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4193 
4194 		if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
4195 		    (!idev || idev->cnf.accept_ra != 2) &&
4196 		    fib6_info_hold_safe(rt)) {
4197 			rcu_read_unlock();
4198 			ip6_del_rt(net, rt);
4199 			goto restart;
4200 		}
4201 	}
4202 	rcu_read_unlock();
4203 
4204 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4205 }
4206 
4207 void rt6_purge_dflt_routers(struct net *net)
4208 {
4209 	struct fib6_table *table;
4210 	struct hlist_head *head;
4211 	unsigned int h;
4212 
4213 	rcu_read_lock();
4214 
4215 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4216 		head = &net->ipv6.fib_table_hash[h];
4217 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4218 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
4219 				__rt6_purge_dflt_routers(net, table);
4220 		}
4221 	}
4222 
4223 	rcu_read_unlock();
4224 }
4225 
4226 static void rtmsg_to_fib6_config(struct net *net,
4227 				 struct in6_rtmsg *rtmsg,
4228 				 struct fib6_config *cfg)
4229 {
4230 	*cfg = (struct fib6_config){
4231 		.fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
4232 			 : RT6_TABLE_MAIN,
4233 		.fc_ifindex = rtmsg->rtmsg_ifindex,
4234 		.fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
4235 		.fc_expires = rtmsg->rtmsg_info,
4236 		.fc_dst_len = rtmsg->rtmsg_dst_len,
4237 		.fc_src_len = rtmsg->rtmsg_src_len,
4238 		.fc_flags = rtmsg->rtmsg_flags,
4239 		.fc_type = rtmsg->rtmsg_type,
4240 
4241 		.fc_nlinfo.nl_net = net,
4242 
4243 		.fc_dst = rtmsg->rtmsg_dst,
4244 		.fc_src = rtmsg->rtmsg_src,
4245 		.fc_gateway = rtmsg->rtmsg_gateway,
4246 	};
4247 }
4248 
4249 int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
4250 {
4251 	struct fib6_config cfg;
4252 	struct in6_rtmsg rtmsg;
4253 	int err;
4254 
4255 	switch (cmd) {
4256 	case SIOCADDRT:		/* Add a route */
4257 	case SIOCDELRT:		/* Delete a route */
4258 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
4259 			return -EPERM;
4260 		err = copy_from_user(&rtmsg, arg,
4261 				     sizeof(struct in6_rtmsg));
4262 		if (err)
4263 			return -EFAULT;
4264 
4265 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
4266 
4267 		rtnl_lock();
4268 		switch (cmd) {
4269 		case SIOCADDRT:
4270 			err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
4271 			break;
4272 		case SIOCDELRT:
4273 			err = ip6_route_del(&cfg, NULL);
4274 			break;
4275 		default:
4276 			err = -EINVAL;
4277 		}
4278 		rtnl_unlock();
4279 
4280 		return err;
4281 	}
4282 
4283 	return -EINVAL;
4284 }
4285 
4286 /*
4287  *	Drop the packet on the floor
4288  */
4289 
4290 static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
4291 {
4292 	struct dst_entry *dst = skb_dst(skb);
4293 	struct net *net = dev_net(dst->dev);
4294 	struct inet6_dev *idev;
4295 	int type;
4296 
4297 	if (netif_is_l3_master(skb->dev) &&
4298 	    dst->dev == net->loopback_dev)
4299 		idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
4300 	else
4301 		idev = ip6_dst_idev(dst);
4302 
4303 	switch (ipstats_mib_noroutes) {
4304 	case IPSTATS_MIB_INNOROUTES:
4305 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
4306 		if (type == IPV6_ADDR_ANY) {
4307 			IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
4308 			break;
4309 		}
4310 		/* FALLTHROUGH */
4311 	case IPSTATS_MIB_OUTNOROUTES:
4312 		IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
4313 		break;
4314 	}
4315 
4316 	/* Start over by dropping the dst for l3mdev case */
4317 	if (netif_is_l3_master(skb->dev))
4318 		skb_dst_drop(skb);
4319 
4320 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
4321 	kfree_skb(skb);
4322 	return 0;
4323 }
4324 
4325 static int ip6_pkt_discard(struct sk_buff *skb)
4326 {
4327 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
4328 }
4329 
4330 static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4331 {
4332 	skb->dev = skb_dst(skb)->dev;
4333 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
4334 }
4335 
4336 static int ip6_pkt_prohibit(struct sk_buff *skb)
4337 {
4338 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
4339 }
4340 
4341 static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
4342 {
4343 	skb->dev = skb_dst(skb)->dev;
4344 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
4345 }
4346 
4347 /*
4348  *	Allocate a dst for local (unicast / anycast) address.
4349  */
4350 
4351 struct fib6_info *addrconf_f6i_alloc(struct net *net,
4352 				     struct inet6_dev *idev,
4353 				     const struct in6_addr *addr,
4354 				     bool anycast, gfp_t gfp_flags)
4355 {
4356 	struct fib6_config cfg = {
4357 		.fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4358 		.fc_ifindex = idev->dev->ifindex,
4359 		.fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP,
4360 		.fc_dst = *addr,
4361 		.fc_dst_len = 128,
4362 		.fc_protocol = RTPROT_KERNEL,
4363 		.fc_nlinfo.nl_net = net,
4364 		.fc_ignore_dev_down = true,
4365 	};
4366 
4367 	if (anycast) {
4368 		cfg.fc_type = RTN_ANYCAST;
4369 		cfg.fc_flags |= RTF_ANYCAST;
4370 	} else {
4371 		cfg.fc_type = RTN_LOCAL;
4372 		cfg.fc_flags |= RTF_LOCAL;
4373 	}
4374 
4375 	return ip6_route_info_create(&cfg, gfp_flags, NULL);
4376 }
4377 
4378 /* remove deleted ip from prefsrc entries */
4379 struct arg_dev_net_ip {
4380 	struct net_device *dev;
4381 	struct net *net;
4382 	struct in6_addr *addr;
4383 };
4384 
4385 static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
4386 {
4387 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4388 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4389 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4390 
4391 	if (!rt->nh &&
4392 	    ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
4393 	    rt != net->ipv6.fib6_null_entry &&
4394 	    ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
4395 		spin_lock_bh(&rt6_exception_lock);
4396 		/* remove prefsrc entry */
4397 		rt->fib6_prefsrc.plen = 0;
4398 		spin_unlock_bh(&rt6_exception_lock);
4399 	}
4400 	return 0;
4401 }
4402 
4403 void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4404 {
4405 	struct net *net = dev_net(ifp->idev->dev);
4406 	struct arg_dev_net_ip adni = {
4407 		.dev = ifp->idev->dev,
4408 		.net = net,
4409 		.addr = &ifp->addr,
4410 	};
4411 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4412 }
4413 
4414 #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT)
4415 
4416 /* Remove routers and update dst entries when gateway turn into host. */
4417 static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4418 {
4419 	struct in6_addr *gateway = (struct in6_addr *)arg;
4420 	struct fib6_nh *nh;
4421 
4422 	/* RA routes do not use nexthops */
4423 	if (rt->nh)
4424 		return 0;
4425 
4426 	nh = rt->fib6_nh;
4427 	if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
4428 	    nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
4429 		return -1;
4430 
4431 	/* Further clean up cached routes in exception table.
4432 	 * This is needed because cached route may have a different
4433 	 * gateway than its 'parent' in the case of an ip redirect.
4434 	 */
4435 	fib6_nh_exceptions_clean_tohost(nh, gateway);
4436 
4437 	return 0;
4438 }
4439 
4440 void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4441 {
4442 	fib6_clean_all(net, fib6_clean_tohost, gateway);
4443 }
4444 
4445 struct arg_netdev_event {
4446 	const struct net_device *dev;
4447 	union {
4448 		unsigned char nh_flags;
4449 		unsigned long event;
4450 	};
4451 };
4452 
4453 static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4454 {
4455 	struct fib6_info *iter;
4456 	struct fib6_node *fn;
4457 
4458 	fn = rcu_dereference_protected(rt->fib6_node,
4459 			lockdep_is_held(&rt->fib6_table->tb6_lock));
4460 	iter = rcu_dereference_protected(fn->leaf,
4461 			lockdep_is_held(&rt->fib6_table->tb6_lock));
4462 	while (iter) {
4463 		if (iter->fib6_metric == rt->fib6_metric &&
4464 		    rt6_qualify_for_ecmp(iter))
4465 			return iter;
4466 		iter = rcu_dereference_protected(iter->fib6_next,
4467 				lockdep_is_held(&rt->fib6_table->tb6_lock));
4468 	}
4469 
4470 	return NULL;
4471 }
4472 
4473 /* only called for fib entries with builtin fib6_nh */
4474 static bool rt6_is_dead(const struct fib6_info *rt)
4475 {
4476 	if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
4477 	    (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
4478 	     ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
4479 		return true;
4480 
4481 	return false;
4482 }
4483 
4484 static int rt6_multipath_total_weight(const struct fib6_info *rt)
4485 {
4486 	struct fib6_info *iter;
4487 	int total = 0;
4488 
4489 	if (!rt6_is_dead(rt))
4490 		total += rt->fib6_nh->fib_nh_weight;
4491 
4492 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4493 		if (!rt6_is_dead(iter))
4494 			total += iter->fib6_nh->fib_nh_weight;
4495 	}
4496 
4497 	return total;
4498 }
4499 
4500 static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4501 {
4502 	int upper_bound = -1;
4503 
4504 	if (!rt6_is_dead(rt)) {
4505 		*weight += rt->fib6_nh->fib_nh_weight;
4506 		upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4507 						    total) - 1;
4508 	}
4509 	atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
4510 }
4511 
4512 static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4513 {
4514 	struct fib6_info *iter;
4515 	int weight = 0;
4516 
4517 	rt6_upper_bound_set(rt, &weight, total);
4518 
4519 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4520 		rt6_upper_bound_set(iter, &weight, total);
4521 }
4522 
4523 void rt6_multipath_rebalance(struct fib6_info *rt)
4524 {
4525 	struct fib6_info *first;
4526 	int total;
4527 
4528 	/* In case the entire multipath route was marked for flushing,
4529 	 * then there is no need to rebalance upon the removal of every
4530 	 * sibling route.
4531 	 */
4532 	if (!rt->fib6_nsiblings || rt->should_flush)
4533 		return;
4534 
4535 	/* During lookup routes are evaluated in order, so we need to
4536 	 * make sure upper bounds are assigned from the first sibling
4537 	 * onwards.
4538 	 */
4539 	first = rt6_multipath_first_sibling(rt);
4540 	if (WARN_ON_ONCE(!first))
4541 		return;
4542 
4543 	total = rt6_multipath_total_weight(first);
4544 	rt6_multipath_upper_bound_set(first, total);
4545 }
4546 
4547 static int fib6_ifup(struct fib6_info *rt, void *p_arg)
4548 {
4549 	const struct arg_netdev_event *arg = p_arg;
4550 	struct net *net = dev_net(arg->dev);
4551 
4552 	if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
4553 	    rt->fib6_nh->fib_nh_dev == arg->dev) {
4554 		rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
4555 		fib6_update_sernum_upto_root(net, rt);
4556 		rt6_multipath_rebalance(rt);
4557 	}
4558 
4559 	return 0;
4560 }
4561 
4562 void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
4563 {
4564 	struct arg_netdev_event arg = {
4565 		.dev = dev,
4566 		{
4567 			.nh_flags = nh_flags,
4568 		},
4569 	};
4570 
4571 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
4572 		arg.nh_flags |= RTNH_F_LINKDOWN;
4573 
4574 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
4575 }
4576 
4577 /* only called for fib entries with inline fib6_nh */
4578 static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
4579 				   const struct net_device *dev)
4580 {
4581 	struct fib6_info *iter;
4582 
4583 	if (rt->fib6_nh->fib_nh_dev == dev)
4584 		return true;
4585 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4586 		if (iter->fib6_nh->fib_nh_dev == dev)
4587 			return true;
4588 
4589 	return false;
4590 }
4591 
4592 static void rt6_multipath_flush(struct fib6_info *rt)
4593 {
4594 	struct fib6_info *iter;
4595 
4596 	rt->should_flush = 1;
4597 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4598 		iter->should_flush = 1;
4599 }
4600 
4601 static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
4602 					     const struct net_device *down_dev)
4603 {
4604 	struct fib6_info *iter;
4605 	unsigned int dead = 0;
4606 
4607 	if (rt->fib6_nh->fib_nh_dev == down_dev ||
4608 	    rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4609 		dead++;
4610 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4611 		if (iter->fib6_nh->fib_nh_dev == down_dev ||
4612 		    iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
4613 			dead++;
4614 
4615 	return dead;
4616 }
4617 
4618 static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
4619 				       const struct net_device *dev,
4620 				       unsigned char nh_flags)
4621 {
4622 	struct fib6_info *iter;
4623 
4624 	if (rt->fib6_nh->fib_nh_dev == dev)
4625 		rt->fib6_nh->fib_nh_flags |= nh_flags;
4626 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4627 		if (iter->fib6_nh->fib_nh_dev == dev)
4628 			iter->fib6_nh->fib_nh_flags |= nh_flags;
4629 }
4630 
4631 /* called with write lock held for table with rt */
4632 static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
4633 {
4634 	const struct arg_netdev_event *arg = p_arg;
4635 	const struct net_device *dev = arg->dev;
4636 	struct net *net = dev_net(dev);
4637 
4638 	if (rt == net->ipv6.fib6_null_entry || rt->nh)
4639 		return 0;
4640 
4641 	switch (arg->event) {
4642 	case NETDEV_UNREGISTER:
4643 		return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4644 	case NETDEV_DOWN:
4645 		if (rt->should_flush)
4646 			return -1;
4647 		if (!rt->fib6_nsiblings)
4648 			return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
4649 		if (rt6_multipath_uses_dev(rt, dev)) {
4650 			unsigned int count;
4651 
4652 			count = rt6_multipath_dead_count(rt, dev);
4653 			if (rt->fib6_nsiblings + 1 == count) {
4654 				rt6_multipath_flush(rt);
4655 				return -1;
4656 			}
4657 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
4658 						   RTNH_F_LINKDOWN);
4659 			fib6_update_sernum(net, rt);
4660 			rt6_multipath_rebalance(rt);
4661 		}
4662 		return -2;
4663 	case NETDEV_CHANGE:
4664 		if (rt->fib6_nh->fib_nh_dev != dev ||
4665 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
4666 			break;
4667 		rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
4668 		rt6_multipath_rebalance(rt);
4669 		break;
4670 	}
4671 
4672 	return 0;
4673 }
4674 
4675 void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
4676 {
4677 	struct arg_netdev_event arg = {
4678 		.dev = dev,
4679 		{
4680 			.event = event,
4681 		},
4682 	};
4683 	struct net *net = dev_net(dev);
4684 
4685 	if (net->ipv6.sysctl.skip_notify_on_dev_down)
4686 		fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4687 	else
4688 		fib6_clean_all(net, fib6_ifdown, &arg);
4689 }
4690 
4691 void rt6_disable_ip(struct net_device *dev, unsigned long event)
4692 {
4693 	rt6_sync_down_dev(dev, event);
4694 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
4695 	neigh_ifdown(&nd_tbl, dev);
4696 }
4697 
4698 struct rt6_mtu_change_arg {
4699 	struct net_device *dev;
4700 	unsigned int mtu;
4701 	struct fib6_info *f6i;
4702 };
4703 
4704 static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
4705 {
4706 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
4707 	struct fib6_info *f6i = arg->f6i;
4708 
4709 	/* For administrative MTU increase, there is no way to discover
4710 	 * IPv6 PMTU increase, so PMTU increase should be updated here.
4711 	 * Since RFC 1981 doesn't include administrative MTU increase
4712 	 * update PMTU increase is a MUST. (i.e. jumbo frame)
4713 	 */
4714 	if (nh->fib_nh_dev == arg->dev) {
4715 		struct inet6_dev *idev = __in6_dev_get(arg->dev);
4716 		u32 mtu = f6i->fib6_pmtu;
4717 
4718 		if (mtu >= arg->mtu ||
4719 		    (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4720 			fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4721 
4722 		spin_lock_bh(&rt6_exception_lock);
4723 		rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
4724 		spin_unlock_bh(&rt6_exception_lock);
4725 	}
4726 
4727 	return 0;
4728 }
4729 
4730 static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
4731 {
4732 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
4733 	struct inet6_dev *idev;
4734 
4735 	/* In IPv6 pmtu discovery is not optional,
4736 	   so that RTAX_MTU lock cannot disable it.
4737 	   We still use this lock to block changes
4738 	   caused by addrconf/ndisc.
4739 	*/
4740 
4741 	idev = __in6_dev_get(arg->dev);
4742 	if (!idev)
4743 		return 0;
4744 
4745 	if (fib6_metric_locked(f6i, RTAX_MTU))
4746 		return 0;
4747 
4748 	arg->f6i = f6i;
4749 	if (f6i->nh) {
4750 		/* fib6_nh_mtu_change only returns 0, so this is safe */
4751 		return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
4752 						arg);
4753 	}
4754 
4755 	return fib6_nh_mtu_change(f6i->fib6_nh, arg);
4756 }
4757 
4758 void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
4759 {
4760 	struct rt6_mtu_change_arg arg = {
4761 		.dev = dev,
4762 		.mtu = mtu,
4763 	};
4764 
4765 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
4766 }
4767 
4768 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
4769 	[RTA_UNSPEC]		= { .strict_start_type = RTA_DPORT + 1 },
4770 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
4771 	[RTA_PREFSRC]		= { .len = sizeof(struct in6_addr) },
4772 	[RTA_OIF]               = { .type = NLA_U32 },
4773 	[RTA_IIF]		= { .type = NLA_U32 },
4774 	[RTA_PRIORITY]          = { .type = NLA_U32 },
4775 	[RTA_METRICS]           = { .type = NLA_NESTED },
4776 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
4777 	[RTA_PREF]              = { .type = NLA_U8 },
4778 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
4779 	[RTA_ENCAP]		= { .type = NLA_NESTED },
4780 	[RTA_EXPIRES]		= { .type = NLA_U32 },
4781 	[RTA_UID]		= { .type = NLA_U32 },
4782 	[RTA_MARK]		= { .type = NLA_U32 },
4783 	[RTA_TABLE]		= { .type = NLA_U32 },
4784 	[RTA_IP_PROTO]		= { .type = NLA_U8 },
4785 	[RTA_SPORT]		= { .type = NLA_U16 },
4786 	[RTA_DPORT]		= { .type = NLA_U16 },
4787 	[RTA_NH_ID]		= { .type = NLA_U32 },
4788 };
4789 
4790 static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4791 			      struct fib6_config *cfg,
4792 			      struct netlink_ext_ack *extack)
4793 {
4794 	struct rtmsg *rtm;
4795 	struct nlattr *tb[RTA_MAX+1];
4796 	unsigned int pref;
4797 	int err;
4798 
4799 	err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
4800 				     rtm_ipv6_policy, extack);
4801 	if (err < 0)
4802 		goto errout;
4803 
4804 	err = -EINVAL;
4805 	rtm = nlmsg_data(nlh);
4806 
4807 	*cfg = (struct fib6_config){
4808 		.fc_table = rtm->rtm_table,
4809 		.fc_dst_len = rtm->rtm_dst_len,
4810 		.fc_src_len = rtm->rtm_src_len,
4811 		.fc_flags = RTF_UP,
4812 		.fc_protocol = rtm->rtm_protocol,
4813 		.fc_type = rtm->rtm_type,
4814 
4815 		.fc_nlinfo.portid = NETLINK_CB(skb).portid,
4816 		.fc_nlinfo.nlh = nlh,
4817 		.fc_nlinfo.nl_net = sock_net(skb->sk),
4818 	};
4819 
4820 	if (rtm->rtm_type == RTN_UNREACHABLE ||
4821 	    rtm->rtm_type == RTN_BLACKHOLE ||
4822 	    rtm->rtm_type == RTN_PROHIBIT ||
4823 	    rtm->rtm_type == RTN_THROW)
4824 		cfg->fc_flags |= RTF_REJECT;
4825 
4826 	if (rtm->rtm_type == RTN_LOCAL)
4827 		cfg->fc_flags |= RTF_LOCAL;
4828 
4829 	if (rtm->rtm_flags & RTM_F_CLONED)
4830 		cfg->fc_flags |= RTF_CACHE;
4831 
4832 	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4833 
4834 	if (tb[RTA_NH_ID]) {
4835 		if (tb[RTA_GATEWAY]   || tb[RTA_OIF] ||
4836 		    tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
4837 			NL_SET_ERR_MSG(extack,
4838 				       "Nexthop specification and nexthop id are mutually exclusive");
4839 			goto errout;
4840 		}
4841 		cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
4842 	}
4843 
4844 	if (tb[RTA_GATEWAY]) {
4845 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
4846 		cfg->fc_flags |= RTF_GATEWAY;
4847 	}
4848 	if (tb[RTA_VIA]) {
4849 		NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4850 		goto errout;
4851 	}
4852 
4853 	if (tb[RTA_DST]) {
4854 		int plen = (rtm->rtm_dst_len + 7) >> 3;
4855 
4856 		if (nla_len(tb[RTA_DST]) < plen)
4857 			goto errout;
4858 
4859 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
4860 	}
4861 
4862 	if (tb[RTA_SRC]) {
4863 		int plen = (rtm->rtm_src_len + 7) >> 3;
4864 
4865 		if (nla_len(tb[RTA_SRC]) < plen)
4866 			goto errout;
4867 
4868 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
4869 	}
4870 
4871 	if (tb[RTA_PREFSRC])
4872 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4873 
4874 	if (tb[RTA_OIF])
4875 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
4876 
4877 	if (tb[RTA_PRIORITY])
4878 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
4879 
4880 	if (tb[RTA_METRICS]) {
4881 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
4882 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
4883 	}
4884 
4885 	if (tb[RTA_TABLE])
4886 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
4887 
4888 	if (tb[RTA_MULTIPATH]) {
4889 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
4890 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
4891 
4892 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4893 						     cfg->fc_mp_len, extack);
4894 		if (err < 0)
4895 			goto errout;
4896 	}
4897 
4898 	if (tb[RTA_PREF]) {
4899 		pref = nla_get_u8(tb[RTA_PREF]);
4900 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
4901 		    pref != ICMPV6_ROUTER_PREF_HIGH)
4902 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
4903 		cfg->fc_flags |= RTF_PREF(pref);
4904 	}
4905 
4906 	if (tb[RTA_ENCAP])
4907 		cfg->fc_encap = tb[RTA_ENCAP];
4908 
4909 	if (tb[RTA_ENCAP_TYPE]) {
4910 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
4911 
4912 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
4913 		if (err < 0)
4914 			goto errout;
4915 	}
4916 
4917 	if (tb[RTA_EXPIRES]) {
4918 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
4919 
4920 		if (addrconf_finite_timeout(timeout)) {
4921 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
4922 			cfg->fc_flags |= RTF_EXPIRES;
4923 		}
4924 	}
4925 
4926 	err = 0;
4927 errout:
4928 	return err;
4929 }
4930 
4931 struct rt6_nh {
4932 	struct fib6_info *fib6_info;
4933 	struct fib6_config r_cfg;
4934 	struct list_head next;
4935 };
4936 
4937 static int ip6_route_info_append(struct net *net,
4938 				 struct list_head *rt6_nh_list,
4939 				 struct fib6_info *rt,
4940 				 struct fib6_config *r_cfg)
4941 {
4942 	struct rt6_nh *nh;
4943 	int err = -EEXIST;
4944 
4945 	list_for_each_entry(nh, rt6_nh_list, next) {
4946 		/* check if fib6_info already exists */
4947 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
4948 			return err;
4949 	}
4950 
4951 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
4952 	if (!nh)
4953 		return -ENOMEM;
4954 	nh->fib6_info = rt;
4955 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
4956 	list_add_tail(&nh->next, rt6_nh_list);
4957 
4958 	return 0;
4959 }
4960 
4961 static void ip6_route_mpath_notify(struct fib6_info *rt,
4962 				   struct fib6_info *rt_last,
4963 				   struct nl_info *info,
4964 				   __u16 nlflags)
4965 {
4966 	/* if this is an APPEND route, then rt points to the first route
4967 	 * inserted and rt_last points to last route inserted. Userspace
4968 	 * wants a consistent dump of the route which starts at the first
4969 	 * nexthop. Since sibling routes are always added at the end of
4970 	 * the list, find the first sibling of the last route appended
4971 	 */
4972 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
4973 		rt = list_first_entry(&rt_last->fib6_siblings,
4974 				      struct fib6_info,
4975 				      fib6_siblings);
4976 	}
4977 
4978 	if (rt)
4979 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
4980 }
4981 
4982 static int ip6_route_multipath_add(struct fib6_config *cfg,
4983 				   struct netlink_ext_ack *extack)
4984 {
4985 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
4986 	struct nl_info *info = &cfg->fc_nlinfo;
4987 	enum fib_event_type event_type;
4988 	struct fib6_config r_cfg;
4989 	struct rtnexthop *rtnh;
4990 	struct fib6_info *rt;
4991 	struct rt6_nh *err_nh;
4992 	struct rt6_nh *nh, *nh_safe;
4993 	__u16 nlflags;
4994 	int remaining;
4995 	int attrlen;
4996 	int err = 1;
4997 	int nhn = 0;
4998 	int replace = (cfg->fc_nlinfo.nlh &&
4999 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5000 	LIST_HEAD(rt6_nh_list);
5001 
5002 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
5003 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
5004 		nlflags |= NLM_F_APPEND;
5005 
5006 	remaining = cfg->fc_mp_len;
5007 	rtnh = (struct rtnexthop *)cfg->fc_mp;
5008 
5009 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
5010 	 * fib6_info structs per nexthop
5011 	 */
5012 	while (rtnh_ok(rtnh, remaining)) {
5013 		memcpy(&r_cfg, cfg, sizeof(*cfg));
5014 		if (rtnh->rtnh_ifindex)
5015 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5016 
5017 		attrlen = rtnh_attrlen(rtnh);
5018 		if (attrlen > 0) {
5019 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5020 
5021 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5022 			if (nla) {
5023 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
5024 				r_cfg.fc_flags |= RTF_GATEWAY;
5025 			}
5026 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5027 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5028 			if (nla)
5029 				r_cfg.fc_encap_type = nla_get_u16(nla);
5030 		}
5031 
5032 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5033 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
5034 		if (IS_ERR(rt)) {
5035 			err = PTR_ERR(rt);
5036 			rt = NULL;
5037 			goto cleanup;
5038 		}
5039 		if (!rt6_qualify_for_ecmp(rt)) {
5040 			err = -EINVAL;
5041 			NL_SET_ERR_MSG(extack,
5042 				       "Device only routes can not be added for IPv6 using the multipath API.");
5043 			fib6_info_release(rt);
5044 			goto cleanup;
5045 		}
5046 
5047 		rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
5048 
5049 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5050 					    rt, &r_cfg);
5051 		if (err) {
5052 			fib6_info_release(rt);
5053 			goto cleanup;
5054 		}
5055 
5056 		rtnh = rtnh_next(rtnh, &remaining);
5057 	}
5058 
5059 	if (list_empty(&rt6_nh_list)) {
5060 		NL_SET_ERR_MSG(extack,
5061 			       "Invalid nexthop configuration - no valid nexthops");
5062 		return -EINVAL;
5063 	}
5064 
5065 	/* for add and replace send one notification with all nexthops.
5066 	 * Skip the notification in fib6_add_rt2node and send one with
5067 	 * the full route when done
5068 	 */
5069 	info->skip_notify = 1;
5070 
5071 	/* For add and replace, send one notification with all nexthops. For
5072 	 * append, send one notification with all appended nexthops.
5073 	 */
5074 	info->skip_notify_kernel = 1;
5075 
5076 	err_nh = NULL;
5077 	list_for_each_entry(nh, &rt6_nh_list, next) {
5078 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
5079 		fib6_info_release(nh->fib6_info);
5080 
5081 		if (!err) {
5082 			/* save reference to last route successfully inserted */
5083 			rt_last = nh->fib6_info;
5084 
5085 			/* save reference to first route for notification */
5086 			if (!rt_notif)
5087 				rt_notif = nh->fib6_info;
5088 		}
5089 
5090 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
5091 		nh->fib6_info = NULL;
5092 		if (err) {
5093 			if (replace && nhn)
5094 				NL_SET_ERR_MSG_MOD(extack,
5095 						   "multipath route replace failed (check consistency of installed routes)");
5096 			err_nh = nh;
5097 			goto add_errout;
5098 		}
5099 
5100 		/* Because each route is added like a single route we remove
5101 		 * these flags after the first nexthop: if there is a collision,
5102 		 * we have already failed to add the first nexthop:
5103 		 * fib6_add_rt2node() has rejected it; when replacing, old
5104 		 * nexthops have been replaced by first new, the rest should
5105 		 * be added to it.
5106 		 */
5107 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
5108 						     NLM_F_REPLACE);
5109 		nhn++;
5110 	}
5111 
5112 	event_type = replace ? FIB_EVENT_ENTRY_REPLACE : FIB_EVENT_ENTRY_ADD;
5113 	err = call_fib6_multipath_entry_notifiers(info->nl_net, event_type,
5114 						  rt_notif, nhn - 1, extack);
5115 	if (err) {
5116 		/* Delete all the siblings that were just added */
5117 		err_nh = NULL;
5118 		goto add_errout;
5119 	}
5120 
5121 	/* success ... tell user about new route */
5122 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5123 	goto cleanup;
5124 
5125 add_errout:
5126 	/* send notification for routes that were added so that
5127 	 * the delete notifications sent by ip6_route_del are
5128 	 * coherent
5129 	 */
5130 	if (rt_notif)
5131 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
5132 
5133 	/* Delete routes that were already added */
5134 	list_for_each_entry(nh, &rt6_nh_list, next) {
5135 		if (err_nh == nh)
5136 			break;
5137 		ip6_route_del(&nh->r_cfg, extack);
5138 	}
5139 
5140 cleanup:
5141 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
5142 		if (nh->fib6_info)
5143 			fib6_info_release(nh->fib6_info);
5144 		list_del(&nh->next);
5145 		kfree(nh);
5146 	}
5147 
5148 	return err;
5149 }
5150 
5151 static int ip6_route_multipath_del(struct fib6_config *cfg,
5152 				   struct netlink_ext_ack *extack)
5153 {
5154 	struct fib6_config r_cfg;
5155 	struct rtnexthop *rtnh;
5156 	int remaining;
5157 	int attrlen;
5158 	int err = 1, last_err = 0;
5159 
5160 	remaining = cfg->fc_mp_len;
5161 	rtnh = (struct rtnexthop *)cfg->fc_mp;
5162 
5163 	/* Parse a Multipath Entry */
5164 	while (rtnh_ok(rtnh, remaining)) {
5165 		memcpy(&r_cfg, cfg, sizeof(*cfg));
5166 		if (rtnh->rtnh_ifindex)
5167 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5168 
5169 		attrlen = rtnh_attrlen(rtnh);
5170 		if (attrlen > 0) {
5171 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5172 
5173 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5174 			if (nla) {
5175 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
5176 				r_cfg.fc_flags |= RTF_GATEWAY;
5177 			}
5178 		}
5179 		err = ip6_route_del(&r_cfg, extack);
5180 		if (err)
5181 			last_err = err;
5182 
5183 		rtnh = rtnh_next(rtnh, &remaining);
5184 	}
5185 
5186 	return last_err;
5187 }
5188 
5189 static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5190 			      struct netlink_ext_ack *extack)
5191 {
5192 	struct fib6_config cfg;
5193 	int err;
5194 
5195 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5196 	if (err < 0)
5197 		return err;
5198 
5199 	if (cfg.fc_nh_id &&
5200 	    !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
5201 		NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
5202 		return -EINVAL;
5203 	}
5204 
5205 	if (cfg.fc_mp)
5206 		return ip6_route_multipath_del(&cfg, extack);
5207 	else {
5208 		cfg.fc_delete_all_nh = 1;
5209 		return ip6_route_del(&cfg, extack);
5210 	}
5211 }
5212 
5213 static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5214 			      struct netlink_ext_ack *extack)
5215 {
5216 	struct fib6_config cfg;
5217 	int err;
5218 
5219 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
5220 	if (err < 0)
5221 		return err;
5222 
5223 	if (cfg.fc_metric == 0)
5224 		cfg.fc_metric = IP6_RT_PRIO_USER;
5225 
5226 	if (cfg.fc_mp)
5227 		return ip6_route_multipath_add(&cfg, extack);
5228 	else
5229 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
5230 }
5231 
5232 /* add the overhead of this fib6_nh to nexthop_len */
5233 static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
5234 {
5235 	int *nexthop_len = arg;
5236 
5237 	*nexthop_len += nla_total_size(0)	 /* RTA_MULTIPATH */
5238 		     + NLA_ALIGN(sizeof(struct rtnexthop))
5239 		     + nla_total_size(16); /* RTA_GATEWAY */
5240 
5241 	if (nh->fib_nh_lws) {
5242 		/* RTA_ENCAP_TYPE */
5243 		*nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5244 		/* RTA_ENCAP */
5245 		*nexthop_len += nla_total_size(2);
5246 	}
5247 
5248 	return 0;
5249 }
5250 
5251 static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5252 {
5253 	int nexthop_len;
5254 
5255 	if (f6i->nh) {
5256 		nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5257 		nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5258 					 &nexthop_len);
5259 	} else {
5260 		struct fib6_nh *nh = f6i->fib6_nh;
5261 
5262 		nexthop_len = 0;
5263 		if (f6i->fib6_nsiblings) {
5264 			nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
5265 				    + NLA_ALIGN(sizeof(struct rtnexthop))
5266 				    + nla_total_size(16) /* RTA_GATEWAY */
5267 				    + lwtunnel_get_encap_size(nh->fib_nh_lws);
5268 
5269 			nexthop_len *= f6i->fib6_nsiblings;
5270 		}
5271 		nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5272 	}
5273 
5274 	return NLMSG_ALIGN(sizeof(struct rtmsg))
5275 	       + nla_total_size(16) /* RTA_SRC */
5276 	       + nla_total_size(16) /* RTA_DST */
5277 	       + nla_total_size(16) /* RTA_GATEWAY */
5278 	       + nla_total_size(16) /* RTA_PREFSRC */
5279 	       + nla_total_size(4) /* RTA_TABLE */
5280 	       + nla_total_size(4) /* RTA_IIF */
5281 	       + nla_total_size(4) /* RTA_OIF */
5282 	       + nla_total_size(4) /* RTA_PRIORITY */
5283 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
5284 	       + nla_total_size(sizeof(struct rta_cacheinfo))
5285 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
5286 	       + nla_total_size(1) /* RTA_PREF */
5287 	       + nexthop_len;
5288 }
5289 
5290 static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5291 				 unsigned char *flags)
5292 {
5293 	if (nexthop_is_multipath(nh)) {
5294 		struct nlattr *mp;
5295 
5296 		mp = nla_nest_start(skb, RTA_MULTIPATH);
5297 		if (!mp)
5298 			goto nla_put_failure;
5299 
5300 		if (nexthop_mpath_fill_node(skb, nh))
5301 			goto nla_put_failure;
5302 
5303 		nla_nest_end(skb, mp);
5304 	} else {
5305 		struct fib6_nh *fib6_nh;
5306 
5307 		fib6_nh = nexthop_fib6_nh(nh);
5308 		if (fib_nexthop_info(skb, &fib6_nh->nh_common,
5309 				     flags, false) < 0)
5310 			goto nla_put_failure;
5311 	}
5312 
5313 	return 0;
5314 
5315 nla_put_failure:
5316 	return -EMSGSIZE;
5317 }
5318 
5319 static int rt6_fill_node(struct net *net, struct sk_buff *skb,
5320 			 struct fib6_info *rt, struct dst_entry *dst,
5321 			 struct in6_addr *dest, struct in6_addr *src,
5322 			 int iif, int type, u32 portid, u32 seq,
5323 			 unsigned int flags)
5324 {
5325 	struct rt6_info *rt6 = (struct rt6_info *)dst;
5326 	struct rt6key *rt6_dst, *rt6_src;
5327 	u32 *pmetrics, table, rt6_flags;
5328 	unsigned char nh_flags = 0;
5329 	struct nlmsghdr *nlh;
5330 	struct rtmsg *rtm;
5331 	long expires = 0;
5332 
5333 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
5334 	if (!nlh)
5335 		return -EMSGSIZE;
5336 
5337 	if (rt6) {
5338 		rt6_dst = &rt6->rt6i_dst;
5339 		rt6_src = &rt6->rt6i_src;
5340 		rt6_flags = rt6->rt6i_flags;
5341 	} else {
5342 		rt6_dst = &rt->fib6_dst;
5343 		rt6_src = &rt->fib6_src;
5344 		rt6_flags = rt->fib6_flags;
5345 	}
5346 
5347 	rtm = nlmsg_data(nlh);
5348 	rtm->rtm_family = AF_INET6;
5349 	rtm->rtm_dst_len = rt6_dst->plen;
5350 	rtm->rtm_src_len = rt6_src->plen;
5351 	rtm->rtm_tos = 0;
5352 	if (rt->fib6_table)
5353 		table = rt->fib6_table->tb6_id;
5354 	else
5355 		table = RT6_TABLE_UNSPEC;
5356 	rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
5357 	if (nla_put_u32(skb, RTA_TABLE, table))
5358 		goto nla_put_failure;
5359 
5360 	rtm->rtm_type = rt->fib6_type;
5361 	rtm->rtm_flags = 0;
5362 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
5363 	rtm->rtm_protocol = rt->fib6_protocol;
5364 
5365 	if (rt6_flags & RTF_CACHE)
5366 		rtm->rtm_flags |= RTM_F_CLONED;
5367 
5368 	if (dest) {
5369 		if (nla_put_in6_addr(skb, RTA_DST, dest))
5370 			goto nla_put_failure;
5371 		rtm->rtm_dst_len = 128;
5372 	} else if (rtm->rtm_dst_len)
5373 		if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
5374 			goto nla_put_failure;
5375 #ifdef CONFIG_IPV6_SUBTREES
5376 	if (src) {
5377 		if (nla_put_in6_addr(skb, RTA_SRC, src))
5378 			goto nla_put_failure;
5379 		rtm->rtm_src_len = 128;
5380 	} else if (rtm->rtm_src_len &&
5381 		   nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
5382 		goto nla_put_failure;
5383 #endif
5384 	if (iif) {
5385 #ifdef CONFIG_IPV6_MROUTE
5386 		if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
5387 			int err = ip6mr_get_route(net, skb, rtm, portid);
5388 
5389 			if (err == 0)
5390 				return 0;
5391 			if (err < 0)
5392 				goto nla_put_failure;
5393 		} else
5394 #endif
5395 			if (nla_put_u32(skb, RTA_IIF, iif))
5396 				goto nla_put_failure;
5397 	} else if (dest) {
5398 		struct in6_addr saddr_buf;
5399 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
5400 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5401 			goto nla_put_failure;
5402 	}
5403 
5404 	if (rt->fib6_prefsrc.plen) {
5405 		struct in6_addr saddr_buf;
5406 		saddr_buf = rt->fib6_prefsrc.addr;
5407 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5408 			goto nla_put_failure;
5409 	}
5410 
5411 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5412 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
5413 		goto nla_put_failure;
5414 
5415 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
5416 		goto nla_put_failure;
5417 
5418 	/* For multipath routes, walk the siblings list and add
5419 	 * each as a nexthop within RTA_MULTIPATH.
5420 	 */
5421 	if (rt6) {
5422 		if (rt6_flags & RTF_GATEWAY &&
5423 		    nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
5424 			goto nla_put_failure;
5425 
5426 		if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
5427 			goto nla_put_failure;
5428 	} else if (rt->fib6_nsiblings) {
5429 		struct fib6_info *sibling, *next_sibling;
5430 		struct nlattr *mp;
5431 
5432 		mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5433 		if (!mp)
5434 			goto nla_put_failure;
5435 
5436 		if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
5437 				    rt->fib6_nh->fib_nh_weight) < 0)
5438 			goto nla_put_failure;
5439 
5440 		list_for_each_entry_safe(sibling, next_sibling,
5441 					 &rt->fib6_siblings, fib6_siblings) {
5442 			if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
5443 					    sibling->fib6_nh->fib_nh_weight) < 0)
5444 				goto nla_put_failure;
5445 		}
5446 
5447 		nla_nest_end(skb, mp);
5448 	} else if (rt->nh) {
5449 		if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5450 			goto nla_put_failure;
5451 
5452 		if (nexthop_is_blackhole(rt->nh))
5453 			rtm->rtm_type = RTN_BLACKHOLE;
5454 
5455 		if (rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5456 			goto nla_put_failure;
5457 
5458 		rtm->rtm_flags |= nh_flags;
5459 	} else {
5460 		if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common,
5461 				     &nh_flags, false) < 0)
5462 			goto nla_put_failure;
5463 
5464 		rtm->rtm_flags |= nh_flags;
5465 	}
5466 
5467 	if (rt6_flags & RTF_EXPIRES) {
5468 		expires = dst ? dst->expires : rt->expires;
5469 		expires -= jiffies;
5470 	}
5471 
5472 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
5473 		goto nla_put_failure;
5474 
5475 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
5476 		goto nla_put_failure;
5477 
5478 
5479 	nlmsg_end(skb, nlh);
5480 	return 0;
5481 
5482 nla_put_failure:
5483 	nlmsg_cancel(skb, nlh);
5484 	return -EMSGSIZE;
5485 }
5486 
5487 static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
5488 {
5489 	const struct net_device *dev = arg;
5490 
5491 	if (nh->fib_nh_dev == dev)
5492 		return 1;
5493 
5494 	return 0;
5495 }
5496 
5497 static bool fib6_info_uses_dev(const struct fib6_info *f6i,
5498 			       const struct net_device *dev)
5499 {
5500 	if (f6i->nh) {
5501 		struct net_device *_dev = (struct net_device *)dev;
5502 
5503 		return !!nexthop_for_each_fib6_nh(f6i->nh,
5504 						  fib6_info_nh_uses_dev,
5505 						  _dev);
5506 	}
5507 
5508 	if (f6i->fib6_nh->fib_nh_dev == dev)
5509 		return true;
5510 
5511 	if (f6i->fib6_nsiblings) {
5512 		struct fib6_info *sibling, *next_sibling;
5513 
5514 		list_for_each_entry_safe(sibling, next_sibling,
5515 					 &f6i->fib6_siblings, fib6_siblings) {
5516 			if (sibling->fib6_nh->fib_nh_dev == dev)
5517 				return true;
5518 		}
5519 	}
5520 
5521 	return false;
5522 }
5523 
5524 int rt6_dump_route(struct fib6_info *rt, void *p_arg)
5525 {
5526 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
5527 	struct fib_dump_filter *filter = &arg->filter;
5528 	unsigned int flags = NLM_F_MULTI;
5529 	struct net *net = arg->net;
5530 
5531 	if (rt == net->ipv6.fib6_null_entry)
5532 		return 0;
5533 
5534 	if ((filter->flags & RTM_F_PREFIX) &&
5535 	    !(rt->fib6_flags & RTF_PREFIX_RT)) {
5536 		/* success since this is not a prefix route */
5537 		return 1;
5538 	}
5539 	if (filter->filter_set) {
5540 		if ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
5541 		    (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
5542 		    (filter->protocol && rt->fib6_protocol != filter->protocol)) {
5543 			return 1;
5544 		}
5545 		flags |= NLM_F_DUMP_FILTERED;
5546 	}
5547 
5548 	return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
5549 			     RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
5550 			     arg->cb->nlh->nlmsg_seq, flags);
5551 }
5552 
5553 static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
5554 					const struct nlmsghdr *nlh,
5555 					struct nlattr **tb,
5556 					struct netlink_ext_ack *extack)
5557 {
5558 	struct rtmsg *rtm;
5559 	int i, err;
5560 
5561 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
5562 		NL_SET_ERR_MSG_MOD(extack,
5563 				   "Invalid header for get route request");
5564 		return -EINVAL;
5565 	}
5566 
5567 	if (!netlink_strict_get_check(skb))
5568 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
5569 					      rtm_ipv6_policy, extack);
5570 
5571 	rtm = nlmsg_data(nlh);
5572 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
5573 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
5574 	    rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
5575 	    rtm->rtm_type) {
5576 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
5577 		return -EINVAL;
5578 	}
5579 	if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
5580 		NL_SET_ERR_MSG_MOD(extack,
5581 				   "Invalid flags for get route request");
5582 		return -EINVAL;
5583 	}
5584 
5585 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
5586 					    rtm_ipv6_policy, extack);
5587 	if (err)
5588 		return err;
5589 
5590 	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
5591 	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
5592 		NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
5593 		return -EINVAL;
5594 	}
5595 
5596 	for (i = 0; i <= RTA_MAX; i++) {
5597 		if (!tb[i])
5598 			continue;
5599 
5600 		switch (i) {
5601 		case RTA_SRC:
5602 		case RTA_DST:
5603 		case RTA_IIF:
5604 		case RTA_OIF:
5605 		case RTA_MARK:
5606 		case RTA_UID:
5607 		case RTA_SPORT:
5608 		case RTA_DPORT:
5609 		case RTA_IP_PROTO:
5610 			break;
5611 		default:
5612 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
5613 			return -EINVAL;
5614 		}
5615 	}
5616 
5617 	return 0;
5618 }
5619 
5620 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5621 			      struct netlink_ext_ack *extack)
5622 {
5623 	struct net *net = sock_net(in_skb->sk);
5624 	struct nlattr *tb[RTA_MAX+1];
5625 	int err, iif = 0, oif = 0;
5626 	struct fib6_info *from;
5627 	struct dst_entry *dst;
5628 	struct rt6_info *rt;
5629 	struct sk_buff *skb;
5630 	struct rtmsg *rtm;
5631 	struct flowi6 fl6 = {};
5632 	bool fibmatch;
5633 
5634 	err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
5635 	if (err < 0)
5636 		goto errout;
5637 
5638 	err = -EINVAL;
5639 	rtm = nlmsg_data(nlh);
5640 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
5641 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
5642 
5643 	if (tb[RTA_SRC]) {
5644 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5645 			goto errout;
5646 
5647 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
5648 	}
5649 
5650 	if (tb[RTA_DST]) {
5651 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5652 			goto errout;
5653 
5654 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
5655 	}
5656 
5657 	if (tb[RTA_IIF])
5658 		iif = nla_get_u32(tb[RTA_IIF]);
5659 
5660 	if (tb[RTA_OIF])
5661 		oif = nla_get_u32(tb[RTA_OIF]);
5662 
5663 	if (tb[RTA_MARK])
5664 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
5665 
5666 	if (tb[RTA_UID])
5667 		fl6.flowi6_uid = make_kuid(current_user_ns(),
5668 					   nla_get_u32(tb[RTA_UID]));
5669 	else
5670 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5671 
5672 	if (tb[RTA_SPORT])
5673 		fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5674 
5675 	if (tb[RTA_DPORT])
5676 		fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5677 
5678 	if (tb[RTA_IP_PROTO]) {
5679 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
5680 						  &fl6.flowi6_proto, AF_INET6,
5681 						  extack);
5682 		if (err)
5683 			goto errout;
5684 	}
5685 
5686 	if (iif) {
5687 		struct net_device *dev;
5688 		int flags = 0;
5689 
5690 		rcu_read_lock();
5691 
5692 		dev = dev_get_by_index_rcu(net, iif);
5693 		if (!dev) {
5694 			rcu_read_unlock();
5695 			err = -ENODEV;
5696 			goto errout;
5697 		}
5698 
5699 		fl6.flowi6_iif = iif;
5700 
5701 		if (!ipv6_addr_any(&fl6.saddr))
5702 			flags |= RT6_LOOKUP_F_HAS_SADDR;
5703 
5704 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
5705 
5706 		rcu_read_unlock();
5707 	} else {
5708 		fl6.flowi6_oif = oif;
5709 
5710 		dst = ip6_route_output(net, NULL, &fl6);
5711 	}
5712 
5713 
5714 	rt = container_of(dst, struct rt6_info, dst);
5715 	if (rt->dst.error) {
5716 		err = rt->dst.error;
5717 		ip6_rt_put(rt);
5718 		goto errout;
5719 	}
5720 
5721 	if (rt == net->ipv6.ip6_null_entry) {
5722 		err = rt->dst.error;
5723 		ip6_rt_put(rt);
5724 		goto errout;
5725 	}
5726 
5727 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
5728 	if (!skb) {
5729 		ip6_rt_put(rt);
5730 		err = -ENOBUFS;
5731 		goto errout;
5732 	}
5733 
5734 	skb_dst_set(skb, &rt->dst);
5735 
5736 	rcu_read_lock();
5737 	from = rcu_dereference(rt->from);
5738 	if (from) {
5739 		if (fibmatch)
5740 			err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5741 					    iif, RTM_NEWROUTE,
5742 					    NETLINK_CB(in_skb).portid,
5743 					    nlh->nlmsg_seq, 0);
5744 		else
5745 			err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5746 					    &fl6.saddr, iif, RTM_NEWROUTE,
5747 					    NETLINK_CB(in_skb).portid,
5748 					    nlh->nlmsg_seq, 0);
5749 	} else {
5750 		err = -ENETUNREACH;
5751 	}
5752 	rcu_read_unlock();
5753 
5754 	if (err < 0) {
5755 		kfree_skb(skb);
5756 		goto errout;
5757 	}
5758 
5759 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
5760 errout:
5761 	return err;
5762 }
5763 
5764 void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
5765 		     unsigned int nlm_flags)
5766 {
5767 	struct sk_buff *skb;
5768 	struct net *net = info->nl_net;
5769 	u32 seq;
5770 	int err;
5771 
5772 	err = -ENOBUFS;
5773 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
5774 
5775 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
5776 	if (!skb)
5777 		goto errout;
5778 
5779 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5780 			    event, info->portid, seq, nlm_flags);
5781 	if (err < 0) {
5782 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5783 		WARN_ON(err == -EMSGSIZE);
5784 		kfree_skb(skb);
5785 		goto errout;
5786 	}
5787 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
5788 		    info->nlh, gfp_any());
5789 	return;
5790 errout:
5791 	if (err < 0)
5792 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
5793 }
5794 
5795 void fib6_rt_update(struct net *net, struct fib6_info *rt,
5796 		    struct nl_info *info)
5797 {
5798 	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
5799 	struct sk_buff *skb;
5800 	int err = -ENOBUFS;
5801 
5802 	/* call_fib6_entry_notifiers will be removed when in-kernel notifier
5803 	 * is implemented and supported for nexthop objects
5804 	 */
5805 	call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
5806 
5807 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
5808 	if (!skb)
5809 		goto errout;
5810 
5811 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5812 			    RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
5813 	if (err < 0) {
5814 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
5815 		WARN_ON(err == -EMSGSIZE);
5816 		kfree_skb(skb);
5817 		goto errout;
5818 	}
5819 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
5820 		    info->nlh, gfp_any());
5821 	return;
5822 errout:
5823 	if (err < 0)
5824 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
5825 }
5826 
5827 static int ip6_route_dev_notify(struct notifier_block *this,
5828 				unsigned long event, void *ptr)
5829 {
5830 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5831 	struct net *net = dev_net(dev);
5832 
5833 	if (!(dev->flags & IFF_LOOPBACK))
5834 		return NOTIFY_OK;
5835 
5836 	if (event == NETDEV_REGISTER) {
5837 		net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
5838 		net->ipv6.ip6_null_entry->dst.dev = dev;
5839 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
5840 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5841 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
5842 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
5843 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
5844 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
5845 #endif
5846 	 } else if (event == NETDEV_UNREGISTER &&
5847 		    dev->reg_state != NETREG_UNREGISTERED) {
5848 		/* NETDEV_UNREGISTER could be fired for multiple times by
5849 		 * netdev_wait_allrefs(). Make sure we only call this once.
5850 		 */
5851 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
5852 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5853 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
5854 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
5855 #endif
5856 	}
5857 
5858 	return NOTIFY_OK;
5859 }
5860 
5861 /*
5862  *	/proc
5863  */
5864 
5865 #ifdef CONFIG_PROC_FS
5866 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
5867 {
5868 	struct net *net = (struct net *)seq->private;
5869 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
5870 		   net->ipv6.rt6_stats->fib_nodes,
5871 		   net->ipv6.rt6_stats->fib_route_nodes,
5872 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
5873 		   net->ipv6.rt6_stats->fib_rt_entries,
5874 		   net->ipv6.rt6_stats->fib_rt_cache,
5875 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
5876 		   net->ipv6.rt6_stats->fib_discarded_routes);
5877 
5878 	return 0;
5879 }
5880 #endif	/* CONFIG_PROC_FS */
5881 
5882 #ifdef CONFIG_SYSCTL
5883 
5884 static
5885 int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
5886 			      void __user *buffer, size_t *lenp, loff_t *ppos)
5887 {
5888 	struct net *net;
5889 	int delay;
5890 	int ret;
5891 	if (!write)
5892 		return -EINVAL;
5893 
5894 	net = (struct net *)ctl->extra1;
5895 	delay = net->ipv6.sysctl.flush_delay;
5896 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5897 	if (ret)
5898 		return ret;
5899 
5900 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
5901 	return 0;
5902 }
5903 
5904 static int zero;
5905 static int one = 1;
5906 
5907 static struct ctl_table ipv6_route_table_template[] = {
5908 	{
5909 		.procname	=	"flush",
5910 		.data		=	&init_net.ipv6.sysctl.flush_delay,
5911 		.maxlen		=	sizeof(int),
5912 		.mode		=	0200,
5913 		.proc_handler	=	ipv6_sysctl_rtcache_flush
5914 	},
5915 	{
5916 		.procname	=	"gc_thresh",
5917 		.data		=	&ip6_dst_ops_template.gc_thresh,
5918 		.maxlen		=	sizeof(int),
5919 		.mode		=	0644,
5920 		.proc_handler	=	proc_dointvec,
5921 	},
5922 	{
5923 		.procname	=	"max_size",
5924 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
5925 		.maxlen		=	sizeof(int),
5926 		.mode		=	0644,
5927 		.proc_handler	=	proc_dointvec,
5928 	},
5929 	{
5930 		.procname	=	"gc_min_interval",
5931 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
5932 		.maxlen		=	sizeof(int),
5933 		.mode		=	0644,
5934 		.proc_handler	=	proc_dointvec_jiffies,
5935 	},
5936 	{
5937 		.procname	=	"gc_timeout",
5938 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
5939 		.maxlen		=	sizeof(int),
5940 		.mode		=	0644,
5941 		.proc_handler	=	proc_dointvec_jiffies,
5942 	},
5943 	{
5944 		.procname	=	"gc_interval",
5945 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
5946 		.maxlen		=	sizeof(int),
5947 		.mode		=	0644,
5948 		.proc_handler	=	proc_dointvec_jiffies,
5949 	},
5950 	{
5951 		.procname	=	"gc_elasticity",
5952 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
5953 		.maxlen		=	sizeof(int),
5954 		.mode		=	0644,
5955 		.proc_handler	=	proc_dointvec,
5956 	},
5957 	{
5958 		.procname	=	"mtu_expires",
5959 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
5960 		.maxlen		=	sizeof(int),
5961 		.mode		=	0644,
5962 		.proc_handler	=	proc_dointvec_jiffies,
5963 	},
5964 	{
5965 		.procname	=	"min_adv_mss",
5966 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
5967 		.maxlen		=	sizeof(int),
5968 		.mode		=	0644,
5969 		.proc_handler	=	proc_dointvec,
5970 	},
5971 	{
5972 		.procname	=	"gc_min_interval_ms",
5973 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
5974 		.maxlen		=	sizeof(int),
5975 		.mode		=	0644,
5976 		.proc_handler	=	proc_dointvec_ms_jiffies,
5977 	},
5978 	{
5979 		.procname	=	"skip_notify_on_dev_down",
5980 		.data		=	&init_net.ipv6.sysctl.skip_notify_on_dev_down,
5981 		.maxlen		=	sizeof(int),
5982 		.mode		=	0644,
5983 		.proc_handler	=	proc_dointvec,
5984 		.extra1		=	&zero,
5985 		.extra2		=	&one,
5986 	},
5987 	{ }
5988 };
5989 
5990 struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
5991 {
5992 	struct ctl_table *table;
5993 
5994 	table = kmemdup(ipv6_route_table_template,
5995 			sizeof(ipv6_route_table_template),
5996 			GFP_KERNEL);
5997 
5998 	if (table) {
5999 		table[0].data = &net->ipv6.sysctl.flush_delay;
6000 		table[0].extra1 = net;
6001 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
6002 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
6003 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6004 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
6005 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
6006 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
6007 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
6008 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
6009 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
6010 		table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
6011 
6012 		/* Don't export sysctls to unprivileged users */
6013 		if (net->user_ns != &init_user_ns)
6014 			table[0].procname = NULL;
6015 	}
6016 
6017 	return table;
6018 }
6019 #endif
6020 
6021 static int __net_init ip6_route_net_init(struct net *net)
6022 {
6023 	int ret = -ENOMEM;
6024 
6025 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
6026 	       sizeof(net->ipv6.ip6_dst_ops));
6027 
6028 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6029 		goto out_ip6_dst_ops;
6030 
6031 	net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
6032 	if (!net->ipv6.fib6_null_entry)
6033 		goto out_ip6_dst_entries;
6034 	memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
6035 	       sizeof(*net->ipv6.fib6_null_entry));
6036 
6037 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
6038 					   sizeof(*net->ipv6.ip6_null_entry),
6039 					   GFP_KERNEL);
6040 	if (!net->ipv6.ip6_null_entry)
6041 		goto out_fib6_null_entry;
6042 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6043 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
6044 			 ip6_template_metrics, true);
6045 	INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
6046 
6047 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6048 	net->ipv6.fib6_has_custom_rules = false;
6049 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
6050 					       sizeof(*net->ipv6.ip6_prohibit_entry),
6051 					       GFP_KERNEL);
6052 	if (!net->ipv6.ip6_prohibit_entry)
6053 		goto out_ip6_null_entry;
6054 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6055 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
6056 			 ip6_template_metrics, true);
6057 	INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
6058 
6059 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
6060 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
6061 					       GFP_KERNEL);
6062 	if (!net->ipv6.ip6_blk_hole_entry)
6063 		goto out_ip6_prohibit_entry;
6064 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
6065 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
6066 			 ip6_template_metrics, true);
6067 	INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
6068 #endif
6069 
6070 	net->ipv6.sysctl.flush_delay = 0;
6071 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
6072 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6073 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6074 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6075 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6076 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6077 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
6078 	net->ipv6.sysctl.skip_notify_on_dev_down = 0;
6079 
6080 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
6081 
6082 	ret = 0;
6083 out:
6084 	return ret;
6085 
6086 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6087 out_ip6_prohibit_entry:
6088 	kfree(net->ipv6.ip6_prohibit_entry);
6089 out_ip6_null_entry:
6090 	kfree(net->ipv6.ip6_null_entry);
6091 #endif
6092 out_fib6_null_entry:
6093 	kfree(net->ipv6.fib6_null_entry);
6094 out_ip6_dst_entries:
6095 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6096 out_ip6_dst_ops:
6097 	goto out;
6098 }
6099 
6100 static void __net_exit ip6_route_net_exit(struct net *net)
6101 {
6102 	kfree(net->ipv6.fib6_null_entry);
6103 	kfree(net->ipv6.ip6_null_entry);
6104 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6105 	kfree(net->ipv6.ip6_prohibit_entry);
6106 	kfree(net->ipv6.ip6_blk_hole_entry);
6107 #endif
6108 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6109 }
6110 
6111 static int __net_init ip6_route_net_init_late(struct net *net)
6112 {
6113 #ifdef CONFIG_PROC_FS
6114 	proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6115 			sizeof(struct ipv6_route_iter));
6116 	proc_create_net_single("rt6_stats", 0444, net->proc_net,
6117 			rt6_stats_seq_show, NULL);
6118 #endif
6119 	return 0;
6120 }
6121 
6122 static void __net_exit ip6_route_net_exit_late(struct net *net)
6123 {
6124 #ifdef CONFIG_PROC_FS
6125 	remove_proc_entry("ipv6_route", net->proc_net);
6126 	remove_proc_entry("rt6_stats", net->proc_net);
6127 #endif
6128 }
6129 
6130 static struct pernet_operations ip6_route_net_ops = {
6131 	.init = ip6_route_net_init,
6132 	.exit = ip6_route_net_exit,
6133 };
6134 
6135 static int __net_init ipv6_inetpeer_init(struct net *net)
6136 {
6137 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6138 
6139 	if (!bp)
6140 		return -ENOMEM;
6141 	inet_peer_base_init(bp);
6142 	net->ipv6.peers = bp;
6143 	return 0;
6144 }
6145 
6146 static void __net_exit ipv6_inetpeer_exit(struct net *net)
6147 {
6148 	struct inet_peer_base *bp = net->ipv6.peers;
6149 
6150 	net->ipv6.peers = NULL;
6151 	inetpeer_invalidate_tree(bp);
6152 	kfree(bp);
6153 }
6154 
6155 static struct pernet_operations ipv6_inetpeer_ops = {
6156 	.init	=	ipv6_inetpeer_init,
6157 	.exit	=	ipv6_inetpeer_exit,
6158 };
6159 
6160 static struct pernet_operations ip6_route_net_late_ops = {
6161 	.init = ip6_route_net_init_late,
6162 	.exit = ip6_route_net_exit_late,
6163 };
6164 
6165 static struct notifier_block ip6_route_dev_notifier = {
6166 	.notifier_call = ip6_route_dev_notify,
6167 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
6168 };
6169 
6170 void __init ip6_route_init_special_entries(void)
6171 {
6172 	/* Registering of the loopback is done before this portion of code,
6173 	 * the loopback reference in rt6_info will not be taken, do it
6174 	 * manually for init_net */
6175 	init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
6176 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
6177 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6178   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6179 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
6180 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6181 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
6182 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
6183   #endif
6184 }
6185 
6186 int __init ip6_route_init(void)
6187 {
6188 	int ret;
6189 	int cpu;
6190 
6191 	ret = -ENOMEM;
6192 	ip6_dst_ops_template.kmem_cachep =
6193 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
6194 				  SLAB_HWCACHE_ALIGN, NULL);
6195 	if (!ip6_dst_ops_template.kmem_cachep)
6196 		goto out;
6197 
6198 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
6199 	if (ret)
6200 		goto out_kmem_cache;
6201 
6202 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6203 	if (ret)
6204 		goto out_dst_entries;
6205 
6206 	ret = register_pernet_subsys(&ip6_route_net_ops);
6207 	if (ret)
6208 		goto out_register_inetpeer;
6209 
6210 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
6211 
6212 	ret = fib6_init();
6213 	if (ret)
6214 		goto out_register_subsys;
6215 
6216 	ret = xfrm6_init();
6217 	if (ret)
6218 		goto out_fib6_init;
6219 
6220 	ret = fib6_rules_init();
6221 	if (ret)
6222 		goto xfrm6_init;
6223 
6224 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
6225 	if (ret)
6226 		goto fib6_rules_init;
6227 
6228 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
6229 				   inet6_rtm_newroute, NULL, 0);
6230 	if (ret < 0)
6231 		goto out_register_late_subsys;
6232 
6233 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
6234 				   inet6_rtm_delroute, NULL, 0);
6235 	if (ret < 0)
6236 		goto out_register_late_subsys;
6237 
6238 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
6239 				   inet6_rtm_getroute, NULL,
6240 				   RTNL_FLAG_DOIT_UNLOCKED);
6241 	if (ret < 0)
6242 		goto out_register_late_subsys;
6243 
6244 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
6245 	if (ret)
6246 		goto out_register_late_subsys;
6247 
6248 	for_each_possible_cpu(cpu) {
6249 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
6250 
6251 		INIT_LIST_HEAD(&ul->head);
6252 		spin_lock_init(&ul->lock);
6253 	}
6254 
6255 out:
6256 	return ret;
6257 
6258 out_register_late_subsys:
6259 	rtnl_unregister_all(PF_INET6);
6260 	unregister_pernet_subsys(&ip6_route_net_late_ops);
6261 fib6_rules_init:
6262 	fib6_rules_cleanup();
6263 xfrm6_init:
6264 	xfrm6_fini();
6265 out_fib6_init:
6266 	fib6_gc_cleanup();
6267 out_register_subsys:
6268 	unregister_pernet_subsys(&ip6_route_net_ops);
6269 out_register_inetpeer:
6270 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
6271 out_dst_entries:
6272 	dst_entries_destroy(&ip6_dst_blackhole_ops);
6273 out_kmem_cache:
6274 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6275 	goto out;
6276 }
6277 
6278 void ip6_route_cleanup(void)
6279 {
6280 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
6281 	unregister_pernet_subsys(&ip6_route_net_late_ops);
6282 	fib6_rules_cleanup();
6283 	xfrm6_fini();
6284 	fib6_gc_cleanup();
6285 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
6286 	unregister_pernet_subsys(&ip6_route_net_ops);
6287 	dst_entries_destroy(&ip6_dst_blackhole_ops);
6288 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6289 }
6290