xref: /openbmc/linux/net/ipv6/route.c (revision 510e2ceda031eed97a7a0f9aad65d271a58b460d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Linux INET6 implementation
31da177e4SLinus Torvalds  *	FIB front-end.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
91da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
101da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
111da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds /*	Changes:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI
171da177e4SLinus Torvalds  *		reworked default router selection.
181da177e4SLinus Torvalds  *		- respect outgoing interface
191da177e4SLinus Torvalds  *		- select from (probably) reachable routers (i.e.
201da177e4SLinus Torvalds  *		routers in REACHABLE, STALE, DELAY or PROBE states).
211da177e4SLinus Torvalds  *		- always select the same router if it is (probably)
221da177e4SLinus Torvalds  *		reachable.  otherwise, round-robin the list.
23c0bece9fSYOSHIFUJI Hideaki  *	Ville Nuorvala
24c0bece9fSYOSHIFUJI Hideaki  *		Fixed routing subtrees.
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
28f3213831SJoe Perches 
294fc268d2SRandy Dunlap #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/errno.h>
31bc3b2d7fSPaul Gortmaker #include <linux/export.h>
321da177e4SLinus Torvalds #include <linux/types.h>
331da177e4SLinus Torvalds #include <linux/times.h>
341da177e4SLinus Torvalds #include <linux/socket.h>
351da177e4SLinus Torvalds #include <linux/sockios.h>
361da177e4SLinus Torvalds #include <linux/net.h>
371da177e4SLinus Torvalds #include <linux/route.h>
381da177e4SLinus Torvalds #include <linux/netdevice.h>
391da177e4SLinus Torvalds #include <linux/in6.h>
407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
411da177e4SLinus Torvalds #include <linux/init.h>
421da177e4SLinus Torvalds #include <linux/if_arp.h>
431da177e4SLinus Torvalds #include <linux/proc_fs.h>
441da177e4SLinus Torvalds #include <linux/seq_file.h>
455b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
4735732d01SWei Wang #include <linux/jhash.h>
48457c4cbcSEric W. Biederman #include <net/net_namespace.h>
491da177e4SLinus Torvalds #include <net/snmp.h>
501da177e4SLinus Torvalds #include <net/ipv6.h>
511da177e4SLinus Torvalds #include <net/ip6_fib.h>
521da177e4SLinus Torvalds #include <net/ip6_route.h>
531da177e4SLinus Torvalds #include <net/ndisc.h>
541da177e4SLinus Torvalds #include <net/addrconf.h>
551da177e4SLinus Torvalds #include <net/tcp.h>
561da177e4SLinus Torvalds #include <linux/rtnetlink.h>
571da177e4SLinus Torvalds #include <net/dst.h>
58904af04dSJiri Benc #include <net/dst_metadata.h>
591da177e4SLinus Torvalds #include <net/xfrm.h>
608d71740cSTom Tucker #include <net/netevent.h>
6121713ebcSThomas Graf #include <net/netlink.h>
623c618c1dSDavid Ahern #include <net/rtnh.h>
6319e42e45SRoopa Prabhu #include <net/lwtunnel.h>
64904af04dSJiri Benc #include <net/ip_tunnels.h>
65ca254490SDavid Ahern #include <net/l3mdev.h>
66eacb9384SRoopa Prabhu #include <net/ip.h>
677c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
701da177e4SLinus Torvalds #include <linux/sysctl.h>
711da177e4SLinus Torvalds #endif
721da177e4SLinus Torvalds 
7330d444d3SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type);
7430d444d3SDavid Ahern 
7530d444d3SDavid Ahern #define CREATE_TRACE_POINTS
7630d444d3SDavid Ahern #include <trace/events/fib6.h>
7730d444d3SDavid Ahern EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
7830d444d3SDavid Ahern #undef CREATE_TRACE_POINTS
7930d444d3SDavid Ahern 
80afc154e9SHannes Frederic Sowa enum rt6_nud_state {
817e980569SJiri Benc 	RT6_NUD_FAIL_HARD = -3,
827e980569SJiri Benc 	RT6_NUD_FAIL_PROBE = -2,
837e980569SJiri Benc 	RT6_NUD_FAIL_DO_RR = -1,
84afc154e9SHannes Frederic Sowa 	RT6_NUD_SUCCEED = 1
85afc154e9SHannes Frederic Sowa };
86afc154e9SHannes Frederic Sowa 
871da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
880dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
89ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
901da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
911da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
921da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
931da177e4SLinus Torvalds 				       struct net_device *dev, int how);
94569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
97ede2059dSEric W. Biederman static int		ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
987150aedeSKamala R static int		ip6_pkt_prohibit(struct sk_buff *skb);
99ede2059dSEric W. Biederman static int		ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
1001da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
1016700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1026700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
1036700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
1046700c270SDavid S. Miller 					struct sk_buff *skb);
105702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
106702cea56SDavid Ahern 			   int strict);
1078d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt);
108d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
1098d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
110d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
11116a16cd3SDavid Ahern 			 int iif, int type, u32 portid, u32 seq,
11216a16cd3SDavid Ahern 			 unsigned int flags);
1137e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
114*510e2cedSWei Wang 					   const struct in6_addr *daddr,
115*510e2cedSWei Wang 					   const struct in6_addr *saddr);
1161da177e4SLinus Torvalds 
11770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1188d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
119b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
120830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
121830218c1SDavid Ahern 					   struct net_device *dev,
12295c96174SEric Dumazet 					   unsigned int pref);
1238d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
124b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
125830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
126830218c1SDavid Ahern 					   struct net_device *dev);
12770ceb4f5SYOSHIFUJI Hideaki #endif
12870ceb4f5SYOSHIFUJI Hideaki 
1298d0b94afSMartin KaFai Lau struct uncached_list {
1308d0b94afSMartin KaFai Lau 	spinlock_t		lock;
1318d0b94afSMartin KaFai Lau 	struct list_head	head;
1328d0b94afSMartin KaFai Lau };
1338d0b94afSMartin KaFai Lau 
1348d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
1358d0b94afSMartin KaFai Lau 
136510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt)
1378d0b94afSMartin KaFai Lau {
1388d0b94afSMartin KaFai Lau 	struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
1398d0b94afSMartin KaFai Lau 
1408d0b94afSMartin KaFai Lau 	rt->rt6i_uncached_list = ul;
1418d0b94afSMartin KaFai Lau 
1428d0b94afSMartin KaFai Lau 	spin_lock_bh(&ul->lock);
1438d0b94afSMartin KaFai Lau 	list_add_tail(&rt->rt6i_uncached, &ul->head);
1448d0b94afSMartin KaFai Lau 	spin_unlock_bh(&ul->lock);
1458d0b94afSMartin KaFai Lau }
1468d0b94afSMartin KaFai Lau 
147510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt)
1488d0b94afSMartin KaFai Lau {
1498d0b94afSMartin KaFai Lau 	if (!list_empty(&rt->rt6i_uncached)) {
1508d0b94afSMartin KaFai Lau 		struct uncached_list *ul = rt->rt6i_uncached_list;
15181eb8447SWei Wang 		struct net *net = dev_net(rt->dst.dev);
1528d0b94afSMartin KaFai Lau 
1538d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1548d0b94afSMartin KaFai Lau 		list_del(&rt->rt6i_uncached);
15581eb8447SWei Wang 		atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
1568d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1578d0b94afSMartin KaFai Lau 	}
1588d0b94afSMartin KaFai Lau }
1598d0b94afSMartin KaFai Lau 
1608d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
1618d0b94afSMartin KaFai Lau {
1628d0b94afSMartin KaFai Lau 	struct net_device *loopback_dev = net->loopback_dev;
1638d0b94afSMartin KaFai Lau 	int cpu;
1648d0b94afSMartin KaFai Lau 
165e332bc67SEric W. Biederman 	if (dev == loopback_dev)
166e332bc67SEric W. Biederman 		return;
167e332bc67SEric W. Biederman 
1688d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
1698d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
1708d0b94afSMartin KaFai Lau 		struct rt6_info *rt;
1718d0b94afSMartin KaFai Lau 
1728d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1738d0b94afSMartin KaFai Lau 		list_for_each_entry(rt, &ul->head, rt6i_uncached) {
1748d0b94afSMartin KaFai Lau 			struct inet6_dev *rt_idev = rt->rt6i_idev;
1758d0b94afSMartin KaFai Lau 			struct net_device *rt_dev = rt->dst.dev;
1768d0b94afSMartin KaFai Lau 
177e332bc67SEric W. Biederman 			if (rt_idev->dev == dev) {
1788d0b94afSMartin KaFai Lau 				rt->rt6i_idev = in6_dev_get(loopback_dev);
1798d0b94afSMartin KaFai Lau 				in6_dev_put(rt_idev);
1808d0b94afSMartin KaFai Lau 			}
1818d0b94afSMartin KaFai Lau 
182e332bc67SEric W. Biederman 			if (rt_dev == dev) {
1838d0b94afSMartin KaFai Lau 				rt->dst.dev = loopback_dev;
1848d0b94afSMartin KaFai Lau 				dev_hold(rt->dst.dev);
1858d0b94afSMartin KaFai Lau 				dev_put(rt_dev);
1868d0b94afSMartin KaFai Lau 			}
1878d0b94afSMartin KaFai Lau 		}
1888d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1898d0b94afSMartin KaFai Lau 	}
1908d0b94afSMartin KaFai Lau }
1918d0b94afSMartin KaFai Lau 
192f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p,
193f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
194f894cbf8SDavid S. Miller 					     const void *daddr)
19539232973SDavid S. Miller {
196a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
19739232973SDavid S. Miller 		return (const void *) p;
198f894cbf8SDavid S. Miller 	else if (skb)
199f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
20039232973SDavid S. Miller 	return daddr;
20139232973SDavid S. Miller }
20239232973SDavid S. Miller 
203f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
204f8a1b43bSDavid Ahern 				   struct net_device *dev,
205f894cbf8SDavid S. Miller 				   struct sk_buff *skb,
206f894cbf8SDavid S. Miller 				   const void *daddr)
207d3aaeb38SDavid S. Miller {
20839232973SDavid S. Miller 	struct neighbour *n;
20939232973SDavid S. Miller 
210f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(gw, skb, daddr);
211f8a1b43bSDavid Ahern 	n = __ipv6_neigh_lookup(dev, daddr);
212f83c7790SDavid S. Miller 	if (n)
213f83c7790SDavid S. Miller 		return n;
2147adf3246SStefano Brivio 
2157adf3246SStefano Brivio 	n = neigh_create(&nd_tbl, daddr, dev);
2167adf3246SStefano Brivio 	return IS_ERR(n) ? NULL : n;
217f8a1b43bSDavid Ahern }
218f8a1b43bSDavid Ahern 
219f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
220f8a1b43bSDavid Ahern 					      struct sk_buff *skb,
221f8a1b43bSDavid Ahern 					      const void *daddr)
222f8a1b43bSDavid Ahern {
223f8a1b43bSDavid Ahern 	const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
224f8a1b43bSDavid Ahern 
225f8a1b43bSDavid Ahern 	return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr);
226f83c7790SDavid S. Miller }
227f83c7790SDavid S. Miller 
22863fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
22963fca65dSJulian Anastasov {
23063fca65dSJulian Anastasov 	struct net_device *dev = dst->dev;
23163fca65dSJulian Anastasov 	struct rt6_info *rt = (struct rt6_info *)dst;
23263fca65dSJulian Anastasov 
233f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
23463fca65dSJulian Anastasov 	if (!daddr)
23563fca65dSJulian Anastasov 		return;
23663fca65dSJulian Anastasov 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
23763fca65dSJulian Anastasov 		return;
23863fca65dSJulian Anastasov 	if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
23963fca65dSJulian Anastasov 		return;
24063fca65dSJulian Anastasov 	__ipv6_confirm_neigh(dev, daddr);
24163fca65dSJulian Anastasov }
24263fca65dSJulian Anastasov 
2439a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
2441da177e4SLinus Torvalds 	.family			=	AF_INET6,
2451da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
2461da177e4SLinus Torvalds 	.gc_thresh		=	1024,
2471da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
2480dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
249ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
250d4ead6b3SDavid Ahern 	.cow_metrics		=	dst_cow_metrics_generic,
2511da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
2521da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
2531da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
2541da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
2551da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
2566e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
2579f8955ccSEric W. Biederman 	.local_out		=	__ip6_local_out,
258f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
25963fca65dSJulian Anastasov 	.confirm_neigh		=	ip6_confirm_neigh,
2601da177e4SLinus Torvalds };
2611da177e4SLinus Torvalds 
262ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
263ec831ea7SRoland Dreier {
264618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
265618f9bc7SSteffen Klassert 
266618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
267ec831ea7SRoland Dreier }
268ec831ea7SRoland Dreier 
2696700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
2706700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
27114e50e57SDavid S. Miller {
27214e50e57SDavid S. Miller }
27314e50e57SDavid S. Miller 
2746700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
2756700c270SDavid S. Miller 				      struct sk_buff *skb)
276b587ee3bSDavid S. Miller {
277b587ee3bSDavid S. Miller }
278b587ee3bSDavid S. Miller 
27914e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
28014e50e57SDavid S. Miller 	.family			=	AF_INET6,
28114e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
28214e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
283ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
284214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
28514e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
286b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2870a1f5962SMartin KaFai Lau 	.cow_metrics		=	dst_cow_metrics_generic,
288f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
28914e50e57SDavid S. Miller };
29014e50e57SDavid S. Miller 
29162fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
29214edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
29362fa8a84SDavid S. Miller };
29462fa8a84SDavid S. Miller 
2958d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = {
29693c2fb25SDavid Ahern 	.fib6_flags	= (RTF_REJECT | RTF_NONEXTHOP),
29793c2fb25SDavid Ahern 	.fib6_protocol  = RTPROT_KERNEL,
29893c2fb25SDavid Ahern 	.fib6_metric	= ~(u32)0,
299f05713e0SEric Dumazet 	.fib6_ref	= REFCOUNT_INIT(1),
300421842edSDavid Ahern 	.fib6_type	= RTN_UNREACHABLE,
301421842edSDavid Ahern 	.fib6_metrics	= (struct dst_metrics *)&dst_default_metrics,
302421842edSDavid Ahern };
303421842edSDavid Ahern 
304fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
3051da177e4SLinus Torvalds 	.dst = {
3061da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
3071da177e4SLinus Torvalds 		.__use		= 1,
3082c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
3091da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
3101da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
3111da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
3121da177e4SLinus Torvalds 	},
3131da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3141da177e4SLinus Torvalds };
3151da177e4SLinus Torvalds 
316101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
317101367c2SThomas Graf 
318fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
319101367c2SThomas Graf 	.dst = {
320101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
321101367c2SThomas Graf 		.__use		= 1,
3222c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
323101367c2SThomas Graf 		.error		= -EACCES,
3249ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
3259ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
326101367c2SThomas Graf 	},
327101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
328101367c2SThomas Graf };
329101367c2SThomas Graf 
330fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
331101367c2SThomas Graf 	.dst = {
332101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
333101367c2SThomas Graf 		.__use		= 1,
3342c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
335101367c2SThomas Graf 		.error		= -EINVAL,
336352e512cSHerbert Xu 		.input		= dst_discard,
337ede2059dSEric W. Biederman 		.output		= dst_discard_out,
338101367c2SThomas Graf 	},
339101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
340101367c2SThomas Graf };
341101367c2SThomas Graf 
342101367c2SThomas Graf #endif
343101367c2SThomas Graf 
344ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt)
345ebfa45f0SMartin KaFai Lau {
346ebfa45f0SMartin KaFai Lau 	struct dst_entry *dst = &rt->dst;
347ebfa45f0SMartin KaFai Lau 
348ebfa45f0SMartin KaFai Lau 	memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
349ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_uncached);
350ebfa45f0SMartin KaFai Lau }
351ebfa45f0SMartin KaFai Lau 
3521da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
35393531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
354ad706862SMartin KaFai Lau 			       int flags)
3551da177e4SLinus Torvalds {
35697bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
357b2a9c0edSWei Wang 					1, DST_OBSOLETE_FORCE_CHK, flags);
358cf911662SDavid S. Miller 
35981eb8447SWei Wang 	if (rt) {
360ebfa45f0SMartin KaFai Lau 		rt6_info_init(rt);
36181eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
36281eb8447SWei Wang 	}
3638104891bSSteffen Klassert 
364cf911662SDavid S. Miller 	return rt;
3651da177e4SLinus Torvalds }
3669ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc);
367d52d3997SMartin KaFai Lau 
3681da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
371a68886a6SDavid Ahern 	struct fib6_info *from;
3728d0b94afSMartin KaFai Lau 	struct inet6_dev *idev;
3731da177e4SLinus Torvalds 
3741620a336SDavid Ahern 	ip_dst_metrics_put(dst);
3758d0b94afSMartin KaFai Lau 	rt6_uncached_list_del(rt);
3768d0b94afSMartin KaFai Lau 
3778d0b94afSMartin KaFai Lau 	idev = rt->rt6i_idev;
37838308473SDavid S. Miller 	if (idev) {
3791da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3801da177e4SLinus Torvalds 		in6_dev_put(idev);
3811da177e4SLinus Torvalds 	}
3821716a961SGao feng 
3830e233874SEric Dumazet 	from = xchg((__force struct fib6_info **)&rt->from, NULL);
38493531c67SDavid Ahern 	fib6_info_release(from);
385b3419363SDavid S. Miller }
386b3419363SDavid S. Miller 
3871da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3881da177e4SLinus Torvalds 			   int how)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3911da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3925a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
393c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3941da177e4SLinus Torvalds 
395e5645f51SWei Wang 	if (idev && idev->dev != loopback_dev) {
396e5645f51SWei Wang 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
39738308473SDavid S. Miller 		if (loopback_idev) {
3981da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
3991da177e4SLinus Torvalds 			in6_dev_put(idev);
4001da177e4SLinus Torvalds 		}
4011da177e4SLinus Torvalds 	}
40297cac082SDavid S. Miller }
4031da177e4SLinus Torvalds 
4045973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt)
4055973fb1eSMartin KaFai Lau {
4065973fb1eSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_EXPIRES)
4075973fb1eSMartin KaFai Lau 		return time_after(jiffies, rt->dst.expires);
4085973fb1eSMartin KaFai Lau 	else
4095973fb1eSMartin KaFai Lau 		return false;
4105973fb1eSMartin KaFai Lau }
4115973fb1eSMartin KaFai Lau 
412a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
4131da177e4SLinus Torvalds {
414a68886a6SDavid Ahern 	struct fib6_info *from;
415a68886a6SDavid Ahern 
416a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
417a68886a6SDavid Ahern 
4181716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
4191716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
420a50feda5SEric Dumazet 			return true;
421a68886a6SDavid Ahern 	} else if (from) {
4221e2ea8adSXin Long 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
423a68886a6SDavid Ahern 			fib6_check_expired(from);
4241716a961SGao feng 	}
425a50feda5SEric Dumazet 	return false;
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
428b1d40991SDavid Ahern void fib6_select_path(const struct net *net, struct fib6_result *res,
429b1d40991SDavid Ahern 		      struct flowi6 *fl6, int oif, bool have_oif_match,
430b1d40991SDavid Ahern 		      const struct sk_buff *skb, int strict)
43151ebd318SNicolas Dichtel {
4328d1c802bSDavid Ahern 	struct fib6_info *sibling, *next_sibling;
433b1d40991SDavid Ahern 	struct fib6_info *match = res->f6i;
434b1d40991SDavid Ahern 
435b1d40991SDavid Ahern 	if (!match->fib6_nsiblings || have_oif_match)
436b1d40991SDavid Ahern 		goto out;
43751ebd318SNicolas Dichtel 
438b673d6ccSJakub Sitnicki 	/* We might have already computed the hash for ICMPv6 errors. In such
439b673d6ccSJakub Sitnicki 	 * case it will always be non-zero. Otherwise now is the time to do it.
440b673d6ccSJakub Sitnicki 	 */
441b673d6ccSJakub Sitnicki 	if (!fl6->mp_hash)
442b4bac172SDavid Ahern 		fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
443b673d6ccSJakub Sitnicki 
444ad1601aeSDavid Ahern 	if (fl6->mp_hash <= atomic_read(&match->fib6_nh.fib_nh_upper_bound))
445b1d40991SDavid Ahern 		goto out;
446bbfcd776SIdo Schimmel 
44793c2fb25SDavid Ahern 	list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
44893c2fb25SDavid Ahern 				 fib6_siblings) {
449702cea56SDavid Ahern 		const struct fib6_nh *nh = &sibling->fib6_nh;
4505e670d84SDavid Ahern 		int nh_upper_bound;
4515e670d84SDavid Ahern 
452702cea56SDavid Ahern 		nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
4535e670d84SDavid Ahern 		if (fl6->mp_hash > nh_upper_bound)
4543d709f69SIdo Schimmel 			continue;
455702cea56SDavid Ahern 		if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
45652bd4c0cSNicolas Dichtel 			break;
45751ebd318SNicolas Dichtel 		match = sibling;
45851ebd318SNicolas Dichtel 		break;
45951ebd318SNicolas Dichtel 	}
4603d709f69SIdo Schimmel 
461b1d40991SDavid Ahern out:
462b1d40991SDavid Ahern 	res->f6i = match;
463b1d40991SDavid Ahern 	res->nh = &match->fib6_nh;
46451ebd318SNicolas Dichtel }
46551ebd318SNicolas Dichtel 
4661da177e4SLinus Torvalds /*
46766f5d6ceSWei Wang  *	Route lookup. rcu_read_lock() should be held.
4681da177e4SLinus Torvalds  */
4691da177e4SLinus Torvalds 
4700c59d006SDavid Ahern static bool __rt6_device_match(struct net *net, const struct fib6_nh *nh,
4710c59d006SDavid Ahern 			       const struct in6_addr *saddr, int oif, int flags)
4720c59d006SDavid Ahern {
4730c59d006SDavid Ahern 	const struct net_device *dev;
4740c59d006SDavid Ahern 
4750c59d006SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD)
4760c59d006SDavid Ahern 		return false;
4770c59d006SDavid Ahern 
4780c59d006SDavid Ahern 	dev = nh->fib_nh_dev;
4790c59d006SDavid Ahern 	if (oif) {
4800c59d006SDavid Ahern 		if (dev->ifindex == oif)
4810c59d006SDavid Ahern 			return true;
4820c59d006SDavid Ahern 	} else {
4830c59d006SDavid Ahern 		if (ipv6_chk_addr(net, saddr, dev,
4840c59d006SDavid Ahern 				  flags & RT6_LOOKUP_F_IFACE))
4850c59d006SDavid Ahern 			return true;
4860c59d006SDavid Ahern 	}
4870c59d006SDavid Ahern 
4880c59d006SDavid Ahern 	return false;
4890c59d006SDavid Ahern }
4900c59d006SDavid Ahern 
49175ef7389SDavid Ahern static void rt6_device_match(struct net *net, struct fib6_result *res,
49275ef7389SDavid Ahern 			     const struct in6_addr *saddr, int oif, int flags)
4931da177e4SLinus Torvalds {
49475ef7389SDavid Ahern 	struct fib6_info *f6i = res->f6i;
49575ef7389SDavid Ahern 	struct fib6_info *spf6i;
49675ef7389SDavid Ahern 	struct fib6_nh *nh;
4971da177e4SLinus Torvalds 
49875ef7389SDavid Ahern 	if (!oif && ipv6_addr_any(saddr)) {
49975ef7389SDavid Ahern 		nh = &f6i->fib6_nh;
5007d21fec9SDavid Ahern 		if (!(nh->fib_nh_flags & RTNH_F_DEAD))
5017d21fec9SDavid Ahern 			goto out;
5021da177e4SLinus Torvalds 	}
5031da177e4SLinus Torvalds 
50475ef7389SDavid Ahern 	for (spf6i = f6i; spf6i; spf6i = rcu_dereference(spf6i->fib6_next)) {
50575ef7389SDavid Ahern 		nh = &spf6i->fib6_nh;
50675ef7389SDavid Ahern 		if (__rt6_device_match(net, nh, saddr, oif, flags)) {
50775ef7389SDavid Ahern 			res->f6i = spf6i;
5087d21fec9SDavid Ahern 			goto out;
50975ef7389SDavid Ahern 		}
51075ef7389SDavid Ahern 	}
5111da177e4SLinus Torvalds 
51275ef7389SDavid Ahern 	if (oif && flags & RT6_LOOKUP_F_IFACE) {
51375ef7389SDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
5147d21fec9SDavid Ahern 		nh = &res->f6i->fib6_nh;
5157d21fec9SDavid Ahern 		goto out;
51675ef7389SDavid Ahern 	}
51775ef7389SDavid Ahern 
5187d21fec9SDavid Ahern 	nh = &f6i->fib6_nh;
5197d21fec9SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD) {
52075ef7389SDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
5217d21fec9SDavid Ahern 		nh = &res->f6i->fib6_nh;
52275ef7389SDavid Ahern 	}
5237d21fec9SDavid Ahern out:
5247d21fec9SDavid Ahern 	res->nh = nh;
5257d21fec9SDavid Ahern 	res->fib6_type = res->f6i->fib6_type;
5267d21fec9SDavid Ahern 	res->fib6_flags = res->f6i->fib6_flags;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
52927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
530c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
531c2f17e82SHannes Frederic Sowa 	struct work_struct work;
532c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
533c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
534c2f17e82SHannes Frederic Sowa };
535c2f17e82SHannes Frederic Sowa 
536c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
537c2f17e82SHannes Frederic Sowa {
538c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
539c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
540c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
541c2f17e82SHannes Frederic Sowa 
542c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
543adc176c5SErik Nordmark 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
544c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
545662f5533SMichael Büsch 	kfree(work);
546c2f17e82SHannes Frederic Sowa }
547c2f17e82SHannes Frederic Sowa 
548cc3a86c8SDavid Ahern static void rt6_probe(struct fib6_nh *fib6_nh)
54927097255SYOSHIFUJI Hideaki {
550f547fac6SSabrina Dubroca 	struct __rt6_probe_work *work = NULL;
5515e670d84SDavid Ahern 	const struct in6_addr *nh_gw;
552f2c31e32SEric Dumazet 	struct neighbour *neigh;
5535e670d84SDavid Ahern 	struct net_device *dev;
554f547fac6SSabrina Dubroca 	struct inet6_dev *idev;
5555e670d84SDavid Ahern 
55627097255SYOSHIFUJI Hideaki 	/*
55727097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
55827097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
55927097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
56027097255SYOSHIFUJI Hideaki 	 *
56127097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
56227097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
56327097255SYOSHIFUJI Hideaki 	 */
564cc3a86c8SDavid Ahern 	if (fib6_nh->fib_nh_gw_family)
565fdd6681dSAmerigo Wang 		return;
5665e670d84SDavid Ahern 
567cc3a86c8SDavid Ahern 	nh_gw = &fib6_nh->fib_nh_gw6;
568cc3a86c8SDavid Ahern 	dev = fib6_nh->fib_nh_dev;
5692152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
570f547fac6SSabrina Dubroca 	idev = __in6_dev_get(dev);
5715e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
5722152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
5738d6c31bfSMartin KaFai Lau 		if (neigh->nud_state & NUD_VALID)
5748d6c31bfSMartin KaFai Lau 			goto out;
5758d6c31bfSMartin KaFai Lau 
5762152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
577990edb42SMartin KaFai Lau 		if (!(neigh->nud_state & NUD_VALID) &&
578990edb42SMartin KaFai Lau 		    time_after(jiffies,
579dcd1f572SDavid Ahern 			       neigh->updated + idev->cnf.rtr_probe_interval)) {
580c2f17e82SHannes Frederic Sowa 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
581990edb42SMartin KaFai Lau 			if (work)
5827e980569SJiri Benc 				__neigh_set_probe_once(neigh);
583990edb42SMartin KaFai Lau 		}
584c2f17e82SHannes Frederic Sowa 		write_unlock(&neigh->lock);
585cc3a86c8SDavid Ahern 	} else if (time_after(jiffies, fib6_nh->last_probe +
586f547fac6SSabrina Dubroca 				       idev->cnf.rtr_probe_interval)) {
587990edb42SMartin KaFai Lau 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
588990edb42SMartin KaFai Lau 	}
589c2f17e82SHannes Frederic Sowa 
590c2f17e82SHannes Frederic Sowa 	if (work) {
591cc3a86c8SDavid Ahern 		fib6_nh->last_probe = jiffies;
592c2f17e82SHannes Frederic Sowa 		INIT_WORK(&work->work, rt6_probe_deferred);
5935e670d84SDavid Ahern 		work->target = *nh_gw;
5945e670d84SDavid Ahern 		dev_hold(dev);
5955e670d84SDavid Ahern 		work->dev = dev;
596c2f17e82SHannes Frederic Sowa 		schedule_work(&work->work);
597c2f17e82SHannes Frederic Sowa 	}
598990edb42SMartin KaFai Lau 
5998d6c31bfSMartin KaFai Lau out:
6002152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
601f2c31e32SEric Dumazet }
60227097255SYOSHIFUJI Hideaki #else
603cc3a86c8SDavid Ahern static inline void rt6_probe(struct fib6_nh *fib6_nh)
60427097255SYOSHIFUJI Hideaki {
60527097255SYOSHIFUJI Hideaki }
60627097255SYOSHIFUJI Hideaki #endif
60727097255SYOSHIFUJI Hideaki 
6081da177e4SLinus Torvalds /*
609554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
6101da177e4SLinus Torvalds  */
6111ba9a895SDavid Ahern static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
6121da177e4SLinus Torvalds {
613afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
6145e670d84SDavid Ahern 	struct neighbour *neigh;
615f2c31e32SEric Dumazet 
616145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
6171ba9a895SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
6181ba9a895SDavid Ahern 					  &fib6_nh->fib_nh_gw6);
619145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
620145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
621554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
622afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
623398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
624a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
625afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
6267e980569SJiri Benc 		else
6277e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
628398bcbebSYOSHIFUJI Hideaki #endif
629145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
630afc154e9SHannes Frederic Sowa 	} else {
631afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
6327e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
633a5a81f0bSPaul Marks 	}
634145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
635145a3621SYOSHIFUJI Hideaki / 吉藤英明 
636a5a81f0bSPaul Marks 	return ret;
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
639702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
640702cea56SDavid Ahern 			   int strict)
641554cfb7eSYOSHIFUJI Hideaki {
6426e1809a5SDavid Ahern 	int m = 0;
6434d0c5911SYOSHIFUJI Hideaki 
6446e1809a5SDavid Ahern 	if (!oif || nh->fib_nh_dev->ifindex == oif)
6456e1809a5SDavid Ahern 		m = 2;
6466e1809a5SDavid Ahern 
64777d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
648afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
649ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
650702cea56SDavid Ahern 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
651ebacaaa0SYOSHIFUJI Hideaki #endif
6521ba9a895SDavid Ahern 	if ((strict & RT6_LOOKUP_F_REACHABLE) &&
653702cea56SDavid Ahern 	    !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
6541ba9a895SDavid Ahern 		int n = rt6_check_neigh(nh);
655afc154e9SHannes Frederic Sowa 		if (n < 0)
656afc154e9SHannes Frederic Sowa 			return n;
657afc154e9SHannes Frederic Sowa 	}
658554cfb7eSYOSHIFUJI Hideaki 	return m;
659554cfb7eSYOSHIFUJI Hideaki }
660554cfb7eSYOSHIFUJI Hideaki 
66128679ed1SDavid Ahern static bool find_match(struct fib6_nh *nh, u32 fib6_flags,
66228679ed1SDavid Ahern 		       int oif, int strict, int *mpri, bool *do_rr)
663554cfb7eSYOSHIFUJI Hideaki {
664afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
66528679ed1SDavid Ahern 	bool rc = false;
66628679ed1SDavid Ahern 	int m;
66735103d11SAndy Gospodarek 
66828679ed1SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD)
6698067bb8cSIdo Schimmel 		goto out;
6708067bb8cSIdo Schimmel 
67128679ed1SDavid Ahern 	if (ip6_ignore_linkdown(nh->fib_nh_dev) &&
67228679ed1SDavid Ahern 	    nh->fib_nh_flags & RTNH_F_LINKDOWN &&
673d5d32e4bSDavid Ahern 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
67435103d11SAndy Gospodarek 		goto out;
675554cfb7eSYOSHIFUJI Hideaki 
67628679ed1SDavid Ahern 	m = rt6_score_route(nh, fib6_flags, oif, strict);
6777e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
678afc154e9SHannes Frederic Sowa 		match_do_rr = true;
679afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
6807e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
681f11e6659SDavid S. Miller 		goto out;
6821da177e4SLinus Torvalds 	}
683f11e6659SDavid S. Miller 
684afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
68528679ed1SDavid Ahern 		rt6_probe(nh);
686afc154e9SHannes Frederic Sowa 
6877e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
688afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
689afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
690afc154e9SHannes Frederic Sowa 		*mpri = m;
69128679ed1SDavid Ahern 		rc = true;
692afc154e9SHannes Frederic Sowa 	}
693f11e6659SDavid S. Miller out:
69428679ed1SDavid Ahern 	return rc;
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
697b7bc4b6aSDavid Ahern static void __find_rr_leaf(struct fib6_info *f6i_start,
69830c15f03SDavid Ahern 			   struct fib6_info *nomatch, u32 metric,
699b7bc4b6aSDavid Ahern 			   struct fib6_result *res, struct fib6_info **cont,
70030c15f03SDavid Ahern 			   int oif, int strict, bool *do_rr, int *mpri)
70130c15f03SDavid Ahern {
702b7bc4b6aSDavid Ahern 	struct fib6_info *f6i;
70330c15f03SDavid Ahern 
704b7bc4b6aSDavid Ahern 	for (f6i = f6i_start;
705b7bc4b6aSDavid Ahern 	     f6i && f6i != nomatch;
706b7bc4b6aSDavid Ahern 	     f6i = rcu_dereference(f6i->fib6_next)) {
70730c15f03SDavid Ahern 		struct fib6_nh *nh;
70830c15f03SDavid Ahern 
709b7bc4b6aSDavid Ahern 		if (cont && f6i->fib6_metric != metric) {
710b7bc4b6aSDavid Ahern 			*cont = f6i;
71130c15f03SDavid Ahern 			return;
71230c15f03SDavid Ahern 		}
71330c15f03SDavid Ahern 
714b7bc4b6aSDavid Ahern 		if (fib6_check_expired(f6i))
71530c15f03SDavid Ahern 			continue;
71630c15f03SDavid Ahern 
717b7bc4b6aSDavid Ahern 		nh = &f6i->fib6_nh;
718b7bc4b6aSDavid Ahern 		if (find_match(nh, f6i->fib6_flags, oif, strict, mpri, do_rr)) {
719b7bc4b6aSDavid Ahern 			res->f6i = f6i;
720b7bc4b6aSDavid Ahern 			res->nh = nh;
7217d21fec9SDavid Ahern 			res->fib6_flags = f6i->fib6_flags;
7227d21fec9SDavid Ahern 			res->fib6_type = f6i->fib6_type;
723b7bc4b6aSDavid Ahern 		}
72430c15f03SDavid Ahern 	}
72530c15f03SDavid Ahern }
72630c15f03SDavid Ahern 
727b7bc4b6aSDavid Ahern static void find_rr_leaf(struct fib6_node *fn, struct fib6_info *leaf,
728b7bc4b6aSDavid Ahern 			 struct fib6_info *rr_head, int oif, int strict,
729b7bc4b6aSDavid Ahern 			 bool *do_rr, struct fib6_result *res)
730f11e6659SDavid S. Miller {
731b7bc4b6aSDavid Ahern 	u32 metric = rr_head->fib6_metric;
732b7bc4b6aSDavid Ahern 	struct fib6_info *cont = NULL;
733f11e6659SDavid S. Miller 	int mpri = -1;
734f11e6659SDavid S. Miller 
735b7bc4b6aSDavid Ahern 	__find_rr_leaf(rr_head, NULL, metric, res, &cont,
73630c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
7379fbdcfafSSteffen Klassert 
738b7bc4b6aSDavid Ahern 	__find_rr_leaf(leaf, rr_head, metric, res, &cont,
73930c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
7409fbdcfafSSteffen Klassert 
741b7bc4b6aSDavid Ahern 	if (res->f6i || !cont)
742b7bc4b6aSDavid Ahern 		return;
7439fbdcfafSSteffen Klassert 
744b7bc4b6aSDavid Ahern 	__find_rr_leaf(cont, NULL, metric, res, NULL,
74530c15f03SDavid Ahern 		       oif, strict, do_rr, &mpri);
746f11e6659SDavid S. Miller }
747f11e6659SDavid S. Miller 
748b7bc4b6aSDavid Ahern static void rt6_select(struct net *net, struct fib6_node *fn, int oif,
749b7bc4b6aSDavid Ahern 		       struct fib6_result *res, int strict)
750f11e6659SDavid S. Miller {
7518d1c802bSDavid Ahern 	struct fib6_info *leaf = rcu_dereference(fn->leaf);
752b7bc4b6aSDavid Ahern 	struct fib6_info *rt0;
753afc154e9SHannes Frederic Sowa 	bool do_rr = false;
75417ecf590SWei Wang 	int key_plen;
755f11e6659SDavid S. Miller 
756b7bc4b6aSDavid Ahern 	/* make sure this function or its helpers sets f6i */
757b7bc4b6aSDavid Ahern 	res->f6i = NULL;
758b7bc4b6aSDavid Ahern 
759421842edSDavid Ahern 	if (!leaf || leaf == net->ipv6.fib6_null_entry)
760b7bc4b6aSDavid Ahern 		goto out;
7618d1040e8SWei Wang 
76266f5d6ceSWei Wang 	rt0 = rcu_dereference(fn->rr_ptr);
763f11e6659SDavid S. Miller 	if (!rt0)
76466f5d6ceSWei Wang 		rt0 = leaf;
765f11e6659SDavid S. Miller 
76617ecf590SWei Wang 	/* Double check to make sure fn is not an intermediate node
76717ecf590SWei Wang 	 * and fn->leaf does not points to its child's leaf
76817ecf590SWei Wang 	 * (This might happen if all routes under fn are deleted from
76917ecf590SWei Wang 	 * the tree and fib6_repair_tree() is called on the node.)
77017ecf590SWei Wang 	 */
77193c2fb25SDavid Ahern 	key_plen = rt0->fib6_dst.plen;
77217ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES
77393c2fb25SDavid Ahern 	if (rt0->fib6_src.plen)
77493c2fb25SDavid Ahern 		key_plen = rt0->fib6_src.plen;
77517ecf590SWei Wang #endif
77617ecf590SWei Wang 	if (fn->fn_bit != key_plen)
777b7bc4b6aSDavid Ahern 		goto out;
77817ecf590SWei Wang 
779b7bc4b6aSDavid Ahern 	find_rr_leaf(fn, leaf, rt0, oif, strict, &do_rr, res);
780afc154e9SHannes Frederic Sowa 	if (do_rr) {
7818fb11a9aSDavid Ahern 		struct fib6_info *next = rcu_dereference(rt0->fib6_next);
782f11e6659SDavid S. Miller 
783554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
78493c2fb25SDavid Ahern 		if (!next || next->fib6_metric != rt0->fib6_metric)
7858d1040e8SWei Wang 			next = leaf;
786f11e6659SDavid S. Miller 
78766f5d6ceSWei Wang 		if (next != rt0) {
78893c2fb25SDavid Ahern 			spin_lock_bh(&leaf->fib6_table->tb6_lock);
78966f5d6ceSWei Wang 			/* make sure next is not being deleted from the tree */
79093c2fb25SDavid Ahern 			if (next->fib6_node)
79166f5d6ceSWei Wang 				rcu_assign_pointer(fn->rr_ptr, next);
79293c2fb25SDavid Ahern 			spin_unlock_bh(&leaf->fib6_table->tb6_lock);
79366f5d6ceSWei Wang 		}
794554cfb7eSYOSHIFUJI Hideaki 	}
795554cfb7eSYOSHIFUJI Hideaki 
796b7bc4b6aSDavid Ahern out:
797b7bc4b6aSDavid Ahern 	if (!res->f6i) {
798b7bc4b6aSDavid Ahern 		res->f6i = net->ipv6.fib6_null_entry;
799b7bc4b6aSDavid Ahern 		res->nh = &res->f6i->fib6_nh;
8007d21fec9SDavid Ahern 		res->fib6_flags = res->f6i->fib6_flags;
8017d21fec9SDavid Ahern 		res->fib6_type = res->f6i->fib6_type;
802b7bc4b6aSDavid Ahern 	}
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
80585bd05deSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_result *res)
8068b9df265SMartin KaFai Lau {
80785bd05deSDavid Ahern 	return (res->f6i->fib6_flags & RTF_NONEXTHOP) ||
80885bd05deSDavid Ahern 	       res->nh->fib_nh_gw_family;
8098b9df265SMartin KaFai Lau }
8108b9df265SMartin KaFai Lau 
81170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
81270ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
813b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
81470ceb4f5SYOSHIFUJI Hideaki {
815c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
81670ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
81770ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
81870ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
8194bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
8208d1c802bSDavid Ahern 	struct fib6_info *rt;
82170ceb4f5SYOSHIFUJI Hideaki 
82270ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
82370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
82470ceb4f5SYOSHIFUJI Hideaki 	}
82570ceb4f5SYOSHIFUJI Hideaki 
82670ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
82770ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
82870ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
82970ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
83070ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
83170ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
83270ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
83370ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
83470ceb4f5SYOSHIFUJI Hideaki 		}
83570ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
83670ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
83770ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
83870ceb4f5SYOSHIFUJI Hideaki 		}
83970ceb4f5SYOSHIFUJI Hideaki 	}
84070ceb4f5SYOSHIFUJI Hideaki 
84170ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
84270ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
8433933fc95SJens Rosenboom 		return -EINVAL;
84470ceb4f5SYOSHIFUJI Hideaki 
8454bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
84670ceb4f5SYOSHIFUJI Hideaki 
84770ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
84870ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
84970ceb4f5SYOSHIFUJI Hideaki 	else {
85070ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
85170ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
85270ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
85370ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
85470ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
85570ceb4f5SYOSHIFUJI Hideaki 	}
85670ceb4f5SYOSHIFUJI Hideaki 
857f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
858afb1d4b5SDavid Ahern 		rt = rt6_get_dflt_router(net, gwaddr, dev);
859f104a567SDuan Jiong 	else
860f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
861830218c1SDavid Ahern 					gwaddr, dev);
86270ceb4f5SYOSHIFUJI Hideaki 
86370ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
864afb1d4b5SDavid Ahern 		ip6_del_rt(net, rt);
86570ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
86670ceb4f5SYOSHIFUJI Hideaki 	}
86770ceb4f5SYOSHIFUJI Hideaki 
86870ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
869830218c1SDavid Ahern 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
870830218c1SDavid Ahern 					dev, pref);
87170ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
87293c2fb25SDavid Ahern 		rt->fib6_flags = RTF_ROUTEINFO |
87393c2fb25SDavid Ahern 				 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
87470ceb4f5SYOSHIFUJI Hideaki 
87570ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
8761716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
87714895687SDavid Ahern 			fib6_clean_expires(rt);
8781716a961SGao feng 		else
87914895687SDavid Ahern 			fib6_set_expires(rt, jiffies + HZ * lifetime);
8801716a961SGao feng 
88193531c67SDavid Ahern 		fib6_info_release(rt);
88270ceb4f5SYOSHIFUJI Hideaki 	}
88370ceb4f5SYOSHIFUJI Hideaki 	return 0;
88470ceb4f5SYOSHIFUJI Hideaki }
88570ceb4f5SYOSHIFUJI Hideaki #endif
88670ceb4f5SYOSHIFUJI Hideaki 
887ae90d867SDavid Ahern /*
888ae90d867SDavid Ahern  *	Misc support functions
889ae90d867SDavid Ahern  */
890ae90d867SDavid Ahern 
891ae90d867SDavid Ahern /* called with rcu_lock held */
8920d161581SDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(const struct fib6_result *res)
893ae90d867SDavid Ahern {
8940d161581SDavid Ahern 	struct net_device *dev = res->nh->fib_nh_dev;
895ae90d867SDavid Ahern 
8967d21fec9SDavid Ahern 	if (res->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
897ae90d867SDavid Ahern 		/* for copies of local routes, dst->dev needs to be the
898ae90d867SDavid Ahern 		 * device if it is a master device, the master device if
899ae90d867SDavid Ahern 		 * device is enslaved, and the loopback as the default
900ae90d867SDavid Ahern 		 */
901ae90d867SDavid Ahern 		if (netif_is_l3_slave(dev) &&
9027d21fec9SDavid Ahern 		    !rt6_need_strict(&res->f6i->fib6_dst.addr))
903ae90d867SDavid Ahern 			dev = l3mdev_master_dev_rcu(dev);
904ae90d867SDavid Ahern 		else if (!netif_is_l3_master(dev))
905ae90d867SDavid Ahern 			dev = dev_net(dev)->loopback_dev;
906ae90d867SDavid Ahern 		/* last case is netif_is_l3_master(dev) is true in which
907ae90d867SDavid Ahern 		 * case we want dev returned to be dev
908ae90d867SDavid Ahern 		 */
909ae90d867SDavid Ahern 	}
910ae90d867SDavid Ahern 
911ae90d867SDavid Ahern 	return dev;
912ae90d867SDavid Ahern }
913ae90d867SDavid Ahern 
9146edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = {
9156edb3c96SDavid Ahern 	[RTN_UNSPEC]	= 0,
9166edb3c96SDavid Ahern 	[RTN_UNICAST]	= 0,
9176edb3c96SDavid Ahern 	[RTN_LOCAL]	= 0,
9186edb3c96SDavid Ahern 	[RTN_BROADCAST]	= 0,
9196edb3c96SDavid Ahern 	[RTN_ANYCAST]	= 0,
9206edb3c96SDavid Ahern 	[RTN_MULTICAST]	= 0,
9216edb3c96SDavid Ahern 	[RTN_BLACKHOLE]	= -EINVAL,
9226edb3c96SDavid Ahern 	[RTN_UNREACHABLE] = -EHOSTUNREACH,
9236edb3c96SDavid Ahern 	[RTN_PROHIBIT]	= -EACCES,
9246edb3c96SDavid Ahern 	[RTN_THROW]	= -EAGAIN,
9256edb3c96SDavid Ahern 	[RTN_NAT]	= -EINVAL,
9266edb3c96SDavid Ahern 	[RTN_XRESOLVE]	= -EINVAL,
9276edb3c96SDavid Ahern };
9286edb3c96SDavid Ahern 
9296edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type)
9306edb3c96SDavid Ahern {
9316edb3c96SDavid Ahern 	return fib6_prop[fib6_type];
9326edb3c96SDavid Ahern }
9336edb3c96SDavid Ahern 
9348d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
9353b6761d1SDavid Ahern {
9363b6761d1SDavid Ahern 	unsigned short flags = 0;
9373b6761d1SDavid Ahern 
9383b6761d1SDavid Ahern 	if (rt->dst_nocount)
9393b6761d1SDavid Ahern 		flags |= DST_NOCOUNT;
9403b6761d1SDavid Ahern 	if (rt->dst_nopolicy)
9413b6761d1SDavid Ahern 		flags |= DST_NOPOLICY;
9423b6761d1SDavid Ahern 	if (rt->dst_host)
9433b6761d1SDavid Ahern 		flags |= DST_HOST;
9443b6761d1SDavid Ahern 
9453b6761d1SDavid Ahern 	return flags;
9463b6761d1SDavid Ahern }
9473b6761d1SDavid Ahern 
9487d21fec9SDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, u8 fib6_type)
9496edb3c96SDavid Ahern {
9507d21fec9SDavid Ahern 	rt->dst.error = ip6_rt_type_to_error(fib6_type);
9516edb3c96SDavid Ahern 
9527d21fec9SDavid Ahern 	switch (fib6_type) {
9536edb3c96SDavid Ahern 	case RTN_BLACKHOLE:
9546edb3c96SDavid Ahern 		rt->dst.output = dst_discard_out;
9556edb3c96SDavid Ahern 		rt->dst.input = dst_discard;
9566edb3c96SDavid Ahern 		break;
9576edb3c96SDavid Ahern 	case RTN_PROHIBIT:
9586edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_prohibit_out;
9596edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_prohibit;
9606edb3c96SDavid Ahern 		break;
9616edb3c96SDavid Ahern 	case RTN_THROW:
9626edb3c96SDavid Ahern 	case RTN_UNREACHABLE:
9636edb3c96SDavid Ahern 	default:
9646edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_discard_out;
9656edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_discard;
9666edb3c96SDavid Ahern 		break;
9676edb3c96SDavid Ahern 	}
9686edb3c96SDavid Ahern }
9696edb3c96SDavid Ahern 
9700d161581SDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, const struct fib6_result *res)
9716edb3c96SDavid Ahern {
9727d21fec9SDavid Ahern 	struct fib6_info *f6i = res->f6i;
9730d161581SDavid Ahern 
9747d21fec9SDavid Ahern 	if (res->fib6_flags & RTF_REJECT) {
9757d21fec9SDavid Ahern 		ip6_rt_init_dst_reject(rt, res->fib6_type);
9766edb3c96SDavid Ahern 		return;
9776edb3c96SDavid Ahern 	}
9786edb3c96SDavid Ahern 
9796edb3c96SDavid Ahern 	rt->dst.error = 0;
9806edb3c96SDavid Ahern 	rt->dst.output = ip6_output;
9816edb3c96SDavid Ahern 
9827d21fec9SDavid Ahern 	if (res->fib6_type == RTN_LOCAL || res->fib6_type == RTN_ANYCAST) {
9836edb3c96SDavid Ahern 		rt->dst.input = ip6_input;
9847d21fec9SDavid Ahern 	} else if (ipv6_addr_type(&f6i->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
9856edb3c96SDavid Ahern 		rt->dst.input = ip6_mc_input;
9866edb3c96SDavid Ahern 	} else {
9876edb3c96SDavid Ahern 		rt->dst.input = ip6_forward;
9886edb3c96SDavid Ahern 	}
9896edb3c96SDavid Ahern 
9900d161581SDavid Ahern 	if (res->nh->fib_nh_lws) {
9910d161581SDavid Ahern 		rt->dst.lwtstate = lwtstate_get(res->nh->fib_nh_lws);
9926edb3c96SDavid Ahern 		lwtunnel_set_redirect(&rt->dst);
9936edb3c96SDavid Ahern 	}
9946edb3c96SDavid Ahern 
9956edb3c96SDavid Ahern 	rt->dst.lastuse = jiffies;
9966edb3c96SDavid Ahern }
9976edb3c96SDavid Ahern 
998e873e4b9SWei Wang /* Caller must already hold reference to @from */
9998d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
1000ae90d867SDavid Ahern {
1001ae90d867SDavid Ahern 	rt->rt6i_flags &= ~RTF_EXPIRES;
1002a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, from);
1003e1255ed4SDavid Ahern 	ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
1004ae90d867SDavid Ahern }
1005ae90d867SDavid Ahern 
10060d161581SDavid Ahern /* Caller must already hold reference to f6i in result */
10070d161581SDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, const struct fib6_result *res)
1008ae90d867SDavid Ahern {
10090d161581SDavid Ahern 	const struct fib6_nh *nh = res->nh;
10100d161581SDavid Ahern 	const struct net_device *dev = nh->fib_nh_dev;
10110d161581SDavid Ahern 	struct fib6_info *f6i = res->f6i;
1012dcd1f572SDavid Ahern 
10130d161581SDavid Ahern 	ip6_rt_init_dst(rt, res);
10146edb3c96SDavid Ahern 
10150d161581SDavid Ahern 	rt->rt6i_dst = f6i->fib6_dst;
1016dcd1f572SDavid Ahern 	rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
10177d21fec9SDavid Ahern 	rt->rt6i_flags = res->fib6_flags;
10180d161581SDavid Ahern 	if (nh->fib_nh_gw_family) {
10190d161581SDavid Ahern 		rt->rt6i_gateway = nh->fib_nh_gw6;
10202b2450caSDavid Ahern 		rt->rt6i_flags |= RTF_GATEWAY;
10212b2450caSDavid Ahern 	}
10220d161581SDavid Ahern 	rt6_set_from(rt, f6i);
1023ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
10240d161581SDavid Ahern 	rt->rt6i_src = f6i->fib6_src;
1025ae90d867SDavid Ahern #endif
1026ae90d867SDavid Ahern }
1027ae90d867SDavid Ahern 
1028a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1029a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
1030a3c00e46SMartin KaFai Lau {
103166f5d6ceSWei Wang 	struct fib6_node *pn, *sn;
1032a3c00e46SMartin KaFai Lau 	while (1) {
1033a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
1034a3c00e46SMartin KaFai Lau 			return NULL;
103566f5d6ceSWei Wang 		pn = rcu_dereference(fn->parent);
103666f5d6ceSWei Wang 		sn = FIB6_SUBTREE(pn);
103766f5d6ceSWei Wang 		if (sn && sn != fn)
10386454743bSDavid Ahern 			fn = fib6_node_lookup(sn, NULL, saddr);
1039a3c00e46SMartin KaFai Lau 		else
1040a3c00e46SMartin KaFai Lau 			fn = pn;
1041a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
1042a3c00e46SMartin KaFai Lau 			return fn;
1043a3c00e46SMartin KaFai Lau 	}
1044a3c00e46SMartin KaFai Lau }
1045c71099acSThomas Graf 
104610585b43SDavid Ahern static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1047d3843fe5SWei Wang {
1048d3843fe5SWei Wang 	struct rt6_info *rt = *prt;
1049d3843fe5SWei Wang 
1050d3843fe5SWei Wang 	if (dst_hold_safe(&rt->dst))
1051d3843fe5SWei Wang 		return true;
105210585b43SDavid Ahern 	if (net) {
1053d3843fe5SWei Wang 		rt = net->ipv6.ip6_null_entry;
1054d3843fe5SWei Wang 		dst_hold(&rt->dst);
1055d3843fe5SWei Wang 	} else {
1056d3843fe5SWei Wang 		rt = NULL;
1057d3843fe5SWei Wang 	}
1058d3843fe5SWei Wang 	*prt = rt;
1059d3843fe5SWei Wang 	return false;
1060d3843fe5SWei Wang }
1061d3843fe5SWei Wang 
1062dec9b0e2SDavid Ahern /* called with rcu_lock held */
10639b6b35abSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
1064dec9b0e2SDavid Ahern {
10659b6b35abSDavid Ahern 	struct net_device *dev = res->nh->fib_nh_dev;
10669b6b35abSDavid Ahern 	struct fib6_info *f6i = res->f6i;
10679b6b35abSDavid Ahern 	unsigned short flags;
1068dec9b0e2SDavid Ahern 	struct rt6_info *nrt;
1069dec9b0e2SDavid Ahern 
10709b6b35abSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
10711c87e79aSXin Long 		goto fallback;
1072e873e4b9SWei Wang 
10739b6b35abSDavid Ahern 	flags = fib6_info_dst_flags(f6i);
107493531c67SDavid Ahern 	nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
10751c87e79aSXin Long 	if (!nrt) {
10769b6b35abSDavid Ahern 		fib6_info_release(f6i);
10771c87e79aSXin Long 		goto fallback;
10781c87e79aSXin Long 	}
1079dec9b0e2SDavid Ahern 
10800d161581SDavid Ahern 	ip6_rt_copy_init(nrt, res);
10811c87e79aSXin Long 	return nrt;
10821c87e79aSXin Long 
10831c87e79aSXin Long fallback:
10841c87e79aSXin Long 	nrt = dev_net(dev)->ipv6.ip6_null_entry;
10851c87e79aSXin Long 	dst_hold(&nrt->dst);
1086dec9b0e2SDavid Ahern 	return nrt;
1087dec9b0e2SDavid Ahern }
1088dec9b0e2SDavid Ahern 
10898ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
10908ed67789SDaniel Lezcano 					     struct fib6_table *table,
1091b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
1092b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
1093b75cc8f9SDavid Ahern 					     int flags)
10941da177e4SLinus Torvalds {
1095b1d40991SDavid Ahern 	struct fib6_result res = {};
10961da177e4SLinus Torvalds 	struct fib6_node *fn;
109723fb93a4SDavid Ahern 	struct rt6_info *rt;
10981da177e4SLinus Torvalds 
1099b6cdbc85SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1100b6cdbc85SDavid Ahern 		flags &= ~RT6_LOOKUP_F_IFACE;
1101b6cdbc85SDavid Ahern 
110266f5d6ceSWei Wang 	rcu_read_lock();
11036454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1104c71099acSThomas Graf restart:
1105b1d40991SDavid Ahern 	res.f6i = rcu_dereference(fn->leaf);
1106b1d40991SDavid Ahern 	if (!res.f6i)
1107b1d40991SDavid Ahern 		res.f6i = net->ipv6.fib6_null_entry;
1108af52a52cSDavid Ahern 	else
110975ef7389SDavid Ahern 		rt6_device_match(net, &res, &fl6->saddr, fl6->flowi6_oif,
111075ef7389SDavid Ahern 				 flags);
1111af52a52cSDavid Ahern 
1112b1d40991SDavid Ahern 	if (res.f6i == net->ipv6.fib6_null_entry) {
1113a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1114a3c00e46SMartin KaFai Lau 		if (fn)
1115a3c00e46SMartin KaFai Lau 			goto restart;
1116af52a52cSDavid Ahern 
1117af52a52cSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
1118af52a52cSDavid Ahern 		dst_hold(&rt->dst);
1119af52a52cSDavid Ahern 		goto out;
1120a3c00e46SMartin KaFai Lau 	}
11212b760fcfSWei Wang 
1122b1d40991SDavid Ahern 	fib6_select_path(net, &res, fl6, fl6->flowi6_oif,
1123b1d40991SDavid Ahern 			 fl6->flowi6_oif != 0, skb, flags);
1124b1d40991SDavid Ahern 
11254c9483b2SDavid S. Miller 	/* Search through exception table */
11267e4b5128SDavid Ahern 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
112723fb93a4SDavid Ahern 	if (rt) {
112810585b43SDavid Ahern 		if (ip6_hold_safe(net, &rt))
1129d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
113023fb93a4SDavid Ahern 	} else {
11319b6b35abSDavid Ahern 		rt = ip6_create_rt_rcu(&res);
1132dec9b0e2SDavid Ahern 	}
1133d3843fe5SWei Wang 
1134af52a52cSDavid Ahern out:
11358ff2e5b2SDavid Ahern 	trace_fib6_table_lookup(net, &res, table, fl6);
1136af52a52cSDavid Ahern 
113766f5d6ceSWei Wang 	rcu_read_unlock();
1138b811580dSDavid Ahern 
11391da177e4SLinus Torvalds 	return rt;
1140c71099acSThomas Graf }
1141c71099acSThomas Graf 
1142ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1143b75cc8f9SDavid Ahern 				   const struct sk_buff *skb, int flags)
1144ea6e574eSFlorian Westphal {
1145b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1146ea6e574eSFlorian Westphal }
1147ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
1148ea6e574eSFlorian Westphal 
11499acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1150b75cc8f9SDavid Ahern 			    const struct in6_addr *saddr, int oif,
1151b75cc8f9SDavid Ahern 			    const struct sk_buff *skb, int strict)
1152c71099acSThomas Graf {
11534c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
11544c9483b2SDavid S. Miller 		.flowi6_oif = oif,
11554c9483b2SDavid S. Miller 		.daddr = *daddr,
1156c71099acSThomas Graf 	};
1157c71099acSThomas Graf 	struct dst_entry *dst;
115877d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1159c71099acSThomas Graf 
1160adaa70bbSThomas Graf 	if (saddr) {
11614c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1162adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1163adaa70bbSThomas Graf 	}
1164adaa70bbSThomas Graf 
1165b75cc8f9SDavid Ahern 	dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1166c71099acSThomas Graf 	if (dst->error == 0)
1167c71099acSThomas Graf 		return (struct rt6_info *) dst;
1168c71099acSThomas Graf 
1169c71099acSThomas Graf 	dst_release(dst);
1170c71099acSThomas Graf 
11711da177e4SLinus Torvalds 	return NULL;
11721da177e4SLinus Torvalds }
11737159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
11747159039aSYOSHIFUJI Hideaki 
1175c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
11761cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
11771cfb71eeSWei Wang  * route is released.
11781cfb71eeSWei Wang  * Caller must hold dst before calling it.
11791da177e4SLinus Torvalds  */
11801da177e4SLinus Torvalds 
11818d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1182333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
11831da177e4SLinus Torvalds {
11841da177e4SLinus Torvalds 	int err;
1185c71099acSThomas Graf 	struct fib6_table *table;
11861da177e4SLinus Torvalds 
118793c2fb25SDavid Ahern 	table = rt->fib6_table;
118866f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
1189d4ead6b3SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, extack);
119066f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	return err;
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds 
11958d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt)
119640e22e8fSThomas Graf {
1197afb1d4b5SDavid Ahern 	struct nl_info info = {	.nl_net = net, };
1198e715b6d3SFlorian Westphal 
1199d4ead6b3SDavid Ahern 	return __ip6_ins_rt(rt, &info, NULL);
120040e22e8fSThomas Graf }
120140e22e8fSThomas Graf 
120285bd05deSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(const struct fib6_result *res,
120321efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
1204b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
12051da177e4SLinus Torvalds {
120685bd05deSDavid Ahern 	struct fib6_info *f6i = res->f6i;
12074832c30dSDavid Ahern 	struct net_device *dev;
12081da177e4SLinus Torvalds 	struct rt6_info *rt;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	/*
12111da177e4SLinus Torvalds 	 *	Clone the route.
12121da177e4SLinus Torvalds 	 */
12131da177e4SLinus Torvalds 
121485bd05deSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
1215e873e4b9SWei Wang 		return NULL;
1216e873e4b9SWei Wang 
12170d161581SDavid Ahern 	dev = ip6_rt_get_dev_rcu(res);
121893531c67SDavid Ahern 	rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1219e873e4b9SWei Wang 	if (!rt) {
122085bd05deSDavid Ahern 		fib6_info_release(f6i);
122183a09abdSMartin KaFai Lau 		return NULL;
1222e873e4b9SWei Wang 	}
122383a09abdSMartin KaFai Lau 
12240d161581SDavid Ahern 	ip6_rt_copy_init(rt, res);
12258b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
122683a09abdSMartin KaFai Lau 	rt->dst.flags |= DST_HOST;
122783a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
122883a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
12298b9df265SMartin KaFai Lau 
123085bd05deSDavid Ahern 	if (!rt6_is_gw_or_nonexthop(res)) {
123185bd05deSDavid Ahern 		if (f6i->fib6_dst.plen != 128 &&
123285bd05deSDavid Ahern 		    ipv6_addr_equal(&f6i->fib6_dst.addr, daddr))
123358c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
12341da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
12351da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
12364e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
12371da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
12381da177e4SLinus Torvalds 		}
12391da177e4SLinus Torvalds #endif
124095a9a5baSYOSHIFUJI Hideaki 	}
124195a9a5baSYOSHIFUJI Hideaki 
1242299d9939SYOSHIFUJI Hideaki 	return rt;
1243299d9939SYOSHIFUJI Hideaki }
1244299d9939SYOSHIFUJI Hideaki 
1245db3fedeeSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(const struct fib6_result *res)
1246d52d3997SMartin KaFai Lau {
1247db3fedeeSDavid Ahern 	struct fib6_info *f6i = res->f6i;
1248db3fedeeSDavid Ahern 	unsigned short flags = fib6_info_dst_flags(f6i);
12494832c30dSDavid Ahern 	struct net_device *dev;
1250d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1251d52d3997SMartin KaFai Lau 
1252db3fedeeSDavid Ahern 	if (!fib6_info_hold_safe(f6i))
1253e873e4b9SWei Wang 		return NULL;
1254e873e4b9SWei Wang 
12554832c30dSDavid Ahern 	rcu_read_lock();
12560d161581SDavid Ahern 	dev = ip6_rt_get_dev_rcu(res);
125793531c67SDavid Ahern 	pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
12584832c30dSDavid Ahern 	rcu_read_unlock();
1259e873e4b9SWei Wang 	if (!pcpu_rt) {
1260db3fedeeSDavid Ahern 		fib6_info_release(f6i);
1261d52d3997SMartin KaFai Lau 		return NULL;
1262e873e4b9SWei Wang 	}
12630d161581SDavid Ahern 	ip6_rt_copy_init(pcpu_rt, res);
1264d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
1265d52d3997SMartin KaFai Lau 	return pcpu_rt;
1266d52d3997SMartin KaFai Lau }
1267d52d3997SMartin KaFai Lau 
126866f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */
1269db3fedeeSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(const struct fib6_result *res)
1270d52d3997SMartin KaFai Lau {
1271a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, **p;
1272d52d3997SMartin KaFai Lau 
1273db3fedeeSDavid Ahern 	p = this_cpu_ptr(res->f6i->rt6i_pcpu);
1274d52d3997SMartin KaFai Lau 	pcpu_rt = *p;
1275d52d3997SMartin KaFai Lau 
1276d4ead6b3SDavid Ahern 	if (pcpu_rt)
127710585b43SDavid Ahern 		ip6_hold_safe(NULL, &pcpu_rt);
1278d3843fe5SWei Wang 
1279a73e4195SMartin KaFai Lau 	return pcpu_rt;
1280a73e4195SMartin KaFai Lau }
1281a73e4195SMartin KaFai Lau 
1282afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net,
1283db3fedeeSDavid Ahern 					    const struct fib6_result *res)
1284a73e4195SMartin KaFai Lau {
1285a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1286d52d3997SMartin KaFai Lau 
1287db3fedeeSDavid Ahern 	pcpu_rt = ip6_rt_pcpu_alloc(res);
1288d52d3997SMartin KaFai Lau 	if (!pcpu_rt) {
12899c7370a1SMartin KaFai Lau 		dst_hold(&net->ipv6.ip6_null_entry->dst);
12909c7370a1SMartin KaFai Lau 		return net->ipv6.ip6_null_entry;
1291d52d3997SMartin KaFai Lau 	}
1292d52d3997SMartin KaFai Lau 
1293a94b9367SWei Wang 	dst_hold(&pcpu_rt->dst);
1294db3fedeeSDavid Ahern 	p = this_cpu_ptr(res->f6i->rt6i_pcpu);
1295d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1296951f788aSEric Dumazet 	BUG_ON(prev);
1297a94b9367SWei Wang 
129861fb0d01SEric Dumazet 	if (res->f6i->fib6_destroying) {
129961fb0d01SEric Dumazet 		struct fib6_info *from;
130061fb0d01SEric Dumazet 
130161fb0d01SEric Dumazet 		from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL);
130261fb0d01SEric Dumazet 		fib6_info_release(from);
130361fb0d01SEric Dumazet 	}
130461fb0d01SEric Dumazet 
1305d52d3997SMartin KaFai Lau 	return pcpu_rt;
1306d52d3997SMartin KaFai Lau }
1307d52d3997SMartin KaFai Lau 
130835732d01SWei Wang /* exception hash table implementation
130935732d01SWei Wang  */
131035732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
131135732d01SWei Wang 
131235732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
131335732d01SWei Wang  * Caller must hold rt6_exception_lock
131435732d01SWei Wang  */
131535732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
131635732d01SWei Wang 				 struct rt6_exception *rt6_ex)
131735732d01SWei Wang {
1318f5b51fe8SPaolo Abeni 	struct fib6_info *from;
1319b2427e67SColin Ian King 	struct net *net;
132081eb8447SWei Wang 
132135732d01SWei Wang 	if (!bucket || !rt6_ex)
132235732d01SWei Wang 		return;
1323b2427e67SColin Ian King 
1324b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
1325f5b51fe8SPaolo Abeni 	net->ipv6.rt6_stats->fib_rt_cache--;
1326f5b51fe8SPaolo Abeni 
1327f5b51fe8SPaolo Abeni 	/* purge completely the exception to allow releasing the held resources:
1328f5b51fe8SPaolo Abeni 	 * some [sk] cache may keep the dst around for unlimited time
1329f5b51fe8SPaolo Abeni 	 */
13300e233874SEric Dumazet 	from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL);
1331f5b51fe8SPaolo Abeni 	fib6_info_release(from);
1332f5b51fe8SPaolo Abeni 	dst_dev_put(&rt6_ex->rt6i->dst);
1333f5b51fe8SPaolo Abeni 
133435732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
133577634cc6SDavid Ahern 	dst_release(&rt6_ex->rt6i->dst);
133635732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
133735732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
133835732d01SWei Wang 	bucket->depth--;
133935732d01SWei Wang }
134035732d01SWei Wang 
134135732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
134235732d01SWei Wang  * Caller must hold rt6_exception_lock
134335732d01SWei Wang  */
134435732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
134535732d01SWei Wang {
134635732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
134735732d01SWei Wang 
134835732d01SWei Wang 	if (!bucket)
134935732d01SWei Wang 		return;
135035732d01SWei Wang 
135135732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
135235732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
135335732d01SWei Wang 			oldest = rt6_ex;
135435732d01SWei Wang 	}
135535732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
135635732d01SWei Wang }
135735732d01SWei Wang 
135835732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
135935732d01SWei Wang 			      const struct in6_addr *src)
136035732d01SWei Wang {
136135732d01SWei Wang 	static u32 seed __read_mostly;
136235732d01SWei Wang 	u32 val;
136335732d01SWei Wang 
136435732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
136535732d01SWei Wang 	val = jhash(dst, sizeof(*dst), seed);
136635732d01SWei Wang 
136735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
136835732d01SWei Wang 	if (src)
136935732d01SWei Wang 		val = jhash(src, sizeof(*src), val);
137035732d01SWei Wang #endif
137135732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
137235732d01SWei Wang }
137335732d01SWei Wang 
137435732d01SWei Wang /* Helper function to find the cached rt in the hash table
137535732d01SWei Wang  * and update bucket pointer to point to the bucket for this
137635732d01SWei Wang  * (daddr, saddr) pair
137735732d01SWei Wang  * Caller must hold rt6_exception_lock
137835732d01SWei Wang  */
137935732d01SWei Wang static struct rt6_exception *
138035732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
138135732d01SWei Wang 			      const struct in6_addr *daddr,
138235732d01SWei Wang 			      const struct in6_addr *saddr)
138335732d01SWei Wang {
138435732d01SWei Wang 	struct rt6_exception *rt6_ex;
138535732d01SWei Wang 	u32 hval;
138635732d01SWei Wang 
138735732d01SWei Wang 	if (!(*bucket) || !daddr)
138835732d01SWei Wang 		return NULL;
138935732d01SWei Wang 
139035732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
139135732d01SWei Wang 	*bucket += hval;
139235732d01SWei Wang 
139335732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
139435732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
139535732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
139635732d01SWei Wang 
139735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
139835732d01SWei Wang 		if (matched && saddr)
139935732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
140035732d01SWei Wang #endif
140135732d01SWei Wang 		if (matched)
140235732d01SWei Wang 			return rt6_ex;
140335732d01SWei Wang 	}
140435732d01SWei Wang 	return NULL;
140535732d01SWei Wang }
140635732d01SWei Wang 
140735732d01SWei Wang /* Helper function to find the cached rt in the hash table
140835732d01SWei Wang  * and update bucket pointer to point to the bucket for this
140935732d01SWei Wang  * (daddr, saddr) pair
141035732d01SWei Wang  * Caller must hold rcu_read_lock()
141135732d01SWei Wang  */
141235732d01SWei Wang static struct rt6_exception *
141335732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
141435732d01SWei Wang 			 const struct in6_addr *daddr,
141535732d01SWei Wang 			 const struct in6_addr *saddr)
141635732d01SWei Wang {
141735732d01SWei Wang 	struct rt6_exception *rt6_ex;
141835732d01SWei Wang 	u32 hval;
141935732d01SWei Wang 
142035732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
142135732d01SWei Wang 
142235732d01SWei Wang 	if (!(*bucket) || !daddr)
142335732d01SWei Wang 		return NULL;
142435732d01SWei Wang 
142535732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
142635732d01SWei Wang 	*bucket += hval;
142735732d01SWei Wang 
142835732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
142935732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
143035732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
143135732d01SWei Wang 
143235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
143335732d01SWei Wang 		if (matched && saddr)
143435732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
143535732d01SWei Wang #endif
143635732d01SWei Wang 		if (matched)
143735732d01SWei Wang 			return rt6_ex;
143835732d01SWei Wang 	}
143935732d01SWei Wang 	return NULL;
144035732d01SWei Wang }
144135732d01SWei Wang 
1442b748f260SDavid Ahern static unsigned int fib6_mtu(const struct fib6_result *res)
144335732d01SWei Wang {
1444b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
1445d4ead6b3SDavid Ahern 	unsigned int mtu;
1446d4ead6b3SDavid Ahern 
1447b748f260SDavid Ahern 	if (res->f6i->fib6_pmtu) {
1448b748f260SDavid Ahern 		mtu = res->f6i->fib6_pmtu;
1449dcd1f572SDavid Ahern 	} else {
1450b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
1451dcd1f572SDavid Ahern 		struct inet6_dev *idev;
1452dcd1f572SDavid Ahern 
1453dcd1f572SDavid Ahern 		rcu_read_lock();
1454dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
1455dcd1f572SDavid Ahern 		mtu = idev->cnf.mtu6;
1456dcd1f572SDavid Ahern 		rcu_read_unlock();
1457dcd1f572SDavid Ahern 	}
1458dcd1f572SDavid Ahern 
1459d4ead6b3SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1460d4ead6b3SDavid Ahern 
1461b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
1462d4ead6b3SDavid Ahern }
1463d4ead6b3SDavid Ahern 
146435732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
14655012f0a5SDavid Ahern 				const struct fib6_result *res)
146635732d01SWei Wang {
14675e670d84SDavid Ahern 	struct net *net = dev_net(nrt->dst.dev);
146835732d01SWei Wang 	struct rt6_exception_bucket *bucket;
146935732d01SWei Wang 	struct in6_addr *src_key = NULL;
147035732d01SWei Wang 	struct rt6_exception *rt6_ex;
14715012f0a5SDavid Ahern 	struct fib6_info *f6i = res->f6i;
147235732d01SWei Wang 	int err = 0;
147335732d01SWei Wang 
147435732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
147535732d01SWei Wang 
14765012f0a5SDavid Ahern 	if (f6i->exception_bucket_flushed) {
147735732d01SWei Wang 		err = -EINVAL;
147835732d01SWei Wang 		goto out;
147935732d01SWei Wang 	}
148035732d01SWei Wang 
14815012f0a5SDavid Ahern 	bucket = rcu_dereference_protected(f6i->rt6i_exception_bucket,
148235732d01SWei Wang 					lockdep_is_held(&rt6_exception_lock));
148335732d01SWei Wang 	if (!bucket) {
148435732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
148535732d01SWei Wang 				 GFP_ATOMIC);
148635732d01SWei Wang 		if (!bucket) {
148735732d01SWei Wang 			err = -ENOMEM;
148835732d01SWei Wang 			goto out;
148935732d01SWei Wang 		}
14905012f0a5SDavid Ahern 		rcu_assign_pointer(f6i->rt6i_exception_bucket, bucket);
149135732d01SWei Wang 	}
149235732d01SWei Wang 
149335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
14945012f0a5SDavid Ahern 	/* fib6_src.plen != 0 indicates f6i is in subtree
149535732d01SWei Wang 	 * and exception table is indexed by a hash of
14965012f0a5SDavid Ahern 	 * both fib6_dst and fib6_src.
149735732d01SWei Wang 	 * Otherwise, the exception table is indexed by
14985012f0a5SDavid Ahern 	 * a hash of only fib6_dst.
149935732d01SWei Wang 	 */
15005012f0a5SDavid Ahern 	if (f6i->fib6_src.plen)
150135732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
150235732d01SWei Wang #endif
15035012f0a5SDavid Ahern 	/* rt6_mtu_change() might lower mtu on f6i.
1504f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
15055012f0a5SDavid Ahern 	 * is less than f6i's mtu value.
1506f5bbe7eeSWei Wang 	 */
1507b748f260SDavid Ahern 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(res)) {
1508f5bbe7eeSWei Wang 		err = -EINVAL;
1509f5bbe7eeSWei Wang 		goto out;
1510f5bbe7eeSWei Wang 	}
151160006a48SWei Wang 
151235732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
151335732d01SWei Wang 					       src_key);
151435732d01SWei Wang 	if (rt6_ex)
151535732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
151635732d01SWei Wang 
151735732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
151835732d01SWei Wang 	if (!rt6_ex) {
151935732d01SWei Wang 		err = -ENOMEM;
152035732d01SWei Wang 		goto out;
152135732d01SWei Wang 	}
152235732d01SWei Wang 	rt6_ex->rt6i = nrt;
152335732d01SWei Wang 	rt6_ex->stamp = jiffies;
152435732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
152535732d01SWei Wang 	bucket->depth++;
152681eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
152735732d01SWei Wang 
152835732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
152935732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
153035732d01SWei Wang 
153135732d01SWei Wang out:
153235732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
153335732d01SWei Wang 
153435732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1535b886d5f2SPaolo Abeni 	if (!err) {
15365012f0a5SDavid Ahern 		spin_lock_bh(&f6i->fib6_table->tb6_lock);
15375012f0a5SDavid Ahern 		fib6_update_sernum(net, f6i);
15385012f0a5SDavid Ahern 		spin_unlock_bh(&f6i->fib6_table->tb6_lock);
1539b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1540b886d5f2SPaolo Abeni 	}
154135732d01SWei Wang 
154235732d01SWei Wang 	return err;
154335732d01SWei Wang }
154435732d01SWei Wang 
15458d1c802bSDavid Ahern void rt6_flush_exceptions(struct fib6_info *rt)
154635732d01SWei Wang {
154735732d01SWei Wang 	struct rt6_exception_bucket *bucket;
154835732d01SWei Wang 	struct rt6_exception *rt6_ex;
154935732d01SWei Wang 	struct hlist_node *tmp;
155035732d01SWei Wang 	int i;
155135732d01SWei Wang 
155235732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
155335732d01SWei Wang 	/* Prevent rt6_insert_exception() to recreate the bucket list */
155435732d01SWei Wang 	rt->exception_bucket_flushed = 1;
155535732d01SWei Wang 
155635732d01SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
155735732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
155835732d01SWei Wang 	if (!bucket)
155935732d01SWei Wang 		goto out;
156035732d01SWei Wang 
156135732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
156235732d01SWei Wang 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
156335732d01SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
156435732d01SWei Wang 		WARN_ON_ONCE(bucket->depth);
156535732d01SWei Wang 		bucket++;
156635732d01SWei Wang 	}
156735732d01SWei Wang 
156835732d01SWei Wang out:
156935732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
157035732d01SWei Wang }
157135732d01SWei Wang 
157235732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
157335732d01SWei Wang  * Caller has to hold rcu_read_lock()
157435732d01SWei Wang  */
15757e4b5128SDavid Ahern static struct rt6_info *rt6_find_cached_rt(const struct fib6_result *res,
1576*510e2cedSWei Wang 					   const struct in6_addr *daddr,
1577*510e2cedSWei Wang 					   const struct in6_addr *saddr)
157835732d01SWei Wang {
1579*510e2cedSWei Wang 	const struct in6_addr *src_key = NULL;
158035732d01SWei Wang 	struct rt6_exception_bucket *bucket;
158135732d01SWei Wang 	struct rt6_exception *rt6_ex;
15827e4b5128SDavid Ahern 	struct rt6_info *ret = NULL;
158335732d01SWei Wang 
158435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
15857e4b5128SDavid Ahern 	/* fib6i_src.plen != 0 indicates f6i is in subtree
158635732d01SWei Wang 	 * and exception table is indexed by a hash of
15877e4b5128SDavid Ahern 	 * both fib6_dst and fib6_src.
1588*510e2cedSWei Wang 	 * However, the src addr used to create the hash
1589*510e2cedSWei Wang 	 * might not be exactly the passed in saddr which
1590*510e2cedSWei Wang 	 * is a /128 addr from the flow.
1591*510e2cedSWei Wang 	 * So we need to use f6i->fib6_src to redo lookup
1592*510e2cedSWei Wang 	 * if the passed in saddr does not find anything.
1593*510e2cedSWei Wang 	 * (See the logic in ip6_rt_cache_alloc() on how
1594*510e2cedSWei Wang 	 * rt->rt6i_src is updated.)
159535732d01SWei Wang 	 */
15967e4b5128SDavid Ahern 	if (res->f6i->fib6_src.plen)
159735732d01SWei Wang 		src_key = saddr;
1598*510e2cedSWei Wang find_ex:
159935732d01SWei Wang #endif
1600*510e2cedSWei Wang 	bucket = rcu_dereference(res->f6i->rt6i_exception_bucket);
160135732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
160235732d01SWei Wang 
160335732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
16047e4b5128SDavid Ahern 		ret = rt6_ex->rt6i;
160535732d01SWei Wang 
1606*510e2cedSWei Wang #ifdef CONFIG_IPV6_SUBTREES
1607*510e2cedSWei Wang 	/* Use fib6_src as src_key and redo lookup */
1608*510e2cedSWei Wang 	if (!ret && src_key && src_key != &res->f6i->fib6_src.addr) {
1609*510e2cedSWei Wang 		src_key = &res->f6i->fib6_src.addr;
1610*510e2cedSWei Wang 		goto find_ex;
1611*510e2cedSWei Wang 	}
1612*510e2cedSWei Wang #endif
1613*510e2cedSWei Wang 
16147e4b5128SDavid Ahern 	return ret;
161535732d01SWei Wang }
161635732d01SWei Wang 
161735732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
161823fb93a4SDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt)
161935732d01SWei Wang {
162035732d01SWei Wang 	struct rt6_exception_bucket *bucket;
162135732d01SWei Wang 	struct in6_addr *src_key = NULL;
162235732d01SWei Wang 	struct rt6_exception *rt6_ex;
16238a14e46fSDavid Ahern 	struct fib6_info *from;
162435732d01SWei Wang 	int err;
162535732d01SWei Wang 
1626091311deSEric Dumazet 	from = rcu_dereference(rt->from);
162735732d01SWei Wang 	if (!from ||
1628442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
162935732d01SWei Wang 		return -EINVAL;
163035732d01SWei Wang 
163135732d01SWei Wang 	if (!rcu_access_pointer(from->rt6i_exception_bucket))
163235732d01SWei Wang 		return -ENOENT;
163335732d01SWei Wang 
163435732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
163535732d01SWei Wang 	bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
163635732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
163735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
163835732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
163935732d01SWei Wang 	 * and exception table is indexed by a hash of
164035732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
164135732d01SWei Wang 	 * Otherwise, the exception table is indexed by
164235732d01SWei Wang 	 * a hash of only rt6i_dst.
164335732d01SWei Wang 	 */
164493c2fb25SDavid Ahern 	if (from->fib6_src.plen)
164535732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
164635732d01SWei Wang #endif
164735732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
164835732d01SWei Wang 					       &rt->rt6i_dst.addr,
164935732d01SWei Wang 					       src_key);
165035732d01SWei Wang 	if (rt6_ex) {
165135732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
165235732d01SWei Wang 		err = 0;
165335732d01SWei Wang 	} else {
165435732d01SWei Wang 		err = -ENOENT;
165535732d01SWei Wang 	}
165635732d01SWei Wang 
165735732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
165835732d01SWei Wang 	return err;
165935732d01SWei Wang }
166035732d01SWei Wang 
166135732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
166235732d01SWei Wang  * refresh its stamp
166335732d01SWei Wang  */
166435732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
166535732d01SWei Wang {
166635732d01SWei Wang 	struct rt6_exception_bucket *bucket;
166735732d01SWei Wang 	struct in6_addr *src_key = NULL;
166835732d01SWei Wang 	struct rt6_exception *rt6_ex;
1669193f3685SPaolo Abeni 	struct fib6_info *from;
167035732d01SWei Wang 
167135732d01SWei Wang 	rcu_read_lock();
1672193f3685SPaolo Abeni 	from = rcu_dereference(rt->from);
1673193f3685SPaolo Abeni 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1674193f3685SPaolo Abeni 		goto unlock;
1675193f3685SPaolo Abeni 
167635732d01SWei Wang 	bucket = rcu_dereference(from->rt6i_exception_bucket);
167735732d01SWei Wang 
167835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
167935732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
168035732d01SWei Wang 	 * and exception table is indexed by a hash of
168135732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
168235732d01SWei Wang 	 * Otherwise, the exception table is indexed by
168335732d01SWei Wang 	 * a hash of only rt6i_dst.
168435732d01SWei Wang 	 */
168593c2fb25SDavid Ahern 	if (from->fib6_src.plen)
168635732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
168735732d01SWei Wang #endif
168835732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket,
168935732d01SWei Wang 					  &rt->rt6i_dst.addr,
169035732d01SWei Wang 					  src_key);
169135732d01SWei Wang 	if (rt6_ex)
169235732d01SWei Wang 		rt6_ex->stamp = jiffies;
169335732d01SWei Wang 
1694193f3685SPaolo Abeni unlock:
169535732d01SWei Wang 	rcu_read_unlock();
169635732d01SWei Wang }
169735732d01SWei Wang 
1698e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1699e9fa1495SStefano Brivio 					 struct rt6_info *rt, int mtu)
1700e9fa1495SStefano Brivio {
1701e9fa1495SStefano Brivio 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1702e9fa1495SStefano Brivio 	 * lowest MTU in the path: always allow updating the route PMTU to
1703e9fa1495SStefano Brivio 	 * reflect PMTU decreases.
1704e9fa1495SStefano Brivio 	 *
1705e9fa1495SStefano Brivio 	 * If the new MTU is higher, and the route PMTU is equal to the local
1706e9fa1495SStefano Brivio 	 * MTU, this means the old MTU is the lowest in the path, so allow
1707e9fa1495SStefano Brivio 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1708e9fa1495SStefano Brivio 	 * handle this.
1709e9fa1495SStefano Brivio 	 */
1710e9fa1495SStefano Brivio 
1711e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) >= mtu)
1712e9fa1495SStefano Brivio 		return true;
1713e9fa1495SStefano Brivio 
1714e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1715e9fa1495SStefano Brivio 		return true;
1716e9fa1495SStefano Brivio 
1717e9fa1495SStefano Brivio 	return false;
1718e9fa1495SStefano Brivio }
1719e9fa1495SStefano Brivio 
1720e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
17218d1c802bSDavid Ahern 				       struct fib6_info *rt, int mtu)
1722f5bbe7eeSWei Wang {
1723f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
1724f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
1725f5bbe7eeSWei Wang 	int i;
1726f5bbe7eeSWei Wang 
1727f5bbe7eeSWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1728f5bbe7eeSWei Wang 					lockdep_is_held(&rt6_exception_lock));
1729f5bbe7eeSWei Wang 
1730e9fa1495SStefano Brivio 	if (!bucket)
1731e9fa1495SStefano Brivio 		return;
1732e9fa1495SStefano Brivio 
1733f5bbe7eeSWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1734f5bbe7eeSWei Wang 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1735f5bbe7eeSWei Wang 			struct rt6_info *entry = rt6_ex->rt6i;
1736e9fa1495SStefano Brivio 
1737e9fa1495SStefano Brivio 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
1738d4ead6b3SDavid Ahern 			 * route), the metrics of its rt->from have already
1739f5bbe7eeSWei Wang 			 * been updated.
1740f5bbe7eeSWei Wang 			 */
1741d4ead6b3SDavid Ahern 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
1742e9fa1495SStefano Brivio 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
1743d4ead6b3SDavid Ahern 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
1744f5bbe7eeSWei Wang 		}
1745f5bbe7eeSWei Wang 		bucket++;
1746f5bbe7eeSWei Wang 	}
1747f5bbe7eeSWei Wang }
1748f5bbe7eeSWei Wang 
1749b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
1750b16cb459SWei Wang 
17518d1c802bSDavid Ahern static void rt6_exceptions_clean_tohost(struct fib6_info *rt,
1752b16cb459SWei Wang 					struct in6_addr *gateway)
1753b16cb459SWei Wang {
1754b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
1755b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
1756b16cb459SWei Wang 	struct hlist_node *tmp;
1757b16cb459SWei Wang 	int i;
1758b16cb459SWei Wang 
1759b16cb459SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1760b16cb459SWei Wang 		return;
1761b16cb459SWei Wang 
1762b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1763b16cb459SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1764b16cb459SWei Wang 				     lockdep_is_held(&rt6_exception_lock));
1765b16cb459SWei Wang 
1766b16cb459SWei Wang 	if (bucket) {
1767b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1768b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1769b16cb459SWei Wang 						  &bucket->chain, hlist) {
1770b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1771b16cb459SWei Wang 
1772b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1773b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
1774b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
1775b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
1776b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
1777b16cb459SWei Wang 				}
1778b16cb459SWei Wang 			}
1779b16cb459SWei Wang 			bucket++;
1780b16cb459SWei Wang 		}
1781b16cb459SWei Wang 	}
1782b16cb459SWei Wang 
1783b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1784b16cb459SWei Wang }
1785b16cb459SWei Wang 
1786c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1787c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
1788c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
1789c757faa8SWei Wang 				      unsigned long now)
1790c757faa8SWei Wang {
1791c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
1792c757faa8SWei Wang 
17931859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
17941859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
17951859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
17961859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
17971859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
17981859bac0SPaolo Abeni 	 */
179931afeb42SWei Wang 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
180031afeb42SWei Wang 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1801c757faa8SWei Wang 			RT6_TRACE("aging clone %p\n", rt);
1802c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1803c757faa8SWei Wang 			return;
180431afeb42SWei Wang 		}
180531afeb42SWei Wang 	} else if (time_after(jiffies, rt->dst.expires)) {
180631afeb42SWei Wang 		RT6_TRACE("purging expired route %p\n", rt);
180731afeb42SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
180831afeb42SWei Wang 		return;
180931afeb42SWei Wang 	}
181031afeb42SWei Wang 
181131afeb42SWei Wang 	if (rt->rt6i_flags & RTF_GATEWAY) {
1812c757faa8SWei Wang 		struct neighbour *neigh;
1813c757faa8SWei Wang 		__u8 neigh_flags = 0;
1814c757faa8SWei Wang 
18151bfa26ffSEric Dumazet 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
18161bfa26ffSEric Dumazet 		if (neigh)
1817c757faa8SWei Wang 			neigh_flags = neigh->flags;
18181bfa26ffSEric Dumazet 
1819c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
1820c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
1821c757faa8SWei Wang 				  rt);
1822c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1823c757faa8SWei Wang 			return;
1824c757faa8SWei Wang 		}
1825c757faa8SWei Wang 	}
182631afeb42SWei Wang 
1827c757faa8SWei Wang 	gc_args->more++;
1828c757faa8SWei Wang }
1829c757faa8SWei Wang 
18308d1c802bSDavid Ahern void rt6_age_exceptions(struct fib6_info *rt,
1831c757faa8SWei Wang 			struct fib6_gc_args *gc_args,
1832c757faa8SWei Wang 			unsigned long now)
1833c757faa8SWei Wang {
1834c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
1835c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
1836c757faa8SWei Wang 	struct hlist_node *tmp;
1837c757faa8SWei Wang 	int i;
1838c757faa8SWei Wang 
1839c757faa8SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1840c757faa8SWei Wang 		return;
1841c757faa8SWei Wang 
18421bfa26ffSEric Dumazet 	rcu_read_lock_bh();
18431bfa26ffSEric Dumazet 	spin_lock(&rt6_exception_lock);
1844c757faa8SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1845c757faa8SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
1846c757faa8SWei Wang 
1847c757faa8SWei Wang 	if (bucket) {
1848c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1849c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1850c757faa8SWei Wang 						  &bucket->chain, hlist) {
1851c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
1852c757faa8SWei Wang 							  gc_args, now);
1853c757faa8SWei Wang 			}
1854c757faa8SWei Wang 			bucket++;
1855c757faa8SWei Wang 		}
1856c757faa8SWei Wang 	}
18571bfa26ffSEric Dumazet 	spin_unlock(&rt6_exception_lock);
18581bfa26ffSEric Dumazet 	rcu_read_unlock_bh();
1859c757faa8SWei Wang }
1860c757faa8SWei Wang 
18611d053da9SDavid Ahern /* must be called with rcu lock held */
1862effda4ddSDavid Ahern int fib6_table_lookup(struct net *net, struct fib6_table *table, int oif,
1863effda4ddSDavid Ahern 		      struct flowi6 *fl6, struct fib6_result *res, int strict)
18641da177e4SLinus Torvalds {
1865367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
18661da177e4SLinus Torvalds 
18676454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1868367efcb9SMartin KaFai Lau 	saved_fn = fn;
18691da177e4SLinus Torvalds 
1870ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1871ca254490SDavid Ahern 		oif = 0;
1872ca254490SDavid Ahern 
1873a3c00e46SMartin KaFai Lau redo_rt6_select:
1874effda4ddSDavid Ahern 	rt6_select(net, fn, oif, res, strict);
1875effda4ddSDavid Ahern 	if (res->f6i == net->ipv6.fib6_null_entry) {
1876a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1877a3c00e46SMartin KaFai Lau 		if (fn)
1878a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
1879367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
1880367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
1881367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
1882367efcb9SMartin KaFai Lau 			fn = saved_fn;
1883367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
1884367efcb9SMartin KaFai Lau 		}
1885a3c00e46SMartin KaFai Lau 	}
1886a3c00e46SMartin KaFai Lau 
1887effda4ddSDavid Ahern 	trace_fib6_table_lookup(net, res, table, fl6);
1888d52d3997SMartin KaFai Lau 
1889effda4ddSDavid Ahern 	return 0;
18901d053da9SDavid Ahern }
18911d053da9SDavid Ahern 
18921d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
18931d053da9SDavid Ahern 			       int oif, struct flowi6 *fl6,
18941d053da9SDavid Ahern 			       const struct sk_buff *skb, int flags)
18951d053da9SDavid Ahern {
1896b1d40991SDavid Ahern 	struct fib6_result res = {};
18971d053da9SDavid Ahern 	struct rt6_info *rt;
18981d053da9SDavid Ahern 	int strict = 0;
18991d053da9SDavid Ahern 
19001d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IFACE;
19011d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
19021d053da9SDavid Ahern 	if (net->ipv6.devconf_all->forwarding == 0)
19031d053da9SDavid Ahern 		strict |= RT6_LOOKUP_F_REACHABLE;
19041d053da9SDavid Ahern 
19051d053da9SDavid Ahern 	rcu_read_lock();
19061d053da9SDavid Ahern 
1907effda4ddSDavid Ahern 	fib6_table_lookup(net, table, oif, fl6, &res, strict);
1908b1d40991SDavid Ahern 	if (res.f6i == net->ipv6.fib6_null_entry) {
1909421842edSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
191066f5d6ceSWei Wang 		rcu_read_unlock();
1911d3843fe5SWei Wang 		dst_hold(&rt->dst);
1912d3843fe5SWei Wang 		return rt;
1913d3843fe5SWei Wang 	}
191423fb93a4SDavid Ahern 
1915b1d40991SDavid Ahern 	fib6_select_path(net, &res, fl6, oif, false, skb, strict);
1916d83009d4SDavid Ahern 
191723fb93a4SDavid Ahern 	/*Search through exception table */
19187e4b5128SDavid Ahern 	rt = rt6_find_cached_rt(&res, &fl6->daddr, &fl6->saddr);
191923fb93a4SDavid Ahern 	if (rt) {
192010585b43SDavid Ahern 		if (ip6_hold_safe(net, &rt))
19211da177e4SLinus Torvalds 			dst_use_noref(&rt->dst, jiffies);
1922d4ead6b3SDavid Ahern 
192366f5d6ceSWei Wang 		rcu_read_unlock();
1924d52d3997SMartin KaFai Lau 		return rt;
19253da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
1926b1d40991SDavid Ahern 			    !res.nh->fib_nh_gw_family)) {
19273da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
19283da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
19293da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
19303da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
19313da59bd9SMartin KaFai Lau 		 */
19323da59bd9SMartin KaFai Lau 		struct rt6_info *uncached_rt;
19333da59bd9SMartin KaFai Lau 
193485bd05deSDavid Ahern 		uncached_rt = ip6_rt_cache_alloc(&res, &fl6->daddr, NULL);
1935d52d3997SMartin KaFai Lau 
19364d85cd0cSDavid Ahern 		rcu_read_unlock();
19373da59bd9SMartin KaFai Lau 
19381cfb71eeSWei Wang 		if (uncached_rt) {
19391cfb71eeSWei Wang 			/* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
19401cfb71eeSWei Wang 			 * No need for another dst_hold()
19411cfb71eeSWei Wang 			 */
19428d0b94afSMartin KaFai Lau 			rt6_uncached_list_add(uncached_rt);
194381eb8447SWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
19441cfb71eeSWei Wang 		} else {
19453da59bd9SMartin KaFai Lau 			uncached_rt = net->ipv6.ip6_null_entry;
19463da59bd9SMartin KaFai Lau 			dst_hold(&uncached_rt->dst);
19471cfb71eeSWei Wang 		}
1948b811580dSDavid Ahern 
19493da59bd9SMartin KaFai Lau 		return uncached_rt;
1950d52d3997SMartin KaFai Lau 	} else {
1951d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
1952d52d3997SMartin KaFai Lau 
1953d52d3997SMartin KaFai Lau 		struct rt6_info *pcpu_rt;
1954d52d3997SMartin KaFai Lau 
1955951f788aSEric Dumazet 		local_bh_disable();
1956db3fedeeSDavid Ahern 		pcpu_rt = rt6_get_pcpu_route(&res);
1957d52d3997SMartin KaFai Lau 
195893531c67SDavid Ahern 		if (!pcpu_rt)
1959db3fedeeSDavid Ahern 			pcpu_rt = rt6_make_pcpu_route(net, &res);
196093531c67SDavid Ahern 
1961951f788aSEric Dumazet 		local_bh_enable();
1962951f788aSEric Dumazet 		rcu_read_unlock();
1963d4bea421SDavid Ahern 
1964d52d3997SMartin KaFai Lau 		return pcpu_rt;
1965d52d3997SMartin KaFai Lau 	}
1966c71099acSThomas Graf }
19679ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
1968c71099acSThomas Graf 
1969b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net,
1970b75cc8f9SDavid Ahern 					    struct fib6_table *table,
1971b75cc8f9SDavid Ahern 					    struct flowi6 *fl6,
1972b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
1973b75cc8f9SDavid Ahern 					    int flags)
19744acad72dSPavel Emelyanov {
1975b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
19764acad72dSPavel Emelyanov }
19774acad72dSPavel Emelyanov 
1978d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
197972331bc0SShmulik Ladkani 					 struct net_device *dev,
1980b75cc8f9SDavid Ahern 					 struct flowi6 *fl6,
1981b75cc8f9SDavid Ahern 					 const struct sk_buff *skb,
1982b75cc8f9SDavid Ahern 					 int flags)
198372331bc0SShmulik Ladkani {
198472331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
198572331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
198672331bc0SShmulik Ladkani 
1987b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
198872331bc0SShmulik Ladkani }
1989d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
199072331bc0SShmulik Ladkani 
199123aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
19925e5d6fedSRoopa Prabhu 				  struct flow_keys *keys,
19935e5d6fedSRoopa Prabhu 				  struct flow_keys *flkeys)
199423aebdacSJakub Sitnicki {
199523aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
199623aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
19975e5d6fedSRoopa Prabhu 	struct flow_keys *_flkeys = flkeys;
199823aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
199923aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
200023aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
2001cea67a2dSEric Dumazet 	struct icmp6hdr _icmph;
200223aebdacSJakub Sitnicki 
200323aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
200423aebdacSJakub Sitnicki 		goto out;
200523aebdacSJakub Sitnicki 
2006cea67a2dSEric Dumazet 	icmph = skb_header_pointer(skb, skb_transport_offset(skb),
2007cea67a2dSEric Dumazet 				   sizeof(_icmph), &_icmph);
2008cea67a2dSEric Dumazet 	if (!icmph)
2009cea67a2dSEric Dumazet 		goto out;
2010cea67a2dSEric Dumazet 
201123aebdacSJakub Sitnicki 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
201223aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
201323aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
201423aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
201523aebdacSJakub Sitnicki 		goto out;
201623aebdacSJakub Sitnicki 
201723aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
201823aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
201923aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
202023aebdacSJakub Sitnicki 	if (!inner_iph)
202123aebdacSJakub Sitnicki 		goto out;
202223aebdacSJakub Sitnicki 
202323aebdacSJakub Sitnicki 	key_iph = inner_iph;
20245e5d6fedSRoopa Prabhu 	_flkeys = NULL;
202523aebdacSJakub Sitnicki out:
20265e5d6fedSRoopa Prabhu 	if (_flkeys) {
20275e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
20285e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
20295e5d6fedSRoopa Prabhu 		keys->tags.flow_label = _flkeys->tags.flow_label;
20305e5d6fedSRoopa Prabhu 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
20315e5d6fedSRoopa Prabhu 	} else {
203223aebdacSJakub Sitnicki 		keys->addrs.v6addrs.src = key_iph->saddr;
203323aebdacSJakub Sitnicki 		keys->addrs.v6addrs.dst = key_iph->daddr;
2034fa1be7e0SMichal Kubecek 		keys->tags.flow_label = ip6_flowlabel(key_iph);
203523aebdacSJakub Sitnicki 		keys->basic.ip_proto = key_iph->nexthdr;
203623aebdacSJakub Sitnicki 	}
20375e5d6fedSRoopa Prabhu }
203823aebdacSJakub Sitnicki 
203923aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
2040b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
2041b4bac172SDavid Ahern 		       const struct sk_buff *skb, struct flow_keys *flkeys)
204223aebdacSJakub Sitnicki {
204323aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
20449a2a537aSDavid Ahern 	u32 mhash;
204523aebdacSJakub Sitnicki 
2046bbfa047aSDavid S. Miller 	switch (ip6_multipath_hash_policy(net)) {
2047b4bac172SDavid Ahern 	case 0:
20486f74b6c2SDavid Ahern 		memset(&hash_keys, 0, sizeof(hash_keys));
20496f74b6c2SDavid Ahern 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
20509a2a537aSDavid Ahern 		if (skb) {
20515e5d6fedSRoopa Prabhu 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
20529a2a537aSDavid Ahern 		} else {
20539a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
20549a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2055fa1be7e0SMichal Kubecek 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
20569a2a537aSDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
205723aebdacSJakub Sitnicki 		}
2058b4bac172SDavid Ahern 		break;
2059b4bac172SDavid Ahern 	case 1:
2060b4bac172SDavid Ahern 		if (skb) {
2061b4bac172SDavid Ahern 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2062b4bac172SDavid Ahern 			struct flow_keys keys;
2063b4bac172SDavid Ahern 
2064b4bac172SDavid Ahern 			/* short-circuit if we already have L4 hash present */
2065b4bac172SDavid Ahern 			if (skb->l4_hash)
2066b4bac172SDavid Ahern 				return skb_get_hash_raw(skb) >> 1;
2067b4bac172SDavid Ahern 
2068b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2069b4bac172SDavid Ahern 
2070b4bac172SDavid Ahern                         if (!flkeys) {
2071b4bac172SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2072b4bac172SDavid Ahern 				flkeys = &keys;
2073b4bac172SDavid Ahern 			}
2074b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2075b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2076b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2077b4bac172SDavid Ahern 			hash_keys.ports.src = flkeys->ports.src;
2078b4bac172SDavid Ahern 			hash_keys.ports.dst = flkeys->ports.dst;
2079b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2080b4bac172SDavid Ahern 		} else {
2081b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2082b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2083b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2084b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2085b4bac172SDavid Ahern 			hash_keys.ports.src = fl6->fl6_sport;
2086b4bac172SDavid Ahern 			hash_keys.ports.dst = fl6->fl6_dport;
2087b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2088b4bac172SDavid Ahern 		}
2089b4bac172SDavid Ahern 		break;
2090b4bac172SDavid Ahern 	}
20919a2a537aSDavid Ahern 	mhash = flow_hash_from_keys(&hash_keys);
209223aebdacSJakub Sitnicki 
20939a2a537aSDavid Ahern 	return mhash >> 1;
209423aebdacSJakub Sitnicki }
209523aebdacSJakub Sitnicki 
2096c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
2097c71099acSThomas Graf {
2098b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2099c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
2100adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2101904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
21024c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
2103e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
21044c9483b2SDavid S. Miller 		.daddr = iph->daddr,
21054c9483b2SDavid S. Miller 		.saddr = iph->saddr,
21066502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
21074c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
21084c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
2109c71099acSThomas Graf 	};
21105e5d6fedSRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2111adaa70bbSThomas Graf 
2112904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
211346fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2114904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
21155e5d6fedSRoopa Prabhu 
21165e5d6fedSRoopa Prabhu 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
21175e5d6fedSRoopa Prabhu 		flkeys = &_flkeys;
21185e5d6fedSRoopa Prabhu 
211923aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2120b4bac172SDavid Ahern 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
212106e9d040SJiri Benc 	skb_dst_drop(skb);
2122b75cc8f9SDavid Ahern 	skb_dst_set(skb,
2123b75cc8f9SDavid Ahern 		    ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
2124c71099acSThomas Graf }
2125c71099acSThomas Graf 
2126b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net,
2127b75cc8f9SDavid Ahern 					     struct fib6_table *table,
2128b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
2129b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2130b75cc8f9SDavid Ahern 					     int flags)
2131c71099acSThomas Graf {
2132b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2133c71099acSThomas Graf }
2134c71099acSThomas Graf 
21356f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
21366f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags)
2137c71099acSThomas Graf {
2138d46a9d67SDavid Ahern 	bool any_src;
2139c71099acSThomas Graf 
21403ede0bbcSRobert Shearman 	if (ipv6_addr_type(&fl6->daddr) &
21413ede0bbcSRobert Shearman 	    (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
21424c1feac5SDavid Ahern 		struct dst_entry *dst;
21434c1feac5SDavid Ahern 
21444c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
2145ca254490SDavid Ahern 		if (dst)
2146ca254490SDavid Ahern 			return dst;
21474c1feac5SDavid Ahern 	}
2148ca254490SDavid Ahern 
21491fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
21504dc27d1cSDavid McCullough 
2151d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
2152741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2153d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
215477d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
2155c71099acSThomas Graf 
2156d46a9d67SDavid Ahern 	if (!any_src)
2157adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
21580c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
21590c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2160adaa70bbSThomas Graf 
2161b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
21621da177e4SLinus Torvalds }
21636f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
21641da177e4SLinus Torvalds 
21652774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
216614e50e57SDavid S. Miller {
21675c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
21681dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
216914e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
217014e50e57SDavid S. Miller 
21711dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
217262cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
217314e50e57SDavid S. Miller 	if (rt) {
21740a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
217581eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
21760a1f5962SMartin KaFai Lau 
2177d8d1f30bSChangli Gao 		new = &rt->dst;
217814e50e57SDavid S. Miller 		new->__use = 1;
2179352e512cSHerbert Xu 		new->input = dst_discard;
2180ede2059dSEric W. Biederman 		new->output = dst_discard_out;
218114e50e57SDavid S. Miller 
2182defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
218314e50e57SDavid S. Miller 
21841dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
21854e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
21860a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
218714e50e57SDavid S. Miller 
218814e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
218914e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
219014e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
219114e50e57SDavid S. Miller #endif
219214e50e57SDavid S. Miller 	}
219314e50e57SDavid S. Miller 
219469ead7afSDavid S. Miller 	dst_release(dst_orig);
219569ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
219614e50e57SDavid S. Miller }
219714e50e57SDavid S. Miller 
21981da177e4SLinus Torvalds /*
21991da177e4SLinus Torvalds  *	Destination cache support functions
22001da177e4SLinus Torvalds  */
22011da177e4SLinus Torvalds 
22028d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie)
22033da59bd9SMartin KaFai Lau {
220436143645SSteffen Klassert 	u32 rt_cookie = 0;
2205c5cff856SWei Wang 
22068ae86971SDavid Ahern 	if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
220793531c67SDavid Ahern 		return false;
220893531c67SDavid Ahern 
220993531c67SDavid Ahern 	if (fib6_check_expired(f6i))
221093531c67SDavid Ahern 		return false;
221193531c67SDavid Ahern 
221293531c67SDavid Ahern 	return true;
221393531c67SDavid Ahern }
221493531c67SDavid Ahern 
2215a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt,
2216a68886a6SDavid Ahern 				   struct fib6_info *from,
2217a68886a6SDavid Ahern 				   u32 cookie)
22183da59bd9SMartin KaFai Lau {
2219c5cff856SWei Wang 	u32 rt_cookie = 0;
2220c5cff856SWei Wang 
2221a68886a6SDavid Ahern 	if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
222293531c67SDavid Ahern 	    rt_cookie != cookie)
22233da59bd9SMartin KaFai Lau 		return NULL;
22243da59bd9SMartin KaFai Lau 
22253da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
22263da59bd9SMartin KaFai Lau 		return NULL;
22273da59bd9SMartin KaFai Lau 
22283da59bd9SMartin KaFai Lau 	return &rt->dst;
22293da59bd9SMartin KaFai Lau }
22303da59bd9SMartin KaFai Lau 
2231a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2232a68886a6SDavid Ahern 					    struct fib6_info *from,
2233a68886a6SDavid Ahern 					    u32 cookie)
22343da59bd9SMartin KaFai Lau {
22355973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
22365973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2237a68886a6SDavid Ahern 	    fib6_check(from, cookie))
22383da59bd9SMartin KaFai Lau 		return &rt->dst;
22393da59bd9SMartin KaFai Lau 	else
22403da59bd9SMartin KaFai Lau 		return NULL;
22413da59bd9SMartin KaFai Lau }
22423da59bd9SMartin KaFai Lau 
22431da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
22441da177e4SLinus Torvalds {
2245a87b7dc9SDavid Ahern 	struct dst_entry *dst_ret;
2246a68886a6SDavid Ahern 	struct fib6_info *from;
22471da177e4SLinus Torvalds 	struct rt6_info *rt;
22481da177e4SLinus Torvalds 
2249a87b7dc9SDavid Ahern 	rt = container_of(dst, struct rt6_info, dst);
2250a87b7dc9SDavid Ahern 
2251a87b7dc9SDavid Ahern 	rcu_read_lock();
22521da177e4SLinus Torvalds 
22536f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
22546f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
22556f3118b5SNicolas Dichtel 	 * into this function always.
22566f3118b5SNicolas Dichtel 	 */
2257e3bc10bdSHannes Frederic Sowa 
2258a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
22594b32b5adSMartin KaFai Lau 
2260a68886a6SDavid Ahern 	if (from && (rt->rt6i_flags & RTF_PCPU ||
2261a68886a6SDavid Ahern 	    unlikely(!list_empty(&rt->rt6i_uncached))))
2262a68886a6SDavid Ahern 		dst_ret = rt6_dst_from_check(rt, from, cookie);
22633da59bd9SMartin KaFai Lau 	else
2264a68886a6SDavid Ahern 		dst_ret = rt6_check(rt, from, cookie);
2265a87b7dc9SDavid Ahern 
2266a87b7dc9SDavid Ahern 	rcu_read_unlock();
2267a87b7dc9SDavid Ahern 
2268a87b7dc9SDavid Ahern 	return dst_ret;
22691da177e4SLinus Torvalds }
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
22721da177e4SLinus Torvalds {
22731da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds 	if (rt) {
227654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
2277c3c14da0SDavid Ahern 			rcu_read_lock();
227854c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
227993531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
228054c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
22811da177e4SLinus Torvalds 			}
2282c3c14da0SDavid Ahern 			rcu_read_unlock();
228354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
228454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
228554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
228654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
228754c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
228854c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
22891da177e4SLinus Torvalds }
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
22921da177e4SLinus Torvalds {
22931da177e4SLinus Torvalds 	struct rt6_info *rt;
22941da177e4SLinus Torvalds 
22953ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
22961da177e4SLinus Torvalds 
2297adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
22981da177e4SLinus Torvalds 	if (rt) {
22998a14e46fSDavid Ahern 		rcu_read_lock();
23001eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
230193531c67SDavid Ahern 			rt6_remove_exception_rt(rt);
2302c5cff856SWei Wang 		} else {
2303a68886a6SDavid Ahern 			struct fib6_info *from;
2304c5cff856SWei Wang 			struct fib6_node *fn;
2305c5cff856SWei Wang 
2306a68886a6SDavid Ahern 			from = rcu_dereference(rt->from);
2307a68886a6SDavid Ahern 			if (from) {
2308a68886a6SDavid Ahern 				fn = rcu_dereference(from->fib6_node);
2309c5cff856SWei Wang 				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2310c5cff856SWei Wang 					fn->fn_sernum = -1;
2311a68886a6SDavid Ahern 			}
23121da177e4SLinus Torvalds 		}
23131da177e4SLinus Torvalds 		rcu_read_unlock();
23141da177e4SLinus Torvalds 	}
23151da177e4SLinus Torvalds }
23161da177e4SLinus Torvalds 
23176a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout)
23186a3e030fSDavid Ahern {
2319a68886a6SDavid Ahern 	if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2320a68886a6SDavid Ahern 		struct fib6_info *from;
2321a68886a6SDavid Ahern 
2322a68886a6SDavid Ahern 		rcu_read_lock();
2323a68886a6SDavid Ahern 		from = rcu_dereference(rt0->from);
2324a68886a6SDavid Ahern 		if (from)
2325a68886a6SDavid Ahern 			rt0->dst.expires = from->expires;
2326a68886a6SDavid Ahern 		rcu_read_unlock();
2327a68886a6SDavid Ahern 	}
23286a3e030fSDavid Ahern 
23296a3e030fSDavid Ahern 	dst_set_expires(&rt0->dst, timeout);
23306a3e030fSDavid Ahern 	rt0->rt6i_flags |= RTF_EXPIRES;
23316700c270SDavid S. Miller }
23321da177e4SLinus Torvalds 
233345e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
233445e4fd26SMartin KaFai Lau {
233545e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
233645e4fd26SMartin KaFai Lau 
2337d4ead6b3SDavid Ahern 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
233845e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
233945e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
234045e4fd26SMartin KaFai Lau }
234145e4fd26SMartin KaFai Lau 
23420d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
23430d3f6d29SMartin KaFai Lau {
23440d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
23451490ed2aSPaolo Abeni 		(rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
23460d3f6d29SMartin KaFai Lau }
23470d3f6d29SMartin KaFai Lau 
234845e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
234945e4fd26SMartin KaFai Lau 				 const struct ipv6hdr *iph, u32 mtu)
23501da177e4SLinus Torvalds {
23510dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
23521da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
23531da177e4SLinus Torvalds 
235419bda36cSXin Long 	if (dst_metric_locked(dst, RTAX_MTU))
235519bda36cSXin Long 		return;
235619bda36cSXin Long 
235745e4fd26SMartin KaFai Lau 	if (iph) {
235845e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
235945e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
236045e4fd26SMartin KaFai Lau 	} else if (sk) {
236145e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
236245e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
236345e4fd26SMartin KaFai Lau 	} else {
23640dec879fSJulian Anastasov 		daddr = NULL;
23650dec879fSJulian Anastasov 		saddr = NULL;
23661da177e4SLinus Torvalds 	}
23670dec879fSJulian Anastasov 	dst_confirm_neigh(dst, daddr);
23680dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
23690dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
23700dec879fSJulian Anastasov 		return;
23710dec879fSJulian Anastasov 
23720dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
23730dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
23742b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
23752b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
23762b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
23770dec879fSJulian Anastasov 	} else if (daddr) {
237885bd05deSDavid Ahern 		struct fib6_result res = {};
23790dec879fSJulian Anastasov 		struct rt6_info *nrt6;
23800dec879fSJulian Anastasov 
23814d85cd0cSDavid Ahern 		rcu_read_lock();
238285bd05deSDavid Ahern 		res.f6i = rcu_dereference(rt6->from);
238385bd05deSDavid Ahern 		if (!res.f6i) {
23849c69a132SJonathan Lemon 			rcu_read_unlock();
23859c69a132SJonathan Lemon 			return;
23869c69a132SJonathan Lemon 		}
238785bd05deSDavid Ahern 		res.nh = &res.f6i->fib6_nh;
23887d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
23897d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
23907d21fec9SDavid Ahern 
239185bd05deSDavid Ahern 		nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
239245e4fd26SMartin KaFai Lau 		if (nrt6) {
239345e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
23945012f0a5SDavid Ahern 			if (rt6_insert_exception(nrt6, &res))
23952b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
239645e4fd26SMartin KaFai Lau 		}
2397a68886a6SDavid Ahern 		rcu_read_unlock();
239845e4fd26SMartin KaFai Lau 	}
239945e4fd26SMartin KaFai Lau }
240045e4fd26SMartin KaFai Lau 
240145e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
240245e4fd26SMartin KaFai Lau 			       struct sk_buff *skb, u32 mtu)
240345e4fd26SMartin KaFai Lau {
240445e4fd26SMartin KaFai Lau 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
24051da177e4SLinus Torvalds }
24061da177e4SLinus Torvalds 
240742ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2408e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
240981aded24SDavid S. Miller {
241081aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
241181aded24SDavid S. Miller 	struct dst_entry *dst;
2412dc92095dSMaciej Żenczykowski 	struct flowi6 fl6 = {
2413dc92095dSMaciej Żenczykowski 		.flowi6_oif = oif,
2414dc92095dSMaciej Żenczykowski 		.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2415dc92095dSMaciej Żenczykowski 		.daddr = iph->daddr,
2416dc92095dSMaciej Żenczykowski 		.saddr = iph->saddr,
2417dc92095dSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
2418dc92095dSMaciej Żenczykowski 		.flowi6_uid = uid,
2419dc92095dSMaciej Żenczykowski 	};
242081aded24SDavid S. Miller 
242181aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
242281aded24SDavid S. Miller 	if (!dst->error)
242345e4fd26SMartin KaFai Lau 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
242481aded24SDavid S. Miller 	dst_release(dst);
242581aded24SDavid S. Miller }
242681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
242781aded24SDavid S. Miller 
242881aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
242981aded24SDavid S. Miller {
24307ddacfa5SDavid Ahern 	int oif = sk->sk_bound_dev_if;
243133c162a9SMartin KaFai Lau 	struct dst_entry *dst;
243233c162a9SMartin KaFai Lau 
24337ddacfa5SDavid Ahern 	if (!oif && skb->dev)
24347ddacfa5SDavid Ahern 		oif = l3mdev_master_ifindex(skb->dev);
24357ddacfa5SDavid Ahern 
24367ddacfa5SDavid Ahern 	ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
243733c162a9SMartin KaFai Lau 
243833c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
243933c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
244033c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
244133c162a9SMartin KaFai Lau 		return;
244233c162a9SMartin KaFai Lau 
244333c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
244433c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
244533c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
244633c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
244781aded24SDavid S. Miller }
244881aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
244981aded24SDavid S. Miller 
24507d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
24517d6850f7SAlexey Kodanev 			   const struct flowi6 *fl6)
24527d6850f7SAlexey Kodanev {
24537d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
24547d6850f7SAlexey Kodanev 	struct ipv6_pinfo *np = inet6_sk(sk);
24557d6850f7SAlexey Kodanev #endif
24567d6850f7SAlexey Kodanev 
24577d6850f7SAlexey Kodanev 	ip6_dst_store(sk, dst,
24587d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
24597d6850f7SAlexey Kodanev 		      &sk->sk_v6_daddr : NULL,
24607d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
24617d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
24627d6850f7SAlexey Kodanev 		      &np->saddr :
24637d6850f7SAlexey Kodanev #endif
24647d6850f7SAlexey Kodanev 		      NULL);
24657d6850f7SAlexey Kodanev }
24667d6850f7SAlexey Kodanev 
24679b6b35abSDavid Ahern static bool ip6_redirect_nh_match(const struct fib6_result *res,
24680b34eb00SDavid Ahern 				  struct flowi6 *fl6,
24690b34eb00SDavid Ahern 				  const struct in6_addr *gw,
24700b34eb00SDavid Ahern 				  struct rt6_info **ret)
24710b34eb00SDavid Ahern {
24729b6b35abSDavid Ahern 	const struct fib6_nh *nh = res->nh;
24739b6b35abSDavid Ahern 
24740b34eb00SDavid Ahern 	if (nh->fib_nh_flags & RTNH_F_DEAD || !nh->fib_nh_gw_family ||
24750b34eb00SDavid Ahern 	    fl6->flowi6_oif != nh->fib_nh_dev->ifindex)
24760b34eb00SDavid Ahern 		return false;
24770b34eb00SDavid Ahern 
24780b34eb00SDavid Ahern 	/* rt_cache's gateway might be different from its 'parent'
24790b34eb00SDavid Ahern 	 * in the case of an ip redirect.
24800b34eb00SDavid Ahern 	 * So we keep searching in the exception table if the gateway
24810b34eb00SDavid Ahern 	 * is different.
24820b34eb00SDavid Ahern 	 */
24830b34eb00SDavid Ahern 	if (!ipv6_addr_equal(gw, &nh->fib_nh_gw6)) {
24840b34eb00SDavid Ahern 		struct rt6_info *rt_cache;
24850b34eb00SDavid Ahern 
24869b6b35abSDavid Ahern 		rt_cache = rt6_find_cached_rt(res, &fl6->daddr, &fl6->saddr);
24870b34eb00SDavid Ahern 		if (rt_cache &&
24880b34eb00SDavid Ahern 		    ipv6_addr_equal(gw, &rt_cache->rt6i_gateway)) {
24890b34eb00SDavid Ahern 			*ret = rt_cache;
24900b34eb00SDavid Ahern 			return true;
24910b34eb00SDavid Ahern 		}
24920b34eb00SDavid Ahern 		return false;
24930b34eb00SDavid Ahern 	}
24940b34eb00SDavid Ahern 	return true;
24950b34eb00SDavid Ahern }
24960b34eb00SDavid Ahern 
2497b55b76b2SDuan Jiong /* Handle redirects */
2498b55b76b2SDuan Jiong struct ip6rd_flowi {
2499b55b76b2SDuan Jiong 	struct flowi6 fl6;
2500b55b76b2SDuan Jiong 	struct in6_addr gateway;
2501b55b76b2SDuan Jiong };
2502b55b76b2SDuan Jiong 
2503b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
2504b55b76b2SDuan Jiong 					     struct fib6_table *table,
2505b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2506b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2507b55b76b2SDuan Jiong 					     int flags)
2508b55b76b2SDuan Jiong {
2509b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
25100b34eb00SDavid Ahern 	struct rt6_info *ret = NULL;
25119b6b35abSDavid Ahern 	struct fib6_result res = {};
25128d1c802bSDavid Ahern 	struct fib6_info *rt;
2513b55b76b2SDuan Jiong 	struct fib6_node *fn;
2514b55b76b2SDuan Jiong 
2515b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
251667c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2517b55b76b2SDuan Jiong 	 *
2518b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2519b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2520b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2521b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2522b55b76b2SDuan Jiong 	 * routes.
2523b55b76b2SDuan Jiong 	 */
2524b55b76b2SDuan Jiong 
252566f5d6ceSWei Wang 	rcu_read_lock();
25266454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2527b55b76b2SDuan Jiong restart:
252866f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
25299b6b35abSDavid Ahern 		res.f6i = rt;
25309b6b35abSDavid Ahern 		res.nh = &rt->fib6_nh;
25319b6b35abSDavid Ahern 
253214895687SDavid Ahern 		if (fib6_check_expired(rt))
2533b55b76b2SDuan Jiong 			continue;
253493c2fb25SDavid Ahern 		if (rt->fib6_flags & RTF_REJECT)
2535b55b76b2SDuan Jiong 			break;
25369b6b35abSDavid Ahern 		if (ip6_redirect_nh_match(&res, fl6, &rdfl->gateway, &ret))
25370b34eb00SDavid Ahern 			goto out;
2538b55b76b2SDuan Jiong 	}
2539b55b76b2SDuan Jiong 
2540b55b76b2SDuan Jiong 	if (!rt)
2541421842edSDavid Ahern 		rt = net->ipv6.fib6_null_entry;
254293c2fb25SDavid Ahern 	else if (rt->fib6_flags & RTF_REJECT) {
254323fb93a4SDavid Ahern 		ret = net->ipv6.ip6_null_entry;
2544b0a1ba59SMartin KaFai Lau 		goto out;
2545b0a1ba59SMartin KaFai Lau 	}
2546b0a1ba59SMartin KaFai Lau 
2547421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
2548a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2549a3c00e46SMartin KaFai Lau 		if (fn)
2550a3c00e46SMartin KaFai Lau 			goto restart;
2551b55b76b2SDuan Jiong 	}
2552a3c00e46SMartin KaFai Lau 
25539b6b35abSDavid Ahern 	res.f6i = rt;
25549b6b35abSDavid Ahern 	res.nh = &rt->fib6_nh;
2555b0a1ba59SMartin KaFai Lau out:
25567d21fec9SDavid Ahern 	if (ret) {
255710585b43SDavid Ahern 		ip6_hold_safe(net, &ret);
25587d21fec9SDavid Ahern 	} else {
25597d21fec9SDavid Ahern 		res.fib6_flags = res.f6i->fib6_flags;
25607d21fec9SDavid Ahern 		res.fib6_type = res.f6i->fib6_type;
25619b6b35abSDavid Ahern 		ret = ip6_create_rt_rcu(&res);
25627d21fec9SDavid Ahern 	}
2563b55b76b2SDuan Jiong 
256466f5d6ceSWei Wang 	rcu_read_unlock();
2565b55b76b2SDuan Jiong 
25668ff2e5b2SDavid Ahern 	trace_fib6_table_lookup(net, &res, table, fl6);
256723fb93a4SDavid Ahern 	return ret;
2568b55b76b2SDuan Jiong };
2569b55b76b2SDuan Jiong 
2570b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
2571b55b76b2SDuan Jiong 					    const struct flowi6 *fl6,
2572b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2573b55b76b2SDuan Jiong 					    const struct in6_addr *gateway)
2574b55b76b2SDuan Jiong {
2575b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2576b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
2577b55b76b2SDuan Jiong 
2578b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
2579b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
2580b55b76b2SDuan Jiong 
2581b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, &rdfl.fl6, skb,
2582b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
2583b55b76b2SDuan Jiong }
2584b55b76b2SDuan Jiong 
2585e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2586e2d118a1SLorenzo Colitti 		  kuid_t uid)
25873a5ad2eeSDavid S. Miller {
25883a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
25893a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
25901f7f10acSMaciej Żenczykowski 	struct flowi6 fl6 = {
25911f7f10acSMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
25921f7f10acSMaciej Żenczykowski 		.flowi6_oif = oif,
25931f7f10acSMaciej Żenczykowski 		.flowi6_mark = mark,
25941f7f10acSMaciej Żenczykowski 		.daddr = iph->daddr,
25951f7f10acSMaciej Żenczykowski 		.saddr = iph->saddr,
25961f7f10acSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
25971f7f10acSMaciej Żenczykowski 		.flowi6_uid = uid,
25981f7f10acSMaciej Żenczykowski 	};
25993a5ad2eeSDavid S. Miller 
2600b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
26016700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
26023a5ad2eeSDavid S. Miller 	dst_release(dst);
26033a5ad2eeSDavid S. Miller }
26043a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
26053a5ad2eeSDavid S. Miller 
2606d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
2607c92a59ecSDuan Jiong {
2608c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2609c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2610c92a59ecSDuan Jiong 	struct dst_entry *dst;
26110b26fb17SMaciej Żenczykowski 	struct flowi6 fl6 = {
26120b26fb17SMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
26130b26fb17SMaciej Żenczykowski 		.flowi6_oif = oif,
26140b26fb17SMaciej Żenczykowski 		.daddr = msg->dest,
26150b26fb17SMaciej Żenczykowski 		.saddr = iph->daddr,
26160b26fb17SMaciej Żenczykowski 		.flowi6_uid = sock_net_uid(net, NULL),
26170b26fb17SMaciej Żenczykowski 	};
2618c92a59ecSDuan Jiong 
2619b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
2620c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
2621c92a59ecSDuan Jiong 	dst_release(dst);
2622c92a59ecSDuan Jiong }
2623c92a59ecSDuan Jiong 
26243a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
26253a5ad2eeSDavid S. Miller {
2626e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2627e2d118a1SLorenzo Colitti 		     sk->sk_uid);
26283a5ad2eeSDavid S. Miller }
26293a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
26303a5ad2eeSDavid S. Miller 
26310dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
26321da177e4SLinus Torvalds {
26330dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
26340dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
26350dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
26360dbaee3bSDavid S. Miller 
26371da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
26381da177e4SLinus Torvalds 
26395578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
26405578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
26411da177e4SLinus Torvalds 
26421da177e4SLinus Torvalds 	/*
26431da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
26441da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
26451da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
26461da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
26471da177e4SLinus Torvalds 	 */
26481da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
26491da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
26501da177e4SLinus Torvalds 	return mtu;
26511da177e4SLinus Torvalds }
26521da177e4SLinus Torvalds 
2653ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
2654d33e4553SDavid S. Miller {
2655d33e4553SDavid S. Miller 	struct inet6_dev *idev;
2656d4ead6b3SDavid Ahern 	unsigned int mtu;
2657618f9bc7SSteffen Klassert 
26584b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
26594b32b5adSMartin KaFai Lau 	if (mtu)
26604b32b5adSMartin KaFai Lau 		goto out;
26614b32b5adSMartin KaFai Lau 
2662618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
2663d33e4553SDavid S. Miller 
2664d33e4553SDavid S. Miller 	rcu_read_lock();
2665d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
2666d33e4553SDavid S. Miller 	if (idev)
2667d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
2668d33e4553SDavid S. Miller 	rcu_read_unlock();
2669d33e4553SDavid S. Miller 
267030f78d8eSEric Dumazet out:
267114972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
267214972cbdSRoopa Prabhu 
267314972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
2674d33e4553SDavid S. Miller }
2675d33e4553SDavid S. Miller 
2676901731b8SDavid Ahern /* MTU selection:
2677901731b8SDavid Ahern  * 1. mtu on route is locked - use it
2678901731b8SDavid Ahern  * 2. mtu from nexthop exception
2679901731b8SDavid Ahern  * 3. mtu from egress device
2680901731b8SDavid Ahern  *
2681901731b8SDavid Ahern  * based on ip6_dst_mtu_forward and exception logic of
2682901731b8SDavid Ahern  * rt6_find_cached_rt; called with rcu_read_lock
2683901731b8SDavid Ahern  */
2684b748f260SDavid Ahern u32 ip6_mtu_from_fib6(const struct fib6_result *res,
2685b748f260SDavid Ahern 		      const struct in6_addr *daddr,
2686b748f260SDavid Ahern 		      const struct in6_addr *saddr)
2687901731b8SDavid Ahern {
2688b748f260SDavid Ahern 	const struct fib6_nh *nh = res->nh;
2689b748f260SDavid Ahern 	struct fib6_info *f6i = res->f6i;
2690901731b8SDavid Ahern 	struct inet6_dev *idev;
2691*510e2cedSWei Wang 	struct rt6_info *rt;
2692901731b8SDavid Ahern 	u32 mtu = 0;
2693901731b8SDavid Ahern 
2694901731b8SDavid Ahern 	if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
2695901731b8SDavid Ahern 		mtu = f6i->fib6_pmtu;
2696901731b8SDavid Ahern 		if (mtu)
2697901731b8SDavid Ahern 			goto out;
2698901731b8SDavid Ahern 	}
2699901731b8SDavid Ahern 
2700*510e2cedSWei Wang 	rt = rt6_find_cached_rt(res, daddr, saddr);
2701*510e2cedSWei Wang 	if (unlikely(rt)) {
2702*510e2cedSWei Wang 		mtu = dst_metric_raw(&rt->dst, RTAX_MTU);
2703*510e2cedSWei Wang 	} else {
2704b748f260SDavid Ahern 		struct net_device *dev = nh->fib_nh_dev;
2705901731b8SDavid Ahern 
2706901731b8SDavid Ahern 		mtu = IPV6_MIN_MTU;
2707901731b8SDavid Ahern 		idev = __in6_dev_get(dev);
2708901731b8SDavid Ahern 		if (idev && idev->cnf.mtu6 > mtu)
2709901731b8SDavid Ahern 			mtu = idev->cnf.mtu6;
2710901731b8SDavid Ahern 	}
2711901731b8SDavid Ahern 
2712901731b8SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2713901731b8SDavid Ahern out:
2714b748f260SDavid Ahern 	return mtu - lwtunnel_headroom(nh->fib_nh_lws, mtu);
2715901731b8SDavid Ahern }
2716901731b8SDavid Ahern 
27173b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
271887a11578SDavid S. Miller 				  struct flowi6 *fl6)
27191da177e4SLinus Torvalds {
272087a11578SDavid S. Miller 	struct dst_entry *dst;
27211da177e4SLinus Torvalds 	struct rt6_info *rt;
27221da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
2723c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
27241da177e4SLinus Torvalds 
272538308473SDavid S. Miller 	if (unlikely(!idev))
2726122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
27271da177e4SLinus Torvalds 
2728ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
272938308473SDavid S. Miller 	if (unlikely(!rt)) {
27301da177e4SLinus Torvalds 		in6_dev_put(idev);
273187a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
27321da177e4SLinus Torvalds 		goto out;
27331da177e4SLinus Torvalds 	}
27341da177e4SLinus Torvalds 
27358e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
2736588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
27378e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
2738550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
273987a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
27408e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
27418e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
274214edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
27431da177e4SLinus Torvalds 
27444c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
2745587fea74SWei Wang 	 * do proper release of the net_device
2746587fea74SWei Wang 	 */
2747587fea74SWei Wang 	rt6_uncached_list_add(rt);
274881eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
27491da177e4SLinus Torvalds 
275087a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
275187a11578SDavid S. Miller 
27521da177e4SLinus Torvalds out:
275387a11578SDavid S. Miller 	return dst;
27541da177e4SLinus Torvalds }
27551da177e4SLinus Torvalds 
2756569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
27571da177e4SLinus Torvalds {
275886393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
27597019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
27607019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
27617019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
27627019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
27637019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
2764fc66f95cSEric Dumazet 	int entries;
27651da177e4SLinus Torvalds 
2766fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
276749a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
2768fc66f95cSEric Dumazet 	    entries <= rt_max_size)
27691da177e4SLinus Torvalds 		goto out;
27701da177e4SLinus Torvalds 
27716891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
277214956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
2773fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
2774fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
27757019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
27761da177e4SLinus Torvalds out:
27777019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
2778fc66f95cSEric Dumazet 	return entries > rt_max_size;
27791da177e4SLinus Torvalds }
27801da177e4SLinus Torvalds 
27818c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net,
27828c14586fSDavid Ahern 					    struct fib6_config *cfg,
2783f4797b33SDavid Ahern 					    const struct in6_addr *gw_addr,
2784f4797b33SDavid Ahern 					    u32 tbid, int flags)
27858c14586fSDavid Ahern {
27868c14586fSDavid Ahern 	struct flowi6 fl6 = {
27878c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
27888c14586fSDavid Ahern 		.daddr = *gw_addr,
27898c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
27908c14586fSDavid Ahern 	};
27918c14586fSDavid Ahern 	struct fib6_table *table;
27928c14586fSDavid Ahern 	struct rt6_info *rt;
27938c14586fSDavid Ahern 
2794f4797b33SDavid Ahern 	table = fib6_get_table(net, tbid);
27958c14586fSDavid Ahern 	if (!table)
27968c14586fSDavid Ahern 		return NULL;
27978c14586fSDavid Ahern 
27988c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
27998c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
28008c14586fSDavid Ahern 
2801f4797b33SDavid Ahern 	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
2802b75cc8f9SDavid Ahern 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
28038c14586fSDavid Ahern 
28048c14586fSDavid Ahern 	/* if table lookup failed, fall back to full lookup */
28058c14586fSDavid Ahern 	if (rt == net->ipv6.ip6_null_entry) {
28068c14586fSDavid Ahern 		ip6_rt_put(rt);
28078c14586fSDavid Ahern 		rt = NULL;
28088c14586fSDavid Ahern 	}
28098c14586fSDavid Ahern 
28108c14586fSDavid Ahern 	return rt;
28118c14586fSDavid Ahern }
28128c14586fSDavid Ahern 
2813fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net,
2814fc1e64e1SDavid Ahern 				     struct fib6_config *cfg,
28159fbb704cSDavid Ahern 				     const struct net_device *dev,
2816fc1e64e1SDavid Ahern 				     struct netlink_ext_ack *extack)
2817fc1e64e1SDavid Ahern {
281844750f84SDavid Ahern 	u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
2819fc1e64e1SDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
2820fc1e64e1SDavid Ahern 	u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2821bf1dc8baSPaolo Abeni 	struct fib6_info *from;
2822fc1e64e1SDavid Ahern 	struct rt6_info *grt;
2823fc1e64e1SDavid Ahern 	int err;
2824fc1e64e1SDavid Ahern 
2825fc1e64e1SDavid Ahern 	err = 0;
2826fc1e64e1SDavid Ahern 	grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2827fc1e64e1SDavid Ahern 	if (grt) {
2828bf1dc8baSPaolo Abeni 		rcu_read_lock();
2829bf1dc8baSPaolo Abeni 		from = rcu_dereference(grt->from);
283058e354c0SDavid Ahern 		if (!grt->dst.error &&
28314ed591c8SDavid Ahern 		    /* ignore match if it is the default route */
2832bf1dc8baSPaolo Abeni 		    from && !ipv6_addr_any(&from->fib6_dst.addr) &&
283358e354c0SDavid Ahern 		    (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
283444750f84SDavid Ahern 			NL_SET_ERR_MSG(extack,
283544750f84SDavid Ahern 				       "Nexthop has invalid gateway or device mismatch");
2836fc1e64e1SDavid Ahern 			err = -EINVAL;
2837fc1e64e1SDavid Ahern 		}
2838bf1dc8baSPaolo Abeni 		rcu_read_unlock();
2839fc1e64e1SDavid Ahern 
2840fc1e64e1SDavid Ahern 		ip6_rt_put(grt);
2841fc1e64e1SDavid Ahern 	}
2842fc1e64e1SDavid Ahern 
2843fc1e64e1SDavid Ahern 	return err;
2844fc1e64e1SDavid Ahern }
2845fc1e64e1SDavid Ahern 
28461edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net,
28471edce99fSDavid Ahern 			      struct fib6_config *cfg,
28481edce99fSDavid Ahern 			      struct net_device **_dev,
28491edce99fSDavid Ahern 			      struct inet6_dev **idev)
28501edce99fSDavid Ahern {
28511edce99fSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
28521edce99fSDavid Ahern 	struct net_device *dev = _dev ? *_dev : NULL;
28531edce99fSDavid Ahern 	struct rt6_info *grt = NULL;
28541edce99fSDavid Ahern 	int err = -EHOSTUNREACH;
28551edce99fSDavid Ahern 
28561edce99fSDavid Ahern 	if (cfg->fc_table) {
2857f4797b33SDavid Ahern 		int flags = RT6_LOOKUP_F_IFACE;
2858f4797b33SDavid Ahern 
2859f4797b33SDavid Ahern 		grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2860f4797b33SDavid Ahern 					  cfg->fc_table, flags);
28611edce99fSDavid Ahern 		if (grt) {
28621edce99fSDavid Ahern 			if (grt->rt6i_flags & RTF_GATEWAY ||
28631edce99fSDavid Ahern 			    (dev && dev != grt->dst.dev)) {
28641edce99fSDavid Ahern 				ip6_rt_put(grt);
28651edce99fSDavid Ahern 				grt = NULL;
28661edce99fSDavid Ahern 			}
28671edce99fSDavid Ahern 		}
28681edce99fSDavid Ahern 	}
28691edce99fSDavid Ahern 
28701edce99fSDavid Ahern 	if (!grt)
2871b75cc8f9SDavid Ahern 		grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
28721edce99fSDavid Ahern 
28731edce99fSDavid Ahern 	if (!grt)
28741edce99fSDavid Ahern 		goto out;
28751edce99fSDavid Ahern 
28761edce99fSDavid Ahern 	if (dev) {
28771edce99fSDavid Ahern 		if (dev != grt->dst.dev) {
28781edce99fSDavid Ahern 			ip6_rt_put(grt);
28791edce99fSDavid Ahern 			goto out;
28801edce99fSDavid Ahern 		}
28811edce99fSDavid Ahern 	} else {
28821edce99fSDavid Ahern 		*_dev = dev = grt->dst.dev;
28831edce99fSDavid Ahern 		*idev = grt->rt6i_idev;
28841edce99fSDavid Ahern 		dev_hold(dev);
28851edce99fSDavid Ahern 		in6_dev_hold(grt->rt6i_idev);
28861edce99fSDavid Ahern 	}
28871edce99fSDavid Ahern 
28881edce99fSDavid Ahern 	if (!(grt->rt6i_flags & RTF_GATEWAY))
28891edce99fSDavid Ahern 		err = 0;
28901edce99fSDavid Ahern 
28911edce99fSDavid Ahern 	ip6_rt_put(grt);
28921edce99fSDavid Ahern 
28931edce99fSDavid Ahern out:
28941edce99fSDavid Ahern 	return err;
28951edce99fSDavid Ahern }
28961edce99fSDavid Ahern 
28979fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
28989fbb704cSDavid Ahern 			   struct net_device **_dev, struct inet6_dev **idev,
28999fbb704cSDavid Ahern 			   struct netlink_ext_ack *extack)
29009fbb704cSDavid Ahern {
29019fbb704cSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
29029fbb704cSDavid Ahern 	int gwa_type = ipv6_addr_type(gw_addr);
2903232378e8SDavid Ahern 	bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
29049fbb704cSDavid Ahern 	const struct net_device *dev = *_dev;
2905232378e8SDavid Ahern 	bool need_addr_check = !dev;
29069fbb704cSDavid Ahern 	int err = -EINVAL;
29079fbb704cSDavid Ahern 
29089fbb704cSDavid Ahern 	/* if gw_addr is local we will fail to detect this in case
29099fbb704cSDavid Ahern 	 * address is still TENTATIVE (DAD in progress). rt6_lookup()
29109fbb704cSDavid Ahern 	 * will return already-added prefix route via interface that
29119fbb704cSDavid Ahern 	 * prefix route was assigned to, which might be non-loopback.
29129fbb704cSDavid Ahern 	 */
2913232378e8SDavid Ahern 	if (dev &&
2914232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2915232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
29169fbb704cSDavid Ahern 		goto out;
29179fbb704cSDavid Ahern 	}
29189fbb704cSDavid Ahern 
29199fbb704cSDavid Ahern 	if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
29209fbb704cSDavid Ahern 		/* IPv6 strictly inhibits using not link-local
29219fbb704cSDavid Ahern 		 * addresses as nexthop address.
29229fbb704cSDavid Ahern 		 * Otherwise, router will not able to send redirects.
29239fbb704cSDavid Ahern 		 * It is very good, but in some (rare!) circumstances
29249fbb704cSDavid Ahern 		 * (SIT, PtP, NBMA NOARP links) it is handy to allow
29259fbb704cSDavid Ahern 		 * some exceptions. --ANK
29269fbb704cSDavid Ahern 		 * We allow IPv4-mapped nexthops to support RFC4798-type
29279fbb704cSDavid Ahern 		 * addressing
29289fbb704cSDavid Ahern 		 */
29299fbb704cSDavid Ahern 		if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
29309fbb704cSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
29319fbb704cSDavid Ahern 			goto out;
29329fbb704cSDavid Ahern 		}
29339fbb704cSDavid Ahern 
29349fbb704cSDavid Ahern 		if (cfg->fc_flags & RTNH_F_ONLINK)
29359fbb704cSDavid Ahern 			err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
29369fbb704cSDavid Ahern 		else
29379fbb704cSDavid Ahern 			err = ip6_route_check_nh(net, cfg, _dev, idev);
29389fbb704cSDavid Ahern 
29399fbb704cSDavid Ahern 		if (err)
29409fbb704cSDavid Ahern 			goto out;
29419fbb704cSDavid Ahern 	}
29429fbb704cSDavid Ahern 
29439fbb704cSDavid Ahern 	/* reload in case device was changed */
29449fbb704cSDavid Ahern 	dev = *_dev;
29459fbb704cSDavid Ahern 
29469fbb704cSDavid Ahern 	err = -EINVAL;
29479fbb704cSDavid Ahern 	if (!dev) {
29489fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Egress device not specified");
29499fbb704cSDavid Ahern 		goto out;
29509fbb704cSDavid Ahern 	} else if (dev->flags & IFF_LOOPBACK) {
29519fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack,
29529fbb704cSDavid Ahern 			       "Egress device can not be loopback device for this route");
29539fbb704cSDavid Ahern 		goto out;
29549fbb704cSDavid Ahern 	}
2955232378e8SDavid Ahern 
2956232378e8SDavid Ahern 	/* if we did not check gw_addr above, do so now that the
2957232378e8SDavid Ahern 	 * egress device has been resolved.
2958232378e8SDavid Ahern 	 */
2959232378e8SDavid Ahern 	if (need_addr_check &&
2960232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2961232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
2962232378e8SDavid Ahern 		goto out;
2963232378e8SDavid Ahern 	}
2964232378e8SDavid Ahern 
29659fbb704cSDavid Ahern 	err = 0;
29669fbb704cSDavid Ahern out:
29679fbb704cSDavid Ahern 	return err;
29689fbb704cSDavid Ahern }
29699fbb704cSDavid Ahern 
297083c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
297183c44251SDavid Ahern {
297283c44251SDavid Ahern 	if ((flags & RTF_REJECT) ||
297383c44251SDavid Ahern 	    (dev && (dev->flags & IFF_LOOPBACK) &&
297483c44251SDavid Ahern 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
297583c44251SDavid Ahern 	     !(flags & RTF_LOCAL)))
297683c44251SDavid Ahern 		return true;
297783c44251SDavid Ahern 
297883c44251SDavid Ahern 	return false;
297983c44251SDavid Ahern }
298083c44251SDavid Ahern 
298183c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
298283c44251SDavid Ahern 		 struct fib6_config *cfg, gfp_t gfp_flags,
298383c44251SDavid Ahern 		 struct netlink_ext_ack *extack)
298483c44251SDavid Ahern {
298583c44251SDavid Ahern 	struct net_device *dev = NULL;
298683c44251SDavid Ahern 	struct inet6_dev *idev = NULL;
298783c44251SDavid Ahern 	int addr_type;
298883c44251SDavid Ahern 	int err;
298983c44251SDavid Ahern 
2990f1741730SDavid Ahern 	fib6_nh->fib_nh_family = AF_INET6;
2991f1741730SDavid Ahern 
299283c44251SDavid Ahern 	err = -ENODEV;
299383c44251SDavid Ahern 	if (cfg->fc_ifindex) {
299483c44251SDavid Ahern 		dev = dev_get_by_index(net, cfg->fc_ifindex);
299583c44251SDavid Ahern 		if (!dev)
299683c44251SDavid Ahern 			goto out;
299783c44251SDavid Ahern 		idev = in6_dev_get(dev);
299883c44251SDavid Ahern 		if (!idev)
299983c44251SDavid Ahern 			goto out;
300083c44251SDavid Ahern 	}
300183c44251SDavid Ahern 
300283c44251SDavid Ahern 	if (cfg->fc_flags & RTNH_F_ONLINK) {
300383c44251SDavid Ahern 		if (!dev) {
300483c44251SDavid Ahern 			NL_SET_ERR_MSG(extack,
300583c44251SDavid Ahern 				       "Nexthop device required for onlink");
300683c44251SDavid Ahern 			goto out;
300783c44251SDavid Ahern 		}
300883c44251SDavid Ahern 
300983c44251SDavid Ahern 		if (!(dev->flags & IFF_UP)) {
301083c44251SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
301183c44251SDavid Ahern 			err = -ENETDOWN;
301283c44251SDavid Ahern 			goto out;
301383c44251SDavid Ahern 		}
301483c44251SDavid Ahern 
3015ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
301683c44251SDavid Ahern 	}
301783c44251SDavid Ahern 
3018ad1601aeSDavid Ahern 	fib6_nh->fib_nh_weight = 1;
301983c44251SDavid Ahern 
302083c44251SDavid Ahern 	/* We cannot add true routes via loopback here,
302183c44251SDavid Ahern 	 * they would result in kernel looping; promote them to reject routes
302283c44251SDavid Ahern 	 */
302383c44251SDavid Ahern 	addr_type = ipv6_addr_type(&cfg->fc_dst);
302483c44251SDavid Ahern 	if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
302583c44251SDavid Ahern 		/* hold loopback dev/idev if we haven't done so. */
302683c44251SDavid Ahern 		if (dev != net->loopback_dev) {
302783c44251SDavid Ahern 			if (dev) {
302883c44251SDavid Ahern 				dev_put(dev);
302983c44251SDavid Ahern 				in6_dev_put(idev);
303083c44251SDavid Ahern 			}
303183c44251SDavid Ahern 			dev = net->loopback_dev;
303283c44251SDavid Ahern 			dev_hold(dev);
303383c44251SDavid Ahern 			idev = in6_dev_get(dev);
303483c44251SDavid Ahern 			if (!idev) {
303583c44251SDavid Ahern 				err = -ENODEV;
303683c44251SDavid Ahern 				goto out;
303783c44251SDavid Ahern 			}
303883c44251SDavid Ahern 		}
303983c44251SDavid Ahern 		goto set_dev;
304083c44251SDavid Ahern 	}
304183c44251SDavid Ahern 
304283c44251SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY) {
304383c44251SDavid Ahern 		err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
304483c44251SDavid Ahern 		if (err)
304583c44251SDavid Ahern 			goto out;
304683c44251SDavid Ahern 
3047ad1601aeSDavid Ahern 		fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
3048bdf00467SDavid Ahern 		fib6_nh->fib_nh_gw_family = AF_INET6;
304983c44251SDavid Ahern 	}
305083c44251SDavid Ahern 
305183c44251SDavid Ahern 	err = -ENODEV;
305283c44251SDavid Ahern 	if (!dev)
305383c44251SDavid Ahern 		goto out;
305483c44251SDavid Ahern 
305583c44251SDavid Ahern 	if (idev->cnf.disable_ipv6) {
305683c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
305783c44251SDavid Ahern 		err = -EACCES;
305883c44251SDavid Ahern 		goto out;
305983c44251SDavid Ahern 	}
306083c44251SDavid Ahern 
306183c44251SDavid Ahern 	if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
306283c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
306383c44251SDavid Ahern 		err = -ENETDOWN;
306483c44251SDavid Ahern 		goto out;
306583c44251SDavid Ahern 	}
306683c44251SDavid Ahern 
306783c44251SDavid Ahern 	if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
306883c44251SDavid Ahern 	    !netif_carrier_ok(dev))
3069ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
307083c44251SDavid Ahern 
3071979e276eSDavid Ahern 	err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
3072979e276eSDavid Ahern 				 cfg->fc_encap_type, cfg, gfp_flags, extack);
3073979e276eSDavid Ahern 	if (err)
3074979e276eSDavid Ahern 		goto out;
307583c44251SDavid Ahern set_dev:
3076ad1601aeSDavid Ahern 	fib6_nh->fib_nh_dev = dev;
3077f1741730SDavid Ahern 	fib6_nh->fib_nh_oif = dev->ifindex;
307883c44251SDavid Ahern 	err = 0;
307983c44251SDavid Ahern out:
308083c44251SDavid Ahern 	if (idev)
308183c44251SDavid Ahern 		in6_dev_put(idev);
308283c44251SDavid Ahern 
308383c44251SDavid Ahern 	if (err) {
3084ad1601aeSDavid Ahern 		lwtstate_put(fib6_nh->fib_nh_lws);
3085ad1601aeSDavid Ahern 		fib6_nh->fib_nh_lws = NULL;
308683c44251SDavid Ahern 		if (dev)
308783c44251SDavid Ahern 			dev_put(dev);
308883c44251SDavid Ahern 	}
308983c44251SDavid Ahern 
309083c44251SDavid Ahern 	return err;
309183c44251SDavid Ahern }
309283c44251SDavid Ahern 
3093dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh)
3094dac7d0f2SDavid Ahern {
3095979e276eSDavid Ahern 	fib_nh_common_release(&fib6_nh->nh_common);
3096dac7d0f2SDavid Ahern }
3097dac7d0f2SDavid Ahern 
30988d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3099acb54e3cSDavid Ahern 					      gfp_t gfp_flags,
3100333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
31011da177e4SLinus Torvalds {
31025578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
31038d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3104c71099acSThomas Graf 	struct fib6_table *table;
31058c5b83f0SRoopa Prabhu 	int err = -EINVAL;
310683c44251SDavid Ahern 	int addr_type;
31071da177e4SLinus Torvalds 
3108557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
3109d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
3110d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3111557c44beSDavid Ahern 		goto out;
3112d5d531cbSDavid Ahern 	}
3113557c44beSDavid Ahern 
31142ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
31152ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
31162ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
31172ea2352eSWei Wang 		goto out;
31182ea2352eSWei Wang 	}
31192ea2352eSWei Wang 
3120e8478e80SDavid Ahern 	if (cfg->fc_type > RTN_MAX) {
3121e8478e80SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid route type");
3122e8478e80SDavid Ahern 		goto out;
3123e8478e80SDavid Ahern 	}
3124e8478e80SDavid Ahern 
3125d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
3126d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
31278c5b83f0SRoopa Prabhu 		goto out;
3128d5d531cbSDavid Ahern 	}
3129d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
3130d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
3131d5d531cbSDavid Ahern 		goto out;
3132d5d531cbSDavid Ahern 	}
31331da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
3134d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
3135d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
3136d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
31378c5b83f0SRoopa Prabhu 		goto out;
3138d5d531cbSDavid Ahern 	}
31391da177e4SLinus Torvalds #endif
3140fc1e64e1SDavid Ahern 
3141c71099acSThomas Graf 	err = -ENOBUFS;
314238308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
3143d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3144d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
314538308473SDavid S. Miller 		if (!table) {
3146f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3147d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
3148d71314b4SMatti Vaittinen 		}
3149d71314b4SMatti Vaittinen 	} else {
3150d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
3151d71314b4SMatti Vaittinen 	}
315238308473SDavid S. Miller 
315338308473SDavid S. Miller 	if (!table)
3154c71099acSThomas Graf 		goto out;
3155c71099acSThomas Graf 
31561da177e4SLinus Torvalds 	err = -ENOMEM;
315793531c67SDavid Ahern 	rt = fib6_info_alloc(gfp_flags);
315893531c67SDavid Ahern 	if (!rt)
31591da177e4SLinus Torvalds 		goto out;
316093531c67SDavid Ahern 
3161d7e774f3SDavid Ahern 	rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3162d7e774f3SDavid Ahern 					       extack);
3163767a2217SDavid Ahern 	if (IS_ERR(rt->fib6_metrics)) {
3164767a2217SDavid Ahern 		err = PTR_ERR(rt->fib6_metrics);
3165fda21d46SEric Dumazet 		/* Do not leave garbage there. */
3166fda21d46SEric Dumazet 		rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3167767a2217SDavid Ahern 		goto out;
3168767a2217SDavid Ahern 	}
3169767a2217SDavid Ahern 
317093531c67SDavid Ahern 	if (cfg->fc_flags & RTF_ADDRCONF)
317193531c67SDavid Ahern 		rt->dst_nocount = true;
31721da177e4SLinus Torvalds 
31731716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
317414895687SDavid Ahern 		fib6_set_expires(rt, jiffies +
31751716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
31761716a961SGao feng 	else
317714895687SDavid Ahern 		fib6_clean_expires(rt);
31781da177e4SLinus Torvalds 
317986872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
318086872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
318193c2fb25SDavid Ahern 	rt->fib6_protocol = cfg->fc_protocol;
318286872cb5SThomas Graf 
318383c44251SDavid Ahern 	rt->fib6_table = table;
318483c44251SDavid Ahern 	rt->fib6_metric = cfg->fc_metric;
318583c44251SDavid Ahern 	rt->fib6_type = cfg->fc_type;
31862b2450caSDavid Ahern 	rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
318719e42e45SRoopa Prabhu 
318893c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
318993c2fb25SDavid Ahern 	rt->fib6_dst.plen = cfg->fc_dst_len;
319093c2fb25SDavid Ahern 	if (rt->fib6_dst.plen == 128)
31913b6761d1SDavid Ahern 		rt->dst_host = true;
31921da177e4SLinus Torvalds 
31931da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
319493c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
319593c2fb25SDavid Ahern 	rt->fib6_src.plen = cfg->fc_src_len;
31961da177e4SLinus Torvalds #endif
319783c44251SDavid Ahern 	err = fib6_nh_init(net, &rt->fib6_nh, cfg, gfp_flags, extack);
31981da177e4SLinus Torvalds 	if (err)
31991da177e4SLinus Torvalds 		goto out;
32009fbb704cSDavid Ahern 
320183c44251SDavid Ahern 	/* We cannot add true routes via loopback here,
320283c44251SDavid Ahern 	 * they would result in kernel looping; promote them to reject routes
320383c44251SDavid Ahern 	 */
320483c44251SDavid Ahern 	addr_type = ipv6_addr_type(&cfg->fc_dst);
3205ad1601aeSDavid Ahern 	if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh.fib_nh_dev, addr_type))
320683c44251SDavid Ahern 		rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3207955ec4cbSDavid Ahern 
3208c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
320983c44251SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
321083c44251SDavid Ahern 
3211c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3212d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
3213c3968a85SDaniel Walter 			err = -EINVAL;
3214c3968a85SDaniel Walter 			goto out;
3215c3968a85SDaniel Walter 		}
321693c2fb25SDavid Ahern 		rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
321793c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 128;
3218c3968a85SDaniel Walter 	} else
321993c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
3220c3968a85SDaniel Walter 
32218c5b83f0SRoopa Prabhu 	return rt;
32221da177e4SLinus Torvalds out:
322393531c67SDavid Ahern 	fib6_info_release(rt);
32248c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
32256b9ea5a6SRoopa Prabhu }
32266b9ea5a6SRoopa Prabhu 
3227acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3228333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
32296b9ea5a6SRoopa Prabhu {
32308d1c802bSDavid Ahern 	struct fib6_info *rt;
32316b9ea5a6SRoopa Prabhu 	int err;
32326b9ea5a6SRoopa Prabhu 
3233acb54e3cSDavid Ahern 	rt = ip6_route_info_create(cfg, gfp_flags, extack);
3234d4ead6b3SDavid Ahern 	if (IS_ERR(rt))
3235d4ead6b3SDavid Ahern 		return PTR_ERR(rt);
32366b9ea5a6SRoopa Prabhu 
3237d4ead6b3SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
323893531c67SDavid Ahern 	fib6_info_release(rt);
32396b9ea5a6SRoopa Prabhu 
32401da177e4SLinus Torvalds 	return err;
32411da177e4SLinus Torvalds }
32421da177e4SLinus Torvalds 
32438d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
32441da177e4SLinus Torvalds {
3245afb1d4b5SDavid Ahern 	struct net *net = info->nl_net;
3246c71099acSThomas Graf 	struct fib6_table *table;
3247afb1d4b5SDavid Ahern 	int err;
32481da177e4SLinus Torvalds 
3249421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
32506825a26cSGao feng 		err = -ENOENT;
32516825a26cSGao feng 		goto out;
32526825a26cSGao feng 	}
32536c813a72SPatrick McHardy 
325493c2fb25SDavid Ahern 	table = rt->fib6_table;
325566f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
325686872cb5SThomas Graf 	err = fib6_del(rt, info);
325766f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
32581da177e4SLinus Torvalds 
32596825a26cSGao feng out:
326093531c67SDavid Ahern 	fib6_info_release(rt);
32611da177e4SLinus Torvalds 	return err;
32621da177e4SLinus Torvalds }
32631da177e4SLinus Torvalds 
32648d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt)
3265e0a1ad73SThomas Graf {
3266afb1d4b5SDavid Ahern 	struct nl_info info = { .nl_net = net };
3267afb1d4b5SDavid Ahern 
3268528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
3269e0a1ad73SThomas Graf }
3270e0a1ad73SThomas Graf 
32718d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
32720ae81335SDavid Ahern {
32730ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
3274e3330039SWANG Cong 	struct net *net = info->nl_net;
327516a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
32760ae81335SDavid Ahern 	struct fib6_table *table;
3277e3330039SWANG Cong 	int err = -ENOENT;
32780ae81335SDavid Ahern 
3279421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
3280e3330039SWANG Cong 		goto out_put;
328193c2fb25SDavid Ahern 	table = rt->fib6_table;
328266f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
32830ae81335SDavid Ahern 
328493c2fb25SDavid Ahern 	if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
32858d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
32860ae81335SDavid Ahern 
328716a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
328816a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
328916a16cd3SDavid Ahern 		if (skb) {
329016a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
329116a16cd3SDavid Ahern 
3292d4ead6b3SDavid Ahern 			if (rt6_fill_node(net, skb, rt, NULL,
329316a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
329416a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
329516a16cd3SDavid Ahern 				kfree_skb(skb);
329616a16cd3SDavid Ahern 				skb = NULL;
329716a16cd3SDavid Ahern 			} else
329816a16cd3SDavid Ahern 				info->skip_notify = 1;
329916a16cd3SDavid Ahern 		}
330016a16cd3SDavid Ahern 
33010ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
330293c2fb25SDavid Ahern 					 &rt->fib6_siblings,
330393c2fb25SDavid Ahern 					 fib6_siblings) {
33040ae81335SDavid Ahern 			err = fib6_del(sibling, info);
33050ae81335SDavid Ahern 			if (err)
3306e3330039SWANG Cong 				goto out_unlock;
33070ae81335SDavid Ahern 		}
33080ae81335SDavid Ahern 	}
33090ae81335SDavid Ahern 
33100ae81335SDavid Ahern 	err = fib6_del(rt, info);
3311e3330039SWANG Cong out_unlock:
331266f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
3313e3330039SWANG Cong out_put:
331493531c67SDavid Ahern 	fib6_info_release(rt);
331516a16cd3SDavid Ahern 
331616a16cd3SDavid Ahern 	if (skb) {
3317e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
331816a16cd3SDavid Ahern 			    info->nlh, gfp_any());
331916a16cd3SDavid Ahern 	}
33200ae81335SDavid Ahern 	return err;
33210ae81335SDavid Ahern }
33220ae81335SDavid Ahern 
332323fb93a4SDavid Ahern static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
332423fb93a4SDavid Ahern {
332523fb93a4SDavid Ahern 	int rc = -ESRCH;
332623fb93a4SDavid Ahern 
332723fb93a4SDavid Ahern 	if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
332823fb93a4SDavid Ahern 		goto out;
332923fb93a4SDavid Ahern 
333023fb93a4SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY &&
333123fb93a4SDavid Ahern 	    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
333223fb93a4SDavid Ahern 		goto out;
3333761f6026SXin Long 
333423fb93a4SDavid Ahern 	rc = rt6_remove_exception_rt(rt);
333523fb93a4SDavid Ahern out:
333623fb93a4SDavid Ahern 	return rc;
333723fb93a4SDavid Ahern }
333823fb93a4SDavid Ahern 
3339333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
3340333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
33411da177e4SLinus Torvalds {
33428d1c802bSDavid Ahern 	struct rt6_info *rt_cache;
3343c71099acSThomas Graf 	struct fib6_table *table;
33448d1c802bSDavid Ahern 	struct fib6_info *rt;
33451da177e4SLinus Torvalds 	struct fib6_node *fn;
33461da177e4SLinus Torvalds 	int err = -ESRCH;
33471da177e4SLinus Torvalds 
33485578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3349d5d531cbSDavid Ahern 	if (!table) {
3350d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
3351c71099acSThomas Graf 		return err;
3352d5d531cbSDavid Ahern 	}
33531da177e4SLinus Torvalds 
335466f5d6ceSWei Wang 	rcu_read_lock();
3355c71099acSThomas Graf 
3356c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
335786872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
335838fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
33592b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
33601da177e4SLinus Torvalds 
33611da177e4SLinus Torvalds 	if (fn) {
336266f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
3363ad1601aeSDavid Ahern 			struct fib6_nh *nh;
3364ad1601aeSDavid Ahern 
33652b760fcfSWei Wang 			if (cfg->fc_flags & RTF_CACHE) {
33667e4b5128SDavid Ahern 				struct fib6_result res = {
33677e4b5128SDavid Ahern 					.f6i = rt,
33687e4b5128SDavid Ahern 				};
336923fb93a4SDavid Ahern 				int rc;
337023fb93a4SDavid Ahern 
33717e4b5128SDavid Ahern 				rt_cache = rt6_find_cached_rt(&res,
33727e4b5128SDavid Ahern 							      &cfg->fc_dst,
33732b760fcfSWei Wang 							      &cfg->fc_src);
337423fb93a4SDavid Ahern 				if (rt_cache) {
337523fb93a4SDavid Ahern 					rc = ip6_del_cached_rt(rt_cache, cfg);
33769e575010SEric Dumazet 					if (rc != -ESRCH) {
33779e575010SEric Dumazet 						rcu_read_unlock();
337823fb93a4SDavid Ahern 						return rc;
337923fb93a4SDavid Ahern 					}
33809e575010SEric Dumazet 				}
33811f56a01fSMartin KaFai Lau 				continue;
33822b760fcfSWei Wang 			}
3383ad1601aeSDavid Ahern 
3384ad1601aeSDavid Ahern 			nh = &rt->fib6_nh;
338586872cb5SThomas Graf 			if (cfg->fc_ifindex &&
3386ad1601aeSDavid Ahern 			    (!nh->fib_nh_dev ||
3387ad1601aeSDavid Ahern 			     nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
33881da177e4SLinus Torvalds 				continue;
338986872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
3390ad1601aeSDavid Ahern 			    !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
33911da177e4SLinus Torvalds 				continue;
339293c2fb25SDavid Ahern 			if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
33931da177e4SLinus Torvalds 				continue;
339493c2fb25SDavid Ahern 			if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
3395c2ed1880SMantas M 				continue;
3396e873e4b9SWei Wang 			if (!fib6_info_hold_safe(rt))
3397e873e4b9SWei Wang 				continue;
339866f5d6ceSWei Wang 			rcu_read_unlock();
33991da177e4SLinus Torvalds 
34000ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
34010ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
340286872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
34030ae81335SDavid Ahern 
34040ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
34051da177e4SLinus Torvalds 		}
34061da177e4SLinus Torvalds 	}
340766f5d6ceSWei Wang 	rcu_read_unlock();
34081da177e4SLinus Torvalds 
34091da177e4SLinus Torvalds 	return err;
34101da177e4SLinus Torvalds }
34111da177e4SLinus Torvalds 
34126700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3413a6279458SYOSHIFUJI Hideaki {
3414a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
3415e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
341685bd05deSDavid Ahern 	struct fib6_result res = {};
3417e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
3418e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
3419e8599ff4SDavid S. Miller 	struct neighbour *neigh;
342071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
34216e157b6aSDavid S. Miller 	int optlen, on_link;
34226e157b6aSDavid S. Miller 	u8 *lladdr;
3423e8599ff4SDavid S. Miller 
342429a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
342571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
3426e8599ff4SDavid S. Miller 
3427e8599ff4SDavid S. Miller 	if (optlen < 0) {
34286e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3429e8599ff4SDavid S. Miller 		return;
3430e8599ff4SDavid S. Miller 	}
3431e8599ff4SDavid S. Miller 
343271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
3433e8599ff4SDavid S. Miller 
343471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
34356e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3436e8599ff4SDavid S. Miller 		return;
3437e8599ff4SDavid S. Miller 	}
3438e8599ff4SDavid S. Miller 
34396e157b6aSDavid S. Miller 	on_link = 0;
344071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
3441e8599ff4SDavid S. Miller 		on_link = 1;
344271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
3443e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
34446e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
3445e8599ff4SDavid S. Miller 		return;
3446e8599ff4SDavid S. Miller 	}
3447e8599ff4SDavid S. Miller 
3448e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
3449e8599ff4SDavid S. Miller 	if (!in6_dev)
3450e8599ff4SDavid S. Miller 		return;
3451e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3452e8599ff4SDavid S. Miller 		return;
3453e8599ff4SDavid S. Miller 
3454e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
3455e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
3456e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
3457e8599ff4SDavid S. Miller 	 */
3458e8599ff4SDavid S. Miller 
3459f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
3460e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3461e8599ff4SDavid S. Miller 		return;
3462e8599ff4SDavid S. Miller 	}
34636e157b6aSDavid S. Miller 
34646e157b6aSDavid S. Miller 	lladdr = NULL;
3465e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
3466e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3467e8599ff4SDavid S. Miller 					     skb->dev);
3468e8599ff4SDavid S. Miller 		if (!lladdr) {
3469e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3470e8599ff4SDavid S. Miller 			return;
3471e8599ff4SDavid S. Miller 		}
3472e8599ff4SDavid S. Miller 	}
3473e8599ff4SDavid S. Miller 
34746e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
3475ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
34766e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
34776e157b6aSDavid S. Miller 		return;
34786e157b6aSDavid S. Miller 	}
34796e157b6aSDavid S. Miller 
34806e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
34816e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
34826e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
34836e157b6aSDavid S. Miller 	 */
34840dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
34856e157b6aSDavid S. Miller 
348671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
3487e8599ff4SDavid S. Miller 	if (!neigh)
3488e8599ff4SDavid S. Miller 		return;
3489e8599ff4SDavid S. Miller 
34901da177e4SLinus Torvalds 	/*
34911da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
34921da177e4SLinus Torvalds 	 */
34931da177e4SLinus Torvalds 
3494f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
34951da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
34961da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
34971da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
3498f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
3499f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
35001da177e4SLinus Torvalds 
35014d85cd0cSDavid Ahern 	rcu_read_lock();
350285bd05deSDavid Ahern 	res.f6i = rcu_dereference(rt->from);
3503ff24e498SDavid S. Miller 	if (!res.f6i)
3504886b7a50SMartin KaFai Lau 		goto out;
35058a14e46fSDavid Ahern 
350685bd05deSDavid Ahern 	res.nh = &res.f6i->fib6_nh;
35077d21fec9SDavid Ahern 	res.fib6_flags = res.f6i->fib6_flags;
35087d21fec9SDavid Ahern 	res.fib6_type = res.f6i->fib6_type;
350985bd05deSDavid Ahern 	nrt = ip6_rt_cache_alloc(&res, &msg->dest, NULL);
351038308473SDavid S. Miller 	if (!nrt)
35111da177e4SLinus Torvalds 		goto out;
35121da177e4SLinus Torvalds 
35131da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
35141da177e4SLinus Torvalds 	if (on_link)
35151da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
35161da177e4SLinus Torvalds 
35174e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
35181da177e4SLinus Torvalds 
3519886b7a50SMartin KaFai Lau 	/* rt6_insert_exception() will take care of duplicated exceptions */
35205012f0a5SDavid Ahern 	if (rt6_insert_exception(nrt, &res)) {
35212b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
35222b760fcfSWei Wang 		goto out;
35232b760fcfSWei Wang 	}
35241da177e4SLinus Torvalds 
3525d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
3526d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
352771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
352860592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
35298d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
35308d71740cSTom Tucker 
35311da177e4SLinus Torvalds out:
3532886b7a50SMartin KaFai Lau 	rcu_read_unlock();
3533e8599ff4SDavid S. Miller 	neigh_release(neigh);
35346e157b6aSDavid S. Miller }
35356e157b6aSDavid S. Miller 
353670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
35378d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
3538b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3539830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3540830218c1SDavid Ahern 					   struct net_device *dev)
354170ceb4f5SYOSHIFUJI Hideaki {
3542830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3543830218c1SDavid Ahern 	int ifindex = dev->ifindex;
354470ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
35458d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3546c71099acSThomas Graf 	struct fib6_table *table;
354770ceb4f5SYOSHIFUJI Hideaki 
3548830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
354938308473SDavid S. Miller 	if (!table)
3550c71099acSThomas Graf 		return NULL;
3551c71099acSThomas Graf 
355266f5d6ceSWei Wang 	rcu_read_lock();
355338fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
355470ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
355570ceb4f5SYOSHIFUJI Hideaki 		goto out;
355670ceb4f5SYOSHIFUJI Hideaki 
355766f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
3558ad1601aeSDavid Ahern 		if (rt->fib6_nh.fib_nh_dev->ifindex != ifindex)
355970ceb4f5SYOSHIFUJI Hideaki 			continue;
35602b2450caSDavid Ahern 		if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
3561bdf00467SDavid Ahern 		    !rt->fib6_nh.fib_nh_gw_family)
356270ceb4f5SYOSHIFUJI Hideaki 			continue;
3563ad1601aeSDavid Ahern 		if (!ipv6_addr_equal(&rt->fib6_nh.fib_nh_gw6, gwaddr))
356470ceb4f5SYOSHIFUJI Hideaki 			continue;
3565e873e4b9SWei Wang 		if (!fib6_info_hold_safe(rt))
3566e873e4b9SWei Wang 			continue;
356770ceb4f5SYOSHIFUJI Hideaki 		break;
356870ceb4f5SYOSHIFUJI Hideaki 	}
356970ceb4f5SYOSHIFUJI Hideaki out:
357066f5d6ceSWei Wang 	rcu_read_unlock();
357170ceb4f5SYOSHIFUJI Hideaki 	return rt;
357270ceb4f5SYOSHIFUJI Hideaki }
357370ceb4f5SYOSHIFUJI Hideaki 
35748d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
3575b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3576830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3577830218c1SDavid Ahern 					   struct net_device *dev,
357895c96174SEric Dumazet 					   unsigned int pref)
357970ceb4f5SYOSHIFUJI Hideaki {
358086872cb5SThomas Graf 	struct fib6_config cfg = {
3581238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
3582830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
358386872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
358486872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
358586872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
3586b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3587e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
358815e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
3589efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3590efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
359186872cb5SThomas Graf 	};
359270ceb4f5SYOSHIFUJI Hideaki 
3593830218c1SDavid Ahern 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
35944e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
35954e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
359686872cb5SThomas Graf 
3597e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
3598e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
359986872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
360070ceb4f5SYOSHIFUJI Hideaki 
3601acb54e3cSDavid Ahern 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
360270ceb4f5SYOSHIFUJI Hideaki 
3603830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
360470ceb4f5SYOSHIFUJI Hideaki }
360570ceb4f5SYOSHIFUJI Hideaki #endif
360670ceb4f5SYOSHIFUJI Hideaki 
36078d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net,
3608afb1d4b5SDavid Ahern 				     const struct in6_addr *addr,
3609afb1d4b5SDavid Ahern 				     struct net_device *dev)
36101da177e4SLinus Torvalds {
3611830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
36128d1c802bSDavid Ahern 	struct fib6_info *rt;
3613c71099acSThomas Graf 	struct fib6_table *table;
36141da177e4SLinus Torvalds 
3615afb1d4b5SDavid Ahern 	table = fib6_get_table(net, tb_id);
361638308473SDavid S. Miller 	if (!table)
3617c71099acSThomas Graf 		return NULL;
36181da177e4SLinus Torvalds 
361966f5d6ceSWei Wang 	rcu_read_lock();
362066f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
3621ad1601aeSDavid Ahern 		struct fib6_nh *nh = &rt->fib6_nh;
3622ad1601aeSDavid Ahern 
3623ad1601aeSDavid Ahern 		if (dev == nh->fib_nh_dev &&
362493c2fb25SDavid Ahern 		    ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
3625ad1601aeSDavid Ahern 		    ipv6_addr_equal(&nh->fib_nh_gw6, addr))
36261da177e4SLinus Torvalds 			break;
36271da177e4SLinus Torvalds 	}
3628e873e4b9SWei Wang 	if (rt && !fib6_info_hold_safe(rt))
3629e873e4b9SWei Wang 		rt = NULL;
363066f5d6ceSWei Wang 	rcu_read_unlock();
36311da177e4SLinus Torvalds 	return rt;
36321da177e4SLinus Torvalds }
36331da177e4SLinus Torvalds 
36348d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net,
3635afb1d4b5SDavid Ahern 				     const struct in6_addr *gwaddr,
3636ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
3637ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
36381da177e4SLinus Torvalds {
363986872cb5SThomas Graf 	struct fib6_config cfg = {
3640ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
3641238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
364286872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
364386872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
364486872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
3645b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3646e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
364715e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
36485578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3649afb1d4b5SDavid Ahern 		.fc_nlinfo.nl_net = net,
365086872cb5SThomas Graf 	};
36511da177e4SLinus Torvalds 
36524e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
36531da177e4SLinus Torvalds 
3654acb54e3cSDavid Ahern 	if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
3655830218c1SDavid Ahern 		struct fib6_table *table;
3656830218c1SDavid Ahern 
3657830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
3658830218c1SDavid Ahern 		if (table)
3659830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3660830218c1SDavid Ahern 	}
36611da177e4SLinus Torvalds 
3662afb1d4b5SDavid Ahern 	return rt6_get_dflt_router(net, gwaddr, dev);
36631da177e4SLinus Torvalds }
36641da177e4SLinus Torvalds 
3665afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net,
3666afb1d4b5SDavid Ahern 				     struct fib6_table *table)
36671da177e4SLinus Torvalds {
36688d1c802bSDavid Ahern 	struct fib6_info *rt;
36691da177e4SLinus Torvalds 
36701da177e4SLinus Torvalds restart:
367166f5d6ceSWei Wang 	rcu_read_lock();
367266f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
3673dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
3674dcd1f572SDavid Ahern 		struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3675dcd1f572SDavid Ahern 
367693c2fb25SDavid Ahern 		if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
3677e873e4b9SWei Wang 		    (!idev || idev->cnf.accept_ra != 2) &&
3678e873e4b9SWei Wang 		    fib6_info_hold_safe(rt)) {
367966f5d6ceSWei Wang 			rcu_read_unlock();
3680afb1d4b5SDavid Ahern 			ip6_del_rt(net, rt);
36811da177e4SLinus Torvalds 			goto restart;
36821da177e4SLinus Torvalds 		}
36831da177e4SLinus Torvalds 	}
368466f5d6ceSWei Wang 	rcu_read_unlock();
3685830218c1SDavid Ahern 
3686830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3687830218c1SDavid Ahern }
3688830218c1SDavid Ahern 
3689830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
3690830218c1SDavid Ahern {
3691830218c1SDavid Ahern 	struct fib6_table *table;
3692830218c1SDavid Ahern 	struct hlist_head *head;
3693830218c1SDavid Ahern 	unsigned int h;
3694830218c1SDavid Ahern 
3695830218c1SDavid Ahern 	rcu_read_lock();
3696830218c1SDavid Ahern 
3697830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3698830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
3699830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3700830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3701afb1d4b5SDavid Ahern 				__rt6_purge_dflt_routers(net, table);
3702830218c1SDavid Ahern 		}
3703830218c1SDavid Ahern 	}
3704830218c1SDavid Ahern 
3705830218c1SDavid Ahern 	rcu_read_unlock();
37061da177e4SLinus Torvalds }
37071da177e4SLinus Torvalds 
37085578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
37095578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
371086872cb5SThomas Graf 				 struct fib6_config *cfg)
371186872cb5SThomas Graf {
37128823a3acSMaciej Żenczykowski 	*cfg = (struct fib6_config){
37138823a3acSMaciej Żenczykowski 		.fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
37148823a3acSMaciej Żenczykowski 			 : RT6_TABLE_MAIN,
37158823a3acSMaciej Żenczykowski 		.fc_ifindex = rtmsg->rtmsg_ifindex,
371667f69513SDavid Ahern 		.fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
37178823a3acSMaciej Żenczykowski 		.fc_expires = rtmsg->rtmsg_info,
37188823a3acSMaciej Żenczykowski 		.fc_dst_len = rtmsg->rtmsg_dst_len,
37198823a3acSMaciej Żenczykowski 		.fc_src_len = rtmsg->rtmsg_src_len,
37208823a3acSMaciej Żenczykowski 		.fc_flags = rtmsg->rtmsg_flags,
37218823a3acSMaciej Żenczykowski 		.fc_type = rtmsg->rtmsg_type,
372286872cb5SThomas Graf 
37238823a3acSMaciej Żenczykowski 		.fc_nlinfo.nl_net = net,
372486872cb5SThomas Graf 
37258823a3acSMaciej Żenczykowski 		.fc_dst = rtmsg->rtmsg_dst,
37268823a3acSMaciej Żenczykowski 		.fc_src = rtmsg->rtmsg_src,
37278823a3acSMaciej Żenczykowski 		.fc_gateway = rtmsg->rtmsg_gateway,
37288823a3acSMaciej Żenczykowski 	};
372986872cb5SThomas Graf }
373086872cb5SThomas Graf 
37315578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
37321da177e4SLinus Torvalds {
373386872cb5SThomas Graf 	struct fib6_config cfg;
37341da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
37351da177e4SLinus Torvalds 	int err;
37361da177e4SLinus Torvalds 
37371da177e4SLinus Torvalds 	switch (cmd) {
37381da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
37391da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
3740af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
37411da177e4SLinus Torvalds 			return -EPERM;
37421da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
37431da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
37441da177e4SLinus Torvalds 		if (err)
37451da177e4SLinus Torvalds 			return -EFAULT;
37461da177e4SLinus Torvalds 
37475578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
374886872cb5SThomas Graf 
37491da177e4SLinus Torvalds 		rtnl_lock();
37501da177e4SLinus Torvalds 		switch (cmd) {
37511da177e4SLinus Torvalds 		case SIOCADDRT:
3752acb54e3cSDavid Ahern 			err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
37531da177e4SLinus Torvalds 			break;
37541da177e4SLinus Torvalds 		case SIOCDELRT:
3755333c4301SDavid Ahern 			err = ip6_route_del(&cfg, NULL);
37561da177e4SLinus Torvalds 			break;
37571da177e4SLinus Torvalds 		default:
37581da177e4SLinus Torvalds 			err = -EINVAL;
37591da177e4SLinus Torvalds 		}
37601da177e4SLinus Torvalds 		rtnl_unlock();
37611da177e4SLinus Torvalds 
37621da177e4SLinus Torvalds 		return err;
37633ff50b79SStephen Hemminger 	}
37641da177e4SLinus Torvalds 
37651da177e4SLinus Torvalds 	return -EINVAL;
37661da177e4SLinus Torvalds }
37671da177e4SLinus Torvalds 
37681da177e4SLinus Torvalds /*
37691da177e4SLinus Torvalds  *	Drop the packet on the floor
37701da177e4SLinus Torvalds  */
37711da177e4SLinus Torvalds 
3772d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
37731da177e4SLinus Torvalds {
3774adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
37751d3fd8a1SStephen Suryaputra 	struct net *net = dev_net(dst->dev);
37761d3fd8a1SStephen Suryaputra 	struct inet6_dev *idev;
37771d3fd8a1SStephen Suryaputra 	int type;
37781d3fd8a1SStephen Suryaputra 
37791d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev) &&
37801d3fd8a1SStephen Suryaputra 	    dst->dev == net->loopback_dev)
37811d3fd8a1SStephen Suryaputra 		idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
37821d3fd8a1SStephen Suryaputra 	else
37831d3fd8a1SStephen Suryaputra 		idev = ip6_dst_idev(dst);
37841d3fd8a1SStephen Suryaputra 
3785612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
3786612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
37870660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
378845bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
37891d3fd8a1SStephen Suryaputra 			IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
3790612f09e8SYOSHIFUJI Hideaki 			break;
3791612f09e8SYOSHIFUJI Hideaki 		}
3792612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
3793612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
37941d3fd8a1SStephen Suryaputra 		IP6_INC_STATS(net, idev, ipstats_mib_noroutes);
3795612f09e8SYOSHIFUJI Hideaki 		break;
3796612f09e8SYOSHIFUJI Hideaki 	}
37971d3fd8a1SStephen Suryaputra 
37981d3fd8a1SStephen Suryaputra 	/* Start over by dropping the dst for l3mdev case */
37991d3fd8a1SStephen Suryaputra 	if (netif_is_l3_master(skb->dev))
38001d3fd8a1SStephen Suryaputra 		skb_dst_drop(skb);
38011d3fd8a1SStephen Suryaputra 
38023ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
38031da177e4SLinus Torvalds 	kfree_skb(skb);
38041da177e4SLinus Torvalds 	return 0;
38051da177e4SLinus Torvalds }
38061da177e4SLinus Torvalds 
38079ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
38089ce8ade0SThomas Graf {
3809612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
38109ce8ade0SThomas Graf }
38119ce8ade0SThomas Graf 
3812ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
38131da177e4SLinus Torvalds {
3814adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3815612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
38161da177e4SLinus Torvalds }
38171da177e4SLinus Torvalds 
38189ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
38199ce8ade0SThomas Graf {
3820612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
38219ce8ade0SThomas Graf }
38229ce8ade0SThomas Graf 
3823ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
38249ce8ade0SThomas Graf {
3825adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3826612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
38279ce8ade0SThomas Graf }
38289ce8ade0SThomas Graf 
38291da177e4SLinus Torvalds /*
38301da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
38311da177e4SLinus Torvalds  */
38321da177e4SLinus Torvalds 
3833360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net,
3834afb1d4b5SDavid Ahern 				     struct inet6_dev *idev,
38351da177e4SLinus Torvalds 				     const struct in6_addr *addr,
3836acb54e3cSDavid Ahern 				     bool anycast, gfp_t gfp_flags)
38371da177e4SLinus Torvalds {
3838c7a1ce39SDavid Ahern 	struct fib6_config cfg = {
3839c7a1ce39SDavid Ahern 		.fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
3840c7a1ce39SDavid Ahern 		.fc_ifindex = idev->dev->ifindex,
3841c7a1ce39SDavid Ahern 		.fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP,
3842c7a1ce39SDavid Ahern 		.fc_dst = *addr,
3843c7a1ce39SDavid Ahern 		.fc_dst_len = 128,
3844c7a1ce39SDavid Ahern 		.fc_protocol = RTPROT_KERNEL,
3845c7a1ce39SDavid Ahern 		.fc_nlinfo.nl_net = net,
3846c7a1ce39SDavid Ahern 		.fc_ignore_dev_down = true,
3847c7a1ce39SDavid Ahern 	};
38485f02ce24SDavid Ahern 
3849e8478e80SDavid Ahern 	if (anycast) {
3850c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_ANYCAST;
3851c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_ANYCAST;
3852e8478e80SDavid Ahern 	} else {
3853c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_LOCAL;
3854c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_LOCAL;
3855e8478e80SDavid Ahern 	}
38561da177e4SLinus Torvalds 
3857c7a1ce39SDavid Ahern 	return ip6_route_info_create(&cfg, gfp_flags, NULL);
38581da177e4SLinus Torvalds }
38591da177e4SLinus Torvalds 
3860c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
3861c3968a85SDaniel Walter struct arg_dev_net_ip {
3862c3968a85SDaniel Walter 	struct net_device *dev;
3863c3968a85SDaniel Walter 	struct net *net;
3864c3968a85SDaniel Walter 	struct in6_addr *addr;
3865c3968a85SDaniel Walter };
3866c3968a85SDaniel Walter 
38678d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
3868c3968a85SDaniel Walter {
3869c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3870c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3871c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3872c3968a85SDaniel Walter 
3873ad1601aeSDavid Ahern 	if (((void *)rt->fib6_nh.fib_nh_dev == dev || !dev) &&
3874421842edSDavid Ahern 	    rt != net->ipv6.fib6_null_entry &&
387593c2fb25SDavid Ahern 	    ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
387660006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
3877c3968a85SDaniel Walter 		/* remove prefsrc entry */
387893c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
387960006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
3880c3968a85SDaniel Walter 	}
3881c3968a85SDaniel Walter 	return 0;
3882c3968a85SDaniel Walter }
3883c3968a85SDaniel Walter 
3884c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3885c3968a85SDaniel Walter {
3886c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
3887c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
3888c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
3889c3968a85SDaniel Walter 		.net = net,
3890c3968a85SDaniel Walter 		.addr = &ifp->addr,
3891c3968a85SDaniel Walter 	};
38920c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
3893c3968a85SDaniel Walter }
3894c3968a85SDaniel Walter 
38952b2450caSDavid Ahern #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT)
3896be7a010dSDuan Jiong 
3897be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
38988d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
3899be7a010dSDuan Jiong {
3900be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
3901be7a010dSDuan Jiong 
390293c2fb25SDavid Ahern 	if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
3903bdf00467SDavid Ahern 	    rt->fib6_nh.fib_nh_gw_family &&
3904ad1601aeSDavid Ahern 	    ipv6_addr_equal(gateway, &rt->fib6_nh.fib_nh_gw6)) {
3905be7a010dSDuan Jiong 		return -1;
3906be7a010dSDuan Jiong 	}
3907b16cb459SWei Wang 
3908b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
3909b16cb459SWei Wang 	 * This is needed because cached route may have a different
3910b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
3911b16cb459SWei Wang 	 */
3912b16cb459SWei Wang 	rt6_exceptions_clean_tohost(rt, gateway);
3913b16cb459SWei Wang 
3914be7a010dSDuan Jiong 	return 0;
3915be7a010dSDuan Jiong }
3916be7a010dSDuan Jiong 
3917be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3918be7a010dSDuan Jiong {
3919be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
3920be7a010dSDuan Jiong }
3921be7a010dSDuan Jiong 
39222127d95aSIdo Schimmel struct arg_netdev_event {
39232127d95aSIdo Schimmel 	const struct net_device *dev;
39244c981e28SIdo Schimmel 	union {
3925ecc5663cSDavid Ahern 		unsigned char nh_flags;
39264c981e28SIdo Schimmel 		unsigned long event;
39274c981e28SIdo Schimmel 	};
39282127d95aSIdo Schimmel };
39292127d95aSIdo Schimmel 
39308d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
3931d7dedee1SIdo Schimmel {
39328d1c802bSDavid Ahern 	struct fib6_info *iter;
3933d7dedee1SIdo Schimmel 	struct fib6_node *fn;
3934d7dedee1SIdo Schimmel 
393593c2fb25SDavid Ahern 	fn = rcu_dereference_protected(rt->fib6_node,
393693c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3937d7dedee1SIdo Schimmel 	iter = rcu_dereference_protected(fn->leaf,
393893c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3939d7dedee1SIdo Schimmel 	while (iter) {
394093c2fb25SDavid Ahern 		if (iter->fib6_metric == rt->fib6_metric &&
394133bd5ac5SDavid Ahern 		    rt6_qualify_for_ecmp(iter))
3942d7dedee1SIdo Schimmel 			return iter;
39438fb11a9aSDavid Ahern 		iter = rcu_dereference_protected(iter->fib6_next,
394493c2fb25SDavid Ahern 				lockdep_is_held(&rt->fib6_table->tb6_lock));
3945d7dedee1SIdo Schimmel 	}
3946d7dedee1SIdo Schimmel 
3947d7dedee1SIdo Schimmel 	return NULL;
3948d7dedee1SIdo Schimmel }
3949d7dedee1SIdo Schimmel 
39508d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt)
3951d7dedee1SIdo Schimmel {
3952ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD ||
3953ad1601aeSDavid Ahern 	    (rt->fib6_nh.fib_nh_flags & RTNH_F_LINKDOWN &&
3954ad1601aeSDavid Ahern 	     ip6_ignore_linkdown(rt->fib6_nh.fib_nh_dev)))
3955d7dedee1SIdo Schimmel 		return true;
3956d7dedee1SIdo Schimmel 
3957d7dedee1SIdo Schimmel 	return false;
3958d7dedee1SIdo Schimmel }
3959d7dedee1SIdo Schimmel 
39608d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt)
3961d7dedee1SIdo Schimmel {
39628d1c802bSDavid Ahern 	struct fib6_info *iter;
3963d7dedee1SIdo Schimmel 	int total = 0;
3964d7dedee1SIdo Schimmel 
3965d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt))
3966ad1601aeSDavid Ahern 		total += rt->fib6_nh.fib_nh_weight;
3967d7dedee1SIdo Schimmel 
396893c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
3969d7dedee1SIdo Schimmel 		if (!rt6_is_dead(iter))
3970ad1601aeSDavid Ahern 			total += iter->fib6_nh.fib_nh_weight;
3971d7dedee1SIdo Schimmel 	}
3972d7dedee1SIdo Schimmel 
3973d7dedee1SIdo Schimmel 	return total;
3974d7dedee1SIdo Schimmel }
3975d7dedee1SIdo Schimmel 
39768d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
3977d7dedee1SIdo Schimmel {
3978d7dedee1SIdo Schimmel 	int upper_bound = -1;
3979d7dedee1SIdo Schimmel 
3980d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt)) {
3981ad1601aeSDavid Ahern 		*weight += rt->fib6_nh.fib_nh_weight;
3982d7dedee1SIdo Schimmel 		upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
3983d7dedee1SIdo Schimmel 						    total) - 1;
3984d7dedee1SIdo Schimmel 	}
3985ad1601aeSDavid Ahern 	atomic_set(&rt->fib6_nh.fib_nh_upper_bound, upper_bound);
3986d7dedee1SIdo Schimmel }
3987d7dedee1SIdo Schimmel 
39888d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
3989d7dedee1SIdo Schimmel {
39908d1c802bSDavid Ahern 	struct fib6_info *iter;
3991d7dedee1SIdo Schimmel 	int weight = 0;
3992d7dedee1SIdo Schimmel 
3993d7dedee1SIdo Schimmel 	rt6_upper_bound_set(rt, &weight, total);
3994d7dedee1SIdo Schimmel 
399593c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
3996d7dedee1SIdo Schimmel 		rt6_upper_bound_set(iter, &weight, total);
3997d7dedee1SIdo Schimmel }
3998d7dedee1SIdo Schimmel 
39998d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt)
4000d7dedee1SIdo Schimmel {
40018d1c802bSDavid Ahern 	struct fib6_info *first;
4002d7dedee1SIdo Schimmel 	int total;
4003d7dedee1SIdo Schimmel 
4004d7dedee1SIdo Schimmel 	/* In case the entire multipath route was marked for flushing,
4005d7dedee1SIdo Schimmel 	 * then there is no need to rebalance upon the removal of every
4006d7dedee1SIdo Schimmel 	 * sibling route.
4007d7dedee1SIdo Schimmel 	 */
400893c2fb25SDavid Ahern 	if (!rt->fib6_nsiblings || rt->should_flush)
4009d7dedee1SIdo Schimmel 		return;
4010d7dedee1SIdo Schimmel 
4011d7dedee1SIdo Schimmel 	/* During lookup routes are evaluated in order, so we need to
4012d7dedee1SIdo Schimmel 	 * make sure upper bounds are assigned from the first sibling
4013d7dedee1SIdo Schimmel 	 * onwards.
4014d7dedee1SIdo Schimmel 	 */
4015d7dedee1SIdo Schimmel 	first = rt6_multipath_first_sibling(rt);
4016d7dedee1SIdo Schimmel 	if (WARN_ON_ONCE(!first))
4017d7dedee1SIdo Schimmel 		return;
4018d7dedee1SIdo Schimmel 
4019d7dedee1SIdo Schimmel 	total = rt6_multipath_total_weight(first);
4020d7dedee1SIdo Schimmel 	rt6_multipath_upper_bound_set(first, total);
4021d7dedee1SIdo Schimmel }
4022d7dedee1SIdo Schimmel 
40238d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg)
40242127d95aSIdo Schimmel {
40252127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
40267aef6859SDavid Ahern 	struct net *net = dev_net(arg->dev);
40272127d95aSIdo Schimmel 
4028ad1601aeSDavid Ahern 	if (rt != net->ipv6.fib6_null_entry &&
4029ad1601aeSDavid Ahern 	    rt->fib6_nh.fib_nh_dev == arg->dev) {
4030ad1601aeSDavid Ahern 		rt->fib6_nh.fib_nh_flags &= ~arg->nh_flags;
40317aef6859SDavid Ahern 		fib6_update_sernum_upto_root(net, rt);
4032d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
40331de178edSIdo Schimmel 	}
40342127d95aSIdo Schimmel 
40352127d95aSIdo Schimmel 	return 0;
40362127d95aSIdo Schimmel }
40372127d95aSIdo Schimmel 
4038ecc5663cSDavid Ahern void rt6_sync_up(struct net_device *dev, unsigned char nh_flags)
40392127d95aSIdo Schimmel {
40402127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
40412127d95aSIdo Schimmel 		.dev = dev,
40426802f3adSIdo Schimmel 		{
40432127d95aSIdo Schimmel 			.nh_flags = nh_flags,
40446802f3adSIdo Schimmel 		},
40452127d95aSIdo Schimmel 	};
40462127d95aSIdo Schimmel 
40472127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
40482127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
40492127d95aSIdo Schimmel 
40502127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
40512127d95aSIdo Schimmel }
40522127d95aSIdo Schimmel 
40538d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
40541de178edSIdo Schimmel 				   const struct net_device *dev)
40551de178edSIdo Schimmel {
40568d1c802bSDavid Ahern 	struct fib6_info *iter;
40571de178edSIdo Schimmel 
4058ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_dev == dev)
40591de178edSIdo Schimmel 		return true;
406093c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4061ad1601aeSDavid Ahern 		if (iter->fib6_nh.fib_nh_dev == dev)
40621de178edSIdo Schimmel 			return true;
40631de178edSIdo Schimmel 
40641de178edSIdo Schimmel 	return false;
40651de178edSIdo Schimmel }
40661de178edSIdo Schimmel 
40678d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt)
40681de178edSIdo Schimmel {
40698d1c802bSDavid Ahern 	struct fib6_info *iter;
40701de178edSIdo Schimmel 
40711de178edSIdo Schimmel 	rt->should_flush = 1;
407293c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
40731de178edSIdo Schimmel 		iter->should_flush = 1;
40741de178edSIdo Schimmel }
40751de178edSIdo Schimmel 
40768d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
40771de178edSIdo Schimmel 					     const struct net_device *down_dev)
40781de178edSIdo Schimmel {
40798d1c802bSDavid Ahern 	struct fib6_info *iter;
40801de178edSIdo Schimmel 	unsigned int dead = 0;
40811de178edSIdo Schimmel 
4082ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_dev == down_dev ||
4083ad1601aeSDavid Ahern 	    rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD)
40841de178edSIdo Schimmel 		dead++;
408593c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4086ad1601aeSDavid Ahern 		if (iter->fib6_nh.fib_nh_dev == down_dev ||
4087ad1601aeSDavid Ahern 		    iter->fib6_nh.fib_nh_flags & RTNH_F_DEAD)
40881de178edSIdo Schimmel 			dead++;
40891de178edSIdo Schimmel 
40901de178edSIdo Schimmel 	return dead;
40911de178edSIdo Schimmel }
40921de178edSIdo Schimmel 
40938d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
40941de178edSIdo Schimmel 				       const struct net_device *dev,
4095ecc5663cSDavid Ahern 				       unsigned char nh_flags)
40961de178edSIdo Schimmel {
40978d1c802bSDavid Ahern 	struct fib6_info *iter;
40981de178edSIdo Schimmel 
4099ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_dev == dev)
4100ad1601aeSDavid Ahern 		rt->fib6_nh.fib_nh_flags |= nh_flags;
410193c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4102ad1601aeSDavid Ahern 		if (iter->fib6_nh.fib_nh_dev == dev)
4103ad1601aeSDavid Ahern 			iter->fib6_nh.fib_nh_flags |= nh_flags;
41041de178edSIdo Schimmel }
41051de178edSIdo Schimmel 
4106a1a22c12SDavid Ahern /* called with write lock held for table with rt */
41078d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
41081da177e4SLinus Torvalds {
41094c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
41104c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
41117aef6859SDavid Ahern 	struct net *net = dev_net(dev);
41128ed67789SDaniel Lezcano 
4113421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
411427c6fa73SIdo Schimmel 		return 0;
411527c6fa73SIdo Schimmel 
411627c6fa73SIdo Schimmel 	switch (arg->event) {
411727c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
4118ad1601aeSDavid Ahern 		return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0;
411927c6fa73SIdo Schimmel 	case NETDEV_DOWN:
41201de178edSIdo Schimmel 		if (rt->should_flush)
412127c6fa73SIdo Schimmel 			return -1;
412293c2fb25SDavid Ahern 		if (!rt->fib6_nsiblings)
4123ad1601aeSDavid Ahern 			return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0;
41241de178edSIdo Schimmel 		if (rt6_multipath_uses_dev(rt, dev)) {
41251de178edSIdo Schimmel 			unsigned int count;
41261de178edSIdo Schimmel 
41271de178edSIdo Schimmel 			count = rt6_multipath_dead_count(rt, dev);
412893c2fb25SDavid Ahern 			if (rt->fib6_nsiblings + 1 == count) {
41291de178edSIdo Schimmel 				rt6_multipath_flush(rt);
41301de178edSIdo Schimmel 				return -1;
41311de178edSIdo Schimmel 			}
41321de178edSIdo Schimmel 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
41331de178edSIdo Schimmel 						   RTNH_F_LINKDOWN);
41347aef6859SDavid Ahern 			fib6_update_sernum(net, rt);
4135d7dedee1SIdo Schimmel 			rt6_multipath_rebalance(rt);
41361de178edSIdo Schimmel 		}
41371de178edSIdo Schimmel 		return -2;
413827c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
4139ad1601aeSDavid Ahern 		if (rt->fib6_nh.fib_nh_dev != dev ||
414093c2fb25SDavid Ahern 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
414127c6fa73SIdo Schimmel 			break;
4142ad1601aeSDavid Ahern 		rt->fib6_nh.fib_nh_flags |= RTNH_F_LINKDOWN;
4143d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
414427c6fa73SIdo Schimmel 		break;
41452b241361SIdo Schimmel 	}
4146c159d30cSDavid S. Miller 
41471da177e4SLinus Torvalds 	return 0;
41481da177e4SLinus Torvalds }
41491da177e4SLinus Torvalds 
415027c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
41511da177e4SLinus Torvalds {
41524c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
41538ed67789SDaniel Lezcano 		.dev = dev,
41546802f3adSIdo Schimmel 		{
41554c981e28SIdo Schimmel 			.event = event,
41566802f3adSIdo Schimmel 		},
41578ed67789SDaniel Lezcano 	};
41587c6bb7d2SDavid Ahern 	struct net *net = dev_net(dev);
41598ed67789SDaniel Lezcano 
41607c6bb7d2SDavid Ahern 	if (net->ipv6.sysctl.skip_notify_on_dev_down)
41617c6bb7d2SDavid Ahern 		fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
41627c6bb7d2SDavid Ahern 	else
41637c6bb7d2SDavid Ahern 		fib6_clean_all(net, fib6_ifdown, &arg);
41644c981e28SIdo Schimmel }
41654c981e28SIdo Schimmel 
41664c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
41674c981e28SIdo Schimmel {
41684c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
41694c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
41704c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
41711da177e4SLinus Torvalds }
41721da177e4SLinus Torvalds 
417395c96174SEric Dumazet struct rt6_mtu_change_arg {
41741da177e4SLinus Torvalds 	struct net_device *dev;
417595c96174SEric Dumazet 	unsigned int mtu;
41761da177e4SLinus Torvalds };
41771da177e4SLinus Torvalds 
41788d1c802bSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg)
41791da177e4SLinus Torvalds {
41801da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
41811da177e4SLinus Torvalds 	struct inet6_dev *idev;
41821da177e4SLinus Torvalds 
41831da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
41841da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
41851da177e4SLinus Torvalds 	   We still use this lock to block changes
41861da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
41871da177e4SLinus Torvalds 	*/
41881da177e4SLinus Torvalds 
41891da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
419038308473SDavid S. Miller 	if (!idev)
41911da177e4SLinus Torvalds 		return 0;
41921da177e4SLinus Torvalds 
41931da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
41941da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
41951da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
41961da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
41971da177e4SLinus Torvalds 	 */
4198ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_dev == arg->dev &&
4199d4ead6b3SDavid Ahern 	    !fib6_metric_locked(rt, RTAX_MTU)) {
4200d4ead6b3SDavid Ahern 		u32 mtu = rt->fib6_pmtu;
4201d4ead6b3SDavid Ahern 
4202d4ead6b3SDavid Ahern 		if (mtu >= arg->mtu ||
4203d4ead6b3SDavid Ahern 		    (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4204d4ead6b3SDavid Ahern 			fib6_metric_set(rt, RTAX_MTU, arg->mtu);
4205d4ead6b3SDavid Ahern 
4206f5bbe7eeSWei Wang 		spin_lock_bh(&rt6_exception_lock);
4207e9fa1495SStefano Brivio 		rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
4208f5bbe7eeSWei Wang 		spin_unlock_bh(&rt6_exception_lock);
42094b32b5adSMartin KaFai Lau 	}
42101da177e4SLinus Torvalds 	return 0;
42111da177e4SLinus Torvalds }
42121da177e4SLinus Torvalds 
421395c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
42141da177e4SLinus Torvalds {
4215c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
4216c71099acSThomas Graf 		.dev = dev,
4217c71099acSThomas Graf 		.mtu = mtu,
4218c71099acSThomas Graf 	};
42191da177e4SLinus Torvalds 
42200c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
42211da177e4SLinus Torvalds }
42221da177e4SLinus Torvalds 
4223ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
42245176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
4225aa8f8778SEric Dumazet 	[RTA_PREFSRC]		= { .len = sizeof(struct in6_addr) },
422686872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
4227ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
422886872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
422986872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
423051ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
4231c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
423219e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
423319e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
423432bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
4235622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
42363b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
4237aa8f8778SEric Dumazet 	[RTA_TABLE]		= { .type = NLA_U32 },
4238eacb9384SRoopa Prabhu 	[RTA_IP_PROTO]		= { .type = NLA_U8 },
4239eacb9384SRoopa Prabhu 	[RTA_SPORT]		= { .type = NLA_U16 },
4240eacb9384SRoopa Prabhu 	[RTA_DPORT]		= { .type = NLA_U16 },
424186872cb5SThomas Graf };
424286872cb5SThomas Graf 
424386872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4244333c4301SDavid Ahern 			      struct fib6_config *cfg,
4245333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
42461da177e4SLinus Torvalds {
424786872cb5SThomas Graf 	struct rtmsg *rtm;
424886872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
4249c78ba6d6SLubomir Rintel 	unsigned int pref;
425086872cb5SThomas Graf 	int err;
42511da177e4SLinus Torvalds 
42528cb08174SJohannes Berg 	err = nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
42538cb08174SJohannes Berg 				     rtm_ipv6_policy, extack);
425486872cb5SThomas Graf 	if (err < 0)
425586872cb5SThomas Graf 		goto errout;
42561da177e4SLinus Torvalds 
425786872cb5SThomas Graf 	err = -EINVAL;
425886872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
425986872cb5SThomas Graf 
426084db8407SMaciej Żenczykowski 	*cfg = (struct fib6_config){
426184db8407SMaciej Żenczykowski 		.fc_table = rtm->rtm_table,
426284db8407SMaciej Żenczykowski 		.fc_dst_len = rtm->rtm_dst_len,
426384db8407SMaciej Żenczykowski 		.fc_src_len = rtm->rtm_src_len,
426484db8407SMaciej Żenczykowski 		.fc_flags = RTF_UP,
426584db8407SMaciej Żenczykowski 		.fc_protocol = rtm->rtm_protocol,
426684db8407SMaciej Żenczykowski 		.fc_type = rtm->rtm_type,
426784db8407SMaciej Żenczykowski 
426884db8407SMaciej Żenczykowski 		.fc_nlinfo.portid = NETLINK_CB(skb).portid,
426984db8407SMaciej Żenczykowski 		.fc_nlinfo.nlh = nlh,
427084db8407SMaciej Żenczykowski 		.fc_nlinfo.nl_net = sock_net(skb->sk),
427184db8407SMaciej Żenczykowski 	};
427286872cb5SThomas Graf 
4273ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
4274ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
4275b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
4276b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
427786872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
427886872cb5SThomas Graf 
4279ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
4280ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
4281ab79ad14SMaciej Żenczykowski 
42821f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
42831f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
42841f56a01fSMartin KaFai Lau 
4285fc1e64e1SDavid Ahern 	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4286fc1e64e1SDavid Ahern 
428786872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
428867b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
428986872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
42901da177e4SLinus Torvalds 	}
4291e3818541SDavid Ahern 	if (tb[RTA_VIA]) {
4292e3818541SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4293e3818541SDavid Ahern 		goto errout;
4294e3818541SDavid Ahern 	}
429586872cb5SThomas Graf 
429686872cb5SThomas Graf 	if (tb[RTA_DST]) {
429786872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
429886872cb5SThomas Graf 
429986872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
430086872cb5SThomas Graf 			goto errout;
430186872cb5SThomas Graf 
430286872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
43031da177e4SLinus Torvalds 	}
430486872cb5SThomas Graf 
430586872cb5SThomas Graf 	if (tb[RTA_SRC]) {
430686872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
430786872cb5SThomas Graf 
430886872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
430986872cb5SThomas Graf 			goto errout;
431086872cb5SThomas Graf 
431186872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
43121da177e4SLinus Torvalds 	}
431386872cb5SThomas Graf 
4314c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
431567b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4316c3968a85SDaniel Walter 
431786872cb5SThomas Graf 	if (tb[RTA_OIF])
431886872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
431986872cb5SThomas Graf 
432086872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
432186872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
432286872cb5SThomas Graf 
432386872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
432486872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
432586872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
43261da177e4SLinus Torvalds 	}
432786872cb5SThomas Graf 
432886872cb5SThomas Graf 	if (tb[RTA_TABLE])
432986872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
433086872cb5SThomas Graf 
433151ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
433251ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
433351ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
43349ed59592SDavid Ahern 
43359ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4336c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
43379ed59592SDavid Ahern 		if (err < 0)
43389ed59592SDavid Ahern 			goto errout;
433951ebd318SNicolas Dichtel 	}
434051ebd318SNicolas Dichtel 
4341c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
4342c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
4343c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
4344c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
4345c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
4346c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
4347c78ba6d6SLubomir Rintel 	}
4348c78ba6d6SLubomir Rintel 
434919e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
435019e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
435119e42e45SRoopa Prabhu 
43529ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
435319e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
435419e42e45SRoopa Prabhu 
4355c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
43569ed59592SDavid Ahern 		if (err < 0)
43579ed59592SDavid Ahern 			goto errout;
43589ed59592SDavid Ahern 	}
43599ed59592SDavid Ahern 
436032bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
436132bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
436232bc201eSXin Long 
436332bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
436432bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
436532bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
436632bc201eSXin Long 		}
436732bc201eSXin Long 	}
436832bc201eSXin Long 
436986872cb5SThomas Graf 	err = 0;
437086872cb5SThomas Graf errout:
437186872cb5SThomas Graf 	return err;
43721da177e4SLinus Torvalds }
43731da177e4SLinus Torvalds 
43746b9ea5a6SRoopa Prabhu struct rt6_nh {
43758d1c802bSDavid Ahern 	struct fib6_info *fib6_info;
43766b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
43776b9ea5a6SRoopa Prabhu 	struct list_head next;
43786b9ea5a6SRoopa Prabhu };
43796b9ea5a6SRoopa Prabhu 
4380d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net,
4381d4ead6b3SDavid Ahern 				 struct list_head *rt6_nh_list,
43828d1c802bSDavid Ahern 				 struct fib6_info *rt,
43838d1c802bSDavid Ahern 				 struct fib6_config *r_cfg)
43846b9ea5a6SRoopa Prabhu {
43856b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
43866b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
43876b9ea5a6SRoopa Prabhu 
43886b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
43898d1c802bSDavid Ahern 		/* check if fib6_info already exists */
43908d1c802bSDavid Ahern 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
43916b9ea5a6SRoopa Prabhu 			return err;
43926b9ea5a6SRoopa Prabhu 	}
43936b9ea5a6SRoopa Prabhu 
43946b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
43956b9ea5a6SRoopa Prabhu 	if (!nh)
43966b9ea5a6SRoopa Prabhu 		return -ENOMEM;
43978d1c802bSDavid Ahern 	nh->fib6_info = rt;
43986b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
43996b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
44006b9ea5a6SRoopa Prabhu 
44016b9ea5a6SRoopa Prabhu 	return 0;
44026b9ea5a6SRoopa Prabhu }
44036b9ea5a6SRoopa Prabhu 
44048d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt,
44058d1c802bSDavid Ahern 				   struct fib6_info *rt_last,
44063b1137feSDavid Ahern 				   struct nl_info *info,
44073b1137feSDavid Ahern 				   __u16 nlflags)
44083b1137feSDavid Ahern {
44093b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
44103b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
44113b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
44123b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
44133b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
44143b1137feSDavid Ahern 	 */
441593c2fb25SDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
441693c2fb25SDavid Ahern 		rt = list_first_entry(&rt_last->fib6_siblings,
44178d1c802bSDavid Ahern 				      struct fib6_info,
441893c2fb25SDavid Ahern 				      fib6_siblings);
44193b1137feSDavid Ahern 	}
44203b1137feSDavid Ahern 
44213b1137feSDavid Ahern 	if (rt)
44223b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
44233b1137feSDavid Ahern }
44243b1137feSDavid Ahern 
4425333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
4426333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
442751ebd318SNicolas Dichtel {
44288d1c802bSDavid Ahern 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
44293b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
443051ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
443151ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
44328d1c802bSDavid Ahern 	struct fib6_info *rt;
44336b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
44346b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
44353b1137feSDavid Ahern 	__u16 nlflags;
443651ebd318SNicolas Dichtel 	int remaining;
443751ebd318SNicolas Dichtel 	int attrlen;
44386b9ea5a6SRoopa Prabhu 	int err = 1;
44396b9ea5a6SRoopa Prabhu 	int nhn = 0;
44406b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
44416b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
44426b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
444351ebd318SNicolas Dichtel 
44443b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
44453b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
44463b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
44473b1137feSDavid Ahern 
444835f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
444951ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
445051ebd318SNicolas Dichtel 
44516b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
44528d1c802bSDavid Ahern 	 * fib6_info structs per nexthop
44536b9ea5a6SRoopa Prabhu 	 */
445451ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
445551ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
445651ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
445751ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
445851ebd318SNicolas Dichtel 
445951ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
446051ebd318SNicolas Dichtel 		if (attrlen > 0) {
446151ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
446251ebd318SNicolas Dichtel 
446351ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
446451ebd318SNicolas Dichtel 			if (nla) {
446567b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
446651ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
446751ebd318SNicolas Dichtel 			}
446819e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
446919e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
447019e42e45SRoopa Prabhu 			if (nla)
447119e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
447251ebd318SNicolas Dichtel 		}
44736b9ea5a6SRoopa Prabhu 
447468e2ffdeSDavid Ahern 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
4475acb54e3cSDavid Ahern 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
44768c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
44778c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
44788c5b83f0SRoopa Prabhu 			rt = NULL;
44796b9ea5a6SRoopa Prabhu 			goto cleanup;
44808c5b83f0SRoopa Prabhu 		}
4481b5d2d75eSDavid Ahern 		if (!rt6_qualify_for_ecmp(rt)) {
4482b5d2d75eSDavid Ahern 			err = -EINVAL;
4483b5d2d75eSDavid Ahern 			NL_SET_ERR_MSG(extack,
4484b5d2d75eSDavid Ahern 				       "Device only routes can not be added for IPv6 using the multipath API.");
4485b5d2d75eSDavid Ahern 			fib6_info_release(rt);
4486b5d2d75eSDavid Ahern 			goto cleanup;
4487b5d2d75eSDavid Ahern 		}
44886b9ea5a6SRoopa Prabhu 
4489ad1601aeSDavid Ahern 		rt->fib6_nh.fib_nh_weight = rtnh->rtnh_hops + 1;
4490398958aeSIdo Schimmel 
4491d4ead6b3SDavid Ahern 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4492d4ead6b3SDavid Ahern 					    rt, &r_cfg);
449351ebd318SNicolas Dichtel 		if (err) {
449493531c67SDavid Ahern 			fib6_info_release(rt);
44956b9ea5a6SRoopa Prabhu 			goto cleanup;
449651ebd318SNicolas Dichtel 		}
44976b9ea5a6SRoopa Prabhu 
44986b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
449951ebd318SNicolas Dichtel 	}
45006b9ea5a6SRoopa Prabhu 
45013b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
45023b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
45033b1137feSDavid Ahern 	 * the full route when done
45043b1137feSDavid Ahern 	 */
45053b1137feSDavid Ahern 	info->skip_notify = 1;
45063b1137feSDavid Ahern 
45076b9ea5a6SRoopa Prabhu 	err_nh = NULL;
45086b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
45098d1c802bSDavid Ahern 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
45108d1c802bSDavid Ahern 		fib6_info_release(nh->fib6_info);
45113b1137feSDavid Ahern 
4512f7225172SDavid Ahern 		if (!err) {
4513f7225172SDavid Ahern 			/* save reference to last route successfully inserted */
4514f7225172SDavid Ahern 			rt_last = nh->fib6_info;
4515f7225172SDavid Ahern 
45166b9ea5a6SRoopa Prabhu 			/* save reference to first route for notification */
4517f7225172SDavid Ahern 			if (!rt_notif)
45188d1c802bSDavid Ahern 				rt_notif = nh->fib6_info;
4519f7225172SDavid Ahern 		}
45206b9ea5a6SRoopa Prabhu 
45218d1c802bSDavid Ahern 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
45228d1c802bSDavid Ahern 		nh->fib6_info = NULL;
45236b9ea5a6SRoopa Prabhu 		if (err) {
45246b9ea5a6SRoopa Prabhu 			if (replace && nhn)
4525a5a82d84SJakub Kicinski 				NL_SET_ERR_MSG_MOD(extack,
4526a5a82d84SJakub Kicinski 						   "multipath route replace failed (check consistency of installed routes)");
45276b9ea5a6SRoopa Prabhu 			err_nh = nh;
45286b9ea5a6SRoopa Prabhu 			goto add_errout;
45296b9ea5a6SRoopa Prabhu 		}
45306b9ea5a6SRoopa Prabhu 
45311a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
453227596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
453327596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
453427596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
453527596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
453627596472SMichal Kubeček 		 * be added to it.
45371a72418bSNicolas Dichtel 		 */
453827596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
453927596472SMichal Kubeček 						     NLM_F_REPLACE);
45406b9ea5a6SRoopa Prabhu 		nhn++;
45416b9ea5a6SRoopa Prabhu 	}
45426b9ea5a6SRoopa Prabhu 
45433b1137feSDavid Ahern 	/* success ... tell user about new route */
45443b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
45456b9ea5a6SRoopa Prabhu 	goto cleanup;
45466b9ea5a6SRoopa Prabhu 
45476b9ea5a6SRoopa Prabhu add_errout:
45483b1137feSDavid Ahern 	/* send notification for routes that were added so that
45493b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
45503b1137feSDavid Ahern 	 * coherent
45513b1137feSDavid Ahern 	 */
45523b1137feSDavid Ahern 	if (rt_notif)
45533b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
45543b1137feSDavid Ahern 
45556b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
45566b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
45576b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
45586b9ea5a6SRoopa Prabhu 			break;
4559333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
45606b9ea5a6SRoopa Prabhu 	}
45616b9ea5a6SRoopa Prabhu 
45626b9ea5a6SRoopa Prabhu cleanup:
45636b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
45648d1c802bSDavid Ahern 		if (nh->fib6_info)
45658d1c802bSDavid Ahern 			fib6_info_release(nh->fib6_info);
45666b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
45676b9ea5a6SRoopa Prabhu 		kfree(nh);
45686b9ea5a6SRoopa Prabhu 	}
45696b9ea5a6SRoopa Prabhu 
45706b9ea5a6SRoopa Prabhu 	return err;
45716b9ea5a6SRoopa Prabhu }
45726b9ea5a6SRoopa Prabhu 
4573333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
4574333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
45756b9ea5a6SRoopa Prabhu {
45766b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
45776b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
45786b9ea5a6SRoopa Prabhu 	int remaining;
45796b9ea5a6SRoopa Prabhu 	int attrlen;
45806b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
45816b9ea5a6SRoopa Prabhu 
45826b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
45836b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
45846b9ea5a6SRoopa Prabhu 
45856b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
45866b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
45876b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
45886b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
45896b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
45906b9ea5a6SRoopa Prabhu 
45916b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
45926b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
45936b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
45946b9ea5a6SRoopa Prabhu 
45956b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
45966b9ea5a6SRoopa Prabhu 			if (nla) {
45976b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
45986b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
45996b9ea5a6SRoopa Prabhu 			}
46006b9ea5a6SRoopa Prabhu 		}
4601333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
46026b9ea5a6SRoopa Prabhu 		if (err)
46036b9ea5a6SRoopa Prabhu 			last_err = err;
46046b9ea5a6SRoopa Prabhu 
460551ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
460651ebd318SNicolas Dichtel 	}
460751ebd318SNicolas Dichtel 
460851ebd318SNicolas Dichtel 	return last_err;
460951ebd318SNicolas Dichtel }
461051ebd318SNicolas Dichtel 
4611c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4612c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
46131da177e4SLinus Torvalds {
461486872cb5SThomas Graf 	struct fib6_config cfg;
461586872cb5SThomas Graf 	int err;
46161da177e4SLinus Torvalds 
4617333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
461886872cb5SThomas Graf 	if (err < 0)
461986872cb5SThomas Graf 		return err;
462086872cb5SThomas Graf 
462151ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4622333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
46230ae81335SDavid Ahern 	else {
46240ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
4625333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
46261da177e4SLinus Torvalds 	}
46270ae81335SDavid Ahern }
46281da177e4SLinus Torvalds 
4629c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4630c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
46311da177e4SLinus Torvalds {
463286872cb5SThomas Graf 	struct fib6_config cfg;
463386872cb5SThomas Graf 	int err;
46341da177e4SLinus Torvalds 
4635333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
463686872cb5SThomas Graf 	if (err < 0)
463786872cb5SThomas Graf 		return err;
463886872cb5SThomas Graf 
463967f69513SDavid Ahern 	if (cfg.fc_metric == 0)
464067f69513SDavid Ahern 		cfg.fc_metric = IP6_RT_PRIO_USER;
464167f69513SDavid Ahern 
464251ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4643333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
464451ebd318SNicolas Dichtel 	else
4645acb54e3cSDavid Ahern 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
46461da177e4SLinus Torvalds }
46471da177e4SLinus Torvalds 
46488d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt)
4649339bf98fSThomas Graf {
4650beb1afacSDavid Ahern 	int nexthop_len = 0;
4651beb1afacSDavid Ahern 
465293c2fb25SDavid Ahern 	if (rt->fib6_nsiblings) {
4653beb1afacSDavid Ahern 		nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
4654beb1afacSDavid Ahern 			    + NLA_ALIGN(sizeof(struct rtnexthop))
4655beb1afacSDavid Ahern 			    + nla_total_size(16) /* RTA_GATEWAY */
4656ad1601aeSDavid Ahern 			    + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws);
4657beb1afacSDavid Ahern 
465893c2fb25SDavid Ahern 		nexthop_len *= rt->fib6_nsiblings;
4659beb1afacSDavid Ahern 	}
4660beb1afacSDavid Ahern 
4661339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
4662339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
4663339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
4664339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
4665339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
4666339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
4667339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
4668339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
4669339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
46706a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
4671ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
4672c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
467319e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
4674ad1601aeSDavid Ahern 	       + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws)
4675beb1afacSDavid Ahern 	       + nexthop_len;
4676beb1afacSDavid Ahern }
4677beb1afacSDavid Ahern 
4678d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
46798d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
4680d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
468115e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
4682f8cfe2ceSDavid Ahern 			 unsigned int flags)
46831da177e4SLinus Torvalds {
468422d0bd82SXin Long 	struct rt6_info *rt6 = (struct rt6_info *)dst;
468522d0bd82SXin Long 	struct rt6key *rt6_dst, *rt6_src;
468622d0bd82SXin Long 	u32 *pmetrics, table, rt6_flags;
46871da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
468822d0bd82SXin Long 	struct rtmsg *rtm;
4689d4ead6b3SDavid Ahern 	long expires = 0;
46901da177e4SLinus Torvalds 
469115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
469238308473SDavid S. Miller 	if (!nlh)
469326932566SPatrick McHardy 		return -EMSGSIZE;
46942d7202bfSThomas Graf 
469522d0bd82SXin Long 	if (rt6) {
469622d0bd82SXin Long 		rt6_dst = &rt6->rt6i_dst;
469722d0bd82SXin Long 		rt6_src = &rt6->rt6i_src;
469822d0bd82SXin Long 		rt6_flags = rt6->rt6i_flags;
469922d0bd82SXin Long 	} else {
470022d0bd82SXin Long 		rt6_dst = &rt->fib6_dst;
470122d0bd82SXin Long 		rt6_src = &rt->fib6_src;
470222d0bd82SXin Long 		rt6_flags = rt->fib6_flags;
470322d0bd82SXin Long 	}
470422d0bd82SXin Long 
47052d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
47061da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
470722d0bd82SXin Long 	rtm->rtm_dst_len = rt6_dst->plen;
470822d0bd82SXin Long 	rtm->rtm_src_len = rt6_src->plen;
47091da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
471093c2fb25SDavid Ahern 	if (rt->fib6_table)
471193c2fb25SDavid Ahern 		table = rt->fib6_table->tb6_id;
4712c71099acSThomas Graf 	else
47139e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
471497f0082aSKalash Nainwal 	rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
4715c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
4716c78679e8SDavid S. Miller 		goto nla_put_failure;
4717e8478e80SDavid Ahern 
4718e8478e80SDavid Ahern 	rtm->rtm_type = rt->fib6_type;
47191da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
47201da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
472193c2fb25SDavid Ahern 	rtm->rtm_protocol = rt->fib6_protocol;
47221da177e4SLinus Torvalds 
472322d0bd82SXin Long 	if (rt6_flags & RTF_CACHE)
47241da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
47251da177e4SLinus Torvalds 
4726d4ead6b3SDavid Ahern 	if (dest) {
4727d4ead6b3SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, dest))
4728c78679e8SDavid S. Miller 			goto nla_put_failure;
47291da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
47301da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
473122d0bd82SXin Long 		if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
4732c78679e8SDavid S. Miller 			goto nla_put_failure;
47331da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
47341da177e4SLinus Torvalds 	if (src) {
4735930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
4736c78679e8SDavid S. Miller 			goto nla_put_failure;
47371da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
4738c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
473922d0bd82SXin Long 		   nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
4740c78679e8SDavid S. Miller 		goto nla_put_failure;
47411da177e4SLinus Torvalds #endif
47427bc570c8SYOSHIFUJI Hideaki 	if (iif) {
47437bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
474422d0bd82SXin Long 		if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
4745fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
47462cf75070SNikolay Aleksandrov 
47477bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
47487bc570c8SYOSHIFUJI Hideaki 				return 0;
4749fd61c6baSDavid Ahern 			if (err < 0)
47507bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
47517bc570c8SYOSHIFUJI Hideaki 		} else
47527bc570c8SYOSHIFUJI Hideaki #endif
4753c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
4754c78679e8SDavid S. Miller 				goto nla_put_failure;
4755d4ead6b3SDavid Ahern 	} else if (dest) {
47561da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
4757d4ead6b3SDavid Ahern 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
4758930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4759c78679e8SDavid S. Miller 			goto nla_put_failure;
4760c3968a85SDaniel Walter 	}
4761c3968a85SDaniel Walter 
476293c2fb25SDavid Ahern 	if (rt->fib6_prefsrc.plen) {
4763c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
476493c2fb25SDavid Ahern 		saddr_buf = rt->fib6_prefsrc.addr;
4765930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4766c78679e8SDavid S. Miller 			goto nla_put_failure;
47671da177e4SLinus Torvalds 	}
47682d7202bfSThomas Graf 
4769d4ead6b3SDavid Ahern 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4770d4ead6b3SDavid Ahern 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
47712d7202bfSThomas Graf 		goto nla_put_failure;
47722d7202bfSThomas Graf 
477393c2fb25SDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
4774beb1afacSDavid Ahern 		goto nla_put_failure;
4775beb1afacSDavid Ahern 
4776beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
4777beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
4778beb1afacSDavid Ahern 	 */
477922d0bd82SXin Long 	if (rt6) {
478022d0bd82SXin Long 		if (rt6_flags & RTF_GATEWAY &&
478122d0bd82SXin Long 		    nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
478222d0bd82SXin Long 			goto nla_put_failure;
478322d0bd82SXin Long 
478422d0bd82SXin Long 		if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
478522d0bd82SXin Long 			goto nla_put_failure;
478622d0bd82SXin Long 	} else if (rt->fib6_nsiblings) {
47878d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
4788beb1afacSDavid Ahern 		struct nlattr *mp;
4789beb1afacSDavid Ahern 
4790ae0be8deSMichal Kubecek 		mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
4791beb1afacSDavid Ahern 		if (!mp)
4792beb1afacSDavid Ahern 			goto nla_put_failure;
4793beb1afacSDavid Ahern 
4794c0a72077SDavid Ahern 		if (fib_add_nexthop(skb, &rt->fib6_nh.nh_common,
4795c0a72077SDavid Ahern 				    rt->fib6_nh.fib_nh_weight) < 0)
4796beb1afacSDavid Ahern 			goto nla_put_failure;
4797beb1afacSDavid Ahern 
4798beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
479993c2fb25SDavid Ahern 					 &rt->fib6_siblings, fib6_siblings) {
4800c0a72077SDavid Ahern 			if (fib_add_nexthop(skb, &sibling->fib6_nh.nh_common,
4801c0a72077SDavid Ahern 					    sibling->fib6_nh.fib_nh_weight) < 0)
480294f826b8SEric Dumazet 				goto nla_put_failure;
480394f826b8SEric Dumazet 		}
48042d7202bfSThomas Graf 
4805beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
4806beb1afacSDavid Ahern 	} else {
4807ecc5663cSDavid Ahern 		unsigned char nh_flags = 0;
4808ecc5663cSDavid Ahern 
4809c0a72077SDavid Ahern 		if (fib_nexthop_info(skb, &rt->fib6_nh.nh_common,
4810ecc5663cSDavid Ahern 				     &nh_flags, false) < 0)
4811c78679e8SDavid S. Miller 			goto nla_put_failure;
4812ecc5663cSDavid Ahern 
4813ecc5663cSDavid Ahern 		rtm->rtm_flags |= nh_flags;
4814beb1afacSDavid Ahern 	}
48158253947eSLi Wei 
481622d0bd82SXin Long 	if (rt6_flags & RTF_EXPIRES) {
481714895687SDavid Ahern 		expires = dst ? dst->expires : rt->expires;
481814895687SDavid Ahern 		expires -= jiffies;
481914895687SDavid Ahern 	}
482069cdf8f9SYOSHIFUJI Hideaki 
4821d4ead6b3SDavid Ahern 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
4822e3703b3dSThomas Graf 		goto nla_put_failure;
48231da177e4SLinus Torvalds 
482422d0bd82SXin Long 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
4825c78ba6d6SLubomir Rintel 		goto nla_put_failure;
4826c78ba6d6SLubomir Rintel 
482719e42e45SRoopa Prabhu 
4828053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4829053c095aSJohannes Berg 	return 0;
48302d7202bfSThomas Graf 
48312d7202bfSThomas Graf nla_put_failure:
483226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
483326932566SPatrick McHardy 	return -EMSGSIZE;
48341da177e4SLinus Torvalds }
48351da177e4SLinus Torvalds 
483613e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i,
483713e38901SDavid Ahern 			       const struct net_device *dev)
483813e38901SDavid Ahern {
4839ad1601aeSDavid Ahern 	if (f6i->fib6_nh.fib_nh_dev == dev)
484013e38901SDavid Ahern 		return true;
484113e38901SDavid Ahern 
484213e38901SDavid Ahern 	if (f6i->fib6_nsiblings) {
484313e38901SDavid Ahern 		struct fib6_info *sibling, *next_sibling;
484413e38901SDavid Ahern 
484513e38901SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
484613e38901SDavid Ahern 					 &f6i->fib6_siblings, fib6_siblings) {
4847ad1601aeSDavid Ahern 			if (sibling->fib6_nh.fib_nh_dev == dev)
484813e38901SDavid Ahern 				return true;
484913e38901SDavid Ahern 		}
485013e38901SDavid Ahern 	}
485113e38901SDavid Ahern 
485213e38901SDavid Ahern 	return false;
485313e38901SDavid Ahern }
485413e38901SDavid Ahern 
48558d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg)
48561da177e4SLinus Torvalds {
48571da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
485813e38901SDavid Ahern 	struct fib_dump_filter *filter = &arg->filter;
485913e38901SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
48601f17e2f2SDavid Ahern 	struct net *net = arg->net;
48611f17e2f2SDavid Ahern 
4862421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
48631f17e2f2SDavid Ahern 		return 0;
48641da177e4SLinus Torvalds 
486513e38901SDavid Ahern 	if ((filter->flags & RTM_F_PREFIX) &&
486693c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_PREFIX_RT)) {
4867f8cfe2ceSDavid Ahern 		/* success since this is not a prefix route */
4868f8cfe2ceSDavid Ahern 		return 1;
4869f8cfe2ceSDavid Ahern 	}
487013e38901SDavid Ahern 	if (filter->filter_set) {
487113e38901SDavid Ahern 		if ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
487213e38901SDavid Ahern 		    (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
487313e38901SDavid Ahern 		    (filter->protocol && rt->fib6_protocol != filter->protocol)) {
487413e38901SDavid Ahern 			return 1;
487513e38901SDavid Ahern 		}
487613e38901SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
4877f8cfe2ceSDavid Ahern 	}
48781da177e4SLinus Torvalds 
4879d4ead6b3SDavid Ahern 	return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4880d4ead6b3SDavid Ahern 			     RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
488113e38901SDavid Ahern 			     arg->cb->nlh->nlmsg_seq, flags);
48821da177e4SLinus Torvalds }
48831da177e4SLinus Torvalds 
48840eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
48850eff0a27SJakub Kicinski 					const struct nlmsghdr *nlh,
48860eff0a27SJakub Kicinski 					struct nlattr **tb,
48870eff0a27SJakub Kicinski 					struct netlink_ext_ack *extack)
48880eff0a27SJakub Kicinski {
48890eff0a27SJakub Kicinski 	struct rtmsg *rtm;
48900eff0a27SJakub Kicinski 	int i, err;
48910eff0a27SJakub Kicinski 
48920eff0a27SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
48930eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
48940eff0a27SJakub Kicinski 				   "Invalid header for get route request");
48950eff0a27SJakub Kicinski 		return -EINVAL;
48960eff0a27SJakub Kicinski 	}
48970eff0a27SJakub Kicinski 
48980eff0a27SJakub Kicinski 	if (!netlink_strict_get_check(skb))
48998cb08174SJohannes Berg 		return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
49000eff0a27SJakub Kicinski 					      rtm_ipv6_policy, extack);
49010eff0a27SJakub Kicinski 
49020eff0a27SJakub Kicinski 	rtm = nlmsg_data(nlh);
49030eff0a27SJakub Kicinski 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
49040eff0a27SJakub Kicinski 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
49050eff0a27SJakub Kicinski 	    rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
49060eff0a27SJakub Kicinski 	    rtm->rtm_type) {
49070eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
49080eff0a27SJakub Kicinski 		return -EINVAL;
49090eff0a27SJakub Kicinski 	}
49100eff0a27SJakub Kicinski 	if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
49110eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
49120eff0a27SJakub Kicinski 				   "Invalid flags for get route request");
49130eff0a27SJakub Kicinski 		return -EINVAL;
49140eff0a27SJakub Kicinski 	}
49150eff0a27SJakub Kicinski 
49168cb08174SJohannes Berg 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
49170eff0a27SJakub Kicinski 					    rtm_ipv6_policy, extack);
49180eff0a27SJakub Kicinski 	if (err)
49190eff0a27SJakub Kicinski 		return err;
49200eff0a27SJakub Kicinski 
49210eff0a27SJakub Kicinski 	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
49220eff0a27SJakub Kicinski 	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
49230eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
49240eff0a27SJakub Kicinski 		return -EINVAL;
49250eff0a27SJakub Kicinski 	}
49260eff0a27SJakub Kicinski 
49270eff0a27SJakub Kicinski 	for (i = 0; i <= RTA_MAX; i++) {
49280eff0a27SJakub Kicinski 		if (!tb[i])
49290eff0a27SJakub Kicinski 			continue;
49300eff0a27SJakub Kicinski 
49310eff0a27SJakub Kicinski 		switch (i) {
49320eff0a27SJakub Kicinski 		case RTA_SRC:
49330eff0a27SJakub Kicinski 		case RTA_DST:
49340eff0a27SJakub Kicinski 		case RTA_IIF:
49350eff0a27SJakub Kicinski 		case RTA_OIF:
49360eff0a27SJakub Kicinski 		case RTA_MARK:
49370eff0a27SJakub Kicinski 		case RTA_UID:
49380eff0a27SJakub Kicinski 		case RTA_SPORT:
49390eff0a27SJakub Kicinski 		case RTA_DPORT:
49400eff0a27SJakub Kicinski 		case RTA_IP_PROTO:
49410eff0a27SJakub Kicinski 			break;
49420eff0a27SJakub Kicinski 		default:
49430eff0a27SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
49440eff0a27SJakub Kicinski 			return -EINVAL;
49450eff0a27SJakub Kicinski 		}
49460eff0a27SJakub Kicinski 	}
49470eff0a27SJakub Kicinski 
49480eff0a27SJakub Kicinski 	return 0;
49490eff0a27SJakub Kicinski }
49500eff0a27SJakub Kicinski 
4951c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4952c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
49531da177e4SLinus Torvalds {
49543b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
4955ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
495618c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
4957a68886a6SDavid Ahern 	struct fib6_info *from;
495818c3a61cSRoopa Prabhu 	struct dst_entry *dst;
49591da177e4SLinus Torvalds 	struct rt6_info *rt;
4960ab364a6fSThomas Graf 	struct sk_buff *skb;
4961ab364a6fSThomas Graf 	struct rtmsg *rtm;
4962744486d4SMaciej Żenczykowski 	struct flowi6 fl6 = {};
496318c3a61cSRoopa Prabhu 	bool fibmatch;
4964ab364a6fSThomas Graf 
49650eff0a27SJakub Kicinski 	err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
4966ab364a6fSThomas Graf 	if (err < 0)
4967ab364a6fSThomas Graf 		goto errout;
4968ab364a6fSThomas Graf 
4969ab364a6fSThomas Graf 	err = -EINVAL;
497038b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
497138b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
497218c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
4973ab364a6fSThomas Graf 
4974ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
4975ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4976ab364a6fSThomas Graf 			goto errout;
4977ab364a6fSThomas Graf 
49784e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
4979ab364a6fSThomas Graf 	}
4980ab364a6fSThomas Graf 
4981ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
4982ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4983ab364a6fSThomas Graf 			goto errout;
4984ab364a6fSThomas Graf 
49854e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
4986ab364a6fSThomas Graf 	}
4987ab364a6fSThomas Graf 
4988ab364a6fSThomas Graf 	if (tb[RTA_IIF])
4989ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
4990ab364a6fSThomas Graf 
4991ab364a6fSThomas Graf 	if (tb[RTA_OIF])
499272331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
4993ab364a6fSThomas Graf 
49942e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
49952e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
49962e47b291SLorenzo Colitti 
4997622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
4998622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
4999622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
5000622ec2c9SLorenzo Colitti 	else
5001622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
5002622ec2c9SLorenzo Colitti 
5003eacb9384SRoopa Prabhu 	if (tb[RTA_SPORT])
5004eacb9384SRoopa Prabhu 		fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
5005eacb9384SRoopa Prabhu 
5006eacb9384SRoopa Prabhu 	if (tb[RTA_DPORT])
5007eacb9384SRoopa Prabhu 		fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
5008eacb9384SRoopa Prabhu 
5009eacb9384SRoopa Prabhu 	if (tb[RTA_IP_PROTO]) {
5010eacb9384SRoopa Prabhu 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
50115e1a99eaSHangbin Liu 						  &fl6.flowi6_proto, AF_INET6,
50125e1a99eaSHangbin Liu 						  extack);
5013eacb9384SRoopa Prabhu 		if (err)
5014eacb9384SRoopa Prabhu 			goto errout;
5015eacb9384SRoopa Prabhu 	}
5016eacb9384SRoopa Prabhu 
5017ab364a6fSThomas Graf 	if (iif) {
5018ab364a6fSThomas Graf 		struct net_device *dev;
501972331bc0SShmulik Ladkani 		int flags = 0;
502072331bc0SShmulik Ladkani 
5021121622dbSFlorian Westphal 		rcu_read_lock();
5022121622dbSFlorian Westphal 
5023121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
5024ab364a6fSThomas Graf 		if (!dev) {
5025121622dbSFlorian Westphal 			rcu_read_unlock();
5026ab364a6fSThomas Graf 			err = -ENODEV;
5027ab364a6fSThomas Graf 			goto errout;
5028ab364a6fSThomas Graf 		}
502972331bc0SShmulik Ladkani 
503072331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
503172331bc0SShmulik Ladkani 
503272331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
503372331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
503472331bc0SShmulik Ladkani 
5035b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
5036121622dbSFlorian Westphal 
5037121622dbSFlorian Westphal 		rcu_read_unlock();
503872331bc0SShmulik Ladkani 	} else {
503972331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
504072331bc0SShmulik Ladkani 
504118c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
504218c3a61cSRoopa Prabhu 	}
504318c3a61cSRoopa Prabhu 
504418c3a61cSRoopa Prabhu 
504518c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
504618c3a61cSRoopa Prabhu 	if (rt->dst.error) {
504718c3a61cSRoopa Prabhu 		err = rt->dst.error;
504818c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
504918c3a61cSRoopa Prabhu 		goto errout;
5050ab364a6fSThomas Graf 	}
50511da177e4SLinus Torvalds 
50529d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
50539d6acb3bSWANG Cong 		err = rt->dst.error;
50549d6acb3bSWANG Cong 		ip6_rt_put(rt);
50559d6acb3bSWANG Cong 		goto errout;
50569d6acb3bSWANG Cong 	}
50579d6acb3bSWANG Cong 
50581da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
505938308473SDavid S. Miller 	if (!skb) {
506094e187c0SAmerigo Wang 		ip6_rt_put(rt);
5061ab364a6fSThomas Graf 		err = -ENOBUFS;
5062ab364a6fSThomas Graf 		goto errout;
5063ab364a6fSThomas Graf 	}
50641da177e4SLinus Torvalds 
5065d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
5066a68886a6SDavid Ahern 
5067a68886a6SDavid Ahern 	rcu_read_lock();
5068a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
5069886b7a50SMartin KaFai Lau 	if (from) {
507018c3a61cSRoopa Prabhu 		if (fibmatch)
5071886b7a50SMartin KaFai Lau 			err = rt6_fill_node(net, skb, from, NULL, NULL, NULL,
5072886b7a50SMartin KaFai Lau 					    iif, RTM_NEWROUTE,
5073886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
507418c3a61cSRoopa Prabhu 					    nlh->nlmsg_seq, 0);
507518c3a61cSRoopa Prabhu 		else
5076a68886a6SDavid Ahern 			err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5077a68886a6SDavid Ahern 					    &fl6.saddr, iif, RTM_NEWROUTE,
5078886b7a50SMartin KaFai Lau 					    NETLINK_CB(in_skb).portid,
5079886b7a50SMartin KaFai Lau 					    nlh->nlmsg_seq, 0);
5080886b7a50SMartin KaFai Lau 	} else {
5081886b7a50SMartin KaFai Lau 		err = -ENETUNREACH;
5082886b7a50SMartin KaFai Lau 	}
5083a68886a6SDavid Ahern 	rcu_read_unlock();
5084a68886a6SDavid Ahern 
50851da177e4SLinus Torvalds 	if (err < 0) {
5086ab364a6fSThomas Graf 		kfree_skb(skb);
5087ab364a6fSThomas Graf 		goto errout;
50881da177e4SLinus Torvalds 	}
50891da177e4SLinus Torvalds 
509015e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
5091ab364a6fSThomas Graf errout:
50921da177e4SLinus Torvalds 	return err;
50931da177e4SLinus Torvalds }
50941da177e4SLinus Torvalds 
50958d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
509637a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
50971da177e4SLinus Torvalds {
50981da177e4SLinus Torvalds 	struct sk_buff *skb;
50995578689aSDaniel Lezcano 	struct net *net = info->nl_net;
5100528c4cebSDenis V. Lunev 	u32 seq;
5101528c4cebSDenis V. Lunev 	int err;
51020d51aa80SJamal Hadi Salim 
5103528c4cebSDenis V. Lunev 	err = -ENOBUFS;
510438308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
510586872cb5SThomas Graf 
510619e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
510738308473SDavid S. Miller 	if (!skb)
510821713ebcSThomas Graf 		goto errout;
51091da177e4SLinus Torvalds 
5110d4ead6b3SDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5111f8cfe2ceSDavid Ahern 			    event, info->portid, seq, nlm_flags);
511226932566SPatrick McHardy 	if (err < 0) {
511326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
511426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
511526932566SPatrick McHardy 		kfree_skb(skb);
511626932566SPatrick McHardy 		goto errout;
511726932566SPatrick McHardy 	}
511815e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
51195578689aSDaniel Lezcano 		    info->nlh, gfp_any());
51201ce85fe4SPablo Neira Ayuso 	return;
512121713ebcSThomas Graf errout:
512221713ebcSThomas Graf 	if (err < 0)
51235578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
51241da177e4SLinus Torvalds }
51251da177e4SLinus Torvalds 
51268ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
5127351638e7SJiri Pirko 				unsigned long event, void *ptr)
51288ed67789SDaniel Lezcano {
5129351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5130c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
51318ed67789SDaniel Lezcano 
5132242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
5133242d3a49SWANG Cong 		return NOTIFY_OK;
5134242d3a49SWANG Cong 
5135242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
5136ad1601aeSDavid Ahern 		net->ipv6.fib6_null_entry->fib6_nh.fib_nh_dev = dev;
5137d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
51388ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
51398ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5140d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
51418ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
5142d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
51438ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
51448ed67789SDaniel Lezcano #endif
514576da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
514676da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
514776da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
514876da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
514976da0704SWANG Cong 		 */
515012d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
5151242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
515212d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
515312d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
5154242d3a49SWANG Cong #endif
51558ed67789SDaniel Lezcano 	}
51568ed67789SDaniel Lezcano 
51578ed67789SDaniel Lezcano 	return NOTIFY_OK;
51588ed67789SDaniel Lezcano }
51598ed67789SDaniel Lezcano 
51601da177e4SLinus Torvalds /*
51611da177e4SLinus Torvalds  *	/proc
51621da177e4SLinus Torvalds  */
51631da177e4SLinus Torvalds 
51641da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
51651da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
51661da177e4SLinus Torvalds {
516769ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
51681da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
516969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
517069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
517181eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
517269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
517369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
5174fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
517569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
51761da177e4SLinus Torvalds 
51771da177e4SLinus Torvalds 	return 0;
51781da177e4SLinus Torvalds }
51791da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
51801da177e4SLinus Torvalds 
51811da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
51821da177e4SLinus Torvalds 
51831da177e4SLinus Torvalds static
5184fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
51851da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
51861da177e4SLinus Torvalds {
5187c486da34SLucian Adrian Grijincu 	struct net *net;
5188c486da34SLucian Adrian Grijincu 	int delay;
5189f0fb9b28SAditya Pakki 	int ret;
5190c486da34SLucian Adrian Grijincu 	if (!write)
5191c486da34SLucian Adrian Grijincu 		return -EINVAL;
5192c486da34SLucian Adrian Grijincu 
5193c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
5194c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
5195f0fb9b28SAditya Pakki 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5196f0fb9b28SAditya Pakki 	if (ret)
5197f0fb9b28SAditya Pakki 		return ret;
5198f0fb9b28SAditya Pakki 
51992ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
52001da177e4SLinus Torvalds 	return 0;
52011da177e4SLinus Torvalds }
52021da177e4SLinus Torvalds 
52037c6bb7d2SDavid Ahern static int zero;
52047c6bb7d2SDavid Ahern static int one = 1;
52057c6bb7d2SDavid Ahern 
5206ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = {
52071da177e4SLinus Torvalds 	{
52081da177e4SLinus Torvalds 		.procname	=	"flush",
52094990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
52101da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
521189c8b3a1SDave Jones 		.mode		=	0200,
52126d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
52131da177e4SLinus Torvalds 	},
52141da177e4SLinus Torvalds 	{
52151da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
52169a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
52171da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52181da177e4SLinus Torvalds 		.mode		=	0644,
52196d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
52201da177e4SLinus Torvalds 	},
52211da177e4SLinus Torvalds 	{
52221da177e4SLinus Torvalds 		.procname	=	"max_size",
52234990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
52241da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52251da177e4SLinus Torvalds 		.mode		=	0644,
52266d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
52271da177e4SLinus Torvalds 	},
52281da177e4SLinus Torvalds 	{
52291da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
52304990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
52311da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52321da177e4SLinus Torvalds 		.mode		=	0644,
52336d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
52341da177e4SLinus Torvalds 	},
52351da177e4SLinus Torvalds 	{
52361da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
52374990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
52381da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52391da177e4SLinus Torvalds 		.mode		=	0644,
52406d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
52411da177e4SLinus Torvalds 	},
52421da177e4SLinus Torvalds 	{
52431da177e4SLinus Torvalds 		.procname	=	"gc_interval",
52444990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
52451da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52461da177e4SLinus Torvalds 		.mode		=	0644,
52476d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
52481da177e4SLinus Torvalds 	},
52491da177e4SLinus Torvalds 	{
52501da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
52514990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
52521da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52531da177e4SLinus Torvalds 		.mode		=	0644,
5254f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
52551da177e4SLinus Torvalds 	},
52561da177e4SLinus Torvalds 	{
52571da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
52584990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
52591da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52601da177e4SLinus Torvalds 		.mode		=	0644,
52616d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
52621da177e4SLinus Torvalds 	},
52631da177e4SLinus Torvalds 	{
52641da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
52654990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
52661da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52671da177e4SLinus Torvalds 		.mode		=	0644,
5268f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
52691da177e4SLinus Torvalds 	},
52701da177e4SLinus Torvalds 	{
52711da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
52724990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
52731da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52741da177e4SLinus Torvalds 		.mode		=	0644,
52756d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
52761da177e4SLinus Torvalds 	},
52777c6bb7d2SDavid Ahern 	{
52787c6bb7d2SDavid Ahern 		.procname	=	"skip_notify_on_dev_down",
52797c6bb7d2SDavid Ahern 		.data		=	&init_net.ipv6.sysctl.skip_notify_on_dev_down,
52807c6bb7d2SDavid Ahern 		.maxlen		=	sizeof(int),
52817c6bb7d2SDavid Ahern 		.mode		=	0644,
52827c6bb7d2SDavid Ahern 		.proc_handler	=	proc_dointvec,
52837c6bb7d2SDavid Ahern 		.extra1		=	&zero,
52847c6bb7d2SDavid Ahern 		.extra2		=	&one,
52857c6bb7d2SDavid Ahern 	},
5286f8572d8fSEric W. Biederman 	{ }
52871da177e4SLinus Torvalds };
52881da177e4SLinus Torvalds 
52892c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
5290760f2d01SDaniel Lezcano {
5291760f2d01SDaniel Lezcano 	struct ctl_table *table;
5292760f2d01SDaniel Lezcano 
5293760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
5294760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
5295760f2d01SDaniel Lezcano 			GFP_KERNEL);
52965ee09105SYOSHIFUJI Hideaki 
52975ee09105SYOSHIFUJI Hideaki 	if (table) {
52985ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
5299c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
530086393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
53015ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
53025ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
53035ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
53045ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
53055ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
53065ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
53075ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
53089c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
53097c6bb7d2SDavid Ahern 		table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
5310464dc801SEric W. Biederman 
5311464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
5312464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
5313464dc801SEric W. Biederman 			table[0].procname = NULL;
53145ee09105SYOSHIFUJI Hideaki 	}
53155ee09105SYOSHIFUJI Hideaki 
5316760f2d01SDaniel Lezcano 	return table;
5317760f2d01SDaniel Lezcano }
53181da177e4SLinus Torvalds #endif
53191da177e4SLinus Torvalds 
53202c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
5321cdb18761SDaniel Lezcano {
5322633d424bSPavel Emelyanov 	int ret = -ENOMEM;
53238ed67789SDaniel Lezcano 
532486393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
532586393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
5326f2fc6a54SBenjamin Thery 
5327fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5328fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
5329fc66f95cSEric Dumazet 
5330421842edSDavid Ahern 	net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template,
5331421842edSDavid Ahern 					    sizeof(*net->ipv6.fib6_null_entry),
5332421842edSDavid Ahern 					    GFP_KERNEL);
5333421842edSDavid Ahern 	if (!net->ipv6.fib6_null_entry)
5334421842edSDavid Ahern 		goto out_ip6_dst_entries;
5335421842edSDavid Ahern 
53368ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
53378ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
53388ed67789SDaniel Lezcano 					   GFP_KERNEL);
53398ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
5340421842edSDavid Ahern 		goto out_fib6_null_entry;
5341d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
534262fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
534362fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
53448ed67789SDaniel Lezcano 
53458ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5346feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
53478ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
53488ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
53498ed67789SDaniel Lezcano 					       GFP_KERNEL);
535068fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
535168fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
5352d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
535362fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
535462fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
53558ed67789SDaniel Lezcano 
53568ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
53578ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
53588ed67789SDaniel Lezcano 					       GFP_KERNEL);
535968fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
536068fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
5361d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
536262fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
536362fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
53648ed67789SDaniel Lezcano #endif
53658ed67789SDaniel Lezcano 
5366b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
5367b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
5368b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5369b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5370b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5371b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5372b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5373b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
53747c6bb7d2SDavid Ahern 	net->ipv6.sysctl.skip_notify_on_dev_down = 0;
5375b339a47cSPeter Zijlstra 
53766891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
53776891a346SBenjamin Thery 
53788ed67789SDaniel Lezcano 	ret = 0;
53798ed67789SDaniel Lezcano out:
53808ed67789SDaniel Lezcano 	return ret;
5381f2fc6a54SBenjamin Thery 
538268fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
538368fffc67SPeter Zijlstra out_ip6_prohibit_entry:
538468fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
538568fffc67SPeter Zijlstra out_ip6_null_entry:
538668fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
538768fffc67SPeter Zijlstra #endif
5388421842edSDavid Ahern out_fib6_null_entry:
5389421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
5390fc66f95cSEric Dumazet out_ip6_dst_entries:
5391fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5392f2fc6a54SBenjamin Thery out_ip6_dst_ops:
5393f2fc6a54SBenjamin Thery 	goto out;
5394cdb18761SDaniel Lezcano }
5395cdb18761SDaniel Lezcano 
53962c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
5397cdb18761SDaniel Lezcano {
5398421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
53998ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
54008ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
54018ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
54028ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
54038ed67789SDaniel Lezcano #endif
540441bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5405cdb18761SDaniel Lezcano }
5406cdb18761SDaniel Lezcano 
5407d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
5408d189634eSThomas Graf {
5409d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5410c3506372SChristoph Hellwig 	proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
5411c3506372SChristoph Hellwig 			sizeof(struct ipv6_route_iter));
54123617d949SChristoph Hellwig 	proc_create_net_single("rt6_stats", 0444, net->proc_net,
54133617d949SChristoph Hellwig 			rt6_stats_seq_show, NULL);
5414d189634eSThomas Graf #endif
5415d189634eSThomas Graf 	return 0;
5416d189634eSThomas Graf }
5417d189634eSThomas Graf 
5418d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
5419d189634eSThomas Graf {
5420d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5421ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
5422ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
5423d189634eSThomas Graf #endif
5424d189634eSThomas Graf }
5425d189634eSThomas Graf 
5426cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
5427cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
5428cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
5429cdb18761SDaniel Lezcano };
5430cdb18761SDaniel Lezcano 
5431c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
5432c3426b47SDavid S. Miller {
5433c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5434c3426b47SDavid S. Miller 
5435c3426b47SDavid S. Miller 	if (!bp)
5436c3426b47SDavid S. Miller 		return -ENOMEM;
5437c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
5438c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
5439c3426b47SDavid S. Miller 	return 0;
5440c3426b47SDavid S. Miller }
5441c3426b47SDavid S. Miller 
5442c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
5443c3426b47SDavid S. Miller {
5444c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
5445c3426b47SDavid S. Miller 
5446c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
544756a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
5448c3426b47SDavid S. Miller 	kfree(bp);
5449c3426b47SDavid S. Miller }
5450c3426b47SDavid S. Miller 
54512b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
5452c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
5453c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
5454c3426b47SDavid S. Miller };
5455c3426b47SDavid S. Miller 
5456d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
5457d189634eSThomas Graf 	.init = ip6_route_net_init_late,
5458d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
5459d189634eSThomas Graf };
5460d189634eSThomas Graf 
54618ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
54628ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
5463242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
54648ed67789SDaniel Lezcano };
54658ed67789SDaniel Lezcano 
54662f460933SWANG Cong void __init ip6_route_init_special_entries(void)
54672f460933SWANG Cong {
54682f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
54692f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
54702f460933SWANG Cong 	 * manually for init_net */
5471ad1601aeSDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_nh.fib_nh_dev = init_net.loopback_dev;
54722f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
54732f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
54742f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
54752f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
54762f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
54772f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
54782f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
54792f460933SWANG Cong   #endif
54802f460933SWANG Cong }
54812f460933SWANG Cong 
5482433d49c3SDaniel Lezcano int __init ip6_route_init(void)
54831da177e4SLinus Torvalds {
5484433d49c3SDaniel Lezcano 	int ret;
54858d0b94afSMartin KaFai Lau 	int cpu;
5486433d49c3SDaniel Lezcano 
54879a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
54889a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
54899a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
54909a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
54919a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
5492c19a28e1SFernando Carrijo 		goto out;
549314e50e57SDavid S. Miller 
5494fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
54958ed67789SDaniel Lezcano 	if (ret)
5496bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
5497bdb3289fSDaniel Lezcano 
5498c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5499c3426b47SDavid S. Miller 	if (ret)
5500e8803b6cSDavid S. Miller 		goto out_dst_entries;
55012a0c451aSThomas Graf 
55027e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
55037e52b33bSDavid S. Miller 	if (ret)
55047e52b33bSDavid S. Miller 		goto out_register_inetpeer;
5505c3426b47SDavid S. Miller 
55065dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
55075dc121e9SArnaud Ebalard 
5508e8803b6cSDavid S. Miller 	ret = fib6_init();
5509433d49c3SDaniel Lezcano 	if (ret)
55108ed67789SDaniel Lezcano 		goto out_register_subsys;
5511433d49c3SDaniel Lezcano 
5512433d49c3SDaniel Lezcano 	ret = xfrm6_init();
5513433d49c3SDaniel Lezcano 	if (ret)
5514e8803b6cSDavid S. Miller 		goto out_fib6_init;
5515c35b7e72SDaniel Lezcano 
5516433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
5517433d49c3SDaniel Lezcano 	if (ret)
5518433d49c3SDaniel Lezcano 		goto xfrm6_init;
55197e5449c2SDaniel Lezcano 
5520d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
5521d189634eSThomas Graf 	if (ret)
5522d189634eSThomas Graf 		goto fib6_rules_init;
5523d189634eSThomas Graf 
552416feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
552516feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
552616feebcfSFlorian Westphal 	if (ret < 0)
552716feebcfSFlorian Westphal 		goto out_register_late_subsys;
552816feebcfSFlorian Westphal 
552916feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
553016feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
553116feebcfSFlorian Westphal 	if (ret < 0)
553216feebcfSFlorian Westphal 		goto out_register_late_subsys;
553316feebcfSFlorian Westphal 
553416feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
553516feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
553616feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
553716feebcfSFlorian Westphal 	if (ret < 0)
5538d189634eSThomas Graf 		goto out_register_late_subsys;
5539433d49c3SDaniel Lezcano 
55408ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
5541cdb18761SDaniel Lezcano 	if (ret)
5542d189634eSThomas Graf 		goto out_register_late_subsys;
55438ed67789SDaniel Lezcano 
55448d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
55458d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
55468d0b94afSMartin KaFai Lau 
55478d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
55488d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
55498d0b94afSMartin KaFai Lau 	}
55508d0b94afSMartin KaFai Lau 
5551433d49c3SDaniel Lezcano out:
5552433d49c3SDaniel Lezcano 	return ret;
5553433d49c3SDaniel Lezcano 
5554d189634eSThomas Graf out_register_late_subsys:
555516feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
5556d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5557433d49c3SDaniel Lezcano fib6_rules_init:
5558433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
5559433d49c3SDaniel Lezcano xfrm6_init:
5560433d49c3SDaniel Lezcano 	xfrm6_fini();
55612a0c451aSThomas Graf out_fib6_init:
55622a0c451aSThomas Graf 	fib6_gc_cleanup();
55638ed67789SDaniel Lezcano out_register_subsys:
55648ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
55657e52b33bSDavid S. Miller out_register_inetpeer:
55667e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
5567fc66f95cSEric Dumazet out_dst_entries:
5568fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5569433d49c3SDaniel Lezcano out_kmem_cache:
5570f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
5571433d49c3SDaniel Lezcano 	goto out;
55721da177e4SLinus Torvalds }
55731da177e4SLinus Torvalds 
55741da177e4SLinus Torvalds void ip6_route_cleanup(void)
55751da177e4SLinus Torvalds {
55768ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
5577d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5578101367c2SThomas Graf 	fib6_rules_cleanup();
55791da177e4SLinus Torvalds 	xfrm6_fini();
55801da177e4SLinus Torvalds 	fib6_gc_cleanup();
5581c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
55828ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
558341bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5584f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
55851da177e4SLinus Torvalds }
5586