xref: /openbmc/linux/net/ipv6/route.c (revision 0fa6efc54780412c3fbd6f6e61a5dfb4ca9bb59c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Linux INET6 implementation
31da177e4SLinus Torvalds  *	FIB front-end.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
91da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
101da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
111da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds /*	Changes:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI
171da177e4SLinus Torvalds  *		reworked default router selection.
181da177e4SLinus Torvalds  *		- respect outgoing interface
191da177e4SLinus Torvalds  *		- select from (probably) reachable routers (i.e.
201da177e4SLinus Torvalds  *		routers in REACHABLE, STALE, DELAY or PROBE states).
211da177e4SLinus Torvalds  *		- always select the same router if it is (probably)
221da177e4SLinus Torvalds  *		reachable.  otherwise, round-robin the list.
23c0bece9fSYOSHIFUJI Hideaki  *	Ville Nuorvala
24c0bece9fSYOSHIFUJI Hideaki  *		Fixed routing subtrees.
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
28f3213831SJoe Perches 
294fc268d2SRandy Dunlap #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/errno.h>
31bc3b2d7fSPaul Gortmaker #include <linux/export.h>
321da177e4SLinus Torvalds #include <linux/types.h>
331da177e4SLinus Torvalds #include <linux/times.h>
341da177e4SLinus Torvalds #include <linux/socket.h>
351da177e4SLinus Torvalds #include <linux/sockios.h>
361da177e4SLinus Torvalds #include <linux/net.h>
371da177e4SLinus Torvalds #include <linux/route.h>
381da177e4SLinus Torvalds #include <linux/netdevice.h>
391da177e4SLinus Torvalds #include <linux/in6.h>
407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
411da177e4SLinus Torvalds #include <linux/init.h>
421da177e4SLinus Torvalds #include <linux/if_arp.h>
431da177e4SLinus Torvalds #include <linux/proc_fs.h>
441da177e4SLinus Torvalds #include <linux/seq_file.h>
455b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
4735732d01SWei Wang #include <linux/jhash.h>
48457c4cbcSEric W. Biederman #include <net/net_namespace.h>
491da177e4SLinus Torvalds #include <net/snmp.h>
501da177e4SLinus Torvalds #include <net/ipv6.h>
511da177e4SLinus Torvalds #include <net/ip6_fib.h>
521da177e4SLinus Torvalds #include <net/ip6_route.h>
531da177e4SLinus Torvalds #include <net/ndisc.h>
541da177e4SLinus Torvalds #include <net/addrconf.h>
551da177e4SLinus Torvalds #include <net/tcp.h>
561da177e4SLinus Torvalds #include <linux/rtnetlink.h>
571da177e4SLinus Torvalds #include <net/dst.h>
58904af04dSJiri Benc #include <net/dst_metadata.h>
591da177e4SLinus Torvalds #include <net/xfrm.h>
608d71740cSTom Tucker #include <net/netevent.h>
6121713ebcSThomas Graf #include <net/netlink.h>
623c618c1dSDavid Ahern #include <net/rtnh.h>
6319e42e45SRoopa Prabhu #include <net/lwtunnel.h>
64904af04dSJiri Benc #include <net/ip_tunnels.h>
65ca254490SDavid Ahern #include <net/l3mdev.h>
66eacb9384SRoopa Prabhu #include <net/ip.h>
677c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
701da177e4SLinus Torvalds #include <linux/sysctl.h>
711da177e4SLinus Torvalds #endif
721da177e4SLinus Torvalds 
7330d444d3SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type);
7430d444d3SDavid Ahern 
7530d444d3SDavid Ahern #define CREATE_TRACE_POINTS
7630d444d3SDavid Ahern #include <trace/events/fib6.h>
7730d444d3SDavid Ahern EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
7830d444d3SDavid Ahern #undef CREATE_TRACE_POINTS
7930d444d3SDavid Ahern 
80afc154e9SHannes Frederic Sowa enum rt6_nud_state {
817e980569SJiri Benc 	RT6_NUD_FAIL_HARD = -3,
827e980569SJiri Benc 	RT6_NUD_FAIL_PROBE = -2,
837e980569SJiri Benc 	RT6_NUD_FAIL_DO_RR = -1,
84afc154e9SHannes Frederic Sowa 	RT6_NUD_SUCCEED = 1
85afc154e9SHannes Frederic Sowa };
86afc154e9SHannes Frederic Sowa 
871da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
880dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
89ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
901da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
911da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
921da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
931da177e4SLinus Torvalds 				       struct net_device *dev, int how);
94569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
97ede2059dSEric W. Biederman static int		ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
987150aedeSKamala R static int		ip6_pkt_prohibit(struct sk_buff *skb);
99ede2059dSEric W. Biederman static int		ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
1001da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
1016700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1026700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
1036700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
1046700c270SDavid S. Miller 					struct sk_buff *skb);
105702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
106702cea56SDavid Ahern 			   int strict);
1078d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt);
108d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
1098d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
110d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
11116a16cd3SDavid Ahern 			 int iif, int type, u32 portid, u32 seq,
11216a16cd3SDavid Ahern 			 unsigned int flags);
1137e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
114510e2cedSWei Wang 					   const struct in6_addr *daddr,
115510e2cedSWei Wang 					   const struct in6_addr *saddr);
1161da177e4SLinus Torvalds 
11770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1188d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
119b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
120830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
121830218c1SDavid Ahern 					   struct net_device *dev,
12295c96174SEric Dumazet 					   unsigned int pref);
1238d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
124b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
125830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
126830218c1SDavid Ahern 					   struct net_device *dev);
12770ceb4f5SYOSHIFUJI Hideaki #endif
12870ceb4f5SYOSHIFUJI Hideaki 
1298d0b94afSMartin KaFai Lau struct uncached_list {
1308d0b94afSMartin KaFai Lau 	spinlock_t		lock;
1318d0b94afSMartin KaFai Lau 	struct list_head	head;
1328d0b94afSMartin KaFai Lau };
1338d0b94afSMartin KaFai Lau 
1348d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
1358d0b94afSMartin KaFai Lau 
136510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt)
1378d0b94afSMartin KaFai Lau {
1388d0b94afSMartin KaFai Lau 	struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
1398d0b94afSMartin KaFai Lau 
1408d0b94afSMartin KaFai Lau 	rt->rt6i_uncached_list = ul;
1418d0b94afSMartin KaFai Lau 
1428d0b94afSMartin KaFai Lau 	spin_lock_bh(&ul->lock);
1438d0b94afSMartin KaFai Lau 	list_add_tail(&rt->rt6i_uncached, &ul->head);
1448d0b94afSMartin KaFai Lau 	spin_unlock_bh(&ul->lock);
1458d0b94afSMartin KaFai Lau }
1468d0b94afSMartin KaFai Lau 
147510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt)
1488d0b94afSMartin KaFai Lau {
1498d0b94afSMartin KaFai Lau 	if (!list_empty(&rt->rt6i_uncached)) {
1508d0b94afSMartin KaFai Lau 		struct uncached_list *ul = rt->rt6i_uncached_list;
15181eb8447SWei Wang 		struct net *net = dev_net(rt->dst.dev);
1528d0b94afSMartin KaFai Lau 
1538d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1548d0b94afSMartin KaFai Lau 		list_del(&rt->rt6i_uncached);
15581eb8447SWei Wang 		atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
1568d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1578d0b94afSMartin KaFai Lau 	}
1588d0b94afSMartin KaFai Lau }
1598d0b94afSMartin KaFai Lau 
1608d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
1618d0b94afSMartin KaFai Lau {
1628d0b94afSMartin KaFai Lau 	struct net_device *loopback_dev = net->loopback_dev;
1638d0b94afSMartin KaFai Lau 	int cpu;
1648d0b94afSMartin KaFai Lau 
165e332bc67SEric W. Biederman 	if (dev == loopback_dev)
166e332bc67SEric W. Biederman 		return;
167e332bc67SEric W. Biederman 
1688d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
1698d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
1708d0b94afSMartin KaFai Lau 		struct rt6_info *rt;
1718d0b94afSMartin KaFai Lau 
1728d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1738d0b94afSMartin KaFai Lau 		list_for_each_entry(rt, &ul->head, rt6i_uncached) {
1748d0b94afSMartin KaFai Lau 			struct inet6_dev *rt_idev = rt->rt6i_idev;
1758d0b94afSMartin KaFai Lau 			struct net_device *rt_dev = rt->dst.dev;
1768d0b94afSMartin KaFai Lau 
177e332bc67SEric W. Biederman 			if (rt_idev->dev == dev) {
1788d0b94afSMartin KaFai Lau 				rt->rt6i_idev = in6_dev_get(loopback_dev);
1798d0b94afSMartin KaFai Lau 				in6_dev_put(rt_idev);
1808d0b94afSMartin KaFai Lau 			}
1818d0b94afSMartin KaFai Lau 
182e332bc67SEric W. Biederman 			if (rt_dev == dev) {
1838d0b94afSMartin KaFai Lau 				rt->dst.dev = loopback_dev;
1848d0b94afSMartin KaFai Lau 				dev_hold(rt->dst.dev);
1858d0b94afSMartin KaFai Lau 				dev_put(rt_dev);
1868d0b94afSMartin KaFai Lau 			}
1878d0b94afSMartin KaFai Lau 		}
1888d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1898d0b94afSMartin KaFai Lau 	}
1908d0b94afSMartin KaFai Lau }
1918d0b94afSMartin KaFai Lau 
192f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p,
193f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
194f894cbf8SDavid S. Miller 					     const void *daddr)
19539232973SDavid S. Miller {
196a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
19739232973SDavid S. Miller 		return (const void *) p;
198f894cbf8SDavid S. Miller 	else if (skb)
199f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
20039232973SDavid S. Miller 	return daddr;
20139232973SDavid S. Miller }
20239232973SDavid S. Miller 
203f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
204f8a1b43bSDavid Ahern 				   struct net_device *dev,
205f894cbf8SDavid S. Miller 				   struct sk_buff *skb,
206f894cbf8SDavid S. Miller 				   const void *daddr)
207d3aaeb38SDavid S. Miller {
20839232973SDavid S. Miller 	struct neighbour *n;
20939232973SDavid S. Miller 
210f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(gw, skb, daddr);
211f8a1b43bSDavid Ahern 	n = __ipv6_neigh_lookup(dev, daddr);
212f83c7790SDavid S. Miller 	if (n)
213f83c7790SDavid S. Miller 		return n;
2147adf3246SStefano Brivio 
2157adf3246SStefano Brivio 	n = neigh_create(&nd_tbl, daddr, dev);
2167adf3246SStefano Brivio 	return IS_ERR(n) ? NULL : n;
217f8a1b43bSDavid Ahern }
218f8a1b43bSDavid Ahern 
219f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
220f8a1b43bSDavid Ahern 					      struct sk_buff *skb,
221f8a1b43bSDavid Ahern 					      const void *daddr)
222f8a1b43bSDavid Ahern {
223f8a1b43bSDavid Ahern 	const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
224f8a1b43bSDavid Ahern 
225f8a1b43bSDavid Ahern 	return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr);
226f83c7790SDavid S. Miller }
227f83c7790SDavid S. Miller 
22863fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
22963fca65dSJulian Anastasov {
23063fca65dSJulian Anastasov 	struct net_device *dev = dst->dev;
23163fca65dSJulian Anastasov 	struct rt6_info *rt = (struct rt6_info *)dst;
23263fca65dSJulian Anastasov 
233f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
23463fca65dSJulian Anastasov 	if (!daddr)
23563fca65dSJulian Anastasov 		return;
23663fca65dSJulian Anastasov 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
23763fca65dSJulian Anastasov 		return;
23863fca65dSJulian Anastasov 	if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
23963fca65dSJulian Anastasov 		return;
24063fca65dSJulian Anastasov 	__ipv6_confirm_neigh(dev, daddr);
24163fca65dSJulian Anastasov }
24263fca65dSJulian Anastasov 
2439a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
2441da177e4SLinus Torvalds 	.family			=	AF_INET6,
2451da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
2461da177e4SLinus Torvalds 	.gc_thresh		=	1024,
2471da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
2480dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
249ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
250d4ead6b3SDavid Ahern 	.cow_metrics		=	dst_cow_metrics_generic,
2511da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
2521da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
2531da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
2541da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
2551da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
2566e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
2579f8955ccSEric W. Biederman 	.local_out		=	__ip6_local_out,
258f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
25963fca65dSJulian Anastasov 	.confirm_neigh		=	ip6_confirm_neigh,
2601da177e4SLinus Torvalds };
2611da177e4SLinus Torvalds 
262ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
263ec831ea7SRoland Dreier {
264618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
265618f9bc7SSteffen Klassert 
266618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
267ec831ea7SRoland Dreier }
268ec831ea7SRoland Dreier 
2696700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
2706700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
27114e50e57SDavid S. Miller {
27214e50e57SDavid S. Miller }
27314e50e57SDavid S. Miller 
2746700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
2756700c270SDavid S. Miller 				      struct sk_buff *skb)
276b587ee3bSDavid S. Miller {
277b587ee3bSDavid S. Miller }
278b587ee3bSDavid S. Miller 
27914e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
28014e50e57SDavid S. Miller 	.family			=	AF_INET6,
28114e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
28214e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
283ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
284214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
28514e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
286b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2870a1f5962SMartin KaFai Lau 	.cow_metrics		=	dst_cow_metrics_generic,
288f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
28914e50e57SDavid S. Miller };
29014e50e57SDavid S. Miller 
29162fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
29214edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
29362fa8a84SDavid S. Miller };
29462fa8a84SDavid S. Miller 
2958d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = {
29693c2fb25SDavid Ahern 	.fib6_flags	= (RTF_REJECT | RTF_NONEXTHOP),
29793c2fb25SDavid Ahern 	.fib6_protocol  = RTPROT_KERNEL,
29893c2fb25SDavid Ahern 	.fib6_metric	= ~(u32)0,
299f05713e0SEric Dumazet 	.fib6_ref	= REFCOUNT_INIT(1),
300421842edSDavid Ahern 	.fib6_type	= RTN_UNREACHABLE,
301421842edSDavid Ahern 	.fib6_metrics	= (struct dst_metrics *)&dst_default_metrics,
302421842edSDavid Ahern };
303421842edSDavid Ahern 
304fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
3051da177e4SLinus Torvalds 	.dst = {
3061da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
3071da177e4SLinus Torvalds 		.__use		= 1,
3082c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
3091da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
3101da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
3111da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
3121da177e4SLinus Torvalds 	},
3131da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3141da177e4SLinus Torvalds };
3151da177e4SLinus Torvalds 
316101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
317101367c2SThomas Graf 
318fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
319101367c2SThomas Graf 	.dst = {
320101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
321101367c2SThomas Graf 		.__use		= 1,
3222c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
323101367c2SThomas Graf 		.error		= -EACCES,
3249ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
3259ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
326101367c2SThomas Graf 	},
327101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
328101367c2SThomas Graf };
329101367c2SThomas Graf 
330fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
331101367c2SThomas Graf 	.dst = {
332101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
333101367c2SThomas Graf 		.__use		= 1,
3342c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
335101367c2SThomas Graf 		.error		= -EINVAL,
336352e512cSHerbert Xu 		.input		= dst_discard,
337ede2059dSEric W. Biederman 		.output		= dst_discard_out,
338101367c2SThomas Graf 	},
339101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
340101367c2SThomas Graf };
341101367c2SThomas Graf 
342101367c2SThomas Graf #endif
343101367c2SThomas Graf 
344ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt)
345ebfa45f0SMartin KaFai Lau {
346ebfa45f0SMartin KaFai Lau 	struct dst_entry *dst = &rt->dst;
347ebfa45f0SMartin KaFai Lau 
348ebfa45f0SMartin KaFai Lau 	memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
349ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_uncached);
350ebfa45f0SMartin KaFai Lau }
351ebfa45f0SMartin KaFai Lau 
3521da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
35393531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
354ad706862SMartin KaFai Lau 			       int flags)
3551da177e4SLinus Torvalds {
35697bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
357b2a9c0edSWei Wang 					1, DST_OBSOLETE_FORCE_CHK, flags);
358cf911662SDavid S. Miller 
35981eb8447SWei Wang 	if (rt) {
360ebfa45f0SMartin KaFai Lau 		rt6_info_init(rt);
36181eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
36281eb8447SWei Wang 	}
3638104891bSSteffen Klassert 
364cf911662SDavid S. Miller 	return rt;
3651da177e4SLinus Torvalds }
3669ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc);
367d52d3997SMartin KaFai Lau 
3681da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
371a68886a6SDavid Ahern 	struct fib6_info *from;
3728d0b94afSMartin KaFai Lau 	struct inet6_dev *idev;
3731da177e4SLinus Torvalds 
3741620a336SDavid Ahern 	ip_dst_metrics_put(dst);
3758d0b94afSMartin KaFai Lau 	rt6_uncached_list_del(rt);
3768d0b94afSMartin KaFai Lau 
3778d0b94afSMartin KaFai Lau 	idev = rt->rt6i_idev;
37838308473SDavid S. Miller 	if (idev) {
3791da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3801da177e4SLinus Torvalds 		in6_dev_put(idev);
3811da177e4SLinus Torvalds 	}
3821716a961SGao feng 
3830e233874SEric Dumazet 	from = xchg((__force struct fib6_info **)&rt->from, NULL);
38493531c67SDavid Ahern 	fib6_info_release(from);
385b3419363SDavid S. Miller }
386b3419363SDavid S. Miller 
3871da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3881da177e4SLinus Torvalds 			   int how)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3911da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3925a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
393c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3941da177e4SLinus Torvalds 
395e5645f51SWei Wang 	if (idev && idev->dev != loopback_dev) {
396e5645f51SWei Wang 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
39738308473SDavid S. Miller 		if (loopback_idev) {
3981da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
3991da177e4SLinus Torvalds 			in6_dev_put(idev);
4001da177e4SLinus Torvalds 		}
4011da177e4SLinus Torvalds 	}
40297cac082SDavid S. Miller }
4031da177e4SLinus Torvalds 
4045973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt)
4055973fb1eSMartin KaFai Lau {
4065973fb1eSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_EXPIRES)
4075973fb1eSMartin KaFai Lau 		return time_after(jiffies, rt->dst.expires);
4085973fb1eSMartin KaFai Lau 	else
4095973fb1eSMartin KaFai Lau 		return false;
4105973fb1eSMartin KaFai Lau }
4115973fb1eSMartin KaFai Lau 
412a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
4131da177e4SLinus Torvalds {
414a68886a6SDavid Ahern 	struct fib6_info *from;
415a68886a6SDavid Ahern 
416a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
417a68886a6SDavid Ahern 
4181716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
4191716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
420a50feda5SEric Dumazet 			return true;
421a68886a6SDavid Ahern 	} else if (from) {
4221e2ea8adSXin Long 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
423a68886a6SDavid Ahern 			fib6_check_expired(from);
4241716a961SGao feng 	}
425a50feda5SEric Dumazet 	return false;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
428b1d40991SDavid Ahern void fib6_select_path(const struct net *net, struct fib6_result *res,
429b1d40991SDavid Ahern 		      struct flowi6 *fl6, int oif, bool have_oif_match,
430b1d40991SDavid Ahern 		      const struct sk_buff *skb, int strict)
43151ebd318SNicolas Dichtel {
4328d1c802bSDavid Ahern 	struct fib6_info *sibling, *next_sibling;
433b1d40991SDavid Ahern 	struct fib6_info *match = res->f6i;
434b1d40991SDavid Ahern 
435b1d40991SDavid Ahern 	if (!match->fib6_nsiblings || have_oif_match)
436b1d40991SDavid Ahern 		goto out;
43751ebd318SNicolas Dichtel 
438b673d6ccSJakub Sitnicki 	/* We might have already computed the hash for ICMPv6 errors. In such
439b673d6ccSJakub Sitnicki 	 * case it will always be non-zero. Otherwise now is the time to do it.
440b673d6ccSJakub Sitnicki 	 */
441b673d6ccSJakub Sitnicki 	if (!fl6->mp_hash)
442b4bac172SDavid Ahern 		fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
443b673d6ccSJakub Sitnicki 
4441cf844c7SDavid Ahern 	if (fl6->mp_hash <= atomic_read(&match->fib6_nh->fib_nh_upper_bound))
445b1d40991SDavid Ahern 		goto out;
446bbfcd776SIdo Schimmel 
44793c2fb25SDavid Ahern 	list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
44893c2fb25SDavid Ahern 				 fib6_siblings) {
4491cf844c7SDavid Ahern 		const struct fib6_nh *nh = sibling->fib6_nh;
4505e670d84SDavid Ahern 		int nh_upper_bound;
4515e670d84SDavid Ahern 
452702cea56SDavid Ahern 		nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
4535e670d84SDavid Ahern 		if (fl6->mp_hash > nh_upper_bound)
4543d709f69SIdo Schimmel 			continue;
455702cea56SDavid Ahern 		if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
45652bd4c0cSNicolas Dichtel 			break;
45751ebd318SNicolas Dichtel 		match = sibling;
45851ebd318SNicolas Dichtel 		break;
45951ebd318SNicolas Dichtel 	}
4603d709f69SIdo Schimmel 
461b1d40991SDavid Ahern out:
462b1d40991SDavid Ahern 	res->f6i = match;
4631cf844c7SDavid Ahern 	res->nh = match->fib6_nh;
46451ebd318SNicolas Dichtel }
46551ebd318SNicolas Dichtel 
4661da177e4SLinus Torvalds /*
46766f5d6ceSWei Wang  *	Route lookup. rcu_read_lock() should be held.
4681da177e4SLinus Torvalds  */
4691da177e4SLinus Torvalds 
4700c59d006SDavid Ahern static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
4710c59d006SDavid Ahern 			       const struct in6_addr *saddr, int oif, int flags)
4720c59d006SDavid Ahern {
4730c59d006SDavid Ahern 	const struct net_device *dev;
4740c59d006SDavid Ahern 
4750c59d006SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD)
4760c59d006SDavid Ahern 		return false;
4770c59d006SDavid Ahern 
4780c59d006SDavid Ahern 	dev = nh->fib_nh_dev;
4790c59d006SDavid Ahern 	if (oif) {
4800c59d006SDavid Ahern 		if (dev->ifindex == oif)
4810c59d006SDavid Ahern 			return true;
4820c59d006SDavid Ahern 	} else {
4830c59d006SDavid Ahern 		if (ipv6_chk_addr(net, saddr, dev,
4840c59d006SDavid Ahern 				  flags & RT6_LOOKUP_F_IFACE))
4850c59d006SDavid Ahern 			return true;
4860c59d006SDavid Ahern 	}
4870c59d006SDavid Ahern 
4880c59d006SDavid Ahern 	return false;
4890c59d006SDavid Ahern }
4900c59d006SDavid Ahern 
49175ef7389SDavid Ahern static void rt6_device_match(struct net *net, struct fib6_result *res,
49275ef7389SDavid Ahern 			     const struct in6_addr *saddr, int oif, int flags)
4931da177e4SLinus Torvalds {
49475ef7389SDavid Ahern 	struct fib6_info *f6i = res->f6i;
49575ef7389SDavid Ahern 	struct fib6_info *spf6i;
49675ef7389SDavid Ahern 	struct fib6_nh *nh;
4971da177e4SLinus Torvalds 
49875ef7389SDavid Ahern 	if (!oif && ipv6_addr_any(saddr)) {
4991cf844c7SDavid Ahern 		nh = f6i->fib6_nh;
5007d21fec9SDavid Ahern 		if (!(nh->fib_nh_flags & RTNH_F_DEAD))
5017d21fec9SDavid Ahern 			goto out;
5021da177e4SLinus Torvalds 	}
5031da177e4SLinus Torvalds 
50475ef7389SDavid Ahern 	for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
5051cf844c7SDavid Ahern 		nh = spf6i->fib6_nh;
50675ef7389SDavid Ahern 		if (__rt6_device_match(net, nh, saddr, oif, flags)) {
50775ef7389SDavid Ahern 			res->f6i = spf6i;
5087d21fec9SDavid Ahern 			goto out;
50975ef7389SDavid Ahern 		}
51075ef7389SDavid Ahern 	}
5111da177e4SLinus Torvalds 
51275ef7389SDavid Ahern 	if (oif && flags & RT6_LOOKUP_F_IFACE) {
51375ef7389SDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
5141cf844c7SDavid Ahern 		nh = res->f6i->fib6_nh;
5157d21fec9SDavid Ahern 		goto out;
51675ef7389SDavid Ahern 	}
51775ef7389SDavid Ahern 
5181cf844c7SDavid Ahern 	nh = f6i->fib6_nh;
5197d21fec9SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD) {
52075ef7389SDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
5211cf844c7SDavid Ahern 		nh = res->f6i->fib6_nh;
52275ef7389SDavid Ahern 	}
5237d21fec9SDavid Ahern out:
5247d21fec9SDavid Ahern 	res->nh = nh;
5257d21fec9SDavid Ahern 	res->fib6_type = res->f6i->fib6_type;
5267d21fec9SDavid Ahern 	res->fib6_flags = res->f6i->fib6_flags;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
52927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
530c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
531c2f17e82SHannes Frederic Sowa 	struct work_struct work;
532c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
533c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
534c2f17e82SHannes Frederic Sowa };
535c2f17e82SHannes Frederic Sowa 
536c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
537c2f17e82SHannes Frederic Sowa {
538c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
539c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
540c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
541c2f17e82SHannes Frederic Sowa 
542c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
543adc176c5SErik Nordmark 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
544c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
545662f5533SMichael Büsch 	kfree(work);
546c2f17e82SHannes Frederic Sowa }
547c2f17e82SHannes Frederic Sowa 
548cc3a86c8SDavid Ahern static void rt6_probe(struct fib6_nh *fib6_nh)
54927097255SYOSHIFUJI Hideaki {
550f547fac6SSabrina Dubroca 	struct __rt6_probe_work *work = NULL;
5515e670d84SDavid Ahern 	const struct in6_addr *nh_gw;
552f2c31e32SEric Dumazet 	struct neighbour *neigh;
5535e670d84SDavid Ahern 	struct net_device *dev;
554f547fac6SSabrina Dubroca 	struct inet6_dev *idev;
5555e670d84SDavid Ahern 
55627097255SYOSHIFUJI Hideaki 	/*
55727097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
55827097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
55927097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
56027097255SYOSHIFUJI Hideaki 	 *
56127097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
56227097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
56327097255SYOSHIFUJI Hideaki 	 */
564cc3a86c8SDavid Ahern 	if (fib6_nh->fib_nh_gw_family)
565fdd6681dSAmerigo Wang 		return;
5665e670d84SDavid Ahern 
567cc3a86c8SDavid Ahern 	nh_gw = &fib6_nh->fib_nh_gw6;
568cc3a86c8SDavid Ahern 	dev = fib6_nh->fib_nh_dev;
5692152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
570f547fac6SSabrina Dubroca 	idev = __in6_dev_get(dev);
5715e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
5722152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
5738d6c31bfSMartin KaFai Lau 		if (neigh->nud_state & NUD_VALID)
5748d6c31bfSMartin KaFai Lau 			goto out;
5758d6c31bfSMartin KaFai Lau 
5762152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
577990edb42SMartin KaFai Lau 		if (!(neigh->nud_state & NUD_VALID) &&
578990edb42SMartin KaFai Lau 		    time_after(jiffies,
579dcd1f572SDavid Ahern 			       neigh->updated + idev->cnf.rtr_probe_interval)) {
580c2f17e82SHannes Frederic Sowa 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
581990edb42SMartin KaFai Lau 			if (work)
5827e980569SJiri Benc 				__neigh_set_probe_once(neigh);
583990edb42SMartin KaFai Lau 		}
584c2f17e82SHannes Frederic Sowa 		write_unlock(&neigh->lock);
585cc3a86c8SDavid Ahern 	} else if (time_after(jiffies, fib6_nh->last_probe +
586f547fac6SSabrina Dubroca 				       idev->cnf.rtr_probe_interval)) {
587990edb42SMartin KaFai Lau 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
588990edb42SMartin KaFai Lau 	}
589c2f17e82SHannes Frederic Sowa 
590c2f17e82SHannes Frederic Sowa 	if (work) {
591cc3a86c8SDavid Ahern 		fib6_nh->last_probe = jiffies;
592c2f17e82SHannes Frederic Sowa 		INIT_WORK(&work->work, rt6_probe_deferred);
5935e670d84SDavid Ahern 		work->target = *nh_gw;
5945e670d84SDavid Ahern 		dev_hold(dev);
5955e670d84SDavid Ahern 		work->dev = dev;
596c2f17e82SHannes Frederic Sowa 		schedule_work(&work->work);
597c2f17e82SHannes Frederic Sowa 	}
598990edb42SMartin KaFai Lau 
5998d6c31bfSMartin KaFai Lau out:
6002152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
601f2c31e32SEric Dumazet }
60227097255SYOSHIFUJI Hideaki #else
603cc3a86c8SDavid Ahern static inline void rt6_probe(struct fib6_nh *fib6_nh)
60427097255SYOSHIFUJI Hideaki {
60527097255SYOSHIFUJI Hideaki }
60627097255SYOSHIFUJI Hideaki #endif
60727097255SYOSHIFUJI Hideaki 
6081da177e4SLinus Torvalds /*
609554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
6101da177e4SLinus Torvalds  */
6111ba9a895SDavid Ahern static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
6121da177e4SLinus Torvalds {
613afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
6145e670d84SDavid Ahern 	struct neighbour *neigh;
615f2c31e32SEric Dumazet 
616145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
6171ba9a895SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
6181ba9a895SDavid Ahern 					  &fib6_nh->fib_nh_gw6);
619145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
620145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
621554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
622afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
623398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
624a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
625afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
6267e980569SJiri Benc 		else
6277e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
628398bcbebSYOSHIFUJI Hideaki #endif
629145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
630afc154e9SHannes Frederic Sowa 	} else {
631afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
6327e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
633a5a81f0bSPaul Marks 	}
634145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
635145a3621SYOSHIFUJI Hideaki / 吉藤英明 
636a5a81f0bSPaul Marks 	return ret;
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
639702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
640702cea56SDavid Ahern 			   int strict)
641554cfb7eSYOSHIFUJI Hideaki {
6426e1809a5SDavid Ahern 	int m = 0;
6434d0c5911SYOSHIFUJI Hideaki 
6446e1809a5SDavid Ahern 	if (!oif || nh->fib_nh_dev->ifindex == oif)
6456e1809a5SDavid Ahern 		m = 2;
6466e1809a5SDavid Ahern 
64777d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
648afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
649ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
650702cea56SDavid Ahern 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
651ebacaaa0SYOSHIFUJI Hideaki #endif
6521ba9a895SDavid Ahern 	if ((strict & RT6_LOOKUP_F_REACHABLE) &&
653702cea56SDavid Ahern 	    !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
6541ba9a895SDavid Ahern 		int n = rt6_check_neigh(nh);
655afc154e9SHannes Frederic Sowa 		if (n < 0)
656afc154e9SHannes Frederic Sowa 			return n;
657afc154e9SHannes Frederic Sowa 	}
658554cfb7eSYOSHIFUJI Hideaki 	return m;
659554cfb7eSYOSHIFUJI Hideaki }
660554cfb7eSYOSHIFUJI Hideaki 
66128679ed1SDavid Ahern static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
66228679ed1SDavid Ahern 		       int oif, int strict, int *mpri, bool *do_rr)
663554cfb7eSYOSHIFUJI Hideaki {
664afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
66528679ed1SDavid Ahern 	bool rc = false;
66628679ed1SDavid Ahern 	int m;
66735103d11SAndy Gospodarek 
66828679ed1SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD)
6698067bb8cSIdo Schimmel 		goto out;
6708067bb8cSIdo Schimmel 
67128679ed1SDavid Ahern 	if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
67228679ed1SDavid Ahern 	    nh->fib_nh_flags & RTNH_F_LINKDOWN &&
673d5d32e4bSDavid Ahern 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
67435103d11SAndy Gospodarek 		goto out;
675554cfb7eSYOSHIFUJI Hideaki 
67628679ed1SDavid Ahern 	m = rt6_score_route(nh, fib6_flags, oif, strict);
6777e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
678afc154e9SHannes Frederic Sowa 		match_do_rr = true;
679afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
6807e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
681f11e6659SDavid S. Miller 		goto out;
6821da177e4SLinus Torvalds 	}
683f11e6659SDavid S. Miller 
684afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
68528679ed1SDavid Ahern 		rt6_probe(nh);
686afc154e9SHannes Frederic Sowa 
6877e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
688afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
689afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
690afc154e9SHannes Frederic Sowa 		*mpri = m;
69128679ed1SDavid Ahern 		rc = true;
692afc154e9SHannes Frederic Sowa 	}
693f11e6659SDavid S. Miller out:
69428679ed1SDavid Ahern 	return rc;
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
697b7bc4b6aSDavid Ahern static void __find_rr_leaf(struct fib6_info *f6i_start,
69830c15f03SDavid Ahern 			   struct fib6_info *nomatch, u32 metric,
699b7bc4b6aSDavid Ahern 			   struct fib6_result *res, struct fib6_info **cont,
70030c15f03SDavid Ahern 			   int oif, int strict, bool *do_rr, int *mpri)
70130c15f03SDavid Ahern {
702b7bc4b6aSDavid Ahern 	struct fib6_info *f6i;
70330c15f03SDavid Ahern 
704b7bc4b6aSDavid Ahern 	for (f6i = f6i_start;
705b7bc4b6aSDavid Ahern 	     f6i && f6i != nomatch;
706b7bc4b6aSDavid Ahern 	     f6i = rcu_dereference(f6i->fib6_next)) {
70730c15f03SDavid Ahern 		struct fib6_nh *nh;
70830c15f03SDavid Ahern 
709b7bc4b6aSDavid Ahern 		if (cont && f6i->fib6_metric != metric) {
710b7bc4b6aSDavid Ahern 			*cont = f6i;
71130c15f03SDavid Ahern 			return;
71230c15f03SDavid Ahern 		}
71330c15f03SDavid Ahern 
714b7bc4b6aSDavid Ahern 		if (fib6_check_expired(f6i))
71530c15f03SDavid Ahern 			continue;
71630c15f03SDavid Ahern 
7171cf844c7SDavid Ahern 		nh = f6i->fib6_nh;
718b7bc4b6aSDavid Ahern 		if (find_match(nh, f6i->fib6_flags, oif, strict, mpri, do_rr)) {
719b7bc4b6aSDavid Ahern 			res->f6i = f6i;
720b7bc4b6aSDavid Ahern 			res->nh = nh;
7217d21fec9SDavid Ahern 			res->fib6_flags = f6i->fib6_flags;
7227d21fec9SDavid Ahern 			res->fib6_type = f6i->fib6_type;
723b7bc4b6aSDavid Ahern 		}
72430c15f03SDavid Ahern 	}
72530c15f03SDavid Ahern }
72630c15f03SDavid Ahern 
727b7bc4b6aSDavid Ahern static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
728b7bc4b6aSDavid Ahern 			 struct fib6_info *rr_head, int oif, int strict,
729b7bc4b6aSDavid Ahern 			 bool *do_rr, struct fib6_result *res)
730f11e6659SDavid S. Miller {
731b7bc4b6aSDavid Ahern 	u32 metric = rr_head->fib6_metric;
732b7bc4b6aSDavid Ahern 	struct fib6_info *cont = NULL;
733f11e6659SDavid S. Miller 	int mpri = -1;
734f11e6659SDavid S. Miller 
735b7bc4b6aSDavid Ahern 	__find_rr_leaf(rr_head, NULL, metric, res, &cont,
73630c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
7379fbdcfafSSteffen Klassert 
738b7bc4b6aSDavid Ahern 	__find_rr_leaf(leaf, rr_head, metric, res, &cont,
73930c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
7409fbdcfafSSteffen Klassert 
741b7bc4b6aSDavid Ahern 	if (res->f6i || !cont)
742b7bc4b6aSDavid Ahern 		return;
7439fbdcfafSSteffen Klassert 
744b7bc4b6aSDavid Ahern 	__find_rr_leaf(cont, NULL, metric, res, NULL,
74530c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
746f11e6659SDavid S. Miller }
747f11e6659SDavid S. Miller 
748b7bc4b6aSDavid Ahern static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
749b7bc4b6aSDavid Ahern 		       struct fib6_result *res, int strict)
750f11e6659SDavid S. Miller {
7518d1c802bSDavid Ahern 	struct fib6_info *leaf = rcu_dereference(fn->leaf);
752b7bc4b6aSDavid Ahern 	struct fib6_info *rt0;
753afc154e9SHannes Frederic Sowa 	bool do_rr = false;
75417ecf590SWei Wang 	int key_plen;
755f11e6659SDavid S. Miller 
756b7bc4b6aSDavid Ahern 	/* make sure this function or its helpers sets f6i */
757b7bc4b6aSDavid Ahern 	res->f6i = NULL;
758b7bc4b6aSDavid Ahern 
759421842edSDavid Ahern 	if (!leaf || leaf == net->ipv6.fib6_null_entry)
760b7bc4b6aSDavid Ahern 		goto out;
7618d1040e8SWei Wang 
76266f5d6ceSWei Wang 	rt0 = rcu_dereference(fn->rr_ptr);
763f11e6659SDavid S. Miller 	if (!rt0)
76466f5d6ceSWei Wang 		rt0 = leaf;
765f11e6659SDavid S. Miller 
76617ecf590SWei Wang 	/* Double check to make sure fn is not an intermediate node
76717ecf590SWei Wang 	 * and fn->leaf does not points to its child's leaf
76817ecf590SWei Wang 	 * (This might happen if all routes under fn are deleted from
76917ecf590SWei Wang 	 * the tree and fib6_repair_tree() is called on the node.)
77017ecf590SWei Wang 	 */
77193c2fb25SDavid Ahern 	key_plen = rt0->fib6_dst.plen;
77217ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES
77393c2fb25SDavid Ahern 	if (rt0->fib6_src.plen)
77493c2fb25SDavid Ahern 		key_plen = rt0->fib6_src.plen;
77517ecf590SWei Wang #endif
77617ecf590SWei Wang 	if (fn->fn_bit != key_plen)
777b7bc4b6aSDavid Ahern 		goto out;
77817ecf590SWei Wang 
779b7bc4b6aSDavid Ahern 	find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
780afc154e9SHannes Frederic Sowa 	if (do_rr) {
7818fb11a9aSDavid Ahern 		struct fib6_info *next = rcu_dereference(rt0->fib6_next);
782f11e6659SDavid S. Miller 
783554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
78493c2fb25SDavid Ahern 		if (!next || next->fib6_metric != rt0->fib6_metric)
7858d1040e8SWei Wang 			next = leaf;
786f11e6659SDavid S. Miller 
78766f5d6ceSWei Wang 		if (next != rt0) {
78893c2fb25SDavid Ahern 			spin_lock_bh(&leaf->fib6_table->tb6_lock);
78966f5d6ceSWei Wang 			/* make sure next is not being deleted from the tree */
79093c2fb25SDavid Ahern 			if (next->fib6_node)
79166f5d6ceSWei Wang 				rcu_assign_pointer(fn->rr_ptr, next);
79293c2fb25SDavid Ahern 			spin_unlock_bh(&leaf->fib6_table->tb6_lock);
79366f5d6ceSWei Wang 		}
794554cfb7eSYOSHIFUJI Hideaki 	}
795554cfb7eSYOSHIFUJI Hideaki 
796b7bc4b6aSDavid Ahern out:
797b7bc4b6aSDavid Ahern 	if (!res->f6i) {
798b7bc4b6aSDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
7991cf844c7SDavid Ahern 		res->nh = res->f6i->fib6_nh;
8007d21fec9SDavid Ahern 		res->fib6_flags = res->f6i->fib6_flags;
8017d21fec9SDavid Ahern 		res->fib6_type = res->f6i->fib6_type;
802b7bc4b6aSDavid Ahern 	}
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
80585bd05deSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
8068b9df265SMartin KaFai Lau {
80785bd05deSDavid Ahern 	return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
80885bd05deSDavid Ahern 	       res->nh->fib_nh_gw_family;
8098b9df265SMartin KaFai Lau }
8108b9df265SMartin KaFai Lau 
81170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
81270ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
813b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
81470ceb4f5SYOSHIFUJI Hideaki {
815c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
81670ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
81770ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
81870ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
8194bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
8208d1c802bSDavid Ahern 	struct fib6_info *rt;
82170ceb4f5SYOSHIFUJI Hideaki 
82270ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
82370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
82470ceb4f5SYOSHIFUJI Hideaki 	}
82570ceb4f5SYOSHIFUJI Hideaki 
82670ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
82770ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
82870ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
82970ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
83070ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
83170ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
83270ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
83370ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
83470ceb4f5SYOSHIFUJI Hideaki 		}
83570ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
83670ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
83770ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
83870ceb4f5SYOSHIFUJI Hideaki 		}
83970ceb4f5SYOSHIFUJI Hideaki 	}
84070ceb4f5SYOSHIFUJI Hideaki 
84170ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
84270ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
8433933fc95SJens Rosenboom 		return -EINVAL;
84470ceb4f5SYOSHIFUJI Hideaki 
8454bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
84670ceb4f5SYOSHIFUJI Hideaki 
84770ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
84870ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
84970ceb4f5SYOSHIFUJI Hideaki 	else {
85070ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
85170ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
85270ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
85370ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
85470ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
85570ceb4f5SYOSHIFUJI Hideaki 	}
85670ceb4f5SYOSHIFUJI Hideaki 
857f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
858afb1d4b5SDavid Ahern 		rt = rt6_get_dflt_router(net, gwaddr, dev);
859f104a567SDuan Jiong 	else
860f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
861830218c1SDavid Ahern 					gwaddr, dev);
86270ceb4f5SYOSHIFUJI Hideaki 
86370ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
864afb1d4b5SDavid Ahern 		ip6_del_rt(net, rt);
86570ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
86670ceb4f5SYOSHIFUJI Hideaki 	}
86770ceb4f5SYOSHIFUJI Hideaki 
86870ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
869830218c1SDavid Ahern 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
870830218c1SDavid Ahern 					dev, pref);
87170ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
87293c2fb25SDavid Ahern 		rt->fib6_flags = RTF_ROUTEINFO |
87393c2fb25SDavid Ahern 				 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
87470ceb4f5SYOSHIFUJI Hideaki 
87570ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
8761716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
87714895687SDavid Ahern 			fib6_clean_expires(rt);
8781716a961SGao feng 		else
87914895687SDavid Ahern 			fib6_set_expires(rt, jiffies + HZ * lifetime);
8801716a961SGao feng 
88193531c67SDavid Ahern 		fib6_info_release(rt);
88270ceb4f5SYOSHIFUJI Hideaki 	}
88370ceb4f5SYOSHIFUJI Hideaki 	return 0;
88470ceb4f5SYOSHIFUJI Hideaki }
88570ceb4f5SYOSHIFUJI Hideaki #endif
88670ceb4f5SYOSHIFUJI Hideaki 
887ae90d867SDavid Ahern /*
888ae90d867SDavid Ahern  *	Misc support functions
889ae90d867SDavid Ahern  */
890ae90d867SDavid Ahern 
891ae90d867SDavid Ahern /* called with rcu_lock held */
8920d161581SDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
893ae90d867SDavid Ahern {
8940d161581SDavid Ahern 	struct net_device *dev = res->nh->fib_nh_dev;
895ae90d867SDavid Ahern 
8967d21fec9SDavid Ahern 	if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
897ae90d867SDavid Ahern 		/* for copies of local routes, dst->dev needs to be the
898ae90d867SDavid Ahern 		 * device if it is a master device, the master device if
899ae90d867SDavid Ahern 		 * device is enslaved, and the loopback as the default
900ae90d867SDavid Ahern 		 */
901ae90d867SDavid Ahern 		if (netif_is_l3_slave(dev) &&
9027d21fec9SDavid Ahern 		    !rt6_need_strict(&res->f6i->fib6_dst.addr))
903ae90d867SDavid Ahern 			dev = l3mdev_master_dev_rcu(dev);
904ae90d867SDavid Ahern 		else if (!netif_is_l3_master(dev))
905ae90d867SDavid Ahern 			dev = dev_net(dev)->loopback_dev;
906ae90d867SDavid Ahern 		/* last case is netif_is_l3_master(dev) is true in which
907ae90d867SDavid Ahern 		 * case we want dev returned to be dev
908ae90d867SDavid Ahern 		 */
909ae90d867SDavid Ahern 	}
910ae90d867SDavid Ahern 
911ae90d867SDavid Ahern 	return dev;
912ae90d867SDavid Ahern }
913ae90d867SDavid Ahern 
9146edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = {
9156edb3c96SDavid Ahern 	[RTN_UNSPEC]	= 0,
9166edb3c96SDavid Ahern 	[RTN_UNICAST]	= 0,
9176edb3c96SDavid Ahern 	[RTN_LOCAL]	= 0,
9186edb3c96SDavid Ahern 	[RTN_BROADCAST]	= 0,
9196edb3c96SDavid Ahern 	[RTN_ANYCAST]	= 0,
9206edb3c96SDavid Ahern 	[RTN_MULTICAST]	= 0,
9216edb3c96SDavid Ahern 	[RTN_BLACKHOLE]	= -EINVAL,
9226edb3c96SDavid Ahern 	[RTN_UNREACHABLE] = -EHOSTUNREACH,
9236edb3c96SDavid Ahern 	[RTN_PROHIBIT]	= -EACCES,
9246edb3c96SDavid Ahern 	[RTN_THROW]	= -EAGAIN,
9256edb3c96SDavid Ahern 	[RTN_NAT]	= -EINVAL,
9266edb3c96SDavid Ahern 	[RTN_XRESOLVE]	= -EINVAL,
9276edb3c96SDavid Ahern };
9286edb3c96SDavid Ahern 
9296edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type)
9306edb3c96SDavid Ahern {
9316edb3c96SDavid Ahern 	return fib6_prop[fib6_type];
9326edb3c96SDavid Ahern }
9336edb3c96SDavid Ahern 
9348d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
9353b6761d1SDavid Ahern {
9363b6761d1SDavid Ahern 	unsigned short flags = 0;
9373b6761d1SDavid Ahern 
9383b6761d1SDavid Ahern 	if (rt->dst_nocount)
9393b6761d1SDavid Ahern 		flags |= DST_NOCOUNT;
9403b6761d1SDavid Ahern 	if (rt->dst_nopolicy)
9413b6761d1SDavid Ahern 		flags |= DST_NOPOLICY;
9423b6761d1SDavid Ahern 	if (rt->dst_host)
9433b6761d1SDavid Ahern 		flags |= DST_HOST;
9443b6761d1SDavid Ahern 
9453b6761d1SDavid Ahern 	return flags;
9463b6761d1SDavid Ahern }
9473b6761d1SDavid Ahern 
9487d21fec9SDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
9496edb3c96SDavid Ahern {
9507d21fec9SDavid Ahern 	rt->dst.error = ip6_rt_type_to_error(fib6_type);
9516edb3c96SDavid Ahern 
9527d21fec9SDavid Ahern 	switch (fib6_type) {
9536edb3c96SDavid Ahern 	case RTN_BLACKHOLE:
9546edb3c96SDavid Ahern 		rt->dst.output = dst_discard_out;
9556edb3c96SDavid Ahern 		rt->dst.input = dst_discard;
9566edb3c96SDavid Ahern 		break;
9576edb3c96SDavid Ahern 	case RTN_PROHIBIT:
9586edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_prohibit_out;
9596edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_prohibit;
9606edb3c96SDavid Ahern 		break;
9616edb3c96SDavid Ahern 	case RTN_THROW:
9626edb3c96SDavid Ahern 	case RTN_UNREACHABLE:
9636edb3c96SDavid Ahern 	default:
9646edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_discard_out;
9656edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_discard;
9666edb3c96SDavid Ahern 		break;
9676edb3c96SDavid Ahern 	}
9686edb3c96SDavid Ahern }
9696edb3c96SDavid Ahern 
9700d161581SDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
9716edb3c96SDavid Ahern {
9727d21fec9SDavid Ahern 	struct fib6_info *f6i = res->f6i;
9730d161581SDavid Ahern 
9747d21fec9SDavid Ahern 	if (res->fib6_flags & RTF_REJECT) {
9757d21fec9SDavid Ahern 		ip6_rt_init_dst_reject(rt, res->fib6_type);
9766edb3c96SDavid Ahern 		return;
9776edb3c96SDavid Ahern 	}
9786edb3c96SDavid Ahern 
9796edb3c96SDavid Ahern 	rt->dst.error = 0;
9806edb3c96SDavid Ahern 	rt->dst.output = ip6_output;
9816edb3c96SDavid Ahern 
9827d21fec9SDavid Ahern 	if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
9836edb3c96SDavid Ahern 		rt->dst.input = ip6_input;
9847d21fec9SDavid Ahern 	} else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
9856edb3c96SDavid Ahern 		rt->dst.input = ip6_mc_input;
9866edb3c96SDavid Ahern 	} else {
9876edb3c96SDavid Ahern 		rt->dst.input = ip6_forward;
9886edb3c96SDavid Ahern 	}
9896edb3c96SDavid Ahern 
9900d161581SDavid Ahern 	if (res->nh->fib_nh_lws) {
9910d161581SDavid Ahern 		rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
9926edb3c96SDavid Ahern 		lwtunnel_set_redirect(&rt->dst);
9936edb3c96SDavid Ahern 	}
9946edb3c96SDavid Ahern 
9956edb3c96SDavid Ahern 	rt->dst.lastuse = jiffies;
9966edb3c96SDavid Ahern }
9976edb3c96SDavid Ahern 
998e873e4b9SWei Wang /* Caller must already hold reference to @from */
9998d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1000ae90d867SDavid Ahern {
1001ae90d867SDavid Ahern 	rt->rt6i_flags &= ~RTF_EXPIRES;
1002a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, from);
1003e1255ed4SDavid Ahern 	ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
1004ae90d867SDavid Ahern }
1005ae90d867SDavid Ahern 
10060d161581SDavid Ahern /* Caller must already hold reference to f6i in result */
10070d161581SDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
1008ae90d867SDavid Ahern {
10090d161581SDavid Ahern 	const struct fib6_nh *nh = res->nh;
10100d161581SDavid Ahern 	const struct net_device *dev = nh->fib_nh_dev;
10110d161581SDavid Ahern 	struct fib6_info *f6i = res->f6i;
1012dcd1f572SDavid Ahern 
10130d161581SDavid Ahern 	ip6_rt_init_dst(rt, res);
10146edb3c96SDavid Ahern 
10150d161581SDavid Ahern 	rt->rt6i_dst = f6i->fib6_dst;
1016dcd1f572SDavid Ahern 	rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
10177d21fec9SDavid Ahern 	rt->rt6i_flags = res->fib6_flags;
10180d161581SDavid Ahern 	if (nh->fib_nh_gw_family) {
10190d161581SDavid Ahern 		rt->rt6i_gateway = nh->fib_nh_gw6;
10202b2450caSDavid Ahern 		rt->rt6i_flags |= RTF_GATEWAY;
10212b2450caSDavid Ahern 	}
10220d161581SDavid Ahern 	rt6_set_from(rt, f6i);
1023ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
10240d161581SDavid Ahern 	rt->rt6i_src = f6i->fib6_src;
1025ae90d867SDavid Ahern #endif
1026ae90d867SDavid Ahern }
1027ae90d867SDavid Ahern 
1028a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1029a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
1030a3c00e46SMartin KaFai Lau {
103166f5d6ceSWei Wang 	struct fib6_node *pn, *sn;
1032a3c00e46SMartin KaFai Lau 	while (1) {
1033a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
1034a3c00e46SMartin KaFai Lau 			return NULL;
103566f5d6ceSWei Wang 		pn = rcu_dereference(fn->parent);
103666f5d6ceSWei Wang 		sn = FIB6_SUBTREE(pn);
103766f5d6ceSWei Wang 		if (sn && sn != fn)
10386454743bSDavid Ahern 			fn = fib6_node_lookup(sn, NULL, saddr);
1039a3c00e46SMartin KaFai Lau 		else
1040a3c00e46SMartin KaFai Lau 			fn = pn;
1041a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
1042a3c00e46SMartin KaFai Lau 			return fn;
1043a3c00e46SMartin KaFai Lau 	}
1044a3c00e46SMartin KaFai Lau }
1045c71099acSThomas Graf 
104610585b43SDavid Ahern static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1047d3843fe5SWei Wang {
1048d3843fe5SWei Wang 	struct rt6_info *rt = *prt;
1049d3843fe5SWei Wang 
1050d3843fe5SWei Wang 	if (dst_hold_safe(&rt->dst))
1051d3843fe5SWei Wang 		return true;
105210585b43SDavid Ahern 	if (net) {
1053d3843fe5SWei Wang 		rt = net->ipv6.ip6_null_entry;
1054d3843fe5SWei Wang 		dst_hold(&rt->dst);
1055d3843fe5SWei Wang 	} else {
1056d3843fe5SWei Wang 		rt = NULL;
1057d3843fe5SWei Wang 	}
1058d3843fe5SWei Wang 	*prt = rt;
1059d3843fe5SWei Wang 	return false;
1060d3843fe5SWei Wang }
1061d3843fe5SWei Wang 
1062dec9b0e2SDavid Ahern /* called with rcu_lock held */
10639b6b35abSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
1064dec9b0e2SDavid Ahern {
10659b6b35abSDavid Ahern 	struct net_device *dev = res->nh->fib_nh_dev;
10669b6b35abSDavid Ahern 	struct fib6_info *f6i = res->f6i;
10679b6b35abSDavid Ahern 	unsigned short flags;
1068dec9b0e2SDavid Ahern 	struct rt6_info *nrt;
1069dec9b0e2SDavid Ahern 
10709b6b35abSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
10711c87e79aSXin Long 		goto fallback;
1072e873e4b9SWei Wang 
10739b6b35abSDavid Ahern 	flags = fib6_info_dst_flags(f6i);
107493531c67SDavid Ahern 	nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
10751c87e79aSXin Long 	if (!nrt) {
10769b6b35abSDavid Ahern 		fib6_info_release(f6i);
10771c87e79aSXin Long 		goto fallback;
10781c87e79aSXin Long 	}
1079dec9b0e2SDavid Ahern 
10800d161581SDavid Ahern 	ip6_rt_copy_init(nrt, res);
10811c87e79aSXin Long 	return nrt;
10821c87e79aSXin Long 
10831c87e79aSXin Long fallback:
10841c87e79aSXin Long 	nrt = dev_net(dev)->ipv6.ip6_null_entry;
10851c87e79aSXin Long 	dst_hold(&nrt->dst);
1086dec9b0e2SDavid Ahern 	return nrt;
1087dec9b0e2SDavid Ahern }
1088dec9b0e2SDavid Ahern 
10898ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
10908ed67789SDaniel Lezcano 					     struct fib6_table *table,
1091b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
1092b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
1093b75cc8f9SDavid Ahern 					     int flags)
10941da177e4SLinus Torvalds {
1095b1d40991SDavid Ahern 	struct fib6_result res = {};
10961da177e4SLinus Torvalds 	struct fib6_node *fn;
109723fb93a4SDavid Ahern 	struct rt6_info *rt;
10981da177e4SLinus Torvalds 
1099b6cdbc85SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1100b6cdbc85SDavid Ahern 		flags &= ~RT6_LOOKUP_F_IFACE;
1101b6cdbc85SDavid Ahern 
110266f5d6ceSWei Wang 	rcu_read_lock();
11036454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1104c71099acSThomas Graf restart:
1105b1d40991SDavid Ahern 	res.f6i = rcu_dereference(fn->leaf);
1106b1d40991SDavid Ahern 	if (!res.f6i)
1107b1d40991SDavid Ahern 		res.f6i = net->ipv6.fib6_null_entry;
1108af52a52cSDavid Ahern 	else
110975ef7389SDavid Ahern 		rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
111075ef7389SDavid Ahern 				 flags);
1111af52a52cSDavid Ahern 
1112b1d40991SDavid Ahern 	if (res.f6i == net->ipv6.fib6_null_entry) {
1113a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1114a3c00e46SMartin KaFai Lau 		if (fn)
1115a3c00e46SMartin KaFai Lau 			goto restart;
1116af52a52cSDavid Ahern 
1117af52a52cSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
1118af52a52cSDavid Ahern 		dst_hold(&rt->dst);
1119af52a52cSDavid Ahern 		goto out;
1120a3c00e46SMartin KaFai Lau 	}
11212b760fcfSWei Wang 
1122b1d40991SDavid Ahern 	fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1123b1d40991SDavid Ahern 			 fl6->flowi6_oif != 0, skb, flags);
1124b1d40991SDavid Ahern 
11254c9483b2SDavid S. Miller 	/* Search through exception table */
11267e4b5128SDavid Ahern 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
112723fb93a4SDavid Ahern 	if (rt) {
112810585b43SDavid Ahern 		if (ip6_hold_safe(net, &rt))
1129d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
113023fb93a4SDavid Ahern 	} else {
11319b6b35abSDavid Ahern 		rt = ip6_create_rt_rcu(&res);
1132dec9b0e2SDavid Ahern 	}
1133d3843fe5SWei Wang 
1134af52a52cSDavid Ahern out:
11358ff2e5b2SDavid Ahern 	trace_fib6_table_lookup(net, &res, table, fl6);
1136af52a52cSDavid Ahern 
113766f5d6ceSWei Wang 	rcu_read_unlock();
1138b811580dSDavid Ahern 
11391da177e4SLinus Torvalds 	return rt;
1140c71099acSThomas Graf }
1141c71099acSThomas Graf 
1142ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1143b75cc8f9SDavid Ahern 				   const struct sk_buff *skb, int flags)
1144ea6e574eSFlorian Westphal {
1145b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1146ea6e574eSFlorian Westphal }
1147ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
1148ea6e574eSFlorian Westphal 
11499acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1150b75cc8f9SDavid Ahern 			    const struct in6_addr *saddr, int oif,
1151b75cc8f9SDavid Ahern 			    const struct sk_buff *skb, int strict)
1152c71099acSThomas Graf {
11534c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
11544c9483b2SDavid S. Miller 		.flowi6_oif = oif,
11554c9483b2SDavid S. Miller 		.daddr = *daddr,
1156c71099acSThomas Graf 	};
1157c71099acSThomas Graf 	struct dst_entry *dst;
115877d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1159c71099acSThomas Graf 
1160adaa70bbSThomas Graf 	if (saddr) {
11614c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1162adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1163adaa70bbSThomas Graf 	}
1164adaa70bbSThomas Graf 
1165b75cc8f9SDavid Ahern 	dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1166c71099acSThomas Graf 	if (dst->error == 0)
1167c71099acSThomas Graf 		return (struct rt6_info *) dst;
1168c71099acSThomas Graf 
1169c71099acSThomas Graf 	dst_release(dst);
1170c71099acSThomas Graf 
11711da177e4SLinus Torvalds 	return NULL;
11721da177e4SLinus Torvalds }
11737159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
11747159039aSYOSHIFUJI Hideaki 
1175c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
11761cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
11771cfb71eeSWei Wang  * route is released.
11781cfb71eeSWei Wang  * Caller must hold dst before calling it.
11791da177e4SLinus Torvalds  */
11801da177e4SLinus Torvalds 
11818d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1182333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
11831da177e4SLinus Torvalds {
11841da177e4SLinus Torvalds 	int err;
1185c71099acSThomas Graf 	struct fib6_table *table;
11861da177e4SLinus Torvalds 
118793c2fb25SDavid Ahern 	table = rt->fib6_table;
118866f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
1189d4ead6b3SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, extack);
119066f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	return err;
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds 
11958d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt)
119640e22e8fSThomas Graf {
1197afb1d4b5SDavid Ahern 	struct nl_info info = {	.nl_net = net, };
1198e715b6d3SFlorian Westphal 
1199d4ead6b3SDavid Ahern 	return __ip6_ins_rt(rt, &info, NULL);
120040e22e8fSThomas Graf }
120140e22e8fSThomas Graf 
120285bd05deSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
120321efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
1204b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
12051da177e4SLinus Torvalds {
120685bd05deSDavid Ahern 	struct fib6_info *f6i = res->f6i;
12074832c30dSDavid Ahern 	struct net_device *dev;
12081da177e4SLinus Torvalds 	struct rt6_info *rt;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	/*
12111da177e4SLinus Torvalds 	 *	Clone the route.
12121da177e4SLinus Torvalds 	 */
12131da177e4SLinus Torvalds 
121485bd05deSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
1215e873e4b9SWei Wang 		return NULL;
1216e873e4b9SWei Wang 
12170d161581SDavid Ahern 	dev = ip6_rt_get_dev_rcu(res);
121893531c67SDavid Ahern 	rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1219e873e4b9SWei Wang 	if (!rt) {
122085bd05deSDavid Ahern 		fib6_info_release(f6i);
122183a09abdSMartin KaFai Lau 		return NULL;
1222e873e4b9SWei Wang 	}
122383a09abdSMartin KaFai Lau 
12240d161581SDavid Ahern 	ip6_rt_copy_init(rt, res);
12258b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
122683a09abdSMartin KaFai Lau 	rt->dst.flags |= DST_HOST;
122783a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
122883a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
12298b9df265SMartin KaFai Lau 
123085bd05deSDavid Ahern 	if (!rt6_is_gw_or_nonexthop(res)) {
123185bd05deSDavid Ahern 		if (f6i->fib6_dst.plen != 128 &&
123285bd05deSDavid Ahern 		    ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
123358c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
12341da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
12351da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
12364e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
12371da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
12381da177e4SLinus Torvalds 		}
12391da177e4SLinus Torvalds #endif
124095a9a5baSYOSHIFUJI Hideaki 	}
124195a9a5baSYOSHIFUJI Hideaki 
1242299d9939SYOSHIFUJI Hideaki 	return rt;
1243299d9939SYOSHIFUJI Hideaki }
1244299d9939SYOSHIFUJI Hideaki 
1245db3fedeeSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
1246d52d3997SMartin KaFai Lau {
1247db3fedeeSDavid Ahern 	struct fib6_info *f6i = res->f6i;
1248db3fedeeSDavid Ahern 	unsigned short flags = fib6_info_dst_flags(f6i);
12494832c30dSDavid Ahern 	struct net_device *dev;
1250d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1251d52d3997SMartin KaFai Lau 
1252db3fedeeSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
1253e873e4b9SWei Wang 		return NULL;
1254e873e4b9SWei Wang 
12554832c30dSDavid Ahern 	rcu_read_lock();
12560d161581SDavid Ahern 	dev = ip6_rt_get_dev_rcu(res);
125793531c67SDavid Ahern 	pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
12584832c30dSDavid Ahern 	rcu_read_unlock();
1259e873e4b9SWei Wang 	if (!pcpu_rt) {
1260db3fedeeSDavid Ahern 		fib6_info_release(f6i);
1261d52d3997SMartin KaFai Lau 		return NULL;
1262e873e4b9SWei Wang 	}
12630d161581SDavid Ahern 	ip6_rt_copy_init(pcpu_rt, res);
1264d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
1265d52d3997SMartin KaFai Lau 	return pcpu_rt;
1266d52d3997SMartin KaFai Lau }
1267d52d3997SMartin KaFai Lau 
126866f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */
1269db3fedeeSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
1270d52d3997SMartin KaFai Lau {
1271a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, **p;
1272d52d3997SMartin KaFai Lau 
1273f40b6ae2SDavid Ahern 	p = this_cpu_ptr(res->nh->rt6i_pcpu);
1274d52d3997SMartin KaFai Lau 	pcpu_rt = *p;
1275d52d3997SMartin KaFai Lau 
1276d4ead6b3SDavid Ahern 	if (pcpu_rt)
127710585b43SDavid Ahern 		ip6_hold_safe(NULL, &pcpu_rt);
1278d3843fe5SWei Wang 
1279a73e4195SMartin KaFai Lau 	return pcpu_rt;
1280a73e4195SMartin KaFai Lau }
1281a73e4195SMartin KaFai Lau 
1282afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1283db3fedeeSDavid Ahern 					    const struct fib6_result *res)
1284a73e4195SMartin KaFai Lau {
1285a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1286d52d3997SMartin KaFai Lau 
1287db3fedeeSDavid Ahern 	pcpu_rt = ip6_rt_pcpu_alloc(res);
1288d52d3997SMartin KaFai Lau 	if (!pcpu_rt) {
12899c7370a1SMartin KaFai Lau 		dst_hold(&net->ipv6.ip6_null_entry->dst);
12909c7370a1SMartin KaFai Lau 		return net->ipv6.ip6_null_entry;
1291d52d3997SMartin KaFai Lau 	}
1292d52d3997SMartin KaFai Lau 
1293a94b9367SWei Wang 	dst_hold(&pcpu_rt->dst);
1294f40b6ae2SDavid Ahern 	p = this_cpu_ptr(res->nh->rt6i_pcpu);
1295d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1296951f788aSEric Dumazet 	BUG_ON(prev);
1297a94b9367SWei Wang 
129861fb0d01SEric Dumazet 	if (res->f6i->fib6_destroying) {
129961fb0d01SEric Dumazet 		struct fib6_info *from;
130061fb0d01SEric Dumazet 
130161fb0d01SEric Dumazet 		from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
130261fb0d01SEric Dumazet 		fib6_info_release(from);
130361fb0d01SEric Dumazet 	}
130461fb0d01SEric Dumazet 
1305d52d3997SMartin KaFai Lau 	return pcpu_rt;
1306d52d3997SMartin KaFai Lau }
1307d52d3997SMartin KaFai Lau 
130835732d01SWei Wang /* exception hash table implementation
130935732d01SWei Wang  */
131035732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
131135732d01SWei Wang 
131235732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
131335732d01SWei Wang  * Caller must hold rt6_exception_lock
131435732d01SWei Wang  */
131535732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
131635732d01SWei Wang 				 struct rt6_exception *rt6_ex)
131735732d01SWei Wang {
1318f5b51fe8SPaolo Abeni 	struct fib6_info *from;
1319b2427e67SColin Ian King 	struct net *net;
132081eb8447SWei Wang 
132135732d01SWei Wang 	if (!bucket || !rt6_ex)
132235732d01SWei Wang 		return;
1323b2427e67SColin Ian King 
1324b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
1325f5b51fe8SPaolo Abeni 	net->ipv6.rt6_stats->fib_rt_cache--;
1326f5b51fe8SPaolo Abeni 
1327f5b51fe8SPaolo Abeni 	/* purge completely the exception to allow releasing the held resources:
1328f5b51fe8SPaolo Abeni 	 * some [sk] cache may keep the dst around for unlimited time
1329f5b51fe8SPaolo Abeni 	 */
13300e233874SEric Dumazet 	from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1331f5b51fe8SPaolo Abeni 	fib6_info_release(from);
1332f5b51fe8SPaolo Abeni 	dst_dev_put(&rt6_ex->rt6i->dst);
1333f5b51fe8SPaolo Abeni 
133435732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
133577634cc6SDavid Ahern 	dst_release(&rt6_ex->rt6i->dst);
133635732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
133735732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
133835732d01SWei Wang 	bucket->depth--;
133935732d01SWei Wang }
134035732d01SWei Wang 
134135732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
134235732d01SWei Wang  * Caller must hold rt6_exception_lock
134335732d01SWei Wang  */
134435732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
134535732d01SWei Wang {
134635732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
134735732d01SWei Wang 
134835732d01SWei Wang 	if (!bucket)
134935732d01SWei Wang 		return;
135035732d01SWei Wang 
135135732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
135235732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
135335732d01SWei Wang 			oldest = rt6_ex;
135435732d01SWei Wang 	}
135535732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
135635732d01SWei Wang }
135735732d01SWei Wang 
135835732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
135935732d01SWei Wang 			      const struct in6_addr *src)
136035732d01SWei Wang {
136135732d01SWei Wang 	static u32 seed __read_mostly;
136235732d01SWei Wang 	u32 val;
136335732d01SWei Wang 
136435732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
136535732d01SWei Wang 	val = jhash(dst, sizeof(*dst), seed);
136635732d01SWei Wang 
136735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
136835732d01SWei Wang 	if (src)
136935732d01SWei Wang 		val = jhash(src, sizeof(*src), val);
137035732d01SWei Wang #endif
137135732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
137235732d01SWei Wang }
137335732d01SWei Wang 
137435732d01SWei Wang /* Helper function to find the cached rt in the hash table
137535732d01SWei Wang  * and update bucket pointer to point to the bucket for this
137635732d01SWei Wang  * (daddr, saddr) pair
137735732d01SWei Wang  * Caller must hold rt6_exception_lock
137835732d01SWei Wang  */
137935732d01SWei Wang static struct rt6_exception *
138035732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
138135732d01SWei Wang 			      const struct in6_addr *daddr,
138235732d01SWei Wang 			      const struct in6_addr *saddr)
138335732d01SWei Wang {
138435732d01SWei Wang 	struct rt6_exception *rt6_ex;
138535732d01SWei Wang 	u32 hval;
138635732d01SWei Wang 
138735732d01SWei Wang 	if (!(*bucket) || !daddr)
138835732d01SWei Wang 		return NULL;
138935732d01SWei Wang 
139035732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
139135732d01SWei Wang 	*bucket += hval;
139235732d01SWei Wang 
139335732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
139435732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
139535732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
139635732d01SWei Wang 
139735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
139835732d01SWei Wang 		if (matched && saddr)
139935732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
140035732d01SWei Wang #endif
140135732d01SWei Wang 		if (matched)
140235732d01SWei Wang 			return rt6_ex;
140335732d01SWei Wang 	}
140435732d01SWei Wang 	return NULL;
140535732d01SWei Wang }
140635732d01SWei Wang 
140735732d01SWei Wang /* Helper function to find the cached rt in the hash table
140835732d01SWei Wang  * and update bucket pointer to point to the bucket for this
140935732d01SWei Wang  * (daddr, saddr) pair
141035732d01SWei Wang  * Caller must hold rcu_read_lock()
141135732d01SWei Wang  */
141235732d01SWei Wang static struct rt6_exception *
141335732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
141435732d01SWei Wang 			 const struct in6_addr *daddr,
141535732d01SWei Wang 			 const struct in6_addr *saddr)
141635732d01SWei Wang {
141735732d01SWei Wang 	struct rt6_exception *rt6_ex;
141835732d01SWei Wang 	u32 hval;
141935732d01SWei Wang 
142035732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
142135732d01SWei Wang 
142235732d01SWei Wang 	if (!(*bucket) || !daddr)
142335732d01SWei Wang 		return NULL;
142435732d01SWei Wang 
142535732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
142635732d01SWei Wang 	*bucket += hval;
142735732d01SWei Wang 
142835732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
142935732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
143035732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
143135732d01SWei Wang 
143235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
143335732d01SWei Wang 		if (matched && saddr)
143435732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
143535732d01SWei Wang #endif
143635732d01SWei Wang 		if (matched)
143735732d01SWei Wang 			return rt6_ex;
143835732d01SWei Wang 	}
143935732d01SWei Wang 	return NULL;
144035732d01SWei Wang }
144135732d01SWei Wang 
1442b748f260SDavid Ahern static unsigned int fib6_mtu(const struct fib6_result *res)
144335732d01SWei Wang {
1444b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
1445d4ead6b3SDavid Ahern 	unsigned int mtu;
1446d4ead6b3SDavid Ahern 
1447b748f260SDavid Ahern 	if (res->f6i->fib6_pmtu) {
1448b748f260SDavid Ahern 		mtu = res->f6i->fib6_pmtu;
1449dcd1f572SDavid Ahern 	} else {
1450b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
1451dcd1f572SDavid Ahern 		struct inet6_dev *idev;
1452dcd1f572SDavid Ahern 
1453dcd1f572SDavid Ahern 		rcu_read_lock();
1454dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
1455dcd1f572SDavid Ahern 		mtu = idev->cnf.mtu6;
1456dcd1f572SDavid Ahern 		rcu_read_unlock();
1457dcd1f572SDavid Ahern 	}
1458dcd1f572SDavid Ahern 
1459d4ead6b3SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1460d4ead6b3SDavid Ahern 
1461b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1462d4ead6b3SDavid Ahern }
1463d4ead6b3SDavid Ahern 
1464cc5c073aSDavid Ahern #define FIB6_EXCEPTION_BUCKET_FLUSHED  0x1UL
1465cc5c073aSDavid Ahern 
1466cc5c073aSDavid Ahern /* used when the flushed bit is not relevant, only access to the bucket
1467cc5c073aSDavid Ahern  * (ie., all bucket users except rt6_insert_exception);
1468cc5c073aSDavid Ahern  *
1469cc5c073aSDavid Ahern  * called under rcu lock; sometimes called with rt6_exception_lock held
1470cc5c073aSDavid Ahern  */
1471cc5c073aSDavid Ahern static
1472cc5c073aSDavid Ahern struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1473cc5c073aSDavid Ahern 						       spinlock_t *lock)
1474cc5c073aSDavid Ahern {
1475cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
1476cc5c073aSDavid Ahern 
1477cc5c073aSDavid Ahern 	if (lock)
1478cc5c073aSDavid Ahern 		bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1479cc5c073aSDavid Ahern 						   lockdep_is_held(lock));
1480cc5c073aSDavid Ahern 	else
1481cc5c073aSDavid Ahern 		bucket = rcu_dereference(nh->rt6i_exception_bucket);
1482cc5c073aSDavid Ahern 
1483cc5c073aSDavid Ahern 	/* remove bucket flushed bit if set */
1484cc5c073aSDavid Ahern 	if (bucket) {
1485cc5c073aSDavid Ahern 		unsigned long p = (unsigned long)bucket;
1486cc5c073aSDavid Ahern 
1487cc5c073aSDavid Ahern 		p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1488cc5c073aSDavid Ahern 		bucket = (struct rt6_exception_bucket *)p;
1489cc5c073aSDavid Ahern 	}
1490cc5c073aSDavid Ahern 
1491cc5c073aSDavid Ahern 	return bucket;
1492cc5c073aSDavid Ahern }
1493cc5c073aSDavid Ahern 
1494cc5c073aSDavid Ahern static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1495cc5c073aSDavid Ahern {
1496cc5c073aSDavid Ahern 	unsigned long p = (unsigned long)bucket;
1497cc5c073aSDavid Ahern 
1498cc5c073aSDavid Ahern 	return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1499cc5c073aSDavid Ahern }
1500cc5c073aSDavid Ahern 
1501cc5c073aSDavid Ahern /* called with rt6_exception_lock held */
1502cc5c073aSDavid Ahern static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1503cc5c073aSDavid Ahern 					      spinlock_t *lock)
1504cc5c073aSDavid Ahern {
1505cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
1506cc5c073aSDavid Ahern 	unsigned long p;
1507cc5c073aSDavid Ahern 
1508cc5c073aSDavid Ahern 	bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1509cc5c073aSDavid Ahern 					   lockdep_is_held(lock));
1510cc5c073aSDavid Ahern 
1511cc5c073aSDavid Ahern 	p = (unsigned long)bucket;
1512cc5c073aSDavid Ahern 	p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1513cc5c073aSDavid Ahern 	bucket = (struct rt6_exception_bucket *)p;
1514cc5c073aSDavid Ahern 	rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1515cc5c073aSDavid Ahern }
1516cc5c073aSDavid Ahern 
151735732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
15185012f0a5SDavid Ahern 				const struct fib6_result *res)
151935732d01SWei Wang {
15205e670d84SDavid Ahern 	struct net *net = dev_net(nrt->dst.dev);
152135732d01SWei Wang 	struct rt6_exception_bucket *bucket;
1522cc5c073aSDavid Ahern 	struct fib6_info *f6i = res->f6i;
152335732d01SWei Wang 	struct in6_addr *src_key = NULL;
152435732d01SWei Wang 	struct rt6_exception *rt6_ex;
1525cc5c073aSDavid Ahern 	struct fib6_nh *nh = res->nh;
152635732d01SWei Wang 	int err = 0;
152735732d01SWei Wang 
152835732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
152935732d01SWei Wang 
1530cc5c073aSDavid Ahern 	bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
153135732d01SWei Wang 					  lockdep_is_held(&rt6_exception_lock));
153235732d01SWei Wang 	if (!bucket) {
153335732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
153435732d01SWei Wang 				 GFP_ATOMIC);
153535732d01SWei Wang 		if (!bucket) {
153635732d01SWei Wang 			err = -ENOMEM;
153735732d01SWei Wang 			goto out;
153835732d01SWei Wang 		}
1539cc5c073aSDavid Ahern 		rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1540cc5c073aSDavid Ahern 	} else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1541cc5c073aSDavid Ahern 		err = -EINVAL;
1542cc5c073aSDavid Ahern 		goto out;
154335732d01SWei Wang 	}
154435732d01SWei Wang 
154535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
15465012f0a5SDavid Ahern 	/* fib6_src.plen != 0 indicates f6i is in subtree
154735732d01SWei Wang 	 * and exception table is indexed by a hash of
15485012f0a5SDavid Ahern 	 * both fib6_dst and fib6_src.
154935732d01SWei Wang 	 * Otherwise, the exception table is indexed by
15505012f0a5SDavid Ahern 	 * a hash of only fib6_dst.
155135732d01SWei Wang 	 */
15525012f0a5SDavid Ahern 	if (f6i->fib6_src.plen)
155335732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
155435732d01SWei Wang #endif
15555012f0a5SDavid Ahern 	/* rt6_mtu_change() might lower mtu on f6i.
1556f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
15575012f0a5SDavid Ahern 	 * is less than f6i's mtu value.
1558f5bbe7eeSWei Wang 	 */
1559b748f260SDavid Ahern 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1560f5bbe7eeSWei Wang 		err = -EINVAL;
1561f5bbe7eeSWei Wang 		goto out;
1562f5bbe7eeSWei Wang 	}
156360006a48SWei Wang 
156435732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
156535732d01SWei Wang 					       src_key);
156635732d01SWei Wang 	if (rt6_ex)
156735732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
156835732d01SWei Wang 
156935732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
157035732d01SWei Wang 	if (!rt6_ex) {
157135732d01SWei Wang 		err = -ENOMEM;
157235732d01SWei Wang 		goto out;
157335732d01SWei Wang 	}
157435732d01SWei Wang 	rt6_ex->rt6i = nrt;
157535732d01SWei Wang 	rt6_ex->stamp = jiffies;
157635732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
157735732d01SWei Wang 	bucket->depth++;
157881eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
157935732d01SWei Wang 
158035732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
158135732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
158235732d01SWei Wang 
158335732d01SWei Wang out:
158435732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
158535732d01SWei Wang 
158635732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1587b886d5f2SPaolo Abeni 	if (!err) {
15885012f0a5SDavid Ahern 		spin_lock_bh(&f6i->fib6_table->tb6_lock);
15895012f0a5SDavid Ahern 		fib6_update_sernum(net, f6i);
15905012f0a5SDavid Ahern 		spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1591b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1592b886d5f2SPaolo Abeni 	}
159335732d01SWei Wang 
159435732d01SWei Wang 	return err;
159535732d01SWei Wang }
159635732d01SWei Wang 
1597c0b220cfSDavid Ahern static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
159835732d01SWei Wang {
159935732d01SWei Wang 	struct rt6_exception_bucket *bucket;
160035732d01SWei Wang 	struct rt6_exception *rt6_ex;
160135732d01SWei Wang 	struct hlist_node *tmp;
160235732d01SWei Wang 	int i;
160335732d01SWei Wang 
160435732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
160535732d01SWei Wang 
1606cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
160735732d01SWei Wang 	if (!bucket)
160835732d01SWei Wang 		goto out;
160935732d01SWei Wang 
1610cc5c073aSDavid Ahern 	/* Prevent rt6_insert_exception() to recreate the bucket list */
1611cc5c073aSDavid Ahern 	if (!from)
1612cc5c073aSDavid Ahern 		fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1613cc5c073aSDavid Ahern 
161435732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1615cc5c073aSDavid Ahern 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1616cc5c073aSDavid Ahern 			if (!from ||
1617cc5c073aSDavid Ahern 			    rcu_access_pointer(rt6_ex->rt6i->from) == from)
161835732d01SWei Wang 				rt6_remove_exception(bucket, rt6_ex);
1619cc5c073aSDavid Ahern 		}
1620cc5c073aSDavid Ahern 		WARN_ON_ONCE(!from && bucket->depth);
162135732d01SWei Wang 		bucket++;
162235732d01SWei Wang 	}
162335732d01SWei Wang out:
162435732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
162535732d01SWei Wang }
162635732d01SWei Wang 
1627c0b220cfSDavid Ahern void rt6_flush_exceptions(struct fib6_info *f6i)
1628c0b220cfSDavid Ahern {
16291cf844c7SDavid Ahern 	fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1630c0b220cfSDavid Ahern }
1631c0b220cfSDavid Ahern 
163235732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
163335732d01SWei Wang  * Caller has to hold rcu_read_lock()
163435732d01SWei Wang  */
16357e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1636510e2cedSWei Wang 					   const struct in6_addr *daddr,
1637510e2cedSWei Wang 					   const struct in6_addr *saddr)
163835732d01SWei Wang {
1639510e2cedSWei Wang 	const struct in6_addr *src_key = NULL;
164035732d01SWei Wang 	struct rt6_exception_bucket *bucket;
164135732d01SWei Wang 	struct rt6_exception *rt6_ex;
16427e4b5128SDavid Ahern 	struct rt6_info *ret = NULL;
164335732d01SWei Wang 
164435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
16457e4b5128SDavid Ahern 	/* fib6i_src.plen != 0 indicates f6i is in subtree
164635732d01SWei Wang 	 * and exception table is indexed by a hash of
16477e4b5128SDavid Ahern 	 * both fib6_dst and fib6_src.
1648510e2cedSWei Wang 	 * However, the src addr used to create the hash
1649510e2cedSWei Wang 	 * might not be exactly the passed in saddr which
1650510e2cedSWei Wang 	 * is a /128 addr from the flow.
1651510e2cedSWei Wang 	 * So we need to use f6i->fib6_src to redo lookup
1652510e2cedSWei Wang 	 * if the passed in saddr does not find anything.
1653510e2cedSWei Wang 	 * (See the logic in ip6_rt_cache_alloc() on how
1654510e2cedSWei Wang 	 * rt->rt6i_src is updated.)
165535732d01SWei Wang 	 */
16567e4b5128SDavid Ahern 	if (res->f6i->fib6_src.plen)
165735732d01SWei Wang 		src_key = saddr;
1658510e2cedSWei Wang find_ex:
165935732d01SWei Wang #endif
1660cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
166135732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
166235732d01SWei Wang 
166335732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
16647e4b5128SDavid Ahern 		ret = rt6_ex->rt6i;
166535732d01SWei Wang 
1666510e2cedSWei Wang #ifdef CONFIG_IPV6_SUBTREES
1667510e2cedSWei Wang 	/* Use fib6_src as src_key and redo lookup */
1668510e2cedSWei Wang 	if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1669510e2cedSWei Wang 		src_key = &res->f6i->fib6_src.addr;
1670510e2cedSWei Wang 		goto find_ex;
1671510e2cedSWei Wang 	}
1672510e2cedSWei Wang #endif
1673510e2cedSWei Wang 
16747e4b5128SDavid Ahern 	return ret;
167535732d01SWei Wang }
167635732d01SWei Wang 
167735732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
1678cc5c073aSDavid Ahern static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1679c0b220cfSDavid Ahern 				    const struct rt6_info *rt)
168035732d01SWei Wang {
1681c0b220cfSDavid Ahern 	const struct in6_addr *src_key = NULL;
168235732d01SWei Wang 	struct rt6_exception_bucket *bucket;
168335732d01SWei Wang 	struct rt6_exception *rt6_ex;
168435732d01SWei Wang 	int err;
168535732d01SWei Wang 
1686cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
168735732d01SWei Wang 		return -ENOENT;
168835732d01SWei Wang 
168935732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1690cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1691cc5c073aSDavid Ahern 
169235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
1693cc5c073aSDavid Ahern 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
1694cc5c073aSDavid Ahern 	 * and exception table is indexed by a hash of
1695cc5c073aSDavid Ahern 	 * both rt6i_dst and rt6i_src.
169635732d01SWei Wang 	 * Otherwise, the exception table is indexed by
169735732d01SWei Wang 	 * a hash of only rt6i_dst.
169835732d01SWei Wang 	 */
1699c0b220cfSDavid Ahern 	if (plen)
170035732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
170135732d01SWei Wang #endif
170235732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
170335732d01SWei Wang 					       &rt->rt6i_dst.addr,
170435732d01SWei Wang 					       src_key);
170535732d01SWei Wang 	if (rt6_ex) {
170635732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
170735732d01SWei Wang 		err = 0;
170835732d01SWei Wang 	} else {
170935732d01SWei Wang 		err = -ENOENT;
171035732d01SWei Wang 	}
171135732d01SWei Wang 
171235732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
171335732d01SWei Wang 	return err;
171435732d01SWei Wang }
171535732d01SWei Wang 
1716c0b220cfSDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt)
1717c0b220cfSDavid Ahern {
1718c0b220cfSDavid Ahern 	struct fib6_info *from;
1719c0b220cfSDavid Ahern 
1720c0b220cfSDavid Ahern 	from = rcu_dereference(rt->from);
1721cc5c073aSDavid Ahern 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1722c0b220cfSDavid Ahern 		return -EINVAL;
1723c0b220cfSDavid Ahern 
17241cf844c7SDavid Ahern 	return fib6_nh_remove_exception(from->fib6_nh,
1725cc5c073aSDavid Ahern 					from->fib6_src.plen, rt);
1726c0b220cfSDavid Ahern }
1727c0b220cfSDavid Ahern 
172835732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
172935732d01SWei Wang  * refresh its stamp
173035732d01SWei Wang  */
1731cc5c073aSDavid Ahern static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1732c0b220cfSDavid Ahern 				     const struct rt6_info *rt)
173335732d01SWei Wang {
1734c0b220cfSDavid Ahern 	const struct in6_addr *src_key = NULL;
173535732d01SWei Wang 	struct rt6_exception_bucket *bucket;
173635732d01SWei Wang 	struct rt6_exception *rt6_ex;
1737193f3685SPaolo Abeni 
1738cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, NULL);
173935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
1740cc5c073aSDavid Ahern 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
1741cc5c073aSDavid Ahern 	 * and exception table is indexed by a hash of
1742cc5c073aSDavid Ahern 	 * both rt6i_dst and rt6i_src.
174335732d01SWei Wang 	 * Otherwise, the exception table is indexed by
174435732d01SWei Wang 	 * a hash of only rt6i_dst.
174535732d01SWei Wang 	 */
1746c0b220cfSDavid Ahern 	if (plen)
174735732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
174835732d01SWei Wang #endif
1749cc5c073aSDavid Ahern 	rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
175035732d01SWei Wang 	if (rt6_ex)
175135732d01SWei Wang 		rt6_ex->stamp = jiffies;
1752c0b220cfSDavid Ahern }
175335732d01SWei Wang 
1754c0b220cfSDavid Ahern static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1755c0b220cfSDavid Ahern {
1756c0b220cfSDavid Ahern 	struct fib6_info *from;
1757c0b220cfSDavid Ahern 
1758c0b220cfSDavid Ahern 	rcu_read_lock();
1759c0b220cfSDavid Ahern 
1760c0b220cfSDavid Ahern 	from = rcu_dereference(rt->from);
1761c0b220cfSDavid Ahern 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1762c0b220cfSDavid Ahern 		goto unlock;
1763c0b220cfSDavid Ahern 
17641cf844c7SDavid Ahern 	fib6_nh_update_exception(from->fib6_nh, from->fib6_src.plen, rt);
1765193f3685SPaolo Abeni unlock:
176635732d01SWei Wang 	rcu_read_unlock();
176735732d01SWei Wang }
176835732d01SWei Wang 
1769e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1770e9fa1495SStefano Brivio 					 struct rt6_info *rt, int mtu)
1771e9fa1495SStefano Brivio {
1772e9fa1495SStefano Brivio 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1773e9fa1495SStefano Brivio 	 * lowest MTU in the path: always allow updating the route PMTU to
1774e9fa1495SStefano Brivio 	 * reflect PMTU decreases.
1775e9fa1495SStefano Brivio 	 *
1776e9fa1495SStefano Brivio 	 * If the new MTU is higher, and the route PMTU is equal to the local
1777e9fa1495SStefano Brivio 	 * MTU, this means the old MTU is the lowest in the path, so allow
1778e9fa1495SStefano Brivio 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1779e9fa1495SStefano Brivio 	 * handle this.
1780e9fa1495SStefano Brivio 	 */
1781e9fa1495SStefano Brivio 
1782e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) >= mtu)
1783e9fa1495SStefano Brivio 		return true;
1784e9fa1495SStefano Brivio 
1785e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1786e9fa1495SStefano Brivio 		return true;
1787e9fa1495SStefano Brivio 
1788e9fa1495SStefano Brivio 	return false;
1789e9fa1495SStefano Brivio }
1790e9fa1495SStefano Brivio 
1791e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
1792cc5c073aSDavid Ahern 				       const struct fib6_nh *nh, int mtu)
1793f5bbe7eeSWei Wang {
1794f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
1795f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
1796f5bbe7eeSWei Wang 	int i;
1797f5bbe7eeSWei Wang 
1798cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1799e9fa1495SStefano Brivio 	if (!bucket)
1800e9fa1495SStefano Brivio 		return;
1801e9fa1495SStefano Brivio 
1802f5bbe7eeSWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1803f5bbe7eeSWei Wang 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1804f5bbe7eeSWei Wang 			struct rt6_info *entry = rt6_ex->rt6i;
1805e9fa1495SStefano Brivio 
1806e9fa1495SStefano Brivio 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
1807d4ead6b3SDavid Ahern 			 * route), the metrics of its rt->from have already
1808f5bbe7eeSWei Wang 			 * been updated.
1809f5bbe7eeSWei Wang 			 */
1810d4ead6b3SDavid Ahern 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
1811e9fa1495SStefano Brivio 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
1812d4ead6b3SDavid Ahern 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
1813f5bbe7eeSWei Wang 		}
1814f5bbe7eeSWei Wang 		bucket++;
1815f5bbe7eeSWei Wang 	}
1816f5bbe7eeSWei Wang }
1817f5bbe7eeSWei Wang 
1818b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
1819b16cb459SWei Wang 
1820cc5c073aSDavid Ahern static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
1821cc5c073aSDavid Ahern 					    const struct in6_addr *gateway)
1822b16cb459SWei Wang {
1823b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
1824b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
1825b16cb459SWei Wang 	struct hlist_node *tmp;
1826b16cb459SWei Wang 	int i;
1827b16cb459SWei Wang 
1828cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
1829b16cb459SWei Wang 		return;
1830b16cb459SWei Wang 
1831b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1832cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1833b16cb459SWei Wang 	if (bucket) {
1834b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1835b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1836b16cb459SWei Wang 						  &bucket->chain, hlist) {
1837b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1838b16cb459SWei Wang 
1839b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1840b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
1841b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
1842b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
1843b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
1844b16cb459SWei Wang 				}
1845b16cb459SWei Wang 			}
1846b16cb459SWei Wang 			bucket++;
1847b16cb459SWei Wang 		}
1848b16cb459SWei Wang 	}
1849b16cb459SWei Wang 
1850b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1851b16cb459SWei Wang }
1852b16cb459SWei Wang 
1853c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1854c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
1855c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
1856c757faa8SWei Wang 				      unsigned long now)
1857c757faa8SWei Wang {
1858c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
1859c757faa8SWei Wang 
18601859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
18611859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
18621859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
18631859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
18641859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
18651859bac0SPaolo Abeni 	 */
186631afeb42SWei Wang 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
186731afeb42SWei Wang 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1868c757faa8SWei Wang 			RT6_TRACE("aging clone %p\n", rt);
1869c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1870c757faa8SWei Wang 			return;
187131afeb42SWei Wang 		}
187231afeb42SWei Wang 	} else if (time_after(jiffies, rt->dst.expires)) {
187331afeb42SWei Wang 		RT6_TRACE("purging expired route %p\n", rt);
187431afeb42SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
187531afeb42SWei Wang 		return;
187631afeb42SWei Wang 	}
187731afeb42SWei Wang 
187831afeb42SWei Wang 	if (rt->rt6i_flags & RTF_GATEWAY) {
1879c757faa8SWei Wang 		struct neighbour *neigh;
1880c757faa8SWei Wang 		__u8 neigh_flags = 0;
1881c757faa8SWei Wang 
18821bfa26ffSEric Dumazet 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
18831bfa26ffSEric Dumazet 		if (neigh)
1884c757faa8SWei Wang 			neigh_flags = neigh->flags;
18851bfa26ffSEric Dumazet 
1886c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
1887c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
1888c757faa8SWei Wang 				  rt);
1889c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1890c757faa8SWei Wang 			return;
1891c757faa8SWei Wang 		}
1892c757faa8SWei Wang 	}
189331afeb42SWei Wang 
1894c757faa8SWei Wang 	gc_args->more++;
1895c757faa8SWei Wang }
1896c757faa8SWei Wang 
1897cc5c073aSDavid Ahern static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
1898c757faa8SWei Wang 				   struct fib6_gc_args *gc_args,
1899c757faa8SWei Wang 				   unsigned long now)
1900c757faa8SWei Wang {
1901c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
1902c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
1903c757faa8SWei Wang 	struct hlist_node *tmp;
1904c757faa8SWei Wang 	int i;
1905c757faa8SWei Wang 
1906cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
1907c757faa8SWei Wang 		return;
1908c757faa8SWei Wang 
19091bfa26ffSEric Dumazet 	rcu_read_lock_bh();
19101bfa26ffSEric Dumazet 	spin_lock(&rt6_exception_lock);
1911cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1912c757faa8SWei Wang 	if (bucket) {
1913c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1914c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1915c757faa8SWei Wang 						  &bucket->chain, hlist) {
1916c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
1917c757faa8SWei Wang 							  gc_args, now);
1918c757faa8SWei Wang 			}
1919c757faa8SWei Wang 			bucket++;
1920c757faa8SWei Wang 		}
1921c757faa8SWei Wang 	}
19221bfa26ffSEric Dumazet 	spin_unlock(&rt6_exception_lock);
19231bfa26ffSEric Dumazet 	rcu_read_unlock_bh();
1924c757faa8SWei Wang }
1925c757faa8SWei Wang 
1926cc5c073aSDavid Ahern void rt6_age_exceptions(struct fib6_info *f6i,
1927c0b220cfSDavid Ahern 			struct fib6_gc_args *gc_args,
1928c0b220cfSDavid Ahern 			unsigned long now)
1929c0b220cfSDavid Ahern {
19301cf844c7SDavid Ahern 	fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
1931c0b220cfSDavid Ahern }
1932c0b220cfSDavid Ahern 
19331d053da9SDavid Ahern /* must be called with rcu lock held */
1934effda4ddSDavid Ahern int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
1935effda4ddSDavid Ahern 		      struct flowi6 *fl6, struct fib6_result *res, int strict)
19361da177e4SLinus Torvalds {
1937367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
19381da177e4SLinus Torvalds 
19396454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1940367efcb9SMartin KaFai Lau 	saved_fn = fn;
19411da177e4SLinus Torvalds 
1942ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1943ca254490SDavid Ahern 		oif = 0;
1944ca254490SDavid Ahern 
1945a3c00e46SMartin KaFai Lau redo_rt6_select:
1946effda4ddSDavid Ahern 	rt6_select(net, fn, oif, res, strict);
1947effda4ddSDavid Ahern 	if (res->f6i == net->ipv6.fib6_null_entry) {
1948a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1949a3c00e46SMartin KaFai Lau 		if (fn)
1950a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
1951367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
1952367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
1953367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
1954367efcb9SMartin KaFai Lau 			fn = saved_fn;
1955367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
1956367efcb9SMartin KaFai Lau 		}
1957a3c00e46SMartin KaFai Lau 	}
1958a3c00e46SMartin KaFai Lau 
1959effda4ddSDavid Ahern 	trace_fib6_table_lookup(net, res, table, fl6);
1960d52d3997SMartin KaFai Lau 
1961effda4ddSDavid Ahern 	return 0;
19621d053da9SDavid Ahern }
19631d053da9SDavid Ahern 
19641d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
19651d053da9SDavid Ahern 			       int oif, struct flowi6 *fl6,
19661d053da9SDavid Ahern 			       const struct sk_buff *skb, int flags)
19671d053da9SDavid Ahern {
1968b1d40991SDavid Ahern 	struct fib6_result res = {};
19691d053da9SDavid Ahern 	struct rt6_info *rt;
19701d053da9SDavid Ahern 	int strict = 0;
19711d053da9SDavid Ahern 
19721d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IFACE;
19731d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
19741d053da9SDavid Ahern 	if (net->ipv6.devconf_all->forwarding == 0)
19751d053da9SDavid Ahern 		strict |= RT6_LOOKUP_F_REACHABLE;
19761d053da9SDavid Ahern 
19771d053da9SDavid Ahern 	rcu_read_lock();
19781d053da9SDavid Ahern 
1979effda4ddSDavid Ahern 	fib6_table_lookup(net, table, oif, fl6, &res, strict);
1980b1d40991SDavid Ahern 	if (res.f6i == net->ipv6.fib6_null_entry) {
1981421842edSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
198266f5d6ceSWei Wang 		rcu_read_unlock();
1983d3843fe5SWei Wang 		dst_hold(&rt->dst);
1984d3843fe5SWei Wang 		return rt;
1985d3843fe5SWei Wang 	}
198623fb93a4SDavid Ahern 
1987b1d40991SDavid Ahern 	fib6_select_path(net, &res, fl6, oif, false, skb, strict);
1988d83009d4SDavid Ahern 
198923fb93a4SDavid Ahern 	/*Search through exception table */
19907e4b5128SDavid Ahern 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
199123fb93a4SDavid Ahern 	if (rt) {
199210585b43SDavid Ahern 		if (ip6_hold_safe(net, &rt))
19931da177e4SLinus Torvalds 			dst_use_noref(&rt->dst, jiffies);
1994d4ead6b3SDavid Ahern 
199566f5d6ceSWei Wang 		rcu_read_unlock();
1996d52d3997SMartin KaFai Lau 		return rt;
19973da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
1998b1d40991SDavid Ahern 			    !res.nh->fib_nh_gw_family)) {
19993da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
20003da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
20013da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
20023da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
20033da59bd9SMartin KaFai Lau 		 */
20043da59bd9SMartin KaFai Lau 		struct rt6_info *uncached_rt;
20053da59bd9SMartin KaFai Lau 
200685bd05deSDavid Ahern 		uncached_rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
2007d52d3997SMartin KaFai Lau 
20084d85cd0cSDavid Ahern 		rcu_read_unlock();
20093da59bd9SMartin KaFai Lau 
20101cfb71eeSWei Wang 		if (uncached_rt) {
20111cfb71eeSWei Wang 			/* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
20121cfb71eeSWei Wang 			 * No need for another dst_hold()
20131cfb71eeSWei Wang 			 */
20148d0b94afSMartin KaFai Lau 			rt6_uncached_list_add(uncached_rt);
201581eb8447SWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
20161cfb71eeSWei Wang 		} else {
20173da59bd9SMartin KaFai Lau 			uncached_rt = net->ipv6.ip6_null_entry;
20183da59bd9SMartin KaFai Lau 			dst_hold(&uncached_rt->dst);
20191cfb71eeSWei Wang 		}
2020b811580dSDavid Ahern 
20213da59bd9SMartin KaFai Lau 		return uncached_rt;
2022d52d3997SMartin KaFai Lau 	} else {
2023d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
2024d52d3997SMartin KaFai Lau 
2025d52d3997SMartin KaFai Lau 		struct rt6_info *pcpu_rt;
2026d52d3997SMartin KaFai Lau 
2027951f788aSEric Dumazet 		local_bh_disable();
2028db3fedeeSDavid Ahern 		pcpu_rt = rt6_get_pcpu_route(&res);
2029d52d3997SMartin KaFai Lau 
203093531c67SDavid Ahern 		if (!pcpu_rt)
2031db3fedeeSDavid Ahern 			pcpu_rt = rt6_make_pcpu_route(net, &res);
203293531c67SDavid Ahern 
2033951f788aSEric Dumazet 		local_bh_enable();
2034951f788aSEric Dumazet 		rcu_read_unlock();
2035d4bea421SDavid Ahern 
2036d52d3997SMartin KaFai Lau 		return pcpu_rt;
2037d52d3997SMartin KaFai Lau 	}
2038c71099acSThomas Graf }
20399ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
2040c71099acSThomas Graf 
2041b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net,
2042b75cc8f9SDavid Ahern 					    struct fib6_table *table,
2043b75cc8f9SDavid Ahern 					    struct flowi6 *fl6,
2044b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2045b75cc8f9SDavid Ahern 					    int flags)
20464acad72dSPavel Emelyanov {
2047b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
20484acad72dSPavel Emelyanov }
20494acad72dSPavel Emelyanov 
2050d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
205172331bc0SShmulik Ladkani 					 struct net_device *dev,
2052b75cc8f9SDavid Ahern 					 struct flowi6 *fl6,
2053b75cc8f9SDavid Ahern 					 const struct sk_buff *skb,
2054b75cc8f9SDavid Ahern 					 int flags)
205572331bc0SShmulik Ladkani {
205672331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
205772331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
205872331bc0SShmulik Ladkani 
2059b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
206072331bc0SShmulik Ladkani }
2061d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
206272331bc0SShmulik Ladkani 
206323aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
20645e5d6fedSRoopa Prabhu 				  struct flow_keys *keys,
20655e5d6fedSRoopa Prabhu 				  struct flow_keys *flkeys)
206623aebdacSJakub Sitnicki {
206723aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
206823aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
20695e5d6fedSRoopa Prabhu 	struct flow_keys *_flkeys = flkeys;
207023aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
207123aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
207223aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
2073cea67a2dSEric Dumazet 	struct icmp6hdr _icmph;
207423aebdacSJakub Sitnicki 
207523aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
207623aebdacSJakub Sitnicki 		goto out;
207723aebdacSJakub Sitnicki 
2078cea67a2dSEric Dumazet 	icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2079cea67a2dSEric Dumazet 				   sizeof(_icmph), &_icmph);
2080cea67a2dSEric Dumazet 	if (!icmph)
2081cea67a2dSEric Dumazet 		goto out;
2082cea67a2dSEric Dumazet 
208323aebdacSJakub Sitnicki 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
208423aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
208523aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
208623aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
208723aebdacSJakub Sitnicki 		goto out;
208823aebdacSJakub Sitnicki 
208923aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
209023aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
209123aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
209223aebdacSJakub Sitnicki 	if (!inner_iph)
209323aebdacSJakub Sitnicki 		goto out;
209423aebdacSJakub Sitnicki 
209523aebdacSJakub Sitnicki 	key_iph = inner_iph;
20965e5d6fedSRoopa Prabhu 	_flkeys = NULL;
209723aebdacSJakub Sitnicki out:
20985e5d6fedSRoopa Prabhu 	if (_flkeys) {
20995e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
21005e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
21015e5d6fedSRoopa Prabhu 		keys->tags.flow_label = _flkeys->tags.flow_label;
21025e5d6fedSRoopa Prabhu 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
21035e5d6fedSRoopa Prabhu 	} else {
210423aebdacSJakub Sitnicki 		keys->addrs.v6addrs.src = key_iph->saddr;
210523aebdacSJakub Sitnicki 		keys->addrs.v6addrs.dst = key_iph->daddr;
2106fa1be7e0SMichal Kubecek 		keys->tags.flow_label = ip6_flowlabel(key_iph);
210723aebdacSJakub Sitnicki 		keys->basic.ip_proto = key_iph->nexthdr;
210823aebdacSJakub Sitnicki 	}
21095e5d6fedSRoopa Prabhu }
211023aebdacSJakub Sitnicki 
211123aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
2112b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2113b4bac172SDavid Ahern 		       const struct sk_buff *skb, struct flow_keys *flkeys)
211423aebdacSJakub Sitnicki {
211523aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
21169a2a537aSDavid Ahern 	u32 mhash;
211723aebdacSJakub Sitnicki 
2118bbfa047aSDavid S. Miller 	switch (ip6_multipath_hash_policy(net)) {
2119b4bac172SDavid Ahern 	case 0:
21206f74b6c2SDavid Ahern 		memset(&hash_keys, 0, sizeof(hash_keys));
21216f74b6c2SDavid Ahern 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
21229a2a537aSDavid Ahern 		if (skb) {
21235e5d6fedSRoopa Prabhu 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
21249a2a537aSDavid Ahern 		} else {
21259a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
21269a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2127fa1be7e0SMichal Kubecek 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
21289a2a537aSDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
212923aebdacSJakub Sitnicki 		}
2130b4bac172SDavid Ahern 		break;
2131b4bac172SDavid Ahern 	case 1:
2132b4bac172SDavid Ahern 		if (skb) {
2133b4bac172SDavid Ahern 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2134b4bac172SDavid Ahern 			struct flow_keys keys;
2135b4bac172SDavid Ahern 
2136b4bac172SDavid Ahern 			/* short-circuit if we already have L4 hash present */
2137b4bac172SDavid Ahern 			if (skb->l4_hash)
2138b4bac172SDavid Ahern 				return skb_get_hash_raw(skb) >> 1;
2139b4bac172SDavid Ahern 
2140b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2141b4bac172SDavid Ahern 
2142b4bac172SDavid Ahern                         if (!flkeys) {
2143b4bac172SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2144b4bac172SDavid Ahern 				flkeys = &keys;
2145b4bac172SDavid Ahern 			}
2146b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2147b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2148b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2149b4bac172SDavid Ahern 			hash_keys.ports.src = flkeys->ports.src;
2150b4bac172SDavid Ahern 			hash_keys.ports.dst = flkeys->ports.dst;
2151b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2152b4bac172SDavid Ahern 		} else {
2153b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2154b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2155b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2156b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2157b4bac172SDavid Ahern 			hash_keys.ports.src = fl6->fl6_sport;
2158b4bac172SDavid Ahern 			hash_keys.ports.dst = fl6->fl6_dport;
2159b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2160b4bac172SDavid Ahern 		}
2161b4bac172SDavid Ahern 		break;
2162b4bac172SDavid Ahern 	}
21639a2a537aSDavid Ahern 	mhash = flow_hash_from_keys(&hash_keys);
216423aebdacSJakub Sitnicki 
21659a2a537aSDavid Ahern 	return mhash >> 1;
216623aebdacSJakub Sitnicki }
216723aebdacSJakub Sitnicki 
2168c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
2169c71099acSThomas Graf {
2170b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2171c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
2172adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2173904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
21744c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
2175e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
21764c9483b2SDavid S. Miller 		.daddr = iph->daddr,
21774c9483b2SDavid S. Miller 		.saddr = iph->saddr,
21786502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
21794c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
21804c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
2181c71099acSThomas Graf 	};
21825e5d6fedSRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2183adaa70bbSThomas Graf 
2184904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
218546fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2186904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
21875e5d6fedSRoopa Prabhu 
21885e5d6fedSRoopa Prabhu 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
21895e5d6fedSRoopa Prabhu 		flkeys = &_flkeys;
21905e5d6fedSRoopa Prabhu 
219123aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2192b4bac172SDavid Ahern 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
219306e9d040SJiri Benc 	skb_dst_drop(skb);
2194b75cc8f9SDavid Ahern 	skb_dst_set(skb,
2195b75cc8f9SDavid Ahern 		    ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
2196c71099acSThomas Graf }
2197c71099acSThomas Graf 
2198b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net,
2199b75cc8f9SDavid Ahern 					     struct fib6_table *table,
2200b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
2201b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2202b75cc8f9SDavid Ahern 					     int flags)
2203c71099acSThomas Graf {
2204b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2205c71099acSThomas Graf }
2206c71099acSThomas Graf 
22076f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
22086f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags)
2209c71099acSThomas Graf {
2210d46a9d67SDavid Ahern 	bool any_src;
2211c71099acSThomas Graf 
22123ede0bbcSRobert Shearman 	if (ipv6_addr_type(&fl6->daddr) &
22133ede0bbcSRobert Shearman 	    (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
22144c1feac5SDavid Ahern 		struct dst_entry *dst;
22154c1feac5SDavid Ahern 
22164c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
2217ca254490SDavid Ahern 		if (dst)
2218ca254490SDavid Ahern 			return dst;
22194c1feac5SDavid Ahern 	}
2220ca254490SDavid Ahern 
22211fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
22224dc27d1cSDavid McCullough 
2223d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
2224741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2225d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
222677d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
2227c71099acSThomas Graf 
2228d46a9d67SDavid Ahern 	if (!any_src)
2229adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
22300c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
22310c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2232adaa70bbSThomas Graf 
2233b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
22341da177e4SLinus Torvalds }
22356f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
22361da177e4SLinus Torvalds 
22372774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
223814e50e57SDavid S. Miller {
22395c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
22401dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
224114e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
224214e50e57SDavid S. Miller 
22431dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
224462cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
224514e50e57SDavid S. Miller 	if (rt) {
22460a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
224781eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
22480a1f5962SMartin KaFai Lau 
2249d8d1f30bSChangli Gao 		new = &rt->dst;
225014e50e57SDavid S. Miller 		new->__use = 1;
2251352e512cSHerbert Xu 		new->input = dst_discard;
2252ede2059dSEric W. Biederman 		new->output = dst_discard_out;
225314e50e57SDavid S. Miller 
2254defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
225514e50e57SDavid S. Miller 
22561dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
22574e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
22580a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
225914e50e57SDavid S. Miller 
226014e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
226114e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
226214e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
226314e50e57SDavid S. Miller #endif
226414e50e57SDavid S. Miller 	}
226514e50e57SDavid S. Miller 
226669ead7afSDavid S. Miller 	dst_release(dst_orig);
226769ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
226814e50e57SDavid S. Miller }
226914e50e57SDavid S. Miller 
22701da177e4SLinus Torvalds /*
22711da177e4SLinus Torvalds  *	Destination cache support functions
22721da177e4SLinus Torvalds  */
22731da177e4SLinus Torvalds 
22748d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie)
22753da59bd9SMartin KaFai Lau {
227636143645SSteffen Klassert 	u32 rt_cookie = 0;
2277c5cff856SWei Wang 
22788ae86971SDavid Ahern 	if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
227993531c67SDavid Ahern 		return false;
228093531c67SDavid Ahern 
228193531c67SDavid Ahern 	if (fib6_check_expired(f6i))
228293531c67SDavid Ahern 		return false;
228393531c67SDavid Ahern 
228493531c67SDavid Ahern 	return true;
228593531c67SDavid Ahern }
228693531c67SDavid Ahern 
2287a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt,
2288a68886a6SDavid Ahern 				   struct fib6_info *from,
2289a68886a6SDavid Ahern 				   u32 cookie)
22903da59bd9SMartin KaFai Lau {
2291c5cff856SWei Wang 	u32 rt_cookie = 0;
2292c5cff856SWei Wang 
2293a68886a6SDavid Ahern 	if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
229493531c67SDavid Ahern 	    rt_cookie != cookie)
22953da59bd9SMartin KaFai Lau 		return NULL;
22963da59bd9SMartin KaFai Lau 
22973da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
22983da59bd9SMartin KaFai Lau 		return NULL;
22993da59bd9SMartin KaFai Lau 
23003da59bd9SMartin KaFai Lau 	return &rt->dst;
23013da59bd9SMartin KaFai Lau }
23023da59bd9SMartin KaFai Lau 
2303a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2304a68886a6SDavid Ahern 					    struct fib6_info *from,
2305a68886a6SDavid Ahern 					    u32 cookie)
23063da59bd9SMartin KaFai Lau {
23075973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
23085973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2309a68886a6SDavid Ahern 	    fib6_check(from, cookie))
23103da59bd9SMartin KaFai Lau 		return &rt->dst;
23113da59bd9SMartin KaFai Lau 	else
23123da59bd9SMartin KaFai Lau 		return NULL;
23133da59bd9SMartin KaFai Lau }
23143da59bd9SMartin KaFai Lau 
23151da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
23161da177e4SLinus Torvalds {
2317a87b7dc9SDavid Ahern 	struct dst_entry *dst_ret;
2318a68886a6SDavid Ahern 	struct fib6_info *from;
23191da177e4SLinus Torvalds 	struct rt6_info *rt;
23201da177e4SLinus Torvalds 
2321a87b7dc9SDavid Ahern 	rt = container_of(dst, struct rt6_info, dst);
2322a87b7dc9SDavid Ahern 
2323a87b7dc9SDavid Ahern 	rcu_read_lock();
23241da177e4SLinus Torvalds 
23256f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
23266f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
23276f3118b5SNicolas Dichtel 	 * into this function always.
23286f3118b5SNicolas Dichtel 	 */
2329e3bc10bdSHannes Frederic Sowa 
2330a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
23314b32b5adSMartin KaFai Lau 
2332a68886a6SDavid Ahern 	if (from && (rt->rt6i_flags & RTF_PCPU ||
2333a68886a6SDavid Ahern 	    unlikely(!list_empty(&rt->rt6i_uncached))))
2334a68886a6SDavid Ahern 		dst_ret = rt6_dst_from_check(rt, from, cookie);
23353da59bd9SMartin KaFai Lau 	else
2336a68886a6SDavid Ahern 		dst_ret = rt6_check(rt, from, cookie);
2337a87b7dc9SDavid Ahern 
2338a87b7dc9SDavid Ahern 	rcu_read_unlock();
2339a87b7dc9SDavid Ahern 
2340a87b7dc9SDavid Ahern 	return dst_ret;
23411da177e4SLinus Torvalds }
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
23441da177e4SLinus Torvalds {
23451da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
23461da177e4SLinus Torvalds 
23471da177e4SLinus Torvalds 	if (rt) {
234854c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
2349c3c14da0SDavid Ahern 			rcu_read_lock();
235054c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
235193531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
235254c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
23531da177e4SLinus Torvalds 			}
2354c3c14da0SDavid Ahern 			rcu_read_unlock();
235554c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
235654c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
235754c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
235854c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
235954c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
236054c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
23611da177e4SLinus Torvalds }
23621da177e4SLinus Torvalds 
23631da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
23641da177e4SLinus Torvalds {
23651da177e4SLinus Torvalds 	struct rt6_info *rt;
23661da177e4SLinus Torvalds 
23673ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
23681da177e4SLinus Torvalds 
2369adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
23701da177e4SLinus Torvalds 	if (rt) {
23718a14e46fSDavid Ahern 		rcu_read_lock();
23721eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
237393531c67SDavid Ahern 			rt6_remove_exception_rt(rt);
2374c5cff856SWei Wang 		} else {
2375a68886a6SDavid Ahern 			struct fib6_info *from;
2376c5cff856SWei Wang 			struct fib6_node *fn;
2377c5cff856SWei Wang 
2378a68886a6SDavid Ahern 			from = rcu_dereference(rt->from);
2379a68886a6SDavid Ahern 			if (from) {
2380a68886a6SDavid Ahern 				fn = rcu_dereference(from->fib6_node);
2381c5cff856SWei Wang 				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2382c5cff856SWei Wang 					fn->fn_sernum = -1;
2383a68886a6SDavid Ahern 			}
23841da177e4SLinus Torvalds 		}
23851da177e4SLinus Torvalds 		rcu_read_unlock();
23861da177e4SLinus Torvalds 	}
23871da177e4SLinus Torvalds }
23881da177e4SLinus Torvalds 
23896a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout)
23906a3e030fSDavid Ahern {
2391a68886a6SDavid Ahern 	if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2392a68886a6SDavid Ahern 		struct fib6_info *from;
2393a68886a6SDavid Ahern 
2394a68886a6SDavid Ahern 		rcu_read_lock();
2395a68886a6SDavid Ahern 		from = rcu_dereference(rt0->from);
2396a68886a6SDavid Ahern 		if (from)
2397a68886a6SDavid Ahern 			rt0->dst.expires = from->expires;
2398a68886a6SDavid Ahern 		rcu_read_unlock();
2399a68886a6SDavid Ahern 	}
24006a3e030fSDavid Ahern 
24016a3e030fSDavid Ahern 	dst_set_expires(&rt0->dst, timeout);
24026a3e030fSDavid Ahern 	rt0->rt6i_flags |= RTF_EXPIRES;
24036700c270SDavid S. Miller }
24041da177e4SLinus Torvalds 
240545e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
240645e4fd26SMartin KaFai Lau {
240745e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
240845e4fd26SMartin KaFai Lau 
2409d4ead6b3SDavid Ahern 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
241045e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
241145e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
241245e4fd26SMartin KaFai Lau }
241345e4fd26SMartin KaFai Lau 
24140d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
24150d3f6d29SMartin KaFai Lau {
24160d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
24171490ed2aSPaolo Abeni 		(rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
24180d3f6d29SMartin KaFai Lau }
24190d3f6d29SMartin KaFai Lau 
242045e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
242145e4fd26SMartin KaFai Lau 				 const struct ipv6hdr *iph, u32 mtu)
24221da177e4SLinus Torvalds {
24230dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
24241da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
24251da177e4SLinus Torvalds 
242619bda36cSXin Long 	if (dst_metric_locked(dst, RTAX_MTU))
242719bda36cSXin Long 		return;
242819bda36cSXin Long 
242945e4fd26SMartin KaFai Lau 	if (iph) {
243045e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
243145e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
243245e4fd26SMartin KaFai Lau 	} else if (sk) {
243345e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
243445e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
243545e4fd26SMartin KaFai Lau 	} else {
24360dec879fSJulian Anastasov 		daddr = NULL;
24370dec879fSJulian Anastasov 		saddr = NULL;
24381da177e4SLinus Torvalds 	}
24390dec879fSJulian Anastasov 	dst_confirm_neigh(dst, daddr);
24400dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
24410dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
24420dec879fSJulian Anastasov 		return;
24430dec879fSJulian Anastasov 
24440dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
24450dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
24462b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
24472b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
24482b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
24490dec879fSJulian Anastasov 	} else if (daddr) {
245085bd05deSDavid Ahern 		struct fib6_result res = {};
24510dec879fSJulian Anastasov 		struct rt6_info *nrt6;
24520dec879fSJulian Anastasov 
24534d85cd0cSDavid Ahern 		rcu_read_lock();
245485bd05deSDavid Ahern 		res.f6i = rcu_dereference(rt6->from);
245585bd05deSDavid Ahern 		if (!res.f6i) {
24569c69a132SJonathan Lemon 			rcu_read_unlock();
24579c69a132SJonathan Lemon 			return;
24589c69a132SJonathan Lemon 		}
24591cf844c7SDavid Ahern 		res.nh = res.f6i->fib6_nh;
24607d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
24617d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
24627d21fec9SDavid Ahern 
246385bd05deSDavid Ahern 		nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
246445e4fd26SMartin KaFai Lau 		if (nrt6) {
246545e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
24665012f0a5SDavid Ahern 			if (rt6_insert_exception(nrt6, &res))
24672b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
246845e4fd26SMartin KaFai Lau 		}
2469a68886a6SDavid Ahern 		rcu_read_unlock();
247045e4fd26SMartin KaFai Lau 	}
247145e4fd26SMartin KaFai Lau }
247245e4fd26SMartin KaFai Lau 
247345e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
247445e4fd26SMartin KaFai Lau 			       struct sk_buff *skb, u32 mtu)
247545e4fd26SMartin KaFai Lau {
247645e4fd26SMartin KaFai Lau 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
24771da177e4SLinus Torvalds }
24781da177e4SLinus Torvalds 
247942ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2480e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
248181aded24SDavid S. Miller {
248281aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
248381aded24SDavid S. Miller 	struct dst_entry *dst;
2484dc92095dSMaciej Żenczykowski 	struct flowi6 fl6 = {
2485dc92095dSMaciej Żenczykowski 		.flowi6_oif = oif,
2486dc92095dSMaciej Żenczykowski 		.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2487dc92095dSMaciej Żenczykowski 		.daddr = iph->daddr,
2488dc92095dSMaciej Żenczykowski 		.saddr = iph->saddr,
2489dc92095dSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
2490dc92095dSMaciej Żenczykowski 		.flowi6_uid = uid,
2491dc92095dSMaciej Żenczykowski 	};
249281aded24SDavid S. Miller 
249381aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
249481aded24SDavid S. Miller 	if (!dst->error)
249545e4fd26SMartin KaFai Lau 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
249681aded24SDavid S. Miller 	dst_release(dst);
249781aded24SDavid S. Miller }
249881aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
249981aded24SDavid S. Miller 
250081aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
250181aded24SDavid S. Miller {
25027ddacfa5SDavid Ahern 	int oif = sk->sk_bound_dev_if;
250333c162a9SMartin KaFai Lau 	struct dst_entry *dst;
250433c162a9SMartin KaFai Lau 
25057ddacfa5SDavid Ahern 	if (!oif && skb->dev)
25067ddacfa5SDavid Ahern 		oif = l3mdev_master_ifindex(skb->dev);
25077ddacfa5SDavid Ahern 
25087ddacfa5SDavid Ahern 	ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
250933c162a9SMartin KaFai Lau 
251033c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
251133c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
251233c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
251333c162a9SMartin KaFai Lau 		return;
251433c162a9SMartin KaFai Lau 
251533c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
251633c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
251733c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
251833c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
251981aded24SDavid S. Miller }
252081aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
252181aded24SDavid S. Miller 
25227d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
25237d6850f7SAlexey Kodanev 			   const struct flowi6 *fl6)
25247d6850f7SAlexey Kodanev {
25257d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
25267d6850f7SAlexey Kodanev 	struct ipv6_pinfo *np = inet6_sk(sk);
25277d6850f7SAlexey Kodanev #endif
25287d6850f7SAlexey Kodanev 
25297d6850f7SAlexey Kodanev 	ip6_dst_store(sk, dst,
25307d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
25317d6850f7SAlexey Kodanev 		      &sk->sk_v6_daddr : NULL,
25327d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
25337d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
25347d6850f7SAlexey Kodanev 		      &np->saddr :
25357d6850f7SAlexey Kodanev #endif
25367d6850f7SAlexey Kodanev 		      NULL);
25377d6850f7SAlexey Kodanev }
25387d6850f7SAlexey Kodanev 
25399b6b35abSDavid Ahern static bool ip6_redirect_nh_match(const struct fib6_result *res,
25400b34eb00SDavid Ahern 				  struct flowi6 *fl6,
25410b34eb00SDavid Ahern 				  const struct in6_addr *gw,
25420b34eb00SDavid Ahern 				  struct rt6_info **ret)
25430b34eb00SDavid Ahern {
25449b6b35abSDavid Ahern 	const struct fib6_nh *nh = res->nh;
25459b6b35abSDavid Ahern 
25460b34eb00SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
25470b34eb00SDavid Ahern 	    fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
25480b34eb00SDavid Ahern 		return false;
25490b34eb00SDavid Ahern 
25500b34eb00SDavid Ahern 	/* rt_cache's gateway might be different from its 'parent'
25510b34eb00SDavid Ahern 	 * in the case of an ip redirect.
25520b34eb00SDavid Ahern 	 * So we keep searching in the exception table if the gateway
25530b34eb00SDavid Ahern 	 * is different.
25540b34eb00SDavid Ahern 	 */
25550b34eb00SDavid Ahern 	if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
25560b34eb00SDavid Ahern 		struct rt6_info *rt_cache;
25570b34eb00SDavid Ahern 
25589b6b35abSDavid Ahern 		rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
25590b34eb00SDavid Ahern 		if (rt_cache &&
25600b34eb00SDavid Ahern 		    ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
25610b34eb00SDavid Ahern 			*ret = rt_cache;
25620b34eb00SDavid Ahern 			return true;
25630b34eb00SDavid Ahern 		}
25640b34eb00SDavid Ahern 		return false;
25650b34eb00SDavid Ahern 	}
25660b34eb00SDavid Ahern 	return true;
25670b34eb00SDavid Ahern }
25680b34eb00SDavid Ahern 
2569b55b76b2SDuan Jiong /* Handle redirects */
2570b55b76b2SDuan Jiong struct ip6rd_flowi {
2571b55b76b2SDuan Jiong 	struct flowi6 fl6;
2572b55b76b2SDuan Jiong 	struct in6_addr gateway;
2573b55b76b2SDuan Jiong };
2574b55b76b2SDuan Jiong 
2575b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
2576b55b76b2SDuan Jiong 					     struct fib6_table *table,
2577b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2578b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2579b55b76b2SDuan Jiong 					     int flags)
2580b55b76b2SDuan Jiong {
2581b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
25820b34eb00SDavid Ahern 	struct rt6_info *ret = NULL;
25839b6b35abSDavid Ahern 	struct fib6_result res = {};
25848d1c802bSDavid Ahern 	struct fib6_info *rt;
2585b55b76b2SDuan Jiong 	struct fib6_node *fn;
2586b55b76b2SDuan Jiong 
2587b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
258867c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2589b55b76b2SDuan Jiong 	 *
2590b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2591b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2592b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2593b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2594b55b76b2SDuan Jiong 	 * routes.
2595b55b76b2SDuan Jiong 	 */
2596b55b76b2SDuan Jiong 
259766f5d6ceSWei Wang 	rcu_read_lock();
25986454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2599b55b76b2SDuan Jiong restart:
260066f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
26019b6b35abSDavid Ahern 		res.f6i = rt;
26021cf844c7SDavid Ahern 		res.nh = rt->fib6_nh;
26039b6b35abSDavid Ahern 
260414895687SDavid Ahern 		if (fib6_check_expired(rt))
2605b55b76b2SDuan Jiong 			continue;
260693c2fb25SDavid Ahern 		if (rt->fib6_flags & RTF_REJECT)
2607b55b76b2SDuan Jiong 			break;
26089b6b35abSDavid Ahern 		if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, &ret))
26090b34eb00SDavid Ahern 			goto out;
2610b55b76b2SDuan Jiong 	}
2611b55b76b2SDuan Jiong 
2612b55b76b2SDuan Jiong 	if (!rt)
2613421842edSDavid Ahern 		rt = net->ipv6.fib6_null_entry;
261493c2fb25SDavid Ahern 	else if (rt->fib6_flags & RTF_REJECT) {
261523fb93a4SDavid Ahern 		ret = net->ipv6.ip6_null_entry;
2616b0a1ba59SMartin KaFai Lau 		goto out;
2617b0a1ba59SMartin KaFai Lau 	}
2618b0a1ba59SMartin KaFai Lau 
2619421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
2620a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2621a3c00e46SMartin KaFai Lau 		if (fn)
2622a3c00e46SMartin KaFai Lau 			goto restart;
2623b55b76b2SDuan Jiong 	}
2624a3c00e46SMartin KaFai Lau 
26259b6b35abSDavid Ahern 	res.f6i = rt;
26261cf844c7SDavid Ahern 	res.nh = rt->fib6_nh;
2627b0a1ba59SMartin KaFai Lau out:
26287d21fec9SDavid Ahern 	if (ret) {
262910585b43SDavid Ahern 		ip6_hold_safe(net, &ret);
26307d21fec9SDavid Ahern 	} else {
26317d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
26327d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
26339b6b35abSDavid Ahern 		ret = ip6_create_rt_rcu(&res);
26347d21fec9SDavid Ahern 	}
2635b55b76b2SDuan Jiong 
263666f5d6ceSWei Wang 	rcu_read_unlock();
2637b55b76b2SDuan Jiong 
26388ff2e5b2SDavid Ahern 	trace_fib6_table_lookup(net, &res, table, fl6);
263923fb93a4SDavid Ahern 	return ret;
2640b55b76b2SDuan Jiong };
2641b55b76b2SDuan Jiong 
2642b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
2643b55b76b2SDuan Jiong 					    const struct flowi6 *fl6,
2644b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2645b55b76b2SDuan Jiong 					    const struct in6_addr *gateway)
2646b55b76b2SDuan Jiong {
2647b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2648b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
2649b55b76b2SDuan Jiong 
2650b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
2651b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
2652b55b76b2SDuan Jiong 
2653b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, &rdfl.fl6, skb,
2654b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
2655b55b76b2SDuan Jiong }
2656b55b76b2SDuan Jiong 
2657e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2658e2d118a1SLorenzo Colitti 		  kuid_t uid)
26593a5ad2eeSDavid S. Miller {
26603a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
26613a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
26621f7f10acSMaciej Żenczykowski 	struct flowi6 fl6 = {
26631f7f10acSMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
26641f7f10acSMaciej Żenczykowski 		.flowi6_oif = oif,
26651f7f10acSMaciej Żenczykowski 		.flowi6_mark = mark,
26661f7f10acSMaciej Żenczykowski 		.daddr = iph->daddr,
26671f7f10acSMaciej Żenczykowski 		.saddr = iph->saddr,
26681f7f10acSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
26691f7f10acSMaciej Żenczykowski 		.flowi6_uid = uid,
26701f7f10acSMaciej Żenczykowski 	};
26713a5ad2eeSDavid S. Miller 
2672b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
26736700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
26743a5ad2eeSDavid S. Miller 	dst_release(dst);
26753a5ad2eeSDavid S. Miller }
26763a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
26773a5ad2eeSDavid S. Miller 
2678d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
2679c92a59ecSDuan Jiong {
2680c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2681c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2682c92a59ecSDuan Jiong 	struct dst_entry *dst;
26830b26fb17SMaciej Żenczykowski 	struct flowi6 fl6 = {
26840b26fb17SMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
26850b26fb17SMaciej Żenczykowski 		.flowi6_oif = oif,
26860b26fb17SMaciej Żenczykowski 		.daddr = msg->dest,
26870b26fb17SMaciej Żenczykowski 		.saddr = iph->daddr,
26880b26fb17SMaciej Żenczykowski 		.flowi6_uid = sock_net_uid(net, NULL),
26890b26fb17SMaciej Żenczykowski 	};
2690c92a59ecSDuan Jiong 
2691b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
2692c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
2693c92a59ecSDuan Jiong 	dst_release(dst);
2694c92a59ecSDuan Jiong }
2695c92a59ecSDuan Jiong 
26963a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
26973a5ad2eeSDavid S. Miller {
2698e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2699e2d118a1SLorenzo Colitti 		     sk->sk_uid);
27003a5ad2eeSDavid S. Miller }
27013a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
27023a5ad2eeSDavid S. Miller 
27030dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
27041da177e4SLinus Torvalds {
27050dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
27060dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
27070dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
27080dbaee3bSDavid S. Miller 
27091da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
27101da177e4SLinus Torvalds 
27115578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
27125578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds 	/*
27151da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
27161da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
27171da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
27181da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
27191da177e4SLinus Torvalds 	 */
27201da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
27211da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
27221da177e4SLinus Torvalds 	return mtu;
27231da177e4SLinus Torvalds }
27241da177e4SLinus Torvalds 
2725ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
2726d33e4553SDavid S. Miller {
2727d33e4553SDavid S. Miller 	struct inet6_dev *idev;
2728d4ead6b3SDavid Ahern 	unsigned int mtu;
2729618f9bc7SSteffen Klassert 
27304b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
27314b32b5adSMartin KaFai Lau 	if (mtu)
27324b32b5adSMartin KaFai Lau 		goto out;
27334b32b5adSMartin KaFai Lau 
2734618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
2735d33e4553SDavid S. Miller 
2736d33e4553SDavid S. Miller 	rcu_read_lock();
2737d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
2738d33e4553SDavid S. Miller 	if (idev)
2739d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
2740d33e4553SDavid S. Miller 	rcu_read_unlock();
2741d33e4553SDavid S. Miller 
274230f78d8eSEric Dumazet out:
274314972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
274414972cbdSRoopa Prabhu 
274514972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
2746d33e4553SDavid S. Miller }
2747d33e4553SDavid S. Miller 
2748901731b8SDavid Ahern /* MTU selection:
2749901731b8SDavid Ahern  * 1. mtu on route is locked - use it
2750901731b8SDavid Ahern  * 2. mtu from nexthop exception
2751901731b8SDavid Ahern  * 3. mtu from egress device
2752901731b8SDavid Ahern  *
2753901731b8SDavid Ahern  * based on ip6_dst_mtu_forward and exception logic of
2754901731b8SDavid Ahern  * rt6_find_cached_rt; called with rcu_read_lock
2755901731b8SDavid Ahern  */
2756b748f260SDavid Ahern u32 ip6_mtu_from_fib6(const struct fib6_result *res,
2757b748f260SDavid Ahern 		      const struct in6_addr *daddr,
2758b748f260SDavid Ahern 		      const struct in6_addr *saddr)
2759901731b8SDavid Ahern {
2760b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
2761b748f260SDavid Ahern 	struct fib6_info *f6i = res->f6i;
2762901731b8SDavid Ahern 	struct inet6_dev *idev;
2763510e2cedSWei Wang 	struct rt6_info *rt;
2764901731b8SDavid Ahern 	u32 mtu = 0;
2765901731b8SDavid Ahern 
2766901731b8SDavid Ahern 	if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
2767901731b8SDavid Ahern 		mtu = f6i->fib6_pmtu;
2768901731b8SDavid Ahern 		if (mtu)
2769901731b8SDavid Ahern 			goto out;
2770901731b8SDavid Ahern 	}
2771901731b8SDavid Ahern 
2772510e2cedSWei Wang 	rt = rt6_find_cached_rt(res, daddr, saddr);
2773510e2cedSWei Wang 	if (unlikely(rt)) {
2774510e2cedSWei Wang 		mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
2775510e2cedSWei Wang 	} else {
2776b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
2777901731b8SDavid Ahern 
2778901731b8SDavid Ahern 		mtu = IPV6_MIN_MTU;
2779901731b8SDavid Ahern 		idev = __in6_dev_get(dev);
2780901731b8SDavid Ahern 		if (idev && idev->cnf.mtu6 > mtu)
2781901731b8SDavid Ahern 			mtu = idev->cnf.mtu6;
2782901731b8SDavid Ahern 	}
2783901731b8SDavid Ahern 
2784901731b8SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2785901731b8SDavid Ahern out:
2786b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
2787901731b8SDavid Ahern }
2788901731b8SDavid Ahern 
27893b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
279087a11578SDavid S. Miller 				  struct flowi6 *fl6)
27911da177e4SLinus Torvalds {
279287a11578SDavid S. Miller 	struct dst_entry *dst;
27931da177e4SLinus Torvalds 	struct rt6_info *rt;
27941da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
2795c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
27961da177e4SLinus Torvalds 
279738308473SDavid S. Miller 	if (unlikely(!idev))
2798122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
27991da177e4SLinus Torvalds 
2800ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
280138308473SDavid S. Miller 	if (unlikely(!rt)) {
28021da177e4SLinus Torvalds 		in6_dev_put(idev);
280387a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
28041da177e4SLinus Torvalds 		goto out;
28051da177e4SLinus Torvalds 	}
28061da177e4SLinus Torvalds 
28078e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
2808588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
28098e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
2810550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
281187a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
28128e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
28138e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
281414edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
28151da177e4SLinus Torvalds 
28164c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
2817587fea74SWei Wang 	 * do proper release of the net_device
2818587fea74SWei Wang 	 */
2819587fea74SWei Wang 	rt6_uncached_list_add(rt);
282081eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
28211da177e4SLinus Torvalds 
282287a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
282387a11578SDavid S. Miller 
28241da177e4SLinus Torvalds out:
282587a11578SDavid S. Miller 	return dst;
28261da177e4SLinus Torvalds }
28271da177e4SLinus Torvalds 
2828569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
28291da177e4SLinus Torvalds {
283086393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
28317019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
28327019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
28337019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
28347019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
28357019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
2836fc66f95cSEric Dumazet 	int entries;
28371da177e4SLinus Torvalds 
2838fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
283949a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
2840fc66f95cSEric Dumazet 	    entries <= rt_max_size)
28411da177e4SLinus Torvalds 		goto out;
28421da177e4SLinus Torvalds 
28436891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
284414956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
2845fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
2846fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
28477019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
28481da177e4SLinus Torvalds out:
28497019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
2850fc66f95cSEric Dumazet 	return entries > rt_max_size;
28511da177e4SLinus Torvalds }
28521da177e4SLinus Torvalds 
28538c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net,
28548c14586fSDavid Ahern 					    struct fib6_config *cfg,
2855f4797b33SDavid Ahern 					    const struct in6_addr *gw_addr,
2856f4797b33SDavid Ahern 					    u32 tbid, int flags)
28578c14586fSDavid Ahern {
28588c14586fSDavid Ahern 	struct flowi6 fl6 = {
28598c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
28608c14586fSDavid Ahern 		.daddr = *gw_addr,
28618c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
28628c14586fSDavid Ahern 	};
28638c14586fSDavid Ahern 	struct fib6_table *table;
28648c14586fSDavid Ahern 	struct rt6_info *rt;
28658c14586fSDavid Ahern 
2866f4797b33SDavid Ahern 	table = fib6_get_table(net, tbid);
28678c14586fSDavid Ahern 	if (!table)
28688c14586fSDavid Ahern 		return NULL;
28698c14586fSDavid Ahern 
28708c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
28718c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
28728c14586fSDavid Ahern 
2873f4797b33SDavid Ahern 	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
2874b75cc8f9SDavid Ahern 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
28758c14586fSDavid Ahern 
28768c14586fSDavid Ahern 	/* if table lookup failed, fall back to full lookup */
28778c14586fSDavid Ahern 	if (rt == net->ipv6.ip6_null_entry) {
28788c14586fSDavid Ahern 		ip6_rt_put(rt);
28798c14586fSDavid Ahern 		rt = NULL;
28808c14586fSDavid Ahern 	}
28818c14586fSDavid Ahern 
28828c14586fSDavid Ahern 	return rt;
28838c14586fSDavid Ahern }
28848c14586fSDavid Ahern 
2885fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net,
2886fc1e64e1SDavid Ahern 				     struct fib6_config *cfg,
28879fbb704cSDavid Ahern 				     const struct net_device *dev,
2888fc1e64e1SDavid Ahern 				     struct netlink_ext_ack *extack)
2889fc1e64e1SDavid Ahern {
289044750f84SDavid Ahern 	u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
2891fc1e64e1SDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
2892fc1e64e1SDavid Ahern 	u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2893bf1dc8baSPaolo Abeni 	struct fib6_info *from;
2894fc1e64e1SDavid Ahern 	struct rt6_info *grt;
2895fc1e64e1SDavid Ahern 	int err;
2896fc1e64e1SDavid Ahern 
2897fc1e64e1SDavid Ahern 	err = 0;
2898fc1e64e1SDavid Ahern 	grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2899fc1e64e1SDavid Ahern 	if (grt) {
2900bf1dc8baSPaolo Abeni 		rcu_read_lock();
2901bf1dc8baSPaolo Abeni 		from = rcu_dereference(grt->from);
290258e354c0SDavid Ahern 		if (!grt->dst.error &&
29034ed591c8SDavid Ahern 		    /* ignore match if it is the default route */
2904bf1dc8baSPaolo Abeni 		    from && !ipv6_addr_any(&from->fib6_dst.addr) &&
290558e354c0SDavid Ahern 		    (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
290644750f84SDavid Ahern 			NL_SET_ERR_MSG(extack,
290744750f84SDavid Ahern 				       "Nexthop has invalid gateway or device mismatch");
2908fc1e64e1SDavid Ahern 			err = -EINVAL;
2909fc1e64e1SDavid Ahern 		}
2910bf1dc8baSPaolo Abeni 		rcu_read_unlock();
2911fc1e64e1SDavid Ahern 
2912fc1e64e1SDavid Ahern 		ip6_rt_put(grt);
2913fc1e64e1SDavid Ahern 	}
2914fc1e64e1SDavid Ahern 
2915fc1e64e1SDavid Ahern 	return err;
2916fc1e64e1SDavid Ahern }
2917fc1e64e1SDavid Ahern 
29181edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net,
29191edce99fSDavid Ahern 			      struct fib6_config *cfg,
29201edce99fSDavid Ahern 			      struct net_device **_dev,
29211edce99fSDavid Ahern 			      struct inet6_dev **idev)
29221edce99fSDavid Ahern {
29231edce99fSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
29241edce99fSDavid Ahern 	struct net_device *dev = _dev ? *_dev : NULL;
29251edce99fSDavid Ahern 	struct rt6_info *grt = NULL;
29261edce99fSDavid Ahern 	int err = -EHOSTUNREACH;
29271edce99fSDavid Ahern 
29281edce99fSDavid Ahern 	if (cfg->fc_table) {
2929f4797b33SDavid Ahern 		int flags = RT6_LOOKUP_F_IFACE;
2930f4797b33SDavid Ahern 
2931f4797b33SDavid Ahern 		grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2932f4797b33SDavid Ahern 					  cfg->fc_table, flags);
29331edce99fSDavid Ahern 		if (grt) {
29341edce99fSDavid Ahern 			if (grt->rt6i_flags & RTF_GATEWAY ||
29351edce99fSDavid Ahern 			    (dev && dev != grt->dst.dev)) {
29361edce99fSDavid Ahern 				ip6_rt_put(grt);
29371edce99fSDavid Ahern 				grt = NULL;
29381edce99fSDavid Ahern 			}
29391edce99fSDavid Ahern 		}
29401edce99fSDavid Ahern 	}
29411edce99fSDavid Ahern 
29421edce99fSDavid Ahern 	if (!grt)
2943b75cc8f9SDavid Ahern 		grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
29441edce99fSDavid Ahern 
29451edce99fSDavid Ahern 	if (!grt)
29461edce99fSDavid Ahern 		goto out;
29471edce99fSDavid Ahern 
29481edce99fSDavid Ahern 	if (dev) {
29491edce99fSDavid Ahern 		if (dev != grt->dst.dev) {
29501edce99fSDavid Ahern 			ip6_rt_put(grt);
29511edce99fSDavid Ahern 			goto out;
29521edce99fSDavid Ahern 		}
29531edce99fSDavid Ahern 	} else {
29541edce99fSDavid Ahern 		*_dev = dev = grt->dst.dev;
29551edce99fSDavid Ahern 		*idev = grt->rt6i_idev;
29561edce99fSDavid Ahern 		dev_hold(dev);
29571edce99fSDavid Ahern 		in6_dev_hold(grt->rt6i_idev);
29581edce99fSDavid Ahern 	}
29591edce99fSDavid Ahern 
29601edce99fSDavid Ahern 	if (!(grt->rt6i_flags & RTF_GATEWAY))
29611edce99fSDavid Ahern 		err = 0;
29621edce99fSDavid Ahern 
29631edce99fSDavid Ahern 	ip6_rt_put(grt);
29641edce99fSDavid Ahern 
29651edce99fSDavid Ahern out:
29661edce99fSDavid Ahern 	return err;
29671edce99fSDavid Ahern }
29681edce99fSDavid Ahern 
29699fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
29709fbb704cSDavid Ahern 			   struct net_device **_dev, struct inet6_dev **idev,
29719fbb704cSDavid Ahern 			   struct netlink_ext_ack *extack)
29729fbb704cSDavid Ahern {
29739fbb704cSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
29749fbb704cSDavid Ahern 	int gwa_type = ipv6_addr_type(gw_addr);
2975232378e8SDavid Ahern 	bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
29769fbb704cSDavid Ahern 	const struct net_device *dev = *_dev;
2977232378e8SDavid Ahern 	bool need_addr_check = !dev;
29789fbb704cSDavid Ahern 	int err = -EINVAL;
29799fbb704cSDavid Ahern 
29809fbb704cSDavid Ahern 	/* if gw_addr is local we will fail to detect this in case
29819fbb704cSDavid Ahern 	 * address is still TENTATIVE (DAD in progress). rt6_lookup()
29829fbb704cSDavid Ahern 	 * will return already-added prefix route via interface that
29839fbb704cSDavid Ahern 	 * prefix route was assigned to, which might be non-loopback.
29849fbb704cSDavid Ahern 	 */
2985232378e8SDavid Ahern 	if (dev &&
2986232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2987232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
29889fbb704cSDavid Ahern 		goto out;
29899fbb704cSDavid Ahern 	}
29909fbb704cSDavid Ahern 
29919fbb704cSDavid Ahern 	if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
29929fbb704cSDavid Ahern 		/* IPv6 strictly inhibits using not link-local
29939fbb704cSDavid Ahern 		 * addresses as nexthop address.
29949fbb704cSDavid Ahern 		 * Otherwise, router will not able to send redirects.
29959fbb704cSDavid Ahern 		 * It is very good, but in some (rare!) circumstances
29969fbb704cSDavid Ahern 		 * (SIT, PtP, NBMA NOARP links) it is handy to allow
29979fbb704cSDavid Ahern 		 * some exceptions. --ANK
29989fbb704cSDavid Ahern 		 * We allow IPv4-mapped nexthops to support RFC4798-type
29999fbb704cSDavid Ahern 		 * addressing
30009fbb704cSDavid Ahern 		 */
30019fbb704cSDavid Ahern 		if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
30029fbb704cSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
30039fbb704cSDavid Ahern 			goto out;
30049fbb704cSDavid Ahern 		}
30059fbb704cSDavid Ahern 
30069fbb704cSDavid Ahern 		if (cfg->fc_flags & RTNH_F_ONLINK)
30079fbb704cSDavid Ahern 			err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
30089fbb704cSDavid Ahern 		else
30099fbb704cSDavid Ahern 			err = ip6_route_check_nh(net, cfg, _dev, idev);
30109fbb704cSDavid Ahern 
30119fbb704cSDavid Ahern 		if (err)
30129fbb704cSDavid Ahern 			goto out;
30139fbb704cSDavid Ahern 	}
30149fbb704cSDavid Ahern 
30159fbb704cSDavid Ahern 	/* reload in case device was changed */
30169fbb704cSDavid Ahern 	dev = *_dev;
30179fbb704cSDavid Ahern 
30189fbb704cSDavid Ahern 	err = -EINVAL;
30199fbb704cSDavid Ahern 	if (!dev) {
30209fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Egress device not specified");
30219fbb704cSDavid Ahern 		goto out;
30229fbb704cSDavid Ahern 	} else if (dev->flags & IFF_LOOPBACK) {
30239fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack,
30249fbb704cSDavid Ahern 			       "Egress device can not be loopback device for this route");
30259fbb704cSDavid Ahern 		goto out;
30269fbb704cSDavid Ahern 	}
3027232378e8SDavid Ahern 
3028232378e8SDavid Ahern 	/* if we did not check gw_addr above, do so now that the
3029232378e8SDavid Ahern 	 * egress device has been resolved.
3030232378e8SDavid Ahern 	 */
3031232378e8SDavid Ahern 	if (need_addr_check &&
3032232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3033232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3034232378e8SDavid Ahern 		goto out;
3035232378e8SDavid Ahern 	}
3036232378e8SDavid Ahern 
30379fbb704cSDavid Ahern 	err = 0;
30389fbb704cSDavid Ahern out:
30399fbb704cSDavid Ahern 	return err;
30409fbb704cSDavid Ahern }
30419fbb704cSDavid Ahern 
304283c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
304383c44251SDavid Ahern {
304483c44251SDavid Ahern 	if ((flags & RTF_REJECT) ||
304583c44251SDavid Ahern 	    (dev && (dev->flags & IFF_LOOPBACK) &&
304683c44251SDavid Ahern 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
304783c44251SDavid Ahern 	     !(flags & RTF_LOCAL)))
304883c44251SDavid Ahern 		return true;
304983c44251SDavid Ahern 
305083c44251SDavid Ahern 	return false;
305183c44251SDavid Ahern }
305283c44251SDavid Ahern 
305383c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
305483c44251SDavid Ahern 		 struct fib6_config *cfg, gfp_t gfp_flags,
305583c44251SDavid Ahern 		 struct netlink_ext_ack *extack)
305683c44251SDavid Ahern {
305783c44251SDavid Ahern 	struct net_device *dev = NULL;
305883c44251SDavid Ahern 	struct inet6_dev *idev = NULL;
305983c44251SDavid Ahern 	int addr_type;
306083c44251SDavid Ahern 	int err;
306183c44251SDavid Ahern 
3062f1741730SDavid Ahern 	fib6_nh->fib_nh_family = AF_INET6;
3063f1741730SDavid Ahern 
306483c44251SDavid Ahern 	err = -ENODEV;
306583c44251SDavid Ahern 	if (cfg->fc_ifindex) {
306683c44251SDavid Ahern 		dev = dev_get_by_index(net, cfg->fc_ifindex);
306783c44251SDavid Ahern 		if (!dev)
306883c44251SDavid Ahern 			goto out;
306983c44251SDavid Ahern 		idev = in6_dev_get(dev);
307083c44251SDavid Ahern 		if (!idev)
307183c44251SDavid Ahern 			goto out;
307283c44251SDavid Ahern 	}
307383c44251SDavid Ahern 
307483c44251SDavid Ahern 	if (cfg->fc_flags & RTNH_F_ONLINK) {
307583c44251SDavid Ahern 		if (!dev) {
307683c44251SDavid Ahern 			NL_SET_ERR_MSG(extack,
307783c44251SDavid Ahern 				       "Nexthop device required for onlink");
307883c44251SDavid Ahern 			goto out;
307983c44251SDavid Ahern 		}
308083c44251SDavid Ahern 
308183c44251SDavid Ahern 		if (!(dev->flags & IFF_UP)) {
308283c44251SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
308383c44251SDavid Ahern 			err = -ENETDOWN;
308483c44251SDavid Ahern 			goto out;
308583c44251SDavid Ahern 		}
308683c44251SDavid Ahern 
3087ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
308883c44251SDavid Ahern 	}
308983c44251SDavid Ahern 
3090ad1601aeSDavid Ahern 	fib6_nh->fib_nh_weight = 1;
309183c44251SDavid Ahern 
309283c44251SDavid Ahern 	/* We cannot add true routes via loopback here,
309383c44251SDavid Ahern 	 * they would result in kernel looping; promote them to reject routes
309483c44251SDavid Ahern 	 */
309583c44251SDavid Ahern 	addr_type = ipv6_addr_type(&cfg->fc_dst);
309683c44251SDavid Ahern 	if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
309783c44251SDavid Ahern 		/* hold loopback dev/idev if we haven't done so. */
309883c44251SDavid Ahern 		if (dev != net->loopback_dev) {
309983c44251SDavid Ahern 			if (dev) {
310083c44251SDavid Ahern 				dev_put(dev);
310183c44251SDavid Ahern 				in6_dev_put(idev);
310283c44251SDavid Ahern 			}
310383c44251SDavid Ahern 			dev = net->loopback_dev;
310483c44251SDavid Ahern 			dev_hold(dev);
310583c44251SDavid Ahern 			idev = in6_dev_get(dev);
310683c44251SDavid Ahern 			if (!idev) {
310783c44251SDavid Ahern 				err = -ENODEV;
310883c44251SDavid Ahern 				goto out;
310983c44251SDavid Ahern 			}
311083c44251SDavid Ahern 		}
311183c44251SDavid Ahern 		goto set_dev;
311283c44251SDavid Ahern 	}
311383c44251SDavid Ahern 
311483c44251SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY) {
311583c44251SDavid Ahern 		err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
311683c44251SDavid Ahern 		if (err)
311783c44251SDavid Ahern 			goto out;
311883c44251SDavid Ahern 
3119ad1601aeSDavid Ahern 		fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3120bdf00467SDavid Ahern 		fib6_nh->fib_nh_gw_family = AF_INET6;
312183c44251SDavid Ahern 	}
312283c44251SDavid Ahern 
312383c44251SDavid Ahern 	err = -ENODEV;
312483c44251SDavid Ahern 	if (!dev)
312583c44251SDavid Ahern 		goto out;
312683c44251SDavid Ahern 
312783c44251SDavid Ahern 	if (idev->cnf.disable_ipv6) {
312883c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
312983c44251SDavid Ahern 		err = -EACCES;
313083c44251SDavid Ahern 		goto out;
313183c44251SDavid Ahern 	}
313283c44251SDavid Ahern 
313383c44251SDavid Ahern 	if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
313483c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
313583c44251SDavid Ahern 		err = -ENETDOWN;
313683c44251SDavid Ahern 		goto out;
313783c44251SDavid Ahern 	}
313883c44251SDavid Ahern 
313983c44251SDavid Ahern 	if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
314083c44251SDavid Ahern 	    !netif_carrier_ok(dev))
3141ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
314283c44251SDavid Ahern 
3143f40b6ae2SDavid Ahern 	fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3144f40b6ae2SDavid Ahern 	if (!fib6_nh->rt6i_pcpu) {
3145f40b6ae2SDavid Ahern 		err = -ENOMEM;
3146f40b6ae2SDavid Ahern 		goto out;
3147f40b6ae2SDavid Ahern 	}
3148f40b6ae2SDavid Ahern 
3149979e276eSDavid Ahern 	err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
3150979e276eSDavid Ahern 				 cfg->fc_encap_type, cfg, gfp_flags, extack);
3151979e276eSDavid Ahern 	if (err)
3152979e276eSDavid Ahern 		goto out;
315383c44251SDavid Ahern set_dev:
3154ad1601aeSDavid Ahern 	fib6_nh->fib_nh_dev = dev;
3155f1741730SDavid Ahern 	fib6_nh->fib_nh_oif = dev->ifindex;
315683c44251SDavid Ahern 	err = 0;
315783c44251SDavid Ahern out:
315883c44251SDavid Ahern 	if (idev)
315983c44251SDavid Ahern 		in6_dev_put(idev);
316083c44251SDavid Ahern 
316183c44251SDavid Ahern 	if (err) {
3162ad1601aeSDavid Ahern 		lwtstate_put(fib6_nh->fib_nh_lws);
3163ad1601aeSDavid Ahern 		fib6_nh->fib_nh_lws = NULL;
316483c44251SDavid Ahern 		if (dev)
316583c44251SDavid Ahern 			dev_put(dev);
316683c44251SDavid Ahern 	}
316783c44251SDavid Ahern 
316883c44251SDavid Ahern 	return err;
316983c44251SDavid Ahern }
317083c44251SDavid Ahern 
3171dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh)
3172dac7d0f2SDavid Ahern {
3173cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
3174cc5c073aSDavid Ahern 
3175cc5c073aSDavid Ahern 	rcu_read_lock();
3176cc5c073aSDavid Ahern 
3177cc5c073aSDavid Ahern 	fib6_nh_flush_exceptions(fib6_nh, NULL);
3178cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3179cc5c073aSDavid Ahern 	if (bucket) {
3180cc5c073aSDavid Ahern 		rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3181cc5c073aSDavid Ahern 		kfree(bucket);
3182cc5c073aSDavid Ahern 	}
3183cc5c073aSDavid Ahern 
3184cc5c073aSDavid Ahern 	rcu_read_unlock();
3185cc5c073aSDavid Ahern 
3186f40b6ae2SDavid Ahern 	if (fib6_nh->rt6i_pcpu) {
3187f40b6ae2SDavid Ahern 		int cpu;
3188f40b6ae2SDavid Ahern 
3189f40b6ae2SDavid Ahern 		for_each_possible_cpu(cpu) {
3190f40b6ae2SDavid Ahern 			struct rt6_info **ppcpu_rt;
3191f40b6ae2SDavid Ahern 			struct rt6_info *pcpu_rt;
3192f40b6ae2SDavid Ahern 
3193f40b6ae2SDavid Ahern 			ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3194f40b6ae2SDavid Ahern 			pcpu_rt = *ppcpu_rt;
3195f40b6ae2SDavid Ahern 			if (pcpu_rt) {
3196f40b6ae2SDavid Ahern 				dst_dev_put(&pcpu_rt->dst);
3197f40b6ae2SDavid Ahern 				dst_release(&pcpu_rt->dst);
3198f40b6ae2SDavid Ahern 				*ppcpu_rt = NULL;
3199f40b6ae2SDavid Ahern 			}
3200f40b6ae2SDavid Ahern 		}
3201f40b6ae2SDavid Ahern 
3202f40b6ae2SDavid Ahern 		free_percpu(fib6_nh->rt6i_pcpu);
3203f40b6ae2SDavid Ahern 	}
3204f40b6ae2SDavid Ahern 
3205979e276eSDavid Ahern 	fib_nh_common_release(&fib6_nh->nh_common);
3206dac7d0f2SDavid Ahern }
3207dac7d0f2SDavid Ahern 
32088d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3209acb54e3cSDavid Ahern 					      gfp_t gfp_flags,
3210333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
32111da177e4SLinus Torvalds {
32125578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
32138d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3214c71099acSThomas Graf 	struct fib6_table *table;
32158c5b83f0SRoopa Prabhu 	int err = -EINVAL;
321683c44251SDavid Ahern 	int addr_type;
32171da177e4SLinus Torvalds 
3218557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
3219d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
3220d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3221557c44beSDavid Ahern 		goto out;
3222d5d531cbSDavid Ahern 	}
3223557c44beSDavid Ahern 
32242ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
32252ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
32262ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
32272ea2352eSWei Wang 		goto out;
32282ea2352eSWei Wang 	}
32292ea2352eSWei Wang 
3230e8478e80SDavid Ahern 	if (cfg->fc_type > RTN_MAX) {
3231e8478e80SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid route type");
3232e8478e80SDavid Ahern 		goto out;
3233e8478e80SDavid Ahern 	}
3234e8478e80SDavid Ahern 
3235d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
3236d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
32378c5b83f0SRoopa Prabhu 		goto out;
3238d5d531cbSDavid Ahern 	}
3239d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
3240d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
3241d5d531cbSDavid Ahern 		goto out;
3242d5d531cbSDavid Ahern 	}
32431da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
3244d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
3245d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
3246d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
32478c5b83f0SRoopa Prabhu 		goto out;
3248d5d531cbSDavid Ahern 	}
32491da177e4SLinus Torvalds #endif
3250fc1e64e1SDavid Ahern 
3251c71099acSThomas Graf 	err = -ENOBUFS;
325238308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
3253d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3254d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
325538308473SDavid S. Miller 		if (!table) {
3256f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3257d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
3258d71314b4SMatti Vaittinen 		}
3259d71314b4SMatti Vaittinen 	} else {
3260d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
3261d71314b4SMatti Vaittinen 	}
326238308473SDavid S. Miller 
326338308473SDavid S. Miller 	if (!table)
3264c71099acSThomas Graf 		goto out;
3265c71099acSThomas Graf 
32661da177e4SLinus Torvalds 	err = -ENOMEM;
32671cf844c7SDavid Ahern 	rt = fib6_info_alloc(gfp_flags, true);
326893531c67SDavid Ahern 	if (!rt)
32691da177e4SLinus Torvalds 		goto out;
327093531c67SDavid Ahern 
3271d7e774f3SDavid Ahern 	rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3272d7e774f3SDavid Ahern 					       extack);
3273767a2217SDavid Ahern 	if (IS_ERR(rt->fib6_metrics)) {
3274767a2217SDavid Ahern 		err = PTR_ERR(rt->fib6_metrics);
3275fda21d46SEric Dumazet 		/* Do not leave garbage there. */
3276fda21d46SEric Dumazet 		rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3277767a2217SDavid Ahern 		goto out;
3278767a2217SDavid Ahern 	}
3279767a2217SDavid Ahern 
328093531c67SDavid Ahern 	if (cfg->fc_flags & RTF_ADDRCONF)
328193531c67SDavid Ahern 		rt->dst_nocount = true;
32821da177e4SLinus Torvalds 
32831716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
328414895687SDavid Ahern 		fib6_set_expires(rt, jiffies +
32851716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
32861716a961SGao feng 	else
328714895687SDavid Ahern 		fib6_clean_expires(rt);
32881da177e4SLinus Torvalds 
328986872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
329086872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
329193c2fb25SDavid Ahern 	rt->fib6_protocol = cfg->fc_protocol;
329286872cb5SThomas Graf 
329383c44251SDavid Ahern 	rt->fib6_table = table;
329483c44251SDavid Ahern 	rt->fib6_metric = cfg->fc_metric;
329583c44251SDavid Ahern 	rt->fib6_type = cfg->fc_type;
32962b2450caSDavid Ahern 	rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
329719e42e45SRoopa Prabhu 
329893c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
329993c2fb25SDavid Ahern 	rt->fib6_dst.plen = cfg->fc_dst_len;
330093c2fb25SDavid Ahern 	if (rt->fib6_dst.plen == 128)
33013b6761d1SDavid Ahern 		rt->dst_host = true;
33021da177e4SLinus Torvalds 
33031da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
330493c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
330593c2fb25SDavid Ahern 	rt->fib6_src.plen = cfg->fc_src_len;
33061da177e4SLinus Torvalds #endif
33071cf844c7SDavid Ahern 	err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
33081da177e4SLinus Torvalds 	if (err)
33091da177e4SLinus Torvalds 		goto out;
33109fbb704cSDavid Ahern 
331183c44251SDavid Ahern 	/* We cannot add true routes via loopback here,
331283c44251SDavid Ahern 	 * they would result in kernel looping; promote them to reject routes
331383c44251SDavid Ahern 	 */
331483c44251SDavid Ahern 	addr_type = ipv6_addr_type(&cfg->fc_dst);
33151cf844c7SDavid Ahern 	if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev, addr_type))
331683c44251SDavid Ahern 		rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3317955ec4cbSDavid Ahern 
3318c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
331983c44251SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
332083c44251SDavid Ahern 
3321c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3322d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
3323c3968a85SDaniel Walter 			err = -EINVAL;
3324c3968a85SDaniel Walter 			goto out;
3325c3968a85SDaniel Walter 		}
332693c2fb25SDavid Ahern 		rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
332793c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 128;
3328c3968a85SDaniel Walter 	} else
332993c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
3330c3968a85SDaniel Walter 
33318c5b83f0SRoopa Prabhu 	return rt;
33321da177e4SLinus Torvalds out:
333393531c67SDavid Ahern 	fib6_info_release(rt);
33348c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
33356b9ea5a6SRoopa Prabhu }
33366b9ea5a6SRoopa Prabhu 
3337acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3338333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
33396b9ea5a6SRoopa Prabhu {
33408d1c802bSDavid Ahern 	struct fib6_info *rt;
33416b9ea5a6SRoopa Prabhu 	int err;
33426b9ea5a6SRoopa Prabhu 
3343acb54e3cSDavid Ahern 	rt = ip6_route_info_create(cfg, gfp_flags, extack);
3344d4ead6b3SDavid Ahern 	if (IS_ERR(rt))
3345d4ead6b3SDavid Ahern 		return PTR_ERR(rt);
33466b9ea5a6SRoopa Prabhu 
3347d4ead6b3SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
334893531c67SDavid Ahern 	fib6_info_release(rt);
33496b9ea5a6SRoopa Prabhu 
33501da177e4SLinus Torvalds 	return err;
33511da177e4SLinus Torvalds }
33521da177e4SLinus Torvalds 
33538d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
33541da177e4SLinus Torvalds {
3355afb1d4b5SDavid Ahern 	struct net *net = info->nl_net;
3356c71099acSThomas Graf 	struct fib6_table *table;
3357afb1d4b5SDavid Ahern 	int err;
33581da177e4SLinus Torvalds 
3359421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
33606825a26cSGao feng 		err = -ENOENT;
33616825a26cSGao feng 		goto out;
33626825a26cSGao feng 	}
33636c813a72SPatrick McHardy 
336493c2fb25SDavid Ahern 	table = rt->fib6_table;
336566f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
336686872cb5SThomas Graf 	err = fib6_del(rt, info);
336766f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
33681da177e4SLinus Torvalds 
33696825a26cSGao feng out:
337093531c67SDavid Ahern 	fib6_info_release(rt);
33711da177e4SLinus Torvalds 	return err;
33721da177e4SLinus Torvalds }
33731da177e4SLinus Torvalds 
33748d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt)
3375e0a1ad73SThomas Graf {
3376afb1d4b5SDavid Ahern 	struct nl_info info = { .nl_net = net };
3377afb1d4b5SDavid Ahern 
3378528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
3379e0a1ad73SThomas Graf }
3380e0a1ad73SThomas Graf 
33818d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
33820ae81335SDavid Ahern {
33830ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
3384e3330039SWANG Cong 	struct net *net = info->nl_net;
338516a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
33860ae81335SDavid Ahern 	struct fib6_table *table;
3387e3330039SWANG Cong 	int err = -ENOENT;
33880ae81335SDavid Ahern 
3389421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
3390e3330039SWANG Cong 		goto out_put;
339193c2fb25SDavid Ahern 	table = rt->fib6_table;
339266f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
33930ae81335SDavid Ahern 
339493c2fb25SDavid Ahern 	if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
33958d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
33960ae81335SDavid Ahern 
339716a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
339816a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
339916a16cd3SDavid Ahern 		if (skb) {
340016a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
340116a16cd3SDavid Ahern 
3402d4ead6b3SDavid Ahern 			if (rt6_fill_node(net, skb, rt, NULL,
340316a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
340416a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
340516a16cd3SDavid Ahern 				kfree_skb(skb);
340616a16cd3SDavid Ahern 				skb = NULL;
340716a16cd3SDavid Ahern 			} else
340816a16cd3SDavid Ahern 				info->skip_notify = 1;
340916a16cd3SDavid Ahern 		}
341016a16cd3SDavid Ahern 
34110ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
341293c2fb25SDavid Ahern 					 &rt->fib6_siblings,
341393c2fb25SDavid Ahern 					 fib6_siblings) {
34140ae81335SDavid Ahern 			err = fib6_del(sibling, info);
34150ae81335SDavid Ahern 			if (err)
3416e3330039SWANG Cong 				goto out_unlock;
34170ae81335SDavid Ahern 		}
34180ae81335SDavid Ahern 	}
34190ae81335SDavid Ahern 
34200ae81335SDavid Ahern 	err = fib6_del(rt, info);
3421e3330039SWANG Cong out_unlock:
342266f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
3423e3330039SWANG Cong out_put:
342493531c67SDavid Ahern 	fib6_info_release(rt);
342516a16cd3SDavid Ahern 
342616a16cd3SDavid Ahern 	if (skb) {
3427e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
342816a16cd3SDavid Ahern 			    info->nlh, gfp_any());
342916a16cd3SDavid Ahern 	}
34300ae81335SDavid Ahern 	return err;
34310ae81335SDavid Ahern }
34320ae81335SDavid Ahern 
3433*0fa6efc5SDavid Ahern static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
343423fb93a4SDavid Ahern {
343523fb93a4SDavid Ahern 	int rc = -ESRCH;
343623fb93a4SDavid Ahern 
343723fb93a4SDavid Ahern 	if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
343823fb93a4SDavid Ahern 		goto out;
343923fb93a4SDavid Ahern 
344023fb93a4SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY &&
344123fb93a4SDavid Ahern 	    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
344223fb93a4SDavid Ahern 		goto out;
3443761f6026SXin Long 
344423fb93a4SDavid Ahern 	rc = rt6_remove_exception_rt(rt);
344523fb93a4SDavid Ahern out:
344623fb93a4SDavid Ahern 	return rc;
344723fb93a4SDavid Ahern }
344823fb93a4SDavid Ahern 
3449*0fa6efc5SDavid Ahern static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
3450*0fa6efc5SDavid Ahern 			     struct fib6_nh *nh)
3451*0fa6efc5SDavid Ahern {
3452*0fa6efc5SDavid Ahern 	struct fib6_result res = {
3453*0fa6efc5SDavid Ahern 		.f6i = rt,
3454*0fa6efc5SDavid Ahern 		.nh = nh,
3455*0fa6efc5SDavid Ahern 	};
3456*0fa6efc5SDavid Ahern 	struct rt6_info *rt_cache;
3457*0fa6efc5SDavid Ahern 
3458*0fa6efc5SDavid Ahern 	rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
3459*0fa6efc5SDavid Ahern 	if (rt_cache)
3460*0fa6efc5SDavid Ahern 		return __ip6_del_cached_rt(rt_cache, cfg);
3461*0fa6efc5SDavid Ahern 
3462*0fa6efc5SDavid Ahern 	return 0;
3463*0fa6efc5SDavid Ahern }
3464*0fa6efc5SDavid Ahern 
3465333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
3466333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
34671da177e4SLinus Torvalds {
3468c71099acSThomas Graf 	struct fib6_table *table;
34698d1c802bSDavid Ahern 	struct fib6_info *rt;
34701da177e4SLinus Torvalds 	struct fib6_node *fn;
34711da177e4SLinus Torvalds 	int err = -ESRCH;
34721da177e4SLinus Torvalds 
34735578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3474d5d531cbSDavid Ahern 	if (!table) {
3475d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
3476c71099acSThomas Graf 		return err;
3477d5d531cbSDavid Ahern 	}
34781da177e4SLinus Torvalds 
347966f5d6ceSWei Wang 	rcu_read_lock();
3480c71099acSThomas Graf 
3481c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
348286872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
348338fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
34842b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
34851da177e4SLinus Torvalds 
34861da177e4SLinus Torvalds 	if (fn) {
348766f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
3488ad1601aeSDavid Ahern 			struct fib6_nh *nh;
3489ad1601aeSDavid Ahern 
34901cf844c7SDavid Ahern 			nh = rt->fib6_nh;
34912b760fcfSWei Wang 			if (cfg->fc_flags & RTF_CACHE) {
349223fb93a4SDavid Ahern 				int rc;
349323fb93a4SDavid Ahern 
3494*0fa6efc5SDavid Ahern 				rc = ip6_del_cached_rt(cfg, rt, nh);
34959e575010SEric Dumazet 				if (rc != -ESRCH) {
34969e575010SEric Dumazet 					rcu_read_unlock();
349723fb93a4SDavid Ahern 					return rc;
349823fb93a4SDavid Ahern 				}
34991f56a01fSMartin KaFai Lau 				continue;
35002b760fcfSWei Wang 			}
3501ad1601aeSDavid Ahern 
350286872cb5SThomas Graf 			if (cfg->fc_ifindex &&
3503ad1601aeSDavid Ahern 			    (!nh->fib_nh_dev ||
3504ad1601aeSDavid Ahern 			     nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
35051da177e4SLinus Torvalds 				continue;
350686872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
3507ad1601aeSDavid Ahern 			    !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
35081da177e4SLinus Torvalds 				continue;
350993c2fb25SDavid Ahern 			if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
35101da177e4SLinus Torvalds 				continue;
351193c2fb25SDavid Ahern 			if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
3512c2ed1880SMantas M 				continue;
3513e873e4b9SWei Wang 			if (!fib6_info_hold_safe(rt))
3514e873e4b9SWei Wang 				continue;
351566f5d6ceSWei Wang 			rcu_read_unlock();
35161da177e4SLinus Torvalds 
35170ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
35180ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
351986872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
35200ae81335SDavid Ahern 
35210ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
35221da177e4SLinus Torvalds 		}
35231da177e4SLinus Torvalds 	}
352466f5d6ceSWei Wang 	rcu_read_unlock();
35251da177e4SLinus Torvalds 
35261da177e4SLinus Torvalds 	return err;
35271da177e4SLinus Torvalds }
35281da177e4SLinus Torvalds 
35296700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3530a6279458SYOSHIFUJI Hideaki {
3531a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
3532e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
353385bd05deSDavid Ahern 	struct fib6_result res = {};
3534e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
3535e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
3536e8599ff4SDavid S. Miller 	struct neighbour *neigh;
353771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
35386e157b6aSDavid S. Miller 	int optlen, on_link;
35396e157b6aSDavid S. Miller 	u8 *lladdr;
3540e8599ff4SDavid S. Miller 
354129a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
354271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
3543e8599ff4SDavid S. Miller 
3544e8599ff4SDavid S. Miller 	if (optlen < 0) {
35456e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3546e8599ff4SDavid S. Miller 		return;
3547e8599ff4SDavid S. Miller 	}
3548e8599ff4SDavid S. Miller 
354971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
3550e8599ff4SDavid S. Miller 
355171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
35526e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3553e8599ff4SDavid S. Miller 		return;
3554e8599ff4SDavid S. Miller 	}
3555e8599ff4SDavid S. Miller 
35566e157b6aSDavid S. Miller 	on_link = 0;
355771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
3558e8599ff4SDavid S. Miller 		on_link = 1;
355971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
3560e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
35616e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
3562e8599ff4SDavid S. Miller 		return;
3563e8599ff4SDavid S. Miller 	}
3564e8599ff4SDavid S. Miller 
3565e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
3566e8599ff4SDavid S. Miller 	if (!in6_dev)
3567e8599ff4SDavid S. Miller 		return;
3568e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3569e8599ff4SDavid S. Miller 		return;
3570e8599ff4SDavid S. Miller 
3571e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
3572e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
3573e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
3574e8599ff4SDavid S. Miller 	 */
3575e8599ff4SDavid S. Miller 
3576f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
3577e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3578e8599ff4SDavid S. Miller 		return;
3579e8599ff4SDavid S. Miller 	}
35806e157b6aSDavid S. Miller 
35816e157b6aSDavid S. Miller 	lladdr = NULL;
3582e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
3583e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3584e8599ff4SDavid S. Miller 					     skb->dev);
3585e8599ff4SDavid S. Miller 		if (!lladdr) {
3586e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3587e8599ff4SDavid S. Miller 			return;
3588e8599ff4SDavid S. Miller 		}
3589e8599ff4SDavid S. Miller 	}
3590e8599ff4SDavid S. Miller 
35916e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
3592ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
35936e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
35946e157b6aSDavid S. Miller 		return;
35956e157b6aSDavid S. Miller 	}
35966e157b6aSDavid S. Miller 
35976e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
35986e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
35996e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
36006e157b6aSDavid S. Miller 	 */
36010dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
36026e157b6aSDavid S. Miller 
360371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
3604e8599ff4SDavid S. Miller 	if (!neigh)
3605e8599ff4SDavid S. Miller 		return;
3606e8599ff4SDavid S. Miller 
36071da177e4SLinus Torvalds 	/*
36081da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
36091da177e4SLinus Torvalds 	 */
36101da177e4SLinus Torvalds 
3611f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
36121da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
36131da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
36141da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
3615f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
3616f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
36171da177e4SLinus Torvalds 
36184d85cd0cSDavid Ahern 	rcu_read_lock();
361985bd05deSDavid Ahern 	res.f6i = rcu_dereference(rt->from);
3620ff24e498SDavid S. Miller 	if (!res.f6i)
3621886b7a50SMartin KaFai Lau 		goto out;
36228a14e46fSDavid Ahern 
36231cf844c7SDavid Ahern 	res.nh = res.f6i->fib6_nh;
36247d21fec9SDavid Ahern 	res.fib6_flags = res.f6i->fib6_flags;
36257d21fec9SDavid Ahern 	res.fib6_type = res.f6i->fib6_type;
362685bd05deSDavid Ahern 	nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
362738308473SDavid S. Miller 	if (!nrt)
36281da177e4SLinus Torvalds 		goto out;
36291da177e4SLinus Torvalds 
36301da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
36311da177e4SLinus Torvalds 	if (on_link)
36321da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
36331da177e4SLinus Torvalds 
36344e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
36351da177e4SLinus Torvalds 
3636886b7a50SMartin KaFai Lau 	/* rt6_insert_exception() will take care of duplicated exceptions */
36375012f0a5SDavid Ahern 	if (rt6_insert_exception(nrt, &res)) {
36382b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
36392b760fcfSWei Wang 		goto out;
36402b760fcfSWei Wang 	}
36411da177e4SLinus Torvalds 
3642d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
3643d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
364471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
364560592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
36468d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
36478d71740cSTom Tucker 
36481da177e4SLinus Torvalds out:
3649886b7a50SMartin KaFai Lau 	rcu_read_unlock();
3650e8599ff4SDavid S. Miller 	neigh_release(neigh);
36516e157b6aSDavid S. Miller }
36526e157b6aSDavid S. Miller 
365370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
36548d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
3655b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3656830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3657830218c1SDavid Ahern 					   struct net_device *dev)
365870ceb4f5SYOSHIFUJI Hideaki {
3659830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3660830218c1SDavid Ahern 	int ifindex = dev->ifindex;
366170ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
36628d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3663c71099acSThomas Graf 	struct fib6_table *table;
366470ceb4f5SYOSHIFUJI Hideaki 
3665830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
366638308473SDavid S. Miller 	if (!table)
3667c71099acSThomas Graf 		return NULL;
3668c71099acSThomas Graf 
366966f5d6ceSWei Wang 	rcu_read_lock();
367038fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
367170ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
367270ceb4f5SYOSHIFUJI Hideaki 		goto out;
367370ceb4f5SYOSHIFUJI Hideaki 
367466f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
36751cf844c7SDavid Ahern 		if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
367670ceb4f5SYOSHIFUJI Hideaki 			continue;
36772b2450caSDavid Ahern 		if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
36781cf844c7SDavid Ahern 		    !rt->fib6_nh->fib_nh_gw_family)
367970ceb4f5SYOSHIFUJI Hideaki 			continue;
36801cf844c7SDavid Ahern 		if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
368170ceb4f5SYOSHIFUJI Hideaki 			continue;
3682e873e4b9SWei Wang 		if (!fib6_info_hold_safe(rt))
3683e873e4b9SWei Wang 			continue;
368470ceb4f5SYOSHIFUJI Hideaki 		break;
368570ceb4f5SYOSHIFUJI Hideaki 	}
368670ceb4f5SYOSHIFUJI Hideaki out:
368766f5d6ceSWei Wang 	rcu_read_unlock();
368870ceb4f5SYOSHIFUJI Hideaki 	return rt;
368970ceb4f5SYOSHIFUJI Hideaki }
369070ceb4f5SYOSHIFUJI Hideaki 
36918d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
3692b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3693830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3694830218c1SDavid Ahern 					   struct net_device *dev,
369595c96174SEric Dumazet 					   unsigned int pref)
369670ceb4f5SYOSHIFUJI Hideaki {
369786872cb5SThomas Graf 	struct fib6_config cfg = {
3698238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
3699830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
370086872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
370186872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
370286872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
3703b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3704e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
370515e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
3706efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3707efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
370886872cb5SThomas Graf 	};
370970ceb4f5SYOSHIFUJI Hideaki 
3710830218c1SDavid Ahern 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
37114e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
37124e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
371386872cb5SThomas Graf 
3714e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
3715e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
371686872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
371770ceb4f5SYOSHIFUJI Hideaki 
3718acb54e3cSDavid Ahern 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
371970ceb4f5SYOSHIFUJI Hideaki 
3720830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
372170ceb4f5SYOSHIFUJI Hideaki }
372270ceb4f5SYOSHIFUJI Hideaki #endif
372370ceb4f5SYOSHIFUJI Hideaki 
37248d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net,
3725afb1d4b5SDavid Ahern 				     const struct in6_addr *addr,
3726afb1d4b5SDavid Ahern 				     struct net_device *dev)
37271da177e4SLinus Torvalds {
3728830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
37298d1c802bSDavid Ahern 	struct fib6_info *rt;
3730c71099acSThomas Graf 	struct fib6_table *table;
37311da177e4SLinus Torvalds 
3732afb1d4b5SDavid Ahern 	table = fib6_get_table(net, tb_id);
373338308473SDavid S. Miller 	if (!table)
3734c71099acSThomas Graf 		return NULL;
37351da177e4SLinus Torvalds 
373666f5d6ceSWei Wang 	rcu_read_lock();
373766f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
37381cf844c7SDavid Ahern 		struct fib6_nh *nh = rt->fib6_nh;
3739ad1601aeSDavid Ahern 
3740ad1601aeSDavid Ahern 		if (dev == nh->fib_nh_dev &&
374193c2fb25SDavid Ahern 		    ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
3742ad1601aeSDavid Ahern 		    ipv6_addr_equal(&nh->fib_nh_gw6, addr))
37431da177e4SLinus Torvalds 			break;
37441da177e4SLinus Torvalds 	}
3745e873e4b9SWei Wang 	if (rt && !fib6_info_hold_safe(rt))
3746e873e4b9SWei Wang 		rt = NULL;
374766f5d6ceSWei Wang 	rcu_read_unlock();
37481da177e4SLinus Torvalds 	return rt;
37491da177e4SLinus Torvalds }
37501da177e4SLinus Torvalds 
37518d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net,
3752afb1d4b5SDavid Ahern 				     const struct in6_addr *gwaddr,
3753ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
3754ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
37551da177e4SLinus Torvalds {
375686872cb5SThomas Graf 	struct fib6_config cfg = {
3757ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
3758238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
375986872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
376086872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
376186872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
3762b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3763e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
376415e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
37655578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3766afb1d4b5SDavid Ahern 		.fc_nlinfo.nl_net = net,
376786872cb5SThomas Graf 	};
37681da177e4SLinus Torvalds 
37694e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
37701da177e4SLinus Torvalds 
3771acb54e3cSDavid Ahern 	if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
3772830218c1SDavid Ahern 		struct fib6_table *table;
3773830218c1SDavid Ahern 
3774830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
3775830218c1SDavid Ahern 		if (table)
3776830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3777830218c1SDavid Ahern 	}
37781da177e4SLinus Torvalds 
3779afb1d4b5SDavid Ahern 	return rt6_get_dflt_router(net, gwaddr, dev);
37801da177e4SLinus Torvalds }
37811da177e4SLinus Torvalds 
3782afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net,
3783afb1d4b5SDavid Ahern 				     struct fib6_table *table)
37841da177e4SLinus Torvalds {
37858d1c802bSDavid Ahern 	struct fib6_info *rt;
37861da177e4SLinus Torvalds 
37871da177e4SLinus Torvalds restart:
378866f5d6ceSWei Wang 	rcu_read_lock();
378966f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
3790dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
3791dcd1f572SDavid Ahern 		struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3792dcd1f572SDavid Ahern 
379393c2fb25SDavid Ahern 		if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
3794e873e4b9SWei Wang 		    (!idev || idev->cnf.accept_ra != 2) &&
3795e873e4b9SWei Wang 		    fib6_info_hold_safe(rt)) {
379666f5d6ceSWei Wang 			rcu_read_unlock();
3797afb1d4b5SDavid Ahern 			ip6_del_rt(net, rt);
37981da177e4SLinus Torvalds 			goto restart;
37991da177e4SLinus Torvalds 		}
38001da177e4SLinus Torvalds 	}
380166f5d6ceSWei Wang 	rcu_read_unlock();
3802830218c1SDavid Ahern 
3803830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3804830218c1SDavid Ahern }
3805830218c1SDavid Ahern 
3806830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
3807830218c1SDavid Ahern {
3808830218c1SDavid Ahern 	struct fib6_table *table;
3809830218c1SDavid Ahern 	struct hlist_head *head;
3810830218c1SDavid Ahern 	unsigned int h;
3811830218c1SDavid Ahern 
3812830218c1SDavid Ahern 	rcu_read_lock();
3813830218c1SDavid Ahern 
3814830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3815830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
3816830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3817830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3818afb1d4b5SDavid Ahern 				__rt6_purge_dflt_routers(net, table);
3819830218c1SDavid Ahern 		}
3820830218c1SDavid Ahern 	}
3821830218c1SDavid Ahern 
3822830218c1SDavid Ahern 	rcu_read_unlock();
38231da177e4SLinus Torvalds }
38241da177e4SLinus Torvalds 
38255578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
38265578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
382786872cb5SThomas Graf 				 struct fib6_config *cfg)
382886872cb5SThomas Graf {
38298823a3acSMaciej Żenczykowski 	*cfg = (struct fib6_config){
38308823a3acSMaciej Żenczykowski 		.fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
38318823a3acSMaciej Żenczykowski 			 : RT6_TABLE_MAIN,
38328823a3acSMaciej Żenczykowski 		.fc_ifindex = rtmsg->rtmsg_ifindex,
383367f69513SDavid Ahern 		.fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
38348823a3acSMaciej Żenczykowski 		.fc_expires = rtmsg->rtmsg_info,
38358823a3acSMaciej Żenczykowski 		.fc_dst_len = rtmsg->rtmsg_dst_len,
38368823a3acSMaciej Żenczykowski 		.fc_src_len = rtmsg->rtmsg_src_len,
38378823a3acSMaciej Żenczykowski 		.fc_flags = rtmsg->rtmsg_flags,
38388823a3acSMaciej Żenczykowski 		.fc_type = rtmsg->rtmsg_type,
383986872cb5SThomas Graf 
38408823a3acSMaciej Żenczykowski 		.fc_nlinfo.nl_net = net,
384186872cb5SThomas Graf 
38428823a3acSMaciej Żenczykowski 		.fc_dst = rtmsg->rtmsg_dst,
38438823a3acSMaciej Żenczykowski 		.fc_src = rtmsg->rtmsg_src,
38448823a3acSMaciej Żenczykowski 		.fc_gateway = rtmsg->rtmsg_gateway,
38458823a3acSMaciej Żenczykowski 	};
384686872cb5SThomas Graf }
384786872cb5SThomas Graf 
38485578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
38491da177e4SLinus Torvalds {
385086872cb5SThomas Graf 	struct fib6_config cfg;
38511da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
38521da177e4SLinus Torvalds 	int err;
38531da177e4SLinus Torvalds 
38541da177e4SLinus Torvalds 	switch (cmd) {
38551da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
38561da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
3857af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
38581da177e4SLinus Torvalds 			return -EPERM;
38591da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
38601da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
38611da177e4SLinus Torvalds 		if (err)
38621da177e4SLinus Torvalds 			return -EFAULT;
38631da177e4SLinus Torvalds 
38645578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
386586872cb5SThomas Graf 
38661da177e4SLinus Torvalds 		rtnl_lock();
38671da177e4SLinus Torvalds 		switch (cmd) {
38681da177e4SLinus Torvalds 		case SIOCADDRT:
3869acb54e3cSDavid Ahern 			err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
38701da177e4SLinus Torvalds 			break;
38711da177e4SLinus Torvalds 		case SIOCDELRT:
3872333c4301SDavid Ahern 			err = ip6_route_del(&cfg, NULL);
38731da177e4SLinus Torvalds 			break;
38741da177e4SLinus Torvalds 		default:
38751da177e4SLinus Torvalds 			err = -EINVAL;
38761da177e4SLinus Torvalds 		}
38771da177e4SLinus Torvalds 		rtnl_unlock();
38781da177e4SLinus Torvalds 
38791da177e4SLinus Torvalds 		return err;
38803ff50b79SStephen Hemminger 	}
38811da177e4SLinus Torvalds 
38821da177e4SLinus Torvalds 	return -EINVAL;
38831da177e4SLinus Torvalds }
38841da177e4SLinus Torvalds 
38851da177e4SLinus Torvalds /*
38861da177e4SLinus Torvalds  *	Drop the packet on the floor
38871da177e4SLinus Torvalds  */
38881da177e4SLinus Torvalds 
3889d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
38901da177e4SLinus Torvalds {
3891adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
38921d3fd8a1SStephen Suryaputra 	struct net *net = dev_net(dst->dev);
38931d3fd8a1SStephen Suryaputra 	struct inet6_dev *idev;
38941d3fd8a1SStephen Suryaputra 	int type;
38951d3fd8a1SStephen Suryaputra 
38961d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev) &&
38971d3fd8a1SStephen Suryaputra 	    dst->dev == net->loopback_dev)
38981d3fd8a1SStephen Suryaputra 		idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
38991d3fd8a1SStephen Suryaputra 	else
39001d3fd8a1SStephen Suryaputra 		idev = ip6_dst_idev(dst);
39011d3fd8a1SStephen Suryaputra 
3902612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
3903612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
39040660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
390545bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
39061d3fd8a1SStephen Suryaputra 			IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
3907612f09e8SYOSHIFUJI Hideaki 			break;
3908612f09e8SYOSHIFUJI Hideaki 		}
3909612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
3910612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
39111d3fd8a1SStephen Suryaputra 		IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
3912612f09e8SYOSHIFUJI Hideaki 		break;
3913612f09e8SYOSHIFUJI Hideaki 	}
39141d3fd8a1SStephen Suryaputra 
39151d3fd8a1SStephen Suryaputra 	/* Start over by dropping the dst for l3mdev case */
39161d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev))
39171d3fd8a1SStephen Suryaputra 		skb_dst_drop(skb);
39181d3fd8a1SStephen Suryaputra 
39193ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
39201da177e4SLinus Torvalds 	kfree_skb(skb);
39211da177e4SLinus Torvalds 	return 0;
39221da177e4SLinus Torvalds }
39231da177e4SLinus Torvalds 
39249ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
39259ce8ade0SThomas Graf {
3926612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
39279ce8ade0SThomas Graf }
39289ce8ade0SThomas Graf 
3929ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
39301da177e4SLinus Torvalds {
3931adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3932612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
39331da177e4SLinus Torvalds }
39341da177e4SLinus Torvalds 
39359ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
39369ce8ade0SThomas Graf {
3937612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
39389ce8ade0SThomas Graf }
39399ce8ade0SThomas Graf 
3940ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
39419ce8ade0SThomas Graf {
3942adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3943612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
39449ce8ade0SThomas Graf }
39459ce8ade0SThomas Graf 
39461da177e4SLinus Torvalds /*
39471da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
39481da177e4SLinus Torvalds  */
39491da177e4SLinus Torvalds 
3950360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net,
3951afb1d4b5SDavid Ahern 				     struct inet6_dev *idev,
39521da177e4SLinus Torvalds 				     const struct in6_addr *addr,
3953acb54e3cSDavid Ahern 				     bool anycast, gfp_t gfp_flags)
39541da177e4SLinus Torvalds {
3955c7a1ce39SDavid Ahern 	struct fib6_config cfg = {
3956c7a1ce39SDavid Ahern 		.fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
3957c7a1ce39SDavid Ahern 		.fc_ifindex = idev->dev->ifindex,
3958c7a1ce39SDavid Ahern 		.fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP,
3959c7a1ce39SDavid Ahern 		.fc_dst = *addr,
3960c7a1ce39SDavid Ahern 		.fc_dst_len = 128,
3961c7a1ce39SDavid Ahern 		.fc_protocol = RTPROT_KERNEL,
3962c7a1ce39SDavid Ahern 		.fc_nlinfo.nl_net = net,
3963c7a1ce39SDavid Ahern 		.fc_ignore_dev_down = true,
3964c7a1ce39SDavid Ahern 	};
39655f02ce24SDavid Ahern 
3966e8478e80SDavid Ahern 	if (anycast) {
3967c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_ANYCAST;
3968c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_ANYCAST;
3969e8478e80SDavid Ahern 	} else {
3970c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_LOCAL;
3971c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_LOCAL;
3972e8478e80SDavid Ahern 	}
39731da177e4SLinus Torvalds 
3974c7a1ce39SDavid Ahern 	return ip6_route_info_create(&cfg, gfp_flags, NULL);
39751da177e4SLinus Torvalds }
39761da177e4SLinus Torvalds 
3977c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
3978c3968a85SDaniel Walter struct arg_dev_net_ip {
3979c3968a85SDaniel Walter 	struct net_device *dev;
3980c3968a85SDaniel Walter 	struct net *net;
3981c3968a85SDaniel Walter 	struct in6_addr *addr;
3982c3968a85SDaniel Walter };
3983c3968a85SDaniel Walter 
39848d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
3985c3968a85SDaniel Walter {
3986c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3987c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3988c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3989c3968a85SDaniel Walter 
39901cf844c7SDavid Ahern 	if (((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
3991421842edSDavid Ahern 	    rt != net->ipv6.fib6_null_entry &&
399293c2fb25SDavid Ahern 	    ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
399360006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
3994c3968a85SDaniel Walter 		/* remove prefsrc entry */
399593c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
399660006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
3997c3968a85SDaniel Walter 	}
3998c3968a85SDaniel Walter 	return 0;
3999c3968a85SDaniel Walter }
4000c3968a85SDaniel Walter 
4001c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4002c3968a85SDaniel Walter {
4003c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
4004c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
4005c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
4006c3968a85SDaniel Walter 		.net = net,
4007c3968a85SDaniel Walter 		.addr = &ifp->addr,
4008c3968a85SDaniel Walter 	};
40090c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4010c3968a85SDaniel Walter }
4011c3968a85SDaniel Walter 
40122b2450caSDavid Ahern #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT)
4013be7a010dSDuan Jiong 
4014be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
40158d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4016be7a010dSDuan Jiong {
4017be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
40181cf844c7SDavid Ahern 	struct fib6_nh *nh = rt->fib6_nh;
4019be7a010dSDuan Jiong 
402093c2fb25SDavid Ahern 	if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
4021cc5c073aSDavid Ahern 	    nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
4022be7a010dSDuan Jiong 		return -1;
4023b16cb459SWei Wang 
4024b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
4025b16cb459SWei Wang 	 * This is needed because cached route may have a different
4026b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
4027b16cb459SWei Wang 	 */
4028cc5c073aSDavid Ahern 	fib6_nh_exceptions_clean_tohost(nh, gateway);
4029b16cb459SWei Wang 
4030be7a010dSDuan Jiong 	return 0;
4031be7a010dSDuan Jiong }
4032be7a010dSDuan Jiong 
4033be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4034be7a010dSDuan Jiong {
4035be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
4036be7a010dSDuan Jiong }
4037be7a010dSDuan Jiong 
40382127d95aSIdo Schimmel struct arg_netdev_event {
40392127d95aSIdo Schimmel 	const struct net_device *dev;
40404c981e28SIdo Schimmel 	union {
4041ecc5663cSDavid Ahern 		unsigned char nh_flags;
40424c981e28SIdo Schimmel 		unsigned long event;
40434c981e28SIdo Schimmel 	};
40442127d95aSIdo Schimmel };
40452127d95aSIdo Schimmel 
40468d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4047d7dedee1SIdo Schimmel {
40488d1c802bSDavid Ahern 	struct fib6_info *iter;
4049d7dedee1SIdo Schimmel 	struct fib6_node *fn;
4050d7dedee1SIdo Schimmel 
405193c2fb25SDavid Ahern 	fn = rcu_dereference_protected(rt->fib6_node,
405293c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
4053d7dedee1SIdo Schimmel 	iter = rcu_dereference_protected(fn->leaf,
405493c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
4055d7dedee1SIdo Schimmel 	while (iter) {
405693c2fb25SDavid Ahern 		if (iter->fib6_metric == rt->fib6_metric &&
405733bd5ac5SDavid Ahern 		    rt6_qualify_for_ecmp(iter))
4058d7dedee1SIdo Schimmel 			return iter;
40598fb11a9aSDavid Ahern 		iter = rcu_dereference_protected(iter->fib6_next,
406093c2fb25SDavid Ahern 				lockdep_is_held(&rt->fib6_table->tb6_lock));
4061d7dedee1SIdo Schimmel 	}
4062d7dedee1SIdo Schimmel 
4063d7dedee1SIdo Schimmel 	return NULL;
4064d7dedee1SIdo Schimmel }
4065d7dedee1SIdo Schimmel 
40668d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt)
4067d7dedee1SIdo Schimmel {
40681cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
40691cf844c7SDavid Ahern 	    (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
40701cf844c7SDavid Ahern 	     ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
4071d7dedee1SIdo Schimmel 		return true;
4072d7dedee1SIdo Schimmel 
4073d7dedee1SIdo Schimmel 	return false;
4074d7dedee1SIdo Schimmel }
4075d7dedee1SIdo Schimmel 
40768d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt)
4077d7dedee1SIdo Schimmel {
40788d1c802bSDavid Ahern 	struct fib6_info *iter;
4079d7dedee1SIdo Schimmel 	int total = 0;
4080d7dedee1SIdo Schimmel 
4081d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt))
40821cf844c7SDavid Ahern 		total += rt->fib6_nh->fib_nh_weight;
4083d7dedee1SIdo Schimmel 
408493c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4085d7dedee1SIdo Schimmel 		if (!rt6_is_dead(iter))
40861cf844c7SDavid Ahern 			total += iter->fib6_nh->fib_nh_weight;
4087d7dedee1SIdo Schimmel 	}
4088d7dedee1SIdo Schimmel 
4089d7dedee1SIdo Schimmel 	return total;
4090d7dedee1SIdo Schimmel }
4091d7dedee1SIdo Schimmel 
40928d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4093d7dedee1SIdo Schimmel {
4094d7dedee1SIdo Schimmel 	int upper_bound = -1;
4095d7dedee1SIdo Schimmel 
4096d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt)) {
40971cf844c7SDavid Ahern 		*weight += rt->fib6_nh->fib_nh_weight;
4098d7dedee1SIdo Schimmel 		upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4099d7dedee1SIdo Schimmel 						    total) - 1;
4100d7dedee1SIdo Schimmel 	}
41011cf844c7SDavid Ahern 	atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
4102d7dedee1SIdo Schimmel }
4103d7dedee1SIdo Schimmel 
41048d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4105d7dedee1SIdo Schimmel {
41068d1c802bSDavid Ahern 	struct fib6_info *iter;
4107d7dedee1SIdo Schimmel 	int weight = 0;
4108d7dedee1SIdo Schimmel 
4109d7dedee1SIdo Schimmel 	rt6_upper_bound_set(rt, &weight, total);
4110d7dedee1SIdo Schimmel 
411193c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4112d7dedee1SIdo Schimmel 		rt6_upper_bound_set(iter, &weight, total);
4113d7dedee1SIdo Schimmel }
4114d7dedee1SIdo Schimmel 
41158d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt)
4116d7dedee1SIdo Schimmel {
41178d1c802bSDavid Ahern 	struct fib6_info *first;
4118d7dedee1SIdo Schimmel 	int total;
4119d7dedee1SIdo Schimmel 
4120d7dedee1SIdo Schimmel 	/* In case the entire multipath route was marked for flushing,
4121d7dedee1SIdo Schimmel 	 * then there is no need to rebalance upon the removal of every
4122d7dedee1SIdo Schimmel 	 * sibling route.
4123d7dedee1SIdo Schimmel 	 */
412493c2fb25SDavid Ahern 	if (!rt->fib6_nsiblings || rt->should_flush)
4125d7dedee1SIdo Schimmel 		return;
4126d7dedee1SIdo Schimmel 
4127d7dedee1SIdo Schimmel 	/* During lookup routes are evaluated in order, so we need to
4128d7dedee1SIdo Schimmel 	 * make sure upper bounds are assigned from the first sibling
4129d7dedee1SIdo Schimmel 	 * onwards.
4130d7dedee1SIdo Schimmel 	 */
4131d7dedee1SIdo Schimmel 	first = rt6_multipath_first_sibling(rt);
4132d7dedee1SIdo Schimmel 	if (WARN_ON_ONCE(!first))
4133d7dedee1SIdo Schimmel 		return;
4134d7dedee1SIdo Schimmel 
4135d7dedee1SIdo Schimmel 	total = rt6_multipath_total_weight(first);
4136d7dedee1SIdo Schimmel 	rt6_multipath_upper_bound_set(first, total);
4137d7dedee1SIdo Schimmel }
4138d7dedee1SIdo Schimmel 
41398d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg)
41402127d95aSIdo Schimmel {
41412127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
41427aef6859SDavid Ahern 	struct net *net = dev_net(arg->dev);
41432127d95aSIdo Schimmel 
4144ad1601aeSDavid Ahern 	if (rt != net->ipv6.fib6_null_entry &&
41451cf844c7SDavid Ahern 	    rt->fib6_nh->fib_nh_dev == arg->dev) {
41461cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
41477aef6859SDavid Ahern 		fib6_update_sernum_upto_root(net, rt);
4148d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
41491de178edSIdo Schimmel 	}
41502127d95aSIdo Schimmel 
41512127d95aSIdo Schimmel 	return 0;
41522127d95aSIdo Schimmel }
41532127d95aSIdo Schimmel 
4154ecc5663cSDavid Ahern void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
41552127d95aSIdo Schimmel {
41562127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
41572127d95aSIdo Schimmel 		.dev = dev,
41586802f3adSIdo Schimmel 		{
41592127d95aSIdo Schimmel 			.nh_flags = nh_flags,
41606802f3adSIdo Schimmel 		},
41612127d95aSIdo Schimmel 	};
41622127d95aSIdo Schimmel 
41632127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
41642127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
41652127d95aSIdo Schimmel 
41662127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
41672127d95aSIdo Schimmel }
41682127d95aSIdo Schimmel 
41698d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
41701de178edSIdo Schimmel 				   const struct net_device *dev)
41711de178edSIdo Schimmel {
41728d1c802bSDavid Ahern 	struct fib6_info *iter;
41731de178edSIdo Schimmel 
41741cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == dev)
41751de178edSIdo Schimmel 		return true;
417693c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
41771cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == dev)
41781de178edSIdo Schimmel 			return true;
41791de178edSIdo Schimmel 
41801de178edSIdo Schimmel 	return false;
41811de178edSIdo Schimmel }
41821de178edSIdo Schimmel 
41838d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt)
41841de178edSIdo Schimmel {
41858d1c802bSDavid Ahern 	struct fib6_info *iter;
41861de178edSIdo Schimmel 
41871de178edSIdo Schimmel 	rt->should_flush = 1;
418893c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
41891de178edSIdo Schimmel 		iter->should_flush = 1;
41901de178edSIdo Schimmel }
41911de178edSIdo Schimmel 
41928d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
41931de178edSIdo Schimmel 					     const struct net_device *down_dev)
41941de178edSIdo Schimmel {
41958d1c802bSDavid Ahern 	struct fib6_info *iter;
41961de178edSIdo Schimmel 	unsigned int dead = 0;
41971de178edSIdo Schimmel 
41981cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == down_dev ||
41991cf844c7SDavid Ahern 	    rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
42001de178edSIdo Schimmel 		dead++;
420193c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
42021cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == down_dev ||
42031cf844c7SDavid Ahern 		    iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
42041de178edSIdo Schimmel 			dead++;
42051de178edSIdo Schimmel 
42061de178edSIdo Schimmel 	return dead;
42071de178edSIdo Schimmel }
42081de178edSIdo Schimmel 
42098d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
42101de178edSIdo Schimmel 				       const struct net_device *dev,
4211ecc5663cSDavid Ahern 				       unsigned char nh_flags)
42121de178edSIdo Schimmel {
42138d1c802bSDavid Ahern 	struct fib6_info *iter;
42141de178edSIdo Schimmel 
42151cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == dev)
42161cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags |= nh_flags;
421793c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
42181cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == dev)
42191cf844c7SDavid Ahern 			iter->fib6_nh->fib_nh_flags |= nh_flags;
42201de178edSIdo Schimmel }
42211de178edSIdo Schimmel 
4222a1a22c12SDavid Ahern /* called with write lock held for table with rt */
42238d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
42241da177e4SLinus Torvalds {
42254c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
42264c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
42277aef6859SDavid Ahern 	struct net *net = dev_net(dev);
42288ed67789SDaniel Lezcano 
4229421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
423027c6fa73SIdo Schimmel 		return 0;
423127c6fa73SIdo Schimmel 
423227c6fa73SIdo Schimmel 	switch (arg->event) {
423327c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
42341cf844c7SDavid Ahern 		return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
423527c6fa73SIdo Schimmel 	case NETDEV_DOWN:
42361de178edSIdo Schimmel 		if (rt->should_flush)
423727c6fa73SIdo Schimmel 			return -1;
423893c2fb25SDavid Ahern 		if (!rt->fib6_nsiblings)
42391cf844c7SDavid Ahern 			return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
42401de178edSIdo Schimmel 		if (rt6_multipath_uses_dev(rt, dev)) {
42411de178edSIdo Schimmel 			unsigned int count;
42421de178edSIdo Schimmel 
42431de178edSIdo Schimmel 			count = rt6_multipath_dead_count(rt, dev);
424493c2fb25SDavid Ahern 			if (rt->fib6_nsiblings + 1 == count) {
42451de178edSIdo Schimmel 				rt6_multipath_flush(rt);
42461de178edSIdo Schimmel 				return -1;
42471de178edSIdo Schimmel 			}
42481de178edSIdo Schimmel 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
42491de178edSIdo Schimmel 						   RTNH_F_LINKDOWN);
42507aef6859SDavid Ahern 			fib6_update_sernum(net, rt);
4251d7dedee1SIdo Schimmel 			rt6_multipath_rebalance(rt);
42521de178edSIdo Schimmel 		}
42531de178edSIdo Schimmel 		return -2;
425427c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
42551cf844c7SDavid Ahern 		if (rt->fib6_nh->fib_nh_dev != dev ||
425693c2fb25SDavid Ahern 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
425727c6fa73SIdo Schimmel 			break;
42581cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
4259d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
426027c6fa73SIdo Schimmel 		break;
42612b241361SIdo Schimmel 	}
4262c159d30cSDavid S. Miller 
42631da177e4SLinus Torvalds 	return 0;
42641da177e4SLinus Torvalds }
42651da177e4SLinus Torvalds 
426627c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
42671da177e4SLinus Torvalds {
42684c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
42698ed67789SDaniel Lezcano 		.dev = dev,
42706802f3adSIdo Schimmel 		{
42714c981e28SIdo Schimmel 			.event = event,
42726802f3adSIdo Schimmel 		},
42738ed67789SDaniel Lezcano 	};
42747c6bb7d2SDavid Ahern 	struct net *net = dev_net(dev);
42758ed67789SDaniel Lezcano 
42767c6bb7d2SDavid Ahern 	if (net->ipv6.sysctl.skip_notify_on_dev_down)
42777c6bb7d2SDavid Ahern 		fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
42787c6bb7d2SDavid Ahern 	else
42797c6bb7d2SDavid Ahern 		fib6_clean_all(net, fib6_ifdown, &arg);
42804c981e28SIdo Schimmel }
42814c981e28SIdo Schimmel 
42824c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
42834c981e28SIdo Schimmel {
42844c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
42854c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
42864c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
42871da177e4SLinus Torvalds }
42881da177e4SLinus Torvalds 
428995c96174SEric Dumazet struct rt6_mtu_change_arg {
42901da177e4SLinus Torvalds 	struct net_device *dev;
429195c96174SEric Dumazet 	unsigned int mtu;
4292c0b220cfSDavid Ahern 	struct fib6_info *f6i;
42931da177e4SLinus Torvalds };
42941da177e4SLinus Torvalds 
4295cc5c073aSDavid Ahern static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
4296c0b220cfSDavid Ahern {
4297c0b220cfSDavid Ahern 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
4298cc5c073aSDavid Ahern 	struct fib6_info *f6i = arg->f6i;
4299c0b220cfSDavid Ahern 
4300c0b220cfSDavid Ahern 	/* For administrative MTU increase, there is no way to discover
4301c0b220cfSDavid Ahern 	 * IPv6 PMTU increase, so PMTU increase should be updated here.
4302c0b220cfSDavid Ahern 	 * Since RFC 1981 doesn't include administrative MTU increase
4303c0b220cfSDavid Ahern 	 * update PMTU increase is a MUST. (i.e. jumbo frame)
4304c0b220cfSDavid Ahern 	 */
4305c0b220cfSDavid Ahern 	if (nh->fib_nh_dev == arg->dev) {
4306c0b220cfSDavid Ahern 		struct inet6_dev *idev = __in6_dev_get(arg->dev);
4307c0b220cfSDavid Ahern 		u32 mtu = f6i->fib6_pmtu;
4308c0b220cfSDavid Ahern 
4309c0b220cfSDavid Ahern 		if (mtu >= arg->mtu ||
4310c0b220cfSDavid Ahern 		    (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4311c0b220cfSDavid Ahern 			fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4312c0b220cfSDavid Ahern 
4313c0b220cfSDavid Ahern 		spin_lock_bh(&rt6_exception_lock);
4314cc5c073aSDavid Ahern 		rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
4315c0b220cfSDavid Ahern 		spin_unlock_bh(&rt6_exception_lock);
4316c0b220cfSDavid Ahern 	}
4317c0b220cfSDavid Ahern 
4318c0b220cfSDavid Ahern 	return 0;
4319c0b220cfSDavid Ahern }
4320c0b220cfSDavid Ahern 
4321c0b220cfSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
43221da177e4SLinus Torvalds {
43231da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
43241da177e4SLinus Torvalds 	struct inet6_dev *idev;
43251da177e4SLinus Torvalds 
43261da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
43271da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
43281da177e4SLinus Torvalds 	   We still use this lock to block changes
43291da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
43301da177e4SLinus Torvalds 	*/
43311da177e4SLinus Torvalds 
43321da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
433338308473SDavid S. Miller 	if (!idev)
43341da177e4SLinus Torvalds 		return 0;
43351da177e4SLinus Torvalds 
4336c0b220cfSDavid Ahern 	if (fib6_metric_locked(f6i, RTAX_MTU))
43371da177e4SLinus Torvalds 		return 0;
4338c0b220cfSDavid Ahern 
4339c0b220cfSDavid Ahern 	arg->f6i = f6i;
43401cf844c7SDavid Ahern 	return fib6_nh_mtu_change(f6i->fib6_nh, arg);
43411da177e4SLinus Torvalds }
43421da177e4SLinus Torvalds 
434395c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
43441da177e4SLinus Torvalds {
4345c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
4346c71099acSThomas Graf 		.dev = dev,
4347c71099acSThomas Graf 		.mtu = mtu,
4348c71099acSThomas Graf 	};
43491da177e4SLinus Torvalds 
43500c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
43511da177e4SLinus Torvalds }
43521da177e4SLinus Torvalds 
4353ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
435475425657SDavid Ahern 	[RTA_UNSPEC]		= { .strict_start_type = RTA_DPORT + 1 },
43555176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
4356aa8f8778SEric Dumazet 	[RTA_PREFSRC]		= { .len = sizeof(struct in6_addr) },
435786872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
4358ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
435986872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
436086872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
436151ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
4362c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
436319e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
436419e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
436532bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
4366622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
43673b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
4368aa8f8778SEric Dumazet 	[RTA_TABLE]		= { .type = NLA_U32 },
4369eacb9384SRoopa Prabhu 	[RTA_IP_PROTO]		= { .type = NLA_U8 },
4370eacb9384SRoopa Prabhu 	[RTA_SPORT]		= { .type = NLA_U16 },
4371eacb9384SRoopa Prabhu 	[RTA_DPORT]		= { .type = NLA_U16 },
437286872cb5SThomas Graf };
437386872cb5SThomas Graf 
437486872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4375333c4301SDavid Ahern 			      struct fib6_config *cfg,
4376333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
43771da177e4SLinus Torvalds {
437886872cb5SThomas Graf 	struct rtmsg *rtm;
437986872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
4380c78ba6d6SLubomir Rintel 	unsigned int pref;
438186872cb5SThomas Graf 	int err;
43821da177e4SLinus Torvalds 
43838cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
43848cb08174SJohannes Berg 				     rtm_ipv6_policy, extack);
438586872cb5SThomas Graf 	if (err < 0)
438686872cb5SThomas Graf 		goto errout;
43871da177e4SLinus Torvalds 
438886872cb5SThomas Graf 	err = -EINVAL;
438986872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
439086872cb5SThomas Graf 
439184db8407SMaciej Żenczykowski 	*cfg = (struct fib6_config){
439284db8407SMaciej Żenczykowski 		.fc_table = rtm->rtm_table,
439384db8407SMaciej Żenczykowski 		.fc_dst_len = rtm->rtm_dst_len,
439484db8407SMaciej Żenczykowski 		.fc_src_len = rtm->rtm_src_len,
439584db8407SMaciej Żenczykowski 		.fc_flags = RTF_UP,
439684db8407SMaciej Żenczykowski 		.fc_protocol = rtm->rtm_protocol,
439784db8407SMaciej Żenczykowski 		.fc_type = rtm->rtm_type,
439884db8407SMaciej Żenczykowski 
439984db8407SMaciej Żenczykowski 		.fc_nlinfo.portid = NETLINK_CB(skb).portid,
440084db8407SMaciej Żenczykowski 		.fc_nlinfo.nlh = nlh,
440184db8407SMaciej Żenczykowski 		.fc_nlinfo.nl_net = sock_net(skb->sk),
440284db8407SMaciej Żenczykowski 	};
440386872cb5SThomas Graf 
4404ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
4405ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
4406b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
4407b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
440886872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
440986872cb5SThomas Graf 
4410ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
4411ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
4412ab79ad14SMaciej Żenczykowski 
44131f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
44141f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
44151f56a01fSMartin KaFai Lau 
4416fc1e64e1SDavid Ahern 	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4417fc1e64e1SDavid Ahern 
441886872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
441967b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
442086872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
44211da177e4SLinus Torvalds 	}
4422e3818541SDavid Ahern 	if (tb[RTA_VIA]) {
4423e3818541SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4424e3818541SDavid Ahern 		goto errout;
4425e3818541SDavid Ahern 	}
442686872cb5SThomas Graf 
442786872cb5SThomas Graf 	if (tb[RTA_DST]) {
442886872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
442986872cb5SThomas Graf 
443086872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
443186872cb5SThomas Graf 			goto errout;
443286872cb5SThomas Graf 
443386872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
44341da177e4SLinus Torvalds 	}
443586872cb5SThomas Graf 
443686872cb5SThomas Graf 	if (tb[RTA_SRC]) {
443786872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
443886872cb5SThomas Graf 
443986872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
444086872cb5SThomas Graf 			goto errout;
444186872cb5SThomas Graf 
444286872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
44431da177e4SLinus Torvalds 	}
444486872cb5SThomas Graf 
4445c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
444667b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4447c3968a85SDaniel Walter 
444886872cb5SThomas Graf 	if (tb[RTA_OIF])
444986872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
445086872cb5SThomas Graf 
445186872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
445286872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
445386872cb5SThomas Graf 
445486872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
445586872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
445686872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
44571da177e4SLinus Torvalds 	}
445886872cb5SThomas Graf 
445986872cb5SThomas Graf 	if (tb[RTA_TABLE])
446086872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
446186872cb5SThomas Graf 
446251ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
446351ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
446451ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
44659ed59592SDavid Ahern 
44669ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4467c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
44689ed59592SDavid Ahern 		if (err < 0)
44699ed59592SDavid Ahern 			goto errout;
447051ebd318SNicolas Dichtel 	}
447151ebd318SNicolas Dichtel 
4472c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
4473c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
4474c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
4475c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
4476c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
4477c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
4478c78ba6d6SLubomir Rintel 	}
4479c78ba6d6SLubomir Rintel 
448019e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
448119e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
448219e42e45SRoopa Prabhu 
44839ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
448419e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
448519e42e45SRoopa Prabhu 
4486c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
44879ed59592SDavid Ahern 		if (err < 0)
44889ed59592SDavid Ahern 			goto errout;
44899ed59592SDavid Ahern 	}
44909ed59592SDavid Ahern 
449132bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
449232bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
449332bc201eSXin Long 
449432bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
449532bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
449632bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
449732bc201eSXin Long 		}
449832bc201eSXin Long 	}
449932bc201eSXin Long 
450086872cb5SThomas Graf 	err = 0;
450186872cb5SThomas Graf errout:
450286872cb5SThomas Graf 	return err;
45031da177e4SLinus Torvalds }
45041da177e4SLinus Torvalds 
45056b9ea5a6SRoopa Prabhu struct rt6_nh {
45068d1c802bSDavid Ahern 	struct fib6_info *fib6_info;
45076b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
45086b9ea5a6SRoopa Prabhu 	struct list_head next;
45096b9ea5a6SRoopa Prabhu };
45106b9ea5a6SRoopa Prabhu 
4511d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net,
4512d4ead6b3SDavid Ahern 				 struct list_head *rt6_nh_list,
45138d1c802bSDavid Ahern 				 struct fib6_info *rt,
45148d1c802bSDavid Ahern 				 struct fib6_config *r_cfg)
45156b9ea5a6SRoopa Prabhu {
45166b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
45176b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
45186b9ea5a6SRoopa Prabhu 
45196b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
45208d1c802bSDavid Ahern 		/* check if fib6_info already exists */
45218d1c802bSDavid Ahern 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
45226b9ea5a6SRoopa Prabhu 			return err;
45236b9ea5a6SRoopa Prabhu 	}
45246b9ea5a6SRoopa Prabhu 
45256b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
45266b9ea5a6SRoopa Prabhu 	if (!nh)
45276b9ea5a6SRoopa Prabhu 		return -ENOMEM;
45288d1c802bSDavid Ahern 	nh->fib6_info = rt;
45296b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
45306b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
45316b9ea5a6SRoopa Prabhu 
45326b9ea5a6SRoopa Prabhu 	return 0;
45336b9ea5a6SRoopa Prabhu }
45346b9ea5a6SRoopa Prabhu 
45358d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt,
45368d1c802bSDavid Ahern 				   struct fib6_info *rt_last,
45373b1137feSDavid Ahern 				   struct nl_info *info,
45383b1137feSDavid Ahern 				   __u16 nlflags)
45393b1137feSDavid Ahern {
45403b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
45413b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
45423b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
45433b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
45443b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
45453b1137feSDavid Ahern 	 */
454693c2fb25SDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
454793c2fb25SDavid Ahern 		rt = list_first_entry(&rt_last->fib6_siblings,
45488d1c802bSDavid Ahern 				      struct fib6_info,
454993c2fb25SDavid Ahern 				      fib6_siblings);
45503b1137feSDavid Ahern 	}
45513b1137feSDavid Ahern 
45523b1137feSDavid Ahern 	if (rt)
45533b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
45543b1137feSDavid Ahern }
45553b1137feSDavid Ahern 
4556333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
4557333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
455851ebd318SNicolas Dichtel {
45598d1c802bSDavid Ahern 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
45603b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
456151ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
456251ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
45638d1c802bSDavid Ahern 	struct fib6_info *rt;
45646b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
45656b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
45663b1137feSDavid Ahern 	__u16 nlflags;
456751ebd318SNicolas Dichtel 	int remaining;
456851ebd318SNicolas Dichtel 	int attrlen;
45696b9ea5a6SRoopa Prabhu 	int err = 1;
45706b9ea5a6SRoopa Prabhu 	int nhn = 0;
45716b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
45726b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
45736b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
457451ebd318SNicolas Dichtel 
45753b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
45763b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
45773b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
45783b1137feSDavid Ahern 
457935f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
458051ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
458151ebd318SNicolas Dichtel 
45826b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
45838d1c802bSDavid Ahern 	 * fib6_info structs per nexthop
45846b9ea5a6SRoopa Prabhu 	 */
458551ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
458651ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
458751ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
458851ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
458951ebd318SNicolas Dichtel 
459051ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
459151ebd318SNicolas Dichtel 		if (attrlen > 0) {
459251ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
459351ebd318SNicolas Dichtel 
459451ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
459551ebd318SNicolas Dichtel 			if (nla) {
459667b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
459751ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
459851ebd318SNicolas Dichtel 			}
459919e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
460019e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
460119e42e45SRoopa Prabhu 			if (nla)
460219e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
460351ebd318SNicolas Dichtel 		}
46046b9ea5a6SRoopa Prabhu 
460568e2ffdeSDavid Ahern 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
4606acb54e3cSDavid Ahern 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
46078c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
46088c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
46098c5b83f0SRoopa Prabhu 			rt = NULL;
46106b9ea5a6SRoopa Prabhu 			goto cleanup;
46118c5b83f0SRoopa Prabhu 		}
4612b5d2d75eSDavid Ahern 		if (!rt6_qualify_for_ecmp(rt)) {
4613b5d2d75eSDavid Ahern 			err = -EINVAL;
4614b5d2d75eSDavid Ahern 			NL_SET_ERR_MSG(extack,
4615b5d2d75eSDavid Ahern 				       "Device only routes can not be added for IPv6 using the multipath API.");
4616b5d2d75eSDavid Ahern 			fib6_info_release(rt);
4617b5d2d75eSDavid Ahern 			goto cleanup;
4618b5d2d75eSDavid Ahern 		}
46196b9ea5a6SRoopa Prabhu 
46201cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
4621398958aeSIdo Schimmel 
4622d4ead6b3SDavid Ahern 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4623d4ead6b3SDavid Ahern 					    rt, &r_cfg);
462451ebd318SNicolas Dichtel 		if (err) {
462593531c67SDavid Ahern 			fib6_info_release(rt);
46266b9ea5a6SRoopa Prabhu 			goto cleanup;
462751ebd318SNicolas Dichtel 		}
46286b9ea5a6SRoopa Prabhu 
46296b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
463051ebd318SNicolas Dichtel 	}
46316b9ea5a6SRoopa Prabhu 
46323b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
46333b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
46343b1137feSDavid Ahern 	 * the full route when done
46353b1137feSDavid Ahern 	 */
46363b1137feSDavid Ahern 	info->skip_notify = 1;
46373b1137feSDavid Ahern 
46386b9ea5a6SRoopa Prabhu 	err_nh = NULL;
46396b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
46408d1c802bSDavid Ahern 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
46418d1c802bSDavid Ahern 		fib6_info_release(nh->fib6_info);
46423b1137feSDavid Ahern 
4643f7225172SDavid Ahern 		if (!err) {
4644f7225172SDavid Ahern 			/* save reference to last route successfully inserted */
4645f7225172SDavid Ahern 			rt_last = nh->fib6_info;
4646f7225172SDavid Ahern 
46476b9ea5a6SRoopa Prabhu 			/* save reference to first route for notification */
4648f7225172SDavid Ahern 			if (!rt_notif)
46498d1c802bSDavid Ahern 				rt_notif = nh->fib6_info;
4650f7225172SDavid Ahern 		}
46516b9ea5a6SRoopa Prabhu 
46528d1c802bSDavid Ahern 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
46538d1c802bSDavid Ahern 		nh->fib6_info = NULL;
46546b9ea5a6SRoopa Prabhu 		if (err) {
46556b9ea5a6SRoopa Prabhu 			if (replace && nhn)
4656a5a82d84SJakub Kicinski 				NL_SET_ERR_MSG_MOD(extack,
4657a5a82d84SJakub Kicinski 						   "multipath route replace failed (check consistency of installed routes)");
46586b9ea5a6SRoopa Prabhu 			err_nh = nh;
46596b9ea5a6SRoopa Prabhu 			goto add_errout;
46606b9ea5a6SRoopa Prabhu 		}
46616b9ea5a6SRoopa Prabhu 
46621a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
466327596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
466427596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
466527596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
466627596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
466727596472SMichal Kubeček 		 * be added to it.
46681a72418bSNicolas Dichtel 		 */
466927596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
467027596472SMichal Kubeček 						     NLM_F_REPLACE);
46716b9ea5a6SRoopa Prabhu 		nhn++;
46726b9ea5a6SRoopa Prabhu 	}
46736b9ea5a6SRoopa Prabhu 
46743b1137feSDavid Ahern 	/* success ... tell user about new route */
46753b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
46766b9ea5a6SRoopa Prabhu 	goto cleanup;
46776b9ea5a6SRoopa Prabhu 
46786b9ea5a6SRoopa Prabhu add_errout:
46793b1137feSDavid Ahern 	/* send notification for routes that were added so that
46803b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
46813b1137feSDavid Ahern 	 * coherent
46823b1137feSDavid Ahern 	 */
46833b1137feSDavid Ahern 	if (rt_notif)
46843b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
46853b1137feSDavid Ahern 
46866b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
46876b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
46886b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
46896b9ea5a6SRoopa Prabhu 			break;
4690333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
46916b9ea5a6SRoopa Prabhu 	}
46926b9ea5a6SRoopa Prabhu 
46936b9ea5a6SRoopa Prabhu cleanup:
46946b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
46958d1c802bSDavid Ahern 		if (nh->fib6_info)
46968d1c802bSDavid Ahern 			fib6_info_release(nh->fib6_info);
46976b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
46986b9ea5a6SRoopa Prabhu 		kfree(nh);
46996b9ea5a6SRoopa Prabhu 	}
47006b9ea5a6SRoopa Prabhu 
47016b9ea5a6SRoopa Prabhu 	return err;
47026b9ea5a6SRoopa Prabhu }
47036b9ea5a6SRoopa Prabhu 
4704333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
4705333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
47066b9ea5a6SRoopa Prabhu {
47076b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
47086b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
47096b9ea5a6SRoopa Prabhu 	int remaining;
47106b9ea5a6SRoopa Prabhu 	int attrlen;
47116b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
47126b9ea5a6SRoopa Prabhu 
47136b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
47146b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
47156b9ea5a6SRoopa Prabhu 
47166b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
47176b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
47186b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
47196b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
47206b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
47216b9ea5a6SRoopa Prabhu 
47226b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
47236b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
47246b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
47256b9ea5a6SRoopa Prabhu 
47266b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
47276b9ea5a6SRoopa Prabhu 			if (nla) {
47286b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
47296b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
47306b9ea5a6SRoopa Prabhu 			}
47316b9ea5a6SRoopa Prabhu 		}
4732333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
47336b9ea5a6SRoopa Prabhu 		if (err)
47346b9ea5a6SRoopa Prabhu 			last_err = err;
47356b9ea5a6SRoopa Prabhu 
473651ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
473751ebd318SNicolas Dichtel 	}
473851ebd318SNicolas Dichtel 
473951ebd318SNicolas Dichtel 	return last_err;
474051ebd318SNicolas Dichtel }
474151ebd318SNicolas Dichtel 
4742c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4743c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
47441da177e4SLinus Torvalds {
474586872cb5SThomas Graf 	struct fib6_config cfg;
474686872cb5SThomas Graf 	int err;
47471da177e4SLinus Torvalds 
4748333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
474986872cb5SThomas Graf 	if (err < 0)
475086872cb5SThomas Graf 		return err;
475186872cb5SThomas Graf 
475251ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4753333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
47540ae81335SDavid Ahern 	else {
47550ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
4756333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
47571da177e4SLinus Torvalds 	}
47580ae81335SDavid Ahern }
47591da177e4SLinus Torvalds 
4760c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4761c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
47621da177e4SLinus Torvalds {
476386872cb5SThomas Graf 	struct fib6_config cfg;
476486872cb5SThomas Graf 	int err;
47651da177e4SLinus Torvalds 
4766333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
476786872cb5SThomas Graf 	if (err < 0)
476886872cb5SThomas Graf 		return err;
476986872cb5SThomas Graf 
477067f69513SDavid Ahern 	if (cfg.fc_metric == 0)
477167f69513SDavid Ahern 		cfg.fc_metric = IP6_RT_PRIO_USER;
477267f69513SDavid Ahern 
477351ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4774333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
477551ebd318SNicolas Dichtel 	else
4776acb54e3cSDavid Ahern 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
47771da177e4SLinus Torvalds }
47781da177e4SLinus Torvalds 
47798d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt)
4780339bf98fSThomas Graf {
4781beb1afacSDavid Ahern 	int nexthop_len = 0;
4782beb1afacSDavid Ahern 
478393c2fb25SDavid Ahern 	if (rt->fib6_nsiblings) {
4784beb1afacSDavid Ahern 		nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
4785beb1afacSDavid Ahern 			    + NLA_ALIGN(sizeof(struct rtnexthop))
4786beb1afacSDavid Ahern 			    + nla_total_size(16) /* RTA_GATEWAY */
47871cf844c7SDavid Ahern 			    + lwtunnel_get_encap_size(rt->fib6_nh->fib_nh_lws);
4788beb1afacSDavid Ahern 
478993c2fb25SDavid Ahern 		nexthop_len *= rt->fib6_nsiblings;
4790beb1afacSDavid Ahern 	}
4791beb1afacSDavid Ahern 
4792339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
4793339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
4794339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
4795339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
4796339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
4797339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
4798339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
4799339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
4800339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
48016a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
4802ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
4803c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
480419e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
48051cf844c7SDavid Ahern 	       + lwtunnel_get_encap_size(rt->fib6_nh->fib_nh_lws)
4806beb1afacSDavid Ahern 	       + nexthop_len;
4807beb1afacSDavid Ahern }
4808beb1afacSDavid Ahern 
4809d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
48108d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
4811d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
481215e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
4813f8cfe2ceSDavid Ahern 			 unsigned int flags)
48141da177e4SLinus Torvalds {
481522d0bd82SXin Long 	struct rt6_info *rt6 = (struct rt6_info *)dst;
481622d0bd82SXin Long 	struct rt6key *rt6_dst, *rt6_src;
481722d0bd82SXin Long 	u32 *pmetrics, table, rt6_flags;
48181da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
481922d0bd82SXin Long 	struct rtmsg *rtm;
4820d4ead6b3SDavid Ahern 	long expires = 0;
48211da177e4SLinus Torvalds 
482215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
482338308473SDavid S. Miller 	if (!nlh)
482426932566SPatrick McHardy 		return -EMSGSIZE;
48252d7202bfSThomas Graf 
482622d0bd82SXin Long 	if (rt6) {
482722d0bd82SXin Long 		rt6_dst = &rt6->rt6i_dst;
482822d0bd82SXin Long 		rt6_src = &rt6->rt6i_src;
482922d0bd82SXin Long 		rt6_flags = rt6->rt6i_flags;
483022d0bd82SXin Long 	} else {
483122d0bd82SXin Long 		rt6_dst = &rt->fib6_dst;
483222d0bd82SXin Long 		rt6_src = &rt->fib6_src;
483322d0bd82SXin Long 		rt6_flags = rt->fib6_flags;
483422d0bd82SXin Long 	}
483522d0bd82SXin Long 
48362d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
48371da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
483822d0bd82SXin Long 	rtm->rtm_dst_len = rt6_dst->plen;
483922d0bd82SXin Long 	rtm->rtm_src_len = rt6_src->plen;
48401da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
484193c2fb25SDavid Ahern 	if (rt->fib6_table)
484293c2fb25SDavid Ahern 		table = rt->fib6_table->tb6_id;
4843c71099acSThomas Graf 	else
48449e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
484597f0082aSKalash Nainwal 	rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
4846c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
4847c78679e8SDavid S. Miller 		goto nla_put_failure;
4848e8478e80SDavid Ahern 
4849e8478e80SDavid Ahern 	rtm->rtm_type = rt->fib6_type;
48501da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
48511da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
485293c2fb25SDavid Ahern 	rtm->rtm_protocol = rt->fib6_protocol;
48531da177e4SLinus Torvalds 
485422d0bd82SXin Long 	if (rt6_flags & RTF_CACHE)
48551da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
48561da177e4SLinus Torvalds 
4857d4ead6b3SDavid Ahern 	if (dest) {
4858d4ead6b3SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, dest))
4859c78679e8SDavid S. Miller 			goto nla_put_failure;
48601da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
48611da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
486222d0bd82SXin Long 		if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
4863c78679e8SDavid S. Miller 			goto nla_put_failure;
48641da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
48651da177e4SLinus Torvalds 	if (src) {
4866930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
4867c78679e8SDavid S. Miller 			goto nla_put_failure;
48681da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
4869c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
487022d0bd82SXin Long 		   nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
4871c78679e8SDavid S. Miller 		goto nla_put_failure;
48721da177e4SLinus Torvalds #endif
48737bc570c8SYOSHIFUJI Hideaki 	if (iif) {
48747bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
487522d0bd82SXin Long 		if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
4876fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
48772cf75070SNikolay Aleksandrov 
48787bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
48797bc570c8SYOSHIFUJI Hideaki 				return 0;
4880fd61c6baSDavid Ahern 			if (err < 0)
48817bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
48827bc570c8SYOSHIFUJI Hideaki 		} else
48837bc570c8SYOSHIFUJI Hideaki #endif
4884c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
4885c78679e8SDavid S. Miller 				goto nla_put_failure;
4886d4ead6b3SDavid Ahern 	} else if (dest) {
48871da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
4888d4ead6b3SDavid Ahern 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
4889930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4890c78679e8SDavid S. Miller 			goto nla_put_failure;
4891c3968a85SDaniel Walter 	}
4892c3968a85SDaniel Walter 
489393c2fb25SDavid Ahern 	if (rt->fib6_prefsrc.plen) {
4894c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
489593c2fb25SDavid Ahern 		saddr_buf = rt->fib6_prefsrc.addr;
4896930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4897c78679e8SDavid S. Miller 			goto nla_put_failure;
48981da177e4SLinus Torvalds 	}
48992d7202bfSThomas Graf 
4900d4ead6b3SDavid Ahern 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4901d4ead6b3SDavid Ahern 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
49022d7202bfSThomas Graf 		goto nla_put_failure;
49032d7202bfSThomas Graf 
490493c2fb25SDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
4905beb1afacSDavid Ahern 		goto nla_put_failure;
4906beb1afacSDavid Ahern 
4907beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
4908beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
4909beb1afacSDavid Ahern 	 */
491022d0bd82SXin Long 	if (rt6) {
491122d0bd82SXin Long 		if (rt6_flags & RTF_GATEWAY &&
491222d0bd82SXin Long 		    nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
491322d0bd82SXin Long 			goto nla_put_failure;
491422d0bd82SXin Long 
491522d0bd82SXin Long 		if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
491622d0bd82SXin Long 			goto nla_put_failure;
491722d0bd82SXin Long 	} else if (rt->fib6_nsiblings) {
49188d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
4919beb1afacSDavid Ahern 		struct nlattr *mp;
4920beb1afacSDavid Ahern 
4921ae0be8deSMichal Kubecek 		mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
4922beb1afacSDavid Ahern 		if (!mp)
4923beb1afacSDavid Ahern 			goto nla_put_failure;
4924beb1afacSDavid Ahern 
49251cf844c7SDavid Ahern 		if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
49261cf844c7SDavid Ahern 				    rt->fib6_nh->fib_nh_weight) < 0)
4927beb1afacSDavid Ahern 			goto nla_put_failure;
4928beb1afacSDavid Ahern 
4929beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
493093c2fb25SDavid Ahern 					 &rt->fib6_siblings, fib6_siblings) {
49311cf844c7SDavid Ahern 			if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
49321cf844c7SDavid Ahern 					    sibling->fib6_nh->fib_nh_weight) < 0)
493394f826b8SEric Dumazet 				goto nla_put_failure;
493494f826b8SEric Dumazet 		}
49352d7202bfSThomas Graf 
4936beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
4937beb1afacSDavid Ahern 	} else {
4938ecc5663cSDavid Ahern 		unsigned char nh_flags = 0;
4939ecc5663cSDavid Ahern 
49401cf844c7SDavid Ahern 		if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common,
4941ecc5663cSDavid Ahern 				     &nh_flags, false) < 0)
4942c78679e8SDavid S. Miller 			goto nla_put_failure;
4943ecc5663cSDavid Ahern 
4944ecc5663cSDavid Ahern 		rtm->rtm_flags |= nh_flags;
4945beb1afacSDavid Ahern 	}
49468253947eSLi Wei 
494722d0bd82SXin Long 	if (rt6_flags & RTF_EXPIRES) {
494814895687SDavid Ahern 		expires = dst ? dst->expires : rt->expires;
494914895687SDavid Ahern 		expires -= jiffies;
495014895687SDavid Ahern 	}
495169cdf8f9SYOSHIFUJI Hideaki 
4952d4ead6b3SDavid Ahern 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
4953e3703b3dSThomas Graf 		goto nla_put_failure;
49541da177e4SLinus Torvalds 
495522d0bd82SXin Long 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
4956c78ba6d6SLubomir Rintel 		goto nla_put_failure;
4957c78ba6d6SLubomir Rintel 
495819e42e45SRoopa Prabhu 
4959053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4960053c095aSJohannes Berg 	return 0;
49612d7202bfSThomas Graf 
49622d7202bfSThomas Graf nla_put_failure:
496326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
496426932566SPatrick McHardy 	return -EMSGSIZE;
49651da177e4SLinus Torvalds }
49661da177e4SLinus Torvalds 
496713e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i,
496813e38901SDavid Ahern 			       const struct net_device *dev)
496913e38901SDavid Ahern {
49701cf844c7SDavid Ahern 	if (f6i->fib6_nh->fib_nh_dev == dev)
497113e38901SDavid Ahern 		return true;
497213e38901SDavid Ahern 
497313e38901SDavid Ahern 	if (f6i->fib6_nsiblings) {
497413e38901SDavid Ahern 		struct fib6_info *sibling, *next_sibling;
497513e38901SDavid Ahern 
497613e38901SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
497713e38901SDavid Ahern 					 &f6i->fib6_siblings, fib6_siblings) {
49781cf844c7SDavid Ahern 			if (sibling->fib6_nh->fib_nh_dev == dev)
497913e38901SDavid Ahern 				return true;
498013e38901SDavid Ahern 		}
498113e38901SDavid Ahern 	}
498213e38901SDavid Ahern 
498313e38901SDavid Ahern 	return false;
498413e38901SDavid Ahern }
498513e38901SDavid Ahern 
49868d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg)
49871da177e4SLinus Torvalds {
49881da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
498913e38901SDavid Ahern 	struct fib_dump_filter *filter = &arg->filter;
499013e38901SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
49911f17e2f2SDavid Ahern 	struct net *net = arg->net;
49921f17e2f2SDavid Ahern 
4993421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
49941f17e2f2SDavid Ahern 		return 0;
49951da177e4SLinus Torvalds 
499613e38901SDavid Ahern 	if ((filter->flags & RTM_F_PREFIX) &&
499793c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_PREFIX_RT)) {
4998f8cfe2ceSDavid Ahern 		/* success since this is not a prefix route */
4999f8cfe2ceSDavid Ahern 		return 1;
5000f8cfe2ceSDavid Ahern 	}
500113e38901SDavid Ahern 	if (filter->filter_set) {
500213e38901SDavid Ahern 		if ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
500313e38901SDavid Ahern 		    (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
500413e38901SDavid Ahern 		    (filter->protocol && rt->fib6_protocol != filter->protocol)) {
500513e38901SDavid Ahern 			return 1;
500613e38901SDavid Ahern 		}
500713e38901SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
5008f8cfe2ceSDavid Ahern 	}
50091da177e4SLinus Torvalds 
5010d4ead6b3SDavid Ahern 	return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
5011d4ead6b3SDavid Ahern 			     RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
501213e38901SDavid Ahern 			     arg->cb->nlh->nlmsg_seq, flags);
50131da177e4SLinus Torvalds }
50141da177e4SLinus Torvalds 
50150eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
50160eff0a27SJakub Kicinski 					const struct nlmsghdr *nlh,
50170eff0a27SJakub Kicinski 					struct nlattr **tb,
50180eff0a27SJakub Kicinski 					struct netlink_ext_ack *extack)
50190eff0a27SJakub Kicinski {
50200eff0a27SJakub Kicinski 	struct rtmsg *rtm;
50210eff0a27SJakub Kicinski 	int i, err;
50220eff0a27SJakub Kicinski 
50230eff0a27SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
50240eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
50250eff0a27SJakub Kicinski 				   "Invalid header for get route request");
50260eff0a27SJakub Kicinski 		return -EINVAL;
50270eff0a27SJakub Kicinski 	}
50280eff0a27SJakub Kicinski 
50290eff0a27SJakub Kicinski 	if (!netlink_strict_get_check(skb))
50308cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
50310eff0a27SJakub Kicinski 					      rtm_ipv6_policy, extack);
50320eff0a27SJakub Kicinski 
50330eff0a27SJakub Kicinski 	rtm = nlmsg_data(nlh);
50340eff0a27SJakub Kicinski 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
50350eff0a27SJakub Kicinski 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
50360eff0a27SJakub Kicinski 	    rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
50370eff0a27SJakub Kicinski 	    rtm->rtm_type) {
50380eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
50390eff0a27SJakub Kicinski 		return -EINVAL;
50400eff0a27SJakub Kicinski 	}
50410eff0a27SJakub Kicinski 	if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
50420eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
50430eff0a27SJakub Kicinski 				   "Invalid flags for get route request");
50440eff0a27SJakub Kicinski 		return -EINVAL;
50450eff0a27SJakub Kicinski 	}
50460eff0a27SJakub Kicinski 
50478cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
50480eff0a27SJakub Kicinski 					    rtm_ipv6_policy, extack);
50490eff0a27SJakub Kicinski 	if (err)
50500eff0a27SJakub Kicinski 		return err;
50510eff0a27SJakub Kicinski 
50520eff0a27SJakub Kicinski 	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
50530eff0a27SJakub Kicinski 	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
50540eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
50550eff0a27SJakub Kicinski 		return -EINVAL;
50560eff0a27SJakub Kicinski 	}
50570eff0a27SJakub Kicinski 
50580eff0a27SJakub Kicinski 	for (i = 0; i <= RTA_MAX; i++) {
50590eff0a27SJakub Kicinski 		if (!tb[i])
50600eff0a27SJakub Kicinski 			continue;
50610eff0a27SJakub Kicinski 
50620eff0a27SJakub Kicinski 		switch (i) {
50630eff0a27SJakub Kicinski 		case RTA_SRC:
50640eff0a27SJakub Kicinski 		case RTA_DST:
50650eff0a27SJakub Kicinski 		case RTA_IIF:
50660eff0a27SJakub Kicinski 		case RTA_OIF:
50670eff0a27SJakub Kicinski 		case RTA_MARK:
50680eff0a27SJakub Kicinski 		case RTA_UID:
50690eff0a27SJakub Kicinski 		case RTA_SPORT:
50700eff0a27SJakub Kicinski 		case RTA_DPORT:
50710eff0a27SJakub Kicinski 		case RTA_IP_PROTO:
50720eff0a27SJakub Kicinski 			break;
50730eff0a27SJakub Kicinski 		default:
50740eff0a27SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
50750eff0a27SJakub Kicinski 			return -EINVAL;
50760eff0a27SJakub Kicinski 		}
50770eff0a27SJakub Kicinski 	}
50780eff0a27SJakub Kicinski 
50790eff0a27SJakub Kicinski 	return 0;
50800eff0a27SJakub Kicinski }
50810eff0a27SJakub Kicinski 
5082c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5083c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
50841da177e4SLinus Torvalds {
50853b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
5086ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
508718c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
5088a68886a6SDavid Ahern 	struct fib6_info *from;
508918c3a61cSRoopa Prabhu 	struct dst_entry *dst;
50901da177e4SLinus Torvalds 	struct rt6_info *rt;
5091ab364a6fSThomas Graf 	struct sk_buff *skb;
5092ab364a6fSThomas Graf 	struct rtmsg *rtm;
5093744486d4SMaciej Żenczykowski 	struct flowi6 fl6 = {};
509418c3a61cSRoopa Prabhu 	bool fibmatch;
5095ab364a6fSThomas Graf 
50960eff0a27SJakub Kicinski 	err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
5097ab364a6fSThomas Graf 	if (err < 0)
5098ab364a6fSThomas Graf 		goto errout;
5099ab364a6fSThomas Graf 
5100ab364a6fSThomas Graf 	err = -EINVAL;
510138b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
510238b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
510318c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
5104ab364a6fSThomas Graf 
5105ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
5106ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5107ab364a6fSThomas Graf 			goto errout;
5108ab364a6fSThomas Graf 
51094e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
5110ab364a6fSThomas Graf 	}
5111ab364a6fSThomas Graf 
5112ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
5113ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5114ab364a6fSThomas Graf 			goto errout;
5115ab364a6fSThomas Graf 
51164e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
5117ab364a6fSThomas Graf 	}
5118ab364a6fSThomas Graf 
5119ab364a6fSThomas Graf 	if (tb[RTA_IIF])
5120ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
5121ab364a6fSThomas Graf 
5122ab364a6fSThomas Graf 	if (tb[RTA_OIF])
512372331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
5124ab364a6fSThomas Graf 
51252e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
51262e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
51272e47b291SLorenzo Colitti 
5128622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
5129622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
5130622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
5131622ec2c9SLorenzo Colitti 	else
5132622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5133622ec2c9SLorenzo Colitti 
5134eacb9384SRoopa Prabhu 	if (tb[RTA_SPORT])
5135eacb9384SRoopa Prabhu 		fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5136eacb9384SRoopa Prabhu 
5137eacb9384SRoopa Prabhu 	if (tb[RTA_DPORT])
5138eacb9384SRoopa Prabhu 		fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5139eacb9384SRoopa Prabhu 
5140eacb9384SRoopa Prabhu 	if (tb[RTA_IP_PROTO]) {
5141eacb9384SRoopa Prabhu 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
51425e1a99eaSHangbin Liu 						  &fl6.flowi6_proto, AF_INET6,
51435e1a99eaSHangbin Liu 						  extack);
5144eacb9384SRoopa Prabhu 		if (err)
5145eacb9384SRoopa Prabhu 			goto errout;
5146eacb9384SRoopa Prabhu 	}
5147eacb9384SRoopa Prabhu 
5148ab364a6fSThomas Graf 	if (iif) {
5149ab364a6fSThomas Graf 		struct net_device *dev;
515072331bc0SShmulik Ladkani 		int flags = 0;
515172331bc0SShmulik Ladkani 
5152121622dbSFlorian Westphal 		rcu_read_lock();
5153121622dbSFlorian Westphal 
5154121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
5155ab364a6fSThomas Graf 		if (!dev) {
5156121622dbSFlorian Westphal 			rcu_read_unlock();
5157ab364a6fSThomas Graf 			err = -ENODEV;
5158ab364a6fSThomas Graf 			goto errout;
5159ab364a6fSThomas Graf 		}
516072331bc0SShmulik Ladkani 
516172331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
516272331bc0SShmulik Ladkani 
516372331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
516472331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
516572331bc0SShmulik Ladkani 
5166b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
5167121622dbSFlorian Westphal 
5168121622dbSFlorian Westphal 		rcu_read_unlock();
516972331bc0SShmulik Ladkani 	} else {
517072331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
517172331bc0SShmulik Ladkani 
517218c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
517318c3a61cSRoopa Prabhu 	}
517418c3a61cSRoopa Prabhu 
517518c3a61cSRoopa Prabhu 
517618c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
517718c3a61cSRoopa Prabhu 	if (rt->dst.error) {
517818c3a61cSRoopa Prabhu 		err = rt->dst.error;
517918c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
518018c3a61cSRoopa Prabhu 		goto errout;
5181ab364a6fSThomas Graf 	}
51821da177e4SLinus Torvalds 
51839d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
51849d6acb3bSWANG Cong 		err = rt->dst.error;
51859d6acb3bSWANG Cong 		ip6_rt_put(rt);
51869d6acb3bSWANG Cong 		goto errout;
51879d6acb3bSWANG Cong 	}
51889d6acb3bSWANG Cong 
51891da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
519038308473SDavid S. Miller 	if (!skb) {
519194e187c0SAmerigo Wang 		ip6_rt_put(rt);
5192ab364a6fSThomas Graf 		err = -ENOBUFS;
5193ab364a6fSThomas Graf 		goto errout;
5194ab364a6fSThomas Graf 	}
51951da177e4SLinus Torvalds 
5196d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
5197a68886a6SDavid Ahern 
5198a68886a6SDavid Ahern 	rcu_read_lock();
5199a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
5200886b7a50SMartin KaFai Lau 	if (from) {
520118c3a61cSRoopa Prabhu 		if (fibmatch)
5202886b7a50SMartin KaFai Lau 			err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5203886b7a50SMartin KaFai Lau 					    iif, RTM_NEWROUTE,
5204886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
520518c3a61cSRoopa Prabhu 					    nlh->nlmsg_seq, 0);
520618c3a61cSRoopa Prabhu 		else
5207a68886a6SDavid Ahern 			err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5208a68886a6SDavid Ahern 					    &fl6.saddr, iif, RTM_NEWROUTE,
5209886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
5210886b7a50SMartin KaFai Lau 					    nlh->nlmsg_seq, 0);
5211886b7a50SMartin KaFai Lau 	} else {
5212886b7a50SMartin KaFai Lau 		err = -ENETUNREACH;
5213886b7a50SMartin KaFai Lau 	}
5214a68886a6SDavid Ahern 	rcu_read_unlock();
5215a68886a6SDavid Ahern 
52161da177e4SLinus Torvalds 	if (err < 0) {
5217ab364a6fSThomas Graf 		kfree_skb(skb);
5218ab364a6fSThomas Graf 		goto errout;
52191da177e4SLinus Torvalds 	}
52201da177e4SLinus Torvalds 
522115e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
5222ab364a6fSThomas Graf errout:
52231da177e4SLinus Torvalds 	return err;
52241da177e4SLinus Torvalds }
52251da177e4SLinus Torvalds 
52268d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
522737a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
52281da177e4SLinus Torvalds {
52291da177e4SLinus Torvalds 	struct sk_buff *skb;
52305578689aSDaniel Lezcano 	struct net *net = info->nl_net;
5231528c4cebSDenis V. Lunev 	u32 seq;
5232528c4cebSDenis V. Lunev 	int err;
52330d51aa80SJamal Hadi Salim 
5234528c4cebSDenis V. Lunev 	err = -ENOBUFS;
523538308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
523686872cb5SThomas Graf 
523719e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
523838308473SDavid S. Miller 	if (!skb)
523921713ebcSThomas Graf 		goto errout;
52401da177e4SLinus Torvalds 
5241d4ead6b3SDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5242f8cfe2ceSDavid Ahern 			    event, info->portid, seq, nlm_flags);
524326932566SPatrick McHardy 	if (err < 0) {
524426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
524526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
524626932566SPatrick McHardy 		kfree_skb(skb);
524726932566SPatrick McHardy 		goto errout;
524826932566SPatrick McHardy 	}
524915e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
52505578689aSDaniel Lezcano 		    info->nlh, gfp_any());
52511ce85fe4SPablo Neira Ayuso 	return;
525221713ebcSThomas Graf errout:
525321713ebcSThomas Graf 	if (err < 0)
52545578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
52551da177e4SLinus Torvalds }
52561da177e4SLinus Torvalds 
525719a3b7eeSDavid Ahern void fib6_rt_update(struct net *net, struct fib6_info *rt,
525819a3b7eeSDavid Ahern 		    struct nl_info *info)
525919a3b7eeSDavid Ahern {
526019a3b7eeSDavid Ahern 	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
526119a3b7eeSDavid Ahern 	struct sk_buff *skb;
526219a3b7eeSDavid Ahern 	int err = -ENOBUFS;
526319a3b7eeSDavid Ahern 
526419a3b7eeSDavid Ahern 	/* call_fib6_entry_notifiers will be removed when in-kernel notifier
526519a3b7eeSDavid Ahern 	 * is implemented and supported for nexthop objects
526619a3b7eeSDavid Ahern 	 */
526719a3b7eeSDavid Ahern 	call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
526819a3b7eeSDavid Ahern 
526919a3b7eeSDavid Ahern 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
527019a3b7eeSDavid Ahern 	if (!skb)
527119a3b7eeSDavid Ahern 		goto errout;
527219a3b7eeSDavid Ahern 
527319a3b7eeSDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
527419a3b7eeSDavid Ahern 			    RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
527519a3b7eeSDavid Ahern 	if (err < 0) {
527619a3b7eeSDavid Ahern 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
527719a3b7eeSDavid Ahern 		WARN_ON(err == -EMSGSIZE);
527819a3b7eeSDavid Ahern 		kfree_skb(skb);
527919a3b7eeSDavid Ahern 		goto errout;
528019a3b7eeSDavid Ahern 	}
528119a3b7eeSDavid Ahern 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
528219a3b7eeSDavid Ahern 		    info->nlh, gfp_any());
528319a3b7eeSDavid Ahern 	return;
528419a3b7eeSDavid Ahern errout:
528519a3b7eeSDavid Ahern 	if (err < 0)
528619a3b7eeSDavid Ahern 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
528719a3b7eeSDavid Ahern }
528819a3b7eeSDavid Ahern 
52898ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
5290351638e7SJiri Pirko 				unsigned long event, void *ptr)
52918ed67789SDaniel Lezcano {
5292351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5293c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
52948ed67789SDaniel Lezcano 
5295242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
5296242d3a49SWANG Cong 		return NOTIFY_OK;
5297242d3a49SWANG Cong 
5298242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
52991cf844c7SDavid Ahern 		net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
5300d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
53018ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
53028ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5303d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
53048ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
5305d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
53068ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
53078ed67789SDaniel Lezcano #endif
530876da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
530976da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
531076da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
531176da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
531276da0704SWANG Cong 		 */
531312d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
5314242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
531512d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
531612d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
5317242d3a49SWANG Cong #endif
53188ed67789SDaniel Lezcano 	}
53198ed67789SDaniel Lezcano 
53208ed67789SDaniel Lezcano 	return NOTIFY_OK;
53218ed67789SDaniel Lezcano }
53228ed67789SDaniel Lezcano 
53231da177e4SLinus Torvalds /*
53241da177e4SLinus Torvalds  *	/proc
53251da177e4SLinus Torvalds  */
53261da177e4SLinus Torvalds 
53271da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
53281da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
53291da177e4SLinus Torvalds {
533069ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
53311da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
533269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
533369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
533481eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
533569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
533669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
5337fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
533869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
53391da177e4SLinus Torvalds 
53401da177e4SLinus Torvalds 	return 0;
53411da177e4SLinus Torvalds }
53421da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
53431da177e4SLinus Torvalds 
53441da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
53451da177e4SLinus Torvalds 
53461da177e4SLinus Torvalds static
5347fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
53481da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
53491da177e4SLinus Torvalds {
5350c486da34SLucian Adrian Grijincu 	struct net *net;
5351c486da34SLucian Adrian Grijincu 	int delay;
5352f0fb9b28SAditya Pakki 	int ret;
5353c486da34SLucian Adrian Grijincu 	if (!write)
5354c486da34SLucian Adrian Grijincu 		return -EINVAL;
5355c486da34SLucian Adrian Grijincu 
5356c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
5357c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
5358f0fb9b28SAditya Pakki 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5359f0fb9b28SAditya Pakki 	if (ret)
5360f0fb9b28SAditya Pakki 		return ret;
5361f0fb9b28SAditya Pakki 
53622ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
53631da177e4SLinus Torvalds 	return 0;
53641da177e4SLinus Torvalds }
53651da177e4SLinus Torvalds 
53667c6bb7d2SDavid Ahern static int zero;
53677c6bb7d2SDavid Ahern static int one = 1;
53687c6bb7d2SDavid Ahern 
5369ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = {
53701da177e4SLinus Torvalds 	{
53711da177e4SLinus Torvalds 		.procname	=	"flush",
53724990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
53731da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
537489c8b3a1SDave Jones 		.mode		=	0200,
53756d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
53761da177e4SLinus Torvalds 	},
53771da177e4SLinus Torvalds 	{
53781da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
53799a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
53801da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
53811da177e4SLinus Torvalds 		.mode		=	0644,
53826d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
53831da177e4SLinus Torvalds 	},
53841da177e4SLinus Torvalds 	{
53851da177e4SLinus Torvalds 		.procname	=	"max_size",
53864990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
53871da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
53881da177e4SLinus Torvalds 		.mode		=	0644,
53896d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
53901da177e4SLinus Torvalds 	},
53911da177e4SLinus Torvalds 	{
53921da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
53934990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
53941da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
53951da177e4SLinus Torvalds 		.mode		=	0644,
53966d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
53971da177e4SLinus Torvalds 	},
53981da177e4SLinus Torvalds 	{
53991da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
54004990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
54011da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
54021da177e4SLinus Torvalds 		.mode		=	0644,
54036d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
54041da177e4SLinus Torvalds 	},
54051da177e4SLinus Torvalds 	{
54061da177e4SLinus Torvalds 		.procname	=	"gc_interval",
54074990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
54081da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
54091da177e4SLinus Torvalds 		.mode		=	0644,
54106d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
54111da177e4SLinus Torvalds 	},
54121da177e4SLinus Torvalds 	{
54131da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
54144990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
54151da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
54161da177e4SLinus Torvalds 		.mode		=	0644,
5417f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
54181da177e4SLinus Torvalds 	},
54191da177e4SLinus Torvalds 	{
54201da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
54214990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
54221da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
54231da177e4SLinus Torvalds 		.mode		=	0644,
54246d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
54251da177e4SLinus Torvalds 	},
54261da177e4SLinus Torvalds 	{
54271da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
54284990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
54291da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
54301da177e4SLinus Torvalds 		.mode		=	0644,
5431f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
54321da177e4SLinus Torvalds 	},
54331da177e4SLinus Torvalds 	{
54341da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
54354990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
54361da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
54371da177e4SLinus Torvalds 		.mode		=	0644,
54386d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
54391da177e4SLinus Torvalds 	},
54407c6bb7d2SDavid Ahern 	{
54417c6bb7d2SDavid Ahern 		.procname	=	"skip_notify_on_dev_down",
54427c6bb7d2SDavid Ahern 		.data		=	&init_net.ipv6.sysctl.skip_notify_on_dev_down,
54437c6bb7d2SDavid Ahern 		.maxlen		=	sizeof(int),
54447c6bb7d2SDavid Ahern 		.mode		=	0644,
54457c6bb7d2SDavid Ahern 		.proc_handler	=	proc_dointvec,
54467c6bb7d2SDavid Ahern 		.extra1		=	&zero,
54477c6bb7d2SDavid Ahern 		.extra2		=	&one,
54487c6bb7d2SDavid Ahern 	},
5449f8572d8fSEric W. Biederman 	{ }
54501da177e4SLinus Torvalds };
54511da177e4SLinus Torvalds 
54522c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
5453760f2d01SDaniel Lezcano {
5454760f2d01SDaniel Lezcano 	struct ctl_table *table;
5455760f2d01SDaniel Lezcano 
5456760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
5457760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
5458760f2d01SDaniel Lezcano 			GFP_KERNEL);
54595ee09105SYOSHIFUJI Hideaki 
54605ee09105SYOSHIFUJI Hideaki 	if (table) {
54615ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
5462c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
546386393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
54645ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
54655ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
54665ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
54675ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
54685ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
54695ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
54705ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
54719c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
54727c6bb7d2SDavid Ahern 		table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
5473464dc801SEric W. Biederman 
5474464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
5475464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
5476464dc801SEric W. Biederman 			table[0].procname = NULL;
54775ee09105SYOSHIFUJI Hideaki 	}
54785ee09105SYOSHIFUJI Hideaki 
5479760f2d01SDaniel Lezcano 	return table;
5480760f2d01SDaniel Lezcano }
54811da177e4SLinus Torvalds #endif
54821da177e4SLinus Torvalds 
54832c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
5484cdb18761SDaniel Lezcano {
5485633d424bSPavel Emelyanov 	int ret = -ENOMEM;
54868ed67789SDaniel Lezcano 
548786393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
548886393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
5489f2fc6a54SBenjamin Thery 
5490fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5491fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
5492fc66f95cSEric Dumazet 
54931cf844c7SDavid Ahern 	net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
5494421842edSDavid Ahern 	if (!net->ipv6.fib6_null_entry)
5495421842edSDavid Ahern 		goto out_ip6_dst_entries;
54961cf844c7SDavid Ahern 	memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
54971cf844c7SDavid Ahern 	       sizeof(*net->ipv6.fib6_null_entry));
5498421842edSDavid Ahern 
54998ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
55008ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
55018ed67789SDaniel Lezcano 					   GFP_KERNEL);
55028ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
5503421842edSDavid Ahern 		goto out_fib6_null_entry;
5504d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
550562fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
550662fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
55078ed67789SDaniel Lezcano 
55088ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5509feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
55108ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
55118ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
55128ed67789SDaniel Lezcano 					       GFP_KERNEL);
551368fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
551468fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
5515d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
551662fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
551762fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
55188ed67789SDaniel Lezcano 
55198ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
55208ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
55218ed67789SDaniel Lezcano 					       GFP_KERNEL);
552268fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
552368fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
5524d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
552562fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
552662fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
55278ed67789SDaniel Lezcano #endif
55288ed67789SDaniel Lezcano 
5529b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
5530b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
5531b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5532b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5533b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5534b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5535b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5536b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
55377c6bb7d2SDavid Ahern 	net->ipv6.sysctl.skip_notify_on_dev_down = 0;
5538b339a47cSPeter Zijlstra 
55396891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
55406891a346SBenjamin Thery 
55418ed67789SDaniel Lezcano 	ret = 0;
55428ed67789SDaniel Lezcano out:
55438ed67789SDaniel Lezcano 	return ret;
5544f2fc6a54SBenjamin Thery 
554568fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
554668fffc67SPeter Zijlstra out_ip6_prohibit_entry:
554768fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
554868fffc67SPeter Zijlstra out_ip6_null_entry:
554968fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
555068fffc67SPeter Zijlstra #endif
5551421842edSDavid Ahern out_fib6_null_entry:
5552421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
5553fc66f95cSEric Dumazet out_ip6_dst_entries:
5554fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5555f2fc6a54SBenjamin Thery out_ip6_dst_ops:
5556f2fc6a54SBenjamin Thery 	goto out;
5557cdb18761SDaniel Lezcano }
5558cdb18761SDaniel Lezcano 
55592c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
5560cdb18761SDaniel Lezcano {
5561421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
55628ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
55638ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
55648ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
55658ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
55668ed67789SDaniel Lezcano #endif
556741bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5568cdb18761SDaniel Lezcano }
5569cdb18761SDaniel Lezcano 
5570d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
5571d189634eSThomas Graf {
5572d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5573c3506372SChristoph Hellwig 	proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
5574c3506372SChristoph Hellwig 			sizeof(struct ipv6_route_iter));
55753617d949SChristoph Hellwig 	proc_create_net_single("rt6_stats", 0444, net->proc_net,
55763617d949SChristoph Hellwig 			rt6_stats_seq_show, NULL);
5577d189634eSThomas Graf #endif
5578d189634eSThomas Graf 	return 0;
5579d189634eSThomas Graf }
5580d189634eSThomas Graf 
5581d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
5582d189634eSThomas Graf {
5583d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5584ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
5585ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
5586d189634eSThomas Graf #endif
5587d189634eSThomas Graf }
5588d189634eSThomas Graf 
5589cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
5590cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
5591cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
5592cdb18761SDaniel Lezcano };
5593cdb18761SDaniel Lezcano 
5594c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
5595c3426b47SDavid S. Miller {
5596c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5597c3426b47SDavid S. Miller 
5598c3426b47SDavid S. Miller 	if (!bp)
5599c3426b47SDavid S. Miller 		return -ENOMEM;
5600c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
5601c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
5602c3426b47SDavid S. Miller 	return 0;
5603c3426b47SDavid S. Miller }
5604c3426b47SDavid S. Miller 
5605c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
5606c3426b47SDavid S. Miller {
5607c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
5608c3426b47SDavid S. Miller 
5609c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
561056a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
5611c3426b47SDavid S. Miller 	kfree(bp);
5612c3426b47SDavid S. Miller }
5613c3426b47SDavid S. Miller 
56142b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
5615c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
5616c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
5617c3426b47SDavid S. Miller };
5618c3426b47SDavid S. Miller 
5619d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
5620d189634eSThomas Graf 	.init = ip6_route_net_init_late,
5621d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
5622d189634eSThomas Graf };
5623d189634eSThomas Graf 
56248ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
56258ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
5626242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
56278ed67789SDaniel Lezcano };
56288ed67789SDaniel Lezcano 
56292f460933SWANG Cong void __init ip6_route_init_special_entries(void)
56302f460933SWANG Cong {
56312f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
56322f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
56332f460933SWANG Cong 	 * manually for init_net */
56341cf844c7SDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
56352f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
56362f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
56372f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
56382f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
56392f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
56402f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
56412f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
56422f460933SWANG Cong   #endif
56432f460933SWANG Cong }
56442f460933SWANG Cong 
5645433d49c3SDaniel Lezcano int __init ip6_route_init(void)
56461da177e4SLinus Torvalds {
5647433d49c3SDaniel Lezcano 	int ret;
56488d0b94afSMartin KaFai Lau 	int cpu;
5649433d49c3SDaniel Lezcano 
56509a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
56519a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
56529a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
56539a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
56549a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
5655c19a28e1SFernando Carrijo 		goto out;
565614e50e57SDavid S. Miller 
5657fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
56588ed67789SDaniel Lezcano 	if (ret)
5659bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
5660bdb3289fSDaniel Lezcano 
5661c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5662c3426b47SDavid S. Miller 	if (ret)
5663e8803b6cSDavid S. Miller 		goto out_dst_entries;
56642a0c451aSThomas Graf 
56657e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
56667e52b33bSDavid S. Miller 	if (ret)
56677e52b33bSDavid S. Miller 		goto out_register_inetpeer;
5668c3426b47SDavid S. Miller 
56695dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
56705dc121e9SArnaud Ebalard 
5671e8803b6cSDavid S. Miller 	ret = fib6_init();
5672433d49c3SDaniel Lezcano 	if (ret)
56738ed67789SDaniel Lezcano 		goto out_register_subsys;
5674433d49c3SDaniel Lezcano 
5675433d49c3SDaniel Lezcano 	ret = xfrm6_init();
5676433d49c3SDaniel Lezcano 	if (ret)
5677e8803b6cSDavid S. Miller 		goto out_fib6_init;
5678c35b7e72SDaniel Lezcano 
5679433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
5680433d49c3SDaniel Lezcano 	if (ret)
5681433d49c3SDaniel Lezcano 		goto xfrm6_init;
56827e5449c2SDaniel Lezcano 
5683d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
5684d189634eSThomas Graf 	if (ret)
5685d189634eSThomas Graf 		goto fib6_rules_init;
5686d189634eSThomas Graf 
568716feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
568816feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
568916feebcfSFlorian Westphal 	if (ret < 0)
569016feebcfSFlorian Westphal 		goto out_register_late_subsys;
569116feebcfSFlorian Westphal 
569216feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
569316feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
569416feebcfSFlorian Westphal 	if (ret < 0)
569516feebcfSFlorian Westphal 		goto out_register_late_subsys;
569616feebcfSFlorian Westphal 
569716feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
569816feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
569916feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
570016feebcfSFlorian Westphal 	if (ret < 0)
5701d189634eSThomas Graf 		goto out_register_late_subsys;
5702433d49c3SDaniel Lezcano 
57038ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
5704cdb18761SDaniel Lezcano 	if (ret)
5705d189634eSThomas Graf 		goto out_register_late_subsys;
57068ed67789SDaniel Lezcano 
57078d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
57088d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
57098d0b94afSMartin KaFai Lau 
57108d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
57118d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
57128d0b94afSMartin KaFai Lau 	}
57138d0b94afSMartin KaFai Lau 
5714433d49c3SDaniel Lezcano out:
5715433d49c3SDaniel Lezcano 	return ret;
5716433d49c3SDaniel Lezcano 
5717d189634eSThomas Graf out_register_late_subsys:
571816feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
5719d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5720433d49c3SDaniel Lezcano fib6_rules_init:
5721433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
5722433d49c3SDaniel Lezcano xfrm6_init:
5723433d49c3SDaniel Lezcano 	xfrm6_fini();
57242a0c451aSThomas Graf out_fib6_init:
57252a0c451aSThomas Graf 	fib6_gc_cleanup();
57268ed67789SDaniel Lezcano out_register_subsys:
57278ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
57287e52b33bSDavid S. Miller out_register_inetpeer:
57297e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
5730fc66f95cSEric Dumazet out_dst_entries:
5731fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5732433d49c3SDaniel Lezcano out_kmem_cache:
5733f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
5734433d49c3SDaniel Lezcano 	goto out;
57351da177e4SLinus Torvalds }
57361da177e4SLinus Torvalds 
57371da177e4SLinus Torvalds void ip6_route_cleanup(void)
57381da177e4SLinus Torvalds {
57398ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
5740d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5741101367c2SThomas Graf 	fib6_rules_cleanup();
57421da177e4SLinus Torvalds 	xfrm6_fini();
57431da177e4SLinus Torvalds 	fib6_gc_cleanup();
5744c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
57458ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
574641bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5747f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
57481da177e4SLinus Torvalds }
5749