xref: /openbmc/linux/net/ipv6/route.c (revision e1255ed4b6dafd9966c99cde5105891cc1ac70df)
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 {
367ce7ea4afSWei Wang 	struct dst_metrics *p = (struct dst_metrics *)DST_METRICS_PTR(dst);
3681da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
369a68886a6SDavid Ahern 	struct fib6_info *from;
3708d0b94afSMartin KaFai Lau 	struct inet6_dev *idev;
3711da177e4SLinus Torvalds 
372ce7ea4afSWei Wang 	if (p != &dst_default_metrics && refcount_dec_and_test(&p->refcnt))
373ce7ea4afSWei Wang 		kfree(p);
374ce7ea4afSWei Wang 
3758d0b94afSMartin KaFai Lau 	rt6_uncached_list_del(rt);
3768d0b94afSMartin KaFai Lau 
3778d0b94afSMartin KaFai Lau 	idev = rt->rt6i_idev;
37838308473SDavid S. Miller 	if (idev) {
3791da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3801da177e4SLinus Torvalds 		in6_dev_put(idev);
3811da177e4SLinus Torvalds 	}
3821716a961SGao feng 
383a68886a6SDavid Ahern 	rcu_read_lock();
384a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
385a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, NULL);
38693531c67SDavid Ahern 	fib6_info_release(from);
387a68886a6SDavid Ahern 	rcu_read_unlock();
388b3419363SDavid S. Miller }
389b3419363SDavid S. Miller 
3901da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3911da177e4SLinus Torvalds 			   int how)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3941da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3955a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
396c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3971da177e4SLinus Torvalds 
398e5645f51SWei Wang 	if (idev && idev->dev != loopback_dev) {
399e5645f51SWei Wang 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
40038308473SDavid S. Miller 		if (loopback_idev) {
4011da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
4021da177e4SLinus Torvalds 			in6_dev_put(idev);
4031da177e4SLinus Torvalds 		}
4041da177e4SLinus Torvalds 	}
40597cac082SDavid S. Miller }
4061da177e4SLinus Torvalds 
4075973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt)
4085973fb1eSMartin KaFai Lau {
4095973fb1eSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_EXPIRES)
4105973fb1eSMartin KaFai Lau 		return time_after(jiffies, rt->dst.expires);
4115973fb1eSMartin KaFai Lau 	else
4125973fb1eSMartin KaFai Lau 		return false;
4135973fb1eSMartin KaFai Lau }
4145973fb1eSMartin KaFai Lau 
415a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
4161da177e4SLinus Torvalds {
417a68886a6SDavid Ahern 	struct fib6_info *from;
418a68886a6SDavid Ahern 
419a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
420a68886a6SDavid Ahern 
4211716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
4221716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
423a50feda5SEric Dumazet 			return true;
424a68886a6SDavid Ahern 	} else if (from) {
4251e2ea8adSXin Long 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
426a68886a6SDavid Ahern 			fib6_check_expired(from);
4271716a961SGao feng 	}
428a50feda5SEric Dumazet 	return false;
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4313b290a31SDavid Ahern struct fib6_info *fib6_multipath_select(const struct net *net,
4328d1c802bSDavid Ahern 					struct fib6_info *match,
43352bd4c0cSNicolas Dichtel 					struct flowi6 *fl6, int oif,
434b75cc8f9SDavid Ahern 					const struct sk_buff *skb,
43552bd4c0cSNicolas Dichtel 					int strict)
43651ebd318SNicolas Dichtel {
4378d1c802bSDavid Ahern 	struct fib6_info *sibling, *next_sibling;
43851ebd318SNicolas Dichtel 
439b673d6ccSJakub Sitnicki 	/* We might have already computed the hash for ICMPv6 errors. In such
440b673d6ccSJakub Sitnicki 	 * case it will always be non-zero. Otherwise now is the time to do it.
441b673d6ccSJakub Sitnicki 	 */
442b673d6ccSJakub Sitnicki 	if (!fl6->mp_hash)
443b4bac172SDavid Ahern 		fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
444b673d6ccSJakub Sitnicki 
4455e670d84SDavid Ahern 	if (fl6->mp_hash <= atomic_read(&match->fib6_nh.nh_upper_bound))
4463d709f69SIdo Schimmel 		return match;
447bbfcd776SIdo Schimmel 
44893c2fb25SDavid Ahern 	list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
44993c2fb25SDavid Ahern 				 fib6_siblings) {
4505e670d84SDavid Ahern 		int nh_upper_bound;
4515e670d84SDavid Ahern 
4525e670d84SDavid Ahern 		nh_upper_bound = atomic_read(&sibling->fib6_nh.nh_upper_bound);
4535e670d84SDavid Ahern 		if (fl6->mp_hash > nh_upper_bound)
4543d709f69SIdo Schimmel 			continue;
45552bd4c0cSNicolas Dichtel 		if (rt6_score_route(sibling, oif, strict) < 0)
45652bd4c0cSNicolas Dichtel 			break;
45751ebd318SNicolas Dichtel 		match = sibling;
45851ebd318SNicolas Dichtel 		break;
45951ebd318SNicolas Dichtel 	}
4603d709f69SIdo Schimmel 
46151ebd318SNicolas Dichtel 	return match;
46251ebd318SNicolas Dichtel }
46351ebd318SNicolas Dichtel 
4641da177e4SLinus Torvalds /*
46566f5d6ceSWei Wang  *	Route lookup. rcu_read_lock() should be held.
4661da177e4SLinus Torvalds  */
4671da177e4SLinus Torvalds 
4688d1c802bSDavid Ahern static inline struct fib6_info *rt6_device_match(struct net *net,
4698d1c802bSDavid Ahern 						 struct fib6_info *rt,
470b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4711da177e4SLinus Torvalds 						    int oif,
472d420895eSYOSHIFUJI Hideaki 						    int flags)
4731da177e4SLinus Torvalds {
4748d1c802bSDavid Ahern 	struct fib6_info *sprt;
4751da177e4SLinus Torvalds 
4765e670d84SDavid Ahern 	if (!oif && ipv6_addr_any(saddr) &&
4775e670d84SDavid Ahern 	    !(rt->fib6_nh.nh_flags & RTNH_F_DEAD))
4788067bb8cSIdo Schimmel 		return rt;
479dd3abc4eSYOSHIFUJI Hideaki 
4808fb11a9aSDavid Ahern 	for (sprt = rt; sprt; sprt = rcu_dereference(sprt->fib6_next)) {
4815e670d84SDavid Ahern 		const struct net_device *dev = sprt->fib6_nh.nh_dev;
482dd3abc4eSYOSHIFUJI Hideaki 
4835e670d84SDavid Ahern 		if (sprt->fib6_nh.nh_flags & RTNH_F_DEAD)
4848067bb8cSIdo Schimmel 			continue;
4858067bb8cSIdo Schimmel 
486dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4871da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4881da177e4SLinus Torvalds 				return sprt;
489dd3abc4eSYOSHIFUJI Hideaki 		} else {
490dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
491dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
492dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
493dd3abc4eSYOSHIFUJI Hideaki 		}
4941da177e4SLinus Torvalds 	}
4951da177e4SLinus Torvalds 
496eea68cd3SDavid Ahern 	if (oif && flags & RT6_LOOKUP_F_IFACE)
497421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
4981da177e4SLinus Torvalds 
499421842edSDavid Ahern 	return rt->fib6_nh.nh_flags & RTNH_F_DEAD ? net->ipv6.fib6_null_entry : rt;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
50227097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
503c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
504c2f17e82SHannes Frederic Sowa 	struct work_struct work;
505c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
506c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
507c2f17e82SHannes Frederic Sowa };
508c2f17e82SHannes Frederic Sowa 
509c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
510c2f17e82SHannes Frederic Sowa {
511c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
512c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
513c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
514c2f17e82SHannes Frederic Sowa 
515c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
516adc176c5SErik Nordmark 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
517c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
518662f5533SMichael Büsch 	kfree(work);
519c2f17e82SHannes Frederic Sowa }
520c2f17e82SHannes Frederic Sowa 
5218d1c802bSDavid Ahern static void rt6_probe(struct fib6_info *rt)
52227097255SYOSHIFUJI Hideaki {
523990edb42SMartin KaFai Lau 	struct __rt6_probe_work *work;
5245e670d84SDavid Ahern 	const struct in6_addr *nh_gw;
525f2c31e32SEric Dumazet 	struct neighbour *neigh;
5265e670d84SDavid Ahern 	struct net_device *dev;
5275e670d84SDavid Ahern 
52827097255SYOSHIFUJI Hideaki 	/*
52927097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
53027097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
53127097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
53227097255SYOSHIFUJI Hideaki 	 *
53327097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
53427097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
53527097255SYOSHIFUJI Hideaki 	 */
53693c2fb25SDavid Ahern 	if (!rt || !(rt->fib6_flags & RTF_GATEWAY))
537fdd6681dSAmerigo Wang 		return;
5385e670d84SDavid Ahern 
5395e670d84SDavid Ahern 	nh_gw = &rt->fib6_nh.nh_gw;
5405e670d84SDavid Ahern 	dev = rt->fib6_nh.nh_dev;
5412152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
5425e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
5432152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
544dcd1f572SDavid Ahern 		struct inet6_dev *idev;
545dcd1f572SDavid Ahern 
5468d6c31bfSMartin KaFai Lau 		if (neigh->nud_state & NUD_VALID)
5478d6c31bfSMartin KaFai Lau 			goto out;
5488d6c31bfSMartin KaFai Lau 
549dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
550990edb42SMartin KaFai Lau 		work = NULL;
5512152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
552990edb42SMartin KaFai Lau 		if (!(neigh->nud_state & NUD_VALID) &&
553990edb42SMartin KaFai Lau 		    time_after(jiffies,
554dcd1f572SDavid Ahern 			       neigh->updated + idev->cnf.rtr_probe_interval)) {
555c2f17e82SHannes Frederic Sowa 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
556990edb42SMartin KaFai Lau 			if (work)
5577e980569SJiri Benc 				__neigh_set_probe_once(neigh);
558990edb42SMartin KaFai Lau 		}
559c2f17e82SHannes Frederic Sowa 		write_unlock(&neigh->lock);
560990edb42SMartin KaFai Lau 	} else {
561990edb42SMartin KaFai Lau 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
562990edb42SMartin KaFai Lau 	}
563c2f17e82SHannes Frederic Sowa 
564c2f17e82SHannes Frederic Sowa 	if (work) {
565c2f17e82SHannes Frederic Sowa 		INIT_WORK(&work->work, rt6_probe_deferred);
5665e670d84SDavid Ahern 		work->target = *nh_gw;
5675e670d84SDavid Ahern 		dev_hold(dev);
5685e670d84SDavid Ahern 		work->dev = dev;
569c2f17e82SHannes Frederic Sowa 		schedule_work(&work->work);
570c2f17e82SHannes Frederic Sowa 	}
571990edb42SMartin KaFai Lau 
5728d6c31bfSMartin KaFai Lau out:
5732152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
574f2c31e32SEric Dumazet }
57527097255SYOSHIFUJI Hideaki #else
5768d1c802bSDavid Ahern static inline void rt6_probe(struct fib6_info *rt)
57727097255SYOSHIFUJI Hideaki {
57827097255SYOSHIFUJI Hideaki }
57927097255SYOSHIFUJI Hideaki #endif
58027097255SYOSHIFUJI Hideaki 
5811da177e4SLinus Torvalds /*
582554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5831da177e4SLinus Torvalds  */
5848d1c802bSDavid Ahern static inline int rt6_check_dev(struct fib6_info *rt, int oif)
5851da177e4SLinus Torvalds {
5865e670d84SDavid Ahern 	const struct net_device *dev = rt->fib6_nh.nh_dev;
5875e670d84SDavid Ahern 
588161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
589554cfb7eSYOSHIFUJI Hideaki 		return 2;
590554cfb7eSYOSHIFUJI Hideaki 	return 0;
5911da177e4SLinus Torvalds }
5921da177e4SLinus Torvalds 
5938d1c802bSDavid Ahern static inline enum rt6_nud_state rt6_check_neigh(struct fib6_info *rt)
5941da177e4SLinus Torvalds {
595afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
5965e670d84SDavid Ahern 	struct neighbour *neigh;
597f2c31e32SEric Dumazet 
59893c2fb25SDavid Ahern 	if (rt->fib6_flags & RTF_NONEXTHOP ||
59993c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_GATEWAY))
600afc154e9SHannes Frederic Sowa 		return RT6_NUD_SUCCEED;
601145a3621SYOSHIFUJI Hideaki / 吉藤英明 
602145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
6035e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(rt->fib6_nh.nh_dev,
6045e670d84SDavid Ahern 					  &rt->fib6_nh.nh_gw);
605145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
606145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
607554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
608afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
609398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
610a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
611afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
6127e980569SJiri Benc 		else
6137e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
614398bcbebSYOSHIFUJI Hideaki #endif
615145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
616afc154e9SHannes Frederic Sowa 	} else {
617afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
6187e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
619a5a81f0bSPaul Marks 	}
620145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
621145a3621SYOSHIFUJI Hideaki / 吉藤英明 
622a5a81f0bSPaul Marks 	return ret;
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
6258d1c802bSDavid Ahern static int rt6_score_route(struct fib6_info *rt, int oif, int strict)
626554cfb7eSYOSHIFUJI Hideaki {
627a5a81f0bSPaul Marks 	int m;
6284d0c5911SYOSHIFUJI Hideaki 
6294d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
63077d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
631afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
632ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
63393c2fb25SDavid Ahern 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->fib6_flags)) << 2;
634ebacaaa0SYOSHIFUJI Hideaki #endif
635afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE) {
636afc154e9SHannes Frederic Sowa 		int n = rt6_check_neigh(rt);
637afc154e9SHannes Frederic Sowa 		if (n < 0)
638afc154e9SHannes Frederic Sowa 			return n;
639afc154e9SHannes Frederic Sowa 	}
640554cfb7eSYOSHIFUJI Hideaki 	return m;
641554cfb7eSYOSHIFUJI Hideaki }
642554cfb7eSYOSHIFUJI Hideaki 
643dcd1f572SDavid Ahern /* called with rc_read_lock held */
644dcd1f572SDavid Ahern static inline bool fib6_ignore_linkdown(const struct fib6_info *f6i)
645dcd1f572SDavid Ahern {
646dcd1f572SDavid Ahern 	const struct net_device *dev = fib6_info_nh_dev(f6i);
647dcd1f572SDavid Ahern 	bool rc = false;
648dcd1f572SDavid Ahern 
649dcd1f572SDavid Ahern 	if (dev) {
650dcd1f572SDavid Ahern 		const struct inet6_dev *idev = __in6_dev_get(dev);
651dcd1f572SDavid Ahern 
652dcd1f572SDavid Ahern 		rc = !!idev->cnf.ignore_routes_with_linkdown;
653dcd1f572SDavid Ahern 	}
654dcd1f572SDavid Ahern 
655dcd1f572SDavid Ahern 	return rc;
656dcd1f572SDavid Ahern }
657dcd1f572SDavid Ahern 
6588d1c802bSDavid Ahern static struct fib6_info *find_match(struct fib6_info *rt, int oif, int strict,
6598d1c802bSDavid Ahern 				   int *mpri, struct fib6_info *match,
660afc154e9SHannes Frederic Sowa 				   bool *do_rr)
661554cfb7eSYOSHIFUJI Hideaki {
662554cfb7eSYOSHIFUJI Hideaki 	int m;
663afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
66435103d11SAndy Gospodarek 
6655e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
6668067bb8cSIdo Schimmel 		goto out;
6678067bb8cSIdo Schimmel 
668dcd1f572SDavid Ahern 	if (fib6_ignore_linkdown(rt) &&
6695e670d84SDavid Ahern 	    rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
670d5d32e4bSDavid Ahern 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
67135103d11SAndy Gospodarek 		goto out;
672554cfb7eSYOSHIFUJI Hideaki 
67314895687SDavid Ahern 	if (fib6_check_expired(rt))
674f11e6659SDavid S. Miller 		goto out;
675554cfb7eSYOSHIFUJI Hideaki 
676554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
6777e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
678afc154e9SHannes Frederic Sowa 		match_do_rr = true;
679afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
6807e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
681f11e6659SDavid S. Miller 		goto out;
6821da177e4SLinus Torvalds 	}
683f11e6659SDavid S. Miller 
684afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
685afc154e9SHannes Frederic Sowa 		rt6_probe(rt);
686afc154e9SHannes Frederic Sowa 
6877e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
688afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
689afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
690afc154e9SHannes Frederic Sowa 		*mpri = m;
691afc154e9SHannes Frederic Sowa 		match = rt;
692afc154e9SHannes Frederic Sowa 	}
693f11e6659SDavid S. Miller out:
694f11e6659SDavid S. Miller 	return match;
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
6978d1c802bSDavid Ahern static struct fib6_info *find_rr_leaf(struct fib6_node *fn,
6988d1c802bSDavid Ahern 				     struct fib6_info *leaf,
6998d1c802bSDavid Ahern 				     struct fib6_info *rr_head,
700afc154e9SHannes Frederic Sowa 				     u32 metric, int oif, int strict,
701afc154e9SHannes Frederic Sowa 				     bool *do_rr)
702f11e6659SDavid S. Miller {
7038d1c802bSDavid Ahern 	struct fib6_info *rt, *match, *cont;
704f11e6659SDavid S. Miller 	int mpri = -1;
705f11e6659SDavid S. Miller 
706f11e6659SDavid S. Miller 	match = NULL;
7079fbdcfafSSteffen Klassert 	cont = NULL;
7088fb11a9aSDavid Ahern 	for (rt = rr_head; rt; rt = rcu_dereference(rt->fib6_next)) {
70993c2fb25SDavid Ahern 		if (rt->fib6_metric != metric) {
7109fbdcfafSSteffen Klassert 			cont = rt;
7119fbdcfafSSteffen Klassert 			break;
7129fbdcfafSSteffen Klassert 		}
7139fbdcfafSSteffen Klassert 
714afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
7159fbdcfafSSteffen Klassert 	}
7169fbdcfafSSteffen Klassert 
71766f5d6ceSWei Wang 	for (rt = leaf; rt && rt != rr_head;
7188fb11a9aSDavid Ahern 	     rt = rcu_dereference(rt->fib6_next)) {
71993c2fb25SDavid Ahern 		if (rt->fib6_metric != metric) {
7209fbdcfafSSteffen Klassert 			cont = rt;
7219fbdcfafSSteffen Klassert 			break;
7229fbdcfafSSteffen Klassert 		}
7239fbdcfafSSteffen Klassert 
7249fbdcfafSSteffen Klassert 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
7259fbdcfafSSteffen Klassert 	}
7269fbdcfafSSteffen Klassert 
7279fbdcfafSSteffen Klassert 	if (match || !cont)
7289fbdcfafSSteffen Klassert 		return match;
7299fbdcfafSSteffen Klassert 
7308fb11a9aSDavid Ahern 	for (rt = cont; rt; rt = rcu_dereference(rt->fib6_next))
731afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
732f11e6659SDavid S. Miller 
733f11e6659SDavid S. Miller 	return match;
734f11e6659SDavid S. Miller }
735f11e6659SDavid S. Miller 
7368d1c802bSDavid Ahern static struct fib6_info *rt6_select(struct net *net, struct fib6_node *fn,
7378d1040e8SWei Wang 				   int oif, int strict)
738f11e6659SDavid S. Miller {
7398d1c802bSDavid Ahern 	struct fib6_info *leaf = rcu_dereference(fn->leaf);
7408d1c802bSDavid Ahern 	struct fib6_info *match, *rt0;
741afc154e9SHannes Frederic Sowa 	bool do_rr = false;
74217ecf590SWei Wang 	int key_plen;
743f11e6659SDavid S. Miller 
744421842edSDavid Ahern 	if (!leaf || leaf == net->ipv6.fib6_null_entry)
745421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
7468d1040e8SWei Wang 
74766f5d6ceSWei Wang 	rt0 = rcu_dereference(fn->rr_ptr);
748f11e6659SDavid S. Miller 	if (!rt0)
74966f5d6ceSWei Wang 		rt0 = leaf;
750f11e6659SDavid S. Miller 
75117ecf590SWei Wang 	/* Double check to make sure fn is not an intermediate node
75217ecf590SWei Wang 	 * and fn->leaf does not points to its child's leaf
75317ecf590SWei Wang 	 * (This might happen if all routes under fn are deleted from
75417ecf590SWei Wang 	 * the tree and fib6_repair_tree() is called on the node.)
75517ecf590SWei Wang 	 */
75693c2fb25SDavid Ahern 	key_plen = rt0->fib6_dst.plen;
75717ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES
75893c2fb25SDavid Ahern 	if (rt0->fib6_src.plen)
75993c2fb25SDavid Ahern 		key_plen = rt0->fib6_src.plen;
76017ecf590SWei Wang #endif
76117ecf590SWei Wang 	if (fn->fn_bit != key_plen)
762421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
76317ecf590SWei Wang 
76493c2fb25SDavid Ahern 	match = find_rr_leaf(fn, leaf, rt0, rt0->fib6_metric, oif, strict,
765afc154e9SHannes Frederic Sowa 			     &do_rr);
766f11e6659SDavid S. Miller 
767afc154e9SHannes Frederic Sowa 	if (do_rr) {
7688fb11a9aSDavid Ahern 		struct fib6_info *next = rcu_dereference(rt0->fib6_next);
769f11e6659SDavid S. Miller 
770554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
77193c2fb25SDavid Ahern 		if (!next || next->fib6_metric != rt0->fib6_metric)
7728d1040e8SWei Wang 			next = leaf;
773f11e6659SDavid S. Miller 
77466f5d6ceSWei Wang 		if (next != rt0) {
77593c2fb25SDavid Ahern 			spin_lock_bh(&leaf->fib6_table->tb6_lock);
77666f5d6ceSWei Wang 			/* make sure next is not being deleted from the tree */
77793c2fb25SDavid Ahern 			if (next->fib6_node)
77866f5d6ceSWei Wang 				rcu_assign_pointer(fn->rr_ptr, next);
77993c2fb25SDavid Ahern 			spin_unlock_bh(&leaf->fib6_table->tb6_lock);
78066f5d6ceSWei Wang 		}
781554cfb7eSYOSHIFUJI Hideaki 	}
782554cfb7eSYOSHIFUJI Hideaki 
783421842edSDavid Ahern 	return match ? match : net->ipv6.fib6_null_entry;
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7868d1c802bSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_info *rt)
7878b9df265SMartin KaFai Lau {
78893c2fb25SDavid Ahern 	return (rt->fib6_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
7898b9df265SMartin KaFai Lau }
7908b9df265SMartin KaFai Lau 
79170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
79270ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
793b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
79470ceb4f5SYOSHIFUJI Hideaki {
795c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
79670ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
79770ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
79870ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
7994bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
8008d1c802bSDavid Ahern 	struct fib6_info *rt;
80170ceb4f5SYOSHIFUJI Hideaki 
80270ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
80370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
80470ceb4f5SYOSHIFUJI Hideaki 	}
80570ceb4f5SYOSHIFUJI Hideaki 
80670ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
80770ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
80870ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
80970ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
81070ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
81170ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
81270ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
81370ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
81470ceb4f5SYOSHIFUJI Hideaki 		}
81570ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
81670ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
81770ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
81870ceb4f5SYOSHIFUJI Hideaki 		}
81970ceb4f5SYOSHIFUJI Hideaki 	}
82070ceb4f5SYOSHIFUJI Hideaki 
82170ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
82270ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
8233933fc95SJens Rosenboom 		return -EINVAL;
82470ceb4f5SYOSHIFUJI Hideaki 
8254bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
82670ceb4f5SYOSHIFUJI Hideaki 
82770ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
82870ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
82970ceb4f5SYOSHIFUJI Hideaki 	else {
83070ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
83170ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
83270ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
83370ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
83470ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
83570ceb4f5SYOSHIFUJI Hideaki 	}
83670ceb4f5SYOSHIFUJI Hideaki 
837f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
838afb1d4b5SDavid Ahern 		rt = rt6_get_dflt_router(net, gwaddr, dev);
839f104a567SDuan Jiong 	else
840f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
841830218c1SDavid Ahern 					gwaddr, dev);
84270ceb4f5SYOSHIFUJI Hideaki 
84370ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
844afb1d4b5SDavid Ahern 		ip6_del_rt(net, rt);
84570ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
84670ceb4f5SYOSHIFUJI Hideaki 	}
84770ceb4f5SYOSHIFUJI Hideaki 
84870ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
849830218c1SDavid Ahern 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
850830218c1SDavid Ahern 					dev, pref);
85170ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
85293c2fb25SDavid Ahern 		rt->fib6_flags = RTF_ROUTEINFO |
85393c2fb25SDavid Ahern 				 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
85470ceb4f5SYOSHIFUJI Hideaki 
85570ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
8561716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
85714895687SDavid Ahern 			fib6_clean_expires(rt);
8581716a961SGao feng 		else
85914895687SDavid Ahern 			fib6_set_expires(rt, jiffies + HZ * lifetime);
8601716a961SGao feng 
86193531c67SDavid Ahern 		fib6_info_release(rt);
86270ceb4f5SYOSHIFUJI Hideaki 	}
86370ceb4f5SYOSHIFUJI Hideaki 	return 0;
86470ceb4f5SYOSHIFUJI Hideaki }
86570ceb4f5SYOSHIFUJI Hideaki #endif
86670ceb4f5SYOSHIFUJI Hideaki 
867ae90d867SDavid Ahern /*
868ae90d867SDavid Ahern  *	Misc support functions
869ae90d867SDavid Ahern  */
870ae90d867SDavid Ahern 
871ae90d867SDavid Ahern /* called with rcu_lock held */
8728d1c802bSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct fib6_info *rt)
873ae90d867SDavid Ahern {
8745e670d84SDavid Ahern 	struct net_device *dev = rt->fib6_nh.nh_dev;
875ae90d867SDavid Ahern 
87693c2fb25SDavid Ahern 	if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
877ae90d867SDavid Ahern 		/* for copies of local routes, dst->dev needs to be the
878ae90d867SDavid Ahern 		 * device if it is a master device, the master device if
879ae90d867SDavid Ahern 		 * device is enslaved, and the loopback as the default
880ae90d867SDavid Ahern 		 */
881ae90d867SDavid Ahern 		if (netif_is_l3_slave(dev) &&
88293c2fb25SDavid Ahern 		    !rt6_need_strict(&rt->fib6_dst.addr))
883ae90d867SDavid Ahern 			dev = l3mdev_master_dev_rcu(dev);
884ae90d867SDavid Ahern 		else if (!netif_is_l3_master(dev))
885ae90d867SDavid Ahern 			dev = dev_net(dev)->loopback_dev;
886ae90d867SDavid Ahern 		/* last case is netif_is_l3_master(dev) is true in which
887ae90d867SDavid Ahern 		 * case we want dev returned to be dev
888ae90d867SDavid Ahern 		 */
889ae90d867SDavid Ahern 	}
890ae90d867SDavid Ahern 
891ae90d867SDavid Ahern 	return dev;
892ae90d867SDavid Ahern }
893ae90d867SDavid Ahern 
8946edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = {
8956edb3c96SDavid Ahern 	[RTN_UNSPEC]	= 0,
8966edb3c96SDavid Ahern 	[RTN_UNICAST]	= 0,
8976edb3c96SDavid Ahern 	[RTN_LOCAL]	= 0,
8986edb3c96SDavid Ahern 	[RTN_BROADCAST]	= 0,
8996edb3c96SDavid Ahern 	[RTN_ANYCAST]	= 0,
9006edb3c96SDavid Ahern 	[RTN_MULTICAST]	= 0,
9016edb3c96SDavid Ahern 	[RTN_BLACKHOLE]	= -EINVAL,
9026edb3c96SDavid Ahern 	[RTN_UNREACHABLE] = -EHOSTUNREACH,
9036edb3c96SDavid Ahern 	[RTN_PROHIBIT]	= -EACCES,
9046edb3c96SDavid Ahern 	[RTN_THROW]	= -EAGAIN,
9056edb3c96SDavid Ahern 	[RTN_NAT]	= -EINVAL,
9066edb3c96SDavid Ahern 	[RTN_XRESOLVE]	= -EINVAL,
9076edb3c96SDavid Ahern };
9086edb3c96SDavid Ahern 
9096edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type)
9106edb3c96SDavid Ahern {
9116edb3c96SDavid Ahern 	return fib6_prop[fib6_type];
9126edb3c96SDavid Ahern }
9136edb3c96SDavid Ahern 
9148d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
9153b6761d1SDavid Ahern {
9163b6761d1SDavid Ahern 	unsigned short flags = 0;
9173b6761d1SDavid Ahern 
9183b6761d1SDavid Ahern 	if (rt->dst_nocount)
9193b6761d1SDavid Ahern 		flags |= DST_NOCOUNT;
9203b6761d1SDavid Ahern 	if (rt->dst_nopolicy)
9213b6761d1SDavid Ahern 		flags |= DST_NOPOLICY;
9223b6761d1SDavid Ahern 	if (rt->dst_host)
9233b6761d1SDavid Ahern 		flags |= DST_HOST;
9243b6761d1SDavid Ahern 
9253b6761d1SDavid Ahern 	return flags;
9263b6761d1SDavid Ahern }
9273b6761d1SDavid Ahern 
9288d1c802bSDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, struct fib6_info *ort)
9296edb3c96SDavid Ahern {
9306edb3c96SDavid Ahern 	rt->dst.error = ip6_rt_type_to_error(ort->fib6_type);
9316edb3c96SDavid Ahern 
9326edb3c96SDavid Ahern 	switch (ort->fib6_type) {
9336edb3c96SDavid Ahern 	case RTN_BLACKHOLE:
9346edb3c96SDavid Ahern 		rt->dst.output = dst_discard_out;
9356edb3c96SDavid Ahern 		rt->dst.input = dst_discard;
9366edb3c96SDavid Ahern 		break;
9376edb3c96SDavid Ahern 	case RTN_PROHIBIT:
9386edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_prohibit_out;
9396edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_prohibit;
9406edb3c96SDavid Ahern 		break;
9416edb3c96SDavid Ahern 	case RTN_THROW:
9426edb3c96SDavid Ahern 	case RTN_UNREACHABLE:
9436edb3c96SDavid Ahern 	default:
9446edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_discard_out;
9456edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_discard;
9466edb3c96SDavid Ahern 		break;
9476edb3c96SDavid Ahern 	}
9486edb3c96SDavid Ahern }
9496edb3c96SDavid Ahern 
9508d1c802bSDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, struct fib6_info *ort)
9516edb3c96SDavid Ahern {
95293c2fb25SDavid Ahern 	if (ort->fib6_flags & RTF_REJECT) {
9536edb3c96SDavid Ahern 		ip6_rt_init_dst_reject(rt, ort);
9546edb3c96SDavid Ahern 		return;
9556edb3c96SDavid Ahern 	}
9566edb3c96SDavid Ahern 
9576edb3c96SDavid Ahern 	rt->dst.error = 0;
9586edb3c96SDavid Ahern 	rt->dst.output = ip6_output;
9596edb3c96SDavid Ahern 
960d23c4b63SHangbin Liu 	if (ort->fib6_type == RTN_LOCAL || ort->fib6_type == RTN_ANYCAST) {
9616edb3c96SDavid Ahern 		rt->dst.input = ip6_input;
96293c2fb25SDavid Ahern 	} else if (ipv6_addr_type(&ort->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
9636edb3c96SDavid Ahern 		rt->dst.input = ip6_mc_input;
9646edb3c96SDavid Ahern 	} else {
9656edb3c96SDavid Ahern 		rt->dst.input = ip6_forward;
9666edb3c96SDavid Ahern 	}
9676edb3c96SDavid Ahern 
9686edb3c96SDavid Ahern 	if (ort->fib6_nh.nh_lwtstate) {
9696edb3c96SDavid Ahern 		rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate);
9706edb3c96SDavid Ahern 		lwtunnel_set_redirect(&rt->dst);
9716edb3c96SDavid Ahern 	}
9726edb3c96SDavid Ahern 
9736edb3c96SDavid Ahern 	rt->dst.lastuse = jiffies;
9746edb3c96SDavid Ahern }
9756edb3c96SDavid Ahern 
976e873e4b9SWei Wang /* Caller must already hold reference to @from */
9778d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
978ae90d867SDavid Ahern {
979ae90d867SDavid Ahern 	rt->rt6i_flags &= ~RTF_EXPIRES;
980a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, from);
981*e1255ed4SDavid Ahern 	ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
982ae90d867SDavid Ahern }
983ae90d867SDavid Ahern 
984e873e4b9SWei Wang /* Caller must already hold reference to @ort */
9858d1c802bSDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, struct fib6_info *ort)
986ae90d867SDavid Ahern {
987dcd1f572SDavid Ahern 	struct net_device *dev = fib6_info_nh_dev(ort);
988dcd1f572SDavid Ahern 
9896edb3c96SDavid Ahern 	ip6_rt_init_dst(rt, ort);
9906edb3c96SDavid Ahern 
99193c2fb25SDavid Ahern 	rt->rt6i_dst = ort->fib6_dst;
992dcd1f572SDavid Ahern 	rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
9935e670d84SDavid Ahern 	rt->rt6i_gateway = ort->fib6_nh.nh_gw;
99493c2fb25SDavid Ahern 	rt->rt6i_flags = ort->fib6_flags;
995ae90d867SDavid Ahern 	rt6_set_from(rt, ort);
996ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
99793c2fb25SDavid Ahern 	rt->rt6i_src = ort->fib6_src;
998ae90d867SDavid Ahern #endif
999ae90d867SDavid Ahern }
1000ae90d867SDavid Ahern 
1001a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1002a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
1003a3c00e46SMartin KaFai Lau {
100466f5d6ceSWei Wang 	struct fib6_node *pn, *sn;
1005a3c00e46SMartin KaFai Lau 	while (1) {
1006a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
1007a3c00e46SMartin KaFai Lau 			return NULL;
100866f5d6ceSWei Wang 		pn = rcu_dereference(fn->parent);
100966f5d6ceSWei Wang 		sn = FIB6_SUBTREE(pn);
101066f5d6ceSWei Wang 		if (sn && sn != fn)
10116454743bSDavid Ahern 			fn = fib6_node_lookup(sn, NULL, saddr);
1012a3c00e46SMartin KaFai Lau 		else
1013a3c00e46SMartin KaFai Lau 			fn = pn;
1014a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
1015a3c00e46SMartin KaFai Lau 			return fn;
1016a3c00e46SMartin KaFai Lau 	}
1017a3c00e46SMartin KaFai Lau }
1018c71099acSThomas Graf 
1019d3843fe5SWei Wang static bool ip6_hold_safe(struct net *net, struct rt6_info **prt,
1020d3843fe5SWei Wang 			  bool null_fallback)
1021d3843fe5SWei Wang {
1022d3843fe5SWei Wang 	struct rt6_info *rt = *prt;
1023d3843fe5SWei Wang 
1024d3843fe5SWei Wang 	if (dst_hold_safe(&rt->dst))
1025d3843fe5SWei Wang 		return true;
1026d3843fe5SWei Wang 	if (null_fallback) {
1027d3843fe5SWei Wang 		rt = net->ipv6.ip6_null_entry;
1028d3843fe5SWei Wang 		dst_hold(&rt->dst);
1029d3843fe5SWei Wang 	} else {
1030d3843fe5SWei Wang 		rt = NULL;
1031d3843fe5SWei Wang 	}
1032d3843fe5SWei Wang 	*prt = rt;
1033d3843fe5SWei Wang 	return false;
1034d3843fe5SWei Wang }
1035d3843fe5SWei Wang 
1036dec9b0e2SDavid Ahern /* called with rcu_lock held */
10378d1c802bSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(struct fib6_info *rt)
1038dec9b0e2SDavid Ahern {
10393b6761d1SDavid Ahern 	unsigned short flags = fib6_info_dst_flags(rt);
1040dec9b0e2SDavid Ahern 	struct net_device *dev = rt->fib6_nh.nh_dev;
1041dec9b0e2SDavid Ahern 	struct rt6_info *nrt;
1042dec9b0e2SDavid Ahern 
1043e873e4b9SWei Wang 	if (!fib6_info_hold_safe(rt))
1044e873e4b9SWei Wang 		return NULL;
1045e873e4b9SWei Wang 
104693531c67SDavid Ahern 	nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
1047dec9b0e2SDavid Ahern 	if (nrt)
1048dec9b0e2SDavid Ahern 		ip6_rt_copy_init(nrt, rt);
1049e873e4b9SWei Wang 	else
1050e873e4b9SWei Wang 		fib6_info_release(rt);
1051dec9b0e2SDavid Ahern 
1052dec9b0e2SDavid Ahern 	return nrt;
1053dec9b0e2SDavid Ahern }
1054dec9b0e2SDavid Ahern 
10558ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
10568ed67789SDaniel Lezcano 					     struct fib6_table *table,
1057b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
1058b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
1059b75cc8f9SDavid Ahern 					     int flags)
10601da177e4SLinus Torvalds {
10618d1c802bSDavid Ahern 	struct fib6_info *f6i;
10621da177e4SLinus Torvalds 	struct fib6_node *fn;
106323fb93a4SDavid Ahern 	struct rt6_info *rt;
10641da177e4SLinus Torvalds 
1065b6cdbc85SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1066b6cdbc85SDavid Ahern 		flags &= ~RT6_LOOKUP_F_IFACE;
1067b6cdbc85SDavid Ahern 
106866f5d6ceSWei Wang 	rcu_read_lock();
10696454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1070c71099acSThomas Graf restart:
107123fb93a4SDavid Ahern 	f6i = rcu_dereference(fn->leaf);
107223fb93a4SDavid Ahern 	if (!f6i) {
107323fb93a4SDavid Ahern 		f6i = net->ipv6.fib6_null_entry;
107466f5d6ceSWei Wang 	} else {
107523fb93a4SDavid Ahern 		f6i = rt6_device_match(net, f6i, &fl6->saddr,
107666f5d6ceSWei Wang 				      fl6->flowi6_oif, flags);
107793c2fb25SDavid Ahern 		if (f6i->fib6_nsiblings && fl6->flowi6_oif == 0)
10783b290a31SDavid Ahern 			f6i = fib6_multipath_select(net, f6i, fl6,
10793b290a31SDavid Ahern 						    fl6->flowi6_oif, skb,
10803b290a31SDavid Ahern 						    flags);
108166f5d6ceSWei Wang 	}
108223fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1083a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1084a3c00e46SMartin KaFai Lau 		if (fn)
1085a3c00e46SMartin KaFai Lau 			goto restart;
1086a3c00e46SMartin KaFai Lau 	}
10872b760fcfSWei Wang 
1088d4bea421SDavid Ahern 	trace_fib6_table_lookup(net, f6i, table, fl6);
1089d4bea421SDavid Ahern 
10904c9483b2SDavid S. Miller 	/* Search through exception table */
109123fb93a4SDavid Ahern 	rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
109223fb93a4SDavid Ahern 	if (rt) {
1093d3843fe5SWei Wang 		if (ip6_hold_safe(net, &rt, true))
1094d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
109523fb93a4SDavid Ahern 	} else if (f6i == net->ipv6.fib6_null_entry) {
1096dec9b0e2SDavid Ahern 		rt = net->ipv6.ip6_null_entry;
1097dec9b0e2SDavid Ahern 		dst_hold(&rt->dst);
109823fb93a4SDavid Ahern 	} else {
109923fb93a4SDavid Ahern 		rt = ip6_create_rt_rcu(f6i);
110023fb93a4SDavid Ahern 		if (!rt) {
110123fb93a4SDavid Ahern 			rt = net->ipv6.ip6_null_entry;
110223fb93a4SDavid Ahern 			dst_hold(&rt->dst);
110323fb93a4SDavid Ahern 		}
1104dec9b0e2SDavid Ahern 	}
1105d3843fe5SWei Wang 
110666f5d6ceSWei Wang 	rcu_read_unlock();
1107b811580dSDavid Ahern 
11081da177e4SLinus Torvalds 	return rt;
1109c71099acSThomas Graf }
1110c71099acSThomas Graf 
1111ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1112b75cc8f9SDavid Ahern 				   const struct sk_buff *skb, int flags)
1113ea6e574eSFlorian Westphal {
1114b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1115ea6e574eSFlorian Westphal }
1116ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
1117ea6e574eSFlorian Westphal 
11189acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1119b75cc8f9SDavid Ahern 			    const struct in6_addr *saddr, int oif,
1120b75cc8f9SDavid Ahern 			    const struct sk_buff *skb, int strict)
1121c71099acSThomas Graf {
11224c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
11234c9483b2SDavid S. Miller 		.flowi6_oif = oif,
11244c9483b2SDavid S. Miller 		.daddr = *daddr,
1125c71099acSThomas Graf 	};
1126c71099acSThomas Graf 	struct dst_entry *dst;
112777d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1128c71099acSThomas Graf 
1129adaa70bbSThomas Graf 	if (saddr) {
11304c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1131adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1132adaa70bbSThomas Graf 	}
1133adaa70bbSThomas Graf 
1134b75cc8f9SDavid Ahern 	dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1135c71099acSThomas Graf 	if (dst->error == 0)
1136c71099acSThomas Graf 		return (struct rt6_info *) dst;
1137c71099acSThomas Graf 
1138c71099acSThomas Graf 	dst_release(dst);
1139c71099acSThomas Graf 
11401da177e4SLinus Torvalds 	return NULL;
11411da177e4SLinus Torvalds }
11427159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
11437159039aSYOSHIFUJI Hideaki 
1144c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
11451cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
11461cfb71eeSWei Wang  * route is released.
11471cfb71eeSWei Wang  * Caller must hold dst before calling it.
11481da177e4SLinus Torvalds  */
11491da177e4SLinus Torvalds 
11508d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1151333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
11521da177e4SLinus Torvalds {
11531da177e4SLinus Torvalds 	int err;
1154c71099acSThomas Graf 	struct fib6_table *table;
11551da177e4SLinus Torvalds 
115693c2fb25SDavid Ahern 	table = rt->fib6_table;
115766f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
1158d4ead6b3SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, extack);
115966f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	return err;
11621da177e4SLinus Torvalds }
11631da177e4SLinus Torvalds 
11648d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt)
116540e22e8fSThomas Graf {
1166afb1d4b5SDavid Ahern 	struct nl_info info = {	.nl_net = net, };
1167e715b6d3SFlorian Westphal 
1168d4ead6b3SDavid Ahern 	return __ip6_ins_rt(rt, &info, NULL);
116940e22e8fSThomas Graf }
117040e22e8fSThomas Graf 
11718d1c802bSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(struct fib6_info *ort,
117221efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
1173b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
11741da177e4SLinus Torvalds {
11754832c30dSDavid Ahern 	struct net_device *dev;
11761da177e4SLinus Torvalds 	struct rt6_info *rt;
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	/*
11791da177e4SLinus Torvalds 	 *	Clone the route.
11801da177e4SLinus Torvalds 	 */
11811da177e4SLinus Torvalds 
1182e873e4b9SWei Wang 	if (!fib6_info_hold_safe(ort))
1183e873e4b9SWei Wang 		return NULL;
1184e873e4b9SWei Wang 
11854832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(ort);
118693531c67SDavid Ahern 	rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1187e873e4b9SWei Wang 	if (!rt) {
1188e873e4b9SWei Wang 		fib6_info_release(ort);
118983a09abdSMartin KaFai Lau 		return NULL;
1190e873e4b9SWei Wang 	}
119183a09abdSMartin KaFai Lau 
119283a09abdSMartin KaFai Lau 	ip6_rt_copy_init(rt, ort);
11938b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
119483a09abdSMartin KaFai Lau 	rt->dst.flags |= DST_HOST;
119583a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
119683a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
11978b9df265SMartin KaFai Lau 
11988b9df265SMartin KaFai Lau 	if (!rt6_is_gw_or_nonexthop(ort)) {
119993c2fb25SDavid Ahern 		if (ort->fib6_dst.plen != 128 &&
120093c2fb25SDavid Ahern 		    ipv6_addr_equal(&ort->fib6_dst.addr, daddr))
120158c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
12021da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
12031da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
12044e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
12051da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
12061da177e4SLinus Torvalds 		}
12071da177e4SLinus Torvalds #endif
120895a9a5baSYOSHIFUJI Hideaki 	}
120995a9a5baSYOSHIFUJI Hideaki 
1210299d9939SYOSHIFUJI Hideaki 	return rt;
1211299d9939SYOSHIFUJI Hideaki }
1212299d9939SYOSHIFUJI Hideaki 
12138d1c802bSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(struct fib6_info *rt)
1214d52d3997SMartin KaFai Lau {
12153b6761d1SDavid Ahern 	unsigned short flags = fib6_info_dst_flags(rt);
12164832c30dSDavid Ahern 	struct net_device *dev;
1217d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1218d52d3997SMartin KaFai Lau 
1219e873e4b9SWei Wang 	if (!fib6_info_hold_safe(rt))
1220e873e4b9SWei Wang 		return NULL;
1221e873e4b9SWei Wang 
12224832c30dSDavid Ahern 	rcu_read_lock();
12234832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(rt);
122493531c67SDavid Ahern 	pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
12254832c30dSDavid Ahern 	rcu_read_unlock();
1226e873e4b9SWei Wang 	if (!pcpu_rt) {
1227e873e4b9SWei Wang 		fib6_info_release(rt);
1228d52d3997SMartin KaFai Lau 		return NULL;
1229e873e4b9SWei Wang 	}
1230d52d3997SMartin KaFai Lau 	ip6_rt_copy_init(pcpu_rt, rt);
1231d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
1232d52d3997SMartin KaFai Lau 	return pcpu_rt;
1233d52d3997SMartin KaFai Lau }
1234d52d3997SMartin KaFai Lau 
123566f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */
12368d1c802bSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(struct fib6_info *rt)
1237d52d3997SMartin KaFai Lau {
1238a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, **p;
1239d52d3997SMartin KaFai Lau 
1240d52d3997SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1241d52d3997SMartin KaFai Lau 	pcpu_rt = *p;
1242d52d3997SMartin KaFai Lau 
1243d4ead6b3SDavid Ahern 	if (pcpu_rt)
1244d4ead6b3SDavid Ahern 		ip6_hold_safe(NULL, &pcpu_rt, false);
1245d3843fe5SWei Wang 
1246a73e4195SMartin KaFai Lau 	return pcpu_rt;
1247a73e4195SMartin KaFai Lau }
1248a73e4195SMartin KaFai Lau 
1249afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net,
12508d1c802bSDavid Ahern 					    struct fib6_info *rt)
1251a73e4195SMartin KaFai Lau {
1252a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1253d52d3997SMartin KaFai Lau 
1254d52d3997SMartin KaFai Lau 	pcpu_rt = ip6_rt_pcpu_alloc(rt);
1255d52d3997SMartin KaFai Lau 	if (!pcpu_rt) {
12569c7370a1SMartin KaFai Lau 		dst_hold(&net->ipv6.ip6_null_entry->dst);
12579c7370a1SMartin KaFai Lau 		return net->ipv6.ip6_null_entry;
1258d52d3997SMartin KaFai Lau 	}
1259d52d3997SMartin KaFai Lau 
1260a94b9367SWei Wang 	dst_hold(&pcpu_rt->dst);
1261a73e4195SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1262d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1263951f788aSEric Dumazet 	BUG_ON(prev);
1264a94b9367SWei Wang 
1265d52d3997SMartin KaFai Lau 	return pcpu_rt;
1266d52d3997SMartin KaFai Lau }
1267d52d3997SMartin KaFai Lau 
126835732d01SWei Wang /* exception hash table implementation
126935732d01SWei Wang  */
127035732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
127135732d01SWei Wang 
127235732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
127335732d01SWei Wang  * Caller must hold rt6_exception_lock
127435732d01SWei Wang  */
127535732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
127635732d01SWei Wang 				 struct rt6_exception *rt6_ex)
127735732d01SWei Wang {
1278b2427e67SColin Ian King 	struct net *net;
127981eb8447SWei Wang 
128035732d01SWei Wang 	if (!bucket || !rt6_ex)
128135732d01SWei Wang 		return;
1282b2427e67SColin Ian King 
1283b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
128435732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
128577634cc6SDavid Ahern 	dst_release(&rt6_ex->rt6i->dst);
128635732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
128735732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
128835732d01SWei Wang 	bucket->depth--;
128981eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache--;
129035732d01SWei Wang }
129135732d01SWei Wang 
129235732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
129335732d01SWei Wang  * Caller must hold rt6_exception_lock
129435732d01SWei Wang  */
129535732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
129635732d01SWei Wang {
129735732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
129835732d01SWei Wang 
129935732d01SWei Wang 	if (!bucket)
130035732d01SWei Wang 		return;
130135732d01SWei Wang 
130235732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
130335732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
130435732d01SWei Wang 			oldest = rt6_ex;
130535732d01SWei Wang 	}
130635732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
130735732d01SWei Wang }
130835732d01SWei Wang 
130935732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
131035732d01SWei Wang 			      const struct in6_addr *src)
131135732d01SWei Wang {
131235732d01SWei Wang 	static u32 seed __read_mostly;
131335732d01SWei Wang 	u32 val;
131435732d01SWei Wang 
131535732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
131635732d01SWei Wang 	val = jhash(dst, sizeof(*dst), seed);
131735732d01SWei Wang 
131835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
131935732d01SWei Wang 	if (src)
132035732d01SWei Wang 		val = jhash(src, sizeof(*src), val);
132135732d01SWei Wang #endif
132235732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
132335732d01SWei Wang }
132435732d01SWei Wang 
132535732d01SWei Wang /* Helper function to find the cached rt in the hash table
132635732d01SWei Wang  * and update bucket pointer to point to the bucket for this
132735732d01SWei Wang  * (daddr, saddr) pair
132835732d01SWei Wang  * Caller must hold rt6_exception_lock
132935732d01SWei Wang  */
133035732d01SWei Wang static struct rt6_exception *
133135732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
133235732d01SWei Wang 			      const struct in6_addr *daddr,
133335732d01SWei Wang 			      const struct in6_addr *saddr)
133435732d01SWei Wang {
133535732d01SWei Wang 	struct rt6_exception *rt6_ex;
133635732d01SWei Wang 	u32 hval;
133735732d01SWei Wang 
133835732d01SWei Wang 	if (!(*bucket) || !daddr)
133935732d01SWei Wang 		return NULL;
134035732d01SWei Wang 
134135732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
134235732d01SWei Wang 	*bucket += hval;
134335732d01SWei Wang 
134435732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
134535732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
134635732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
134735732d01SWei Wang 
134835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
134935732d01SWei Wang 		if (matched && saddr)
135035732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
135135732d01SWei Wang #endif
135235732d01SWei Wang 		if (matched)
135335732d01SWei Wang 			return rt6_ex;
135435732d01SWei Wang 	}
135535732d01SWei Wang 	return NULL;
135635732d01SWei Wang }
135735732d01SWei Wang 
135835732d01SWei Wang /* Helper function to find the cached rt in the hash table
135935732d01SWei Wang  * and update bucket pointer to point to the bucket for this
136035732d01SWei Wang  * (daddr, saddr) pair
136135732d01SWei Wang  * Caller must hold rcu_read_lock()
136235732d01SWei Wang  */
136335732d01SWei Wang static struct rt6_exception *
136435732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
136535732d01SWei Wang 			 const struct in6_addr *daddr,
136635732d01SWei Wang 			 const struct in6_addr *saddr)
136735732d01SWei Wang {
136835732d01SWei Wang 	struct rt6_exception *rt6_ex;
136935732d01SWei Wang 	u32 hval;
137035732d01SWei Wang 
137135732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
137235732d01SWei Wang 
137335732d01SWei Wang 	if (!(*bucket) || !daddr)
137435732d01SWei Wang 		return NULL;
137535732d01SWei Wang 
137635732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
137735732d01SWei Wang 	*bucket += hval;
137835732d01SWei Wang 
137935732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
138035732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
138135732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
138235732d01SWei Wang 
138335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
138435732d01SWei Wang 		if (matched && saddr)
138535732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
138635732d01SWei Wang #endif
138735732d01SWei Wang 		if (matched)
138835732d01SWei Wang 			return rt6_ex;
138935732d01SWei Wang 	}
139035732d01SWei Wang 	return NULL;
139135732d01SWei Wang }
139235732d01SWei Wang 
13938d1c802bSDavid Ahern static unsigned int fib6_mtu(const struct fib6_info *rt)
139435732d01SWei Wang {
1395d4ead6b3SDavid Ahern 	unsigned int mtu;
1396d4ead6b3SDavid Ahern 
1397dcd1f572SDavid Ahern 	if (rt->fib6_pmtu) {
1398dcd1f572SDavid Ahern 		mtu = rt->fib6_pmtu;
1399dcd1f572SDavid Ahern 	} else {
1400dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
1401dcd1f572SDavid Ahern 		struct inet6_dev *idev;
1402dcd1f572SDavid Ahern 
1403dcd1f572SDavid Ahern 		rcu_read_lock();
1404dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
1405dcd1f572SDavid Ahern 		mtu = idev->cnf.mtu6;
1406dcd1f572SDavid Ahern 		rcu_read_unlock();
1407dcd1f572SDavid Ahern 	}
1408dcd1f572SDavid Ahern 
1409d4ead6b3SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1410d4ead6b3SDavid Ahern 
1411d4ead6b3SDavid Ahern 	return mtu - lwtunnel_headroom(rt->fib6_nh.nh_lwtstate, mtu);
1412d4ead6b3SDavid Ahern }
1413d4ead6b3SDavid Ahern 
141435732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
14158d1c802bSDavid Ahern 				struct fib6_info *ort)
141635732d01SWei Wang {
14175e670d84SDavid Ahern 	struct net *net = dev_net(nrt->dst.dev);
141835732d01SWei Wang 	struct rt6_exception_bucket *bucket;
141935732d01SWei Wang 	struct in6_addr *src_key = NULL;
142035732d01SWei Wang 	struct rt6_exception *rt6_ex;
142135732d01SWei Wang 	int err = 0;
142235732d01SWei Wang 
142335732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
142435732d01SWei Wang 
142535732d01SWei Wang 	if (ort->exception_bucket_flushed) {
142635732d01SWei Wang 		err = -EINVAL;
142735732d01SWei Wang 		goto out;
142835732d01SWei Wang 	}
142935732d01SWei Wang 
143035732d01SWei Wang 	bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
143135732d01SWei Wang 					lockdep_is_held(&rt6_exception_lock));
143235732d01SWei Wang 	if (!bucket) {
143335732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
143435732d01SWei Wang 				 GFP_ATOMIC);
143535732d01SWei Wang 		if (!bucket) {
143635732d01SWei Wang 			err = -ENOMEM;
143735732d01SWei Wang 			goto out;
143835732d01SWei Wang 		}
143935732d01SWei Wang 		rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
144035732d01SWei Wang 	}
144135732d01SWei Wang 
144235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
144335732d01SWei Wang 	/* rt6i_src.plen != 0 indicates ort is in subtree
144435732d01SWei Wang 	 * and exception table is indexed by a hash of
144535732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
144635732d01SWei Wang 	 * Otherwise, the exception table is indexed by
144735732d01SWei Wang 	 * a hash of only rt6i_dst.
144835732d01SWei Wang 	 */
144993c2fb25SDavid Ahern 	if (ort->fib6_src.plen)
145035732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
145135732d01SWei Wang #endif
1452f5bbe7eeSWei Wang 	/* rt6_mtu_change() might lower mtu on ort.
1453f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
1454f5bbe7eeSWei Wang 	 * is less than ort's mtu value.
1455f5bbe7eeSWei Wang 	 */
1456d4ead6b3SDavid Ahern 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(ort)) {
1457f5bbe7eeSWei Wang 		err = -EINVAL;
1458f5bbe7eeSWei Wang 		goto out;
1459f5bbe7eeSWei Wang 	}
146060006a48SWei Wang 
146135732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
146235732d01SWei Wang 					       src_key);
146335732d01SWei Wang 	if (rt6_ex)
146435732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
146535732d01SWei Wang 
146635732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
146735732d01SWei Wang 	if (!rt6_ex) {
146835732d01SWei Wang 		err = -ENOMEM;
146935732d01SWei Wang 		goto out;
147035732d01SWei Wang 	}
147135732d01SWei Wang 	rt6_ex->rt6i = nrt;
147235732d01SWei Wang 	rt6_ex->stamp = jiffies;
147335732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
147435732d01SWei Wang 	bucket->depth++;
147581eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
147635732d01SWei Wang 
147735732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
147835732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
147935732d01SWei Wang 
148035732d01SWei Wang out:
148135732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
148235732d01SWei Wang 
148335732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1484b886d5f2SPaolo Abeni 	if (!err) {
148593c2fb25SDavid Ahern 		spin_lock_bh(&ort->fib6_table->tb6_lock);
14867aef6859SDavid Ahern 		fib6_update_sernum(net, ort);
148793c2fb25SDavid Ahern 		spin_unlock_bh(&ort->fib6_table->tb6_lock);
1488b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1489b886d5f2SPaolo Abeni 	}
149035732d01SWei Wang 
149135732d01SWei Wang 	return err;
149235732d01SWei Wang }
149335732d01SWei Wang 
14948d1c802bSDavid Ahern void rt6_flush_exceptions(struct fib6_info *rt)
149535732d01SWei Wang {
149635732d01SWei Wang 	struct rt6_exception_bucket *bucket;
149735732d01SWei Wang 	struct rt6_exception *rt6_ex;
149835732d01SWei Wang 	struct hlist_node *tmp;
149935732d01SWei Wang 	int i;
150035732d01SWei Wang 
150135732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
150235732d01SWei Wang 	/* Prevent rt6_insert_exception() to recreate the bucket list */
150335732d01SWei Wang 	rt->exception_bucket_flushed = 1;
150435732d01SWei Wang 
150535732d01SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
150635732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
150735732d01SWei Wang 	if (!bucket)
150835732d01SWei Wang 		goto out;
150935732d01SWei Wang 
151035732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
151135732d01SWei Wang 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
151235732d01SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
151335732d01SWei Wang 		WARN_ON_ONCE(bucket->depth);
151435732d01SWei Wang 		bucket++;
151535732d01SWei Wang 	}
151635732d01SWei Wang 
151735732d01SWei Wang out:
151835732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
151935732d01SWei Wang }
152035732d01SWei Wang 
152135732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
152235732d01SWei Wang  * Caller has to hold rcu_read_lock()
152335732d01SWei Wang  */
15248d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
152535732d01SWei Wang 					   struct in6_addr *daddr,
152635732d01SWei Wang 					   struct in6_addr *saddr)
152735732d01SWei Wang {
152835732d01SWei Wang 	struct rt6_exception_bucket *bucket;
152935732d01SWei Wang 	struct in6_addr *src_key = NULL;
153035732d01SWei Wang 	struct rt6_exception *rt6_ex;
153135732d01SWei Wang 	struct rt6_info *res = NULL;
153235732d01SWei Wang 
153335732d01SWei Wang 	bucket = rcu_dereference(rt->rt6i_exception_bucket);
153435732d01SWei Wang 
153535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
153635732d01SWei Wang 	/* rt6i_src.plen != 0 indicates rt is in subtree
153735732d01SWei Wang 	 * and exception table is indexed by a hash of
153835732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
153935732d01SWei Wang 	 * Otherwise, the exception table is indexed by
154035732d01SWei Wang 	 * a hash of only rt6i_dst.
154135732d01SWei Wang 	 */
154293c2fb25SDavid Ahern 	if (rt->fib6_src.plen)
154335732d01SWei Wang 		src_key = saddr;
154435732d01SWei Wang #endif
154535732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
154635732d01SWei Wang 
154735732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
154835732d01SWei Wang 		res = rt6_ex->rt6i;
154935732d01SWei Wang 
155035732d01SWei Wang 	return res;
155135732d01SWei Wang }
155235732d01SWei Wang 
155335732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
155423fb93a4SDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt)
155535732d01SWei Wang {
155635732d01SWei Wang 	struct rt6_exception_bucket *bucket;
155735732d01SWei Wang 	struct in6_addr *src_key = NULL;
155835732d01SWei Wang 	struct rt6_exception *rt6_ex;
15598a14e46fSDavid Ahern 	struct fib6_info *from;
156035732d01SWei Wang 	int err;
156135732d01SWei Wang 
1562091311deSEric Dumazet 	from = rcu_dereference(rt->from);
156335732d01SWei Wang 	if (!from ||
1564442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
156535732d01SWei Wang 		return -EINVAL;
156635732d01SWei Wang 
156735732d01SWei Wang 	if (!rcu_access_pointer(from->rt6i_exception_bucket))
156835732d01SWei Wang 		return -ENOENT;
156935732d01SWei Wang 
157035732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
157135732d01SWei Wang 	bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
157235732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
157335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
157435732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
157535732d01SWei Wang 	 * and exception table is indexed by a hash of
157635732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
157735732d01SWei Wang 	 * Otherwise, the exception table is indexed by
157835732d01SWei Wang 	 * a hash of only rt6i_dst.
157935732d01SWei Wang 	 */
158093c2fb25SDavid Ahern 	if (from->fib6_src.plen)
158135732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
158235732d01SWei Wang #endif
158335732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
158435732d01SWei Wang 					       &rt->rt6i_dst.addr,
158535732d01SWei Wang 					       src_key);
158635732d01SWei Wang 	if (rt6_ex) {
158735732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
158835732d01SWei Wang 		err = 0;
158935732d01SWei Wang 	} else {
159035732d01SWei Wang 		err = -ENOENT;
159135732d01SWei Wang 	}
159235732d01SWei Wang 
159335732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
159435732d01SWei Wang 	return err;
159535732d01SWei Wang }
159635732d01SWei Wang 
159735732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
159835732d01SWei Wang  * refresh its stamp
159935732d01SWei Wang  */
160035732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
160135732d01SWei Wang {
160235732d01SWei Wang 	struct rt6_exception_bucket *bucket;
16038d1c802bSDavid Ahern 	struct fib6_info *from = rt->from;
160435732d01SWei Wang 	struct in6_addr *src_key = NULL;
160535732d01SWei Wang 	struct rt6_exception *rt6_ex;
160635732d01SWei Wang 
160735732d01SWei Wang 	if (!from ||
1608442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
160935732d01SWei Wang 		return;
161035732d01SWei Wang 
161135732d01SWei Wang 	rcu_read_lock();
161235732d01SWei Wang 	bucket = rcu_dereference(from->rt6i_exception_bucket);
161335732d01SWei Wang 
161435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
161535732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
161635732d01SWei Wang 	 * and exception table is indexed by a hash of
161735732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
161835732d01SWei Wang 	 * Otherwise, the exception table is indexed by
161935732d01SWei Wang 	 * a hash of only rt6i_dst.
162035732d01SWei Wang 	 */
162193c2fb25SDavid Ahern 	if (from->fib6_src.plen)
162235732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
162335732d01SWei Wang #endif
162435732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket,
162535732d01SWei Wang 					  &rt->rt6i_dst.addr,
162635732d01SWei Wang 					  src_key);
162735732d01SWei Wang 	if (rt6_ex)
162835732d01SWei Wang 		rt6_ex->stamp = jiffies;
162935732d01SWei Wang 
163035732d01SWei Wang 	rcu_read_unlock();
163135732d01SWei Wang }
163235732d01SWei Wang 
1633e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1634e9fa1495SStefano Brivio 					 struct rt6_info *rt, int mtu)
1635e9fa1495SStefano Brivio {
1636e9fa1495SStefano Brivio 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1637e9fa1495SStefano Brivio 	 * lowest MTU in the path: always allow updating the route PMTU to
1638e9fa1495SStefano Brivio 	 * reflect PMTU decreases.
1639e9fa1495SStefano Brivio 	 *
1640e9fa1495SStefano Brivio 	 * If the new MTU is higher, and the route PMTU is equal to the local
1641e9fa1495SStefano Brivio 	 * MTU, this means the old MTU is the lowest in the path, so allow
1642e9fa1495SStefano Brivio 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1643e9fa1495SStefano Brivio 	 * handle this.
1644e9fa1495SStefano Brivio 	 */
1645e9fa1495SStefano Brivio 
1646e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) >= mtu)
1647e9fa1495SStefano Brivio 		return true;
1648e9fa1495SStefano Brivio 
1649e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1650e9fa1495SStefano Brivio 		return true;
1651e9fa1495SStefano Brivio 
1652e9fa1495SStefano Brivio 	return false;
1653e9fa1495SStefano Brivio }
1654e9fa1495SStefano Brivio 
1655e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
16568d1c802bSDavid Ahern 				       struct fib6_info *rt, int mtu)
1657f5bbe7eeSWei Wang {
1658f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
1659f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
1660f5bbe7eeSWei Wang 	int i;
1661f5bbe7eeSWei Wang 
1662f5bbe7eeSWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1663f5bbe7eeSWei Wang 					lockdep_is_held(&rt6_exception_lock));
1664f5bbe7eeSWei Wang 
1665e9fa1495SStefano Brivio 	if (!bucket)
1666e9fa1495SStefano Brivio 		return;
1667e9fa1495SStefano Brivio 
1668f5bbe7eeSWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1669f5bbe7eeSWei Wang 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1670f5bbe7eeSWei Wang 			struct rt6_info *entry = rt6_ex->rt6i;
1671e9fa1495SStefano Brivio 
1672e9fa1495SStefano Brivio 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
1673d4ead6b3SDavid Ahern 			 * route), the metrics of its rt->from have already
1674f5bbe7eeSWei Wang 			 * been updated.
1675f5bbe7eeSWei Wang 			 */
1676d4ead6b3SDavid Ahern 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
1677e9fa1495SStefano Brivio 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
1678d4ead6b3SDavid Ahern 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
1679f5bbe7eeSWei Wang 		}
1680f5bbe7eeSWei Wang 		bucket++;
1681f5bbe7eeSWei Wang 	}
1682f5bbe7eeSWei Wang }
1683f5bbe7eeSWei Wang 
1684b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
1685b16cb459SWei Wang 
16868d1c802bSDavid Ahern static void rt6_exceptions_clean_tohost(struct fib6_info *rt,
1687b16cb459SWei Wang 					struct in6_addr *gateway)
1688b16cb459SWei Wang {
1689b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
1690b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
1691b16cb459SWei Wang 	struct hlist_node *tmp;
1692b16cb459SWei Wang 	int i;
1693b16cb459SWei Wang 
1694b16cb459SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1695b16cb459SWei Wang 		return;
1696b16cb459SWei Wang 
1697b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1698b16cb459SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1699b16cb459SWei Wang 				     lockdep_is_held(&rt6_exception_lock));
1700b16cb459SWei Wang 
1701b16cb459SWei Wang 	if (bucket) {
1702b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1703b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1704b16cb459SWei Wang 						  &bucket->chain, hlist) {
1705b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1706b16cb459SWei Wang 
1707b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1708b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
1709b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
1710b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
1711b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
1712b16cb459SWei Wang 				}
1713b16cb459SWei Wang 			}
1714b16cb459SWei Wang 			bucket++;
1715b16cb459SWei Wang 		}
1716b16cb459SWei Wang 	}
1717b16cb459SWei Wang 
1718b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1719b16cb459SWei Wang }
1720b16cb459SWei Wang 
1721c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1722c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
1723c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
1724c757faa8SWei Wang 				      unsigned long now)
1725c757faa8SWei Wang {
1726c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
1727c757faa8SWei Wang 
17281859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
17291859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
17301859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
17311859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
17321859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
17331859bac0SPaolo Abeni 	 */
173431afeb42SWei Wang 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
173531afeb42SWei Wang 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1736c757faa8SWei Wang 			RT6_TRACE("aging clone %p\n", rt);
1737c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1738c757faa8SWei Wang 			return;
173931afeb42SWei Wang 		}
174031afeb42SWei Wang 	} else if (time_after(jiffies, rt->dst.expires)) {
174131afeb42SWei Wang 		RT6_TRACE("purging expired route %p\n", rt);
174231afeb42SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
174331afeb42SWei Wang 		return;
174431afeb42SWei Wang 	}
174531afeb42SWei Wang 
174631afeb42SWei Wang 	if (rt->rt6i_flags & RTF_GATEWAY) {
1747c757faa8SWei Wang 		struct neighbour *neigh;
1748c757faa8SWei Wang 		__u8 neigh_flags = 0;
1749c757faa8SWei Wang 
17501bfa26ffSEric Dumazet 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
17511bfa26ffSEric Dumazet 		if (neigh)
1752c757faa8SWei Wang 			neigh_flags = neigh->flags;
17531bfa26ffSEric Dumazet 
1754c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
1755c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
1756c757faa8SWei Wang 				  rt);
1757c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1758c757faa8SWei Wang 			return;
1759c757faa8SWei Wang 		}
1760c757faa8SWei Wang 	}
176131afeb42SWei Wang 
1762c757faa8SWei Wang 	gc_args->more++;
1763c757faa8SWei Wang }
1764c757faa8SWei Wang 
17658d1c802bSDavid Ahern void rt6_age_exceptions(struct fib6_info *rt,
1766c757faa8SWei Wang 			struct fib6_gc_args *gc_args,
1767c757faa8SWei Wang 			unsigned long now)
1768c757faa8SWei Wang {
1769c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
1770c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
1771c757faa8SWei Wang 	struct hlist_node *tmp;
1772c757faa8SWei Wang 	int i;
1773c757faa8SWei Wang 
1774c757faa8SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1775c757faa8SWei Wang 		return;
1776c757faa8SWei Wang 
17771bfa26ffSEric Dumazet 	rcu_read_lock_bh();
17781bfa26ffSEric Dumazet 	spin_lock(&rt6_exception_lock);
1779c757faa8SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1780c757faa8SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
1781c757faa8SWei Wang 
1782c757faa8SWei Wang 	if (bucket) {
1783c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1784c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1785c757faa8SWei Wang 						  &bucket->chain, hlist) {
1786c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
1787c757faa8SWei Wang 							  gc_args, now);
1788c757faa8SWei Wang 			}
1789c757faa8SWei Wang 			bucket++;
1790c757faa8SWei Wang 		}
1791c757faa8SWei Wang 	}
17921bfa26ffSEric Dumazet 	spin_unlock(&rt6_exception_lock);
17931bfa26ffSEric Dumazet 	rcu_read_unlock_bh();
1794c757faa8SWei Wang }
1795c757faa8SWei Wang 
17961d053da9SDavid Ahern /* must be called with rcu lock held */
17971d053da9SDavid Ahern struct fib6_info *fib6_table_lookup(struct net *net, struct fib6_table *table,
17981d053da9SDavid Ahern 				    int oif, struct flowi6 *fl6, int strict)
17991da177e4SLinus Torvalds {
1800367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
18018d1c802bSDavid Ahern 	struct fib6_info *f6i;
18021da177e4SLinus Torvalds 
18036454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1804367efcb9SMartin KaFai Lau 	saved_fn = fn;
18051da177e4SLinus Torvalds 
1806ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1807ca254490SDavid Ahern 		oif = 0;
1808ca254490SDavid Ahern 
1809a3c00e46SMartin KaFai Lau redo_rt6_select:
181023fb93a4SDavid Ahern 	f6i = rt6_select(net, fn, oif, strict);
181123fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1812a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1813a3c00e46SMartin KaFai Lau 		if (fn)
1814a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
1815367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
1816367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
1817367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
1818367efcb9SMartin KaFai Lau 			fn = saved_fn;
1819367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
1820367efcb9SMartin KaFai Lau 		}
1821a3c00e46SMartin KaFai Lau 	}
1822a3c00e46SMartin KaFai Lau 
1823d4bea421SDavid Ahern 	trace_fib6_table_lookup(net, f6i, table, fl6);
1824d52d3997SMartin KaFai Lau 
18251d053da9SDavid Ahern 	return f6i;
18261d053da9SDavid Ahern }
18271d053da9SDavid Ahern 
18281d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
18291d053da9SDavid Ahern 			       int oif, struct flowi6 *fl6,
18301d053da9SDavid Ahern 			       const struct sk_buff *skb, int flags)
18311d053da9SDavid Ahern {
18321d053da9SDavid Ahern 	struct fib6_info *f6i;
18331d053da9SDavid Ahern 	struct rt6_info *rt;
18341d053da9SDavid Ahern 	int strict = 0;
18351d053da9SDavid Ahern 
18361d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IFACE;
18371d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
18381d053da9SDavid Ahern 	if (net->ipv6.devconf_all->forwarding == 0)
18391d053da9SDavid Ahern 		strict |= RT6_LOOKUP_F_REACHABLE;
18401d053da9SDavid Ahern 
18411d053da9SDavid Ahern 	rcu_read_lock();
18421d053da9SDavid Ahern 
18431d053da9SDavid Ahern 	f6i = fib6_table_lookup(net, table, oif, fl6, strict);
18441d053da9SDavid Ahern 	if (f6i->fib6_nsiblings)
18451d053da9SDavid Ahern 		f6i = fib6_multipath_select(net, f6i, fl6, oif, skb, strict);
18461d053da9SDavid Ahern 
184723fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1848421842edSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
184966f5d6ceSWei Wang 		rcu_read_unlock();
1850d3843fe5SWei Wang 		dst_hold(&rt->dst);
1851d3843fe5SWei Wang 		return rt;
1852d3843fe5SWei Wang 	}
185323fb93a4SDavid Ahern 
185423fb93a4SDavid Ahern 	/*Search through exception table */
185523fb93a4SDavid Ahern 	rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
185623fb93a4SDavid Ahern 	if (rt) {
1857d4ead6b3SDavid Ahern 		if (ip6_hold_safe(net, &rt, true))
18581da177e4SLinus Torvalds 			dst_use_noref(&rt->dst, jiffies);
1859d4ead6b3SDavid Ahern 
186066f5d6ceSWei Wang 		rcu_read_unlock();
1861d52d3997SMartin KaFai Lau 		return rt;
18623da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
186393c2fb25SDavid Ahern 			    !(f6i->fib6_flags & RTF_GATEWAY))) {
18643da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
18653da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
18663da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
18673da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
18683da59bd9SMartin KaFai Lau 		 */
18693da59bd9SMartin KaFai Lau 		struct rt6_info *uncached_rt;
18703da59bd9SMartin KaFai Lau 
187123fb93a4SDavid Ahern 		uncached_rt = ip6_rt_cache_alloc(f6i, &fl6->daddr, NULL);
1872d52d3997SMartin KaFai Lau 
18734d85cd0cSDavid Ahern 		rcu_read_unlock();
18743da59bd9SMartin KaFai Lau 
18751cfb71eeSWei Wang 		if (uncached_rt) {
18761cfb71eeSWei Wang 			/* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
18771cfb71eeSWei Wang 			 * No need for another dst_hold()
18781cfb71eeSWei Wang 			 */
18798d0b94afSMartin KaFai Lau 			rt6_uncached_list_add(uncached_rt);
188081eb8447SWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
18811cfb71eeSWei Wang 		} else {
18823da59bd9SMartin KaFai Lau 			uncached_rt = net->ipv6.ip6_null_entry;
18833da59bd9SMartin KaFai Lau 			dst_hold(&uncached_rt->dst);
18841cfb71eeSWei Wang 		}
1885b811580dSDavid Ahern 
18863da59bd9SMartin KaFai Lau 		return uncached_rt;
1887d52d3997SMartin KaFai Lau 	} else {
1888d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
1889d52d3997SMartin KaFai Lau 
1890d52d3997SMartin KaFai Lau 		struct rt6_info *pcpu_rt;
1891d52d3997SMartin KaFai Lau 
1892951f788aSEric Dumazet 		local_bh_disable();
189323fb93a4SDavid Ahern 		pcpu_rt = rt6_get_pcpu_route(f6i);
1894d52d3997SMartin KaFai Lau 
189593531c67SDavid Ahern 		if (!pcpu_rt)
189623fb93a4SDavid Ahern 			pcpu_rt = rt6_make_pcpu_route(net, f6i);
189793531c67SDavid Ahern 
1898951f788aSEric Dumazet 		local_bh_enable();
1899951f788aSEric Dumazet 		rcu_read_unlock();
1900d4bea421SDavid Ahern 
1901d52d3997SMartin KaFai Lau 		return pcpu_rt;
1902d52d3997SMartin KaFai Lau 	}
1903c71099acSThomas Graf }
19049ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
1905c71099acSThomas Graf 
1906b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net,
1907b75cc8f9SDavid Ahern 					    struct fib6_table *table,
1908b75cc8f9SDavid Ahern 					    struct flowi6 *fl6,
1909b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
1910b75cc8f9SDavid Ahern 					    int flags)
19114acad72dSPavel Emelyanov {
1912b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
19134acad72dSPavel Emelyanov }
19144acad72dSPavel Emelyanov 
1915d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
191672331bc0SShmulik Ladkani 					 struct net_device *dev,
1917b75cc8f9SDavid Ahern 					 struct flowi6 *fl6,
1918b75cc8f9SDavid Ahern 					 const struct sk_buff *skb,
1919b75cc8f9SDavid Ahern 					 int flags)
192072331bc0SShmulik Ladkani {
192172331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
192272331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
192372331bc0SShmulik Ladkani 
1924b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
192572331bc0SShmulik Ladkani }
1926d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
192772331bc0SShmulik Ladkani 
192823aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
19295e5d6fedSRoopa Prabhu 				  struct flow_keys *keys,
19305e5d6fedSRoopa Prabhu 				  struct flow_keys *flkeys)
193123aebdacSJakub Sitnicki {
193223aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
193323aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
19345e5d6fedSRoopa Prabhu 	struct flow_keys *_flkeys = flkeys;
193523aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
193623aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
193723aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
1938cea67a2dSEric Dumazet 	struct icmp6hdr _icmph;
193923aebdacSJakub Sitnicki 
194023aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
194123aebdacSJakub Sitnicki 		goto out;
194223aebdacSJakub Sitnicki 
1943cea67a2dSEric Dumazet 	icmph = skb_header_pointer(skb, skb_transport_offset(skb),
1944cea67a2dSEric Dumazet 				   sizeof(_icmph), &_icmph);
1945cea67a2dSEric Dumazet 	if (!icmph)
1946cea67a2dSEric Dumazet 		goto out;
1947cea67a2dSEric Dumazet 
194823aebdacSJakub Sitnicki 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
194923aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
195023aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
195123aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
195223aebdacSJakub Sitnicki 		goto out;
195323aebdacSJakub Sitnicki 
195423aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
195523aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
195623aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
195723aebdacSJakub Sitnicki 	if (!inner_iph)
195823aebdacSJakub Sitnicki 		goto out;
195923aebdacSJakub Sitnicki 
196023aebdacSJakub Sitnicki 	key_iph = inner_iph;
19615e5d6fedSRoopa Prabhu 	_flkeys = NULL;
196223aebdacSJakub Sitnicki out:
19635e5d6fedSRoopa Prabhu 	if (_flkeys) {
19645e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
19655e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
19665e5d6fedSRoopa Prabhu 		keys->tags.flow_label = _flkeys->tags.flow_label;
19675e5d6fedSRoopa Prabhu 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
19685e5d6fedSRoopa Prabhu 	} else {
196923aebdacSJakub Sitnicki 		keys->addrs.v6addrs.src = key_iph->saddr;
197023aebdacSJakub Sitnicki 		keys->addrs.v6addrs.dst = key_iph->daddr;
1971fa1be7e0SMichal Kubecek 		keys->tags.flow_label = ip6_flowlabel(key_iph);
197223aebdacSJakub Sitnicki 		keys->basic.ip_proto = key_iph->nexthdr;
197323aebdacSJakub Sitnicki 	}
19745e5d6fedSRoopa Prabhu }
197523aebdacSJakub Sitnicki 
197623aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
1977b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
1978b4bac172SDavid Ahern 		       const struct sk_buff *skb, struct flow_keys *flkeys)
197923aebdacSJakub Sitnicki {
198023aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
19819a2a537aSDavid Ahern 	u32 mhash;
198223aebdacSJakub Sitnicki 
1983bbfa047aSDavid S. Miller 	switch (ip6_multipath_hash_policy(net)) {
1984b4bac172SDavid Ahern 	case 0:
19856f74b6c2SDavid Ahern 		memset(&hash_keys, 0, sizeof(hash_keys));
19866f74b6c2SDavid Ahern 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
19879a2a537aSDavid Ahern 		if (skb) {
19885e5d6fedSRoopa Prabhu 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
19899a2a537aSDavid Ahern 		} else {
19909a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
19919a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
1992fa1be7e0SMichal Kubecek 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
19939a2a537aSDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
199423aebdacSJakub Sitnicki 		}
1995b4bac172SDavid Ahern 		break;
1996b4bac172SDavid Ahern 	case 1:
1997b4bac172SDavid Ahern 		if (skb) {
1998b4bac172SDavid Ahern 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
1999b4bac172SDavid Ahern 			struct flow_keys keys;
2000b4bac172SDavid Ahern 
2001b4bac172SDavid Ahern 			/* short-circuit if we already have L4 hash present */
2002b4bac172SDavid Ahern 			if (skb->l4_hash)
2003b4bac172SDavid Ahern 				return skb_get_hash_raw(skb) >> 1;
2004b4bac172SDavid Ahern 
2005b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2006b4bac172SDavid Ahern 
2007b4bac172SDavid Ahern                         if (!flkeys) {
2008b4bac172SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2009b4bac172SDavid Ahern 				flkeys = &keys;
2010b4bac172SDavid Ahern 			}
2011b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2012b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2013b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2014b4bac172SDavid Ahern 			hash_keys.ports.src = flkeys->ports.src;
2015b4bac172SDavid Ahern 			hash_keys.ports.dst = flkeys->ports.dst;
2016b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2017b4bac172SDavid Ahern 		} else {
2018b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2019b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2020b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2021b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2022b4bac172SDavid Ahern 			hash_keys.ports.src = fl6->fl6_sport;
2023b4bac172SDavid Ahern 			hash_keys.ports.dst = fl6->fl6_dport;
2024b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2025b4bac172SDavid Ahern 		}
2026b4bac172SDavid Ahern 		break;
2027b4bac172SDavid Ahern 	}
20289a2a537aSDavid Ahern 	mhash = flow_hash_from_keys(&hash_keys);
202923aebdacSJakub Sitnicki 
20309a2a537aSDavid Ahern 	return mhash >> 1;
203123aebdacSJakub Sitnicki }
203223aebdacSJakub Sitnicki 
2033c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
2034c71099acSThomas Graf {
2035b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2036c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
2037adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2038904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
20394c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
2040e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
20414c9483b2SDavid S. Miller 		.daddr = iph->daddr,
20424c9483b2SDavid S. Miller 		.saddr = iph->saddr,
20436502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
20444c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
20454c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
2046c71099acSThomas Graf 	};
20475e5d6fedSRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2048adaa70bbSThomas Graf 
2049904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
205046fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2051904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
20525e5d6fedSRoopa Prabhu 
20535e5d6fedSRoopa Prabhu 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
20545e5d6fedSRoopa Prabhu 		flkeys = &_flkeys;
20555e5d6fedSRoopa Prabhu 
205623aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2057b4bac172SDavid Ahern 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
205806e9d040SJiri Benc 	skb_dst_drop(skb);
2059b75cc8f9SDavid Ahern 	skb_dst_set(skb,
2060b75cc8f9SDavid Ahern 		    ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
2061c71099acSThomas Graf }
2062c71099acSThomas Graf 
2063b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net,
2064b75cc8f9SDavid Ahern 					     struct fib6_table *table,
2065b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
2066b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2067b75cc8f9SDavid Ahern 					     int flags)
2068c71099acSThomas Graf {
2069b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2070c71099acSThomas Graf }
2071c71099acSThomas Graf 
20726f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
20736f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags)
2074c71099acSThomas Graf {
2075d46a9d67SDavid Ahern 	bool any_src;
2076c71099acSThomas Graf 
20773ede0bbcSRobert Shearman 	if (ipv6_addr_type(&fl6->daddr) &
20783ede0bbcSRobert Shearman 	    (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
20794c1feac5SDavid Ahern 		struct dst_entry *dst;
20804c1feac5SDavid Ahern 
20814c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
2082ca254490SDavid Ahern 		if (dst)
2083ca254490SDavid Ahern 			return dst;
20844c1feac5SDavid Ahern 	}
2085ca254490SDavid Ahern 
20861fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
20874dc27d1cSDavid McCullough 
2088d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
2089741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2090d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
209177d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
2092c71099acSThomas Graf 
2093d46a9d67SDavid Ahern 	if (!any_src)
2094adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
20950c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
20960c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2097adaa70bbSThomas Graf 
2098b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
20991da177e4SLinus Torvalds }
21006f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
21011da177e4SLinus Torvalds 
21022774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
210314e50e57SDavid S. Miller {
21045c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
21051dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
210614e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
210714e50e57SDavid S. Miller 
21081dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
210962cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
211014e50e57SDavid S. Miller 	if (rt) {
21110a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
211281eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
21130a1f5962SMartin KaFai Lau 
2114d8d1f30bSChangli Gao 		new = &rt->dst;
211514e50e57SDavid S. Miller 		new->__use = 1;
2116352e512cSHerbert Xu 		new->input = dst_discard;
2117ede2059dSEric W. Biederman 		new->output = dst_discard_out;
211814e50e57SDavid S. Miller 
2119defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
212014e50e57SDavid S. Miller 
21211dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
21224e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
21230a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
212414e50e57SDavid S. Miller 
212514e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
212614e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
212714e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
212814e50e57SDavid S. Miller #endif
212914e50e57SDavid S. Miller 	}
213014e50e57SDavid S. Miller 
213169ead7afSDavid S. Miller 	dst_release(dst_orig);
213269ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
213314e50e57SDavid S. Miller }
213414e50e57SDavid S. Miller 
21351da177e4SLinus Torvalds /*
21361da177e4SLinus Torvalds  *	Destination cache support functions
21371da177e4SLinus Torvalds  */
21381da177e4SLinus Torvalds 
21398d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie)
21403da59bd9SMartin KaFai Lau {
214136143645SSteffen Klassert 	u32 rt_cookie = 0;
2142c5cff856SWei Wang 
21438ae86971SDavid Ahern 	if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
214493531c67SDavid Ahern 		return false;
214593531c67SDavid Ahern 
214693531c67SDavid Ahern 	if (fib6_check_expired(f6i))
214793531c67SDavid Ahern 		return false;
214893531c67SDavid Ahern 
214993531c67SDavid Ahern 	return true;
215093531c67SDavid Ahern }
215193531c67SDavid Ahern 
2152a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt,
2153a68886a6SDavid Ahern 				   struct fib6_info *from,
2154a68886a6SDavid Ahern 				   u32 cookie)
21553da59bd9SMartin KaFai Lau {
2156c5cff856SWei Wang 	u32 rt_cookie = 0;
2157c5cff856SWei Wang 
2158a68886a6SDavid Ahern 	if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
215993531c67SDavid Ahern 	    rt_cookie != cookie)
21603da59bd9SMartin KaFai Lau 		return NULL;
21613da59bd9SMartin KaFai Lau 
21623da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
21633da59bd9SMartin KaFai Lau 		return NULL;
21643da59bd9SMartin KaFai Lau 
21653da59bd9SMartin KaFai Lau 	return &rt->dst;
21663da59bd9SMartin KaFai Lau }
21673da59bd9SMartin KaFai Lau 
2168a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2169a68886a6SDavid Ahern 					    struct fib6_info *from,
2170a68886a6SDavid Ahern 					    u32 cookie)
21713da59bd9SMartin KaFai Lau {
21725973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
21735973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2174a68886a6SDavid Ahern 	    fib6_check(from, cookie))
21753da59bd9SMartin KaFai Lau 		return &rt->dst;
21763da59bd9SMartin KaFai Lau 	else
21773da59bd9SMartin KaFai Lau 		return NULL;
21783da59bd9SMartin KaFai Lau }
21793da59bd9SMartin KaFai Lau 
21801da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
21811da177e4SLinus Torvalds {
2182a87b7dc9SDavid Ahern 	struct dst_entry *dst_ret;
2183a68886a6SDavid Ahern 	struct fib6_info *from;
21841da177e4SLinus Torvalds 	struct rt6_info *rt;
21851da177e4SLinus Torvalds 
2186a87b7dc9SDavid Ahern 	rt = container_of(dst, struct rt6_info, dst);
2187a87b7dc9SDavid Ahern 
2188a87b7dc9SDavid Ahern 	rcu_read_lock();
21891da177e4SLinus Torvalds 
21906f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
21916f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
21926f3118b5SNicolas Dichtel 	 * into this function always.
21936f3118b5SNicolas Dichtel 	 */
2194e3bc10bdSHannes Frederic Sowa 
2195a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
21964b32b5adSMartin KaFai Lau 
2197a68886a6SDavid Ahern 	if (from && (rt->rt6i_flags & RTF_PCPU ||
2198a68886a6SDavid Ahern 	    unlikely(!list_empty(&rt->rt6i_uncached))))
2199a68886a6SDavid Ahern 		dst_ret = rt6_dst_from_check(rt, from, cookie);
22003da59bd9SMartin KaFai Lau 	else
2201a68886a6SDavid Ahern 		dst_ret = rt6_check(rt, from, cookie);
2202a87b7dc9SDavid Ahern 
2203a87b7dc9SDavid Ahern 	rcu_read_unlock();
2204a87b7dc9SDavid Ahern 
2205a87b7dc9SDavid Ahern 	return dst_ret;
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
22091da177e4SLinus Torvalds {
22101da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds 	if (rt) {
221354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
2214c3c14da0SDavid Ahern 			rcu_read_lock();
221554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
221693531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
221754c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
22181da177e4SLinus Torvalds 			}
2219c3c14da0SDavid Ahern 			rcu_read_unlock();
222054c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
222154c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
222254c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
222354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
222454c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
222554c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
22261da177e4SLinus Torvalds }
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
22291da177e4SLinus Torvalds {
22301da177e4SLinus Torvalds 	struct rt6_info *rt;
22311da177e4SLinus Torvalds 
22323ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
22331da177e4SLinus Torvalds 
2234adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
22351da177e4SLinus Torvalds 	if (rt) {
22368a14e46fSDavid Ahern 		rcu_read_lock();
22371eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
2238ad65a2f0SWei Wang 			if (dst_hold_safe(&rt->dst))
223993531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
2240c5cff856SWei Wang 		} else {
2241a68886a6SDavid Ahern 			struct fib6_info *from;
2242c5cff856SWei Wang 			struct fib6_node *fn;
2243c5cff856SWei Wang 
2244a68886a6SDavid Ahern 			from = rcu_dereference(rt->from);
2245a68886a6SDavid Ahern 			if (from) {
2246a68886a6SDavid Ahern 				fn = rcu_dereference(from->fib6_node);
2247c5cff856SWei Wang 				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2248c5cff856SWei Wang 					fn->fn_sernum = -1;
2249a68886a6SDavid Ahern 			}
22501da177e4SLinus Torvalds 		}
22511da177e4SLinus Torvalds 		rcu_read_unlock();
22521da177e4SLinus Torvalds 	}
22531da177e4SLinus Torvalds }
22541da177e4SLinus Torvalds 
22556a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout)
22566a3e030fSDavid Ahern {
2257a68886a6SDavid Ahern 	if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2258a68886a6SDavid Ahern 		struct fib6_info *from;
2259a68886a6SDavid Ahern 
2260a68886a6SDavid Ahern 		rcu_read_lock();
2261a68886a6SDavid Ahern 		from = rcu_dereference(rt0->from);
2262a68886a6SDavid Ahern 		if (from)
2263a68886a6SDavid Ahern 			rt0->dst.expires = from->expires;
2264a68886a6SDavid Ahern 		rcu_read_unlock();
2265a68886a6SDavid Ahern 	}
22666a3e030fSDavid Ahern 
22676a3e030fSDavid Ahern 	dst_set_expires(&rt0->dst, timeout);
22686a3e030fSDavid Ahern 	rt0->rt6i_flags |= RTF_EXPIRES;
22696700c270SDavid S. Miller }
22701da177e4SLinus Torvalds 
227145e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
227245e4fd26SMartin KaFai Lau {
227345e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
227445e4fd26SMartin KaFai Lau 
2275d4ead6b3SDavid Ahern 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
227645e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
227745e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
227845e4fd26SMartin KaFai Lau }
227945e4fd26SMartin KaFai Lau 
22800d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
22810d3f6d29SMartin KaFai Lau {
2282a68886a6SDavid Ahern 	bool from_set;
2283a68886a6SDavid Ahern 
2284a68886a6SDavid Ahern 	rcu_read_lock();
2285a68886a6SDavid Ahern 	from_set = !!rcu_dereference(rt->from);
2286a68886a6SDavid Ahern 	rcu_read_unlock();
2287a68886a6SDavid Ahern 
22880d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
2289a68886a6SDavid Ahern 		(rt->rt6i_flags & RTF_PCPU || from_set);
22900d3f6d29SMartin KaFai Lau }
22910d3f6d29SMartin KaFai Lau 
229245e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
229345e4fd26SMartin KaFai Lau 				 const struct ipv6hdr *iph, u32 mtu)
22941da177e4SLinus Torvalds {
22950dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
22961da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
22971da177e4SLinus Torvalds 
229819bda36cSXin Long 	if (dst_metric_locked(dst, RTAX_MTU))
229919bda36cSXin Long 		return;
230019bda36cSXin Long 
230145e4fd26SMartin KaFai Lau 	if (iph) {
230245e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
230345e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
230445e4fd26SMartin KaFai Lau 	} else if (sk) {
230545e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
230645e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
230745e4fd26SMartin KaFai Lau 	} else {
23080dec879fSJulian Anastasov 		daddr = NULL;
23090dec879fSJulian Anastasov 		saddr = NULL;
23101da177e4SLinus Torvalds 	}
23110dec879fSJulian Anastasov 	dst_confirm_neigh(dst, daddr);
23120dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
23130dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
23140dec879fSJulian Anastasov 		return;
23150dec879fSJulian Anastasov 
23160dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
23170dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
23182b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
23192b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
23202b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
23210dec879fSJulian Anastasov 	} else if (daddr) {
2322a68886a6SDavid Ahern 		struct fib6_info *from;
23230dec879fSJulian Anastasov 		struct rt6_info *nrt6;
23240dec879fSJulian Anastasov 
23254d85cd0cSDavid Ahern 		rcu_read_lock();
2326a68886a6SDavid Ahern 		from = rcu_dereference(rt6->from);
2327a68886a6SDavid Ahern 		nrt6 = ip6_rt_cache_alloc(from, daddr, saddr);
232845e4fd26SMartin KaFai Lau 		if (nrt6) {
232945e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
2330a68886a6SDavid Ahern 			if (rt6_insert_exception(nrt6, from))
23312b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
233245e4fd26SMartin KaFai Lau 		}
2333a68886a6SDavid Ahern 		rcu_read_unlock();
233445e4fd26SMartin KaFai Lau 	}
233545e4fd26SMartin KaFai Lau }
233645e4fd26SMartin KaFai Lau 
233745e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
233845e4fd26SMartin KaFai Lau 			       struct sk_buff *skb, u32 mtu)
233945e4fd26SMartin KaFai Lau {
234045e4fd26SMartin KaFai Lau 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
23411da177e4SLinus Torvalds }
23421da177e4SLinus Torvalds 
234342ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2344e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
234581aded24SDavid S. Miller {
234681aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
234781aded24SDavid S. Miller 	struct dst_entry *dst;
2348dc92095dSMaciej Żenczykowski 	struct flowi6 fl6 = {
2349dc92095dSMaciej Żenczykowski 		.flowi6_oif = oif,
2350dc92095dSMaciej Żenczykowski 		.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2351dc92095dSMaciej Żenczykowski 		.daddr = iph->daddr,
2352dc92095dSMaciej Żenczykowski 		.saddr = iph->saddr,
2353dc92095dSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
2354dc92095dSMaciej Żenczykowski 		.flowi6_uid = uid,
2355dc92095dSMaciej Żenczykowski 	};
235681aded24SDavid S. Miller 
235781aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
235881aded24SDavid S. Miller 	if (!dst->error)
235945e4fd26SMartin KaFai Lau 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
236081aded24SDavid S. Miller 	dst_release(dst);
236181aded24SDavid S. Miller }
236281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
236381aded24SDavid S. Miller 
236481aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
236581aded24SDavid S. Miller {
236633c162a9SMartin KaFai Lau 	struct dst_entry *dst;
236733c162a9SMartin KaFai Lau 
236881aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
2369e2d118a1SLorenzo Colitti 			sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid);
237033c162a9SMartin KaFai Lau 
237133c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
237233c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
237333c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
237433c162a9SMartin KaFai Lau 		return;
237533c162a9SMartin KaFai Lau 
237633c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
237733c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
237833c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
237933c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
238081aded24SDavid S. Miller }
238181aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
238281aded24SDavid S. Miller 
23837d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
23847d6850f7SAlexey Kodanev 			   const struct flowi6 *fl6)
23857d6850f7SAlexey Kodanev {
23867d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
23877d6850f7SAlexey Kodanev 	struct ipv6_pinfo *np = inet6_sk(sk);
23887d6850f7SAlexey Kodanev #endif
23897d6850f7SAlexey Kodanev 
23907d6850f7SAlexey Kodanev 	ip6_dst_store(sk, dst,
23917d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
23927d6850f7SAlexey Kodanev 		      &sk->sk_v6_daddr : NULL,
23937d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
23947d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
23957d6850f7SAlexey Kodanev 		      &np->saddr :
23967d6850f7SAlexey Kodanev #endif
23977d6850f7SAlexey Kodanev 		      NULL);
23987d6850f7SAlexey Kodanev }
23997d6850f7SAlexey Kodanev 
2400b55b76b2SDuan Jiong /* Handle redirects */
2401b55b76b2SDuan Jiong struct ip6rd_flowi {
2402b55b76b2SDuan Jiong 	struct flowi6 fl6;
2403b55b76b2SDuan Jiong 	struct in6_addr gateway;
2404b55b76b2SDuan Jiong };
2405b55b76b2SDuan Jiong 
2406b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
2407b55b76b2SDuan Jiong 					     struct fib6_table *table,
2408b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2409b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2410b55b76b2SDuan Jiong 					     int flags)
2411b55b76b2SDuan Jiong {
2412b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
241323fb93a4SDavid Ahern 	struct rt6_info *ret = NULL, *rt_cache;
24148d1c802bSDavid Ahern 	struct fib6_info *rt;
2415b55b76b2SDuan Jiong 	struct fib6_node *fn;
2416b55b76b2SDuan Jiong 
2417b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
241867c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2419b55b76b2SDuan Jiong 	 *
2420b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2421b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2422b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2423b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2424b55b76b2SDuan Jiong 	 * routes.
2425b55b76b2SDuan Jiong 	 */
2426b55b76b2SDuan Jiong 
242766f5d6ceSWei Wang 	rcu_read_lock();
24286454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2429b55b76b2SDuan Jiong restart:
243066f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
24315e670d84SDavid Ahern 		if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
24328067bb8cSIdo Schimmel 			continue;
243314895687SDavid Ahern 		if (fib6_check_expired(rt))
2434b55b76b2SDuan Jiong 			continue;
243593c2fb25SDavid Ahern 		if (rt->fib6_flags & RTF_REJECT)
2436b55b76b2SDuan Jiong 			break;
243793c2fb25SDavid Ahern 		if (!(rt->fib6_flags & RTF_GATEWAY))
2438b55b76b2SDuan Jiong 			continue;
24395e670d84SDavid Ahern 		if (fl6->flowi6_oif != rt->fib6_nh.nh_dev->ifindex)
2440b55b76b2SDuan Jiong 			continue;
24412b760fcfSWei Wang 		/* rt_cache's gateway might be different from its 'parent'
24422b760fcfSWei Wang 		 * in the case of an ip redirect.
24432b760fcfSWei Wang 		 * So we keep searching in the exception table if the gateway
24442b760fcfSWei Wang 		 * is different.
24452b760fcfSWei Wang 		 */
24465e670d84SDavid Ahern 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.nh_gw)) {
24472b760fcfSWei Wang 			rt_cache = rt6_find_cached_rt(rt,
24482b760fcfSWei Wang 						      &fl6->daddr,
24492b760fcfSWei Wang 						      &fl6->saddr);
24502b760fcfSWei Wang 			if (rt_cache &&
24512b760fcfSWei Wang 			    ipv6_addr_equal(&rdfl->gateway,
24522b760fcfSWei Wang 					    &rt_cache->rt6i_gateway)) {
245323fb93a4SDavid Ahern 				ret = rt_cache;
24542b760fcfSWei Wang 				break;
24552b760fcfSWei Wang 			}
2456b55b76b2SDuan Jiong 			continue;
24572b760fcfSWei Wang 		}
2458b55b76b2SDuan Jiong 		break;
2459b55b76b2SDuan Jiong 	}
2460b55b76b2SDuan Jiong 
2461b55b76b2SDuan Jiong 	if (!rt)
2462421842edSDavid Ahern 		rt = net->ipv6.fib6_null_entry;
246393c2fb25SDavid Ahern 	else if (rt->fib6_flags & RTF_REJECT) {
246423fb93a4SDavid Ahern 		ret = net->ipv6.ip6_null_entry;
2465b0a1ba59SMartin KaFai Lau 		goto out;
2466b0a1ba59SMartin KaFai Lau 	}
2467b0a1ba59SMartin KaFai Lau 
2468421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
2469a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2470a3c00e46SMartin KaFai Lau 		if (fn)
2471a3c00e46SMartin KaFai Lau 			goto restart;
2472b55b76b2SDuan Jiong 	}
2473a3c00e46SMartin KaFai Lau 
2474b0a1ba59SMartin KaFai Lau out:
247523fb93a4SDavid Ahern 	if (ret)
2476e873e4b9SWei Wang 		ip6_hold_safe(net, &ret, true);
247723fb93a4SDavid Ahern 	else
247823fb93a4SDavid Ahern 		ret = ip6_create_rt_rcu(rt);
2479b55b76b2SDuan Jiong 
248066f5d6ceSWei Wang 	rcu_read_unlock();
2481b55b76b2SDuan Jiong 
2482b65f164dSPaolo Abeni 	trace_fib6_table_lookup(net, rt, table, fl6);
248323fb93a4SDavid Ahern 	return ret;
2484b55b76b2SDuan Jiong };
2485b55b76b2SDuan Jiong 
2486b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
2487b55b76b2SDuan Jiong 					    const struct flowi6 *fl6,
2488b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2489b55b76b2SDuan Jiong 					    const struct in6_addr *gateway)
2490b55b76b2SDuan Jiong {
2491b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2492b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
2493b55b76b2SDuan Jiong 
2494b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
2495b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
2496b55b76b2SDuan Jiong 
2497b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, &rdfl.fl6, skb,
2498b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
2499b55b76b2SDuan Jiong }
2500b55b76b2SDuan Jiong 
2501e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2502e2d118a1SLorenzo Colitti 		  kuid_t uid)
25033a5ad2eeSDavid S. Miller {
25043a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
25053a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
25061f7f10acSMaciej Żenczykowski 	struct flowi6 fl6 = {
25071f7f10acSMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
25081f7f10acSMaciej Żenczykowski 		.flowi6_oif = oif,
25091f7f10acSMaciej Żenczykowski 		.flowi6_mark = mark,
25101f7f10acSMaciej Żenczykowski 		.daddr = iph->daddr,
25111f7f10acSMaciej Żenczykowski 		.saddr = iph->saddr,
25121f7f10acSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
25131f7f10acSMaciej Żenczykowski 		.flowi6_uid = uid,
25141f7f10acSMaciej Żenczykowski 	};
25153a5ad2eeSDavid S. Miller 
2516b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
25176700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
25183a5ad2eeSDavid S. Miller 	dst_release(dst);
25193a5ad2eeSDavid S. Miller }
25203a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
25213a5ad2eeSDavid S. Miller 
2522d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
2523c92a59ecSDuan Jiong {
2524c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2525c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2526c92a59ecSDuan Jiong 	struct dst_entry *dst;
25270b26fb17SMaciej Żenczykowski 	struct flowi6 fl6 = {
25280b26fb17SMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
25290b26fb17SMaciej Żenczykowski 		.flowi6_oif = oif,
25300b26fb17SMaciej Żenczykowski 		.daddr = msg->dest,
25310b26fb17SMaciej Żenczykowski 		.saddr = iph->daddr,
25320b26fb17SMaciej Żenczykowski 		.flowi6_uid = sock_net_uid(net, NULL),
25330b26fb17SMaciej Żenczykowski 	};
2534c92a59ecSDuan Jiong 
2535b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
2536c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
2537c92a59ecSDuan Jiong 	dst_release(dst);
2538c92a59ecSDuan Jiong }
2539c92a59ecSDuan Jiong 
25403a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
25413a5ad2eeSDavid S. Miller {
2542e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2543e2d118a1SLorenzo Colitti 		     sk->sk_uid);
25443a5ad2eeSDavid S. Miller }
25453a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
25463a5ad2eeSDavid S. Miller 
25470dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
25481da177e4SLinus Torvalds {
25490dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
25500dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
25510dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
25520dbaee3bSDavid S. Miller 
25531da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
25541da177e4SLinus Torvalds 
25555578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
25565578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds 	/*
25591da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
25601da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
25611da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
25621da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
25631da177e4SLinus Torvalds 	 */
25641da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
25651da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
25661da177e4SLinus Torvalds 	return mtu;
25671da177e4SLinus Torvalds }
25681da177e4SLinus Torvalds 
2569ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
2570d33e4553SDavid S. Miller {
2571d33e4553SDavid S. Miller 	struct inet6_dev *idev;
2572d4ead6b3SDavid Ahern 	unsigned int mtu;
2573618f9bc7SSteffen Klassert 
25744b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
25754b32b5adSMartin KaFai Lau 	if (mtu)
25764b32b5adSMartin KaFai Lau 		goto out;
25774b32b5adSMartin KaFai Lau 
2578618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
2579d33e4553SDavid S. Miller 
2580d33e4553SDavid S. Miller 	rcu_read_lock();
2581d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
2582d33e4553SDavid S. Miller 	if (idev)
2583d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
2584d33e4553SDavid S. Miller 	rcu_read_unlock();
2585d33e4553SDavid S. Miller 
258630f78d8eSEric Dumazet out:
258714972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
258814972cbdSRoopa Prabhu 
258914972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
2590d33e4553SDavid S. Miller }
2591d33e4553SDavid S. Miller 
2592901731b8SDavid Ahern /* MTU selection:
2593901731b8SDavid Ahern  * 1. mtu on route is locked - use it
2594901731b8SDavid Ahern  * 2. mtu from nexthop exception
2595901731b8SDavid Ahern  * 3. mtu from egress device
2596901731b8SDavid Ahern  *
2597901731b8SDavid Ahern  * based on ip6_dst_mtu_forward and exception logic of
2598901731b8SDavid Ahern  * rt6_find_cached_rt; called with rcu_read_lock
2599901731b8SDavid Ahern  */
2600901731b8SDavid Ahern u32 ip6_mtu_from_fib6(struct fib6_info *f6i, struct in6_addr *daddr,
2601901731b8SDavid Ahern 		      struct in6_addr *saddr)
2602901731b8SDavid Ahern {
2603901731b8SDavid Ahern 	struct rt6_exception_bucket *bucket;
2604901731b8SDavid Ahern 	struct rt6_exception *rt6_ex;
2605901731b8SDavid Ahern 	struct in6_addr *src_key;
2606901731b8SDavid Ahern 	struct inet6_dev *idev;
2607901731b8SDavid Ahern 	u32 mtu = 0;
2608901731b8SDavid Ahern 
2609901731b8SDavid Ahern 	if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
2610901731b8SDavid Ahern 		mtu = f6i->fib6_pmtu;
2611901731b8SDavid Ahern 		if (mtu)
2612901731b8SDavid Ahern 			goto out;
2613901731b8SDavid Ahern 	}
2614901731b8SDavid Ahern 
2615901731b8SDavid Ahern 	src_key = NULL;
2616901731b8SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
2617901731b8SDavid Ahern 	if (f6i->fib6_src.plen)
2618901731b8SDavid Ahern 		src_key = saddr;
2619901731b8SDavid Ahern #endif
2620901731b8SDavid Ahern 
2621901731b8SDavid Ahern 	bucket = rcu_dereference(f6i->rt6i_exception_bucket);
2622901731b8SDavid Ahern 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
2623901731b8SDavid Ahern 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
2624901731b8SDavid Ahern 		mtu = dst_metric_raw(&rt6_ex->rt6i->dst, RTAX_MTU);
2625901731b8SDavid Ahern 
2626901731b8SDavid Ahern 	if (likely(!mtu)) {
2627901731b8SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(f6i);
2628901731b8SDavid Ahern 
2629901731b8SDavid Ahern 		mtu = IPV6_MIN_MTU;
2630901731b8SDavid Ahern 		idev = __in6_dev_get(dev);
2631901731b8SDavid Ahern 		if (idev && idev->cnf.mtu6 > mtu)
2632901731b8SDavid Ahern 			mtu = idev->cnf.mtu6;
2633901731b8SDavid Ahern 	}
2634901731b8SDavid Ahern 
2635901731b8SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2636901731b8SDavid Ahern out:
2637901731b8SDavid Ahern 	return mtu - lwtunnel_headroom(fib6_info_nh_lwt(f6i), mtu);
2638901731b8SDavid Ahern }
2639901731b8SDavid Ahern 
26403b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
264187a11578SDavid S. Miller 				  struct flowi6 *fl6)
26421da177e4SLinus Torvalds {
264387a11578SDavid S. Miller 	struct dst_entry *dst;
26441da177e4SLinus Torvalds 	struct rt6_info *rt;
26451da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
2646c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26471da177e4SLinus Torvalds 
264838308473SDavid S. Miller 	if (unlikely(!idev))
2649122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
26501da177e4SLinus Torvalds 
2651ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
265238308473SDavid S. Miller 	if (unlikely(!rt)) {
26531da177e4SLinus Torvalds 		in6_dev_put(idev);
265487a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
26551da177e4SLinus Torvalds 		goto out;
26561da177e4SLinus Torvalds 	}
26571da177e4SLinus Torvalds 
26588e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
2659588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
26608e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
2661550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
266287a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
26638e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
26648e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
266514edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
26661da177e4SLinus Torvalds 
26674c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
2668587fea74SWei Wang 	 * do proper release of the net_device
2669587fea74SWei Wang 	 */
2670587fea74SWei Wang 	rt6_uncached_list_add(rt);
267181eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
26721da177e4SLinus Torvalds 
267387a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
267487a11578SDavid S. Miller 
26751da177e4SLinus Torvalds out:
267687a11578SDavid S. Miller 	return dst;
26771da177e4SLinus Torvalds }
26781da177e4SLinus Torvalds 
2679569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
26801da177e4SLinus Torvalds {
268186393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
26827019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
26837019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
26847019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
26857019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
26867019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
2687fc66f95cSEric Dumazet 	int entries;
26881da177e4SLinus Torvalds 
2689fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
269049a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
2691fc66f95cSEric Dumazet 	    entries <= rt_max_size)
26921da177e4SLinus Torvalds 		goto out;
26931da177e4SLinus Torvalds 
26946891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
269514956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
2696fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
2697fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
26987019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
26991da177e4SLinus Torvalds out:
27007019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
2701fc66f95cSEric Dumazet 	return entries > rt_max_size;
27021da177e4SLinus Torvalds }
27031da177e4SLinus Torvalds 
27048c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net,
27058c14586fSDavid Ahern 					    struct fib6_config *cfg,
2706f4797b33SDavid Ahern 					    const struct in6_addr *gw_addr,
2707f4797b33SDavid Ahern 					    u32 tbid, int flags)
27088c14586fSDavid Ahern {
27098c14586fSDavid Ahern 	struct flowi6 fl6 = {
27108c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
27118c14586fSDavid Ahern 		.daddr = *gw_addr,
27128c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
27138c14586fSDavid Ahern 	};
27148c14586fSDavid Ahern 	struct fib6_table *table;
27158c14586fSDavid Ahern 	struct rt6_info *rt;
27168c14586fSDavid Ahern 
2717f4797b33SDavid Ahern 	table = fib6_get_table(net, tbid);
27188c14586fSDavid Ahern 	if (!table)
27198c14586fSDavid Ahern 		return NULL;
27208c14586fSDavid Ahern 
27218c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
27228c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
27238c14586fSDavid Ahern 
2724f4797b33SDavid Ahern 	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
2725b75cc8f9SDavid Ahern 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
27268c14586fSDavid Ahern 
27278c14586fSDavid Ahern 	/* if table lookup failed, fall back to full lookup */
27288c14586fSDavid Ahern 	if (rt == net->ipv6.ip6_null_entry) {
27298c14586fSDavid Ahern 		ip6_rt_put(rt);
27308c14586fSDavid Ahern 		rt = NULL;
27318c14586fSDavid Ahern 	}
27328c14586fSDavid Ahern 
27338c14586fSDavid Ahern 	return rt;
27348c14586fSDavid Ahern }
27358c14586fSDavid Ahern 
2736fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net,
2737fc1e64e1SDavid Ahern 				     struct fib6_config *cfg,
27389fbb704cSDavid Ahern 				     const struct net_device *dev,
2739fc1e64e1SDavid Ahern 				     struct netlink_ext_ack *extack)
2740fc1e64e1SDavid Ahern {
274144750f84SDavid Ahern 	u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
2742fc1e64e1SDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
2743fc1e64e1SDavid Ahern 	u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2744fc1e64e1SDavid Ahern 	struct rt6_info *grt;
2745fc1e64e1SDavid Ahern 	int err;
2746fc1e64e1SDavid Ahern 
2747fc1e64e1SDavid Ahern 	err = 0;
2748fc1e64e1SDavid Ahern 	grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2749fc1e64e1SDavid Ahern 	if (grt) {
275058e354c0SDavid Ahern 		if (!grt->dst.error &&
275158e354c0SDavid Ahern 		    (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
275244750f84SDavid Ahern 			NL_SET_ERR_MSG(extack,
275344750f84SDavid Ahern 				       "Nexthop has invalid gateway or device mismatch");
2754fc1e64e1SDavid Ahern 			err = -EINVAL;
2755fc1e64e1SDavid Ahern 		}
2756fc1e64e1SDavid Ahern 
2757fc1e64e1SDavid Ahern 		ip6_rt_put(grt);
2758fc1e64e1SDavid Ahern 	}
2759fc1e64e1SDavid Ahern 
2760fc1e64e1SDavid Ahern 	return err;
2761fc1e64e1SDavid Ahern }
2762fc1e64e1SDavid Ahern 
27631edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net,
27641edce99fSDavid Ahern 			      struct fib6_config *cfg,
27651edce99fSDavid Ahern 			      struct net_device **_dev,
27661edce99fSDavid Ahern 			      struct inet6_dev **idev)
27671edce99fSDavid Ahern {
27681edce99fSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
27691edce99fSDavid Ahern 	struct net_device *dev = _dev ? *_dev : NULL;
27701edce99fSDavid Ahern 	struct rt6_info *grt = NULL;
27711edce99fSDavid Ahern 	int err = -EHOSTUNREACH;
27721edce99fSDavid Ahern 
27731edce99fSDavid Ahern 	if (cfg->fc_table) {
2774f4797b33SDavid Ahern 		int flags = RT6_LOOKUP_F_IFACE;
2775f4797b33SDavid Ahern 
2776f4797b33SDavid Ahern 		grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2777f4797b33SDavid Ahern 					  cfg->fc_table, flags);
27781edce99fSDavid Ahern 		if (grt) {
27791edce99fSDavid Ahern 			if (grt->rt6i_flags & RTF_GATEWAY ||
27801edce99fSDavid Ahern 			    (dev && dev != grt->dst.dev)) {
27811edce99fSDavid Ahern 				ip6_rt_put(grt);
27821edce99fSDavid Ahern 				grt = NULL;
27831edce99fSDavid Ahern 			}
27841edce99fSDavid Ahern 		}
27851edce99fSDavid Ahern 	}
27861edce99fSDavid Ahern 
27871edce99fSDavid Ahern 	if (!grt)
2788b75cc8f9SDavid Ahern 		grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
27891edce99fSDavid Ahern 
27901edce99fSDavid Ahern 	if (!grt)
27911edce99fSDavid Ahern 		goto out;
27921edce99fSDavid Ahern 
27931edce99fSDavid Ahern 	if (dev) {
27941edce99fSDavid Ahern 		if (dev != grt->dst.dev) {
27951edce99fSDavid Ahern 			ip6_rt_put(grt);
27961edce99fSDavid Ahern 			goto out;
27971edce99fSDavid Ahern 		}
27981edce99fSDavid Ahern 	} else {
27991edce99fSDavid Ahern 		*_dev = dev = grt->dst.dev;
28001edce99fSDavid Ahern 		*idev = grt->rt6i_idev;
28011edce99fSDavid Ahern 		dev_hold(dev);
28021edce99fSDavid Ahern 		in6_dev_hold(grt->rt6i_idev);
28031edce99fSDavid Ahern 	}
28041edce99fSDavid Ahern 
28051edce99fSDavid Ahern 	if (!(grt->rt6i_flags & RTF_GATEWAY))
28061edce99fSDavid Ahern 		err = 0;
28071edce99fSDavid Ahern 
28081edce99fSDavid Ahern 	ip6_rt_put(grt);
28091edce99fSDavid Ahern 
28101edce99fSDavid Ahern out:
28111edce99fSDavid Ahern 	return err;
28121edce99fSDavid Ahern }
28131edce99fSDavid Ahern 
28149fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
28159fbb704cSDavid Ahern 			   struct net_device **_dev, struct inet6_dev **idev,
28169fbb704cSDavid Ahern 			   struct netlink_ext_ack *extack)
28179fbb704cSDavid Ahern {
28189fbb704cSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
28199fbb704cSDavid Ahern 	int gwa_type = ipv6_addr_type(gw_addr);
2820232378e8SDavid Ahern 	bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
28219fbb704cSDavid Ahern 	const struct net_device *dev = *_dev;
2822232378e8SDavid Ahern 	bool need_addr_check = !dev;
28239fbb704cSDavid Ahern 	int err = -EINVAL;
28249fbb704cSDavid Ahern 
28259fbb704cSDavid Ahern 	/* if gw_addr is local we will fail to detect this in case
28269fbb704cSDavid Ahern 	 * address is still TENTATIVE (DAD in progress). rt6_lookup()
28279fbb704cSDavid Ahern 	 * will return already-added prefix route via interface that
28289fbb704cSDavid Ahern 	 * prefix route was assigned to, which might be non-loopback.
28299fbb704cSDavid Ahern 	 */
2830232378e8SDavid Ahern 	if (dev &&
2831232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2832232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
28339fbb704cSDavid Ahern 		goto out;
28349fbb704cSDavid Ahern 	}
28359fbb704cSDavid Ahern 
28369fbb704cSDavid Ahern 	if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
28379fbb704cSDavid Ahern 		/* IPv6 strictly inhibits using not link-local
28389fbb704cSDavid Ahern 		 * addresses as nexthop address.
28399fbb704cSDavid Ahern 		 * Otherwise, router will not able to send redirects.
28409fbb704cSDavid Ahern 		 * It is very good, but in some (rare!) circumstances
28419fbb704cSDavid Ahern 		 * (SIT, PtP, NBMA NOARP links) it is handy to allow
28429fbb704cSDavid Ahern 		 * some exceptions. --ANK
28439fbb704cSDavid Ahern 		 * We allow IPv4-mapped nexthops to support RFC4798-type
28449fbb704cSDavid Ahern 		 * addressing
28459fbb704cSDavid Ahern 		 */
28469fbb704cSDavid Ahern 		if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
28479fbb704cSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
28489fbb704cSDavid Ahern 			goto out;
28499fbb704cSDavid Ahern 		}
28509fbb704cSDavid Ahern 
28519fbb704cSDavid Ahern 		if (cfg->fc_flags & RTNH_F_ONLINK)
28529fbb704cSDavid Ahern 			err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
28539fbb704cSDavid Ahern 		else
28549fbb704cSDavid Ahern 			err = ip6_route_check_nh(net, cfg, _dev, idev);
28559fbb704cSDavid Ahern 
28569fbb704cSDavid Ahern 		if (err)
28579fbb704cSDavid Ahern 			goto out;
28589fbb704cSDavid Ahern 	}
28599fbb704cSDavid Ahern 
28609fbb704cSDavid Ahern 	/* reload in case device was changed */
28619fbb704cSDavid Ahern 	dev = *_dev;
28629fbb704cSDavid Ahern 
28639fbb704cSDavid Ahern 	err = -EINVAL;
28649fbb704cSDavid Ahern 	if (!dev) {
28659fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Egress device not specified");
28669fbb704cSDavid Ahern 		goto out;
28679fbb704cSDavid Ahern 	} else if (dev->flags & IFF_LOOPBACK) {
28689fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack,
28699fbb704cSDavid Ahern 			       "Egress device can not be loopback device for this route");
28709fbb704cSDavid Ahern 		goto out;
28719fbb704cSDavid Ahern 	}
2872232378e8SDavid Ahern 
2873232378e8SDavid Ahern 	/* if we did not check gw_addr above, do so now that the
2874232378e8SDavid Ahern 	 * egress device has been resolved.
2875232378e8SDavid Ahern 	 */
2876232378e8SDavid Ahern 	if (need_addr_check &&
2877232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2878232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
2879232378e8SDavid Ahern 		goto out;
2880232378e8SDavid Ahern 	}
2881232378e8SDavid Ahern 
28829fbb704cSDavid Ahern 	err = 0;
28839fbb704cSDavid Ahern out:
28849fbb704cSDavid Ahern 	return err;
28859fbb704cSDavid Ahern }
28869fbb704cSDavid Ahern 
28878d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
2888acb54e3cSDavid Ahern 					      gfp_t gfp_flags,
2889333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
28901da177e4SLinus Torvalds {
28915578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
28928d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
28931da177e4SLinus Torvalds 	struct net_device *dev = NULL;
28941da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
2895c71099acSThomas Graf 	struct fib6_table *table;
28961da177e4SLinus Torvalds 	int addr_type;
28978c5b83f0SRoopa Prabhu 	int err = -EINVAL;
28981da177e4SLinus Torvalds 
2899557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
2900d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
2901d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
2902557c44beSDavid Ahern 		goto out;
2903d5d531cbSDavid Ahern 	}
2904557c44beSDavid Ahern 
29052ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
29062ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
29072ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
29082ea2352eSWei Wang 		goto out;
29092ea2352eSWei Wang 	}
29102ea2352eSWei Wang 
2911e8478e80SDavid Ahern 	if (cfg->fc_type > RTN_MAX) {
2912e8478e80SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid route type");
2913e8478e80SDavid Ahern 		goto out;
2914e8478e80SDavid Ahern 	}
2915e8478e80SDavid Ahern 
2916d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
2917d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
29188c5b83f0SRoopa Prabhu 		goto out;
2919d5d531cbSDavid Ahern 	}
2920d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
2921d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
2922d5d531cbSDavid Ahern 		goto out;
2923d5d531cbSDavid Ahern 	}
29241da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
2925d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
2926d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
2927d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
29288c5b83f0SRoopa Prabhu 		goto out;
2929d5d531cbSDavid Ahern 	}
29301da177e4SLinus Torvalds #endif
293186872cb5SThomas Graf 	if (cfg->fc_ifindex) {
29321da177e4SLinus Torvalds 		err = -ENODEV;
29335578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
29341da177e4SLinus Torvalds 		if (!dev)
29351da177e4SLinus Torvalds 			goto out;
29361da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
29371da177e4SLinus Torvalds 		if (!idev)
29381da177e4SLinus Torvalds 			goto out;
29391da177e4SLinus Torvalds 	}
29401da177e4SLinus Torvalds 
294186872cb5SThomas Graf 	if (cfg->fc_metric == 0)
294286872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
29431da177e4SLinus Torvalds 
2944fc1e64e1SDavid Ahern 	if (cfg->fc_flags & RTNH_F_ONLINK) {
2945fc1e64e1SDavid Ahern 		if (!dev) {
2946fc1e64e1SDavid Ahern 			NL_SET_ERR_MSG(extack,
2947fc1e64e1SDavid Ahern 				       "Nexthop device required for onlink");
2948fc1e64e1SDavid Ahern 			err = -ENODEV;
2949fc1e64e1SDavid Ahern 			goto out;
2950fc1e64e1SDavid Ahern 		}
2951fc1e64e1SDavid Ahern 
2952fc1e64e1SDavid Ahern 		if (!(dev->flags & IFF_UP)) {
2953fc1e64e1SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2954fc1e64e1SDavid Ahern 			err = -ENETDOWN;
2955fc1e64e1SDavid Ahern 			goto out;
2956fc1e64e1SDavid Ahern 		}
2957fc1e64e1SDavid Ahern 	}
2958fc1e64e1SDavid Ahern 
2959c71099acSThomas Graf 	err = -ENOBUFS;
296038308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
2961d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
2962d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
296338308473SDavid S. Miller 		if (!table) {
2964f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
2965d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
2966d71314b4SMatti Vaittinen 		}
2967d71314b4SMatti Vaittinen 	} else {
2968d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
2969d71314b4SMatti Vaittinen 	}
297038308473SDavid S. Miller 
297138308473SDavid S. Miller 	if (!table)
2972c71099acSThomas Graf 		goto out;
2973c71099acSThomas Graf 
29741da177e4SLinus Torvalds 	err = -ENOMEM;
297593531c67SDavid Ahern 	rt = fib6_info_alloc(gfp_flags);
297693531c67SDavid Ahern 	if (!rt)
29771da177e4SLinus Torvalds 		goto out;
297893531c67SDavid Ahern 
2979767a2217SDavid Ahern 	rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len);
2980767a2217SDavid Ahern 	if (IS_ERR(rt->fib6_metrics)) {
2981767a2217SDavid Ahern 		err = PTR_ERR(rt->fib6_metrics);
2982767a2217SDavid Ahern 		goto out;
2983767a2217SDavid Ahern 	}
2984767a2217SDavid Ahern 
298593531c67SDavid Ahern 	if (cfg->fc_flags & RTF_ADDRCONF)
298693531c67SDavid Ahern 		rt->dst_nocount = true;
29871da177e4SLinus Torvalds 
29881716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
298914895687SDavid Ahern 		fib6_set_expires(rt, jiffies +
29901716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
29911716a961SGao feng 	else
299214895687SDavid Ahern 		fib6_clean_expires(rt);
29931da177e4SLinus Torvalds 
299486872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
299586872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
299693c2fb25SDavid Ahern 	rt->fib6_protocol = cfg->fc_protocol;
299786872cb5SThomas Graf 
299886872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
29991da177e4SLinus Torvalds 
300019e42e45SRoopa Prabhu 	if (cfg->fc_encap) {
300119e42e45SRoopa Prabhu 		struct lwtunnel_state *lwtstate;
300219e42e45SRoopa Prabhu 
300330357d7dSDavid Ahern 		err = lwtunnel_build_state(cfg->fc_encap_type,
3004127eb7cdSTom Herbert 					   cfg->fc_encap, AF_INET6, cfg,
30059ae28727SDavid Ahern 					   &lwtstate, extack);
300619e42e45SRoopa Prabhu 		if (err)
300719e42e45SRoopa Prabhu 			goto out;
30085e670d84SDavid Ahern 		rt->fib6_nh.nh_lwtstate = lwtstate_get(lwtstate);
300925368623STom Herbert 	}
301019e42e45SRoopa Prabhu 
301193c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
301293c2fb25SDavid Ahern 	rt->fib6_dst.plen = cfg->fc_dst_len;
301393c2fb25SDavid Ahern 	if (rt->fib6_dst.plen == 128)
30143b6761d1SDavid Ahern 		rt->dst_host = true;
30151da177e4SLinus Torvalds 
30161da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
301793c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
301893c2fb25SDavid Ahern 	rt->fib6_src.plen = cfg->fc_src_len;
30191da177e4SLinus Torvalds #endif
30201da177e4SLinus Torvalds 
302193c2fb25SDavid Ahern 	rt->fib6_metric = cfg->fc_metric;
30225e670d84SDavid Ahern 	rt->fib6_nh.nh_weight = 1;
30231da177e4SLinus Torvalds 
3024e8478e80SDavid Ahern 	rt->fib6_type = cfg->fc_type;
30251da177e4SLinus Torvalds 
30261da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
30271da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
30281da177e4SLinus Torvalds 	 */
302986872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
303038308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
303138308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
303238308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
30331da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
30345578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
30351da177e4SLinus Torvalds 			if (dev) {
30361da177e4SLinus Torvalds 				dev_put(dev);
30371da177e4SLinus Torvalds 				in6_dev_put(idev);
30381da177e4SLinus Torvalds 			}
30395578689aSDaniel Lezcano 			dev = net->loopback_dev;
30401da177e4SLinus Torvalds 			dev_hold(dev);
30411da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
30421da177e4SLinus Torvalds 			if (!idev) {
30431da177e4SLinus Torvalds 				err = -ENODEV;
30441da177e4SLinus Torvalds 				goto out;
30451da177e4SLinus Torvalds 			}
30461da177e4SLinus Torvalds 		}
304793c2fb25SDavid Ahern 		rt->fib6_flags = RTF_REJECT|RTF_NONEXTHOP;
30481da177e4SLinus Torvalds 		goto install_route;
30491da177e4SLinus Torvalds 	}
30501da177e4SLinus Torvalds 
305186872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
30529fbb704cSDavid Ahern 		err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
30531da177e4SLinus Torvalds 		if (err)
30541da177e4SLinus Torvalds 			goto out;
30559fbb704cSDavid Ahern 
305693531c67SDavid Ahern 		rt->fib6_nh.nh_gw = cfg->fc_gateway;
30571da177e4SLinus Torvalds 	}
30581da177e4SLinus Torvalds 
30591da177e4SLinus Torvalds 	err = -ENODEV;
306038308473SDavid S. Miller 	if (!dev)
30611da177e4SLinus Torvalds 		goto out;
30621da177e4SLinus Torvalds 
3063428604fbSLorenzo Bianconi 	if (idev->cnf.disable_ipv6) {
3064428604fbSLorenzo Bianconi 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3065428604fbSLorenzo Bianconi 		err = -EACCES;
3066428604fbSLorenzo Bianconi 		goto out;
3067428604fbSLorenzo Bianconi 	}
3068428604fbSLorenzo Bianconi 
3069955ec4cbSDavid Ahern 	if (!(dev->flags & IFF_UP)) {
3070955ec4cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3071955ec4cbSDavid Ahern 		err = -ENETDOWN;
3072955ec4cbSDavid Ahern 		goto out;
3073955ec4cbSDavid Ahern 	}
3074955ec4cbSDavid Ahern 
3075c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3076c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3077d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
3078c3968a85SDaniel Walter 			err = -EINVAL;
3079c3968a85SDaniel Walter 			goto out;
3080c3968a85SDaniel Walter 		}
308193c2fb25SDavid Ahern 		rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
308293c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 128;
3083c3968a85SDaniel Walter 	} else
308493c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
3085c3968a85SDaniel Walter 
308693c2fb25SDavid Ahern 	rt->fib6_flags = cfg->fc_flags;
30871da177e4SLinus Torvalds 
30881da177e4SLinus Torvalds install_route:
308993c2fb25SDavid Ahern 	if (!(rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
30905609b80aSIdo Schimmel 	    !netif_carrier_ok(dev))
30915e670d84SDavid Ahern 		rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
30925e670d84SDavid Ahern 	rt->fib6_nh.nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK);
309393531c67SDavid Ahern 	rt->fib6_nh.nh_dev = dev;
309493c2fb25SDavid Ahern 	rt->fib6_table = table;
309563152fc0SDaniel Lezcano 
3096dcd1f572SDavid Ahern 	if (idev)
3097dcd1f572SDavid Ahern 		in6_dev_put(idev);
3098dcd1f572SDavid Ahern 
30998c5b83f0SRoopa Prabhu 	return rt;
31001da177e4SLinus Torvalds out:
31011da177e4SLinus Torvalds 	if (dev)
31021da177e4SLinus Torvalds 		dev_put(dev);
31031da177e4SLinus Torvalds 	if (idev)
31041da177e4SLinus Torvalds 		in6_dev_put(idev);
31056b9ea5a6SRoopa Prabhu 
310693531c67SDavid Ahern 	fib6_info_release(rt);
31078c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
31086b9ea5a6SRoopa Prabhu }
31096b9ea5a6SRoopa Prabhu 
3110acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3111333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
31126b9ea5a6SRoopa Prabhu {
31138d1c802bSDavid Ahern 	struct fib6_info *rt;
31146b9ea5a6SRoopa Prabhu 	int err;
31156b9ea5a6SRoopa Prabhu 
3116acb54e3cSDavid Ahern 	rt = ip6_route_info_create(cfg, gfp_flags, extack);
3117d4ead6b3SDavid Ahern 	if (IS_ERR(rt))
3118d4ead6b3SDavid Ahern 		return PTR_ERR(rt);
31196b9ea5a6SRoopa Prabhu 
3120d4ead6b3SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
312193531c67SDavid Ahern 	fib6_info_release(rt);
31226b9ea5a6SRoopa Prabhu 
31231da177e4SLinus Torvalds 	return err;
31241da177e4SLinus Torvalds }
31251da177e4SLinus Torvalds 
31268d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
31271da177e4SLinus Torvalds {
3128afb1d4b5SDavid Ahern 	struct net *net = info->nl_net;
3129c71099acSThomas Graf 	struct fib6_table *table;
3130afb1d4b5SDavid Ahern 	int err;
31311da177e4SLinus Torvalds 
3132421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
31336825a26cSGao feng 		err = -ENOENT;
31346825a26cSGao feng 		goto out;
31356825a26cSGao feng 	}
31366c813a72SPatrick McHardy 
313793c2fb25SDavid Ahern 	table = rt->fib6_table;
313866f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
313986872cb5SThomas Graf 	err = fib6_del(rt, info);
314066f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
31411da177e4SLinus Torvalds 
31426825a26cSGao feng out:
314393531c67SDavid Ahern 	fib6_info_release(rt);
31441da177e4SLinus Torvalds 	return err;
31451da177e4SLinus Torvalds }
31461da177e4SLinus Torvalds 
31478d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt)
3148e0a1ad73SThomas Graf {
3149afb1d4b5SDavid Ahern 	struct nl_info info = { .nl_net = net };
3150afb1d4b5SDavid Ahern 
3151528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
3152e0a1ad73SThomas Graf }
3153e0a1ad73SThomas Graf 
31548d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
31550ae81335SDavid Ahern {
31560ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
3157e3330039SWANG Cong 	struct net *net = info->nl_net;
315816a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
31590ae81335SDavid Ahern 	struct fib6_table *table;
3160e3330039SWANG Cong 	int err = -ENOENT;
31610ae81335SDavid Ahern 
3162421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
3163e3330039SWANG Cong 		goto out_put;
316493c2fb25SDavid Ahern 	table = rt->fib6_table;
316566f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
31660ae81335SDavid Ahern 
316793c2fb25SDavid Ahern 	if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
31688d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
31690ae81335SDavid Ahern 
317016a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
317116a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
317216a16cd3SDavid Ahern 		if (skb) {
317316a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
317416a16cd3SDavid Ahern 
3175d4ead6b3SDavid Ahern 			if (rt6_fill_node(net, skb, rt, NULL,
317616a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
317716a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
317816a16cd3SDavid Ahern 				kfree_skb(skb);
317916a16cd3SDavid Ahern 				skb = NULL;
318016a16cd3SDavid Ahern 			} else
318116a16cd3SDavid Ahern 				info->skip_notify = 1;
318216a16cd3SDavid Ahern 		}
318316a16cd3SDavid Ahern 
31840ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
318593c2fb25SDavid Ahern 					 &rt->fib6_siblings,
318693c2fb25SDavid Ahern 					 fib6_siblings) {
31870ae81335SDavid Ahern 			err = fib6_del(sibling, info);
31880ae81335SDavid Ahern 			if (err)
3189e3330039SWANG Cong 				goto out_unlock;
31900ae81335SDavid Ahern 		}
31910ae81335SDavid Ahern 	}
31920ae81335SDavid Ahern 
31930ae81335SDavid Ahern 	err = fib6_del(rt, info);
3194e3330039SWANG Cong out_unlock:
319566f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
3196e3330039SWANG Cong out_put:
319793531c67SDavid Ahern 	fib6_info_release(rt);
319816a16cd3SDavid Ahern 
319916a16cd3SDavid Ahern 	if (skb) {
3200e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
320116a16cd3SDavid Ahern 			    info->nlh, gfp_any());
320216a16cd3SDavid Ahern 	}
32030ae81335SDavid Ahern 	return err;
32040ae81335SDavid Ahern }
32050ae81335SDavid Ahern 
320623fb93a4SDavid Ahern static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
320723fb93a4SDavid Ahern {
320823fb93a4SDavid Ahern 	int rc = -ESRCH;
320923fb93a4SDavid Ahern 
321023fb93a4SDavid Ahern 	if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
321123fb93a4SDavid Ahern 		goto out;
321223fb93a4SDavid Ahern 
321323fb93a4SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY &&
321423fb93a4SDavid Ahern 	    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
321523fb93a4SDavid Ahern 		goto out;
321623fb93a4SDavid Ahern 	if (dst_hold_safe(&rt->dst))
321723fb93a4SDavid Ahern 		rc = rt6_remove_exception_rt(rt);
321823fb93a4SDavid Ahern out:
321923fb93a4SDavid Ahern 	return rc;
322023fb93a4SDavid Ahern }
322123fb93a4SDavid Ahern 
3222333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
3223333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
32241da177e4SLinus Torvalds {
32258d1c802bSDavid Ahern 	struct rt6_info *rt_cache;
3226c71099acSThomas Graf 	struct fib6_table *table;
32278d1c802bSDavid Ahern 	struct fib6_info *rt;
32281da177e4SLinus Torvalds 	struct fib6_node *fn;
32291da177e4SLinus Torvalds 	int err = -ESRCH;
32301da177e4SLinus Torvalds 
32315578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3232d5d531cbSDavid Ahern 	if (!table) {
3233d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
3234c71099acSThomas Graf 		return err;
3235d5d531cbSDavid Ahern 	}
32361da177e4SLinus Torvalds 
323766f5d6ceSWei Wang 	rcu_read_lock();
3238c71099acSThomas Graf 
3239c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
324086872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
324138fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
32422b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds 	if (fn) {
324566f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
32462b760fcfSWei Wang 			if (cfg->fc_flags & RTF_CACHE) {
324723fb93a4SDavid Ahern 				int rc;
324823fb93a4SDavid Ahern 
32492b760fcfSWei Wang 				rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
32502b760fcfSWei Wang 							      &cfg->fc_src);
325123fb93a4SDavid Ahern 				if (rt_cache) {
325223fb93a4SDavid Ahern 					rc = ip6_del_cached_rt(rt_cache, cfg);
32539e575010SEric Dumazet 					if (rc != -ESRCH) {
32549e575010SEric Dumazet 						rcu_read_unlock();
325523fb93a4SDavid Ahern 						return rc;
325623fb93a4SDavid Ahern 					}
32579e575010SEric Dumazet 				}
32581f56a01fSMartin KaFai Lau 				continue;
32592b760fcfSWei Wang 			}
326086872cb5SThomas Graf 			if (cfg->fc_ifindex &&
32615e670d84SDavid Ahern 			    (!rt->fib6_nh.nh_dev ||
32625e670d84SDavid Ahern 			     rt->fib6_nh.nh_dev->ifindex != cfg->fc_ifindex))
32631da177e4SLinus Torvalds 				continue;
326486872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
32655e670d84SDavid Ahern 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->fib6_nh.nh_gw))
32661da177e4SLinus Torvalds 				continue;
326793c2fb25SDavid Ahern 			if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
32681da177e4SLinus Torvalds 				continue;
326993c2fb25SDavid Ahern 			if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
3270c2ed1880SMantas M 				continue;
3271e873e4b9SWei Wang 			if (!fib6_info_hold_safe(rt))
3272e873e4b9SWei Wang 				continue;
327366f5d6ceSWei Wang 			rcu_read_unlock();
32741da177e4SLinus Torvalds 
32750ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
32760ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
327786872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
32780ae81335SDavid Ahern 
32790ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
32801da177e4SLinus Torvalds 		}
32811da177e4SLinus Torvalds 	}
328266f5d6ceSWei Wang 	rcu_read_unlock();
32831da177e4SLinus Torvalds 
32841da177e4SLinus Torvalds 	return err;
32851da177e4SLinus Torvalds }
32861da177e4SLinus Torvalds 
32876700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3288a6279458SYOSHIFUJI Hideaki {
3289a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
3290e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
3291e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
3292e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
3293e8599ff4SDavid S. Miller 	struct neighbour *neigh;
3294a68886a6SDavid Ahern 	struct fib6_info *from;
329571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
32966e157b6aSDavid S. Miller 	int optlen, on_link;
32976e157b6aSDavid S. Miller 	u8 *lladdr;
3298e8599ff4SDavid S. Miller 
329929a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
330071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
3301e8599ff4SDavid S. Miller 
3302e8599ff4SDavid S. Miller 	if (optlen < 0) {
33036e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3304e8599ff4SDavid S. Miller 		return;
3305e8599ff4SDavid S. Miller 	}
3306e8599ff4SDavid S. Miller 
330771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
3308e8599ff4SDavid S. Miller 
330971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
33106e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3311e8599ff4SDavid S. Miller 		return;
3312e8599ff4SDavid S. Miller 	}
3313e8599ff4SDavid S. Miller 
33146e157b6aSDavid S. Miller 	on_link = 0;
331571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
3316e8599ff4SDavid S. Miller 		on_link = 1;
331771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
3318e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
33196e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
3320e8599ff4SDavid S. Miller 		return;
3321e8599ff4SDavid S. Miller 	}
3322e8599ff4SDavid S. Miller 
3323e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
3324e8599ff4SDavid S. Miller 	if (!in6_dev)
3325e8599ff4SDavid S. Miller 		return;
3326e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3327e8599ff4SDavid S. Miller 		return;
3328e8599ff4SDavid S. Miller 
3329e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
3330e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
3331e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
3332e8599ff4SDavid S. Miller 	 */
3333e8599ff4SDavid S. Miller 
3334f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
3335e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3336e8599ff4SDavid S. Miller 		return;
3337e8599ff4SDavid S. Miller 	}
33386e157b6aSDavid S. Miller 
33396e157b6aSDavid S. Miller 	lladdr = NULL;
3340e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
3341e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3342e8599ff4SDavid S. Miller 					     skb->dev);
3343e8599ff4SDavid S. Miller 		if (!lladdr) {
3344e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3345e8599ff4SDavid S. Miller 			return;
3346e8599ff4SDavid S. Miller 		}
3347e8599ff4SDavid S. Miller 	}
3348e8599ff4SDavid S. Miller 
33496e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
3350ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
33516e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
33526e157b6aSDavid S. Miller 		return;
33536e157b6aSDavid S. Miller 	}
33546e157b6aSDavid S. Miller 
33556e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
33566e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
33576e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
33586e157b6aSDavid S. Miller 	 */
33590dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
33606e157b6aSDavid S. Miller 
336171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
3362e8599ff4SDavid S. Miller 	if (!neigh)
3363e8599ff4SDavid S. Miller 		return;
3364e8599ff4SDavid S. Miller 
33651da177e4SLinus Torvalds 	/*
33661da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
33671da177e4SLinus Torvalds 	 */
33681da177e4SLinus Torvalds 
3369f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
33701da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
33711da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
33721da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
3373f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
3374f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
33751da177e4SLinus Torvalds 
33764d85cd0cSDavid Ahern 	rcu_read_lock();
3377a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
3378e873e4b9SWei Wang 	/* This fib6_info_hold() is safe here because we hold reference to rt
3379e873e4b9SWei Wang 	 * and rt already holds reference to fib6_info.
3380e873e4b9SWei Wang 	 */
33818a14e46fSDavid Ahern 	fib6_info_hold(from);
33824d85cd0cSDavid Ahern 	rcu_read_unlock();
33838a14e46fSDavid Ahern 
33848a14e46fSDavid Ahern 	nrt = ip6_rt_cache_alloc(from, &msg->dest, NULL);
338538308473SDavid S. Miller 	if (!nrt)
33861da177e4SLinus Torvalds 		goto out;
33871da177e4SLinus Torvalds 
33881da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
33891da177e4SLinus Torvalds 	if (on_link)
33901da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
33911da177e4SLinus Torvalds 
33924e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
33931da177e4SLinus Torvalds 
33942b760fcfSWei Wang 	/* No need to remove rt from the exception table if rt is
33952b760fcfSWei Wang 	 * a cached route because rt6_insert_exception() will
33962b760fcfSWei Wang 	 * takes care of it
33972b760fcfSWei Wang 	 */
33988a14e46fSDavid Ahern 	if (rt6_insert_exception(nrt, from)) {
33992b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
34002b760fcfSWei Wang 		goto out;
34012b760fcfSWei Wang 	}
34021da177e4SLinus Torvalds 
3403d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
3404d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
340571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
340660592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
34078d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
34088d71740cSTom Tucker 
34091da177e4SLinus Torvalds out:
34108a14e46fSDavid Ahern 	fib6_info_release(from);
3411e8599ff4SDavid S. Miller 	neigh_release(neigh);
34126e157b6aSDavid S. Miller }
34136e157b6aSDavid S. Miller 
341470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
34158d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
3416b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3417830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3418830218c1SDavid Ahern 					   struct net_device *dev)
341970ceb4f5SYOSHIFUJI Hideaki {
3420830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3421830218c1SDavid Ahern 	int ifindex = dev->ifindex;
342270ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
34238d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3424c71099acSThomas Graf 	struct fib6_table *table;
342570ceb4f5SYOSHIFUJI Hideaki 
3426830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
342738308473SDavid S. Miller 	if (!table)
3428c71099acSThomas Graf 		return NULL;
3429c71099acSThomas Graf 
343066f5d6ceSWei Wang 	rcu_read_lock();
343138fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
343270ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
343370ceb4f5SYOSHIFUJI Hideaki 		goto out;
343470ceb4f5SYOSHIFUJI Hideaki 
343566f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
34365e670d84SDavid Ahern 		if (rt->fib6_nh.nh_dev->ifindex != ifindex)
343770ceb4f5SYOSHIFUJI Hideaki 			continue;
343893c2fb25SDavid Ahern 		if ((rt->fib6_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
343970ceb4f5SYOSHIFUJI Hideaki 			continue;
34405e670d84SDavid Ahern 		if (!ipv6_addr_equal(&rt->fib6_nh.nh_gw, gwaddr))
344170ceb4f5SYOSHIFUJI Hideaki 			continue;
3442e873e4b9SWei Wang 		if (!fib6_info_hold_safe(rt))
3443e873e4b9SWei Wang 			continue;
344470ceb4f5SYOSHIFUJI Hideaki 		break;
344570ceb4f5SYOSHIFUJI Hideaki 	}
344670ceb4f5SYOSHIFUJI Hideaki out:
344766f5d6ceSWei Wang 	rcu_read_unlock();
344870ceb4f5SYOSHIFUJI Hideaki 	return rt;
344970ceb4f5SYOSHIFUJI Hideaki }
345070ceb4f5SYOSHIFUJI Hideaki 
34518d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
3452b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3453830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3454830218c1SDavid Ahern 					   struct net_device *dev,
345595c96174SEric Dumazet 					   unsigned int pref)
345670ceb4f5SYOSHIFUJI Hideaki {
345786872cb5SThomas Graf 	struct fib6_config cfg = {
3458238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
3459830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
346086872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
346186872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
346286872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
3463b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3464e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
346515e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
3466efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3467efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
346886872cb5SThomas Graf 	};
346970ceb4f5SYOSHIFUJI Hideaki 
3470830218c1SDavid Ahern 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
34714e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
34724e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
347386872cb5SThomas Graf 
3474e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
3475e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
347686872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
347770ceb4f5SYOSHIFUJI Hideaki 
3478acb54e3cSDavid Ahern 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
347970ceb4f5SYOSHIFUJI Hideaki 
3480830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
348170ceb4f5SYOSHIFUJI Hideaki }
348270ceb4f5SYOSHIFUJI Hideaki #endif
348370ceb4f5SYOSHIFUJI Hideaki 
34848d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net,
3485afb1d4b5SDavid Ahern 				     const struct in6_addr *addr,
3486afb1d4b5SDavid Ahern 				     struct net_device *dev)
34871da177e4SLinus Torvalds {
3488830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
34898d1c802bSDavid Ahern 	struct fib6_info *rt;
3490c71099acSThomas Graf 	struct fib6_table *table;
34911da177e4SLinus Torvalds 
3492afb1d4b5SDavid Ahern 	table = fib6_get_table(net, tb_id);
349338308473SDavid S. Miller 	if (!table)
3494c71099acSThomas Graf 		return NULL;
34951da177e4SLinus Torvalds 
349666f5d6ceSWei Wang 	rcu_read_lock();
349766f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
34985e670d84SDavid Ahern 		if (dev == rt->fib6_nh.nh_dev &&
349993c2fb25SDavid Ahern 		    ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
35005e670d84SDavid Ahern 		    ipv6_addr_equal(&rt->fib6_nh.nh_gw, addr))
35011da177e4SLinus Torvalds 			break;
35021da177e4SLinus Torvalds 	}
3503e873e4b9SWei Wang 	if (rt && !fib6_info_hold_safe(rt))
3504e873e4b9SWei Wang 		rt = NULL;
350566f5d6ceSWei Wang 	rcu_read_unlock();
35061da177e4SLinus Torvalds 	return rt;
35071da177e4SLinus Torvalds }
35081da177e4SLinus Torvalds 
35098d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net,
3510afb1d4b5SDavid Ahern 				     const struct in6_addr *gwaddr,
3511ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
3512ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
35131da177e4SLinus Torvalds {
351486872cb5SThomas Graf 	struct fib6_config cfg = {
3515ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
3516238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
351786872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
351886872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
351986872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
3520b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3521e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
352215e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
35235578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3524afb1d4b5SDavid Ahern 		.fc_nlinfo.nl_net = net,
352586872cb5SThomas Graf 	};
35261da177e4SLinus Torvalds 
35274e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
35281da177e4SLinus Torvalds 
3529acb54e3cSDavid Ahern 	if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
3530830218c1SDavid Ahern 		struct fib6_table *table;
3531830218c1SDavid Ahern 
3532830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
3533830218c1SDavid Ahern 		if (table)
3534830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3535830218c1SDavid Ahern 	}
35361da177e4SLinus Torvalds 
3537afb1d4b5SDavid Ahern 	return rt6_get_dflt_router(net, gwaddr, dev);
35381da177e4SLinus Torvalds }
35391da177e4SLinus Torvalds 
3540afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net,
3541afb1d4b5SDavid Ahern 				     struct fib6_table *table)
35421da177e4SLinus Torvalds {
35438d1c802bSDavid Ahern 	struct fib6_info *rt;
35441da177e4SLinus Torvalds 
35451da177e4SLinus Torvalds restart:
354666f5d6ceSWei Wang 	rcu_read_lock();
354766f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
3548dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
3549dcd1f572SDavid Ahern 		struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3550dcd1f572SDavid Ahern 
355193c2fb25SDavid Ahern 		if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
3552e873e4b9SWei Wang 		    (!idev || idev->cnf.accept_ra != 2) &&
3553e873e4b9SWei Wang 		    fib6_info_hold_safe(rt)) {
355466f5d6ceSWei Wang 			rcu_read_unlock();
3555afb1d4b5SDavid Ahern 			ip6_del_rt(net, rt);
35561da177e4SLinus Torvalds 			goto restart;
35571da177e4SLinus Torvalds 		}
35581da177e4SLinus Torvalds 	}
355966f5d6ceSWei Wang 	rcu_read_unlock();
3560830218c1SDavid Ahern 
3561830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3562830218c1SDavid Ahern }
3563830218c1SDavid Ahern 
3564830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
3565830218c1SDavid Ahern {
3566830218c1SDavid Ahern 	struct fib6_table *table;
3567830218c1SDavid Ahern 	struct hlist_head *head;
3568830218c1SDavid Ahern 	unsigned int h;
3569830218c1SDavid Ahern 
3570830218c1SDavid Ahern 	rcu_read_lock();
3571830218c1SDavid Ahern 
3572830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3573830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
3574830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3575830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3576afb1d4b5SDavid Ahern 				__rt6_purge_dflt_routers(net, table);
3577830218c1SDavid Ahern 		}
3578830218c1SDavid Ahern 	}
3579830218c1SDavid Ahern 
3580830218c1SDavid Ahern 	rcu_read_unlock();
35811da177e4SLinus Torvalds }
35821da177e4SLinus Torvalds 
35835578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
35845578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
358586872cb5SThomas Graf 				 struct fib6_config *cfg)
358686872cb5SThomas Graf {
35878823a3acSMaciej Żenczykowski 	*cfg = (struct fib6_config){
35888823a3acSMaciej Żenczykowski 		.fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
35898823a3acSMaciej Żenczykowski 			 : RT6_TABLE_MAIN,
35908823a3acSMaciej Żenczykowski 		.fc_ifindex = rtmsg->rtmsg_ifindex,
35918823a3acSMaciej Żenczykowski 		.fc_metric = rtmsg->rtmsg_metric,
35928823a3acSMaciej Żenczykowski 		.fc_expires = rtmsg->rtmsg_info,
35938823a3acSMaciej Żenczykowski 		.fc_dst_len = rtmsg->rtmsg_dst_len,
35948823a3acSMaciej Żenczykowski 		.fc_src_len = rtmsg->rtmsg_src_len,
35958823a3acSMaciej Żenczykowski 		.fc_flags = rtmsg->rtmsg_flags,
35968823a3acSMaciej Żenczykowski 		.fc_type = rtmsg->rtmsg_type,
359786872cb5SThomas Graf 
35988823a3acSMaciej Żenczykowski 		.fc_nlinfo.nl_net = net,
359986872cb5SThomas Graf 
36008823a3acSMaciej Żenczykowski 		.fc_dst = rtmsg->rtmsg_dst,
36018823a3acSMaciej Żenczykowski 		.fc_src = rtmsg->rtmsg_src,
36028823a3acSMaciej Żenczykowski 		.fc_gateway = rtmsg->rtmsg_gateway,
36038823a3acSMaciej Żenczykowski 	};
360486872cb5SThomas Graf }
360586872cb5SThomas Graf 
36065578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
36071da177e4SLinus Torvalds {
360886872cb5SThomas Graf 	struct fib6_config cfg;
36091da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
36101da177e4SLinus Torvalds 	int err;
36111da177e4SLinus Torvalds 
36121da177e4SLinus Torvalds 	switch (cmd) {
36131da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
36141da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
3615af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
36161da177e4SLinus Torvalds 			return -EPERM;
36171da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
36181da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
36191da177e4SLinus Torvalds 		if (err)
36201da177e4SLinus Torvalds 			return -EFAULT;
36211da177e4SLinus Torvalds 
36225578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
362386872cb5SThomas Graf 
36241da177e4SLinus Torvalds 		rtnl_lock();
36251da177e4SLinus Torvalds 		switch (cmd) {
36261da177e4SLinus Torvalds 		case SIOCADDRT:
3627acb54e3cSDavid Ahern 			err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
36281da177e4SLinus Torvalds 			break;
36291da177e4SLinus Torvalds 		case SIOCDELRT:
3630333c4301SDavid Ahern 			err = ip6_route_del(&cfg, NULL);
36311da177e4SLinus Torvalds 			break;
36321da177e4SLinus Torvalds 		default:
36331da177e4SLinus Torvalds 			err = -EINVAL;
36341da177e4SLinus Torvalds 		}
36351da177e4SLinus Torvalds 		rtnl_unlock();
36361da177e4SLinus Torvalds 
36371da177e4SLinus Torvalds 		return err;
36383ff50b79SStephen Hemminger 	}
36391da177e4SLinus Torvalds 
36401da177e4SLinus Torvalds 	return -EINVAL;
36411da177e4SLinus Torvalds }
36421da177e4SLinus Torvalds 
36431da177e4SLinus Torvalds /*
36441da177e4SLinus Torvalds  *	Drop the packet on the floor
36451da177e4SLinus Torvalds  */
36461da177e4SLinus Torvalds 
3647d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
36481da177e4SLinus Torvalds {
3649612f09e8SYOSHIFUJI Hideaki 	int type;
3650adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
3651612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
3652612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
36530660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
365445bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
3655bdb7cc64SStephen Suryaputra 			IP6_INC_STATS(dev_net(dst->dev),
3656bdb7cc64SStephen Suryaputra 				      __in6_dev_get_safely(skb->dev),
36573bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
3658612f09e8SYOSHIFUJI Hideaki 			break;
3659612f09e8SYOSHIFUJI Hideaki 		}
3660612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
3661612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
36623bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
36633bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
3664612f09e8SYOSHIFUJI Hideaki 		break;
3665612f09e8SYOSHIFUJI Hideaki 	}
36663ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
36671da177e4SLinus Torvalds 	kfree_skb(skb);
36681da177e4SLinus Torvalds 	return 0;
36691da177e4SLinus Torvalds }
36701da177e4SLinus Torvalds 
36719ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
36729ce8ade0SThomas Graf {
3673612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
36749ce8ade0SThomas Graf }
36759ce8ade0SThomas Graf 
3676ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
36771da177e4SLinus Torvalds {
3678adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3679612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
36801da177e4SLinus Torvalds }
36811da177e4SLinus Torvalds 
36829ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
36839ce8ade0SThomas Graf {
3684612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
36859ce8ade0SThomas Graf }
36869ce8ade0SThomas Graf 
3687ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
36889ce8ade0SThomas Graf {
3689adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3690612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
36919ce8ade0SThomas Graf }
36929ce8ade0SThomas Graf 
36931da177e4SLinus Torvalds /*
36941da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
36951da177e4SLinus Torvalds  */
36961da177e4SLinus Torvalds 
3697360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net,
3698afb1d4b5SDavid Ahern 				     struct inet6_dev *idev,
36991da177e4SLinus Torvalds 				     const struct in6_addr *addr,
3700acb54e3cSDavid Ahern 				     bool anycast, gfp_t gfp_flags)
37011da177e4SLinus Torvalds {
3702ca254490SDavid Ahern 	u32 tb_id;
37034832c30dSDavid Ahern 	struct net_device *dev = idev->dev;
3704360a9887SDavid Ahern 	struct fib6_info *f6i;
37055f02ce24SDavid Ahern 
3706360a9887SDavid Ahern 	f6i = fib6_info_alloc(gfp_flags);
3707360a9887SDavid Ahern 	if (!f6i)
37081da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
37091da177e4SLinus Torvalds 
3710767a2217SDavid Ahern 	f6i->fib6_metrics = ip_fib_metrics_init(net, NULL, 0);
3711360a9887SDavid Ahern 	f6i->dst_nocount = true;
3712360a9887SDavid Ahern 	f6i->dst_host = true;
3713360a9887SDavid Ahern 	f6i->fib6_protocol = RTPROT_KERNEL;
3714360a9887SDavid Ahern 	f6i->fib6_flags = RTF_UP | RTF_NONEXTHOP;
3715e8478e80SDavid Ahern 	if (anycast) {
3716360a9887SDavid Ahern 		f6i->fib6_type = RTN_ANYCAST;
3717360a9887SDavid Ahern 		f6i->fib6_flags |= RTF_ANYCAST;
3718e8478e80SDavid Ahern 	} else {
3719360a9887SDavid Ahern 		f6i->fib6_type = RTN_LOCAL;
3720360a9887SDavid Ahern 		f6i->fib6_flags |= RTF_LOCAL;
3721e8478e80SDavid Ahern 	}
37221da177e4SLinus Torvalds 
3723360a9887SDavid Ahern 	f6i->fib6_nh.nh_gw = *addr;
372493531c67SDavid Ahern 	dev_hold(dev);
3725360a9887SDavid Ahern 	f6i->fib6_nh.nh_dev = dev;
3726360a9887SDavid Ahern 	f6i->fib6_dst.addr = *addr;
3727360a9887SDavid Ahern 	f6i->fib6_dst.plen = 128;
3728ca254490SDavid Ahern 	tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
3729360a9887SDavid Ahern 	f6i->fib6_table = fib6_get_table(net, tb_id);
37301da177e4SLinus Torvalds 
3731360a9887SDavid Ahern 	return f6i;
37321da177e4SLinus Torvalds }
37331da177e4SLinus Torvalds 
3734c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
3735c3968a85SDaniel Walter struct arg_dev_net_ip {
3736c3968a85SDaniel Walter 	struct net_device *dev;
3737c3968a85SDaniel Walter 	struct net *net;
3738c3968a85SDaniel Walter 	struct in6_addr *addr;
3739c3968a85SDaniel Walter };
3740c3968a85SDaniel Walter 
37418d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
3742c3968a85SDaniel Walter {
3743c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3744c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3745c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3746c3968a85SDaniel Walter 
37475e670d84SDavid Ahern 	if (((void *)rt->fib6_nh.nh_dev == dev || !dev) &&
3748421842edSDavid Ahern 	    rt != net->ipv6.fib6_null_entry &&
374993c2fb25SDavid Ahern 	    ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
375060006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
3751c3968a85SDaniel Walter 		/* remove prefsrc entry */
375293c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
375360006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
3754c3968a85SDaniel Walter 	}
3755c3968a85SDaniel Walter 	return 0;
3756c3968a85SDaniel Walter }
3757c3968a85SDaniel Walter 
3758c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3759c3968a85SDaniel Walter {
3760c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
3761c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
3762c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
3763c3968a85SDaniel Walter 		.net = net,
3764c3968a85SDaniel Walter 		.addr = &ifp->addr,
3765c3968a85SDaniel Walter 	};
37660c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
3767c3968a85SDaniel Walter }
3768c3968a85SDaniel Walter 
3769be7a010dSDuan Jiong #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
3770be7a010dSDuan Jiong 
3771be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
37728d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
3773be7a010dSDuan Jiong {
3774be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
3775be7a010dSDuan Jiong 
377693c2fb25SDavid Ahern 	if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
37775e670d84SDavid Ahern 	    ipv6_addr_equal(gateway, &rt->fib6_nh.nh_gw)) {
3778be7a010dSDuan Jiong 		return -1;
3779be7a010dSDuan Jiong 	}
3780b16cb459SWei Wang 
3781b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
3782b16cb459SWei Wang 	 * This is needed because cached route may have a different
3783b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
3784b16cb459SWei Wang 	 */
3785b16cb459SWei Wang 	rt6_exceptions_clean_tohost(rt, gateway);
3786b16cb459SWei Wang 
3787be7a010dSDuan Jiong 	return 0;
3788be7a010dSDuan Jiong }
3789be7a010dSDuan Jiong 
3790be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3791be7a010dSDuan Jiong {
3792be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
3793be7a010dSDuan Jiong }
3794be7a010dSDuan Jiong 
37952127d95aSIdo Schimmel struct arg_netdev_event {
37962127d95aSIdo Schimmel 	const struct net_device *dev;
37974c981e28SIdo Schimmel 	union {
37982127d95aSIdo Schimmel 		unsigned int nh_flags;
37994c981e28SIdo Schimmel 		unsigned long event;
38004c981e28SIdo Schimmel 	};
38012127d95aSIdo Schimmel };
38022127d95aSIdo Schimmel 
38038d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
3804d7dedee1SIdo Schimmel {
38058d1c802bSDavid Ahern 	struct fib6_info *iter;
3806d7dedee1SIdo Schimmel 	struct fib6_node *fn;
3807d7dedee1SIdo Schimmel 
380893c2fb25SDavid Ahern 	fn = rcu_dereference_protected(rt->fib6_node,
380993c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3810d7dedee1SIdo Schimmel 	iter = rcu_dereference_protected(fn->leaf,
381193c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3812d7dedee1SIdo Schimmel 	while (iter) {
381393c2fb25SDavid Ahern 		if (iter->fib6_metric == rt->fib6_metric &&
381433bd5ac5SDavid Ahern 		    rt6_qualify_for_ecmp(iter))
3815d7dedee1SIdo Schimmel 			return iter;
38168fb11a9aSDavid Ahern 		iter = rcu_dereference_protected(iter->fib6_next,
381793c2fb25SDavid Ahern 				lockdep_is_held(&rt->fib6_table->tb6_lock));
3818d7dedee1SIdo Schimmel 	}
3819d7dedee1SIdo Schimmel 
3820d7dedee1SIdo Schimmel 	return NULL;
3821d7dedee1SIdo Schimmel }
3822d7dedee1SIdo Schimmel 
38238d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt)
3824d7dedee1SIdo Schimmel {
38255e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_DEAD ||
38265e670d84SDavid Ahern 	    (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
3827dcd1f572SDavid Ahern 	     fib6_ignore_linkdown(rt)))
3828d7dedee1SIdo Schimmel 		return true;
3829d7dedee1SIdo Schimmel 
3830d7dedee1SIdo Schimmel 	return false;
3831d7dedee1SIdo Schimmel }
3832d7dedee1SIdo Schimmel 
38338d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt)
3834d7dedee1SIdo Schimmel {
38358d1c802bSDavid Ahern 	struct fib6_info *iter;
3836d7dedee1SIdo Schimmel 	int total = 0;
3837d7dedee1SIdo Schimmel 
3838d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt))
38395e670d84SDavid Ahern 		total += rt->fib6_nh.nh_weight;
3840d7dedee1SIdo Schimmel 
384193c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
3842d7dedee1SIdo Schimmel 		if (!rt6_is_dead(iter))
38435e670d84SDavid Ahern 			total += iter->fib6_nh.nh_weight;
3844d7dedee1SIdo Schimmel 	}
3845d7dedee1SIdo Schimmel 
3846d7dedee1SIdo Schimmel 	return total;
3847d7dedee1SIdo Schimmel }
3848d7dedee1SIdo Schimmel 
38498d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
3850d7dedee1SIdo Schimmel {
3851d7dedee1SIdo Schimmel 	int upper_bound = -1;
3852d7dedee1SIdo Schimmel 
3853d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt)) {
38545e670d84SDavid Ahern 		*weight += rt->fib6_nh.nh_weight;
3855d7dedee1SIdo Schimmel 		upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
3856d7dedee1SIdo Schimmel 						    total) - 1;
3857d7dedee1SIdo Schimmel 	}
38585e670d84SDavid Ahern 	atomic_set(&rt->fib6_nh.nh_upper_bound, upper_bound);
3859d7dedee1SIdo Schimmel }
3860d7dedee1SIdo Schimmel 
38618d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
3862d7dedee1SIdo Schimmel {
38638d1c802bSDavid Ahern 	struct fib6_info *iter;
3864d7dedee1SIdo Schimmel 	int weight = 0;
3865d7dedee1SIdo Schimmel 
3866d7dedee1SIdo Schimmel 	rt6_upper_bound_set(rt, &weight, total);
3867d7dedee1SIdo Schimmel 
386893c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
3869d7dedee1SIdo Schimmel 		rt6_upper_bound_set(iter, &weight, total);
3870d7dedee1SIdo Schimmel }
3871d7dedee1SIdo Schimmel 
38728d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt)
3873d7dedee1SIdo Schimmel {
38748d1c802bSDavid Ahern 	struct fib6_info *first;
3875d7dedee1SIdo Schimmel 	int total;
3876d7dedee1SIdo Schimmel 
3877d7dedee1SIdo Schimmel 	/* In case the entire multipath route was marked for flushing,
3878d7dedee1SIdo Schimmel 	 * then there is no need to rebalance upon the removal of every
3879d7dedee1SIdo Schimmel 	 * sibling route.
3880d7dedee1SIdo Schimmel 	 */
388193c2fb25SDavid Ahern 	if (!rt->fib6_nsiblings || rt->should_flush)
3882d7dedee1SIdo Schimmel 		return;
3883d7dedee1SIdo Schimmel 
3884d7dedee1SIdo Schimmel 	/* During lookup routes are evaluated in order, so we need to
3885d7dedee1SIdo Schimmel 	 * make sure upper bounds are assigned from the first sibling
3886d7dedee1SIdo Schimmel 	 * onwards.
3887d7dedee1SIdo Schimmel 	 */
3888d7dedee1SIdo Schimmel 	first = rt6_multipath_first_sibling(rt);
3889d7dedee1SIdo Schimmel 	if (WARN_ON_ONCE(!first))
3890d7dedee1SIdo Schimmel 		return;
3891d7dedee1SIdo Schimmel 
3892d7dedee1SIdo Schimmel 	total = rt6_multipath_total_weight(first);
3893d7dedee1SIdo Schimmel 	rt6_multipath_upper_bound_set(first, total);
3894d7dedee1SIdo Schimmel }
3895d7dedee1SIdo Schimmel 
38968d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg)
38972127d95aSIdo Schimmel {
38982127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
38997aef6859SDavid Ahern 	struct net *net = dev_net(arg->dev);
39002127d95aSIdo Schimmel 
3901421842edSDavid Ahern 	if (rt != net->ipv6.fib6_null_entry && rt->fib6_nh.nh_dev == arg->dev) {
39025e670d84SDavid Ahern 		rt->fib6_nh.nh_flags &= ~arg->nh_flags;
39037aef6859SDavid Ahern 		fib6_update_sernum_upto_root(net, rt);
3904d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
39051de178edSIdo Schimmel 	}
39062127d95aSIdo Schimmel 
39072127d95aSIdo Schimmel 	return 0;
39082127d95aSIdo Schimmel }
39092127d95aSIdo Schimmel 
39102127d95aSIdo Schimmel void rt6_sync_up(struct net_device *dev, unsigned int nh_flags)
39112127d95aSIdo Schimmel {
39122127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
39132127d95aSIdo Schimmel 		.dev = dev,
39146802f3adSIdo Schimmel 		{
39152127d95aSIdo Schimmel 			.nh_flags = nh_flags,
39166802f3adSIdo Schimmel 		},
39172127d95aSIdo Schimmel 	};
39182127d95aSIdo Schimmel 
39192127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
39202127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
39212127d95aSIdo Schimmel 
39222127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
39232127d95aSIdo Schimmel }
39242127d95aSIdo Schimmel 
39258d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
39261de178edSIdo Schimmel 				   const struct net_device *dev)
39271de178edSIdo Schimmel {
39288d1c802bSDavid Ahern 	struct fib6_info *iter;
39291de178edSIdo Schimmel 
39305e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == dev)
39311de178edSIdo Schimmel 		return true;
393293c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
39335e670d84SDavid Ahern 		if (iter->fib6_nh.nh_dev == dev)
39341de178edSIdo Schimmel 			return true;
39351de178edSIdo Schimmel 
39361de178edSIdo Schimmel 	return false;
39371de178edSIdo Schimmel }
39381de178edSIdo Schimmel 
39398d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt)
39401de178edSIdo Schimmel {
39418d1c802bSDavid Ahern 	struct fib6_info *iter;
39421de178edSIdo Schimmel 
39431de178edSIdo Schimmel 	rt->should_flush = 1;
394493c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
39451de178edSIdo Schimmel 		iter->should_flush = 1;
39461de178edSIdo Schimmel }
39471de178edSIdo Schimmel 
39488d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
39491de178edSIdo Schimmel 					     const struct net_device *down_dev)
39501de178edSIdo Schimmel {
39518d1c802bSDavid Ahern 	struct fib6_info *iter;
39521de178edSIdo Schimmel 	unsigned int dead = 0;
39531de178edSIdo Schimmel 
39545e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == down_dev ||
39555e670d84SDavid Ahern 	    rt->fib6_nh.nh_flags & RTNH_F_DEAD)
39561de178edSIdo Schimmel 		dead++;
395793c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
39585e670d84SDavid Ahern 		if (iter->fib6_nh.nh_dev == down_dev ||
39595e670d84SDavid Ahern 		    iter->fib6_nh.nh_flags & RTNH_F_DEAD)
39601de178edSIdo Schimmel 			dead++;
39611de178edSIdo Schimmel 
39621de178edSIdo Schimmel 	return dead;
39631de178edSIdo Schimmel }
39641de178edSIdo Schimmel 
39658d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
39661de178edSIdo Schimmel 				       const struct net_device *dev,
39671de178edSIdo Schimmel 				       unsigned int nh_flags)
39681de178edSIdo Schimmel {
39698d1c802bSDavid Ahern 	struct fib6_info *iter;
39701de178edSIdo Schimmel 
39715e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == dev)
39725e670d84SDavid Ahern 		rt->fib6_nh.nh_flags |= nh_flags;
397393c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
39745e670d84SDavid Ahern 		if (iter->fib6_nh.nh_dev == dev)
39755e670d84SDavid Ahern 			iter->fib6_nh.nh_flags |= nh_flags;
39761de178edSIdo Schimmel }
39771de178edSIdo Schimmel 
3978a1a22c12SDavid Ahern /* called with write lock held for table with rt */
39798d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
39801da177e4SLinus Torvalds {
39814c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
39824c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
39837aef6859SDavid Ahern 	struct net *net = dev_net(dev);
39848ed67789SDaniel Lezcano 
3985421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
398627c6fa73SIdo Schimmel 		return 0;
398727c6fa73SIdo Schimmel 
398827c6fa73SIdo Schimmel 	switch (arg->event) {
398927c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
39905e670d84SDavid Ahern 		return rt->fib6_nh.nh_dev == dev ? -1 : 0;
399127c6fa73SIdo Schimmel 	case NETDEV_DOWN:
39921de178edSIdo Schimmel 		if (rt->should_flush)
399327c6fa73SIdo Schimmel 			return -1;
399493c2fb25SDavid Ahern 		if (!rt->fib6_nsiblings)
39955e670d84SDavid Ahern 			return rt->fib6_nh.nh_dev == dev ? -1 : 0;
39961de178edSIdo Schimmel 		if (rt6_multipath_uses_dev(rt, dev)) {
39971de178edSIdo Schimmel 			unsigned int count;
39981de178edSIdo Schimmel 
39991de178edSIdo Schimmel 			count = rt6_multipath_dead_count(rt, dev);
400093c2fb25SDavid Ahern 			if (rt->fib6_nsiblings + 1 == count) {
40011de178edSIdo Schimmel 				rt6_multipath_flush(rt);
40021de178edSIdo Schimmel 				return -1;
40031de178edSIdo Schimmel 			}
40041de178edSIdo Schimmel 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
40051de178edSIdo Schimmel 						   RTNH_F_LINKDOWN);
40067aef6859SDavid Ahern 			fib6_update_sernum(net, rt);
4007d7dedee1SIdo Schimmel 			rt6_multipath_rebalance(rt);
40081de178edSIdo Schimmel 		}
40091de178edSIdo Schimmel 		return -2;
401027c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
40115e670d84SDavid Ahern 		if (rt->fib6_nh.nh_dev != dev ||
401293c2fb25SDavid Ahern 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
401327c6fa73SIdo Schimmel 			break;
40145e670d84SDavid Ahern 		rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
4015d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
401627c6fa73SIdo Schimmel 		break;
40172b241361SIdo Schimmel 	}
4018c159d30cSDavid S. Miller 
40191da177e4SLinus Torvalds 	return 0;
40201da177e4SLinus Torvalds }
40211da177e4SLinus Torvalds 
402227c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
40231da177e4SLinus Torvalds {
40244c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
40258ed67789SDaniel Lezcano 		.dev = dev,
40266802f3adSIdo Schimmel 		{
40274c981e28SIdo Schimmel 			.event = event,
40286802f3adSIdo Schimmel 		},
40298ed67789SDaniel Lezcano 	};
40308ed67789SDaniel Lezcano 
40314c981e28SIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifdown, &arg);
40324c981e28SIdo Schimmel }
40334c981e28SIdo Schimmel 
40344c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
40354c981e28SIdo Schimmel {
40364c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
40374c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
40384c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
40391da177e4SLinus Torvalds }
40401da177e4SLinus Torvalds 
404195c96174SEric Dumazet struct rt6_mtu_change_arg {
40421da177e4SLinus Torvalds 	struct net_device *dev;
404395c96174SEric Dumazet 	unsigned int mtu;
40441da177e4SLinus Torvalds };
40451da177e4SLinus Torvalds 
40468d1c802bSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg)
40471da177e4SLinus Torvalds {
40481da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
40491da177e4SLinus Torvalds 	struct inet6_dev *idev;
40501da177e4SLinus Torvalds 
40511da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
40521da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
40531da177e4SLinus Torvalds 	   We still use this lock to block changes
40541da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
40551da177e4SLinus Torvalds 	*/
40561da177e4SLinus Torvalds 
40571da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
405838308473SDavid S. Miller 	if (!idev)
40591da177e4SLinus Torvalds 		return 0;
40601da177e4SLinus Torvalds 
40611da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
40621da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
40631da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
40641da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
40651da177e4SLinus Torvalds 	 */
40665e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == arg->dev &&
4067d4ead6b3SDavid Ahern 	    !fib6_metric_locked(rt, RTAX_MTU)) {
4068d4ead6b3SDavid Ahern 		u32 mtu = rt->fib6_pmtu;
4069d4ead6b3SDavid Ahern 
4070d4ead6b3SDavid Ahern 		if (mtu >= arg->mtu ||
4071d4ead6b3SDavid Ahern 		    (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4072d4ead6b3SDavid Ahern 			fib6_metric_set(rt, RTAX_MTU, arg->mtu);
4073d4ead6b3SDavid Ahern 
4074f5bbe7eeSWei Wang 		spin_lock_bh(&rt6_exception_lock);
4075e9fa1495SStefano Brivio 		rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
4076f5bbe7eeSWei Wang 		spin_unlock_bh(&rt6_exception_lock);
40774b32b5adSMartin KaFai Lau 	}
40781da177e4SLinus Torvalds 	return 0;
40791da177e4SLinus Torvalds }
40801da177e4SLinus Torvalds 
408195c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
40821da177e4SLinus Torvalds {
4083c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
4084c71099acSThomas Graf 		.dev = dev,
4085c71099acSThomas Graf 		.mtu = mtu,
4086c71099acSThomas Graf 	};
40871da177e4SLinus Torvalds 
40880c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
40891da177e4SLinus Torvalds }
40901da177e4SLinus Torvalds 
4091ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
40925176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
4093aa8f8778SEric Dumazet 	[RTA_PREFSRC]		= { .len = sizeof(struct in6_addr) },
409486872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
4095ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
409686872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
409786872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
409851ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
4099c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
410019e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
410119e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
410232bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
4103622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
41043b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
4105aa8f8778SEric Dumazet 	[RTA_TABLE]		= { .type = NLA_U32 },
4106eacb9384SRoopa Prabhu 	[RTA_IP_PROTO]		= { .type = NLA_U8 },
4107eacb9384SRoopa Prabhu 	[RTA_SPORT]		= { .type = NLA_U16 },
4108eacb9384SRoopa Prabhu 	[RTA_DPORT]		= { .type = NLA_U16 },
410986872cb5SThomas Graf };
411086872cb5SThomas Graf 
411186872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4112333c4301SDavid Ahern 			      struct fib6_config *cfg,
4113333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
41141da177e4SLinus Torvalds {
411586872cb5SThomas Graf 	struct rtmsg *rtm;
411686872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
4117c78ba6d6SLubomir Rintel 	unsigned int pref;
411886872cb5SThomas Graf 	int err;
41191da177e4SLinus Torvalds 
4120fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4121fceb6435SJohannes Berg 			  NULL);
412286872cb5SThomas Graf 	if (err < 0)
412386872cb5SThomas Graf 		goto errout;
41241da177e4SLinus Torvalds 
412586872cb5SThomas Graf 	err = -EINVAL;
412686872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
412786872cb5SThomas Graf 
412884db8407SMaciej Żenczykowski 	*cfg = (struct fib6_config){
412984db8407SMaciej Żenczykowski 		.fc_table = rtm->rtm_table,
413084db8407SMaciej Żenczykowski 		.fc_dst_len = rtm->rtm_dst_len,
413184db8407SMaciej Żenczykowski 		.fc_src_len = rtm->rtm_src_len,
413284db8407SMaciej Żenczykowski 		.fc_flags = RTF_UP,
413384db8407SMaciej Żenczykowski 		.fc_protocol = rtm->rtm_protocol,
413484db8407SMaciej Żenczykowski 		.fc_type = rtm->rtm_type,
413584db8407SMaciej Żenczykowski 
413684db8407SMaciej Żenczykowski 		.fc_nlinfo.portid = NETLINK_CB(skb).portid,
413784db8407SMaciej Żenczykowski 		.fc_nlinfo.nlh = nlh,
413884db8407SMaciej Żenczykowski 		.fc_nlinfo.nl_net = sock_net(skb->sk),
413984db8407SMaciej Żenczykowski 	};
414086872cb5SThomas Graf 
4141ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
4142ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
4143b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
4144b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
414586872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
414686872cb5SThomas Graf 
4147ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
4148ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
4149ab79ad14SMaciej Żenczykowski 
41501f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
41511f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
41521f56a01fSMartin KaFai Lau 
4153fc1e64e1SDavid Ahern 	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4154fc1e64e1SDavid Ahern 
415586872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
415667b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
415786872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
41581da177e4SLinus Torvalds 	}
415986872cb5SThomas Graf 
416086872cb5SThomas Graf 	if (tb[RTA_DST]) {
416186872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
416286872cb5SThomas Graf 
416386872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
416486872cb5SThomas Graf 			goto errout;
416586872cb5SThomas Graf 
416686872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
41671da177e4SLinus Torvalds 	}
416886872cb5SThomas Graf 
416986872cb5SThomas Graf 	if (tb[RTA_SRC]) {
417086872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
417186872cb5SThomas Graf 
417286872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
417386872cb5SThomas Graf 			goto errout;
417486872cb5SThomas Graf 
417586872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
41761da177e4SLinus Torvalds 	}
417786872cb5SThomas Graf 
4178c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
417967b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4180c3968a85SDaniel Walter 
418186872cb5SThomas Graf 	if (tb[RTA_OIF])
418286872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
418386872cb5SThomas Graf 
418486872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
418586872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
418686872cb5SThomas Graf 
418786872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
418886872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
418986872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
41901da177e4SLinus Torvalds 	}
419186872cb5SThomas Graf 
419286872cb5SThomas Graf 	if (tb[RTA_TABLE])
419386872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
419486872cb5SThomas Graf 
419551ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
419651ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
419751ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
41989ed59592SDavid Ahern 
41999ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4200c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
42019ed59592SDavid Ahern 		if (err < 0)
42029ed59592SDavid Ahern 			goto errout;
420351ebd318SNicolas Dichtel 	}
420451ebd318SNicolas Dichtel 
4205c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
4206c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
4207c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
4208c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
4209c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
4210c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
4211c78ba6d6SLubomir Rintel 	}
4212c78ba6d6SLubomir Rintel 
421319e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
421419e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
421519e42e45SRoopa Prabhu 
42169ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
421719e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
421819e42e45SRoopa Prabhu 
4219c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
42209ed59592SDavid Ahern 		if (err < 0)
42219ed59592SDavid Ahern 			goto errout;
42229ed59592SDavid Ahern 	}
42239ed59592SDavid Ahern 
422432bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
422532bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
422632bc201eSXin Long 
422732bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
422832bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
422932bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
423032bc201eSXin Long 		}
423132bc201eSXin Long 	}
423232bc201eSXin Long 
423386872cb5SThomas Graf 	err = 0;
423486872cb5SThomas Graf errout:
423586872cb5SThomas Graf 	return err;
42361da177e4SLinus Torvalds }
42371da177e4SLinus Torvalds 
42386b9ea5a6SRoopa Prabhu struct rt6_nh {
42398d1c802bSDavid Ahern 	struct fib6_info *fib6_info;
42406b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
42416b9ea5a6SRoopa Prabhu 	struct list_head next;
42426b9ea5a6SRoopa Prabhu };
42436b9ea5a6SRoopa Prabhu 
42446b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
42456b9ea5a6SRoopa Prabhu {
42466b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
42476b9ea5a6SRoopa Prabhu 
42486b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
42497d4d5065SDavid Ahern 		pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n",
42506b9ea5a6SRoopa Prabhu 		        &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
42516b9ea5a6SRoopa Prabhu 		        nh->r_cfg.fc_ifindex);
42526b9ea5a6SRoopa Prabhu 	}
42536b9ea5a6SRoopa Prabhu }
42546b9ea5a6SRoopa Prabhu 
4255d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net,
4256d4ead6b3SDavid Ahern 				 struct list_head *rt6_nh_list,
42578d1c802bSDavid Ahern 				 struct fib6_info *rt,
42588d1c802bSDavid Ahern 				 struct fib6_config *r_cfg)
42596b9ea5a6SRoopa Prabhu {
42606b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
42616b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
42626b9ea5a6SRoopa Prabhu 
42636b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
42648d1c802bSDavid Ahern 		/* check if fib6_info already exists */
42658d1c802bSDavid Ahern 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
42666b9ea5a6SRoopa Prabhu 			return err;
42676b9ea5a6SRoopa Prabhu 	}
42686b9ea5a6SRoopa Prabhu 
42696b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
42706b9ea5a6SRoopa Prabhu 	if (!nh)
42716b9ea5a6SRoopa Prabhu 		return -ENOMEM;
42728d1c802bSDavid Ahern 	nh->fib6_info = rt;
42736b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
42746b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
42756b9ea5a6SRoopa Prabhu 
42766b9ea5a6SRoopa Prabhu 	return 0;
42776b9ea5a6SRoopa Prabhu }
42786b9ea5a6SRoopa Prabhu 
42798d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt,
42808d1c802bSDavid Ahern 				   struct fib6_info *rt_last,
42813b1137feSDavid Ahern 				   struct nl_info *info,
42823b1137feSDavid Ahern 				   __u16 nlflags)
42833b1137feSDavid Ahern {
42843b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
42853b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
42863b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
42873b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
42883b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
42893b1137feSDavid Ahern 	 */
429093c2fb25SDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
429193c2fb25SDavid Ahern 		rt = list_first_entry(&rt_last->fib6_siblings,
42928d1c802bSDavid Ahern 				      struct fib6_info,
429393c2fb25SDavid Ahern 				      fib6_siblings);
42943b1137feSDavid Ahern 	}
42953b1137feSDavid Ahern 
42963b1137feSDavid Ahern 	if (rt)
42973b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
42983b1137feSDavid Ahern }
42993b1137feSDavid Ahern 
4300333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
4301333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
430251ebd318SNicolas Dichtel {
43038d1c802bSDavid Ahern 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
43043b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
430551ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
430651ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
43078d1c802bSDavid Ahern 	struct fib6_info *rt;
43086b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
43096b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
43103b1137feSDavid Ahern 	__u16 nlflags;
431151ebd318SNicolas Dichtel 	int remaining;
431251ebd318SNicolas Dichtel 	int attrlen;
43136b9ea5a6SRoopa Prabhu 	int err = 1;
43146b9ea5a6SRoopa Prabhu 	int nhn = 0;
43156b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
43166b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
43176b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
431851ebd318SNicolas Dichtel 
43193b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
43203b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
43213b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
43223b1137feSDavid Ahern 
432335f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
432451ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
432551ebd318SNicolas Dichtel 
43266b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
43278d1c802bSDavid Ahern 	 * fib6_info structs per nexthop
43286b9ea5a6SRoopa Prabhu 	 */
432951ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
433051ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
433151ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
433251ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
433351ebd318SNicolas Dichtel 
433451ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
433551ebd318SNicolas Dichtel 		if (attrlen > 0) {
433651ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
433751ebd318SNicolas Dichtel 
433851ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
433951ebd318SNicolas Dichtel 			if (nla) {
434067b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
434151ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
434251ebd318SNicolas Dichtel 			}
434319e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
434419e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
434519e42e45SRoopa Prabhu 			if (nla)
434619e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
434751ebd318SNicolas Dichtel 		}
43486b9ea5a6SRoopa Prabhu 
434968e2ffdeSDavid Ahern 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
4350acb54e3cSDavid Ahern 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
43518c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
43528c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
43538c5b83f0SRoopa Prabhu 			rt = NULL;
43546b9ea5a6SRoopa Prabhu 			goto cleanup;
43558c5b83f0SRoopa Prabhu 		}
4356b5d2d75eSDavid Ahern 		if (!rt6_qualify_for_ecmp(rt)) {
4357b5d2d75eSDavid Ahern 			err = -EINVAL;
4358b5d2d75eSDavid Ahern 			NL_SET_ERR_MSG(extack,
4359b5d2d75eSDavid Ahern 				       "Device only routes can not be added for IPv6 using the multipath API.");
4360b5d2d75eSDavid Ahern 			fib6_info_release(rt);
4361b5d2d75eSDavid Ahern 			goto cleanup;
4362b5d2d75eSDavid Ahern 		}
43636b9ea5a6SRoopa Prabhu 
43645e670d84SDavid Ahern 		rt->fib6_nh.nh_weight = rtnh->rtnh_hops + 1;
4365398958aeSIdo Schimmel 
4366d4ead6b3SDavid Ahern 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4367d4ead6b3SDavid Ahern 					    rt, &r_cfg);
436851ebd318SNicolas Dichtel 		if (err) {
436993531c67SDavid Ahern 			fib6_info_release(rt);
43706b9ea5a6SRoopa Prabhu 			goto cleanup;
437151ebd318SNicolas Dichtel 		}
43726b9ea5a6SRoopa Prabhu 
43736b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
437451ebd318SNicolas Dichtel 	}
43756b9ea5a6SRoopa Prabhu 
43763b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
43773b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
43783b1137feSDavid Ahern 	 * the full route when done
43793b1137feSDavid Ahern 	 */
43803b1137feSDavid Ahern 	info->skip_notify = 1;
43813b1137feSDavid Ahern 
43826b9ea5a6SRoopa Prabhu 	err_nh = NULL;
43836b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
43848d1c802bSDavid Ahern 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
43858d1c802bSDavid Ahern 		fib6_info_release(nh->fib6_info);
43863b1137feSDavid Ahern 
4387f7225172SDavid Ahern 		if (!err) {
4388f7225172SDavid Ahern 			/* save reference to last route successfully inserted */
4389f7225172SDavid Ahern 			rt_last = nh->fib6_info;
4390f7225172SDavid Ahern 
43916b9ea5a6SRoopa Prabhu 			/* save reference to first route for notification */
4392f7225172SDavid Ahern 			if (!rt_notif)
43938d1c802bSDavid Ahern 				rt_notif = nh->fib6_info;
4394f7225172SDavid Ahern 		}
43956b9ea5a6SRoopa Prabhu 
43968d1c802bSDavid Ahern 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
43978d1c802bSDavid Ahern 		nh->fib6_info = NULL;
43986b9ea5a6SRoopa Prabhu 		if (err) {
43996b9ea5a6SRoopa Prabhu 			if (replace && nhn)
44006b9ea5a6SRoopa Prabhu 				ip6_print_replace_route_err(&rt6_nh_list);
44016b9ea5a6SRoopa Prabhu 			err_nh = nh;
44026b9ea5a6SRoopa Prabhu 			goto add_errout;
44036b9ea5a6SRoopa Prabhu 		}
44046b9ea5a6SRoopa Prabhu 
44051a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
440627596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
440727596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
440827596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
440927596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
441027596472SMichal Kubeček 		 * be added to it.
44111a72418bSNicolas Dichtel 		 */
441227596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
441327596472SMichal Kubeček 						     NLM_F_REPLACE);
44146b9ea5a6SRoopa Prabhu 		nhn++;
44156b9ea5a6SRoopa Prabhu 	}
44166b9ea5a6SRoopa Prabhu 
44173b1137feSDavid Ahern 	/* success ... tell user about new route */
44183b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
44196b9ea5a6SRoopa Prabhu 	goto cleanup;
44206b9ea5a6SRoopa Prabhu 
44216b9ea5a6SRoopa Prabhu add_errout:
44223b1137feSDavid Ahern 	/* send notification for routes that were added so that
44233b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
44243b1137feSDavid Ahern 	 * coherent
44253b1137feSDavid Ahern 	 */
44263b1137feSDavid Ahern 	if (rt_notif)
44273b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
44283b1137feSDavid Ahern 
44296b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
44306b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
44316b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
44326b9ea5a6SRoopa Prabhu 			break;
4433333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
44346b9ea5a6SRoopa Prabhu 	}
44356b9ea5a6SRoopa Prabhu 
44366b9ea5a6SRoopa Prabhu cleanup:
44376b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
44388d1c802bSDavid Ahern 		if (nh->fib6_info)
44398d1c802bSDavid Ahern 			fib6_info_release(nh->fib6_info);
44406b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
44416b9ea5a6SRoopa Prabhu 		kfree(nh);
44426b9ea5a6SRoopa Prabhu 	}
44436b9ea5a6SRoopa Prabhu 
44446b9ea5a6SRoopa Prabhu 	return err;
44456b9ea5a6SRoopa Prabhu }
44466b9ea5a6SRoopa Prabhu 
4447333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
4448333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
44496b9ea5a6SRoopa Prabhu {
44506b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
44516b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
44526b9ea5a6SRoopa Prabhu 	int remaining;
44536b9ea5a6SRoopa Prabhu 	int attrlen;
44546b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
44556b9ea5a6SRoopa Prabhu 
44566b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
44576b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
44586b9ea5a6SRoopa Prabhu 
44596b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
44606b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
44616b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
44626b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
44636b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
44646b9ea5a6SRoopa Prabhu 
44656b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
44666b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
44676b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
44686b9ea5a6SRoopa Prabhu 
44696b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
44706b9ea5a6SRoopa Prabhu 			if (nla) {
44716b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
44726b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
44736b9ea5a6SRoopa Prabhu 			}
44746b9ea5a6SRoopa Prabhu 		}
4475333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
44766b9ea5a6SRoopa Prabhu 		if (err)
44776b9ea5a6SRoopa Prabhu 			last_err = err;
44786b9ea5a6SRoopa Prabhu 
447951ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
448051ebd318SNicolas Dichtel 	}
448151ebd318SNicolas Dichtel 
448251ebd318SNicolas Dichtel 	return last_err;
448351ebd318SNicolas Dichtel }
448451ebd318SNicolas Dichtel 
4485c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4486c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
44871da177e4SLinus Torvalds {
448886872cb5SThomas Graf 	struct fib6_config cfg;
448986872cb5SThomas Graf 	int err;
44901da177e4SLinus Torvalds 
4491333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
449286872cb5SThomas Graf 	if (err < 0)
449386872cb5SThomas Graf 		return err;
449486872cb5SThomas Graf 
449551ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4496333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
44970ae81335SDavid Ahern 	else {
44980ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
4499333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
45001da177e4SLinus Torvalds 	}
45010ae81335SDavid Ahern }
45021da177e4SLinus Torvalds 
4503c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4504c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
45051da177e4SLinus Torvalds {
450686872cb5SThomas Graf 	struct fib6_config cfg;
450786872cb5SThomas Graf 	int err;
45081da177e4SLinus Torvalds 
4509333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
451086872cb5SThomas Graf 	if (err < 0)
451186872cb5SThomas Graf 		return err;
451286872cb5SThomas Graf 
451351ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4514333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
451551ebd318SNicolas Dichtel 	else
4516acb54e3cSDavid Ahern 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
45171da177e4SLinus Torvalds }
45181da177e4SLinus Torvalds 
45198d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt)
4520339bf98fSThomas Graf {
4521beb1afacSDavid Ahern 	int nexthop_len = 0;
4522beb1afacSDavid Ahern 
452393c2fb25SDavid Ahern 	if (rt->fib6_nsiblings) {
4524beb1afacSDavid Ahern 		nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
4525beb1afacSDavid Ahern 			    + NLA_ALIGN(sizeof(struct rtnexthop))
4526beb1afacSDavid Ahern 			    + nla_total_size(16) /* RTA_GATEWAY */
45275e670d84SDavid Ahern 			    + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate);
4528beb1afacSDavid Ahern 
452993c2fb25SDavid Ahern 		nexthop_len *= rt->fib6_nsiblings;
4530beb1afacSDavid Ahern 	}
4531beb1afacSDavid Ahern 
4532339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
4533339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
4534339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
4535339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
4536339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
4537339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
4538339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
4539339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
4540339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
45416a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
4542ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
4543c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
454419e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
45455e670d84SDavid Ahern 	       + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate)
4546beb1afacSDavid Ahern 	       + nexthop_len;
4547beb1afacSDavid Ahern }
4548beb1afacSDavid Ahern 
45498d1c802bSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct fib6_info *rt,
45505be083ceSDavid Ahern 			    unsigned int *flags, bool skip_oif)
4551beb1afacSDavid Ahern {
45525e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
4553f9d882eaSIdo Schimmel 		*flags |= RTNH_F_DEAD;
4554f9d882eaSIdo Schimmel 
45555e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN) {
4556beb1afacSDavid Ahern 		*flags |= RTNH_F_LINKDOWN;
4557dcd1f572SDavid Ahern 
4558dcd1f572SDavid Ahern 		rcu_read_lock();
4559dcd1f572SDavid Ahern 		if (fib6_ignore_linkdown(rt))
4560beb1afacSDavid Ahern 			*flags |= RTNH_F_DEAD;
4561dcd1f572SDavid Ahern 		rcu_read_unlock();
4562beb1afacSDavid Ahern 	}
4563beb1afacSDavid Ahern 
456493c2fb25SDavid Ahern 	if (rt->fib6_flags & RTF_GATEWAY) {
45655e670d84SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->fib6_nh.nh_gw) < 0)
4566beb1afacSDavid Ahern 			goto nla_put_failure;
4567beb1afacSDavid Ahern 	}
4568beb1afacSDavid Ahern 
45695e670d84SDavid Ahern 	*flags |= (rt->fib6_nh.nh_flags & RTNH_F_ONLINK);
45705e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_OFFLOAD)
457161e4d01eSIdo Schimmel 		*flags |= RTNH_F_OFFLOAD;
457261e4d01eSIdo Schimmel 
45735be083ceSDavid Ahern 	/* not needed for multipath encoding b/c it has a rtnexthop struct */
45745e670d84SDavid Ahern 	if (!skip_oif && rt->fib6_nh.nh_dev &&
45755e670d84SDavid Ahern 	    nla_put_u32(skb, RTA_OIF, rt->fib6_nh.nh_dev->ifindex))
4576beb1afacSDavid Ahern 		goto nla_put_failure;
4577beb1afacSDavid Ahern 
45785e670d84SDavid Ahern 	if (rt->fib6_nh.nh_lwtstate &&
45795e670d84SDavid Ahern 	    lwtunnel_fill_encap(skb, rt->fib6_nh.nh_lwtstate) < 0)
4580beb1afacSDavid Ahern 		goto nla_put_failure;
4581beb1afacSDavid Ahern 
4582beb1afacSDavid Ahern 	return 0;
4583beb1afacSDavid Ahern 
4584beb1afacSDavid Ahern nla_put_failure:
4585beb1afacSDavid Ahern 	return -EMSGSIZE;
4586beb1afacSDavid Ahern }
4587beb1afacSDavid Ahern 
45885be083ceSDavid Ahern /* add multipath next hop */
45898d1c802bSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct fib6_info *rt)
4590beb1afacSDavid Ahern {
45915e670d84SDavid Ahern 	const struct net_device *dev = rt->fib6_nh.nh_dev;
4592beb1afacSDavid Ahern 	struct rtnexthop *rtnh;
4593beb1afacSDavid Ahern 	unsigned int flags = 0;
4594beb1afacSDavid Ahern 
4595beb1afacSDavid Ahern 	rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
4596beb1afacSDavid Ahern 	if (!rtnh)
4597beb1afacSDavid Ahern 		goto nla_put_failure;
4598beb1afacSDavid Ahern 
45995e670d84SDavid Ahern 	rtnh->rtnh_hops = rt->fib6_nh.nh_weight - 1;
46005e670d84SDavid Ahern 	rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
4601beb1afacSDavid Ahern 
46025be083ceSDavid Ahern 	if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
4603beb1afacSDavid Ahern 		goto nla_put_failure;
4604beb1afacSDavid Ahern 
4605beb1afacSDavid Ahern 	rtnh->rtnh_flags = flags;
4606beb1afacSDavid Ahern 
4607beb1afacSDavid Ahern 	/* length of rtnetlink header + attributes */
4608beb1afacSDavid Ahern 	rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
4609beb1afacSDavid Ahern 
4610beb1afacSDavid Ahern 	return 0;
4611beb1afacSDavid Ahern 
4612beb1afacSDavid Ahern nla_put_failure:
4613beb1afacSDavid Ahern 	return -EMSGSIZE;
4614339bf98fSThomas Graf }
4615339bf98fSThomas Graf 
4616d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
46178d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
4618d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
461915e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
4620f8cfe2ceSDavid Ahern 			 unsigned int flags)
46211da177e4SLinus Torvalds {
462222d0bd82SXin Long 	struct rt6_info *rt6 = (struct rt6_info *)dst;
462322d0bd82SXin Long 	struct rt6key *rt6_dst, *rt6_src;
462422d0bd82SXin Long 	u32 *pmetrics, table, rt6_flags;
46251da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
462622d0bd82SXin Long 	struct rtmsg *rtm;
4627d4ead6b3SDavid Ahern 	long expires = 0;
46281da177e4SLinus Torvalds 
462915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
463038308473SDavid S. Miller 	if (!nlh)
463126932566SPatrick McHardy 		return -EMSGSIZE;
46322d7202bfSThomas Graf 
463322d0bd82SXin Long 	if (rt6) {
463422d0bd82SXin Long 		rt6_dst = &rt6->rt6i_dst;
463522d0bd82SXin Long 		rt6_src = &rt6->rt6i_src;
463622d0bd82SXin Long 		rt6_flags = rt6->rt6i_flags;
463722d0bd82SXin Long 	} else {
463822d0bd82SXin Long 		rt6_dst = &rt->fib6_dst;
463922d0bd82SXin Long 		rt6_src = &rt->fib6_src;
464022d0bd82SXin Long 		rt6_flags = rt->fib6_flags;
464122d0bd82SXin Long 	}
464222d0bd82SXin Long 
46432d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
46441da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
464522d0bd82SXin Long 	rtm->rtm_dst_len = rt6_dst->plen;
464622d0bd82SXin Long 	rtm->rtm_src_len = rt6_src->plen;
46471da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
464893c2fb25SDavid Ahern 	if (rt->fib6_table)
464993c2fb25SDavid Ahern 		table = rt->fib6_table->tb6_id;
4650c71099acSThomas Graf 	else
46519e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
46529e762a4aSPatrick McHardy 	rtm->rtm_table = table;
4653c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
4654c78679e8SDavid S. Miller 		goto nla_put_failure;
4655e8478e80SDavid Ahern 
4656e8478e80SDavid Ahern 	rtm->rtm_type = rt->fib6_type;
46571da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
46581da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
465993c2fb25SDavid Ahern 	rtm->rtm_protocol = rt->fib6_protocol;
46601da177e4SLinus Torvalds 
466122d0bd82SXin Long 	if (rt6_flags & RTF_CACHE)
46621da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
46631da177e4SLinus Torvalds 
4664d4ead6b3SDavid Ahern 	if (dest) {
4665d4ead6b3SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, dest))
4666c78679e8SDavid S. Miller 			goto nla_put_failure;
46671da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
46681da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
466922d0bd82SXin Long 		if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
4670c78679e8SDavid S. Miller 			goto nla_put_failure;
46711da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
46721da177e4SLinus Torvalds 	if (src) {
4673930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
4674c78679e8SDavid S. Miller 			goto nla_put_failure;
46751da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
4676c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
467722d0bd82SXin Long 		   nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
4678c78679e8SDavid S. Miller 		goto nla_put_failure;
46791da177e4SLinus Torvalds #endif
46807bc570c8SYOSHIFUJI Hideaki 	if (iif) {
46817bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
468222d0bd82SXin Long 		if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
4683fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
46842cf75070SNikolay Aleksandrov 
46857bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
46867bc570c8SYOSHIFUJI Hideaki 				return 0;
4687fd61c6baSDavid Ahern 			if (err < 0)
46887bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
46897bc570c8SYOSHIFUJI Hideaki 		} else
46907bc570c8SYOSHIFUJI Hideaki #endif
4691c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
4692c78679e8SDavid S. Miller 				goto nla_put_failure;
4693d4ead6b3SDavid Ahern 	} else if (dest) {
46941da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
4695d4ead6b3SDavid Ahern 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
4696930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4697c78679e8SDavid S. Miller 			goto nla_put_failure;
4698c3968a85SDaniel Walter 	}
4699c3968a85SDaniel Walter 
470093c2fb25SDavid Ahern 	if (rt->fib6_prefsrc.plen) {
4701c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
470293c2fb25SDavid Ahern 		saddr_buf = rt->fib6_prefsrc.addr;
4703930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4704c78679e8SDavid S. Miller 			goto nla_put_failure;
47051da177e4SLinus Torvalds 	}
47062d7202bfSThomas Graf 
4707d4ead6b3SDavid Ahern 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4708d4ead6b3SDavid Ahern 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
47092d7202bfSThomas Graf 		goto nla_put_failure;
47102d7202bfSThomas Graf 
471193c2fb25SDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
4712beb1afacSDavid Ahern 		goto nla_put_failure;
4713beb1afacSDavid Ahern 
4714beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
4715beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
4716beb1afacSDavid Ahern 	 */
471722d0bd82SXin Long 	if (rt6) {
471822d0bd82SXin Long 		if (rt6_flags & RTF_GATEWAY &&
471922d0bd82SXin Long 		    nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
472022d0bd82SXin Long 			goto nla_put_failure;
472122d0bd82SXin Long 
472222d0bd82SXin Long 		if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
472322d0bd82SXin Long 			goto nla_put_failure;
472422d0bd82SXin Long 	} else if (rt->fib6_nsiblings) {
47258d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
4726beb1afacSDavid Ahern 		struct nlattr *mp;
4727beb1afacSDavid Ahern 
4728beb1afacSDavid Ahern 		mp = nla_nest_start(skb, RTA_MULTIPATH);
4729beb1afacSDavid Ahern 		if (!mp)
4730beb1afacSDavid Ahern 			goto nla_put_failure;
4731beb1afacSDavid Ahern 
4732beb1afacSDavid Ahern 		if (rt6_add_nexthop(skb, rt) < 0)
4733beb1afacSDavid Ahern 			goto nla_put_failure;
4734beb1afacSDavid Ahern 
4735beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
473693c2fb25SDavid Ahern 					 &rt->fib6_siblings, fib6_siblings) {
4737beb1afacSDavid Ahern 			if (rt6_add_nexthop(skb, sibling) < 0)
473894f826b8SEric Dumazet 				goto nla_put_failure;
473994f826b8SEric Dumazet 		}
47402d7202bfSThomas Graf 
4741beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
4742beb1afacSDavid Ahern 	} else {
47435be083ceSDavid Ahern 		if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
4744c78679e8SDavid S. Miller 			goto nla_put_failure;
4745beb1afacSDavid Ahern 	}
47468253947eSLi Wei 
474722d0bd82SXin Long 	if (rt6_flags & RTF_EXPIRES) {
474814895687SDavid Ahern 		expires = dst ? dst->expires : rt->expires;
474914895687SDavid Ahern 		expires -= jiffies;
475014895687SDavid Ahern 	}
475169cdf8f9SYOSHIFUJI Hideaki 
4752d4ead6b3SDavid Ahern 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
4753e3703b3dSThomas Graf 		goto nla_put_failure;
47541da177e4SLinus Torvalds 
475522d0bd82SXin Long 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
4756c78ba6d6SLubomir Rintel 		goto nla_put_failure;
4757c78ba6d6SLubomir Rintel 
475819e42e45SRoopa Prabhu 
4759053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4760053c095aSJohannes Berg 	return 0;
47612d7202bfSThomas Graf 
47622d7202bfSThomas Graf nla_put_failure:
476326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
476426932566SPatrick McHardy 	return -EMSGSIZE;
47651da177e4SLinus Torvalds }
47661da177e4SLinus Torvalds 
47678d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg)
47681da177e4SLinus Torvalds {
47691da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
47701f17e2f2SDavid Ahern 	struct net *net = arg->net;
47711f17e2f2SDavid Ahern 
4772421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
47731f17e2f2SDavid Ahern 		return 0;
47741da177e4SLinus Torvalds 
47752d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
47762d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
4777f8cfe2ceSDavid Ahern 
4778f8cfe2ceSDavid Ahern 		/* user wants prefix routes only */
4779f8cfe2ceSDavid Ahern 		if (rtm->rtm_flags & RTM_F_PREFIX &&
478093c2fb25SDavid Ahern 		    !(rt->fib6_flags & RTF_PREFIX_RT)) {
4781f8cfe2ceSDavid Ahern 			/* success since this is not a prefix route */
4782f8cfe2ceSDavid Ahern 			return 1;
4783f8cfe2ceSDavid Ahern 		}
4784f8cfe2ceSDavid Ahern 	}
47851da177e4SLinus Torvalds 
4786d4ead6b3SDavid Ahern 	return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4787d4ead6b3SDavid Ahern 			     RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
4788d4ead6b3SDavid Ahern 			     arg->cb->nlh->nlmsg_seq, NLM_F_MULTI);
47891da177e4SLinus Torvalds }
47901da177e4SLinus Torvalds 
4791c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4792c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
47931da177e4SLinus Torvalds {
47943b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
4795ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
479618c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
4797a68886a6SDavid Ahern 	struct fib6_info *from;
479818c3a61cSRoopa Prabhu 	struct dst_entry *dst;
47991da177e4SLinus Torvalds 	struct rt6_info *rt;
4800ab364a6fSThomas Graf 	struct sk_buff *skb;
4801ab364a6fSThomas Graf 	struct rtmsg *rtm;
4802744486d4SMaciej Żenczykowski 	struct flowi6 fl6 = {};
480318c3a61cSRoopa Prabhu 	bool fibmatch;
4804ab364a6fSThomas Graf 
4805fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4806c21ef3e3SDavid Ahern 			  extack);
4807ab364a6fSThomas Graf 	if (err < 0)
4808ab364a6fSThomas Graf 		goto errout;
4809ab364a6fSThomas Graf 
4810ab364a6fSThomas Graf 	err = -EINVAL;
481138b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
481238b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
481318c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
4814ab364a6fSThomas Graf 
4815ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
4816ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4817ab364a6fSThomas Graf 			goto errout;
4818ab364a6fSThomas Graf 
48194e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
4820ab364a6fSThomas Graf 	}
4821ab364a6fSThomas Graf 
4822ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
4823ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4824ab364a6fSThomas Graf 			goto errout;
4825ab364a6fSThomas Graf 
48264e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
4827ab364a6fSThomas Graf 	}
4828ab364a6fSThomas Graf 
4829ab364a6fSThomas Graf 	if (tb[RTA_IIF])
4830ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
4831ab364a6fSThomas Graf 
4832ab364a6fSThomas Graf 	if (tb[RTA_OIF])
483372331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
4834ab364a6fSThomas Graf 
48352e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
48362e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
48372e47b291SLorenzo Colitti 
4838622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
4839622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
4840622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
4841622ec2c9SLorenzo Colitti 	else
4842622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4843622ec2c9SLorenzo Colitti 
4844eacb9384SRoopa Prabhu 	if (tb[RTA_SPORT])
4845eacb9384SRoopa Prabhu 		fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
4846eacb9384SRoopa Prabhu 
4847eacb9384SRoopa Prabhu 	if (tb[RTA_DPORT])
4848eacb9384SRoopa Prabhu 		fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
4849eacb9384SRoopa Prabhu 
4850eacb9384SRoopa Prabhu 	if (tb[RTA_IP_PROTO]) {
4851eacb9384SRoopa Prabhu 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
4852eacb9384SRoopa Prabhu 						  &fl6.flowi6_proto, extack);
4853eacb9384SRoopa Prabhu 		if (err)
4854eacb9384SRoopa Prabhu 			goto errout;
4855eacb9384SRoopa Prabhu 	}
4856eacb9384SRoopa Prabhu 
4857ab364a6fSThomas Graf 	if (iif) {
4858ab364a6fSThomas Graf 		struct net_device *dev;
485972331bc0SShmulik Ladkani 		int flags = 0;
486072331bc0SShmulik Ladkani 
4861121622dbSFlorian Westphal 		rcu_read_lock();
4862121622dbSFlorian Westphal 
4863121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
4864ab364a6fSThomas Graf 		if (!dev) {
4865121622dbSFlorian Westphal 			rcu_read_unlock();
4866ab364a6fSThomas Graf 			err = -ENODEV;
4867ab364a6fSThomas Graf 			goto errout;
4868ab364a6fSThomas Graf 		}
486972331bc0SShmulik Ladkani 
487072331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
487172331bc0SShmulik Ladkani 
487272331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
487372331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
487472331bc0SShmulik Ladkani 
4875b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
4876121622dbSFlorian Westphal 
4877121622dbSFlorian Westphal 		rcu_read_unlock();
487872331bc0SShmulik Ladkani 	} else {
487972331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
488072331bc0SShmulik Ladkani 
488118c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
488218c3a61cSRoopa Prabhu 	}
488318c3a61cSRoopa Prabhu 
488418c3a61cSRoopa Prabhu 
488518c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
488618c3a61cSRoopa Prabhu 	if (rt->dst.error) {
488718c3a61cSRoopa Prabhu 		err = rt->dst.error;
488818c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
488918c3a61cSRoopa Prabhu 		goto errout;
4890ab364a6fSThomas Graf 	}
48911da177e4SLinus Torvalds 
48929d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
48939d6acb3bSWANG Cong 		err = rt->dst.error;
48949d6acb3bSWANG Cong 		ip6_rt_put(rt);
48959d6acb3bSWANG Cong 		goto errout;
48969d6acb3bSWANG Cong 	}
48979d6acb3bSWANG Cong 
48981da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
489938308473SDavid S. Miller 	if (!skb) {
490094e187c0SAmerigo Wang 		ip6_rt_put(rt);
4901ab364a6fSThomas Graf 		err = -ENOBUFS;
4902ab364a6fSThomas Graf 		goto errout;
4903ab364a6fSThomas Graf 	}
49041da177e4SLinus Torvalds 
4905d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
4906a68886a6SDavid Ahern 
4907a68886a6SDavid Ahern 	rcu_read_lock();
4908a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
4909a68886a6SDavid Ahern 
491018c3a61cSRoopa Prabhu 	if (fibmatch)
4911a68886a6SDavid Ahern 		err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, iif,
491218c3a61cSRoopa Prabhu 				    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
491318c3a61cSRoopa Prabhu 				    nlh->nlmsg_seq, 0);
491418c3a61cSRoopa Prabhu 	else
4915a68886a6SDavid Ahern 		err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
4916a68886a6SDavid Ahern 				    &fl6.saddr, iif, RTM_NEWROUTE,
4917d4ead6b3SDavid Ahern 				    NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
4918d4ead6b3SDavid Ahern 				    0);
4919a68886a6SDavid Ahern 	rcu_read_unlock();
4920a68886a6SDavid Ahern 
49211da177e4SLinus Torvalds 	if (err < 0) {
4922ab364a6fSThomas Graf 		kfree_skb(skb);
4923ab364a6fSThomas Graf 		goto errout;
49241da177e4SLinus Torvalds 	}
49251da177e4SLinus Torvalds 
492615e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4927ab364a6fSThomas Graf errout:
49281da177e4SLinus Torvalds 	return err;
49291da177e4SLinus Torvalds }
49301da177e4SLinus Torvalds 
49318d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
493237a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
49331da177e4SLinus Torvalds {
49341da177e4SLinus Torvalds 	struct sk_buff *skb;
49355578689aSDaniel Lezcano 	struct net *net = info->nl_net;
4936528c4cebSDenis V. Lunev 	u32 seq;
4937528c4cebSDenis V. Lunev 	int err;
49380d51aa80SJamal Hadi Salim 
4939528c4cebSDenis V. Lunev 	err = -ENOBUFS;
494038308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
494186872cb5SThomas Graf 
494219e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
494338308473SDavid S. Miller 	if (!skb)
494421713ebcSThomas Graf 		goto errout;
49451da177e4SLinus Torvalds 
4946d4ead6b3SDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
4947f8cfe2ceSDavid Ahern 			    event, info->portid, seq, nlm_flags);
494826932566SPatrick McHardy 	if (err < 0) {
494926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
495026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
495126932566SPatrick McHardy 		kfree_skb(skb);
495226932566SPatrick McHardy 		goto errout;
495326932566SPatrick McHardy 	}
495415e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
49555578689aSDaniel Lezcano 		    info->nlh, gfp_any());
49561ce85fe4SPablo Neira Ayuso 	return;
495721713ebcSThomas Graf errout:
495821713ebcSThomas Graf 	if (err < 0)
49595578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
49601da177e4SLinus Torvalds }
49611da177e4SLinus Torvalds 
49628ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
4963351638e7SJiri Pirko 				unsigned long event, void *ptr)
49648ed67789SDaniel Lezcano {
4965351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4966c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
49678ed67789SDaniel Lezcano 
4968242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
4969242d3a49SWANG Cong 		return NOTIFY_OK;
4970242d3a49SWANG Cong 
4971242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
4972421842edSDavid Ahern 		net->ipv6.fib6_null_entry->fib6_nh.nh_dev = dev;
4973d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
49748ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
49758ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
4976d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
49778ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
4978d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
49798ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
49808ed67789SDaniel Lezcano #endif
498176da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
498276da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
498376da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
498476da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
498576da0704SWANG Cong 		 */
498612d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
4987242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
498812d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
498912d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
4990242d3a49SWANG Cong #endif
49918ed67789SDaniel Lezcano 	}
49928ed67789SDaniel Lezcano 
49938ed67789SDaniel Lezcano 	return NOTIFY_OK;
49948ed67789SDaniel Lezcano }
49958ed67789SDaniel Lezcano 
49961da177e4SLinus Torvalds /*
49971da177e4SLinus Torvalds  *	/proc
49981da177e4SLinus Torvalds  */
49991da177e4SLinus Torvalds 
50001da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
50011da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
50021da177e4SLinus Torvalds {
500369ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
50041da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
500569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
500669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
500781eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
500869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
500969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
5010fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
501169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
50121da177e4SLinus Torvalds 
50131da177e4SLinus Torvalds 	return 0;
50141da177e4SLinus Torvalds }
50151da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
50161da177e4SLinus Torvalds 
50171da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
50181da177e4SLinus Torvalds 
50191da177e4SLinus Torvalds static
5020fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
50211da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
50221da177e4SLinus Torvalds {
5023c486da34SLucian Adrian Grijincu 	struct net *net;
5024c486da34SLucian Adrian Grijincu 	int delay;
5025c486da34SLucian Adrian Grijincu 	if (!write)
5026c486da34SLucian Adrian Grijincu 		return -EINVAL;
5027c486da34SLucian Adrian Grijincu 
5028c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
5029c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
50308d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
50312ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
50321da177e4SLinus Torvalds 	return 0;
50331da177e4SLinus Torvalds }
50341da177e4SLinus Torvalds 
5035fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = {
50361da177e4SLinus Torvalds 	{
50371da177e4SLinus Torvalds 		.procname	=	"flush",
50384990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
50391da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
504089c8b3a1SDave Jones 		.mode		=	0200,
50416d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
50421da177e4SLinus Torvalds 	},
50431da177e4SLinus Torvalds 	{
50441da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
50459a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
50461da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50471da177e4SLinus Torvalds 		.mode		=	0644,
50486d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
50491da177e4SLinus Torvalds 	},
50501da177e4SLinus Torvalds 	{
50511da177e4SLinus Torvalds 		.procname	=	"max_size",
50524990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
50531da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50541da177e4SLinus Torvalds 		.mode		=	0644,
50556d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
50561da177e4SLinus Torvalds 	},
50571da177e4SLinus Torvalds 	{
50581da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
50594990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
50601da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50611da177e4SLinus Torvalds 		.mode		=	0644,
50626d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
50631da177e4SLinus Torvalds 	},
50641da177e4SLinus Torvalds 	{
50651da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
50664990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
50671da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50681da177e4SLinus Torvalds 		.mode		=	0644,
50696d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
50701da177e4SLinus Torvalds 	},
50711da177e4SLinus Torvalds 	{
50721da177e4SLinus Torvalds 		.procname	=	"gc_interval",
50734990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
50741da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50751da177e4SLinus Torvalds 		.mode		=	0644,
50766d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
50771da177e4SLinus Torvalds 	},
50781da177e4SLinus Torvalds 	{
50791da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
50804990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
50811da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50821da177e4SLinus Torvalds 		.mode		=	0644,
5083f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
50841da177e4SLinus Torvalds 	},
50851da177e4SLinus Torvalds 	{
50861da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
50874990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
50881da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50891da177e4SLinus Torvalds 		.mode		=	0644,
50906d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
50911da177e4SLinus Torvalds 	},
50921da177e4SLinus Torvalds 	{
50931da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
50944990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
50951da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50961da177e4SLinus Torvalds 		.mode		=	0644,
5097f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
50981da177e4SLinus Torvalds 	},
50991da177e4SLinus Torvalds 	{
51001da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
51014990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
51021da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51031da177e4SLinus Torvalds 		.mode		=	0644,
51046d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
51051da177e4SLinus Torvalds 	},
5106f8572d8fSEric W. Biederman 	{ }
51071da177e4SLinus Torvalds };
51081da177e4SLinus Torvalds 
51092c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
5110760f2d01SDaniel Lezcano {
5111760f2d01SDaniel Lezcano 	struct ctl_table *table;
5112760f2d01SDaniel Lezcano 
5113760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
5114760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
5115760f2d01SDaniel Lezcano 			GFP_KERNEL);
51165ee09105SYOSHIFUJI Hideaki 
51175ee09105SYOSHIFUJI Hideaki 	if (table) {
51185ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
5119c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
512086393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
51215ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
51225ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
51235ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
51245ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
51255ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
51265ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
51275ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
51289c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
5129464dc801SEric W. Biederman 
5130464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
5131464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
5132464dc801SEric W. Biederman 			table[0].procname = NULL;
51335ee09105SYOSHIFUJI Hideaki 	}
51345ee09105SYOSHIFUJI Hideaki 
5135760f2d01SDaniel Lezcano 	return table;
5136760f2d01SDaniel Lezcano }
51371da177e4SLinus Torvalds #endif
51381da177e4SLinus Torvalds 
51392c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
5140cdb18761SDaniel Lezcano {
5141633d424bSPavel Emelyanov 	int ret = -ENOMEM;
51428ed67789SDaniel Lezcano 
514386393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
514486393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
5145f2fc6a54SBenjamin Thery 
5146fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5147fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
5148fc66f95cSEric Dumazet 
5149421842edSDavid Ahern 	net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template,
5150421842edSDavid Ahern 					    sizeof(*net->ipv6.fib6_null_entry),
5151421842edSDavid Ahern 					    GFP_KERNEL);
5152421842edSDavid Ahern 	if (!net->ipv6.fib6_null_entry)
5153421842edSDavid Ahern 		goto out_ip6_dst_entries;
5154421842edSDavid Ahern 
51558ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
51568ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
51578ed67789SDaniel Lezcano 					   GFP_KERNEL);
51588ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
5159421842edSDavid Ahern 		goto out_fib6_null_entry;
5160d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
516162fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
516262fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
51638ed67789SDaniel Lezcano 
51648ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5165feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
51668ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
51678ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
51688ed67789SDaniel Lezcano 					       GFP_KERNEL);
516968fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
517068fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
5171d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
517262fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
517362fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
51748ed67789SDaniel Lezcano 
51758ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
51768ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
51778ed67789SDaniel Lezcano 					       GFP_KERNEL);
517868fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
517968fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
5180d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
518162fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
518262fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
51838ed67789SDaniel Lezcano #endif
51848ed67789SDaniel Lezcano 
5185b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
5186b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
5187b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5188b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5189b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5190b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5191b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5192b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
5193b339a47cSPeter Zijlstra 
51946891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
51956891a346SBenjamin Thery 
51968ed67789SDaniel Lezcano 	ret = 0;
51978ed67789SDaniel Lezcano out:
51988ed67789SDaniel Lezcano 	return ret;
5199f2fc6a54SBenjamin Thery 
520068fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
520168fffc67SPeter Zijlstra out_ip6_prohibit_entry:
520268fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
520368fffc67SPeter Zijlstra out_ip6_null_entry:
520468fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
520568fffc67SPeter Zijlstra #endif
5206421842edSDavid Ahern out_fib6_null_entry:
5207421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
5208fc66f95cSEric Dumazet out_ip6_dst_entries:
5209fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5210f2fc6a54SBenjamin Thery out_ip6_dst_ops:
5211f2fc6a54SBenjamin Thery 	goto out;
5212cdb18761SDaniel Lezcano }
5213cdb18761SDaniel Lezcano 
52142c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
5215cdb18761SDaniel Lezcano {
5216421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
52178ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
52188ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
52198ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
52208ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
52218ed67789SDaniel Lezcano #endif
522241bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5223cdb18761SDaniel Lezcano }
5224cdb18761SDaniel Lezcano 
5225d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
5226d189634eSThomas Graf {
5227d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5228c3506372SChristoph Hellwig 	proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
5229c3506372SChristoph Hellwig 			sizeof(struct ipv6_route_iter));
52303617d949SChristoph Hellwig 	proc_create_net_single("rt6_stats", 0444, net->proc_net,
52313617d949SChristoph Hellwig 			rt6_stats_seq_show, NULL);
5232d189634eSThomas Graf #endif
5233d189634eSThomas Graf 	return 0;
5234d189634eSThomas Graf }
5235d189634eSThomas Graf 
5236d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
5237d189634eSThomas Graf {
5238d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5239ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
5240ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
5241d189634eSThomas Graf #endif
5242d189634eSThomas Graf }
5243d189634eSThomas Graf 
5244cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
5245cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
5246cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
5247cdb18761SDaniel Lezcano };
5248cdb18761SDaniel Lezcano 
5249c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
5250c3426b47SDavid S. Miller {
5251c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5252c3426b47SDavid S. Miller 
5253c3426b47SDavid S. Miller 	if (!bp)
5254c3426b47SDavid S. Miller 		return -ENOMEM;
5255c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
5256c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
5257c3426b47SDavid S. Miller 	return 0;
5258c3426b47SDavid S. Miller }
5259c3426b47SDavid S. Miller 
5260c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
5261c3426b47SDavid S. Miller {
5262c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
5263c3426b47SDavid S. Miller 
5264c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
526556a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
5266c3426b47SDavid S. Miller 	kfree(bp);
5267c3426b47SDavid S. Miller }
5268c3426b47SDavid S. Miller 
52692b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
5270c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
5271c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
5272c3426b47SDavid S. Miller };
5273c3426b47SDavid S. Miller 
5274d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
5275d189634eSThomas Graf 	.init = ip6_route_net_init_late,
5276d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
5277d189634eSThomas Graf };
5278d189634eSThomas Graf 
52798ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
52808ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
5281242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
52828ed67789SDaniel Lezcano };
52838ed67789SDaniel Lezcano 
52842f460933SWANG Cong void __init ip6_route_init_special_entries(void)
52852f460933SWANG Cong {
52862f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
52872f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
52882f460933SWANG Cong 	 * manually for init_net */
5289421842edSDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_nh.nh_dev = init_net.loopback_dev;
52902f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
52912f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
52922f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
52932f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
52942f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
52952f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
52962f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
52972f460933SWANG Cong   #endif
52982f460933SWANG Cong }
52992f460933SWANG Cong 
5300433d49c3SDaniel Lezcano int __init ip6_route_init(void)
53011da177e4SLinus Torvalds {
5302433d49c3SDaniel Lezcano 	int ret;
53038d0b94afSMartin KaFai Lau 	int cpu;
5304433d49c3SDaniel Lezcano 
53059a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
53069a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
53079a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
53089a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
53099a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
5310c19a28e1SFernando Carrijo 		goto out;
531114e50e57SDavid S. Miller 
5312fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
53138ed67789SDaniel Lezcano 	if (ret)
5314bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
5315bdb3289fSDaniel Lezcano 
5316c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5317c3426b47SDavid S. Miller 	if (ret)
5318e8803b6cSDavid S. Miller 		goto out_dst_entries;
53192a0c451aSThomas Graf 
53207e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
53217e52b33bSDavid S. Miller 	if (ret)
53227e52b33bSDavid S. Miller 		goto out_register_inetpeer;
5323c3426b47SDavid S. Miller 
53245dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
53255dc121e9SArnaud Ebalard 
5326e8803b6cSDavid S. Miller 	ret = fib6_init();
5327433d49c3SDaniel Lezcano 	if (ret)
53288ed67789SDaniel Lezcano 		goto out_register_subsys;
5329433d49c3SDaniel Lezcano 
5330433d49c3SDaniel Lezcano 	ret = xfrm6_init();
5331433d49c3SDaniel Lezcano 	if (ret)
5332e8803b6cSDavid S. Miller 		goto out_fib6_init;
5333c35b7e72SDaniel Lezcano 
5334433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
5335433d49c3SDaniel Lezcano 	if (ret)
5336433d49c3SDaniel Lezcano 		goto xfrm6_init;
53377e5449c2SDaniel Lezcano 
5338d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
5339d189634eSThomas Graf 	if (ret)
5340d189634eSThomas Graf 		goto fib6_rules_init;
5341d189634eSThomas Graf 
534216feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
534316feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
534416feebcfSFlorian Westphal 	if (ret < 0)
534516feebcfSFlorian Westphal 		goto out_register_late_subsys;
534616feebcfSFlorian Westphal 
534716feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
534816feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
534916feebcfSFlorian Westphal 	if (ret < 0)
535016feebcfSFlorian Westphal 		goto out_register_late_subsys;
535116feebcfSFlorian Westphal 
535216feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
535316feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
535416feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
535516feebcfSFlorian Westphal 	if (ret < 0)
5356d189634eSThomas Graf 		goto out_register_late_subsys;
5357433d49c3SDaniel Lezcano 
53588ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
5359cdb18761SDaniel Lezcano 	if (ret)
5360d189634eSThomas Graf 		goto out_register_late_subsys;
53618ed67789SDaniel Lezcano 
53628d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
53638d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
53648d0b94afSMartin KaFai Lau 
53658d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
53668d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
53678d0b94afSMartin KaFai Lau 	}
53688d0b94afSMartin KaFai Lau 
5369433d49c3SDaniel Lezcano out:
5370433d49c3SDaniel Lezcano 	return ret;
5371433d49c3SDaniel Lezcano 
5372d189634eSThomas Graf out_register_late_subsys:
537316feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
5374d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5375433d49c3SDaniel Lezcano fib6_rules_init:
5376433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
5377433d49c3SDaniel Lezcano xfrm6_init:
5378433d49c3SDaniel Lezcano 	xfrm6_fini();
53792a0c451aSThomas Graf out_fib6_init:
53802a0c451aSThomas Graf 	fib6_gc_cleanup();
53818ed67789SDaniel Lezcano out_register_subsys:
53828ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
53837e52b33bSDavid S. Miller out_register_inetpeer:
53847e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
5385fc66f95cSEric Dumazet out_dst_entries:
5386fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5387433d49c3SDaniel Lezcano out_kmem_cache:
5388f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
5389433d49c3SDaniel Lezcano 	goto out;
53901da177e4SLinus Torvalds }
53911da177e4SLinus Torvalds 
53921da177e4SLinus Torvalds void ip6_route_cleanup(void)
53931da177e4SLinus Torvalds {
53948ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
5395d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5396101367c2SThomas Graf 	fib6_rules_cleanup();
53971da177e4SLinus Torvalds 	xfrm6_fini();
53981da177e4SLinus Torvalds 	fib6_gc_cleanup();
5399c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
54008ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
540141bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5402f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
54031da177e4SLinus Torvalds }
5404