xref: /openbmc/linux/net/ipv6/route.c (revision 73c2c5cbb15a8a82d5bea52594b0beb038963bcc)
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 
26314e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
26414e50e57SDavid S. Miller 	.family			= AF_INET6,
265214f45c9SEric Dumazet 	.default_advmss		= ip6_default_advmss,
266f8a1b43bSDavid Ahern 	.neigh_lookup		= ip6_dst_neigh_lookup,
267c4c877b2SDaniel Borkmann 	.check			= ip6_dst_check,
268c4c877b2SDaniel Borkmann 	.destroy		= ip6_dst_destroy,
269c4c877b2SDaniel Borkmann 	.cow_metrics		= dst_cow_metrics_generic,
270c4c877b2SDaniel Borkmann 	.update_pmtu		= dst_blackhole_update_pmtu,
271c4c877b2SDaniel Borkmann 	.redirect		= dst_blackhole_redirect,
272c4c877b2SDaniel Borkmann 	.mtu			= dst_blackhole_mtu,
27314e50e57SDavid S. Miller };
27414e50e57SDavid S. Miller 
27562fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
27614edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
27762fa8a84SDavid S. Miller };
27862fa8a84SDavid S. Miller 
2798d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = {
28093c2fb25SDavid Ahern 	.fib6_flags	= (RTF_REJECT | RTF_NONEXTHOP),
28193c2fb25SDavid Ahern 	.fib6_protocol  = RTPROT_KERNEL,
28293c2fb25SDavid Ahern 	.fib6_metric	= ~(u32)0,
283f05713e0SEric Dumazet 	.fib6_ref	= REFCOUNT_INIT(1),
284421842edSDavid Ahern 	.fib6_type	= RTN_UNREACHABLE,
285421842edSDavid Ahern 	.fib6_metrics	= (struct dst_metrics *)&dst_default_metrics,
286421842edSDavid Ahern };
287421842edSDavid Ahern 
288fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
2891da177e4SLinus Torvalds 	.dst = {
2901da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2911da177e4SLinus Torvalds 		.__use		= 1,
2922c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
2931da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2941da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2951da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2961da177e4SLinus Torvalds 	},
2971da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2981da177e4SLinus Torvalds };
2991da177e4SLinus Torvalds 
300101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
301101367c2SThomas Graf 
302fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
303101367c2SThomas Graf 	.dst = {
304101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
305101367c2SThomas Graf 		.__use		= 1,
3062c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
307101367c2SThomas Graf 		.error		= -EACCES,
3089ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
3099ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
310101367c2SThomas Graf 	},
311101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
312101367c2SThomas Graf };
313101367c2SThomas Graf 
314fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
315101367c2SThomas Graf 	.dst = {
316101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
317101367c2SThomas Graf 		.__use		= 1,
3182c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
319101367c2SThomas Graf 		.error		= -EINVAL,
320352e512cSHerbert Xu 		.input		= dst_discard,
321ede2059dSEric W. Biederman 		.output		= dst_discard_out,
322101367c2SThomas Graf 	},
323101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
324101367c2SThomas Graf };
325101367c2SThomas Graf 
326101367c2SThomas Graf #endif
327101367c2SThomas Graf 
328ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt)
329ebfa45f0SMartin KaFai Lau {
330ebfa45f0SMartin KaFai Lau 	struct dst_entry *dst = &rt->dst;
331ebfa45f0SMartin KaFai Lau 
332ebfa45f0SMartin KaFai Lau 	memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
333ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_uncached);
334ebfa45f0SMartin KaFai Lau }
335ebfa45f0SMartin KaFai Lau 
3361da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
33793531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
338ad706862SMartin KaFai Lau 			       int flags)
3391da177e4SLinus Torvalds {
34097bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
341b2a9c0edSWei Wang 					1, DST_OBSOLETE_FORCE_CHK, flags);
342cf911662SDavid S. Miller 
34381eb8447SWei Wang 	if (rt) {
344ebfa45f0SMartin KaFai Lau 		rt6_info_init(rt);
34581eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
34681eb8447SWei Wang 	}
3478104891bSSteffen Klassert 
348cf911662SDavid S. Miller 	return rt;
3491da177e4SLinus Torvalds }
3509ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc);
351d52d3997SMartin KaFai Lau 
3521da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
355a68886a6SDavid Ahern 	struct fib6_info *from;
3568d0b94afSMartin KaFai Lau 	struct inet6_dev *idev;
3571da177e4SLinus Torvalds 
3581620a336SDavid Ahern 	ip_dst_metrics_put(dst);
3598d0b94afSMartin KaFai Lau 	rt6_uncached_list_del(rt);
3608d0b94afSMartin KaFai Lau 
3618d0b94afSMartin KaFai Lau 	idev = rt->rt6i_idev;
36238308473SDavid S. Miller 	if (idev) {
3631da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3641da177e4SLinus Torvalds 		in6_dev_put(idev);
3651da177e4SLinus Torvalds 	}
3661716a961SGao feng 
3670e233874SEric Dumazet 	from = xchg((__force struct fib6_info **)&rt->from, NULL);
36893531c67SDavid Ahern 	fib6_info_release(from);
369b3419363SDavid S. Miller }
370b3419363SDavid S. Miller 
3711da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3721da177e4SLinus Torvalds 			   int how)
3731da177e4SLinus Torvalds {
3741da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3751da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3765a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
377c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3781da177e4SLinus Torvalds 
379e5645f51SWei Wang 	if (idev && idev->dev != loopback_dev) {
380e5645f51SWei Wang 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
38138308473SDavid S. Miller 		if (loopback_idev) {
3821da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
3831da177e4SLinus Torvalds 			in6_dev_put(idev);
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 	}
38697cac082SDavid S. Miller }
3871da177e4SLinus Torvalds 
3885973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt)
3895973fb1eSMartin KaFai Lau {
3905973fb1eSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_EXPIRES)
3915973fb1eSMartin KaFai Lau 		return time_after(jiffies, rt->dst.expires);
3925973fb1eSMartin KaFai Lau 	else
3935973fb1eSMartin KaFai Lau 		return false;
3945973fb1eSMartin KaFai Lau }
3955973fb1eSMartin KaFai Lau 
396a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3971da177e4SLinus Torvalds {
398a68886a6SDavid Ahern 	struct fib6_info *from;
399a68886a6SDavid Ahern 
400a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
401a68886a6SDavid Ahern 
4021716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
4031716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
404a50feda5SEric Dumazet 			return true;
405a68886a6SDavid Ahern 	} else if (from) {
4061e2ea8adSXin Long 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
407a68886a6SDavid Ahern 			fib6_check_expired(from);
4081716a961SGao feng 	}
409a50feda5SEric Dumazet 	return false;
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
412b1d40991SDavid Ahern void fib6_select_path(const struct net *net, struct fib6_result *res,
413b1d40991SDavid Ahern 		      struct flowi6 *fl6, int oif, bool have_oif_match,
414b1d40991SDavid Ahern 		      const struct sk_buff *skb, int strict)
41551ebd318SNicolas Dichtel {
4168d1c802bSDavid Ahern 	struct fib6_info *sibling, *next_sibling;
417b1d40991SDavid Ahern 	struct fib6_info *match = res->f6i;
418b1d40991SDavid Ahern 
41934fe5a1cSDavid Ahern 	if (!match->nh && (!match->fib6_nsiblings || have_oif_match))
420b1d40991SDavid Ahern 		goto out;
42151ebd318SNicolas Dichtel 
42234fe5a1cSDavid Ahern 	if (match->nh && have_oif_match && res->nh)
42334fe5a1cSDavid Ahern 		return;
42434fe5a1cSDavid Ahern 
425b673d6ccSJakub Sitnicki 	/* We might have already computed the hash for ICMPv6 errors. In such
426b673d6ccSJakub Sitnicki 	 * case it will always be non-zero. Otherwise now is the time to do it.
427b673d6ccSJakub Sitnicki 	 */
428f88d8ea6SDavid Ahern 	if (!fl6->mp_hash &&
429f88d8ea6SDavid Ahern 	    (!match->nh || nexthop_is_multipath(match->nh)))
430b4bac172SDavid Ahern 		fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
431b673d6ccSJakub Sitnicki 
432f88d8ea6SDavid Ahern 	if (unlikely(match->nh)) {
433f88d8ea6SDavid Ahern 		nexthop_path_fib6_result(res, fl6->mp_hash);
434f88d8ea6SDavid Ahern 		return;
435f88d8ea6SDavid Ahern 	}
436f88d8ea6SDavid Ahern 
4371cf844c7SDavid Ahern 	if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
438b1d40991SDavid Ahern 		goto out;
439bbfcd776SIdo Schimmel 
44093c2fb25SDavid Ahern 	list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
44193c2fb25SDavid Ahern 				 fib6_siblings) {
4421cf844c7SDavid Ahern 		const struct fib6_nh *nh = sibling->fib6_nh;
4435e670d84SDavid Ahern 		int nh_upper_bound;
4445e670d84SDavid Ahern 
445702cea56SDavid Ahern 		nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
4465e670d84SDavid Ahern 		if (fl6->mp_hash > nh_upper_bound)
4473d709f69SIdo Schimmel 			continue;
448702cea56SDavid Ahern 		if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
44952bd4c0cSNicolas Dichtel 			break;
45051ebd318SNicolas Dichtel 		match = sibling;
45151ebd318SNicolas Dichtel 		break;
45251ebd318SNicolas Dichtel 	}
4533d709f69SIdo Schimmel 
454b1d40991SDavid Ahern out:
455b1d40991SDavid Ahern 	res->f6i = match;
4561cf844c7SDavid Ahern 	res->nh = match->fib6_nh;
45751ebd318SNicolas Dichtel }
45851ebd318SNicolas Dichtel 
4591da177e4SLinus Torvalds /*
46066f5d6ceSWei Wang  *	Route lookup. rcu_read_lock() should be held.
4611da177e4SLinus Torvalds  */
4621da177e4SLinus Torvalds 
4630c59d006SDavid Ahern static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
4640c59d006SDavid Ahern 			       const struct in6_addr *saddr, int oif, int flags)
4650c59d006SDavid Ahern {
4660c59d006SDavid Ahern 	const struct net_device *dev;
4670c59d006SDavid Ahern 
4680c59d006SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD)
4690c59d006SDavid Ahern 		return false;
4700c59d006SDavid Ahern 
4710c59d006SDavid Ahern 	dev = nh->fib_nh_dev;
4720c59d006SDavid Ahern 	if (oif) {
4730c59d006SDavid Ahern 		if (dev->ifindex == oif)
4740c59d006SDavid Ahern 			return true;
4750c59d006SDavid Ahern 	} else {
4760c59d006SDavid Ahern 		if (ipv6_chk_addr(net, saddr, dev,
4770c59d006SDavid Ahern 				  flags & RT6_LOOKUP_F_IFACE))
4780c59d006SDavid Ahern 			return true;
4790c59d006SDavid Ahern 	}
4800c59d006SDavid Ahern 
4810c59d006SDavid Ahern 	return false;
4820c59d006SDavid Ahern }
4830c59d006SDavid Ahern 
484962b6803SDavid Ahern struct fib6_nh_dm_arg {
485962b6803SDavid Ahern 	struct net		*net;
486962b6803SDavid Ahern 	const struct in6_addr	*saddr;
487962b6803SDavid Ahern 	int			oif;
488962b6803SDavid Ahern 	int			flags;
489962b6803SDavid Ahern 	struct fib6_nh		*nh;
490962b6803SDavid Ahern };
491962b6803SDavid Ahern 
492962b6803SDavid Ahern static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
493962b6803SDavid Ahern {
494962b6803SDavid Ahern 	struct fib6_nh_dm_arg *arg = _arg;
495962b6803SDavid Ahern 
496962b6803SDavid Ahern 	arg->nh = nh;
497962b6803SDavid Ahern 	return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
498962b6803SDavid Ahern 				  arg->flags);
499962b6803SDavid Ahern }
500962b6803SDavid Ahern 
501962b6803SDavid Ahern /* returns fib6_nh from nexthop or NULL */
502962b6803SDavid Ahern static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
503962b6803SDavid Ahern 					struct fib6_result *res,
504962b6803SDavid Ahern 					const struct in6_addr *saddr,
505962b6803SDavid Ahern 					int oif, int flags)
506962b6803SDavid Ahern {
507962b6803SDavid Ahern 	struct fib6_nh_dm_arg arg = {
508962b6803SDavid Ahern 		.net   = net,
509962b6803SDavid Ahern 		.saddr = saddr,
510962b6803SDavid Ahern 		.oif   = oif,
511962b6803SDavid Ahern 		.flags = flags,
512962b6803SDavid Ahern 	};
513962b6803SDavid Ahern 
514962b6803SDavid Ahern 	if (nexthop_is_blackhole(nh))
515962b6803SDavid Ahern 		return NULL;
516962b6803SDavid Ahern 
517962b6803SDavid Ahern 	if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
518962b6803SDavid Ahern 		return arg.nh;
519962b6803SDavid Ahern 
520962b6803SDavid Ahern 	return NULL;
521962b6803SDavid Ahern }
522962b6803SDavid Ahern 
52375ef7389SDavid Ahern static void rt6_device_match(struct net *net, struct fib6_result *res,
52475ef7389SDavid Ahern 			     const struct in6_addr *saddr, int oif, int flags)
5251da177e4SLinus Torvalds {
52675ef7389SDavid Ahern 	struct fib6_info *f6i = res->f6i;
52775ef7389SDavid Ahern 	struct fib6_info *spf6i;
52875ef7389SDavid Ahern 	struct fib6_nh *nh;
5291da177e4SLinus Torvalds 
53075ef7389SDavid Ahern 	if (!oif && ipv6_addr_any(saddr)) {
531f88d8ea6SDavid Ahern 		if (unlikely(f6i->nh)) {
532f88d8ea6SDavid Ahern 			nh = nexthop_fib6_nh(f6i->nh);
533f88d8ea6SDavid Ahern 			if (nexthop_is_blackhole(f6i->nh))
534f88d8ea6SDavid Ahern 				goto out_blackhole;
535f88d8ea6SDavid Ahern 		} else {
5361cf844c7SDavid Ahern 			nh = f6i->fib6_nh;
537f88d8ea6SDavid Ahern 		}
5387d21fec9SDavid Ahern 		if (!(nh->fib_nh_flags & RTNH_F_DEAD))
5397d21fec9SDavid Ahern 			goto out;
5401da177e4SLinus Torvalds 	}
5411da177e4SLinus Torvalds 
54275ef7389SDavid Ahern 	for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
543962b6803SDavid Ahern 		bool matched = false;
544962b6803SDavid Ahern 
545962b6803SDavid Ahern 		if (unlikely(spf6i->nh)) {
546962b6803SDavid Ahern 			nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
547962b6803SDavid Ahern 					      oif, flags);
548962b6803SDavid Ahern 			if (nh)
549962b6803SDavid Ahern 				matched = true;
550962b6803SDavid Ahern 		} else {
5511cf844c7SDavid Ahern 			nh = spf6i->fib6_nh;
552962b6803SDavid Ahern 			if (__rt6_device_match(net, nh, saddr, oif, flags))
553962b6803SDavid Ahern 				matched = true;
554962b6803SDavid Ahern 		}
555962b6803SDavid Ahern 		if (matched) {
55675ef7389SDavid Ahern 			res->f6i = spf6i;
5577d21fec9SDavid Ahern 			goto out;
55875ef7389SDavid Ahern 		}
55975ef7389SDavid Ahern 	}
5601da177e4SLinus Torvalds 
56175ef7389SDavid Ahern 	if (oif && flags & RT6_LOOKUP_F_IFACE) {
56275ef7389SDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
5631cf844c7SDavid Ahern 		nh = res->f6i->fib6_nh;
5647d21fec9SDavid Ahern 		goto out;
56575ef7389SDavid Ahern 	}
56675ef7389SDavid Ahern 
567f88d8ea6SDavid Ahern 	if (unlikely(f6i->nh)) {
568f88d8ea6SDavid Ahern 		nh = nexthop_fib6_nh(f6i->nh);
569f88d8ea6SDavid Ahern 		if (nexthop_is_blackhole(f6i->nh))
570f88d8ea6SDavid Ahern 			goto out_blackhole;
571f88d8ea6SDavid Ahern 	} else {
5721cf844c7SDavid Ahern 		nh = f6i->fib6_nh;
573f88d8ea6SDavid Ahern 	}
574f88d8ea6SDavid Ahern 
5757d21fec9SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD) {
57675ef7389SDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
5771cf844c7SDavid Ahern 		nh = res->f6i->fib6_nh;
57875ef7389SDavid Ahern 	}
5797d21fec9SDavid Ahern out:
5807d21fec9SDavid Ahern 	res->nh = nh;
5817d21fec9SDavid Ahern 	res->fib6_type = res->f6i->fib6_type;
5827d21fec9SDavid Ahern 	res->fib6_flags = res->f6i->fib6_flags;
583f88d8ea6SDavid Ahern 	return;
584f88d8ea6SDavid Ahern 
585f88d8ea6SDavid Ahern out_blackhole:
586f88d8ea6SDavid Ahern 	res->fib6_flags |= RTF_REJECT;
587f88d8ea6SDavid Ahern 	res->fib6_type = RTN_BLACKHOLE;
588f88d8ea6SDavid Ahern 	res->nh = nh;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
59127097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
592c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
593c2f17e82SHannes Frederic Sowa 	struct work_struct work;
594c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
595c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
596c2f17e82SHannes Frederic Sowa };
597c2f17e82SHannes Frederic Sowa 
598c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
599c2f17e82SHannes Frederic Sowa {
600c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
601c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
602c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
603c2f17e82SHannes Frederic Sowa 
604c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
605adc176c5SErik Nordmark 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
606c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
607662f5533SMichael Büsch 	kfree(work);
608c2f17e82SHannes Frederic Sowa }
609c2f17e82SHannes Frederic Sowa 
610cc3a86c8SDavid Ahern static void rt6_probe(struct fib6_nh *fib6_nh)
61127097255SYOSHIFUJI Hideaki {
612f547fac6SSabrina Dubroca 	struct __rt6_probe_work *work = NULL;
6135e670d84SDavid Ahern 	const struct in6_addr *nh_gw;
6141bef4c22SEric Dumazet 	unsigned long last_probe;
615f2c31e32SEric Dumazet 	struct neighbour *neigh;
6165e670d84SDavid Ahern 	struct net_device *dev;
617f547fac6SSabrina Dubroca 	struct inet6_dev *idev;
6185e670d84SDavid Ahern 
61927097255SYOSHIFUJI Hideaki 	/*
62027097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
62127097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
62227097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
62327097255SYOSHIFUJI Hideaki 	 *
62427097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
62527097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
62627097255SYOSHIFUJI Hideaki 	 */
627004b3942SHangbin Liu 	if (!fib6_nh->fib_nh_gw_family)
628fdd6681dSAmerigo Wang 		return;
6295e670d84SDavid Ahern 
630cc3a86c8SDavid Ahern 	nh_gw = &fib6_nh->fib_nh_gw6;
631cc3a86c8SDavid Ahern 	dev = fib6_nh->fib_nh_dev;
6322152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
6331bef4c22SEric Dumazet 	last_probe = READ_ONCE(fib6_nh->last_probe);
634f547fac6SSabrina Dubroca 	idev = __in6_dev_get(dev);
6355e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
6362152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
6378d6c31bfSMartin KaFai Lau 		if (neigh->nud_state & NUD_VALID)
6388d6c31bfSMartin KaFai Lau 			goto out;
6398d6c31bfSMartin KaFai Lau 
6402152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
641990edb42SMartin KaFai Lau 		if (!(neigh->nud_state & NUD_VALID) &&
642990edb42SMartin KaFai Lau 		    time_after(jiffies,
643dcd1f572SDavid Ahern 			       neigh->updated + idev->cnf.rtr_probe_interval)) {
644c2f17e82SHannes Frederic Sowa 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
645990edb42SMartin KaFai Lau 			if (work)
6467e980569SJiri Benc 				__neigh_set_probe_once(neigh);
647990edb42SMartin KaFai Lau 		}
648c2f17e82SHannes Frederic Sowa 		write_unlock(&neigh->lock);
6491bef4c22SEric Dumazet 	} else if (time_after(jiffies, last_probe +
650f547fac6SSabrina Dubroca 				       idev->cnf.rtr_probe_interval)) {
651990edb42SMartin KaFai Lau 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
652990edb42SMartin KaFai Lau 	}
653c2f17e82SHannes Frederic Sowa 
6541bef4c22SEric Dumazet 	if (!work || cmpxchg(&fib6_nh->last_probe,
6551bef4c22SEric Dumazet 			     last_probe, jiffies) != last_probe) {
6561bef4c22SEric Dumazet 		kfree(work);
6571bef4c22SEric Dumazet 	} else {
658c2f17e82SHannes Frederic Sowa 		INIT_WORK(&work->work, rt6_probe_deferred);
6595e670d84SDavid Ahern 		work->target = *nh_gw;
6605e670d84SDavid Ahern 		dev_hold(dev);
6615e670d84SDavid Ahern 		work->dev = dev;
662c2f17e82SHannes Frederic Sowa 		schedule_work(&work->work);
663c2f17e82SHannes Frederic Sowa 	}
664990edb42SMartin KaFai Lau 
6658d6c31bfSMartin KaFai Lau out:
6662152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
667f2c31e32SEric Dumazet }
66827097255SYOSHIFUJI Hideaki #else
669cc3a86c8SDavid Ahern static inline void rt6_probe(struct fib6_nh *fib6_nh)
67027097255SYOSHIFUJI Hideaki {
67127097255SYOSHIFUJI Hideaki }
67227097255SYOSHIFUJI Hideaki #endif
67327097255SYOSHIFUJI Hideaki 
6741da177e4SLinus Torvalds /*
675554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
6761da177e4SLinus Torvalds  */
6771ba9a895SDavid Ahern static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
6781da177e4SLinus Torvalds {
679afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
6805e670d84SDavid Ahern 	struct neighbour *neigh;
681f2c31e32SEric Dumazet 
682145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
6831ba9a895SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
6841ba9a895SDavid Ahern 					  &fib6_nh->fib_nh_gw6);
685145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
686145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
687554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
688afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
689398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
690a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
691afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
6927e980569SJiri Benc 		else
6937e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
694398bcbebSYOSHIFUJI Hideaki #endif
695145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
696afc154e9SHannes Frederic Sowa 	} else {
697afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
6987e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
699a5a81f0bSPaul Marks 	}
700145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
701145a3621SYOSHIFUJI Hideaki / 吉藤英明 
702a5a81f0bSPaul Marks 	return ret;
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
705702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
706702cea56SDavid Ahern 			   int strict)
707554cfb7eSYOSHIFUJI Hideaki {
7086e1809a5SDavid Ahern 	int m = 0;
7094d0c5911SYOSHIFUJI Hideaki 
7106e1809a5SDavid Ahern 	if (!oif || nh->fib_nh_dev->ifindex == oif)
7116e1809a5SDavid Ahern 		m = 2;
7126e1809a5SDavid Ahern 
71377d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
714afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
715ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
716702cea56SDavid Ahern 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
717ebacaaa0SYOSHIFUJI Hideaki #endif
7181ba9a895SDavid Ahern 	if ((strict & RT6_LOOKUP_F_REACHABLE) &&
719702cea56SDavid Ahern 	    !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
7201ba9a895SDavid Ahern 		int n = rt6_check_neigh(nh);
721afc154e9SHannes Frederic Sowa 		if (n < 0)
722afc154e9SHannes Frederic Sowa 			return n;
723afc154e9SHannes Frederic Sowa 	}
724554cfb7eSYOSHIFUJI Hideaki 	return m;
725554cfb7eSYOSHIFUJI Hideaki }
726554cfb7eSYOSHIFUJI Hideaki 
72728679ed1SDavid Ahern static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
72828679ed1SDavid Ahern 		       int oif, int strict, int *mpri, bool *do_rr)
729554cfb7eSYOSHIFUJI Hideaki {
730afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
73128679ed1SDavid Ahern 	bool rc = false;
73228679ed1SDavid Ahern 	int m;
73335103d11SAndy Gospodarek 
73428679ed1SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD)
7358067bb8cSIdo Schimmel 		goto out;
7368067bb8cSIdo Schimmel 
73728679ed1SDavid Ahern 	if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
73828679ed1SDavid Ahern 	    nh->fib_nh_flags & RTNH_F_LINKDOWN &&
739d5d32e4bSDavid Ahern 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
74035103d11SAndy Gospodarek 		goto out;
741554cfb7eSYOSHIFUJI Hideaki 
74228679ed1SDavid Ahern 	m = rt6_score_route(nh, fib6_flags, oif, strict);
7437e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
744afc154e9SHannes Frederic Sowa 		match_do_rr = true;
745afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
7467e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
747f11e6659SDavid S. Miller 		goto out;
7481da177e4SLinus Torvalds 	}
749f11e6659SDavid S. Miller 
750afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
75128679ed1SDavid Ahern 		rt6_probe(nh);
752afc154e9SHannes Frederic Sowa 
7537e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
754afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
755afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
756afc154e9SHannes Frederic Sowa 		*mpri = m;
75728679ed1SDavid Ahern 		rc = true;
758afc154e9SHannes Frederic Sowa 	}
759f11e6659SDavid S. Miller out:
76028679ed1SDavid Ahern 	return rc;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
76317a5984eSDavid Ahern struct fib6_nh_frl_arg {
76417a5984eSDavid Ahern 	u32		flags;
76517a5984eSDavid Ahern 	int		oif;
76617a5984eSDavid Ahern 	int		strict;
76717a5984eSDavid Ahern 	int		*mpri;
76817a5984eSDavid Ahern 	bool		*do_rr;
76917a5984eSDavid Ahern 	struct fib6_nh	*nh;
77017a5984eSDavid Ahern };
77117a5984eSDavid Ahern 
77217a5984eSDavid Ahern static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
77317a5984eSDavid Ahern {
77417a5984eSDavid Ahern 	struct fib6_nh_frl_arg *arg = _arg;
77517a5984eSDavid Ahern 
77617a5984eSDavid Ahern 	arg->nh = nh;
77717a5984eSDavid Ahern 	return find_match(nh, arg->flags, arg->oif, arg->strict,
77817a5984eSDavid Ahern 			  arg->mpri, arg->do_rr);
77917a5984eSDavid Ahern }
78017a5984eSDavid Ahern 
781b7bc4b6aSDavid Ahern static void __find_rr_leaf(struct fib6_info *f6i_start,
78230c15f03SDavid Ahern 			   struct fib6_info *nomatch, u32 metric,
783b7bc4b6aSDavid Ahern 			   struct fib6_result *res, struct fib6_info **cont,
78430c15f03SDavid Ahern 			   int oif, int strict, bool *do_rr, int *mpri)
78530c15f03SDavid Ahern {
786b7bc4b6aSDavid Ahern 	struct fib6_info *f6i;
78730c15f03SDavid Ahern 
788b7bc4b6aSDavid Ahern 	for (f6i = f6i_start;
789b7bc4b6aSDavid Ahern 	     f6i && f6i != nomatch;
790b7bc4b6aSDavid Ahern 	     f6i = rcu_dereference(f6i->fib6_next)) {
79117a5984eSDavid Ahern 		bool matched = false;
79230c15f03SDavid Ahern 		struct fib6_nh *nh;
79330c15f03SDavid Ahern 
794b7bc4b6aSDavid Ahern 		if (cont && f6i->fib6_metric != metric) {
795b7bc4b6aSDavid Ahern 			*cont = f6i;
79630c15f03SDavid Ahern 			return;
79730c15f03SDavid Ahern 		}
79830c15f03SDavid Ahern 
799b7bc4b6aSDavid Ahern 		if (fib6_check_expired(f6i))
80030c15f03SDavid Ahern 			continue;
80130c15f03SDavid Ahern 
80217a5984eSDavid Ahern 		if (unlikely(f6i->nh)) {
80317a5984eSDavid Ahern 			struct fib6_nh_frl_arg arg = {
80417a5984eSDavid Ahern 				.flags  = f6i->fib6_flags,
80517a5984eSDavid Ahern 				.oif    = oif,
80617a5984eSDavid Ahern 				.strict = strict,
80717a5984eSDavid Ahern 				.mpri   = mpri,
80817a5984eSDavid Ahern 				.do_rr  = do_rr
80917a5984eSDavid Ahern 			};
81017a5984eSDavid Ahern 
81117a5984eSDavid Ahern 			if (nexthop_is_blackhole(f6i->nh)) {
81217a5984eSDavid Ahern 				res->fib6_flags = RTF_REJECT;
81317a5984eSDavid Ahern 				res->fib6_type = RTN_BLACKHOLE;
81417a5984eSDavid Ahern 				res->f6i = f6i;
81517a5984eSDavid Ahern 				res->nh = nexthop_fib6_nh(f6i->nh);
81617a5984eSDavid Ahern 				return;
81717a5984eSDavid Ahern 			}
81817a5984eSDavid Ahern 			if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
81917a5984eSDavid Ahern 						     &arg)) {
82017a5984eSDavid Ahern 				matched = true;
82117a5984eSDavid Ahern 				nh = arg.nh;
82217a5984eSDavid Ahern 			}
82317a5984eSDavid Ahern 		} else {
8241cf844c7SDavid Ahern 			nh = f6i->fib6_nh;
82517a5984eSDavid Ahern 			if (find_match(nh, f6i->fib6_flags, oif, strict,
82617a5984eSDavid Ahern 				       mpri, do_rr))
82717a5984eSDavid Ahern 				matched = true;
82817a5984eSDavid Ahern 		}
82917a5984eSDavid Ahern 		if (matched) {
830b7bc4b6aSDavid Ahern 			res->f6i = f6i;
831b7bc4b6aSDavid Ahern 			res->nh = nh;
8327d21fec9SDavid Ahern 			res->fib6_flags = f6i->fib6_flags;
8337d21fec9SDavid Ahern 			res->fib6_type = f6i->fib6_type;
834b7bc4b6aSDavid Ahern 		}
83530c15f03SDavid Ahern 	}
83630c15f03SDavid Ahern }
83730c15f03SDavid Ahern 
838b7bc4b6aSDavid Ahern static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
839b7bc4b6aSDavid Ahern 			 struct fib6_info *rr_head, int oif, int strict,
840b7bc4b6aSDavid Ahern 			 bool *do_rr, struct fib6_result *res)
841f11e6659SDavid S. Miller {
842b7bc4b6aSDavid Ahern 	u32 metric = rr_head->fib6_metric;
843b7bc4b6aSDavid Ahern 	struct fib6_info *cont = NULL;
844f11e6659SDavid S. Miller 	int mpri = -1;
845f11e6659SDavid S. Miller 
846b7bc4b6aSDavid Ahern 	__find_rr_leaf(rr_head, NULL, metric, res, &cont,
84730c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
8489fbdcfafSSteffen Klassert 
849b7bc4b6aSDavid Ahern 	__find_rr_leaf(leaf, rr_head, metric, res, &cont,
85030c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
8519fbdcfafSSteffen Klassert 
852b7bc4b6aSDavid Ahern 	if (res->f6i || !cont)
853b7bc4b6aSDavid Ahern 		return;
8549fbdcfafSSteffen Klassert 
855b7bc4b6aSDavid Ahern 	__find_rr_leaf(cont, NULL, metric, res, NULL,
85630c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
857f11e6659SDavid S. Miller }
858f11e6659SDavid S. Miller 
859b7bc4b6aSDavid Ahern static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
860b7bc4b6aSDavid Ahern 		       struct fib6_result *res, int strict)
861f11e6659SDavid S. Miller {
8628d1c802bSDavid Ahern 	struct fib6_info *leaf = rcu_dereference(fn->leaf);
863b7bc4b6aSDavid Ahern 	struct fib6_info *rt0;
864afc154e9SHannes Frederic Sowa 	bool do_rr = false;
86517ecf590SWei Wang 	int key_plen;
866f11e6659SDavid S. Miller 
867b7bc4b6aSDavid Ahern 	/* make sure this function or its helpers sets f6i */
868b7bc4b6aSDavid Ahern 	res->f6i = NULL;
869b7bc4b6aSDavid Ahern 
870421842edSDavid Ahern 	if (!leaf || leaf == net->ipv6.fib6_null_entry)
871b7bc4b6aSDavid Ahern 		goto out;
8728d1040e8SWei Wang 
87366f5d6ceSWei Wang 	rt0 = rcu_dereference(fn->rr_ptr);
874f11e6659SDavid S. Miller 	if (!rt0)
87566f5d6ceSWei Wang 		rt0 = leaf;
876f11e6659SDavid S. Miller 
87717ecf590SWei Wang 	/* Double check to make sure fn is not an intermediate node
87817ecf590SWei Wang 	 * and fn->leaf does not points to its child's leaf
87917ecf590SWei Wang 	 * (This might happen if all routes under fn are deleted from
88017ecf590SWei Wang 	 * the tree and fib6_repair_tree() is called on the node.)
88117ecf590SWei Wang 	 */
88293c2fb25SDavid Ahern 	key_plen = rt0->fib6_dst.plen;
88317ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES
88493c2fb25SDavid Ahern 	if (rt0->fib6_src.plen)
88593c2fb25SDavid Ahern 		key_plen = rt0->fib6_src.plen;
88617ecf590SWei Wang #endif
88717ecf590SWei Wang 	if (fn->fn_bit != key_plen)
888b7bc4b6aSDavid Ahern 		goto out;
88917ecf590SWei Wang 
890b7bc4b6aSDavid Ahern 	find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
891afc154e9SHannes Frederic Sowa 	if (do_rr) {
8928fb11a9aSDavid Ahern 		struct fib6_info *next = rcu_dereference(rt0->fib6_next);
893f11e6659SDavid S. Miller 
894554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
89593c2fb25SDavid Ahern 		if (!next || next->fib6_metric != rt0->fib6_metric)
8968d1040e8SWei Wang 			next = leaf;
897f11e6659SDavid S. Miller 
89866f5d6ceSWei Wang 		if (next != rt0) {
89993c2fb25SDavid Ahern 			spin_lock_bh(&leaf->fib6_table->tb6_lock);
90066f5d6ceSWei Wang 			/* make sure next is not being deleted from the tree */
90193c2fb25SDavid Ahern 			if (next->fib6_node)
90266f5d6ceSWei Wang 				rcu_assign_pointer(fn->rr_ptr, next);
90393c2fb25SDavid Ahern 			spin_unlock_bh(&leaf->fib6_table->tb6_lock);
90466f5d6ceSWei Wang 		}
905554cfb7eSYOSHIFUJI Hideaki 	}
906554cfb7eSYOSHIFUJI Hideaki 
907b7bc4b6aSDavid Ahern out:
908b7bc4b6aSDavid Ahern 	if (!res->f6i) {
909b7bc4b6aSDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
9101cf844c7SDavid Ahern 		res->nh = res->f6i->fib6_nh;
9117d21fec9SDavid Ahern 		res->fib6_flags = res->f6i->fib6_flags;
9127d21fec9SDavid Ahern 		res->fib6_type = res->f6i->fib6_type;
913b7bc4b6aSDavid Ahern 	}
9141da177e4SLinus Torvalds }
9151da177e4SLinus Torvalds 
91685bd05deSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
9178b9df265SMartin KaFai Lau {
91885bd05deSDavid Ahern 	return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
91985bd05deSDavid Ahern 	       res->nh->fib_nh_gw_family;
9208b9df265SMartin KaFai Lau }
9218b9df265SMartin KaFai Lau 
92270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
92370ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
924b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
92570ceb4f5SYOSHIFUJI Hideaki {
926c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
92770ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
92870ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
92970ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
9304bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
9318d1c802bSDavid Ahern 	struct fib6_info *rt;
93270ceb4f5SYOSHIFUJI Hideaki 
93370ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
93470ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
93570ceb4f5SYOSHIFUJI Hideaki 	}
93670ceb4f5SYOSHIFUJI Hideaki 
93770ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
93870ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
93970ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
94070ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
94170ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
94270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
94370ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
94470ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
94570ceb4f5SYOSHIFUJI Hideaki 		}
94670ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
94770ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
94870ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
94970ceb4f5SYOSHIFUJI Hideaki 		}
95070ceb4f5SYOSHIFUJI Hideaki 	}
95170ceb4f5SYOSHIFUJI Hideaki 
95270ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
95370ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
9543933fc95SJens Rosenboom 		return -EINVAL;
95570ceb4f5SYOSHIFUJI Hideaki 
9564bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
95770ceb4f5SYOSHIFUJI Hideaki 
95870ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
95970ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
96070ceb4f5SYOSHIFUJI Hideaki 	else {
96170ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
96270ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
96370ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
96470ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
96570ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
96670ceb4f5SYOSHIFUJI Hideaki 	}
96770ceb4f5SYOSHIFUJI Hideaki 
968f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
969afb1d4b5SDavid Ahern 		rt = rt6_get_dflt_router(net, gwaddr, dev);
970f104a567SDuan Jiong 	else
971f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
972830218c1SDavid Ahern 					gwaddr, dev);
97370ceb4f5SYOSHIFUJI Hideaki 
97470ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
97511dd74b3SRoopa Prabhu 		ip6_del_rt(net, rt, false);
97670ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
97770ceb4f5SYOSHIFUJI Hideaki 	}
97870ceb4f5SYOSHIFUJI Hideaki 
97970ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
980830218c1SDavid Ahern 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
981830218c1SDavid Ahern 					dev, pref);
98270ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
98393c2fb25SDavid Ahern 		rt->fib6_flags = RTF_ROUTEINFO |
98493c2fb25SDavid Ahern 				 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
98570ceb4f5SYOSHIFUJI Hideaki 
98670ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
9871716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
98814895687SDavid Ahern 			fib6_clean_expires(rt);
9891716a961SGao feng 		else
99014895687SDavid Ahern 			fib6_set_expires(rt, jiffies + HZ * lifetime);
9911716a961SGao feng 
99293531c67SDavid Ahern 		fib6_info_release(rt);
99370ceb4f5SYOSHIFUJI Hideaki 	}
99470ceb4f5SYOSHIFUJI Hideaki 	return 0;
99570ceb4f5SYOSHIFUJI Hideaki }
99670ceb4f5SYOSHIFUJI Hideaki #endif
99770ceb4f5SYOSHIFUJI Hideaki 
998ae90d867SDavid Ahern /*
999ae90d867SDavid Ahern  *	Misc support functions
1000ae90d867SDavid Ahern  */
1001ae90d867SDavid Ahern 
1002ae90d867SDavid Ahern /* called with rcu_lock held */
10030d161581SDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
1004ae90d867SDavid Ahern {
10050d161581SDavid Ahern 	struct net_device *dev = res->nh->fib_nh_dev;
1006ae90d867SDavid Ahern 
10077d21fec9SDavid Ahern 	if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
1008ae90d867SDavid Ahern 		/* for copies of local routes, dst->dev needs to be the
1009ae90d867SDavid Ahern 		 * device if it is a master device, the master device if
1010ae90d867SDavid Ahern 		 * device is enslaved, and the loopback as the default
1011ae90d867SDavid Ahern 		 */
1012ae90d867SDavid Ahern 		if (netif_is_l3_slave(dev) &&
10137d21fec9SDavid Ahern 		    !rt6_need_strict(&res->f6i->fib6_dst.addr))
1014ae90d867SDavid Ahern 			dev = l3mdev_master_dev_rcu(dev);
1015ae90d867SDavid Ahern 		else if (!netif_is_l3_master(dev))
1016ae90d867SDavid Ahern 			dev = dev_net(dev)->loopback_dev;
1017ae90d867SDavid Ahern 		/* last case is netif_is_l3_master(dev) is true in which
1018ae90d867SDavid Ahern 		 * case we want dev returned to be dev
1019ae90d867SDavid Ahern 		 */
1020ae90d867SDavid Ahern 	}
1021ae90d867SDavid Ahern 
1022ae90d867SDavid Ahern 	return dev;
1023ae90d867SDavid Ahern }
1024ae90d867SDavid Ahern 
10256edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = {
10266edb3c96SDavid Ahern 	[RTN_UNSPEC]	= 0,
10276edb3c96SDavid Ahern 	[RTN_UNICAST]	= 0,
10286edb3c96SDavid Ahern 	[RTN_LOCAL]	= 0,
10296edb3c96SDavid Ahern 	[RTN_BROADCAST]	= 0,
10306edb3c96SDavid Ahern 	[RTN_ANYCAST]	= 0,
10316edb3c96SDavid Ahern 	[RTN_MULTICAST]	= 0,
10326edb3c96SDavid Ahern 	[RTN_BLACKHOLE]	= -EINVAL,
10336edb3c96SDavid Ahern 	[RTN_UNREACHABLE] = -EHOSTUNREACH,
10346edb3c96SDavid Ahern 	[RTN_PROHIBIT]	= -EACCES,
10356edb3c96SDavid Ahern 	[RTN_THROW]	= -EAGAIN,
10366edb3c96SDavid Ahern 	[RTN_NAT]	= -EINVAL,
10376edb3c96SDavid Ahern 	[RTN_XRESOLVE]	= -EINVAL,
10386edb3c96SDavid Ahern };
10396edb3c96SDavid Ahern 
10406edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type)
10416edb3c96SDavid Ahern {
10426edb3c96SDavid Ahern 	return fib6_prop[fib6_type];
10436edb3c96SDavid Ahern }
10446edb3c96SDavid Ahern 
10458d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
10463b6761d1SDavid Ahern {
10473b6761d1SDavid Ahern 	unsigned short flags = 0;
10483b6761d1SDavid Ahern 
10493b6761d1SDavid Ahern 	if (rt->dst_nocount)
10503b6761d1SDavid Ahern 		flags |= DST_NOCOUNT;
10513b6761d1SDavid Ahern 	if (rt->dst_nopolicy)
10523b6761d1SDavid Ahern 		flags |= DST_NOPOLICY;
10533b6761d1SDavid Ahern 
10543b6761d1SDavid Ahern 	return flags;
10553b6761d1SDavid Ahern }
10563b6761d1SDavid Ahern 
10577d21fec9SDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
10586edb3c96SDavid Ahern {
10597d21fec9SDavid Ahern 	rt->dst.error = ip6_rt_type_to_error(fib6_type);
10606edb3c96SDavid Ahern 
10617d21fec9SDavid Ahern 	switch (fib6_type) {
10626edb3c96SDavid Ahern 	case RTN_BLACKHOLE:
10636edb3c96SDavid Ahern 		rt->dst.output = dst_discard_out;
10646edb3c96SDavid Ahern 		rt->dst.input = dst_discard;
10656edb3c96SDavid Ahern 		break;
10666edb3c96SDavid Ahern 	case RTN_PROHIBIT:
10676edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_prohibit_out;
10686edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_prohibit;
10696edb3c96SDavid Ahern 		break;
10706edb3c96SDavid Ahern 	case RTN_THROW:
10716edb3c96SDavid Ahern 	case RTN_UNREACHABLE:
10726edb3c96SDavid Ahern 	default:
10736edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_discard_out;
10746edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_discard;
10756edb3c96SDavid Ahern 		break;
10766edb3c96SDavid Ahern 	}
10776edb3c96SDavid Ahern }
10786edb3c96SDavid Ahern 
10790d161581SDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
10806edb3c96SDavid Ahern {
10817d21fec9SDavid Ahern 	struct fib6_info *f6i = res->f6i;
10820d161581SDavid Ahern 
10837d21fec9SDavid Ahern 	if (res->fib6_flags & RTF_REJECT) {
10847d21fec9SDavid Ahern 		ip6_rt_init_dst_reject(rt, res->fib6_type);
10856edb3c96SDavid Ahern 		return;
10866edb3c96SDavid Ahern 	}
10876edb3c96SDavid Ahern 
10886edb3c96SDavid Ahern 	rt->dst.error = 0;
10896edb3c96SDavid Ahern 	rt->dst.output = ip6_output;
10906edb3c96SDavid Ahern 
10917d21fec9SDavid Ahern 	if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
10926edb3c96SDavid Ahern 		rt->dst.input = ip6_input;
10937d21fec9SDavid Ahern 	} else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
10946edb3c96SDavid Ahern 		rt->dst.input = ip6_mc_input;
10956edb3c96SDavid Ahern 	} else {
10966edb3c96SDavid Ahern 		rt->dst.input = ip6_forward;
10976edb3c96SDavid Ahern 	}
10986edb3c96SDavid Ahern 
10990d161581SDavid Ahern 	if (res->nh->fib_nh_lws) {
11000d161581SDavid Ahern 		rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
11016edb3c96SDavid Ahern 		lwtunnel_set_redirect(&rt->dst);
11026edb3c96SDavid Ahern 	}
11036edb3c96SDavid Ahern 
11046edb3c96SDavid Ahern 	rt->dst.lastuse = jiffies;
11056edb3c96SDavid Ahern }
11066edb3c96SDavid Ahern 
1107e873e4b9SWei Wang /* Caller must already hold reference to @from */
11088d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1109ae90d867SDavid Ahern {
1110ae90d867SDavid Ahern 	rt->rt6i_flags &= ~RTF_EXPIRES;
1111a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, from);
1112e1255ed4SDavid Ahern 	ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
1113ae90d867SDavid Ahern }
1114ae90d867SDavid Ahern 
11150d161581SDavid Ahern /* Caller must already hold reference to f6i in result */
11160d161581SDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
1117ae90d867SDavid Ahern {
11180d161581SDavid Ahern 	const struct fib6_nh *nh = res->nh;
11190d161581SDavid Ahern 	const struct net_device *dev = nh->fib_nh_dev;
11200d161581SDavid Ahern 	struct fib6_info *f6i = res->f6i;
1121dcd1f572SDavid Ahern 
11220d161581SDavid Ahern 	ip6_rt_init_dst(rt, res);
11236edb3c96SDavid Ahern 
11240d161581SDavid Ahern 	rt->rt6i_dst = f6i->fib6_dst;
1125dcd1f572SDavid Ahern 	rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
11267d21fec9SDavid Ahern 	rt->rt6i_flags = res->fib6_flags;
11270d161581SDavid Ahern 	if (nh->fib_nh_gw_family) {
11280d161581SDavid Ahern 		rt->rt6i_gateway = nh->fib_nh_gw6;
11292b2450caSDavid Ahern 		rt->rt6i_flags |= RTF_GATEWAY;
11302b2450caSDavid Ahern 	}
11310d161581SDavid Ahern 	rt6_set_from(rt, f6i);
1132ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
11330d161581SDavid Ahern 	rt->rt6i_src = f6i->fib6_src;
1134ae90d867SDavid Ahern #endif
1135ae90d867SDavid Ahern }
1136ae90d867SDavid Ahern 
1137a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1138a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
1139a3c00e46SMartin KaFai Lau {
114066f5d6ceSWei Wang 	struct fib6_node *pn, *sn;
1141a3c00e46SMartin KaFai Lau 	while (1) {
1142a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
1143a3c00e46SMartin KaFai Lau 			return NULL;
114466f5d6ceSWei Wang 		pn = rcu_dereference(fn->parent);
114566f5d6ceSWei Wang 		sn = FIB6_SUBTREE(pn);
114666f5d6ceSWei Wang 		if (sn && sn != fn)
11476454743bSDavid Ahern 			fn = fib6_node_lookup(sn, NULL, saddr);
1148a3c00e46SMartin KaFai Lau 		else
1149a3c00e46SMartin KaFai Lau 			fn = pn;
1150a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
1151a3c00e46SMartin KaFai Lau 			return fn;
1152a3c00e46SMartin KaFai Lau 	}
1153a3c00e46SMartin KaFai Lau }
1154c71099acSThomas Graf 
115510585b43SDavid Ahern static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1156d3843fe5SWei Wang {
1157d3843fe5SWei Wang 	struct rt6_info *rt = *prt;
1158d3843fe5SWei Wang 
1159d3843fe5SWei Wang 	if (dst_hold_safe(&rt->dst))
1160d3843fe5SWei Wang 		return true;
116110585b43SDavid Ahern 	if (net) {
1162d3843fe5SWei Wang 		rt = net->ipv6.ip6_null_entry;
1163d3843fe5SWei Wang 		dst_hold(&rt->dst);
1164d3843fe5SWei Wang 	} else {
1165d3843fe5SWei Wang 		rt = NULL;
1166d3843fe5SWei Wang 	}
1167d3843fe5SWei Wang 	*prt = rt;
1168d3843fe5SWei Wang 	return false;
1169d3843fe5SWei Wang }
1170d3843fe5SWei Wang 
1171dec9b0e2SDavid Ahern /* called with rcu_lock held */
11729b6b35abSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
1173dec9b0e2SDavid Ahern {
11749b6b35abSDavid Ahern 	struct net_device *dev = res->nh->fib_nh_dev;
11759b6b35abSDavid Ahern 	struct fib6_info *f6i = res->f6i;
11769b6b35abSDavid Ahern 	unsigned short flags;
1177dec9b0e2SDavid Ahern 	struct rt6_info *nrt;
1178dec9b0e2SDavid Ahern 
11799b6b35abSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
11801c87e79aSXin Long 		goto fallback;
1181e873e4b9SWei Wang 
11829b6b35abSDavid Ahern 	flags = fib6_info_dst_flags(f6i);
118393531c67SDavid Ahern 	nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
11841c87e79aSXin Long 	if (!nrt) {
11859b6b35abSDavid Ahern 		fib6_info_release(f6i);
11861c87e79aSXin Long 		goto fallback;
11871c87e79aSXin Long 	}
1188dec9b0e2SDavid Ahern 
11890d161581SDavid Ahern 	ip6_rt_copy_init(nrt, res);
11901c87e79aSXin Long 	return nrt;
11911c87e79aSXin Long 
11921c87e79aSXin Long fallback:
11931c87e79aSXin Long 	nrt = dev_net(dev)->ipv6.ip6_null_entry;
11941c87e79aSXin Long 	dst_hold(&nrt->dst);
1195dec9b0e2SDavid Ahern 	return nrt;
1196dec9b0e2SDavid Ahern }
1197dec9b0e2SDavid Ahern 
119855cced4fSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
11998ed67789SDaniel Lezcano 					     struct fib6_table *table,
1200b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
1201b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
1202b75cc8f9SDavid Ahern 					     int flags)
12031da177e4SLinus Torvalds {
1204b1d40991SDavid Ahern 	struct fib6_result res = {};
12051da177e4SLinus Torvalds 	struct fib6_node *fn;
120623fb93a4SDavid Ahern 	struct rt6_info *rt;
12071da177e4SLinus Torvalds 
1208b6cdbc85SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1209b6cdbc85SDavid Ahern 		flags &= ~RT6_LOOKUP_F_IFACE;
1210b6cdbc85SDavid Ahern 
121166f5d6ceSWei Wang 	rcu_read_lock();
12126454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1213c71099acSThomas Graf restart:
1214b1d40991SDavid Ahern 	res.f6i = rcu_dereference(fn->leaf);
1215b1d40991SDavid Ahern 	if (!res.f6i)
1216b1d40991SDavid Ahern 		res.f6i = net->ipv6.fib6_null_entry;
1217af52a52cSDavid Ahern 	else
121875ef7389SDavid Ahern 		rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
121975ef7389SDavid Ahern 				 flags);
1220af52a52cSDavid Ahern 
1221b1d40991SDavid Ahern 	if (res.f6i == net->ipv6.fib6_null_entry) {
1222a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1223a3c00e46SMartin KaFai Lau 		if (fn)
1224a3c00e46SMartin KaFai Lau 			goto restart;
1225af52a52cSDavid Ahern 
1226af52a52cSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
1227af52a52cSDavid Ahern 		dst_hold(&rt->dst);
1228af52a52cSDavid Ahern 		goto out;
1229f88d8ea6SDavid Ahern 	} else if (res.fib6_flags & RTF_REJECT) {
1230f88d8ea6SDavid Ahern 		goto do_create;
1231a3c00e46SMartin KaFai Lau 	}
12322b760fcfSWei Wang 
1233b1d40991SDavid Ahern 	fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1234b1d40991SDavid Ahern 			 fl6->flowi6_oif != 0, skb, flags);
1235b1d40991SDavid Ahern 
12364c9483b2SDavid S. Miller 	/* Search through exception table */
12377e4b5128SDavid Ahern 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
123823fb93a4SDavid Ahern 	if (rt) {
123910585b43SDavid Ahern 		if (ip6_hold_safe(net, &rt))
1240d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
124123fb93a4SDavid Ahern 	} else {
1242f88d8ea6SDavid Ahern do_create:
12439b6b35abSDavid Ahern 		rt = ip6_create_rt_rcu(&res);
1244dec9b0e2SDavid Ahern 	}
1245d3843fe5SWei Wang 
1246af52a52cSDavid Ahern out:
12478ff2e5b2SDavid Ahern 	trace_fib6_table_lookup(net, &res, table, fl6);
1248af52a52cSDavid Ahern 
124966f5d6ceSWei Wang 	rcu_read_unlock();
1250b811580dSDavid Ahern 
12511da177e4SLinus Torvalds 	return rt;
1252c71099acSThomas Graf }
1253c71099acSThomas Graf 
1254ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1255b75cc8f9SDavid Ahern 				   const struct sk_buff *skb, int flags)
1256ea6e574eSFlorian Westphal {
1257b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1258ea6e574eSFlorian Westphal }
1259ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
1260ea6e574eSFlorian Westphal 
12619acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1262b75cc8f9SDavid Ahern 			    const struct in6_addr *saddr, int oif,
1263b75cc8f9SDavid Ahern 			    const struct sk_buff *skb, int strict)
1264c71099acSThomas Graf {
12654c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
12664c9483b2SDavid S. Miller 		.flowi6_oif = oif,
12674c9483b2SDavid S. Miller 		.daddr = *daddr,
1268c71099acSThomas Graf 	};
1269c71099acSThomas Graf 	struct dst_entry *dst;
127077d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1271c71099acSThomas Graf 
1272adaa70bbSThomas Graf 	if (saddr) {
12734c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1274adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1275adaa70bbSThomas Graf 	}
1276adaa70bbSThomas Graf 
1277b75cc8f9SDavid Ahern 	dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1278c71099acSThomas Graf 	if (dst->error == 0)
1279c71099acSThomas Graf 		return (struct rt6_info *) dst;
1280c71099acSThomas Graf 
1281c71099acSThomas Graf 	dst_release(dst);
1282c71099acSThomas Graf 
12831da177e4SLinus Torvalds 	return NULL;
12841da177e4SLinus Torvalds }
12857159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
12867159039aSYOSHIFUJI Hideaki 
1287c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
12881cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
12891cfb71eeSWei Wang  * route is released.
12901cfb71eeSWei Wang  * Caller must hold dst before calling it.
12911da177e4SLinus Torvalds  */
12921da177e4SLinus Torvalds 
12938d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1294333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
12951da177e4SLinus Torvalds {
12961da177e4SLinus Torvalds 	int err;
1297c71099acSThomas Graf 	struct fib6_table *table;
12981da177e4SLinus Torvalds 
129993c2fb25SDavid Ahern 	table = rt->fib6_table;
130066f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
1301d4ead6b3SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, extack);
130266f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	return err;
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds 
13078d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt)
130840e22e8fSThomas Graf {
1309afb1d4b5SDavid Ahern 	struct nl_info info = {	.nl_net = net, };
1310e715b6d3SFlorian Westphal 
1311d4ead6b3SDavid Ahern 	return __ip6_ins_rt(rt, &info, NULL);
131240e22e8fSThomas Graf }
131340e22e8fSThomas Graf 
131485bd05deSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
131521efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
1316b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
13171da177e4SLinus Torvalds {
131885bd05deSDavid Ahern 	struct fib6_info *f6i = res->f6i;
13194832c30dSDavid Ahern 	struct net_device *dev;
13201da177e4SLinus Torvalds 	struct rt6_info *rt;
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	/*
13231da177e4SLinus Torvalds 	 *	Clone the route.
13241da177e4SLinus Torvalds 	 */
13251da177e4SLinus Torvalds 
132685bd05deSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
1327e873e4b9SWei Wang 		return NULL;
1328e873e4b9SWei Wang 
13290d161581SDavid Ahern 	dev = ip6_rt_get_dev_rcu(res);
133093531c67SDavid Ahern 	rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1331e873e4b9SWei Wang 	if (!rt) {
133285bd05deSDavid Ahern 		fib6_info_release(f6i);
133383a09abdSMartin KaFai Lau 		return NULL;
1334e873e4b9SWei Wang 	}
133583a09abdSMartin KaFai Lau 
13360d161581SDavid Ahern 	ip6_rt_copy_init(rt, res);
13378b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
133883a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
133983a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
13408b9df265SMartin KaFai Lau 
134185bd05deSDavid Ahern 	if (!rt6_is_gw_or_nonexthop(res)) {
134285bd05deSDavid Ahern 		if (f6i->fib6_dst.plen != 128 &&
134385bd05deSDavid Ahern 		    ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
134458c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
13451da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
13461da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
13474e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
13481da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
13491da177e4SLinus Torvalds 		}
13501da177e4SLinus Torvalds #endif
135195a9a5baSYOSHIFUJI Hideaki 	}
135295a9a5baSYOSHIFUJI Hideaki 
1353299d9939SYOSHIFUJI Hideaki 	return rt;
1354299d9939SYOSHIFUJI Hideaki }
1355299d9939SYOSHIFUJI Hideaki 
1356db3fedeeSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
1357d52d3997SMartin KaFai Lau {
1358db3fedeeSDavid Ahern 	struct fib6_info *f6i = res->f6i;
1359db3fedeeSDavid Ahern 	unsigned short flags = fib6_info_dst_flags(f6i);
13604832c30dSDavid Ahern 	struct net_device *dev;
1361d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1362d52d3997SMartin KaFai Lau 
1363db3fedeeSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
1364e873e4b9SWei Wang 		return NULL;
1365e873e4b9SWei Wang 
13664832c30dSDavid Ahern 	rcu_read_lock();
13670d161581SDavid Ahern 	dev = ip6_rt_get_dev_rcu(res);
1368d8882935SEric Dumazet 	pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
13694832c30dSDavid Ahern 	rcu_read_unlock();
1370e873e4b9SWei Wang 	if (!pcpu_rt) {
1371db3fedeeSDavid Ahern 		fib6_info_release(f6i);
1372d52d3997SMartin KaFai Lau 		return NULL;
1373e873e4b9SWei Wang 	}
13740d161581SDavid Ahern 	ip6_rt_copy_init(pcpu_rt, res);
1375d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
13768f34e53bSDavid Ahern 
13778f34e53bSDavid Ahern 	if (f6i->nh)
13788f34e53bSDavid Ahern 		pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
13798f34e53bSDavid Ahern 
1380d52d3997SMartin KaFai Lau 	return pcpu_rt;
1381d52d3997SMartin KaFai Lau }
1382d52d3997SMartin KaFai Lau 
13838f34e53bSDavid Ahern static bool rt6_is_valid(const struct rt6_info *rt6)
13848f34e53bSDavid Ahern {
13858f34e53bSDavid Ahern 	return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
13868f34e53bSDavid Ahern }
13878f34e53bSDavid Ahern 
138866f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */
1389db3fedeeSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
1390d52d3997SMartin KaFai Lau {
1391c353071aSEric Dumazet 	struct rt6_info *pcpu_rt;
1392d52d3997SMartin KaFai Lau 
1393c353071aSEric Dumazet 	pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
1394d52d3997SMartin KaFai Lau 
13958f34e53bSDavid Ahern 	if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
13968f34e53bSDavid Ahern 		struct rt6_info *prev, **p;
13978f34e53bSDavid Ahern 
13988f34e53bSDavid Ahern 		p = this_cpu_ptr(res->nh->rt6i_pcpu);
13998f34e53bSDavid Ahern 		prev = xchg(p, NULL);
14008f34e53bSDavid Ahern 		if (prev) {
14018f34e53bSDavid Ahern 			dst_dev_put(&prev->dst);
14028f34e53bSDavid Ahern 			dst_release(&prev->dst);
14038f34e53bSDavid Ahern 		}
14048f34e53bSDavid Ahern 
14058f34e53bSDavid Ahern 		pcpu_rt = NULL;
14068f34e53bSDavid Ahern 	}
14078f34e53bSDavid Ahern 
1408a73e4195SMartin KaFai Lau 	return pcpu_rt;
1409a73e4195SMartin KaFai Lau }
1410a73e4195SMartin KaFai Lau 
1411afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1412db3fedeeSDavid Ahern 					    const struct fib6_result *res)
1413a73e4195SMartin KaFai Lau {
1414a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1415d52d3997SMartin KaFai Lau 
1416db3fedeeSDavid Ahern 	pcpu_rt = ip6_rt_pcpu_alloc(res);
14170e09edccSWei Wang 	if (!pcpu_rt)
14180e09edccSWei Wang 		return NULL;
1419d52d3997SMartin KaFai Lau 
1420f40b6ae2SDavid Ahern 	p = this_cpu_ptr(res->nh->rt6i_pcpu);
1421d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1422951f788aSEric Dumazet 	BUG_ON(prev);
1423a94b9367SWei Wang 
142461fb0d01SEric Dumazet 	if (res->f6i->fib6_destroying) {
142561fb0d01SEric Dumazet 		struct fib6_info *from;
142661fb0d01SEric Dumazet 
142761fb0d01SEric Dumazet 		from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
142861fb0d01SEric Dumazet 		fib6_info_release(from);
142961fb0d01SEric Dumazet 	}
143061fb0d01SEric Dumazet 
1431d52d3997SMartin KaFai Lau 	return pcpu_rt;
1432d52d3997SMartin KaFai Lau }
1433d52d3997SMartin KaFai Lau 
143435732d01SWei Wang /* exception hash table implementation
143535732d01SWei Wang  */
143635732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
143735732d01SWei Wang 
143835732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
143935732d01SWei Wang  * Caller must hold rt6_exception_lock
144035732d01SWei Wang  */
144135732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
144235732d01SWei Wang 				 struct rt6_exception *rt6_ex)
144335732d01SWei Wang {
1444f5b51fe8SPaolo Abeni 	struct fib6_info *from;
1445b2427e67SColin Ian King 	struct net *net;
144681eb8447SWei Wang 
144735732d01SWei Wang 	if (!bucket || !rt6_ex)
144835732d01SWei Wang 		return;
1449b2427e67SColin Ian King 
1450b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
1451f5b51fe8SPaolo Abeni 	net->ipv6.rt6_stats->fib_rt_cache--;
1452f5b51fe8SPaolo Abeni 
1453f5b51fe8SPaolo Abeni 	/* purge completely the exception to allow releasing the held resources:
1454f5b51fe8SPaolo Abeni 	 * some [sk] cache may keep the dst around for unlimited time
1455f5b51fe8SPaolo Abeni 	 */
14560e233874SEric Dumazet 	from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1457f5b51fe8SPaolo Abeni 	fib6_info_release(from);
1458f5b51fe8SPaolo Abeni 	dst_dev_put(&rt6_ex->rt6i->dst);
1459f5b51fe8SPaolo Abeni 
146035732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
146177634cc6SDavid Ahern 	dst_release(&rt6_ex->rt6i->dst);
146235732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
146335732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
146435732d01SWei Wang 	bucket->depth--;
146535732d01SWei Wang }
146635732d01SWei Wang 
146735732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
146835732d01SWei Wang  * Caller must hold rt6_exception_lock
146935732d01SWei Wang  */
147035732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
147135732d01SWei Wang {
147235732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
147335732d01SWei Wang 
147435732d01SWei Wang 	if (!bucket)
147535732d01SWei Wang 		return;
147635732d01SWei Wang 
147735732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
147835732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
147935732d01SWei Wang 			oldest = rt6_ex;
148035732d01SWei Wang 	}
148135732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
148235732d01SWei Wang }
148335732d01SWei Wang 
148435732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
148535732d01SWei Wang 			      const struct in6_addr *src)
148635732d01SWei Wang {
148735732d01SWei Wang 	static u32 seed __read_mostly;
148835732d01SWei Wang 	u32 val;
148935732d01SWei Wang 
149035732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
1491b6b556afSEric Dumazet 	val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed);
149235732d01SWei Wang 
149335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
149435732d01SWei Wang 	if (src)
1495b6b556afSEric Dumazet 		val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val);
149635732d01SWei Wang #endif
149735732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
149835732d01SWei Wang }
149935732d01SWei Wang 
150035732d01SWei Wang /* Helper function to find the cached rt in the hash table
150135732d01SWei Wang  * and update bucket pointer to point to the bucket for this
150235732d01SWei Wang  * (daddr, saddr) pair
150335732d01SWei Wang  * Caller must hold rt6_exception_lock
150435732d01SWei Wang  */
150535732d01SWei Wang static struct rt6_exception *
150635732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
150735732d01SWei Wang 			      const struct in6_addr *daddr,
150835732d01SWei Wang 			      const struct in6_addr *saddr)
150935732d01SWei Wang {
151035732d01SWei Wang 	struct rt6_exception *rt6_ex;
151135732d01SWei Wang 	u32 hval;
151235732d01SWei Wang 
151335732d01SWei Wang 	if (!(*bucket) || !daddr)
151435732d01SWei Wang 		return NULL;
151535732d01SWei Wang 
151635732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
151735732d01SWei Wang 	*bucket += hval;
151835732d01SWei Wang 
151935732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
152035732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
152135732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
152235732d01SWei Wang 
152335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
152435732d01SWei Wang 		if (matched && saddr)
152535732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
152635732d01SWei Wang #endif
152735732d01SWei Wang 		if (matched)
152835732d01SWei Wang 			return rt6_ex;
152935732d01SWei Wang 	}
153035732d01SWei Wang 	return NULL;
153135732d01SWei Wang }
153235732d01SWei Wang 
153335732d01SWei Wang /* Helper function to find the cached rt in the hash table
153435732d01SWei Wang  * and update bucket pointer to point to the bucket for this
153535732d01SWei Wang  * (daddr, saddr) pair
153635732d01SWei Wang  * Caller must hold rcu_read_lock()
153735732d01SWei Wang  */
153835732d01SWei Wang static struct rt6_exception *
153935732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
154035732d01SWei Wang 			 const struct in6_addr *daddr,
154135732d01SWei Wang 			 const struct in6_addr *saddr)
154235732d01SWei Wang {
154335732d01SWei Wang 	struct rt6_exception *rt6_ex;
154435732d01SWei Wang 	u32 hval;
154535732d01SWei Wang 
154635732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
154735732d01SWei Wang 
154835732d01SWei Wang 	if (!(*bucket) || !daddr)
154935732d01SWei Wang 		return NULL;
155035732d01SWei Wang 
155135732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
155235732d01SWei Wang 	*bucket += hval;
155335732d01SWei Wang 
155435732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
155535732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
155635732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
155735732d01SWei Wang 
155835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
155935732d01SWei Wang 		if (matched && saddr)
156035732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
156135732d01SWei Wang #endif
156235732d01SWei Wang 		if (matched)
156335732d01SWei Wang 			return rt6_ex;
156435732d01SWei Wang 	}
156535732d01SWei Wang 	return NULL;
156635732d01SWei Wang }
156735732d01SWei Wang 
1568b748f260SDavid Ahern static unsigned int fib6_mtu(const struct fib6_result *res)
156935732d01SWei Wang {
1570b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
1571d4ead6b3SDavid Ahern 	unsigned int mtu;
1572d4ead6b3SDavid Ahern 
1573b748f260SDavid Ahern 	if (res->f6i->fib6_pmtu) {
1574b748f260SDavid Ahern 		mtu = res->f6i->fib6_pmtu;
1575dcd1f572SDavid Ahern 	} else {
1576b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
1577dcd1f572SDavid Ahern 		struct inet6_dev *idev;
1578dcd1f572SDavid Ahern 
1579dcd1f572SDavid Ahern 		rcu_read_lock();
1580dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
1581dcd1f572SDavid Ahern 		mtu = idev->cnf.mtu6;
1582dcd1f572SDavid Ahern 		rcu_read_unlock();
1583dcd1f572SDavid Ahern 	}
1584dcd1f572SDavid Ahern 
1585d4ead6b3SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1586d4ead6b3SDavid Ahern 
1587b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1588d4ead6b3SDavid Ahern }
1589d4ead6b3SDavid Ahern 
1590cc5c073aSDavid Ahern #define FIB6_EXCEPTION_BUCKET_FLUSHED  0x1UL
1591cc5c073aSDavid Ahern 
1592cc5c073aSDavid Ahern /* used when the flushed bit is not relevant, only access to the bucket
1593cc5c073aSDavid Ahern  * (ie., all bucket users except rt6_insert_exception);
1594cc5c073aSDavid Ahern  *
1595cc5c073aSDavid Ahern  * called under rcu lock; sometimes called with rt6_exception_lock held
1596cc5c073aSDavid Ahern  */
1597cc5c073aSDavid Ahern static
1598cc5c073aSDavid Ahern struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1599cc5c073aSDavid Ahern 						       spinlock_t *lock)
1600cc5c073aSDavid Ahern {
1601cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
1602cc5c073aSDavid Ahern 
1603cc5c073aSDavid Ahern 	if (lock)
1604cc5c073aSDavid Ahern 		bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1605cc5c073aSDavid Ahern 						   lockdep_is_held(lock));
1606cc5c073aSDavid Ahern 	else
1607cc5c073aSDavid Ahern 		bucket = rcu_dereference(nh->rt6i_exception_bucket);
1608cc5c073aSDavid Ahern 
1609cc5c073aSDavid Ahern 	/* remove bucket flushed bit if set */
1610cc5c073aSDavid Ahern 	if (bucket) {
1611cc5c073aSDavid Ahern 		unsigned long p = (unsigned long)bucket;
1612cc5c073aSDavid Ahern 
1613cc5c073aSDavid Ahern 		p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1614cc5c073aSDavid Ahern 		bucket = (struct rt6_exception_bucket *)p;
1615cc5c073aSDavid Ahern 	}
1616cc5c073aSDavid Ahern 
1617cc5c073aSDavid Ahern 	return bucket;
1618cc5c073aSDavid Ahern }
1619cc5c073aSDavid Ahern 
1620cc5c073aSDavid Ahern static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1621cc5c073aSDavid Ahern {
1622cc5c073aSDavid Ahern 	unsigned long p = (unsigned long)bucket;
1623cc5c073aSDavid Ahern 
1624cc5c073aSDavid Ahern 	return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1625cc5c073aSDavid Ahern }
1626cc5c073aSDavid Ahern 
1627cc5c073aSDavid Ahern /* called with rt6_exception_lock held */
1628cc5c073aSDavid Ahern static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1629cc5c073aSDavid Ahern 					      spinlock_t *lock)
1630cc5c073aSDavid Ahern {
1631cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
1632cc5c073aSDavid Ahern 	unsigned long p;
1633cc5c073aSDavid Ahern 
1634cc5c073aSDavid Ahern 	bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1635cc5c073aSDavid Ahern 					   lockdep_is_held(lock));
1636cc5c073aSDavid Ahern 
1637cc5c073aSDavid Ahern 	p = (unsigned long)bucket;
1638cc5c073aSDavid Ahern 	p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1639cc5c073aSDavid Ahern 	bucket = (struct rt6_exception_bucket *)p;
1640cc5c073aSDavid Ahern 	rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1641cc5c073aSDavid Ahern }
1642cc5c073aSDavid Ahern 
164335732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
16445012f0a5SDavid Ahern 				const struct fib6_result *res)
164535732d01SWei Wang {
16465e670d84SDavid Ahern 	struct net *net = dev_net(nrt->dst.dev);
164735732d01SWei Wang 	struct rt6_exception_bucket *bucket;
1648cc5c073aSDavid Ahern 	struct fib6_info *f6i = res->f6i;
164935732d01SWei Wang 	struct in6_addr *src_key = NULL;
165035732d01SWei Wang 	struct rt6_exception *rt6_ex;
1651cc5c073aSDavid Ahern 	struct fib6_nh *nh = res->nh;
165235732d01SWei Wang 	int err = 0;
165335732d01SWei Wang 
165435732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
165535732d01SWei Wang 
1656cc5c073aSDavid Ahern 	bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
165735732d01SWei Wang 					  lockdep_is_held(&rt6_exception_lock));
165835732d01SWei Wang 	if (!bucket) {
165935732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
166035732d01SWei Wang 				 GFP_ATOMIC);
166135732d01SWei Wang 		if (!bucket) {
166235732d01SWei Wang 			err = -ENOMEM;
166335732d01SWei Wang 			goto out;
166435732d01SWei Wang 		}
1665cc5c073aSDavid Ahern 		rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1666cc5c073aSDavid Ahern 	} else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1667cc5c073aSDavid Ahern 		err = -EINVAL;
1668cc5c073aSDavid Ahern 		goto out;
166935732d01SWei Wang 	}
167035732d01SWei Wang 
167135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
16725012f0a5SDavid Ahern 	/* fib6_src.plen != 0 indicates f6i is in subtree
167335732d01SWei Wang 	 * and exception table is indexed by a hash of
16745012f0a5SDavid Ahern 	 * both fib6_dst and fib6_src.
167535732d01SWei Wang 	 * Otherwise, the exception table is indexed by
16765012f0a5SDavid Ahern 	 * a hash of only fib6_dst.
167735732d01SWei Wang 	 */
16785012f0a5SDavid Ahern 	if (f6i->fib6_src.plen)
167935732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
168035732d01SWei Wang #endif
16815012f0a5SDavid Ahern 	/* rt6_mtu_change() might lower mtu on f6i.
1682f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
16835012f0a5SDavid Ahern 	 * is less than f6i's mtu value.
1684f5bbe7eeSWei Wang 	 */
1685b748f260SDavid Ahern 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1686f5bbe7eeSWei Wang 		err = -EINVAL;
1687f5bbe7eeSWei Wang 		goto out;
1688f5bbe7eeSWei Wang 	}
168960006a48SWei Wang 
169035732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
169135732d01SWei Wang 					       src_key);
169235732d01SWei Wang 	if (rt6_ex)
169335732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
169435732d01SWei Wang 
169535732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
169635732d01SWei Wang 	if (!rt6_ex) {
169735732d01SWei Wang 		err = -ENOMEM;
169835732d01SWei Wang 		goto out;
169935732d01SWei Wang 	}
170035732d01SWei Wang 	rt6_ex->rt6i = nrt;
170135732d01SWei Wang 	rt6_ex->stamp = jiffies;
170235732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
170335732d01SWei Wang 	bucket->depth++;
170481eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
170535732d01SWei Wang 
170635732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
170735732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
170835732d01SWei Wang 
170935732d01SWei Wang out:
171035732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
171135732d01SWei Wang 
171235732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1713b886d5f2SPaolo Abeni 	if (!err) {
17145012f0a5SDavid Ahern 		spin_lock_bh(&f6i->fib6_table->tb6_lock);
17155012f0a5SDavid Ahern 		fib6_update_sernum(net, f6i);
17165012f0a5SDavid Ahern 		spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1717b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1718b886d5f2SPaolo Abeni 	}
171935732d01SWei Wang 
172035732d01SWei Wang 	return err;
172135732d01SWei Wang }
172235732d01SWei Wang 
1723c0b220cfSDavid Ahern static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
172435732d01SWei Wang {
172535732d01SWei Wang 	struct rt6_exception_bucket *bucket;
172635732d01SWei Wang 	struct rt6_exception *rt6_ex;
172735732d01SWei Wang 	struct hlist_node *tmp;
172835732d01SWei Wang 	int i;
172935732d01SWei Wang 
173035732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
173135732d01SWei Wang 
1732cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
173335732d01SWei Wang 	if (!bucket)
173435732d01SWei Wang 		goto out;
173535732d01SWei Wang 
1736cc5c073aSDavid Ahern 	/* Prevent rt6_insert_exception() to recreate the bucket list */
1737cc5c073aSDavid Ahern 	if (!from)
1738cc5c073aSDavid Ahern 		fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1739cc5c073aSDavid Ahern 
174035732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1741cc5c073aSDavid Ahern 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1742cc5c073aSDavid Ahern 			if (!from ||
1743cc5c073aSDavid Ahern 			    rcu_access_pointer(rt6_ex->rt6i->from) == from)
174435732d01SWei Wang 				rt6_remove_exception(bucket, rt6_ex);
1745cc5c073aSDavid Ahern 		}
1746cc5c073aSDavid Ahern 		WARN_ON_ONCE(!from && bucket->depth);
174735732d01SWei Wang 		bucket++;
174835732d01SWei Wang 	}
174935732d01SWei Wang out:
175035732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
175135732d01SWei Wang }
175235732d01SWei Wang 
1753e659ba31SDavid Ahern static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1754e659ba31SDavid Ahern {
1755e659ba31SDavid Ahern 	struct fib6_info *f6i = arg;
1756e659ba31SDavid Ahern 
1757e659ba31SDavid Ahern 	fib6_nh_flush_exceptions(nh, f6i);
1758e659ba31SDavid Ahern 
1759e659ba31SDavid Ahern 	return 0;
1760e659ba31SDavid Ahern }
1761e659ba31SDavid Ahern 
1762c0b220cfSDavid Ahern void rt6_flush_exceptions(struct fib6_info *f6i)
1763c0b220cfSDavid Ahern {
1764e659ba31SDavid Ahern 	if (f6i->nh)
1765e659ba31SDavid Ahern 		nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1766e659ba31SDavid Ahern 					 f6i);
1767e659ba31SDavid Ahern 	else
17681cf844c7SDavid Ahern 		fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1769c0b220cfSDavid Ahern }
1770c0b220cfSDavid Ahern 
177135732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
177235732d01SWei Wang  * Caller has to hold rcu_read_lock()
177335732d01SWei Wang  */
17747e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1775510e2cedSWei Wang 					   const struct in6_addr *daddr,
1776510e2cedSWei Wang 					   const struct in6_addr *saddr)
177735732d01SWei Wang {
1778510e2cedSWei Wang 	const struct in6_addr *src_key = NULL;
177935732d01SWei Wang 	struct rt6_exception_bucket *bucket;
178035732d01SWei Wang 	struct rt6_exception *rt6_ex;
17817e4b5128SDavid Ahern 	struct rt6_info *ret = NULL;
178235732d01SWei Wang 
178335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
17847e4b5128SDavid Ahern 	/* fib6i_src.plen != 0 indicates f6i is in subtree
178535732d01SWei Wang 	 * and exception table is indexed by a hash of
17867e4b5128SDavid Ahern 	 * both fib6_dst and fib6_src.
1787510e2cedSWei Wang 	 * However, the src addr used to create the hash
1788510e2cedSWei Wang 	 * might not be exactly the passed in saddr which
1789510e2cedSWei Wang 	 * is a /128 addr from the flow.
1790510e2cedSWei Wang 	 * So we need to use f6i->fib6_src to redo lookup
1791510e2cedSWei Wang 	 * if the passed in saddr does not find anything.
1792510e2cedSWei Wang 	 * (See the logic in ip6_rt_cache_alloc() on how
1793510e2cedSWei Wang 	 * rt->rt6i_src is updated.)
179435732d01SWei Wang 	 */
17957e4b5128SDavid Ahern 	if (res->f6i->fib6_src.plen)
179635732d01SWei Wang 		src_key = saddr;
1797510e2cedSWei Wang find_ex:
179835732d01SWei Wang #endif
1799cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
180035732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
180135732d01SWei Wang 
180235732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
18037e4b5128SDavid Ahern 		ret = rt6_ex->rt6i;
180435732d01SWei Wang 
1805510e2cedSWei Wang #ifdef CONFIG_IPV6_SUBTREES
1806510e2cedSWei Wang 	/* Use fib6_src as src_key and redo lookup */
1807510e2cedSWei Wang 	if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1808510e2cedSWei Wang 		src_key = &res->f6i->fib6_src.addr;
1809510e2cedSWei Wang 		goto find_ex;
1810510e2cedSWei Wang 	}
1811510e2cedSWei Wang #endif
1812510e2cedSWei Wang 
18137e4b5128SDavid Ahern 	return ret;
181435732d01SWei Wang }
181535732d01SWei Wang 
181635732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
1817cc5c073aSDavid Ahern static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1818c0b220cfSDavid Ahern 				    const struct rt6_info *rt)
181935732d01SWei Wang {
1820c0b220cfSDavid Ahern 	const struct in6_addr *src_key = NULL;
182135732d01SWei Wang 	struct rt6_exception_bucket *bucket;
182235732d01SWei Wang 	struct rt6_exception *rt6_ex;
182335732d01SWei Wang 	int err;
182435732d01SWei Wang 
1825cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
182635732d01SWei Wang 		return -ENOENT;
182735732d01SWei Wang 
182835732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1829cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1830cc5c073aSDavid Ahern 
183135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
1832cc5c073aSDavid Ahern 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
1833cc5c073aSDavid Ahern 	 * and exception table is indexed by a hash of
1834cc5c073aSDavid Ahern 	 * both rt6i_dst and rt6i_src.
183535732d01SWei Wang 	 * Otherwise, the exception table is indexed by
183635732d01SWei Wang 	 * a hash of only rt6i_dst.
183735732d01SWei Wang 	 */
1838c0b220cfSDavid Ahern 	if (plen)
183935732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
184035732d01SWei Wang #endif
184135732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
184235732d01SWei Wang 					       &rt->rt6i_dst.addr,
184335732d01SWei Wang 					       src_key);
184435732d01SWei Wang 	if (rt6_ex) {
184535732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
184635732d01SWei Wang 		err = 0;
184735732d01SWei Wang 	} else {
184835732d01SWei Wang 		err = -ENOENT;
184935732d01SWei Wang 	}
185035732d01SWei Wang 
185135732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
185235732d01SWei Wang 	return err;
185335732d01SWei Wang }
185435732d01SWei Wang 
1855e659ba31SDavid Ahern struct fib6_nh_excptn_arg {
1856e659ba31SDavid Ahern 	struct rt6_info	*rt;
1857e659ba31SDavid Ahern 	int		plen;
1858e659ba31SDavid Ahern };
1859e659ba31SDavid Ahern 
1860e659ba31SDavid Ahern static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1861e659ba31SDavid Ahern {
1862e659ba31SDavid Ahern 	struct fib6_nh_excptn_arg *arg = _arg;
1863e659ba31SDavid Ahern 	int err;
1864e659ba31SDavid Ahern 
1865e659ba31SDavid Ahern 	err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1866e659ba31SDavid Ahern 	if (err == 0)
1867e659ba31SDavid Ahern 		return 1;
1868e659ba31SDavid Ahern 
1869e659ba31SDavid Ahern 	return 0;
1870e659ba31SDavid Ahern }
1871e659ba31SDavid Ahern 
1872c0b220cfSDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt)
1873c0b220cfSDavid Ahern {
1874c0b220cfSDavid Ahern 	struct fib6_info *from;
1875c0b220cfSDavid Ahern 
1876c0b220cfSDavid Ahern 	from = rcu_dereference(rt->from);
1877cc5c073aSDavid Ahern 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1878c0b220cfSDavid Ahern 		return -EINVAL;
1879c0b220cfSDavid Ahern 
1880e659ba31SDavid Ahern 	if (from->nh) {
1881e659ba31SDavid Ahern 		struct fib6_nh_excptn_arg arg = {
1882e659ba31SDavid Ahern 			.rt = rt,
1883e659ba31SDavid Ahern 			.plen = from->fib6_src.plen
1884e659ba31SDavid Ahern 		};
1885e659ba31SDavid Ahern 		int rc;
1886e659ba31SDavid Ahern 
1887e659ba31SDavid Ahern 		/* rc = 1 means an entry was found */
1888e659ba31SDavid Ahern 		rc = nexthop_for_each_fib6_nh(from->nh,
1889e659ba31SDavid Ahern 					      rt6_nh_remove_exception_rt,
1890e659ba31SDavid Ahern 					      &arg);
1891e659ba31SDavid Ahern 		return rc ? 0 : -ENOENT;
1892e659ba31SDavid Ahern 	}
1893e659ba31SDavid Ahern 
18941cf844c7SDavid Ahern 	return fib6_nh_remove_exception(from->fib6_nh,
1895cc5c073aSDavid Ahern 					from->fib6_src.plen, rt);
1896c0b220cfSDavid Ahern }
1897c0b220cfSDavid Ahern 
189835732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
189935732d01SWei Wang  * refresh its stamp
190035732d01SWei Wang  */
1901cc5c073aSDavid Ahern static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1902c0b220cfSDavid Ahern 				     const struct rt6_info *rt)
190335732d01SWei Wang {
1904c0b220cfSDavid Ahern 	const struct in6_addr *src_key = NULL;
190535732d01SWei Wang 	struct rt6_exception_bucket *bucket;
190635732d01SWei Wang 	struct rt6_exception *rt6_ex;
1907193f3685SPaolo Abeni 
1908cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, NULL);
190935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
1910cc5c073aSDavid Ahern 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
1911cc5c073aSDavid Ahern 	 * and exception table is indexed by a hash of
1912cc5c073aSDavid Ahern 	 * both rt6i_dst and rt6i_src.
191335732d01SWei Wang 	 * Otherwise, the exception table is indexed by
191435732d01SWei Wang 	 * a hash of only rt6i_dst.
191535732d01SWei Wang 	 */
1916c0b220cfSDavid Ahern 	if (plen)
191735732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
191835732d01SWei Wang #endif
1919cc5c073aSDavid Ahern 	rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
192035732d01SWei Wang 	if (rt6_ex)
192135732d01SWei Wang 		rt6_ex->stamp = jiffies;
1922c0b220cfSDavid Ahern }
192335732d01SWei Wang 
1924e659ba31SDavid Ahern struct fib6_nh_match_arg {
1925e659ba31SDavid Ahern 	const struct net_device *dev;
1926e659ba31SDavid Ahern 	const struct in6_addr	*gw;
1927e659ba31SDavid Ahern 	struct fib6_nh		*match;
1928e659ba31SDavid Ahern };
1929e659ba31SDavid Ahern 
1930e659ba31SDavid Ahern /* determine if fib6_nh has given device and gateway */
1931e659ba31SDavid Ahern static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1932e659ba31SDavid Ahern {
1933e659ba31SDavid Ahern 	struct fib6_nh_match_arg *arg = _arg;
1934e659ba31SDavid Ahern 
1935e659ba31SDavid Ahern 	if (arg->dev != nh->fib_nh_dev ||
1936e659ba31SDavid Ahern 	    (arg->gw && !nh->fib_nh_gw_family) ||
1937e659ba31SDavid Ahern 	    (!arg->gw && nh->fib_nh_gw_family) ||
1938e659ba31SDavid Ahern 	    (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1939e659ba31SDavid Ahern 		return 0;
1940e659ba31SDavid Ahern 
1941e659ba31SDavid Ahern 	arg->match = nh;
1942e659ba31SDavid Ahern 
1943e659ba31SDavid Ahern 	/* found a match, break the loop */
1944e659ba31SDavid Ahern 	return 1;
1945e659ba31SDavid Ahern }
1946e659ba31SDavid Ahern 
1947c0b220cfSDavid Ahern static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1948c0b220cfSDavid Ahern {
1949c0b220cfSDavid Ahern 	struct fib6_info *from;
1950e659ba31SDavid Ahern 	struct fib6_nh *fib6_nh;
1951c0b220cfSDavid Ahern 
1952c0b220cfSDavid Ahern 	rcu_read_lock();
1953c0b220cfSDavid Ahern 
1954c0b220cfSDavid Ahern 	from = rcu_dereference(rt->from);
1955c0b220cfSDavid Ahern 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1956c0b220cfSDavid Ahern 		goto unlock;
1957c0b220cfSDavid Ahern 
1958e659ba31SDavid Ahern 	if (from->nh) {
1959e659ba31SDavid Ahern 		struct fib6_nh_match_arg arg = {
1960e659ba31SDavid Ahern 			.dev = rt->dst.dev,
1961e659ba31SDavid Ahern 			.gw = &rt->rt6i_gateway,
1962e659ba31SDavid Ahern 		};
1963e659ba31SDavid Ahern 
1964e659ba31SDavid Ahern 		nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1965e659ba31SDavid Ahern 
1966e659ba31SDavid Ahern 		if (!arg.match)
1967cff6a327SDavid Ahern 			goto unlock;
1968e659ba31SDavid Ahern 		fib6_nh = arg.match;
1969e659ba31SDavid Ahern 	} else {
1970e659ba31SDavid Ahern 		fib6_nh = from->fib6_nh;
1971e659ba31SDavid Ahern 	}
1972e659ba31SDavid Ahern 	fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
1973193f3685SPaolo Abeni unlock:
197435732d01SWei Wang 	rcu_read_unlock();
197535732d01SWei Wang }
197635732d01SWei Wang 
1977e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1978e9fa1495SStefano Brivio 					 struct rt6_info *rt, int mtu)
1979e9fa1495SStefano Brivio {
1980e9fa1495SStefano Brivio 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1981e9fa1495SStefano Brivio 	 * lowest MTU in the path: always allow updating the route PMTU to
1982e9fa1495SStefano Brivio 	 * reflect PMTU decreases.
1983e9fa1495SStefano Brivio 	 *
1984e9fa1495SStefano Brivio 	 * If the new MTU is higher, and the route PMTU is equal to the local
1985e9fa1495SStefano Brivio 	 * MTU, this means the old MTU is the lowest in the path, so allow
1986e9fa1495SStefano Brivio 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1987e9fa1495SStefano Brivio 	 * handle this.
1988e9fa1495SStefano Brivio 	 */
1989e9fa1495SStefano Brivio 
1990e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) >= mtu)
1991e9fa1495SStefano Brivio 		return true;
1992e9fa1495SStefano Brivio 
1993e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1994e9fa1495SStefano Brivio 		return true;
1995e9fa1495SStefano Brivio 
1996e9fa1495SStefano Brivio 	return false;
1997e9fa1495SStefano Brivio }
1998e9fa1495SStefano Brivio 
1999e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
2000cc5c073aSDavid Ahern 				       const struct fib6_nh *nh, int mtu)
2001f5bbe7eeSWei Wang {
2002f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
2003f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
2004f5bbe7eeSWei Wang 	int i;
2005f5bbe7eeSWei Wang 
2006cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2007e9fa1495SStefano Brivio 	if (!bucket)
2008e9fa1495SStefano Brivio 		return;
2009e9fa1495SStefano Brivio 
2010f5bbe7eeSWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2011f5bbe7eeSWei Wang 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2012f5bbe7eeSWei Wang 			struct rt6_info *entry = rt6_ex->rt6i;
2013e9fa1495SStefano Brivio 
2014e9fa1495SStefano Brivio 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2015d4ead6b3SDavid Ahern 			 * route), the metrics of its rt->from have already
2016f5bbe7eeSWei Wang 			 * been updated.
2017f5bbe7eeSWei Wang 			 */
2018d4ead6b3SDavid Ahern 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
2019e9fa1495SStefano Brivio 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
2020d4ead6b3SDavid Ahern 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
2021f5bbe7eeSWei Wang 		}
2022f5bbe7eeSWei Wang 		bucket++;
2023f5bbe7eeSWei Wang 	}
2024f5bbe7eeSWei Wang }
2025f5bbe7eeSWei Wang 
2026b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
2027b16cb459SWei Wang 
2028cc5c073aSDavid Ahern static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2029cc5c073aSDavid Ahern 					    const struct in6_addr *gateway)
2030b16cb459SWei Wang {
2031b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
2032b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
2033b16cb459SWei Wang 	struct hlist_node *tmp;
2034b16cb459SWei Wang 	int i;
2035b16cb459SWei Wang 
2036cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2037b16cb459SWei Wang 		return;
2038b16cb459SWei Wang 
2039b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
2040cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2041b16cb459SWei Wang 	if (bucket) {
2042b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2043b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
2044b16cb459SWei Wang 						  &bucket->chain, hlist) {
2045b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
2046b16cb459SWei Wang 
2047b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2048b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
2049b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
2050b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
2051b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
2052b16cb459SWei Wang 				}
2053b16cb459SWei Wang 			}
2054b16cb459SWei Wang 			bucket++;
2055b16cb459SWei Wang 		}
2056b16cb459SWei Wang 	}
2057b16cb459SWei Wang 
2058b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
2059b16cb459SWei Wang }
2060b16cb459SWei Wang 
2061c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2062c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
2063c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
2064c757faa8SWei Wang 				      unsigned long now)
2065c757faa8SWei Wang {
2066c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
2067c757faa8SWei Wang 
20681859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
20691859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
20701859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
20711859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
20721859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
20731859bac0SPaolo Abeni 	 */
207431afeb42SWei Wang 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
207531afeb42SWei Wang 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2076c757faa8SWei Wang 			RT6_TRACE("aging clone %p\n", rt);
2077c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
2078c757faa8SWei Wang 			return;
207931afeb42SWei Wang 		}
208031afeb42SWei Wang 	} else if (time_after(jiffies, rt->dst.expires)) {
208131afeb42SWei Wang 		RT6_TRACE("purging expired route %p\n", rt);
208231afeb42SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
208331afeb42SWei Wang 		return;
208431afeb42SWei Wang 	}
208531afeb42SWei Wang 
208631afeb42SWei Wang 	if (rt->rt6i_flags & RTF_GATEWAY) {
2087c757faa8SWei Wang 		struct neighbour *neigh;
2088c757faa8SWei Wang 
20891bfa26ffSEric Dumazet 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
20901bfa26ffSEric Dumazet 
2091b7a320c3SXu Jia 		if (!(neigh && (neigh->flags & NTF_ROUTER))) {
2092c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
2093c757faa8SWei Wang 				  rt);
2094c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
2095c757faa8SWei Wang 			return;
2096c757faa8SWei Wang 		}
2097c757faa8SWei Wang 	}
209831afeb42SWei Wang 
2099c757faa8SWei Wang 	gc_args->more++;
2100c757faa8SWei Wang }
2101c757faa8SWei Wang 
2102cc5c073aSDavid Ahern static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
2103c757faa8SWei Wang 				   struct fib6_gc_args *gc_args,
2104c757faa8SWei Wang 				   unsigned long now)
2105c757faa8SWei Wang {
2106c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
2107c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
2108c757faa8SWei Wang 	struct hlist_node *tmp;
2109c757faa8SWei Wang 	int i;
2110c757faa8SWei Wang 
2111cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2112c757faa8SWei Wang 		return;
2113c757faa8SWei Wang 
21141bfa26ffSEric Dumazet 	rcu_read_lock_bh();
21151bfa26ffSEric Dumazet 	spin_lock(&rt6_exception_lock);
2116cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2117c757faa8SWei Wang 	if (bucket) {
2118c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2119c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
2120c757faa8SWei Wang 						  &bucket->chain, hlist) {
2121c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
2122c757faa8SWei Wang 							  gc_args, now);
2123c757faa8SWei Wang 			}
2124c757faa8SWei Wang 			bucket++;
2125c757faa8SWei Wang 		}
2126c757faa8SWei Wang 	}
21271bfa26ffSEric Dumazet 	spin_unlock(&rt6_exception_lock);
21281bfa26ffSEric Dumazet 	rcu_read_unlock_bh();
2129c757faa8SWei Wang }
2130c757faa8SWei Wang 
2131e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg {
2132e659ba31SDavid Ahern 	struct fib6_gc_args	*gc_args;
2133e659ba31SDavid Ahern 	unsigned long		now;
2134e659ba31SDavid Ahern };
2135e659ba31SDavid Ahern 
2136e659ba31SDavid Ahern static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2137e659ba31SDavid Ahern {
2138e659ba31SDavid Ahern 	struct fib6_nh_age_excptn_arg *arg = _arg;
2139e659ba31SDavid Ahern 
2140e659ba31SDavid Ahern 	fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2141e659ba31SDavid Ahern 	return 0;
2142e659ba31SDavid Ahern }
2143e659ba31SDavid Ahern 
2144cc5c073aSDavid Ahern void rt6_age_exceptions(struct fib6_info *f6i,
2145c0b220cfSDavid Ahern 			struct fib6_gc_args *gc_args,
2146c0b220cfSDavid Ahern 			unsigned long now)
2147c0b220cfSDavid Ahern {
2148e659ba31SDavid Ahern 	if (f6i->nh) {
2149e659ba31SDavid Ahern 		struct fib6_nh_age_excptn_arg arg = {
2150e659ba31SDavid Ahern 			.gc_args = gc_args,
2151e659ba31SDavid Ahern 			.now = now
2152e659ba31SDavid Ahern 		};
2153e659ba31SDavid Ahern 
2154e659ba31SDavid Ahern 		nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2155e659ba31SDavid Ahern 					 &arg);
2156e659ba31SDavid Ahern 	} else {
21571cf844c7SDavid Ahern 		fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2158c0b220cfSDavid Ahern 	}
2159e659ba31SDavid Ahern }
2160c0b220cfSDavid Ahern 
21611d053da9SDavid Ahern /* must be called with rcu lock held */
2162effda4ddSDavid Ahern int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2163effda4ddSDavid Ahern 		      struct flowi6 *fl6, struct fib6_result *res, int strict)
21641da177e4SLinus Torvalds {
2165367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
21661da177e4SLinus Torvalds 
21676454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2168367efcb9SMartin KaFai Lau 	saved_fn = fn;
21691da177e4SLinus Torvalds 
2170ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2171ca254490SDavid Ahern 		oif = 0;
2172ca254490SDavid Ahern 
2173a3c00e46SMartin KaFai Lau redo_rt6_select:
2174effda4ddSDavid Ahern 	rt6_select(net, fn, oif, res, strict);
2175effda4ddSDavid Ahern 	if (res->f6i == net->ipv6.fib6_null_entry) {
2176a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2177a3c00e46SMartin KaFai Lau 		if (fn)
2178a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
2179367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
2180367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
2181367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
2182367efcb9SMartin KaFai Lau 			fn = saved_fn;
2183367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
2184367efcb9SMartin KaFai Lau 		}
2185a3c00e46SMartin KaFai Lau 	}
2186a3c00e46SMartin KaFai Lau 
2187effda4ddSDavid Ahern 	trace_fib6_table_lookup(net, res, table, fl6);
2188d52d3997SMartin KaFai Lau 
2189effda4ddSDavid Ahern 	return 0;
21901d053da9SDavid Ahern }
21911d053da9SDavid Ahern 
21921d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
21931d053da9SDavid Ahern 			       int oif, struct flowi6 *fl6,
21941d053da9SDavid Ahern 			       const struct sk_buff *skb, int flags)
21951d053da9SDavid Ahern {
2196b1d40991SDavid Ahern 	struct fib6_result res = {};
21970e09edccSWei Wang 	struct rt6_info *rt = NULL;
21981d053da9SDavid Ahern 	int strict = 0;
21991d053da9SDavid Ahern 
22000e09edccSWei Wang 	WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
22010e09edccSWei Wang 		     !rcu_read_lock_held());
22020e09edccSWei Wang 
22031d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IFACE;
22041d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
22051d053da9SDavid Ahern 	if (net->ipv6.devconf_all->forwarding == 0)
22061d053da9SDavid Ahern 		strict |= RT6_LOOKUP_F_REACHABLE;
22071d053da9SDavid Ahern 
22081d053da9SDavid Ahern 	rcu_read_lock();
22091d053da9SDavid Ahern 
2210effda4ddSDavid Ahern 	fib6_table_lookup(net, table, oif, fl6, &res, strict);
22110e09edccSWei Wang 	if (res.f6i == net->ipv6.fib6_null_entry)
22120e09edccSWei Wang 		goto out;
221323fb93a4SDavid Ahern 
2214b1d40991SDavid Ahern 	fib6_select_path(net, &res, fl6, oif, false, skb, strict);
2215d83009d4SDavid Ahern 
221623fb93a4SDavid Ahern 	/*Search through exception table */
22177e4b5128SDavid Ahern 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
221823fb93a4SDavid Ahern 	if (rt) {
22190e09edccSWei Wang 		goto out;
22203da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
2221b1d40991SDavid Ahern 			    !res.nh->fib_nh_gw_family)) {
22223da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
22233da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
22243da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
22253da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
22263da59bd9SMartin KaFai Lau 		 */
22270e09edccSWei Wang 		rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
22283da59bd9SMartin KaFai Lau 
22290e09edccSWei Wang 		if (rt) {
22300e09edccSWei Wang 			/* 1 refcnt is taken during ip6_rt_cache_alloc().
22310e09edccSWei Wang 			 * As rt6_uncached_list_add() does not consume refcnt,
22320e09edccSWei Wang 			 * this refcnt is always returned to the caller even
22330e09edccSWei Wang 			 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
22340e09edccSWei Wang 			 */
22350e09edccSWei Wang 			rt6_uncached_list_add(rt);
22360e09edccSWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
22374d85cd0cSDavid Ahern 			rcu_read_unlock();
22383da59bd9SMartin KaFai Lau 
22390e09edccSWei Wang 			return rt;
22401cfb71eeSWei Wang 		}
2241d52d3997SMartin KaFai Lau 	} else {
2242d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
2243951f788aSEric Dumazet 		local_bh_disable();
22440e09edccSWei Wang 		rt = rt6_get_pcpu_route(&res);
2245d52d3997SMartin KaFai Lau 
22460e09edccSWei Wang 		if (!rt)
22470e09edccSWei Wang 			rt = rt6_make_pcpu_route(net, &res);
224893531c67SDavid Ahern 
2249951f788aSEric Dumazet 		local_bh_enable();
22500e09edccSWei Wang 	}
22510e09edccSWei Wang out:
22520e09edccSWei Wang 	if (!rt)
22530e09edccSWei Wang 		rt = net->ipv6.ip6_null_entry;
22540e09edccSWei Wang 	if (!(flags & RT6_LOOKUP_F_DST_NOREF))
22550e09edccSWei Wang 		ip6_hold_safe(net, &rt);
2256951f788aSEric Dumazet 	rcu_read_unlock();
2257d4bea421SDavid Ahern 
22580e09edccSWei Wang 	return rt;
2259c71099acSThomas Graf }
22609ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
2261c71099acSThomas Graf 
226255cced4fSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_input(struct net *net,
2263b75cc8f9SDavid Ahern 					    struct fib6_table *table,
2264b75cc8f9SDavid Ahern 					    struct flowi6 *fl6,
2265b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2266b75cc8f9SDavid Ahern 					    int flags)
22674acad72dSPavel Emelyanov {
2268b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
22694acad72dSPavel Emelyanov }
22704acad72dSPavel Emelyanov 
2271d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
227272331bc0SShmulik Ladkani 					 struct net_device *dev,
2273b75cc8f9SDavid Ahern 					 struct flowi6 *fl6,
2274b75cc8f9SDavid Ahern 					 const struct sk_buff *skb,
2275b75cc8f9SDavid Ahern 					 int flags)
227672331bc0SShmulik Ladkani {
227772331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
227872331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
227972331bc0SShmulik Ladkani 
2280b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
228172331bc0SShmulik Ladkani }
2282d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
228372331bc0SShmulik Ladkani 
228423aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
22855e5d6fedSRoopa Prabhu 				  struct flow_keys *keys,
22865e5d6fedSRoopa Prabhu 				  struct flow_keys *flkeys)
228723aebdacSJakub Sitnicki {
228823aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
228923aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
22905e5d6fedSRoopa Prabhu 	struct flow_keys *_flkeys = flkeys;
229123aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
229223aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
229323aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
2294cea67a2dSEric Dumazet 	struct icmp6hdr _icmph;
229523aebdacSJakub Sitnicki 
229623aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
229723aebdacSJakub Sitnicki 		goto out;
229823aebdacSJakub Sitnicki 
2299cea67a2dSEric Dumazet 	icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2300cea67a2dSEric Dumazet 				   sizeof(_icmph), &_icmph);
2301cea67a2dSEric Dumazet 	if (!icmph)
2302cea67a2dSEric Dumazet 		goto out;
2303cea67a2dSEric Dumazet 
230454074f1dSMatteo Croce 	if (!icmpv6_is_err(icmph->icmp6_type))
230523aebdacSJakub Sitnicki 		goto out;
230623aebdacSJakub Sitnicki 
230723aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
230823aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
230923aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
231023aebdacSJakub Sitnicki 	if (!inner_iph)
231123aebdacSJakub Sitnicki 		goto out;
231223aebdacSJakub Sitnicki 
231323aebdacSJakub Sitnicki 	key_iph = inner_iph;
23145e5d6fedSRoopa Prabhu 	_flkeys = NULL;
231523aebdacSJakub Sitnicki out:
23165e5d6fedSRoopa Prabhu 	if (_flkeys) {
23175e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
23185e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
23195e5d6fedSRoopa Prabhu 		keys->tags.flow_label = _flkeys->tags.flow_label;
23205e5d6fedSRoopa Prabhu 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
23215e5d6fedSRoopa Prabhu 	} else {
232223aebdacSJakub Sitnicki 		keys->addrs.v6addrs.src = key_iph->saddr;
232323aebdacSJakub Sitnicki 		keys->addrs.v6addrs.dst = key_iph->daddr;
2324fa1be7e0SMichal Kubecek 		keys->tags.flow_label = ip6_flowlabel(key_iph);
232523aebdacSJakub Sitnicki 		keys->basic.ip_proto = key_iph->nexthdr;
232623aebdacSJakub Sitnicki 	}
23275e5d6fedSRoopa Prabhu }
232823aebdacSJakub Sitnicki 
2329*73c2c5cbSIdo Schimmel static u32 rt6_multipath_custom_hash_outer(const struct net *net,
2330*73c2c5cbSIdo Schimmel 					   const struct sk_buff *skb,
2331*73c2c5cbSIdo Schimmel 					   bool *p_has_inner)
2332*73c2c5cbSIdo Schimmel {
2333*73c2c5cbSIdo Schimmel 	u32 hash_fields = ip6_multipath_hash_fields(net);
2334*73c2c5cbSIdo Schimmel 	struct flow_keys keys, hash_keys;
2335*73c2c5cbSIdo Schimmel 
2336*73c2c5cbSIdo Schimmel 	if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
2337*73c2c5cbSIdo Schimmel 		return 0;
2338*73c2c5cbSIdo Schimmel 
2339*73c2c5cbSIdo Schimmel 	memset(&hash_keys, 0, sizeof(hash_keys));
2340*73c2c5cbSIdo Schimmel 	skb_flow_dissect_flow_keys(skb, &keys, FLOW_DISSECTOR_F_STOP_AT_ENCAP);
2341*73c2c5cbSIdo Schimmel 
2342*73c2c5cbSIdo Schimmel 	hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2343*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
2344*73c2c5cbSIdo Schimmel 		hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2345*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
2346*73c2c5cbSIdo Schimmel 		hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2347*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
2348*73c2c5cbSIdo Schimmel 		hash_keys.basic.ip_proto = keys.basic.ip_proto;
2349*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
2350*73c2c5cbSIdo Schimmel 		hash_keys.tags.flow_label = keys.tags.flow_label;
2351*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
2352*73c2c5cbSIdo Schimmel 		hash_keys.ports.src = keys.ports.src;
2353*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
2354*73c2c5cbSIdo Schimmel 		hash_keys.ports.dst = keys.ports.dst;
2355*73c2c5cbSIdo Schimmel 
2356*73c2c5cbSIdo Schimmel 	*p_has_inner = !!(keys.control.flags & FLOW_DIS_ENCAPSULATION);
2357*73c2c5cbSIdo Schimmel 	return flow_hash_from_keys(&hash_keys);
2358*73c2c5cbSIdo Schimmel }
2359*73c2c5cbSIdo Schimmel 
2360*73c2c5cbSIdo Schimmel static u32 rt6_multipath_custom_hash_inner(const struct net *net,
2361*73c2c5cbSIdo Schimmel 					   const struct sk_buff *skb,
2362*73c2c5cbSIdo Schimmel 					   bool has_inner)
2363*73c2c5cbSIdo Schimmel {
2364*73c2c5cbSIdo Schimmel 	u32 hash_fields = ip6_multipath_hash_fields(net);
2365*73c2c5cbSIdo Schimmel 	struct flow_keys keys, hash_keys;
2366*73c2c5cbSIdo Schimmel 
2367*73c2c5cbSIdo Schimmel 	/* We assume the packet carries an encapsulation, but if none was
2368*73c2c5cbSIdo Schimmel 	 * encountered during dissection of the outer flow, then there is no
2369*73c2c5cbSIdo Schimmel 	 * point in calling the flow dissector again.
2370*73c2c5cbSIdo Schimmel 	 */
2371*73c2c5cbSIdo Schimmel 	if (!has_inner)
2372*73c2c5cbSIdo Schimmel 		return 0;
2373*73c2c5cbSIdo Schimmel 
2374*73c2c5cbSIdo Schimmel 	if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_MASK))
2375*73c2c5cbSIdo Schimmel 		return 0;
2376*73c2c5cbSIdo Schimmel 
2377*73c2c5cbSIdo Schimmel 	memset(&hash_keys, 0, sizeof(hash_keys));
2378*73c2c5cbSIdo Schimmel 	skb_flow_dissect_flow_keys(skb, &keys, 0);
2379*73c2c5cbSIdo Schimmel 
2380*73c2c5cbSIdo Schimmel 	if (!(keys.control.flags & FLOW_DIS_ENCAPSULATION))
2381*73c2c5cbSIdo Schimmel 		return 0;
2382*73c2c5cbSIdo Schimmel 
2383*73c2c5cbSIdo Schimmel 	if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2384*73c2c5cbSIdo Schimmel 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2385*73c2c5cbSIdo Schimmel 		if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
2386*73c2c5cbSIdo Schimmel 			hash_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
2387*73c2c5cbSIdo Schimmel 		if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
2388*73c2c5cbSIdo Schimmel 			hash_keys.addrs.v4addrs.dst = keys.addrs.v4addrs.dst;
2389*73c2c5cbSIdo Schimmel 	} else if (keys.control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2390*73c2c5cbSIdo Schimmel 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2391*73c2c5cbSIdo Schimmel 		if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_IP)
2392*73c2c5cbSIdo Schimmel 			hash_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
2393*73c2c5cbSIdo Schimmel 		if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_IP)
2394*73c2c5cbSIdo Schimmel 			hash_keys.addrs.v6addrs.dst = keys.addrs.v6addrs.dst;
2395*73c2c5cbSIdo Schimmel 		if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_FLOWLABEL)
2396*73c2c5cbSIdo Schimmel 			hash_keys.tags.flow_label = keys.tags.flow_label;
2397*73c2c5cbSIdo Schimmel 	}
2398*73c2c5cbSIdo Schimmel 
2399*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_IP_PROTO)
2400*73c2c5cbSIdo Schimmel 		hash_keys.basic.ip_proto = keys.basic.ip_proto;
2401*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_SRC_PORT)
2402*73c2c5cbSIdo Schimmel 		hash_keys.ports.src = keys.ports.src;
2403*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_INNER_DST_PORT)
2404*73c2c5cbSIdo Schimmel 		hash_keys.ports.dst = keys.ports.dst;
2405*73c2c5cbSIdo Schimmel 
2406*73c2c5cbSIdo Schimmel 	return flow_hash_from_keys(&hash_keys);
2407*73c2c5cbSIdo Schimmel }
2408*73c2c5cbSIdo Schimmel 
2409*73c2c5cbSIdo Schimmel static u32 rt6_multipath_custom_hash_skb(const struct net *net,
2410*73c2c5cbSIdo Schimmel 					 const struct sk_buff *skb)
2411*73c2c5cbSIdo Schimmel {
2412*73c2c5cbSIdo Schimmel 	u32 mhash, mhash_inner;
2413*73c2c5cbSIdo Schimmel 	bool has_inner = true;
2414*73c2c5cbSIdo Schimmel 
2415*73c2c5cbSIdo Schimmel 	mhash = rt6_multipath_custom_hash_outer(net, skb, &has_inner);
2416*73c2c5cbSIdo Schimmel 	mhash_inner = rt6_multipath_custom_hash_inner(net, skb, has_inner);
2417*73c2c5cbSIdo Schimmel 
2418*73c2c5cbSIdo Schimmel 	return jhash_2words(mhash, mhash_inner, 0);
2419*73c2c5cbSIdo Schimmel }
2420*73c2c5cbSIdo Schimmel 
2421*73c2c5cbSIdo Schimmel static u32 rt6_multipath_custom_hash_fl6(const struct net *net,
2422*73c2c5cbSIdo Schimmel 					 const struct flowi6 *fl6)
2423*73c2c5cbSIdo Schimmel {
2424*73c2c5cbSIdo Schimmel 	u32 hash_fields = ip6_multipath_hash_fields(net);
2425*73c2c5cbSIdo Schimmel 	struct flow_keys hash_keys;
2426*73c2c5cbSIdo Schimmel 
2427*73c2c5cbSIdo Schimmel 	if (!(hash_fields & FIB_MULTIPATH_HASH_FIELD_OUTER_MASK))
2428*73c2c5cbSIdo Schimmel 		return 0;
2429*73c2c5cbSIdo Schimmel 
2430*73c2c5cbSIdo Schimmel 	memset(&hash_keys, 0, sizeof(hash_keys));
2431*73c2c5cbSIdo Schimmel 	hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2432*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_IP)
2433*73c2c5cbSIdo Schimmel 		hash_keys.addrs.v6addrs.src = fl6->saddr;
2434*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_IP)
2435*73c2c5cbSIdo Schimmel 		hash_keys.addrs.v6addrs.dst = fl6->daddr;
2436*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_IP_PROTO)
2437*73c2c5cbSIdo Schimmel 		hash_keys.basic.ip_proto = fl6->flowi6_proto;
2438*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_FLOWLABEL)
2439*73c2c5cbSIdo Schimmel 		hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2440*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_SRC_PORT)
2441*73c2c5cbSIdo Schimmel 		hash_keys.ports.src = fl6->fl6_sport;
2442*73c2c5cbSIdo Schimmel 	if (hash_fields & FIB_MULTIPATH_HASH_FIELD_DST_PORT)
2443*73c2c5cbSIdo Schimmel 		hash_keys.ports.dst = fl6->fl6_dport;
2444*73c2c5cbSIdo Schimmel 
2445*73c2c5cbSIdo Schimmel 	return flow_hash_from_keys(&hash_keys);
2446*73c2c5cbSIdo Schimmel }
2447*73c2c5cbSIdo Schimmel 
244823aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
2449b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2450b4bac172SDavid Ahern 		       const struct sk_buff *skb, struct flow_keys *flkeys)
245123aebdacSJakub Sitnicki {
245223aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
2453b95b6e07SIdo Schimmel 	u32 mhash = 0;
245423aebdacSJakub Sitnicki 
2455bbfa047aSDavid S. Miller 	switch (ip6_multipath_hash_policy(net)) {
2456b4bac172SDavid Ahern 	case 0:
24576f74b6c2SDavid Ahern 		memset(&hash_keys, 0, sizeof(hash_keys));
24586f74b6c2SDavid Ahern 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
24599a2a537aSDavid Ahern 		if (skb) {
24605e5d6fedSRoopa Prabhu 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
24619a2a537aSDavid Ahern 		} else {
24629a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
24639a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2464fa1be7e0SMichal Kubecek 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
24659a2a537aSDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
246623aebdacSJakub Sitnicki 		}
2467b95b6e07SIdo Schimmel 		mhash = flow_hash_from_keys(&hash_keys);
2468b4bac172SDavid Ahern 		break;
2469b4bac172SDavid Ahern 	case 1:
2470b4bac172SDavid Ahern 		if (skb) {
2471b4bac172SDavid Ahern 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2472b4bac172SDavid Ahern 			struct flow_keys keys;
2473b4bac172SDavid Ahern 
2474b4bac172SDavid Ahern 			/* short-circuit if we already have L4 hash present */
2475b4bac172SDavid Ahern 			if (skb->l4_hash)
2476b4bac172SDavid Ahern 				return skb_get_hash_raw(skb) >> 1;
2477b4bac172SDavid Ahern 
2478b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2479b4bac172SDavid Ahern 
2480b4bac172SDavid Ahern 			if (!flkeys) {
2481b4bac172SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2482b4bac172SDavid Ahern 				flkeys = &keys;
2483b4bac172SDavid Ahern 			}
2484b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2485b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2486b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2487b4bac172SDavid Ahern 			hash_keys.ports.src = flkeys->ports.src;
2488b4bac172SDavid Ahern 			hash_keys.ports.dst = flkeys->ports.dst;
2489b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2490b4bac172SDavid Ahern 		} else {
2491b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2492b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2493b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2494b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2495b4bac172SDavid Ahern 			hash_keys.ports.src = fl6->fl6_sport;
2496b4bac172SDavid Ahern 			hash_keys.ports.dst = fl6->fl6_dport;
2497b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2498b4bac172SDavid Ahern 		}
2499b95b6e07SIdo Schimmel 		mhash = flow_hash_from_keys(&hash_keys);
2500b4bac172SDavid Ahern 		break;
2501d8f74f09SStephen Suryaputra 	case 2:
2502d8f74f09SStephen Suryaputra 		memset(&hash_keys, 0, sizeof(hash_keys));
2503d8f74f09SStephen Suryaputra 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2504d8f74f09SStephen Suryaputra 		if (skb) {
2505d8f74f09SStephen Suryaputra 			struct flow_keys keys;
2506d8f74f09SStephen Suryaputra 
2507d8f74f09SStephen Suryaputra 			if (!flkeys) {
2508d8f74f09SStephen Suryaputra 				skb_flow_dissect_flow_keys(skb, &keys, 0);
2509d8f74f09SStephen Suryaputra 				flkeys = &keys;
2510d8f74f09SStephen Suryaputra 			}
2511d8f74f09SStephen Suryaputra 
2512d8f74f09SStephen Suryaputra 			/* Inner can be v4 or v6 */
2513d8f74f09SStephen Suryaputra 			if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2514d8f74f09SStephen Suryaputra 				hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2515d8f74f09SStephen Suryaputra 				hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2516d8f74f09SStephen Suryaputra 				hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2517d8f74f09SStephen Suryaputra 			} else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2518d8f74f09SStephen Suryaputra 				hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2519d8f74f09SStephen Suryaputra 				hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2520d8f74f09SStephen Suryaputra 				hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2521d8f74f09SStephen Suryaputra 				hash_keys.tags.flow_label = flkeys->tags.flow_label;
2522d8f74f09SStephen Suryaputra 				hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2523d8f74f09SStephen Suryaputra 			} else {
2524d8f74f09SStephen Suryaputra 				/* Same as case 0 */
2525d8f74f09SStephen Suryaputra 				hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2526d8f74f09SStephen Suryaputra 				ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2527d8f74f09SStephen Suryaputra 			}
2528d8f74f09SStephen Suryaputra 		} else {
2529d8f74f09SStephen Suryaputra 			/* Same as case 0 */
2530d8f74f09SStephen Suryaputra 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2531d8f74f09SStephen Suryaputra 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2532d8f74f09SStephen Suryaputra 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2533d8f74f09SStephen Suryaputra 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2534d8f74f09SStephen Suryaputra 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2535d8f74f09SStephen Suryaputra 		}
2536b95b6e07SIdo Schimmel 		mhash = flow_hash_from_keys(&hash_keys);
2537d8f74f09SStephen Suryaputra 		break;
2538*73c2c5cbSIdo Schimmel 	case 3:
2539*73c2c5cbSIdo Schimmel 		if (skb)
2540*73c2c5cbSIdo Schimmel 			mhash = rt6_multipath_custom_hash_skb(net, skb);
2541*73c2c5cbSIdo Schimmel 		else
2542*73c2c5cbSIdo Schimmel 			mhash = rt6_multipath_custom_hash_fl6(net, fl6);
2543*73c2c5cbSIdo Schimmel 		break;
2544b4bac172SDavid Ahern 	}
254523aebdacSJakub Sitnicki 
25469a2a537aSDavid Ahern 	return mhash >> 1;
254723aebdacSJakub Sitnicki }
254823aebdacSJakub Sitnicki 
254967f415ddSWei Wang /* Called with rcu held */
2550c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
2551c71099acSThomas Graf {
2552b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2553c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
255467f415ddSWei Wang 	int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
2555904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
25564c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
2557e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
25584c9483b2SDavid S. Miller 		.daddr = iph->daddr,
25594c9483b2SDavid S. Miller 		.saddr = iph->saddr,
25606502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
25614c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
25624c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
2563c71099acSThomas Graf 	};
25645e5d6fedSRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2565adaa70bbSThomas Graf 
2566904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
256746fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2568904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
25695e5d6fedSRoopa Prabhu 
25705e5d6fedSRoopa Prabhu 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
25715e5d6fedSRoopa Prabhu 		flkeys = &_flkeys;
25725e5d6fedSRoopa Prabhu 
257323aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2574b4bac172SDavid Ahern 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
257506e9d040SJiri Benc 	skb_dst_drop(skb);
257667f415ddSWei Wang 	skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
257767f415ddSWei Wang 						      &fl6, skb, flags));
2578c71099acSThomas Graf }
2579c71099acSThomas Graf 
258055cced4fSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_output(struct net *net,
2581b75cc8f9SDavid Ahern 					     struct fib6_table *table,
2582b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
2583b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2584b75cc8f9SDavid Ahern 					     int flags)
2585c71099acSThomas Graf {
2586b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2587c71099acSThomas Graf }
2588c71099acSThomas Graf 
25897d9e5f42SWei Wang struct dst_entry *ip6_route_output_flags_noref(struct net *net,
25907d9e5f42SWei Wang 					       const struct sock *sk,
25916f21c96aSPaolo Abeni 					       struct flowi6 *fl6, int flags)
2592c71099acSThomas Graf {
2593d46a9d67SDavid Ahern 	bool any_src;
2594c71099acSThomas Graf 
25953ede0bbcSRobert Shearman 	if (ipv6_addr_type(&fl6->daddr) &
25963ede0bbcSRobert Shearman 	    (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
25974c1feac5SDavid Ahern 		struct dst_entry *dst;
25984c1feac5SDavid Ahern 
25997d9e5f42SWei Wang 		/* This function does not take refcnt on the dst */
26004c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
2601ca254490SDavid Ahern 		if (dst)
2602ca254490SDavid Ahern 			return dst;
26034c1feac5SDavid Ahern 	}
2604ca254490SDavid Ahern 
26051fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
26064dc27d1cSDavid McCullough 
26077d9e5f42SWei Wang 	flags |= RT6_LOOKUP_F_DST_NOREF;
2608d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
2609741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2610d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
261177d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
2612c71099acSThomas Graf 
2613d46a9d67SDavid Ahern 	if (!any_src)
2614adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
26150c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
26160c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2617adaa70bbSThomas Graf 
2618b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
26191da177e4SLinus Torvalds }
26207d9e5f42SWei Wang EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
26217d9e5f42SWei Wang 
26227d9e5f42SWei Wang struct dst_entry *ip6_route_output_flags(struct net *net,
26237d9e5f42SWei Wang 					 const struct sock *sk,
26247d9e5f42SWei Wang 					 struct flowi6 *fl6,
26257d9e5f42SWei Wang 					 int flags)
26267d9e5f42SWei Wang {
26277d9e5f42SWei Wang 	struct dst_entry *dst;
26287d9e5f42SWei Wang 	struct rt6_info *rt6;
26297d9e5f42SWei Wang 
26307d9e5f42SWei Wang 	rcu_read_lock();
26317d9e5f42SWei Wang 	dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
26327d9e5f42SWei Wang 	rt6 = (struct rt6_info *)dst;
26337d9e5f42SWei Wang 	/* For dst cached in uncached_list, refcnt is already taken. */
26347d9e5f42SWei Wang 	if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
26357d9e5f42SWei Wang 		dst = &net->ipv6.ip6_null_entry->dst;
26367d9e5f42SWei Wang 		dst_hold(dst);
26377d9e5f42SWei Wang 	}
26387d9e5f42SWei Wang 	rcu_read_unlock();
26397d9e5f42SWei Wang 
26407d9e5f42SWei Wang 	return dst;
26417d9e5f42SWei Wang }
26426f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
26431da177e4SLinus Torvalds 
26442774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
264514e50e57SDavid S. Miller {
26465c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
26471dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
264814e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
264914e50e57SDavid S. Miller 
26501dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
265162cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
265214e50e57SDavid S. Miller 	if (rt) {
26530a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
265481eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
26550a1f5962SMartin KaFai Lau 
2656d8d1f30bSChangli Gao 		new = &rt->dst;
265714e50e57SDavid S. Miller 		new->__use = 1;
2658352e512cSHerbert Xu 		new->input = dst_discard;
2659ede2059dSEric W. Biederman 		new->output = dst_discard_out;
266014e50e57SDavid S. Miller 
2661defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
266214e50e57SDavid S. Miller 
26631dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
26644e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
26650a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
266614e50e57SDavid S. Miller 
266714e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
266814e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
266914e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
267014e50e57SDavid S. Miller #endif
267114e50e57SDavid S. Miller 	}
267214e50e57SDavid S. Miller 
267369ead7afSDavid S. Miller 	dst_release(dst_orig);
267469ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
267514e50e57SDavid S. Miller }
267614e50e57SDavid S. Miller 
26771da177e4SLinus Torvalds /*
26781da177e4SLinus Torvalds  *	Destination cache support functions
26791da177e4SLinus Torvalds  */
26801da177e4SLinus Torvalds 
26818d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie)
26823da59bd9SMartin KaFai Lau {
268336143645SSteffen Klassert 	u32 rt_cookie = 0;
2684c5cff856SWei Wang 
26858ae86971SDavid Ahern 	if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
268693531c67SDavid Ahern 		return false;
268793531c67SDavid Ahern 
268893531c67SDavid Ahern 	if (fib6_check_expired(f6i))
268993531c67SDavid Ahern 		return false;
269093531c67SDavid Ahern 
269193531c67SDavid Ahern 	return true;
269293531c67SDavid Ahern }
269393531c67SDavid Ahern 
2694a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt,
2695a68886a6SDavid Ahern 				   struct fib6_info *from,
2696a68886a6SDavid Ahern 				   u32 cookie)
26973da59bd9SMartin KaFai Lau {
2698c5cff856SWei Wang 	u32 rt_cookie = 0;
2699c5cff856SWei Wang 
270049d05fe2SDavid Ahern 	if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
270193531c67SDavid Ahern 	    rt_cookie != cookie)
27023da59bd9SMartin KaFai Lau 		return NULL;
27033da59bd9SMartin KaFai Lau 
27043da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
27053da59bd9SMartin KaFai Lau 		return NULL;
27063da59bd9SMartin KaFai Lau 
27073da59bd9SMartin KaFai Lau 	return &rt->dst;
27083da59bd9SMartin KaFai Lau }
27093da59bd9SMartin KaFai Lau 
2710a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2711a68886a6SDavid Ahern 					    struct fib6_info *from,
2712a68886a6SDavid Ahern 					    u32 cookie)
27133da59bd9SMartin KaFai Lau {
27145973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
27155973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2716a68886a6SDavid Ahern 	    fib6_check(from, cookie))
27173da59bd9SMartin KaFai Lau 		return &rt->dst;
27183da59bd9SMartin KaFai Lau 	else
27193da59bd9SMartin KaFai Lau 		return NULL;
27203da59bd9SMartin KaFai Lau }
27213da59bd9SMartin KaFai Lau 
2722bbd807dfSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct dst_entry *ip6_dst_check(struct dst_entry *dst,
2723bbd807dfSBrian Vazquez 							u32 cookie)
27241da177e4SLinus Torvalds {
2725a87b7dc9SDavid Ahern 	struct dst_entry *dst_ret;
2726a68886a6SDavid Ahern 	struct fib6_info *from;
27271da177e4SLinus Torvalds 	struct rt6_info *rt;
27281da177e4SLinus Torvalds 
2729a87b7dc9SDavid Ahern 	rt = container_of(dst, struct rt6_info, dst);
2730a87b7dc9SDavid Ahern 
27318f34e53bSDavid Ahern 	if (rt->sernum)
27328f34e53bSDavid Ahern 		return rt6_is_valid(rt) ? dst : NULL;
27338f34e53bSDavid Ahern 
2734a87b7dc9SDavid Ahern 	rcu_read_lock();
27351da177e4SLinus Torvalds 
27366f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
27376f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
27386f3118b5SNicolas Dichtel 	 * into this function always.
27396f3118b5SNicolas Dichtel 	 */
2740e3bc10bdSHannes Frederic Sowa 
2741a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
27424b32b5adSMartin KaFai Lau 
2743a68886a6SDavid Ahern 	if (from && (rt->rt6i_flags & RTF_PCPU ||
2744a68886a6SDavid Ahern 	    unlikely(!list_empty(&rt->rt6i_uncached))))
2745a68886a6SDavid Ahern 		dst_ret = rt6_dst_from_check(rt, from, cookie);
27463da59bd9SMartin KaFai Lau 	else
2747a68886a6SDavid Ahern 		dst_ret = rt6_check(rt, from, cookie);
2748a87b7dc9SDavid Ahern 
2749a87b7dc9SDavid Ahern 	rcu_read_unlock();
2750a87b7dc9SDavid Ahern 
2751a87b7dc9SDavid Ahern 	return dst_ret;
27521da177e4SLinus Torvalds }
27539c97921aSBrian Vazquez EXPORT_INDIRECT_CALLABLE(ip6_dst_check);
27541da177e4SLinus Torvalds 
27551da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
27561da177e4SLinus Torvalds {
27571da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
27581da177e4SLinus Torvalds 
27591da177e4SLinus Torvalds 	if (rt) {
276054c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
2761c3c14da0SDavid Ahern 			rcu_read_lock();
276254c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
276393531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
276454c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
27651da177e4SLinus Torvalds 			}
2766c3c14da0SDavid Ahern 			rcu_read_unlock();
276754c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
276854c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
276954c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
277054c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
277154c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
277254c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
27731da177e4SLinus Torvalds }
27741da177e4SLinus Torvalds 
27751da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
27761da177e4SLinus Torvalds {
27771da177e4SLinus Torvalds 	struct rt6_info *rt;
27781da177e4SLinus Torvalds 
27793ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
27801da177e4SLinus Torvalds 
2781adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
27821da177e4SLinus Torvalds 	if (rt) {
27838a14e46fSDavid Ahern 		rcu_read_lock();
27841eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
278593531c67SDavid Ahern 			rt6_remove_exception_rt(rt);
2786c5cff856SWei Wang 		} else {
2787a68886a6SDavid Ahern 			struct fib6_info *from;
2788c5cff856SWei Wang 			struct fib6_node *fn;
2789c5cff856SWei Wang 
2790a68886a6SDavid Ahern 			from = rcu_dereference(rt->from);
2791a68886a6SDavid Ahern 			if (from) {
2792a68886a6SDavid Ahern 				fn = rcu_dereference(from->fib6_node);
2793c5cff856SWei Wang 				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2794c5cff856SWei Wang 					fn->fn_sernum = -1;
2795a68886a6SDavid Ahern 			}
27961da177e4SLinus Torvalds 		}
27971da177e4SLinus Torvalds 		rcu_read_unlock();
27981da177e4SLinus Torvalds 	}
27991da177e4SLinus Torvalds }
28001da177e4SLinus Torvalds 
28016a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout)
28026a3e030fSDavid Ahern {
2803a68886a6SDavid Ahern 	if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2804a68886a6SDavid Ahern 		struct fib6_info *from;
2805a68886a6SDavid Ahern 
2806a68886a6SDavid Ahern 		rcu_read_lock();
2807a68886a6SDavid Ahern 		from = rcu_dereference(rt0->from);
2808a68886a6SDavid Ahern 		if (from)
2809a68886a6SDavid Ahern 			rt0->dst.expires = from->expires;
2810a68886a6SDavid Ahern 		rcu_read_unlock();
2811a68886a6SDavid Ahern 	}
28126a3e030fSDavid Ahern 
28136a3e030fSDavid Ahern 	dst_set_expires(&rt0->dst, timeout);
28146a3e030fSDavid Ahern 	rt0->rt6i_flags |= RTF_EXPIRES;
28156700c270SDavid S. Miller }
28161da177e4SLinus Torvalds 
281745e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
281845e4fd26SMartin KaFai Lau {
281945e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
282045e4fd26SMartin KaFai Lau 
2821d4ead6b3SDavid Ahern 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
282245e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
282345e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
282445e4fd26SMartin KaFai Lau }
282545e4fd26SMartin KaFai Lau 
28260d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
28270d3f6d29SMartin KaFai Lau {
28280d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
28291490ed2aSPaolo Abeni 		(rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
28300d3f6d29SMartin KaFai Lau }
28310d3f6d29SMartin KaFai Lau 
283245e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2833bd085ef6SHangbin Liu 				 const struct ipv6hdr *iph, u32 mtu,
2834bd085ef6SHangbin Liu 				 bool confirm_neigh)
28351da177e4SLinus Torvalds {
28360dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
28371da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
28381da177e4SLinus Torvalds 
283909454fd0SMaciej Żenczykowski 	/* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
284009454fd0SMaciej Żenczykowski 	 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
284109454fd0SMaciej Żenczykowski 	 * [see also comment in rt6_mtu_change_route()]
284209454fd0SMaciej Żenczykowski 	 */
284319bda36cSXin Long 
284445e4fd26SMartin KaFai Lau 	if (iph) {
284545e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
284645e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
284745e4fd26SMartin KaFai Lau 	} else if (sk) {
284845e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
284945e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
285045e4fd26SMartin KaFai Lau 	} else {
28510dec879fSJulian Anastasov 		daddr = NULL;
28520dec879fSJulian Anastasov 		saddr = NULL;
28531da177e4SLinus Torvalds 	}
2854bd085ef6SHangbin Liu 
2855bd085ef6SHangbin Liu 	if (confirm_neigh)
28560dec879fSJulian Anastasov 		dst_confirm_neigh(dst, daddr);
2857bd085ef6SHangbin Liu 
28584a65dff8SGeorg Kohmann 	if (mtu < IPV6_MIN_MTU)
28594a65dff8SGeorg Kohmann 		return;
28600dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
28610dec879fSJulian Anastasov 		return;
28620dec879fSJulian Anastasov 
28630dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
28640dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
28652b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
28662b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
28672b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
28680dec879fSJulian Anastasov 	} else if (daddr) {
286985bd05deSDavid Ahern 		struct fib6_result res = {};
28700dec879fSJulian Anastasov 		struct rt6_info *nrt6;
28710dec879fSJulian Anastasov 
28724d85cd0cSDavid Ahern 		rcu_read_lock();
287385bd05deSDavid Ahern 		res.f6i = rcu_dereference(rt6->from);
287443a4b60dSDavid Ahern 		if (!res.f6i)
287543a4b60dSDavid Ahern 			goto out_unlock;
287643a4b60dSDavid Ahern 
28777d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
28787d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
28797d21fec9SDavid Ahern 
28802d44234bSDavid Ahern 		if (res.f6i->nh) {
28812d44234bSDavid Ahern 			struct fib6_nh_match_arg arg = {
28822d44234bSDavid Ahern 				.dev = dst->dev,
28832d44234bSDavid Ahern 				.gw = &rt6->rt6i_gateway,
28842d44234bSDavid Ahern 			};
28852d44234bSDavid Ahern 
28862d44234bSDavid Ahern 			nexthop_for_each_fib6_nh(res.f6i->nh,
28872d44234bSDavid Ahern 						 fib6_nh_find_match, &arg);
28882d44234bSDavid Ahern 
28892d44234bSDavid Ahern 			/* fib6_info uses a nexthop that does not have fib6_nh
28902d44234bSDavid Ahern 			 * using the dst->dev + gw. Should be impossible.
28912d44234bSDavid Ahern 			 */
289243a4b60dSDavid Ahern 			if (!arg.match)
289343a4b60dSDavid Ahern 				goto out_unlock;
28942d44234bSDavid Ahern 
28952d44234bSDavid Ahern 			res.nh = arg.match;
28962d44234bSDavid Ahern 		} else {
28972d44234bSDavid Ahern 			res.nh = res.f6i->fib6_nh;
28982d44234bSDavid Ahern 		}
28992d44234bSDavid Ahern 
290085bd05deSDavid Ahern 		nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
290145e4fd26SMartin KaFai Lau 		if (nrt6) {
290245e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
29035012f0a5SDavid Ahern 			if (rt6_insert_exception(nrt6, &res))
29042b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
290545e4fd26SMartin KaFai Lau 		}
290643a4b60dSDavid Ahern out_unlock:
2907a68886a6SDavid Ahern 		rcu_read_unlock();
290845e4fd26SMartin KaFai Lau 	}
290945e4fd26SMartin KaFai Lau }
291045e4fd26SMartin KaFai Lau 
291145e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2912bd085ef6SHangbin Liu 			       struct sk_buff *skb, u32 mtu,
2913bd085ef6SHangbin Liu 			       bool confirm_neigh)
291445e4fd26SMartin KaFai Lau {
2915bd085ef6SHangbin Liu 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2916bd085ef6SHangbin Liu 			     confirm_neigh);
29171da177e4SLinus Torvalds }
29181da177e4SLinus Torvalds 
291942ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2920e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
292181aded24SDavid S. Miller {
292281aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
292381aded24SDavid S. Miller 	struct dst_entry *dst;
2924dc92095dSMaciej Żenczykowski 	struct flowi6 fl6 = {
2925dc92095dSMaciej Żenczykowski 		.flowi6_oif = oif,
2926dc92095dSMaciej Żenczykowski 		.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2927dc92095dSMaciej Żenczykowski 		.daddr = iph->daddr,
2928dc92095dSMaciej Żenczykowski 		.saddr = iph->saddr,
2929dc92095dSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
2930dc92095dSMaciej Żenczykowski 		.flowi6_uid = uid,
2931dc92095dSMaciej Żenczykowski 	};
293281aded24SDavid S. Miller 
293381aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
293481aded24SDavid S. Miller 	if (!dst->error)
2935bd085ef6SHangbin Liu 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
293681aded24SDavid S. Miller 	dst_release(dst);
293781aded24SDavid S. Miller }
293881aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
293981aded24SDavid S. Miller 
294081aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
294181aded24SDavid S. Miller {
29427ddacfa5SDavid Ahern 	int oif = sk->sk_bound_dev_if;
294333c162a9SMartin KaFai Lau 	struct dst_entry *dst;
294433c162a9SMartin KaFai Lau 
29457ddacfa5SDavid Ahern 	if (!oif && skb->dev)
29467ddacfa5SDavid Ahern 		oif = l3mdev_master_ifindex(skb->dev);
29477ddacfa5SDavid Ahern 
29487ddacfa5SDavid Ahern 	ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
294933c162a9SMartin KaFai Lau 
295033c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
295133c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
295233c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
295333c162a9SMartin KaFai Lau 		return;
295433c162a9SMartin KaFai Lau 
295533c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
295633c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
295733c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
295833c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
295981aded24SDavid S. Miller }
296081aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
296181aded24SDavid S. Miller 
29627d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
29637d6850f7SAlexey Kodanev 			   const struct flowi6 *fl6)
29647d6850f7SAlexey Kodanev {
29657d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
29667d6850f7SAlexey Kodanev 	struct ipv6_pinfo *np = inet6_sk(sk);
29677d6850f7SAlexey Kodanev #endif
29687d6850f7SAlexey Kodanev 
29697d6850f7SAlexey Kodanev 	ip6_dst_store(sk, dst,
29707d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
29717d6850f7SAlexey Kodanev 		      &sk->sk_v6_daddr : NULL,
29727d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
29737d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
29747d6850f7SAlexey Kodanev 		      &np->saddr :
29757d6850f7SAlexey Kodanev #endif
29767d6850f7SAlexey Kodanev 		      NULL);
29777d6850f7SAlexey Kodanev }
29787d6850f7SAlexey Kodanev 
29799b6b35abSDavid Ahern static bool ip6_redirect_nh_match(const struct fib6_result *res,
29800b34eb00SDavid Ahern 				  struct flowi6 *fl6,
29810b34eb00SDavid Ahern 				  const struct in6_addr *gw,
29820b34eb00SDavid Ahern 				  struct rt6_info **ret)
29830b34eb00SDavid Ahern {
29849b6b35abSDavid Ahern 	const struct fib6_nh *nh = res->nh;
29859b6b35abSDavid Ahern 
29860b34eb00SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
29870b34eb00SDavid Ahern 	    fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
29880b34eb00SDavid Ahern 		return false;
29890b34eb00SDavid Ahern 
29900b34eb00SDavid Ahern 	/* rt_cache's gateway might be different from its 'parent'
29910b34eb00SDavid Ahern 	 * in the case of an ip redirect.
29920b34eb00SDavid Ahern 	 * So we keep searching in the exception table if the gateway
29930b34eb00SDavid Ahern 	 * is different.
29940b34eb00SDavid Ahern 	 */
29950b34eb00SDavid Ahern 	if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
29960b34eb00SDavid Ahern 		struct rt6_info *rt_cache;
29970b34eb00SDavid Ahern 
29989b6b35abSDavid Ahern 		rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
29990b34eb00SDavid Ahern 		if (rt_cache &&
30000b34eb00SDavid Ahern 		    ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
30010b34eb00SDavid Ahern 			*ret = rt_cache;
30020b34eb00SDavid Ahern 			return true;
30030b34eb00SDavid Ahern 		}
30040b34eb00SDavid Ahern 		return false;
30050b34eb00SDavid Ahern 	}
30060b34eb00SDavid Ahern 	return true;
30070b34eb00SDavid Ahern }
30080b34eb00SDavid Ahern 
3009c55c8988SDavid Ahern struct fib6_nh_rd_arg {
3010c55c8988SDavid Ahern 	struct fib6_result	*res;
3011c55c8988SDavid Ahern 	struct flowi6		*fl6;
3012c55c8988SDavid Ahern 	const struct in6_addr	*gw;
3013c55c8988SDavid Ahern 	struct rt6_info		**ret;
3014c55c8988SDavid Ahern };
3015c55c8988SDavid Ahern 
3016c55c8988SDavid Ahern static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
3017c55c8988SDavid Ahern {
3018c55c8988SDavid Ahern 	struct fib6_nh_rd_arg *arg = _arg;
3019c55c8988SDavid Ahern 
3020c55c8988SDavid Ahern 	arg->res->nh = nh;
3021c55c8988SDavid Ahern 	return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
3022c55c8988SDavid Ahern }
3023c55c8988SDavid Ahern 
3024b55b76b2SDuan Jiong /* Handle redirects */
3025b55b76b2SDuan Jiong struct ip6rd_flowi {
3026b55b76b2SDuan Jiong 	struct flowi6 fl6;
3027b55b76b2SDuan Jiong 	struct in6_addr gateway;
3028b55b76b2SDuan Jiong };
3029b55b76b2SDuan Jiong 
303055cced4fSBrian Vazquez INDIRECT_CALLABLE_SCOPE struct rt6_info *__ip6_route_redirect(struct net *net,
3031b55b76b2SDuan Jiong 					     struct fib6_table *table,
3032b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
3033b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
3034b55b76b2SDuan Jiong 					     int flags)
3035b55b76b2SDuan Jiong {
3036b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
30370b34eb00SDavid Ahern 	struct rt6_info *ret = NULL;
30389b6b35abSDavid Ahern 	struct fib6_result res = {};
3039c55c8988SDavid Ahern 	struct fib6_nh_rd_arg arg = {
3040c55c8988SDavid Ahern 		.res = &res,
3041c55c8988SDavid Ahern 		.fl6 = fl6,
3042c55c8988SDavid Ahern 		.gw  = &rdfl->gateway,
3043c55c8988SDavid Ahern 		.ret = &ret
3044c55c8988SDavid Ahern 	};
30458d1c802bSDavid Ahern 	struct fib6_info *rt;
3046b55b76b2SDuan Jiong 	struct fib6_node *fn;
3047b55b76b2SDuan Jiong 
304831680ac2SDavid Ahern 	/* l3mdev_update_flow overrides oif if the device is enslaved; in
304931680ac2SDavid Ahern 	 * this case we must match on the real ingress device, so reset it
305031680ac2SDavid Ahern 	 */
305131680ac2SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
305231680ac2SDavid Ahern 		fl6->flowi6_oif = skb->dev->ifindex;
305331680ac2SDavid Ahern 
3054b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
305567c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
3056b55b76b2SDuan Jiong 	 *
3057b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
3058b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
3059b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
3060b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
3061b55b76b2SDuan Jiong 	 * routes.
3062b55b76b2SDuan Jiong 	 */
3063b55b76b2SDuan Jiong 
306466f5d6ceSWei Wang 	rcu_read_lock();
30656454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
3066b55b76b2SDuan Jiong restart:
306766f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
30689b6b35abSDavid Ahern 		res.f6i = rt;
306914895687SDavid Ahern 		if (fib6_check_expired(rt))
3070b55b76b2SDuan Jiong 			continue;
307193c2fb25SDavid Ahern 		if (rt->fib6_flags & RTF_REJECT)
3072b55b76b2SDuan Jiong 			break;
3073c55c8988SDavid Ahern 		if (unlikely(rt->nh)) {
3074c55c8988SDavid Ahern 			if (nexthop_is_blackhole(rt->nh))
3075c55c8988SDavid Ahern 				continue;
3076c55c8988SDavid Ahern 			/* on match, res->nh is filled in and potentially ret */
3077c55c8988SDavid Ahern 			if (nexthop_for_each_fib6_nh(rt->nh,
3078c55c8988SDavid Ahern 						     fib6_nh_redirect_match,
3079c55c8988SDavid Ahern 						     &arg))
30800b34eb00SDavid Ahern 				goto out;
3081c55c8988SDavid Ahern 		} else {
3082c55c8988SDavid Ahern 			res.nh = rt->fib6_nh;
3083c55c8988SDavid Ahern 			if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
3084c55c8988SDavid Ahern 						  &ret))
3085c55c8988SDavid Ahern 				goto out;
3086c55c8988SDavid Ahern 		}
3087b55b76b2SDuan Jiong 	}
3088b55b76b2SDuan Jiong 
3089b55b76b2SDuan Jiong 	if (!rt)
3090421842edSDavid Ahern 		rt = net->ipv6.fib6_null_entry;
309193c2fb25SDavid Ahern 	else if (rt->fib6_flags & RTF_REJECT) {
309223fb93a4SDavid Ahern 		ret = net->ipv6.ip6_null_entry;
3093b0a1ba59SMartin KaFai Lau 		goto out;
3094b0a1ba59SMartin KaFai Lau 	}
3095b0a1ba59SMartin KaFai Lau 
3096421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
3097a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
3098a3c00e46SMartin KaFai Lau 		if (fn)
3099a3c00e46SMartin KaFai Lau 			goto restart;
3100b55b76b2SDuan Jiong 	}
3101a3c00e46SMartin KaFai Lau 
31029b6b35abSDavid Ahern 	res.f6i = rt;
31031cf844c7SDavid Ahern 	res.nh = rt->fib6_nh;
3104b0a1ba59SMartin KaFai Lau out:
31057d21fec9SDavid Ahern 	if (ret) {
310610585b43SDavid Ahern 		ip6_hold_safe(net, &ret);
31077d21fec9SDavid Ahern 	} else {
31087d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
31097d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
31109b6b35abSDavid Ahern 		ret = ip6_create_rt_rcu(&res);
31117d21fec9SDavid Ahern 	}
3112b55b76b2SDuan Jiong 
311366f5d6ceSWei Wang 	rcu_read_unlock();
3114b55b76b2SDuan Jiong 
31158ff2e5b2SDavid Ahern 	trace_fib6_table_lookup(net, &res, table, fl6);
311623fb93a4SDavid Ahern 	return ret;
3117b55b76b2SDuan Jiong };
3118b55b76b2SDuan Jiong 
3119b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
3120b55b76b2SDuan Jiong 					    const struct flowi6 *fl6,
3121b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
3122b55b76b2SDuan Jiong 					    const struct in6_addr *gateway)
3123b55b76b2SDuan Jiong {
3124b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
3125b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
3126b55b76b2SDuan Jiong 
3127b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
3128b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
3129b55b76b2SDuan Jiong 
3130b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, &rdfl.fl6, skb,
3131b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
3132b55b76b2SDuan Jiong }
3133b55b76b2SDuan Jiong 
3134e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3135e2d118a1SLorenzo Colitti 		  kuid_t uid)
31363a5ad2eeSDavid S. Miller {
31373a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
31383a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
31391f7f10acSMaciej Żenczykowski 	struct flowi6 fl6 = {
31401f7f10acSMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
31411f7f10acSMaciej Żenczykowski 		.flowi6_oif = oif,
31421f7f10acSMaciej Żenczykowski 		.flowi6_mark = mark,
31431f7f10acSMaciej Żenczykowski 		.daddr = iph->daddr,
31441f7f10acSMaciej Żenczykowski 		.saddr = iph->saddr,
31451f7f10acSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
31461f7f10acSMaciej Żenczykowski 		.flowi6_uid = uid,
31471f7f10acSMaciej Żenczykowski 	};
31483a5ad2eeSDavid S. Miller 
3149b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
31506700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
31513a5ad2eeSDavid S. Miller 	dst_release(dst);
31523a5ad2eeSDavid S. Miller }
31533a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
31543a5ad2eeSDavid S. Miller 
3155d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
3156c92a59ecSDuan Jiong {
3157c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
3158c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3159c92a59ecSDuan Jiong 	struct dst_entry *dst;
31600b26fb17SMaciej Żenczykowski 	struct flowi6 fl6 = {
31610b26fb17SMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
31620b26fb17SMaciej Żenczykowski 		.flowi6_oif = oif,
31630b26fb17SMaciej Żenczykowski 		.daddr = msg->dest,
31640b26fb17SMaciej Żenczykowski 		.saddr = iph->daddr,
31650b26fb17SMaciej Żenczykowski 		.flowi6_uid = sock_net_uid(net, NULL),
31660b26fb17SMaciej Żenczykowski 	};
3167c92a59ecSDuan Jiong 
3168b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
3169c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
3170c92a59ecSDuan Jiong 	dst_release(dst);
3171c92a59ecSDuan Jiong }
3172c92a59ecSDuan Jiong 
31733a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
31743a5ad2eeSDavid S. Miller {
3175e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3176e2d118a1SLorenzo Colitti 		     sk->sk_uid);
31773a5ad2eeSDavid S. Miller }
31783a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
31793a5ad2eeSDavid S. Miller 
31800dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
31811da177e4SLinus Torvalds {
31820dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
31830dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
31840dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
31850dbaee3bSDavid S. Miller 
31861da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
31871da177e4SLinus Torvalds 
31885578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
31895578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
31901da177e4SLinus Torvalds 
31911da177e4SLinus Torvalds 	/*
31921da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
31931da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
31941da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
31951da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
31961da177e4SLinus Torvalds 	 */
31971da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
31981da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
31991da177e4SLinus Torvalds 	return mtu;
32001da177e4SLinus Torvalds }
32011da177e4SLinus Torvalds 
3202f67fbeaeSBrian Vazquez INDIRECT_CALLABLE_SCOPE unsigned int ip6_mtu(const struct dst_entry *dst)
3203d33e4553SDavid S. Miller {
3204d33e4553SDavid S. Miller 	struct inet6_dev *idev;
3205d4ead6b3SDavid Ahern 	unsigned int mtu;
3206618f9bc7SSteffen Klassert 
32074b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
32084b32b5adSMartin KaFai Lau 	if (mtu)
32094b32b5adSMartin KaFai Lau 		goto out;
32104b32b5adSMartin KaFai Lau 
3211618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
3212d33e4553SDavid S. Miller 
3213d33e4553SDavid S. Miller 	rcu_read_lock();
3214d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
3215d33e4553SDavid S. Miller 	if (idev)
3216d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
3217d33e4553SDavid S. Miller 	rcu_read_unlock();
3218d33e4553SDavid S. Miller 
321930f78d8eSEric Dumazet out:
322014972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
322114972cbdSRoopa Prabhu 
322214972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
3223d33e4553SDavid S. Miller }
32249c97921aSBrian Vazquez EXPORT_INDIRECT_CALLABLE(ip6_mtu);
3225d33e4553SDavid S. Miller 
3226901731b8SDavid Ahern /* MTU selection:
3227901731b8SDavid Ahern  * 1. mtu on route is locked - use it
3228901731b8SDavid Ahern  * 2. mtu from nexthop exception
3229901731b8SDavid Ahern  * 3. mtu from egress device
3230901731b8SDavid Ahern  *
3231901731b8SDavid Ahern  * based on ip6_dst_mtu_forward and exception logic of
3232901731b8SDavid Ahern  * rt6_find_cached_rt; called with rcu_read_lock
3233901731b8SDavid Ahern  */
3234b748f260SDavid Ahern u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3235b748f260SDavid Ahern 		      const struct in6_addr *daddr,
3236b748f260SDavid Ahern 		      const struct in6_addr *saddr)
3237901731b8SDavid Ahern {
3238b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
3239b748f260SDavid Ahern 	struct fib6_info *f6i = res->f6i;
3240901731b8SDavid Ahern 	struct inet6_dev *idev;
3241510e2cedSWei Wang 	struct rt6_info *rt;
3242901731b8SDavid Ahern 	u32 mtu = 0;
3243901731b8SDavid Ahern 
3244901731b8SDavid Ahern 	if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3245901731b8SDavid Ahern 		mtu = f6i->fib6_pmtu;
3246901731b8SDavid Ahern 		if (mtu)
3247901731b8SDavid Ahern 			goto out;
3248901731b8SDavid Ahern 	}
3249901731b8SDavid Ahern 
3250510e2cedSWei Wang 	rt = rt6_find_cached_rt(res, daddr, saddr);
3251510e2cedSWei Wang 	if (unlikely(rt)) {
3252510e2cedSWei Wang 		mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3253510e2cedSWei Wang 	} else {
3254b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
3255901731b8SDavid Ahern 
3256901731b8SDavid Ahern 		mtu = IPV6_MIN_MTU;
3257901731b8SDavid Ahern 		idev = __in6_dev_get(dev);
3258901731b8SDavid Ahern 		if (idev && idev->cnf.mtu6 > mtu)
3259901731b8SDavid Ahern 			mtu = idev->cnf.mtu6;
3260901731b8SDavid Ahern 	}
3261901731b8SDavid Ahern 
3262901731b8SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3263901731b8SDavid Ahern out:
3264b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
3265901731b8SDavid Ahern }
3266901731b8SDavid Ahern 
32673b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
326887a11578SDavid S. Miller 				  struct flowi6 *fl6)
32691da177e4SLinus Torvalds {
327087a11578SDavid S. Miller 	struct dst_entry *dst;
32711da177e4SLinus Torvalds 	struct rt6_info *rt;
32721da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
3273c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
32741da177e4SLinus Torvalds 
327538308473SDavid S. Miller 	if (unlikely(!idev))
3276122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
32771da177e4SLinus Torvalds 
3278ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
327938308473SDavid S. Miller 	if (unlikely(!rt)) {
32801da177e4SLinus Torvalds 		in6_dev_put(idev);
328187a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
32821da177e4SLinus Torvalds 		goto out;
32831da177e4SLinus Torvalds 	}
32841da177e4SLinus Torvalds 
3285588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
32868e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
3287550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
328887a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
32898e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
32908e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
329114edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
32921da177e4SLinus Torvalds 
32934c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
3294587fea74SWei Wang 	 * do proper release of the net_device
3295587fea74SWei Wang 	 */
3296587fea74SWei Wang 	rt6_uncached_list_add(rt);
329781eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
32981da177e4SLinus Torvalds 
329987a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
330087a11578SDavid S. Miller 
33011da177e4SLinus Torvalds out:
330287a11578SDavid S. Miller 	return dst;
33031da177e4SLinus Torvalds }
33041da177e4SLinus Torvalds 
3305569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
33061da177e4SLinus Torvalds {
330786393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
33087019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
33097019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
33107019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
33117019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
33127019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
3313fc66f95cSEric Dumazet 	int entries;
33141da177e4SLinus Torvalds 
3315fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
3316cf86a086SEric Dumazet 	if (entries > rt_max_size)
3317cf86a086SEric Dumazet 		entries = dst_entries_get_slow(ops);
3318cf86a086SEric Dumazet 
331949a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
3320fc66f95cSEric Dumazet 	    entries <= rt_max_size)
33211da177e4SLinus Torvalds 		goto out;
33221da177e4SLinus Torvalds 
33236891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
332414956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
3325fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
3326fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
33277019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
33281da177e4SLinus Torvalds out:
33297019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
3330fc66f95cSEric Dumazet 	return entries > rt_max_size;
33311da177e4SLinus Torvalds }
33321da177e4SLinus Torvalds 
3333b2c709ccSDavid Ahern static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3334b2c709ccSDavid Ahern 			       const struct in6_addr *gw_addr, u32 tbid,
3335b2c709ccSDavid Ahern 			       int flags, struct fib6_result *res)
33368c14586fSDavid Ahern {
33378c14586fSDavid Ahern 	struct flowi6 fl6 = {
33388c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
33398c14586fSDavid Ahern 		.daddr = *gw_addr,
33408c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
33418c14586fSDavid Ahern 	};
33428c14586fSDavid Ahern 	struct fib6_table *table;
3343b2c709ccSDavid Ahern 	int err;
33448c14586fSDavid Ahern 
3345f4797b33SDavid Ahern 	table = fib6_get_table(net, tbid);
33468c14586fSDavid Ahern 	if (!table)
3347b2c709ccSDavid Ahern 		return -EINVAL;
33488c14586fSDavid Ahern 
33498c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
33508c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
33518c14586fSDavid Ahern 
3352f4797b33SDavid Ahern 	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
33538c14586fSDavid Ahern 
3354b2c709ccSDavid Ahern 	err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3355b2c709ccSDavid Ahern 	if (!err && res->f6i != net->ipv6.fib6_null_entry)
3356b2c709ccSDavid Ahern 		fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3357b2c709ccSDavid Ahern 				 cfg->fc_ifindex != 0, NULL, flags);
33588c14586fSDavid Ahern 
3359b2c709ccSDavid Ahern 	return err;
33608c14586fSDavid Ahern }
33618c14586fSDavid Ahern 
3362fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net,
3363fc1e64e1SDavid Ahern 				     struct fib6_config *cfg,
33649fbb704cSDavid Ahern 				     const struct net_device *dev,
3365fc1e64e1SDavid Ahern 				     struct netlink_ext_ack *extack)
3366fc1e64e1SDavid Ahern {
3367b2c709ccSDavid Ahern 	u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
3368fc1e64e1SDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
3369b2c709ccSDavid Ahern 	struct fib6_result res = {};
3370fc1e64e1SDavid Ahern 	int err;
3371fc1e64e1SDavid Ahern 
3372b2c709ccSDavid Ahern 	err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3373b2c709ccSDavid Ahern 	if (!err && !(res.fib6_flags & RTF_REJECT) &&
33744ed591c8SDavid Ahern 	    /* ignore match if it is the default route */
3375b2c709ccSDavid Ahern 	    !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3376b2c709ccSDavid Ahern 	    (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
337744750f84SDavid Ahern 		NL_SET_ERR_MSG(extack,
337844750f84SDavid Ahern 			       "Nexthop has invalid gateway or device mismatch");
3379fc1e64e1SDavid Ahern 		err = -EINVAL;
3380fc1e64e1SDavid Ahern 	}
3381fc1e64e1SDavid Ahern 
3382fc1e64e1SDavid Ahern 	return err;
3383fc1e64e1SDavid Ahern }
3384fc1e64e1SDavid Ahern 
33851edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net,
33861edce99fSDavid Ahern 			      struct fib6_config *cfg,
33871edce99fSDavid Ahern 			      struct net_device **_dev,
33881edce99fSDavid Ahern 			      struct inet6_dev **idev)
33891edce99fSDavid Ahern {
33901edce99fSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
33911edce99fSDavid Ahern 	struct net_device *dev = _dev ? *_dev : NULL;
3392b2c709ccSDavid Ahern 	int flags = RT6_LOOKUP_F_IFACE;
3393b2c709ccSDavid Ahern 	struct fib6_result res = {};
33941edce99fSDavid Ahern 	int err = -EHOSTUNREACH;
33951edce99fSDavid Ahern 
33961edce99fSDavid Ahern 	if (cfg->fc_table) {
3397b2c709ccSDavid Ahern 		err = ip6_nh_lookup_table(net, cfg, gw_addr,
3398b2c709ccSDavid Ahern 					  cfg->fc_table, flags, &res);
3399b2c709ccSDavid Ahern 		/* gw_addr can not require a gateway or resolve to a reject
3400b2c709ccSDavid Ahern 		 * route. If a device is given, it must match the result.
3401b2c709ccSDavid Ahern 		 */
3402b2c709ccSDavid Ahern 		if (err || res.fib6_flags & RTF_REJECT ||
3403b2c709ccSDavid Ahern 		    res.nh->fib_nh_gw_family ||
3404b2c709ccSDavid Ahern 		    (dev && dev != res.nh->fib_nh_dev))
3405b2c709ccSDavid Ahern 			err = -EHOSTUNREACH;
34061edce99fSDavid Ahern 	}
34071edce99fSDavid Ahern 
3408b2c709ccSDavid Ahern 	if (err < 0) {
3409b2c709ccSDavid Ahern 		struct flowi6 fl6 = {
3410b2c709ccSDavid Ahern 			.flowi6_oif = cfg->fc_ifindex,
3411b2c709ccSDavid Ahern 			.daddr = *gw_addr,
3412b2c709ccSDavid Ahern 		};
34131edce99fSDavid Ahern 
3414b2c709ccSDavid Ahern 		err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3415b2c709ccSDavid Ahern 		if (err || res.fib6_flags & RTF_REJECT ||
3416b2c709ccSDavid Ahern 		    res.nh->fib_nh_gw_family)
3417b2c709ccSDavid Ahern 			err = -EHOSTUNREACH;
34181edce99fSDavid Ahern 
3419b2c709ccSDavid Ahern 		if (err)
3420b2c709ccSDavid Ahern 			return err;
3421b2c709ccSDavid Ahern 
3422b2c709ccSDavid Ahern 		fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3423b2c709ccSDavid Ahern 				 cfg->fc_ifindex != 0, NULL, flags);
34241edce99fSDavid Ahern 	}
34251edce99fSDavid Ahern 
34261edce99fSDavid Ahern 	err = 0;
3427b2c709ccSDavid Ahern 	if (dev) {
3428b2c709ccSDavid Ahern 		if (dev != res.nh->fib_nh_dev)
3429b2c709ccSDavid Ahern 			err = -EHOSTUNREACH;
3430b2c709ccSDavid Ahern 	} else {
3431b2c709ccSDavid Ahern 		*_dev = dev = res.nh->fib_nh_dev;
3432b2c709ccSDavid Ahern 		dev_hold(dev);
3433b2c709ccSDavid Ahern 		*idev = in6_dev_get(dev);
3434b2c709ccSDavid Ahern 	}
34351edce99fSDavid Ahern 
34361edce99fSDavid Ahern 	return err;
34371edce99fSDavid Ahern }
34381edce99fSDavid Ahern 
34399fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
34409fbb704cSDavid Ahern 			   struct net_device **_dev, struct inet6_dev **idev,
34419fbb704cSDavid Ahern 			   struct netlink_ext_ack *extack)
34429fbb704cSDavid Ahern {
34439fbb704cSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
34449fbb704cSDavid Ahern 	int gwa_type = ipv6_addr_type(gw_addr);
3445232378e8SDavid Ahern 	bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
34469fbb704cSDavid Ahern 	const struct net_device *dev = *_dev;
3447232378e8SDavid Ahern 	bool need_addr_check = !dev;
34489fbb704cSDavid Ahern 	int err = -EINVAL;
34499fbb704cSDavid Ahern 
34509fbb704cSDavid Ahern 	/* if gw_addr is local we will fail to detect this in case
34519fbb704cSDavid Ahern 	 * address is still TENTATIVE (DAD in progress). rt6_lookup()
34529fbb704cSDavid Ahern 	 * will return already-added prefix route via interface that
34539fbb704cSDavid Ahern 	 * prefix route was assigned to, which might be non-loopback.
34549fbb704cSDavid Ahern 	 */
3455232378e8SDavid Ahern 	if (dev &&
3456232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3457232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
34589fbb704cSDavid Ahern 		goto out;
34599fbb704cSDavid Ahern 	}
34609fbb704cSDavid Ahern 
34619fbb704cSDavid Ahern 	if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
34629fbb704cSDavid Ahern 		/* IPv6 strictly inhibits using not link-local
34639fbb704cSDavid Ahern 		 * addresses as nexthop address.
34649fbb704cSDavid Ahern 		 * Otherwise, router will not able to send redirects.
34659fbb704cSDavid Ahern 		 * It is very good, but in some (rare!) circumstances
34669fbb704cSDavid Ahern 		 * (SIT, PtP, NBMA NOARP links) it is handy to allow
34679fbb704cSDavid Ahern 		 * some exceptions. --ANK
34689fbb704cSDavid Ahern 		 * We allow IPv4-mapped nexthops to support RFC4798-type
34699fbb704cSDavid Ahern 		 * addressing
34709fbb704cSDavid Ahern 		 */
34719fbb704cSDavid Ahern 		if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
34729fbb704cSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
34739fbb704cSDavid Ahern 			goto out;
34749fbb704cSDavid Ahern 		}
34759fbb704cSDavid Ahern 
3476b2c709ccSDavid Ahern 		rcu_read_lock();
3477b2c709ccSDavid Ahern 
34789fbb704cSDavid Ahern 		if (cfg->fc_flags & RTNH_F_ONLINK)
34799fbb704cSDavid Ahern 			err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
34809fbb704cSDavid Ahern 		else
34819fbb704cSDavid Ahern 			err = ip6_route_check_nh(net, cfg, _dev, idev);
34829fbb704cSDavid Ahern 
3483b2c709ccSDavid Ahern 		rcu_read_unlock();
3484b2c709ccSDavid Ahern 
34859fbb704cSDavid Ahern 		if (err)
34869fbb704cSDavid Ahern 			goto out;
34879fbb704cSDavid Ahern 	}
34889fbb704cSDavid Ahern 
34899fbb704cSDavid Ahern 	/* reload in case device was changed */
34909fbb704cSDavid Ahern 	dev = *_dev;
34919fbb704cSDavid Ahern 
34929fbb704cSDavid Ahern 	err = -EINVAL;
34939fbb704cSDavid Ahern 	if (!dev) {
34949fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Egress device not specified");
34959fbb704cSDavid Ahern 		goto out;
34969fbb704cSDavid Ahern 	} else if (dev->flags & IFF_LOOPBACK) {
34979fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack,
34989fbb704cSDavid Ahern 			       "Egress device can not be loopback device for this route");
34999fbb704cSDavid Ahern 		goto out;
35009fbb704cSDavid Ahern 	}
3501232378e8SDavid Ahern 
3502232378e8SDavid Ahern 	/* if we did not check gw_addr above, do so now that the
3503232378e8SDavid Ahern 	 * egress device has been resolved.
3504232378e8SDavid Ahern 	 */
3505232378e8SDavid Ahern 	if (need_addr_check &&
3506232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3507232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3508232378e8SDavid Ahern 		goto out;
3509232378e8SDavid Ahern 	}
3510232378e8SDavid Ahern 
35119fbb704cSDavid Ahern 	err = 0;
35129fbb704cSDavid Ahern out:
35139fbb704cSDavid Ahern 	return err;
35149fbb704cSDavid Ahern }
35159fbb704cSDavid Ahern 
351683c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
351783c44251SDavid Ahern {
351883c44251SDavid Ahern 	if ((flags & RTF_REJECT) ||
351983c44251SDavid Ahern 	    (dev && (dev->flags & IFF_LOOPBACK) &&
352083c44251SDavid Ahern 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
3521aea23c32SDavid Ahern 	     !(flags & (RTF_ANYCAST | RTF_LOCAL))))
352283c44251SDavid Ahern 		return true;
352383c44251SDavid Ahern 
352483c44251SDavid Ahern 	return false;
352583c44251SDavid Ahern }
352683c44251SDavid Ahern 
352783c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
352883c44251SDavid Ahern 		 struct fib6_config *cfg, gfp_t gfp_flags,
352983c44251SDavid Ahern 		 struct netlink_ext_ack *extack)
353083c44251SDavid Ahern {
353183c44251SDavid Ahern 	struct net_device *dev = NULL;
353283c44251SDavid Ahern 	struct inet6_dev *idev = NULL;
353383c44251SDavid Ahern 	int addr_type;
353483c44251SDavid Ahern 	int err;
353583c44251SDavid Ahern 
3536f1741730SDavid Ahern 	fib6_nh->fib_nh_family = AF_INET6;
35371bef4c22SEric Dumazet #ifdef CONFIG_IPV6_ROUTER_PREF
35381bef4c22SEric Dumazet 	fib6_nh->last_probe = jiffies;
35391bef4c22SEric Dumazet #endif
354038428d68SRoopa Prabhu 	if (cfg->fc_is_fdb) {
354138428d68SRoopa Prabhu 		fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
354238428d68SRoopa Prabhu 		fib6_nh->fib_nh_gw_family = AF_INET6;
354338428d68SRoopa Prabhu 		return 0;
354438428d68SRoopa Prabhu 	}
3545f1741730SDavid Ahern 
354683c44251SDavid Ahern 	err = -ENODEV;
354783c44251SDavid Ahern 	if (cfg->fc_ifindex) {
354883c44251SDavid Ahern 		dev = dev_get_by_index(net, cfg->fc_ifindex);
354983c44251SDavid Ahern 		if (!dev)
355083c44251SDavid Ahern 			goto out;
355183c44251SDavid Ahern 		idev = in6_dev_get(dev);
355283c44251SDavid Ahern 		if (!idev)
355383c44251SDavid Ahern 			goto out;
355483c44251SDavid Ahern 	}
355583c44251SDavid Ahern 
355683c44251SDavid Ahern 	if (cfg->fc_flags & RTNH_F_ONLINK) {
355783c44251SDavid Ahern 		if (!dev) {
355883c44251SDavid Ahern 			NL_SET_ERR_MSG(extack,
355983c44251SDavid Ahern 				       "Nexthop device required for onlink");
356083c44251SDavid Ahern 			goto out;
356183c44251SDavid Ahern 		}
356283c44251SDavid Ahern 
356383c44251SDavid Ahern 		if (!(dev->flags & IFF_UP)) {
356483c44251SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
356583c44251SDavid Ahern 			err = -ENETDOWN;
356683c44251SDavid Ahern 			goto out;
356783c44251SDavid Ahern 		}
356883c44251SDavid Ahern 
3569ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
357083c44251SDavid Ahern 	}
357183c44251SDavid Ahern 
3572ad1601aeSDavid Ahern 	fib6_nh->fib_nh_weight = 1;
357383c44251SDavid Ahern 
357483c44251SDavid Ahern 	/* We cannot add true routes via loopback here,
357583c44251SDavid Ahern 	 * they would result in kernel looping; promote them to reject routes
357683c44251SDavid Ahern 	 */
357783c44251SDavid Ahern 	addr_type = ipv6_addr_type(&cfg->fc_dst);
357883c44251SDavid Ahern 	if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
357983c44251SDavid Ahern 		/* hold loopback dev/idev if we haven't done so. */
358083c44251SDavid Ahern 		if (dev != net->loopback_dev) {
358183c44251SDavid Ahern 			if (dev) {
358283c44251SDavid Ahern 				dev_put(dev);
358383c44251SDavid Ahern 				in6_dev_put(idev);
358483c44251SDavid Ahern 			}
358583c44251SDavid Ahern 			dev = net->loopback_dev;
358683c44251SDavid Ahern 			dev_hold(dev);
358783c44251SDavid Ahern 			idev = in6_dev_get(dev);
358883c44251SDavid Ahern 			if (!idev) {
358983c44251SDavid Ahern 				err = -ENODEV;
359083c44251SDavid Ahern 				goto out;
359183c44251SDavid Ahern 			}
359283c44251SDavid Ahern 		}
35937dd73168SDavid Ahern 		goto pcpu_alloc;
359483c44251SDavid Ahern 	}
359583c44251SDavid Ahern 
359683c44251SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY) {
359783c44251SDavid Ahern 		err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
359883c44251SDavid Ahern 		if (err)
359983c44251SDavid Ahern 			goto out;
360083c44251SDavid Ahern 
3601ad1601aeSDavid Ahern 		fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3602bdf00467SDavid Ahern 		fib6_nh->fib_nh_gw_family = AF_INET6;
360383c44251SDavid Ahern 	}
360483c44251SDavid Ahern 
360583c44251SDavid Ahern 	err = -ENODEV;
360683c44251SDavid Ahern 	if (!dev)
360783c44251SDavid Ahern 		goto out;
360883c44251SDavid Ahern 
360983c44251SDavid Ahern 	if (idev->cnf.disable_ipv6) {
361083c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
361183c44251SDavid Ahern 		err = -EACCES;
361283c44251SDavid Ahern 		goto out;
361383c44251SDavid Ahern 	}
361483c44251SDavid Ahern 
361583c44251SDavid Ahern 	if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
361683c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
361783c44251SDavid Ahern 		err = -ENETDOWN;
361883c44251SDavid Ahern 		goto out;
361983c44251SDavid Ahern 	}
362083c44251SDavid Ahern 
362183c44251SDavid Ahern 	if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
362283c44251SDavid Ahern 	    !netif_carrier_ok(dev))
3623ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
362483c44251SDavid Ahern 
3625faee6769SAlexander Aring 	err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
36267dd73168SDavid Ahern 				 cfg->fc_encap_type, cfg, gfp_flags, extack);
36277dd73168SDavid Ahern 	if (err)
36287dd73168SDavid Ahern 		goto out;
36297dd73168SDavid Ahern 
36307dd73168SDavid Ahern pcpu_alloc:
3631f40b6ae2SDavid Ahern 	fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3632f40b6ae2SDavid Ahern 	if (!fib6_nh->rt6i_pcpu) {
3633f40b6ae2SDavid Ahern 		err = -ENOMEM;
3634f40b6ae2SDavid Ahern 		goto out;
3635f40b6ae2SDavid Ahern 	}
3636f40b6ae2SDavid Ahern 
3637ad1601aeSDavid Ahern 	fib6_nh->fib_nh_dev = dev;
3638f1741730SDavid Ahern 	fib6_nh->fib_nh_oif = dev->ifindex;
363983c44251SDavid Ahern 	err = 0;
364083c44251SDavid Ahern out:
364183c44251SDavid Ahern 	if (idev)
364283c44251SDavid Ahern 		in6_dev_put(idev);
364383c44251SDavid Ahern 
364483c44251SDavid Ahern 	if (err) {
3645ad1601aeSDavid Ahern 		lwtstate_put(fib6_nh->fib_nh_lws);
3646ad1601aeSDavid Ahern 		fib6_nh->fib_nh_lws = NULL;
364783c44251SDavid Ahern 		if (dev)
364883c44251SDavid Ahern 			dev_put(dev);
364983c44251SDavid Ahern 	}
365083c44251SDavid Ahern 
365183c44251SDavid Ahern 	return err;
365283c44251SDavid Ahern }
365383c44251SDavid Ahern 
3654dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh)
3655dac7d0f2SDavid Ahern {
3656cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
3657cc5c073aSDavid Ahern 
3658cc5c073aSDavid Ahern 	rcu_read_lock();
3659cc5c073aSDavid Ahern 
3660cc5c073aSDavid Ahern 	fib6_nh_flush_exceptions(fib6_nh, NULL);
3661cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3662cc5c073aSDavid Ahern 	if (bucket) {
3663cc5c073aSDavid Ahern 		rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3664cc5c073aSDavid Ahern 		kfree(bucket);
3665cc5c073aSDavid Ahern 	}
3666cc5c073aSDavid Ahern 
3667cc5c073aSDavid Ahern 	rcu_read_unlock();
3668cc5c073aSDavid Ahern 
3669f40b6ae2SDavid Ahern 	if (fib6_nh->rt6i_pcpu) {
3670f40b6ae2SDavid Ahern 		int cpu;
3671f40b6ae2SDavid Ahern 
3672f40b6ae2SDavid Ahern 		for_each_possible_cpu(cpu) {
3673f40b6ae2SDavid Ahern 			struct rt6_info **ppcpu_rt;
3674f40b6ae2SDavid Ahern 			struct rt6_info *pcpu_rt;
3675f40b6ae2SDavid Ahern 
3676f40b6ae2SDavid Ahern 			ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3677f40b6ae2SDavid Ahern 			pcpu_rt = *ppcpu_rt;
3678f40b6ae2SDavid Ahern 			if (pcpu_rt) {
3679f40b6ae2SDavid Ahern 				dst_dev_put(&pcpu_rt->dst);
3680f40b6ae2SDavid Ahern 				dst_release(&pcpu_rt->dst);
3681f40b6ae2SDavid Ahern 				*ppcpu_rt = NULL;
3682f40b6ae2SDavid Ahern 			}
3683f40b6ae2SDavid Ahern 		}
3684f40b6ae2SDavid Ahern 
3685f40b6ae2SDavid Ahern 		free_percpu(fib6_nh->rt6i_pcpu);
3686f40b6ae2SDavid Ahern 	}
3687f40b6ae2SDavid Ahern 
3688979e276eSDavid Ahern 	fib_nh_common_release(&fib6_nh->nh_common);
3689dac7d0f2SDavid Ahern }
3690dac7d0f2SDavid Ahern 
36918d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3692acb54e3cSDavid Ahern 					      gfp_t gfp_flags,
3693333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
36941da177e4SLinus Torvalds {
36955578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
36968d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3697f88d8ea6SDavid Ahern 	struct nexthop *nh = NULL;
3698c71099acSThomas Graf 	struct fib6_table *table;
3699f88d8ea6SDavid Ahern 	struct fib6_nh *fib6_nh;
37008c5b83f0SRoopa Prabhu 	int err = -EINVAL;
370183c44251SDavid Ahern 	int addr_type;
37021da177e4SLinus Torvalds 
3703557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
3704d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
3705d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3706557c44beSDavid Ahern 		goto out;
3707d5d531cbSDavid Ahern 	}
3708557c44beSDavid Ahern 
37092ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
37102ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
37112ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
37122ea2352eSWei Wang 		goto out;
37132ea2352eSWei Wang 	}
37142ea2352eSWei Wang 
3715e8478e80SDavid Ahern 	if (cfg->fc_type > RTN_MAX) {
3716e8478e80SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid route type");
3717e8478e80SDavid Ahern 		goto out;
3718e8478e80SDavid Ahern 	}
3719e8478e80SDavid Ahern 
3720d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
3721d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
37228c5b83f0SRoopa Prabhu 		goto out;
3723d5d531cbSDavid Ahern 	}
3724d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
3725d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
3726d5d531cbSDavid Ahern 		goto out;
3727d5d531cbSDavid Ahern 	}
37281da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
3729d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
3730d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
3731d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
37328c5b83f0SRoopa Prabhu 		goto out;
3733d5d531cbSDavid Ahern 	}
37341da177e4SLinus Torvalds #endif
37355b98324eSDavid Ahern 	if (cfg->fc_nh_id) {
37365b98324eSDavid Ahern 		nh = nexthop_find_by_id(net, cfg->fc_nh_id);
37375b98324eSDavid Ahern 		if (!nh) {
37385b98324eSDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
37395b98324eSDavid Ahern 			goto out;
37405b98324eSDavid Ahern 		}
37415b98324eSDavid Ahern 		err = fib6_check_nexthop(nh, cfg, extack);
37425b98324eSDavid Ahern 		if (err)
37435b98324eSDavid Ahern 			goto out;
37445b98324eSDavid Ahern 	}
3745fc1e64e1SDavid Ahern 
3746c71099acSThomas Graf 	err = -ENOBUFS;
374738308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
3748d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3749d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
375038308473SDavid S. Miller 		if (!table) {
3751f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3752d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
3753d71314b4SMatti Vaittinen 		}
3754d71314b4SMatti Vaittinen 	} else {
3755d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
3756d71314b4SMatti Vaittinen 	}
375738308473SDavid S. Miller 
375838308473SDavid S. Miller 	if (!table)
3759c71099acSThomas Graf 		goto out;
3760c71099acSThomas Graf 
37611da177e4SLinus Torvalds 	err = -ENOMEM;
3762f88d8ea6SDavid Ahern 	rt = fib6_info_alloc(gfp_flags, !nh);
376393531c67SDavid Ahern 	if (!rt)
37641da177e4SLinus Torvalds 		goto out;
376593531c67SDavid Ahern 
3766d7e774f3SDavid Ahern 	rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3767d7e774f3SDavid Ahern 					       extack);
3768767a2217SDavid Ahern 	if (IS_ERR(rt->fib6_metrics)) {
3769767a2217SDavid Ahern 		err = PTR_ERR(rt->fib6_metrics);
3770fda21d46SEric Dumazet 		/* Do not leave garbage there. */
3771fda21d46SEric Dumazet 		rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3772767a2217SDavid Ahern 		goto out;
3773767a2217SDavid Ahern 	}
3774767a2217SDavid Ahern 
377593531c67SDavid Ahern 	if (cfg->fc_flags & RTF_ADDRCONF)
377693531c67SDavid Ahern 		rt->dst_nocount = true;
37771da177e4SLinus Torvalds 
37781716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
377914895687SDavid Ahern 		fib6_set_expires(rt, jiffies +
37801716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
37811716a961SGao feng 	else
378214895687SDavid Ahern 		fib6_clean_expires(rt);
37831da177e4SLinus Torvalds 
378486872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
378586872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
378693c2fb25SDavid Ahern 	rt->fib6_protocol = cfg->fc_protocol;
378786872cb5SThomas Graf 
378883c44251SDavid Ahern 	rt->fib6_table = table;
378983c44251SDavid Ahern 	rt->fib6_metric = cfg->fc_metric;
3790c7036d97SDavid Ahern 	rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
37912b2450caSDavid Ahern 	rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
379219e42e45SRoopa Prabhu 
379393c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
379493c2fb25SDavid Ahern 	rt->fib6_dst.plen = cfg->fc_dst_len;
37951da177e4SLinus Torvalds 
37961da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
379793c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
379893c2fb25SDavid Ahern 	rt->fib6_src.plen = cfg->fc_src_len;
37991da177e4SLinus Torvalds #endif
3800f88d8ea6SDavid Ahern 	if (nh) {
3801f88d8ea6SDavid Ahern 		if (rt->fib6_src.plen) {
38024daa95afSColin Ian King 			NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
3803f88d8ea6SDavid Ahern 			goto out;
3804f88d8ea6SDavid Ahern 		}
3805706ec919SXiyu Yang 		if (!nexthop_get(nh)) {
3806706ec919SXiyu Yang 			NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3807706ec919SXiyu Yang 			goto out;
3808706ec919SXiyu Yang 		}
3809f88d8ea6SDavid Ahern 		rt->nh = nh;
3810f88d8ea6SDavid Ahern 		fib6_nh = nexthop_fib6_nh(rt->nh);
3811f88d8ea6SDavid Ahern 	} else {
38121cf844c7SDavid Ahern 		err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
38131da177e4SLinus Torvalds 		if (err)
38141da177e4SLinus Torvalds 			goto out;
38159fbb704cSDavid Ahern 
3816f88d8ea6SDavid Ahern 		fib6_nh = rt->fib6_nh;
3817f88d8ea6SDavid Ahern 
3818f88d8ea6SDavid Ahern 		/* We cannot add true routes via loopback here, they would
3819f88d8ea6SDavid Ahern 		 * result in kernel looping; promote them to reject routes
382083c44251SDavid Ahern 		 */
382183c44251SDavid Ahern 		addr_type = ipv6_addr_type(&cfg->fc_dst);
3822f88d8ea6SDavid Ahern 		if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3823f88d8ea6SDavid Ahern 				   addr_type))
382483c44251SDavid Ahern 			rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3825f88d8ea6SDavid Ahern 	}
3826955ec4cbSDavid Ahern 
3827c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3828f88d8ea6SDavid Ahern 		struct net_device *dev = fib6_nh->fib_nh_dev;
382983c44251SDavid Ahern 
3830c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3831d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
3832c3968a85SDaniel Walter 			err = -EINVAL;
3833c3968a85SDaniel Walter 			goto out;
3834c3968a85SDaniel Walter 		}
383593c2fb25SDavid Ahern 		rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
383693c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 128;
3837c3968a85SDaniel Walter 	} else
383893c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
3839c3968a85SDaniel Walter 
38408c5b83f0SRoopa Prabhu 	return rt;
38411da177e4SLinus Torvalds out:
384293531c67SDavid Ahern 	fib6_info_release(rt);
38438c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
38446b9ea5a6SRoopa Prabhu }
38456b9ea5a6SRoopa Prabhu 
3846acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3847333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
38486b9ea5a6SRoopa Prabhu {
38498d1c802bSDavid Ahern 	struct fib6_info *rt;
38506b9ea5a6SRoopa Prabhu 	int err;
38516b9ea5a6SRoopa Prabhu 
3852acb54e3cSDavid Ahern 	rt = ip6_route_info_create(cfg, gfp_flags, extack);
3853d4ead6b3SDavid Ahern 	if (IS_ERR(rt))
3854d4ead6b3SDavid Ahern 		return PTR_ERR(rt);
38556b9ea5a6SRoopa Prabhu 
3856d4ead6b3SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
385793531c67SDavid Ahern 	fib6_info_release(rt);
38586b9ea5a6SRoopa Prabhu 
38591da177e4SLinus Torvalds 	return err;
38601da177e4SLinus Torvalds }
38611da177e4SLinus Torvalds 
38628d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
38631da177e4SLinus Torvalds {
3864afb1d4b5SDavid Ahern 	struct net *net = info->nl_net;
3865c71099acSThomas Graf 	struct fib6_table *table;
3866afb1d4b5SDavid Ahern 	int err;
38671da177e4SLinus Torvalds 
3868421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
38696825a26cSGao feng 		err = -ENOENT;
38706825a26cSGao feng 		goto out;
38716825a26cSGao feng 	}
38726c813a72SPatrick McHardy 
387393c2fb25SDavid Ahern 	table = rt->fib6_table;
387466f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
387586872cb5SThomas Graf 	err = fib6_del(rt, info);
387666f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
38771da177e4SLinus Torvalds 
38786825a26cSGao feng out:
387993531c67SDavid Ahern 	fib6_info_release(rt);
38801da177e4SLinus Torvalds 	return err;
38811da177e4SLinus Torvalds }
38821da177e4SLinus Torvalds 
388311dd74b3SRoopa Prabhu int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
3884e0a1ad73SThomas Graf {
388511dd74b3SRoopa Prabhu 	struct nl_info info = {
388611dd74b3SRoopa Prabhu 		.nl_net = net,
388711dd74b3SRoopa Prabhu 		.skip_notify = skip_notify
388811dd74b3SRoopa Prabhu 	};
3889afb1d4b5SDavid Ahern 
3890528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
3891e0a1ad73SThomas Graf }
3892e0a1ad73SThomas Graf 
38938d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
38940ae81335SDavid Ahern {
38950ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
3896e3330039SWANG Cong 	struct net *net = info->nl_net;
389716a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
38980ae81335SDavid Ahern 	struct fib6_table *table;
3899e3330039SWANG Cong 	int err = -ENOENT;
39000ae81335SDavid Ahern 
3901421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
3902e3330039SWANG Cong 		goto out_put;
390393c2fb25SDavid Ahern 	table = rt->fib6_table;
390466f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
39050ae81335SDavid Ahern 
390693c2fb25SDavid Ahern 	if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
39078d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
39080284696bSIdo Schimmel 		struct fib6_node *fn;
39090ae81335SDavid Ahern 
391016a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
391116a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
391216a16cd3SDavid Ahern 		if (skb) {
391316a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
391416a16cd3SDavid Ahern 
3915d4ead6b3SDavid Ahern 			if (rt6_fill_node(net, skb, rt, NULL,
391616a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
391716a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
391816a16cd3SDavid Ahern 				kfree_skb(skb);
391916a16cd3SDavid Ahern 				skb = NULL;
392016a16cd3SDavid Ahern 			} else
392116a16cd3SDavid Ahern 				info->skip_notify = 1;
392216a16cd3SDavid Ahern 		}
392316a16cd3SDavid Ahern 
39240284696bSIdo Schimmel 		/* 'rt' points to the first sibling route. If it is not the
39250284696bSIdo Schimmel 		 * leaf, then we do not need to send a notification. Otherwise,
39260284696bSIdo Schimmel 		 * we need to check if the last sibling has a next route or not
39270284696bSIdo Schimmel 		 * and emit a replace or delete notification, respectively.
39280284696bSIdo Schimmel 		 */
39292881fd61SIdo Schimmel 		info->skip_notify_kernel = 1;
39300284696bSIdo Schimmel 		fn = rcu_dereference_protected(rt->fib6_node,
39310284696bSIdo Schimmel 					    lockdep_is_held(&table->tb6_lock));
39320284696bSIdo Schimmel 		if (rcu_access_pointer(fn->leaf) == rt) {
39330284696bSIdo Schimmel 			struct fib6_info *last_sibling, *replace_rt;
39340284696bSIdo Schimmel 
39350284696bSIdo Schimmel 			last_sibling = list_last_entry(&rt->fib6_siblings,
39360284696bSIdo Schimmel 						       struct fib6_info,
39370284696bSIdo Schimmel 						       fib6_siblings);
39380284696bSIdo Schimmel 			replace_rt = rcu_dereference_protected(
39390284696bSIdo Schimmel 					    last_sibling->fib6_next,
39400284696bSIdo Schimmel 					    lockdep_is_held(&table->tb6_lock));
39410284696bSIdo Schimmel 			if (replace_rt)
39420284696bSIdo Schimmel 				call_fib6_entry_notifiers_replace(net,
39430284696bSIdo Schimmel 								  replace_rt);
39440284696bSIdo Schimmel 			else
39450284696bSIdo Schimmel 				call_fib6_multipath_entry_notifiers(net,
3946caafb250SIdo Schimmel 						       FIB_EVENT_ENTRY_DEL,
39470284696bSIdo Schimmel 						       rt, rt->fib6_nsiblings,
39480284696bSIdo Schimmel 						       NULL);
39490284696bSIdo Schimmel 		}
39500ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
395193c2fb25SDavid Ahern 					 &rt->fib6_siblings,
395293c2fb25SDavid Ahern 					 fib6_siblings) {
39530ae81335SDavid Ahern 			err = fib6_del(sibling, info);
39540ae81335SDavid Ahern 			if (err)
3955e3330039SWANG Cong 				goto out_unlock;
39560ae81335SDavid Ahern 		}
39570ae81335SDavid Ahern 	}
39580ae81335SDavid Ahern 
39590ae81335SDavid Ahern 	err = fib6_del(rt, info);
3960e3330039SWANG Cong out_unlock:
396166f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
3962e3330039SWANG Cong out_put:
396393531c67SDavid Ahern 	fib6_info_release(rt);
396416a16cd3SDavid Ahern 
396516a16cd3SDavid Ahern 	if (skb) {
3966e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
396716a16cd3SDavid Ahern 			    info->nlh, gfp_any());
396816a16cd3SDavid Ahern 	}
39690ae81335SDavid Ahern 	return err;
39700ae81335SDavid Ahern }
39710ae81335SDavid Ahern 
39720fa6efc5SDavid Ahern static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
397323fb93a4SDavid Ahern {
397423fb93a4SDavid Ahern 	int rc = -ESRCH;
397523fb93a4SDavid Ahern 
397623fb93a4SDavid Ahern 	if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
397723fb93a4SDavid Ahern 		goto out;
397823fb93a4SDavid Ahern 
397923fb93a4SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY &&
398023fb93a4SDavid Ahern 	    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
398123fb93a4SDavid Ahern 		goto out;
3982761f6026SXin Long 
398323fb93a4SDavid Ahern 	rc = rt6_remove_exception_rt(rt);
398423fb93a4SDavid Ahern out:
398523fb93a4SDavid Ahern 	return rc;
398623fb93a4SDavid Ahern }
398723fb93a4SDavid Ahern 
39880fa6efc5SDavid Ahern static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
39890fa6efc5SDavid Ahern 			     struct fib6_nh *nh)
39900fa6efc5SDavid Ahern {
39910fa6efc5SDavid Ahern 	struct fib6_result res = {
39920fa6efc5SDavid Ahern 		.f6i = rt,
39930fa6efc5SDavid Ahern 		.nh = nh,
39940fa6efc5SDavid Ahern 	};
39950fa6efc5SDavid Ahern 	struct rt6_info *rt_cache;
39960fa6efc5SDavid Ahern 
39970fa6efc5SDavid Ahern 	rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
39980fa6efc5SDavid Ahern 	if (rt_cache)
39990fa6efc5SDavid Ahern 		return __ip6_del_cached_rt(rt_cache, cfg);
40000fa6efc5SDavid Ahern 
40010fa6efc5SDavid Ahern 	return 0;
40020fa6efc5SDavid Ahern }
40030fa6efc5SDavid Ahern 
40045b98324eSDavid Ahern struct fib6_nh_del_cached_rt_arg {
40055b98324eSDavid Ahern 	struct fib6_config *cfg;
40065b98324eSDavid Ahern 	struct fib6_info *f6i;
40075b98324eSDavid Ahern };
40085b98324eSDavid Ahern 
40095b98324eSDavid Ahern static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
40105b98324eSDavid Ahern {
40115b98324eSDavid Ahern 	struct fib6_nh_del_cached_rt_arg *arg = _arg;
40125b98324eSDavid Ahern 	int rc;
40135b98324eSDavid Ahern 
40145b98324eSDavid Ahern 	rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
40155b98324eSDavid Ahern 	return rc != -ESRCH ? rc : 0;
40165b98324eSDavid Ahern }
40175b98324eSDavid Ahern 
40185b98324eSDavid Ahern static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
40195b98324eSDavid Ahern {
40205b98324eSDavid Ahern 	struct fib6_nh_del_cached_rt_arg arg = {
40215b98324eSDavid Ahern 		.cfg = cfg,
40225b98324eSDavid Ahern 		.f6i = f6i
40235b98324eSDavid Ahern 	};
40245b98324eSDavid Ahern 
40255b98324eSDavid Ahern 	return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
40265b98324eSDavid Ahern }
40275b98324eSDavid Ahern 
4028333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
4029333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
40301da177e4SLinus Torvalds {
4031c71099acSThomas Graf 	struct fib6_table *table;
40328d1c802bSDavid Ahern 	struct fib6_info *rt;
40331da177e4SLinus Torvalds 	struct fib6_node *fn;
40341da177e4SLinus Torvalds 	int err = -ESRCH;
40351da177e4SLinus Torvalds 
40365578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
4037d5d531cbSDavid Ahern 	if (!table) {
4038d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
4039c71099acSThomas Graf 		return err;
4040d5d531cbSDavid Ahern 	}
40411da177e4SLinus Torvalds 
404266f5d6ceSWei Wang 	rcu_read_lock();
4043c71099acSThomas Graf 
4044c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
404586872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
404638fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
40472b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
40481da177e4SLinus Torvalds 
40491da177e4SLinus Torvalds 	if (fn) {
405066f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
4051ad1601aeSDavid Ahern 			struct fib6_nh *nh;
4052ad1601aeSDavid Ahern 
40533401bfb1SStefano Brivio 			if (rt->nh && cfg->fc_nh_id &&
40543401bfb1SStefano Brivio 			    rt->nh->id != cfg->fc_nh_id)
40555b98324eSDavid Ahern 				continue;
405623fb93a4SDavid Ahern 
40575b98324eSDavid Ahern 			if (cfg->fc_flags & RTF_CACHE) {
40585b98324eSDavid Ahern 				int rc = 0;
40595b98324eSDavid Ahern 
40605b98324eSDavid Ahern 				if (rt->nh) {
40615b98324eSDavid Ahern 					rc = ip6_del_cached_rt_nh(cfg, rt);
40625b98324eSDavid Ahern 				} else if (cfg->fc_nh_id) {
40635b98324eSDavid Ahern 					continue;
40645b98324eSDavid Ahern 				} else {
40655b98324eSDavid Ahern 					nh = rt->fib6_nh;
40660fa6efc5SDavid Ahern 					rc = ip6_del_cached_rt(cfg, rt, nh);
40675b98324eSDavid Ahern 				}
40689e575010SEric Dumazet 				if (rc != -ESRCH) {
40699e575010SEric Dumazet 					rcu_read_unlock();
407023fb93a4SDavid Ahern 					return rc;
407123fb93a4SDavid Ahern 				}
40721f56a01fSMartin KaFai Lau 				continue;
40732b760fcfSWei Wang 			}
4074ad1601aeSDavid Ahern 
40755b98324eSDavid Ahern 			if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
40765b98324eSDavid Ahern 				continue;
40775b98324eSDavid Ahern 			if (cfg->fc_protocol &&
40785b98324eSDavid Ahern 			    cfg->fc_protocol != rt->fib6_protocol)
40795b98324eSDavid Ahern 				continue;
40805b98324eSDavid Ahern 
40815b98324eSDavid Ahern 			if (rt->nh) {
40825b98324eSDavid Ahern 				if (!fib6_info_hold_safe(rt))
40835b98324eSDavid Ahern 					continue;
40845b98324eSDavid Ahern 				rcu_read_unlock();
40855b98324eSDavid Ahern 
40865b98324eSDavid Ahern 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
40875b98324eSDavid Ahern 			}
40885b98324eSDavid Ahern 			if (cfg->fc_nh_id)
40895b98324eSDavid Ahern 				continue;
40905b98324eSDavid Ahern 
40915b98324eSDavid Ahern 			nh = rt->fib6_nh;
409286872cb5SThomas Graf 			if (cfg->fc_ifindex &&
4093ad1601aeSDavid Ahern 			    (!nh->fib_nh_dev ||
4094ad1601aeSDavid Ahern 			     nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
40951da177e4SLinus Torvalds 				continue;
409686872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
4097ad1601aeSDavid Ahern 			    !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
40981da177e4SLinus Torvalds 				continue;
4099e873e4b9SWei Wang 			if (!fib6_info_hold_safe(rt))
4100e873e4b9SWei Wang 				continue;
410166f5d6ceSWei Wang 			rcu_read_unlock();
41021da177e4SLinus Torvalds 
41030ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
41040ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
410586872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
41060ae81335SDavid Ahern 
41070ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
41081da177e4SLinus Torvalds 		}
41091da177e4SLinus Torvalds 	}
411066f5d6ceSWei Wang 	rcu_read_unlock();
41111da177e4SLinus Torvalds 
41121da177e4SLinus Torvalds 	return err;
41131da177e4SLinus Torvalds }
41141da177e4SLinus Torvalds 
41156700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
4116a6279458SYOSHIFUJI Hideaki {
4117a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
4118e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
411985bd05deSDavid Ahern 	struct fib6_result res = {};
4120e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
4121e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
4122e8599ff4SDavid S. Miller 	struct neighbour *neigh;
412371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
41246e157b6aSDavid S. Miller 	int optlen, on_link;
41256e157b6aSDavid S. Miller 	u8 *lladdr;
4126e8599ff4SDavid S. Miller 
412729a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
412871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
4129e8599ff4SDavid S. Miller 
4130e8599ff4SDavid S. Miller 	if (optlen < 0) {
41316e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
4132e8599ff4SDavid S. Miller 		return;
4133e8599ff4SDavid S. Miller 	}
4134e8599ff4SDavid S. Miller 
413571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
4136e8599ff4SDavid S. Miller 
413771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
41386e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
4139e8599ff4SDavid S. Miller 		return;
4140e8599ff4SDavid S. Miller 	}
4141e8599ff4SDavid S. Miller 
41426e157b6aSDavid S. Miller 	on_link = 0;
414371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
4144e8599ff4SDavid S. Miller 		on_link = 1;
414571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
4146e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
41476e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
4148e8599ff4SDavid S. Miller 		return;
4149e8599ff4SDavid S. Miller 	}
4150e8599ff4SDavid S. Miller 
4151e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
4152e8599ff4SDavid S. Miller 	if (!in6_dev)
4153e8599ff4SDavid S. Miller 		return;
4154e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4155e8599ff4SDavid S. Miller 		return;
4156e8599ff4SDavid S. Miller 
4157e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
4158e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
4159e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
4160e8599ff4SDavid S. Miller 	 */
4161e8599ff4SDavid S. Miller 
4162f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
4163e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4164e8599ff4SDavid S. Miller 		return;
4165e8599ff4SDavid S. Miller 	}
41666e157b6aSDavid S. Miller 
41676e157b6aSDavid S. Miller 	lladdr = NULL;
4168e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
4169e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4170e8599ff4SDavid S. Miller 					     skb->dev);
4171e8599ff4SDavid S. Miller 		if (!lladdr) {
4172e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4173e8599ff4SDavid S. Miller 			return;
4174e8599ff4SDavid S. Miller 		}
4175e8599ff4SDavid S. Miller 	}
4176e8599ff4SDavid S. Miller 
41776e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
4178ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
41796e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
41806e157b6aSDavid S. Miller 		return;
41816e157b6aSDavid S. Miller 	}
41826e157b6aSDavid S. Miller 
41836e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
41846e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
41856e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
41866e157b6aSDavid S. Miller 	 */
41870dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
41886e157b6aSDavid S. Miller 
418971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
4190e8599ff4SDavid S. Miller 	if (!neigh)
4191e8599ff4SDavid S. Miller 		return;
4192e8599ff4SDavid S. Miller 
41931da177e4SLinus Torvalds 	/*
41941da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
41951da177e4SLinus Torvalds 	 */
41961da177e4SLinus Torvalds 
4197f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
41981da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
41991da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
42001da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
4201f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
4202f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
42031da177e4SLinus Torvalds 
42044d85cd0cSDavid Ahern 	rcu_read_lock();
420585bd05deSDavid Ahern 	res.f6i = rcu_dereference(rt->from);
4206ff24e498SDavid S. Miller 	if (!res.f6i)
4207886b7a50SMartin KaFai Lau 		goto out;
42088a14e46fSDavid Ahern 
420949d5b8efSDavid Ahern 	if (res.f6i->nh) {
421049d5b8efSDavid Ahern 		struct fib6_nh_match_arg arg = {
421149d5b8efSDavid Ahern 			.dev = dst->dev,
421249d5b8efSDavid Ahern 			.gw = &rt->rt6i_gateway,
421349d5b8efSDavid Ahern 		};
421449d5b8efSDavid Ahern 
421549d5b8efSDavid Ahern 		nexthop_for_each_fib6_nh(res.f6i->nh,
421649d5b8efSDavid Ahern 					 fib6_nh_find_match, &arg);
421749d5b8efSDavid Ahern 
421849d5b8efSDavid Ahern 		/* fib6_info uses a nexthop that does not have fib6_nh
421949d5b8efSDavid Ahern 		 * using the dst->dev. Should be impossible
422049d5b8efSDavid Ahern 		 */
422149d5b8efSDavid Ahern 		if (!arg.match)
422249d5b8efSDavid Ahern 			goto out;
422349d5b8efSDavid Ahern 		res.nh = arg.match;
422449d5b8efSDavid Ahern 	} else {
42251cf844c7SDavid Ahern 		res.nh = res.f6i->fib6_nh;
422649d5b8efSDavid Ahern 	}
422749d5b8efSDavid Ahern 
42287d21fec9SDavid Ahern 	res.fib6_flags = res.f6i->fib6_flags;
42297d21fec9SDavid Ahern 	res.fib6_type = res.f6i->fib6_type;
423085bd05deSDavid Ahern 	nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
423138308473SDavid S. Miller 	if (!nrt)
42321da177e4SLinus Torvalds 		goto out;
42331da177e4SLinus Torvalds 
42341da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
42351da177e4SLinus Torvalds 	if (on_link)
42361da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
42371da177e4SLinus Torvalds 
42384e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
42391da177e4SLinus Torvalds 
4240886b7a50SMartin KaFai Lau 	/* rt6_insert_exception() will take care of duplicated exceptions */
42415012f0a5SDavid Ahern 	if (rt6_insert_exception(nrt, &res)) {
42422b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
42432b760fcfSWei Wang 		goto out;
42442b760fcfSWei Wang 	}
42451da177e4SLinus Torvalds 
4246d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
4247d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
424871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
424960592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
42508d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
42518d71740cSTom Tucker 
42521da177e4SLinus Torvalds out:
4253886b7a50SMartin KaFai Lau 	rcu_read_unlock();
4254e8599ff4SDavid S. Miller 	neigh_release(neigh);
42556e157b6aSDavid S. Miller }
42566e157b6aSDavid S. Miller 
425770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
42588d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
4259b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
4260830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
4261830218c1SDavid Ahern 					   struct net_device *dev)
426270ceb4f5SYOSHIFUJI Hideaki {
4263830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4264830218c1SDavid Ahern 	int ifindex = dev->ifindex;
426570ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
42668d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
4267c71099acSThomas Graf 	struct fib6_table *table;
426870ceb4f5SYOSHIFUJI Hideaki 
4269830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
427038308473SDavid S. Miller 	if (!table)
4271c71099acSThomas Graf 		return NULL;
4272c71099acSThomas Graf 
427366f5d6ceSWei Wang 	rcu_read_lock();
427438fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
427570ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
427670ceb4f5SYOSHIFUJI Hideaki 		goto out;
427770ceb4f5SYOSHIFUJI Hideaki 
427866f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
4279f88d8ea6SDavid Ahern 		/* these routes do not use nexthops */
4280f88d8ea6SDavid Ahern 		if (rt->nh)
4281f88d8ea6SDavid Ahern 			continue;
42821cf844c7SDavid Ahern 		if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
428370ceb4f5SYOSHIFUJI Hideaki 			continue;
42842b2450caSDavid Ahern 		if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
42851cf844c7SDavid Ahern 		    !rt->fib6_nh->fib_nh_gw_family)
428670ceb4f5SYOSHIFUJI Hideaki 			continue;
42871cf844c7SDavid Ahern 		if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
428870ceb4f5SYOSHIFUJI Hideaki 			continue;
4289e873e4b9SWei Wang 		if (!fib6_info_hold_safe(rt))
4290e873e4b9SWei Wang 			continue;
429170ceb4f5SYOSHIFUJI Hideaki 		break;
429270ceb4f5SYOSHIFUJI Hideaki 	}
429370ceb4f5SYOSHIFUJI Hideaki out:
429466f5d6ceSWei Wang 	rcu_read_unlock();
429570ceb4f5SYOSHIFUJI Hideaki 	return rt;
429670ceb4f5SYOSHIFUJI Hideaki }
429770ceb4f5SYOSHIFUJI Hideaki 
42988d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
4299b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
4300830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
4301830218c1SDavid Ahern 					   struct net_device *dev,
430295c96174SEric Dumazet 					   unsigned int pref)
430370ceb4f5SYOSHIFUJI Hideaki {
430486872cb5SThomas Graf 	struct fib6_config cfg = {
4305238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
4306830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
430786872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
430886872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
430986872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
4310b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
4311e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
431215e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
4313efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
4314efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
431586872cb5SThomas Graf 	};
431670ceb4f5SYOSHIFUJI Hideaki 
431791b2c9a0SXu Wang 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
43184e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
43194e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
432086872cb5SThomas Graf 
4321e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
4322e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
432386872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
432470ceb4f5SYOSHIFUJI Hideaki 
4325acb54e3cSDavid Ahern 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
432670ceb4f5SYOSHIFUJI Hideaki 
4327830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
432870ceb4f5SYOSHIFUJI Hideaki }
432970ceb4f5SYOSHIFUJI Hideaki #endif
433070ceb4f5SYOSHIFUJI Hideaki 
43318d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net,
4332afb1d4b5SDavid Ahern 				     const struct in6_addr *addr,
4333afb1d4b5SDavid Ahern 				     struct net_device *dev)
43341da177e4SLinus Torvalds {
4335830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
43368d1c802bSDavid Ahern 	struct fib6_info *rt;
4337c71099acSThomas Graf 	struct fib6_table *table;
43381da177e4SLinus Torvalds 
4339afb1d4b5SDavid Ahern 	table = fib6_get_table(net, tb_id);
434038308473SDavid S. Miller 	if (!table)
4341c71099acSThomas Graf 		return NULL;
43421da177e4SLinus Torvalds 
434366f5d6ceSWei Wang 	rcu_read_lock();
434466f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
4345f88d8ea6SDavid Ahern 		struct fib6_nh *nh;
4346ad1601aeSDavid Ahern 
4347f88d8ea6SDavid Ahern 		/* RA routes do not use nexthops */
4348f88d8ea6SDavid Ahern 		if (rt->nh)
4349f88d8ea6SDavid Ahern 			continue;
4350f88d8ea6SDavid Ahern 
4351f88d8ea6SDavid Ahern 		nh = rt->fib6_nh;
4352ad1601aeSDavid Ahern 		if (dev == nh->fib_nh_dev &&
435393c2fb25SDavid Ahern 		    ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
4354ad1601aeSDavid Ahern 		    ipv6_addr_equal(&nh->fib_nh_gw6, addr))
43551da177e4SLinus Torvalds 			break;
43561da177e4SLinus Torvalds 	}
4357e873e4b9SWei Wang 	if (rt && !fib6_info_hold_safe(rt))
4358e873e4b9SWei Wang 		rt = NULL;
435966f5d6ceSWei Wang 	rcu_read_unlock();
43601da177e4SLinus Torvalds 	return rt;
43611da177e4SLinus Torvalds }
43621da177e4SLinus Torvalds 
43638d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net,
4364afb1d4b5SDavid Ahern 				     const struct in6_addr *gwaddr,
4365ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
43666b2e04bcSPraveen Chaudhary 				     unsigned int pref,
43676b2e04bcSPraveen Chaudhary 				     u32 defrtr_usr_metric)
43681da177e4SLinus Torvalds {
436986872cb5SThomas Graf 	struct fib6_config cfg = {
4370ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
43716b2e04bcSPraveen Chaudhary 		.fc_metric	= defrtr_usr_metric,
437286872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
437386872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
437486872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
4375b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
4376e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
437715e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
43785578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
4379afb1d4b5SDavid Ahern 		.fc_nlinfo.nl_net = net,
438086872cb5SThomas Graf 	};
43811da177e4SLinus Torvalds 
43824e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
43831da177e4SLinus Torvalds 
4384acb54e3cSDavid Ahern 	if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
4385830218c1SDavid Ahern 		struct fib6_table *table;
4386830218c1SDavid Ahern 
4387830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
4388830218c1SDavid Ahern 		if (table)
4389830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4390830218c1SDavid Ahern 	}
43911da177e4SLinus Torvalds 
4392afb1d4b5SDavid Ahern 	return rt6_get_dflt_router(net, gwaddr, dev);
43931da177e4SLinus Torvalds }
43941da177e4SLinus Torvalds 
4395afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net,
4396afb1d4b5SDavid Ahern 				     struct fib6_table *table)
43971da177e4SLinus Torvalds {
43988d1c802bSDavid Ahern 	struct fib6_info *rt;
43991da177e4SLinus Torvalds 
44001da177e4SLinus Torvalds restart:
440166f5d6ceSWei Wang 	rcu_read_lock();
440266f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
4403dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
4404dcd1f572SDavid Ahern 		struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4405dcd1f572SDavid Ahern 
440693c2fb25SDavid Ahern 		if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
4407e873e4b9SWei Wang 		    (!idev || idev->cnf.accept_ra != 2) &&
4408e873e4b9SWei Wang 		    fib6_info_hold_safe(rt)) {
440966f5d6ceSWei Wang 			rcu_read_unlock();
441011dd74b3SRoopa Prabhu 			ip6_del_rt(net, rt, false);
44111da177e4SLinus Torvalds 			goto restart;
44121da177e4SLinus Torvalds 		}
44131da177e4SLinus Torvalds 	}
441466f5d6ceSWei Wang 	rcu_read_unlock();
4415830218c1SDavid Ahern 
4416830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4417830218c1SDavid Ahern }
4418830218c1SDavid Ahern 
4419830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
4420830218c1SDavid Ahern {
4421830218c1SDavid Ahern 	struct fib6_table *table;
4422830218c1SDavid Ahern 	struct hlist_head *head;
4423830218c1SDavid Ahern 	unsigned int h;
4424830218c1SDavid Ahern 
4425830218c1SDavid Ahern 	rcu_read_lock();
4426830218c1SDavid Ahern 
4427830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4428830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
4429830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4430830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
4431afb1d4b5SDavid Ahern 				__rt6_purge_dflt_routers(net, table);
4432830218c1SDavid Ahern 		}
4433830218c1SDavid Ahern 	}
4434830218c1SDavid Ahern 
4435830218c1SDavid Ahern 	rcu_read_unlock();
44361da177e4SLinus Torvalds }
44371da177e4SLinus Torvalds 
44385578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
44395578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
444086872cb5SThomas Graf 				 struct fib6_config *cfg)
444186872cb5SThomas Graf {
44428823a3acSMaciej Żenczykowski 	*cfg = (struct fib6_config){
44438823a3acSMaciej Żenczykowski 		.fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
44448823a3acSMaciej Żenczykowski 			 : RT6_TABLE_MAIN,
44458823a3acSMaciej Żenczykowski 		.fc_ifindex = rtmsg->rtmsg_ifindex,
444667f69513SDavid Ahern 		.fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
44478823a3acSMaciej Żenczykowski 		.fc_expires = rtmsg->rtmsg_info,
44488823a3acSMaciej Żenczykowski 		.fc_dst_len = rtmsg->rtmsg_dst_len,
44498823a3acSMaciej Żenczykowski 		.fc_src_len = rtmsg->rtmsg_src_len,
44508823a3acSMaciej Żenczykowski 		.fc_flags = rtmsg->rtmsg_flags,
44518823a3acSMaciej Żenczykowski 		.fc_type = rtmsg->rtmsg_type,
445286872cb5SThomas Graf 
44538823a3acSMaciej Żenczykowski 		.fc_nlinfo.nl_net = net,
445486872cb5SThomas Graf 
44558823a3acSMaciej Żenczykowski 		.fc_dst = rtmsg->rtmsg_dst,
44568823a3acSMaciej Żenczykowski 		.fc_src = rtmsg->rtmsg_src,
44578823a3acSMaciej Żenczykowski 		.fc_gateway = rtmsg->rtmsg_gateway,
44588823a3acSMaciej Żenczykowski 	};
445986872cb5SThomas Graf }
446086872cb5SThomas Graf 
44617c1552daSChristoph Hellwig int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
44621da177e4SLinus Torvalds {
446386872cb5SThomas Graf 	struct fib6_config cfg;
44641da177e4SLinus Torvalds 	int err;
44651da177e4SLinus Torvalds 
44667c1552daSChristoph Hellwig 	if (cmd != SIOCADDRT && cmd != SIOCDELRT)
44677c1552daSChristoph Hellwig 		return -EINVAL;
4468af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
44691da177e4SLinus Torvalds 		return -EPERM;
44701da177e4SLinus Torvalds 
44717c1552daSChristoph Hellwig 	rtmsg_to_fib6_config(net, rtmsg, &cfg);
447286872cb5SThomas Graf 
44731da177e4SLinus Torvalds 	rtnl_lock();
44741da177e4SLinus Torvalds 	switch (cmd) {
44751da177e4SLinus Torvalds 	case SIOCADDRT:
4476acb54e3cSDavid Ahern 		err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
44771da177e4SLinus Torvalds 		break;
44781da177e4SLinus Torvalds 	case SIOCDELRT:
4479333c4301SDavid Ahern 		err = ip6_route_del(&cfg, NULL);
44801da177e4SLinus Torvalds 		break;
44811da177e4SLinus Torvalds 	}
44821da177e4SLinus Torvalds 	rtnl_unlock();
44831da177e4SLinus Torvalds 	return err;
44843ff50b79SStephen Hemminger }
44851da177e4SLinus Torvalds 
44861da177e4SLinus Torvalds /*
44871da177e4SLinus Torvalds  *	Drop the packet on the floor
44881da177e4SLinus Torvalds  */
44891da177e4SLinus Torvalds 
4490d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
44911da177e4SLinus Torvalds {
4492adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
44931d3fd8a1SStephen Suryaputra 	struct net *net = dev_net(dst->dev);
44941d3fd8a1SStephen Suryaputra 	struct inet6_dev *idev;
44951d3fd8a1SStephen Suryaputra 	int type;
44961d3fd8a1SStephen Suryaputra 
44971d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev) &&
44981d3fd8a1SStephen Suryaputra 	    dst->dev == net->loopback_dev)
44991d3fd8a1SStephen Suryaputra 		idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
45001d3fd8a1SStephen Suryaputra 	else
45011d3fd8a1SStephen Suryaputra 		idev = ip6_dst_idev(dst);
45021d3fd8a1SStephen Suryaputra 
4503612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
4504612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
45050660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
450645bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
45071d3fd8a1SStephen Suryaputra 			IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
4508612f09e8SYOSHIFUJI Hideaki 			break;
4509612f09e8SYOSHIFUJI Hideaki 		}
4510a8eceea8SJoe Perches 		fallthrough;
4511612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
45121d3fd8a1SStephen Suryaputra 		IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
4513612f09e8SYOSHIFUJI Hideaki 		break;
4514612f09e8SYOSHIFUJI Hideaki 	}
45151d3fd8a1SStephen Suryaputra 
45161d3fd8a1SStephen Suryaputra 	/* Start over by dropping the dst for l3mdev case */
45171d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev))
45181d3fd8a1SStephen Suryaputra 		skb_dst_drop(skb);
45191d3fd8a1SStephen Suryaputra 
45203ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
45211da177e4SLinus Torvalds 	kfree_skb(skb);
45221da177e4SLinus Torvalds 	return 0;
45231da177e4SLinus Torvalds }
45241da177e4SLinus Torvalds 
45259ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
45269ce8ade0SThomas Graf {
4527612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
45289ce8ade0SThomas Graf }
45299ce8ade0SThomas Graf 
4530ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
45311da177e4SLinus Torvalds {
4532adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
4533612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
45341da177e4SLinus Torvalds }
45351da177e4SLinus Torvalds 
45369ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
45379ce8ade0SThomas Graf {
4538612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
45399ce8ade0SThomas Graf }
45409ce8ade0SThomas Graf 
4541ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
45429ce8ade0SThomas Graf {
4543adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
4544612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
45459ce8ade0SThomas Graf }
45469ce8ade0SThomas Graf 
45471da177e4SLinus Torvalds /*
45481da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
45491da177e4SLinus Torvalds  */
45501da177e4SLinus Torvalds 
4551360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net,
4552afb1d4b5SDavid Ahern 				     struct inet6_dev *idev,
45531da177e4SLinus Torvalds 				     const struct in6_addr *addr,
4554acb54e3cSDavid Ahern 				     bool anycast, gfp_t gfp_flags)
45551da177e4SLinus Torvalds {
4556c7a1ce39SDavid Ahern 	struct fib6_config cfg = {
4557c7a1ce39SDavid Ahern 		.fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4558c7a1ce39SDavid Ahern 		.fc_ifindex = idev->dev->ifindex,
4559d55a2e37SMaciej Żenczykowski 		.fc_flags = RTF_UP | RTF_NONEXTHOP,
4560c7a1ce39SDavid Ahern 		.fc_dst = *addr,
4561c7a1ce39SDavid Ahern 		.fc_dst_len = 128,
4562c7a1ce39SDavid Ahern 		.fc_protocol = RTPROT_KERNEL,
4563c7a1ce39SDavid Ahern 		.fc_nlinfo.nl_net = net,
4564c7a1ce39SDavid Ahern 		.fc_ignore_dev_down = true,
4565c7a1ce39SDavid Ahern 	};
4566d55a2e37SMaciej Żenczykowski 	struct fib6_info *f6i;
45675f02ce24SDavid Ahern 
4568e8478e80SDavid Ahern 	if (anycast) {
4569c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_ANYCAST;
4570c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_ANYCAST;
4571e8478e80SDavid Ahern 	} else {
4572c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_LOCAL;
4573c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_LOCAL;
4574e8478e80SDavid Ahern 	}
45751da177e4SLinus Torvalds 
4576d55a2e37SMaciej Żenczykowski 	f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
45778652f17cSMaciej Żenczykowski 	if (!IS_ERR(f6i))
4578d55a2e37SMaciej Żenczykowski 		f6i->dst_nocount = true;
4579d55a2e37SMaciej Żenczykowski 	return f6i;
45801da177e4SLinus Torvalds }
45811da177e4SLinus Torvalds 
4582c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
4583c3968a85SDaniel Walter struct arg_dev_net_ip {
4584c3968a85SDaniel Walter 	struct net_device *dev;
4585c3968a85SDaniel Walter 	struct net *net;
4586c3968a85SDaniel Walter 	struct in6_addr *addr;
4587c3968a85SDaniel Walter };
4588c3968a85SDaniel Walter 
45898d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
4590c3968a85SDaniel Walter {
4591c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4592c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4593c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4594c3968a85SDaniel Walter 
4595f88d8ea6SDavid Ahern 	if (!rt->nh &&
4596f88d8ea6SDavid Ahern 	    ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
4597421842edSDavid Ahern 	    rt != net->ipv6.fib6_null_entry &&
459893c2fb25SDavid Ahern 	    ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
459960006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
4600c3968a85SDaniel Walter 		/* remove prefsrc entry */
460193c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
460260006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
4603c3968a85SDaniel Walter 	}
4604c3968a85SDaniel Walter 	return 0;
4605c3968a85SDaniel Walter }
4606c3968a85SDaniel Walter 
4607c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4608c3968a85SDaniel Walter {
4609c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
4610c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
4611c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
4612c3968a85SDaniel Walter 		.net = net,
4613c3968a85SDaniel Walter 		.addr = &ifp->addr,
4614c3968a85SDaniel Walter 	};
46150c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4616c3968a85SDaniel Walter }
4617c3968a85SDaniel Walter 
46182b2450caSDavid Ahern #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT)
4619be7a010dSDuan Jiong 
4620be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
46218d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4622be7a010dSDuan Jiong {
4623be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
4624f88d8ea6SDavid Ahern 	struct fib6_nh *nh;
4625be7a010dSDuan Jiong 
4626f88d8ea6SDavid Ahern 	/* RA routes do not use nexthops */
4627f88d8ea6SDavid Ahern 	if (rt->nh)
4628f88d8ea6SDavid Ahern 		return 0;
4629f88d8ea6SDavid Ahern 
4630f88d8ea6SDavid Ahern 	nh = rt->fib6_nh;
463193c2fb25SDavid Ahern 	if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
4632cc5c073aSDavid Ahern 	    nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
4633be7a010dSDuan Jiong 		return -1;
4634b16cb459SWei Wang 
4635b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
4636b16cb459SWei Wang 	 * This is needed because cached route may have a different
4637b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
4638b16cb459SWei Wang 	 */
4639cc5c073aSDavid Ahern 	fib6_nh_exceptions_clean_tohost(nh, gateway);
4640b16cb459SWei Wang 
4641be7a010dSDuan Jiong 	return 0;
4642be7a010dSDuan Jiong }
4643be7a010dSDuan Jiong 
4644be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4645be7a010dSDuan Jiong {
4646be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
4647be7a010dSDuan Jiong }
4648be7a010dSDuan Jiong 
46492127d95aSIdo Schimmel struct arg_netdev_event {
46502127d95aSIdo Schimmel 	const struct net_device *dev;
46514c981e28SIdo Schimmel 	union {
4652ecc5663cSDavid Ahern 		unsigned char nh_flags;
46534c981e28SIdo Schimmel 		unsigned long event;
46544c981e28SIdo Schimmel 	};
46552127d95aSIdo Schimmel };
46562127d95aSIdo Schimmel 
46578d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4658d7dedee1SIdo Schimmel {
46598d1c802bSDavid Ahern 	struct fib6_info *iter;
4660d7dedee1SIdo Schimmel 	struct fib6_node *fn;
4661d7dedee1SIdo Schimmel 
466293c2fb25SDavid Ahern 	fn = rcu_dereference_protected(rt->fib6_node,
466393c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
4664d7dedee1SIdo Schimmel 	iter = rcu_dereference_protected(fn->leaf,
466593c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
4666d7dedee1SIdo Schimmel 	while (iter) {
466793c2fb25SDavid Ahern 		if (iter->fib6_metric == rt->fib6_metric &&
466833bd5ac5SDavid Ahern 		    rt6_qualify_for_ecmp(iter))
4669d7dedee1SIdo Schimmel 			return iter;
46708fb11a9aSDavid Ahern 		iter = rcu_dereference_protected(iter->fib6_next,
467193c2fb25SDavid Ahern 				lockdep_is_held(&rt->fib6_table->tb6_lock));
4672d7dedee1SIdo Schimmel 	}
4673d7dedee1SIdo Schimmel 
4674d7dedee1SIdo Schimmel 	return NULL;
4675d7dedee1SIdo Schimmel }
4676d7dedee1SIdo Schimmel 
4677f88d8ea6SDavid Ahern /* only called for fib entries with builtin fib6_nh */
46788d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt)
4679d7dedee1SIdo Schimmel {
46801cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
46811cf844c7SDavid Ahern 	    (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
46821cf844c7SDavid Ahern 	     ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
4683d7dedee1SIdo Schimmel 		return true;
4684d7dedee1SIdo Schimmel 
4685d7dedee1SIdo Schimmel 	return false;
4686d7dedee1SIdo Schimmel }
4687d7dedee1SIdo Schimmel 
46888d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt)
4689d7dedee1SIdo Schimmel {
46908d1c802bSDavid Ahern 	struct fib6_info *iter;
4691d7dedee1SIdo Schimmel 	int total = 0;
4692d7dedee1SIdo Schimmel 
4693d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt))
46941cf844c7SDavid Ahern 		total += rt->fib6_nh->fib_nh_weight;
4695d7dedee1SIdo Schimmel 
469693c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4697d7dedee1SIdo Schimmel 		if (!rt6_is_dead(iter))
46981cf844c7SDavid Ahern 			total += iter->fib6_nh->fib_nh_weight;
4699d7dedee1SIdo Schimmel 	}
4700d7dedee1SIdo Schimmel 
4701d7dedee1SIdo Schimmel 	return total;
4702d7dedee1SIdo Schimmel }
4703d7dedee1SIdo Schimmel 
47048d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4705d7dedee1SIdo Schimmel {
4706d7dedee1SIdo Schimmel 	int upper_bound = -1;
4707d7dedee1SIdo Schimmel 
4708d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt)) {
47091cf844c7SDavid Ahern 		*weight += rt->fib6_nh->fib_nh_weight;
4710d7dedee1SIdo Schimmel 		upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4711d7dedee1SIdo Schimmel 						    total) - 1;
4712d7dedee1SIdo Schimmel 	}
47131cf844c7SDavid Ahern 	atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
4714d7dedee1SIdo Schimmel }
4715d7dedee1SIdo Schimmel 
47168d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4717d7dedee1SIdo Schimmel {
47188d1c802bSDavid Ahern 	struct fib6_info *iter;
4719d7dedee1SIdo Schimmel 	int weight = 0;
4720d7dedee1SIdo Schimmel 
4721d7dedee1SIdo Schimmel 	rt6_upper_bound_set(rt, &weight, total);
4722d7dedee1SIdo Schimmel 
472393c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4724d7dedee1SIdo Schimmel 		rt6_upper_bound_set(iter, &weight, total);
4725d7dedee1SIdo Schimmel }
4726d7dedee1SIdo Schimmel 
47278d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt)
4728d7dedee1SIdo Schimmel {
47298d1c802bSDavid Ahern 	struct fib6_info *first;
4730d7dedee1SIdo Schimmel 	int total;
4731d7dedee1SIdo Schimmel 
4732d7dedee1SIdo Schimmel 	/* In case the entire multipath route was marked for flushing,
4733d7dedee1SIdo Schimmel 	 * then there is no need to rebalance upon the removal of every
4734d7dedee1SIdo Schimmel 	 * sibling route.
4735d7dedee1SIdo Schimmel 	 */
473693c2fb25SDavid Ahern 	if (!rt->fib6_nsiblings || rt->should_flush)
4737d7dedee1SIdo Schimmel 		return;
4738d7dedee1SIdo Schimmel 
4739d7dedee1SIdo Schimmel 	/* During lookup routes are evaluated in order, so we need to
4740d7dedee1SIdo Schimmel 	 * make sure upper bounds are assigned from the first sibling
4741d7dedee1SIdo Schimmel 	 * onwards.
4742d7dedee1SIdo Schimmel 	 */
4743d7dedee1SIdo Schimmel 	first = rt6_multipath_first_sibling(rt);
4744d7dedee1SIdo Schimmel 	if (WARN_ON_ONCE(!first))
4745d7dedee1SIdo Schimmel 		return;
4746d7dedee1SIdo Schimmel 
4747d7dedee1SIdo Schimmel 	total = rt6_multipath_total_weight(first);
4748d7dedee1SIdo Schimmel 	rt6_multipath_upper_bound_set(first, total);
4749d7dedee1SIdo Schimmel }
4750d7dedee1SIdo Schimmel 
47518d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg)
47522127d95aSIdo Schimmel {
47532127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
47547aef6859SDavid Ahern 	struct net *net = dev_net(arg->dev);
47552127d95aSIdo Schimmel 
4756f88d8ea6SDavid Ahern 	if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
47571cf844c7SDavid Ahern 	    rt->fib6_nh->fib_nh_dev == arg->dev) {
47581cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
47597aef6859SDavid Ahern 		fib6_update_sernum_upto_root(net, rt);
4760d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
47611de178edSIdo Schimmel 	}
47622127d95aSIdo Schimmel 
47632127d95aSIdo Schimmel 	return 0;
47642127d95aSIdo Schimmel }
47652127d95aSIdo Schimmel 
4766ecc5663cSDavid Ahern void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
47672127d95aSIdo Schimmel {
47682127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
47692127d95aSIdo Schimmel 		.dev = dev,
47706802f3adSIdo Schimmel 		{
47712127d95aSIdo Schimmel 			.nh_flags = nh_flags,
47726802f3adSIdo Schimmel 		},
47732127d95aSIdo Schimmel 	};
47742127d95aSIdo Schimmel 
47752127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
47762127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
47772127d95aSIdo Schimmel 
47782127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
47792127d95aSIdo Schimmel }
47802127d95aSIdo Schimmel 
4781f88d8ea6SDavid Ahern /* only called for fib entries with inline fib6_nh */
47828d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
47831de178edSIdo Schimmel 				   const struct net_device *dev)
47841de178edSIdo Schimmel {
47858d1c802bSDavid Ahern 	struct fib6_info *iter;
47861de178edSIdo Schimmel 
47871cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == dev)
47881de178edSIdo Schimmel 		return true;
478993c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
47901cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == dev)
47911de178edSIdo Schimmel 			return true;
47921de178edSIdo Schimmel 
47931de178edSIdo Schimmel 	return false;
47941de178edSIdo Schimmel }
47951de178edSIdo Schimmel 
47968d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt)
47971de178edSIdo Schimmel {
47988d1c802bSDavid Ahern 	struct fib6_info *iter;
47991de178edSIdo Schimmel 
48001de178edSIdo Schimmel 	rt->should_flush = 1;
480193c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
48021de178edSIdo Schimmel 		iter->should_flush = 1;
48031de178edSIdo Schimmel }
48041de178edSIdo Schimmel 
48058d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
48061de178edSIdo Schimmel 					     const struct net_device *down_dev)
48071de178edSIdo Schimmel {
48088d1c802bSDavid Ahern 	struct fib6_info *iter;
48091de178edSIdo Schimmel 	unsigned int dead = 0;
48101de178edSIdo Schimmel 
48111cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == down_dev ||
48121cf844c7SDavid Ahern 	    rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
48131de178edSIdo Schimmel 		dead++;
481493c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
48151cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == down_dev ||
48161cf844c7SDavid Ahern 		    iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
48171de178edSIdo Schimmel 			dead++;
48181de178edSIdo Schimmel 
48191de178edSIdo Schimmel 	return dead;
48201de178edSIdo Schimmel }
48211de178edSIdo Schimmel 
48228d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
48231de178edSIdo Schimmel 				       const struct net_device *dev,
4824ecc5663cSDavid Ahern 				       unsigned char nh_flags)
48251de178edSIdo Schimmel {
48268d1c802bSDavid Ahern 	struct fib6_info *iter;
48271de178edSIdo Schimmel 
48281cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == dev)
48291cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags |= nh_flags;
483093c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
48311cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == dev)
48321cf844c7SDavid Ahern 			iter->fib6_nh->fib_nh_flags |= nh_flags;
48331de178edSIdo Schimmel }
48341de178edSIdo Schimmel 
4835a1a22c12SDavid Ahern /* called with write lock held for table with rt */
48368d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
48371da177e4SLinus Torvalds {
48384c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
48394c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
48407aef6859SDavid Ahern 	struct net *net = dev_net(dev);
48418ed67789SDaniel Lezcano 
4842f88d8ea6SDavid Ahern 	if (rt == net->ipv6.fib6_null_entry || rt->nh)
484327c6fa73SIdo Schimmel 		return 0;
484427c6fa73SIdo Schimmel 
484527c6fa73SIdo Schimmel 	switch (arg->event) {
484627c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
48471cf844c7SDavid Ahern 		return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
484827c6fa73SIdo Schimmel 	case NETDEV_DOWN:
48491de178edSIdo Schimmel 		if (rt->should_flush)
485027c6fa73SIdo Schimmel 			return -1;
485193c2fb25SDavid Ahern 		if (!rt->fib6_nsiblings)
48521cf844c7SDavid Ahern 			return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
48531de178edSIdo Schimmel 		if (rt6_multipath_uses_dev(rt, dev)) {
48541de178edSIdo Schimmel 			unsigned int count;
48551de178edSIdo Schimmel 
48561de178edSIdo Schimmel 			count = rt6_multipath_dead_count(rt, dev);
485793c2fb25SDavid Ahern 			if (rt->fib6_nsiblings + 1 == count) {
48581de178edSIdo Schimmel 				rt6_multipath_flush(rt);
48591de178edSIdo Schimmel 				return -1;
48601de178edSIdo Schimmel 			}
48611de178edSIdo Schimmel 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
48621de178edSIdo Schimmel 						   RTNH_F_LINKDOWN);
48637aef6859SDavid Ahern 			fib6_update_sernum(net, rt);
4864d7dedee1SIdo Schimmel 			rt6_multipath_rebalance(rt);
48651de178edSIdo Schimmel 		}
48661de178edSIdo Schimmel 		return -2;
486727c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
48681cf844c7SDavid Ahern 		if (rt->fib6_nh->fib_nh_dev != dev ||
486993c2fb25SDavid Ahern 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
487027c6fa73SIdo Schimmel 			break;
48711cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
4872d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
487327c6fa73SIdo Schimmel 		break;
48742b241361SIdo Schimmel 	}
4875c159d30cSDavid S. Miller 
48761da177e4SLinus Torvalds 	return 0;
48771da177e4SLinus Torvalds }
48781da177e4SLinus Torvalds 
487927c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
48801da177e4SLinus Torvalds {
48814c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
48828ed67789SDaniel Lezcano 		.dev = dev,
48836802f3adSIdo Schimmel 		{
48844c981e28SIdo Schimmel 			.event = event,
48856802f3adSIdo Schimmel 		},
48868ed67789SDaniel Lezcano 	};
48877c6bb7d2SDavid Ahern 	struct net *net = dev_net(dev);
48888ed67789SDaniel Lezcano 
48897c6bb7d2SDavid Ahern 	if (net->ipv6.sysctl.skip_notify_on_dev_down)
48907c6bb7d2SDavid Ahern 		fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
48917c6bb7d2SDavid Ahern 	else
48927c6bb7d2SDavid Ahern 		fib6_clean_all(net, fib6_ifdown, &arg);
48934c981e28SIdo Schimmel }
48944c981e28SIdo Schimmel 
48954c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
48964c981e28SIdo Schimmel {
48974c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
48984c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
48994c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
49001da177e4SLinus Torvalds }
49011da177e4SLinus Torvalds 
490295c96174SEric Dumazet struct rt6_mtu_change_arg {
49031da177e4SLinus Torvalds 	struct net_device *dev;
490495c96174SEric Dumazet 	unsigned int mtu;
4905c0b220cfSDavid Ahern 	struct fib6_info *f6i;
49061da177e4SLinus Torvalds };
49071da177e4SLinus Torvalds 
4908cc5c073aSDavid Ahern static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
4909c0b220cfSDavid Ahern {
4910c0b220cfSDavid Ahern 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
4911cc5c073aSDavid Ahern 	struct fib6_info *f6i = arg->f6i;
4912c0b220cfSDavid Ahern 
4913c0b220cfSDavid Ahern 	/* For administrative MTU increase, there is no way to discover
4914c0b220cfSDavid Ahern 	 * IPv6 PMTU increase, so PMTU increase should be updated here.
4915c0b220cfSDavid Ahern 	 * Since RFC 1981 doesn't include administrative MTU increase
4916c0b220cfSDavid Ahern 	 * update PMTU increase is a MUST. (i.e. jumbo frame)
4917c0b220cfSDavid Ahern 	 */
4918c0b220cfSDavid Ahern 	if (nh->fib_nh_dev == arg->dev) {
4919c0b220cfSDavid Ahern 		struct inet6_dev *idev = __in6_dev_get(arg->dev);
4920c0b220cfSDavid Ahern 		u32 mtu = f6i->fib6_pmtu;
4921c0b220cfSDavid Ahern 
4922c0b220cfSDavid Ahern 		if (mtu >= arg->mtu ||
4923c0b220cfSDavid Ahern 		    (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4924c0b220cfSDavid Ahern 			fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4925c0b220cfSDavid Ahern 
4926c0b220cfSDavid Ahern 		spin_lock_bh(&rt6_exception_lock);
4927cc5c073aSDavid Ahern 		rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
4928c0b220cfSDavid Ahern 		spin_unlock_bh(&rt6_exception_lock);
4929c0b220cfSDavid Ahern 	}
4930c0b220cfSDavid Ahern 
4931c0b220cfSDavid Ahern 	return 0;
4932c0b220cfSDavid Ahern }
4933c0b220cfSDavid Ahern 
4934c0b220cfSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
49351da177e4SLinus Torvalds {
49361da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
49371da177e4SLinus Torvalds 	struct inet6_dev *idev;
49381da177e4SLinus Torvalds 
49391da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
49401da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
49411da177e4SLinus Torvalds 	   We still use this lock to block changes
49421da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
49431da177e4SLinus Torvalds 	*/
49441da177e4SLinus Torvalds 
49451da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
494638308473SDavid S. Miller 	if (!idev)
49471da177e4SLinus Torvalds 		return 0;
49481da177e4SLinus Torvalds 
4949c0b220cfSDavid Ahern 	if (fib6_metric_locked(f6i, RTAX_MTU))
49501da177e4SLinus Torvalds 		return 0;
4951c0b220cfSDavid Ahern 
4952c0b220cfSDavid Ahern 	arg->f6i = f6i;
49532d44234bSDavid Ahern 	if (f6i->nh) {
49542d44234bSDavid Ahern 		/* fib6_nh_mtu_change only returns 0, so this is safe */
49552d44234bSDavid Ahern 		return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
49562d44234bSDavid Ahern 						arg);
49572d44234bSDavid Ahern 	}
49582d44234bSDavid Ahern 
49591cf844c7SDavid Ahern 	return fib6_nh_mtu_change(f6i->fib6_nh, arg);
49601da177e4SLinus Torvalds }
49611da177e4SLinus Torvalds 
496295c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
49631da177e4SLinus Torvalds {
4964c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
4965c71099acSThomas Graf 		.dev = dev,
4966c71099acSThomas Graf 		.mtu = mtu,
4967c71099acSThomas Graf 	};
49681da177e4SLinus Torvalds 
49690c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
49701da177e4SLinus Torvalds }
49711da177e4SLinus Torvalds 
4972ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
497375425657SDavid Ahern 	[RTA_UNSPEC]		= { .strict_start_type = RTA_DPORT + 1 },
49745176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
4975aa8f8778SEric Dumazet 	[RTA_PREFSRC]		= { .len = sizeof(struct in6_addr) },
497686872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
4977ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
497886872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
497986872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
498051ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
4981c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
498219e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
498319e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
498432bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
4985622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
49863b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
4987aa8f8778SEric Dumazet 	[RTA_TABLE]		= { .type = NLA_U32 },
4988eacb9384SRoopa Prabhu 	[RTA_IP_PROTO]		= { .type = NLA_U8 },
4989eacb9384SRoopa Prabhu 	[RTA_SPORT]		= { .type = NLA_U16 },
4990eacb9384SRoopa Prabhu 	[RTA_DPORT]		= { .type = NLA_U16 },
49915b98324eSDavid Ahern 	[RTA_NH_ID]		= { .type = NLA_U32 },
499286872cb5SThomas Graf };
499386872cb5SThomas Graf 
499486872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4995333c4301SDavid Ahern 			      struct fib6_config *cfg,
4996333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
49971da177e4SLinus Torvalds {
499886872cb5SThomas Graf 	struct rtmsg *rtm;
499986872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
5000c78ba6d6SLubomir Rintel 	unsigned int pref;
500186872cb5SThomas Graf 	int err;
50021da177e4SLinus Torvalds 
50038cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
50048cb08174SJohannes Berg 				     rtm_ipv6_policy, extack);
500586872cb5SThomas Graf 	if (err < 0)
500686872cb5SThomas Graf 		goto errout;
50071da177e4SLinus Torvalds 
500886872cb5SThomas Graf 	err = -EINVAL;
500986872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
501086872cb5SThomas Graf 
501184db8407SMaciej Żenczykowski 	*cfg = (struct fib6_config){
501284db8407SMaciej Żenczykowski 		.fc_table = rtm->rtm_table,
501384db8407SMaciej Żenczykowski 		.fc_dst_len = rtm->rtm_dst_len,
501484db8407SMaciej Żenczykowski 		.fc_src_len = rtm->rtm_src_len,
501584db8407SMaciej Żenczykowski 		.fc_flags = RTF_UP,
501684db8407SMaciej Żenczykowski 		.fc_protocol = rtm->rtm_protocol,
501784db8407SMaciej Żenczykowski 		.fc_type = rtm->rtm_type,
501884db8407SMaciej Żenczykowski 
501984db8407SMaciej Żenczykowski 		.fc_nlinfo.portid = NETLINK_CB(skb).portid,
502084db8407SMaciej Żenczykowski 		.fc_nlinfo.nlh = nlh,
502184db8407SMaciej Żenczykowski 		.fc_nlinfo.nl_net = sock_net(skb->sk),
502284db8407SMaciej Żenczykowski 	};
502386872cb5SThomas Graf 
5024ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
5025ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
5026b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
5027b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
502886872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
502986872cb5SThomas Graf 
5030ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
5031ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
5032ab79ad14SMaciej Żenczykowski 
50331f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
50341f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
50351f56a01fSMartin KaFai Lau 
5036fc1e64e1SDavid Ahern 	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
5037fc1e64e1SDavid Ahern 
50385b98324eSDavid Ahern 	if (tb[RTA_NH_ID]) {
50395b98324eSDavid Ahern 		if (tb[RTA_GATEWAY]   || tb[RTA_OIF] ||
50405b98324eSDavid Ahern 		    tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
50415b98324eSDavid Ahern 			NL_SET_ERR_MSG(extack,
50425b98324eSDavid Ahern 				       "Nexthop specification and nexthop id are mutually exclusive");
50435b98324eSDavid Ahern 			goto errout;
50445b98324eSDavid Ahern 		}
50455b98324eSDavid Ahern 		cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
50465b98324eSDavid Ahern 	}
50475b98324eSDavid Ahern 
504886872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
504967b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
505086872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
50511da177e4SLinus Torvalds 	}
5052e3818541SDavid Ahern 	if (tb[RTA_VIA]) {
5053e3818541SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
5054e3818541SDavid Ahern 		goto errout;
5055e3818541SDavid Ahern 	}
505686872cb5SThomas Graf 
505786872cb5SThomas Graf 	if (tb[RTA_DST]) {
505886872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
505986872cb5SThomas Graf 
506086872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
506186872cb5SThomas Graf 			goto errout;
506286872cb5SThomas Graf 
506386872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
50641da177e4SLinus Torvalds 	}
506586872cb5SThomas Graf 
506686872cb5SThomas Graf 	if (tb[RTA_SRC]) {
506786872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
506886872cb5SThomas Graf 
506986872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
507086872cb5SThomas Graf 			goto errout;
507186872cb5SThomas Graf 
507286872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
50731da177e4SLinus Torvalds 	}
507486872cb5SThomas Graf 
5075c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
507667b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
5077c3968a85SDaniel Walter 
507886872cb5SThomas Graf 	if (tb[RTA_OIF])
507986872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
508086872cb5SThomas Graf 
508186872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
508286872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
508386872cb5SThomas Graf 
508486872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
508586872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
508686872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
50871da177e4SLinus Torvalds 	}
508886872cb5SThomas Graf 
508986872cb5SThomas Graf 	if (tb[RTA_TABLE])
509086872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
509186872cb5SThomas Graf 
509251ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
509351ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
509451ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
50959ed59592SDavid Ahern 
50969ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
5097c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
50989ed59592SDavid Ahern 		if (err < 0)
50999ed59592SDavid Ahern 			goto errout;
510051ebd318SNicolas Dichtel 	}
510151ebd318SNicolas Dichtel 
5102c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
5103c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
5104c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
5105c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
5106c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
5107c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
5108c78ba6d6SLubomir Rintel 	}
5109c78ba6d6SLubomir Rintel 
511019e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
511119e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
511219e42e45SRoopa Prabhu 
51139ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
511419e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
511519e42e45SRoopa Prabhu 
5116c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
51179ed59592SDavid Ahern 		if (err < 0)
51189ed59592SDavid Ahern 			goto errout;
51199ed59592SDavid Ahern 	}
51209ed59592SDavid Ahern 
512132bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
512232bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
512332bc201eSXin Long 
512432bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
512532bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
512632bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
512732bc201eSXin Long 		}
512832bc201eSXin Long 	}
512932bc201eSXin Long 
513086872cb5SThomas Graf 	err = 0;
513186872cb5SThomas Graf errout:
513286872cb5SThomas Graf 	return err;
51331da177e4SLinus Torvalds }
51341da177e4SLinus Torvalds 
51356b9ea5a6SRoopa Prabhu struct rt6_nh {
51368d1c802bSDavid Ahern 	struct fib6_info *fib6_info;
51376b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
51386b9ea5a6SRoopa Prabhu 	struct list_head next;
51396b9ea5a6SRoopa Prabhu };
51406b9ea5a6SRoopa Prabhu 
5141d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net,
5142d4ead6b3SDavid Ahern 				 struct list_head *rt6_nh_list,
51438d1c802bSDavid Ahern 				 struct fib6_info *rt,
51448d1c802bSDavid Ahern 				 struct fib6_config *r_cfg)
51456b9ea5a6SRoopa Prabhu {
51466b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
51476b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
51486b9ea5a6SRoopa Prabhu 
51496b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
51508d1c802bSDavid Ahern 		/* check if fib6_info already exists */
51518d1c802bSDavid Ahern 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
51526b9ea5a6SRoopa Prabhu 			return err;
51536b9ea5a6SRoopa Prabhu 	}
51546b9ea5a6SRoopa Prabhu 
51556b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
51566b9ea5a6SRoopa Prabhu 	if (!nh)
51576b9ea5a6SRoopa Prabhu 		return -ENOMEM;
51588d1c802bSDavid Ahern 	nh->fib6_info = rt;
51596b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
51606b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
51616b9ea5a6SRoopa Prabhu 
51626b9ea5a6SRoopa Prabhu 	return 0;
51636b9ea5a6SRoopa Prabhu }
51646b9ea5a6SRoopa Prabhu 
51658d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt,
51668d1c802bSDavid Ahern 				   struct fib6_info *rt_last,
51673b1137feSDavid Ahern 				   struct nl_info *info,
51683b1137feSDavid Ahern 				   __u16 nlflags)
51693b1137feSDavid Ahern {
51703b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
51713b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
51723b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
51733b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
51743b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
51753b1137feSDavid Ahern 	 */
517693c2fb25SDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
517793c2fb25SDavid Ahern 		rt = list_first_entry(&rt_last->fib6_siblings,
51788d1c802bSDavid Ahern 				      struct fib6_info,
517993c2fb25SDavid Ahern 				      fib6_siblings);
51803b1137feSDavid Ahern 	}
51813b1137feSDavid Ahern 
51823b1137feSDavid Ahern 	if (rt)
51833b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
51843b1137feSDavid Ahern }
51853b1137feSDavid Ahern 
51860ee0f47cSIdo Schimmel static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
51870ee0f47cSIdo Schimmel {
51880ee0f47cSIdo Schimmel 	bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
51890ee0f47cSIdo Schimmel 	bool should_notify = false;
51900ee0f47cSIdo Schimmel 	struct fib6_info *leaf;
51910ee0f47cSIdo Schimmel 	struct fib6_node *fn;
51920ee0f47cSIdo Schimmel 
51930ee0f47cSIdo Schimmel 	rcu_read_lock();
51940ee0f47cSIdo Schimmel 	fn = rcu_dereference(rt->fib6_node);
51950ee0f47cSIdo Schimmel 	if (!fn)
51960ee0f47cSIdo Schimmel 		goto out;
51970ee0f47cSIdo Schimmel 
51980ee0f47cSIdo Schimmel 	leaf = rcu_dereference(fn->leaf);
51990ee0f47cSIdo Schimmel 	if (!leaf)
52000ee0f47cSIdo Schimmel 		goto out;
52010ee0f47cSIdo Schimmel 
52020ee0f47cSIdo Schimmel 	if (rt == leaf ||
52030ee0f47cSIdo Schimmel 	    (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
52040ee0f47cSIdo Schimmel 	     rt6_qualify_for_ecmp(leaf)))
52050ee0f47cSIdo Schimmel 		should_notify = true;
52060ee0f47cSIdo Schimmel out:
52070ee0f47cSIdo Schimmel 	rcu_read_unlock();
52080ee0f47cSIdo Schimmel 
52090ee0f47cSIdo Schimmel 	return should_notify;
52100ee0f47cSIdo Schimmel }
52110ee0f47cSIdo Schimmel 
5212333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
5213333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
521451ebd318SNicolas Dichtel {
52158d1c802bSDavid Ahern 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
52163b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
521751ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
521851ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
52198d1c802bSDavid Ahern 	struct fib6_info *rt;
52206b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
52216b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
52223b1137feSDavid Ahern 	__u16 nlflags;
522351ebd318SNicolas Dichtel 	int remaining;
522451ebd318SNicolas Dichtel 	int attrlen;
52256b9ea5a6SRoopa Prabhu 	int err = 1;
52266b9ea5a6SRoopa Prabhu 	int nhn = 0;
52276b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
52286b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
52296b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
523051ebd318SNicolas Dichtel 
52313b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
52323b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
52333b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
52343b1137feSDavid Ahern 
523535f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
523651ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
523751ebd318SNicolas Dichtel 
52386b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
52398d1c802bSDavid Ahern 	 * fib6_info structs per nexthop
52406b9ea5a6SRoopa Prabhu 	 */
524151ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
524251ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
524351ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
524451ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
524551ebd318SNicolas Dichtel 
524651ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
524751ebd318SNicolas Dichtel 		if (attrlen > 0) {
524851ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
524951ebd318SNicolas Dichtel 
525051ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
525151ebd318SNicolas Dichtel 			if (nla) {
525267b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
525351ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
525451ebd318SNicolas Dichtel 			}
525519e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
525619e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
525719e42e45SRoopa Prabhu 			if (nla)
525819e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
525951ebd318SNicolas Dichtel 		}
52606b9ea5a6SRoopa Prabhu 
526168e2ffdeSDavid Ahern 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5262acb54e3cSDavid Ahern 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
52638c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
52648c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
52658c5b83f0SRoopa Prabhu 			rt = NULL;
52666b9ea5a6SRoopa Prabhu 			goto cleanup;
52678c5b83f0SRoopa Prabhu 		}
5268b5d2d75eSDavid Ahern 		if (!rt6_qualify_for_ecmp(rt)) {
5269b5d2d75eSDavid Ahern 			err = -EINVAL;
5270b5d2d75eSDavid Ahern 			NL_SET_ERR_MSG(extack,
5271b5d2d75eSDavid Ahern 				       "Device only routes can not be added for IPv6 using the multipath API.");
5272b5d2d75eSDavid Ahern 			fib6_info_release(rt);
5273b5d2d75eSDavid Ahern 			goto cleanup;
5274b5d2d75eSDavid Ahern 		}
52756b9ea5a6SRoopa Prabhu 
52761cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
5277398958aeSIdo Schimmel 
5278d4ead6b3SDavid Ahern 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5279d4ead6b3SDavid Ahern 					    rt, &r_cfg);
528051ebd318SNicolas Dichtel 		if (err) {
528193531c67SDavid Ahern 			fib6_info_release(rt);
52826b9ea5a6SRoopa Prabhu 			goto cleanup;
528351ebd318SNicolas Dichtel 		}
52846b9ea5a6SRoopa Prabhu 
52856b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
528651ebd318SNicolas Dichtel 	}
52876b9ea5a6SRoopa Prabhu 
52889eee3b49SIdo Schimmel 	if (list_empty(&rt6_nh_list)) {
52899eee3b49SIdo Schimmel 		NL_SET_ERR_MSG(extack,
52909eee3b49SIdo Schimmel 			       "Invalid nexthop configuration - no valid nexthops");
52919eee3b49SIdo Schimmel 		return -EINVAL;
52929eee3b49SIdo Schimmel 	}
52939eee3b49SIdo Schimmel 
52943b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
52953b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
52963b1137feSDavid Ahern 	 * the full route when done
52973b1137feSDavid Ahern 	 */
52983b1137feSDavid Ahern 	info->skip_notify = 1;
52993b1137feSDavid Ahern 
5300ebee3cadSIdo Schimmel 	/* For add and replace, send one notification with all nexthops. For
5301ebee3cadSIdo Schimmel 	 * append, send one notification with all appended nexthops.
5302ebee3cadSIdo Schimmel 	 */
5303ebee3cadSIdo Schimmel 	info->skip_notify_kernel = 1;
5304ebee3cadSIdo Schimmel 
53056b9ea5a6SRoopa Prabhu 	err_nh = NULL;
53066b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
53078d1c802bSDavid Ahern 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
53088d1c802bSDavid Ahern 		fib6_info_release(nh->fib6_info);
53093b1137feSDavid Ahern 
5310f7225172SDavid Ahern 		if (!err) {
5311f7225172SDavid Ahern 			/* save reference to last route successfully inserted */
5312f7225172SDavid Ahern 			rt_last = nh->fib6_info;
5313f7225172SDavid Ahern 
53146b9ea5a6SRoopa Prabhu 			/* save reference to first route for notification */
5315f7225172SDavid Ahern 			if (!rt_notif)
53168d1c802bSDavid Ahern 				rt_notif = nh->fib6_info;
5317f7225172SDavid Ahern 		}
53186b9ea5a6SRoopa Prabhu 
53198d1c802bSDavid Ahern 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
53208d1c802bSDavid Ahern 		nh->fib6_info = NULL;
53216b9ea5a6SRoopa Prabhu 		if (err) {
53226b9ea5a6SRoopa Prabhu 			if (replace && nhn)
5323a5a82d84SJakub Kicinski 				NL_SET_ERR_MSG_MOD(extack,
5324a5a82d84SJakub Kicinski 						   "multipath route replace failed (check consistency of installed routes)");
53256b9ea5a6SRoopa Prabhu 			err_nh = nh;
53266b9ea5a6SRoopa Prabhu 			goto add_errout;
53276b9ea5a6SRoopa Prabhu 		}
53286b9ea5a6SRoopa Prabhu 
53291a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
533027596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
533127596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
533227596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
533327596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
533427596472SMichal Kubeček 		 * be added to it.
53351a72418bSNicolas Dichtel 		 */
5336864db232SMuhammad Usama Anjum 		if (cfg->fc_nlinfo.nlh) {
533727596472SMichal Kubeček 			cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
533827596472SMichal Kubeček 							     NLM_F_REPLACE);
5339afecdb37SBenjamin Poirier 			cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
5340864db232SMuhammad Usama Anjum 		}
53416b9ea5a6SRoopa Prabhu 		nhn++;
53426b9ea5a6SRoopa Prabhu 	}
53436b9ea5a6SRoopa Prabhu 
53440ee0f47cSIdo Schimmel 	/* An in-kernel notification should only be sent in case the new
53450ee0f47cSIdo Schimmel 	 * multipath route is added as the first route in the node, or if
53460ee0f47cSIdo Schimmel 	 * it was appended to it. We pass 'rt_notif' since it is the first
53470ee0f47cSIdo Schimmel 	 * sibling and might allow us to skip some checks in the replace case.
53480ee0f47cSIdo Schimmel 	 */
53490ee0f47cSIdo Schimmel 	if (ip6_route_mpath_should_notify(rt_notif)) {
53500ee0f47cSIdo Schimmel 		enum fib_event_type fib_event;
53510ee0f47cSIdo Schimmel 
53520ee0f47cSIdo Schimmel 		if (rt_notif->fib6_nsiblings != nhn - 1)
53530ee0f47cSIdo Schimmel 			fib_event = FIB_EVENT_ENTRY_APPEND;
53540ee0f47cSIdo Schimmel 		else
5355caafb250SIdo Schimmel 			fib_event = FIB_EVENT_ENTRY_REPLACE;
53560ee0f47cSIdo Schimmel 
53570ee0f47cSIdo Schimmel 		err = call_fib6_multipath_entry_notifiers(info->nl_net,
53580ee0f47cSIdo Schimmel 							  fib_event, rt_notif,
53590ee0f47cSIdo Schimmel 							  nhn - 1, extack);
53600ee0f47cSIdo Schimmel 		if (err) {
53610ee0f47cSIdo Schimmel 			/* Delete all the siblings that were just added */
53620ee0f47cSIdo Schimmel 			err_nh = NULL;
53630ee0f47cSIdo Schimmel 			goto add_errout;
53640ee0f47cSIdo Schimmel 		}
53650ee0f47cSIdo Schimmel 	}
5366ebee3cadSIdo Schimmel 
53673b1137feSDavid Ahern 	/* success ... tell user about new route */
53683b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
53696b9ea5a6SRoopa Prabhu 	goto cleanup;
53706b9ea5a6SRoopa Prabhu 
53716b9ea5a6SRoopa Prabhu add_errout:
53723b1137feSDavid Ahern 	/* send notification for routes that were added so that
53733b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
53743b1137feSDavid Ahern 	 * coherent
53753b1137feSDavid Ahern 	 */
53763b1137feSDavid Ahern 	if (rt_notif)
53773b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
53783b1137feSDavid Ahern 
53796b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
53806b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
53816b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
53826b9ea5a6SRoopa Prabhu 			break;
5383333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
53846b9ea5a6SRoopa Prabhu 	}
53856b9ea5a6SRoopa Prabhu 
53866b9ea5a6SRoopa Prabhu cleanup:
53876b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
53888d1c802bSDavid Ahern 		if (nh->fib6_info)
53898d1c802bSDavid Ahern 			fib6_info_release(nh->fib6_info);
53906b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
53916b9ea5a6SRoopa Prabhu 		kfree(nh);
53926b9ea5a6SRoopa Prabhu 	}
53936b9ea5a6SRoopa Prabhu 
53946b9ea5a6SRoopa Prabhu 	return err;
53956b9ea5a6SRoopa Prabhu }
53966b9ea5a6SRoopa Prabhu 
5397333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
5398333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
53996b9ea5a6SRoopa Prabhu {
54006b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
54016b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
54022291267eSColin Ian King 	int last_err = 0;
54036b9ea5a6SRoopa Prabhu 	int remaining;
54046b9ea5a6SRoopa Prabhu 	int attrlen;
54052291267eSColin Ian King 	int err;
54066b9ea5a6SRoopa Prabhu 
54076b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
54086b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
54096b9ea5a6SRoopa Prabhu 
54106b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
54116b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
54126b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
54136b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
54146b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
54156b9ea5a6SRoopa Prabhu 
54166b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
54176b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
54186b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
54196b9ea5a6SRoopa Prabhu 
54206b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
54216b9ea5a6SRoopa Prabhu 			if (nla) {
54226b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
54236b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
54246b9ea5a6SRoopa Prabhu 			}
54256b9ea5a6SRoopa Prabhu 		}
5426333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
54276b9ea5a6SRoopa Prabhu 		if (err)
54286b9ea5a6SRoopa Prabhu 			last_err = err;
54296b9ea5a6SRoopa Prabhu 
543051ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
543151ebd318SNicolas Dichtel 	}
543251ebd318SNicolas Dichtel 
543351ebd318SNicolas Dichtel 	return last_err;
543451ebd318SNicolas Dichtel }
543551ebd318SNicolas Dichtel 
5436c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5437c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
54381da177e4SLinus Torvalds {
543986872cb5SThomas Graf 	struct fib6_config cfg;
544086872cb5SThomas Graf 	int err;
54411da177e4SLinus Torvalds 
5442333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
544386872cb5SThomas Graf 	if (err < 0)
544486872cb5SThomas Graf 		return err;
544586872cb5SThomas Graf 
54465b98324eSDavid Ahern 	if (cfg.fc_nh_id &&
54475b98324eSDavid Ahern 	    !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
54485b98324eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
54495b98324eSDavid Ahern 		return -EINVAL;
54505b98324eSDavid Ahern 	}
54515b98324eSDavid Ahern 
545251ebd318SNicolas Dichtel 	if (cfg.fc_mp)
5453333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
54540ae81335SDavid Ahern 	else {
54550ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
5456333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
54571da177e4SLinus Torvalds 	}
54580ae81335SDavid Ahern }
54591da177e4SLinus Torvalds 
5460c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5461c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
54621da177e4SLinus Torvalds {
546386872cb5SThomas Graf 	struct fib6_config cfg;
546486872cb5SThomas Graf 	int err;
54651da177e4SLinus Torvalds 
5466333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
546786872cb5SThomas Graf 	if (err < 0)
546886872cb5SThomas Graf 		return err;
546986872cb5SThomas Graf 
547067f69513SDavid Ahern 	if (cfg.fc_metric == 0)
547167f69513SDavid Ahern 		cfg.fc_metric = IP6_RT_PRIO_USER;
547267f69513SDavid Ahern 
547351ebd318SNicolas Dichtel 	if (cfg.fc_mp)
5474333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
547551ebd318SNicolas Dichtel 	else
5476acb54e3cSDavid Ahern 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
54771da177e4SLinus Torvalds }
54781da177e4SLinus Torvalds 
5479a1b7a1f0SDavid Ahern /* add the overhead of this fib6_nh to nexthop_len */
5480a1b7a1f0SDavid Ahern static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
5481339bf98fSThomas Graf {
5482a1b7a1f0SDavid Ahern 	int *nexthop_len = arg;
5483beb1afacSDavid Ahern 
5484a1b7a1f0SDavid Ahern 	*nexthop_len += nla_total_size(0)	 /* RTA_MULTIPATH */
5485a1b7a1f0SDavid Ahern 		     + NLA_ALIGN(sizeof(struct rtnexthop))
5486a1b7a1f0SDavid Ahern 		     + nla_total_size(16); /* RTA_GATEWAY */
5487f88d8ea6SDavid Ahern 
5488a1b7a1f0SDavid Ahern 	if (nh->fib_nh_lws) {
5489a1b7a1f0SDavid Ahern 		/* RTA_ENCAP_TYPE */
5490a1b7a1f0SDavid Ahern 		*nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5491a1b7a1f0SDavid Ahern 		/* RTA_ENCAP */
5492a1b7a1f0SDavid Ahern 		*nexthop_len += nla_total_size(2);
5493a1b7a1f0SDavid Ahern 	}
5494a1b7a1f0SDavid Ahern 
5495a1b7a1f0SDavid Ahern 	return 0;
5496a1b7a1f0SDavid Ahern }
5497a1b7a1f0SDavid Ahern 
5498a1b7a1f0SDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5499a1b7a1f0SDavid Ahern {
5500a1b7a1f0SDavid Ahern 	int nexthop_len;
5501a1b7a1f0SDavid Ahern 
5502a1b7a1f0SDavid Ahern 	if (f6i->nh) {
5503a1b7a1f0SDavid Ahern 		nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5504a1b7a1f0SDavid Ahern 		nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5505a1b7a1f0SDavid Ahern 					 &nexthop_len);
5506a1b7a1f0SDavid Ahern 	} else {
5507a1b7a1f0SDavid Ahern 		struct fib6_nh *nh = f6i->fib6_nh;
5508a1b7a1f0SDavid Ahern 
5509a1b7a1f0SDavid Ahern 		nexthop_len = 0;
5510a1b7a1f0SDavid Ahern 		if (f6i->fib6_nsiblings) {
5511beb1afacSDavid Ahern 			nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
5512beb1afacSDavid Ahern 				    + NLA_ALIGN(sizeof(struct rtnexthop))
5513beb1afacSDavid Ahern 				    + nla_total_size(16) /* RTA_GATEWAY */
5514a1b7a1f0SDavid Ahern 				    + lwtunnel_get_encap_size(nh->fib_nh_lws);
5515beb1afacSDavid Ahern 
5516a1b7a1f0SDavid Ahern 			nexthop_len *= f6i->fib6_nsiblings;
5517a1b7a1f0SDavid Ahern 		}
5518a1b7a1f0SDavid Ahern 		nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5519beb1afacSDavid Ahern 	}
5520beb1afacSDavid Ahern 
5521339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
5522339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
5523339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
5524339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
5525339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
5526339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
5527339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
5528339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
5529339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
55306a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
5531ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
5532c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
553319e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
5534beb1afacSDavid Ahern 	       + nexthop_len;
5535beb1afacSDavid Ahern }
5536beb1afacSDavid Ahern 
5537f88d8ea6SDavid Ahern static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5538f88d8ea6SDavid Ahern 				 unsigned char *flags)
5539f88d8ea6SDavid Ahern {
5540f88d8ea6SDavid Ahern 	if (nexthop_is_multipath(nh)) {
5541f88d8ea6SDavid Ahern 		struct nlattr *mp;
5542f88d8ea6SDavid Ahern 
55434255ff05SDavid Ahern 		mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5544f88d8ea6SDavid Ahern 		if (!mp)
5545f88d8ea6SDavid Ahern 			goto nla_put_failure;
5546f88d8ea6SDavid Ahern 
55477bdf4de1SDonald Sharp 		if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
5548f88d8ea6SDavid Ahern 			goto nla_put_failure;
5549f88d8ea6SDavid Ahern 
5550f88d8ea6SDavid Ahern 		nla_nest_end(skb, mp);
5551f88d8ea6SDavid Ahern 	} else {
5552f88d8ea6SDavid Ahern 		struct fib6_nh *fib6_nh;
5553f88d8ea6SDavid Ahern 
5554f88d8ea6SDavid Ahern 		fib6_nh = nexthop_fib6_nh(nh);
55557bdf4de1SDonald Sharp 		if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
5556f88d8ea6SDavid Ahern 				     flags, false) < 0)
5557f88d8ea6SDavid Ahern 			goto nla_put_failure;
5558f88d8ea6SDavid Ahern 	}
5559f88d8ea6SDavid Ahern 
5560f88d8ea6SDavid Ahern 	return 0;
5561f88d8ea6SDavid Ahern 
5562f88d8ea6SDavid Ahern nla_put_failure:
5563f88d8ea6SDavid Ahern 	return -EMSGSIZE;
5564f88d8ea6SDavid Ahern }
5565f88d8ea6SDavid Ahern 
5566d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
55678d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
5568d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
556915e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
5570f8cfe2ceSDavid Ahern 			 unsigned int flags)
55711da177e4SLinus Torvalds {
557222d0bd82SXin Long 	struct rt6_info *rt6 = (struct rt6_info *)dst;
557322d0bd82SXin Long 	struct rt6key *rt6_dst, *rt6_src;
557422d0bd82SXin Long 	u32 *pmetrics, table, rt6_flags;
5575f88d8ea6SDavid Ahern 	unsigned char nh_flags = 0;
55761da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
557722d0bd82SXin Long 	struct rtmsg *rtm;
5578d4ead6b3SDavid Ahern 	long expires = 0;
55791da177e4SLinus Torvalds 
558015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
558138308473SDavid S. Miller 	if (!nlh)
558226932566SPatrick McHardy 		return -EMSGSIZE;
55832d7202bfSThomas Graf 
558422d0bd82SXin Long 	if (rt6) {
558522d0bd82SXin Long 		rt6_dst = &rt6->rt6i_dst;
558622d0bd82SXin Long 		rt6_src = &rt6->rt6i_src;
558722d0bd82SXin Long 		rt6_flags = rt6->rt6i_flags;
558822d0bd82SXin Long 	} else {
558922d0bd82SXin Long 		rt6_dst = &rt->fib6_dst;
559022d0bd82SXin Long 		rt6_src = &rt->fib6_src;
559122d0bd82SXin Long 		rt6_flags = rt->fib6_flags;
559222d0bd82SXin Long 	}
559322d0bd82SXin Long 
55942d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
55951da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
559622d0bd82SXin Long 	rtm->rtm_dst_len = rt6_dst->plen;
559722d0bd82SXin Long 	rtm->rtm_src_len = rt6_src->plen;
55981da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
559993c2fb25SDavid Ahern 	if (rt->fib6_table)
560093c2fb25SDavid Ahern 		table = rt->fib6_table->tb6_id;
5601c71099acSThomas Graf 	else
56029e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
560397f0082aSKalash Nainwal 	rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
5604c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
5605c78679e8SDavid S. Miller 		goto nla_put_failure;
5606e8478e80SDavid Ahern 
5607e8478e80SDavid Ahern 	rtm->rtm_type = rt->fib6_type;
56081da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
56091da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
561093c2fb25SDavid Ahern 	rtm->rtm_protocol = rt->fib6_protocol;
56111da177e4SLinus Torvalds 
561222d0bd82SXin Long 	if (rt6_flags & RTF_CACHE)
56131da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
56141da177e4SLinus Torvalds 
5615d4ead6b3SDavid Ahern 	if (dest) {
5616d4ead6b3SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, dest))
5617c78679e8SDavid S. Miller 			goto nla_put_failure;
56181da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
56191da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
562022d0bd82SXin Long 		if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
5621c78679e8SDavid S. Miller 			goto nla_put_failure;
56221da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
56231da177e4SLinus Torvalds 	if (src) {
5624930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
5625c78679e8SDavid S. Miller 			goto nla_put_failure;
56261da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
5627c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
562822d0bd82SXin Long 		   nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
5629c78679e8SDavid S. Miller 		goto nla_put_failure;
56301da177e4SLinus Torvalds #endif
56317bc570c8SYOSHIFUJI Hideaki 	if (iif) {
56327bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
563322d0bd82SXin Long 		if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
5634fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
56352cf75070SNikolay Aleksandrov 
56367bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
56377bc570c8SYOSHIFUJI Hideaki 				return 0;
5638fd61c6baSDavid Ahern 			if (err < 0)
56397bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
56407bc570c8SYOSHIFUJI Hideaki 		} else
56417bc570c8SYOSHIFUJI Hideaki #endif
5642c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
5643c78679e8SDavid S. Miller 				goto nla_put_failure;
5644d4ead6b3SDavid Ahern 	} else if (dest) {
56451da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
5646d4ead6b3SDavid Ahern 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
5647930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5648c78679e8SDavid S. Miller 			goto nla_put_failure;
5649c3968a85SDaniel Walter 	}
5650c3968a85SDaniel Walter 
565193c2fb25SDavid Ahern 	if (rt->fib6_prefsrc.plen) {
5652c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
565393c2fb25SDavid Ahern 		saddr_buf = rt->fib6_prefsrc.addr;
5654930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5655c78679e8SDavid S. Miller 			goto nla_put_failure;
56561da177e4SLinus Torvalds 	}
56572d7202bfSThomas Graf 
5658d4ead6b3SDavid Ahern 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5659d4ead6b3SDavid Ahern 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
56602d7202bfSThomas Graf 		goto nla_put_failure;
56612d7202bfSThomas Graf 
566293c2fb25SDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
5663beb1afacSDavid Ahern 		goto nla_put_failure;
5664beb1afacSDavid Ahern 
5665beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
5666beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
5667beb1afacSDavid Ahern 	 */
566822d0bd82SXin Long 	if (rt6) {
566922d0bd82SXin Long 		if (rt6_flags & RTF_GATEWAY &&
567022d0bd82SXin Long 		    nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
567122d0bd82SXin Long 			goto nla_put_failure;
567222d0bd82SXin Long 
567322d0bd82SXin Long 		if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
567422d0bd82SXin Long 			goto nla_put_failure;
56756b13d8f7SOliver Herms 
56766b13d8f7SOliver Herms 		if (dst->lwtstate &&
56776b13d8f7SOliver Herms 		    lwtunnel_fill_encap(skb, dst->lwtstate, RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
56786b13d8f7SOliver Herms 			goto nla_put_failure;
567922d0bd82SXin Long 	} else if (rt->fib6_nsiblings) {
56808d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
5681beb1afacSDavid Ahern 		struct nlattr *mp;
5682beb1afacSDavid Ahern 
5683ae0be8deSMichal Kubecek 		mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5684beb1afacSDavid Ahern 		if (!mp)
5685beb1afacSDavid Ahern 			goto nla_put_failure;
5686beb1afacSDavid Ahern 
56871cf844c7SDavid Ahern 		if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
56887bdf4de1SDonald Sharp 				    rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
5689beb1afacSDavid Ahern 			goto nla_put_failure;
5690beb1afacSDavid Ahern 
5691beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
569293c2fb25SDavid Ahern 					 &rt->fib6_siblings, fib6_siblings) {
56931cf844c7SDavid Ahern 			if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
56947bdf4de1SDonald Sharp 					    sibling->fib6_nh->fib_nh_weight,
56957bdf4de1SDonald Sharp 					    AF_INET6) < 0)
569694f826b8SEric Dumazet 				goto nla_put_failure;
569794f826b8SEric Dumazet 		}
56982d7202bfSThomas Graf 
5699beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
5700f88d8ea6SDavid Ahern 	} else if (rt->nh) {
5701f88d8ea6SDavid Ahern 		if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5702f88d8ea6SDavid Ahern 			goto nla_put_failure;
5703ecc5663cSDavid Ahern 
5704f88d8ea6SDavid Ahern 		if (nexthop_is_blackhole(rt->nh))
5705f88d8ea6SDavid Ahern 			rtm->rtm_type = RTN_BLACKHOLE;
5706f88d8ea6SDavid Ahern 
57074f80116dSRoopa Prabhu 		if (net->ipv4.sysctl_nexthop_compat_mode &&
57084f80116dSRoopa Prabhu 		    rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5709f88d8ea6SDavid Ahern 			goto nla_put_failure;
5710f88d8ea6SDavid Ahern 
5711f88d8ea6SDavid Ahern 		rtm->rtm_flags |= nh_flags;
5712f88d8ea6SDavid Ahern 	} else {
57137bdf4de1SDonald Sharp 		if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
5714ecc5663cSDavid Ahern 				     &nh_flags, false) < 0)
5715c78679e8SDavid S. Miller 			goto nla_put_failure;
5716ecc5663cSDavid Ahern 
5717ecc5663cSDavid Ahern 		rtm->rtm_flags |= nh_flags;
5718beb1afacSDavid Ahern 	}
57198253947eSLi Wei 
572022d0bd82SXin Long 	if (rt6_flags & RTF_EXPIRES) {
572114895687SDavid Ahern 		expires = dst ? dst->expires : rt->expires;
572214895687SDavid Ahern 		expires -= jiffies;
572314895687SDavid Ahern 	}
572469cdf8f9SYOSHIFUJI Hideaki 
5725bb3c4ab9SIdo Schimmel 	if (!dst) {
5726bb3c4ab9SIdo Schimmel 		if (rt->offload)
5727bb3c4ab9SIdo Schimmel 			rtm->rtm_flags |= RTM_F_OFFLOAD;
5728bb3c4ab9SIdo Schimmel 		if (rt->trap)
5729bb3c4ab9SIdo Schimmel 			rtm->rtm_flags |= RTM_F_TRAP;
57300c5fcf9eSAmit Cohen 		if (rt->offload_failed)
57310c5fcf9eSAmit Cohen 			rtm->rtm_flags |= RTM_F_OFFLOAD_FAILED;
5732bb3c4ab9SIdo Schimmel 	}
5733bb3c4ab9SIdo Schimmel 
5734d4ead6b3SDavid Ahern 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
5735e3703b3dSThomas Graf 		goto nla_put_failure;
57361da177e4SLinus Torvalds 
573722d0bd82SXin Long 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
5738c78ba6d6SLubomir Rintel 		goto nla_put_failure;
5739c78ba6d6SLubomir Rintel 
574019e42e45SRoopa Prabhu 
5741053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5742053c095aSJohannes Berg 	return 0;
57432d7202bfSThomas Graf 
57442d7202bfSThomas Graf nla_put_failure:
574526932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
574626932566SPatrick McHardy 	return -EMSGSIZE;
57471da177e4SLinus Torvalds }
57481da177e4SLinus Torvalds 
57492c170e07SDavid Ahern static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
57502c170e07SDavid Ahern {
57512c170e07SDavid Ahern 	const struct net_device *dev = arg;
57522c170e07SDavid Ahern 
57532c170e07SDavid Ahern 	if (nh->fib_nh_dev == dev)
57542c170e07SDavid Ahern 		return 1;
57552c170e07SDavid Ahern 
57562c170e07SDavid Ahern 	return 0;
57572c170e07SDavid Ahern }
57582c170e07SDavid Ahern 
575913e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i,
576013e38901SDavid Ahern 			       const struct net_device *dev)
576113e38901SDavid Ahern {
57622c170e07SDavid Ahern 	if (f6i->nh) {
57632c170e07SDavid Ahern 		struct net_device *_dev = (struct net_device *)dev;
57642c170e07SDavid Ahern 
57652c170e07SDavid Ahern 		return !!nexthop_for_each_fib6_nh(f6i->nh,
57662c170e07SDavid Ahern 						  fib6_info_nh_uses_dev,
57672c170e07SDavid Ahern 						  _dev);
57682c170e07SDavid Ahern 	}
57692c170e07SDavid Ahern 
57701cf844c7SDavid Ahern 	if (f6i->fib6_nh->fib_nh_dev == dev)
577113e38901SDavid Ahern 		return true;
577213e38901SDavid Ahern 
577313e38901SDavid Ahern 	if (f6i->fib6_nsiblings) {
577413e38901SDavid Ahern 		struct fib6_info *sibling, *next_sibling;
577513e38901SDavid Ahern 
577613e38901SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
577713e38901SDavid Ahern 					 &f6i->fib6_siblings, fib6_siblings) {
57781cf844c7SDavid Ahern 			if (sibling->fib6_nh->fib_nh_dev == dev)
577913e38901SDavid Ahern 				return true;
578013e38901SDavid Ahern 		}
578113e38901SDavid Ahern 	}
578213e38901SDavid Ahern 
578313e38901SDavid Ahern 	return false;
578413e38901SDavid Ahern }
578513e38901SDavid Ahern 
57861e47b483SStefano Brivio struct fib6_nh_exception_dump_walker {
57871e47b483SStefano Brivio 	struct rt6_rtnl_dump_arg *dump;
57881e47b483SStefano Brivio 	struct fib6_info *rt;
57891e47b483SStefano Brivio 	unsigned int flags;
57901e47b483SStefano Brivio 	unsigned int skip;
57911e47b483SStefano Brivio 	unsigned int count;
57921e47b483SStefano Brivio };
57931e47b483SStefano Brivio 
57941e47b483SStefano Brivio static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
57951e47b483SStefano Brivio {
57961e47b483SStefano Brivio 	struct fib6_nh_exception_dump_walker *w = arg;
57971e47b483SStefano Brivio 	struct rt6_rtnl_dump_arg *dump = w->dump;
57981e47b483SStefano Brivio 	struct rt6_exception_bucket *bucket;
57991e47b483SStefano Brivio 	struct rt6_exception *rt6_ex;
58001e47b483SStefano Brivio 	int i, err;
58011e47b483SStefano Brivio 
58021e47b483SStefano Brivio 	bucket = fib6_nh_get_excptn_bucket(nh, NULL);
58031e47b483SStefano Brivio 	if (!bucket)
58041e47b483SStefano Brivio 		return 0;
58051e47b483SStefano Brivio 
58061e47b483SStefano Brivio 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
58071e47b483SStefano Brivio 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
58081e47b483SStefano Brivio 			if (w->skip) {
58091e47b483SStefano Brivio 				w->skip--;
58101e47b483SStefano Brivio 				continue;
58111e47b483SStefano Brivio 			}
58121e47b483SStefano Brivio 
58131e47b483SStefano Brivio 			/* Expiration of entries doesn't bump sernum, insertion
58141e47b483SStefano Brivio 			 * does. Removal is triggered by insertion, so we can
58151e47b483SStefano Brivio 			 * rely on the fact that if entries change between two
58161e47b483SStefano Brivio 			 * partial dumps, this node is scanned again completely,
58171e47b483SStefano Brivio 			 * see rt6_insert_exception() and fib6_dump_table().
58181e47b483SStefano Brivio 			 *
58191e47b483SStefano Brivio 			 * Count expired entries we go through as handled
58201e47b483SStefano Brivio 			 * entries that we'll skip next time, in case of partial
58211e47b483SStefano Brivio 			 * node dump. Otherwise, if entries expire meanwhile,
58221e47b483SStefano Brivio 			 * we'll skip the wrong amount.
58231e47b483SStefano Brivio 			 */
58241e47b483SStefano Brivio 			if (rt6_check_expired(rt6_ex->rt6i)) {
58251e47b483SStefano Brivio 				w->count++;
58261e47b483SStefano Brivio 				continue;
58271e47b483SStefano Brivio 			}
58281e47b483SStefano Brivio 
58291e47b483SStefano Brivio 			err = rt6_fill_node(dump->net, dump->skb, w->rt,
58301e47b483SStefano Brivio 					    &rt6_ex->rt6i->dst, NULL, NULL, 0,
58311e47b483SStefano Brivio 					    RTM_NEWROUTE,
58321e47b483SStefano Brivio 					    NETLINK_CB(dump->cb->skb).portid,
58331e47b483SStefano Brivio 					    dump->cb->nlh->nlmsg_seq, w->flags);
58341e47b483SStefano Brivio 			if (err)
58351e47b483SStefano Brivio 				return err;
58361e47b483SStefano Brivio 
58371e47b483SStefano Brivio 			w->count++;
58381e47b483SStefano Brivio 		}
58391e47b483SStefano Brivio 		bucket++;
58401e47b483SStefano Brivio 	}
58411e47b483SStefano Brivio 
58421e47b483SStefano Brivio 	return 0;
58431e47b483SStefano Brivio }
58441e47b483SStefano Brivio 
5845bf9a8a06SStefano Brivio /* Return -1 if done with node, number of handled routes on partial dump */
58461e47b483SStefano Brivio int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
58471da177e4SLinus Torvalds {
58481da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
584913e38901SDavid Ahern 	struct fib_dump_filter *filter = &arg->filter;
585013e38901SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
58511f17e2f2SDavid Ahern 	struct net *net = arg->net;
58521e47b483SStefano Brivio 	int count = 0;
58531f17e2f2SDavid Ahern 
5854421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
5855bf9a8a06SStefano Brivio 		return -1;
58561da177e4SLinus Torvalds 
585713e38901SDavid Ahern 	if ((filter->flags & RTM_F_PREFIX) &&
585893c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_PREFIX_RT)) {
5859f8cfe2ceSDavid Ahern 		/* success since this is not a prefix route */
5860bf9a8a06SStefano Brivio 		return -1;
5861f8cfe2ceSDavid Ahern 	}
58621e47b483SStefano Brivio 	if (filter->filter_set &&
58631e47b483SStefano Brivio 	    ((filter->rt_type  && rt->fib6_type != filter->rt_type) ||
586413e38901SDavid Ahern 	     (filter->dev      && !fib6_info_uses_dev(rt, filter->dev)) ||
58651e47b483SStefano Brivio 	     (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5866bf9a8a06SStefano Brivio 		return -1;
586713e38901SDavid Ahern 	}
58681e47b483SStefano Brivio 
58691e47b483SStefano Brivio 	if (filter->filter_set ||
58701e47b483SStefano Brivio 	    !filter->dump_routes || !filter->dump_exceptions) {
587113e38901SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
5872f8cfe2ceSDavid Ahern 	}
58731da177e4SLinus Torvalds 
58741e47b483SStefano Brivio 	if (filter->dump_routes) {
58751e47b483SStefano Brivio 		if (skip) {
58761e47b483SStefano Brivio 			skip--;
58771e47b483SStefano Brivio 		} else {
58781e47b483SStefano Brivio 			if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
58791e47b483SStefano Brivio 					  0, RTM_NEWROUTE,
5880bf9a8a06SStefano Brivio 					  NETLINK_CB(arg->cb->skb).portid,
58811e47b483SStefano Brivio 					  arg->cb->nlh->nlmsg_seq, flags)) {
5882bf9a8a06SStefano Brivio 				return 0;
58831e47b483SStefano Brivio 			}
58841e47b483SStefano Brivio 			count++;
58851e47b483SStefano Brivio 		}
58861e47b483SStefano Brivio 	}
58871e47b483SStefano Brivio 
58881e47b483SStefano Brivio 	if (filter->dump_exceptions) {
58891e47b483SStefano Brivio 		struct fib6_nh_exception_dump_walker w = { .dump = arg,
58901e47b483SStefano Brivio 							   .rt = rt,
58911e47b483SStefano Brivio 							   .flags = flags,
58921e47b483SStefano Brivio 							   .skip = skip,
58931e47b483SStefano Brivio 							   .count = 0 };
58941e47b483SStefano Brivio 		int err;
58951e47b483SStefano Brivio 
58963b525691SEric Dumazet 		rcu_read_lock();
58971e47b483SStefano Brivio 		if (rt->nh) {
58981e47b483SStefano Brivio 			err = nexthop_for_each_fib6_nh(rt->nh,
58991e47b483SStefano Brivio 						       rt6_nh_dump_exceptions,
59001e47b483SStefano Brivio 						       &w);
59011e47b483SStefano Brivio 		} else {
59021e47b483SStefano Brivio 			err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
59031e47b483SStefano Brivio 		}
59043b525691SEric Dumazet 		rcu_read_unlock();
59051e47b483SStefano Brivio 
59061e47b483SStefano Brivio 		if (err)
59071e47b483SStefano Brivio 			return count += w.count;
59081e47b483SStefano Brivio 	}
5909bf9a8a06SStefano Brivio 
5910bf9a8a06SStefano Brivio 	return -1;
59111da177e4SLinus Torvalds }
59121da177e4SLinus Torvalds 
59130eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
59140eff0a27SJakub Kicinski 					const struct nlmsghdr *nlh,
59150eff0a27SJakub Kicinski 					struct nlattr **tb,
59160eff0a27SJakub Kicinski 					struct netlink_ext_ack *extack)
59170eff0a27SJakub Kicinski {
59180eff0a27SJakub Kicinski 	struct rtmsg *rtm;
59190eff0a27SJakub Kicinski 	int i, err;
59200eff0a27SJakub Kicinski 
59210eff0a27SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
59220eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
59230eff0a27SJakub Kicinski 				   "Invalid header for get route request");
59240eff0a27SJakub Kicinski 		return -EINVAL;
59250eff0a27SJakub Kicinski 	}
59260eff0a27SJakub Kicinski 
59270eff0a27SJakub Kicinski 	if (!netlink_strict_get_check(skb))
59288cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
59290eff0a27SJakub Kicinski 					      rtm_ipv6_policy, extack);
59300eff0a27SJakub Kicinski 
59310eff0a27SJakub Kicinski 	rtm = nlmsg_data(nlh);
59320eff0a27SJakub Kicinski 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
59330eff0a27SJakub Kicinski 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
59340eff0a27SJakub Kicinski 	    rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
59350eff0a27SJakub Kicinski 	    rtm->rtm_type) {
59360eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
59370eff0a27SJakub Kicinski 		return -EINVAL;
59380eff0a27SJakub Kicinski 	}
59390eff0a27SJakub Kicinski 	if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
59400eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
59410eff0a27SJakub Kicinski 				   "Invalid flags for get route request");
59420eff0a27SJakub Kicinski 		return -EINVAL;
59430eff0a27SJakub Kicinski 	}
59440eff0a27SJakub Kicinski 
59458cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
59460eff0a27SJakub Kicinski 					    rtm_ipv6_policy, extack);
59470eff0a27SJakub Kicinski 	if (err)
59480eff0a27SJakub Kicinski 		return err;
59490eff0a27SJakub Kicinski 
59500eff0a27SJakub Kicinski 	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
59510eff0a27SJakub Kicinski 	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
59520eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
59530eff0a27SJakub Kicinski 		return -EINVAL;
59540eff0a27SJakub Kicinski 	}
59550eff0a27SJakub Kicinski 
59560eff0a27SJakub Kicinski 	for (i = 0; i <= RTA_MAX; i++) {
59570eff0a27SJakub Kicinski 		if (!tb[i])
59580eff0a27SJakub Kicinski 			continue;
59590eff0a27SJakub Kicinski 
59600eff0a27SJakub Kicinski 		switch (i) {
59610eff0a27SJakub Kicinski 		case RTA_SRC:
59620eff0a27SJakub Kicinski 		case RTA_DST:
59630eff0a27SJakub Kicinski 		case RTA_IIF:
59640eff0a27SJakub Kicinski 		case RTA_OIF:
59650eff0a27SJakub Kicinski 		case RTA_MARK:
59660eff0a27SJakub Kicinski 		case RTA_UID:
59670eff0a27SJakub Kicinski 		case RTA_SPORT:
59680eff0a27SJakub Kicinski 		case RTA_DPORT:
59690eff0a27SJakub Kicinski 		case RTA_IP_PROTO:
59700eff0a27SJakub Kicinski 			break;
59710eff0a27SJakub Kicinski 		default:
59720eff0a27SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
59730eff0a27SJakub Kicinski 			return -EINVAL;
59740eff0a27SJakub Kicinski 		}
59750eff0a27SJakub Kicinski 	}
59760eff0a27SJakub Kicinski 
59770eff0a27SJakub Kicinski 	return 0;
59780eff0a27SJakub Kicinski }
59790eff0a27SJakub Kicinski 
5980c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5981c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
59821da177e4SLinus Torvalds {
59833b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
5984ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
598518c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
5986a68886a6SDavid Ahern 	struct fib6_info *from;
598718c3a61cSRoopa Prabhu 	struct dst_entry *dst;
59881da177e4SLinus Torvalds 	struct rt6_info *rt;
5989ab364a6fSThomas Graf 	struct sk_buff *skb;
5990ab364a6fSThomas Graf 	struct rtmsg *rtm;
5991744486d4SMaciej Żenczykowski 	struct flowi6 fl6 = {};
599218c3a61cSRoopa Prabhu 	bool fibmatch;
5993ab364a6fSThomas Graf 
59940eff0a27SJakub Kicinski 	err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
5995ab364a6fSThomas Graf 	if (err < 0)
5996ab364a6fSThomas Graf 		goto errout;
5997ab364a6fSThomas Graf 
5998ab364a6fSThomas Graf 	err = -EINVAL;
599938b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
600038b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
600118c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
6002ab364a6fSThomas Graf 
6003ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
6004ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
6005ab364a6fSThomas Graf 			goto errout;
6006ab364a6fSThomas Graf 
60074e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
6008ab364a6fSThomas Graf 	}
6009ab364a6fSThomas Graf 
6010ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
6011ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
6012ab364a6fSThomas Graf 			goto errout;
6013ab364a6fSThomas Graf 
60144e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
6015ab364a6fSThomas Graf 	}
6016ab364a6fSThomas Graf 
6017ab364a6fSThomas Graf 	if (tb[RTA_IIF])
6018ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
6019ab364a6fSThomas Graf 
6020ab364a6fSThomas Graf 	if (tb[RTA_OIF])
602172331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
6022ab364a6fSThomas Graf 
60232e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
60242e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
60252e47b291SLorenzo Colitti 
6026622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
6027622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
6028622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
6029622ec2c9SLorenzo Colitti 	else
6030622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
6031622ec2c9SLorenzo Colitti 
6032eacb9384SRoopa Prabhu 	if (tb[RTA_SPORT])
6033eacb9384SRoopa Prabhu 		fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
6034eacb9384SRoopa Prabhu 
6035eacb9384SRoopa Prabhu 	if (tb[RTA_DPORT])
6036eacb9384SRoopa Prabhu 		fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
6037eacb9384SRoopa Prabhu 
6038eacb9384SRoopa Prabhu 	if (tb[RTA_IP_PROTO]) {
6039eacb9384SRoopa Prabhu 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
60405e1a99eaSHangbin Liu 						  &fl6.flowi6_proto, AF_INET6,
60415e1a99eaSHangbin Liu 						  extack);
6042eacb9384SRoopa Prabhu 		if (err)
6043eacb9384SRoopa Prabhu 			goto errout;
6044eacb9384SRoopa Prabhu 	}
6045eacb9384SRoopa Prabhu 
6046ab364a6fSThomas Graf 	if (iif) {
6047ab364a6fSThomas Graf 		struct net_device *dev;
604872331bc0SShmulik Ladkani 		int flags = 0;
604972331bc0SShmulik Ladkani 
6050121622dbSFlorian Westphal 		rcu_read_lock();
6051121622dbSFlorian Westphal 
6052121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
6053ab364a6fSThomas Graf 		if (!dev) {
6054121622dbSFlorian Westphal 			rcu_read_unlock();
6055ab364a6fSThomas Graf 			err = -ENODEV;
6056ab364a6fSThomas Graf 			goto errout;
6057ab364a6fSThomas Graf 		}
605872331bc0SShmulik Ladkani 
605972331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
606072331bc0SShmulik Ladkani 
606172331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
606272331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
606372331bc0SShmulik Ladkani 
6064b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
6065121622dbSFlorian Westphal 
6066121622dbSFlorian Westphal 		rcu_read_unlock();
606772331bc0SShmulik Ladkani 	} else {
606872331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
606972331bc0SShmulik Ladkani 
607018c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
607118c3a61cSRoopa Prabhu 	}
607218c3a61cSRoopa Prabhu 
607318c3a61cSRoopa Prabhu 
607418c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
607518c3a61cSRoopa Prabhu 	if (rt->dst.error) {
607618c3a61cSRoopa Prabhu 		err = rt->dst.error;
607718c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
607818c3a61cSRoopa Prabhu 		goto errout;
6079ab364a6fSThomas Graf 	}
60801da177e4SLinus Torvalds 
60819d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
60829d6acb3bSWANG Cong 		err = rt->dst.error;
60839d6acb3bSWANG Cong 		ip6_rt_put(rt);
60849d6acb3bSWANG Cong 		goto errout;
60859d6acb3bSWANG Cong 	}
60869d6acb3bSWANG Cong 
60871da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
608838308473SDavid S. Miller 	if (!skb) {
608994e187c0SAmerigo Wang 		ip6_rt_put(rt);
6090ab364a6fSThomas Graf 		err = -ENOBUFS;
6091ab364a6fSThomas Graf 		goto errout;
6092ab364a6fSThomas Graf 	}
60931da177e4SLinus Torvalds 
6094d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
6095a68886a6SDavid Ahern 
6096a68886a6SDavid Ahern 	rcu_read_lock();
6097a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
6098886b7a50SMartin KaFai Lau 	if (from) {
609918c3a61cSRoopa Prabhu 		if (fibmatch)
6100886b7a50SMartin KaFai Lau 			err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
6101886b7a50SMartin KaFai Lau 					    iif, RTM_NEWROUTE,
6102886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
610318c3a61cSRoopa Prabhu 					    nlh->nlmsg_seq, 0);
610418c3a61cSRoopa Prabhu 		else
6105a68886a6SDavid Ahern 			err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
6106a68886a6SDavid Ahern 					    &fl6.saddr, iif, RTM_NEWROUTE,
6107886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
6108886b7a50SMartin KaFai Lau 					    nlh->nlmsg_seq, 0);
6109886b7a50SMartin KaFai Lau 	} else {
6110886b7a50SMartin KaFai Lau 		err = -ENETUNREACH;
6111886b7a50SMartin KaFai Lau 	}
6112a68886a6SDavid Ahern 	rcu_read_unlock();
6113a68886a6SDavid Ahern 
61141da177e4SLinus Torvalds 	if (err < 0) {
6115ab364a6fSThomas Graf 		kfree_skb(skb);
6116ab364a6fSThomas Graf 		goto errout;
61171da177e4SLinus Torvalds 	}
61181da177e4SLinus Torvalds 
611915e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
6120ab364a6fSThomas Graf errout:
61211da177e4SLinus Torvalds 	return err;
61221da177e4SLinus Torvalds }
61231da177e4SLinus Torvalds 
61248d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
612537a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
61261da177e4SLinus Torvalds {
61271da177e4SLinus Torvalds 	struct sk_buff *skb;
61285578689aSDaniel Lezcano 	struct net *net = info->nl_net;
6129528c4cebSDenis V. Lunev 	u32 seq;
6130528c4cebSDenis V. Lunev 	int err;
61310d51aa80SJamal Hadi Salim 
6132528c4cebSDenis V. Lunev 	err = -ENOBUFS;
613338308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
613486872cb5SThomas Graf 
613519e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
613638308473SDavid S. Miller 	if (!skb)
613721713ebcSThomas Graf 		goto errout;
61381da177e4SLinus Torvalds 
6139d4ead6b3SDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6140f8cfe2ceSDavid Ahern 			    event, info->portid, seq, nlm_flags);
614126932566SPatrick McHardy 	if (err < 0) {
614226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
614326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
614426932566SPatrick McHardy 		kfree_skb(skb);
614526932566SPatrick McHardy 		goto errout;
614626932566SPatrick McHardy 	}
614715e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
61485578689aSDaniel Lezcano 		    info->nlh, gfp_any());
61491ce85fe4SPablo Neira Ayuso 	return;
615021713ebcSThomas Graf errout:
615121713ebcSThomas Graf 	if (err < 0)
61525578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
61531da177e4SLinus Torvalds }
61541da177e4SLinus Torvalds 
615519a3b7eeSDavid Ahern void fib6_rt_update(struct net *net, struct fib6_info *rt,
615619a3b7eeSDavid Ahern 		    struct nl_info *info)
615719a3b7eeSDavid Ahern {
615819a3b7eeSDavid Ahern 	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
615919a3b7eeSDavid Ahern 	struct sk_buff *skb;
616019a3b7eeSDavid Ahern 	int err = -ENOBUFS;
616119a3b7eeSDavid Ahern 
616219a3b7eeSDavid Ahern 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
616319a3b7eeSDavid Ahern 	if (!skb)
616419a3b7eeSDavid Ahern 		goto errout;
616519a3b7eeSDavid Ahern 
616619a3b7eeSDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
616719a3b7eeSDavid Ahern 			    RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
616819a3b7eeSDavid Ahern 	if (err < 0) {
616919a3b7eeSDavid Ahern 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
617019a3b7eeSDavid Ahern 		WARN_ON(err == -EMSGSIZE);
617119a3b7eeSDavid Ahern 		kfree_skb(skb);
617219a3b7eeSDavid Ahern 		goto errout;
617319a3b7eeSDavid Ahern 	}
617419a3b7eeSDavid Ahern 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
617519a3b7eeSDavid Ahern 		    info->nlh, gfp_any());
617619a3b7eeSDavid Ahern 	return;
617719a3b7eeSDavid Ahern errout:
617819a3b7eeSDavid Ahern 	if (err < 0)
617919a3b7eeSDavid Ahern 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
618019a3b7eeSDavid Ahern }
618119a3b7eeSDavid Ahern 
6182907eea48SAmit Cohen void fib6_info_hw_flags_set(struct net *net, struct fib6_info *f6i,
61830c5fcf9eSAmit Cohen 			    bool offload, bool trap, bool offload_failed)
6184907eea48SAmit Cohen {
6185907eea48SAmit Cohen 	struct sk_buff *skb;
6186907eea48SAmit Cohen 	int err;
6187907eea48SAmit Cohen 
61880c5fcf9eSAmit Cohen 	if (f6i->offload == offload && f6i->trap == trap &&
61890c5fcf9eSAmit Cohen 	    f6i->offload_failed == offload_failed)
6190907eea48SAmit Cohen 		return;
6191907eea48SAmit Cohen 
6192907eea48SAmit Cohen 	f6i->offload = offload;
6193907eea48SAmit Cohen 	f6i->trap = trap;
61946fad361aSAmit Cohen 
61956fad361aSAmit Cohen 	/* 2 means send notifications only if offload_failed was changed. */
61966fad361aSAmit Cohen 	if (net->ipv6.sysctl.fib_notify_on_flag_change == 2 &&
61976fad361aSAmit Cohen 	    f6i->offload_failed == offload_failed)
61986fad361aSAmit Cohen 		return;
61996fad361aSAmit Cohen 
62000c5fcf9eSAmit Cohen 	f6i->offload_failed = offload_failed;
6201907eea48SAmit Cohen 
6202907eea48SAmit Cohen 	if (!rcu_access_pointer(f6i->fib6_node))
6203907eea48SAmit Cohen 		/* The route was removed from the tree, do not send
620489e8347fSBhaskar Chowdhury 		 * notification.
6205907eea48SAmit Cohen 		 */
6206907eea48SAmit Cohen 		return;
6207907eea48SAmit Cohen 
6208907eea48SAmit Cohen 	if (!net->ipv6.sysctl.fib_notify_on_flag_change)
6209907eea48SAmit Cohen 		return;
6210907eea48SAmit Cohen 
6211907eea48SAmit Cohen 	skb = nlmsg_new(rt6_nlmsg_size(f6i), GFP_KERNEL);
6212907eea48SAmit Cohen 	if (!skb) {
6213907eea48SAmit Cohen 		err = -ENOBUFS;
6214907eea48SAmit Cohen 		goto errout;
6215907eea48SAmit Cohen 	}
6216907eea48SAmit Cohen 
6217907eea48SAmit Cohen 	err = rt6_fill_node(net, skb, f6i, NULL, NULL, NULL, 0, RTM_NEWROUTE, 0,
6218907eea48SAmit Cohen 			    0, 0);
6219907eea48SAmit Cohen 	if (err < 0) {
6220907eea48SAmit Cohen 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
6221907eea48SAmit Cohen 		WARN_ON(err == -EMSGSIZE);
6222907eea48SAmit Cohen 		kfree_skb(skb);
6223907eea48SAmit Cohen 		goto errout;
6224907eea48SAmit Cohen 	}
6225907eea48SAmit Cohen 
6226907eea48SAmit Cohen 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_ROUTE, NULL, GFP_KERNEL);
6227907eea48SAmit Cohen 	return;
6228907eea48SAmit Cohen 
6229907eea48SAmit Cohen errout:
6230907eea48SAmit Cohen 	rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
6231907eea48SAmit Cohen }
6232907eea48SAmit Cohen EXPORT_SYMBOL(fib6_info_hw_flags_set);
6233907eea48SAmit Cohen 
62348ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
6235351638e7SJiri Pirko 				unsigned long event, void *ptr)
62368ed67789SDaniel Lezcano {
6237351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6238c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
62398ed67789SDaniel Lezcano 
6240242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
6241242d3a49SWANG Cong 		return NOTIFY_OK;
6242242d3a49SWANG Cong 
6243242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
62441cf844c7SDavid Ahern 		net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
6245d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
62468ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
62478ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6248d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
62498ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
6250d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
62518ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
62528ed67789SDaniel Lezcano #endif
625376da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
625476da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
625576da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
625676da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
625776da0704SWANG Cong 		 */
625812d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
6259242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
626012d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
626112d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
6262242d3a49SWANG Cong #endif
62638ed67789SDaniel Lezcano 	}
62648ed67789SDaniel Lezcano 
62658ed67789SDaniel Lezcano 	return NOTIFY_OK;
62668ed67789SDaniel Lezcano }
62678ed67789SDaniel Lezcano 
62681da177e4SLinus Torvalds /*
62691da177e4SLinus Torvalds  *	/proc
62701da177e4SLinus Torvalds  */
62711da177e4SLinus Torvalds 
62721da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
62731da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
62741da177e4SLinus Torvalds {
627569ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
62761da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
627769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
627869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
627981eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
628069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
628169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
6282fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
628369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
62841da177e4SLinus Torvalds 
62851da177e4SLinus Torvalds 	return 0;
62861da177e4SLinus Torvalds }
62871da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
62881da177e4SLinus Torvalds 
62891da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
62901da177e4SLinus Torvalds 
629132927393SChristoph Hellwig static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
629232927393SChristoph Hellwig 			      void *buffer, size_t *lenp, loff_t *ppos)
62931da177e4SLinus Torvalds {
6294c486da34SLucian Adrian Grijincu 	struct net *net;
6295c486da34SLucian Adrian Grijincu 	int delay;
6296f0fb9b28SAditya Pakki 	int ret;
6297c486da34SLucian Adrian Grijincu 	if (!write)
6298c486da34SLucian Adrian Grijincu 		return -EINVAL;
6299c486da34SLucian Adrian Grijincu 
6300c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
6301c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
6302f0fb9b28SAditya Pakki 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6303f0fb9b28SAditya Pakki 	if (ret)
6304f0fb9b28SAditya Pakki 		return ret;
6305f0fb9b28SAditya Pakki 
63062ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
63071da177e4SLinus Torvalds 	return 0;
63081da177e4SLinus Torvalds }
63091da177e4SLinus Torvalds 
6310ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = {
63111da177e4SLinus Torvalds 	{
63121da177e4SLinus Torvalds 		.procname	=	"flush",
63134990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
63141da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
631589c8b3a1SDave Jones 		.mode		=	0200,
63166d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
63171da177e4SLinus Torvalds 	},
63181da177e4SLinus Torvalds 	{
63191da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
63209a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
63211da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
63221da177e4SLinus Torvalds 		.mode		=	0644,
63236d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
63241da177e4SLinus Torvalds 	},
63251da177e4SLinus Torvalds 	{
63261da177e4SLinus Torvalds 		.procname	=	"max_size",
63274990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
63281da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
63291da177e4SLinus Torvalds 		.mode		=	0644,
63306d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
63311da177e4SLinus Torvalds 	},
63321da177e4SLinus Torvalds 	{
63331da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
63344990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
63351da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
63361da177e4SLinus Torvalds 		.mode		=	0644,
63376d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
63381da177e4SLinus Torvalds 	},
63391da177e4SLinus Torvalds 	{
63401da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
63414990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
63421da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
63431da177e4SLinus Torvalds 		.mode		=	0644,
63446d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
63451da177e4SLinus Torvalds 	},
63461da177e4SLinus Torvalds 	{
63471da177e4SLinus Torvalds 		.procname	=	"gc_interval",
63484990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
63491da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
63501da177e4SLinus Torvalds 		.mode		=	0644,
63516d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
63521da177e4SLinus Torvalds 	},
63531da177e4SLinus Torvalds 	{
63541da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
63554990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
63561da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
63571da177e4SLinus Torvalds 		.mode		=	0644,
6358f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
63591da177e4SLinus Torvalds 	},
63601da177e4SLinus Torvalds 	{
63611da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
63624990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
63631da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
63641da177e4SLinus Torvalds 		.mode		=	0644,
63656d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
63661da177e4SLinus Torvalds 	},
63671da177e4SLinus Torvalds 	{
63681da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
63694990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
63701da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
63711da177e4SLinus Torvalds 		.mode		=	0644,
6372f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
63731da177e4SLinus Torvalds 	},
63741da177e4SLinus Torvalds 	{
63751da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
63764990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
63771da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
63781da177e4SLinus Torvalds 		.mode		=	0644,
63796d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
63801da177e4SLinus Torvalds 	},
63817c6bb7d2SDavid Ahern 	{
63827c6bb7d2SDavid Ahern 		.procname	=	"skip_notify_on_dev_down",
63837c6bb7d2SDavid Ahern 		.data		=	&init_net.ipv6.sysctl.skip_notify_on_dev_down,
63847c6bb7d2SDavid Ahern 		.maxlen		=	sizeof(int),
63857c6bb7d2SDavid Ahern 		.mode		=	0644,
6386b8e8a863SEiichi Tsukata 		.proc_handler	=	proc_dointvec_minmax,
6387eec4844fSMatteo Croce 		.extra1		=	SYSCTL_ZERO,
6388eec4844fSMatteo Croce 		.extra2		=	SYSCTL_ONE,
63897c6bb7d2SDavid Ahern 	},
6390f8572d8fSEric W. Biederman 	{ }
63911da177e4SLinus Torvalds };
63921da177e4SLinus Torvalds 
63932c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
6394760f2d01SDaniel Lezcano {
6395760f2d01SDaniel Lezcano 	struct ctl_table *table;
6396760f2d01SDaniel Lezcano 
6397760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
6398760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
6399760f2d01SDaniel Lezcano 			GFP_KERNEL);
64005ee09105SYOSHIFUJI Hideaki 
64015ee09105SYOSHIFUJI Hideaki 	if (table) {
64025ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
6403c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
640486393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
64055ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
64065ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
64075ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
64085ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
64095ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
64105ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
64115ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
64129c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
64137c6bb7d2SDavid Ahern 		table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
6414464dc801SEric W. Biederman 
6415464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
6416464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
6417464dc801SEric W. Biederman 			table[0].procname = NULL;
64185ee09105SYOSHIFUJI Hideaki 	}
64195ee09105SYOSHIFUJI Hideaki 
6420760f2d01SDaniel Lezcano 	return table;
6421760f2d01SDaniel Lezcano }
64221da177e4SLinus Torvalds #endif
64231da177e4SLinus Torvalds 
64242c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
6425cdb18761SDaniel Lezcano {
6426633d424bSPavel Emelyanov 	int ret = -ENOMEM;
64278ed67789SDaniel Lezcano 
642886393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
642986393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
6430f2fc6a54SBenjamin Thery 
6431fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6432fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
6433fc66f95cSEric Dumazet 
64341cf844c7SDavid Ahern 	net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
6435421842edSDavid Ahern 	if (!net->ipv6.fib6_null_entry)
6436421842edSDavid Ahern 		goto out_ip6_dst_entries;
64371cf844c7SDavid Ahern 	memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
64381cf844c7SDavid Ahern 	       sizeof(*net->ipv6.fib6_null_entry));
6439421842edSDavid Ahern 
64408ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
64418ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
64428ed67789SDaniel Lezcano 					   GFP_KERNEL);
64438ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
6444421842edSDavid Ahern 		goto out_fib6_null_entry;
6445d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
644662fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
644762fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
644874109218SWei Wang 	INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
64498ed67789SDaniel Lezcano 
64508ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6451feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
64528ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
64538ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
64548ed67789SDaniel Lezcano 					       GFP_KERNEL);
645568fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
645668fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
6457d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
645862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
645962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
646074109218SWei Wang 	INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
64618ed67789SDaniel Lezcano 
64628ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
64638ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
64648ed67789SDaniel Lezcano 					       GFP_KERNEL);
646568fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
646668fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
6467d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
646862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
646962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
647074109218SWei Wang 	INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
6471b9b33e7cSPaolo Abeni #ifdef CONFIG_IPV6_SUBTREES
6472b9b33e7cSPaolo Abeni 	net->ipv6.fib6_routes_require_src = 0;
6473b9b33e7cSPaolo Abeni #endif
64748ed67789SDaniel Lezcano #endif
64758ed67789SDaniel Lezcano 
6476b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
6477b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
6478b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6479b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6480b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6481b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6482b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6483b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
64847c6bb7d2SDavid Ahern 	net->ipv6.sysctl.skip_notify_on_dev_down = 0;
6485b339a47cSPeter Zijlstra 
64866891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
64876891a346SBenjamin Thery 
64888ed67789SDaniel Lezcano 	ret = 0;
64898ed67789SDaniel Lezcano out:
64908ed67789SDaniel Lezcano 	return ret;
6491f2fc6a54SBenjamin Thery 
649268fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
649368fffc67SPeter Zijlstra out_ip6_prohibit_entry:
649468fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
649568fffc67SPeter Zijlstra out_ip6_null_entry:
649668fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
649768fffc67SPeter Zijlstra #endif
6498421842edSDavid Ahern out_fib6_null_entry:
6499421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
6500fc66f95cSEric Dumazet out_ip6_dst_entries:
6501fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6502f2fc6a54SBenjamin Thery out_ip6_dst_ops:
6503f2fc6a54SBenjamin Thery 	goto out;
6504cdb18761SDaniel Lezcano }
6505cdb18761SDaniel Lezcano 
65062c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
6507cdb18761SDaniel Lezcano {
6508421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
65098ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
65108ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
65118ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
65128ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
65138ed67789SDaniel Lezcano #endif
651441bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6515cdb18761SDaniel Lezcano }
6516cdb18761SDaniel Lezcano 
6517d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
6518d189634eSThomas Graf {
6519d189634eSThomas Graf #ifdef CONFIG_PROC_FS
6520c3506372SChristoph Hellwig 	proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6521c3506372SChristoph Hellwig 			sizeof(struct ipv6_route_iter));
65223617d949SChristoph Hellwig 	proc_create_net_single("rt6_stats", 0444, net->proc_net,
65233617d949SChristoph Hellwig 			rt6_stats_seq_show, NULL);
6524d189634eSThomas Graf #endif
6525d189634eSThomas Graf 	return 0;
6526d189634eSThomas Graf }
6527d189634eSThomas Graf 
6528d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
6529d189634eSThomas Graf {
6530d189634eSThomas Graf #ifdef CONFIG_PROC_FS
6531ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
6532ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
6533d189634eSThomas Graf #endif
6534d189634eSThomas Graf }
6535d189634eSThomas Graf 
6536cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
6537cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
6538cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
6539cdb18761SDaniel Lezcano };
6540cdb18761SDaniel Lezcano 
6541c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
6542c3426b47SDavid S. Miller {
6543c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6544c3426b47SDavid S. Miller 
6545c3426b47SDavid S. Miller 	if (!bp)
6546c3426b47SDavid S. Miller 		return -ENOMEM;
6547c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
6548c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
6549c3426b47SDavid S. Miller 	return 0;
6550c3426b47SDavid S. Miller }
6551c3426b47SDavid S. Miller 
6552c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
6553c3426b47SDavid S. Miller {
6554c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
6555c3426b47SDavid S. Miller 
6556c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
655756a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
6558c3426b47SDavid S. Miller 	kfree(bp);
6559c3426b47SDavid S. Miller }
6560c3426b47SDavid S. Miller 
65612b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
6562c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
6563c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
6564c3426b47SDavid S. Miller };
6565c3426b47SDavid S. Miller 
6566d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
6567d189634eSThomas Graf 	.init = ip6_route_net_init_late,
6568d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
6569d189634eSThomas Graf };
6570d189634eSThomas Graf 
65718ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
65728ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
6573242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
65748ed67789SDaniel Lezcano };
65758ed67789SDaniel Lezcano 
65762f460933SWANG Cong void __init ip6_route_init_special_entries(void)
65772f460933SWANG Cong {
65782f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
65792f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
65802f460933SWANG Cong 	 * manually for init_net */
65811cf844c7SDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
65822f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
65832f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
65842f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
65852f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
65862f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
65872f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
65882f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
65892f460933SWANG Cong   #endif
65902f460933SWANG Cong }
65912f460933SWANG Cong 
6592138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6)
6593138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6594138d0be3SYonghong Song DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6595138d0be3SYonghong Song 
6596951cf368SYonghong Song BTF_ID_LIST(btf_fib6_info_id)
6597951cf368SYonghong Song BTF_ID(struct, fib6_info)
6598951cf368SYonghong Song 
659914fc6bd6SYonghong Song static const struct bpf_iter_seq_info ipv6_route_seq_info = {
6600138d0be3SYonghong Song 	.seq_ops		= &ipv6_route_seq_ops,
6601138d0be3SYonghong Song 	.init_seq_private	= bpf_iter_init_seq_net,
6602138d0be3SYonghong Song 	.fini_seq_private	= bpf_iter_fini_seq_net,
6603138d0be3SYonghong Song 	.seq_priv_size		= sizeof(struct ipv6_route_iter),
660414fc6bd6SYonghong Song };
660514fc6bd6SYonghong Song 
660614fc6bd6SYonghong Song static struct bpf_iter_reg ipv6_route_reg_info = {
660714fc6bd6SYonghong Song 	.target			= "ipv6_route",
66083c32cc1bSYonghong Song 	.ctx_arg_info_size	= 1,
66093c32cc1bSYonghong Song 	.ctx_arg_info		= {
66103c32cc1bSYonghong Song 		{ offsetof(struct bpf_iter__ipv6_route, rt),
66113c32cc1bSYonghong Song 		  PTR_TO_BTF_ID_OR_NULL },
66123c32cc1bSYonghong Song 	},
661314fc6bd6SYonghong Song 	.seq_info		= &ipv6_route_seq_info,
6614138d0be3SYonghong Song };
6615138d0be3SYonghong Song 
661615172a46SYonghong Song static int __init bpf_iter_register(void)
661715172a46SYonghong Song {
6618951cf368SYonghong Song 	ipv6_route_reg_info.ctx_arg_info[0].btf_id = *btf_fib6_info_id;
661915172a46SYonghong Song 	return bpf_iter_reg_target(&ipv6_route_reg_info);
6620138d0be3SYonghong Song }
6621138d0be3SYonghong Song 
6622138d0be3SYonghong Song static void bpf_iter_unregister(void)
6623138d0be3SYonghong Song {
6624ab2ee4fcSYonghong Song 	bpf_iter_unreg_target(&ipv6_route_reg_info);
6625138d0be3SYonghong Song }
6626138d0be3SYonghong Song #endif
6627138d0be3SYonghong Song #endif
6628138d0be3SYonghong Song 
6629433d49c3SDaniel Lezcano int __init ip6_route_init(void)
66301da177e4SLinus Torvalds {
6631433d49c3SDaniel Lezcano 	int ret;
66328d0b94afSMartin KaFai Lau 	int cpu;
6633433d49c3SDaniel Lezcano 
66349a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
66359a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
66369a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
66379a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
66389a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
6639c19a28e1SFernando Carrijo 		goto out;
664014e50e57SDavid S. Miller 
6641fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
66428ed67789SDaniel Lezcano 	if (ret)
6643bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
6644bdb3289fSDaniel Lezcano 
6645c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6646c3426b47SDavid S. Miller 	if (ret)
6647e8803b6cSDavid S. Miller 		goto out_dst_entries;
66482a0c451aSThomas Graf 
66497e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
66507e52b33bSDavid S. Miller 	if (ret)
66517e52b33bSDavid S. Miller 		goto out_register_inetpeer;
6652c3426b47SDavid S. Miller 
66535dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
66545dc121e9SArnaud Ebalard 
6655e8803b6cSDavid S. Miller 	ret = fib6_init();
6656433d49c3SDaniel Lezcano 	if (ret)
66578ed67789SDaniel Lezcano 		goto out_register_subsys;
6658433d49c3SDaniel Lezcano 
6659433d49c3SDaniel Lezcano 	ret = xfrm6_init();
6660433d49c3SDaniel Lezcano 	if (ret)
6661e8803b6cSDavid S. Miller 		goto out_fib6_init;
6662c35b7e72SDaniel Lezcano 
6663433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
6664433d49c3SDaniel Lezcano 	if (ret)
6665433d49c3SDaniel Lezcano 		goto xfrm6_init;
66667e5449c2SDaniel Lezcano 
6667d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
6668d189634eSThomas Graf 	if (ret)
6669d189634eSThomas Graf 		goto fib6_rules_init;
6670d189634eSThomas Graf 
667116feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
667216feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
667316feebcfSFlorian Westphal 	if (ret < 0)
667416feebcfSFlorian Westphal 		goto out_register_late_subsys;
667516feebcfSFlorian Westphal 
667616feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
667716feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
667816feebcfSFlorian Westphal 	if (ret < 0)
667916feebcfSFlorian Westphal 		goto out_register_late_subsys;
668016feebcfSFlorian Westphal 
668116feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
668216feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
668316feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
668416feebcfSFlorian Westphal 	if (ret < 0)
6685d189634eSThomas Graf 		goto out_register_late_subsys;
6686433d49c3SDaniel Lezcano 
66878ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
6688cdb18761SDaniel Lezcano 	if (ret)
6689d189634eSThomas Graf 		goto out_register_late_subsys;
66908ed67789SDaniel Lezcano 
6691138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6)
6692138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6693138d0be3SYonghong Song 	ret = bpf_iter_register();
6694138d0be3SYonghong Song 	if (ret)
6695138d0be3SYonghong Song 		goto out_register_late_subsys;
6696138d0be3SYonghong Song #endif
6697138d0be3SYonghong Song #endif
6698138d0be3SYonghong Song 
66998d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
67008d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
67018d0b94afSMartin KaFai Lau 
67028d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
67038d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
67048d0b94afSMartin KaFai Lau 	}
67058d0b94afSMartin KaFai Lau 
6706433d49c3SDaniel Lezcano out:
6707433d49c3SDaniel Lezcano 	return ret;
6708433d49c3SDaniel Lezcano 
6709d189634eSThomas Graf out_register_late_subsys:
671016feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
6711d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
6712433d49c3SDaniel Lezcano fib6_rules_init:
6713433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
6714433d49c3SDaniel Lezcano xfrm6_init:
6715433d49c3SDaniel Lezcano 	xfrm6_fini();
67162a0c451aSThomas Graf out_fib6_init:
67172a0c451aSThomas Graf 	fib6_gc_cleanup();
67188ed67789SDaniel Lezcano out_register_subsys:
67198ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
67207e52b33bSDavid S. Miller out_register_inetpeer:
67217e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
6722fc66f95cSEric Dumazet out_dst_entries:
6723fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
6724433d49c3SDaniel Lezcano out_kmem_cache:
6725f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6726433d49c3SDaniel Lezcano 	goto out;
67271da177e4SLinus Torvalds }
67281da177e4SLinus Torvalds 
67291da177e4SLinus Torvalds void ip6_route_cleanup(void)
67301da177e4SLinus Torvalds {
6731138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6)
6732138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6733138d0be3SYonghong Song 	bpf_iter_unregister();
6734138d0be3SYonghong Song #endif
6735138d0be3SYonghong Song #endif
67368ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
6737d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
6738101367c2SThomas Graf 	fib6_rules_cleanup();
67391da177e4SLinus Torvalds 	xfrm6_fini();
67401da177e4SLinus Torvalds 	fib6_gc_cleanup();
6741c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
67428ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
674341bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
6744f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
67451da177e4SLinus Torvalds }
6746