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