xref: /openbmc/linux/net/ipv6/route.c (revision 6fad361ae9f43616bc6a3acc9180e75396031fe1)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *	FIB front-end.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	Authors:
71da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds /*	Changes:
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI
131da177e4SLinus Torvalds  *		reworked default router selection.
141da177e4SLinus Torvalds  *		- respect outgoing interface
151da177e4SLinus Torvalds  *		- select from (probably) reachable routers (i.e.
161da177e4SLinus Torvalds  *		routers in REACHABLE, STALE, DELAY or PROBE states).
171da177e4SLinus Torvalds  *		- always select the same router if it is (probably)
181da177e4SLinus Torvalds  *		reachable.  otherwise, round-robin the list.
19c0bece9fSYOSHIFUJI Hideaki  *	Ville Nuorvala
20c0bece9fSYOSHIFUJI Hideaki  *		Fixed routing subtrees.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
23f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
24f3213831SJoe Perches 
254fc268d2SRandy Dunlap #include <linux/capability.h>
261da177e4SLinus Torvalds #include <linux/errno.h>
27bc3b2d7fSPaul Gortmaker #include <linux/export.h>
281da177e4SLinus Torvalds #include <linux/types.h>
291da177e4SLinus Torvalds #include <linux/times.h>
301da177e4SLinus Torvalds #include <linux/socket.h>
311da177e4SLinus Torvalds #include <linux/sockios.h>
321da177e4SLinus Torvalds #include <linux/net.h>
331da177e4SLinus Torvalds #include <linux/route.h>
341da177e4SLinus Torvalds #include <linux/netdevice.h>
351da177e4SLinus Torvalds #include <linux/in6.h>
367bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
371da177e4SLinus Torvalds #include <linux/init.h>
381da177e4SLinus Torvalds #include <linux/if_arp.h>
391da177e4SLinus Torvalds #include <linux/proc_fs.h>
401da177e4SLinus Torvalds #include <linux/seq_file.h>
415b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
425a0e3ad6STejun Heo #include <linux/slab.h>
4335732d01SWei Wang #include <linux/jhash.h>
44457c4cbcSEric W. Biederman #include <net/net_namespace.h>
451da177e4SLinus Torvalds #include <net/snmp.h>
461da177e4SLinus Torvalds #include <net/ipv6.h>
471da177e4SLinus Torvalds #include <net/ip6_fib.h>
481da177e4SLinus Torvalds #include <net/ip6_route.h>
491da177e4SLinus Torvalds #include <net/ndisc.h>
501da177e4SLinus Torvalds #include <net/addrconf.h>
511da177e4SLinus Torvalds #include <net/tcp.h>
521da177e4SLinus Torvalds #include <linux/rtnetlink.h>
531da177e4SLinus Torvalds #include <net/dst.h>
54904af04dSJiri Benc #include <net/dst_metadata.h>
551da177e4SLinus Torvalds #include <net/xfrm.h>
568d71740cSTom Tucker #include <net/netevent.h>
5721713ebcSThomas Graf #include <net/netlink.h>
583c618c1dSDavid Ahern #include <net/rtnh.h>
5919e42e45SRoopa Prabhu #include <net/lwtunnel.h>
60904af04dSJiri Benc #include <net/ip_tunnels.h>
61ca254490SDavid Ahern #include <net/l3mdev.h>
62eacb9384SRoopa Prabhu #include <net/ip.h>
637c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
64951cf368SYonghong Song #include <linux/btf_ids.h>
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
671da177e4SLinus Torvalds #include <linux/sysctl.h>
681da177e4SLinus Torvalds #endif
691da177e4SLinus Torvalds 
7030d444d3SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type);
7130d444d3SDavid Ahern 
7230d444d3SDavid Ahern #define CREATE_TRACE_POINTS
7330d444d3SDavid Ahern #include <trace/events/fib6.h>
7430d444d3SDavid Ahern EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
7530d444d3SDavid Ahern #undef CREATE_TRACE_POINTS
7630d444d3SDavid Ahern 
77afc154e9SHannes Frederic Sowa enum rt6_nud_state {
787e980569SJiri Benc 	RT6_NUD_FAIL_HARD = -3,
797e980569SJiri Benc 	RT6_NUD_FAIL_PROBE = -2,
807e980569SJiri Benc 	RT6_NUD_FAIL_DO_RR = -1,
81afc154e9SHannes Frederic Sowa 	RT6_NUD_SUCCEED = 1
82afc154e9SHannes Frederic Sowa };
83afc154e9SHannes Frederic Sowa 
84bbd807dfSBrian Vazquez INDIRECT_CALLABLE_SCOPE
85bbd807dfSBrian Vazquez struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
860dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
87f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_SCOPE
88f67fbeaeSBrian Vazquez unsigned int		ip6_mtu(const struct dst_entry *dst);
891da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
901da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
911da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
921da177e4SLinus Torvalds 				       struct net_device *dev, int how);
93569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
96ede2059dSEric W. Biederman static int		ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
977150aedeSKamala R static int		ip6_pkt_prohibit(struct sk_buff *skb);
98ede2059dSEric W. Biederman static int		ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
991da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
1006700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
101bd085ef6SHangbin Liu 					   struct sk_buff *skb, u32 mtu,
102bd085ef6SHangbin Liu 					   bool confirm_neigh);
1036700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
1046700c270SDavid S. Miller 					struct sk_buff *skb);
105702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
106702cea56SDavid Ahern 			   int strict);
107a1b7a1f0SDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *f6i);
108d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
1098d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
110d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
11116a16cd3SDavid Ahern 			 int iif, int type, u32 portid, u32 seq,
11216a16cd3SDavid Ahern 			 unsigned int flags);
1137e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
114510e2cedSWei Wang 					   const struct in6_addr *daddr,
115510e2cedSWei Wang 					   const struct in6_addr *saddr);
1161da177e4SLinus Torvalds 
11770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1188d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
119b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
120830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
121830218c1SDavid Ahern 					   struct net_device *dev,
12295c96174SEric Dumazet 					   unsigned int pref);
1238d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
124b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
125830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
126830218c1SDavid Ahern 					   struct net_device *dev);
12770ceb4f5SYOSHIFUJI Hideaki #endif
12870ceb4f5SYOSHIFUJI Hideaki 
1298d0b94afSMartin KaFai Lau struct uncached_list {
1308d0b94afSMartin KaFai Lau 	spinlock_t		lock;
1318d0b94afSMartin KaFai Lau 	struct list_head	head;
1328d0b94afSMartin KaFai Lau };
1338d0b94afSMartin KaFai Lau 
1348d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
1358d0b94afSMartin KaFai Lau 
136510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt)
1378d0b94afSMartin KaFai Lau {
1388d0b94afSMartin KaFai Lau 	struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
1398d0b94afSMartin KaFai Lau 
1408d0b94afSMartin KaFai Lau 	rt->rt6i_uncached_list = ul;
1418d0b94afSMartin KaFai Lau 
1428d0b94afSMartin KaFai Lau 	spin_lock_bh(&ul->lock);
1438d0b94afSMartin KaFai Lau 	list_add_tail(&rt->rt6i_uncached, &ul->head);
1448d0b94afSMartin KaFai Lau 	spin_unlock_bh(&ul->lock);
1458d0b94afSMartin KaFai Lau }
1468d0b94afSMartin KaFai Lau 
147510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt)
1488d0b94afSMartin KaFai Lau {
1498d0b94afSMartin KaFai Lau 	if (!list_empty(&rt->rt6i_uncached)) {
1508d0b94afSMartin KaFai Lau 		struct uncached_list *ul = rt->rt6i_uncached_list;
15181eb8447SWei Wang 		struct net *net = dev_net(rt->dst.dev);
1528d0b94afSMartin KaFai Lau 
1538d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1548d0b94afSMartin KaFai Lau 		list_del(&rt->rt6i_uncached);
15581eb8447SWei Wang 		atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
1568d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1578d0b94afSMartin KaFai Lau 	}
1588d0b94afSMartin KaFai Lau }
1598d0b94afSMartin KaFai Lau 
1608d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
1618d0b94afSMartin KaFai Lau {
1628d0b94afSMartin KaFai Lau 	struct net_device *loopback_dev = net->loopback_dev;
1638d0b94afSMartin KaFai Lau 	int cpu;
1648d0b94afSMartin KaFai Lau 
165e332bc67SEric W. Biederman 	if (dev == loopback_dev)
166e332bc67SEric W. Biederman 		return;
167e332bc67SEric W. Biederman 
1688d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
1698d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
1708d0b94afSMartin KaFai Lau 		struct rt6_info *rt;
1718d0b94afSMartin KaFai Lau 
1728d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1738d0b94afSMartin KaFai Lau 		list_for_each_entry(rt, &ul->head, rt6i_uncached) {
1748d0b94afSMartin KaFai Lau 			struct inet6_dev *rt_idev = rt->rt6i_idev;
1758d0b94afSMartin KaFai Lau 			struct net_device *rt_dev = rt->dst.dev;
1768d0b94afSMartin KaFai Lau 
177e332bc67SEric W. Biederman 			if (rt_idev->dev == dev) {
1788d0b94afSMartin KaFai Lau 				rt->rt6i_idev = in6_dev_get(loopback_dev);
1798d0b94afSMartin KaFai Lau 				in6_dev_put(rt_idev);
1808d0b94afSMartin KaFai Lau 			}
1818d0b94afSMartin KaFai Lau 
182e332bc67SEric W. Biederman 			if (rt_dev == dev) {
1838d7017fdSMahesh Bandewar 				rt->dst.dev = blackhole_netdev;
1848d0b94afSMartin KaFai Lau 				dev_hold(rt->dst.dev);
1858d0b94afSMartin KaFai Lau 				dev_put(rt_dev);
1868d0b94afSMartin KaFai Lau 			}
1878d0b94afSMartin KaFai Lau 		}
1888d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1898d0b94afSMartin KaFai Lau 	}
1908d0b94afSMartin KaFai Lau }
1918d0b94afSMartin KaFai Lau 
192f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p,
193f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
194f894cbf8SDavid S. Miller 					     const void *daddr)
19539232973SDavid S. Miller {
196a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
19739232973SDavid S. Miller 		return (const void *) p;
198f894cbf8SDavid S. Miller 	else if (skb)
199f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
20039232973SDavid S. Miller 	return daddr;
20139232973SDavid S. Miller }
20239232973SDavid S. Miller 
203f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
204f8a1b43bSDavid Ahern 				   struct net_device *dev,
205f894cbf8SDavid S. Miller 				   struct sk_buff *skb,
206f894cbf8SDavid S. Miller 				   const void *daddr)
207d3aaeb38SDavid S. Miller {
20839232973SDavid S. Miller 	struct neighbour *n;
20939232973SDavid S. Miller 
210f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(gw, skb, daddr);
211f8a1b43bSDavid Ahern 	n = __ipv6_neigh_lookup(dev, daddr);
212f83c7790SDavid S. Miller 	if (n)
213f83c7790SDavid S. Miller 		return n;
2147adf3246SStefano Brivio 
2157adf3246SStefano Brivio 	n = neigh_create(&nd_tbl, daddr, dev);
2167adf3246SStefano Brivio 	return IS_ERR(n) ? NULL : n;
217f8a1b43bSDavid Ahern }
218f8a1b43bSDavid Ahern 
219f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
220f8a1b43bSDavid Ahern 					      struct sk_buff *skb,
221f8a1b43bSDavid Ahern 					      const void *daddr)
222f8a1b43bSDavid Ahern {
223f8a1b43bSDavid Ahern 	const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
224f8a1b43bSDavid Ahern 
2252c6b55f4SNicolas Dichtel 	return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
2262c6b55f4SNicolas Dichtel 				dst->dev, skb, daddr);
227f83c7790SDavid S. Miller }
228f83c7790SDavid S. Miller 
22963fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
23063fca65dSJulian Anastasov {
23163fca65dSJulian Anastasov 	struct net_device *dev = dst->dev;
23263fca65dSJulian Anastasov 	struct rt6_info *rt = (struct rt6_info *)dst;
23363fca65dSJulian Anastasov 
234cbfd6891SStefano Brivio 	daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
23563fca65dSJulian Anastasov 	if (!daddr)
23663fca65dSJulian Anastasov 		return;
23763fca65dSJulian Anastasov 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
23863fca65dSJulian Anastasov 		return;
23963fca65dSJulian Anastasov 	if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
24063fca65dSJulian Anastasov 		return;
24163fca65dSJulian Anastasov 	__ipv6_confirm_neigh(dev, daddr);
24263fca65dSJulian Anastasov }
24363fca65dSJulian Anastasov 
2449a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
2451da177e4SLinus Torvalds 	.family			=	AF_INET6,
2461da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
2471da177e4SLinus Torvalds 	.gc_thresh		=	1024,
2481da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
2490dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
250ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
251d4ead6b3SDavid Ahern 	.cow_metrics		=	dst_cow_metrics_generic,
2521da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
2531da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
2541da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
2551da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
2561da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
2576e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
2589f8955ccSEric W. Biederman 	.local_out		=	__ip6_local_out,
259f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
26063fca65dSJulian Anastasov 	.confirm_neigh		=	ip6_confirm_neigh,
2611da177e4SLinus Torvalds };
2621da177e4SLinus Torvalds 
263ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
264ec831ea7SRoland Dreier {
265618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
266618f9bc7SSteffen Klassert 
267618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
268ec831ea7SRoland Dreier }
269ec831ea7SRoland Dreier 
2706700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
271bd085ef6SHangbin Liu 					 struct sk_buff *skb, u32 mtu,
272bd085ef6SHangbin Liu 					 bool confirm_neigh)
27314e50e57SDavid S. Miller {
27414e50e57SDavid S. Miller }
27514e50e57SDavid S. Miller 
2766700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
2776700c270SDavid S. Miller 				      struct sk_buff *skb)
278b587ee3bSDavid S. Miller {
279b587ee3bSDavid S. Miller }
280b587ee3bSDavid S. Miller 
28114e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
28214e50e57SDavid S. Miller 	.family			=	AF_INET6,
28314e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
28414e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
285ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
286214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
28714e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
288b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2890a1f5962SMartin KaFai Lau 	.cow_metrics		=	dst_cow_metrics_generic,
290f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
29114e50e57SDavid S. Miller };
29214e50e57SDavid S. Miller 
29362fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
29414edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
29562fa8a84SDavid S. Miller };
29662fa8a84SDavid S. Miller 
2978d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = {
29893c2fb25SDavid Ahern 	.fib6_flags	= (RTF_REJECT | RTF_NONEXTHOP),
29993c2fb25SDavid Ahern 	.fib6_protocol  = RTPROT_KERNEL,
30093c2fb25SDavid Ahern 	.fib6_metric	= ~(u32)0,
301f05713e0SEric Dumazet 	.fib6_ref	= REFCOUNT_INIT(1),
302421842edSDavid Ahern 	.fib6_type	= RTN_UNREACHABLE,
303421842edSDavid Ahern 	.fib6_metrics	= (struct dst_metrics *)&dst_default_metrics,
304421842edSDavid Ahern };
305421842edSDavid Ahern 
306fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
3071da177e4SLinus Torvalds 	.dst = {
3081da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
3091da177e4SLinus Torvalds 		.__use		= 1,
3102c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
3111da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
3121da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
3131da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
3141da177e4SLinus Torvalds 	},
3151da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3161da177e4SLinus Torvalds };
3171da177e4SLinus Torvalds 
318101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
319101367c2SThomas Graf 
320fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
321101367c2SThomas Graf 	.dst = {
322101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
323101367c2SThomas Graf 		.__use		= 1,
3242c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
325101367c2SThomas Graf 		.error		= -EACCES,
3269ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
3279ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
328101367c2SThomas Graf 	},
329101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
330101367c2SThomas Graf };
331101367c2SThomas Graf 
332fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
333101367c2SThomas Graf 	.dst = {
334101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
335101367c2SThomas Graf 		.__use		= 1,
3362c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
337101367c2SThomas Graf 		.error		= -EINVAL,
338352e512cSHerbert Xu 		.input		= dst_discard,
339ede2059dSEric W. Biederman 		.output		= dst_discard_out,
340101367c2SThomas Graf 	},
341101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
342101367c2SThomas Graf };
343101367c2SThomas Graf 
344101367c2SThomas Graf #endif
345101367c2SThomas Graf 
346ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt)
347ebfa45f0SMartin KaFai Lau {
348ebfa45f0SMartin KaFai Lau 	struct dst_entry *dst = &rt->dst;
349ebfa45f0SMartin KaFai Lau 
350ebfa45f0SMartin KaFai Lau 	memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
351ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_uncached);
352ebfa45f0SMartin KaFai Lau }
353ebfa45f0SMartin KaFai Lau 
3541da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
35593531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
356ad706862SMartin KaFai Lau 			       int flags)
3571da177e4SLinus Torvalds {
35897bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
359b2a9c0edSWei Wang 					1, DST_OBSOLETE_FORCE_CHK, flags);
360cf911662SDavid S. Miller 
36181eb8447SWei Wang 	if (rt) {
362ebfa45f0SMartin KaFai Lau 		rt6_info_init(rt);
36381eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
36481eb8447SWei Wang 	}
3658104891bSSteffen Klassert 
366cf911662SDavid S. Miller 	return rt;
3671da177e4SLinus Torvalds }
3689ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc);
369d52d3997SMartin KaFai Lau 
3701da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
373a68886a6SDavid Ahern 	struct fib6_info *from;
3748d0b94afSMartin KaFai Lau 	struct inet6_dev *idev;
3751da177e4SLinus Torvalds 
3761620a336SDavid Ahern 	ip_dst_metrics_put(dst);
3778d0b94afSMartin KaFai Lau 	rt6_uncached_list_del(rt);
3788d0b94afSMartin KaFai Lau 
3798d0b94afSMartin KaFai Lau 	idev = rt->rt6i_idev;
38038308473SDavid S. Miller 	if (idev) {
3811da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3821da177e4SLinus Torvalds 		in6_dev_put(idev);
3831da177e4SLinus Torvalds 	}
3841716a961SGao feng 
3850e233874SEric Dumazet 	from = xchg((__force struct fib6_info **)&rt->from, NULL);
38693531c67SDavid Ahern 	fib6_info_release(from);
387b3419363SDavid S. Miller }
388b3419363SDavid S. Miller 
3891da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3901da177e4SLinus Torvalds 			   int how)
3911da177e4SLinus Torvalds {
3921da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3931da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3945a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
395c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3961da177e4SLinus Torvalds 
397e5645f51SWei Wang 	if (idev && idev->dev != loopback_dev) {
398e5645f51SWei Wang 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
39938308473SDavid S. Miller 		if (loopback_idev) {
4001da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
4011da177e4SLinus Torvalds 			in6_dev_put(idev);
4021da177e4SLinus Torvalds 		}
4031da177e4SLinus Torvalds 	}
40497cac082SDavid S. Miller }
4051da177e4SLinus Torvalds 
4065973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt)
4075973fb1eSMartin KaFai Lau {
4085973fb1eSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_EXPIRES)
4095973fb1eSMartin KaFai Lau 		return time_after(jiffies, rt->dst.expires);
4105973fb1eSMartin KaFai Lau 	else
4115973fb1eSMartin KaFai Lau 		return false;
4125973fb1eSMartin KaFai Lau }
4135973fb1eSMartin KaFai Lau 
414a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
4151da177e4SLinus Torvalds {
416a68886a6SDavid Ahern 	struct fib6_info *from;
417a68886a6SDavid Ahern 
418a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
419a68886a6SDavid Ahern 
4201716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
4211716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
422a50feda5SEric Dumazet 			return true;
423a68886a6SDavid Ahern 	} else if (from) {
4241e2ea8adSXin Long 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
425a68886a6SDavid Ahern 			fib6_check_expired(from);
4261716a961SGao feng 	}
427a50feda5SEric Dumazet 	return false;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
430b1d40991SDavid Ahern void fib6_select_path(const struct net *net, struct fib6_result *res,
431b1d40991SDavid Ahern 		      struct flowi6 *fl6, int oif, bool have_oif_match,
432b1d40991SDavid Ahern 		      const struct sk_buff *skb, int strict)
43351ebd318SNicolas Dichtel {
4348d1c802bSDavid Ahern 	struct fib6_info *sibling, *next_sibling;
435b1d40991SDavid Ahern 	struct fib6_info *match = res->f6i;
436b1d40991SDavid Ahern 
43734fe5a1cSDavid Ahern 	if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
438b1d40991SDavid Ahern 		goto out;
43951ebd318SNicolas Dichtel 
44034fe5a1cSDavid Ahern 	if (match->nh && have_oif_match && res->nh)
44134fe5a1cSDavid Ahern 		return;
44234fe5a1cSDavid Ahern 
443b673d6ccSJakub Sitnicki 	/* We might have already computed the hash for ICMPv6 errors. In such
444b673d6ccSJakub Sitnicki 	 * case it will always be non-zero. Otherwise now is the time to do it.
445b673d6ccSJakub Sitnicki 	 */
446f88d8ea6SDavid Ahern 	if (!fl6->mp_hash &&
447f88d8ea6SDavid Ahern 	    (!match->nh || nexthop_is_multipath(match->nh)))
448b4bac172SDavid Ahern 		fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
449b673d6ccSJakub Sitnicki 
450f88d8ea6SDavid Ahern 	if (unlikely(match->nh)) {
451f88d8ea6SDavid Ahern 		nexthop_path_fib6_result(res, fl6->mp_hash);
452f88d8ea6SDavid Ahern 		return;
453f88d8ea6SDavid Ahern 	}
454f88d8ea6SDavid Ahern 
4551cf844c7SDavid Ahern 	if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
456b1d40991SDavid Ahern 		goto out;
457bbfcd776SIdo Schimmel 
45893c2fb25SDavid Ahern 	list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
45993c2fb25SDavid Ahern 				 fib6_siblings) {
4601cf844c7SDavid Ahern 		const struct fib6_nh *nh = sibling->fib6_nh;
4615e670d84SDavid Ahern 		int nh_upper_bound;
4625e670d84SDavid Ahern 
463702cea56SDavid Ahern 		nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
4645e670d84SDavid Ahern 		if (fl6->mp_hash > nh_upper_bound)
4653d709f69SIdo Schimmel 			continue;
466702cea56SDavid Ahern 		if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
46752bd4c0cSNicolas Dichtel 			break;
46851ebd318SNicolas Dichtel 		match = sibling;
46951ebd318SNicolas Dichtel 		break;
47051ebd318SNicolas Dichtel 	}
4713d709f69SIdo Schimmel 
472b1d40991SDavid Ahern out:
473b1d40991SDavid Ahern 	res->f6i = match;
4741cf844c7SDavid Ahern 	res->nh = match->fib6_nh;
47551ebd318SNicolas Dichtel }
47651ebd318SNicolas Dichtel 
4771da177e4SLinus Torvalds /*
47866f5d6ceSWei Wang  *	Route lookup. rcu_read_lock() should be held.
4791da177e4SLinus Torvalds  */
4801da177e4SLinus Torvalds 
4810c59d006SDavid Ahern static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
4820c59d006SDavid Ahern 			       const struct in6_addr *saddr, int oif, int flags)
4830c59d006SDavid Ahern {
4840c59d006SDavid Ahern 	const struct net_device *dev;
4850c59d006SDavid Ahern 
4860c59d006SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD)
4870c59d006SDavid Ahern 		return false;
4880c59d006SDavid Ahern 
4890c59d006SDavid Ahern 	dev = nh->fib_nh_dev;
4900c59d006SDavid Ahern 	if (oif) {
4910c59d006SDavid Ahern 		if (dev->ifindex == oif)
4920c59d006SDavid Ahern 			return true;
4930c59d006SDavid Ahern 	} else {
4940c59d006SDavid Ahern 		if (ipv6_chk_addr(net, saddr, dev,
4950c59d006SDavid Ahern 				  flags & RT6_LOOKUP_F_IFACE))
4960c59d006SDavid Ahern 			return true;
4970c59d006SDavid Ahern 	}
4980c59d006SDavid Ahern 
4990c59d006SDavid Ahern 	return false;
5000c59d006SDavid Ahern }
5010c59d006SDavid Ahern 
502962b6803SDavid Ahern struct fib6_nh_dm_arg {
503962b6803SDavid Ahern 	struct net		*net;
504962b6803SDavid Ahern 	const struct in6_addr	*saddr;
505962b6803SDavid Ahern 	int			oif;
506962b6803SDavid Ahern 	int			flags;
507962b6803SDavid Ahern 	struct fib6_nh		*nh;
508962b6803SDavid Ahern };
509962b6803SDavid Ahern 
510962b6803SDavid Ahern static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
511962b6803SDavid Ahern {
512962b6803SDavid Ahern 	struct fib6_nh_dm_arg *arg = _arg;
513962b6803SDavid Ahern 
514962b6803SDavid Ahern 	arg->nh = nh;
515962b6803SDavid Ahern 	return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
516962b6803SDavid Ahern 				  arg->flags);
517962b6803SDavid Ahern }
518962b6803SDavid Ahern 
519962b6803SDavid Ahern /* returns fib6_nh from nexthop or NULL */
520962b6803SDavid Ahern static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
521962b6803SDavid Ahern 					struct fib6_result *res,
522962b6803SDavid Ahern 					const struct in6_addr *saddr,
523962b6803SDavid Ahern 					int oif, int flags)
524962b6803SDavid Ahern {
525962b6803SDavid Ahern 	struct fib6_nh_dm_arg arg = {
526962b6803SDavid Ahern 		.net   = net,
527962b6803SDavid Ahern 		.saddr = saddr,
528962b6803SDavid Ahern 		.oif   = oif,
529962b6803SDavid Ahern 		.flags = flags,
530962b6803SDavid Ahern 	};
531962b6803SDavid Ahern 
532962b6803SDavid Ahern 	if (nexthop_is_blackhole(nh))
533962b6803SDavid Ahern 		return NULL;
534962b6803SDavid Ahern 
535962b6803SDavid Ahern 	if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
536962b6803SDavid Ahern 		return arg.nh;
537962b6803SDavid Ahern 
538962b6803SDavid Ahern 	return NULL;
539962b6803SDavid Ahern }
540962b6803SDavid Ahern 
54175ef7389SDavid Ahern static void rt6_device_match(struct net *net, struct fib6_result *res,
54275ef7389SDavid Ahern 			     const struct in6_addr *saddr, int oif, int flags)
5431da177e4SLinus Torvalds {
54475ef7389SDavid Ahern 	struct fib6_info *f6i = res->f6i;
54575ef7389SDavid Ahern 	struct fib6_info *spf6i;
54675ef7389SDavid Ahern 	struct fib6_nh *nh;
5471da177e4SLinus Torvalds 
54875ef7389SDavid Ahern 	if (!oif && ipv6_addr_any(saddr)) {
549f88d8ea6SDavid Ahern 		if (unlikely(f6i->nh)) {
550f88d8ea6SDavid Ahern 			nh = nexthop_fib6_nh(f6i->nh);
551f88d8ea6SDavid Ahern 			if (nexthop_is_blackhole(f6i->nh))
552f88d8ea6SDavid Ahern 				goto out_blackhole;
553f88d8ea6SDavid Ahern 		} else {
5541cf844c7SDavid Ahern 			nh = f6i->fib6_nh;
555f88d8ea6SDavid Ahern 		}
5567d21fec9SDavid Ahern 		if (!(nh->fib_nh_flags & RTNH_F_DEAD))
5577d21fec9SDavid Ahern 			goto out;
5581da177e4SLinus Torvalds 	}
5591da177e4SLinus Torvalds 
56075ef7389SDavid Ahern 	for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
561962b6803SDavid Ahern 		bool matched = false;
562962b6803SDavid Ahern 
563962b6803SDavid Ahern 		if (unlikely(spf6i->nh)) {
564962b6803SDavid Ahern 			nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
565962b6803SDavid Ahern 					      oif, flags);
566962b6803SDavid Ahern 			if (nh)
567962b6803SDavid Ahern 				matched = true;
568962b6803SDavid Ahern 		} else {
5691cf844c7SDavid Ahern 			nh = spf6i->fib6_nh;
570962b6803SDavid Ahern 			if (__rt6_device_match(net, nh, saddr, oif, flags))
571962b6803SDavid Ahern 				matched = true;
572962b6803SDavid Ahern 		}
573962b6803SDavid Ahern 		if (matched) {
57475ef7389SDavid Ahern 			res->f6i = spf6i;
5757d21fec9SDavid Ahern 			goto out;
57675ef7389SDavid Ahern 		}
57775ef7389SDavid Ahern 	}
5781da177e4SLinus Torvalds 
57975ef7389SDavid Ahern 	if (oif && flags & RT6_LOOKUP_F_IFACE) {
58075ef7389SDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
5811cf844c7SDavid Ahern 		nh = res->f6i->fib6_nh;
5827d21fec9SDavid Ahern 		goto out;
58375ef7389SDavid Ahern 	}
58475ef7389SDavid Ahern 
585f88d8ea6SDavid Ahern 	if (unlikely(f6i->nh)) {
586f88d8ea6SDavid Ahern 		nh = nexthop_fib6_nh(f6i->nh);
587f88d8ea6SDavid Ahern 		if (nexthop_is_blackhole(f6i->nh))
588f88d8ea6SDavid Ahern 			goto out_blackhole;
589f88d8ea6SDavid Ahern 	} else {
5901cf844c7SDavid Ahern 		nh = f6i->fib6_nh;
591f88d8ea6SDavid Ahern 	}
592f88d8ea6SDavid Ahern 
5937d21fec9SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD) {
59475ef7389SDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
5951cf844c7SDavid Ahern 		nh = res->f6i->fib6_nh;
59675ef7389SDavid Ahern 	}
5977d21fec9SDavid Ahern out:
5987d21fec9SDavid Ahern 	res->nh = nh;
5997d21fec9SDavid Ahern 	res->fib6_type = res->f6i->fib6_type;
6007d21fec9SDavid Ahern 	res->fib6_flags = res->f6i->fib6_flags;
601f88d8ea6SDavid Ahern 	return;
602f88d8ea6SDavid Ahern 
603f88d8ea6SDavid Ahern out_blackhole:
604f88d8ea6SDavid Ahern 	res->fib6_flags |= RTF_REJECT;
605f88d8ea6SDavid Ahern 	res->fib6_type = RTN_BLACKHOLE;
606f88d8ea6SDavid Ahern 	res->nh = nh;
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
60927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
610c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
611c2f17e82SHannes Frederic Sowa 	struct work_struct work;
612c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
613c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
614c2f17e82SHannes Frederic Sowa };
615c2f17e82SHannes Frederic Sowa 
616c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
617c2f17e82SHannes Frederic Sowa {
618c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
619c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
620c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
621c2f17e82SHannes Frederic Sowa 
622c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
623adc176c5SErik Nordmark 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
624c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
625662f5533SMichael Büsch 	kfree(work);
626c2f17e82SHannes Frederic Sowa }
627c2f17e82SHannes Frederic Sowa 
628cc3a86c8SDavid Ahern static void rt6_probe(struct fib6_nh *fib6_nh)
62927097255SYOSHIFUJI Hideaki {
630f547fac6SSabrina Dubroca 	struct __rt6_probe_work *work = NULL;
6315e670d84SDavid Ahern 	const struct in6_addr *nh_gw;
6321bef4c22SEric Dumazet 	unsigned long last_probe;
633f2c31e32SEric Dumazet 	struct neighbour *neigh;
6345e670d84SDavid Ahern 	struct net_device *dev;
635f547fac6SSabrina Dubroca 	struct inet6_dev *idev;
6365e670d84SDavid Ahern 
63727097255SYOSHIFUJI Hideaki 	/*
63827097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
63927097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
64027097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
64127097255SYOSHIFUJI Hideaki 	 *
64227097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
64327097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
64427097255SYOSHIFUJI Hideaki 	 */
645004b3942SHangbin Liu 	if (!fib6_nh->fib_nh_gw_family)
646fdd6681dSAmerigo Wang 		return;
6475e670d84SDavid Ahern 
648cc3a86c8SDavid Ahern 	nh_gw = &fib6_nh->fib_nh_gw6;
649cc3a86c8SDavid Ahern 	dev = fib6_nh->fib_nh_dev;
6502152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
6511bef4c22SEric Dumazet 	last_probe = READ_ONCE(fib6_nh->last_probe);
652f547fac6SSabrina Dubroca 	idev = __in6_dev_get(dev);
6535e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
6542152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
6558d6c31bfSMartin KaFai Lau 		if (neigh->nud_state & NUD_VALID)
6568d6c31bfSMartin KaFai Lau 			goto out;
6578d6c31bfSMartin KaFai Lau 
6582152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
659990edb42SMartin KaFai Lau 		if (!(neigh->nud_state & NUD_VALID) &&
660990edb42SMartin KaFai Lau 		    time_after(jiffies,
661dcd1f572SDavid Ahern 			       neigh->updated + idev->cnf.rtr_probe_interval)) {
662c2f17e82SHannes Frederic Sowa 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
663990edb42SMartin KaFai Lau 			if (work)
6647e980569SJiri Benc 				__neigh_set_probe_once(neigh);
665990edb42SMartin KaFai Lau 		}
666c2f17e82SHannes Frederic Sowa 		write_unlock(&neigh->lock);
6671bef4c22SEric Dumazet 	} else if (time_after(jiffies, last_probe +
668f547fac6SSabrina Dubroca 				       idev->cnf.rtr_probe_interval)) {
669990edb42SMartin KaFai Lau 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
670990edb42SMartin KaFai Lau 	}
671c2f17e82SHannes Frederic Sowa 
6721bef4c22SEric Dumazet 	if (!work || cmpxchg(&fib6_nh->last_probe,
6731bef4c22SEric Dumazet 			     last_probe, jiffies) != last_probe) {
6741bef4c22SEric Dumazet 		kfree(work);
6751bef4c22SEric Dumazet 	} else {
676c2f17e82SHannes Frederic Sowa 		INIT_WORK(&work->work, rt6_probe_deferred);
6775e670d84SDavid Ahern 		work->target = *nh_gw;
6785e670d84SDavid Ahern 		dev_hold(dev);
6795e670d84SDavid Ahern 		work->dev = dev;
680c2f17e82SHannes Frederic Sowa 		schedule_work(&work->work);
681c2f17e82SHannes Frederic Sowa 	}
682990edb42SMartin KaFai Lau 
6838d6c31bfSMartin KaFai Lau out:
6842152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
685f2c31e32SEric Dumazet }
68627097255SYOSHIFUJI Hideaki #else
687cc3a86c8SDavid Ahern static inline void rt6_probe(struct fib6_nh *fib6_nh)
68827097255SYOSHIFUJI Hideaki {
68927097255SYOSHIFUJI Hideaki }
69027097255SYOSHIFUJI Hideaki #endif
69127097255SYOSHIFUJI Hideaki 
6921da177e4SLinus Torvalds /*
693554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
6941da177e4SLinus Torvalds  */
6951ba9a895SDavid Ahern static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
6961da177e4SLinus Torvalds {
697afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
6985e670d84SDavid Ahern 	struct neighbour *neigh;
699f2c31e32SEric Dumazet 
700145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
7011ba9a895SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
7021ba9a895SDavid Ahern 					  &fib6_nh->fib_nh_gw6);
703145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
704145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
705554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
706afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
707398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
708a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
709afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
7107e980569SJiri Benc 		else
7117e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
712398bcbebSYOSHIFUJI Hideaki #endif
713145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
714afc154e9SHannes Frederic Sowa 	} else {
715afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
7167e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
717a5a81f0bSPaul Marks 	}
718145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
719145a3621SYOSHIFUJI Hideaki / 吉藤英明 
720a5a81f0bSPaul Marks 	return ret;
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
723702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
724702cea56SDavid Ahern 			   int strict)
725554cfb7eSYOSHIFUJI Hideaki {
7266e1809a5SDavid Ahern 	int m = 0;
7274d0c5911SYOSHIFUJI Hideaki 
7286e1809a5SDavid Ahern 	if (!oif || nh->fib_nh_dev->ifindex == oif)
7296e1809a5SDavid Ahern 		m = 2;
7306e1809a5SDavid Ahern 
73177d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
732afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
733ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
734702cea56SDavid Ahern 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
735ebacaaa0SYOSHIFUJI Hideaki #endif
7361ba9a895SDavid Ahern 	if ((strict & RT6_LOOKUP_F_REACHABLE) &&
737702cea56SDavid Ahern 	    !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
7381ba9a895SDavid Ahern 		int n = rt6_check_neigh(nh);
739afc154e9SHannes Frederic Sowa 		if (n < 0)
740afc154e9SHannes Frederic Sowa 			return n;
741afc154e9SHannes Frederic Sowa 	}
742554cfb7eSYOSHIFUJI Hideaki 	return m;
743554cfb7eSYOSHIFUJI Hideaki }
744554cfb7eSYOSHIFUJI Hideaki 
74528679ed1SDavid Ahern static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
74628679ed1SDavid Ahern 		       int oif, int strict, int *mpri, bool *do_rr)
747554cfb7eSYOSHIFUJI Hideaki {
748afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
74928679ed1SDavid Ahern 	bool rc = false;
75028679ed1SDavid Ahern 	int m;
75135103d11SAndy Gospodarek 
75228679ed1SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD)
7538067bb8cSIdo Schimmel 		goto out;
7548067bb8cSIdo Schimmel 
75528679ed1SDavid Ahern 	if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
75628679ed1SDavid Ahern 	    nh->fib_nh_flags & RTNH_F_LINKDOWN &&
757d5d32e4bSDavid Ahern 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
75835103d11SAndy Gospodarek 		goto out;
759554cfb7eSYOSHIFUJI Hideaki 
76028679ed1SDavid Ahern 	m = rt6_score_route(nh, fib6_flags, oif, strict);
7617e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
762afc154e9SHannes Frederic Sowa 		match_do_rr = true;
763afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
7647e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
765f11e6659SDavid S. Miller 		goto out;
7661da177e4SLinus Torvalds 	}
767f11e6659SDavid S. Miller 
768afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
76928679ed1SDavid Ahern 		rt6_probe(nh);
770afc154e9SHannes Frederic Sowa 
7717e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
772afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
773afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
774afc154e9SHannes Frederic Sowa 		*mpri = m;
77528679ed1SDavid Ahern 		rc = true;
776afc154e9SHannes Frederic Sowa 	}
777f11e6659SDavid S. Miller out:
77828679ed1SDavid Ahern 	return rc;
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
78117a5984eSDavid Ahern struct fib6_nh_frl_arg {
78217a5984eSDavid Ahern 	u32		flags;
78317a5984eSDavid Ahern 	int		oif;
78417a5984eSDavid Ahern 	int		strict;
78517a5984eSDavid Ahern 	int		*mpri;
78617a5984eSDavid Ahern 	bool		*do_rr;
78717a5984eSDavid Ahern 	struct fib6_nh	*nh;
78817a5984eSDavid Ahern };
78917a5984eSDavid Ahern 
79017a5984eSDavid Ahern static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
79117a5984eSDavid Ahern {
79217a5984eSDavid Ahern 	struct fib6_nh_frl_arg *arg = _arg;
79317a5984eSDavid Ahern 
79417a5984eSDavid Ahern 	arg->nh = nh;
79517a5984eSDavid Ahern 	return find_match(nh, arg->flags, arg->oif, arg->strict,
79617a5984eSDavid Ahern 			  arg->mpri, arg->do_rr);
79717a5984eSDavid Ahern }
79817a5984eSDavid Ahern 
799b7bc4b6aSDavid Ahern static void __find_rr_leaf(struct fib6_info *f6i_start,
80030c15f03SDavid Ahern 			   struct fib6_info *nomatch, u32 metric,
801b7bc4b6aSDavid Ahern 			   struct fib6_result *res, struct fib6_info **cont,
80230c15f03SDavid Ahern 			   int oif, int strict, bool *do_rr, int *mpri)
80330c15f03SDavid Ahern {
804b7bc4b6aSDavid Ahern 	struct fib6_info *f6i;
80530c15f03SDavid Ahern 
806b7bc4b6aSDavid Ahern 	for (f6i = f6i_start;
807b7bc4b6aSDavid Ahern 	     f6i && f6i != nomatch;
808b7bc4b6aSDavid Ahern 	     f6i = rcu_dereference(f6i->fib6_next)) {
80917a5984eSDavid Ahern 		bool matched = false;
81030c15f03SDavid Ahern 		struct fib6_nh *nh;
81130c15f03SDavid Ahern 
812b7bc4b6aSDavid Ahern 		if (cont && f6i->fib6_metric != metric) {
813b7bc4b6aSDavid Ahern 			*cont = f6i;
81430c15f03SDavid Ahern 			return;
81530c15f03SDavid Ahern 		}
81630c15f03SDavid Ahern 
817b7bc4b6aSDavid Ahern 		if (fib6_check_expired(f6i))
81830c15f03SDavid Ahern 			continue;
81930c15f03SDavid Ahern 
82017a5984eSDavid Ahern 		if (unlikely(f6i->nh)) {
82117a5984eSDavid Ahern 			struct fib6_nh_frl_arg arg = {
82217a5984eSDavid Ahern 				.flags  = f6i->fib6_flags,
82317a5984eSDavid Ahern 				.oif    = oif,
82417a5984eSDavid Ahern 				.strict = strict,
82517a5984eSDavid Ahern 				.mpri   = mpri,
82617a5984eSDavid Ahern 				.do_rr  = do_rr
82717a5984eSDavid Ahern 			};
82817a5984eSDavid Ahern 
82917a5984eSDavid Ahern 			if (nexthop_is_blackhole(f6i->nh)) {
83017a5984eSDavid Ahern 				res->fib6_flags = RTF_REJECT;
83117a5984eSDavid Ahern 				res->fib6_type = RTN_BLACKHOLE;
83217a5984eSDavid Ahern 				res->f6i = f6i;
83317a5984eSDavid Ahern 				res->nh = nexthop_fib6_nh(f6i->nh);
83417a5984eSDavid Ahern 				return;
83517a5984eSDavid Ahern 			}
83617a5984eSDavid Ahern 			if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
83717a5984eSDavid Ahern 						     &arg)) {
83817a5984eSDavid Ahern 				matched = true;
83917a5984eSDavid Ahern 				nh = arg.nh;
84017a5984eSDavid Ahern 			}
84117a5984eSDavid Ahern 		} else {
8421cf844c7SDavid Ahern 			nh = f6i->fib6_nh;
84317a5984eSDavid Ahern 			if (find_match(nh, f6i->fib6_flags, oif, strict,
84417a5984eSDavid Ahern 				       mpri, do_rr))
84517a5984eSDavid Ahern 				matched = true;
84617a5984eSDavid Ahern 		}
84717a5984eSDavid Ahern 		if (matched) {
848b7bc4b6aSDavid Ahern 			res->f6i = f6i;
849b7bc4b6aSDavid Ahern 			res->nh = nh;
8507d21fec9SDavid Ahern 			res->fib6_flags = f6i->fib6_flags;
8517d21fec9SDavid Ahern 			res->fib6_type = f6i->fib6_type;
852b7bc4b6aSDavid Ahern 		}
85330c15f03SDavid Ahern 	}
85430c15f03SDavid Ahern }
85530c15f03SDavid Ahern 
856b7bc4b6aSDavid Ahern static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
857b7bc4b6aSDavid Ahern 			 struct fib6_info *rr_head, int oif, int strict,
858b7bc4b6aSDavid Ahern 			 bool *do_rr, struct fib6_result *res)
859f11e6659SDavid S. Miller {
860b7bc4b6aSDavid Ahern 	u32 metric = rr_head->fib6_metric;
861b7bc4b6aSDavid Ahern 	struct fib6_info *cont = NULL;
862f11e6659SDavid S. Miller 	int mpri = -1;
863f11e6659SDavid S. Miller 
864b7bc4b6aSDavid Ahern 	__find_rr_leaf(rr_head, NULL, metric, res, &cont,
86530c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
8669fbdcfafSSteffen Klassert 
867b7bc4b6aSDavid Ahern 	__find_rr_leaf(leaf, rr_head, metric, res, &cont,
86830c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
8699fbdcfafSSteffen Klassert 
870b7bc4b6aSDavid Ahern 	if (res->f6i || !cont)
871b7bc4b6aSDavid Ahern 		return;
8729fbdcfafSSteffen Klassert 
873b7bc4b6aSDavid Ahern 	__find_rr_leaf(cont, NULL, metric, res, NULL,
87430c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
875f11e6659SDavid S. Miller }
876f11e6659SDavid S. Miller 
877b7bc4b6aSDavid Ahern static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
878b7bc4b6aSDavid Ahern 		       struct fib6_result *res, int strict)
879f11e6659SDavid S. Miller {
8808d1c802bSDavid Ahern 	struct fib6_info *leaf = rcu_dereference(fn->leaf);
881b7bc4b6aSDavid Ahern 	struct fib6_info *rt0;
882afc154e9SHannes Frederic Sowa 	bool do_rr = false;
88317ecf590SWei Wang 	int key_plen;
884f11e6659SDavid S. Miller 
885b7bc4b6aSDavid Ahern 	/* make sure this function or its helpers sets f6i */
886b7bc4b6aSDavid Ahern 	res->f6i = NULL;
887b7bc4b6aSDavid Ahern 
888421842edSDavid Ahern 	if (!leaf || leaf == net->ipv6.fib6_null_entry)
889b7bc4b6aSDavid Ahern 		goto out;
8908d1040e8SWei Wang 
89166f5d6ceSWei Wang 	rt0 = rcu_dereference(fn->rr_ptr);
892f11e6659SDavid S. Miller 	if (!rt0)
89366f5d6ceSWei Wang 		rt0 = leaf;
894f11e6659SDavid S. Miller 
89517ecf590SWei Wang 	/* Double check to make sure fn is not an intermediate node
89617ecf590SWei Wang 	 * and fn->leaf does not points to its child's leaf
89717ecf590SWei Wang 	 * (This might happen if all routes under fn are deleted from
89817ecf590SWei Wang 	 * the tree and fib6_repair_tree() is called on the node.)
89917ecf590SWei Wang 	 */
90093c2fb25SDavid Ahern 	key_plen = rt0->fib6_dst.plen;
90117ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES
90293c2fb25SDavid Ahern 	if (rt0->fib6_src.plen)
90393c2fb25SDavid Ahern 		key_plen = rt0->fib6_src.plen;
90417ecf590SWei Wang #endif
90517ecf590SWei Wang 	if (fn->fn_bit != key_plen)
906b7bc4b6aSDavid Ahern 		goto out;
90717ecf590SWei Wang 
908b7bc4b6aSDavid Ahern 	find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
909afc154e9SHannes Frederic Sowa 	if (do_rr) {
9108fb11a9aSDavid Ahern 		struct fib6_info *next = rcu_dereference(rt0->fib6_next);
911f11e6659SDavid S. Miller 
912554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
91393c2fb25SDavid Ahern 		if (!next || next->fib6_metric != rt0->fib6_metric)
9148d1040e8SWei Wang 			next = leaf;
915f11e6659SDavid S. Miller 
91666f5d6ceSWei Wang 		if (next != rt0) {
91793c2fb25SDavid Ahern 			spin_lock_bh(&leaf->fib6_table->tb6_lock);
91866f5d6ceSWei Wang 			/* make sure next is not being deleted from the tree */
91993c2fb25SDavid Ahern 			if (next->fib6_node)
92066f5d6ceSWei Wang 				rcu_assign_pointer(fn->rr_ptr, next);
92193c2fb25SDavid Ahern 			spin_unlock_bh(&leaf->fib6_table->tb6_lock);
92266f5d6ceSWei Wang 		}
923554cfb7eSYOSHIFUJI Hideaki 	}
924554cfb7eSYOSHIFUJI Hideaki 
925b7bc4b6aSDavid Ahern out:
926b7bc4b6aSDavid Ahern 	if (!res->f6i) {
927b7bc4b6aSDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
9281cf844c7SDavid Ahern 		res->nh = res->f6i->fib6_nh;
9297d21fec9SDavid Ahern 		res->fib6_flags = res->f6i->fib6_flags;
9307d21fec9SDavid Ahern 		res->fib6_type = res->f6i->fib6_type;
931b7bc4b6aSDavid Ahern 	}
9321da177e4SLinus Torvalds }
9331da177e4SLinus Torvalds 
93485bd05deSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
9358b9df265SMartin KaFai Lau {
93685bd05deSDavid Ahern 	return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
93785bd05deSDavid Ahern 	       res->nh->fib_nh_gw_family;
9388b9df265SMartin KaFai Lau }
9398b9df265SMartin KaFai Lau 
94070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
94170ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
942b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
94370ceb4f5SYOSHIFUJI Hideaki {
944c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
94570ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
94670ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
94770ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
9484bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
9498d1c802bSDavid Ahern 	struct fib6_info *rt;
95070ceb4f5SYOSHIFUJI Hideaki 
95170ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
95270ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
95370ceb4f5SYOSHIFUJI Hideaki 	}
95470ceb4f5SYOSHIFUJI Hideaki 
95570ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
95670ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
95770ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
95870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
95970ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
96070ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
96170ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
96270ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
96370ceb4f5SYOSHIFUJI Hideaki 		}
96470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
96570ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
96670ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
96770ceb4f5SYOSHIFUJI Hideaki 		}
96870ceb4f5SYOSHIFUJI Hideaki 	}
96970ceb4f5SYOSHIFUJI Hideaki 
97070ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
97170ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
9723933fc95SJens Rosenboom 		return -EINVAL;
97370ceb4f5SYOSHIFUJI Hideaki 
9744bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
97570ceb4f5SYOSHIFUJI Hideaki 
97670ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
97770ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
97870ceb4f5SYOSHIFUJI Hideaki 	else {
97970ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
98070ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
98170ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
98270ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
98370ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
98470ceb4f5SYOSHIFUJI Hideaki 	}
98570ceb4f5SYOSHIFUJI Hideaki 
986f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
987afb1d4b5SDavid Ahern 		rt = rt6_get_dflt_router(net, gwaddr, dev);
988f104a567SDuan Jiong 	else
989f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
990830218c1SDavid Ahern 					gwaddr, dev);
99170ceb4f5SYOSHIFUJI Hideaki 
99270ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
99311dd74b3SRoopa Prabhu 		ip6_del_rt(net, rt, false);
99470ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
99570ceb4f5SYOSHIFUJI Hideaki 	}
99670ceb4f5SYOSHIFUJI Hideaki 
99770ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
998830218c1SDavid Ahern 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
999830218c1SDavid Ahern 					dev, pref);
100070ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
100193c2fb25SDavid Ahern 		rt->fib6_flags = RTF_ROUTEINFO |
100293c2fb25SDavid Ahern 				 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
100370ceb4f5SYOSHIFUJI Hideaki 
100470ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
10051716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
100614895687SDavid Ahern 			fib6_clean_expires(rt);
10071716a961SGao feng 		else
100814895687SDavid Ahern 			fib6_set_expires(rt, jiffies + HZ * lifetime);
10091716a961SGao feng 
101093531c67SDavid Ahern 		fib6_info_release(rt);
101170ceb4f5SYOSHIFUJI Hideaki 	}
101270ceb4f5SYOSHIFUJI Hideaki 	return 0;
101370ceb4f5SYOSHIFUJI Hideaki }
101470ceb4f5SYOSHIFUJI Hideaki #endif
101570ceb4f5SYOSHIFUJI Hideaki 
1016ae90d867SDavid Ahern /*
1017ae90d867SDavid Ahern  *	Misc support functions
1018ae90d867SDavid Ahern  */
1019ae90d867SDavid Ahern 
1020ae90d867SDavid Ahern /* called with rcu_lock held */
10210d161581SDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
1022ae90d867SDavid Ahern {
10230d161581SDavid Ahern 	struct net_device *dev = res->nh->fib_nh_dev;
1024ae90d867SDavid Ahern 
10257d21fec9SDavid Ahern 	if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
1026ae90d867SDavid Ahern 		/* for copies of local routes, dst->dev needs to be the
1027ae90d867SDavid Ahern 		 * device if it is a master device, the master device if
1028ae90d867SDavid Ahern 		 * device is enslaved, and the loopback as the default
1029ae90d867SDavid Ahern 		 */
1030ae90d867SDavid Ahern 		if (netif_is_l3_slave(dev) &&
10317d21fec9SDavid Ahern 		    !rt6_need_strict(&res->f6i->fib6_dst.addr))
1032ae90d867SDavid Ahern 			dev = l3mdev_master_dev_rcu(dev);
1033ae90d867SDavid Ahern 		else if (!netif_is_l3_master(dev))
1034ae90d867SDavid Ahern 			dev = dev_net(dev)->loopback_dev;
1035ae90d867SDavid Ahern 		/* last case is netif_is_l3_master(dev) is true in which
1036ae90d867SDavid Ahern 		 * case we want dev returned to be dev
1037ae90d867SDavid Ahern 		 */
1038ae90d867SDavid Ahern 	}
1039ae90d867SDavid Ahern 
1040ae90d867SDavid Ahern 	return dev;
1041ae90d867SDavid Ahern }
1042ae90d867SDavid Ahern 
10436edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = {
10446edb3c96SDavid Ahern 	[RTN_UNSPEC]	= 0,
10456edb3c96SDavid Ahern 	[RTN_UNICAST]	= 0,
10466edb3c96SDavid Ahern 	[RTN_LOCAL]	= 0,
10476edb3c96SDavid Ahern 	[RTN_BROADCAST]	= 0,
10486edb3c96SDavid Ahern 	[RTN_ANYCAST]	= 0,
10496edb3c96SDavid Ahern 	[RTN_MULTICAST]	= 0,
10506edb3c96SDavid Ahern 	[RTN_BLACKHOLE]	= -EINVAL,
10516edb3c96SDavid Ahern 	[RTN_UNREACHABLE] = -EHOSTUNREACH,
10526edb3c96SDavid Ahern 	[RTN_PROHIBIT]	= -EACCES,
10536edb3c96SDavid Ahern 	[RTN_THROW]	= -EAGAIN,
10546edb3c96SDavid Ahern 	[RTN_NAT]	= -EINVAL,
10556edb3c96SDavid Ahern 	[RTN_XRESOLVE]	= -EINVAL,
10566edb3c96SDavid Ahern };
10576edb3c96SDavid Ahern 
10586edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type)
10596edb3c96SDavid Ahern {
10606edb3c96SDavid Ahern 	return fib6_prop[fib6_type];
10616edb3c96SDavid Ahern }
10626edb3c96SDavid Ahern 
10638d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
10643b6761d1SDavid Ahern {
10653b6761d1SDavid Ahern 	unsigned short flags = 0;
10663b6761d1SDavid Ahern 
10673b6761d1SDavid Ahern 	if (rt->dst_nocount)
10683b6761d1SDavid Ahern 		flags |= DST_NOCOUNT;
10693b6761d1SDavid Ahern 	if (rt->dst_nopolicy)
10703b6761d1SDavid Ahern 		flags |= DST_NOPOLICY;
10713b6761d1SDavid Ahern 
10723b6761d1SDavid Ahern 	return flags;
10733b6761d1SDavid Ahern }
10743b6761d1SDavid Ahern 
10757d21fec9SDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
10766edb3c96SDavid Ahern {
10777d21fec9SDavid Ahern 	rt->dst.error = ip6_rt_type_to_error(fib6_type);
10786edb3c96SDavid Ahern 
10797d21fec9SDavid Ahern 	switch (fib6_type) {
10806edb3c96SDavid Ahern 	case RTN_BLACKHOLE:
10816edb3c96SDavid Ahern 		rt->dst.output = dst_discard_out;
10826edb3c96SDavid Ahern 		rt->dst.input = dst_discard;
10836edb3c96SDavid Ahern 		break;
10846edb3c96SDavid Ahern 	case RTN_PROHIBIT:
10856edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_prohibit_out;
10866edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_prohibit;
10876edb3c96SDavid Ahern 		break;
10886edb3c96SDavid Ahern 	case RTN_THROW:
10896edb3c96SDavid Ahern 	case RTN_UNREACHABLE:
10906edb3c96SDavid Ahern 	default:
10916edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_discard_out;
10926edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_discard;
10936edb3c96SDavid Ahern 		break;
10946edb3c96SDavid Ahern 	}
10956edb3c96SDavid Ahern }
10966edb3c96SDavid Ahern 
10970d161581SDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
10986edb3c96SDavid Ahern {
10997d21fec9SDavid Ahern 	struct fib6_info *f6i = res->f6i;
11000d161581SDavid Ahern 
11017d21fec9SDavid Ahern 	if (res->fib6_flags & RTF_REJECT) {
11027d21fec9SDavid Ahern 		ip6_rt_init_dst_reject(rt, res->fib6_type);
11036edb3c96SDavid Ahern 		return;
11046edb3c96SDavid Ahern 	}
11056edb3c96SDavid Ahern 
11066edb3c96SDavid Ahern 	rt->dst.error = 0;
11076edb3c96SDavid Ahern 	rt->dst.output = ip6_output;
11086edb3c96SDavid Ahern 
11097d21fec9SDavid Ahern 	if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
11106edb3c96SDavid Ahern 		rt->dst.input = ip6_input;
11117d21fec9SDavid Ahern 	} else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
11126edb3c96SDavid Ahern 		rt->dst.input = ip6_mc_input;
11136edb3c96SDavid Ahern 	} else {
11146edb3c96SDavid Ahern 		rt->dst.input = ip6_forward;
11156edb3c96SDavid Ahern 	}
11166edb3c96SDavid Ahern 
11170d161581SDavid Ahern 	if (res->nh->fib_nh_lws) {
11180d161581SDavid Ahern 		rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
11196edb3c96SDavid Ahern 		lwtunnel_set_redirect(&rt->dst);
11206edb3c96SDavid Ahern 	}
11216edb3c96SDavid Ahern 
11226edb3c96SDavid Ahern 	rt->dst.lastuse = jiffies;
11236edb3c96SDavid Ahern }
11246edb3c96SDavid Ahern 
1125e873e4b9SWei Wang /* Caller must already hold reference to @from */
11268d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1127ae90d867SDavid Ahern {
1128ae90d867SDavid Ahern 	rt->rt6i_flags &= ~RTF_EXPIRES;
1129a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, from);
1130e1255ed4SDavid Ahern 	ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
1131ae90d867SDavid Ahern }
1132ae90d867SDavid Ahern 
11330d161581SDavid Ahern /* Caller must already hold reference to f6i in result */
11340d161581SDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
1135ae90d867SDavid Ahern {
11360d161581SDavid Ahern 	const struct fib6_nh *nh = res->nh;
11370d161581SDavid Ahern 	const struct net_device *dev = nh->fib_nh_dev;
11380d161581SDavid Ahern 	struct fib6_info *f6i = res->f6i;
1139dcd1f572SDavid Ahern 
11400d161581SDavid Ahern 	ip6_rt_init_dst(rt, res);
11416edb3c96SDavid Ahern 
11420d161581SDavid Ahern 	rt->rt6i_dst = f6i->fib6_dst;
1143dcd1f572SDavid Ahern 	rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
11447d21fec9SDavid Ahern 	rt->rt6i_flags = res->fib6_flags;
11450d161581SDavid Ahern 	if (nh->fib_nh_gw_family) {
11460d161581SDavid Ahern 		rt->rt6i_gateway = nh->fib_nh_gw6;
11472b2450caSDavid Ahern 		rt->rt6i_flags |= RTF_GATEWAY;
11482b2450caSDavid Ahern 	}
11490d161581SDavid Ahern 	rt6_set_from(rt, f6i);
1150ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
11510d161581SDavid Ahern 	rt->rt6i_src = f6i->fib6_src;
1152ae90d867SDavid Ahern #endif
1153ae90d867SDavid Ahern }
1154ae90d867SDavid Ahern 
1155a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1156a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
1157a3c00e46SMartin KaFai Lau {
115866f5d6ceSWei Wang 	struct fib6_node *pn, *sn;
1159a3c00e46SMartin KaFai Lau 	while (1) {
1160a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
1161a3c00e46SMartin KaFai Lau 			return NULL;
116266f5d6ceSWei Wang 		pn = rcu_dereference(fn->parent);
116366f5d6ceSWei Wang 		sn = FIB6_SUBTREE(pn);
116466f5d6ceSWei Wang 		if (sn && sn != fn)
11656454743bSDavid Ahern 			fn = fib6_node_lookup(sn, NULL, saddr);
1166a3c00e46SMartin KaFai Lau 		else
1167a3c00e46SMartin KaFai Lau 			fn = pn;
1168a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
1169a3c00e46SMartin KaFai Lau 			return fn;
1170a3c00e46SMartin KaFai Lau 	}
1171a3c00e46SMartin KaFai Lau }
1172c71099acSThomas Graf 
117310585b43SDavid Ahern static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1174d3843fe5SWei Wang {
1175d3843fe5SWei Wang 	struct rt6_info *rt = *prt;
1176d3843fe5SWei Wang 
1177d3843fe5SWei Wang 	if (dst_hold_safe(&rt->dst))
1178d3843fe5SWei Wang 		return true;
117910585b43SDavid Ahern 	if (net) {
1180d3843fe5SWei Wang 		rt = net->ipv6.ip6_null_entry;
1181d3843fe5SWei Wang 		dst_hold(&rt->dst);
1182d3843fe5SWei Wang 	} else {
1183d3843fe5SWei Wang 		rt = NULL;
1184d3843fe5SWei Wang 	}
1185d3843fe5SWei Wang 	*prt = rt;
1186d3843fe5SWei Wang 	return false;
1187d3843fe5SWei Wang }
1188d3843fe5SWei Wang 
1189dec9b0e2SDavid Ahern /* called with rcu_lock held */
11909b6b35abSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
1191dec9b0e2SDavid Ahern {
11929b6b35abSDavid Ahern 	struct net_device *dev = res->nh->fib_nh_dev;
11939b6b35abSDavid Ahern 	struct fib6_info *f6i = res->f6i;
11949b6b35abSDavid Ahern 	unsigned short flags;
1195dec9b0e2SDavid Ahern 	struct rt6_info *nrt;
1196dec9b0e2SDavid Ahern 
11979b6b35abSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
11981c87e79aSXin Long 		goto fallback;
1199e873e4b9SWei Wang 
12009b6b35abSDavid Ahern 	flags = fib6_info_dst_flags(f6i);
120193531c67SDavid Ahern 	nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
12021c87e79aSXin Long 	if (!nrt) {
12039b6b35abSDavid Ahern 		fib6_info_release(f6i);
12041c87e79aSXin Long 		goto fallback;
12051c87e79aSXin Long 	}
1206dec9b0e2SDavid Ahern 
12070d161581SDavid Ahern 	ip6_rt_copy_init(nrt, res);
12081c87e79aSXin Long 	return nrt;
12091c87e79aSXin Long 
12101c87e79aSXin Long fallback:
12111c87e79aSXin Long 	nrt = dev_net(dev)->ipv6.ip6_null_entry;
12121c87e79aSXin Long 	dst_hold(&nrt->dst);
1213dec9b0e2SDavid Ahern 	return nrt;
1214dec9b0e2SDavid Ahern }
1215dec9b0e2SDavid Ahern 
121655cced4fSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
12178ed67789SDaniel Lezcano 					     struct fib6_table *table,
1218b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
1219b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
1220b75cc8f9SDavid Ahern 					     int flags)
12211da177e4SLinus Torvalds {
1222b1d40991SDavid Ahern 	struct fib6_result res = {};
12231da177e4SLinus Torvalds 	struct fib6_node *fn;
122423fb93a4SDavid Ahern 	struct rt6_info *rt;
12251da177e4SLinus Torvalds 
1226b6cdbc85SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1227b6cdbc85SDavid Ahern 		flags &= ~RT6_LOOKUP_F_IFACE;
1228b6cdbc85SDavid Ahern 
122966f5d6ceSWei Wang 	rcu_read_lock();
12306454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1231c71099acSThomas Graf restart:
1232b1d40991SDavid Ahern 	res.f6i = rcu_dereference(fn->leaf);
1233b1d40991SDavid Ahern 	if (!res.f6i)
1234b1d40991SDavid Ahern 		res.f6i = net->ipv6.fib6_null_entry;
1235af52a52cSDavid Ahern 	else
123675ef7389SDavid Ahern 		rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
123775ef7389SDavid Ahern 				 flags);
1238af52a52cSDavid Ahern 
1239b1d40991SDavid Ahern 	if (res.f6i == net->ipv6.fib6_null_entry) {
1240a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1241a3c00e46SMartin KaFai Lau 		if (fn)
1242a3c00e46SMartin KaFai Lau 			goto restart;
1243af52a52cSDavid Ahern 
1244af52a52cSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
1245af52a52cSDavid Ahern 		dst_hold(&rt->dst);
1246af52a52cSDavid Ahern 		goto out;
1247f88d8ea6SDavid Ahern 	} else if (res.fib6_flags & RTF_REJECT) {
1248f88d8ea6SDavid Ahern 		goto do_create;
1249a3c00e46SMartin KaFai Lau 	}
12502b760fcfSWei Wang 
1251b1d40991SDavid Ahern 	fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1252b1d40991SDavid Ahern 			 fl6->flowi6_oif != 0, skb, flags);
1253b1d40991SDavid Ahern 
12544c9483b2SDavid S. Miller 	/* Search through exception table */
12557e4b5128SDavid Ahern 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
125623fb93a4SDavid Ahern 	if (rt) {
125710585b43SDavid Ahern 		if (ip6_hold_safe(net, &rt))
1258d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
125923fb93a4SDavid Ahern 	} else {
1260f88d8ea6SDavid Ahern do_create:
12619b6b35abSDavid Ahern 		rt = ip6_create_rt_rcu(&res);
1262dec9b0e2SDavid Ahern 	}
1263d3843fe5SWei Wang 
1264af52a52cSDavid Ahern out:
12658ff2e5b2SDavid Ahern 	trace_fib6_table_lookup(net, &res, table, fl6);
1266af52a52cSDavid Ahern 
126766f5d6ceSWei Wang 	rcu_read_unlock();
1268b811580dSDavid Ahern 
12691da177e4SLinus Torvalds 	return rt;
1270c71099acSThomas Graf }
1271c71099acSThomas Graf 
1272ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1273b75cc8f9SDavid Ahern 				   const struct sk_buff *skb, int flags)
1274ea6e574eSFlorian Westphal {
1275b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1276ea6e574eSFlorian Westphal }
1277ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
1278ea6e574eSFlorian Westphal 
12799acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1280b75cc8f9SDavid Ahern 			    const struct in6_addr *saddr, int oif,
1281b75cc8f9SDavid Ahern 			    const struct sk_buff *skb, int strict)
1282c71099acSThomas Graf {
12834c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
12844c9483b2SDavid S. Miller 		.flowi6_oif = oif,
12854c9483b2SDavid S. Miller 		.daddr = *daddr,
1286c71099acSThomas Graf 	};
1287c71099acSThomas Graf 	struct dst_entry *dst;
128877d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1289c71099acSThomas Graf 
1290adaa70bbSThomas Graf 	if (saddr) {
12914c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1292adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1293adaa70bbSThomas Graf 	}
1294adaa70bbSThomas Graf 
1295b75cc8f9SDavid Ahern 	dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1296c71099acSThomas Graf 	if (dst->error == 0)
1297c71099acSThomas Graf 		return (struct rt6_info *) dst;
1298c71099acSThomas Graf 
1299c71099acSThomas Graf 	dst_release(dst);
1300c71099acSThomas Graf 
13011da177e4SLinus Torvalds 	return NULL;
13021da177e4SLinus Torvalds }
13037159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
13047159039aSYOSHIFUJI Hideaki 
1305c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
13061cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
13071cfb71eeSWei Wang  * route is released.
13081cfb71eeSWei Wang  * Caller must hold dst before calling it.
13091da177e4SLinus Torvalds  */
13101da177e4SLinus Torvalds 
13118d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1312333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
13131da177e4SLinus Torvalds {
13141da177e4SLinus Torvalds 	int err;
1315c71099acSThomas Graf 	struct fib6_table *table;
13161da177e4SLinus Torvalds 
131793c2fb25SDavid Ahern 	table = rt->fib6_table;
131866f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
1319d4ead6b3SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, extack);
132066f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	return err;
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds 
13258d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt)
132640e22e8fSThomas Graf {
1327afb1d4b5SDavid Ahern 	struct nl_info info = {	.nl_net = net, };
1328e715b6d3SFlorian Westphal 
1329d4ead6b3SDavid Ahern 	return __ip6_ins_rt(rt, &info, NULL);
133040e22e8fSThomas Graf }
133140e22e8fSThomas Graf 
133285bd05deSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
133321efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
1334b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
13351da177e4SLinus Torvalds {
133685bd05deSDavid Ahern 	struct fib6_info *f6i = res->f6i;
13374832c30dSDavid Ahern 	struct net_device *dev;
13381da177e4SLinus Torvalds 	struct rt6_info *rt;
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	/*
13411da177e4SLinus Torvalds 	 *	Clone the route.
13421da177e4SLinus Torvalds 	 */
13431da177e4SLinus Torvalds 
134485bd05deSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
1345e873e4b9SWei Wang 		return NULL;
1346e873e4b9SWei Wang 
13470d161581SDavid Ahern 	dev = ip6_rt_get_dev_rcu(res);
134893531c67SDavid Ahern 	rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1349e873e4b9SWei Wang 	if (!rt) {
135085bd05deSDavid Ahern 		fib6_info_release(f6i);
135183a09abdSMartin KaFai Lau 		return NULL;
1352e873e4b9SWei Wang 	}
135383a09abdSMartin KaFai Lau 
13540d161581SDavid Ahern 	ip6_rt_copy_init(rt, res);
13558b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
135683a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
135783a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
13588b9df265SMartin KaFai Lau 
135985bd05deSDavid Ahern 	if (!rt6_is_gw_or_nonexthop(res)) {
136085bd05deSDavid Ahern 		if (f6i->fib6_dst.plen != 128 &&
136185bd05deSDavid Ahern 		    ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
136258c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
13631da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
13641da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
13654e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
13661da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
13671da177e4SLinus Torvalds 		}
13681da177e4SLinus Torvalds #endif
136995a9a5baSYOSHIFUJI Hideaki 	}
137095a9a5baSYOSHIFUJI Hideaki 
1371299d9939SYOSHIFUJI Hideaki 	return rt;
1372299d9939SYOSHIFUJI Hideaki }
1373299d9939SYOSHIFUJI Hideaki 
1374db3fedeeSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
1375d52d3997SMartin KaFai Lau {
1376db3fedeeSDavid Ahern 	struct fib6_info *f6i = res->f6i;
1377db3fedeeSDavid Ahern 	unsigned short flags = fib6_info_dst_flags(f6i);
13784832c30dSDavid Ahern 	struct net_device *dev;
1379d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1380d52d3997SMartin KaFai Lau 
1381db3fedeeSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
1382e873e4b9SWei Wang 		return NULL;
1383e873e4b9SWei Wang 
13844832c30dSDavid Ahern 	rcu_read_lock();
13850d161581SDavid Ahern 	dev = ip6_rt_get_dev_rcu(res);
1386d8882935SEric Dumazet 	pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
13874832c30dSDavid Ahern 	rcu_read_unlock();
1388e873e4b9SWei Wang 	if (!pcpu_rt) {
1389db3fedeeSDavid Ahern 		fib6_info_release(f6i);
1390d52d3997SMartin KaFai Lau 		return NULL;
1391e873e4b9SWei Wang 	}
13920d161581SDavid Ahern 	ip6_rt_copy_init(pcpu_rt, res);
1393d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
13948f34e53bSDavid Ahern 
13958f34e53bSDavid Ahern 	if (f6i->nh)
13968f34e53bSDavid Ahern 		pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
13978f34e53bSDavid Ahern 
1398d52d3997SMartin KaFai Lau 	return pcpu_rt;
1399d52d3997SMartin KaFai Lau }
1400d52d3997SMartin KaFai Lau 
14018f34e53bSDavid Ahern static bool rt6_is_valid(const struct rt6_info *rt6)
14028f34e53bSDavid Ahern {
14038f34e53bSDavid Ahern 	return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
14048f34e53bSDavid Ahern }
14058f34e53bSDavid Ahern 
140666f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */
1407db3fedeeSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
1408d52d3997SMartin KaFai Lau {
1409c353071aSEric Dumazet 	struct rt6_info *pcpu_rt;
1410d52d3997SMartin KaFai Lau 
1411c353071aSEric Dumazet 	pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
1412d52d3997SMartin KaFai Lau 
14138f34e53bSDavid Ahern 	if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
14148f34e53bSDavid Ahern 		struct rt6_info *prev, **p;
14158f34e53bSDavid Ahern 
14168f34e53bSDavid Ahern 		p = this_cpu_ptr(res->nh->rt6i_pcpu);
14178f34e53bSDavid Ahern 		prev = xchg(p, NULL);
14188f34e53bSDavid Ahern 		if (prev) {
14198f34e53bSDavid Ahern 			dst_dev_put(&prev->dst);
14208f34e53bSDavid Ahern 			dst_release(&prev->dst);
14218f34e53bSDavid Ahern 		}
14228f34e53bSDavid Ahern 
14238f34e53bSDavid Ahern 		pcpu_rt = NULL;
14248f34e53bSDavid Ahern 	}
14258f34e53bSDavid Ahern 
1426a73e4195SMartin KaFai Lau 	return pcpu_rt;
1427a73e4195SMartin KaFai Lau }
1428a73e4195SMartin KaFai Lau 
1429afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1430db3fedeeSDavid Ahern 					    const struct fib6_result *res)
1431a73e4195SMartin KaFai Lau {
1432a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1433d52d3997SMartin KaFai Lau 
1434db3fedeeSDavid Ahern 	pcpu_rt = ip6_rt_pcpu_alloc(res);
14350e09edccSWei Wang 	if (!pcpu_rt)
14360e09edccSWei Wang 		return NULL;
1437d52d3997SMartin KaFai Lau 
1438f40b6ae2SDavid Ahern 	p = this_cpu_ptr(res->nh->rt6i_pcpu);
1439d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1440951f788aSEric Dumazet 	BUG_ON(prev);
1441a94b9367SWei Wang 
144261fb0d01SEric Dumazet 	if (res->f6i->fib6_destroying) {
144361fb0d01SEric Dumazet 		struct fib6_info *from;
144461fb0d01SEric Dumazet 
144561fb0d01SEric Dumazet 		from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
144661fb0d01SEric Dumazet 		fib6_info_release(from);
144761fb0d01SEric Dumazet 	}
144861fb0d01SEric Dumazet 
1449d52d3997SMartin KaFai Lau 	return pcpu_rt;
1450d52d3997SMartin KaFai Lau }
1451d52d3997SMartin KaFai Lau 
145235732d01SWei Wang /* exception hash table implementation
145335732d01SWei Wang  */
145435732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
145535732d01SWei Wang 
145635732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
145735732d01SWei Wang  * Caller must hold rt6_exception_lock
145835732d01SWei Wang  */
145935732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
146035732d01SWei Wang 				 struct rt6_exception *rt6_ex)
146135732d01SWei Wang {
1462f5b51fe8SPaolo Abeni 	struct fib6_info *from;
1463b2427e67SColin Ian King 	struct net *net;
146481eb8447SWei Wang 
146535732d01SWei Wang 	if (!bucket || !rt6_ex)
146635732d01SWei Wang 		return;
1467b2427e67SColin Ian King 
1468b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
1469f5b51fe8SPaolo Abeni 	net->ipv6.rt6_stats->fib_rt_cache--;
1470f5b51fe8SPaolo Abeni 
1471f5b51fe8SPaolo Abeni 	/* purge completely the exception to allow releasing the held resources:
1472f5b51fe8SPaolo Abeni 	 * some [sk] cache may keep the dst around for unlimited time
1473f5b51fe8SPaolo Abeni 	 */
14740e233874SEric Dumazet 	from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1475f5b51fe8SPaolo Abeni 	fib6_info_release(from);
1476f5b51fe8SPaolo Abeni 	dst_dev_put(&rt6_ex->rt6i->dst);
1477f5b51fe8SPaolo Abeni 
147835732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
147977634cc6SDavid Ahern 	dst_release(&rt6_ex->rt6i->dst);
148035732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
148135732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
148235732d01SWei Wang 	bucket->depth--;
148335732d01SWei Wang }
148435732d01SWei Wang 
148535732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
148635732d01SWei Wang  * Caller must hold rt6_exception_lock
148735732d01SWei Wang  */
148835732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
148935732d01SWei Wang {
149035732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
149135732d01SWei Wang 
149235732d01SWei Wang 	if (!bucket)
149335732d01SWei Wang 		return;
149435732d01SWei Wang 
149535732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
149635732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
149735732d01SWei Wang 			oldest = rt6_ex;
149835732d01SWei Wang 	}
149935732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
150035732d01SWei Wang }
150135732d01SWei Wang 
150235732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
150335732d01SWei Wang 			      const struct in6_addr *src)
150435732d01SWei Wang {
150535732d01SWei Wang 	static u32 seed __read_mostly;
150635732d01SWei Wang 	u32 val;
150735732d01SWei Wang 
150835732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
1509b6b556afSEric Dumazet 	val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed);
151035732d01SWei Wang 
151135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
151235732d01SWei Wang 	if (src)
1513b6b556afSEric Dumazet 		val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val);
151435732d01SWei Wang #endif
151535732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
151635732d01SWei Wang }
151735732d01SWei Wang 
151835732d01SWei Wang /* Helper function to find the cached rt in the hash table
151935732d01SWei Wang  * and update bucket pointer to point to the bucket for this
152035732d01SWei Wang  * (daddr, saddr) pair
152135732d01SWei Wang  * Caller must hold rt6_exception_lock
152235732d01SWei Wang  */
152335732d01SWei Wang static struct rt6_exception *
152435732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
152535732d01SWei Wang 			      const struct in6_addr *daddr,
152635732d01SWei Wang 			      const struct in6_addr *saddr)
152735732d01SWei Wang {
152835732d01SWei Wang 	struct rt6_exception *rt6_ex;
152935732d01SWei Wang 	u32 hval;
153035732d01SWei Wang 
153135732d01SWei Wang 	if (!(*bucket) || !daddr)
153235732d01SWei Wang 		return NULL;
153335732d01SWei Wang 
153435732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
153535732d01SWei Wang 	*bucket += hval;
153635732d01SWei Wang 
153735732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
153835732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
153935732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
154035732d01SWei Wang 
154135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
154235732d01SWei Wang 		if (matched && saddr)
154335732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
154435732d01SWei Wang #endif
154535732d01SWei Wang 		if (matched)
154635732d01SWei Wang 			return rt6_ex;
154735732d01SWei Wang 	}
154835732d01SWei Wang 	return NULL;
154935732d01SWei Wang }
155035732d01SWei Wang 
155135732d01SWei Wang /* Helper function to find the cached rt in the hash table
155235732d01SWei Wang  * and update bucket pointer to point to the bucket for this
155335732d01SWei Wang  * (daddr, saddr) pair
155435732d01SWei Wang  * Caller must hold rcu_read_lock()
155535732d01SWei Wang  */
155635732d01SWei Wang static struct rt6_exception *
155735732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
155835732d01SWei Wang 			 const struct in6_addr *daddr,
155935732d01SWei Wang 			 const struct in6_addr *saddr)
156035732d01SWei Wang {
156135732d01SWei Wang 	struct rt6_exception *rt6_ex;
156235732d01SWei Wang 	u32 hval;
156335732d01SWei Wang 
156435732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
156535732d01SWei Wang 
156635732d01SWei Wang 	if (!(*bucket) || !daddr)
156735732d01SWei Wang 		return NULL;
156835732d01SWei Wang 
156935732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
157035732d01SWei Wang 	*bucket += hval;
157135732d01SWei Wang 
157235732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
157335732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
157435732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
157535732d01SWei Wang 
157635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
157735732d01SWei Wang 		if (matched && saddr)
157835732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
157935732d01SWei Wang #endif
158035732d01SWei Wang 		if (matched)
158135732d01SWei Wang 			return rt6_ex;
158235732d01SWei Wang 	}
158335732d01SWei Wang 	return NULL;
158435732d01SWei Wang }
158535732d01SWei Wang 
1586b748f260SDavid Ahern static unsigned int fib6_mtu(const struct fib6_result *res)
158735732d01SWei Wang {
1588b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
1589d4ead6b3SDavid Ahern 	unsigned int mtu;
1590d4ead6b3SDavid Ahern 
1591b748f260SDavid Ahern 	if (res->f6i->fib6_pmtu) {
1592b748f260SDavid Ahern 		mtu = res->f6i->fib6_pmtu;
1593dcd1f572SDavid Ahern 	} else {
1594b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
1595dcd1f572SDavid Ahern 		struct inet6_dev *idev;
1596dcd1f572SDavid Ahern 
1597dcd1f572SDavid Ahern 		rcu_read_lock();
1598dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
1599dcd1f572SDavid Ahern 		mtu = idev->cnf.mtu6;
1600dcd1f572SDavid Ahern 		rcu_read_unlock();
1601dcd1f572SDavid Ahern 	}
1602dcd1f572SDavid Ahern 
1603d4ead6b3SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1604d4ead6b3SDavid Ahern 
1605b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1606d4ead6b3SDavid Ahern }
1607d4ead6b3SDavid Ahern 
1608cc5c073aSDavid Ahern #define FIB6_EXCEPTION_BUCKET_FLUSHED  0x1UL
1609cc5c073aSDavid Ahern 
1610cc5c073aSDavid Ahern /* used when the flushed bit is not relevant, only access to the bucket
1611cc5c073aSDavid Ahern  * (ie., all bucket users except rt6_insert_exception);
1612cc5c073aSDavid Ahern  *
1613cc5c073aSDavid Ahern  * called under rcu lock; sometimes called with rt6_exception_lock held
1614cc5c073aSDavid Ahern  */
1615cc5c073aSDavid Ahern static
1616cc5c073aSDavid Ahern struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1617cc5c073aSDavid Ahern 						       spinlock_t *lock)
1618cc5c073aSDavid Ahern {
1619cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
1620cc5c073aSDavid Ahern 
1621cc5c073aSDavid Ahern 	if (lock)
1622cc5c073aSDavid Ahern 		bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1623cc5c073aSDavid Ahern 						   lockdep_is_held(lock));
1624cc5c073aSDavid Ahern 	else
1625cc5c073aSDavid Ahern 		bucket = rcu_dereference(nh->rt6i_exception_bucket);
1626cc5c073aSDavid Ahern 
1627cc5c073aSDavid Ahern 	/* remove bucket flushed bit if set */
1628cc5c073aSDavid Ahern 	if (bucket) {
1629cc5c073aSDavid Ahern 		unsigned long p = (unsigned long)bucket;
1630cc5c073aSDavid Ahern 
1631cc5c073aSDavid Ahern 		p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1632cc5c073aSDavid Ahern 		bucket = (struct rt6_exception_bucket *)p;
1633cc5c073aSDavid Ahern 	}
1634cc5c073aSDavid Ahern 
1635cc5c073aSDavid Ahern 	return bucket;
1636cc5c073aSDavid Ahern }
1637cc5c073aSDavid Ahern 
1638cc5c073aSDavid Ahern static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1639cc5c073aSDavid Ahern {
1640cc5c073aSDavid Ahern 	unsigned long p = (unsigned long)bucket;
1641cc5c073aSDavid Ahern 
1642cc5c073aSDavid Ahern 	return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1643cc5c073aSDavid Ahern }
1644cc5c073aSDavid Ahern 
1645cc5c073aSDavid Ahern /* called with rt6_exception_lock held */
1646cc5c073aSDavid Ahern static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1647cc5c073aSDavid Ahern 					      spinlock_t *lock)
1648cc5c073aSDavid Ahern {
1649cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
1650cc5c073aSDavid Ahern 	unsigned long p;
1651cc5c073aSDavid Ahern 
1652cc5c073aSDavid Ahern 	bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1653cc5c073aSDavid Ahern 					   lockdep_is_held(lock));
1654cc5c073aSDavid Ahern 
1655cc5c073aSDavid Ahern 	p = (unsigned long)bucket;
1656cc5c073aSDavid Ahern 	p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1657cc5c073aSDavid Ahern 	bucket = (struct rt6_exception_bucket *)p;
1658cc5c073aSDavid Ahern 	rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1659cc5c073aSDavid Ahern }
1660cc5c073aSDavid Ahern 
166135732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
16625012f0a5SDavid Ahern 				const struct fib6_result *res)
166335732d01SWei Wang {
16645e670d84SDavid Ahern 	struct net *net = dev_net(nrt->dst.dev);
166535732d01SWei Wang 	struct rt6_exception_bucket *bucket;
1666cc5c073aSDavid Ahern 	struct fib6_info *f6i = res->f6i;
166735732d01SWei Wang 	struct in6_addr *src_key = NULL;
166835732d01SWei Wang 	struct rt6_exception *rt6_ex;
1669cc5c073aSDavid Ahern 	struct fib6_nh *nh = res->nh;
167035732d01SWei Wang 	int err = 0;
167135732d01SWei Wang 
167235732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
167335732d01SWei Wang 
1674cc5c073aSDavid Ahern 	bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
167535732d01SWei Wang 					  lockdep_is_held(&rt6_exception_lock));
167635732d01SWei Wang 	if (!bucket) {
167735732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
167835732d01SWei Wang 				 GFP_ATOMIC);
167935732d01SWei Wang 		if (!bucket) {
168035732d01SWei Wang 			err = -ENOMEM;
168135732d01SWei Wang 			goto out;
168235732d01SWei Wang 		}
1683cc5c073aSDavid Ahern 		rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1684cc5c073aSDavid Ahern 	} else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1685cc5c073aSDavid Ahern 		err = -EINVAL;
1686cc5c073aSDavid Ahern 		goto out;
168735732d01SWei Wang 	}
168835732d01SWei Wang 
168935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
16905012f0a5SDavid Ahern 	/* fib6_src.plen != 0 indicates f6i is in subtree
169135732d01SWei Wang 	 * and exception table is indexed by a hash of
16925012f0a5SDavid Ahern 	 * both fib6_dst and fib6_src.
169335732d01SWei Wang 	 * Otherwise, the exception table is indexed by
16945012f0a5SDavid Ahern 	 * a hash of only fib6_dst.
169535732d01SWei Wang 	 */
16965012f0a5SDavid Ahern 	if (f6i->fib6_src.plen)
169735732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
169835732d01SWei Wang #endif
16995012f0a5SDavid Ahern 	/* rt6_mtu_change() might lower mtu on f6i.
1700f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
17015012f0a5SDavid Ahern 	 * is less than f6i's mtu value.
1702f5bbe7eeSWei Wang 	 */
1703b748f260SDavid Ahern 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1704f5bbe7eeSWei Wang 		err = -EINVAL;
1705f5bbe7eeSWei Wang 		goto out;
1706f5bbe7eeSWei Wang 	}
170760006a48SWei Wang 
170835732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
170935732d01SWei Wang 					       src_key);
171035732d01SWei Wang 	if (rt6_ex)
171135732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
171235732d01SWei Wang 
171335732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
171435732d01SWei Wang 	if (!rt6_ex) {
171535732d01SWei Wang 		err = -ENOMEM;
171635732d01SWei Wang 		goto out;
171735732d01SWei Wang 	}
171835732d01SWei Wang 	rt6_ex->rt6i = nrt;
171935732d01SWei Wang 	rt6_ex->stamp = jiffies;
172035732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
172135732d01SWei Wang 	bucket->depth++;
172281eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
172335732d01SWei Wang 
172435732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
172535732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
172635732d01SWei Wang 
172735732d01SWei Wang out:
172835732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
172935732d01SWei Wang 
173035732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1731b886d5f2SPaolo Abeni 	if (!err) {
17325012f0a5SDavid Ahern 		spin_lock_bh(&f6i->fib6_table->tb6_lock);
17335012f0a5SDavid Ahern 		fib6_update_sernum(net, f6i);
17345012f0a5SDavid Ahern 		spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1735b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1736b886d5f2SPaolo Abeni 	}
173735732d01SWei Wang 
173835732d01SWei Wang 	return err;
173935732d01SWei Wang }
174035732d01SWei Wang 
1741c0b220cfSDavid Ahern static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
174235732d01SWei Wang {
174335732d01SWei Wang 	struct rt6_exception_bucket *bucket;
174435732d01SWei Wang 	struct rt6_exception *rt6_ex;
174535732d01SWei Wang 	struct hlist_node *tmp;
174635732d01SWei Wang 	int i;
174735732d01SWei Wang 
174835732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
174935732d01SWei Wang 
1750cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
175135732d01SWei Wang 	if (!bucket)
175235732d01SWei Wang 		goto out;
175335732d01SWei Wang 
1754cc5c073aSDavid Ahern 	/* Prevent rt6_insert_exception() to recreate the bucket list */
1755cc5c073aSDavid Ahern 	if (!from)
1756cc5c073aSDavid Ahern 		fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1757cc5c073aSDavid Ahern 
175835732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1759cc5c073aSDavid Ahern 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1760cc5c073aSDavid Ahern 			if (!from ||
1761cc5c073aSDavid Ahern 			    rcu_access_pointer(rt6_ex->rt6i->from) == from)
176235732d01SWei Wang 				rt6_remove_exception(bucket, rt6_ex);
1763cc5c073aSDavid Ahern 		}
1764cc5c073aSDavid Ahern 		WARN_ON_ONCE(!from && bucket->depth);
176535732d01SWei Wang 		bucket++;
176635732d01SWei Wang 	}
176735732d01SWei Wang out:
176835732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
176935732d01SWei Wang }
177035732d01SWei Wang 
1771e659ba31SDavid Ahern static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1772e659ba31SDavid Ahern {
1773e659ba31SDavid Ahern 	struct fib6_info *f6i = arg;
1774e659ba31SDavid Ahern 
1775e659ba31SDavid Ahern 	fib6_nh_flush_exceptions(nh, f6i);
1776e659ba31SDavid Ahern 
1777e659ba31SDavid Ahern 	return 0;
1778e659ba31SDavid Ahern }
1779e659ba31SDavid Ahern 
1780c0b220cfSDavid Ahern void rt6_flush_exceptions(struct fib6_info *f6i)
1781c0b220cfSDavid Ahern {
1782e659ba31SDavid Ahern 	if (f6i->nh)
1783e659ba31SDavid Ahern 		nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1784e659ba31SDavid Ahern 					 f6i);
1785e659ba31SDavid Ahern 	else
17861cf844c7SDavid Ahern 		fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1787c0b220cfSDavid Ahern }
1788c0b220cfSDavid Ahern 
178935732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
179035732d01SWei Wang  * Caller has to hold rcu_read_lock()
179135732d01SWei Wang  */
17927e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1793510e2cedSWei Wang 					   const struct in6_addr *daddr,
1794510e2cedSWei Wang 					   const struct in6_addr *saddr)
179535732d01SWei Wang {
1796510e2cedSWei Wang 	const struct in6_addr *src_key = NULL;
179735732d01SWei Wang 	struct rt6_exception_bucket *bucket;
179835732d01SWei Wang 	struct rt6_exception *rt6_ex;
17997e4b5128SDavid Ahern 	struct rt6_info *ret = NULL;
180035732d01SWei Wang 
180135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
18027e4b5128SDavid Ahern 	/* fib6i_src.plen != 0 indicates f6i is in subtree
180335732d01SWei Wang 	 * and exception table is indexed by a hash of
18047e4b5128SDavid Ahern 	 * both fib6_dst and fib6_src.
1805510e2cedSWei Wang 	 * However, the src addr used to create the hash
1806510e2cedSWei Wang 	 * might not be exactly the passed in saddr which
1807510e2cedSWei Wang 	 * is a /128 addr from the flow.
1808510e2cedSWei Wang 	 * So we need to use f6i->fib6_src to redo lookup
1809510e2cedSWei Wang 	 * if the passed in saddr does not find anything.
1810510e2cedSWei Wang 	 * (See the logic in ip6_rt_cache_alloc() on how
1811510e2cedSWei Wang 	 * rt->rt6i_src is updated.)
181235732d01SWei Wang 	 */
18137e4b5128SDavid Ahern 	if (res->f6i->fib6_src.plen)
181435732d01SWei Wang 		src_key = saddr;
1815510e2cedSWei Wang find_ex:
181635732d01SWei Wang #endif
1817cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
181835732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
181935732d01SWei Wang 
182035732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
18217e4b5128SDavid Ahern 		ret = rt6_ex->rt6i;
182235732d01SWei Wang 
1823510e2cedSWei Wang #ifdef CONFIG_IPV6_SUBTREES
1824510e2cedSWei Wang 	/* Use fib6_src as src_key and redo lookup */
1825510e2cedSWei Wang 	if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1826510e2cedSWei Wang 		src_key = &res->f6i->fib6_src.addr;
1827510e2cedSWei Wang 		goto find_ex;
1828510e2cedSWei Wang 	}
1829510e2cedSWei Wang #endif
1830510e2cedSWei Wang 
18317e4b5128SDavid Ahern 	return ret;
183235732d01SWei Wang }
183335732d01SWei Wang 
183435732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
1835cc5c073aSDavid Ahern static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1836c0b220cfSDavid Ahern 				    const struct rt6_info *rt)
183735732d01SWei Wang {
1838c0b220cfSDavid Ahern 	const struct in6_addr *src_key = NULL;
183935732d01SWei Wang 	struct rt6_exception_bucket *bucket;
184035732d01SWei Wang 	struct rt6_exception *rt6_ex;
184135732d01SWei Wang 	int err;
184235732d01SWei Wang 
1843cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
184435732d01SWei Wang 		return -ENOENT;
184535732d01SWei Wang 
184635732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1847cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1848cc5c073aSDavid Ahern 
184935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
1850cc5c073aSDavid Ahern 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
1851cc5c073aSDavid Ahern 	 * and exception table is indexed by a hash of
1852cc5c073aSDavid Ahern 	 * both rt6i_dst and rt6i_src.
185335732d01SWei Wang 	 * Otherwise, the exception table is indexed by
185435732d01SWei Wang 	 * a hash of only rt6i_dst.
185535732d01SWei Wang 	 */
1856c0b220cfSDavid Ahern 	if (plen)
185735732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
185835732d01SWei Wang #endif
185935732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
186035732d01SWei Wang 					       &rt->rt6i_dst.addr,
186135732d01SWei Wang 					       src_key);
186235732d01SWei Wang 	if (rt6_ex) {
186335732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
186435732d01SWei Wang 		err = 0;
186535732d01SWei Wang 	} else {
186635732d01SWei Wang 		err = -ENOENT;
186735732d01SWei Wang 	}
186835732d01SWei Wang 
186935732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
187035732d01SWei Wang 	return err;
187135732d01SWei Wang }
187235732d01SWei Wang 
1873e659ba31SDavid Ahern struct fib6_nh_excptn_arg {
1874e659ba31SDavid Ahern 	struct rt6_info	*rt;
1875e659ba31SDavid Ahern 	int		plen;
1876e659ba31SDavid Ahern };
1877e659ba31SDavid Ahern 
1878e659ba31SDavid Ahern static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1879e659ba31SDavid Ahern {
1880e659ba31SDavid Ahern 	struct fib6_nh_excptn_arg *arg = _arg;
1881e659ba31SDavid Ahern 	int err;
1882e659ba31SDavid Ahern 
1883e659ba31SDavid Ahern 	err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1884e659ba31SDavid Ahern 	if (err == 0)
1885e659ba31SDavid Ahern 		return 1;
1886e659ba31SDavid Ahern 
1887e659ba31SDavid Ahern 	return 0;
1888e659ba31SDavid Ahern }
1889e659ba31SDavid Ahern 
1890c0b220cfSDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt)
1891c0b220cfSDavid Ahern {
1892c0b220cfSDavid Ahern 	struct fib6_info *from;
1893c0b220cfSDavid Ahern 
1894c0b220cfSDavid Ahern 	from = rcu_dereference(rt->from);
1895cc5c073aSDavid Ahern 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1896c0b220cfSDavid Ahern 		return -EINVAL;
1897c0b220cfSDavid Ahern 
1898e659ba31SDavid Ahern 	if (from->nh) {
1899e659ba31SDavid Ahern 		struct fib6_nh_excptn_arg arg = {
1900e659ba31SDavid Ahern 			.rt = rt,
1901e659ba31SDavid Ahern 			.plen = from->fib6_src.plen
1902e659ba31SDavid Ahern 		};
1903e659ba31SDavid Ahern 		int rc;
1904e659ba31SDavid Ahern 
1905e659ba31SDavid Ahern 		/* rc = 1 means an entry was found */
1906e659ba31SDavid Ahern 		rc = nexthop_for_each_fib6_nh(from->nh,
1907e659ba31SDavid Ahern 					      rt6_nh_remove_exception_rt,
1908e659ba31SDavid Ahern 					      &arg);
1909e659ba31SDavid Ahern 		return rc ? 0 : -ENOENT;
1910e659ba31SDavid Ahern 	}
1911e659ba31SDavid Ahern 
19121cf844c7SDavid Ahern 	return fib6_nh_remove_exception(from->fib6_nh,
1913cc5c073aSDavid Ahern 					from->fib6_src.plen, rt);
1914c0b220cfSDavid Ahern }
1915c0b220cfSDavid Ahern 
191635732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
191735732d01SWei Wang  * refresh its stamp
191835732d01SWei Wang  */
1919cc5c073aSDavid Ahern static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1920c0b220cfSDavid Ahern 				     const struct rt6_info *rt)
192135732d01SWei Wang {
1922c0b220cfSDavid Ahern 	const struct in6_addr *src_key = NULL;
192335732d01SWei Wang 	struct rt6_exception_bucket *bucket;
192435732d01SWei Wang 	struct rt6_exception *rt6_ex;
1925193f3685SPaolo Abeni 
1926cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, NULL);
192735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
1928cc5c073aSDavid Ahern 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
1929cc5c073aSDavid Ahern 	 * and exception table is indexed by a hash of
1930cc5c073aSDavid Ahern 	 * both rt6i_dst and rt6i_src.
193135732d01SWei Wang 	 * Otherwise, the exception table is indexed by
193235732d01SWei Wang 	 * a hash of only rt6i_dst.
193335732d01SWei Wang 	 */
1934c0b220cfSDavid Ahern 	if (plen)
193535732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
193635732d01SWei Wang #endif
1937cc5c073aSDavid Ahern 	rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
193835732d01SWei Wang 	if (rt6_ex)
193935732d01SWei Wang 		rt6_ex->stamp = jiffies;
1940c0b220cfSDavid Ahern }
194135732d01SWei Wang 
1942e659ba31SDavid Ahern struct fib6_nh_match_arg {
1943e659ba31SDavid Ahern 	const struct net_device *dev;
1944e659ba31SDavid Ahern 	const struct in6_addr	*gw;
1945e659ba31SDavid Ahern 	struct fib6_nh		*match;
1946e659ba31SDavid Ahern };
1947e659ba31SDavid Ahern 
1948e659ba31SDavid Ahern /* determine if fib6_nh has given device and gateway */
1949e659ba31SDavid Ahern static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1950e659ba31SDavid Ahern {
1951e659ba31SDavid Ahern 	struct fib6_nh_match_arg *arg = _arg;
1952e659ba31SDavid Ahern 
1953e659ba31SDavid Ahern 	if (arg->dev != nh->fib_nh_dev ||
1954e659ba31SDavid Ahern 	    (arg->gw && !nh->fib_nh_gw_family) ||
1955e659ba31SDavid Ahern 	    (!arg->gw && nh->fib_nh_gw_family) ||
1956e659ba31SDavid Ahern 	    (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1957e659ba31SDavid Ahern 		return 0;
1958e659ba31SDavid Ahern 
1959e659ba31SDavid Ahern 	arg->match = nh;
1960e659ba31SDavid Ahern 
1961e659ba31SDavid Ahern 	/* found a match, break the loop */
1962e659ba31SDavid Ahern 	return 1;
1963e659ba31SDavid Ahern }
1964e659ba31SDavid Ahern 
1965c0b220cfSDavid Ahern static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1966c0b220cfSDavid Ahern {
1967c0b220cfSDavid Ahern 	struct fib6_info *from;
1968e659ba31SDavid Ahern 	struct fib6_nh *fib6_nh;
1969c0b220cfSDavid Ahern 
1970c0b220cfSDavid Ahern 	rcu_read_lock();
1971c0b220cfSDavid Ahern 
1972c0b220cfSDavid Ahern 	from = rcu_dereference(rt->from);
1973c0b220cfSDavid Ahern 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1974c0b220cfSDavid Ahern 		goto unlock;
1975c0b220cfSDavid Ahern 
1976e659ba31SDavid Ahern 	if (from->nh) {
1977e659ba31SDavid Ahern 		struct fib6_nh_match_arg arg = {
1978e659ba31SDavid Ahern 			.dev = rt->dst.dev,
1979e659ba31SDavid Ahern 			.gw = &rt->rt6i_gateway,
1980e659ba31SDavid Ahern 		};
1981e659ba31SDavid Ahern 
1982e659ba31SDavid Ahern 		nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1983e659ba31SDavid Ahern 
1984e659ba31SDavid Ahern 		if (!arg.match)
1985cff6a327SDavid Ahern 			goto unlock;
1986e659ba31SDavid Ahern 		fib6_nh = arg.match;
1987e659ba31SDavid Ahern 	} else {
1988e659ba31SDavid Ahern 		fib6_nh = from->fib6_nh;
1989e659ba31SDavid Ahern 	}
1990e659ba31SDavid Ahern 	fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
1991193f3685SPaolo Abeni unlock:
199235732d01SWei Wang 	rcu_read_unlock();
199335732d01SWei Wang }
199435732d01SWei Wang 
1995e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1996e9fa1495SStefano Brivio 					 struct rt6_info *rt, int mtu)
1997e9fa1495SStefano Brivio {
1998e9fa1495SStefano Brivio 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1999e9fa1495SStefano Brivio 	 * lowest MTU in the path: always allow updating the route PMTU to
2000e9fa1495SStefano Brivio 	 * reflect PMTU decreases.
2001e9fa1495SStefano Brivio 	 *
2002e9fa1495SStefano Brivio 	 * If the new MTU is higher, and the route PMTU is equal to the local
2003e9fa1495SStefano Brivio 	 * MTU, this means the old MTU is the lowest in the path, so allow
2004e9fa1495SStefano Brivio 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
2005e9fa1495SStefano Brivio 	 * handle this.
2006e9fa1495SStefano Brivio 	 */
2007e9fa1495SStefano Brivio 
2008e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) >= mtu)
2009e9fa1495SStefano Brivio 		return true;
2010e9fa1495SStefano Brivio 
2011e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
2012e9fa1495SStefano Brivio 		return true;
2013e9fa1495SStefano Brivio 
2014e9fa1495SStefano Brivio 	return false;
2015e9fa1495SStefano Brivio }
2016e9fa1495SStefano Brivio 
2017e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
2018cc5c073aSDavid Ahern 				       const struct fib6_nh *nh, int mtu)
2019f5bbe7eeSWei Wang {
2020f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
2021f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
2022f5bbe7eeSWei Wang 	int i;
2023f5bbe7eeSWei Wang 
2024cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2025e9fa1495SStefano Brivio 	if (!bucket)
2026e9fa1495SStefano Brivio 		return;
2027e9fa1495SStefano Brivio 
2028f5bbe7eeSWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2029f5bbe7eeSWei Wang 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2030f5bbe7eeSWei Wang 			struct rt6_info *entry = rt6_ex->rt6i;
2031e9fa1495SStefano Brivio 
2032e9fa1495SStefano Brivio 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2033d4ead6b3SDavid Ahern 			 * route), the metrics of its rt->from have already
2034f5bbe7eeSWei Wang 			 * been updated.
2035f5bbe7eeSWei Wang 			 */
2036d4ead6b3SDavid Ahern 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
2037e9fa1495SStefano Brivio 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
2038d4ead6b3SDavid Ahern 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
2039f5bbe7eeSWei Wang 		}
2040f5bbe7eeSWei Wang 		bucket++;
2041f5bbe7eeSWei Wang 	}
2042f5bbe7eeSWei Wang }
2043f5bbe7eeSWei Wang 
2044b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
2045b16cb459SWei Wang 
2046cc5c073aSDavid Ahern static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2047cc5c073aSDavid Ahern 					    const struct in6_addr *gateway)
2048b16cb459SWei Wang {
2049b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
2050b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
2051b16cb459SWei Wang 	struct hlist_node *tmp;
2052b16cb459SWei Wang 	int i;
2053b16cb459SWei Wang 
2054cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2055b16cb459SWei Wang 		return;
2056b16cb459SWei Wang 
2057b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
2058cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2059b16cb459SWei Wang 	if (bucket) {
2060b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2061b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
2062b16cb459SWei Wang 						  &bucket->chain, hlist) {
2063b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
2064b16cb459SWei Wang 
2065b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2066b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
2067b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
2068b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
2069b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
2070b16cb459SWei Wang 				}
2071b16cb459SWei Wang 			}
2072b16cb459SWei Wang 			bucket++;
2073b16cb459SWei Wang 		}
2074b16cb459SWei Wang 	}
2075b16cb459SWei Wang 
2076b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
2077b16cb459SWei Wang }
2078b16cb459SWei Wang 
2079c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2080c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
2081c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
2082c757faa8SWei Wang 				      unsigned long now)
2083c757faa8SWei Wang {
2084c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
2085c757faa8SWei Wang 
20861859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
20871859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
20881859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
20891859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
20901859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
20911859bac0SPaolo Abeni 	 */
209231afeb42SWei Wang 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
209331afeb42SWei Wang 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2094c757faa8SWei Wang 			RT6_TRACE("aging clone %p\n", rt);
2095c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
2096c757faa8SWei Wang 			return;
209731afeb42SWei Wang 		}
209831afeb42SWei Wang 	} else if (time_after(jiffies, rt->dst.expires)) {
209931afeb42SWei Wang 		RT6_TRACE("purging expired route %p\n", rt);
210031afeb42SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
210131afeb42SWei Wang 		return;
210231afeb42SWei Wang 	}
210331afeb42SWei Wang 
210431afeb42SWei Wang 	if (rt->rt6i_flags & RTF_GATEWAY) {
2105c757faa8SWei Wang 		struct neighbour *neigh;
2106c757faa8SWei Wang 		__u8 neigh_flags = 0;
2107c757faa8SWei Wang 
21081bfa26ffSEric Dumazet 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
21091bfa26ffSEric Dumazet 		if (neigh)
2110c757faa8SWei Wang 			neigh_flags = neigh->flags;
21111bfa26ffSEric Dumazet 
2112c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
2113c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
2114c757faa8SWei Wang 				  rt);
2115c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
2116c757faa8SWei Wang 			return;
2117c757faa8SWei Wang 		}
2118c757faa8SWei Wang 	}
211931afeb42SWei Wang 
2120c757faa8SWei Wang 	gc_args->more++;
2121c757faa8SWei Wang }
2122c757faa8SWei Wang 
2123cc5c073aSDavid Ahern static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
2124c757faa8SWei Wang 				   struct fib6_gc_args *gc_args,
2125c757faa8SWei Wang 				   unsigned long now)
2126c757faa8SWei Wang {
2127c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
2128c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
2129c757faa8SWei Wang 	struct hlist_node *tmp;
2130c757faa8SWei Wang 	int i;
2131c757faa8SWei Wang 
2132cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2133c757faa8SWei Wang 		return;
2134c757faa8SWei Wang 
21351bfa26ffSEric Dumazet 	rcu_read_lock_bh();
21361bfa26ffSEric Dumazet 	spin_lock(&rt6_exception_lock);
2137cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2138c757faa8SWei Wang 	if (bucket) {
2139c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2140c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
2141c757faa8SWei Wang 						  &bucket->chain, hlist) {
2142c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
2143c757faa8SWei Wang 							  gc_args, now);
2144c757faa8SWei Wang 			}
2145c757faa8SWei Wang 			bucket++;
2146c757faa8SWei Wang 		}
2147c757faa8SWei Wang 	}
21481bfa26ffSEric Dumazet 	spin_unlock(&rt6_exception_lock);
21491bfa26ffSEric Dumazet 	rcu_read_unlock_bh();
2150c757faa8SWei Wang }
2151c757faa8SWei Wang 
2152e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg {
2153e659ba31SDavid Ahern 	struct fib6_gc_args	*gc_args;
2154e659ba31SDavid Ahern 	unsigned long		now;
2155e659ba31SDavid Ahern };
2156e659ba31SDavid Ahern 
2157e659ba31SDavid Ahern static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2158e659ba31SDavid Ahern {
2159e659ba31SDavid Ahern 	struct fib6_nh_age_excptn_arg *arg = _arg;
2160e659ba31SDavid Ahern 
2161e659ba31SDavid Ahern 	fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2162e659ba31SDavid Ahern 	return 0;
2163e659ba31SDavid Ahern }
2164e659ba31SDavid Ahern 
2165cc5c073aSDavid Ahern void rt6_age_exceptions(struct fib6_info *f6i,
2166c0b220cfSDavid Ahern 			struct fib6_gc_args *gc_args,
2167c0b220cfSDavid Ahern 			unsigned long now)
2168c0b220cfSDavid Ahern {
2169e659ba31SDavid Ahern 	if (f6i->nh) {
2170e659ba31SDavid Ahern 		struct fib6_nh_age_excptn_arg arg = {
2171e659ba31SDavid Ahern 			.gc_args = gc_args,
2172e659ba31SDavid Ahern 			.now = now
2173e659ba31SDavid Ahern 		};
2174e659ba31SDavid Ahern 
2175e659ba31SDavid Ahern 		nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2176e659ba31SDavid Ahern 					 &arg);
2177e659ba31SDavid Ahern 	} else {
21781cf844c7SDavid Ahern 		fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2179c0b220cfSDavid Ahern 	}
2180e659ba31SDavid Ahern }
2181c0b220cfSDavid Ahern 
21821d053da9SDavid Ahern /* must be called with rcu lock held */
2183effda4ddSDavid Ahern int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2184effda4ddSDavid Ahern 		      struct flowi6 *fl6, struct fib6_result *res, int strict)
21851da177e4SLinus Torvalds {
2186367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
21871da177e4SLinus Torvalds 
21886454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2189367efcb9SMartin KaFai Lau 	saved_fn = fn;
21901da177e4SLinus Torvalds 
2191ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2192ca254490SDavid Ahern 		oif = 0;
2193ca254490SDavid Ahern 
2194a3c00e46SMartin KaFai Lau redo_rt6_select:
2195effda4ddSDavid Ahern 	rt6_select(net, fn, oif, res, strict);
2196effda4ddSDavid Ahern 	if (res->f6i == net->ipv6.fib6_null_entry) {
2197a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2198a3c00e46SMartin KaFai Lau 		if (fn)
2199a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
2200367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
2201367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
2202367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
2203367efcb9SMartin KaFai Lau 			fn = saved_fn;
2204367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
2205367efcb9SMartin KaFai Lau 		}
2206a3c00e46SMartin KaFai Lau 	}
2207a3c00e46SMartin KaFai Lau 
2208effda4ddSDavid Ahern 	trace_fib6_table_lookup(net, res, table, fl6);
2209d52d3997SMartin KaFai Lau 
2210effda4ddSDavid Ahern 	return 0;
22111d053da9SDavid Ahern }
22121d053da9SDavid Ahern 
22131d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
22141d053da9SDavid Ahern 			       int oif, struct flowi6 *fl6,
22151d053da9SDavid Ahern 			       const struct sk_buff *skb, int flags)
22161d053da9SDavid Ahern {
2217b1d40991SDavid Ahern 	struct fib6_result res = {};
22180e09edccSWei Wang 	struct rt6_info *rt = NULL;
22191d053da9SDavid Ahern 	int strict = 0;
22201d053da9SDavid Ahern 
22210e09edccSWei Wang 	WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
22220e09edccSWei Wang 		     !rcu_read_lock_held());
22230e09edccSWei Wang 
22241d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IFACE;
22251d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
22261d053da9SDavid Ahern 	if (net->ipv6.devconf_all->forwarding == 0)
22271d053da9SDavid Ahern 		strict |= RT6_LOOKUP_F_REACHABLE;
22281d053da9SDavid Ahern 
22291d053da9SDavid Ahern 	rcu_read_lock();
22301d053da9SDavid Ahern 
2231effda4ddSDavid Ahern 	fib6_table_lookup(net, table, oif, fl6, &res, strict);
22320e09edccSWei Wang 	if (res.f6i == net->ipv6.fib6_null_entry)
22330e09edccSWei Wang 		goto out;
223423fb93a4SDavid Ahern 
2235b1d40991SDavid Ahern 	fib6_select_path(net, &res, fl6, oif, false, skb, strict);
2236d83009d4SDavid Ahern 
223723fb93a4SDavid Ahern 	/*Search through exception table */
22387e4b5128SDavid Ahern 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
223923fb93a4SDavid Ahern 	if (rt) {
22400e09edccSWei Wang 		goto out;
22413da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
2242b1d40991SDavid Ahern 			    !res.nh->fib_nh_gw_family)) {
22433da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
22443da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
22453da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
22463da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
22473da59bd9SMartin KaFai Lau 		 */
22480e09edccSWei Wang 		rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
22493da59bd9SMartin KaFai Lau 
22500e09edccSWei Wang 		if (rt) {
22510e09edccSWei Wang 			/* 1 refcnt is taken during ip6_rt_cache_alloc().
22520e09edccSWei Wang 			 * As rt6_uncached_list_add() does not consume refcnt,
22530e09edccSWei Wang 			 * this refcnt is always returned to the caller even
22540e09edccSWei Wang 			 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
22550e09edccSWei Wang 			 */
22560e09edccSWei Wang 			rt6_uncached_list_add(rt);
22570e09edccSWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
22584d85cd0cSDavid Ahern 			rcu_read_unlock();
22593da59bd9SMartin KaFai Lau 
22600e09edccSWei Wang 			return rt;
22611cfb71eeSWei Wang 		}
2262d52d3997SMartin KaFai Lau 	} else {
2263d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
2264951f788aSEric Dumazet 		local_bh_disable();
22650e09edccSWei Wang 		rt = rt6_get_pcpu_route(&res);
2266d52d3997SMartin KaFai Lau 
22670e09edccSWei Wang 		if (!rt)
22680e09edccSWei Wang 			rt = rt6_make_pcpu_route(net, &res);
226993531c67SDavid Ahern 
2270951f788aSEric Dumazet 		local_bh_enable();
22710e09edccSWei Wang 	}
22720e09edccSWei Wang out:
22730e09edccSWei Wang 	if (!rt)
22740e09edccSWei Wang 		rt = net->ipv6.ip6_null_entry;
22750e09edccSWei Wang 	if (!(flags & RT6_LOOKUP_F_DST_NOREF))
22760e09edccSWei Wang 		ip6_hold_safe(net, &rt);
2277951f788aSEric Dumazet 	rcu_read_unlock();
2278d4bea421SDavid Ahern 
22790e09edccSWei Wang 	return rt;
2280c71099acSThomas Graf }
22819ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
2282c71099acSThomas Graf 
228355cced4fSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_input(struct net *net,
2284b75cc8f9SDavid Ahern 					    struct fib6_table *table,
2285b75cc8f9SDavid Ahern 					    struct flowi6 *fl6,
2286b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2287b75cc8f9SDavid Ahern 					    int flags)
22884acad72dSPavel Emelyanov {
2289b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
22904acad72dSPavel Emelyanov }
22914acad72dSPavel Emelyanov 
2292d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
229372331bc0SShmulik Ladkani 					 struct net_device *dev,
2294b75cc8f9SDavid Ahern 					 struct flowi6 *fl6,
2295b75cc8f9SDavid Ahern 					 const struct sk_buff *skb,
2296b75cc8f9SDavid Ahern 					 int flags)
229772331bc0SShmulik Ladkani {
229872331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
229972331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
230072331bc0SShmulik Ladkani 
2301b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
230272331bc0SShmulik Ladkani }
2303d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
230472331bc0SShmulik Ladkani 
230523aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
23065e5d6fedSRoopa Prabhu 				  struct flow_keys *keys,
23075e5d6fedSRoopa Prabhu 				  struct flow_keys *flkeys)
230823aebdacSJakub Sitnicki {
230923aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
231023aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
23115e5d6fedSRoopa Prabhu 	struct flow_keys *_flkeys = flkeys;
231223aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
231323aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
231423aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
2315cea67a2dSEric Dumazet 	struct icmp6hdr _icmph;
231623aebdacSJakub Sitnicki 
231723aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
231823aebdacSJakub Sitnicki 		goto out;
231923aebdacSJakub Sitnicki 
2320cea67a2dSEric Dumazet 	icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2321cea67a2dSEric Dumazet 				   sizeof(_icmph), &_icmph);
2322cea67a2dSEric Dumazet 	if (!icmph)
2323cea67a2dSEric Dumazet 		goto out;
2324cea67a2dSEric Dumazet 
232554074f1dSMatteo Croce 	if (!icmpv6_is_err(icmph->icmp6_type))
232623aebdacSJakub Sitnicki 		goto out;
232723aebdacSJakub Sitnicki 
232823aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
232923aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
233023aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
233123aebdacSJakub Sitnicki 	if (!inner_iph)
233223aebdacSJakub Sitnicki 		goto out;
233323aebdacSJakub Sitnicki 
233423aebdacSJakub Sitnicki 	key_iph = inner_iph;
23355e5d6fedSRoopa Prabhu 	_flkeys = NULL;
233623aebdacSJakub Sitnicki out:
23375e5d6fedSRoopa Prabhu 	if (_flkeys) {
23385e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
23395e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
23405e5d6fedSRoopa Prabhu 		keys->tags.flow_label = _flkeys->tags.flow_label;
23415e5d6fedSRoopa Prabhu 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
23425e5d6fedSRoopa Prabhu 	} else {
234323aebdacSJakub Sitnicki 		keys->addrs.v6addrs.src = key_iph->saddr;
234423aebdacSJakub Sitnicki 		keys->addrs.v6addrs.dst = key_iph->daddr;
2345fa1be7e0SMichal Kubecek 		keys->tags.flow_label = ip6_flowlabel(key_iph);
234623aebdacSJakub Sitnicki 		keys->basic.ip_proto = key_iph->nexthdr;
234723aebdacSJakub Sitnicki 	}
23485e5d6fedSRoopa Prabhu }
234923aebdacSJakub Sitnicki 
235023aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
2351b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2352b4bac172SDavid Ahern 		       const struct sk_buff *skb, struct flow_keys *flkeys)
235323aebdacSJakub Sitnicki {
235423aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
23559a2a537aSDavid Ahern 	u32 mhash;
235623aebdacSJakub Sitnicki 
2357bbfa047aSDavid S. Miller 	switch (ip6_multipath_hash_policy(net)) {
2358b4bac172SDavid Ahern 	case 0:
23596f74b6c2SDavid Ahern 		memset(&hash_keys, 0, sizeof(hash_keys));
23606f74b6c2SDavid Ahern 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
23619a2a537aSDavid Ahern 		if (skb) {
23625e5d6fedSRoopa Prabhu 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
23639a2a537aSDavid Ahern 		} else {
23649a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
23659a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2366fa1be7e0SMichal Kubecek 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
23679a2a537aSDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
236823aebdacSJakub Sitnicki 		}
2369b4bac172SDavid Ahern 		break;
2370b4bac172SDavid Ahern 	case 1:
2371b4bac172SDavid Ahern 		if (skb) {
2372b4bac172SDavid Ahern 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2373b4bac172SDavid Ahern 			struct flow_keys keys;
2374b4bac172SDavid Ahern 
2375b4bac172SDavid Ahern 			/* short-circuit if we already have L4 hash present */
2376b4bac172SDavid Ahern 			if (skb->l4_hash)
2377b4bac172SDavid Ahern 				return skb_get_hash_raw(skb) >> 1;
2378b4bac172SDavid Ahern 
2379b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2380b4bac172SDavid Ahern 
2381b4bac172SDavid Ahern                         if (!flkeys) {
2382b4bac172SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2383b4bac172SDavid Ahern 				flkeys = &keys;
2384b4bac172SDavid Ahern 			}
2385b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2386b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2387b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2388b4bac172SDavid Ahern 			hash_keys.ports.src = flkeys->ports.src;
2389b4bac172SDavid Ahern 			hash_keys.ports.dst = flkeys->ports.dst;
2390b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2391b4bac172SDavid Ahern 		} else {
2392b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2393b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2394b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2395b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2396b4bac172SDavid Ahern 			hash_keys.ports.src = fl6->fl6_sport;
2397b4bac172SDavid Ahern 			hash_keys.ports.dst = fl6->fl6_dport;
2398b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2399b4bac172SDavid Ahern 		}
2400b4bac172SDavid Ahern 		break;
2401d8f74f09SStephen Suryaputra 	case 2:
2402d8f74f09SStephen Suryaputra 		memset(&hash_keys, 0, sizeof(hash_keys));
2403d8f74f09SStephen Suryaputra 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2404d8f74f09SStephen Suryaputra 		if (skb) {
2405d8f74f09SStephen Suryaputra 			struct flow_keys keys;
2406d8f74f09SStephen Suryaputra 
2407d8f74f09SStephen Suryaputra 			if (!flkeys) {
2408d8f74f09SStephen Suryaputra 				skb_flow_dissect_flow_keys(skb, &keys, 0);
2409d8f74f09SStephen Suryaputra 				flkeys = &keys;
2410d8f74f09SStephen Suryaputra 			}
2411d8f74f09SStephen Suryaputra 
2412d8f74f09SStephen Suryaputra 			/* Inner can be v4 or v6 */
2413d8f74f09SStephen Suryaputra 			if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2414d8f74f09SStephen Suryaputra 				hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2415d8f74f09SStephen Suryaputra 				hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2416d8f74f09SStephen Suryaputra 				hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2417d8f74f09SStephen Suryaputra 			} else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2418d8f74f09SStephen Suryaputra 				hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2419d8f74f09SStephen Suryaputra 				hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2420d8f74f09SStephen Suryaputra 				hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2421d8f74f09SStephen Suryaputra 				hash_keys.tags.flow_label = flkeys->tags.flow_label;
2422d8f74f09SStephen Suryaputra 				hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2423d8f74f09SStephen Suryaputra 			} else {
2424d8f74f09SStephen Suryaputra 				/* Same as case 0 */
2425d8f74f09SStephen Suryaputra 				hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2426d8f74f09SStephen Suryaputra 				ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2427d8f74f09SStephen Suryaputra 			}
2428d8f74f09SStephen Suryaputra 		} else {
2429d8f74f09SStephen Suryaputra 			/* Same as case 0 */
2430d8f74f09SStephen Suryaputra 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2431d8f74f09SStephen Suryaputra 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2432d8f74f09SStephen Suryaputra 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2433d8f74f09SStephen Suryaputra 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2434d8f74f09SStephen Suryaputra 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2435d8f74f09SStephen Suryaputra 		}
2436d8f74f09SStephen Suryaputra 		break;
2437b4bac172SDavid Ahern 	}
24389a2a537aSDavid Ahern 	mhash = flow_hash_from_keys(&hash_keys);
243923aebdacSJakub Sitnicki 
24409a2a537aSDavid Ahern 	return mhash >> 1;
244123aebdacSJakub Sitnicki }
244223aebdacSJakub Sitnicki 
244367f415ddSWei Wang /* Called with rcu held */
2444c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
2445c71099acSThomas Graf {
2446b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2447c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
244867f415ddSWei Wang 	int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
2449904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
24504c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
2451e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
24524c9483b2SDavid S. Miller 		.daddr = iph->daddr,
24534c9483b2SDavid S. Miller 		.saddr = iph->saddr,
24546502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
24554c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
24564c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
2457c71099acSThomas Graf 	};
24585e5d6fedSRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2459adaa70bbSThomas Graf 
2460904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
246146fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2462904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
24635e5d6fedSRoopa Prabhu 
24645e5d6fedSRoopa Prabhu 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
24655e5d6fedSRoopa Prabhu 		flkeys = &_flkeys;
24665e5d6fedSRoopa Prabhu 
246723aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2468b4bac172SDavid Ahern 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
246906e9d040SJiri Benc 	skb_dst_drop(skb);
247067f415ddSWei Wang 	skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
247167f415ddSWei Wang 						      &fl6, skb, flags));
2472c71099acSThomas Graf }
2473c71099acSThomas Graf 
247455cced4fSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_output(struct net *net,
2475b75cc8f9SDavid Ahern 					     struct fib6_table *table,
2476b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
2477b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2478b75cc8f9SDavid Ahern 					     int flags)
2479c71099acSThomas Graf {
2480b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2481c71099acSThomas Graf }
2482c71099acSThomas Graf 
24837d9e5f42SWei Wang struct dst_entry *ip6_route_output_flags_noref(struct net *net,
24847d9e5f42SWei Wang 					       const struct sock *sk,
24856f21c96aSPaolo Abeni 					       struct flowi6 *fl6, int flags)
2486c71099acSThomas Graf {
2487d46a9d67SDavid Ahern 	bool any_src;
2488c71099acSThomas Graf 
24893ede0bbcSRobert Shearman 	if (ipv6_addr_type(&fl6->daddr) &
24903ede0bbcSRobert Shearman 	    (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
24914c1feac5SDavid Ahern 		struct dst_entry *dst;
24924c1feac5SDavid Ahern 
24937d9e5f42SWei Wang 		/* This function does not take refcnt on the dst */
24944c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
2495ca254490SDavid Ahern 		if (dst)
2496ca254490SDavid Ahern 			return dst;
24974c1feac5SDavid Ahern 	}
2498ca254490SDavid Ahern 
24991fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
25004dc27d1cSDavid McCullough 
25017d9e5f42SWei Wang 	flags |= RT6_LOOKUP_F_DST_NOREF;
2502d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
2503741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2504d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
250577d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
2506c71099acSThomas Graf 
2507d46a9d67SDavid Ahern 	if (!any_src)
2508adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
25090c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
25100c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2511adaa70bbSThomas Graf 
2512b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
25131da177e4SLinus Torvalds }
25147d9e5f42SWei Wang EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
25157d9e5f42SWei Wang 
25167d9e5f42SWei Wang struct dst_entry *ip6_route_output_flags(struct net *net,
25177d9e5f42SWei Wang 					 const struct sock *sk,
25187d9e5f42SWei Wang 					 struct flowi6 *fl6,
25197d9e5f42SWei Wang 					 int flags)
25207d9e5f42SWei Wang {
25217d9e5f42SWei Wang         struct dst_entry *dst;
25227d9e5f42SWei Wang         struct rt6_info *rt6;
25237d9e5f42SWei Wang 
25247d9e5f42SWei Wang         rcu_read_lock();
25257d9e5f42SWei Wang         dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
25267d9e5f42SWei Wang         rt6 = (struct rt6_info *)dst;
25277d9e5f42SWei Wang         /* For dst cached in uncached_list, refcnt is already taken. */
25287d9e5f42SWei Wang         if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
25297d9e5f42SWei Wang                 dst = &net->ipv6.ip6_null_entry->dst;
25307d9e5f42SWei Wang                 dst_hold(dst);
25317d9e5f42SWei Wang         }
25327d9e5f42SWei Wang         rcu_read_unlock();
25337d9e5f42SWei Wang 
25347d9e5f42SWei Wang         return dst;
25357d9e5f42SWei Wang }
25366f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
25371da177e4SLinus Torvalds 
25382774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
253914e50e57SDavid S. Miller {
25405c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
25411dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
254214e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
254314e50e57SDavid S. Miller 
25441dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
254562cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
254614e50e57SDavid S. Miller 	if (rt) {
25470a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
254881eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
25490a1f5962SMartin KaFai Lau 
2550d8d1f30bSChangli Gao 		new = &rt->dst;
255114e50e57SDavid S. Miller 		new->__use = 1;
2552352e512cSHerbert Xu 		new->input = dst_discard;
2553ede2059dSEric W. Biederman 		new->output = dst_discard_out;
255414e50e57SDavid S. Miller 
2555defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
255614e50e57SDavid S. Miller 
25571dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
25584e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
25590a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
256014e50e57SDavid S. Miller 
256114e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
256214e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
256314e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
256414e50e57SDavid S. Miller #endif
256514e50e57SDavid S. Miller 	}
256614e50e57SDavid S. Miller 
256769ead7afSDavid S. Miller 	dst_release(dst_orig);
256869ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
256914e50e57SDavid S. Miller }
257014e50e57SDavid S. Miller 
25711da177e4SLinus Torvalds /*
25721da177e4SLinus Torvalds  *	Destination cache support functions
25731da177e4SLinus Torvalds  */
25741da177e4SLinus Torvalds 
25758d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie)
25763da59bd9SMartin KaFai Lau {
257736143645SSteffen Klassert 	u32 rt_cookie = 0;
2578c5cff856SWei Wang 
25798ae86971SDavid Ahern 	if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
258093531c67SDavid Ahern 		return false;
258193531c67SDavid Ahern 
258293531c67SDavid Ahern 	if (fib6_check_expired(f6i))
258393531c67SDavid Ahern 		return false;
258493531c67SDavid Ahern 
258593531c67SDavid Ahern 	return true;
258693531c67SDavid Ahern }
258793531c67SDavid Ahern 
2588a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt,
2589a68886a6SDavid Ahern 				   struct fib6_info *from,
2590a68886a6SDavid Ahern 				   u32 cookie)
25913da59bd9SMartin KaFai Lau {
2592c5cff856SWei Wang 	u32 rt_cookie = 0;
2593c5cff856SWei Wang 
259449d05fe2SDavid Ahern 	if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
259593531c67SDavid Ahern 	    rt_cookie != cookie)
25963da59bd9SMartin KaFai Lau 		return NULL;
25973da59bd9SMartin KaFai Lau 
25983da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
25993da59bd9SMartin KaFai Lau 		return NULL;
26003da59bd9SMartin KaFai Lau 
26013da59bd9SMartin KaFai Lau 	return &rt->dst;
26023da59bd9SMartin KaFai Lau }
26033da59bd9SMartin KaFai Lau 
2604a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2605a68886a6SDavid Ahern 					    struct fib6_info *from,
2606a68886a6SDavid Ahern 					    u32 cookie)
26073da59bd9SMartin KaFai Lau {
26085973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
26095973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2610a68886a6SDavid Ahern 	    fib6_check(from, cookie))
26113da59bd9SMartin KaFai Lau 		return &rt->dst;
26123da59bd9SMartin KaFai Lau 	else
26133da59bd9SMartin KaFai Lau 		return NULL;
26143da59bd9SMartin KaFai Lau }
26153da59bd9SMartin KaFai Lau 
2616bbd807dfSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
2617bbd807dfSBrian Vazquez 							u32 cookie)
26181da177e4SLinus Torvalds {
2619a87b7dc9SDavid Ahern 	struct dst_entry *dst_ret;
2620a68886a6SDavid Ahern 	struct fib6_info *from;
26211da177e4SLinus Torvalds 	struct rt6_info *rt;
26221da177e4SLinus Torvalds 
2623a87b7dc9SDavid Ahern 	rt = container_of(dst, struct rt6_info, dst);
2624a87b7dc9SDavid Ahern 
26258f34e53bSDavid Ahern 	if (rt->sernum)
26268f34e53bSDavid Ahern 		return rt6_is_valid(rt) ? dst : NULL;
26278f34e53bSDavid Ahern 
2628a87b7dc9SDavid Ahern 	rcu_read_lock();
26291da177e4SLinus Torvalds 
26306f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
26316f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
26326f3118b5SNicolas Dichtel 	 * into this function always.
26336f3118b5SNicolas Dichtel 	 */
2634e3bc10bdSHannes Frederic Sowa 
2635a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
26364b32b5adSMartin KaFai Lau 
2637a68886a6SDavid Ahern 	if (from && (rt->rt6i_flags & RTF_PCPU ||
2638a68886a6SDavid Ahern 	    unlikely(!list_empty(&rt->rt6i_uncached))))
2639a68886a6SDavid Ahern 		dst_ret = rt6_dst_from_check(rt, from, cookie);
26403da59bd9SMartin KaFai Lau 	else
2641a68886a6SDavid Ahern 		dst_ret = rt6_check(rt, from, cookie);
2642a87b7dc9SDavid Ahern 
2643a87b7dc9SDavid Ahern 	rcu_read_unlock();
2644a87b7dc9SDavid Ahern 
2645a87b7dc9SDavid Ahern 	return dst_ret;
26461da177e4SLinus Torvalds }
26479c97921aSBrian Vazquez EXPORT_INDIRECT_CALLABLE(ip6_dst_check);
26481da177e4SLinus Torvalds 
26491da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
26501da177e4SLinus Torvalds {
26511da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
26521da177e4SLinus Torvalds 
26531da177e4SLinus Torvalds 	if (rt) {
265454c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
2655c3c14da0SDavid Ahern 			rcu_read_lock();
265654c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
265793531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
265854c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
26591da177e4SLinus Torvalds 			}
2660c3c14da0SDavid Ahern 			rcu_read_unlock();
266154c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
266254c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
266354c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
266454c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
266554c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
266654c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
26671da177e4SLinus Torvalds }
26681da177e4SLinus Torvalds 
26691da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
26701da177e4SLinus Torvalds {
26711da177e4SLinus Torvalds 	struct rt6_info *rt;
26721da177e4SLinus Torvalds 
26733ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
26741da177e4SLinus Torvalds 
2675adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
26761da177e4SLinus Torvalds 	if (rt) {
26778a14e46fSDavid Ahern 		rcu_read_lock();
26781eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
267993531c67SDavid Ahern 			rt6_remove_exception_rt(rt);
2680c5cff856SWei Wang 		} else {
2681a68886a6SDavid Ahern 			struct fib6_info *from;
2682c5cff856SWei Wang 			struct fib6_node *fn;
2683c5cff856SWei Wang 
2684a68886a6SDavid Ahern 			from = rcu_dereference(rt->from);
2685a68886a6SDavid Ahern 			if (from) {
2686a68886a6SDavid Ahern 				fn = rcu_dereference(from->fib6_node);
2687c5cff856SWei Wang 				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2688c5cff856SWei Wang 					fn->fn_sernum = -1;
2689a68886a6SDavid Ahern 			}
26901da177e4SLinus Torvalds 		}
26911da177e4SLinus Torvalds 		rcu_read_unlock();
26921da177e4SLinus Torvalds 	}
26931da177e4SLinus Torvalds }
26941da177e4SLinus Torvalds 
26956a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout)
26966a3e030fSDavid Ahern {
2697a68886a6SDavid Ahern 	if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2698a68886a6SDavid Ahern 		struct fib6_info *from;
2699a68886a6SDavid Ahern 
2700a68886a6SDavid Ahern 		rcu_read_lock();
2701a68886a6SDavid Ahern 		from = rcu_dereference(rt0->from);
2702a68886a6SDavid Ahern 		if (from)
2703a68886a6SDavid Ahern 			rt0->dst.expires = from->expires;
2704a68886a6SDavid Ahern 		rcu_read_unlock();
2705a68886a6SDavid Ahern 	}
27066a3e030fSDavid Ahern 
27076a3e030fSDavid Ahern 	dst_set_expires(&rt0->dst, timeout);
27086a3e030fSDavid Ahern 	rt0->rt6i_flags |= RTF_EXPIRES;
27096700c270SDavid S. Miller }
27101da177e4SLinus Torvalds 
271145e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
271245e4fd26SMartin KaFai Lau {
271345e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
271445e4fd26SMartin KaFai Lau 
2715d4ead6b3SDavid Ahern 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
271645e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
271745e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
271845e4fd26SMartin KaFai Lau }
271945e4fd26SMartin KaFai Lau 
27200d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
27210d3f6d29SMartin KaFai Lau {
27220d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
27231490ed2aSPaolo Abeni 		(rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
27240d3f6d29SMartin KaFai Lau }
27250d3f6d29SMartin KaFai Lau 
272645e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2727bd085ef6SHangbin Liu 				 const struct ipv6hdr *iph, u32 mtu,
2728bd085ef6SHangbin Liu 				 bool confirm_neigh)
27291da177e4SLinus Torvalds {
27300dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
27311da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
27321da177e4SLinus Torvalds 
273309454fd0SMaciej Żenczykowski 	/* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
273409454fd0SMaciej Żenczykowski 	 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
273509454fd0SMaciej Żenczykowski 	 * [see also comment in rt6_mtu_change_route()]
273609454fd0SMaciej Żenczykowski 	 */
273719bda36cSXin Long 
273845e4fd26SMartin KaFai Lau 	if (iph) {
273945e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
274045e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
274145e4fd26SMartin KaFai Lau 	} else if (sk) {
274245e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
274345e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
274445e4fd26SMartin KaFai Lau 	} else {
27450dec879fSJulian Anastasov 		daddr = NULL;
27460dec879fSJulian Anastasov 		saddr = NULL;
27471da177e4SLinus Torvalds 	}
2748bd085ef6SHangbin Liu 
2749bd085ef6SHangbin Liu 	if (confirm_neigh)
27500dec879fSJulian Anastasov 		dst_confirm_neigh(dst, daddr);
2751bd085ef6SHangbin Liu 
27524a65dff8SGeorg Kohmann 	if (mtu < IPV6_MIN_MTU)
27534a65dff8SGeorg Kohmann 		return;
27540dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
27550dec879fSJulian Anastasov 		return;
27560dec879fSJulian Anastasov 
27570dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
27580dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
27592b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
27602b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
27612b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
27620dec879fSJulian Anastasov 	} else if (daddr) {
276385bd05deSDavid Ahern 		struct fib6_result res = {};
27640dec879fSJulian Anastasov 		struct rt6_info *nrt6;
27650dec879fSJulian Anastasov 
27664d85cd0cSDavid Ahern 		rcu_read_lock();
276785bd05deSDavid Ahern 		res.f6i = rcu_dereference(rt6->from);
276843a4b60dSDavid Ahern 		if (!res.f6i)
276943a4b60dSDavid Ahern 			goto out_unlock;
277043a4b60dSDavid Ahern 
27717d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
27727d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
27737d21fec9SDavid Ahern 
27742d44234bSDavid Ahern 		if (res.f6i->nh) {
27752d44234bSDavid Ahern 			struct fib6_nh_match_arg arg = {
27762d44234bSDavid Ahern 				.dev = dst->dev,
27772d44234bSDavid Ahern 				.gw = &rt6->rt6i_gateway,
27782d44234bSDavid Ahern 			};
27792d44234bSDavid Ahern 
27802d44234bSDavid Ahern 			nexthop_for_each_fib6_nh(res.f6i->nh,
27812d44234bSDavid Ahern 						 fib6_nh_find_match, &arg);
27822d44234bSDavid Ahern 
27832d44234bSDavid Ahern 			/* fib6_info uses a nexthop that does not have fib6_nh
27842d44234bSDavid Ahern 			 * using the dst->dev + gw. Should be impossible.
27852d44234bSDavid Ahern 			 */
278643a4b60dSDavid Ahern 			if (!arg.match)
278743a4b60dSDavid Ahern 				goto out_unlock;
27882d44234bSDavid Ahern 
27892d44234bSDavid Ahern 			res.nh = arg.match;
27902d44234bSDavid Ahern 		} else {
27912d44234bSDavid Ahern 			res.nh = res.f6i->fib6_nh;
27922d44234bSDavid Ahern 		}
27932d44234bSDavid Ahern 
279485bd05deSDavid Ahern 		nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
279545e4fd26SMartin KaFai Lau 		if (nrt6) {
279645e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
27975012f0a5SDavid Ahern 			if (rt6_insert_exception(nrt6, &res))
27982b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
279945e4fd26SMartin KaFai Lau 		}
280043a4b60dSDavid Ahern out_unlock:
2801a68886a6SDavid Ahern 		rcu_read_unlock();
280245e4fd26SMartin KaFai Lau 	}
280345e4fd26SMartin KaFai Lau }
280445e4fd26SMartin KaFai Lau 
280545e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2806bd085ef6SHangbin Liu 			       struct sk_buff *skb, u32 mtu,
2807bd085ef6SHangbin Liu 			       bool confirm_neigh)
280845e4fd26SMartin KaFai Lau {
2809bd085ef6SHangbin Liu 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2810bd085ef6SHangbin Liu 			     confirm_neigh);
28111da177e4SLinus Torvalds }
28121da177e4SLinus Torvalds 
281342ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2814e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
281581aded24SDavid S. Miller {
281681aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
281781aded24SDavid S. Miller 	struct dst_entry *dst;
2818dc92095dSMaciej Żenczykowski 	struct flowi6 fl6 = {
2819dc92095dSMaciej Żenczykowski 		.flowi6_oif = oif,
2820dc92095dSMaciej Żenczykowski 		.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2821dc92095dSMaciej Żenczykowski 		.daddr = iph->daddr,
2822dc92095dSMaciej Żenczykowski 		.saddr = iph->saddr,
2823dc92095dSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
2824dc92095dSMaciej Żenczykowski 		.flowi6_uid = uid,
2825dc92095dSMaciej Żenczykowski 	};
282681aded24SDavid S. Miller 
282781aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
282881aded24SDavid S. Miller 	if (!dst->error)
2829bd085ef6SHangbin Liu 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
283081aded24SDavid S. Miller 	dst_release(dst);
283181aded24SDavid S. Miller }
283281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
283381aded24SDavid S. Miller 
283481aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
283581aded24SDavid S. Miller {
28367ddacfa5SDavid Ahern 	int oif = sk->sk_bound_dev_if;
283733c162a9SMartin KaFai Lau 	struct dst_entry *dst;
283833c162a9SMartin KaFai Lau 
28397ddacfa5SDavid Ahern 	if (!oif && skb->dev)
28407ddacfa5SDavid Ahern 		oif = l3mdev_master_ifindex(skb->dev);
28417ddacfa5SDavid Ahern 
28427ddacfa5SDavid Ahern 	ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
284333c162a9SMartin KaFai Lau 
284433c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
284533c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
284633c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
284733c162a9SMartin KaFai Lau 		return;
284833c162a9SMartin KaFai Lau 
284933c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
285033c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
285133c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
285233c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
285381aded24SDavid S. Miller }
285481aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
285581aded24SDavid S. Miller 
28567d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
28577d6850f7SAlexey Kodanev 			   const struct flowi6 *fl6)
28587d6850f7SAlexey Kodanev {
28597d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
28607d6850f7SAlexey Kodanev 	struct ipv6_pinfo *np = inet6_sk(sk);
28617d6850f7SAlexey Kodanev #endif
28627d6850f7SAlexey Kodanev 
28637d6850f7SAlexey Kodanev 	ip6_dst_store(sk, dst,
28647d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
28657d6850f7SAlexey Kodanev 		      &sk->sk_v6_daddr : NULL,
28667d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
28677d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
28687d6850f7SAlexey Kodanev 		      &np->saddr :
28697d6850f7SAlexey Kodanev #endif
28707d6850f7SAlexey Kodanev 		      NULL);
28717d6850f7SAlexey Kodanev }
28727d6850f7SAlexey Kodanev 
28739b6b35abSDavid Ahern static bool ip6_redirect_nh_match(const struct fib6_result *res,
28740b34eb00SDavid Ahern 				  struct flowi6 *fl6,
28750b34eb00SDavid Ahern 				  const struct in6_addr *gw,
28760b34eb00SDavid Ahern 				  struct rt6_info **ret)
28770b34eb00SDavid Ahern {
28789b6b35abSDavid Ahern 	const struct fib6_nh *nh = res->nh;
28799b6b35abSDavid Ahern 
28800b34eb00SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
28810b34eb00SDavid Ahern 	    fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
28820b34eb00SDavid Ahern 		return false;
28830b34eb00SDavid Ahern 
28840b34eb00SDavid Ahern 	/* rt_cache's gateway might be different from its 'parent'
28850b34eb00SDavid Ahern 	 * in the case of an ip redirect.
28860b34eb00SDavid Ahern 	 * So we keep searching in the exception table if the gateway
28870b34eb00SDavid Ahern 	 * is different.
28880b34eb00SDavid Ahern 	 */
28890b34eb00SDavid Ahern 	if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
28900b34eb00SDavid Ahern 		struct rt6_info *rt_cache;
28910b34eb00SDavid Ahern 
28929b6b35abSDavid Ahern 		rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
28930b34eb00SDavid Ahern 		if (rt_cache &&
28940b34eb00SDavid Ahern 		    ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
28950b34eb00SDavid Ahern 			*ret = rt_cache;
28960b34eb00SDavid Ahern 			return true;
28970b34eb00SDavid Ahern 		}
28980b34eb00SDavid Ahern 		return false;
28990b34eb00SDavid Ahern 	}
29000b34eb00SDavid Ahern 	return true;
29010b34eb00SDavid Ahern }
29020b34eb00SDavid Ahern 
2903c55c8988SDavid Ahern struct fib6_nh_rd_arg {
2904c55c8988SDavid Ahern 	struct fib6_result	*res;
2905c55c8988SDavid Ahern 	struct flowi6		*fl6;
2906c55c8988SDavid Ahern 	const struct in6_addr	*gw;
2907c55c8988SDavid Ahern 	struct rt6_info		**ret;
2908c55c8988SDavid Ahern };
2909c55c8988SDavid Ahern 
2910c55c8988SDavid Ahern static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2911c55c8988SDavid Ahern {
2912c55c8988SDavid Ahern 	struct fib6_nh_rd_arg *arg = _arg;
2913c55c8988SDavid Ahern 
2914c55c8988SDavid Ahern 	arg->res->nh = nh;
2915c55c8988SDavid Ahern 	return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2916c55c8988SDavid Ahern }
2917c55c8988SDavid Ahern 
2918b55b76b2SDuan Jiong /* Handle redirects */
2919b55b76b2SDuan Jiong struct ip6rd_flowi {
2920b55b76b2SDuan Jiong 	struct flowi6 fl6;
2921b55b76b2SDuan Jiong 	struct in6_addr gateway;
2922b55b76b2SDuan Jiong };
2923b55b76b2SDuan Jiong 
292455cced4fSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net,
2925b55b76b2SDuan Jiong 					     struct fib6_table *table,
2926b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2927b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2928b55b76b2SDuan Jiong 					     int flags)
2929b55b76b2SDuan Jiong {
2930b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
29310b34eb00SDavid Ahern 	struct rt6_info *ret = NULL;
29329b6b35abSDavid Ahern 	struct fib6_result res = {};
2933c55c8988SDavid Ahern 	struct fib6_nh_rd_arg arg = {
2934c55c8988SDavid Ahern 		.res = &res,
2935c55c8988SDavid Ahern 		.fl6 = fl6,
2936c55c8988SDavid Ahern 		.gw  = &rdfl->gateway,
2937c55c8988SDavid Ahern 		.ret = &ret
2938c55c8988SDavid Ahern 	};
29398d1c802bSDavid Ahern 	struct fib6_info *rt;
2940b55b76b2SDuan Jiong 	struct fib6_node *fn;
2941b55b76b2SDuan Jiong 
294231680ac2SDavid Ahern 	/* l3mdev_update_flow overrides oif if the device is enslaved; in
294331680ac2SDavid Ahern 	 * this case we must match on the real ingress device, so reset it
294431680ac2SDavid Ahern 	 */
294531680ac2SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
294631680ac2SDavid Ahern 		fl6->flowi6_oif = skb->dev->ifindex;
294731680ac2SDavid Ahern 
2948b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
294967c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2950b55b76b2SDuan Jiong 	 *
2951b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2952b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2953b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2954b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2955b55b76b2SDuan Jiong 	 * routes.
2956b55b76b2SDuan Jiong 	 */
2957b55b76b2SDuan Jiong 
295866f5d6ceSWei Wang 	rcu_read_lock();
29596454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2960b55b76b2SDuan Jiong restart:
296166f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
29629b6b35abSDavid Ahern 		res.f6i = rt;
296314895687SDavid Ahern 		if (fib6_check_expired(rt))
2964b55b76b2SDuan Jiong 			continue;
296593c2fb25SDavid Ahern 		if (rt->fib6_flags & RTF_REJECT)
2966b55b76b2SDuan Jiong 			break;
2967c55c8988SDavid Ahern 		if (unlikely(rt->nh)) {
2968c55c8988SDavid Ahern 			if (nexthop_is_blackhole(rt->nh))
2969c55c8988SDavid Ahern 				continue;
2970c55c8988SDavid Ahern 			/* on match, res->nh is filled in and potentially ret */
2971c55c8988SDavid Ahern 			if (nexthop_for_each_fib6_nh(rt->nh,
2972c55c8988SDavid Ahern 						     fib6_nh_redirect_match,
2973c55c8988SDavid Ahern 						     &arg))
29740b34eb00SDavid Ahern 				goto out;
2975c55c8988SDavid Ahern 		} else {
2976c55c8988SDavid Ahern 			res.nh = rt->fib6_nh;
2977c55c8988SDavid Ahern 			if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2978c55c8988SDavid Ahern 						  &ret))
2979c55c8988SDavid Ahern 				goto out;
2980c55c8988SDavid Ahern 		}
2981b55b76b2SDuan Jiong 	}
2982b55b76b2SDuan Jiong 
2983b55b76b2SDuan Jiong 	if (!rt)
2984421842edSDavid Ahern 		rt = net->ipv6.fib6_null_entry;
298593c2fb25SDavid Ahern 	else if (rt->fib6_flags & RTF_REJECT) {
298623fb93a4SDavid Ahern 		ret = net->ipv6.ip6_null_entry;
2987b0a1ba59SMartin KaFai Lau 		goto out;
2988b0a1ba59SMartin KaFai Lau 	}
2989b0a1ba59SMartin KaFai Lau 
2990421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
2991a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2992a3c00e46SMartin KaFai Lau 		if (fn)
2993a3c00e46SMartin KaFai Lau 			goto restart;
2994b55b76b2SDuan Jiong 	}
2995a3c00e46SMartin KaFai Lau 
29969b6b35abSDavid Ahern 	res.f6i = rt;
29971cf844c7SDavid Ahern 	res.nh = rt->fib6_nh;
2998b0a1ba59SMartin KaFai Lau out:
29997d21fec9SDavid Ahern 	if (ret) {
300010585b43SDavid Ahern 		ip6_hold_safe(net, &ret);
30017d21fec9SDavid Ahern 	} else {
30027d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
30037d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
30049b6b35abSDavid Ahern 		ret = ip6_create_rt_rcu(&res);
30057d21fec9SDavid Ahern 	}
3006b55b76b2SDuan Jiong 
300766f5d6ceSWei Wang 	rcu_read_unlock();
3008b55b76b2SDuan Jiong 
30098ff2e5b2SDavid Ahern 	trace_fib6_table_lookup(net, &res, table, fl6);
301023fb93a4SDavid Ahern 	return ret;
3011b55b76b2SDuan Jiong };
3012b55b76b2SDuan Jiong 
3013b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
3014b55b76b2SDuan Jiong 					    const struct flowi6 *fl6,
3015b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
3016b55b76b2SDuan Jiong 					    const struct in6_addr *gateway)
3017b55b76b2SDuan Jiong {
3018b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
3019b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
3020b55b76b2SDuan Jiong 
3021b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
3022b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
3023b55b76b2SDuan Jiong 
3024b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, &rdfl.fl6, skb,
3025b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
3026b55b76b2SDuan Jiong }
3027b55b76b2SDuan Jiong 
3028e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3029e2d118a1SLorenzo Colitti 		  kuid_t uid)
30303a5ad2eeSDavid S. Miller {
30313a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
30323a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
30331f7f10acSMaciej Żenczykowski 	struct flowi6 fl6 = {
30341f7f10acSMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
30351f7f10acSMaciej Żenczykowski 		.flowi6_oif = oif,
30361f7f10acSMaciej Żenczykowski 		.flowi6_mark = mark,
30371f7f10acSMaciej Żenczykowski 		.daddr = iph->daddr,
30381f7f10acSMaciej Żenczykowski 		.saddr = iph->saddr,
30391f7f10acSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
30401f7f10acSMaciej Żenczykowski 		.flowi6_uid = uid,
30411f7f10acSMaciej Żenczykowski 	};
30423a5ad2eeSDavid S. Miller 
3043b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
30446700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
30453a5ad2eeSDavid S. Miller 	dst_release(dst);
30463a5ad2eeSDavid S. Miller }
30473a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
30483a5ad2eeSDavid S. Miller 
3049d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
3050c92a59ecSDuan Jiong {
3051c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
3052c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3053c92a59ecSDuan Jiong 	struct dst_entry *dst;
30540b26fb17SMaciej Żenczykowski 	struct flowi6 fl6 = {
30550b26fb17SMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
30560b26fb17SMaciej Żenczykowski 		.flowi6_oif = oif,
30570b26fb17SMaciej Żenczykowski 		.daddr = msg->dest,
30580b26fb17SMaciej Żenczykowski 		.saddr = iph->daddr,
30590b26fb17SMaciej Żenczykowski 		.flowi6_uid = sock_net_uid(net, NULL),
30600b26fb17SMaciej Żenczykowski 	};
3061c92a59ecSDuan Jiong 
3062b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
3063c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
3064c92a59ecSDuan Jiong 	dst_release(dst);
3065c92a59ecSDuan Jiong }
3066c92a59ecSDuan Jiong 
30673a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
30683a5ad2eeSDavid S. Miller {
3069e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3070e2d118a1SLorenzo Colitti 		     sk->sk_uid);
30713a5ad2eeSDavid S. Miller }
30723a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
30733a5ad2eeSDavid S. Miller 
30740dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
30751da177e4SLinus Torvalds {
30760dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
30770dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
30780dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
30790dbaee3bSDavid S. Miller 
30801da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
30811da177e4SLinus Torvalds 
30825578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
30835578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
30841da177e4SLinus Torvalds 
30851da177e4SLinus Torvalds 	/*
30861da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
30871da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
30881da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
30891da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
30901da177e4SLinus Torvalds 	 */
30911da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
30921da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
30931da177e4SLinus Torvalds 	return mtu;
30941da177e4SLinus Torvalds }
30951da177e4SLinus Torvalds 
3096f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_SCOPE unsigned int ip6_mtu(const struct dst_entry *dst)
3097d33e4553SDavid S. Miller {
3098d33e4553SDavid S. Miller 	struct inet6_dev *idev;
3099d4ead6b3SDavid Ahern 	unsigned int mtu;
3100618f9bc7SSteffen Klassert 
31014b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
31024b32b5adSMartin KaFai Lau 	if (mtu)
31034b32b5adSMartin KaFai Lau 		goto out;
31044b32b5adSMartin KaFai Lau 
3105618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
3106d33e4553SDavid S. Miller 
3107d33e4553SDavid S. Miller 	rcu_read_lock();
3108d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
3109d33e4553SDavid S. Miller 	if (idev)
3110d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
3111d33e4553SDavid S. Miller 	rcu_read_unlock();
3112d33e4553SDavid S. Miller 
311330f78d8eSEric Dumazet out:
311414972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
311514972cbdSRoopa Prabhu 
311614972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
3117d33e4553SDavid S. Miller }
31189c97921aSBrian Vazquez EXPORT_INDIRECT_CALLABLE(ip6_mtu);
3119d33e4553SDavid S. Miller 
3120901731b8SDavid Ahern /* MTU selection:
3121901731b8SDavid Ahern  * 1. mtu on route is locked - use it
3122901731b8SDavid Ahern  * 2. mtu from nexthop exception
3123901731b8SDavid Ahern  * 3. mtu from egress device
3124901731b8SDavid Ahern  *
3125901731b8SDavid Ahern  * based on ip6_dst_mtu_forward and exception logic of
3126901731b8SDavid Ahern  * rt6_find_cached_rt; called with rcu_read_lock
3127901731b8SDavid Ahern  */
3128b748f260SDavid Ahern u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3129b748f260SDavid Ahern 		      const struct in6_addr *daddr,
3130b748f260SDavid Ahern 		      const struct in6_addr *saddr)
3131901731b8SDavid Ahern {
3132b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
3133b748f260SDavid Ahern 	struct fib6_info *f6i = res->f6i;
3134901731b8SDavid Ahern 	struct inet6_dev *idev;
3135510e2cedSWei Wang 	struct rt6_info *rt;
3136901731b8SDavid Ahern 	u32 mtu = 0;
3137901731b8SDavid Ahern 
3138901731b8SDavid Ahern 	if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3139901731b8SDavid Ahern 		mtu = f6i->fib6_pmtu;
3140901731b8SDavid Ahern 		if (mtu)
3141901731b8SDavid Ahern 			goto out;
3142901731b8SDavid Ahern 	}
3143901731b8SDavid Ahern 
3144510e2cedSWei Wang 	rt = rt6_find_cached_rt(res, daddr, saddr);
3145510e2cedSWei Wang 	if (unlikely(rt)) {
3146510e2cedSWei Wang 		mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3147510e2cedSWei Wang 	} else {
3148b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
3149901731b8SDavid Ahern 
3150901731b8SDavid Ahern 		mtu = IPV6_MIN_MTU;
3151901731b8SDavid Ahern 		idev = __in6_dev_get(dev);
3152901731b8SDavid Ahern 		if (idev && idev->cnf.mtu6 > mtu)
3153901731b8SDavid Ahern 			mtu = idev->cnf.mtu6;
3154901731b8SDavid Ahern 	}
3155901731b8SDavid Ahern 
3156901731b8SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3157901731b8SDavid Ahern out:
3158b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
3159901731b8SDavid Ahern }
3160901731b8SDavid Ahern 
31613b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
316287a11578SDavid S. Miller 				  struct flowi6 *fl6)
31631da177e4SLinus Torvalds {
316487a11578SDavid S. Miller 	struct dst_entry *dst;
31651da177e4SLinus Torvalds 	struct rt6_info *rt;
31661da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
3167c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
31681da177e4SLinus Torvalds 
316938308473SDavid S. Miller 	if (unlikely(!idev))
3170122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
31711da177e4SLinus Torvalds 
3172ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
317338308473SDavid S. Miller 	if (unlikely(!rt)) {
31741da177e4SLinus Torvalds 		in6_dev_put(idev);
317587a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
31761da177e4SLinus Torvalds 		goto out;
31771da177e4SLinus Torvalds 	}
31781da177e4SLinus Torvalds 
3179588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
31808e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
3181550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
318287a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
31838e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
31848e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
318514edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
31861da177e4SLinus Torvalds 
31874c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
3188587fea74SWei Wang 	 * do proper release of the net_device
3189587fea74SWei Wang 	 */
3190587fea74SWei Wang 	rt6_uncached_list_add(rt);
319181eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
31921da177e4SLinus Torvalds 
319387a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
319487a11578SDavid S. Miller 
31951da177e4SLinus Torvalds out:
319687a11578SDavid S. Miller 	return dst;
31971da177e4SLinus Torvalds }
31981da177e4SLinus Torvalds 
3199569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
32001da177e4SLinus Torvalds {
320186393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
32027019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
32037019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
32047019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
32057019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
32067019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
3207fc66f95cSEric Dumazet 	int entries;
32081da177e4SLinus Torvalds 
3209fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
3210cf86a086SEric Dumazet 	if (entries > rt_max_size)
3211cf86a086SEric Dumazet 		entries = dst_entries_get_slow(ops);
3212cf86a086SEric Dumazet 
321349a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
3214fc66f95cSEric Dumazet 	    entries <= rt_max_size)
32151da177e4SLinus Torvalds 		goto out;
32161da177e4SLinus Torvalds 
32176891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
321814956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
3219fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
3220fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
32217019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
32221da177e4SLinus Torvalds out:
32237019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
3224fc66f95cSEric Dumazet 	return entries > rt_max_size;
32251da177e4SLinus Torvalds }
32261da177e4SLinus Torvalds 
3227b2c709ccSDavid Ahern static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3228b2c709ccSDavid Ahern 			       const struct in6_addr *gw_addr, u32 tbid,
3229b2c709ccSDavid Ahern 			       int flags, struct fib6_result *res)
32308c14586fSDavid Ahern {
32318c14586fSDavid Ahern 	struct flowi6 fl6 = {
32328c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
32338c14586fSDavid Ahern 		.daddr = *gw_addr,
32348c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
32358c14586fSDavid Ahern 	};
32368c14586fSDavid Ahern 	struct fib6_table *table;
3237b2c709ccSDavid Ahern 	int err;
32388c14586fSDavid Ahern 
3239f4797b33SDavid Ahern 	table = fib6_get_table(net, tbid);
32408c14586fSDavid Ahern 	if (!table)
3241b2c709ccSDavid Ahern 		return -EINVAL;
32428c14586fSDavid Ahern 
32438c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
32448c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
32458c14586fSDavid Ahern 
3246f4797b33SDavid Ahern 	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
32478c14586fSDavid Ahern 
3248b2c709ccSDavid Ahern 	err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3249b2c709ccSDavid Ahern 	if (!err && res->f6i != net->ipv6.fib6_null_entry)
3250b2c709ccSDavid Ahern 		fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3251b2c709ccSDavid Ahern 				 cfg->fc_ifindex != 0, NULL, flags);
32528c14586fSDavid Ahern 
3253b2c709ccSDavid Ahern 	return err;
32548c14586fSDavid Ahern }
32558c14586fSDavid Ahern 
3256fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net,
3257fc1e64e1SDavid Ahern 				     struct fib6_config *cfg,
32589fbb704cSDavid Ahern 				     const struct net_device *dev,
3259fc1e64e1SDavid Ahern 				     struct netlink_ext_ack *extack)
3260fc1e64e1SDavid Ahern {
3261b2c709ccSDavid Ahern 	u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
3262fc1e64e1SDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
3263b2c709ccSDavid Ahern 	struct fib6_result res = {};
3264fc1e64e1SDavid Ahern 	int err;
3265fc1e64e1SDavid Ahern 
3266b2c709ccSDavid Ahern 	err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3267b2c709ccSDavid Ahern 	if (!err && !(res.fib6_flags & RTF_REJECT) &&
32684ed591c8SDavid Ahern 	    /* ignore match if it is the default route */
3269b2c709ccSDavid Ahern 	    !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3270b2c709ccSDavid Ahern 	    (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
327144750f84SDavid Ahern 		NL_SET_ERR_MSG(extack,
327244750f84SDavid Ahern 			       "Nexthop has invalid gateway or device mismatch");
3273fc1e64e1SDavid Ahern 		err = -EINVAL;
3274fc1e64e1SDavid Ahern 	}
3275fc1e64e1SDavid Ahern 
3276fc1e64e1SDavid Ahern 	return err;
3277fc1e64e1SDavid Ahern }
3278fc1e64e1SDavid Ahern 
32791edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net,
32801edce99fSDavid Ahern 			      struct fib6_config *cfg,
32811edce99fSDavid Ahern 			      struct net_device **_dev,
32821edce99fSDavid Ahern 			      struct inet6_dev **idev)
32831edce99fSDavid Ahern {
32841edce99fSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
32851edce99fSDavid Ahern 	struct net_device *dev = _dev ? *_dev : NULL;
3286b2c709ccSDavid Ahern 	int flags = RT6_LOOKUP_F_IFACE;
3287b2c709ccSDavid Ahern 	struct fib6_result res = {};
32881edce99fSDavid Ahern 	int err = -EHOSTUNREACH;
32891edce99fSDavid Ahern 
32901edce99fSDavid Ahern 	if (cfg->fc_table) {
3291b2c709ccSDavid Ahern 		err = ip6_nh_lookup_table(net, cfg, gw_addr,
3292b2c709ccSDavid Ahern 					  cfg->fc_table, flags, &res);
3293b2c709ccSDavid Ahern 		/* gw_addr can not require a gateway or resolve to a reject
3294b2c709ccSDavid Ahern 		 * route. If a device is given, it must match the result.
3295b2c709ccSDavid Ahern 		 */
3296b2c709ccSDavid Ahern 		if (err || res.fib6_flags & RTF_REJECT ||
3297b2c709ccSDavid Ahern 		    res.nh->fib_nh_gw_family ||
3298b2c709ccSDavid Ahern 		    (dev && dev != res.nh->fib_nh_dev))
3299b2c709ccSDavid Ahern 			err = -EHOSTUNREACH;
33001edce99fSDavid Ahern 	}
33011edce99fSDavid Ahern 
3302b2c709ccSDavid Ahern 	if (err < 0) {
3303b2c709ccSDavid Ahern 		struct flowi6 fl6 = {
3304b2c709ccSDavid Ahern 			.flowi6_oif = cfg->fc_ifindex,
3305b2c709ccSDavid Ahern 			.daddr = *gw_addr,
3306b2c709ccSDavid Ahern 		};
33071edce99fSDavid Ahern 
3308b2c709ccSDavid Ahern 		err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3309b2c709ccSDavid Ahern 		if (err || res.fib6_flags & RTF_REJECT ||
3310b2c709ccSDavid Ahern 		    res.nh->fib_nh_gw_family)
3311b2c709ccSDavid Ahern 			err = -EHOSTUNREACH;
33121edce99fSDavid Ahern 
3313b2c709ccSDavid Ahern 		if (err)
3314b2c709ccSDavid Ahern 			return err;
3315b2c709ccSDavid Ahern 
3316b2c709ccSDavid Ahern 		fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3317b2c709ccSDavid Ahern 				 cfg->fc_ifindex != 0, NULL, flags);
33181edce99fSDavid Ahern 	}
33191edce99fSDavid Ahern 
33201edce99fSDavid Ahern 	err = 0;
3321b2c709ccSDavid Ahern 	if (dev) {
3322b2c709ccSDavid Ahern 		if (dev != res.nh->fib_nh_dev)
3323b2c709ccSDavid Ahern 			err = -EHOSTUNREACH;
3324b2c709ccSDavid Ahern 	} else {
3325b2c709ccSDavid Ahern 		*_dev = dev = res.nh->fib_nh_dev;
3326b2c709ccSDavid Ahern 		dev_hold(dev);
3327b2c709ccSDavid Ahern 		*idev = in6_dev_get(dev);
3328b2c709ccSDavid Ahern 	}
33291edce99fSDavid Ahern 
33301edce99fSDavid Ahern 	return err;
33311edce99fSDavid Ahern }
33321edce99fSDavid Ahern 
33339fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
33349fbb704cSDavid Ahern 			   struct net_device **_dev, struct inet6_dev **idev,
33359fbb704cSDavid Ahern 			   struct netlink_ext_ack *extack)
33369fbb704cSDavid Ahern {
33379fbb704cSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
33389fbb704cSDavid Ahern 	int gwa_type = ipv6_addr_type(gw_addr);
3339232378e8SDavid Ahern 	bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
33409fbb704cSDavid Ahern 	const struct net_device *dev = *_dev;
3341232378e8SDavid Ahern 	bool need_addr_check = !dev;
33429fbb704cSDavid Ahern 	int err = -EINVAL;
33439fbb704cSDavid Ahern 
33449fbb704cSDavid Ahern 	/* if gw_addr is local we will fail to detect this in case
33459fbb704cSDavid Ahern 	 * address is still TENTATIVE (DAD in progress). rt6_lookup()
33469fbb704cSDavid Ahern 	 * will return already-added prefix route via interface that
33479fbb704cSDavid Ahern 	 * prefix route was assigned to, which might be non-loopback.
33489fbb704cSDavid Ahern 	 */
3349232378e8SDavid Ahern 	if (dev &&
3350232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3351232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
33529fbb704cSDavid Ahern 		goto out;
33539fbb704cSDavid Ahern 	}
33549fbb704cSDavid Ahern 
33559fbb704cSDavid Ahern 	if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
33569fbb704cSDavid Ahern 		/* IPv6 strictly inhibits using not link-local
33579fbb704cSDavid Ahern 		 * addresses as nexthop address.
33589fbb704cSDavid Ahern 		 * Otherwise, router will not able to send redirects.
33599fbb704cSDavid Ahern 		 * It is very good, but in some (rare!) circumstances
33609fbb704cSDavid Ahern 		 * (SIT, PtP, NBMA NOARP links) it is handy to allow
33619fbb704cSDavid Ahern 		 * some exceptions. --ANK
33629fbb704cSDavid Ahern 		 * We allow IPv4-mapped nexthops to support RFC4798-type
33639fbb704cSDavid Ahern 		 * addressing
33649fbb704cSDavid Ahern 		 */
33659fbb704cSDavid Ahern 		if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
33669fbb704cSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
33679fbb704cSDavid Ahern 			goto out;
33689fbb704cSDavid Ahern 		}
33699fbb704cSDavid Ahern 
3370b2c709ccSDavid Ahern 		rcu_read_lock();
3371b2c709ccSDavid Ahern 
33729fbb704cSDavid Ahern 		if (cfg->fc_flags & RTNH_F_ONLINK)
33739fbb704cSDavid Ahern 			err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
33749fbb704cSDavid Ahern 		else
33759fbb704cSDavid Ahern 			err = ip6_route_check_nh(net, cfg, _dev, idev);
33769fbb704cSDavid Ahern 
3377b2c709ccSDavid Ahern 		rcu_read_unlock();
3378b2c709ccSDavid Ahern 
33799fbb704cSDavid Ahern 		if (err)
33809fbb704cSDavid Ahern 			goto out;
33819fbb704cSDavid Ahern 	}
33829fbb704cSDavid Ahern 
33839fbb704cSDavid Ahern 	/* reload in case device was changed */
33849fbb704cSDavid Ahern 	dev = *_dev;
33859fbb704cSDavid Ahern 
33869fbb704cSDavid Ahern 	err = -EINVAL;
33879fbb704cSDavid Ahern 	if (!dev) {
33889fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Egress device not specified");
33899fbb704cSDavid Ahern 		goto out;
33909fbb704cSDavid Ahern 	} else if (dev->flags & IFF_LOOPBACK) {
33919fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack,
33929fbb704cSDavid Ahern 			       "Egress device can not be loopback device for this route");
33939fbb704cSDavid Ahern 		goto out;
33949fbb704cSDavid Ahern 	}
3395232378e8SDavid Ahern 
3396232378e8SDavid Ahern 	/* if we did not check gw_addr above, do so now that the
3397232378e8SDavid Ahern 	 * egress device has been resolved.
3398232378e8SDavid Ahern 	 */
3399232378e8SDavid Ahern 	if (need_addr_check &&
3400232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3401232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3402232378e8SDavid Ahern 		goto out;
3403232378e8SDavid Ahern 	}
3404232378e8SDavid Ahern 
34059fbb704cSDavid Ahern 	err = 0;
34069fbb704cSDavid Ahern out:
34079fbb704cSDavid Ahern 	return err;
34089fbb704cSDavid Ahern }
34099fbb704cSDavid Ahern 
341083c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
341183c44251SDavid Ahern {
341283c44251SDavid Ahern 	if ((flags & RTF_REJECT) ||
341383c44251SDavid Ahern 	    (dev && (dev->flags & IFF_LOOPBACK) &&
341483c44251SDavid Ahern 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
3415aea23c32SDavid Ahern 	     !(flags & (RTF_ANYCAST | RTF_LOCAL))))
341683c44251SDavid Ahern 		return true;
341783c44251SDavid Ahern 
341883c44251SDavid Ahern 	return false;
341983c44251SDavid Ahern }
342083c44251SDavid Ahern 
342183c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
342283c44251SDavid Ahern 		 struct fib6_config *cfg, gfp_t gfp_flags,
342383c44251SDavid Ahern 		 struct netlink_ext_ack *extack)
342483c44251SDavid Ahern {
342583c44251SDavid Ahern 	struct net_device *dev = NULL;
342683c44251SDavid Ahern 	struct inet6_dev *idev = NULL;
342783c44251SDavid Ahern 	int addr_type;
342883c44251SDavid Ahern 	int err;
342983c44251SDavid Ahern 
3430f1741730SDavid Ahern 	fib6_nh->fib_nh_family = AF_INET6;
34311bef4c22SEric Dumazet #ifdef CONFIG_IPV6_ROUTER_PREF
34321bef4c22SEric Dumazet 	fib6_nh->last_probe = jiffies;
34331bef4c22SEric Dumazet #endif
343438428d68SRoopa Prabhu 	if (cfg->fc_is_fdb) {
343538428d68SRoopa Prabhu 		fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
343638428d68SRoopa Prabhu 		fib6_nh->fib_nh_gw_family = AF_INET6;
343738428d68SRoopa Prabhu 		return 0;
343838428d68SRoopa Prabhu 	}
3439f1741730SDavid Ahern 
344083c44251SDavid Ahern 	err = -ENODEV;
344183c44251SDavid Ahern 	if (cfg->fc_ifindex) {
344283c44251SDavid Ahern 		dev = dev_get_by_index(net, cfg->fc_ifindex);
344383c44251SDavid Ahern 		if (!dev)
344483c44251SDavid Ahern 			goto out;
344583c44251SDavid Ahern 		idev = in6_dev_get(dev);
344683c44251SDavid Ahern 		if (!idev)
344783c44251SDavid Ahern 			goto out;
344883c44251SDavid Ahern 	}
344983c44251SDavid Ahern 
345083c44251SDavid Ahern 	if (cfg->fc_flags & RTNH_F_ONLINK) {
345183c44251SDavid Ahern 		if (!dev) {
345283c44251SDavid Ahern 			NL_SET_ERR_MSG(extack,
345383c44251SDavid Ahern 				       "Nexthop device required for onlink");
345483c44251SDavid Ahern 			goto out;
345583c44251SDavid Ahern 		}
345683c44251SDavid Ahern 
345783c44251SDavid Ahern 		if (!(dev->flags & IFF_UP)) {
345883c44251SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
345983c44251SDavid Ahern 			err = -ENETDOWN;
346083c44251SDavid Ahern 			goto out;
346183c44251SDavid Ahern 		}
346283c44251SDavid Ahern 
3463ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
346483c44251SDavid Ahern 	}
346583c44251SDavid Ahern 
3466ad1601aeSDavid Ahern 	fib6_nh->fib_nh_weight = 1;
346783c44251SDavid Ahern 
346883c44251SDavid Ahern 	/* We cannot add true routes via loopback here,
346983c44251SDavid Ahern 	 * they would result in kernel looping; promote them to reject routes
347083c44251SDavid Ahern 	 */
347183c44251SDavid Ahern 	addr_type = ipv6_addr_type(&cfg->fc_dst);
347283c44251SDavid Ahern 	if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
347383c44251SDavid Ahern 		/* hold loopback dev/idev if we haven't done so. */
347483c44251SDavid Ahern 		if (dev != net->loopback_dev) {
347583c44251SDavid Ahern 			if (dev) {
347683c44251SDavid Ahern 				dev_put(dev);
347783c44251SDavid Ahern 				in6_dev_put(idev);
347883c44251SDavid Ahern 			}
347983c44251SDavid Ahern 			dev = net->loopback_dev;
348083c44251SDavid Ahern 			dev_hold(dev);
348183c44251SDavid Ahern 			idev = in6_dev_get(dev);
348283c44251SDavid Ahern 			if (!idev) {
348383c44251SDavid Ahern 				err = -ENODEV;
348483c44251SDavid Ahern 				goto out;
348583c44251SDavid Ahern 			}
348683c44251SDavid Ahern 		}
34877dd73168SDavid Ahern 		goto pcpu_alloc;
348883c44251SDavid Ahern 	}
348983c44251SDavid Ahern 
349083c44251SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY) {
349183c44251SDavid Ahern 		err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
349283c44251SDavid Ahern 		if (err)
349383c44251SDavid Ahern 			goto out;
349483c44251SDavid Ahern 
3495ad1601aeSDavid Ahern 		fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3496bdf00467SDavid Ahern 		fib6_nh->fib_nh_gw_family = AF_INET6;
349783c44251SDavid Ahern 	}
349883c44251SDavid Ahern 
349983c44251SDavid Ahern 	err = -ENODEV;
350083c44251SDavid Ahern 	if (!dev)
350183c44251SDavid Ahern 		goto out;
350283c44251SDavid Ahern 
350383c44251SDavid Ahern 	if (idev->cnf.disable_ipv6) {
350483c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
350583c44251SDavid Ahern 		err = -EACCES;
350683c44251SDavid Ahern 		goto out;
350783c44251SDavid Ahern 	}
350883c44251SDavid Ahern 
350983c44251SDavid Ahern 	if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
351083c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
351183c44251SDavid Ahern 		err = -ENETDOWN;
351283c44251SDavid Ahern 		goto out;
351383c44251SDavid Ahern 	}
351483c44251SDavid Ahern 
351583c44251SDavid Ahern 	if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
351683c44251SDavid Ahern 	    !netif_carrier_ok(dev))
3517ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
351883c44251SDavid Ahern 
3519faee6769SAlexander Aring 	err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
35207dd73168SDavid Ahern 				 cfg->fc_encap_type, cfg, gfp_flags, extack);
35217dd73168SDavid Ahern 	if (err)
35227dd73168SDavid Ahern 		goto out;
35237dd73168SDavid Ahern 
35247dd73168SDavid Ahern pcpu_alloc:
3525f40b6ae2SDavid Ahern 	fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3526f40b6ae2SDavid Ahern 	if (!fib6_nh->rt6i_pcpu) {
3527f40b6ae2SDavid Ahern 		err = -ENOMEM;
3528f40b6ae2SDavid Ahern 		goto out;
3529f40b6ae2SDavid Ahern 	}
3530f40b6ae2SDavid Ahern 
3531ad1601aeSDavid Ahern 	fib6_nh->fib_nh_dev = dev;
3532f1741730SDavid Ahern 	fib6_nh->fib_nh_oif = dev->ifindex;
353383c44251SDavid Ahern 	err = 0;
353483c44251SDavid Ahern out:
353583c44251SDavid Ahern 	if (idev)
353683c44251SDavid Ahern 		in6_dev_put(idev);
353783c44251SDavid Ahern 
353883c44251SDavid Ahern 	if (err) {
3539ad1601aeSDavid Ahern 		lwtstate_put(fib6_nh->fib_nh_lws);
3540ad1601aeSDavid Ahern 		fib6_nh->fib_nh_lws = NULL;
354183c44251SDavid Ahern 		if (dev)
354283c44251SDavid Ahern 			dev_put(dev);
354383c44251SDavid Ahern 	}
354483c44251SDavid Ahern 
354583c44251SDavid Ahern 	return err;
354683c44251SDavid Ahern }
354783c44251SDavid Ahern 
3548dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh)
3549dac7d0f2SDavid Ahern {
3550cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
3551cc5c073aSDavid Ahern 
3552cc5c073aSDavid Ahern 	rcu_read_lock();
3553cc5c073aSDavid Ahern 
3554cc5c073aSDavid Ahern 	fib6_nh_flush_exceptions(fib6_nh, NULL);
3555cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3556cc5c073aSDavid Ahern 	if (bucket) {
3557cc5c073aSDavid Ahern 		rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3558cc5c073aSDavid Ahern 		kfree(bucket);
3559cc5c073aSDavid Ahern 	}
3560cc5c073aSDavid Ahern 
3561cc5c073aSDavid Ahern 	rcu_read_unlock();
3562cc5c073aSDavid Ahern 
3563f40b6ae2SDavid Ahern 	if (fib6_nh->rt6i_pcpu) {
3564f40b6ae2SDavid Ahern 		int cpu;
3565f40b6ae2SDavid Ahern 
3566f40b6ae2SDavid Ahern 		for_each_possible_cpu(cpu) {
3567f40b6ae2SDavid Ahern 			struct rt6_info **ppcpu_rt;
3568f40b6ae2SDavid Ahern 			struct rt6_info *pcpu_rt;
3569f40b6ae2SDavid Ahern 
3570f40b6ae2SDavid Ahern 			ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3571f40b6ae2SDavid Ahern 			pcpu_rt = *ppcpu_rt;
3572f40b6ae2SDavid Ahern 			if (pcpu_rt) {
3573f40b6ae2SDavid Ahern 				dst_dev_put(&pcpu_rt->dst);
3574f40b6ae2SDavid Ahern 				dst_release(&pcpu_rt->dst);
3575f40b6ae2SDavid Ahern 				*ppcpu_rt = NULL;
3576f40b6ae2SDavid Ahern 			}
3577f40b6ae2SDavid Ahern 		}
3578f40b6ae2SDavid Ahern 
3579f40b6ae2SDavid Ahern 		free_percpu(fib6_nh->rt6i_pcpu);
3580f40b6ae2SDavid Ahern 	}
3581f40b6ae2SDavid Ahern 
3582979e276eSDavid Ahern 	fib_nh_common_release(&fib6_nh->nh_common);
3583dac7d0f2SDavid Ahern }
3584dac7d0f2SDavid Ahern 
35858d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3586acb54e3cSDavid Ahern 					      gfp_t gfp_flags,
3587333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
35881da177e4SLinus Torvalds {
35895578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
35908d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3591f88d8ea6SDavid Ahern 	struct nexthop *nh = NULL;
3592c71099acSThomas Graf 	struct fib6_table *table;
3593f88d8ea6SDavid Ahern 	struct fib6_nh *fib6_nh;
35948c5b83f0SRoopa Prabhu 	int err = -EINVAL;
359583c44251SDavid Ahern 	int addr_type;
35961da177e4SLinus Torvalds 
3597557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
3598d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
3599d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3600557c44beSDavid Ahern 		goto out;
3601d5d531cbSDavid Ahern 	}
3602557c44beSDavid Ahern 
36032ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
36042ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
36052ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
36062ea2352eSWei Wang 		goto out;
36072ea2352eSWei Wang 	}
36082ea2352eSWei Wang 
3609e8478e80SDavid Ahern 	if (cfg->fc_type > RTN_MAX) {
3610e8478e80SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid route type");
3611e8478e80SDavid Ahern 		goto out;
3612e8478e80SDavid Ahern 	}
3613e8478e80SDavid Ahern 
3614d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
3615d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
36168c5b83f0SRoopa Prabhu 		goto out;
3617d5d531cbSDavid Ahern 	}
3618d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
3619d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
3620d5d531cbSDavid Ahern 		goto out;
3621d5d531cbSDavid Ahern 	}
36221da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
3623d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
3624d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
3625d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
36268c5b83f0SRoopa Prabhu 		goto out;
3627d5d531cbSDavid Ahern 	}
36281da177e4SLinus Torvalds #endif
36295b98324eSDavid Ahern 	if (cfg->fc_nh_id) {
36305b98324eSDavid Ahern 		nh = nexthop_find_by_id(net, cfg->fc_nh_id);
36315b98324eSDavid Ahern 		if (!nh) {
36325b98324eSDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
36335b98324eSDavid Ahern 			goto out;
36345b98324eSDavid Ahern 		}
36355b98324eSDavid Ahern 		err = fib6_check_nexthop(nh, cfg, extack);
36365b98324eSDavid Ahern 		if (err)
36375b98324eSDavid Ahern 			goto out;
36385b98324eSDavid Ahern 	}
3639fc1e64e1SDavid Ahern 
3640c71099acSThomas Graf 	err = -ENOBUFS;
364138308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
3642d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3643d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
364438308473SDavid S. Miller 		if (!table) {
3645f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3646d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
3647d71314b4SMatti Vaittinen 		}
3648d71314b4SMatti Vaittinen 	} else {
3649d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
3650d71314b4SMatti Vaittinen 	}
365138308473SDavid S. Miller 
365238308473SDavid S. Miller 	if (!table)
3653c71099acSThomas Graf 		goto out;
3654c71099acSThomas Graf 
36551da177e4SLinus Torvalds 	err = -ENOMEM;
3656f88d8ea6SDavid Ahern 	rt = fib6_info_alloc(gfp_flags, !nh);
365793531c67SDavid Ahern 	if (!rt)
36581da177e4SLinus Torvalds 		goto out;
365993531c67SDavid Ahern 
3660d7e774f3SDavid Ahern 	rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3661d7e774f3SDavid Ahern 					       extack);
3662767a2217SDavid Ahern 	if (IS_ERR(rt->fib6_metrics)) {
3663767a2217SDavid Ahern 		err = PTR_ERR(rt->fib6_metrics);
3664fda21d46SEric Dumazet 		/* Do not leave garbage there. */
3665fda21d46SEric Dumazet 		rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3666767a2217SDavid Ahern 		goto out;
3667767a2217SDavid Ahern 	}
3668767a2217SDavid Ahern 
366993531c67SDavid Ahern 	if (cfg->fc_flags & RTF_ADDRCONF)
367093531c67SDavid Ahern 		rt->dst_nocount = true;
36711da177e4SLinus Torvalds 
36721716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
367314895687SDavid Ahern 		fib6_set_expires(rt, jiffies +
36741716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
36751716a961SGao feng 	else
367614895687SDavid Ahern 		fib6_clean_expires(rt);
36771da177e4SLinus Torvalds 
367886872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
367986872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
368093c2fb25SDavid Ahern 	rt->fib6_protocol = cfg->fc_protocol;
368186872cb5SThomas Graf 
368283c44251SDavid Ahern 	rt->fib6_table = table;
368383c44251SDavid Ahern 	rt->fib6_metric = cfg->fc_metric;
3684c7036d97SDavid Ahern 	rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
36852b2450caSDavid Ahern 	rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
368619e42e45SRoopa Prabhu 
368793c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
368893c2fb25SDavid Ahern 	rt->fib6_dst.plen = cfg->fc_dst_len;
36891da177e4SLinus Torvalds 
36901da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
369193c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
369293c2fb25SDavid Ahern 	rt->fib6_src.plen = cfg->fc_src_len;
36931da177e4SLinus Torvalds #endif
3694f88d8ea6SDavid Ahern 	if (nh) {
3695f88d8ea6SDavid Ahern 		if (rt->fib6_src.plen) {
36964daa95afSColin Ian King 			NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
3697f88d8ea6SDavid Ahern 			goto out;
3698f88d8ea6SDavid Ahern 		}
3699706ec919SXiyu Yang 		if (!nexthop_get(nh)) {
3700706ec919SXiyu Yang 			NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3701706ec919SXiyu Yang 			goto out;
3702706ec919SXiyu Yang 		}
3703f88d8ea6SDavid Ahern 		rt->nh = nh;
3704f88d8ea6SDavid Ahern 		fib6_nh = nexthop_fib6_nh(rt->nh);
3705f88d8ea6SDavid Ahern 	} else {
37061cf844c7SDavid Ahern 		err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
37071da177e4SLinus Torvalds 		if (err)
37081da177e4SLinus Torvalds 			goto out;
37099fbb704cSDavid Ahern 
3710f88d8ea6SDavid Ahern 		fib6_nh = rt->fib6_nh;
3711f88d8ea6SDavid Ahern 
3712f88d8ea6SDavid Ahern 		/* We cannot add true routes via loopback here, they would
3713f88d8ea6SDavid Ahern 		 * result in kernel looping; promote them to reject routes
371483c44251SDavid Ahern 		 */
371583c44251SDavid Ahern 		addr_type = ipv6_addr_type(&cfg->fc_dst);
3716f88d8ea6SDavid Ahern 		if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3717f88d8ea6SDavid Ahern 				   addr_type))
371883c44251SDavid Ahern 			rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3719f88d8ea6SDavid Ahern 	}
3720955ec4cbSDavid Ahern 
3721c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3722f88d8ea6SDavid Ahern 		struct net_device *dev = fib6_nh->fib_nh_dev;
372383c44251SDavid Ahern 
3724c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3725d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
3726c3968a85SDaniel Walter 			err = -EINVAL;
3727c3968a85SDaniel Walter 			goto out;
3728c3968a85SDaniel Walter 		}
372993c2fb25SDavid Ahern 		rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
373093c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 128;
3731c3968a85SDaniel Walter 	} else
373293c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
3733c3968a85SDaniel Walter 
37348c5b83f0SRoopa Prabhu 	return rt;
37351da177e4SLinus Torvalds out:
373693531c67SDavid Ahern 	fib6_info_release(rt);
37378c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
37386b9ea5a6SRoopa Prabhu }
37396b9ea5a6SRoopa Prabhu 
3740acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3741333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
37426b9ea5a6SRoopa Prabhu {
37438d1c802bSDavid Ahern 	struct fib6_info *rt;
37446b9ea5a6SRoopa Prabhu 	int err;
37456b9ea5a6SRoopa Prabhu 
3746acb54e3cSDavid Ahern 	rt = ip6_route_info_create(cfg, gfp_flags, extack);
3747d4ead6b3SDavid Ahern 	if (IS_ERR(rt))
3748d4ead6b3SDavid Ahern 		return PTR_ERR(rt);
37496b9ea5a6SRoopa Prabhu 
3750d4ead6b3SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
375193531c67SDavid Ahern 	fib6_info_release(rt);
37526b9ea5a6SRoopa Prabhu 
37531da177e4SLinus Torvalds 	return err;
37541da177e4SLinus Torvalds }
37551da177e4SLinus Torvalds 
37568d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
37571da177e4SLinus Torvalds {
3758afb1d4b5SDavid Ahern 	struct net *net = info->nl_net;
3759c71099acSThomas Graf 	struct fib6_table *table;
3760afb1d4b5SDavid Ahern 	int err;
37611da177e4SLinus Torvalds 
3762421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
37636825a26cSGao feng 		err = -ENOENT;
37646825a26cSGao feng 		goto out;
37656825a26cSGao feng 	}
37666c813a72SPatrick McHardy 
376793c2fb25SDavid Ahern 	table = rt->fib6_table;
376866f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
376986872cb5SThomas Graf 	err = fib6_del(rt, info);
377066f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
37711da177e4SLinus Torvalds 
37726825a26cSGao feng out:
377393531c67SDavid Ahern 	fib6_info_release(rt);
37741da177e4SLinus Torvalds 	return err;
37751da177e4SLinus Torvalds }
37761da177e4SLinus Torvalds 
377711dd74b3SRoopa Prabhu int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
3778e0a1ad73SThomas Graf {
377911dd74b3SRoopa Prabhu 	struct nl_info info = {
378011dd74b3SRoopa Prabhu 		.nl_net = net,
378111dd74b3SRoopa Prabhu 		.skip_notify = skip_notify
378211dd74b3SRoopa Prabhu 	};
3783afb1d4b5SDavid Ahern 
3784528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
3785e0a1ad73SThomas Graf }
3786e0a1ad73SThomas Graf 
37878d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
37880ae81335SDavid Ahern {
37890ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
3790e3330039SWANG Cong 	struct net *net = info->nl_net;
379116a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
37920ae81335SDavid Ahern 	struct fib6_table *table;
3793e3330039SWANG Cong 	int err = -ENOENT;
37940ae81335SDavid Ahern 
3795421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
3796e3330039SWANG Cong 		goto out_put;
379793c2fb25SDavid Ahern 	table = rt->fib6_table;
379866f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
37990ae81335SDavid Ahern 
380093c2fb25SDavid Ahern 	if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
38018d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
38020284696bSIdo Schimmel 		struct fib6_node *fn;
38030ae81335SDavid Ahern 
380416a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
380516a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
380616a16cd3SDavid Ahern 		if (skb) {
380716a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
380816a16cd3SDavid Ahern 
3809d4ead6b3SDavid Ahern 			if (rt6_fill_node(net, skb, rt, NULL,
381016a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
381116a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
381216a16cd3SDavid Ahern 				kfree_skb(skb);
381316a16cd3SDavid Ahern 				skb = NULL;
381416a16cd3SDavid Ahern 			} else
381516a16cd3SDavid Ahern 				info->skip_notify = 1;
381616a16cd3SDavid Ahern 		}
381716a16cd3SDavid Ahern 
38180284696bSIdo Schimmel 		/* 'rt' points to the first sibling route. If it is not the
38190284696bSIdo Schimmel 		 * leaf, then we do not need to send a notification. Otherwise,
38200284696bSIdo Schimmel 		 * we need to check if the last sibling has a next route or not
38210284696bSIdo Schimmel 		 * and emit a replace or delete notification, respectively.
38220284696bSIdo Schimmel 		 */
38232881fd61SIdo Schimmel 		info->skip_notify_kernel = 1;
38240284696bSIdo Schimmel 		fn = rcu_dereference_protected(rt->fib6_node,
38250284696bSIdo Schimmel 					    lockdep_is_held(&table->tb6_lock));
38260284696bSIdo Schimmel 		if (rcu_access_pointer(fn->leaf) == rt) {
38270284696bSIdo Schimmel 			struct fib6_info *last_sibling, *replace_rt;
38280284696bSIdo Schimmel 
38290284696bSIdo Schimmel 			last_sibling = list_last_entry(&rt->fib6_siblings,
38300284696bSIdo Schimmel 						       struct fib6_info,
38310284696bSIdo Schimmel 						       fib6_siblings);
38320284696bSIdo Schimmel 			replace_rt = rcu_dereference_protected(
38330284696bSIdo Schimmel 					    last_sibling->fib6_next,
38340284696bSIdo Schimmel 					    lockdep_is_held(&table->tb6_lock));
38350284696bSIdo Schimmel 			if (replace_rt)
38360284696bSIdo Schimmel 				call_fib6_entry_notifiers_replace(net,
38370284696bSIdo Schimmel 								  replace_rt);
38380284696bSIdo Schimmel 			else
38390284696bSIdo Schimmel 				call_fib6_multipath_entry_notifiers(net,
3840caafb250SIdo Schimmel 						       FIB_EVENT_ENTRY_DEL,
38410284696bSIdo Schimmel 						       rt, rt->fib6_nsiblings,
38420284696bSIdo Schimmel 						       NULL);
38430284696bSIdo Schimmel 		}
38440ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
384593c2fb25SDavid Ahern 					 &rt->fib6_siblings,
384693c2fb25SDavid Ahern 					 fib6_siblings) {
38470ae81335SDavid Ahern 			err = fib6_del(sibling, info);
38480ae81335SDavid Ahern 			if (err)
3849e3330039SWANG Cong 				goto out_unlock;
38500ae81335SDavid Ahern 		}
38510ae81335SDavid Ahern 	}
38520ae81335SDavid Ahern 
38530ae81335SDavid Ahern 	err = fib6_del(rt, info);
3854e3330039SWANG Cong out_unlock:
385566f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
3856e3330039SWANG Cong out_put:
385793531c67SDavid Ahern 	fib6_info_release(rt);
385816a16cd3SDavid Ahern 
385916a16cd3SDavid Ahern 	if (skb) {
3860e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
386116a16cd3SDavid Ahern 			    info->nlh, gfp_any());
386216a16cd3SDavid Ahern 	}
38630ae81335SDavid Ahern 	return err;
38640ae81335SDavid Ahern }
38650ae81335SDavid Ahern 
38660fa6efc5SDavid Ahern static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
386723fb93a4SDavid Ahern {
386823fb93a4SDavid Ahern 	int rc = -ESRCH;
386923fb93a4SDavid Ahern 
387023fb93a4SDavid Ahern 	if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
387123fb93a4SDavid Ahern 		goto out;
387223fb93a4SDavid Ahern 
387323fb93a4SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY &&
387423fb93a4SDavid Ahern 	    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
387523fb93a4SDavid Ahern 		goto out;
3876761f6026SXin Long 
387723fb93a4SDavid Ahern 	rc = rt6_remove_exception_rt(rt);
387823fb93a4SDavid Ahern out:
387923fb93a4SDavid Ahern 	return rc;
388023fb93a4SDavid Ahern }
388123fb93a4SDavid Ahern 
38820fa6efc5SDavid Ahern static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
38830fa6efc5SDavid Ahern 			     struct fib6_nh *nh)
38840fa6efc5SDavid Ahern {
38850fa6efc5SDavid Ahern 	struct fib6_result res = {
38860fa6efc5SDavid Ahern 		.f6i = rt,
38870fa6efc5SDavid Ahern 		.nh = nh,
38880fa6efc5SDavid Ahern 	};
38890fa6efc5SDavid Ahern 	struct rt6_info *rt_cache;
38900fa6efc5SDavid Ahern 
38910fa6efc5SDavid Ahern 	rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
38920fa6efc5SDavid Ahern 	if (rt_cache)
38930fa6efc5SDavid Ahern 		return __ip6_del_cached_rt(rt_cache, cfg);
38940fa6efc5SDavid Ahern 
38950fa6efc5SDavid Ahern 	return 0;
38960fa6efc5SDavid Ahern }
38970fa6efc5SDavid Ahern 
38985b98324eSDavid Ahern struct fib6_nh_del_cached_rt_arg {
38995b98324eSDavid Ahern 	struct fib6_config *cfg;
39005b98324eSDavid Ahern 	struct fib6_info *f6i;
39015b98324eSDavid Ahern };
39025b98324eSDavid Ahern 
39035b98324eSDavid Ahern static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
39045b98324eSDavid Ahern {
39055b98324eSDavid Ahern 	struct fib6_nh_del_cached_rt_arg *arg = _arg;
39065b98324eSDavid Ahern 	int rc;
39075b98324eSDavid Ahern 
39085b98324eSDavid Ahern 	rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
39095b98324eSDavid Ahern 	return rc != -ESRCH ? rc : 0;
39105b98324eSDavid Ahern }
39115b98324eSDavid Ahern 
39125b98324eSDavid Ahern static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
39135b98324eSDavid Ahern {
39145b98324eSDavid Ahern 	struct fib6_nh_del_cached_rt_arg arg = {
39155b98324eSDavid Ahern 		.cfg = cfg,
39165b98324eSDavid Ahern 		.f6i = f6i
39175b98324eSDavid Ahern 	};
39185b98324eSDavid Ahern 
39195b98324eSDavid Ahern 	return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
39205b98324eSDavid Ahern }
39215b98324eSDavid Ahern 
3922333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
3923333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
39241da177e4SLinus Torvalds {
3925c71099acSThomas Graf 	struct fib6_table *table;
39268d1c802bSDavid Ahern 	struct fib6_info *rt;
39271da177e4SLinus Torvalds 	struct fib6_node *fn;
39281da177e4SLinus Torvalds 	int err = -ESRCH;
39291da177e4SLinus Torvalds 
39305578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3931d5d531cbSDavid Ahern 	if (!table) {
3932d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
3933c71099acSThomas Graf 		return err;
3934d5d531cbSDavid Ahern 	}
39351da177e4SLinus Torvalds 
393666f5d6ceSWei Wang 	rcu_read_lock();
3937c71099acSThomas Graf 
3938c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
393986872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
394038fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
39412b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
39421da177e4SLinus Torvalds 
39431da177e4SLinus Torvalds 	if (fn) {
394466f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
3945ad1601aeSDavid Ahern 			struct fib6_nh *nh;
3946ad1601aeSDavid Ahern 
39473401bfb1SStefano Brivio 			if (rt->nh && cfg->fc_nh_id &&
39483401bfb1SStefano Brivio 			    rt->nh->id != cfg->fc_nh_id)
39495b98324eSDavid Ahern 				continue;
395023fb93a4SDavid Ahern 
39515b98324eSDavid Ahern 			if (cfg->fc_flags & RTF_CACHE) {
39525b98324eSDavid Ahern 				int rc = 0;
39535b98324eSDavid Ahern 
39545b98324eSDavid Ahern 				if (rt->nh) {
39555b98324eSDavid Ahern 					rc = ip6_del_cached_rt_nh(cfg, rt);
39565b98324eSDavid Ahern 				} else if (cfg->fc_nh_id) {
39575b98324eSDavid Ahern 					continue;
39585b98324eSDavid Ahern 				} else {
39595b98324eSDavid Ahern 					nh = rt->fib6_nh;
39600fa6efc5SDavid Ahern 					rc = ip6_del_cached_rt(cfg, rt, nh);
39615b98324eSDavid Ahern 				}
39629e575010SEric Dumazet 				if (rc != -ESRCH) {
39639e575010SEric Dumazet 					rcu_read_unlock();
396423fb93a4SDavid Ahern 					return rc;
396523fb93a4SDavid Ahern 				}
39661f56a01fSMartin KaFai Lau 				continue;
39672b760fcfSWei Wang 			}
3968ad1601aeSDavid Ahern 
39695b98324eSDavid Ahern 			if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
39705b98324eSDavid Ahern 				continue;
39715b98324eSDavid Ahern 			if (cfg->fc_protocol &&
39725b98324eSDavid Ahern 			    cfg->fc_protocol != rt->fib6_protocol)
39735b98324eSDavid Ahern 				continue;
39745b98324eSDavid Ahern 
39755b98324eSDavid Ahern 			if (rt->nh) {
39765b98324eSDavid Ahern 				if (!fib6_info_hold_safe(rt))
39775b98324eSDavid Ahern 					continue;
39785b98324eSDavid Ahern 				rcu_read_unlock();
39795b98324eSDavid Ahern 
39805b98324eSDavid Ahern 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
39815b98324eSDavid Ahern 			}
39825b98324eSDavid Ahern 			if (cfg->fc_nh_id)
39835b98324eSDavid Ahern 				continue;
39845b98324eSDavid Ahern 
39855b98324eSDavid Ahern 			nh = rt->fib6_nh;
398686872cb5SThomas Graf 			if (cfg->fc_ifindex &&
3987ad1601aeSDavid Ahern 			    (!nh->fib_nh_dev ||
3988ad1601aeSDavid Ahern 			     nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
39891da177e4SLinus Torvalds 				continue;
399086872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
3991ad1601aeSDavid Ahern 			    !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
39921da177e4SLinus Torvalds 				continue;
3993e873e4b9SWei Wang 			if (!fib6_info_hold_safe(rt))
3994e873e4b9SWei Wang 				continue;
399566f5d6ceSWei Wang 			rcu_read_unlock();
39961da177e4SLinus Torvalds 
39970ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
39980ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
399986872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
40000ae81335SDavid Ahern 
40010ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
40021da177e4SLinus Torvalds 		}
40031da177e4SLinus Torvalds 	}
400466f5d6ceSWei Wang 	rcu_read_unlock();
40051da177e4SLinus Torvalds 
40061da177e4SLinus Torvalds 	return err;
40071da177e4SLinus Torvalds }
40081da177e4SLinus Torvalds 
40096700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
4010a6279458SYOSHIFUJI Hideaki {
4011a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
4012e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
401385bd05deSDavid Ahern 	struct fib6_result res = {};
4014e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
4015e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
4016e8599ff4SDavid S. Miller 	struct neighbour *neigh;
401771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
40186e157b6aSDavid S. Miller 	int optlen, on_link;
40196e157b6aSDavid S. Miller 	u8 *lladdr;
4020e8599ff4SDavid S. Miller 
402129a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
402271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
4023e8599ff4SDavid S. Miller 
4024e8599ff4SDavid S. Miller 	if (optlen < 0) {
40256e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
4026e8599ff4SDavid S. Miller 		return;
4027e8599ff4SDavid S. Miller 	}
4028e8599ff4SDavid S. Miller 
402971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
4030e8599ff4SDavid S. Miller 
403171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
40326e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
4033e8599ff4SDavid S. Miller 		return;
4034e8599ff4SDavid S. Miller 	}
4035e8599ff4SDavid S. Miller 
40366e157b6aSDavid S. Miller 	on_link = 0;
403771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
4038e8599ff4SDavid S. Miller 		on_link = 1;
403971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
4040e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
40416e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
4042e8599ff4SDavid S. Miller 		return;
4043e8599ff4SDavid S. Miller 	}
4044e8599ff4SDavid S. Miller 
4045e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
4046e8599ff4SDavid S. Miller 	if (!in6_dev)
4047e8599ff4SDavid S. Miller 		return;
4048e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4049e8599ff4SDavid S. Miller 		return;
4050e8599ff4SDavid S. Miller 
4051e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
4052e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
4053e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
4054e8599ff4SDavid S. Miller 	 */
4055e8599ff4SDavid S. Miller 
4056f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
4057e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4058e8599ff4SDavid S. Miller 		return;
4059e8599ff4SDavid S. Miller 	}
40606e157b6aSDavid S. Miller 
40616e157b6aSDavid S. Miller 	lladdr = NULL;
4062e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
4063e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4064e8599ff4SDavid S. Miller 					     skb->dev);
4065e8599ff4SDavid S. Miller 		if (!lladdr) {
4066e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4067e8599ff4SDavid S. Miller 			return;
4068e8599ff4SDavid S. Miller 		}
4069e8599ff4SDavid S. Miller 	}
4070e8599ff4SDavid S. Miller 
40716e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
4072ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
40736e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
40746e157b6aSDavid S. Miller 		return;
40756e157b6aSDavid S. Miller 	}
40766e157b6aSDavid S. Miller 
40776e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
40786e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
40796e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
40806e157b6aSDavid S. Miller 	 */
40810dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
40826e157b6aSDavid S. Miller 
408371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
4084e8599ff4SDavid S. Miller 	if (!neigh)
4085e8599ff4SDavid S. Miller 		return;
4086e8599ff4SDavid S. Miller 
40871da177e4SLinus Torvalds 	/*
40881da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
40891da177e4SLinus Torvalds 	 */
40901da177e4SLinus Torvalds 
4091f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
40921da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
40931da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
40941da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
4095f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
4096f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
40971da177e4SLinus Torvalds 
40984d85cd0cSDavid Ahern 	rcu_read_lock();
409985bd05deSDavid Ahern 	res.f6i = rcu_dereference(rt->from);
4100ff24e498SDavid S. Miller 	if (!res.f6i)
4101886b7a50SMartin KaFai Lau 		goto out;
41028a14e46fSDavid Ahern 
410349d5b8efSDavid Ahern 	if (res.f6i->nh) {
410449d5b8efSDavid Ahern 		struct fib6_nh_match_arg arg = {
410549d5b8efSDavid Ahern 			.dev = dst->dev,
410649d5b8efSDavid Ahern 			.gw = &rt->rt6i_gateway,
410749d5b8efSDavid Ahern 		};
410849d5b8efSDavid Ahern 
410949d5b8efSDavid Ahern 		nexthop_for_each_fib6_nh(res.f6i->nh,
411049d5b8efSDavid Ahern 					 fib6_nh_find_match, &arg);
411149d5b8efSDavid Ahern 
411249d5b8efSDavid Ahern 		/* fib6_info uses a nexthop that does not have fib6_nh
411349d5b8efSDavid Ahern 		 * using the dst->dev. Should be impossible
411449d5b8efSDavid Ahern 		 */
411549d5b8efSDavid Ahern 		if (!arg.match)
411649d5b8efSDavid Ahern 			goto out;
411749d5b8efSDavid Ahern 		res.nh = arg.match;
411849d5b8efSDavid Ahern 	} else {
41191cf844c7SDavid Ahern 		res.nh = res.f6i->fib6_nh;
412049d5b8efSDavid Ahern 	}
412149d5b8efSDavid Ahern 
41227d21fec9SDavid Ahern 	res.fib6_flags = res.f6i->fib6_flags;
41237d21fec9SDavid Ahern 	res.fib6_type = res.f6i->fib6_type;
412485bd05deSDavid Ahern 	nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
412538308473SDavid S. Miller 	if (!nrt)
41261da177e4SLinus Torvalds 		goto out;
41271da177e4SLinus Torvalds 
41281da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
41291da177e4SLinus Torvalds 	if (on_link)
41301da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
41311da177e4SLinus Torvalds 
41324e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
41331da177e4SLinus Torvalds 
4134886b7a50SMartin KaFai Lau 	/* rt6_insert_exception() will take care of duplicated exceptions */
41355012f0a5SDavid Ahern 	if (rt6_insert_exception(nrt, &res)) {
41362b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
41372b760fcfSWei Wang 		goto out;
41382b760fcfSWei Wang 	}
41391da177e4SLinus Torvalds 
4140d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
4141d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
414271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
414360592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
41448d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
41458d71740cSTom Tucker 
41461da177e4SLinus Torvalds out:
4147886b7a50SMartin KaFai Lau 	rcu_read_unlock();
4148e8599ff4SDavid S. Miller 	neigh_release(neigh);
41496e157b6aSDavid S. Miller }
41506e157b6aSDavid S. Miller 
415170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
41528d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
4153b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
4154830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
4155830218c1SDavid Ahern 					   struct net_device *dev)
415670ceb4f5SYOSHIFUJI Hideaki {
4157830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4158830218c1SDavid Ahern 	int ifindex = dev->ifindex;
415970ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
41608d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
4161c71099acSThomas Graf 	struct fib6_table *table;
416270ceb4f5SYOSHIFUJI Hideaki 
4163830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
416438308473SDavid S. Miller 	if (!table)
4165c71099acSThomas Graf 		return NULL;
4166c71099acSThomas Graf 
416766f5d6ceSWei Wang 	rcu_read_lock();
416838fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
416970ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
417070ceb4f5SYOSHIFUJI Hideaki 		goto out;
417170ceb4f5SYOSHIFUJI Hideaki 
417266f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
4173f88d8ea6SDavid Ahern 		/* these routes do not use nexthops */
4174f88d8ea6SDavid Ahern 		if (rt->nh)
4175f88d8ea6SDavid Ahern 			continue;
41761cf844c7SDavid Ahern 		if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
417770ceb4f5SYOSHIFUJI Hideaki 			continue;
41782b2450caSDavid Ahern 		if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
41791cf844c7SDavid Ahern 		    !rt->fib6_nh->fib_nh_gw_family)
418070ceb4f5SYOSHIFUJI Hideaki 			continue;
41811cf844c7SDavid Ahern 		if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
418270ceb4f5SYOSHIFUJI Hideaki 			continue;
4183e873e4b9SWei Wang 		if (!fib6_info_hold_safe(rt))
4184e873e4b9SWei Wang 			continue;
418570ceb4f5SYOSHIFUJI Hideaki 		break;
418670ceb4f5SYOSHIFUJI Hideaki 	}
418770ceb4f5SYOSHIFUJI Hideaki out:
418866f5d6ceSWei Wang 	rcu_read_unlock();
418970ceb4f5SYOSHIFUJI Hideaki 	return rt;
419070ceb4f5SYOSHIFUJI Hideaki }
419170ceb4f5SYOSHIFUJI Hideaki 
41928d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
4193b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
4194830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
4195830218c1SDavid Ahern 					   struct net_device *dev,
419695c96174SEric Dumazet 					   unsigned int pref)
419770ceb4f5SYOSHIFUJI Hideaki {
419886872cb5SThomas Graf 	struct fib6_config cfg = {
4199238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
4200830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
420186872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
420286872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
420386872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
4204b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
4205e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
420615e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
4207efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
4208efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
420986872cb5SThomas Graf 	};
421070ceb4f5SYOSHIFUJI Hideaki 
421191b2c9a0SXu Wang 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
42124e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
42134e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
421486872cb5SThomas Graf 
4215e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
4216e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
421786872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
421870ceb4f5SYOSHIFUJI Hideaki 
4219acb54e3cSDavid Ahern 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
422070ceb4f5SYOSHIFUJI Hideaki 
4221830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
422270ceb4f5SYOSHIFUJI Hideaki }
422370ceb4f5SYOSHIFUJI Hideaki #endif
422470ceb4f5SYOSHIFUJI Hideaki 
42258d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net,
4226afb1d4b5SDavid Ahern 				     const struct in6_addr *addr,
4227afb1d4b5SDavid Ahern 				     struct net_device *dev)
42281da177e4SLinus Torvalds {
4229830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
42308d1c802bSDavid Ahern 	struct fib6_info *rt;
4231c71099acSThomas Graf 	struct fib6_table *table;
42321da177e4SLinus Torvalds 
4233afb1d4b5SDavid Ahern 	table = fib6_get_table(net, tb_id);
423438308473SDavid S. Miller 	if (!table)
4235c71099acSThomas Graf 		return NULL;
42361da177e4SLinus Torvalds 
423766f5d6ceSWei Wang 	rcu_read_lock();
423866f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
4239f88d8ea6SDavid Ahern 		struct fib6_nh *nh;
4240ad1601aeSDavid Ahern 
4241f88d8ea6SDavid Ahern 		/* RA routes do not use nexthops */
4242f88d8ea6SDavid Ahern 		if (rt->nh)
4243f88d8ea6SDavid Ahern 			continue;
4244f88d8ea6SDavid Ahern 
4245f88d8ea6SDavid Ahern 		nh = rt->fib6_nh;
4246ad1601aeSDavid Ahern 		if (dev == nh->fib_nh_dev &&
424793c2fb25SDavid Ahern 		    ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
4248ad1601aeSDavid Ahern 		    ipv6_addr_equal(&nh->fib_nh_gw6, addr))
42491da177e4SLinus Torvalds 			break;
42501da177e4SLinus Torvalds 	}
4251e873e4b9SWei Wang 	if (rt && !fib6_info_hold_safe(rt))
4252e873e4b9SWei Wang 		rt = NULL;
425366f5d6ceSWei Wang 	rcu_read_unlock();
42541da177e4SLinus Torvalds 	return rt;
42551da177e4SLinus Torvalds }
42561da177e4SLinus Torvalds 
42578d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net,
4258afb1d4b5SDavid Ahern 				     const struct in6_addr *gwaddr,
4259ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
42606b2e04bcSPraveen Chaudhary 				     unsigned int pref,
42616b2e04bcSPraveen Chaudhary 				     u32 defrtr_usr_metric)
42621da177e4SLinus Torvalds {
426386872cb5SThomas Graf 	struct fib6_config cfg = {
4264ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
42656b2e04bcSPraveen Chaudhary 		.fc_metric	= defrtr_usr_metric,
426686872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
426786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
426886872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
4269b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
4270e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
427115e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
42725578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
4273afb1d4b5SDavid Ahern 		.fc_nlinfo.nl_net = net,
427486872cb5SThomas Graf 	};
42751da177e4SLinus Torvalds 
42764e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
42771da177e4SLinus Torvalds 
4278acb54e3cSDavid Ahern 	if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
4279830218c1SDavid Ahern 		struct fib6_table *table;
4280830218c1SDavid Ahern 
4281830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
4282830218c1SDavid Ahern 		if (table)
4283830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4284830218c1SDavid Ahern 	}
42851da177e4SLinus Torvalds 
4286afb1d4b5SDavid Ahern 	return rt6_get_dflt_router(net, gwaddr, dev);
42871da177e4SLinus Torvalds }
42881da177e4SLinus Torvalds 
4289afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net,
4290afb1d4b5SDavid Ahern 				     struct fib6_table *table)
42911da177e4SLinus Torvalds {
42928d1c802bSDavid Ahern 	struct fib6_info *rt;
42931da177e4SLinus Torvalds 
42941da177e4SLinus Torvalds restart:
429566f5d6ceSWei Wang 	rcu_read_lock();
429666f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
4297dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
4298dcd1f572SDavid Ahern 		struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4299dcd1f572SDavid Ahern 
430093c2fb25SDavid Ahern 		if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
4301e873e4b9SWei Wang 		    (!idev || idev->cnf.accept_ra != 2) &&
4302e873e4b9SWei Wang 		    fib6_info_hold_safe(rt)) {
430366f5d6ceSWei Wang 			rcu_read_unlock();
430411dd74b3SRoopa Prabhu 			ip6_del_rt(net, rt, false);
43051da177e4SLinus Torvalds 			goto restart;
43061da177e4SLinus Torvalds 		}
43071da177e4SLinus Torvalds 	}
430866f5d6ceSWei Wang 	rcu_read_unlock();
4309830218c1SDavid Ahern 
4310830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4311830218c1SDavid Ahern }
4312830218c1SDavid Ahern 
4313830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
4314830218c1SDavid Ahern {
4315830218c1SDavid Ahern 	struct fib6_table *table;
4316830218c1SDavid Ahern 	struct hlist_head *head;
4317830218c1SDavid Ahern 	unsigned int h;
4318830218c1SDavid Ahern 
4319830218c1SDavid Ahern 	rcu_read_lock();
4320830218c1SDavid Ahern 
4321830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4322830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
4323830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4324830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
4325afb1d4b5SDavid Ahern 				__rt6_purge_dflt_routers(net, table);
4326830218c1SDavid Ahern 		}
4327830218c1SDavid Ahern 	}
4328830218c1SDavid Ahern 
4329830218c1SDavid Ahern 	rcu_read_unlock();
43301da177e4SLinus Torvalds }
43311da177e4SLinus Torvalds 
43325578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
43335578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
433486872cb5SThomas Graf 				 struct fib6_config *cfg)
433586872cb5SThomas Graf {
43368823a3acSMaciej Żenczykowski 	*cfg = (struct fib6_config){
43378823a3acSMaciej Żenczykowski 		.fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
43388823a3acSMaciej Żenczykowski 			 : RT6_TABLE_MAIN,
43398823a3acSMaciej Żenczykowski 		.fc_ifindex = rtmsg->rtmsg_ifindex,
434067f69513SDavid Ahern 		.fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
43418823a3acSMaciej Żenczykowski 		.fc_expires = rtmsg->rtmsg_info,
43428823a3acSMaciej Żenczykowski 		.fc_dst_len = rtmsg->rtmsg_dst_len,
43438823a3acSMaciej Żenczykowski 		.fc_src_len = rtmsg->rtmsg_src_len,
43448823a3acSMaciej Żenczykowski 		.fc_flags = rtmsg->rtmsg_flags,
43458823a3acSMaciej Żenczykowski 		.fc_type = rtmsg->rtmsg_type,
434686872cb5SThomas Graf 
43478823a3acSMaciej Żenczykowski 		.fc_nlinfo.nl_net = net,
434886872cb5SThomas Graf 
43498823a3acSMaciej Żenczykowski 		.fc_dst = rtmsg->rtmsg_dst,
43508823a3acSMaciej Żenczykowski 		.fc_src = rtmsg->rtmsg_src,
43518823a3acSMaciej Żenczykowski 		.fc_gateway = rtmsg->rtmsg_gateway,
43528823a3acSMaciej Żenczykowski 	};
435386872cb5SThomas Graf }
435486872cb5SThomas Graf 
43557c1552daSChristoph Hellwig int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
43561da177e4SLinus Torvalds {
435786872cb5SThomas Graf 	struct fib6_config cfg;
43581da177e4SLinus Torvalds 	int err;
43591da177e4SLinus Torvalds 
43607c1552daSChristoph Hellwig 	if (cmd != SIOCADDRT && cmd != SIOCDELRT)
43617c1552daSChristoph Hellwig 		return -EINVAL;
4362af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
43631da177e4SLinus Torvalds 		return -EPERM;
43641da177e4SLinus Torvalds 
43657c1552daSChristoph Hellwig 	rtmsg_to_fib6_config(net, rtmsg, &cfg);
436686872cb5SThomas Graf 
43671da177e4SLinus Torvalds 	rtnl_lock();
43681da177e4SLinus Torvalds 	switch (cmd) {
43691da177e4SLinus Torvalds 	case SIOCADDRT:
4370acb54e3cSDavid Ahern 		err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
43711da177e4SLinus Torvalds 		break;
43721da177e4SLinus Torvalds 	case SIOCDELRT:
4373333c4301SDavid Ahern 		err = ip6_route_del(&cfg, NULL);
43741da177e4SLinus Torvalds 		break;
43751da177e4SLinus Torvalds 	}
43761da177e4SLinus Torvalds 	rtnl_unlock();
43771da177e4SLinus Torvalds 	return err;
43783ff50b79SStephen Hemminger }
43791da177e4SLinus Torvalds 
43801da177e4SLinus Torvalds /*
43811da177e4SLinus Torvalds  *	Drop the packet on the floor
43821da177e4SLinus Torvalds  */
43831da177e4SLinus Torvalds 
4384d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
43851da177e4SLinus Torvalds {
4386adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
43871d3fd8a1SStephen Suryaputra 	struct net *net = dev_net(dst->dev);
43881d3fd8a1SStephen Suryaputra 	struct inet6_dev *idev;
43891d3fd8a1SStephen Suryaputra 	int type;
43901d3fd8a1SStephen Suryaputra 
43911d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev) &&
43921d3fd8a1SStephen Suryaputra 	    dst->dev == net->loopback_dev)
43931d3fd8a1SStephen Suryaputra 		idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
43941d3fd8a1SStephen Suryaputra 	else
43951d3fd8a1SStephen Suryaputra 		idev = ip6_dst_idev(dst);
43961d3fd8a1SStephen Suryaputra 
4397612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
4398612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
43990660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
440045bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
44011d3fd8a1SStephen Suryaputra 			IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
4402612f09e8SYOSHIFUJI Hideaki 			break;
4403612f09e8SYOSHIFUJI Hideaki 		}
4404a8eceea8SJoe Perches 		fallthrough;
4405612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
44061d3fd8a1SStephen Suryaputra 		IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
4407612f09e8SYOSHIFUJI Hideaki 		break;
4408612f09e8SYOSHIFUJI Hideaki 	}
44091d3fd8a1SStephen Suryaputra 
44101d3fd8a1SStephen Suryaputra 	/* Start over by dropping the dst for l3mdev case */
44111d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev))
44121d3fd8a1SStephen Suryaputra 		skb_dst_drop(skb);
44131d3fd8a1SStephen Suryaputra 
44143ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
44151da177e4SLinus Torvalds 	kfree_skb(skb);
44161da177e4SLinus Torvalds 	return 0;
44171da177e4SLinus Torvalds }
44181da177e4SLinus Torvalds 
44199ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
44209ce8ade0SThomas Graf {
4421612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
44229ce8ade0SThomas Graf }
44239ce8ade0SThomas Graf 
4424ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
44251da177e4SLinus Torvalds {
4426adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
4427612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
44281da177e4SLinus Torvalds }
44291da177e4SLinus Torvalds 
44309ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
44319ce8ade0SThomas Graf {
4432612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
44339ce8ade0SThomas Graf }
44349ce8ade0SThomas Graf 
4435ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
44369ce8ade0SThomas Graf {
4437adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
4438612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
44399ce8ade0SThomas Graf }
44409ce8ade0SThomas Graf 
44411da177e4SLinus Torvalds /*
44421da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
44431da177e4SLinus Torvalds  */
44441da177e4SLinus Torvalds 
4445360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net,
4446afb1d4b5SDavid Ahern 				     struct inet6_dev *idev,
44471da177e4SLinus Torvalds 				     const struct in6_addr *addr,
4448acb54e3cSDavid Ahern 				     bool anycast, gfp_t gfp_flags)
44491da177e4SLinus Torvalds {
4450c7a1ce39SDavid Ahern 	struct fib6_config cfg = {
4451c7a1ce39SDavid Ahern 		.fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4452c7a1ce39SDavid Ahern 		.fc_ifindex = idev->dev->ifindex,
4453d55a2e37SMaciej Żenczykowski 		.fc_flags = RTF_UP | RTF_NONEXTHOP,
4454c7a1ce39SDavid Ahern 		.fc_dst = *addr,
4455c7a1ce39SDavid Ahern 		.fc_dst_len = 128,
4456c7a1ce39SDavid Ahern 		.fc_protocol = RTPROT_KERNEL,
4457c7a1ce39SDavid Ahern 		.fc_nlinfo.nl_net = net,
4458c7a1ce39SDavid Ahern 		.fc_ignore_dev_down = true,
4459c7a1ce39SDavid Ahern 	};
4460d55a2e37SMaciej Żenczykowski 	struct fib6_info *f6i;
44615f02ce24SDavid Ahern 
4462e8478e80SDavid Ahern 	if (anycast) {
4463c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_ANYCAST;
4464c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_ANYCAST;
4465e8478e80SDavid Ahern 	} else {
4466c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_LOCAL;
4467c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_LOCAL;
4468e8478e80SDavid Ahern 	}
44691da177e4SLinus Torvalds 
4470d55a2e37SMaciej Żenczykowski 	f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
44718652f17cSMaciej Żenczykowski 	if (!IS_ERR(f6i))
4472d55a2e37SMaciej Żenczykowski 		f6i->dst_nocount = true;
4473d55a2e37SMaciej Żenczykowski 	return f6i;
44741da177e4SLinus Torvalds }
44751da177e4SLinus Torvalds 
4476c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
4477c3968a85SDaniel Walter struct arg_dev_net_ip {
4478c3968a85SDaniel Walter 	struct net_device *dev;
4479c3968a85SDaniel Walter 	struct net *net;
4480c3968a85SDaniel Walter 	struct in6_addr *addr;
4481c3968a85SDaniel Walter };
4482c3968a85SDaniel Walter 
44838d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
4484c3968a85SDaniel Walter {
4485c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4486c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4487c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4488c3968a85SDaniel Walter 
4489f88d8ea6SDavid Ahern 	if (!rt->nh &&
4490f88d8ea6SDavid Ahern 	    ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
4491421842edSDavid Ahern 	    rt != net->ipv6.fib6_null_entry &&
449293c2fb25SDavid Ahern 	    ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
449360006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
4494c3968a85SDaniel Walter 		/* remove prefsrc entry */
449593c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
449660006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
4497c3968a85SDaniel Walter 	}
4498c3968a85SDaniel Walter 	return 0;
4499c3968a85SDaniel Walter }
4500c3968a85SDaniel Walter 
4501c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4502c3968a85SDaniel Walter {
4503c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
4504c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
4505c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
4506c3968a85SDaniel Walter 		.net = net,
4507c3968a85SDaniel Walter 		.addr = &ifp->addr,
4508c3968a85SDaniel Walter 	};
45090c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4510c3968a85SDaniel Walter }
4511c3968a85SDaniel Walter 
45122b2450caSDavid Ahern #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT)
4513be7a010dSDuan Jiong 
4514be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
45158d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4516be7a010dSDuan Jiong {
4517be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
4518f88d8ea6SDavid Ahern 	struct fib6_nh *nh;
4519be7a010dSDuan Jiong 
4520f88d8ea6SDavid Ahern 	/* RA routes do not use nexthops */
4521f88d8ea6SDavid Ahern 	if (rt->nh)
4522f88d8ea6SDavid Ahern 		return 0;
4523f88d8ea6SDavid Ahern 
4524f88d8ea6SDavid Ahern 	nh = rt->fib6_nh;
452593c2fb25SDavid Ahern 	if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
4526cc5c073aSDavid Ahern 	    nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
4527be7a010dSDuan Jiong 		return -1;
4528b16cb459SWei Wang 
4529b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
4530b16cb459SWei Wang 	 * This is needed because cached route may have a different
4531b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
4532b16cb459SWei Wang 	 */
4533cc5c073aSDavid Ahern 	fib6_nh_exceptions_clean_tohost(nh, gateway);
4534b16cb459SWei Wang 
4535be7a010dSDuan Jiong 	return 0;
4536be7a010dSDuan Jiong }
4537be7a010dSDuan Jiong 
4538be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4539be7a010dSDuan Jiong {
4540be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
4541be7a010dSDuan Jiong }
4542be7a010dSDuan Jiong 
45432127d95aSIdo Schimmel struct arg_netdev_event {
45442127d95aSIdo Schimmel 	const struct net_device *dev;
45454c981e28SIdo Schimmel 	union {
4546ecc5663cSDavid Ahern 		unsigned char nh_flags;
45474c981e28SIdo Schimmel 		unsigned long event;
45484c981e28SIdo Schimmel 	};
45492127d95aSIdo Schimmel };
45502127d95aSIdo Schimmel 
45518d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4552d7dedee1SIdo Schimmel {
45538d1c802bSDavid Ahern 	struct fib6_info *iter;
4554d7dedee1SIdo Schimmel 	struct fib6_node *fn;
4555d7dedee1SIdo Schimmel 
455693c2fb25SDavid Ahern 	fn = rcu_dereference_protected(rt->fib6_node,
455793c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
4558d7dedee1SIdo Schimmel 	iter = rcu_dereference_protected(fn->leaf,
455993c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
4560d7dedee1SIdo Schimmel 	while (iter) {
456193c2fb25SDavid Ahern 		if (iter->fib6_metric == rt->fib6_metric &&
456233bd5ac5SDavid Ahern 		    rt6_qualify_for_ecmp(iter))
4563d7dedee1SIdo Schimmel 			return iter;
45648fb11a9aSDavid Ahern 		iter = rcu_dereference_protected(iter->fib6_next,
456593c2fb25SDavid Ahern 				lockdep_is_held(&rt->fib6_table->tb6_lock));
4566d7dedee1SIdo Schimmel 	}
4567d7dedee1SIdo Schimmel 
4568d7dedee1SIdo Schimmel 	return NULL;
4569d7dedee1SIdo Schimmel }
4570d7dedee1SIdo Schimmel 
4571f88d8ea6SDavid Ahern /* only called for fib entries with builtin fib6_nh */
45728d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt)
4573d7dedee1SIdo Schimmel {
45741cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
45751cf844c7SDavid Ahern 	    (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
45761cf844c7SDavid Ahern 	     ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
4577d7dedee1SIdo Schimmel 		return true;
4578d7dedee1SIdo Schimmel 
4579d7dedee1SIdo Schimmel 	return false;
4580d7dedee1SIdo Schimmel }
4581d7dedee1SIdo Schimmel 
45828d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt)
4583d7dedee1SIdo Schimmel {
45848d1c802bSDavid Ahern 	struct fib6_info *iter;
4585d7dedee1SIdo Schimmel 	int total = 0;
4586d7dedee1SIdo Schimmel 
4587d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt))
45881cf844c7SDavid Ahern 		total += rt->fib6_nh->fib_nh_weight;
4589d7dedee1SIdo Schimmel 
459093c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4591d7dedee1SIdo Schimmel 		if (!rt6_is_dead(iter))
45921cf844c7SDavid Ahern 			total += iter->fib6_nh->fib_nh_weight;
4593d7dedee1SIdo Schimmel 	}
4594d7dedee1SIdo Schimmel 
4595d7dedee1SIdo Schimmel 	return total;
4596d7dedee1SIdo Schimmel }
4597d7dedee1SIdo Schimmel 
45988d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4599d7dedee1SIdo Schimmel {
4600d7dedee1SIdo Schimmel 	int upper_bound = -1;
4601d7dedee1SIdo Schimmel 
4602d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt)) {
46031cf844c7SDavid Ahern 		*weight += rt->fib6_nh->fib_nh_weight;
4604d7dedee1SIdo Schimmel 		upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4605d7dedee1SIdo Schimmel 						    total) - 1;
4606d7dedee1SIdo Schimmel 	}
46071cf844c7SDavid Ahern 	atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
4608d7dedee1SIdo Schimmel }
4609d7dedee1SIdo Schimmel 
46108d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4611d7dedee1SIdo Schimmel {
46128d1c802bSDavid Ahern 	struct fib6_info *iter;
4613d7dedee1SIdo Schimmel 	int weight = 0;
4614d7dedee1SIdo Schimmel 
4615d7dedee1SIdo Schimmel 	rt6_upper_bound_set(rt, &weight, total);
4616d7dedee1SIdo Schimmel 
461793c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4618d7dedee1SIdo Schimmel 		rt6_upper_bound_set(iter, &weight, total);
4619d7dedee1SIdo Schimmel }
4620d7dedee1SIdo Schimmel 
46218d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt)
4622d7dedee1SIdo Schimmel {
46238d1c802bSDavid Ahern 	struct fib6_info *first;
4624d7dedee1SIdo Schimmel 	int total;
4625d7dedee1SIdo Schimmel 
4626d7dedee1SIdo Schimmel 	/* In case the entire multipath route was marked for flushing,
4627d7dedee1SIdo Schimmel 	 * then there is no need to rebalance upon the removal of every
4628d7dedee1SIdo Schimmel 	 * sibling route.
4629d7dedee1SIdo Schimmel 	 */
463093c2fb25SDavid Ahern 	if (!rt->fib6_nsiblings || rt->should_flush)
4631d7dedee1SIdo Schimmel 		return;
4632d7dedee1SIdo Schimmel 
4633d7dedee1SIdo Schimmel 	/* During lookup routes are evaluated in order, so we need to
4634d7dedee1SIdo Schimmel 	 * make sure upper bounds are assigned from the first sibling
4635d7dedee1SIdo Schimmel 	 * onwards.
4636d7dedee1SIdo Schimmel 	 */
4637d7dedee1SIdo Schimmel 	first = rt6_multipath_first_sibling(rt);
4638d7dedee1SIdo Schimmel 	if (WARN_ON_ONCE(!first))
4639d7dedee1SIdo Schimmel 		return;
4640d7dedee1SIdo Schimmel 
4641d7dedee1SIdo Schimmel 	total = rt6_multipath_total_weight(first);
4642d7dedee1SIdo Schimmel 	rt6_multipath_upper_bound_set(first, total);
4643d7dedee1SIdo Schimmel }
4644d7dedee1SIdo Schimmel 
46458d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg)
46462127d95aSIdo Schimmel {
46472127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
46487aef6859SDavid Ahern 	struct net *net = dev_net(arg->dev);
46492127d95aSIdo Schimmel 
4650f88d8ea6SDavid Ahern 	if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
46511cf844c7SDavid Ahern 	    rt->fib6_nh->fib_nh_dev == arg->dev) {
46521cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
46537aef6859SDavid Ahern 		fib6_update_sernum_upto_root(net, rt);
4654d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
46551de178edSIdo Schimmel 	}
46562127d95aSIdo Schimmel 
46572127d95aSIdo Schimmel 	return 0;
46582127d95aSIdo Schimmel }
46592127d95aSIdo Schimmel 
4660ecc5663cSDavid Ahern void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
46612127d95aSIdo Schimmel {
46622127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
46632127d95aSIdo Schimmel 		.dev = dev,
46646802f3adSIdo Schimmel 		{
46652127d95aSIdo Schimmel 			.nh_flags = nh_flags,
46666802f3adSIdo Schimmel 		},
46672127d95aSIdo Schimmel 	};
46682127d95aSIdo Schimmel 
46692127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
46702127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
46712127d95aSIdo Schimmel 
46722127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
46732127d95aSIdo Schimmel }
46742127d95aSIdo Schimmel 
4675f88d8ea6SDavid Ahern /* only called for fib entries with inline fib6_nh */
46768d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
46771de178edSIdo Schimmel 				   const struct net_device *dev)
46781de178edSIdo Schimmel {
46798d1c802bSDavid Ahern 	struct fib6_info *iter;
46801de178edSIdo Schimmel 
46811cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == dev)
46821de178edSIdo Schimmel 		return true;
468393c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
46841cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == dev)
46851de178edSIdo Schimmel 			return true;
46861de178edSIdo Schimmel 
46871de178edSIdo Schimmel 	return false;
46881de178edSIdo Schimmel }
46891de178edSIdo Schimmel 
46908d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt)
46911de178edSIdo Schimmel {
46928d1c802bSDavid Ahern 	struct fib6_info *iter;
46931de178edSIdo Schimmel 
46941de178edSIdo Schimmel 	rt->should_flush = 1;
469593c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
46961de178edSIdo Schimmel 		iter->should_flush = 1;
46971de178edSIdo Schimmel }
46981de178edSIdo Schimmel 
46998d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
47001de178edSIdo Schimmel 					     const struct net_device *down_dev)
47011de178edSIdo Schimmel {
47028d1c802bSDavid Ahern 	struct fib6_info *iter;
47031de178edSIdo Schimmel 	unsigned int dead = 0;
47041de178edSIdo Schimmel 
47051cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == down_dev ||
47061cf844c7SDavid Ahern 	    rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
47071de178edSIdo Schimmel 		dead++;
470893c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
47091cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == down_dev ||
47101cf844c7SDavid Ahern 		    iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
47111de178edSIdo Schimmel 			dead++;
47121de178edSIdo Schimmel 
47131de178edSIdo Schimmel 	return dead;
47141de178edSIdo Schimmel }
47151de178edSIdo Schimmel 
47168d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
47171de178edSIdo Schimmel 				       const struct net_device *dev,
4718ecc5663cSDavid Ahern 				       unsigned char nh_flags)
47191de178edSIdo Schimmel {
47208d1c802bSDavid Ahern 	struct fib6_info *iter;
47211de178edSIdo Schimmel 
47221cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == dev)
47231cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags |= nh_flags;
472493c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
47251cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == dev)
47261cf844c7SDavid Ahern 			iter->fib6_nh->fib_nh_flags |= nh_flags;
47271de178edSIdo Schimmel }
47281de178edSIdo Schimmel 
4729a1a22c12SDavid Ahern /* called with write lock held for table with rt */
47308d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
47311da177e4SLinus Torvalds {
47324c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
47334c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
47347aef6859SDavid Ahern 	struct net *net = dev_net(dev);
47358ed67789SDaniel Lezcano 
4736f88d8ea6SDavid Ahern 	if (rt == net->ipv6.fib6_null_entry || rt->nh)
473727c6fa73SIdo Schimmel 		return 0;
473827c6fa73SIdo Schimmel 
473927c6fa73SIdo Schimmel 	switch (arg->event) {
474027c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
47411cf844c7SDavid Ahern 		return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
474227c6fa73SIdo Schimmel 	case NETDEV_DOWN:
47431de178edSIdo Schimmel 		if (rt->should_flush)
474427c6fa73SIdo Schimmel 			return -1;
474593c2fb25SDavid Ahern 		if (!rt->fib6_nsiblings)
47461cf844c7SDavid Ahern 			return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
47471de178edSIdo Schimmel 		if (rt6_multipath_uses_dev(rt, dev)) {
47481de178edSIdo Schimmel 			unsigned int count;
47491de178edSIdo Schimmel 
47501de178edSIdo Schimmel 			count = rt6_multipath_dead_count(rt, dev);
475193c2fb25SDavid Ahern 			if (rt->fib6_nsiblings + 1 == count) {
47521de178edSIdo Schimmel 				rt6_multipath_flush(rt);
47531de178edSIdo Schimmel 				return -1;
47541de178edSIdo Schimmel 			}
47551de178edSIdo Schimmel 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
47561de178edSIdo Schimmel 						   RTNH_F_LINKDOWN);
47577aef6859SDavid Ahern 			fib6_update_sernum(net, rt);
4758d7dedee1SIdo Schimmel 			rt6_multipath_rebalance(rt);
47591de178edSIdo Schimmel 		}
47601de178edSIdo Schimmel 		return -2;
476127c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
47621cf844c7SDavid Ahern 		if (rt->fib6_nh->fib_nh_dev != dev ||
476393c2fb25SDavid Ahern 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
476427c6fa73SIdo Schimmel 			break;
47651cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
4766d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
476727c6fa73SIdo Schimmel 		break;
47682b241361SIdo Schimmel 	}
4769c159d30cSDavid S. Miller 
47701da177e4SLinus Torvalds 	return 0;
47711da177e4SLinus Torvalds }
47721da177e4SLinus Torvalds 
477327c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
47741da177e4SLinus Torvalds {
47754c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
47768ed67789SDaniel Lezcano 		.dev = dev,
47776802f3adSIdo Schimmel 		{
47784c981e28SIdo Schimmel 			.event = event,
47796802f3adSIdo Schimmel 		},
47808ed67789SDaniel Lezcano 	};
47817c6bb7d2SDavid Ahern 	struct net *net = dev_net(dev);
47828ed67789SDaniel Lezcano 
47837c6bb7d2SDavid Ahern 	if (net->ipv6.sysctl.skip_notify_on_dev_down)
47847c6bb7d2SDavid Ahern 		fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
47857c6bb7d2SDavid Ahern 	else
47867c6bb7d2SDavid Ahern 		fib6_clean_all(net, fib6_ifdown, &arg);
47874c981e28SIdo Schimmel }
47884c981e28SIdo Schimmel 
47894c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
47904c981e28SIdo Schimmel {
47914c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
47924c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
47934c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
47941da177e4SLinus Torvalds }
47951da177e4SLinus Torvalds 
479695c96174SEric Dumazet struct rt6_mtu_change_arg {
47971da177e4SLinus Torvalds 	struct net_device *dev;
479895c96174SEric Dumazet 	unsigned int mtu;
4799c0b220cfSDavid Ahern 	struct fib6_info *f6i;
48001da177e4SLinus Torvalds };
48011da177e4SLinus Torvalds 
4802cc5c073aSDavid Ahern static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
4803c0b220cfSDavid Ahern {
4804c0b220cfSDavid Ahern 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
4805cc5c073aSDavid Ahern 	struct fib6_info *f6i = arg->f6i;
4806c0b220cfSDavid Ahern 
4807c0b220cfSDavid Ahern 	/* For administrative MTU increase, there is no way to discover
4808c0b220cfSDavid Ahern 	 * IPv6 PMTU increase, so PMTU increase should be updated here.
4809c0b220cfSDavid Ahern 	 * Since RFC 1981 doesn't include administrative MTU increase
4810c0b220cfSDavid Ahern 	 * update PMTU increase is a MUST. (i.e. jumbo frame)
4811c0b220cfSDavid Ahern 	 */
4812c0b220cfSDavid Ahern 	if (nh->fib_nh_dev == arg->dev) {
4813c0b220cfSDavid Ahern 		struct inet6_dev *idev = __in6_dev_get(arg->dev);
4814c0b220cfSDavid Ahern 		u32 mtu = f6i->fib6_pmtu;
4815c0b220cfSDavid Ahern 
4816c0b220cfSDavid Ahern 		if (mtu >= arg->mtu ||
4817c0b220cfSDavid Ahern 		    (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4818c0b220cfSDavid Ahern 			fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4819c0b220cfSDavid Ahern 
4820c0b220cfSDavid Ahern 		spin_lock_bh(&rt6_exception_lock);
4821cc5c073aSDavid Ahern 		rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
4822c0b220cfSDavid Ahern 		spin_unlock_bh(&rt6_exception_lock);
4823c0b220cfSDavid Ahern 	}
4824c0b220cfSDavid Ahern 
4825c0b220cfSDavid Ahern 	return 0;
4826c0b220cfSDavid Ahern }
4827c0b220cfSDavid Ahern 
4828c0b220cfSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
48291da177e4SLinus Torvalds {
48301da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
48311da177e4SLinus Torvalds 	struct inet6_dev *idev;
48321da177e4SLinus Torvalds 
48331da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
48341da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
48351da177e4SLinus Torvalds 	   We still use this lock to block changes
48361da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
48371da177e4SLinus Torvalds 	*/
48381da177e4SLinus Torvalds 
48391da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
484038308473SDavid S. Miller 	if (!idev)
48411da177e4SLinus Torvalds 		return 0;
48421da177e4SLinus Torvalds 
4843c0b220cfSDavid Ahern 	if (fib6_metric_locked(f6i, RTAX_MTU))
48441da177e4SLinus Torvalds 		return 0;
4845c0b220cfSDavid Ahern 
4846c0b220cfSDavid Ahern 	arg->f6i = f6i;
48472d44234bSDavid Ahern 	if (f6i->nh) {
48482d44234bSDavid Ahern 		/* fib6_nh_mtu_change only returns 0, so this is safe */
48492d44234bSDavid Ahern 		return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
48502d44234bSDavid Ahern 						arg);
48512d44234bSDavid Ahern 	}
48522d44234bSDavid Ahern 
48531cf844c7SDavid Ahern 	return fib6_nh_mtu_change(f6i->fib6_nh, arg);
48541da177e4SLinus Torvalds }
48551da177e4SLinus Torvalds 
485695c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
48571da177e4SLinus Torvalds {
4858c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
4859c71099acSThomas Graf 		.dev = dev,
4860c71099acSThomas Graf 		.mtu = mtu,
4861c71099acSThomas Graf 	};
48621da177e4SLinus Torvalds 
48630c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
48641da177e4SLinus Torvalds }
48651da177e4SLinus Torvalds 
4866ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
486775425657SDavid Ahern 	[RTA_UNSPEC]		= { .strict_start_type = RTA_DPORT + 1 },
48685176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
4869aa8f8778SEric Dumazet 	[RTA_PREFSRC]		= { .len = sizeof(struct in6_addr) },
487086872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
4871ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
487286872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
487386872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
487451ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
4875c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
487619e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
487719e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
487832bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
4879622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
48803b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
4881aa8f8778SEric Dumazet 	[RTA_TABLE]		= { .type = NLA_U32 },
4882eacb9384SRoopa Prabhu 	[RTA_IP_PROTO]		= { .type = NLA_U8 },
4883eacb9384SRoopa Prabhu 	[RTA_SPORT]		= { .type = NLA_U16 },
4884eacb9384SRoopa Prabhu 	[RTA_DPORT]		= { .type = NLA_U16 },
48855b98324eSDavid Ahern 	[RTA_NH_ID]		= { .type = NLA_U32 },
488686872cb5SThomas Graf };
488786872cb5SThomas Graf 
488886872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4889333c4301SDavid Ahern 			      struct fib6_config *cfg,
4890333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
48911da177e4SLinus Torvalds {
489286872cb5SThomas Graf 	struct rtmsg *rtm;
489386872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
4894c78ba6d6SLubomir Rintel 	unsigned int pref;
489586872cb5SThomas Graf 	int err;
48961da177e4SLinus Torvalds 
48978cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
48988cb08174SJohannes Berg 				     rtm_ipv6_policy, extack);
489986872cb5SThomas Graf 	if (err < 0)
490086872cb5SThomas Graf 		goto errout;
49011da177e4SLinus Torvalds 
490286872cb5SThomas Graf 	err = -EINVAL;
490386872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
490486872cb5SThomas Graf 
490584db8407SMaciej Żenczykowski 	*cfg = (struct fib6_config){
490684db8407SMaciej Żenczykowski 		.fc_table = rtm->rtm_table,
490784db8407SMaciej Żenczykowski 		.fc_dst_len = rtm->rtm_dst_len,
490884db8407SMaciej Żenczykowski 		.fc_src_len = rtm->rtm_src_len,
490984db8407SMaciej Żenczykowski 		.fc_flags = RTF_UP,
491084db8407SMaciej Żenczykowski 		.fc_protocol = rtm->rtm_protocol,
491184db8407SMaciej Żenczykowski 		.fc_type = rtm->rtm_type,
491284db8407SMaciej Żenczykowski 
491384db8407SMaciej Żenczykowski 		.fc_nlinfo.portid = NETLINK_CB(skb).portid,
491484db8407SMaciej Żenczykowski 		.fc_nlinfo.nlh = nlh,
491584db8407SMaciej Żenczykowski 		.fc_nlinfo.nl_net = sock_net(skb->sk),
491684db8407SMaciej Żenczykowski 	};
491786872cb5SThomas Graf 
4918ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
4919ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
4920b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
4921b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
492286872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
492386872cb5SThomas Graf 
4924ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
4925ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
4926ab79ad14SMaciej Żenczykowski 
49271f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
49281f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
49291f56a01fSMartin KaFai Lau 
4930fc1e64e1SDavid Ahern 	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4931fc1e64e1SDavid Ahern 
49325b98324eSDavid Ahern 	if (tb[RTA_NH_ID]) {
49335b98324eSDavid Ahern 		if (tb[RTA_GATEWAY]   || tb[RTA_OIF] ||
49345b98324eSDavid Ahern 		    tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
49355b98324eSDavid Ahern 			NL_SET_ERR_MSG(extack,
49365b98324eSDavid Ahern 				       "Nexthop specification and nexthop id are mutually exclusive");
49375b98324eSDavid Ahern 			goto errout;
49385b98324eSDavid Ahern 		}
49395b98324eSDavid Ahern 		cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
49405b98324eSDavid Ahern 	}
49415b98324eSDavid Ahern 
494286872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
494367b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
494486872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
49451da177e4SLinus Torvalds 	}
4946e3818541SDavid Ahern 	if (tb[RTA_VIA]) {
4947e3818541SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4948e3818541SDavid Ahern 		goto errout;
4949e3818541SDavid Ahern 	}
495086872cb5SThomas Graf 
495186872cb5SThomas Graf 	if (tb[RTA_DST]) {
495286872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
495386872cb5SThomas Graf 
495486872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
495586872cb5SThomas Graf 			goto errout;
495686872cb5SThomas Graf 
495786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
49581da177e4SLinus Torvalds 	}
495986872cb5SThomas Graf 
496086872cb5SThomas Graf 	if (tb[RTA_SRC]) {
496186872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
496286872cb5SThomas Graf 
496386872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
496486872cb5SThomas Graf 			goto errout;
496586872cb5SThomas Graf 
496686872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
49671da177e4SLinus Torvalds 	}
496886872cb5SThomas Graf 
4969c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
497067b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4971c3968a85SDaniel Walter 
497286872cb5SThomas Graf 	if (tb[RTA_OIF])
497386872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
497486872cb5SThomas Graf 
497586872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
497686872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
497786872cb5SThomas Graf 
497886872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
497986872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
498086872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
49811da177e4SLinus Torvalds 	}
498286872cb5SThomas Graf 
498386872cb5SThomas Graf 	if (tb[RTA_TABLE])
498486872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
498586872cb5SThomas Graf 
498651ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
498751ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
498851ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
49899ed59592SDavid Ahern 
49909ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4991c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
49929ed59592SDavid Ahern 		if (err < 0)
49939ed59592SDavid Ahern 			goto errout;
499451ebd318SNicolas Dichtel 	}
499551ebd318SNicolas Dichtel 
4996c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
4997c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
4998c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
4999c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
5000c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
5001c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
5002c78ba6d6SLubomir Rintel 	}
5003c78ba6d6SLubomir Rintel 
500419e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
500519e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
500619e42e45SRoopa Prabhu 
50079ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
500819e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
500919e42e45SRoopa Prabhu 
5010c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
50119ed59592SDavid Ahern 		if (err < 0)
50129ed59592SDavid Ahern 			goto errout;
50139ed59592SDavid Ahern 	}
50149ed59592SDavid Ahern 
501532bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
501632bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
501732bc201eSXin Long 
501832bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
501932bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
502032bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
502132bc201eSXin Long 		}
502232bc201eSXin Long 	}
502332bc201eSXin Long 
502486872cb5SThomas Graf 	err = 0;
502586872cb5SThomas Graf errout:
502686872cb5SThomas Graf 	return err;
50271da177e4SLinus Torvalds }
50281da177e4SLinus Torvalds 
50296b9ea5a6SRoopa Prabhu struct rt6_nh {
50308d1c802bSDavid Ahern 	struct fib6_info *fib6_info;
50316b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
50326b9ea5a6SRoopa Prabhu 	struct list_head next;
50336b9ea5a6SRoopa Prabhu };
50346b9ea5a6SRoopa Prabhu 
5035d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net,
5036d4ead6b3SDavid Ahern 				 struct list_head *rt6_nh_list,
50378d1c802bSDavid Ahern 				 struct fib6_info *rt,
50388d1c802bSDavid Ahern 				 struct fib6_config *r_cfg)
50396b9ea5a6SRoopa Prabhu {
50406b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
50416b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
50426b9ea5a6SRoopa Prabhu 
50436b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
50448d1c802bSDavid Ahern 		/* check if fib6_info already exists */
50458d1c802bSDavid Ahern 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
50466b9ea5a6SRoopa Prabhu 			return err;
50476b9ea5a6SRoopa Prabhu 	}
50486b9ea5a6SRoopa Prabhu 
50496b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
50506b9ea5a6SRoopa Prabhu 	if (!nh)
50516b9ea5a6SRoopa Prabhu 		return -ENOMEM;
50528d1c802bSDavid Ahern 	nh->fib6_info = rt;
50536b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
50546b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
50556b9ea5a6SRoopa Prabhu 
50566b9ea5a6SRoopa Prabhu 	return 0;
50576b9ea5a6SRoopa Prabhu }
50586b9ea5a6SRoopa Prabhu 
50598d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt,
50608d1c802bSDavid Ahern 				   struct fib6_info *rt_last,
50613b1137feSDavid Ahern 				   struct nl_info *info,
50623b1137feSDavid Ahern 				   __u16 nlflags)
50633b1137feSDavid Ahern {
50643b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
50653b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
50663b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
50673b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
50683b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
50693b1137feSDavid Ahern 	 */
507093c2fb25SDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
507193c2fb25SDavid Ahern 		rt = list_first_entry(&rt_last->fib6_siblings,
50728d1c802bSDavid Ahern 				      struct fib6_info,
507393c2fb25SDavid Ahern 				      fib6_siblings);
50743b1137feSDavid Ahern 	}
50753b1137feSDavid Ahern 
50763b1137feSDavid Ahern 	if (rt)
50773b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
50783b1137feSDavid Ahern }
50793b1137feSDavid Ahern 
50800ee0f47cSIdo Schimmel static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
50810ee0f47cSIdo Schimmel {
50820ee0f47cSIdo Schimmel 	bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
50830ee0f47cSIdo Schimmel 	bool should_notify = false;
50840ee0f47cSIdo Schimmel 	struct fib6_info *leaf;
50850ee0f47cSIdo Schimmel 	struct fib6_node *fn;
50860ee0f47cSIdo Schimmel 
50870ee0f47cSIdo Schimmel 	rcu_read_lock();
50880ee0f47cSIdo Schimmel 	fn = rcu_dereference(rt->fib6_node);
50890ee0f47cSIdo Schimmel 	if (!fn)
50900ee0f47cSIdo Schimmel 		goto out;
50910ee0f47cSIdo Schimmel 
50920ee0f47cSIdo Schimmel 	leaf = rcu_dereference(fn->leaf);
50930ee0f47cSIdo Schimmel 	if (!leaf)
50940ee0f47cSIdo Schimmel 		goto out;
50950ee0f47cSIdo Schimmel 
50960ee0f47cSIdo Schimmel 	if (rt == leaf ||
50970ee0f47cSIdo Schimmel 	    (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
50980ee0f47cSIdo Schimmel 	     rt6_qualify_for_ecmp(leaf)))
50990ee0f47cSIdo Schimmel 		should_notify = true;
51000ee0f47cSIdo Schimmel out:
51010ee0f47cSIdo Schimmel 	rcu_read_unlock();
51020ee0f47cSIdo Schimmel 
51030ee0f47cSIdo Schimmel 	return should_notify;
51040ee0f47cSIdo Schimmel }
51050ee0f47cSIdo Schimmel 
5106333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
5107333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
510851ebd318SNicolas Dichtel {
51098d1c802bSDavid Ahern 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
51103b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
511151ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
511251ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
51138d1c802bSDavid Ahern 	struct fib6_info *rt;
51146b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
51156b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
51163b1137feSDavid Ahern 	__u16 nlflags;
511751ebd318SNicolas Dichtel 	int remaining;
511851ebd318SNicolas Dichtel 	int attrlen;
51196b9ea5a6SRoopa Prabhu 	int err = 1;
51206b9ea5a6SRoopa Prabhu 	int nhn = 0;
51216b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
51226b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
51236b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
512451ebd318SNicolas Dichtel 
51253b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
51263b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
51273b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
51283b1137feSDavid Ahern 
512935f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
513051ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
513151ebd318SNicolas Dichtel 
51326b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
51338d1c802bSDavid Ahern 	 * fib6_info structs per nexthop
51346b9ea5a6SRoopa Prabhu 	 */
513551ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
513651ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
513751ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
513851ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
513951ebd318SNicolas Dichtel 
514051ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
514151ebd318SNicolas Dichtel 		if (attrlen > 0) {
514251ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
514351ebd318SNicolas Dichtel 
514451ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
514551ebd318SNicolas Dichtel 			if (nla) {
514667b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
514751ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
514851ebd318SNicolas Dichtel 			}
514919e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
515019e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
515119e42e45SRoopa Prabhu 			if (nla)
515219e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
515351ebd318SNicolas Dichtel 		}
51546b9ea5a6SRoopa Prabhu 
515568e2ffdeSDavid Ahern 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5156acb54e3cSDavid Ahern 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
51578c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
51588c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
51598c5b83f0SRoopa Prabhu 			rt = NULL;
51606b9ea5a6SRoopa Prabhu 			goto cleanup;
51618c5b83f0SRoopa Prabhu 		}
5162b5d2d75eSDavid Ahern 		if (!rt6_qualify_for_ecmp(rt)) {
5163b5d2d75eSDavid Ahern 			err = -EINVAL;
5164b5d2d75eSDavid Ahern 			NL_SET_ERR_MSG(extack,
5165b5d2d75eSDavid Ahern 				       "Device only routes can not be added for IPv6 using the multipath API.");
5166b5d2d75eSDavid Ahern 			fib6_info_release(rt);
5167b5d2d75eSDavid Ahern 			goto cleanup;
5168b5d2d75eSDavid Ahern 		}
51696b9ea5a6SRoopa Prabhu 
51701cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
5171398958aeSIdo Schimmel 
5172d4ead6b3SDavid Ahern 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5173d4ead6b3SDavid Ahern 					    rt, &r_cfg);
517451ebd318SNicolas Dichtel 		if (err) {
517593531c67SDavid Ahern 			fib6_info_release(rt);
51766b9ea5a6SRoopa Prabhu 			goto cleanup;
517751ebd318SNicolas Dichtel 		}
51786b9ea5a6SRoopa Prabhu 
51796b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
518051ebd318SNicolas Dichtel 	}
51816b9ea5a6SRoopa Prabhu 
51829eee3b49SIdo Schimmel 	if (list_empty(&rt6_nh_list)) {
51839eee3b49SIdo Schimmel 		NL_SET_ERR_MSG(extack,
51849eee3b49SIdo Schimmel 			       "Invalid nexthop configuration - no valid nexthops");
51859eee3b49SIdo Schimmel 		return -EINVAL;
51869eee3b49SIdo Schimmel 	}
51879eee3b49SIdo Schimmel 
51883b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
51893b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
51903b1137feSDavid Ahern 	 * the full route when done
51913b1137feSDavid Ahern 	 */
51923b1137feSDavid Ahern 	info->skip_notify = 1;
51933b1137feSDavid Ahern 
5194ebee3cadSIdo Schimmel 	/* For add and replace, send one notification with all nexthops. For
5195ebee3cadSIdo Schimmel 	 * append, send one notification with all appended nexthops.
5196ebee3cadSIdo Schimmel 	 */
5197ebee3cadSIdo Schimmel 	info->skip_notify_kernel = 1;
5198ebee3cadSIdo Schimmel 
51996b9ea5a6SRoopa Prabhu 	err_nh = NULL;
52006b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
52018d1c802bSDavid Ahern 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
52028d1c802bSDavid Ahern 		fib6_info_release(nh->fib6_info);
52033b1137feSDavid Ahern 
5204f7225172SDavid Ahern 		if (!err) {
5205f7225172SDavid Ahern 			/* save reference to last route successfully inserted */
5206f7225172SDavid Ahern 			rt_last = nh->fib6_info;
5207f7225172SDavid Ahern 
52086b9ea5a6SRoopa Prabhu 			/* save reference to first route for notification */
5209f7225172SDavid Ahern 			if (!rt_notif)
52108d1c802bSDavid Ahern 				rt_notif = nh->fib6_info;
5211f7225172SDavid Ahern 		}
52126b9ea5a6SRoopa Prabhu 
52138d1c802bSDavid Ahern 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
52148d1c802bSDavid Ahern 		nh->fib6_info = NULL;
52156b9ea5a6SRoopa Prabhu 		if (err) {
52166b9ea5a6SRoopa Prabhu 			if (replace && nhn)
5217a5a82d84SJakub Kicinski 				NL_SET_ERR_MSG_MOD(extack,
5218a5a82d84SJakub Kicinski 						   "multipath route replace failed (check consistency of installed routes)");
52196b9ea5a6SRoopa Prabhu 			err_nh = nh;
52206b9ea5a6SRoopa Prabhu 			goto add_errout;
52216b9ea5a6SRoopa Prabhu 		}
52226b9ea5a6SRoopa Prabhu 
52231a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
522427596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
522527596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
522627596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
522727596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
522827596472SMichal Kubeček 		 * be added to it.
52291a72418bSNicolas Dichtel 		 */
523027596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
523127596472SMichal Kubeček 						     NLM_F_REPLACE);
5232afecdb37SBenjamin Poirier 		cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
52336b9ea5a6SRoopa Prabhu 		nhn++;
52346b9ea5a6SRoopa Prabhu 	}
52356b9ea5a6SRoopa Prabhu 
52360ee0f47cSIdo Schimmel 	/* An in-kernel notification should only be sent in case the new
52370ee0f47cSIdo Schimmel 	 * multipath route is added as the first route in the node, or if
52380ee0f47cSIdo Schimmel 	 * it was appended to it. We pass 'rt_notif' since it is the first
52390ee0f47cSIdo Schimmel 	 * sibling and might allow us to skip some checks in the replace case.
52400ee0f47cSIdo Schimmel 	 */
52410ee0f47cSIdo Schimmel 	if (ip6_route_mpath_should_notify(rt_notif)) {
52420ee0f47cSIdo Schimmel 		enum fib_event_type fib_event;
52430ee0f47cSIdo Schimmel 
52440ee0f47cSIdo Schimmel 		if (rt_notif->fib6_nsiblings != nhn - 1)
52450ee0f47cSIdo Schimmel 			fib_event = FIB_EVENT_ENTRY_APPEND;
52460ee0f47cSIdo Schimmel 		else
5247caafb250SIdo Schimmel 			fib_event = FIB_EVENT_ENTRY_REPLACE;
52480ee0f47cSIdo Schimmel 
52490ee0f47cSIdo Schimmel 		err = call_fib6_multipath_entry_notifiers(info->nl_net,
52500ee0f47cSIdo Schimmel 							  fib_event, rt_notif,
52510ee0f47cSIdo Schimmel 							  nhn - 1, extack);
52520ee0f47cSIdo Schimmel 		if (err) {
52530ee0f47cSIdo Schimmel 			/* Delete all the siblings that were just added */
52540ee0f47cSIdo Schimmel 			err_nh = NULL;
52550ee0f47cSIdo Schimmel 			goto add_errout;
52560ee0f47cSIdo Schimmel 		}
52570ee0f47cSIdo Schimmel 	}
5258ebee3cadSIdo Schimmel 
52593b1137feSDavid Ahern 	/* success ... tell user about new route */
52603b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
52616b9ea5a6SRoopa Prabhu 	goto cleanup;
52626b9ea5a6SRoopa Prabhu 
52636b9ea5a6SRoopa Prabhu add_errout:
52643b1137feSDavid Ahern 	/* send notification for routes that were added so that
52653b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
52663b1137feSDavid Ahern 	 * coherent
52673b1137feSDavid Ahern 	 */
52683b1137feSDavid Ahern 	if (rt_notif)
52693b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
52703b1137feSDavid Ahern 
52716b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
52726b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
52736b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
52746b9ea5a6SRoopa Prabhu 			break;
5275333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
52766b9ea5a6SRoopa Prabhu 	}
52776b9ea5a6SRoopa Prabhu 
52786b9ea5a6SRoopa Prabhu cleanup:
52796b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
52808d1c802bSDavid Ahern 		if (nh->fib6_info)
52818d1c802bSDavid Ahern 			fib6_info_release(nh->fib6_info);
52826b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
52836b9ea5a6SRoopa Prabhu 		kfree(nh);
52846b9ea5a6SRoopa Prabhu 	}
52856b9ea5a6SRoopa Prabhu 
52866b9ea5a6SRoopa Prabhu 	return err;
52876b9ea5a6SRoopa Prabhu }
52886b9ea5a6SRoopa Prabhu 
5289333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
5290333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
52916b9ea5a6SRoopa Prabhu {
52926b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
52936b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
52942291267eSColin Ian King 	int last_err = 0;
52956b9ea5a6SRoopa Prabhu 	int remaining;
52966b9ea5a6SRoopa Prabhu 	int attrlen;
52972291267eSColin Ian King 	int err;
52986b9ea5a6SRoopa Prabhu 
52996b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
53006b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
53016b9ea5a6SRoopa Prabhu 
53026b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
53036b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
53046b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
53056b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
53066b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
53076b9ea5a6SRoopa Prabhu 
53086b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
53096b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
53106b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
53116b9ea5a6SRoopa Prabhu 
53126b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
53136b9ea5a6SRoopa Prabhu 			if (nla) {
53146b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
53156b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
53166b9ea5a6SRoopa Prabhu 			}
53176b9ea5a6SRoopa Prabhu 		}
5318333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
53196b9ea5a6SRoopa Prabhu 		if (err)
53206b9ea5a6SRoopa Prabhu 			last_err = err;
53216b9ea5a6SRoopa Prabhu 
532251ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
532351ebd318SNicolas Dichtel 	}
532451ebd318SNicolas Dichtel 
532551ebd318SNicolas Dichtel 	return last_err;
532651ebd318SNicolas Dichtel }
532751ebd318SNicolas Dichtel 
5328c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5329c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
53301da177e4SLinus Torvalds {
533186872cb5SThomas Graf 	struct fib6_config cfg;
533286872cb5SThomas Graf 	int err;
53331da177e4SLinus Torvalds 
5334333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
533586872cb5SThomas Graf 	if (err < 0)
533686872cb5SThomas Graf 		return err;
533786872cb5SThomas Graf 
53385b98324eSDavid Ahern 	if (cfg.fc_nh_id &&
53395b98324eSDavid Ahern 	    !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
53405b98324eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
53415b98324eSDavid Ahern 		return -EINVAL;
53425b98324eSDavid Ahern 	}
53435b98324eSDavid Ahern 
534451ebd318SNicolas Dichtel 	if (cfg.fc_mp)
5345333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
53460ae81335SDavid Ahern 	else {
53470ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
5348333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
53491da177e4SLinus Torvalds 	}
53500ae81335SDavid Ahern }
53511da177e4SLinus Torvalds 
5352c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5353c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
53541da177e4SLinus Torvalds {
535586872cb5SThomas Graf 	struct fib6_config cfg;
535686872cb5SThomas Graf 	int err;
53571da177e4SLinus Torvalds 
5358333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
535986872cb5SThomas Graf 	if (err < 0)
536086872cb5SThomas Graf 		return err;
536186872cb5SThomas Graf 
536267f69513SDavid Ahern 	if (cfg.fc_metric == 0)
536367f69513SDavid Ahern 		cfg.fc_metric = IP6_RT_PRIO_USER;
536467f69513SDavid Ahern 
536551ebd318SNicolas Dichtel 	if (cfg.fc_mp)
5366333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
536751ebd318SNicolas Dichtel 	else
5368acb54e3cSDavid Ahern 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
53691da177e4SLinus Torvalds }
53701da177e4SLinus Torvalds 
5371a1b7a1f0SDavid Ahern /* add the overhead of this fib6_nh to nexthop_len */
5372a1b7a1f0SDavid Ahern static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
5373339bf98fSThomas Graf {
5374a1b7a1f0SDavid Ahern 	int *nexthop_len = arg;
5375beb1afacSDavid Ahern 
5376a1b7a1f0SDavid Ahern 	*nexthop_len += nla_total_size(0)	 /* RTA_MULTIPATH */
5377a1b7a1f0SDavid Ahern 		     + NLA_ALIGN(sizeof(struct rtnexthop))
5378a1b7a1f0SDavid Ahern 		     + nla_total_size(16); /* RTA_GATEWAY */
5379f88d8ea6SDavid Ahern 
5380a1b7a1f0SDavid Ahern 	if (nh->fib_nh_lws) {
5381a1b7a1f0SDavid Ahern 		/* RTA_ENCAP_TYPE */
5382a1b7a1f0SDavid Ahern 		*nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5383a1b7a1f0SDavid Ahern 		/* RTA_ENCAP */
5384a1b7a1f0SDavid Ahern 		*nexthop_len += nla_total_size(2);
5385a1b7a1f0SDavid Ahern 	}
5386a1b7a1f0SDavid Ahern 
5387a1b7a1f0SDavid Ahern 	return 0;
5388a1b7a1f0SDavid Ahern }
5389a1b7a1f0SDavid Ahern 
5390a1b7a1f0SDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5391a1b7a1f0SDavid Ahern {
5392a1b7a1f0SDavid Ahern 	int nexthop_len;
5393a1b7a1f0SDavid Ahern 
5394a1b7a1f0SDavid Ahern 	if (f6i->nh) {
5395a1b7a1f0SDavid Ahern 		nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5396a1b7a1f0SDavid Ahern 		nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5397a1b7a1f0SDavid Ahern 					 &nexthop_len);
5398a1b7a1f0SDavid Ahern 	} else {
5399a1b7a1f0SDavid Ahern 		struct fib6_nh *nh = f6i->fib6_nh;
5400a1b7a1f0SDavid Ahern 
5401a1b7a1f0SDavid Ahern 		nexthop_len = 0;
5402a1b7a1f0SDavid Ahern 		if (f6i->fib6_nsiblings) {
5403beb1afacSDavid Ahern 			nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
5404beb1afacSDavid Ahern 				    + NLA_ALIGN(sizeof(struct rtnexthop))
5405beb1afacSDavid Ahern 				    + nla_total_size(16) /* RTA_GATEWAY */
5406a1b7a1f0SDavid Ahern 				    + lwtunnel_get_encap_size(nh->fib_nh_lws);
5407beb1afacSDavid Ahern 
5408a1b7a1f0SDavid Ahern 			nexthop_len *= f6i->fib6_nsiblings;
5409a1b7a1f0SDavid Ahern 		}
5410a1b7a1f0SDavid Ahern 		nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5411beb1afacSDavid Ahern 	}
5412beb1afacSDavid Ahern 
5413339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
5414339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
5415339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
5416339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
5417339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
5418339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
5419339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
5420339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
5421339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
54226a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
5423ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
5424c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
542519e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
5426beb1afacSDavid Ahern 	       + nexthop_len;
5427beb1afacSDavid Ahern }
5428beb1afacSDavid Ahern 
5429f88d8ea6SDavid Ahern static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5430f88d8ea6SDavid Ahern 				 unsigned char *flags)
5431f88d8ea6SDavid Ahern {
5432f88d8ea6SDavid Ahern 	if (nexthop_is_multipath(nh)) {
5433f88d8ea6SDavid Ahern 		struct nlattr *mp;
5434f88d8ea6SDavid Ahern 
54354255ff05SDavid Ahern 		mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5436f88d8ea6SDavid Ahern 		if (!mp)
5437f88d8ea6SDavid Ahern 			goto nla_put_failure;
5438f88d8ea6SDavid Ahern 
54397bdf4de1SDonald Sharp 		if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
5440f88d8ea6SDavid Ahern 			goto nla_put_failure;
5441f88d8ea6SDavid Ahern 
5442f88d8ea6SDavid Ahern 		nla_nest_end(skb, mp);
5443f88d8ea6SDavid Ahern 	} else {
5444f88d8ea6SDavid Ahern 		struct fib6_nh *fib6_nh;
5445f88d8ea6SDavid Ahern 
5446f88d8ea6SDavid Ahern 		fib6_nh = nexthop_fib6_nh(nh);
54477bdf4de1SDonald Sharp 		if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
5448f88d8ea6SDavid Ahern 				     flags, false) < 0)
5449f88d8ea6SDavid Ahern 			goto nla_put_failure;
5450f88d8ea6SDavid Ahern 	}
5451f88d8ea6SDavid Ahern 
5452f88d8ea6SDavid Ahern 	return 0;
5453f88d8ea6SDavid Ahern 
5454f88d8ea6SDavid Ahern nla_put_failure:
5455f88d8ea6SDavid Ahern 	return -EMSGSIZE;
5456f88d8ea6SDavid Ahern }
5457f88d8ea6SDavid Ahern 
5458d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
54598d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
5460d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
546115e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
5462f8cfe2ceSDavid Ahern 			 unsigned int flags)
54631da177e4SLinus Torvalds {
546422d0bd82SXin Long 	struct rt6_info *rt6 = (struct rt6_info *)dst;
546522d0bd82SXin Long 	struct rt6key *rt6_dst, *rt6_src;
546622d0bd82SXin Long 	u32 *pmetrics, table, rt6_flags;
5467f88d8ea6SDavid Ahern 	unsigned char nh_flags = 0;
54681da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
546922d0bd82SXin Long 	struct rtmsg *rtm;
5470d4ead6b3SDavid Ahern 	long expires = 0;
54711da177e4SLinus Torvalds 
547215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
547338308473SDavid S. Miller 	if (!nlh)
547426932566SPatrick McHardy 		return -EMSGSIZE;
54752d7202bfSThomas Graf 
547622d0bd82SXin Long 	if (rt6) {
547722d0bd82SXin Long 		rt6_dst = &rt6->rt6i_dst;
547822d0bd82SXin Long 		rt6_src = &rt6->rt6i_src;
547922d0bd82SXin Long 		rt6_flags = rt6->rt6i_flags;
548022d0bd82SXin Long 	} else {
548122d0bd82SXin Long 		rt6_dst = &rt->fib6_dst;
548222d0bd82SXin Long 		rt6_src = &rt->fib6_src;
548322d0bd82SXin Long 		rt6_flags = rt->fib6_flags;
548422d0bd82SXin Long 	}
548522d0bd82SXin Long 
54862d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
54871da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
548822d0bd82SXin Long 	rtm->rtm_dst_len = rt6_dst->plen;
548922d0bd82SXin Long 	rtm->rtm_src_len = rt6_src->plen;
54901da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
549193c2fb25SDavid Ahern 	if (rt->fib6_table)
549293c2fb25SDavid Ahern 		table = rt->fib6_table->tb6_id;
5493c71099acSThomas Graf 	else
54949e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
549597f0082aSKalash Nainwal 	rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
5496c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
5497c78679e8SDavid S. Miller 		goto nla_put_failure;
5498e8478e80SDavid Ahern 
5499e8478e80SDavid Ahern 	rtm->rtm_type = rt->fib6_type;
55001da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
55011da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
550293c2fb25SDavid Ahern 	rtm->rtm_protocol = rt->fib6_protocol;
55031da177e4SLinus Torvalds 
550422d0bd82SXin Long 	if (rt6_flags & RTF_CACHE)
55051da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
55061da177e4SLinus Torvalds 
5507d4ead6b3SDavid Ahern 	if (dest) {
5508d4ead6b3SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, dest))
5509c78679e8SDavid S. Miller 			goto nla_put_failure;
55101da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
55111da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
551222d0bd82SXin Long 		if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
5513c78679e8SDavid S. Miller 			goto nla_put_failure;
55141da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
55151da177e4SLinus Torvalds 	if (src) {
5516930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
5517c78679e8SDavid S. Miller 			goto nla_put_failure;
55181da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
5519c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
552022d0bd82SXin Long 		   nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
5521c78679e8SDavid S. Miller 		goto nla_put_failure;
55221da177e4SLinus Torvalds #endif
55237bc570c8SYOSHIFUJI Hideaki 	if (iif) {
55247bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
552522d0bd82SXin Long 		if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
5526fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
55272cf75070SNikolay Aleksandrov 
55287bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
55297bc570c8SYOSHIFUJI Hideaki 				return 0;
5530fd61c6baSDavid Ahern 			if (err < 0)
55317bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
55327bc570c8SYOSHIFUJI Hideaki 		} else
55337bc570c8SYOSHIFUJI Hideaki #endif
5534c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
5535c78679e8SDavid S. Miller 				goto nla_put_failure;
5536d4ead6b3SDavid Ahern 	} else if (dest) {
55371da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
5538d4ead6b3SDavid Ahern 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
5539930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5540c78679e8SDavid S. Miller 			goto nla_put_failure;
5541c3968a85SDaniel Walter 	}
5542c3968a85SDaniel Walter 
554393c2fb25SDavid Ahern 	if (rt->fib6_prefsrc.plen) {
5544c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
554593c2fb25SDavid Ahern 		saddr_buf = rt->fib6_prefsrc.addr;
5546930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5547c78679e8SDavid S. Miller 			goto nla_put_failure;
55481da177e4SLinus Torvalds 	}
55492d7202bfSThomas Graf 
5550d4ead6b3SDavid Ahern 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5551d4ead6b3SDavid Ahern 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
55522d7202bfSThomas Graf 		goto nla_put_failure;
55532d7202bfSThomas Graf 
555493c2fb25SDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
5555beb1afacSDavid Ahern 		goto nla_put_failure;
5556beb1afacSDavid Ahern 
5557beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
5558beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
5559beb1afacSDavid Ahern 	 */
556022d0bd82SXin Long 	if (rt6) {
556122d0bd82SXin Long 		if (rt6_flags & RTF_GATEWAY &&
556222d0bd82SXin Long 		    nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
556322d0bd82SXin Long 			goto nla_put_failure;
556422d0bd82SXin Long 
556522d0bd82SXin Long 		if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
556622d0bd82SXin Long 			goto nla_put_failure;
55676b13d8f7SOliver Herms 
55686b13d8f7SOliver Herms 		if (dst->lwtstate &&
55696b13d8f7SOliver Herms 		    lwtunnel_fill_encap(skb, dst->lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
55706b13d8f7SOliver Herms 			goto nla_put_failure;
557122d0bd82SXin Long 	} else if (rt->fib6_nsiblings) {
55728d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
5573beb1afacSDavid Ahern 		struct nlattr *mp;
5574beb1afacSDavid Ahern 
5575ae0be8deSMichal Kubecek 		mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5576beb1afacSDavid Ahern 		if (!mp)
5577beb1afacSDavid Ahern 			goto nla_put_failure;
5578beb1afacSDavid Ahern 
55791cf844c7SDavid Ahern 		if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
55807bdf4de1SDonald Sharp 				    rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
5581beb1afacSDavid Ahern 			goto nla_put_failure;
5582beb1afacSDavid Ahern 
5583beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
558493c2fb25SDavid Ahern 					 &rt->fib6_siblings, fib6_siblings) {
55851cf844c7SDavid Ahern 			if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
55867bdf4de1SDonald Sharp 					    sibling->fib6_nh->fib_nh_weight,
55877bdf4de1SDonald Sharp 					    AF_INET6) < 0)
558894f826b8SEric Dumazet 				goto nla_put_failure;
558994f826b8SEric Dumazet 		}
55902d7202bfSThomas Graf 
5591beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
5592f88d8ea6SDavid Ahern 	} else if (rt->nh) {
5593f88d8ea6SDavid Ahern 		if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5594f88d8ea6SDavid Ahern 			goto nla_put_failure;
5595ecc5663cSDavid Ahern 
5596f88d8ea6SDavid Ahern 		if (nexthop_is_blackhole(rt->nh))
5597f88d8ea6SDavid Ahern 			rtm->rtm_type = RTN_BLACKHOLE;
5598f88d8ea6SDavid Ahern 
55994f80116dSRoopa Prabhu 		if (net->ipv4.sysctl_nexthop_compat_mode &&
56004f80116dSRoopa Prabhu 		    rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5601f88d8ea6SDavid Ahern 			goto nla_put_failure;
5602f88d8ea6SDavid Ahern 
5603f88d8ea6SDavid Ahern 		rtm->rtm_flags |= nh_flags;
5604f88d8ea6SDavid Ahern 	} else {
56057bdf4de1SDonald Sharp 		if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
5606ecc5663cSDavid Ahern 				     &nh_flags, false) < 0)
5607c78679e8SDavid S. Miller 			goto nla_put_failure;
5608ecc5663cSDavid Ahern 
5609ecc5663cSDavid Ahern 		rtm->rtm_flags |= nh_flags;
5610beb1afacSDavid Ahern 	}
56118253947eSLi Wei 
561222d0bd82SXin Long 	if (rt6_flags & RTF_EXPIRES) {
561314895687SDavid Ahern 		expires = dst ? dst->expires : rt->expires;
561414895687SDavid Ahern 		expires -= jiffies;
561514895687SDavid Ahern 	}
561669cdf8f9SYOSHIFUJI Hideaki 
5617bb3c4ab9SIdo Schimmel 	if (!dst) {
5618bb3c4ab9SIdo Schimmel 		if (rt->offload)
5619bb3c4ab9SIdo Schimmel 			rtm->rtm_flags |= RTM_F_OFFLOAD;
5620bb3c4ab9SIdo Schimmel 		if (rt->trap)
5621bb3c4ab9SIdo Schimmel 			rtm->rtm_flags |= RTM_F_TRAP;
56220c5fcf9eSAmit Cohen 		if (rt->offload_failed)
56230c5fcf9eSAmit Cohen 			rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
5624bb3c4ab9SIdo Schimmel 	}
5625bb3c4ab9SIdo Schimmel 
5626d4ead6b3SDavid Ahern 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
5627e3703b3dSThomas Graf 		goto nla_put_failure;
56281da177e4SLinus Torvalds 
562922d0bd82SXin Long 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
5630c78ba6d6SLubomir Rintel 		goto nla_put_failure;
5631c78ba6d6SLubomir Rintel 
563219e42e45SRoopa Prabhu 
5633053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5634053c095aSJohannes Berg 	return 0;
56352d7202bfSThomas Graf 
56362d7202bfSThomas Graf nla_put_failure:
563726932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
563826932566SPatrick McHardy 	return -EMSGSIZE;
56391da177e4SLinus Torvalds }
56401da177e4SLinus Torvalds 
56412c170e07SDavid Ahern static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
56422c170e07SDavid Ahern {
56432c170e07SDavid Ahern 	const struct net_device *dev = arg;
56442c170e07SDavid Ahern 
56452c170e07SDavid Ahern 	if (nh->fib_nh_dev == dev)
56462c170e07SDavid Ahern 		return 1;
56472c170e07SDavid Ahern 
56482c170e07SDavid Ahern 	return 0;
56492c170e07SDavid Ahern }
56502c170e07SDavid Ahern 
565113e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i,
565213e38901SDavid Ahern 			       const struct net_device *dev)
565313e38901SDavid Ahern {
56542c170e07SDavid Ahern 	if (f6i->nh) {
56552c170e07SDavid Ahern 		struct net_device *_dev = (struct net_device *)dev;
56562c170e07SDavid Ahern 
56572c170e07SDavid Ahern 		return !!nexthop_for_each_fib6_nh(f6i->nh,
56582c170e07SDavid Ahern 						  fib6_info_nh_uses_dev,
56592c170e07SDavid Ahern 						  _dev);
56602c170e07SDavid Ahern 	}
56612c170e07SDavid Ahern 
56621cf844c7SDavid Ahern 	if (f6i->fib6_nh->fib_nh_dev == dev)
566313e38901SDavid Ahern 		return true;
566413e38901SDavid Ahern 
566513e38901SDavid Ahern 	if (f6i->fib6_nsiblings) {
566613e38901SDavid Ahern 		struct fib6_info *sibling, *next_sibling;
566713e38901SDavid Ahern 
566813e38901SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
566913e38901SDavid Ahern 					 &f6i->fib6_siblings, fib6_siblings) {
56701cf844c7SDavid Ahern 			if (sibling->fib6_nh->fib_nh_dev == dev)
567113e38901SDavid Ahern 				return true;
567213e38901SDavid Ahern 		}
567313e38901SDavid Ahern 	}
567413e38901SDavid Ahern 
567513e38901SDavid Ahern 	return false;
567613e38901SDavid Ahern }
567713e38901SDavid Ahern 
56781e47b483SStefano Brivio struct fib6_nh_exception_dump_walker {
56791e47b483SStefano Brivio 	struct rt6_rtnl_dump_arg *dump;
56801e47b483SStefano Brivio 	struct fib6_info *rt;
56811e47b483SStefano Brivio 	unsigned int flags;
56821e47b483SStefano Brivio 	unsigned int skip;
56831e47b483SStefano Brivio 	unsigned int count;
56841e47b483SStefano Brivio };
56851e47b483SStefano Brivio 
56861e47b483SStefano Brivio static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
56871e47b483SStefano Brivio {
56881e47b483SStefano Brivio 	struct fib6_nh_exception_dump_walker *w = arg;
56891e47b483SStefano Brivio 	struct rt6_rtnl_dump_arg *dump = w->dump;
56901e47b483SStefano Brivio 	struct rt6_exception_bucket *bucket;
56911e47b483SStefano Brivio 	struct rt6_exception *rt6_ex;
56921e47b483SStefano Brivio 	int i, err;
56931e47b483SStefano Brivio 
56941e47b483SStefano Brivio 	bucket = fib6_nh_get_excptn_bucket(nh, NULL);
56951e47b483SStefano Brivio 	if (!bucket)
56961e47b483SStefano Brivio 		return 0;
56971e47b483SStefano Brivio 
56981e47b483SStefano Brivio 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
56991e47b483SStefano Brivio 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
57001e47b483SStefano Brivio 			if (w->skip) {
57011e47b483SStefano Brivio 				w->skip--;
57021e47b483SStefano Brivio 				continue;
57031e47b483SStefano Brivio 			}
57041e47b483SStefano Brivio 
57051e47b483SStefano Brivio 			/* Expiration of entries doesn't bump sernum, insertion
57061e47b483SStefano Brivio 			 * does. Removal is triggered by insertion, so we can
57071e47b483SStefano Brivio 			 * rely on the fact that if entries change between two
57081e47b483SStefano Brivio 			 * partial dumps, this node is scanned again completely,
57091e47b483SStefano Brivio 			 * see rt6_insert_exception() and fib6_dump_table().
57101e47b483SStefano Brivio 			 *
57111e47b483SStefano Brivio 			 * Count expired entries we go through as handled
57121e47b483SStefano Brivio 			 * entries that we'll skip next time, in case of partial
57131e47b483SStefano Brivio 			 * node dump. Otherwise, if entries expire meanwhile,
57141e47b483SStefano Brivio 			 * we'll skip the wrong amount.
57151e47b483SStefano Brivio 			 */
57161e47b483SStefano Brivio 			if (rt6_check_expired(rt6_ex->rt6i)) {
57171e47b483SStefano Brivio 				w->count++;
57181e47b483SStefano Brivio 				continue;
57191e47b483SStefano Brivio 			}
57201e47b483SStefano Brivio 
57211e47b483SStefano Brivio 			err = rt6_fill_node(dump->net, dump->skb, w->rt,
57221e47b483SStefano Brivio 					    &rt6_ex->rt6i->dst, NULL, NULL, 0,
57231e47b483SStefano Brivio 					    RTM_NEWROUTE,
57241e47b483SStefano Brivio 					    NETLINK_CB(dump->cb->skb).portid,
57251e47b483SStefano Brivio 					    dump->cb->nlh->nlmsg_seq, w->flags);
57261e47b483SStefano Brivio 			if (err)
57271e47b483SStefano Brivio 				return err;
57281e47b483SStefano Brivio 
57291e47b483SStefano Brivio 			w->count++;
57301e47b483SStefano Brivio 		}
57311e47b483SStefano Brivio 		bucket++;
57321e47b483SStefano Brivio 	}
57331e47b483SStefano Brivio 
57341e47b483SStefano Brivio 	return 0;
57351e47b483SStefano Brivio }
57361e47b483SStefano Brivio 
5737bf9a8a06SStefano Brivio /* Return -1 if done with node, number of handled routes on partial dump */
57381e47b483SStefano Brivio int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
57391da177e4SLinus Torvalds {
57401da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
574113e38901SDavid Ahern 	struct fib_dump_filter *filter = &arg->filter;
574213e38901SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
57431f17e2f2SDavid Ahern 	struct net *net = arg->net;
57441e47b483SStefano Brivio 	int count = 0;
57451f17e2f2SDavid Ahern 
5746421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
5747bf9a8a06SStefano Brivio 		return -1;
57481da177e4SLinus Torvalds 
574913e38901SDavid Ahern 	if ((filter->flags & RTM_F_PREFIX) &&
575093c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_PREFIX_RT)) {
5751f8cfe2ceSDavid Ahern 		/* success since this is not a prefix route */
5752bf9a8a06SStefano Brivio 		return -1;
5753f8cfe2ceSDavid Ahern 	}
57541e47b483SStefano Brivio 	if (filter->filter_set &&
57551e47b483SStefano Brivio 	    ((filter->rt_type  && rt->fib6_type != filter->rt_type) ||
575613e38901SDavid Ahern 	     (filter->dev      && !fib6_info_uses_dev(rt, filter->dev)) ||
57571e47b483SStefano Brivio 	     (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5758bf9a8a06SStefano Brivio 		return -1;
575913e38901SDavid Ahern 	}
57601e47b483SStefano Brivio 
57611e47b483SStefano Brivio 	if (filter->filter_set ||
57621e47b483SStefano Brivio 	    !filter->dump_routes || !filter->dump_exceptions) {
576313e38901SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
5764f8cfe2ceSDavid Ahern 	}
57651da177e4SLinus Torvalds 
57661e47b483SStefano Brivio 	if (filter->dump_routes) {
57671e47b483SStefano Brivio 		if (skip) {
57681e47b483SStefano Brivio 			skip--;
57691e47b483SStefano Brivio 		} else {
57701e47b483SStefano Brivio 			if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
57711e47b483SStefano Brivio 					  0, RTM_NEWROUTE,
5772bf9a8a06SStefano Brivio 					  NETLINK_CB(arg->cb->skb).portid,
57731e47b483SStefano Brivio 					  arg->cb->nlh->nlmsg_seq, flags)) {
5774bf9a8a06SStefano Brivio 				return 0;
57751e47b483SStefano Brivio 			}
57761e47b483SStefano Brivio 			count++;
57771e47b483SStefano Brivio 		}
57781e47b483SStefano Brivio 	}
57791e47b483SStefano Brivio 
57801e47b483SStefano Brivio 	if (filter->dump_exceptions) {
57811e47b483SStefano Brivio 		struct fib6_nh_exception_dump_walker w = { .dump = arg,
57821e47b483SStefano Brivio 							   .rt = rt,
57831e47b483SStefano Brivio 							   .flags = flags,
57841e47b483SStefano Brivio 							   .skip = skip,
57851e47b483SStefano Brivio 							   .count = 0 };
57861e47b483SStefano Brivio 		int err;
57871e47b483SStefano Brivio 
57883b525691SEric Dumazet 		rcu_read_lock();
57891e47b483SStefano Brivio 		if (rt->nh) {
57901e47b483SStefano Brivio 			err = nexthop_for_each_fib6_nh(rt->nh,
57911e47b483SStefano Brivio 						       rt6_nh_dump_exceptions,
57921e47b483SStefano Brivio 						       &w);
57931e47b483SStefano Brivio 		} else {
57941e47b483SStefano Brivio 			err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
57951e47b483SStefano Brivio 		}
57963b525691SEric Dumazet 		rcu_read_unlock();
57971e47b483SStefano Brivio 
57981e47b483SStefano Brivio 		if (err)
57991e47b483SStefano Brivio 			return count += w.count;
58001e47b483SStefano Brivio 	}
5801bf9a8a06SStefano Brivio 
5802bf9a8a06SStefano Brivio 	return -1;
58031da177e4SLinus Torvalds }
58041da177e4SLinus Torvalds 
58050eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
58060eff0a27SJakub Kicinski 					const struct nlmsghdr *nlh,
58070eff0a27SJakub Kicinski 					struct nlattr **tb,
58080eff0a27SJakub Kicinski 					struct netlink_ext_ack *extack)
58090eff0a27SJakub Kicinski {
58100eff0a27SJakub Kicinski 	struct rtmsg *rtm;
58110eff0a27SJakub Kicinski 	int i, err;
58120eff0a27SJakub Kicinski 
58130eff0a27SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
58140eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
58150eff0a27SJakub Kicinski 				   "Invalid header for get route request");
58160eff0a27SJakub Kicinski 		return -EINVAL;
58170eff0a27SJakub Kicinski 	}
58180eff0a27SJakub Kicinski 
58190eff0a27SJakub Kicinski 	if (!netlink_strict_get_check(skb))
58208cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
58210eff0a27SJakub Kicinski 					      rtm_ipv6_policy, extack);
58220eff0a27SJakub Kicinski 
58230eff0a27SJakub Kicinski 	rtm = nlmsg_data(nlh);
58240eff0a27SJakub Kicinski 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
58250eff0a27SJakub Kicinski 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
58260eff0a27SJakub Kicinski 	    rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
58270eff0a27SJakub Kicinski 	    rtm->rtm_type) {
58280eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
58290eff0a27SJakub Kicinski 		return -EINVAL;
58300eff0a27SJakub Kicinski 	}
58310eff0a27SJakub Kicinski 	if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
58320eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
58330eff0a27SJakub Kicinski 				   "Invalid flags for get route request");
58340eff0a27SJakub Kicinski 		return -EINVAL;
58350eff0a27SJakub Kicinski 	}
58360eff0a27SJakub Kicinski 
58378cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
58380eff0a27SJakub Kicinski 					    rtm_ipv6_policy, extack);
58390eff0a27SJakub Kicinski 	if (err)
58400eff0a27SJakub Kicinski 		return err;
58410eff0a27SJakub Kicinski 
58420eff0a27SJakub Kicinski 	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
58430eff0a27SJakub Kicinski 	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
58440eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
58450eff0a27SJakub Kicinski 		return -EINVAL;
58460eff0a27SJakub Kicinski 	}
58470eff0a27SJakub Kicinski 
58480eff0a27SJakub Kicinski 	for (i = 0; i <= RTA_MAX; i++) {
58490eff0a27SJakub Kicinski 		if (!tb[i])
58500eff0a27SJakub Kicinski 			continue;
58510eff0a27SJakub Kicinski 
58520eff0a27SJakub Kicinski 		switch (i) {
58530eff0a27SJakub Kicinski 		case RTA_SRC:
58540eff0a27SJakub Kicinski 		case RTA_DST:
58550eff0a27SJakub Kicinski 		case RTA_IIF:
58560eff0a27SJakub Kicinski 		case RTA_OIF:
58570eff0a27SJakub Kicinski 		case RTA_MARK:
58580eff0a27SJakub Kicinski 		case RTA_UID:
58590eff0a27SJakub Kicinski 		case RTA_SPORT:
58600eff0a27SJakub Kicinski 		case RTA_DPORT:
58610eff0a27SJakub Kicinski 		case RTA_IP_PROTO:
58620eff0a27SJakub Kicinski 			break;
58630eff0a27SJakub Kicinski 		default:
58640eff0a27SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
58650eff0a27SJakub Kicinski 			return -EINVAL;
58660eff0a27SJakub Kicinski 		}
58670eff0a27SJakub Kicinski 	}
58680eff0a27SJakub Kicinski 
58690eff0a27SJakub Kicinski 	return 0;
58700eff0a27SJakub Kicinski }
58710eff0a27SJakub Kicinski 
5872c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5873c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
58741da177e4SLinus Torvalds {
58753b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
5876ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
587718c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
5878a68886a6SDavid Ahern 	struct fib6_info *from;
587918c3a61cSRoopa Prabhu 	struct dst_entry *dst;
58801da177e4SLinus Torvalds 	struct rt6_info *rt;
5881ab364a6fSThomas Graf 	struct sk_buff *skb;
5882ab364a6fSThomas Graf 	struct rtmsg *rtm;
5883744486d4SMaciej Żenczykowski 	struct flowi6 fl6 = {};
588418c3a61cSRoopa Prabhu 	bool fibmatch;
5885ab364a6fSThomas Graf 
58860eff0a27SJakub Kicinski 	err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
5887ab364a6fSThomas Graf 	if (err < 0)
5888ab364a6fSThomas Graf 		goto errout;
5889ab364a6fSThomas Graf 
5890ab364a6fSThomas Graf 	err = -EINVAL;
589138b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
589238b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
589318c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
5894ab364a6fSThomas Graf 
5895ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
5896ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5897ab364a6fSThomas Graf 			goto errout;
5898ab364a6fSThomas Graf 
58994e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
5900ab364a6fSThomas Graf 	}
5901ab364a6fSThomas Graf 
5902ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
5903ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5904ab364a6fSThomas Graf 			goto errout;
5905ab364a6fSThomas Graf 
59064e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
5907ab364a6fSThomas Graf 	}
5908ab364a6fSThomas Graf 
5909ab364a6fSThomas Graf 	if (tb[RTA_IIF])
5910ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
5911ab364a6fSThomas Graf 
5912ab364a6fSThomas Graf 	if (tb[RTA_OIF])
591372331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
5914ab364a6fSThomas Graf 
59152e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
59162e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
59172e47b291SLorenzo Colitti 
5918622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
5919622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
5920622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
5921622ec2c9SLorenzo Colitti 	else
5922622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5923622ec2c9SLorenzo Colitti 
5924eacb9384SRoopa Prabhu 	if (tb[RTA_SPORT])
5925eacb9384SRoopa Prabhu 		fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5926eacb9384SRoopa Prabhu 
5927eacb9384SRoopa Prabhu 	if (tb[RTA_DPORT])
5928eacb9384SRoopa Prabhu 		fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5929eacb9384SRoopa Prabhu 
5930eacb9384SRoopa Prabhu 	if (tb[RTA_IP_PROTO]) {
5931eacb9384SRoopa Prabhu 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
59325e1a99eaSHangbin Liu 						  &fl6.flowi6_proto, AF_INET6,
59335e1a99eaSHangbin Liu 						  extack);
5934eacb9384SRoopa Prabhu 		if (err)
5935eacb9384SRoopa Prabhu 			goto errout;
5936eacb9384SRoopa Prabhu 	}
5937eacb9384SRoopa Prabhu 
5938ab364a6fSThomas Graf 	if (iif) {
5939ab364a6fSThomas Graf 		struct net_device *dev;
594072331bc0SShmulik Ladkani 		int flags = 0;
594172331bc0SShmulik Ladkani 
5942121622dbSFlorian Westphal 		rcu_read_lock();
5943121622dbSFlorian Westphal 
5944121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
5945ab364a6fSThomas Graf 		if (!dev) {
5946121622dbSFlorian Westphal 			rcu_read_unlock();
5947ab364a6fSThomas Graf 			err = -ENODEV;
5948ab364a6fSThomas Graf 			goto errout;
5949ab364a6fSThomas Graf 		}
595072331bc0SShmulik Ladkani 
595172331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
595272331bc0SShmulik Ladkani 
595372331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
595472331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
595572331bc0SShmulik Ladkani 
5956b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
5957121622dbSFlorian Westphal 
5958121622dbSFlorian Westphal 		rcu_read_unlock();
595972331bc0SShmulik Ladkani 	} else {
596072331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
596172331bc0SShmulik Ladkani 
596218c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
596318c3a61cSRoopa Prabhu 	}
596418c3a61cSRoopa Prabhu 
596518c3a61cSRoopa Prabhu 
596618c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
596718c3a61cSRoopa Prabhu 	if (rt->dst.error) {
596818c3a61cSRoopa Prabhu 		err = rt->dst.error;
596918c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
597018c3a61cSRoopa Prabhu 		goto errout;
5971ab364a6fSThomas Graf 	}
59721da177e4SLinus Torvalds 
59739d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
59749d6acb3bSWANG Cong 		err = rt->dst.error;
59759d6acb3bSWANG Cong 		ip6_rt_put(rt);
59769d6acb3bSWANG Cong 		goto errout;
59779d6acb3bSWANG Cong 	}
59789d6acb3bSWANG Cong 
59791da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
598038308473SDavid S. Miller 	if (!skb) {
598194e187c0SAmerigo Wang 		ip6_rt_put(rt);
5982ab364a6fSThomas Graf 		err = -ENOBUFS;
5983ab364a6fSThomas Graf 		goto errout;
5984ab364a6fSThomas Graf 	}
59851da177e4SLinus Torvalds 
5986d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
5987a68886a6SDavid Ahern 
5988a68886a6SDavid Ahern 	rcu_read_lock();
5989a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
5990886b7a50SMartin KaFai Lau 	if (from) {
599118c3a61cSRoopa Prabhu 		if (fibmatch)
5992886b7a50SMartin KaFai Lau 			err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5993886b7a50SMartin KaFai Lau 					    iif, RTM_NEWROUTE,
5994886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
599518c3a61cSRoopa Prabhu 					    nlh->nlmsg_seq, 0);
599618c3a61cSRoopa Prabhu 		else
5997a68886a6SDavid Ahern 			err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5998a68886a6SDavid Ahern 					    &fl6.saddr, iif, RTM_NEWROUTE,
5999886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
6000886b7a50SMartin KaFai Lau 					    nlh->nlmsg_seq, 0);
6001886b7a50SMartin KaFai Lau 	} else {
6002886b7a50SMartin KaFai Lau 		err = -ENETUNREACH;
6003886b7a50SMartin KaFai Lau 	}
6004a68886a6SDavid Ahern 	rcu_read_unlock();
6005a68886a6SDavid Ahern 
60061da177e4SLinus Torvalds 	if (err < 0) {
6007ab364a6fSThomas Graf 		kfree_skb(skb);
6008ab364a6fSThomas Graf 		goto errout;
60091da177e4SLinus Torvalds 	}
60101da177e4SLinus Torvalds 
601115e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
6012ab364a6fSThomas Graf errout:
60131da177e4SLinus Torvalds 	return err;
60141da177e4SLinus Torvalds }
60151da177e4SLinus Torvalds 
60168d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
601737a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
60181da177e4SLinus Torvalds {
60191da177e4SLinus Torvalds 	struct sk_buff *skb;
60205578689aSDaniel Lezcano 	struct net *net = info->nl_net;
6021528c4cebSDenis V. Lunev 	u32 seq;
6022528c4cebSDenis V. Lunev 	int err;
60230d51aa80SJamal Hadi Salim 
6024528c4cebSDenis V. Lunev 	err = -ENOBUFS;
602538308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
602686872cb5SThomas Graf 
602719e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
602838308473SDavid S. Miller 	if (!skb)
602921713ebcSThomas Graf 		goto errout;
60301da177e4SLinus Torvalds 
6031d4ead6b3SDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6032f8cfe2ceSDavid Ahern 			    event, info->portid, seq, nlm_flags);
603326932566SPatrick McHardy 	if (err < 0) {
603426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
603526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
603626932566SPatrick McHardy 		kfree_skb(skb);
603726932566SPatrick McHardy 		goto errout;
603826932566SPatrick McHardy 	}
603915e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
60405578689aSDaniel Lezcano 		    info->nlh, gfp_any());
60411ce85fe4SPablo Neira Ayuso 	return;
604221713ebcSThomas Graf errout:
604321713ebcSThomas Graf 	if (err < 0)
60445578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
60451da177e4SLinus Torvalds }
60461da177e4SLinus Torvalds 
604719a3b7eeSDavid Ahern void fib6_rt_update(struct net *net, struct fib6_info *rt,
604819a3b7eeSDavid Ahern 		    struct nl_info *info)
604919a3b7eeSDavid Ahern {
605019a3b7eeSDavid Ahern 	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
605119a3b7eeSDavid Ahern 	struct sk_buff *skb;
605219a3b7eeSDavid Ahern 	int err = -ENOBUFS;
605319a3b7eeSDavid Ahern 
605419a3b7eeSDavid Ahern 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
605519a3b7eeSDavid Ahern 	if (!skb)
605619a3b7eeSDavid Ahern 		goto errout;
605719a3b7eeSDavid Ahern 
605819a3b7eeSDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
605919a3b7eeSDavid Ahern 			    RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
606019a3b7eeSDavid Ahern 	if (err < 0) {
606119a3b7eeSDavid Ahern 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
606219a3b7eeSDavid Ahern 		WARN_ON(err == -EMSGSIZE);
606319a3b7eeSDavid Ahern 		kfree_skb(skb);
606419a3b7eeSDavid Ahern 		goto errout;
606519a3b7eeSDavid Ahern 	}
606619a3b7eeSDavid Ahern 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
606719a3b7eeSDavid Ahern 		    info->nlh, gfp_any());
606819a3b7eeSDavid Ahern 	return;
606919a3b7eeSDavid Ahern errout:
607019a3b7eeSDavid Ahern 	if (err < 0)
607119a3b7eeSDavid Ahern 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
607219a3b7eeSDavid Ahern }
607319a3b7eeSDavid Ahern 
6074907eea48SAmit Cohen void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
60750c5fcf9eSAmit Cohen 			    bool offload, bool trap, bool offload_failed)
6076907eea48SAmit Cohen {
6077907eea48SAmit Cohen 	struct sk_buff *skb;
6078907eea48SAmit Cohen 	int err;
6079907eea48SAmit Cohen 
60800c5fcf9eSAmit Cohen 	if (f6i->offload == offload && f6i->trap == trap &&
60810c5fcf9eSAmit Cohen 	    f6i->offload_failed == offload_failed)
6082907eea48SAmit Cohen 		return;
6083907eea48SAmit Cohen 
6084907eea48SAmit Cohen 	f6i->offload = offload;
6085907eea48SAmit Cohen 	f6i->trap = trap;
6086*6fad361aSAmit Cohen 
6087*6fad361aSAmit Cohen 	/* 2 means send notifications only if offload_failed was changed. */
6088*6fad361aSAmit Cohen 	if (net->ipv6.sysctl.fib_notify_on_flag_change == 2 &&
6089*6fad361aSAmit Cohen 	    f6i->offload_failed == offload_failed)
6090*6fad361aSAmit Cohen 		return;
6091*6fad361aSAmit Cohen 
60920c5fcf9eSAmit Cohen 	f6i->offload_failed = offload_failed;
6093907eea48SAmit Cohen 
6094907eea48SAmit Cohen 	if (!rcu_access_pointer(f6i->fib6_node))
6095907eea48SAmit Cohen 		/* The route was removed from the tree, do not send
6096907eea48SAmit Cohen 		 * notfication.
6097907eea48SAmit Cohen 		 */
6098907eea48SAmit Cohen 		return;
6099907eea48SAmit Cohen 
6100907eea48SAmit Cohen 	if (!net->ipv6.sysctl.fib_notify_on_flag_change)
6101907eea48SAmit Cohen 		return;
6102907eea48SAmit Cohen 
6103907eea48SAmit Cohen 	skb = nlmsg_new(rt6_nlmsg_size(f6i), GFP_KERNEL);
6104907eea48SAmit Cohen 	if (!skb) {
6105907eea48SAmit Cohen 		err = -ENOBUFS;
6106907eea48SAmit Cohen 		goto errout;
6107907eea48SAmit Cohen 	}
6108907eea48SAmit Cohen 
6109907eea48SAmit Cohen 	err = rt6_fill_node(net, skb, f6i, NULL, NULL, NULL, 0, RTM_NEWROUTE, 0,
6110907eea48SAmit Cohen 			    0, 0);
6111907eea48SAmit Cohen 	if (err < 0) {
6112907eea48SAmit Cohen 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6113907eea48SAmit Cohen 		WARN_ON(err == -EMSGSIZE);
6114907eea48SAmit Cohen 		kfree_skb(skb);
6115907eea48SAmit Cohen 		goto errout;
6116907eea48SAmit Cohen 	}
6117907eea48SAmit Cohen 
6118907eea48SAmit Cohen 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ROUTE, NULL, GFP_KERNEL);
6119907eea48SAmit Cohen 	return;
6120907eea48SAmit Cohen 
6121907eea48SAmit Cohen errout:
6122907eea48SAmit Cohen 	rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6123907eea48SAmit Cohen }
6124907eea48SAmit Cohen EXPORT_SYMBOL(fib6_info_hw_flags_set);
6125907eea48SAmit Cohen 
61268ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
6127351638e7SJiri Pirko 				unsigned long event, void *ptr)
61288ed67789SDaniel Lezcano {
6129351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6130c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
61318ed67789SDaniel Lezcano 
6132242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
6133242d3a49SWANG Cong 		return NOTIFY_OK;
6134242d3a49SWANG Cong 
6135242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
61361cf844c7SDavid Ahern 		net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
6137d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
61388ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
61398ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6140d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
61418ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
6142d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
61438ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
61448ed67789SDaniel Lezcano #endif
614576da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
614676da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
614776da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
614876da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
614976da0704SWANG Cong 		 */
615012d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
6151242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
615212d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
615312d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
6154242d3a49SWANG Cong #endif
61558ed67789SDaniel Lezcano 	}
61568ed67789SDaniel Lezcano 
61578ed67789SDaniel Lezcano 	return NOTIFY_OK;
61588ed67789SDaniel Lezcano }
61598ed67789SDaniel Lezcano 
61601da177e4SLinus Torvalds /*
61611da177e4SLinus Torvalds  *	/proc
61621da177e4SLinus Torvalds  */
61631da177e4SLinus Torvalds 
61641da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
61651da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
61661da177e4SLinus Torvalds {
616769ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
61681da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
616969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
617069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
617181eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
617269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
617369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
6174fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
617569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
61761da177e4SLinus Torvalds 
61771da177e4SLinus Torvalds 	return 0;
61781da177e4SLinus Torvalds }
61791da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
61801da177e4SLinus Torvalds 
61811da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
61821da177e4SLinus Torvalds 
618332927393SChristoph Hellwig static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
618432927393SChristoph Hellwig 			      void *buffer, size_t *lenp, loff_t *ppos)
61851da177e4SLinus Torvalds {
6186c486da34SLucian Adrian Grijincu 	struct net *net;
6187c486da34SLucian Adrian Grijincu 	int delay;
6188f0fb9b28SAditya Pakki 	int ret;
6189c486da34SLucian Adrian Grijincu 	if (!write)
6190c486da34SLucian Adrian Grijincu 		return -EINVAL;
6191c486da34SLucian Adrian Grijincu 
6192c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
6193c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
6194f0fb9b28SAditya Pakki 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6195f0fb9b28SAditya Pakki 	if (ret)
6196f0fb9b28SAditya Pakki 		return ret;
6197f0fb9b28SAditya Pakki 
61982ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
61991da177e4SLinus Torvalds 	return 0;
62001da177e4SLinus Torvalds }
62011da177e4SLinus Torvalds 
6202ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = {
62031da177e4SLinus Torvalds 	{
62041da177e4SLinus Torvalds 		.procname	=	"flush",
62054990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
62061da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
620789c8b3a1SDave Jones 		.mode		=	0200,
62086d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
62091da177e4SLinus Torvalds 	},
62101da177e4SLinus Torvalds 	{
62111da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
62129a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
62131da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
62141da177e4SLinus Torvalds 		.mode		=	0644,
62156d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
62161da177e4SLinus Torvalds 	},
62171da177e4SLinus Torvalds 	{
62181da177e4SLinus Torvalds 		.procname	=	"max_size",
62194990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
62201da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
62211da177e4SLinus Torvalds 		.mode		=	0644,
62226d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
62231da177e4SLinus Torvalds 	},
62241da177e4SLinus Torvalds 	{
62251da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
62264990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
62271da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
62281da177e4SLinus Torvalds 		.mode		=	0644,
62296d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
62301da177e4SLinus Torvalds 	},
62311da177e4SLinus Torvalds 	{
62321da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
62334990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
62341da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
62351da177e4SLinus Torvalds 		.mode		=	0644,
62366d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
62371da177e4SLinus Torvalds 	},
62381da177e4SLinus Torvalds 	{
62391da177e4SLinus Torvalds 		.procname	=	"gc_interval",
62404990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
62411da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
62421da177e4SLinus Torvalds 		.mode		=	0644,
62436d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
62441da177e4SLinus Torvalds 	},
62451da177e4SLinus Torvalds 	{
62461da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
62474990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
62481da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
62491da177e4SLinus Torvalds 		.mode		=	0644,
6250f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
62511da177e4SLinus Torvalds 	},
62521da177e4SLinus Torvalds 	{
62531da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
62544990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
62551da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
62561da177e4SLinus Torvalds 		.mode		=	0644,
62576d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
62581da177e4SLinus Torvalds 	},
62591da177e4SLinus Torvalds 	{
62601da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
62614990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
62621da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
62631da177e4SLinus Torvalds 		.mode		=	0644,
6264f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
62651da177e4SLinus Torvalds 	},
62661da177e4SLinus Torvalds 	{
62671da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
62684990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
62691da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
62701da177e4SLinus Torvalds 		.mode		=	0644,
62716d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
62721da177e4SLinus Torvalds 	},
62737c6bb7d2SDavid Ahern 	{
62747c6bb7d2SDavid Ahern 		.procname	=	"skip_notify_on_dev_down",
62757c6bb7d2SDavid Ahern 		.data		=	&init_net.ipv6.sysctl.skip_notify_on_dev_down,
62767c6bb7d2SDavid Ahern 		.maxlen		=	sizeof(int),
62777c6bb7d2SDavid Ahern 		.mode		=	0644,
6278b8e8a863SEiichi Tsukata 		.proc_handler	=	proc_dointvec_minmax,
6279eec4844fSMatteo Croce 		.extra1		=	SYSCTL_ZERO,
6280eec4844fSMatteo Croce 		.extra2		=	SYSCTL_ONE,
62817c6bb7d2SDavid Ahern 	},
6282f8572d8fSEric W. Biederman 	{ }
62831da177e4SLinus Torvalds };
62841da177e4SLinus Torvalds 
62852c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
6286760f2d01SDaniel Lezcano {
6287760f2d01SDaniel Lezcano 	struct ctl_table *table;
6288760f2d01SDaniel Lezcano 
6289760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
6290760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
6291760f2d01SDaniel Lezcano 			GFP_KERNEL);
62925ee09105SYOSHIFUJI Hideaki 
62935ee09105SYOSHIFUJI Hideaki 	if (table) {
62945ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
6295c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
629686393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
62975ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
62985ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
62995ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
63005ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
63015ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
63025ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
63035ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
63049c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
63057c6bb7d2SDavid Ahern 		table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
6306464dc801SEric W. Biederman 
6307464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
6308464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
6309464dc801SEric W. Biederman 			table[0].procname = NULL;
63105ee09105SYOSHIFUJI Hideaki 	}
63115ee09105SYOSHIFUJI Hideaki 
6312760f2d01SDaniel Lezcano 	return table;
6313760f2d01SDaniel Lezcano }
63141da177e4SLinus Torvalds #endif
63151da177e4SLinus Torvalds 
63162c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
6317cdb18761SDaniel Lezcano {
6318633d424bSPavel Emelyanov 	int ret = -ENOMEM;
63198ed67789SDaniel Lezcano 
632086393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
632186393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
6322f2fc6a54SBenjamin Thery 
6323fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6324fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
6325fc66f95cSEric Dumazet 
63261cf844c7SDavid Ahern 	net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
6327421842edSDavid Ahern 	if (!net->ipv6.fib6_null_entry)
6328421842edSDavid Ahern 		goto out_ip6_dst_entries;
63291cf844c7SDavid Ahern 	memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
63301cf844c7SDavid Ahern 	       sizeof(*net->ipv6.fib6_null_entry));
6331421842edSDavid Ahern 
63328ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
63338ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
63348ed67789SDaniel Lezcano 					   GFP_KERNEL);
63358ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
6336421842edSDavid Ahern 		goto out_fib6_null_entry;
6337d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
633862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
633962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
634074109218SWei Wang 	INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
63418ed67789SDaniel Lezcano 
63428ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6343feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
63448ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
63458ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
63468ed67789SDaniel Lezcano 					       GFP_KERNEL);
634768fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
634868fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
6349d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
635062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
635162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
635274109218SWei Wang 	INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
63538ed67789SDaniel Lezcano 
63548ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
63558ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
63568ed67789SDaniel Lezcano 					       GFP_KERNEL);
635768fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
635868fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
6359d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
636062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
636162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
636274109218SWei Wang 	INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
6363b9b33e7cSPaolo Abeni #ifdef CONFIG_IPV6_SUBTREES
6364b9b33e7cSPaolo Abeni 	net->ipv6.fib6_routes_require_src = 0;
6365b9b33e7cSPaolo Abeni #endif
63668ed67789SDaniel Lezcano #endif
63678ed67789SDaniel Lezcano 
6368b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
6369b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
6370b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6371b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6372b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6373b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6374b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6375b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
63767c6bb7d2SDavid Ahern 	net->ipv6.sysctl.skip_notify_on_dev_down = 0;
6377b339a47cSPeter Zijlstra 
63786891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
63796891a346SBenjamin Thery 
63808ed67789SDaniel Lezcano 	ret = 0;
63818ed67789SDaniel Lezcano out:
63828ed67789SDaniel Lezcano 	return ret;
6383f2fc6a54SBenjamin Thery 
638468fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
638568fffc67SPeter Zijlstra out_ip6_prohibit_entry:
638668fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
638768fffc67SPeter Zijlstra out_ip6_null_entry:
638868fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
638968fffc67SPeter Zijlstra #endif
6390421842edSDavid Ahern out_fib6_null_entry:
6391421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
6392fc66f95cSEric Dumazet out_ip6_dst_entries:
6393fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6394f2fc6a54SBenjamin Thery out_ip6_dst_ops:
6395f2fc6a54SBenjamin Thery 	goto out;
6396cdb18761SDaniel Lezcano }
6397cdb18761SDaniel Lezcano 
63982c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
6399cdb18761SDaniel Lezcano {
6400421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
64018ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
64028ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
64038ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
64048ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
64058ed67789SDaniel Lezcano #endif
640641bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6407cdb18761SDaniel Lezcano }
6408cdb18761SDaniel Lezcano 
6409d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
6410d189634eSThomas Graf {
6411d189634eSThomas Graf #ifdef CONFIG_PROC_FS
6412c3506372SChristoph Hellwig 	proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6413c3506372SChristoph Hellwig 			sizeof(struct ipv6_route_iter));
64143617d949SChristoph Hellwig 	proc_create_net_single("rt6_stats", 0444, net->proc_net,
64153617d949SChristoph Hellwig 			rt6_stats_seq_show, NULL);
6416d189634eSThomas Graf #endif
6417d189634eSThomas Graf 	return 0;
6418d189634eSThomas Graf }
6419d189634eSThomas Graf 
6420d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
6421d189634eSThomas Graf {
6422d189634eSThomas Graf #ifdef CONFIG_PROC_FS
6423ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
6424ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
6425d189634eSThomas Graf #endif
6426d189634eSThomas Graf }
6427d189634eSThomas Graf 
6428cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
6429cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
6430cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
6431cdb18761SDaniel Lezcano };
6432cdb18761SDaniel Lezcano 
6433c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
6434c3426b47SDavid S. Miller {
6435c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6436c3426b47SDavid S. Miller 
6437c3426b47SDavid S. Miller 	if (!bp)
6438c3426b47SDavid S. Miller 		return -ENOMEM;
6439c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
6440c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
6441c3426b47SDavid S. Miller 	return 0;
6442c3426b47SDavid S. Miller }
6443c3426b47SDavid S. Miller 
6444c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
6445c3426b47SDavid S. Miller {
6446c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
6447c3426b47SDavid S. Miller 
6448c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
644956a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
6450c3426b47SDavid S. Miller 	kfree(bp);
6451c3426b47SDavid S. Miller }
6452c3426b47SDavid S. Miller 
64532b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
6454c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
6455c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
6456c3426b47SDavid S. Miller };
6457c3426b47SDavid S. Miller 
6458d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
6459d189634eSThomas Graf 	.init = ip6_route_net_init_late,
6460d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
6461d189634eSThomas Graf };
6462d189634eSThomas Graf 
64638ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
64648ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
6465242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
64668ed67789SDaniel Lezcano };
64678ed67789SDaniel Lezcano 
64682f460933SWANG Cong void __init ip6_route_init_special_entries(void)
64692f460933SWANG Cong {
64702f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
64712f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
64722f460933SWANG Cong 	 * manually for init_net */
64731cf844c7SDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
64742f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
64752f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
64762f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
64772f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
64782f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
64792f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
64802f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
64812f460933SWANG Cong   #endif
64822f460933SWANG Cong }
64832f460933SWANG Cong 
6484138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6)
6485138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6486138d0be3SYonghong Song DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6487138d0be3SYonghong Song 
6488951cf368SYonghong Song BTF_ID_LIST(btf_fib6_info_id)
6489951cf368SYonghong Song BTF_ID(struct, fib6_info)
6490951cf368SYonghong Song 
649114fc6bd6SYonghong Song static const struct bpf_iter_seq_info ipv6_route_seq_info = {
6492138d0be3SYonghong Song 	.seq_ops		= &ipv6_route_seq_ops,
6493138d0be3SYonghong Song 	.init_seq_private	= bpf_iter_init_seq_net,
6494138d0be3SYonghong Song 	.fini_seq_private	= bpf_iter_fini_seq_net,
6495138d0be3SYonghong Song 	.seq_priv_size		= sizeof(struct ipv6_route_iter),
649614fc6bd6SYonghong Song };
649714fc6bd6SYonghong Song 
649814fc6bd6SYonghong Song static struct bpf_iter_reg ipv6_route_reg_info = {
649914fc6bd6SYonghong Song 	.target			= "ipv6_route",
65003c32cc1bSYonghong Song 	.ctx_arg_info_size	= 1,
65013c32cc1bSYonghong Song 	.ctx_arg_info		= {
65023c32cc1bSYonghong Song 		{ offsetof(struct bpf_iter__ipv6_route, rt),
65033c32cc1bSYonghong Song 		  PTR_TO_BTF_ID_OR_NULL },
65043c32cc1bSYonghong Song 	},
650514fc6bd6SYonghong Song 	.seq_info		= &ipv6_route_seq_info,
6506138d0be3SYonghong Song };
6507138d0be3SYonghong Song 
650815172a46SYonghong Song static int __init bpf_iter_register(void)
650915172a46SYonghong Song {
6510951cf368SYonghong Song 	ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id;
651115172a46SYonghong Song 	return bpf_iter_reg_target(&ipv6_route_reg_info);
6512138d0be3SYonghong Song }
6513138d0be3SYonghong Song 
6514138d0be3SYonghong Song static void bpf_iter_unregister(void)
6515138d0be3SYonghong Song {
6516ab2ee4fcSYonghong Song 	bpf_iter_unreg_target(&ipv6_route_reg_info);
6517138d0be3SYonghong Song }
6518138d0be3SYonghong Song #endif
6519138d0be3SYonghong Song #endif
6520138d0be3SYonghong Song 
6521433d49c3SDaniel Lezcano int __init ip6_route_init(void)
65221da177e4SLinus Torvalds {
6523433d49c3SDaniel Lezcano 	int ret;
65248d0b94afSMartin KaFai Lau 	int cpu;
6525433d49c3SDaniel Lezcano 
65269a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
65279a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
65289a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
65299a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
65309a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
6531c19a28e1SFernando Carrijo 		goto out;
653214e50e57SDavid S. Miller 
6533fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
65348ed67789SDaniel Lezcano 	if (ret)
6535bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
6536bdb3289fSDaniel Lezcano 
6537c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6538c3426b47SDavid S. Miller 	if (ret)
6539e8803b6cSDavid S. Miller 		goto out_dst_entries;
65402a0c451aSThomas Graf 
65417e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
65427e52b33bSDavid S. Miller 	if (ret)
65437e52b33bSDavid S. Miller 		goto out_register_inetpeer;
6544c3426b47SDavid S. Miller 
65455dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
65465dc121e9SArnaud Ebalard 
6547e8803b6cSDavid S. Miller 	ret = fib6_init();
6548433d49c3SDaniel Lezcano 	if (ret)
65498ed67789SDaniel Lezcano 		goto out_register_subsys;
6550433d49c3SDaniel Lezcano 
6551433d49c3SDaniel Lezcano 	ret = xfrm6_init();
6552433d49c3SDaniel Lezcano 	if (ret)
6553e8803b6cSDavid S. Miller 		goto out_fib6_init;
6554c35b7e72SDaniel Lezcano 
6555433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
6556433d49c3SDaniel Lezcano 	if (ret)
6557433d49c3SDaniel Lezcano 		goto xfrm6_init;
65587e5449c2SDaniel Lezcano 
6559d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
6560d189634eSThomas Graf 	if (ret)
6561d189634eSThomas Graf 		goto fib6_rules_init;
6562d189634eSThomas Graf 
656316feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
656416feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
656516feebcfSFlorian Westphal 	if (ret < 0)
656616feebcfSFlorian Westphal 		goto out_register_late_subsys;
656716feebcfSFlorian Westphal 
656816feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
656916feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
657016feebcfSFlorian Westphal 	if (ret < 0)
657116feebcfSFlorian Westphal 		goto out_register_late_subsys;
657216feebcfSFlorian Westphal 
657316feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
657416feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
657516feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
657616feebcfSFlorian Westphal 	if (ret < 0)
6577d189634eSThomas Graf 		goto out_register_late_subsys;
6578433d49c3SDaniel Lezcano 
65798ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
6580cdb18761SDaniel Lezcano 	if (ret)
6581d189634eSThomas Graf 		goto out_register_late_subsys;
65828ed67789SDaniel Lezcano 
6583138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6)
6584138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6585138d0be3SYonghong Song 	ret = bpf_iter_register();
6586138d0be3SYonghong Song 	if (ret)
6587138d0be3SYonghong Song 		goto out_register_late_subsys;
6588138d0be3SYonghong Song #endif
6589138d0be3SYonghong Song #endif
6590138d0be3SYonghong Song 
65918d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
65928d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
65938d0b94afSMartin KaFai Lau 
65948d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
65958d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
65968d0b94afSMartin KaFai Lau 	}
65978d0b94afSMartin KaFai Lau 
6598433d49c3SDaniel Lezcano out:
6599433d49c3SDaniel Lezcano 	return ret;
6600433d49c3SDaniel Lezcano 
6601d189634eSThomas Graf out_register_late_subsys:
660216feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
6603d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
6604433d49c3SDaniel Lezcano fib6_rules_init:
6605433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
6606433d49c3SDaniel Lezcano xfrm6_init:
6607433d49c3SDaniel Lezcano 	xfrm6_fini();
66082a0c451aSThomas Graf out_fib6_init:
66092a0c451aSThomas Graf 	fib6_gc_cleanup();
66108ed67789SDaniel Lezcano out_register_subsys:
66118ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
66127e52b33bSDavid S. Miller out_register_inetpeer:
66137e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
6614fc66f95cSEric Dumazet out_dst_entries:
6615fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
6616433d49c3SDaniel Lezcano out_kmem_cache:
6617f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6618433d49c3SDaniel Lezcano 	goto out;
66191da177e4SLinus Torvalds }
66201da177e4SLinus Torvalds 
66211da177e4SLinus Torvalds void ip6_route_cleanup(void)
66221da177e4SLinus Torvalds {
6623138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6)
6624138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6625138d0be3SYonghong Song 	bpf_iter_unregister();
6626138d0be3SYonghong Song #endif
6627138d0be3SYonghong Song #endif
66288ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
6629d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
6630101367c2SThomas Graf 	fib6_rules_cleanup();
66311da177e4SLinus Torvalds 	xfrm6_fini();
66321da177e4SLinus Torvalds 	fib6_gc_cleanup();
6633c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
66348ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
663541bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
6636f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
66371da177e4SLinus Torvalds }
6638