xref: /openbmc/linux/net/ipv6/route.c (revision 7dd73168e273938b9e9bb42ca51b0c27d807992b)
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 {
1271c353071aSEric Dumazet 	struct rt6_info *pcpu_rt;
1272d52d3997SMartin KaFai Lau 
1273c353071aSEric Dumazet 	pcpu_rt = this_cpu_read(*res->nh->rt6i_pcpu);
1274d52d3997SMartin KaFai Lau 
1275d4ead6b3SDavid Ahern 	if (pcpu_rt)
127610585b43SDavid Ahern 		ip6_hold_safe(NULL, &pcpu_rt);
1277d3843fe5SWei Wang 
1278a73e4195SMartin KaFai Lau 	return pcpu_rt;
1279a73e4195SMartin KaFai Lau }
1280a73e4195SMartin KaFai Lau 
1281afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1282db3fedeeSDavid Ahern 					    const struct fib6_result *res)
1283a73e4195SMartin KaFai Lau {
1284a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1285d52d3997SMartin KaFai Lau 
1286db3fedeeSDavid Ahern 	pcpu_rt = ip6_rt_pcpu_alloc(res);
1287d52d3997SMartin KaFai Lau 	if (!pcpu_rt) {
12889c7370a1SMartin KaFai Lau 		dst_hold(&net->ipv6.ip6_null_entry->dst);
12899c7370a1SMartin KaFai Lau 		return net->ipv6.ip6_null_entry;
1290d52d3997SMartin KaFai Lau 	}
1291d52d3997SMartin KaFai Lau 
1292a94b9367SWei Wang 	dst_hold(&pcpu_rt->dst);
1293f40b6ae2SDavid Ahern 	p = this_cpu_ptr(res->nh->rt6i_pcpu);
1294d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1295951f788aSEric Dumazet 	BUG_ON(prev);
1296a94b9367SWei Wang 
129761fb0d01SEric Dumazet 	if (res->f6i->fib6_destroying) {
129861fb0d01SEric Dumazet 		struct fib6_info *from;
129961fb0d01SEric Dumazet 
130061fb0d01SEric Dumazet 		from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
130161fb0d01SEric Dumazet 		fib6_info_release(from);
130261fb0d01SEric Dumazet 	}
130361fb0d01SEric Dumazet 
1304d52d3997SMartin KaFai Lau 	return pcpu_rt;
1305d52d3997SMartin KaFai Lau }
1306d52d3997SMartin KaFai Lau 
130735732d01SWei Wang /* exception hash table implementation
130835732d01SWei Wang  */
130935732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
131035732d01SWei Wang 
131135732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
131235732d01SWei Wang  * Caller must hold rt6_exception_lock
131335732d01SWei Wang  */
131435732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
131535732d01SWei Wang 				 struct rt6_exception *rt6_ex)
131635732d01SWei Wang {
1317f5b51fe8SPaolo Abeni 	struct fib6_info *from;
1318b2427e67SColin Ian King 	struct net *net;
131981eb8447SWei Wang 
132035732d01SWei Wang 	if (!bucket || !rt6_ex)
132135732d01SWei Wang 		return;
1322b2427e67SColin Ian King 
1323b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
1324f5b51fe8SPaolo Abeni 	net->ipv6.rt6_stats->fib_rt_cache--;
1325f5b51fe8SPaolo Abeni 
1326f5b51fe8SPaolo Abeni 	/* purge completely the exception to allow releasing the held resources:
1327f5b51fe8SPaolo Abeni 	 * some [sk] cache may keep the dst around for unlimited time
1328f5b51fe8SPaolo Abeni 	 */
13290e233874SEric Dumazet 	from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1330f5b51fe8SPaolo Abeni 	fib6_info_release(from);
1331f5b51fe8SPaolo Abeni 	dst_dev_put(&rt6_ex->rt6i->dst);
1332f5b51fe8SPaolo Abeni 
133335732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
133477634cc6SDavid Ahern 	dst_release(&rt6_ex->rt6i->dst);
133535732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
133635732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
133735732d01SWei Wang 	bucket->depth--;
133835732d01SWei Wang }
133935732d01SWei Wang 
134035732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
134135732d01SWei Wang  * Caller must hold rt6_exception_lock
134235732d01SWei Wang  */
134335732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
134435732d01SWei Wang {
134535732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
134635732d01SWei Wang 
134735732d01SWei Wang 	if (!bucket)
134835732d01SWei Wang 		return;
134935732d01SWei Wang 
135035732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
135135732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
135235732d01SWei Wang 			oldest = rt6_ex;
135335732d01SWei Wang 	}
135435732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
135535732d01SWei Wang }
135635732d01SWei Wang 
135735732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
135835732d01SWei Wang 			      const struct in6_addr *src)
135935732d01SWei Wang {
136035732d01SWei Wang 	static u32 seed __read_mostly;
136135732d01SWei Wang 	u32 val;
136235732d01SWei Wang 
136335732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
136435732d01SWei Wang 	val = jhash(dst, sizeof(*dst), seed);
136535732d01SWei Wang 
136635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
136735732d01SWei Wang 	if (src)
136835732d01SWei Wang 		val = jhash(src, sizeof(*src), val);
136935732d01SWei Wang #endif
137035732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
137135732d01SWei Wang }
137235732d01SWei Wang 
137335732d01SWei Wang /* Helper function to find the cached rt in the hash table
137435732d01SWei Wang  * and update bucket pointer to point to the bucket for this
137535732d01SWei Wang  * (daddr, saddr) pair
137635732d01SWei Wang  * Caller must hold rt6_exception_lock
137735732d01SWei Wang  */
137835732d01SWei Wang static struct rt6_exception *
137935732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
138035732d01SWei Wang 			      const struct in6_addr *daddr,
138135732d01SWei Wang 			      const struct in6_addr *saddr)
138235732d01SWei Wang {
138335732d01SWei Wang 	struct rt6_exception *rt6_ex;
138435732d01SWei Wang 	u32 hval;
138535732d01SWei Wang 
138635732d01SWei Wang 	if (!(*bucket) || !daddr)
138735732d01SWei Wang 		return NULL;
138835732d01SWei Wang 
138935732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
139035732d01SWei Wang 	*bucket += hval;
139135732d01SWei Wang 
139235732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
139335732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
139435732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
139535732d01SWei Wang 
139635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
139735732d01SWei Wang 		if (matched && saddr)
139835732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
139935732d01SWei Wang #endif
140035732d01SWei Wang 		if (matched)
140135732d01SWei Wang 			return rt6_ex;
140235732d01SWei Wang 	}
140335732d01SWei Wang 	return NULL;
140435732d01SWei Wang }
140535732d01SWei Wang 
140635732d01SWei Wang /* Helper function to find the cached rt in the hash table
140735732d01SWei Wang  * and update bucket pointer to point to the bucket for this
140835732d01SWei Wang  * (daddr, saddr) pair
140935732d01SWei Wang  * Caller must hold rcu_read_lock()
141035732d01SWei Wang  */
141135732d01SWei Wang static struct rt6_exception *
141235732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
141335732d01SWei Wang 			 const struct in6_addr *daddr,
141435732d01SWei Wang 			 const struct in6_addr *saddr)
141535732d01SWei Wang {
141635732d01SWei Wang 	struct rt6_exception *rt6_ex;
141735732d01SWei Wang 	u32 hval;
141835732d01SWei Wang 
141935732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
142035732d01SWei Wang 
142135732d01SWei Wang 	if (!(*bucket) || !daddr)
142235732d01SWei Wang 		return NULL;
142335732d01SWei Wang 
142435732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
142535732d01SWei Wang 	*bucket += hval;
142635732d01SWei Wang 
142735732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
142835732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
142935732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
143035732d01SWei Wang 
143135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
143235732d01SWei Wang 		if (matched && saddr)
143335732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
143435732d01SWei Wang #endif
143535732d01SWei Wang 		if (matched)
143635732d01SWei Wang 			return rt6_ex;
143735732d01SWei Wang 	}
143835732d01SWei Wang 	return NULL;
143935732d01SWei Wang }
144035732d01SWei Wang 
1441b748f260SDavid Ahern static unsigned int fib6_mtu(const struct fib6_result *res)
144235732d01SWei Wang {
1443b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
1444d4ead6b3SDavid Ahern 	unsigned int mtu;
1445d4ead6b3SDavid Ahern 
1446b748f260SDavid Ahern 	if (res->f6i->fib6_pmtu) {
1447b748f260SDavid Ahern 		mtu = res->f6i->fib6_pmtu;
1448dcd1f572SDavid Ahern 	} else {
1449b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
1450dcd1f572SDavid Ahern 		struct inet6_dev *idev;
1451dcd1f572SDavid Ahern 
1452dcd1f572SDavid Ahern 		rcu_read_lock();
1453dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
1454dcd1f572SDavid Ahern 		mtu = idev->cnf.mtu6;
1455dcd1f572SDavid Ahern 		rcu_read_unlock();
1456dcd1f572SDavid Ahern 	}
1457dcd1f572SDavid Ahern 
1458d4ead6b3SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1459d4ead6b3SDavid Ahern 
1460b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1461d4ead6b3SDavid Ahern }
1462d4ead6b3SDavid Ahern 
1463cc5c073aSDavid Ahern #define FIB6_EXCEPTION_BUCKET_FLUSHED  0x1UL
1464cc5c073aSDavid Ahern 
1465cc5c073aSDavid Ahern /* used when the flushed bit is not relevant, only access to the bucket
1466cc5c073aSDavid Ahern  * (ie., all bucket users except rt6_insert_exception);
1467cc5c073aSDavid Ahern  *
1468cc5c073aSDavid Ahern  * called under rcu lock; sometimes called with rt6_exception_lock held
1469cc5c073aSDavid Ahern  */
1470cc5c073aSDavid Ahern static
1471cc5c073aSDavid Ahern struct rt6_exception_bucket *fib6_nh_get_excptn_bucket(const struct fib6_nh *nh,
1472cc5c073aSDavid Ahern 						       spinlock_t *lock)
1473cc5c073aSDavid Ahern {
1474cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
1475cc5c073aSDavid Ahern 
1476cc5c073aSDavid Ahern 	if (lock)
1477cc5c073aSDavid Ahern 		bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1478cc5c073aSDavid Ahern 						   lockdep_is_held(lock));
1479cc5c073aSDavid Ahern 	else
1480cc5c073aSDavid Ahern 		bucket = rcu_dereference(nh->rt6i_exception_bucket);
1481cc5c073aSDavid Ahern 
1482cc5c073aSDavid Ahern 	/* remove bucket flushed bit if set */
1483cc5c073aSDavid Ahern 	if (bucket) {
1484cc5c073aSDavid Ahern 		unsigned long p = (unsigned long)bucket;
1485cc5c073aSDavid Ahern 
1486cc5c073aSDavid Ahern 		p &= ~FIB6_EXCEPTION_BUCKET_FLUSHED;
1487cc5c073aSDavid Ahern 		bucket = (struct rt6_exception_bucket *)p;
1488cc5c073aSDavid Ahern 	}
1489cc5c073aSDavid Ahern 
1490cc5c073aSDavid Ahern 	return bucket;
1491cc5c073aSDavid Ahern }
1492cc5c073aSDavid Ahern 
1493cc5c073aSDavid Ahern static bool fib6_nh_excptn_bucket_flushed(struct rt6_exception_bucket *bucket)
1494cc5c073aSDavid Ahern {
1495cc5c073aSDavid Ahern 	unsigned long p = (unsigned long)bucket;
1496cc5c073aSDavid Ahern 
1497cc5c073aSDavid Ahern 	return !!(p & FIB6_EXCEPTION_BUCKET_FLUSHED);
1498cc5c073aSDavid Ahern }
1499cc5c073aSDavid Ahern 
1500cc5c073aSDavid Ahern /* called with rt6_exception_lock held */
1501cc5c073aSDavid Ahern static void fib6_nh_excptn_bucket_set_flushed(struct fib6_nh *nh,
1502cc5c073aSDavid Ahern 					      spinlock_t *lock)
1503cc5c073aSDavid Ahern {
1504cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
1505cc5c073aSDavid Ahern 	unsigned long p;
1506cc5c073aSDavid Ahern 
1507cc5c073aSDavid Ahern 	bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
1508cc5c073aSDavid Ahern 					   lockdep_is_held(lock));
1509cc5c073aSDavid Ahern 
1510cc5c073aSDavid Ahern 	p = (unsigned long)bucket;
1511cc5c073aSDavid Ahern 	p |= FIB6_EXCEPTION_BUCKET_FLUSHED;
1512cc5c073aSDavid Ahern 	bucket = (struct rt6_exception_bucket *)p;
1513cc5c073aSDavid Ahern 	rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1514cc5c073aSDavid Ahern }
1515cc5c073aSDavid Ahern 
151635732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
15175012f0a5SDavid Ahern 				const struct fib6_result *res)
151835732d01SWei Wang {
15195e670d84SDavid Ahern 	struct net *net = dev_net(nrt->dst.dev);
152035732d01SWei Wang 	struct rt6_exception_bucket *bucket;
1521cc5c073aSDavid Ahern 	struct fib6_info *f6i = res->f6i;
152235732d01SWei Wang 	struct in6_addr *src_key = NULL;
152335732d01SWei Wang 	struct rt6_exception *rt6_ex;
1524cc5c073aSDavid Ahern 	struct fib6_nh *nh = res->nh;
152535732d01SWei Wang 	int err = 0;
152635732d01SWei Wang 
152735732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
152835732d01SWei Wang 
1529cc5c073aSDavid Ahern 	bucket = rcu_dereference_protected(nh->rt6i_exception_bucket,
153035732d01SWei Wang 					  lockdep_is_held(&rt6_exception_lock));
153135732d01SWei Wang 	if (!bucket) {
153235732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
153335732d01SWei Wang 				 GFP_ATOMIC);
153435732d01SWei Wang 		if (!bucket) {
153535732d01SWei Wang 			err = -ENOMEM;
153635732d01SWei Wang 			goto out;
153735732d01SWei Wang 		}
1538cc5c073aSDavid Ahern 		rcu_assign_pointer(nh->rt6i_exception_bucket, bucket);
1539cc5c073aSDavid Ahern 	} else if (fib6_nh_excptn_bucket_flushed(bucket)) {
1540cc5c073aSDavid Ahern 		err = -EINVAL;
1541cc5c073aSDavid Ahern 		goto out;
154235732d01SWei Wang 	}
154335732d01SWei Wang 
154435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
15455012f0a5SDavid Ahern 	/* fib6_src.plen != 0 indicates f6i is in subtree
154635732d01SWei Wang 	 * and exception table is indexed by a hash of
15475012f0a5SDavid Ahern 	 * both fib6_dst and fib6_src.
154835732d01SWei Wang 	 * Otherwise, the exception table is indexed by
15495012f0a5SDavid Ahern 	 * a hash of only fib6_dst.
155035732d01SWei Wang 	 */
15515012f0a5SDavid Ahern 	if (f6i->fib6_src.plen)
155235732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
155335732d01SWei Wang #endif
15545012f0a5SDavid Ahern 	/* rt6_mtu_change() might lower mtu on f6i.
1555f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
15565012f0a5SDavid Ahern 	 * is less than f6i's mtu value.
1557f5bbe7eeSWei Wang 	 */
1558b748f260SDavid Ahern 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1559f5bbe7eeSWei Wang 		err = -EINVAL;
1560f5bbe7eeSWei Wang 		goto out;
1561f5bbe7eeSWei Wang 	}
156260006a48SWei Wang 
156335732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
156435732d01SWei Wang 					       src_key);
156535732d01SWei Wang 	if (rt6_ex)
156635732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
156735732d01SWei Wang 
156835732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
156935732d01SWei Wang 	if (!rt6_ex) {
157035732d01SWei Wang 		err = -ENOMEM;
157135732d01SWei Wang 		goto out;
157235732d01SWei Wang 	}
157335732d01SWei Wang 	rt6_ex->rt6i = nrt;
157435732d01SWei Wang 	rt6_ex->stamp = jiffies;
157535732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
157635732d01SWei Wang 	bucket->depth++;
157781eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
157835732d01SWei Wang 
157935732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
158035732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
158135732d01SWei Wang 
158235732d01SWei Wang out:
158335732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
158435732d01SWei Wang 
158535732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1586b886d5f2SPaolo Abeni 	if (!err) {
15875012f0a5SDavid Ahern 		spin_lock_bh(&f6i->fib6_table->tb6_lock);
15885012f0a5SDavid Ahern 		fib6_update_sernum(net, f6i);
15895012f0a5SDavid Ahern 		spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1590b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1591b886d5f2SPaolo Abeni 	}
159235732d01SWei Wang 
159335732d01SWei Wang 	return err;
159435732d01SWei Wang }
159535732d01SWei Wang 
1596c0b220cfSDavid Ahern static void fib6_nh_flush_exceptions(struct fib6_nh *nh, struct fib6_info *from)
159735732d01SWei Wang {
159835732d01SWei Wang 	struct rt6_exception_bucket *bucket;
159935732d01SWei Wang 	struct rt6_exception *rt6_ex;
160035732d01SWei Wang 	struct hlist_node *tmp;
160135732d01SWei Wang 	int i;
160235732d01SWei Wang 
160335732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
160435732d01SWei Wang 
1605cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
160635732d01SWei Wang 	if (!bucket)
160735732d01SWei Wang 		goto out;
160835732d01SWei Wang 
1609cc5c073aSDavid Ahern 	/* Prevent rt6_insert_exception() to recreate the bucket list */
1610cc5c073aSDavid Ahern 	if (!from)
1611cc5c073aSDavid Ahern 		fib6_nh_excptn_bucket_set_flushed(nh, &rt6_exception_lock);
1612cc5c073aSDavid Ahern 
161335732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1614cc5c073aSDavid Ahern 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) {
1615cc5c073aSDavid Ahern 			if (!from ||
1616cc5c073aSDavid Ahern 			    rcu_access_pointer(rt6_ex->rt6i->from) == from)
161735732d01SWei Wang 				rt6_remove_exception(bucket, rt6_ex);
1618cc5c073aSDavid Ahern 		}
1619cc5c073aSDavid Ahern 		WARN_ON_ONCE(!from && bucket->depth);
162035732d01SWei Wang 		bucket++;
162135732d01SWei Wang 	}
162235732d01SWei Wang out:
162335732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
162435732d01SWei Wang }
162535732d01SWei Wang 
1626c0b220cfSDavid Ahern void rt6_flush_exceptions(struct fib6_info *f6i)
1627c0b220cfSDavid Ahern {
16281cf844c7SDavid Ahern 	fib6_nh_flush_exceptions(f6i->fib6_nh, f6i);
1629c0b220cfSDavid Ahern }
1630c0b220cfSDavid Ahern 
163135732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
163235732d01SWei Wang  * Caller has to hold rcu_read_lock()
163335732d01SWei Wang  */
16347e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1635510e2cedSWei Wang 					   const struct in6_addr *daddr,
1636510e2cedSWei Wang 					   const struct in6_addr *saddr)
163735732d01SWei Wang {
1638510e2cedSWei Wang 	const struct in6_addr *src_key = NULL;
163935732d01SWei Wang 	struct rt6_exception_bucket *bucket;
164035732d01SWei Wang 	struct rt6_exception *rt6_ex;
16417e4b5128SDavid Ahern 	struct rt6_info *ret = NULL;
164235732d01SWei Wang 
164335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
16447e4b5128SDavid Ahern 	/* fib6i_src.plen != 0 indicates f6i is in subtree
164535732d01SWei Wang 	 * and exception table is indexed by a hash of
16467e4b5128SDavid Ahern 	 * both fib6_dst and fib6_src.
1647510e2cedSWei Wang 	 * However, the src addr used to create the hash
1648510e2cedSWei Wang 	 * might not be exactly the passed in saddr which
1649510e2cedSWei Wang 	 * is a /128 addr from the flow.
1650510e2cedSWei Wang 	 * So we need to use f6i->fib6_src to redo lookup
1651510e2cedSWei Wang 	 * if the passed in saddr does not find anything.
1652510e2cedSWei Wang 	 * (See the logic in ip6_rt_cache_alloc() on how
1653510e2cedSWei Wang 	 * rt->rt6i_src is updated.)
165435732d01SWei Wang 	 */
16557e4b5128SDavid Ahern 	if (res->f6i->fib6_src.plen)
165635732d01SWei Wang 		src_key = saddr;
1657510e2cedSWei Wang find_ex:
165835732d01SWei Wang #endif
1659cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(res->nh, NULL);
166035732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
166135732d01SWei Wang 
166235732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
16637e4b5128SDavid Ahern 		ret = rt6_ex->rt6i;
166435732d01SWei Wang 
1665510e2cedSWei Wang #ifdef CONFIG_IPV6_SUBTREES
1666510e2cedSWei Wang 	/* Use fib6_src as src_key and redo lookup */
1667510e2cedSWei Wang 	if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1668510e2cedSWei Wang 		src_key = &res->f6i->fib6_src.addr;
1669510e2cedSWei Wang 		goto find_ex;
1670510e2cedSWei Wang 	}
1671510e2cedSWei Wang #endif
1672510e2cedSWei Wang 
16737e4b5128SDavid Ahern 	return ret;
167435732d01SWei Wang }
167535732d01SWei Wang 
167635732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
1677cc5c073aSDavid Ahern static int fib6_nh_remove_exception(const struct fib6_nh *nh, int plen,
1678c0b220cfSDavid Ahern 				    const struct rt6_info *rt)
167935732d01SWei Wang {
1680c0b220cfSDavid Ahern 	const struct in6_addr *src_key = NULL;
168135732d01SWei Wang 	struct rt6_exception_bucket *bucket;
168235732d01SWei Wang 	struct rt6_exception *rt6_ex;
168335732d01SWei Wang 	int err;
168435732d01SWei Wang 
1685cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
168635732d01SWei Wang 		return -ENOENT;
168735732d01SWei Wang 
168835732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1689cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1690cc5c073aSDavid Ahern 
169135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
1692cc5c073aSDavid Ahern 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
1693cc5c073aSDavid Ahern 	 * and exception table is indexed by a hash of
1694cc5c073aSDavid Ahern 	 * both rt6i_dst and rt6i_src.
169535732d01SWei Wang 	 * Otherwise, the exception table is indexed by
169635732d01SWei Wang 	 * a hash of only rt6i_dst.
169735732d01SWei Wang 	 */
1698c0b220cfSDavid Ahern 	if (plen)
169935732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
170035732d01SWei Wang #endif
170135732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
170235732d01SWei Wang 					       &rt->rt6i_dst.addr,
170335732d01SWei Wang 					       src_key);
170435732d01SWei Wang 	if (rt6_ex) {
170535732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
170635732d01SWei Wang 		err = 0;
170735732d01SWei Wang 	} else {
170835732d01SWei Wang 		err = -ENOENT;
170935732d01SWei Wang 	}
171035732d01SWei Wang 
171135732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
171235732d01SWei Wang 	return err;
171335732d01SWei Wang }
171435732d01SWei Wang 
1715c0b220cfSDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt)
1716c0b220cfSDavid Ahern {
1717c0b220cfSDavid Ahern 	struct fib6_info *from;
1718c0b220cfSDavid Ahern 
1719c0b220cfSDavid Ahern 	from = rcu_dereference(rt->from);
1720cc5c073aSDavid Ahern 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1721c0b220cfSDavid Ahern 		return -EINVAL;
1722c0b220cfSDavid Ahern 
17231cf844c7SDavid Ahern 	return fib6_nh_remove_exception(from->fib6_nh,
1724cc5c073aSDavid Ahern 					from->fib6_src.plen, rt);
1725c0b220cfSDavid Ahern }
1726c0b220cfSDavid Ahern 
172735732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
172835732d01SWei Wang  * refresh its stamp
172935732d01SWei Wang  */
1730cc5c073aSDavid Ahern static void fib6_nh_update_exception(const struct fib6_nh *nh, int plen,
1731c0b220cfSDavid Ahern 				     const struct rt6_info *rt)
173235732d01SWei Wang {
1733c0b220cfSDavid Ahern 	const struct in6_addr *src_key = NULL;
173435732d01SWei Wang 	struct rt6_exception_bucket *bucket;
173535732d01SWei Wang 	struct rt6_exception *rt6_ex;
1736193f3685SPaolo Abeni 
1737cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, NULL);
173835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
1739cc5c073aSDavid Ahern 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
1740cc5c073aSDavid Ahern 	 * and exception table is indexed by a hash of
1741cc5c073aSDavid Ahern 	 * both rt6i_dst and rt6i_src.
174235732d01SWei Wang 	 * Otherwise, the exception table is indexed by
174335732d01SWei Wang 	 * a hash of only rt6i_dst.
174435732d01SWei Wang 	 */
1745c0b220cfSDavid Ahern 	if (plen)
174635732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
174735732d01SWei Wang #endif
1748cc5c073aSDavid Ahern 	rt6_ex = __rt6_find_exception_rcu(&bucket, &rt->rt6i_dst.addr, src_key);
174935732d01SWei Wang 	if (rt6_ex)
175035732d01SWei Wang 		rt6_ex->stamp = jiffies;
1751c0b220cfSDavid Ahern }
175235732d01SWei Wang 
1753c0b220cfSDavid Ahern static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
1754c0b220cfSDavid Ahern {
1755c0b220cfSDavid Ahern 	struct fib6_info *from;
1756c0b220cfSDavid Ahern 
1757c0b220cfSDavid Ahern 	rcu_read_lock();
1758c0b220cfSDavid Ahern 
1759c0b220cfSDavid Ahern 	from = rcu_dereference(rt->from);
1760c0b220cfSDavid Ahern 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1761c0b220cfSDavid Ahern 		goto unlock;
1762c0b220cfSDavid Ahern 
17631cf844c7SDavid Ahern 	fib6_nh_update_exception(from->fib6_nh, from->fib6_src.plen, rt);
1764193f3685SPaolo Abeni unlock:
176535732d01SWei Wang 	rcu_read_unlock();
176635732d01SWei Wang }
176735732d01SWei Wang 
1768e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1769e9fa1495SStefano Brivio 					 struct rt6_info *rt, int mtu)
1770e9fa1495SStefano Brivio {
1771e9fa1495SStefano Brivio 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1772e9fa1495SStefano Brivio 	 * lowest MTU in the path: always allow updating the route PMTU to
1773e9fa1495SStefano Brivio 	 * reflect PMTU decreases.
1774e9fa1495SStefano Brivio 	 *
1775e9fa1495SStefano Brivio 	 * If the new MTU is higher, and the route PMTU is equal to the local
1776e9fa1495SStefano Brivio 	 * MTU, this means the old MTU is the lowest in the path, so allow
1777e9fa1495SStefano Brivio 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1778e9fa1495SStefano Brivio 	 * handle this.
1779e9fa1495SStefano Brivio 	 */
1780e9fa1495SStefano Brivio 
1781e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) >= mtu)
1782e9fa1495SStefano Brivio 		return true;
1783e9fa1495SStefano Brivio 
1784e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1785e9fa1495SStefano Brivio 		return true;
1786e9fa1495SStefano Brivio 
1787e9fa1495SStefano Brivio 	return false;
1788e9fa1495SStefano Brivio }
1789e9fa1495SStefano Brivio 
1790e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
1791cc5c073aSDavid Ahern 				       const struct fib6_nh *nh, int mtu)
1792f5bbe7eeSWei Wang {
1793f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
1794f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
1795f5bbe7eeSWei Wang 	int i;
1796f5bbe7eeSWei Wang 
1797cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1798e9fa1495SStefano Brivio 	if (!bucket)
1799e9fa1495SStefano Brivio 		return;
1800e9fa1495SStefano Brivio 
1801f5bbe7eeSWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1802f5bbe7eeSWei Wang 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1803f5bbe7eeSWei Wang 			struct rt6_info *entry = rt6_ex->rt6i;
1804e9fa1495SStefano Brivio 
1805e9fa1495SStefano Brivio 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
1806d4ead6b3SDavid Ahern 			 * route), the metrics of its rt->from have already
1807f5bbe7eeSWei Wang 			 * been updated.
1808f5bbe7eeSWei Wang 			 */
1809d4ead6b3SDavid Ahern 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
1810e9fa1495SStefano Brivio 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
1811d4ead6b3SDavid Ahern 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
1812f5bbe7eeSWei Wang 		}
1813f5bbe7eeSWei Wang 		bucket++;
1814f5bbe7eeSWei Wang 	}
1815f5bbe7eeSWei Wang }
1816f5bbe7eeSWei Wang 
1817b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
1818b16cb459SWei Wang 
1819cc5c073aSDavid Ahern static void fib6_nh_exceptions_clean_tohost(const struct fib6_nh *nh,
1820cc5c073aSDavid Ahern 					    const struct in6_addr *gateway)
1821b16cb459SWei Wang {
1822b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
1823b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
1824b16cb459SWei Wang 	struct hlist_node *tmp;
1825b16cb459SWei Wang 	int i;
1826b16cb459SWei Wang 
1827cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
1828b16cb459SWei Wang 		return;
1829b16cb459SWei Wang 
1830b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1831cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1832b16cb459SWei Wang 	if (bucket) {
1833b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1834b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1835b16cb459SWei Wang 						  &bucket->chain, hlist) {
1836b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1837b16cb459SWei Wang 
1838b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1839b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
1840b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
1841b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
1842b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
1843b16cb459SWei Wang 				}
1844b16cb459SWei Wang 			}
1845b16cb459SWei Wang 			bucket++;
1846b16cb459SWei Wang 		}
1847b16cb459SWei Wang 	}
1848b16cb459SWei Wang 
1849b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1850b16cb459SWei Wang }
1851b16cb459SWei Wang 
1852c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1853c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
1854c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
1855c757faa8SWei Wang 				      unsigned long now)
1856c757faa8SWei Wang {
1857c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
1858c757faa8SWei Wang 
18591859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
18601859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
18611859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
18621859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
18631859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
18641859bac0SPaolo Abeni 	 */
186531afeb42SWei Wang 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
186631afeb42SWei Wang 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1867c757faa8SWei Wang 			RT6_TRACE("aging clone %p\n", rt);
1868c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1869c757faa8SWei Wang 			return;
187031afeb42SWei Wang 		}
187131afeb42SWei Wang 	} else if (time_after(jiffies, rt->dst.expires)) {
187231afeb42SWei Wang 		RT6_TRACE("purging expired route %p\n", rt);
187331afeb42SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
187431afeb42SWei Wang 		return;
187531afeb42SWei Wang 	}
187631afeb42SWei Wang 
187731afeb42SWei Wang 	if (rt->rt6i_flags & RTF_GATEWAY) {
1878c757faa8SWei Wang 		struct neighbour *neigh;
1879c757faa8SWei Wang 		__u8 neigh_flags = 0;
1880c757faa8SWei Wang 
18811bfa26ffSEric Dumazet 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
18821bfa26ffSEric Dumazet 		if (neigh)
1883c757faa8SWei Wang 			neigh_flags = neigh->flags;
18841bfa26ffSEric Dumazet 
1885c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
1886c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
1887c757faa8SWei Wang 				  rt);
1888c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1889c757faa8SWei Wang 			return;
1890c757faa8SWei Wang 		}
1891c757faa8SWei Wang 	}
189231afeb42SWei Wang 
1893c757faa8SWei Wang 	gc_args->more++;
1894c757faa8SWei Wang }
1895c757faa8SWei Wang 
1896cc5c073aSDavid Ahern static void fib6_nh_age_exceptions(const struct fib6_nh *nh,
1897c757faa8SWei Wang 				   struct fib6_gc_args *gc_args,
1898c757faa8SWei Wang 				   unsigned long now)
1899c757faa8SWei Wang {
1900c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
1901c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
1902c757faa8SWei Wang 	struct hlist_node *tmp;
1903c757faa8SWei Wang 	int i;
1904c757faa8SWei Wang 
1905cc5c073aSDavid Ahern 	if (!rcu_access_pointer(nh->rt6i_exception_bucket))
1906c757faa8SWei Wang 		return;
1907c757faa8SWei Wang 
19081bfa26ffSEric Dumazet 	rcu_read_lock_bh();
19091bfa26ffSEric Dumazet 	spin_lock(&rt6_exception_lock);
1910cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(nh, &rt6_exception_lock);
1911c757faa8SWei Wang 	if (bucket) {
1912c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1913c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1914c757faa8SWei Wang 						  &bucket->chain, hlist) {
1915c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
1916c757faa8SWei Wang 							  gc_args, now);
1917c757faa8SWei Wang 			}
1918c757faa8SWei Wang 			bucket++;
1919c757faa8SWei Wang 		}
1920c757faa8SWei Wang 	}
19211bfa26ffSEric Dumazet 	spin_unlock(&rt6_exception_lock);
19221bfa26ffSEric Dumazet 	rcu_read_unlock_bh();
1923c757faa8SWei Wang }
1924c757faa8SWei Wang 
1925cc5c073aSDavid Ahern void rt6_age_exceptions(struct fib6_info *f6i,
1926c0b220cfSDavid Ahern 			struct fib6_gc_args *gc_args,
1927c0b220cfSDavid Ahern 			unsigned long now)
1928c0b220cfSDavid Ahern {
19291cf844c7SDavid Ahern 	fib6_nh_age_exceptions(f6i->fib6_nh, gc_args, now);
1930c0b220cfSDavid Ahern }
1931c0b220cfSDavid Ahern 
19321d053da9SDavid Ahern /* must be called with rcu lock held */
1933effda4ddSDavid Ahern int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
1934effda4ddSDavid Ahern 		      struct flowi6 *fl6, struct fib6_result *res, int strict)
19351da177e4SLinus Torvalds {
1936367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
19371da177e4SLinus Torvalds 
19386454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1939367efcb9SMartin KaFai Lau 	saved_fn = fn;
19401da177e4SLinus Torvalds 
1941ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1942ca254490SDavid Ahern 		oif = 0;
1943ca254490SDavid Ahern 
1944a3c00e46SMartin KaFai Lau redo_rt6_select:
1945effda4ddSDavid Ahern 	rt6_select(net, fn, oif, res, strict);
1946effda4ddSDavid Ahern 	if (res->f6i == net->ipv6.fib6_null_entry) {
1947a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1948a3c00e46SMartin KaFai Lau 		if (fn)
1949a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
1950367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
1951367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
1952367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
1953367efcb9SMartin KaFai Lau 			fn = saved_fn;
1954367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
1955367efcb9SMartin KaFai Lau 		}
1956a3c00e46SMartin KaFai Lau 	}
1957a3c00e46SMartin KaFai Lau 
1958effda4ddSDavid Ahern 	trace_fib6_table_lookup(net, res, table, fl6);
1959d52d3997SMartin KaFai Lau 
1960effda4ddSDavid Ahern 	return 0;
19611d053da9SDavid Ahern }
19621d053da9SDavid Ahern 
19631d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
19641d053da9SDavid Ahern 			       int oif, struct flowi6 *fl6,
19651d053da9SDavid Ahern 			       const struct sk_buff *skb, int flags)
19661d053da9SDavid Ahern {
1967b1d40991SDavid Ahern 	struct fib6_result res = {};
19681d053da9SDavid Ahern 	struct rt6_info *rt;
19691d053da9SDavid Ahern 	int strict = 0;
19701d053da9SDavid Ahern 
19711d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IFACE;
19721d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
19731d053da9SDavid Ahern 	if (net->ipv6.devconf_all->forwarding == 0)
19741d053da9SDavid Ahern 		strict |= RT6_LOOKUP_F_REACHABLE;
19751d053da9SDavid Ahern 
19761d053da9SDavid Ahern 	rcu_read_lock();
19771d053da9SDavid Ahern 
1978effda4ddSDavid Ahern 	fib6_table_lookup(net, table, oif, fl6, &res, strict);
1979b1d40991SDavid Ahern 	if (res.f6i == net->ipv6.fib6_null_entry) {
1980421842edSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
198166f5d6ceSWei Wang 		rcu_read_unlock();
1982d3843fe5SWei Wang 		dst_hold(&rt->dst);
1983d3843fe5SWei Wang 		return rt;
1984d3843fe5SWei Wang 	}
198523fb93a4SDavid Ahern 
1986b1d40991SDavid Ahern 	fib6_select_path(net, &res, fl6, oif, false, skb, strict);
1987d83009d4SDavid Ahern 
198823fb93a4SDavid Ahern 	/*Search through exception table */
19897e4b5128SDavid Ahern 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
199023fb93a4SDavid Ahern 	if (rt) {
199110585b43SDavid Ahern 		if (ip6_hold_safe(net, &rt))
19921da177e4SLinus Torvalds 			dst_use_noref(&rt->dst, jiffies);
1993d4ead6b3SDavid Ahern 
199466f5d6ceSWei Wang 		rcu_read_unlock();
1995d52d3997SMartin KaFai Lau 		return rt;
19963da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
1997b1d40991SDavid Ahern 			    !res.nh->fib_nh_gw_family)) {
19983da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
19993da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
20003da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
20013da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
20023da59bd9SMartin KaFai Lau 		 */
20033da59bd9SMartin KaFai Lau 		struct rt6_info *uncached_rt;
20043da59bd9SMartin KaFai Lau 
200585bd05deSDavid Ahern 		uncached_rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
2006d52d3997SMartin KaFai Lau 
20074d85cd0cSDavid Ahern 		rcu_read_unlock();
20083da59bd9SMartin KaFai Lau 
20091cfb71eeSWei Wang 		if (uncached_rt) {
20101cfb71eeSWei Wang 			/* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
20111cfb71eeSWei Wang 			 * No need for another dst_hold()
20121cfb71eeSWei Wang 			 */
20138d0b94afSMartin KaFai Lau 			rt6_uncached_list_add(uncached_rt);
201481eb8447SWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
20151cfb71eeSWei Wang 		} else {
20163da59bd9SMartin KaFai Lau 			uncached_rt = net->ipv6.ip6_null_entry;
20173da59bd9SMartin KaFai Lau 			dst_hold(&uncached_rt->dst);
20181cfb71eeSWei Wang 		}
2019b811580dSDavid Ahern 
20203da59bd9SMartin KaFai Lau 		return uncached_rt;
2021d52d3997SMartin KaFai Lau 	} else {
2022d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
2023d52d3997SMartin KaFai Lau 
2024d52d3997SMartin KaFai Lau 		struct rt6_info *pcpu_rt;
2025d52d3997SMartin KaFai Lau 
2026951f788aSEric Dumazet 		local_bh_disable();
2027db3fedeeSDavid Ahern 		pcpu_rt = rt6_get_pcpu_route(&res);
2028d52d3997SMartin KaFai Lau 
202993531c67SDavid Ahern 		if (!pcpu_rt)
2030db3fedeeSDavid Ahern 			pcpu_rt = rt6_make_pcpu_route(net, &res);
203193531c67SDavid Ahern 
2032951f788aSEric Dumazet 		local_bh_enable();
2033951f788aSEric Dumazet 		rcu_read_unlock();
2034d4bea421SDavid Ahern 
2035d52d3997SMartin KaFai Lau 		return pcpu_rt;
2036d52d3997SMartin KaFai Lau 	}
2037c71099acSThomas Graf }
20389ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
2039c71099acSThomas Graf 
2040b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net,
2041b75cc8f9SDavid Ahern 					    struct fib6_table *table,
2042b75cc8f9SDavid Ahern 					    struct flowi6 *fl6,
2043b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2044b75cc8f9SDavid Ahern 					    int flags)
20454acad72dSPavel Emelyanov {
2046b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
20474acad72dSPavel Emelyanov }
20484acad72dSPavel Emelyanov 
2049d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
205072331bc0SShmulik Ladkani 					 struct net_device *dev,
2051b75cc8f9SDavid Ahern 					 struct flowi6 *fl6,
2052b75cc8f9SDavid Ahern 					 const struct sk_buff *skb,
2053b75cc8f9SDavid Ahern 					 int flags)
205472331bc0SShmulik Ladkani {
205572331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
205672331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
205772331bc0SShmulik Ladkani 
2058b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
205972331bc0SShmulik Ladkani }
2060d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
206172331bc0SShmulik Ladkani 
206223aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
20635e5d6fedSRoopa Prabhu 				  struct flow_keys *keys,
20645e5d6fedSRoopa Prabhu 				  struct flow_keys *flkeys)
206523aebdacSJakub Sitnicki {
206623aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
206723aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
20685e5d6fedSRoopa Prabhu 	struct flow_keys *_flkeys = flkeys;
206923aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
207023aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
207123aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
2072cea67a2dSEric Dumazet 	struct icmp6hdr _icmph;
207323aebdacSJakub Sitnicki 
207423aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
207523aebdacSJakub Sitnicki 		goto out;
207623aebdacSJakub Sitnicki 
2077cea67a2dSEric Dumazet 	icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2078cea67a2dSEric Dumazet 				   sizeof(_icmph), &_icmph);
2079cea67a2dSEric Dumazet 	if (!icmph)
2080cea67a2dSEric Dumazet 		goto out;
2081cea67a2dSEric Dumazet 
208223aebdacSJakub Sitnicki 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
208323aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
208423aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
208523aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
208623aebdacSJakub Sitnicki 		goto out;
208723aebdacSJakub Sitnicki 
208823aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
208923aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
209023aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
209123aebdacSJakub Sitnicki 	if (!inner_iph)
209223aebdacSJakub Sitnicki 		goto out;
209323aebdacSJakub Sitnicki 
209423aebdacSJakub Sitnicki 	key_iph = inner_iph;
20955e5d6fedSRoopa Prabhu 	_flkeys = NULL;
209623aebdacSJakub Sitnicki out:
20975e5d6fedSRoopa Prabhu 	if (_flkeys) {
20985e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
20995e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
21005e5d6fedSRoopa Prabhu 		keys->tags.flow_label = _flkeys->tags.flow_label;
21015e5d6fedSRoopa Prabhu 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
21025e5d6fedSRoopa Prabhu 	} else {
210323aebdacSJakub Sitnicki 		keys->addrs.v6addrs.src = key_iph->saddr;
210423aebdacSJakub Sitnicki 		keys->addrs.v6addrs.dst = key_iph->daddr;
2105fa1be7e0SMichal Kubecek 		keys->tags.flow_label = ip6_flowlabel(key_iph);
210623aebdacSJakub Sitnicki 		keys->basic.ip_proto = key_iph->nexthdr;
210723aebdacSJakub Sitnicki 	}
21085e5d6fedSRoopa Prabhu }
210923aebdacSJakub Sitnicki 
211023aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
2111b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2112b4bac172SDavid Ahern 		       const struct sk_buff *skb, struct flow_keys *flkeys)
211323aebdacSJakub Sitnicki {
211423aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
21159a2a537aSDavid Ahern 	u32 mhash;
211623aebdacSJakub Sitnicki 
2117bbfa047aSDavid S. Miller 	switch (ip6_multipath_hash_policy(net)) {
2118b4bac172SDavid Ahern 	case 0:
21196f74b6c2SDavid Ahern 		memset(&hash_keys, 0, sizeof(hash_keys));
21206f74b6c2SDavid Ahern 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
21219a2a537aSDavid Ahern 		if (skb) {
21225e5d6fedSRoopa Prabhu 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
21239a2a537aSDavid Ahern 		} else {
21249a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
21259a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2126fa1be7e0SMichal Kubecek 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
21279a2a537aSDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
212823aebdacSJakub Sitnicki 		}
2129b4bac172SDavid Ahern 		break;
2130b4bac172SDavid Ahern 	case 1:
2131b4bac172SDavid Ahern 		if (skb) {
2132b4bac172SDavid Ahern 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2133b4bac172SDavid Ahern 			struct flow_keys keys;
2134b4bac172SDavid Ahern 
2135b4bac172SDavid Ahern 			/* short-circuit if we already have L4 hash present */
2136b4bac172SDavid Ahern 			if (skb->l4_hash)
2137b4bac172SDavid Ahern 				return skb_get_hash_raw(skb) >> 1;
2138b4bac172SDavid Ahern 
2139b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2140b4bac172SDavid Ahern 
2141b4bac172SDavid Ahern                         if (!flkeys) {
2142b4bac172SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2143b4bac172SDavid Ahern 				flkeys = &keys;
2144b4bac172SDavid Ahern 			}
2145b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2146b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2147b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2148b4bac172SDavid Ahern 			hash_keys.ports.src = flkeys->ports.src;
2149b4bac172SDavid Ahern 			hash_keys.ports.dst = flkeys->ports.dst;
2150b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2151b4bac172SDavid Ahern 		} else {
2152b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2153b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2154b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2155b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2156b4bac172SDavid Ahern 			hash_keys.ports.src = fl6->fl6_sport;
2157b4bac172SDavid Ahern 			hash_keys.ports.dst = fl6->fl6_dport;
2158b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2159b4bac172SDavid Ahern 		}
2160b4bac172SDavid Ahern 		break;
2161b4bac172SDavid Ahern 	}
21629a2a537aSDavid Ahern 	mhash = flow_hash_from_keys(&hash_keys);
216323aebdacSJakub Sitnicki 
21649a2a537aSDavid Ahern 	return mhash >> 1;
216523aebdacSJakub Sitnicki }
216623aebdacSJakub Sitnicki 
2167c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
2168c71099acSThomas Graf {
2169b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2170c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
2171adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2172904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
21734c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
2174e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
21754c9483b2SDavid S. Miller 		.daddr = iph->daddr,
21764c9483b2SDavid S. Miller 		.saddr = iph->saddr,
21776502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
21784c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
21794c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
2180c71099acSThomas Graf 	};
21815e5d6fedSRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2182adaa70bbSThomas Graf 
2183904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
218446fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2185904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
21865e5d6fedSRoopa Prabhu 
21875e5d6fedSRoopa Prabhu 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
21885e5d6fedSRoopa Prabhu 		flkeys = &_flkeys;
21895e5d6fedSRoopa Prabhu 
219023aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2191b4bac172SDavid Ahern 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
219206e9d040SJiri Benc 	skb_dst_drop(skb);
2193b75cc8f9SDavid Ahern 	skb_dst_set(skb,
2194b75cc8f9SDavid Ahern 		    ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
2195c71099acSThomas Graf }
2196c71099acSThomas Graf 
2197b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net,
2198b75cc8f9SDavid Ahern 					     struct fib6_table *table,
2199b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
2200b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2201b75cc8f9SDavid Ahern 					     int flags)
2202c71099acSThomas Graf {
2203b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2204c71099acSThomas Graf }
2205c71099acSThomas Graf 
22066f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
22076f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags)
2208c71099acSThomas Graf {
2209d46a9d67SDavid Ahern 	bool any_src;
2210c71099acSThomas Graf 
22113ede0bbcSRobert Shearman 	if (ipv6_addr_type(&fl6->daddr) &
22123ede0bbcSRobert Shearman 	    (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
22134c1feac5SDavid Ahern 		struct dst_entry *dst;
22144c1feac5SDavid Ahern 
22154c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
2216ca254490SDavid Ahern 		if (dst)
2217ca254490SDavid Ahern 			return dst;
22184c1feac5SDavid Ahern 	}
2219ca254490SDavid Ahern 
22201fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
22214dc27d1cSDavid McCullough 
2222d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
2223741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2224d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
222577d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
2226c71099acSThomas Graf 
2227d46a9d67SDavid Ahern 	if (!any_src)
2228adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
22290c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
22300c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2231adaa70bbSThomas Graf 
2232b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
22331da177e4SLinus Torvalds }
22346f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
22351da177e4SLinus Torvalds 
22362774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
223714e50e57SDavid S. Miller {
22385c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
22391dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
224014e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
224114e50e57SDavid S. Miller 
22421dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
224362cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
224414e50e57SDavid S. Miller 	if (rt) {
22450a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
224681eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
22470a1f5962SMartin KaFai Lau 
2248d8d1f30bSChangli Gao 		new = &rt->dst;
224914e50e57SDavid S. Miller 		new->__use = 1;
2250352e512cSHerbert Xu 		new->input = dst_discard;
2251ede2059dSEric W. Biederman 		new->output = dst_discard_out;
225214e50e57SDavid S. Miller 
2253defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
225414e50e57SDavid S. Miller 
22551dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
22564e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
22570a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
225814e50e57SDavid S. Miller 
225914e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
226014e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
226114e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
226214e50e57SDavid S. Miller #endif
226314e50e57SDavid S. Miller 	}
226414e50e57SDavid S. Miller 
226569ead7afSDavid S. Miller 	dst_release(dst_orig);
226669ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
226714e50e57SDavid S. Miller }
226814e50e57SDavid S. Miller 
22691da177e4SLinus Torvalds /*
22701da177e4SLinus Torvalds  *	Destination cache support functions
22711da177e4SLinus Torvalds  */
22721da177e4SLinus Torvalds 
22738d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie)
22743da59bd9SMartin KaFai Lau {
227536143645SSteffen Klassert 	u32 rt_cookie = 0;
2276c5cff856SWei Wang 
22778ae86971SDavid Ahern 	if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
227893531c67SDavid Ahern 		return false;
227993531c67SDavid Ahern 
228093531c67SDavid Ahern 	if (fib6_check_expired(f6i))
228193531c67SDavid Ahern 		return false;
228293531c67SDavid Ahern 
228393531c67SDavid Ahern 	return true;
228493531c67SDavid Ahern }
228593531c67SDavid Ahern 
2286a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt,
2287a68886a6SDavid Ahern 				   struct fib6_info *from,
2288a68886a6SDavid Ahern 				   u32 cookie)
22893da59bd9SMartin KaFai Lau {
2290c5cff856SWei Wang 	u32 rt_cookie = 0;
2291c5cff856SWei Wang 
2292a68886a6SDavid Ahern 	if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
229393531c67SDavid Ahern 	    rt_cookie != cookie)
22943da59bd9SMartin KaFai Lau 		return NULL;
22953da59bd9SMartin KaFai Lau 
22963da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
22973da59bd9SMartin KaFai Lau 		return NULL;
22983da59bd9SMartin KaFai Lau 
22993da59bd9SMartin KaFai Lau 	return &rt->dst;
23003da59bd9SMartin KaFai Lau }
23013da59bd9SMartin KaFai Lau 
2302a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2303a68886a6SDavid Ahern 					    struct fib6_info *from,
2304a68886a6SDavid Ahern 					    u32 cookie)
23053da59bd9SMartin KaFai Lau {
23065973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
23075973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2308a68886a6SDavid Ahern 	    fib6_check(from, cookie))
23093da59bd9SMartin KaFai Lau 		return &rt->dst;
23103da59bd9SMartin KaFai Lau 	else
23113da59bd9SMartin KaFai Lau 		return NULL;
23123da59bd9SMartin KaFai Lau }
23133da59bd9SMartin KaFai Lau 
23141da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
23151da177e4SLinus Torvalds {
2316a87b7dc9SDavid Ahern 	struct dst_entry *dst_ret;
2317a68886a6SDavid Ahern 	struct fib6_info *from;
23181da177e4SLinus Torvalds 	struct rt6_info *rt;
23191da177e4SLinus Torvalds 
2320a87b7dc9SDavid Ahern 	rt = container_of(dst, struct rt6_info, dst);
2321a87b7dc9SDavid Ahern 
2322a87b7dc9SDavid Ahern 	rcu_read_lock();
23231da177e4SLinus Torvalds 
23246f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
23256f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
23266f3118b5SNicolas Dichtel 	 * into this function always.
23276f3118b5SNicolas Dichtel 	 */
2328e3bc10bdSHannes Frederic Sowa 
2329a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
23304b32b5adSMartin KaFai Lau 
2331a68886a6SDavid Ahern 	if (from && (rt->rt6i_flags & RTF_PCPU ||
2332a68886a6SDavid Ahern 	    unlikely(!list_empty(&rt->rt6i_uncached))))
2333a68886a6SDavid Ahern 		dst_ret = rt6_dst_from_check(rt, from, cookie);
23343da59bd9SMartin KaFai Lau 	else
2335a68886a6SDavid Ahern 		dst_ret = rt6_check(rt, from, cookie);
2336a87b7dc9SDavid Ahern 
2337a87b7dc9SDavid Ahern 	rcu_read_unlock();
2338a87b7dc9SDavid Ahern 
2339a87b7dc9SDavid Ahern 	return dst_ret;
23401da177e4SLinus Torvalds }
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
23431da177e4SLinus Torvalds {
23441da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	if (rt) {
234754c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
2348c3c14da0SDavid Ahern 			rcu_read_lock();
234954c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
235093531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
235154c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
23521da177e4SLinus Torvalds 			}
2353c3c14da0SDavid Ahern 			rcu_read_unlock();
235454c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
235554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
235654c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
235754c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
235854c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
235954c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
23601da177e4SLinus Torvalds }
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
23631da177e4SLinus Torvalds {
23641da177e4SLinus Torvalds 	struct rt6_info *rt;
23651da177e4SLinus Torvalds 
23663ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
23671da177e4SLinus Torvalds 
2368adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
23691da177e4SLinus Torvalds 	if (rt) {
23708a14e46fSDavid Ahern 		rcu_read_lock();
23711eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
237293531c67SDavid Ahern 			rt6_remove_exception_rt(rt);
2373c5cff856SWei Wang 		} else {
2374a68886a6SDavid Ahern 			struct fib6_info *from;
2375c5cff856SWei Wang 			struct fib6_node *fn;
2376c5cff856SWei Wang 
2377a68886a6SDavid Ahern 			from = rcu_dereference(rt->from);
2378a68886a6SDavid Ahern 			if (from) {
2379a68886a6SDavid Ahern 				fn = rcu_dereference(from->fib6_node);
2380c5cff856SWei Wang 				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2381c5cff856SWei Wang 					fn->fn_sernum = -1;
2382a68886a6SDavid Ahern 			}
23831da177e4SLinus Torvalds 		}
23841da177e4SLinus Torvalds 		rcu_read_unlock();
23851da177e4SLinus Torvalds 	}
23861da177e4SLinus Torvalds }
23871da177e4SLinus Torvalds 
23886a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout)
23896a3e030fSDavid Ahern {
2390a68886a6SDavid Ahern 	if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2391a68886a6SDavid Ahern 		struct fib6_info *from;
2392a68886a6SDavid Ahern 
2393a68886a6SDavid Ahern 		rcu_read_lock();
2394a68886a6SDavid Ahern 		from = rcu_dereference(rt0->from);
2395a68886a6SDavid Ahern 		if (from)
2396a68886a6SDavid Ahern 			rt0->dst.expires = from->expires;
2397a68886a6SDavid Ahern 		rcu_read_unlock();
2398a68886a6SDavid Ahern 	}
23996a3e030fSDavid Ahern 
24006a3e030fSDavid Ahern 	dst_set_expires(&rt0->dst, timeout);
24016a3e030fSDavid Ahern 	rt0->rt6i_flags |= RTF_EXPIRES;
24026700c270SDavid S. Miller }
24031da177e4SLinus Torvalds 
240445e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
240545e4fd26SMartin KaFai Lau {
240645e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
240745e4fd26SMartin KaFai Lau 
2408d4ead6b3SDavid Ahern 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
240945e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
241045e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
241145e4fd26SMartin KaFai Lau }
241245e4fd26SMartin KaFai Lau 
24130d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
24140d3f6d29SMartin KaFai Lau {
24150d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
24161490ed2aSPaolo Abeni 		(rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
24170d3f6d29SMartin KaFai Lau }
24180d3f6d29SMartin KaFai Lau 
241945e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
242045e4fd26SMartin KaFai Lau 				 const struct ipv6hdr *iph, u32 mtu)
24211da177e4SLinus Torvalds {
24220dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
24231da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
24241da177e4SLinus Torvalds 
242519bda36cSXin Long 	if (dst_metric_locked(dst, RTAX_MTU))
242619bda36cSXin Long 		return;
242719bda36cSXin Long 
242845e4fd26SMartin KaFai Lau 	if (iph) {
242945e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
243045e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
243145e4fd26SMartin KaFai Lau 	} else if (sk) {
243245e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
243345e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
243445e4fd26SMartin KaFai Lau 	} else {
24350dec879fSJulian Anastasov 		daddr = NULL;
24360dec879fSJulian Anastasov 		saddr = NULL;
24371da177e4SLinus Torvalds 	}
24380dec879fSJulian Anastasov 	dst_confirm_neigh(dst, daddr);
24390dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
24400dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
24410dec879fSJulian Anastasov 		return;
24420dec879fSJulian Anastasov 
24430dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
24440dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
24452b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
24462b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
24472b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
24480dec879fSJulian Anastasov 	} else if (daddr) {
244985bd05deSDavid Ahern 		struct fib6_result res = {};
24500dec879fSJulian Anastasov 		struct rt6_info *nrt6;
24510dec879fSJulian Anastasov 
24524d85cd0cSDavid Ahern 		rcu_read_lock();
245385bd05deSDavid Ahern 		res.f6i = rcu_dereference(rt6->from);
245485bd05deSDavid Ahern 		if (!res.f6i) {
24559c69a132SJonathan Lemon 			rcu_read_unlock();
24569c69a132SJonathan Lemon 			return;
24579c69a132SJonathan Lemon 		}
24581cf844c7SDavid Ahern 		res.nh = res.f6i->fib6_nh;
24597d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
24607d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
24617d21fec9SDavid Ahern 
246285bd05deSDavid Ahern 		nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
246345e4fd26SMartin KaFai Lau 		if (nrt6) {
246445e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
24655012f0a5SDavid Ahern 			if (rt6_insert_exception(nrt6, &res))
24662b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
246745e4fd26SMartin KaFai Lau 		}
2468a68886a6SDavid Ahern 		rcu_read_unlock();
246945e4fd26SMartin KaFai Lau 	}
247045e4fd26SMartin KaFai Lau }
247145e4fd26SMartin KaFai Lau 
247245e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
247345e4fd26SMartin KaFai Lau 			       struct sk_buff *skb, u32 mtu)
247445e4fd26SMartin KaFai Lau {
247545e4fd26SMartin KaFai Lau 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
24761da177e4SLinus Torvalds }
24771da177e4SLinus Torvalds 
247842ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2479e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
248081aded24SDavid S. Miller {
248181aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
248281aded24SDavid S. Miller 	struct dst_entry *dst;
2483dc92095dSMaciej Żenczykowski 	struct flowi6 fl6 = {
2484dc92095dSMaciej Żenczykowski 		.flowi6_oif = oif,
2485dc92095dSMaciej Żenczykowski 		.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2486dc92095dSMaciej Żenczykowski 		.daddr = iph->daddr,
2487dc92095dSMaciej Żenczykowski 		.saddr = iph->saddr,
2488dc92095dSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
2489dc92095dSMaciej Żenczykowski 		.flowi6_uid = uid,
2490dc92095dSMaciej Żenczykowski 	};
249181aded24SDavid S. Miller 
249281aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
249381aded24SDavid S. Miller 	if (!dst->error)
249445e4fd26SMartin KaFai Lau 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
249581aded24SDavid S. Miller 	dst_release(dst);
249681aded24SDavid S. Miller }
249781aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
249881aded24SDavid S. Miller 
249981aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
250081aded24SDavid S. Miller {
25017ddacfa5SDavid Ahern 	int oif = sk->sk_bound_dev_if;
250233c162a9SMartin KaFai Lau 	struct dst_entry *dst;
250333c162a9SMartin KaFai Lau 
25047ddacfa5SDavid Ahern 	if (!oif && skb->dev)
25057ddacfa5SDavid Ahern 		oif = l3mdev_master_ifindex(skb->dev);
25067ddacfa5SDavid Ahern 
25077ddacfa5SDavid Ahern 	ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
250833c162a9SMartin KaFai Lau 
250933c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
251033c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
251133c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
251233c162a9SMartin KaFai Lau 		return;
251333c162a9SMartin KaFai Lau 
251433c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
251533c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
251633c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
251733c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
251881aded24SDavid S. Miller }
251981aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
252081aded24SDavid S. Miller 
25217d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
25227d6850f7SAlexey Kodanev 			   const struct flowi6 *fl6)
25237d6850f7SAlexey Kodanev {
25247d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
25257d6850f7SAlexey Kodanev 	struct ipv6_pinfo *np = inet6_sk(sk);
25267d6850f7SAlexey Kodanev #endif
25277d6850f7SAlexey Kodanev 
25287d6850f7SAlexey Kodanev 	ip6_dst_store(sk, dst,
25297d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
25307d6850f7SAlexey Kodanev 		      &sk->sk_v6_daddr : NULL,
25317d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
25327d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
25337d6850f7SAlexey Kodanev 		      &np->saddr :
25347d6850f7SAlexey Kodanev #endif
25357d6850f7SAlexey Kodanev 		      NULL);
25367d6850f7SAlexey Kodanev }
25377d6850f7SAlexey Kodanev 
25389b6b35abSDavid Ahern static bool ip6_redirect_nh_match(const struct fib6_result *res,
25390b34eb00SDavid Ahern 				  struct flowi6 *fl6,
25400b34eb00SDavid Ahern 				  const struct in6_addr *gw,
25410b34eb00SDavid Ahern 				  struct rt6_info **ret)
25420b34eb00SDavid Ahern {
25439b6b35abSDavid Ahern 	const struct fib6_nh *nh = res->nh;
25449b6b35abSDavid Ahern 
25450b34eb00SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
25460b34eb00SDavid Ahern 	    fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
25470b34eb00SDavid Ahern 		return false;
25480b34eb00SDavid Ahern 
25490b34eb00SDavid Ahern 	/* rt_cache's gateway might be different from its 'parent'
25500b34eb00SDavid Ahern 	 * in the case of an ip redirect.
25510b34eb00SDavid Ahern 	 * So we keep searching in the exception table if the gateway
25520b34eb00SDavid Ahern 	 * is different.
25530b34eb00SDavid Ahern 	 */
25540b34eb00SDavid Ahern 	if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
25550b34eb00SDavid Ahern 		struct rt6_info *rt_cache;
25560b34eb00SDavid Ahern 
25579b6b35abSDavid Ahern 		rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
25580b34eb00SDavid Ahern 		if (rt_cache &&
25590b34eb00SDavid Ahern 		    ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
25600b34eb00SDavid Ahern 			*ret = rt_cache;
25610b34eb00SDavid Ahern 			return true;
25620b34eb00SDavid Ahern 		}
25630b34eb00SDavid Ahern 		return false;
25640b34eb00SDavid Ahern 	}
25650b34eb00SDavid Ahern 	return true;
25660b34eb00SDavid Ahern }
25670b34eb00SDavid Ahern 
2568b55b76b2SDuan Jiong /* Handle redirects */
2569b55b76b2SDuan Jiong struct ip6rd_flowi {
2570b55b76b2SDuan Jiong 	struct flowi6 fl6;
2571b55b76b2SDuan Jiong 	struct in6_addr gateway;
2572b55b76b2SDuan Jiong };
2573b55b76b2SDuan Jiong 
2574b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
2575b55b76b2SDuan Jiong 					     struct fib6_table *table,
2576b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2577b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2578b55b76b2SDuan Jiong 					     int flags)
2579b55b76b2SDuan Jiong {
2580b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
25810b34eb00SDavid Ahern 	struct rt6_info *ret = NULL;
25829b6b35abSDavid Ahern 	struct fib6_result res = {};
25838d1c802bSDavid Ahern 	struct fib6_info *rt;
2584b55b76b2SDuan Jiong 	struct fib6_node *fn;
2585b55b76b2SDuan Jiong 
258631680ac2SDavid Ahern 	/* l3mdev_update_flow overrides oif if the device is enslaved; in
258731680ac2SDavid Ahern 	 * this case we must match on the real ingress device, so reset it
258831680ac2SDavid Ahern 	 */
258931680ac2SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
259031680ac2SDavid Ahern 		fl6->flowi6_oif = skb->dev->ifindex;
259131680ac2SDavid Ahern 
2592b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
259367c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2594b55b76b2SDuan Jiong 	 *
2595b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2596b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2597b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2598b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2599b55b76b2SDuan Jiong 	 * routes.
2600b55b76b2SDuan Jiong 	 */
2601b55b76b2SDuan Jiong 
260266f5d6ceSWei Wang 	rcu_read_lock();
26036454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2604b55b76b2SDuan Jiong restart:
260566f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
26069b6b35abSDavid Ahern 		res.f6i = rt;
26071cf844c7SDavid Ahern 		res.nh = rt->fib6_nh;
26089b6b35abSDavid Ahern 
260914895687SDavid Ahern 		if (fib6_check_expired(rt))
2610b55b76b2SDuan Jiong 			continue;
261193c2fb25SDavid Ahern 		if (rt->fib6_flags & RTF_REJECT)
2612b55b76b2SDuan Jiong 			break;
26139b6b35abSDavid Ahern 		if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, &ret))
26140b34eb00SDavid Ahern 			goto out;
2615b55b76b2SDuan Jiong 	}
2616b55b76b2SDuan Jiong 
2617b55b76b2SDuan Jiong 	if (!rt)
2618421842edSDavid Ahern 		rt = net->ipv6.fib6_null_entry;
261993c2fb25SDavid Ahern 	else if (rt->fib6_flags & RTF_REJECT) {
262023fb93a4SDavid Ahern 		ret = net->ipv6.ip6_null_entry;
2621b0a1ba59SMartin KaFai Lau 		goto out;
2622b0a1ba59SMartin KaFai Lau 	}
2623b0a1ba59SMartin KaFai Lau 
2624421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
2625a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2626a3c00e46SMartin KaFai Lau 		if (fn)
2627a3c00e46SMartin KaFai Lau 			goto restart;
2628b55b76b2SDuan Jiong 	}
2629a3c00e46SMartin KaFai Lau 
26309b6b35abSDavid Ahern 	res.f6i = rt;
26311cf844c7SDavid Ahern 	res.nh = rt->fib6_nh;
2632b0a1ba59SMartin KaFai Lau out:
26337d21fec9SDavid Ahern 	if (ret) {
263410585b43SDavid Ahern 		ip6_hold_safe(net, &ret);
26357d21fec9SDavid Ahern 	} else {
26367d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
26377d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
26389b6b35abSDavid Ahern 		ret = ip6_create_rt_rcu(&res);
26397d21fec9SDavid Ahern 	}
2640b55b76b2SDuan Jiong 
264166f5d6ceSWei Wang 	rcu_read_unlock();
2642b55b76b2SDuan Jiong 
26438ff2e5b2SDavid Ahern 	trace_fib6_table_lookup(net, &res, table, fl6);
264423fb93a4SDavid Ahern 	return ret;
2645b55b76b2SDuan Jiong };
2646b55b76b2SDuan Jiong 
2647b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
2648b55b76b2SDuan Jiong 					    const struct flowi6 *fl6,
2649b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2650b55b76b2SDuan Jiong 					    const struct in6_addr *gateway)
2651b55b76b2SDuan Jiong {
2652b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2653b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
2654b55b76b2SDuan Jiong 
2655b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
2656b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
2657b55b76b2SDuan Jiong 
2658b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, &rdfl.fl6, skb,
2659b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
2660b55b76b2SDuan Jiong }
2661b55b76b2SDuan Jiong 
2662e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2663e2d118a1SLorenzo Colitti 		  kuid_t uid)
26643a5ad2eeSDavid S. Miller {
26653a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
26663a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
26671f7f10acSMaciej Żenczykowski 	struct flowi6 fl6 = {
26681f7f10acSMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
26691f7f10acSMaciej Żenczykowski 		.flowi6_oif = oif,
26701f7f10acSMaciej Żenczykowski 		.flowi6_mark = mark,
26711f7f10acSMaciej Żenczykowski 		.daddr = iph->daddr,
26721f7f10acSMaciej Żenczykowski 		.saddr = iph->saddr,
26731f7f10acSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
26741f7f10acSMaciej Żenczykowski 		.flowi6_uid = uid,
26751f7f10acSMaciej Żenczykowski 	};
26763a5ad2eeSDavid S. Miller 
2677b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
26786700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
26793a5ad2eeSDavid S. Miller 	dst_release(dst);
26803a5ad2eeSDavid S. Miller }
26813a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
26823a5ad2eeSDavid S. Miller 
2683d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
2684c92a59ecSDuan Jiong {
2685c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2686c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2687c92a59ecSDuan Jiong 	struct dst_entry *dst;
26880b26fb17SMaciej Żenczykowski 	struct flowi6 fl6 = {
26890b26fb17SMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
26900b26fb17SMaciej Żenczykowski 		.flowi6_oif = oif,
26910b26fb17SMaciej Żenczykowski 		.daddr = msg->dest,
26920b26fb17SMaciej Żenczykowski 		.saddr = iph->daddr,
26930b26fb17SMaciej Żenczykowski 		.flowi6_uid = sock_net_uid(net, NULL),
26940b26fb17SMaciej Żenczykowski 	};
2695c92a59ecSDuan Jiong 
2696b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
2697c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
2698c92a59ecSDuan Jiong 	dst_release(dst);
2699c92a59ecSDuan Jiong }
2700c92a59ecSDuan Jiong 
27013a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
27023a5ad2eeSDavid S. Miller {
2703e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2704e2d118a1SLorenzo Colitti 		     sk->sk_uid);
27053a5ad2eeSDavid S. Miller }
27063a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
27073a5ad2eeSDavid S. Miller 
27080dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
27091da177e4SLinus Torvalds {
27100dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
27110dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
27120dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
27130dbaee3bSDavid S. Miller 
27141da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
27151da177e4SLinus Torvalds 
27165578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
27175578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds 	/*
27201da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
27211da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
27221da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
27231da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
27241da177e4SLinus Torvalds 	 */
27251da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
27261da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
27271da177e4SLinus Torvalds 	return mtu;
27281da177e4SLinus Torvalds }
27291da177e4SLinus Torvalds 
2730ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
2731d33e4553SDavid S. Miller {
2732d33e4553SDavid S. Miller 	struct inet6_dev *idev;
2733d4ead6b3SDavid Ahern 	unsigned int mtu;
2734618f9bc7SSteffen Klassert 
27354b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
27364b32b5adSMartin KaFai Lau 	if (mtu)
27374b32b5adSMartin KaFai Lau 		goto out;
27384b32b5adSMartin KaFai Lau 
2739618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
2740d33e4553SDavid S. Miller 
2741d33e4553SDavid S. Miller 	rcu_read_lock();
2742d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
2743d33e4553SDavid S. Miller 	if (idev)
2744d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
2745d33e4553SDavid S. Miller 	rcu_read_unlock();
2746d33e4553SDavid S. Miller 
274730f78d8eSEric Dumazet out:
274814972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
274914972cbdSRoopa Prabhu 
275014972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
2751d33e4553SDavid S. Miller }
2752d33e4553SDavid S. Miller 
2753901731b8SDavid Ahern /* MTU selection:
2754901731b8SDavid Ahern  * 1. mtu on route is locked - use it
2755901731b8SDavid Ahern  * 2. mtu from nexthop exception
2756901731b8SDavid Ahern  * 3. mtu from egress device
2757901731b8SDavid Ahern  *
2758901731b8SDavid Ahern  * based on ip6_dst_mtu_forward and exception logic of
2759901731b8SDavid Ahern  * rt6_find_cached_rt; called with rcu_read_lock
2760901731b8SDavid Ahern  */
2761b748f260SDavid Ahern u32 ip6_mtu_from_fib6(const struct fib6_result *res,
2762b748f260SDavid Ahern 		      const struct in6_addr *daddr,
2763b748f260SDavid Ahern 		      const struct in6_addr *saddr)
2764901731b8SDavid Ahern {
2765b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
2766b748f260SDavid Ahern 	struct fib6_info *f6i = res->f6i;
2767901731b8SDavid Ahern 	struct inet6_dev *idev;
2768510e2cedSWei Wang 	struct rt6_info *rt;
2769901731b8SDavid Ahern 	u32 mtu = 0;
2770901731b8SDavid Ahern 
2771901731b8SDavid Ahern 	if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
2772901731b8SDavid Ahern 		mtu = f6i->fib6_pmtu;
2773901731b8SDavid Ahern 		if (mtu)
2774901731b8SDavid Ahern 			goto out;
2775901731b8SDavid Ahern 	}
2776901731b8SDavid Ahern 
2777510e2cedSWei Wang 	rt = rt6_find_cached_rt(res, daddr, saddr);
2778510e2cedSWei Wang 	if (unlikely(rt)) {
2779510e2cedSWei Wang 		mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
2780510e2cedSWei Wang 	} else {
2781b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
2782901731b8SDavid Ahern 
2783901731b8SDavid Ahern 		mtu = IPV6_MIN_MTU;
2784901731b8SDavid Ahern 		idev = __in6_dev_get(dev);
2785901731b8SDavid Ahern 		if (idev && idev->cnf.mtu6 > mtu)
2786901731b8SDavid Ahern 			mtu = idev->cnf.mtu6;
2787901731b8SDavid Ahern 	}
2788901731b8SDavid Ahern 
2789901731b8SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2790901731b8SDavid Ahern out:
2791b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
2792901731b8SDavid Ahern }
2793901731b8SDavid Ahern 
27943b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
279587a11578SDavid S. Miller 				  struct flowi6 *fl6)
27961da177e4SLinus Torvalds {
279787a11578SDavid S. Miller 	struct dst_entry *dst;
27981da177e4SLinus Torvalds 	struct rt6_info *rt;
27991da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
2800c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
28011da177e4SLinus Torvalds 
280238308473SDavid S. Miller 	if (unlikely(!idev))
2803122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
28041da177e4SLinus Torvalds 
2805ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
280638308473SDavid S. Miller 	if (unlikely(!rt)) {
28071da177e4SLinus Torvalds 		in6_dev_put(idev);
280887a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
28091da177e4SLinus Torvalds 		goto out;
28101da177e4SLinus Torvalds 	}
28111da177e4SLinus Torvalds 
28128e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
2813588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
28148e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
2815550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
281687a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
28178e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
28188e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
281914edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
28201da177e4SLinus Torvalds 
28214c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
2822587fea74SWei Wang 	 * do proper release of the net_device
2823587fea74SWei Wang 	 */
2824587fea74SWei Wang 	rt6_uncached_list_add(rt);
282581eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
28261da177e4SLinus Torvalds 
282787a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
282887a11578SDavid S. Miller 
28291da177e4SLinus Torvalds out:
283087a11578SDavid S. Miller 	return dst;
28311da177e4SLinus Torvalds }
28321da177e4SLinus Torvalds 
2833569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
28341da177e4SLinus Torvalds {
283586393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
28367019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
28377019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
28387019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
28397019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
28407019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
2841fc66f95cSEric Dumazet 	int entries;
28421da177e4SLinus Torvalds 
2843fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
284449a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
2845fc66f95cSEric Dumazet 	    entries <= rt_max_size)
28461da177e4SLinus Torvalds 		goto out;
28471da177e4SLinus Torvalds 
28486891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
284914956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
2850fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
2851fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
28527019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
28531da177e4SLinus Torvalds out:
28547019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
2855fc66f95cSEric Dumazet 	return entries > rt_max_size;
28561da177e4SLinus Torvalds }
28571da177e4SLinus Torvalds 
28588c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net,
28598c14586fSDavid Ahern 					    struct fib6_config *cfg,
2860f4797b33SDavid Ahern 					    const struct in6_addr *gw_addr,
2861f4797b33SDavid Ahern 					    u32 tbid, int flags)
28628c14586fSDavid Ahern {
28638c14586fSDavid Ahern 	struct flowi6 fl6 = {
28648c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
28658c14586fSDavid Ahern 		.daddr = *gw_addr,
28668c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
28678c14586fSDavid Ahern 	};
28688c14586fSDavid Ahern 	struct fib6_table *table;
28698c14586fSDavid Ahern 	struct rt6_info *rt;
28708c14586fSDavid Ahern 
2871f4797b33SDavid Ahern 	table = fib6_get_table(net, tbid);
28728c14586fSDavid Ahern 	if (!table)
28738c14586fSDavid Ahern 		return NULL;
28748c14586fSDavid Ahern 
28758c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
28768c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
28778c14586fSDavid Ahern 
2878f4797b33SDavid Ahern 	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
2879b75cc8f9SDavid Ahern 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
28808c14586fSDavid Ahern 
28818c14586fSDavid Ahern 	/* if table lookup failed, fall back to full lookup */
28828c14586fSDavid Ahern 	if (rt == net->ipv6.ip6_null_entry) {
28838c14586fSDavid Ahern 		ip6_rt_put(rt);
28848c14586fSDavid Ahern 		rt = NULL;
28858c14586fSDavid Ahern 	}
28868c14586fSDavid Ahern 
28878c14586fSDavid Ahern 	return rt;
28888c14586fSDavid Ahern }
28898c14586fSDavid Ahern 
2890fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net,
2891fc1e64e1SDavid Ahern 				     struct fib6_config *cfg,
28929fbb704cSDavid Ahern 				     const struct net_device *dev,
2893fc1e64e1SDavid Ahern 				     struct netlink_ext_ack *extack)
2894fc1e64e1SDavid Ahern {
289544750f84SDavid Ahern 	u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
2896fc1e64e1SDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
2897fc1e64e1SDavid Ahern 	u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2898bf1dc8baSPaolo Abeni 	struct fib6_info *from;
2899fc1e64e1SDavid Ahern 	struct rt6_info *grt;
2900fc1e64e1SDavid Ahern 	int err;
2901fc1e64e1SDavid Ahern 
2902fc1e64e1SDavid Ahern 	err = 0;
2903fc1e64e1SDavid Ahern 	grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2904fc1e64e1SDavid Ahern 	if (grt) {
2905bf1dc8baSPaolo Abeni 		rcu_read_lock();
2906bf1dc8baSPaolo Abeni 		from = rcu_dereference(grt->from);
290758e354c0SDavid Ahern 		if (!grt->dst.error &&
29084ed591c8SDavid Ahern 		    /* ignore match if it is the default route */
2909bf1dc8baSPaolo Abeni 		    from && !ipv6_addr_any(&from->fib6_dst.addr) &&
291058e354c0SDavid Ahern 		    (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
291144750f84SDavid Ahern 			NL_SET_ERR_MSG(extack,
291244750f84SDavid Ahern 				       "Nexthop has invalid gateway or device mismatch");
2913fc1e64e1SDavid Ahern 			err = -EINVAL;
2914fc1e64e1SDavid Ahern 		}
2915bf1dc8baSPaolo Abeni 		rcu_read_unlock();
2916fc1e64e1SDavid Ahern 
2917fc1e64e1SDavid Ahern 		ip6_rt_put(grt);
2918fc1e64e1SDavid Ahern 	}
2919fc1e64e1SDavid Ahern 
2920fc1e64e1SDavid Ahern 	return err;
2921fc1e64e1SDavid Ahern }
2922fc1e64e1SDavid Ahern 
29231edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net,
29241edce99fSDavid Ahern 			      struct fib6_config *cfg,
29251edce99fSDavid Ahern 			      struct net_device **_dev,
29261edce99fSDavid Ahern 			      struct inet6_dev **idev)
29271edce99fSDavid Ahern {
29281edce99fSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
29291edce99fSDavid Ahern 	struct net_device *dev = _dev ? *_dev : NULL;
29301edce99fSDavid Ahern 	struct rt6_info *grt = NULL;
29311edce99fSDavid Ahern 	int err = -EHOSTUNREACH;
29321edce99fSDavid Ahern 
29331edce99fSDavid Ahern 	if (cfg->fc_table) {
2934f4797b33SDavid Ahern 		int flags = RT6_LOOKUP_F_IFACE;
2935f4797b33SDavid Ahern 
2936f4797b33SDavid Ahern 		grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2937f4797b33SDavid Ahern 					  cfg->fc_table, flags);
29381edce99fSDavid Ahern 		if (grt) {
29391edce99fSDavid Ahern 			if (grt->rt6i_flags & RTF_GATEWAY ||
29401edce99fSDavid Ahern 			    (dev && dev != grt->dst.dev)) {
29411edce99fSDavid Ahern 				ip6_rt_put(grt);
29421edce99fSDavid Ahern 				grt = NULL;
29431edce99fSDavid Ahern 			}
29441edce99fSDavid Ahern 		}
29451edce99fSDavid Ahern 	}
29461edce99fSDavid Ahern 
29471edce99fSDavid Ahern 	if (!grt)
2948b75cc8f9SDavid Ahern 		grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
29491edce99fSDavid Ahern 
29501edce99fSDavid Ahern 	if (!grt)
29511edce99fSDavid Ahern 		goto out;
29521edce99fSDavid Ahern 
29531edce99fSDavid Ahern 	if (dev) {
29541edce99fSDavid Ahern 		if (dev != grt->dst.dev) {
29551edce99fSDavid Ahern 			ip6_rt_put(grt);
29561edce99fSDavid Ahern 			goto out;
29571edce99fSDavid Ahern 		}
29581edce99fSDavid Ahern 	} else {
29591edce99fSDavid Ahern 		*_dev = dev = grt->dst.dev;
29601edce99fSDavid Ahern 		*idev = grt->rt6i_idev;
29611edce99fSDavid Ahern 		dev_hold(dev);
29621edce99fSDavid Ahern 		in6_dev_hold(grt->rt6i_idev);
29631edce99fSDavid Ahern 	}
29641edce99fSDavid Ahern 
29651edce99fSDavid Ahern 	if (!(grt->rt6i_flags & RTF_GATEWAY))
29661edce99fSDavid Ahern 		err = 0;
29671edce99fSDavid Ahern 
29681edce99fSDavid Ahern 	ip6_rt_put(grt);
29691edce99fSDavid Ahern 
29701edce99fSDavid Ahern out:
29711edce99fSDavid Ahern 	return err;
29721edce99fSDavid Ahern }
29731edce99fSDavid Ahern 
29749fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
29759fbb704cSDavid Ahern 			   struct net_device **_dev, struct inet6_dev **idev,
29769fbb704cSDavid Ahern 			   struct netlink_ext_ack *extack)
29779fbb704cSDavid Ahern {
29789fbb704cSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
29799fbb704cSDavid Ahern 	int gwa_type = ipv6_addr_type(gw_addr);
2980232378e8SDavid Ahern 	bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
29819fbb704cSDavid Ahern 	const struct net_device *dev = *_dev;
2982232378e8SDavid Ahern 	bool need_addr_check = !dev;
29839fbb704cSDavid Ahern 	int err = -EINVAL;
29849fbb704cSDavid Ahern 
29859fbb704cSDavid Ahern 	/* if gw_addr is local we will fail to detect this in case
29869fbb704cSDavid Ahern 	 * address is still TENTATIVE (DAD in progress). rt6_lookup()
29879fbb704cSDavid Ahern 	 * will return already-added prefix route via interface that
29889fbb704cSDavid Ahern 	 * prefix route was assigned to, which might be non-loopback.
29899fbb704cSDavid Ahern 	 */
2990232378e8SDavid Ahern 	if (dev &&
2991232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2992232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
29939fbb704cSDavid Ahern 		goto out;
29949fbb704cSDavid Ahern 	}
29959fbb704cSDavid Ahern 
29969fbb704cSDavid Ahern 	if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
29979fbb704cSDavid Ahern 		/* IPv6 strictly inhibits using not link-local
29989fbb704cSDavid Ahern 		 * addresses as nexthop address.
29999fbb704cSDavid Ahern 		 * Otherwise, router will not able to send redirects.
30009fbb704cSDavid Ahern 		 * It is very good, but in some (rare!) circumstances
30019fbb704cSDavid Ahern 		 * (SIT, PtP, NBMA NOARP links) it is handy to allow
30029fbb704cSDavid Ahern 		 * some exceptions. --ANK
30039fbb704cSDavid Ahern 		 * We allow IPv4-mapped nexthops to support RFC4798-type
30049fbb704cSDavid Ahern 		 * addressing
30059fbb704cSDavid Ahern 		 */
30069fbb704cSDavid Ahern 		if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
30079fbb704cSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
30089fbb704cSDavid Ahern 			goto out;
30099fbb704cSDavid Ahern 		}
30109fbb704cSDavid Ahern 
30119fbb704cSDavid Ahern 		if (cfg->fc_flags & RTNH_F_ONLINK)
30129fbb704cSDavid Ahern 			err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
30139fbb704cSDavid Ahern 		else
30149fbb704cSDavid Ahern 			err = ip6_route_check_nh(net, cfg, _dev, idev);
30159fbb704cSDavid Ahern 
30169fbb704cSDavid Ahern 		if (err)
30179fbb704cSDavid Ahern 			goto out;
30189fbb704cSDavid Ahern 	}
30199fbb704cSDavid Ahern 
30209fbb704cSDavid Ahern 	/* reload in case device was changed */
30219fbb704cSDavid Ahern 	dev = *_dev;
30229fbb704cSDavid Ahern 
30239fbb704cSDavid Ahern 	err = -EINVAL;
30249fbb704cSDavid Ahern 	if (!dev) {
30259fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Egress device not specified");
30269fbb704cSDavid Ahern 		goto out;
30279fbb704cSDavid Ahern 	} else if (dev->flags & IFF_LOOPBACK) {
30289fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack,
30299fbb704cSDavid Ahern 			       "Egress device can not be loopback device for this route");
30309fbb704cSDavid Ahern 		goto out;
30319fbb704cSDavid Ahern 	}
3032232378e8SDavid Ahern 
3033232378e8SDavid Ahern 	/* if we did not check gw_addr above, do so now that the
3034232378e8SDavid Ahern 	 * egress device has been resolved.
3035232378e8SDavid Ahern 	 */
3036232378e8SDavid Ahern 	if (need_addr_check &&
3037232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
3038232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
3039232378e8SDavid Ahern 		goto out;
3040232378e8SDavid Ahern 	}
3041232378e8SDavid Ahern 
30429fbb704cSDavid Ahern 	err = 0;
30439fbb704cSDavid Ahern out:
30449fbb704cSDavid Ahern 	return err;
30459fbb704cSDavid Ahern }
30469fbb704cSDavid Ahern 
304783c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
304883c44251SDavid Ahern {
304983c44251SDavid Ahern 	if ((flags & RTF_REJECT) ||
305083c44251SDavid Ahern 	    (dev && (dev->flags & IFF_LOOPBACK) &&
305183c44251SDavid Ahern 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
305283c44251SDavid Ahern 	     !(flags & RTF_LOCAL)))
305383c44251SDavid Ahern 		return true;
305483c44251SDavid Ahern 
305583c44251SDavid Ahern 	return false;
305683c44251SDavid Ahern }
305783c44251SDavid Ahern 
305883c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
305983c44251SDavid Ahern 		 struct fib6_config *cfg, gfp_t gfp_flags,
306083c44251SDavid Ahern 		 struct netlink_ext_ack *extack)
306183c44251SDavid Ahern {
306283c44251SDavid Ahern 	struct net_device *dev = NULL;
306383c44251SDavid Ahern 	struct inet6_dev *idev = NULL;
306483c44251SDavid Ahern 	int addr_type;
306583c44251SDavid Ahern 	int err;
306683c44251SDavid Ahern 
3067f1741730SDavid Ahern 	fib6_nh->fib_nh_family = AF_INET6;
3068f1741730SDavid Ahern 
306983c44251SDavid Ahern 	err = -ENODEV;
307083c44251SDavid Ahern 	if (cfg->fc_ifindex) {
307183c44251SDavid Ahern 		dev = dev_get_by_index(net, cfg->fc_ifindex);
307283c44251SDavid Ahern 		if (!dev)
307383c44251SDavid Ahern 			goto out;
307483c44251SDavid Ahern 		idev = in6_dev_get(dev);
307583c44251SDavid Ahern 		if (!idev)
307683c44251SDavid Ahern 			goto out;
307783c44251SDavid Ahern 	}
307883c44251SDavid Ahern 
307983c44251SDavid Ahern 	if (cfg->fc_flags & RTNH_F_ONLINK) {
308083c44251SDavid Ahern 		if (!dev) {
308183c44251SDavid Ahern 			NL_SET_ERR_MSG(extack,
308283c44251SDavid Ahern 				       "Nexthop device required for onlink");
308383c44251SDavid Ahern 			goto out;
308483c44251SDavid Ahern 		}
308583c44251SDavid Ahern 
308683c44251SDavid Ahern 		if (!(dev->flags & IFF_UP)) {
308783c44251SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
308883c44251SDavid Ahern 			err = -ENETDOWN;
308983c44251SDavid Ahern 			goto out;
309083c44251SDavid Ahern 		}
309183c44251SDavid Ahern 
3092ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
309383c44251SDavid Ahern 	}
309483c44251SDavid Ahern 
3095ad1601aeSDavid Ahern 	fib6_nh->fib_nh_weight = 1;
309683c44251SDavid Ahern 
309783c44251SDavid Ahern 	/* We cannot add true routes via loopback here,
309883c44251SDavid Ahern 	 * they would result in kernel looping; promote them to reject routes
309983c44251SDavid Ahern 	 */
310083c44251SDavid Ahern 	addr_type = ipv6_addr_type(&cfg->fc_dst);
310183c44251SDavid Ahern 	if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
310283c44251SDavid Ahern 		/* hold loopback dev/idev if we haven't done so. */
310383c44251SDavid Ahern 		if (dev != net->loopback_dev) {
310483c44251SDavid Ahern 			if (dev) {
310583c44251SDavid Ahern 				dev_put(dev);
310683c44251SDavid Ahern 				in6_dev_put(idev);
310783c44251SDavid Ahern 			}
310883c44251SDavid Ahern 			dev = net->loopback_dev;
310983c44251SDavid Ahern 			dev_hold(dev);
311083c44251SDavid Ahern 			idev = in6_dev_get(dev);
311183c44251SDavid Ahern 			if (!idev) {
311283c44251SDavid Ahern 				err = -ENODEV;
311383c44251SDavid Ahern 				goto out;
311483c44251SDavid Ahern 			}
311583c44251SDavid Ahern 		}
3116*7dd73168SDavid Ahern 		goto pcpu_alloc;
311783c44251SDavid Ahern 	}
311883c44251SDavid Ahern 
311983c44251SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY) {
312083c44251SDavid Ahern 		err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
312183c44251SDavid Ahern 		if (err)
312283c44251SDavid Ahern 			goto out;
312383c44251SDavid Ahern 
3124ad1601aeSDavid Ahern 		fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3125bdf00467SDavid Ahern 		fib6_nh->fib_nh_gw_family = AF_INET6;
312683c44251SDavid Ahern 	}
312783c44251SDavid Ahern 
312883c44251SDavid Ahern 	err = -ENODEV;
312983c44251SDavid Ahern 	if (!dev)
313083c44251SDavid Ahern 		goto out;
313183c44251SDavid Ahern 
313283c44251SDavid Ahern 	if (idev->cnf.disable_ipv6) {
313383c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
313483c44251SDavid Ahern 		err = -EACCES;
313583c44251SDavid Ahern 		goto out;
313683c44251SDavid Ahern 	}
313783c44251SDavid Ahern 
313883c44251SDavid Ahern 	if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
313983c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
314083c44251SDavid Ahern 		err = -ENETDOWN;
314183c44251SDavid Ahern 		goto out;
314283c44251SDavid Ahern 	}
314383c44251SDavid Ahern 
314483c44251SDavid Ahern 	if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
314583c44251SDavid Ahern 	    !netif_carrier_ok(dev))
3146ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
314783c44251SDavid Ahern 
3148*7dd73168SDavid Ahern 	err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
3149*7dd73168SDavid Ahern 				 cfg->fc_encap_type, cfg, gfp_flags, extack);
3150*7dd73168SDavid Ahern 	if (err)
3151*7dd73168SDavid Ahern 		goto out;
3152*7dd73168SDavid Ahern 
3153*7dd73168SDavid Ahern pcpu_alloc:
3154f40b6ae2SDavid Ahern 	fib6_nh->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, gfp_flags);
3155f40b6ae2SDavid Ahern 	if (!fib6_nh->rt6i_pcpu) {
3156f40b6ae2SDavid Ahern 		err = -ENOMEM;
3157f40b6ae2SDavid Ahern 		goto out;
3158f40b6ae2SDavid Ahern 	}
3159f40b6ae2SDavid Ahern 
3160ad1601aeSDavid Ahern 	fib6_nh->fib_nh_dev = dev;
3161f1741730SDavid Ahern 	fib6_nh->fib_nh_oif = dev->ifindex;
316283c44251SDavid Ahern 	err = 0;
316383c44251SDavid Ahern out:
316483c44251SDavid Ahern 	if (idev)
316583c44251SDavid Ahern 		in6_dev_put(idev);
316683c44251SDavid Ahern 
316783c44251SDavid Ahern 	if (err) {
3168ad1601aeSDavid Ahern 		lwtstate_put(fib6_nh->fib_nh_lws);
3169ad1601aeSDavid Ahern 		fib6_nh->fib_nh_lws = NULL;
317083c44251SDavid Ahern 		if (dev)
317183c44251SDavid Ahern 			dev_put(dev);
317283c44251SDavid Ahern 	}
317383c44251SDavid Ahern 
317483c44251SDavid Ahern 	return err;
317583c44251SDavid Ahern }
317683c44251SDavid Ahern 
3177dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh)
3178dac7d0f2SDavid Ahern {
3179cc5c073aSDavid Ahern 	struct rt6_exception_bucket *bucket;
3180cc5c073aSDavid Ahern 
3181cc5c073aSDavid Ahern 	rcu_read_lock();
3182cc5c073aSDavid Ahern 
3183cc5c073aSDavid Ahern 	fib6_nh_flush_exceptions(fib6_nh, NULL);
3184cc5c073aSDavid Ahern 	bucket = fib6_nh_get_excptn_bucket(fib6_nh, NULL);
3185cc5c073aSDavid Ahern 	if (bucket) {
3186cc5c073aSDavid Ahern 		rcu_assign_pointer(fib6_nh->rt6i_exception_bucket, NULL);
3187cc5c073aSDavid Ahern 		kfree(bucket);
3188cc5c073aSDavid Ahern 	}
3189cc5c073aSDavid Ahern 
3190cc5c073aSDavid Ahern 	rcu_read_unlock();
3191cc5c073aSDavid Ahern 
3192f40b6ae2SDavid Ahern 	if (fib6_nh->rt6i_pcpu) {
3193f40b6ae2SDavid Ahern 		int cpu;
3194f40b6ae2SDavid Ahern 
3195f40b6ae2SDavid Ahern 		for_each_possible_cpu(cpu) {
3196f40b6ae2SDavid Ahern 			struct rt6_info **ppcpu_rt;
3197f40b6ae2SDavid Ahern 			struct rt6_info *pcpu_rt;
3198f40b6ae2SDavid Ahern 
3199f40b6ae2SDavid Ahern 			ppcpu_rt = per_cpu_ptr(fib6_nh->rt6i_pcpu, cpu);
3200f40b6ae2SDavid Ahern 			pcpu_rt = *ppcpu_rt;
3201f40b6ae2SDavid Ahern 			if (pcpu_rt) {
3202f40b6ae2SDavid Ahern 				dst_dev_put(&pcpu_rt->dst);
3203f40b6ae2SDavid Ahern 				dst_release(&pcpu_rt->dst);
3204f40b6ae2SDavid Ahern 				*ppcpu_rt = NULL;
3205f40b6ae2SDavid Ahern 			}
3206f40b6ae2SDavid Ahern 		}
3207f40b6ae2SDavid Ahern 
3208f40b6ae2SDavid Ahern 		free_percpu(fib6_nh->rt6i_pcpu);
3209f40b6ae2SDavid Ahern 	}
3210f40b6ae2SDavid Ahern 
3211979e276eSDavid Ahern 	fib_nh_common_release(&fib6_nh->nh_common);
3212dac7d0f2SDavid Ahern }
3213dac7d0f2SDavid Ahern 
32148d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3215acb54e3cSDavid Ahern 					      gfp_t gfp_flags,
3216333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
32171da177e4SLinus Torvalds {
32185578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
32198d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3220c71099acSThomas Graf 	struct fib6_table *table;
32218c5b83f0SRoopa Prabhu 	int err = -EINVAL;
322283c44251SDavid Ahern 	int addr_type;
32231da177e4SLinus Torvalds 
3224557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
3225d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
3226d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3227557c44beSDavid Ahern 		goto out;
3228d5d531cbSDavid Ahern 	}
3229557c44beSDavid Ahern 
32302ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
32312ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
32322ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
32332ea2352eSWei Wang 		goto out;
32342ea2352eSWei Wang 	}
32352ea2352eSWei Wang 
3236e8478e80SDavid Ahern 	if (cfg->fc_type > RTN_MAX) {
3237e8478e80SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid route type");
3238e8478e80SDavid Ahern 		goto out;
3239e8478e80SDavid Ahern 	}
3240e8478e80SDavid Ahern 
3241d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
3242d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
32438c5b83f0SRoopa Prabhu 		goto out;
3244d5d531cbSDavid Ahern 	}
3245d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
3246d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
3247d5d531cbSDavid Ahern 		goto out;
3248d5d531cbSDavid Ahern 	}
32491da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
3250d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
3251d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
3252d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
32538c5b83f0SRoopa Prabhu 		goto out;
3254d5d531cbSDavid Ahern 	}
32551da177e4SLinus Torvalds #endif
3256fc1e64e1SDavid Ahern 
3257c71099acSThomas Graf 	err = -ENOBUFS;
325838308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
3259d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3260d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
326138308473SDavid S. Miller 		if (!table) {
3262f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3263d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
3264d71314b4SMatti Vaittinen 		}
3265d71314b4SMatti Vaittinen 	} else {
3266d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
3267d71314b4SMatti Vaittinen 	}
326838308473SDavid S. Miller 
326938308473SDavid S. Miller 	if (!table)
3270c71099acSThomas Graf 		goto out;
3271c71099acSThomas Graf 
32721da177e4SLinus Torvalds 	err = -ENOMEM;
32731cf844c7SDavid Ahern 	rt = fib6_info_alloc(gfp_flags, true);
327493531c67SDavid Ahern 	if (!rt)
32751da177e4SLinus Torvalds 		goto out;
327693531c67SDavid Ahern 
3277d7e774f3SDavid Ahern 	rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3278d7e774f3SDavid Ahern 					       extack);
3279767a2217SDavid Ahern 	if (IS_ERR(rt->fib6_metrics)) {
3280767a2217SDavid Ahern 		err = PTR_ERR(rt->fib6_metrics);
3281fda21d46SEric Dumazet 		/* Do not leave garbage there. */
3282fda21d46SEric Dumazet 		rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3283767a2217SDavid Ahern 		goto out;
3284767a2217SDavid Ahern 	}
3285767a2217SDavid Ahern 
328693531c67SDavid Ahern 	if (cfg->fc_flags & RTF_ADDRCONF)
328793531c67SDavid Ahern 		rt->dst_nocount = true;
32881da177e4SLinus Torvalds 
32891716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
329014895687SDavid Ahern 		fib6_set_expires(rt, jiffies +
32911716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
32921716a961SGao feng 	else
329314895687SDavid Ahern 		fib6_clean_expires(rt);
32941da177e4SLinus Torvalds 
329586872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
329686872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
329793c2fb25SDavid Ahern 	rt->fib6_protocol = cfg->fc_protocol;
329886872cb5SThomas Graf 
329983c44251SDavid Ahern 	rt->fib6_table = table;
330083c44251SDavid Ahern 	rt->fib6_metric = cfg->fc_metric;
330183c44251SDavid Ahern 	rt->fib6_type = cfg->fc_type;
33022b2450caSDavid Ahern 	rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
330319e42e45SRoopa Prabhu 
330493c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
330593c2fb25SDavid Ahern 	rt->fib6_dst.plen = cfg->fc_dst_len;
330693c2fb25SDavid Ahern 	if (rt->fib6_dst.plen == 128)
33073b6761d1SDavid Ahern 		rt->dst_host = true;
33081da177e4SLinus Torvalds 
33091da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
331093c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
331193c2fb25SDavid Ahern 	rt->fib6_src.plen = cfg->fc_src_len;
33121da177e4SLinus Torvalds #endif
33131cf844c7SDavid Ahern 	err = fib6_nh_init(net, rt->fib6_nh, cfg, gfp_flags, extack);
33141da177e4SLinus Torvalds 	if (err)
33151da177e4SLinus Torvalds 		goto out;
33169fbb704cSDavid Ahern 
331783c44251SDavid Ahern 	/* We cannot add true routes via loopback here,
331883c44251SDavid Ahern 	 * they would result in kernel looping; promote them to reject routes
331983c44251SDavid Ahern 	 */
332083c44251SDavid Ahern 	addr_type = ipv6_addr_type(&cfg->fc_dst);
33211cf844c7SDavid Ahern 	if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh->fib_nh_dev, addr_type))
332283c44251SDavid Ahern 		rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3323955ec4cbSDavid Ahern 
3324c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
332583c44251SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
332683c44251SDavid Ahern 
3327c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3328d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
3329c3968a85SDaniel Walter 			err = -EINVAL;
3330c3968a85SDaniel Walter 			goto out;
3331c3968a85SDaniel Walter 		}
333293c2fb25SDavid Ahern 		rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
333393c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 128;
3334c3968a85SDaniel Walter 	} else
333593c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
3336c3968a85SDaniel Walter 
33378c5b83f0SRoopa Prabhu 	return rt;
33381da177e4SLinus Torvalds out:
333993531c67SDavid Ahern 	fib6_info_release(rt);
33408c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
33416b9ea5a6SRoopa Prabhu }
33426b9ea5a6SRoopa Prabhu 
3343acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3344333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
33456b9ea5a6SRoopa Prabhu {
33468d1c802bSDavid Ahern 	struct fib6_info *rt;
33476b9ea5a6SRoopa Prabhu 	int err;
33486b9ea5a6SRoopa Prabhu 
3349acb54e3cSDavid Ahern 	rt = ip6_route_info_create(cfg, gfp_flags, extack);
3350d4ead6b3SDavid Ahern 	if (IS_ERR(rt))
3351d4ead6b3SDavid Ahern 		return PTR_ERR(rt);
33526b9ea5a6SRoopa Prabhu 
3353d4ead6b3SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
335493531c67SDavid Ahern 	fib6_info_release(rt);
33556b9ea5a6SRoopa Prabhu 
33561da177e4SLinus Torvalds 	return err;
33571da177e4SLinus Torvalds }
33581da177e4SLinus Torvalds 
33598d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
33601da177e4SLinus Torvalds {
3361afb1d4b5SDavid Ahern 	struct net *net = info->nl_net;
3362c71099acSThomas Graf 	struct fib6_table *table;
3363afb1d4b5SDavid Ahern 	int err;
33641da177e4SLinus Torvalds 
3365421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
33666825a26cSGao feng 		err = -ENOENT;
33676825a26cSGao feng 		goto out;
33686825a26cSGao feng 	}
33696c813a72SPatrick McHardy 
337093c2fb25SDavid Ahern 	table = rt->fib6_table;
337166f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
337286872cb5SThomas Graf 	err = fib6_del(rt, info);
337366f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
33741da177e4SLinus Torvalds 
33756825a26cSGao feng out:
337693531c67SDavid Ahern 	fib6_info_release(rt);
33771da177e4SLinus Torvalds 	return err;
33781da177e4SLinus Torvalds }
33791da177e4SLinus Torvalds 
33808d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt)
3381e0a1ad73SThomas Graf {
3382afb1d4b5SDavid Ahern 	struct nl_info info = { .nl_net = net };
3383afb1d4b5SDavid Ahern 
3384528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
3385e0a1ad73SThomas Graf }
3386e0a1ad73SThomas Graf 
33878d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
33880ae81335SDavid Ahern {
33890ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
3390e3330039SWANG Cong 	struct net *net = info->nl_net;
339116a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
33920ae81335SDavid Ahern 	struct fib6_table *table;
3393e3330039SWANG Cong 	int err = -ENOENT;
33940ae81335SDavid Ahern 
3395421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
3396e3330039SWANG Cong 		goto out_put;
339793c2fb25SDavid Ahern 	table = rt->fib6_table;
339866f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
33990ae81335SDavid Ahern 
340093c2fb25SDavid Ahern 	if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
34018d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
34020ae81335SDavid Ahern 
340316a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
340416a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
340516a16cd3SDavid Ahern 		if (skb) {
340616a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
340716a16cd3SDavid Ahern 
3408d4ead6b3SDavid Ahern 			if (rt6_fill_node(net, skb, rt, NULL,
340916a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
341016a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
341116a16cd3SDavid Ahern 				kfree_skb(skb);
341216a16cd3SDavid Ahern 				skb = NULL;
341316a16cd3SDavid Ahern 			} else
341416a16cd3SDavid Ahern 				info->skip_notify = 1;
341516a16cd3SDavid Ahern 		}
341616a16cd3SDavid Ahern 
34170ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
341893c2fb25SDavid Ahern 					 &rt->fib6_siblings,
341993c2fb25SDavid Ahern 					 fib6_siblings) {
34200ae81335SDavid Ahern 			err = fib6_del(sibling, info);
34210ae81335SDavid Ahern 			if (err)
3422e3330039SWANG Cong 				goto out_unlock;
34230ae81335SDavid Ahern 		}
34240ae81335SDavid Ahern 	}
34250ae81335SDavid Ahern 
34260ae81335SDavid Ahern 	err = fib6_del(rt, info);
3427e3330039SWANG Cong out_unlock:
342866f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
3429e3330039SWANG Cong out_put:
343093531c67SDavid Ahern 	fib6_info_release(rt);
343116a16cd3SDavid Ahern 
343216a16cd3SDavid Ahern 	if (skb) {
3433e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
343416a16cd3SDavid Ahern 			    info->nlh, gfp_any());
343516a16cd3SDavid Ahern 	}
34360ae81335SDavid Ahern 	return err;
34370ae81335SDavid Ahern }
34380ae81335SDavid Ahern 
34390fa6efc5SDavid Ahern static int __ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
344023fb93a4SDavid Ahern {
344123fb93a4SDavid Ahern 	int rc = -ESRCH;
344223fb93a4SDavid Ahern 
344323fb93a4SDavid Ahern 	if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
344423fb93a4SDavid Ahern 		goto out;
344523fb93a4SDavid Ahern 
344623fb93a4SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY &&
344723fb93a4SDavid Ahern 	    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
344823fb93a4SDavid Ahern 		goto out;
3449761f6026SXin Long 
345023fb93a4SDavid Ahern 	rc = rt6_remove_exception_rt(rt);
345123fb93a4SDavid Ahern out:
345223fb93a4SDavid Ahern 	return rc;
345323fb93a4SDavid Ahern }
345423fb93a4SDavid Ahern 
34550fa6efc5SDavid Ahern static int ip6_del_cached_rt(struct fib6_config *cfg, struct fib6_info *rt,
34560fa6efc5SDavid Ahern 			     struct fib6_nh *nh)
34570fa6efc5SDavid Ahern {
34580fa6efc5SDavid Ahern 	struct fib6_result res = {
34590fa6efc5SDavid Ahern 		.f6i = rt,
34600fa6efc5SDavid Ahern 		.nh = nh,
34610fa6efc5SDavid Ahern 	};
34620fa6efc5SDavid Ahern 	struct rt6_info *rt_cache;
34630fa6efc5SDavid Ahern 
34640fa6efc5SDavid Ahern 	rt_cache = rt6_find_cached_rt(&res, &cfg->fc_dst, &cfg->fc_src);
34650fa6efc5SDavid Ahern 	if (rt_cache)
34660fa6efc5SDavid Ahern 		return __ip6_del_cached_rt(rt_cache, cfg);
34670fa6efc5SDavid Ahern 
34680fa6efc5SDavid Ahern 	return 0;
34690fa6efc5SDavid Ahern }
34700fa6efc5SDavid Ahern 
3471333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
3472333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
34731da177e4SLinus Torvalds {
3474c71099acSThomas Graf 	struct fib6_table *table;
34758d1c802bSDavid Ahern 	struct fib6_info *rt;
34761da177e4SLinus Torvalds 	struct fib6_node *fn;
34771da177e4SLinus Torvalds 	int err = -ESRCH;
34781da177e4SLinus Torvalds 
34795578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3480d5d531cbSDavid Ahern 	if (!table) {
3481d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
3482c71099acSThomas Graf 		return err;
3483d5d531cbSDavid Ahern 	}
34841da177e4SLinus Torvalds 
348566f5d6ceSWei Wang 	rcu_read_lock();
3486c71099acSThomas Graf 
3487c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
348886872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
348938fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
34902b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
34911da177e4SLinus Torvalds 
34921da177e4SLinus Torvalds 	if (fn) {
349366f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
3494ad1601aeSDavid Ahern 			struct fib6_nh *nh;
3495ad1601aeSDavid Ahern 
34961cf844c7SDavid Ahern 			nh = rt->fib6_nh;
34972b760fcfSWei Wang 			if (cfg->fc_flags & RTF_CACHE) {
349823fb93a4SDavid Ahern 				int rc;
349923fb93a4SDavid Ahern 
35000fa6efc5SDavid Ahern 				rc = ip6_del_cached_rt(cfg, rt, nh);
35019e575010SEric Dumazet 				if (rc != -ESRCH) {
35029e575010SEric Dumazet 					rcu_read_unlock();
350323fb93a4SDavid Ahern 					return rc;
350423fb93a4SDavid Ahern 				}
35051f56a01fSMartin KaFai Lau 				continue;
35062b760fcfSWei Wang 			}
3507ad1601aeSDavid Ahern 
350886872cb5SThomas Graf 			if (cfg->fc_ifindex &&
3509ad1601aeSDavid Ahern 			    (!nh->fib_nh_dev ||
3510ad1601aeSDavid Ahern 			     nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
35111da177e4SLinus Torvalds 				continue;
351286872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
3513ad1601aeSDavid Ahern 			    !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
35141da177e4SLinus Torvalds 				continue;
351593c2fb25SDavid Ahern 			if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
35161da177e4SLinus Torvalds 				continue;
351793c2fb25SDavid Ahern 			if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
3518c2ed1880SMantas M 				continue;
3519e873e4b9SWei Wang 			if (!fib6_info_hold_safe(rt))
3520e873e4b9SWei Wang 				continue;
352166f5d6ceSWei Wang 			rcu_read_unlock();
35221da177e4SLinus Torvalds 
35230ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
35240ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
352586872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
35260ae81335SDavid Ahern 
35270ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
35281da177e4SLinus Torvalds 		}
35291da177e4SLinus Torvalds 	}
353066f5d6ceSWei Wang 	rcu_read_unlock();
35311da177e4SLinus Torvalds 
35321da177e4SLinus Torvalds 	return err;
35331da177e4SLinus Torvalds }
35341da177e4SLinus Torvalds 
35356700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3536a6279458SYOSHIFUJI Hideaki {
3537a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
3538e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
353985bd05deSDavid Ahern 	struct fib6_result res = {};
3540e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
3541e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
3542e8599ff4SDavid S. Miller 	struct neighbour *neigh;
354371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
35446e157b6aSDavid S. Miller 	int optlen, on_link;
35456e157b6aSDavid S. Miller 	u8 *lladdr;
3546e8599ff4SDavid S. Miller 
354729a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
354871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
3549e8599ff4SDavid S. Miller 
3550e8599ff4SDavid S. Miller 	if (optlen < 0) {
35516e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3552e8599ff4SDavid S. Miller 		return;
3553e8599ff4SDavid S. Miller 	}
3554e8599ff4SDavid S. Miller 
355571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
3556e8599ff4SDavid S. Miller 
355771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
35586e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3559e8599ff4SDavid S. Miller 		return;
3560e8599ff4SDavid S. Miller 	}
3561e8599ff4SDavid S. Miller 
35626e157b6aSDavid S. Miller 	on_link = 0;
356371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
3564e8599ff4SDavid S. Miller 		on_link = 1;
356571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
3566e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
35676e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
3568e8599ff4SDavid S. Miller 		return;
3569e8599ff4SDavid S. Miller 	}
3570e8599ff4SDavid S. Miller 
3571e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
3572e8599ff4SDavid S. Miller 	if (!in6_dev)
3573e8599ff4SDavid S. Miller 		return;
3574e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3575e8599ff4SDavid S. Miller 		return;
3576e8599ff4SDavid S. Miller 
3577e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
3578e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
3579e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
3580e8599ff4SDavid S. Miller 	 */
3581e8599ff4SDavid S. Miller 
3582f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
3583e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3584e8599ff4SDavid S. Miller 		return;
3585e8599ff4SDavid S. Miller 	}
35866e157b6aSDavid S. Miller 
35876e157b6aSDavid S. Miller 	lladdr = NULL;
3588e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
3589e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3590e8599ff4SDavid S. Miller 					     skb->dev);
3591e8599ff4SDavid S. Miller 		if (!lladdr) {
3592e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3593e8599ff4SDavid S. Miller 			return;
3594e8599ff4SDavid S. Miller 		}
3595e8599ff4SDavid S. Miller 	}
3596e8599ff4SDavid S. Miller 
35976e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
3598ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
35996e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
36006e157b6aSDavid S. Miller 		return;
36016e157b6aSDavid S. Miller 	}
36026e157b6aSDavid S. Miller 
36036e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
36046e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
36056e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
36066e157b6aSDavid S. Miller 	 */
36070dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
36086e157b6aSDavid S. Miller 
360971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
3610e8599ff4SDavid S. Miller 	if (!neigh)
3611e8599ff4SDavid S. Miller 		return;
3612e8599ff4SDavid S. Miller 
36131da177e4SLinus Torvalds 	/*
36141da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
36151da177e4SLinus Torvalds 	 */
36161da177e4SLinus Torvalds 
3617f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
36181da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
36191da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
36201da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
3621f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
3622f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
36231da177e4SLinus Torvalds 
36244d85cd0cSDavid Ahern 	rcu_read_lock();
362585bd05deSDavid Ahern 	res.f6i = rcu_dereference(rt->from);
3626ff24e498SDavid S. Miller 	if (!res.f6i)
3627886b7a50SMartin KaFai Lau 		goto out;
36288a14e46fSDavid Ahern 
36291cf844c7SDavid Ahern 	res.nh = res.f6i->fib6_nh;
36307d21fec9SDavid Ahern 	res.fib6_flags = res.f6i->fib6_flags;
36317d21fec9SDavid Ahern 	res.fib6_type = res.f6i->fib6_type;
363285bd05deSDavid Ahern 	nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
363338308473SDavid S. Miller 	if (!nrt)
36341da177e4SLinus Torvalds 		goto out;
36351da177e4SLinus Torvalds 
36361da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
36371da177e4SLinus Torvalds 	if (on_link)
36381da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
36391da177e4SLinus Torvalds 
36404e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
36411da177e4SLinus Torvalds 
3642886b7a50SMartin KaFai Lau 	/* rt6_insert_exception() will take care of duplicated exceptions */
36435012f0a5SDavid Ahern 	if (rt6_insert_exception(nrt, &res)) {
36442b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
36452b760fcfSWei Wang 		goto out;
36462b760fcfSWei Wang 	}
36471da177e4SLinus Torvalds 
3648d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
3649d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
365071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
365160592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
36528d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
36538d71740cSTom Tucker 
36541da177e4SLinus Torvalds out:
3655886b7a50SMartin KaFai Lau 	rcu_read_unlock();
3656e8599ff4SDavid S. Miller 	neigh_release(neigh);
36576e157b6aSDavid S. Miller }
36586e157b6aSDavid S. Miller 
365970ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
36608d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
3661b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3662830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3663830218c1SDavid Ahern 					   struct net_device *dev)
366470ceb4f5SYOSHIFUJI Hideaki {
3665830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3666830218c1SDavid Ahern 	int ifindex = dev->ifindex;
366770ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
36688d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3669c71099acSThomas Graf 	struct fib6_table *table;
367070ceb4f5SYOSHIFUJI Hideaki 
3671830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
367238308473SDavid S. Miller 	if (!table)
3673c71099acSThomas Graf 		return NULL;
3674c71099acSThomas Graf 
367566f5d6ceSWei Wang 	rcu_read_lock();
367638fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
367770ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
367870ceb4f5SYOSHIFUJI Hideaki 		goto out;
367970ceb4f5SYOSHIFUJI Hideaki 
368066f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
36811cf844c7SDavid Ahern 		if (rt->fib6_nh->fib_nh_dev->ifindex != ifindex)
368270ceb4f5SYOSHIFUJI Hideaki 			continue;
36832b2450caSDavid Ahern 		if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
36841cf844c7SDavid Ahern 		    !rt->fib6_nh->fib_nh_gw_family)
368570ceb4f5SYOSHIFUJI Hideaki 			continue;
36861cf844c7SDavid Ahern 		if (!ipv6_addr_equal(&rt->fib6_nh->fib_nh_gw6, gwaddr))
368770ceb4f5SYOSHIFUJI Hideaki 			continue;
3688e873e4b9SWei Wang 		if (!fib6_info_hold_safe(rt))
3689e873e4b9SWei Wang 			continue;
369070ceb4f5SYOSHIFUJI Hideaki 		break;
369170ceb4f5SYOSHIFUJI Hideaki 	}
369270ceb4f5SYOSHIFUJI Hideaki out:
369366f5d6ceSWei Wang 	rcu_read_unlock();
369470ceb4f5SYOSHIFUJI Hideaki 	return rt;
369570ceb4f5SYOSHIFUJI Hideaki }
369670ceb4f5SYOSHIFUJI Hideaki 
36978d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
3698b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3699830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3700830218c1SDavid Ahern 					   struct net_device *dev,
370195c96174SEric Dumazet 					   unsigned int pref)
370270ceb4f5SYOSHIFUJI Hideaki {
370386872cb5SThomas Graf 	struct fib6_config cfg = {
3704238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
3705830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
370686872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
370786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
370886872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
3709b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3710e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
371115e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
3712efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3713efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
371486872cb5SThomas Graf 	};
371570ceb4f5SYOSHIFUJI Hideaki 
3716830218c1SDavid Ahern 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
37174e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
37184e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
371986872cb5SThomas Graf 
3720e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
3721e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
372286872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
372370ceb4f5SYOSHIFUJI Hideaki 
3724acb54e3cSDavid Ahern 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
372570ceb4f5SYOSHIFUJI Hideaki 
3726830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
372770ceb4f5SYOSHIFUJI Hideaki }
372870ceb4f5SYOSHIFUJI Hideaki #endif
372970ceb4f5SYOSHIFUJI Hideaki 
37308d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net,
3731afb1d4b5SDavid Ahern 				     const struct in6_addr *addr,
3732afb1d4b5SDavid Ahern 				     struct net_device *dev)
37331da177e4SLinus Torvalds {
3734830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
37358d1c802bSDavid Ahern 	struct fib6_info *rt;
3736c71099acSThomas Graf 	struct fib6_table *table;
37371da177e4SLinus Torvalds 
3738afb1d4b5SDavid Ahern 	table = fib6_get_table(net, tb_id);
373938308473SDavid S. Miller 	if (!table)
3740c71099acSThomas Graf 		return NULL;
37411da177e4SLinus Torvalds 
374266f5d6ceSWei Wang 	rcu_read_lock();
374366f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
37441cf844c7SDavid Ahern 		struct fib6_nh *nh = rt->fib6_nh;
3745ad1601aeSDavid Ahern 
3746ad1601aeSDavid Ahern 		if (dev == nh->fib_nh_dev &&
374793c2fb25SDavid Ahern 		    ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
3748ad1601aeSDavid Ahern 		    ipv6_addr_equal(&nh->fib_nh_gw6, addr))
37491da177e4SLinus Torvalds 			break;
37501da177e4SLinus Torvalds 	}
3751e873e4b9SWei Wang 	if (rt && !fib6_info_hold_safe(rt))
3752e873e4b9SWei Wang 		rt = NULL;
375366f5d6ceSWei Wang 	rcu_read_unlock();
37541da177e4SLinus Torvalds 	return rt;
37551da177e4SLinus Torvalds }
37561da177e4SLinus Torvalds 
37578d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net,
3758afb1d4b5SDavid Ahern 				     const struct in6_addr *gwaddr,
3759ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
3760ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
37611da177e4SLinus Torvalds {
376286872cb5SThomas Graf 	struct fib6_config cfg = {
3763ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
3764238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
376586872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
376686872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
376786872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
3768b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3769e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
377015e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
37715578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3772afb1d4b5SDavid Ahern 		.fc_nlinfo.nl_net = net,
377386872cb5SThomas Graf 	};
37741da177e4SLinus Torvalds 
37754e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
37761da177e4SLinus Torvalds 
3777acb54e3cSDavid Ahern 	if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
3778830218c1SDavid Ahern 		struct fib6_table *table;
3779830218c1SDavid Ahern 
3780830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
3781830218c1SDavid Ahern 		if (table)
3782830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3783830218c1SDavid Ahern 	}
37841da177e4SLinus Torvalds 
3785afb1d4b5SDavid Ahern 	return rt6_get_dflt_router(net, gwaddr, dev);
37861da177e4SLinus Torvalds }
37871da177e4SLinus Torvalds 
3788afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net,
3789afb1d4b5SDavid Ahern 				     struct fib6_table *table)
37901da177e4SLinus Torvalds {
37918d1c802bSDavid Ahern 	struct fib6_info *rt;
37921da177e4SLinus Torvalds 
37931da177e4SLinus Torvalds restart:
379466f5d6ceSWei Wang 	rcu_read_lock();
379566f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
3796dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
3797dcd1f572SDavid Ahern 		struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3798dcd1f572SDavid Ahern 
379993c2fb25SDavid Ahern 		if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
3800e873e4b9SWei Wang 		    (!idev || idev->cnf.accept_ra != 2) &&
3801e873e4b9SWei Wang 		    fib6_info_hold_safe(rt)) {
380266f5d6ceSWei Wang 			rcu_read_unlock();
3803afb1d4b5SDavid Ahern 			ip6_del_rt(net, rt);
38041da177e4SLinus Torvalds 			goto restart;
38051da177e4SLinus Torvalds 		}
38061da177e4SLinus Torvalds 	}
380766f5d6ceSWei Wang 	rcu_read_unlock();
3808830218c1SDavid Ahern 
3809830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3810830218c1SDavid Ahern }
3811830218c1SDavid Ahern 
3812830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
3813830218c1SDavid Ahern {
3814830218c1SDavid Ahern 	struct fib6_table *table;
3815830218c1SDavid Ahern 	struct hlist_head *head;
3816830218c1SDavid Ahern 	unsigned int h;
3817830218c1SDavid Ahern 
3818830218c1SDavid Ahern 	rcu_read_lock();
3819830218c1SDavid Ahern 
3820830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3821830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
3822830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3823830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3824afb1d4b5SDavid Ahern 				__rt6_purge_dflt_routers(net, table);
3825830218c1SDavid Ahern 		}
3826830218c1SDavid Ahern 	}
3827830218c1SDavid Ahern 
3828830218c1SDavid Ahern 	rcu_read_unlock();
38291da177e4SLinus Torvalds }
38301da177e4SLinus Torvalds 
38315578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
38325578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
383386872cb5SThomas Graf 				 struct fib6_config *cfg)
383486872cb5SThomas Graf {
38358823a3acSMaciej Żenczykowski 	*cfg = (struct fib6_config){
38368823a3acSMaciej Żenczykowski 		.fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
38378823a3acSMaciej Żenczykowski 			 : RT6_TABLE_MAIN,
38388823a3acSMaciej Żenczykowski 		.fc_ifindex = rtmsg->rtmsg_ifindex,
383967f69513SDavid Ahern 		.fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
38408823a3acSMaciej Żenczykowski 		.fc_expires = rtmsg->rtmsg_info,
38418823a3acSMaciej Żenczykowski 		.fc_dst_len = rtmsg->rtmsg_dst_len,
38428823a3acSMaciej Żenczykowski 		.fc_src_len = rtmsg->rtmsg_src_len,
38438823a3acSMaciej Żenczykowski 		.fc_flags = rtmsg->rtmsg_flags,
38448823a3acSMaciej Żenczykowski 		.fc_type = rtmsg->rtmsg_type,
384586872cb5SThomas Graf 
38468823a3acSMaciej Żenczykowski 		.fc_nlinfo.nl_net = net,
384786872cb5SThomas Graf 
38488823a3acSMaciej Żenczykowski 		.fc_dst = rtmsg->rtmsg_dst,
38498823a3acSMaciej Żenczykowski 		.fc_src = rtmsg->rtmsg_src,
38508823a3acSMaciej Żenczykowski 		.fc_gateway = rtmsg->rtmsg_gateway,
38518823a3acSMaciej Żenczykowski 	};
385286872cb5SThomas Graf }
385386872cb5SThomas Graf 
38545578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
38551da177e4SLinus Torvalds {
385686872cb5SThomas Graf 	struct fib6_config cfg;
38571da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
38581da177e4SLinus Torvalds 	int err;
38591da177e4SLinus Torvalds 
38601da177e4SLinus Torvalds 	switch (cmd) {
38611da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
38621da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
3863af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
38641da177e4SLinus Torvalds 			return -EPERM;
38651da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
38661da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
38671da177e4SLinus Torvalds 		if (err)
38681da177e4SLinus Torvalds 			return -EFAULT;
38691da177e4SLinus Torvalds 
38705578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
387186872cb5SThomas Graf 
38721da177e4SLinus Torvalds 		rtnl_lock();
38731da177e4SLinus Torvalds 		switch (cmd) {
38741da177e4SLinus Torvalds 		case SIOCADDRT:
3875acb54e3cSDavid Ahern 			err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
38761da177e4SLinus Torvalds 			break;
38771da177e4SLinus Torvalds 		case SIOCDELRT:
3878333c4301SDavid Ahern 			err = ip6_route_del(&cfg, NULL);
38791da177e4SLinus Torvalds 			break;
38801da177e4SLinus Torvalds 		default:
38811da177e4SLinus Torvalds 			err = -EINVAL;
38821da177e4SLinus Torvalds 		}
38831da177e4SLinus Torvalds 		rtnl_unlock();
38841da177e4SLinus Torvalds 
38851da177e4SLinus Torvalds 		return err;
38863ff50b79SStephen Hemminger 	}
38871da177e4SLinus Torvalds 
38881da177e4SLinus Torvalds 	return -EINVAL;
38891da177e4SLinus Torvalds }
38901da177e4SLinus Torvalds 
38911da177e4SLinus Torvalds /*
38921da177e4SLinus Torvalds  *	Drop the packet on the floor
38931da177e4SLinus Torvalds  */
38941da177e4SLinus Torvalds 
3895d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
38961da177e4SLinus Torvalds {
3897adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
38981d3fd8a1SStephen Suryaputra 	struct net *net = dev_net(dst->dev);
38991d3fd8a1SStephen Suryaputra 	struct inet6_dev *idev;
39001d3fd8a1SStephen Suryaputra 	int type;
39011d3fd8a1SStephen Suryaputra 
39021d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev) &&
39031d3fd8a1SStephen Suryaputra 	    dst->dev == net->loopback_dev)
39041d3fd8a1SStephen Suryaputra 		idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
39051d3fd8a1SStephen Suryaputra 	else
39061d3fd8a1SStephen Suryaputra 		idev = ip6_dst_idev(dst);
39071d3fd8a1SStephen Suryaputra 
3908612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
3909612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
39100660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
391145bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
39121d3fd8a1SStephen Suryaputra 			IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
3913612f09e8SYOSHIFUJI Hideaki 			break;
3914612f09e8SYOSHIFUJI Hideaki 		}
3915612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
3916612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
39171d3fd8a1SStephen Suryaputra 		IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
3918612f09e8SYOSHIFUJI Hideaki 		break;
3919612f09e8SYOSHIFUJI Hideaki 	}
39201d3fd8a1SStephen Suryaputra 
39211d3fd8a1SStephen Suryaputra 	/* Start over by dropping the dst for l3mdev case */
39221d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev))
39231d3fd8a1SStephen Suryaputra 		skb_dst_drop(skb);
39241d3fd8a1SStephen Suryaputra 
39253ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
39261da177e4SLinus Torvalds 	kfree_skb(skb);
39271da177e4SLinus Torvalds 	return 0;
39281da177e4SLinus Torvalds }
39291da177e4SLinus Torvalds 
39309ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
39319ce8ade0SThomas Graf {
3932612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
39339ce8ade0SThomas Graf }
39349ce8ade0SThomas Graf 
3935ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
39361da177e4SLinus Torvalds {
3937adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3938612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
39391da177e4SLinus Torvalds }
39401da177e4SLinus Torvalds 
39419ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
39429ce8ade0SThomas Graf {
3943612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
39449ce8ade0SThomas Graf }
39459ce8ade0SThomas Graf 
3946ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
39479ce8ade0SThomas Graf {
3948adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3949612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
39509ce8ade0SThomas Graf }
39519ce8ade0SThomas Graf 
39521da177e4SLinus Torvalds /*
39531da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
39541da177e4SLinus Torvalds  */
39551da177e4SLinus Torvalds 
3956360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net,
3957afb1d4b5SDavid Ahern 				     struct inet6_dev *idev,
39581da177e4SLinus Torvalds 				     const struct in6_addr *addr,
3959acb54e3cSDavid Ahern 				     bool anycast, gfp_t gfp_flags)
39601da177e4SLinus Torvalds {
3961c7a1ce39SDavid Ahern 	struct fib6_config cfg = {
3962c7a1ce39SDavid Ahern 		.fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
3963c7a1ce39SDavid Ahern 		.fc_ifindex = idev->dev->ifindex,
3964c7a1ce39SDavid Ahern 		.fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP,
3965c7a1ce39SDavid Ahern 		.fc_dst = *addr,
3966c7a1ce39SDavid Ahern 		.fc_dst_len = 128,
3967c7a1ce39SDavid Ahern 		.fc_protocol = RTPROT_KERNEL,
3968c7a1ce39SDavid Ahern 		.fc_nlinfo.nl_net = net,
3969c7a1ce39SDavid Ahern 		.fc_ignore_dev_down = true,
3970c7a1ce39SDavid Ahern 	};
39715f02ce24SDavid Ahern 
3972e8478e80SDavid Ahern 	if (anycast) {
3973c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_ANYCAST;
3974c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_ANYCAST;
3975e8478e80SDavid Ahern 	} else {
3976c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_LOCAL;
3977c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_LOCAL;
3978e8478e80SDavid Ahern 	}
39791da177e4SLinus Torvalds 
3980c7a1ce39SDavid Ahern 	return ip6_route_info_create(&cfg, gfp_flags, NULL);
39811da177e4SLinus Torvalds }
39821da177e4SLinus Torvalds 
3983c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
3984c3968a85SDaniel Walter struct arg_dev_net_ip {
3985c3968a85SDaniel Walter 	struct net_device *dev;
3986c3968a85SDaniel Walter 	struct net *net;
3987c3968a85SDaniel Walter 	struct in6_addr *addr;
3988c3968a85SDaniel Walter };
3989c3968a85SDaniel Walter 
39908d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
3991c3968a85SDaniel Walter {
3992c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3993c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3994c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3995c3968a85SDaniel Walter 
39961cf844c7SDavid Ahern 	if (((void *)rt->fib6_nh->fib_nh_dev == dev || !dev) &&
3997421842edSDavid Ahern 	    rt != net->ipv6.fib6_null_entry &&
399893c2fb25SDavid Ahern 	    ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
399960006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
4000c3968a85SDaniel Walter 		/* remove prefsrc entry */
400193c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
400260006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
4003c3968a85SDaniel Walter 	}
4004c3968a85SDaniel Walter 	return 0;
4005c3968a85SDaniel Walter }
4006c3968a85SDaniel Walter 
4007c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
4008c3968a85SDaniel Walter {
4009c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
4010c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
4011c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
4012c3968a85SDaniel Walter 		.net = net,
4013c3968a85SDaniel Walter 		.addr = &ifp->addr,
4014c3968a85SDaniel Walter 	};
40150c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
4016c3968a85SDaniel Walter }
4017c3968a85SDaniel Walter 
40182b2450caSDavid Ahern #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT)
4019be7a010dSDuan Jiong 
4020be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
40218d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
4022be7a010dSDuan Jiong {
4023be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
40241cf844c7SDavid Ahern 	struct fib6_nh *nh = rt->fib6_nh;
4025be7a010dSDuan Jiong 
402693c2fb25SDavid Ahern 	if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
4027cc5c073aSDavid Ahern 	    nh->fib_nh_gw_family && ipv6_addr_equal(gateway, &nh->fib_nh_gw6))
4028be7a010dSDuan Jiong 		return -1;
4029b16cb459SWei Wang 
4030b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
4031b16cb459SWei Wang 	 * This is needed because cached route may have a different
4032b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
4033b16cb459SWei Wang 	 */
4034cc5c073aSDavid Ahern 	fib6_nh_exceptions_clean_tohost(nh, gateway);
4035b16cb459SWei Wang 
4036be7a010dSDuan Jiong 	return 0;
4037be7a010dSDuan Jiong }
4038be7a010dSDuan Jiong 
4039be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
4040be7a010dSDuan Jiong {
4041be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
4042be7a010dSDuan Jiong }
4043be7a010dSDuan Jiong 
40442127d95aSIdo Schimmel struct arg_netdev_event {
40452127d95aSIdo Schimmel 	const struct net_device *dev;
40464c981e28SIdo Schimmel 	union {
4047ecc5663cSDavid Ahern 		unsigned char nh_flags;
40484c981e28SIdo Schimmel 		unsigned long event;
40494c981e28SIdo Schimmel 	};
40502127d95aSIdo Schimmel };
40512127d95aSIdo Schimmel 
40528d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
4053d7dedee1SIdo Schimmel {
40548d1c802bSDavid Ahern 	struct fib6_info *iter;
4055d7dedee1SIdo Schimmel 	struct fib6_node *fn;
4056d7dedee1SIdo Schimmel 
405793c2fb25SDavid Ahern 	fn = rcu_dereference_protected(rt->fib6_node,
405893c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
4059d7dedee1SIdo Schimmel 	iter = rcu_dereference_protected(fn->leaf,
406093c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
4061d7dedee1SIdo Schimmel 	while (iter) {
406293c2fb25SDavid Ahern 		if (iter->fib6_metric == rt->fib6_metric &&
406333bd5ac5SDavid Ahern 		    rt6_qualify_for_ecmp(iter))
4064d7dedee1SIdo Schimmel 			return iter;
40658fb11a9aSDavid Ahern 		iter = rcu_dereference_protected(iter->fib6_next,
406693c2fb25SDavid Ahern 				lockdep_is_held(&rt->fib6_table->tb6_lock));
4067d7dedee1SIdo Schimmel 	}
4068d7dedee1SIdo Schimmel 
4069d7dedee1SIdo Schimmel 	return NULL;
4070d7dedee1SIdo Schimmel }
4071d7dedee1SIdo Schimmel 
40728d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt)
4073d7dedee1SIdo Schimmel {
40741cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD ||
40751cf844c7SDavid Ahern 	    (rt->fib6_nh->fib_nh_flags & RTNH_F_LINKDOWN &&
40761cf844c7SDavid Ahern 	     ip6_ignore_linkdown(rt->fib6_nh->fib_nh_dev)))
4077d7dedee1SIdo Schimmel 		return true;
4078d7dedee1SIdo Schimmel 
4079d7dedee1SIdo Schimmel 	return false;
4080d7dedee1SIdo Schimmel }
4081d7dedee1SIdo Schimmel 
40828d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt)
4083d7dedee1SIdo Schimmel {
40848d1c802bSDavid Ahern 	struct fib6_info *iter;
4085d7dedee1SIdo Schimmel 	int total = 0;
4086d7dedee1SIdo Schimmel 
4087d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt))
40881cf844c7SDavid Ahern 		total += rt->fib6_nh->fib_nh_weight;
4089d7dedee1SIdo Schimmel 
409093c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
4091d7dedee1SIdo Schimmel 		if (!rt6_is_dead(iter))
40921cf844c7SDavid Ahern 			total += iter->fib6_nh->fib_nh_weight;
4093d7dedee1SIdo Schimmel 	}
4094d7dedee1SIdo Schimmel 
4095d7dedee1SIdo Schimmel 	return total;
4096d7dedee1SIdo Schimmel }
4097d7dedee1SIdo Schimmel 
40988d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
4099d7dedee1SIdo Schimmel {
4100d7dedee1SIdo Schimmel 	int upper_bound = -1;
4101d7dedee1SIdo Schimmel 
4102d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt)) {
41031cf844c7SDavid Ahern 		*weight += rt->fib6_nh->fib_nh_weight;
4104d7dedee1SIdo Schimmel 		upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
4105d7dedee1SIdo Schimmel 						    total) - 1;
4106d7dedee1SIdo Schimmel 	}
41071cf844c7SDavid Ahern 	atomic_set(&rt->fib6_nh->fib_nh_upper_bound, upper_bound);
4108d7dedee1SIdo Schimmel }
4109d7dedee1SIdo Schimmel 
41108d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
4111d7dedee1SIdo Schimmel {
41128d1c802bSDavid Ahern 	struct fib6_info *iter;
4113d7dedee1SIdo Schimmel 	int weight = 0;
4114d7dedee1SIdo Schimmel 
4115d7dedee1SIdo Schimmel 	rt6_upper_bound_set(rt, &weight, total);
4116d7dedee1SIdo Schimmel 
411793c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4118d7dedee1SIdo Schimmel 		rt6_upper_bound_set(iter, &weight, total);
4119d7dedee1SIdo Schimmel }
4120d7dedee1SIdo Schimmel 
41218d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt)
4122d7dedee1SIdo Schimmel {
41238d1c802bSDavid Ahern 	struct fib6_info *first;
4124d7dedee1SIdo Schimmel 	int total;
4125d7dedee1SIdo Schimmel 
4126d7dedee1SIdo Schimmel 	/* In case the entire multipath route was marked for flushing,
4127d7dedee1SIdo Schimmel 	 * then there is no need to rebalance upon the removal of every
4128d7dedee1SIdo Schimmel 	 * sibling route.
4129d7dedee1SIdo Schimmel 	 */
413093c2fb25SDavid Ahern 	if (!rt->fib6_nsiblings || rt->should_flush)
4131d7dedee1SIdo Schimmel 		return;
4132d7dedee1SIdo Schimmel 
4133d7dedee1SIdo Schimmel 	/* During lookup routes are evaluated in order, so we need to
4134d7dedee1SIdo Schimmel 	 * make sure upper bounds are assigned from the first sibling
4135d7dedee1SIdo Schimmel 	 * onwards.
4136d7dedee1SIdo Schimmel 	 */
4137d7dedee1SIdo Schimmel 	first = rt6_multipath_first_sibling(rt);
4138d7dedee1SIdo Schimmel 	if (WARN_ON_ONCE(!first))
4139d7dedee1SIdo Schimmel 		return;
4140d7dedee1SIdo Schimmel 
4141d7dedee1SIdo Schimmel 	total = rt6_multipath_total_weight(first);
4142d7dedee1SIdo Schimmel 	rt6_multipath_upper_bound_set(first, total);
4143d7dedee1SIdo Schimmel }
4144d7dedee1SIdo Schimmel 
41458d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg)
41462127d95aSIdo Schimmel {
41472127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
41487aef6859SDavid Ahern 	struct net *net = dev_net(arg->dev);
41492127d95aSIdo Schimmel 
4150ad1601aeSDavid Ahern 	if (rt != net->ipv6.fib6_null_entry &&
41511cf844c7SDavid Ahern 	    rt->fib6_nh->fib_nh_dev == arg->dev) {
41521cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags &= ~arg->nh_flags;
41537aef6859SDavid Ahern 		fib6_update_sernum_upto_root(net, rt);
4154d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
41551de178edSIdo Schimmel 	}
41562127d95aSIdo Schimmel 
41572127d95aSIdo Schimmel 	return 0;
41582127d95aSIdo Schimmel }
41592127d95aSIdo Schimmel 
4160ecc5663cSDavid Ahern void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
41612127d95aSIdo Schimmel {
41622127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
41632127d95aSIdo Schimmel 		.dev = dev,
41646802f3adSIdo Schimmel 		{
41652127d95aSIdo Schimmel 			.nh_flags = nh_flags,
41666802f3adSIdo Schimmel 		},
41672127d95aSIdo Schimmel 	};
41682127d95aSIdo Schimmel 
41692127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
41702127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
41712127d95aSIdo Schimmel 
41722127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
41732127d95aSIdo Schimmel }
41742127d95aSIdo Schimmel 
41758d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
41761de178edSIdo Schimmel 				   const struct net_device *dev)
41771de178edSIdo Schimmel {
41788d1c802bSDavid Ahern 	struct fib6_info *iter;
41791de178edSIdo Schimmel 
41801cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == dev)
41811de178edSIdo Schimmel 		return true;
418293c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
41831cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == dev)
41841de178edSIdo Schimmel 			return true;
41851de178edSIdo Schimmel 
41861de178edSIdo Schimmel 	return false;
41871de178edSIdo Schimmel }
41881de178edSIdo Schimmel 
41898d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt)
41901de178edSIdo Schimmel {
41918d1c802bSDavid Ahern 	struct fib6_info *iter;
41921de178edSIdo Schimmel 
41931de178edSIdo Schimmel 	rt->should_flush = 1;
419493c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
41951de178edSIdo Schimmel 		iter->should_flush = 1;
41961de178edSIdo Schimmel }
41971de178edSIdo Schimmel 
41988d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
41991de178edSIdo Schimmel 					     const struct net_device *down_dev)
42001de178edSIdo Schimmel {
42018d1c802bSDavid Ahern 	struct fib6_info *iter;
42021de178edSIdo Schimmel 	unsigned int dead = 0;
42031de178edSIdo Schimmel 
42041cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == down_dev ||
42051cf844c7SDavid Ahern 	    rt->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
42061de178edSIdo Schimmel 		dead++;
420793c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
42081cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == down_dev ||
42091cf844c7SDavid Ahern 		    iter->fib6_nh->fib_nh_flags & RTNH_F_DEAD)
42101de178edSIdo Schimmel 			dead++;
42111de178edSIdo Schimmel 
42121de178edSIdo Schimmel 	return dead;
42131de178edSIdo Schimmel }
42141de178edSIdo Schimmel 
42158d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
42161de178edSIdo Schimmel 				       const struct net_device *dev,
4217ecc5663cSDavid Ahern 				       unsigned char nh_flags)
42181de178edSIdo Schimmel {
42198d1c802bSDavid Ahern 	struct fib6_info *iter;
42201de178edSIdo Schimmel 
42211cf844c7SDavid Ahern 	if (rt->fib6_nh->fib_nh_dev == dev)
42221cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags |= nh_flags;
422393c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
42241cf844c7SDavid Ahern 		if (iter->fib6_nh->fib_nh_dev == dev)
42251cf844c7SDavid Ahern 			iter->fib6_nh->fib_nh_flags |= nh_flags;
42261de178edSIdo Schimmel }
42271de178edSIdo Schimmel 
4228a1a22c12SDavid Ahern /* called with write lock held for table with rt */
42298d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
42301da177e4SLinus Torvalds {
42314c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
42324c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
42337aef6859SDavid Ahern 	struct net *net = dev_net(dev);
42348ed67789SDaniel Lezcano 
4235421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
423627c6fa73SIdo Schimmel 		return 0;
423727c6fa73SIdo Schimmel 
423827c6fa73SIdo Schimmel 	switch (arg->event) {
423927c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
42401cf844c7SDavid Ahern 		return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
424127c6fa73SIdo Schimmel 	case NETDEV_DOWN:
42421de178edSIdo Schimmel 		if (rt->should_flush)
424327c6fa73SIdo Schimmel 			return -1;
424493c2fb25SDavid Ahern 		if (!rt->fib6_nsiblings)
42451cf844c7SDavid Ahern 			return rt->fib6_nh->fib_nh_dev == dev ? -1 : 0;
42461de178edSIdo Schimmel 		if (rt6_multipath_uses_dev(rt, dev)) {
42471de178edSIdo Schimmel 			unsigned int count;
42481de178edSIdo Schimmel 
42491de178edSIdo Schimmel 			count = rt6_multipath_dead_count(rt, dev);
425093c2fb25SDavid Ahern 			if (rt->fib6_nsiblings + 1 == count) {
42511de178edSIdo Schimmel 				rt6_multipath_flush(rt);
42521de178edSIdo Schimmel 				return -1;
42531de178edSIdo Schimmel 			}
42541de178edSIdo Schimmel 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
42551de178edSIdo Schimmel 						   RTNH_F_LINKDOWN);
42567aef6859SDavid Ahern 			fib6_update_sernum(net, rt);
4257d7dedee1SIdo Schimmel 			rt6_multipath_rebalance(rt);
42581de178edSIdo Schimmel 		}
42591de178edSIdo Schimmel 		return -2;
426027c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
42611cf844c7SDavid Ahern 		if (rt->fib6_nh->fib_nh_dev != dev ||
426293c2fb25SDavid Ahern 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
426327c6fa73SIdo Schimmel 			break;
42641cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
4265d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
426627c6fa73SIdo Schimmel 		break;
42672b241361SIdo Schimmel 	}
4268c159d30cSDavid S. Miller 
42691da177e4SLinus Torvalds 	return 0;
42701da177e4SLinus Torvalds }
42711da177e4SLinus Torvalds 
427227c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
42731da177e4SLinus Torvalds {
42744c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
42758ed67789SDaniel Lezcano 		.dev = dev,
42766802f3adSIdo Schimmel 		{
42774c981e28SIdo Schimmel 			.event = event,
42786802f3adSIdo Schimmel 		},
42798ed67789SDaniel Lezcano 	};
42807c6bb7d2SDavid Ahern 	struct net *net = dev_net(dev);
42818ed67789SDaniel Lezcano 
42827c6bb7d2SDavid Ahern 	if (net->ipv6.sysctl.skip_notify_on_dev_down)
42837c6bb7d2SDavid Ahern 		fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
42847c6bb7d2SDavid Ahern 	else
42857c6bb7d2SDavid Ahern 		fib6_clean_all(net, fib6_ifdown, &arg);
42864c981e28SIdo Schimmel }
42874c981e28SIdo Schimmel 
42884c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
42894c981e28SIdo Schimmel {
42904c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
42914c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
42924c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
42931da177e4SLinus Torvalds }
42941da177e4SLinus Torvalds 
429595c96174SEric Dumazet struct rt6_mtu_change_arg {
42961da177e4SLinus Torvalds 	struct net_device *dev;
429795c96174SEric Dumazet 	unsigned int mtu;
4298c0b220cfSDavid Ahern 	struct fib6_info *f6i;
42991da177e4SLinus Torvalds };
43001da177e4SLinus Torvalds 
4301cc5c073aSDavid Ahern static int fib6_nh_mtu_change(struct fib6_nh *nh, void *_arg)
4302c0b220cfSDavid Ahern {
4303c0b220cfSDavid Ahern 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *)_arg;
4304cc5c073aSDavid Ahern 	struct fib6_info *f6i = arg->f6i;
4305c0b220cfSDavid Ahern 
4306c0b220cfSDavid Ahern 	/* For administrative MTU increase, there is no way to discover
4307c0b220cfSDavid Ahern 	 * IPv6 PMTU increase, so PMTU increase should be updated here.
4308c0b220cfSDavid Ahern 	 * Since RFC 1981 doesn't include administrative MTU increase
4309c0b220cfSDavid Ahern 	 * update PMTU increase is a MUST. (i.e. jumbo frame)
4310c0b220cfSDavid Ahern 	 */
4311c0b220cfSDavid Ahern 	if (nh->fib_nh_dev == arg->dev) {
4312c0b220cfSDavid Ahern 		struct inet6_dev *idev = __in6_dev_get(arg->dev);
4313c0b220cfSDavid Ahern 		u32 mtu = f6i->fib6_pmtu;
4314c0b220cfSDavid Ahern 
4315c0b220cfSDavid Ahern 		if (mtu >= arg->mtu ||
4316c0b220cfSDavid Ahern 		    (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4317c0b220cfSDavid Ahern 			fib6_metric_set(f6i, RTAX_MTU, arg->mtu);
4318c0b220cfSDavid Ahern 
4319c0b220cfSDavid Ahern 		spin_lock_bh(&rt6_exception_lock);
4320cc5c073aSDavid Ahern 		rt6_exceptions_update_pmtu(idev, nh, arg->mtu);
4321c0b220cfSDavid Ahern 		spin_unlock_bh(&rt6_exception_lock);
4322c0b220cfSDavid Ahern 	}
4323c0b220cfSDavid Ahern 
4324c0b220cfSDavid Ahern 	return 0;
4325c0b220cfSDavid Ahern }
4326c0b220cfSDavid Ahern 
4327c0b220cfSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *f6i, void *p_arg)
43281da177e4SLinus Torvalds {
43291da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
43301da177e4SLinus Torvalds 	struct inet6_dev *idev;
43311da177e4SLinus Torvalds 
43321da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
43331da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
43341da177e4SLinus Torvalds 	   We still use this lock to block changes
43351da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
43361da177e4SLinus Torvalds 	*/
43371da177e4SLinus Torvalds 
43381da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
433938308473SDavid S. Miller 	if (!idev)
43401da177e4SLinus Torvalds 		return 0;
43411da177e4SLinus Torvalds 
4342c0b220cfSDavid Ahern 	if (fib6_metric_locked(f6i, RTAX_MTU))
43431da177e4SLinus Torvalds 		return 0;
4344c0b220cfSDavid Ahern 
4345c0b220cfSDavid Ahern 	arg->f6i = f6i;
43461cf844c7SDavid Ahern 	return fib6_nh_mtu_change(f6i->fib6_nh, arg);
43471da177e4SLinus Torvalds }
43481da177e4SLinus Torvalds 
434995c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
43501da177e4SLinus Torvalds {
4351c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
4352c71099acSThomas Graf 		.dev = dev,
4353c71099acSThomas Graf 		.mtu = mtu,
4354c71099acSThomas Graf 	};
43551da177e4SLinus Torvalds 
43560c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
43571da177e4SLinus Torvalds }
43581da177e4SLinus Torvalds 
4359ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
436075425657SDavid Ahern 	[RTA_UNSPEC]		= { .strict_start_type = RTA_DPORT + 1 },
43615176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
4362aa8f8778SEric Dumazet 	[RTA_PREFSRC]		= { .len = sizeof(struct in6_addr) },
436386872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
4364ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
436586872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
436686872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
436751ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
4368c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
436919e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
437019e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
437132bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
4372622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
43733b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
4374aa8f8778SEric Dumazet 	[RTA_TABLE]		= { .type = NLA_U32 },
4375eacb9384SRoopa Prabhu 	[RTA_IP_PROTO]		= { .type = NLA_U8 },
4376eacb9384SRoopa Prabhu 	[RTA_SPORT]		= { .type = NLA_U16 },
4377eacb9384SRoopa Prabhu 	[RTA_DPORT]		= { .type = NLA_U16 },
437886872cb5SThomas Graf };
437986872cb5SThomas Graf 
438086872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4381333c4301SDavid Ahern 			      struct fib6_config *cfg,
4382333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
43831da177e4SLinus Torvalds {
438486872cb5SThomas Graf 	struct rtmsg *rtm;
438586872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
4386c78ba6d6SLubomir Rintel 	unsigned int pref;
438786872cb5SThomas Graf 	int err;
43881da177e4SLinus Torvalds 
43898cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
43908cb08174SJohannes Berg 				     rtm_ipv6_policy, extack);
439186872cb5SThomas Graf 	if (err < 0)
439286872cb5SThomas Graf 		goto errout;
43931da177e4SLinus Torvalds 
439486872cb5SThomas Graf 	err = -EINVAL;
439586872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
439686872cb5SThomas Graf 
439784db8407SMaciej Żenczykowski 	*cfg = (struct fib6_config){
439884db8407SMaciej Żenczykowski 		.fc_table = rtm->rtm_table,
439984db8407SMaciej Żenczykowski 		.fc_dst_len = rtm->rtm_dst_len,
440084db8407SMaciej Żenczykowski 		.fc_src_len = rtm->rtm_src_len,
440184db8407SMaciej Żenczykowski 		.fc_flags = RTF_UP,
440284db8407SMaciej Żenczykowski 		.fc_protocol = rtm->rtm_protocol,
440384db8407SMaciej Żenczykowski 		.fc_type = rtm->rtm_type,
440484db8407SMaciej Żenczykowski 
440584db8407SMaciej Żenczykowski 		.fc_nlinfo.portid = NETLINK_CB(skb).portid,
440684db8407SMaciej Żenczykowski 		.fc_nlinfo.nlh = nlh,
440784db8407SMaciej Żenczykowski 		.fc_nlinfo.nl_net = sock_net(skb->sk),
440884db8407SMaciej Żenczykowski 	};
440986872cb5SThomas Graf 
4410ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
4411ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
4412b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
4413b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
441486872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
441586872cb5SThomas Graf 
4416ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
4417ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
4418ab79ad14SMaciej Żenczykowski 
44191f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
44201f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
44211f56a01fSMartin KaFai Lau 
4422fc1e64e1SDavid Ahern 	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4423fc1e64e1SDavid Ahern 
442486872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
442567b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
442686872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
44271da177e4SLinus Torvalds 	}
4428e3818541SDavid Ahern 	if (tb[RTA_VIA]) {
4429e3818541SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4430e3818541SDavid Ahern 		goto errout;
4431e3818541SDavid Ahern 	}
443286872cb5SThomas Graf 
443386872cb5SThomas Graf 	if (tb[RTA_DST]) {
443486872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
443586872cb5SThomas Graf 
443686872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
443786872cb5SThomas Graf 			goto errout;
443886872cb5SThomas Graf 
443986872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
44401da177e4SLinus Torvalds 	}
444186872cb5SThomas Graf 
444286872cb5SThomas Graf 	if (tb[RTA_SRC]) {
444386872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
444486872cb5SThomas Graf 
444586872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
444686872cb5SThomas Graf 			goto errout;
444786872cb5SThomas Graf 
444886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
44491da177e4SLinus Torvalds 	}
445086872cb5SThomas Graf 
4451c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
445267b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4453c3968a85SDaniel Walter 
445486872cb5SThomas Graf 	if (tb[RTA_OIF])
445586872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
445686872cb5SThomas Graf 
445786872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
445886872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
445986872cb5SThomas Graf 
446086872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
446186872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
446286872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
44631da177e4SLinus Torvalds 	}
446486872cb5SThomas Graf 
446586872cb5SThomas Graf 	if (tb[RTA_TABLE])
446686872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
446786872cb5SThomas Graf 
446851ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
446951ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
447051ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
44719ed59592SDavid Ahern 
44729ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4473c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
44749ed59592SDavid Ahern 		if (err < 0)
44759ed59592SDavid Ahern 			goto errout;
447651ebd318SNicolas Dichtel 	}
447751ebd318SNicolas Dichtel 
4478c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
4479c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
4480c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
4481c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
4482c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
4483c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
4484c78ba6d6SLubomir Rintel 	}
4485c78ba6d6SLubomir Rintel 
448619e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
448719e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
448819e42e45SRoopa Prabhu 
44899ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
449019e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
449119e42e45SRoopa Prabhu 
4492c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
44939ed59592SDavid Ahern 		if (err < 0)
44949ed59592SDavid Ahern 			goto errout;
44959ed59592SDavid Ahern 	}
44969ed59592SDavid Ahern 
449732bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
449832bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
449932bc201eSXin Long 
450032bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
450132bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
450232bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
450332bc201eSXin Long 		}
450432bc201eSXin Long 	}
450532bc201eSXin Long 
450686872cb5SThomas Graf 	err = 0;
450786872cb5SThomas Graf errout:
450886872cb5SThomas Graf 	return err;
45091da177e4SLinus Torvalds }
45101da177e4SLinus Torvalds 
45116b9ea5a6SRoopa Prabhu struct rt6_nh {
45128d1c802bSDavid Ahern 	struct fib6_info *fib6_info;
45136b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
45146b9ea5a6SRoopa Prabhu 	struct list_head next;
45156b9ea5a6SRoopa Prabhu };
45166b9ea5a6SRoopa Prabhu 
4517d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net,
4518d4ead6b3SDavid Ahern 				 struct list_head *rt6_nh_list,
45198d1c802bSDavid Ahern 				 struct fib6_info *rt,
45208d1c802bSDavid Ahern 				 struct fib6_config *r_cfg)
45216b9ea5a6SRoopa Prabhu {
45226b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
45236b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
45246b9ea5a6SRoopa Prabhu 
45256b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
45268d1c802bSDavid Ahern 		/* check if fib6_info already exists */
45278d1c802bSDavid Ahern 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
45286b9ea5a6SRoopa Prabhu 			return err;
45296b9ea5a6SRoopa Prabhu 	}
45306b9ea5a6SRoopa Prabhu 
45316b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
45326b9ea5a6SRoopa Prabhu 	if (!nh)
45336b9ea5a6SRoopa Prabhu 		return -ENOMEM;
45348d1c802bSDavid Ahern 	nh->fib6_info = rt;
45356b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
45366b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
45376b9ea5a6SRoopa Prabhu 
45386b9ea5a6SRoopa Prabhu 	return 0;
45396b9ea5a6SRoopa Prabhu }
45406b9ea5a6SRoopa Prabhu 
45418d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt,
45428d1c802bSDavid Ahern 				   struct fib6_info *rt_last,
45433b1137feSDavid Ahern 				   struct nl_info *info,
45443b1137feSDavid Ahern 				   __u16 nlflags)
45453b1137feSDavid Ahern {
45463b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
45473b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
45483b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
45493b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
45503b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
45513b1137feSDavid Ahern 	 */
455293c2fb25SDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
455393c2fb25SDavid Ahern 		rt = list_first_entry(&rt_last->fib6_siblings,
45548d1c802bSDavid Ahern 				      struct fib6_info,
455593c2fb25SDavid Ahern 				      fib6_siblings);
45563b1137feSDavid Ahern 	}
45573b1137feSDavid Ahern 
45583b1137feSDavid Ahern 	if (rt)
45593b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
45603b1137feSDavid Ahern }
45613b1137feSDavid Ahern 
4562333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
4563333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
456451ebd318SNicolas Dichtel {
45658d1c802bSDavid Ahern 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
45663b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
456751ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
456851ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
45698d1c802bSDavid Ahern 	struct fib6_info *rt;
45706b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
45716b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
45723b1137feSDavid Ahern 	__u16 nlflags;
457351ebd318SNicolas Dichtel 	int remaining;
457451ebd318SNicolas Dichtel 	int attrlen;
45756b9ea5a6SRoopa Prabhu 	int err = 1;
45766b9ea5a6SRoopa Prabhu 	int nhn = 0;
45776b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
45786b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
45796b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
458051ebd318SNicolas Dichtel 
45813b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
45823b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
45833b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
45843b1137feSDavid Ahern 
458535f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
458651ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
458751ebd318SNicolas Dichtel 
45886b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
45898d1c802bSDavid Ahern 	 * fib6_info structs per nexthop
45906b9ea5a6SRoopa Prabhu 	 */
459151ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
459251ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
459351ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
459451ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
459551ebd318SNicolas Dichtel 
459651ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
459751ebd318SNicolas Dichtel 		if (attrlen > 0) {
459851ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
459951ebd318SNicolas Dichtel 
460051ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
460151ebd318SNicolas Dichtel 			if (nla) {
460267b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
460351ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
460451ebd318SNicolas Dichtel 			}
460519e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
460619e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
460719e42e45SRoopa Prabhu 			if (nla)
460819e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
460951ebd318SNicolas Dichtel 		}
46106b9ea5a6SRoopa Prabhu 
461168e2ffdeSDavid Ahern 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
4612acb54e3cSDavid Ahern 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
46138c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
46148c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
46158c5b83f0SRoopa Prabhu 			rt = NULL;
46166b9ea5a6SRoopa Prabhu 			goto cleanup;
46178c5b83f0SRoopa Prabhu 		}
4618b5d2d75eSDavid Ahern 		if (!rt6_qualify_for_ecmp(rt)) {
4619b5d2d75eSDavid Ahern 			err = -EINVAL;
4620b5d2d75eSDavid Ahern 			NL_SET_ERR_MSG(extack,
4621b5d2d75eSDavid Ahern 				       "Device only routes can not be added for IPv6 using the multipath API.");
4622b5d2d75eSDavid Ahern 			fib6_info_release(rt);
4623b5d2d75eSDavid Ahern 			goto cleanup;
4624b5d2d75eSDavid Ahern 		}
46256b9ea5a6SRoopa Prabhu 
46261cf844c7SDavid Ahern 		rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
4627398958aeSIdo Schimmel 
4628d4ead6b3SDavid Ahern 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4629d4ead6b3SDavid Ahern 					    rt, &r_cfg);
463051ebd318SNicolas Dichtel 		if (err) {
463193531c67SDavid Ahern 			fib6_info_release(rt);
46326b9ea5a6SRoopa Prabhu 			goto cleanup;
463351ebd318SNicolas Dichtel 		}
46346b9ea5a6SRoopa Prabhu 
46356b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
463651ebd318SNicolas Dichtel 	}
46376b9ea5a6SRoopa Prabhu 
46383b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
46393b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
46403b1137feSDavid Ahern 	 * the full route when done
46413b1137feSDavid Ahern 	 */
46423b1137feSDavid Ahern 	info->skip_notify = 1;
46433b1137feSDavid Ahern 
46446b9ea5a6SRoopa Prabhu 	err_nh = NULL;
46456b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
46468d1c802bSDavid Ahern 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
46478d1c802bSDavid Ahern 		fib6_info_release(nh->fib6_info);
46483b1137feSDavid Ahern 
4649f7225172SDavid Ahern 		if (!err) {
4650f7225172SDavid Ahern 			/* save reference to last route successfully inserted */
4651f7225172SDavid Ahern 			rt_last = nh->fib6_info;
4652f7225172SDavid Ahern 
46536b9ea5a6SRoopa Prabhu 			/* save reference to first route for notification */
4654f7225172SDavid Ahern 			if (!rt_notif)
46558d1c802bSDavid Ahern 				rt_notif = nh->fib6_info;
4656f7225172SDavid Ahern 		}
46576b9ea5a6SRoopa Prabhu 
46588d1c802bSDavid Ahern 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
46598d1c802bSDavid Ahern 		nh->fib6_info = NULL;
46606b9ea5a6SRoopa Prabhu 		if (err) {
46616b9ea5a6SRoopa Prabhu 			if (replace && nhn)
4662a5a82d84SJakub Kicinski 				NL_SET_ERR_MSG_MOD(extack,
4663a5a82d84SJakub Kicinski 						   "multipath route replace failed (check consistency of installed routes)");
46646b9ea5a6SRoopa Prabhu 			err_nh = nh;
46656b9ea5a6SRoopa Prabhu 			goto add_errout;
46666b9ea5a6SRoopa Prabhu 		}
46676b9ea5a6SRoopa Prabhu 
46681a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
466927596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
467027596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
467127596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
467227596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
467327596472SMichal Kubeček 		 * be added to it.
46741a72418bSNicolas Dichtel 		 */
467527596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
467627596472SMichal Kubeček 						     NLM_F_REPLACE);
46776b9ea5a6SRoopa Prabhu 		nhn++;
46786b9ea5a6SRoopa Prabhu 	}
46796b9ea5a6SRoopa Prabhu 
46803b1137feSDavid Ahern 	/* success ... tell user about new route */
46813b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
46826b9ea5a6SRoopa Prabhu 	goto cleanup;
46836b9ea5a6SRoopa Prabhu 
46846b9ea5a6SRoopa Prabhu add_errout:
46853b1137feSDavid Ahern 	/* send notification for routes that were added so that
46863b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
46873b1137feSDavid Ahern 	 * coherent
46883b1137feSDavid Ahern 	 */
46893b1137feSDavid Ahern 	if (rt_notif)
46903b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
46913b1137feSDavid Ahern 
46926b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
46936b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
46946b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
46956b9ea5a6SRoopa Prabhu 			break;
4696333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
46976b9ea5a6SRoopa Prabhu 	}
46986b9ea5a6SRoopa Prabhu 
46996b9ea5a6SRoopa Prabhu cleanup:
47006b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
47018d1c802bSDavid Ahern 		if (nh->fib6_info)
47028d1c802bSDavid Ahern 			fib6_info_release(nh->fib6_info);
47036b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
47046b9ea5a6SRoopa Prabhu 		kfree(nh);
47056b9ea5a6SRoopa Prabhu 	}
47066b9ea5a6SRoopa Prabhu 
47076b9ea5a6SRoopa Prabhu 	return err;
47086b9ea5a6SRoopa Prabhu }
47096b9ea5a6SRoopa Prabhu 
4710333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
4711333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
47126b9ea5a6SRoopa Prabhu {
47136b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
47146b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
47156b9ea5a6SRoopa Prabhu 	int remaining;
47166b9ea5a6SRoopa Prabhu 	int attrlen;
47176b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
47186b9ea5a6SRoopa Prabhu 
47196b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
47206b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
47216b9ea5a6SRoopa Prabhu 
47226b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
47236b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
47246b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
47256b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
47266b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
47276b9ea5a6SRoopa Prabhu 
47286b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
47296b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
47306b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
47316b9ea5a6SRoopa Prabhu 
47326b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
47336b9ea5a6SRoopa Prabhu 			if (nla) {
47346b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
47356b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
47366b9ea5a6SRoopa Prabhu 			}
47376b9ea5a6SRoopa Prabhu 		}
4738333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
47396b9ea5a6SRoopa Prabhu 		if (err)
47406b9ea5a6SRoopa Prabhu 			last_err = err;
47416b9ea5a6SRoopa Prabhu 
474251ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
474351ebd318SNicolas Dichtel 	}
474451ebd318SNicolas Dichtel 
474551ebd318SNicolas Dichtel 	return last_err;
474651ebd318SNicolas Dichtel }
474751ebd318SNicolas Dichtel 
4748c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4749c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
47501da177e4SLinus Torvalds {
475186872cb5SThomas Graf 	struct fib6_config cfg;
475286872cb5SThomas Graf 	int err;
47531da177e4SLinus Torvalds 
4754333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
475586872cb5SThomas Graf 	if (err < 0)
475686872cb5SThomas Graf 		return err;
475786872cb5SThomas Graf 
475851ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4759333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
47600ae81335SDavid Ahern 	else {
47610ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
4762333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
47631da177e4SLinus Torvalds 	}
47640ae81335SDavid Ahern }
47651da177e4SLinus Torvalds 
4766c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4767c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
47681da177e4SLinus Torvalds {
476986872cb5SThomas Graf 	struct fib6_config cfg;
477086872cb5SThomas Graf 	int err;
47711da177e4SLinus Torvalds 
4772333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
477386872cb5SThomas Graf 	if (err < 0)
477486872cb5SThomas Graf 		return err;
477586872cb5SThomas Graf 
477667f69513SDavid Ahern 	if (cfg.fc_metric == 0)
477767f69513SDavid Ahern 		cfg.fc_metric = IP6_RT_PRIO_USER;
477867f69513SDavid Ahern 
477951ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4780333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
478151ebd318SNicolas Dichtel 	else
4782acb54e3cSDavid Ahern 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
47831da177e4SLinus Torvalds }
47841da177e4SLinus Torvalds 
47858d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt)
4786339bf98fSThomas Graf {
4787beb1afacSDavid Ahern 	int nexthop_len = 0;
4788beb1afacSDavid Ahern 
478993c2fb25SDavid Ahern 	if (rt->fib6_nsiblings) {
4790beb1afacSDavid Ahern 		nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
4791beb1afacSDavid Ahern 			    + NLA_ALIGN(sizeof(struct rtnexthop))
4792beb1afacSDavid Ahern 			    + nla_total_size(16) /* RTA_GATEWAY */
47931cf844c7SDavid Ahern 			    + lwtunnel_get_encap_size(rt->fib6_nh->fib_nh_lws);
4794beb1afacSDavid Ahern 
479593c2fb25SDavid Ahern 		nexthop_len *= rt->fib6_nsiblings;
4796beb1afacSDavid Ahern 	}
4797beb1afacSDavid Ahern 
4798339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
4799339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
4800339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
4801339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
4802339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
4803339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
4804339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
4805339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
4806339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
48076a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
4808ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
4809c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
481019e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
48111cf844c7SDavid Ahern 	       + lwtunnel_get_encap_size(rt->fib6_nh->fib_nh_lws)
4812beb1afacSDavid Ahern 	       + nexthop_len;
4813beb1afacSDavid Ahern }
4814beb1afacSDavid Ahern 
4815d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
48168d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
4817d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
481815e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
4819f8cfe2ceSDavid Ahern 			 unsigned int flags)
48201da177e4SLinus Torvalds {
482122d0bd82SXin Long 	struct rt6_info *rt6 = (struct rt6_info *)dst;
482222d0bd82SXin Long 	struct rt6key *rt6_dst, *rt6_src;
482322d0bd82SXin Long 	u32 *pmetrics, table, rt6_flags;
48241da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
482522d0bd82SXin Long 	struct rtmsg *rtm;
4826d4ead6b3SDavid Ahern 	long expires = 0;
48271da177e4SLinus Torvalds 
482815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
482938308473SDavid S. Miller 	if (!nlh)
483026932566SPatrick McHardy 		return -EMSGSIZE;
48312d7202bfSThomas Graf 
483222d0bd82SXin Long 	if (rt6) {
483322d0bd82SXin Long 		rt6_dst = &rt6->rt6i_dst;
483422d0bd82SXin Long 		rt6_src = &rt6->rt6i_src;
483522d0bd82SXin Long 		rt6_flags = rt6->rt6i_flags;
483622d0bd82SXin Long 	} else {
483722d0bd82SXin Long 		rt6_dst = &rt->fib6_dst;
483822d0bd82SXin Long 		rt6_src = &rt->fib6_src;
483922d0bd82SXin Long 		rt6_flags = rt->fib6_flags;
484022d0bd82SXin Long 	}
484122d0bd82SXin Long 
48422d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
48431da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
484422d0bd82SXin Long 	rtm->rtm_dst_len = rt6_dst->plen;
484522d0bd82SXin Long 	rtm->rtm_src_len = rt6_src->plen;
48461da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
484793c2fb25SDavid Ahern 	if (rt->fib6_table)
484893c2fb25SDavid Ahern 		table = rt->fib6_table->tb6_id;
4849c71099acSThomas Graf 	else
48509e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
485197f0082aSKalash Nainwal 	rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
4852c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
4853c78679e8SDavid S. Miller 		goto nla_put_failure;
4854e8478e80SDavid Ahern 
4855e8478e80SDavid Ahern 	rtm->rtm_type = rt->fib6_type;
48561da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
48571da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
485893c2fb25SDavid Ahern 	rtm->rtm_protocol = rt->fib6_protocol;
48591da177e4SLinus Torvalds 
486022d0bd82SXin Long 	if (rt6_flags & RTF_CACHE)
48611da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
48621da177e4SLinus Torvalds 
4863d4ead6b3SDavid Ahern 	if (dest) {
4864d4ead6b3SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, dest))
4865c78679e8SDavid S. Miller 			goto nla_put_failure;
48661da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
48671da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
486822d0bd82SXin Long 		if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
4869c78679e8SDavid S. Miller 			goto nla_put_failure;
48701da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
48711da177e4SLinus Torvalds 	if (src) {
4872930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
4873c78679e8SDavid S. Miller 			goto nla_put_failure;
48741da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
4875c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
487622d0bd82SXin Long 		   nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
4877c78679e8SDavid S. Miller 		goto nla_put_failure;
48781da177e4SLinus Torvalds #endif
48797bc570c8SYOSHIFUJI Hideaki 	if (iif) {
48807bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
488122d0bd82SXin Long 		if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
4882fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
48832cf75070SNikolay Aleksandrov 
48847bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
48857bc570c8SYOSHIFUJI Hideaki 				return 0;
4886fd61c6baSDavid Ahern 			if (err < 0)
48877bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
48887bc570c8SYOSHIFUJI Hideaki 		} else
48897bc570c8SYOSHIFUJI Hideaki #endif
4890c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
4891c78679e8SDavid S. Miller 				goto nla_put_failure;
4892d4ead6b3SDavid Ahern 	} else if (dest) {
48931da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
4894d4ead6b3SDavid Ahern 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
4895930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4896c78679e8SDavid S. Miller 			goto nla_put_failure;
4897c3968a85SDaniel Walter 	}
4898c3968a85SDaniel Walter 
489993c2fb25SDavid Ahern 	if (rt->fib6_prefsrc.plen) {
4900c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
490193c2fb25SDavid Ahern 		saddr_buf = rt->fib6_prefsrc.addr;
4902930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4903c78679e8SDavid S. Miller 			goto nla_put_failure;
49041da177e4SLinus Torvalds 	}
49052d7202bfSThomas Graf 
4906d4ead6b3SDavid Ahern 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4907d4ead6b3SDavid Ahern 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
49082d7202bfSThomas Graf 		goto nla_put_failure;
49092d7202bfSThomas Graf 
491093c2fb25SDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
4911beb1afacSDavid Ahern 		goto nla_put_failure;
4912beb1afacSDavid Ahern 
4913beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
4914beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
4915beb1afacSDavid Ahern 	 */
491622d0bd82SXin Long 	if (rt6) {
491722d0bd82SXin Long 		if (rt6_flags & RTF_GATEWAY &&
491822d0bd82SXin Long 		    nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
491922d0bd82SXin Long 			goto nla_put_failure;
492022d0bd82SXin Long 
492122d0bd82SXin Long 		if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
492222d0bd82SXin Long 			goto nla_put_failure;
492322d0bd82SXin Long 	} else if (rt->fib6_nsiblings) {
49248d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
4925beb1afacSDavid Ahern 		struct nlattr *mp;
4926beb1afacSDavid Ahern 
4927ae0be8deSMichal Kubecek 		mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
4928beb1afacSDavid Ahern 		if (!mp)
4929beb1afacSDavid Ahern 			goto nla_put_failure;
4930beb1afacSDavid Ahern 
49311cf844c7SDavid Ahern 		if (fib_add_nexthop(skb, &rt->fib6_nh->nh_common,
49321cf844c7SDavid Ahern 				    rt->fib6_nh->fib_nh_weight) < 0)
4933beb1afacSDavid Ahern 			goto nla_put_failure;
4934beb1afacSDavid Ahern 
4935beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
493693c2fb25SDavid Ahern 					 &rt->fib6_siblings, fib6_siblings) {
49371cf844c7SDavid Ahern 			if (fib_add_nexthop(skb, &sibling->fib6_nh->nh_common,
49381cf844c7SDavid Ahern 					    sibling->fib6_nh->fib_nh_weight) < 0)
493994f826b8SEric Dumazet 				goto nla_put_failure;
494094f826b8SEric Dumazet 		}
49412d7202bfSThomas Graf 
4942beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
4943beb1afacSDavid Ahern 	} else {
4944ecc5663cSDavid Ahern 		unsigned char nh_flags = 0;
4945ecc5663cSDavid Ahern 
49461cf844c7SDavid Ahern 		if (fib_nexthop_info(skb, &rt->fib6_nh->nh_common,
4947ecc5663cSDavid Ahern 				     &nh_flags, false) < 0)
4948c78679e8SDavid S. Miller 			goto nla_put_failure;
4949ecc5663cSDavid Ahern 
4950ecc5663cSDavid Ahern 		rtm->rtm_flags |= nh_flags;
4951beb1afacSDavid Ahern 	}
49528253947eSLi Wei 
495322d0bd82SXin Long 	if (rt6_flags & RTF_EXPIRES) {
495414895687SDavid Ahern 		expires = dst ? dst->expires : rt->expires;
495514895687SDavid Ahern 		expires -= jiffies;
495614895687SDavid Ahern 	}
495769cdf8f9SYOSHIFUJI Hideaki 
4958d4ead6b3SDavid Ahern 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
4959e3703b3dSThomas Graf 		goto nla_put_failure;
49601da177e4SLinus Torvalds 
496122d0bd82SXin Long 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
4962c78ba6d6SLubomir Rintel 		goto nla_put_failure;
4963c78ba6d6SLubomir Rintel 
496419e42e45SRoopa Prabhu 
4965053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4966053c095aSJohannes Berg 	return 0;
49672d7202bfSThomas Graf 
49682d7202bfSThomas Graf nla_put_failure:
496926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
497026932566SPatrick McHardy 	return -EMSGSIZE;
49711da177e4SLinus Torvalds }
49721da177e4SLinus Torvalds 
497313e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i,
497413e38901SDavid Ahern 			       const struct net_device *dev)
497513e38901SDavid Ahern {
49761cf844c7SDavid Ahern 	if (f6i->fib6_nh->fib_nh_dev == dev)
497713e38901SDavid Ahern 		return true;
497813e38901SDavid Ahern 
497913e38901SDavid Ahern 	if (f6i->fib6_nsiblings) {
498013e38901SDavid Ahern 		struct fib6_info *sibling, *next_sibling;
498113e38901SDavid Ahern 
498213e38901SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
498313e38901SDavid Ahern 					 &f6i->fib6_siblings, fib6_siblings) {
49841cf844c7SDavid Ahern 			if (sibling->fib6_nh->fib_nh_dev == dev)
498513e38901SDavid Ahern 				return true;
498613e38901SDavid Ahern 		}
498713e38901SDavid Ahern 	}
498813e38901SDavid Ahern 
498913e38901SDavid Ahern 	return false;
499013e38901SDavid Ahern }
499113e38901SDavid Ahern 
49928d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg)
49931da177e4SLinus Torvalds {
49941da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
499513e38901SDavid Ahern 	struct fib_dump_filter *filter = &arg->filter;
499613e38901SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
49971f17e2f2SDavid Ahern 	struct net *net = arg->net;
49981f17e2f2SDavid Ahern 
4999421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
50001f17e2f2SDavid Ahern 		return 0;
50011da177e4SLinus Torvalds 
500213e38901SDavid Ahern 	if ((filter->flags & RTM_F_PREFIX) &&
500393c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_PREFIX_RT)) {
5004f8cfe2ceSDavid Ahern 		/* success since this is not a prefix route */
5005f8cfe2ceSDavid Ahern 		return 1;
5006f8cfe2ceSDavid Ahern 	}
500713e38901SDavid Ahern 	if (filter->filter_set) {
500813e38901SDavid Ahern 		if ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
500913e38901SDavid Ahern 		    (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
501013e38901SDavid Ahern 		    (filter->protocol && rt->fib6_protocol != filter->protocol)) {
501113e38901SDavid Ahern 			return 1;
501213e38901SDavid Ahern 		}
501313e38901SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
5014f8cfe2ceSDavid Ahern 	}
50151da177e4SLinus Torvalds 
5016d4ead6b3SDavid Ahern 	return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
5017d4ead6b3SDavid Ahern 			     RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
501813e38901SDavid Ahern 			     arg->cb->nlh->nlmsg_seq, flags);
50191da177e4SLinus Torvalds }
50201da177e4SLinus Torvalds 
50210eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
50220eff0a27SJakub Kicinski 					const struct nlmsghdr *nlh,
50230eff0a27SJakub Kicinski 					struct nlattr **tb,
50240eff0a27SJakub Kicinski 					struct netlink_ext_ack *extack)
50250eff0a27SJakub Kicinski {
50260eff0a27SJakub Kicinski 	struct rtmsg *rtm;
50270eff0a27SJakub Kicinski 	int i, err;
50280eff0a27SJakub Kicinski 
50290eff0a27SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
50300eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
50310eff0a27SJakub Kicinski 				   "Invalid header for get route request");
50320eff0a27SJakub Kicinski 		return -EINVAL;
50330eff0a27SJakub Kicinski 	}
50340eff0a27SJakub Kicinski 
50350eff0a27SJakub Kicinski 	if (!netlink_strict_get_check(skb))
50368cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
50370eff0a27SJakub Kicinski 					      rtm_ipv6_policy, extack);
50380eff0a27SJakub Kicinski 
50390eff0a27SJakub Kicinski 	rtm = nlmsg_data(nlh);
50400eff0a27SJakub Kicinski 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
50410eff0a27SJakub Kicinski 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
50420eff0a27SJakub Kicinski 	    rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
50430eff0a27SJakub Kicinski 	    rtm->rtm_type) {
50440eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
50450eff0a27SJakub Kicinski 		return -EINVAL;
50460eff0a27SJakub Kicinski 	}
50470eff0a27SJakub Kicinski 	if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
50480eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
50490eff0a27SJakub Kicinski 				   "Invalid flags for get route request");
50500eff0a27SJakub Kicinski 		return -EINVAL;
50510eff0a27SJakub Kicinski 	}
50520eff0a27SJakub Kicinski 
50538cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
50540eff0a27SJakub Kicinski 					    rtm_ipv6_policy, extack);
50550eff0a27SJakub Kicinski 	if (err)
50560eff0a27SJakub Kicinski 		return err;
50570eff0a27SJakub Kicinski 
50580eff0a27SJakub Kicinski 	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
50590eff0a27SJakub Kicinski 	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
50600eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
50610eff0a27SJakub Kicinski 		return -EINVAL;
50620eff0a27SJakub Kicinski 	}
50630eff0a27SJakub Kicinski 
50640eff0a27SJakub Kicinski 	for (i = 0; i <= RTA_MAX; i++) {
50650eff0a27SJakub Kicinski 		if (!tb[i])
50660eff0a27SJakub Kicinski 			continue;
50670eff0a27SJakub Kicinski 
50680eff0a27SJakub Kicinski 		switch (i) {
50690eff0a27SJakub Kicinski 		case RTA_SRC:
50700eff0a27SJakub Kicinski 		case RTA_DST:
50710eff0a27SJakub Kicinski 		case RTA_IIF:
50720eff0a27SJakub Kicinski 		case RTA_OIF:
50730eff0a27SJakub Kicinski 		case RTA_MARK:
50740eff0a27SJakub Kicinski 		case RTA_UID:
50750eff0a27SJakub Kicinski 		case RTA_SPORT:
50760eff0a27SJakub Kicinski 		case RTA_DPORT:
50770eff0a27SJakub Kicinski 		case RTA_IP_PROTO:
50780eff0a27SJakub Kicinski 			break;
50790eff0a27SJakub Kicinski 		default:
50800eff0a27SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
50810eff0a27SJakub Kicinski 			return -EINVAL;
50820eff0a27SJakub Kicinski 		}
50830eff0a27SJakub Kicinski 	}
50840eff0a27SJakub Kicinski 
50850eff0a27SJakub Kicinski 	return 0;
50860eff0a27SJakub Kicinski }
50870eff0a27SJakub Kicinski 
5088c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5089c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
50901da177e4SLinus Torvalds {
50913b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
5092ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
509318c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
5094a68886a6SDavid Ahern 	struct fib6_info *from;
509518c3a61cSRoopa Prabhu 	struct dst_entry *dst;
50961da177e4SLinus Torvalds 	struct rt6_info *rt;
5097ab364a6fSThomas Graf 	struct sk_buff *skb;
5098ab364a6fSThomas Graf 	struct rtmsg *rtm;
5099744486d4SMaciej Żenczykowski 	struct flowi6 fl6 = {};
510018c3a61cSRoopa Prabhu 	bool fibmatch;
5101ab364a6fSThomas Graf 
51020eff0a27SJakub Kicinski 	err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
5103ab364a6fSThomas Graf 	if (err < 0)
5104ab364a6fSThomas Graf 		goto errout;
5105ab364a6fSThomas Graf 
5106ab364a6fSThomas Graf 	err = -EINVAL;
510738b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
510838b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
510918c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
5110ab364a6fSThomas Graf 
5111ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
5112ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
5113ab364a6fSThomas Graf 			goto errout;
5114ab364a6fSThomas Graf 
51154e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
5116ab364a6fSThomas Graf 	}
5117ab364a6fSThomas Graf 
5118ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
5119ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
5120ab364a6fSThomas Graf 			goto errout;
5121ab364a6fSThomas Graf 
51224e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
5123ab364a6fSThomas Graf 	}
5124ab364a6fSThomas Graf 
5125ab364a6fSThomas Graf 	if (tb[RTA_IIF])
5126ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
5127ab364a6fSThomas Graf 
5128ab364a6fSThomas Graf 	if (tb[RTA_OIF])
512972331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
5130ab364a6fSThomas Graf 
51312e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
51322e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
51332e47b291SLorenzo Colitti 
5134622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
5135622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
5136622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
5137622ec2c9SLorenzo Colitti 	else
5138622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5139622ec2c9SLorenzo Colitti 
5140eacb9384SRoopa Prabhu 	if (tb[RTA_SPORT])
5141eacb9384SRoopa Prabhu 		fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5142eacb9384SRoopa Prabhu 
5143eacb9384SRoopa Prabhu 	if (tb[RTA_DPORT])
5144eacb9384SRoopa Prabhu 		fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5145eacb9384SRoopa Prabhu 
5146eacb9384SRoopa Prabhu 	if (tb[RTA_IP_PROTO]) {
5147eacb9384SRoopa Prabhu 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
51485e1a99eaSHangbin Liu 						  &fl6.flowi6_proto, AF_INET6,
51495e1a99eaSHangbin Liu 						  extack);
5150eacb9384SRoopa Prabhu 		if (err)
5151eacb9384SRoopa Prabhu 			goto errout;
5152eacb9384SRoopa Prabhu 	}
5153eacb9384SRoopa Prabhu 
5154ab364a6fSThomas Graf 	if (iif) {
5155ab364a6fSThomas Graf 		struct net_device *dev;
515672331bc0SShmulik Ladkani 		int flags = 0;
515772331bc0SShmulik Ladkani 
5158121622dbSFlorian Westphal 		rcu_read_lock();
5159121622dbSFlorian Westphal 
5160121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
5161ab364a6fSThomas Graf 		if (!dev) {
5162121622dbSFlorian Westphal 			rcu_read_unlock();
5163ab364a6fSThomas Graf 			err = -ENODEV;
5164ab364a6fSThomas Graf 			goto errout;
5165ab364a6fSThomas Graf 		}
516672331bc0SShmulik Ladkani 
516772331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
516872331bc0SShmulik Ladkani 
516972331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
517072331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
517172331bc0SShmulik Ladkani 
5172b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
5173121622dbSFlorian Westphal 
5174121622dbSFlorian Westphal 		rcu_read_unlock();
517572331bc0SShmulik Ladkani 	} else {
517672331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
517772331bc0SShmulik Ladkani 
517818c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
517918c3a61cSRoopa Prabhu 	}
518018c3a61cSRoopa Prabhu 
518118c3a61cSRoopa Prabhu 
518218c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
518318c3a61cSRoopa Prabhu 	if (rt->dst.error) {
518418c3a61cSRoopa Prabhu 		err = rt->dst.error;
518518c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
518618c3a61cSRoopa Prabhu 		goto errout;
5187ab364a6fSThomas Graf 	}
51881da177e4SLinus Torvalds 
51899d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
51909d6acb3bSWANG Cong 		err = rt->dst.error;
51919d6acb3bSWANG Cong 		ip6_rt_put(rt);
51929d6acb3bSWANG Cong 		goto errout;
51939d6acb3bSWANG Cong 	}
51949d6acb3bSWANG Cong 
51951da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
519638308473SDavid S. Miller 	if (!skb) {
519794e187c0SAmerigo Wang 		ip6_rt_put(rt);
5198ab364a6fSThomas Graf 		err = -ENOBUFS;
5199ab364a6fSThomas Graf 		goto errout;
5200ab364a6fSThomas Graf 	}
52011da177e4SLinus Torvalds 
5202d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
5203a68886a6SDavid Ahern 
5204a68886a6SDavid Ahern 	rcu_read_lock();
5205a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
5206886b7a50SMartin KaFai Lau 	if (from) {
520718c3a61cSRoopa Prabhu 		if (fibmatch)
5208886b7a50SMartin KaFai Lau 			err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5209886b7a50SMartin KaFai Lau 					    iif, RTM_NEWROUTE,
5210886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
521118c3a61cSRoopa Prabhu 					    nlh->nlmsg_seq, 0);
521218c3a61cSRoopa Prabhu 		else
5213a68886a6SDavid Ahern 			err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5214a68886a6SDavid Ahern 					    &fl6.saddr, iif, RTM_NEWROUTE,
5215886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
5216886b7a50SMartin KaFai Lau 					    nlh->nlmsg_seq, 0);
5217886b7a50SMartin KaFai Lau 	} else {
5218886b7a50SMartin KaFai Lau 		err = -ENETUNREACH;
5219886b7a50SMartin KaFai Lau 	}
5220a68886a6SDavid Ahern 	rcu_read_unlock();
5221a68886a6SDavid Ahern 
52221da177e4SLinus Torvalds 	if (err < 0) {
5223ab364a6fSThomas Graf 		kfree_skb(skb);
5224ab364a6fSThomas Graf 		goto errout;
52251da177e4SLinus Torvalds 	}
52261da177e4SLinus Torvalds 
522715e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
5228ab364a6fSThomas Graf errout:
52291da177e4SLinus Torvalds 	return err;
52301da177e4SLinus Torvalds }
52311da177e4SLinus Torvalds 
52328d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
523337a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
52341da177e4SLinus Torvalds {
52351da177e4SLinus Torvalds 	struct sk_buff *skb;
52365578689aSDaniel Lezcano 	struct net *net = info->nl_net;
5237528c4cebSDenis V. Lunev 	u32 seq;
5238528c4cebSDenis V. Lunev 	int err;
52390d51aa80SJamal Hadi Salim 
5240528c4cebSDenis V. Lunev 	err = -ENOBUFS;
524138308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
524286872cb5SThomas Graf 
524319e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
524438308473SDavid S. Miller 	if (!skb)
524521713ebcSThomas Graf 		goto errout;
52461da177e4SLinus Torvalds 
5247d4ead6b3SDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5248f8cfe2ceSDavid Ahern 			    event, info->portid, seq, nlm_flags);
524926932566SPatrick McHardy 	if (err < 0) {
525026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
525126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
525226932566SPatrick McHardy 		kfree_skb(skb);
525326932566SPatrick McHardy 		goto errout;
525426932566SPatrick McHardy 	}
525515e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
52565578689aSDaniel Lezcano 		    info->nlh, gfp_any());
52571ce85fe4SPablo Neira Ayuso 	return;
525821713ebcSThomas Graf errout:
525921713ebcSThomas Graf 	if (err < 0)
52605578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
52611da177e4SLinus Torvalds }
52621da177e4SLinus Torvalds 
526319a3b7eeSDavid Ahern void fib6_rt_update(struct net *net, struct fib6_info *rt,
526419a3b7eeSDavid Ahern 		    struct nl_info *info)
526519a3b7eeSDavid Ahern {
526619a3b7eeSDavid Ahern 	u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
526719a3b7eeSDavid Ahern 	struct sk_buff *skb;
526819a3b7eeSDavid Ahern 	int err = -ENOBUFS;
526919a3b7eeSDavid Ahern 
527019a3b7eeSDavid Ahern 	/* call_fib6_entry_notifiers will be removed when in-kernel notifier
527119a3b7eeSDavid Ahern 	 * is implemented and supported for nexthop objects
527219a3b7eeSDavid Ahern 	 */
527319a3b7eeSDavid Ahern 	call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, rt, NULL);
527419a3b7eeSDavid Ahern 
527519a3b7eeSDavid Ahern 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
527619a3b7eeSDavid Ahern 	if (!skb)
527719a3b7eeSDavid Ahern 		goto errout;
527819a3b7eeSDavid Ahern 
527919a3b7eeSDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
528019a3b7eeSDavid Ahern 			    RTM_NEWROUTE, info->portid, seq, NLM_F_REPLACE);
528119a3b7eeSDavid Ahern 	if (err < 0) {
528219a3b7eeSDavid Ahern 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
528319a3b7eeSDavid Ahern 		WARN_ON(err == -EMSGSIZE);
528419a3b7eeSDavid Ahern 		kfree_skb(skb);
528519a3b7eeSDavid Ahern 		goto errout;
528619a3b7eeSDavid Ahern 	}
528719a3b7eeSDavid Ahern 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
528819a3b7eeSDavid Ahern 		    info->nlh, gfp_any());
528919a3b7eeSDavid Ahern 	return;
529019a3b7eeSDavid Ahern errout:
529119a3b7eeSDavid Ahern 	if (err < 0)
529219a3b7eeSDavid Ahern 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
529319a3b7eeSDavid Ahern }
529419a3b7eeSDavid Ahern 
52958ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
5296351638e7SJiri Pirko 				unsigned long event, void *ptr)
52978ed67789SDaniel Lezcano {
5298351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5299c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
53008ed67789SDaniel Lezcano 
5301242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
5302242d3a49SWANG Cong 		return NOTIFY_OK;
5303242d3a49SWANG Cong 
5304242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
53051cf844c7SDavid Ahern 		net->ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = dev;
5306d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
53078ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
53088ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5309d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
53108ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
5311d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
53128ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
53138ed67789SDaniel Lezcano #endif
531476da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
531576da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
531676da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
531776da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
531876da0704SWANG Cong 		 */
531912d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
5320242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
532112d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
532212d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
5323242d3a49SWANG Cong #endif
53248ed67789SDaniel Lezcano 	}
53258ed67789SDaniel Lezcano 
53268ed67789SDaniel Lezcano 	return NOTIFY_OK;
53278ed67789SDaniel Lezcano }
53288ed67789SDaniel Lezcano 
53291da177e4SLinus Torvalds /*
53301da177e4SLinus Torvalds  *	/proc
53311da177e4SLinus Torvalds  */
53321da177e4SLinus Torvalds 
53331da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
53341da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
53351da177e4SLinus Torvalds {
533669ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
53371da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
533869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
533969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
534081eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
534169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
534269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
5343fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
534469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
53451da177e4SLinus Torvalds 
53461da177e4SLinus Torvalds 	return 0;
53471da177e4SLinus Torvalds }
53481da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
53491da177e4SLinus Torvalds 
53501da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
53511da177e4SLinus Torvalds 
53521da177e4SLinus Torvalds static
5353fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
53541da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
53551da177e4SLinus Torvalds {
5356c486da34SLucian Adrian Grijincu 	struct net *net;
5357c486da34SLucian Adrian Grijincu 	int delay;
5358f0fb9b28SAditya Pakki 	int ret;
5359c486da34SLucian Adrian Grijincu 	if (!write)
5360c486da34SLucian Adrian Grijincu 		return -EINVAL;
5361c486da34SLucian Adrian Grijincu 
5362c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
5363c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
5364f0fb9b28SAditya Pakki 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5365f0fb9b28SAditya Pakki 	if (ret)
5366f0fb9b28SAditya Pakki 		return ret;
5367f0fb9b28SAditya Pakki 
53682ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
53691da177e4SLinus Torvalds 	return 0;
53701da177e4SLinus Torvalds }
53711da177e4SLinus Torvalds 
53727c6bb7d2SDavid Ahern static int zero;
53737c6bb7d2SDavid Ahern static int one = 1;
53747c6bb7d2SDavid Ahern 
5375ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = {
53761da177e4SLinus Torvalds 	{
53771da177e4SLinus Torvalds 		.procname	=	"flush",
53784990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
53791da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
538089c8b3a1SDave Jones 		.mode		=	0200,
53816d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
53821da177e4SLinus Torvalds 	},
53831da177e4SLinus Torvalds 	{
53841da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
53859a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
53861da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
53871da177e4SLinus Torvalds 		.mode		=	0644,
53886d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
53891da177e4SLinus Torvalds 	},
53901da177e4SLinus Torvalds 	{
53911da177e4SLinus Torvalds 		.procname	=	"max_size",
53924990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
53931da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
53941da177e4SLinus Torvalds 		.mode		=	0644,
53956d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
53961da177e4SLinus Torvalds 	},
53971da177e4SLinus Torvalds 	{
53981da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
53994990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
54001da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
54011da177e4SLinus Torvalds 		.mode		=	0644,
54026d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
54031da177e4SLinus Torvalds 	},
54041da177e4SLinus Torvalds 	{
54051da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
54064990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
54071da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
54081da177e4SLinus Torvalds 		.mode		=	0644,
54096d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
54101da177e4SLinus Torvalds 	},
54111da177e4SLinus Torvalds 	{
54121da177e4SLinus Torvalds 		.procname	=	"gc_interval",
54134990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
54141da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
54151da177e4SLinus Torvalds 		.mode		=	0644,
54166d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
54171da177e4SLinus Torvalds 	},
54181da177e4SLinus Torvalds 	{
54191da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
54204990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
54211da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
54221da177e4SLinus Torvalds 		.mode		=	0644,
5423f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
54241da177e4SLinus Torvalds 	},
54251da177e4SLinus Torvalds 	{
54261da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
54274990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
54281da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
54291da177e4SLinus Torvalds 		.mode		=	0644,
54306d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
54311da177e4SLinus Torvalds 	},
54321da177e4SLinus Torvalds 	{
54331da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
54344990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
54351da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
54361da177e4SLinus Torvalds 		.mode		=	0644,
5437f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
54381da177e4SLinus Torvalds 	},
54391da177e4SLinus Torvalds 	{
54401da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
54414990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
54421da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
54431da177e4SLinus Torvalds 		.mode		=	0644,
54446d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
54451da177e4SLinus Torvalds 	},
54467c6bb7d2SDavid Ahern 	{
54477c6bb7d2SDavid Ahern 		.procname	=	"skip_notify_on_dev_down",
54487c6bb7d2SDavid Ahern 		.data		=	&init_net.ipv6.sysctl.skip_notify_on_dev_down,
54497c6bb7d2SDavid Ahern 		.maxlen		=	sizeof(int),
54507c6bb7d2SDavid Ahern 		.mode		=	0644,
54517c6bb7d2SDavid Ahern 		.proc_handler	=	proc_dointvec,
54527c6bb7d2SDavid Ahern 		.extra1		=	&zero,
54537c6bb7d2SDavid Ahern 		.extra2		=	&one,
54547c6bb7d2SDavid Ahern 	},
5455f8572d8fSEric W. Biederman 	{ }
54561da177e4SLinus Torvalds };
54571da177e4SLinus Torvalds 
54582c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
5459760f2d01SDaniel Lezcano {
5460760f2d01SDaniel Lezcano 	struct ctl_table *table;
5461760f2d01SDaniel Lezcano 
5462760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
5463760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
5464760f2d01SDaniel Lezcano 			GFP_KERNEL);
54655ee09105SYOSHIFUJI Hideaki 
54665ee09105SYOSHIFUJI Hideaki 	if (table) {
54675ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
5468c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
546986393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
54705ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
54715ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
54725ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
54735ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
54745ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
54755ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
54765ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
54779c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
54787c6bb7d2SDavid Ahern 		table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
5479464dc801SEric W. Biederman 
5480464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
5481464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
5482464dc801SEric W. Biederman 			table[0].procname = NULL;
54835ee09105SYOSHIFUJI Hideaki 	}
54845ee09105SYOSHIFUJI Hideaki 
5485760f2d01SDaniel Lezcano 	return table;
5486760f2d01SDaniel Lezcano }
54871da177e4SLinus Torvalds #endif
54881da177e4SLinus Torvalds 
54892c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
5490cdb18761SDaniel Lezcano {
5491633d424bSPavel Emelyanov 	int ret = -ENOMEM;
54928ed67789SDaniel Lezcano 
549386393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
549486393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
5495f2fc6a54SBenjamin Thery 
5496fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5497fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
5498fc66f95cSEric Dumazet 
54991cf844c7SDavid Ahern 	net->ipv6.fib6_null_entry = fib6_info_alloc(GFP_KERNEL, true);
5500421842edSDavid Ahern 	if (!net->ipv6.fib6_null_entry)
5501421842edSDavid Ahern 		goto out_ip6_dst_entries;
55021cf844c7SDavid Ahern 	memcpy(net->ipv6.fib6_null_entry, &fib6_null_entry_template,
55031cf844c7SDavid Ahern 	       sizeof(*net->ipv6.fib6_null_entry));
5504421842edSDavid Ahern 
55058ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
55068ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
55078ed67789SDaniel Lezcano 					   GFP_KERNEL);
55088ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
5509421842edSDavid Ahern 		goto out_fib6_null_entry;
5510d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
551162fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
551262fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
55138ed67789SDaniel Lezcano 
55148ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5515feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
55168ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
55178ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
55188ed67789SDaniel Lezcano 					       GFP_KERNEL);
551968fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
552068fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
5521d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
552262fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
552362fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
55248ed67789SDaniel Lezcano 
55258ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
55268ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
55278ed67789SDaniel Lezcano 					       GFP_KERNEL);
552868fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
552968fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
5530d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
553162fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
553262fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
55338ed67789SDaniel Lezcano #endif
55348ed67789SDaniel Lezcano 
5535b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
5536b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
5537b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5538b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5539b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5540b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5541b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5542b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
55437c6bb7d2SDavid Ahern 	net->ipv6.sysctl.skip_notify_on_dev_down = 0;
5544b339a47cSPeter Zijlstra 
55456891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
55466891a346SBenjamin Thery 
55478ed67789SDaniel Lezcano 	ret = 0;
55488ed67789SDaniel Lezcano out:
55498ed67789SDaniel Lezcano 	return ret;
5550f2fc6a54SBenjamin Thery 
555168fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
555268fffc67SPeter Zijlstra out_ip6_prohibit_entry:
555368fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
555468fffc67SPeter Zijlstra out_ip6_null_entry:
555568fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
555668fffc67SPeter Zijlstra #endif
5557421842edSDavid Ahern out_fib6_null_entry:
5558421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
5559fc66f95cSEric Dumazet out_ip6_dst_entries:
5560fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5561f2fc6a54SBenjamin Thery out_ip6_dst_ops:
5562f2fc6a54SBenjamin Thery 	goto out;
5563cdb18761SDaniel Lezcano }
5564cdb18761SDaniel Lezcano 
55652c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
5566cdb18761SDaniel Lezcano {
5567421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
55688ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
55698ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
55708ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
55718ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
55728ed67789SDaniel Lezcano #endif
557341bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5574cdb18761SDaniel Lezcano }
5575cdb18761SDaniel Lezcano 
5576d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
5577d189634eSThomas Graf {
5578d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5579c3506372SChristoph Hellwig 	proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
5580c3506372SChristoph Hellwig 			sizeof(struct ipv6_route_iter));
55813617d949SChristoph Hellwig 	proc_create_net_single("rt6_stats", 0444, net->proc_net,
55823617d949SChristoph Hellwig 			rt6_stats_seq_show, NULL);
5583d189634eSThomas Graf #endif
5584d189634eSThomas Graf 	return 0;
5585d189634eSThomas Graf }
5586d189634eSThomas Graf 
5587d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
5588d189634eSThomas Graf {
5589d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5590ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
5591ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
5592d189634eSThomas Graf #endif
5593d189634eSThomas Graf }
5594d189634eSThomas Graf 
5595cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
5596cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
5597cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
5598cdb18761SDaniel Lezcano };
5599cdb18761SDaniel Lezcano 
5600c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
5601c3426b47SDavid S. Miller {
5602c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5603c3426b47SDavid S. Miller 
5604c3426b47SDavid S. Miller 	if (!bp)
5605c3426b47SDavid S. Miller 		return -ENOMEM;
5606c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
5607c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
5608c3426b47SDavid S. Miller 	return 0;
5609c3426b47SDavid S. Miller }
5610c3426b47SDavid S. Miller 
5611c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
5612c3426b47SDavid S. Miller {
5613c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
5614c3426b47SDavid S. Miller 
5615c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
561656a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
5617c3426b47SDavid S. Miller 	kfree(bp);
5618c3426b47SDavid S. Miller }
5619c3426b47SDavid S. Miller 
56202b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
5621c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
5622c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
5623c3426b47SDavid S. Miller };
5624c3426b47SDavid S. Miller 
5625d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
5626d189634eSThomas Graf 	.init = ip6_route_net_init_late,
5627d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
5628d189634eSThomas Graf };
5629d189634eSThomas Graf 
56308ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
56318ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
5632242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
56338ed67789SDaniel Lezcano };
56348ed67789SDaniel Lezcano 
56352f460933SWANG Cong void __init ip6_route_init_special_entries(void)
56362f460933SWANG Cong {
56372f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
56382f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
56392f460933SWANG Cong 	 * manually for init_net */
56401cf844c7SDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_nh->fib_nh_dev = init_net.loopback_dev;
56412f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
56422f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
56432f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
56442f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
56452f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
56462f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
56472f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
56482f460933SWANG Cong   #endif
56492f460933SWANG Cong }
56502f460933SWANG Cong 
5651433d49c3SDaniel Lezcano int __init ip6_route_init(void)
56521da177e4SLinus Torvalds {
5653433d49c3SDaniel Lezcano 	int ret;
56548d0b94afSMartin KaFai Lau 	int cpu;
5655433d49c3SDaniel Lezcano 
56569a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
56579a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
56589a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
56599a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
56609a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
5661c19a28e1SFernando Carrijo 		goto out;
566214e50e57SDavid S. Miller 
5663fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
56648ed67789SDaniel Lezcano 	if (ret)
5665bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
5666bdb3289fSDaniel Lezcano 
5667c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5668c3426b47SDavid S. Miller 	if (ret)
5669e8803b6cSDavid S. Miller 		goto out_dst_entries;
56702a0c451aSThomas Graf 
56717e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
56727e52b33bSDavid S. Miller 	if (ret)
56737e52b33bSDavid S. Miller 		goto out_register_inetpeer;
5674c3426b47SDavid S. Miller 
56755dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
56765dc121e9SArnaud Ebalard 
5677e8803b6cSDavid S. Miller 	ret = fib6_init();
5678433d49c3SDaniel Lezcano 	if (ret)
56798ed67789SDaniel Lezcano 		goto out_register_subsys;
5680433d49c3SDaniel Lezcano 
5681433d49c3SDaniel Lezcano 	ret = xfrm6_init();
5682433d49c3SDaniel Lezcano 	if (ret)
5683e8803b6cSDavid S. Miller 		goto out_fib6_init;
5684c35b7e72SDaniel Lezcano 
5685433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
5686433d49c3SDaniel Lezcano 	if (ret)
5687433d49c3SDaniel Lezcano 		goto xfrm6_init;
56887e5449c2SDaniel Lezcano 
5689d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
5690d189634eSThomas Graf 	if (ret)
5691d189634eSThomas Graf 		goto fib6_rules_init;
5692d189634eSThomas Graf 
569316feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
569416feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
569516feebcfSFlorian Westphal 	if (ret < 0)
569616feebcfSFlorian Westphal 		goto out_register_late_subsys;
569716feebcfSFlorian Westphal 
569816feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
569916feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
570016feebcfSFlorian Westphal 	if (ret < 0)
570116feebcfSFlorian Westphal 		goto out_register_late_subsys;
570216feebcfSFlorian Westphal 
570316feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
570416feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
570516feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
570616feebcfSFlorian Westphal 	if (ret < 0)
5707d189634eSThomas Graf 		goto out_register_late_subsys;
5708433d49c3SDaniel Lezcano 
57098ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
5710cdb18761SDaniel Lezcano 	if (ret)
5711d189634eSThomas Graf 		goto out_register_late_subsys;
57128ed67789SDaniel Lezcano 
57138d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
57148d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
57158d0b94afSMartin KaFai Lau 
57168d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
57178d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
57188d0b94afSMartin KaFai Lau 	}
57198d0b94afSMartin KaFai Lau 
5720433d49c3SDaniel Lezcano out:
5721433d49c3SDaniel Lezcano 	return ret;
5722433d49c3SDaniel Lezcano 
5723d189634eSThomas Graf out_register_late_subsys:
572416feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
5725d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5726433d49c3SDaniel Lezcano fib6_rules_init:
5727433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
5728433d49c3SDaniel Lezcano xfrm6_init:
5729433d49c3SDaniel Lezcano 	xfrm6_fini();
57302a0c451aSThomas Graf out_fib6_init:
57312a0c451aSThomas Graf 	fib6_gc_cleanup();
57328ed67789SDaniel Lezcano out_register_subsys:
57338ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
57347e52b33bSDavid S. Miller out_register_inetpeer:
57357e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
5736fc66f95cSEric Dumazet out_dst_entries:
5737fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5738433d49c3SDaniel Lezcano out_kmem_cache:
5739f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
5740433d49c3SDaniel Lezcano 	goto out;
57411da177e4SLinus Torvalds }
57421da177e4SLinus Torvalds 
57431da177e4SLinus Torvalds void ip6_route_cleanup(void)
57441da177e4SLinus Torvalds {
57458ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
5746d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5747101367c2SThomas Graf 	fib6_rules_cleanup();
57481da177e4SLinus Torvalds 	xfrm6_fini();
57491da177e4SLinus Torvalds 	fib6_gc_cleanup();
5750c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
57518ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
575241bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5753f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
57541da177e4SLinus Torvalds }
5755