xref: /openbmc/linux/net/ipv6/route.c (revision 360a9887c8c01a715b2b4b131f7c7462f7cce576)
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>
6251ebd318SNicolas Dichtel #include <net/nexthop.h>
6319e42e45SRoopa Prabhu #include <net/lwtunnel.h>
64904af04dSJiri Benc #include <net/ip_tunnels.h>
65ca254490SDavid Ahern #include <net/l3mdev.h>
66b811580dSDavid Ahern #include <trace/events/fib6.h>
671da177e4SLinus Torvalds 
687c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
711da177e4SLinus Torvalds #include <linux/sysctl.h>
721da177e4SLinus Torvalds #endif
731da177e4SLinus Torvalds 
74afc154e9SHannes Frederic Sowa enum rt6_nud_state {
757e980569SJiri Benc 	RT6_NUD_FAIL_HARD = -3,
767e980569SJiri Benc 	RT6_NUD_FAIL_PROBE = -2,
777e980569SJiri Benc 	RT6_NUD_FAIL_DO_RR = -1,
78afc154e9SHannes Frederic Sowa 	RT6_NUD_SUCCEED = 1
79afc154e9SHannes Frederic Sowa };
80afc154e9SHannes Frederic Sowa 
811da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
820dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
83ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
841da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
851da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
861da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
871da177e4SLinus Torvalds 				       struct net_device *dev, int how);
88569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
91ede2059dSEric W. Biederman static int		ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
927150aedeSKamala R static int		ip6_pkt_prohibit(struct sk_buff *skb);
93ede2059dSEric W. Biederman static int		ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
941da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
956700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
966700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
976700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
986700c270SDavid S. Miller 					struct sk_buff *skb);
998d1c802bSDavid Ahern static int rt6_score_route(struct fib6_info *rt, int oif, int strict);
1008d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt);
101d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
1028d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
103d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
10416a16cd3SDavid Ahern 			 int iif, int type, u32 portid, u32 seq,
10516a16cd3SDavid Ahern 			 unsigned int flags);
1068d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
10735732d01SWei Wang 					   struct in6_addr *daddr,
10835732d01SWei Wang 					   struct in6_addr *saddr);
1091da177e4SLinus Torvalds 
11070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1118d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
112b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
113830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
114830218c1SDavid Ahern 					   struct net_device *dev,
11595c96174SEric Dumazet 					   unsigned int pref);
1168d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
117b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
118830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
119830218c1SDavid Ahern 					   struct net_device *dev);
12070ceb4f5SYOSHIFUJI Hideaki #endif
12170ceb4f5SYOSHIFUJI Hideaki 
1228d0b94afSMartin KaFai Lau struct uncached_list {
1238d0b94afSMartin KaFai Lau 	spinlock_t		lock;
1248d0b94afSMartin KaFai Lau 	struct list_head	head;
1258d0b94afSMartin KaFai Lau };
1268d0b94afSMartin KaFai Lau 
1278d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
1288d0b94afSMartin KaFai Lau 
129510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt)
1308d0b94afSMartin KaFai Lau {
1318d0b94afSMartin KaFai Lau 	struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
1328d0b94afSMartin KaFai Lau 
1338d0b94afSMartin KaFai Lau 	rt->rt6i_uncached_list = ul;
1348d0b94afSMartin KaFai Lau 
1358d0b94afSMartin KaFai Lau 	spin_lock_bh(&ul->lock);
1368d0b94afSMartin KaFai Lau 	list_add_tail(&rt->rt6i_uncached, &ul->head);
1378d0b94afSMartin KaFai Lau 	spin_unlock_bh(&ul->lock);
1388d0b94afSMartin KaFai Lau }
1398d0b94afSMartin KaFai Lau 
140510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt)
1418d0b94afSMartin KaFai Lau {
1428d0b94afSMartin KaFai Lau 	if (!list_empty(&rt->rt6i_uncached)) {
1438d0b94afSMartin KaFai Lau 		struct uncached_list *ul = rt->rt6i_uncached_list;
14481eb8447SWei Wang 		struct net *net = dev_net(rt->dst.dev);
1458d0b94afSMartin KaFai Lau 
1468d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1478d0b94afSMartin KaFai Lau 		list_del(&rt->rt6i_uncached);
14881eb8447SWei Wang 		atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
1498d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1508d0b94afSMartin KaFai Lau 	}
1518d0b94afSMartin KaFai Lau }
1528d0b94afSMartin KaFai Lau 
1538d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
1548d0b94afSMartin KaFai Lau {
1558d0b94afSMartin KaFai Lau 	struct net_device *loopback_dev = net->loopback_dev;
1568d0b94afSMartin KaFai Lau 	int cpu;
1578d0b94afSMartin KaFai Lau 
158e332bc67SEric W. Biederman 	if (dev == loopback_dev)
159e332bc67SEric W. Biederman 		return;
160e332bc67SEric W. Biederman 
1618d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
1628d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
1638d0b94afSMartin KaFai Lau 		struct rt6_info *rt;
1648d0b94afSMartin KaFai Lau 
1658d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1668d0b94afSMartin KaFai Lau 		list_for_each_entry(rt, &ul->head, rt6i_uncached) {
1678d0b94afSMartin KaFai Lau 			struct inet6_dev *rt_idev = rt->rt6i_idev;
1688d0b94afSMartin KaFai Lau 			struct net_device *rt_dev = rt->dst.dev;
1698d0b94afSMartin KaFai Lau 
170e332bc67SEric W. Biederman 			if (rt_idev->dev == dev) {
1718d0b94afSMartin KaFai Lau 				rt->rt6i_idev = in6_dev_get(loopback_dev);
1728d0b94afSMartin KaFai Lau 				in6_dev_put(rt_idev);
1738d0b94afSMartin KaFai Lau 			}
1748d0b94afSMartin KaFai Lau 
175e332bc67SEric W. Biederman 			if (rt_dev == dev) {
1768d0b94afSMartin KaFai Lau 				rt->dst.dev = loopback_dev;
1778d0b94afSMartin KaFai Lau 				dev_hold(rt->dst.dev);
1788d0b94afSMartin KaFai Lau 				dev_put(rt_dev);
1798d0b94afSMartin KaFai Lau 			}
1808d0b94afSMartin KaFai Lau 		}
1818d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1828d0b94afSMartin KaFai Lau 	}
1838d0b94afSMartin KaFai Lau }
1848d0b94afSMartin KaFai Lau 
185f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p,
186f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
187f894cbf8SDavid S. Miller 					     const void *daddr)
18839232973SDavid S. Miller {
189a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
19039232973SDavid S. Miller 		return (const void *) p;
191f894cbf8SDavid S. Miller 	else if (skb)
192f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
19339232973SDavid S. Miller 	return daddr;
19439232973SDavid S. Miller }
19539232973SDavid S. Miller 
196f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
197f8a1b43bSDavid Ahern 				   struct net_device *dev,
198f894cbf8SDavid S. Miller 				   struct sk_buff *skb,
199f894cbf8SDavid S. Miller 				   const void *daddr)
200d3aaeb38SDavid S. Miller {
20139232973SDavid S. Miller 	struct neighbour *n;
20239232973SDavid S. Miller 
203f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(gw, skb, daddr);
204f8a1b43bSDavid Ahern 	n = __ipv6_neigh_lookup(dev, daddr);
205f83c7790SDavid S. Miller 	if (n)
206f83c7790SDavid S. Miller 		return n;
207f8a1b43bSDavid Ahern 	return neigh_create(&nd_tbl, daddr, dev);
208f8a1b43bSDavid Ahern }
209f8a1b43bSDavid Ahern 
210f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
211f8a1b43bSDavid Ahern 					      struct sk_buff *skb,
212f8a1b43bSDavid Ahern 					      const void *daddr)
213f8a1b43bSDavid Ahern {
214f8a1b43bSDavid Ahern 	const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
215f8a1b43bSDavid Ahern 
216f8a1b43bSDavid Ahern 	return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr);
217f83c7790SDavid S. Miller }
218f83c7790SDavid S. Miller 
21963fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
22063fca65dSJulian Anastasov {
22163fca65dSJulian Anastasov 	struct net_device *dev = dst->dev;
22263fca65dSJulian Anastasov 	struct rt6_info *rt = (struct rt6_info *)dst;
22363fca65dSJulian Anastasov 
224f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
22563fca65dSJulian Anastasov 	if (!daddr)
22663fca65dSJulian Anastasov 		return;
22763fca65dSJulian Anastasov 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
22863fca65dSJulian Anastasov 		return;
22963fca65dSJulian Anastasov 	if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
23063fca65dSJulian Anastasov 		return;
23163fca65dSJulian Anastasov 	__ipv6_confirm_neigh(dev, daddr);
23263fca65dSJulian Anastasov }
23363fca65dSJulian Anastasov 
2349a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
2351da177e4SLinus Torvalds 	.family			=	AF_INET6,
2361da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
2371da177e4SLinus Torvalds 	.gc_thresh		=	1024,
2381da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
2390dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
240ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
241d4ead6b3SDavid Ahern 	.cow_metrics		=	dst_cow_metrics_generic,
2421da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
2431da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
2441da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
2451da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
2461da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
2476e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
2489f8955ccSEric W. Biederman 	.local_out		=	__ip6_local_out,
249f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
25063fca65dSJulian Anastasov 	.confirm_neigh		=	ip6_confirm_neigh,
2511da177e4SLinus Torvalds };
2521da177e4SLinus Torvalds 
253ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
254ec831ea7SRoland Dreier {
255618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
256618f9bc7SSteffen Klassert 
257618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
258ec831ea7SRoland Dreier }
259ec831ea7SRoland Dreier 
2606700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
2616700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
26214e50e57SDavid S. Miller {
26314e50e57SDavid S. Miller }
26414e50e57SDavid S. Miller 
2656700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
2666700c270SDavid S. Miller 				      struct sk_buff *skb)
267b587ee3bSDavid S. Miller {
268b587ee3bSDavid S. Miller }
269b587ee3bSDavid S. Miller 
27014e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
27114e50e57SDavid S. Miller 	.family			=	AF_INET6,
27214e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
27314e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
274ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
275214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
27614e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
277b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2780a1f5962SMartin KaFai Lau 	.cow_metrics		=	dst_cow_metrics_generic,
279f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
28014e50e57SDavid S. Miller };
28114e50e57SDavid S. Miller 
28262fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
28314edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
28462fa8a84SDavid S. Miller };
28562fa8a84SDavid S. Miller 
2868d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = {
28793c2fb25SDavid Ahern 	.fib6_flags	= (RTF_REJECT | RTF_NONEXTHOP),
28893c2fb25SDavid Ahern 	.fib6_protocol  = RTPROT_KERNEL,
28993c2fb25SDavid Ahern 	.fib6_metric	= ~(u32)0,
29093c2fb25SDavid Ahern 	.fib6_ref	= ATOMIC_INIT(1),
291421842edSDavid Ahern 	.fib6_type	= RTN_UNREACHABLE,
292421842edSDavid Ahern 	.fib6_metrics	= (struct dst_metrics *)&dst_default_metrics,
293421842edSDavid Ahern };
294421842edSDavid Ahern 
295fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
2961da177e4SLinus Torvalds 	.dst = {
2971da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2981da177e4SLinus Torvalds 		.__use		= 1,
2992c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
3001da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
3011da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
3021da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
3031da177e4SLinus Torvalds 	},
3041da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3051da177e4SLinus Torvalds };
3061da177e4SLinus Torvalds 
307101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
308101367c2SThomas Graf 
309fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
310101367c2SThomas Graf 	.dst = {
311101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
312101367c2SThomas Graf 		.__use		= 1,
3132c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
314101367c2SThomas Graf 		.error		= -EACCES,
3159ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
3169ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
317101367c2SThomas Graf 	},
318101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
319101367c2SThomas Graf };
320101367c2SThomas Graf 
321fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
322101367c2SThomas Graf 	.dst = {
323101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
324101367c2SThomas Graf 		.__use		= 1,
3252c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
326101367c2SThomas Graf 		.error		= -EINVAL,
327352e512cSHerbert Xu 		.input		= dst_discard,
328ede2059dSEric W. Biederman 		.output		= dst_discard_out,
329101367c2SThomas Graf 	},
330101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
331101367c2SThomas Graf };
332101367c2SThomas Graf 
333101367c2SThomas Graf #endif
334101367c2SThomas Graf 
335ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt)
336ebfa45f0SMartin KaFai Lau {
337ebfa45f0SMartin KaFai Lau 	struct dst_entry *dst = &rt->dst;
338ebfa45f0SMartin KaFai Lau 
339ebfa45f0SMartin KaFai Lau 	memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
340ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_uncached);
341ebfa45f0SMartin KaFai Lau }
342ebfa45f0SMartin KaFai Lau 
3431da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
34493531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
345ad706862SMartin KaFai Lau 			       int flags)
3461da177e4SLinus Torvalds {
34797bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
348b2a9c0edSWei Wang 					1, DST_OBSOLETE_FORCE_CHK, flags);
349cf911662SDavid S. Miller 
35081eb8447SWei Wang 	if (rt) {
351ebfa45f0SMartin KaFai Lau 		rt6_info_init(rt);
35281eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
35381eb8447SWei Wang 	}
3548104891bSSteffen Klassert 
355cf911662SDavid S. Miller 	return rt;
3561da177e4SLinus Torvalds }
3579ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc);
358d52d3997SMartin KaFai Lau 
3591da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3601da177e4SLinus Torvalds {
3611da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3628d1c802bSDavid Ahern 	struct fib6_info *from = rt->from;
3638d0b94afSMartin KaFai Lau 	struct inet6_dev *idev;
3641da177e4SLinus Torvalds 
3658e2ec639SYan, Zheng 	dst_destroy_metrics_generic(dst);
3668d0b94afSMartin KaFai Lau 	rt6_uncached_list_del(rt);
3678d0b94afSMartin KaFai Lau 
3688d0b94afSMartin KaFai Lau 	idev = rt->rt6i_idev;
36938308473SDavid S. Miller 	if (idev) {
3701da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3711da177e4SLinus Torvalds 		in6_dev_put(idev);
3721da177e4SLinus Torvalds 	}
373d4ead6b3SDavid Ahern 
3743a2232e9SDavid Miller 	rt->from = NULL;
37593531c67SDavid Ahern 	fib6_info_release(from);
376b3419363SDavid S. Miller }
377b3419363SDavid S. Miller 
3781da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3791da177e4SLinus Torvalds 			   int how)
3801da177e4SLinus Torvalds {
3811da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3821da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3835a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
384c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3851da177e4SLinus Torvalds 
386e5645f51SWei Wang 	if (idev && idev->dev != loopback_dev) {
387e5645f51SWei Wang 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
38838308473SDavid S. Miller 		if (loopback_idev) {
3891da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
3901da177e4SLinus Torvalds 			in6_dev_put(idev);
3911da177e4SLinus Torvalds 		}
3921da177e4SLinus Torvalds 	}
39397cac082SDavid S. Miller }
3941da177e4SLinus Torvalds 
3955973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt)
3965973fb1eSMartin KaFai Lau {
3975973fb1eSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_EXPIRES)
3985973fb1eSMartin KaFai Lau 		return time_after(jiffies, rt->dst.expires);
3995973fb1eSMartin KaFai Lau 	else
4005973fb1eSMartin KaFai Lau 		return false;
4015973fb1eSMartin KaFai Lau }
4025973fb1eSMartin KaFai Lau 
403a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
4041da177e4SLinus Torvalds {
4051716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
4061716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
407a50feda5SEric Dumazet 			return true;
4083a2232e9SDavid Miller 	} else if (rt->from) {
4091e2ea8adSXin Long 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
41014895687SDavid Ahern 			fib6_check_expired(rt->from);
4111716a961SGao feng 	}
412a50feda5SEric Dumazet 	return false;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
4158d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_select(const struct net *net,
4168d1c802bSDavid Ahern 					      struct fib6_info *match,
41752bd4c0cSNicolas Dichtel 					     struct flowi6 *fl6, int oif,
418b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
41952bd4c0cSNicolas Dichtel 					     int strict)
42051ebd318SNicolas Dichtel {
4218d1c802bSDavid Ahern 	struct fib6_info *sibling, *next_sibling;
42251ebd318SNicolas Dichtel 
423b673d6ccSJakub Sitnicki 	/* We might have already computed the hash for ICMPv6 errors. In such
424b673d6ccSJakub Sitnicki 	 * case it will always be non-zero. Otherwise now is the time to do it.
425b673d6ccSJakub Sitnicki 	 */
426b673d6ccSJakub Sitnicki 	if (!fl6->mp_hash)
427b4bac172SDavid Ahern 		fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
428b673d6ccSJakub Sitnicki 
4295e670d84SDavid Ahern 	if (fl6->mp_hash <= atomic_read(&match->fib6_nh.nh_upper_bound))
4303d709f69SIdo Schimmel 		return match;
431bbfcd776SIdo Schimmel 
43293c2fb25SDavid Ahern 	list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
43393c2fb25SDavid Ahern 				 fib6_siblings) {
4345e670d84SDavid Ahern 		int nh_upper_bound;
4355e670d84SDavid Ahern 
4365e670d84SDavid Ahern 		nh_upper_bound = atomic_read(&sibling->fib6_nh.nh_upper_bound);
4375e670d84SDavid Ahern 		if (fl6->mp_hash > nh_upper_bound)
4383d709f69SIdo Schimmel 			continue;
43952bd4c0cSNicolas Dichtel 		if (rt6_score_route(sibling, oif, strict) < 0)
44052bd4c0cSNicolas Dichtel 			break;
44151ebd318SNicolas Dichtel 		match = sibling;
44251ebd318SNicolas Dichtel 		break;
44351ebd318SNicolas Dichtel 	}
4443d709f69SIdo Schimmel 
44551ebd318SNicolas Dichtel 	return match;
44651ebd318SNicolas Dichtel }
44751ebd318SNicolas Dichtel 
4481da177e4SLinus Torvalds /*
44966f5d6ceSWei Wang  *	Route lookup. rcu_read_lock() should be held.
4501da177e4SLinus Torvalds  */
4511da177e4SLinus Torvalds 
4528d1c802bSDavid Ahern static inline struct fib6_info *rt6_device_match(struct net *net,
4538d1c802bSDavid Ahern 						 struct fib6_info *rt,
454b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4551da177e4SLinus Torvalds 						    int oif,
456d420895eSYOSHIFUJI Hideaki 						    int flags)
4571da177e4SLinus Torvalds {
4588d1c802bSDavid Ahern 	struct fib6_info *local = NULL;
4598d1c802bSDavid Ahern 	struct fib6_info *sprt;
4601da177e4SLinus Torvalds 
4615e670d84SDavid Ahern 	if (!oif && ipv6_addr_any(saddr) &&
4625e670d84SDavid Ahern 	    !(rt->fib6_nh.nh_flags & RTNH_F_DEAD))
4638067bb8cSIdo Schimmel 		return rt;
464dd3abc4eSYOSHIFUJI Hideaki 
465071fb37eSDavid Miller 	for (sprt = rt; sprt; sprt = rcu_dereference(sprt->rt6_next)) {
4665e670d84SDavid Ahern 		const struct net_device *dev = sprt->fib6_nh.nh_dev;
467dd3abc4eSYOSHIFUJI Hideaki 
4685e670d84SDavid Ahern 		if (sprt->fib6_nh.nh_flags & RTNH_F_DEAD)
4698067bb8cSIdo Schimmel 			continue;
4708067bb8cSIdo Schimmel 
471dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4721da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4731da177e4SLinus Torvalds 				return sprt;
4741da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
47593c2fb25SDavid Ahern 				if (!sprt->fib6_idev ||
47693c2fb25SDavid Ahern 				    sprt->fib6_idev->dev->ifindex != oif) {
47717fb0b2bSDavid Ahern 					if (flags & RT6_LOOKUP_F_IFACE)
4781da177e4SLinus Torvalds 						continue;
47917fb0b2bSDavid Ahern 					if (local &&
48093c2fb25SDavid Ahern 					    local->fib6_idev->dev->ifindex == oif)
4811da177e4SLinus Torvalds 						continue;
4821da177e4SLinus Torvalds 				}
4831da177e4SLinus Torvalds 				local = sprt;
4841da177e4SLinus Torvalds 			}
485dd3abc4eSYOSHIFUJI Hideaki 		} else {
486dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
487dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
488dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
489dd3abc4eSYOSHIFUJI Hideaki 		}
4901da177e4SLinus Torvalds 	}
4911da177e4SLinus Torvalds 
492dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4931da177e4SLinus Torvalds 		if (local)
4941da177e4SLinus Torvalds 			return local;
4951da177e4SLinus Torvalds 
496d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
497421842edSDavid Ahern 			return net->ipv6.fib6_null_entry;
4981da177e4SLinus Torvalds 	}
4998067bb8cSIdo Schimmel 
500421842edSDavid Ahern 	return rt->fib6_nh.nh_flags & RTNH_F_DEAD ? net->ipv6.fib6_null_entry : rt;
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds 
50327097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
504c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
505c2f17e82SHannes Frederic Sowa 	struct work_struct work;
506c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
507c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
508c2f17e82SHannes Frederic Sowa };
509c2f17e82SHannes Frederic Sowa 
510c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
511c2f17e82SHannes Frederic Sowa {
512c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
513c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
514c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
515c2f17e82SHannes Frederic Sowa 
516c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
517adc176c5SErik Nordmark 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
518c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
519662f5533SMichael Büsch 	kfree(work);
520c2f17e82SHannes Frederic Sowa }
521c2f17e82SHannes Frederic Sowa 
5228d1c802bSDavid Ahern static void rt6_probe(struct fib6_info *rt)
52327097255SYOSHIFUJI Hideaki {
524990edb42SMartin KaFai Lau 	struct __rt6_probe_work *work;
5255e670d84SDavid Ahern 	const struct in6_addr *nh_gw;
526f2c31e32SEric Dumazet 	struct neighbour *neigh;
5275e670d84SDavid Ahern 	struct net_device *dev;
5285e670d84SDavid Ahern 
52927097255SYOSHIFUJI Hideaki 	/*
53027097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
53127097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
53227097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
53327097255SYOSHIFUJI Hideaki 	 *
53427097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
53527097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
53627097255SYOSHIFUJI Hideaki 	 */
53793c2fb25SDavid Ahern 	if (!rt || !(rt->fib6_flags & RTF_GATEWAY))
538fdd6681dSAmerigo Wang 		return;
5395e670d84SDavid Ahern 
5405e670d84SDavid Ahern 	nh_gw = &rt->fib6_nh.nh_gw;
5415e670d84SDavid Ahern 	dev = rt->fib6_nh.nh_dev;
5422152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
5435e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
5442152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
5458d6c31bfSMartin KaFai Lau 		if (neigh->nud_state & NUD_VALID)
5468d6c31bfSMartin KaFai Lau 			goto out;
5478d6c31bfSMartin KaFai Lau 
548990edb42SMartin KaFai Lau 		work = NULL;
5492152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
550990edb42SMartin KaFai Lau 		if (!(neigh->nud_state & NUD_VALID) &&
551990edb42SMartin KaFai Lau 		    time_after(jiffies,
552990edb42SMartin KaFai Lau 			       neigh->updated +
55393c2fb25SDavid Ahern 			       rt->fib6_idev->cnf.rtr_probe_interval)) {
554c2f17e82SHannes Frederic Sowa 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
555990edb42SMartin KaFai Lau 			if (work)
5567e980569SJiri Benc 				__neigh_set_probe_once(neigh);
557990edb42SMartin KaFai Lau 		}
558c2f17e82SHannes Frederic Sowa 		write_unlock(&neigh->lock);
559990edb42SMartin KaFai Lau 	} else {
560990edb42SMartin KaFai Lau 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
561990edb42SMartin KaFai Lau 	}
562c2f17e82SHannes Frederic Sowa 
563c2f17e82SHannes Frederic Sowa 	if (work) {
564c2f17e82SHannes Frederic Sowa 		INIT_WORK(&work->work, rt6_probe_deferred);
5655e670d84SDavid Ahern 		work->target = *nh_gw;
5665e670d84SDavid Ahern 		dev_hold(dev);
5675e670d84SDavid Ahern 		work->dev = dev;
568c2f17e82SHannes Frederic Sowa 		schedule_work(&work->work);
569c2f17e82SHannes Frederic Sowa 	}
570990edb42SMartin KaFai Lau 
5718d6c31bfSMartin KaFai Lau out:
5722152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
573f2c31e32SEric Dumazet }
57427097255SYOSHIFUJI Hideaki #else
5758d1c802bSDavid Ahern static inline void rt6_probe(struct fib6_info *rt)
57627097255SYOSHIFUJI Hideaki {
57727097255SYOSHIFUJI Hideaki }
57827097255SYOSHIFUJI Hideaki #endif
57927097255SYOSHIFUJI Hideaki 
5801da177e4SLinus Torvalds /*
581554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5821da177e4SLinus Torvalds  */
5838d1c802bSDavid Ahern static inline int rt6_check_dev(struct fib6_info *rt, int oif)
5841da177e4SLinus Torvalds {
5855e670d84SDavid Ahern 	const struct net_device *dev = rt->fib6_nh.nh_dev;
5865e670d84SDavid Ahern 
587161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
588554cfb7eSYOSHIFUJI Hideaki 		return 2;
589161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
59093c2fb25SDavid Ahern 	    rt->fib6_idev && rt->fib6_idev->dev->ifindex == oif)
591161980f4SDavid S. Miller 		return 1;
592554cfb7eSYOSHIFUJI Hideaki 	return 0;
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
5958d1c802bSDavid Ahern static inline enum rt6_nud_state rt6_check_neigh(struct fib6_info *rt)
5961da177e4SLinus Torvalds {
597afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
5985e670d84SDavid Ahern 	struct neighbour *neigh;
599f2c31e32SEric Dumazet 
60093c2fb25SDavid Ahern 	if (rt->fib6_flags & RTF_NONEXTHOP ||
60193c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_GATEWAY))
602afc154e9SHannes Frederic Sowa 		return RT6_NUD_SUCCEED;
603145a3621SYOSHIFUJI Hideaki / 吉藤英明 
604145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
6055e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(rt->fib6_nh.nh_dev,
6065e670d84SDavid Ahern 					  &rt->fib6_nh.nh_gw);
607145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
608145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
609554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
610afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
611398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
612a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
613afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
6147e980569SJiri Benc 		else
6157e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
616398bcbebSYOSHIFUJI Hideaki #endif
617145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
618afc154e9SHannes Frederic Sowa 	} else {
619afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
6207e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
621a5a81f0bSPaul Marks 	}
622145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
623145a3621SYOSHIFUJI Hideaki / 吉藤英明 
624a5a81f0bSPaul Marks 	return ret;
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds 
6278d1c802bSDavid Ahern static int rt6_score_route(struct fib6_info *rt, int oif, int strict)
628554cfb7eSYOSHIFUJI Hideaki {
629a5a81f0bSPaul Marks 	int m;
6304d0c5911SYOSHIFUJI Hideaki 
6314d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
63277d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
633afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
634ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
63593c2fb25SDavid Ahern 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->fib6_flags)) << 2;
636ebacaaa0SYOSHIFUJI Hideaki #endif
637afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE) {
638afc154e9SHannes Frederic Sowa 		int n = rt6_check_neigh(rt);
639afc154e9SHannes Frederic Sowa 		if (n < 0)
640afc154e9SHannes Frederic Sowa 			return n;
641afc154e9SHannes Frederic Sowa 	}
642554cfb7eSYOSHIFUJI Hideaki 	return m;
643554cfb7eSYOSHIFUJI Hideaki }
644554cfb7eSYOSHIFUJI Hideaki 
6458d1c802bSDavid Ahern static struct fib6_info *find_match(struct fib6_info *rt, int oif, int strict,
6468d1c802bSDavid Ahern 				   int *mpri, struct fib6_info *match,
647afc154e9SHannes Frederic Sowa 				   bool *do_rr)
648554cfb7eSYOSHIFUJI Hideaki {
649554cfb7eSYOSHIFUJI Hideaki 	int m;
650afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
65193c2fb25SDavid Ahern 	struct inet6_dev *idev = rt->fib6_idev;
65235103d11SAndy Gospodarek 
6535e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
6548067bb8cSIdo Schimmel 		goto out;
6558067bb8cSIdo Schimmel 
65614c5206cSIdo Schimmel 	if (idev->cnf.ignore_routes_with_linkdown &&
6575e670d84SDavid Ahern 	    rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
658d5d32e4bSDavid Ahern 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
65935103d11SAndy Gospodarek 		goto out;
660554cfb7eSYOSHIFUJI Hideaki 
66114895687SDavid Ahern 	if (fib6_check_expired(rt))
662f11e6659SDavid S. Miller 		goto out;
663554cfb7eSYOSHIFUJI Hideaki 
664554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
6657e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
666afc154e9SHannes Frederic Sowa 		match_do_rr = true;
667afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
6687e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
669f11e6659SDavid S. Miller 		goto out;
6701da177e4SLinus Torvalds 	}
671f11e6659SDavid S. Miller 
672afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
673afc154e9SHannes Frederic Sowa 		rt6_probe(rt);
674afc154e9SHannes Frederic Sowa 
6757e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
676afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
677afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
678afc154e9SHannes Frederic Sowa 		*mpri = m;
679afc154e9SHannes Frederic Sowa 		match = rt;
680afc154e9SHannes Frederic Sowa 	}
681f11e6659SDavid S. Miller out:
682f11e6659SDavid S. Miller 	return match;
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6858d1c802bSDavid Ahern static struct fib6_info *find_rr_leaf(struct fib6_node *fn,
6868d1c802bSDavid Ahern 				     struct fib6_info *leaf,
6878d1c802bSDavid Ahern 				     struct fib6_info *rr_head,
688afc154e9SHannes Frederic Sowa 				     u32 metric, int oif, int strict,
689afc154e9SHannes Frederic Sowa 				     bool *do_rr)
690f11e6659SDavid S. Miller {
6918d1c802bSDavid Ahern 	struct fib6_info *rt, *match, *cont;
692f11e6659SDavid S. Miller 	int mpri = -1;
693f11e6659SDavid S. Miller 
694f11e6659SDavid S. Miller 	match = NULL;
6959fbdcfafSSteffen Klassert 	cont = NULL;
696071fb37eSDavid Miller 	for (rt = rr_head; rt; rt = rcu_dereference(rt->rt6_next)) {
69793c2fb25SDavid Ahern 		if (rt->fib6_metric != metric) {
6989fbdcfafSSteffen Klassert 			cont = rt;
6999fbdcfafSSteffen Klassert 			break;
7009fbdcfafSSteffen Klassert 		}
7019fbdcfafSSteffen Klassert 
702afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
7039fbdcfafSSteffen Klassert 	}
7049fbdcfafSSteffen Klassert 
70566f5d6ceSWei Wang 	for (rt = leaf; rt && rt != rr_head;
706071fb37eSDavid Miller 	     rt = rcu_dereference(rt->rt6_next)) {
70793c2fb25SDavid Ahern 		if (rt->fib6_metric != metric) {
7089fbdcfafSSteffen Klassert 			cont = rt;
7099fbdcfafSSteffen Klassert 			break;
7109fbdcfafSSteffen Klassert 		}
7119fbdcfafSSteffen Klassert 
7129fbdcfafSSteffen Klassert 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
7139fbdcfafSSteffen Klassert 	}
7149fbdcfafSSteffen Klassert 
7159fbdcfafSSteffen Klassert 	if (match || !cont)
7169fbdcfafSSteffen Klassert 		return match;
7179fbdcfafSSteffen Klassert 
718071fb37eSDavid Miller 	for (rt = cont; rt; rt = rcu_dereference(rt->rt6_next))
719afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
720f11e6659SDavid S. Miller 
721f11e6659SDavid S. Miller 	return match;
722f11e6659SDavid S. Miller }
723f11e6659SDavid S. Miller 
7248d1c802bSDavid Ahern static struct fib6_info *rt6_select(struct net *net, struct fib6_node *fn,
7258d1040e8SWei Wang 				   int oif, int strict)
726f11e6659SDavid S. Miller {
7278d1c802bSDavid Ahern 	struct fib6_info *leaf = rcu_dereference(fn->leaf);
7288d1c802bSDavid Ahern 	struct fib6_info *match, *rt0;
729afc154e9SHannes Frederic Sowa 	bool do_rr = false;
73017ecf590SWei Wang 	int key_plen;
731f11e6659SDavid S. Miller 
732421842edSDavid Ahern 	if (!leaf || leaf == net->ipv6.fib6_null_entry)
733421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
7348d1040e8SWei Wang 
73566f5d6ceSWei Wang 	rt0 = rcu_dereference(fn->rr_ptr);
736f11e6659SDavid S. Miller 	if (!rt0)
73766f5d6ceSWei Wang 		rt0 = leaf;
738f11e6659SDavid S. Miller 
73917ecf590SWei Wang 	/* Double check to make sure fn is not an intermediate node
74017ecf590SWei Wang 	 * and fn->leaf does not points to its child's leaf
74117ecf590SWei Wang 	 * (This might happen if all routes under fn are deleted from
74217ecf590SWei Wang 	 * the tree and fib6_repair_tree() is called on the node.)
74317ecf590SWei Wang 	 */
74493c2fb25SDavid Ahern 	key_plen = rt0->fib6_dst.plen;
74517ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES
74693c2fb25SDavid Ahern 	if (rt0->fib6_src.plen)
74793c2fb25SDavid Ahern 		key_plen = rt0->fib6_src.plen;
74817ecf590SWei Wang #endif
74917ecf590SWei Wang 	if (fn->fn_bit != key_plen)
750421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
75117ecf590SWei Wang 
75293c2fb25SDavid Ahern 	match = find_rr_leaf(fn, leaf, rt0, rt0->fib6_metric, oif, strict,
753afc154e9SHannes Frederic Sowa 			     &do_rr);
754f11e6659SDavid S. Miller 
755afc154e9SHannes Frederic Sowa 	if (do_rr) {
7568d1c802bSDavid Ahern 		struct fib6_info *next = rcu_dereference(rt0->rt6_next);
757f11e6659SDavid S. Miller 
758554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
75993c2fb25SDavid Ahern 		if (!next || next->fib6_metric != rt0->fib6_metric)
7608d1040e8SWei Wang 			next = leaf;
761f11e6659SDavid S. Miller 
76266f5d6ceSWei Wang 		if (next != rt0) {
76393c2fb25SDavid Ahern 			spin_lock_bh(&leaf->fib6_table->tb6_lock);
76466f5d6ceSWei Wang 			/* make sure next is not being deleted from the tree */
76593c2fb25SDavid Ahern 			if (next->fib6_node)
76666f5d6ceSWei Wang 				rcu_assign_pointer(fn->rr_ptr, next);
76793c2fb25SDavid Ahern 			spin_unlock_bh(&leaf->fib6_table->tb6_lock);
76866f5d6ceSWei Wang 		}
769554cfb7eSYOSHIFUJI Hideaki 	}
770554cfb7eSYOSHIFUJI Hideaki 
771421842edSDavid Ahern 	return match ? match : net->ipv6.fib6_null_entry;
7721da177e4SLinus Torvalds }
7731da177e4SLinus Torvalds 
7748d1c802bSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_info *rt)
7758b9df265SMartin KaFai Lau {
77693c2fb25SDavid Ahern 	return (rt->fib6_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
7778b9df265SMartin KaFai Lau }
7788b9df265SMartin KaFai Lau 
77970ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
78070ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
781b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
78270ceb4f5SYOSHIFUJI Hideaki {
783c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
78470ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
78570ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
78670ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
7874bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
7888d1c802bSDavid Ahern 	struct fib6_info *rt;
78970ceb4f5SYOSHIFUJI Hideaki 
79070ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
79170ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
79270ceb4f5SYOSHIFUJI Hideaki 	}
79370ceb4f5SYOSHIFUJI Hideaki 
79470ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
79570ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
79670ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
79770ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
79870ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
79970ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
80070ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
80170ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
80270ceb4f5SYOSHIFUJI Hideaki 		}
80370ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
80470ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
80570ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
80670ceb4f5SYOSHIFUJI Hideaki 		}
80770ceb4f5SYOSHIFUJI Hideaki 	}
80870ceb4f5SYOSHIFUJI Hideaki 
80970ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
81070ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
8113933fc95SJens Rosenboom 		return -EINVAL;
81270ceb4f5SYOSHIFUJI Hideaki 
8134bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
81470ceb4f5SYOSHIFUJI Hideaki 
81570ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
81670ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
81770ceb4f5SYOSHIFUJI Hideaki 	else {
81870ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
81970ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
82070ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
82170ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
82270ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
82370ceb4f5SYOSHIFUJI Hideaki 	}
82470ceb4f5SYOSHIFUJI Hideaki 
825f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
826afb1d4b5SDavid Ahern 		rt = rt6_get_dflt_router(net, gwaddr, dev);
827f104a567SDuan Jiong 	else
828f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
829830218c1SDavid Ahern 					gwaddr, dev);
83070ceb4f5SYOSHIFUJI Hideaki 
83170ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
832afb1d4b5SDavid Ahern 		ip6_del_rt(net, rt);
83370ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
83470ceb4f5SYOSHIFUJI Hideaki 	}
83570ceb4f5SYOSHIFUJI Hideaki 
83670ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
837830218c1SDavid Ahern 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
838830218c1SDavid Ahern 					dev, pref);
83970ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
84093c2fb25SDavid Ahern 		rt->fib6_flags = RTF_ROUTEINFO |
84193c2fb25SDavid Ahern 				 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
84270ceb4f5SYOSHIFUJI Hideaki 
84370ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
8441716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
84514895687SDavid Ahern 			fib6_clean_expires(rt);
8461716a961SGao feng 		else
84714895687SDavid Ahern 			fib6_set_expires(rt, jiffies + HZ * lifetime);
8481716a961SGao feng 
84993531c67SDavid Ahern 		fib6_info_release(rt);
85070ceb4f5SYOSHIFUJI Hideaki 	}
85170ceb4f5SYOSHIFUJI Hideaki 	return 0;
85270ceb4f5SYOSHIFUJI Hideaki }
85370ceb4f5SYOSHIFUJI Hideaki #endif
85470ceb4f5SYOSHIFUJI Hideaki 
855ae90d867SDavid Ahern /*
856ae90d867SDavid Ahern  *	Misc support functions
857ae90d867SDavid Ahern  */
858ae90d867SDavid Ahern 
859ae90d867SDavid Ahern /* called with rcu_lock held */
8608d1c802bSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct fib6_info *rt)
861ae90d867SDavid Ahern {
8625e670d84SDavid Ahern 	struct net_device *dev = rt->fib6_nh.nh_dev;
863ae90d867SDavid Ahern 
86493c2fb25SDavid Ahern 	if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
865ae90d867SDavid Ahern 		/* for copies of local routes, dst->dev needs to be the
866ae90d867SDavid Ahern 		 * device if it is a master device, the master device if
867ae90d867SDavid Ahern 		 * device is enslaved, and the loopback as the default
868ae90d867SDavid Ahern 		 */
869ae90d867SDavid Ahern 		if (netif_is_l3_slave(dev) &&
87093c2fb25SDavid Ahern 		    !rt6_need_strict(&rt->fib6_dst.addr))
871ae90d867SDavid Ahern 			dev = l3mdev_master_dev_rcu(dev);
872ae90d867SDavid Ahern 		else if (!netif_is_l3_master(dev))
873ae90d867SDavid Ahern 			dev = dev_net(dev)->loopback_dev;
874ae90d867SDavid Ahern 		/* last case is netif_is_l3_master(dev) is true in which
875ae90d867SDavid Ahern 		 * case we want dev returned to be dev
876ae90d867SDavid Ahern 		 */
877ae90d867SDavid Ahern 	}
878ae90d867SDavid Ahern 
879ae90d867SDavid Ahern 	return dev;
880ae90d867SDavid Ahern }
881ae90d867SDavid Ahern 
8826edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = {
8836edb3c96SDavid Ahern 	[RTN_UNSPEC]	= 0,
8846edb3c96SDavid Ahern 	[RTN_UNICAST]	= 0,
8856edb3c96SDavid Ahern 	[RTN_LOCAL]	= 0,
8866edb3c96SDavid Ahern 	[RTN_BROADCAST]	= 0,
8876edb3c96SDavid Ahern 	[RTN_ANYCAST]	= 0,
8886edb3c96SDavid Ahern 	[RTN_MULTICAST]	= 0,
8896edb3c96SDavid Ahern 	[RTN_BLACKHOLE]	= -EINVAL,
8906edb3c96SDavid Ahern 	[RTN_UNREACHABLE] = -EHOSTUNREACH,
8916edb3c96SDavid Ahern 	[RTN_PROHIBIT]	= -EACCES,
8926edb3c96SDavid Ahern 	[RTN_THROW]	= -EAGAIN,
8936edb3c96SDavid Ahern 	[RTN_NAT]	= -EINVAL,
8946edb3c96SDavid Ahern 	[RTN_XRESOLVE]	= -EINVAL,
8956edb3c96SDavid Ahern };
8966edb3c96SDavid Ahern 
8976edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type)
8986edb3c96SDavid Ahern {
8996edb3c96SDavid Ahern 	return fib6_prop[fib6_type];
9006edb3c96SDavid Ahern }
9016edb3c96SDavid Ahern 
9028d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
9033b6761d1SDavid Ahern {
9043b6761d1SDavid Ahern 	unsigned short flags = 0;
9053b6761d1SDavid Ahern 
9063b6761d1SDavid Ahern 	if (rt->dst_nocount)
9073b6761d1SDavid Ahern 		flags |= DST_NOCOUNT;
9083b6761d1SDavid Ahern 	if (rt->dst_nopolicy)
9093b6761d1SDavid Ahern 		flags |= DST_NOPOLICY;
9103b6761d1SDavid Ahern 	if (rt->dst_host)
9113b6761d1SDavid Ahern 		flags |= DST_HOST;
9123b6761d1SDavid Ahern 
9133b6761d1SDavid Ahern 	return flags;
9143b6761d1SDavid Ahern }
9153b6761d1SDavid Ahern 
9168d1c802bSDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, struct fib6_info *ort)
9176edb3c96SDavid Ahern {
9186edb3c96SDavid Ahern 	rt->dst.error = ip6_rt_type_to_error(ort->fib6_type);
9196edb3c96SDavid Ahern 
9206edb3c96SDavid Ahern 	switch (ort->fib6_type) {
9216edb3c96SDavid Ahern 	case RTN_BLACKHOLE:
9226edb3c96SDavid Ahern 		rt->dst.output = dst_discard_out;
9236edb3c96SDavid Ahern 		rt->dst.input = dst_discard;
9246edb3c96SDavid Ahern 		break;
9256edb3c96SDavid Ahern 	case RTN_PROHIBIT:
9266edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_prohibit_out;
9276edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_prohibit;
9286edb3c96SDavid Ahern 		break;
9296edb3c96SDavid Ahern 	case RTN_THROW:
9306edb3c96SDavid Ahern 	case RTN_UNREACHABLE:
9316edb3c96SDavid Ahern 	default:
9326edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_discard_out;
9336edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_discard;
9346edb3c96SDavid Ahern 		break;
9356edb3c96SDavid Ahern 	}
9366edb3c96SDavid Ahern }
9376edb3c96SDavid Ahern 
9388d1c802bSDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, struct fib6_info *ort)
9396edb3c96SDavid Ahern {
9403b6761d1SDavid Ahern 	rt->dst.flags |= fib6_info_dst_flags(ort);
9413b6761d1SDavid Ahern 
94293c2fb25SDavid Ahern 	if (ort->fib6_flags & RTF_REJECT) {
9436edb3c96SDavid Ahern 		ip6_rt_init_dst_reject(rt, ort);
9446edb3c96SDavid Ahern 		return;
9456edb3c96SDavid Ahern 	}
9466edb3c96SDavid Ahern 
9476edb3c96SDavid Ahern 	rt->dst.error = 0;
9486edb3c96SDavid Ahern 	rt->dst.output = ip6_output;
9496edb3c96SDavid Ahern 
9506edb3c96SDavid Ahern 	if (ort->fib6_type == RTN_LOCAL) {
9516edb3c96SDavid Ahern 		rt->dst.input = ip6_input;
95293c2fb25SDavid Ahern 	} else if (ipv6_addr_type(&ort->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
9536edb3c96SDavid Ahern 		rt->dst.input = ip6_mc_input;
9546edb3c96SDavid Ahern 	} else {
9556edb3c96SDavid Ahern 		rt->dst.input = ip6_forward;
9566edb3c96SDavid Ahern 	}
9576edb3c96SDavid Ahern 
9586edb3c96SDavid Ahern 	if (ort->fib6_nh.nh_lwtstate) {
9596edb3c96SDavid Ahern 		rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate);
9606edb3c96SDavid Ahern 		lwtunnel_set_redirect(&rt->dst);
9616edb3c96SDavid Ahern 	}
9626edb3c96SDavid Ahern 
9636edb3c96SDavid Ahern 	rt->dst.lastuse = jiffies;
9646edb3c96SDavid Ahern }
9656edb3c96SDavid Ahern 
9668d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
967ae90d867SDavid Ahern {
968ae90d867SDavid Ahern 	rt->rt6i_flags &= ~RTF_EXPIRES;
96993531c67SDavid Ahern 	fib6_info_hold(from);
970ae90d867SDavid Ahern 	rt->from = from;
971d4ead6b3SDavid Ahern 	dst_init_metrics(&rt->dst, from->fib6_metrics->metrics, true);
972d4ead6b3SDavid Ahern 	if (from->fib6_metrics != &dst_default_metrics) {
973d4ead6b3SDavid Ahern 		rt->dst._metrics |= DST_METRICS_REFCOUNTED;
974d4ead6b3SDavid Ahern 		refcount_inc(&from->fib6_metrics->refcnt);
975d4ead6b3SDavid Ahern 	}
976ae90d867SDavid Ahern }
977ae90d867SDavid Ahern 
9788d1c802bSDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, struct fib6_info *ort)
979ae90d867SDavid Ahern {
9806edb3c96SDavid Ahern 	ip6_rt_init_dst(rt, ort);
9816edb3c96SDavid Ahern 
98293c2fb25SDavid Ahern 	rt->rt6i_dst = ort->fib6_dst;
98393c2fb25SDavid Ahern 	rt->rt6i_idev = ort->fib6_idev;
984ae90d867SDavid Ahern 	if (rt->rt6i_idev)
985ae90d867SDavid Ahern 		in6_dev_hold(rt->rt6i_idev);
9865e670d84SDavid Ahern 	rt->rt6i_gateway = ort->fib6_nh.nh_gw;
98793c2fb25SDavid Ahern 	rt->rt6i_flags = ort->fib6_flags;
988ae90d867SDavid Ahern 	rt6_set_from(rt, ort);
989ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
99093c2fb25SDavid Ahern 	rt->rt6i_src = ort->fib6_src;
991ae90d867SDavid Ahern #endif
99293c2fb25SDavid Ahern 	rt->rt6i_prefsrc = ort->fib6_prefsrc;
9935e670d84SDavid Ahern 	rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate);
994ae90d867SDavid Ahern }
995ae90d867SDavid Ahern 
996a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
997a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
998a3c00e46SMartin KaFai Lau {
99966f5d6ceSWei Wang 	struct fib6_node *pn, *sn;
1000a3c00e46SMartin KaFai Lau 	while (1) {
1001a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
1002a3c00e46SMartin KaFai Lau 			return NULL;
100366f5d6ceSWei Wang 		pn = rcu_dereference(fn->parent);
100466f5d6ceSWei Wang 		sn = FIB6_SUBTREE(pn);
100566f5d6ceSWei Wang 		if (sn && sn != fn)
100666f5d6ceSWei Wang 			fn = fib6_lookup(sn, NULL, saddr);
1007a3c00e46SMartin KaFai Lau 		else
1008a3c00e46SMartin KaFai Lau 			fn = pn;
1009a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
1010a3c00e46SMartin KaFai Lau 			return fn;
1011a3c00e46SMartin KaFai Lau 	}
1012a3c00e46SMartin KaFai Lau }
1013c71099acSThomas Graf 
1014d3843fe5SWei Wang static bool ip6_hold_safe(struct net *net, struct rt6_info **prt,
1015d3843fe5SWei Wang 			  bool null_fallback)
1016d3843fe5SWei Wang {
1017d3843fe5SWei Wang 	struct rt6_info *rt = *prt;
1018d3843fe5SWei Wang 
1019d3843fe5SWei Wang 	if (dst_hold_safe(&rt->dst))
1020d3843fe5SWei Wang 		return true;
1021d3843fe5SWei Wang 	if (null_fallback) {
1022d3843fe5SWei Wang 		rt = net->ipv6.ip6_null_entry;
1023d3843fe5SWei Wang 		dst_hold(&rt->dst);
1024d3843fe5SWei Wang 	} else {
1025d3843fe5SWei Wang 		rt = NULL;
1026d3843fe5SWei Wang 	}
1027d3843fe5SWei Wang 	*prt = rt;
1028d3843fe5SWei Wang 	return false;
1029d3843fe5SWei Wang }
1030d3843fe5SWei Wang 
1031dec9b0e2SDavid Ahern /* called with rcu_lock held */
10328d1c802bSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(struct fib6_info *rt)
1033dec9b0e2SDavid Ahern {
10343b6761d1SDavid Ahern 	unsigned short flags = fib6_info_dst_flags(rt);
1035dec9b0e2SDavid Ahern 	struct net_device *dev = rt->fib6_nh.nh_dev;
1036dec9b0e2SDavid Ahern 	struct rt6_info *nrt;
1037dec9b0e2SDavid Ahern 
103893531c67SDavid Ahern 	nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
1039dec9b0e2SDavid Ahern 	if (nrt)
1040dec9b0e2SDavid Ahern 		ip6_rt_copy_init(nrt, rt);
1041dec9b0e2SDavid Ahern 
1042dec9b0e2SDavid Ahern 	return nrt;
1043dec9b0e2SDavid Ahern }
1044dec9b0e2SDavid Ahern 
10458ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
10468ed67789SDaniel Lezcano 					     struct fib6_table *table,
1047b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
1048b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
1049b75cc8f9SDavid Ahern 					     int flags)
10501da177e4SLinus Torvalds {
10518d1c802bSDavid Ahern 	struct fib6_info *f6i;
10521da177e4SLinus Torvalds 	struct fib6_node *fn;
105323fb93a4SDavid Ahern 	struct rt6_info *rt;
10541da177e4SLinus Torvalds 
1055b6cdbc85SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1056b6cdbc85SDavid Ahern 		flags &= ~RT6_LOOKUP_F_IFACE;
1057b6cdbc85SDavid Ahern 
105866f5d6ceSWei Wang 	rcu_read_lock();
10594c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1060c71099acSThomas Graf restart:
106123fb93a4SDavid Ahern 	f6i = rcu_dereference(fn->leaf);
106223fb93a4SDavid Ahern 	if (!f6i) {
106323fb93a4SDavid Ahern 		f6i = net->ipv6.fib6_null_entry;
106466f5d6ceSWei Wang 	} else {
106523fb93a4SDavid Ahern 		f6i = rt6_device_match(net, f6i, &fl6->saddr,
106666f5d6ceSWei Wang 				      fl6->flowi6_oif, flags);
106793c2fb25SDavid Ahern 		if (f6i->fib6_nsiblings && fl6->flowi6_oif == 0)
106823fb93a4SDavid Ahern 			f6i = rt6_multipath_select(net, f6i, fl6,
106923fb93a4SDavid Ahern 						   fl6->flowi6_oif, skb, flags);
107066f5d6ceSWei Wang 	}
107123fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1072a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1073a3c00e46SMartin KaFai Lau 		if (fn)
1074a3c00e46SMartin KaFai Lau 			goto restart;
1075a3c00e46SMartin KaFai Lau 	}
107623fb93a4SDavid Ahern 
10772b760fcfSWei Wang 	/* Search through exception table */
107823fb93a4SDavid Ahern 	rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
107923fb93a4SDavid Ahern 	if (rt) {
1080d3843fe5SWei Wang 		if (ip6_hold_safe(net, &rt, true))
1081d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
108223fb93a4SDavid Ahern 	} else if (f6i == net->ipv6.fib6_null_entry) {
1083dec9b0e2SDavid Ahern 		rt = net->ipv6.ip6_null_entry;
1084dec9b0e2SDavid Ahern 		dst_hold(&rt->dst);
108523fb93a4SDavid Ahern 	} else {
108623fb93a4SDavid Ahern 		rt = ip6_create_rt_rcu(f6i);
108723fb93a4SDavid Ahern 		if (!rt) {
108823fb93a4SDavid Ahern 			rt = net->ipv6.ip6_null_entry;
108923fb93a4SDavid Ahern 			dst_hold(&rt->dst);
109023fb93a4SDavid Ahern 		}
1091dec9b0e2SDavid Ahern 	}
1092d3843fe5SWei Wang 
109366f5d6ceSWei Wang 	rcu_read_unlock();
1094b811580dSDavid Ahern 
1095b65f164dSPaolo Abeni 	trace_fib6_table_lookup(net, rt, table, fl6);
1096b811580dSDavid Ahern 
10971da177e4SLinus Torvalds 	return rt;
1098c71099acSThomas Graf }
1099c71099acSThomas Graf 
1100ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1101b75cc8f9SDavid Ahern 				   const struct sk_buff *skb, int flags)
1102ea6e574eSFlorian Westphal {
1103b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1104ea6e574eSFlorian Westphal }
1105ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
1106ea6e574eSFlorian Westphal 
11079acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1108b75cc8f9SDavid Ahern 			    const struct in6_addr *saddr, int oif,
1109b75cc8f9SDavid Ahern 			    const struct sk_buff *skb, int strict)
1110c71099acSThomas Graf {
11114c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
11124c9483b2SDavid S. Miller 		.flowi6_oif = oif,
11134c9483b2SDavid S. Miller 		.daddr = *daddr,
1114c71099acSThomas Graf 	};
1115c71099acSThomas Graf 	struct dst_entry *dst;
111677d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1117c71099acSThomas Graf 
1118adaa70bbSThomas Graf 	if (saddr) {
11194c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1120adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1121adaa70bbSThomas Graf 	}
1122adaa70bbSThomas Graf 
1123b75cc8f9SDavid Ahern 	dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1124c71099acSThomas Graf 	if (dst->error == 0)
1125c71099acSThomas Graf 		return (struct rt6_info *) dst;
1126c71099acSThomas Graf 
1127c71099acSThomas Graf 	dst_release(dst);
1128c71099acSThomas Graf 
11291da177e4SLinus Torvalds 	return NULL;
11301da177e4SLinus Torvalds }
11317159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
11327159039aSYOSHIFUJI Hideaki 
1133c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
11341cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
11351cfb71eeSWei Wang  * route is released.
11361cfb71eeSWei Wang  * Caller must hold dst before calling it.
11371da177e4SLinus Torvalds  */
11381da177e4SLinus Torvalds 
11398d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1140333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
11411da177e4SLinus Torvalds {
11421da177e4SLinus Torvalds 	int err;
1143c71099acSThomas Graf 	struct fib6_table *table;
11441da177e4SLinus Torvalds 
114593c2fb25SDavid Ahern 	table = rt->fib6_table;
114666f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
1147d4ead6b3SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, extack);
114866f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	return err;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
11538d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt)
115440e22e8fSThomas Graf {
1155afb1d4b5SDavid Ahern 	struct nl_info info = {	.nl_net = net, };
1156e715b6d3SFlorian Westphal 
1157d4ead6b3SDavid Ahern 	return __ip6_ins_rt(rt, &info, NULL);
115840e22e8fSThomas Graf }
115940e22e8fSThomas Graf 
11608d1c802bSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(struct fib6_info *ort,
116121efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
1162b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
11631da177e4SLinus Torvalds {
11644832c30dSDavid Ahern 	struct net_device *dev;
11651da177e4SLinus Torvalds 	struct rt6_info *rt;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	/*
11681da177e4SLinus Torvalds 	 *	Clone the route.
11691da177e4SLinus Torvalds 	 */
11701da177e4SLinus Torvalds 
11714832c30dSDavid Ahern 	rcu_read_lock();
11724832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(ort);
117393531c67SDavid Ahern 	rt = ip6_dst_alloc(dev_net(dev), dev, 0);
11744832c30dSDavid Ahern 	rcu_read_unlock();
117583a09abdSMartin KaFai Lau 	if (!rt)
117683a09abdSMartin KaFai Lau 		return NULL;
117783a09abdSMartin KaFai Lau 
117883a09abdSMartin KaFai Lau 	ip6_rt_copy_init(rt, ort);
11798b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
118083a09abdSMartin KaFai Lau 	rt->dst.flags |= DST_HOST;
118183a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
118283a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
11838b9df265SMartin KaFai Lau 
11848b9df265SMartin KaFai Lau 	if (!rt6_is_gw_or_nonexthop(ort)) {
118593c2fb25SDavid Ahern 		if (ort->fib6_dst.plen != 128 &&
118693c2fb25SDavid Ahern 		    ipv6_addr_equal(&ort->fib6_dst.addr, daddr))
118758c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
11881da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
11891da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
11904e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
11911da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
11921da177e4SLinus Torvalds 		}
11931da177e4SLinus Torvalds #endif
119495a9a5baSYOSHIFUJI Hideaki 	}
119595a9a5baSYOSHIFUJI Hideaki 
1196299d9939SYOSHIFUJI Hideaki 	return rt;
1197299d9939SYOSHIFUJI Hideaki }
1198299d9939SYOSHIFUJI Hideaki 
11998d1c802bSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(struct fib6_info *rt)
1200d52d3997SMartin KaFai Lau {
12013b6761d1SDavid Ahern 	unsigned short flags = fib6_info_dst_flags(rt);
12024832c30dSDavid Ahern 	struct net_device *dev;
1203d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1204d52d3997SMartin KaFai Lau 
12054832c30dSDavid Ahern 	rcu_read_lock();
12064832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(rt);
120793531c67SDavid Ahern 	pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
12084832c30dSDavid Ahern 	rcu_read_unlock();
1209d52d3997SMartin KaFai Lau 	if (!pcpu_rt)
1210d52d3997SMartin KaFai Lau 		return NULL;
1211d52d3997SMartin KaFai Lau 	ip6_rt_copy_init(pcpu_rt, rt);
1212d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
1213d52d3997SMartin KaFai Lau 	return pcpu_rt;
1214d52d3997SMartin KaFai Lau }
1215d52d3997SMartin KaFai Lau 
121666f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */
12178d1c802bSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(struct fib6_info *rt)
1218d52d3997SMartin KaFai Lau {
1219a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, **p;
1220d52d3997SMartin KaFai Lau 
1221d52d3997SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1222d52d3997SMartin KaFai Lau 	pcpu_rt = *p;
1223d52d3997SMartin KaFai Lau 
1224d4ead6b3SDavid Ahern 	if (pcpu_rt)
1225d4ead6b3SDavid Ahern 		ip6_hold_safe(NULL, &pcpu_rt, false);
1226d3843fe5SWei Wang 
1227a73e4195SMartin KaFai Lau 	return pcpu_rt;
1228a73e4195SMartin KaFai Lau }
1229a73e4195SMartin KaFai Lau 
1230afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net,
12318d1c802bSDavid Ahern 					    struct fib6_info *rt)
1232a73e4195SMartin KaFai Lau {
1233a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1234d52d3997SMartin KaFai Lau 
1235d52d3997SMartin KaFai Lau 	pcpu_rt = ip6_rt_pcpu_alloc(rt);
1236d52d3997SMartin KaFai Lau 	if (!pcpu_rt) {
12379c7370a1SMartin KaFai Lau 		dst_hold(&net->ipv6.ip6_null_entry->dst);
12389c7370a1SMartin KaFai Lau 		return net->ipv6.ip6_null_entry;
1239d52d3997SMartin KaFai Lau 	}
1240d52d3997SMartin KaFai Lau 
1241a94b9367SWei Wang 	dst_hold(&pcpu_rt->dst);
1242a73e4195SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1243d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1244951f788aSEric Dumazet 	BUG_ON(prev);
1245a94b9367SWei Wang 
1246d52d3997SMartin KaFai Lau 	return pcpu_rt;
1247d52d3997SMartin KaFai Lau }
1248d52d3997SMartin KaFai Lau 
124935732d01SWei Wang /* exception hash table implementation
125035732d01SWei Wang  */
125135732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
125235732d01SWei Wang 
125335732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
125435732d01SWei Wang  * Caller must hold rt6_exception_lock
125535732d01SWei Wang  */
125635732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
125735732d01SWei Wang 				 struct rt6_exception *rt6_ex)
125835732d01SWei Wang {
1259b2427e67SColin Ian King 	struct net *net;
126081eb8447SWei Wang 
126135732d01SWei Wang 	if (!bucket || !rt6_ex)
126235732d01SWei Wang 		return;
1263b2427e67SColin Ian King 
1264b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
126535732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
126677634cc6SDavid Ahern 	dst_release(&rt6_ex->rt6i->dst);
126735732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
126835732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
126935732d01SWei Wang 	bucket->depth--;
127081eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache--;
127135732d01SWei Wang }
127235732d01SWei Wang 
127335732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
127435732d01SWei Wang  * Caller must hold rt6_exception_lock
127535732d01SWei Wang  */
127635732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
127735732d01SWei Wang {
127835732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
127935732d01SWei Wang 
128035732d01SWei Wang 	if (!bucket)
128135732d01SWei Wang 		return;
128235732d01SWei Wang 
128335732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
128435732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
128535732d01SWei Wang 			oldest = rt6_ex;
128635732d01SWei Wang 	}
128735732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
128835732d01SWei Wang }
128935732d01SWei Wang 
129035732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
129135732d01SWei Wang 			      const struct in6_addr *src)
129235732d01SWei Wang {
129335732d01SWei Wang 	static u32 seed __read_mostly;
129435732d01SWei Wang 	u32 val;
129535732d01SWei Wang 
129635732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
129735732d01SWei Wang 	val = jhash(dst, sizeof(*dst), seed);
129835732d01SWei Wang 
129935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
130035732d01SWei Wang 	if (src)
130135732d01SWei Wang 		val = jhash(src, sizeof(*src), val);
130235732d01SWei Wang #endif
130335732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
130435732d01SWei Wang }
130535732d01SWei Wang 
130635732d01SWei Wang /* Helper function to find the cached rt in the hash table
130735732d01SWei Wang  * and update bucket pointer to point to the bucket for this
130835732d01SWei Wang  * (daddr, saddr) pair
130935732d01SWei Wang  * Caller must hold rt6_exception_lock
131035732d01SWei Wang  */
131135732d01SWei Wang static struct rt6_exception *
131235732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
131335732d01SWei Wang 			      const struct in6_addr *daddr,
131435732d01SWei Wang 			      const struct in6_addr *saddr)
131535732d01SWei Wang {
131635732d01SWei Wang 	struct rt6_exception *rt6_ex;
131735732d01SWei Wang 	u32 hval;
131835732d01SWei Wang 
131935732d01SWei Wang 	if (!(*bucket) || !daddr)
132035732d01SWei Wang 		return NULL;
132135732d01SWei Wang 
132235732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
132335732d01SWei Wang 	*bucket += hval;
132435732d01SWei Wang 
132535732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
132635732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
132735732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
132835732d01SWei Wang 
132935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
133035732d01SWei Wang 		if (matched && saddr)
133135732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
133235732d01SWei Wang #endif
133335732d01SWei Wang 		if (matched)
133435732d01SWei Wang 			return rt6_ex;
133535732d01SWei Wang 	}
133635732d01SWei Wang 	return NULL;
133735732d01SWei Wang }
133835732d01SWei Wang 
133935732d01SWei Wang /* Helper function to find the cached rt in the hash table
134035732d01SWei Wang  * and update bucket pointer to point to the bucket for this
134135732d01SWei Wang  * (daddr, saddr) pair
134235732d01SWei Wang  * Caller must hold rcu_read_lock()
134335732d01SWei Wang  */
134435732d01SWei Wang static struct rt6_exception *
134535732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
134635732d01SWei Wang 			 const struct in6_addr *daddr,
134735732d01SWei Wang 			 const struct in6_addr *saddr)
134835732d01SWei Wang {
134935732d01SWei Wang 	struct rt6_exception *rt6_ex;
135035732d01SWei Wang 	u32 hval;
135135732d01SWei Wang 
135235732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
135335732d01SWei Wang 
135435732d01SWei Wang 	if (!(*bucket) || !daddr)
135535732d01SWei Wang 		return NULL;
135635732d01SWei Wang 
135735732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
135835732d01SWei Wang 	*bucket += hval;
135935732d01SWei Wang 
136035732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
136135732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
136235732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
136335732d01SWei Wang 
136435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
136535732d01SWei Wang 		if (matched && saddr)
136635732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
136735732d01SWei Wang #endif
136835732d01SWei Wang 		if (matched)
136935732d01SWei Wang 			return rt6_ex;
137035732d01SWei Wang 	}
137135732d01SWei Wang 	return NULL;
137235732d01SWei Wang }
137335732d01SWei Wang 
13748d1c802bSDavid Ahern static unsigned int fib6_mtu(const struct fib6_info *rt)
1375d4ead6b3SDavid Ahern {
1376d4ead6b3SDavid Ahern 	unsigned int mtu;
1377d4ead6b3SDavid Ahern 
137893c2fb25SDavid Ahern 	mtu = rt->fib6_pmtu ? : rt->fib6_idev->cnf.mtu6;
1379d4ead6b3SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1380d4ead6b3SDavid Ahern 
1381d4ead6b3SDavid Ahern 	return mtu - lwtunnel_headroom(rt->fib6_nh.nh_lwtstate, mtu);
1382d4ead6b3SDavid Ahern }
1383d4ead6b3SDavid Ahern 
138435732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
13858d1c802bSDavid Ahern 				struct fib6_info *ort)
138635732d01SWei Wang {
13875e670d84SDavid Ahern 	struct net *net = dev_net(nrt->dst.dev);
138835732d01SWei Wang 	struct rt6_exception_bucket *bucket;
138935732d01SWei Wang 	struct in6_addr *src_key = NULL;
139035732d01SWei Wang 	struct rt6_exception *rt6_ex;
139135732d01SWei Wang 	int err = 0;
139235732d01SWei Wang 
139335732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
139435732d01SWei Wang 
139535732d01SWei Wang 	if (ort->exception_bucket_flushed) {
139635732d01SWei Wang 		err = -EINVAL;
139735732d01SWei Wang 		goto out;
139835732d01SWei Wang 	}
139935732d01SWei Wang 
140035732d01SWei Wang 	bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
140135732d01SWei Wang 					lockdep_is_held(&rt6_exception_lock));
140235732d01SWei Wang 	if (!bucket) {
140335732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
140435732d01SWei Wang 				 GFP_ATOMIC);
140535732d01SWei Wang 		if (!bucket) {
140635732d01SWei Wang 			err = -ENOMEM;
140735732d01SWei Wang 			goto out;
140835732d01SWei Wang 		}
140935732d01SWei Wang 		rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
141035732d01SWei Wang 	}
141135732d01SWei Wang 
141235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
141335732d01SWei Wang 	/* rt6i_src.plen != 0 indicates ort is in subtree
141435732d01SWei Wang 	 * and exception table is indexed by a hash of
141535732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
141635732d01SWei Wang 	 * Otherwise, the exception table is indexed by
141735732d01SWei Wang 	 * a hash of only rt6i_dst.
141835732d01SWei Wang 	 */
141993c2fb25SDavid Ahern 	if (ort->fib6_src.plen)
142035732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
142135732d01SWei Wang #endif
142260006a48SWei Wang 
142360006a48SWei Wang 	/* Update rt6i_prefsrc as it could be changed
142460006a48SWei Wang 	 * in rt6_remove_prefsrc()
142560006a48SWei Wang 	 */
142693c2fb25SDavid Ahern 	nrt->rt6i_prefsrc = ort->fib6_prefsrc;
1427f5bbe7eeSWei Wang 	/* rt6_mtu_change() might lower mtu on ort.
1428f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
1429f5bbe7eeSWei Wang 	 * is less than ort's mtu value.
1430f5bbe7eeSWei Wang 	 */
1431d4ead6b3SDavid Ahern 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(ort)) {
1432f5bbe7eeSWei Wang 		err = -EINVAL;
1433f5bbe7eeSWei Wang 		goto out;
1434f5bbe7eeSWei Wang 	}
143560006a48SWei Wang 
143635732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
143735732d01SWei Wang 					       src_key);
143835732d01SWei Wang 	if (rt6_ex)
143935732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
144035732d01SWei Wang 
144135732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
144235732d01SWei Wang 	if (!rt6_ex) {
144335732d01SWei Wang 		err = -ENOMEM;
144435732d01SWei Wang 		goto out;
144535732d01SWei Wang 	}
144635732d01SWei Wang 	rt6_ex->rt6i = nrt;
144735732d01SWei Wang 	rt6_ex->stamp = jiffies;
144835732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
144935732d01SWei Wang 	bucket->depth++;
145081eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
145135732d01SWei Wang 
145235732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
145335732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
145435732d01SWei Wang 
145535732d01SWei Wang out:
145635732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
145735732d01SWei Wang 
145835732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1459b886d5f2SPaolo Abeni 	if (!err) {
146093c2fb25SDavid Ahern 		spin_lock_bh(&ort->fib6_table->tb6_lock);
14617aef6859SDavid Ahern 		fib6_update_sernum(net, ort);
146293c2fb25SDavid Ahern 		spin_unlock_bh(&ort->fib6_table->tb6_lock);
1463b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1464b886d5f2SPaolo Abeni 	}
146535732d01SWei Wang 
146635732d01SWei Wang 	return err;
146735732d01SWei Wang }
146835732d01SWei Wang 
14698d1c802bSDavid Ahern void rt6_flush_exceptions(struct fib6_info *rt)
147035732d01SWei Wang {
147135732d01SWei Wang 	struct rt6_exception_bucket *bucket;
147235732d01SWei Wang 	struct rt6_exception *rt6_ex;
147335732d01SWei Wang 	struct hlist_node *tmp;
147435732d01SWei Wang 	int i;
147535732d01SWei Wang 
147635732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
147735732d01SWei Wang 	/* Prevent rt6_insert_exception() to recreate the bucket list */
147835732d01SWei Wang 	rt->exception_bucket_flushed = 1;
147935732d01SWei Wang 
148035732d01SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
148135732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
148235732d01SWei Wang 	if (!bucket)
148335732d01SWei Wang 		goto out;
148435732d01SWei Wang 
148535732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
148635732d01SWei Wang 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
148735732d01SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
148835732d01SWei Wang 		WARN_ON_ONCE(bucket->depth);
148935732d01SWei Wang 		bucket++;
149035732d01SWei Wang 	}
149135732d01SWei Wang 
149235732d01SWei Wang out:
149335732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
149435732d01SWei Wang }
149535732d01SWei Wang 
149635732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
149735732d01SWei Wang  * Caller has to hold rcu_read_lock()
149835732d01SWei Wang  */
14998d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
150035732d01SWei Wang 					   struct in6_addr *daddr,
150135732d01SWei Wang 					   struct in6_addr *saddr)
150235732d01SWei Wang {
150335732d01SWei Wang 	struct rt6_exception_bucket *bucket;
150435732d01SWei Wang 	struct in6_addr *src_key = NULL;
150535732d01SWei Wang 	struct rt6_exception *rt6_ex;
150635732d01SWei Wang 	struct rt6_info *res = NULL;
150735732d01SWei Wang 
150835732d01SWei Wang 	bucket = rcu_dereference(rt->rt6i_exception_bucket);
150935732d01SWei Wang 
151035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
151135732d01SWei Wang 	/* rt6i_src.plen != 0 indicates rt is in subtree
151235732d01SWei Wang 	 * and exception table is indexed by a hash of
151335732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
151435732d01SWei Wang 	 * Otherwise, the exception table is indexed by
151535732d01SWei Wang 	 * a hash of only rt6i_dst.
151635732d01SWei Wang 	 */
151793c2fb25SDavid Ahern 	if (rt->fib6_src.plen)
151835732d01SWei Wang 		src_key = saddr;
151935732d01SWei Wang #endif
152035732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
152135732d01SWei Wang 
152235732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
152335732d01SWei Wang 		res = rt6_ex->rt6i;
152435732d01SWei Wang 
152535732d01SWei Wang 	return res;
152635732d01SWei Wang }
152735732d01SWei Wang 
152835732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
152923fb93a4SDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt)
153035732d01SWei Wang {
153135732d01SWei Wang 	struct rt6_exception_bucket *bucket;
15328d1c802bSDavid Ahern 	struct fib6_info *from = rt->from;
153335732d01SWei Wang 	struct in6_addr *src_key = NULL;
153435732d01SWei Wang 	struct rt6_exception *rt6_ex;
153535732d01SWei Wang 	int err;
153635732d01SWei Wang 
153735732d01SWei Wang 	if (!from ||
1538442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
153935732d01SWei Wang 		return -EINVAL;
154035732d01SWei Wang 
154135732d01SWei Wang 	if (!rcu_access_pointer(from->rt6i_exception_bucket))
154235732d01SWei Wang 		return -ENOENT;
154335732d01SWei Wang 
154435732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
154535732d01SWei Wang 	bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
154635732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
154735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
154835732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
154935732d01SWei Wang 	 * and exception table is indexed by a hash of
155035732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
155135732d01SWei Wang 	 * Otherwise, the exception table is indexed by
155235732d01SWei Wang 	 * a hash of only rt6i_dst.
155335732d01SWei Wang 	 */
155493c2fb25SDavid Ahern 	if (from->fib6_src.plen)
155535732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
155635732d01SWei Wang #endif
155735732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
155835732d01SWei Wang 					       &rt->rt6i_dst.addr,
155935732d01SWei Wang 					       src_key);
156035732d01SWei Wang 	if (rt6_ex) {
156135732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
156235732d01SWei Wang 		err = 0;
156335732d01SWei Wang 	} else {
156435732d01SWei Wang 		err = -ENOENT;
156535732d01SWei Wang 	}
156635732d01SWei Wang 
156735732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
156835732d01SWei Wang 	return err;
156935732d01SWei Wang }
157035732d01SWei Wang 
157135732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
157235732d01SWei Wang  * refresh its stamp
157335732d01SWei Wang  */
157435732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
157535732d01SWei Wang {
157635732d01SWei Wang 	struct rt6_exception_bucket *bucket;
15778d1c802bSDavid Ahern 	struct fib6_info *from = rt->from;
157835732d01SWei Wang 	struct in6_addr *src_key = NULL;
157935732d01SWei Wang 	struct rt6_exception *rt6_ex;
158035732d01SWei Wang 
158135732d01SWei Wang 	if (!from ||
1582442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
158335732d01SWei Wang 		return;
158435732d01SWei Wang 
158535732d01SWei Wang 	rcu_read_lock();
158635732d01SWei Wang 	bucket = rcu_dereference(from->rt6i_exception_bucket);
158735732d01SWei Wang 
158835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
158935732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
159035732d01SWei Wang 	 * and exception table is indexed by a hash of
159135732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
159235732d01SWei Wang 	 * Otherwise, the exception table is indexed by
159335732d01SWei Wang 	 * a hash of only rt6i_dst.
159435732d01SWei Wang 	 */
159593c2fb25SDavid Ahern 	if (from->fib6_src.plen)
159635732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
159735732d01SWei Wang #endif
159835732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket,
159935732d01SWei Wang 					  &rt->rt6i_dst.addr,
160035732d01SWei Wang 					  src_key);
160135732d01SWei Wang 	if (rt6_ex)
160235732d01SWei Wang 		rt6_ex->stamp = jiffies;
160335732d01SWei Wang 
160435732d01SWei Wang 	rcu_read_unlock();
160535732d01SWei Wang }
160635732d01SWei Wang 
16078d1c802bSDavid Ahern static void rt6_exceptions_remove_prefsrc(struct fib6_info *rt)
160860006a48SWei Wang {
160960006a48SWei Wang 	struct rt6_exception_bucket *bucket;
161060006a48SWei Wang 	struct rt6_exception *rt6_ex;
161160006a48SWei Wang 	int i;
161260006a48SWei Wang 
161360006a48SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
161460006a48SWei Wang 					lockdep_is_held(&rt6_exception_lock));
161560006a48SWei Wang 
161660006a48SWei Wang 	if (bucket) {
161760006a48SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
161860006a48SWei Wang 			hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
161960006a48SWei Wang 				rt6_ex->rt6i->rt6i_prefsrc.plen = 0;
162060006a48SWei Wang 			}
162160006a48SWei Wang 			bucket++;
162260006a48SWei Wang 		}
162360006a48SWei Wang 	}
162460006a48SWei Wang }
162560006a48SWei Wang 
1626e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1627e9fa1495SStefano Brivio 					 struct rt6_info *rt, int mtu)
1628e9fa1495SStefano Brivio {
1629e9fa1495SStefano Brivio 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1630e9fa1495SStefano Brivio 	 * lowest MTU in the path: always allow updating the route PMTU to
1631e9fa1495SStefano Brivio 	 * reflect PMTU decreases.
1632e9fa1495SStefano Brivio 	 *
1633e9fa1495SStefano Brivio 	 * If the new MTU is higher, and the route PMTU is equal to the local
1634e9fa1495SStefano Brivio 	 * MTU, this means the old MTU is the lowest in the path, so allow
1635e9fa1495SStefano Brivio 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1636e9fa1495SStefano Brivio 	 * handle this.
1637e9fa1495SStefano Brivio 	 */
1638e9fa1495SStefano Brivio 
1639e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) >= mtu)
1640e9fa1495SStefano Brivio 		return true;
1641e9fa1495SStefano Brivio 
1642e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1643e9fa1495SStefano Brivio 		return true;
1644e9fa1495SStefano Brivio 
1645e9fa1495SStefano Brivio 	return false;
1646e9fa1495SStefano Brivio }
1647e9fa1495SStefano Brivio 
1648e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
16498d1c802bSDavid Ahern 				       struct fib6_info *rt, int mtu)
1650f5bbe7eeSWei Wang {
1651f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
1652f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
1653f5bbe7eeSWei Wang 	int i;
1654f5bbe7eeSWei Wang 
1655f5bbe7eeSWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1656f5bbe7eeSWei Wang 					lockdep_is_held(&rt6_exception_lock));
1657f5bbe7eeSWei Wang 
1658e9fa1495SStefano Brivio 	if (!bucket)
1659e9fa1495SStefano Brivio 		return;
1660e9fa1495SStefano Brivio 
1661f5bbe7eeSWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1662f5bbe7eeSWei Wang 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1663f5bbe7eeSWei Wang 			struct rt6_info *entry = rt6_ex->rt6i;
1664e9fa1495SStefano Brivio 
1665e9fa1495SStefano Brivio 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
1666d4ead6b3SDavid Ahern 			 * route), the metrics of its rt->from have already
1667f5bbe7eeSWei Wang 			 * been updated.
1668f5bbe7eeSWei Wang 			 */
1669d4ead6b3SDavid Ahern 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
1670e9fa1495SStefano Brivio 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
1671d4ead6b3SDavid Ahern 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
1672f5bbe7eeSWei Wang 		}
1673f5bbe7eeSWei Wang 		bucket++;
1674f5bbe7eeSWei Wang 	}
1675f5bbe7eeSWei Wang }
1676f5bbe7eeSWei Wang 
1677b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
1678b16cb459SWei Wang 
16798d1c802bSDavid Ahern static void rt6_exceptions_clean_tohost(struct fib6_info *rt,
1680b16cb459SWei Wang 					struct in6_addr *gateway)
1681b16cb459SWei Wang {
1682b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
1683b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
1684b16cb459SWei Wang 	struct hlist_node *tmp;
1685b16cb459SWei Wang 	int i;
1686b16cb459SWei Wang 
1687b16cb459SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1688b16cb459SWei Wang 		return;
1689b16cb459SWei Wang 
1690b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1691b16cb459SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1692b16cb459SWei Wang 				     lockdep_is_held(&rt6_exception_lock));
1693b16cb459SWei Wang 
1694b16cb459SWei Wang 	if (bucket) {
1695b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1696b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1697b16cb459SWei Wang 						  &bucket->chain, hlist) {
1698b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1699b16cb459SWei Wang 
1700b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1701b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
1702b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
1703b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
1704b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
1705b16cb459SWei Wang 				}
1706b16cb459SWei Wang 			}
1707b16cb459SWei Wang 			bucket++;
1708b16cb459SWei Wang 		}
1709b16cb459SWei Wang 	}
1710b16cb459SWei Wang 
1711b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1712b16cb459SWei Wang }
1713b16cb459SWei Wang 
1714c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1715c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
1716c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
1717c757faa8SWei Wang 				      unsigned long now)
1718c757faa8SWei Wang {
1719c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
1720c757faa8SWei Wang 
17211859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
17221859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
17231859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
17241859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
17251859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
17261859bac0SPaolo Abeni 	 */
172731afeb42SWei Wang 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
172831afeb42SWei Wang 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1729c757faa8SWei Wang 			RT6_TRACE("aging clone %p\n", rt);
1730c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1731c757faa8SWei Wang 			return;
173231afeb42SWei Wang 		}
173331afeb42SWei Wang 	} else if (time_after(jiffies, rt->dst.expires)) {
173431afeb42SWei Wang 		RT6_TRACE("purging expired route %p\n", rt);
173531afeb42SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
173631afeb42SWei Wang 		return;
173731afeb42SWei Wang 	}
173831afeb42SWei Wang 
173931afeb42SWei Wang 	if (rt->rt6i_flags & RTF_GATEWAY) {
1740c757faa8SWei Wang 		struct neighbour *neigh;
1741c757faa8SWei Wang 		__u8 neigh_flags = 0;
1742c757faa8SWei Wang 
17431bfa26ffSEric Dumazet 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
17441bfa26ffSEric Dumazet 		if (neigh)
1745c757faa8SWei Wang 			neigh_flags = neigh->flags;
17461bfa26ffSEric Dumazet 
1747c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
1748c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
1749c757faa8SWei Wang 				  rt);
1750c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1751c757faa8SWei Wang 			return;
1752c757faa8SWei Wang 		}
1753c757faa8SWei Wang 	}
175431afeb42SWei Wang 
1755c757faa8SWei Wang 	gc_args->more++;
1756c757faa8SWei Wang }
1757c757faa8SWei Wang 
17588d1c802bSDavid Ahern void rt6_age_exceptions(struct fib6_info *rt,
1759c757faa8SWei Wang 			struct fib6_gc_args *gc_args,
1760c757faa8SWei Wang 			unsigned long now)
1761c757faa8SWei Wang {
1762c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
1763c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
1764c757faa8SWei Wang 	struct hlist_node *tmp;
1765c757faa8SWei Wang 	int i;
1766c757faa8SWei Wang 
1767c757faa8SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1768c757faa8SWei Wang 		return;
1769c757faa8SWei Wang 
17701bfa26ffSEric Dumazet 	rcu_read_lock_bh();
17711bfa26ffSEric Dumazet 	spin_lock(&rt6_exception_lock);
1772c757faa8SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1773c757faa8SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
1774c757faa8SWei Wang 
1775c757faa8SWei Wang 	if (bucket) {
1776c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1777c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1778c757faa8SWei Wang 						  &bucket->chain, hlist) {
1779c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
1780c757faa8SWei Wang 							  gc_args, now);
1781c757faa8SWei Wang 			}
1782c757faa8SWei Wang 			bucket++;
1783c757faa8SWei Wang 		}
1784c757faa8SWei Wang 	}
17851bfa26ffSEric Dumazet 	spin_unlock(&rt6_exception_lock);
17861bfa26ffSEric Dumazet 	rcu_read_unlock_bh();
1787c757faa8SWei Wang }
1788c757faa8SWei Wang 
17899ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
1790b75cc8f9SDavid Ahern 			       int oif, struct flowi6 *fl6,
1791b75cc8f9SDavid Ahern 			       const struct sk_buff *skb, int flags)
17921da177e4SLinus Torvalds {
1793367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
17948d1c802bSDavid Ahern 	struct fib6_info *f6i;
179523fb93a4SDavid Ahern 	struct rt6_info *rt;
1796c71099acSThomas Graf 	int strict = 0;
17971da177e4SLinus Torvalds 
179877d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
1799d5d32e4bSDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
1800367efcb9SMartin KaFai Lau 	if (net->ipv6.devconf_all->forwarding == 0)
1801367efcb9SMartin KaFai Lau 		strict |= RT6_LOOKUP_F_REACHABLE;
18021da177e4SLinus Torvalds 
180366f5d6ceSWei Wang 	rcu_read_lock();
18041da177e4SLinus Torvalds 
18054c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1806367efcb9SMartin KaFai Lau 	saved_fn = fn;
18071da177e4SLinus Torvalds 
1808ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1809ca254490SDavid Ahern 		oif = 0;
1810ca254490SDavid Ahern 
1811a3c00e46SMartin KaFai Lau redo_rt6_select:
181223fb93a4SDavid Ahern 	f6i = rt6_select(net, fn, oif, strict);
181393c2fb25SDavid Ahern 	if (f6i->fib6_nsiblings)
181423fb93a4SDavid Ahern 		f6i = rt6_multipath_select(net, f6i, fl6, oif, skb, strict);
181523fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1816a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1817a3c00e46SMartin KaFai Lau 		if (fn)
1818a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
1819367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
1820367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
1821367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
1822367efcb9SMartin KaFai Lau 			fn = saved_fn;
1823367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
1824367efcb9SMartin KaFai Lau 		}
1825a3c00e46SMartin KaFai Lau 	}
1826a3c00e46SMartin KaFai Lau 
182723fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1828421842edSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
182966f5d6ceSWei Wang 		rcu_read_unlock();
1830d3843fe5SWei Wang 		dst_hold(&rt->dst);
1831b65f164dSPaolo Abeni 		trace_fib6_table_lookup(net, rt, table, fl6);
1832d3843fe5SWei Wang 		return rt;
183323fb93a4SDavid Ahern 	}
183423fb93a4SDavid Ahern 
183523fb93a4SDavid Ahern 	/*Search through exception table */
183623fb93a4SDavid Ahern 	rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
183723fb93a4SDavid Ahern 	if (rt) {
1838d4ead6b3SDavid Ahern 		if (ip6_hold_safe(net, &rt, true))
1839d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
1840d4ead6b3SDavid Ahern 
184166f5d6ceSWei Wang 		rcu_read_unlock();
1842b65f164dSPaolo Abeni 		trace_fib6_table_lookup(net, rt, table, fl6);
1843d52d3997SMartin KaFai Lau 		return rt;
18443da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
184593c2fb25SDavid Ahern 			    !(f6i->fib6_flags & RTF_GATEWAY))) {
18463da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
18473da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
18483da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
18493da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
18503da59bd9SMartin KaFai Lau 		 */
1851c71099acSThomas Graf 
18523da59bd9SMartin KaFai Lau 		struct rt6_info *uncached_rt;
18533da59bd9SMartin KaFai Lau 
185493531c67SDavid Ahern 		fib6_info_hold(f6i);
185566f5d6ceSWei Wang 		rcu_read_unlock();
1856d52d3997SMartin KaFai Lau 
185723fb93a4SDavid Ahern 		uncached_rt = ip6_rt_cache_alloc(f6i, &fl6->daddr, NULL);
185893531c67SDavid Ahern 		fib6_info_release(f6i);
18593da59bd9SMartin KaFai Lau 
18601cfb71eeSWei Wang 		if (uncached_rt) {
18611cfb71eeSWei Wang 			/* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
18621cfb71eeSWei Wang 			 * No need for another dst_hold()
18631cfb71eeSWei Wang 			 */
18648d0b94afSMartin KaFai Lau 			rt6_uncached_list_add(uncached_rt);
186581eb8447SWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
18661cfb71eeSWei Wang 		} else {
18673da59bd9SMartin KaFai Lau 			uncached_rt = net->ipv6.ip6_null_entry;
18683da59bd9SMartin KaFai Lau 			dst_hold(&uncached_rt->dst);
18691cfb71eeSWei Wang 		}
1870b811580dSDavid Ahern 
1871b65f164dSPaolo Abeni 		trace_fib6_table_lookup(net, uncached_rt, table, fl6);
18723da59bd9SMartin KaFai Lau 		return uncached_rt;
18733da59bd9SMartin KaFai Lau 
1874d52d3997SMartin KaFai Lau 	} else {
1875d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
1876d52d3997SMartin KaFai Lau 
1877d52d3997SMartin KaFai Lau 		struct rt6_info *pcpu_rt;
1878d52d3997SMartin KaFai Lau 
1879951f788aSEric Dumazet 		local_bh_disable();
188023fb93a4SDavid Ahern 		pcpu_rt = rt6_get_pcpu_route(f6i);
1881d52d3997SMartin KaFai Lau 
188293531c67SDavid Ahern 		if (!pcpu_rt)
188323fb93a4SDavid Ahern 			pcpu_rt = rt6_make_pcpu_route(net, f6i);
188493531c67SDavid Ahern 
1885951f788aSEric Dumazet 		local_bh_enable();
1886951f788aSEric Dumazet 		rcu_read_unlock();
1887b65f164dSPaolo Abeni 		trace_fib6_table_lookup(net, pcpu_rt, table, fl6);
1888d52d3997SMartin KaFai Lau 		return pcpu_rt;
1889d52d3997SMartin KaFai Lau 	}
1890c71099acSThomas Graf }
18919ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
1892c71099acSThomas Graf 
1893b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net,
1894b75cc8f9SDavid Ahern 					    struct fib6_table *table,
1895b75cc8f9SDavid Ahern 					    struct flowi6 *fl6,
1896b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
1897b75cc8f9SDavid Ahern 					    int flags)
18984acad72dSPavel Emelyanov {
1899b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
19004acad72dSPavel Emelyanov }
19014acad72dSPavel Emelyanov 
1902d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
190372331bc0SShmulik Ladkani 					 struct net_device *dev,
1904b75cc8f9SDavid Ahern 					 struct flowi6 *fl6,
1905b75cc8f9SDavid Ahern 					 const struct sk_buff *skb,
1906b75cc8f9SDavid Ahern 					 int flags)
190772331bc0SShmulik Ladkani {
190872331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
190972331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
191072331bc0SShmulik Ladkani 
1911b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
191272331bc0SShmulik Ladkani }
1913d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
191472331bc0SShmulik Ladkani 
191523aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
19165e5d6fedSRoopa Prabhu 				  struct flow_keys *keys,
19175e5d6fedSRoopa Prabhu 				  struct flow_keys *flkeys)
191823aebdacSJakub Sitnicki {
191923aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
192023aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
19215e5d6fedSRoopa Prabhu 	struct flow_keys *_flkeys = flkeys;
192223aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
192323aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
192423aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
192523aebdacSJakub Sitnicki 
192623aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
192723aebdacSJakub Sitnicki 		goto out;
192823aebdacSJakub Sitnicki 
192923aebdacSJakub Sitnicki 	icmph = icmp6_hdr(skb);
193023aebdacSJakub Sitnicki 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
193123aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
193223aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
193323aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
193423aebdacSJakub Sitnicki 		goto out;
193523aebdacSJakub Sitnicki 
193623aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
193723aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
193823aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
193923aebdacSJakub Sitnicki 	if (!inner_iph)
194023aebdacSJakub Sitnicki 		goto out;
194123aebdacSJakub Sitnicki 
194223aebdacSJakub Sitnicki 	key_iph = inner_iph;
19435e5d6fedSRoopa Prabhu 	_flkeys = NULL;
194423aebdacSJakub Sitnicki out:
19455e5d6fedSRoopa Prabhu 	if (_flkeys) {
19465e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
19475e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
19485e5d6fedSRoopa Prabhu 		keys->tags.flow_label = _flkeys->tags.flow_label;
19495e5d6fedSRoopa Prabhu 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
19505e5d6fedSRoopa Prabhu 	} else {
195123aebdacSJakub Sitnicki 		keys->addrs.v6addrs.src = key_iph->saddr;
195223aebdacSJakub Sitnicki 		keys->addrs.v6addrs.dst = key_iph->daddr;
195323aebdacSJakub Sitnicki 		keys->tags.flow_label = ip6_flowinfo(key_iph);
195423aebdacSJakub Sitnicki 		keys->basic.ip_proto = key_iph->nexthdr;
195523aebdacSJakub Sitnicki 	}
19565e5d6fedSRoopa Prabhu }
195723aebdacSJakub Sitnicki 
195823aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
1959b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
1960b4bac172SDavid Ahern 		       const struct sk_buff *skb, struct flow_keys *flkeys)
196123aebdacSJakub Sitnicki {
196223aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
19639a2a537aSDavid Ahern 	u32 mhash;
196423aebdacSJakub Sitnicki 
1965bbfa047aSDavid S. Miller 	switch (ip6_multipath_hash_policy(net)) {
1966b4bac172SDavid Ahern 	case 0:
19676f74b6c2SDavid Ahern 		memset(&hash_keys, 0, sizeof(hash_keys));
19686f74b6c2SDavid Ahern 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
19699a2a537aSDavid Ahern 		if (skb) {
19705e5d6fedSRoopa Prabhu 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
19719a2a537aSDavid Ahern 		} else {
19729a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
19739a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
19749a2a537aSDavid Ahern 			hash_keys.tags.flow_label = (__force u32)fl6->flowlabel;
19759a2a537aSDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
197623aebdacSJakub Sitnicki 		}
1977b4bac172SDavid Ahern 		break;
1978b4bac172SDavid Ahern 	case 1:
1979b4bac172SDavid Ahern 		if (skb) {
1980b4bac172SDavid Ahern 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
1981b4bac172SDavid Ahern 			struct flow_keys keys;
1982b4bac172SDavid Ahern 
1983b4bac172SDavid Ahern 			/* short-circuit if we already have L4 hash present */
1984b4bac172SDavid Ahern 			if (skb->l4_hash)
1985b4bac172SDavid Ahern 				return skb_get_hash_raw(skb) >> 1;
1986b4bac172SDavid Ahern 
1987b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
1988b4bac172SDavid Ahern 
1989b4bac172SDavid Ahern                         if (!flkeys) {
1990b4bac172SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
1991b4bac172SDavid Ahern 				flkeys = &keys;
1992b4bac172SDavid Ahern 			}
1993b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1994b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
1995b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
1996b4bac172SDavid Ahern 			hash_keys.ports.src = flkeys->ports.src;
1997b4bac172SDavid Ahern 			hash_keys.ports.dst = flkeys->ports.dst;
1998b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
1999b4bac172SDavid Ahern 		} else {
2000b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2001b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2002b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2003b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2004b4bac172SDavid Ahern 			hash_keys.ports.src = fl6->fl6_sport;
2005b4bac172SDavid Ahern 			hash_keys.ports.dst = fl6->fl6_dport;
2006b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2007b4bac172SDavid Ahern 		}
2008b4bac172SDavid Ahern 		break;
2009b4bac172SDavid Ahern 	}
20109a2a537aSDavid Ahern 	mhash = flow_hash_from_keys(&hash_keys);
201123aebdacSJakub Sitnicki 
20129a2a537aSDavid Ahern 	return mhash >> 1;
201323aebdacSJakub Sitnicki }
201423aebdacSJakub Sitnicki 
2015c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
2016c71099acSThomas Graf {
2017b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2018c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
2019adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2020904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
20214c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
2022e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
20234c9483b2SDavid S. Miller 		.daddr = iph->daddr,
20244c9483b2SDavid S. Miller 		.saddr = iph->saddr,
20256502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
20264c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
20274c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
2028c71099acSThomas Graf 	};
20295e5d6fedSRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2030adaa70bbSThomas Graf 
2031904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
203246fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2033904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
20345e5d6fedSRoopa Prabhu 
20355e5d6fedSRoopa Prabhu 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
20365e5d6fedSRoopa Prabhu 		flkeys = &_flkeys;
20375e5d6fedSRoopa Prabhu 
203823aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2039b4bac172SDavid Ahern 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
204006e9d040SJiri Benc 	skb_dst_drop(skb);
2041b75cc8f9SDavid Ahern 	skb_dst_set(skb,
2042b75cc8f9SDavid Ahern 		    ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
2043c71099acSThomas Graf }
2044c71099acSThomas Graf 
2045b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net,
2046b75cc8f9SDavid Ahern 					     struct fib6_table *table,
2047b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
2048b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2049b75cc8f9SDavid Ahern 					     int flags)
2050c71099acSThomas Graf {
2051b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2052c71099acSThomas Graf }
2053c71099acSThomas Graf 
20546f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
20556f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags)
2056c71099acSThomas Graf {
2057d46a9d67SDavid Ahern 	bool any_src;
2058c71099acSThomas Graf 
20594c1feac5SDavid Ahern 	if (rt6_need_strict(&fl6->daddr)) {
20604c1feac5SDavid Ahern 		struct dst_entry *dst;
20614c1feac5SDavid Ahern 
20624c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
2063ca254490SDavid Ahern 		if (dst)
2064ca254490SDavid Ahern 			return dst;
20654c1feac5SDavid Ahern 	}
2066ca254490SDavid Ahern 
20671fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
20684dc27d1cSDavid McCullough 
2069d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
2070741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2071d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
207277d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
2073c71099acSThomas Graf 
2074d46a9d67SDavid Ahern 	if (!any_src)
2075adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
20760c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
20770c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2078adaa70bbSThomas Graf 
2079b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
20801da177e4SLinus Torvalds }
20816f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
20821da177e4SLinus Torvalds 
20832774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
208414e50e57SDavid S. Miller {
20855c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
20861dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
208714e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
208814e50e57SDavid S. Miller 
20891dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
209062cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
209114e50e57SDavid S. Miller 	if (rt) {
20920a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
209381eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
20940a1f5962SMartin KaFai Lau 
2095d8d1f30bSChangli Gao 		new = &rt->dst;
209614e50e57SDavid S. Miller 		new->__use = 1;
2097352e512cSHerbert Xu 		new->input = dst_discard;
2098ede2059dSEric W. Biederman 		new->output = dst_discard_out;
209914e50e57SDavid S. Miller 
2100defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
210114e50e57SDavid S. Miller 
21021dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
21034e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
21040a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
210514e50e57SDavid S. Miller 
210614e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
210714e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
210814e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
210914e50e57SDavid S. Miller #endif
211014e50e57SDavid S. Miller 	}
211114e50e57SDavid S. Miller 
211269ead7afSDavid S. Miller 	dst_release(dst_orig);
211369ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
211414e50e57SDavid S. Miller }
211514e50e57SDavid S. Miller 
21161da177e4SLinus Torvalds /*
21171da177e4SLinus Torvalds  *	Destination cache support functions
21181da177e4SLinus Torvalds  */
21191da177e4SLinus Torvalds 
21208d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie)
212193531c67SDavid Ahern {
212293531c67SDavid Ahern 	u32 rt_cookie = 0;
212393531c67SDavid Ahern 
212493531c67SDavid Ahern 	if ((f6i && !rt6_get_cookie_safe(f6i, &rt_cookie)) ||
212593531c67SDavid Ahern 	     rt_cookie != cookie)
212693531c67SDavid Ahern 		return false;
212793531c67SDavid Ahern 
212893531c67SDavid Ahern 	if (fib6_check_expired(f6i))
212993531c67SDavid Ahern 		return false;
213093531c67SDavid Ahern 
213193531c67SDavid Ahern 	return true;
213293531c67SDavid Ahern }
213393531c67SDavid Ahern 
21343da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie)
21353da59bd9SMartin KaFai Lau {
213636143645SSteffen Klassert 	u32 rt_cookie = 0;
2137c5cff856SWei Wang 
213893531c67SDavid Ahern 	if ((rt->from && !rt6_get_cookie_safe(rt->from, &rt_cookie)) ||
213993531c67SDavid Ahern 	    rt_cookie != cookie)
21403da59bd9SMartin KaFai Lau 		return NULL;
21413da59bd9SMartin KaFai Lau 
21423da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
21433da59bd9SMartin KaFai Lau 		return NULL;
21443da59bd9SMartin KaFai Lau 
21453da59bd9SMartin KaFai Lau 	return &rt->dst;
21463da59bd9SMartin KaFai Lau }
21473da59bd9SMartin KaFai Lau 
21483da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie)
21493da59bd9SMartin KaFai Lau {
21505973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
21515973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
215293531c67SDavid Ahern 	    fib6_check(rt->from, cookie))
21533da59bd9SMartin KaFai Lau 		return &rt->dst;
21543da59bd9SMartin KaFai Lau 	else
21553da59bd9SMartin KaFai Lau 		return NULL;
21563da59bd9SMartin KaFai Lau }
21573da59bd9SMartin KaFai Lau 
21581da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
21591da177e4SLinus Torvalds {
21601da177e4SLinus Torvalds 	struct rt6_info *rt;
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
21631da177e4SLinus Torvalds 
21646f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
21656f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
21666f3118b5SNicolas Dichtel 	 * into this function always.
21676f3118b5SNicolas Dichtel 	 */
2168e3bc10bdSHannes Frederic Sowa 
216902bcf4e0SMartin KaFai Lau 	if (rt->rt6i_flags & RTF_PCPU ||
21703a2232e9SDavid Miller 	    (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from))
21713da59bd9SMartin KaFai Lau 		return rt6_dst_from_check(rt, cookie);
21723da59bd9SMartin KaFai Lau 	else
21733da59bd9SMartin KaFai Lau 		return rt6_check(rt, cookie);
21741da177e4SLinus Torvalds }
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
21771da177e4SLinus Torvalds {
21781da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 	if (rt) {
218154c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
218254c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
218393531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
218454c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
21851da177e4SLinus Torvalds 			}
218654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
218754c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
218854c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
218954c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
219054c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
219154c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
21921da177e4SLinus Torvalds }
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
21951da177e4SLinus Torvalds {
21961da177e4SLinus Torvalds 	struct rt6_info *rt;
21971da177e4SLinus Torvalds 
21983ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
21991da177e4SLinus Torvalds 
2200adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
22011da177e4SLinus Torvalds 	if (rt) {
22021eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
2203ad65a2f0SWei Wang 			if (dst_hold_safe(&rt->dst))
220493531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
220593531c67SDavid Ahern 		} else if (rt->from) {
2206c5cff856SWei Wang 			struct fib6_node *fn;
2207c5cff856SWei Wang 
2208c5cff856SWei Wang 			rcu_read_lock();
220993c2fb25SDavid Ahern 			fn = rcu_dereference(rt->from->fib6_node);
2210c5cff856SWei Wang 			if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2211c5cff856SWei Wang 				fn->fn_sernum = -1;
2212c5cff856SWei Wang 			rcu_read_unlock();
22131da177e4SLinus Torvalds 		}
22141da177e4SLinus Torvalds 	}
22151eb4f758SHannes Frederic Sowa }
22161da177e4SLinus Torvalds 
221745e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
221845e4fd26SMartin KaFai Lau {
221945e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
222045e4fd26SMartin KaFai Lau 
2221d4ead6b3SDavid Ahern 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
222245e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
222345e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
222445e4fd26SMartin KaFai Lau }
222545e4fd26SMartin KaFai Lau 
22260d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
22270d3f6d29SMartin KaFai Lau {
22280d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
222977634cc6SDavid Ahern 		(rt->rt6i_flags & RTF_PCPU || rt->from);
22300d3f6d29SMartin KaFai Lau }
22310d3f6d29SMartin KaFai Lau 
223245e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
223345e4fd26SMartin KaFai Lau 				 const struct ipv6hdr *iph, u32 mtu)
22341da177e4SLinus Torvalds {
22350dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
22361da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
22371da177e4SLinus Torvalds 
223845e4fd26SMartin KaFai Lau 	if (rt6->rt6i_flags & RTF_LOCAL)
223945e4fd26SMartin KaFai Lau 		return;
224045e4fd26SMartin KaFai Lau 
224119bda36cSXin Long 	if (dst_metric_locked(dst, RTAX_MTU))
224219bda36cSXin Long 		return;
224319bda36cSXin Long 
224445e4fd26SMartin KaFai Lau 	if (iph) {
224545e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
224645e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
224745e4fd26SMartin KaFai Lau 	} else if (sk) {
224845e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
224945e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
225045e4fd26SMartin KaFai Lau 	} else {
22510dec879fSJulian Anastasov 		daddr = NULL;
22520dec879fSJulian Anastasov 		saddr = NULL;
22531da177e4SLinus Torvalds 	}
22540dec879fSJulian Anastasov 	dst_confirm_neigh(dst, daddr);
22550dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
22560dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
22570dec879fSJulian Anastasov 		return;
22580dec879fSJulian Anastasov 
22590dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
22600dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
22612b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
22622b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
22632b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
22640dec879fSJulian Anastasov 	} else if (daddr) {
22650dec879fSJulian Anastasov 		struct rt6_info *nrt6;
22660dec879fSJulian Anastasov 
2267d4ead6b3SDavid Ahern 		nrt6 = ip6_rt_cache_alloc(rt6->from, daddr, saddr);
226845e4fd26SMartin KaFai Lau 		if (nrt6) {
226945e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
2270d4ead6b3SDavid Ahern 			if (rt6_insert_exception(nrt6, rt6->from))
22712b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
227245e4fd26SMartin KaFai Lau 		}
227345e4fd26SMartin KaFai Lau 	}
227445e4fd26SMartin KaFai Lau }
227545e4fd26SMartin KaFai Lau 
227645e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
227745e4fd26SMartin KaFai Lau 			       struct sk_buff *skb, u32 mtu)
227845e4fd26SMartin KaFai Lau {
227945e4fd26SMartin KaFai Lau 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
22801da177e4SLinus Torvalds }
22811da177e4SLinus Torvalds 
228242ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2283e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
228481aded24SDavid S. Miller {
228581aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
228681aded24SDavid S. Miller 	struct dst_entry *dst;
228781aded24SDavid S. Miller 	struct flowi6 fl6;
228881aded24SDavid S. Miller 
228981aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
229081aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
22911b3c61dcSLorenzo Colitti 	fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
229281aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
229381aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
22946502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
2295e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = uid;
229681aded24SDavid S. Miller 
229781aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
229881aded24SDavid S. Miller 	if (!dst->error)
229945e4fd26SMartin KaFai Lau 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
230081aded24SDavid S. Miller 	dst_release(dst);
230181aded24SDavid S. Miller }
230281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
230381aded24SDavid S. Miller 
230481aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
230581aded24SDavid S. Miller {
230633c162a9SMartin KaFai Lau 	struct dst_entry *dst;
230733c162a9SMartin KaFai Lau 
230881aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
2309e2d118a1SLorenzo Colitti 			sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid);
231033c162a9SMartin KaFai Lau 
231133c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
231233c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
231333c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
231433c162a9SMartin KaFai Lau 		return;
231533c162a9SMartin KaFai Lau 
231633c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
231733c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
231833c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
231933c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
232081aded24SDavid S. Miller }
232181aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
232281aded24SDavid S. Miller 
23237d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
23247d6850f7SAlexey Kodanev 			   const struct flowi6 *fl6)
23257d6850f7SAlexey Kodanev {
23267d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
23277d6850f7SAlexey Kodanev 	struct ipv6_pinfo *np = inet6_sk(sk);
23287d6850f7SAlexey Kodanev #endif
23297d6850f7SAlexey Kodanev 
23307d6850f7SAlexey Kodanev 	ip6_dst_store(sk, dst,
23317d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
23327d6850f7SAlexey Kodanev 		      &sk->sk_v6_daddr : NULL,
23337d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
23347d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
23357d6850f7SAlexey Kodanev 		      &np->saddr :
23367d6850f7SAlexey Kodanev #endif
23377d6850f7SAlexey Kodanev 		      NULL);
23387d6850f7SAlexey Kodanev }
23397d6850f7SAlexey Kodanev 
2340b55b76b2SDuan Jiong /* Handle redirects */
2341b55b76b2SDuan Jiong struct ip6rd_flowi {
2342b55b76b2SDuan Jiong 	struct flowi6 fl6;
2343b55b76b2SDuan Jiong 	struct in6_addr gateway;
2344b55b76b2SDuan Jiong };
2345b55b76b2SDuan Jiong 
2346b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
2347b55b76b2SDuan Jiong 					     struct fib6_table *table,
2348b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2349b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2350b55b76b2SDuan Jiong 					     int flags)
2351b55b76b2SDuan Jiong {
2352b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
235323fb93a4SDavid Ahern 	struct rt6_info *ret = NULL, *rt_cache;
23548d1c802bSDavid Ahern 	struct fib6_info *rt;
2355b55b76b2SDuan Jiong 	struct fib6_node *fn;
2356b55b76b2SDuan Jiong 
2357b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
235867c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2359b55b76b2SDuan Jiong 	 *
2360b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2361b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2362b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2363b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2364b55b76b2SDuan Jiong 	 * routes.
2365b55b76b2SDuan Jiong 	 */
2366b55b76b2SDuan Jiong 
236766f5d6ceSWei Wang 	rcu_read_lock();
2368b55b76b2SDuan Jiong 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2369b55b76b2SDuan Jiong restart:
237066f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
23715e670d84SDavid Ahern 		if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
23728067bb8cSIdo Schimmel 			continue;
237314895687SDavid Ahern 		if (fib6_check_expired(rt))
2374b55b76b2SDuan Jiong 			continue;
237593c2fb25SDavid Ahern 		if (rt->fib6_flags & RTF_REJECT)
2376b55b76b2SDuan Jiong 			break;
237793c2fb25SDavid Ahern 		if (!(rt->fib6_flags & RTF_GATEWAY))
2378b55b76b2SDuan Jiong 			continue;
23795e670d84SDavid Ahern 		if (fl6->flowi6_oif != rt->fib6_nh.nh_dev->ifindex)
2380b55b76b2SDuan Jiong 			continue;
23812b760fcfSWei Wang 		/* rt_cache's gateway might be different from its 'parent'
23822b760fcfSWei Wang 		 * in the case of an ip redirect.
23832b760fcfSWei Wang 		 * So we keep searching in the exception table if the gateway
23842b760fcfSWei Wang 		 * is different.
23852b760fcfSWei Wang 		 */
23865e670d84SDavid Ahern 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.nh_gw)) {
23872b760fcfSWei Wang 			rt_cache = rt6_find_cached_rt(rt,
23882b760fcfSWei Wang 						      &fl6->daddr,
23892b760fcfSWei Wang 						      &fl6->saddr);
23902b760fcfSWei Wang 			if (rt_cache &&
23912b760fcfSWei Wang 			    ipv6_addr_equal(&rdfl->gateway,
23922b760fcfSWei Wang 					    &rt_cache->rt6i_gateway)) {
239323fb93a4SDavid Ahern 				ret = rt_cache;
23942b760fcfSWei Wang 				break;
23952b760fcfSWei Wang 			}
2396b55b76b2SDuan Jiong 			continue;
23972b760fcfSWei Wang 		}
2398b55b76b2SDuan Jiong 		break;
2399b55b76b2SDuan Jiong 	}
2400b55b76b2SDuan Jiong 
2401b55b76b2SDuan Jiong 	if (!rt)
2402421842edSDavid Ahern 		rt = net->ipv6.fib6_null_entry;
240393c2fb25SDavid Ahern 	else if (rt->fib6_flags & RTF_REJECT) {
240423fb93a4SDavid Ahern 		ret = net->ipv6.ip6_null_entry;
2405b0a1ba59SMartin KaFai Lau 		goto out;
2406b0a1ba59SMartin KaFai Lau 	}
2407b0a1ba59SMartin KaFai Lau 
2408421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
2409a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2410a3c00e46SMartin KaFai Lau 		if (fn)
2411a3c00e46SMartin KaFai Lau 			goto restart;
2412b55b76b2SDuan Jiong 	}
2413a3c00e46SMartin KaFai Lau 
2414b0a1ba59SMartin KaFai Lau out:
241523fb93a4SDavid Ahern 	if (ret)
241623fb93a4SDavid Ahern 		dst_hold(&ret->dst);
241723fb93a4SDavid Ahern 	else
241823fb93a4SDavid Ahern 		ret = ip6_create_rt_rcu(rt);
2419b55b76b2SDuan Jiong 
242066f5d6ceSWei Wang 	rcu_read_unlock();
2421b55b76b2SDuan Jiong 
242223fb93a4SDavid Ahern 	trace_fib6_table_lookup(net, ret, table, fl6);
242323fb93a4SDavid Ahern 	return ret;
2424b55b76b2SDuan Jiong };
2425b55b76b2SDuan Jiong 
2426b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
2427b55b76b2SDuan Jiong 					    const struct flowi6 *fl6,
2428b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2429b55b76b2SDuan Jiong 					    const struct in6_addr *gateway)
2430b55b76b2SDuan Jiong {
2431b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2432b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
2433b55b76b2SDuan Jiong 
2434b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
2435b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
2436b55b76b2SDuan Jiong 
2437b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, &rdfl.fl6, skb,
2438b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
2439b55b76b2SDuan Jiong }
2440b55b76b2SDuan Jiong 
2441e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2442e2d118a1SLorenzo Colitti 		  kuid_t uid)
24433a5ad2eeSDavid S. Miller {
24443a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
24453a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
24463a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
24473a5ad2eeSDavid S. Miller 
24483a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2449e374c618SJulian Anastasov 	fl6.flowi6_iif = LOOPBACK_IFINDEX;
24503a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
24513a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
24523a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
24533a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
24546502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
2455e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = uid;
24563a5ad2eeSDavid S. Miller 
2457b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
24586700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
24593a5ad2eeSDavid S. Miller 	dst_release(dst);
24603a5ad2eeSDavid S. Miller }
24613a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
24623a5ad2eeSDavid S. Miller 
2463c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
2464c92a59ecSDuan Jiong 			    u32 mark)
2465c92a59ecSDuan Jiong {
2466c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2467c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2468c92a59ecSDuan Jiong 	struct dst_entry *dst;
2469c92a59ecSDuan Jiong 	struct flowi6 fl6;
2470c92a59ecSDuan Jiong 
2471c92a59ecSDuan Jiong 	memset(&fl6, 0, sizeof(fl6));
2472e374c618SJulian Anastasov 	fl6.flowi6_iif = LOOPBACK_IFINDEX;
2473c92a59ecSDuan Jiong 	fl6.flowi6_oif = oif;
2474c92a59ecSDuan Jiong 	fl6.flowi6_mark = mark;
2475c92a59ecSDuan Jiong 	fl6.daddr = msg->dest;
2476c92a59ecSDuan Jiong 	fl6.saddr = iph->daddr;
2477e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sock_net_uid(net, NULL);
2478c92a59ecSDuan Jiong 
2479b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
2480c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
2481c92a59ecSDuan Jiong 	dst_release(dst);
2482c92a59ecSDuan Jiong }
2483c92a59ecSDuan Jiong 
24843a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
24853a5ad2eeSDavid S. Miller {
2486e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2487e2d118a1SLorenzo Colitti 		     sk->sk_uid);
24883a5ad2eeSDavid S. Miller }
24893a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
24903a5ad2eeSDavid S. Miller 
24910dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
24921da177e4SLinus Torvalds {
24930dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
24940dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
24950dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
24960dbaee3bSDavid S. Miller 
24971da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
24981da177e4SLinus Torvalds 
24995578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
25005578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
25011da177e4SLinus Torvalds 
25021da177e4SLinus Torvalds 	/*
25031da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
25041da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
25051da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
25061da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
25071da177e4SLinus Torvalds 	 */
25081da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
25091da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
25101da177e4SLinus Torvalds 	return mtu;
25111da177e4SLinus Torvalds }
25121da177e4SLinus Torvalds 
2513ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
2514d33e4553SDavid S. Miller {
2515d33e4553SDavid S. Miller 	struct inet6_dev *idev;
2516d4ead6b3SDavid Ahern 	unsigned int mtu;
2517618f9bc7SSteffen Klassert 
25184b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
25194b32b5adSMartin KaFai Lau 	if (mtu)
25204b32b5adSMartin KaFai Lau 		goto out;
25214b32b5adSMartin KaFai Lau 
2522618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
2523d33e4553SDavid S. Miller 
2524d33e4553SDavid S. Miller 	rcu_read_lock();
2525d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
2526d33e4553SDavid S. Miller 	if (idev)
2527d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
2528d33e4553SDavid S. Miller 	rcu_read_unlock();
2529d33e4553SDavid S. Miller 
253030f78d8eSEric Dumazet out:
253114972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
253214972cbdSRoopa Prabhu 
253314972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
2534d33e4553SDavid S. Miller }
2535d33e4553SDavid S. Miller 
25363b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
253787a11578SDavid S. Miller 				  struct flowi6 *fl6)
25381da177e4SLinus Torvalds {
253987a11578SDavid S. Miller 	struct dst_entry *dst;
25401da177e4SLinus Torvalds 	struct rt6_info *rt;
25411da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
2542c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
25431da177e4SLinus Torvalds 
254438308473SDavid S. Miller 	if (unlikely(!idev))
2545122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
25461da177e4SLinus Torvalds 
2547ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
254838308473SDavid S. Miller 	if (unlikely(!rt)) {
25491da177e4SLinus Torvalds 		in6_dev_put(idev);
255087a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
25511da177e4SLinus Torvalds 		goto out;
25521da177e4SLinus Torvalds 	}
25531da177e4SLinus Torvalds 
25548e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
2555588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
25568e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
2557550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
255887a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
25598e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
25608e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
256114edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
25621da177e4SLinus Torvalds 
25634c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
2564587fea74SWei Wang 	 * do proper release of the net_device
2565587fea74SWei Wang 	 */
2566587fea74SWei Wang 	rt6_uncached_list_add(rt);
256781eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
25681da177e4SLinus Torvalds 
256987a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
257087a11578SDavid S. Miller 
25711da177e4SLinus Torvalds out:
257287a11578SDavid S. Miller 	return dst;
25731da177e4SLinus Torvalds }
25741da177e4SLinus Torvalds 
2575569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
25761da177e4SLinus Torvalds {
257786393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
25787019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
25797019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
25807019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
25817019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
25827019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
2583fc66f95cSEric Dumazet 	int entries;
25841da177e4SLinus Torvalds 
2585fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
258649a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
2587fc66f95cSEric Dumazet 	    entries <= rt_max_size)
25881da177e4SLinus Torvalds 		goto out;
25891da177e4SLinus Torvalds 
25906891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
259114956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
2592fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
2593fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
25947019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
25951da177e4SLinus Torvalds out:
25967019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
2597fc66f95cSEric Dumazet 	return entries > rt_max_size;
25981da177e4SLinus Torvalds }
25991da177e4SLinus Torvalds 
26008d1c802bSDavid Ahern static int ip6_convert_metrics(struct net *net, struct fib6_info *rt,
2601d4ead6b3SDavid Ahern 			       struct fib6_config *cfg)
2602e715b6d3SFlorian Westphal {
2603d4ead6b3SDavid Ahern 	int err = 0;
2604e715b6d3SFlorian Westphal 
2605d4ead6b3SDavid Ahern 	if (cfg->fc_mx) {
2606d4ead6b3SDavid Ahern 		rt->fib6_metrics = kzalloc(sizeof(*rt->fib6_metrics),
2607d4ead6b3SDavid Ahern 					   GFP_KERNEL);
2608d4ead6b3SDavid Ahern 		if (unlikely(!rt->fib6_metrics))
2609e715b6d3SFlorian Westphal 			return -ENOMEM;
2610e715b6d3SFlorian Westphal 
2611d4ead6b3SDavid Ahern 		refcount_set(&rt->fib6_metrics->refcnt, 1);
2612ea697639SDaniel Borkmann 
2613d4ead6b3SDavid Ahern 		err = ip_metrics_convert(net, cfg->fc_mx, cfg->fc_mx_len,
2614d4ead6b3SDavid Ahern 					 rt->fib6_metrics->metrics);
2615e715b6d3SFlorian Westphal 	}
2616e715b6d3SFlorian Westphal 
2617d4ead6b3SDavid Ahern 	return err;
2618e715b6d3SFlorian Westphal }
26191da177e4SLinus Torvalds 
26208c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net,
26218c14586fSDavid Ahern 					    struct fib6_config *cfg,
2622f4797b33SDavid Ahern 					    const struct in6_addr *gw_addr,
2623f4797b33SDavid Ahern 					    u32 tbid, int flags)
26248c14586fSDavid Ahern {
26258c14586fSDavid Ahern 	struct flowi6 fl6 = {
26268c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
26278c14586fSDavid Ahern 		.daddr = *gw_addr,
26288c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
26298c14586fSDavid Ahern 	};
26308c14586fSDavid Ahern 	struct fib6_table *table;
26318c14586fSDavid Ahern 	struct rt6_info *rt;
26328c14586fSDavid Ahern 
2633f4797b33SDavid Ahern 	table = fib6_get_table(net, tbid);
26348c14586fSDavid Ahern 	if (!table)
26358c14586fSDavid Ahern 		return NULL;
26368c14586fSDavid Ahern 
26378c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
26388c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
26398c14586fSDavid Ahern 
2640f4797b33SDavid Ahern 	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
2641b75cc8f9SDavid Ahern 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
26428c14586fSDavid Ahern 
26438c14586fSDavid Ahern 	/* if table lookup failed, fall back to full lookup */
26448c14586fSDavid Ahern 	if (rt == net->ipv6.ip6_null_entry) {
26458c14586fSDavid Ahern 		ip6_rt_put(rt);
26468c14586fSDavid Ahern 		rt = NULL;
26478c14586fSDavid Ahern 	}
26488c14586fSDavid Ahern 
26498c14586fSDavid Ahern 	return rt;
26508c14586fSDavid Ahern }
26518c14586fSDavid Ahern 
2652fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net,
2653fc1e64e1SDavid Ahern 				     struct fib6_config *cfg,
26549fbb704cSDavid Ahern 				     const struct net_device *dev,
2655fc1e64e1SDavid Ahern 				     struct netlink_ext_ack *extack)
2656fc1e64e1SDavid Ahern {
265744750f84SDavid Ahern 	u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
2658fc1e64e1SDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
2659fc1e64e1SDavid Ahern 	u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2660fc1e64e1SDavid Ahern 	struct rt6_info *grt;
2661fc1e64e1SDavid Ahern 	int err;
2662fc1e64e1SDavid Ahern 
2663fc1e64e1SDavid Ahern 	err = 0;
2664fc1e64e1SDavid Ahern 	grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2665fc1e64e1SDavid Ahern 	if (grt) {
266658e354c0SDavid Ahern 		if (!grt->dst.error &&
266758e354c0SDavid Ahern 		    (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
266844750f84SDavid Ahern 			NL_SET_ERR_MSG(extack,
266944750f84SDavid Ahern 				       "Nexthop has invalid gateway or device mismatch");
2670fc1e64e1SDavid Ahern 			err = -EINVAL;
2671fc1e64e1SDavid Ahern 		}
2672fc1e64e1SDavid Ahern 
2673fc1e64e1SDavid Ahern 		ip6_rt_put(grt);
2674fc1e64e1SDavid Ahern 	}
2675fc1e64e1SDavid Ahern 
2676fc1e64e1SDavid Ahern 	return err;
2677fc1e64e1SDavid Ahern }
2678fc1e64e1SDavid Ahern 
26791edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net,
26801edce99fSDavid Ahern 			      struct fib6_config *cfg,
26811edce99fSDavid Ahern 			      struct net_device **_dev,
26821edce99fSDavid Ahern 			      struct inet6_dev **idev)
26831edce99fSDavid Ahern {
26841edce99fSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
26851edce99fSDavid Ahern 	struct net_device *dev = _dev ? *_dev : NULL;
26861edce99fSDavid Ahern 	struct rt6_info *grt = NULL;
26871edce99fSDavid Ahern 	int err = -EHOSTUNREACH;
26881edce99fSDavid Ahern 
26891edce99fSDavid Ahern 	if (cfg->fc_table) {
2690f4797b33SDavid Ahern 		int flags = RT6_LOOKUP_F_IFACE;
2691f4797b33SDavid Ahern 
2692f4797b33SDavid Ahern 		grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2693f4797b33SDavid Ahern 					  cfg->fc_table, flags);
26941edce99fSDavid Ahern 		if (grt) {
26951edce99fSDavid Ahern 			if (grt->rt6i_flags & RTF_GATEWAY ||
26961edce99fSDavid Ahern 			    (dev && dev != grt->dst.dev)) {
26971edce99fSDavid Ahern 				ip6_rt_put(grt);
26981edce99fSDavid Ahern 				grt = NULL;
26991edce99fSDavid Ahern 			}
27001edce99fSDavid Ahern 		}
27011edce99fSDavid Ahern 	}
27021edce99fSDavid Ahern 
27031edce99fSDavid Ahern 	if (!grt)
2704b75cc8f9SDavid Ahern 		grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
27051edce99fSDavid Ahern 
27061edce99fSDavid Ahern 	if (!grt)
27071edce99fSDavid Ahern 		goto out;
27081edce99fSDavid Ahern 
27091edce99fSDavid Ahern 	if (dev) {
27101edce99fSDavid Ahern 		if (dev != grt->dst.dev) {
27111edce99fSDavid Ahern 			ip6_rt_put(grt);
27121edce99fSDavid Ahern 			goto out;
27131edce99fSDavid Ahern 		}
27141edce99fSDavid Ahern 	} else {
27151edce99fSDavid Ahern 		*_dev = dev = grt->dst.dev;
27161edce99fSDavid Ahern 		*idev = grt->rt6i_idev;
27171edce99fSDavid Ahern 		dev_hold(dev);
27181edce99fSDavid Ahern 		in6_dev_hold(grt->rt6i_idev);
27191edce99fSDavid Ahern 	}
27201edce99fSDavid Ahern 
27211edce99fSDavid Ahern 	if (!(grt->rt6i_flags & RTF_GATEWAY))
27221edce99fSDavid Ahern 		err = 0;
27231edce99fSDavid Ahern 
27241edce99fSDavid Ahern 	ip6_rt_put(grt);
27251edce99fSDavid Ahern 
27261edce99fSDavid Ahern out:
27271edce99fSDavid Ahern 	return err;
27281edce99fSDavid Ahern }
27291edce99fSDavid Ahern 
27309fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
27319fbb704cSDavid Ahern 			   struct net_device **_dev, struct inet6_dev **idev,
27329fbb704cSDavid Ahern 			   struct netlink_ext_ack *extack)
27339fbb704cSDavid Ahern {
27349fbb704cSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
27359fbb704cSDavid Ahern 	int gwa_type = ipv6_addr_type(gw_addr);
2736232378e8SDavid Ahern 	bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
27379fbb704cSDavid Ahern 	const struct net_device *dev = *_dev;
2738232378e8SDavid Ahern 	bool need_addr_check = !dev;
27399fbb704cSDavid Ahern 	int err = -EINVAL;
27409fbb704cSDavid Ahern 
27419fbb704cSDavid Ahern 	/* if gw_addr is local we will fail to detect this in case
27429fbb704cSDavid Ahern 	 * address is still TENTATIVE (DAD in progress). rt6_lookup()
27439fbb704cSDavid Ahern 	 * will return already-added prefix route via interface that
27449fbb704cSDavid Ahern 	 * prefix route was assigned to, which might be non-loopback.
27459fbb704cSDavid Ahern 	 */
2746232378e8SDavid Ahern 	if (dev &&
2747232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2748232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
27499fbb704cSDavid Ahern 		goto out;
27509fbb704cSDavid Ahern 	}
27519fbb704cSDavid Ahern 
27529fbb704cSDavid Ahern 	if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
27539fbb704cSDavid Ahern 		/* IPv6 strictly inhibits using not link-local
27549fbb704cSDavid Ahern 		 * addresses as nexthop address.
27559fbb704cSDavid Ahern 		 * Otherwise, router will not able to send redirects.
27569fbb704cSDavid Ahern 		 * It is very good, but in some (rare!) circumstances
27579fbb704cSDavid Ahern 		 * (SIT, PtP, NBMA NOARP links) it is handy to allow
27589fbb704cSDavid Ahern 		 * some exceptions. --ANK
27599fbb704cSDavid Ahern 		 * We allow IPv4-mapped nexthops to support RFC4798-type
27609fbb704cSDavid Ahern 		 * addressing
27619fbb704cSDavid Ahern 		 */
27629fbb704cSDavid Ahern 		if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
27639fbb704cSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
27649fbb704cSDavid Ahern 			goto out;
27659fbb704cSDavid Ahern 		}
27669fbb704cSDavid Ahern 
27679fbb704cSDavid Ahern 		if (cfg->fc_flags & RTNH_F_ONLINK)
27689fbb704cSDavid Ahern 			err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
27699fbb704cSDavid Ahern 		else
27709fbb704cSDavid Ahern 			err = ip6_route_check_nh(net, cfg, _dev, idev);
27719fbb704cSDavid Ahern 
27729fbb704cSDavid Ahern 		if (err)
27739fbb704cSDavid Ahern 			goto out;
27749fbb704cSDavid Ahern 	}
27759fbb704cSDavid Ahern 
27769fbb704cSDavid Ahern 	/* reload in case device was changed */
27779fbb704cSDavid Ahern 	dev = *_dev;
27789fbb704cSDavid Ahern 
27799fbb704cSDavid Ahern 	err = -EINVAL;
27809fbb704cSDavid Ahern 	if (!dev) {
27819fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Egress device not specified");
27829fbb704cSDavid Ahern 		goto out;
27839fbb704cSDavid Ahern 	} else if (dev->flags & IFF_LOOPBACK) {
27849fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack,
27859fbb704cSDavid Ahern 			       "Egress device can not be loopback device for this route");
27869fbb704cSDavid Ahern 		goto out;
27879fbb704cSDavid Ahern 	}
2788232378e8SDavid Ahern 
2789232378e8SDavid Ahern 	/* if we did not check gw_addr above, do so now that the
2790232378e8SDavid Ahern 	 * egress device has been resolved.
2791232378e8SDavid Ahern 	 */
2792232378e8SDavid Ahern 	if (need_addr_check &&
2793232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2794232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
2795232378e8SDavid Ahern 		goto out;
2796232378e8SDavid Ahern 	}
2797232378e8SDavid Ahern 
27989fbb704cSDavid Ahern 	err = 0;
27999fbb704cSDavid Ahern out:
28009fbb704cSDavid Ahern 	return err;
28019fbb704cSDavid Ahern }
28029fbb704cSDavid Ahern 
28038d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
2804acb54e3cSDavid Ahern 					      gfp_t gfp_flags,
2805333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
28061da177e4SLinus Torvalds {
28075578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
28088d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
28091da177e4SLinus Torvalds 	struct net_device *dev = NULL;
28101da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
2811c71099acSThomas Graf 	struct fib6_table *table;
28121da177e4SLinus Torvalds 	int addr_type;
28138c5b83f0SRoopa Prabhu 	int err = -EINVAL;
28141da177e4SLinus Torvalds 
2815557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
2816d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
2817d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
2818557c44beSDavid Ahern 		goto out;
2819d5d531cbSDavid Ahern 	}
2820557c44beSDavid Ahern 
28212ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
28222ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
28232ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
28242ea2352eSWei Wang 		goto out;
28252ea2352eSWei Wang 	}
28262ea2352eSWei Wang 
2827e8478e80SDavid Ahern 	if (cfg->fc_type > RTN_MAX) {
2828e8478e80SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid route type");
2829e8478e80SDavid Ahern 		goto out;
2830e8478e80SDavid Ahern 	}
2831e8478e80SDavid Ahern 
2832d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
2833d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
28348c5b83f0SRoopa Prabhu 		goto out;
2835d5d531cbSDavid Ahern 	}
2836d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
2837d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
2838d5d531cbSDavid Ahern 		goto out;
2839d5d531cbSDavid Ahern 	}
28401da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
2841d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
2842d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
2843d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
28448c5b83f0SRoopa Prabhu 		goto out;
2845d5d531cbSDavid Ahern 	}
28461da177e4SLinus Torvalds #endif
284786872cb5SThomas Graf 	if (cfg->fc_ifindex) {
28481da177e4SLinus Torvalds 		err = -ENODEV;
28495578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
28501da177e4SLinus Torvalds 		if (!dev)
28511da177e4SLinus Torvalds 			goto out;
28521da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
28531da177e4SLinus Torvalds 		if (!idev)
28541da177e4SLinus Torvalds 			goto out;
28551da177e4SLinus Torvalds 	}
28561da177e4SLinus Torvalds 
285786872cb5SThomas Graf 	if (cfg->fc_metric == 0)
285886872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
28591da177e4SLinus Torvalds 
2860fc1e64e1SDavid Ahern 	if (cfg->fc_flags & RTNH_F_ONLINK) {
2861fc1e64e1SDavid Ahern 		if (!dev) {
2862fc1e64e1SDavid Ahern 			NL_SET_ERR_MSG(extack,
2863fc1e64e1SDavid Ahern 				       "Nexthop device required for onlink");
2864fc1e64e1SDavid Ahern 			err = -ENODEV;
2865fc1e64e1SDavid Ahern 			goto out;
2866fc1e64e1SDavid Ahern 		}
2867fc1e64e1SDavid Ahern 
2868fc1e64e1SDavid Ahern 		if (!(dev->flags & IFF_UP)) {
2869fc1e64e1SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2870fc1e64e1SDavid Ahern 			err = -ENETDOWN;
2871fc1e64e1SDavid Ahern 			goto out;
2872fc1e64e1SDavid Ahern 		}
2873fc1e64e1SDavid Ahern 	}
2874fc1e64e1SDavid Ahern 
2875c71099acSThomas Graf 	err = -ENOBUFS;
287638308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
2877d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
2878d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
287938308473SDavid S. Miller 		if (!table) {
2880f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
2881d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
2882d71314b4SMatti Vaittinen 		}
2883d71314b4SMatti Vaittinen 	} else {
2884d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
2885d71314b4SMatti Vaittinen 	}
288638308473SDavid S. Miller 
288738308473SDavid S. Miller 	if (!table)
2888c71099acSThomas Graf 		goto out;
2889c71099acSThomas Graf 
28901da177e4SLinus Torvalds 	err = -ENOMEM;
289193531c67SDavid Ahern 	rt = fib6_info_alloc(gfp_flags);
289293531c67SDavid Ahern 	if (!rt)
28931da177e4SLinus Torvalds 		goto out;
289493531c67SDavid Ahern 
289593531c67SDavid Ahern 	if (cfg->fc_flags & RTF_ADDRCONF)
289693531c67SDavid Ahern 		rt->dst_nocount = true;
28971da177e4SLinus Torvalds 
2898d4ead6b3SDavid Ahern 	err = ip6_convert_metrics(net, rt, cfg);
2899d4ead6b3SDavid Ahern 	if (err < 0)
2900d4ead6b3SDavid Ahern 		goto out;
2901d4ead6b3SDavid Ahern 
29021716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
290314895687SDavid Ahern 		fib6_set_expires(rt, jiffies +
29041716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
29051716a961SGao feng 	else
290614895687SDavid Ahern 		fib6_clean_expires(rt);
29071da177e4SLinus Torvalds 
290886872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
290986872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
291093c2fb25SDavid Ahern 	rt->fib6_protocol = cfg->fc_protocol;
291186872cb5SThomas Graf 
291286872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
29131da177e4SLinus Torvalds 
291419e42e45SRoopa Prabhu 	if (cfg->fc_encap) {
291519e42e45SRoopa Prabhu 		struct lwtunnel_state *lwtstate;
291619e42e45SRoopa Prabhu 
291730357d7dSDavid Ahern 		err = lwtunnel_build_state(cfg->fc_encap_type,
2918127eb7cdSTom Herbert 					   cfg->fc_encap, AF_INET6, cfg,
29199ae28727SDavid Ahern 					   &lwtstate, extack);
292019e42e45SRoopa Prabhu 		if (err)
292119e42e45SRoopa Prabhu 			goto out;
29225e670d84SDavid Ahern 		rt->fib6_nh.nh_lwtstate = lwtstate_get(lwtstate);
292325368623STom Herbert 	}
292419e42e45SRoopa Prabhu 
292593c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
292693c2fb25SDavid Ahern 	rt->fib6_dst.plen = cfg->fc_dst_len;
292793c2fb25SDavid Ahern 	if (rt->fib6_dst.plen == 128)
29283b6761d1SDavid Ahern 		rt->dst_host = true;
29291da177e4SLinus Torvalds 
29301da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
293193c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
293293c2fb25SDavid Ahern 	rt->fib6_src.plen = cfg->fc_src_len;
29331da177e4SLinus Torvalds #endif
29341da177e4SLinus Torvalds 
293593c2fb25SDavid Ahern 	rt->fib6_metric = cfg->fc_metric;
29365e670d84SDavid Ahern 	rt->fib6_nh.nh_weight = 1;
29371da177e4SLinus Torvalds 
2938e8478e80SDavid Ahern 	rt->fib6_type = cfg->fc_type;
2939e8478e80SDavid Ahern 
29401da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
29411da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
29421da177e4SLinus Torvalds 	 */
294386872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
294438308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
294538308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
294638308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
29471da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
29485578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
29491da177e4SLinus Torvalds 			if (dev) {
29501da177e4SLinus Torvalds 				dev_put(dev);
29511da177e4SLinus Torvalds 				in6_dev_put(idev);
29521da177e4SLinus Torvalds 			}
29535578689aSDaniel Lezcano 			dev = net->loopback_dev;
29541da177e4SLinus Torvalds 			dev_hold(dev);
29551da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
29561da177e4SLinus Torvalds 			if (!idev) {
29571da177e4SLinus Torvalds 				err = -ENODEV;
29581da177e4SLinus Torvalds 				goto out;
29591da177e4SLinus Torvalds 			}
29601da177e4SLinus Torvalds 		}
296193c2fb25SDavid Ahern 		rt->fib6_flags = RTF_REJECT|RTF_NONEXTHOP;
29621da177e4SLinus Torvalds 		goto install_route;
29631da177e4SLinus Torvalds 	}
29641da177e4SLinus Torvalds 
296586872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
29669fbb704cSDavid Ahern 		err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
29671da177e4SLinus Torvalds 		if (err)
29681da177e4SLinus Torvalds 			goto out;
29699fbb704cSDavid Ahern 
297093531c67SDavid Ahern 		rt->fib6_nh.nh_gw = cfg->fc_gateway;
29711da177e4SLinus Torvalds 	}
29721da177e4SLinus Torvalds 
29731da177e4SLinus Torvalds 	err = -ENODEV;
297438308473SDavid S. Miller 	if (!dev)
29751da177e4SLinus Torvalds 		goto out;
29761da177e4SLinus Torvalds 
2977428604fbSLorenzo Bianconi 	if (idev->cnf.disable_ipv6) {
2978428604fbSLorenzo Bianconi 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
2979428604fbSLorenzo Bianconi 		err = -EACCES;
2980428604fbSLorenzo Bianconi 		goto out;
2981428604fbSLorenzo Bianconi 	}
2982428604fbSLorenzo Bianconi 
2983955ec4cbSDavid Ahern 	if (!(dev->flags & IFF_UP)) {
2984955ec4cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2985955ec4cbSDavid Ahern 		err = -ENETDOWN;
2986955ec4cbSDavid Ahern 		goto out;
2987955ec4cbSDavid Ahern 	}
2988955ec4cbSDavid Ahern 
2989c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
2990c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
2991d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
2992c3968a85SDaniel Walter 			err = -EINVAL;
2993c3968a85SDaniel Walter 			goto out;
2994c3968a85SDaniel Walter 		}
299593c2fb25SDavid Ahern 		rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
299693c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 128;
2997c3968a85SDaniel Walter 	} else
299893c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
2999c3968a85SDaniel Walter 
300093c2fb25SDavid Ahern 	rt->fib6_flags = cfg->fc_flags;
30011da177e4SLinus Torvalds 
30021da177e4SLinus Torvalds install_route:
300393c2fb25SDavid Ahern 	if (!(rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
30045609b80aSIdo Schimmel 	    !netif_carrier_ok(dev))
30055e670d84SDavid Ahern 		rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
30065e670d84SDavid Ahern 	rt->fib6_nh.nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK);
300793531c67SDavid Ahern 	rt->fib6_nh.nh_dev = dev;
300893c2fb25SDavid Ahern 	rt->fib6_idev = idev;
300993c2fb25SDavid Ahern 	rt->fib6_table = table;
301063152fc0SDaniel Lezcano 
3011c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
301263152fc0SDaniel Lezcano 
30138c5b83f0SRoopa Prabhu 	return rt;
30141da177e4SLinus Torvalds out:
30151da177e4SLinus Torvalds 	if (dev)
30161da177e4SLinus Torvalds 		dev_put(dev);
30171da177e4SLinus Torvalds 	if (idev)
30181da177e4SLinus Torvalds 		in6_dev_put(idev);
30196b9ea5a6SRoopa Prabhu 
302093531c67SDavid Ahern 	fib6_info_release(rt);
30218c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
30226b9ea5a6SRoopa Prabhu }
30236b9ea5a6SRoopa Prabhu 
3024acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3025acb54e3cSDavid Ahern 		  struct netlink_ext_ack *extack)
30266b9ea5a6SRoopa Prabhu {
30278d1c802bSDavid Ahern 	struct fib6_info *rt;
30286b9ea5a6SRoopa Prabhu 	int err;
30296b9ea5a6SRoopa Prabhu 
3030acb54e3cSDavid Ahern 	rt = ip6_route_info_create(cfg, gfp_flags, extack);
3031d4ead6b3SDavid Ahern 	if (IS_ERR(rt))
3032d4ead6b3SDavid Ahern 		return PTR_ERR(rt);
30336b9ea5a6SRoopa Prabhu 
3034d4ead6b3SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
303593531c67SDavid Ahern 	fib6_info_release(rt);
30366b9ea5a6SRoopa Prabhu 
30371da177e4SLinus Torvalds 	return err;
30381da177e4SLinus Torvalds }
30391da177e4SLinus Torvalds 
30408d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
30411da177e4SLinus Torvalds {
3042afb1d4b5SDavid Ahern 	struct net *net = info->nl_net;
3043c71099acSThomas Graf 	struct fib6_table *table;
3044afb1d4b5SDavid Ahern 	int err;
30451da177e4SLinus Torvalds 
3046421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
30476825a26cSGao feng 		err = -ENOENT;
30486825a26cSGao feng 		goto out;
30496825a26cSGao feng 	}
30506c813a72SPatrick McHardy 
305193c2fb25SDavid Ahern 	table = rt->fib6_table;
305266f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
305386872cb5SThomas Graf 	err = fib6_del(rt, info);
305466f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
30551da177e4SLinus Torvalds 
30566825a26cSGao feng out:
305793531c67SDavid Ahern 	fib6_info_release(rt);
30581da177e4SLinus Torvalds 	return err;
30591da177e4SLinus Torvalds }
30601da177e4SLinus Torvalds 
30618d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt)
3062e0a1ad73SThomas Graf {
3063afb1d4b5SDavid Ahern 	struct nl_info info = { .nl_net = net };
3064afb1d4b5SDavid Ahern 
3065528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
3066e0a1ad73SThomas Graf }
3067e0a1ad73SThomas Graf 
30688d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
30690ae81335SDavid Ahern {
30700ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
3071e3330039SWANG Cong 	struct net *net = info->nl_net;
307216a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
30730ae81335SDavid Ahern 	struct fib6_table *table;
3074e3330039SWANG Cong 	int err = -ENOENT;
30750ae81335SDavid Ahern 
3076421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
3077e3330039SWANG Cong 		goto out_put;
307893c2fb25SDavid Ahern 	table = rt->fib6_table;
307966f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
30800ae81335SDavid Ahern 
308193c2fb25SDavid Ahern 	if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
30828d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
30830ae81335SDavid Ahern 
308416a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
308516a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
308616a16cd3SDavid Ahern 		if (skb) {
308716a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
308816a16cd3SDavid Ahern 
3089d4ead6b3SDavid Ahern 			if (rt6_fill_node(net, skb, rt, NULL,
309016a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
309116a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
309216a16cd3SDavid Ahern 				kfree_skb(skb);
309316a16cd3SDavid Ahern 				skb = NULL;
309416a16cd3SDavid Ahern 			} else
309516a16cd3SDavid Ahern 				info->skip_notify = 1;
309616a16cd3SDavid Ahern 		}
309716a16cd3SDavid Ahern 
30980ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
309993c2fb25SDavid Ahern 					 &rt->fib6_siblings,
310093c2fb25SDavid Ahern 					 fib6_siblings) {
31010ae81335SDavid Ahern 			err = fib6_del(sibling, info);
31020ae81335SDavid Ahern 			if (err)
3103e3330039SWANG Cong 				goto out_unlock;
31040ae81335SDavid Ahern 		}
31050ae81335SDavid Ahern 	}
31060ae81335SDavid Ahern 
31070ae81335SDavid Ahern 	err = fib6_del(rt, info);
3108e3330039SWANG Cong out_unlock:
310966f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
3110e3330039SWANG Cong out_put:
311193531c67SDavid Ahern 	fib6_info_release(rt);
311216a16cd3SDavid Ahern 
311316a16cd3SDavid Ahern 	if (skb) {
3114e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
311516a16cd3SDavid Ahern 			    info->nlh, gfp_any());
311616a16cd3SDavid Ahern 	}
31170ae81335SDavid Ahern 	return err;
31180ae81335SDavid Ahern }
31190ae81335SDavid Ahern 
312023fb93a4SDavid Ahern static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
312123fb93a4SDavid Ahern {
312223fb93a4SDavid Ahern 	int rc = -ESRCH;
312323fb93a4SDavid Ahern 
312423fb93a4SDavid Ahern 	if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
312523fb93a4SDavid Ahern 		goto out;
312623fb93a4SDavid Ahern 
312723fb93a4SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY &&
312823fb93a4SDavid Ahern 	    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
312923fb93a4SDavid Ahern 		goto out;
313023fb93a4SDavid Ahern 	if (dst_hold_safe(&rt->dst))
313123fb93a4SDavid Ahern 		rc = rt6_remove_exception_rt(rt);
313223fb93a4SDavid Ahern out:
313323fb93a4SDavid Ahern 	return rc;
313423fb93a4SDavid Ahern }
313523fb93a4SDavid Ahern 
3136333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
3137333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
31381da177e4SLinus Torvalds {
31398d1c802bSDavid Ahern 	struct rt6_info *rt_cache;
3140c71099acSThomas Graf 	struct fib6_table *table;
31418d1c802bSDavid Ahern 	struct fib6_info *rt;
31421da177e4SLinus Torvalds 	struct fib6_node *fn;
31431da177e4SLinus Torvalds 	int err = -ESRCH;
31441da177e4SLinus Torvalds 
31455578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3146d5d531cbSDavid Ahern 	if (!table) {
3147d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
3148c71099acSThomas Graf 		return err;
3149d5d531cbSDavid Ahern 	}
31501da177e4SLinus Torvalds 
315166f5d6ceSWei Wang 	rcu_read_lock();
3152c71099acSThomas Graf 
3153c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
315486872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
315538fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
31562b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
31571da177e4SLinus Torvalds 
31581da177e4SLinus Torvalds 	if (fn) {
315966f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
31602b760fcfSWei Wang 			if (cfg->fc_flags & RTF_CACHE) {
316123fb93a4SDavid Ahern 				int rc;
316223fb93a4SDavid Ahern 
31632b760fcfSWei Wang 				rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
31642b760fcfSWei Wang 							      &cfg->fc_src);
316523fb93a4SDavid Ahern 				if (rt_cache) {
316623fb93a4SDavid Ahern 					rc = ip6_del_cached_rt(rt_cache, cfg);
316723fb93a4SDavid Ahern 					if (rc != -ESRCH)
316823fb93a4SDavid Ahern 						return rc;
316923fb93a4SDavid Ahern 				}
31701f56a01fSMartin KaFai Lau 				continue;
31712b760fcfSWei Wang 			}
317286872cb5SThomas Graf 			if (cfg->fc_ifindex &&
31735e670d84SDavid Ahern 			    (!rt->fib6_nh.nh_dev ||
31745e670d84SDavid Ahern 			     rt->fib6_nh.nh_dev->ifindex != cfg->fc_ifindex))
31751da177e4SLinus Torvalds 				continue;
317686872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
31775e670d84SDavid Ahern 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->fib6_nh.nh_gw))
31781da177e4SLinus Torvalds 				continue;
317993c2fb25SDavid Ahern 			if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
31801da177e4SLinus Torvalds 				continue;
318193c2fb25SDavid Ahern 			if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
3182c2ed1880SMantas M 				continue;
318393531c67SDavid Ahern 			fib6_info_hold(rt);
318466f5d6ceSWei Wang 			rcu_read_unlock();
31851da177e4SLinus Torvalds 
31860ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
31870ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
318886872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
31890ae81335SDavid Ahern 
31900ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
31911da177e4SLinus Torvalds 		}
31921da177e4SLinus Torvalds 	}
319366f5d6ceSWei Wang 	rcu_read_unlock();
31941da177e4SLinus Torvalds 
31951da177e4SLinus Torvalds 	return err;
31961da177e4SLinus Torvalds }
31971da177e4SLinus Torvalds 
31986700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3199a6279458SYOSHIFUJI Hideaki {
3200a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
3201e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
3202e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
3203e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
3204e8599ff4SDavid S. Miller 	struct neighbour *neigh;
320571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
32066e157b6aSDavid S. Miller 	int optlen, on_link;
32076e157b6aSDavid S. Miller 	u8 *lladdr;
3208e8599ff4SDavid S. Miller 
320929a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
321071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
3211e8599ff4SDavid S. Miller 
3212e8599ff4SDavid S. Miller 	if (optlen < 0) {
32136e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3214e8599ff4SDavid S. Miller 		return;
3215e8599ff4SDavid S. Miller 	}
3216e8599ff4SDavid S. Miller 
321771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
3218e8599ff4SDavid S. Miller 
321971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
32206e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3221e8599ff4SDavid S. Miller 		return;
3222e8599ff4SDavid S. Miller 	}
3223e8599ff4SDavid S. Miller 
32246e157b6aSDavid S. Miller 	on_link = 0;
322571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
3226e8599ff4SDavid S. Miller 		on_link = 1;
322771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
3228e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
32296e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
3230e8599ff4SDavid S. Miller 		return;
3231e8599ff4SDavid S. Miller 	}
3232e8599ff4SDavid S. Miller 
3233e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
3234e8599ff4SDavid S. Miller 	if (!in6_dev)
3235e8599ff4SDavid S. Miller 		return;
3236e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3237e8599ff4SDavid S. Miller 		return;
3238e8599ff4SDavid S. Miller 
3239e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
3240e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
3241e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
3242e8599ff4SDavid S. Miller 	 */
3243e8599ff4SDavid S. Miller 
3244f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
3245e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3246e8599ff4SDavid S. Miller 		return;
3247e8599ff4SDavid S. Miller 	}
32486e157b6aSDavid S. Miller 
32496e157b6aSDavid S. Miller 	lladdr = NULL;
3250e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
3251e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3252e8599ff4SDavid S. Miller 					     skb->dev);
3253e8599ff4SDavid S. Miller 		if (!lladdr) {
3254e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3255e8599ff4SDavid S. Miller 			return;
3256e8599ff4SDavid S. Miller 		}
3257e8599ff4SDavid S. Miller 	}
3258e8599ff4SDavid S. Miller 
32596e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
3260ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
32616e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
32626e157b6aSDavid S. Miller 		return;
32636e157b6aSDavid S. Miller 	}
32646e157b6aSDavid S. Miller 
32656e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
32666e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
32676e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
32686e157b6aSDavid S. Miller 	 */
32690dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
32706e157b6aSDavid S. Miller 
327171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
3272e8599ff4SDavid S. Miller 	if (!neigh)
3273e8599ff4SDavid S. Miller 		return;
3274e8599ff4SDavid S. Miller 
32751da177e4SLinus Torvalds 	/*
32761da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
32771da177e4SLinus Torvalds 	 */
32781da177e4SLinus Torvalds 
3279f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
32801da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
32811da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
32821da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
3283f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
3284f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
32851da177e4SLinus Torvalds 
328623fb93a4SDavid Ahern 	nrt = ip6_rt_cache_alloc(rt->from, &msg->dest, NULL);
328738308473SDavid S. Miller 	if (!nrt)
32881da177e4SLinus Torvalds 		goto out;
32891da177e4SLinus Torvalds 
32901da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
32911da177e4SLinus Torvalds 	if (on_link)
32921da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
32931da177e4SLinus Torvalds 
32944e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
32951da177e4SLinus Torvalds 
32962b760fcfSWei Wang 	/* No need to remove rt from the exception table if rt is
32972b760fcfSWei Wang 	 * a cached route because rt6_insert_exception() will
32982b760fcfSWei Wang 	 * takes care of it
32992b760fcfSWei Wang 	 */
3300d4ead6b3SDavid Ahern 	if (rt6_insert_exception(nrt, rt->from)) {
33012b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
33022b760fcfSWei Wang 		goto out;
33032b760fcfSWei Wang 	}
33041da177e4SLinus Torvalds 
3305d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
3306d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
330771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
330860592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
33098d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
33108d71740cSTom Tucker 
33111da177e4SLinus Torvalds out:
3312e8599ff4SDavid S. Miller 	neigh_release(neigh);
33136e157b6aSDavid S. Miller }
33146e157b6aSDavid S. Miller 
331570ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
33168d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
3317b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3318830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3319830218c1SDavid Ahern 					   struct net_device *dev)
332070ceb4f5SYOSHIFUJI Hideaki {
3321830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3322830218c1SDavid Ahern 	int ifindex = dev->ifindex;
332370ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
33248d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3325c71099acSThomas Graf 	struct fib6_table *table;
332670ceb4f5SYOSHIFUJI Hideaki 
3327830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
332838308473SDavid S. Miller 	if (!table)
3329c71099acSThomas Graf 		return NULL;
3330c71099acSThomas Graf 
333166f5d6ceSWei Wang 	rcu_read_lock();
333238fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
333370ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
333470ceb4f5SYOSHIFUJI Hideaki 		goto out;
333570ceb4f5SYOSHIFUJI Hideaki 
333666f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
33375e670d84SDavid Ahern 		if (rt->fib6_nh.nh_dev->ifindex != ifindex)
333870ceb4f5SYOSHIFUJI Hideaki 			continue;
333993c2fb25SDavid Ahern 		if ((rt->fib6_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
334070ceb4f5SYOSHIFUJI Hideaki 			continue;
33415e670d84SDavid Ahern 		if (!ipv6_addr_equal(&rt->fib6_nh.nh_gw, gwaddr))
334270ceb4f5SYOSHIFUJI Hideaki 			continue;
33438d1c802bSDavid Ahern 		fib6_info_hold(rt);
334470ceb4f5SYOSHIFUJI Hideaki 		break;
334570ceb4f5SYOSHIFUJI Hideaki 	}
334670ceb4f5SYOSHIFUJI Hideaki out:
334766f5d6ceSWei Wang 	rcu_read_unlock();
334870ceb4f5SYOSHIFUJI Hideaki 	return rt;
334970ceb4f5SYOSHIFUJI Hideaki }
335070ceb4f5SYOSHIFUJI Hideaki 
33518d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
3352b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3353830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3354830218c1SDavid Ahern 					   struct net_device *dev,
335595c96174SEric Dumazet 					   unsigned int pref)
335670ceb4f5SYOSHIFUJI Hideaki {
335786872cb5SThomas Graf 	struct fib6_config cfg = {
3358238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
3359830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
336086872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
336186872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
336286872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
3363b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3364e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
336515e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
3366efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3367efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
336886872cb5SThomas Graf 	};
336970ceb4f5SYOSHIFUJI Hideaki 
3370830218c1SDavid Ahern 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
33714e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
33724e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
337386872cb5SThomas Graf 
3374e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
3375e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
337686872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
337770ceb4f5SYOSHIFUJI Hideaki 
3378acb54e3cSDavid Ahern 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
337970ceb4f5SYOSHIFUJI Hideaki 
3380830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
338170ceb4f5SYOSHIFUJI Hideaki }
338270ceb4f5SYOSHIFUJI Hideaki #endif
338370ceb4f5SYOSHIFUJI Hideaki 
33848d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net,
3385afb1d4b5SDavid Ahern 				     const struct in6_addr *addr,
3386afb1d4b5SDavid Ahern 				     struct net_device *dev)
33871da177e4SLinus Torvalds {
3388830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
33898d1c802bSDavid Ahern 	struct fib6_info *rt;
3390c71099acSThomas Graf 	struct fib6_table *table;
33911da177e4SLinus Torvalds 
3392afb1d4b5SDavid Ahern 	table = fib6_get_table(net, tb_id);
339338308473SDavid S. Miller 	if (!table)
3394c71099acSThomas Graf 		return NULL;
33951da177e4SLinus Torvalds 
339666f5d6ceSWei Wang 	rcu_read_lock();
339766f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
33985e670d84SDavid Ahern 		if (dev == rt->fib6_nh.nh_dev &&
339993c2fb25SDavid Ahern 		    ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
34005e670d84SDavid Ahern 		    ipv6_addr_equal(&rt->fib6_nh.nh_gw, addr))
34011da177e4SLinus Torvalds 			break;
34021da177e4SLinus Torvalds 	}
34031da177e4SLinus Torvalds 	if (rt)
34048d1c802bSDavid Ahern 		fib6_info_hold(rt);
340566f5d6ceSWei Wang 	rcu_read_unlock();
34061da177e4SLinus Torvalds 	return rt;
34071da177e4SLinus Torvalds }
34081da177e4SLinus Torvalds 
34098d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net,
3410afb1d4b5SDavid Ahern 				     const struct in6_addr *gwaddr,
3411ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
3412ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
34131da177e4SLinus Torvalds {
341486872cb5SThomas Graf 	struct fib6_config cfg = {
3415ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
3416238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
341786872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
341886872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
341986872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
3420b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3421e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
342215e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
34235578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3424afb1d4b5SDavid Ahern 		.fc_nlinfo.nl_net = net,
342586872cb5SThomas Graf 	};
34261da177e4SLinus Torvalds 
34274e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
34281da177e4SLinus Torvalds 
3429acb54e3cSDavid Ahern 	if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
3430830218c1SDavid Ahern 		struct fib6_table *table;
3431830218c1SDavid Ahern 
3432830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
3433830218c1SDavid Ahern 		if (table)
3434830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3435830218c1SDavid Ahern 	}
34361da177e4SLinus Torvalds 
3437afb1d4b5SDavid Ahern 	return rt6_get_dflt_router(net, gwaddr, dev);
34381da177e4SLinus Torvalds }
34391da177e4SLinus Torvalds 
3440afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net,
3441afb1d4b5SDavid Ahern 				     struct fib6_table *table)
34421da177e4SLinus Torvalds {
34438d1c802bSDavid Ahern 	struct fib6_info *rt;
34441da177e4SLinus Torvalds 
34451da177e4SLinus Torvalds restart:
344666f5d6ceSWei Wang 	rcu_read_lock();
344766f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
344893c2fb25SDavid Ahern 		if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
344993c2fb25SDavid Ahern 		    (!rt->fib6_idev || rt->fib6_idev->cnf.accept_ra != 2)) {
345093531c67SDavid Ahern 			fib6_info_hold(rt);
345166f5d6ceSWei Wang 			rcu_read_unlock();
3452afb1d4b5SDavid Ahern 			ip6_del_rt(net, rt);
34531da177e4SLinus Torvalds 			goto restart;
34541da177e4SLinus Torvalds 		}
34551da177e4SLinus Torvalds 	}
345666f5d6ceSWei Wang 	rcu_read_unlock();
3457830218c1SDavid Ahern 
3458830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3459830218c1SDavid Ahern }
3460830218c1SDavid Ahern 
3461830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
3462830218c1SDavid Ahern {
3463830218c1SDavid Ahern 	struct fib6_table *table;
3464830218c1SDavid Ahern 	struct hlist_head *head;
3465830218c1SDavid Ahern 	unsigned int h;
3466830218c1SDavid Ahern 
3467830218c1SDavid Ahern 	rcu_read_lock();
3468830218c1SDavid Ahern 
3469830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3470830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
3471830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3472830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3473afb1d4b5SDavid Ahern 				__rt6_purge_dflt_routers(net, table);
3474830218c1SDavid Ahern 		}
3475830218c1SDavid Ahern 	}
3476830218c1SDavid Ahern 
3477830218c1SDavid Ahern 	rcu_read_unlock();
34781da177e4SLinus Torvalds }
34791da177e4SLinus Torvalds 
34805578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
34815578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
348286872cb5SThomas Graf 				 struct fib6_config *cfg)
348386872cb5SThomas Graf {
348486872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
348586872cb5SThomas Graf 
3486ca254490SDavid Ahern 	cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
3487ca254490SDavid Ahern 			 : RT6_TABLE_MAIN;
348886872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
348986872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
349086872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
349186872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
349286872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
349386872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
3494e8478e80SDavid Ahern 	cfg->fc_type = rtmsg->rtmsg_type;
349586872cb5SThomas Graf 
34965578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
3497f1243c2dSBenjamin Thery 
34984e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
34994e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
35004e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
350186872cb5SThomas Graf }
350286872cb5SThomas Graf 
35035578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
35041da177e4SLinus Torvalds {
350586872cb5SThomas Graf 	struct fib6_config cfg;
35061da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
35071da177e4SLinus Torvalds 	int err;
35081da177e4SLinus Torvalds 
35091da177e4SLinus Torvalds 	switch (cmd) {
35101da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
35111da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
3512af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
35131da177e4SLinus Torvalds 			return -EPERM;
35141da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
35151da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
35161da177e4SLinus Torvalds 		if (err)
35171da177e4SLinus Torvalds 			return -EFAULT;
35181da177e4SLinus Torvalds 
35195578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
352086872cb5SThomas Graf 
35211da177e4SLinus Torvalds 		rtnl_lock();
35221da177e4SLinus Torvalds 		switch (cmd) {
35231da177e4SLinus Torvalds 		case SIOCADDRT:
3524acb54e3cSDavid Ahern 			err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
35251da177e4SLinus Torvalds 			break;
35261da177e4SLinus Torvalds 		case SIOCDELRT:
3527333c4301SDavid Ahern 			err = ip6_route_del(&cfg, NULL);
35281da177e4SLinus Torvalds 			break;
35291da177e4SLinus Torvalds 		default:
35301da177e4SLinus Torvalds 			err = -EINVAL;
35311da177e4SLinus Torvalds 		}
35321da177e4SLinus Torvalds 		rtnl_unlock();
35331da177e4SLinus Torvalds 
35341da177e4SLinus Torvalds 		return err;
35353ff50b79SStephen Hemminger 	}
35361da177e4SLinus Torvalds 
35371da177e4SLinus Torvalds 	return -EINVAL;
35381da177e4SLinus Torvalds }
35391da177e4SLinus Torvalds 
35401da177e4SLinus Torvalds /*
35411da177e4SLinus Torvalds  *	Drop the packet on the floor
35421da177e4SLinus Torvalds  */
35431da177e4SLinus Torvalds 
3544d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
35451da177e4SLinus Torvalds {
3546612f09e8SYOSHIFUJI Hideaki 	int type;
3547adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
3548612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
3549612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
35500660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
355145bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
3552bdb7cc64SStephen Suryaputra 			IP6_INC_STATS(dev_net(dst->dev),
3553bdb7cc64SStephen Suryaputra 				      __in6_dev_get_safely(skb->dev),
35543bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
3555612f09e8SYOSHIFUJI Hideaki 			break;
3556612f09e8SYOSHIFUJI Hideaki 		}
3557612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
3558612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
35593bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
35603bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
3561612f09e8SYOSHIFUJI Hideaki 		break;
3562612f09e8SYOSHIFUJI Hideaki 	}
35633ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
35641da177e4SLinus Torvalds 	kfree_skb(skb);
35651da177e4SLinus Torvalds 	return 0;
35661da177e4SLinus Torvalds }
35671da177e4SLinus Torvalds 
35689ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
35699ce8ade0SThomas Graf {
3570612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
35719ce8ade0SThomas Graf }
35729ce8ade0SThomas Graf 
3573ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
35741da177e4SLinus Torvalds {
3575adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3576612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
35771da177e4SLinus Torvalds }
35781da177e4SLinus Torvalds 
35799ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
35809ce8ade0SThomas Graf {
3581612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
35829ce8ade0SThomas Graf }
35839ce8ade0SThomas Graf 
3584ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
35859ce8ade0SThomas Graf {
3586adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3587612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
35889ce8ade0SThomas Graf }
35899ce8ade0SThomas Graf 
35901da177e4SLinus Torvalds /*
35911da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
35921da177e4SLinus Torvalds  */
35931da177e4SLinus Torvalds 
3594*360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net,
3595afb1d4b5SDavid Ahern 				     struct inet6_dev *idev,
35961da177e4SLinus Torvalds 				     const struct in6_addr *addr,
3597acb54e3cSDavid Ahern 				     bool anycast, gfp_t gfp_flags)
35981da177e4SLinus Torvalds {
3599ca254490SDavid Ahern 	u32 tb_id;
36004832c30dSDavid Ahern 	struct net_device *dev = idev->dev;
3601*360a9887SDavid Ahern 	struct fib6_info *f6i;
36025f02ce24SDavid Ahern 
3603*360a9887SDavid Ahern 	f6i = fib6_info_alloc(gfp_flags);
3604*360a9887SDavid Ahern 	if (!f6i)
36051da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
36061da177e4SLinus Torvalds 
3607*360a9887SDavid Ahern 	f6i->dst_nocount = true;
36083b6761d1SDavid Ahern 
36091da177e4SLinus Torvalds 	in6_dev_hold(idev);
3610*360a9887SDavid Ahern 	f6i->fib6_idev = idev;
36111da177e4SLinus Torvalds 
3612*360a9887SDavid Ahern 	f6i->dst_host = true;
3613*360a9887SDavid Ahern 	f6i->fib6_protocol = RTPROT_KERNEL;
3614*360a9887SDavid Ahern 	f6i->fib6_flags = RTF_UP | RTF_NONEXTHOP;
3615e8478e80SDavid Ahern 	if (anycast) {
3616*360a9887SDavid Ahern 		f6i->fib6_type = RTN_ANYCAST;
3617*360a9887SDavid Ahern 		f6i->fib6_flags |= RTF_ANYCAST;
3618e8478e80SDavid Ahern 	} else {
3619*360a9887SDavid Ahern 		f6i->fib6_type = RTN_LOCAL;
3620*360a9887SDavid Ahern 		f6i->fib6_flags |= RTF_LOCAL;
3621e8478e80SDavid Ahern 	}
36221da177e4SLinus Torvalds 
3623*360a9887SDavid Ahern 	f6i->fib6_nh.nh_gw = *addr;
362493531c67SDavid Ahern 	dev_hold(dev);
3625*360a9887SDavid Ahern 	f6i->fib6_nh.nh_dev = dev;
3626*360a9887SDavid Ahern 	f6i->fib6_dst.addr = *addr;
3627*360a9887SDavid Ahern 	f6i->fib6_dst.plen = 128;
3628ca254490SDavid Ahern 	tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
3629*360a9887SDavid Ahern 	f6i->fib6_table = fib6_get_table(net, tb_id);
36301da177e4SLinus Torvalds 
3631*360a9887SDavid Ahern 	return f6i;
36321da177e4SLinus Torvalds }
36331da177e4SLinus Torvalds 
3634c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
3635c3968a85SDaniel Walter struct arg_dev_net_ip {
3636c3968a85SDaniel Walter 	struct net_device *dev;
3637c3968a85SDaniel Walter 	struct net *net;
3638c3968a85SDaniel Walter 	struct in6_addr *addr;
3639c3968a85SDaniel Walter };
3640c3968a85SDaniel Walter 
36418d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
3642c3968a85SDaniel Walter {
3643c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3644c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3645c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3646c3968a85SDaniel Walter 
36475e670d84SDavid Ahern 	if (((void *)rt->fib6_nh.nh_dev == dev || !dev) &&
3648421842edSDavid Ahern 	    rt != net->ipv6.fib6_null_entry &&
364993c2fb25SDavid Ahern 	    ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
365060006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
3651c3968a85SDaniel Walter 		/* remove prefsrc entry */
365293c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
365360006a48SWei Wang 		/* need to update cache as well */
365460006a48SWei Wang 		rt6_exceptions_remove_prefsrc(rt);
365560006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
3656c3968a85SDaniel Walter 	}
3657c3968a85SDaniel Walter 	return 0;
3658c3968a85SDaniel Walter }
3659c3968a85SDaniel Walter 
3660c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3661c3968a85SDaniel Walter {
3662c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
3663c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
3664c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
3665c3968a85SDaniel Walter 		.net = net,
3666c3968a85SDaniel Walter 		.addr = &ifp->addr,
3667c3968a85SDaniel Walter 	};
36680c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
3669c3968a85SDaniel Walter }
3670c3968a85SDaniel Walter 
3671be7a010dSDuan Jiong #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
3672be7a010dSDuan Jiong 
3673be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
36748d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
3675be7a010dSDuan Jiong {
3676be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
3677be7a010dSDuan Jiong 
367893c2fb25SDavid Ahern 	if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
36795e670d84SDavid Ahern 	    ipv6_addr_equal(gateway, &rt->fib6_nh.nh_gw)) {
3680be7a010dSDuan Jiong 		return -1;
3681be7a010dSDuan Jiong 	}
3682b16cb459SWei Wang 
3683b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
3684b16cb459SWei Wang 	 * This is needed because cached route may have a different
3685b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
3686b16cb459SWei Wang 	 */
3687b16cb459SWei Wang 	rt6_exceptions_clean_tohost(rt, gateway);
3688b16cb459SWei Wang 
3689be7a010dSDuan Jiong 	return 0;
3690be7a010dSDuan Jiong }
3691be7a010dSDuan Jiong 
3692be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3693be7a010dSDuan Jiong {
3694be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
3695be7a010dSDuan Jiong }
3696be7a010dSDuan Jiong 
36972127d95aSIdo Schimmel struct arg_netdev_event {
36982127d95aSIdo Schimmel 	const struct net_device *dev;
36994c981e28SIdo Schimmel 	union {
37002127d95aSIdo Schimmel 		unsigned int nh_flags;
37014c981e28SIdo Schimmel 		unsigned long event;
37024c981e28SIdo Schimmel 	};
37032127d95aSIdo Schimmel };
37042127d95aSIdo Schimmel 
37058d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
3706d7dedee1SIdo Schimmel {
37078d1c802bSDavid Ahern 	struct fib6_info *iter;
3708d7dedee1SIdo Schimmel 	struct fib6_node *fn;
3709d7dedee1SIdo Schimmel 
371093c2fb25SDavid Ahern 	fn = rcu_dereference_protected(rt->fib6_node,
371193c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3712d7dedee1SIdo Schimmel 	iter = rcu_dereference_protected(fn->leaf,
371393c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3714d7dedee1SIdo Schimmel 	while (iter) {
371593c2fb25SDavid Ahern 		if (iter->fib6_metric == rt->fib6_metric &&
3716d7dedee1SIdo Schimmel 		    rt6_qualify_for_ecmp(iter))
3717d7dedee1SIdo Schimmel 			return iter;
3718d7dedee1SIdo Schimmel 		iter = rcu_dereference_protected(iter->rt6_next,
371993c2fb25SDavid Ahern 				lockdep_is_held(&rt->fib6_table->tb6_lock));
3720d7dedee1SIdo Schimmel 	}
3721d7dedee1SIdo Schimmel 
3722d7dedee1SIdo Schimmel 	return NULL;
3723d7dedee1SIdo Schimmel }
3724d7dedee1SIdo Schimmel 
37258d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt)
3726d7dedee1SIdo Schimmel {
37275e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_DEAD ||
37285e670d84SDavid Ahern 	    (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
372993c2fb25SDavid Ahern 	     rt->fib6_idev->cnf.ignore_routes_with_linkdown))
3730d7dedee1SIdo Schimmel 		return true;
3731d7dedee1SIdo Schimmel 
3732d7dedee1SIdo Schimmel 	return false;
3733d7dedee1SIdo Schimmel }
3734d7dedee1SIdo Schimmel 
37358d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt)
3736d7dedee1SIdo Schimmel {
37378d1c802bSDavid Ahern 	struct fib6_info *iter;
3738d7dedee1SIdo Schimmel 	int total = 0;
3739d7dedee1SIdo Schimmel 
3740d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt))
37415e670d84SDavid Ahern 		total += rt->fib6_nh.nh_weight;
3742d7dedee1SIdo Schimmel 
374393c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
3744d7dedee1SIdo Schimmel 		if (!rt6_is_dead(iter))
37455e670d84SDavid Ahern 			total += iter->fib6_nh.nh_weight;
3746d7dedee1SIdo Schimmel 	}
3747d7dedee1SIdo Schimmel 
3748d7dedee1SIdo Schimmel 	return total;
3749d7dedee1SIdo Schimmel }
3750d7dedee1SIdo Schimmel 
37518d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
3752d7dedee1SIdo Schimmel {
3753d7dedee1SIdo Schimmel 	int upper_bound = -1;
3754d7dedee1SIdo Schimmel 
3755d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt)) {
37565e670d84SDavid Ahern 		*weight += rt->fib6_nh.nh_weight;
3757d7dedee1SIdo Schimmel 		upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
3758d7dedee1SIdo Schimmel 						    total) - 1;
3759d7dedee1SIdo Schimmel 	}
37605e670d84SDavid Ahern 	atomic_set(&rt->fib6_nh.nh_upper_bound, upper_bound);
3761d7dedee1SIdo Schimmel }
3762d7dedee1SIdo Schimmel 
37638d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
3764d7dedee1SIdo Schimmel {
37658d1c802bSDavid Ahern 	struct fib6_info *iter;
3766d7dedee1SIdo Schimmel 	int weight = 0;
3767d7dedee1SIdo Schimmel 
3768d7dedee1SIdo Schimmel 	rt6_upper_bound_set(rt, &weight, total);
3769d7dedee1SIdo Schimmel 
377093c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
3771d7dedee1SIdo Schimmel 		rt6_upper_bound_set(iter, &weight, total);
3772d7dedee1SIdo Schimmel }
3773d7dedee1SIdo Schimmel 
37748d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt)
3775d7dedee1SIdo Schimmel {
37768d1c802bSDavid Ahern 	struct fib6_info *first;
3777d7dedee1SIdo Schimmel 	int total;
3778d7dedee1SIdo Schimmel 
3779d7dedee1SIdo Schimmel 	/* In case the entire multipath route was marked for flushing,
3780d7dedee1SIdo Schimmel 	 * then there is no need to rebalance upon the removal of every
3781d7dedee1SIdo Schimmel 	 * sibling route.
3782d7dedee1SIdo Schimmel 	 */
378393c2fb25SDavid Ahern 	if (!rt->fib6_nsiblings || rt->should_flush)
3784d7dedee1SIdo Schimmel 		return;
3785d7dedee1SIdo Schimmel 
3786d7dedee1SIdo Schimmel 	/* During lookup routes are evaluated in order, so we need to
3787d7dedee1SIdo Schimmel 	 * make sure upper bounds are assigned from the first sibling
3788d7dedee1SIdo Schimmel 	 * onwards.
3789d7dedee1SIdo Schimmel 	 */
3790d7dedee1SIdo Schimmel 	first = rt6_multipath_first_sibling(rt);
3791d7dedee1SIdo Schimmel 	if (WARN_ON_ONCE(!first))
3792d7dedee1SIdo Schimmel 		return;
3793d7dedee1SIdo Schimmel 
3794d7dedee1SIdo Schimmel 	total = rt6_multipath_total_weight(first);
3795d7dedee1SIdo Schimmel 	rt6_multipath_upper_bound_set(first, total);
3796d7dedee1SIdo Schimmel }
3797d7dedee1SIdo Schimmel 
37988d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg)
37992127d95aSIdo Schimmel {
38002127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
38017aef6859SDavid Ahern 	struct net *net = dev_net(arg->dev);
38022127d95aSIdo Schimmel 
3803421842edSDavid Ahern 	if (rt != net->ipv6.fib6_null_entry && rt->fib6_nh.nh_dev == arg->dev) {
38045e670d84SDavid Ahern 		rt->fib6_nh.nh_flags &= ~arg->nh_flags;
38057aef6859SDavid Ahern 		fib6_update_sernum_upto_root(net, rt);
3806d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
38071de178edSIdo Schimmel 	}
38082127d95aSIdo Schimmel 
38092127d95aSIdo Schimmel 	return 0;
38102127d95aSIdo Schimmel }
38112127d95aSIdo Schimmel 
38122127d95aSIdo Schimmel void rt6_sync_up(struct net_device *dev, unsigned int nh_flags)
38132127d95aSIdo Schimmel {
38142127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
38152127d95aSIdo Schimmel 		.dev = dev,
38166802f3adSIdo Schimmel 		{
38172127d95aSIdo Schimmel 			.nh_flags = nh_flags,
38186802f3adSIdo Schimmel 		},
38192127d95aSIdo Schimmel 	};
38202127d95aSIdo Schimmel 
38212127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
38222127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
38232127d95aSIdo Schimmel 
38242127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
38252127d95aSIdo Schimmel }
38262127d95aSIdo Schimmel 
38278d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
38281de178edSIdo Schimmel 				   const struct net_device *dev)
38291de178edSIdo Schimmel {
38308d1c802bSDavid Ahern 	struct fib6_info *iter;
38311de178edSIdo Schimmel 
38325e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == dev)
38331de178edSIdo Schimmel 		return true;
383493c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
38355e670d84SDavid Ahern 		if (iter->fib6_nh.nh_dev == dev)
38361de178edSIdo Schimmel 			return true;
38371de178edSIdo Schimmel 
38381de178edSIdo Schimmel 	return false;
38391de178edSIdo Schimmel }
38401de178edSIdo Schimmel 
38418d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt)
38421de178edSIdo Schimmel {
38438d1c802bSDavid Ahern 	struct fib6_info *iter;
38441de178edSIdo Schimmel 
38451de178edSIdo Schimmel 	rt->should_flush = 1;
384693c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
38471de178edSIdo Schimmel 		iter->should_flush = 1;
38481de178edSIdo Schimmel }
38491de178edSIdo Schimmel 
38508d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
38511de178edSIdo Schimmel 					     const struct net_device *down_dev)
38521de178edSIdo Schimmel {
38538d1c802bSDavid Ahern 	struct fib6_info *iter;
38541de178edSIdo Schimmel 	unsigned int dead = 0;
38551de178edSIdo Schimmel 
38565e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == down_dev ||
38575e670d84SDavid Ahern 	    rt->fib6_nh.nh_flags & RTNH_F_DEAD)
38581de178edSIdo Schimmel 		dead++;
385993c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
38605e670d84SDavid Ahern 		if (iter->fib6_nh.nh_dev == down_dev ||
38615e670d84SDavid Ahern 		    iter->fib6_nh.nh_flags & RTNH_F_DEAD)
38621de178edSIdo Schimmel 			dead++;
38631de178edSIdo Schimmel 
38641de178edSIdo Schimmel 	return dead;
38651de178edSIdo Schimmel }
38661de178edSIdo Schimmel 
38678d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
38681de178edSIdo Schimmel 				       const struct net_device *dev,
38691de178edSIdo Schimmel 				       unsigned int nh_flags)
38701de178edSIdo Schimmel {
38718d1c802bSDavid Ahern 	struct fib6_info *iter;
38721de178edSIdo Schimmel 
38735e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == dev)
38745e670d84SDavid Ahern 		rt->fib6_nh.nh_flags |= nh_flags;
387593c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
38765e670d84SDavid Ahern 		if (iter->fib6_nh.nh_dev == dev)
38775e670d84SDavid Ahern 			iter->fib6_nh.nh_flags |= nh_flags;
38781de178edSIdo Schimmel }
38791de178edSIdo Schimmel 
3880a1a22c12SDavid Ahern /* called with write lock held for table with rt */
38818d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
38821da177e4SLinus Torvalds {
38834c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
38844c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
38857aef6859SDavid Ahern 	struct net *net = dev_net(dev);
38868ed67789SDaniel Lezcano 
3887421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
388827c6fa73SIdo Schimmel 		return 0;
388927c6fa73SIdo Schimmel 
389027c6fa73SIdo Schimmel 	switch (arg->event) {
389127c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
38925e670d84SDavid Ahern 		return rt->fib6_nh.nh_dev == dev ? -1 : 0;
389327c6fa73SIdo Schimmel 	case NETDEV_DOWN:
38941de178edSIdo Schimmel 		if (rt->should_flush)
389527c6fa73SIdo Schimmel 			return -1;
389693c2fb25SDavid Ahern 		if (!rt->fib6_nsiblings)
38975e670d84SDavid Ahern 			return rt->fib6_nh.nh_dev == dev ? -1 : 0;
38981de178edSIdo Schimmel 		if (rt6_multipath_uses_dev(rt, dev)) {
38991de178edSIdo Schimmel 			unsigned int count;
39001de178edSIdo Schimmel 
39011de178edSIdo Schimmel 			count = rt6_multipath_dead_count(rt, dev);
390293c2fb25SDavid Ahern 			if (rt->fib6_nsiblings + 1 == count) {
39031de178edSIdo Schimmel 				rt6_multipath_flush(rt);
39041de178edSIdo Schimmel 				return -1;
39051de178edSIdo Schimmel 			}
39061de178edSIdo Schimmel 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
39071de178edSIdo Schimmel 						   RTNH_F_LINKDOWN);
39087aef6859SDavid Ahern 			fib6_update_sernum(net, rt);
3909d7dedee1SIdo Schimmel 			rt6_multipath_rebalance(rt);
39101de178edSIdo Schimmel 		}
39111de178edSIdo Schimmel 		return -2;
391227c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
39135e670d84SDavid Ahern 		if (rt->fib6_nh.nh_dev != dev ||
391493c2fb25SDavid Ahern 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
391527c6fa73SIdo Schimmel 			break;
39165e670d84SDavid Ahern 		rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
3917d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
391827c6fa73SIdo Schimmel 		break;
39192b241361SIdo Schimmel 	}
3920c159d30cSDavid S. Miller 
39211da177e4SLinus Torvalds 	return 0;
39221da177e4SLinus Torvalds }
39231da177e4SLinus Torvalds 
392427c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
39251da177e4SLinus Torvalds {
39264c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
39278ed67789SDaniel Lezcano 		.dev = dev,
39286802f3adSIdo Schimmel 		{
39294c981e28SIdo Schimmel 			.event = event,
39306802f3adSIdo Schimmel 		},
39318ed67789SDaniel Lezcano 	};
39328ed67789SDaniel Lezcano 
39334c981e28SIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifdown, &arg);
39344c981e28SIdo Schimmel }
39354c981e28SIdo Schimmel 
39364c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
39374c981e28SIdo Schimmel {
39384c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
39394c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
39404c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
39411da177e4SLinus Torvalds }
39421da177e4SLinus Torvalds 
394395c96174SEric Dumazet struct rt6_mtu_change_arg {
39441da177e4SLinus Torvalds 	struct net_device *dev;
394595c96174SEric Dumazet 	unsigned int mtu;
39461da177e4SLinus Torvalds };
39471da177e4SLinus Torvalds 
39488d1c802bSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg)
39491da177e4SLinus Torvalds {
39501da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
39511da177e4SLinus Torvalds 	struct inet6_dev *idev;
39521da177e4SLinus Torvalds 
39531da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
39541da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
39551da177e4SLinus Torvalds 	   We still use this lock to block changes
39561da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
39571da177e4SLinus Torvalds 	*/
39581da177e4SLinus Torvalds 
39591da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
396038308473SDavid S. Miller 	if (!idev)
39611da177e4SLinus Torvalds 		return 0;
39621da177e4SLinus Torvalds 
39631da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
39641da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
39651da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
39661da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
39671da177e4SLinus Torvalds 	 */
39685e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == arg->dev &&
3969d4ead6b3SDavid Ahern 	    !fib6_metric_locked(rt, RTAX_MTU)) {
3970d4ead6b3SDavid Ahern 		u32 mtu = rt->fib6_pmtu;
3971d4ead6b3SDavid Ahern 
3972d4ead6b3SDavid Ahern 		if (mtu >= arg->mtu ||
3973d4ead6b3SDavid Ahern 		    (mtu < arg->mtu && mtu == idev->cnf.mtu6))
3974d4ead6b3SDavid Ahern 			fib6_metric_set(rt, RTAX_MTU, arg->mtu);
3975d4ead6b3SDavid Ahern 
3976f5bbe7eeSWei Wang 		spin_lock_bh(&rt6_exception_lock);
3977e9fa1495SStefano Brivio 		rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
3978f5bbe7eeSWei Wang 		spin_unlock_bh(&rt6_exception_lock);
39794b32b5adSMartin KaFai Lau 	}
39801da177e4SLinus Torvalds 	return 0;
39811da177e4SLinus Torvalds }
39821da177e4SLinus Torvalds 
398395c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
39841da177e4SLinus Torvalds {
3985c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
3986c71099acSThomas Graf 		.dev = dev,
3987c71099acSThomas Graf 		.mtu = mtu,
3988c71099acSThomas Graf 	};
39891da177e4SLinus Torvalds 
39900c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
39911da177e4SLinus Torvalds }
39921da177e4SLinus Torvalds 
3993ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
39945176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
399586872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
3996ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
399786872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
399886872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
399951ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
4000c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
400119e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
400219e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
400332bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
4004622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
40053b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
400686872cb5SThomas Graf };
400786872cb5SThomas Graf 
400886872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4009333c4301SDavid Ahern 			      struct fib6_config *cfg,
4010333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
40111da177e4SLinus Torvalds {
401286872cb5SThomas Graf 	struct rtmsg *rtm;
401386872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
4014c78ba6d6SLubomir Rintel 	unsigned int pref;
401586872cb5SThomas Graf 	int err;
40161da177e4SLinus Torvalds 
4017fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4018fceb6435SJohannes Berg 			  NULL);
401986872cb5SThomas Graf 	if (err < 0)
402086872cb5SThomas Graf 		goto errout;
40211da177e4SLinus Torvalds 
402286872cb5SThomas Graf 	err = -EINVAL;
402386872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
402486872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
402586872cb5SThomas Graf 
402686872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
402786872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
402886872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
402986872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
403086872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
4031ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
403286872cb5SThomas Graf 
4033ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
4034ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
4035b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
4036b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
403786872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
403886872cb5SThomas Graf 
4039ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
4040ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
4041ab79ad14SMaciej Żenczykowski 
40421f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
40431f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
40441f56a01fSMartin KaFai Lau 
4045fc1e64e1SDavid Ahern 	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4046fc1e64e1SDavid Ahern 
404715e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
404886872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
40493b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
405086872cb5SThomas Graf 
405186872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
405267b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
405386872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
40541da177e4SLinus Torvalds 	}
405586872cb5SThomas Graf 
405686872cb5SThomas Graf 	if (tb[RTA_DST]) {
405786872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
405886872cb5SThomas Graf 
405986872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
406086872cb5SThomas Graf 			goto errout;
406186872cb5SThomas Graf 
406286872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
40631da177e4SLinus Torvalds 	}
406486872cb5SThomas Graf 
406586872cb5SThomas Graf 	if (tb[RTA_SRC]) {
406686872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
406786872cb5SThomas Graf 
406886872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
406986872cb5SThomas Graf 			goto errout;
407086872cb5SThomas Graf 
407186872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
40721da177e4SLinus Torvalds 	}
407386872cb5SThomas Graf 
4074c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
407567b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4076c3968a85SDaniel Walter 
407786872cb5SThomas Graf 	if (tb[RTA_OIF])
407886872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
407986872cb5SThomas Graf 
408086872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
408186872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
408286872cb5SThomas Graf 
408386872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
408486872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
408586872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
40861da177e4SLinus Torvalds 	}
408786872cb5SThomas Graf 
408886872cb5SThomas Graf 	if (tb[RTA_TABLE])
408986872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
409086872cb5SThomas Graf 
409151ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
409251ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
409351ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
40949ed59592SDavid Ahern 
40959ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4096c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
40979ed59592SDavid Ahern 		if (err < 0)
40989ed59592SDavid Ahern 			goto errout;
409951ebd318SNicolas Dichtel 	}
410051ebd318SNicolas Dichtel 
4101c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
4102c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
4103c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
4104c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
4105c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
4106c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
4107c78ba6d6SLubomir Rintel 	}
4108c78ba6d6SLubomir Rintel 
410919e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
411019e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
411119e42e45SRoopa Prabhu 
41129ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
411319e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
411419e42e45SRoopa Prabhu 
4115c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
41169ed59592SDavid Ahern 		if (err < 0)
41179ed59592SDavid Ahern 			goto errout;
41189ed59592SDavid Ahern 	}
41199ed59592SDavid Ahern 
412032bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
412132bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
412232bc201eSXin Long 
412332bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
412432bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
412532bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
412632bc201eSXin Long 		}
412732bc201eSXin Long 	}
412832bc201eSXin Long 
412986872cb5SThomas Graf 	err = 0;
413086872cb5SThomas Graf errout:
413186872cb5SThomas Graf 	return err;
41321da177e4SLinus Torvalds }
41331da177e4SLinus Torvalds 
41346b9ea5a6SRoopa Prabhu struct rt6_nh {
41358d1c802bSDavid Ahern 	struct fib6_info *fib6_info;
41366b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
41376b9ea5a6SRoopa Prabhu 	struct list_head next;
41386b9ea5a6SRoopa Prabhu };
41396b9ea5a6SRoopa Prabhu 
41406b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
41416b9ea5a6SRoopa Prabhu {
41426b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
41436b9ea5a6SRoopa Prabhu 
41446b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
41457d4d5065SDavid Ahern 		pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n",
41466b9ea5a6SRoopa Prabhu 		        &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
41476b9ea5a6SRoopa Prabhu 		        nh->r_cfg.fc_ifindex);
41486b9ea5a6SRoopa Prabhu 	}
41496b9ea5a6SRoopa Prabhu }
41506b9ea5a6SRoopa Prabhu 
4151d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net,
4152d4ead6b3SDavid Ahern 				 struct list_head *rt6_nh_list,
41538d1c802bSDavid Ahern 				 struct fib6_info *rt,
41548d1c802bSDavid Ahern 				 struct fib6_config *r_cfg)
41556b9ea5a6SRoopa Prabhu {
41566b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
41576b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
41586b9ea5a6SRoopa Prabhu 
41596b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
41608d1c802bSDavid Ahern 		/* check if fib6_info already exists */
41618d1c802bSDavid Ahern 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
41626b9ea5a6SRoopa Prabhu 			return err;
41636b9ea5a6SRoopa Prabhu 	}
41646b9ea5a6SRoopa Prabhu 
41656b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
41666b9ea5a6SRoopa Prabhu 	if (!nh)
41676b9ea5a6SRoopa Prabhu 		return -ENOMEM;
41688d1c802bSDavid Ahern 	nh->fib6_info = rt;
4169d4ead6b3SDavid Ahern 	err = ip6_convert_metrics(net, rt, r_cfg);
41706b9ea5a6SRoopa Prabhu 	if (err) {
41716b9ea5a6SRoopa Prabhu 		kfree(nh);
41726b9ea5a6SRoopa Prabhu 		return err;
41736b9ea5a6SRoopa Prabhu 	}
41746b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
41756b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
41766b9ea5a6SRoopa Prabhu 
41776b9ea5a6SRoopa Prabhu 	return 0;
41786b9ea5a6SRoopa Prabhu }
41796b9ea5a6SRoopa Prabhu 
41808d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt,
41818d1c802bSDavid Ahern 				   struct fib6_info *rt_last,
41823b1137feSDavid Ahern 				   struct nl_info *info,
41833b1137feSDavid Ahern 				   __u16 nlflags)
41843b1137feSDavid Ahern {
41853b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
41863b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
41873b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
41883b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
41893b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
41903b1137feSDavid Ahern 	 */
419193c2fb25SDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
419293c2fb25SDavid Ahern 		rt = list_first_entry(&rt_last->fib6_siblings,
41938d1c802bSDavid Ahern 				      struct fib6_info,
419493c2fb25SDavid Ahern 				      fib6_siblings);
41953b1137feSDavid Ahern 	}
41963b1137feSDavid Ahern 
41973b1137feSDavid Ahern 	if (rt)
41983b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
41993b1137feSDavid Ahern }
42003b1137feSDavid Ahern 
4201333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
4202333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
420351ebd318SNicolas Dichtel {
42048d1c802bSDavid Ahern 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
42053b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
420651ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
420751ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
42088d1c802bSDavid Ahern 	struct fib6_info *rt;
42096b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
42106b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
42113b1137feSDavid Ahern 	__u16 nlflags;
421251ebd318SNicolas Dichtel 	int remaining;
421351ebd318SNicolas Dichtel 	int attrlen;
42146b9ea5a6SRoopa Prabhu 	int err = 1;
42156b9ea5a6SRoopa Prabhu 	int nhn = 0;
42166b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
42176b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
42186b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
421951ebd318SNicolas Dichtel 
42203b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
42213b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
42223b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
42233b1137feSDavid Ahern 
422435f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
422551ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
422651ebd318SNicolas Dichtel 
42276b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
42288d1c802bSDavid Ahern 	 * fib6_info structs per nexthop
42296b9ea5a6SRoopa Prabhu 	 */
423051ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
423151ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
423251ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
423351ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
423451ebd318SNicolas Dichtel 
423551ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
423651ebd318SNicolas Dichtel 		if (attrlen > 0) {
423751ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
423851ebd318SNicolas Dichtel 
423951ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
424051ebd318SNicolas Dichtel 			if (nla) {
424167b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
424251ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
424351ebd318SNicolas Dichtel 			}
424419e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
424519e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
424619e42e45SRoopa Prabhu 			if (nla)
424719e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
424851ebd318SNicolas Dichtel 		}
42496b9ea5a6SRoopa Prabhu 
425068e2ffdeSDavid Ahern 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
4251acb54e3cSDavid Ahern 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
42528c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
42538c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
42548c5b83f0SRoopa Prabhu 			rt = NULL;
42556b9ea5a6SRoopa Prabhu 			goto cleanup;
42568c5b83f0SRoopa Prabhu 		}
42576b9ea5a6SRoopa Prabhu 
42585e670d84SDavid Ahern 		rt->fib6_nh.nh_weight = rtnh->rtnh_hops + 1;
4259398958aeSIdo Schimmel 
4260d4ead6b3SDavid Ahern 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4261d4ead6b3SDavid Ahern 					    rt, &r_cfg);
426251ebd318SNicolas Dichtel 		if (err) {
426393531c67SDavid Ahern 			fib6_info_release(rt);
42646b9ea5a6SRoopa Prabhu 			goto cleanup;
426551ebd318SNicolas Dichtel 		}
42666b9ea5a6SRoopa Prabhu 
42676b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
426851ebd318SNicolas Dichtel 	}
42696b9ea5a6SRoopa Prabhu 
42703b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
42713b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
42723b1137feSDavid Ahern 	 * the full route when done
42733b1137feSDavid Ahern 	 */
42743b1137feSDavid Ahern 	info->skip_notify = 1;
42753b1137feSDavid Ahern 
42766b9ea5a6SRoopa Prabhu 	err_nh = NULL;
42776b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
42788d1c802bSDavid Ahern 		rt_last = nh->fib6_info;
42798d1c802bSDavid Ahern 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
42808d1c802bSDavid Ahern 		fib6_info_release(nh->fib6_info);
428193531c67SDavid Ahern 
42823b1137feSDavid Ahern 		/* save reference to first route for notification */
42833b1137feSDavid Ahern 		if (!rt_notif && !err)
42848d1c802bSDavid Ahern 			rt_notif = nh->fib6_info;
42853b1137feSDavid Ahern 
42868d1c802bSDavid Ahern 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
42878d1c802bSDavid Ahern 		nh->fib6_info = NULL;
42886b9ea5a6SRoopa Prabhu 		if (err) {
42896b9ea5a6SRoopa Prabhu 			if (replace && nhn)
42906b9ea5a6SRoopa Prabhu 				ip6_print_replace_route_err(&rt6_nh_list);
42916b9ea5a6SRoopa Prabhu 			err_nh = nh;
42926b9ea5a6SRoopa Prabhu 			goto add_errout;
42936b9ea5a6SRoopa Prabhu 		}
42946b9ea5a6SRoopa Prabhu 
42951a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
429627596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
429727596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
429827596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
429927596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
430027596472SMichal Kubeček 		 * be added to it.
43011a72418bSNicolas Dichtel 		 */
430227596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
430327596472SMichal Kubeček 						     NLM_F_REPLACE);
43046b9ea5a6SRoopa Prabhu 		nhn++;
43056b9ea5a6SRoopa Prabhu 	}
43066b9ea5a6SRoopa Prabhu 
43073b1137feSDavid Ahern 	/* success ... tell user about new route */
43083b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
43096b9ea5a6SRoopa Prabhu 	goto cleanup;
43106b9ea5a6SRoopa Prabhu 
43116b9ea5a6SRoopa Prabhu add_errout:
43123b1137feSDavid Ahern 	/* send notification for routes that were added so that
43133b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
43143b1137feSDavid Ahern 	 * coherent
43153b1137feSDavid Ahern 	 */
43163b1137feSDavid Ahern 	if (rt_notif)
43173b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
43183b1137feSDavid Ahern 
43196b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
43206b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
43216b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
43226b9ea5a6SRoopa Prabhu 			break;
4323333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
43246b9ea5a6SRoopa Prabhu 	}
43256b9ea5a6SRoopa Prabhu 
43266b9ea5a6SRoopa Prabhu cleanup:
43276b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
43288d1c802bSDavid Ahern 		if (nh->fib6_info)
43298d1c802bSDavid Ahern 			fib6_info_release(nh->fib6_info);
43306b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
43316b9ea5a6SRoopa Prabhu 		kfree(nh);
43326b9ea5a6SRoopa Prabhu 	}
43336b9ea5a6SRoopa Prabhu 
43346b9ea5a6SRoopa Prabhu 	return err;
43356b9ea5a6SRoopa Prabhu }
43366b9ea5a6SRoopa Prabhu 
4337333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
4338333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
43396b9ea5a6SRoopa Prabhu {
43406b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
43416b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
43426b9ea5a6SRoopa Prabhu 	int remaining;
43436b9ea5a6SRoopa Prabhu 	int attrlen;
43446b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
43456b9ea5a6SRoopa Prabhu 
43466b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
43476b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
43486b9ea5a6SRoopa Prabhu 
43496b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
43506b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
43516b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
43526b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
43536b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
43546b9ea5a6SRoopa Prabhu 
43556b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
43566b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
43576b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
43586b9ea5a6SRoopa Prabhu 
43596b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
43606b9ea5a6SRoopa Prabhu 			if (nla) {
43616b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
43626b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
43636b9ea5a6SRoopa Prabhu 			}
43646b9ea5a6SRoopa Prabhu 		}
4365333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
43666b9ea5a6SRoopa Prabhu 		if (err)
43676b9ea5a6SRoopa Prabhu 			last_err = err;
43686b9ea5a6SRoopa Prabhu 
436951ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
437051ebd318SNicolas Dichtel 	}
437151ebd318SNicolas Dichtel 
437251ebd318SNicolas Dichtel 	return last_err;
437351ebd318SNicolas Dichtel }
437451ebd318SNicolas Dichtel 
4375c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4376c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
43771da177e4SLinus Torvalds {
437886872cb5SThomas Graf 	struct fib6_config cfg;
437986872cb5SThomas Graf 	int err;
43801da177e4SLinus Torvalds 
4381333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
438286872cb5SThomas Graf 	if (err < 0)
438386872cb5SThomas Graf 		return err;
438486872cb5SThomas Graf 
438551ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4386333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
43870ae81335SDavid Ahern 	else {
43880ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
4389333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
43901da177e4SLinus Torvalds 	}
43910ae81335SDavid Ahern }
43921da177e4SLinus Torvalds 
4393c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4394c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
43951da177e4SLinus Torvalds {
439686872cb5SThomas Graf 	struct fib6_config cfg;
439786872cb5SThomas Graf 	int err;
43981da177e4SLinus Torvalds 
4399333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
440086872cb5SThomas Graf 	if (err < 0)
440186872cb5SThomas Graf 		return err;
440286872cb5SThomas Graf 
440351ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4404333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
440551ebd318SNicolas Dichtel 	else
4406acb54e3cSDavid Ahern 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
44071da177e4SLinus Torvalds }
44081da177e4SLinus Torvalds 
44098d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt)
4410339bf98fSThomas Graf {
4411beb1afacSDavid Ahern 	int nexthop_len = 0;
4412beb1afacSDavid Ahern 
441393c2fb25SDavid Ahern 	if (rt->fib6_nsiblings) {
4414beb1afacSDavid Ahern 		nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
4415beb1afacSDavid Ahern 			    + NLA_ALIGN(sizeof(struct rtnexthop))
4416beb1afacSDavid Ahern 			    + nla_total_size(16) /* RTA_GATEWAY */
44175e670d84SDavid Ahern 			    + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate);
4418beb1afacSDavid Ahern 
441993c2fb25SDavid Ahern 		nexthop_len *= rt->fib6_nsiblings;
4420beb1afacSDavid Ahern 	}
4421beb1afacSDavid Ahern 
4422339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
4423339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
4424339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
4425339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
4426339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
4427339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
4428339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
4429339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
4430339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
44316a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
4432ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
4433c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
443419e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
44355e670d84SDavid Ahern 	       + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate)
4436beb1afacSDavid Ahern 	       + nexthop_len;
4437beb1afacSDavid Ahern }
4438beb1afacSDavid Ahern 
44398d1c802bSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct fib6_info *rt,
44405be083ceSDavid Ahern 			    unsigned int *flags, bool skip_oif)
4441beb1afacSDavid Ahern {
44425e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
4443f9d882eaSIdo Schimmel 		*flags |= RTNH_F_DEAD;
4444f9d882eaSIdo Schimmel 
44455e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN) {
4446beb1afacSDavid Ahern 		*flags |= RTNH_F_LINKDOWN;
444793c2fb25SDavid Ahern 		if (rt->fib6_idev->cnf.ignore_routes_with_linkdown)
4448beb1afacSDavid Ahern 			*flags |= RTNH_F_DEAD;
4449beb1afacSDavid Ahern 	}
4450beb1afacSDavid Ahern 
445193c2fb25SDavid Ahern 	if (rt->fib6_flags & RTF_GATEWAY) {
44525e670d84SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->fib6_nh.nh_gw) < 0)
4453beb1afacSDavid Ahern 			goto nla_put_failure;
4454beb1afacSDavid Ahern 	}
4455beb1afacSDavid Ahern 
44565e670d84SDavid Ahern 	*flags |= (rt->fib6_nh.nh_flags & RTNH_F_ONLINK);
44575e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_OFFLOAD)
445861e4d01eSIdo Schimmel 		*flags |= RTNH_F_OFFLOAD;
445961e4d01eSIdo Schimmel 
44605be083ceSDavid Ahern 	/* not needed for multipath encoding b/c it has a rtnexthop struct */
44615e670d84SDavid Ahern 	if (!skip_oif && rt->fib6_nh.nh_dev &&
44625e670d84SDavid Ahern 	    nla_put_u32(skb, RTA_OIF, rt->fib6_nh.nh_dev->ifindex))
4463beb1afacSDavid Ahern 		goto nla_put_failure;
4464beb1afacSDavid Ahern 
44655e670d84SDavid Ahern 	if (rt->fib6_nh.nh_lwtstate &&
44665e670d84SDavid Ahern 	    lwtunnel_fill_encap(skb, rt->fib6_nh.nh_lwtstate) < 0)
4467beb1afacSDavid Ahern 		goto nla_put_failure;
4468beb1afacSDavid Ahern 
4469beb1afacSDavid Ahern 	return 0;
4470beb1afacSDavid Ahern 
4471beb1afacSDavid Ahern nla_put_failure:
4472beb1afacSDavid Ahern 	return -EMSGSIZE;
4473beb1afacSDavid Ahern }
4474beb1afacSDavid Ahern 
44755be083ceSDavid Ahern /* add multipath next hop */
44768d1c802bSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct fib6_info *rt)
4477beb1afacSDavid Ahern {
44785e670d84SDavid Ahern 	const struct net_device *dev = rt->fib6_nh.nh_dev;
4479beb1afacSDavid Ahern 	struct rtnexthop *rtnh;
4480beb1afacSDavid Ahern 	unsigned int flags = 0;
4481beb1afacSDavid Ahern 
4482beb1afacSDavid Ahern 	rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
4483beb1afacSDavid Ahern 	if (!rtnh)
4484beb1afacSDavid Ahern 		goto nla_put_failure;
4485beb1afacSDavid Ahern 
44865e670d84SDavid Ahern 	rtnh->rtnh_hops = rt->fib6_nh.nh_weight - 1;
44875e670d84SDavid Ahern 	rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
4488beb1afacSDavid Ahern 
44895be083ceSDavid Ahern 	if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
4490beb1afacSDavid Ahern 		goto nla_put_failure;
4491beb1afacSDavid Ahern 
4492beb1afacSDavid Ahern 	rtnh->rtnh_flags = flags;
4493beb1afacSDavid Ahern 
4494beb1afacSDavid Ahern 	/* length of rtnetlink header + attributes */
4495beb1afacSDavid Ahern 	rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
4496beb1afacSDavid Ahern 
4497beb1afacSDavid Ahern 	return 0;
4498beb1afacSDavid Ahern 
4499beb1afacSDavid Ahern nla_put_failure:
4500beb1afacSDavid Ahern 	return -EMSGSIZE;
4501339bf98fSThomas Graf }
4502339bf98fSThomas Graf 
4503d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
45048d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
4505d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
450615e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
4507f8cfe2ceSDavid Ahern 			 unsigned int flags)
45081da177e4SLinus Torvalds {
45091da177e4SLinus Torvalds 	struct rtmsg *rtm;
45101da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
4511d4ead6b3SDavid Ahern 	long expires = 0;
4512d4ead6b3SDavid Ahern 	u32 *pmetrics;
45139e762a4aSPatrick McHardy 	u32 table;
45141da177e4SLinus Torvalds 
451515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
451638308473SDavid S. Miller 	if (!nlh)
451726932566SPatrick McHardy 		return -EMSGSIZE;
45182d7202bfSThomas Graf 
45192d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
45201da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
452193c2fb25SDavid Ahern 	rtm->rtm_dst_len = rt->fib6_dst.plen;
452293c2fb25SDavid Ahern 	rtm->rtm_src_len = rt->fib6_src.plen;
45231da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
452493c2fb25SDavid Ahern 	if (rt->fib6_table)
452593c2fb25SDavid Ahern 		table = rt->fib6_table->tb6_id;
4526c71099acSThomas Graf 	else
45279e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
45289e762a4aSPatrick McHardy 	rtm->rtm_table = table;
4529c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
4530c78679e8SDavid S. Miller 		goto nla_put_failure;
4531e8478e80SDavid Ahern 
4532e8478e80SDavid Ahern 	rtm->rtm_type = rt->fib6_type;
45331da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
45341da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
453593c2fb25SDavid Ahern 	rtm->rtm_protocol = rt->fib6_protocol;
45361da177e4SLinus Torvalds 
453793c2fb25SDavid Ahern 	if (rt->fib6_flags & RTF_CACHE)
45381da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
45391da177e4SLinus Torvalds 
4540d4ead6b3SDavid Ahern 	if (dest) {
4541d4ead6b3SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, dest))
4542c78679e8SDavid S. Miller 			goto nla_put_failure;
45431da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
45441da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
454593c2fb25SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, &rt->fib6_dst.addr))
4546c78679e8SDavid S. Miller 			goto nla_put_failure;
45471da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
45481da177e4SLinus Torvalds 	if (src) {
4549930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
4550c78679e8SDavid S. Miller 			goto nla_put_failure;
45511da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
4552c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
455393c2fb25SDavid Ahern 		   nla_put_in6_addr(skb, RTA_SRC, &rt->fib6_src.addr))
4554c78679e8SDavid S. Miller 		goto nla_put_failure;
45551da177e4SLinus Torvalds #endif
45567bc570c8SYOSHIFUJI Hideaki 	if (iif) {
45577bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
455893c2fb25SDavid Ahern 		if (ipv6_addr_is_multicast(&rt->fib6_dst.addr)) {
4559fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
45602cf75070SNikolay Aleksandrov 
45617bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
45627bc570c8SYOSHIFUJI Hideaki 				return 0;
4563fd61c6baSDavid Ahern 			if (err < 0)
45647bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
45657bc570c8SYOSHIFUJI Hideaki 		} else
45667bc570c8SYOSHIFUJI Hideaki #endif
4567c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
4568c78679e8SDavid S. Miller 				goto nla_put_failure;
4569d4ead6b3SDavid Ahern 	} else if (dest) {
45701da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
4571d4ead6b3SDavid Ahern 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
4572930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4573c78679e8SDavid S. Miller 			goto nla_put_failure;
4574c3968a85SDaniel Walter 	}
4575c3968a85SDaniel Walter 
457693c2fb25SDavid Ahern 	if (rt->fib6_prefsrc.plen) {
4577c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
457893c2fb25SDavid Ahern 		saddr_buf = rt->fib6_prefsrc.addr;
4579930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4580c78679e8SDavid S. Miller 			goto nla_put_failure;
45811da177e4SLinus Torvalds 	}
45822d7202bfSThomas Graf 
4583d4ead6b3SDavid Ahern 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4584d4ead6b3SDavid Ahern 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
45852d7202bfSThomas Graf 		goto nla_put_failure;
45862d7202bfSThomas Graf 
458793c2fb25SDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
4588beb1afacSDavid Ahern 		goto nla_put_failure;
4589beb1afacSDavid Ahern 
4590beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
4591beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
4592beb1afacSDavid Ahern 	 */
459393c2fb25SDavid Ahern 	if (rt->fib6_nsiblings) {
45948d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
4595beb1afacSDavid Ahern 		struct nlattr *mp;
4596beb1afacSDavid Ahern 
4597beb1afacSDavid Ahern 		mp = nla_nest_start(skb, RTA_MULTIPATH);
4598beb1afacSDavid Ahern 		if (!mp)
4599beb1afacSDavid Ahern 			goto nla_put_failure;
4600beb1afacSDavid Ahern 
4601beb1afacSDavid Ahern 		if (rt6_add_nexthop(skb, rt) < 0)
4602beb1afacSDavid Ahern 			goto nla_put_failure;
4603beb1afacSDavid Ahern 
4604beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
460593c2fb25SDavid Ahern 					 &rt->fib6_siblings, fib6_siblings) {
4606beb1afacSDavid Ahern 			if (rt6_add_nexthop(skb, sibling) < 0)
460794f826b8SEric Dumazet 				goto nla_put_failure;
460894f826b8SEric Dumazet 		}
46092d7202bfSThomas Graf 
4610beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
4611beb1afacSDavid Ahern 	} else {
46125be083ceSDavid Ahern 		if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
4613c78679e8SDavid S. Miller 			goto nla_put_failure;
4614beb1afacSDavid Ahern 	}
46158253947eSLi Wei 
461693c2fb25SDavid Ahern 	if (rt->fib6_flags & RTF_EXPIRES) {
461714895687SDavid Ahern 		expires = dst ? dst->expires : rt->expires;
461814895687SDavid Ahern 		expires -= jiffies;
461914895687SDavid Ahern 	}
462069cdf8f9SYOSHIFUJI Hideaki 
4621d4ead6b3SDavid Ahern 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
4622e3703b3dSThomas Graf 		goto nla_put_failure;
46231da177e4SLinus Torvalds 
462493c2fb25SDavid Ahern 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->fib6_flags)))
4625c78ba6d6SLubomir Rintel 		goto nla_put_failure;
4626c78ba6d6SLubomir Rintel 
462719e42e45SRoopa Prabhu 
4628053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4629053c095aSJohannes Berg 	return 0;
46302d7202bfSThomas Graf 
46312d7202bfSThomas Graf nla_put_failure:
463226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
463326932566SPatrick McHardy 	return -EMSGSIZE;
46341da177e4SLinus Torvalds }
46351da177e4SLinus Torvalds 
46368d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg)
46371da177e4SLinus Torvalds {
46381da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
46391f17e2f2SDavid Ahern 	struct net *net = arg->net;
46401f17e2f2SDavid Ahern 
4641421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
46421f17e2f2SDavid Ahern 		return 0;
46431da177e4SLinus Torvalds 
46442d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
46452d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
4646f8cfe2ceSDavid Ahern 
4647f8cfe2ceSDavid Ahern 		/* user wants prefix routes only */
4648f8cfe2ceSDavid Ahern 		if (rtm->rtm_flags & RTM_F_PREFIX &&
464993c2fb25SDavid Ahern 		    !(rt->fib6_flags & RTF_PREFIX_RT)) {
4650f8cfe2ceSDavid Ahern 			/* success since this is not a prefix route */
4651f8cfe2ceSDavid Ahern 			return 1;
4652f8cfe2ceSDavid Ahern 		}
4653f8cfe2ceSDavid Ahern 	}
46541da177e4SLinus Torvalds 
4655d4ead6b3SDavid Ahern 	return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4656d4ead6b3SDavid Ahern 			     RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
4657d4ead6b3SDavid Ahern 			     arg->cb->nlh->nlmsg_seq, NLM_F_MULTI);
46581da177e4SLinus Torvalds }
46591da177e4SLinus Torvalds 
4660c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4661c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
46621da177e4SLinus Torvalds {
46633b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
4664ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
466518c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
466618c3a61cSRoopa Prabhu 	struct dst_entry *dst;
46671da177e4SLinus Torvalds 	struct rt6_info *rt;
4668ab364a6fSThomas Graf 	struct sk_buff *skb;
4669ab364a6fSThomas Graf 	struct rtmsg *rtm;
46704c9483b2SDavid S. Miller 	struct flowi6 fl6;
467118c3a61cSRoopa Prabhu 	bool fibmatch;
4672ab364a6fSThomas Graf 
4673fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4674c21ef3e3SDavid Ahern 			  extack);
4675ab364a6fSThomas Graf 	if (err < 0)
4676ab364a6fSThomas Graf 		goto errout;
4677ab364a6fSThomas Graf 
4678ab364a6fSThomas Graf 	err = -EINVAL;
46794c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
468038b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
468138b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
468218c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
4683ab364a6fSThomas Graf 
4684ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
4685ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4686ab364a6fSThomas Graf 			goto errout;
4687ab364a6fSThomas Graf 
46884e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
4689ab364a6fSThomas Graf 	}
4690ab364a6fSThomas Graf 
4691ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
4692ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4693ab364a6fSThomas Graf 			goto errout;
4694ab364a6fSThomas Graf 
46954e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
4696ab364a6fSThomas Graf 	}
4697ab364a6fSThomas Graf 
4698ab364a6fSThomas Graf 	if (tb[RTA_IIF])
4699ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
4700ab364a6fSThomas Graf 
4701ab364a6fSThomas Graf 	if (tb[RTA_OIF])
470272331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
4703ab364a6fSThomas Graf 
47042e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
47052e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
47062e47b291SLorenzo Colitti 
4707622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
4708622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
4709622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
4710622ec2c9SLorenzo Colitti 	else
4711622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4712622ec2c9SLorenzo Colitti 
4713ab364a6fSThomas Graf 	if (iif) {
4714ab364a6fSThomas Graf 		struct net_device *dev;
471572331bc0SShmulik Ladkani 		int flags = 0;
471672331bc0SShmulik Ladkani 
4717121622dbSFlorian Westphal 		rcu_read_lock();
4718121622dbSFlorian Westphal 
4719121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
4720ab364a6fSThomas Graf 		if (!dev) {
4721121622dbSFlorian Westphal 			rcu_read_unlock();
4722ab364a6fSThomas Graf 			err = -ENODEV;
4723ab364a6fSThomas Graf 			goto errout;
4724ab364a6fSThomas Graf 		}
472572331bc0SShmulik Ladkani 
472672331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
472772331bc0SShmulik Ladkani 
472872331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
472972331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
473072331bc0SShmulik Ladkani 
4731b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
4732121622dbSFlorian Westphal 
4733121622dbSFlorian Westphal 		rcu_read_unlock();
473472331bc0SShmulik Ladkani 	} else {
473572331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
473672331bc0SShmulik Ladkani 
473718c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
473818c3a61cSRoopa Prabhu 	}
473918c3a61cSRoopa Prabhu 
474018c3a61cSRoopa Prabhu 
474118c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
474218c3a61cSRoopa Prabhu 	if (rt->dst.error) {
474318c3a61cSRoopa Prabhu 		err = rt->dst.error;
474418c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
474518c3a61cSRoopa Prabhu 		goto errout;
4746ab364a6fSThomas Graf 	}
47471da177e4SLinus Torvalds 
47489d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
47499d6acb3bSWANG Cong 		err = rt->dst.error;
47509d6acb3bSWANG Cong 		ip6_rt_put(rt);
47519d6acb3bSWANG Cong 		goto errout;
47529d6acb3bSWANG Cong 	}
47539d6acb3bSWANG Cong 
47541da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
475538308473SDavid S. Miller 	if (!skb) {
475694e187c0SAmerigo Wang 		ip6_rt_put(rt);
4757ab364a6fSThomas Graf 		err = -ENOBUFS;
4758ab364a6fSThomas Graf 		goto errout;
4759ab364a6fSThomas Graf 	}
47601da177e4SLinus Torvalds 
4761d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
476218c3a61cSRoopa Prabhu 	if (fibmatch)
476393531c67SDavid Ahern 		err = rt6_fill_node(net, skb, rt->from, NULL, NULL, NULL, iif,
476418c3a61cSRoopa Prabhu 				    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
476518c3a61cSRoopa Prabhu 				    nlh->nlmsg_seq, 0);
476618c3a61cSRoopa Prabhu 	else
476793531c67SDavid Ahern 		err = rt6_fill_node(net, skb, rt->from, dst,
476893531c67SDavid Ahern 				    &fl6.daddr, &fl6.saddr, iif, RTM_NEWROUTE,
4769d4ead6b3SDavid Ahern 				    NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
4770d4ead6b3SDavid Ahern 				    0);
47711da177e4SLinus Torvalds 	if (err < 0) {
4772ab364a6fSThomas Graf 		kfree_skb(skb);
4773ab364a6fSThomas Graf 		goto errout;
47741da177e4SLinus Torvalds 	}
47751da177e4SLinus Torvalds 
477615e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4777ab364a6fSThomas Graf errout:
47781da177e4SLinus Torvalds 	return err;
47791da177e4SLinus Torvalds }
47801da177e4SLinus Torvalds 
47818d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
478237a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
47831da177e4SLinus Torvalds {
47841da177e4SLinus Torvalds 	struct sk_buff *skb;
47855578689aSDaniel Lezcano 	struct net *net = info->nl_net;
4786528c4cebSDenis V. Lunev 	u32 seq;
4787528c4cebSDenis V. Lunev 	int err;
47880d51aa80SJamal Hadi Salim 
4789528c4cebSDenis V. Lunev 	err = -ENOBUFS;
479038308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
479186872cb5SThomas Graf 
479219e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
479338308473SDavid S. Miller 	if (!skb)
479421713ebcSThomas Graf 		goto errout;
47951da177e4SLinus Torvalds 
4796d4ead6b3SDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
4797f8cfe2ceSDavid Ahern 			    event, info->portid, seq, nlm_flags);
479826932566SPatrick McHardy 	if (err < 0) {
479926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
480026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
480126932566SPatrick McHardy 		kfree_skb(skb);
480226932566SPatrick McHardy 		goto errout;
480326932566SPatrick McHardy 	}
480415e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
48055578689aSDaniel Lezcano 		    info->nlh, gfp_any());
48061ce85fe4SPablo Neira Ayuso 	return;
480721713ebcSThomas Graf errout:
480821713ebcSThomas Graf 	if (err < 0)
48095578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
48101da177e4SLinus Torvalds }
48111da177e4SLinus Torvalds 
48128ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
4813351638e7SJiri Pirko 				unsigned long event, void *ptr)
48148ed67789SDaniel Lezcano {
4815351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4816c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
48178ed67789SDaniel Lezcano 
4818242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
4819242d3a49SWANG Cong 		return NOTIFY_OK;
4820242d3a49SWANG Cong 
4821242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
4822421842edSDavid Ahern 		net->ipv6.fib6_null_entry->fib6_nh.nh_dev = dev;
482393c2fb25SDavid Ahern 		net->ipv6.fib6_null_entry->fib6_idev = in6_dev_get(dev);
4824d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
48258ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
48268ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
4827d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
48288ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
4829d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
48308ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
48318ed67789SDaniel Lezcano #endif
483276da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
483376da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
483476da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
483576da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
483676da0704SWANG Cong 		 */
483793c2fb25SDavid Ahern 		in6_dev_put_clear(&net->ipv6.fib6_null_entry->fib6_idev);
483812d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
4839242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
484012d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
484112d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
4842242d3a49SWANG Cong #endif
48438ed67789SDaniel Lezcano 	}
48448ed67789SDaniel Lezcano 
48458ed67789SDaniel Lezcano 	return NOTIFY_OK;
48468ed67789SDaniel Lezcano }
48478ed67789SDaniel Lezcano 
48481da177e4SLinus Torvalds /*
48491da177e4SLinus Torvalds  *	/proc
48501da177e4SLinus Torvalds  */
48511da177e4SLinus Torvalds 
48521da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
48531da177e4SLinus Torvalds 
485433120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
485533120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
485633120b30SAlexey Dobriyan 	.read		= seq_read,
485733120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
48588d2ca1d7SHannes Frederic Sowa 	.release	= seq_release_net,
485933120b30SAlexey Dobriyan };
486033120b30SAlexey Dobriyan 
48611da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
48621da177e4SLinus Torvalds {
486369ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
48641da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
486569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
486669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
486781eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
486869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
486969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
4870fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
487169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
48721da177e4SLinus Torvalds 
48731da177e4SLinus Torvalds 	return 0;
48741da177e4SLinus Torvalds }
48751da177e4SLinus Torvalds 
48761da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
48771da177e4SLinus Torvalds {
4878de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
487969ddb805SDaniel Lezcano }
488069ddb805SDaniel Lezcano 
48819a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
48821da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
48831da177e4SLinus Torvalds 	.read	 = seq_read,
48841da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
4885b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
48861da177e4SLinus Torvalds };
48871da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
48881da177e4SLinus Torvalds 
48891da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
48901da177e4SLinus Torvalds 
48911da177e4SLinus Torvalds static
4892fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
48931da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
48941da177e4SLinus Torvalds {
4895c486da34SLucian Adrian Grijincu 	struct net *net;
4896c486da34SLucian Adrian Grijincu 	int delay;
4897c486da34SLucian Adrian Grijincu 	if (!write)
4898c486da34SLucian Adrian Grijincu 		return -EINVAL;
4899c486da34SLucian Adrian Grijincu 
4900c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
4901c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
49028d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
49032ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
49041da177e4SLinus Torvalds 	return 0;
49051da177e4SLinus Torvalds }
49061da177e4SLinus Torvalds 
4907fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = {
49081da177e4SLinus Torvalds 	{
49091da177e4SLinus Torvalds 		.procname	=	"flush",
49104990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
49111da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
491289c8b3a1SDave Jones 		.mode		=	0200,
49136d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
49141da177e4SLinus Torvalds 	},
49151da177e4SLinus Torvalds 	{
49161da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
49179a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
49181da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
49191da177e4SLinus Torvalds 		.mode		=	0644,
49206d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
49211da177e4SLinus Torvalds 	},
49221da177e4SLinus Torvalds 	{
49231da177e4SLinus Torvalds 		.procname	=	"max_size",
49244990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
49251da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
49261da177e4SLinus Torvalds 		.mode		=	0644,
49276d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
49281da177e4SLinus Torvalds 	},
49291da177e4SLinus Torvalds 	{
49301da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
49314990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
49321da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
49331da177e4SLinus Torvalds 		.mode		=	0644,
49346d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
49351da177e4SLinus Torvalds 	},
49361da177e4SLinus Torvalds 	{
49371da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
49384990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
49391da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
49401da177e4SLinus Torvalds 		.mode		=	0644,
49416d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
49421da177e4SLinus Torvalds 	},
49431da177e4SLinus Torvalds 	{
49441da177e4SLinus Torvalds 		.procname	=	"gc_interval",
49454990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
49461da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
49471da177e4SLinus Torvalds 		.mode		=	0644,
49486d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
49491da177e4SLinus Torvalds 	},
49501da177e4SLinus Torvalds 	{
49511da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
49524990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
49531da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
49541da177e4SLinus Torvalds 		.mode		=	0644,
4955f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
49561da177e4SLinus Torvalds 	},
49571da177e4SLinus Torvalds 	{
49581da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
49594990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
49601da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
49611da177e4SLinus Torvalds 		.mode		=	0644,
49626d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
49631da177e4SLinus Torvalds 	},
49641da177e4SLinus Torvalds 	{
49651da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
49664990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
49671da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
49681da177e4SLinus Torvalds 		.mode		=	0644,
4969f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
49701da177e4SLinus Torvalds 	},
49711da177e4SLinus Torvalds 	{
49721da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
49734990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
49741da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
49751da177e4SLinus Torvalds 		.mode		=	0644,
49766d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
49771da177e4SLinus Torvalds 	},
4978f8572d8fSEric W. Biederman 	{ }
49791da177e4SLinus Torvalds };
49801da177e4SLinus Torvalds 
49812c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
4982760f2d01SDaniel Lezcano {
4983760f2d01SDaniel Lezcano 	struct ctl_table *table;
4984760f2d01SDaniel Lezcano 
4985760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
4986760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
4987760f2d01SDaniel Lezcano 			GFP_KERNEL);
49885ee09105SYOSHIFUJI Hideaki 
49895ee09105SYOSHIFUJI Hideaki 	if (table) {
49905ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
4991c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
499286393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
49935ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
49945ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
49955ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
49965ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
49975ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
49985ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
49995ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
50009c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
5001464dc801SEric W. Biederman 
5002464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
5003464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
5004464dc801SEric W. Biederman 			table[0].procname = NULL;
50055ee09105SYOSHIFUJI Hideaki 	}
50065ee09105SYOSHIFUJI Hideaki 
5007760f2d01SDaniel Lezcano 	return table;
5008760f2d01SDaniel Lezcano }
50091da177e4SLinus Torvalds #endif
50101da177e4SLinus Torvalds 
50112c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
5012cdb18761SDaniel Lezcano {
5013633d424bSPavel Emelyanov 	int ret = -ENOMEM;
50148ed67789SDaniel Lezcano 
501586393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
501686393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
5017f2fc6a54SBenjamin Thery 
5018fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5019fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
5020fc66f95cSEric Dumazet 
5021421842edSDavid Ahern 	net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template,
5022421842edSDavid Ahern 					    sizeof(*net->ipv6.fib6_null_entry),
5023421842edSDavid Ahern 					    GFP_KERNEL);
5024421842edSDavid Ahern 	if (!net->ipv6.fib6_null_entry)
5025421842edSDavid Ahern 		goto out_ip6_dst_entries;
5026421842edSDavid Ahern 
50278ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
50288ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
50298ed67789SDaniel Lezcano 					   GFP_KERNEL);
50308ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
5031421842edSDavid Ahern 		goto out_fib6_null_entry;
5032d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
503362fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
503462fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
50358ed67789SDaniel Lezcano 
50368ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5037feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
50388ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
50398ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
50408ed67789SDaniel Lezcano 					       GFP_KERNEL);
504168fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
504268fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
5043d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
504462fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
504562fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
50468ed67789SDaniel Lezcano 
50478ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
50488ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
50498ed67789SDaniel Lezcano 					       GFP_KERNEL);
505068fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
505168fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
5052d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
505362fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
505462fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
50558ed67789SDaniel Lezcano #endif
50568ed67789SDaniel Lezcano 
5057b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
5058b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
5059b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5060b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5061b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5062b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5063b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5064b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
5065b339a47cSPeter Zijlstra 
50666891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
50676891a346SBenjamin Thery 
50688ed67789SDaniel Lezcano 	ret = 0;
50698ed67789SDaniel Lezcano out:
50708ed67789SDaniel Lezcano 	return ret;
5071f2fc6a54SBenjamin Thery 
507268fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
507368fffc67SPeter Zijlstra out_ip6_prohibit_entry:
507468fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
507568fffc67SPeter Zijlstra out_ip6_null_entry:
507668fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
507768fffc67SPeter Zijlstra #endif
5078421842edSDavid Ahern out_fib6_null_entry:
5079421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
5080fc66f95cSEric Dumazet out_ip6_dst_entries:
5081fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5082f2fc6a54SBenjamin Thery out_ip6_dst_ops:
5083f2fc6a54SBenjamin Thery 	goto out;
5084cdb18761SDaniel Lezcano }
5085cdb18761SDaniel Lezcano 
50862c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
5087cdb18761SDaniel Lezcano {
5088421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
50898ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
50908ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
50918ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
50928ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
50938ed67789SDaniel Lezcano #endif
509441bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5095cdb18761SDaniel Lezcano }
5096cdb18761SDaniel Lezcano 
5097d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
5098d189634eSThomas Graf {
5099d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5100d4beaa66SGao feng 	proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
5101d6444062SJoe Perches 	proc_create("rt6_stats", 0444, net->proc_net, &rt6_stats_seq_fops);
5102d189634eSThomas Graf #endif
5103d189634eSThomas Graf 	return 0;
5104d189634eSThomas Graf }
5105d189634eSThomas Graf 
5106d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
5107d189634eSThomas Graf {
5108d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5109ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
5110ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
5111d189634eSThomas Graf #endif
5112d189634eSThomas Graf }
5113d189634eSThomas Graf 
5114cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
5115cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
5116cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
5117cdb18761SDaniel Lezcano };
5118cdb18761SDaniel Lezcano 
5119c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
5120c3426b47SDavid S. Miller {
5121c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5122c3426b47SDavid S. Miller 
5123c3426b47SDavid S. Miller 	if (!bp)
5124c3426b47SDavid S. Miller 		return -ENOMEM;
5125c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
5126c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
5127c3426b47SDavid S. Miller 	return 0;
5128c3426b47SDavid S. Miller }
5129c3426b47SDavid S. Miller 
5130c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
5131c3426b47SDavid S. Miller {
5132c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
5133c3426b47SDavid S. Miller 
5134c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
513556a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
5136c3426b47SDavid S. Miller 	kfree(bp);
5137c3426b47SDavid S. Miller }
5138c3426b47SDavid S. Miller 
51392b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
5140c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
5141c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
5142c3426b47SDavid S. Miller };
5143c3426b47SDavid S. Miller 
5144d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
5145d189634eSThomas Graf 	.init = ip6_route_net_init_late,
5146d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
5147d189634eSThomas Graf };
5148d189634eSThomas Graf 
51498ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
51508ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
5151242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
51528ed67789SDaniel Lezcano };
51538ed67789SDaniel Lezcano 
51542f460933SWANG Cong void __init ip6_route_init_special_entries(void)
51552f460933SWANG Cong {
51562f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
51572f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
51582f460933SWANG Cong 	 * manually for init_net */
5159421842edSDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_nh.nh_dev = init_net.loopback_dev;
516093c2fb25SDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_idev = in6_dev_get(init_net.loopback_dev);
51612f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
51622f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
51632f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
51642f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
51652f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
51662f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
51672f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
51682f460933SWANG Cong   #endif
51692f460933SWANG Cong }
51702f460933SWANG Cong 
5171433d49c3SDaniel Lezcano int __init ip6_route_init(void)
51721da177e4SLinus Torvalds {
5173433d49c3SDaniel Lezcano 	int ret;
51748d0b94afSMartin KaFai Lau 	int cpu;
5175433d49c3SDaniel Lezcano 
51769a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
51779a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
51789a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
51799a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
51809a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
5181c19a28e1SFernando Carrijo 		goto out;
518214e50e57SDavid S. Miller 
5183fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
51848ed67789SDaniel Lezcano 	if (ret)
5185bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
5186bdb3289fSDaniel Lezcano 
5187c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5188c3426b47SDavid S. Miller 	if (ret)
5189e8803b6cSDavid S. Miller 		goto out_dst_entries;
51902a0c451aSThomas Graf 
51917e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
51927e52b33bSDavid S. Miller 	if (ret)
51937e52b33bSDavid S. Miller 		goto out_register_inetpeer;
5194c3426b47SDavid S. Miller 
51955dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
51965dc121e9SArnaud Ebalard 
5197e8803b6cSDavid S. Miller 	ret = fib6_init();
5198433d49c3SDaniel Lezcano 	if (ret)
51998ed67789SDaniel Lezcano 		goto out_register_subsys;
5200433d49c3SDaniel Lezcano 
5201433d49c3SDaniel Lezcano 	ret = xfrm6_init();
5202433d49c3SDaniel Lezcano 	if (ret)
5203e8803b6cSDavid S. Miller 		goto out_fib6_init;
5204c35b7e72SDaniel Lezcano 
5205433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
5206433d49c3SDaniel Lezcano 	if (ret)
5207433d49c3SDaniel Lezcano 		goto xfrm6_init;
52087e5449c2SDaniel Lezcano 
5209d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
5210d189634eSThomas Graf 	if (ret)
5211d189634eSThomas Graf 		goto fib6_rules_init;
5212d189634eSThomas Graf 
521316feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
521416feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
521516feebcfSFlorian Westphal 	if (ret < 0)
521616feebcfSFlorian Westphal 		goto out_register_late_subsys;
521716feebcfSFlorian Westphal 
521816feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
521916feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
522016feebcfSFlorian Westphal 	if (ret < 0)
522116feebcfSFlorian Westphal 		goto out_register_late_subsys;
522216feebcfSFlorian Westphal 
522316feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
522416feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
522516feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
522616feebcfSFlorian Westphal 	if (ret < 0)
5227d189634eSThomas Graf 		goto out_register_late_subsys;
5228433d49c3SDaniel Lezcano 
52298ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
5230cdb18761SDaniel Lezcano 	if (ret)
5231d189634eSThomas Graf 		goto out_register_late_subsys;
52328ed67789SDaniel Lezcano 
52338d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
52348d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
52358d0b94afSMartin KaFai Lau 
52368d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
52378d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
52388d0b94afSMartin KaFai Lau 	}
52398d0b94afSMartin KaFai Lau 
5240433d49c3SDaniel Lezcano out:
5241433d49c3SDaniel Lezcano 	return ret;
5242433d49c3SDaniel Lezcano 
5243d189634eSThomas Graf out_register_late_subsys:
524416feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
5245d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5246433d49c3SDaniel Lezcano fib6_rules_init:
5247433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
5248433d49c3SDaniel Lezcano xfrm6_init:
5249433d49c3SDaniel Lezcano 	xfrm6_fini();
52502a0c451aSThomas Graf out_fib6_init:
52512a0c451aSThomas Graf 	fib6_gc_cleanup();
52528ed67789SDaniel Lezcano out_register_subsys:
52538ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
52547e52b33bSDavid S. Miller out_register_inetpeer:
52557e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
5256fc66f95cSEric Dumazet out_dst_entries:
5257fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5258433d49c3SDaniel Lezcano out_kmem_cache:
5259f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
5260433d49c3SDaniel Lezcano 	goto out;
52611da177e4SLinus Torvalds }
52621da177e4SLinus Torvalds 
52631da177e4SLinus Torvalds void ip6_route_cleanup(void)
52641da177e4SLinus Torvalds {
52658ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
5266d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5267101367c2SThomas Graf 	fib6_rules_cleanup();
52681da177e4SLinus Torvalds 	xfrm6_fini();
52691da177e4SLinus Torvalds 	fib6_gc_cleanup();
5270c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
52718ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
527241bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5273f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
52741da177e4SLinus Torvalds }
5275