xref: /openbmc/linux/net/ipv6/route.c (revision 2874c5fd284268364ece81a7bd936f3c8168e567)
1*2874c5fdSThomas 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>
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
661da177e4SLinus Torvalds #include <linux/sysctl.h>
671da177e4SLinus Torvalds #endif
681da177e4SLinus Torvalds 
6930d444d3SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type);
7030d444d3SDavid Ahern 
7130d444d3SDavid Ahern #define CREATE_TRACE_POINTS
7230d444d3SDavid Ahern #include <trace/events/fib6.h>
7330d444d3SDavid Ahern EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
7430d444d3SDavid Ahern #undef CREATE_TRACE_POINTS
7530d444d3SDavid Ahern 
76afc154e9SHannes Frederic Sowa enum rt6_nud_state {
777e980569SJiri Benc 	RT6_NUD_FAIL_HARD = -3,
787e980569SJiri Benc 	RT6_NUD_FAIL_PROBE = -2,
797e980569SJiri Benc 	RT6_NUD_FAIL_DO_RR = -1,
80afc154e9SHannes Frederic Sowa 	RT6_NUD_SUCCEED = 1
81afc154e9SHannes Frederic Sowa };
82afc154e9SHannes Frederic Sowa 
831da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
840dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
85ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
861da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
871da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
881da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
891da177e4SLinus Torvalds 				       struct net_device *dev, int how);
90569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
93ede2059dSEric W. Biederman static int		ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
947150aedeSKamala R static int		ip6_pkt_prohibit(struct sk_buff *skb);
95ede2059dSEric W. Biederman static int		ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
961da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
976700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
986700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
996700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
1006700c270SDavid S. Miller 					struct sk_buff *skb);
101702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
102702cea56SDavid Ahern 			   int strict);
1038d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt);
104d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
1058d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
106d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
10716a16cd3SDavid Ahern 			 int iif, int type, u32 portid, u32 seq,
10816a16cd3SDavid Ahern 			 unsigned int flags);
1097e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
110510e2cedSWei Wang 					   const struct in6_addr *daddr,
111510e2cedSWei Wang 					   const struct in6_addr *saddr);
1121da177e4SLinus Torvalds 
11370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1148d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
115b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
116830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
117830218c1SDavid Ahern 					   struct net_device *dev,
11895c96174SEric Dumazet 					   unsigned int pref);
1198d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
120b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
121830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
122830218c1SDavid Ahern 					   struct net_device *dev);
12370ceb4f5SYOSHIFUJI Hideaki #endif
12470ceb4f5SYOSHIFUJI Hideaki 
1258d0b94afSMartin KaFai Lau struct uncached_list {
1268d0b94afSMartin KaFai Lau 	spinlock_t		lock;
1278d0b94afSMartin KaFai Lau 	struct list_head	head;
1288d0b94afSMartin KaFai Lau };
1298d0b94afSMartin KaFai Lau 
1308d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
1318d0b94afSMartin KaFai Lau 
132510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt)
1338d0b94afSMartin KaFai Lau {
1348d0b94afSMartin KaFai Lau 	struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
1358d0b94afSMartin KaFai Lau 
1368d0b94afSMartin KaFai Lau 	rt->rt6i_uncached_list = ul;
1378d0b94afSMartin KaFai Lau 
1388d0b94afSMartin KaFai Lau 	spin_lock_bh(&ul->lock);
1398d0b94afSMartin KaFai Lau 	list_add_tail(&rt->rt6i_uncached, &ul->head);
1408d0b94afSMartin KaFai Lau 	spin_unlock_bh(&ul->lock);
1418d0b94afSMartin KaFai Lau }
1428d0b94afSMartin KaFai Lau 
143510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt)
1448d0b94afSMartin KaFai Lau {
1458d0b94afSMartin KaFai Lau 	if (!list_empty(&rt->rt6i_uncached)) {
1468d0b94afSMartin KaFai Lau 		struct uncached_list *ul = rt->rt6i_uncached_list;
14781eb8447SWei Wang 		struct net *net = dev_net(rt->dst.dev);
1488d0b94afSMartin KaFai Lau 
1498d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1508d0b94afSMartin KaFai Lau 		list_del(&rt->rt6i_uncached);
15181eb8447SWei Wang 		atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
1528d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1538d0b94afSMartin KaFai Lau 	}
1548d0b94afSMartin KaFai Lau }
1558d0b94afSMartin KaFai Lau 
1568d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
1578d0b94afSMartin KaFai Lau {
1588d0b94afSMartin KaFai Lau 	struct net_device *loopback_dev = net->loopback_dev;
1598d0b94afSMartin KaFai Lau 	int cpu;
1608d0b94afSMartin KaFai Lau 
161e332bc67SEric W. Biederman 	if (dev == loopback_dev)
162e332bc67SEric W. Biederman 		return;
163e332bc67SEric W. Biederman 
1648d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
1658d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
1668d0b94afSMartin KaFai Lau 		struct rt6_info *rt;
1678d0b94afSMartin KaFai Lau 
1688d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1698d0b94afSMartin KaFai Lau 		list_for_each_entry(rt, &ul->head, rt6i_uncached) {
1708d0b94afSMartin KaFai Lau 			struct inet6_dev *rt_idev = rt->rt6i_idev;
1718d0b94afSMartin KaFai Lau 			struct net_device *rt_dev = rt->dst.dev;
1728d0b94afSMartin KaFai Lau 
173e332bc67SEric W. Biederman 			if (rt_idev->dev == dev) {
1748d0b94afSMartin KaFai Lau 				rt->rt6i_idev = in6_dev_get(loopback_dev);
1758d0b94afSMartin KaFai Lau 				in6_dev_put(rt_idev);
1768d0b94afSMartin KaFai Lau 			}
1778d0b94afSMartin KaFai Lau 
178e332bc67SEric W. Biederman 			if (rt_dev == dev) {
1798d0b94afSMartin KaFai Lau 				rt->dst.dev = loopback_dev;
1808d0b94afSMartin KaFai Lau 				dev_hold(rt->dst.dev);
1818d0b94afSMartin KaFai Lau 				dev_put(rt_dev);
1828d0b94afSMartin KaFai Lau 			}
1838d0b94afSMartin KaFai Lau 		}
1848d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1858d0b94afSMartin KaFai Lau 	}
1868d0b94afSMartin KaFai Lau }
1878d0b94afSMartin KaFai Lau 
188f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p,
189f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
190f894cbf8SDavid S. Miller 					     const void *daddr)
19139232973SDavid S. Miller {
192a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
19339232973SDavid S. Miller 		return (const void *) p;
194f894cbf8SDavid S. Miller 	else if (skb)
195f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
19639232973SDavid S. Miller 	return daddr;
19739232973SDavid S. Miller }
19839232973SDavid S. Miller 
199f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
200f8a1b43bSDavid Ahern 				   struct net_device *dev,
201f894cbf8SDavid S. Miller 				   struct sk_buff *skb,
202f894cbf8SDavid S. Miller 				   const void *daddr)
203d3aaeb38SDavid S. Miller {
20439232973SDavid S. Miller 	struct neighbour *n;
20539232973SDavid S. Miller 
206f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(gw, skb, daddr);
207f8a1b43bSDavid Ahern 	n = __ipv6_neigh_lookup(dev, daddr);
208f83c7790SDavid S. Miller 	if (n)
209f83c7790SDavid S. Miller 		return n;
2107adf3246SStefano Brivio 
2117adf3246SStefano Brivio 	n = neigh_create(&nd_tbl, daddr, dev);
2127adf3246SStefano Brivio 	return IS_ERR(n) ? NULL : n;
213f8a1b43bSDavid Ahern }
214f8a1b43bSDavid Ahern 
215f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
216f8a1b43bSDavid Ahern 					      struct sk_buff *skb,
217f8a1b43bSDavid Ahern 					      const void *daddr)
218f8a1b43bSDavid Ahern {
219f8a1b43bSDavid Ahern 	const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
220f8a1b43bSDavid Ahern 
221f8a1b43bSDavid Ahern 	return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr);
222f83c7790SDavid S. Miller }
223f83c7790SDavid S. Miller 
22463fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
22563fca65dSJulian Anastasov {
22663fca65dSJulian Anastasov 	struct net_device *dev = dst->dev;
22763fca65dSJulian Anastasov 	struct rt6_info *rt = (struct rt6_info *)dst;
22863fca65dSJulian Anastasov 
229f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
23063fca65dSJulian Anastasov 	if (!daddr)
23163fca65dSJulian Anastasov 		return;
23263fca65dSJulian Anastasov 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
23363fca65dSJulian Anastasov 		return;
23463fca65dSJulian Anastasov 	if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
23563fca65dSJulian Anastasov 		return;
23663fca65dSJulian Anastasov 	__ipv6_confirm_neigh(dev, daddr);
23763fca65dSJulian Anastasov }
23863fca65dSJulian Anastasov 
2399a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
2401da177e4SLinus Torvalds 	.family			=	AF_INET6,
2411da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
2421da177e4SLinus Torvalds 	.gc_thresh		=	1024,
2431da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
2440dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
245ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
246d4ead6b3SDavid Ahern 	.cow_metrics		=	dst_cow_metrics_generic,
2471da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
2481da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
2491da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
2501da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
2511da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
2526e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
2539f8955ccSEric W. Biederman 	.local_out		=	__ip6_local_out,
254f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
25563fca65dSJulian Anastasov 	.confirm_neigh		=	ip6_confirm_neigh,
2561da177e4SLinus Torvalds };
2571da177e4SLinus Torvalds 
258ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
259ec831ea7SRoland Dreier {
260618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
261618f9bc7SSteffen Klassert 
262618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
263ec831ea7SRoland Dreier }
264ec831ea7SRoland Dreier 
2656700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
2666700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
26714e50e57SDavid S. Miller {
26814e50e57SDavid S. Miller }
26914e50e57SDavid S. Miller 
2706700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
2716700c270SDavid S. Miller 				      struct sk_buff *skb)
272b587ee3bSDavid S. Miller {
273b587ee3bSDavid S. Miller }
274b587ee3bSDavid S. Miller 
27514e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
27614e50e57SDavid S. Miller 	.family			=	AF_INET6,
27714e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
27814e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
279ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
280214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
28114e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
282b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2830a1f5962SMartin KaFai Lau 	.cow_metrics		=	dst_cow_metrics_generic,
284f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
28514e50e57SDavid S. Miller };
28614e50e57SDavid S. Miller 
28762fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
28814edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
28962fa8a84SDavid S. Miller };
29062fa8a84SDavid S. Miller 
2918d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = {
29293c2fb25SDavid Ahern 	.fib6_flags	= (RTF_REJECT | RTF_NONEXTHOP),
29393c2fb25SDavid Ahern 	.fib6_protocol  = RTPROT_KERNEL,
29493c2fb25SDavid Ahern 	.fib6_metric	= ~(u32)0,
295f05713e0SEric Dumazet 	.fib6_ref	= REFCOUNT_INIT(1),
296421842edSDavid Ahern 	.fib6_type	= RTN_UNREACHABLE,
297421842edSDavid Ahern 	.fib6_metrics	= (struct dst_metrics *)&dst_default_metrics,
298421842edSDavid Ahern };
299421842edSDavid Ahern 
300fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
3011da177e4SLinus Torvalds 	.dst = {
3021da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
3031da177e4SLinus Torvalds 		.__use		= 1,
3042c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
3051da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
3061da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
3071da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
3081da177e4SLinus Torvalds 	},
3091da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3101da177e4SLinus Torvalds };
3111da177e4SLinus Torvalds 
312101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
313101367c2SThomas Graf 
314fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_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		= -EACCES,
3209ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
3219ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
322101367c2SThomas Graf 	},
323101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
324101367c2SThomas Graf };
325101367c2SThomas Graf 
326fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
327101367c2SThomas Graf 	.dst = {
328101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
329101367c2SThomas Graf 		.__use		= 1,
3302c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
331101367c2SThomas Graf 		.error		= -EINVAL,
332352e512cSHerbert Xu 		.input		= dst_discard,
333ede2059dSEric W. Biederman 		.output		= dst_discard_out,
334101367c2SThomas Graf 	},
335101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
336101367c2SThomas Graf };
337101367c2SThomas Graf 
338101367c2SThomas Graf #endif
339101367c2SThomas Graf 
340ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt)
341ebfa45f0SMartin KaFai Lau {
342ebfa45f0SMartin KaFai Lau 	struct dst_entry *dst = &rt->dst;
343ebfa45f0SMartin KaFai Lau 
344ebfa45f0SMartin KaFai Lau 	memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
345ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_uncached);
346ebfa45f0SMartin KaFai Lau }
347ebfa45f0SMartin KaFai Lau 
3481da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
34993531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
350ad706862SMartin KaFai Lau 			       int flags)
3511da177e4SLinus Torvalds {
35297bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
353b2a9c0edSWei Wang 					1, DST_OBSOLETE_FORCE_CHK, flags);
354cf911662SDavid S. Miller 
35581eb8447SWei Wang 	if (rt) {
356ebfa45f0SMartin KaFai Lau 		rt6_info_init(rt);
35781eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
35881eb8447SWei Wang 	}
3598104891bSSteffen Klassert 
360cf911662SDavid S. Miller 	return rt;
3611da177e4SLinus Torvalds }
3629ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc);
363d52d3997SMartin KaFai Lau 
3641da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
367a68886a6SDavid Ahern 	struct fib6_info *from;
3688d0b94afSMartin KaFai Lau 	struct inet6_dev *idev;
3691da177e4SLinus Torvalds 
3701620a336SDavid Ahern 	ip_dst_metrics_put(dst);
3718d0b94afSMartin KaFai Lau 	rt6_uncached_list_del(rt);
3728d0b94afSMartin KaFai Lau 
3738d0b94afSMartin KaFai Lau 	idev = rt->rt6i_idev;
37438308473SDavid S. Miller 	if (idev) {
3751da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3761da177e4SLinus Torvalds 		in6_dev_put(idev);
3771da177e4SLinus Torvalds 	}
3781716a961SGao feng 
3790e233874SEric Dumazet 	from = xchg((__force struct fib6_info **)&rt->from, NULL);
38093531c67SDavid Ahern 	fib6_info_release(from);
381b3419363SDavid S. Miller }
382b3419363SDavid S. Miller 
3831da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3841da177e4SLinus Torvalds 			   int how)
3851da177e4SLinus Torvalds {
3861da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3871da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3885a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
389c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3901da177e4SLinus Torvalds 
391e5645f51SWei Wang 	if (idev && idev->dev != loopback_dev) {
392e5645f51SWei Wang 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
39338308473SDavid S. Miller 		if (loopback_idev) {
3941da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
3951da177e4SLinus Torvalds 			in6_dev_put(idev);
3961da177e4SLinus Torvalds 		}
3971da177e4SLinus Torvalds 	}
39897cac082SDavid S. Miller }
3991da177e4SLinus Torvalds 
4005973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt)
4015973fb1eSMartin KaFai Lau {
4025973fb1eSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_EXPIRES)
4035973fb1eSMartin KaFai Lau 		return time_after(jiffies, rt->dst.expires);
4045973fb1eSMartin KaFai Lau 	else
4055973fb1eSMartin KaFai Lau 		return false;
4065973fb1eSMartin KaFai Lau }
4075973fb1eSMartin KaFai Lau 
408a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
4091da177e4SLinus Torvalds {
410a68886a6SDavid Ahern 	struct fib6_info *from;
411a68886a6SDavid Ahern 
412a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
413a68886a6SDavid Ahern 
4141716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
4151716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
416a50feda5SEric Dumazet 			return true;
417a68886a6SDavid Ahern 	} else if (from) {
4181e2ea8adSXin Long 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
419a68886a6SDavid Ahern 			fib6_check_expired(from);
4201716a961SGao feng 	}
421a50feda5SEric Dumazet 	return false;
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
424b1d40991SDavid Ahern void fib6_select_path(const struct net *net, struct fib6_result *res,
425b1d40991SDavid Ahern 		      struct flowi6 *fl6, int oif, bool have_oif_match,
426b1d40991SDavid Ahern 		      const struct sk_buff *skb, int strict)
42751ebd318SNicolas Dichtel {
4288d1c802bSDavid Ahern 	struct fib6_info *sibling, *next_sibling;
429b1d40991SDavid Ahern 	struct fib6_info *match = res->f6i;
430b1d40991SDavid Ahern 
431b1d40991SDavid Ahern 	if (!match->fib6_nsiblings || have_oif_match)
432b1d40991SDavid Ahern 		goto out;
43351ebd318SNicolas Dichtel 
434b673d6ccSJakub Sitnicki 	/* We might have already computed the hash for ICMPv6 errors. In such
435b673d6ccSJakub Sitnicki 	 * case it will always be non-zero. Otherwise now is the time to do it.
436b673d6ccSJakub Sitnicki 	 */
437b673d6ccSJakub Sitnicki 	if (!fl6->mp_hash)
438b4bac172SDavid Ahern 		fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
439b673d6ccSJakub Sitnicki 
440ad1601aeSDavid Ahern 	if (fl6->mp_hash <= atomic_read(&match->fib6_nh.fib_nh_upper_bound))
441b1d40991SDavid Ahern 		goto out;
442bbfcd776SIdo Schimmel 
44393c2fb25SDavid Ahern 	list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
44493c2fb25SDavid Ahern 				 fib6_siblings) {
445702cea56SDavid Ahern 		const struct fib6_nh *nh = &sibling->fib6_nh;
4465e670d84SDavid Ahern 		int nh_upper_bound;
4475e670d84SDavid Ahern 
448702cea56SDavid Ahern 		nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
4495e670d84SDavid Ahern 		if (fl6->mp_hash > nh_upper_bound)
4503d709f69SIdo Schimmel 			continue;
451702cea56SDavid Ahern 		if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
45252bd4c0cSNicolas Dichtel 			break;
45351ebd318SNicolas Dichtel 		match = sibling;
45451ebd318SNicolas Dichtel 		break;
45551ebd318SNicolas Dichtel 	}
4563d709f69SIdo Schimmel 
457b1d40991SDavid Ahern out:
458b1d40991SDavid Ahern 	res->f6i = match;
459b1d40991SDavid Ahern 	res->nh = &match->fib6_nh;
46051ebd318SNicolas Dichtel }
46151ebd318SNicolas Dichtel 
4621da177e4SLinus Torvalds /*
46366f5d6ceSWei Wang  *	Route lookup. rcu_read_lock() should be held.
4641da177e4SLinus Torvalds  */
4651da177e4SLinus Torvalds 
4660c59d006SDavid Ahern static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
4670c59d006SDavid Ahern 			       const struct in6_addr *saddr, int oif, int flags)
4680c59d006SDavid Ahern {
4690c59d006SDavid Ahern 	const struct net_device *dev;
4700c59d006SDavid Ahern 
4710c59d006SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD)
4720c59d006SDavid Ahern 		return false;
4730c59d006SDavid Ahern 
4740c59d006SDavid Ahern 	dev = nh->fib_nh_dev;
4750c59d006SDavid Ahern 	if (oif) {
4760c59d006SDavid Ahern 		if (dev->ifindex == oif)
4770c59d006SDavid Ahern 			return true;
4780c59d006SDavid Ahern 	} else {
4790c59d006SDavid Ahern 		if (ipv6_chk_addr(net, saddr, dev,
4800c59d006SDavid Ahern 				  flags & RT6_LOOKUP_F_IFACE))
4810c59d006SDavid Ahern 			return true;
4820c59d006SDavid Ahern 	}
4830c59d006SDavid Ahern 
4840c59d006SDavid Ahern 	return false;
4850c59d006SDavid Ahern }
4860c59d006SDavid Ahern 
48775ef7389SDavid Ahern static void rt6_device_match(struct net *net, struct fib6_result *res,
48875ef7389SDavid Ahern 			     const struct in6_addr *saddr, int oif, int flags)
4891da177e4SLinus Torvalds {
49075ef7389SDavid Ahern 	struct fib6_info *f6i = res->f6i;
49175ef7389SDavid Ahern 	struct fib6_info *spf6i;
49275ef7389SDavid Ahern 	struct fib6_nh *nh;
4931da177e4SLinus Torvalds 
49475ef7389SDavid Ahern 	if (!oif && ipv6_addr_any(saddr)) {
49575ef7389SDavid Ahern 		nh = &f6i->fib6_nh;
4967d21fec9SDavid Ahern 		if (!(nh->fib_nh_flags & RTNH_F_DEAD))
4977d21fec9SDavid Ahern 			goto out;
4981da177e4SLinus Torvalds 	}
4991da177e4SLinus Torvalds 
50075ef7389SDavid Ahern 	for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
50175ef7389SDavid Ahern 		nh = &spf6i->fib6_nh;
50275ef7389SDavid Ahern 		if (__rt6_device_match(net, nh, saddr, oif, flags)) {
50375ef7389SDavid Ahern 			res->f6i = spf6i;
5047d21fec9SDavid Ahern 			goto out;
50575ef7389SDavid Ahern 		}
50675ef7389SDavid Ahern 	}
5071da177e4SLinus Torvalds 
50875ef7389SDavid Ahern 	if (oif && flags & RT6_LOOKUP_F_IFACE) {
50975ef7389SDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
5107d21fec9SDavid Ahern 		nh = &res->f6i->fib6_nh;
5117d21fec9SDavid Ahern 		goto out;
51275ef7389SDavid Ahern 	}
51375ef7389SDavid Ahern 
5147d21fec9SDavid Ahern 	nh = &f6i->fib6_nh;
5157d21fec9SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD) {
51675ef7389SDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
5177d21fec9SDavid Ahern 		nh = &res->f6i->fib6_nh;
51875ef7389SDavid Ahern 	}
5197d21fec9SDavid Ahern out:
5207d21fec9SDavid Ahern 	res->nh = nh;
5217d21fec9SDavid Ahern 	res->fib6_type = res->f6i->fib6_type;
5227d21fec9SDavid Ahern 	res->fib6_flags = res->f6i->fib6_flags;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
52527097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
526c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
527c2f17e82SHannes Frederic Sowa 	struct work_struct work;
528c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
529c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
530c2f17e82SHannes Frederic Sowa };
531c2f17e82SHannes Frederic Sowa 
532c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
533c2f17e82SHannes Frederic Sowa {
534c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
535c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
536c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
537c2f17e82SHannes Frederic Sowa 
538c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
539adc176c5SErik Nordmark 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
540c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
541662f5533SMichael Büsch 	kfree(work);
542c2f17e82SHannes Frederic Sowa }
543c2f17e82SHannes Frederic Sowa 
544cc3a86c8SDavid Ahern static void rt6_probe(struct fib6_nh *fib6_nh)
54527097255SYOSHIFUJI Hideaki {
546f547fac6SSabrina Dubroca 	struct __rt6_probe_work *work = NULL;
5475e670d84SDavid Ahern 	const struct in6_addr *nh_gw;
548f2c31e32SEric Dumazet 	struct neighbour *neigh;
5495e670d84SDavid Ahern 	struct net_device *dev;
550f547fac6SSabrina Dubroca 	struct inet6_dev *idev;
5515e670d84SDavid Ahern 
55227097255SYOSHIFUJI Hideaki 	/*
55327097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
55427097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
55527097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
55627097255SYOSHIFUJI Hideaki 	 *
55727097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
55827097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
55927097255SYOSHIFUJI Hideaki 	 */
560cc3a86c8SDavid Ahern 	if (fib6_nh->fib_nh_gw_family)
561fdd6681dSAmerigo Wang 		return;
5625e670d84SDavid Ahern 
563cc3a86c8SDavid Ahern 	nh_gw = &fib6_nh->fib_nh_gw6;
564cc3a86c8SDavid Ahern 	dev = fib6_nh->fib_nh_dev;
5652152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
566f547fac6SSabrina Dubroca 	idev = __in6_dev_get(dev);
5675e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
5682152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
5698d6c31bfSMartin KaFai Lau 		if (neigh->nud_state & NUD_VALID)
5708d6c31bfSMartin KaFai Lau 			goto out;
5718d6c31bfSMartin KaFai Lau 
5722152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
573990edb42SMartin KaFai Lau 		if (!(neigh->nud_state & NUD_VALID) &&
574990edb42SMartin KaFai Lau 		    time_after(jiffies,
575dcd1f572SDavid Ahern 			       neigh->updated + idev->cnf.rtr_probe_interval)) {
576c2f17e82SHannes Frederic Sowa 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
577990edb42SMartin KaFai Lau 			if (work)
5787e980569SJiri Benc 				__neigh_set_probe_once(neigh);
579990edb42SMartin KaFai Lau 		}
580c2f17e82SHannes Frederic Sowa 		write_unlock(&neigh->lock);
581cc3a86c8SDavid Ahern 	} else if (time_after(jiffies, fib6_nh->last_probe +
582f547fac6SSabrina Dubroca 				       idev->cnf.rtr_probe_interval)) {
583990edb42SMartin KaFai Lau 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
584990edb42SMartin KaFai Lau 	}
585c2f17e82SHannes Frederic Sowa 
586c2f17e82SHannes Frederic Sowa 	if (work) {
587cc3a86c8SDavid Ahern 		fib6_nh->last_probe = jiffies;
588c2f17e82SHannes Frederic Sowa 		INIT_WORK(&work->work, rt6_probe_deferred);
5895e670d84SDavid Ahern 		work->target = *nh_gw;
5905e670d84SDavid Ahern 		dev_hold(dev);
5915e670d84SDavid Ahern 		work->dev = dev;
592c2f17e82SHannes Frederic Sowa 		schedule_work(&work->work);
593c2f17e82SHannes Frederic Sowa 	}
594990edb42SMartin KaFai Lau 
5958d6c31bfSMartin KaFai Lau out:
5962152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
597f2c31e32SEric Dumazet }
59827097255SYOSHIFUJI Hideaki #else
599cc3a86c8SDavid Ahern static inline void rt6_probe(struct fib6_nh *fib6_nh)
60027097255SYOSHIFUJI Hideaki {
60127097255SYOSHIFUJI Hideaki }
60227097255SYOSHIFUJI Hideaki #endif
60327097255SYOSHIFUJI Hideaki 
6041da177e4SLinus Torvalds /*
605554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
6061da177e4SLinus Torvalds  */
6071ba9a895SDavid Ahern static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
6081da177e4SLinus Torvalds {
609afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
6105e670d84SDavid Ahern 	struct neighbour *neigh;
611f2c31e32SEric Dumazet 
612145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
6131ba9a895SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
6141ba9a895SDavid Ahern 					  &fib6_nh->fib_nh_gw6);
615145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
616145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
617554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
618afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
619398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
620a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
621afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
6227e980569SJiri Benc 		else
6237e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
624398bcbebSYOSHIFUJI Hideaki #endif
625145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
626afc154e9SHannes Frederic Sowa 	} else {
627afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
6287e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
629a5a81f0bSPaul Marks 	}
630145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
631145a3621SYOSHIFUJI Hideaki / 吉藤英明 
632a5a81f0bSPaul Marks 	return ret;
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
635702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
636702cea56SDavid Ahern 			   int strict)
637554cfb7eSYOSHIFUJI Hideaki {
6386e1809a5SDavid Ahern 	int m = 0;
6394d0c5911SYOSHIFUJI Hideaki 
6406e1809a5SDavid Ahern 	if (!oif || nh->fib_nh_dev->ifindex == oif)
6416e1809a5SDavid Ahern 		m = 2;
6426e1809a5SDavid Ahern 
64377d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
644afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
645ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
646702cea56SDavid Ahern 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
647ebacaaa0SYOSHIFUJI Hideaki #endif
6481ba9a895SDavid Ahern 	if ((strict & RT6_LOOKUP_F_REACHABLE) &&
649702cea56SDavid Ahern 	    !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
6501ba9a895SDavid Ahern 		int n = rt6_check_neigh(nh);
651afc154e9SHannes Frederic Sowa 		if (n < 0)
652afc154e9SHannes Frederic Sowa 			return n;
653afc154e9SHannes Frederic Sowa 	}
654554cfb7eSYOSHIFUJI Hideaki 	return m;
655554cfb7eSYOSHIFUJI Hideaki }
656554cfb7eSYOSHIFUJI Hideaki 
65728679ed1SDavid Ahern static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
65828679ed1SDavid Ahern 		       int oif, int strict, int *mpri, bool *do_rr)
659554cfb7eSYOSHIFUJI Hideaki {
660afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
66128679ed1SDavid Ahern 	bool rc = false;
66228679ed1SDavid Ahern 	int m;
66335103d11SAndy Gospodarek 
66428679ed1SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD)
6658067bb8cSIdo Schimmel 		goto out;
6668067bb8cSIdo Schimmel 
66728679ed1SDavid Ahern 	if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
66828679ed1SDavid Ahern 	    nh->fib_nh_flags & RTNH_F_LINKDOWN &&
669d5d32e4bSDavid Ahern 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
67035103d11SAndy Gospodarek 		goto out;
671554cfb7eSYOSHIFUJI Hideaki 
67228679ed1SDavid Ahern 	m = rt6_score_route(nh, fib6_flags, oif, strict);
6737e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
674afc154e9SHannes Frederic Sowa 		match_do_rr = true;
675afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
6767e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
677f11e6659SDavid S. Miller 		goto out;
6781da177e4SLinus Torvalds 	}
679f11e6659SDavid S. Miller 
680afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
68128679ed1SDavid Ahern 		rt6_probe(nh);
682afc154e9SHannes Frederic Sowa 
6837e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
684afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
685afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
686afc154e9SHannes Frederic Sowa 		*mpri = m;
68728679ed1SDavid Ahern 		rc = true;
688afc154e9SHannes Frederic Sowa 	}
689f11e6659SDavid S. Miller out:
69028679ed1SDavid Ahern 	return rc;
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
693b7bc4b6aSDavid Ahern static void __find_rr_leaf(struct fib6_info *f6i_start,
69430c15f03SDavid Ahern 			   struct fib6_info *nomatch, u32 metric,
695b7bc4b6aSDavid Ahern 			   struct fib6_result *res, struct fib6_info **cont,
69630c15f03SDavid Ahern 			   int oif, int strict, bool *do_rr, int *mpri)
69730c15f03SDavid Ahern {
698b7bc4b6aSDavid Ahern 	struct fib6_info *f6i;
69930c15f03SDavid Ahern 
700b7bc4b6aSDavid Ahern 	for (f6i = f6i_start;
701b7bc4b6aSDavid Ahern 	     f6i && f6i != nomatch;
702b7bc4b6aSDavid Ahern 	     f6i = rcu_dereference(f6i->fib6_next)) {
70330c15f03SDavid Ahern 		struct fib6_nh *nh;
70430c15f03SDavid Ahern 
705b7bc4b6aSDavid Ahern 		if (cont && f6i->fib6_metric != metric) {
706b7bc4b6aSDavid Ahern 			*cont = f6i;
70730c15f03SDavid Ahern 			return;
70830c15f03SDavid Ahern 		}
70930c15f03SDavid Ahern 
710b7bc4b6aSDavid Ahern 		if (fib6_check_expired(f6i))
71130c15f03SDavid Ahern 			continue;
71230c15f03SDavid Ahern 
713b7bc4b6aSDavid Ahern 		nh = &f6i->fib6_nh;
714b7bc4b6aSDavid Ahern 		if (find_match(nh, f6i->fib6_flags, oif, strict, mpri, do_rr)) {
715b7bc4b6aSDavid Ahern 			res->f6i = f6i;
716b7bc4b6aSDavid Ahern 			res->nh = nh;
7177d21fec9SDavid Ahern 			res->fib6_flags = f6i->fib6_flags;
7187d21fec9SDavid Ahern 			res->fib6_type = f6i->fib6_type;
719b7bc4b6aSDavid Ahern 		}
72030c15f03SDavid Ahern 	}
72130c15f03SDavid Ahern }
72230c15f03SDavid Ahern 
723b7bc4b6aSDavid Ahern static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
724b7bc4b6aSDavid Ahern 			 struct fib6_info *rr_head, int oif, int strict,
725b7bc4b6aSDavid Ahern 			 bool *do_rr, struct fib6_result *res)
726f11e6659SDavid S. Miller {
727b7bc4b6aSDavid Ahern 	u32 metric = rr_head->fib6_metric;
728b7bc4b6aSDavid Ahern 	struct fib6_info *cont = NULL;
729f11e6659SDavid S. Miller 	int mpri = -1;
730f11e6659SDavid S. Miller 
731b7bc4b6aSDavid Ahern 	__find_rr_leaf(rr_head, NULL, metric, res, &cont,
73230c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
7339fbdcfafSSteffen Klassert 
734b7bc4b6aSDavid Ahern 	__find_rr_leaf(leaf, rr_head, metric, res, &cont,
73530c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
7369fbdcfafSSteffen Klassert 
737b7bc4b6aSDavid Ahern 	if (res->f6i || !cont)
738b7bc4b6aSDavid Ahern 		return;
7399fbdcfafSSteffen Klassert 
740b7bc4b6aSDavid Ahern 	__find_rr_leaf(cont, NULL, metric, res, NULL,
74130c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
742f11e6659SDavid S. Miller }
743f11e6659SDavid S. Miller 
744b7bc4b6aSDavid Ahern static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
745b7bc4b6aSDavid Ahern 		       struct fib6_result *res, int strict)
746f11e6659SDavid S. Miller {
7478d1c802bSDavid Ahern 	struct fib6_info *leaf = rcu_dereference(fn->leaf);
748b7bc4b6aSDavid Ahern 	struct fib6_info *rt0;
749afc154e9SHannes Frederic Sowa 	bool do_rr = false;
75017ecf590SWei Wang 	int key_plen;
751f11e6659SDavid S. Miller 
752b7bc4b6aSDavid Ahern 	/* make sure this function or its helpers sets f6i */
753b7bc4b6aSDavid Ahern 	res->f6i = NULL;
754b7bc4b6aSDavid Ahern 
755421842edSDavid Ahern 	if (!leaf || leaf == net->ipv6.fib6_null_entry)
756b7bc4b6aSDavid Ahern 		goto out;
7578d1040e8SWei Wang 
75866f5d6ceSWei Wang 	rt0 = rcu_dereference(fn->rr_ptr);
759f11e6659SDavid S. Miller 	if (!rt0)
76066f5d6ceSWei Wang 		rt0 = leaf;
761f11e6659SDavid S. Miller 
76217ecf590SWei Wang 	/* Double check to make sure fn is not an intermediate node
76317ecf590SWei Wang 	 * and fn->leaf does not points to its child's leaf
76417ecf590SWei Wang 	 * (This might happen if all routes under fn are deleted from
76517ecf590SWei Wang 	 * the tree and fib6_repair_tree() is called on the node.)
76617ecf590SWei Wang 	 */
76793c2fb25SDavid Ahern 	key_plen = rt0->fib6_dst.plen;
76817ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES
76993c2fb25SDavid Ahern 	if (rt0->fib6_src.plen)
77093c2fb25SDavid Ahern 		key_plen = rt0->fib6_src.plen;
77117ecf590SWei Wang #endif
77217ecf590SWei Wang 	if (fn->fn_bit != key_plen)
773b7bc4b6aSDavid Ahern 		goto out;
77417ecf590SWei Wang 
775b7bc4b6aSDavid Ahern 	find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
776afc154e9SHannes Frederic Sowa 	if (do_rr) {
7778fb11a9aSDavid Ahern 		struct fib6_info *next = rcu_dereference(rt0->fib6_next);
778f11e6659SDavid S. Miller 
779554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
78093c2fb25SDavid Ahern 		if (!next || next->fib6_metric != rt0->fib6_metric)
7818d1040e8SWei Wang 			next = leaf;
782f11e6659SDavid S. Miller 
78366f5d6ceSWei Wang 		if (next != rt0) {
78493c2fb25SDavid Ahern 			spin_lock_bh(&leaf->fib6_table->tb6_lock);
78566f5d6ceSWei Wang 			/* make sure next is not being deleted from the tree */
78693c2fb25SDavid Ahern 			if (next->fib6_node)
78766f5d6ceSWei Wang 				rcu_assign_pointer(fn->rr_ptr, next);
78893c2fb25SDavid Ahern 			spin_unlock_bh(&leaf->fib6_table->tb6_lock);
78966f5d6ceSWei Wang 		}
790554cfb7eSYOSHIFUJI Hideaki 	}
791554cfb7eSYOSHIFUJI Hideaki 
792b7bc4b6aSDavid Ahern out:
793b7bc4b6aSDavid Ahern 	if (!res->f6i) {
794b7bc4b6aSDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
795b7bc4b6aSDavid Ahern 		res->nh = &res->f6i->fib6_nh;
7967d21fec9SDavid Ahern 		res->fib6_flags = res->f6i->fib6_flags;
7977d21fec9SDavid Ahern 		res->fib6_type = res->f6i->fib6_type;
798b7bc4b6aSDavid Ahern 	}
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
80185bd05deSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
8028b9df265SMartin KaFai Lau {
80385bd05deSDavid Ahern 	return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
80485bd05deSDavid Ahern 	       res->nh->fib_nh_gw_family;
8058b9df265SMartin KaFai Lau }
8068b9df265SMartin KaFai Lau 
80770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
80870ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
809b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
81070ceb4f5SYOSHIFUJI Hideaki {
811c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
81270ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
81370ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
81470ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
8154bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
8168d1c802bSDavid Ahern 	struct fib6_info *rt;
81770ceb4f5SYOSHIFUJI Hideaki 
81870ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
81970ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
82070ceb4f5SYOSHIFUJI Hideaki 	}
82170ceb4f5SYOSHIFUJI Hideaki 
82270ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
82370ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
82470ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
82570ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
82670ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
82770ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
82870ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
82970ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
83070ceb4f5SYOSHIFUJI Hideaki 		}
83170ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
83270ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
83370ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
83470ceb4f5SYOSHIFUJI Hideaki 		}
83570ceb4f5SYOSHIFUJI Hideaki 	}
83670ceb4f5SYOSHIFUJI Hideaki 
83770ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
83870ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
8393933fc95SJens Rosenboom 		return -EINVAL;
84070ceb4f5SYOSHIFUJI Hideaki 
8414bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
84270ceb4f5SYOSHIFUJI Hideaki 
84370ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
84470ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
84570ceb4f5SYOSHIFUJI Hideaki 	else {
84670ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
84770ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
84870ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
84970ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
85070ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
85170ceb4f5SYOSHIFUJI Hideaki 	}
85270ceb4f5SYOSHIFUJI Hideaki 
853f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
854afb1d4b5SDavid Ahern 		rt = rt6_get_dflt_router(net, gwaddr, dev);
855f104a567SDuan Jiong 	else
856f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
857830218c1SDavid Ahern 					gwaddr, dev);
85870ceb4f5SYOSHIFUJI Hideaki 
85970ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
860afb1d4b5SDavid Ahern 		ip6_del_rt(net, rt);
86170ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
86270ceb4f5SYOSHIFUJI Hideaki 	}
86370ceb4f5SYOSHIFUJI Hideaki 
86470ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
865830218c1SDavid Ahern 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
866830218c1SDavid Ahern 					dev, pref);
86770ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
86893c2fb25SDavid Ahern 		rt->fib6_flags = RTF_ROUTEINFO |
86993c2fb25SDavid Ahern 				 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
87070ceb4f5SYOSHIFUJI Hideaki 
87170ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
8721716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
87314895687SDavid Ahern 			fib6_clean_expires(rt);
8741716a961SGao feng 		else
87514895687SDavid Ahern 			fib6_set_expires(rt, jiffies + HZ * lifetime);
8761716a961SGao feng 
87793531c67SDavid Ahern 		fib6_info_release(rt);
87870ceb4f5SYOSHIFUJI Hideaki 	}
87970ceb4f5SYOSHIFUJI Hideaki 	return 0;
88070ceb4f5SYOSHIFUJI Hideaki }
88170ceb4f5SYOSHIFUJI Hideaki #endif
88270ceb4f5SYOSHIFUJI Hideaki 
883ae90d867SDavid Ahern /*
884ae90d867SDavid Ahern  *	Misc support functions
885ae90d867SDavid Ahern  */
886ae90d867SDavid Ahern 
887ae90d867SDavid Ahern /* called with rcu_lock held */
8880d161581SDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
889ae90d867SDavid Ahern {
8900d161581SDavid Ahern 	struct net_device *dev = res->nh->fib_nh_dev;
891ae90d867SDavid Ahern 
8927d21fec9SDavid Ahern 	if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
893ae90d867SDavid Ahern 		/* for copies of local routes, dst->dev needs to be the
894ae90d867SDavid Ahern 		 * device if it is a master device, the master device if
895ae90d867SDavid Ahern 		 * device is enslaved, and the loopback as the default
896ae90d867SDavid Ahern 		 */
897ae90d867SDavid Ahern 		if (netif_is_l3_slave(dev) &&
8987d21fec9SDavid Ahern 		    !rt6_need_strict(&res->f6i->fib6_dst.addr))
899ae90d867SDavid Ahern 			dev = l3mdev_master_dev_rcu(dev);
900ae90d867SDavid Ahern 		else if (!netif_is_l3_master(dev))
901ae90d867SDavid Ahern 			dev = dev_net(dev)->loopback_dev;
902ae90d867SDavid Ahern 		/* last case is netif_is_l3_master(dev) is true in which
903ae90d867SDavid Ahern 		 * case we want dev returned to be dev
904ae90d867SDavid Ahern 		 */
905ae90d867SDavid Ahern 	}
906ae90d867SDavid Ahern 
907ae90d867SDavid Ahern 	return dev;
908ae90d867SDavid Ahern }
909ae90d867SDavid Ahern 
9106edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = {
9116edb3c96SDavid Ahern 	[RTN_UNSPEC]	= 0,
9126edb3c96SDavid Ahern 	[RTN_UNICAST]	= 0,
9136edb3c96SDavid Ahern 	[RTN_LOCAL]	= 0,
9146edb3c96SDavid Ahern 	[RTN_BROADCAST]	= 0,
9156edb3c96SDavid Ahern 	[RTN_ANYCAST]	= 0,
9166edb3c96SDavid Ahern 	[RTN_MULTICAST]	= 0,
9176edb3c96SDavid Ahern 	[RTN_BLACKHOLE]	= -EINVAL,
9186edb3c96SDavid Ahern 	[RTN_UNREACHABLE] = -EHOSTUNREACH,
9196edb3c96SDavid Ahern 	[RTN_PROHIBIT]	= -EACCES,
9206edb3c96SDavid Ahern 	[RTN_THROW]	= -EAGAIN,
9216edb3c96SDavid Ahern 	[RTN_NAT]	= -EINVAL,
9226edb3c96SDavid Ahern 	[RTN_XRESOLVE]	= -EINVAL,
9236edb3c96SDavid Ahern };
9246edb3c96SDavid Ahern 
9256edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type)
9266edb3c96SDavid Ahern {
9276edb3c96SDavid Ahern 	return fib6_prop[fib6_type];
9286edb3c96SDavid Ahern }
9296edb3c96SDavid Ahern 
9308d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
9313b6761d1SDavid Ahern {
9323b6761d1SDavid Ahern 	unsigned short flags = 0;
9333b6761d1SDavid Ahern 
9343b6761d1SDavid Ahern 	if (rt->dst_nocount)
9353b6761d1SDavid Ahern 		flags |= DST_NOCOUNT;
9363b6761d1SDavid Ahern 	if (rt->dst_nopolicy)
9373b6761d1SDavid Ahern 		flags |= DST_NOPOLICY;
9383b6761d1SDavid Ahern 	if (rt->dst_host)
9393b6761d1SDavid Ahern 		flags |= DST_HOST;
9403b6761d1SDavid Ahern 
9413b6761d1SDavid Ahern 	return flags;
9423b6761d1SDavid Ahern }
9433b6761d1SDavid Ahern 
9447d21fec9SDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
9456edb3c96SDavid Ahern {
9467d21fec9SDavid Ahern 	rt->dst.error = ip6_rt_type_to_error(fib6_type);
9476edb3c96SDavid Ahern 
9487d21fec9SDavid Ahern 	switch (fib6_type) {
9496edb3c96SDavid Ahern 	case RTN_BLACKHOLE:
9506edb3c96SDavid Ahern 		rt->dst.output = dst_discard_out;
9516edb3c96SDavid Ahern 		rt->dst.input = dst_discard;
9526edb3c96SDavid Ahern 		break;
9536edb3c96SDavid Ahern 	case RTN_PROHIBIT:
9546edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_prohibit_out;
9556edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_prohibit;
9566edb3c96SDavid Ahern 		break;
9576edb3c96SDavid Ahern 	case RTN_THROW:
9586edb3c96SDavid Ahern 	case RTN_UNREACHABLE:
9596edb3c96SDavid Ahern 	default:
9606edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_discard_out;
9616edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_discard;
9626edb3c96SDavid Ahern 		break;
9636edb3c96SDavid Ahern 	}
9646edb3c96SDavid Ahern }
9656edb3c96SDavid Ahern 
9660d161581SDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
9676edb3c96SDavid Ahern {
9687d21fec9SDavid Ahern 	struct fib6_info *f6i = res->f6i;
9690d161581SDavid Ahern 
9707d21fec9SDavid Ahern 	if (res->fib6_flags & RTF_REJECT) {
9717d21fec9SDavid Ahern 		ip6_rt_init_dst_reject(rt, res->fib6_type);
9726edb3c96SDavid Ahern 		return;
9736edb3c96SDavid Ahern 	}
9746edb3c96SDavid Ahern 
9756edb3c96SDavid Ahern 	rt->dst.error = 0;
9766edb3c96SDavid Ahern 	rt->dst.output = ip6_output;
9776edb3c96SDavid Ahern 
9787d21fec9SDavid Ahern 	if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
9796edb3c96SDavid Ahern 		rt->dst.input = ip6_input;
9807d21fec9SDavid Ahern 	} else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
9816edb3c96SDavid Ahern 		rt->dst.input = ip6_mc_input;
9826edb3c96SDavid Ahern 	} else {
9836edb3c96SDavid Ahern 		rt->dst.input = ip6_forward;
9846edb3c96SDavid Ahern 	}
9856edb3c96SDavid Ahern 
9860d161581SDavid Ahern 	if (res->nh->fib_nh_lws) {
9870d161581SDavid Ahern 		rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
9886edb3c96SDavid Ahern 		lwtunnel_set_redirect(&rt->dst);
9896edb3c96SDavid Ahern 	}
9906edb3c96SDavid Ahern 
9916edb3c96SDavid Ahern 	rt->dst.lastuse = jiffies;
9926edb3c96SDavid Ahern }
9936edb3c96SDavid Ahern 
994e873e4b9SWei Wang /* Caller must already hold reference to @from */
9958d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
996ae90d867SDavid Ahern {
997ae90d867SDavid Ahern 	rt->rt6i_flags &= ~RTF_EXPIRES;
998a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, from);
999e1255ed4SDavid Ahern 	ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
1000ae90d867SDavid Ahern }
1001ae90d867SDavid Ahern 
10020d161581SDavid Ahern /* Caller must already hold reference to f6i in result */
10030d161581SDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
1004ae90d867SDavid Ahern {
10050d161581SDavid Ahern 	const struct fib6_nh *nh = res->nh;
10060d161581SDavid Ahern 	const struct net_device *dev = nh->fib_nh_dev;
10070d161581SDavid Ahern 	struct fib6_info *f6i = res->f6i;
1008dcd1f572SDavid Ahern 
10090d161581SDavid Ahern 	ip6_rt_init_dst(rt, res);
10106edb3c96SDavid Ahern 
10110d161581SDavid Ahern 	rt->rt6i_dst = f6i->fib6_dst;
1012dcd1f572SDavid Ahern 	rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
10137d21fec9SDavid Ahern 	rt->rt6i_flags = res->fib6_flags;
10140d161581SDavid Ahern 	if (nh->fib_nh_gw_family) {
10150d161581SDavid Ahern 		rt->rt6i_gateway = nh->fib_nh_gw6;
10162b2450caSDavid Ahern 		rt->rt6i_flags |= RTF_GATEWAY;
10172b2450caSDavid Ahern 	}
10180d161581SDavid Ahern 	rt6_set_from(rt, f6i);
1019ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
10200d161581SDavid Ahern 	rt->rt6i_src = f6i->fib6_src;
1021ae90d867SDavid Ahern #endif
1022ae90d867SDavid Ahern }
1023ae90d867SDavid Ahern 
1024a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1025a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
1026a3c00e46SMartin KaFai Lau {
102766f5d6ceSWei Wang 	struct fib6_node *pn, *sn;
1028a3c00e46SMartin KaFai Lau 	while (1) {
1029a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
1030a3c00e46SMartin KaFai Lau 			return NULL;
103166f5d6ceSWei Wang 		pn = rcu_dereference(fn->parent);
103266f5d6ceSWei Wang 		sn = FIB6_SUBTREE(pn);
103366f5d6ceSWei Wang 		if (sn && sn != fn)
10346454743bSDavid Ahern 			fn = fib6_node_lookup(sn, NULL, saddr);
1035a3c00e46SMartin KaFai Lau 		else
1036a3c00e46SMartin KaFai Lau 			fn = pn;
1037a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
1038a3c00e46SMartin KaFai Lau 			return fn;
1039a3c00e46SMartin KaFai Lau 	}
1040a3c00e46SMartin KaFai Lau }
1041c71099acSThomas Graf 
104210585b43SDavid Ahern static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1043d3843fe5SWei Wang {
1044d3843fe5SWei Wang 	struct rt6_info *rt = *prt;
1045d3843fe5SWei Wang 
1046d3843fe5SWei Wang 	if (dst_hold_safe(&rt->dst))
1047d3843fe5SWei Wang 		return true;
104810585b43SDavid Ahern 	if (net) {
1049d3843fe5SWei Wang 		rt = net->ipv6.ip6_null_entry;
1050d3843fe5SWei Wang 		dst_hold(&rt->dst);
1051d3843fe5SWei Wang 	} else {
1052d3843fe5SWei Wang 		rt = NULL;
1053d3843fe5SWei Wang 	}
1054d3843fe5SWei Wang 	*prt = rt;
1055d3843fe5SWei Wang 	return false;
1056d3843fe5SWei Wang }
1057d3843fe5SWei Wang 
1058dec9b0e2SDavid Ahern /* called with rcu_lock held */
10599b6b35abSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
1060dec9b0e2SDavid Ahern {
10619b6b35abSDavid Ahern 	struct net_device *dev = res->nh->fib_nh_dev;
10629b6b35abSDavid Ahern 	struct fib6_info *f6i = res->f6i;
10639b6b35abSDavid Ahern 	unsigned short flags;
1064dec9b0e2SDavid Ahern 	struct rt6_info *nrt;
1065dec9b0e2SDavid Ahern 
10669b6b35abSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
10671c87e79aSXin Long 		goto fallback;
1068e873e4b9SWei Wang 
10699b6b35abSDavid Ahern 	flags = fib6_info_dst_flags(f6i);
107093531c67SDavid Ahern 	nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
10711c87e79aSXin Long 	if (!nrt) {
10729b6b35abSDavid Ahern 		fib6_info_release(f6i);
10731c87e79aSXin Long 		goto fallback;
10741c87e79aSXin Long 	}
1075dec9b0e2SDavid Ahern 
10760d161581SDavid Ahern 	ip6_rt_copy_init(nrt, res);
10771c87e79aSXin Long 	return nrt;
10781c87e79aSXin Long 
10791c87e79aSXin Long fallback:
10801c87e79aSXin Long 	nrt = dev_net(dev)->ipv6.ip6_null_entry;
10811c87e79aSXin Long 	dst_hold(&nrt->dst);
1082dec9b0e2SDavid Ahern 	return nrt;
1083dec9b0e2SDavid Ahern }
1084dec9b0e2SDavid Ahern 
10858ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
10868ed67789SDaniel Lezcano 					     struct fib6_table *table,
1087b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
1088b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
1089b75cc8f9SDavid Ahern 					     int flags)
10901da177e4SLinus Torvalds {
1091b1d40991SDavid Ahern 	struct fib6_result res = {};
10921da177e4SLinus Torvalds 	struct fib6_node *fn;
109323fb93a4SDavid Ahern 	struct rt6_info *rt;
10941da177e4SLinus Torvalds 
1095b6cdbc85SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1096b6cdbc85SDavid Ahern 		flags &= ~RT6_LOOKUP_F_IFACE;
1097b6cdbc85SDavid Ahern 
109866f5d6ceSWei Wang 	rcu_read_lock();
10996454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1100c71099acSThomas Graf restart:
1101b1d40991SDavid Ahern 	res.f6i = rcu_dereference(fn->leaf);
1102b1d40991SDavid Ahern 	if (!res.f6i)
1103b1d40991SDavid Ahern 		res.f6i = net->ipv6.fib6_null_entry;
1104af52a52cSDavid Ahern 	else
110575ef7389SDavid Ahern 		rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
110675ef7389SDavid Ahern 				 flags);
1107af52a52cSDavid Ahern 
1108b1d40991SDavid Ahern 	if (res.f6i == net->ipv6.fib6_null_entry) {
1109a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1110a3c00e46SMartin KaFai Lau 		if (fn)
1111a3c00e46SMartin KaFai Lau 			goto restart;
1112af52a52cSDavid Ahern 
1113af52a52cSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
1114af52a52cSDavid Ahern 		dst_hold(&rt->dst);
1115af52a52cSDavid Ahern 		goto out;
1116a3c00e46SMartin KaFai Lau 	}
11172b760fcfSWei Wang 
1118b1d40991SDavid Ahern 	fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1119b1d40991SDavid Ahern 			 fl6->flowi6_oif != 0, skb, flags);
1120b1d40991SDavid Ahern 
11214c9483b2SDavid S. Miller 	/* Search through exception table */
11227e4b5128SDavid Ahern 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
112323fb93a4SDavid Ahern 	if (rt) {
112410585b43SDavid Ahern 		if (ip6_hold_safe(net, &rt))
1125d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
112623fb93a4SDavid Ahern 	} else {
11279b6b35abSDavid Ahern 		rt = ip6_create_rt_rcu(&res);
1128dec9b0e2SDavid Ahern 	}
1129d3843fe5SWei Wang 
1130af52a52cSDavid Ahern out:
11318ff2e5b2SDavid Ahern 	trace_fib6_table_lookup(net, &res, table, fl6);
1132af52a52cSDavid Ahern 
113366f5d6ceSWei Wang 	rcu_read_unlock();
1134b811580dSDavid Ahern 
11351da177e4SLinus Torvalds 	return rt;
1136c71099acSThomas Graf }
1137c71099acSThomas Graf 
1138ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1139b75cc8f9SDavid Ahern 				   const struct sk_buff *skb, int flags)
1140ea6e574eSFlorian Westphal {
1141b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1142ea6e574eSFlorian Westphal }
1143ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
1144ea6e574eSFlorian Westphal 
11459acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1146b75cc8f9SDavid Ahern 			    const struct in6_addr *saddr, int oif,
1147b75cc8f9SDavid Ahern 			    const struct sk_buff *skb, int strict)
1148c71099acSThomas Graf {
11494c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
11504c9483b2SDavid S. Miller 		.flowi6_oif = oif,
11514c9483b2SDavid S. Miller 		.daddr = *daddr,
1152c71099acSThomas Graf 	};
1153c71099acSThomas Graf 	struct dst_entry *dst;
115477d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1155c71099acSThomas Graf 
1156adaa70bbSThomas Graf 	if (saddr) {
11574c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1158adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1159adaa70bbSThomas Graf 	}
1160adaa70bbSThomas Graf 
1161b75cc8f9SDavid Ahern 	dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1162c71099acSThomas Graf 	if (dst->error == 0)
1163c71099acSThomas Graf 		return (struct rt6_info *) dst;
1164c71099acSThomas Graf 
1165c71099acSThomas Graf 	dst_release(dst);
1166c71099acSThomas Graf 
11671da177e4SLinus Torvalds 	return NULL;
11681da177e4SLinus Torvalds }
11697159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
11707159039aSYOSHIFUJI Hideaki 
1171c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
11721cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
11731cfb71eeSWei Wang  * route is released.
11741cfb71eeSWei Wang  * Caller must hold dst before calling it.
11751da177e4SLinus Torvalds  */
11761da177e4SLinus Torvalds 
11778d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1178333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
11791da177e4SLinus Torvalds {
11801da177e4SLinus Torvalds 	int err;
1181c71099acSThomas Graf 	struct fib6_table *table;
11821da177e4SLinus Torvalds 
118393c2fb25SDavid Ahern 	table = rt->fib6_table;
118466f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
1185d4ead6b3SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, extack);
118666f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	return err;
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds 
11918d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt)
119240e22e8fSThomas Graf {
1193afb1d4b5SDavid Ahern 	struct nl_info info = {	.nl_net = net, };
1194e715b6d3SFlorian Westphal 
1195d4ead6b3SDavid Ahern 	return __ip6_ins_rt(rt, &info, NULL);
119640e22e8fSThomas Graf }
119740e22e8fSThomas Graf 
119885bd05deSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
119921efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
1200b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
12011da177e4SLinus Torvalds {
120285bd05deSDavid Ahern 	struct fib6_info *f6i = res->f6i;
12034832c30dSDavid Ahern 	struct net_device *dev;
12041da177e4SLinus Torvalds 	struct rt6_info *rt;
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	/*
12071da177e4SLinus Torvalds 	 *	Clone the route.
12081da177e4SLinus Torvalds 	 */
12091da177e4SLinus Torvalds 
121085bd05deSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
1211e873e4b9SWei Wang 		return NULL;
1212e873e4b9SWei Wang 
12130d161581SDavid Ahern 	dev = ip6_rt_get_dev_rcu(res);
121493531c67SDavid Ahern 	rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1215e873e4b9SWei Wang 	if (!rt) {
121685bd05deSDavid Ahern 		fib6_info_release(f6i);
121783a09abdSMartin KaFai Lau 		return NULL;
1218e873e4b9SWei Wang 	}
121983a09abdSMartin KaFai Lau 
12200d161581SDavid Ahern 	ip6_rt_copy_init(rt, res);
12218b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
122283a09abdSMartin KaFai Lau 	rt->dst.flags |= DST_HOST;
122383a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
122483a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
12258b9df265SMartin KaFai Lau 
122685bd05deSDavid Ahern 	if (!rt6_is_gw_or_nonexthop(res)) {
122785bd05deSDavid Ahern 		if (f6i->fib6_dst.plen != 128 &&
122885bd05deSDavid Ahern 		    ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
122958c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
12301da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
12311da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
12324e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
12331da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
12341da177e4SLinus Torvalds 		}
12351da177e4SLinus Torvalds #endif
123695a9a5baSYOSHIFUJI Hideaki 	}
123795a9a5baSYOSHIFUJI Hideaki 
1238299d9939SYOSHIFUJI Hideaki 	return rt;
1239299d9939SYOSHIFUJI Hideaki }
1240299d9939SYOSHIFUJI Hideaki 
1241db3fedeeSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
1242d52d3997SMartin KaFai Lau {
1243db3fedeeSDavid Ahern 	struct fib6_info *f6i = res->f6i;
1244db3fedeeSDavid Ahern 	unsigned short flags = fib6_info_dst_flags(f6i);
12454832c30dSDavid Ahern 	struct net_device *dev;
1246d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1247d52d3997SMartin KaFai Lau 
1248db3fedeeSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
1249e873e4b9SWei Wang 		return NULL;
1250e873e4b9SWei Wang 
12514832c30dSDavid Ahern 	rcu_read_lock();
12520d161581SDavid Ahern 	dev = ip6_rt_get_dev_rcu(res);
125393531c67SDavid Ahern 	pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
12544832c30dSDavid Ahern 	rcu_read_unlock();
1255e873e4b9SWei Wang 	if (!pcpu_rt) {
1256db3fedeeSDavid Ahern 		fib6_info_release(f6i);
1257d52d3997SMartin KaFai Lau 		return NULL;
1258e873e4b9SWei Wang 	}
12590d161581SDavid Ahern 	ip6_rt_copy_init(pcpu_rt, res);
1260d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
1261d52d3997SMartin KaFai Lau 	return pcpu_rt;
1262d52d3997SMartin KaFai Lau }
1263d52d3997SMartin KaFai Lau 
126466f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */
1265db3fedeeSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
1266d52d3997SMartin KaFai Lau {
1267a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, **p;
1268d52d3997SMartin KaFai Lau 
1269db3fedeeSDavid Ahern 	p = this_cpu_ptr(res->f6i->rt6i_pcpu);
1270d52d3997SMartin KaFai Lau 	pcpu_rt = *p;
1271d52d3997SMartin KaFai Lau 
1272d4ead6b3SDavid Ahern 	if (pcpu_rt)
127310585b43SDavid Ahern 		ip6_hold_safe(NULL, &pcpu_rt);
1274d3843fe5SWei Wang 
1275a73e4195SMartin KaFai Lau 	return pcpu_rt;
1276a73e4195SMartin KaFai Lau }
1277a73e4195SMartin KaFai Lau 
1278afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1279db3fedeeSDavid Ahern 					    const struct fib6_result *res)
1280a73e4195SMartin KaFai Lau {
1281a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1282d52d3997SMartin KaFai Lau 
1283db3fedeeSDavid Ahern 	pcpu_rt = ip6_rt_pcpu_alloc(res);
1284d52d3997SMartin KaFai Lau 	if (!pcpu_rt) {
12859c7370a1SMartin KaFai Lau 		dst_hold(&net->ipv6.ip6_null_entry->dst);
12869c7370a1SMartin KaFai Lau 		return net->ipv6.ip6_null_entry;
1287d52d3997SMartin KaFai Lau 	}
1288d52d3997SMartin KaFai Lau 
1289a94b9367SWei Wang 	dst_hold(&pcpu_rt->dst);
1290db3fedeeSDavid Ahern 	p = this_cpu_ptr(res->f6i->rt6i_pcpu);
1291d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1292951f788aSEric Dumazet 	BUG_ON(prev);
1293a94b9367SWei Wang 
129461fb0d01SEric Dumazet 	if (res->f6i->fib6_destroying) {
129561fb0d01SEric Dumazet 		struct fib6_info *from;
129661fb0d01SEric Dumazet 
129761fb0d01SEric Dumazet 		from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
129861fb0d01SEric Dumazet 		fib6_info_release(from);
129961fb0d01SEric Dumazet 	}
130061fb0d01SEric Dumazet 
1301d52d3997SMartin KaFai Lau 	return pcpu_rt;
1302d52d3997SMartin KaFai Lau }
1303d52d3997SMartin KaFai Lau 
130435732d01SWei Wang /* exception hash table implementation
130535732d01SWei Wang  */
130635732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
130735732d01SWei Wang 
130835732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
130935732d01SWei Wang  * Caller must hold rt6_exception_lock
131035732d01SWei Wang  */
131135732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
131235732d01SWei Wang 				 struct rt6_exception *rt6_ex)
131335732d01SWei Wang {
1314f5b51fe8SPaolo Abeni 	struct fib6_info *from;
1315b2427e67SColin Ian King 	struct net *net;
131681eb8447SWei Wang 
131735732d01SWei Wang 	if (!bucket || !rt6_ex)
131835732d01SWei Wang 		return;
1319b2427e67SColin Ian King 
1320b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
1321f5b51fe8SPaolo Abeni 	net->ipv6.rt6_stats->fib_rt_cache--;
1322f5b51fe8SPaolo Abeni 
1323f5b51fe8SPaolo Abeni 	/* purge completely the exception to allow releasing the held resources:
1324f5b51fe8SPaolo Abeni 	 * some [sk] cache may keep the dst around for unlimited time
1325f5b51fe8SPaolo Abeni 	 */
13260e233874SEric Dumazet 	from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1327f5b51fe8SPaolo Abeni 	fib6_info_release(from);
1328f5b51fe8SPaolo Abeni 	dst_dev_put(&rt6_ex->rt6i->dst);
1329f5b51fe8SPaolo Abeni 
133035732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
133177634cc6SDavid Ahern 	dst_release(&rt6_ex->rt6i->dst);
133235732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
133335732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
133435732d01SWei Wang 	bucket->depth--;
133535732d01SWei Wang }
133635732d01SWei Wang 
133735732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
133835732d01SWei Wang  * Caller must hold rt6_exception_lock
133935732d01SWei Wang  */
134035732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
134135732d01SWei Wang {
134235732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
134335732d01SWei Wang 
134435732d01SWei Wang 	if (!bucket)
134535732d01SWei Wang 		return;
134635732d01SWei Wang 
134735732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
134835732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
134935732d01SWei Wang 			oldest = rt6_ex;
135035732d01SWei Wang 	}
135135732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
135235732d01SWei Wang }
135335732d01SWei Wang 
135435732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
135535732d01SWei Wang 			      const struct in6_addr *src)
135635732d01SWei Wang {
135735732d01SWei Wang 	static u32 seed __read_mostly;
135835732d01SWei Wang 	u32 val;
135935732d01SWei Wang 
136035732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
136135732d01SWei Wang 	val = jhash(dst, sizeof(*dst), seed);
136235732d01SWei Wang 
136335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
136435732d01SWei Wang 	if (src)
136535732d01SWei Wang 		val = jhash(src, sizeof(*src), val);
136635732d01SWei Wang #endif
136735732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
136835732d01SWei Wang }
136935732d01SWei Wang 
137035732d01SWei Wang /* Helper function to find the cached rt in the hash table
137135732d01SWei Wang  * and update bucket pointer to point to the bucket for this
137235732d01SWei Wang  * (daddr, saddr) pair
137335732d01SWei Wang  * Caller must hold rt6_exception_lock
137435732d01SWei Wang  */
137535732d01SWei Wang static struct rt6_exception *
137635732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
137735732d01SWei Wang 			      const struct in6_addr *daddr,
137835732d01SWei Wang 			      const struct in6_addr *saddr)
137935732d01SWei Wang {
138035732d01SWei Wang 	struct rt6_exception *rt6_ex;
138135732d01SWei Wang 	u32 hval;
138235732d01SWei Wang 
138335732d01SWei Wang 	if (!(*bucket) || !daddr)
138435732d01SWei Wang 		return NULL;
138535732d01SWei Wang 
138635732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
138735732d01SWei Wang 	*bucket += hval;
138835732d01SWei Wang 
138935732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
139035732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
139135732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
139235732d01SWei Wang 
139335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
139435732d01SWei Wang 		if (matched && saddr)
139535732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
139635732d01SWei Wang #endif
139735732d01SWei Wang 		if (matched)
139835732d01SWei Wang 			return rt6_ex;
139935732d01SWei Wang 	}
140035732d01SWei Wang 	return NULL;
140135732d01SWei Wang }
140235732d01SWei Wang 
140335732d01SWei Wang /* Helper function to find the cached rt in the hash table
140435732d01SWei Wang  * and update bucket pointer to point to the bucket for this
140535732d01SWei Wang  * (daddr, saddr) pair
140635732d01SWei Wang  * Caller must hold rcu_read_lock()
140735732d01SWei Wang  */
140835732d01SWei Wang static struct rt6_exception *
140935732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
141035732d01SWei Wang 			 const struct in6_addr *daddr,
141135732d01SWei Wang 			 const struct in6_addr *saddr)
141235732d01SWei Wang {
141335732d01SWei Wang 	struct rt6_exception *rt6_ex;
141435732d01SWei Wang 	u32 hval;
141535732d01SWei Wang 
141635732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
141735732d01SWei Wang 
141835732d01SWei Wang 	if (!(*bucket) || !daddr)
141935732d01SWei Wang 		return NULL;
142035732d01SWei Wang 
142135732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
142235732d01SWei Wang 	*bucket += hval;
142335732d01SWei Wang 
142435732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
142535732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
142635732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
142735732d01SWei Wang 
142835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
142935732d01SWei Wang 		if (matched && saddr)
143035732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
143135732d01SWei Wang #endif
143235732d01SWei Wang 		if (matched)
143335732d01SWei Wang 			return rt6_ex;
143435732d01SWei Wang 	}
143535732d01SWei Wang 	return NULL;
143635732d01SWei Wang }
143735732d01SWei Wang 
1438b748f260SDavid Ahern static unsigned int fib6_mtu(const struct fib6_result *res)
143935732d01SWei Wang {
1440b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
1441d4ead6b3SDavid Ahern 	unsigned int mtu;
1442d4ead6b3SDavid Ahern 
1443b748f260SDavid Ahern 	if (res->f6i->fib6_pmtu) {
1444b748f260SDavid Ahern 		mtu = res->f6i->fib6_pmtu;
1445dcd1f572SDavid Ahern 	} else {
1446b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
1447dcd1f572SDavid Ahern 		struct inet6_dev *idev;
1448dcd1f572SDavid Ahern 
1449dcd1f572SDavid Ahern 		rcu_read_lock();
1450dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
1451dcd1f572SDavid Ahern 		mtu = idev->cnf.mtu6;
1452dcd1f572SDavid Ahern 		rcu_read_unlock();
1453dcd1f572SDavid Ahern 	}
1454dcd1f572SDavid Ahern 
1455d4ead6b3SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1456d4ead6b3SDavid Ahern 
1457b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1458d4ead6b3SDavid Ahern }
1459d4ead6b3SDavid Ahern 
146035732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
14615012f0a5SDavid Ahern 				const struct fib6_result *res)
146235732d01SWei Wang {
14635e670d84SDavid Ahern 	struct net *net = dev_net(nrt->dst.dev);
146435732d01SWei Wang 	struct rt6_exception_bucket *bucket;
146535732d01SWei Wang 	struct in6_addr *src_key = NULL;
146635732d01SWei Wang 	struct rt6_exception *rt6_ex;
14675012f0a5SDavid Ahern 	struct fib6_info *f6i = res->f6i;
146835732d01SWei Wang 	int err = 0;
146935732d01SWei Wang 
147035732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
147135732d01SWei Wang 
14725012f0a5SDavid Ahern 	if (f6i->exception_bucket_flushed) {
147335732d01SWei Wang 		err = -EINVAL;
147435732d01SWei Wang 		goto out;
147535732d01SWei Wang 	}
147635732d01SWei Wang 
14775012f0a5SDavid Ahern 	bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket,
147835732d01SWei Wang 					lockdep_is_held(&rt6_exception_lock));
147935732d01SWei Wang 	if (!bucket) {
148035732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
148135732d01SWei Wang 				 GFP_ATOMIC);
148235732d01SWei Wang 		if (!bucket) {
148335732d01SWei Wang 			err = -ENOMEM;
148435732d01SWei Wang 			goto out;
148535732d01SWei Wang 		}
14865012f0a5SDavid Ahern 		rcu_assign_pointer(f6i->rt6i_exception_bucket, bucket);
148735732d01SWei Wang 	}
148835732d01SWei Wang 
148935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
14905012f0a5SDavid Ahern 	/* fib6_src.plen != 0 indicates f6i is in subtree
149135732d01SWei Wang 	 * and exception table is indexed by a hash of
14925012f0a5SDavid Ahern 	 * both fib6_dst and fib6_src.
149335732d01SWei Wang 	 * Otherwise, the exception table is indexed by
14945012f0a5SDavid Ahern 	 * a hash of only fib6_dst.
149535732d01SWei Wang 	 */
14965012f0a5SDavid Ahern 	if (f6i->fib6_src.plen)
149735732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
149835732d01SWei Wang #endif
14995012f0a5SDavid Ahern 	/* rt6_mtu_change() might lower mtu on f6i.
1500f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
15015012f0a5SDavid Ahern 	 * is less than f6i's mtu value.
1502f5bbe7eeSWei Wang 	 */
1503b748f260SDavid Ahern 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1504f5bbe7eeSWei Wang 		err = -EINVAL;
1505f5bbe7eeSWei Wang 		goto out;
1506f5bbe7eeSWei Wang 	}
150760006a48SWei Wang 
150835732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
150935732d01SWei Wang 					       src_key);
151035732d01SWei Wang 	if (rt6_ex)
151135732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
151235732d01SWei Wang 
151335732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
151435732d01SWei Wang 	if (!rt6_ex) {
151535732d01SWei Wang 		err = -ENOMEM;
151635732d01SWei Wang 		goto out;
151735732d01SWei Wang 	}
151835732d01SWei Wang 	rt6_ex->rt6i = nrt;
151935732d01SWei Wang 	rt6_ex->stamp = jiffies;
152035732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
152135732d01SWei Wang 	bucket->depth++;
152281eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
152335732d01SWei Wang 
152435732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
152535732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
152635732d01SWei Wang 
152735732d01SWei Wang out:
152835732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
152935732d01SWei Wang 
153035732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1531b886d5f2SPaolo Abeni 	if (!err) {
15325012f0a5SDavid Ahern 		spin_lock_bh(&f6i->fib6_table->tb6_lock);
15335012f0a5SDavid Ahern 		fib6_update_sernum(net, f6i);
15345012f0a5SDavid Ahern 		spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1535b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1536b886d5f2SPaolo Abeni 	}
153735732d01SWei Wang 
153835732d01SWei Wang 	return err;
153935732d01SWei Wang }
154035732d01SWei Wang 
15418d1c802bSDavid Ahern void rt6_flush_exceptions(struct fib6_info *rt)
154235732d01SWei Wang {
154335732d01SWei Wang 	struct rt6_exception_bucket *bucket;
154435732d01SWei Wang 	struct rt6_exception *rt6_ex;
154535732d01SWei Wang 	struct hlist_node *tmp;
154635732d01SWei Wang 	int i;
154735732d01SWei Wang 
154835732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
154935732d01SWei Wang 	/* Prevent rt6_insert_exception() to recreate the bucket list */
155035732d01SWei Wang 	rt->exception_bucket_flushed = 1;
155135732d01SWei Wang 
155235732d01SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
155335732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
155435732d01SWei Wang 	if (!bucket)
155535732d01SWei Wang 		goto out;
155635732d01SWei Wang 
155735732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
155835732d01SWei Wang 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
155935732d01SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
156035732d01SWei Wang 		WARN_ON_ONCE(bucket->depth);
156135732d01SWei Wang 		bucket++;
156235732d01SWei Wang 	}
156335732d01SWei Wang 
156435732d01SWei Wang out:
156535732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
156635732d01SWei Wang }
156735732d01SWei Wang 
156835732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
156935732d01SWei Wang  * Caller has to hold rcu_read_lock()
157035732d01SWei Wang  */
15717e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1572510e2cedSWei Wang 					   const struct in6_addr *daddr,
1573510e2cedSWei Wang 					   const struct in6_addr *saddr)
157435732d01SWei Wang {
1575510e2cedSWei Wang 	const struct in6_addr *src_key = NULL;
157635732d01SWei Wang 	struct rt6_exception_bucket *bucket;
157735732d01SWei Wang 	struct rt6_exception *rt6_ex;
15787e4b5128SDavid Ahern 	struct rt6_info *ret = NULL;
157935732d01SWei Wang 
158035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
15817e4b5128SDavid Ahern 	/* fib6i_src.plen != 0 indicates f6i is in subtree
158235732d01SWei Wang 	 * and exception table is indexed by a hash of
15837e4b5128SDavid Ahern 	 * both fib6_dst and fib6_src.
1584510e2cedSWei Wang 	 * However, the src addr used to create the hash
1585510e2cedSWei Wang 	 * might not be exactly the passed in saddr which
1586510e2cedSWei Wang 	 * is a /128 addr from the flow.
1587510e2cedSWei Wang 	 * So we need to use f6i->fib6_src to redo lookup
1588510e2cedSWei Wang 	 * if the passed in saddr does not find anything.
1589510e2cedSWei Wang 	 * (See the logic in ip6_rt_cache_alloc() on how
1590510e2cedSWei Wang 	 * rt->rt6i_src is updated.)
159135732d01SWei Wang 	 */
15927e4b5128SDavid Ahern 	if (res->f6i->fib6_src.plen)
159335732d01SWei Wang 		src_key = saddr;
1594510e2cedSWei Wang find_ex:
159535732d01SWei Wang #endif
1596510e2cedSWei Wang 	bucket = rcu_dereference(res->f6i->rt6i_exception_bucket);
159735732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
159835732d01SWei Wang 
159935732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
16007e4b5128SDavid Ahern 		ret = rt6_ex->rt6i;
160135732d01SWei Wang 
1602510e2cedSWei Wang #ifdef CONFIG_IPV6_SUBTREES
1603510e2cedSWei Wang 	/* Use fib6_src as src_key and redo lookup */
1604510e2cedSWei Wang 	if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1605510e2cedSWei Wang 		src_key = &res->f6i->fib6_src.addr;
1606510e2cedSWei Wang 		goto find_ex;
1607510e2cedSWei Wang 	}
1608510e2cedSWei Wang #endif
1609510e2cedSWei Wang 
16107e4b5128SDavid Ahern 	return ret;
161135732d01SWei Wang }
161235732d01SWei Wang 
161335732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
161423fb93a4SDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt)
161535732d01SWei Wang {
161635732d01SWei Wang 	struct rt6_exception_bucket *bucket;
161735732d01SWei Wang 	struct in6_addr *src_key = NULL;
161835732d01SWei Wang 	struct rt6_exception *rt6_ex;
16198a14e46fSDavid Ahern 	struct fib6_info *from;
162035732d01SWei Wang 	int err;
162135732d01SWei Wang 
1622091311deSEric Dumazet 	from = rcu_dereference(rt->from);
162335732d01SWei Wang 	if (!from ||
1624442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
162535732d01SWei Wang 		return -EINVAL;
162635732d01SWei Wang 
162735732d01SWei Wang 	if (!rcu_access_pointer(from->rt6i_exception_bucket))
162835732d01SWei Wang 		return -ENOENT;
162935732d01SWei Wang 
163035732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
163135732d01SWei Wang 	bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
163235732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
163335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
163435732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
163535732d01SWei Wang 	 * and exception table is indexed by a hash of
163635732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
163735732d01SWei Wang 	 * Otherwise, the exception table is indexed by
163835732d01SWei Wang 	 * a hash of only rt6i_dst.
163935732d01SWei Wang 	 */
164093c2fb25SDavid Ahern 	if (from->fib6_src.plen)
164135732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
164235732d01SWei Wang #endif
164335732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
164435732d01SWei Wang 					       &rt->rt6i_dst.addr,
164535732d01SWei Wang 					       src_key);
164635732d01SWei Wang 	if (rt6_ex) {
164735732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
164835732d01SWei Wang 		err = 0;
164935732d01SWei Wang 	} else {
165035732d01SWei Wang 		err = -ENOENT;
165135732d01SWei Wang 	}
165235732d01SWei Wang 
165335732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
165435732d01SWei Wang 	return err;
165535732d01SWei Wang }
165635732d01SWei Wang 
165735732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
165835732d01SWei Wang  * refresh its stamp
165935732d01SWei Wang  */
166035732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
166135732d01SWei Wang {
166235732d01SWei Wang 	struct rt6_exception_bucket *bucket;
166335732d01SWei Wang 	struct in6_addr *src_key = NULL;
166435732d01SWei Wang 	struct rt6_exception *rt6_ex;
1665193f3685SPaolo Abeni 	struct fib6_info *from;
166635732d01SWei Wang 
166735732d01SWei Wang 	rcu_read_lock();
1668193f3685SPaolo Abeni 	from = rcu_dereference(rt->from);
1669193f3685SPaolo Abeni 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1670193f3685SPaolo Abeni 		goto unlock;
1671193f3685SPaolo Abeni 
167235732d01SWei Wang 	bucket = rcu_dereference(from->rt6i_exception_bucket);
167335732d01SWei Wang 
167435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
167535732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
167635732d01SWei Wang 	 * and exception table is indexed by a hash of
167735732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
167835732d01SWei Wang 	 * Otherwise, the exception table is indexed by
167935732d01SWei Wang 	 * a hash of only rt6i_dst.
168035732d01SWei Wang 	 */
168193c2fb25SDavid Ahern 	if (from->fib6_src.plen)
168235732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
168335732d01SWei Wang #endif
168435732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket,
168535732d01SWei Wang 					  &rt->rt6i_dst.addr,
168635732d01SWei Wang 					  src_key);
168735732d01SWei Wang 	if (rt6_ex)
168835732d01SWei Wang 		rt6_ex->stamp = jiffies;
168935732d01SWei Wang 
1690193f3685SPaolo Abeni unlock:
169135732d01SWei Wang 	rcu_read_unlock();
169235732d01SWei Wang }
169335732d01SWei Wang 
1694e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1695e9fa1495SStefano Brivio 					 struct rt6_info *rt, int mtu)
1696e9fa1495SStefano Brivio {
1697e9fa1495SStefano Brivio 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1698e9fa1495SStefano Brivio 	 * lowest MTU in the path: always allow updating the route PMTU to
1699e9fa1495SStefano Brivio 	 * reflect PMTU decreases.
1700e9fa1495SStefano Brivio 	 *
1701e9fa1495SStefano Brivio 	 * If the new MTU is higher, and the route PMTU is equal to the local
1702e9fa1495SStefano Brivio 	 * MTU, this means the old MTU is the lowest in the path, so allow
1703e9fa1495SStefano Brivio 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1704e9fa1495SStefano Brivio 	 * handle this.
1705e9fa1495SStefano Brivio 	 */
1706e9fa1495SStefano Brivio 
1707e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) >= mtu)
1708e9fa1495SStefano Brivio 		return true;
1709e9fa1495SStefano Brivio 
1710e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1711e9fa1495SStefano Brivio 		return true;
1712e9fa1495SStefano Brivio 
1713e9fa1495SStefano Brivio 	return false;
1714e9fa1495SStefano Brivio }
1715e9fa1495SStefano Brivio 
1716e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
17178d1c802bSDavid Ahern 				       struct fib6_info *rt, int mtu)
1718f5bbe7eeSWei Wang {
1719f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
1720f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
1721f5bbe7eeSWei Wang 	int i;
1722f5bbe7eeSWei Wang 
1723f5bbe7eeSWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1724f5bbe7eeSWei Wang 					lockdep_is_held(&rt6_exception_lock));
1725f5bbe7eeSWei Wang 
1726e9fa1495SStefano Brivio 	if (!bucket)
1727e9fa1495SStefano Brivio 		return;
1728e9fa1495SStefano Brivio 
1729f5bbe7eeSWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1730f5bbe7eeSWei Wang 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1731f5bbe7eeSWei Wang 			struct rt6_info *entry = rt6_ex->rt6i;
1732e9fa1495SStefano Brivio 
1733e9fa1495SStefano Brivio 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
1734d4ead6b3SDavid Ahern 			 * route), the metrics of its rt->from have already
1735f5bbe7eeSWei Wang 			 * been updated.
1736f5bbe7eeSWei Wang 			 */
1737d4ead6b3SDavid Ahern 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
1738e9fa1495SStefano Brivio 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
1739d4ead6b3SDavid Ahern 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
1740f5bbe7eeSWei Wang 		}
1741f5bbe7eeSWei Wang 		bucket++;
1742f5bbe7eeSWei Wang 	}
1743f5bbe7eeSWei Wang }
1744f5bbe7eeSWei Wang 
1745b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
1746b16cb459SWei Wang 
17478d1c802bSDavid Ahern static void rt6_exceptions_clean_tohost(struct fib6_info *rt,
1748b16cb459SWei Wang 					struct in6_addr *gateway)
1749b16cb459SWei Wang {
1750b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
1751b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
1752b16cb459SWei Wang 	struct hlist_node *tmp;
1753b16cb459SWei Wang 	int i;
1754b16cb459SWei Wang 
1755b16cb459SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1756b16cb459SWei Wang 		return;
1757b16cb459SWei Wang 
1758b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1759b16cb459SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1760b16cb459SWei Wang 				     lockdep_is_held(&rt6_exception_lock));
1761b16cb459SWei Wang 
1762b16cb459SWei Wang 	if (bucket) {
1763b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1764b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1765b16cb459SWei Wang 						  &bucket->chain, hlist) {
1766b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1767b16cb459SWei Wang 
1768b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1769b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
1770b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
1771b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
1772b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
1773b16cb459SWei Wang 				}
1774b16cb459SWei Wang 			}
1775b16cb459SWei Wang 			bucket++;
1776b16cb459SWei Wang 		}
1777b16cb459SWei Wang 	}
1778b16cb459SWei Wang 
1779b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1780b16cb459SWei Wang }
1781b16cb459SWei Wang 
1782c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1783c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
1784c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
1785c757faa8SWei Wang 				      unsigned long now)
1786c757faa8SWei Wang {
1787c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
1788c757faa8SWei Wang 
17891859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
17901859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
17911859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
17921859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
17931859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
17941859bac0SPaolo Abeni 	 */
179531afeb42SWei Wang 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
179631afeb42SWei Wang 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1797c757faa8SWei Wang 			RT6_TRACE("aging clone %p\n", rt);
1798c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1799c757faa8SWei Wang 			return;
180031afeb42SWei Wang 		}
180131afeb42SWei Wang 	} else if (time_after(jiffies, rt->dst.expires)) {
180231afeb42SWei Wang 		RT6_TRACE("purging expired route %p\n", rt);
180331afeb42SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
180431afeb42SWei Wang 		return;
180531afeb42SWei Wang 	}
180631afeb42SWei Wang 
180731afeb42SWei Wang 	if (rt->rt6i_flags & RTF_GATEWAY) {
1808c757faa8SWei Wang 		struct neighbour *neigh;
1809c757faa8SWei Wang 		__u8 neigh_flags = 0;
1810c757faa8SWei Wang 
18111bfa26ffSEric Dumazet 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
18121bfa26ffSEric Dumazet 		if (neigh)
1813c757faa8SWei Wang 			neigh_flags = neigh->flags;
18141bfa26ffSEric Dumazet 
1815c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
1816c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
1817c757faa8SWei Wang 				  rt);
1818c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1819c757faa8SWei Wang 			return;
1820c757faa8SWei Wang 		}
1821c757faa8SWei Wang 	}
182231afeb42SWei Wang 
1823c757faa8SWei Wang 	gc_args->more++;
1824c757faa8SWei Wang }
1825c757faa8SWei Wang 
18268d1c802bSDavid Ahern void rt6_age_exceptions(struct fib6_info *rt,
1827c757faa8SWei Wang 			struct fib6_gc_args *gc_args,
1828c757faa8SWei Wang 			unsigned long now)
1829c757faa8SWei Wang {
1830c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
1831c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
1832c757faa8SWei Wang 	struct hlist_node *tmp;
1833c757faa8SWei Wang 	int i;
1834c757faa8SWei Wang 
1835c757faa8SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1836c757faa8SWei Wang 		return;
1837c757faa8SWei Wang 
18381bfa26ffSEric Dumazet 	rcu_read_lock_bh();
18391bfa26ffSEric Dumazet 	spin_lock(&rt6_exception_lock);
1840c757faa8SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1841c757faa8SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
1842c757faa8SWei Wang 
1843c757faa8SWei Wang 	if (bucket) {
1844c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1845c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1846c757faa8SWei Wang 						  &bucket->chain, hlist) {
1847c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
1848c757faa8SWei Wang 							  gc_args, now);
1849c757faa8SWei Wang 			}
1850c757faa8SWei Wang 			bucket++;
1851c757faa8SWei Wang 		}
1852c757faa8SWei Wang 	}
18531bfa26ffSEric Dumazet 	spin_unlock(&rt6_exception_lock);
18541bfa26ffSEric Dumazet 	rcu_read_unlock_bh();
1855c757faa8SWei Wang }
1856c757faa8SWei Wang 
18571d053da9SDavid Ahern /* must be called with rcu lock held */
1858effda4ddSDavid Ahern int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
1859effda4ddSDavid Ahern 		      struct flowi6 *fl6, struct fib6_result *res, int strict)
18601da177e4SLinus Torvalds {
1861367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
18621da177e4SLinus Torvalds 
18636454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1864367efcb9SMartin KaFai Lau 	saved_fn = fn;
18651da177e4SLinus Torvalds 
1866ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1867ca254490SDavid Ahern 		oif = 0;
1868ca254490SDavid Ahern 
1869a3c00e46SMartin KaFai Lau redo_rt6_select:
1870effda4ddSDavid Ahern 	rt6_select(net, fn, oif, res, strict);
1871effda4ddSDavid Ahern 	if (res->f6i == net->ipv6.fib6_null_entry) {
1872a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1873a3c00e46SMartin KaFai Lau 		if (fn)
1874a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
1875367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
1876367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
1877367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
1878367efcb9SMartin KaFai Lau 			fn = saved_fn;
1879367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
1880367efcb9SMartin KaFai Lau 		}
1881a3c00e46SMartin KaFai Lau 	}
1882a3c00e46SMartin KaFai Lau 
1883effda4ddSDavid Ahern 	trace_fib6_table_lookup(net, res, table, fl6);
1884d52d3997SMartin KaFai Lau 
1885effda4ddSDavid Ahern 	return 0;
18861d053da9SDavid Ahern }
18871d053da9SDavid Ahern 
18881d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
18891d053da9SDavid Ahern 			       int oif, struct flowi6 *fl6,
18901d053da9SDavid Ahern 			       const struct sk_buff *skb, int flags)
18911d053da9SDavid Ahern {
1892b1d40991SDavid Ahern 	struct fib6_result res = {};
18931d053da9SDavid Ahern 	struct rt6_info *rt;
18941d053da9SDavid Ahern 	int strict = 0;
18951d053da9SDavid Ahern 
18961d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IFACE;
18971d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
18981d053da9SDavid Ahern 	if (net->ipv6.devconf_all->forwarding == 0)
18991d053da9SDavid Ahern 		strict |= RT6_LOOKUP_F_REACHABLE;
19001d053da9SDavid Ahern 
19011d053da9SDavid Ahern 	rcu_read_lock();
19021d053da9SDavid Ahern 
1903effda4ddSDavid Ahern 	fib6_table_lookup(net, table, oif, fl6, &res, strict);
1904b1d40991SDavid Ahern 	if (res.f6i == net->ipv6.fib6_null_entry) {
1905421842edSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
190666f5d6ceSWei Wang 		rcu_read_unlock();
1907d3843fe5SWei Wang 		dst_hold(&rt->dst);
1908d3843fe5SWei Wang 		return rt;
1909d3843fe5SWei Wang 	}
191023fb93a4SDavid Ahern 
1911b1d40991SDavid Ahern 	fib6_select_path(net, &res, fl6, oif, false, skb, strict);
1912d83009d4SDavid Ahern 
191323fb93a4SDavid Ahern 	/*Search through exception table */
19147e4b5128SDavid Ahern 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
191523fb93a4SDavid Ahern 	if (rt) {
191610585b43SDavid Ahern 		if (ip6_hold_safe(net, &rt))
19171da177e4SLinus Torvalds 			dst_use_noref(&rt->dst, jiffies);
1918d4ead6b3SDavid Ahern 
191966f5d6ceSWei Wang 		rcu_read_unlock();
1920d52d3997SMartin KaFai Lau 		return rt;
19213da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
1922b1d40991SDavid Ahern 			    !res.nh->fib_nh_gw_family)) {
19233da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
19243da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
19253da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
19263da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
19273da59bd9SMartin KaFai Lau 		 */
19283da59bd9SMartin KaFai Lau 		struct rt6_info *uncached_rt;
19293da59bd9SMartin KaFai Lau 
193085bd05deSDavid Ahern 		uncached_rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
1931d52d3997SMartin KaFai Lau 
19324d85cd0cSDavid Ahern 		rcu_read_unlock();
19333da59bd9SMartin KaFai Lau 
19341cfb71eeSWei Wang 		if (uncached_rt) {
19351cfb71eeSWei Wang 			/* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
19361cfb71eeSWei Wang 			 * No need for another dst_hold()
19371cfb71eeSWei Wang 			 */
19388d0b94afSMartin KaFai Lau 			rt6_uncached_list_add(uncached_rt);
193981eb8447SWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
19401cfb71eeSWei Wang 		} else {
19413da59bd9SMartin KaFai Lau 			uncached_rt = net->ipv6.ip6_null_entry;
19423da59bd9SMartin KaFai Lau 			dst_hold(&uncached_rt->dst);
19431cfb71eeSWei Wang 		}
1944b811580dSDavid Ahern 
19453da59bd9SMartin KaFai Lau 		return uncached_rt;
1946d52d3997SMartin KaFai Lau 	} else {
1947d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
1948d52d3997SMartin KaFai Lau 
1949d52d3997SMartin KaFai Lau 		struct rt6_info *pcpu_rt;
1950d52d3997SMartin KaFai Lau 
1951951f788aSEric Dumazet 		local_bh_disable();
1952db3fedeeSDavid Ahern 		pcpu_rt = rt6_get_pcpu_route(&res);
1953d52d3997SMartin KaFai Lau 
195493531c67SDavid Ahern 		if (!pcpu_rt)
1955db3fedeeSDavid Ahern 			pcpu_rt = rt6_make_pcpu_route(net, &res);
195693531c67SDavid Ahern 
1957951f788aSEric Dumazet 		local_bh_enable();
1958951f788aSEric Dumazet 		rcu_read_unlock();
1959d4bea421SDavid Ahern 
1960d52d3997SMartin KaFai Lau 		return pcpu_rt;
1961d52d3997SMartin KaFai Lau 	}
1962c71099acSThomas Graf }
19639ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
1964c71099acSThomas Graf 
1965b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net,
1966b75cc8f9SDavid Ahern 					    struct fib6_table *table,
1967b75cc8f9SDavid Ahern 					    struct flowi6 *fl6,
1968b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
1969b75cc8f9SDavid Ahern 					    int flags)
19704acad72dSPavel Emelyanov {
1971b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
19724acad72dSPavel Emelyanov }
19734acad72dSPavel Emelyanov 
1974d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
197572331bc0SShmulik Ladkani 					 struct net_device *dev,
1976b75cc8f9SDavid Ahern 					 struct flowi6 *fl6,
1977b75cc8f9SDavid Ahern 					 const struct sk_buff *skb,
1978b75cc8f9SDavid Ahern 					 int flags)
197972331bc0SShmulik Ladkani {
198072331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
198172331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
198272331bc0SShmulik Ladkani 
1983b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
198472331bc0SShmulik Ladkani }
1985d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
198672331bc0SShmulik Ladkani 
198723aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
19885e5d6fedSRoopa Prabhu 				  struct flow_keys *keys,
19895e5d6fedSRoopa Prabhu 				  struct flow_keys *flkeys)
199023aebdacSJakub Sitnicki {
199123aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
199223aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
19935e5d6fedSRoopa Prabhu 	struct flow_keys *_flkeys = flkeys;
199423aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
199523aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
199623aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
1997cea67a2dSEric Dumazet 	struct icmp6hdr _icmph;
199823aebdacSJakub Sitnicki 
199923aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
200023aebdacSJakub Sitnicki 		goto out;
200123aebdacSJakub Sitnicki 
2002cea67a2dSEric Dumazet 	icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2003cea67a2dSEric Dumazet 				   sizeof(_icmph), &_icmph);
2004cea67a2dSEric Dumazet 	if (!icmph)
2005cea67a2dSEric Dumazet 		goto out;
2006cea67a2dSEric Dumazet 
200723aebdacSJakub Sitnicki 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
200823aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
200923aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
201023aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
201123aebdacSJakub Sitnicki 		goto out;
201223aebdacSJakub Sitnicki 
201323aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
201423aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
201523aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
201623aebdacSJakub Sitnicki 	if (!inner_iph)
201723aebdacSJakub Sitnicki 		goto out;
201823aebdacSJakub Sitnicki 
201923aebdacSJakub Sitnicki 	key_iph = inner_iph;
20205e5d6fedSRoopa Prabhu 	_flkeys = NULL;
202123aebdacSJakub Sitnicki out:
20225e5d6fedSRoopa Prabhu 	if (_flkeys) {
20235e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
20245e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
20255e5d6fedSRoopa Prabhu 		keys->tags.flow_label = _flkeys->tags.flow_label;
20265e5d6fedSRoopa Prabhu 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
20275e5d6fedSRoopa Prabhu 	} else {
202823aebdacSJakub Sitnicki 		keys->addrs.v6addrs.src = key_iph->saddr;
202923aebdacSJakub Sitnicki 		keys->addrs.v6addrs.dst = key_iph->daddr;
2030fa1be7e0SMichal Kubecek 		keys->tags.flow_label = ip6_flowlabel(key_iph);
203123aebdacSJakub Sitnicki 		keys->basic.ip_proto = key_iph->nexthdr;
203223aebdacSJakub Sitnicki 	}
20335e5d6fedSRoopa Prabhu }
203423aebdacSJakub Sitnicki 
203523aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
2036b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2037b4bac172SDavid Ahern 		       const struct sk_buff *skb, struct flow_keys *flkeys)
203823aebdacSJakub Sitnicki {
203923aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
20409a2a537aSDavid Ahern 	u32 mhash;
204123aebdacSJakub Sitnicki 
2042bbfa047aSDavid S. Miller 	switch (ip6_multipath_hash_policy(net)) {
2043b4bac172SDavid Ahern 	case 0:
20446f74b6c2SDavid Ahern 		memset(&hash_keys, 0, sizeof(hash_keys));
20456f74b6c2SDavid Ahern 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
20469a2a537aSDavid Ahern 		if (skb) {
20475e5d6fedSRoopa Prabhu 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
20489a2a537aSDavid Ahern 		} else {
20499a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
20509a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2051fa1be7e0SMichal Kubecek 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
20529a2a537aSDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
205323aebdacSJakub Sitnicki 		}
2054b4bac172SDavid Ahern 		break;
2055b4bac172SDavid Ahern 	case 1:
2056b4bac172SDavid Ahern 		if (skb) {
2057b4bac172SDavid Ahern 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2058b4bac172SDavid Ahern 			struct flow_keys keys;
2059b4bac172SDavid Ahern 
2060b4bac172SDavid Ahern 			/* short-circuit if we already have L4 hash present */
2061b4bac172SDavid Ahern 			if (skb->l4_hash)
2062b4bac172SDavid Ahern 				return skb_get_hash_raw(skb) >> 1;
2063b4bac172SDavid Ahern 
2064b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2065b4bac172SDavid Ahern 
2066b4bac172SDavid Ahern                         if (!flkeys) {
2067b4bac172SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2068b4bac172SDavid Ahern 				flkeys = &keys;
2069b4bac172SDavid Ahern 			}
2070b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2071b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2072b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2073b4bac172SDavid Ahern 			hash_keys.ports.src = flkeys->ports.src;
2074b4bac172SDavid Ahern 			hash_keys.ports.dst = flkeys->ports.dst;
2075b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2076b4bac172SDavid Ahern 		} else {
2077b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2078b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2079b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2080b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2081b4bac172SDavid Ahern 			hash_keys.ports.src = fl6->fl6_sport;
2082b4bac172SDavid Ahern 			hash_keys.ports.dst = fl6->fl6_dport;
2083b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2084b4bac172SDavid Ahern 		}
2085b4bac172SDavid Ahern 		break;
2086b4bac172SDavid Ahern 	}
20879a2a537aSDavid Ahern 	mhash = flow_hash_from_keys(&hash_keys);
208823aebdacSJakub Sitnicki 
20899a2a537aSDavid Ahern 	return mhash >> 1;
209023aebdacSJakub Sitnicki }
209123aebdacSJakub Sitnicki 
2092c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
2093c71099acSThomas Graf {
2094b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2095c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
2096adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2097904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
20984c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
2099e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
21004c9483b2SDavid S. Miller 		.daddr = iph->daddr,
21014c9483b2SDavid S. Miller 		.saddr = iph->saddr,
21026502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
21034c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
21044c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
2105c71099acSThomas Graf 	};
21065e5d6fedSRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2107adaa70bbSThomas Graf 
2108904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
210946fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2110904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
21115e5d6fedSRoopa Prabhu 
21125e5d6fedSRoopa Prabhu 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
21135e5d6fedSRoopa Prabhu 		flkeys = &_flkeys;
21145e5d6fedSRoopa Prabhu 
211523aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2116b4bac172SDavid Ahern 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
211706e9d040SJiri Benc 	skb_dst_drop(skb);
2118b75cc8f9SDavid Ahern 	skb_dst_set(skb,
2119b75cc8f9SDavid Ahern 		    ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
2120c71099acSThomas Graf }
2121c71099acSThomas Graf 
2122b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net,
2123b75cc8f9SDavid Ahern 					     struct fib6_table *table,
2124b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
2125b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2126b75cc8f9SDavid Ahern 					     int flags)
2127c71099acSThomas Graf {
2128b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2129c71099acSThomas Graf }
2130c71099acSThomas Graf 
21316f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
21326f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags)
2133c71099acSThomas Graf {
2134d46a9d67SDavid Ahern 	bool any_src;
2135c71099acSThomas Graf 
21363ede0bbcSRobert Shearman 	if (ipv6_addr_type(&fl6->daddr) &
21373ede0bbcSRobert Shearman 	    (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
21384c1feac5SDavid Ahern 		struct dst_entry *dst;
21394c1feac5SDavid Ahern 
21404c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
2141ca254490SDavid Ahern 		if (dst)
2142ca254490SDavid Ahern 			return dst;
21434c1feac5SDavid Ahern 	}
2144ca254490SDavid Ahern 
21451fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
21464dc27d1cSDavid McCullough 
2147d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
2148741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2149d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
215077d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
2151c71099acSThomas Graf 
2152d46a9d67SDavid Ahern 	if (!any_src)
2153adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
21540c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
21550c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2156adaa70bbSThomas Graf 
2157b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
21581da177e4SLinus Torvalds }
21596f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
21601da177e4SLinus Torvalds 
21612774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
216214e50e57SDavid S. Miller {
21635c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
21641dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
216514e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
216614e50e57SDavid S. Miller 
21671dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
216862cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
216914e50e57SDavid S. Miller 	if (rt) {
21700a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
217181eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
21720a1f5962SMartin KaFai Lau 
2173d8d1f30bSChangli Gao 		new = &rt->dst;
217414e50e57SDavid S. Miller 		new->__use = 1;
2175352e512cSHerbert Xu 		new->input = dst_discard;
2176ede2059dSEric W. Biederman 		new->output = dst_discard_out;
217714e50e57SDavid S. Miller 
2178defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
217914e50e57SDavid S. Miller 
21801dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
21814e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
21820a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
218314e50e57SDavid S. Miller 
218414e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
218514e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
218614e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
218714e50e57SDavid S. Miller #endif
218814e50e57SDavid S. Miller 	}
218914e50e57SDavid S. Miller 
219069ead7afSDavid S. Miller 	dst_release(dst_orig);
219169ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
219214e50e57SDavid S. Miller }
219314e50e57SDavid S. Miller 
21941da177e4SLinus Torvalds /*
21951da177e4SLinus Torvalds  *	Destination cache support functions
21961da177e4SLinus Torvalds  */
21971da177e4SLinus Torvalds 
21988d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie)
21993da59bd9SMartin KaFai Lau {
220036143645SSteffen Klassert 	u32 rt_cookie = 0;
2201c5cff856SWei Wang 
22028ae86971SDavid Ahern 	if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
220393531c67SDavid Ahern 		return false;
220493531c67SDavid Ahern 
220593531c67SDavid Ahern 	if (fib6_check_expired(f6i))
220693531c67SDavid Ahern 		return false;
220793531c67SDavid Ahern 
220893531c67SDavid Ahern 	return true;
220993531c67SDavid Ahern }
221093531c67SDavid Ahern 
2211a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt,
2212a68886a6SDavid Ahern 				   struct fib6_info *from,
2213a68886a6SDavid Ahern 				   u32 cookie)
22143da59bd9SMartin KaFai Lau {
2215c5cff856SWei Wang 	u32 rt_cookie = 0;
2216c5cff856SWei Wang 
2217a68886a6SDavid Ahern 	if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
221893531c67SDavid Ahern 	    rt_cookie != cookie)
22193da59bd9SMartin KaFai Lau 		return NULL;
22203da59bd9SMartin KaFai Lau 
22213da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
22223da59bd9SMartin KaFai Lau 		return NULL;
22233da59bd9SMartin KaFai Lau 
22243da59bd9SMartin KaFai Lau 	return &rt->dst;
22253da59bd9SMartin KaFai Lau }
22263da59bd9SMartin KaFai Lau 
2227a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2228a68886a6SDavid Ahern 					    struct fib6_info *from,
2229a68886a6SDavid Ahern 					    u32 cookie)
22303da59bd9SMartin KaFai Lau {
22315973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
22325973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2233a68886a6SDavid Ahern 	    fib6_check(from, cookie))
22343da59bd9SMartin KaFai Lau 		return &rt->dst;
22353da59bd9SMartin KaFai Lau 	else
22363da59bd9SMartin KaFai Lau 		return NULL;
22373da59bd9SMartin KaFai Lau }
22383da59bd9SMartin KaFai Lau 
22391da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
22401da177e4SLinus Torvalds {
2241a87b7dc9SDavid Ahern 	struct dst_entry *dst_ret;
2242a68886a6SDavid Ahern 	struct fib6_info *from;
22431da177e4SLinus Torvalds 	struct rt6_info *rt;
22441da177e4SLinus Torvalds 
2245a87b7dc9SDavid Ahern 	rt = container_of(dst, struct rt6_info, dst);
2246a87b7dc9SDavid Ahern 
2247a87b7dc9SDavid Ahern 	rcu_read_lock();
22481da177e4SLinus Torvalds 
22496f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
22506f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
22516f3118b5SNicolas Dichtel 	 * into this function always.
22526f3118b5SNicolas Dichtel 	 */
2253e3bc10bdSHannes Frederic Sowa 
2254a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
22554b32b5adSMartin KaFai Lau 
2256a68886a6SDavid Ahern 	if (from && (rt->rt6i_flags & RTF_PCPU ||
2257a68886a6SDavid Ahern 	    unlikely(!list_empty(&rt->rt6i_uncached))))
2258a68886a6SDavid Ahern 		dst_ret = rt6_dst_from_check(rt, from, cookie);
22593da59bd9SMartin KaFai Lau 	else
2260a68886a6SDavid Ahern 		dst_ret = rt6_check(rt, from, cookie);
2261a87b7dc9SDavid Ahern 
2262a87b7dc9SDavid Ahern 	rcu_read_unlock();
2263a87b7dc9SDavid Ahern 
2264a87b7dc9SDavid Ahern 	return dst_ret;
22651da177e4SLinus Torvalds }
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
22681da177e4SLinus Torvalds {
22691da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds 	if (rt) {
227254c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
2273c3c14da0SDavid Ahern 			rcu_read_lock();
227454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
227593531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
227654c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
22771da177e4SLinus Torvalds 			}
2278c3c14da0SDavid Ahern 			rcu_read_unlock();
227954c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
228054c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
228154c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
228254c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
228354c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
228454c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
22851da177e4SLinus Torvalds }
22861da177e4SLinus Torvalds 
22871da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
22881da177e4SLinus Torvalds {
22891da177e4SLinus Torvalds 	struct rt6_info *rt;
22901da177e4SLinus Torvalds 
22913ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
22921da177e4SLinus Torvalds 
2293adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
22941da177e4SLinus Torvalds 	if (rt) {
22958a14e46fSDavid Ahern 		rcu_read_lock();
22961eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
229793531c67SDavid Ahern 			rt6_remove_exception_rt(rt);
2298c5cff856SWei Wang 		} else {
2299a68886a6SDavid Ahern 			struct fib6_info *from;
2300c5cff856SWei Wang 			struct fib6_node *fn;
2301c5cff856SWei Wang 
2302a68886a6SDavid Ahern 			from = rcu_dereference(rt->from);
2303a68886a6SDavid Ahern 			if (from) {
2304a68886a6SDavid Ahern 				fn = rcu_dereference(from->fib6_node);
2305c5cff856SWei Wang 				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2306c5cff856SWei Wang 					fn->fn_sernum = -1;
2307a68886a6SDavid Ahern 			}
23081da177e4SLinus Torvalds 		}
23091da177e4SLinus Torvalds 		rcu_read_unlock();
23101da177e4SLinus Torvalds 	}
23111da177e4SLinus Torvalds }
23121da177e4SLinus Torvalds 
23136a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout)
23146a3e030fSDavid Ahern {
2315a68886a6SDavid Ahern 	if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2316a68886a6SDavid Ahern 		struct fib6_info *from;
2317a68886a6SDavid Ahern 
2318a68886a6SDavid Ahern 		rcu_read_lock();
2319a68886a6SDavid Ahern 		from = rcu_dereference(rt0->from);
2320a68886a6SDavid Ahern 		if (from)
2321a68886a6SDavid Ahern 			rt0->dst.expires = from->expires;
2322a68886a6SDavid Ahern 		rcu_read_unlock();
2323a68886a6SDavid Ahern 	}
23246a3e030fSDavid Ahern 
23256a3e030fSDavid Ahern 	dst_set_expires(&rt0->dst, timeout);
23266a3e030fSDavid Ahern 	rt0->rt6i_flags |= RTF_EXPIRES;
23276700c270SDavid S. Miller }
23281da177e4SLinus Torvalds 
232945e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
233045e4fd26SMartin KaFai Lau {
233145e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
233245e4fd26SMartin KaFai Lau 
2333d4ead6b3SDavid Ahern 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
233445e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
233545e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
233645e4fd26SMartin KaFai Lau }
233745e4fd26SMartin KaFai Lau 
23380d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
23390d3f6d29SMartin KaFai Lau {
23400d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
23411490ed2aSPaolo Abeni 		(rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
23420d3f6d29SMartin KaFai Lau }
23430d3f6d29SMartin KaFai Lau 
234445e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
234545e4fd26SMartin KaFai Lau 				 const struct ipv6hdr *iph, u32 mtu)
23461da177e4SLinus Torvalds {
23470dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
23481da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
23491da177e4SLinus Torvalds 
235019bda36cSXin Long 	if (dst_metric_locked(dst, RTAX_MTU))
235119bda36cSXin Long 		return;
235219bda36cSXin Long 
235345e4fd26SMartin KaFai Lau 	if (iph) {
235445e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
235545e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
235645e4fd26SMartin KaFai Lau 	} else if (sk) {
235745e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
235845e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
235945e4fd26SMartin KaFai Lau 	} else {
23600dec879fSJulian Anastasov 		daddr = NULL;
23610dec879fSJulian Anastasov 		saddr = NULL;
23621da177e4SLinus Torvalds 	}
23630dec879fSJulian Anastasov 	dst_confirm_neigh(dst, daddr);
23640dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
23650dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
23660dec879fSJulian Anastasov 		return;
23670dec879fSJulian Anastasov 
23680dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
23690dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
23702b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
23712b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
23722b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
23730dec879fSJulian Anastasov 	} else if (daddr) {
237485bd05deSDavid Ahern 		struct fib6_result res = {};
23750dec879fSJulian Anastasov 		struct rt6_info *nrt6;
23760dec879fSJulian Anastasov 
23774d85cd0cSDavid Ahern 		rcu_read_lock();
237885bd05deSDavid Ahern 		res.f6i = rcu_dereference(rt6->from);
237985bd05deSDavid Ahern 		if (!res.f6i) {
23809c69a132SJonathan Lemon 			rcu_read_unlock();
23819c69a132SJonathan Lemon 			return;
23829c69a132SJonathan Lemon 		}
238385bd05deSDavid Ahern 		res.nh = &res.f6i->fib6_nh;
23847d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
23857d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
23867d21fec9SDavid Ahern 
238785bd05deSDavid Ahern 		nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
238845e4fd26SMartin KaFai Lau 		if (nrt6) {
238945e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
23905012f0a5SDavid Ahern 			if (rt6_insert_exception(nrt6, &res))
23912b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
239245e4fd26SMartin KaFai Lau 		}
2393a68886a6SDavid Ahern 		rcu_read_unlock();
239445e4fd26SMartin KaFai Lau 	}
239545e4fd26SMartin KaFai Lau }
239645e4fd26SMartin KaFai Lau 
239745e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
239845e4fd26SMartin KaFai Lau 			       struct sk_buff *skb, u32 mtu)
239945e4fd26SMartin KaFai Lau {
240045e4fd26SMartin KaFai Lau 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
24011da177e4SLinus Torvalds }
24021da177e4SLinus Torvalds 
240342ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2404e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
240581aded24SDavid S. Miller {
240681aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
240781aded24SDavid S. Miller 	struct dst_entry *dst;
2408dc92095dSMaciej Żenczykowski 	struct flowi6 fl6 = {
2409dc92095dSMaciej Żenczykowski 		.flowi6_oif = oif,
2410dc92095dSMaciej Żenczykowski 		.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2411dc92095dSMaciej Żenczykowski 		.daddr = iph->daddr,
2412dc92095dSMaciej Żenczykowski 		.saddr = iph->saddr,
2413dc92095dSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
2414dc92095dSMaciej Żenczykowski 		.flowi6_uid = uid,
2415dc92095dSMaciej Żenczykowski 	};
241681aded24SDavid S. Miller 
241781aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
241881aded24SDavid S. Miller 	if (!dst->error)
241945e4fd26SMartin KaFai Lau 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
242081aded24SDavid S. Miller 	dst_release(dst);
242181aded24SDavid S. Miller }
242281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
242381aded24SDavid S. Miller 
242481aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
242581aded24SDavid S. Miller {
24267ddacfa5SDavid Ahern 	int oif = sk->sk_bound_dev_if;
242733c162a9SMartin KaFai Lau 	struct dst_entry *dst;
242833c162a9SMartin KaFai Lau 
24297ddacfa5SDavid Ahern 	if (!oif && skb->dev)
24307ddacfa5SDavid Ahern 		oif = l3mdev_master_ifindex(skb->dev);
24317ddacfa5SDavid Ahern 
24327ddacfa5SDavid Ahern 	ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
243333c162a9SMartin KaFai Lau 
243433c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
243533c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
243633c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
243733c162a9SMartin KaFai Lau 		return;
243833c162a9SMartin KaFai Lau 
243933c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
244033c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
244133c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
244233c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
244381aded24SDavid S. Miller }
244481aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
244581aded24SDavid S. Miller 
24467d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
24477d6850f7SAlexey Kodanev 			   const struct flowi6 *fl6)
24487d6850f7SAlexey Kodanev {
24497d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
24507d6850f7SAlexey Kodanev 	struct ipv6_pinfo *np = inet6_sk(sk);
24517d6850f7SAlexey Kodanev #endif
24527d6850f7SAlexey Kodanev 
24537d6850f7SAlexey Kodanev 	ip6_dst_store(sk, dst,
24547d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
24557d6850f7SAlexey Kodanev 		      &sk->sk_v6_daddr : NULL,
24567d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
24577d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
24587d6850f7SAlexey Kodanev 		      &np->saddr :
24597d6850f7SAlexey Kodanev #endif
24607d6850f7SAlexey Kodanev 		      NULL);
24617d6850f7SAlexey Kodanev }
24627d6850f7SAlexey Kodanev 
24639b6b35abSDavid Ahern static bool ip6_redirect_nh_match(const struct fib6_result *res,
24640b34eb00SDavid Ahern 				  struct flowi6 *fl6,
24650b34eb00SDavid Ahern 				  const struct in6_addr *gw,
24660b34eb00SDavid Ahern 				  struct rt6_info **ret)
24670b34eb00SDavid Ahern {
24689b6b35abSDavid Ahern 	const struct fib6_nh *nh = res->nh;
24699b6b35abSDavid Ahern 
24700b34eb00SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
24710b34eb00SDavid Ahern 	    fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
24720b34eb00SDavid Ahern 		return false;
24730b34eb00SDavid Ahern 
24740b34eb00SDavid Ahern 	/* rt_cache's gateway might be different from its 'parent'
24750b34eb00SDavid Ahern 	 * in the case of an ip redirect.
24760b34eb00SDavid Ahern 	 * So we keep searching in the exception table if the gateway
24770b34eb00SDavid Ahern 	 * is different.
24780b34eb00SDavid Ahern 	 */
24790b34eb00SDavid Ahern 	if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
24800b34eb00SDavid Ahern 		struct rt6_info *rt_cache;
24810b34eb00SDavid Ahern 
24829b6b35abSDavid Ahern 		rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
24830b34eb00SDavid Ahern 		if (rt_cache &&
24840b34eb00SDavid Ahern 		    ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
24850b34eb00SDavid Ahern 			*ret = rt_cache;
24860b34eb00SDavid Ahern 			return true;
24870b34eb00SDavid Ahern 		}
24880b34eb00SDavid Ahern 		return false;
24890b34eb00SDavid Ahern 	}
24900b34eb00SDavid Ahern 	return true;
24910b34eb00SDavid Ahern }
24920b34eb00SDavid Ahern 
2493b55b76b2SDuan Jiong /* Handle redirects */
2494b55b76b2SDuan Jiong struct ip6rd_flowi {
2495b55b76b2SDuan Jiong 	struct flowi6 fl6;
2496b55b76b2SDuan Jiong 	struct in6_addr gateway;
2497b55b76b2SDuan Jiong };
2498b55b76b2SDuan Jiong 
2499b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
2500b55b76b2SDuan Jiong 					     struct fib6_table *table,
2501b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2502b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2503b55b76b2SDuan Jiong 					     int flags)
2504b55b76b2SDuan Jiong {
2505b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
25060b34eb00SDavid Ahern 	struct rt6_info *ret = NULL;
25079b6b35abSDavid Ahern 	struct fib6_result res = {};
25088d1c802bSDavid Ahern 	struct fib6_info *rt;
2509b55b76b2SDuan Jiong 	struct fib6_node *fn;
2510b55b76b2SDuan Jiong 
2511b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
251267c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2513b55b76b2SDuan Jiong 	 *
2514b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2515b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2516b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2517b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2518b55b76b2SDuan Jiong 	 * routes.
2519b55b76b2SDuan Jiong 	 */
2520b55b76b2SDuan Jiong 
252166f5d6ceSWei Wang 	rcu_read_lock();
25226454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2523b55b76b2SDuan Jiong restart:
252466f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
25259b6b35abSDavid Ahern 		res.f6i = rt;
25269b6b35abSDavid Ahern 		res.nh = &rt->fib6_nh;
25279b6b35abSDavid Ahern 
252814895687SDavid Ahern 		if (fib6_check_expired(rt))
2529b55b76b2SDuan Jiong 			continue;
253093c2fb25SDavid Ahern 		if (rt->fib6_flags & RTF_REJECT)
2531b55b76b2SDuan Jiong 			break;
25329b6b35abSDavid Ahern 		if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, &ret))
25330b34eb00SDavid Ahern 			goto out;
2534b55b76b2SDuan Jiong 	}
2535b55b76b2SDuan Jiong 
2536b55b76b2SDuan Jiong 	if (!rt)
2537421842edSDavid Ahern 		rt = net->ipv6.fib6_null_entry;
253893c2fb25SDavid Ahern 	else if (rt->fib6_flags & RTF_REJECT) {
253923fb93a4SDavid Ahern 		ret = net->ipv6.ip6_null_entry;
2540b0a1ba59SMartin KaFai Lau 		goto out;
2541b0a1ba59SMartin KaFai Lau 	}
2542b0a1ba59SMartin KaFai Lau 
2543421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
2544a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2545a3c00e46SMartin KaFai Lau 		if (fn)
2546a3c00e46SMartin KaFai Lau 			goto restart;
2547b55b76b2SDuan Jiong 	}
2548a3c00e46SMartin KaFai Lau 
25499b6b35abSDavid Ahern 	res.f6i = rt;
25509b6b35abSDavid Ahern 	res.nh = &rt->fib6_nh;
2551b0a1ba59SMartin KaFai Lau out:
25527d21fec9SDavid Ahern 	if (ret) {
255310585b43SDavid Ahern 		ip6_hold_safe(net, &ret);
25547d21fec9SDavid Ahern 	} else {
25557d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
25567d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
25579b6b35abSDavid Ahern 		ret = ip6_create_rt_rcu(&res);
25587d21fec9SDavid Ahern 	}
2559b55b76b2SDuan Jiong 
256066f5d6ceSWei Wang 	rcu_read_unlock();
2561b55b76b2SDuan Jiong 
25628ff2e5b2SDavid Ahern 	trace_fib6_table_lookup(net, &res, table, fl6);
256323fb93a4SDavid Ahern 	return ret;
2564b55b76b2SDuan Jiong };
2565b55b76b2SDuan Jiong 
2566b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
2567b55b76b2SDuan Jiong 					    const struct flowi6 *fl6,
2568b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2569b55b76b2SDuan Jiong 					    const struct in6_addr *gateway)
2570b55b76b2SDuan Jiong {
2571b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2572b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
2573b55b76b2SDuan Jiong 
2574b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
2575b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
2576b55b76b2SDuan Jiong 
2577b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, &rdfl.fl6, skb,
2578b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
2579b55b76b2SDuan Jiong }
2580b55b76b2SDuan Jiong 
2581e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2582e2d118a1SLorenzo Colitti 		  kuid_t uid)
25833a5ad2eeSDavid S. Miller {
25843a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
25853a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
25861f7f10acSMaciej Żenczykowski 	struct flowi6 fl6 = {
25871f7f10acSMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
25881f7f10acSMaciej Żenczykowski 		.flowi6_oif = oif,
25891f7f10acSMaciej Żenczykowski 		.flowi6_mark = mark,
25901f7f10acSMaciej Żenczykowski 		.daddr = iph->daddr,
25911f7f10acSMaciej Żenczykowski 		.saddr = iph->saddr,
25921f7f10acSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
25931f7f10acSMaciej Żenczykowski 		.flowi6_uid = uid,
25941f7f10acSMaciej Żenczykowski 	};
25953a5ad2eeSDavid S. Miller 
2596b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
25976700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
25983a5ad2eeSDavid S. Miller 	dst_release(dst);
25993a5ad2eeSDavid S. Miller }
26003a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
26013a5ad2eeSDavid S. Miller 
2602d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
2603c92a59ecSDuan Jiong {
2604c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2605c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2606c92a59ecSDuan Jiong 	struct dst_entry *dst;
26070b26fb17SMaciej Żenczykowski 	struct flowi6 fl6 = {
26080b26fb17SMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
26090b26fb17SMaciej Żenczykowski 		.flowi6_oif = oif,
26100b26fb17SMaciej Żenczykowski 		.daddr = msg->dest,
26110b26fb17SMaciej Żenczykowski 		.saddr = iph->daddr,
26120b26fb17SMaciej Żenczykowski 		.flowi6_uid = sock_net_uid(net, NULL),
26130b26fb17SMaciej Żenczykowski 	};
2614c92a59ecSDuan Jiong 
2615b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
2616c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
2617c92a59ecSDuan Jiong 	dst_release(dst);
2618c92a59ecSDuan Jiong }
2619c92a59ecSDuan Jiong 
26203a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
26213a5ad2eeSDavid S. Miller {
2622e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2623e2d118a1SLorenzo Colitti 		     sk->sk_uid);
26243a5ad2eeSDavid S. Miller }
26253a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
26263a5ad2eeSDavid S. Miller 
26270dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
26281da177e4SLinus Torvalds {
26290dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
26300dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
26310dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
26320dbaee3bSDavid S. Miller 
26331da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
26341da177e4SLinus Torvalds 
26355578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
26365578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
26371da177e4SLinus Torvalds 
26381da177e4SLinus Torvalds 	/*
26391da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
26401da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
26411da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
26421da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
26431da177e4SLinus Torvalds 	 */
26441da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
26451da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
26461da177e4SLinus Torvalds 	return mtu;
26471da177e4SLinus Torvalds }
26481da177e4SLinus Torvalds 
2649ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
2650d33e4553SDavid S. Miller {
2651d33e4553SDavid S. Miller 	struct inet6_dev *idev;
2652d4ead6b3SDavid Ahern 	unsigned int mtu;
2653618f9bc7SSteffen Klassert 
26544b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
26554b32b5adSMartin KaFai Lau 	if (mtu)
26564b32b5adSMartin KaFai Lau 		goto out;
26574b32b5adSMartin KaFai Lau 
2658618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
2659d33e4553SDavid S. Miller 
2660d33e4553SDavid S. Miller 	rcu_read_lock();
2661d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
2662d33e4553SDavid S. Miller 	if (idev)
2663d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
2664d33e4553SDavid S. Miller 	rcu_read_unlock();
2665d33e4553SDavid S. Miller 
266630f78d8eSEric Dumazet out:
266714972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
266814972cbdSRoopa Prabhu 
266914972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
2670d33e4553SDavid S. Miller }
2671d33e4553SDavid S. Miller 
2672901731b8SDavid Ahern /* MTU selection:
2673901731b8SDavid Ahern  * 1. mtu on route is locked - use it
2674901731b8SDavid Ahern  * 2. mtu from nexthop exception
2675901731b8SDavid Ahern  * 3. mtu from egress device
2676901731b8SDavid Ahern  *
2677901731b8SDavid Ahern  * based on ip6_dst_mtu_forward and exception logic of
2678901731b8SDavid Ahern  * rt6_find_cached_rt; called with rcu_read_lock
2679901731b8SDavid Ahern  */
2680b748f260SDavid Ahern u32 ip6_mtu_from_fib6(const struct fib6_result *res,
2681b748f260SDavid Ahern 		      const struct in6_addr *daddr,
2682b748f260SDavid Ahern 		      const struct in6_addr *saddr)
2683901731b8SDavid Ahern {
2684b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
2685b748f260SDavid Ahern 	struct fib6_info *f6i = res->f6i;
2686901731b8SDavid Ahern 	struct inet6_dev *idev;
2687510e2cedSWei Wang 	struct rt6_info *rt;
2688901731b8SDavid Ahern 	u32 mtu = 0;
2689901731b8SDavid Ahern 
2690901731b8SDavid Ahern 	if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
2691901731b8SDavid Ahern 		mtu = f6i->fib6_pmtu;
2692901731b8SDavid Ahern 		if (mtu)
2693901731b8SDavid Ahern 			goto out;
2694901731b8SDavid Ahern 	}
2695901731b8SDavid Ahern 
2696510e2cedSWei Wang 	rt = rt6_find_cached_rt(res, daddr, saddr);
2697510e2cedSWei Wang 	if (unlikely(rt)) {
2698510e2cedSWei Wang 		mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
2699510e2cedSWei Wang 	} else {
2700b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
2701901731b8SDavid Ahern 
2702901731b8SDavid Ahern 		mtu = IPV6_MIN_MTU;
2703901731b8SDavid Ahern 		idev = __in6_dev_get(dev);
2704901731b8SDavid Ahern 		if (idev && idev->cnf.mtu6 > mtu)
2705901731b8SDavid Ahern 			mtu = idev->cnf.mtu6;
2706901731b8SDavid Ahern 	}
2707901731b8SDavid Ahern 
2708901731b8SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2709901731b8SDavid Ahern out:
2710b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
2711901731b8SDavid Ahern }
2712901731b8SDavid Ahern 
27133b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
271487a11578SDavid S. Miller 				  struct flowi6 *fl6)
27151da177e4SLinus Torvalds {
271687a11578SDavid S. Miller 	struct dst_entry *dst;
27171da177e4SLinus Torvalds 	struct rt6_info *rt;
27181da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
2719c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
27201da177e4SLinus Torvalds 
272138308473SDavid S. Miller 	if (unlikely(!idev))
2722122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
27231da177e4SLinus Torvalds 
2724ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
272538308473SDavid S. Miller 	if (unlikely(!rt)) {
27261da177e4SLinus Torvalds 		in6_dev_put(idev);
272787a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
27281da177e4SLinus Torvalds 		goto out;
27291da177e4SLinus Torvalds 	}
27301da177e4SLinus Torvalds 
27318e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
2732588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
27338e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
2734550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
273587a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
27368e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
27378e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
273814edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
27391da177e4SLinus Torvalds 
27404c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
2741587fea74SWei Wang 	 * do proper release of the net_device
2742587fea74SWei Wang 	 */
2743587fea74SWei Wang 	rt6_uncached_list_add(rt);
274481eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
27451da177e4SLinus Torvalds 
274687a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
274787a11578SDavid S. Miller 
27481da177e4SLinus Torvalds out:
274987a11578SDavid S. Miller 	return dst;
27501da177e4SLinus Torvalds }
27511da177e4SLinus Torvalds 
2752569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
27531da177e4SLinus Torvalds {
275486393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
27557019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
27567019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
27577019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
27587019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
27597019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
2760fc66f95cSEric Dumazet 	int entries;
27611da177e4SLinus Torvalds 
2762fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
276349a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
2764fc66f95cSEric Dumazet 	    entries <= rt_max_size)
27651da177e4SLinus Torvalds 		goto out;
27661da177e4SLinus Torvalds 
27676891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
276814956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
2769fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
2770fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
27717019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
27721da177e4SLinus Torvalds out:
27737019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
2774fc66f95cSEric Dumazet 	return entries > rt_max_size;
27751da177e4SLinus Torvalds }
27761da177e4SLinus Torvalds 
27778c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net,
27788c14586fSDavid Ahern 					    struct fib6_config *cfg,
2779f4797b33SDavid Ahern 					    const struct in6_addr *gw_addr,
2780f4797b33SDavid Ahern 					    u32 tbid, int flags)
27818c14586fSDavid Ahern {
27828c14586fSDavid Ahern 	struct flowi6 fl6 = {
27838c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
27848c14586fSDavid Ahern 		.daddr = *gw_addr,
27858c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
27868c14586fSDavid Ahern 	};
27878c14586fSDavid Ahern 	struct fib6_table *table;
27888c14586fSDavid Ahern 	struct rt6_info *rt;
27898c14586fSDavid Ahern 
2790f4797b33SDavid Ahern 	table = fib6_get_table(net, tbid);
27918c14586fSDavid Ahern 	if (!table)
27928c14586fSDavid Ahern 		return NULL;
27938c14586fSDavid Ahern 
27948c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
27958c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
27968c14586fSDavid Ahern 
2797f4797b33SDavid Ahern 	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
2798b75cc8f9SDavid Ahern 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
27998c14586fSDavid Ahern 
28008c14586fSDavid Ahern 	/* if table lookup failed, fall back to full lookup */
28018c14586fSDavid Ahern 	if (rt == net->ipv6.ip6_null_entry) {
28028c14586fSDavid Ahern 		ip6_rt_put(rt);
28038c14586fSDavid Ahern 		rt = NULL;
28048c14586fSDavid Ahern 	}
28058c14586fSDavid Ahern 
28068c14586fSDavid Ahern 	return rt;
28078c14586fSDavid Ahern }
28088c14586fSDavid Ahern 
2809fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net,
2810fc1e64e1SDavid Ahern 				     struct fib6_config *cfg,
28119fbb704cSDavid Ahern 				     const struct net_device *dev,
2812fc1e64e1SDavid Ahern 				     struct netlink_ext_ack *extack)
2813fc1e64e1SDavid Ahern {
281444750f84SDavid Ahern 	u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
2815fc1e64e1SDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
2816fc1e64e1SDavid Ahern 	u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2817bf1dc8baSPaolo Abeni 	struct fib6_info *from;
2818fc1e64e1SDavid Ahern 	struct rt6_info *grt;
2819fc1e64e1SDavid Ahern 	int err;
2820fc1e64e1SDavid Ahern 
2821fc1e64e1SDavid Ahern 	err = 0;
2822fc1e64e1SDavid Ahern 	grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2823fc1e64e1SDavid Ahern 	if (grt) {
2824bf1dc8baSPaolo Abeni 		rcu_read_lock();
2825bf1dc8baSPaolo Abeni 		from = rcu_dereference(grt->from);
282658e354c0SDavid Ahern 		if (!grt->dst.error &&
28274ed591c8SDavid Ahern 		    /* ignore match if it is the default route */
2828bf1dc8baSPaolo Abeni 		    from && !ipv6_addr_any(&from->fib6_dst.addr) &&
282958e354c0SDavid Ahern 		    (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
283044750f84SDavid Ahern 			NL_SET_ERR_MSG(extack,
283144750f84SDavid Ahern 				       "Nexthop has invalid gateway or device mismatch");
2832fc1e64e1SDavid Ahern 			err = -EINVAL;
2833fc1e64e1SDavid Ahern 		}
2834bf1dc8baSPaolo Abeni 		rcu_read_unlock();
2835fc1e64e1SDavid Ahern 
2836fc1e64e1SDavid Ahern 		ip6_rt_put(grt);
2837fc1e64e1SDavid Ahern 	}
2838fc1e64e1SDavid Ahern 
2839fc1e64e1SDavid Ahern 	return err;
2840fc1e64e1SDavid Ahern }
2841fc1e64e1SDavid Ahern 
28421edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net,
28431edce99fSDavid Ahern 			      struct fib6_config *cfg,
28441edce99fSDavid Ahern 			      struct net_device **_dev,
28451edce99fSDavid Ahern 			      struct inet6_dev **idev)
28461edce99fSDavid Ahern {
28471edce99fSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
28481edce99fSDavid Ahern 	struct net_device *dev = _dev ? *_dev : NULL;
28491edce99fSDavid Ahern 	struct rt6_info *grt = NULL;
28501edce99fSDavid Ahern 	int err = -EHOSTUNREACH;
28511edce99fSDavid Ahern 
28521edce99fSDavid Ahern 	if (cfg->fc_table) {
2853f4797b33SDavid Ahern 		int flags = RT6_LOOKUP_F_IFACE;
2854f4797b33SDavid Ahern 
2855f4797b33SDavid Ahern 		grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2856f4797b33SDavid Ahern 					  cfg->fc_table, flags);
28571edce99fSDavid Ahern 		if (grt) {
28581edce99fSDavid Ahern 			if (grt->rt6i_flags & RTF_GATEWAY ||
28591edce99fSDavid Ahern 			    (dev && dev != grt->dst.dev)) {
28601edce99fSDavid Ahern 				ip6_rt_put(grt);
28611edce99fSDavid Ahern 				grt = NULL;
28621edce99fSDavid Ahern 			}
28631edce99fSDavid Ahern 		}
28641edce99fSDavid Ahern 	}
28651edce99fSDavid Ahern 
28661edce99fSDavid Ahern 	if (!grt)
2867b75cc8f9SDavid Ahern 		grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
28681edce99fSDavid Ahern 
28691edce99fSDavid Ahern 	if (!grt)
28701edce99fSDavid Ahern 		goto out;
28711edce99fSDavid Ahern 
28721edce99fSDavid Ahern 	if (dev) {
28731edce99fSDavid Ahern 		if (dev != grt->dst.dev) {
28741edce99fSDavid Ahern 			ip6_rt_put(grt);
28751edce99fSDavid Ahern 			goto out;
28761edce99fSDavid Ahern 		}
28771edce99fSDavid Ahern 	} else {
28781edce99fSDavid Ahern 		*_dev = dev = grt->dst.dev;
28791edce99fSDavid Ahern 		*idev = grt->rt6i_idev;
28801edce99fSDavid Ahern 		dev_hold(dev);
28811edce99fSDavid Ahern 		in6_dev_hold(grt->rt6i_idev);
28821edce99fSDavid Ahern 	}
28831edce99fSDavid Ahern 
28841edce99fSDavid Ahern 	if (!(grt->rt6i_flags & RTF_GATEWAY))
28851edce99fSDavid Ahern 		err = 0;
28861edce99fSDavid Ahern 
28871edce99fSDavid Ahern 	ip6_rt_put(grt);
28881edce99fSDavid Ahern 
28891edce99fSDavid Ahern out:
28901edce99fSDavid Ahern 	return err;
28911edce99fSDavid Ahern }
28921edce99fSDavid Ahern 
28939fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
28949fbb704cSDavid Ahern 			   struct net_device **_dev, struct inet6_dev **idev,
28959fbb704cSDavid Ahern 			   struct netlink_ext_ack *extack)
28969fbb704cSDavid Ahern {
28979fbb704cSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
28989fbb704cSDavid Ahern 	int gwa_type = ipv6_addr_type(gw_addr);
2899232378e8SDavid Ahern 	bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
29009fbb704cSDavid Ahern 	const struct net_device *dev = *_dev;
2901232378e8SDavid Ahern 	bool need_addr_check = !dev;
29029fbb704cSDavid Ahern 	int err = -EINVAL;
29039fbb704cSDavid Ahern 
29049fbb704cSDavid Ahern 	/* if gw_addr is local we will fail to detect this in case
29059fbb704cSDavid Ahern 	 * address is still TENTATIVE (DAD in progress). rt6_lookup()
29069fbb704cSDavid Ahern 	 * will return already-added prefix route via interface that
29079fbb704cSDavid Ahern 	 * prefix route was assigned to, which might be non-loopback.
29089fbb704cSDavid Ahern 	 */
2909232378e8SDavid Ahern 	if (dev &&
2910232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2911232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
29129fbb704cSDavid Ahern 		goto out;
29139fbb704cSDavid Ahern 	}
29149fbb704cSDavid Ahern 
29159fbb704cSDavid Ahern 	if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
29169fbb704cSDavid Ahern 		/* IPv6 strictly inhibits using not link-local
29179fbb704cSDavid Ahern 		 * addresses as nexthop address.
29189fbb704cSDavid Ahern 		 * Otherwise, router will not able to send redirects.
29199fbb704cSDavid Ahern 		 * It is very good, but in some (rare!) circumstances
29209fbb704cSDavid Ahern 		 * (SIT, PtP, NBMA NOARP links) it is handy to allow
29219fbb704cSDavid Ahern 		 * some exceptions. --ANK
29229fbb704cSDavid Ahern 		 * We allow IPv4-mapped nexthops to support RFC4798-type
29239fbb704cSDavid Ahern 		 * addressing
29249fbb704cSDavid Ahern 		 */
29259fbb704cSDavid Ahern 		if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
29269fbb704cSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
29279fbb704cSDavid Ahern 			goto out;
29289fbb704cSDavid Ahern 		}
29299fbb704cSDavid Ahern 
29309fbb704cSDavid Ahern 		if (cfg->fc_flags & RTNH_F_ONLINK)
29319fbb704cSDavid Ahern 			err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
29329fbb704cSDavid Ahern 		else
29339fbb704cSDavid Ahern 			err = ip6_route_check_nh(net, cfg, _dev, idev);
29349fbb704cSDavid Ahern 
29359fbb704cSDavid Ahern 		if (err)
29369fbb704cSDavid Ahern 			goto out;
29379fbb704cSDavid Ahern 	}
29389fbb704cSDavid Ahern 
29399fbb704cSDavid Ahern 	/* reload in case device was changed */
29409fbb704cSDavid Ahern 	dev = *_dev;
29419fbb704cSDavid Ahern 
29429fbb704cSDavid Ahern 	err = -EINVAL;
29439fbb704cSDavid Ahern 	if (!dev) {
29449fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Egress device not specified");
29459fbb704cSDavid Ahern 		goto out;
29469fbb704cSDavid Ahern 	} else if (dev->flags & IFF_LOOPBACK) {
29479fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack,
29489fbb704cSDavid Ahern 			       "Egress device can not be loopback device for this route");
29499fbb704cSDavid Ahern 		goto out;
29509fbb704cSDavid Ahern 	}
2951232378e8SDavid Ahern 
2952232378e8SDavid Ahern 	/* if we did not check gw_addr above, do so now that the
2953232378e8SDavid Ahern 	 * egress device has been resolved.
2954232378e8SDavid Ahern 	 */
2955232378e8SDavid Ahern 	if (need_addr_check &&
2956232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2957232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
2958232378e8SDavid Ahern 		goto out;
2959232378e8SDavid Ahern 	}
2960232378e8SDavid Ahern 
29619fbb704cSDavid Ahern 	err = 0;
29629fbb704cSDavid Ahern out:
29639fbb704cSDavid Ahern 	return err;
29649fbb704cSDavid Ahern }
29659fbb704cSDavid Ahern 
296683c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
296783c44251SDavid Ahern {
296883c44251SDavid Ahern 	if ((flags & RTF_REJECT) ||
296983c44251SDavid Ahern 	    (dev && (dev->flags & IFF_LOOPBACK) &&
297083c44251SDavid Ahern 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
297183c44251SDavid Ahern 	     !(flags & RTF_LOCAL)))
297283c44251SDavid Ahern 		return true;
297383c44251SDavid Ahern 
297483c44251SDavid Ahern 	return false;
297583c44251SDavid Ahern }
297683c44251SDavid Ahern 
297783c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
297883c44251SDavid Ahern 		 struct fib6_config *cfg, gfp_t gfp_flags,
297983c44251SDavid Ahern 		 struct netlink_ext_ack *extack)
298083c44251SDavid Ahern {
298183c44251SDavid Ahern 	struct net_device *dev = NULL;
298283c44251SDavid Ahern 	struct inet6_dev *idev = NULL;
298383c44251SDavid Ahern 	int addr_type;
298483c44251SDavid Ahern 	int err;
298583c44251SDavid Ahern 
2986f1741730SDavid Ahern 	fib6_nh->fib_nh_family = AF_INET6;
2987f1741730SDavid Ahern 
298883c44251SDavid Ahern 	err = -ENODEV;
298983c44251SDavid Ahern 	if (cfg->fc_ifindex) {
299083c44251SDavid Ahern 		dev = dev_get_by_index(net, cfg->fc_ifindex);
299183c44251SDavid Ahern 		if (!dev)
299283c44251SDavid Ahern 			goto out;
299383c44251SDavid Ahern 		idev = in6_dev_get(dev);
299483c44251SDavid Ahern 		if (!idev)
299583c44251SDavid Ahern 			goto out;
299683c44251SDavid Ahern 	}
299783c44251SDavid Ahern 
299883c44251SDavid Ahern 	if (cfg->fc_flags & RTNH_F_ONLINK) {
299983c44251SDavid Ahern 		if (!dev) {
300083c44251SDavid Ahern 			NL_SET_ERR_MSG(extack,
300183c44251SDavid Ahern 				       "Nexthop device required for onlink");
300283c44251SDavid Ahern 			goto out;
300383c44251SDavid Ahern 		}
300483c44251SDavid Ahern 
300583c44251SDavid Ahern 		if (!(dev->flags & IFF_UP)) {
300683c44251SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
300783c44251SDavid Ahern 			err = -ENETDOWN;
300883c44251SDavid Ahern 			goto out;
300983c44251SDavid Ahern 		}
301083c44251SDavid Ahern 
3011ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
301283c44251SDavid Ahern 	}
301383c44251SDavid Ahern 
3014ad1601aeSDavid Ahern 	fib6_nh->fib_nh_weight = 1;
301583c44251SDavid Ahern 
301683c44251SDavid Ahern 	/* We cannot add true routes via loopback here,
301783c44251SDavid Ahern 	 * they would result in kernel looping; promote them to reject routes
301883c44251SDavid Ahern 	 */
301983c44251SDavid Ahern 	addr_type = ipv6_addr_type(&cfg->fc_dst);
302083c44251SDavid Ahern 	if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
302183c44251SDavid Ahern 		/* hold loopback dev/idev if we haven't done so. */
302283c44251SDavid Ahern 		if (dev != net->loopback_dev) {
302383c44251SDavid Ahern 			if (dev) {
302483c44251SDavid Ahern 				dev_put(dev);
302583c44251SDavid Ahern 				in6_dev_put(idev);
302683c44251SDavid Ahern 			}
302783c44251SDavid Ahern 			dev = net->loopback_dev;
302883c44251SDavid Ahern 			dev_hold(dev);
302983c44251SDavid Ahern 			idev = in6_dev_get(dev);
303083c44251SDavid Ahern 			if (!idev) {
303183c44251SDavid Ahern 				err = -ENODEV;
303283c44251SDavid Ahern 				goto out;
303383c44251SDavid Ahern 			}
303483c44251SDavid Ahern 		}
303583c44251SDavid Ahern 		goto set_dev;
303683c44251SDavid Ahern 	}
303783c44251SDavid Ahern 
303883c44251SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY) {
303983c44251SDavid Ahern 		err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
304083c44251SDavid Ahern 		if (err)
304183c44251SDavid Ahern 			goto out;
304283c44251SDavid Ahern 
3043ad1601aeSDavid Ahern 		fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3044bdf00467SDavid Ahern 		fib6_nh->fib_nh_gw_family = AF_INET6;
304583c44251SDavid Ahern 	}
304683c44251SDavid Ahern 
304783c44251SDavid Ahern 	err = -ENODEV;
304883c44251SDavid Ahern 	if (!dev)
304983c44251SDavid Ahern 		goto out;
305083c44251SDavid Ahern 
305183c44251SDavid Ahern 	if (idev->cnf.disable_ipv6) {
305283c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
305383c44251SDavid Ahern 		err = -EACCES;
305483c44251SDavid Ahern 		goto out;
305583c44251SDavid Ahern 	}
305683c44251SDavid Ahern 
305783c44251SDavid Ahern 	if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
305883c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
305983c44251SDavid Ahern 		err = -ENETDOWN;
306083c44251SDavid Ahern 		goto out;
306183c44251SDavid Ahern 	}
306283c44251SDavid Ahern 
306383c44251SDavid Ahern 	if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
306483c44251SDavid Ahern 	    !netif_carrier_ok(dev))
3065ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
306683c44251SDavid Ahern 
3067979e276eSDavid Ahern 	err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
3068979e276eSDavid Ahern 				 cfg->fc_encap_type, cfg, gfp_flags, extack);
3069979e276eSDavid Ahern 	if (err)
3070979e276eSDavid Ahern 		goto out;
307183c44251SDavid Ahern set_dev:
3072ad1601aeSDavid Ahern 	fib6_nh->fib_nh_dev = dev;
3073f1741730SDavid Ahern 	fib6_nh->fib_nh_oif = dev->ifindex;
307483c44251SDavid Ahern 	err = 0;
307583c44251SDavid Ahern out:
307683c44251SDavid Ahern 	if (idev)
307783c44251SDavid Ahern 		in6_dev_put(idev);
307883c44251SDavid Ahern 
307983c44251SDavid Ahern 	if (err) {
3080ad1601aeSDavid Ahern 		lwtstate_put(fib6_nh->fib_nh_lws);
3081ad1601aeSDavid Ahern 		fib6_nh->fib_nh_lws = NULL;
308283c44251SDavid Ahern 		if (dev)
308383c44251SDavid Ahern 			dev_put(dev);
308483c44251SDavid Ahern 	}
308583c44251SDavid Ahern 
308683c44251SDavid Ahern 	return err;
308783c44251SDavid Ahern }
308883c44251SDavid Ahern 
3089dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh)
3090dac7d0f2SDavid Ahern {
3091979e276eSDavid Ahern 	fib_nh_common_release(&fib6_nh->nh_common);
3092dac7d0f2SDavid Ahern }
3093dac7d0f2SDavid Ahern 
30948d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3095acb54e3cSDavid Ahern 					      gfp_t gfp_flags,
3096333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
30971da177e4SLinus Torvalds {
30985578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
30998d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3100c71099acSThomas Graf 	struct fib6_table *table;
31018c5b83f0SRoopa Prabhu 	int err = -EINVAL;
310283c44251SDavid Ahern 	int addr_type;
31031da177e4SLinus Torvalds 
3104557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
3105d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
3106d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3107557c44beSDavid Ahern 		goto out;
3108d5d531cbSDavid Ahern 	}
3109557c44beSDavid Ahern 
31102ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
31112ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
31122ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
31132ea2352eSWei Wang 		goto out;
31142ea2352eSWei Wang 	}
31152ea2352eSWei Wang 
3116e8478e80SDavid Ahern 	if (cfg->fc_type > RTN_MAX) {
3117e8478e80SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid route type");
3118e8478e80SDavid Ahern 		goto out;
3119e8478e80SDavid Ahern 	}
3120e8478e80SDavid Ahern 
3121d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
3122d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
31238c5b83f0SRoopa Prabhu 		goto out;
3124d5d531cbSDavid Ahern 	}
3125d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
3126d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
3127d5d531cbSDavid Ahern 		goto out;
3128d5d531cbSDavid Ahern 	}
31291da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
3130d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
3131d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
3132d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
31338c5b83f0SRoopa Prabhu 		goto out;
3134d5d531cbSDavid Ahern 	}
31351da177e4SLinus Torvalds #endif
3136fc1e64e1SDavid Ahern 
3137c71099acSThomas Graf 	err = -ENOBUFS;
313838308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
3139d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3140d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
314138308473SDavid S. Miller 		if (!table) {
3142f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3143d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
3144d71314b4SMatti Vaittinen 		}
3145d71314b4SMatti Vaittinen 	} else {
3146d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
3147d71314b4SMatti Vaittinen 	}
314838308473SDavid S. Miller 
314938308473SDavid S. Miller 	if (!table)
3150c71099acSThomas Graf 		goto out;
3151c71099acSThomas Graf 
31521da177e4SLinus Torvalds 	err = -ENOMEM;
315393531c67SDavid Ahern 	rt = fib6_info_alloc(gfp_flags);
315493531c67SDavid Ahern 	if (!rt)
31551da177e4SLinus Torvalds 		goto out;
315693531c67SDavid Ahern 
3157d7e774f3SDavid Ahern 	rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3158d7e774f3SDavid Ahern 					       extack);
3159767a2217SDavid Ahern 	if (IS_ERR(rt->fib6_metrics)) {
3160767a2217SDavid Ahern 		err = PTR_ERR(rt->fib6_metrics);
3161fda21d46SEric Dumazet 		/* Do not leave garbage there. */
3162fda21d46SEric Dumazet 		rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3163767a2217SDavid Ahern 		goto out;
3164767a2217SDavid Ahern 	}
3165767a2217SDavid Ahern 
316693531c67SDavid Ahern 	if (cfg->fc_flags & RTF_ADDRCONF)
316793531c67SDavid Ahern 		rt->dst_nocount = true;
31681da177e4SLinus Torvalds 
31691716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
317014895687SDavid Ahern 		fib6_set_expires(rt, jiffies +
31711716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
31721716a961SGao feng 	else
317314895687SDavid Ahern 		fib6_clean_expires(rt);
31741da177e4SLinus Torvalds 
317586872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
317686872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
317793c2fb25SDavid Ahern 	rt->fib6_protocol = cfg->fc_protocol;
317886872cb5SThomas Graf 
317983c44251SDavid Ahern 	rt->fib6_table = table;
318083c44251SDavid Ahern 	rt->fib6_metric = cfg->fc_metric;
318183c44251SDavid Ahern 	rt->fib6_type = cfg->fc_type;
31822b2450caSDavid Ahern 	rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
318319e42e45SRoopa Prabhu 
318493c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
318593c2fb25SDavid Ahern 	rt->fib6_dst.plen = cfg->fc_dst_len;
318693c2fb25SDavid Ahern 	if (rt->fib6_dst.plen == 128)
31873b6761d1SDavid Ahern 		rt->dst_host = true;
31881da177e4SLinus Torvalds 
31891da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
319093c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
319193c2fb25SDavid Ahern 	rt->fib6_src.plen = cfg->fc_src_len;
31921da177e4SLinus Torvalds #endif
319383c44251SDavid Ahern 	err = fib6_nh_init(net, &rt->fib6_nh, cfg, gfp_flags, extack);
31941da177e4SLinus Torvalds 	if (err)
31951da177e4SLinus Torvalds 		goto out;
31969fbb704cSDavid Ahern 
319783c44251SDavid Ahern 	/* We cannot add true routes via loopback here,
319883c44251SDavid Ahern 	 * they would result in kernel looping; promote them to reject routes
319983c44251SDavid Ahern 	 */
320083c44251SDavid Ahern 	addr_type = ipv6_addr_type(&cfg->fc_dst);
3201ad1601aeSDavid Ahern 	if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh.fib_nh_dev, addr_type))
320283c44251SDavid Ahern 		rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3203955ec4cbSDavid Ahern 
3204c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
320583c44251SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
320683c44251SDavid Ahern 
3207c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3208d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
3209c3968a85SDaniel Walter 			err = -EINVAL;
3210c3968a85SDaniel Walter 			goto out;
3211c3968a85SDaniel Walter 		}
321293c2fb25SDavid Ahern 		rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
321393c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 128;
3214c3968a85SDaniel Walter 	} else
321593c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
3216c3968a85SDaniel Walter 
32178c5b83f0SRoopa Prabhu 	return rt;
32181da177e4SLinus Torvalds out:
321993531c67SDavid Ahern 	fib6_info_release(rt);
32208c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
32216b9ea5a6SRoopa Prabhu }
32226b9ea5a6SRoopa Prabhu 
3223acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3224333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
32256b9ea5a6SRoopa Prabhu {
32268d1c802bSDavid Ahern 	struct fib6_info *rt;
32276b9ea5a6SRoopa Prabhu 	int err;
32286b9ea5a6SRoopa Prabhu 
3229acb54e3cSDavid Ahern 	rt = ip6_route_info_create(cfg, gfp_flags, extack);
3230d4ead6b3SDavid Ahern 	if (IS_ERR(rt))
3231d4ead6b3SDavid Ahern 		return PTR_ERR(rt);
32326b9ea5a6SRoopa Prabhu 
3233d4ead6b3SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
323493531c67SDavid Ahern 	fib6_info_release(rt);
32356b9ea5a6SRoopa Prabhu 
32361da177e4SLinus Torvalds 	return err;
32371da177e4SLinus Torvalds }
32381da177e4SLinus Torvalds 
32398d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
32401da177e4SLinus Torvalds {
3241afb1d4b5SDavid Ahern 	struct net *net = info->nl_net;
3242c71099acSThomas Graf 	struct fib6_table *table;
3243afb1d4b5SDavid Ahern 	int err;
32441da177e4SLinus Torvalds 
3245421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
32466825a26cSGao feng 		err = -ENOENT;
32476825a26cSGao feng 		goto out;
32486825a26cSGao feng 	}
32496c813a72SPatrick McHardy 
325093c2fb25SDavid Ahern 	table = rt->fib6_table;
325166f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
325286872cb5SThomas Graf 	err = fib6_del(rt, info);
325366f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
32541da177e4SLinus Torvalds 
32556825a26cSGao feng out:
325693531c67SDavid Ahern 	fib6_info_release(rt);
32571da177e4SLinus Torvalds 	return err;
32581da177e4SLinus Torvalds }
32591da177e4SLinus Torvalds 
32608d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt)
3261e0a1ad73SThomas Graf {
3262afb1d4b5SDavid Ahern 	struct nl_info info = { .nl_net = net };
3263afb1d4b5SDavid Ahern 
3264528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
3265e0a1ad73SThomas Graf }
3266e0a1ad73SThomas Graf 
32678d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
32680ae81335SDavid Ahern {
32690ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
3270e3330039SWANG Cong 	struct net *net = info->nl_net;
327116a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
32720ae81335SDavid Ahern 	struct fib6_table *table;
3273e3330039SWANG Cong 	int err = -ENOENT;
32740ae81335SDavid Ahern 
3275421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
3276e3330039SWANG Cong 		goto out_put;
327793c2fb25SDavid Ahern 	table = rt->fib6_table;
327866f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
32790ae81335SDavid Ahern 
328093c2fb25SDavid Ahern 	if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
32818d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
32820ae81335SDavid Ahern 
328316a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
328416a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
328516a16cd3SDavid Ahern 		if (skb) {
328616a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
328716a16cd3SDavid Ahern 
3288d4ead6b3SDavid Ahern 			if (rt6_fill_node(net, skb, rt, NULL,
328916a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
329016a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
329116a16cd3SDavid Ahern 				kfree_skb(skb);
329216a16cd3SDavid Ahern 				skb = NULL;
329316a16cd3SDavid Ahern 			} else
329416a16cd3SDavid Ahern 				info->skip_notify = 1;
329516a16cd3SDavid Ahern 		}
329616a16cd3SDavid Ahern 
32970ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
329893c2fb25SDavid Ahern 					 &rt->fib6_siblings,
329993c2fb25SDavid Ahern 					 fib6_siblings) {
33000ae81335SDavid Ahern 			err = fib6_del(sibling, info);
33010ae81335SDavid Ahern 			if (err)
3302e3330039SWANG Cong 				goto out_unlock;
33030ae81335SDavid Ahern 		}
33040ae81335SDavid Ahern 	}
33050ae81335SDavid Ahern 
33060ae81335SDavid Ahern 	err = fib6_del(rt, info);
3307e3330039SWANG Cong out_unlock:
330866f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
3309e3330039SWANG Cong out_put:
331093531c67SDavid Ahern 	fib6_info_release(rt);
331116a16cd3SDavid Ahern 
331216a16cd3SDavid Ahern 	if (skb) {
3313e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
331416a16cd3SDavid Ahern 			    info->nlh, gfp_any());
331516a16cd3SDavid Ahern 	}
33160ae81335SDavid Ahern 	return err;
33170ae81335SDavid Ahern }
33180ae81335SDavid Ahern 
331923fb93a4SDavid Ahern static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
332023fb93a4SDavid Ahern {
332123fb93a4SDavid Ahern 	int rc = -ESRCH;
332223fb93a4SDavid Ahern 
332323fb93a4SDavid Ahern 	if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
332423fb93a4SDavid Ahern 		goto out;
332523fb93a4SDavid Ahern 
332623fb93a4SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY &&
332723fb93a4SDavid Ahern 	    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
332823fb93a4SDavid Ahern 		goto out;
3329761f6026SXin Long 
333023fb93a4SDavid Ahern 	rc = rt6_remove_exception_rt(rt);
333123fb93a4SDavid Ahern out:
333223fb93a4SDavid Ahern 	return rc;
333323fb93a4SDavid Ahern }
333423fb93a4SDavid Ahern 
3335333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
3336333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
33371da177e4SLinus Torvalds {
33388d1c802bSDavid Ahern 	struct rt6_info *rt_cache;
3339c71099acSThomas Graf 	struct fib6_table *table;
33408d1c802bSDavid Ahern 	struct fib6_info *rt;
33411da177e4SLinus Torvalds 	struct fib6_node *fn;
33421da177e4SLinus Torvalds 	int err = -ESRCH;
33431da177e4SLinus Torvalds 
33445578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3345d5d531cbSDavid Ahern 	if (!table) {
3346d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
3347c71099acSThomas Graf 		return err;
3348d5d531cbSDavid Ahern 	}
33491da177e4SLinus Torvalds 
335066f5d6ceSWei Wang 	rcu_read_lock();
3351c71099acSThomas Graf 
3352c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
335386872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
335438fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
33552b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
33561da177e4SLinus Torvalds 
33571da177e4SLinus Torvalds 	if (fn) {
335866f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
3359ad1601aeSDavid Ahern 			struct fib6_nh *nh;
3360ad1601aeSDavid Ahern 
33612b760fcfSWei Wang 			if (cfg->fc_flags & RTF_CACHE) {
33627e4b5128SDavid Ahern 				struct fib6_result res = {
33637e4b5128SDavid Ahern 					.f6i = rt,
33647e4b5128SDavid Ahern 				};
336523fb93a4SDavid Ahern 				int rc;
336623fb93a4SDavid Ahern 
33677e4b5128SDavid Ahern 				rt_cache = rt6_find_cached_rt(&res,
33687e4b5128SDavid Ahern 							      &cfg->fc_dst,
33692b760fcfSWei Wang 							      &cfg->fc_src);
337023fb93a4SDavid Ahern 				if (rt_cache) {
337123fb93a4SDavid Ahern 					rc = ip6_del_cached_rt(rt_cache, cfg);
33729e575010SEric Dumazet 					if (rc != -ESRCH) {
33739e575010SEric Dumazet 						rcu_read_unlock();
337423fb93a4SDavid Ahern 						return rc;
337523fb93a4SDavid Ahern 					}
33769e575010SEric Dumazet 				}
33771f56a01fSMartin KaFai Lau 				continue;
33782b760fcfSWei Wang 			}
3379ad1601aeSDavid Ahern 
3380ad1601aeSDavid Ahern 			nh = &rt->fib6_nh;
338186872cb5SThomas Graf 			if (cfg->fc_ifindex &&
3382ad1601aeSDavid Ahern 			    (!nh->fib_nh_dev ||
3383ad1601aeSDavid Ahern 			     nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
33841da177e4SLinus Torvalds 				continue;
338586872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
3386ad1601aeSDavid Ahern 			    !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
33871da177e4SLinus Torvalds 				continue;
338893c2fb25SDavid Ahern 			if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
33891da177e4SLinus Torvalds 				continue;
339093c2fb25SDavid Ahern 			if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
3391c2ed1880SMantas M 				continue;
3392e873e4b9SWei Wang 			if (!fib6_info_hold_safe(rt))
3393e873e4b9SWei Wang 				continue;
339466f5d6ceSWei Wang 			rcu_read_unlock();
33951da177e4SLinus Torvalds 
33960ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
33970ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
339886872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
33990ae81335SDavid Ahern 
34000ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
34011da177e4SLinus Torvalds 		}
34021da177e4SLinus Torvalds 	}
340366f5d6ceSWei Wang 	rcu_read_unlock();
34041da177e4SLinus Torvalds 
34051da177e4SLinus Torvalds 	return err;
34061da177e4SLinus Torvalds }
34071da177e4SLinus Torvalds 
34086700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3409a6279458SYOSHIFUJI Hideaki {
3410a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
3411e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
341285bd05deSDavid Ahern 	struct fib6_result res = {};
3413e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
3414e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
3415e8599ff4SDavid S. Miller 	struct neighbour *neigh;
341671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
34176e157b6aSDavid S. Miller 	int optlen, on_link;
34186e157b6aSDavid S. Miller 	u8 *lladdr;
3419e8599ff4SDavid S. Miller 
342029a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
342171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
3422e8599ff4SDavid S. Miller 
3423e8599ff4SDavid S. Miller 	if (optlen < 0) {
34246e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3425e8599ff4SDavid S. Miller 		return;
3426e8599ff4SDavid S. Miller 	}
3427e8599ff4SDavid S. Miller 
342871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
3429e8599ff4SDavid S. Miller 
343071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
34316e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3432e8599ff4SDavid S. Miller 		return;
3433e8599ff4SDavid S. Miller 	}
3434e8599ff4SDavid S. Miller 
34356e157b6aSDavid S. Miller 	on_link = 0;
343671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
3437e8599ff4SDavid S. Miller 		on_link = 1;
343871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
3439e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
34406e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
3441e8599ff4SDavid S. Miller 		return;
3442e8599ff4SDavid S. Miller 	}
3443e8599ff4SDavid S. Miller 
3444e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
3445e8599ff4SDavid S. Miller 	if (!in6_dev)
3446e8599ff4SDavid S. Miller 		return;
3447e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3448e8599ff4SDavid S. Miller 		return;
3449e8599ff4SDavid S. Miller 
3450e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
3451e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
3452e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
3453e8599ff4SDavid S. Miller 	 */
3454e8599ff4SDavid S. Miller 
3455f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
3456e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3457e8599ff4SDavid S. Miller 		return;
3458e8599ff4SDavid S. Miller 	}
34596e157b6aSDavid S. Miller 
34606e157b6aSDavid S. Miller 	lladdr = NULL;
3461e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
3462e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3463e8599ff4SDavid S. Miller 					     skb->dev);
3464e8599ff4SDavid S. Miller 		if (!lladdr) {
3465e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3466e8599ff4SDavid S. Miller 			return;
3467e8599ff4SDavid S. Miller 		}
3468e8599ff4SDavid S. Miller 	}
3469e8599ff4SDavid S. Miller 
34706e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
3471ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
34726e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
34736e157b6aSDavid S. Miller 		return;
34746e157b6aSDavid S. Miller 	}
34756e157b6aSDavid S. Miller 
34766e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
34776e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
34786e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
34796e157b6aSDavid S. Miller 	 */
34800dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
34816e157b6aSDavid S. Miller 
348271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
3483e8599ff4SDavid S. Miller 	if (!neigh)
3484e8599ff4SDavid S. Miller 		return;
3485e8599ff4SDavid S. Miller 
34861da177e4SLinus Torvalds 	/*
34871da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
34881da177e4SLinus Torvalds 	 */
34891da177e4SLinus Torvalds 
3490f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
34911da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
34921da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
34931da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
3494f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
3495f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
34961da177e4SLinus Torvalds 
34974d85cd0cSDavid Ahern 	rcu_read_lock();
349885bd05deSDavid Ahern 	res.f6i = rcu_dereference(rt->from);
3499ff24e498SDavid S. Miller 	if (!res.f6i)
3500886b7a50SMartin KaFai Lau 		goto out;
35018a14e46fSDavid Ahern 
350285bd05deSDavid Ahern 	res.nh = &res.f6i->fib6_nh;
35037d21fec9SDavid Ahern 	res.fib6_flags = res.f6i->fib6_flags;
35047d21fec9SDavid Ahern 	res.fib6_type = res.f6i->fib6_type;
350585bd05deSDavid Ahern 	nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
350638308473SDavid S. Miller 	if (!nrt)
35071da177e4SLinus Torvalds 		goto out;
35081da177e4SLinus Torvalds 
35091da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
35101da177e4SLinus Torvalds 	if (on_link)
35111da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
35121da177e4SLinus Torvalds 
35134e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
35141da177e4SLinus Torvalds 
3515886b7a50SMartin KaFai Lau 	/* rt6_insert_exception() will take care of duplicated exceptions */
35165012f0a5SDavid Ahern 	if (rt6_insert_exception(nrt, &res)) {
35172b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
35182b760fcfSWei Wang 		goto out;
35192b760fcfSWei Wang 	}
35201da177e4SLinus Torvalds 
3521d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
3522d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
352371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
352460592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
35258d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
35268d71740cSTom Tucker 
35271da177e4SLinus Torvalds out:
3528886b7a50SMartin KaFai Lau 	rcu_read_unlock();
3529e8599ff4SDavid S. Miller 	neigh_release(neigh);
35306e157b6aSDavid S. Miller }
35316e157b6aSDavid S. Miller 
353270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
35338d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
3534b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3535830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3536830218c1SDavid Ahern 					   struct net_device *dev)
353770ceb4f5SYOSHIFUJI Hideaki {
3538830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3539830218c1SDavid Ahern 	int ifindex = dev->ifindex;
354070ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
35418d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3542c71099acSThomas Graf 	struct fib6_table *table;
354370ceb4f5SYOSHIFUJI Hideaki 
3544830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
354538308473SDavid S. Miller 	if (!table)
3546c71099acSThomas Graf 		return NULL;
3547c71099acSThomas Graf 
354866f5d6ceSWei Wang 	rcu_read_lock();
354938fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
355070ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
355170ceb4f5SYOSHIFUJI Hideaki 		goto out;
355270ceb4f5SYOSHIFUJI Hideaki 
355366f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
3554ad1601aeSDavid Ahern 		if (rt->fib6_nh.fib_nh_dev->ifindex != ifindex)
355570ceb4f5SYOSHIFUJI Hideaki 			continue;
35562b2450caSDavid Ahern 		if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
3557bdf00467SDavid Ahern 		    !rt->fib6_nh.fib_nh_gw_family)
355870ceb4f5SYOSHIFUJI Hideaki 			continue;
3559ad1601aeSDavid Ahern 		if (!ipv6_addr_equal(&rt->fib6_nh.fib_nh_gw6, gwaddr))
356070ceb4f5SYOSHIFUJI Hideaki 			continue;
3561e873e4b9SWei Wang 		if (!fib6_info_hold_safe(rt))
3562e873e4b9SWei Wang 			continue;
356370ceb4f5SYOSHIFUJI Hideaki 		break;
356470ceb4f5SYOSHIFUJI Hideaki 	}
356570ceb4f5SYOSHIFUJI Hideaki out:
356666f5d6ceSWei Wang 	rcu_read_unlock();
356770ceb4f5SYOSHIFUJI Hideaki 	return rt;
356870ceb4f5SYOSHIFUJI Hideaki }
356970ceb4f5SYOSHIFUJI Hideaki 
35708d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
3571b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3572830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3573830218c1SDavid Ahern 					   struct net_device *dev,
357495c96174SEric Dumazet 					   unsigned int pref)
357570ceb4f5SYOSHIFUJI Hideaki {
357686872cb5SThomas Graf 	struct fib6_config cfg = {
3577238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
3578830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
357986872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
358086872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
358186872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
3582b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3583e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
358415e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
3585efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3586efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
358786872cb5SThomas Graf 	};
358870ceb4f5SYOSHIFUJI Hideaki 
3589830218c1SDavid Ahern 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
35904e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
35914e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
359286872cb5SThomas Graf 
3593e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
3594e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
359586872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
359670ceb4f5SYOSHIFUJI Hideaki 
3597acb54e3cSDavid Ahern 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
359870ceb4f5SYOSHIFUJI Hideaki 
3599830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
360070ceb4f5SYOSHIFUJI Hideaki }
360170ceb4f5SYOSHIFUJI Hideaki #endif
360270ceb4f5SYOSHIFUJI Hideaki 
36038d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net,
3604afb1d4b5SDavid Ahern 				     const struct in6_addr *addr,
3605afb1d4b5SDavid Ahern 				     struct net_device *dev)
36061da177e4SLinus Torvalds {
3607830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
36088d1c802bSDavid Ahern 	struct fib6_info *rt;
3609c71099acSThomas Graf 	struct fib6_table *table;
36101da177e4SLinus Torvalds 
3611afb1d4b5SDavid Ahern 	table = fib6_get_table(net, tb_id);
361238308473SDavid S. Miller 	if (!table)
3613c71099acSThomas Graf 		return NULL;
36141da177e4SLinus Torvalds 
361566f5d6ceSWei Wang 	rcu_read_lock();
361666f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
3617ad1601aeSDavid Ahern 		struct fib6_nh *nh = &rt->fib6_nh;
3618ad1601aeSDavid Ahern 
3619ad1601aeSDavid Ahern 		if (dev == nh->fib_nh_dev &&
362093c2fb25SDavid Ahern 		    ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
3621ad1601aeSDavid Ahern 		    ipv6_addr_equal(&nh->fib_nh_gw6, addr))
36221da177e4SLinus Torvalds 			break;
36231da177e4SLinus Torvalds 	}
3624e873e4b9SWei Wang 	if (rt && !fib6_info_hold_safe(rt))
3625e873e4b9SWei Wang 		rt = NULL;
362666f5d6ceSWei Wang 	rcu_read_unlock();
36271da177e4SLinus Torvalds 	return rt;
36281da177e4SLinus Torvalds }
36291da177e4SLinus Torvalds 
36308d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net,
3631afb1d4b5SDavid Ahern 				     const struct in6_addr *gwaddr,
3632ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
3633ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
36341da177e4SLinus Torvalds {
363586872cb5SThomas Graf 	struct fib6_config cfg = {
3636ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
3637238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
363886872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
363986872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
364086872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
3641b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3642e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
364315e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
36445578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3645afb1d4b5SDavid Ahern 		.fc_nlinfo.nl_net = net,
364686872cb5SThomas Graf 	};
36471da177e4SLinus Torvalds 
36484e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
36491da177e4SLinus Torvalds 
3650acb54e3cSDavid Ahern 	if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
3651830218c1SDavid Ahern 		struct fib6_table *table;
3652830218c1SDavid Ahern 
3653830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
3654830218c1SDavid Ahern 		if (table)
3655830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3656830218c1SDavid Ahern 	}
36571da177e4SLinus Torvalds 
3658afb1d4b5SDavid Ahern 	return rt6_get_dflt_router(net, gwaddr, dev);
36591da177e4SLinus Torvalds }
36601da177e4SLinus Torvalds 
3661afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net,
3662afb1d4b5SDavid Ahern 				     struct fib6_table *table)
36631da177e4SLinus Torvalds {
36648d1c802bSDavid Ahern 	struct fib6_info *rt;
36651da177e4SLinus Torvalds 
36661da177e4SLinus Torvalds restart:
366766f5d6ceSWei Wang 	rcu_read_lock();
366866f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
3669dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
3670dcd1f572SDavid Ahern 		struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3671dcd1f572SDavid Ahern 
367293c2fb25SDavid Ahern 		if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
3673e873e4b9SWei Wang 		    (!idev || idev->cnf.accept_ra != 2) &&
3674e873e4b9SWei Wang 		    fib6_info_hold_safe(rt)) {
367566f5d6ceSWei Wang 			rcu_read_unlock();
3676afb1d4b5SDavid Ahern 			ip6_del_rt(net, rt);
36771da177e4SLinus Torvalds 			goto restart;
36781da177e4SLinus Torvalds 		}
36791da177e4SLinus Torvalds 	}
368066f5d6ceSWei Wang 	rcu_read_unlock();
3681830218c1SDavid Ahern 
3682830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3683830218c1SDavid Ahern }
3684830218c1SDavid Ahern 
3685830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
3686830218c1SDavid Ahern {
3687830218c1SDavid Ahern 	struct fib6_table *table;
3688830218c1SDavid Ahern 	struct hlist_head *head;
3689830218c1SDavid Ahern 	unsigned int h;
3690830218c1SDavid Ahern 
3691830218c1SDavid Ahern 	rcu_read_lock();
3692830218c1SDavid Ahern 
3693830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3694830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
3695830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3696830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3697afb1d4b5SDavid Ahern 				__rt6_purge_dflt_routers(net, table);
3698830218c1SDavid Ahern 		}
3699830218c1SDavid Ahern 	}
3700830218c1SDavid Ahern 
3701830218c1SDavid Ahern 	rcu_read_unlock();
37021da177e4SLinus Torvalds }
37031da177e4SLinus Torvalds 
37045578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
37055578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
370686872cb5SThomas Graf 				 struct fib6_config *cfg)
370786872cb5SThomas Graf {
37088823a3acSMaciej Żenczykowski 	*cfg = (struct fib6_config){
37098823a3acSMaciej Żenczykowski 		.fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
37108823a3acSMaciej Żenczykowski 			 : RT6_TABLE_MAIN,
37118823a3acSMaciej Żenczykowski 		.fc_ifindex = rtmsg->rtmsg_ifindex,
371267f69513SDavid Ahern 		.fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
37138823a3acSMaciej Żenczykowski 		.fc_expires = rtmsg->rtmsg_info,
37148823a3acSMaciej Żenczykowski 		.fc_dst_len = rtmsg->rtmsg_dst_len,
37158823a3acSMaciej Żenczykowski 		.fc_src_len = rtmsg->rtmsg_src_len,
37168823a3acSMaciej Żenczykowski 		.fc_flags = rtmsg->rtmsg_flags,
37178823a3acSMaciej Żenczykowski 		.fc_type = rtmsg->rtmsg_type,
371886872cb5SThomas Graf 
37198823a3acSMaciej Żenczykowski 		.fc_nlinfo.nl_net = net,
372086872cb5SThomas Graf 
37218823a3acSMaciej Żenczykowski 		.fc_dst = rtmsg->rtmsg_dst,
37228823a3acSMaciej Żenczykowski 		.fc_src = rtmsg->rtmsg_src,
37238823a3acSMaciej Żenczykowski 		.fc_gateway = rtmsg->rtmsg_gateway,
37248823a3acSMaciej Żenczykowski 	};
372586872cb5SThomas Graf }
372686872cb5SThomas Graf 
37275578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
37281da177e4SLinus Torvalds {
372986872cb5SThomas Graf 	struct fib6_config cfg;
37301da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
37311da177e4SLinus Torvalds 	int err;
37321da177e4SLinus Torvalds 
37331da177e4SLinus Torvalds 	switch (cmd) {
37341da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
37351da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
3736af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
37371da177e4SLinus Torvalds 			return -EPERM;
37381da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
37391da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
37401da177e4SLinus Torvalds 		if (err)
37411da177e4SLinus Torvalds 			return -EFAULT;
37421da177e4SLinus Torvalds 
37435578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
374486872cb5SThomas Graf 
37451da177e4SLinus Torvalds 		rtnl_lock();
37461da177e4SLinus Torvalds 		switch (cmd) {
37471da177e4SLinus Torvalds 		case SIOCADDRT:
3748acb54e3cSDavid Ahern 			err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
37491da177e4SLinus Torvalds 			break;
37501da177e4SLinus Torvalds 		case SIOCDELRT:
3751333c4301SDavid Ahern 			err = ip6_route_del(&cfg, NULL);
37521da177e4SLinus Torvalds 			break;
37531da177e4SLinus Torvalds 		default:
37541da177e4SLinus Torvalds 			err = -EINVAL;
37551da177e4SLinus Torvalds 		}
37561da177e4SLinus Torvalds 		rtnl_unlock();
37571da177e4SLinus Torvalds 
37581da177e4SLinus Torvalds 		return err;
37593ff50b79SStephen Hemminger 	}
37601da177e4SLinus Torvalds 
37611da177e4SLinus Torvalds 	return -EINVAL;
37621da177e4SLinus Torvalds }
37631da177e4SLinus Torvalds 
37641da177e4SLinus Torvalds /*
37651da177e4SLinus Torvalds  *	Drop the packet on the floor
37661da177e4SLinus Torvalds  */
37671da177e4SLinus Torvalds 
3768d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
37691da177e4SLinus Torvalds {
3770adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
37711d3fd8a1SStephen Suryaputra 	struct net *net = dev_net(dst->dev);
37721d3fd8a1SStephen Suryaputra 	struct inet6_dev *idev;
37731d3fd8a1SStephen Suryaputra 	int type;
37741d3fd8a1SStephen Suryaputra 
37751d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev) &&
37761d3fd8a1SStephen Suryaputra 	    dst->dev == net->loopback_dev)
37771d3fd8a1SStephen Suryaputra 		idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
37781d3fd8a1SStephen Suryaputra 	else
37791d3fd8a1SStephen Suryaputra 		idev = ip6_dst_idev(dst);
37801d3fd8a1SStephen Suryaputra 
3781612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
3782612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
37830660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
378445bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
37851d3fd8a1SStephen Suryaputra 			IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
3786612f09e8SYOSHIFUJI Hideaki 			break;
3787612f09e8SYOSHIFUJI Hideaki 		}
3788612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
3789612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
37901d3fd8a1SStephen Suryaputra 		IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
3791612f09e8SYOSHIFUJI Hideaki 		break;
3792612f09e8SYOSHIFUJI Hideaki 	}
37931d3fd8a1SStephen Suryaputra 
37941d3fd8a1SStephen Suryaputra 	/* Start over by dropping the dst for l3mdev case */
37951d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev))
37961d3fd8a1SStephen Suryaputra 		skb_dst_drop(skb);
37971d3fd8a1SStephen Suryaputra 
37983ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
37991da177e4SLinus Torvalds 	kfree_skb(skb);
38001da177e4SLinus Torvalds 	return 0;
38011da177e4SLinus Torvalds }
38021da177e4SLinus Torvalds 
38039ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
38049ce8ade0SThomas Graf {
3805612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
38069ce8ade0SThomas Graf }
38079ce8ade0SThomas Graf 
3808ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
38091da177e4SLinus Torvalds {
3810adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3811612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
38121da177e4SLinus Torvalds }
38131da177e4SLinus Torvalds 
38149ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
38159ce8ade0SThomas Graf {
3816612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
38179ce8ade0SThomas Graf }
38189ce8ade0SThomas Graf 
3819ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
38209ce8ade0SThomas Graf {
3821adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3822612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
38239ce8ade0SThomas Graf }
38249ce8ade0SThomas Graf 
38251da177e4SLinus Torvalds /*
38261da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
38271da177e4SLinus Torvalds  */
38281da177e4SLinus Torvalds 
3829360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net,
3830afb1d4b5SDavid Ahern 				     struct inet6_dev *idev,
38311da177e4SLinus Torvalds 				     const struct in6_addr *addr,
3832acb54e3cSDavid Ahern 				     bool anycast, gfp_t gfp_flags)
38331da177e4SLinus Torvalds {
3834c7a1ce39SDavid Ahern 	struct fib6_config cfg = {
3835c7a1ce39SDavid Ahern 		.fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
3836c7a1ce39SDavid Ahern 		.fc_ifindex = idev->dev->ifindex,
3837c7a1ce39SDavid Ahern 		.fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP,
3838c7a1ce39SDavid Ahern 		.fc_dst = *addr,
3839c7a1ce39SDavid Ahern 		.fc_dst_len = 128,
3840c7a1ce39SDavid Ahern 		.fc_protocol = RTPROT_KERNEL,
3841c7a1ce39SDavid Ahern 		.fc_nlinfo.nl_net = net,
3842c7a1ce39SDavid Ahern 		.fc_ignore_dev_down = true,
3843c7a1ce39SDavid Ahern 	};
38445f02ce24SDavid Ahern 
3845e8478e80SDavid Ahern 	if (anycast) {
3846c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_ANYCAST;
3847c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_ANYCAST;
3848e8478e80SDavid Ahern 	} else {
3849c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_LOCAL;
3850c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_LOCAL;
3851e8478e80SDavid Ahern 	}
38521da177e4SLinus Torvalds 
3853c7a1ce39SDavid Ahern 	return ip6_route_info_create(&cfg, gfp_flags, NULL);
38541da177e4SLinus Torvalds }
38551da177e4SLinus Torvalds 
3856c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
3857c3968a85SDaniel Walter struct arg_dev_net_ip {
3858c3968a85SDaniel Walter 	struct net_device *dev;
3859c3968a85SDaniel Walter 	struct net *net;
3860c3968a85SDaniel Walter 	struct in6_addr *addr;
3861c3968a85SDaniel Walter };
3862c3968a85SDaniel Walter 
38638d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
3864c3968a85SDaniel Walter {
3865c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3866c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3867c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3868c3968a85SDaniel Walter 
3869ad1601aeSDavid Ahern 	if (((void *)rt->fib6_nh.fib_nh_dev == dev || !dev) &&
3870421842edSDavid Ahern 	    rt != net->ipv6.fib6_null_entry &&
387193c2fb25SDavid Ahern 	    ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
387260006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
3873c3968a85SDaniel Walter 		/* remove prefsrc entry */
387493c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
387560006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
3876c3968a85SDaniel Walter 	}
3877c3968a85SDaniel Walter 	return 0;
3878c3968a85SDaniel Walter }
3879c3968a85SDaniel Walter 
3880c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3881c3968a85SDaniel Walter {
3882c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
3883c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
3884c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
3885c3968a85SDaniel Walter 		.net = net,
3886c3968a85SDaniel Walter 		.addr = &ifp->addr,
3887c3968a85SDaniel Walter 	};
38880c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
3889c3968a85SDaniel Walter }
3890c3968a85SDaniel Walter 
38912b2450caSDavid Ahern #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT)
3892be7a010dSDuan Jiong 
3893be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
38948d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
3895be7a010dSDuan Jiong {
3896be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
3897be7a010dSDuan Jiong 
389893c2fb25SDavid Ahern 	if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
3899bdf00467SDavid Ahern 	    rt->fib6_nh.fib_nh_gw_family &&
3900ad1601aeSDavid Ahern 	    ipv6_addr_equal(gateway, &rt->fib6_nh.fib_nh_gw6)) {
3901be7a010dSDuan Jiong 		return -1;
3902be7a010dSDuan Jiong 	}
3903b16cb459SWei Wang 
3904b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
3905b16cb459SWei Wang 	 * This is needed because cached route may have a different
3906b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
3907b16cb459SWei Wang 	 */
3908b16cb459SWei Wang 	rt6_exceptions_clean_tohost(rt, gateway);
3909b16cb459SWei Wang 
3910be7a010dSDuan Jiong 	return 0;
3911be7a010dSDuan Jiong }
3912be7a010dSDuan Jiong 
3913be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3914be7a010dSDuan Jiong {
3915be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
3916be7a010dSDuan Jiong }
3917be7a010dSDuan Jiong 
39182127d95aSIdo Schimmel struct arg_netdev_event {
39192127d95aSIdo Schimmel 	const struct net_device *dev;
39204c981e28SIdo Schimmel 	union {
3921ecc5663cSDavid Ahern 		unsigned char nh_flags;
39224c981e28SIdo Schimmel 		unsigned long event;
39234c981e28SIdo Schimmel 	};
39242127d95aSIdo Schimmel };
39252127d95aSIdo Schimmel 
39268d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
3927d7dedee1SIdo Schimmel {
39288d1c802bSDavid Ahern 	struct fib6_info *iter;
3929d7dedee1SIdo Schimmel 	struct fib6_node *fn;
3930d7dedee1SIdo Schimmel 
393193c2fb25SDavid Ahern 	fn = rcu_dereference_protected(rt->fib6_node,
393293c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3933d7dedee1SIdo Schimmel 	iter = rcu_dereference_protected(fn->leaf,
393493c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3935d7dedee1SIdo Schimmel 	while (iter) {
393693c2fb25SDavid Ahern 		if (iter->fib6_metric == rt->fib6_metric &&
393733bd5ac5SDavid Ahern 		    rt6_qualify_for_ecmp(iter))
3938d7dedee1SIdo Schimmel 			return iter;
39398fb11a9aSDavid Ahern 		iter = rcu_dereference_protected(iter->fib6_next,
394093c2fb25SDavid Ahern 				lockdep_is_held(&rt->fib6_table->tb6_lock));
3941d7dedee1SIdo Schimmel 	}
3942d7dedee1SIdo Schimmel 
3943d7dedee1SIdo Schimmel 	return NULL;
3944d7dedee1SIdo Schimmel }
3945d7dedee1SIdo Schimmel 
39468d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt)
3947d7dedee1SIdo Schimmel {
3948ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD ||
3949ad1601aeSDavid Ahern 	    (rt->fib6_nh.fib_nh_flags & RTNH_F_LINKDOWN &&
3950ad1601aeSDavid Ahern 	     ip6_ignore_linkdown(rt->fib6_nh.fib_nh_dev)))
3951d7dedee1SIdo Schimmel 		return true;
3952d7dedee1SIdo Schimmel 
3953d7dedee1SIdo Schimmel 	return false;
3954d7dedee1SIdo Schimmel }
3955d7dedee1SIdo Schimmel 
39568d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt)
3957d7dedee1SIdo Schimmel {
39588d1c802bSDavid Ahern 	struct fib6_info *iter;
3959d7dedee1SIdo Schimmel 	int total = 0;
3960d7dedee1SIdo Schimmel 
3961d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt))
3962ad1601aeSDavid Ahern 		total += rt->fib6_nh.fib_nh_weight;
3963d7dedee1SIdo Schimmel 
396493c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
3965d7dedee1SIdo Schimmel 		if (!rt6_is_dead(iter))
3966ad1601aeSDavid Ahern 			total += iter->fib6_nh.fib_nh_weight;
3967d7dedee1SIdo Schimmel 	}
3968d7dedee1SIdo Schimmel 
3969d7dedee1SIdo Schimmel 	return total;
3970d7dedee1SIdo Schimmel }
3971d7dedee1SIdo Schimmel 
39728d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
3973d7dedee1SIdo Schimmel {
3974d7dedee1SIdo Schimmel 	int upper_bound = -1;
3975d7dedee1SIdo Schimmel 
3976d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt)) {
3977ad1601aeSDavid Ahern 		*weight += rt->fib6_nh.fib_nh_weight;
3978d7dedee1SIdo Schimmel 		upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
3979d7dedee1SIdo Schimmel 						    total) - 1;
3980d7dedee1SIdo Schimmel 	}
3981ad1601aeSDavid Ahern 	atomic_set(&rt->fib6_nh.fib_nh_upper_bound, upper_bound);
3982d7dedee1SIdo Schimmel }
3983d7dedee1SIdo Schimmel 
39848d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
3985d7dedee1SIdo Schimmel {
39868d1c802bSDavid Ahern 	struct fib6_info *iter;
3987d7dedee1SIdo Schimmel 	int weight = 0;
3988d7dedee1SIdo Schimmel 
3989d7dedee1SIdo Schimmel 	rt6_upper_bound_set(rt, &weight, total);
3990d7dedee1SIdo Schimmel 
399193c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
3992d7dedee1SIdo Schimmel 		rt6_upper_bound_set(iter, &weight, total);
3993d7dedee1SIdo Schimmel }
3994d7dedee1SIdo Schimmel 
39958d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt)
3996d7dedee1SIdo Schimmel {
39978d1c802bSDavid Ahern 	struct fib6_info *first;
3998d7dedee1SIdo Schimmel 	int total;
3999d7dedee1SIdo Schimmel 
4000d7dedee1SIdo Schimmel 	/* In case the entire multipath route was marked for flushing,
4001d7dedee1SIdo Schimmel 	 * then there is no need to rebalance upon the removal of every
4002d7dedee1SIdo Schimmel 	 * sibling route.
4003d7dedee1SIdo Schimmel 	 */
400493c2fb25SDavid Ahern 	if (!rt->fib6_nsiblings || rt->should_flush)
4005d7dedee1SIdo Schimmel 		return;
4006d7dedee1SIdo Schimmel 
4007d7dedee1SIdo Schimmel 	/* During lookup routes are evaluated in order, so we need to
4008d7dedee1SIdo Schimmel 	 * make sure upper bounds are assigned from the first sibling
4009d7dedee1SIdo Schimmel 	 * onwards.
4010d7dedee1SIdo Schimmel 	 */
4011d7dedee1SIdo Schimmel 	first = rt6_multipath_first_sibling(rt);
4012d7dedee1SIdo Schimmel 	if (WARN_ON_ONCE(!first))
4013d7dedee1SIdo Schimmel 		return;
4014d7dedee1SIdo Schimmel 
4015d7dedee1SIdo Schimmel 	total = rt6_multipath_total_weight(first);
4016d7dedee1SIdo Schimmel 	rt6_multipath_upper_bound_set(first, total);
4017d7dedee1SIdo Schimmel }
4018d7dedee1SIdo Schimmel 
40198d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg)
40202127d95aSIdo Schimmel {
40212127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
40227aef6859SDavid Ahern 	struct net *net = dev_net(arg->dev);
40232127d95aSIdo Schimmel 
4024ad1601aeSDavid Ahern 	if (rt != net->ipv6.fib6_null_entry &&
4025ad1601aeSDavid Ahern 	    rt->fib6_nh.fib_nh_dev == arg->dev) {
4026ad1601aeSDavid Ahern 		rt->fib6_nh.fib_nh_flags &= ~arg->nh_flags;
40277aef6859SDavid Ahern 		fib6_update_sernum_upto_root(net, rt);
4028d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
40291de178edSIdo Schimmel 	}
40302127d95aSIdo Schimmel 
40312127d95aSIdo Schimmel 	return 0;
40322127d95aSIdo Schimmel }
40332127d95aSIdo Schimmel 
4034ecc5663cSDavid Ahern void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
40352127d95aSIdo Schimmel {
40362127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
40372127d95aSIdo Schimmel 		.dev = dev,
40386802f3adSIdo Schimmel 		{
40392127d95aSIdo Schimmel 			.nh_flags = nh_flags,
40406802f3adSIdo Schimmel 		},
40412127d95aSIdo Schimmel 	};
40422127d95aSIdo Schimmel 
40432127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
40442127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
40452127d95aSIdo Schimmel 
40462127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
40472127d95aSIdo Schimmel }
40482127d95aSIdo Schimmel 
40498d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
40501de178edSIdo Schimmel 				   const struct net_device *dev)
40511de178edSIdo Schimmel {
40528d1c802bSDavid Ahern 	struct fib6_info *iter;
40531de178edSIdo Schimmel 
4054ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_dev == dev)
40551de178edSIdo Schimmel 		return true;
405693c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4057ad1601aeSDavid Ahern 		if (iter->fib6_nh.fib_nh_dev == dev)
40581de178edSIdo Schimmel 			return true;
40591de178edSIdo Schimmel 
40601de178edSIdo Schimmel 	return false;
40611de178edSIdo Schimmel }
40621de178edSIdo Schimmel 
40638d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt)
40641de178edSIdo Schimmel {
40658d1c802bSDavid Ahern 	struct fib6_info *iter;
40661de178edSIdo Schimmel 
40671de178edSIdo Schimmel 	rt->should_flush = 1;
406893c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
40691de178edSIdo Schimmel 		iter->should_flush = 1;
40701de178edSIdo Schimmel }
40711de178edSIdo Schimmel 
40728d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
40731de178edSIdo Schimmel 					     const struct net_device *down_dev)
40741de178edSIdo Schimmel {
40758d1c802bSDavid Ahern 	struct fib6_info *iter;
40761de178edSIdo Schimmel 	unsigned int dead = 0;
40771de178edSIdo Schimmel 
4078ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_dev == down_dev ||
4079ad1601aeSDavid Ahern 	    rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD)
40801de178edSIdo Schimmel 		dead++;
408193c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4082ad1601aeSDavid Ahern 		if (iter->fib6_nh.fib_nh_dev == down_dev ||
4083ad1601aeSDavid Ahern 		    iter->fib6_nh.fib_nh_flags & RTNH_F_DEAD)
40841de178edSIdo Schimmel 			dead++;
40851de178edSIdo Schimmel 
40861de178edSIdo Schimmel 	return dead;
40871de178edSIdo Schimmel }
40881de178edSIdo Schimmel 
40898d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
40901de178edSIdo Schimmel 				       const struct net_device *dev,
4091ecc5663cSDavid Ahern 				       unsigned char nh_flags)
40921de178edSIdo Schimmel {
40938d1c802bSDavid Ahern 	struct fib6_info *iter;
40941de178edSIdo Schimmel 
4095ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_dev == dev)
4096ad1601aeSDavid Ahern 		rt->fib6_nh.fib_nh_flags |= nh_flags;
409793c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4098ad1601aeSDavid Ahern 		if (iter->fib6_nh.fib_nh_dev == dev)
4099ad1601aeSDavid Ahern 			iter->fib6_nh.fib_nh_flags |= nh_flags;
41001de178edSIdo Schimmel }
41011de178edSIdo Schimmel 
4102a1a22c12SDavid Ahern /* called with write lock held for table with rt */
41038d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
41041da177e4SLinus Torvalds {
41054c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
41064c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
41077aef6859SDavid Ahern 	struct net *net = dev_net(dev);
41088ed67789SDaniel Lezcano 
4109421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
411027c6fa73SIdo Schimmel 		return 0;
411127c6fa73SIdo Schimmel 
411227c6fa73SIdo Schimmel 	switch (arg->event) {
411327c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
4114ad1601aeSDavid Ahern 		return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0;
411527c6fa73SIdo Schimmel 	case NETDEV_DOWN:
41161de178edSIdo Schimmel 		if (rt->should_flush)
411727c6fa73SIdo Schimmel 			return -1;
411893c2fb25SDavid Ahern 		if (!rt->fib6_nsiblings)
4119ad1601aeSDavid Ahern 			return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0;
41201de178edSIdo Schimmel 		if (rt6_multipath_uses_dev(rt, dev)) {
41211de178edSIdo Schimmel 			unsigned int count;
41221de178edSIdo Schimmel 
41231de178edSIdo Schimmel 			count = rt6_multipath_dead_count(rt, dev);
412493c2fb25SDavid Ahern 			if (rt->fib6_nsiblings + 1 == count) {
41251de178edSIdo Schimmel 				rt6_multipath_flush(rt);
41261de178edSIdo Schimmel 				return -1;
41271de178edSIdo Schimmel 			}
41281de178edSIdo Schimmel 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
41291de178edSIdo Schimmel 						   RTNH_F_LINKDOWN);
41307aef6859SDavid Ahern 			fib6_update_sernum(net, rt);
4131d7dedee1SIdo Schimmel 			rt6_multipath_rebalance(rt);
41321de178edSIdo Schimmel 		}
41331de178edSIdo Schimmel 		return -2;
413427c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
4135ad1601aeSDavid Ahern 		if (rt->fib6_nh.fib_nh_dev != dev ||
413693c2fb25SDavid Ahern 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
413727c6fa73SIdo Schimmel 			break;
4138ad1601aeSDavid Ahern 		rt->fib6_nh.fib_nh_flags |= RTNH_F_LINKDOWN;
4139d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
414027c6fa73SIdo Schimmel 		break;
41412b241361SIdo Schimmel 	}
4142c159d30cSDavid S. Miller 
41431da177e4SLinus Torvalds 	return 0;
41441da177e4SLinus Torvalds }
41451da177e4SLinus Torvalds 
414627c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
41471da177e4SLinus Torvalds {
41484c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
41498ed67789SDaniel Lezcano 		.dev = dev,
41506802f3adSIdo Schimmel 		{
41514c981e28SIdo Schimmel 			.event = event,
41526802f3adSIdo Schimmel 		},
41538ed67789SDaniel Lezcano 	};
41547c6bb7d2SDavid Ahern 	struct net *net = dev_net(dev);
41558ed67789SDaniel Lezcano 
41567c6bb7d2SDavid Ahern 	if (net->ipv6.sysctl.skip_notify_on_dev_down)
41577c6bb7d2SDavid Ahern 		fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
41587c6bb7d2SDavid Ahern 	else
41597c6bb7d2SDavid Ahern 		fib6_clean_all(net, fib6_ifdown, &arg);
41604c981e28SIdo Schimmel }
41614c981e28SIdo Schimmel 
41624c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
41634c981e28SIdo Schimmel {
41644c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
41654c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
41664c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
41671da177e4SLinus Torvalds }
41681da177e4SLinus Torvalds 
416995c96174SEric Dumazet struct rt6_mtu_change_arg {
41701da177e4SLinus Torvalds 	struct net_device *dev;
417195c96174SEric Dumazet 	unsigned int mtu;
41721da177e4SLinus Torvalds };
41731da177e4SLinus Torvalds 
41748d1c802bSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg)
41751da177e4SLinus Torvalds {
41761da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
41771da177e4SLinus Torvalds 	struct inet6_dev *idev;
41781da177e4SLinus Torvalds 
41791da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
41801da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
41811da177e4SLinus Torvalds 	   We still use this lock to block changes
41821da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
41831da177e4SLinus Torvalds 	*/
41841da177e4SLinus Torvalds 
41851da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
418638308473SDavid S. Miller 	if (!idev)
41871da177e4SLinus Torvalds 		return 0;
41881da177e4SLinus Torvalds 
41891da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
41901da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
41911da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
41921da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
41931da177e4SLinus Torvalds 	 */
4194ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_dev == arg->dev &&
4195d4ead6b3SDavid Ahern 	    !fib6_metric_locked(rt, RTAX_MTU)) {
4196d4ead6b3SDavid Ahern 		u32 mtu = rt->fib6_pmtu;
4197d4ead6b3SDavid Ahern 
4198d4ead6b3SDavid Ahern 		if (mtu >= arg->mtu ||
4199d4ead6b3SDavid Ahern 		    (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4200d4ead6b3SDavid Ahern 			fib6_metric_set(rt, RTAX_MTU, arg->mtu);
4201d4ead6b3SDavid Ahern 
4202f5bbe7eeSWei Wang 		spin_lock_bh(&rt6_exception_lock);
4203e9fa1495SStefano Brivio 		rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
4204f5bbe7eeSWei Wang 		spin_unlock_bh(&rt6_exception_lock);
42054b32b5adSMartin KaFai Lau 	}
42061da177e4SLinus Torvalds 	return 0;
42071da177e4SLinus Torvalds }
42081da177e4SLinus Torvalds 
420995c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
42101da177e4SLinus Torvalds {
4211c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
4212c71099acSThomas Graf 		.dev = dev,
4213c71099acSThomas Graf 		.mtu = mtu,
4214c71099acSThomas Graf 	};
42151da177e4SLinus Torvalds 
42160c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
42171da177e4SLinus Torvalds }
42181da177e4SLinus Torvalds 
4219ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
42205176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
4221aa8f8778SEric Dumazet 	[RTA_PREFSRC]		= { .len = sizeof(struct in6_addr) },
422286872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
4223ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
422486872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
422586872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
422651ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
4227c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
422819e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
422919e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
423032bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
4231622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
42323b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
4233aa8f8778SEric Dumazet 	[RTA_TABLE]		= { .type = NLA_U32 },
4234eacb9384SRoopa Prabhu 	[RTA_IP_PROTO]		= { .type = NLA_U8 },
4235eacb9384SRoopa Prabhu 	[RTA_SPORT]		= { .type = NLA_U16 },
4236eacb9384SRoopa Prabhu 	[RTA_DPORT]		= { .type = NLA_U16 },
423786872cb5SThomas Graf };
423886872cb5SThomas Graf 
423986872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4240333c4301SDavid Ahern 			      struct fib6_config *cfg,
4241333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
42421da177e4SLinus Torvalds {
424386872cb5SThomas Graf 	struct rtmsg *rtm;
424486872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
4245c78ba6d6SLubomir Rintel 	unsigned int pref;
424686872cb5SThomas Graf 	int err;
42471da177e4SLinus Torvalds 
42488cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
42498cb08174SJohannes Berg 				     rtm_ipv6_policy, extack);
425086872cb5SThomas Graf 	if (err < 0)
425186872cb5SThomas Graf 		goto errout;
42521da177e4SLinus Torvalds 
425386872cb5SThomas Graf 	err = -EINVAL;
425486872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
425586872cb5SThomas Graf 
425684db8407SMaciej Żenczykowski 	*cfg = (struct fib6_config){
425784db8407SMaciej Żenczykowski 		.fc_table = rtm->rtm_table,
425884db8407SMaciej Żenczykowski 		.fc_dst_len = rtm->rtm_dst_len,
425984db8407SMaciej Żenczykowski 		.fc_src_len = rtm->rtm_src_len,
426084db8407SMaciej Żenczykowski 		.fc_flags = RTF_UP,
426184db8407SMaciej Żenczykowski 		.fc_protocol = rtm->rtm_protocol,
426284db8407SMaciej Żenczykowski 		.fc_type = rtm->rtm_type,
426384db8407SMaciej Żenczykowski 
426484db8407SMaciej Żenczykowski 		.fc_nlinfo.portid = NETLINK_CB(skb).portid,
426584db8407SMaciej Żenczykowski 		.fc_nlinfo.nlh = nlh,
426684db8407SMaciej Żenczykowski 		.fc_nlinfo.nl_net = sock_net(skb->sk),
426784db8407SMaciej Żenczykowski 	};
426886872cb5SThomas Graf 
4269ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
4270ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
4271b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
4272b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
427386872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
427486872cb5SThomas Graf 
4275ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
4276ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
4277ab79ad14SMaciej Żenczykowski 
42781f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
42791f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
42801f56a01fSMartin KaFai Lau 
4281fc1e64e1SDavid Ahern 	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4282fc1e64e1SDavid Ahern 
428386872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
428467b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
428586872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
42861da177e4SLinus Torvalds 	}
4287e3818541SDavid Ahern 	if (tb[RTA_VIA]) {
4288e3818541SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4289e3818541SDavid Ahern 		goto errout;
4290e3818541SDavid Ahern 	}
429186872cb5SThomas Graf 
429286872cb5SThomas Graf 	if (tb[RTA_DST]) {
429386872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
429486872cb5SThomas Graf 
429586872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
429686872cb5SThomas Graf 			goto errout;
429786872cb5SThomas Graf 
429886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
42991da177e4SLinus Torvalds 	}
430086872cb5SThomas Graf 
430186872cb5SThomas Graf 	if (tb[RTA_SRC]) {
430286872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
430386872cb5SThomas Graf 
430486872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
430586872cb5SThomas Graf 			goto errout;
430686872cb5SThomas Graf 
430786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
43081da177e4SLinus Torvalds 	}
430986872cb5SThomas Graf 
4310c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
431167b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4312c3968a85SDaniel Walter 
431386872cb5SThomas Graf 	if (tb[RTA_OIF])
431486872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
431586872cb5SThomas Graf 
431686872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
431786872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
431886872cb5SThomas Graf 
431986872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
432086872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
432186872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
43221da177e4SLinus Torvalds 	}
432386872cb5SThomas Graf 
432486872cb5SThomas Graf 	if (tb[RTA_TABLE])
432586872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
432686872cb5SThomas Graf 
432751ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
432851ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
432951ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
43309ed59592SDavid Ahern 
43319ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4332c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
43339ed59592SDavid Ahern 		if (err < 0)
43349ed59592SDavid Ahern 			goto errout;
433551ebd318SNicolas Dichtel 	}
433651ebd318SNicolas Dichtel 
4337c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
4338c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
4339c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
4340c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
4341c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
4342c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
4343c78ba6d6SLubomir Rintel 	}
4344c78ba6d6SLubomir Rintel 
434519e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
434619e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
434719e42e45SRoopa Prabhu 
43489ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
434919e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
435019e42e45SRoopa Prabhu 
4351c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
43529ed59592SDavid Ahern 		if (err < 0)
43539ed59592SDavid Ahern 			goto errout;
43549ed59592SDavid Ahern 	}
43559ed59592SDavid Ahern 
435632bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
435732bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
435832bc201eSXin Long 
435932bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
436032bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
436132bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
436232bc201eSXin Long 		}
436332bc201eSXin Long 	}
436432bc201eSXin Long 
436586872cb5SThomas Graf 	err = 0;
436686872cb5SThomas Graf errout:
436786872cb5SThomas Graf 	return err;
43681da177e4SLinus Torvalds }
43691da177e4SLinus Torvalds 
43706b9ea5a6SRoopa Prabhu struct rt6_nh {
43718d1c802bSDavid Ahern 	struct fib6_info *fib6_info;
43726b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
43736b9ea5a6SRoopa Prabhu 	struct list_head next;
43746b9ea5a6SRoopa Prabhu };
43756b9ea5a6SRoopa Prabhu 
4376d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net,
4377d4ead6b3SDavid Ahern 				 struct list_head *rt6_nh_list,
43788d1c802bSDavid Ahern 				 struct fib6_info *rt,
43798d1c802bSDavid Ahern 				 struct fib6_config *r_cfg)
43806b9ea5a6SRoopa Prabhu {
43816b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
43826b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
43836b9ea5a6SRoopa Prabhu 
43846b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
43858d1c802bSDavid Ahern 		/* check if fib6_info already exists */
43868d1c802bSDavid Ahern 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
43876b9ea5a6SRoopa Prabhu 			return err;
43886b9ea5a6SRoopa Prabhu 	}
43896b9ea5a6SRoopa Prabhu 
43906b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
43916b9ea5a6SRoopa Prabhu 	if (!nh)
43926b9ea5a6SRoopa Prabhu 		return -ENOMEM;
43938d1c802bSDavid Ahern 	nh->fib6_info = rt;
43946b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
43956b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
43966b9ea5a6SRoopa Prabhu 
43976b9ea5a6SRoopa Prabhu 	return 0;
43986b9ea5a6SRoopa Prabhu }
43996b9ea5a6SRoopa Prabhu 
44008d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt,
44018d1c802bSDavid Ahern 				   struct fib6_info *rt_last,
44023b1137feSDavid Ahern 				   struct nl_info *info,
44033b1137feSDavid Ahern 				   __u16 nlflags)
44043b1137feSDavid Ahern {
44053b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
44063b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
44073b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
44083b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
44093b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
44103b1137feSDavid Ahern 	 */
441193c2fb25SDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
441293c2fb25SDavid Ahern 		rt = list_first_entry(&rt_last->fib6_siblings,
44138d1c802bSDavid Ahern 				      struct fib6_info,
441493c2fb25SDavid Ahern 				      fib6_siblings);
44153b1137feSDavid Ahern 	}
44163b1137feSDavid Ahern 
44173b1137feSDavid Ahern 	if (rt)
44183b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
44193b1137feSDavid Ahern }
44203b1137feSDavid Ahern 
4421333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
4422333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
442351ebd318SNicolas Dichtel {
44248d1c802bSDavid Ahern 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
44253b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
442651ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
442751ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
44288d1c802bSDavid Ahern 	struct fib6_info *rt;
44296b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
44306b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
44313b1137feSDavid Ahern 	__u16 nlflags;
443251ebd318SNicolas Dichtel 	int remaining;
443351ebd318SNicolas Dichtel 	int attrlen;
44346b9ea5a6SRoopa Prabhu 	int err = 1;
44356b9ea5a6SRoopa Prabhu 	int nhn = 0;
44366b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
44376b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
44386b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
443951ebd318SNicolas Dichtel 
44403b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
44413b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
44423b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
44433b1137feSDavid Ahern 
444435f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
444551ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
444651ebd318SNicolas Dichtel 
44476b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
44488d1c802bSDavid Ahern 	 * fib6_info structs per nexthop
44496b9ea5a6SRoopa Prabhu 	 */
445051ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
445151ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
445251ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
445351ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
445451ebd318SNicolas Dichtel 
445551ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
445651ebd318SNicolas Dichtel 		if (attrlen > 0) {
445751ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
445851ebd318SNicolas Dichtel 
445951ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
446051ebd318SNicolas Dichtel 			if (nla) {
446167b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
446251ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
446351ebd318SNicolas Dichtel 			}
446419e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
446519e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
446619e42e45SRoopa Prabhu 			if (nla)
446719e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
446851ebd318SNicolas Dichtel 		}
44696b9ea5a6SRoopa Prabhu 
447068e2ffdeSDavid Ahern 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
4471acb54e3cSDavid Ahern 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
44728c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
44738c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
44748c5b83f0SRoopa Prabhu 			rt = NULL;
44756b9ea5a6SRoopa Prabhu 			goto cleanup;
44768c5b83f0SRoopa Prabhu 		}
4477b5d2d75eSDavid Ahern 		if (!rt6_qualify_for_ecmp(rt)) {
4478b5d2d75eSDavid Ahern 			err = -EINVAL;
4479b5d2d75eSDavid Ahern 			NL_SET_ERR_MSG(extack,
4480b5d2d75eSDavid Ahern 				       "Device only routes can not be added for IPv6 using the multipath API.");
4481b5d2d75eSDavid Ahern 			fib6_info_release(rt);
4482b5d2d75eSDavid Ahern 			goto cleanup;
4483b5d2d75eSDavid Ahern 		}
44846b9ea5a6SRoopa Prabhu 
4485ad1601aeSDavid Ahern 		rt->fib6_nh.fib_nh_weight = rtnh->rtnh_hops + 1;
4486398958aeSIdo Schimmel 
4487d4ead6b3SDavid Ahern 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4488d4ead6b3SDavid Ahern 					    rt, &r_cfg);
448951ebd318SNicolas Dichtel 		if (err) {
449093531c67SDavid Ahern 			fib6_info_release(rt);
44916b9ea5a6SRoopa Prabhu 			goto cleanup;
449251ebd318SNicolas Dichtel 		}
44936b9ea5a6SRoopa Prabhu 
44946b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
449551ebd318SNicolas Dichtel 	}
44966b9ea5a6SRoopa Prabhu 
44973b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
44983b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
44993b1137feSDavid Ahern 	 * the full route when done
45003b1137feSDavid Ahern 	 */
45013b1137feSDavid Ahern 	info->skip_notify = 1;
45023b1137feSDavid Ahern 
45036b9ea5a6SRoopa Prabhu 	err_nh = NULL;
45046b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
45058d1c802bSDavid Ahern 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
45068d1c802bSDavid Ahern 		fib6_info_release(nh->fib6_info);
45073b1137feSDavid Ahern 
4508f7225172SDavid Ahern 		if (!err) {
4509f7225172SDavid Ahern 			/* save reference to last route successfully inserted */
4510f7225172SDavid Ahern 			rt_last = nh->fib6_info;
4511f7225172SDavid Ahern 
45126b9ea5a6SRoopa Prabhu 			/* save reference to first route for notification */
4513f7225172SDavid Ahern 			if (!rt_notif)
45148d1c802bSDavid Ahern 				rt_notif = nh->fib6_info;
4515f7225172SDavid Ahern 		}
45166b9ea5a6SRoopa Prabhu 
45178d1c802bSDavid Ahern 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
45188d1c802bSDavid Ahern 		nh->fib6_info = NULL;
45196b9ea5a6SRoopa Prabhu 		if (err) {
45206b9ea5a6SRoopa Prabhu 			if (replace && nhn)
4521a5a82d84SJakub Kicinski 				NL_SET_ERR_MSG_MOD(extack,
4522a5a82d84SJakub Kicinski 						   "multipath route replace failed (check consistency of installed routes)");
45236b9ea5a6SRoopa Prabhu 			err_nh = nh;
45246b9ea5a6SRoopa Prabhu 			goto add_errout;
45256b9ea5a6SRoopa Prabhu 		}
45266b9ea5a6SRoopa Prabhu 
45271a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
452827596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
452927596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
453027596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
453127596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
453227596472SMichal Kubeček 		 * be added to it.
45331a72418bSNicolas Dichtel 		 */
453427596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
453527596472SMichal Kubeček 						     NLM_F_REPLACE);
45366b9ea5a6SRoopa Prabhu 		nhn++;
45376b9ea5a6SRoopa Prabhu 	}
45386b9ea5a6SRoopa Prabhu 
45393b1137feSDavid Ahern 	/* success ... tell user about new route */
45403b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
45416b9ea5a6SRoopa Prabhu 	goto cleanup;
45426b9ea5a6SRoopa Prabhu 
45436b9ea5a6SRoopa Prabhu add_errout:
45443b1137feSDavid Ahern 	/* send notification for routes that were added so that
45453b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
45463b1137feSDavid Ahern 	 * coherent
45473b1137feSDavid Ahern 	 */
45483b1137feSDavid Ahern 	if (rt_notif)
45493b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
45503b1137feSDavid Ahern 
45516b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
45526b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
45536b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
45546b9ea5a6SRoopa Prabhu 			break;
4555333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
45566b9ea5a6SRoopa Prabhu 	}
45576b9ea5a6SRoopa Prabhu 
45586b9ea5a6SRoopa Prabhu cleanup:
45596b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
45608d1c802bSDavid Ahern 		if (nh->fib6_info)
45618d1c802bSDavid Ahern 			fib6_info_release(nh->fib6_info);
45626b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
45636b9ea5a6SRoopa Prabhu 		kfree(nh);
45646b9ea5a6SRoopa Prabhu 	}
45656b9ea5a6SRoopa Prabhu 
45666b9ea5a6SRoopa Prabhu 	return err;
45676b9ea5a6SRoopa Prabhu }
45686b9ea5a6SRoopa Prabhu 
4569333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
4570333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
45716b9ea5a6SRoopa Prabhu {
45726b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
45736b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
45746b9ea5a6SRoopa Prabhu 	int remaining;
45756b9ea5a6SRoopa Prabhu 	int attrlen;
45766b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
45776b9ea5a6SRoopa Prabhu 
45786b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
45796b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
45806b9ea5a6SRoopa Prabhu 
45816b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
45826b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
45836b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
45846b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
45856b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
45866b9ea5a6SRoopa Prabhu 
45876b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
45886b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
45896b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
45906b9ea5a6SRoopa Prabhu 
45916b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
45926b9ea5a6SRoopa Prabhu 			if (nla) {
45936b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
45946b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
45956b9ea5a6SRoopa Prabhu 			}
45966b9ea5a6SRoopa Prabhu 		}
4597333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
45986b9ea5a6SRoopa Prabhu 		if (err)
45996b9ea5a6SRoopa Prabhu 			last_err = err;
46006b9ea5a6SRoopa Prabhu 
460151ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
460251ebd318SNicolas Dichtel 	}
460351ebd318SNicolas Dichtel 
460451ebd318SNicolas Dichtel 	return last_err;
460551ebd318SNicolas Dichtel }
460651ebd318SNicolas Dichtel 
4607c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4608c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
46091da177e4SLinus Torvalds {
461086872cb5SThomas Graf 	struct fib6_config cfg;
461186872cb5SThomas Graf 	int err;
46121da177e4SLinus Torvalds 
4613333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
461486872cb5SThomas Graf 	if (err < 0)
461586872cb5SThomas Graf 		return err;
461686872cb5SThomas Graf 
461751ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4618333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
46190ae81335SDavid Ahern 	else {
46200ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
4621333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
46221da177e4SLinus Torvalds 	}
46230ae81335SDavid Ahern }
46241da177e4SLinus Torvalds 
4625c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4626c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
46271da177e4SLinus Torvalds {
462886872cb5SThomas Graf 	struct fib6_config cfg;
462986872cb5SThomas Graf 	int err;
46301da177e4SLinus Torvalds 
4631333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
463286872cb5SThomas Graf 	if (err < 0)
463386872cb5SThomas Graf 		return err;
463486872cb5SThomas Graf 
463567f69513SDavid Ahern 	if (cfg.fc_metric == 0)
463667f69513SDavid Ahern 		cfg.fc_metric = IP6_RT_PRIO_USER;
463767f69513SDavid Ahern 
463851ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4639333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
464051ebd318SNicolas Dichtel 	else
4641acb54e3cSDavid Ahern 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
46421da177e4SLinus Torvalds }
46431da177e4SLinus Torvalds 
46448d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt)
4645339bf98fSThomas Graf {
4646beb1afacSDavid Ahern 	int nexthop_len = 0;
4647beb1afacSDavid Ahern 
464893c2fb25SDavid Ahern 	if (rt->fib6_nsiblings) {
4649beb1afacSDavid Ahern 		nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
4650beb1afacSDavid Ahern 			    + NLA_ALIGN(sizeof(struct rtnexthop))
4651beb1afacSDavid Ahern 			    + nla_total_size(16) /* RTA_GATEWAY */
4652ad1601aeSDavid Ahern 			    + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws);
4653beb1afacSDavid Ahern 
465493c2fb25SDavid Ahern 		nexthop_len *= rt->fib6_nsiblings;
4655beb1afacSDavid Ahern 	}
4656beb1afacSDavid Ahern 
4657339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
4658339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
4659339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
4660339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
4661339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
4662339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
4663339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
4664339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
4665339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
46666a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
4667ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
4668c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
466919e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
4670ad1601aeSDavid Ahern 	       + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws)
4671beb1afacSDavid Ahern 	       + nexthop_len;
4672beb1afacSDavid Ahern }
4673beb1afacSDavid Ahern 
4674d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
46758d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
4676d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
467715e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
4678f8cfe2ceSDavid Ahern 			 unsigned int flags)
46791da177e4SLinus Torvalds {
468022d0bd82SXin Long 	struct rt6_info *rt6 = (struct rt6_info *)dst;
468122d0bd82SXin Long 	struct rt6key *rt6_dst, *rt6_src;
468222d0bd82SXin Long 	u32 *pmetrics, table, rt6_flags;
46831da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
468422d0bd82SXin Long 	struct rtmsg *rtm;
4685d4ead6b3SDavid Ahern 	long expires = 0;
46861da177e4SLinus Torvalds 
468715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
468838308473SDavid S. Miller 	if (!nlh)
468926932566SPatrick McHardy 		return -EMSGSIZE;
46902d7202bfSThomas Graf 
469122d0bd82SXin Long 	if (rt6) {
469222d0bd82SXin Long 		rt6_dst = &rt6->rt6i_dst;
469322d0bd82SXin Long 		rt6_src = &rt6->rt6i_src;
469422d0bd82SXin Long 		rt6_flags = rt6->rt6i_flags;
469522d0bd82SXin Long 	} else {
469622d0bd82SXin Long 		rt6_dst = &rt->fib6_dst;
469722d0bd82SXin Long 		rt6_src = &rt->fib6_src;
469822d0bd82SXin Long 		rt6_flags = rt->fib6_flags;
469922d0bd82SXin Long 	}
470022d0bd82SXin Long 
47012d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
47021da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
470322d0bd82SXin Long 	rtm->rtm_dst_len = rt6_dst->plen;
470422d0bd82SXin Long 	rtm->rtm_src_len = rt6_src->plen;
47051da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
470693c2fb25SDavid Ahern 	if (rt->fib6_table)
470793c2fb25SDavid Ahern 		table = rt->fib6_table->tb6_id;
4708c71099acSThomas Graf 	else
47099e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
471097f0082aSKalash Nainwal 	rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
4711c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
4712c78679e8SDavid S. Miller 		goto nla_put_failure;
4713e8478e80SDavid Ahern 
4714e8478e80SDavid Ahern 	rtm->rtm_type = rt->fib6_type;
47151da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
47161da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
471793c2fb25SDavid Ahern 	rtm->rtm_protocol = rt->fib6_protocol;
47181da177e4SLinus Torvalds 
471922d0bd82SXin Long 	if (rt6_flags & RTF_CACHE)
47201da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
47211da177e4SLinus Torvalds 
4722d4ead6b3SDavid Ahern 	if (dest) {
4723d4ead6b3SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, dest))
4724c78679e8SDavid S. Miller 			goto nla_put_failure;
47251da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
47261da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
472722d0bd82SXin Long 		if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
4728c78679e8SDavid S. Miller 			goto nla_put_failure;
47291da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
47301da177e4SLinus Torvalds 	if (src) {
4731930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
4732c78679e8SDavid S. Miller 			goto nla_put_failure;
47331da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
4734c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
473522d0bd82SXin Long 		   nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
4736c78679e8SDavid S. Miller 		goto nla_put_failure;
47371da177e4SLinus Torvalds #endif
47387bc570c8SYOSHIFUJI Hideaki 	if (iif) {
47397bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
474022d0bd82SXin Long 		if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
4741fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
47422cf75070SNikolay Aleksandrov 
47437bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
47447bc570c8SYOSHIFUJI Hideaki 				return 0;
4745fd61c6baSDavid Ahern 			if (err < 0)
47467bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
47477bc570c8SYOSHIFUJI Hideaki 		} else
47487bc570c8SYOSHIFUJI Hideaki #endif
4749c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
4750c78679e8SDavid S. Miller 				goto nla_put_failure;
4751d4ead6b3SDavid Ahern 	} else if (dest) {
47521da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
4753d4ead6b3SDavid Ahern 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
4754930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4755c78679e8SDavid S. Miller 			goto nla_put_failure;
4756c3968a85SDaniel Walter 	}
4757c3968a85SDaniel Walter 
475893c2fb25SDavid Ahern 	if (rt->fib6_prefsrc.plen) {
4759c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
476093c2fb25SDavid Ahern 		saddr_buf = rt->fib6_prefsrc.addr;
4761930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4762c78679e8SDavid S. Miller 			goto nla_put_failure;
47631da177e4SLinus Torvalds 	}
47642d7202bfSThomas Graf 
4765d4ead6b3SDavid Ahern 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4766d4ead6b3SDavid Ahern 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
47672d7202bfSThomas Graf 		goto nla_put_failure;
47682d7202bfSThomas Graf 
476993c2fb25SDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
4770beb1afacSDavid Ahern 		goto nla_put_failure;
4771beb1afacSDavid Ahern 
4772beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
4773beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
4774beb1afacSDavid Ahern 	 */
477522d0bd82SXin Long 	if (rt6) {
477622d0bd82SXin Long 		if (rt6_flags & RTF_GATEWAY &&
477722d0bd82SXin Long 		    nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
477822d0bd82SXin Long 			goto nla_put_failure;
477922d0bd82SXin Long 
478022d0bd82SXin Long 		if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
478122d0bd82SXin Long 			goto nla_put_failure;
478222d0bd82SXin Long 	} else if (rt->fib6_nsiblings) {
47838d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
4784beb1afacSDavid Ahern 		struct nlattr *mp;
4785beb1afacSDavid Ahern 
4786ae0be8deSMichal Kubecek 		mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
4787beb1afacSDavid Ahern 		if (!mp)
4788beb1afacSDavid Ahern 			goto nla_put_failure;
4789beb1afacSDavid Ahern 
4790c0a72077SDavid Ahern 		if (fib_add_nexthop(skb, &rt->fib6_nh.nh_common,
4791c0a72077SDavid Ahern 				    rt->fib6_nh.fib_nh_weight) < 0)
4792beb1afacSDavid Ahern 			goto nla_put_failure;
4793beb1afacSDavid Ahern 
4794beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
479593c2fb25SDavid Ahern 					 &rt->fib6_siblings, fib6_siblings) {
4796c0a72077SDavid Ahern 			if (fib_add_nexthop(skb, &sibling->fib6_nh.nh_common,
4797c0a72077SDavid Ahern 					    sibling->fib6_nh.fib_nh_weight) < 0)
479894f826b8SEric Dumazet 				goto nla_put_failure;
479994f826b8SEric Dumazet 		}
48002d7202bfSThomas Graf 
4801beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
4802beb1afacSDavid Ahern 	} else {
4803ecc5663cSDavid Ahern 		unsigned char nh_flags = 0;
4804ecc5663cSDavid Ahern 
4805c0a72077SDavid Ahern 		if (fib_nexthop_info(skb, &rt->fib6_nh.nh_common,
4806ecc5663cSDavid Ahern 				     &nh_flags, false) < 0)
4807c78679e8SDavid S. Miller 			goto nla_put_failure;
4808ecc5663cSDavid Ahern 
4809ecc5663cSDavid Ahern 		rtm->rtm_flags |= nh_flags;
4810beb1afacSDavid Ahern 	}
48118253947eSLi Wei 
481222d0bd82SXin Long 	if (rt6_flags & RTF_EXPIRES) {
481314895687SDavid Ahern 		expires = dst ? dst->expires : rt->expires;
481414895687SDavid Ahern 		expires -= jiffies;
481514895687SDavid Ahern 	}
481669cdf8f9SYOSHIFUJI Hideaki 
4817d4ead6b3SDavid Ahern 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
4818e3703b3dSThomas Graf 		goto nla_put_failure;
48191da177e4SLinus Torvalds 
482022d0bd82SXin Long 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
4821c78ba6d6SLubomir Rintel 		goto nla_put_failure;
4822c78ba6d6SLubomir Rintel 
482319e42e45SRoopa Prabhu 
4824053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4825053c095aSJohannes Berg 	return 0;
48262d7202bfSThomas Graf 
48272d7202bfSThomas Graf nla_put_failure:
482826932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
482926932566SPatrick McHardy 	return -EMSGSIZE;
48301da177e4SLinus Torvalds }
48311da177e4SLinus Torvalds 
483213e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i,
483313e38901SDavid Ahern 			       const struct net_device *dev)
483413e38901SDavid Ahern {
4835ad1601aeSDavid Ahern 	if (f6i->fib6_nh.fib_nh_dev == dev)
483613e38901SDavid Ahern 		return true;
483713e38901SDavid Ahern 
483813e38901SDavid Ahern 	if (f6i->fib6_nsiblings) {
483913e38901SDavid Ahern 		struct fib6_info *sibling, *next_sibling;
484013e38901SDavid Ahern 
484113e38901SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
484213e38901SDavid Ahern 					 &f6i->fib6_siblings, fib6_siblings) {
4843ad1601aeSDavid Ahern 			if (sibling->fib6_nh.fib_nh_dev == dev)
484413e38901SDavid Ahern 				return true;
484513e38901SDavid Ahern 		}
484613e38901SDavid Ahern 	}
484713e38901SDavid Ahern 
484813e38901SDavid Ahern 	return false;
484913e38901SDavid Ahern }
485013e38901SDavid Ahern 
48518d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg)
48521da177e4SLinus Torvalds {
48531da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
485413e38901SDavid Ahern 	struct fib_dump_filter *filter = &arg->filter;
485513e38901SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
48561f17e2f2SDavid Ahern 	struct net *net = arg->net;
48571f17e2f2SDavid Ahern 
4858421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
48591f17e2f2SDavid Ahern 		return 0;
48601da177e4SLinus Torvalds 
486113e38901SDavid Ahern 	if ((filter->flags & RTM_F_PREFIX) &&
486293c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_PREFIX_RT)) {
4863f8cfe2ceSDavid Ahern 		/* success since this is not a prefix route */
4864f8cfe2ceSDavid Ahern 		return 1;
4865f8cfe2ceSDavid Ahern 	}
486613e38901SDavid Ahern 	if (filter->filter_set) {
486713e38901SDavid Ahern 		if ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
486813e38901SDavid Ahern 		    (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
486913e38901SDavid Ahern 		    (filter->protocol && rt->fib6_protocol != filter->protocol)) {
487013e38901SDavid Ahern 			return 1;
487113e38901SDavid Ahern 		}
487213e38901SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
4873f8cfe2ceSDavid Ahern 	}
48741da177e4SLinus Torvalds 
4875d4ead6b3SDavid Ahern 	return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4876d4ead6b3SDavid Ahern 			     RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
487713e38901SDavid Ahern 			     arg->cb->nlh->nlmsg_seq, flags);
48781da177e4SLinus Torvalds }
48791da177e4SLinus Torvalds 
48800eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
48810eff0a27SJakub Kicinski 					const struct nlmsghdr *nlh,
48820eff0a27SJakub Kicinski 					struct nlattr **tb,
48830eff0a27SJakub Kicinski 					struct netlink_ext_ack *extack)
48840eff0a27SJakub Kicinski {
48850eff0a27SJakub Kicinski 	struct rtmsg *rtm;
48860eff0a27SJakub Kicinski 	int i, err;
48870eff0a27SJakub Kicinski 
48880eff0a27SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
48890eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
48900eff0a27SJakub Kicinski 				   "Invalid header for get route request");
48910eff0a27SJakub Kicinski 		return -EINVAL;
48920eff0a27SJakub Kicinski 	}
48930eff0a27SJakub Kicinski 
48940eff0a27SJakub Kicinski 	if (!netlink_strict_get_check(skb))
48958cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
48960eff0a27SJakub Kicinski 					      rtm_ipv6_policy, extack);
48970eff0a27SJakub Kicinski 
48980eff0a27SJakub Kicinski 	rtm = nlmsg_data(nlh);
48990eff0a27SJakub Kicinski 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
49000eff0a27SJakub Kicinski 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
49010eff0a27SJakub Kicinski 	    rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
49020eff0a27SJakub Kicinski 	    rtm->rtm_type) {
49030eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
49040eff0a27SJakub Kicinski 		return -EINVAL;
49050eff0a27SJakub Kicinski 	}
49060eff0a27SJakub Kicinski 	if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
49070eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
49080eff0a27SJakub Kicinski 				   "Invalid flags for get route request");
49090eff0a27SJakub Kicinski 		return -EINVAL;
49100eff0a27SJakub Kicinski 	}
49110eff0a27SJakub Kicinski 
49128cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
49130eff0a27SJakub Kicinski 					    rtm_ipv6_policy, extack);
49140eff0a27SJakub Kicinski 	if (err)
49150eff0a27SJakub Kicinski 		return err;
49160eff0a27SJakub Kicinski 
49170eff0a27SJakub Kicinski 	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
49180eff0a27SJakub Kicinski 	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
49190eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
49200eff0a27SJakub Kicinski 		return -EINVAL;
49210eff0a27SJakub Kicinski 	}
49220eff0a27SJakub Kicinski 
49230eff0a27SJakub Kicinski 	for (i = 0; i <= RTA_MAX; i++) {
49240eff0a27SJakub Kicinski 		if (!tb[i])
49250eff0a27SJakub Kicinski 			continue;
49260eff0a27SJakub Kicinski 
49270eff0a27SJakub Kicinski 		switch (i) {
49280eff0a27SJakub Kicinski 		case RTA_SRC:
49290eff0a27SJakub Kicinski 		case RTA_DST:
49300eff0a27SJakub Kicinski 		case RTA_IIF:
49310eff0a27SJakub Kicinski 		case RTA_OIF:
49320eff0a27SJakub Kicinski 		case RTA_MARK:
49330eff0a27SJakub Kicinski 		case RTA_UID:
49340eff0a27SJakub Kicinski 		case RTA_SPORT:
49350eff0a27SJakub Kicinski 		case RTA_DPORT:
49360eff0a27SJakub Kicinski 		case RTA_IP_PROTO:
49370eff0a27SJakub Kicinski 			break;
49380eff0a27SJakub Kicinski 		default:
49390eff0a27SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
49400eff0a27SJakub Kicinski 			return -EINVAL;
49410eff0a27SJakub Kicinski 		}
49420eff0a27SJakub Kicinski 	}
49430eff0a27SJakub Kicinski 
49440eff0a27SJakub Kicinski 	return 0;
49450eff0a27SJakub Kicinski }
49460eff0a27SJakub Kicinski 
4947c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4948c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
49491da177e4SLinus Torvalds {
49503b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
4951ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
495218c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
4953a68886a6SDavid Ahern 	struct fib6_info *from;
495418c3a61cSRoopa Prabhu 	struct dst_entry *dst;
49551da177e4SLinus Torvalds 	struct rt6_info *rt;
4956ab364a6fSThomas Graf 	struct sk_buff *skb;
4957ab364a6fSThomas Graf 	struct rtmsg *rtm;
4958744486d4SMaciej Żenczykowski 	struct flowi6 fl6 = {};
495918c3a61cSRoopa Prabhu 	bool fibmatch;
4960ab364a6fSThomas Graf 
49610eff0a27SJakub Kicinski 	err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
4962ab364a6fSThomas Graf 	if (err < 0)
4963ab364a6fSThomas Graf 		goto errout;
4964ab364a6fSThomas Graf 
4965ab364a6fSThomas Graf 	err = -EINVAL;
496638b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
496738b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
496818c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
4969ab364a6fSThomas Graf 
4970ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
4971ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4972ab364a6fSThomas Graf 			goto errout;
4973ab364a6fSThomas Graf 
49744e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
4975ab364a6fSThomas Graf 	}
4976ab364a6fSThomas Graf 
4977ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
4978ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4979ab364a6fSThomas Graf 			goto errout;
4980ab364a6fSThomas Graf 
49814e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
4982ab364a6fSThomas Graf 	}
4983ab364a6fSThomas Graf 
4984ab364a6fSThomas Graf 	if (tb[RTA_IIF])
4985ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
4986ab364a6fSThomas Graf 
4987ab364a6fSThomas Graf 	if (tb[RTA_OIF])
498872331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
4989ab364a6fSThomas Graf 
49902e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
49912e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
49922e47b291SLorenzo Colitti 
4993622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
4994622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
4995622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
4996622ec2c9SLorenzo Colitti 	else
4997622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4998622ec2c9SLorenzo Colitti 
4999eacb9384SRoopa Prabhu 	if (tb[RTA_SPORT])
5000eacb9384SRoopa Prabhu 		fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5001eacb9384SRoopa Prabhu 
5002eacb9384SRoopa Prabhu 	if (tb[RTA_DPORT])
5003eacb9384SRoopa Prabhu 		fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5004eacb9384SRoopa Prabhu 
5005eacb9384SRoopa Prabhu 	if (tb[RTA_IP_PROTO]) {
5006eacb9384SRoopa Prabhu 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
50075e1a99eaSHangbin Liu 						  &fl6.flowi6_proto, AF_INET6,
50085e1a99eaSHangbin Liu 						  extack);
5009eacb9384SRoopa Prabhu 		if (err)
5010eacb9384SRoopa Prabhu 			goto errout;
5011eacb9384SRoopa Prabhu 	}
5012eacb9384SRoopa Prabhu 
5013ab364a6fSThomas Graf 	if (iif) {
5014ab364a6fSThomas Graf 		struct net_device *dev;
501572331bc0SShmulik Ladkani 		int flags = 0;
501672331bc0SShmulik Ladkani 
5017121622dbSFlorian Westphal 		rcu_read_lock();
5018121622dbSFlorian Westphal 
5019121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
5020ab364a6fSThomas Graf 		if (!dev) {
5021121622dbSFlorian Westphal 			rcu_read_unlock();
5022ab364a6fSThomas Graf 			err = -ENODEV;
5023ab364a6fSThomas Graf 			goto errout;
5024ab364a6fSThomas Graf 		}
502572331bc0SShmulik Ladkani 
502672331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
502772331bc0SShmulik Ladkani 
502872331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
502972331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
503072331bc0SShmulik Ladkani 
5031b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
5032121622dbSFlorian Westphal 
5033121622dbSFlorian Westphal 		rcu_read_unlock();
503472331bc0SShmulik Ladkani 	} else {
503572331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
503672331bc0SShmulik Ladkani 
503718c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
503818c3a61cSRoopa Prabhu 	}
503918c3a61cSRoopa Prabhu 
504018c3a61cSRoopa Prabhu 
504118c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
504218c3a61cSRoopa Prabhu 	if (rt->dst.error) {
504318c3a61cSRoopa Prabhu 		err = rt->dst.error;
504418c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
504518c3a61cSRoopa Prabhu 		goto errout;
5046ab364a6fSThomas Graf 	}
50471da177e4SLinus Torvalds 
50489d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
50499d6acb3bSWANG Cong 		err = rt->dst.error;
50509d6acb3bSWANG Cong 		ip6_rt_put(rt);
50519d6acb3bSWANG Cong 		goto errout;
50529d6acb3bSWANG Cong 	}
50539d6acb3bSWANG Cong 
50541da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
505538308473SDavid S. Miller 	if (!skb) {
505694e187c0SAmerigo Wang 		ip6_rt_put(rt);
5057ab364a6fSThomas Graf 		err = -ENOBUFS;
5058ab364a6fSThomas Graf 		goto errout;
5059ab364a6fSThomas Graf 	}
50601da177e4SLinus Torvalds 
5061d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
5062a68886a6SDavid Ahern 
5063a68886a6SDavid Ahern 	rcu_read_lock();
5064a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
5065886b7a50SMartin KaFai Lau 	if (from) {
506618c3a61cSRoopa Prabhu 		if (fibmatch)
5067886b7a50SMartin KaFai Lau 			err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5068886b7a50SMartin KaFai Lau 					    iif, RTM_NEWROUTE,
5069886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
507018c3a61cSRoopa Prabhu 					    nlh->nlmsg_seq, 0);
507118c3a61cSRoopa Prabhu 		else
5072a68886a6SDavid Ahern 			err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5073a68886a6SDavid Ahern 					    &fl6.saddr, iif, RTM_NEWROUTE,
5074886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
5075886b7a50SMartin KaFai Lau 					    nlh->nlmsg_seq, 0);
5076886b7a50SMartin KaFai Lau 	} else {
5077886b7a50SMartin KaFai Lau 		err = -ENETUNREACH;
5078886b7a50SMartin KaFai Lau 	}
5079a68886a6SDavid Ahern 	rcu_read_unlock();
5080a68886a6SDavid Ahern 
50811da177e4SLinus Torvalds 	if (err < 0) {
5082ab364a6fSThomas Graf 		kfree_skb(skb);
5083ab364a6fSThomas Graf 		goto errout;
50841da177e4SLinus Torvalds 	}
50851da177e4SLinus Torvalds 
508615e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
5087ab364a6fSThomas Graf errout:
50881da177e4SLinus Torvalds 	return err;
50891da177e4SLinus Torvalds }
50901da177e4SLinus Torvalds 
50918d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
509237a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
50931da177e4SLinus Torvalds {
50941da177e4SLinus Torvalds 	struct sk_buff *skb;
50955578689aSDaniel Lezcano 	struct net *net = info->nl_net;
5096528c4cebSDenis V. Lunev 	u32 seq;
5097528c4cebSDenis V. Lunev 	int err;
50980d51aa80SJamal Hadi Salim 
5099528c4cebSDenis V. Lunev 	err = -ENOBUFS;
510038308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
510186872cb5SThomas Graf 
510219e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
510338308473SDavid S. Miller 	if (!skb)
510421713ebcSThomas Graf 		goto errout;
51051da177e4SLinus Torvalds 
5106d4ead6b3SDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5107f8cfe2ceSDavid Ahern 			    event, info->portid, seq, nlm_flags);
510826932566SPatrick McHardy 	if (err < 0) {
510926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
511026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
511126932566SPatrick McHardy 		kfree_skb(skb);
511226932566SPatrick McHardy 		goto errout;
511326932566SPatrick McHardy 	}
511415e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
51155578689aSDaniel Lezcano 		    info->nlh, gfp_any());
51161ce85fe4SPablo Neira Ayuso 	return;
511721713ebcSThomas Graf errout:
511821713ebcSThomas Graf 	if (err < 0)
51195578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
51201da177e4SLinus Torvalds }
51211da177e4SLinus Torvalds 
51228ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
5123351638e7SJiri Pirko 				unsigned long event, void *ptr)
51248ed67789SDaniel Lezcano {
5125351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5126c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
51278ed67789SDaniel Lezcano 
5128242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
5129242d3a49SWANG Cong 		return NOTIFY_OK;
5130242d3a49SWANG Cong 
5131242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
5132ad1601aeSDavid Ahern 		net->ipv6.fib6_null_entry->fib6_nh.fib_nh_dev = dev;
5133d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
51348ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
51358ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5136d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
51378ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
5138d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
51398ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
51408ed67789SDaniel Lezcano #endif
514176da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
514276da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
514376da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
514476da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
514576da0704SWANG Cong 		 */
514612d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
5147242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
514812d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
514912d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
5150242d3a49SWANG Cong #endif
51518ed67789SDaniel Lezcano 	}
51528ed67789SDaniel Lezcano 
51538ed67789SDaniel Lezcano 	return NOTIFY_OK;
51548ed67789SDaniel Lezcano }
51558ed67789SDaniel Lezcano 
51561da177e4SLinus Torvalds /*
51571da177e4SLinus Torvalds  *	/proc
51581da177e4SLinus Torvalds  */
51591da177e4SLinus Torvalds 
51601da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
51611da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
51621da177e4SLinus Torvalds {
516369ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
51641da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
516569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
516669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
516781eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
516869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
516969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
5170fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
517169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
51721da177e4SLinus Torvalds 
51731da177e4SLinus Torvalds 	return 0;
51741da177e4SLinus Torvalds }
51751da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
51761da177e4SLinus Torvalds 
51771da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
51781da177e4SLinus Torvalds 
51791da177e4SLinus Torvalds static
5180fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
51811da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
51821da177e4SLinus Torvalds {
5183c486da34SLucian Adrian Grijincu 	struct net *net;
5184c486da34SLucian Adrian Grijincu 	int delay;
5185f0fb9b28SAditya Pakki 	int ret;
5186c486da34SLucian Adrian Grijincu 	if (!write)
5187c486da34SLucian Adrian Grijincu 		return -EINVAL;
5188c486da34SLucian Adrian Grijincu 
5189c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
5190c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
5191f0fb9b28SAditya Pakki 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5192f0fb9b28SAditya Pakki 	if (ret)
5193f0fb9b28SAditya Pakki 		return ret;
5194f0fb9b28SAditya Pakki 
51952ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
51961da177e4SLinus Torvalds 	return 0;
51971da177e4SLinus Torvalds }
51981da177e4SLinus Torvalds 
51997c6bb7d2SDavid Ahern static int zero;
52007c6bb7d2SDavid Ahern static int one = 1;
52017c6bb7d2SDavid Ahern 
5202ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = {
52031da177e4SLinus Torvalds 	{
52041da177e4SLinus Torvalds 		.procname	=	"flush",
52054990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
52061da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
520789c8b3a1SDave Jones 		.mode		=	0200,
52086d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
52091da177e4SLinus Torvalds 	},
52101da177e4SLinus Torvalds 	{
52111da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
52129a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
52131da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52141da177e4SLinus Torvalds 		.mode		=	0644,
52156d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
52161da177e4SLinus Torvalds 	},
52171da177e4SLinus Torvalds 	{
52181da177e4SLinus Torvalds 		.procname	=	"max_size",
52194990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
52201da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52211da177e4SLinus Torvalds 		.mode		=	0644,
52226d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
52231da177e4SLinus Torvalds 	},
52241da177e4SLinus Torvalds 	{
52251da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
52264990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
52271da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52281da177e4SLinus Torvalds 		.mode		=	0644,
52296d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
52301da177e4SLinus Torvalds 	},
52311da177e4SLinus Torvalds 	{
52321da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
52334990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
52341da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52351da177e4SLinus Torvalds 		.mode		=	0644,
52366d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
52371da177e4SLinus Torvalds 	},
52381da177e4SLinus Torvalds 	{
52391da177e4SLinus Torvalds 		.procname	=	"gc_interval",
52404990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
52411da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52421da177e4SLinus Torvalds 		.mode		=	0644,
52436d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
52441da177e4SLinus Torvalds 	},
52451da177e4SLinus Torvalds 	{
52461da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
52474990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
52481da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52491da177e4SLinus Torvalds 		.mode		=	0644,
5250f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
52511da177e4SLinus Torvalds 	},
52521da177e4SLinus Torvalds 	{
52531da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
52544990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
52551da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52561da177e4SLinus Torvalds 		.mode		=	0644,
52576d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
52581da177e4SLinus Torvalds 	},
52591da177e4SLinus Torvalds 	{
52601da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
52614990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
52621da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52631da177e4SLinus Torvalds 		.mode		=	0644,
5264f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
52651da177e4SLinus Torvalds 	},
52661da177e4SLinus Torvalds 	{
52671da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
52684990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
52691da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52701da177e4SLinus Torvalds 		.mode		=	0644,
52716d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
52721da177e4SLinus Torvalds 	},
52737c6bb7d2SDavid Ahern 	{
52747c6bb7d2SDavid Ahern 		.procname	=	"skip_notify_on_dev_down",
52757c6bb7d2SDavid Ahern 		.data		=	&init_net.ipv6.sysctl.skip_notify_on_dev_down,
52767c6bb7d2SDavid Ahern 		.maxlen		=	sizeof(int),
52777c6bb7d2SDavid Ahern 		.mode		=	0644,
52787c6bb7d2SDavid Ahern 		.proc_handler	=	proc_dointvec,
52797c6bb7d2SDavid Ahern 		.extra1		=	&zero,
52807c6bb7d2SDavid Ahern 		.extra2		=	&one,
52817c6bb7d2SDavid Ahern 	},
5282f8572d8fSEric W. Biederman 	{ }
52831da177e4SLinus Torvalds };
52841da177e4SLinus Torvalds 
52852c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
5286760f2d01SDaniel Lezcano {
5287760f2d01SDaniel Lezcano 	struct ctl_table *table;
5288760f2d01SDaniel Lezcano 
5289760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
5290760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
5291760f2d01SDaniel Lezcano 			GFP_KERNEL);
52925ee09105SYOSHIFUJI Hideaki 
52935ee09105SYOSHIFUJI Hideaki 	if (table) {
52945ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
5295c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
529686393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
52975ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
52985ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
52995ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
53005ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
53015ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
53025ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
53035ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
53049c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
53057c6bb7d2SDavid Ahern 		table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
5306464dc801SEric W. Biederman 
5307464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
5308464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
5309464dc801SEric W. Biederman 			table[0].procname = NULL;
53105ee09105SYOSHIFUJI Hideaki 	}
53115ee09105SYOSHIFUJI Hideaki 
5312760f2d01SDaniel Lezcano 	return table;
5313760f2d01SDaniel Lezcano }
53141da177e4SLinus Torvalds #endif
53151da177e4SLinus Torvalds 
53162c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
5317cdb18761SDaniel Lezcano {
5318633d424bSPavel Emelyanov 	int ret = -ENOMEM;
53198ed67789SDaniel Lezcano 
532086393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
532186393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
5322f2fc6a54SBenjamin Thery 
5323fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5324fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
5325fc66f95cSEric Dumazet 
5326421842edSDavid Ahern 	net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template,
5327421842edSDavid Ahern 					    sizeof(*net->ipv6.fib6_null_entry),
5328421842edSDavid Ahern 					    GFP_KERNEL);
5329421842edSDavid Ahern 	if (!net->ipv6.fib6_null_entry)
5330421842edSDavid Ahern 		goto out_ip6_dst_entries;
5331421842edSDavid Ahern 
53328ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
53338ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
53348ed67789SDaniel Lezcano 					   GFP_KERNEL);
53358ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
5336421842edSDavid Ahern 		goto out_fib6_null_entry;
5337d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
533862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
533962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
53408ed67789SDaniel Lezcano 
53418ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5342feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
53438ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
53448ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
53458ed67789SDaniel Lezcano 					       GFP_KERNEL);
534668fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
534768fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
5348d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
534962fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
535062fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
53518ed67789SDaniel Lezcano 
53528ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
53538ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
53548ed67789SDaniel Lezcano 					       GFP_KERNEL);
535568fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
535668fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
5357d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
535862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
535962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
53608ed67789SDaniel Lezcano #endif
53618ed67789SDaniel Lezcano 
5362b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
5363b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
5364b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5365b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5366b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5367b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5368b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5369b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
53707c6bb7d2SDavid Ahern 	net->ipv6.sysctl.skip_notify_on_dev_down = 0;
5371b339a47cSPeter Zijlstra 
53726891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
53736891a346SBenjamin Thery 
53748ed67789SDaniel Lezcano 	ret = 0;
53758ed67789SDaniel Lezcano out:
53768ed67789SDaniel Lezcano 	return ret;
5377f2fc6a54SBenjamin Thery 
537868fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
537968fffc67SPeter Zijlstra out_ip6_prohibit_entry:
538068fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
538168fffc67SPeter Zijlstra out_ip6_null_entry:
538268fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
538368fffc67SPeter Zijlstra #endif
5384421842edSDavid Ahern out_fib6_null_entry:
5385421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
5386fc66f95cSEric Dumazet out_ip6_dst_entries:
5387fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5388f2fc6a54SBenjamin Thery out_ip6_dst_ops:
5389f2fc6a54SBenjamin Thery 	goto out;
5390cdb18761SDaniel Lezcano }
5391cdb18761SDaniel Lezcano 
53922c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
5393cdb18761SDaniel Lezcano {
5394421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
53958ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
53968ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
53978ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
53988ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
53998ed67789SDaniel Lezcano #endif
540041bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5401cdb18761SDaniel Lezcano }
5402cdb18761SDaniel Lezcano 
5403d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
5404d189634eSThomas Graf {
5405d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5406c3506372SChristoph Hellwig 	proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
5407c3506372SChristoph Hellwig 			sizeof(struct ipv6_route_iter));
54083617d949SChristoph Hellwig 	proc_create_net_single("rt6_stats", 0444, net->proc_net,
54093617d949SChristoph Hellwig 			rt6_stats_seq_show, NULL);
5410d189634eSThomas Graf #endif
5411d189634eSThomas Graf 	return 0;
5412d189634eSThomas Graf }
5413d189634eSThomas Graf 
5414d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
5415d189634eSThomas Graf {
5416d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5417ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
5418ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
5419d189634eSThomas Graf #endif
5420d189634eSThomas Graf }
5421d189634eSThomas Graf 
5422cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
5423cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
5424cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
5425cdb18761SDaniel Lezcano };
5426cdb18761SDaniel Lezcano 
5427c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
5428c3426b47SDavid S. Miller {
5429c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5430c3426b47SDavid S. Miller 
5431c3426b47SDavid S. Miller 	if (!bp)
5432c3426b47SDavid S. Miller 		return -ENOMEM;
5433c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
5434c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
5435c3426b47SDavid S. Miller 	return 0;
5436c3426b47SDavid S. Miller }
5437c3426b47SDavid S. Miller 
5438c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
5439c3426b47SDavid S. Miller {
5440c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
5441c3426b47SDavid S. Miller 
5442c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
544356a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
5444c3426b47SDavid S. Miller 	kfree(bp);
5445c3426b47SDavid S. Miller }
5446c3426b47SDavid S. Miller 
54472b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
5448c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
5449c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
5450c3426b47SDavid S. Miller };
5451c3426b47SDavid S. Miller 
5452d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
5453d189634eSThomas Graf 	.init = ip6_route_net_init_late,
5454d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
5455d189634eSThomas Graf };
5456d189634eSThomas Graf 
54578ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
54588ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
5459242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
54608ed67789SDaniel Lezcano };
54618ed67789SDaniel Lezcano 
54622f460933SWANG Cong void __init ip6_route_init_special_entries(void)
54632f460933SWANG Cong {
54642f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
54652f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
54662f460933SWANG Cong 	 * manually for init_net */
5467ad1601aeSDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_nh.fib_nh_dev = init_net.loopback_dev;
54682f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
54692f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
54702f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
54712f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
54722f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
54732f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
54742f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
54752f460933SWANG Cong   #endif
54762f460933SWANG Cong }
54772f460933SWANG Cong 
5478433d49c3SDaniel Lezcano int __init ip6_route_init(void)
54791da177e4SLinus Torvalds {
5480433d49c3SDaniel Lezcano 	int ret;
54818d0b94afSMartin KaFai Lau 	int cpu;
5482433d49c3SDaniel Lezcano 
54839a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
54849a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
54859a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
54869a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
54879a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
5488c19a28e1SFernando Carrijo 		goto out;
548914e50e57SDavid S. Miller 
5490fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
54918ed67789SDaniel Lezcano 	if (ret)
5492bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
5493bdb3289fSDaniel Lezcano 
5494c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5495c3426b47SDavid S. Miller 	if (ret)
5496e8803b6cSDavid S. Miller 		goto out_dst_entries;
54972a0c451aSThomas Graf 
54987e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
54997e52b33bSDavid S. Miller 	if (ret)
55007e52b33bSDavid S. Miller 		goto out_register_inetpeer;
5501c3426b47SDavid S. Miller 
55025dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
55035dc121e9SArnaud Ebalard 
5504e8803b6cSDavid S. Miller 	ret = fib6_init();
5505433d49c3SDaniel Lezcano 	if (ret)
55068ed67789SDaniel Lezcano 		goto out_register_subsys;
5507433d49c3SDaniel Lezcano 
5508433d49c3SDaniel Lezcano 	ret = xfrm6_init();
5509433d49c3SDaniel Lezcano 	if (ret)
5510e8803b6cSDavid S. Miller 		goto out_fib6_init;
5511c35b7e72SDaniel Lezcano 
5512433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
5513433d49c3SDaniel Lezcano 	if (ret)
5514433d49c3SDaniel Lezcano 		goto xfrm6_init;
55157e5449c2SDaniel Lezcano 
5516d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
5517d189634eSThomas Graf 	if (ret)
5518d189634eSThomas Graf 		goto fib6_rules_init;
5519d189634eSThomas Graf 
552016feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
552116feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
552216feebcfSFlorian Westphal 	if (ret < 0)
552316feebcfSFlorian Westphal 		goto out_register_late_subsys;
552416feebcfSFlorian Westphal 
552516feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
552616feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
552716feebcfSFlorian Westphal 	if (ret < 0)
552816feebcfSFlorian Westphal 		goto out_register_late_subsys;
552916feebcfSFlorian Westphal 
553016feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
553116feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
553216feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
553316feebcfSFlorian Westphal 	if (ret < 0)
5534d189634eSThomas Graf 		goto out_register_late_subsys;
5535433d49c3SDaniel Lezcano 
55368ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
5537cdb18761SDaniel Lezcano 	if (ret)
5538d189634eSThomas Graf 		goto out_register_late_subsys;
55398ed67789SDaniel Lezcano 
55408d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
55418d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
55428d0b94afSMartin KaFai Lau 
55438d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
55448d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
55458d0b94afSMartin KaFai Lau 	}
55468d0b94afSMartin KaFai Lau 
5547433d49c3SDaniel Lezcano out:
5548433d49c3SDaniel Lezcano 	return ret;
5549433d49c3SDaniel Lezcano 
5550d189634eSThomas Graf out_register_late_subsys:
555116feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
5552d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5553433d49c3SDaniel Lezcano fib6_rules_init:
5554433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
5555433d49c3SDaniel Lezcano xfrm6_init:
5556433d49c3SDaniel Lezcano 	xfrm6_fini();
55572a0c451aSThomas Graf out_fib6_init:
55582a0c451aSThomas Graf 	fib6_gc_cleanup();
55598ed67789SDaniel Lezcano out_register_subsys:
55608ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
55617e52b33bSDavid S. Miller out_register_inetpeer:
55627e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
5563fc66f95cSEric Dumazet out_dst_entries:
5564fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5565433d49c3SDaniel Lezcano out_kmem_cache:
5566f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
5567433d49c3SDaniel Lezcano 	goto out;
55681da177e4SLinus Torvalds }
55691da177e4SLinus Torvalds 
55701da177e4SLinus Torvalds void ip6_route_cleanup(void)
55711da177e4SLinus Torvalds {
55728ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
5573d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5574101367c2SThomas Graf 	fib6_rules_cleanup();
55751da177e4SLinus Torvalds 	xfrm6_fini();
55761da177e4SLinus Torvalds 	fib6_gc_cleanup();
5577c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
55788ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
557941bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5580f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
55811da177e4SLinus Torvalds }
5582