xref: /openbmc/linux/net/ipv6/route.c (revision 7c6bb7d2faaf1ed7d78bafd712476e4cf2cf0d7d)
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>
66eacb9384SRoopa Prabhu #include <net/ip.h>
677c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
701da177e4SLinus Torvalds #include <linux/sysctl.h>
711da177e4SLinus Torvalds #endif
721da177e4SLinus Torvalds 
7330d444d3SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type);
7430d444d3SDavid Ahern 
7530d444d3SDavid Ahern #define CREATE_TRACE_POINTS
7630d444d3SDavid Ahern #include <trace/events/fib6.h>
7730d444d3SDavid Ahern EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
7830d444d3SDavid Ahern #undef CREATE_TRACE_POINTS
7930d444d3SDavid Ahern 
80afc154e9SHannes Frederic Sowa enum rt6_nud_state {
817e980569SJiri Benc 	RT6_NUD_FAIL_HARD = -3,
827e980569SJiri Benc 	RT6_NUD_FAIL_PROBE = -2,
837e980569SJiri Benc 	RT6_NUD_FAIL_DO_RR = -1,
84afc154e9SHannes Frederic Sowa 	RT6_NUD_SUCCEED = 1
85afc154e9SHannes Frederic Sowa };
86afc154e9SHannes Frederic Sowa 
871da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
880dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
89ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
901da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
911da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
921da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
931da177e4SLinus Torvalds 				       struct net_device *dev, int how);
94569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
97ede2059dSEric W. Biederman static int		ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
987150aedeSKamala R static int		ip6_pkt_prohibit(struct sk_buff *skb);
99ede2059dSEric W. Biederman static int		ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
1001da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
1016700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1026700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
1036700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
1046700c270SDavid S. Miller 					struct sk_buff *skb);
1058d1c802bSDavid Ahern static int rt6_score_route(struct fib6_info *rt, int oif, int strict);
1068d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt);
107d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
1088d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
109d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
11016a16cd3SDavid Ahern 			 int iif, int type, u32 portid, u32 seq,
11116a16cd3SDavid Ahern 			 unsigned int flags);
1128d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
11335732d01SWei Wang 					   struct in6_addr *daddr,
11435732d01SWei Wang 					   struct in6_addr *saddr);
1151da177e4SLinus Torvalds 
11670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1178d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
118b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
119830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
120830218c1SDavid Ahern 					   struct net_device *dev,
12195c96174SEric Dumazet 					   unsigned int pref);
1228d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
123b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
124830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
125830218c1SDavid Ahern 					   struct net_device *dev);
12670ceb4f5SYOSHIFUJI Hideaki #endif
12770ceb4f5SYOSHIFUJI Hideaki 
1288d0b94afSMartin KaFai Lau struct uncached_list {
1298d0b94afSMartin KaFai Lau 	spinlock_t		lock;
1308d0b94afSMartin KaFai Lau 	struct list_head	head;
1318d0b94afSMartin KaFai Lau };
1328d0b94afSMartin KaFai Lau 
1338d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
1348d0b94afSMartin KaFai Lau 
135510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt)
1368d0b94afSMartin KaFai Lau {
1378d0b94afSMartin KaFai Lau 	struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
1388d0b94afSMartin KaFai Lau 
1398d0b94afSMartin KaFai Lau 	rt->rt6i_uncached_list = ul;
1408d0b94afSMartin KaFai Lau 
1418d0b94afSMartin KaFai Lau 	spin_lock_bh(&ul->lock);
1428d0b94afSMartin KaFai Lau 	list_add_tail(&rt->rt6i_uncached, &ul->head);
1438d0b94afSMartin KaFai Lau 	spin_unlock_bh(&ul->lock);
1448d0b94afSMartin KaFai Lau }
1458d0b94afSMartin KaFai Lau 
146510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt)
1478d0b94afSMartin KaFai Lau {
1488d0b94afSMartin KaFai Lau 	if (!list_empty(&rt->rt6i_uncached)) {
1498d0b94afSMartin KaFai Lau 		struct uncached_list *ul = rt->rt6i_uncached_list;
15081eb8447SWei Wang 		struct net *net = dev_net(rt->dst.dev);
1518d0b94afSMartin KaFai Lau 
1528d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1538d0b94afSMartin KaFai Lau 		list_del(&rt->rt6i_uncached);
15481eb8447SWei Wang 		atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
1558d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1568d0b94afSMartin KaFai Lau 	}
1578d0b94afSMartin KaFai Lau }
1588d0b94afSMartin KaFai Lau 
1598d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
1608d0b94afSMartin KaFai Lau {
1618d0b94afSMartin KaFai Lau 	struct net_device *loopback_dev = net->loopback_dev;
1628d0b94afSMartin KaFai Lau 	int cpu;
1638d0b94afSMartin KaFai Lau 
164e332bc67SEric W. Biederman 	if (dev == loopback_dev)
165e332bc67SEric W. Biederman 		return;
166e332bc67SEric W. Biederman 
1678d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
1688d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
1698d0b94afSMartin KaFai Lau 		struct rt6_info *rt;
1708d0b94afSMartin KaFai Lau 
1718d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1728d0b94afSMartin KaFai Lau 		list_for_each_entry(rt, &ul->head, rt6i_uncached) {
1738d0b94afSMartin KaFai Lau 			struct inet6_dev *rt_idev = rt->rt6i_idev;
1748d0b94afSMartin KaFai Lau 			struct net_device *rt_dev = rt->dst.dev;
1758d0b94afSMartin KaFai Lau 
176e332bc67SEric W. Biederman 			if (rt_idev->dev == dev) {
1778d0b94afSMartin KaFai Lau 				rt->rt6i_idev = in6_dev_get(loopback_dev);
1788d0b94afSMartin KaFai Lau 				in6_dev_put(rt_idev);
1798d0b94afSMartin KaFai Lau 			}
1808d0b94afSMartin KaFai Lau 
181e332bc67SEric W. Biederman 			if (rt_dev == dev) {
1828d0b94afSMartin KaFai Lau 				rt->dst.dev = loopback_dev;
1838d0b94afSMartin KaFai Lau 				dev_hold(rt->dst.dev);
1848d0b94afSMartin KaFai Lau 				dev_put(rt_dev);
1858d0b94afSMartin KaFai Lau 			}
1868d0b94afSMartin KaFai Lau 		}
1878d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1888d0b94afSMartin KaFai Lau 	}
1898d0b94afSMartin KaFai Lau }
1908d0b94afSMartin KaFai Lau 
191f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p,
192f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
193f894cbf8SDavid S. Miller 					     const void *daddr)
19439232973SDavid S. Miller {
195a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
19639232973SDavid S. Miller 		return (const void *) p;
197f894cbf8SDavid S. Miller 	else if (skb)
198f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
19939232973SDavid S. Miller 	return daddr;
20039232973SDavid S. Miller }
20139232973SDavid S. Miller 
202f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
203f8a1b43bSDavid Ahern 				   struct net_device *dev,
204f894cbf8SDavid S. Miller 				   struct sk_buff *skb,
205f894cbf8SDavid S. Miller 				   const void *daddr)
206d3aaeb38SDavid S. Miller {
20739232973SDavid S. Miller 	struct neighbour *n;
20839232973SDavid S. Miller 
209f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(gw, skb, daddr);
210f8a1b43bSDavid Ahern 	n = __ipv6_neigh_lookup(dev, daddr);
211f83c7790SDavid S. Miller 	if (n)
212f83c7790SDavid S. Miller 		return n;
213f8a1b43bSDavid Ahern 	return neigh_create(&nd_tbl, daddr, dev);
214f8a1b43bSDavid Ahern }
215f8a1b43bSDavid Ahern 
216f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
217f8a1b43bSDavid Ahern 					      struct sk_buff *skb,
218f8a1b43bSDavid Ahern 					      const void *daddr)
219f8a1b43bSDavid Ahern {
220f8a1b43bSDavid Ahern 	const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
221f8a1b43bSDavid Ahern 
222f8a1b43bSDavid Ahern 	return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr);
223f83c7790SDavid S. Miller }
224f83c7790SDavid S. Miller 
22563fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
22663fca65dSJulian Anastasov {
22763fca65dSJulian Anastasov 	struct net_device *dev = dst->dev;
22863fca65dSJulian Anastasov 	struct rt6_info *rt = (struct rt6_info *)dst;
22963fca65dSJulian Anastasov 
230f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
23163fca65dSJulian Anastasov 	if (!daddr)
23263fca65dSJulian Anastasov 		return;
23363fca65dSJulian Anastasov 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
23463fca65dSJulian Anastasov 		return;
23563fca65dSJulian Anastasov 	if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
23663fca65dSJulian Anastasov 		return;
23763fca65dSJulian Anastasov 	__ipv6_confirm_neigh(dev, daddr);
23863fca65dSJulian Anastasov }
23963fca65dSJulian Anastasov 
2409a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
2411da177e4SLinus Torvalds 	.family			=	AF_INET6,
2421da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
2431da177e4SLinus Torvalds 	.gc_thresh		=	1024,
2441da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
2450dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
246ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
247d4ead6b3SDavid Ahern 	.cow_metrics		=	dst_cow_metrics_generic,
2481da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
2491da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
2501da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
2511da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
2521da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
2536e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
2549f8955ccSEric W. Biederman 	.local_out		=	__ip6_local_out,
255f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
25663fca65dSJulian Anastasov 	.confirm_neigh		=	ip6_confirm_neigh,
2571da177e4SLinus Torvalds };
2581da177e4SLinus Torvalds 
259ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
260ec831ea7SRoland Dreier {
261618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
262618f9bc7SSteffen Klassert 
263618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
264ec831ea7SRoland Dreier }
265ec831ea7SRoland Dreier 
2666700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
2676700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
26814e50e57SDavid S. Miller {
26914e50e57SDavid S. Miller }
27014e50e57SDavid S. Miller 
2716700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
2726700c270SDavid S. Miller 				      struct sk_buff *skb)
273b587ee3bSDavid S. Miller {
274b587ee3bSDavid S. Miller }
275b587ee3bSDavid S. Miller 
27614e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
27714e50e57SDavid S. Miller 	.family			=	AF_INET6,
27814e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
27914e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
280ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
281214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
28214e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
283b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2840a1f5962SMartin KaFai Lau 	.cow_metrics		=	dst_cow_metrics_generic,
285f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
28614e50e57SDavid S. Miller };
28714e50e57SDavid S. Miller 
28862fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
28914edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
29062fa8a84SDavid S. Miller };
29162fa8a84SDavid S. Miller 
2928d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = {
29393c2fb25SDavid Ahern 	.fib6_flags	= (RTF_REJECT | RTF_NONEXTHOP),
29493c2fb25SDavid Ahern 	.fib6_protocol  = RTPROT_KERNEL,
29593c2fb25SDavid Ahern 	.fib6_metric	= ~(u32)0,
29693c2fb25SDavid Ahern 	.fib6_ref	= ATOMIC_INIT(1),
297421842edSDavid Ahern 	.fib6_type	= RTN_UNREACHABLE,
298421842edSDavid Ahern 	.fib6_metrics	= (struct dst_metrics *)&dst_default_metrics,
299421842edSDavid Ahern };
300421842edSDavid Ahern 
301fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
3021da177e4SLinus Torvalds 	.dst = {
3031da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
3041da177e4SLinus Torvalds 		.__use		= 1,
3052c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
3061da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
3071da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
3081da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
3091da177e4SLinus Torvalds 	},
3101da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3111da177e4SLinus Torvalds };
3121da177e4SLinus Torvalds 
313101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
314101367c2SThomas Graf 
315fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
316101367c2SThomas Graf 	.dst = {
317101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
318101367c2SThomas Graf 		.__use		= 1,
3192c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
320101367c2SThomas Graf 		.error		= -EACCES,
3219ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
3229ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
323101367c2SThomas Graf 	},
324101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
325101367c2SThomas Graf };
326101367c2SThomas Graf 
327fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
328101367c2SThomas Graf 	.dst = {
329101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
330101367c2SThomas Graf 		.__use		= 1,
3312c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
332101367c2SThomas Graf 		.error		= -EINVAL,
333352e512cSHerbert Xu 		.input		= dst_discard,
334ede2059dSEric W. Biederman 		.output		= dst_discard_out,
335101367c2SThomas Graf 	},
336101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
337101367c2SThomas Graf };
338101367c2SThomas Graf 
339101367c2SThomas Graf #endif
340101367c2SThomas Graf 
341ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt)
342ebfa45f0SMartin KaFai Lau {
343ebfa45f0SMartin KaFai Lau 	struct dst_entry *dst = &rt->dst;
344ebfa45f0SMartin KaFai Lau 
345ebfa45f0SMartin KaFai Lau 	memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
346ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_uncached);
347ebfa45f0SMartin KaFai Lau }
348ebfa45f0SMartin KaFai Lau 
3491da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
35093531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
351ad706862SMartin KaFai Lau 			       int flags)
3521da177e4SLinus Torvalds {
35397bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
354b2a9c0edSWei Wang 					1, DST_OBSOLETE_FORCE_CHK, flags);
355cf911662SDavid S. Miller 
35681eb8447SWei Wang 	if (rt) {
357ebfa45f0SMartin KaFai Lau 		rt6_info_init(rt);
35881eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
35981eb8447SWei Wang 	}
3608104891bSSteffen Klassert 
361cf911662SDavid S. Miller 	return rt;
3621da177e4SLinus Torvalds }
3639ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc);
364d52d3997SMartin KaFai Lau 
3651da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
368a68886a6SDavid Ahern 	struct fib6_info *from;
3698d0b94afSMartin KaFai Lau 	struct inet6_dev *idev;
3701da177e4SLinus Torvalds 
3711620a336SDavid Ahern 	ip_dst_metrics_put(dst);
3728d0b94afSMartin KaFai Lau 	rt6_uncached_list_del(rt);
3738d0b94afSMartin KaFai Lau 
3748d0b94afSMartin KaFai Lau 	idev = rt->rt6i_idev;
37538308473SDavid S. Miller 	if (idev) {
3761da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3771da177e4SLinus Torvalds 		in6_dev_put(idev);
3781da177e4SLinus Torvalds 	}
3791716a961SGao feng 
380a68886a6SDavid Ahern 	rcu_read_lock();
381a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
382a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, NULL);
38393531c67SDavid Ahern 	fib6_info_release(from);
384a68886a6SDavid Ahern 	rcu_read_unlock();
385b3419363SDavid S. Miller }
386b3419363SDavid S. Miller 
3871da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3881da177e4SLinus Torvalds 			   int how)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3911da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3925a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
393c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3941da177e4SLinus Torvalds 
395e5645f51SWei Wang 	if (idev && idev->dev != loopback_dev) {
396e5645f51SWei Wang 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
39738308473SDavid S. Miller 		if (loopback_idev) {
3981da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
3991da177e4SLinus Torvalds 			in6_dev_put(idev);
4001da177e4SLinus Torvalds 		}
4011da177e4SLinus Torvalds 	}
40297cac082SDavid S. Miller }
4031da177e4SLinus Torvalds 
4045973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt)
4055973fb1eSMartin KaFai Lau {
4065973fb1eSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_EXPIRES)
4075973fb1eSMartin KaFai Lau 		return time_after(jiffies, rt->dst.expires);
4085973fb1eSMartin KaFai Lau 	else
4095973fb1eSMartin KaFai Lau 		return false;
4105973fb1eSMartin KaFai Lau }
4115973fb1eSMartin KaFai Lau 
412a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
4131da177e4SLinus Torvalds {
414a68886a6SDavid Ahern 	struct fib6_info *from;
415a68886a6SDavid Ahern 
416a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
417a68886a6SDavid Ahern 
4181716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
4191716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
420a50feda5SEric Dumazet 			return true;
421a68886a6SDavid Ahern 	} else if (from) {
4221e2ea8adSXin Long 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
423a68886a6SDavid Ahern 			fib6_check_expired(from);
4241716a961SGao feng 	}
425a50feda5SEric Dumazet 	return false;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
4283b290a31SDavid Ahern struct fib6_info *fib6_multipath_select(const struct net *net,
4298d1c802bSDavid Ahern 					struct fib6_info *match,
43052bd4c0cSNicolas Dichtel 					struct flowi6 *fl6, int oif,
431b75cc8f9SDavid Ahern 					const struct sk_buff *skb,
43252bd4c0cSNicolas Dichtel 					int strict)
43351ebd318SNicolas Dichtel {
4348d1c802bSDavid Ahern 	struct fib6_info *sibling, *next_sibling;
43551ebd318SNicolas Dichtel 
436b673d6ccSJakub Sitnicki 	/* We might have already computed the hash for ICMPv6 errors. In such
437b673d6ccSJakub Sitnicki 	 * case it will always be non-zero. Otherwise now is the time to do it.
438b673d6ccSJakub Sitnicki 	 */
439b673d6ccSJakub Sitnicki 	if (!fl6->mp_hash)
440b4bac172SDavid Ahern 		fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
441b673d6ccSJakub Sitnicki 
4425e670d84SDavid Ahern 	if (fl6->mp_hash <= atomic_read(&match->fib6_nh.nh_upper_bound))
4433d709f69SIdo Schimmel 		return match;
444bbfcd776SIdo Schimmel 
44593c2fb25SDavid Ahern 	list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
44693c2fb25SDavid Ahern 				 fib6_siblings) {
4475e670d84SDavid Ahern 		int nh_upper_bound;
4485e670d84SDavid Ahern 
4495e670d84SDavid Ahern 		nh_upper_bound = atomic_read(&sibling->fib6_nh.nh_upper_bound);
4505e670d84SDavid Ahern 		if (fl6->mp_hash > nh_upper_bound)
4513d709f69SIdo Schimmel 			continue;
45252bd4c0cSNicolas Dichtel 		if (rt6_score_route(sibling, oif, strict) < 0)
45352bd4c0cSNicolas Dichtel 			break;
45451ebd318SNicolas Dichtel 		match = sibling;
45551ebd318SNicolas Dichtel 		break;
45651ebd318SNicolas Dichtel 	}
4573d709f69SIdo Schimmel 
45851ebd318SNicolas Dichtel 	return match;
45951ebd318SNicolas Dichtel }
46051ebd318SNicolas Dichtel 
4611da177e4SLinus Torvalds /*
46266f5d6ceSWei Wang  *	Route lookup. rcu_read_lock() should be held.
4631da177e4SLinus Torvalds  */
4641da177e4SLinus Torvalds 
4658d1c802bSDavid Ahern static inline struct fib6_info *rt6_device_match(struct net *net,
4668d1c802bSDavid Ahern 						 struct fib6_info *rt,
467b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4681da177e4SLinus Torvalds 						    int oif,
469d420895eSYOSHIFUJI Hideaki 						    int flags)
4701da177e4SLinus Torvalds {
4718d1c802bSDavid Ahern 	struct fib6_info *sprt;
4721da177e4SLinus Torvalds 
4735e670d84SDavid Ahern 	if (!oif && ipv6_addr_any(saddr) &&
4745e670d84SDavid Ahern 	    !(rt->fib6_nh.nh_flags & RTNH_F_DEAD))
4758067bb8cSIdo Schimmel 		return rt;
476dd3abc4eSYOSHIFUJI Hideaki 
4778fb11a9aSDavid Ahern 	for (sprt = rt; sprt; sprt = rcu_dereference(sprt->fib6_next)) {
4785e670d84SDavid Ahern 		const struct net_device *dev = sprt->fib6_nh.nh_dev;
479dd3abc4eSYOSHIFUJI Hideaki 
4805e670d84SDavid Ahern 		if (sprt->fib6_nh.nh_flags & RTNH_F_DEAD)
4818067bb8cSIdo Schimmel 			continue;
4828067bb8cSIdo Schimmel 
483dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4841da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4851da177e4SLinus Torvalds 				return sprt;
486dd3abc4eSYOSHIFUJI Hideaki 		} else {
487dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
488dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
489dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
490dd3abc4eSYOSHIFUJI Hideaki 		}
4911da177e4SLinus Torvalds 	}
4921da177e4SLinus Torvalds 
493eea68cd3SDavid Ahern 	if (oif && flags & RT6_LOOKUP_F_IFACE)
494421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
4951da177e4SLinus Torvalds 
496421842edSDavid Ahern 	return rt->fib6_nh.nh_flags & RTNH_F_DEAD ? net->ipv6.fib6_null_entry : rt;
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds 
49927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
500c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
501c2f17e82SHannes Frederic Sowa 	struct work_struct work;
502c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
503c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
504c2f17e82SHannes Frederic Sowa };
505c2f17e82SHannes Frederic Sowa 
506c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
507c2f17e82SHannes Frederic Sowa {
508c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
509c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
510c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
511c2f17e82SHannes Frederic Sowa 
512c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
513adc176c5SErik Nordmark 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
514c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
515662f5533SMichael Büsch 	kfree(work);
516c2f17e82SHannes Frederic Sowa }
517c2f17e82SHannes Frederic Sowa 
5188d1c802bSDavid Ahern static void rt6_probe(struct fib6_info *rt)
51927097255SYOSHIFUJI Hideaki {
520990edb42SMartin KaFai Lau 	struct __rt6_probe_work *work;
5215e670d84SDavid Ahern 	const struct in6_addr *nh_gw;
522f2c31e32SEric Dumazet 	struct neighbour *neigh;
5235e670d84SDavid Ahern 	struct net_device *dev;
5245e670d84SDavid Ahern 
52527097255SYOSHIFUJI Hideaki 	/*
52627097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
52727097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
52827097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
52927097255SYOSHIFUJI Hideaki 	 *
53027097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
53127097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
53227097255SYOSHIFUJI Hideaki 	 */
53393c2fb25SDavid Ahern 	if (!rt || !(rt->fib6_flags & RTF_GATEWAY))
534fdd6681dSAmerigo Wang 		return;
5355e670d84SDavid Ahern 
5365e670d84SDavid Ahern 	nh_gw = &rt->fib6_nh.nh_gw;
5375e670d84SDavid Ahern 	dev = rt->fib6_nh.nh_dev;
5382152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
5395e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
5402152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
541dcd1f572SDavid Ahern 		struct inet6_dev *idev;
542dcd1f572SDavid Ahern 
5438d6c31bfSMartin KaFai Lau 		if (neigh->nud_state & NUD_VALID)
5448d6c31bfSMartin KaFai Lau 			goto out;
5458d6c31bfSMartin KaFai Lau 
546dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
547990edb42SMartin KaFai Lau 		work = NULL;
5482152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
549990edb42SMartin KaFai Lau 		if (!(neigh->nud_state & NUD_VALID) &&
550990edb42SMartin KaFai Lau 		    time_after(jiffies,
551dcd1f572SDavid Ahern 			       neigh->updated + idev->cnf.rtr_probe_interval)) {
552c2f17e82SHannes Frederic Sowa 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
553990edb42SMartin KaFai Lau 			if (work)
5547e980569SJiri Benc 				__neigh_set_probe_once(neigh);
555990edb42SMartin KaFai Lau 		}
556c2f17e82SHannes Frederic Sowa 		write_unlock(&neigh->lock);
557990edb42SMartin KaFai Lau 	} else {
558990edb42SMartin KaFai Lau 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
559990edb42SMartin KaFai Lau 	}
560c2f17e82SHannes Frederic Sowa 
561c2f17e82SHannes Frederic Sowa 	if (work) {
562c2f17e82SHannes Frederic Sowa 		INIT_WORK(&work->work, rt6_probe_deferred);
5635e670d84SDavid Ahern 		work->target = *nh_gw;
5645e670d84SDavid Ahern 		dev_hold(dev);
5655e670d84SDavid Ahern 		work->dev = dev;
566c2f17e82SHannes Frederic Sowa 		schedule_work(&work->work);
567c2f17e82SHannes Frederic Sowa 	}
568990edb42SMartin KaFai Lau 
5698d6c31bfSMartin KaFai Lau out:
5702152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
571f2c31e32SEric Dumazet }
57227097255SYOSHIFUJI Hideaki #else
5738d1c802bSDavid Ahern static inline void rt6_probe(struct fib6_info *rt)
57427097255SYOSHIFUJI Hideaki {
57527097255SYOSHIFUJI Hideaki }
57627097255SYOSHIFUJI Hideaki #endif
57727097255SYOSHIFUJI Hideaki 
5781da177e4SLinus Torvalds /*
579554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5801da177e4SLinus Torvalds  */
5818d1c802bSDavid Ahern static inline int rt6_check_dev(struct fib6_info *rt, int oif)
5821da177e4SLinus Torvalds {
5835e670d84SDavid Ahern 	const struct net_device *dev = rt->fib6_nh.nh_dev;
5845e670d84SDavid Ahern 
585161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
586554cfb7eSYOSHIFUJI Hideaki 		return 2;
587554cfb7eSYOSHIFUJI Hideaki 	return 0;
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
5908d1c802bSDavid Ahern static inline enum rt6_nud_state rt6_check_neigh(struct fib6_info *rt)
5911da177e4SLinus Torvalds {
592afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
5935e670d84SDavid Ahern 	struct neighbour *neigh;
594f2c31e32SEric Dumazet 
59593c2fb25SDavid Ahern 	if (rt->fib6_flags & RTF_NONEXTHOP ||
59693c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_GATEWAY))
597afc154e9SHannes Frederic Sowa 		return RT6_NUD_SUCCEED;
598145a3621SYOSHIFUJI Hideaki / 吉藤英明 
599145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
6005e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(rt->fib6_nh.nh_dev,
6015e670d84SDavid Ahern 					  &rt->fib6_nh.nh_gw);
602145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
603145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
604554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
605afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
606398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
607a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
608afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
6097e980569SJiri Benc 		else
6107e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
611398bcbebSYOSHIFUJI Hideaki #endif
612145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
613afc154e9SHannes Frederic Sowa 	} else {
614afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
6157e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
616a5a81f0bSPaul Marks 	}
617145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
618145a3621SYOSHIFUJI Hideaki / 吉藤英明 
619a5a81f0bSPaul Marks 	return ret;
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
6228d1c802bSDavid Ahern static int rt6_score_route(struct fib6_info *rt, int oif, int strict)
623554cfb7eSYOSHIFUJI Hideaki {
624a5a81f0bSPaul Marks 	int m;
6254d0c5911SYOSHIFUJI Hideaki 
6264d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
62777d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
628afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
629ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
63093c2fb25SDavid Ahern 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->fib6_flags)) << 2;
631ebacaaa0SYOSHIFUJI Hideaki #endif
632afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE) {
633afc154e9SHannes Frederic Sowa 		int n = rt6_check_neigh(rt);
634afc154e9SHannes Frederic Sowa 		if (n < 0)
635afc154e9SHannes Frederic Sowa 			return n;
636afc154e9SHannes Frederic Sowa 	}
637554cfb7eSYOSHIFUJI Hideaki 	return m;
638554cfb7eSYOSHIFUJI Hideaki }
639554cfb7eSYOSHIFUJI Hideaki 
640dcd1f572SDavid Ahern /* called with rc_read_lock held */
641dcd1f572SDavid Ahern static inline bool fib6_ignore_linkdown(const struct fib6_info *f6i)
642dcd1f572SDavid Ahern {
643dcd1f572SDavid Ahern 	const struct net_device *dev = fib6_info_nh_dev(f6i);
644dcd1f572SDavid Ahern 	bool rc = false;
645dcd1f572SDavid Ahern 
646dcd1f572SDavid Ahern 	if (dev) {
647dcd1f572SDavid Ahern 		const struct inet6_dev *idev = __in6_dev_get(dev);
648dcd1f572SDavid Ahern 
649dcd1f572SDavid Ahern 		rc = !!idev->cnf.ignore_routes_with_linkdown;
650dcd1f572SDavid Ahern 	}
651dcd1f572SDavid Ahern 
652dcd1f572SDavid Ahern 	return rc;
653dcd1f572SDavid Ahern }
654dcd1f572SDavid Ahern 
6558d1c802bSDavid Ahern static struct fib6_info *find_match(struct fib6_info *rt, int oif, int strict,
6568d1c802bSDavid Ahern 				   int *mpri, struct fib6_info *match,
657afc154e9SHannes Frederic Sowa 				   bool *do_rr)
658554cfb7eSYOSHIFUJI Hideaki {
659554cfb7eSYOSHIFUJI Hideaki 	int m;
660afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
66135103d11SAndy Gospodarek 
6625e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
6638067bb8cSIdo Schimmel 		goto out;
6648067bb8cSIdo Schimmel 
665dcd1f572SDavid Ahern 	if (fib6_ignore_linkdown(rt) &&
6665e670d84SDavid Ahern 	    rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
667d5d32e4bSDavid Ahern 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
66835103d11SAndy Gospodarek 		goto out;
669554cfb7eSYOSHIFUJI Hideaki 
67014895687SDavid Ahern 	if (fib6_check_expired(rt))
671f11e6659SDavid S. Miller 		goto out;
672554cfb7eSYOSHIFUJI Hideaki 
673554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
6747e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
675afc154e9SHannes Frederic Sowa 		match_do_rr = true;
676afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
6777e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
678f11e6659SDavid S. Miller 		goto out;
6791da177e4SLinus Torvalds 	}
680f11e6659SDavid S. Miller 
681afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
682afc154e9SHannes Frederic Sowa 		rt6_probe(rt);
683afc154e9SHannes Frederic Sowa 
6847e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
685afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
686afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
687afc154e9SHannes Frederic Sowa 		*mpri = m;
688afc154e9SHannes Frederic Sowa 		match = rt;
689afc154e9SHannes Frederic Sowa 	}
690f11e6659SDavid S. Miller out:
691f11e6659SDavid S. Miller 	return match;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6948d1c802bSDavid Ahern static struct fib6_info *find_rr_leaf(struct fib6_node *fn,
6958d1c802bSDavid Ahern 				     struct fib6_info *leaf,
6968d1c802bSDavid Ahern 				     struct fib6_info *rr_head,
697afc154e9SHannes Frederic Sowa 				     u32 metric, int oif, int strict,
698afc154e9SHannes Frederic Sowa 				     bool *do_rr)
699f11e6659SDavid S. Miller {
7008d1c802bSDavid Ahern 	struct fib6_info *rt, *match, *cont;
701f11e6659SDavid S. Miller 	int mpri = -1;
702f11e6659SDavid S. Miller 
703f11e6659SDavid S. Miller 	match = NULL;
7049fbdcfafSSteffen Klassert 	cont = NULL;
7058fb11a9aSDavid Ahern 	for (rt = rr_head; rt; rt = rcu_dereference(rt->fib6_next)) {
70693c2fb25SDavid Ahern 		if (rt->fib6_metric != metric) {
7079fbdcfafSSteffen Klassert 			cont = rt;
7089fbdcfafSSteffen Klassert 			break;
7099fbdcfafSSteffen Klassert 		}
7109fbdcfafSSteffen Klassert 
711afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
7129fbdcfafSSteffen Klassert 	}
7139fbdcfafSSteffen Klassert 
71466f5d6ceSWei Wang 	for (rt = leaf; rt && rt != rr_head;
7158fb11a9aSDavid Ahern 	     rt = rcu_dereference(rt->fib6_next)) {
71693c2fb25SDavid Ahern 		if (rt->fib6_metric != metric) {
7179fbdcfafSSteffen Klassert 			cont = rt;
7189fbdcfafSSteffen Klassert 			break;
7199fbdcfafSSteffen Klassert 		}
7209fbdcfafSSteffen Klassert 
7219fbdcfafSSteffen Klassert 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
7229fbdcfafSSteffen Klassert 	}
7239fbdcfafSSteffen Klassert 
7249fbdcfafSSteffen Klassert 	if (match || !cont)
7259fbdcfafSSteffen Klassert 		return match;
7269fbdcfafSSteffen Klassert 
7278fb11a9aSDavid Ahern 	for (rt = cont; rt; rt = rcu_dereference(rt->fib6_next))
728afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
729f11e6659SDavid S. Miller 
730f11e6659SDavid S. Miller 	return match;
731f11e6659SDavid S. Miller }
732f11e6659SDavid S. Miller 
7338d1c802bSDavid Ahern static struct fib6_info *rt6_select(struct net *net, struct fib6_node *fn,
7348d1040e8SWei Wang 				   int oif, int strict)
735f11e6659SDavid S. Miller {
7368d1c802bSDavid Ahern 	struct fib6_info *leaf = rcu_dereference(fn->leaf);
7378d1c802bSDavid Ahern 	struct fib6_info *match, *rt0;
738afc154e9SHannes Frederic Sowa 	bool do_rr = false;
73917ecf590SWei Wang 	int key_plen;
740f11e6659SDavid S. Miller 
741421842edSDavid Ahern 	if (!leaf || leaf == net->ipv6.fib6_null_entry)
742421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
7438d1040e8SWei Wang 
74466f5d6ceSWei Wang 	rt0 = rcu_dereference(fn->rr_ptr);
745f11e6659SDavid S. Miller 	if (!rt0)
74666f5d6ceSWei Wang 		rt0 = leaf;
747f11e6659SDavid S. Miller 
74817ecf590SWei Wang 	/* Double check to make sure fn is not an intermediate node
74917ecf590SWei Wang 	 * and fn->leaf does not points to its child's leaf
75017ecf590SWei Wang 	 * (This might happen if all routes under fn are deleted from
75117ecf590SWei Wang 	 * the tree and fib6_repair_tree() is called on the node.)
75217ecf590SWei Wang 	 */
75393c2fb25SDavid Ahern 	key_plen = rt0->fib6_dst.plen;
75417ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES
75593c2fb25SDavid Ahern 	if (rt0->fib6_src.plen)
75693c2fb25SDavid Ahern 		key_plen = rt0->fib6_src.plen;
75717ecf590SWei Wang #endif
75817ecf590SWei Wang 	if (fn->fn_bit != key_plen)
759421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
76017ecf590SWei Wang 
76193c2fb25SDavid Ahern 	match = find_rr_leaf(fn, leaf, rt0, rt0->fib6_metric, oif, strict,
762afc154e9SHannes Frederic Sowa 			     &do_rr);
763f11e6659SDavid S. Miller 
764afc154e9SHannes Frederic Sowa 	if (do_rr) {
7658fb11a9aSDavid Ahern 		struct fib6_info *next = rcu_dereference(rt0->fib6_next);
766f11e6659SDavid S. Miller 
767554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
76893c2fb25SDavid Ahern 		if (!next || next->fib6_metric != rt0->fib6_metric)
7698d1040e8SWei Wang 			next = leaf;
770f11e6659SDavid S. Miller 
77166f5d6ceSWei Wang 		if (next != rt0) {
77293c2fb25SDavid Ahern 			spin_lock_bh(&leaf->fib6_table->tb6_lock);
77366f5d6ceSWei Wang 			/* make sure next is not being deleted from the tree */
77493c2fb25SDavid Ahern 			if (next->fib6_node)
77566f5d6ceSWei Wang 				rcu_assign_pointer(fn->rr_ptr, next);
77693c2fb25SDavid Ahern 			spin_unlock_bh(&leaf->fib6_table->tb6_lock);
77766f5d6ceSWei Wang 		}
778554cfb7eSYOSHIFUJI Hideaki 	}
779554cfb7eSYOSHIFUJI Hideaki 
780421842edSDavid Ahern 	return match ? match : net->ipv6.fib6_null_entry;
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
7838d1c802bSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_info *rt)
7848b9df265SMartin KaFai Lau {
78593c2fb25SDavid Ahern 	return (rt->fib6_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
7868b9df265SMartin KaFai Lau }
7878b9df265SMartin KaFai Lau 
78870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
78970ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
790b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
79170ceb4f5SYOSHIFUJI Hideaki {
792c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
79370ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
79470ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
79570ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
7964bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
7978d1c802bSDavid Ahern 	struct fib6_info *rt;
79870ceb4f5SYOSHIFUJI Hideaki 
79970ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
80070ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
80170ceb4f5SYOSHIFUJI Hideaki 	}
80270ceb4f5SYOSHIFUJI Hideaki 
80370ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
80470ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
80570ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
80670ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
80770ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
80870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
80970ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
81070ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
81170ceb4f5SYOSHIFUJI Hideaki 		}
81270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
81370ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
81470ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
81570ceb4f5SYOSHIFUJI Hideaki 		}
81670ceb4f5SYOSHIFUJI Hideaki 	}
81770ceb4f5SYOSHIFUJI Hideaki 
81870ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
81970ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
8203933fc95SJens Rosenboom 		return -EINVAL;
82170ceb4f5SYOSHIFUJI Hideaki 
8224bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
82370ceb4f5SYOSHIFUJI Hideaki 
82470ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
82570ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
82670ceb4f5SYOSHIFUJI Hideaki 	else {
82770ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
82870ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
82970ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
83070ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
83170ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
83270ceb4f5SYOSHIFUJI Hideaki 	}
83370ceb4f5SYOSHIFUJI Hideaki 
834f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
835afb1d4b5SDavid Ahern 		rt = rt6_get_dflt_router(net, gwaddr, dev);
836f104a567SDuan Jiong 	else
837f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
838830218c1SDavid Ahern 					gwaddr, dev);
83970ceb4f5SYOSHIFUJI Hideaki 
84070ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
841afb1d4b5SDavid Ahern 		ip6_del_rt(net, rt);
84270ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
84370ceb4f5SYOSHIFUJI Hideaki 	}
84470ceb4f5SYOSHIFUJI Hideaki 
84570ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
846830218c1SDavid Ahern 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
847830218c1SDavid Ahern 					dev, pref);
84870ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
84993c2fb25SDavid Ahern 		rt->fib6_flags = RTF_ROUTEINFO |
85093c2fb25SDavid Ahern 				 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
85170ceb4f5SYOSHIFUJI Hideaki 
85270ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
8531716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
85414895687SDavid Ahern 			fib6_clean_expires(rt);
8551716a961SGao feng 		else
85614895687SDavid Ahern 			fib6_set_expires(rt, jiffies + HZ * lifetime);
8571716a961SGao feng 
85893531c67SDavid Ahern 		fib6_info_release(rt);
85970ceb4f5SYOSHIFUJI Hideaki 	}
86070ceb4f5SYOSHIFUJI Hideaki 	return 0;
86170ceb4f5SYOSHIFUJI Hideaki }
86270ceb4f5SYOSHIFUJI Hideaki #endif
86370ceb4f5SYOSHIFUJI Hideaki 
864ae90d867SDavid Ahern /*
865ae90d867SDavid Ahern  *	Misc support functions
866ae90d867SDavid Ahern  */
867ae90d867SDavid Ahern 
868ae90d867SDavid Ahern /* called with rcu_lock held */
8698d1c802bSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct fib6_info *rt)
870ae90d867SDavid Ahern {
8715e670d84SDavid Ahern 	struct net_device *dev = rt->fib6_nh.nh_dev;
872ae90d867SDavid Ahern 
87393c2fb25SDavid Ahern 	if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
874ae90d867SDavid Ahern 		/* for copies of local routes, dst->dev needs to be the
875ae90d867SDavid Ahern 		 * device if it is a master device, the master device if
876ae90d867SDavid Ahern 		 * device is enslaved, and the loopback as the default
877ae90d867SDavid Ahern 		 */
878ae90d867SDavid Ahern 		if (netif_is_l3_slave(dev) &&
87993c2fb25SDavid Ahern 		    !rt6_need_strict(&rt->fib6_dst.addr))
880ae90d867SDavid Ahern 			dev = l3mdev_master_dev_rcu(dev);
881ae90d867SDavid Ahern 		else if (!netif_is_l3_master(dev))
882ae90d867SDavid Ahern 			dev = dev_net(dev)->loopback_dev;
883ae90d867SDavid Ahern 		/* last case is netif_is_l3_master(dev) is true in which
884ae90d867SDavid Ahern 		 * case we want dev returned to be dev
885ae90d867SDavid Ahern 		 */
886ae90d867SDavid Ahern 	}
887ae90d867SDavid Ahern 
888ae90d867SDavid Ahern 	return dev;
889ae90d867SDavid Ahern }
890ae90d867SDavid Ahern 
8916edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = {
8926edb3c96SDavid Ahern 	[RTN_UNSPEC]	= 0,
8936edb3c96SDavid Ahern 	[RTN_UNICAST]	= 0,
8946edb3c96SDavid Ahern 	[RTN_LOCAL]	= 0,
8956edb3c96SDavid Ahern 	[RTN_BROADCAST]	= 0,
8966edb3c96SDavid Ahern 	[RTN_ANYCAST]	= 0,
8976edb3c96SDavid Ahern 	[RTN_MULTICAST]	= 0,
8986edb3c96SDavid Ahern 	[RTN_BLACKHOLE]	= -EINVAL,
8996edb3c96SDavid Ahern 	[RTN_UNREACHABLE] = -EHOSTUNREACH,
9006edb3c96SDavid Ahern 	[RTN_PROHIBIT]	= -EACCES,
9016edb3c96SDavid Ahern 	[RTN_THROW]	= -EAGAIN,
9026edb3c96SDavid Ahern 	[RTN_NAT]	= -EINVAL,
9036edb3c96SDavid Ahern 	[RTN_XRESOLVE]	= -EINVAL,
9046edb3c96SDavid Ahern };
9056edb3c96SDavid Ahern 
9066edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type)
9076edb3c96SDavid Ahern {
9086edb3c96SDavid Ahern 	return fib6_prop[fib6_type];
9096edb3c96SDavid Ahern }
9106edb3c96SDavid Ahern 
9118d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
9123b6761d1SDavid Ahern {
9133b6761d1SDavid Ahern 	unsigned short flags = 0;
9143b6761d1SDavid Ahern 
9153b6761d1SDavid Ahern 	if (rt->dst_nocount)
9163b6761d1SDavid Ahern 		flags |= DST_NOCOUNT;
9173b6761d1SDavid Ahern 	if (rt->dst_nopolicy)
9183b6761d1SDavid Ahern 		flags |= DST_NOPOLICY;
9193b6761d1SDavid Ahern 	if (rt->dst_host)
9203b6761d1SDavid Ahern 		flags |= DST_HOST;
9213b6761d1SDavid Ahern 
9223b6761d1SDavid Ahern 	return flags;
9233b6761d1SDavid Ahern }
9243b6761d1SDavid Ahern 
9258d1c802bSDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, struct fib6_info *ort)
9266edb3c96SDavid Ahern {
9276edb3c96SDavid Ahern 	rt->dst.error = ip6_rt_type_to_error(ort->fib6_type);
9286edb3c96SDavid Ahern 
9296edb3c96SDavid Ahern 	switch (ort->fib6_type) {
9306edb3c96SDavid Ahern 	case RTN_BLACKHOLE:
9316edb3c96SDavid Ahern 		rt->dst.output = dst_discard_out;
9326edb3c96SDavid Ahern 		rt->dst.input = dst_discard;
9336edb3c96SDavid Ahern 		break;
9346edb3c96SDavid Ahern 	case RTN_PROHIBIT:
9356edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_prohibit_out;
9366edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_prohibit;
9376edb3c96SDavid Ahern 		break;
9386edb3c96SDavid Ahern 	case RTN_THROW:
9396edb3c96SDavid Ahern 	case RTN_UNREACHABLE:
9406edb3c96SDavid Ahern 	default:
9416edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_discard_out;
9426edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_discard;
9436edb3c96SDavid Ahern 		break;
9446edb3c96SDavid Ahern 	}
9456edb3c96SDavid Ahern }
9466edb3c96SDavid Ahern 
9478d1c802bSDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, struct fib6_info *ort)
9486edb3c96SDavid Ahern {
94993c2fb25SDavid Ahern 	if (ort->fib6_flags & RTF_REJECT) {
9506edb3c96SDavid Ahern 		ip6_rt_init_dst_reject(rt, ort);
9516edb3c96SDavid Ahern 		return;
9526edb3c96SDavid Ahern 	}
9536edb3c96SDavid Ahern 
9546edb3c96SDavid Ahern 	rt->dst.error = 0;
9556edb3c96SDavid Ahern 	rt->dst.output = ip6_output;
9566edb3c96SDavid Ahern 
957d23c4b63SHangbin Liu 	if (ort->fib6_type == RTN_LOCAL || ort->fib6_type == RTN_ANYCAST) {
9586edb3c96SDavid Ahern 		rt->dst.input = ip6_input;
95993c2fb25SDavid Ahern 	} else if (ipv6_addr_type(&ort->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
9606edb3c96SDavid Ahern 		rt->dst.input = ip6_mc_input;
9616edb3c96SDavid Ahern 	} else {
9626edb3c96SDavid Ahern 		rt->dst.input = ip6_forward;
9636edb3c96SDavid Ahern 	}
9646edb3c96SDavid Ahern 
9656edb3c96SDavid Ahern 	if (ort->fib6_nh.nh_lwtstate) {
9666edb3c96SDavid Ahern 		rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate);
9676edb3c96SDavid Ahern 		lwtunnel_set_redirect(&rt->dst);
9686edb3c96SDavid Ahern 	}
9696edb3c96SDavid Ahern 
9706edb3c96SDavid Ahern 	rt->dst.lastuse = jiffies;
9716edb3c96SDavid Ahern }
9726edb3c96SDavid Ahern 
973e873e4b9SWei Wang /* Caller must already hold reference to @from */
9748d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
975ae90d867SDavid Ahern {
976ae90d867SDavid Ahern 	rt->rt6i_flags &= ~RTF_EXPIRES;
977a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, from);
978e1255ed4SDavid Ahern 	ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
979ae90d867SDavid Ahern }
980ae90d867SDavid Ahern 
981e873e4b9SWei Wang /* Caller must already hold reference to @ort */
9828d1c802bSDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, struct fib6_info *ort)
983ae90d867SDavid Ahern {
984dcd1f572SDavid Ahern 	struct net_device *dev = fib6_info_nh_dev(ort);
985dcd1f572SDavid Ahern 
9866edb3c96SDavid Ahern 	ip6_rt_init_dst(rt, ort);
9876edb3c96SDavid Ahern 
98893c2fb25SDavid Ahern 	rt->rt6i_dst = ort->fib6_dst;
989dcd1f572SDavid Ahern 	rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
9905e670d84SDavid Ahern 	rt->rt6i_gateway = ort->fib6_nh.nh_gw;
99193c2fb25SDavid Ahern 	rt->rt6i_flags = ort->fib6_flags;
992ae90d867SDavid Ahern 	rt6_set_from(rt, ort);
993ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
99493c2fb25SDavid Ahern 	rt->rt6i_src = ort->fib6_src;
995ae90d867SDavid Ahern #endif
996ae90d867SDavid Ahern }
997ae90d867SDavid Ahern 
998a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
999a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
1000a3c00e46SMartin KaFai Lau {
100166f5d6ceSWei Wang 	struct fib6_node *pn, *sn;
1002a3c00e46SMartin KaFai Lau 	while (1) {
1003a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
1004a3c00e46SMartin KaFai Lau 			return NULL;
100566f5d6ceSWei Wang 		pn = rcu_dereference(fn->parent);
100666f5d6ceSWei Wang 		sn = FIB6_SUBTREE(pn);
100766f5d6ceSWei Wang 		if (sn && sn != fn)
10086454743bSDavid Ahern 			fn = fib6_node_lookup(sn, NULL, saddr);
1009a3c00e46SMartin KaFai Lau 		else
1010a3c00e46SMartin KaFai Lau 			fn = pn;
1011a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
1012a3c00e46SMartin KaFai Lau 			return fn;
1013a3c00e46SMartin KaFai Lau 	}
1014a3c00e46SMartin KaFai Lau }
1015c71099acSThomas Graf 
1016d3843fe5SWei Wang static bool ip6_hold_safe(struct net *net, struct rt6_info **prt,
1017d3843fe5SWei Wang 			  bool null_fallback)
1018d3843fe5SWei Wang {
1019d3843fe5SWei Wang 	struct rt6_info *rt = *prt;
1020d3843fe5SWei Wang 
1021d3843fe5SWei Wang 	if (dst_hold_safe(&rt->dst))
1022d3843fe5SWei Wang 		return true;
1023d3843fe5SWei Wang 	if (null_fallback) {
1024d3843fe5SWei Wang 		rt = net->ipv6.ip6_null_entry;
1025d3843fe5SWei Wang 		dst_hold(&rt->dst);
1026d3843fe5SWei Wang 	} else {
1027d3843fe5SWei Wang 		rt = NULL;
1028d3843fe5SWei Wang 	}
1029d3843fe5SWei Wang 	*prt = rt;
1030d3843fe5SWei Wang 	return false;
1031d3843fe5SWei Wang }
1032d3843fe5SWei Wang 
1033dec9b0e2SDavid Ahern /* called with rcu_lock held */
10348d1c802bSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(struct fib6_info *rt)
1035dec9b0e2SDavid Ahern {
10363b6761d1SDavid Ahern 	unsigned short flags = fib6_info_dst_flags(rt);
1037dec9b0e2SDavid Ahern 	struct net_device *dev = rt->fib6_nh.nh_dev;
1038dec9b0e2SDavid Ahern 	struct rt6_info *nrt;
1039dec9b0e2SDavid Ahern 
1040e873e4b9SWei Wang 	if (!fib6_info_hold_safe(rt))
1041e873e4b9SWei Wang 		return NULL;
1042e873e4b9SWei Wang 
104393531c67SDavid Ahern 	nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
1044dec9b0e2SDavid Ahern 	if (nrt)
1045dec9b0e2SDavid Ahern 		ip6_rt_copy_init(nrt, rt);
1046e873e4b9SWei Wang 	else
1047e873e4b9SWei Wang 		fib6_info_release(rt);
1048dec9b0e2SDavid Ahern 
1049dec9b0e2SDavid Ahern 	return nrt;
1050dec9b0e2SDavid Ahern }
1051dec9b0e2SDavid Ahern 
10528ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
10538ed67789SDaniel Lezcano 					     struct fib6_table *table,
1054b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
1055b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
1056b75cc8f9SDavid Ahern 					     int flags)
10571da177e4SLinus Torvalds {
10588d1c802bSDavid Ahern 	struct fib6_info *f6i;
10591da177e4SLinus Torvalds 	struct fib6_node *fn;
106023fb93a4SDavid Ahern 	struct rt6_info *rt;
10611da177e4SLinus Torvalds 
1062b6cdbc85SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1063b6cdbc85SDavid Ahern 		flags &= ~RT6_LOOKUP_F_IFACE;
1064b6cdbc85SDavid Ahern 
106566f5d6ceSWei Wang 	rcu_read_lock();
10666454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1067c71099acSThomas Graf restart:
106823fb93a4SDavid Ahern 	f6i = rcu_dereference(fn->leaf);
106923fb93a4SDavid Ahern 	if (!f6i) {
107023fb93a4SDavid Ahern 		f6i = net->ipv6.fib6_null_entry;
107166f5d6ceSWei Wang 	} else {
107223fb93a4SDavid Ahern 		f6i = rt6_device_match(net, f6i, &fl6->saddr,
107366f5d6ceSWei Wang 				      fl6->flowi6_oif, flags);
107493c2fb25SDavid Ahern 		if (f6i->fib6_nsiblings && fl6->flowi6_oif == 0)
10753b290a31SDavid Ahern 			f6i = fib6_multipath_select(net, f6i, fl6,
10763b290a31SDavid Ahern 						    fl6->flowi6_oif, skb,
10773b290a31SDavid Ahern 						    flags);
107866f5d6ceSWei Wang 	}
107923fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1080a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1081a3c00e46SMartin KaFai Lau 		if (fn)
1082a3c00e46SMartin KaFai Lau 			goto restart;
1083a3c00e46SMartin KaFai Lau 	}
10842b760fcfSWei Wang 
1085d4bea421SDavid Ahern 	trace_fib6_table_lookup(net, f6i, table, fl6);
1086d4bea421SDavid Ahern 
10874c9483b2SDavid S. Miller 	/* Search through exception table */
108823fb93a4SDavid Ahern 	rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
108923fb93a4SDavid Ahern 	if (rt) {
1090d3843fe5SWei Wang 		if (ip6_hold_safe(net, &rt, true))
1091d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
109223fb93a4SDavid Ahern 	} else if (f6i == net->ipv6.fib6_null_entry) {
1093dec9b0e2SDavid Ahern 		rt = net->ipv6.ip6_null_entry;
1094dec9b0e2SDavid Ahern 		dst_hold(&rt->dst);
109523fb93a4SDavid Ahern 	} else {
109623fb93a4SDavid Ahern 		rt = ip6_create_rt_rcu(f6i);
109723fb93a4SDavid Ahern 		if (!rt) {
109823fb93a4SDavid Ahern 			rt = net->ipv6.ip6_null_entry;
109923fb93a4SDavid Ahern 			dst_hold(&rt->dst);
110023fb93a4SDavid Ahern 		}
1101dec9b0e2SDavid Ahern 	}
1102d3843fe5SWei Wang 
110366f5d6ceSWei Wang 	rcu_read_unlock();
1104b811580dSDavid Ahern 
11051da177e4SLinus Torvalds 	return rt;
1106c71099acSThomas Graf }
1107c71099acSThomas Graf 
1108ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1109b75cc8f9SDavid Ahern 				   const struct sk_buff *skb, int flags)
1110ea6e574eSFlorian Westphal {
1111b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1112ea6e574eSFlorian Westphal }
1113ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
1114ea6e574eSFlorian Westphal 
11159acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1116b75cc8f9SDavid Ahern 			    const struct in6_addr *saddr, int oif,
1117b75cc8f9SDavid Ahern 			    const struct sk_buff *skb, int strict)
1118c71099acSThomas Graf {
11194c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
11204c9483b2SDavid S. Miller 		.flowi6_oif = oif,
11214c9483b2SDavid S. Miller 		.daddr = *daddr,
1122c71099acSThomas Graf 	};
1123c71099acSThomas Graf 	struct dst_entry *dst;
112477d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1125c71099acSThomas Graf 
1126adaa70bbSThomas Graf 	if (saddr) {
11274c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1128adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1129adaa70bbSThomas Graf 	}
1130adaa70bbSThomas Graf 
1131b75cc8f9SDavid Ahern 	dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1132c71099acSThomas Graf 	if (dst->error == 0)
1133c71099acSThomas Graf 		return (struct rt6_info *) dst;
1134c71099acSThomas Graf 
1135c71099acSThomas Graf 	dst_release(dst);
1136c71099acSThomas Graf 
11371da177e4SLinus Torvalds 	return NULL;
11381da177e4SLinus Torvalds }
11397159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
11407159039aSYOSHIFUJI Hideaki 
1141c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
11421cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
11431cfb71eeSWei Wang  * route is released.
11441cfb71eeSWei Wang  * Caller must hold dst before calling it.
11451da177e4SLinus Torvalds  */
11461da177e4SLinus Torvalds 
11478d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1148333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
11491da177e4SLinus Torvalds {
11501da177e4SLinus Torvalds 	int err;
1151c71099acSThomas Graf 	struct fib6_table *table;
11521da177e4SLinus Torvalds 
115393c2fb25SDavid Ahern 	table = rt->fib6_table;
115466f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
1155d4ead6b3SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, extack);
115666f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	return err;
11591da177e4SLinus Torvalds }
11601da177e4SLinus Torvalds 
11618d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt)
116240e22e8fSThomas Graf {
1163afb1d4b5SDavid Ahern 	struct nl_info info = {	.nl_net = net, };
1164e715b6d3SFlorian Westphal 
1165d4ead6b3SDavid Ahern 	return __ip6_ins_rt(rt, &info, NULL);
116640e22e8fSThomas Graf }
116740e22e8fSThomas Graf 
11688d1c802bSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(struct fib6_info *ort,
116921efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
1170b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
11711da177e4SLinus Torvalds {
11724832c30dSDavid Ahern 	struct net_device *dev;
11731da177e4SLinus Torvalds 	struct rt6_info *rt;
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	/*
11761da177e4SLinus Torvalds 	 *	Clone the route.
11771da177e4SLinus Torvalds 	 */
11781da177e4SLinus Torvalds 
1179e873e4b9SWei Wang 	if (!fib6_info_hold_safe(ort))
1180e873e4b9SWei Wang 		return NULL;
1181e873e4b9SWei Wang 
11824832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(ort);
118393531c67SDavid Ahern 	rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1184e873e4b9SWei Wang 	if (!rt) {
1185e873e4b9SWei Wang 		fib6_info_release(ort);
118683a09abdSMartin KaFai Lau 		return NULL;
1187e873e4b9SWei Wang 	}
118883a09abdSMartin KaFai Lau 
118983a09abdSMartin KaFai Lau 	ip6_rt_copy_init(rt, ort);
11908b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
119183a09abdSMartin KaFai Lau 	rt->dst.flags |= DST_HOST;
119283a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
119383a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
11948b9df265SMartin KaFai Lau 
11958b9df265SMartin KaFai Lau 	if (!rt6_is_gw_or_nonexthop(ort)) {
119693c2fb25SDavid Ahern 		if (ort->fib6_dst.plen != 128 &&
119793c2fb25SDavid Ahern 		    ipv6_addr_equal(&ort->fib6_dst.addr, daddr))
119858c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
11991da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
12001da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
12014e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
12021da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
12031da177e4SLinus Torvalds 		}
12041da177e4SLinus Torvalds #endif
120595a9a5baSYOSHIFUJI Hideaki 	}
120695a9a5baSYOSHIFUJI Hideaki 
1207299d9939SYOSHIFUJI Hideaki 	return rt;
1208299d9939SYOSHIFUJI Hideaki }
1209299d9939SYOSHIFUJI Hideaki 
12108d1c802bSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(struct fib6_info *rt)
1211d52d3997SMartin KaFai Lau {
12123b6761d1SDavid Ahern 	unsigned short flags = fib6_info_dst_flags(rt);
12134832c30dSDavid Ahern 	struct net_device *dev;
1214d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1215d52d3997SMartin KaFai Lau 
1216e873e4b9SWei Wang 	if (!fib6_info_hold_safe(rt))
1217e873e4b9SWei Wang 		return NULL;
1218e873e4b9SWei Wang 
12194832c30dSDavid Ahern 	rcu_read_lock();
12204832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(rt);
122193531c67SDavid Ahern 	pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
12224832c30dSDavid Ahern 	rcu_read_unlock();
1223e873e4b9SWei Wang 	if (!pcpu_rt) {
1224e873e4b9SWei Wang 		fib6_info_release(rt);
1225d52d3997SMartin KaFai Lau 		return NULL;
1226e873e4b9SWei Wang 	}
1227d52d3997SMartin KaFai Lau 	ip6_rt_copy_init(pcpu_rt, rt);
1228d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
1229d52d3997SMartin KaFai Lau 	return pcpu_rt;
1230d52d3997SMartin KaFai Lau }
1231d52d3997SMartin KaFai Lau 
123266f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */
12338d1c802bSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(struct fib6_info *rt)
1234d52d3997SMartin KaFai Lau {
1235a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, **p;
1236d52d3997SMartin KaFai Lau 
1237d52d3997SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1238d52d3997SMartin KaFai Lau 	pcpu_rt = *p;
1239d52d3997SMartin KaFai Lau 
1240d4ead6b3SDavid Ahern 	if (pcpu_rt)
1241d4ead6b3SDavid Ahern 		ip6_hold_safe(NULL, &pcpu_rt, false);
1242d3843fe5SWei Wang 
1243a73e4195SMartin KaFai Lau 	return pcpu_rt;
1244a73e4195SMartin KaFai Lau }
1245a73e4195SMartin KaFai Lau 
1246afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net,
12478d1c802bSDavid Ahern 					    struct fib6_info *rt)
1248a73e4195SMartin KaFai Lau {
1249a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1250d52d3997SMartin KaFai Lau 
1251d52d3997SMartin KaFai Lau 	pcpu_rt = ip6_rt_pcpu_alloc(rt);
1252d52d3997SMartin KaFai Lau 	if (!pcpu_rt) {
12539c7370a1SMartin KaFai Lau 		dst_hold(&net->ipv6.ip6_null_entry->dst);
12549c7370a1SMartin KaFai Lau 		return net->ipv6.ip6_null_entry;
1255d52d3997SMartin KaFai Lau 	}
1256d52d3997SMartin KaFai Lau 
1257a94b9367SWei Wang 	dst_hold(&pcpu_rt->dst);
1258a73e4195SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1259d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1260951f788aSEric Dumazet 	BUG_ON(prev);
1261a94b9367SWei Wang 
1262d52d3997SMartin KaFai Lau 	return pcpu_rt;
1263d52d3997SMartin KaFai Lau }
1264d52d3997SMartin KaFai Lau 
126535732d01SWei Wang /* exception hash table implementation
126635732d01SWei Wang  */
126735732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
126835732d01SWei Wang 
126935732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
127035732d01SWei Wang  * Caller must hold rt6_exception_lock
127135732d01SWei Wang  */
127235732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
127335732d01SWei Wang 				 struct rt6_exception *rt6_ex)
127435732d01SWei Wang {
1275b2427e67SColin Ian King 	struct net *net;
127681eb8447SWei Wang 
127735732d01SWei Wang 	if (!bucket || !rt6_ex)
127835732d01SWei Wang 		return;
1279b2427e67SColin Ian King 
1280b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
128135732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
128277634cc6SDavid Ahern 	dst_release(&rt6_ex->rt6i->dst);
128335732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
128435732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
128535732d01SWei Wang 	bucket->depth--;
128681eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache--;
128735732d01SWei Wang }
128835732d01SWei Wang 
128935732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
129035732d01SWei Wang  * Caller must hold rt6_exception_lock
129135732d01SWei Wang  */
129235732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
129335732d01SWei Wang {
129435732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
129535732d01SWei Wang 
129635732d01SWei Wang 	if (!bucket)
129735732d01SWei Wang 		return;
129835732d01SWei Wang 
129935732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
130035732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
130135732d01SWei Wang 			oldest = rt6_ex;
130235732d01SWei Wang 	}
130335732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
130435732d01SWei Wang }
130535732d01SWei Wang 
130635732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
130735732d01SWei Wang 			      const struct in6_addr *src)
130835732d01SWei Wang {
130935732d01SWei Wang 	static u32 seed __read_mostly;
131035732d01SWei Wang 	u32 val;
131135732d01SWei Wang 
131235732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
131335732d01SWei Wang 	val = jhash(dst, sizeof(*dst), seed);
131435732d01SWei Wang 
131535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
131635732d01SWei Wang 	if (src)
131735732d01SWei Wang 		val = jhash(src, sizeof(*src), val);
131835732d01SWei Wang #endif
131935732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
132035732d01SWei Wang }
132135732d01SWei Wang 
132235732d01SWei Wang /* Helper function to find the cached rt in the hash table
132335732d01SWei Wang  * and update bucket pointer to point to the bucket for this
132435732d01SWei Wang  * (daddr, saddr) pair
132535732d01SWei Wang  * Caller must hold rt6_exception_lock
132635732d01SWei Wang  */
132735732d01SWei Wang static struct rt6_exception *
132835732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
132935732d01SWei Wang 			      const struct in6_addr *daddr,
133035732d01SWei Wang 			      const struct in6_addr *saddr)
133135732d01SWei Wang {
133235732d01SWei Wang 	struct rt6_exception *rt6_ex;
133335732d01SWei Wang 	u32 hval;
133435732d01SWei Wang 
133535732d01SWei Wang 	if (!(*bucket) || !daddr)
133635732d01SWei Wang 		return NULL;
133735732d01SWei Wang 
133835732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
133935732d01SWei Wang 	*bucket += hval;
134035732d01SWei Wang 
134135732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
134235732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
134335732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
134435732d01SWei Wang 
134535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
134635732d01SWei Wang 		if (matched && saddr)
134735732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
134835732d01SWei Wang #endif
134935732d01SWei Wang 		if (matched)
135035732d01SWei Wang 			return rt6_ex;
135135732d01SWei Wang 	}
135235732d01SWei Wang 	return NULL;
135335732d01SWei Wang }
135435732d01SWei Wang 
135535732d01SWei Wang /* Helper function to find the cached rt in the hash table
135635732d01SWei Wang  * and update bucket pointer to point to the bucket for this
135735732d01SWei Wang  * (daddr, saddr) pair
135835732d01SWei Wang  * Caller must hold rcu_read_lock()
135935732d01SWei Wang  */
136035732d01SWei Wang static struct rt6_exception *
136135732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
136235732d01SWei Wang 			 const struct in6_addr *daddr,
136335732d01SWei Wang 			 const struct in6_addr *saddr)
136435732d01SWei Wang {
136535732d01SWei Wang 	struct rt6_exception *rt6_ex;
136635732d01SWei Wang 	u32 hval;
136735732d01SWei Wang 
136835732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
136935732d01SWei Wang 
137035732d01SWei Wang 	if (!(*bucket) || !daddr)
137135732d01SWei Wang 		return NULL;
137235732d01SWei Wang 
137335732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
137435732d01SWei Wang 	*bucket += hval;
137535732d01SWei Wang 
137635732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
137735732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
137835732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
137935732d01SWei Wang 
138035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
138135732d01SWei Wang 		if (matched && saddr)
138235732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
138335732d01SWei Wang #endif
138435732d01SWei Wang 		if (matched)
138535732d01SWei Wang 			return rt6_ex;
138635732d01SWei Wang 	}
138735732d01SWei Wang 	return NULL;
138835732d01SWei Wang }
138935732d01SWei Wang 
13908d1c802bSDavid Ahern static unsigned int fib6_mtu(const struct fib6_info *rt)
139135732d01SWei Wang {
1392d4ead6b3SDavid Ahern 	unsigned int mtu;
1393d4ead6b3SDavid Ahern 
1394dcd1f572SDavid Ahern 	if (rt->fib6_pmtu) {
1395dcd1f572SDavid Ahern 		mtu = rt->fib6_pmtu;
1396dcd1f572SDavid Ahern 	} else {
1397dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
1398dcd1f572SDavid Ahern 		struct inet6_dev *idev;
1399dcd1f572SDavid Ahern 
1400dcd1f572SDavid Ahern 		rcu_read_lock();
1401dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
1402dcd1f572SDavid Ahern 		mtu = idev->cnf.mtu6;
1403dcd1f572SDavid Ahern 		rcu_read_unlock();
1404dcd1f572SDavid Ahern 	}
1405dcd1f572SDavid Ahern 
1406d4ead6b3SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1407d4ead6b3SDavid Ahern 
1408d4ead6b3SDavid Ahern 	return mtu - lwtunnel_headroom(rt->fib6_nh.nh_lwtstate, mtu);
1409d4ead6b3SDavid Ahern }
1410d4ead6b3SDavid Ahern 
141135732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
14128d1c802bSDavid Ahern 				struct fib6_info *ort)
141335732d01SWei Wang {
14145e670d84SDavid Ahern 	struct net *net = dev_net(nrt->dst.dev);
141535732d01SWei Wang 	struct rt6_exception_bucket *bucket;
141635732d01SWei Wang 	struct in6_addr *src_key = NULL;
141735732d01SWei Wang 	struct rt6_exception *rt6_ex;
141835732d01SWei Wang 	int err = 0;
141935732d01SWei Wang 
142035732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
142135732d01SWei Wang 
142235732d01SWei Wang 	if (ort->exception_bucket_flushed) {
142335732d01SWei Wang 		err = -EINVAL;
142435732d01SWei Wang 		goto out;
142535732d01SWei Wang 	}
142635732d01SWei Wang 
142735732d01SWei Wang 	bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
142835732d01SWei Wang 					lockdep_is_held(&rt6_exception_lock));
142935732d01SWei Wang 	if (!bucket) {
143035732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
143135732d01SWei Wang 				 GFP_ATOMIC);
143235732d01SWei Wang 		if (!bucket) {
143335732d01SWei Wang 			err = -ENOMEM;
143435732d01SWei Wang 			goto out;
143535732d01SWei Wang 		}
143635732d01SWei Wang 		rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
143735732d01SWei Wang 	}
143835732d01SWei Wang 
143935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
144035732d01SWei Wang 	/* rt6i_src.plen != 0 indicates ort is in subtree
144135732d01SWei Wang 	 * and exception table is indexed by a hash of
144235732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
144335732d01SWei Wang 	 * Otherwise, the exception table is indexed by
144435732d01SWei Wang 	 * a hash of only rt6i_dst.
144535732d01SWei Wang 	 */
144693c2fb25SDavid Ahern 	if (ort->fib6_src.plen)
144735732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
144835732d01SWei Wang #endif
1449f5bbe7eeSWei Wang 	/* rt6_mtu_change() might lower mtu on ort.
1450f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
1451f5bbe7eeSWei Wang 	 * is less than ort's mtu value.
1452f5bbe7eeSWei Wang 	 */
1453d4ead6b3SDavid Ahern 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(ort)) {
1454f5bbe7eeSWei Wang 		err = -EINVAL;
1455f5bbe7eeSWei Wang 		goto out;
1456f5bbe7eeSWei Wang 	}
145760006a48SWei Wang 
145835732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
145935732d01SWei Wang 					       src_key);
146035732d01SWei Wang 	if (rt6_ex)
146135732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
146235732d01SWei Wang 
146335732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
146435732d01SWei Wang 	if (!rt6_ex) {
146535732d01SWei Wang 		err = -ENOMEM;
146635732d01SWei Wang 		goto out;
146735732d01SWei Wang 	}
146835732d01SWei Wang 	rt6_ex->rt6i = nrt;
146935732d01SWei Wang 	rt6_ex->stamp = jiffies;
147035732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
147135732d01SWei Wang 	bucket->depth++;
147281eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
147335732d01SWei Wang 
147435732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
147535732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
147635732d01SWei Wang 
147735732d01SWei Wang out:
147835732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
147935732d01SWei Wang 
148035732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1481b886d5f2SPaolo Abeni 	if (!err) {
148293c2fb25SDavid Ahern 		spin_lock_bh(&ort->fib6_table->tb6_lock);
14837aef6859SDavid Ahern 		fib6_update_sernum(net, ort);
148493c2fb25SDavid Ahern 		spin_unlock_bh(&ort->fib6_table->tb6_lock);
1485b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1486b886d5f2SPaolo Abeni 	}
148735732d01SWei Wang 
148835732d01SWei Wang 	return err;
148935732d01SWei Wang }
149035732d01SWei Wang 
14918d1c802bSDavid Ahern void rt6_flush_exceptions(struct fib6_info *rt)
149235732d01SWei Wang {
149335732d01SWei Wang 	struct rt6_exception_bucket *bucket;
149435732d01SWei Wang 	struct rt6_exception *rt6_ex;
149535732d01SWei Wang 	struct hlist_node *tmp;
149635732d01SWei Wang 	int i;
149735732d01SWei Wang 
149835732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
149935732d01SWei Wang 	/* Prevent rt6_insert_exception() to recreate the bucket list */
150035732d01SWei Wang 	rt->exception_bucket_flushed = 1;
150135732d01SWei Wang 
150235732d01SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
150335732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
150435732d01SWei Wang 	if (!bucket)
150535732d01SWei Wang 		goto out;
150635732d01SWei Wang 
150735732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
150835732d01SWei Wang 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
150935732d01SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
151035732d01SWei Wang 		WARN_ON_ONCE(bucket->depth);
151135732d01SWei Wang 		bucket++;
151235732d01SWei Wang 	}
151335732d01SWei Wang 
151435732d01SWei Wang out:
151535732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
151635732d01SWei Wang }
151735732d01SWei Wang 
151835732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
151935732d01SWei Wang  * Caller has to hold rcu_read_lock()
152035732d01SWei Wang  */
15218d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
152235732d01SWei Wang 					   struct in6_addr *daddr,
152335732d01SWei Wang 					   struct in6_addr *saddr)
152435732d01SWei Wang {
152535732d01SWei Wang 	struct rt6_exception_bucket *bucket;
152635732d01SWei Wang 	struct in6_addr *src_key = NULL;
152735732d01SWei Wang 	struct rt6_exception *rt6_ex;
152835732d01SWei Wang 	struct rt6_info *res = NULL;
152935732d01SWei Wang 
153035732d01SWei Wang 	bucket = rcu_dereference(rt->rt6i_exception_bucket);
153135732d01SWei Wang 
153235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
153335732d01SWei Wang 	/* rt6i_src.plen != 0 indicates rt is in subtree
153435732d01SWei Wang 	 * and exception table is indexed by a hash of
153535732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
153635732d01SWei Wang 	 * Otherwise, the exception table is indexed by
153735732d01SWei Wang 	 * a hash of only rt6i_dst.
153835732d01SWei Wang 	 */
153993c2fb25SDavid Ahern 	if (rt->fib6_src.plen)
154035732d01SWei Wang 		src_key = saddr;
154135732d01SWei Wang #endif
154235732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
154335732d01SWei Wang 
154435732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
154535732d01SWei Wang 		res = rt6_ex->rt6i;
154635732d01SWei Wang 
154735732d01SWei Wang 	return res;
154835732d01SWei Wang }
154935732d01SWei Wang 
155035732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
155123fb93a4SDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt)
155235732d01SWei Wang {
155335732d01SWei Wang 	struct rt6_exception_bucket *bucket;
155435732d01SWei Wang 	struct in6_addr *src_key = NULL;
155535732d01SWei Wang 	struct rt6_exception *rt6_ex;
15568a14e46fSDavid Ahern 	struct fib6_info *from;
155735732d01SWei Wang 	int err;
155835732d01SWei Wang 
1559091311deSEric Dumazet 	from = rcu_dereference(rt->from);
156035732d01SWei Wang 	if (!from ||
1561442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
156235732d01SWei Wang 		return -EINVAL;
156335732d01SWei Wang 
156435732d01SWei Wang 	if (!rcu_access_pointer(from->rt6i_exception_bucket))
156535732d01SWei Wang 		return -ENOENT;
156635732d01SWei Wang 
156735732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
156835732d01SWei Wang 	bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
156935732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
157035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
157135732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
157235732d01SWei Wang 	 * and exception table is indexed by a hash of
157335732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
157435732d01SWei Wang 	 * Otherwise, the exception table is indexed by
157535732d01SWei Wang 	 * a hash of only rt6i_dst.
157635732d01SWei Wang 	 */
157793c2fb25SDavid Ahern 	if (from->fib6_src.plen)
157835732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
157935732d01SWei Wang #endif
158035732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
158135732d01SWei Wang 					       &rt->rt6i_dst.addr,
158235732d01SWei Wang 					       src_key);
158335732d01SWei Wang 	if (rt6_ex) {
158435732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
158535732d01SWei Wang 		err = 0;
158635732d01SWei Wang 	} else {
158735732d01SWei Wang 		err = -ENOENT;
158835732d01SWei Wang 	}
158935732d01SWei Wang 
159035732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
159135732d01SWei Wang 	return err;
159235732d01SWei Wang }
159335732d01SWei Wang 
159435732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
159535732d01SWei Wang  * refresh its stamp
159635732d01SWei Wang  */
159735732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
159835732d01SWei Wang {
159935732d01SWei Wang 	struct rt6_exception_bucket *bucket;
16008d1c802bSDavid Ahern 	struct fib6_info *from = rt->from;
160135732d01SWei Wang 	struct in6_addr *src_key = NULL;
160235732d01SWei Wang 	struct rt6_exception *rt6_ex;
160335732d01SWei Wang 
160435732d01SWei Wang 	if (!from ||
1605442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
160635732d01SWei Wang 		return;
160735732d01SWei Wang 
160835732d01SWei Wang 	rcu_read_lock();
160935732d01SWei Wang 	bucket = rcu_dereference(from->rt6i_exception_bucket);
161035732d01SWei Wang 
161135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
161235732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
161335732d01SWei Wang 	 * and exception table is indexed by a hash of
161435732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
161535732d01SWei Wang 	 * Otherwise, the exception table is indexed by
161635732d01SWei Wang 	 * a hash of only rt6i_dst.
161735732d01SWei Wang 	 */
161893c2fb25SDavid Ahern 	if (from->fib6_src.plen)
161935732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
162035732d01SWei Wang #endif
162135732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket,
162235732d01SWei Wang 					  &rt->rt6i_dst.addr,
162335732d01SWei Wang 					  src_key);
162435732d01SWei Wang 	if (rt6_ex)
162535732d01SWei Wang 		rt6_ex->stamp = jiffies;
162635732d01SWei Wang 
162735732d01SWei Wang 	rcu_read_unlock();
162835732d01SWei Wang }
162935732d01SWei Wang 
1630e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1631e9fa1495SStefano Brivio 					 struct rt6_info *rt, int mtu)
1632e9fa1495SStefano Brivio {
1633e9fa1495SStefano Brivio 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1634e9fa1495SStefano Brivio 	 * lowest MTU in the path: always allow updating the route PMTU to
1635e9fa1495SStefano Brivio 	 * reflect PMTU decreases.
1636e9fa1495SStefano Brivio 	 *
1637e9fa1495SStefano Brivio 	 * If the new MTU is higher, and the route PMTU is equal to the local
1638e9fa1495SStefano Brivio 	 * MTU, this means the old MTU is the lowest in the path, so allow
1639e9fa1495SStefano Brivio 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1640e9fa1495SStefano Brivio 	 * handle this.
1641e9fa1495SStefano Brivio 	 */
1642e9fa1495SStefano Brivio 
1643e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) >= mtu)
1644e9fa1495SStefano Brivio 		return true;
1645e9fa1495SStefano Brivio 
1646e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1647e9fa1495SStefano Brivio 		return true;
1648e9fa1495SStefano Brivio 
1649e9fa1495SStefano Brivio 	return false;
1650e9fa1495SStefano Brivio }
1651e9fa1495SStefano Brivio 
1652e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
16538d1c802bSDavid Ahern 				       struct fib6_info *rt, int mtu)
1654f5bbe7eeSWei Wang {
1655f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
1656f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
1657f5bbe7eeSWei Wang 	int i;
1658f5bbe7eeSWei Wang 
1659f5bbe7eeSWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1660f5bbe7eeSWei Wang 					lockdep_is_held(&rt6_exception_lock));
1661f5bbe7eeSWei Wang 
1662e9fa1495SStefano Brivio 	if (!bucket)
1663e9fa1495SStefano Brivio 		return;
1664e9fa1495SStefano Brivio 
1665f5bbe7eeSWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1666f5bbe7eeSWei Wang 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1667f5bbe7eeSWei Wang 			struct rt6_info *entry = rt6_ex->rt6i;
1668e9fa1495SStefano Brivio 
1669e9fa1495SStefano Brivio 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
1670d4ead6b3SDavid Ahern 			 * route), the metrics of its rt->from have already
1671f5bbe7eeSWei Wang 			 * been updated.
1672f5bbe7eeSWei Wang 			 */
1673d4ead6b3SDavid Ahern 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
1674e9fa1495SStefano Brivio 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
1675d4ead6b3SDavid Ahern 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
1676f5bbe7eeSWei Wang 		}
1677f5bbe7eeSWei Wang 		bucket++;
1678f5bbe7eeSWei Wang 	}
1679f5bbe7eeSWei Wang }
1680f5bbe7eeSWei Wang 
1681b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
1682b16cb459SWei Wang 
16838d1c802bSDavid Ahern static void rt6_exceptions_clean_tohost(struct fib6_info *rt,
1684b16cb459SWei Wang 					struct in6_addr *gateway)
1685b16cb459SWei Wang {
1686b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
1687b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
1688b16cb459SWei Wang 	struct hlist_node *tmp;
1689b16cb459SWei Wang 	int i;
1690b16cb459SWei Wang 
1691b16cb459SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1692b16cb459SWei Wang 		return;
1693b16cb459SWei Wang 
1694b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1695b16cb459SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1696b16cb459SWei Wang 				     lockdep_is_held(&rt6_exception_lock));
1697b16cb459SWei Wang 
1698b16cb459SWei Wang 	if (bucket) {
1699b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1700b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1701b16cb459SWei Wang 						  &bucket->chain, hlist) {
1702b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1703b16cb459SWei Wang 
1704b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1705b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
1706b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
1707b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
1708b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
1709b16cb459SWei Wang 				}
1710b16cb459SWei Wang 			}
1711b16cb459SWei Wang 			bucket++;
1712b16cb459SWei Wang 		}
1713b16cb459SWei Wang 	}
1714b16cb459SWei Wang 
1715b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1716b16cb459SWei Wang }
1717b16cb459SWei Wang 
1718c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1719c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
1720c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
1721c757faa8SWei Wang 				      unsigned long now)
1722c757faa8SWei Wang {
1723c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
1724c757faa8SWei Wang 
17251859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
17261859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
17271859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
17281859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
17291859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
17301859bac0SPaolo Abeni 	 */
173131afeb42SWei Wang 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
173231afeb42SWei Wang 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1733c757faa8SWei Wang 			RT6_TRACE("aging clone %p\n", rt);
1734c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1735c757faa8SWei Wang 			return;
173631afeb42SWei Wang 		}
173731afeb42SWei Wang 	} else if (time_after(jiffies, rt->dst.expires)) {
173831afeb42SWei Wang 		RT6_TRACE("purging expired route %p\n", rt);
173931afeb42SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
174031afeb42SWei Wang 		return;
174131afeb42SWei Wang 	}
174231afeb42SWei Wang 
174331afeb42SWei Wang 	if (rt->rt6i_flags & RTF_GATEWAY) {
1744c757faa8SWei Wang 		struct neighbour *neigh;
1745c757faa8SWei Wang 		__u8 neigh_flags = 0;
1746c757faa8SWei Wang 
17471bfa26ffSEric Dumazet 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
17481bfa26ffSEric Dumazet 		if (neigh)
1749c757faa8SWei Wang 			neigh_flags = neigh->flags;
17501bfa26ffSEric Dumazet 
1751c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
1752c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
1753c757faa8SWei Wang 				  rt);
1754c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1755c757faa8SWei Wang 			return;
1756c757faa8SWei Wang 		}
1757c757faa8SWei Wang 	}
175831afeb42SWei Wang 
1759c757faa8SWei Wang 	gc_args->more++;
1760c757faa8SWei Wang }
1761c757faa8SWei Wang 
17628d1c802bSDavid Ahern void rt6_age_exceptions(struct fib6_info *rt,
1763c757faa8SWei Wang 			struct fib6_gc_args *gc_args,
1764c757faa8SWei Wang 			unsigned long now)
1765c757faa8SWei Wang {
1766c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
1767c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
1768c757faa8SWei Wang 	struct hlist_node *tmp;
1769c757faa8SWei Wang 	int i;
1770c757faa8SWei Wang 
1771c757faa8SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1772c757faa8SWei Wang 		return;
1773c757faa8SWei Wang 
17741bfa26ffSEric Dumazet 	rcu_read_lock_bh();
17751bfa26ffSEric Dumazet 	spin_lock(&rt6_exception_lock);
1776c757faa8SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1777c757faa8SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
1778c757faa8SWei Wang 
1779c757faa8SWei Wang 	if (bucket) {
1780c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1781c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1782c757faa8SWei Wang 						  &bucket->chain, hlist) {
1783c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
1784c757faa8SWei Wang 							  gc_args, now);
1785c757faa8SWei Wang 			}
1786c757faa8SWei Wang 			bucket++;
1787c757faa8SWei Wang 		}
1788c757faa8SWei Wang 	}
17891bfa26ffSEric Dumazet 	spin_unlock(&rt6_exception_lock);
17901bfa26ffSEric Dumazet 	rcu_read_unlock_bh();
1791c757faa8SWei Wang }
1792c757faa8SWei Wang 
17931d053da9SDavid Ahern /* must be called with rcu lock held */
17941d053da9SDavid Ahern struct fib6_info *fib6_table_lookup(struct net *net, struct fib6_table *table,
17951d053da9SDavid Ahern 				    int oif, struct flowi6 *fl6, int strict)
17961da177e4SLinus Torvalds {
1797367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
17988d1c802bSDavid Ahern 	struct fib6_info *f6i;
17991da177e4SLinus Torvalds 
18006454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1801367efcb9SMartin KaFai Lau 	saved_fn = fn;
18021da177e4SLinus Torvalds 
1803ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1804ca254490SDavid Ahern 		oif = 0;
1805ca254490SDavid Ahern 
1806a3c00e46SMartin KaFai Lau redo_rt6_select:
180723fb93a4SDavid Ahern 	f6i = rt6_select(net, fn, oif, strict);
180823fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1809a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1810a3c00e46SMartin KaFai Lau 		if (fn)
1811a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
1812367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
1813367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
1814367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
1815367efcb9SMartin KaFai Lau 			fn = saved_fn;
1816367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
1817367efcb9SMartin KaFai Lau 		}
1818a3c00e46SMartin KaFai Lau 	}
1819a3c00e46SMartin KaFai Lau 
1820d4bea421SDavid Ahern 	trace_fib6_table_lookup(net, f6i, table, fl6);
1821d52d3997SMartin KaFai Lau 
18221d053da9SDavid Ahern 	return f6i;
18231d053da9SDavid Ahern }
18241d053da9SDavid Ahern 
18251d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
18261d053da9SDavid Ahern 			       int oif, struct flowi6 *fl6,
18271d053da9SDavid Ahern 			       const struct sk_buff *skb, int flags)
18281d053da9SDavid Ahern {
18291d053da9SDavid Ahern 	struct fib6_info *f6i;
18301d053da9SDavid Ahern 	struct rt6_info *rt;
18311d053da9SDavid Ahern 	int strict = 0;
18321d053da9SDavid Ahern 
18331d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IFACE;
18341d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
18351d053da9SDavid Ahern 	if (net->ipv6.devconf_all->forwarding == 0)
18361d053da9SDavid Ahern 		strict |= RT6_LOOKUP_F_REACHABLE;
18371d053da9SDavid Ahern 
18381d053da9SDavid Ahern 	rcu_read_lock();
18391d053da9SDavid Ahern 
18401d053da9SDavid Ahern 	f6i = fib6_table_lookup(net, table, oif, fl6, strict);
18411d053da9SDavid Ahern 	if (f6i->fib6_nsiblings)
18421d053da9SDavid Ahern 		f6i = fib6_multipath_select(net, f6i, fl6, oif, skb, strict);
18431d053da9SDavid Ahern 
184423fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1845421842edSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
184666f5d6ceSWei Wang 		rcu_read_unlock();
1847d3843fe5SWei Wang 		dst_hold(&rt->dst);
1848d3843fe5SWei Wang 		return rt;
1849d3843fe5SWei Wang 	}
185023fb93a4SDavid Ahern 
185123fb93a4SDavid Ahern 	/*Search through exception table */
185223fb93a4SDavid Ahern 	rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
185323fb93a4SDavid Ahern 	if (rt) {
1854d4ead6b3SDavid Ahern 		if (ip6_hold_safe(net, &rt, true))
18551da177e4SLinus Torvalds 			dst_use_noref(&rt->dst, jiffies);
1856d4ead6b3SDavid Ahern 
185766f5d6ceSWei Wang 		rcu_read_unlock();
1858d52d3997SMartin KaFai Lau 		return rt;
18593da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
186093c2fb25SDavid Ahern 			    !(f6i->fib6_flags & RTF_GATEWAY))) {
18613da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
18623da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
18633da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
18643da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
18653da59bd9SMartin KaFai Lau 		 */
18663da59bd9SMartin KaFai Lau 		struct rt6_info *uncached_rt;
18673da59bd9SMartin KaFai Lau 
186823fb93a4SDavid Ahern 		uncached_rt = ip6_rt_cache_alloc(f6i, &fl6->daddr, NULL);
1869d52d3997SMartin KaFai Lau 
18704d85cd0cSDavid Ahern 		rcu_read_unlock();
18713da59bd9SMartin KaFai Lau 
18721cfb71eeSWei Wang 		if (uncached_rt) {
18731cfb71eeSWei Wang 			/* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
18741cfb71eeSWei Wang 			 * No need for another dst_hold()
18751cfb71eeSWei Wang 			 */
18768d0b94afSMartin KaFai Lau 			rt6_uncached_list_add(uncached_rt);
187781eb8447SWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
18781cfb71eeSWei Wang 		} else {
18793da59bd9SMartin KaFai Lau 			uncached_rt = net->ipv6.ip6_null_entry;
18803da59bd9SMartin KaFai Lau 			dst_hold(&uncached_rt->dst);
18811cfb71eeSWei Wang 		}
1882b811580dSDavid Ahern 
18833da59bd9SMartin KaFai Lau 		return uncached_rt;
1884d52d3997SMartin KaFai Lau 	} else {
1885d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
1886d52d3997SMartin KaFai Lau 
1887d52d3997SMartin KaFai Lau 		struct rt6_info *pcpu_rt;
1888d52d3997SMartin KaFai Lau 
1889951f788aSEric Dumazet 		local_bh_disable();
189023fb93a4SDavid Ahern 		pcpu_rt = rt6_get_pcpu_route(f6i);
1891d52d3997SMartin KaFai Lau 
189293531c67SDavid Ahern 		if (!pcpu_rt)
189323fb93a4SDavid Ahern 			pcpu_rt = rt6_make_pcpu_route(net, f6i);
189493531c67SDavid Ahern 
1895951f788aSEric Dumazet 		local_bh_enable();
1896951f788aSEric Dumazet 		rcu_read_unlock();
1897d4bea421SDavid Ahern 
1898d52d3997SMartin KaFai Lau 		return pcpu_rt;
1899d52d3997SMartin KaFai Lau 	}
1900c71099acSThomas Graf }
19019ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
1902c71099acSThomas Graf 
1903b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net,
1904b75cc8f9SDavid Ahern 					    struct fib6_table *table,
1905b75cc8f9SDavid Ahern 					    struct flowi6 *fl6,
1906b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
1907b75cc8f9SDavid Ahern 					    int flags)
19084acad72dSPavel Emelyanov {
1909b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
19104acad72dSPavel Emelyanov }
19114acad72dSPavel Emelyanov 
1912d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
191372331bc0SShmulik Ladkani 					 struct net_device *dev,
1914b75cc8f9SDavid Ahern 					 struct flowi6 *fl6,
1915b75cc8f9SDavid Ahern 					 const struct sk_buff *skb,
1916b75cc8f9SDavid Ahern 					 int flags)
191772331bc0SShmulik Ladkani {
191872331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
191972331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
192072331bc0SShmulik Ladkani 
1921b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
192272331bc0SShmulik Ladkani }
1923d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
192472331bc0SShmulik Ladkani 
192523aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
19265e5d6fedSRoopa Prabhu 				  struct flow_keys *keys,
19275e5d6fedSRoopa Prabhu 				  struct flow_keys *flkeys)
192823aebdacSJakub Sitnicki {
192923aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
193023aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
19315e5d6fedSRoopa Prabhu 	struct flow_keys *_flkeys = flkeys;
193223aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
193323aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
193423aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
1935cea67a2dSEric Dumazet 	struct icmp6hdr _icmph;
193623aebdacSJakub Sitnicki 
193723aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
193823aebdacSJakub Sitnicki 		goto out;
193923aebdacSJakub Sitnicki 
1940cea67a2dSEric Dumazet 	icmph = skb_header_pointer(skb, skb_transport_offset(skb),
1941cea67a2dSEric Dumazet 				   sizeof(_icmph), &_icmph);
1942cea67a2dSEric Dumazet 	if (!icmph)
1943cea67a2dSEric Dumazet 		goto out;
1944cea67a2dSEric Dumazet 
194523aebdacSJakub Sitnicki 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
194623aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
194723aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
194823aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
194923aebdacSJakub Sitnicki 		goto out;
195023aebdacSJakub Sitnicki 
195123aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
195223aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
195323aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
195423aebdacSJakub Sitnicki 	if (!inner_iph)
195523aebdacSJakub Sitnicki 		goto out;
195623aebdacSJakub Sitnicki 
195723aebdacSJakub Sitnicki 	key_iph = inner_iph;
19585e5d6fedSRoopa Prabhu 	_flkeys = NULL;
195923aebdacSJakub Sitnicki out:
19605e5d6fedSRoopa Prabhu 	if (_flkeys) {
19615e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
19625e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
19635e5d6fedSRoopa Prabhu 		keys->tags.flow_label = _flkeys->tags.flow_label;
19645e5d6fedSRoopa Prabhu 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
19655e5d6fedSRoopa Prabhu 	} else {
196623aebdacSJakub Sitnicki 		keys->addrs.v6addrs.src = key_iph->saddr;
196723aebdacSJakub Sitnicki 		keys->addrs.v6addrs.dst = key_iph->daddr;
1968fa1be7e0SMichal Kubecek 		keys->tags.flow_label = ip6_flowlabel(key_iph);
196923aebdacSJakub Sitnicki 		keys->basic.ip_proto = key_iph->nexthdr;
197023aebdacSJakub Sitnicki 	}
19715e5d6fedSRoopa Prabhu }
197223aebdacSJakub Sitnicki 
197323aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
1974b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
1975b4bac172SDavid Ahern 		       const struct sk_buff *skb, struct flow_keys *flkeys)
197623aebdacSJakub Sitnicki {
197723aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
19789a2a537aSDavid Ahern 	u32 mhash;
197923aebdacSJakub Sitnicki 
1980bbfa047aSDavid S. Miller 	switch (ip6_multipath_hash_policy(net)) {
1981b4bac172SDavid Ahern 	case 0:
19826f74b6c2SDavid Ahern 		memset(&hash_keys, 0, sizeof(hash_keys));
19836f74b6c2SDavid Ahern 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
19849a2a537aSDavid Ahern 		if (skb) {
19855e5d6fedSRoopa Prabhu 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
19869a2a537aSDavid Ahern 		} else {
19879a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
19889a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
1989fa1be7e0SMichal Kubecek 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
19909a2a537aSDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
199123aebdacSJakub Sitnicki 		}
1992b4bac172SDavid Ahern 		break;
1993b4bac172SDavid Ahern 	case 1:
1994b4bac172SDavid Ahern 		if (skb) {
1995b4bac172SDavid Ahern 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
1996b4bac172SDavid Ahern 			struct flow_keys keys;
1997b4bac172SDavid Ahern 
1998b4bac172SDavid Ahern 			/* short-circuit if we already have L4 hash present */
1999b4bac172SDavid Ahern 			if (skb->l4_hash)
2000b4bac172SDavid Ahern 				return skb_get_hash_raw(skb) >> 1;
2001b4bac172SDavid Ahern 
2002b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2003b4bac172SDavid Ahern 
2004b4bac172SDavid Ahern                         if (!flkeys) {
2005b4bac172SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2006b4bac172SDavid Ahern 				flkeys = &keys;
2007b4bac172SDavid Ahern 			}
2008b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2009b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2010b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2011b4bac172SDavid Ahern 			hash_keys.ports.src = flkeys->ports.src;
2012b4bac172SDavid Ahern 			hash_keys.ports.dst = flkeys->ports.dst;
2013b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2014b4bac172SDavid Ahern 		} else {
2015b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2016b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2017b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2018b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2019b4bac172SDavid Ahern 			hash_keys.ports.src = fl6->fl6_sport;
2020b4bac172SDavid Ahern 			hash_keys.ports.dst = fl6->fl6_dport;
2021b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2022b4bac172SDavid Ahern 		}
2023b4bac172SDavid Ahern 		break;
2024b4bac172SDavid Ahern 	}
20259a2a537aSDavid Ahern 	mhash = flow_hash_from_keys(&hash_keys);
202623aebdacSJakub Sitnicki 
20279a2a537aSDavid Ahern 	return mhash >> 1;
202823aebdacSJakub Sitnicki }
202923aebdacSJakub Sitnicki 
2030c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
2031c71099acSThomas Graf {
2032b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2033c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
2034adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2035904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
20364c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
2037e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
20384c9483b2SDavid S. Miller 		.daddr = iph->daddr,
20394c9483b2SDavid S. Miller 		.saddr = iph->saddr,
20406502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
20414c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
20424c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
2043c71099acSThomas Graf 	};
20445e5d6fedSRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2045adaa70bbSThomas Graf 
2046904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
204746fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2048904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
20495e5d6fedSRoopa Prabhu 
20505e5d6fedSRoopa Prabhu 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
20515e5d6fedSRoopa Prabhu 		flkeys = &_flkeys;
20525e5d6fedSRoopa Prabhu 
205323aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2054b4bac172SDavid Ahern 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
205506e9d040SJiri Benc 	skb_dst_drop(skb);
2056b75cc8f9SDavid Ahern 	skb_dst_set(skb,
2057b75cc8f9SDavid Ahern 		    ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
2058c71099acSThomas Graf }
2059c71099acSThomas Graf 
2060b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net,
2061b75cc8f9SDavid Ahern 					     struct fib6_table *table,
2062b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
2063b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2064b75cc8f9SDavid Ahern 					     int flags)
2065c71099acSThomas Graf {
2066b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2067c71099acSThomas Graf }
2068c71099acSThomas Graf 
20696f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
20706f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags)
2071c71099acSThomas Graf {
2072d46a9d67SDavid Ahern 	bool any_src;
2073c71099acSThomas Graf 
20743ede0bbcSRobert Shearman 	if (ipv6_addr_type(&fl6->daddr) &
20753ede0bbcSRobert Shearman 	    (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
20764c1feac5SDavid Ahern 		struct dst_entry *dst;
20774c1feac5SDavid Ahern 
20784c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
2079ca254490SDavid Ahern 		if (dst)
2080ca254490SDavid Ahern 			return dst;
20814c1feac5SDavid Ahern 	}
2082ca254490SDavid Ahern 
20831fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
20844dc27d1cSDavid McCullough 
2085d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
2086741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2087d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
208877d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
2089c71099acSThomas Graf 
2090d46a9d67SDavid Ahern 	if (!any_src)
2091adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
20920c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
20930c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2094adaa70bbSThomas Graf 
2095b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
20961da177e4SLinus Torvalds }
20976f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
20981da177e4SLinus Torvalds 
20992774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
210014e50e57SDavid S. Miller {
21015c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
21021dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
210314e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
210414e50e57SDavid S. Miller 
21051dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
210662cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
210714e50e57SDavid S. Miller 	if (rt) {
21080a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
210981eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
21100a1f5962SMartin KaFai Lau 
2111d8d1f30bSChangli Gao 		new = &rt->dst;
211214e50e57SDavid S. Miller 		new->__use = 1;
2113352e512cSHerbert Xu 		new->input = dst_discard;
2114ede2059dSEric W. Biederman 		new->output = dst_discard_out;
211514e50e57SDavid S. Miller 
2116defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
211714e50e57SDavid S. Miller 
21181dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
21194e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
21200a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
212114e50e57SDavid S. Miller 
212214e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
212314e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
212414e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
212514e50e57SDavid S. Miller #endif
212614e50e57SDavid S. Miller 	}
212714e50e57SDavid S. Miller 
212869ead7afSDavid S. Miller 	dst_release(dst_orig);
212969ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
213014e50e57SDavid S. Miller }
213114e50e57SDavid S. Miller 
21321da177e4SLinus Torvalds /*
21331da177e4SLinus Torvalds  *	Destination cache support functions
21341da177e4SLinus Torvalds  */
21351da177e4SLinus Torvalds 
21368d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie)
21373da59bd9SMartin KaFai Lau {
213836143645SSteffen Klassert 	u32 rt_cookie = 0;
2139c5cff856SWei Wang 
21408ae86971SDavid Ahern 	if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
214193531c67SDavid Ahern 		return false;
214293531c67SDavid Ahern 
214393531c67SDavid Ahern 	if (fib6_check_expired(f6i))
214493531c67SDavid Ahern 		return false;
214593531c67SDavid Ahern 
214693531c67SDavid Ahern 	return true;
214793531c67SDavid Ahern }
214893531c67SDavid Ahern 
2149a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt,
2150a68886a6SDavid Ahern 				   struct fib6_info *from,
2151a68886a6SDavid Ahern 				   u32 cookie)
21523da59bd9SMartin KaFai Lau {
2153c5cff856SWei Wang 	u32 rt_cookie = 0;
2154c5cff856SWei Wang 
2155a68886a6SDavid Ahern 	if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
215693531c67SDavid Ahern 	    rt_cookie != cookie)
21573da59bd9SMartin KaFai Lau 		return NULL;
21583da59bd9SMartin KaFai Lau 
21593da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
21603da59bd9SMartin KaFai Lau 		return NULL;
21613da59bd9SMartin KaFai Lau 
21623da59bd9SMartin KaFai Lau 	return &rt->dst;
21633da59bd9SMartin KaFai Lau }
21643da59bd9SMartin KaFai Lau 
2165a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2166a68886a6SDavid Ahern 					    struct fib6_info *from,
2167a68886a6SDavid Ahern 					    u32 cookie)
21683da59bd9SMartin KaFai Lau {
21695973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
21705973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2171a68886a6SDavid Ahern 	    fib6_check(from, cookie))
21723da59bd9SMartin KaFai Lau 		return &rt->dst;
21733da59bd9SMartin KaFai Lau 	else
21743da59bd9SMartin KaFai Lau 		return NULL;
21753da59bd9SMartin KaFai Lau }
21763da59bd9SMartin KaFai Lau 
21771da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
21781da177e4SLinus Torvalds {
2179a87b7dc9SDavid Ahern 	struct dst_entry *dst_ret;
2180a68886a6SDavid Ahern 	struct fib6_info *from;
21811da177e4SLinus Torvalds 	struct rt6_info *rt;
21821da177e4SLinus Torvalds 
2183a87b7dc9SDavid Ahern 	rt = container_of(dst, struct rt6_info, dst);
2184a87b7dc9SDavid Ahern 
2185a87b7dc9SDavid Ahern 	rcu_read_lock();
21861da177e4SLinus Torvalds 
21876f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
21886f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
21896f3118b5SNicolas Dichtel 	 * into this function always.
21906f3118b5SNicolas Dichtel 	 */
2191e3bc10bdSHannes Frederic Sowa 
2192a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
21934b32b5adSMartin KaFai Lau 
2194a68886a6SDavid Ahern 	if (from && (rt->rt6i_flags & RTF_PCPU ||
2195a68886a6SDavid Ahern 	    unlikely(!list_empty(&rt->rt6i_uncached))))
2196a68886a6SDavid Ahern 		dst_ret = rt6_dst_from_check(rt, from, cookie);
21973da59bd9SMartin KaFai Lau 	else
2198a68886a6SDavid Ahern 		dst_ret = rt6_check(rt, from, cookie);
2199a87b7dc9SDavid Ahern 
2200a87b7dc9SDavid Ahern 	rcu_read_unlock();
2201a87b7dc9SDavid Ahern 
2202a87b7dc9SDavid Ahern 	return dst_ret;
22031da177e4SLinus Torvalds }
22041da177e4SLinus Torvalds 
22051da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
22061da177e4SLinus Torvalds {
22071da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 	if (rt) {
221054c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
2211c3c14da0SDavid Ahern 			rcu_read_lock();
221254c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
221393531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
221454c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
22151da177e4SLinus Torvalds 			}
2216c3c14da0SDavid Ahern 			rcu_read_unlock();
221754c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
221854c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
221954c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
222054c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
222154c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
222254c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
22231da177e4SLinus Torvalds }
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
22261da177e4SLinus Torvalds {
22271da177e4SLinus Torvalds 	struct rt6_info *rt;
22281da177e4SLinus Torvalds 
22293ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
22301da177e4SLinus Torvalds 
2231adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
22321da177e4SLinus Torvalds 	if (rt) {
22338a14e46fSDavid Ahern 		rcu_read_lock();
22341eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
2235ad65a2f0SWei Wang 			if (dst_hold_safe(&rt->dst))
223693531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
2237c5cff856SWei Wang 		} else {
2238a68886a6SDavid Ahern 			struct fib6_info *from;
2239c5cff856SWei Wang 			struct fib6_node *fn;
2240c5cff856SWei Wang 
2241a68886a6SDavid Ahern 			from = rcu_dereference(rt->from);
2242a68886a6SDavid Ahern 			if (from) {
2243a68886a6SDavid Ahern 				fn = rcu_dereference(from->fib6_node);
2244c5cff856SWei Wang 				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2245c5cff856SWei Wang 					fn->fn_sernum = -1;
2246a68886a6SDavid Ahern 			}
22471da177e4SLinus Torvalds 		}
22481da177e4SLinus Torvalds 		rcu_read_unlock();
22491da177e4SLinus Torvalds 	}
22501da177e4SLinus Torvalds }
22511da177e4SLinus Torvalds 
22526a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout)
22536a3e030fSDavid Ahern {
2254a68886a6SDavid Ahern 	if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2255a68886a6SDavid Ahern 		struct fib6_info *from;
2256a68886a6SDavid Ahern 
2257a68886a6SDavid Ahern 		rcu_read_lock();
2258a68886a6SDavid Ahern 		from = rcu_dereference(rt0->from);
2259a68886a6SDavid Ahern 		if (from)
2260a68886a6SDavid Ahern 			rt0->dst.expires = from->expires;
2261a68886a6SDavid Ahern 		rcu_read_unlock();
2262a68886a6SDavid Ahern 	}
22636a3e030fSDavid Ahern 
22646a3e030fSDavid Ahern 	dst_set_expires(&rt0->dst, timeout);
22656a3e030fSDavid Ahern 	rt0->rt6i_flags |= RTF_EXPIRES;
22666700c270SDavid S. Miller }
22671da177e4SLinus Torvalds 
226845e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
226945e4fd26SMartin KaFai Lau {
227045e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
227145e4fd26SMartin KaFai Lau 
2272d4ead6b3SDavid Ahern 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
227345e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
227445e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
227545e4fd26SMartin KaFai Lau }
227645e4fd26SMartin KaFai Lau 
22770d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
22780d3f6d29SMartin KaFai Lau {
2279a68886a6SDavid Ahern 	bool from_set;
2280a68886a6SDavid Ahern 
2281a68886a6SDavid Ahern 	rcu_read_lock();
2282a68886a6SDavid Ahern 	from_set = !!rcu_dereference(rt->from);
2283a68886a6SDavid Ahern 	rcu_read_unlock();
2284a68886a6SDavid Ahern 
22850d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
2286a68886a6SDavid Ahern 		(rt->rt6i_flags & RTF_PCPU || from_set);
22870d3f6d29SMartin KaFai Lau }
22880d3f6d29SMartin KaFai Lau 
228945e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
229045e4fd26SMartin KaFai Lau 				 const struct ipv6hdr *iph, u32 mtu)
22911da177e4SLinus Torvalds {
22920dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
22931da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
22941da177e4SLinus Torvalds 
229519bda36cSXin Long 	if (dst_metric_locked(dst, RTAX_MTU))
229619bda36cSXin Long 		return;
229719bda36cSXin Long 
229845e4fd26SMartin KaFai Lau 	if (iph) {
229945e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
230045e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
230145e4fd26SMartin KaFai Lau 	} else if (sk) {
230245e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
230345e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
230445e4fd26SMartin KaFai Lau 	} else {
23050dec879fSJulian Anastasov 		daddr = NULL;
23060dec879fSJulian Anastasov 		saddr = NULL;
23071da177e4SLinus Torvalds 	}
23080dec879fSJulian Anastasov 	dst_confirm_neigh(dst, daddr);
23090dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
23100dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
23110dec879fSJulian Anastasov 		return;
23120dec879fSJulian Anastasov 
23130dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
23140dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
23152b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
23162b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
23172b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
23180dec879fSJulian Anastasov 	} else if (daddr) {
2319a68886a6SDavid Ahern 		struct fib6_info *from;
23200dec879fSJulian Anastasov 		struct rt6_info *nrt6;
23210dec879fSJulian Anastasov 
23224d85cd0cSDavid Ahern 		rcu_read_lock();
2323a68886a6SDavid Ahern 		from = rcu_dereference(rt6->from);
2324a68886a6SDavid Ahern 		nrt6 = ip6_rt_cache_alloc(from, daddr, saddr);
232545e4fd26SMartin KaFai Lau 		if (nrt6) {
232645e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
2327a68886a6SDavid Ahern 			if (rt6_insert_exception(nrt6, from))
23282b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
232945e4fd26SMartin KaFai Lau 		}
2330a68886a6SDavid Ahern 		rcu_read_unlock();
233145e4fd26SMartin KaFai Lau 	}
233245e4fd26SMartin KaFai Lau }
233345e4fd26SMartin KaFai Lau 
233445e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
233545e4fd26SMartin KaFai Lau 			       struct sk_buff *skb, u32 mtu)
233645e4fd26SMartin KaFai Lau {
233745e4fd26SMartin KaFai Lau 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
23381da177e4SLinus Torvalds }
23391da177e4SLinus Torvalds 
234042ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2341e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
234281aded24SDavid S. Miller {
234381aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
234481aded24SDavid S. Miller 	struct dst_entry *dst;
2345dc92095dSMaciej Żenczykowski 	struct flowi6 fl6 = {
2346dc92095dSMaciej Żenczykowski 		.flowi6_oif = oif,
2347dc92095dSMaciej Żenczykowski 		.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2348dc92095dSMaciej Żenczykowski 		.daddr = iph->daddr,
2349dc92095dSMaciej Żenczykowski 		.saddr = iph->saddr,
2350dc92095dSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
2351dc92095dSMaciej Żenczykowski 		.flowi6_uid = uid,
2352dc92095dSMaciej Żenczykowski 	};
235381aded24SDavid S. Miller 
235481aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
235581aded24SDavid S. Miller 	if (!dst->error)
235645e4fd26SMartin KaFai Lau 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
235781aded24SDavid S. Miller 	dst_release(dst);
235881aded24SDavid S. Miller }
235981aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
236081aded24SDavid S. Miller 
236181aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
236281aded24SDavid S. Miller {
236333c162a9SMartin KaFai Lau 	struct dst_entry *dst;
236433c162a9SMartin KaFai Lau 
236581aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
2366e2d118a1SLorenzo Colitti 			sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid);
236733c162a9SMartin KaFai Lau 
236833c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
236933c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
237033c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
237133c162a9SMartin KaFai Lau 		return;
237233c162a9SMartin KaFai Lau 
237333c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
237433c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
237533c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
237633c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
237781aded24SDavid S. Miller }
237881aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
237981aded24SDavid S. Miller 
23807d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
23817d6850f7SAlexey Kodanev 			   const struct flowi6 *fl6)
23827d6850f7SAlexey Kodanev {
23837d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
23847d6850f7SAlexey Kodanev 	struct ipv6_pinfo *np = inet6_sk(sk);
23857d6850f7SAlexey Kodanev #endif
23867d6850f7SAlexey Kodanev 
23877d6850f7SAlexey Kodanev 	ip6_dst_store(sk, dst,
23887d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
23897d6850f7SAlexey Kodanev 		      &sk->sk_v6_daddr : NULL,
23907d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
23917d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
23927d6850f7SAlexey Kodanev 		      &np->saddr :
23937d6850f7SAlexey Kodanev #endif
23947d6850f7SAlexey Kodanev 		      NULL);
23957d6850f7SAlexey Kodanev }
23967d6850f7SAlexey Kodanev 
2397b55b76b2SDuan Jiong /* Handle redirects */
2398b55b76b2SDuan Jiong struct ip6rd_flowi {
2399b55b76b2SDuan Jiong 	struct flowi6 fl6;
2400b55b76b2SDuan Jiong 	struct in6_addr gateway;
2401b55b76b2SDuan Jiong };
2402b55b76b2SDuan Jiong 
2403b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
2404b55b76b2SDuan Jiong 					     struct fib6_table *table,
2405b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2406b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2407b55b76b2SDuan Jiong 					     int flags)
2408b55b76b2SDuan Jiong {
2409b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
241023fb93a4SDavid Ahern 	struct rt6_info *ret = NULL, *rt_cache;
24118d1c802bSDavid Ahern 	struct fib6_info *rt;
2412b55b76b2SDuan Jiong 	struct fib6_node *fn;
2413b55b76b2SDuan Jiong 
2414b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
241567c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2416b55b76b2SDuan Jiong 	 *
2417b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2418b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2419b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2420b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2421b55b76b2SDuan Jiong 	 * routes.
2422b55b76b2SDuan Jiong 	 */
2423b55b76b2SDuan Jiong 
242466f5d6ceSWei Wang 	rcu_read_lock();
24256454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2426b55b76b2SDuan Jiong restart:
242766f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
24285e670d84SDavid Ahern 		if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
24298067bb8cSIdo Schimmel 			continue;
243014895687SDavid Ahern 		if (fib6_check_expired(rt))
2431b55b76b2SDuan Jiong 			continue;
243293c2fb25SDavid Ahern 		if (rt->fib6_flags & RTF_REJECT)
2433b55b76b2SDuan Jiong 			break;
243493c2fb25SDavid Ahern 		if (!(rt->fib6_flags & RTF_GATEWAY))
2435b55b76b2SDuan Jiong 			continue;
24365e670d84SDavid Ahern 		if (fl6->flowi6_oif != rt->fib6_nh.nh_dev->ifindex)
2437b55b76b2SDuan Jiong 			continue;
24382b760fcfSWei Wang 		/* rt_cache's gateway might be different from its 'parent'
24392b760fcfSWei Wang 		 * in the case of an ip redirect.
24402b760fcfSWei Wang 		 * So we keep searching in the exception table if the gateway
24412b760fcfSWei Wang 		 * is different.
24422b760fcfSWei Wang 		 */
24435e670d84SDavid Ahern 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.nh_gw)) {
24442b760fcfSWei Wang 			rt_cache = rt6_find_cached_rt(rt,
24452b760fcfSWei Wang 						      &fl6->daddr,
24462b760fcfSWei Wang 						      &fl6->saddr);
24472b760fcfSWei Wang 			if (rt_cache &&
24482b760fcfSWei Wang 			    ipv6_addr_equal(&rdfl->gateway,
24492b760fcfSWei Wang 					    &rt_cache->rt6i_gateway)) {
245023fb93a4SDavid Ahern 				ret = rt_cache;
24512b760fcfSWei Wang 				break;
24522b760fcfSWei Wang 			}
2453b55b76b2SDuan Jiong 			continue;
24542b760fcfSWei Wang 		}
2455b55b76b2SDuan Jiong 		break;
2456b55b76b2SDuan Jiong 	}
2457b55b76b2SDuan Jiong 
2458b55b76b2SDuan Jiong 	if (!rt)
2459421842edSDavid Ahern 		rt = net->ipv6.fib6_null_entry;
246093c2fb25SDavid Ahern 	else if (rt->fib6_flags & RTF_REJECT) {
246123fb93a4SDavid Ahern 		ret = net->ipv6.ip6_null_entry;
2462b0a1ba59SMartin KaFai Lau 		goto out;
2463b0a1ba59SMartin KaFai Lau 	}
2464b0a1ba59SMartin KaFai Lau 
2465421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
2466a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2467a3c00e46SMartin KaFai Lau 		if (fn)
2468a3c00e46SMartin KaFai Lau 			goto restart;
2469b55b76b2SDuan Jiong 	}
2470a3c00e46SMartin KaFai Lau 
2471b0a1ba59SMartin KaFai Lau out:
247223fb93a4SDavid Ahern 	if (ret)
2473e873e4b9SWei Wang 		ip6_hold_safe(net, &ret, true);
247423fb93a4SDavid Ahern 	else
247523fb93a4SDavid Ahern 		ret = ip6_create_rt_rcu(rt);
2476b55b76b2SDuan Jiong 
247766f5d6ceSWei Wang 	rcu_read_unlock();
2478b55b76b2SDuan Jiong 
2479b65f164dSPaolo Abeni 	trace_fib6_table_lookup(net, rt, table, fl6);
248023fb93a4SDavid Ahern 	return ret;
2481b55b76b2SDuan Jiong };
2482b55b76b2SDuan Jiong 
2483b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
2484b55b76b2SDuan Jiong 					    const struct flowi6 *fl6,
2485b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2486b55b76b2SDuan Jiong 					    const struct in6_addr *gateway)
2487b55b76b2SDuan Jiong {
2488b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2489b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
2490b55b76b2SDuan Jiong 
2491b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
2492b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
2493b55b76b2SDuan Jiong 
2494b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, &rdfl.fl6, skb,
2495b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
2496b55b76b2SDuan Jiong }
2497b55b76b2SDuan Jiong 
2498e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2499e2d118a1SLorenzo Colitti 		  kuid_t uid)
25003a5ad2eeSDavid S. Miller {
25013a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
25023a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
25031f7f10acSMaciej Żenczykowski 	struct flowi6 fl6 = {
25041f7f10acSMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
25051f7f10acSMaciej Żenczykowski 		.flowi6_oif = oif,
25061f7f10acSMaciej Żenczykowski 		.flowi6_mark = mark,
25071f7f10acSMaciej Żenczykowski 		.daddr = iph->daddr,
25081f7f10acSMaciej Żenczykowski 		.saddr = iph->saddr,
25091f7f10acSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
25101f7f10acSMaciej Żenczykowski 		.flowi6_uid = uid,
25111f7f10acSMaciej Żenczykowski 	};
25123a5ad2eeSDavid S. Miller 
2513b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
25146700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
25153a5ad2eeSDavid S. Miller 	dst_release(dst);
25163a5ad2eeSDavid S. Miller }
25173a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
25183a5ad2eeSDavid S. Miller 
2519d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
2520c92a59ecSDuan Jiong {
2521c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2522c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2523c92a59ecSDuan Jiong 	struct dst_entry *dst;
25240b26fb17SMaciej Żenczykowski 	struct flowi6 fl6 = {
25250b26fb17SMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
25260b26fb17SMaciej Żenczykowski 		.flowi6_oif = oif,
25270b26fb17SMaciej Żenczykowski 		.daddr = msg->dest,
25280b26fb17SMaciej Żenczykowski 		.saddr = iph->daddr,
25290b26fb17SMaciej Żenczykowski 		.flowi6_uid = sock_net_uid(net, NULL),
25300b26fb17SMaciej Żenczykowski 	};
2531c92a59ecSDuan Jiong 
2532b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
2533c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
2534c92a59ecSDuan Jiong 	dst_release(dst);
2535c92a59ecSDuan Jiong }
2536c92a59ecSDuan Jiong 
25373a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
25383a5ad2eeSDavid S. Miller {
2539e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2540e2d118a1SLorenzo Colitti 		     sk->sk_uid);
25413a5ad2eeSDavid S. Miller }
25423a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
25433a5ad2eeSDavid S. Miller 
25440dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
25451da177e4SLinus Torvalds {
25460dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
25470dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
25480dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
25490dbaee3bSDavid S. Miller 
25501da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
25511da177e4SLinus Torvalds 
25525578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
25535578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds 	/*
25561da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
25571da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
25581da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
25591da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
25601da177e4SLinus Torvalds 	 */
25611da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
25621da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
25631da177e4SLinus Torvalds 	return mtu;
25641da177e4SLinus Torvalds }
25651da177e4SLinus Torvalds 
2566ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
2567d33e4553SDavid S. Miller {
2568d33e4553SDavid S. Miller 	struct inet6_dev *idev;
2569d4ead6b3SDavid Ahern 	unsigned int mtu;
2570618f9bc7SSteffen Klassert 
25714b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
25724b32b5adSMartin KaFai Lau 	if (mtu)
25734b32b5adSMartin KaFai Lau 		goto out;
25744b32b5adSMartin KaFai Lau 
2575618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
2576d33e4553SDavid S. Miller 
2577d33e4553SDavid S. Miller 	rcu_read_lock();
2578d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
2579d33e4553SDavid S. Miller 	if (idev)
2580d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
2581d33e4553SDavid S. Miller 	rcu_read_unlock();
2582d33e4553SDavid S. Miller 
258330f78d8eSEric Dumazet out:
258414972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
258514972cbdSRoopa Prabhu 
258614972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
2587d33e4553SDavid S. Miller }
2588d33e4553SDavid S. Miller 
2589901731b8SDavid Ahern /* MTU selection:
2590901731b8SDavid Ahern  * 1. mtu on route is locked - use it
2591901731b8SDavid Ahern  * 2. mtu from nexthop exception
2592901731b8SDavid Ahern  * 3. mtu from egress device
2593901731b8SDavid Ahern  *
2594901731b8SDavid Ahern  * based on ip6_dst_mtu_forward and exception logic of
2595901731b8SDavid Ahern  * rt6_find_cached_rt; called with rcu_read_lock
2596901731b8SDavid Ahern  */
2597901731b8SDavid Ahern u32 ip6_mtu_from_fib6(struct fib6_info *f6i, struct in6_addr *daddr,
2598901731b8SDavid Ahern 		      struct in6_addr *saddr)
2599901731b8SDavid Ahern {
2600901731b8SDavid Ahern 	struct rt6_exception_bucket *bucket;
2601901731b8SDavid Ahern 	struct rt6_exception *rt6_ex;
2602901731b8SDavid Ahern 	struct in6_addr *src_key;
2603901731b8SDavid Ahern 	struct inet6_dev *idev;
2604901731b8SDavid Ahern 	u32 mtu = 0;
2605901731b8SDavid Ahern 
2606901731b8SDavid Ahern 	if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
2607901731b8SDavid Ahern 		mtu = f6i->fib6_pmtu;
2608901731b8SDavid Ahern 		if (mtu)
2609901731b8SDavid Ahern 			goto out;
2610901731b8SDavid Ahern 	}
2611901731b8SDavid Ahern 
2612901731b8SDavid Ahern 	src_key = NULL;
2613901731b8SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
2614901731b8SDavid Ahern 	if (f6i->fib6_src.plen)
2615901731b8SDavid Ahern 		src_key = saddr;
2616901731b8SDavid Ahern #endif
2617901731b8SDavid Ahern 
2618901731b8SDavid Ahern 	bucket = rcu_dereference(f6i->rt6i_exception_bucket);
2619901731b8SDavid Ahern 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
2620901731b8SDavid Ahern 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
2621901731b8SDavid Ahern 		mtu = dst_metric_raw(&rt6_ex->rt6i->dst, RTAX_MTU);
2622901731b8SDavid Ahern 
2623901731b8SDavid Ahern 	if (likely(!mtu)) {
2624901731b8SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(f6i);
2625901731b8SDavid Ahern 
2626901731b8SDavid Ahern 		mtu = IPV6_MIN_MTU;
2627901731b8SDavid Ahern 		idev = __in6_dev_get(dev);
2628901731b8SDavid Ahern 		if (idev && idev->cnf.mtu6 > mtu)
2629901731b8SDavid Ahern 			mtu = idev->cnf.mtu6;
2630901731b8SDavid Ahern 	}
2631901731b8SDavid Ahern 
2632901731b8SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2633901731b8SDavid Ahern out:
2634901731b8SDavid Ahern 	return mtu - lwtunnel_headroom(fib6_info_nh_lwt(f6i), mtu);
2635901731b8SDavid Ahern }
2636901731b8SDavid Ahern 
26373b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
263887a11578SDavid S. Miller 				  struct flowi6 *fl6)
26391da177e4SLinus Torvalds {
264087a11578SDavid S. Miller 	struct dst_entry *dst;
26411da177e4SLinus Torvalds 	struct rt6_info *rt;
26421da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
2643c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26441da177e4SLinus Torvalds 
264538308473SDavid S. Miller 	if (unlikely(!idev))
2646122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
26471da177e4SLinus Torvalds 
2648ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
264938308473SDavid S. Miller 	if (unlikely(!rt)) {
26501da177e4SLinus Torvalds 		in6_dev_put(idev);
265187a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
26521da177e4SLinus Torvalds 		goto out;
26531da177e4SLinus Torvalds 	}
26541da177e4SLinus Torvalds 
26558e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
2656588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
26578e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
2658550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
265987a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
26608e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
26618e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
266214edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
26631da177e4SLinus Torvalds 
26644c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
2665587fea74SWei Wang 	 * do proper release of the net_device
2666587fea74SWei Wang 	 */
2667587fea74SWei Wang 	rt6_uncached_list_add(rt);
266881eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
26691da177e4SLinus Torvalds 
267087a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
267187a11578SDavid S. Miller 
26721da177e4SLinus Torvalds out:
267387a11578SDavid S. Miller 	return dst;
26741da177e4SLinus Torvalds }
26751da177e4SLinus Torvalds 
2676569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
26771da177e4SLinus Torvalds {
267886393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
26797019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
26807019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
26817019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
26827019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
26837019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
2684fc66f95cSEric Dumazet 	int entries;
26851da177e4SLinus Torvalds 
2686fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
268749a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
2688fc66f95cSEric Dumazet 	    entries <= rt_max_size)
26891da177e4SLinus Torvalds 		goto out;
26901da177e4SLinus Torvalds 
26916891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
269214956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
2693fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
2694fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
26957019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
26961da177e4SLinus Torvalds out:
26977019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
2698fc66f95cSEric Dumazet 	return entries > rt_max_size;
26991da177e4SLinus Torvalds }
27001da177e4SLinus Torvalds 
27018c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net,
27028c14586fSDavid Ahern 					    struct fib6_config *cfg,
2703f4797b33SDavid Ahern 					    const struct in6_addr *gw_addr,
2704f4797b33SDavid Ahern 					    u32 tbid, int flags)
27058c14586fSDavid Ahern {
27068c14586fSDavid Ahern 	struct flowi6 fl6 = {
27078c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
27088c14586fSDavid Ahern 		.daddr = *gw_addr,
27098c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
27108c14586fSDavid Ahern 	};
27118c14586fSDavid Ahern 	struct fib6_table *table;
27128c14586fSDavid Ahern 	struct rt6_info *rt;
27138c14586fSDavid Ahern 
2714f4797b33SDavid Ahern 	table = fib6_get_table(net, tbid);
27158c14586fSDavid Ahern 	if (!table)
27168c14586fSDavid Ahern 		return NULL;
27178c14586fSDavid Ahern 
27188c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
27198c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
27208c14586fSDavid Ahern 
2721f4797b33SDavid Ahern 	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
2722b75cc8f9SDavid Ahern 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
27238c14586fSDavid Ahern 
27248c14586fSDavid Ahern 	/* if table lookup failed, fall back to full lookup */
27258c14586fSDavid Ahern 	if (rt == net->ipv6.ip6_null_entry) {
27268c14586fSDavid Ahern 		ip6_rt_put(rt);
27278c14586fSDavid Ahern 		rt = NULL;
27288c14586fSDavid Ahern 	}
27298c14586fSDavid Ahern 
27308c14586fSDavid Ahern 	return rt;
27318c14586fSDavid Ahern }
27328c14586fSDavid Ahern 
2733fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net,
2734fc1e64e1SDavid Ahern 				     struct fib6_config *cfg,
27359fbb704cSDavid Ahern 				     const struct net_device *dev,
2736fc1e64e1SDavid Ahern 				     struct netlink_ext_ack *extack)
2737fc1e64e1SDavid Ahern {
273844750f84SDavid Ahern 	u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
2739fc1e64e1SDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
2740fc1e64e1SDavid Ahern 	u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2741fc1e64e1SDavid Ahern 	struct rt6_info *grt;
2742fc1e64e1SDavid Ahern 	int err;
2743fc1e64e1SDavid Ahern 
2744fc1e64e1SDavid Ahern 	err = 0;
2745fc1e64e1SDavid Ahern 	grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2746fc1e64e1SDavid Ahern 	if (grt) {
274758e354c0SDavid Ahern 		if (!grt->dst.error &&
274858e354c0SDavid Ahern 		    (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
274944750f84SDavid Ahern 			NL_SET_ERR_MSG(extack,
275044750f84SDavid Ahern 				       "Nexthop has invalid gateway or device mismatch");
2751fc1e64e1SDavid Ahern 			err = -EINVAL;
2752fc1e64e1SDavid Ahern 		}
2753fc1e64e1SDavid Ahern 
2754fc1e64e1SDavid Ahern 		ip6_rt_put(grt);
2755fc1e64e1SDavid Ahern 	}
2756fc1e64e1SDavid Ahern 
2757fc1e64e1SDavid Ahern 	return err;
2758fc1e64e1SDavid Ahern }
2759fc1e64e1SDavid Ahern 
27601edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net,
27611edce99fSDavid Ahern 			      struct fib6_config *cfg,
27621edce99fSDavid Ahern 			      struct net_device **_dev,
27631edce99fSDavid Ahern 			      struct inet6_dev **idev)
27641edce99fSDavid Ahern {
27651edce99fSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
27661edce99fSDavid Ahern 	struct net_device *dev = _dev ? *_dev : NULL;
27671edce99fSDavid Ahern 	struct rt6_info *grt = NULL;
27681edce99fSDavid Ahern 	int err = -EHOSTUNREACH;
27691edce99fSDavid Ahern 
27701edce99fSDavid Ahern 	if (cfg->fc_table) {
2771f4797b33SDavid Ahern 		int flags = RT6_LOOKUP_F_IFACE;
2772f4797b33SDavid Ahern 
2773f4797b33SDavid Ahern 		grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2774f4797b33SDavid Ahern 					  cfg->fc_table, flags);
27751edce99fSDavid Ahern 		if (grt) {
27761edce99fSDavid Ahern 			if (grt->rt6i_flags & RTF_GATEWAY ||
27771edce99fSDavid Ahern 			    (dev && dev != grt->dst.dev)) {
27781edce99fSDavid Ahern 				ip6_rt_put(grt);
27791edce99fSDavid Ahern 				grt = NULL;
27801edce99fSDavid Ahern 			}
27811edce99fSDavid Ahern 		}
27821edce99fSDavid Ahern 	}
27831edce99fSDavid Ahern 
27841edce99fSDavid Ahern 	if (!grt)
2785b75cc8f9SDavid Ahern 		grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
27861edce99fSDavid Ahern 
27871edce99fSDavid Ahern 	if (!grt)
27881edce99fSDavid Ahern 		goto out;
27891edce99fSDavid Ahern 
27901edce99fSDavid Ahern 	if (dev) {
27911edce99fSDavid Ahern 		if (dev != grt->dst.dev) {
27921edce99fSDavid Ahern 			ip6_rt_put(grt);
27931edce99fSDavid Ahern 			goto out;
27941edce99fSDavid Ahern 		}
27951edce99fSDavid Ahern 	} else {
27961edce99fSDavid Ahern 		*_dev = dev = grt->dst.dev;
27971edce99fSDavid Ahern 		*idev = grt->rt6i_idev;
27981edce99fSDavid Ahern 		dev_hold(dev);
27991edce99fSDavid Ahern 		in6_dev_hold(grt->rt6i_idev);
28001edce99fSDavid Ahern 	}
28011edce99fSDavid Ahern 
28021edce99fSDavid Ahern 	if (!(grt->rt6i_flags & RTF_GATEWAY))
28031edce99fSDavid Ahern 		err = 0;
28041edce99fSDavid Ahern 
28051edce99fSDavid Ahern 	ip6_rt_put(grt);
28061edce99fSDavid Ahern 
28071edce99fSDavid Ahern out:
28081edce99fSDavid Ahern 	return err;
28091edce99fSDavid Ahern }
28101edce99fSDavid Ahern 
28119fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
28129fbb704cSDavid Ahern 			   struct net_device **_dev, struct inet6_dev **idev,
28139fbb704cSDavid Ahern 			   struct netlink_ext_ack *extack)
28149fbb704cSDavid Ahern {
28159fbb704cSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
28169fbb704cSDavid Ahern 	int gwa_type = ipv6_addr_type(gw_addr);
2817232378e8SDavid Ahern 	bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
28189fbb704cSDavid Ahern 	const struct net_device *dev = *_dev;
2819232378e8SDavid Ahern 	bool need_addr_check = !dev;
28209fbb704cSDavid Ahern 	int err = -EINVAL;
28219fbb704cSDavid Ahern 
28229fbb704cSDavid Ahern 	/* if gw_addr is local we will fail to detect this in case
28239fbb704cSDavid Ahern 	 * address is still TENTATIVE (DAD in progress). rt6_lookup()
28249fbb704cSDavid Ahern 	 * will return already-added prefix route via interface that
28259fbb704cSDavid Ahern 	 * prefix route was assigned to, which might be non-loopback.
28269fbb704cSDavid Ahern 	 */
2827232378e8SDavid Ahern 	if (dev &&
2828232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2829232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
28309fbb704cSDavid Ahern 		goto out;
28319fbb704cSDavid Ahern 	}
28329fbb704cSDavid Ahern 
28339fbb704cSDavid Ahern 	if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
28349fbb704cSDavid Ahern 		/* IPv6 strictly inhibits using not link-local
28359fbb704cSDavid Ahern 		 * addresses as nexthop address.
28369fbb704cSDavid Ahern 		 * Otherwise, router will not able to send redirects.
28379fbb704cSDavid Ahern 		 * It is very good, but in some (rare!) circumstances
28389fbb704cSDavid Ahern 		 * (SIT, PtP, NBMA NOARP links) it is handy to allow
28399fbb704cSDavid Ahern 		 * some exceptions. --ANK
28409fbb704cSDavid Ahern 		 * We allow IPv4-mapped nexthops to support RFC4798-type
28419fbb704cSDavid Ahern 		 * addressing
28429fbb704cSDavid Ahern 		 */
28439fbb704cSDavid Ahern 		if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
28449fbb704cSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
28459fbb704cSDavid Ahern 			goto out;
28469fbb704cSDavid Ahern 		}
28479fbb704cSDavid Ahern 
28489fbb704cSDavid Ahern 		if (cfg->fc_flags & RTNH_F_ONLINK)
28499fbb704cSDavid Ahern 			err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
28509fbb704cSDavid Ahern 		else
28519fbb704cSDavid Ahern 			err = ip6_route_check_nh(net, cfg, _dev, idev);
28529fbb704cSDavid Ahern 
28539fbb704cSDavid Ahern 		if (err)
28549fbb704cSDavid Ahern 			goto out;
28559fbb704cSDavid Ahern 	}
28569fbb704cSDavid Ahern 
28579fbb704cSDavid Ahern 	/* reload in case device was changed */
28589fbb704cSDavid Ahern 	dev = *_dev;
28599fbb704cSDavid Ahern 
28609fbb704cSDavid Ahern 	err = -EINVAL;
28619fbb704cSDavid Ahern 	if (!dev) {
28629fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Egress device not specified");
28639fbb704cSDavid Ahern 		goto out;
28649fbb704cSDavid Ahern 	} else if (dev->flags & IFF_LOOPBACK) {
28659fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack,
28669fbb704cSDavid Ahern 			       "Egress device can not be loopback device for this route");
28679fbb704cSDavid Ahern 		goto out;
28689fbb704cSDavid Ahern 	}
2869232378e8SDavid Ahern 
2870232378e8SDavid Ahern 	/* if we did not check gw_addr above, do so now that the
2871232378e8SDavid Ahern 	 * egress device has been resolved.
2872232378e8SDavid Ahern 	 */
2873232378e8SDavid Ahern 	if (need_addr_check &&
2874232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2875232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
2876232378e8SDavid Ahern 		goto out;
2877232378e8SDavid Ahern 	}
2878232378e8SDavid Ahern 
28799fbb704cSDavid Ahern 	err = 0;
28809fbb704cSDavid Ahern out:
28819fbb704cSDavid Ahern 	return err;
28829fbb704cSDavid Ahern }
28839fbb704cSDavid Ahern 
28848d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
2885acb54e3cSDavid Ahern 					      gfp_t gfp_flags,
2886333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
28871da177e4SLinus Torvalds {
28885578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
28898d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
28901da177e4SLinus Torvalds 	struct net_device *dev = NULL;
28911da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
2892c71099acSThomas Graf 	struct fib6_table *table;
28931da177e4SLinus Torvalds 	int addr_type;
28948c5b83f0SRoopa Prabhu 	int err = -EINVAL;
28951da177e4SLinus Torvalds 
2896557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
2897d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
2898d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
2899557c44beSDavid Ahern 		goto out;
2900d5d531cbSDavid Ahern 	}
2901557c44beSDavid Ahern 
29022ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
29032ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
29042ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
29052ea2352eSWei Wang 		goto out;
29062ea2352eSWei Wang 	}
29072ea2352eSWei Wang 
2908e8478e80SDavid Ahern 	if (cfg->fc_type > RTN_MAX) {
2909e8478e80SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid route type");
2910e8478e80SDavid Ahern 		goto out;
2911e8478e80SDavid Ahern 	}
2912e8478e80SDavid Ahern 
2913d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
2914d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
29158c5b83f0SRoopa Prabhu 		goto out;
2916d5d531cbSDavid Ahern 	}
2917d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
2918d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
2919d5d531cbSDavid Ahern 		goto out;
2920d5d531cbSDavid Ahern 	}
29211da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
2922d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
2923d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
2924d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
29258c5b83f0SRoopa Prabhu 		goto out;
2926d5d531cbSDavid Ahern 	}
29271da177e4SLinus Torvalds #endif
292886872cb5SThomas Graf 	if (cfg->fc_ifindex) {
29291da177e4SLinus Torvalds 		err = -ENODEV;
29305578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
29311da177e4SLinus Torvalds 		if (!dev)
29321da177e4SLinus Torvalds 			goto out;
29331da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
29341da177e4SLinus Torvalds 		if (!idev)
29351da177e4SLinus Torvalds 			goto out;
29361da177e4SLinus Torvalds 	}
29371da177e4SLinus Torvalds 
293886872cb5SThomas Graf 	if (cfg->fc_metric == 0)
293986872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
29401da177e4SLinus Torvalds 
2941fc1e64e1SDavid Ahern 	if (cfg->fc_flags & RTNH_F_ONLINK) {
2942fc1e64e1SDavid Ahern 		if (!dev) {
2943fc1e64e1SDavid Ahern 			NL_SET_ERR_MSG(extack,
2944fc1e64e1SDavid Ahern 				       "Nexthop device required for onlink");
2945fc1e64e1SDavid Ahern 			err = -ENODEV;
2946fc1e64e1SDavid Ahern 			goto out;
2947fc1e64e1SDavid Ahern 		}
2948fc1e64e1SDavid Ahern 
2949fc1e64e1SDavid Ahern 		if (!(dev->flags & IFF_UP)) {
2950fc1e64e1SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2951fc1e64e1SDavid Ahern 			err = -ENETDOWN;
2952fc1e64e1SDavid Ahern 			goto out;
2953fc1e64e1SDavid Ahern 		}
2954fc1e64e1SDavid Ahern 	}
2955fc1e64e1SDavid Ahern 
2956c71099acSThomas Graf 	err = -ENOBUFS;
295738308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
2958d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
2959d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
296038308473SDavid S. Miller 		if (!table) {
2961f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
2962d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
2963d71314b4SMatti Vaittinen 		}
2964d71314b4SMatti Vaittinen 	} else {
2965d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
2966d71314b4SMatti Vaittinen 	}
296738308473SDavid S. Miller 
296838308473SDavid S. Miller 	if (!table)
2969c71099acSThomas Graf 		goto out;
2970c71099acSThomas Graf 
29711da177e4SLinus Torvalds 	err = -ENOMEM;
297293531c67SDavid Ahern 	rt = fib6_info_alloc(gfp_flags);
297393531c67SDavid Ahern 	if (!rt)
29741da177e4SLinus Torvalds 		goto out;
297593531c67SDavid Ahern 
2976767a2217SDavid Ahern 	rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len);
2977767a2217SDavid Ahern 	if (IS_ERR(rt->fib6_metrics)) {
2978767a2217SDavid Ahern 		err = PTR_ERR(rt->fib6_metrics);
2979fda21d46SEric Dumazet 		/* Do not leave garbage there. */
2980fda21d46SEric Dumazet 		rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
2981767a2217SDavid Ahern 		goto out;
2982767a2217SDavid Ahern 	}
2983767a2217SDavid Ahern 
298493531c67SDavid Ahern 	if (cfg->fc_flags & RTF_ADDRCONF)
298593531c67SDavid Ahern 		rt->dst_nocount = true;
29861da177e4SLinus Torvalds 
29871716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
298814895687SDavid Ahern 		fib6_set_expires(rt, jiffies +
29891716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
29901716a961SGao feng 	else
299114895687SDavid Ahern 		fib6_clean_expires(rt);
29921da177e4SLinus Torvalds 
299386872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
299486872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
299593c2fb25SDavid Ahern 	rt->fib6_protocol = cfg->fc_protocol;
299686872cb5SThomas Graf 
299786872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
29981da177e4SLinus Torvalds 
299919e42e45SRoopa Prabhu 	if (cfg->fc_encap) {
300019e42e45SRoopa Prabhu 		struct lwtunnel_state *lwtstate;
300119e42e45SRoopa Prabhu 
300230357d7dSDavid Ahern 		err = lwtunnel_build_state(cfg->fc_encap_type,
3003127eb7cdSTom Herbert 					   cfg->fc_encap, AF_INET6, cfg,
30049ae28727SDavid Ahern 					   &lwtstate, extack);
300519e42e45SRoopa Prabhu 		if (err)
300619e42e45SRoopa Prabhu 			goto out;
30075e670d84SDavid Ahern 		rt->fib6_nh.nh_lwtstate = lwtstate_get(lwtstate);
300825368623STom Herbert 	}
300919e42e45SRoopa Prabhu 
301093c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
301193c2fb25SDavid Ahern 	rt->fib6_dst.plen = cfg->fc_dst_len;
301293c2fb25SDavid Ahern 	if (rt->fib6_dst.plen == 128)
30133b6761d1SDavid Ahern 		rt->dst_host = true;
30141da177e4SLinus Torvalds 
30151da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
301693c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
301793c2fb25SDavid Ahern 	rt->fib6_src.plen = cfg->fc_src_len;
30181da177e4SLinus Torvalds #endif
30191da177e4SLinus Torvalds 
302093c2fb25SDavid Ahern 	rt->fib6_metric = cfg->fc_metric;
30215e670d84SDavid Ahern 	rt->fib6_nh.nh_weight = 1;
30221da177e4SLinus Torvalds 
3023e8478e80SDavid Ahern 	rt->fib6_type = cfg->fc_type;
30241da177e4SLinus Torvalds 
30251da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
30261da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
30271da177e4SLinus Torvalds 	 */
302886872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
302938308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
303038308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
303138308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
30321da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
30335578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
30341da177e4SLinus Torvalds 			if (dev) {
30351da177e4SLinus Torvalds 				dev_put(dev);
30361da177e4SLinus Torvalds 				in6_dev_put(idev);
30371da177e4SLinus Torvalds 			}
30385578689aSDaniel Lezcano 			dev = net->loopback_dev;
30391da177e4SLinus Torvalds 			dev_hold(dev);
30401da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
30411da177e4SLinus Torvalds 			if (!idev) {
30421da177e4SLinus Torvalds 				err = -ENODEV;
30431da177e4SLinus Torvalds 				goto out;
30441da177e4SLinus Torvalds 			}
30451da177e4SLinus Torvalds 		}
304693c2fb25SDavid Ahern 		rt->fib6_flags = RTF_REJECT|RTF_NONEXTHOP;
30471da177e4SLinus Torvalds 		goto install_route;
30481da177e4SLinus Torvalds 	}
30491da177e4SLinus Torvalds 
305086872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
30519fbb704cSDavid Ahern 		err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
30521da177e4SLinus Torvalds 		if (err)
30531da177e4SLinus Torvalds 			goto out;
30549fbb704cSDavid Ahern 
305593531c67SDavid Ahern 		rt->fib6_nh.nh_gw = cfg->fc_gateway;
30561da177e4SLinus Torvalds 	}
30571da177e4SLinus Torvalds 
30581da177e4SLinus Torvalds 	err = -ENODEV;
305938308473SDavid S. Miller 	if (!dev)
30601da177e4SLinus Torvalds 		goto out;
30611da177e4SLinus Torvalds 
3062428604fbSLorenzo Bianconi 	if (idev->cnf.disable_ipv6) {
3063428604fbSLorenzo Bianconi 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3064428604fbSLorenzo Bianconi 		err = -EACCES;
3065428604fbSLorenzo Bianconi 		goto out;
3066428604fbSLorenzo Bianconi 	}
3067428604fbSLorenzo Bianconi 
3068955ec4cbSDavid Ahern 	if (!(dev->flags & IFF_UP)) {
3069955ec4cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3070955ec4cbSDavid Ahern 		err = -ENETDOWN;
3071955ec4cbSDavid Ahern 		goto out;
3072955ec4cbSDavid Ahern 	}
3073955ec4cbSDavid Ahern 
3074c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3075c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3076d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
3077c3968a85SDaniel Walter 			err = -EINVAL;
3078c3968a85SDaniel Walter 			goto out;
3079c3968a85SDaniel Walter 		}
308093c2fb25SDavid Ahern 		rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
308193c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 128;
3082c3968a85SDaniel Walter 	} else
308393c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
3084c3968a85SDaniel Walter 
308593c2fb25SDavid Ahern 	rt->fib6_flags = cfg->fc_flags;
30861da177e4SLinus Torvalds 
30871da177e4SLinus Torvalds install_route:
308893c2fb25SDavid Ahern 	if (!(rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
30895609b80aSIdo Schimmel 	    !netif_carrier_ok(dev))
30905e670d84SDavid Ahern 		rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
30915e670d84SDavid Ahern 	rt->fib6_nh.nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK);
309293531c67SDavid Ahern 	rt->fib6_nh.nh_dev = dev;
309393c2fb25SDavid Ahern 	rt->fib6_table = table;
309463152fc0SDaniel Lezcano 
3095dcd1f572SDavid Ahern 	if (idev)
3096dcd1f572SDavid Ahern 		in6_dev_put(idev);
3097dcd1f572SDavid Ahern 
30988c5b83f0SRoopa Prabhu 	return rt;
30991da177e4SLinus Torvalds out:
31001da177e4SLinus Torvalds 	if (dev)
31011da177e4SLinus Torvalds 		dev_put(dev);
31021da177e4SLinus Torvalds 	if (idev)
31031da177e4SLinus Torvalds 		in6_dev_put(idev);
31046b9ea5a6SRoopa Prabhu 
310593531c67SDavid Ahern 	fib6_info_release(rt);
31068c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
31076b9ea5a6SRoopa Prabhu }
31086b9ea5a6SRoopa Prabhu 
3109acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3110333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
31116b9ea5a6SRoopa Prabhu {
31128d1c802bSDavid Ahern 	struct fib6_info *rt;
31136b9ea5a6SRoopa Prabhu 	int err;
31146b9ea5a6SRoopa Prabhu 
3115acb54e3cSDavid Ahern 	rt = ip6_route_info_create(cfg, gfp_flags, extack);
3116d4ead6b3SDavid Ahern 	if (IS_ERR(rt))
3117d4ead6b3SDavid Ahern 		return PTR_ERR(rt);
31186b9ea5a6SRoopa Prabhu 
3119d4ead6b3SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
312093531c67SDavid Ahern 	fib6_info_release(rt);
31216b9ea5a6SRoopa Prabhu 
31221da177e4SLinus Torvalds 	return err;
31231da177e4SLinus Torvalds }
31241da177e4SLinus Torvalds 
31258d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
31261da177e4SLinus Torvalds {
3127afb1d4b5SDavid Ahern 	struct net *net = info->nl_net;
3128c71099acSThomas Graf 	struct fib6_table *table;
3129afb1d4b5SDavid Ahern 	int err;
31301da177e4SLinus Torvalds 
3131421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
31326825a26cSGao feng 		err = -ENOENT;
31336825a26cSGao feng 		goto out;
31346825a26cSGao feng 	}
31356c813a72SPatrick McHardy 
313693c2fb25SDavid Ahern 	table = rt->fib6_table;
313766f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
313886872cb5SThomas Graf 	err = fib6_del(rt, info);
313966f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
31401da177e4SLinus Torvalds 
31416825a26cSGao feng out:
314293531c67SDavid Ahern 	fib6_info_release(rt);
31431da177e4SLinus Torvalds 	return err;
31441da177e4SLinus Torvalds }
31451da177e4SLinus Torvalds 
31468d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt)
3147e0a1ad73SThomas Graf {
3148afb1d4b5SDavid Ahern 	struct nl_info info = { .nl_net = net };
3149afb1d4b5SDavid Ahern 
3150528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
3151e0a1ad73SThomas Graf }
3152e0a1ad73SThomas Graf 
31538d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
31540ae81335SDavid Ahern {
31550ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
3156e3330039SWANG Cong 	struct net *net = info->nl_net;
315716a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
31580ae81335SDavid Ahern 	struct fib6_table *table;
3159e3330039SWANG Cong 	int err = -ENOENT;
31600ae81335SDavid Ahern 
3161421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
3162e3330039SWANG Cong 		goto out_put;
316393c2fb25SDavid Ahern 	table = rt->fib6_table;
316466f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
31650ae81335SDavid Ahern 
316693c2fb25SDavid Ahern 	if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
31678d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
31680ae81335SDavid Ahern 
316916a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
317016a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
317116a16cd3SDavid Ahern 		if (skb) {
317216a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
317316a16cd3SDavid Ahern 
3174d4ead6b3SDavid Ahern 			if (rt6_fill_node(net, skb, rt, NULL,
317516a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
317616a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
317716a16cd3SDavid Ahern 				kfree_skb(skb);
317816a16cd3SDavid Ahern 				skb = NULL;
317916a16cd3SDavid Ahern 			} else
318016a16cd3SDavid Ahern 				info->skip_notify = 1;
318116a16cd3SDavid Ahern 		}
318216a16cd3SDavid Ahern 
31830ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
318493c2fb25SDavid Ahern 					 &rt->fib6_siblings,
318593c2fb25SDavid Ahern 					 fib6_siblings) {
31860ae81335SDavid Ahern 			err = fib6_del(sibling, info);
31870ae81335SDavid Ahern 			if (err)
3188e3330039SWANG Cong 				goto out_unlock;
31890ae81335SDavid Ahern 		}
31900ae81335SDavid Ahern 	}
31910ae81335SDavid Ahern 
31920ae81335SDavid Ahern 	err = fib6_del(rt, info);
3193e3330039SWANG Cong out_unlock:
319466f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
3195e3330039SWANG Cong out_put:
319693531c67SDavid Ahern 	fib6_info_release(rt);
319716a16cd3SDavid Ahern 
319816a16cd3SDavid Ahern 	if (skb) {
3199e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
320016a16cd3SDavid Ahern 			    info->nlh, gfp_any());
320116a16cd3SDavid Ahern 	}
32020ae81335SDavid Ahern 	return err;
32030ae81335SDavid Ahern }
32040ae81335SDavid Ahern 
320523fb93a4SDavid Ahern static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
320623fb93a4SDavid Ahern {
320723fb93a4SDavid Ahern 	int rc = -ESRCH;
320823fb93a4SDavid Ahern 
320923fb93a4SDavid Ahern 	if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
321023fb93a4SDavid Ahern 		goto out;
321123fb93a4SDavid Ahern 
321223fb93a4SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY &&
321323fb93a4SDavid Ahern 	    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
321423fb93a4SDavid Ahern 		goto out;
321523fb93a4SDavid Ahern 	if (dst_hold_safe(&rt->dst))
321623fb93a4SDavid Ahern 		rc = rt6_remove_exception_rt(rt);
321723fb93a4SDavid Ahern out:
321823fb93a4SDavid Ahern 	return rc;
321923fb93a4SDavid Ahern }
322023fb93a4SDavid Ahern 
3221333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
3222333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
32231da177e4SLinus Torvalds {
32248d1c802bSDavid Ahern 	struct rt6_info *rt_cache;
3225c71099acSThomas Graf 	struct fib6_table *table;
32268d1c802bSDavid Ahern 	struct fib6_info *rt;
32271da177e4SLinus Torvalds 	struct fib6_node *fn;
32281da177e4SLinus Torvalds 	int err = -ESRCH;
32291da177e4SLinus Torvalds 
32305578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3231d5d531cbSDavid Ahern 	if (!table) {
3232d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
3233c71099acSThomas Graf 		return err;
3234d5d531cbSDavid Ahern 	}
32351da177e4SLinus Torvalds 
323666f5d6ceSWei Wang 	rcu_read_lock();
3237c71099acSThomas Graf 
3238c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
323986872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
324038fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
32412b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
32421da177e4SLinus Torvalds 
32431da177e4SLinus Torvalds 	if (fn) {
324466f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
32452b760fcfSWei Wang 			if (cfg->fc_flags & RTF_CACHE) {
324623fb93a4SDavid Ahern 				int rc;
324723fb93a4SDavid Ahern 
32482b760fcfSWei Wang 				rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
32492b760fcfSWei Wang 							      &cfg->fc_src);
325023fb93a4SDavid Ahern 				if (rt_cache) {
325123fb93a4SDavid Ahern 					rc = ip6_del_cached_rt(rt_cache, cfg);
32529e575010SEric Dumazet 					if (rc != -ESRCH) {
32539e575010SEric Dumazet 						rcu_read_unlock();
325423fb93a4SDavid Ahern 						return rc;
325523fb93a4SDavid Ahern 					}
32569e575010SEric Dumazet 				}
32571f56a01fSMartin KaFai Lau 				continue;
32582b760fcfSWei Wang 			}
325986872cb5SThomas Graf 			if (cfg->fc_ifindex &&
32605e670d84SDavid Ahern 			    (!rt->fib6_nh.nh_dev ||
32615e670d84SDavid Ahern 			     rt->fib6_nh.nh_dev->ifindex != cfg->fc_ifindex))
32621da177e4SLinus Torvalds 				continue;
326386872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
32645e670d84SDavid Ahern 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->fib6_nh.nh_gw))
32651da177e4SLinus Torvalds 				continue;
326693c2fb25SDavid Ahern 			if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
32671da177e4SLinus Torvalds 				continue;
326893c2fb25SDavid Ahern 			if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
3269c2ed1880SMantas M 				continue;
3270e873e4b9SWei Wang 			if (!fib6_info_hold_safe(rt))
3271e873e4b9SWei Wang 				continue;
327266f5d6ceSWei Wang 			rcu_read_unlock();
32731da177e4SLinus Torvalds 
32740ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
32750ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
327686872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
32770ae81335SDavid Ahern 
32780ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
32791da177e4SLinus Torvalds 		}
32801da177e4SLinus Torvalds 	}
328166f5d6ceSWei Wang 	rcu_read_unlock();
32821da177e4SLinus Torvalds 
32831da177e4SLinus Torvalds 	return err;
32841da177e4SLinus Torvalds }
32851da177e4SLinus Torvalds 
32866700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3287a6279458SYOSHIFUJI Hideaki {
3288a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
3289e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
3290e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
3291e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
3292e8599ff4SDavid S. Miller 	struct neighbour *neigh;
3293a68886a6SDavid Ahern 	struct fib6_info *from;
329471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
32956e157b6aSDavid S. Miller 	int optlen, on_link;
32966e157b6aSDavid S. Miller 	u8 *lladdr;
3297e8599ff4SDavid S. Miller 
329829a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
329971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
3300e8599ff4SDavid S. Miller 
3301e8599ff4SDavid S. Miller 	if (optlen < 0) {
33026e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3303e8599ff4SDavid S. Miller 		return;
3304e8599ff4SDavid S. Miller 	}
3305e8599ff4SDavid S. Miller 
330671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
3307e8599ff4SDavid S. Miller 
330871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
33096e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3310e8599ff4SDavid S. Miller 		return;
3311e8599ff4SDavid S. Miller 	}
3312e8599ff4SDavid S. Miller 
33136e157b6aSDavid S. Miller 	on_link = 0;
331471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
3315e8599ff4SDavid S. Miller 		on_link = 1;
331671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
3317e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
33186e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
3319e8599ff4SDavid S. Miller 		return;
3320e8599ff4SDavid S. Miller 	}
3321e8599ff4SDavid S. Miller 
3322e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
3323e8599ff4SDavid S. Miller 	if (!in6_dev)
3324e8599ff4SDavid S. Miller 		return;
3325e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3326e8599ff4SDavid S. Miller 		return;
3327e8599ff4SDavid S. Miller 
3328e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
3329e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
3330e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
3331e8599ff4SDavid S. Miller 	 */
3332e8599ff4SDavid S. Miller 
3333f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
3334e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3335e8599ff4SDavid S. Miller 		return;
3336e8599ff4SDavid S. Miller 	}
33376e157b6aSDavid S. Miller 
33386e157b6aSDavid S. Miller 	lladdr = NULL;
3339e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
3340e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3341e8599ff4SDavid S. Miller 					     skb->dev);
3342e8599ff4SDavid S. Miller 		if (!lladdr) {
3343e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3344e8599ff4SDavid S. Miller 			return;
3345e8599ff4SDavid S. Miller 		}
3346e8599ff4SDavid S. Miller 	}
3347e8599ff4SDavid S. Miller 
33486e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
3349ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
33506e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
33516e157b6aSDavid S. Miller 		return;
33526e157b6aSDavid S. Miller 	}
33536e157b6aSDavid S. Miller 
33546e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
33556e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
33566e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
33576e157b6aSDavid S. Miller 	 */
33580dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
33596e157b6aSDavid S. Miller 
336071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
3361e8599ff4SDavid S. Miller 	if (!neigh)
3362e8599ff4SDavid S. Miller 		return;
3363e8599ff4SDavid S. Miller 
33641da177e4SLinus Torvalds 	/*
33651da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
33661da177e4SLinus Torvalds 	 */
33671da177e4SLinus Torvalds 
3368f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
33691da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
33701da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
33711da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
3372f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
3373f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
33741da177e4SLinus Torvalds 
33754d85cd0cSDavid Ahern 	rcu_read_lock();
3376a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
3377e873e4b9SWei Wang 	/* This fib6_info_hold() is safe here because we hold reference to rt
3378e873e4b9SWei Wang 	 * and rt already holds reference to fib6_info.
3379e873e4b9SWei Wang 	 */
33808a14e46fSDavid Ahern 	fib6_info_hold(from);
33814d85cd0cSDavid Ahern 	rcu_read_unlock();
33828a14e46fSDavid Ahern 
33838a14e46fSDavid Ahern 	nrt = ip6_rt_cache_alloc(from, &msg->dest, NULL);
338438308473SDavid S. Miller 	if (!nrt)
33851da177e4SLinus Torvalds 		goto out;
33861da177e4SLinus Torvalds 
33871da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
33881da177e4SLinus Torvalds 	if (on_link)
33891da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
33901da177e4SLinus Torvalds 
33914e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
33921da177e4SLinus Torvalds 
33932b760fcfSWei Wang 	/* No need to remove rt from the exception table if rt is
33942b760fcfSWei Wang 	 * a cached route because rt6_insert_exception() will
33952b760fcfSWei Wang 	 * takes care of it
33962b760fcfSWei Wang 	 */
33978a14e46fSDavid Ahern 	if (rt6_insert_exception(nrt, from)) {
33982b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
33992b760fcfSWei Wang 		goto out;
34002b760fcfSWei Wang 	}
34011da177e4SLinus Torvalds 
3402d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
3403d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
340471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
340560592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
34068d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
34078d71740cSTom Tucker 
34081da177e4SLinus Torvalds out:
34098a14e46fSDavid Ahern 	fib6_info_release(from);
3410e8599ff4SDavid S. Miller 	neigh_release(neigh);
34116e157b6aSDavid S. Miller }
34126e157b6aSDavid S. Miller 
341370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
34148d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
3415b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3416830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3417830218c1SDavid Ahern 					   struct net_device *dev)
341870ceb4f5SYOSHIFUJI Hideaki {
3419830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3420830218c1SDavid Ahern 	int ifindex = dev->ifindex;
342170ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
34228d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3423c71099acSThomas Graf 	struct fib6_table *table;
342470ceb4f5SYOSHIFUJI Hideaki 
3425830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
342638308473SDavid S. Miller 	if (!table)
3427c71099acSThomas Graf 		return NULL;
3428c71099acSThomas Graf 
342966f5d6ceSWei Wang 	rcu_read_lock();
343038fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
343170ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
343270ceb4f5SYOSHIFUJI Hideaki 		goto out;
343370ceb4f5SYOSHIFUJI Hideaki 
343466f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
34355e670d84SDavid Ahern 		if (rt->fib6_nh.nh_dev->ifindex != ifindex)
343670ceb4f5SYOSHIFUJI Hideaki 			continue;
343793c2fb25SDavid Ahern 		if ((rt->fib6_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
343870ceb4f5SYOSHIFUJI Hideaki 			continue;
34395e670d84SDavid Ahern 		if (!ipv6_addr_equal(&rt->fib6_nh.nh_gw, gwaddr))
344070ceb4f5SYOSHIFUJI Hideaki 			continue;
3441e873e4b9SWei Wang 		if (!fib6_info_hold_safe(rt))
3442e873e4b9SWei Wang 			continue;
344370ceb4f5SYOSHIFUJI Hideaki 		break;
344470ceb4f5SYOSHIFUJI Hideaki 	}
344570ceb4f5SYOSHIFUJI Hideaki out:
344666f5d6ceSWei Wang 	rcu_read_unlock();
344770ceb4f5SYOSHIFUJI Hideaki 	return rt;
344870ceb4f5SYOSHIFUJI Hideaki }
344970ceb4f5SYOSHIFUJI Hideaki 
34508d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
3451b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3452830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3453830218c1SDavid Ahern 					   struct net_device *dev,
345495c96174SEric Dumazet 					   unsigned int pref)
345570ceb4f5SYOSHIFUJI Hideaki {
345686872cb5SThomas Graf 	struct fib6_config cfg = {
3457238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
3458830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
345986872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
346086872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
346186872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
3462b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3463e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
346415e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
3465efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3466efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
346786872cb5SThomas Graf 	};
346870ceb4f5SYOSHIFUJI Hideaki 
3469830218c1SDavid Ahern 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
34704e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
34714e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
347286872cb5SThomas Graf 
3473e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
3474e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
347586872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
347670ceb4f5SYOSHIFUJI Hideaki 
3477acb54e3cSDavid Ahern 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
347870ceb4f5SYOSHIFUJI Hideaki 
3479830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
348070ceb4f5SYOSHIFUJI Hideaki }
348170ceb4f5SYOSHIFUJI Hideaki #endif
348270ceb4f5SYOSHIFUJI Hideaki 
34838d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net,
3484afb1d4b5SDavid Ahern 				     const struct in6_addr *addr,
3485afb1d4b5SDavid Ahern 				     struct net_device *dev)
34861da177e4SLinus Torvalds {
3487830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
34888d1c802bSDavid Ahern 	struct fib6_info *rt;
3489c71099acSThomas Graf 	struct fib6_table *table;
34901da177e4SLinus Torvalds 
3491afb1d4b5SDavid Ahern 	table = fib6_get_table(net, tb_id);
349238308473SDavid S. Miller 	if (!table)
3493c71099acSThomas Graf 		return NULL;
34941da177e4SLinus Torvalds 
349566f5d6ceSWei Wang 	rcu_read_lock();
349666f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
34975e670d84SDavid Ahern 		if (dev == rt->fib6_nh.nh_dev &&
349893c2fb25SDavid Ahern 		    ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
34995e670d84SDavid Ahern 		    ipv6_addr_equal(&rt->fib6_nh.nh_gw, addr))
35001da177e4SLinus Torvalds 			break;
35011da177e4SLinus Torvalds 	}
3502e873e4b9SWei Wang 	if (rt && !fib6_info_hold_safe(rt))
3503e873e4b9SWei Wang 		rt = NULL;
350466f5d6ceSWei Wang 	rcu_read_unlock();
35051da177e4SLinus Torvalds 	return rt;
35061da177e4SLinus Torvalds }
35071da177e4SLinus Torvalds 
35088d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net,
3509afb1d4b5SDavid Ahern 				     const struct in6_addr *gwaddr,
3510ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
3511ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
35121da177e4SLinus Torvalds {
351386872cb5SThomas Graf 	struct fib6_config cfg = {
3514ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
3515238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
351686872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
351786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
351886872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
3519b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3520e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
352115e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
35225578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3523afb1d4b5SDavid Ahern 		.fc_nlinfo.nl_net = net,
352486872cb5SThomas Graf 	};
35251da177e4SLinus Torvalds 
35264e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
35271da177e4SLinus Torvalds 
3528acb54e3cSDavid Ahern 	if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
3529830218c1SDavid Ahern 		struct fib6_table *table;
3530830218c1SDavid Ahern 
3531830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
3532830218c1SDavid Ahern 		if (table)
3533830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3534830218c1SDavid Ahern 	}
35351da177e4SLinus Torvalds 
3536afb1d4b5SDavid Ahern 	return rt6_get_dflt_router(net, gwaddr, dev);
35371da177e4SLinus Torvalds }
35381da177e4SLinus Torvalds 
3539afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net,
3540afb1d4b5SDavid Ahern 				     struct fib6_table *table)
35411da177e4SLinus Torvalds {
35428d1c802bSDavid Ahern 	struct fib6_info *rt;
35431da177e4SLinus Torvalds 
35441da177e4SLinus Torvalds restart:
354566f5d6ceSWei Wang 	rcu_read_lock();
354666f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
3547dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
3548dcd1f572SDavid Ahern 		struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3549dcd1f572SDavid Ahern 
355093c2fb25SDavid Ahern 		if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
3551e873e4b9SWei Wang 		    (!idev || idev->cnf.accept_ra != 2) &&
3552e873e4b9SWei Wang 		    fib6_info_hold_safe(rt)) {
355366f5d6ceSWei Wang 			rcu_read_unlock();
3554afb1d4b5SDavid Ahern 			ip6_del_rt(net, rt);
35551da177e4SLinus Torvalds 			goto restart;
35561da177e4SLinus Torvalds 		}
35571da177e4SLinus Torvalds 	}
355866f5d6ceSWei Wang 	rcu_read_unlock();
3559830218c1SDavid Ahern 
3560830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3561830218c1SDavid Ahern }
3562830218c1SDavid Ahern 
3563830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
3564830218c1SDavid Ahern {
3565830218c1SDavid Ahern 	struct fib6_table *table;
3566830218c1SDavid Ahern 	struct hlist_head *head;
3567830218c1SDavid Ahern 	unsigned int h;
3568830218c1SDavid Ahern 
3569830218c1SDavid Ahern 	rcu_read_lock();
3570830218c1SDavid Ahern 
3571830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3572830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
3573830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3574830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3575afb1d4b5SDavid Ahern 				__rt6_purge_dflt_routers(net, table);
3576830218c1SDavid Ahern 		}
3577830218c1SDavid Ahern 	}
3578830218c1SDavid Ahern 
3579830218c1SDavid Ahern 	rcu_read_unlock();
35801da177e4SLinus Torvalds }
35811da177e4SLinus Torvalds 
35825578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
35835578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
358486872cb5SThomas Graf 				 struct fib6_config *cfg)
358586872cb5SThomas Graf {
35868823a3acSMaciej Żenczykowski 	*cfg = (struct fib6_config){
35878823a3acSMaciej Żenczykowski 		.fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
35888823a3acSMaciej Żenczykowski 			 : RT6_TABLE_MAIN,
35898823a3acSMaciej Żenczykowski 		.fc_ifindex = rtmsg->rtmsg_ifindex,
35908823a3acSMaciej Żenczykowski 		.fc_metric = rtmsg->rtmsg_metric,
35918823a3acSMaciej Żenczykowski 		.fc_expires = rtmsg->rtmsg_info,
35928823a3acSMaciej Żenczykowski 		.fc_dst_len = rtmsg->rtmsg_dst_len,
35938823a3acSMaciej Żenczykowski 		.fc_src_len = rtmsg->rtmsg_src_len,
35948823a3acSMaciej Żenczykowski 		.fc_flags = rtmsg->rtmsg_flags,
35958823a3acSMaciej Żenczykowski 		.fc_type = rtmsg->rtmsg_type,
359686872cb5SThomas Graf 
35978823a3acSMaciej Żenczykowski 		.fc_nlinfo.nl_net = net,
359886872cb5SThomas Graf 
35998823a3acSMaciej Żenczykowski 		.fc_dst = rtmsg->rtmsg_dst,
36008823a3acSMaciej Żenczykowski 		.fc_src = rtmsg->rtmsg_src,
36018823a3acSMaciej Żenczykowski 		.fc_gateway = rtmsg->rtmsg_gateway,
36028823a3acSMaciej Żenczykowski 	};
360386872cb5SThomas Graf }
360486872cb5SThomas Graf 
36055578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
36061da177e4SLinus Torvalds {
360786872cb5SThomas Graf 	struct fib6_config cfg;
36081da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
36091da177e4SLinus Torvalds 	int err;
36101da177e4SLinus Torvalds 
36111da177e4SLinus Torvalds 	switch (cmd) {
36121da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
36131da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
3614af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
36151da177e4SLinus Torvalds 			return -EPERM;
36161da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
36171da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
36181da177e4SLinus Torvalds 		if (err)
36191da177e4SLinus Torvalds 			return -EFAULT;
36201da177e4SLinus Torvalds 
36215578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
362286872cb5SThomas Graf 
36231da177e4SLinus Torvalds 		rtnl_lock();
36241da177e4SLinus Torvalds 		switch (cmd) {
36251da177e4SLinus Torvalds 		case SIOCADDRT:
3626acb54e3cSDavid Ahern 			err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
36271da177e4SLinus Torvalds 			break;
36281da177e4SLinus Torvalds 		case SIOCDELRT:
3629333c4301SDavid Ahern 			err = ip6_route_del(&cfg, NULL);
36301da177e4SLinus Torvalds 			break;
36311da177e4SLinus Torvalds 		default:
36321da177e4SLinus Torvalds 			err = -EINVAL;
36331da177e4SLinus Torvalds 		}
36341da177e4SLinus Torvalds 		rtnl_unlock();
36351da177e4SLinus Torvalds 
36361da177e4SLinus Torvalds 		return err;
36373ff50b79SStephen Hemminger 	}
36381da177e4SLinus Torvalds 
36391da177e4SLinus Torvalds 	return -EINVAL;
36401da177e4SLinus Torvalds }
36411da177e4SLinus Torvalds 
36421da177e4SLinus Torvalds /*
36431da177e4SLinus Torvalds  *	Drop the packet on the floor
36441da177e4SLinus Torvalds  */
36451da177e4SLinus Torvalds 
3646d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
36471da177e4SLinus Torvalds {
3648612f09e8SYOSHIFUJI Hideaki 	int type;
3649adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
3650612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
3651612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
36520660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
365345bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
3654bdb7cc64SStephen Suryaputra 			IP6_INC_STATS(dev_net(dst->dev),
3655bdb7cc64SStephen Suryaputra 				      __in6_dev_get_safely(skb->dev),
36563bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
3657612f09e8SYOSHIFUJI Hideaki 			break;
3658612f09e8SYOSHIFUJI Hideaki 		}
3659612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
3660612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
36613bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
36623bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
3663612f09e8SYOSHIFUJI Hideaki 		break;
3664612f09e8SYOSHIFUJI Hideaki 	}
36653ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
36661da177e4SLinus Torvalds 	kfree_skb(skb);
36671da177e4SLinus Torvalds 	return 0;
36681da177e4SLinus Torvalds }
36691da177e4SLinus Torvalds 
36709ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
36719ce8ade0SThomas Graf {
3672612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
36739ce8ade0SThomas Graf }
36749ce8ade0SThomas Graf 
3675ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
36761da177e4SLinus Torvalds {
3677adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3678612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
36791da177e4SLinus Torvalds }
36801da177e4SLinus Torvalds 
36819ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
36829ce8ade0SThomas Graf {
3683612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
36849ce8ade0SThomas Graf }
36859ce8ade0SThomas Graf 
3686ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
36879ce8ade0SThomas Graf {
3688adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3689612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
36909ce8ade0SThomas Graf }
36919ce8ade0SThomas Graf 
36921da177e4SLinus Torvalds /*
36931da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
36941da177e4SLinus Torvalds  */
36951da177e4SLinus Torvalds 
3696360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net,
3697afb1d4b5SDavid Ahern 				     struct inet6_dev *idev,
36981da177e4SLinus Torvalds 				     const struct in6_addr *addr,
3699acb54e3cSDavid Ahern 				     bool anycast, gfp_t gfp_flags)
37001da177e4SLinus Torvalds {
3701ca254490SDavid Ahern 	u32 tb_id;
37024832c30dSDavid Ahern 	struct net_device *dev = idev->dev;
3703360a9887SDavid Ahern 	struct fib6_info *f6i;
37045f02ce24SDavid Ahern 
3705360a9887SDavid Ahern 	f6i = fib6_info_alloc(gfp_flags);
3706360a9887SDavid Ahern 	if (!f6i)
37071da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
37081da177e4SLinus Torvalds 
3709767a2217SDavid Ahern 	f6i->fib6_metrics = ip_fib_metrics_init(net, NULL, 0);
3710360a9887SDavid Ahern 	f6i->dst_nocount = true;
3711360a9887SDavid Ahern 	f6i->dst_host = true;
3712360a9887SDavid Ahern 	f6i->fib6_protocol = RTPROT_KERNEL;
3713360a9887SDavid Ahern 	f6i->fib6_flags = RTF_UP | RTF_NONEXTHOP;
3714e8478e80SDavid Ahern 	if (anycast) {
3715360a9887SDavid Ahern 		f6i->fib6_type = RTN_ANYCAST;
3716360a9887SDavid Ahern 		f6i->fib6_flags |= RTF_ANYCAST;
3717e8478e80SDavid Ahern 	} else {
3718360a9887SDavid Ahern 		f6i->fib6_type = RTN_LOCAL;
3719360a9887SDavid Ahern 		f6i->fib6_flags |= RTF_LOCAL;
3720e8478e80SDavid Ahern 	}
37211da177e4SLinus Torvalds 
3722360a9887SDavid Ahern 	f6i->fib6_nh.nh_gw = *addr;
372393531c67SDavid Ahern 	dev_hold(dev);
3724360a9887SDavid Ahern 	f6i->fib6_nh.nh_dev = dev;
3725360a9887SDavid Ahern 	f6i->fib6_dst.addr = *addr;
3726360a9887SDavid Ahern 	f6i->fib6_dst.plen = 128;
3727ca254490SDavid Ahern 	tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
3728360a9887SDavid Ahern 	f6i->fib6_table = fib6_get_table(net, tb_id);
37291da177e4SLinus Torvalds 
3730360a9887SDavid Ahern 	return f6i;
37311da177e4SLinus Torvalds }
37321da177e4SLinus Torvalds 
3733c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
3734c3968a85SDaniel Walter struct arg_dev_net_ip {
3735c3968a85SDaniel Walter 	struct net_device *dev;
3736c3968a85SDaniel Walter 	struct net *net;
3737c3968a85SDaniel Walter 	struct in6_addr *addr;
3738c3968a85SDaniel Walter };
3739c3968a85SDaniel Walter 
37408d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
3741c3968a85SDaniel Walter {
3742c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3743c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3744c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3745c3968a85SDaniel Walter 
37465e670d84SDavid Ahern 	if (((void *)rt->fib6_nh.nh_dev == dev || !dev) &&
3747421842edSDavid Ahern 	    rt != net->ipv6.fib6_null_entry &&
374893c2fb25SDavid Ahern 	    ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
374960006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
3750c3968a85SDaniel Walter 		/* remove prefsrc entry */
375193c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
375260006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
3753c3968a85SDaniel Walter 	}
3754c3968a85SDaniel Walter 	return 0;
3755c3968a85SDaniel Walter }
3756c3968a85SDaniel Walter 
3757c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3758c3968a85SDaniel Walter {
3759c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
3760c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
3761c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
3762c3968a85SDaniel Walter 		.net = net,
3763c3968a85SDaniel Walter 		.addr = &ifp->addr,
3764c3968a85SDaniel Walter 	};
37650c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
3766c3968a85SDaniel Walter }
3767c3968a85SDaniel Walter 
3768be7a010dSDuan Jiong #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
3769be7a010dSDuan Jiong 
3770be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
37718d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
3772be7a010dSDuan Jiong {
3773be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
3774be7a010dSDuan Jiong 
377593c2fb25SDavid Ahern 	if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
37765e670d84SDavid Ahern 	    ipv6_addr_equal(gateway, &rt->fib6_nh.nh_gw)) {
3777be7a010dSDuan Jiong 		return -1;
3778be7a010dSDuan Jiong 	}
3779b16cb459SWei Wang 
3780b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
3781b16cb459SWei Wang 	 * This is needed because cached route may have a different
3782b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
3783b16cb459SWei Wang 	 */
3784b16cb459SWei Wang 	rt6_exceptions_clean_tohost(rt, gateway);
3785b16cb459SWei Wang 
3786be7a010dSDuan Jiong 	return 0;
3787be7a010dSDuan Jiong }
3788be7a010dSDuan Jiong 
3789be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3790be7a010dSDuan Jiong {
3791be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
3792be7a010dSDuan Jiong }
3793be7a010dSDuan Jiong 
37942127d95aSIdo Schimmel struct arg_netdev_event {
37952127d95aSIdo Schimmel 	const struct net_device *dev;
37964c981e28SIdo Schimmel 	union {
37972127d95aSIdo Schimmel 		unsigned int nh_flags;
37984c981e28SIdo Schimmel 		unsigned long event;
37994c981e28SIdo Schimmel 	};
38002127d95aSIdo Schimmel };
38012127d95aSIdo Schimmel 
38028d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
3803d7dedee1SIdo Schimmel {
38048d1c802bSDavid Ahern 	struct fib6_info *iter;
3805d7dedee1SIdo Schimmel 	struct fib6_node *fn;
3806d7dedee1SIdo Schimmel 
380793c2fb25SDavid Ahern 	fn = rcu_dereference_protected(rt->fib6_node,
380893c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3809d7dedee1SIdo Schimmel 	iter = rcu_dereference_protected(fn->leaf,
381093c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3811d7dedee1SIdo Schimmel 	while (iter) {
381293c2fb25SDavid Ahern 		if (iter->fib6_metric == rt->fib6_metric &&
381333bd5ac5SDavid Ahern 		    rt6_qualify_for_ecmp(iter))
3814d7dedee1SIdo Schimmel 			return iter;
38158fb11a9aSDavid Ahern 		iter = rcu_dereference_protected(iter->fib6_next,
381693c2fb25SDavid Ahern 				lockdep_is_held(&rt->fib6_table->tb6_lock));
3817d7dedee1SIdo Schimmel 	}
3818d7dedee1SIdo Schimmel 
3819d7dedee1SIdo Schimmel 	return NULL;
3820d7dedee1SIdo Schimmel }
3821d7dedee1SIdo Schimmel 
38228d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt)
3823d7dedee1SIdo Schimmel {
38245e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_DEAD ||
38255e670d84SDavid Ahern 	    (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
3826dcd1f572SDavid Ahern 	     fib6_ignore_linkdown(rt)))
3827d7dedee1SIdo Schimmel 		return true;
3828d7dedee1SIdo Schimmel 
3829d7dedee1SIdo Schimmel 	return false;
3830d7dedee1SIdo Schimmel }
3831d7dedee1SIdo Schimmel 
38328d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt)
3833d7dedee1SIdo Schimmel {
38348d1c802bSDavid Ahern 	struct fib6_info *iter;
3835d7dedee1SIdo Schimmel 	int total = 0;
3836d7dedee1SIdo Schimmel 
3837d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt))
38385e670d84SDavid Ahern 		total += rt->fib6_nh.nh_weight;
3839d7dedee1SIdo Schimmel 
384093c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
3841d7dedee1SIdo Schimmel 		if (!rt6_is_dead(iter))
38425e670d84SDavid Ahern 			total += iter->fib6_nh.nh_weight;
3843d7dedee1SIdo Schimmel 	}
3844d7dedee1SIdo Schimmel 
3845d7dedee1SIdo Schimmel 	return total;
3846d7dedee1SIdo Schimmel }
3847d7dedee1SIdo Schimmel 
38488d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
3849d7dedee1SIdo Schimmel {
3850d7dedee1SIdo Schimmel 	int upper_bound = -1;
3851d7dedee1SIdo Schimmel 
3852d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt)) {
38535e670d84SDavid Ahern 		*weight += rt->fib6_nh.nh_weight;
3854d7dedee1SIdo Schimmel 		upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
3855d7dedee1SIdo Schimmel 						    total) - 1;
3856d7dedee1SIdo Schimmel 	}
38575e670d84SDavid Ahern 	atomic_set(&rt->fib6_nh.nh_upper_bound, upper_bound);
3858d7dedee1SIdo Schimmel }
3859d7dedee1SIdo Schimmel 
38608d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
3861d7dedee1SIdo Schimmel {
38628d1c802bSDavid Ahern 	struct fib6_info *iter;
3863d7dedee1SIdo Schimmel 	int weight = 0;
3864d7dedee1SIdo Schimmel 
3865d7dedee1SIdo Schimmel 	rt6_upper_bound_set(rt, &weight, total);
3866d7dedee1SIdo Schimmel 
386793c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
3868d7dedee1SIdo Schimmel 		rt6_upper_bound_set(iter, &weight, total);
3869d7dedee1SIdo Schimmel }
3870d7dedee1SIdo Schimmel 
38718d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt)
3872d7dedee1SIdo Schimmel {
38738d1c802bSDavid Ahern 	struct fib6_info *first;
3874d7dedee1SIdo Schimmel 	int total;
3875d7dedee1SIdo Schimmel 
3876d7dedee1SIdo Schimmel 	/* In case the entire multipath route was marked for flushing,
3877d7dedee1SIdo Schimmel 	 * then there is no need to rebalance upon the removal of every
3878d7dedee1SIdo Schimmel 	 * sibling route.
3879d7dedee1SIdo Schimmel 	 */
388093c2fb25SDavid Ahern 	if (!rt->fib6_nsiblings || rt->should_flush)
3881d7dedee1SIdo Schimmel 		return;
3882d7dedee1SIdo Schimmel 
3883d7dedee1SIdo Schimmel 	/* During lookup routes are evaluated in order, so we need to
3884d7dedee1SIdo Schimmel 	 * make sure upper bounds are assigned from the first sibling
3885d7dedee1SIdo Schimmel 	 * onwards.
3886d7dedee1SIdo Schimmel 	 */
3887d7dedee1SIdo Schimmel 	first = rt6_multipath_first_sibling(rt);
3888d7dedee1SIdo Schimmel 	if (WARN_ON_ONCE(!first))
3889d7dedee1SIdo Schimmel 		return;
3890d7dedee1SIdo Schimmel 
3891d7dedee1SIdo Schimmel 	total = rt6_multipath_total_weight(first);
3892d7dedee1SIdo Schimmel 	rt6_multipath_upper_bound_set(first, total);
3893d7dedee1SIdo Schimmel }
3894d7dedee1SIdo Schimmel 
38958d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg)
38962127d95aSIdo Schimmel {
38972127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
38987aef6859SDavid Ahern 	struct net *net = dev_net(arg->dev);
38992127d95aSIdo Schimmel 
3900421842edSDavid Ahern 	if (rt != net->ipv6.fib6_null_entry && rt->fib6_nh.nh_dev == arg->dev) {
39015e670d84SDavid Ahern 		rt->fib6_nh.nh_flags &= ~arg->nh_flags;
39027aef6859SDavid Ahern 		fib6_update_sernum_upto_root(net, rt);
3903d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
39041de178edSIdo Schimmel 	}
39052127d95aSIdo Schimmel 
39062127d95aSIdo Schimmel 	return 0;
39072127d95aSIdo Schimmel }
39082127d95aSIdo Schimmel 
39092127d95aSIdo Schimmel void rt6_sync_up(struct net_device *dev, unsigned int nh_flags)
39102127d95aSIdo Schimmel {
39112127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
39122127d95aSIdo Schimmel 		.dev = dev,
39136802f3adSIdo Schimmel 		{
39142127d95aSIdo Schimmel 			.nh_flags = nh_flags,
39156802f3adSIdo Schimmel 		},
39162127d95aSIdo Schimmel 	};
39172127d95aSIdo Schimmel 
39182127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
39192127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
39202127d95aSIdo Schimmel 
39212127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
39222127d95aSIdo Schimmel }
39232127d95aSIdo Schimmel 
39248d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
39251de178edSIdo Schimmel 				   const struct net_device *dev)
39261de178edSIdo Schimmel {
39278d1c802bSDavid Ahern 	struct fib6_info *iter;
39281de178edSIdo Schimmel 
39295e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == dev)
39301de178edSIdo Schimmel 		return true;
393193c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
39325e670d84SDavid Ahern 		if (iter->fib6_nh.nh_dev == dev)
39331de178edSIdo Schimmel 			return true;
39341de178edSIdo Schimmel 
39351de178edSIdo Schimmel 	return false;
39361de178edSIdo Schimmel }
39371de178edSIdo Schimmel 
39388d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt)
39391de178edSIdo Schimmel {
39408d1c802bSDavid Ahern 	struct fib6_info *iter;
39411de178edSIdo Schimmel 
39421de178edSIdo Schimmel 	rt->should_flush = 1;
394393c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
39441de178edSIdo Schimmel 		iter->should_flush = 1;
39451de178edSIdo Schimmel }
39461de178edSIdo Schimmel 
39478d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
39481de178edSIdo Schimmel 					     const struct net_device *down_dev)
39491de178edSIdo Schimmel {
39508d1c802bSDavid Ahern 	struct fib6_info *iter;
39511de178edSIdo Schimmel 	unsigned int dead = 0;
39521de178edSIdo Schimmel 
39535e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == down_dev ||
39545e670d84SDavid Ahern 	    rt->fib6_nh.nh_flags & RTNH_F_DEAD)
39551de178edSIdo Schimmel 		dead++;
395693c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
39575e670d84SDavid Ahern 		if (iter->fib6_nh.nh_dev == down_dev ||
39585e670d84SDavid Ahern 		    iter->fib6_nh.nh_flags & RTNH_F_DEAD)
39591de178edSIdo Schimmel 			dead++;
39601de178edSIdo Schimmel 
39611de178edSIdo Schimmel 	return dead;
39621de178edSIdo Schimmel }
39631de178edSIdo Schimmel 
39648d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
39651de178edSIdo Schimmel 				       const struct net_device *dev,
39661de178edSIdo Schimmel 				       unsigned int nh_flags)
39671de178edSIdo Schimmel {
39688d1c802bSDavid Ahern 	struct fib6_info *iter;
39691de178edSIdo Schimmel 
39705e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == dev)
39715e670d84SDavid Ahern 		rt->fib6_nh.nh_flags |= nh_flags;
397293c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
39735e670d84SDavid Ahern 		if (iter->fib6_nh.nh_dev == dev)
39745e670d84SDavid Ahern 			iter->fib6_nh.nh_flags |= nh_flags;
39751de178edSIdo Schimmel }
39761de178edSIdo Schimmel 
3977a1a22c12SDavid Ahern /* called with write lock held for table with rt */
39788d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
39791da177e4SLinus Torvalds {
39804c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
39814c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
39827aef6859SDavid Ahern 	struct net *net = dev_net(dev);
39838ed67789SDaniel Lezcano 
3984421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
398527c6fa73SIdo Schimmel 		return 0;
398627c6fa73SIdo Schimmel 
398727c6fa73SIdo Schimmel 	switch (arg->event) {
398827c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
39895e670d84SDavid Ahern 		return rt->fib6_nh.nh_dev == dev ? -1 : 0;
399027c6fa73SIdo Schimmel 	case NETDEV_DOWN:
39911de178edSIdo Schimmel 		if (rt->should_flush)
399227c6fa73SIdo Schimmel 			return -1;
399393c2fb25SDavid Ahern 		if (!rt->fib6_nsiblings)
39945e670d84SDavid Ahern 			return rt->fib6_nh.nh_dev == dev ? -1 : 0;
39951de178edSIdo Schimmel 		if (rt6_multipath_uses_dev(rt, dev)) {
39961de178edSIdo Schimmel 			unsigned int count;
39971de178edSIdo Schimmel 
39981de178edSIdo Schimmel 			count = rt6_multipath_dead_count(rt, dev);
399993c2fb25SDavid Ahern 			if (rt->fib6_nsiblings + 1 == count) {
40001de178edSIdo Schimmel 				rt6_multipath_flush(rt);
40011de178edSIdo Schimmel 				return -1;
40021de178edSIdo Schimmel 			}
40031de178edSIdo Schimmel 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
40041de178edSIdo Schimmel 						   RTNH_F_LINKDOWN);
40057aef6859SDavid Ahern 			fib6_update_sernum(net, rt);
4006d7dedee1SIdo Schimmel 			rt6_multipath_rebalance(rt);
40071de178edSIdo Schimmel 		}
40081de178edSIdo Schimmel 		return -2;
400927c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
40105e670d84SDavid Ahern 		if (rt->fib6_nh.nh_dev != dev ||
401193c2fb25SDavid Ahern 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
401227c6fa73SIdo Schimmel 			break;
40135e670d84SDavid Ahern 		rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
4014d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
401527c6fa73SIdo Schimmel 		break;
40162b241361SIdo Schimmel 	}
4017c159d30cSDavid S. Miller 
40181da177e4SLinus Torvalds 	return 0;
40191da177e4SLinus Torvalds }
40201da177e4SLinus Torvalds 
402127c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
40221da177e4SLinus Torvalds {
40234c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
40248ed67789SDaniel Lezcano 		.dev = dev,
40256802f3adSIdo Schimmel 		{
40264c981e28SIdo Schimmel 			.event = event,
40276802f3adSIdo Schimmel 		},
40288ed67789SDaniel Lezcano 	};
4029*7c6bb7d2SDavid Ahern 	struct net *net = dev_net(dev);
40308ed67789SDaniel Lezcano 
4031*7c6bb7d2SDavid Ahern 	if (net->ipv6.sysctl.skip_notify_on_dev_down)
4032*7c6bb7d2SDavid Ahern 		fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
4033*7c6bb7d2SDavid Ahern 	else
4034*7c6bb7d2SDavid Ahern 		fib6_clean_all(net, fib6_ifdown, &arg);
40354c981e28SIdo Schimmel }
40364c981e28SIdo Schimmel 
40374c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
40384c981e28SIdo Schimmel {
40394c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
40404c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
40414c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
40421da177e4SLinus Torvalds }
40431da177e4SLinus Torvalds 
404495c96174SEric Dumazet struct rt6_mtu_change_arg {
40451da177e4SLinus Torvalds 	struct net_device *dev;
404695c96174SEric Dumazet 	unsigned int mtu;
40471da177e4SLinus Torvalds };
40481da177e4SLinus Torvalds 
40498d1c802bSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg)
40501da177e4SLinus Torvalds {
40511da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
40521da177e4SLinus Torvalds 	struct inet6_dev *idev;
40531da177e4SLinus Torvalds 
40541da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
40551da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
40561da177e4SLinus Torvalds 	   We still use this lock to block changes
40571da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
40581da177e4SLinus Torvalds 	*/
40591da177e4SLinus Torvalds 
40601da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
406138308473SDavid S. Miller 	if (!idev)
40621da177e4SLinus Torvalds 		return 0;
40631da177e4SLinus Torvalds 
40641da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
40651da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
40661da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
40671da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
40681da177e4SLinus Torvalds 	 */
40695e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == arg->dev &&
4070d4ead6b3SDavid Ahern 	    !fib6_metric_locked(rt, RTAX_MTU)) {
4071d4ead6b3SDavid Ahern 		u32 mtu = rt->fib6_pmtu;
4072d4ead6b3SDavid Ahern 
4073d4ead6b3SDavid Ahern 		if (mtu >= arg->mtu ||
4074d4ead6b3SDavid Ahern 		    (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4075d4ead6b3SDavid Ahern 			fib6_metric_set(rt, RTAX_MTU, arg->mtu);
4076d4ead6b3SDavid Ahern 
4077f5bbe7eeSWei Wang 		spin_lock_bh(&rt6_exception_lock);
4078e9fa1495SStefano Brivio 		rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
4079f5bbe7eeSWei Wang 		spin_unlock_bh(&rt6_exception_lock);
40804b32b5adSMartin KaFai Lau 	}
40811da177e4SLinus Torvalds 	return 0;
40821da177e4SLinus Torvalds }
40831da177e4SLinus Torvalds 
408495c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
40851da177e4SLinus Torvalds {
4086c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
4087c71099acSThomas Graf 		.dev = dev,
4088c71099acSThomas Graf 		.mtu = mtu,
4089c71099acSThomas Graf 	};
40901da177e4SLinus Torvalds 
40910c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
40921da177e4SLinus Torvalds }
40931da177e4SLinus Torvalds 
4094ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
40955176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
4096aa8f8778SEric Dumazet 	[RTA_PREFSRC]		= { .len = sizeof(struct in6_addr) },
409786872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
4098ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
409986872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
410086872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
410151ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
4102c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
410319e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
410419e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
410532bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
4106622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
41073b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
4108aa8f8778SEric Dumazet 	[RTA_TABLE]		= { .type = NLA_U32 },
4109eacb9384SRoopa Prabhu 	[RTA_IP_PROTO]		= { .type = NLA_U8 },
4110eacb9384SRoopa Prabhu 	[RTA_SPORT]		= { .type = NLA_U16 },
4111eacb9384SRoopa Prabhu 	[RTA_DPORT]		= { .type = NLA_U16 },
411286872cb5SThomas Graf };
411386872cb5SThomas Graf 
411486872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4115333c4301SDavid Ahern 			      struct fib6_config *cfg,
4116333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
41171da177e4SLinus Torvalds {
411886872cb5SThomas Graf 	struct rtmsg *rtm;
411986872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
4120c78ba6d6SLubomir Rintel 	unsigned int pref;
412186872cb5SThomas Graf 	int err;
41221da177e4SLinus Torvalds 
4123fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4124dac9c979SDavid Ahern 			  extack);
412586872cb5SThomas Graf 	if (err < 0)
412686872cb5SThomas Graf 		goto errout;
41271da177e4SLinus Torvalds 
412886872cb5SThomas Graf 	err = -EINVAL;
412986872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
413086872cb5SThomas Graf 
413184db8407SMaciej Żenczykowski 	*cfg = (struct fib6_config){
413284db8407SMaciej Żenczykowski 		.fc_table = rtm->rtm_table,
413384db8407SMaciej Żenczykowski 		.fc_dst_len = rtm->rtm_dst_len,
413484db8407SMaciej Żenczykowski 		.fc_src_len = rtm->rtm_src_len,
413584db8407SMaciej Żenczykowski 		.fc_flags = RTF_UP,
413684db8407SMaciej Żenczykowski 		.fc_protocol = rtm->rtm_protocol,
413784db8407SMaciej Żenczykowski 		.fc_type = rtm->rtm_type,
413884db8407SMaciej Żenczykowski 
413984db8407SMaciej Żenczykowski 		.fc_nlinfo.portid = NETLINK_CB(skb).portid,
414084db8407SMaciej Żenczykowski 		.fc_nlinfo.nlh = nlh,
414184db8407SMaciej Żenczykowski 		.fc_nlinfo.nl_net = sock_net(skb->sk),
414284db8407SMaciej Żenczykowski 	};
414386872cb5SThomas Graf 
4144ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
4145ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
4146b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
4147b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
414886872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
414986872cb5SThomas Graf 
4150ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
4151ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
4152ab79ad14SMaciej Żenczykowski 
41531f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
41541f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
41551f56a01fSMartin KaFai Lau 
4156fc1e64e1SDavid Ahern 	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4157fc1e64e1SDavid Ahern 
415886872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
415967b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
416086872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
41611da177e4SLinus Torvalds 	}
416286872cb5SThomas Graf 
416386872cb5SThomas Graf 	if (tb[RTA_DST]) {
416486872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
416586872cb5SThomas Graf 
416686872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
416786872cb5SThomas Graf 			goto errout;
416886872cb5SThomas Graf 
416986872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
41701da177e4SLinus Torvalds 	}
417186872cb5SThomas Graf 
417286872cb5SThomas Graf 	if (tb[RTA_SRC]) {
417386872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
417486872cb5SThomas Graf 
417586872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
417686872cb5SThomas Graf 			goto errout;
417786872cb5SThomas Graf 
417886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
41791da177e4SLinus Torvalds 	}
418086872cb5SThomas Graf 
4181c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
418267b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4183c3968a85SDaniel Walter 
418486872cb5SThomas Graf 	if (tb[RTA_OIF])
418586872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
418686872cb5SThomas Graf 
418786872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
418886872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
418986872cb5SThomas Graf 
419086872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
419186872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
419286872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
41931da177e4SLinus Torvalds 	}
419486872cb5SThomas Graf 
419586872cb5SThomas Graf 	if (tb[RTA_TABLE])
419686872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
419786872cb5SThomas Graf 
419851ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
419951ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
420051ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
42019ed59592SDavid Ahern 
42029ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4203c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
42049ed59592SDavid Ahern 		if (err < 0)
42059ed59592SDavid Ahern 			goto errout;
420651ebd318SNicolas Dichtel 	}
420751ebd318SNicolas Dichtel 
4208c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
4209c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
4210c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
4211c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
4212c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
4213c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
4214c78ba6d6SLubomir Rintel 	}
4215c78ba6d6SLubomir Rintel 
421619e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
421719e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
421819e42e45SRoopa Prabhu 
42199ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
422019e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
422119e42e45SRoopa Prabhu 
4222c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
42239ed59592SDavid Ahern 		if (err < 0)
42249ed59592SDavid Ahern 			goto errout;
42259ed59592SDavid Ahern 	}
42269ed59592SDavid Ahern 
422732bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
422832bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
422932bc201eSXin Long 
423032bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
423132bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
423232bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
423332bc201eSXin Long 		}
423432bc201eSXin Long 	}
423532bc201eSXin Long 
423686872cb5SThomas Graf 	err = 0;
423786872cb5SThomas Graf errout:
423886872cb5SThomas Graf 	return err;
42391da177e4SLinus Torvalds }
42401da177e4SLinus Torvalds 
42416b9ea5a6SRoopa Prabhu struct rt6_nh {
42428d1c802bSDavid Ahern 	struct fib6_info *fib6_info;
42436b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
42446b9ea5a6SRoopa Prabhu 	struct list_head next;
42456b9ea5a6SRoopa Prabhu };
42466b9ea5a6SRoopa Prabhu 
42476b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
42486b9ea5a6SRoopa Prabhu {
42496b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
42506b9ea5a6SRoopa Prabhu 
42516b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
42527d4d5065SDavid Ahern 		pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n",
42536b9ea5a6SRoopa Prabhu 		        &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
42546b9ea5a6SRoopa Prabhu 		        nh->r_cfg.fc_ifindex);
42556b9ea5a6SRoopa Prabhu 	}
42566b9ea5a6SRoopa Prabhu }
42576b9ea5a6SRoopa Prabhu 
4258d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net,
4259d4ead6b3SDavid Ahern 				 struct list_head *rt6_nh_list,
42608d1c802bSDavid Ahern 				 struct fib6_info *rt,
42618d1c802bSDavid Ahern 				 struct fib6_config *r_cfg)
42626b9ea5a6SRoopa Prabhu {
42636b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
42646b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
42656b9ea5a6SRoopa Prabhu 
42666b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
42678d1c802bSDavid Ahern 		/* check if fib6_info already exists */
42688d1c802bSDavid Ahern 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
42696b9ea5a6SRoopa Prabhu 			return err;
42706b9ea5a6SRoopa Prabhu 	}
42716b9ea5a6SRoopa Prabhu 
42726b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
42736b9ea5a6SRoopa Prabhu 	if (!nh)
42746b9ea5a6SRoopa Prabhu 		return -ENOMEM;
42758d1c802bSDavid Ahern 	nh->fib6_info = rt;
42766b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
42776b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
42786b9ea5a6SRoopa Prabhu 
42796b9ea5a6SRoopa Prabhu 	return 0;
42806b9ea5a6SRoopa Prabhu }
42816b9ea5a6SRoopa Prabhu 
42828d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt,
42838d1c802bSDavid Ahern 				   struct fib6_info *rt_last,
42843b1137feSDavid Ahern 				   struct nl_info *info,
42853b1137feSDavid Ahern 				   __u16 nlflags)
42863b1137feSDavid Ahern {
42873b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
42883b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
42893b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
42903b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
42913b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
42923b1137feSDavid Ahern 	 */
429393c2fb25SDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
429493c2fb25SDavid Ahern 		rt = list_first_entry(&rt_last->fib6_siblings,
42958d1c802bSDavid Ahern 				      struct fib6_info,
429693c2fb25SDavid Ahern 				      fib6_siblings);
42973b1137feSDavid Ahern 	}
42983b1137feSDavid Ahern 
42993b1137feSDavid Ahern 	if (rt)
43003b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
43013b1137feSDavid Ahern }
43023b1137feSDavid Ahern 
4303333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
4304333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
430551ebd318SNicolas Dichtel {
43068d1c802bSDavid Ahern 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
43073b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
430851ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
430951ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
43108d1c802bSDavid Ahern 	struct fib6_info *rt;
43116b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
43126b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
43133b1137feSDavid Ahern 	__u16 nlflags;
431451ebd318SNicolas Dichtel 	int remaining;
431551ebd318SNicolas Dichtel 	int attrlen;
43166b9ea5a6SRoopa Prabhu 	int err = 1;
43176b9ea5a6SRoopa Prabhu 	int nhn = 0;
43186b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
43196b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
43206b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
432151ebd318SNicolas Dichtel 
43223b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
43233b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
43243b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
43253b1137feSDavid Ahern 
432635f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
432751ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
432851ebd318SNicolas Dichtel 
43296b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
43308d1c802bSDavid Ahern 	 * fib6_info structs per nexthop
43316b9ea5a6SRoopa Prabhu 	 */
433251ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
433351ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
433451ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
433551ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
433651ebd318SNicolas Dichtel 
433751ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
433851ebd318SNicolas Dichtel 		if (attrlen > 0) {
433951ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
434051ebd318SNicolas Dichtel 
434151ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
434251ebd318SNicolas Dichtel 			if (nla) {
434367b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
434451ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
434551ebd318SNicolas Dichtel 			}
434619e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
434719e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
434819e42e45SRoopa Prabhu 			if (nla)
434919e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
435051ebd318SNicolas Dichtel 		}
43516b9ea5a6SRoopa Prabhu 
435268e2ffdeSDavid Ahern 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
4353acb54e3cSDavid Ahern 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
43548c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
43558c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
43568c5b83f0SRoopa Prabhu 			rt = NULL;
43576b9ea5a6SRoopa Prabhu 			goto cleanup;
43588c5b83f0SRoopa Prabhu 		}
4359b5d2d75eSDavid Ahern 		if (!rt6_qualify_for_ecmp(rt)) {
4360b5d2d75eSDavid Ahern 			err = -EINVAL;
4361b5d2d75eSDavid Ahern 			NL_SET_ERR_MSG(extack,
4362b5d2d75eSDavid Ahern 				       "Device only routes can not be added for IPv6 using the multipath API.");
4363b5d2d75eSDavid Ahern 			fib6_info_release(rt);
4364b5d2d75eSDavid Ahern 			goto cleanup;
4365b5d2d75eSDavid Ahern 		}
43666b9ea5a6SRoopa Prabhu 
43675e670d84SDavid Ahern 		rt->fib6_nh.nh_weight = rtnh->rtnh_hops + 1;
4368398958aeSIdo Schimmel 
4369d4ead6b3SDavid Ahern 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4370d4ead6b3SDavid Ahern 					    rt, &r_cfg);
437151ebd318SNicolas Dichtel 		if (err) {
437293531c67SDavid Ahern 			fib6_info_release(rt);
43736b9ea5a6SRoopa Prabhu 			goto cleanup;
437451ebd318SNicolas Dichtel 		}
43756b9ea5a6SRoopa Prabhu 
43766b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
437751ebd318SNicolas Dichtel 	}
43786b9ea5a6SRoopa Prabhu 
43793b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
43803b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
43813b1137feSDavid Ahern 	 * the full route when done
43823b1137feSDavid Ahern 	 */
43833b1137feSDavid Ahern 	info->skip_notify = 1;
43843b1137feSDavid Ahern 
43856b9ea5a6SRoopa Prabhu 	err_nh = NULL;
43866b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
43878d1c802bSDavid Ahern 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
43888d1c802bSDavid Ahern 		fib6_info_release(nh->fib6_info);
43893b1137feSDavid Ahern 
4390f7225172SDavid Ahern 		if (!err) {
4391f7225172SDavid Ahern 			/* save reference to last route successfully inserted */
4392f7225172SDavid Ahern 			rt_last = nh->fib6_info;
4393f7225172SDavid Ahern 
43946b9ea5a6SRoopa Prabhu 			/* save reference to first route for notification */
4395f7225172SDavid Ahern 			if (!rt_notif)
43968d1c802bSDavid Ahern 				rt_notif = nh->fib6_info;
4397f7225172SDavid Ahern 		}
43986b9ea5a6SRoopa Prabhu 
43998d1c802bSDavid Ahern 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
44008d1c802bSDavid Ahern 		nh->fib6_info = NULL;
44016b9ea5a6SRoopa Prabhu 		if (err) {
44026b9ea5a6SRoopa Prabhu 			if (replace && nhn)
44036b9ea5a6SRoopa Prabhu 				ip6_print_replace_route_err(&rt6_nh_list);
44046b9ea5a6SRoopa Prabhu 			err_nh = nh;
44056b9ea5a6SRoopa Prabhu 			goto add_errout;
44066b9ea5a6SRoopa Prabhu 		}
44076b9ea5a6SRoopa Prabhu 
44081a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
440927596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
441027596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
441127596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
441227596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
441327596472SMichal Kubeček 		 * be added to it.
44141a72418bSNicolas Dichtel 		 */
441527596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
441627596472SMichal Kubeček 						     NLM_F_REPLACE);
44176b9ea5a6SRoopa Prabhu 		nhn++;
44186b9ea5a6SRoopa Prabhu 	}
44196b9ea5a6SRoopa Prabhu 
44203b1137feSDavid Ahern 	/* success ... tell user about new route */
44213b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
44226b9ea5a6SRoopa Prabhu 	goto cleanup;
44236b9ea5a6SRoopa Prabhu 
44246b9ea5a6SRoopa Prabhu add_errout:
44253b1137feSDavid Ahern 	/* send notification for routes that were added so that
44263b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
44273b1137feSDavid Ahern 	 * coherent
44283b1137feSDavid Ahern 	 */
44293b1137feSDavid Ahern 	if (rt_notif)
44303b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
44313b1137feSDavid Ahern 
44326b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
44336b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
44346b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
44356b9ea5a6SRoopa Prabhu 			break;
4436333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
44376b9ea5a6SRoopa Prabhu 	}
44386b9ea5a6SRoopa Prabhu 
44396b9ea5a6SRoopa Prabhu cleanup:
44406b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
44418d1c802bSDavid Ahern 		if (nh->fib6_info)
44428d1c802bSDavid Ahern 			fib6_info_release(nh->fib6_info);
44436b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
44446b9ea5a6SRoopa Prabhu 		kfree(nh);
44456b9ea5a6SRoopa Prabhu 	}
44466b9ea5a6SRoopa Prabhu 
44476b9ea5a6SRoopa Prabhu 	return err;
44486b9ea5a6SRoopa Prabhu }
44496b9ea5a6SRoopa Prabhu 
4450333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
4451333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
44526b9ea5a6SRoopa Prabhu {
44536b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
44546b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
44556b9ea5a6SRoopa Prabhu 	int remaining;
44566b9ea5a6SRoopa Prabhu 	int attrlen;
44576b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
44586b9ea5a6SRoopa Prabhu 
44596b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
44606b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
44616b9ea5a6SRoopa Prabhu 
44626b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
44636b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
44646b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
44656b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
44666b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
44676b9ea5a6SRoopa Prabhu 
44686b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
44696b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
44706b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
44716b9ea5a6SRoopa Prabhu 
44726b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
44736b9ea5a6SRoopa Prabhu 			if (nla) {
44746b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
44756b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
44766b9ea5a6SRoopa Prabhu 			}
44776b9ea5a6SRoopa Prabhu 		}
4478333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
44796b9ea5a6SRoopa Prabhu 		if (err)
44806b9ea5a6SRoopa Prabhu 			last_err = err;
44816b9ea5a6SRoopa Prabhu 
448251ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
448351ebd318SNicolas Dichtel 	}
448451ebd318SNicolas Dichtel 
448551ebd318SNicolas Dichtel 	return last_err;
448651ebd318SNicolas Dichtel }
448751ebd318SNicolas Dichtel 
4488c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4489c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
44901da177e4SLinus Torvalds {
449186872cb5SThomas Graf 	struct fib6_config cfg;
449286872cb5SThomas Graf 	int err;
44931da177e4SLinus Torvalds 
4494333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
449586872cb5SThomas Graf 	if (err < 0)
449686872cb5SThomas Graf 		return err;
449786872cb5SThomas Graf 
449851ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4499333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
45000ae81335SDavid Ahern 	else {
45010ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
4502333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
45031da177e4SLinus Torvalds 	}
45040ae81335SDavid Ahern }
45051da177e4SLinus Torvalds 
4506c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4507c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
45081da177e4SLinus Torvalds {
450986872cb5SThomas Graf 	struct fib6_config cfg;
451086872cb5SThomas Graf 	int err;
45111da177e4SLinus Torvalds 
4512333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
451386872cb5SThomas Graf 	if (err < 0)
451486872cb5SThomas Graf 		return err;
451586872cb5SThomas Graf 
451651ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4517333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
451851ebd318SNicolas Dichtel 	else
4519acb54e3cSDavid Ahern 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
45201da177e4SLinus Torvalds }
45211da177e4SLinus Torvalds 
45228d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt)
4523339bf98fSThomas Graf {
4524beb1afacSDavid Ahern 	int nexthop_len = 0;
4525beb1afacSDavid Ahern 
452693c2fb25SDavid Ahern 	if (rt->fib6_nsiblings) {
4527beb1afacSDavid Ahern 		nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
4528beb1afacSDavid Ahern 			    + NLA_ALIGN(sizeof(struct rtnexthop))
4529beb1afacSDavid Ahern 			    + nla_total_size(16) /* RTA_GATEWAY */
45305e670d84SDavid Ahern 			    + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate);
4531beb1afacSDavid Ahern 
453293c2fb25SDavid Ahern 		nexthop_len *= rt->fib6_nsiblings;
4533beb1afacSDavid Ahern 	}
4534beb1afacSDavid Ahern 
4535339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
4536339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
4537339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
4538339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
4539339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
4540339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
4541339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
4542339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
4543339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
45446a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
4545ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
4546c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
454719e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
45485e670d84SDavid Ahern 	       + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate)
4549beb1afacSDavid Ahern 	       + nexthop_len;
4550beb1afacSDavid Ahern }
4551beb1afacSDavid Ahern 
45528d1c802bSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct fib6_info *rt,
45535be083ceSDavid Ahern 			    unsigned int *flags, bool skip_oif)
4554beb1afacSDavid Ahern {
45555e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
4556f9d882eaSIdo Schimmel 		*flags |= RTNH_F_DEAD;
4557f9d882eaSIdo Schimmel 
45585e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN) {
4559beb1afacSDavid Ahern 		*flags |= RTNH_F_LINKDOWN;
4560dcd1f572SDavid Ahern 
4561dcd1f572SDavid Ahern 		rcu_read_lock();
4562dcd1f572SDavid Ahern 		if (fib6_ignore_linkdown(rt))
4563beb1afacSDavid Ahern 			*flags |= RTNH_F_DEAD;
4564dcd1f572SDavid Ahern 		rcu_read_unlock();
4565beb1afacSDavid Ahern 	}
4566beb1afacSDavid Ahern 
456793c2fb25SDavid Ahern 	if (rt->fib6_flags & RTF_GATEWAY) {
45685e670d84SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->fib6_nh.nh_gw) < 0)
4569beb1afacSDavid Ahern 			goto nla_put_failure;
4570beb1afacSDavid Ahern 	}
4571beb1afacSDavid Ahern 
45725e670d84SDavid Ahern 	*flags |= (rt->fib6_nh.nh_flags & RTNH_F_ONLINK);
45735e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_OFFLOAD)
457461e4d01eSIdo Schimmel 		*flags |= RTNH_F_OFFLOAD;
457561e4d01eSIdo Schimmel 
45765be083ceSDavid Ahern 	/* not needed for multipath encoding b/c it has a rtnexthop struct */
45775e670d84SDavid Ahern 	if (!skip_oif && rt->fib6_nh.nh_dev &&
45785e670d84SDavid Ahern 	    nla_put_u32(skb, RTA_OIF, rt->fib6_nh.nh_dev->ifindex))
4579beb1afacSDavid Ahern 		goto nla_put_failure;
4580beb1afacSDavid Ahern 
45815e670d84SDavid Ahern 	if (rt->fib6_nh.nh_lwtstate &&
45825e670d84SDavid Ahern 	    lwtunnel_fill_encap(skb, rt->fib6_nh.nh_lwtstate) < 0)
4583beb1afacSDavid Ahern 		goto nla_put_failure;
4584beb1afacSDavid Ahern 
4585beb1afacSDavid Ahern 	return 0;
4586beb1afacSDavid Ahern 
4587beb1afacSDavid Ahern nla_put_failure:
4588beb1afacSDavid Ahern 	return -EMSGSIZE;
4589beb1afacSDavid Ahern }
4590beb1afacSDavid Ahern 
45915be083ceSDavid Ahern /* add multipath next hop */
45928d1c802bSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct fib6_info *rt)
4593beb1afacSDavid Ahern {
45945e670d84SDavid Ahern 	const struct net_device *dev = rt->fib6_nh.nh_dev;
4595beb1afacSDavid Ahern 	struct rtnexthop *rtnh;
4596beb1afacSDavid Ahern 	unsigned int flags = 0;
4597beb1afacSDavid Ahern 
4598beb1afacSDavid Ahern 	rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
4599beb1afacSDavid Ahern 	if (!rtnh)
4600beb1afacSDavid Ahern 		goto nla_put_failure;
4601beb1afacSDavid Ahern 
46025e670d84SDavid Ahern 	rtnh->rtnh_hops = rt->fib6_nh.nh_weight - 1;
46035e670d84SDavid Ahern 	rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
4604beb1afacSDavid Ahern 
46055be083ceSDavid Ahern 	if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
4606beb1afacSDavid Ahern 		goto nla_put_failure;
4607beb1afacSDavid Ahern 
4608beb1afacSDavid Ahern 	rtnh->rtnh_flags = flags;
4609beb1afacSDavid Ahern 
4610beb1afacSDavid Ahern 	/* length of rtnetlink header + attributes */
4611beb1afacSDavid Ahern 	rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
4612beb1afacSDavid Ahern 
4613beb1afacSDavid Ahern 	return 0;
4614beb1afacSDavid Ahern 
4615beb1afacSDavid Ahern nla_put_failure:
4616beb1afacSDavid Ahern 	return -EMSGSIZE;
4617339bf98fSThomas Graf }
4618339bf98fSThomas Graf 
4619d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
46208d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
4621d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
462215e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
4623f8cfe2ceSDavid Ahern 			 unsigned int flags)
46241da177e4SLinus Torvalds {
462522d0bd82SXin Long 	struct rt6_info *rt6 = (struct rt6_info *)dst;
462622d0bd82SXin Long 	struct rt6key *rt6_dst, *rt6_src;
462722d0bd82SXin Long 	u32 *pmetrics, table, rt6_flags;
46281da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
462922d0bd82SXin Long 	struct rtmsg *rtm;
4630d4ead6b3SDavid Ahern 	long expires = 0;
46311da177e4SLinus Torvalds 
463215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
463338308473SDavid S. Miller 	if (!nlh)
463426932566SPatrick McHardy 		return -EMSGSIZE;
46352d7202bfSThomas Graf 
463622d0bd82SXin Long 	if (rt6) {
463722d0bd82SXin Long 		rt6_dst = &rt6->rt6i_dst;
463822d0bd82SXin Long 		rt6_src = &rt6->rt6i_src;
463922d0bd82SXin Long 		rt6_flags = rt6->rt6i_flags;
464022d0bd82SXin Long 	} else {
464122d0bd82SXin Long 		rt6_dst = &rt->fib6_dst;
464222d0bd82SXin Long 		rt6_src = &rt->fib6_src;
464322d0bd82SXin Long 		rt6_flags = rt->fib6_flags;
464422d0bd82SXin Long 	}
464522d0bd82SXin Long 
46462d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
46471da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
464822d0bd82SXin Long 	rtm->rtm_dst_len = rt6_dst->plen;
464922d0bd82SXin Long 	rtm->rtm_src_len = rt6_src->plen;
46501da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
465193c2fb25SDavid Ahern 	if (rt->fib6_table)
465293c2fb25SDavid Ahern 		table = rt->fib6_table->tb6_id;
4653c71099acSThomas Graf 	else
46549e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
46559e762a4aSPatrick McHardy 	rtm->rtm_table = table;
4656c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
4657c78679e8SDavid S. Miller 		goto nla_put_failure;
4658e8478e80SDavid Ahern 
4659e8478e80SDavid Ahern 	rtm->rtm_type = rt->fib6_type;
46601da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
46611da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
466293c2fb25SDavid Ahern 	rtm->rtm_protocol = rt->fib6_protocol;
46631da177e4SLinus Torvalds 
466422d0bd82SXin Long 	if (rt6_flags & RTF_CACHE)
46651da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
46661da177e4SLinus Torvalds 
4667d4ead6b3SDavid Ahern 	if (dest) {
4668d4ead6b3SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, dest))
4669c78679e8SDavid S. Miller 			goto nla_put_failure;
46701da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
46711da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
467222d0bd82SXin Long 		if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
4673c78679e8SDavid S. Miller 			goto nla_put_failure;
46741da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
46751da177e4SLinus Torvalds 	if (src) {
4676930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
4677c78679e8SDavid S. Miller 			goto nla_put_failure;
46781da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
4679c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
468022d0bd82SXin Long 		   nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
4681c78679e8SDavid S. Miller 		goto nla_put_failure;
46821da177e4SLinus Torvalds #endif
46837bc570c8SYOSHIFUJI Hideaki 	if (iif) {
46847bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
468522d0bd82SXin Long 		if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
4686fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
46872cf75070SNikolay Aleksandrov 
46887bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
46897bc570c8SYOSHIFUJI Hideaki 				return 0;
4690fd61c6baSDavid Ahern 			if (err < 0)
46917bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
46927bc570c8SYOSHIFUJI Hideaki 		} else
46937bc570c8SYOSHIFUJI Hideaki #endif
4694c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
4695c78679e8SDavid S. Miller 				goto nla_put_failure;
4696d4ead6b3SDavid Ahern 	} else if (dest) {
46971da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
4698d4ead6b3SDavid Ahern 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
4699930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4700c78679e8SDavid S. Miller 			goto nla_put_failure;
4701c3968a85SDaniel Walter 	}
4702c3968a85SDaniel Walter 
470393c2fb25SDavid Ahern 	if (rt->fib6_prefsrc.plen) {
4704c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
470593c2fb25SDavid Ahern 		saddr_buf = rt->fib6_prefsrc.addr;
4706930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4707c78679e8SDavid S. Miller 			goto nla_put_failure;
47081da177e4SLinus Torvalds 	}
47092d7202bfSThomas Graf 
4710d4ead6b3SDavid Ahern 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4711d4ead6b3SDavid Ahern 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
47122d7202bfSThomas Graf 		goto nla_put_failure;
47132d7202bfSThomas Graf 
471493c2fb25SDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
4715beb1afacSDavid Ahern 		goto nla_put_failure;
4716beb1afacSDavid Ahern 
4717beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
4718beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
4719beb1afacSDavid Ahern 	 */
472022d0bd82SXin Long 	if (rt6) {
472122d0bd82SXin Long 		if (rt6_flags & RTF_GATEWAY &&
472222d0bd82SXin Long 		    nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
472322d0bd82SXin Long 			goto nla_put_failure;
472422d0bd82SXin Long 
472522d0bd82SXin Long 		if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
472622d0bd82SXin Long 			goto nla_put_failure;
472722d0bd82SXin Long 	} else if (rt->fib6_nsiblings) {
47288d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
4729beb1afacSDavid Ahern 		struct nlattr *mp;
4730beb1afacSDavid Ahern 
4731beb1afacSDavid Ahern 		mp = nla_nest_start(skb, RTA_MULTIPATH);
4732beb1afacSDavid Ahern 		if (!mp)
4733beb1afacSDavid Ahern 			goto nla_put_failure;
4734beb1afacSDavid Ahern 
4735beb1afacSDavid Ahern 		if (rt6_add_nexthop(skb, rt) < 0)
4736beb1afacSDavid Ahern 			goto nla_put_failure;
4737beb1afacSDavid Ahern 
4738beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
473993c2fb25SDavid Ahern 					 &rt->fib6_siblings, fib6_siblings) {
4740beb1afacSDavid Ahern 			if (rt6_add_nexthop(skb, sibling) < 0)
474194f826b8SEric Dumazet 				goto nla_put_failure;
474294f826b8SEric Dumazet 		}
47432d7202bfSThomas Graf 
4744beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
4745beb1afacSDavid Ahern 	} else {
47465be083ceSDavid Ahern 		if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
4747c78679e8SDavid S. Miller 			goto nla_put_failure;
4748beb1afacSDavid Ahern 	}
47498253947eSLi Wei 
475022d0bd82SXin Long 	if (rt6_flags & RTF_EXPIRES) {
475114895687SDavid Ahern 		expires = dst ? dst->expires : rt->expires;
475214895687SDavid Ahern 		expires -= jiffies;
475314895687SDavid Ahern 	}
475469cdf8f9SYOSHIFUJI Hideaki 
4755d4ead6b3SDavid Ahern 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
4756e3703b3dSThomas Graf 		goto nla_put_failure;
47571da177e4SLinus Torvalds 
475822d0bd82SXin Long 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
4759c78ba6d6SLubomir Rintel 		goto nla_put_failure;
4760c78ba6d6SLubomir Rintel 
476119e42e45SRoopa Prabhu 
4762053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4763053c095aSJohannes Berg 	return 0;
47642d7202bfSThomas Graf 
47652d7202bfSThomas Graf nla_put_failure:
476626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
476726932566SPatrick McHardy 	return -EMSGSIZE;
47681da177e4SLinus Torvalds }
47691da177e4SLinus Torvalds 
47708d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg)
47711da177e4SLinus Torvalds {
47721da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
47731f17e2f2SDavid Ahern 	struct net *net = arg->net;
47741f17e2f2SDavid Ahern 
4775421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
47761f17e2f2SDavid Ahern 		return 0;
47771da177e4SLinus Torvalds 
47782d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
47792d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
4780f8cfe2ceSDavid Ahern 
4781f8cfe2ceSDavid Ahern 		/* user wants prefix routes only */
4782f8cfe2ceSDavid Ahern 		if (rtm->rtm_flags & RTM_F_PREFIX &&
478393c2fb25SDavid Ahern 		    !(rt->fib6_flags & RTF_PREFIX_RT)) {
4784f8cfe2ceSDavid Ahern 			/* success since this is not a prefix route */
4785f8cfe2ceSDavid Ahern 			return 1;
4786f8cfe2ceSDavid Ahern 		}
4787f8cfe2ceSDavid Ahern 	}
47881da177e4SLinus Torvalds 
4789d4ead6b3SDavid Ahern 	return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4790d4ead6b3SDavid Ahern 			     RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
4791d4ead6b3SDavid Ahern 			     arg->cb->nlh->nlmsg_seq, NLM_F_MULTI);
47921da177e4SLinus Torvalds }
47931da177e4SLinus Torvalds 
4794c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4795c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
47961da177e4SLinus Torvalds {
47973b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
4798ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
479918c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
4800a68886a6SDavid Ahern 	struct fib6_info *from;
480118c3a61cSRoopa Prabhu 	struct dst_entry *dst;
48021da177e4SLinus Torvalds 	struct rt6_info *rt;
4803ab364a6fSThomas Graf 	struct sk_buff *skb;
4804ab364a6fSThomas Graf 	struct rtmsg *rtm;
4805744486d4SMaciej Żenczykowski 	struct flowi6 fl6 = {};
480618c3a61cSRoopa Prabhu 	bool fibmatch;
4807ab364a6fSThomas Graf 
4808fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4809c21ef3e3SDavid Ahern 			  extack);
4810ab364a6fSThomas Graf 	if (err < 0)
4811ab364a6fSThomas Graf 		goto errout;
4812ab364a6fSThomas Graf 
4813ab364a6fSThomas Graf 	err = -EINVAL;
481438b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
481538b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
481618c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
4817ab364a6fSThomas Graf 
4818ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
4819ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4820ab364a6fSThomas Graf 			goto errout;
4821ab364a6fSThomas Graf 
48224e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
4823ab364a6fSThomas Graf 	}
4824ab364a6fSThomas Graf 
4825ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
4826ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4827ab364a6fSThomas Graf 			goto errout;
4828ab364a6fSThomas Graf 
48294e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
4830ab364a6fSThomas Graf 	}
4831ab364a6fSThomas Graf 
4832ab364a6fSThomas Graf 	if (tb[RTA_IIF])
4833ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
4834ab364a6fSThomas Graf 
4835ab364a6fSThomas Graf 	if (tb[RTA_OIF])
483672331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
4837ab364a6fSThomas Graf 
48382e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
48392e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
48402e47b291SLorenzo Colitti 
4841622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
4842622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
4843622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
4844622ec2c9SLorenzo Colitti 	else
4845622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4846622ec2c9SLorenzo Colitti 
4847eacb9384SRoopa Prabhu 	if (tb[RTA_SPORT])
4848eacb9384SRoopa Prabhu 		fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
4849eacb9384SRoopa Prabhu 
4850eacb9384SRoopa Prabhu 	if (tb[RTA_DPORT])
4851eacb9384SRoopa Prabhu 		fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
4852eacb9384SRoopa Prabhu 
4853eacb9384SRoopa Prabhu 	if (tb[RTA_IP_PROTO]) {
4854eacb9384SRoopa Prabhu 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
4855eacb9384SRoopa Prabhu 						  &fl6.flowi6_proto, extack);
4856eacb9384SRoopa Prabhu 		if (err)
4857eacb9384SRoopa Prabhu 			goto errout;
4858eacb9384SRoopa Prabhu 	}
4859eacb9384SRoopa Prabhu 
4860ab364a6fSThomas Graf 	if (iif) {
4861ab364a6fSThomas Graf 		struct net_device *dev;
486272331bc0SShmulik Ladkani 		int flags = 0;
486372331bc0SShmulik Ladkani 
4864121622dbSFlorian Westphal 		rcu_read_lock();
4865121622dbSFlorian Westphal 
4866121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
4867ab364a6fSThomas Graf 		if (!dev) {
4868121622dbSFlorian Westphal 			rcu_read_unlock();
4869ab364a6fSThomas Graf 			err = -ENODEV;
4870ab364a6fSThomas Graf 			goto errout;
4871ab364a6fSThomas Graf 		}
487272331bc0SShmulik Ladkani 
487372331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
487472331bc0SShmulik Ladkani 
487572331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
487672331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
487772331bc0SShmulik Ladkani 
4878b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
4879121622dbSFlorian Westphal 
4880121622dbSFlorian Westphal 		rcu_read_unlock();
488172331bc0SShmulik Ladkani 	} else {
488272331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
488372331bc0SShmulik Ladkani 
488418c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
488518c3a61cSRoopa Prabhu 	}
488618c3a61cSRoopa Prabhu 
488718c3a61cSRoopa Prabhu 
488818c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
488918c3a61cSRoopa Prabhu 	if (rt->dst.error) {
489018c3a61cSRoopa Prabhu 		err = rt->dst.error;
489118c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
489218c3a61cSRoopa Prabhu 		goto errout;
4893ab364a6fSThomas Graf 	}
48941da177e4SLinus Torvalds 
48959d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
48969d6acb3bSWANG Cong 		err = rt->dst.error;
48979d6acb3bSWANG Cong 		ip6_rt_put(rt);
48989d6acb3bSWANG Cong 		goto errout;
48999d6acb3bSWANG Cong 	}
49009d6acb3bSWANG Cong 
49011da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
490238308473SDavid S. Miller 	if (!skb) {
490394e187c0SAmerigo Wang 		ip6_rt_put(rt);
4904ab364a6fSThomas Graf 		err = -ENOBUFS;
4905ab364a6fSThomas Graf 		goto errout;
4906ab364a6fSThomas Graf 	}
49071da177e4SLinus Torvalds 
4908d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
4909a68886a6SDavid Ahern 
4910a68886a6SDavid Ahern 	rcu_read_lock();
4911a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
4912a68886a6SDavid Ahern 
491318c3a61cSRoopa Prabhu 	if (fibmatch)
4914a68886a6SDavid Ahern 		err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, iif,
491518c3a61cSRoopa Prabhu 				    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
491618c3a61cSRoopa Prabhu 				    nlh->nlmsg_seq, 0);
491718c3a61cSRoopa Prabhu 	else
4918a68886a6SDavid Ahern 		err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
4919a68886a6SDavid Ahern 				    &fl6.saddr, iif, RTM_NEWROUTE,
4920d4ead6b3SDavid Ahern 				    NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
4921d4ead6b3SDavid Ahern 				    0);
4922a68886a6SDavid Ahern 	rcu_read_unlock();
4923a68886a6SDavid Ahern 
49241da177e4SLinus Torvalds 	if (err < 0) {
4925ab364a6fSThomas Graf 		kfree_skb(skb);
4926ab364a6fSThomas Graf 		goto errout;
49271da177e4SLinus Torvalds 	}
49281da177e4SLinus Torvalds 
492915e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4930ab364a6fSThomas Graf errout:
49311da177e4SLinus Torvalds 	return err;
49321da177e4SLinus Torvalds }
49331da177e4SLinus Torvalds 
49348d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
493537a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
49361da177e4SLinus Torvalds {
49371da177e4SLinus Torvalds 	struct sk_buff *skb;
49385578689aSDaniel Lezcano 	struct net *net = info->nl_net;
4939528c4cebSDenis V. Lunev 	u32 seq;
4940528c4cebSDenis V. Lunev 	int err;
49410d51aa80SJamal Hadi Salim 
4942528c4cebSDenis V. Lunev 	err = -ENOBUFS;
494338308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
494486872cb5SThomas Graf 
494519e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
494638308473SDavid S. Miller 	if (!skb)
494721713ebcSThomas Graf 		goto errout;
49481da177e4SLinus Torvalds 
4949d4ead6b3SDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
4950f8cfe2ceSDavid Ahern 			    event, info->portid, seq, nlm_flags);
495126932566SPatrick McHardy 	if (err < 0) {
495226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
495326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
495426932566SPatrick McHardy 		kfree_skb(skb);
495526932566SPatrick McHardy 		goto errout;
495626932566SPatrick McHardy 	}
495715e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
49585578689aSDaniel Lezcano 		    info->nlh, gfp_any());
49591ce85fe4SPablo Neira Ayuso 	return;
496021713ebcSThomas Graf errout:
496121713ebcSThomas Graf 	if (err < 0)
49625578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
49631da177e4SLinus Torvalds }
49641da177e4SLinus Torvalds 
49658ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
4966351638e7SJiri Pirko 				unsigned long event, void *ptr)
49678ed67789SDaniel Lezcano {
4968351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4969c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
49708ed67789SDaniel Lezcano 
4971242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
4972242d3a49SWANG Cong 		return NOTIFY_OK;
4973242d3a49SWANG Cong 
4974242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
4975421842edSDavid Ahern 		net->ipv6.fib6_null_entry->fib6_nh.nh_dev = dev;
4976d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
49778ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
49788ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
4979d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
49808ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
4981d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
49828ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
49838ed67789SDaniel Lezcano #endif
498476da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
498576da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
498676da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
498776da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
498876da0704SWANG Cong 		 */
498912d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
4990242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
499112d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
499212d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
4993242d3a49SWANG Cong #endif
49948ed67789SDaniel Lezcano 	}
49958ed67789SDaniel Lezcano 
49968ed67789SDaniel Lezcano 	return NOTIFY_OK;
49978ed67789SDaniel Lezcano }
49988ed67789SDaniel Lezcano 
49991da177e4SLinus Torvalds /*
50001da177e4SLinus Torvalds  *	/proc
50011da177e4SLinus Torvalds  */
50021da177e4SLinus Torvalds 
50031da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
50041da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
50051da177e4SLinus Torvalds {
500669ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
50071da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
500869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
500969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
501081eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
501169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
501269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
5013fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
501469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
50151da177e4SLinus Torvalds 
50161da177e4SLinus Torvalds 	return 0;
50171da177e4SLinus Torvalds }
50181da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
50191da177e4SLinus Torvalds 
50201da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
50211da177e4SLinus Torvalds 
50221da177e4SLinus Torvalds static
5023fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
50241da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
50251da177e4SLinus Torvalds {
5026c486da34SLucian Adrian Grijincu 	struct net *net;
5027c486da34SLucian Adrian Grijincu 	int delay;
5028c486da34SLucian Adrian Grijincu 	if (!write)
5029c486da34SLucian Adrian Grijincu 		return -EINVAL;
5030c486da34SLucian Adrian Grijincu 
5031c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
5032c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
50338d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
50342ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
50351da177e4SLinus Torvalds 	return 0;
50361da177e4SLinus Torvalds }
50371da177e4SLinus Torvalds 
5038*7c6bb7d2SDavid Ahern static int zero;
5039*7c6bb7d2SDavid Ahern static int one = 1;
5040*7c6bb7d2SDavid Ahern 
5041ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = {
50421da177e4SLinus Torvalds 	{
50431da177e4SLinus Torvalds 		.procname	=	"flush",
50444990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
50451da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
504689c8b3a1SDave Jones 		.mode		=	0200,
50476d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
50481da177e4SLinus Torvalds 	},
50491da177e4SLinus Torvalds 	{
50501da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
50519a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
50521da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50531da177e4SLinus Torvalds 		.mode		=	0644,
50546d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
50551da177e4SLinus Torvalds 	},
50561da177e4SLinus Torvalds 	{
50571da177e4SLinus Torvalds 		.procname	=	"max_size",
50584990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
50591da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50601da177e4SLinus Torvalds 		.mode		=	0644,
50616d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
50621da177e4SLinus Torvalds 	},
50631da177e4SLinus Torvalds 	{
50641da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
50654990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
50661da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50671da177e4SLinus Torvalds 		.mode		=	0644,
50686d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
50691da177e4SLinus Torvalds 	},
50701da177e4SLinus Torvalds 	{
50711da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
50724990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
50731da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50741da177e4SLinus Torvalds 		.mode		=	0644,
50756d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
50761da177e4SLinus Torvalds 	},
50771da177e4SLinus Torvalds 	{
50781da177e4SLinus Torvalds 		.procname	=	"gc_interval",
50794990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
50801da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50811da177e4SLinus Torvalds 		.mode		=	0644,
50826d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
50831da177e4SLinus Torvalds 	},
50841da177e4SLinus Torvalds 	{
50851da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
50864990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
50871da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50881da177e4SLinus Torvalds 		.mode		=	0644,
5089f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
50901da177e4SLinus Torvalds 	},
50911da177e4SLinus Torvalds 	{
50921da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
50934990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
50941da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50951da177e4SLinus Torvalds 		.mode		=	0644,
50966d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
50971da177e4SLinus Torvalds 	},
50981da177e4SLinus Torvalds 	{
50991da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
51004990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
51011da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51021da177e4SLinus Torvalds 		.mode		=	0644,
5103f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
51041da177e4SLinus Torvalds 	},
51051da177e4SLinus Torvalds 	{
51061da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
51074990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
51081da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51091da177e4SLinus Torvalds 		.mode		=	0644,
51106d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
51111da177e4SLinus Torvalds 	},
5112*7c6bb7d2SDavid Ahern 	{
5113*7c6bb7d2SDavid Ahern 		.procname	=	"skip_notify_on_dev_down",
5114*7c6bb7d2SDavid Ahern 		.data		=	&init_net.ipv6.sysctl.skip_notify_on_dev_down,
5115*7c6bb7d2SDavid Ahern 		.maxlen		=	sizeof(int),
5116*7c6bb7d2SDavid Ahern 		.mode		=	0644,
5117*7c6bb7d2SDavid Ahern 		.proc_handler	=	proc_dointvec,
5118*7c6bb7d2SDavid Ahern 		.extra1		=	&zero,
5119*7c6bb7d2SDavid Ahern 		.extra2		=	&one,
5120*7c6bb7d2SDavid Ahern 	},
5121f8572d8fSEric W. Biederman 	{ }
51221da177e4SLinus Torvalds };
51231da177e4SLinus Torvalds 
51242c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
5125760f2d01SDaniel Lezcano {
5126760f2d01SDaniel Lezcano 	struct ctl_table *table;
5127760f2d01SDaniel Lezcano 
5128760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
5129760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
5130760f2d01SDaniel Lezcano 			GFP_KERNEL);
51315ee09105SYOSHIFUJI Hideaki 
51325ee09105SYOSHIFUJI Hideaki 	if (table) {
51335ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
5134c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
513586393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
51365ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
51375ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
51385ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
51395ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
51405ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
51415ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
51425ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
51439c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
5144*7c6bb7d2SDavid Ahern 		table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
5145464dc801SEric W. Biederman 
5146464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
5147464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
5148464dc801SEric W. Biederman 			table[0].procname = NULL;
51495ee09105SYOSHIFUJI Hideaki 	}
51505ee09105SYOSHIFUJI Hideaki 
5151760f2d01SDaniel Lezcano 	return table;
5152760f2d01SDaniel Lezcano }
51531da177e4SLinus Torvalds #endif
51541da177e4SLinus Torvalds 
51552c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
5156cdb18761SDaniel Lezcano {
5157633d424bSPavel Emelyanov 	int ret = -ENOMEM;
51588ed67789SDaniel Lezcano 
515986393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
516086393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
5161f2fc6a54SBenjamin Thery 
5162fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5163fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
5164fc66f95cSEric Dumazet 
5165421842edSDavid Ahern 	net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template,
5166421842edSDavid Ahern 					    sizeof(*net->ipv6.fib6_null_entry),
5167421842edSDavid Ahern 					    GFP_KERNEL);
5168421842edSDavid Ahern 	if (!net->ipv6.fib6_null_entry)
5169421842edSDavid Ahern 		goto out_ip6_dst_entries;
5170421842edSDavid Ahern 
51718ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
51728ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
51738ed67789SDaniel Lezcano 					   GFP_KERNEL);
51748ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
5175421842edSDavid Ahern 		goto out_fib6_null_entry;
5176d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
517762fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
517862fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
51798ed67789SDaniel Lezcano 
51808ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5181feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
51828ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
51838ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
51848ed67789SDaniel Lezcano 					       GFP_KERNEL);
518568fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
518668fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
5187d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
518862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
518962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
51908ed67789SDaniel Lezcano 
51918ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
51928ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
51938ed67789SDaniel Lezcano 					       GFP_KERNEL);
519468fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
519568fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
5196d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
519762fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
519862fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
51998ed67789SDaniel Lezcano #endif
52008ed67789SDaniel Lezcano 
5201b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
5202b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
5203b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5204b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5205b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5206b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5207b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5208b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
5209*7c6bb7d2SDavid Ahern 	net->ipv6.sysctl.skip_notify_on_dev_down = 0;
5210b339a47cSPeter Zijlstra 
52116891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
52126891a346SBenjamin Thery 
52138ed67789SDaniel Lezcano 	ret = 0;
52148ed67789SDaniel Lezcano out:
52158ed67789SDaniel Lezcano 	return ret;
5216f2fc6a54SBenjamin Thery 
521768fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
521868fffc67SPeter Zijlstra out_ip6_prohibit_entry:
521968fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
522068fffc67SPeter Zijlstra out_ip6_null_entry:
522168fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
522268fffc67SPeter Zijlstra #endif
5223421842edSDavid Ahern out_fib6_null_entry:
5224421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
5225fc66f95cSEric Dumazet out_ip6_dst_entries:
5226fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5227f2fc6a54SBenjamin Thery out_ip6_dst_ops:
5228f2fc6a54SBenjamin Thery 	goto out;
5229cdb18761SDaniel Lezcano }
5230cdb18761SDaniel Lezcano 
52312c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
5232cdb18761SDaniel Lezcano {
5233421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
52348ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
52358ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
52368ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
52378ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
52388ed67789SDaniel Lezcano #endif
523941bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5240cdb18761SDaniel Lezcano }
5241cdb18761SDaniel Lezcano 
5242d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
5243d189634eSThomas Graf {
5244d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5245c3506372SChristoph Hellwig 	proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
5246c3506372SChristoph Hellwig 			sizeof(struct ipv6_route_iter));
52473617d949SChristoph Hellwig 	proc_create_net_single("rt6_stats", 0444, net->proc_net,
52483617d949SChristoph Hellwig 			rt6_stats_seq_show, NULL);
5249d189634eSThomas Graf #endif
5250d189634eSThomas Graf 	return 0;
5251d189634eSThomas Graf }
5252d189634eSThomas Graf 
5253d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
5254d189634eSThomas Graf {
5255d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5256ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
5257ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
5258d189634eSThomas Graf #endif
5259d189634eSThomas Graf }
5260d189634eSThomas Graf 
5261cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
5262cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
5263cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
5264cdb18761SDaniel Lezcano };
5265cdb18761SDaniel Lezcano 
5266c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
5267c3426b47SDavid S. Miller {
5268c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5269c3426b47SDavid S. Miller 
5270c3426b47SDavid S. Miller 	if (!bp)
5271c3426b47SDavid S. Miller 		return -ENOMEM;
5272c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
5273c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
5274c3426b47SDavid S. Miller 	return 0;
5275c3426b47SDavid S. Miller }
5276c3426b47SDavid S. Miller 
5277c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
5278c3426b47SDavid S. Miller {
5279c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
5280c3426b47SDavid S. Miller 
5281c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
528256a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
5283c3426b47SDavid S. Miller 	kfree(bp);
5284c3426b47SDavid S. Miller }
5285c3426b47SDavid S. Miller 
52862b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
5287c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
5288c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
5289c3426b47SDavid S. Miller };
5290c3426b47SDavid S. Miller 
5291d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
5292d189634eSThomas Graf 	.init = ip6_route_net_init_late,
5293d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
5294d189634eSThomas Graf };
5295d189634eSThomas Graf 
52968ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
52978ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
5298242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
52998ed67789SDaniel Lezcano };
53008ed67789SDaniel Lezcano 
53012f460933SWANG Cong void __init ip6_route_init_special_entries(void)
53022f460933SWANG Cong {
53032f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
53042f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
53052f460933SWANG Cong 	 * manually for init_net */
5306421842edSDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_nh.nh_dev = init_net.loopback_dev;
53072f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
53082f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
53092f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
53102f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
53112f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
53122f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
53132f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
53142f460933SWANG Cong   #endif
53152f460933SWANG Cong }
53162f460933SWANG Cong 
5317433d49c3SDaniel Lezcano int __init ip6_route_init(void)
53181da177e4SLinus Torvalds {
5319433d49c3SDaniel Lezcano 	int ret;
53208d0b94afSMartin KaFai Lau 	int cpu;
5321433d49c3SDaniel Lezcano 
53229a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
53239a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
53249a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
53259a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
53269a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
5327c19a28e1SFernando Carrijo 		goto out;
532814e50e57SDavid S. Miller 
5329fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
53308ed67789SDaniel Lezcano 	if (ret)
5331bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
5332bdb3289fSDaniel Lezcano 
5333c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5334c3426b47SDavid S. Miller 	if (ret)
5335e8803b6cSDavid S. Miller 		goto out_dst_entries;
53362a0c451aSThomas Graf 
53377e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
53387e52b33bSDavid S. Miller 	if (ret)
53397e52b33bSDavid S. Miller 		goto out_register_inetpeer;
5340c3426b47SDavid S. Miller 
53415dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
53425dc121e9SArnaud Ebalard 
5343e8803b6cSDavid S. Miller 	ret = fib6_init();
5344433d49c3SDaniel Lezcano 	if (ret)
53458ed67789SDaniel Lezcano 		goto out_register_subsys;
5346433d49c3SDaniel Lezcano 
5347433d49c3SDaniel Lezcano 	ret = xfrm6_init();
5348433d49c3SDaniel Lezcano 	if (ret)
5349e8803b6cSDavid S. Miller 		goto out_fib6_init;
5350c35b7e72SDaniel Lezcano 
5351433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
5352433d49c3SDaniel Lezcano 	if (ret)
5353433d49c3SDaniel Lezcano 		goto xfrm6_init;
53547e5449c2SDaniel Lezcano 
5355d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
5356d189634eSThomas Graf 	if (ret)
5357d189634eSThomas Graf 		goto fib6_rules_init;
5358d189634eSThomas Graf 
535916feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
536016feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
536116feebcfSFlorian Westphal 	if (ret < 0)
536216feebcfSFlorian Westphal 		goto out_register_late_subsys;
536316feebcfSFlorian Westphal 
536416feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
536516feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
536616feebcfSFlorian Westphal 	if (ret < 0)
536716feebcfSFlorian Westphal 		goto out_register_late_subsys;
536816feebcfSFlorian Westphal 
536916feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
537016feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
537116feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
537216feebcfSFlorian Westphal 	if (ret < 0)
5373d189634eSThomas Graf 		goto out_register_late_subsys;
5374433d49c3SDaniel Lezcano 
53758ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
5376cdb18761SDaniel Lezcano 	if (ret)
5377d189634eSThomas Graf 		goto out_register_late_subsys;
53788ed67789SDaniel Lezcano 
53798d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
53808d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
53818d0b94afSMartin KaFai Lau 
53828d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
53838d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
53848d0b94afSMartin KaFai Lau 	}
53858d0b94afSMartin KaFai Lau 
5386433d49c3SDaniel Lezcano out:
5387433d49c3SDaniel Lezcano 	return ret;
5388433d49c3SDaniel Lezcano 
5389d189634eSThomas Graf out_register_late_subsys:
539016feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
5391d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5392433d49c3SDaniel Lezcano fib6_rules_init:
5393433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
5394433d49c3SDaniel Lezcano xfrm6_init:
5395433d49c3SDaniel Lezcano 	xfrm6_fini();
53962a0c451aSThomas Graf out_fib6_init:
53972a0c451aSThomas Graf 	fib6_gc_cleanup();
53988ed67789SDaniel Lezcano out_register_subsys:
53998ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
54007e52b33bSDavid S. Miller out_register_inetpeer:
54017e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
5402fc66f95cSEric Dumazet out_dst_entries:
5403fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5404433d49c3SDaniel Lezcano out_kmem_cache:
5405f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
5406433d49c3SDaniel Lezcano 	goto out;
54071da177e4SLinus Torvalds }
54081da177e4SLinus Torvalds 
54091da177e4SLinus Torvalds void ip6_route_cleanup(void)
54101da177e4SLinus Torvalds {
54118ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
5412d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5413101367c2SThomas Graf 	fib6_rules_cleanup();
54141da177e4SLinus Torvalds 	xfrm6_fini();
54151da177e4SLinus Torvalds 	fib6_gc_cleanup();
5416c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
54178ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
541841bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5419f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
54201da177e4SLinus Torvalds }
5421