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