xref: /openbmc/linux/net/ipv6/route.c (revision 7c1552da900c159a13473a2106c7547746ebe4a1)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *	FIB front-end.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	Authors:
71da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds /*	Changes:
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI
131da177e4SLinus Torvalds  *		reworked default router selection.
141da177e4SLinus Torvalds  *		- respect outgoing interface
151da177e4SLinus Torvalds  *		- select from (probably) reachable routers (i.e.
161da177e4SLinus Torvalds  *		routers in REACHABLE, STALE, DELAY or PROBE states).
171da177e4SLinus Torvalds  *		- always select the same router if it is (probably)
181da177e4SLinus Torvalds  *		reachable.  otherwise, round-robin the list.
19c0bece9fSYOSHIFUJI Hideaki  *	Ville Nuorvala
20c0bece9fSYOSHIFUJI Hideaki  *		Fixed routing subtrees.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
23f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
24f3213831SJoe Perches 
254fc268d2SRandy Dunlap #include <linux/capability.h>
261da177e4SLinus Torvalds #include <linux/errno.h>
27bc3b2d7fSPaul Gortmaker #include <linux/export.h>
281da177e4SLinus Torvalds #include <linux/types.h>
291da177e4SLinus Torvalds #include <linux/times.h>
301da177e4SLinus Torvalds #include <linux/socket.h>
311da177e4SLinus Torvalds #include <linux/sockios.h>
321da177e4SLinus Torvalds #include <linux/net.h>
331da177e4SLinus Torvalds #include <linux/route.h>
341da177e4SLinus Torvalds #include <linux/netdevice.h>
351da177e4SLinus Torvalds #include <linux/in6.h>
367bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
371da177e4SLinus Torvalds #include <linux/init.h>
381da177e4SLinus Torvalds #include <linux/if_arp.h>
391da177e4SLinus Torvalds #include <linux/proc_fs.h>
401da177e4SLinus Torvalds #include <linux/seq_file.h>
415b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
425a0e3ad6STejun Heo #include <linux/slab.h>
4335732d01SWei Wang #include <linux/jhash.h>
44457c4cbcSEric W. Biederman #include <net/net_namespace.h>
451da177e4SLinus Torvalds #include <net/snmp.h>
461da177e4SLinus Torvalds #include <net/ipv6.h>
471da177e4SLinus Torvalds #include <net/ip6_fib.h>
481da177e4SLinus Torvalds #include <net/ip6_route.h>
491da177e4SLinus Torvalds #include <net/ndisc.h>
501da177e4SLinus Torvalds #include <net/addrconf.h>
511da177e4SLinus Torvalds #include <net/tcp.h>
521da177e4SLinus Torvalds #include <linux/rtnetlink.h>
531da177e4SLinus Torvalds #include <net/dst.h>
54904af04dSJiri Benc #include <net/dst_metadata.h>
551da177e4SLinus Torvalds #include <net/xfrm.h>
568d71740cSTom Tucker #include <net/netevent.h>
5721713ebcSThomas Graf #include <net/netlink.h>
583c618c1dSDavid Ahern #include <net/rtnh.h>
5919e42e45SRoopa Prabhu #include <net/lwtunnel.h>
60904af04dSJiri Benc #include <net/ip_tunnels.h>
61ca254490SDavid Ahern #include <net/l3mdev.h>
62eacb9384SRoopa Prabhu #include <net/ip.h>
637c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
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,
98bd085ef6SHangbin Liu 					   struct sk_buff *skb, u32 mtu,
99bd085ef6SHangbin Liu 					   bool confirm_neigh);
1006700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
1016700c270SDavid S. Miller 					struct sk_buff *skb);
102702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
103702cea56SDavid Ahern 			   int strict);
104a1b7a1f0SDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *f6i);
105d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
1068d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
107d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
10816a16cd3SDavid Ahern 			 int iif, int type, u32 portid, u32 seq,
10916a16cd3SDavid Ahern 			 unsigned int flags);
1107e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
111510e2cedSWei Wang 					   const struct in6_addr *daddr,
112510e2cedSWei Wang 					   const struct in6_addr *saddr);
1131da177e4SLinus Torvalds 
11470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1158d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
116b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
117830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
118830218c1SDavid Ahern 					   struct net_device *dev,
11995c96174SEric Dumazet 					   unsigned int pref);
1208d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
121b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
122830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
123830218c1SDavid Ahern 					   struct net_device *dev);
12470ceb4f5SYOSHIFUJI Hideaki #endif
12570ceb4f5SYOSHIFUJI Hideaki 
1268d0b94afSMartin KaFai Lau struct uncached_list {
1278d0b94afSMartin KaFai Lau 	spinlock_t		lock;
1288d0b94afSMartin KaFai Lau 	struct list_head	head;
1298d0b94afSMartin KaFai Lau };
1308d0b94afSMartin KaFai Lau 
1318d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
1328d0b94afSMartin KaFai Lau 
133510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt)
1348d0b94afSMartin KaFai Lau {
1358d0b94afSMartin KaFai Lau 	struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
1368d0b94afSMartin KaFai Lau 
1378d0b94afSMartin KaFai Lau 	rt->rt6i_uncached_list = ul;
1388d0b94afSMartin KaFai Lau 
1398d0b94afSMartin KaFai Lau 	spin_lock_bh(&ul->lock);
1408d0b94afSMartin KaFai Lau 	list_add_tail(&rt->rt6i_uncached, &ul->head);
1418d0b94afSMartin KaFai Lau 	spin_unlock_bh(&ul->lock);
1428d0b94afSMartin KaFai Lau }
1438d0b94afSMartin KaFai Lau 
144510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt)
1458d0b94afSMartin KaFai Lau {
1468d0b94afSMartin KaFai Lau 	if (!list_empty(&rt->rt6i_uncached)) {
1478d0b94afSMartin KaFai Lau 		struct uncached_list *ul = rt->rt6i_uncached_list;
14881eb8447SWei Wang 		struct net *net = dev_net(rt->dst.dev);
1498d0b94afSMartin KaFai Lau 
1508d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1518d0b94afSMartin KaFai Lau 		list_del(&rt->rt6i_uncached);
15281eb8447SWei Wang 		atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
1538d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1548d0b94afSMartin KaFai Lau 	}
1558d0b94afSMartin KaFai Lau }
1568d0b94afSMartin KaFai Lau 
1578d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
1588d0b94afSMartin KaFai Lau {
1598d0b94afSMartin KaFai Lau 	struct net_device *loopback_dev = net->loopback_dev;
1608d0b94afSMartin KaFai Lau 	int cpu;
1618d0b94afSMartin KaFai Lau 
162e332bc67SEric W. Biederman 	if (dev == loopback_dev)
163e332bc67SEric W. Biederman 		return;
164e332bc67SEric W. Biederman 
1658d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
1668d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
1678d0b94afSMartin KaFai Lau 		struct rt6_info *rt;
1688d0b94afSMartin KaFai Lau 
1698d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1708d0b94afSMartin KaFai Lau 		list_for_each_entry(rt, &ul->head, rt6i_uncached) {
1718d0b94afSMartin KaFai Lau 			struct inet6_dev *rt_idev = rt->rt6i_idev;
1728d0b94afSMartin KaFai Lau 			struct net_device *rt_dev = rt->dst.dev;
1738d0b94afSMartin KaFai Lau 
174e332bc67SEric W. Biederman 			if (rt_idev->dev == dev) {
1758d0b94afSMartin KaFai Lau 				rt->rt6i_idev = in6_dev_get(loopback_dev);
1768d0b94afSMartin KaFai Lau 				in6_dev_put(rt_idev);
1778d0b94afSMartin KaFai Lau 			}
1788d0b94afSMartin KaFai Lau 
179e332bc67SEric W. Biederman 			if (rt_dev == dev) {
1808d7017fdSMahesh Bandewar 				rt->dst.dev = blackhole_netdev;
1818d0b94afSMartin KaFai Lau 				dev_hold(rt->dst.dev);
1828d0b94afSMartin KaFai Lau 				dev_put(rt_dev);
1838d0b94afSMartin KaFai Lau 			}
1848d0b94afSMartin KaFai Lau 		}
1858d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1868d0b94afSMartin KaFai Lau 	}
1878d0b94afSMartin KaFai Lau }
1888d0b94afSMartin KaFai Lau 
189f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p,
190f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
191f894cbf8SDavid S. Miller 					     const void *daddr)
19239232973SDavid S. Miller {
193a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
19439232973SDavid S. Miller 		return (const void *) p;
195f894cbf8SDavid S. Miller 	else if (skb)
196f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
19739232973SDavid S. Miller 	return daddr;
19839232973SDavid S. Miller }
19939232973SDavid S. Miller 
200f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
201f8a1b43bSDavid Ahern 				   struct net_device *dev,
202f894cbf8SDavid S. Miller 				   struct sk_buff *skb,
203f894cbf8SDavid S. Miller 				   const void *daddr)
204d3aaeb38SDavid S. Miller {
20539232973SDavid S. Miller 	struct neighbour *n;
20639232973SDavid S. Miller 
207f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(gw, skb, daddr);
208f8a1b43bSDavid Ahern 	n = __ipv6_neigh_lookup(dev, daddr);
209f83c7790SDavid S. Miller 	if (n)
210f83c7790SDavid S. Miller 		return n;
2117adf3246SStefano Brivio 
2127adf3246SStefano Brivio 	n = neigh_create(&nd_tbl, daddr, dev);
2137adf3246SStefano Brivio 	return IS_ERR(n) ? NULL : n;
214f8a1b43bSDavid Ahern }
215f8a1b43bSDavid Ahern 
216f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
217f8a1b43bSDavid Ahern 					      struct sk_buff *skb,
218f8a1b43bSDavid Ahern 					      const void *daddr)
219f8a1b43bSDavid Ahern {
220f8a1b43bSDavid Ahern 	const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
221f8a1b43bSDavid Ahern 
2222c6b55f4SNicolas Dichtel 	return ip6_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
2232c6b55f4SNicolas Dichtel 				dst->dev, skb, daddr);
224f83c7790SDavid S. Miller }
225f83c7790SDavid S. Miller 
22663fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
22763fca65dSJulian Anastasov {
22863fca65dSJulian Anastasov 	struct net_device *dev = dst->dev;
22963fca65dSJulian Anastasov 	struct rt6_info *rt = (struct rt6_info *)dst;
23063fca65dSJulian Anastasov 
231cbfd6891SStefano Brivio 	daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
23263fca65dSJulian Anastasov 	if (!daddr)
23363fca65dSJulian Anastasov 		return;
23463fca65dSJulian Anastasov 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
23563fca65dSJulian Anastasov 		return;
23663fca65dSJulian Anastasov 	if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
23763fca65dSJulian Anastasov 		return;
23863fca65dSJulian Anastasov 	__ipv6_confirm_neigh(dev, daddr);
23963fca65dSJulian Anastasov }
24063fca65dSJulian Anastasov 
2419a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
2421da177e4SLinus Torvalds 	.family			=	AF_INET6,
2431da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
2441da177e4SLinus Torvalds 	.gc_thresh		=	1024,
2451da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
2460dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
247ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
248d4ead6b3SDavid Ahern 	.cow_metrics		=	dst_cow_metrics_generic,
2491da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
2501da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
2511da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
2521da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
2531da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
2546e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
2559f8955ccSEric W. Biederman 	.local_out		=	__ip6_local_out,
256f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
25763fca65dSJulian Anastasov 	.confirm_neigh		=	ip6_confirm_neigh,
2581da177e4SLinus Torvalds };
2591da177e4SLinus Torvalds 
260ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
261ec831ea7SRoland Dreier {
262618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
263618f9bc7SSteffen Klassert 
264618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
265ec831ea7SRoland Dreier }
266ec831ea7SRoland Dreier 
2676700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
268bd085ef6SHangbin Liu 					 struct sk_buff *skb, u32 mtu,
269bd085ef6SHangbin Liu 					 bool confirm_neigh)
27014e50e57SDavid S. Miller {
27114e50e57SDavid S. Miller }
27214e50e57SDavid S. Miller 
2736700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
2746700c270SDavid S. Miller 				      struct sk_buff *skb)
275b587ee3bSDavid S. Miller {
276b587ee3bSDavid S. Miller }
277b587ee3bSDavid S. Miller 
27814e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
27914e50e57SDavid S. Miller 	.family			=	AF_INET6,
28014e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
28114e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
282ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
283214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
28414e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
285b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2860a1f5962SMartin KaFai Lau 	.cow_metrics		=	dst_cow_metrics_generic,
287f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
28814e50e57SDavid S. Miller };
28914e50e57SDavid S. Miller 
29062fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
29114edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
29262fa8a84SDavid S. Miller };
29362fa8a84SDavid S. Miller 
2948d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = {
29593c2fb25SDavid Ahern 	.fib6_flags	= (RTF_REJECT | RTF_NONEXTHOP),
29693c2fb25SDavid Ahern 	.fib6_protocol  = RTPROT_KERNEL,
29793c2fb25SDavid Ahern 	.fib6_metric	= ~(u32)0,
298f05713e0SEric Dumazet 	.fib6_ref	= REFCOUNT_INIT(1),
299421842edSDavid Ahern 	.fib6_type	= RTN_UNREACHABLE,
300421842edSDavid Ahern 	.fib6_metrics	= (struct dst_metrics *)&dst_default_metrics,
301421842edSDavid Ahern };
302421842edSDavid Ahern 
303fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
3041da177e4SLinus Torvalds 	.dst = {
3051da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
3061da177e4SLinus Torvalds 		.__use		= 1,
3072c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
3081da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
3091da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
3101da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
3111da177e4SLinus Torvalds 	},
3121da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3131da177e4SLinus Torvalds };
3141da177e4SLinus Torvalds 
315101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
316101367c2SThomas Graf 
317fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
318101367c2SThomas Graf 	.dst = {
319101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
320101367c2SThomas Graf 		.__use		= 1,
3212c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
322101367c2SThomas Graf 		.error		= -EACCES,
3239ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
3249ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
325101367c2SThomas Graf 	},
326101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
327101367c2SThomas Graf };
328101367c2SThomas Graf 
329fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
330101367c2SThomas Graf 	.dst = {
331101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
332101367c2SThomas Graf 		.__use		= 1,
3332c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
334101367c2SThomas Graf 		.error		= -EINVAL,
335352e512cSHerbert Xu 		.input		= dst_discard,
336ede2059dSEric W. Biederman 		.output		= dst_discard_out,
337101367c2SThomas Graf 	},
338101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
339101367c2SThomas Graf };
340101367c2SThomas Graf 
341101367c2SThomas Graf #endif
342101367c2SThomas Graf 
343ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt)
344ebfa45f0SMartin KaFai Lau {
345ebfa45f0SMartin KaFai Lau 	struct dst_entry *dst = &rt->dst;
346ebfa45f0SMartin KaFai Lau 
347ebfa45f0SMartin KaFai Lau 	memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
348ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_uncached);
349ebfa45f0SMartin KaFai Lau }
350ebfa45f0SMartin KaFai Lau 
3511da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
35293531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
353ad706862SMartin KaFai Lau 			       int flags)
3541da177e4SLinus Torvalds {
35597bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
356b2a9c0edSWei Wang 					1, DST_OBSOLETE_FORCE_CHK, flags);
357cf911662SDavid S. Miller 
35881eb8447SWei Wang 	if (rt) {
359ebfa45f0SMartin KaFai Lau 		rt6_info_init(rt);
36081eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
36181eb8447SWei Wang 	}
3628104891bSSteffen Klassert 
363cf911662SDavid S. Miller 	return rt;
3641da177e4SLinus Torvalds }
3659ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc);
366d52d3997SMartin KaFai Lau 
3671da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
370a68886a6SDavid Ahern 	struct fib6_info *from;
3718d0b94afSMartin KaFai Lau 	struct inet6_dev *idev;
3721da177e4SLinus Torvalds 
3731620a336SDavid Ahern 	ip_dst_metrics_put(dst);
3748d0b94afSMartin KaFai Lau 	rt6_uncached_list_del(rt);
3758d0b94afSMartin KaFai Lau 
3768d0b94afSMartin KaFai Lau 	idev = rt->rt6i_idev;
37738308473SDavid S. Miller 	if (idev) {
3781da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3791da177e4SLinus Torvalds 		in6_dev_put(idev);
3801da177e4SLinus Torvalds 	}
3811716a961SGao feng 
3820e233874SEric Dumazet 	from = xchg((__force struct fib6_info **)&rt->from, NULL);
38393531c67SDavid Ahern 	fib6_info_release(from);
384b3419363SDavid S. Miller }
385b3419363SDavid S. Miller 
3861da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3871da177e4SLinus Torvalds 			   int how)
3881da177e4SLinus Torvalds {
3891da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3901da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3915a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
392c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3931da177e4SLinus Torvalds 
394e5645f51SWei Wang 	if (idev && idev->dev != loopback_dev) {
395e5645f51SWei Wang 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
39638308473SDavid S. Miller 		if (loopback_idev) {
3971da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
3981da177e4SLinus Torvalds 			in6_dev_put(idev);
3991da177e4SLinus Torvalds 		}
4001da177e4SLinus Torvalds 	}
40197cac082SDavid S. Miller }
4021da177e4SLinus Torvalds 
4035973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt)
4045973fb1eSMartin KaFai Lau {
4055973fb1eSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_EXPIRES)
4065973fb1eSMartin KaFai Lau 		return time_after(jiffies, rt->dst.expires);
4075973fb1eSMartin KaFai Lau 	else
4085973fb1eSMartin KaFai Lau 		return false;
4095973fb1eSMartin KaFai Lau }
4105973fb1eSMartin KaFai Lau 
411a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
4121da177e4SLinus Torvalds {
413a68886a6SDavid Ahern 	struct fib6_info *from;
414a68886a6SDavid Ahern 
415a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
416a68886a6SDavid Ahern 
4171716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
4181716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
419a50feda5SEric Dumazet 			return true;
420a68886a6SDavid Ahern 	} else if (from) {
4211e2ea8adSXin Long 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
422a68886a6SDavid Ahern 			fib6_check_expired(from);
4231716a961SGao feng 	}
424a50feda5SEric Dumazet 	return false;
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
427b1d40991SDavid Ahern void fib6_select_path(const struct net *net, struct fib6_result *res,
428b1d40991SDavid Ahern 		      struct flowi6 *fl6, int oif, bool have_oif_match,
429b1d40991SDavid Ahern 		      const struct sk_buff *skb, int strict)
43051ebd318SNicolas Dichtel {
4318d1c802bSDavid Ahern 	struct fib6_info *sibling, *next_sibling;
432b1d40991SDavid Ahern 	struct fib6_info *match = res->f6i;
433b1d40991SDavid Ahern 
434f88d8ea6SDavid Ahern 	if ((!match->fib6_nsiblings && !match->nh) || have_oif_match)
435b1d40991SDavid Ahern 		goto out;
43651ebd318SNicolas Dichtel 
437b673d6ccSJakub Sitnicki 	/* We might have already computed the hash for ICMPv6 errors. In such
438b673d6ccSJakub Sitnicki 	 * case it will always be non-zero. Otherwise now is the time to do it.
439b673d6ccSJakub Sitnicki 	 */
440f88d8ea6SDavid Ahern 	if (!fl6->mp_hash &&
441f88d8ea6SDavid Ahern 	    (!match->nh || nexthop_is_multipath(match->nh)))
442b4bac172SDavid Ahern 		fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
443b673d6ccSJakub Sitnicki 
444f88d8ea6SDavid Ahern 	if (unlikely(match->nh)) {
445f88d8ea6SDavid Ahern 		nexthop_path_fib6_result(res, fl6->mp_hash);
446f88d8ea6SDavid Ahern 		return;
447f88d8ea6SDavid Ahern 	}
448f88d8ea6SDavid Ahern 
4491cf844c7SDavid Ahern 	if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
450b1d40991SDavid Ahern 		goto out;
451bbfcd776SIdo Schimmel 
45293c2fb25SDavid Ahern 	list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
45393c2fb25SDavid Ahern 				 fib6_siblings) {
4541cf844c7SDavid Ahern 		const struct fib6_nh *nh = sibling->fib6_nh;
4555e670d84SDavid Ahern 		int nh_upper_bound;
4565e670d84SDavid Ahern 
457702cea56SDavid Ahern 		nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
4585e670d84SDavid Ahern 		if (fl6->mp_hash > nh_upper_bound)
4593d709f69SIdo Schimmel 			continue;
460702cea56SDavid Ahern 		if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
46152bd4c0cSNicolas Dichtel 			break;
46251ebd318SNicolas Dichtel 		match = sibling;
46351ebd318SNicolas Dichtel 		break;
46451ebd318SNicolas Dichtel 	}
4653d709f69SIdo Schimmel 
466b1d40991SDavid Ahern out:
467b1d40991SDavid Ahern 	res->f6i = match;
4681cf844c7SDavid Ahern 	res->nh = match->fib6_nh;
46951ebd318SNicolas Dichtel }
47051ebd318SNicolas Dichtel 
4711da177e4SLinus Torvalds /*
47266f5d6ceSWei Wang  *	Route lookup. rcu_read_lock() should be held.
4731da177e4SLinus Torvalds  */
4741da177e4SLinus Torvalds 
4750c59d006SDavid Ahern static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
4760c59d006SDavid Ahern 			       const struct in6_addr *saddr, int oif, int flags)
4770c59d006SDavid Ahern {
4780c59d006SDavid Ahern 	const struct net_device *dev;
4790c59d006SDavid Ahern 
4800c59d006SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD)
4810c59d006SDavid Ahern 		return false;
4820c59d006SDavid Ahern 
4830c59d006SDavid Ahern 	dev = nh->fib_nh_dev;
4840c59d006SDavid Ahern 	if (oif) {
4850c59d006SDavid Ahern 		if (dev->ifindex == oif)
4860c59d006SDavid Ahern 			return true;
4870c59d006SDavid Ahern 	} else {
4880c59d006SDavid Ahern 		if (ipv6_chk_addr(net, saddr, dev,
4890c59d006SDavid Ahern 				  flags & RT6_LOOKUP_F_IFACE))
4900c59d006SDavid Ahern 			return true;
4910c59d006SDavid Ahern 	}
4920c59d006SDavid Ahern 
4930c59d006SDavid Ahern 	return false;
4940c59d006SDavid Ahern }
4950c59d006SDavid Ahern 
496962b6803SDavid Ahern struct fib6_nh_dm_arg {
497962b6803SDavid Ahern 	struct net		*net;
498962b6803SDavid Ahern 	const struct in6_addr	*saddr;
499962b6803SDavid Ahern 	int			oif;
500962b6803SDavid Ahern 	int			flags;
501962b6803SDavid Ahern 	struct fib6_nh		*nh;
502962b6803SDavid Ahern };
503962b6803SDavid Ahern 
504962b6803SDavid Ahern static int __rt6_nh_dev_match(struct fib6_nh *nh, void *_arg)
505962b6803SDavid Ahern {
506962b6803SDavid Ahern 	struct fib6_nh_dm_arg *arg = _arg;
507962b6803SDavid Ahern 
508962b6803SDavid Ahern 	arg->nh = nh;
509962b6803SDavid Ahern 	return __rt6_device_match(arg->net, nh, arg->saddr, arg->oif,
510962b6803SDavid Ahern 				  arg->flags);
511962b6803SDavid Ahern }
512962b6803SDavid Ahern 
513962b6803SDavid Ahern /* returns fib6_nh from nexthop or NULL */
514962b6803SDavid Ahern static struct fib6_nh *rt6_nh_dev_match(struct net *net, struct nexthop *nh,
515962b6803SDavid Ahern 					struct fib6_result *res,
516962b6803SDavid Ahern 					const struct in6_addr *saddr,
517962b6803SDavid Ahern 					int oif, int flags)
518962b6803SDavid Ahern {
519962b6803SDavid Ahern 	struct fib6_nh_dm_arg arg = {
520962b6803SDavid Ahern 		.net   = net,
521962b6803SDavid Ahern 		.saddr = saddr,
522962b6803SDavid Ahern 		.oif   = oif,
523962b6803SDavid Ahern 		.flags = flags,
524962b6803SDavid Ahern 	};
525962b6803SDavid Ahern 
526962b6803SDavid Ahern 	if (nexthop_is_blackhole(nh))
527962b6803SDavid Ahern 		return NULL;
528962b6803SDavid Ahern 
529962b6803SDavid Ahern 	if (nexthop_for_each_fib6_nh(nh, __rt6_nh_dev_match, &arg))
530962b6803SDavid Ahern 		return arg.nh;
531962b6803SDavid Ahern 
532962b6803SDavid Ahern 	return NULL;
533962b6803SDavid Ahern }
534962b6803SDavid Ahern 
53575ef7389SDavid Ahern static void rt6_device_match(struct net *net, struct fib6_result *res,
53675ef7389SDavid Ahern 			     const struct in6_addr *saddr, int oif, int flags)
5371da177e4SLinus Torvalds {
53875ef7389SDavid Ahern 	struct fib6_info *f6i = res->f6i;
53975ef7389SDavid Ahern 	struct fib6_info *spf6i;
54075ef7389SDavid Ahern 	struct fib6_nh *nh;
5411da177e4SLinus Torvalds 
54275ef7389SDavid Ahern 	if (!oif && ipv6_addr_any(saddr)) {
543f88d8ea6SDavid Ahern 		if (unlikely(f6i->nh)) {
544f88d8ea6SDavid Ahern 			nh = nexthop_fib6_nh(f6i->nh);
545f88d8ea6SDavid Ahern 			if (nexthop_is_blackhole(f6i->nh))
546f88d8ea6SDavid Ahern 				goto out_blackhole;
547f88d8ea6SDavid Ahern 		} else {
5481cf844c7SDavid Ahern 			nh = f6i->fib6_nh;
549f88d8ea6SDavid Ahern 		}
5507d21fec9SDavid Ahern 		if (!(nh->fib_nh_flags & RTNH_F_DEAD))
5517d21fec9SDavid Ahern 			goto out;
5521da177e4SLinus Torvalds 	}
5531da177e4SLinus Torvalds 
55475ef7389SDavid Ahern 	for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
555962b6803SDavid Ahern 		bool matched = false;
556962b6803SDavid Ahern 
557962b6803SDavid Ahern 		if (unlikely(spf6i->nh)) {
558962b6803SDavid Ahern 			nh = rt6_nh_dev_match(net, spf6i->nh, res, saddr,
559962b6803SDavid Ahern 					      oif, flags);
560962b6803SDavid Ahern 			if (nh)
561962b6803SDavid Ahern 				matched = true;
562962b6803SDavid Ahern 		} else {
5631cf844c7SDavid Ahern 			nh = spf6i->fib6_nh;
564962b6803SDavid Ahern 			if (__rt6_device_match(net, nh, saddr, oif, flags))
565962b6803SDavid Ahern 				matched = true;
566962b6803SDavid Ahern 		}
567962b6803SDavid Ahern 		if (matched) {
56875ef7389SDavid Ahern 			res->f6i = spf6i;
5697d21fec9SDavid Ahern 			goto out;
57075ef7389SDavid Ahern 		}
57175ef7389SDavid Ahern 	}
5721da177e4SLinus Torvalds 
57375ef7389SDavid Ahern 	if (oif && flags & RT6_LOOKUP_F_IFACE) {
57475ef7389SDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
5751cf844c7SDavid Ahern 		nh = res->f6i->fib6_nh;
5767d21fec9SDavid Ahern 		goto out;
57775ef7389SDavid Ahern 	}
57875ef7389SDavid Ahern 
579f88d8ea6SDavid Ahern 	if (unlikely(f6i->nh)) {
580f88d8ea6SDavid Ahern 		nh = nexthop_fib6_nh(f6i->nh);
581f88d8ea6SDavid Ahern 		if (nexthop_is_blackhole(f6i->nh))
582f88d8ea6SDavid Ahern 			goto out_blackhole;
583f88d8ea6SDavid Ahern 	} else {
5841cf844c7SDavid Ahern 		nh = f6i->fib6_nh;
585f88d8ea6SDavid Ahern 	}
586f88d8ea6SDavid Ahern 
5877d21fec9SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD) {
58875ef7389SDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
5891cf844c7SDavid Ahern 		nh = res->f6i->fib6_nh;
59075ef7389SDavid Ahern 	}
5917d21fec9SDavid Ahern out:
5927d21fec9SDavid Ahern 	res->nh = nh;
5937d21fec9SDavid Ahern 	res->fib6_type = res->f6i->fib6_type;
5947d21fec9SDavid Ahern 	res->fib6_flags = res->f6i->fib6_flags;
595f88d8ea6SDavid Ahern 	return;
596f88d8ea6SDavid Ahern 
597f88d8ea6SDavid Ahern out_blackhole:
598f88d8ea6SDavid Ahern 	res->fib6_flags |= RTF_REJECT;
599f88d8ea6SDavid Ahern 	res->fib6_type = RTN_BLACKHOLE;
600f88d8ea6SDavid Ahern 	res->nh = nh;
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
60327097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
604c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
605c2f17e82SHannes Frederic Sowa 	struct work_struct work;
606c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
607c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
608c2f17e82SHannes Frederic Sowa };
609c2f17e82SHannes Frederic Sowa 
610c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
611c2f17e82SHannes Frederic Sowa {
612c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
613c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
614c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
615c2f17e82SHannes Frederic Sowa 
616c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
617adc176c5SErik Nordmark 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
618c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
619662f5533SMichael Büsch 	kfree(work);
620c2f17e82SHannes Frederic Sowa }
621c2f17e82SHannes Frederic Sowa 
622cc3a86c8SDavid Ahern static void rt6_probe(struct fib6_nh *fib6_nh)
62327097255SYOSHIFUJI Hideaki {
624f547fac6SSabrina Dubroca 	struct __rt6_probe_work *work = NULL;
6255e670d84SDavid Ahern 	const struct in6_addr *nh_gw;
6261bef4c22SEric Dumazet 	unsigned long last_probe;
627f2c31e32SEric Dumazet 	struct neighbour *neigh;
6285e670d84SDavid Ahern 	struct net_device *dev;
629f547fac6SSabrina Dubroca 	struct inet6_dev *idev;
6305e670d84SDavid Ahern 
63127097255SYOSHIFUJI Hideaki 	/*
63227097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
63327097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
63427097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
63527097255SYOSHIFUJI Hideaki 	 *
63627097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
63727097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
63827097255SYOSHIFUJI Hideaki 	 */
639004b3942SHangbin Liu 	if (!fib6_nh->fib_nh_gw_family)
640fdd6681dSAmerigo Wang 		return;
6415e670d84SDavid Ahern 
642cc3a86c8SDavid Ahern 	nh_gw = &fib6_nh->fib_nh_gw6;
643cc3a86c8SDavid Ahern 	dev = fib6_nh->fib_nh_dev;
6442152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
6451bef4c22SEric Dumazet 	last_probe = READ_ONCE(fib6_nh->last_probe);
646f547fac6SSabrina Dubroca 	idev = __in6_dev_get(dev);
6475e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
6482152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
6498d6c31bfSMartin KaFai Lau 		if (neigh->nud_state & NUD_VALID)
6508d6c31bfSMartin KaFai Lau 			goto out;
6518d6c31bfSMartin KaFai Lau 
6522152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
653990edb42SMartin KaFai Lau 		if (!(neigh->nud_state & NUD_VALID) &&
654990edb42SMartin KaFai Lau 		    time_after(jiffies,
655dcd1f572SDavid Ahern 			       neigh->updated + idev->cnf.rtr_probe_interval)) {
656c2f17e82SHannes Frederic Sowa 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
657990edb42SMartin KaFai Lau 			if (work)
6587e980569SJiri Benc 				__neigh_set_probe_once(neigh);
659990edb42SMartin KaFai Lau 		}
660c2f17e82SHannes Frederic Sowa 		write_unlock(&neigh->lock);
6611bef4c22SEric Dumazet 	} else if (time_after(jiffies, last_probe +
662f547fac6SSabrina Dubroca 				       idev->cnf.rtr_probe_interval)) {
663990edb42SMartin KaFai Lau 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
664990edb42SMartin KaFai Lau 	}
665c2f17e82SHannes Frederic Sowa 
6661bef4c22SEric Dumazet 	if (!work || cmpxchg(&fib6_nh->last_probe,
6671bef4c22SEric Dumazet 			     last_probe, jiffies) != last_probe) {
6681bef4c22SEric Dumazet 		kfree(work);
6691bef4c22SEric Dumazet 	} else {
670c2f17e82SHannes Frederic Sowa 		INIT_WORK(&work->work, rt6_probe_deferred);
6715e670d84SDavid Ahern 		work->target = *nh_gw;
6725e670d84SDavid Ahern 		dev_hold(dev);
6735e670d84SDavid Ahern 		work->dev = dev;
674c2f17e82SHannes Frederic Sowa 		schedule_work(&work->work);
675c2f17e82SHannes Frederic Sowa 	}
676990edb42SMartin KaFai Lau 
6778d6c31bfSMartin KaFai Lau out:
6782152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
679f2c31e32SEric Dumazet }
68027097255SYOSHIFUJI Hideaki #else
681cc3a86c8SDavid Ahern static inline void rt6_probe(struct fib6_nh *fib6_nh)
68227097255SYOSHIFUJI Hideaki {
68327097255SYOSHIFUJI Hideaki }
68427097255SYOSHIFUJI Hideaki #endif
68527097255SYOSHIFUJI Hideaki 
6861da177e4SLinus Torvalds /*
687554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
6881da177e4SLinus Torvalds  */
6891ba9a895SDavid Ahern static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
6901da177e4SLinus Torvalds {
691afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
6925e670d84SDavid Ahern 	struct neighbour *neigh;
693f2c31e32SEric Dumazet 
694145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
6951ba9a895SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
6961ba9a895SDavid Ahern 					  &fib6_nh->fib_nh_gw6);
697145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
698145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
699554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
700afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
701398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
702a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
703afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
7047e980569SJiri Benc 		else
7057e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
706398bcbebSYOSHIFUJI Hideaki #endif
707145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
708afc154e9SHannes Frederic Sowa 	} else {
709afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
7107e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
711a5a81f0bSPaul Marks 	}
712145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
713145a3621SYOSHIFUJI Hideaki / 吉藤英明 
714a5a81f0bSPaul Marks 	return ret;
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
717702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
718702cea56SDavid Ahern 			   int strict)
719554cfb7eSYOSHIFUJI Hideaki {
7206e1809a5SDavid Ahern 	int m = 0;
7214d0c5911SYOSHIFUJI Hideaki 
7226e1809a5SDavid Ahern 	if (!oif || nh->fib_nh_dev->ifindex == oif)
7236e1809a5SDavid Ahern 		m = 2;
7246e1809a5SDavid Ahern 
72577d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
726afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
727ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
728702cea56SDavid Ahern 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
729ebacaaa0SYOSHIFUJI Hideaki #endif
7301ba9a895SDavid Ahern 	if ((strict & RT6_LOOKUP_F_REACHABLE) &&
731702cea56SDavid Ahern 	    !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
7321ba9a895SDavid Ahern 		int n = rt6_check_neigh(nh);
733afc154e9SHannes Frederic Sowa 		if (n < 0)
734afc154e9SHannes Frederic Sowa 			return n;
735afc154e9SHannes Frederic Sowa 	}
736554cfb7eSYOSHIFUJI Hideaki 	return m;
737554cfb7eSYOSHIFUJI Hideaki }
738554cfb7eSYOSHIFUJI Hideaki 
73928679ed1SDavid Ahern static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
74028679ed1SDavid Ahern 		       int oif, int strict, int *mpri, bool *do_rr)
741554cfb7eSYOSHIFUJI Hideaki {
742afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
74328679ed1SDavid Ahern 	bool rc = false;
74428679ed1SDavid Ahern 	int m;
74535103d11SAndy Gospodarek 
74628679ed1SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD)
7478067bb8cSIdo Schimmel 		goto out;
7488067bb8cSIdo Schimmel 
74928679ed1SDavid Ahern 	if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
75028679ed1SDavid Ahern 	    nh->fib_nh_flags & RTNH_F_LINKDOWN &&
751d5d32e4bSDavid Ahern 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
75235103d11SAndy Gospodarek 		goto out;
753554cfb7eSYOSHIFUJI Hideaki 
75428679ed1SDavid Ahern 	m = rt6_score_route(nh, fib6_flags, oif, strict);
7557e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
756afc154e9SHannes Frederic Sowa 		match_do_rr = true;
757afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
7587e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
759f11e6659SDavid S. Miller 		goto out;
7601da177e4SLinus Torvalds 	}
761f11e6659SDavid S. Miller 
762afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
76328679ed1SDavid Ahern 		rt6_probe(nh);
764afc154e9SHannes Frederic Sowa 
7657e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
766afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
767afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
768afc154e9SHannes Frederic Sowa 		*mpri = m;
76928679ed1SDavid Ahern 		rc = true;
770afc154e9SHannes Frederic Sowa 	}
771f11e6659SDavid S. Miller out:
77228679ed1SDavid Ahern 	return rc;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
77517a5984eSDavid Ahern struct fib6_nh_frl_arg {
77617a5984eSDavid Ahern 	u32		flags;
77717a5984eSDavid Ahern 	int		oif;
77817a5984eSDavid Ahern 	int		strict;
77917a5984eSDavid Ahern 	int		*mpri;
78017a5984eSDavid Ahern 	bool		*do_rr;
78117a5984eSDavid Ahern 	struct fib6_nh	*nh;
78217a5984eSDavid Ahern };
78317a5984eSDavid Ahern 
78417a5984eSDavid Ahern static int rt6_nh_find_match(struct fib6_nh *nh, void *_arg)
78517a5984eSDavid Ahern {
78617a5984eSDavid Ahern 	struct fib6_nh_frl_arg *arg = _arg;
78717a5984eSDavid Ahern 
78817a5984eSDavid Ahern 	arg->nh = nh;
78917a5984eSDavid Ahern 	return find_match(nh, arg->flags, arg->oif, arg->strict,
79017a5984eSDavid Ahern 			  arg->mpri, arg->do_rr);
79117a5984eSDavid Ahern }
79217a5984eSDavid Ahern 
793b7bc4b6aSDavid Ahern static void __find_rr_leaf(struct fib6_info *f6i_start,
79430c15f03SDavid Ahern 			   struct fib6_info *nomatch, u32 metric,
795b7bc4b6aSDavid Ahern 			   struct fib6_result *res, struct fib6_info **cont,
79630c15f03SDavid Ahern 			   int oif, int strict, bool *do_rr, int *mpri)
79730c15f03SDavid Ahern {
798b7bc4b6aSDavid Ahern 	struct fib6_info *f6i;
79930c15f03SDavid Ahern 
800b7bc4b6aSDavid Ahern 	for (f6i = f6i_start;
801b7bc4b6aSDavid Ahern 	     f6i && f6i != nomatch;
802b7bc4b6aSDavid Ahern 	     f6i = rcu_dereference(f6i->fib6_next)) {
80317a5984eSDavid Ahern 		bool matched = false;
80430c15f03SDavid Ahern 		struct fib6_nh *nh;
80530c15f03SDavid Ahern 
806b7bc4b6aSDavid Ahern 		if (cont && f6i->fib6_metric != metric) {
807b7bc4b6aSDavid Ahern 			*cont = f6i;
80830c15f03SDavid Ahern 			return;
80930c15f03SDavid Ahern 		}
81030c15f03SDavid Ahern 
811b7bc4b6aSDavid Ahern 		if (fib6_check_expired(f6i))
81230c15f03SDavid Ahern 			continue;
81330c15f03SDavid Ahern 
81417a5984eSDavid Ahern 		if (unlikely(f6i->nh)) {
81517a5984eSDavid Ahern 			struct fib6_nh_frl_arg arg = {
81617a5984eSDavid Ahern 				.flags  = f6i->fib6_flags,
81717a5984eSDavid Ahern 				.oif    = oif,
81817a5984eSDavid Ahern 				.strict = strict,
81917a5984eSDavid Ahern 				.mpri   = mpri,
82017a5984eSDavid Ahern 				.do_rr  = do_rr
82117a5984eSDavid Ahern 			};
82217a5984eSDavid Ahern 
82317a5984eSDavid Ahern 			if (nexthop_is_blackhole(f6i->nh)) {
82417a5984eSDavid Ahern 				res->fib6_flags = RTF_REJECT;
82517a5984eSDavid Ahern 				res->fib6_type = RTN_BLACKHOLE;
82617a5984eSDavid Ahern 				res->f6i = f6i;
82717a5984eSDavid Ahern 				res->nh = nexthop_fib6_nh(f6i->nh);
82817a5984eSDavid Ahern 				return;
82917a5984eSDavid Ahern 			}
83017a5984eSDavid Ahern 			if (nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_find_match,
83117a5984eSDavid Ahern 						     &arg)) {
83217a5984eSDavid Ahern 				matched = true;
83317a5984eSDavid Ahern 				nh = arg.nh;
83417a5984eSDavid Ahern 			}
83517a5984eSDavid Ahern 		} else {
8361cf844c7SDavid Ahern 			nh = f6i->fib6_nh;
83717a5984eSDavid Ahern 			if (find_match(nh, f6i->fib6_flags, oif, strict,
83817a5984eSDavid Ahern 				       mpri, do_rr))
83917a5984eSDavid Ahern 				matched = true;
84017a5984eSDavid Ahern 		}
84117a5984eSDavid Ahern 		if (matched) {
842b7bc4b6aSDavid Ahern 			res->f6i = f6i;
843b7bc4b6aSDavid Ahern 			res->nh = nh;
8447d21fec9SDavid Ahern 			res->fib6_flags = f6i->fib6_flags;
8457d21fec9SDavid Ahern 			res->fib6_type = f6i->fib6_type;
846b7bc4b6aSDavid Ahern 		}
84730c15f03SDavid Ahern 	}
84830c15f03SDavid Ahern }
84930c15f03SDavid Ahern 
850b7bc4b6aSDavid Ahern static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
851b7bc4b6aSDavid Ahern 			 struct fib6_info *rr_head, int oif, int strict,
852b7bc4b6aSDavid Ahern 			 bool *do_rr, struct fib6_result *res)
853f11e6659SDavid S. Miller {
854b7bc4b6aSDavid Ahern 	u32 metric = rr_head->fib6_metric;
855b7bc4b6aSDavid Ahern 	struct fib6_info *cont = NULL;
856f11e6659SDavid S. Miller 	int mpri = -1;
857f11e6659SDavid S. Miller 
858b7bc4b6aSDavid Ahern 	__find_rr_leaf(rr_head, NULL, metric, res, &cont,
85930c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
8609fbdcfafSSteffen Klassert 
861b7bc4b6aSDavid Ahern 	__find_rr_leaf(leaf, rr_head, metric, res, &cont,
86230c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
8639fbdcfafSSteffen Klassert 
864b7bc4b6aSDavid Ahern 	if (res->f6i || !cont)
865b7bc4b6aSDavid Ahern 		return;
8669fbdcfafSSteffen Klassert 
867b7bc4b6aSDavid Ahern 	__find_rr_leaf(cont, NULL, metric, res, NULL,
86830c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
869f11e6659SDavid S. Miller }
870f11e6659SDavid S. Miller 
871b7bc4b6aSDavid Ahern static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
872b7bc4b6aSDavid Ahern 		       struct fib6_result *res, int strict)
873f11e6659SDavid S. Miller {
8748d1c802bSDavid Ahern 	struct fib6_info *leaf = rcu_dereference(fn->leaf);
875b7bc4b6aSDavid Ahern 	struct fib6_info *rt0;
876afc154e9SHannes Frederic Sowa 	bool do_rr = false;
87717ecf590SWei Wang 	int key_plen;
878f11e6659SDavid S. Miller 
879b7bc4b6aSDavid Ahern 	/* make sure this function or its helpers sets f6i */
880b7bc4b6aSDavid Ahern 	res->f6i = NULL;
881b7bc4b6aSDavid Ahern 
882421842edSDavid Ahern 	if (!leaf || leaf == net->ipv6.fib6_null_entry)
883b7bc4b6aSDavid Ahern 		goto out;
8848d1040e8SWei Wang 
88566f5d6ceSWei Wang 	rt0 = rcu_dereference(fn->rr_ptr);
886f11e6659SDavid S. Miller 	if (!rt0)
88766f5d6ceSWei Wang 		rt0 = leaf;
888f11e6659SDavid S. Miller 
88917ecf590SWei Wang 	/* Double check to make sure fn is not an intermediate node
89017ecf590SWei Wang 	 * and fn->leaf does not points to its child's leaf
89117ecf590SWei Wang 	 * (This might happen if all routes under fn are deleted from
89217ecf590SWei Wang 	 * the tree and fib6_repair_tree() is called on the node.)
89317ecf590SWei Wang 	 */
89493c2fb25SDavid Ahern 	key_plen = rt0->fib6_dst.plen;
89517ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES
89693c2fb25SDavid Ahern 	if (rt0->fib6_src.plen)
89793c2fb25SDavid Ahern 		key_plen = rt0->fib6_src.plen;
89817ecf590SWei Wang #endif
89917ecf590SWei Wang 	if (fn->fn_bit != key_plen)
900b7bc4b6aSDavid Ahern 		goto out;
90117ecf590SWei Wang 
902b7bc4b6aSDavid Ahern 	find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
903afc154e9SHannes Frederic Sowa 	if (do_rr) {
9048fb11a9aSDavid Ahern 		struct fib6_info *next = rcu_dereference(rt0->fib6_next);
905f11e6659SDavid S. Miller 
906554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
90793c2fb25SDavid Ahern 		if (!next || next->fib6_metric != rt0->fib6_metric)
9088d1040e8SWei Wang 			next = leaf;
909f11e6659SDavid S. Miller 
91066f5d6ceSWei Wang 		if (next != rt0) {
91193c2fb25SDavid Ahern 			spin_lock_bh(&leaf->fib6_table->tb6_lock);
91266f5d6ceSWei Wang 			/* make sure next is not being deleted from the tree */
91393c2fb25SDavid Ahern 			if (next->fib6_node)
91466f5d6ceSWei Wang 				rcu_assign_pointer(fn->rr_ptr, next);
91593c2fb25SDavid Ahern 			spin_unlock_bh(&leaf->fib6_table->tb6_lock);
91666f5d6ceSWei Wang 		}
917554cfb7eSYOSHIFUJI Hideaki 	}
918554cfb7eSYOSHIFUJI Hideaki 
919b7bc4b6aSDavid Ahern out:
920b7bc4b6aSDavid Ahern 	if (!res->f6i) {
921b7bc4b6aSDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
9221cf844c7SDavid Ahern 		res->nh = res->f6i->fib6_nh;
9237d21fec9SDavid Ahern 		res->fib6_flags = res->f6i->fib6_flags;
9247d21fec9SDavid Ahern 		res->fib6_type = res->f6i->fib6_type;
925b7bc4b6aSDavid Ahern 	}
9261da177e4SLinus Torvalds }
9271da177e4SLinus Torvalds 
92885bd05deSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
9298b9df265SMartin KaFai Lau {
93085bd05deSDavid Ahern 	return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
93185bd05deSDavid Ahern 	       res->nh->fib_nh_gw_family;
9328b9df265SMartin KaFai Lau }
9338b9df265SMartin KaFai Lau 
93470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
93570ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
936b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
93770ceb4f5SYOSHIFUJI Hideaki {
938c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
93970ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
94070ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
94170ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
9424bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
9438d1c802bSDavid Ahern 	struct fib6_info *rt;
94470ceb4f5SYOSHIFUJI Hideaki 
94570ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
94670ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
94770ceb4f5SYOSHIFUJI Hideaki 	}
94870ceb4f5SYOSHIFUJI Hideaki 
94970ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
95070ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
95170ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
95270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
95370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
95470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
95570ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
95670ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
95770ceb4f5SYOSHIFUJI Hideaki 		}
95870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
95970ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
96070ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
96170ceb4f5SYOSHIFUJI Hideaki 		}
96270ceb4f5SYOSHIFUJI Hideaki 	}
96370ceb4f5SYOSHIFUJI Hideaki 
96470ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
96570ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
9663933fc95SJens Rosenboom 		return -EINVAL;
96770ceb4f5SYOSHIFUJI Hideaki 
9684bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
96970ceb4f5SYOSHIFUJI Hideaki 
97070ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
97170ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
97270ceb4f5SYOSHIFUJI Hideaki 	else {
97370ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
97470ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
97570ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
97670ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
97770ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
97870ceb4f5SYOSHIFUJI Hideaki 	}
97970ceb4f5SYOSHIFUJI Hideaki 
980f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
981afb1d4b5SDavid Ahern 		rt = rt6_get_dflt_router(net, gwaddr, dev);
982f104a567SDuan Jiong 	else
983f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
984830218c1SDavid Ahern 					gwaddr, dev);
98570ceb4f5SYOSHIFUJI Hideaki 
98670ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
98711dd74b3SRoopa Prabhu 		ip6_del_rt(net, rt, false);
98870ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
98970ceb4f5SYOSHIFUJI Hideaki 	}
99070ceb4f5SYOSHIFUJI Hideaki 
99170ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
992830218c1SDavid Ahern 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
993830218c1SDavid Ahern 					dev, pref);
99470ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
99593c2fb25SDavid Ahern 		rt->fib6_flags = RTF_ROUTEINFO |
99693c2fb25SDavid Ahern 				 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
99770ceb4f5SYOSHIFUJI Hideaki 
99870ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
9991716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
100014895687SDavid Ahern 			fib6_clean_expires(rt);
10011716a961SGao feng 		else
100214895687SDavid Ahern 			fib6_set_expires(rt, jiffies + HZ * lifetime);
10031716a961SGao feng 
100493531c67SDavid Ahern 		fib6_info_release(rt);
100570ceb4f5SYOSHIFUJI Hideaki 	}
100670ceb4f5SYOSHIFUJI Hideaki 	return 0;
100770ceb4f5SYOSHIFUJI Hideaki }
100870ceb4f5SYOSHIFUJI Hideaki #endif
100970ceb4f5SYOSHIFUJI Hideaki 
1010ae90d867SDavid Ahern /*
1011ae90d867SDavid Ahern  *	Misc support functions
1012ae90d867SDavid Ahern  */
1013ae90d867SDavid Ahern 
1014ae90d867SDavid Ahern /* called with rcu_lock held */
10150d161581SDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
1016ae90d867SDavid Ahern {
10170d161581SDavid Ahern 	struct net_device *dev = res->nh->fib_nh_dev;
1018ae90d867SDavid Ahern 
10197d21fec9SDavid Ahern 	if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
1020ae90d867SDavid Ahern 		/* for copies of local routes, dst->dev needs to be the
1021ae90d867SDavid Ahern 		 * device if it is a master device, the master device if
1022ae90d867SDavid Ahern 		 * device is enslaved, and the loopback as the default
1023ae90d867SDavid Ahern 		 */
1024ae90d867SDavid Ahern 		if (netif_is_l3_slave(dev) &&
10257d21fec9SDavid Ahern 		    !rt6_need_strict(&res->f6i->fib6_dst.addr))
1026ae90d867SDavid Ahern 			dev = l3mdev_master_dev_rcu(dev);
1027ae90d867SDavid Ahern 		else if (!netif_is_l3_master(dev))
1028ae90d867SDavid Ahern 			dev = dev_net(dev)->loopback_dev;
1029ae90d867SDavid Ahern 		/* last case is netif_is_l3_master(dev) is true in which
1030ae90d867SDavid Ahern 		 * case we want dev returned to be dev
1031ae90d867SDavid Ahern 		 */
1032ae90d867SDavid Ahern 	}
1033ae90d867SDavid Ahern 
1034ae90d867SDavid Ahern 	return dev;
1035ae90d867SDavid Ahern }
1036ae90d867SDavid Ahern 
10376edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = {
10386edb3c96SDavid Ahern 	[RTN_UNSPEC]	= 0,
10396edb3c96SDavid Ahern 	[RTN_UNICAST]	= 0,
10406edb3c96SDavid Ahern 	[RTN_LOCAL]	= 0,
10416edb3c96SDavid Ahern 	[RTN_BROADCAST]	= 0,
10426edb3c96SDavid Ahern 	[RTN_ANYCAST]	= 0,
10436edb3c96SDavid Ahern 	[RTN_MULTICAST]	= 0,
10446edb3c96SDavid Ahern 	[RTN_BLACKHOLE]	= -EINVAL,
10456edb3c96SDavid Ahern 	[RTN_UNREACHABLE] = -EHOSTUNREACH,
10466edb3c96SDavid Ahern 	[RTN_PROHIBIT]	= -EACCES,
10476edb3c96SDavid Ahern 	[RTN_THROW]	= -EAGAIN,
10486edb3c96SDavid Ahern 	[RTN_NAT]	= -EINVAL,
10496edb3c96SDavid Ahern 	[RTN_XRESOLVE]	= -EINVAL,
10506edb3c96SDavid Ahern };
10516edb3c96SDavid Ahern 
10526edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type)
10536edb3c96SDavid Ahern {
10546edb3c96SDavid Ahern 	return fib6_prop[fib6_type];
10556edb3c96SDavid Ahern }
10566edb3c96SDavid Ahern 
10578d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
10583b6761d1SDavid Ahern {
10593b6761d1SDavid Ahern 	unsigned short flags = 0;
10603b6761d1SDavid Ahern 
10613b6761d1SDavid Ahern 	if (rt->dst_nocount)
10623b6761d1SDavid Ahern 		flags |= DST_NOCOUNT;
10633b6761d1SDavid Ahern 	if (rt->dst_nopolicy)
10643b6761d1SDavid Ahern 		flags |= DST_NOPOLICY;
10653b6761d1SDavid Ahern 
10663b6761d1SDavid Ahern 	return flags;
10673b6761d1SDavid Ahern }
10683b6761d1SDavid Ahern 
10697d21fec9SDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
10706edb3c96SDavid Ahern {
10717d21fec9SDavid Ahern 	rt->dst.error = ip6_rt_type_to_error(fib6_type);
10726edb3c96SDavid Ahern 
10737d21fec9SDavid Ahern 	switch (fib6_type) {
10746edb3c96SDavid Ahern 	case RTN_BLACKHOLE:
10756edb3c96SDavid Ahern 		rt->dst.output = dst_discard_out;
10766edb3c96SDavid Ahern 		rt->dst.input = dst_discard;
10776edb3c96SDavid Ahern 		break;
10786edb3c96SDavid Ahern 	case RTN_PROHIBIT:
10796edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_prohibit_out;
10806edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_prohibit;
10816edb3c96SDavid Ahern 		break;
10826edb3c96SDavid Ahern 	case RTN_THROW:
10836edb3c96SDavid Ahern 	case RTN_UNREACHABLE:
10846edb3c96SDavid Ahern 	default:
10856edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_discard_out;
10866edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_discard;
10876edb3c96SDavid Ahern 		break;
10886edb3c96SDavid Ahern 	}
10896edb3c96SDavid Ahern }
10906edb3c96SDavid Ahern 
10910d161581SDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
10926edb3c96SDavid Ahern {
10937d21fec9SDavid Ahern 	struct fib6_info *f6i = res->f6i;
10940d161581SDavid Ahern 
10957d21fec9SDavid Ahern 	if (res->fib6_flags & RTF_REJECT) {
10967d21fec9SDavid Ahern 		ip6_rt_init_dst_reject(rt, res->fib6_type);
10976edb3c96SDavid Ahern 		return;
10986edb3c96SDavid Ahern 	}
10996edb3c96SDavid Ahern 
11006edb3c96SDavid Ahern 	rt->dst.error = 0;
11016edb3c96SDavid Ahern 	rt->dst.output = ip6_output;
11026edb3c96SDavid Ahern 
11037d21fec9SDavid Ahern 	if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
11046edb3c96SDavid Ahern 		rt->dst.input = ip6_input;
11057d21fec9SDavid Ahern 	} else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
11066edb3c96SDavid Ahern 		rt->dst.input = ip6_mc_input;
11076edb3c96SDavid Ahern 	} else {
11086edb3c96SDavid Ahern 		rt->dst.input = ip6_forward;
11096edb3c96SDavid Ahern 	}
11106edb3c96SDavid Ahern 
11110d161581SDavid Ahern 	if (res->nh->fib_nh_lws) {
11120d161581SDavid Ahern 		rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
11136edb3c96SDavid Ahern 		lwtunnel_set_redirect(&rt->dst);
11146edb3c96SDavid Ahern 	}
11156edb3c96SDavid Ahern 
11166edb3c96SDavid Ahern 	rt->dst.lastuse = jiffies;
11176edb3c96SDavid Ahern }
11186edb3c96SDavid Ahern 
1119e873e4b9SWei Wang /* Caller must already hold reference to @from */
11208d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1121ae90d867SDavid Ahern {
1122ae90d867SDavid Ahern 	rt->rt6i_flags &= ~RTF_EXPIRES;
1123a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, from);
1124e1255ed4SDavid Ahern 	ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
1125ae90d867SDavid Ahern }
1126ae90d867SDavid Ahern 
11270d161581SDavid Ahern /* Caller must already hold reference to f6i in result */
11280d161581SDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
1129ae90d867SDavid Ahern {
11300d161581SDavid Ahern 	const struct fib6_nh *nh = res->nh;
11310d161581SDavid Ahern 	const struct net_device *dev = nh->fib_nh_dev;
11320d161581SDavid Ahern 	struct fib6_info *f6i = res->f6i;
1133dcd1f572SDavid Ahern 
11340d161581SDavid Ahern 	ip6_rt_init_dst(rt, res);
11356edb3c96SDavid Ahern 
11360d161581SDavid Ahern 	rt->rt6i_dst = f6i->fib6_dst;
1137dcd1f572SDavid Ahern 	rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
11387d21fec9SDavid Ahern 	rt->rt6i_flags = res->fib6_flags;
11390d161581SDavid Ahern 	if (nh->fib_nh_gw_family) {
11400d161581SDavid Ahern 		rt->rt6i_gateway = nh->fib_nh_gw6;
11412b2450caSDavid Ahern 		rt->rt6i_flags |= RTF_GATEWAY;
11422b2450caSDavid Ahern 	}
11430d161581SDavid Ahern 	rt6_set_from(rt, f6i);
1144ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
11450d161581SDavid Ahern 	rt->rt6i_src = f6i->fib6_src;
1146ae90d867SDavid Ahern #endif
1147ae90d867SDavid Ahern }
1148ae90d867SDavid Ahern 
1149a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1150a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
1151a3c00e46SMartin KaFai Lau {
115266f5d6ceSWei Wang 	struct fib6_node *pn, *sn;
1153a3c00e46SMartin KaFai Lau 	while (1) {
1154a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
1155a3c00e46SMartin KaFai Lau 			return NULL;
115666f5d6ceSWei Wang 		pn = rcu_dereference(fn->parent);
115766f5d6ceSWei Wang 		sn = FIB6_SUBTREE(pn);
115866f5d6ceSWei Wang 		if (sn && sn != fn)
11596454743bSDavid Ahern 			fn = fib6_node_lookup(sn, NULL, saddr);
1160a3c00e46SMartin KaFai Lau 		else
1161a3c00e46SMartin KaFai Lau 			fn = pn;
1162a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
1163a3c00e46SMartin KaFai Lau 			return fn;
1164a3c00e46SMartin KaFai Lau 	}
1165a3c00e46SMartin KaFai Lau }
1166c71099acSThomas Graf 
116710585b43SDavid Ahern static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1168d3843fe5SWei Wang {
1169d3843fe5SWei Wang 	struct rt6_info *rt = *prt;
1170d3843fe5SWei Wang 
1171d3843fe5SWei Wang 	if (dst_hold_safe(&rt->dst))
1172d3843fe5SWei Wang 		return true;
117310585b43SDavid Ahern 	if (net) {
1174d3843fe5SWei Wang 		rt = net->ipv6.ip6_null_entry;
1175d3843fe5SWei Wang 		dst_hold(&rt->dst);
1176d3843fe5SWei Wang 	} else {
1177d3843fe5SWei Wang 		rt = NULL;
1178d3843fe5SWei Wang 	}
1179d3843fe5SWei Wang 	*prt = rt;
1180d3843fe5SWei Wang 	return false;
1181d3843fe5SWei Wang }
1182d3843fe5SWei Wang 
1183dec9b0e2SDavid Ahern /* called with rcu_lock held */
11849b6b35abSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
1185dec9b0e2SDavid Ahern {
11869b6b35abSDavid Ahern 	struct net_device *dev = res->nh->fib_nh_dev;
11879b6b35abSDavid Ahern 	struct fib6_info *f6i = res->f6i;
11889b6b35abSDavid Ahern 	unsigned short flags;
1189dec9b0e2SDavid Ahern 	struct rt6_info *nrt;
1190dec9b0e2SDavid Ahern 
11919b6b35abSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
11921c87e79aSXin Long 		goto fallback;
1193e873e4b9SWei Wang 
11949b6b35abSDavid Ahern 	flags = fib6_info_dst_flags(f6i);
119593531c67SDavid Ahern 	nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
11961c87e79aSXin Long 	if (!nrt) {
11979b6b35abSDavid Ahern 		fib6_info_release(f6i);
11981c87e79aSXin Long 		goto fallback;
11991c87e79aSXin Long 	}
1200dec9b0e2SDavid Ahern 
12010d161581SDavid Ahern 	ip6_rt_copy_init(nrt, res);
12021c87e79aSXin Long 	return nrt;
12031c87e79aSXin Long 
12041c87e79aSXin Long fallback:
12051c87e79aSXin Long 	nrt = dev_net(dev)->ipv6.ip6_null_entry;
12061c87e79aSXin Long 	dst_hold(&nrt->dst);
1207dec9b0e2SDavid Ahern 	return nrt;
1208dec9b0e2SDavid Ahern }
1209dec9b0e2SDavid Ahern 
12108ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
12118ed67789SDaniel Lezcano 					     struct fib6_table *table,
1212b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
1213b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
1214b75cc8f9SDavid Ahern 					     int flags)
12151da177e4SLinus Torvalds {
1216b1d40991SDavid Ahern 	struct fib6_result res = {};
12171da177e4SLinus Torvalds 	struct fib6_node *fn;
121823fb93a4SDavid Ahern 	struct rt6_info *rt;
12191da177e4SLinus Torvalds 
1220b6cdbc85SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1221b6cdbc85SDavid Ahern 		flags &= ~RT6_LOOKUP_F_IFACE;
1222b6cdbc85SDavid Ahern 
122366f5d6ceSWei Wang 	rcu_read_lock();
12246454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1225c71099acSThomas Graf restart:
1226b1d40991SDavid Ahern 	res.f6i = rcu_dereference(fn->leaf);
1227b1d40991SDavid Ahern 	if (!res.f6i)
1228b1d40991SDavid Ahern 		res.f6i = net->ipv6.fib6_null_entry;
1229af52a52cSDavid Ahern 	else
123075ef7389SDavid Ahern 		rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
123175ef7389SDavid Ahern 				 flags);
1232af52a52cSDavid Ahern 
1233b1d40991SDavid Ahern 	if (res.f6i == net->ipv6.fib6_null_entry) {
1234a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1235a3c00e46SMartin KaFai Lau 		if (fn)
1236a3c00e46SMartin KaFai Lau 			goto restart;
1237af52a52cSDavid Ahern 
1238af52a52cSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
1239af52a52cSDavid Ahern 		dst_hold(&rt->dst);
1240af52a52cSDavid Ahern 		goto out;
1241f88d8ea6SDavid Ahern 	} else if (res.fib6_flags & RTF_REJECT) {
1242f88d8ea6SDavid Ahern 		goto do_create;
1243a3c00e46SMartin KaFai Lau 	}
12442b760fcfSWei Wang 
1245b1d40991SDavid Ahern 	fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1246b1d40991SDavid Ahern 			 fl6->flowi6_oif != 0, skb, flags);
1247b1d40991SDavid Ahern 
12484c9483b2SDavid S. Miller 	/* Search through exception table */
12497e4b5128SDavid Ahern 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
125023fb93a4SDavid Ahern 	if (rt) {
125110585b43SDavid Ahern 		if (ip6_hold_safe(net, &rt))
1252d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
125323fb93a4SDavid Ahern 	} else {
1254f88d8ea6SDavid Ahern do_create:
12559b6b35abSDavid Ahern 		rt = ip6_create_rt_rcu(&res);
1256dec9b0e2SDavid Ahern 	}
1257d3843fe5SWei Wang 
1258af52a52cSDavid Ahern out:
12598ff2e5b2SDavid Ahern 	trace_fib6_table_lookup(net, &res, table, fl6);
1260af52a52cSDavid Ahern 
126166f5d6ceSWei Wang 	rcu_read_unlock();
1262b811580dSDavid Ahern 
12631da177e4SLinus Torvalds 	return rt;
1264c71099acSThomas Graf }
1265c71099acSThomas Graf 
1266ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1267b75cc8f9SDavid Ahern 				   const struct sk_buff *skb, int flags)
1268ea6e574eSFlorian Westphal {
1269b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1270ea6e574eSFlorian Westphal }
1271ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
1272ea6e574eSFlorian Westphal 
12739acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1274b75cc8f9SDavid Ahern 			    const struct in6_addr *saddr, int oif,
1275b75cc8f9SDavid Ahern 			    const struct sk_buff *skb, int strict)
1276c71099acSThomas Graf {
12774c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
12784c9483b2SDavid S. Miller 		.flowi6_oif = oif,
12794c9483b2SDavid S. Miller 		.daddr = *daddr,
1280c71099acSThomas Graf 	};
1281c71099acSThomas Graf 	struct dst_entry *dst;
128277d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1283c71099acSThomas Graf 
1284adaa70bbSThomas Graf 	if (saddr) {
12854c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1286adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1287adaa70bbSThomas Graf 	}
1288adaa70bbSThomas Graf 
1289b75cc8f9SDavid Ahern 	dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1290c71099acSThomas Graf 	if (dst->error == 0)
1291c71099acSThomas Graf 		return (struct rt6_info *) dst;
1292c71099acSThomas Graf 
1293c71099acSThomas Graf 	dst_release(dst);
1294c71099acSThomas Graf 
12951da177e4SLinus Torvalds 	return NULL;
12961da177e4SLinus Torvalds }
12977159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
12987159039aSYOSHIFUJI Hideaki 
1299c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
13001cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
13011cfb71eeSWei Wang  * route is released.
13021cfb71eeSWei Wang  * Caller must hold dst before calling it.
13031da177e4SLinus Torvalds  */
13041da177e4SLinus Torvalds 
13058d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1306333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
13071da177e4SLinus Torvalds {
13081da177e4SLinus Torvalds 	int err;
1309c71099acSThomas Graf 	struct fib6_table *table;
13101da177e4SLinus Torvalds 
131193c2fb25SDavid Ahern 	table = rt->fib6_table;
131266f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
1313d4ead6b3SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, extack);
131466f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 	return err;
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
13198d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt)
132040e22e8fSThomas Graf {
1321afb1d4b5SDavid Ahern 	struct nl_info info = {	.nl_net = net, };
1322e715b6d3SFlorian Westphal 
1323d4ead6b3SDavid Ahern 	return __ip6_ins_rt(rt, &info, NULL);
132440e22e8fSThomas Graf }
132540e22e8fSThomas Graf 
132685bd05deSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
132721efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
1328b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
13291da177e4SLinus Torvalds {
133085bd05deSDavid Ahern 	struct fib6_info *f6i = res->f6i;
13314832c30dSDavid Ahern 	struct net_device *dev;
13321da177e4SLinus Torvalds 	struct rt6_info *rt;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 	/*
13351da177e4SLinus Torvalds 	 *	Clone the route.
13361da177e4SLinus Torvalds 	 */
13371da177e4SLinus Torvalds 
133885bd05deSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
1339e873e4b9SWei Wang 		return NULL;
1340e873e4b9SWei Wang 
13410d161581SDavid Ahern 	dev = ip6_rt_get_dev_rcu(res);
134293531c67SDavid Ahern 	rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1343e873e4b9SWei Wang 	if (!rt) {
134485bd05deSDavid Ahern 		fib6_info_release(f6i);
134583a09abdSMartin KaFai Lau 		return NULL;
1346e873e4b9SWei Wang 	}
134783a09abdSMartin KaFai Lau 
13480d161581SDavid Ahern 	ip6_rt_copy_init(rt, res);
13498b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
135083a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
135183a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
13528b9df265SMartin KaFai Lau 
135385bd05deSDavid Ahern 	if (!rt6_is_gw_or_nonexthop(res)) {
135485bd05deSDavid Ahern 		if (f6i->fib6_dst.plen != 128 &&
135585bd05deSDavid Ahern 		    ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
135658c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
13571da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
13581da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
13594e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
13601da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
13611da177e4SLinus Torvalds 		}
13621da177e4SLinus Torvalds #endif
136395a9a5baSYOSHIFUJI Hideaki 	}
136495a9a5baSYOSHIFUJI Hideaki 
1365299d9939SYOSHIFUJI Hideaki 	return rt;
1366299d9939SYOSHIFUJI Hideaki }
1367299d9939SYOSHIFUJI Hideaki 
1368db3fedeeSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
1369d52d3997SMartin KaFai Lau {
1370db3fedeeSDavid Ahern 	struct fib6_info *f6i = res->f6i;
1371db3fedeeSDavid Ahern 	unsigned short flags = fib6_info_dst_flags(f6i);
13724832c30dSDavid Ahern 	struct net_device *dev;
1373d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1374d52d3997SMartin KaFai Lau 
1375db3fedeeSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
1376e873e4b9SWei Wang 		return NULL;
1377e873e4b9SWei Wang 
13784832c30dSDavid Ahern 	rcu_read_lock();
13790d161581SDavid Ahern 	dev = ip6_rt_get_dev_rcu(res);
1380d8882935SEric Dumazet 	pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags | DST_NOCOUNT);
13814832c30dSDavid Ahern 	rcu_read_unlock();
1382e873e4b9SWei Wang 	if (!pcpu_rt) {
1383db3fedeeSDavid Ahern 		fib6_info_release(f6i);
1384d52d3997SMartin KaFai Lau 		return NULL;
1385e873e4b9SWei Wang 	}
13860d161581SDavid Ahern 	ip6_rt_copy_init(pcpu_rt, res);
1387d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
13888f34e53bSDavid Ahern 
13898f34e53bSDavid Ahern 	if (f6i->nh)
13908f34e53bSDavid Ahern 		pcpu_rt->sernum = rt_genid_ipv6(dev_net(dev));
13918f34e53bSDavid Ahern 
1392d52d3997SMartin KaFai Lau 	return pcpu_rt;
1393d52d3997SMartin KaFai Lau }
1394d52d3997SMartin KaFai Lau 
13958f34e53bSDavid Ahern static bool rt6_is_valid(const struct rt6_info *rt6)
13968f34e53bSDavid Ahern {
13978f34e53bSDavid Ahern 	return rt6->sernum == rt_genid_ipv6(dev_net(rt6->dst.dev));
13988f34e53bSDavid Ahern }
13998f34e53bSDavid Ahern 
140066f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */
1401db3fedeeSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
1402d52d3997SMartin KaFai Lau {
1403c353071aSEric Dumazet 	struct rt6_info *pcpu_rt;
1404d52d3997SMartin KaFai Lau 
1405c353071aSEric Dumazet 	pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
1406d52d3997SMartin KaFai Lau 
14078f34e53bSDavid Ahern 	if (pcpu_rt && pcpu_rt->sernum && !rt6_is_valid(pcpu_rt)) {
14088f34e53bSDavid Ahern 		struct rt6_info *prev, **p;
14098f34e53bSDavid Ahern 
14108f34e53bSDavid Ahern 		p = this_cpu_ptr(res->nh->rt6i_pcpu);
14118f34e53bSDavid Ahern 		prev = xchg(p, NULL);
14128f34e53bSDavid Ahern 		if (prev) {
14138f34e53bSDavid Ahern 			dst_dev_put(&prev->dst);
14148f34e53bSDavid Ahern 			dst_release(&prev->dst);
14158f34e53bSDavid Ahern 		}
14168f34e53bSDavid Ahern 
14178f34e53bSDavid Ahern 		pcpu_rt = NULL;
14188f34e53bSDavid Ahern 	}
14198f34e53bSDavid Ahern 
1420a73e4195SMartin KaFai Lau 	return pcpu_rt;
1421a73e4195SMartin KaFai Lau }
1422a73e4195SMartin KaFai Lau 
1423afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1424db3fedeeSDavid Ahern 					    const struct fib6_result *res)
1425a73e4195SMartin KaFai Lau {
1426a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1427d52d3997SMartin KaFai Lau 
1428db3fedeeSDavid Ahern 	pcpu_rt = ip6_rt_pcpu_alloc(res);
14290e09edccSWei Wang 	if (!pcpu_rt)
14300e09edccSWei Wang 		return NULL;
1431d52d3997SMartin KaFai Lau 
1432f40b6ae2SDavid Ahern 	p = this_cpu_ptr(res->nh->rt6i_pcpu);
1433d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1434951f788aSEric Dumazet 	BUG_ON(prev);
1435a94b9367SWei Wang 
143661fb0d01SEric Dumazet 	if (res->f6i->fib6_destroying) {
143761fb0d01SEric Dumazet 		struct fib6_info *from;
143861fb0d01SEric Dumazet 
143961fb0d01SEric Dumazet 		from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
144061fb0d01SEric Dumazet 		fib6_info_release(from);
144161fb0d01SEric Dumazet 	}
144261fb0d01SEric Dumazet 
1443d52d3997SMartin KaFai Lau 	return pcpu_rt;
1444d52d3997SMartin KaFai Lau }
1445d52d3997SMartin KaFai Lau 
144635732d01SWei Wang /* exception hash table implementation
144735732d01SWei Wang  */
144835732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
144935732d01SWei Wang 
145035732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
145135732d01SWei Wang  * Caller must hold rt6_exception_lock
145235732d01SWei Wang  */
145335732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
145435732d01SWei Wang 				 struct rt6_exception *rt6_ex)
145535732d01SWei Wang {
1456f5b51fe8SPaolo Abeni 	struct fib6_info *from;
1457b2427e67SColin Ian King 	struct net *net;
145881eb8447SWei Wang 
145935732d01SWei Wang 	if (!bucket || !rt6_ex)
146035732d01SWei Wang 		return;
1461b2427e67SColin Ian King 
1462b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
1463f5b51fe8SPaolo Abeni 	net->ipv6.rt6_stats->fib_rt_cache--;
1464f5b51fe8SPaolo Abeni 
1465f5b51fe8SPaolo Abeni 	/* purge completely the exception to allow releasing the held resources:
1466f5b51fe8SPaolo Abeni 	 * some [sk] cache may keep the dst around for unlimited time
1467f5b51fe8SPaolo Abeni 	 */
14680e233874SEric Dumazet 	from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1469f5b51fe8SPaolo Abeni 	fib6_info_release(from);
1470f5b51fe8SPaolo Abeni 	dst_dev_put(&rt6_ex->rt6i->dst);
1471f5b51fe8SPaolo Abeni 
147235732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
147377634cc6SDavid Ahern 	dst_release(&rt6_ex->rt6i->dst);
147435732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
147535732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
147635732d01SWei Wang 	bucket->depth--;
147735732d01SWei Wang }
147835732d01SWei Wang 
147935732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
148035732d01SWei Wang  * Caller must hold rt6_exception_lock
148135732d01SWei Wang  */
148235732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
148335732d01SWei Wang {
148435732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
148535732d01SWei Wang 
148635732d01SWei Wang 	if (!bucket)
148735732d01SWei Wang 		return;
148835732d01SWei Wang 
148935732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
149035732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
149135732d01SWei Wang 			oldest = rt6_ex;
149235732d01SWei Wang 	}
149335732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
149435732d01SWei Wang }
149535732d01SWei Wang 
149635732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
149735732d01SWei Wang 			      const struct in6_addr *src)
149835732d01SWei Wang {
149935732d01SWei Wang 	static u32 seed __read_mostly;
150035732d01SWei Wang 	u32 val;
150135732d01SWei Wang 
150235732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
1503b6b556afSEric Dumazet 	val = jhash2((const u32 *)dst, sizeof(*dst)/sizeof(u32), seed);
150435732d01SWei Wang 
150535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
150635732d01SWei Wang 	if (src)
1507b6b556afSEric Dumazet 		val = jhash2((const u32 *)src, sizeof(*src)/sizeof(u32), val);
150835732d01SWei Wang #endif
150935732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
151035732d01SWei Wang }
151135732d01SWei Wang 
151235732d01SWei Wang /* Helper function to find the cached rt in the hash table
151335732d01SWei Wang  * and update bucket pointer to point to the bucket for this
151435732d01SWei Wang  * (daddr, saddr) pair
151535732d01SWei Wang  * Caller must hold rt6_exception_lock
151635732d01SWei Wang  */
151735732d01SWei Wang static struct rt6_exception *
151835732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
151935732d01SWei Wang 			      const struct in6_addr *daddr,
152035732d01SWei Wang 			      const struct in6_addr *saddr)
152135732d01SWei Wang {
152235732d01SWei Wang 	struct rt6_exception *rt6_ex;
152335732d01SWei Wang 	u32 hval;
152435732d01SWei Wang 
152535732d01SWei Wang 	if (!(*bucket) || !daddr)
152635732d01SWei Wang 		return NULL;
152735732d01SWei Wang 
152835732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
152935732d01SWei Wang 	*bucket += hval;
153035732d01SWei Wang 
153135732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
153235732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
153335732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
153435732d01SWei Wang 
153535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
153635732d01SWei Wang 		if (matched && saddr)
153735732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
153835732d01SWei Wang #endif
153935732d01SWei Wang 		if (matched)
154035732d01SWei Wang 			return rt6_ex;
154135732d01SWei Wang 	}
154235732d01SWei Wang 	return NULL;
154335732d01SWei Wang }
154435732d01SWei Wang 
154535732d01SWei Wang /* Helper function to find the cached rt in the hash table
154635732d01SWei Wang  * and update bucket pointer to point to the bucket for this
154735732d01SWei Wang  * (daddr, saddr) pair
154835732d01SWei Wang  * Caller must hold rcu_read_lock()
154935732d01SWei Wang  */
155035732d01SWei Wang static struct rt6_exception *
155135732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
155235732d01SWei Wang 			 const struct in6_addr *daddr,
155335732d01SWei Wang 			 const struct in6_addr *saddr)
155435732d01SWei Wang {
155535732d01SWei Wang 	struct rt6_exception *rt6_ex;
155635732d01SWei Wang 	u32 hval;
155735732d01SWei Wang 
155835732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
155935732d01SWei Wang 
156035732d01SWei Wang 	if (!(*bucket) || !daddr)
156135732d01SWei Wang 		return NULL;
156235732d01SWei Wang 
156335732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
156435732d01SWei Wang 	*bucket += hval;
156535732d01SWei Wang 
156635732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
156735732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
156835732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
156935732d01SWei Wang 
157035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
157135732d01SWei Wang 		if (matched && saddr)
157235732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
157335732d01SWei Wang #endif
157435732d01SWei Wang 		if (matched)
157535732d01SWei Wang 			return rt6_ex;
157635732d01SWei Wang 	}
157735732d01SWei Wang 	return NULL;
157835732d01SWei Wang }
157935732d01SWei Wang 
1580b748f260SDavid Ahern static unsigned int fib6_mtu(const struct fib6_result *res)
158135732d01SWei Wang {
1582b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
1583d4ead6b3SDavid Ahern 	unsigned int mtu;
1584d4ead6b3SDavid Ahern 
1585b748f260SDavid Ahern 	if (res->f6i->fib6_pmtu) {
1586b748f260SDavid Ahern 		mtu = res->f6i->fib6_pmtu;
1587dcd1f572SDavid Ahern 	} else {
1588b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
1589dcd1f572SDavid Ahern 		struct inet6_dev *idev;
1590dcd1f572SDavid Ahern 
1591dcd1f572SDavid Ahern 		rcu_read_lock();
1592dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
1593dcd1f572SDavid Ahern 		mtu = idev->cnf.mtu6;
1594dcd1f572SDavid Ahern 		rcu_read_unlock();
1595dcd1f572SDavid Ahern 	}
1596dcd1f572SDavid Ahern 
1597d4ead6b3SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1598d4ead6b3SDavid Ahern 
1599b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1600d4ead6b3SDavid Ahern }
1601d4ead6b3SDavid Ahern 
1602cc5c073aSDavid Ahern #define FIB6_EXCEPTION_BUCKET_FLUSHED  0x1UL
1603cc5c073aSDavid Ahern 
1604cc5c073aSDavid Ahern /* used when the flushed bit is not relevant, only access to the bucket
1605cc5c073aSDavid Ahern  * (ie., all bucket users except rt6_insert_exception);
1606cc5c073aSDavid Ahern  *
1607cc5c073aSDavid Ahern  * called under rcu lock; sometimes called with rt6_exception_lock held
1608cc5c073aSDavid Ahern  */
1609cc5c073aSDavid Ahern static
1610cc5c073aSDavid Ahern struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1611cc5c073aSDavid Ahern 						       spinlock_t *lock)
1612cc5c073aSDavid Ahern {
1613cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
1614cc5c073aSDavid Ahern 
1615cc5c073aSDavid Ahern 	if (lock)
1616cc5c073aSDavid Ahern 		bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1617cc5c073aSDavid Ahern 						   lockdep_is_held(lock));
1618cc5c073aSDavid Ahern 	else
1619cc5c073aSDavid Ahern 		bucket = rcu_dereference(nh->rt6i_exception_bucket);
1620cc5c073aSDavid Ahern 
1621cc5c073aSDavid Ahern 	/* remove bucket flushed bit if set */
1622cc5c073aSDavid Ahern 	if (bucket) {
1623cc5c073aSDavid Ahern 		unsigned long p = (unsigned long)bucket;
1624cc5c073aSDavid Ahern 
1625cc5c073aSDavid Ahern 		p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1626cc5c073aSDavid Ahern 		bucket = (struct rt6_exception_bucket *)p;
1627cc5c073aSDavid Ahern 	}
1628cc5c073aSDavid Ahern 
1629cc5c073aSDavid Ahern 	return bucket;
1630cc5c073aSDavid Ahern }
1631cc5c073aSDavid Ahern 
1632cc5c073aSDavid Ahern static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1633cc5c073aSDavid Ahern {
1634cc5c073aSDavid Ahern 	unsigned long p = (unsigned long)bucket;
1635cc5c073aSDavid Ahern 
1636cc5c073aSDavid Ahern 	return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1637cc5c073aSDavid Ahern }
1638cc5c073aSDavid Ahern 
1639cc5c073aSDavid Ahern /* called with rt6_exception_lock held */
1640cc5c073aSDavid Ahern static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1641cc5c073aSDavid Ahern 					      spinlock_t *lock)
1642cc5c073aSDavid Ahern {
1643cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
1644cc5c073aSDavid Ahern 	unsigned long p;
1645cc5c073aSDavid Ahern 
1646cc5c073aSDavid Ahern 	bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1647cc5c073aSDavid Ahern 					   lockdep_is_held(lock));
1648cc5c073aSDavid Ahern 
1649cc5c073aSDavid Ahern 	p = (unsigned long)bucket;
1650cc5c073aSDavid Ahern 	p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1651cc5c073aSDavid Ahern 	bucket = (struct rt6_exception_bucket *)p;
1652cc5c073aSDavid Ahern 	rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1653cc5c073aSDavid Ahern }
1654cc5c073aSDavid Ahern 
165535732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
16565012f0a5SDavid Ahern 				const struct fib6_result *res)
165735732d01SWei Wang {
16585e670d84SDavid Ahern 	struct net *net = dev_net(nrt->dst.dev);
165935732d01SWei Wang 	struct rt6_exception_bucket *bucket;
1660cc5c073aSDavid Ahern 	struct fib6_info *f6i = res->f6i;
166135732d01SWei Wang 	struct in6_addr *src_key = NULL;
166235732d01SWei Wang 	struct rt6_exception *rt6_ex;
1663cc5c073aSDavid Ahern 	struct fib6_nh *nh = res->nh;
166435732d01SWei Wang 	int err = 0;
166535732d01SWei Wang 
166635732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
166735732d01SWei Wang 
1668cc5c073aSDavid Ahern 	bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
166935732d01SWei Wang 					  lockdep_is_held(&rt6_exception_lock));
167035732d01SWei Wang 	if (!bucket) {
167135732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
167235732d01SWei Wang 				 GFP_ATOMIC);
167335732d01SWei Wang 		if (!bucket) {
167435732d01SWei Wang 			err = -ENOMEM;
167535732d01SWei Wang 			goto out;
167635732d01SWei Wang 		}
1677cc5c073aSDavid Ahern 		rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1678cc5c073aSDavid Ahern 	} else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1679cc5c073aSDavid Ahern 		err = -EINVAL;
1680cc5c073aSDavid Ahern 		goto out;
168135732d01SWei Wang 	}
168235732d01SWei Wang 
168335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
16845012f0a5SDavid Ahern 	/* fib6_src.plen != 0 indicates f6i is in subtree
168535732d01SWei Wang 	 * and exception table is indexed by a hash of
16865012f0a5SDavid Ahern 	 * both fib6_dst and fib6_src.
168735732d01SWei Wang 	 * Otherwise, the exception table is indexed by
16885012f0a5SDavid Ahern 	 * a hash of only fib6_dst.
168935732d01SWei Wang 	 */
16905012f0a5SDavid Ahern 	if (f6i->fib6_src.plen)
169135732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
169235732d01SWei Wang #endif
16935012f0a5SDavid Ahern 	/* rt6_mtu_change() might lower mtu on f6i.
1694f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
16955012f0a5SDavid Ahern 	 * is less than f6i's mtu value.
1696f5bbe7eeSWei Wang 	 */
1697b748f260SDavid Ahern 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1698f5bbe7eeSWei Wang 		err = -EINVAL;
1699f5bbe7eeSWei Wang 		goto out;
1700f5bbe7eeSWei Wang 	}
170160006a48SWei Wang 
170235732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
170335732d01SWei Wang 					       src_key);
170435732d01SWei Wang 	if (rt6_ex)
170535732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
170635732d01SWei Wang 
170735732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
170835732d01SWei Wang 	if (!rt6_ex) {
170935732d01SWei Wang 		err = -ENOMEM;
171035732d01SWei Wang 		goto out;
171135732d01SWei Wang 	}
171235732d01SWei Wang 	rt6_ex->rt6i = nrt;
171335732d01SWei Wang 	rt6_ex->stamp = jiffies;
171435732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
171535732d01SWei Wang 	bucket->depth++;
171681eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
171735732d01SWei Wang 
171835732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
171935732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
172035732d01SWei Wang 
172135732d01SWei Wang out:
172235732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
172335732d01SWei Wang 
172435732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1725b886d5f2SPaolo Abeni 	if (!err) {
17265012f0a5SDavid Ahern 		spin_lock_bh(&f6i->fib6_table->tb6_lock);
17275012f0a5SDavid Ahern 		fib6_update_sernum(net, f6i);
17285012f0a5SDavid Ahern 		spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1729b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1730b886d5f2SPaolo Abeni 	}
173135732d01SWei Wang 
173235732d01SWei Wang 	return err;
173335732d01SWei Wang }
173435732d01SWei Wang 
1735c0b220cfSDavid Ahern static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
173635732d01SWei Wang {
173735732d01SWei Wang 	struct rt6_exception_bucket *bucket;
173835732d01SWei Wang 	struct rt6_exception *rt6_ex;
173935732d01SWei Wang 	struct hlist_node *tmp;
174035732d01SWei Wang 	int i;
174135732d01SWei Wang 
174235732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
174335732d01SWei Wang 
1744cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
174535732d01SWei Wang 	if (!bucket)
174635732d01SWei Wang 		goto out;
174735732d01SWei Wang 
1748cc5c073aSDavid Ahern 	/* Prevent rt6_insert_exception() to recreate the bucket list */
1749cc5c073aSDavid Ahern 	if (!from)
1750cc5c073aSDavid Ahern 		fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1751cc5c073aSDavid Ahern 
175235732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1753cc5c073aSDavid Ahern 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1754cc5c073aSDavid Ahern 			if (!from ||
1755cc5c073aSDavid Ahern 			    rcu_access_pointer(rt6_ex->rt6i->from) == from)
175635732d01SWei Wang 				rt6_remove_exception(bucket, rt6_ex);
1757cc5c073aSDavid Ahern 		}
1758cc5c073aSDavid Ahern 		WARN_ON_ONCE(!from && bucket->depth);
175935732d01SWei Wang 		bucket++;
176035732d01SWei Wang 	}
176135732d01SWei Wang out:
176235732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
176335732d01SWei Wang }
176435732d01SWei Wang 
1765e659ba31SDavid Ahern static int rt6_nh_flush_exceptions(struct fib6_nh *nh, void *arg)
1766e659ba31SDavid Ahern {
1767e659ba31SDavid Ahern 	struct fib6_info *f6i = arg;
1768e659ba31SDavid Ahern 
1769e659ba31SDavid Ahern 	fib6_nh_flush_exceptions(nh, f6i);
1770e659ba31SDavid Ahern 
1771e659ba31SDavid Ahern 	return 0;
1772e659ba31SDavid Ahern }
1773e659ba31SDavid Ahern 
1774c0b220cfSDavid Ahern void rt6_flush_exceptions(struct fib6_info *f6i)
1775c0b220cfSDavid Ahern {
1776e659ba31SDavid Ahern 	if (f6i->nh)
1777e659ba31SDavid Ahern 		nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_flush_exceptions,
1778e659ba31SDavid Ahern 					 f6i);
1779e659ba31SDavid Ahern 	else
17801cf844c7SDavid Ahern 		fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1781c0b220cfSDavid Ahern }
1782c0b220cfSDavid Ahern 
178335732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
178435732d01SWei Wang  * Caller has to hold rcu_read_lock()
178535732d01SWei Wang  */
17867e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1787510e2cedSWei Wang 					   const struct in6_addr *daddr,
1788510e2cedSWei Wang 					   const struct in6_addr *saddr)
178935732d01SWei Wang {
1790510e2cedSWei Wang 	const struct in6_addr *src_key = NULL;
179135732d01SWei Wang 	struct rt6_exception_bucket *bucket;
179235732d01SWei Wang 	struct rt6_exception *rt6_ex;
17937e4b5128SDavid Ahern 	struct rt6_info *ret = NULL;
179435732d01SWei Wang 
179535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
17967e4b5128SDavid Ahern 	/* fib6i_src.plen != 0 indicates f6i is in subtree
179735732d01SWei Wang 	 * and exception table is indexed by a hash of
17987e4b5128SDavid Ahern 	 * both fib6_dst and fib6_src.
1799510e2cedSWei Wang 	 * However, the src addr used to create the hash
1800510e2cedSWei Wang 	 * might not be exactly the passed in saddr which
1801510e2cedSWei Wang 	 * is a /128 addr from the flow.
1802510e2cedSWei Wang 	 * So we need to use f6i->fib6_src to redo lookup
1803510e2cedSWei Wang 	 * if the passed in saddr does not find anything.
1804510e2cedSWei Wang 	 * (See the logic in ip6_rt_cache_alloc() on how
1805510e2cedSWei Wang 	 * rt->rt6i_src is updated.)
180635732d01SWei Wang 	 */
18077e4b5128SDavid Ahern 	if (res->f6i->fib6_src.plen)
180835732d01SWei Wang 		src_key = saddr;
1809510e2cedSWei Wang find_ex:
181035732d01SWei Wang #endif
1811cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
181235732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
181335732d01SWei Wang 
181435732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
18157e4b5128SDavid Ahern 		ret = rt6_ex->rt6i;
181635732d01SWei Wang 
1817510e2cedSWei Wang #ifdef CONFIG_IPV6_SUBTREES
1818510e2cedSWei Wang 	/* Use fib6_src as src_key and redo lookup */
1819510e2cedSWei Wang 	if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1820510e2cedSWei Wang 		src_key = &res->f6i->fib6_src.addr;
1821510e2cedSWei Wang 		goto find_ex;
1822510e2cedSWei Wang 	}
1823510e2cedSWei Wang #endif
1824510e2cedSWei Wang 
18257e4b5128SDavid Ahern 	return ret;
182635732d01SWei Wang }
182735732d01SWei Wang 
182835732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
1829cc5c073aSDavid Ahern static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1830c0b220cfSDavid Ahern 				    const struct rt6_info *rt)
183135732d01SWei Wang {
1832c0b220cfSDavid Ahern 	const struct in6_addr *src_key = NULL;
183335732d01SWei Wang 	struct rt6_exception_bucket *bucket;
183435732d01SWei Wang 	struct rt6_exception *rt6_ex;
183535732d01SWei Wang 	int err;
183635732d01SWei Wang 
1837cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
183835732d01SWei Wang 		return -ENOENT;
183935732d01SWei Wang 
184035732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1841cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1842cc5c073aSDavid Ahern 
184335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
1844cc5c073aSDavid Ahern 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
1845cc5c073aSDavid Ahern 	 * and exception table is indexed by a hash of
1846cc5c073aSDavid Ahern 	 * both rt6i_dst and rt6i_src.
184735732d01SWei Wang 	 * Otherwise, the exception table is indexed by
184835732d01SWei Wang 	 * a hash of only rt6i_dst.
184935732d01SWei Wang 	 */
1850c0b220cfSDavid Ahern 	if (plen)
185135732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
185235732d01SWei Wang #endif
185335732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
185435732d01SWei Wang 					       &rt->rt6i_dst.addr,
185535732d01SWei Wang 					       src_key);
185635732d01SWei Wang 	if (rt6_ex) {
185735732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
185835732d01SWei Wang 		err = 0;
185935732d01SWei Wang 	} else {
186035732d01SWei Wang 		err = -ENOENT;
186135732d01SWei Wang 	}
186235732d01SWei Wang 
186335732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
186435732d01SWei Wang 	return err;
186535732d01SWei Wang }
186635732d01SWei Wang 
1867e659ba31SDavid Ahern struct fib6_nh_excptn_arg {
1868e659ba31SDavid Ahern 	struct rt6_info	*rt;
1869e659ba31SDavid Ahern 	int		plen;
1870e659ba31SDavid Ahern };
1871e659ba31SDavid Ahern 
1872e659ba31SDavid Ahern static int rt6_nh_remove_exception_rt(struct fib6_nh *nh, void *_arg)
1873e659ba31SDavid Ahern {
1874e659ba31SDavid Ahern 	struct fib6_nh_excptn_arg *arg = _arg;
1875e659ba31SDavid Ahern 	int err;
1876e659ba31SDavid Ahern 
1877e659ba31SDavid Ahern 	err = fib6_nh_remove_exception(nh, arg->plen, arg->rt);
1878e659ba31SDavid Ahern 	if (err == 0)
1879e659ba31SDavid Ahern 		return 1;
1880e659ba31SDavid Ahern 
1881e659ba31SDavid Ahern 	return 0;
1882e659ba31SDavid Ahern }
1883e659ba31SDavid Ahern 
1884c0b220cfSDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt)
1885c0b220cfSDavid Ahern {
1886c0b220cfSDavid Ahern 	struct fib6_info *from;
1887c0b220cfSDavid Ahern 
1888c0b220cfSDavid Ahern 	from = rcu_dereference(rt->from);
1889cc5c073aSDavid Ahern 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1890c0b220cfSDavid Ahern 		return -EINVAL;
1891c0b220cfSDavid Ahern 
1892e659ba31SDavid Ahern 	if (from->nh) {
1893e659ba31SDavid Ahern 		struct fib6_nh_excptn_arg arg = {
1894e659ba31SDavid Ahern 			.rt = rt,
1895e659ba31SDavid Ahern 			.plen = from->fib6_src.plen
1896e659ba31SDavid Ahern 		};
1897e659ba31SDavid Ahern 		int rc;
1898e659ba31SDavid Ahern 
1899e659ba31SDavid Ahern 		/* rc = 1 means an entry was found */
1900e659ba31SDavid Ahern 		rc = nexthop_for_each_fib6_nh(from->nh,
1901e659ba31SDavid Ahern 					      rt6_nh_remove_exception_rt,
1902e659ba31SDavid Ahern 					      &arg);
1903e659ba31SDavid Ahern 		return rc ? 0 : -ENOENT;
1904e659ba31SDavid Ahern 	}
1905e659ba31SDavid Ahern 
19061cf844c7SDavid Ahern 	return fib6_nh_remove_exception(from->fib6_nh,
1907cc5c073aSDavid Ahern 					from->fib6_src.plen, rt);
1908c0b220cfSDavid Ahern }
1909c0b220cfSDavid Ahern 
191035732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
191135732d01SWei Wang  * refresh its stamp
191235732d01SWei Wang  */
1913cc5c073aSDavid Ahern static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1914c0b220cfSDavid Ahern 				     const struct rt6_info *rt)
191535732d01SWei Wang {
1916c0b220cfSDavid Ahern 	const struct in6_addr *src_key = NULL;
191735732d01SWei Wang 	struct rt6_exception_bucket *bucket;
191835732d01SWei Wang 	struct rt6_exception *rt6_ex;
1919193f3685SPaolo Abeni 
1920cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, NULL);
192135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
1922cc5c073aSDavid Ahern 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
1923cc5c073aSDavid Ahern 	 * and exception table is indexed by a hash of
1924cc5c073aSDavid Ahern 	 * both rt6i_dst and rt6i_src.
192535732d01SWei Wang 	 * Otherwise, the exception table is indexed by
192635732d01SWei Wang 	 * a hash of only rt6i_dst.
192735732d01SWei Wang 	 */
1928c0b220cfSDavid Ahern 	if (plen)
192935732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
193035732d01SWei Wang #endif
1931cc5c073aSDavid Ahern 	rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
193235732d01SWei Wang 	if (rt6_ex)
193335732d01SWei Wang 		rt6_ex->stamp = jiffies;
1934c0b220cfSDavid Ahern }
193535732d01SWei Wang 
1936e659ba31SDavid Ahern struct fib6_nh_match_arg {
1937e659ba31SDavid Ahern 	const struct net_device *dev;
1938e659ba31SDavid Ahern 	const struct in6_addr	*gw;
1939e659ba31SDavid Ahern 	struct fib6_nh		*match;
1940e659ba31SDavid Ahern };
1941e659ba31SDavid Ahern 
1942e659ba31SDavid Ahern /* determine if fib6_nh has given device and gateway */
1943e659ba31SDavid Ahern static int fib6_nh_find_match(struct fib6_nh *nh, void *_arg)
1944e659ba31SDavid Ahern {
1945e659ba31SDavid Ahern 	struct fib6_nh_match_arg *arg = _arg;
1946e659ba31SDavid Ahern 
1947e659ba31SDavid Ahern 	if (arg->dev != nh->fib_nh_dev ||
1948e659ba31SDavid Ahern 	    (arg->gw && !nh->fib_nh_gw_family) ||
1949e659ba31SDavid Ahern 	    (!arg->gw && nh->fib_nh_gw_family) ||
1950e659ba31SDavid Ahern 	    (arg->gw && !ipv6_addr_equal(arg->gw, &nh->fib_nh_gw6)))
1951e659ba31SDavid Ahern 		return 0;
1952e659ba31SDavid Ahern 
1953e659ba31SDavid Ahern 	arg->match = nh;
1954e659ba31SDavid Ahern 
1955e659ba31SDavid Ahern 	/* found a match, break the loop */
1956e659ba31SDavid Ahern 	return 1;
1957e659ba31SDavid Ahern }
1958e659ba31SDavid Ahern 
1959c0b220cfSDavid Ahern static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1960c0b220cfSDavid Ahern {
1961c0b220cfSDavid Ahern 	struct fib6_info *from;
1962e659ba31SDavid Ahern 	struct fib6_nh *fib6_nh;
1963c0b220cfSDavid Ahern 
1964c0b220cfSDavid Ahern 	rcu_read_lock();
1965c0b220cfSDavid Ahern 
1966c0b220cfSDavid Ahern 	from = rcu_dereference(rt->from);
1967c0b220cfSDavid Ahern 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1968c0b220cfSDavid Ahern 		goto unlock;
1969c0b220cfSDavid Ahern 
1970e659ba31SDavid Ahern 	if (from->nh) {
1971e659ba31SDavid Ahern 		struct fib6_nh_match_arg arg = {
1972e659ba31SDavid Ahern 			.dev = rt->dst.dev,
1973e659ba31SDavid Ahern 			.gw = &rt->rt6i_gateway,
1974e659ba31SDavid Ahern 		};
1975e659ba31SDavid Ahern 
1976e659ba31SDavid Ahern 		nexthop_for_each_fib6_nh(from->nh, fib6_nh_find_match, &arg);
1977e659ba31SDavid Ahern 
1978e659ba31SDavid Ahern 		if (!arg.match)
1979cff6a327SDavid Ahern 			goto unlock;
1980e659ba31SDavid Ahern 		fib6_nh = arg.match;
1981e659ba31SDavid Ahern 	} else {
1982e659ba31SDavid Ahern 		fib6_nh = from->fib6_nh;
1983e659ba31SDavid Ahern 	}
1984e659ba31SDavid Ahern 	fib6_nh_update_exception(fib6_nh, from->fib6_src.plen, rt);
1985193f3685SPaolo Abeni unlock:
198635732d01SWei Wang 	rcu_read_unlock();
198735732d01SWei Wang }
198835732d01SWei Wang 
1989e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1990e9fa1495SStefano Brivio 					 struct rt6_info *rt, int mtu)
1991e9fa1495SStefano Brivio {
1992e9fa1495SStefano Brivio 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1993e9fa1495SStefano Brivio 	 * lowest MTU in the path: always allow updating the route PMTU to
1994e9fa1495SStefano Brivio 	 * reflect PMTU decreases.
1995e9fa1495SStefano Brivio 	 *
1996e9fa1495SStefano Brivio 	 * If the new MTU is higher, and the route PMTU is equal to the local
1997e9fa1495SStefano Brivio 	 * MTU, this means the old MTU is the lowest in the path, so allow
1998e9fa1495SStefano Brivio 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1999e9fa1495SStefano Brivio 	 * handle this.
2000e9fa1495SStefano Brivio 	 */
2001e9fa1495SStefano Brivio 
2002e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) >= mtu)
2003e9fa1495SStefano Brivio 		return true;
2004e9fa1495SStefano Brivio 
2005e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
2006e9fa1495SStefano Brivio 		return true;
2007e9fa1495SStefano Brivio 
2008e9fa1495SStefano Brivio 	return false;
2009e9fa1495SStefano Brivio }
2010e9fa1495SStefano Brivio 
2011e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
2012cc5c073aSDavid Ahern 				       const struct fib6_nh *nh, int mtu)
2013f5bbe7eeSWei Wang {
2014f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
2015f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
2016f5bbe7eeSWei Wang 	int i;
2017f5bbe7eeSWei Wang 
2018cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2019e9fa1495SStefano Brivio 	if (!bucket)
2020e9fa1495SStefano Brivio 		return;
2021e9fa1495SStefano Brivio 
2022f5bbe7eeSWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2023f5bbe7eeSWei Wang 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
2024f5bbe7eeSWei Wang 			struct rt6_info *entry = rt6_ex->rt6i;
2025e9fa1495SStefano Brivio 
2026e9fa1495SStefano Brivio 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
2027d4ead6b3SDavid Ahern 			 * route), the metrics of its rt->from have already
2028f5bbe7eeSWei Wang 			 * been updated.
2029f5bbe7eeSWei Wang 			 */
2030d4ead6b3SDavid Ahern 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
2031e9fa1495SStefano Brivio 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
2032d4ead6b3SDavid Ahern 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
2033f5bbe7eeSWei Wang 		}
2034f5bbe7eeSWei Wang 		bucket++;
2035f5bbe7eeSWei Wang 	}
2036f5bbe7eeSWei Wang }
2037f5bbe7eeSWei Wang 
2038b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
2039b16cb459SWei Wang 
2040cc5c073aSDavid Ahern static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
2041cc5c073aSDavid Ahern 					    const struct in6_addr *gateway)
2042b16cb459SWei Wang {
2043b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
2044b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
2045b16cb459SWei Wang 	struct hlist_node *tmp;
2046b16cb459SWei Wang 	int i;
2047b16cb459SWei Wang 
2048cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2049b16cb459SWei Wang 		return;
2050b16cb459SWei Wang 
2051b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
2052cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2053b16cb459SWei Wang 	if (bucket) {
2054b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2055b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
2056b16cb459SWei Wang 						  &bucket->chain, hlist) {
2057b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
2058b16cb459SWei Wang 
2059b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
2060b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
2061b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
2062b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
2063b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
2064b16cb459SWei Wang 				}
2065b16cb459SWei Wang 			}
2066b16cb459SWei Wang 			bucket++;
2067b16cb459SWei Wang 		}
2068b16cb459SWei Wang 	}
2069b16cb459SWei Wang 
2070b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
2071b16cb459SWei Wang }
2072b16cb459SWei Wang 
2073c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
2074c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
2075c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
2076c757faa8SWei Wang 				      unsigned long now)
2077c757faa8SWei Wang {
2078c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
2079c757faa8SWei Wang 
20801859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
20811859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
20821859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
20831859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
20841859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
20851859bac0SPaolo Abeni 	 */
208631afeb42SWei Wang 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
208731afeb42SWei Wang 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
2088c757faa8SWei Wang 			RT6_TRACE("aging clone %p\n", rt);
2089c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
2090c757faa8SWei Wang 			return;
209131afeb42SWei Wang 		}
209231afeb42SWei Wang 	} else if (time_after(jiffies, rt->dst.expires)) {
209331afeb42SWei Wang 		RT6_TRACE("purging expired route %p\n", rt);
209431afeb42SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
209531afeb42SWei Wang 		return;
209631afeb42SWei Wang 	}
209731afeb42SWei Wang 
209831afeb42SWei Wang 	if (rt->rt6i_flags & RTF_GATEWAY) {
2099c757faa8SWei Wang 		struct neighbour *neigh;
2100c757faa8SWei Wang 		__u8 neigh_flags = 0;
2101c757faa8SWei Wang 
21021bfa26ffSEric Dumazet 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
21031bfa26ffSEric Dumazet 		if (neigh)
2104c757faa8SWei Wang 			neigh_flags = neigh->flags;
21051bfa26ffSEric Dumazet 
2106c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
2107c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
2108c757faa8SWei Wang 				  rt);
2109c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
2110c757faa8SWei Wang 			return;
2111c757faa8SWei Wang 		}
2112c757faa8SWei Wang 	}
211331afeb42SWei Wang 
2114c757faa8SWei Wang 	gc_args->more++;
2115c757faa8SWei Wang }
2116c757faa8SWei Wang 
2117cc5c073aSDavid Ahern static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
2118c757faa8SWei Wang 				   struct fib6_gc_args *gc_args,
2119c757faa8SWei Wang 				   unsigned long now)
2120c757faa8SWei Wang {
2121c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
2122c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
2123c757faa8SWei Wang 	struct hlist_node *tmp;
2124c757faa8SWei Wang 	int i;
2125c757faa8SWei Wang 
2126cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
2127c757faa8SWei Wang 		return;
2128c757faa8SWei Wang 
21291bfa26ffSEric Dumazet 	rcu_read_lock_bh();
21301bfa26ffSEric Dumazet 	spin_lock(&rt6_exception_lock);
2131cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
2132c757faa8SWei Wang 	if (bucket) {
2133c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
2134c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
2135c757faa8SWei Wang 						  &bucket->chain, hlist) {
2136c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
2137c757faa8SWei Wang 							  gc_args, now);
2138c757faa8SWei Wang 			}
2139c757faa8SWei Wang 			bucket++;
2140c757faa8SWei Wang 		}
2141c757faa8SWei Wang 	}
21421bfa26ffSEric Dumazet 	spin_unlock(&rt6_exception_lock);
21431bfa26ffSEric Dumazet 	rcu_read_unlock_bh();
2144c757faa8SWei Wang }
2145c757faa8SWei Wang 
2146e659ba31SDavid Ahern struct fib6_nh_age_excptn_arg {
2147e659ba31SDavid Ahern 	struct fib6_gc_args	*gc_args;
2148e659ba31SDavid Ahern 	unsigned long		now;
2149e659ba31SDavid Ahern };
2150e659ba31SDavid Ahern 
2151e659ba31SDavid Ahern static int rt6_nh_age_exceptions(struct fib6_nh *nh, void *_arg)
2152e659ba31SDavid Ahern {
2153e659ba31SDavid Ahern 	struct fib6_nh_age_excptn_arg *arg = _arg;
2154e659ba31SDavid Ahern 
2155e659ba31SDavid Ahern 	fib6_nh_age_exceptions(nh, arg->gc_args, arg->now);
2156e659ba31SDavid Ahern 	return 0;
2157e659ba31SDavid Ahern }
2158e659ba31SDavid Ahern 
2159cc5c073aSDavid Ahern void rt6_age_exceptions(struct fib6_info *f6i,
2160c0b220cfSDavid Ahern 			struct fib6_gc_args *gc_args,
2161c0b220cfSDavid Ahern 			unsigned long now)
2162c0b220cfSDavid Ahern {
2163e659ba31SDavid Ahern 	if (f6i->nh) {
2164e659ba31SDavid Ahern 		struct fib6_nh_age_excptn_arg arg = {
2165e659ba31SDavid Ahern 			.gc_args = gc_args,
2166e659ba31SDavid Ahern 			.now = now
2167e659ba31SDavid Ahern 		};
2168e659ba31SDavid Ahern 
2169e659ba31SDavid Ahern 		nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_age_exceptions,
2170e659ba31SDavid Ahern 					 &arg);
2171e659ba31SDavid Ahern 	} else {
21721cf844c7SDavid Ahern 		fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
2173c0b220cfSDavid Ahern 	}
2174e659ba31SDavid Ahern }
2175c0b220cfSDavid Ahern 
21761d053da9SDavid Ahern /* must be called with rcu lock held */
2177effda4ddSDavid Ahern int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
2178effda4ddSDavid Ahern 		      struct flowi6 *fl6, struct fib6_result *res, int strict)
21791da177e4SLinus Torvalds {
2180367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
21811da177e4SLinus Torvalds 
21826454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2183367efcb9SMartin KaFai Lau 	saved_fn = fn;
21841da177e4SLinus Torvalds 
2185ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
2186ca254490SDavid Ahern 		oif = 0;
2187ca254490SDavid Ahern 
2188a3c00e46SMartin KaFai Lau redo_rt6_select:
2189effda4ddSDavid Ahern 	rt6_select(net, fn, oif, res, strict);
2190effda4ddSDavid Ahern 	if (res->f6i == net->ipv6.fib6_null_entry) {
2191a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2192a3c00e46SMartin KaFai Lau 		if (fn)
2193a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
2194367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
2195367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
2196367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
2197367efcb9SMartin KaFai Lau 			fn = saved_fn;
2198367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
2199367efcb9SMartin KaFai Lau 		}
2200a3c00e46SMartin KaFai Lau 	}
2201a3c00e46SMartin KaFai Lau 
2202effda4ddSDavid Ahern 	trace_fib6_table_lookup(net, res, table, fl6);
2203d52d3997SMartin KaFai Lau 
2204effda4ddSDavid Ahern 	return 0;
22051d053da9SDavid Ahern }
22061d053da9SDavid Ahern 
22071d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
22081d053da9SDavid Ahern 			       int oif, struct flowi6 *fl6,
22091d053da9SDavid Ahern 			       const struct sk_buff *skb, int flags)
22101d053da9SDavid Ahern {
2211b1d40991SDavid Ahern 	struct fib6_result res = {};
22120e09edccSWei Wang 	struct rt6_info *rt = NULL;
22131d053da9SDavid Ahern 	int strict = 0;
22141d053da9SDavid Ahern 
22150e09edccSWei Wang 	WARN_ON_ONCE((flags & RT6_LOOKUP_F_DST_NOREF) &&
22160e09edccSWei Wang 		     !rcu_read_lock_held());
22170e09edccSWei Wang 
22181d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IFACE;
22191d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
22201d053da9SDavid Ahern 	if (net->ipv6.devconf_all->forwarding == 0)
22211d053da9SDavid Ahern 		strict |= RT6_LOOKUP_F_REACHABLE;
22221d053da9SDavid Ahern 
22231d053da9SDavid Ahern 	rcu_read_lock();
22241d053da9SDavid Ahern 
2225effda4ddSDavid Ahern 	fib6_table_lookup(net, table, oif, fl6, &res, strict);
22260e09edccSWei Wang 	if (res.f6i == net->ipv6.fib6_null_entry)
22270e09edccSWei Wang 		goto out;
222823fb93a4SDavid Ahern 
2229b1d40991SDavid Ahern 	fib6_select_path(net, &res, fl6, oif, false, skb, strict);
2230d83009d4SDavid Ahern 
223123fb93a4SDavid Ahern 	/*Search through exception table */
22327e4b5128SDavid Ahern 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
223323fb93a4SDavid Ahern 	if (rt) {
22340e09edccSWei Wang 		goto out;
22353da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
2236b1d40991SDavid Ahern 			    !res.nh->fib_nh_gw_family)) {
22373da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
22383da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
22393da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
22403da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
22413da59bd9SMartin KaFai Lau 		 */
22420e09edccSWei Wang 		rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
22433da59bd9SMartin KaFai Lau 
22440e09edccSWei Wang 		if (rt) {
22450e09edccSWei Wang 			/* 1 refcnt is taken during ip6_rt_cache_alloc().
22460e09edccSWei Wang 			 * As rt6_uncached_list_add() does not consume refcnt,
22470e09edccSWei Wang 			 * this refcnt is always returned to the caller even
22480e09edccSWei Wang 			 * if caller sets RT6_LOOKUP_F_DST_NOREF flag.
22490e09edccSWei Wang 			 */
22500e09edccSWei Wang 			rt6_uncached_list_add(rt);
22510e09edccSWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
22524d85cd0cSDavid Ahern 			rcu_read_unlock();
22533da59bd9SMartin KaFai Lau 
22540e09edccSWei Wang 			return rt;
22551cfb71eeSWei Wang 		}
2256d52d3997SMartin KaFai Lau 	} else {
2257d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
2258951f788aSEric Dumazet 		local_bh_disable();
22590e09edccSWei Wang 		rt = rt6_get_pcpu_route(&res);
2260d52d3997SMartin KaFai Lau 
22610e09edccSWei Wang 		if (!rt)
22620e09edccSWei Wang 			rt = rt6_make_pcpu_route(net, &res);
226393531c67SDavid Ahern 
2264951f788aSEric Dumazet 		local_bh_enable();
22650e09edccSWei Wang 	}
22660e09edccSWei Wang out:
22670e09edccSWei Wang 	if (!rt)
22680e09edccSWei Wang 		rt = net->ipv6.ip6_null_entry;
22690e09edccSWei Wang 	if (!(flags & RT6_LOOKUP_F_DST_NOREF))
22700e09edccSWei Wang 		ip6_hold_safe(net, &rt);
2271951f788aSEric Dumazet 	rcu_read_unlock();
2272d4bea421SDavid Ahern 
22730e09edccSWei Wang 	return rt;
2274c71099acSThomas Graf }
22759ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
2276c71099acSThomas Graf 
2277b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net,
2278b75cc8f9SDavid Ahern 					    struct fib6_table *table,
2279b75cc8f9SDavid Ahern 					    struct flowi6 *fl6,
2280b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2281b75cc8f9SDavid Ahern 					    int flags)
22824acad72dSPavel Emelyanov {
2283b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
22844acad72dSPavel Emelyanov }
22854acad72dSPavel Emelyanov 
2286d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
228772331bc0SShmulik Ladkani 					 struct net_device *dev,
2288b75cc8f9SDavid Ahern 					 struct flowi6 *fl6,
2289b75cc8f9SDavid Ahern 					 const struct sk_buff *skb,
2290b75cc8f9SDavid Ahern 					 int flags)
229172331bc0SShmulik Ladkani {
229272331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
229372331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
229472331bc0SShmulik Ladkani 
2295b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
229672331bc0SShmulik Ladkani }
2297d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
229872331bc0SShmulik Ladkani 
229923aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
23005e5d6fedSRoopa Prabhu 				  struct flow_keys *keys,
23015e5d6fedSRoopa Prabhu 				  struct flow_keys *flkeys)
230223aebdacSJakub Sitnicki {
230323aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
230423aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
23055e5d6fedSRoopa Prabhu 	struct flow_keys *_flkeys = flkeys;
230623aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
230723aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
230823aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
2309cea67a2dSEric Dumazet 	struct icmp6hdr _icmph;
231023aebdacSJakub Sitnicki 
231123aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
231223aebdacSJakub Sitnicki 		goto out;
231323aebdacSJakub Sitnicki 
2314cea67a2dSEric Dumazet 	icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2315cea67a2dSEric Dumazet 				   sizeof(_icmph), &_icmph);
2316cea67a2dSEric Dumazet 	if (!icmph)
2317cea67a2dSEric Dumazet 		goto out;
2318cea67a2dSEric Dumazet 
231954074f1dSMatteo Croce 	if (!icmpv6_is_err(icmph->icmp6_type))
232023aebdacSJakub Sitnicki 		goto out;
232123aebdacSJakub Sitnicki 
232223aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
232323aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
232423aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
232523aebdacSJakub Sitnicki 	if (!inner_iph)
232623aebdacSJakub Sitnicki 		goto out;
232723aebdacSJakub Sitnicki 
232823aebdacSJakub Sitnicki 	key_iph = inner_iph;
23295e5d6fedSRoopa Prabhu 	_flkeys = NULL;
233023aebdacSJakub Sitnicki out:
23315e5d6fedSRoopa Prabhu 	if (_flkeys) {
23325e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
23335e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
23345e5d6fedSRoopa Prabhu 		keys->tags.flow_label = _flkeys->tags.flow_label;
23355e5d6fedSRoopa Prabhu 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
23365e5d6fedSRoopa Prabhu 	} else {
233723aebdacSJakub Sitnicki 		keys->addrs.v6addrs.src = key_iph->saddr;
233823aebdacSJakub Sitnicki 		keys->addrs.v6addrs.dst = key_iph->daddr;
2339fa1be7e0SMichal Kubecek 		keys->tags.flow_label = ip6_flowlabel(key_iph);
234023aebdacSJakub Sitnicki 		keys->basic.ip_proto = key_iph->nexthdr;
234123aebdacSJakub Sitnicki 	}
23425e5d6fedSRoopa Prabhu }
234323aebdacSJakub Sitnicki 
234423aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
2345b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2346b4bac172SDavid Ahern 		       const struct sk_buff *skb, struct flow_keys *flkeys)
234723aebdacSJakub Sitnicki {
234823aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
23499a2a537aSDavid Ahern 	u32 mhash;
235023aebdacSJakub Sitnicki 
2351bbfa047aSDavid S. Miller 	switch (ip6_multipath_hash_policy(net)) {
2352b4bac172SDavid Ahern 	case 0:
23536f74b6c2SDavid Ahern 		memset(&hash_keys, 0, sizeof(hash_keys));
23546f74b6c2SDavid Ahern 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
23559a2a537aSDavid Ahern 		if (skb) {
23565e5d6fedSRoopa Prabhu 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
23579a2a537aSDavid Ahern 		} else {
23589a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
23599a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2360fa1be7e0SMichal Kubecek 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
23619a2a537aSDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
236223aebdacSJakub Sitnicki 		}
2363b4bac172SDavid Ahern 		break;
2364b4bac172SDavid Ahern 	case 1:
2365b4bac172SDavid Ahern 		if (skb) {
2366b4bac172SDavid Ahern 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2367b4bac172SDavid Ahern 			struct flow_keys keys;
2368b4bac172SDavid Ahern 
2369b4bac172SDavid Ahern 			/* short-circuit if we already have L4 hash present */
2370b4bac172SDavid Ahern 			if (skb->l4_hash)
2371b4bac172SDavid Ahern 				return skb_get_hash_raw(skb) >> 1;
2372b4bac172SDavid Ahern 
2373b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2374b4bac172SDavid Ahern 
2375b4bac172SDavid Ahern                         if (!flkeys) {
2376b4bac172SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2377b4bac172SDavid Ahern 				flkeys = &keys;
2378b4bac172SDavid Ahern 			}
2379b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2380b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2381b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2382b4bac172SDavid Ahern 			hash_keys.ports.src = flkeys->ports.src;
2383b4bac172SDavid Ahern 			hash_keys.ports.dst = flkeys->ports.dst;
2384b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2385b4bac172SDavid Ahern 		} else {
2386b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2387b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2388b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2389b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2390b4bac172SDavid Ahern 			hash_keys.ports.src = fl6->fl6_sport;
2391b4bac172SDavid Ahern 			hash_keys.ports.dst = fl6->fl6_dport;
2392b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2393b4bac172SDavid Ahern 		}
2394b4bac172SDavid Ahern 		break;
2395d8f74f09SStephen Suryaputra 	case 2:
2396d8f74f09SStephen Suryaputra 		memset(&hash_keys, 0, sizeof(hash_keys));
2397d8f74f09SStephen Suryaputra 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2398d8f74f09SStephen Suryaputra 		if (skb) {
2399d8f74f09SStephen Suryaputra 			struct flow_keys keys;
2400d8f74f09SStephen Suryaputra 
2401d8f74f09SStephen Suryaputra 			if (!flkeys) {
2402d8f74f09SStephen Suryaputra 				skb_flow_dissect_flow_keys(skb, &keys, 0);
2403d8f74f09SStephen Suryaputra 				flkeys = &keys;
2404d8f74f09SStephen Suryaputra 			}
2405d8f74f09SStephen Suryaputra 
2406d8f74f09SStephen Suryaputra 			/* Inner can be v4 or v6 */
2407d8f74f09SStephen Suryaputra 			if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2408d8f74f09SStephen Suryaputra 				hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2409d8f74f09SStephen Suryaputra 				hash_keys.addrs.v4addrs.src = flkeys->addrs.v4addrs.src;
2410d8f74f09SStephen Suryaputra 				hash_keys.addrs.v4addrs.dst = flkeys->addrs.v4addrs.dst;
2411d8f74f09SStephen Suryaputra 			} else if (flkeys->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2412d8f74f09SStephen Suryaputra 				hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2413d8f74f09SStephen Suryaputra 				hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2414d8f74f09SStephen Suryaputra 				hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2415d8f74f09SStephen Suryaputra 				hash_keys.tags.flow_label = flkeys->tags.flow_label;
2416d8f74f09SStephen Suryaputra 				hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2417d8f74f09SStephen Suryaputra 			} else {
2418d8f74f09SStephen Suryaputra 				/* Same as case 0 */
2419d8f74f09SStephen Suryaputra 				hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2420d8f74f09SStephen Suryaputra 				ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
2421d8f74f09SStephen Suryaputra 			}
2422d8f74f09SStephen Suryaputra 		} else {
2423d8f74f09SStephen Suryaputra 			/* Same as case 0 */
2424d8f74f09SStephen Suryaputra 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2425d8f74f09SStephen Suryaputra 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2426d8f74f09SStephen Suryaputra 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2427d8f74f09SStephen Suryaputra 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
2428d8f74f09SStephen Suryaputra 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2429d8f74f09SStephen Suryaputra 		}
2430d8f74f09SStephen Suryaputra 		break;
2431b4bac172SDavid Ahern 	}
24329a2a537aSDavid Ahern 	mhash = flow_hash_from_keys(&hash_keys);
243323aebdacSJakub Sitnicki 
24349a2a537aSDavid Ahern 	return mhash >> 1;
243523aebdacSJakub Sitnicki }
243623aebdacSJakub Sitnicki 
243767f415ddSWei Wang /* Called with rcu held */
2438c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
2439c71099acSThomas Graf {
2440b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2441c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
244267f415ddSWei Wang 	int flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_DST_NOREF;
2443904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
24444c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
2445e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
24464c9483b2SDavid S. Miller 		.daddr = iph->daddr,
24474c9483b2SDavid S. Miller 		.saddr = iph->saddr,
24486502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
24494c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
24504c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
2451c71099acSThomas Graf 	};
24525e5d6fedSRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2453adaa70bbSThomas Graf 
2454904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
245546fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2456904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
24575e5d6fedSRoopa Prabhu 
24585e5d6fedSRoopa Prabhu 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
24595e5d6fedSRoopa Prabhu 		flkeys = &_flkeys;
24605e5d6fedSRoopa Prabhu 
246123aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2462b4bac172SDavid Ahern 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
246306e9d040SJiri Benc 	skb_dst_drop(skb);
246467f415ddSWei Wang 	skb_dst_set_noref(skb, ip6_route_input_lookup(net, skb->dev,
246567f415ddSWei Wang 						      &fl6, skb, flags));
2466c71099acSThomas Graf }
2467c71099acSThomas Graf 
2468b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net,
2469b75cc8f9SDavid Ahern 					     struct fib6_table *table,
2470b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
2471b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2472b75cc8f9SDavid Ahern 					     int flags)
2473c71099acSThomas Graf {
2474b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2475c71099acSThomas Graf }
2476c71099acSThomas Graf 
24777d9e5f42SWei Wang struct dst_entry *ip6_route_output_flags_noref(struct net *net,
24787d9e5f42SWei Wang 					       const struct sock *sk,
24796f21c96aSPaolo Abeni 					       struct flowi6 *fl6, int flags)
2480c71099acSThomas Graf {
2481d46a9d67SDavid Ahern 	bool any_src;
2482c71099acSThomas Graf 
24833ede0bbcSRobert Shearman 	if (ipv6_addr_type(&fl6->daddr) &
24843ede0bbcSRobert Shearman 	    (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
24854c1feac5SDavid Ahern 		struct dst_entry *dst;
24864c1feac5SDavid Ahern 
24877d9e5f42SWei Wang 		/* This function does not take refcnt on the dst */
24884c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
2489ca254490SDavid Ahern 		if (dst)
2490ca254490SDavid Ahern 			return dst;
24914c1feac5SDavid Ahern 	}
2492ca254490SDavid Ahern 
24931fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
24944dc27d1cSDavid McCullough 
24957d9e5f42SWei Wang 	flags |= RT6_LOOKUP_F_DST_NOREF;
2496d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
2497741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2498d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
249977d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
2500c71099acSThomas Graf 
2501d46a9d67SDavid Ahern 	if (!any_src)
2502adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
25030c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
25040c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2505adaa70bbSThomas Graf 
2506b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
25071da177e4SLinus Torvalds }
25087d9e5f42SWei Wang EXPORT_SYMBOL_GPL(ip6_route_output_flags_noref);
25097d9e5f42SWei Wang 
25107d9e5f42SWei Wang struct dst_entry *ip6_route_output_flags(struct net *net,
25117d9e5f42SWei Wang 					 const struct sock *sk,
25127d9e5f42SWei Wang 					 struct flowi6 *fl6,
25137d9e5f42SWei Wang 					 int flags)
25147d9e5f42SWei Wang {
25157d9e5f42SWei Wang         struct dst_entry *dst;
25167d9e5f42SWei Wang         struct rt6_info *rt6;
25177d9e5f42SWei Wang 
25187d9e5f42SWei Wang         rcu_read_lock();
25197d9e5f42SWei Wang         dst = ip6_route_output_flags_noref(net, sk, fl6, flags);
25207d9e5f42SWei Wang         rt6 = (struct rt6_info *)dst;
25217d9e5f42SWei Wang         /* For dst cached in uncached_list, refcnt is already taken. */
25227d9e5f42SWei Wang         if (list_empty(&rt6->rt6i_uncached) && !dst_hold_safe(dst)) {
25237d9e5f42SWei Wang                 dst = &net->ipv6.ip6_null_entry->dst;
25247d9e5f42SWei Wang                 dst_hold(dst);
25257d9e5f42SWei Wang         }
25267d9e5f42SWei Wang         rcu_read_unlock();
25277d9e5f42SWei Wang 
25287d9e5f42SWei Wang         return dst;
25297d9e5f42SWei Wang }
25306f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
25311da177e4SLinus Torvalds 
25322774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
253314e50e57SDavid S. Miller {
25345c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
25351dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
253614e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
253714e50e57SDavid S. Miller 
25381dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
253962cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
254014e50e57SDavid S. Miller 	if (rt) {
25410a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
254281eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
25430a1f5962SMartin KaFai Lau 
2544d8d1f30bSChangli Gao 		new = &rt->dst;
254514e50e57SDavid S. Miller 		new->__use = 1;
2546352e512cSHerbert Xu 		new->input = dst_discard;
2547ede2059dSEric W. Biederman 		new->output = dst_discard_out;
254814e50e57SDavid S. Miller 
2549defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
255014e50e57SDavid S. Miller 
25511dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
25524e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
25530a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
255414e50e57SDavid S. Miller 
255514e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
255614e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
255714e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
255814e50e57SDavid S. Miller #endif
255914e50e57SDavid S. Miller 	}
256014e50e57SDavid S. Miller 
256169ead7afSDavid S. Miller 	dst_release(dst_orig);
256269ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
256314e50e57SDavid S. Miller }
256414e50e57SDavid S. Miller 
25651da177e4SLinus Torvalds /*
25661da177e4SLinus Torvalds  *	Destination cache support functions
25671da177e4SLinus Torvalds  */
25681da177e4SLinus Torvalds 
25698d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie)
25703da59bd9SMartin KaFai Lau {
257136143645SSteffen Klassert 	u32 rt_cookie = 0;
2572c5cff856SWei Wang 
25738ae86971SDavid Ahern 	if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
257493531c67SDavid Ahern 		return false;
257593531c67SDavid Ahern 
257693531c67SDavid Ahern 	if (fib6_check_expired(f6i))
257793531c67SDavid Ahern 		return false;
257893531c67SDavid Ahern 
257993531c67SDavid Ahern 	return true;
258093531c67SDavid Ahern }
258193531c67SDavid Ahern 
2582a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt,
2583a68886a6SDavid Ahern 				   struct fib6_info *from,
2584a68886a6SDavid Ahern 				   u32 cookie)
25853da59bd9SMartin KaFai Lau {
2586c5cff856SWei Wang 	u32 rt_cookie = 0;
2587c5cff856SWei Wang 
258849d05fe2SDavid Ahern 	if (!from || !fib6_get_cookie_safe(from, &rt_cookie) ||
258993531c67SDavid Ahern 	    rt_cookie != cookie)
25903da59bd9SMartin KaFai Lau 		return NULL;
25913da59bd9SMartin KaFai Lau 
25923da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
25933da59bd9SMartin KaFai Lau 		return NULL;
25943da59bd9SMartin KaFai Lau 
25953da59bd9SMartin KaFai Lau 	return &rt->dst;
25963da59bd9SMartin KaFai Lau }
25973da59bd9SMartin KaFai Lau 
2598a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2599a68886a6SDavid Ahern 					    struct fib6_info *from,
2600a68886a6SDavid Ahern 					    u32 cookie)
26013da59bd9SMartin KaFai Lau {
26025973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
26035973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2604a68886a6SDavid Ahern 	    fib6_check(from, cookie))
26053da59bd9SMartin KaFai Lau 		return &rt->dst;
26063da59bd9SMartin KaFai Lau 	else
26073da59bd9SMartin KaFai Lau 		return NULL;
26083da59bd9SMartin KaFai Lau }
26093da59bd9SMartin KaFai Lau 
26101da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
26111da177e4SLinus Torvalds {
2612a87b7dc9SDavid Ahern 	struct dst_entry *dst_ret;
2613a68886a6SDavid Ahern 	struct fib6_info *from;
26141da177e4SLinus Torvalds 	struct rt6_info *rt;
26151da177e4SLinus Torvalds 
2616a87b7dc9SDavid Ahern 	rt = container_of(dst, struct rt6_info, dst);
2617a87b7dc9SDavid Ahern 
26188f34e53bSDavid Ahern 	if (rt->sernum)
26198f34e53bSDavid Ahern 		return rt6_is_valid(rt) ? dst : NULL;
26208f34e53bSDavid Ahern 
2621a87b7dc9SDavid Ahern 	rcu_read_lock();
26221da177e4SLinus Torvalds 
26236f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
26246f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
26256f3118b5SNicolas Dichtel 	 * into this function always.
26266f3118b5SNicolas Dichtel 	 */
2627e3bc10bdSHannes Frederic Sowa 
2628a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
26294b32b5adSMartin KaFai Lau 
2630a68886a6SDavid Ahern 	if (from && (rt->rt6i_flags & RTF_PCPU ||
2631a68886a6SDavid Ahern 	    unlikely(!list_empty(&rt->rt6i_uncached))))
2632a68886a6SDavid Ahern 		dst_ret = rt6_dst_from_check(rt, from, cookie);
26333da59bd9SMartin KaFai Lau 	else
2634a68886a6SDavid Ahern 		dst_ret = rt6_check(rt, from, cookie);
2635a87b7dc9SDavid Ahern 
2636a87b7dc9SDavid Ahern 	rcu_read_unlock();
2637a87b7dc9SDavid Ahern 
2638a87b7dc9SDavid Ahern 	return dst_ret;
26391da177e4SLinus Torvalds }
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
26421da177e4SLinus Torvalds {
26431da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
26441da177e4SLinus Torvalds 
26451da177e4SLinus Torvalds 	if (rt) {
264654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
2647c3c14da0SDavid Ahern 			rcu_read_lock();
264854c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
264993531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
265054c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
26511da177e4SLinus Torvalds 			}
2652c3c14da0SDavid Ahern 			rcu_read_unlock();
265354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
265454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
265554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
265654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
265754c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
265854c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
26591da177e4SLinus Torvalds }
26601da177e4SLinus Torvalds 
26611da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
26621da177e4SLinus Torvalds {
26631da177e4SLinus Torvalds 	struct rt6_info *rt;
26641da177e4SLinus Torvalds 
26653ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
26661da177e4SLinus Torvalds 
2667adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
26681da177e4SLinus Torvalds 	if (rt) {
26698a14e46fSDavid Ahern 		rcu_read_lock();
26701eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
267193531c67SDavid Ahern 			rt6_remove_exception_rt(rt);
2672c5cff856SWei Wang 		} else {
2673a68886a6SDavid Ahern 			struct fib6_info *from;
2674c5cff856SWei Wang 			struct fib6_node *fn;
2675c5cff856SWei Wang 
2676a68886a6SDavid Ahern 			from = rcu_dereference(rt->from);
2677a68886a6SDavid Ahern 			if (from) {
2678a68886a6SDavid Ahern 				fn = rcu_dereference(from->fib6_node);
2679c5cff856SWei Wang 				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2680c5cff856SWei Wang 					fn->fn_sernum = -1;
2681a68886a6SDavid Ahern 			}
26821da177e4SLinus Torvalds 		}
26831da177e4SLinus Torvalds 		rcu_read_unlock();
26841da177e4SLinus Torvalds 	}
26851da177e4SLinus Torvalds }
26861da177e4SLinus Torvalds 
26876a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout)
26886a3e030fSDavid Ahern {
2689a68886a6SDavid Ahern 	if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2690a68886a6SDavid Ahern 		struct fib6_info *from;
2691a68886a6SDavid Ahern 
2692a68886a6SDavid Ahern 		rcu_read_lock();
2693a68886a6SDavid Ahern 		from = rcu_dereference(rt0->from);
2694a68886a6SDavid Ahern 		if (from)
2695a68886a6SDavid Ahern 			rt0->dst.expires = from->expires;
2696a68886a6SDavid Ahern 		rcu_read_unlock();
2697a68886a6SDavid Ahern 	}
26986a3e030fSDavid Ahern 
26996a3e030fSDavid Ahern 	dst_set_expires(&rt0->dst, timeout);
27006a3e030fSDavid Ahern 	rt0->rt6i_flags |= RTF_EXPIRES;
27016700c270SDavid S. Miller }
27021da177e4SLinus Torvalds 
270345e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
270445e4fd26SMartin KaFai Lau {
270545e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
270645e4fd26SMartin KaFai Lau 
2707d4ead6b3SDavid Ahern 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
270845e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
270945e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
271045e4fd26SMartin KaFai Lau }
271145e4fd26SMartin KaFai Lau 
27120d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
27130d3f6d29SMartin KaFai Lau {
27140d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
27151490ed2aSPaolo Abeni 		(rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
27160d3f6d29SMartin KaFai Lau }
27170d3f6d29SMartin KaFai Lau 
271845e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
2719bd085ef6SHangbin Liu 				 const struct ipv6hdr *iph, u32 mtu,
2720bd085ef6SHangbin Liu 				 bool confirm_neigh)
27211da177e4SLinus Torvalds {
27220dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
27231da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
27241da177e4SLinus Torvalds 
272509454fd0SMaciej Żenczykowski 	/* Note: do *NOT* check dst_metric_locked(dst, RTAX_MTU)
272609454fd0SMaciej Żenczykowski 	 * IPv6 pmtu discovery isn't optional, so 'mtu lock' cannot disable it.
272709454fd0SMaciej Żenczykowski 	 * [see also comment in rt6_mtu_change_route()]
272809454fd0SMaciej Żenczykowski 	 */
272919bda36cSXin Long 
273045e4fd26SMartin KaFai Lau 	if (iph) {
273145e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
273245e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
273345e4fd26SMartin KaFai Lau 	} else if (sk) {
273445e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
273545e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
273645e4fd26SMartin KaFai Lau 	} else {
27370dec879fSJulian Anastasov 		daddr = NULL;
27380dec879fSJulian Anastasov 		saddr = NULL;
27391da177e4SLinus Torvalds 	}
2740bd085ef6SHangbin Liu 
2741bd085ef6SHangbin Liu 	if (confirm_neigh)
27420dec879fSJulian Anastasov 		dst_confirm_neigh(dst, daddr);
2743bd085ef6SHangbin Liu 
27440dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
27450dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
27460dec879fSJulian Anastasov 		return;
27470dec879fSJulian Anastasov 
27480dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
27490dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
27502b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
27512b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
27522b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
27530dec879fSJulian Anastasov 	} else if (daddr) {
275485bd05deSDavid Ahern 		struct fib6_result res = {};
27550dec879fSJulian Anastasov 		struct rt6_info *nrt6;
27560dec879fSJulian Anastasov 
27574d85cd0cSDavid Ahern 		rcu_read_lock();
275885bd05deSDavid Ahern 		res.f6i = rcu_dereference(rt6->from);
275943a4b60dSDavid Ahern 		if (!res.f6i)
276043a4b60dSDavid Ahern 			goto out_unlock;
276143a4b60dSDavid Ahern 
27627d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
27637d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
27647d21fec9SDavid Ahern 
27652d44234bSDavid Ahern 		if (res.f6i->nh) {
27662d44234bSDavid Ahern 			struct fib6_nh_match_arg arg = {
27672d44234bSDavid Ahern 				.dev = dst->dev,
27682d44234bSDavid Ahern 				.gw = &rt6->rt6i_gateway,
27692d44234bSDavid Ahern 			};
27702d44234bSDavid Ahern 
27712d44234bSDavid Ahern 			nexthop_for_each_fib6_nh(res.f6i->nh,
27722d44234bSDavid Ahern 						 fib6_nh_find_match, &arg);
27732d44234bSDavid Ahern 
27742d44234bSDavid Ahern 			/* fib6_info uses a nexthop that does not have fib6_nh
27752d44234bSDavid Ahern 			 * using the dst->dev + gw. Should be impossible.
27762d44234bSDavid Ahern 			 */
277743a4b60dSDavid Ahern 			if (!arg.match)
277843a4b60dSDavid Ahern 				goto out_unlock;
27792d44234bSDavid Ahern 
27802d44234bSDavid Ahern 			res.nh = arg.match;
27812d44234bSDavid Ahern 		} else {
27822d44234bSDavid Ahern 			res.nh = res.f6i->fib6_nh;
27832d44234bSDavid Ahern 		}
27842d44234bSDavid Ahern 
278585bd05deSDavid Ahern 		nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
278645e4fd26SMartin KaFai Lau 		if (nrt6) {
278745e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
27885012f0a5SDavid Ahern 			if (rt6_insert_exception(nrt6, &res))
27892b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
279045e4fd26SMartin KaFai Lau 		}
279143a4b60dSDavid Ahern out_unlock:
2792a68886a6SDavid Ahern 		rcu_read_unlock();
279345e4fd26SMartin KaFai Lau 	}
279445e4fd26SMartin KaFai Lau }
279545e4fd26SMartin KaFai Lau 
279645e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
2797bd085ef6SHangbin Liu 			       struct sk_buff *skb, u32 mtu,
2798bd085ef6SHangbin Liu 			       bool confirm_neigh)
279945e4fd26SMartin KaFai Lau {
2800bd085ef6SHangbin Liu 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu,
2801bd085ef6SHangbin Liu 			     confirm_neigh);
28021da177e4SLinus Torvalds }
28031da177e4SLinus Torvalds 
280442ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2805e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
280681aded24SDavid S. Miller {
280781aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
280881aded24SDavid S. Miller 	struct dst_entry *dst;
2809dc92095dSMaciej Żenczykowski 	struct flowi6 fl6 = {
2810dc92095dSMaciej Żenczykowski 		.flowi6_oif = oif,
2811dc92095dSMaciej Żenczykowski 		.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2812dc92095dSMaciej Żenczykowski 		.daddr = iph->daddr,
2813dc92095dSMaciej Żenczykowski 		.saddr = iph->saddr,
2814dc92095dSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
2815dc92095dSMaciej Żenczykowski 		.flowi6_uid = uid,
2816dc92095dSMaciej Żenczykowski 	};
281781aded24SDavid S. Miller 
281881aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
281981aded24SDavid S. Miller 	if (!dst->error)
2820bd085ef6SHangbin Liu 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu), true);
282181aded24SDavid S. Miller 	dst_release(dst);
282281aded24SDavid S. Miller }
282381aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
282481aded24SDavid S. Miller 
282581aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
282681aded24SDavid S. Miller {
28277ddacfa5SDavid Ahern 	int oif = sk->sk_bound_dev_if;
282833c162a9SMartin KaFai Lau 	struct dst_entry *dst;
282933c162a9SMartin KaFai Lau 
28307ddacfa5SDavid Ahern 	if (!oif && skb->dev)
28317ddacfa5SDavid Ahern 		oif = l3mdev_master_ifindex(skb->dev);
28327ddacfa5SDavid Ahern 
28337ddacfa5SDavid Ahern 	ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
283433c162a9SMartin KaFai Lau 
283533c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
283633c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
283733c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
283833c162a9SMartin KaFai Lau 		return;
283933c162a9SMartin KaFai Lau 
284033c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
284133c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
284233c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
284333c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
284481aded24SDavid S. Miller }
284581aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
284681aded24SDavid S. Miller 
28477d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
28487d6850f7SAlexey Kodanev 			   const struct flowi6 *fl6)
28497d6850f7SAlexey Kodanev {
28507d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
28517d6850f7SAlexey Kodanev 	struct ipv6_pinfo *np = inet6_sk(sk);
28527d6850f7SAlexey Kodanev #endif
28537d6850f7SAlexey Kodanev 
28547d6850f7SAlexey Kodanev 	ip6_dst_store(sk, dst,
28557d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
28567d6850f7SAlexey Kodanev 		      &sk->sk_v6_daddr : NULL,
28577d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
28587d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
28597d6850f7SAlexey Kodanev 		      &np->saddr :
28607d6850f7SAlexey Kodanev #endif
28617d6850f7SAlexey Kodanev 		      NULL);
28627d6850f7SAlexey Kodanev }
28637d6850f7SAlexey Kodanev 
28649b6b35abSDavid Ahern static bool ip6_redirect_nh_match(const struct fib6_result *res,
28650b34eb00SDavid Ahern 				  struct flowi6 *fl6,
28660b34eb00SDavid Ahern 				  const struct in6_addr *gw,
28670b34eb00SDavid Ahern 				  struct rt6_info **ret)
28680b34eb00SDavid Ahern {
28699b6b35abSDavid Ahern 	const struct fib6_nh *nh = res->nh;
28709b6b35abSDavid Ahern 
28710b34eb00SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
28720b34eb00SDavid Ahern 	    fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
28730b34eb00SDavid Ahern 		return false;
28740b34eb00SDavid Ahern 
28750b34eb00SDavid Ahern 	/* rt_cache's gateway might be different from its 'parent'
28760b34eb00SDavid Ahern 	 * in the case of an ip redirect.
28770b34eb00SDavid Ahern 	 * So we keep searching in the exception table if the gateway
28780b34eb00SDavid Ahern 	 * is different.
28790b34eb00SDavid Ahern 	 */
28800b34eb00SDavid Ahern 	if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
28810b34eb00SDavid Ahern 		struct rt6_info *rt_cache;
28820b34eb00SDavid Ahern 
28839b6b35abSDavid Ahern 		rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
28840b34eb00SDavid Ahern 		if (rt_cache &&
28850b34eb00SDavid Ahern 		    ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
28860b34eb00SDavid Ahern 			*ret = rt_cache;
28870b34eb00SDavid Ahern 			return true;
28880b34eb00SDavid Ahern 		}
28890b34eb00SDavid Ahern 		return false;
28900b34eb00SDavid Ahern 	}
28910b34eb00SDavid Ahern 	return true;
28920b34eb00SDavid Ahern }
28930b34eb00SDavid Ahern 
2894c55c8988SDavid Ahern struct fib6_nh_rd_arg {
2895c55c8988SDavid Ahern 	struct fib6_result	*res;
2896c55c8988SDavid Ahern 	struct flowi6		*fl6;
2897c55c8988SDavid Ahern 	const struct in6_addr	*gw;
2898c55c8988SDavid Ahern 	struct rt6_info		**ret;
2899c55c8988SDavid Ahern };
2900c55c8988SDavid Ahern 
2901c55c8988SDavid Ahern static int fib6_nh_redirect_match(struct fib6_nh *nh, void *_arg)
2902c55c8988SDavid Ahern {
2903c55c8988SDavid Ahern 	struct fib6_nh_rd_arg *arg = _arg;
2904c55c8988SDavid Ahern 
2905c55c8988SDavid Ahern 	arg->res->nh = nh;
2906c55c8988SDavid Ahern 	return ip6_redirect_nh_match(arg->res, arg->fl6, arg->gw, arg->ret);
2907c55c8988SDavid Ahern }
2908c55c8988SDavid Ahern 
2909b55b76b2SDuan Jiong /* Handle redirects */
2910b55b76b2SDuan Jiong struct ip6rd_flowi {
2911b55b76b2SDuan Jiong 	struct flowi6 fl6;
2912b55b76b2SDuan Jiong 	struct in6_addr gateway;
2913b55b76b2SDuan Jiong };
2914b55b76b2SDuan Jiong 
2915b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
2916b55b76b2SDuan Jiong 					     struct fib6_table *table,
2917b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2918b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2919b55b76b2SDuan Jiong 					     int flags)
2920b55b76b2SDuan Jiong {
2921b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
29220b34eb00SDavid Ahern 	struct rt6_info *ret = NULL;
29239b6b35abSDavid Ahern 	struct fib6_result res = {};
2924c55c8988SDavid Ahern 	struct fib6_nh_rd_arg arg = {
2925c55c8988SDavid Ahern 		.res = &res,
2926c55c8988SDavid Ahern 		.fl6 = fl6,
2927c55c8988SDavid Ahern 		.gw  = &rdfl->gateway,
2928c55c8988SDavid Ahern 		.ret = &ret
2929c55c8988SDavid Ahern 	};
29308d1c802bSDavid Ahern 	struct fib6_info *rt;
2931b55b76b2SDuan Jiong 	struct fib6_node *fn;
2932b55b76b2SDuan Jiong 
293331680ac2SDavid Ahern 	/* l3mdev_update_flow overrides oif if the device is enslaved; in
293431680ac2SDavid Ahern 	 * this case we must match on the real ingress device, so reset it
293531680ac2SDavid Ahern 	 */
293631680ac2SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
293731680ac2SDavid Ahern 		fl6->flowi6_oif = skb->dev->ifindex;
293831680ac2SDavid Ahern 
2939b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
294067c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2941b55b76b2SDuan Jiong 	 *
2942b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2943b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2944b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2945b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2946b55b76b2SDuan Jiong 	 * routes.
2947b55b76b2SDuan Jiong 	 */
2948b55b76b2SDuan Jiong 
294966f5d6ceSWei Wang 	rcu_read_lock();
29506454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2951b55b76b2SDuan Jiong restart:
295266f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
29539b6b35abSDavid Ahern 		res.f6i = rt;
295414895687SDavid Ahern 		if (fib6_check_expired(rt))
2955b55b76b2SDuan Jiong 			continue;
295693c2fb25SDavid Ahern 		if (rt->fib6_flags & RTF_REJECT)
2957b55b76b2SDuan Jiong 			break;
2958c55c8988SDavid Ahern 		if (unlikely(rt->nh)) {
2959c55c8988SDavid Ahern 			if (nexthop_is_blackhole(rt->nh))
2960c55c8988SDavid Ahern 				continue;
2961c55c8988SDavid Ahern 			/* on match, res->nh is filled in and potentially ret */
2962c55c8988SDavid Ahern 			if (nexthop_for_each_fib6_nh(rt->nh,
2963c55c8988SDavid Ahern 						     fib6_nh_redirect_match,
2964c55c8988SDavid Ahern 						     &arg))
29650b34eb00SDavid Ahern 				goto out;
2966c55c8988SDavid Ahern 		} else {
2967c55c8988SDavid Ahern 			res.nh = rt->fib6_nh;
2968c55c8988SDavid Ahern 			if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway,
2969c55c8988SDavid Ahern 						  &ret))
2970c55c8988SDavid Ahern 				goto out;
2971c55c8988SDavid Ahern 		}
2972b55b76b2SDuan Jiong 	}
2973b55b76b2SDuan Jiong 
2974b55b76b2SDuan Jiong 	if (!rt)
2975421842edSDavid Ahern 		rt = net->ipv6.fib6_null_entry;
297693c2fb25SDavid Ahern 	else if (rt->fib6_flags & RTF_REJECT) {
297723fb93a4SDavid Ahern 		ret = net->ipv6.ip6_null_entry;
2978b0a1ba59SMartin KaFai Lau 		goto out;
2979b0a1ba59SMartin KaFai Lau 	}
2980b0a1ba59SMartin KaFai Lau 
2981421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
2982a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2983a3c00e46SMartin KaFai Lau 		if (fn)
2984a3c00e46SMartin KaFai Lau 			goto restart;
2985b55b76b2SDuan Jiong 	}
2986a3c00e46SMartin KaFai Lau 
29879b6b35abSDavid Ahern 	res.f6i = rt;
29881cf844c7SDavid Ahern 	res.nh = rt->fib6_nh;
2989b0a1ba59SMartin KaFai Lau out:
29907d21fec9SDavid Ahern 	if (ret) {
299110585b43SDavid Ahern 		ip6_hold_safe(net, &ret);
29927d21fec9SDavid Ahern 	} else {
29937d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
29947d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
29959b6b35abSDavid Ahern 		ret = ip6_create_rt_rcu(&res);
29967d21fec9SDavid Ahern 	}
2997b55b76b2SDuan Jiong 
299866f5d6ceSWei Wang 	rcu_read_unlock();
2999b55b76b2SDuan Jiong 
30008ff2e5b2SDavid Ahern 	trace_fib6_table_lookup(net, &res, table, fl6);
300123fb93a4SDavid Ahern 	return ret;
3002b55b76b2SDuan Jiong };
3003b55b76b2SDuan Jiong 
3004b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
3005b55b76b2SDuan Jiong 					    const struct flowi6 *fl6,
3006b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
3007b55b76b2SDuan Jiong 					    const struct in6_addr *gateway)
3008b55b76b2SDuan Jiong {
3009b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
3010b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
3011b55b76b2SDuan Jiong 
3012b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
3013b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
3014b55b76b2SDuan Jiong 
3015b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, &rdfl.fl6, skb,
3016b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
3017b55b76b2SDuan Jiong }
3018b55b76b2SDuan Jiong 
3019e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
3020e2d118a1SLorenzo Colitti 		  kuid_t uid)
30213a5ad2eeSDavid S. Miller {
30223a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
30233a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
30241f7f10acSMaciej Żenczykowski 	struct flowi6 fl6 = {
30251f7f10acSMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
30261f7f10acSMaciej Żenczykowski 		.flowi6_oif = oif,
30271f7f10acSMaciej Żenczykowski 		.flowi6_mark = mark,
30281f7f10acSMaciej Żenczykowski 		.daddr = iph->daddr,
30291f7f10acSMaciej Żenczykowski 		.saddr = iph->saddr,
30301f7f10acSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
30311f7f10acSMaciej Żenczykowski 		.flowi6_uid = uid,
30321f7f10acSMaciej Żenczykowski 	};
30333a5ad2eeSDavid S. Miller 
3034b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
30356700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
30363a5ad2eeSDavid S. Miller 	dst_release(dst);
30373a5ad2eeSDavid S. Miller }
30383a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
30393a5ad2eeSDavid S. Miller 
3040d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
3041c92a59ecSDuan Jiong {
3042c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
3043c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
3044c92a59ecSDuan Jiong 	struct dst_entry *dst;
30450b26fb17SMaciej Żenczykowski 	struct flowi6 fl6 = {
30460b26fb17SMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
30470b26fb17SMaciej Żenczykowski 		.flowi6_oif = oif,
30480b26fb17SMaciej Żenczykowski 		.daddr = msg->dest,
30490b26fb17SMaciej Żenczykowski 		.saddr = iph->daddr,
30500b26fb17SMaciej Żenczykowski 		.flowi6_uid = sock_net_uid(net, NULL),
30510b26fb17SMaciej Żenczykowski 	};
3052c92a59ecSDuan Jiong 
3053b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
3054c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
3055c92a59ecSDuan Jiong 	dst_release(dst);
3056c92a59ecSDuan Jiong }
3057c92a59ecSDuan Jiong 
30583a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
30593a5ad2eeSDavid S. Miller {
3060e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
3061e2d118a1SLorenzo Colitti 		     sk->sk_uid);
30623a5ad2eeSDavid S. Miller }
30633a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
30643a5ad2eeSDavid S. Miller 
30650dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
30661da177e4SLinus Torvalds {
30670dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
30680dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
30690dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
30700dbaee3bSDavid S. Miller 
30711da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
30721da177e4SLinus Torvalds 
30735578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
30745578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
30751da177e4SLinus Torvalds 
30761da177e4SLinus Torvalds 	/*
30771da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
30781da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
30791da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
30801da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
30811da177e4SLinus Torvalds 	 */
30821da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
30831da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
30841da177e4SLinus Torvalds 	return mtu;
30851da177e4SLinus Torvalds }
30861da177e4SLinus Torvalds 
3087ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
3088d33e4553SDavid S. Miller {
3089d33e4553SDavid S. Miller 	struct inet6_dev *idev;
3090d4ead6b3SDavid Ahern 	unsigned int mtu;
3091618f9bc7SSteffen Klassert 
30924b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
30934b32b5adSMartin KaFai Lau 	if (mtu)
30944b32b5adSMartin KaFai Lau 		goto out;
30954b32b5adSMartin KaFai Lau 
3096618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
3097d33e4553SDavid S. Miller 
3098d33e4553SDavid S. Miller 	rcu_read_lock();
3099d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
3100d33e4553SDavid S. Miller 	if (idev)
3101d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
3102d33e4553SDavid S. Miller 	rcu_read_unlock();
3103d33e4553SDavid S. Miller 
310430f78d8eSEric Dumazet out:
310514972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
310614972cbdSRoopa Prabhu 
310714972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
3108d33e4553SDavid S. Miller }
3109d33e4553SDavid S. Miller 
3110901731b8SDavid Ahern /* MTU selection:
3111901731b8SDavid Ahern  * 1. mtu on route is locked - use it
3112901731b8SDavid Ahern  * 2. mtu from nexthop exception
3113901731b8SDavid Ahern  * 3. mtu from egress device
3114901731b8SDavid Ahern  *
3115901731b8SDavid Ahern  * based on ip6_dst_mtu_forward and exception logic of
3116901731b8SDavid Ahern  * rt6_find_cached_rt; called with rcu_read_lock
3117901731b8SDavid Ahern  */
3118b748f260SDavid Ahern u32 ip6_mtu_from_fib6(const struct fib6_result *res,
3119b748f260SDavid Ahern 		      const struct in6_addr *daddr,
3120b748f260SDavid Ahern 		      const struct in6_addr *saddr)
3121901731b8SDavid Ahern {
3122b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
3123b748f260SDavid Ahern 	struct fib6_info *f6i = res->f6i;
3124901731b8SDavid Ahern 	struct inet6_dev *idev;
3125510e2cedSWei Wang 	struct rt6_info *rt;
3126901731b8SDavid Ahern 	u32 mtu = 0;
3127901731b8SDavid Ahern 
3128901731b8SDavid Ahern 	if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
3129901731b8SDavid Ahern 		mtu = f6i->fib6_pmtu;
3130901731b8SDavid Ahern 		if (mtu)
3131901731b8SDavid Ahern 			goto out;
3132901731b8SDavid Ahern 	}
3133901731b8SDavid Ahern 
3134510e2cedSWei Wang 	rt = rt6_find_cached_rt(res, daddr, saddr);
3135510e2cedSWei Wang 	if (unlikely(rt)) {
3136510e2cedSWei Wang 		mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
3137510e2cedSWei Wang 	} else {
3138b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
3139901731b8SDavid Ahern 
3140901731b8SDavid Ahern 		mtu = IPV6_MIN_MTU;
3141901731b8SDavid Ahern 		idev = __in6_dev_get(dev);
3142901731b8SDavid Ahern 		if (idev && idev->cnf.mtu6 > mtu)
3143901731b8SDavid Ahern 			mtu = idev->cnf.mtu6;
3144901731b8SDavid Ahern 	}
3145901731b8SDavid Ahern 
3146901731b8SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3147901731b8SDavid Ahern out:
3148b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
3149901731b8SDavid Ahern }
3150901731b8SDavid Ahern 
31513b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
315287a11578SDavid S. Miller 				  struct flowi6 *fl6)
31531da177e4SLinus Torvalds {
315487a11578SDavid S. Miller 	struct dst_entry *dst;
31551da177e4SLinus Torvalds 	struct rt6_info *rt;
31561da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
3157c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
31581da177e4SLinus Torvalds 
315938308473SDavid S. Miller 	if (unlikely(!idev))
3160122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
31611da177e4SLinus Torvalds 
3162ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
316338308473SDavid S. Miller 	if (unlikely(!rt)) {
31641da177e4SLinus Torvalds 		in6_dev_put(idev);
316587a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
31661da177e4SLinus Torvalds 		goto out;
31671da177e4SLinus Torvalds 	}
31681da177e4SLinus Torvalds 
3169588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
31708e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
3171550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
317287a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
31738e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
31748e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
317514edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
31761da177e4SLinus Torvalds 
31774c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
3178587fea74SWei Wang 	 * do proper release of the net_device
3179587fea74SWei Wang 	 */
3180587fea74SWei Wang 	rt6_uncached_list_add(rt);
318181eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
31821da177e4SLinus Torvalds 
318387a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
318487a11578SDavid S. Miller 
31851da177e4SLinus Torvalds out:
318687a11578SDavid S. Miller 	return dst;
31871da177e4SLinus Torvalds }
31881da177e4SLinus Torvalds 
3189569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
31901da177e4SLinus Torvalds {
319186393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
31927019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
31937019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
31947019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
31957019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
31967019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
3197fc66f95cSEric Dumazet 	int entries;
31981da177e4SLinus Torvalds 
3199fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
3200cf86a086SEric Dumazet 	if (entries > rt_max_size)
3201cf86a086SEric Dumazet 		entries = dst_entries_get_slow(ops);
3202cf86a086SEric Dumazet 
320349a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
3204fc66f95cSEric Dumazet 	    entries <= rt_max_size)
32051da177e4SLinus Torvalds 		goto out;
32061da177e4SLinus Torvalds 
32076891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
320814956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
3209fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
3210fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
32117019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
32121da177e4SLinus Torvalds out:
32137019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
3214fc66f95cSEric Dumazet 	return entries > rt_max_size;
32151da177e4SLinus Torvalds }
32161da177e4SLinus Torvalds 
3217b2c709ccSDavid Ahern static int ip6_nh_lookup_table(struct net *net, struct fib6_config *cfg,
3218b2c709ccSDavid Ahern 			       const struct in6_addr *gw_addr, u32 tbid,
3219b2c709ccSDavid Ahern 			       int flags, struct fib6_result *res)
32208c14586fSDavid Ahern {
32218c14586fSDavid Ahern 	struct flowi6 fl6 = {
32228c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
32238c14586fSDavid Ahern 		.daddr = *gw_addr,
32248c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
32258c14586fSDavid Ahern 	};
32268c14586fSDavid Ahern 	struct fib6_table *table;
3227b2c709ccSDavid Ahern 	int err;
32288c14586fSDavid Ahern 
3229f4797b33SDavid Ahern 	table = fib6_get_table(net, tbid);
32308c14586fSDavid Ahern 	if (!table)
3231b2c709ccSDavid Ahern 		return -EINVAL;
32328c14586fSDavid Ahern 
32338c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
32348c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
32358c14586fSDavid Ahern 
3236f4797b33SDavid Ahern 	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
32378c14586fSDavid Ahern 
3238b2c709ccSDavid Ahern 	err = fib6_table_lookup(net, table, cfg->fc_ifindex, &fl6, res, flags);
3239b2c709ccSDavid Ahern 	if (!err && res->f6i != net->ipv6.fib6_null_entry)
3240b2c709ccSDavid Ahern 		fib6_select_path(net, res, &fl6, cfg->fc_ifindex,
3241b2c709ccSDavid Ahern 				 cfg->fc_ifindex != 0, NULL, flags);
32428c14586fSDavid Ahern 
3243b2c709ccSDavid Ahern 	return err;
32448c14586fSDavid Ahern }
32458c14586fSDavid Ahern 
3246fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net,
3247fc1e64e1SDavid Ahern 				     struct fib6_config *cfg,
32489fbb704cSDavid Ahern 				     const struct net_device *dev,
3249fc1e64e1SDavid Ahern 				     struct netlink_ext_ack *extack)
3250fc1e64e1SDavid Ahern {
3251b2c709ccSDavid Ahern 	u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
3252fc1e64e1SDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
3253b2c709ccSDavid Ahern 	struct fib6_result res = {};
3254fc1e64e1SDavid Ahern 	int err;
3255fc1e64e1SDavid Ahern 
3256b2c709ccSDavid Ahern 	err = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0, &res);
3257b2c709ccSDavid Ahern 	if (!err && !(res.fib6_flags & RTF_REJECT) &&
32584ed591c8SDavid Ahern 	    /* ignore match if it is the default route */
3259b2c709ccSDavid Ahern 	    !ipv6_addr_any(&res.f6i->fib6_dst.addr) &&
3260b2c709ccSDavid Ahern 	    (res.fib6_type != RTN_UNICAST || dev != res.nh->fib_nh_dev)) {
326144750f84SDavid Ahern 		NL_SET_ERR_MSG(extack,
326244750f84SDavid Ahern 			       "Nexthop has invalid gateway or device mismatch");
3263fc1e64e1SDavid Ahern 		err = -EINVAL;
3264fc1e64e1SDavid Ahern 	}
3265fc1e64e1SDavid Ahern 
3266fc1e64e1SDavid Ahern 	return err;
3267fc1e64e1SDavid Ahern }
3268fc1e64e1SDavid Ahern 
32691edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net,
32701edce99fSDavid Ahern 			      struct fib6_config *cfg,
32711edce99fSDavid Ahern 			      struct net_device **_dev,
32721edce99fSDavid Ahern 			      struct inet6_dev **idev)
32731edce99fSDavid Ahern {
32741edce99fSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
32751edce99fSDavid Ahern 	struct net_device *dev = _dev ? *_dev : NULL;
3276b2c709ccSDavid Ahern 	int flags = RT6_LOOKUP_F_IFACE;
3277b2c709ccSDavid Ahern 	struct fib6_result res = {};
32781edce99fSDavid Ahern 	int err = -EHOSTUNREACH;
32791edce99fSDavid Ahern 
32801edce99fSDavid Ahern 	if (cfg->fc_table) {
3281b2c709ccSDavid Ahern 		err = ip6_nh_lookup_table(net, cfg, gw_addr,
3282b2c709ccSDavid Ahern 					  cfg->fc_table, flags, &res);
3283b2c709ccSDavid Ahern 		/* gw_addr can not require a gateway or resolve to a reject
3284b2c709ccSDavid Ahern 		 * route. If a device is given, it must match the result.
3285b2c709ccSDavid Ahern 		 */
3286b2c709ccSDavid Ahern 		if (err || res.fib6_flags & RTF_REJECT ||
3287b2c709ccSDavid Ahern 		    res.nh->fib_nh_gw_family ||
3288b2c709ccSDavid Ahern 		    (dev && dev != res.nh->fib_nh_dev))
3289b2c709ccSDavid Ahern 			err = -EHOSTUNREACH;
32901edce99fSDavid Ahern 	}
32911edce99fSDavid Ahern 
3292b2c709ccSDavid Ahern 	if (err < 0) {
3293b2c709ccSDavid Ahern 		struct flowi6 fl6 = {
3294b2c709ccSDavid Ahern 			.flowi6_oif = cfg->fc_ifindex,
3295b2c709ccSDavid Ahern 			.daddr = *gw_addr,
3296b2c709ccSDavid Ahern 		};
32971edce99fSDavid Ahern 
3298b2c709ccSDavid Ahern 		err = fib6_lookup(net, cfg->fc_ifindex, &fl6, &res, flags);
3299b2c709ccSDavid Ahern 		if (err || res.fib6_flags & RTF_REJECT ||
3300b2c709ccSDavid Ahern 		    res.nh->fib_nh_gw_family)
3301b2c709ccSDavid Ahern 			err = -EHOSTUNREACH;
33021edce99fSDavid Ahern 
3303b2c709ccSDavid Ahern 		if (err)
3304b2c709ccSDavid Ahern 			return err;
3305b2c709ccSDavid Ahern 
3306b2c709ccSDavid Ahern 		fib6_select_path(net, &res, &fl6, cfg->fc_ifindex,
3307b2c709ccSDavid Ahern 				 cfg->fc_ifindex != 0, NULL, flags);
33081edce99fSDavid Ahern 	}
33091edce99fSDavid Ahern 
33101edce99fSDavid Ahern 	err = 0;
3311b2c709ccSDavid Ahern 	if (dev) {
3312b2c709ccSDavid Ahern 		if (dev != res.nh->fib_nh_dev)
3313b2c709ccSDavid Ahern 			err = -EHOSTUNREACH;
3314b2c709ccSDavid Ahern 	} else {
3315b2c709ccSDavid Ahern 		*_dev = dev = res.nh->fib_nh_dev;
3316b2c709ccSDavid Ahern 		dev_hold(dev);
3317b2c709ccSDavid Ahern 		*idev = in6_dev_get(dev);
3318b2c709ccSDavid Ahern 	}
33191edce99fSDavid Ahern 
33201edce99fSDavid Ahern 	return err;
33211edce99fSDavid Ahern }
33221edce99fSDavid Ahern 
33239fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
33249fbb704cSDavid Ahern 			   struct net_device **_dev, struct inet6_dev **idev,
33259fbb704cSDavid Ahern 			   struct netlink_ext_ack *extack)
33269fbb704cSDavid Ahern {
33279fbb704cSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
33289fbb704cSDavid Ahern 	int gwa_type = ipv6_addr_type(gw_addr);
3329232378e8SDavid Ahern 	bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
33309fbb704cSDavid Ahern 	const struct net_device *dev = *_dev;
3331232378e8SDavid Ahern 	bool need_addr_check = !dev;
33329fbb704cSDavid Ahern 	int err = -EINVAL;
33339fbb704cSDavid Ahern 
33349fbb704cSDavid Ahern 	/* if gw_addr is local we will fail to detect this in case
33359fbb704cSDavid Ahern 	 * address is still TENTATIVE (DAD in progress). rt6_lookup()
33369fbb704cSDavid Ahern 	 * will return already-added prefix route via interface that
33379fbb704cSDavid Ahern 	 * prefix route was assigned to, which might be non-loopback.
33389fbb704cSDavid Ahern 	 */
3339232378e8SDavid Ahern 	if (dev &&
3340232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3341232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
33429fbb704cSDavid Ahern 		goto out;
33439fbb704cSDavid Ahern 	}
33449fbb704cSDavid Ahern 
33459fbb704cSDavid Ahern 	if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
33469fbb704cSDavid Ahern 		/* IPv6 strictly inhibits using not link-local
33479fbb704cSDavid Ahern 		 * addresses as nexthop address.
33489fbb704cSDavid Ahern 		 * Otherwise, router will not able to send redirects.
33499fbb704cSDavid Ahern 		 * It is very good, but in some (rare!) circumstances
33509fbb704cSDavid Ahern 		 * (SIT, PtP, NBMA NOARP links) it is handy to allow
33519fbb704cSDavid Ahern 		 * some exceptions. --ANK
33529fbb704cSDavid Ahern 		 * We allow IPv4-mapped nexthops to support RFC4798-type
33539fbb704cSDavid Ahern 		 * addressing
33549fbb704cSDavid Ahern 		 */
33559fbb704cSDavid Ahern 		if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
33569fbb704cSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
33579fbb704cSDavid Ahern 			goto out;
33589fbb704cSDavid Ahern 		}
33599fbb704cSDavid Ahern 
3360b2c709ccSDavid Ahern 		rcu_read_lock();
3361b2c709ccSDavid Ahern 
33629fbb704cSDavid Ahern 		if (cfg->fc_flags & RTNH_F_ONLINK)
33639fbb704cSDavid Ahern 			err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
33649fbb704cSDavid Ahern 		else
33659fbb704cSDavid Ahern 			err = ip6_route_check_nh(net, cfg, _dev, idev);
33669fbb704cSDavid Ahern 
3367b2c709ccSDavid Ahern 		rcu_read_unlock();
3368b2c709ccSDavid Ahern 
33699fbb704cSDavid Ahern 		if (err)
33709fbb704cSDavid Ahern 			goto out;
33719fbb704cSDavid Ahern 	}
33729fbb704cSDavid Ahern 
33739fbb704cSDavid Ahern 	/* reload in case device was changed */
33749fbb704cSDavid Ahern 	dev = *_dev;
33759fbb704cSDavid Ahern 
33769fbb704cSDavid Ahern 	err = -EINVAL;
33779fbb704cSDavid Ahern 	if (!dev) {
33789fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Egress device not specified");
33799fbb704cSDavid Ahern 		goto out;
33809fbb704cSDavid Ahern 	} else if (dev->flags & IFF_LOOPBACK) {
33819fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack,
33829fbb704cSDavid Ahern 			       "Egress device can not be loopback device for this route");
33839fbb704cSDavid Ahern 		goto out;
33849fbb704cSDavid Ahern 	}
3385232378e8SDavid Ahern 
3386232378e8SDavid Ahern 	/* if we did not check gw_addr above, do so now that the
3387232378e8SDavid Ahern 	 * egress device has been resolved.
3388232378e8SDavid Ahern 	 */
3389232378e8SDavid Ahern 	if (need_addr_check &&
3390232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3391232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3392232378e8SDavid Ahern 		goto out;
3393232378e8SDavid Ahern 	}
3394232378e8SDavid Ahern 
33959fbb704cSDavid Ahern 	err = 0;
33969fbb704cSDavid Ahern out:
33979fbb704cSDavid Ahern 	return err;
33989fbb704cSDavid Ahern }
33999fbb704cSDavid Ahern 
340083c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
340183c44251SDavid Ahern {
340283c44251SDavid Ahern 	if ((flags & RTF_REJECT) ||
340383c44251SDavid Ahern 	    (dev && (dev->flags & IFF_LOOPBACK) &&
340483c44251SDavid Ahern 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
340583c44251SDavid Ahern 	     !(flags & RTF_LOCAL)))
340683c44251SDavid Ahern 		return true;
340783c44251SDavid Ahern 
340883c44251SDavid Ahern 	return false;
340983c44251SDavid Ahern }
341083c44251SDavid Ahern 
341183c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
341283c44251SDavid Ahern 		 struct fib6_config *cfg, gfp_t gfp_flags,
341383c44251SDavid Ahern 		 struct netlink_ext_ack *extack)
341483c44251SDavid Ahern {
341583c44251SDavid Ahern 	struct net_device *dev = NULL;
341683c44251SDavid Ahern 	struct inet6_dev *idev = NULL;
341783c44251SDavid Ahern 	int addr_type;
341883c44251SDavid Ahern 	int err;
341983c44251SDavid Ahern 
3420f1741730SDavid Ahern 	fib6_nh->fib_nh_family = AF_INET6;
34211bef4c22SEric Dumazet #ifdef CONFIG_IPV6_ROUTER_PREF
34221bef4c22SEric Dumazet 	fib6_nh->last_probe = jiffies;
34231bef4c22SEric Dumazet #endif
3424f1741730SDavid Ahern 
342583c44251SDavid Ahern 	err = -ENODEV;
342683c44251SDavid Ahern 	if (cfg->fc_ifindex) {
342783c44251SDavid Ahern 		dev = dev_get_by_index(net, cfg->fc_ifindex);
342883c44251SDavid Ahern 		if (!dev)
342983c44251SDavid Ahern 			goto out;
343083c44251SDavid Ahern 		idev = in6_dev_get(dev);
343183c44251SDavid Ahern 		if (!idev)
343283c44251SDavid Ahern 			goto out;
343383c44251SDavid Ahern 	}
343483c44251SDavid Ahern 
343583c44251SDavid Ahern 	if (cfg->fc_flags & RTNH_F_ONLINK) {
343683c44251SDavid Ahern 		if (!dev) {
343783c44251SDavid Ahern 			NL_SET_ERR_MSG(extack,
343883c44251SDavid Ahern 				       "Nexthop device required for onlink");
343983c44251SDavid Ahern 			goto out;
344083c44251SDavid Ahern 		}
344183c44251SDavid Ahern 
344283c44251SDavid Ahern 		if (!(dev->flags & IFF_UP)) {
344383c44251SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
344483c44251SDavid Ahern 			err = -ENETDOWN;
344583c44251SDavid Ahern 			goto out;
344683c44251SDavid Ahern 		}
344783c44251SDavid Ahern 
3448ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
344983c44251SDavid Ahern 	}
345083c44251SDavid Ahern 
3451ad1601aeSDavid Ahern 	fib6_nh->fib_nh_weight = 1;
345283c44251SDavid Ahern 
345383c44251SDavid Ahern 	/* We cannot add true routes via loopback here,
345483c44251SDavid Ahern 	 * they would result in kernel looping; promote them to reject routes
345583c44251SDavid Ahern 	 */
345683c44251SDavid Ahern 	addr_type = ipv6_addr_type(&cfg->fc_dst);
345783c44251SDavid Ahern 	if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
345883c44251SDavid Ahern 		/* hold loopback dev/idev if we haven't done so. */
345983c44251SDavid Ahern 		if (dev != net->loopback_dev) {
346083c44251SDavid Ahern 			if (dev) {
346183c44251SDavid Ahern 				dev_put(dev);
346283c44251SDavid Ahern 				in6_dev_put(idev);
346383c44251SDavid Ahern 			}
346483c44251SDavid Ahern 			dev = net->loopback_dev;
346583c44251SDavid Ahern 			dev_hold(dev);
346683c44251SDavid Ahern 			idev = in6_dev_get(dev);
346783c44251SDavid Ahern 			if (!idev) {
346883c44251SDavid Ahern 				err = -ENODEV;
346983c44251SDavid Ahern 				goto out;
347083c44251SDavid Ahern 			}
347183c44251SDavid Ahern 		}
34727dd73168SDavid Ahern 		goto pcpu_alloc;
347383c44251SDavid Ahern 	}
347483c44251SDavid Ahern 
347583c44251SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY) {
347683c44251SDavid Ahern 		err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
347783c44251SDavid Ahern 		if (err)
347883c44251SDavid Ahern 			goto out;
347983c44251SDavid Ahern 
3480ad1601aeSDavid Ahern 		fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3481bdf00467SDavid Ahern 		fib6_nh->fib_nh_gw_family = AF_INET6;
348283c44251SDavid Ahern 	}
348383c44251SDavid Ahern 
348483c44251SDavid Ahern 	err = -ENODEV;
348583c44251SDavid Ahern 	if (!dev)
348683c44251SDavid Ahern 		goto out;
348783c44251SDavid Ahern 
348883c44251SDavid Ahern 	if (idev->cnf.disable_ipv6) {
348983c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
349083c44251SDavid Ahern 		err = -EACCES;
349183c44251SDavid Ahern 		goto out;
349283c44251SDavid Ahern 	}
349383c44251SDavid Ahern 
349483c44251SDavid Ahern 	if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
349583c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
349683c44251SDavid Ahern 		err = -ENETDOWN;
349783c44251SDavid Ahern 		goto out;
349883c44251SDavid Ahern 	}
349983c44251SDavid Ahern 
350083c44251SDavid Ahern 	if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
350183c44251SDavid Ahern 	    !netif_carrier_ok(dev))
3502ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
350383c44251SDavid Ahern 
3504faee6769SAlexander Aring 	err = fib_nh_common_init(net, &fib6_nh->nh_common, cfg->fc_encap,
35057dd73168SDavid Ahern 				 cfg->fc_encap_type, cfg, gfp_flags, extack);
35067dd73168SDavid Ahern 	if (err)
35077dd73168SDavid Ahern 		goto out;
35087dd73168SDavid Ahern 
35097dd73168SDavid Ahern pcpu_alloc:
3510f40b6ae2SDavid Ahern 	fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3511f40b6ae2SDavid Ahern 	if (!fib6_nh->rt6i_pcpu) {
3512f40b6ae2SDavid Ahern 		err = -ENOMEM;
3513f40b6ae2SDavid Ahern 		goto out;
3514f40b6ae2SDavid Ahern 	}
3515f40b6ae2SDavid Ahern 
3516ad1601aeSDavid Ahern 	fib6_nh->fib_nh_dev = dev;
3517f1741730SDavid Ahern 	fib6_nh->fib_nh_oif = dev->ifindex;
351883c44251SDavid Ahern 	err = 0;
351983c44251SDavid Ahern out:
352083c44251SDavid Ahern 	if (idev)
352183c44251SDavid Ahern 		in6_dev_put(idev);
352283c44251SDavid Ahern 
352383c44251SDavid Ahern 	if (err) {
3524ad1601aeSDavid Ahern 		lwtstate_put(fib6_nh->fib_nh_lws);
3525ad1601aeSDavid Ahern 		fib6_nh->fib_nh_lws = NULL;
352683c44251SDavid Ahern 		if (dev)
352783c44251SDavid Ahern 			dev_put(dev);
352883c44251SDavid Ahern 	}
352983c44251SDavid Ahern 
353083c44251SDavid Ahern 	return err;
353183c44251SDavid Ahern }
353283c44251SDavid Ahern 
3533dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh)
3534dac7d0f2SDavid Ahern {
3535cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
3536cc5c073aSDavid Ahern 
3537cc5c073aSDavid Ahern 	rcu_read_lock();
3538cc5c073aSDavid Ahern 
3539cc5c073aSDavid Ahern 	fib6_nh_flush_exceptions(fib6_nh, NULL);
3540cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3541cc5c073aSDavid Ahern 	if (bucket) {
3542cc5c073aSDavid Ahern 		rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3543cc5c073aSDavid Ahern 		kfree(bucket);
3544cc5c073aSDavid Ahern 	}
3545cc5c073aSDavid Ahern 
3546cc5c073aSDavid Ahern 	rcu_read_unlock();
3547cc5c073aSDavid Ahern 
3548f40b6ae2SDavid Ahern 	if (fib6_nh->rt6i_pcpu) {
3549f40b6ae2SDavid Ahern 		int cpu;
3550f40b6ae2SDavid Ahern 
3551f40b6ae2SDavid Ahern 		for_each_possible_cpu(cpu) {
3552f40b6ae2SDavid Ahern 			struct rt6_info **ppcpu_rt;
3553f40b6ae2SDavid Ahern 			struct rt6_info *pcpu_rt;
3554f40b6ae2SDavid Ahern 
3555f40b6ae2SDavid Ahern 			ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3556f40b6ae2SDavid Ahern 			pcpu_rt = *ppcpu_rt;
3557f40b6ae2SDavid Ahern 			if (pcpu_rt) {
3558f40b6ae2SDavid Ahern 				dst_dev_put(&pcpu_rt->dst);
3559f40b6ae2SDavid Ahern 				dst_release(&pcpu_rt->dst);
3560f40b6ae2SDavid Ahern 				*ppcpu_rt = NULL;
3561f40b6ae2SDavid Ahern 			}
3562f40b6ae2SDavid Ahern 		}
3563f40b6ae2SDavid Ahern 
3564f40b6ae2SDavid Ahern 		free_percpu(fib6_nh->rt6i_pcpu);
3565f40b6ae2SDavid Ahern 	}
3566f40b6ae2SDavid Ahern 
3567979e276eSDavid Ahern 	fib_nh_common_release(&fib6_nh->nh_common);
3568dac7d0f2SDavid Ahern }
3569dac7d0f2SDavid Ahern 
35708d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3571acb54e3cSDavid Ahern 					      gfp_t gfp_flags,
3572333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
35731da177e4SLinus Torvalds {
35745578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
35758d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3576f88d8ea6SDavid Ahern 	struct nexthop *nh = NULL;
3577c71099acSThomas Graf 	struct fib6_table *table;
3578f88d8ea6SDavid Ahern 	struct fib6_nh *fib6_nh;
35798c5b83f0SRoopa Prabhu 	int err = -EINVAL;
358083c44251SDavid Ahern 	int addr_type;
35811da177e4SLinus Torvalds 
3582557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
3583d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
3584d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3585557c44beSDavid Ahern 		goto out;
3586d5d531cbSDavid Ahern 	}
3587557c44beSDavid Ahern 
35882ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
35892ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
35902ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
35912ea2352eSWei Wang 		goto out;
35922ea2352eSWei Wang 	}
35932ea2352eSWei Wang 
3594e8478e80SDavid Ahern 	if (cfg->fc_type > RTN_MAX) {
3595e8478e80SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid route type");
3596e8478e80SDavid Ahern 		goto out;
3597e8478e80SDavid Ahern 	}
3598e8478e80SDavid Ahern 
3599d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
3600d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
36018c5b83f0SRoopa Prabhu 		goto out;
3602d5d531cbSDavid Ahern 	}
3603d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
3604d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
3605d5d531cbSDavid Ahern 		goto out;
3606d5d531cbSDavid Ahern 	}
36071da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
3608d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
3609d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
3610d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
36118c5b83f0SRoopa Prabhu 		goto out;
3612d5d531cbSDavid Ahern 	}
36131da177e4SLinus Torvalds #endif
36145b98324eSDavid Ahern 	if (cfg->fc_nh_id) {
36155b98324eSDavid Ahern 		nh = nexthop_find_by_id(net, cfg->fc_nh_id);
36165b98324eSDavid Ahern 		if (!nh) {
36175b98324eSDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
36185b98324eSDavid Ahern 			goto out;
36195b98324eSDavid Ahern 		}
36205b98324eSDavid Ahern 		err = fib6_check_nexthop(nh, cfg, extack);
36215b98324eSDavid Ahern 		if (err)
36225b98324eSDavid Ahern 			goto out;
36235b98324eSDavid Ahern 	}
3624fc1e64e1SDavid Ahern 
3625c71099acSThomas Graf 	err = -ENOBUFS;
362638308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
3627d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3628d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
362938308473SDavid S. Miller 		if (!table) {
3630f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3631d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
3632d71314b4SMatti Vaittinen 		}
3633d71314b4SMatti Vaittinen 	} else {
3634d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
3635d71314b4SMatti Vaittinen 	}
363638308473SDavid S. Miller 
363738308473SDavid S. Miller 	if (!table)
3638c71099acSThomas Graf 		goto out;
3639c71099acSThomas Graf 
36401da177e4SLinus Torvalds 	err = -ENOMEM;
3641f88d8ea6SDavid Ahern 	rt = fib6_info_alloc(gfp_flags, !nh);
364293531c67SDavid Ahern 	if (!rt)
36431da177e4SLinus Torvalds 		goto out;
364493531c67SDavid Ahern 
3645d7e774f3SDavid Ahern 	rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3646d7e774f3SDavid Ahern 					       extack);
3647767a2217SDavid Ahern 	if (IS_ERR(rt->fib6_metrics)) {
3648767a2217SDavid Ahern 		err = PTR_ERR(rt->fib6_metrics);
3649fda21d46SEric Dumazet 		/* Do not leave garbage there. */
3650fda21d46SEric Dumazet 		rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3651767a2217SDavid Ahern 		goto out;
3652767a2217SDavid Ahern 	}
3653767a2217SDavid Ahern 
365493531c67SDavid Ahern 	if (cfg->fc_flags & RTF_ADDRCONF)
365593531c67SDavid Ahern 		rt->dst_nocount = true;
36561da177e4SLinus Torvalds 
36571716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
365814895687SDavid Ahern 		fib6_set_expires(rt, jiffies +
36591716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
36601716a961SGao feng 	else
366114895687SDavid Ahern 		fib6_clean_expires(rt);
36621da177e4SLinus Torvalds 
366386872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
366486872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
366593c2fb25SDavid Ahern 	rt->fib6_protocol = cfg->fc_protocol;
366686872cb5SThomas Graf 
366783c44251SDavid Ahern 	rt->fib6_table = table;
366883c44251SDavid Ahern 	rt->fib6_metric = cfg->fc_metric;
3669c7036d97SDavid Ahern 	rt->fib6_type = cfg->fc_type ? : RTN_UNICAST;
36702b2450caSDavid Ahern 	rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
367119e42e45SRoopa Prabhu 
367293c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
367393c2fb25SDavid Ahern 	rt->fib6_dst.plen = cfg->fc_dst_len;
36741da177e4SLinus Torvalds 
36751da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
367693c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
367793c2fb25SDavid Ahern 	rt->fib6_src.plen = cfg->fc_src_len;
36781da177e4SLinus Torvalds #endif
3679f88d8ea6SDavid Ahern 	if (nh) {
3680f88d8ea6SDavid Ahern 		if (!nexthop_get(nh)) {
3681f88d8ea6SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
3682f88d8ea6SDavid Ahern 			goto out;
3683f88d8ea6SDavid Ahern 		}
3684f88d8ea6SDavid Ahern 		if (rt->fib6_src.plen) {
36854daa95afSColin Ian King 			NL_SET_ERR_MSG(extack, "Nexthops can not be used with source routing");
3686f88d8ea6SDavid Ahern 			goto out;
3687f88d8ea6SDavid Ahern 		}
3688f88d8ea6SDavid Ahern 		rt->nh = nh;
3689f88d8ea6SDavid Ahern 		fib6_nh = nexthop_fib6_nh(rt->nh);
3690f88d8ea6SDavid Ahern 	} else {
36911cf844c7SDavid Ahern 		err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
36921da177e4SLinus Torvalds 		if (err)
36931da177e4SLinus Torvalds 			goto out;
36949fbb704cSDavid Ahern 
3695f88d8ea6SDavid Ahern 		fib6_nh = rt->fib6_nh;
3696f88d8ea6SDavid Ahern 
3697f88d8ea6SDavid Ahern 		/* We cannot add true routes via loopback here, they would
3698f88d8ea6SDavid Ahern 		 * result in kernel looping; promote them to reject routes
369983c44251SDavid Ahern 		 */
370083c44251SDavid Ahern 		addr_type = ipv6_addr_type(&cfg->fc_dst);
3701f88d8ea6SDavid Ahern 		if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev,
3702f88d8ea6SDavid Ahern 				   addr_type))
370383c44251SDavid Ahern 			rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3704f88d8ea6SDavid Ahern 	}
3705955ec4cbSDavid Ahern 
3706c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3707f88d8ea6SDavid Ahern 		struct net_device *dev = fib6_nh->fib_nh_dev;
370883c44251SDavid Ahern 
3709c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3710d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
3711c3968a85SDaniel Walter 			err = -EINVAL;
3712c3968a85SDaniel Walter 			goto out;
3713c3968a85SDaniel Walter 		}
371493c2fb25SDavid Ahern 		rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
371593c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 128;
3716c3968a85SDaniel Walter 	} else
371793c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
3718c3968a85SDaniel Walter 
37198c5b83f0SRoopa Prabhu 	return rt;
37201da177e4SLinus Torvalds out:
372193531c67SDavid Ahern 	fib6_info_release(rt);
37228c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
37236b9ea5a6SRoopa Prabhu }
37246b9ea5a6SRoopa Prabhu 
3725acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3726333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
37276b9ea5a6SRoopa Prabhu {
37288d1c802bSDavid Ahern 	struct fib6_info *rt;
37296b9ea5a6SRoopa Prabhu 	int err;
37306b9ea5a6SRoopa Prabhu 
3731acb54e3cSDavid Ahern 	rt = ip6_route_info_create(cfg, gfp_flags, extack);
3732d4ead6b3SDavid Ahern 	if (IS_ERR(rt))
3733d4ead6b3SDavid Ahern 		return PTR_ERR(rt);
37346b9ea5a6SRoopa Prabhu 
3735d4ead6b3SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
373693531c67SDavid Ahern 	fib6_info_release(rt);
37376b9ea5a6SRoopa Prabhu 
37381da177e4SLinus Torvalds 	return err;
37391da177e4SLinus Torvalds }
37401da177e4SLinus Torvalds 
37418d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
37421da177e4SLinus Torvalds {
3743afb1d4b5SDavid Ahern 	struct net *net = info->nl_net;
3744c71099acSThomas Graf 	struct fib6_table *table;
3745afb1d4b5SDavid Ahern 	int err;
37461da177e4SLinus Torvalds 
3747421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
37486825a26cSGao feng 		err = -ENOENT;
37496825a26cSGao feng 		goto out;
37506825a26cSGao feng 	}
37516c813a72SPatrick McHardy 
375293c2fb25SDavid Ahern 	table = rt->fib6_table;
375366f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
375486872cb5SThomas Graf 	err = fib6_del(rt, info);
375566f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
37561da177e4SLinus Torvalds 
37576825a26cSGao feng out:
375893531c67SDavid Ahern 	fib6_info_release(rt);
37591da177e4SLinus Torvalds 	return err;
37601da177e4SLinus Torvalds }
37611da177e4SLinus Torvalds 
376211dd74b3SRoopa Prabhu int ip6_del_rt(struct net *net, struct fib6_info *rt, bool skip_notify)
3763e0a1ad73SThomas Graf {
376411dd74b3SRoopa Prabhu 	struct nl_info info = {
376511dd74b3SRoopa Prabhu 		.nl_net = net,
376611dd74b3SRoopa Prabhu 		.skip_notify = skip_notify
376711dd74b3SRoopa Prabhu 	};
3768afb1d4b5SDavid Ahern 
3769528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
3770e0a1ad73SThomas Graf }
3771e0a1ad73SThomas Graf 
37728d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
37730ae81335SDavid Ahern {
37740ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
3775e3330039SWANG Cong 	struct net *net = info->nl_net;
377616a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
37770ae81335SDavid Ahern 	struct fib6_table *table;
3778e3330039SWANG Cong 	int err = -ENOENT;
37790ae81335SDavid Ahern 
3780421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
3781e3330039SWANG Cong 		goto out_put;
378293c2fb25SDavid Ahern 	table = rt->fib6_table;
378366f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
37840ae81335SDavid Ahern 
378593c2fb25SDavid Ahern 	if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
37868d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
37870284696bSIdo Schimmel 		struct fib6_node *fn;
37880ae81335SDavid Ahern 
378916a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
379016a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
379116a16cd3SDavid Ahern 		if (skb) {
379216a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
379316a16cd3SDavid Ahern 
3794d4ead6b3SDavid Ahern 			if (rt6_fill_node(net, skb, rt, NULL,
379516a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
379616a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
379716a16cd3SDavid Ahern 				kfree_skb(skb);
379816a16cd3SDavid Ahern 				skb = NULL;
379916a16cd3SDavid Ahern 			} else
380016a16cd3SDavid Ahern 				info->skip_notify = 1;
380116a16cd3SDavid Ahern 		}
380216a16cd3SDavid Ahern 
38030284696bSIdo Schimmel 		/* 'rt' points to the first sibling route. If it is not the
38040284696bSIdo Schimmel 		 * leaf, then we do not need to send a notification. Otherwise,
38050284696bSIdo Schimmel 		 * we need to check if the last sibling has a next route or not
38060284696bSIdo Schimmel 		 * and emit a replace or delete notification, respectively.
38070284696bSIdo Schimmel 		 */
38082881fd61SIdo Schimmel 		info->skip_notify_kernel = 1;
38090284696bSIdo Schimmel 		fn = rcu_dereference_protected(rt->fib6_node,
38100284696bSIdo Schimmel 					    lockdep_is_held(&table->tb6_lock));
38110284696bSIdo Schimmel 		if (rcu_access_pointer(fn->leaf) == rt) {
38120284696bSIdo Schimmel 			struct fib6_info *last_sibling, *replace_rt;
38130284696bSIdo Schimmel 
38140284696bSIdo Schimmel 			last_sibling = list_last_entry(&rt->fib6_siblings,
38150284696bSIdo Schimmel 						       struct fib6_info,
38160284696bSIdo Schimmel 						       fib6_siblings);
38170284696bSIdo Schimmel 			replace_rt = rcu_dereference_protected(
38180284696bSIdo Schimmel 					    last_sibling->fib6_next,
38190284696bSIdo Schimmel 					    lockdep_is_held(&table->tb6_lock));
38200284696bSIdo Schimmel 			if (replace_rt)
38210284696bSIdo Schimmel 				call_fib6_entry_notifiers_replace(net,
38220284696bSIdo Schimmel 								  replace_rt);
38230284696bSIdo Schimmel 			else
38240284696bSIdo Schimmel 				call_fib6_multipath_entry_notifiers(net,
3825caafb250SIdo Schimmel 						       FIB_EVENT_ENTRY_DEL,
38260284696bSIdo Schimmel 						       rt, rt->fib6_nsiblings,
38270284696bSIdo Schimmel 						       NULL);
38280284696bSIdo Schimmel 		}
38290ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
383093c2fb25SDavid Ahern 					 &rt->fib6_siblings,
383193c2fb25SDavid Ahern 					 fib6_siblings) {
38320ae81335SDavid Ahern 			err = fib6_del(sibling, info);
38330ae81335SDavid Ahern 			if (err)
3834e3330039SWANG Cong 				goto out_unlock;
38350ae81335SDavid Ahern 		}
38360ae81335SDavid Ahern 	}
38370ae81335SDavid Ahern 
38380ae81335SDavid Ahern 	err = fib6_del(rt, info);
3839e3330039SWANG Cong out_unlock:
384066f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
3841e3330039SWANG Cong out_put:
384293531c67SDavid Ahern 	fib6_info_release(rt);
384316a16cd3SDavid Ahern 
384416a16cd3SDavid Ahern 	if (skb) {
3845e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
384616a16cd3SDavid Ahern 			    info->nlh, gfp_any());
384716a16cd3SDavid Ahern 	}
38480ae81335SDavid Ahern 	return err;
38490ae81335SDavid Ahern }
38500ae81335SDavid Ahern 
38510fa6efc5SDavid Ahern static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
385223fb93a4SDavid Ahern {
385323fb93a4SDavid Ahern 	int rc = -ESRCH;
385423fb93a4SDavid Ahern 
385523fb93a4SDavid Ahern 	if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
385623fb93a4SDavid Ahern 		goto out;
385723fb93a4SDavid Ahern 
385823fb93a4SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY &&
385923fb93a4SDavid Ahern 	    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
386023fb93a4SDavid Ahern 		goto out;
3861761f6026SXin Long 
386223fb93a4SDavid Ahern 	rc = rt6_remove_exception_rt(rt);
386323fb93a4SDavid Ahern out:
386423fb93a4SDavid Ahern 	return rc;
386523fb93a4SDavid Ahern }
386623fb93a4SDavid Ahern 
38670fa6efc5SDavid Ahern static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
38680fa6efc5SDavid Ahern 			     struct fib6_nh *nh)
38690fa6efc5SDavid Ahern {
38700fa6efc5SDavid Ahern 	struct fib6_result res = {
38710fa6efc5SDavid Ahern 		.f6i = rt,
38720fa6efc5SDavid Ahern 		.nh = nh,
38730fa6efc5SDavid Ahern 	};
38740fa6efc5SDavid Ahern 	struct rt6_info *rt_cache;
38750fa6efc5SDavid Ahern 
38760fa6efc5SDavid Ahern 	rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
38770fa6efc5SDavid Ahern 	if (rt_cache)
38780fa6efc5SDavid Ahern 		return __ip6_del_cached_rt(rt_cache, cfg);
38790fa6efc5SDavid Ahern 
38800fa6efc5SDavid Ahern 	return 0;
38810fa6efc5SDavid Ahern }
38820fa6efc5SDavid Ahern 
38835b98324eSDavid Ahern struct fib6_nh_del_cached_rt_arg {
38845b98324eSDavid Ahern 	struct fib6_config *cfg;
38855b98324eSDavid Ahern 	struct fib6_info *f6i;
38865b98324eSDavid Ahern };
38875b98324eSDavid Ahern 
38885b98324eSDavid Ahern static int fib6_nh_del_cached_rt(struct fib6_nh *nh, void *_arg)
38895b98324eSDavid Ahern {
38905b98324eSDavid Ahern 	struct fib6_nh_del_cached_rt_arg *arg = _arg;
38915b98324eSDavid Ahern 	int rc;
38925b98324eSDavid Ahern 
38935b98324eSDavid Ahern 	rc = ip6_del_cached_rt(arg->cfg, arg->f6i, nh);
38945b98324eSDavid Ahern 	return rc != -ESRCH ? rc : 0;
38955b98324eSDavid Ahern }
38965b98324eSDavid Ahern 
38975b98324eSDavid Ahern static int ip6_del_cached_rt_nh(struct fib6_config *cfg, struct fib6_info *f6i)
38985b98324eSDavid Ahern {
38995b98324eSDavid Ahern 	struct fib6_nh_del_cached_rt_arg arg = {
39005b98324eSDavid Ahern 		.cfg = cfg,
39015b98324eSDavid Ahern 		.f6i = f6i
39025b98324eSDavid Ahern 	};
39035b98324eSDavid Ahern 
39045b98324eSDavid Ahern 	return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_del_cached_rt, &arg);
39055b98324eSDavid Ahern }
39065b98324eSDavid Ahern 
3907333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
3908333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
39091da177e4SLinus Torvalds {
3910c71099acSThomas Graf 	struct fib6_table *table;
39118d1c802bSDavid Ahern 	struct fib6_info *rt;
39121da177e4SLinus Torvalds 	struct fib6_node *fn;
39131da177e4SLinus Torvalds 	int err = -ESRCH;
39141da177e4SLinus Torvalds 
39155578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3916d5d531cbSDavid Ahern 	if (!table) {
3917d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
3918c71099acSThomas Graf 		return err;
3919d5d531cbSDavid Ahern 	}
39201da177e4SLinus Torvalds 
392166f5d6ceSWei Wang 	rcu_read_lock();
3922c71099acSThomas Graf 
3923c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
392486872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
392538fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
39262b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
39271da177e4SLinus Torvalds 
39281da177e4SLinus Torvalds 	if (fn) {
392966f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
3930ad1601aeSDavid Ahern 			struct fib6_nh *nh;
3931ad1601aeSDavid Ahern 
39323401bfb1SStefano Brivio 			if (rt->nh && cfg->fc_nh_id &&
39333401bfb1SStefano Brivio 			    rt->nh->id != cfg->fc_nh_id)
39345b98324eSDavid Ahern 				continue;
393523fb93a4SDavid Ahern 
39365b98324eSDavid Ahern 			if (cfg->fc_flags & RTF_CACHE) {
39375b98324eSDavid Ahern 				int rc = 0;
39385b98324eSDavid Ahern 
39395b98324eSDavid Ahern 				if (rt->nh) {
39405b98324eSDavid Ahern 					rc = ip6_del_cached_rt_nh(cfg, rt);
39415b98324eSDavid Ahern 				} else if (cfg->fc_nh_id) {
39425b98324eSDavid Ahern 					continue;
39435b98324eSDavid Ahern 				} else {
39445b98324eSDavid Ahern 					nh = rt->fib6_nh;
39450fa6efc5SDavid Ahern 					rc = ip6_del_cached_rt(cfg, rt, nh);
39465b98324eSDavid Ahern 				}
39479e575010SEric Dumazet 				if (rc != -ESRCH) {
39489e575010SEric Dumazet 					rcu_read_unlock();
394923fb93a4SDavid Ahern 					return rc;
395023fb93a4SDavid Ahern 				}
39511f56a01fSMartin KaFai Lau 				continue;
39522b760fcfSWei Wang 			}
3953ad1601aeSDavid Ahern 
39545b98324eSDavid Ahern 			if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
39555b98324eSDavid Ahern 				continue;
39565b98324eSDavid Ahern 			if (cfg->fc_protocol &&
39575b98324eSDavid Ahern 			    cfg->fc_protocol != rt->fib6_protocol)
39585b98324eSDavid Ahern 				continue;
39595b98324eSDavid Ahern 
39605b98324eSDavid Ahern 			if (rt->nh) {
39615b98324eSDavid Ahern 				if (!fib6_info_hold_safe(rt))
39625b98324eSDavid Ahern 					continue;
39635b98324eSDavid Ahern 				rcu_read_unlock();
39645b98324eSDavid Ahern 
39655b98324eSDavid Ahern 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
39665b98324eSDavid Ahern 			}
39675b98324eSDavid Ahern 			if (cfg->fc_nh_id)
39685b98324eSDavid Ahern 				continue;
39695b98324eSDavid Ahern 
39705b98324eSDavid Ahern 			nh = rt->fib6_nh;
397186872cb5SThomas Graf 			if (cfg->fc_ifindex &&
3972ad1601aeSDavid Ahern 			    (!nh->fib_nh_dev ||
3973ad1601aeSDavid Ahern 			     nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
39741da177e4SLinus Torvalds 				continue;
397586872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
3976ad1601aeSDavid Ahern 			    !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
39771da177e4SLinus Torvalds 				continue;
3978e873e4b9SWei Wang 			if (!fib6_info_hold_safe(rt))
3979e873e4b9SWei Wang 				continue;
398066f5d6ceSWei Wang 			rcu_read_unlock();
39811da177e4SLinus Torvalds 
39820ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
39830ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
398486872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
39850ae81335SDavid Ahern 
39860ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
39871da177e4SLinus Torvalds 		}
39881da177e4SLinus Torvalds 	}
398966f5d6ceSWei Wang 	rcu_read_unlock();
39901da177e4SLinus Torvalds 
39911da177e4SLinus Torvalds 	return err;
39921da177e4SLinus Torvalds }
39931da177e4SLinus Torvalds 
39946700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3995a6279458SYOSHIFUJI Hideaki {
3996a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
3997e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
399885bd05deSDavid Ahern 	struct fib6_result res = {};
3999e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
4000e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
4001e8599ff4SDavid S. Miller 	struct neighbour *neigh;
400271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
40036e157b6aSDavid S. Miller 	int optlen, on_link;
40046e157b6aSDavid S. Miller 	u8 *lladdr;
4005e8599ff4SDavid S. Miller 
400629a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
400771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
4008e8599ff4SDavid S. Miller 
4009e8599ff4SDavid S. Miller 	if (optlen < 0) {
40106e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
4011e8599ff4SDavid S. Miller 		return;
4012e8599ff4SDavid S. Miller 	}
4013e8599ff4SDavid S. Miller 
401471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
4015e8599ff4SDavid S. Miller 
401671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
40176e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
4018e8599ff4SDavid S. Miller 		return;
4019e8599ff4SDavid S. Miller 	}
4020e8599ff4SDavid S. Miller 
40216e157b6aSDavid S. Miller 	on_link = 0;
402271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
4023e8599ff4SDavid S. Miller 		on_link = 1;
402471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
4025e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
40266e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
4027e8599ff4SDavid S. Miller 		return;
4028e8599ff4SDavid S. Miller 	}
4029e8599ff4SDavid S. Miller 
4030e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
4031e8599ff4SDavid S. Miller 	if (!in6_dev)
4032e8599ff4SDavid S. Miller 		return;
4033e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
4034e8599ff4SDavid S. Miller 		return;
4035e8599ff4SDavid S. Miller 
4036e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
4037e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
4038e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
4039e8599ff4SDavid S. Miller 	 */
4040e8599ff4SDavid S. Miller 
4041f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
4042e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
4043e8599ff4SDavid S. Miller 		return;
4044e8599ff4SDavid S. Miller 	}
40456e157b6aSDavid S. Miller 
40466e157b6aSDavid S. Miller 	lladdr = NULL;
4047e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
4048e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
4049e8599ff4SDavid S. Miller 					     skb->dev);
4050e8599ff4SDavid S. Miller 		if (!lladdr) {
4051e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
4052e8599ff4SDavid S. Miller 			return;
4053e8599ff4SDavid S. Miller 		}
4054e8599ff4SDavid S. Miller 	}
4055e8599ff4SDavid S. Miller 
40566e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
4057ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
40586e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
40596e157b6aSDavid S. Miller 		return;
40606e157b6aSDavid S. Miller 	}
40616e157b6aSDavid S. Miller 
40626e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
40636e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
40646e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
40656e157b6aSDavid S. Miller 	 */
40660dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
40676e157b6aSDavid S. Miller 
406871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
4069e8599ff4SDavid S. Miller 	if (!neigh)
4070e8599ff4SDavid S. Miller 		return;
4071e8599ff4SDavid S. Miller 
40721da177e4SLinus Torvalds 	/*
40731da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
40741da177e4SLinus Torvalds 	 */
40751da177e4SLinus Torvalds 
4076f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
40771da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
40781da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
40791da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
4080f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
4081f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
40821da177e4SLinus Torvalds 
40834d85cd0cSDavid Ahern 	rcu_read_lock();
408485bd05deSDavid Ahern 	res.f6i = rcu_dereference(rt->from);
4085ff24e498SDavid S. Miller 	if (!res.f6i)
4086886b7a50SMartin KaFai Lau 		goto out;
40878a14e46fSDavid Ahern 
408849d5b8efSDavid Ahern 	if (res.f6i->nh) {
408949d5b8efSDavid Ahern 		struct fib6_nh_match_arg arg = {
409049d5b8efSDavid Ahern 			.dev = dst->dev,
409149d5b8efSDavid Ahern 			.gw = &rt->rt6i_gateway,
409249d5b8efSDavid Ahern 		};
409349d5b8efSDavid Ahern 
409449d5b8efSDavid Ahern 		nexthop_for_each_fib6_nh(res.f6i->nh,
409549d5b8efSDavid Ahern 					 fib6_nh_find_match, &arg);
409649d5b8efSDavid Ahern 
409749d5b8efSDavid Ahern 		/* fib6_info uses a nexthop that does not have fib6_nh
409849d5b8efSDavid Ahern 		 * using the dst->dev. Should be impossible
409949d5b8efSDavid Ahern 		 */
410049d5b8efSDavid Ahern 		if (!arg.match)
410149d5b8efSDavid Ahern 			goto out;
410249d5b8efSDavid Ahern 		res.nh = arg.match;
410349d5b8efSDavid Ahern 	} else {
41041cf844c7SDavid Ahern 		res.nh = res.f6i->fib6_nh;
410549d5b8efSDavid Ahern 	}
410649d5b8efSDavid Ahern 
41077d21fec9SDavid Ahern 	res.fib6_flags = res.f6i->fib6_flags;
41087d21fec9SDavid Ahern 	res.fib6_type = res.f6i->fib6_type;
410985bd05deSDavid Ahern 	nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
411038308473SDavid S. Miller 	if (!nrt)
41111da177e4SLinus Torvalds 		goto out;
41121da177e4SLinus Torvalds 
41131da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
41141da177e4SLinus Torvalds 	if (on_link)
41151da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
41161da177e4SLinus Torvalds 
41174e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
41181da177e4SLinus Torvalds 
4119886b7a50SMartin KaFai Lau 	/* rt6_insert_exception() will take care of duplicated exceptions */
41205012f0a5SDavid Ahern 	if (rt6_insert_exception(nrt, &res)) {
41212b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
41222b760fcfSWei Wang 		goto out;
41232b760fcfSWei Wang 	}
41241da177e4SLinus Torvalds 
4125d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
4126d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
412771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
412860592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
41298d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
41308d71740cSTom Tucker 
41311da177e4SLinus Torvalds out:
4132886b7a50SMartin KaFai Lau 	rcu_read_unlock();
4133e8599ff4SDavid S. Miller 	neigh_release(neigh);
41346e157b6aSDavid S. Miller }
41356e157b6aSDavid S. Miller 
413670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
41378d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
4138b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
4139830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
4140830218c1SDavid Ahern 					   struct net_device *dev)
414170ceb4f5SYOSHIFUJI Hideaki {
4142830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
4143830218c1SDavid Ahern 	int ifindex = dev->ifindex;
414470ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
41458d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
4146c71099acSThomas Graf 	struct fib6_table *table;
414770ceb4f5SYOSHIFUJI Hideaki 
4148830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
414938308473SDavid S. Miller 	if (!table)
4150c71099acSThomas Graf 		return NULL;
4151c71099acSThomas Graf 
415266f5d6ceSWei Wang 	rcu_read_lock();
415338fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
415470ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
415570ceb4f5SYOSHIFUJI Hideaki 		goto out;
415670ceb4f5SYOSHIFUJI Hideaki 
415766f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
4158f88d8ea6SDavid Ahern 		/* these routes do not use nexthops */
4159f88d8ea6SDavid Ahern 		if (rt->nh)
4160f88d8ea6SDavid Ahern 			continue;
41611cf844c7SDavid Ahern 		if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
416270ceb4f5SYOSHIFUJI Hideaki 			continue;
41632b2450caSDavid Ahern 		if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
41641cf844c7SDavid Ahern 		    !rt->fib6_nh->fib_nh_gw_family)
416570ceb4f5SYOSHIFUJI Hideaki 			continue;
41661cf844c7SDavid Ahern 		if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
416770ceb4f5SYOSHIFUJI Hideaki 			continue;
4168e873e4b9SWei Wang 		if (!fib6_info_hold_safe(rt))
4169e873e4b9SWei Wang 			continue;
417070ceb4f5SYOSHIFUJI Hideaki 		break;
417170ceb4f5SYOSHIFUJI Hideaki 	}
417270ceb4f5SYOSHIFUJI Hideaki out:
417366f5d6ceSWei Wang 	rcu_read_unlock();
417470ceb4f5SYOSHIFUJI Hideaki 	return rt;
417570ceb4f5SYOSHIFUJI Hideaki }
417670ceb4f5SYOSHIFUJI Hideaki 
41778d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
4178b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
4179830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
4180830218c1SDavid Ahern 					   struct net_device *dev,
418195c96174SEric Dumazet 					   unsigned int pref)
418270ceb4f5SYOSHIFUJI Hideaki {
418386872cb5SThomas Graf 	struct fib6_config cfg = {
4184238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
4185830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
418686872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
418786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
418886872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
4189b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
4190e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
419115e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
4192efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
4193efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
419486872cb5SThomas Graf 	};
419570ceb4f5SYOSHIFUJI Hideaki 
4196830218c1SDavid Ahern 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
41974e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
41984e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
419986872cb5SThomas Graf 
4200e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
4201e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
420286872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
420370ceb4f5SYOSHIFUJI Hideaki 
4204acb54e3cSDavid Ahern 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
420570ceb4f5SYOSHIFUJI Hideaki 
4206830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
420770ceb4f5SYOSHIFUJI Hideaki }
420870ceb4f5SYOSHIFUJI Hideaki #endif
420970ceb4f5SYOSHIFUJI Hideaki 
42108d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net,
4211afb1d4b5SDavid Ahern 				     const struct in6_addr *addr,
4212afb1d4b5SDavid Ahern 				     struct net_device *dev)
42131da177e4SLinus Torvalds {
4214830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
42158d1c802bSDavid Ahern 	struct fib6_info *rt;
4216c71099acSThomas Graf 	struct fib6_table *table;
42171da177e4SLinus Torvalds 
4218afb1d4b5SDavid Ahern 	table = fib6_get_table(net, tb_id);
421938308473SDavid S. Miller 	if (!table)
4220c71099acSThomas Graf 		return NULL;
42211da177e4SLinus Torvalds 
422266f5d6ceSWei Wang 	rcu_read_lock();
422366f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
4224f88d8ea6SDavid Ahern 		struct fib6_nh *nh;
4225ad1601aeSDavid Ahern 
4226f88d8ea6SDavid Ahern 		/* RA routes do not use nexthops */
4227f88d8ea6SDavid Ahern 		if (rt->nh)
4228f88d8ea6SDavid Ahern 			continue;
4229f88d8ea6SDavid Ahern 
4230f88d8ea6SDavid Ahern 		nh = rt->fib6_nh;
4231ad1601aeSDavid Ahern 		if (dev == nh->fib_nh_dev &&
423293c2fb25SDavid Ahern 		    ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
4233ad1601aeSDavid Ahern 		    ipv6_addr_equal(&nh->fib_nh_gw6, addr))
42341da177e4SLinus Torvalds 			break;
42351da177e4SLinus Torvalds 	}
4236e873e4b9SWei Wang 	if (rt && !fib6_info_hold_safe(rt))
4237e873e4b9SWei Wang 		rt = NULL;
423866f5d6ceSWei Wang 	rcu_read_unlock();
42391da177e4SLinus Torvalds 	return rt;
42401da177e4SLinus Torvalds }
42411da177e4SLinus Torvalds 
42428d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net,
4243afb1d4b5SDavid Ahern 				     const struct in6_addr *gwaddr,
4244ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
4245ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
42461da177e4SLinus Torvalds {
424786872cb5SThomas Graf 	struct fib6_config cfg = {
4248ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
4249238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
425086872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
425186872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
425286872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
4253b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
4254e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
425515e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
42565578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
4257afb1d4b5SDavid Ahern 		.fc_nlinfo.nl_net = net,
425886872cb5SThomas Graf 	};
42591da177e4SLinus Torvalds 
42604e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
42611da177e4SLinus Torvalds 
4262acb54e3cSDavid Ahern 	if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
4263830218c1SDavid Ahern 		struct fib6_table *table;
4264830218c1SDavid Ahern 
4265830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
4266830218c1SDavid Ahern 		if (table)
4267830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
4268830218c1SDavid Ahern 	}
42691da177e4SLinus Torvalds 
4270afb1d4b5SDavid Ahern 	return rt6_get_dflt_router(net, gwaddr, dev);
42711da177e4SLinus Torvalds }
42721da177e4SLinus Torvalds 
4273afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net,
4274afb1d4b5SDavid Ahern 				     struct fib6_table *table)
42751da177e4SLinus Torvalds {
42768d1c802bSDavid Ahern 	struct fib6_info *rt;
42771da177e4SLinus Torvalds 
42781da177e4SLinus Torvalds restart:
427966f5d6ceSWei Wang 	rcu_read_lock();
428066f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
4281dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
4282dcd1f572SDavid Ahern 		struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
4283dcd1f572SDavid Ahern 
428493c2fb25SDavid Ahern 		if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
4285e873e4b9SWei Wang 		    (!idev || idev->cnf.accept_ra != 2) &&
4286e873e4b9SWei Wang 		    fib6_info_hold_safe(rt)) {
428766f5d6ceSWei Wang 			rcu_read_unlock();
428811dd74b3SRoopa Prabhu 			ip6_del_rt(net, rt, false);
42891da177e4SLinus Torvalds 			goto restart;
42901da177e4SLinus Torvalds 		}
42911da177e4SLinus Torvalds 	}
429266f5d6ceSWei Wang 	rcu_read_unlock();
4293830218c1SDavid Ahern 
4294830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
4295830218c1SDavid Ahern }
4296830218c1SDavid Ahern 
4297830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
4298830218c1SDavid Ahern {
4299830218c1SDavid Ahern 	struct fib6_table *table;
4300830218c1SDavid Ahern 	struct hlist_head *head;
4301830218c1SDavid Ahern 	unsigned int h;
4302830218c1SDavid Ahern 
4303830218c1SDavid Ahern 	rcu_read_lock();
4304830218c1SDavid Ahern 
4305830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
4306830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
4307830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
4308830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
4309afb1d4b5SDavid Ahern 				__rt6_purge_dflt_routers(net, table);
4310830218c1SDavid Ahern 		}
4311830218c1SDavid Ahern 	}
4312830218c1SDavid Ahern 
4313830218c1SDavid Ahern 	rcu_read_unlock();
43141da177e4SLinus Torvalds }
43151da177e4SLinus Torvalds 
43165578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
43175578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
431886872cb5SThomas Graf 				 struct fib6_config *cfg)
431986872cb5SThomas Graf {
43208823a3acSMaciej Żenczykowski 	*cfg = (struct fib6_config){
43218823a3acSMaciej Żenczykowski 		.fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
43228823a3acSMaciej Żenczykowski 			 : RT6_TABLE_MAIN,
43238823a3acSMaciej Żenczykowski 		.fc_ifindex = rtmsg->rtmsg_ifindex,
432467f69513SDavid Ahern 		.fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
43258823a3acSMaciej Żenczykowski 		.fc_expires = rtmsg->rtmsg_info,
43268823a3acSMaciej Żenczykowski 		.fc_dst_len = rtmsg->rtmsg_dst_len,
43278823a3acSMaciej Żenczykowski 		.fc_src_len = rtmsg->rtmsg_src_len,
43288823a3acSMaciej Żenczykowski 		.fc_flags = rtmsg->rtmsg_flags,
43298823a3acSMaciej Żenczykowski 		.fc_type = rtmsg->rtmsg_type,
433086872cb5SThomas Graf 
43318823a3acSMaciej Żenczykowski 		.fc_nlinfo.nl_net = net,
433286872cb5SThomas Graf 
43338823a3acSMaciej Żenczykowski 		.fc_dst = rtmsg->rtmsg_dst,
43348823a3acSMaciej Żenczykowski 		.fc_src = rtmsg->rtmsg_src,
43358823a3acSMaciej Żenczykowski 		.fc_gateway = rtmsg->rtmsg_gateway,
43368823a3acSMaciej Żenczykowski 	};
433786872cb5SThomas Graf }
433886872cb5SThomas Graf 
4339*7c1552daSChristoph Hellwig int ipv6_route_ioctl(struct net *net, unsigned int cmd, struct in6_rtmsg *rtmsg)
43401da177e4SLinus Torvalds {
434186872cb5SThomas Graf 	struct fib6_config cfg;
43421da177e4SLinus Torvalds 	int err;
43431da177e4SLinus Torvalds 
4344*7c1552daSChristoph Hellwig 	if (cmd != SIOCADDRT && cmd != SIOCDELRT)
4345*7c1552daSChristoph Hellwig 		return -EINVAL;
4346af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
43471da177e4SLinus Torvalds 		return -EPERM;
43481da177e4SLinus Torvalds 
4349*7c1552daSChristoph Hellwig 	rtmsg_to_fib6_config(net, rtmsg, &cfg);
435086872cb5SThomas Graf 
43511da177e4SLinus Torvalds 	rtnl_lock();
43521da177e4SLinus Torvalds 	switch (cmd) {
43531da177e4SLinus Torvalds 	case SIOCADDRT:
4354acb54e3cSDavid Ahern 		err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
43551da177e4SLinus Torvalds 		break;
43561da177e4SLinus Torvalds 	case SIOCDELRT:
4357333c4301SDavid Ahern 		err = ip6_route_del(&cfg, NULL);
43581da177e4SLinus Torvalds 		break;
43591da177e4SLinus Torvalds 	}
43601da177e4SLinus Torvalds 	rtnl_unlock();
43611da177e4SLinus Torvalds 	return err;
43623ff50b79SStephen Hemminger }
43631da177e4SLinus Torvalds 
43641da177e4SLinus Torvalds /*
43651da177e4SLinus Torvalds  *	Drop the packet on the floor
43661da177e4SLinus Torvalds  */
43671da177e4SLinus Torvalds 
4368d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
43691da177e4SLinus Torvalds {
4370adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
43711d3fd8a1SStephen Suryaputra 	struct net *net = dev_net(dst->dev);
43721d3fd8a1SStephen Suryaputra 	struct inet6_dev *idev;
43731d3fd8a1SStephen Suryaputra 	int type;
43741d3fd8a1SStephen Suryaputra 
43751d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev) &&
43761d3fd8a1SStephen Suryaputra 	    dst->dev == net->loopback_dev)
43771d3fd8a1SStephen Suryaputra 		idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
43781d3fd8a1SStephen Suryaputra 	else
43791d3fd8a1SStephen Suryaputra 		idev = ip6_dst_idev(dst);
43801d3fd8a1SStephen Suryaputra 
4381612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
4382612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
43830660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
438445bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
43851d3fd8a1SStephen Suryaputra 			IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
4386612f09e8SYOSHIFUJI Hideaki 			break;
4387612f09e8SYOSHIFUJI Hideaki 		}
4388a8eceea8SJoe Perches 		fallthrough;
4389612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
43901d3fd8a1SStephen Suryaputra 		IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
4391612f09e8SYOSHIFUJI Hideaki 		break;
4392612f09e8SYOSHIFUJI Hideaki 	}
43931d3fd8a1SStephen Suryaputra 
43941d3fd8a1SStephen Suryaputra 	/* Start over by dropping the dst for l3mdev case */
43951d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev))
43961d3fd8a1SStephen Suryaputra 		skb_dst_drop(skb);
43971d3fd8a1SStephen Suryaputra 
43983ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
43991da177e4SLinus Torvalds 	kfree_skb(skb);
44001da177e4SLinus Torvalds 	return 0;
44011da177e4SLinus Torvalds }
44021da177e4SLinus Torvalds 
44039ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
44049ce8ade0SThomas Graf {
4405612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
44069ce8ade0SThomas Graf }
44079ce8ade0SThomas Graf 
4408ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
44091da177e4SLinus Torvalds {
4410adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
4411612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
44121da177e4SLinus Torvalds }
44131da177e4SLinus Torvalds 
44149ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
44159ce8ade0SThomas Graf {
4416612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
44179ce8ade0SThomas Graf }
44189ce8ade0SThomas Graf 
4419ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
44209ce8ade0SThomas Graf {
4421adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
4422612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
44239ce8ade0SThomas Graf }
44249ce8ade0SThomas Graf 
44251da177e4SLinus Torvalds /*
44261da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
44271da177e4SLinus Torvalds  */
44281da177e4SLinus Torvalds 
4429360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net,
4430afb1d4b5SDavid Ahern 				     struct inet6_dev *idev,
44311da177e4SLinus Torvalds 				     const struct in6_addr *addr,
4432acb54e3cSDavid Ahern 				     bool anycast, gfp_t gfp_flags)
44331da177e4SLinus Torvalds {
4434c7a1ce39SDavid Ahern 	struct fib6_config cfg = {
4435c7a1ce39SDavid Ahern 		.fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
4436c7a1ce39SDavid Ahern 		.fc_ifindex = idev->dev->ifindex,
4437d55a2e37SMaciej Żenczykowski 		.fc_flags = RTF_UP | RTF_NONEXTHOP,
4438c7a1ce39SDavid Ahern 		.fc_dst = *addr,
4439c7a1ce39SDavid Ahern 		.fc_dst_len = 128,
4440c7a1ce39SDavid Ahern 		.fc_protocol = RTPROT_KERNEL,
4441c7a1ce39SDavid Ahern 		.fc_nlinfo.nl_net = net,
4442c7a1ce39SDavid Ahern 		.fc_ignore_dev_down = true,
4443c7a1ce39SDavid Ahern 	};
4444d55a2e37SMaciej Żenczykowski 	struct fib6_info *f6i;
44455f02ce24SDavid Ahern 
4446e8478e80SDavid Ahern 	if (anycast) {
4447c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_ANYCAST;
4448c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_ANYCAST;
4449e8478e80SDavid Ahern 	} else {
4450c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_LOCAL;
4451c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_LOCAL;
4452e8478e80SDavid Ahern 	}
44531da177e4SLinus Torvalds 
4454d55a2e37SMaciej Żenczykowski 	f6i = ip6_route_info_create(&cfg, gfp_flags, NULL);
44558652f17cSMaciej Żenczykowski 	if (!IS_ERR(f6i))
4456d55a2e37SMaciej Żenczykowski 		f6i->dst_nocount = true;
4457d55a2e37SMaciej Żenczykowski 	return f6i;
44581da177e4SLinus Torvalds }
44591da177e4SLinus Torvalds 
4460c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
4461c3968a85SDaniel Walter struct arg_dev_net_ip {
4462c3968a85SDaniel Walter 	struct net_device *dev;
4463c3968a85SDaniel Walter 	struct net *net;
4464c3968a85SDaniel Walter 	struct in6_addr *addr;
4465c3968a85SDaniel Walter };
4466c3968a85SDaniel Walter 
44678d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
4468c3968a85SDaniel Walter {
4469c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
4470c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
4471c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
4472c3968a85SDaniel Walter 
4473f88d8ea6SDavid Ahern 	if (!rt->nh &&
4474f88d8ea6SDavid Ahern 	    ((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
4475421842edSDavid Ahern 	    rt != net->ipv6.fib6_null_entry &&
447693c2fb25SDavid Ahern 	    ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
447760006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
4478c3968a85SDaniel Walter 		/* remove prefsrc entry */
447993c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
448060006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
4481c3968a85SDaniel Walter 	}
4482c3968a85SDaniel Walter 	return 0;
4483c3968a85SDaniel Walter }
4484c3968a85SDaniel Walter 
4485c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4486c3968a85SDaniel Walter {
4487c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
4488c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
4489c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
4490c3968a85SDaniel Walter 		.net = net,
4491c3968a85SDaniel Walter 		.addr = &ifp->addr,
4492c3968a85SDaniel Walter 	};
44930c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4494c3968a85SDaniel Walter }
4495c3968a85SDaniel Walter 
44962b2450caSDavid Ahern #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT)
4497be7a010dSDuan Jiong 
4498be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
44998d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4500be7a010dSDuan Jiong {
4501be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
4502f88d8ea6SDavid Ahern 	struct fib6_nh *nh;
4503be7a010dSDuan Jiong 
4504f88d8ea6SDavid Ahern 	/* RA routes do not use nexthops */
4505f88d8ea6SDavid Ahern 	if (rt->nh)
4506f88d8ea6SDavid Ahern 		return 0;
4507f88d8ea6SDavid Ahern 
4508f88d8ea6SDavid Ahern 	nh = rt->fib6_nh;
450993c2fb25SDavid Ahern 	if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
4510cc5c073aSDavid Ahern 	    nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
4511be7a010dSDuan Jiong 		return -1;
4512b16cb459SWei Wang 
4513b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
4514b16cb459SWei Wang 	 * This is needed because cached route may have a different
4515b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
4516b16cb459SWei Wang 	 */
4517cc5c073aSDavid Ahern 	fib6_nh_exceptions_clean_tohost(nh, gateway);
4518b16cb459SWei Wang 
4519be7a010dSDuan Jiong 	return 0;
4520be7a010dSDuan Jiong }
4521be7a010dSDuan Jiong 
4522be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4523be7a010dSDuan Jiong {
4524be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
4525be7a010dSDuan Jiong }
4526be7a010dSDuan Jiong 
45272127d95aSIdo Schimmel struct arg_netdev_event {
45282127d95aSIdo Schimmel 	const struct net_device *dev;
45294c981e28SIdo Schimmel 	union {
4530ecc5663cSDavid Ahern 		unsigned char nh_flags;
45314c981e28SIdo Schimmel 		unsigned long event;
45324c981e28SIdo Schimmel 	};
45332127d95aSIdo Schimmel };
45342127d95aSIdo Schimmel 
45358d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4536d7dedee1SIdo Schimmel {
45378d1c802bSDavid Ahern 	struct fib6_info *iter;
4538d7dedee1SIdo Schimmel 	struct fib6_node *fn;
4539d7dedee1SIdo Schimmel 
454093c2fb25SDavid Ahern 	fn = rcu_dereference_protected(rt->fib6_node,
454193c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
4542d7dedee1SIdo Schimmel 	iter = rcu_dereference_protected(fn->leaf,
454393c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
4544d7dedee1SIdo Schimmel 	while (iter) {
454593c2fb25SDavid Ahern 		if (iter->fib6_metric == rt->fib6_metric &&
454633bd5ac5SDavid Ahern 		    rt6_qualify_for_ecmp(iter))
4547d7dedee1SIdo Schimmel 			return iter;
45488fb11a9aSDavid Ahern 		iter = rcu_dereference_protected(iter->fib6_next,
454993c2fb25SDavid Ahern 				lockdep_is_held(&rt->fib6_table->tb6_lock));
4550d7dedee1SIdo Schimmel 	}
4551d7dedee1SIdo Schimmel 
4552d7dedee1SIdo Schimmel 	return NULL;
4553d7dedee1SIdo Schimmel }
4554d7dedee1SIdo Schimmel 
4555f88d8ea6SDavid Ahern /* only called for fib entries with builtin fib6_nh */
45568d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt)
4557d7dedee1SIdo Schimmel {
45581cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
45591cf844c7SDavid Ahern 	    (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
45601cf844c7SDavid Ahern 	     ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
4561d7dedee1SIdo Schimmel 		return true;
4562d7dedee1SIdo Schimmel 
4563d7dedee1SIdo Schimmel 	return false;
4564d7dedee1SIdo Schimmel }
4565d7dedee1SIdo Schimmel 
45668d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt)
4567d7dedee1SIdo Schimmel {
45688d1c802bSDavid Ahern 	struct fib6_info *iter;
4569d7dedee1SIdo Schimmel 	int total = 0;
4570d7dedee1SIdo Schimmel 
4571d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt))
45721cf844c7SDavid Ahern 		total += rt->fib6_nh->fib_nh_weight;
4573d7dedee1SIdo Schimmel 
457493c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4575d7dedee1SIdo Schimmel 		if (!rt6_is_dead(iter))
45761cf844c7SDavid Ahern 			total += iter->fib6_nh->fib_nh_weight;
4577d7dedee1SIdo Schimmel 	}
4578d7dedee1SIdo Schimmel 
4579d7dedee1SIdo Schimmel 	return total;
4580d7dedee1SIdo Schimmel }
4581d7dedee1SIdo Schimmel 
45828d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4583d7dedee1SIdo Schimmel {
4584d7dedee1SIdo Schimmel 	int upper_bound = -1;
4585d7dedee1SIdo Schimmel 
4586d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt)) {
45871cf844c7SDavid Ahern 		*weight += rt->fib6_nh->fib_nh_weight;
4588d7dedee1SIdo Schimmel 		upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4589d7dedee1SIdo Schimmel 						    total) - 1;
4590d7dedee1SIdo Schimmel 	}
45911cf844c7SDavid Ahern 	atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
4592d7dedee1SIdo Schimmel }
4593d7dedee1SIdo Schimmel 
45948d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4595d7dedee1SIdo Schimmel {
45968d1c802bSDavid Ahern 	struct fib6_info *iter;
4597d7dedee1SIdo Schimmel 	int weight = 0;
4598d7dedee1SIdo Schimmel 
4599d7dedee1SIdo Schimmel 	rt6_upper_bound_set(rt, &weight, total);
4600d7dedee1SIdo Schimmel 
460193c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4602d7dedee1SIdo Schimmel 		rt6_upper_bound_set(iter, &weight, total);
4603d7dedee1SIdo Schimmel }
4604d7dedee1SIdo Schimmel 
46058d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt)
4606d7dedee1SIdo Schimmel {
46078d1c802bSDavid Ahern 	struct fib6_info *first;
4608d7dedee1SIdo Schimmel 	int total;
4609d7dedee1SIdo Schimmel 
4610d7dedee1SIdo Schimmel 	/* In case the entire multipath route was marked for flushing,
4611d7dedee1SIdo Schimmel 	 * then there is no need to rebalance upon the removal of every
4612d7dedee1SIdo Schimmel 	 * sibling route.
4613d7dedee1SIdo Schimmel 	 */
461493c2fb25SDavid Ahern 	if (!rt->fib6_nsiblings || rt->should_flush)
4615d7dedee1SIdo Schimmel 		return;
4616d7dedee1SIdo Schimmel 
4617d7dedee1SIdo Schimmel 	/* During lookup routes are evaluated in order, so we need to
4618d7dedee1SIdo Schimmel 	 * make sure upper bounds are assigned from the first sibling
4619d7dedee1SIdo Schimmel 	 * onwards.
4620d7dedee1SIdo Schimmel 	 */
4621d7dedee1SIdo Schimmel 	first = rt6_multipath_first_sibling(rt);
4622d7dedee1SIdo Schimmel 	if (WARN_ON_ONCE(!first))
4623d7dedee1SIdo Schimmel 		return;
4624d7dedee1SIdo Schimmel 
4625d7dedee1SIdo Schimmel 	total = rt6_multipath_total_weight(first);
4626d7dedee1SIdo Schimmel 	rt6_multipath_upper_bound_set(first, total);
4627d7dedee1SIdo Schimmel }
4628d7dedee1SIdo Schimmel 
46298d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg)
46302127d95aSIdo Schimmel {
46312127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
46327aef6859SDavid Ahern 	struct net *net = dev_net(arg->dev);
46332127d95aSIdo Schimmel 
4634f88d8ea6SDavid Ahern 	if (rt != net->ipv6.fib6_null_entry && !rt->nh &&
46351cf844c7SDavid Ahern 	    rt->fib6_nh->fib_nh_dev == arg->dev) {
46361cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
46377aef6859SDavid Ahern 		fib6_update_sernum_upto_root(net, rt);
4638d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
46391de178edSIdo Schimmel 	}
46402127d95aSIdo Schimmel 
46412127d95aSIdo Schimmel 	return 0;
46422127d95aSIdo Schimmel }
46432127d95aSIdo Schimmel 
4644ecc5663cSDavid Ahern void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
46452127d95aSIdo Schimmel {
46462127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
46472127d95aSIdo Schimmel 		.dev = dev,
46486802f3adSIdo Schimmel 		{
46492127d95aSIdo Schimmel 			.nh_flags = nh_flags,
46506802f3adSIdo Schimmel 		},
46512127d95aSIdo Schimmel 	};
46522127d95aSIdo Schimmel 
46532127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
46542127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
46552127d95aSIdo Schimmel 
46562127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
46572127d95aSIdo Schimmel }
46582127d95aSIdo Schimmel 
4659f88d8ea6SDavid Ahern /* only called for fib entries with inline fib6_nh */
46608d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
46611de178edSIdo Schimmel 				   const struct net_device *dev)
46621de178edSIdo Schimmel {
46638d1c802bSDavid Ahern 	struct fib6_info *iter;
46641de178edSIdo Schimmel 
46651cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == dev)
46661de178edSIdo Schimmel 		return true;
466793c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
46681cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == dev)
46691de178edSIdo Schimmel 			return true;
46701de178edSIdo Schimmel 
46711de178edSIdo Schimmel 	return false;
46721de178edSIdo Schimmel }
46731de178edSIdo Schimmel 
46748d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt)
46751de178edSIdo Schimmel {
46768d1c802bSDavid Ahern 	struct fib6_info *iter;
46771de178edSIdo Schimmel 
46781de178edSIdo Schimmel 	rt->should_flush = 1;
467993c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
46801de178edSIdo Schimmel 		iter->should_flush = 1;
46811de178edSIdo Schimmel }
46821de178edSIdo Schimmel 
46838d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
46841de178edSIdo Schimmel 					     const struct net_device *down_dev)
46851de178edSIdo Schimmel {
46868d1c802bSDavid Ahern 	struct fib6_info *iter;
46871de178edSIdo Schimmel 	unsigned int dead = 0;
46881de178edSIdo Schimmel 
46891cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == down_dev ||
46901cf844c7SDavid Ahern 	    rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
46911de178edSIdo Schimmel 		dead++;
469293c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
46931cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == down_dev ||
46941cf844c7SDavid Ahern 		    iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
46951de178edSIdo Schimmel 			dead++;
46961de178edSIdo Schimmel 
46971de178edSIdo Schimmel 	return dead;
46981de178edSIdo Schimmel }
46991de178edSIdo Schimmel 
47008d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
47011de178edSIdo Schimmel 				       const struct net_device *dev,
4702ecc5663cSDavid Ahern 				       unsigned char nh_flags)
47031de178edSIdo Schimmel {
47048d1c802bSDavid Ahern 	struct fib6_info *iter;
47051de178edSIdo Schimmel 
47061cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == dev)
47071cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags |= nh_flags;
470893c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
47091cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == dev)
47101cf844c7SDavid Ahern 			iter->fib6_nh->fib_nh_flags |= nh_flags;
47111de178edSIdo Schimmel }
47121de178edSIdo Schimmel 
4713a1a22c12SDavid Ahern /* called with write lock held for table with rt */
47148d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
47151da177e4SLinus Torvalds {
47164c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
47174c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
47187aef6859SDavid Ahern 	struct net *net = dev_net(dev);
47198ed67789SDaniel Lezcano 
4720f88d8ea6SDavid Ahern 	if (rt == net->ipv6.fib6_null_entry || rt->nh)
472127c6fa73SIdo Schimmel 		return 0;
472227c6fa73SIdo Schimmel 
472327c6fa73SIdo Schimmel 	switch (arg->event) {
472427c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
47251cf844c7SDavid Ahern 		return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
472627c6fa73SIdo Schimmel 	case NETDEV_DOWN:
47271de178edSIdo Schimmel 		if (rt->should_flush)
472827c6fa73SIdo Schimmel 			return -1;
472993c2fb25SDavid Ahern 		if (!rt->fib6_nsiblings)
47301cf844c7SDavid Ahern 			return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
47311de178edSIdo Schimmel 		if (rt6_multipath_uses_dev(rt, dev)) {
47321de178edSIdo Schimmel 			unsigned int count;
47331de178edSIdo Schimmel 
47341de178edSIdo Schimmel 			count = rt6_multipath_dead_count(rt, dev);
473593c2fb25SDavid Ahern 			if (rt->fib6_nsiblings + 1 == count) {
47361de178edSIdo Schimmel 				rt6_multipath_flush(rt);
47371de178edSIdo Schimmel 				return -1;
47381de178edSIdo Schimmel 			}
47391de178edSIdo Schimmel 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
47401de178edSIdo Schimmel 						   RTNH_F_LINKDOWN);
47417aef6859SDavid Ahern 			fib6_update_sernum(net, rt);
4742d7dedee1SIdo Schimmel 			rt6_multipath_rebalance(rt);
47431de178edSIdo Schimmel 		}
47441de178edSIdo Schimmel 		return -2;
474527c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
47461cf844c7SDavid Ahern 		if (rt->fib6_nh->fib_nh_dev != dev ||
474793c2fb25SDavid Ahern 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
474827c6fa73SIdo Schimmel 			break;
47491cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
4750d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
475127c6fa73SIdo Schimmel 		break;
47522b241361SIdo Schimmel 	}
4753c159d30cSDavid S. Miller 
47541da177e4SLinus Torvalds 	return 0;
47551da177e4SLinus Torvalds }
47561da177e4SLinus Torvalds 
475727c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
47581da177e4SLinus Torvalds {
47594c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
47608ed67789SDaniel Lezcano 		.dev = dev,
47616802f3adSIdo Schimmel 		{
47624c981e28SIdo Schimmel 			.event = event,
47636802f3adSIdo Schimmel 		},
47648ed67789SDaniel Lezcano 	};
47657c6bb7d2SDavid Ahern 	struct net *net = dev_net(dev);
47668ed67789SDaniel Lezcano 
47677c6bb7d2SDavid Ahern 	if (net->ipv6.sysctl.skip_notify_on_dev_down)
47687c6bb7d2SDavid Ahern 		fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
47697c6bb7d2SDavid Ahern 	else
47707c6bb7d2SDavid Ahern 		fib6_clean_all(net, fib6_ifdown, &arg);
47714c981e28SIdo Schimmel }
47724c981e28SIdo Schimmel 
47734c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
47744c981e28SIdo Schimmel {
47754c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
47764c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
47774c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
47781da177e4SLinus Torvalds }
47791da177e4SLinus Torvalds 
478095c96174SEric Dumazet struct rt6_mtu_change_arg {
47811da177e4SLinus Torvalds 	struct net_device *dev;
478295c96174SEric Dumazet 	unsigned int mtu;
4783c0b220cfSDavid Ahern 	struct fib6_info *f6i;
47841da177e4SLinus Torvalds };
47851da177e4SLinus Torvalds 
4786cc5c073aSDavid Ahern static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
4787c0b220cfSDavid Ahern {
4788c0b220cfSDavid Ahern 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
4789cc5c073aSDavid Ahern 	struct fib6_info *f6i = arg->f6i;
4790c0b220cfSDavid Ahern 
4791c0b220cfSDavid Ahern 	/* For administrative MTU increase, there is no way to discover
4792c0b220cfSDavid Ahern 	 * IPv6 PMTU increase, so PMTU increase should be updated here.
4793c0b220cfSDavid Ahern 	 * Since RFC 1981 doesn't include administrative MTU increase
4794c0b220cfSDavid Ahern 	 * update PMTU increase is a MUST. (i.e. jumbo frame)
4795c0b220cfSDavid Ahern 	 */
4796c0b220cfSDavid Ahern 	if (nh->fib_nh_dev == arg->dev) {
4797c0b220cfSDavid Ahern 		struct inet6_dev *idev = __in6_dev_get(arg->dev);
4798c0b220cfSDavid Ahern 		u32 mtu = f6i->fib6_pmtu;
4799c0b220cfSDavid Ahern 
4800c0b220cfSDavid Ahern 		if (mtu >= arg->mtu ||
4801c0b220cfSDavid Ahern 		    (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4802c0b220cfSDavid Ahern 			fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4803c0b220cfSDavid Ahern 
4804c0b220cfSDavid Ahern 		spin_lock_bh(&rt6_exception_lock);
4805cc5c073aSDavid Ahern 		rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
4806c0b220cfSDavid Ahern 		spin_unlock_bh(&rt6_exception_lock);
4807c0b220cfSDavid Ahern 	}
4808c0b220cfSDavid Ahern 
4809c0b220cfSDavid Ahern 	return 0;
4810c0b220cfSDavid Ahern }
4811c0b220cfSDavid Ahern 
4812c0b220cfSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
48131da177e4SLinus Torvalds {
48141da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
48151da177e4SLinus Torvalds 	struct inet6_dev *idev;
48161da177e4SLinus Torvalds 
48171da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
48181da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
48191da177e4SLinus Torvalds 	   We still use this lock to block changes
48201da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
48211da177e4SLinus Torvalds 	*/
48221da177e4SLinus Torvalds 
48231da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
482438308473SDavid S. Miller 	if (!idev)
48251da177e4SLinus Torvalds 		return 0;
48261da177e4SLinus Torvalds 
4827c0b220cfSDavid Ahern 	if (fib6_metric_locked(f6i, RTAX_MTU))
48281da177e4SLinus Torvalds 		return 0;
4829c0b220cfSDavid Ahern 
4830c0b220cfSDavid Ahern 	arg->f6i = f6i;
48312d44234bSDavid Ahern 	if (f6i->nh) {
48322d44234bSDavid Ahern 		/* fib6_nh_mtu_change only returns 0, so this is safe */
48332d44234bSDavid Ahern 		return nexthop_for_each_fib6_nh(f6i->nh, fib6_nh_mtu_change,
48342d44234bSDavid Ahern 						arg);
48352d44234bSDavid Ahern 	}
48362d44234bSDavid Ahern 
48371cf844c7SDavid Ahern 	return fib6_nh_mtu_change(f6i->fib6_nh, arg);
48381da177e4SLinus Torvalds }
48391da177e4SLinus Torvalds 
484095c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
48411da177e4SLinus Torvalds {
4842c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
4843c71099acSThomas Graf 		.dev = dev,
4844c71099acSThomas Graf 		.mtu = mtu,
4845c71099acSThomas Graf 	};
48461da177e4SLinus Torvalds 
48470c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
48481da177e4SLinus Torvalds }
48491da177e4SLinus Torvalds 
4850ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
485175425657SDavid Ahern 	[RTA_UNSPEC]		= { .strict_start_type = RTA_DPORT + 1 },
48525176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
4853aa8f8778SEric Dumazet 	[RTA_PREFSRC]		= { .len = sizeof(struct in6_addr) },
485486872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
4855ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
485686872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
485786872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
485851ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
4859c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
486019e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
486119e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
486232bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
4863622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
48643b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
4865aa8f8778SEric Dumazet 	[RTA_TABLE]		= { .type = NLA_U32 },
4866eacb9384SRoopa Prabhu 	[RTA_IP_PROTO]		= { .type = NLA_U8 },
4867eacb9384SRoopa Prabhu 	[RTA_SPORT]		= { .type = NLA_U16 },
4868eacb9384SRoopa Prabhu 	[RTA_DPORT]		= { .type = NLA_U16 },
48695b98324eSDavid Ahern 	[RTA_NH_ID]		= { .type = NLA_U32 },
487086872cb5SThomas Graf };
487186872cb5SThomas Graf 
487286872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4873333c4301SDavid Ahern 			      struct fib6_config *cfg,
4874333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
48751da177e4SLinus Torvalds {
487686872cb5SThomas Graf 	struct rtmsg *rtm;
487786872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
4878c78ba6d6SLubomir Rintel 	unsigned int pref;
487986872cb5SThomas Graf 	int err;
48801da177e4SLinus Torvalds 
48818cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
48828cb08174SJohannes Berg 				     rtm_ipv6_policy, extack);
488386872cb5SThomas Graf 	if (err < 0)
488486872cb5SThomas Graf 		goto errout;
48851da177e4SLinus Torvalds 
488686872cb5SThomas Graf 	err = -EINVAL;
488786872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
488886872cb5SThomas Graf 
488984db8407SMaciej Żenczykowski 	*cfg = (struct fib6_config){
489084db8407SMaciej Żenczykowski 		.fc_table = rtm->rtm_table,
489184db8407SMaciej Żenczykowski 		.fc_dst_len = rtm->rtm_dst_len,
489284db8407SMaciej Żenczykowski 		.fc_src_len = rtm->rtm_src_len,
489384db8407SMaciej Żenczykowski 		.fc_flags = RTF_UP,
489484db8407SMaciej Żenczykowski 		.fc_protocol = rtm->rtm_protocol,
489584db8407SMaciej Żenczykowski 		.fc_type = rtm->rtm_type,
489684db8407SMaciej Żenczykowski 
489784db8407SMaciej Żenczykowski 		.fc_nlinfo.portid = NETLINK_CB(skb).portid,
489884db8407SMaciej Żenczykowski 		.fc_nlinfo.nlh = nlh,
489984db8407SMaciej Żenczykowski 		.fc_nlinfo.nl_net = sock_net(skb->sk),
490084db8407SMaciej Żenczykowski 	};
490186872cb5SThomas Graf 
4902ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
4903ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
4904b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
4905b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
490686872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
490786872cb5SThomas Graf 
4908ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
4909ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
4910ab79ad14SMaciej Żenczykowski 
49111f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
49121f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
49131f56a01fSMartin KaFai Lau 
4914fc1e64e1SDavid Ahern 	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4915fc1e64e1SDavid Ahern 
49165b98324eSDavid Ahern 	if (tb[RTA_NH_ID]) {
49175b98324eSDavid Ahern 		if (tb[RTA_GATEWAY]   || tb[RTA_OIF] ||
49185b98324eSDavid Ahern 		    tb[RTA_MULTIPATH] || tb[RTA_ENCAP]) {
49195b98324eSDavid Ahern 			NL_SET_ERR_MSG(extack,
49205b98324eSDavid Ahern 				       "Nexthop specification and nexthop id are mutually exclusive");
49215b98324eSDavid Ahern 			goto errout;
49225b98324eSDavid Ahern 		}
49235b98324eSDavid Ahern 		cfg->fc_nh_id = nla_get_u32(tb[RTA_NH_ID]);
49245b98324eSDavid Ahern 	}
49255b98324eSDavid Ahern 
492686872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
492767b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
492886872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
49291da177e4SLinus Torvalds 	}
4930e3818541SDavid Ahern 	if (tb[RTA_VIA]) {
4931e3818541SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4932e3818541SDavid Ahern 		goto errout;
4933e3818541SDavid Ahern 	}
493486872cb5SThomas Graf 
493586872cb5SThomas Graf 	if (tb[RTA_DST]) {
493686872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
493786872cb5SThomas Graf 
493886872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
493986872cb5SThomas Graf 			goto errout;
494086872cb5SThomas Graf 
494186872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
49421da177e4SLinus Torvalds 	}
494386872cb5SThomas Graf 
494486872cb5SThomas Graf 	if (tb[RTA_SRC]) {
494586872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
494686872cb5SThomas Graf 
494786872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
494886872cb5SThomas Graf 			goto errout;
494986872cb5SThomas Graf 
495086872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
49511da177e4SLinus Torvalds 	}
495286872cb5SThomas Graf 
4953c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
495467b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4955c3968a85SDaniel Walter 
495686872cb5SThomas Graf 	if (tb[RTA_OIF])
495786872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
495886872cb5SThomas Graf 
495986872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
496086872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
496186872cb5SThomas Graf 
496286872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
496386872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
496486872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
49651da177e4SLinus Torvalds 	}
496686872cb5SThomas Graf 
496786872cb5SThomas Graf 	if (tb[RTA_TABLE])
496886872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
496986872cb5SThomas Graf 
497051ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
497151ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
497251ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
49739ed59592SDavid Ahern 
49749ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4975c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
49769ed59592SDavid Ahern 		if (err < 0)
49779ed59592SDavid Ahern 			goto errout;
497851ebd318SNicolas Dichtel 	}
497951ebd318SNicolas Dichtel 
4980c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
4981c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
4982c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
4983c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
4984c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
4985c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
4986c78ba6d6SLubomir Rintel 	}
4987c78ba6d6SLubomir Rintel 
498819e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
498919e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
499019e42e45SRoopa Prabhu 
49919ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
499219e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
499319e42e45SRoopa Prabhu 
4994c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
49959ed59592SDavid Ahern 		if (err < 0)
49969ed59592SDavid Ahern 			goto errout;
49979ed59592SDavid Ahern 	}
49989ed59592SDavid Ahern 
499932bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
500032bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
500132bc201eSXin Long 
500232bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
500332bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
500432bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
500532bc201eSXin Long 		}
500632bc201eSXin Long 	}
500732bc201eSXin Long 
500886872cb5SThomas Graf 	err = 0;
500986872cb5SThomas Graf errout:
501086872cb5SThomas Graf 	return err;
50111da177e4SLinus Torvalds }
50121da177e4SLinus Torvalds 
50136b9ea5a6SRoopa Prabhu struct rt6_nh {
50148d1c802bSDavid Ahern 	struct fib6_info *fib6_info;
50156b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
50166b9ea5a6SRoopa Prabhu 	struct list_head next;
50176b9ea5a6SRoopa Prabhu };
50186b9ea5a6SRoopa Prabhu 
5019d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net,
5020d4ead6b3SDavid Ahern 				 struct list_head *rt6_nh_list,
50218d1c802bSDavid Ahern 				 struct fib6_info *rt,
50228d1c802bSDavid Ahern 				 struct fib6_config *r_cfg)
50236b9ea5a6SRoopa Prabhu {
50246b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
50256b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
50266b9ea5a6SRoopa Prabhu 
50276b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
50288d1c802bSDavid Ahern 		/* check if fib6_info already exists */
50298d1c802bSDavid Ahern 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
50306b9ea5a6SRoopa Prabhu 			return err;
50316b9ea5a6SRoopa Prabhu 	}
50326b9ea5a6SRoopa Prabhu 
50336b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
50346b9ea5a6SRoopa Prabhu 	if (!nh)
50356b9ea5a6SRoopa Prabhu 		return -ENOMEM;
50368d1c802bSDavid Ahern 	nh->fib6_info = rt;
50376b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
50386b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
50396b9ea5a6SRoopa Prabhu 
50406b9ea5a6SRoopa Prabhu 	return 0;
50416b9ea5a6SRoopa Prabhu }
50426b9ea5a6SRoopa Prabhu 
50438d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt,
50448d1c802bSDavid Ahern 				   struct fib6_info *rt_last,
50453b1137feSDavid Ahern 				   struct nl_info *info,
50463b1137feSDavid Ahern 				   __u16 nlflags)
50473b1137feSDavid Ahern {
50483b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
50493b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
50503b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
50513b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
50523b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
50533b1137feSDavid Ahern 	 */
505493c2fb25SDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
505593c2fb25SDavid Ahern 		rt = list_first_entry(&rt_last->fib6_siblings,
50568d1c802bSDavid Ahern 				      struct fib6_info,
505793c2fb25SDavid Ahern 				      fib6_siblings);
50583b1137feSDavid Ahern 	}
50593b1137feSDavid Ahern 
50603b1137feSDavid Ahern 	if (rt)
50613b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
50623b1137feSDavid Ahern }
50633b1137feSDavid Ahern 
50640ee0f47cSIdo Schimmel static bool ip6_route_mpath_should_notify(const struct fib6_info *rt)
50650ee0f47cSIdo Schimmel {
50660ee0f47cSIdo Schimmel 	bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
50670ee0f47cSIdo Schimmel 	bool should_notify = false;
50680ee0f47cSIdo Schimmel 	struct fib6_info *leaf;
50690ee0f47cSIdo Schimmel 	struct fib6_node *fn;
50700ee0f47cSIdo Schimmel 
50710ee0f47cSIdo Schimmel 	rcu_read_lock();
50720ee0f47cSIdo Schimmel 	fn = rcu_dereference(rt->fib6_node);
50730ee0f47cSIdo Schimmel 	if (!fn)
50740ee0f47cSIdo Schimmel 		goto out;
50750ee0f47cSIdo Schimmel 
50760ee0f47cSIdo Schimmel 	leaf = rcu_dereference(fn->leaf);
50770ee0f47cSIdo Schimmel 	if (!leaf)
50780ee0f47cSIdo Schimmel 		goto out;
50790ee0f47cSIdo Schimmel 
50800ee0f47cSIdo Schimmel 	if (rt == leaf ||
50810ee0f47cSIdo Schimmel 	    (rt_can_ecmp && rt->fib6_metric == leaf->fib6_metric &&
50820ee0f47cSIdo Schimmel 	     rt6_qualify_for_ecmp(leaf)))
50830ee0f47cSIdo Schimmel 		should_notify = true;
50840ee0f47cSIdo Schimmel out:
50850ee0f47cSIdo Schimmel 	rcu_read_unlock();
50860ee0f47cSIdo Schimmel 
50870ee0f47cSIdo Schimmel 	return should_notify;
50880ee0f47cSIdo Schimmel }
50890ee0f47cSIdo Schimmel 
5090333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
5091333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
509251ebd318SNicolas Dichtel {
50938d1c802bSDavid Ahern 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
50943b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
509551ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
509651ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
50978d1c802bSDavid Ahern 	struct fib6_info *rt;
50986b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
50996b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
51003b1137feSDavid Ahern 	__u16 nlflags;
510151ebd318SNicolas Dichtel 	int remaining;
510251ebd318SNicolas Dichtel 	int attrlen;
51036b9ea5a6SRoopa Prabhu 	int err = 1;
51046b9ea5a6SRoopa Prabhu 	int nhn = 0;
51056b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
51066b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
51076b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
510851ebd318SNicolas Dichtel 
51093b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
51103b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
51113b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
51123b1137feSDavid Ahern 
511335f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
511451ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
511551ebd318SNicolas Dichtel 
51166b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
51178d1c802bSDavid Ahern 	 * fib6_info structs per nexthop
51186b9ea5a6SRoopa Prabhu 	 */
511951ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
512051ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
512151ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
512251ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
512351ebd318SNicolas Dichtel 
512451ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
512551ebd318SNicolas Dichtel 		if (attrlen > 0) {
512651ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
512751ebd318SNicolas Dichtel 
512851ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
512951ebd318SNicolas Dichtel 			if (nla) {
513067b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
513151ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
513251ebd318SNicolas Dichtel 			}
513319e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
513419e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
513519e42e45SRoopa Prabhu 			if (nla)
513619e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
513751ebd318SNicolas Dichtel 		}
51386b9ea5a6SRoopa Prabhu 
513968e2ffdeSDavid Ahern 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5140acb54e3cSDavid Ahern 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
51418c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
51428c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
51438c5b83f0SRoopa Prabhu 			rt = NULL;
51446b9ea5a6SRoopa Prabhu 			goto cleanup;
51458c5b83f0SRoopa Prabhu 		}
5146b5d2d75eSDavid Ahern 		if (!rt6_qualify_for_ecmp(rt)) {
5147b5d2d75eSDavid Ahern 			err = -EINVAL;
5148b5d2d75eSDavid Ahern 			NL_SET_ERR_MSG(extack,
5149b5d2d75eSDavid Ahern 				       "Device only routes can not be added for IPv6 using the multipath API.");
5150b5d2d75eSDavid Ahern 			fib6_info_release(rt);
5151b5d2d75eSDavid Ahern 			goto cleanup;
5152b5d2d75eSDavid Ahern 		}
51536b9ea5a6SRoopa Prabhu 
51541cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
5155398958aeSIdo Schimmel 
5156d4ead6b3SDavid Ahern 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
5157d4ead6b3SDavid Ahern 					    rt, &r_cfg);
515851ebd318SNicolas Dichtel 		if (err) {
515993531c67SDavid Ahern 			fib6_info_release(rt);
51606b9ea5a6SRoopa Prabhu 			goto cleanup;
516151ebd318SNicolas Dichtel 		}
51626b9ea5a6SRoopa Prabhu 
51636b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
516451ebd318SNicolas Dichtel 	}
51656b9ea5a6SRoopa Prabhu 
51669eee3b49SIdo Schimmel 	if (list_empty(&rt6_nh_list)) {
51679eee3b49SIdo Schimmel 		NL_SET_ERR_MSG(extack,
51689eee3b49SIdo Schimmel 			       "Invalid nexthop configuration - no valid nexthops");
51699eee3b49SIdo Schimmel 		return -EINVAL;
51709eee3b49SIdo Schimmel 	}
51719eee3b49SIdo Schimmel 
51723b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
51733b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
51743b1137feSDavid Ahern 	 * the full route when done
51753b1137feSDavid Ahern 	 */
51763b1137feSDavid Ahern 	info->skip_notify = 1;
51773b1137feSDavid Ahern 
5178ebee3cadSIdo Schimmel 	/* For add and replace, send one notification with all nexthops. For
5179ebee3cadSIdo Schimmel 	 * append, send one notification with all appended nexthops.
5180ebee3cadSIdo Schimmel 	 */
5181ebee3cadSIdo Schimmel 	info->skip_notify_kernel = 1;
5182ebee3cadSIdo Schimmel 
51836b9ea5a6SRoopa Prabhu 	err_nh = NULL;
51846b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
51858d1c802bSDavid Ahern 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
51868d1c802bSDavid Ahern 		fib6_info_release(nh->fib6_info);
51873b1137feSDavid Ahern 
5188f7225172SDavid Ahern 		if (!err) {
5189f7225172SDavid Ahern 			/* save reference to last route successfully inserted */
5190f7225172SDavid Ahern 			rt_last = nh->fib6_info;
5191f7225172SDavid Ahern 
51926b9ea5a6SRoopa Prabhu 			/* save reference to first route for notification */
5193f7225172SDavid Ahern 			if (!rt_notif)
51948d1c802bSDavid Ahern 				rt_notif = nh->fib6_info;
5195f7225172SDavid Ahern 		}
51966b9ea5a6SRoopa Prabhu 
51978d1c802bSDavid Ahern 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
51988d1c802bSDavid Ahern 		nh->fib6_info = NULL;
51996b9ea5a6SRoopa Prabhu 		if (err) {
52006b9ea5a6SRoopa Prabhu 			if (replace && nhn)
5201a5a82d84SJakub Kicinski 				NL_SET_ERR_MSG_MOD(extack,
5202a5a82d84SJakub Kicinski 						   "multipath route replace failed (check consistency of installed routes)");
52036b9ea5a6SRoopa Prabhu 			err_nh = nh;
52046b9ea5a6SRoopa Prabhu 			goto add_errout;
52056b9ea5a6SRoopa Prabhu 		}
52066b9ea5a6SRoopa Prabhu 
52071a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
520827596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
520927596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
521027596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
521127596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
521227596472SMichal Kubeček 		 * be added to it.
52131a72418bSNicolas Dichtel 		 */
521427596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
521527596472SMichal Kubeček 						     NLM_F_REPLACE);
5216afecdb37SBenjamin Poirier 		cfg->fc_nlinfo.nlh->nlmsg_flags |= NLM_F_CREATE;
52176b9ea5a6SRoopa Prabhu 		nhn++;
52186b9ea5a6SRoopa Prabhu 	}
52196b9ea5a6SRoopa Prabhu 
52200ee0f47cSIdo Schimmel 	/* An in-kernel notification should only be sent in case the new
52210ee0f47cSIdo Schimmel 	 * multipath route is added as the first route in the node, or if
52220ee0f47cSIdo Schimmel 	 * it was appended to it. We pass 'rt_notif' since it is the first
52230ee0f47cSIdo Schimmel 	 * sibling and might allow us to skip some checks in the replace case.
52240ee0f47cSIdo Schimmel 	 */
52250ee0f47cSIdo Schimmel 	if (ip6_route_mpath_should_notify(rt_notif)) {
52260ee0f47cSIdo Schimmel 		enum fib_event_type fib_event;
52270ee0f47cSIdo Schimmel 
52280ee0f47cSIdo Schimmel 		if (rt_notif->fib6_nsiblings != nhn - 1)
52290ee0f47cSIdo Schimmel 			fib_event = FIB_EVENT_ENTRY_APPEND;
52300ee0f47cSIdo Schimmel 		else
5231caafb250SIdo Schimmel 			fib_event = FIB_EVENT_ENTRY_REPLACE;
52320ee0f47cSIdo Schimmel 
52330ee0f47cSIdo Schimmel 		err = call_fib6_multipath_entry_notifiers(info->nl_net,
52340ee0f47cSIdo Schimmel 							  fib_event, rt_notif,
52350ee0f47cSIdo Schimmel 							  nhn - 1, extack);
52360ee0f47cSIdo Schimmel 		if (err) {
52370ee0f47cSIdo Schimmel 			/* Delete all the siblings that were just added */
52380ee0f47cSIdo Schimmel 			err_nh = NULL;
52390ee0f47cSIdo Schimmel 			goto add_errout;
52400ee0f47cSIdo Schimmel 		}
52410ee0f47cSIdo Schimmel 	}
5242ebee3cadSIdo Schimmel 
52433b1137feSDavid Ahern 	/* success ... tell user about new route */
52443b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
52456b9ea5a6SRoopa Prabhu 	goto cleanup;
52466b9ea5a6SRoopa Prabhu 
52476b9ea5a6SRoopa Prabhu add_errout:
52483b1137feSDavid Ahern 	/* send notification for routes that were added so that
52493b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
52503b1137feSDavid Ahern 	 * coherent
52513b1137feSDavid Ahern 	 */
52523b1137feSDavid Ahern 	if (rt_notif)
52533b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
52543b1137feSDavid Ahern 
52556b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
52566b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
52576b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
52586b9ea5a6SRoopa Prabhu 			break;
5259333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
52606b9ea5a6SRoopa Prabhu 	}
52616b9ea5a6SRoopa Prabhu 
52626b9ea5a6SRoopa Prabhu cleanup:
52636b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
52648d1c802bSDavid Ahern 		if (nh->fib6_info)
52658d1c802bSDavid Ahern 			fib6_info_release(nh->fib6_info);
52666b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
52676b9ea5a6SRoopa Prabhu 		kfree(nh);
52686b9ea5a6SRoopa Prabhu 	}
52696b9ea5a6SRoopa Prabhu 
52706b9ea5a6SRoopa Prabhu 	return err;
52716b9ea5a6SRoopa Prabhu }
52726b9ea5a6SRoopa Prabhu 
5273333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
5274333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
52756b9ea5a6SRoopa Prabhu {
52766b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
52776b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
52786b9ea5a6SRoopa Prabhu 	int remaining;
52796b9ea5a6SRoopa Prabhu 	int attrlen;
52806b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
52816b9ea5a6SRoopa Prabhu 
52826b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
52836b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
52846b9ea5a6SRoopa Prabhu 
52856b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
52866b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
52876b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
52886b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
52896b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
52906b9ea5a6SRoopa Prabhu 
52916b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
52926b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
52936b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
52946b9ea5a6SRoopa Prabhu 
52956b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
52966b9ea5a6SRoopa Prabhu 			if (nla) {
52976b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
52986b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
52996b9ea5a6SRoopa Prabhu 			}
53006b9ea5a6SRoopa Prabhu 		}
5301333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
53026b9ea5a6SRoopa Prabhu 		if (err)
53036b9ea5a6SRoopa Prabhu 			last_err = err;
53046b9ea5a6SRoopa Prabhu 
530551ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
530651ebd318SNicolas Dichtel 	}
530751ebd318SNicolas Dichtel 
530851ebd318SNicolas Dichtel 	return last_err;
530951ebd318SNicolas Dichtel }
531051ebd318SNicolas Dichtel 
5311c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5312c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
53131da177e4SLinus Torvalds {
531486872cb5SThomas Graf 	struct fib6_config cfg;
531586872cb5SThomas Graf 	int err;
53161da177e4SLinus Torvalds 
5317333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
531886872cb5SThomas Graf 	if (err < 0)
531986872cb5SThomas Graf 		return err;
532086872cb5SThomas Graf 
53215b98324eSDavid Ahern 	if (cfg.fc_nh_id &&
53225b98324eSDavid Ahern 	    !nexthop_find_by_id(sock_net(skb->sk), cfg.fc_nh_id)) {
53235b98324eSDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
53245b98324eSDavid Ahern 		return -EINVAL;
53255b98324eSDavid Ahern 	}
53265b98324eSDavid Ahern 
532751ebd318SNicolas Dichtel 	if (cfg.fc_mp)
5328333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
53290ae81335SDavid Ahern 	else {
53300ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
5331333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
53321da177e4SLinus Torvalds 	}
53330ae81335SDavid Ahern }
53341da177e4SLinus Torvalds 
5335c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
5336c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
53371da177e4SLinus Torvalds {
533886872cb5SThomas Graf 	struct fib6_config cfg;
533986872cb5SThomas Graf 	int err;
53401da177e4SLinus Torvalds 
5341333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
534286872cb5SThomas Graf 	if (err < 0)
534386872cb5SThomas Graf 		return err;
534486872cb5SThomas Graf 
534567f69513SDavid Ahern 	if (cfg.fc_metric == 0)
534667f69513SDavid Ahern 		cfg.fc_metric = IP6_RT_PRIO_USER;
534767f69513SDavid Ahern 
534851ebd318SNicolas Dichtel 	if (cfg.fc_mp)
5349333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
535051ebd318SNicolas Dichtel 	else
5351acb54e3cSDavid Ahern 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
53521da177e4SLinus Torvalds }
53531da177e4SLinus Torvalds 
5354a1b7a1f0SDavid Ahern /* add the overhead of this fib6_nh to nexthop_len */
5355a1b7a1f0SDavid Ahern static int rt6_nh_nlmsg_size(struct fib6_nh *nh, void *arg)
5356339bf98fSThomas Graf {
5357a1b7a1f0SDavid Ahern 	int *nexthop_len = arg;
5358beb1afacSDavid Ahern 
5359a1b7a1f0SDavid Ahern 	*nexthop_len += nla_total_size(0)	 /* RTA_MULTIPATH */
5360a1b7a1f0SDavid Ahern 		     + NLA_ALIGN(sizeof(struct rtnexthop))
5361a1b7a1f0SDavid Ahern 		     + nla_total_size(16); /* RTA_GATEWAY */
5362f88d8ea6SDavid Ahern 
5363a1b7a1f0SDavid Ahern 	if (nh->fib_nh_lws) {
5364a1b7a1f0SDavid Ahern 		/* RTA_ENCAP_TYPE */
5365a1b7a1f0SDavid Ahern 		*nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5366a1b7a1f0SDavid Ahern 		/* RTA_ENCAP */
5367a1b7a1f0SDavid Ahern 		*nexthop_len += nla_total_size(2);
5368a1b7a1f0SDavid Ahern 	}
5369a1b7a1f0SDavid Ahern 
5370a1b7a1f0SDavid Ahern 	return 0;
5371a1b7a1f0SDavid Ahern }
5372a1b7a1f0SDavid Ahern 
5373a1b7a1f0SDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *f6i)
5374a1b7a1f0SDavid Ahern {
5375a1b7a1f0SDavid Ahern 	int nexthop_len;
5376a1b7a1f0SDavid Ahern 
5377a1b7a1f0SDavid Ahern 	if (f6i->nh) {
5378a1b7a1f0SDavid Ahern 		nexthop_len = nla_total_size(4); /* RTA_NH_ID */
5379a1b7a1f0SDavid Ahern 		nexthop_for_each_fib6_nh(f6i->nh, rt6_nh_nlmsg_size,
5380a1b7a1f0SDavid Ahern 					 &nexthop_len);
5381a1b7a1f0SDavid Ahern 	} else {
5382a1b7a1f0SDavid Ahern 		struct fib6_nh *nh = f6i->fib6_nh;
5383a1b7a1f0SDavid Ahern 
5384a1b7a1f0SDavid Ahern 		nexthop_len = 0;
5385a1b7a1f0SDavid Ahern 		if (f6i->fib6_nsiblings) {
5386beb1afacSDavid Ahern 			nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
5387beb1afacSDavid Ahern 				    + NLA_ALIGN(sizeof(struct rtnexthop))
5388beb1afacSDavid Ahern 				    + nla_total_size(16) /* RTA_GATEWAY */
5389a1b7a1f0SDavid Ahern 				    + lwtunnel_get_encap_size(nh->fib_nh_lws);
5390beb1afacSDavid Ahern 
5391a1b7a1f0SDavid Ahern 			nexthop_len *= f6i->fib6_nsiblings;
5392a1b7a1f0SDavid Ahern 		}
5393a1b7a1f0SDavid Ahern 		nexthop_len += lwtunnel_get_encap_size(nh->fib_nh_lws);
5394beb1afacSDavid Ahern 	}
5395beb1afacSDavid Ahern 
5396339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
5397339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
5398339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
5399339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
5400339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
5401339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
5402339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
5403339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
5404339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
54056a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
5406ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
5407c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
540819e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
5409beb1afacSDavid Ahern 	       + nexthop_len;
5410beb1afacSDavid Ahern }
5411beb1afacSDavid Ahern 
5412f88d8ea6SDavid Ahern static int rt6_fill_node_nexthop(struct sk_buff *skb, struct nexthop *nh,
5413f88d8ea6SDavid Ahern 				 unsigned char *flags)
5414f88d8ea6SDavid Ahern {
5415f88d8ea6SDavid Ahern 	if (nexthop_is_multipath(nh)) {
5416f88d8ea6SDavid Ahern 		struct nlattr *mp;
5417f88d8ea6SDavid Ahern 
54184255ff05SDavid Ahern 		mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5419f88d8ea6SDavid Ahern 		if (!mp)
5420f88d8ea6SDavid Ahern 			goto nla_put_failure;
5421f88d8ea6SDavid Ahern 
54227bdf4de1SDonald Sharp 		if (nexthop_mpath_fill_node(skb, nh, AF_INET6))
5423f88d8ea6SDavid Ahern 			goto nla_put_failure;
5424f88d8ea6SDavid Ahern 
5425f88d8ea6SDavid Ahern 		nla_nest_end(skb, mp);
5426f88d8ea6SDavid Ahern 	} else {
5427f88d8ea6SDavid Ahern 		struct fib6_nh *fib6_nh;
5428f88d8ea6SDavid Ahern 
5429f88d8ea6SDavid Ahern 		fib6_nh = nexthop_fib6_nh(nh);
54307bdf4de1SDonald Sharp 		if (fib_nexthop_info(skb, &fib6_nh->nh_common, AF_INET6,
5431f88d8ea6SDavid Ahern 				     flags, false) < 0)
5432f88d8ea6SDavid Ahern 			goto nla_put_failure;
5433f88d8ea6SDavid Ahern 	}
5434f88d8ea6SDavid Ahern 
5435f88d8ea6SDavid Ahern 	return 0;
5436f88d8ea6SDavid Ahern 
5437f88d8ea6SDavid Ahern nla_put_failure:
5438f88d8ea6SDavid Ahern 	return -EMSGSIZE;
5439f88d8ea6SDavid Ahern }
5440f88d8ea6SDavid Ahern 
5441d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
54428d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
5443d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
544415e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
5445f8cfe2ceSDavid Ahern 			 unsigned int flags)
54461da177e4SLinus Torvalds {
544722d0bd82SXin Long 	struct rt6_info *rt6 = (struct rt6_info *)dst;
544822d0bd82SXin Long 	struct rt6key *rt6_dst, *rt6_src;
544922d0bd82SXin Long 	u32 *pmetrics, table, rt6_flags;
5450f88d8ea6SDavid Ahern 	unsigned char nh_flags = 0;
54511da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
545222d0bd82SXin Long 	struct rtmsg *rtm;
5453d4ead6b3SDavid Ahern 	long expires = 0;
54541da177e4SLinus Torvalds 
545515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
545638308473SDavid S. Miller 	if (!nlh)
545726932566SPatrick McHardy 		return -EMSGSIZE;
54582d7202bfSThomas Graf 
545922d0bd82SXin Long 	if (rt6) {
546022d0bd82SXin Long 		rt6_dst = &rt6->rt6i_dst;
546122d0bd82SXin Long 		rt6_src = &rt6->rt6i_src;
546222d0bd82SXin Long 		rt6_flags = rt6->rt6i_flags;
546322d0bd82SXin Long 	} else {
546422d0bd82SXin Long 		rt6_dst = &rt->fib6_dst;
546522d0bd82SXin Long 		rt6_src = &rt->fib6_src;
546622d0bd82SXin Long 		rt6_flags = rt->fib6_flags;
546722d0bd82SXin Long 	}
546822d0bd82SXin Long 
54692d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
54701da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
547122d0bd82SXin Long 	rtm->rtm_dst_len = rt6_dst->plen;
547222d0bd82SXin Long 	rtm->rtm_src_len = rt6_src->plen;
54731da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
547493c2fb25SDavid Ahern 	if (rt->fib6_table)
547593c2fb25SDavid Ahern 		table = rt->fib6_table->tb6_id;
5476c71099acSThomas Graf 	else
54779e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
547897f0082aSKalash Nainwal 	rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
5479c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
5480c78679e8SDavid S. Miller 		goto nla_put_failure;
5481e8478e80SDavid Ahern 
5482e8478e80SDavid Ahern 	rtm->rtm_type = rt->fib6_type;
54831da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
54841da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
548593c2fb25SDavid Ahern 	rtm->rtm_protocol = rt->fib6_protocol;
54861da177e4SLinus Torvalds 
548722d0bd82SXin Long 	if (rt6_flags & RTF_CACHE)
54881da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
54891da177e4SLinus Torvalds 
5490d4ead6b3SDavid Ahern 	if (dest) {
5491d4ead6b3SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, dest))
5492c78679e8SDavid S. Miller 			goto nla_put_failure;
54931da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
54941da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
549522d0bd82SXin Long 		if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
5496c78679e8SDavid S. Miller 			goto nla_put_failure;
54971da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
54981da177e4SLinus Torvalds 	if (src) {
5499930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
5500c78679e8SDavid S. Miller 			goto nla_put_failure;
55011da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
5502c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
550322d0bd82SXin Long 		   nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
5504c78679e8SDavid S. Miller 		goto nla_put_failure;
55051da177e4SLinus Torvalds #endif
55067bc570c8SYOSHIFUJI Hideaki 	if (iif) {
55077bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
550822d0bd82SXin Long 		if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
5509fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
55102cf75070SNikolay Aleksandrov 
55117bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
55127bc570c8SYOSHIFUJI Hideaki 				return 0;
5513fd61c6baSDavid Ahern 			if (err < 0)
55147bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
55157bc570c8SYOSHIFUJI Hideaki 		} else
55167bc570c8SYOSHIFUJI Hideaki #endif
5517c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
5518c78679e8SDavid S. Miller 				goto nla_put_failure;
5519d4ead6b3SDavid Ahern 	} else if (dest) {
55201da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
5521d4ead6b3SDavid Ahern 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
5522930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5523c78679e8SDavid S. Miller 			goto nla_put_failure;
5524c3968a85SDaniel Walter 	}
5525c3968a85SDaniel Walter 
552693c2fb25SDavid Ahern 	if (rt->fib6_prefsrc.plen) {
5527c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
552893c2fb25SDavid Ahern 		saddr_buf = rt->fib6_prefsrc.addr;
5529930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
5530c78679e8SDavid S. Miller 			goto nla_put_failure;
55311da177e4SLinus Torvalds 	}
55322d7202bfSThomas Graf 
5533d4ead6b3SDavid Ahern 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
5534d4ead6b3SDavid Ahern 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
55352d7202bfSThomas Graf 		goto nla_put_failure;
55362d7202bfSThomas Graf 
553793c2fb25SDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
5538beb1afacSDavid Ahern 		goto nla_put_failure;
5539beb1afacSDavid Ahern 
5540beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
5541beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
5542beb1afacSDavid Ahern 	 */
554322d0bd82SXin Long 	if (rt6) {
554422d0bd82SXin Long 		if (rt6_flags & RTF_GATEWAY &&
554522d0bd82SXin Long 		    nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
554622d0bd82SXin Long 			goto nla_put_failure;
554722d0bd82SXin Long 
554822d0bd82SXin Long 		if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
554922d0bd82SXin Long 			goto nla_put_failure;
555022d0bd82SXin Long 	} else if (rt->fib6_nsiblings) {
55518d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
5552beb1afacSDavid Ahern 		struct nlattr *mp;
5553beb1afacSDavid Ahern 
5554ae0be8deSMichal Kubecek 		mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
5555beb1afacSDavid Ahern 		if (!mp)
5556beb1afacSDavid Ahern 			goto nla_put_failure;
5557beb1afacSDavid Ahern 
55581cf844c7SDavid Ahern 		if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
55597bdf4de1SDonald Sharp 				    rt->fib6_nh->fib_nh_weight, AF_INET6) < 0)
5560beb1afacSDavid Ahern 			goto nla_put_failure;
5561beb1afacSDavid Ahern 
5562beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
556393c2fb25SDavid Ahern 					 &rt->fib6_siblings, fib6_siblings) {
55641cf844c7SDavid Ahern 			if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
55657bdf4de1SDonald Sharp 					    sibling->fib6_nh->fib_nh_weight,
55667bdf4de1SDonald Sharp 					    AF_INET6) < 0)
556794f826b8SEric Dumazet 				goto nla_put_failure;
556894f826b8SEric Dumazet 		}
55692d7202bfSThomas Graf 
5570beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
5571f88d8ea6SDavid Ahern 	} else if (rt->nh) {
5572f88d8ea6SDavid Ahern 		if (nla_put_u32(skb, RTA_NH_ID, rt->nh->id))
5573f88d8ea6SDavid Ahern 			goto nla_put_failure;
5574ecc5663cSDavid Ahern 
5575f88d8ea6SDavid Ahern 		if (nexthop_is_blackhole(rt->nh))
5576f88d8ea6SDavid Ahern 			rtm->rtm_type = RTN_BLACKHOLE;
5577f88d8ea6SDavid Ahern 
55784f80116dSRoopa Prabhu 		if (net->ipv4.sysctl_nexthop_compat_mode &&
55794f80116dSRoopa Prabhu 		    rt6_fill_node_nexthop(skb, rt->nh, &nh_flags) < 0)
5580f88d8ea6SDavid Ahern 			goto nla_put_failure;
5581f88d8ea6SDavid Ahern 
5582f88d8ea6SDavid Ahern 		rtm->rtm_flags |= nh_flags;
5583f88d8ea6SDavid Ahern 	} else {
55847bdf4de1SDonald Sharp 		if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common, AF_INET6,
5585ecc5663cSDavid Ahern 				     &nh_flags, false) < 0)
5586c78679e8SDavid S. Miller 			goto nla_put_failure;
5587ecc5663cSDavid Ahern 
5588ecc5663cSDavid Ahern 		rtm->rtm_flags |= nh_flags;
5589beb1afacSDavid Ahern 	}
55908253947eSLi Wei 
559122d0bd82SXin Long 	if (rt6_flags & RTF_EXPIRES) {
559214895687SDavid Ahern 		expires = dst ? dst->expires : rt->expires;
559314895687SDavid Ahern 		expires -= jiffies;
559414895687SDavid Ahern 	}
559569cdf8f9SYOSHIFUJI Hideaki 
5596bb3c4ab9SIdo Schimmel 	if (!dst) {
5597bb3c4ab9SIdo Schimmel 		if (rt->offload)
5598bb3c4ab9SIdo Schimmel 			rtm->rtm_flags |= RTM_F_OFFLOAD;
5599bb3c4ab9SIdo Schimmel 		if (rt->trap)
5600bb3c4ab9SIdo Schimmel 			rtm->rtm_flags |= RTM_F_TRAP;
5601bb3c4ab9SIdo Schimmel 	}
5602bb3c4ab9SIdo Schimmel 
5603d4ead6b3SDavid Ahern 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
5604e3703b3dSThomas Graf 		goto nla_put_failure;
56051da177e4SLinus Torvalds 
560622d0bd82SXin Long 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
5607c78ba6d6SLubomir Rintel 		goto nla_put_failure;
5608c78ba6d6SLubomir Rintel 
560919e42e45SRoopa Prabhu 
5610053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
5611053c095aSJohannes Berg 	return 0;
56122d7202bfSThomas Graf 
56132d7202bfSThomas Graf nla_put_failure:
561426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
561526932566SPatrick McHardy 	return -EMSGSIZE;
56161da177e4SLinus Torvalds }
56171da177e4SLinus Torvalds 
56182c170e07SDavid Ahern static int fib6_info_nh_uses_dev(struct fib6_nh *nh, void *arg)
56192c170e07SDavid Ahern {
56202c170e07SDavid Ahern 	const struct net_device *dev = arg;
56212c170e07SDavid Ahern 
56222c170e07SDavid Ahern 	if (nh->fib_nh_dev == dev)
56232c170e07SDavid Ahern 		return 1;
56242c170e07SDavid Ahern 
56252c170e07SDavid Ahern 	return 0;
56262c170e07SDavid Ahern }
56272c170e07SDavid Ahern 
562813e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i,
562913e38901SDavid Ahern 			       const struct net_device *dev)
563013e38901SDavid Ahern {
56312c170e07SDavid Ahern 	if (f6i->nh) {
56322c170e07SDavid Ahern 		struct net_device *_dev = (struct net_device *)dev;
56332c170e07SDavid Ahern 
56342c170e07SDavid Ahern 		return !!nexthop_for_each_fib6_nh(f6i->nh,
56352c170e07SDavid Ahern 						  fib6_info_nh_uses_dev,
56362c170e07SDavid Ahern 						  _dev);
56372c170e07SDavid Ahern 	}
56382c170e07SDavid Ahern 
56391cf844c7SDavid Ahern 	if (f6i->fib6_nh->fib_nh_dev == dev)
564013e38901SDavid Ahern 		return true;
564113e38901SDavid Ahern 
564213e38901SDavid Ahern 	if (f6i->fib6_nsiblings) {
564313e38901SDavid Ahern 		struct fib6_info *sibling, *next_sibling;
564413e38901SDavid Ahern 
564513e38901SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
564613e38901SDavid Ahern 					 &f6i->fib6_siblings, fib6_siblings) {
56471cf844c7SDavid Ahern 			if (sibling->fib6_nh->fib_nh_dev == dev)
564813e38901SDavid Ahern 				return true;
564913e38901SDavid Ahern 		}
565013e38901SDavid Ahern 	}
565113e38901SDavid Ahern 
565213e38901SDavid Ahern 	return false;
565313e38901SDavid Ahern }
565413e38901SDavid Ahern 
56551e47b483SStefano Brivio struct fib6_nh_exception_dump_walker {
56561e47b483SStefano Brivio 	struct rt6_rtnl_dump_arg *dump;
56571e47b483SStefano Brivio 	struct fib6_info *rt;
56581e47b483SStefano Brivio 	unsigned int flags;
56591e47b483SStefano Brivio 	unsigned int skip;
56601e47b483SStefano Brivio 	unsigned int count;
56611e47b483SStefano Brivio };
56621e47b483SStefano Brivio 
56631e47b483SStefano Brivio static int rt6_nh_dump_exceptions(struct fib6_nh *nh, void *arg)
56641e47b483SStefano Brivio {
56651e47b483SStefano Brivio 	struct fib6_nh_exception_dump_walker *w = arg;
56661e47b483SStefano Brivio 	struct rt6_rtnl_dump_arg *dump = w->dump;
56671e47b483SStefano Brivio 	struct rt6_exception_bucket *bucket;
56681e47b483SStefano Brivio 	struct rt6_exception *rt6_ex;
56691e47b483SStefano Brivio 	int i, err;
56701e47b483SStefano Brivio 
56711e47b483SStefano Brivio 	bucket = fib6_nh_get_excptn_bucket(nh, NULL);
56721e47b483SStefano Brivio 	if (!bucket)
56731e47b483SStefano Brivio 		return 0;
56741e47b483SStefano Brivio 
56751e47b483SStefano Brivio 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
56761e47b483SStefano Brivio 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
56771e47b483SStefano Brivio 			if (w->skip) {
56781e47b483SStefano Brivio 				w->skip--;
56791e47b483SStefano Brivio 				continue;
56801e47b483SStefano Brivio 			}
56811e47b483SStefano Brivio 
56821e47b483SStefano Brivio 			/* Expiration of entries doesn't bump sernum, insertion
56831e47b483SStefano Brivio 			 * does. Removal is triggered by insertion, so we can
56841e47b483SStefano Brivio 			 * rely on the fact that if entries change between two
56851e47b483SStefano Brivio 			 * partial dumps, this node is scanned again completely,
56861e47b483SStefano Brivio 			 * see rt6_insert_exception() and fib6_dump_table().
56871e47b483SStefano Brivio 			 *
56881e47b483SStefano Brivio 			 * Count expired entries we go through as handled
56891e47b483SStefano Brivio 			 * entries that we'll skip next time, in case of partial
56901e47b483SStefano Brivio 			 * node dump. Otherwise, if entries expire meanwhile,
56911e47b483SStefano Brivio 			 * we'll skip the wrong amount.
56921e47b483SStefano Brivio 			 */
56931e47b483SStefano Brivio 			if (rt6_check_expired(rt6_ex->rt6i)) {
56941e47b483SStefano Brivio 				w->count++;
56951e47b483SStefano Brivio 				continue;
56961e47b483SStefano Brivio 			}
56971e47b483SStefano Brivio 
56981e47b483SStefano Brivio 			err = rt6_fill_node(dump->net, dump->skb, w->rt,
56991e47b483SStefano Brivio 					    &rt6_ex->rt6i->dst, NULL, NULL, 0,
57001e47b483SStefano Brivio 					    RTM_NEWROUTE,
57011e47b483SStefano Brivio 					    NETLINK_CB(dump->cb->skb).portid,
57021e47b483SStefano Brivio 					    dump->cb->nlh->nlmsg_seq, w->flags);
57031e47b483SStefano Brivio 			if (err)
57041e47b483SStefano Brivio 				return err;
57051e47b483SStefano Brivio 
57061e47b483SStefano Brivio 			w->count++;
57071e47b483SStefano Brivio 		}
57081e47b483SStefano Brivio 		bucket++;
57091e47b483SStefano Brivio 	}
57101e47b483SStefano Brivio 
57111e47b483SStefano Brivio 	return 0;
57121e47b483SStefano Brivio }
57131e47b483SStefano Brivio 
5714bf9a8a06SStefano Brivio /* Return -1 if done with node, number of handled routes on partial dump */
57151e47b483SStefano Brivio int rt6_dump_route(struct fib6_info *rt, void *p_arg, unsigned int skip)
57161da177e4SLinus Torvalds {
57171da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
571813e38901SDavid Ahern 	struct fib_dump_filter *filter = &arg->filter;
571913e38901SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
57201f17e2f2SDavid Ahern 	struct net *net = arg->net;
57211e47b483SStefano Brivio 	int count = 0;
57221f17e2f2SDavid Ahern 
5723421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
5724bf9a8a06SStefano Brivio 		return -1;
57251da177e4SLinus Torvalds 
572613e38901SDavid Ahern 	if ((filter->flags & RTM_F_PREFIX) &&
572793c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_PREFIX_RT)) {
5728f8cfe2ceSDavid Ahern 		/* success since this is not a prefix route */
5729bf9a8a06SStefano Brivio 		return -1;
5730f8cfe2ceSDavid Ahern 	}
57311e47b483SStefano Brivio 	if (filter->filter_set &&
57321e47b483SStefano Brivio 	    ((filter->rt_type  && rt->fib6_type != filter->rt_type) ||
573313e38901SDavid Ahern 	     (filter->dev      && !fib6_info_uses_dev(rt, filter->dev)) ||
57341e47b483SStefano Brivio 	     (filter->protocol && rt->fib6_protocol != filter->protocol))) {
5735bf9a8a06SStefano Brivio 		return -1;
573613e38901SDavid Ahern 	}
57371e47b483SStefano Brivio 
57381e47b483SStefano Brivio 	if (filter->filter_set ||
57391e47b483SStefano Brivio 	    !filter->dump_routes || !filter->dump_exceptions) {
574013e38901SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
5741f8cfe2ceSDavid Ahern 	}
57421da177e4SLinus Torvalds 
57431e47b483SStefano Brivio 	if (filter->dump_routes) {
57441e47b483SStefano Brivio 		if (skip) {
57451e47b483SStefano Brivio 			skip--;
57461e47b483SStefano Brivio 		} else {
57471e47b483SStefano Brivio 			if (rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL,
57481e47b483SStefano Brivio 					  0, RTM_NEWROUTE,
5749bf9a8a06SStefano Brivio 					  NETLINK_CB(arg->cb->skb).portid,
57501e47b483SStefano Brivio 					  arg->cb->nlh->nlmsg_seq, flags)) {
5751bf9a8a06SStefano Brivio 				return 0;
57521e47b483SStefano Brivio 			}
57531e47b483SStefano Brivio 			count++;
57541e47b483SStefano Brivio 		}
57551e47b483SStefano Brivio 	}
57561e47b483SStefano Brivio 
57571e47b483SStefano Brivio 	if (filter->dump_exceptions) {
57581e47b483SStefano Brivio 		struct fib6_nh_exception_dump_walker w = { .dump = arg,
57591e47b483SStefano Brivio 							   .rt = rt,
57601e47b483SStefano Brivio 							   .flags = flags,
57611e47b483SStefano Brivio 							   .skip = skip,
57621e47b483SStefano Brivio 							   .count = 0 };
57631e47b483SStefano Brivio 		int err;
57641e47b483SStefano Brivio 
57653b525691SEric Dumazet 		rcu_read_lock();
57661e47b483SStefano Brivio 		if (rt->nh) {
57671e47b483SStefano Brivio 			err = nexthop_for_each_fib6_nh(rt->nh,
57681e47b483SStefano Brivio 						       rt6_nh_dump_exceptions,
57691e47b483SStefano Brivio 						       &w);
57701e47b483SStefano Brivio 		} else {
57711e47b483SStefano Brivio 			err = rt6_nh_dump_exceptions(rt->fib6_nh, &w);
57721e47b483SStefano Brivio 		}
57733b525691SEric Dumazet 		rcu_read_unlock();
57741e47b483SStefano Brivio 
57751e47b483SStefano Brivio 		if (err)
57761e47b483SStefano Brivio 			return count += w.count;
57771e47b483SStefano Brivio 	}
5778bf9a8a06SStefano Brivio 
5779bf9a8a06SStefano Brivio 	return -1;
57801da177e4SLinus Torvalds }
57811da177e4SLinus Torvalds 
57820eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
57830eff0a27SJakub Kicinski 					const struct nlmsghdr *nlh,
57840eff0a27SJakub Kicinski 					struct nlattr **tb,
57850eff0a27SJakub Kicinski 					struct netlink_ext_ack *extack)
57860eff0a27SJakub Kicinski {
57870eff0a27SJakub Kicinski 	struct rtmsg *rtm;
57880eff0a27SJakub Kicinski 	int i, err;
57890eff0a27SJakub Kicinski 
57900eff0a27SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
57910eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
57920eff0a27SJakub Kicinski 				   "Invalid header for get route request");
57930eff0a27SJakub Kicinski 		return -EINVAL;
57940eff0a27SJakub Kicinski 	}
57950eff0a27SJakub Kicinski 
57960eff0a27SJakub Kicinski 	if (!netlink_strict_get_check(skb))
57978cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
57980eff0a27SJakub Kicinski 					      rtm_ipv6_policy, extack);
57990eff0a27SJakub Kicinski 
58000eff0a27SJakub Kicinski 	rtm = nlmsg_data(nlh);
58010eff0a27SJakub Kicinski 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
58020eff0a27SJakub Kicinski 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
58030eff0a27SJakub Kicinski 	    rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
58040eff0a27SJakub Kicinski 	    rtm->rtm_type) {
58050eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
58060eff0a27SJakub Kicinski 		return -EINVAL;
58070eff0a27SJakub Kicinski 	}
58080eff0a27SJakub Kicinski 	if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
58090eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
58100eff0a27SJakub Kicinski 				   "Invalid flags for get route request");
58110eff0a27SJakub Kicinski 		return -EINVAL;
58120eff0a27SJakub Kicinski 	}
58130eff0a27SJakub Kicinski 
58148cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
58150eff0a27SJakub Kicinski 					    rtm_ipv6_policy, extack);
58160eff0a27SJakub Kicinski 	if (err)
58170eff0a27SJakub Kicinski 		return err;
58180eff0a27SJakub Kicinski 
58190eff0a27SJakub Kicinski 	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
58200eff0a27SJakub Kicinski 	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
58210eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
58220eff0a27SJakub Kicinski 		return -EINVAL;
58230eff0a27SJakub Kicinski 	}
58240eff0a27SJakub Kicinski 
58250eff0a27SJakub Kicinski 	for (i = 0; i <= RTA_MAX; i++) {
58260eff0a27SJakub Kicinski 		if (!tb[i])
58270eff0a27SJakub Kicinski 			continue;
58280eff0a27SJakub Kicinski 
58290eff0a27SJakub Kicinski 		switch (i) {
58300eff0a27SJakub Kicinski 		case RTA_SRC:
58310eff0a27SJakub Kicinski 		case RTA_DST:
58320eff0a27SJakub Kicinski 		case RTA_IIF:
58330eff0a27SJakub Kicinski 		case RTA_OIF:
58340eff0a27SJakub Kicinski 		case RTA_MARK:
58350eff0a27SJakub Kicinski 		case RTA_UID:
58360eff0a27SJakub Kicinski 		case RTA_SPORT:
58370eff0a27SJakub Kicinski 		case RTA_DPORT:
58380eff0a27SJakub Kicinski 		case RTA_IP_PROTO:
58390eff0a27SJakub Kicinski 			break;
58400eff0a27SJakub Kicinski 		default:
58410eff0a27SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
58420eff0a27SJakub Kicinski 			return -EINVAL;
58430eff0a27SJakub Kicinski 		}
58440eff0a27SJakub Kicinski 	}
58450eff0a27SJakub Kicinski 
58460eff0a27SJakub Kicinski 	return 0;
58470eff0a27SJakub Kicinski }
58480eff0a27SJakub Kicinski 
5849c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5850c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
58511da177e4SLinus Torvalds {
58523b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
5853ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
585418c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
5855a68886a6SDavid Ahern 	struct fib6_info *from;
585618c3a61cSRoopa Prabhu 	struct dst_entry *dst;
58571da177e4SLinus Torvalds 	struct rt6_info *rt;
5858ab364a6fSThomas Graf 	struct sk_buff *skb;
5859ab364a6fSThomas Graf 	struct rtmsg *rtm;
5860744486d4SMaciej Żenczykowski 	struct flowi6 fl6 = {};
586118c3a61cSRoopa Prabhu 	bool fibmatch;
5862ab364a6fSThomas Graf 
58630eff0a27SJakub Kicinski 	err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
5864ab364a6fSThomas Graf 	if (err < 0)
5865ab364a6fSThomas Graf 		goto errout;
5866ab364a6fSThomas Graf 
5867ab364a6fSThomas Graf 	err = -EINVAL;
586838b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
586938b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
587018c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
5871ab364a6fSThomas Graf 
5872ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
5873ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5874ab364a6fSThomas Graf 			goto errout;
5875ab364a6fSThomas Graf 
58764e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
5877ab364a6fSThomas Graf 	}
5878ab364a6fSThomas Graf 
5879ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
5880ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5881ab364a6fSThomas Graf 			goto errout;
5882ab364a6fSThomas Graf 
58834e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
5884ab364a6fSThomas Graf 	}
5885ab364a6fSThomas Graf 
5886ab364a6fSThomas Graf 	if (tb[RTA_IIF])
5887ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
5888ab364a6fSThomas Graf 
5889ab364a6fSThomas Graf 	if (tb[RTA_OIF])
589072331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
5891ab364a6fSThomas Graf 
58922e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
58932e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
58942e47b291SLorenzo Colitti 
5895622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
5896622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
5897622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
5898622ec2c9SLorenzo Colitti 	else
5899622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5900622ec2c9SLorenzo Colitti 
5901eacb9384SRoopa Prabhu 	if (tb[RTA_SPORT])
5902eacb9384SRoopa Prabhu 		fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5903eacb9384SRoopa Prabhu 
5904eacb9384SRoopa Prabhu 	if (tb[RTA_DPORT])
5905eacb9384SRoopa Prabhu 		fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5906eacb9384SRoopa Prabhu 
5907eacb9384SRoopa Prabhu 	if (tb[RTA_IP_PROTO]) {
5908eacb9384SRoopa Prabhu 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
59095e1a99eaSHangbin Liu 						  &fl6.flowi6_proto, AF_INET6,
59105e1a99eaSHangbin Liu 						  extack);
5911eacb9384SRoopa Prabhu 		if (err)
5912eacb9384SRoopa Prabhu 			goto errout;
5913eacb9384SRoopa Prabhu 	}
5914eacb9384SRoopa Prabhu 
5915ab364a6fSThomas Graf 	if (iif) {
5916ab364a6fSThomas Graf 		struct net_device *dev;
591772331bc0SShmulik Ladkani 		int flags = 0;
591872331bc0SShmulik Ladkani 
5919121622dbSFlorian Westphal 		rcu_read_lock();
5920121622dbSFlorian Westphal 
5921121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
5922ab364a6fSThomas Graf 		if (!dev) {
5923121622dbSFlorian Westphal 			rcu_read_unlock();
5924ab364a6fSThomas Graf 			err = -ENODEV;
5925ab364a6fSThomas Graf 			goto errout;
5926ab364a6fSThomas Graf 		}
592772331bc0SShmulik Ladkani 
592872331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
592972331bc0SShmulik Ladkani 
593072331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
593172331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
593272331bc0SShmulik Ladkani 
5933b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
5934121622dbSFlorian Westphal 
5935121622dbSFlorian Westphal 		rcu_read_unlock();
593672331bc0SShmulik Ladkani 	} else {
593772331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
593872331bc0SShmulik Ladkani 
593918c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
594018c3a61cSRoopa Prabhu 	}
594118c3a61cSRoopa Prabhu 
594218c3a61cSRoopa Prabhu 
594318c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
594418c3a61cSRoopa Prabhu 	if (rt->dst.error) {
594518c3a61cSRoopa Prabhu 		err = rt->dst.error;
594618c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
594718c3a61cSRoopa Prabhu 		goto errout;
5948ab364a6fSThomas Graf 	}
59491da177e4SLinus Torvalds 
59509d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
59519d6acb3bSWANG Cong 		err = rt->dst.error;
59529d6acb3bSWANG Cong 		ip6_rt_put(rt);
59539d6acb3bSWANG Cong 		goto errout;
59549d6acb3bSWANG Cong 	}
59559d6acb3bSWANG Cong 
59561da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
595738308473SDavid S. Miller 	if (!skb) {
595894e187c0SAmerigo Wang 		ip6_rt_put(rt);
5959ab364a6fSThomas Graf 		err = -ENOBUFS;
5960ab364a6fSThomas Graf 		goto errout;
5961ab364a6fSThomas Graf 	}
59621da177e4SLinus Torvalds 
5963d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
5964a68886a6SDavid Ahern 
5965a68886a6SDavid Ahern 	rcu_read_lock();
5966a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
5967886b7a50SMartin KaFai Lau 	if (from) {
596818c3a61cSRoopa Prabhu 		if (fibmatch)
5969886b7a50SMartin KaFai Lau 			err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5970886b7a50SMartin KaFai Lau 					    iif, RTM_NEWROUTE,
5971886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
597218c3a61cSRoopa Prabhu 					    nlh->nlmsg_seq, 0);
597318c3a61cSRoopa Prabhu 		else
5974a68886a6SDavid Ahern 			err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5975a68886a6SDavid Ahern 					    &fl6.saddr, iif, RTM_NEWROUTE,
5976886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
5977886b7a50SMartin KaFai Lau 					    nlh->nlmsg_seq, 0);
5978886b7a50SMartin KaFai Lau 	} else {
5979886b7a50SMartin KaFai Lau 		err = -ENETUNREACH;
5980886b7a50SMartin KaFai Lau 	}
5981a68886a6SDavid Ahern 	rcu_read_unlock();
5982a68886a6SDavid Ahern 
59831da177e4SLinus Torvalds 	if (err < 0) {
5984ab364a6fSThomas Graf 		kfree_skb(skb);
5985ab364a6fSThomas Graf 		goto errout;
59861da177e4SLinus Torvalds 	}
59871da177e4SLinus Torvalds 
598815e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
5989ab364a6fSThomas Graf errout:
59901da177e4SLinus Torvalds 	return err;
59911da177e4SLinus Torvalds }
59921da177e4SLinus Torvalds 
59938d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
599437a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
59951da177e4SLinus Torvalds {
59961da177e4SLinus Torvalds 	struct sk_buff *skb;
59975578689aSDaniel Lezcano 	struct net *net = info->nl_net;
5998528c4cebSDenis V. Lunev 	u32 seq;
5999528c4cebSDenis V. Lunev 	int err;
60000d51aa80SJamal Hadi Salim 
6001528c4cebSDenis V. Lunev 	err = -ENOBUFS;
600238308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
600386872cb5SThomas Graf 
600419e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
600538308473SDavid S. Miller 	if (!skb)
600621713ebcSThomas Graf 		goto errout;
60071da177e4SLinus Torvalds 
6008d4ead6b3SDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
6009f8cfe2ceSDavid Ahern 			    event, info->portid, seq, nlm_flags);
601026932566SPatrick McHardy 	if (err < 0) {
601126932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
601226932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
601326932566SPatrick McHardy 		kfree_skb(skb);
601426932566SPatrick McHardy 		goto errout;
601526932566SPatrick McHardy 	}
601615e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
60175578689aSDaniel Lezcano 		    info->nlh, gfp_any());
60181ce85fe4SPablo Neira Ayuso 	return;
601921713ebcSThomas Graf errout:
602021713ebcSThomas Graf 	if (err < 0)
60215578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
60221da177e4SLinus Torvalds }
60231da177e4SLinus Torvalds 
602419a3b7eeSDavid Ahern void fib6_rt_update(struct net *net, struct fib6_info *rt,
602519a3b7eeSDavid Ahern 		    struct nl_info *info)
602619a3b7eeSDavid Ahern {
602719a3b7eeSDavid Ahern 	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
602819a3b7eeSDavid Ahern 	struct sk_buff *skb;
602919a3b7eeSDavid Ahern 	int err = -ENOBUFS;
603019a3b7eeSDavid Ahern 
603119a3b7eeSDavid Ahern 	/* call_fib6_entry_notifiers will be removed when in-kernel notifier
603219a3b7eeSDavid Ahern 	 * is implemented and supported for nexthop objects
603319a3b7eeSDavid Ahern 	 */
603419a3b7eeSDavid Ahern 	call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
603519a3b7eeSDavid Ahern 
603619a3b7eeSDavid Ahern 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
603719a3b7eeSDavid Ahern 	if (!skb)
603819a3b7eeSDavid Ahern 		goto errout;
603919a3b7eeSDavid Ahern 
604019a3b7eeSDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
604119a3b7eeSDavid Ahern 			    RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
604219a3b7eeSDavid Ahern 	if (err < 0) {
604319a3b7eeSDavid Ahern 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
604419a3b7eeSDavid Ahern 		WARN_ON(err == -EMSGSIZE);
604519a3b7eeSDavid Ahern 		kfree_skb(skb);
604619a3b7eeSDavid Ahern 		goto errout;
604719a3b7eeSDavid Ahern 	}
604819a3b7eeSDavid Ahern 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
604919a3b7eeSDavid Ahern 		    info->nlh, gfp_any());
605019a3b7eeSDavid Ahern 	return;
605119a3b7eeSDavid Ahern errout:
605219a3b7eeSDavid Ahern 	if (err < 0)
605319a3b7eeSDavid Ahern 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
605419a3b7eeSDavid Ahern }
605519a3b7eeSDavid Ahern 
60568ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
6057351638e7SJiri Pirko 				unsigned long event, void *ptr)
60588ed67789SDaniel Lezcano {
6059351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6060c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
60618ed67789SDaniel Lezcano 
6062242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
6063242d3a49SWANG Cong 		return NOTIFY_OK;
6064242d3a49SWANG Cong 
6065242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
60661cf844c7SDavid Ahern 		net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
6067d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
60688ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
60698ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6070d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
60718ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
6072d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
60738ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
60748ed67789SDaniel Lezcano #endif
607576da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
607676da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
607776da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
607876da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
607976da0704SWANG Cong 		 */
608012d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
6081242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
608212d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
608312d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
6084242d3a49SWANG Cong #endif
60858ed67789SDaniel Lezcano 	}
60868ed67789SDaniel Lezcano 
60878ed67789SDaniel Lezcano 	return NOTIFY_OK;
60888ed67789SDaniel Lezcano }
60898ed67789SDaniel Lezcano 
60901da177e4SLinus Torvalds /*
60911da177e4SLinus Torvalds  *	/proc
60921da177e4SLinus Torvalds  */
60931da177e4SLinus Torvalds 
60941da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
60951da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
60961da177e4SLinus Torvalds {
609769ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
60981da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
609969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
610069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
610181eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
610269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
610369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
6104fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
610569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
61061da177e4SLinus Torvalds 
61071da177e4SLinus Torvalds 	return 0;
61081da177e4SLinus Torvalds }
61091da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
61101da177e4SLinus Torvalds 
61111da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
61121da177e4SLinus Torvalds 
611332927393SChristoph Hellwig static int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
611432927393SChristoph Hellwig 			      void *buffer, size_t *lenp, loff_t *ppos)
61151da177e4SLinus Torvalds {
6116c486da34SLucian Adrian Grijincu 	struct net *net;
6117c486da34SLucian Adrian Grijincu 	int delay;
6118f0fb9b28SAditya Pakki 	int ret;
6119c486da34SLucian Adrian Grijincu 	if (!write)
6120c486da34SLucian Adrian Grijincu 		return -EINVAL;
6121c486da34SLucian Adrian Grijincu 
6122c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
6123c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
6124f0fb9b28SAditya Pakki 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6125f0fb9b28SAditya Pakki 	if (ret)
6126f0fb9b28SAditya Pakki 		return ret;
6127f0fb9b28SAditya Pakki 
61282ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
61291da177e4SLinus Torvalds 	return 0;
61301da177e4SLinus Torvalds }
61311da177e4SLinus Torvalds 
6132ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = {
61331da177e4SLinus Torvalds 	{
61341da177e4SLinus Torvalds 		.procname	=	"flush",
61354990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
61361da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
613789c8b3a1SDave Jones 		.mode		=	0200,
61386d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
61391da177e4SLinus Torvalds 	},
61401da177e4SLinus Torvalds 	{
61411da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
61429a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
61431da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
61441da177e4SLinus Torvalds 		.mode		=	0644,
61456d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
61461da177e4SLinus Torvalds 	},
61471da177e4SLinus Torvalds 	{
61481da177e4SLinus Torvalds 		.procname	=	"max_size",
61494990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
61501da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
61511da177e4SLinus Torvalds 		.mode		=	0644,
61526d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
61531da177e4SLinus Torvalds 	},
61541da177e4SLinus Torvalds 	{
61551da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
61564990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
61571da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
61581da177e4SLinus Torvalds 		.mode		=	0644,
61596d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
61601da177e4SLinus Torvalds 	},
61611da177e4SLinus Torvalds 	{
61621da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
61634990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
61641da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
61651da177e4SLinus Torvalds 		.mode		=	0644,
61666d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
61671da177e4SLinus Torvalds 	},
61681da177e4SLinus Torvalds 	{
61691da177e4SLinus Torvalds 		.procname	=	"gc_interval",
61704990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
61711da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
61721da177e4SLinus Torvalds 		.mode		=	0644,
61736d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
61741da177e4SLinus Torvalds 	},
61751da177e4SLinus Torvalds 	{
61761da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
61774990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
61781da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
61791da177e4SLinus Torvalds 		.mode		=	0644,
6180f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
61811da177e4SLinus Torvalds 	},
61821da177e4SLinus Torvalds 	{
61831da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
61844990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
61851da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
61861da177e4SLinus Torvalds 		.mode		=	0644,
61876d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
61881da177e4SLinus Torvalds 	},
61891da177e4SLinus Torvalds 	{
61901da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
61914990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
61921da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
61931da177e4SLinus Torvalds 		.mode		=	0644,
6194f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
61951da177e4SLinus Torvalds 	},
61961da177e4SLinus Torvalds 	{
61971da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
61984990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
61991da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
62001da177e4SLinus Torvalds 		.mode		=	0644,
62016d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
62021da177e4SLinus Torvalds 	},
62037c6bb7d2SDavid Ahern 	{
62047c6bb7d2SDavid Ahern 		.procname	=	"skip_notify_on_dev_down",
62057c6bb7d2SDavid Ahern 		.data		=	&init_net.ipv6.sysctl.skip_notify_on_dev_down,
62067c6bb7d2SDavid Ahern 		.maxlen		=	sizeof(int),
62077c6bb7d2SDavid Ahern 		.mode		=	0644,
6208b8e8a863SEiichi Tsukata 		.proc_handler	=	proc_dointvec_minmax,
6209eec4844fSMatteo Croce 		.extra1		=	SYSCTL_ZERO,
6210eec4844fSMatteo Croce 		.extra2		=	SYSCTL_ONE,
62117c6bb7d2SDavid Ahern 	},
6212f8572d8fSEric W. Biederman 	{ }
62131da177e4SLinus Torvalds };
62141da177e4SLinus Torvalds 
62152c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
6216760f2d01SDaniel Lezcano {
6217760f2d01SDaniel Lezcano 	struct ctl_table *table;
6218760f2d01SDaniel Lezcano 
6219760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
6220760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
6221760f2d01SDaniel Lezcano 			GFP_KERNEL);
62225ee09105SYOSHIFUJI Hideaki 
62235ee09105SYOSHIFUJI Hideaki 	if (table) {
62245ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
6225c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
622686393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
62275ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
62285ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
62295ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
62305ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
62315ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
62325ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
62335ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
62349c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
62357c6bb7d2SDavid Ahern 		table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
6236464dc801SEric W. Biederman 
6237464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
6238464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
6239464dc801SEric W. Biederman 			table[0].procname = NULL;
62405ee09105SYOSHIFUJI Hideaki 	}
62415ee09105SYOSHIFUJI Hideaki 
6242760f2d01SDaniel Lezcano 	return table;
6243760f2d01SDaniel Lezcano }
62441da177e4SLinus Torvalds #endif
62451da177e4SLinus Torvalds 
62462c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
6247cdb18761SDaniel Lezcano {
6248633d424bSPavel Emelyanov 	int ret = -ENOMEM;
62498ed67789SDaniel Lezcano 
625086393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
625186393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
6252f2fc6a54SBenjamin Thery 
6253fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
6254fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
6255fc66f95cSEric Dumazet 
62561cf844c7SDavid Ahern 	net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
6257421842edSDavid Ahern 	if (!net->ipv6.fib6_null_entry)
6258421842edSDavid Ahern 		goto out_ip6_dst_entries;
62591cf844c7SDavid Ahern 	memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
62601cf844c7SDavid Ahern 	       sizeof(*net->ipv6.fib6_null_entry));
6261421842edSDavid Ahern 
62628ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
62638ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
62648ed67789SDaniel Lezcano 					   GFP_KERNEL);
62658ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
6266421842edSDavid Ahern 		goto out_fib6_null_entry;
6267d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
626862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
626962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
627074109218SWei Wang 	INIT_LIST_HEAD(&net->ipv6.ip6_null_entry->rt6i_uncached);
62718ed67789SDaniel Lezcano 
62728ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
6273feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
62748ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
62758ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
62768ed67789SDaniel Lezcano 					       GFP_KERNEL);
627768fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
627868fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
6279d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
628062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
628162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
628274109218SWei Wang 	INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->rt6i_uncached);
62838ed67789SDaniel Lezcano 
62848ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
62858ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
62868ed67789SDaniel Lezcano 					       GFP_KERNEL);
628768fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
628868fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
6289d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
629062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
629162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
629274109218SWei Wang 	INIT_LIST_HEAD(&net->ipv6.ip6_blk_hole_entry->rt6i_uncached);
6293b9b33e7cSPaolo Abeni #ifdef CONFIG_IPV6_SUBTREES
6294b9b33e7cSPaolo Abeni 	net->ipv6.fib6_routes_require_src = 0;
6295b9b33e7cSPaolo Abeni #endif
62968ed67789SDaniel Lezcano #endif
62978ed67789SDaniel Lezcano 
6298b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
6299b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
6300b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
6301b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
6302b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
6303b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
6304b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
6305b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
63067c6bb7d2SDavid Ahern 	net->ipv6.sysctl.skip_notify_on_dev_down = 0;
6307b339a47cSPeter Zijlstra 
63086891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
63096891a346SBenjamin Thery 
63108ed67789SDaniel Lezcano 	ret = 0;
63118ed67789SDaniel Lezcano out:
63128ed67789SDaniel Lezcano 	return ret;
6313f2fc6a54SBenjamin Thery 
631468fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
631568fffc67SPeter Zijlstra out_ip6_prohibit_entry:
631668fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
631768fffc67SPeter Zijlstra out_ip6_null_entry:
631868fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
631968fffc67SPeter Zijlstra #endif
6320421842edSDavid Ahern out_fib6_null_entry:
6321421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
6322fc66f95cSEric Dumazet out_ip6_dst_entries:
6323fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6324f2fc6a54SBenjamin Thery out_ip6_dst_ops:
6325f2fc6a54SBenjamin Thery 	goto out;
6326cdb18761SDaniel Lezcano }
6327cdb18761SDaniel Lezcano 
63282c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
6329cdb18761SDaniel Lezcano {
6330421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
63318ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
63328ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
63338ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
63348ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
63358ed67789SDaniel Lezcano #endif
633641bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
6337cdb18761SDaniel Lezcano }
6338cdb18761SDaniel Lezcano 
6339d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
6340d189634eSThomas Graf {
6341d189634eSThomas Graf #ifdef CONFIG_PROC_FS
6342c3506372SChristoph Hellwig 	proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
6343c3506372SChristoph Hellwig 			sizeof(struct ipv6_route_iter));
63443617d949SChristoph Hellwig 	proc_create_net_single("rt6_stats", 0444, net->proc_net,
63453617d949SChristoph Hellwig 			rt6_stats_seq_show, NULL);
6346d189634eSThomas Graf #endif
6347d189634eSThomas Graf 	return 0;
6348d189634eSThomas Graf }
6349d189634eSThomas Graf 
6350d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
6351d189634eSThomas Graf {
6352d189634eSThomas Graf #ifdef CONFIG_PROC_FS
6353ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
6354ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
6355d189634eSThomas Graf #endif
6356d189634eSThomas Graf }
6357d189634eSThomas Graf 
6358cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
6359cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
6360cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
6361cdb18761SDaniel Lezcano };
6362cdb18761SDaniel Lezcano 
6363c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
6364c3426b47SDavid S. Miller {
6365c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
6366c3426b47SDavid S. Miller 
6367c3426b47SDavid S. Miller 	if (!bp)
6368c3426b47SDavid S. Miller 		return -ENOMEM;
6369c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
6370c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
6371c3426b47SDavid S. Miller 	return 0;
6372c3426b47SDavid S. Miller }
6373c3426b47SDavid S. Miller 
6374c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
6375c3426b47SDavid S. Miller {
6376c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
6377c3426b47SDavid S. Miller 
6378c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
637956a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
6380c3426b47SDavid S. Miller 	kfree(bp);
6381c3426b47SDavid S. Miller }
6382c3426b47SDavid S. Miller 
63832b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
6384c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
6385c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
6386c3426b47SDavid S. Miller };
6387c3426b47SDavid S. Miller 
6388d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
6389d189634eSThomas Graf 	.init = ip6_route_net_init_late,
6390d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
6391d189634eSThomas Graf };
6392d189634eSThomas Graf 
63938ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
63948ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
6395242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
63968ed67789SDaniel Lezcano };
63978ed67789SDaniel Lezcano 
63982f460933SWANG Cong void __init ip6_route_init_special_entries(void)
63992f460933SWANG Cong {
64002f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
64012f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
64022f460933SWANG Cong 	 * manually for init_net */
64031cf844c7SDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
64042f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
64052f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
64062f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
64072f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
64082f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
64092f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
64102f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
64112f460933SWANG Cong   #endif
64122f460933SWANG Cong }
64132f460933SWANG Cong 
6414138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6)
6415138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6416138d0be3SYonghong Song DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
6417138d0be3SYonghong Song 
641815172a46SYonghong Song static const struct bpf_iter_reg ipv6_route_reg_info = {
6419138d0be3SYonghong Song 	.target			= "ipv6_route",
6420138d0be3SYonghong Song 	.seq_ops		= &ipv6_route_seq_ops,
6421138d0be3SYonghong Song 	.init_seq_private	= bpf_iter_init_seq_net,
6422138d0be3SYonghong Song 	.fini_seq_private	= bpf_iter_fini_seq_net,
6423138d0be3SYonghong Song 	.seq_priv_size		= sizeof(struct ipv6_route_iter),
64243c32cc1bSYonghong Song 	.ctx_arg_info_size	= 1,
64253c32cc1bSYonghong Song 	.ctx_arg_info		= {
64263c32cc1bSYonghong Song 		{ offsetof(struct bpf_iter__ipv6_route, rt),
64273c32cc1bSYonghong Song 		  PTR_TO_BTF_ID_OR_NULL },
64283c32cc1bSYonghong Song 	},
6429138d0be3SYonghong Song };
6430138d0be3SYonghong Song 
643115172a46SYonghong Song static int __init bpf_iter_register(void)
643215172a46SYonghong Song {
643315172a46SYonghong Song 	return bpf_iter_reg_target(&ipv6_route_reg_info);
6434138d0be3SYonghong Song }
6435138d0be3SYonghong Song 
6436138d0be3SYonghong Song static void bpf_iter_unregister(void)
6437138d0be3SYonghong Song {
6438ab2ee4fcSYonghong Song 	bpf_iter_unreg_target(&ipv6_route_reg_info);
6439138d0be3SYonghong Song }
6440138d0be3SYonghong Song #endif
6441138d0be3SYonghong Song #endif
6442138d0be3SYonghong Song 
6443433d49c3SDaniel Lezcano int __init ip6_route_init(void)
64441da177e4SLinus Torvalds {
6445433d49c3SDaniel Lezcano 	int ret;
64468d0b94afSMartin KaFai Lau 	int cpu;
6447433d49c3SDaniel Lezcano 
64489a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
64499a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
64509a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
64519a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
64529a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
6453c19a28e1SFernando Carrijo 		goto out;
645414e50e57SDavid S. Miller 
6455fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
64568ed67789SDaniel Lezcano 	if (ret)
6457bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
6458bdb3289fSDaniel Lezcano 
6459c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
6460c3426b47SDavid S. Miller 	if (ret)
6461e8803b6cSDavid S. Miller 		goto out_dst_entries;
64622a0c451aSThomas Graf 
64637e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
64647e52b33bSDavid S. Miller 	if (ret)
64657e52b33bSDavid S. Miller 		goto out_register_inetpeer;
6466c3426b47SDavid S. Miller 
64675dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
64685dc121e9SArnaud Ebalard 
6469e8803b6cSDavid S. Miller 	ret = fib6_init();
6470433d49c3SDaniel Lezcano 	if (ret)
64718ed67789SDaniel Lezcano 		goto out_register_subsys;
6472433d49c3SDaniel Lezcano 
6473433d49c3SDaniel Lezcano 	ret = xfrm6_init();
6474433d49c3SDaniel Lezcano 	if (ret)
6475e8803b6cSDavid S. Miller 		goto out_fib6_init;
6476c35b7e72SDaniel Lezcano 
6477433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
6478433d49c3SDaniel Lezcano 	if (ret)
6479433d49c3SDaniel Lezcano 		goto xfrm6_init;
64807e5449c2SDaniel Lezcano 
6481d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
6482d189634eSThomas Graf 	if (ret)
6483d189634eSThomas Graf 		goto fib6_rules_init;
6484d189634eSThomas Graf 
648516feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
648616feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
648716feebcfSFlorian Westphal 	if (ret < 0)
648816feebcfSFlorian Westphal 		goto out_register_late_subsys;
648916feebcfSFlorian Westphal 
649016feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
649116feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
649216feebcfSFlorian Westphal 	if (ret < 0)
649316feebcfSFlorian Westphal 		goto out_register_late_subsys;
649416feebcfSFlorian Westphal 
649516feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
649616feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
649716feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
649816feebcfSFlorian Westphal 	if (ret < 0)
6499d189634eSThomas Graf 		goto out_register_late_subsys;
6500433d49c3SDaniel Lezcano 
65018ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
6502cdb18761SDaniel Lezcano 	if (ret)
6503d189634eSThomas Graf 		goto out_register_late_subsys;
65048ed67789SDaniel Lezcano 
6505138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6)
6506138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6507138d0be3SYonghong Song 	ret = bpf_iter_register();
6508138d0be3SYonghong Song 	if (ret)
6509138d0be3SYonghong Song 		goto out_register_late_subsys;
6510138d0be3SYonghong Song #endif
6511138d0be3SYonghong Song #endif
6512138d0be3SYonghong Song 
65138d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
65148d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
65158d0b94afSMartin KaFai Lau 
65168d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
65178d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
65188d0b94afSMartin KaFai Lau 	}
65198d0b94afSMartin KaFai Lau 
6520433d49c3SDaniel Lezcano out:
6521433d49c3SDaniel Lezcano 	return ret;
6522433d49c3SDaniel Lezcano 
6523d189634eSThomas Graf out_register_late_subsys:
652416feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
6525d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
6526433d49c3SDaniel Lezcano fib6_rules_init:
6527433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
6528433d49c3SDaniel Lezcano xfrm6_init:
6529433d49c3SDaniel Lezcano 	xfrm6_fini();
65302a0c451aSThomas Graf out_fib6_init:
65312a0c451aSThomas Graf 	fib6_gc_cleanup();
65328ed67789SDaniel Lezcano out_register_subsys:
65338ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
65347e52b33bSDavid S. Miller out_register_inetpeer:
65357e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
6536fc66f95cSEric Dumazet out_dst_entries:
6537fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
6538433d49c3SDaniel Lezcano out_kmem_cache:
6539f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
6540433d49c3SDaniel Lezcano 	goto out;
65411da177e4SLinus Torvalds }
65421da177e4SLinus Torvalds 
65431da177e4SLinus Torvalds void ip6_route_cleanup(void)
65441da177e4SLinus Torvalds {
6545138d0be3SYonghong Song #if IS_BUILTIN(CONFIG_IPV6)
6546138d0be3SYonghong Song #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
6547138d0be3SYonghong Song 	bpf_iter_unregister();
6548138d0be3SYonghong Song #endif
6549138d0be3SYonghong Song #endif
65508ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
6551d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
6552101367c2SThomas Graf 	fib6_rules_cleanup();
65531da177e4SLinus Torvalds 	xfrm6_fini();
65541da177e4SLinus Torvalds 	fib6_gc_cleanup();
6555c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
65568ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
655741bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
6558f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
65591da177e4SLinus Torvalds }
6560