xref: /openbmc/linux/net/ipv6/route.c (revision 9c69a13205151c0d801de9f9d83a818e6e8f60ec)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Linux INET6 implementation
31da177e4SLinus Torvalds  *	FIB front-end.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
91da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
101da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
111da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds /*	Changes:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI
171da177e4SLinus Torvalds  *		reworked default router selection.
181da177e4SLinus Torvalds  *		- respect outgoing interface
191da177e4SLinus Torvalds  *		- select from (probably) reachable routers (i.e.
201da177e4SLinus Torvalds  *		routers in REACHABLE, STALE, DELAY or PROBE states).
211da177e4SLinus Torvalds  *		- always select the same router if it is (probably)
221da177e4SLinus Torvalds  *		reachable.  otherwise, round-robin the list.
23c0bece9fSYOSHIFUJI Hideaki  *	Ville Nuorvala
24c0bece9fSYOSHIFUJI Hideaki  *		Fixed routing subtrees.
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
28f3213831SJoe Perches 
294fc268d2SRandy Dunlap #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/errno.h>
31bc3b2d7fSPaul Gortmaker #include <linux/export.h>
321da177e4SLinus Torvalds #include <linux/types.h>
331da177e4SLinus Torvalds #include <linux/times.h>
341da177e4SLinus Torvalds #include <linux/socket.h>
351da177e4SLinus Torvalds #include <linux/sockios.h>
361da177e4SLinus Torvalds #include <linux/net.h>
371da177e4SLinus Torvalds #include <linux/route.h>
381da177e4SLinus Torvalds #include <linux/netdevice.h>
391da177e4SLinus Torvalds #include <linux/in6.h>
407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
411da177e4SLinus Torvalds #include <linux/init.h>
421da177e4SLinus Torvalds #include <linux/if_arp.h>
431da177e4SLinus Torvalds #include <linux/proc_fs.h>
441da177e4SLinus Torvalds #include <linux/seq_file.h>
455b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
4735732d01SWei Wang #include <linux/jhash.h>
48457c4cbcSEric W. Biederman #include <net/net_namespace.h>
491da177e4SLinus Torvalds #include <net/snmp.h>
501da177e4SLinus Torvalds #include <net/ipv6.h>
511da177e4SLinus Torvalds #include <net/ip6_fib.h>
521da177e4SLinus Torvalds #include <net/ip6_route.h>
531da177e4SLinus Torvalds #include <net/ndisc.h>
541da177e4SLinus Torvalds #include <net/addrconf.h>
551da177e4SLinus Torvalds #include <net/tcp.h>
561da177e4SLinus Torvalds #include <linux/rtnetlink.h>
571da177e4SLinus Torvalds #include <net/dst.h>
58904af04dSJiri Benc #include <net/dst_metadata.h>
591da177e4SLinus Torvalds #include <net/xfrm.h>
608d71740cSTom Tucker #include <net/netevent.h>
6121713ebcSThomas Graf #include <net/netlink.h>
6251ebd318SNicolas Dichtel #include <net/nexthop.h>
6319e42e45SRoopa Prabhu #include <net/lwtunnel.h>
64904af04dSJiri Benc #include <net/ip_tunnels.h>
65ca254490SDavid Ahern #include <net/l3mdev.h>
66eacb9384SRoopa Prabhu #include <net/ip.h>
677c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
701da177e4SLinus Torvalds #include <linux/sysctl.h>
711da177e4SLinus Torvalds #endif
721da177e4SLinus Torvalds 
7330d444d3SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type);
7430d444d3SDavid Ahern 
7530d444d3SDavid Ahern #define CREATE_TRACE_POINTS
7630d444d3SDavid Ahern #include <trace/events/fib6.h>
7730d444d3SDavid Ahern EXPORT_TRACEPOINT_SYMBOL_GPL(fib6_table_lookup);
7830d444d3SDavid Ahern #undef CREATE_TRACE_POINTS
7930d444d3SDavid Ahern 
80afc154e9SHannes Frederic Sowa enum rt6_nud_state {
817e980569SJiri Benc 	RT6_NUD_FAIL_HARD = -3,
827e980569SJiri Benc 	RT6_NUD_FAIL_PROBE = -2,
837e980569SJiri Benc 	RT6_NUD_FAIL_DO_RR = -1,
84afc154e9SHannes Frederic Sowa 	RT6_NUD_SUCCEED = 1
85afc154e9SHannes Frederic Sowa };
86afc154e9SHannes Frederic Sowa 
871da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
880dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
89ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
901da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
911da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
921da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
931da177e4SLinus Torvalds 				       struct net_device *dev, int how);
94569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
97ede2059dSEric W. Biederman static int		ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
987150aedeSKamala R static int		ip6_pkt_prohibit(struct sk_buff *skb);
99ede2059dSEric W. Biederman static int		ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
1001da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
1016700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
1026700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
1036700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
1046700c270SDavid S. Miller 					struct sk_buff *skb);
1058d1c802bSDavid Ahern static int rt6_score_route(struct fib6_info *rt, int oif, int strict);
1068d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt);
107d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
1088d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
109d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
11016a16cd3SDavid Ahern 			 int iif, int type, u32 portid, u32 seq,
11116a16cd3SDavid Ahern 			 unsigned int flags);
1128d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
11335732d01SWei Wang 					   struct in6_addr *daddr,
11435732d01SWei Wang 					   struct in6_addr *saddr);
1151da177e4SLinus Torvalds 
11670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1178d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
118b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
119830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
120830218c1SDavid Ahern 					   struct net_device *dev,
12195c96174SEric Dumazet 					   unsigned int pref);
1228d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
123b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
124830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
125830218c1SDavid Ahern 					   struct net_device *dev);
12670ceb4f5SYOSHIFUJI Hideaki #endif
12770ceb4f5SYOSHIFUJI Hideaki 
1288d0b94afSMartin KaFai Lau struct uncached_list {
1298d0b94afSMartin KaFai Lau 	spinlock_t		lock;
1308d0b94afSMartin KaFai Lau 	struct list_head	head;
1318d0b94afSMartin KaFai Lau };
1328d0b94afSMartin KaFai Lau 
1338d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
1348d0b94afSMartin KaFai Lau 
135510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt)
1368d0b94afSMartin KaFai Lau {
1378d0b94afSMartin KaFai Lau 	struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
1388d0b94afSMartin KaFai Lau 
1398d0b94afSMartin KaFai Lau 	rt->rt6i_uncached_list = ul;
1408d0b94afSMartin KaFai Lau 
1418d0b94afSMartin KaFai Lau 	spin_lock_bh(&ul->lock);
1428d0b94afSMartin KaFai Lau 	list_add_tail(&rt->rt6i_uncached, &ul->head);
1438d0b94afSMartin KaFai Lau 	spin_unlock_bh(&ul->lock);
1448d0b94afSMartin KaFai Lau }
1458d0b94afSMartin KaFai Lau 
146510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt)
1478d0b94afSMartin KaFai Lau {
1488d0b94afSMartin KaFai Lau 	if (!list_empty(&rt->rt6i_uncached)) {
1498d0b94afSMartin KaFai Lau 		struct uncached_list *ul = rt->rt6i_uncached_list;
15081eb8447SWei Wang 		struct net *net = dev_net(rt->dst.dev);
1518d0b94afSMartin KaFai Lau 
1528d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1538d0b94afSMartin KaFai Lau 		list_del(&rt->rt6i_uncached);
15481eb8447SWei Wang 		atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
1558d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1568d0b94afSMartin KaFai Lau 	}
1578d0b94afSMartin KaFai Lau }
1588d0b94afSMartin KaFai Lau 
1598d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
1608d0b94afSMartin KaFai Lau {
1618d0b94afSMartin KaFai Lau 	struct net_device *loopback_dev = net->loopback_dev;
1628d0b94afSMartin KaFai Lau 	int cpu;
1638d0b94afSMartin KaFai Lau 
164e332bc67SEric W. Biederman 	if (dev == loopback_dev)
165e332bc67SEric W. Biederman 		return;
166e332bc67SEric W. Biederman 
1678d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
1688d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
1698d0b94afSMartin KaFai Lau 		struct rt6_info *rt;
1708d0b94afSMartin KaFai Lau 
1718d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1728d0b94afSMartin KaFai Lau 		list_for_each_entry(rt, &ul->head, rt6i_uncached) {
1738d0b94afSMartin KaFai Lau 			struct inet6_dev *rt_idev = rt->rt6i_idev;
1748d0b94afSMartin KaFai Lau 			struct net_device *rt_dev = rt->dst.dev;
1758d0b94afSMartin KaFai Lau 
176e332bc67SEric W. Biederman 			if (rt_idev->dev == dev) {
1778d0b94afSMartin KaFai Lau 				rt->rt6i_idev = in6_dev_get(loopback_dev);
1788d0b94afSMartin KaFai Lau 				in6_dev_put(rt_idev);
1798d0b94afSMartin KaFai Lau 			}
1808d0b94afSMartin KaFai Lau 
181e332bc67SEric W. Biederman 			if (rt_dev == dev) {
1828d0b94afSMartin KaFai Lau 				rt->dst.dev = loopback_dev;
1838d0b94afSMartin KaFai Lau 				dev_hold(rt->dst.dev);
1848d0b94afSMartin KaFai Lau 				dev_put(rt_dev);
1858d0b94afSMartin KaFai Lau 			}
1868d0b94afSMartin KaFai Lau 		}
1878d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1888d0b94afSMartin KaFai Lau 	}
1898d0b94afSMartin KaFai Lau }
1908d0b94afSMartin KaFai Lau 
191f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p,
192f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
193f894cbf8SDavid S. Miller 					     const void *daddr)
19439232973SDavid S. Miller {
195a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
19639232973SDavid S. Miller 		return (const void *) p;
197f894cbf8SDavid S. Miller 	else if (skb)
198f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
19939232973SDavid S. Miller 	return daddr;
20039232973SDavid S. Miller }
20139232973SDavid S. Miller 
202f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
203f8a1b43bSDavid Ahern 				   struct net_device *dev,
204f894cbf8SDavid S. Miller 				   struct sk_buff *skb,
205f894cbf8SDavid S. Miller 				   const void *daddr)
206d3aaeb38SDavid S. Miller {
20739232973SDavid S. Miller 	struct neighbour *n;
20839232973SDavid S. Miller 
209f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(gw, skb, daddr);
210f8a1b43bSDavid Ahern 	n = __ipv6_neigh_lookup(dev, daddr);
211f83c7790SDavid S. Miller 	if (n)
212f83c7790SDavid S. Miller 		return n;
2137adf3246SStefano Brivio 
2147adf3246SStefano Brivio 	n = neigh_create(&nd_tbl, daddr, dev);
2157adf3246SStefano Brivio 	return IS_ERR(n) ? NULL : n;
216f8a1b43bSDavid Ahern }
217f8a1b43bSDavid Ahern 
218f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
219f8a1b43bSDavid Ahern 					      struct sk_buff *skb,
220f8a1b43bSDavid Ahern 					      const void *daddr)
221f8a1b43bSDavid Ahern {
222f8a1b43bSDavid Ahern 	const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
223f8a1b43bSDavid Ahern 
224f8a1b43bSDavid Ahern 	return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr);
225f83c7790SDavid S. Miller }
226f83c7790SDavid S. Miller 
22763fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
22863fca65dSJulian Anastasov {
22963fca65dSJulian Anastasov 	struct net_device *dev = dst->dev;
23063fca65dSJulian Anastasov 	struct rt6_info *rt = (struct rt6_info *)dst;
23163fca65dSJulian Anastasov 
232f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
23363fca65dSJulian Anastasov 	if (!daddr)
23463fca65dSJulian Anastasov 		return;
23563fca65dSJulian Anastasov 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
23663fca65dSJulian Anastasov 		return;
23763fca65dSJulian Anastasov 	if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
23863fca65dSJulian Anastasov 		return;
23963fca65dSJulian Anastasov 	__ipv6_confirm_neigh(dev, daddr);
24063fca65dSJulian Anastasov }
24163fca65dSJulian Anastasov 
2429a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
2431da177e4SLinus Torvalds 	.family			=	AF_INET6,
2441da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
2451da177e4SLinus Torvalds 	.gc_thresh		=	1024,
2461da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
2470dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
248ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
249d4ead6b3SDavid Ahern 	.cow_metrics		=	dst_cow_metrics_generic,
2501da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
2511da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
2521da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
2531da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
2541da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
2556e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
2569f8955ccSEric W. Biederman 	.local_out		=	__ip6_local_out,
257f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
25863fca65dSJulian Anastasov 	.confirm_neigh		=	ip6_confirm_neigh,
2591da177e4SLinus Torvalds };
2601da177e4SLinus Torvalds 
261ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
262ec831ea7SRoland Dreier {
263618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
264618f9bc7SSteffen Klassert 
265618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
266ec831ea7SRoland Dreier }
267ec831ea7SRoland Dreier 
2686700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
2696700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
27014e50e57SDavid S. Miller {
27114e50e57SDavid S. Miller }
27214e50e57SDavid S. Miller 
2736700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
2746700c270SDavid S. Miller 				      struct sk_buff *skb)
275b587ee3bSDavid S. Miller {
276b587ee3bSDavid S. Miller }
277b587ee3bSDavid S. Miller 
27814e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
27914e50e57SDavid S. Miller 	.family			=	AF_INET6,
28014e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
28114e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
282ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
283214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
28414e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
285b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2860a1f5962SMartin KaFai Lau 	.cow_metrics		=	dst_cow_metrics_generic,
287f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
28814e50e57SDavid S. Miller };
28914e50e57SDavid S. Miller 
29062fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
29114edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
29262fa8a84SDavid S. Miller };
29362fa8a84SDavid S. Miller 
2948d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = {
29593c2fb25SDavid Ahern 	.fib6_flags	= (RTF_REJECT | RTF_NONEXTHOP),
29693c2fb25SDavid Ahern 	.fib6_protocol  = RTPROT_KERNEL,
29793c2fb25SDavid Ahern 	.fib6_metric	= ~(u32)0,
29893c2fb25SDavid Ahern 	.fib6_ref	= ATOMIC_INIT(1),
299421842edSDavid Ahern 	.fib6_type	= RTN_UNREACHABLE,
300421842edSDavid Ahern 	.fib6_metrics	= (struct dst_metrics *)&dst_default_metrics,
301421842edSDavid Ahern };
302421842edSDavid Ahern 
303fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
3041da177e4SLinus Torvalds 	.dst = {
3051da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
3061da177e4SLinus Torvalds 		.__use		= 1,
3072c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
3081da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
3091da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
3101da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
3111da177e4SLinus Torvalds 	},
3121da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3131da177e4SLinus Torvalds };
3141da177e4SLinus Torvalds 
315101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
316101367c2SThomas Graf 
317fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
318101367c2SThomas Graf 	.dst = {
319101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
320101367c2SThomas Graf 		.__use		= 1,
3212c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
322101367c2SThomas Graf 		.error		= -EACCES,
3239ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
3249ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
325101367c2SThomas Graf 	},
326101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
327101367c2SThomas Graf };
328101367c2SThomas Graf 
329fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
330101367c2SThomas Graf 	.dst = {
331101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
332101367c2SThomas Graf 		.__use		= 1,
3332c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
334101367c2SThomas Graf 		.error		= -EINVAL,
335352e512cSHerbert Xu 		.input		= dst_discard,
336ede2059dSEric W. Biederman 		.output		= dst_discard_out,
337101367c2SThomas Graf 	},
338101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
339101367c2SThomas Graf };
340101367c2SThomas Graf 
341101367c2SThomas Graf #endif
342101367c2SThomas Graf 
343ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt)
344ebfa45f0SMartin KaFai Lau {
345ebfa45f0SMartin KaFai Lau 	struct dst_entry *dst = &rt->dst;
346ebfa45f0SMartin KaFai Lau 
347ebfa45f0SMartin KaFai Lau 	memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
348ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_uncached);
349ebfa45f0SMartin KaFai Lau }
350ebfa45f0SMartin KaFai Lau 
3511da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
35293531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
353ad706862SMartin KaFai Lau 			       int flags)
3541da177e4SLinus Torvalds {
35597bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
356b2a9c0edSWei Wang 					1, DST_OBSOLETE_FORCE_CHK, flags);
357cf911662SDavid S. Miller 
35881eb8447SWei Wang 	if (rt) {
359ebfa45f0SMartin KaFai Lau 		rt6_info_init(rt);
36081eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
36181eb8447SWei Wang 	}
3628104891bSSteffen Klassert 
363cf911662SDavid S. Miller 	return rt;
3641da177e4SLinus Torvalds }
3659ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc);
366d52d3997SMartin KaFai Lau 
3671da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
370a68886a6SDavid Ahern 	struct fib6_info *from;
3718d0b94afSMartin KaFai Lau 	struct inet6_dev *idev;
3721da177e4SLinus Torvalds 
3731620a336SDavid Ahern 	ip_dst_metrics_put(dst);
3748d0b94afSMartin KaFai Lau 	rt6_uncached_list_del(rt);
3758d0b94afSMartin KaFai Lau 
3768d0b94afSMartin KaFai Lau 	idev = rt->rt6i_idev;
37738308473SDavid S. Miller 	if (idev) {
3781da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3791da177e4SLinus Torvalds 		in6_dev_put(idev);
3801da177e4SLinus Torvalds 	}
3811716a961SGao feng 
382a68886a6SDavid Ahern 	rcu_read_lock();
383a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
384a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, NULL);
38593531c67SDavid Ahern 	fib6_info_release(from);
386a68886a6SDavid Ahern 	rcu_read_unlock();
387b3419363SDavid S. Miller }
388b3419363SDavid S. Miller 
3891da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3901da177e4SLinus Torvalds 			   int how)
3911da177e4SLinus Torvalds {
3921da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3931da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3945a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
395c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3961da177e4SLinus Torvalds 
397e5645f51SWei Wang 	if (idev && idev->dev != loopback_dev) {
398e5645f51SWei Wang 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
39938308473SDavid S. Miller 		if (loopback_idev) {
4001da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
4011da177e4SLinus Torvalds 			in6_dev_put(idev);
4021da177e4SLinus Torvalds 		}
4031da177e4SLinus Torvalds 	}
40497cac082SDavid S. Miller }
4051da177e4SLinus Torvalds 
4065973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt)
4075973fb1eSMartin KaFai Lau {
4085973fb1eSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_EXPIRES)
4095973fb1eSMartin KaFai Lau 		return time_after(jiffies, rt->dst.expires);
4105973fb1eSMartin KaFai Lau 	else
4115973fb1eSMartin KaFai Lau 		return false;
4125973fb1eSMartin KaFai Lau }
4135973fb1eSMartin KaFai Lau 
414a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
4151da177e4SLinus Torvalds {
416a68886a6SDavid Ahern 	struct fib6_info *from;
417a68886a6SDavid Ahern 
418a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
419a68886a6SDavid Ahern 
4201716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
4211716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
422a50feda5SEric Dumazet 			return true;
423a68886a6SDavid Ahern 	} else if (from) {
4241e2ea8adSXin Long 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
425a68886a6SDavid Ahern 			fib6_check_expired(from);
4261716a961SGao feng 	}
427a50feda5SEric Dumazet 	return false;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4303b290a31SDavid Ahern struct fib6_info *fib6_multipath_select(const struct net *net,
4318d1c802bSDavid Ahern 					struct fib6_info *match,
43252bd4c0cSNicolas Dichtel 					struct flowi6 *fl6, int oif,
433b75cc8f9SDavid Ahern 					const struct sk_buff *skb,
43452bd4c0cSNicolas Dichtel 					int strict)
43551ebd318SNicolas Dichtel {
4368d1c802bSDavid Ahern 	struct fib6_info *sibling, *next_sibling;
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 
4445e670d84SDavid Ahern 	if (fl6->mp_hash <= atomic_read(&match->fib6_nh.nh_upper_bound))
4453d709f69SIdo Schimmel 		return match;
446bbfcd776SIdo Schimmel 
44793c2fb25SDavid Ahern 	list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
44893c2fb25SDavid Ahern 				 fib6_siblings) {
4495e670d84SDavid Ahern 		int nh_upper_bound;
4505e670d84SDavid Ahern 
4515e670d84SDavid Ahern 		nh_upper_bound = atomic_read(&sibling->fib6_nh.nh_upper_bound);
4525e670d84SDavid Ahern 		if (fl6->mp_hash > nh_upper_bound)
4533d709f69SIdo Schimmel 			continue;
45452bd4c0cSNicolas Dichtel 		if (rt6_score_route(sibling, oif, strict) < 0)
45552bd4c0cSNicolas Dichtel 			break;
45651ebd318SNicolas Dichtel 		match = sibling;
45751ebd318SNicolas Dichtel 		break;
45851ebd318SNicolas Dichtel 	}
4593d709f69SIdo Schimmel 
46051ebd318SNicolas Dichtel 	return match;
46151ebd318SNicolas Dichtel }
46251ebd318SNicolas Dichtel 
4631da177e4SLinus Torvalds /*
46466f5d6ceSWei Wang  *	Route lookup. rcu_read_lock() should be held.
4651da177e4SLinus Torvalds  */
4661da177e4SLinus Torvalds 
4678d1c802bSDavid Ahern static inline struct fib6_info *rt6_device_match(struct net *net,
4688d1c802bSDavid Ahern 						 struct fib6_info *rt,
469b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4701da177e4SLinus Torvalds 						    int oif,
471d420895eSYOSHIFUJI Hideaki 						    int flags)
4721da177e4SLinus Torvalds {
4738d1c802bSDavid Ahern 	struct fib6_info *sprt;
4741da177e4SLinus Torvalds 
4755e670d84SDavid Ahern 	if (!oif && ipv6_addr_any(saddr) &&
4765e670d84SDavid Ahern 	    !(rt->fib6_nh.nh_flags & RTNH_F_DEAD))
4778067bb8cSIdo Schimmel 		return rt;
478dd3abc4eSYOSHIFUJI Hideaki 
4798fb11a9aSDavid Ahern 	for (sprt = rt; sprt; sprt = rcu_dereference(sprt->fib6_next)) {
4805e670d84SDavid Ahern 		const struct net_device *dev = sprt->fib6_nh.nh_dev;
481dd3abc4eSYOSHIFUJI Hideaki 
4825e670d84SDavid Ahern 		if (sprt->fib6_nh.nh_flags & RTNH_F_DEAD)
4838067bb8cSIdo Schimmel 			continue;
4848067bb8cSIdo Schimmel 
485dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4861da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4871da177e4SLinus Torvalds 				return sprt;
488dd3abc4eSYOSHIFUJI Hideaki 		} else {
489dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
490dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
491dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
492dd3abc4eSYOSHIFUJI Hideaki 		}
4931da177e4SLinus Torvalds 	}
4941da177e4SLinus Torvalds 
495eea68cd3SDavid Ahern 	if (oif && flags & RT6_LOOKUP_F_IFACE)
496421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
4971da177e4SLinus Torvalds 
498421842edSDavid Ahern 	return rt->fib6_nh.nh_flags & RTNH_F_DEAD ? net->ipv6.fib6_null_entry : rt;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
50127097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
502c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
503c2f17e82SHannes Frederic Sowa 	struct work_struct work;
504c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
505c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
506c2f17e82SHannes Frederic Sowa };
507c2f17e82SHannes Frederic Sowa 
508c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
509c2f17e82SHannes Frederic Sowa {
510c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
511c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
512c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
513c2f17e82SHannes Frederic Sowa 
514c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
515adc176c5SErik Nordmark 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
516c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
517662f5533SMichael Büsch 	kfree(work);
518c2f17e82SHannes Frederic Sowa }
519c2f17e82SHannes Frederic Sowa 
5208d1c802bSDavid Ahern static void rt6_probe(struct fib6_info *rt)
52127097255SYOSHIFUJI Hideaki {
522f547fac6SSabrina Dubroca 	struct __rt6_probe_work *work = NULL;
5235e670d84SDavid Ahern 	const struct in6_addr *nh_gw;
524f2c31e32SEric Dumazet 	struct neighbour *neigh;
5255e670d84SDavid Ahern 	struct net_device *dev;
526f547fac6SSabrina Dubroca 	struct inet6_dev *idev;
5275e670d84SDavid Ahern 
52827097255SYOSHIFUJI Hideaki 	/*
52927097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
53027097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
53127097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
53227097255SYOSHIFUJI Hideaki 	 *
53327097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
53427097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
53527097255SYOSHIFUJI Hideaki 	 */
53693c2fb25SDavid Ahern 	if (!rt || !(rt->fib6_flags & RTF_GATEWAY))
537fdd6681dSAmerigo Wang 		return;
5385e670d84SDavid Ahern 
5395e670d84SDavid Ahern 	nh_gw = &rt->fib6_nh.nh_gw;
5405e670d84SDavid Ahern 	dev = rt->fib6_nh.nh_dev;
5412152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
542f547fac6SSabrina Dubroca 	idev = __in6_dev_get(dev);
5435e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
5442152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
5458d6c31bfSMartin KaFai Lau 		if (neigh->nud_state & NUD_VALID)
5468d6c31bfSMartin KaFai Lau 			goto out;
5478d6c31bfSMartin KaFai Lau 
5482152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
549990edb42SMartin KaFai Lau 		if (!(neigh->nud_state & NUD_VALID) &&
550990edb42SMartin KaFai Lau 		    time_after(jiffies,
551dcd1f572SDavid Ahern 			       neigh->updated + idev->cnf.rtr_probe_interval)) {
552c2f17e82SHannes Frederic Sowa 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
553990edb42SMartin KaFai Lau 			if (work)
5547e980569SJiri Benc 				__neigh_set_probe_once(neigh);
555990edb42SMartin KaFai Lau 		}
556c2f17e82SHannes Frederic Sowa 		write_unlock(&neigh->lock);
557f547fac6SSabrina Dubroca 	} else if (time_after(jiffies, rt->last_probe +
558f547fac6SSabrina Dubroca 				       idev->cnf.rtr_probe_interval)) {
559990edb42SMartin KaFai Lau 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
560990edb42SMartin KaFai Lau 	}
561c2f17e82SHannes Frederic Sowa 
562c2f17e82SHannes Frederic Sowa 	if (work) {
563f547fac6SSabrina Dubroca 		rt->last_probe = jiffies;
564c2f17e82SHannes Frederic Sowa 		INIT_WORK(&work->work, rt6_probe_deferred);
5655e670d84SDavid Ahern 		work->target = *nh_gw;
5665e670d84SDavid Ahern 		dev_hold(dev);
5675e670d84SDavid Ahern 		work->dev = dev;
568c2f17e82SHannes Frederic Sowa 		schedule_work(&work->work);
569c2f17e82SHannes Frederic Sowa 	}
570990edb42SMartin KaFai Lau 
5718d6c31bfSMartin KaFai Lau out:
5722152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
573f2c31e32SEric Dumazet }
57427097255SYOSHIFUJI Hideaki #else
5758d1c802bSDavid Ahern static inline void rt6_probe(struct fib6_info *rt)
57627097255SYOSHIFUJI Hideaki {
57727097255SYOSHIFUJI Hideaki }
57827097255SYOSHIFUJI Hideaki #endif
57927097255SYOSHIFUJI Hideaki 
5801da177e4SLinus Torvalds /*
581554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5821da177e4SLinus Torvalds  */
5838d1c802bSDavid Ahern static inline int rt6_check_dev(struct fib6_info *rt, int oif)
5841da177e4SLinus Torvalds {
5855e670d84SDavid Ahern 	const struct net_device *dev = rt->fib6_nh.nh_dev;
5865e670d84SDavid Ahern 
587161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
588554cfb7eSYOSHIFUJI Hideaki 		return 2;
589554cfb7eSYOSHIFUJI Hideaki 	return 0;
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds 
5928d1c802bSDavid Ahern static inline enum rt6_nud_state rt6_check_neigh(struct fib6_info *rt)
5931da177e4SLinus Torvalds {
594afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
5955e670d84SDavid Ahern 	struct neighbour *neigh;
596f2c31e32SEric Dumazet 
59793c2fb25SDavid Ahern 	if (rt->fib6_flags & RTF_NONEXTHOP ||
59893c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_GATEWAY))
599afc154e9SHannes Frederic Sowa 		return RT6_NUD_SUCCEED;
600145a3621SYOSHIFUJI Hideaki / 吉藤英明 
601145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
6025e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(rt->fib6_nh.nh_dev,
6035e670d84SDavid Ahern 					  &rt->fib6_nh.nh_gw);
604145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
605145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
606554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
607afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
608398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
609a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
610afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
6117e980569SJiri Benc 		else
6127e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
613398bcbebSYOSHIFUJI Hideaki #endif
614145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
615afc154e9SHannes Frederic Sowa 	} else {
616afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
6177e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
618a5a81f0bSPaul Marks 	}
619145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
620145a3621SYOSHIFUJI Hideaki / 吉藤英明 
621a5a81f0bSPaul Marks 	return ret;
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
6248d1c802bSDavid Ahern static int rt6_score_route(struct fib6_info *rt, int oif, int strict)
625554cfb7eSYOSHIFUJI Hideaki {
626a5a81f0bSPaul Marks 	int m;
6274d0c5911SYOSHIFUJI Hideaki 
6284d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
62977d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
630afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
631ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
63293c2fb25SDavid Ahern 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->fib6_flags)) << 2;
633ebacaaa0SYOSHIFUJI Hideaki #endif
634afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE) {
635afc154e9SHannes Frederic Sowa 		int n = rt6_check_neigh(rt);
636afc154e9SHannes Frederic Sowa 		if (n < 0)
637afc154e9SHannes Frederic Sowa 			return n;
638afc154e9SHannes Frederic Sowa 	}
639554cfb7eSYOSHIFUJI Hideaki 	return m;
640554cfb7eSYOSHIFUJI Hideaki }
641554cfb7eSYOSHIFUJI Hideaki 
642dcd1f572SDavid Ahern /* called with rc_read_lock held */
643dcd1f572SDavid Ahern static inline bool fib6_ignore_linkdown(const struct fib6_info *f6i)
644dcd1f572SDavid Ahern {
645dcd1f572SDavid Ahern 	const struct net_device *dev = fib6_info_nh_dev(f6i);
646dcd1f572SDavid Ahern 	bool rc = false;
647dcd1f572SDavid Ahern 
648dcd1f572SDavid Ahern 	if (dev) {
649dcd1f572SDavid Ahern 		const struct inet6_dev *idev = __in6_dev_get(dev);
650dcd1f572SDavid Ahern 
651dcd1f572SDavid Ahern 		rc = !!idev->cnf.ignore_routes_with_linkdown;
652dcd1f572SDavid Ahern 	}
653dcd1f572SDavid Ahern 
654dcd1f572SDavid Ahern 	return rc;
655dcd1f572SDavid Ahern }
656dcd1f572SDavid Ahern 
6578d1c802bSDavid Ahern static struct fib6_info *find_match(struct fib6_info *rt, int oif, int strict,
6588d1c802bSDavid Ahern 				   int *mpri, struct fib6_info *match,
659afc154e9SHannes Frederic Sowa 				   bool *do_rr)
660554cfb7eSYOSHIFUJI Hideaki {
661554cfb7eSYOSHIFUJI Hideaki 	int m;
662afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
66335103d11SAndy Gospodarek 
6645e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
6658067bb8cSIdo Schimmel 		goto out;
6668067bb8cSIdo Schimmel 
667dcd1f572SDavid Ahern 	if (fib6_ignore_linkdown(rt) &&
6685e670d84SDavid Ahern 	    rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
669d5d32e4bSDavid Ahern 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
67035103d11SAndy Gospodarek 		goto out;
671554cfb7eSYOSHIFUJI Hideaki 
67214895687SDavid Ahern 	if (fib6_check_expired(rt))
673f11e6659SDavid S. Miller 		goto out;
674554cfb7eSYOSHIFUJI Hideaki 
675554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
6767e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
677afc154e9SHannes Frederic Sowa 		match_do_rr = true;
678afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
6797e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
680f11e6659SDavid S. Miller 		goto out;
6811da177e4SLinus Torvalds 	}
682f11e6659SDavid S. Miller 
683afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
684afc154e9SHannes Frederic Sowa 		rt6_probe(rt);
685afc154e9SHannes Frederic Sowa 
6867e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
687afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
688afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
689afc154e9SHannes Frederic Sowa 		*mpri = m;
690afc154e9SHannes Frederic Sowa 		match = rt;
691afc154e9SHannes Frederic Sowa 	}
692f11e6659SDavid S. Miller out:
693f11e6659SDavid S. Miller 	return match;
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
6968d1c802bSDavid Ahern static struct fib6_info *find_rr_leaf(struct fib6_node *fn,
6978d1c802bSDavid Ahern 				     struct fib6_info *leaf,
6988d1c802bSDavid Ahern 				     struct fib6_info *rr_head,
699afc154e9SHannes Frederic Sowa 				     u32 metric, int oif, int strict,
700afc154e9SHannes Frederic Sowa 				     bool *do_rr)
701f11e6659SDavid S. Miller {
7028d1c802bSDavid Ahern 	struct fib6_info *rt, *match, *cont;
703f11e6659SDavid S. Miller 	int mpri = -1;
704f11e6659SDavid S. Miller 
705f11e6659SDavid S. Miller 	match = NULL;
7069fbdcfafSSteffen Klassert 	cont = NULL;
7078fb11a9aSDavid Ahern 	for (rt = rr_head; rt; rt = rcu_dereference(rt->fib6_next)) {
70893c2fb25SDavid Ahern 		if (rt->fib6_metric != metric) {
7099fbdcfafSSteffen Klassert 			cont = rt;
7109fbdcfafSSteffen Klassert 			break;
7119fbdcfafSSteffen Klassert 		}
7129fbdcfafSSteffen Klassert 
713afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
7149fbdcfafSSteffen Klassert 	}
7159fbdcfafSSteffen Klassert 
71666f5d6ceSWei Wang 	for (rt = leaf; rt && rt != rr_head;
7178fb11a9aSDavid Ahern 	     rt = rcu_dereference(rt->fib6_next)) {
71893c2fb25SDavid Ahern 		if (rt->fib6_metric != metric) {
7199fbdcfafSSteffen Klassert 			cont = rt;
7209fbdcfafSSteffen Klassert 			break;
7219fbdcfafSSteffen Klassert 		}
7229fbdcfafSSteffen Klassert 
7239fbdcfafSSteffen Klassert 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
7249fbdcfafSSteffen Klassert 	}
7259fbdcfafSSteffen Klassert 
7269fbdcfafSSteffen Klassert 	if (match || !cont)
7279fbdcfafSSteffen Klassert 		return match;
7289fbdcfafSSteffen Klassert 
7298fb11a9aSDavid Ahern 	for (rt = cont; rt; rt = rcu_dereference(rt->fib6_next))
730afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
731f11e6659SDavid S. Miller 
732f11e6659SDavid S. Miller 	return match;
733f11e6659SDavid S. Miller }
734f11e6659SDavid S. Miller 
7358d1c802bSDavid Ahern static struct fib6_info *rt6_select(struct net *net, struct fib6_node *fn,
7368d1040e8SWei Wang 				   int oif, int strict)
737f11e6659SDavid S. Miller {
7388d1c802bSDavid Ahern 	struct fib6_info *leaf = rcu_dereference(fn->leaf);
7398d1c802bSDavid Ahern 	struct fib6_info *match, *rt0;
740afc154e9SHannes Frederic Sowa 	bool do_rr = false;
74117ecf590SWei Wang 	int key_plen;
742f11e6659SDavid S. Miller 
743421842edSDavid Ahern 	if (!leaf || leaf == net->ipv6.fib6_null_entry)
744421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
7458d1040e8SWei Wang 
74666f5d6ceSWei Wang 	rt0 = rcu_dereference(fn->rr_ptr);
747f11e6659SDavid S. Miller 	if (!rt0)
74866f5d6ceSWei Wang 		rt0 = leaf;
749f11e6659SDavid S. Miller 
75017ecf590SWei Wang 	/* Double check to make sure fn is not an intermediate node
75117ecf590SWei Wang 	 * and fn->leaf does not points to its child's leaf
75217ecf590SWei Wang 	 * (This might happen if all routes under fn are deleted from
75317ecf590SWei Wang 	 * the tree and fib6_repair_tree() is called on the node.)
75417ecf590SWei Wang 	 */
75593c2fb25SDavid Ahern 	key_plen = rt0->fib6_dst.plen;
75617ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES
75793c2fb25SDavid Ahern 	if (rt0->fib6_src.plen)
75893c2fb25SDavid Ahern 		key_plen = rt0->fib6_src.plen;
75917ecf590SWei Wang #endif
76017ecf590SWei Wang 	if (fn->fn_bit != key_plen)
761421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
76217ecf590SWei Wang 
76393c2fb25SDavid Ahern 	match = find_rr_leaf(fn, leaf, rt0, rt0->fib6_metric, oif, strict,
764afc154e9SHannes Frederic Sowa 			     &do_rr);
765f11e6659SDavid S. Miller 
766afc154e9SHannes Frederic Sowa 	if (do_rr) {
7678fb11a9aSDavid Ahern 		struct fib6_info *next = rcu_dereference(rt0->fib6_next);
768f11e6659SDavid S. Miller 
769554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
77093c2fb25SDavid Ahern 		if (!next || next->fib6_metric != rt0->fib6_metric)
7718d1040e8SWei Wang 			next = leaf;
772f11e6659SDavid S. Miller 
77366f5d6ceSWei Wang 		if (next != rt0) {
77493c2fb25SDavid Ahern 			spin_lock_bh(&leaf->fib6_table->tb6_lock);
77566f5d6ceSWei Wang 			/* make sure next is not being deleted from the tree */
77693c2fb25SDavid Ahern 			if (next->fib6_node)
77766f5d6ceSWei Wang 				rcu_assign_pointer(fn->rr_ptr, next);
77893c2fb25SDavid Ahern 			spin_unlock_bh(&leaf->fib6_table->tb6_lock);
77966f5d6ceSWei Wang 		}
780554cfb7eSYOSHIFUJI Hideaki 	}
781554cfb7eSYOSHIFUJI Hideaki 
782421842edSDavid Ahern 	return match ? match : net->ipv6.fib6_null_entry;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
7858d1c802bSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_info *rt)
7868b9df265SMartin KaFai Lau {
78793c2fb25SDavid Ahern 	return (rt->fib6_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
7888b9df265SMartin KaFai Lau }
7898b9df265SMartin KaFai Lau 
79070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
79170ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
792b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
79370ceb4f5SYOSHIFUJI Hideaki {
794c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
79570ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
79670ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
79770ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
7984bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
7998d1c802bSDavid Ahern 	struct fib6_info *rt;
80070ceb4f5SYOSHIFUJI Hideaki 
80170ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
80270ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
80370ceb4f5SYOSHIFUJI Hideaki 	}
80470ceb4f5SYOSHIFUJI Hideaki 
80570ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
80670ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
80770ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
80870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
80970ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
81070ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
81170ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
81270ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
81370ceb4f5SYOSHIFUJI Hideaki 		}
81470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
81570ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
81670ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
81770ceb4f5SYOSHIFUJI Hideaki 		}
81870ceb4f5SYOSHIFUJI Hideaki 	}
81970ceb4f5SYOSHIFUJI Hideaki 
82070ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
82170ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
8223933fc95SJens Rosenboom 		return -EINVAL;
82370ceb4f5SYOSHIFUJI Hideaki 
8244bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
82570ceb4f5SYOSHIFUJI Hideaki 
82670ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
82770ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
82870ceb4f5SYOSHIFUJI Hideaki 	else {
82970ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
83070ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
83170ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
83270ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
83370ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
83470ceb4f5SYOSHIFUJI Hideaki 	}
83570ceb4f5SYOSHIFUJI Hideaki 
836f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
837afb1d4b5SDavid Ahern 		rt = rt6_get_dflt_router(net, gwaddr, dev);
838f104a567SDuan Jiong 	else
839f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
840830218c1SDavid Ahern 					gwaddr, dev);
84170ceb4f5SYOSHIFUJI Hideaki 
84270ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
843afb1d4b5SDavid Ahern 		ip6_del_rt(net, rt);
84470ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
84570ceb4f5SYOSHIFUJI Hideaki 	}
84670ceb4f5SYOSHIFUJI Hideaki 
84770ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
848830218c1SDavid Ahern 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
849830218c1SDavid Ahern 					dev, pref);
85070ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
85193c2fb25SDavid Ahern 		rt->fib6_flags = RTF_ROUTEINFO |
85293c2fb25SDavid Ahern 				 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
85370ceb4f5SYOSHIFUJI Hideaki 
85470ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
8551716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
85614895687SDavid Ahern 			fib6_clean_expires(rt);
8571716a961SGao feng 		else
85814895687SDavid Ahern 			fib6_set_expires(rt, jiffies + HZ * lifetime);
8591716a961SGao feng 
86093531c67SDavid Ahern 		fib6_info_release(rt);
86170ceb4f5SYOSHIFUJI Hideaki 	}
86270ceb4f5SYOSHIFUJI Hideaki 	return 0;
86370ceb4f5SYOSHIFUJI Hideaki }
86470ceb4f5SYOSHIFUJI Hideaki #endif
86570ceb4f5SYOSHIFUJI Hideaki 
866ae90d867SDavid Ahern /*
867ae90d867SDavid Ahern  *	Misc support functions
868ae90d867SDavid Ahern  */
869ae90d867SDavid Ahern 
870ae90d867SDavid Ahern /* called with rcu_lock held */
8718d1c802bSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct fib6_info *rt)
872ae90d867SDavid Ahern {
8735e670d84SDavid Ahern 	struct net_device *dev = rt->fib6_nh.nh_dev;
874ae90d867SDavid Ahern 
87593c2fb25SDavid Ahern 	if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
876ae90d867SDavid Ahern 		/* for copies of local routes, dst->dev needs to be the
877ae90d867SDavid Ahern 		 * device if it is a master device, the master device if
878ae90d867SDavid Ahern 		 * device is enslaved, and the loopback as the default
879ae90d867SDavid Ahern 		 */
880ae90d867SDavid Ahern 		if (netif_is_l3_slave(dev) &&
88193c2fb25SDavid Ahern 		    !rt6_need_strict(&rt->fib6_dst.addr))
882ae90d867SDavid Ahern 			dev = l3mdev_master_dev_rcu(dev);
883ae90d867SDavid Ahern 		else if (!netif_is_l3_master(dev))
884ae90d867SDavid Ahern 			dev = dev_net(dev)->loopback_dev;
885ae90d867SDavid Ahern 		/* last case is netif_is_l3_master(dev) is true in which
886ae90d867SDavid Ahern 		 * case we want dev returned to be dev
887ae90d867SDavid Ahern 		 */
888ae90d867SDavid Ahern 	}
889ae90d867SDavid Ahern 
890ae90d867SDavid Ahern 	return dev;
891ae90d867SDavid Ahern }
892ae90d867SDavid Ahern 
8936edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = {
8946edb3c96SDavid Ahern 	[RTN_UNSPEC]	= 0,
8956edb3c96SDavid Ahern 	[RTN_UNICAST]	= 0,
8966edb3c96SDavid Ahern 	[RTN_LOCAL]	= 0,
8976edb3c96SDavid Ahern 	[RTN_BROADCAST]	= 0,
8986edb3c96SDavid Ahern 	[RTN_ANYCAST]	= 0,
8996edb3c96SDavid Ahern 	[RTN_MULTICAST]	= 0,
9006edb3c96SDavid Ahern 	[RTN_BLACKHOLE]	= -EINVAL,
9016edb3c96SDavid Ahern 	[RTN_UNREACHABLE] = -EHOSTUNREACH,
9026edb3c96SDavid Ahern 	[RTN_PROHIBIT]	= -EACCES,
9036edb3c96SDavid Ahern 	[RTN_THROW]	= -EAGAIN,
9046edb3c96SDavid Ahern 	[RTN_NAT]	= -EINVAL,
9056edb3c96SDavid Ahern 	[RTN_XRESOLVE]	= -EINVAL,
9066edb3c96SDavid Ahern };
9076edb3c96SDavid Ahern 
9086edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type)
9096edb3c96SDavid Ahern {
9106edb3c96SDavid Ahern 	return fib6_prop[fib6_type];
9116edb3c96SDavid Ahern }
9126edb3c96SDavid Ahern 
9138d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
9143b6761d1SDavid Ahern {
9153b6761d1SDavid Ahern 	unsigned short flags = 0;
9163b6761d1SDavid Ahern 
9173b6761d1SDavid Ahern 	if (rt->dst_nocount)
9183b6761d1SDavid Ahern 		flags |= DST_NOCOUNT;
9193b6761d1SDavid Ahern 	if (rt->dst_nopolicy)
9203b6761d1SDavid Ahern 		flags |= DST_NOPOLICY;
9213b6761d1SDavid Ahern 	if (rt->dst_host)
9223b6761d1SDavid Ahern 		flags |= DST_HOST;
9233b6761d1SDavid Ahern 
9243b6761d1SDavid Ahern 	return flags;
9253b6761d1SDavid Ahern }
9263b6761d1SDavid Ahern 
9278d1c802bSDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, struct fib6_info *ort)
9286edb3c96SDavid Ahern {
9296edb3c96SDavid Ahern 	rt->dst.error = ip6_rt_type_to_error(ort->fib6_type);
9306edb3c96SDavid Ahern 
9316edb3c96SDavid Ahern 	switch (ort->fib6_type) {
9326edb3c96SDavid Ahern 	case RTN_BLACKHOLE:
9336edb3c96SDavid Ahern 		rt->dst.output = dst_discard_out;
9346edb3c96SDavid Ahern 		rt->dst.input = dst_discard;
9356edb3c96SDavid Ahern 		break;
9366edb3c96SDavid Ahern 	case RTN_PROHIBIT:
9376edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_prohibit_out;
9386edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_prohibit;
9396edb3c96SDavid Ahern 		break;
9406edb3c96SDavid Ahern 	case RTN_THROW:
9416edb3c96SDavid Ahern 	case RTN_UNREACHABLE:
9426edb3c96SDavid Ahern 	default:
9436edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_discard_out;
9446edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_discard;
9456edb3c96SDavid Ahern 		break;
9466edb3c96SDavid Ahern 	}
9476edb3c96SDavid Ahern }
9486edb3c96SDavid Ahern 
9498d1c802bSDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, struct fib6_info *ort)
9506edb3c96SDavid Ahern {
95193c2fb25SDavid Ahern 	if (ort->fib6_flags & RTF_REJECT) {
9526edb3c96SDavid Ahern 		ip6_rt_init_dst_reject(rt, ort);
9536edb3c96SDavid Ahern 		return;
9546edb3c96SDavid Ahern 	}
9556edb3c96SDavid Ahern 
9566edb3c96SDavid Ahern 	rt->dst.error = 0;
9576edb3c96SDavid Ahern 	rt->dst.output = ip6_output;
9586edb3c96SDavid Ahern 
959d23c4b63SHangbin Liu 	if (ort->fib6_type == RTN_LOCAL || ort->fib6_type == RTN_ANYCAST) {
9606edb3c96SDavid Ahern 		rt->dst.input = ip6_input;
96193c2fb25SDavid Ahern 	} else if (ipv6_addr_type(&ort->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
9626edb3c96SDavid Ahern 		rt->dst.input = ip6_mc_input;
9636edb3c96SDavid Ahern 	} else {
9646edb3c96SDavid Ahern 		rt->dst.input = ip6_forward;
9656edb3c96SDavid Ahern 	}
9666edb3c96SDavid Ahern 
9676edb3c96SDavid Ahern 	if (ort->fib6_nh.nh_lwtstate) {
9686edb3c96SDavid Ahern 		rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.nh_lwtstate);
9696edb3c96SDavid Ahern 		lwtunnel_set_redirect(&rt->dst);
9706edb3c96SDavid Ahern 	}
9716edb3c96SDavid Ahern 
9726edb3c96SDavid Ahern 	rt->dst.lastuse = jiffies;
9736edb3c96SDavid Ahern }
9746edb3c96SDavid Ahern 
975e873e4b9SWei Wang /* Caller must already hold reference to @from */
9768d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
977ae90d867SDavid Ahern {
978ae90d867SDavid Ahern 	rt->rt6i_flags &= ~RTF_EXPIRES;
979a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, from);
980e1255ed4SDavid Ahern 	ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
981ae90d867SDavid Ahern }
982ae90d867SDavid Ahern 
983e873e4b9SWei Wang /* Caller must already hold reference to @ort */
9848d1c802bSDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, struct fib6_info *ort)
985ae90d867SDavid Ahern {
986dcd1f572SDavid Ahern 	struct net_device *dev = fib6_info_nh_dev(ort);
987dcd1f572SDavid Ahern 
9886edb3c96SDavid Ahern 	ip6_rt_init_dst(rt, ort);
9896edb3c96SDavid Ahern 
99093c2fb25SDavid Ahern 	rt->rt6i_dst = ort->fib6_dst;
991dcd1f572SDavid Ahern 	rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
9925e670d84SDavid Ahern 	rt->rt6i_gateway = ort->fib6_nh.nh_gw;
99393c2fb25SDavid Ahern 	rt->rt6i_flags = ort->fib6_flags;
994ae90d867SDavid Ahern 	rt6_set_from(rt, ort);
995ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
99693c2fb25SDavid Ahern 	rt->rt6i_src = ort->fib6_src;
997ae90d867SDavid Ahern #endif
998ae90d867SDavid Ahern }
999ae90d867SDavid Ahern 
1000a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
1001a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
1002a3c00e46SMartin KaFai Lau {
100366f5d6ceSWei Wang 	struct fib6_node *pn, *sn;
1004a3c00e46SMartin KaFai Lau 	while (1) {
1005a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
1006a3c00e46SMartin KaFai Lau 			return NULL;
100766f5d6ceSWei Wang 		pn = rcu_dereference(fn->parent);
100866f5d6ceSWei Wang 		sn = FIB6_SUBTREE(pn);
100966f5d6ceSWei Wang 		if (sn && sn != fn)
10106454743bSDavid Ahern 			fn = fib6_node_lookup(sn, NULL, saddr);
1011a3c00e46SMartin KaFai Lau 		else
1012a3c00e46SMartin KaFai Lau 			fn = pn;
1013a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
1014a3c00e46SMartin KaFai Lau 			return fn;
1015a3c00e46SMartin KaFai Lau 	}
1016a3c00e46SMartin KaFai Lau }
1017c71099acSThomas Graf 
1018d3843fe5SWei Wang static bool ip6_hold_safe(struct net *net, struct rt6_info **prt,
1019d3843fe5SWei Wang 			  bool null_fallback)
1020d3843fe5SWei Wang {
1021d3843fe5SWei Wang 	struct rt6_info *rt = *prt;
1022d3843fe5SWei Wang 
1023d3843fe5SWei Wang 	if (dst_hold_safe(&rt->dst))
1024d3843fe5SWei Wang 		return true;
1025d3843fe5SWei Wang 	if (null_fallback) {
1026d3843fe5SWei Wang 		rt = net->ipv6.ip6_null_entry;
1027d3843fe5SWei Wang 		dst_hold(&rt->dst);
1028d3843fe5SWei Wang 	} else {
1029d3843fe5SWei Wang 		rt = NULL;
1030d3843fe5SWei Wang 	}
1031d3843fe5SWei Wang 	*prt = rt;
1032d3843fe5SWei Wang 	return false;
1033d3843fe5SWei Wang }
1034d3843fe5SWei Wang 
1035dec9b0e2SDavid Ahern /* called with rcu_lock held */
10368d1c802bSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(struct fib6_info *rt)
1037dec9b0e2SDavid Ahern {
10383b6761d1SDavid Ahern 	unsigned short flags = fib6_info_dst_flags(rt);
1039dec9b0e2SDavid Ahern 	struct net_device *dev = rt->fib6_nh.nh_dev;
1040dec9b0e2SDavid Ahern 	struct rt6_info *nrt;
1041dec9b0e2SDavid Ahern 
1042e873e4b9SWei Wang 	if (!fib6_info_hold_safe(rt))
10431c87e79aSXin Long 		goto fallback;
1044e873e4b9SWei Wang 
104593531c67SDavid Ahern 	nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
10461c87e79aSXin Long 	if (!nrt) {
1047e873e4b9SWei Wang 		fib6_info_release(rt);
10481c87e79aSXin Long 		goto fallback;
10491c87e79aSXin Long 	}
1050dec9b0e2SDavid Ahern 
10511c87e79aSXin Long 	ip6_rt_copy_init(nrt, rt);
10521c87e79aSXin Long 	return nrt;
10531c87e79aSXin Long 
10541c87e79aSXin Long fallback:
10551c87e79aSXin Long 	nrt = dev_net(dev)->ipv6.ip6_null_entry;
10561c87e79aSXin Long 	dst_hold(&nrt->dst);
1057dec9b0e2SDavid Ahern 	return nrt;
1058dec9b0e2SDavid Ahern }
1059dec9b0e2SDavid Ahern 
10608ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
10618ed67789SDaniel Lezcano 					     struct fib6_table *table,
1062b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
1063b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
1064b75cc8f9SDavid Ahern 					     int flags)
10651da177e4SLinus Torvalds {
10668d1c802bSDavid Ahern 	struct fib6_info *f6i;
10671da177e4SLinus Torvalds 	struct fib6_node *fn;
106823fb93a4SDavid Ahern 	struct rt6_info *rt;
10691da177e4SLinus Torvalds 
1070b6cdbc85SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1071b6cdbc85SDavid Ahern 		flags &= ~RT6_LOOKUP_F_IFACE;
1072b6cdbc85SDavid Ahern 
107366f5d6ceSWei Wang 	rcu_read_lock();
10746454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1075c71099acSThomas Graf restart:
107623fb93a4SDavid Ahern 	f6i = rcu_dereference(fn->leaf);
107723fb93a4SDavid Ahern 	if (!f6i) {
107823fb93a4SDavid Ahern 		f6i = net->ipv6.fib6_null_entry;
107966f5d6ceSWei Wang 	} else {
108023fb93a4SDavid Ahern 		f6i = rt6_device_match(net, f6i, &fl6->saddr,
108166f5d6ceSWei Wang 				      fl6->flowi6_oif, flags);
108293c2fb25SDavid Ahern 		if (f6i->fib6_nsiblings && fl6->flowi6_oif == 0)
10833b290a31SDavid Ahern 			f6i = fib6_multipath_select(net, f6i, fl6,
10843b290a31SDavid Ahern 						    fl6->flowi6_oif, skb,
10853b290a31SDavid Ahern 						    flags);
108666f5d6ceSWei Wang 	}
108723fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1088a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1089a3c00e46SMartin KaFai Lau 		if (fn)
1090a3c00e46SMartin KaFai Lau 			goto restart;
1091a3c00e46SMartin KaFai Lau 	}
10922b760fcfSWei Wang 
1093d4bea421SDavid Ahern 	trace_fib6_table_lookup(net, f6i, table, fl6);
1094d4bea421SDavid Ahern 
10954c9483b2SDavid S. Miller 	/* Search through exception table */
109623fb93a4SDavid Ahern 	rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
109723fb93a4SDavid Ahern 	if (rt) {
1098d3843fe5SWei Wang 		if (ip6_hold_safe(net, &rt, true))
1099d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
110023fb93a4SDavid Ahern 	} else if (f6i == net->ipv6.fib6_null_entry) {
1101dec9b0e2SDavid Ahern 		rt = net->ipv6.ip6_null_entry;
1102dec9b0e2SDavid Ahern 		dst_hold(&rt->dst);
110323fb93a4SDavid Ahern 	} else {
110423fb93a4SDavid Ahern 		rt = ip6_create_rt_rcu(f6i);
1105dec9b0e2SDavid Ahern 	}
1106d3843fe5SWei Wang 
110766f5d6ceSWei Wang 	rcu_read_unlock();
1108b811580dSDavid Ahern 
11091da177e4SLinus Torvalds 	return rt;
1110c71099acSThomas Graf }
1111c71099acSThomas Graf 
1112ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1113b75cc8f9SDavid Ahern 				   const struct sk_buff *skb, int flags)
1114ea6e574eSFlorian Westphal {
1115b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1116ea6e574eSFlorian Westphal }
1117ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
1118ea6e574eSFlorian Westphal 
11199acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1120b75cc8f9SDavid Ahern 			    const struct in6_addr *saddr, int oif,
1121b75cc8f9SDavid Ahern 			    const struct sk_buff *skb, int strict)
1122c71099acSThomas Graf {
11234c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
11244c9483b2SDavid S. Miller 		.flowi6_oif = oif,
11254c9483b2SDavid S. Miller 		.daddr = *daddr,
1126c71099acSThomas Graf 	};
1127c71099acSThomas Graf 	struct dst_entry *dst;
112877d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1129c71099acSThomas Graf 
1130adaa70bbSThomas Graf 	if (saddr) {
11314c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1132adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1133adaa70bbSThomas Graf 	}
1134adaa70bbSThomas Graf 
1135b75cc8f9SDavid Ahern 	dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1136c71099acSThomas Graf 	if (dst->error == 0)
1137c71099acSThomas Graf 		return (struct rt6_info *) dst;
1138c71099acSThomas Graf 
1139c71099acSThomas Graf 	dst_release(dst);
1140c71099acSThomas Graf 
11411da177e4SLinus Torvalds 	return NULL;
11421da177e4SLinus Torvalds }
11437159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
11447159039aSYOSHIFUJI Hideaki 
1145c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
11461cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
11471cfb71eeSWei Wang  * route is released.
11481cfb71eeSWei Wang  * Caller must hold dst before calling it.
11491da177e4SLinus Torvalds  */
11501da177e4SLinus Torvalds 
11518d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1152333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
11531da177e4SLinus Torvalds {
11541da177e4SLinus Torvalds 	int err;
1155c71099acSThomas Graf 	struct fib6_table *table;
11561da177e4SLinus Torvalds 
115793c2fb25SDavid Ahern 	table = rt->fib6_table;
115866f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
1159d4ead6b3SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, extack);
116066f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	return err;
11631da177e4SLinus Torvalds }
11641da177e4SLinus Torvalds 
11658d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt)
116640e22e8fSThomas Graf {
1167afb1d4b5SDavid Ahern 	struct nl_info info = {	.nl_net = net, };
1168e715b6d3SFlorian Westphal 
1169d4ead6b3SDavid Ahern 	return __ip6_ins_rt(rt, &info, NULL);
117040e22e8fSThomas Graf }
117140e22e8fSThomas Graf 
11728d1c802bSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(struct fib6_info *ort,
117321efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
1174b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
11751da177e4SLinus Torvalds {
11764832c30dSDavid Ahern 	struct net_device *dev;
11771da177e4SLinus Torvalds 	struct rt6_info *rt;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	/*
11801da177e4SLinus Torvalds 	 *	Clone the route.
11811da177e4SLinus Torvalds 	 */
11821da177e4SLinus Torvalds 
1183e873e4b9SWei Wang 	if (!fib6_info_hold_safe(ort))
1184e873e4b9SWei Wang 		return NULL;
1185e873e4b9SWei Wang 
11864832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(ort);
118793531c67SDavid Ahern 	rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1188e873e4b9SWei Wang 	if (!rt) {
1189e873e4b9SWei Wang 		fib6_info_release(ort);
119083a09abdSMartin KaFai Lau 		return NULL;
1191e873e4b9SWei Wang 	}
119283a09abdSMartin KaFai Lau 
119383a09abdSMartin KaFai Lau 	ip6_rt_copy_init(rt, ort);
11948b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
119583a09abdSMartin KaFai Lau 	rt->dst.flags |= DST_HOST;
119683a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
119783a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
11988b9df265SMartin KaFai Lau 
11998b9df265SMartin KaFai Lau 	if (!rt6_is_gw_or_nonexthop(ort)) {
120093c2fb25SDavid Ahern 		if (ort->fib6_dst.plen != 128 &&
120193c2fb25SDavid Ahern 		    ipv6_addr_equal(&ort->fib6_dst.addr, daddr))
120258c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
12031da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
12041da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
12054e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
12061da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
12071da177e4SLinus Torvalds 		}
12081da177e4SLinus Torvalds #endif
120995a9a5baSYOSHIFUJI Hideaki 	}
121095a9a5baSYOSHIFUJI Hideaki 
1211299d9939SYOSHIFUJI Hideaki 	return rt;
1212299d9939SYOSHIFUJI Hideaki }
1213299d9939SYOSHIFUJI Hideaki 
12148d1c802bSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(struct fib6_info *rt)
1215d52d3997SMartin KaFai Lau {
12163b6761d1SDavid Ahern 	unsigned short flags = fib6_info_dst_flags(rt);
12174832c30dSDavid Ahern 	struct net_device *dev;
1218d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1219d52d3997SMartin KaFai Lau 
1220e873e4b9SWei Wang 	if (!fib6_info_hold_safe(rt))
1221e873e4b9SWei Wang 		return NULL;
1222e873e4b9SWei Wang 
12234832c30dSDavid Ahern 	rcu_read_lock();
12244832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(rt);
122593531c67SDavid Ahern 	pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
12264832c30dSDavid Ahern 	rcu_read_unlock();
1227e873e4b9SWei Wang 	if (!pcpu_rt) {
1228e873e4b9SWei Wang 		fib6_info_release(rt);
1229d52d3997SMartin KaFai Lau 		return NULL;
1230e873e4b9SWei Wang 	}
1231d52d3997SMartin KaFai Lau 	ip6_rt_copy_init(pcpu_rt, rt);
1232d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
1233d52d3997SMartin KaFai Lau 	return pcpu_rt;
1234d52d3997SMartin KaFai Lau }
1235d52d3997SMartin KaFai Lau 
123666f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */
12378d1c802bSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(struct fib6_info *rt)
1238d52d3997SMartin KaFai Lau {
1239a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, **p;
1240d52d3997SMartin KaFai Lau 
1241d52d3997SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1242d52d3997SMartin KaFai Lau 	pcpu_rt = *p;
1243d52d3997SMartin KaFai Lau 
1244d4ead6b3SDavid Ahern 	if (pcpu_rt)
1245d4ead6b3SDavid Ahern 		ip6_hold_safe(NULL, &pcpu_rt, false);
1246d3843fe5SWei Wang 
1247a73e4195SMartin KaFai Lau 	return pcpu_rt;
1248a73e4195SMartin KaFai Lau }
1249a73e4195SMartin KaFai Lau 
1250afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net,
12518d1c802bSDavid Ahern 					    struct fib6_info *rt)
1252a73e4195SMartin KaFai Lau {
1253a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1254d52d3997SMartin KaFai Lau 
1255d52d3997SMartin KaFai Lau 	pcpu_rt = ip6_rt_pcpu_alloc(rt);
1256d52d3997SMartin KaFai Lau 	if (!pcpu_rt) {
12579c7370a1SMartin KaFai Lau 		dst_hold(&net->ipv6.ip6_null_entry->dst);
12589c7370a1SMartin KaFai Lau 		return net->ipv6.ip6_null_entry;
1259d52d3997SMartin KaFai Lau 	}
1260d52d3997SMartin KaFai Lau 
1261a94b9367SWei Wang 	dst_hold(&pcpu_rt->dst);
1262a73e4195SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1263d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1264951f788aSEric Dumazet 	BUG_ON(prev);
1265a94b9367SWei Wang 
1266d52d3997SMartin KaFai Lau 	return pcpu_rt;
1267d52d3997SMartin KaFai Lau }
1268d52d3997SMartin KaFai Lau 
126935732d01SWei Wang /* exception hash table implementation
127035732d01SWei Wang  */
127135732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
127235732d01SWei Wang 
127335732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
127435732d01SWei Wang  * Caller must hold rt6_exception_lock
127535732d01SWei Wang  */
127635732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
127735732d01SWei Wang 				 struct rt6_exception *rt6_ex)
127835732d01SWei Wang {
1279f5b51fe8SPaolo Abeni 	struct fib6_info *from;
1280b2427e67SColin Ian King 	struct net *net;
128181eb8447SWei Wang 
128235732d01SWei Wang 	if (!bucket || !rt6_ex)
128335732d01SWei Wang 		return;
1284b2427e67SColin Ian King 
1285b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
1286f5b51fe8SPaolo Abeni 	net->ipv6.rt6_stats->fib_rt_cache--;
1287f5b51fe8SPaolo Abeni 
1288f5b51fe8SPaolo Abeni 	/* purge completely the exception to allow releasing the held resources:
1289f5b51fe8SPaolo Abeni 	 * some [sk] cache may keep the dst around for unlimited time
1290f5b51fe8SPaolo Abeni 	 */
1291f5b51fe8SPaolo Abeni 	from = rcu_dereference_protected(rt6_ex->rt6i->from,
1292f5b51fe8SPaolo Abeni 					 lockdep_is_held(&rt6_exception_lock));
1293f5b51fe8SPaolo Abeni 	rcu_assign_pointer(rt6_ex->rt6i->from, NULL);
1294f5b51fe8SPaolo Abeni 	fib6_info_release(from);
1295f5b51fe8SPaolo Abeni 	dst_dev_put(&rt6_ex->rt6i->dst);
1296f5b51fe8SPaolo Abeni 
129735732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
129877634cc6SDavid Ahern 	dst_release(&rt6_ex->rt6i->dst);
129935732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
130035732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
130135732d01SWei Wang 	bucket->depth--;
130235732d01SWei Wang }
130335732d01SWei Wang 
130435732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
130535732d01SWei Wang  * Caller must hold rt6_exception_lock
130635732d01SWei Wang  */
130735732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
130835732d01SWei Wang {
130935732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
131035732d01SWei Wang 
131135732d01SWei Wang 	if (!bucket)
131235732d01SWei Wang 		return;
131335732d01SWei Wang 
131435732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
131535732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
131635732d01SWei Wang 			oldest = rt6_ex;
131735732d01SWei Wang 	}
131835732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
131935732d01SWei Wang }
132035732d01SWei Wang 
132135732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
132235732d01SWei Wang 			      const struct in6_addr *src)
132335732d01SWei Wang {
132435732d01SWei Wang 	static u32 seed __read_mostly;
132535732d01SWei Wang 	u32 val;
132635732d01SWei Wang 
132735732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
132835732d01SWei Wang 	val = jhash(dst, sizeof(*dst), seed);
132935732d01SWei Wang 
133035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
133135732d01SWei Wang 	if (src)
133235732d01SWei Wang 		val = jhash(src, sizeof(*src), val);
133335732d01SWei Wang #endif
133435732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
133535732d01SWei Wang }
133635732d01SWei Wang 
133735732d01SWei Wang /* Helper function to find the cached rt in the hash table
133835732d01SWei Wang  * and update bucket pointer to point to the bucket for this
133935732d01SWei Wang  * (daddr, saddr) pair
134035732d01SWei Wang  * Caller must hold rt6_exception_lock
134135732d01SWei Wang  */
134235732d01SWei Wang static struct rt6_exception *
134335732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
134435732d01SWei Wang 			      const struct in6_addr *daddr,
134535732d01SWei Wang 			      const struct in6_addr *saddr)
134635732d01SWei Wang {
134735732d01SWei Wang 	struct rt6_exception *rt6_ex;
134835732d01SWei Wang 	u32 hval;
134935732d01SWei Wang 
135035732d01SWei Wang 	if (!(*bucket) || !daddr)
135135732d01SWei Wang 		return NULL;
135235732d01SWei Wang 
135335732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
135435732d01SWei Wang 	*bucket += hval;
135535732d01SWei Wang 
135635732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
135735732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
135835732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
135935732d01SWei Wang 
136035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
136135732d01SWei Wang 		if (matched && saddr)
136235732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
136335732d01SWei Wang #endif
136435732d01SWei Wang 		if (matched)
136535732d01SWei Wang 			return rt6_ex;
136635732d01SWei Wang 	}
136735732d01SWei Wang 	return NULL;
136835732d01SWei Wang }
136935732d01SWei Wang 
137035732d01SWei Wang /* Helper function to find the cached rt in the hash table
137135732d01SWei Wang  * and update bucket pointer to point to the bucket for this
137235732d01SWei Wang  * (daddr, saddr) pair
137335732d01SWei Wang  * Caller must hold rcu_read_lock()
137435732d01SWei Wang  */
137535732d01SWei Wang static struct rt6_exception *
137635732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
137735732d01SWei Wang 			 const struct in6_addr *daddr,
137835732d01SWei Wang 			 const struct in6_addr *saddr)
137935732d01SWei Wang {
138035732d01SWei Wang 	struct rt6_exception *rt6_ex;
138135732d01SWei Wang 	u32 hval;
138235732d01SWei Wang 
138335732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
138435732d01SWei Wang 
138535732d01SWei Wang 	if (!(*bucket) || !daddr)
138635732d01SWei Wang 		return NULL;
138735732d01SWei Wang 
138835732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
138935732d01SWei Wang 	*bucket += hval;
139035732d01SWei Wang 
139135732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
139235732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
139335732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
139435732d01SWei Wang 
139535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
139635732d01SWei Wang 		if (matched && saddr)
139735732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
139835732d01SWei Wang #endif
139935732d01SWei Wang 		if (matched)
140035732d01SWei Wang 			return rt6_ex;
140135732d01SWei Wang 	}
140235732d01SWei Wang 	return NULL;
140335732d01SWei Wang }
140435732d01SWei Wang 
14058d1c802bSDavid Ahern static unsigned int fib6_mtu(const struct fib6_info *rt)
140635732d01SWei Wang {
1407d4ead6b3SDavid Ahern 	unsigned int mtu;
1408d4ead6b3SDavid Ahern 
1409dcd1f572SDavid Ahern 	if (rt->fib6_pmtu) {
1410dcd1f572SDavid Ahern 		mtu = rt->fib6_pmtu;
1411dcd1f572SDavid Ahern 	} else {
1412dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
1413dcd1f572SDavid Ahern 		struct inet6_dev *idev;
1414dcd1f572SDavid Ahern 
1415dcd1f572SDavid Ahern 		rcu_read_lock();
1416dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
1417dcd1f572SDavid Ahern 		mtu = idev->cnf.mtu6;
1418dcd1f572SDavid Ahern 		rcu_read_unlock();
1419dcd1f572SDavid Ahern 	}
1420dcd1f572SDavid Ahern 
1421d4ead6b3SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1422d4ead6b3SDavid Ahern 
1423d4ead6b3SDavid Ahern 	return mtu - lwtunnel_headroom(rt->fib6_nh.nh_lwtstate, mtu);
1424d4ead6b3SDavid Ahern }
1425d4ead6b3SDavid Ahern 
142635732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
14278d1c802bSDavid Ahern 				struct fib6_info *ort)
142835732d01SWei Wang {
14295e670d84SDavid Ahern 	struct net *net = dev_net(nrt->dst.dev);
143035732d01SWei Wang 	struct rt6_exception_bucket *bucket;
143135732d01SWei Wang 	struct in6_addr *src_key = NULL;
143235732d01SWei Wang 	struct rt6_exception *rt6_ex;
143335732d01SWei Wang 	int err = 0;
143435732d01SWei Wang 
143535732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
143635732d01SWei Wang 
143735732d01SWei Wang 	if (ort->exception_bucket_flushed) {
143835732d01SWei Wang 		err = -EINVAL;
143935732d01SWei Wang 		goto out;
144035732d01SWei Wang 	}
144135732d01SWei Wang 
144235732d01SWei Wang 	bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
144335732d01SWei Wang 					lockdep_is_held(&rt6_exception_lock));
144435732d01SWei Wang 	if (!bucket) {
144535732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
144635732d01SWei Wang 				 GFP_ATOMIC);
144735732d01SWei Wang 		if (!bucket) {
144835732d01SWei Wang 			err = -ENOMEM;
144935732d01SWei Wang 			goto out;
145035732d01SWei Wang 		}
145135732d01SWei Wang 		rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
145235732d01SWei Wang 	}
145335732d01SWei Wang 
145435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
145535732d01SWei Wang 	/* rt6i_src.plen != 0 indicates ort is in subtree
145635732d01SWei Wang 	 * and exception table is indexed by a hash of
145735732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
145835732d01SWei Wang 	 * Otherwise, the exception table is indexed by
145935732d01SWei Wang 	 * a hash of only rt6i_dst.
146035732d01SWei Wang 	 */
146193c2fb25SDavid Ahern 	if (ort->fib6_src.plen)
146235732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
146335732d01SWei Wang #endif
1464f5bbe7eeSWei Wang 	/* rt6_mtu_change() might lower mtu on ort.
1465f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
1466f5bbe7eeSWei Wang 	 * is less than ort's mtu value.
1467f5bbe7eeSWei Wang 	 */
1468d4ead6b3SDavid Ahern 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(ort)) {
1469f5bbe7eeSWei Wang 		err = -EINVAL;
1470f5bbe7eeSWei Wang 		goto out;
1471f5bbe7eeSWei Wang 	}
147260006a48SWei Wang 
147335732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
147435732d01SWei Wang 					       src_key);
147535732d01SWei Wang 	if (rt6_ex)
147635732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
147735732d01SWei Wang 
147835732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
147935732d01SWei Wang 	if (!rt6_ex) {
148035732d01SWei Wang 		err = -ENOMEM;
148135732d01SWei Wang 		goto out;
148235732d01SWei Wang 	}
148335732d01SWei Wang 	rt6_ex->rt6i = nrt;
148435732d01SWei Wang 	rt6_ex->stamp = jiffies;
148535732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
148635732d01SWei Wang 	bucket->depth++;
148781eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
148835732d01SWei Wang 
148935732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
149035732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
149135732d01SWei Wang 
149235732d01SWei Wang out:
149335732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
149435732d01SWei Wang 
149535732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1496b886d5f2SPaolo Abeni 	if (!err) {
149793c2fb25SDavid Ahern 		spin_lock_bh(&ort->fib6_table->tb6_lock);
14987aef6859SDavid Ahern 		fib6_update_sernum(net, ort);
149993c2fb25SDavid Ahern 		spin_unlock_bh(&ort->fib6_table->tb6_lock);
1500b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1501b886d5f2SPaolo Abeni 	}
150235732d01SWei Wang 
150335732d01SWei Wang 	return err;
150435732d01SWei Wang }
150535732d01SWei Wang 
15068d1c802bSDavid Ahern void rt6_flush_exceptions(struct fib6_info *rt)
150735732d01SWei Wang {
150835732d01SWei Wang 	struct rt6_exception_bucket *bucket;
150935732d01SWei Wang 	struct rt6_exception *rt6_ex;
151035732d01SWei Wang 	struct hlist_node *tmp;
151135732d01SWei Wang 	int i;
151235732d01SWei Wang 
151335732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
151435732d01SWei Wang 	/* Prevent rt6_insert_exception() to recreate the bucket list */
151535732d01SWei Wang 	rt->exception_bucket_flushed = 1;
151635732d01SWei Wang 
151735732d01SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
151835732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
151935732d01SWei Wang 	if (!bucket)
152035732d01SWei Wang 		goto out;
152135732d01SWei Wang 
152235732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
152335732d01SWei Wang 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
152435732d01SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
152535732d01SWei Wang 		WARN_ON_ONCE(bucket->depth);
152635732d01SWei Wang 		bucket++;
152735732d01SWei Wang 	}
152835732d01SWei Wang 
152935732d01SWei Wang out:
153035732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
153135732d01SWei Wang }
153235732d01SWei Wang 
153335732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
153435732d01SWei Wang  * Caller has to hold rcu_read_lock()
153535732d01SWei Wang  */
15368d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
153735732d01SWei Wang 					   struct in6_addr *daddr,
153835732d01SWei Wang 					   struct in6_addr *saddr)
153935732d01SWei Wang {
154035732d01SWei Wang 	struct rt6_exception_bucket *bucket;
154135732d01SWei Wang 	struct in6_addr *src_key = NULL;
154235732d01SWei Wang 	struct rt6_exception *rt6_ex;
154335732d01SWei Wang 	struct rt6_info *res = NULL;
154435732d01SWei Wang 
154535732d01SWei Wang 	bucket = rcu_dereference(rt->rt6i_exception_bucket);
154635732d01SWei Wang 
154735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
154835732d01SWei Wang 	/* rt6i_src.plen != 0 indicates rt is in subtree
154935732d01SWei Wang 	 * and exception table is indexed by a hash of
155035732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
155135732d01SWei Wang 	 * Otherwise, the exception table is indexed by
155235732d01SWei Wang 	 * a hash of only rt6i_dst.
155335732d01SWei Wang 	 */
155493c2fb25SDavid Ahern 	if (rt->fib6_src.plen)
155535732d01SWei Wang 		src_key = saddr;
155635732d01SWei Wang #endif
155735732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
155835732d01SWei Wang 
155935732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
156035732d01SWei Wang 		res = rt6_ex->rt6i;
156135732d01SWei Wang 
156235732d01SWei Wang 	return res;
156335732d01SWei Wang }
156435732d01SWei Wang 
156535732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
156623fb93a4SDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt)
156735732d01SWei Wang {
156835732d01SWei Wang 	struct rt6_exception_bucket *bucket;
156935732d01SWei Wang 	struct in6_addr *src_key = NULL;
157035732d01SWei Wang 	struct rt6_exception *rt6_ex;
15718a14e46fSDavid Ahern 	struct fib6_info *from;
157235732d01SWei Wang 	int err;
157335732d01SWei Wang 
1574091311deSEric Dumazet 	from = rcu_dereference(rt->from);
157535732d01SWei Wang 	if (!from ||
1576442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
157735732d01SWei Wang 		return -EINVAL;
157835732d01SWei Wang 
157935732d01SWei Wang 	if (!rcu_access_pointer(from->rt6i_exception_bucket))
158035732d01SWei Wang 		return -ENOENT;
158135732d01SWei Wang 
158235732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
158335732d01SWei Wang 	bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
158435732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
158535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
158635732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
158735732d01SWei Wang 	 * and exception table is indexed by a hash of
158835732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
158935732d01SWei Wang 	 * Otherwise, the exception table is indexed by
159035732d01SWei Wang 	 * a hash of only rt6i_dst.
159135732d01SWei Wang 	 */
159293c2fb25SDavid Ahern 	if (from->fib6_src.plen)
159335732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
159435732d01SWei Wang #endif
159535732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
159635732d01SWei Wang 					       &rt->rt6i_dst.addr,
159735732d01SWei Wang 					       src_key);
159835732d01SWei Wang 	if (rt6_ex) {
159935732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
160035732d01SWei Wang 		err = 0;
160135732d01SWei Wang 	} else {
160235732d01SWei Wang 		err = -ENOENT;
160335732d01SWei Wang 	}
160435732d01SWei Wang 
160535732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
160635732d01SWei Wang 	return err;
160735732d01SWei Wang }
160835732d01SWei Wang 
160935732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
161035732d01SWei Wang  * refresh its stamp
161135732d01SWei Wang  */
161235732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
161335732d01SWei Wang {
161435732d01SWei Wang 	struct rt6_exception_bucket *bucket;
161535732d01SWei Wang 	struct in6_addr *src_key = NULL;
161635732d01SWei Wang 	struct rt6_exception *rt6_ex;
1617193f3685SPaolo Abeni 	struct fib6_info *from;
161835732d01SWei Wang 
161935732d01SWei Wang 	rcu_read_lock();
1620193f3685SPaolo Abeni 	from = rcu_dereference(rt->from);
1621193f3685SPaolo Abeni 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1622193f3685SPaolo Abeni 		goto unlock;
1623193f3685SPaolo Abeni 
162435732d01SWei Wang 	bucket = rcu_dereference(from->rt6i_exception_bucket);
162535732d01SWei Wang 
162635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
162735732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
162835732d01SWei Wang 	 * and exception table is indexed by a hash of
162935732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
163035732d01SWei Wang 	 * Otherwise, the exception table is indexed by
163135732d01SWei Wang 	 * a hash of only rt6i_dst.
163235732d01SWei Wang 	 */
163393c2fb25SDavid Ahern 	if (from->fib6_src.plen)
163435732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
163535732d01SWei Wang #endif
163635732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket,
163735732d01SWei Wang 					  &rt->rt6i_dst.addr,
163835732d01SWei Wang 					  src_key);
163935732d01SWei Wang 	if (rt6_ex)
164035732d01SWei Wang 		rt6_ex->stamp = jiffies;
164135732d01SWei Wang 
1642193f3685SPaolo Abeni unlock:
164335732d01SWei Wang 	rcu_read_unlock();
164435732d01SWei Wang }
164535732d01SWei Wang 
1646e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1647e9fa1495SStefano Brivio 					 struct rt6_info *rt, int mtu)
1648e9fa1495SStefano Brivio {
1649e9fa1495SStefano Brivio 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1650e9fa1495SStefano Brivio 	 * lowest MTU in the path: always allow updating the route PMTU to
1651e9fa1495SStefano Brivio 	 * reflect PMTU decreases.
1652e9fa1495SStefano Brivio 	 *
1653e9fa1495SStefano Brivio 	 * If the new MTU is higher, and the route PMTU is equal to the local
1654e9fa1495SStefano Brivio 	 * MTU, this means the old MTU is the lowest in the path, so allow
1655e9fa1495SStefano Brivio 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1656e9fa1495SStefano Brivio 	 * handle this.
1657e9fa1495SStefano Brivio 	 */
1658e9fa1495SStefano Brivio 
1659e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) >= mtu)
1660e9fa1495SStefano Brivio 		return true;
1661e9fa1495SStefano Brivio 
1662e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1663e9fa1495SStefano Brivio 		return true;
1664e9fa1495SStefano Brivio 
1665e9fa1495SStefano Brivio 	return false;
1666e9fa1495SStefano Brivio }
1667e9fa1495SStefano Brivio 
1668e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
16698d1c802bSDavid Ahern 				       struct fib6_info *rt, int mtu)
1670f5bbe7eeSWei Wang {
1671f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
1672f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
1673f5bbe7eeSWei Wang 	int i;
1674f5bbe7eeSWei Wang 
1675f5bbe7eeSWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1676f5bbe7eeSWei Wang 					lockdep_is_held(&rt6_exception_lock));
1677f5bbe7eeSWei Wang 
1678e9fa1495SStefano Brivio 	if (!bucket)
1679e9fa1495SStefano Brivio 		return;
1680e9fa1495SStefano Brivio 
1681f5bbe7eeSWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1682f5bbe7eeSWei Wang 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1683f5bbe7eeSWei Wang 			struct rt6_info *entry = rt6_ex->rt6i;
1684e9fa1495SStefano Brivio 
1685e9fa1495SStefano Brivio 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
1686d4ead6b3SDavid Ahern 			 * route), the metrics of its rt->from have already
1687f5bbe7eeSWei Wang 			 * been updated.
1688f5bbe7eeSWei Wang 			 */
1689d4ead6b3SDavid Ahern 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
1690e9fa1495SStefano Brivio 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
1691d4ead6b3SDavid Ahern 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
1692f5bbe7eeSWei Wang 		}
1693f5bbe7eeSWei Wang 		bucket++;
1694f5bbe7eeSWei Wang 	}
1695f5bbe7eeSWei Wang }
1696f5bbe7eeSWei Wang 
1697b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
1698b16cb459SWei Wang 
16998d1c802bSDavid Ahern static void rt6_exceptions_clean_tohost(struct fib6_info *rt,
1700b16cb459SWei Wang 					struct in6_addr *gateway)
1701b16cb459SWei Wang {
1702b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
1703b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
1704b16cb459SWei Wang 	struct hlist_node *tmp;
1705b16cb459SWei Wang 	int i;
1706b16cb459SWei Wang 
1707b16cb459SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1708b16cb459SWei Wang 		return;
1709b16cb459SWei Wang 
1710b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1711b16cb459SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1712b16cb459SWei Wang 				     lockdep_is_held(&rt6_exception_lock));
1713b16cb459SWei Wang 
1714b16cb459SWei Wang 	if (bucket) {
1715b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1716b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1717b16cb459SWei Wang 						  &bucket->chain, hlist) {
1718b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1719b16cb459SWei Wang 
1720b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1721b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
1722b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
1723b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
1724b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
1725b16cb459SWei Wang 				}
1726b16cb459SWei Wang 			}
1727b16cb459SWei Wang 			bucket++;
1728b16cb459SWei Wang 		}
1729b16cb459SWei Wang 	}
1730b16cb459SWei Wang 
1731b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1732b16cb459SWei Wang }
1733b16cb459SWei Wang 
1734c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1735c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
1736c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
1737c757faa8SWei Wang 				      unsigned long now)
1738c757faa8SWei Wang {
1739c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
1740c757faa8SWei Wang 
17411859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
17421859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
17431859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
17441859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
17451859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
17461859bac0SPaolo Abeni 	 */
174731afeb42SWei Wang 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
174831afeb42SWei Wang 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1749c757faa8SWei Wang 			RT6_TRACE("aging clone %p\n", rt);
1750c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1751c757faa8SWei Wang 			return;
175231afeb42SWei Wang 		}
175331afeb42SWei Wang 	} else if (time_after(jiffies, rt->dst.expires)) {
175431afeb42SWei Wang 		RT6_TRACE("purging expired route %p\n", rt);
175531afeb42SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
175631afeb42SWei Wang 		return;
175731afeb42SWei Wang 	}
175831afeb42SWei Wang 
175931afeb42SWei Wang 	if (rt->rt6i_flags & RTF_GATEWAY) {
1760c757faa8SWei Wang 		struct neighbour *neigh;
1761c757faa8SWei Wang 		__u8 neigh_flags = 0;
1762c757faa8SWei Wang 
17631bfa26ffSEric Dumazet 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
17641bfa26ffSEric Dumazet 		if (neigh)
1765c757faa8SWei Wang 			neigh_flags = neigh->flags;
17661bfa26ffSEric Dumazet 
1767c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
1768c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
1769c757faa8SWei Wang 				  rt);
1770c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1771c757faa8SWei Wang 			return;
1772c757faa8SWei Wang 		}
1773c757faa8SWei Wang 	}
177431afeb42SWei Wang 
1775c757faa8SWei Wang 	gc_args->more++;
1776c757faa8SWei Wang }
1777c757faa8SWei Wang 
17788d1c802bSDavid Ahern void rt6_age_exceptions(struct fib6_info *rt,
1779c757faa8SWei Wang 			struct fib6_gc_args *gc_args,
1780c757faa8SWei Wang 			unsigned long now)
1781c757faa8SWei Wang {
1782c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
1783c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
1784c757faa8SWei Wang 	struct hlist_node *tmp;
1785c757faa8SWei Wang 	int i;
1786c757faa8SWei Wang 
1787c757faa8SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1788c757faa8SWei Wang 		return;
1789c757faa8SWei Wang 
17901bfa26ffSEric Dumazet 	rcu_read_lock_bh();
17911bfa26ffSEric Dumazet 	spin_lock(&rt6_exception_lock);
1792c757faa8SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1793c757faa8SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
1794c757faa8SWei Wang 
1795c757faa8SWei Wang 	if (bucket) {
1796c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1797c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1798c757faa8SWei Wang 						  &bucket->chain, hlist) {
1799c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
1800c757faa8SWei Wang 							  gc_args, now);
1801c757faa8SWei Wang 			}
1802c757faa8SWei Wang 			bucket++;
1803c757faa8SWei Wang 		}
1804c757faa8SWei Wang 	}
18051bfa26ffSEric Dumazet 	spin_unlock(&rt6_exception_lock);
18061bfa26ffSEric Dumazet 	rcu_read_unlock_bh();
1807c757faa8SWei Wang }
1808c757faa8SWei Wang 
18091d053da9SDavid Ahern /* must be called with rcu lock held */
18101d053da9SDavid Ahern struct fib6_info *fib6_table_lookup(struct net *net, struct fib6_table *table,
18111d053da9SDavid Ahern 				    int oif, struct flowi6 *fl6, int strict)
18121da177e4SLinus Torvalds {
1813367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
18148d1c802bSDavid Ahern 	struct fib6_info *f6i;
18151da177e4SLinus Torvalds 
18166454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1817367efcb9SMartin KaFai Lau 	saved_fn = fn;
18181da177e4SLinus Torvalds 
1819ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1820ca254490SDavid Ahern 		oif = 0;
1821ca254490SDavid Ahern 
1822a3c00e46SMartin KaFai Lau redo_rt6_select:
182323fb93a4SDavid Ahern 	f6i = rt6_select(net, fn, oif, strict);
182423fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1825a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1826a3c00e46SMartin KaFai Lau 		if (fn)
1827a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
1828367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
1829367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
1830367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
1831367efcb9SMartin KaFai Lau 			fn = saved_fn;
1832367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
1833367efcb9SMartin KaFai Lau 		}
1834a3c00e46SMartin KaFai Lau 	}
1835a3c00e46SMartin KaFai Lau 
1836d4bea421SDavid Ahern 	trace_fib6_table_lookup(net, f6i, table, fl6);
1837d52d3997SMartin KaFai Lau 
18381d053da9SDavid Ahern 	return f6i;
18391d053da9SDavid Ahern }
18401d053da9SDavid Ahern 
18411d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
18421d053da9SDavid Ahern 			       int oif, struct flowi6 *fl6,
18431d053da9SDavid Ahern 			       const struct sk_buff *skb, int flags)
18441d053da9SDavid Ahern {
18451d053da9SDavid Ahern 	struct fib6_info *f6i;
18461d053da9SDavid Ahern 	struct rt6_info *rt;
18471d053da9SDavid Ahern 	int strict = 0;
18481d053da9SDavid Ahern 
18491d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IFACE;
18501d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
18511d053da9SDavid Ahern 	if (net->ipv6.devconf_all->forwarding == 0)
18521d053da9SDavid Ahern 		strict |= RT6_LOOKUP_F_REACHABLE;
18531d053da9SDavid Ahern 
18541d053da9SDavid Ahern 	rcu_read_lock();
18551d053da9SDavid Ahern 
18561d053da9SDavid Ahern 	f6i = fib6_table_lookup(net, table, oif, fl6, strict);
18571d053da9SDavid Ahern 	if (f6i->fib6_nsiblings)
18581d053da9SDavid Ahern 		f6i = fib6_multipath_select(net, f6i, fl6, oif, skb, strict);
18591d053da9SDavid Ahern 
186023fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1861421842edSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
186266f5d6ceSWei Wang 		rcu_read_unlock();
1863d3843fe5SWei Wang 		dst_hold(&rt->dst);
1864d3843fe5SWei Wang 		return rt;
1865d3843fe5SWei Wang 	}
186623fb93a4SDavid Ahern 
186723fb93a4SDavid Ahern 	/*Search through exception table */
186823fb93a4SDavid Ahern 	rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
186923fb93a4SDavid Ahern 	if (rt) {
1870d4ead6b3SDavid Ahern 		if (ip6_hold_safe(net, &rt, true))
18711da177e4SLinus Torvalds 			dst_use_noref(&rt->dst, jiffies);
1872d4ead6b3SDavid Ahern 
187366f5d6ceSWei Wang 		rcu_read_unlock();
1874d52d3997SMartin KaFai Lau 		return rt;
18753da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
187693c2fb25SDavid Ahern 			    !(f6i->fib6_flags & RTF_GATEWAY))) {
18773da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
18783da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
18793da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
18803da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
18813da59bd9SMartin KaFai Lau 		 */
18823da59bd9SMartin KaFai Lau 		struct rt6_info *uncached_rt;
18833da59bd9SMartin KaFai Lau 
188423fb93a4SDavid Ahern 		uncached_rt = ip6_rt_cache_alloc(f6i, &fl6->daddr, NULL);
1885d52d3997SMartin KaFai Lau 
18864d85cd0cSDavid Ahern 		rcu_read_unlock();
18873da59bd9SMartin KaFai Lau 
18881cfb71eeSWei Wang 		if (uncached_rt) {
18891cfb71eeSWei Wang 			/* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
18901cfb71eeSWei Wang 			 * No need for another dst_hold()
18911cfb71eeSWei Wang 			 */
18928d0b94afSMartin KaFai Lau 			rt6_uncached_list_add(uncached_rt);
189381eb8447SWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
18941cfb71eeSWei Wang 		} else {
18953da59bd9SMartin KaFai Lau 			uncached_rt = net->ipv6.ip6_null_entry;
18963da59bd9SMartin KaFai Lau 			dst_hold(&uncached_rt->dst);
18971cfb71eeSWei Wang 		}
1898b811580dSDavid Ahern 
18993da59bd9SMartin KaFai Lau 		return uncached_rt;
1900d52d3997SMartin KaFai Lau 	} else {
1901d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
1902d52d3997SMartin KaFai Lau 
1903d52d3997SMartin KaFai Lau 		struct rt6_info *pcpu_rt;
1904d52d3997SMartin KaFai Lau 
1905951f788aSEric Dumazet 		local_bh_disable();
190623fb93a4SDavid Ahern 		pcpu_rt = rt6_get_pcpu_route(f6i);
1907d52d3997SMartin KaFai Lau 
190893531c67SDavid Ahern 		if (!pcpu_rt)
190923fb93a4SDavid Ahern 			pcpu_rt = rt6_make_pcpu_route(net, f6i);
191093531c67SDavid Ahern 
1911951f788aSEric Dumazet 		local_bh_enable();
1912951f788aSEric Dumazet 		rcu_read_unlock();
1913d4bea421SDavid Ahern 
1914d52d3997SMartin KaFai Lau 		return pcpu_rt;
1915d52d3997SMartin KaFai Lau 	}
1916c71099acSThomas Graf }
19179ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
1918c71099acSThomas Graf 
1919b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net,
1920b75cc8f9SDavid Ahern 					    struct fib6_table *table,
1921b75cc8f9SDavid Ahern 					    struct flowi6 *fl6,
1922b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
1923b75cc8f9SDavid Ahern 					    int flags)
19244acad72dSPavel Emelyanov {
1925b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
19264acad72dSPavel Emelyanov }
19274acad72dSPavel Emelyanov 
1928d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
192972331bc0SShmulik Ladkani 					 struct net_device *dev,
1930b75cc8f9SDavid Ahern 					 struct flowi6 *fl6,
1931b75cc8f9SDavid Ahern 					 const struct sk_buff *skb,
1932b75cc8f9SDavid Ahern 					 int flags)
193372331bc0SShmulik Ladkani {
193472331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
193572331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
193672331bc0SShmulik Ladkani 
1937b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
193872331bc0SShmulik Ladkani }
1939d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
194072331bc0SShmulik Ladkani 
194123aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
19425e5d6fedSRoopa Prabhu 				  struct flow_keys *keys,
19435e5d6fedSRoopa Prabhu 				  struct flow_keys *flkeys)
194423aebdacSJakub Sitnicki {
194523aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
194623aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
19475e5d6fedSRoopa Prabhu 	struct flow_keys *_flkeys = flkeys;
194823aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
194923aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
195023aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
1951cea67a2dSEric Dumazet 	struct icmp6hdr _icmph;
195223aebdacSJakub Sitnicki 
195323aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
195423aebdacSJakub Sitnicki 		goto out;
195523aebdacSJakub Sitnicki 
1956cea67a2dSEric Dumazet 	icmph = skb_header_pointer(skb, skb_transport_offset(skb),
1957cea67a2dSEric Dumazet 				   sizeof(_icmph), &_icmph);
1958cea67a2dSEric Dumazet 	if (!icmph)
1959cea67a2dSEric Dumazet 		goto out;
1960cea67a2dSEric Dumazet 
196123aebdacSJakub Sitnicki 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
196223aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
196323aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
196423aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
196523aebdacSJakub Sitnicki 		goto out;
196623aebdacSJakub Sitnicki 
196723aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
196823aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
196923aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
197023aebdacSJakub Sitnicki 	if (!inner_iph)
197123aebdacSJakub Sitnicki 		goto out;
197223aebdacSJakub Sitnicki 
197323aebdacSJakub Sitnicki 	key_iph = inner_iph;
19745e5d6fedSRoopa Prabhu 	_flkeys = NULL;
197523aebdacSJakub Sitnicki out:
19765e5d6fedSRoopa Prabhu 	if (_flkeys) {
19775e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
19785e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
19795e5d6fedSRoopa Prabhu 		keys->tags.flow_label = _flkeys->tags.flow_label;
19805e5d6fedSRoopa Prabhu 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
19815e5d6fedSRoopa Prabhu 	} else {
198223aebdacSJakub Sitnicki 		keys->addrs.v6addrs.src = key_iph->saddr;
198323aebdacSJakub Sitnicki 		keys->addrs.v6addrs.dst = key_iph->daddr;
1984fa1be7e0SMichal Kubecek 		keys->tags.flow_label = ip6_flowlabel(key_iph);
198523aebdacSJakub Sitnicki 		keys->basic.ip_proto = key_iph->nexthdr;
198623aebdacSJakub Sitnicki 	}
19875e5d6fedSRoopa Prabhu }
198823aebdacSJakub Sitnicki 
198923aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
1990b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
1991b4bac172SDavid Ahern 		       const struct sk_buff *skb, struct flow_keys *flkeys)
199223aebdacSJakub Sitnicki {
199323aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
19949a2a537aSDavid Ahern 	u32 mhash;
199523aebdacSJakub Sitnicki 
1996bbfa047aSDavid S. Miller 	switch (ip6_multipath_hash_policy(net)) {
1997b4bac172SDavid Ahern 	case 0:
19986f74b6c2SDavid Ahern 		memset(&hash_keys, 0, sizeof(hash_keys));
19996f74b6c2SDavid Ahern 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
20009a2a537aSDavid Ahern 		if (skb) {
20015e5d6fedSRoopa Prabhu 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
20029a2a537aSDavid Ahern 		} else {
20039a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
20049a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2005fa1be7e0SMichal Kubecek 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
20069a2a537aSDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
200723aebdacSJakub Sitnicki 		}
2008b4bac172SDavid Ahern 		break;
2009b4bac172SDavid Ahern 	case 1:
2010b4bac172SDavid Ahern 		if (skb) {
2011b4bac172SDavid Ahern 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2012b4bac172SDavid Ahern 			struct flow_keys keys;
2013b4bac172SDavid Ahern 
2014b4bac172SDavid Ahern 			/* short-circuit if we already have L4 hash present */
2015b4bac172SDavid Ahern 			if (skb->l4_hash)
2016b4bac172SDavid Ahern 				return skb_get_hash_raw(skb) >> 1;
2017b4bac172SDavid Ahern 
2018b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2019b4bac172SDavid Ahern 
2020b4bac172SDavid Ahern                         if (!flkeys) {
2021b4bac172SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2022b4bac172SDavid Ahern 				flkeys = &keys;
2023b4bac172SDavid Ahern 			}
2024b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2025b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2026b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2027b4bac172SDavid Ahern 			hash_keys.ports.src = flkeys->ports.src;
2028b4bac172SDavid Ahern 			hash_keys.ports.dst = flkeys->ports.dst;
2029b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2030b4bac172SDavid Ahern 		} else {
2031b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2032b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2033b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2034b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2035b4bac172SDavid Ahern 			hash_keys.ports.src = fl6->fl6_sport;
2036b4bac172SDavid Ahern 			hash_keys.ports.dst = fl6->fl6_dport;
2037b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2038b4bac172SDavid Ahern 		}
2039b4bac172SDavid Ahern 		break;
2040b4bac172SDavid Ahern 	}
20419a2a537aSDavid Ahern 	mhash = flow_hash_from_keys(&hash_keys);
204223aebdacSJakub Sitnicki 
20439a2a537aSDavid Ahern 	return mhash >> 1;
204423aebdacSJakub Sitnicki }
204523aebdacSJakub Sitnicki 
2046c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
2047c71099acSThomas Graf {
2048b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2049c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
2050adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2051904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
20524c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
2053e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
20544c9483b2SDavid S. Miller 		.daddr = iph->daddr,
20554c9483b2SDavid S. Miller 		.saddr = iph->saddr,
20566502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
20574c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
20584c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
2059c71099acSThomas Graf 	};
20605e5d6fedSRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2061adaa70bbSThomas Graf 
2062904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
206346fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2064904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
20655e5d6fedSRoopa Prabhu 
20665e5d6fedSRoopa Prabhu 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
20675e5d6fedSRoopa Prabhu 		flkeys = &_flkeys;
20685e5d6fedSRoopa Prabhu 
206923aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2070b4bac172SDavid Ahern 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
207106e9d040SJiri Benc 	skb_dst_drop(skb);
2072b75cc8f9SDavid Ahern 	skb_dst_set(skb,
2073b75cc8f9SDavid Ahern 		    ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
2074c71099acSThomas Graf }
2075c71099acSThomas Graf 
2076b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net,
2077b75cc8f9SDavid Ahern 					     struct fib6_table *table,
2078b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
2079b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2080b75cc8f9SDavid Ahern 					     int flags)
2081c71099acSThomas Graf {
2082b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2083c71099acSThomas Graf }
2084c71099acSThomas Graf 
20856f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
20866f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags)
2087c71099acSThomas Graf {
2088d46a9d67SDavid Ahern 	bool any_src;
2089c71099acSThomas Graf 
20903ede0bbcSRobert Shearman 	if (ipv6_addr_type(&fl6->daddr) &
20913ede0bbcSRobert Shearman 	    (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
20924c1feac5SDavid Ahern 		struct dst_entry *dst;
20934c1feac5SDavid Ahern 
20944c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
2095ca254490SDavid Ahern 		if (dst)
2096ca254490SDavid Ahern 			return dst;
20974c1feac5SDavid Ahern 	}
2098ca254490SDavid Ahern 
20991fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
21004dc27d1cSDavid McCullough 
2101d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
2102741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2103d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
210477d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
2105c71099acSThomas Graf 
2106d46a9d67SDavid Ahern 	if (!any_src)
2107adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
21080c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
21090c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2110adaa70bbSThomas Graf 
2111b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
21121da177e4SLinus Torvalds }
21136f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
21141da177e4SLinus Torvalds 
21152774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
211614e50e57SDavid S. Miller {
21175c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
21181dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
211914e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
212014e50e57SDavid S. Miller 
21211dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
212262cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
212314e50e57SDavid S. Miller 	if (rt) {
21240a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
212581eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
21260a1f5962SMartin KaFai Lau 
2127d8d1f30bSChangli Gao 		new = &rt->dst;
212814e50e57SDavid S. Miller 		new->__use = 1;
2129352e512cSHerbert Xu 		new->input = dst_discard;
2130ede2059dSEric W. Biederman 		new->output = dst_discard_out;
213114e50e57SDavid S. Miller 
2132defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
213314e50e57SDavid S. Miller 
21341dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
21354e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
21360a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
213714e50e57SDavid S. Miller 
213814e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
213914e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
214014e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
214114e50e57SDavid S. Miller #endif
214214e50e57SDavid S. Miller 	}
214314e50e57SDavid S. Miller 
214469ead7afSDavid S. Miller 	dst_release(dst_orig);
214569ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
214614e50e57SDavid S. Miller }
214714e50e57SDavid S. Miller 
21481da177e4SLinus Torvalds /*
21491da177e4SLinus Torvalds  *	Destination cache support functions
21501da177e4SLinus Torvalds  */
21511da177e4SLinus Torvalds 
21528d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie)
21533da59bd9SMartin KaFai Lau {
215436143645SSteffen Klassert 	u32 rt_cookie = 0;
2155c5cff856SWei Wang 
21568ae86971SDavid Ahern 	if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
215793531c67SDavid Ahern 		return false;
215893531c67SDavid Ahern 
215993531c67SDavid Ahern 	if (fib6_check_expired(f6i))
216093531c67SDavid Ahern 		return false;
216193531c67SDavid Ahern 
216293531c67SDavid Ahern 	return true;
216393531c67SDavid Ahern }
216493531c67SDavid Ahern 
2165a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt,
2166a68886a6SDavid Ahern 				   struct fib6_info *from,
2167a68886a6SDavid Ahern 				   u32 cookie)
21683da59bd9SMartin KaFai Lau {
2169c5cff856SWei Wang 	u32 rt_cookie = 0;
2170c5cff856SWei Wang 
2171a68886a6SDavid Ahern 	if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
217293531c67SDavid Ahern 	    rt_cookie != cookie)
21733da59bd9SMartin KaFai Lau 		return NULL;
21743da59bd9SMartin KaFai Lau 
21753da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
21763da59bd9SMartin KaFai Lau 		return NULL;
21773da59bd9SMartin KaFai Lau 
21783da59bd9SMartin KaFai Lau 	return &rt->dst;
21793da59bd9SMartin KaFai Lau }
21803da59bd9SMartin KaFai Lau 
2181a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2182a68886a6SDavid Ahern 					    struct fib6_info *from,
2183a68886a6SDavid Ahern 					    u32 cookie)
21843da59bd9SMartin KaFai Lau {
21855973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
21865973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2187a68886a6SDavid Ahern 	    fib6_check(from, cookie))
21883da59bd9SMartin KaFai Lau 		return &rt->dst;
21893da59bd9SMartin KaFai Lau 	else
21903da59bd9SMartin KaFai Lau 		return NULL;
21913da59bd9SMartin KaFai Lau }
21923da59bd9SMartin KaFai Lau 
21931da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
21941da177e4SLinus Torvalds {
2195a87b7dc9SDavid Ahern 	struct dst_entry *dst_ret;
2196a68886a6SDavid Ahern 	struct fib6_info *from;
21971da177e4SLinus Torvalds 	struct rt6_info *rt;
21981da177e4SLinus Torvalds 
2199a87b7dc9SDavid Ahern 	rt = container_of(dst, struct rt6_info, dst);
2200a87b7dc9SDavid Ahern 
2201a87b7dc9SDavid Ahern 	rcu_read_lock();
22021da177e4SLinus Torvalds 
22036f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
22046f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
22056f3118b5SNicolas Dichtel 	 * into this function always.
22066f3118b5SNicolas Dichtel 	 */
2207e3bc10bdSHannes Frederic Sowa 
2208a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
22094b32b5adSMartin KaFai Lau 
2210a68886a6SDavid Ahern 	if (from && (rt->rt6i_flags & RTF_PCPU ||
2211a68886a6SDavid Ahern 	    unlikely(!list_empty(&rt->rt6i_uncached))))
2212a68886a6SDavid Ahern 		dst_ret = rt6_dst_from_check(rt, from, cookie);
22133da59bd9SMartin KaFai Lau 	else
2214a68886a6SDavid Ahern 		dst_ret = rt6_check(rt, from, cookie);
2215a87b7dc9SDavid Ahern 
2216a87b7dc9SDavid Ahern 	rcu_read_unlock();
2217a87b7dc9SDavid Ahern 
2218a87b7dc9SDavid Ahern 	return dst_ret;
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
22221da177e4SLinus Torvalds {
22231da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds 	if (rt) {
222654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
2227c3c14da0SDavid Ahern 			rcu_read_lock();
222854c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
222993531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
223054c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
22311da177e4SLinus Torvalds 			}
2232c3c14da0SDavid Ahern 			rcu_read_unlock();
223354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
223454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
223554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
223654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
223754c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
223854c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
22391da177e4SLinus Torvalds }
22401da177e4SLinus Torvalds 
22411da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
22421da177e4SLinus Torvalds {
22431da177e4SLinus Torvalds 	struct rt6_info *rt;
22441da177e4SLinus Torvalds 
22453ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
22461da177e4SLinus Torvalds 
2247adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
22481da177e4SLinus Torvalds 	if (rt) {
22498a14e46fSDavid Ahern 		rcu_read_lock();
22501eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
225193531c67SDavid Ahern 			rt6_remove_exception_rt(rt);
2252c5cff856SWei Wang 		} else {
2253a68886a6SDavid Ahern 			struct fib6_info *from;
2254c5cff856SWei Wang 			struct fib6_node *fn;
2255c5cff856SWei Wang 
2256a68886a6SDavid Ahern 			from = rcu_dereference(rt->from);
2257a68886a6SDavid Ahern 			if (from) {
2258a68886a6SDavid Ahern 				fn = rcu_dereference(from->fib6_node);
2259c5cff856SWei Wang 				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2260c5cff856SWei Wang 					fn->fn_sernum = -1;
2261a68886a6SDavid Ahern 			}
22621da177e4SLinus Torvalds 		}
22631da177e4SLinus Torvalds 		rcu_read_unlock();
22641da177e4SLinus Torvalds 	}
22651da177e4SLinus Torvalds }
22661da177e4SLinus Torvalds 
22676a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout)
22686a3e030fSDavid Ahern {
2269a68886a6SDavid Ahern 	if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2270a68886a6SDavid Ahern 		struct fib6_info *from;
2271a68886a6SDavid Ahern 
2272a68886a6SDavid Ahern 		rcu_read_lock();
2273a68886a6SDavid Ahern 		from = rcu_dereference(rt0->from);
2274a68886a6SDavid Ahern 		if (from)
2275a68886a6SDavid Ahern 			rt0->dst.expires = from->expires;
2276a68886a6SDavid Ahern 		rcu_read_unlock();
2277a68886a6SDavid Ahern 	}
22786a3e030fSDavid Ahern 
22796a3e030fSDavid Ahern 	dst_set_expires(&rt0->dst, timeout);
22806a3e030fSDavid Ahern 	rt0->rt6i_flags |= RTF_EXPIRES;
22816700c270SDavid S. Miller }
22821da177e4SLinus Torvalds 
228345e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
228445e4fd26SMartin KaFai Lau {
228545e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
228645e4fd26SMartin KaFai Lau 
2287d4ead6b3SDavid Ahern 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
228845e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
228945e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
229045e4fd26SMartin KaFai Lau }
229145e4fd26SMartin KaFai Lau 
22920d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
22930d3f6d29SMartin KaFai Lau {
22940d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
22951490ed2aSPaolo Abeni 		(rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
22960d3f6d29SMartin KaFai Lau }
22970d3f6d29SMartin KaFai Lau 
229845e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
229945e4fd26SMartin KaFai Lau 				 const struct ipv6hdr *iph, u32 mtu)
23001da177e4SLinus Torvalds {
23010dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
23021da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
23031da177e4SLinus Torvalds 
230419bda36cSXin Long 	if (dst_metric_locked(dst, RTAX_MTU))
230519bda36cSXin Long 		return;
230619bda36cSXin Long 
230745e4fd26SMartin KaFai Lau 	if (iph) {
230845e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
230945e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
231045e4fd26SMartin KaFai Lau 	} else if (sk) {
231145e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
231245e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
231345e4fd26SMartin KaFai Lau 	} else {
23140dec879fSJulian Anastasov 		daddr = NULL;
23150dec879fSJulian Anastasov 		saddr = NULL;
23161da177e4SLinus Torvalds 	}
23170dec879fSJulian Anastasov 	dst_confirm_neigh(dst, daddr);
23180dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
23190dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
23200dec879fSJulian Anastasov 		return;
23210dec879fSJulian Anastasov 
23220dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
23230dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
23242b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
23252b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
23262b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
23270dec879fSJulian Anastasov 	} else if (daddr) {
2328a68886a6SDavid Ahern 		struct fib6_info *from;
23290dec879fSJulian Anastasov 		struct rt6_info *nrt6;
23300dec879fSJulian Anastasov 
23314d85cd0cSDavid Ahern 		rcu_read_lock();
2332a68886a6SDavid Ahern 		from = rcu_dereference(rt6->from);
2333*9c69a132SJonathan Lemon 		if (!from) {
2334*9c69a132SJonathan Lemon 			rcu_read_unlock();
2335*9c69a132SJonathan Lemon 			return;
2336*9c69a132SJonathan Lemon 		}
2337a68886a6SDavid Ahern 		nrt6 = ip6_rt_cache_alloc(from, daddr, saddr);
233845e4fd26SMartin KaFai Lau 		if (nrt6) {
233945e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
2340a68886a6SDavid Ahern 			if (rt6_insert_exception(nrt6, from))
23412b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
234245e4fd26SMartin KaFai Lau 		}
2343a68886a6SDavid Ahern 		rcu_read_unlock();
234445e4fd26SMartin KaFai Lau 	}
234545e4fd26SMartin KaFai Lau }
234645e4fd26SMartin KaFai Lau 
234745e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
234845e4fd26SMartin KaFai Lau 			       struct sk_buff *skb, u32 mtu)
234945e4fd26SMartin KaFai Lau {
235045e4fd26SMartin KaFai Lau 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
23511da177e4SLinus Torvalds }
23521da177e4SLinus Torvalds 
235342ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2354e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
235581aded24SDavid S. Miller {
235681aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
235781aded24SDavid S. Miller 	struct dst_entry *dst;
2358dc92095dSMaciej Żenczykowski 	struct flowi6 fl6 = {
2359dc92095dSMaciej Żenczykowski 		.flowi6_oif = oif,
2360dc92095dSMaciej Żenczykowski 		.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2361dc92095dSMaciej Żenczykowski 		.daddr = iph->daddr,
2362dc92095dSMaciej Żenczykowski 		.saddr = iph->saddr,
2363dc92095dSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
2364dc92095dSMaciej Żenczykowski 		.flowi6_uid = uid,
2365dc92095dSMaciej Żenczykowski 	};
236681aded24SDavid S. Miller 
236781aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
236881aded24SDavid S. Miller 	if (!dst->error)
236945e4fd26SMartin KaFai Lau 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
237081aded24SDavid S. Miller 	dst_release(dst);
237181aded24SDavid S. Miller }
237281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
237381aded24SDavid S. Miller 
237481aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
237581aded24SDavid S. Miller {
23767ddacfa5SDavid Ahern 	int oif = sk->sk_bound_dev_if;
237733c162a9SMartin KaFai Lau 	struct dst_entry *dst;
237833c162a9SMartin KaFai Lau 
23797ddacfa5SDavid Ahern 	if (!oif && skb->dev)
23807ddacfa5SDavid Ahern 		oif = l3mdev_master_ifindex(skb->dev);
23817ddacfa5SDavid Ahern 
23827ddacfa5SDavid Ahern 	ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
238333c162a9SMartin KaFai Lau 
238433c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
238533c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
238633c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
238733c162a9SMartin KaFai Lau 		return;
238833c162a9SMartin KaFai Lau 
238933c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
239033c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
239133c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
239233c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
239381aded24SDavid S. Miller }
239481aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
239581aded24SDavid S. Miller 
23967d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
23977d6850f7SAlexey Kodanev 			   const struct flowi6 *fl6)
23987d6850f7SAlexey Kodanev {
23997d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
24007d6850f7SAlexey Kodanev 	struct ipv6_pinfo *np = inet6_sk(sk);
24017d6850f7SAlexey Kodanev #endif
24027d6850f7SAlexey Kodanev 
24037d6850f7SAlexey Kodanev 	ip6_dst_store(sk, dst,
24047d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
24057d6850f7SAlexey Kodanev 		      &sk->sk_v6_daddr : NULL,
24067d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
24077d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
24087d6850f7SAlexey Kodanev 		      &np->saddr :
24097d6850f7SAlexey Kodanev #endif
24107d6850f7SAlexey Kodanev 		      NULL);
24117d6850f7SAlexey Kodanev }
24127d6850f7SAlexey Kodanev 
2413b55b76b2SDuan Jiong /* Handle redirects */
2414b55b76b2SDuan Jiong struct ip6rd_flowi {
2415b55b76b2SDuan Jiong 	struct flowi6 fl6;
2416b55b76b2SDuan Jiong 	struct in6_addr gateway;
2417b55b76b2SDuan Jiong };
2418b55b76b2SDuan Jiong 
2419b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
2420b55b76b2SDuan Jiong 					     struct fib6_table *table,
2421b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2422b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2423b55b76b2SDuan Jiong 					     int flags)
2424b55b76b2SDuan Jiong {
2425b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
242623fb93a4SDavid Ahern 	struct rt6_info *ret = NULL, *rt_cache;
24278d1c802bSDavid Ahern 	struct fib6_info *rt;
2428b55b76b2SDuan Jiong 	struct fib6_node *fn;
2429b55b76b2SDuan Jiong 
2430b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
243167c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2432b55b76b2SDuan Jiong 	 *
2433b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2434b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2435b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2436b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2437b55b76b2SDuan Jiong 	 * routes.
2438b55b76b2SDuan Jiong 	 */
2439b55b76b2SDuan Jiong 
244066f5d6ceSWei Wang 	rcu_read_lock();
24416454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2442b55b76b2SDuan Jiong restart:
244366f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
24445e670d84SDavid Ahern 		if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
24458067bb8cSIdo Schimmel 			continue;
244614895687SDavid Ahern 		if (fib6_check_expired(rt))
2447b55b76b2SDuan Jiong 			continue;
244893c2fb25SDavid Ahern 		if (rt->fib6_flags & RTF_REJECT)
2449b55b76b2SDuan Jiong 			break;
245093c2fb25SDavid Ahern 		if (!(rt->fib6_flags & RTF_GATEWAY))
2451b55b76b2SDuan Jiong 			continue;
24525e670d84SDavid Ahern 		if (fl6->flowi6_oif != rt->fib6_nh.nh_dev->ifindex)
2453b55b76b2SDuan Jiong 			continue;
24542b760fcfSWei Wang 		/* rt_cache's gateway might be different from its 'parent'
24552b760fcfSWei Wang 		 * in the case of an ip redirect.
24562b760fcfSWei Wang 		 * So we keep searching in the exception table if the gateway
24572b760fcfSWei Wang 		 * is different.
24582b760fcfSWei Wang 		 */
24595e670d84SDavid Ahern 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.nh_gw)) {
24602b760fcfSWei Wang 			rt_cache = rt6_find_cached_rt(rt,
24612b760fcfSWei Wang 						      &fl6->daddr,
24622b760fcfSWei Wang 						      &fl6->saddr);
24632b760fcfSWei Wang 			if (rt_cache &&
24642b760fcfSWei Wang 			    ipv6_addr_equal(&rdfl->gateway,
24652b760fcfSWei Wang 					    &rt_cache->rt6i_gateway)) {
246623fb93a4SDavid Ahern 				ret = rt_cache;
24672b760fcfSWei Wang 				break;
24682b760fcfSWei Wang 			}
2469b55b76b2SDuan Jiong 			continue;
24702b760fcfSWei Wang 		}
2471b55b76b2SDuan Jiong 		break;
2472b55b76b2SDuan Jiong 	}
2473b55b76b2SDuan Jiong 
2474b55b76b2SDuan Jiong 	if (!rt)
2475421842edSDavid Ahern 		rt = net->ipv6.fib6_null_entry;
247693c2fb25SDavid Ahern 	else if (rt->fib6_flags & RTF_REJECT) {
247723fb93a4SDavid Ahern 		ret = net->ipv6.ip6_null_entry;
2478b0a1ba59SMartin KaFai Lau 		goto out;
2479b0a1ba59SMartin KaFai Lau 	}
2480b0a1ba59SMartin KaFai Lau 
2481421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
2482a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2483a3c00e46SMartin KaFai Lau 		if (fn)
2484a3c00e46SMartin KaFai Lau 			goto restart;
2485b55b76b2SDuan Jiong 	}
2486a3c00e46SMartin KaFai Lau 
2487b0a1ba59SMartin KaFai Lau out:
248823fb93a4SDavid Ahern 	if (ret)
2489e873e4b9SWei Wang 		ip6_hold_safe(net, &ret, true);
249023fb93a4SDavid Ahern 	else
249123fb93a4SDavid Ahern 		ret = ip6_create_rt_rcu(rt);
2492b55b76b2SDuan Jiong 
249366f5d6ceSWei Wang 	rcu_read_unlock();
2494b55b76b2SDuan Jiong 
2495b65f164dSPaolo Abeni 	trace_fib6_table_lookup(net, rt, table, fl6);
249623fb93a4SDavid Ahern 	return ret;
2497b55b76b2SDuan Jiong };
2498b55b76b2SDuan Jiong 
2499b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
2500b55b76b2SDuan Jiong 					    const struct flowi6 *fl6,
2501b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2502b55b76b2SDuan Jiong 					    const struct in6_addr *gateway)
2503b55b76b2SDuan Jiong {
2504b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2505b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
2506b55b76b2SDuan Jiong 
2507b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
2508b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
2509b55b76b2SDuan Jiong 
2510b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, &rdfl.fl6, skb,
2511b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
2512b55b76b2SDuan Jiong }
2513b55b76b2SDuan Jiong 
2514e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2515e2d118a1SLorenzo Colitti 		  kuid_t uid)
25163a5ad2eeSDavid S. Miller {
25173a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
25183a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
25191f7f10acSMaciej Żenczykowski 	struct flowi6 fl6 = {
25201f7f10acSMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
25211f7f10acSMaciej Żenczykowski 		.flowi6_oif = oif,
25221f7f10acSMaciej Żenczykowski 		.flowi6_mark = mark,
25231f7f10acSMaciej Żenczykowski 		.daddr = iph->daddr,
25241f7f10acSMaciej Żenczykowski 		.saddr = iph->saddr,
25251f7f10acSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
25261f7f10acSMaciej Żenczykowski 		.flowi6_uid = uid,
25271f7f10acSMaciej Żenczykowski 	};
25283a5ad2eeSDavid S. Miller 
2529b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
25306700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
25313a5ad2eeSDavid S. Miller 	dst_release(dst);
25323a5ad2eeSDavid S. Miller }
25333a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
25343a5ad2eeSDavid S. Miller 
2535d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
2536c92a59ecSDuan Jiong {
2537c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2538c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2539c92a59ecSDuan Jiong 	struct dst_entry *dst;
25400b26fb17SMaciej Żenczykowski 	struct flowi6 fl6 = {
25410b26fb17SMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
25420b26fb17SMaciej Żenczykowski 		.flowi6_oif = oif,
25430b26fb17SMaciej Żenczykowski 		.daddr = msg->dest,
25440b26fb17SMaciej Żenczykowski 		.saddr = iph->daddr,
25450b26fb17SMaciej Żenczykowski 		.flowi6_uid = sock_net_uid(net, NULL),
25460b26fb17SMaciej Żenczykowski 	};
2547c92a59ecSDuan Jiong 
2548b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
2549c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
2550c92a59ecSDuan Jiong 	dst_release(dst);
2551c92a59ecSDuan Jiong }
2552c92a59ecSDuan Jiong 
25533a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
25543a5ad2eeSDavid S. Miller {
2555e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2556e2d118a1SLorenzo Colitti 		     sk->sk_uid);
25573a5ad2eeSDavid S. Miller }
25583a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
25593a5ad2eeSDavid S. Miller 
25600dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
25611da177e4SLinus Torvalds {
25620dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
25630dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
25640dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
25650dbaee3bSDavid S. Miller 
25661da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
25671da177e4SLinus Torvalds 
25685578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
25695578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
25701da177e4SLinus Torvalds 
25711da177e4SLinus Torvalds 	/*
25721da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
25731da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
25741da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
25751da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
25761da177e4SLinus Torvalds 	 */
25771da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
25781da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
25791da177e4SLinus Torvalds 	return mtu;
25801da177e4SLinus Torvalds }
25811da177e4SLinus Torvalds 
2582ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
2583d33e4553SDavid S. Miller {
2584d33e4553SDavid S. Miller 	struct inet6_dev *idev;
2585d4ead6b3SDavid Ahern 	unsigned int mtu;
2586618f9bc7SSteffen Klassert 
25874b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
25884b32b5adSMartin KaFai Lau 	if (mtu)
25894b32b5adSMartin KaFai Lau 		goto out;
25904b32b5adSMartin KaFai Lau 
2591618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
2592d33e4553SDavid S. Miller 
2593d33e4553SDavid S. Miller 	rcu_read_lock();
2594d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
2595d33e4553SDavid S. Miller 	if (idev)
2596d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
2597d33e4553SDavid S. Miller 	rcu_read_unlock();
2598d33e4553SDavid S. Miller 
259930f78d8eSEric Dumazet out:
260014972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
260114972cbdSRoopa Prabhu 
260214972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
2603d33e4553SDavid S. Miller }
2604d33e4553SDavid S. Miller 
2605901731b8SDavid Ahern /* MTU selection:
2606901731b8SDavid Ahern  * 1. mtu on route is locked - use it
2607901731b8SDavid Ahern  * 2. mtu from nexthop exception
2608901731b8SDavid Ahern  * 3. mtu from egress device
2609901731b8SDavid Ahern  *
2610901731b8SDavid Ahern  * based on ip6_dst_mtu_forward and exception logic of
2611901731b8SDavid Ahern  * rt6_find_cached_rt; called with rcu_read_lock
2612901731b8SDavid Ahern  */
2613901731b8SDavid Ahern u32 ip6_mtu_from_fib6(struct fib6_info *f6i, struct in6_addr *daddr,
2614901731b8SDavid Ahern 		      struct in6_addr *saddr)
2615901731b8SDavid Ahern {
2616901731b8SDavid Ahern 	struct rt6_exception_bucket *bucket;
2617901731b8SDavid Ahern 	struct rt6_exception *rt6_ex;
2618901731b8SDavid Ahern 	struct in6_addr *src_key;
2619901731b8SDavid Ahern 	struct inet6_dev *idev;
2620901731b8SDavid Ahern 	u32 mtu = 0;
2621901731b8SDavid Ahern 
2622901731b8SDavid Ahern 	if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
2623901731b8SDavid Ahern 		mtu = f6i->fib6_pmtu;
2624901731b8SDavid Ahern 		if (mtu)
2625901731b8SDavid Ahern 			goto out;
2626901731b8SDavid Ahern 	}
2627901731b8SDavid Ahern 
2628901731b8SDavid Ahern 	src_key = NULL;
2629901731b8SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
2630901731b8SDavid Ahern 	if (f6i->fib6_src.plen)
2631901731b8SDavid Ahern 		src_key = saddr;
2632901731b8SDavid Ahern #endif
2633901731b8SDavid Ahern 
2634901731b8SDavid Ahern 	bucket = rcu_dereference(f6i->rt6i_exception_bucket);
2635901731b8SDavid Ahern 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
2636901731b8SDavid Ahern 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
2637901731b8SDavid Ahern 		mtu = dst_metric_raw(&rt6_ex->rt6i->dst, RTAX_MTU);
2638901731b8SDavid Ahern 
2639901731b8SDavid Ahern 	if (likely(!mtu)) {
2640901731b8SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(f6i);
2641901731b8SDavid Ahern 
2642901731b8SDavid Ahern 		mtu = IPV6_MIN_MTU;
2643901731b8SDavid Ahern 		idev = __in6_dev_get(dev);
2644901731b8SDavid Ahern 		if (idev && idev->cnf.mtu6 > mtu)
2645901731b8SDavid Ahern 			mtu = idev->cnf.mtu6;
2646901731b8SDavid Ahern 	}
2647901731b8SDavid Ahern 
2648901731b8SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2649901731b8SDavid Ahern out:
2650901731b8SDavid Ahern 	return mtu - lwtunnel_headroom(fib6_info_nh_lwt(f6i), mtu);
2651901731b8SDavid Ahern }
2652901731b8SDavid Ahern 
26533b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
265487a11578SDavid S. Miller 				  struct flowi6 *fl6)
26551da177e4SLinus Torvalds {
265687a11578SDavid S. Miller 	struct dst_entry *dst;
26571da177e4SLinus Torvalds 	struct rt6_info *rt;
26581da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
2659c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26601da177e4SLinus Torvalds 
266138308473SDavid S. Miller 	if (unlikely(!idev))
2662122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
26631da177e4SLinus Torvalds 
2664ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
266538308473SDavid S. Miller 	if (unlikely(!rt)) {
26661da177e4SLinus Torvalds 		in6_dev_put(idev);
266787a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
26681da177e4SLinus Torvalds 		goto out;
26691da177e4SLinus Torvalds 	}
26701da177e4SLinus Torvalds 
26718e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
2672588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
26738e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
2674550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
267587a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
26768e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
26778e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
267814edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
26791da177e4SLinus Torvalds 
26804c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
2681587fea74SWei Wang 	 * do proper release of the net_device
2682587fea74SWei Wang 	 */
2683587fea74SWei Wang 	rt6_uncached_list_add(rt);
268481eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
26851da177e4SLinus Torvalds 
268687a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
268787a11578SDavid S. Miller 
26881da177e4SLinus Torvalds out:
268987a11578SDavid S. Miller 	return dst;
26901da177e4SLinus Torvalds }
26911da177e4SLinus Torvalds 
2692569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
26931da177e4SLinus Torvalds {
269486393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
26957019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
26967019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
26977019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
26987019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
26997019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
2700fc66f95cSEric Dumazet 	int entries;
27011da177e4SLinus Torvalds 
2702fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
270349a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
2704fc66f95cSEric Dumazet 	    entries <= rt_max_size)
27051da177e4SLinus Torvalds 		goto out;
27061da177e4SLinus Torvalds 
27076891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
270814956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
2709fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
2710fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
27117019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
27121da177e4SLinus Torvalds out:
27137019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
2714fc66f95cSEric Dumazet 	return entries > rt_max_size;
27151da177e4SLinus Torvalds }
27161da177e4SLinus Torvalds 
27178c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net,
27188c14586fSDavid Ahern 					    struct fib6_config *cfg,
2719f4797b33SDavid Ahern 					    const struct in6_addr *gw_addr,
2720f4797b33SDavid Ahern 					    u32 tbid, int flags)
27218c14586fSDavid Ahern {
27228c14586fSDavid Ahern 	struct flowi6 fl6 = {
27238c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
27248c14586fSDavid Ahern 		.daddr = *gw_addr,
27258c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
27268c14586fSDavid Ahern 	};
27278c14586fSDavid Ahern 	struct fib6_table *table;
27288c14586fSDavid Ahern 	struct rt6_info *rt;
27298c14586fSDavid Ahern 
2730f4797b33SDavid Ahern 	table = fib6_get_table(net, tbid);
27318c14586fSDavid Ahern 	if (!table)
27328c14586fSDavid Ahern 		return NULL;
27338c14586fSDavid Ahern 
27348c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
27358c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
27368c14586fSDavid Ahern 
2737f4797b33SDavid Ahern 	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
2738b75cc8f9SDavid Ahern 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
27398c14586fSDavid Ahern 
27408c14586fSDavid Ahern 	/* if table lookup failed, fall back to full lookup */
27418c14586fSDavid Ahern 	if (rt == net->ipv6.ip6_null_entry) {
27428c14586fSDavid Ahern 		ip6_rt_put(rt);
27438c14586fSDavid Ahern 		rt = NULL;
27448c14586fSDavid Ahern 	}
27458c14586fSDavid Ahern 
27468c14586fSDavid Ahern 	return rt;
27478c14586fSDavid Ahern }
27488c14586fSDavid Ahern 
2749fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net,
2750fc1e64e1SDavid Ahern 				     struct fib6_config *cfg,
27519fbb704cSDavid Ahern 				     const struct net_device *dev,
2752fc1e64e1SDavid Ahern 				     struct netlink_ext_ack *extack)
2753fc1e64e1SDavid Ahern {
275444750f84SDavid Ahern 	u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
2755fc1e64e1SDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
2756fc1e64e1SDavid Ahern 	u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2757bf1dc8baSPaolo Abeni 	struct fib6_info *from;
2758fc1e64e1SDavid Ahern 	struct rt6_info *grt;
2759fc1e64e1SDavid Ahern 	int err;
2760fc1e64e1SDavid Ahern 
2761fc1e64e1SDavid Ahern 	err = 0;
2762fc1e64e1SDavid Ahern 	grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2763fc1e64e1SDavid Ahern 	if (grt) {
2764bf1dc8baSPaolo Abeni 		rcu_read_lock();
2765bf1dc8baSPaolo Abeni 		from = rcu_dereference(grt->from);
276658e354c0SDavid Ahern 		if (!grt->dst.error &&
27674ed591c8SDavid Ahern 		    /* ignore match if it is the default route */
2768bf1dc8baSPaolo Abeni 		    from && !ipv6_addr_any(&from->fib6_dst.addr) &&
276958e354c0SDavid Ahern 		    (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
277044750f84SDavid Ahern 			NL_SET_ERR_MSG(extack,
277144750f84SDavid Ahern 				       "Nexthop has invalid gateway or device mismatch");
2772fc1e64e1SDavid Ahern 			err = -EINVAL;
2773fc1e64e1SDavid Ahern 		}
2774bf1dc8baSPaolo Abeni 		rcu_read_unlock();
2775fc1e64e1SDavid Ahern 
2776fc1e64e1SDavid Ahern 		ip6_rt_put(grt);
2777fc1e64e1SDavid Ahern 	}
2778fc1e64e1SDavid Ahern 
2779fc1e64e1SDavid Ahern 	return err;
2780fc1e64e1SDavid Ahern }
2781fc1e64e1SDavid Ahern 
27821edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net,
27831edce99fSDavid Ahern 			      struct fib6_config *cfg,
27841edce99fSDavid Ahern 			      struct net_device **_dev,
27851edce99fSDavid Ahern 			      struct inet6_dev **idev)
27861edce99fSDavid Ahern {
27871edce99fSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
27881edce99fSDavid Ahern 	struct net_device *dev = _dev ? *_dev : NULL;
27891edce99fSDavid Ahern 	struct rt6_info *grt = NULL;
27901edce99fSDavid Ahern 	int err = -EHOSTUNREACH;
27911edce99fSDavid Ahern 
27921edce99fSDavid Ahern 	if (cfg->fc_table) {
2793f4797b33SDavid Ahern 		int flags = RT6_LOOKUP_F_IFACE;
2794f4797b33SDavid Ahern 
2795f4797b33SDavid Ahern 		grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2796f4797b33SDavid Ahern 					  cfg->fc_table, flags);
27971edce99fSDavid Ahern 		if (grt) {
27981edce99fSDavid Ahern 			if (grt->rt6i_flags & RTF_GATEWAY ||
27991edce99fSDavid Ahern 			    (dev && dev != grt->dst.dev)) {
28001edce99fSDavid Ahern 				ip6_rt_put(grt);
28011edce99fSDavid Ahern 				grt = NULL;
28021edce99fSDavid Ahern 			}
28031edce99fSDavid Ahern 		}
28041edce99fSDavid Ahern 	}
28051edce99fSDavid Ahern 
28061edce99fSDavid Ahern 	if (!grt)
2807b75cc8f9SDavid Ahern 		grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
28081edce99fSDavid Ahern 
28091edce99fSDavid Ahern 	if (!grt)
28101edce99fSDavid Ahern 		goto out;
28111edce99fSDavid Ahern 
28121edce99fSDavid Ahern 	if (dev) {
28131edce99fSDavid Ahern 		if (dev != grt->dst.dev) {
28141edce99fSDavid Ahern 			ip6_rt_put(grt);
28151edce99fSDavid Ahern 			goto out;
28161edce99fSDavid Ahern 		}
28171edce99fSDavid Ahern 	} else {
28181edce99fSDavid Ahern 		*_dev = dev = grt->dst.dev;
28191edce99fSDavid Ahern 		*idev = grt->rt6i_idev;
28201edce99fSDavid Ahern 		dev_hold(dev);
28211edce99fSDavid Ahern 		in6_dev_hold(grt->rt6i_idev);
28221edce99fSDavid Ahern 	}
28231edce99fSDavid Ahern 
28241edce99fSDavid Ahern 	if (!(grt->rt6i_flags & RTF_GATEWAY))
28251edce99fSDavid Ahern 		err = 0;
28261edce99fSDavid Ahern 
28271edce99fSDavid Ahern 	ip6_rt_put(grt);
28281edce99fSDavid Ahern 
28291edce99fSDavid Ahern out:
28301edce99fSDavid Ahern 	return err;
28311edce99fSDavid Ahern }
28321edce99fSDavid Ahern 
28339fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
28349fbb704cSDavid Ahern 			   struct net_device **_dev, struct inet6_dev **idev,
28359fbb704cSDavid Ahern 			   struct netlink_ext_ack *extack)
28369fbb704cSDavid Ahern {
28379fbb704cSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
28389fbb704cSDavid Ahern 	int gwa_type = ipv6_addr_type(gw_addr);
2839232378e8SDavid Ahern 	bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
28409fbb704cSDavid Ahern 	const struct net_device *dev = *_dev;
2841232378e8SDavid Ahern 	bool need_addr_check = !dev;
28429fbb704cSDavid Ahern 	int err = -EINVAL;
28439fbb704cSDavid Ahern 
28449fbb704cSDavid Ahern 	/* if gw_addr is local we will fail to detect this in case
28459fbb704cSDavid Ahern 	 * address is still TENTATIVE (DAD in progress). rt6_lookup()
28469fbb704cSDavid Ahern 	 * will return already-added prefix route via interface that
28479fbb704cSDavid Ahern 	 * prefix route was assigned to, which might be non-loopback.
28489fbb704cSDavid Ahern 	 */
2849232378e8SDavid Ahern 	if (dev &&
2850232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2851232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
28529fbb704cSDavid Ahern 		goto out;
28539fbb704cSDavid Ahern 	}
28549fbb704cSDavid Ahern 
28559fbb704cSDavid Ahern 	if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
28569fbb704cSDavid Ahern 		/* IPv6 strictly inhibits using not link-local
28579fbb704cSDavid Ahern 		 * addresses as nexthop address.
28589fbb704cSDavid Ahern 		 * Otherwise, router will not able to send redirects.
28599fbb704cSDavid Ahern 		 * It is very good, but in some (rare!) circumstances
28609fbb704cSDavid Ahern 		 * (SIT, PtP, NBMA NOARP links) it is handy to allow
28619fbb704cSDavid Ahern 		 * some exceptions. --ANK
28629fbb704cSDavid Ahern 		 * We allow IPv4-mapped nexthops to support RFC4798-type
28639fbb704cSDavid Ahern 		 * addressing
28649fbb704cSDavid Ahern 		 */
28659fbb704cSDavid Ahern 		if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
28669fbb704cSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
28679fbb704cSDavid Ahern 			goto out;
28689fbb704cSDavid Ahern 		}
28699fbb704cSDavid Ahern 
28709fbb704cSDavid Ahern 		if (cfg->fc_flags & RTNH_F_ONLINK)
28719fbb704cSDavid Ahern 			err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
28729fbb704cSDavid Ahern 		else
28739fbb704cSDavid Ahern 			err = ip6_route_check_nh(net, cfg, _dev, idev);
28749fbb704cSDavid Ahern 
28759fbb704cSDavid Ahern 		if (err)
28769fbb704cSDavid Ahern 			goto out;
28779fbb704cSDavid Ahern 	}
28789fbb704cSDavid Ahern 
28799fbb704cSDavid Ahern 	/* reload in case device was changed */
28809fbb704cSDavid Ahern 	dev = *_dev;
28819fbb704cSDavid Ahern 
28829fbb704cSDavid Ahern 	err = -EINVAL;
28839fbb704cSDavid Ahern 	if (!dev) {
28849fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Egress device not specified");
28859fbb704cSDavid Ahern 		goto out;
28869fbb704cSDavid Ahern 	} else if (dev->flags & IFF_LOOPBACK) {
28879fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack,
28889fbb704cSDavid Ahern 			       "Egress device can not be loopback device for this route");
28899fbb704cSDavid Ahern 		goto out;
28909fbb704cSDavid Ahern 	}
2891232378e8SDavid Ahern 
2892232378e8SDavid Ahern 	/* if we did not check gw_addr above, do so now that the
2893232378e8SDavid Ahern 	 * egress device has been resolved.
2894232378e8SDavid Ahern 	 */
2895232378e8SDavid Ahern 	if (need_addr_check &&
2896232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2897232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
2898232378e8SDavid Ahern 		goto out;
2899232378e8SDavid Ahern 	}
2900232378e8SDavid Ahern 
29019fbb704cSDavid Ahern 	err = 0;
29029fbb704cSDavid Ahern out:
29039fbb704cSDavid Ahern 	return err;
29049fbb704cSDavid Ahern }
29059fbb704cSDavid Ahern 
29068d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
2907acb54e3cSDavid Ahern 					      gfp_t gfp_flags,
2908333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
29091da177e4SLinus Torvalds {
29105578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
29118d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
29121da177e4SLinus Torvalds 	struct net_device *dev = NULL;
29131da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
2914c71099acSThomas Graf 	struct fib6_table *table;
29151da177e4SLinus Torvalds 	int addr_type;
29168c5b83f0SRoopa Prabhu 	int err = -EINVAL;
29171da177e4SLinus Torvalds 
2918557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
2919d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
2920d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
2921557c44beSDavid Ahern 		goto out;
2922d5d531cbSDavid Ahern 	}
2923557c44beSDavid Ahern 
29242ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
29252ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
29262ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
29272ea2352eSWei Wang 		goto out;
29282ea2352eSWei Wang 	}
29292ea2352eSWei Wang 
2930e8478e80SDavid Ahern 	if (cfg->fc_type > RTN_MAX) {
2931e8478e80SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid route type");
2932e8478e80SDavid Ahern 		goto out;
2933e8478e80SDavid Ahern 	}
2934e8478e80SDavid Ahern 
2935d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
2936d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
29378c5b83f0SRoopa Prabhu 		goto out;
2938d5d531cbSDavid Ahern 	}
2939d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
2940d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
2941d5d531cbSDavid Ahern 		goto out;
2942d5d531cbSDavid Ahern 	}
29431da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
2944d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
2945d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
2946d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
29478c5b83f0SRoopa Prabhu 		goto out;
2948d5d531cbSDavid Ahern 	}
29491da177e4SLinus Torvalds #endif
295086872cb5SThomas Graf 	if (cfg->fc_ifindex) {
29511da177e4SLinus Torvalds 		err = -ENODEV;
29525578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
29531da177e4SLinus Torvalds 		if (!dev)
29541da177e4SLinus Torvalds 			goto out;
29551da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
29561da177e4SLinus Torvalds 		if (!idev)
29571da177e4SLinus Torvalds 			goto out;
29581da177e4SLinus Torvalds 	}
29591da177e4SLinus Torvalds 
296086872cb5SThomas Graf 	if (cfg->fc_metric == 0)
296186872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
29621da177e4SLinus Torvalds 
2963fc1e64e1SDavid Ahern 	if (cfg->fc_flags & RTNH_F_ONLINK) {
2964fc1e64e1SDavid Ahern 		if (!dev) {
2965fc1e64e1SDavid Ahern 			NL_SET_ERR_MSG(extack,
2966fc1e64e1SDavid Ahern 				       "Nexthop device required for onlink");
2967fc1e64e1SDavid Ahern 			err = -ENODEV;
2968fc1e64e1SDavid Ahern 			goto out;
2969fc1e64e1SDavid Ahern 		}
2970fc1e64e1SDavid Ahern 
2971fc1e64e1SDavid Ahern 		if (!(dev->flags & IFF_UP)) {
2972fc1e64e1SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2973fc1e64e1SDavid Ahern 			err = -ENETDOWN;
2974fc1e64e1SDavid Ahern 			goto out;
2975fc1e64e1SDavid Ahern 		}
2976fc1e64e1SDavid Ahern 	}
2977fc1e64e1SDavid Ahern 
2978c71099acSThomas Graf 	err = -ENOBUFS;
297938308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
2980d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
2981d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
298238308473SDavid S. Miller 		if (!table) {
2983f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
2984d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
2985d71314b4SMatti Vaittinen 		}
2986d71314b4SMatti Vaittinen 	} else {
2987d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
2988d71314b4SMatti Vaittinen 	}
298938308473SDavid S. Miller 
299038308473SDavid S. Miller 	if (!table)
2991c71099acSThomas Graf 		goto out;
2992c71099acSThomas Graf 
29931da177e4SLinus Torvalds 	err = -ENOMEM;
299493531c67SDavid Ahern 	rt = fib6_info_alloc(gfp_flags);
299593531c67SDavid Ahern 	if (!rt)
29961da177e4SLinus Torvalds 		goto out;
299793531c67SDavid Ahern 
2998d7e774f3SDavid Ahern 	rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
2999d7e774f3SDavid Ahern 					       extack);
3000767a2217SDavid Ahern 	if (IS_ERR(rt->fib6_metrics)) {
3001767a2217SDavid Ahern 		err = PTR_ERR(rt->fib6_metrics);
3002fda21d46SEric Dumazet 		/* Do not leave garbage there. */
3003fda21d46SEric Dumazet 		rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3004767a2217SDavid Ahern 		goto out;
3005767a2217SDavid Ahern 	}
3006767a2217SDavid Ahern 
300793531c67SDavid Ahern 	if (cfg->fc_flags & RTF_ADDRCONF)
300893531c67SDavid Ahern 		rt->dst_nocount = true;
30091da177e4SLinus Torvalds 
30101716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
301114895687SDavid Ahern 		fib6_set_expires(rt, jiffies +
30121716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
30131716a961SGao feng 	else
301414895687SDavid Ahern 		fib6_clean_expires(rt);
30151da177e4SLinus Torvalds 
301686872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
301786872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
301893c2fb25SDavid Ahern 	rt->fib6_protocol = cfg->fc_protocol;
301986872cb5SThomas Graf 
302086872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
30211da177e4SLinus Torvalds 
302219e42e45SRoopa Prabhu 	if (cfg->fc_encap) {
302319e42e45SRoopa Prabhu 		struct lwtunnel_state *lwtstate;
302419e42e45SRoopa Prabhu 
302530357d7dSDavid Ahern 		err = lwtunnel_build_state(cfg->fc_encap_type,
3026127eb7cdSTom Herbert 					   cfg->fc_encap, AF_INET6, cfg,
30279ae28727SDavid Ahern 					   &lwtstate, extack);
302819e42e45SRoopa Prabhu 		if (err)
302919e42e45SRoopa Prabhu 			goto out;
30305e670d84SDavid Ahern 		rt->fib6_nh.nh_lwtstate = lwtstate_get(lwtstate);
303125368623STom Herbert 	}
303219e42e45SRoopa Prabhu 
303393c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
303493c2fb25SDavid Ahern 	rt->fib6_dst.plen = cfg->fc_dst_len;
303593c2fb25SDavid Ahern 	if (rt->fib6_dst.plen == 128)
30363b6761d1SDavid Ahern 		rt->dst_host = true;
30371da177e4SLinus Torvalds 
30381da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
303993c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
304093c2fb25SDavid Ahern 	rt->fib6_src.plen = cfg->fc_src_len;
30411da177e4SLinus Torvalds #endif
30421da177e4SLinus Torvalds 
304393c2fb25SDavid Ahern 	rt->fib6_metric = cfg->fc_metric;
30445e670d84SDavid Ahern 	rt->fib6_nh.nh_weight = 1;
30451da177e4SLinus Torvalds 
3046e8478e80SDavid Ahern 	rt->fib6_type = cfg->fc_type;
30471da177e4SLinus Torvalds 
30481da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
30491da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
30501da177e4SLinus Torvalds 	 */
305186872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
305238308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
305338308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
305438308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
30551da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
30565578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
30571da177e4SLinus Torvalds 			if (dev) {
30581da177e4SLinus Torvalds 				dev_put(dev);
30591da177e4SLinus Torvalds 				in6_dev_put(idev);
30601da177e4SLinus Torvalds 			}
30615578689aSDaniel Lezcano 			dev = net->loopback_dev;
30621da177e4SLinus Torvalds 			dev_hold(dev);
30631da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
30641da177e4SLinus Torvalds 			if (!idev) {
30651da177e4SLinus Torvalds 				err = -ENODEV;
30661da177e4SLinus Torvalds 				goto out;
30671da177e4SLinus Torvalds 			}
30681da177e4SLinus Torvalds 		}
306993c2fb25SDavid Ahern 		rt->fib6_flags = RTF_REJECT|RTF_NONEXTHOP;
30701da177e4SLinus Torvalds 		goto install_route;
30711da177e4SLinus Torvalds 	}
30721da177e4SLinus Torvalds 
307386872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
30749fbb704cSDavid Ahern 		err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
30751da177e4SLinus Torvalds 		if (err)
30761da177e4SLinus Torvalds 			goto out;
30779fbb704cSDavid Ahern 
307893531c67SDavid Ahern 		rt->fib6_nh.nh_gw = cfg->fc_gateway;
30791da177e4SLinus Torvalds 	}
30801da177e4SLinus Torvalds 
30811da177e4SLinus Torvalds 	err = -ENODEV;
308238308473SDavid S. Miller 	if (!dev)
30831da177e4SLinus Torvalds 		goto out;
30841da177e4SLinus Torvalds 
3085428604fbSLorenzo Bianconi 	if (idev->cnf.disable_ipv6) {
3086428604fbSLorenzo Bianconi 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3087428604fbSLorenzo Bianconi 		err = -EACCES;
3088428604fbSLorenzo Bianconi 		goto out;
3089428604fbSLorenzo Bianconi 	}
3090428604fbSLorenzo Bianconi 
3091955ec4cbSDavid Ahern 	if (!(dev->flags & IFF_UP)) {
3092955ec4cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3093955ec4cbSDavid Ahern 		err = -ENETDOWN;
3094955ec4cbSDavid Ahern 		goto out;
3095955ec4cbSDavid Ahern 	}
3096955ec4cbSDavid Ahern 
3097c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3098c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3099d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
3100c3968a85SDaniel Walter 			err = -EINVAL;
3101c3968a85SDaniel Walter 			goto out;
3102c3968a85SDaniel Walter 		}
310393c2fb25SDavid Ahern 		rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
310493c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 128;
3105c3968a85SDaniel Walter 	} else
310693c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
3107c3968a85SDaniel Walter 
310893c2fb25SDavid Ahern 	rt->fib6_flags = cfg->fc_flags;
31091da177e4SLinus Torvalds 
31101da177e4SLinus Torvalds install_route:
311193c2fb25SDavid Ahern 	if (!(rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
31125609b80aSIdo Schimmel 	    !netif_carrier_ok(dev))
31135e670d84SDavid Ahern 		rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
31145e670d84SDavid Ahern 	rt->fib6_nh.nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK);
311593531c67SDavid Ahern 	rt->fib6_nh.nh_dev = dev;
311693c2fb25SDavid Ahern 	rt->fib6_table = table;
311763152fc0SDaniel Lezcano 
3118dcd1f572SDavid Ahern 	if (idev)
3119dcd1f572SDavid Ahern 		in6_dev_put(idev);
3120dcd1f572SDavid Ahern 
31218c5b83f0SRoopa Prabhu 	return rt;
31221da177e4SLinus Torvalds out:
31231da177e4SLinus Torvalds 	if (dev)
31241da177e4SLinus Torvalds 		dev_put(dev);
31251da177e4SLinus Torvalds 	if (idev)
31261da177e4SLinus Torvalds 		in6_dev_put(idev);
31276b9ea5a6SRoopa Prabhu 
312893531c67SDavid Ahern 	fib6_info_release(rt);
31298c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
31306b9ea5a6SRoopa Prabhu }
31316b9ea5a6SRoopa Prabhu 
3132acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3133333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
31346b9ea5a6SRoopa Prabhu {
31358d1c802bSDavid Ahern 	struct fib6_info *rt;
31366b9ea5a6SRoopa Prabhu 	int err;
31376b9ea5a6SRoopa Prabhu 
3138acb54e3cSDavid Ahern 	rt = ip6_route_info_create(cfg, gfp_flags, extack);
3139d4ead6b3SDavid Ahern 	if (IS_ERR(rt))
3140d4ead6b3SDavid Ahern 		return PTR_ERR(rt);
31416b9ea5a6SRoopa Prabhu 
3142d4ead6b3SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
314393531c67SDavid Ahern 	fib6_info_release(rt);
31446b9ea5a6SRoopa Prabhu 
31451da177e4SLinus Torvalds 	return err;
31461da177e4SLinus Torvalds }
31471da177e4SLinus Torvalds 
31488d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
31491da177e4SLinus Torvalds {
3150afb1d4b5SDavid Ahern 	struct net *net = info->nl_net;
3151c71099acSThomas Graf 	struct fib6_table *table;
3152afb1d4b5SDavid Ahern 	int err;
31531da177e4SLinus Torvalds 
3154421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
31556825a26cSGao feng 		err = -ENOENT;
31566825a26cSGao feng 		goto out;
31576825a26cSGao feng 	}
31586c813a72SPatrick McHardy 
315993c2fb25SDavid Ahern 	table = rt->fib6_table;
316066f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
316186872cb5SThomas Graf 	err = fib6_del(rt, info);
316266f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
31631da177e4SLinus Torvalds 
31646825a26cSGao feng out:
316593531c67SDavid Ahern 	fib6_info_release(rt);
31661da177e4SLinus Torvalds 	return err;
31671da177e4SLinus Torvalds }
31681da177e4SLinus Torvalds 
31698d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt)
3170e0a1ad73SThomas Graf {
3171afb1d4b5SDavid Ahern 	struct nl_info info = { .nl_net = net };
3172afb1d4b5SDavid Ahern 
3173528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
3174e0a1ad73SThomas Graf }
3175e0a1ad73SThomas Graf 
31768d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
31770ae81335SDavid Ahern {
31780ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
3179e3330039SWANG Cong 	struct net *net = info->nl_net;
318016a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
31810ae81335SDavid Ahern 	struct fib6_table *table;
3182e3330039SWANG Cong 	int err = -ENOENT;
31830ae81335SDavid Ahern 
3184421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
3185e3330039SWANG Cong 		goto out_put;
318693c2fb25SDavid Ahern 	table = rt->fib6_table;
318766f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
31880ae81335SDavid Ahern 
318993c2fb25SDavid Ahern 	if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
31908d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
31910ae81335SDavid Ahern 
319216a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
319316a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
319416a16cd3SDavid Ahern 		if (skb) {
319516a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
319616a16cd3SDavid Ahern 
3197d4ead6b3SDavid Ahern 			if (rt6_fill_node(net, skb, rt, NULL,
319816a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
319916a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
320016a16cd3SDavid Ahern 				kfree_skb(skb);
320116a16cd3SDavid Ahern 				skb = NULL;
320216a16cd3SDavid Ahern 			} else
320316a16cd3SDavid Ahern 				info->skip_notify = 1;
320416a16cd3SDavid Ahern 		}
320516a16cd3SDavid Ahern 
32060ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
320793c2fb25SDavid Ahern 					 &rt->fib6_siblings,
320893c2fb25SDavid Ahern 					 fib6_siblings) {
32090ae81335SDavid Ahern 			err = fib6_del(sibling, info);
32100ae81335SDavid Ahern 			if (err)
3211e3330039SWANG Cong 				goto out_unlock;
32120ae81335SDavid Ahern 		}
32130ae81335SDavid Ahern 	}
32140ae81335SDavid Ahern 
32150ae81335SDavid Ahern 	err = fib6_del(rt, info);
3216e3330039SWANG Cong out_unlock:
321766f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
3218e3330039SWANG Cong out_put:
321993531c67SDavid Ahern 	fib6_info_release(rt);
322016a16cd3SDavid Ahern 
322116a16cd3SDavid Ahern 	if (skb) {
3222e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
322316a16cd3SDavid Ahern 			    info->nlh, gfp_any());
322416a16cd3SDavid Ahern 	}
32250ae81335SDavid Ahern 	return err;
32260ae81335SDavid Ahern }
32270ae81335SDavid Ahern 
322823fb93a4SDavid Ahern static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
322923fb93a4SDavid Ahern {
323023fb93a4SDavid Ahern 	int rc = -ESRCH;
323123fb93a4SDavid Ahern 
323223fb93a4SDavid Ahern 	if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
323323fb93a4SDavid Ahern 		goto out;
323423fb93a4SDavid Ahern 
323523fb93a4SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY &&
323623fb93a4SDavid Ahern 	    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
323723fb93a4SDavid Ahern 		goto out;
3238761f6026SXin Long 
323923fb93a4SDavid Ahern 	rc = rt6_remove_exception_rt(rt);
324023fb93a4SDavid Ahern out:
324123fb93a4SDavid Ahern 	return rc;
324223fb93a4SDavid Ahern }
324323fb93a4SDavid Ahern 
3244333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
3245333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
32461da177e4SLinus Torvalds {
32478d1c802bSDavid Ahern 	struct rt6_info *rt_cache;
3248c71099acSThomas Graf 	struct fib6_table *table;
32498d1c802bSDavid Ahern 	struct fib6_info *rt;
32501da177e4SLinus Torvalds 	struct fib6_node *fn;
32511da177e4SLinus Torvalds 	int err = -ESRCH;
32521da177e4SLinus Torvalds 
32535578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3254d5d531cbSDavid Ahern 	if (!table) {
3255d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
3256c71099acSThomas Graf 		return err;
3257d5d531cbSDavid Ahern 	}
32581da177e4SLinus Torvalds 
325966f5d6ceSWei Wang 	rcu_read_lock();
3260c71099acSThomas Graf 
3261c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
326286872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
326338fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
32642b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
32651da177e4SLinus Torvalds 
32661da177e4SLinus Torvalds 	if (fn) {
326766f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
32682b760fcfSWei Wang 			if (cfg->fc_flags & RTF_CACHE) {
326923fb93a4SDavid Ahern 				int rc;
327023fb93a4SDavid Ahern 
32712b760fcfSWei Wang 				rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
32722b760fcfSWei Wang 							      &cfg->fc_src);
327323fb93a4SDavid Ahern 				if (rt_cache) {
327423fb93a4SDavid Ahern 					rc = ip6_del_cached_rt(rt_cache, cfg);
32759e575010SEric Dumazet 					if (rc != -ESRCH) {
32769e575010SEric Dumazet 						rcu_read_unlock();
327723fb93a4SDavid Ahern 						return rc;
327823fb93a4SDavid Ahern 					}
32799e575010SEric Dumazet 				}
32801f56a01fSMartin KaFai Lau 				continue;
32812b760fcfSWei Wang 			}
328286872cb5SThomas Graf 			if (cfg->fc_ifindex &&
32835e670d84SDavid Ahern 			    (!rt->fib6_nh.nh_dev ||
32845e670d84SDavid Ahern 			     rt->fib6_nh.nh_dev->ifindex != cfg->fc_ifindex))
32851da177e4SLinus Torvalds 				continue;
328686872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
32875e670d84SDavid Ahern 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->fib6_nh.nh_gw))
32881da177e4SLinus Torvalds 				continue;
328993c2fb25SDavid Ahern 			if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
32901da177e4SLinus Torvalds 				continue;
329193c2fb25SDavid Ahern 			if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
3292c2ed1880SMantas M 				continue;
3293e873e4b9SWei Wang 			if (!fib6_info_hold_safe(rt))
3294e873e4b9SWei Wang 				continue;
329566f5d6ceSWei Wang 			rcu_read_unlock();
32961da177e4SLinus Torvalds 
32970ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
32980ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
329986872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
33000ae81335SDavid Ahern 
33010ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
33021da177e4SLinus Torvalds 		}
33031da177e4SLinus Torvalds 	}
330466f5d6ceSWei Wang 	rcu_read_unlock();
33051da177e4SLinus Torvalds 
33061da177e4SLinus Torvalds 	return err;
33071da177e4SLinus Torvalds }
33081da177e4SLinus Torvalds 
33096700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3310a6279458SYOSHIFUJI Hideaki {
3311a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
3312e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
3313e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
3314e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
3315e8599ff4SDavid S. Miller 	struct neighbour *neigh;
3316a68886a6SDavid Ahern 	struct fib6_info *from;
331771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
33186e157b6aSDavid S. Miller 	int optlen, on_link;
33196e157b6aSDavid S. Miller 	u8 *lladdr;
3320e8599ff4SDavid S. Miller 
332129a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
332271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
3323e8599ff4SDavid S. Miller 
3324e8599ff4SDavid S. Miller 	if (optlen < 0) {
33256e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3326e8599ff4SDavid S. Miller 		return;
3327e8599ff4SDavid S. Miller 	}
3328e8599ff4SDavid S. Miller 
332971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
3330e8599ff4SDavid S. Miller 
333171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
33326e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3333e8599ff4SDavid S. Miller 		return;
3334e8599ff4SDavid S. Miller 	}
3335e8599ff4SDavid S. Miller 
33366e157b6aSDavid S. Miller 	on_link = 0;
333771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
3338e8599ff4SDavid S. Miller 		on_link = 1;
333971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
3340e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
33416e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
3342e8599ff4SDavid S. Miller 		return;
3343e8599ff4SDavid S. Miller 	}
3344e8599ff4SDavid S. Miller 
3345e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
3346e8599ff4SDavid S. Miller 	if (!in6_dev)
3347e8599ff4SDavid S. Miller 		return;
3348e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3349e8599ff4SDavid S. Miller 		return;
3350e8599ff4SDavid S. Miller 
3351e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
3352e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
3353e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
3354e8599ff4SDavid S. Miller 	 */
3355e8599ff4SDavid S. Miller 
3356f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
3357e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3358e8599ff4SDavid S. Miller 		return;
3359e8599ff4SDavid S. Miller 	}
33606e157b6aSDavid S. Miller 
33616e157b6aSDavid S. Miller 	lladdr = NULL;
3362e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
3363e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3364e8599ff4SDavid S. Miller 					     skb->dev);
3365e8599ff4SDavid S. Miller 		if (!lladdr) {
3366e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3367e8599ff4SDavid S. Miller 			return;
3368e8599ff4SDavid S. Miller 		}
3369e8599ff4SDavid S. Miller 	}
3370e8599ff4SDavid S. Miller 
33716e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
3372ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
33736e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
33746e157b6aSDavid S. Miller 		return;
33756e157b6aSDavid S. Miller 	}
33766e157b6aSDavid S. Miller 
33776e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
33786e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
33796e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
33806e157b6aSDavid S. Miller 	 */
33810dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
33826e157b6aSDavid S. Miller 
338371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
3384e8599ff4SDavid S. Miller 	if (!neigh)
3385e8599ff4SDavid S. Miller 		return;
3386e8599ff4SDavid S. Miller 
33871da177e4SLinus Torvalds 	/*
33881da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
33891da177e4SLinus Torvalds 	 */
33901da177e4SLinus Torvalds 
3391f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
33921da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
33931da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
33941da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
3395f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
3396f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
33971da177e4SLinus Torvalds 
33984d85cd0cSDavid Ahern 	rcu_read_lock();
3399a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
3400e873e4b9SWei Wang 	/* This fib6_info_hold() is safe here because we hold reference to rt
3401e873e4b9SWei Wang 	 * and rt already holds reference to fib6_info.
3402e873e4b9SWei Wang 	 */
34038a14e46fSDavid Ahern 	fib6_info_hold(from);
34044d85cd0cSDavid Ahern 	rcu_read_unlock();
34058a14e46fSDavid Ahern 
34068a14e46fSDavid Ahern 	nrt = ip6_rt_cache_alloc(from, &msg->dest, NULL);
340738308473SDavid S. Miller 	if (!nrt)
34081da177e4SLinus Torvalds 		goto out;
34091da177e4SLinus Torvalds 
34101da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
34111da177e4SLinus Torvalds 	if (on_link)
34121da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
34131da177e4SLinus Torvalds 
34144e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
34151da177e4SLinus Torvalds 
34162b760fcfSWei Wang 	/* No need to remove rt from the exception table if rt is
34172b760fcfSWei Wang 	 * a cached route because rt6_insert_exception() will
34182b760fcfSWei Wang 	 * takes care of it
34192b760fcfSWei Wang 	 */
34208a14e46fSDavid Ahern 	if (rt6_insert_exception(nrt, from)) {
34212b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
34222b760fcfSWei Wang 		goto out;
34232b760fcfSWei Wang 	}
34241da177e4SLinus Torvalds 
3425d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
3426d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
342771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
342860592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
34298d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
34308d71740cSTom Tucker 
34311da177e4SLinus Torvalds out:
34328a14e46fSDavid Ahern 	fib6_info_release(from);
3433e8599ff4SDavid S. Miller 	neigh_release(neigh);
34346e157b6aSDavid S. Miller }
34356e157b6aSDavid S. Miller 
343670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
34378d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
3438b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3439830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3440830218c1SDavid Ahern 					   struct net_device *dev)
344170ceb4f5SYOSHIFUJI Hideaki {
3442830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3443830218c1SDavid Ahern 	int ifindex = dev->ifindex;
344470ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
34458d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3446c71099acSThomas Graf 	struct fib6_table *table;
344770ceb4f5SYOSHIFUJI Hideaki 
3448830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
344938308473SDavid S. Miller 	if (!table)
3450c71099acSThomas Graf 		return NULL;
3451c71099acSThomas Graf 
345266f5d6ceSWei Wang 	rcu_read_lock();
345338fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
345470ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
345570ceb4f5SYOSHIFUJI Hideaki 		goto out;
345670ceb4f5SYOSHIFUJI Hideaki 
345766f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
34585e670d84SDavid Ahern 		if (rt->fib6_nh.nh_dev->ifindex != ifindex)
345970ceb4f5SYOSHIFUJI Hideaki 			continue;
346093c2fb25SDavid Ahern 		if ((rt->fib6_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
346170ceb4f5SYOSHIFUJI Hideaki 			continue;
34625e670d84SDavid Ahern 		if (!ipv6_addr_equal(&rt->fib6_nh.nh_gw, gwaddr))
346370ceb4f5SYOSHIFUJI Hideaki 			continue;
3464e873e4b9SWei Wang 		if (!fib6_info_hold_safe(rt))
3465e873e4b9SWei Wang 			continue;
346670ceb4f5SYOSHIFUJI Hideaki 		break;
346770ceb4f5SYOSHIFUJI Hideaki 	}
346870ceb4f5SYOSHIFUJI Hideaki out:
346966f5d6ceSWei Wang 	rcu_read_unlock();
347070ceb4f5SYOSHIFUJI Hideaki 	return rt;
347170ceb4f5SYOSHIFUJI Hideaki }
347270ceb4f5SYOSHIFUJI Hideaki 
34738d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
3474b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3475830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3476830218c1SDavid Ahern 					   struct net_device *dev,
347795c96174SEric Dumazet 					   unsigned int pref)
347870ceb4f5SYOSHIFUJI Hideaki {
347986872cb5SThomas Graf 	struct fib6_config cfg = {
3480238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
3481830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
348286872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
348386872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
348486872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
3485b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3486e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
348715e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
3488efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3489efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
349086872cb5SThomas Graf 	};
349170ceb4f5SYOSHIFUJI Hideaki 
3492830218c1SDavid Ahern 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
34934e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
34944e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
349586872cb5SThomas Graf 
3496e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
3497e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
349886872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
349970ceb4f5SYOSHIFUJI Hideaki 
3500acb54e3cSDavid Ahern 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
350170ceb4f5SYOSHIFUJI Hideaki 
3502830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
350370ceb4f5SYOSHIFUJI Hideaki }
350470ceb4f5SYOSHIFUJI Hideaki #endif
350570ceb4f5SYOSHIFUJI Hideaki 
35068d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net,
3507afb1d4b5SDavid Ahern 				     const struct in6_addr *addr,
3508afb1d4b5SDavid Ahern 				     struct net_device *dev)
35091da177e4SLinus Torvalds {
3510830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
35118d1c802bSDavid Ahern 	struct fib6_info *rt;
3512c71099acSThomas Graf 	struct fib6_table *table;
35131da177e4SLinus Torvalds 
3514afb1d4b5SDavid Ahern 	table = fib6_get_table(net, tb_id);
351538308473SDavid S. Miller 	if (!table)
3516c71099acSThomas Graf 		return NULL;
35171da177e4SLinus Torvalds 
351866f5d6ceSWei Wang 	rcu_read_lock();
351966f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
35205e670d84SDavid Ahern 		if (dev == rt->fib6_nh.nh_dev &&
352193c2fb25SDavid Ahern 		    ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
35225e670d84SDavid Ahern 		    ipv6_addr_equal(&rt->fib6_nh.nh_gw, addr))
35231da177e4SLinus Torvalds 			break;
35241da177e4SLinus Torvalds 	}
3525e873e4b9SWei Wang 	if (rt && !fib6_info_hold_safe(rt))
3526e873e4b9SWei Wang 		rt = NULL;
352766f5d6ceSWei Wang 	rcu_read_unlock();
35281da177e4SLinus Torvalds 	return rt;
35291da177e4SLinus Torvalds }
35301da177e4SLinus Torvalds 
35318d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net,
3532afb1d4b5SDavid Ahern 				     const struct in6_addr *gwaddr,
3533ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
3534ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
35351da177e4SLinus Torvalds {
353686872cb5SThomas Graf 	struct fib6_config cfg = {
3537ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
3538238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
353986872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
354086872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
354186872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
3542b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3543e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
354415e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
35455578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3546afb1d4b5SDavid Ahern 		.fc_nlinfo.nl_net = net,
354786872cb5SThomas Graf 	};
35481da177e4SLinus Torvalds 
35494e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
35501da177e4SLinus Torvalds 
3551acb54e3cSDavid Ahern 	if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
3552830218c1SDavid Ahern 		struct fib6_table *table;
3553830218c1SDavid Ahern 
3554830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
3555830218c1SDavid Ahern 		if (table)
3556830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3557830218c1SDavid Ahern 	}
35581da177e4SLinus Torvalds 
3559afb1d4b5SDavid Ahern 	return rt6_get_dflt_router(net, gwaddr, dev);
35601da177e4SLinus Torvalds }
35611da177e4SLinus Torvalds 
3562afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net,
3563afb1d4b5SDavid Ahern 				     struct fib6_table *table)
35641da177e4SLinus Torvalds {
35658d1c802bSDavid Ahern 	struct fib6_info *rt;
35661da177e4SLinus Torvalds 
35671da177e4SLinus Torvalds restart:
356866f5d6ceSWei Wang 	rcu_read_lock();
356966f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
3570dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
3571dcd1f572SDavid Ahern 		struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3572dcd1f572SDavid Ahern 
357393c2fb25SDavid Ahern 		if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
3574e873e4b9SWei Wang 		    (!idev || idev->cnf.accept_ra != 2) &&
3575e873e4b9SWei Wang 		    fib6_info_hold_safe(rt)) {
357666f5d6ceSWei Wang 			rcu_read_unlock();
3577afb1d4b5SDavid Ahern 			ip6_del_rt(net, rt);
35781da177e4SLinus Torvalds 			goto restart;
35791da177e4SLinus Torvalds 		}
35801da177e4SLinus Torvalds 	}
358166f5d6ceSWei Wang 	rcu_read_unlock();
3582830218c1SDavid Ahern 
3583830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3584830218c1SDavid Ahern }
3585830218c1SDavid Ahern 
3586830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
3587830218c1SDavid Ahern {
3588830218c1SDavid Ahern 	struct fib6_table *table;
3589830218c1SDavid Ahern 	struct hlist_head *head;
3590830218c1SDavid Ahern 	unsigned int h;
3591830218c1SDavid Ahern 
3592830218c1SDavid Ahern 	rcu_read_lock();
3593830218c1SDavid Ahern 
3594830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3595830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
3596830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3597830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3598afb1d4b5SDavid Ahern 				__rt6_purge_dflt_routers(net, table);
3599830218c1SDavid Ahern 		}
3600830218c1SDavid Ahern 	}
3601830218c1SDavid Ahern 
3602830218c1SDavid Ahern 	rcu_read_unlock();
36031da177e4SLinus Torvalds }
36041da177e4SLinus Torvalds 
36055578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
36065578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
360786872cb5SThomas Graf 				 struct fib6_config *cfg)
360886872cb5SThomas Graf {
36098823a3acSMaciej Żenczykowski 	*cfg = (struct fib6_config){
36108823a3acSMaciej Żenczykowski 		.fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
36118823a3acSMaciej Żenczykowski 			 : RT6_TABLE_MAIN,
36128823a3acSMaciej Żenczykowski 		.fc_ifindex = rtmsg->rtmsg_ifindex,
36138823a3acSMaciej Żenczykowski 		.fc_metric = rtmsg->rtmsg_metric,
36148823a3acSMaciej Żenczykowski 		.fc_expires = rtmsg->rtmsg_info,
36158823a3acSMaciej Żenczykowski 		.fc_dst_len = rtmsg->rtmsg_dst_len,
36168823a3acSMaciej Żenczykowski 		.fc_src_len = rtmsg->rtmsg_src_len,
36178823a3acSMaciej Żenczykowski 		.fc_flags = rtmsg->rtmsg_flags,
36188823a3acSMaciej Żenczykowski 		.fc_type = rtmsg->rtmsg_type,
361986872cb5SThomas Graf 
36208823a3acSMaciej Żenczykowski 		.fc_nlinfo.nl_net = net,
362186872cb5SThomas Graf 
36228823a3acSMaciej Żenczykowski 		.fc_dst = rtmsg->rtmsg_dst,
36238823a3acSMaciej Żenczykowski 		.fc_src = rtmsg->rtmsg_src,
36248823a3acSMaciej Żenczykowski 		.fc_gateway = rtmsg->rtmsg_gateway,
36258823a3acSMaciej Żenczykowski 	};
362686872cb5SThomas Graf }
362786872cb5SThomas Graf 
36285578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
36291da177e4SLinus Torvalds {
363086872cb5SThomas Graf 	struct fib6_config cfg;
36311da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
36321da177e4SLinus Torvalds 	int err;
36331da177e4SLinus Torvalds 
36341da177e4SLinus Torvalds 	switch (cmd) {
36351da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
36361da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
3637af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
36381da177e4SLinus Torvalds 			return -EPERM;
36391da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
36401da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
36411da177e4SLinus Torvalds 		if (err)
36421da177e4SLinus Torvalds 			return -EFAULT;
36431da177e4SLinus Torvalds 
36445578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
364586872cb5SThomas Graf 
36461da177e4SLinus Torvalds 		rtnl_lock();
36471da177e4SLinus Torvalds 		switch (cmd) {
36481da177e4SLinus Torvalds 		case SIOCADDRT:
3649acb54e3cSDavid Ahern 			err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
36501da177e4SLinus Torvalds 			break;
36511da177e4SLinus Torvalds 		case SIOCDELRT:
3652333c4301SDavid Ahern 			err = ip6_route_del(&cfg, NULL);
36531da177e4SLinus Torvalds 			break;
36541da177e4SLinus Torvalds 		default:
36551da177e4SLinus Torvalds 			err = -EINVAL;
36561da177e4SLinus Torvalds 		}
36571da177e4SLinus Torvalds 		rtnl_unlock();
36581da177e4SLinus Torvalds 
36591da177e4SLinus Torvalds 		return err;
36603ff50b79SStephen Hemminger 	}
36611da177e4SLinus Torvalds 
36621da177e4SLinus Torvalds 	return -EINVAL;
36631da177e4SLinus Torvalds }
36641da177e4SLinus Torvalds 
36651da177e4SLinus Torvalds /*
36661da177e4SLinus Torvalds  *	Drop the packet on the floor
36671da177e4SLinus Torvalds  */
36681da177e4SLinus Torvalds 
3669d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
36701da177e4SLinus Torvalds {
3671612f09e8SYOSHIFUJI Hideaki 	int type;
3672adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
3673612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
3674612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
36750660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
367645bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
3677bdb7cc64SStephen Suryaputra 			IP6_INC_STATS(dev_net(dst->dev),
3678bdb7cc64SStephen Suryaputra 				      __in6_dev_get_safely(skb->dev),
36793bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
3680612f09e8SYOSHIFUJI Hideaki 			break;
3681612f09e8SYOSHIFUJI Hideaki 		}
3682612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
3683612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
36843bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
36853bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
3686612f09e8SYOSHIFUJI Hideaki 		break;
3687612f09e8SYOSHIFUJI Hideaki 	}
36883ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
36891da177e4SLinus Torvalds 	kfree_skb(skb);
36901da177e4SLinus Torvalds 	return 0;
36911da177e4SLinus Torvalds }
36921da177e4SLinus Torvalds 
36939ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
36949ce8ade0SThomas Graf {
3695612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
36969ce8ade0SThomas Graf }
36979ce8ade0SThomas Graf 
3698ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
36991da177e4SLinus Torvalds {
3700adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3701612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
37021da177e4SLinus Torvalds }
37031da177e4SLinus Torvalds 
37049ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
37059ce8ade0SThomas Graf {
3706612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
37079ce8ade0SThomas Graf }
37089ce8ade0SThomas Graf 
3709ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
37109ce8ade0SThomas Graf {
3711adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3712612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
37139ce8ade0SThomas Graf }
37149ce8ade0SThomas Graf 
37151da177e4SLinus Torvalds /*
37161da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
37171da177e4SLinus Torvalds  */
37181da177e4SLinus Torvalds 
3719360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net,
3720afb1d4b5SDavid Ahern 				     struct inet6_dev *idev,
37211da177e4SLinus Torvalds 				     const struct in6_addr *addr,
3722acb54e3cSDavid Ahern 				     bool anycast, gfp_t gfp_flags)
37231da177e4SLinus Torvalds {
3724ca254490SDavid Ahern 	u32 tb_id;
37254832c30dSDavid Ahern 	struct net_device *dev = idev->dev;
3726360a9887SDavid Ahern 	struct fib6_info *f6i;
37275f02ce24SDavid Ahern 
3728360a9887SDavid Ahern 	f6i = fib6_info_alloc(gfp_flags);
3729360a9887SDavid Ahern 	if (!f6i)
37301da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
37311da177e4SLinus Torvalds 
3732d7e774f3SDavid Ahern 	f6i->fib6_metrics = ip_fib_metrics_init(net, NULL, 0, NULL);
3733360a9887SDavid Ahern 	f6i->dst_nocount = true;
3734360a9887SDavid Ahern 	f6i->dst_host = true;
3735360a9887SDavid Ahern 	f6i->fib6_protocol = RTPROT_KERNEL;
3736360a9887SDavid Ahern 	f6i->fib6_flags = RTF_UP | RTF_NONEXTHOP;
3737e8478e80SDavid Ahern 	if (anycast) {
3738360a9887SDavid Ahern 		f6i->fib6_type = RTN_ANYCAST;
3739360a9887SDavid Ahern 		f6i->fib6_flags |= RTF_ANYCAST;
3740e8478e80SDavid Ahern 	} else {
3741360a9887SDavid Ahern 		f6i->fib6_type = RTN_LOCAL;
3742360a9887SDavid Ahern 		f6i->fib6_flags |= RTF_LOCAL;
3743e8478e80SDavid Ahern 	}
37441da177e4SLinus Torvalds 
3745360a9887SDavid Ahern 	f6i->fib6_nh.nh_gw = *addr;
374693531c67SDavid Ahern 	dev_hold(dev);
3747360a9887SDavid Ahern 	f6i->fib6_nh.nh_dev = dev;
3748360a9887SDavid Ahern 	f6i->fib6_dst.addr = *addr;
3749360a9887SDavid Ahern 	f6i->fib6_dst.plen = 128;
3750ca254490SDavid Ahern 	tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
3751360a9887SDavid Ahern 	f6i->fib6_table = fib6_get_table(net, tb_id);
37521da177e4SLinus Torvalds 
3753360a9887SDavid Ahern 	return f6i;
37541da177e4SLinus Torvalds }
37551da177e4SLinus Torvalds 
3756c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
3757c3968a85SDaniel Walter struct arg_dev_net_ip {
3758c3968a85SDaniel Walter 	struct net_device *dev;
3759c3968a85SDaniel Walter 	struct net *net;
3760c3968a85SDaniel Walter 	struct in6_addr *addr;
3761c3968a85SDaniel Walter };
3762c3968a85SDaniel Walter 
37638d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
3764c3968a85SDaniel Walter {
3765c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3766c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3767c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3768c3968a85SDaniel Walter 
37695e670d84SDavid Ahern 	if (((void *)rt->fib6_nh.nh_dev == dev || !dev) &&
3770421842edSDavid Ahern 	    rt != net->ipv6.fib6_null_entry &&
377193c2fb25SDavid Ahern 	    ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
377260006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
3773c3968a85SDaniel Walter 		/* remove prefsrc entry */
377493c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
377560006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
3776c3968a85SDaniel Walter 	}
3777c3968a85SDaniel Walter 	return 0;
3778c3968a85SDaniel Walter }
3779c3968a85SDaniel Walter 
3780c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3781c3968a85SDaniel Walter {
3782c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
3783c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
3784c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
3785c3968a85SDaniel Walter 		.net = net,
3786c3968a85SDaniel Walter 		.addr = &ifp->addr,
3787c3968a85SDaniel Walter 	};
37880c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
3789c3968a85SDaniel Walter }
3790c3968a85SDaniel Walter 
3791be7a010dSDuan Jiong #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
3792be7a010dSDuan Jiong 
3793be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
37948d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
3795be7a010dSDuan Jiong {
3796be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
3797be7a010dSDuan Jiong 
379893c2fb25SDavid Ahern 	if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
37995e670d84SDavid Ahern 	    ipv6_addr_equal(gateway, &rt->fib6_nh.nh_gw)) {
3800be7a010dSDuan Jiong 		return -1;
3801be7a010dSDuan Jiong 	}
3802b16cb459SWei Wang 
3803b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
3804b16cb459SWei Wang 	 * This is needed because cached route may have a different
3805b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
3806b16cb459SWei Wang 	 */
3807b16cb459SWei Wang 	rt6_exceptions_clean_tohost(rt, gateway);
3808b16cb459SWei Wang 
3809be7a010dSDuan Jiong 	return 0;
3810be7a010dSDuan Jiong }
3811be7a010dSDuan Jiong 
3812be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3813be7a010dSDuan Jiong {
3814be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
3815be7a010dSDuan Jiong }
3816be7a010dSDuan Jiong 
38172127d95aSIdo Schimmel struct arg_netdev_event {
38182127d95aSIdo Schimmel 	const struct net_device *dev;
38194c981e28SIdo Schimmel 	union {
38202127d95aSIdo Schimmel 		unsigned int nh_flags;
38214c981e28SIdo Schimmel 		unsigned long event;
38224c981e28SIdo Schimmel 	};
38232127d95aSIdo Schimmel };
38242127d95aSIdo Schimmel 
38258d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
3826d7dedee1SIdo Schimmel {
38278d1c802bSDavid Ahern 	struct fib6_info *iter;
3828d7dedee1SIdo Schimmel 	struct fib6_node *fn;
3829d7dedee1SIdo Schimmel 
383093c2fb25SDavid Ahern 	fn = rcu_dereference_protected(rt->fib6_node,
383193c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3832d7dedee1SIdo Schimmel 	iter = rcu_dereference_protected(fn->leaf,
383393c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3834d7dedee1SIdo Schimmel 	while (iter) {
383593c2fb25SDavid Ahern 		if (iter->fib6_metric == rt->fib6_metric &&
383633bd5ac5SDavid Ahern 		    rt6_qualify_for_ecmp(iter))
3837d7dedee1SIdo Schimmel 			return iter;
38388fb11a9aSDavid Ahern 		iter = rcu_dereference_protected(iter->fib6_next,
383993c2fb25SDavid Ahern 				lockdep_is_held(&rt->fib6_table->tb6_lock));
3840d7dedee1SIdo Schimmel 	}
3841d7dedee1SIdo Schimmel 
3842d7dedee1SIdo Schimmel 	return NULL;
3843d7dedee1SIdo Schimmel }
3844d7dedee1SIdo Schimmel 
38458d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt)
3846d7dedee1SIdo Schimmel {
38475e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_DEAD ||
38485e670d84SDavid Ahern 	    (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN &&
3849dcd1f572SDavid Ahern 	     fib6_ignore_linkdown(rt)))
3850d7dedee1SIdo Schimmel 		return true;
3851d7dedee1SIdo Schimmel 
3852d7dedee1SIdo Schimmel 	return false;
3853d7dedee1SIdo Schimmel }
3854d7dedee1SIdo Schimmel 
38558d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt)
3856d7dedee1SIdo Schimmel {
38578d1c802bSDavid Ahern 	struct fib6_info *iter;
3858d7dedee1SIdo Schimmel 	int total = 0;
3859d7dedee1SIdo Schimmel 
3860d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt))
38615e670d84SDavid Ahern 		total += rt->fib6_nh.nh_weight;
3862d7dedee1SIdo Schimmel 
386393c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
3864d7dedee1SIdo Schimmel 		if (!rt6_is_dead(iter))
38655e670d84SDavid Ahern 			total += iter->fib6_nh.nh_weight;
3866d7dedee1SIdo Schimmel 	}
3867d7dedee1SIdo Schimmel 
3868d7dedee1SIdo Schimmel 	return total;
3869d7dedee1SIdo Schimmel }
3870d7dedee1SIdo Schimmel 
38718d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
3872d7dedee1SIdo Schimmel {
3873d7dedee1SIdo Schimmel 	int upper_bound = -1;
3874d7dedee1SIdo Schimmel 
3875d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt)) {
38765e670d84SDavid Ahern 		*weight += rt->fib6_nh.nh_weight;
3877d7dedee1SIdo Schimmel 		upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
3878d7dedee1SIdo Schimmel 						    total) - 1;
3879d7dedee1SIdo Schimmel 	}
38805e670d84SDavid Ahern 	atomic_set(&rt->fib6_nh.nh_upper_bound, upper_bound);
3881d7dedee1SIdo Schimmel }
3882d7dedee1SIdo Schimmel 
38838d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
3884d7dedee1SIdo Schimmel {
38858d1c802bSDavid Ahern 	struct fib6_info *iter;
3886d7dedee1SIdo Schimmel 	int weight = 0;
3887d7dedee1SIdo Schimmel 
3888d7dedee1SIdo Schimmel 	rt6_upper_bound_set(rt, &weight, total);
3889d7dedee1SIdo Schimmel 
389093c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
3891d7dedee1SIdo Schimmel 		rt6_upper_bound_set(iter, &weight, total);
3892d7dedee1SIdo Schimmel }
3893d7dedee1SIdo Schimmel 
38948d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt)
3895d7dedee1SIdo Schimmel {
38968d1c802bSDavid Ahern 	struct fib6_info *first;
3897d7dedee1SIdo Schimmel 	int total;
3898d7dedee1SIdo Schimmel 
3899d7dedee1SIdo Schimmel 	/* In case the entire multipath route was marked for flushing,
3900d7dedee1SIdo Schimmel 	 * then there is no need to rebalance upon the removal of every
3901d7dedee1SIdo Schimmel 	 * sibling route.
3902d7dedee1SIdo Schimmel 	 */
390393c2fb25SDavid Ahern 	if (!rt->fib6_nsiblings || rt->should_flush)
3904d7dedee1SIdo Schimmel 		return;
3905d7dedee1SIdo Schimmel 
3906d7dedee1SIdo Schimmel 	/* During lookup routes are evaluated in order, so we need to
3907d7dedee1SIdo Schimmel 	 * make sure upper bounds are assigned from the first sibling
3908d7dedee1SIdo Schimmel 	 * onwards.
3909d7dedee1SIdo Schimmel 	 */
3910d7dedee1SIdo Schimmel 	first = rt6_multipath_first_sibling(rt);
3911d7dedee1SIdo Schimmel 	if (WARN_ON_ONCE(!first))
3912d7dedee1SIdo Schimmel 		return;
3913d7dedee1SIdo Schimmel 
3914d7dedee1SIdo Schimmel 	total = rt6_multipath_total_weight(first);
3915d7dedee1SIdo Schimmel 	rt6_multipath_upper_bound_set(first, total);
3916d7dedee1SIdo Schimmel }
3917d7dedee1SIdo Schimmel 
39188d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg)
39192127d95aSIdo Schimmel {
39202127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
39217aef6859SDavid Ahern 	struct net *net = dev_net(arg->dev);
39222127d95aSIdo Schimmel 
3923421842edSDavid Ahern 	if (rt != net->ipv6.fib6_null_entry && rt->fib6_nh.nh_dev == arg->dev) {
39245e670d84SDavid Ahern 		rt->fib6_nh.nh_flags &= ~arg->nh_flags;
39257aef6859SDavid Ahern 		fib6_update_sernum_upto_root(net, rt);
3926d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
39271de178edSIdo Schimmel 	}
39282127d95aSIdo Schimmel 
39292127d95aSIdo Schimmel 	return 0;
39302127d95aSIdo Schimmel }
39312127d95aSIdo Schimmel 
39322127d95aSIdo Schimmel void rt6_sync_up(struct net_device *dev, unsigned int nh_flags)
39332127d95aSIdo Schimmel {
39342127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
39352127d95aSIdo Schimmel 		.dev = dev,
39366802f3adSIdo Schimmel 		{
39372127d95aSIdo Schimmel 			.nh_flags = nh_flags,
39386802f3adSIdo Schimmel 		},
39392127d95aSIdo Schimmel 	};
39402127d95aSIdo Schimmel 
39412127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
39422127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
39432127d95aSIdo Schimmel 
39442127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
39452127d95aSIdo Schimmel }
39462127d95aSIdo Schimmel 
39478d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
39481de178edSIdo Schimmel 				   const struct net_device *dev)
39491de178edSIdo Schimmel {
39508d1c802bSDavid Ahern 	struct fib6_info *iter;
39511de178edSIdo Schimmel 
39525e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == dev)
39531de178edSIdo Schimmel 		return true;
395493c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
39555e670d84SDavid Ahern 		if (iter->fib6_nh.nh_dev == dev)
39561de178edSIdo Schimmel 			return true;
39571de178edSIdo Schimmel 
39581de178edSIdo Schimmel 	return false;
39591de178edSIdo Schimmel }
39601de178edSIdo Schimmel 
39618d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt)
39621de178edSIdo Schimmel {
39638d1c802bSDavid Ahern 	struct fib6_info *iter;
39641de178edSIdo Schimmel 
39651de178edSIdo Schimmel 	rt->should_flush = 1;
396693c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
39671de178edSIdo Schimmel 		iter->should_flush = 1;
39681de178edSIdo Schimmel }
39691de178edSIdo Schimmel 
39708d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
39711de178edSIdo Schimmel 					     const struct net_device *down_dev)
39721de178edSIdo Schimmel {
39738d1c802bSDavid Ahern 	struct fib6_info *iter;
39741de178edSIdo Schimmel 	unsigned int dead = 0;
39751de178edSIdo Schimmel 
39765e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == down_dev ||
39775e670d84SDavid Ahern 	    rt->fib6_nh.nh_flags & RTNH_F_DEAD)
39781de178edSIdo Schimmel 		dead++;
397993c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
39805e670d84SDavid Ahern 		if (iter->fib6_nh.nh_dev == down_dev ||
39815e670d84SDavid Ahern 		    iter->fib6_nh.nh_flags & RTNH_F_DEAD)
39821de178edSIdo Schimmel 			dead++;
39831de178edSIdo Schimmel 
39841de178edSIdo Schimmel 	return dead;
39851de178edSIdo Schimmel }
39861de178edSIdo Schimmel 
39878d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
39881de178edSIdo Schimmel 				       const struct net_device *dev,
39891de178edSIdo Schimmel 				       unsigned int nh_flags)
39901de178edSIdo Schimmel {
39918d1c802bSDavid Ahern 	struct fib6_info *iter;
39921de178edSIdo Schimmel 
39935e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == dev)
39945e670d84SDavid Ahern 		rt->fib6_nh.nh_flags |= nh_flags;
399593c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
39965e670d84SDavid Ahern 		if (iter->fib6_nh.nh_dev == dev)
39975e670d84SDavid Ahern 			iter->fib6_nh.nh_flags |= nh_flags;
39981de178edSIdo Schimmel }
39991de178edSIdo Schimmel 
4000a1a22c12SDavid Ahern /* called with write lock held for table with rt */
40018d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
40021da177e4SLinus Torvalds {
40034c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
40044c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
40057aef6859SDavid Ahern 	struct net *net = dev_net(dev);
40068ed67789SDaniel Lezcano 
4007421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
400827c6fa73SIdo Schimmel 		return 0;
400927c6fa73SIdo Schimmel 
401027c6fa73SIdo Schimmel 	switch (arg->event) {
401127c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
40125e670d84SDavid Ahern 		return rt->fib6_nh.nh_dev == dev ? -1 : 0;
401327c6fa73SIdo Schimmel 	case NETDEV_DOWN:
40141de178edSIdo Schimmel 		if (rt->should_flush)
401527c6fa73SIdo Schimmel 			return -1;
401693c2fb25SDavid Ahern 		if (!rt->fib6_nsiblings)
40175e670d84SDavid Ahern 			return rt->fib6_nh.nh_dev == dev ? -1 : 0;
40181de178edSIdo Schimmel 		if (rt6_multipath_uses_dev(rt, dev)) {
40191de178edSIdo Schimmel 			unsigned int count;
40201de178edSIdo Schimmel 
40211de178edSIdo Schimmel 			count = rt6_multipath_dead_count(rt, dev);
402293c2fb25SDavid Ahern 			if (rt->fib6_nsiblings + 1 == count) {
40231de178edSIdo Schimmel 				rt6_multipath_flush(rt);
40241de178edSIdo Schimmel 				return -1;
40251de178edSIdo Schimmel 			}
40261de178edSIdo Schimmel 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
40271de178edSIdo Schimmel 						   RTNH_F_LINKDOWN);
40287aef6859SDavid Ahern 			fib6_update_sernum(net, rt);
4029d7dedee1SIdo Schimmel 			rt6_multipath_rebalance(rt);
40301de178edSIdo Schimmel 		}
40311de178edSIdo Schimmel 		return -2;
403227c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
40335e670d84SDavid Ahern 		if (rt->fib6_nh.nh_dev != dev ||
403493c2fb25SDavid Ahern 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
403527c6fa73SIdo Schimmel 			break;
40365e670d84SDavid Ahern 		rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
4037d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
403827c6fa73SIdo Schimmel 		break;
40392b241361SIdo Schimmel 	}
4040c159d30cSDavid S. Miller 
40411da177e4SLinus Torvalds 	return 0;
40421da177e4SLinus Torvalds }
40431da177e4SLinus Torvalds 
404427c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
40451da177e4SLinus Torvalds {
40464c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
40478ed67789SDaniel Lezcano 		.dev = dev,
40486802f3adSIdo Schimmel 		{
40494c981e28SIdo Schimmel 			.event = event,
40506802f3adSIdo Schimmel 		},
40518ed67789SDaniel Lezcano 	};
40527c6bb7d2SDavid Ahern 	struct net *net = dev_net(dev);
40538ed67789SDaniel Lezcano 
40547c6bb7d2SDavid Ahern 	if (net->ipv6.sysctl.skip_notify_on_dev_down)
40557c6bb7d2SDavid Ahern 		fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
40567c6bb7d2SDavid Ahern 	else
40577c6bb7d2SDavid Ahern 		fib6_clean_all(net, fib6_ifdown, &arg);
40584c981e28SIdo Schimmel }
40594c981e28SIdo Schimmel 
40604c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
40614c981e28SIdo Schimmel {
40624c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
40634c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
40644c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
40651da177e4SLinus Torvalds }
40661da177e4SLinus Torvalds 
406795c96174SEric Dumazet struct rt6_mtu_change_arg {
40681da177e4SLinus Torvalds 	struct net_device *dev;
406995c96174SEric Dumazet 	unsigned int mtu;
40701da177e4SLinus Torvalds };
40711da177e4SLinus Torvalds 
40728d1c802bSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg)
40731da177e4SLinus Torvalds {
40741da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
40751da177e4SLinus Torvalds 	struct inet6_dev *idev;
40761da177e4SLinus Torvalds 
40771da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
40781da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
40791da177e4SLinus Torvalds 	   We still use this lock to block changes
40801da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
40811da177e4SLinus Torvalds 	*/
40821da177e4SLinus Torvalds 
40831da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
408438308473SDavid S. Miller 	if (!idev)
40851da177e4SLinus Torvalds 		return 0;
40861da177e4SLinus Torvalds 
40871da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
40881da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
40891da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
40901da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
40911da177e4SLinus Torvalds 	 */
40925e670d84SDavid Ahern 	if (rt->fib6_nh.nh_dev == arg->dev &&
4093d4ead6b3SDavid Ahern 	    !fib6_metric_locked(rt, RTAX_MTU)) {
4094d4ead6b3SDavid Ahern 		u32 mtu = rt->fib6_pmtu;
4095d4ead6b3SDavid Ahern 
4096d4ead6b3SDavid Ahern 		if (mtu >= arg->mtu ||
4097d4ead6b3SDavid Ahern 		    (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4098d4ead6b3SDavid Ahern 			fib6_metric_set(rt, RTAX_MTU, arg->mtu);
4099d4ead6b3SDavid Ahern 
4100f5bbe7eeSWei Wang 		spin_lock_bh(&rt6_exception_lock);
4101e9fa1495SStefano Brivio 		rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
4102f5bbe7eeSWei Wang 		spin_unlock_bh(&rt6_exception_lock);
41034b32b5adSMartin KaFai Lau 	}
41041da177e4SLinus Torvalds 	return 0;
41051da177e4SLinus Torvalds }
41061da177e4SLinus Torvalds 
410795c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
41081da177e4SLinus Torvalds {
4109c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
4110c71099acSThomas Graf 		.dev = dev,
4111c71099acSThomas Graf 		.mtu = mtu,
4112c71099acSThomas Graf 	};
41131da177e4SLinus Torvalds 
41140c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
41151da177e4SLinus Torvalds }
41161da177e4SLinus Torvalds 
4117ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
41185176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
4119aa8f8778SEric Dumazet 	[RTA_PREFSRC]		= { .len = sizeof(struct in6_addr) },
412086872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
4121ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
412286872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
412386872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
412451ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
4125c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
412619e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
412719e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
412832bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
4129622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
41303b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
4131aa8f8778SEric Dumazet 	[RTA_TABLE]		= { .type = NLA_U32 },
4132eacb9384SRoopa Prabhu 	[RTA_IP_PROTO]		= { .type = NLA_U8 },
4133eacb9384SRoopa Prabhu 	[RTA_SPORT]		= { .type = NLA_U16 },
4134eacb9384SRoopa Prabhu 	[RTA_DPORT]		= { .type = NLA_U16 },
413586872cb5SThomas Graf };
413686872cb5SThomas Graf 
413786872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4138333c4301SDavid Ahern 			      struct fib6_config *cfg,
4139333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
41401da177e4SLinus Torvalds {
414186872cb5SThomas Graf 	struct rtmsg *rtm;
414286872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
4143c78ba6d6SLubomir Rintel 	unsigned int pref;
414486872cb5SThomas Graf 	int err;
41451da177e4SLinus Torvalds 
4146fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4147dac9c979SDavid Ahern 			  extack);
414886872cb5SThomas Graf 	if (err < 0)
414986872cb5SThomas Graf 		goto errout;
41501da177e4SLinus Torvalds 
415186872cb5SThomas Graf 	err = -EINVAL;
415286872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
415386872cb5SThomas Graf 
415484db8407SMaciej Żenczykowski 	*cfg = (struct fib6_config){
415584db8407SMaciej Żenczykowski 		.fc_table = rtm->rtm_table,
415684db8407SMaciej Żenczykowski 		.fc_dst_len = rtm->rtm_dst_len,
415784db8407SMaciej Żenczykowski 		.fc_src_len = rtm->rtm_src_len,
415884db8407SMaciej Żenczykowski 		.fc_flags = RTF_UP,
415984db8407SMaciej Żenczykowski 		.fc_protocol = rtm->rtm_protocol,
416084db8407SMaciej Żenczykowski 		.fc_type = rtm->rtm_type,
416184db8407SMaciej Żenczykowski 
416284db8407SMaciej Żenczykowski 		.fc_nlinfo.portid = NETLINK_CB(skb).portid,
416384db8407SMaciej Żenczykowski 		.fc_nlinfo.nlh = nlh,
416484db8407SMaciej Żenczykowski 		.fc_nlinfo.nl_net = sock_net(skb->sk),
416584db8407SMaciej Żenczykowski 	};
416686872cb5SThomas Graf 
4167ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
4168ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
4169b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
4170b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
417186872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
417286872cb5SThomas Graf 
4173ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
4174ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
4175ab79ad14SMaciej Żenczykowski 
41761f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
41771f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
41781f56a01fSMartin KaFai Lau 
4179fc1e64e1SDavid Ahern 	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4180fc1e64e1SDavid Ahern 
418186872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
418267b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
418386872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
41841da177e4SLinus Torvalds 	}
4185e3818541SDavid Ahern 	if (tb[RTA_VIA]) {
4186e3818541SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4187e3818541SDavid Ahern 		goto errout;
4188e3818541SDavid Ahern 	}
418986872cb5SThomas Graf 
419086872cb5SThomas Graf 	if (tb[RTA_DST]) {
419186872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
419286872cb5SThomas Graf 
419386872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
419486872cb5SThomas Graf 			goto errout;
419586872cb5SThomas Graf 
419686872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
41971da177e4SLinus Torvalds 	}
419886872cb5SThomas Graf 
419986872cb5SThomas Graf 	if (tb[RTA_SRC]) {
420086872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
420186872cb5SThomas Graf 
420286872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
420386872cb5SThomas Graf 			goto errout;
420486872cb5SThomas Graf 
420586872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
42061da177e4SLinus Torvalds 	}
420786872cb5SThomas Graf 
4208c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
420967b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4210c3968a85SDaniel Walter 
421186872cb5SThomas Graf 	if (tb[RTA_OIF])
421286872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
421386872cb5SThomas Graf 
421486872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
421586872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
421686872cb5SThomas Graf 
421786872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
421886872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
421986872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
42201da177e4SLinus Torvalds 	}
422186872cb5SThomas Graf 
422286872cb5SThomas Graf 	if (tb[RTA_TABLE])
422386872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
422486872cb5SThomas Graf 
422551ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
422651ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
422751ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
42289ed59592SDavid Ahern 
42299ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4230c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
42319ed59592SDavid Ahern 		if (err < 0)
42329ed59592SDavid Ahern 			goto errout;
423351ebd318SNicolas Dichtel 	}
423451ebd318SNicolas Dichtel 
4235c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
4236c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
4237c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
4238c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
4239c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
4240c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
4241c78ba6d6SLubomir Rintel 	}
4242c78ba6d6SLubomir Rintel 
424319e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
424419e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
424519e42e45SRoopa Prabhu 
42469ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
424719e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
424819e42e45SRoopa Prabhu 
4249c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
42509ed59592SDavid Ahern 		if (err < 0)
42519ed59592SDavid Ahern 			goto errout;
42529ed59592SDavid Ahern 	}
42539ed59592SDavid Ahern 
425432bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
425532bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
425632bc201eSXin Long 
425732bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
425832bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
425932bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
426032bc201eSXin Long 		}
426132bc201eSXin Long 	}
426232bc201eSXin Long 
426386872cb5SThomas Graf 	err = 0;
426486872cb5SThomas Graf errout:
426586872cb5SThomas Graf 	return err;
42661da177e4SLinus Torvalds }
42671da177e4SLinus Torvalds 
42686b9ea5a6SRoopa Prabhu struct rt6_nh {
42698d1c802bSDavid Ahern 	struct fib6_info *fib6_info;
42706b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
42716b9ea5a6SRoopa Prabhu 	struct list_head next;
42726b9ea5a6SRoopa Prabhu };
42736b9ea5a6SRoopa Prabhu 
4274d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net,
4275d4ead6b3SDavid Ahern 				 struct list_head *rt6_nh_list,
42768d1c802bSDavid Ahern 				 struct fib6_info *rt,
42778d1c802bSDavid Ahern 				 struct fib6_config *r_cfg)
42786b9ea5a6SRoopa Prabhu {
42796b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
42806b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
42816b9ea5a6SRoopa Prabhu 
42826b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
42838d1c802bSDavid Ahern 		/* check if fib6_info already exists */
42848d1c802bSDavid Ahern 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
42856b9ea5a6SRoopa Prabhu 			return err;
42866b9ea5a6SRoopa Prabhu 	}
42876b9ea5a6SRoopa Prabhu 
42886b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
42896b9ea5a6SRoopa Prabhu 	if (!nh)
42906b9ea5a6SRoopa Prabhu 		return -ENOMEM;
42918d1c802bSDavid Ahern 	nh->fib6_info = rt;
42926b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
42936b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
42946b9ea5a6SRoopa Prabhu 
42956b9ea5a6SRoopa Prabhu 	return 0;
42966b9ea5a6SRoopa Prabhu }
42976b9ea5a6SRoopa Prabhu 
42988d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt,
42998d1c802bSDavid Ahern 				   struct fib6_info *rt_last,
43003b1137feSDavid Ahern 				   struct nl_info *info,
43013b1137feSDavid Ahern 				   __u16 nlflags)
43023b1137feSDavid Ahern {
43033b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
43043b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
43053b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
43063b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
43073b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
43083b1137feSDavid Ahern 	 */
430993c2fb25SDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
431093c2fb25SDavid Ahern 		rt = list_first_entry(&rt_last->fib6_siblings,
43118d1c802bSDavid Ahern 				      struct fib6_info,
431293c2fb25SDavid Ahern 				      fib6_siblings);
43133b1137feSDavid Ahern 	}
43143b1137feSDavid Ahern 
43153b1137feSDavid Ahern 	if (rt)
43163b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
43173b1137feSDavid Ahern }
43183b1137feSDavid Ahern 
4319333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
4320333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
432151ebd318SNicolas Dichtel {
43228d1c802bSDavid Ahern 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
43233b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
432451ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
432551ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
43268d1c802bSDavid Ahern 	struct fib6_info *rt;
43276b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
43286b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
43293b1137feSDavid Ahern 	__u16 nlflags;
433051ebd318SNicolas Dichtel 	int remaining;
433151ebd318SNicolas Dichtel 	int attrlen;
43326b9ea5a6SRoopa Prabhu 	int err = 1;
43336b9ea5a6SRoopa Prabhu 	int nhn = 0;
43346b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
43356b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
43366b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
433751ebd318SNicolas Dichtel 
43383b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
43393b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
43403b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
43413b1137feSDavid Ahern 
434235f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
434351ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
434451ebd318SNicolas Dichtel 
43456b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
43468d1c802bSDavid Ahern 	 * fib6_info structs per nexthop
43476b9ea5a6SRoopa Prabhu 	 */
434851ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
434951ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
435051ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
435151ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
435251ebd318SNicolas Dichtel 
435351ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
435451ebd318SNicolas Dichtel 		if (attrlen > 0) {
435551ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
435651ebd318SNicolas Dichtel 
435751ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
435851ebd318SNicolas Dichtel 			if (nla) {
435967b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
436051ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
436151ebd318SNicolas Dichtel 			}
436219e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
436319e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
436419e42e45SRoopa Prabhu 			if (nla)
436519e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
436651ebd318SNicolas Dichtel 		}
43676b9ea5a6SRoopa Prabhu 
436868e2ffdeSDavid Ahern 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
4369acb54e3cSDavid Ahern 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
43708c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
43718c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
43728c5b83f0SRoopa Prabhu 			rt = NULL;
43736b9ea5a6SRoopa Prabhu 			goto cleanup;
43748c5b83f0SRoopa Prabhu 		}
4375b5d2d75eSDavid Ahern 		if (!rt6_qualify_for_ecmp(rt)) {
4376b5d2d75eSDavid Ahern 			err = -EINVAL;
4377b5d2d75eSDavid Ahern 			NL_SET_ERR_MSG(extack,
4378b5d2d75eSDavid Ahern 				       "Device only routes can not be added for IPv6 using the multipath API.");
4379b5d2d75eSDavid Ahern 			fib6_info_release(rt);
4380b5d2d75eSDavid Ahern 			goto cleanup;
4381b5d2d75eSDavid Ahern 		}
43826b9ea5a6SRoopa Prabhu 
43835e670d84SDavid Ahern 		rt->fib6_nh.nh_weight = rtnh->rtnh_hops + 1;
4384398958aeSIdo Schimmel 
4385d4ead6b3SDavid Ahern 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4386d4ead6b3SDavid Ahern 					    rt, &r_cfg);
438751ebd318SNicolas Dichtel 		if (err) {
438893531c67SDavid Ahern 			fib6_info_release(rt);
43896b9ea5a6SRoopa Prabhu 			goto cleanup;
439051ebd318SNicolas Dichtel 		}
43916b9ea5a6SRoopa Prabhu 
43926b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
439351ebd318SNicolas Dichtel 	}
43946b9ea5a6SRoopa Prabhu 
43953b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
43963b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
43973b1137feSDavid Ahern 	 * the full route when done
43983b1137feSDavid Ahern 	 */
43993b1137feSDavid Ahern 	info->skip_notify = 1;
44003b1137feSDavid Ahern 
44016b9ea5a6SRoopa Prabhu 	err_nh = NULL;
44026b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
44038d1c802bSDavid Ahern 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
44048d1c802bSDavid Ahern 		fib6_info_release(nh->fib6_info);
44053b1137feSDavid Ahern 
4406f7225172SDavid Ahern 		if (!err) {
4407f7225172SDavid Ahern 			/* save reference to last route successfully inserted */
4408f7225172SDavid Ahern 			rt_last = nh->fib6_info;
4409f7225172SDavid Ahern 
44106b9ea5a6SRoopa Prabhu 			/* save reference to first route for notification */
4411f7225172SDavid Ahern 			if (!rt_notif)
44128d1c802bSDavid Ahern 				rt_notif = nh->fib6_info;
4413f7225172SDavid Ahern 		}
44146b9ea5a6SRoopa Prabhu 
44158d1c802bSDavid Ahern 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
44168d1c802bSDavid Ahern 		nh->fib6_info = NULL;
44176b9ea5a6SRoopa Prabhu 		if (err) {
44186b9ea5a6SRoopa Prabhu 			if (replace && nhn)
4419a5a82d84SJakub Kicinski 				NL_SET_ERR_MSG_MOD(extack,
4420a5a82d84SJakub Kicinski 						   "multipath route replace failed (check consistency of installed routes)");
44216b9ea5a6SRoopa Prabhu 			err_nh = nh;
44226b9ea5a6SRoopa Prabhu 			goto add_errout;
44236b9ea5a6SRoopa Prabhu 		}
44246b9ea5a6SRoopa Prabhu 
44251a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
442627596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
442727596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
442827596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
442927596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
443027596472SMichal Kubeček 		 * be added to it.
44311a72418bSNicolas Dichtel 		 */
443227596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
443327596472SMichal Kubeček 						     NLM_F_REPLACE);
44346b9ea5a6SRoopa Prabhu 		nhn++;
44356b9ea5a6SRoopa Prabhu 	}
44366b9ea5a6SRoopa Prabhu 
44373b1137feSDavid Ahern 	/* success ... tell user about new route */
44383b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
44396b9ea5a6SRoopa Prabhu 	goto cleanup;
44406b9ea5a6SRoopa Prabhu 
44416b9ea5a6SRoopa Prabhu add_errout:
44423b1137feSDavid Ahern 	/* send notification for routes that were added so that
44433b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
44443b1137feSDavid Ahern 	 * coherent
44453b1137feSDavid Ahern 	 */
44463b1137feSDavid Ahern 	if (rt_notif)
44473b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
44483b1137feSDavid Ahern 
44496b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
44506b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
44516b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
44526b9ea5a6SRoopa Prabhu 			break;
4453333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
44546b9ea5a6SRoopa Prabhu 	}
44556b9ea5a6SRoopa Prabhu 
44566b9ea5a6SRoopa Prabhu cleanup:
44576b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
44588d1c802bSDavid Ahern 		if (nh->fib6_info)
44598d1c802bSDavid Ahern 			fib6_info_release(nh->fib6_info);
44606b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
44616b9ea5a6SRoopa Prabhu 		kfree(nh);
44626b9ea5a6SRoopa Prabhu 	}
44636b9ea5a6SRoopa Prabhu 
44646b9ea5a6SRoopa Prabhu 	return err;
44656b9ea5a6SRoopa Prabhu }
44666b9ea5a6SRoopa Prabhu 
4467333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
4468333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
44696b9ea5a6SRoopa Prabhu {
44706b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
44716b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
44726b9ea5a6SRoopa Prabhu 	int remaining;
44736b9ea5a6SRoopa Prabhu 	int attrlen;
44746b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
44756b9ea5a6SRoopa Prabhu 
44766b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
44776b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
44786b9ea5a6SRoopa Prabhu 
44796b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
44806b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
44816b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
44826b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
44836b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
44846b9ea5a6SRoopa Prabhu 
44856b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
44866b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
44876b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
44886b9ea5a6SRoopa Prabhu 
44896b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
44906b9ea5a6SRoopa Prabhu 			if (nla) {
44916b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
44926b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
44936b9ea5a6SRoopa Prabhu 			}
44946b9ea5a6SRoopa Prabhu 		}
4495333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
44966b9ea5a6SRoopa Prabhu 		if (err)
44976b9ea5a6SRoopa Prabhu 			last_err = err;
44986b9ea5a6SRoopa Prabhu 
449951ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
450051ebd318SNicolas Dichtel 	}
450151ebd318SNicolas Dichtel 
450251ebd318SNicolas Dichtel 	return last_err;
450351ebd318SNicolas Dichtel }
450451ebd318SNicolas Dichtel 
4505c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4506c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
45071da177e4SLinus Torvalds {
450886872cb5SThomas Graf 	struct fib6_config cfg;
450986872cb5SThomas Graf 	int err;
45101da177e4SLinus Torvalds 
4511333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
451286872cb5SThomas Graf 	if (err < 0)
451386872cb5SThomas Graf 		return err;
451486872cb5SThomas Graf 
451551ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4516333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
45170ae81335SDavid Ahern 	else {
45180ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
4519333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
45201da177e4SLinus Torvalds 	}
45210ae81335SDavid Ahern }
45221da177e4SLinus Torvalds 
4523c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4524c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
45251da177e4SLinus Torvalds {
452686872cb5SThomas Graf 	struct fib6_config cfg;
452786872cb5SThomas Graf 	int err;
45281da177e4SLinus Torvalds 
4529333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
453086872cb5SThomas Graf 	if (err < 0)
453186872cb5SThomas Graf 		return err;
453286872cb5SThomas Graf 
453351ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4534333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
453551ebd318SNicolas Dichtel 	else
4536acb54e3cSDavid Ahern 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
45371da177e4SLinus Torvalds }
45381da177e4SLinus Torvalds 
45398d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt)
4540339bf98fSThomas Graf {
4541beb1afacSDavid Ahern 	int nexthop_len = 0;
4542beb1afacSDavid Ahern 
454393c2fb25SDavid Ahern 	if (rt->fib6_nsiblings) {
4544beb1afacSDavid Ahern 		nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
4545beb1afacSDavid Ahern 			    + NLA_ALIGN(sizeof(struct rtnexthop))
4546beb1afacSDavid Ahern 			    + nla_total_size(16) /* RTA_GATEWAY */
45475e670d84SDavid Ahern 			    + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate);
4548beb1afacSDavid Ahern 
454993c2fb25SDavid Ahern 		nexthop_len *= rt->fib6_nsiblings;
4550beb1afacSDavid Ahern 	}
4551beb1afacSDavid Ahern 
4552339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
4553339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
4554339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
4555339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
4556339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
4557339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
4558339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
4559339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
4560339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
45616a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
4562ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
4563c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
456419e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
45655e670d84SDavid Ahern 	       + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate)
4566beb1afacSDavid Ahern 	       + nexthop_len;
4567beb1afacSDavid Ahern }
4568beb1afacSDavid Ahern 
45698d1c802bSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct fib6_info *rt,
45705be083ceSDavid Ahern 			    unsigned int *flags, bool skip_oif)
4571beb1afacSDavid Ahern {
45725e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
4573f9d882eaSIdo Schimmel 		*flags |= RTNH_F_DEAD;
4574f9d882eaSIdo Schimmel 
45755e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN) {
4576beb1afacSDavid Ahern 		*flags |= RTNH_F_LINKDOWN;
4577dcd1f572SDavid Ahern 
4578dcd1f572SDavid Ahern 		rcu_read_lock();
4579dcd1f572SDavid Ahern 		if (fib6_ignore_linkdown(rt))
4580beb1afacSDavid Ahern 			*flags |= RTNH_F_DEAD;
4581dcd1f572SDavid Ahern 		rcu_read_unlock();
4582beb1afacSDavid Ahern 	}
4583beb1afacSDavid Ahern 
458493c2fb25SDavid Ahern 	if (rt->fib6_flags & RTF_GATEWAY) {
45855e670d84SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->fib6_nh.nh_gw) < 0)
4586beb1afacSDavid Ahern 			goto nla_put_failure;
4587beb1afacSDavid Ahern 	}
4588beb1afacSDavid Ahern 
45895e670d84SDavid Ahern 	*flags |= (rt->fib6_nh.nh_flags & RTNH_F_ONLINK);
45905e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_OFFLOAD)
459161e4d01eSIdo Schimmel 		*flags |= RTNH_F_OFFLOAD;
459261e4d01eSIdo Schimmel 
45935be083ceSDavid Ahern 	/* not needed for multipath encoding b/c it has a rtnexthop struct */
45945e670d84SDavid Ahern 	if (!skip_oif && rt->fib6_nh.nh_dev &&
45955e670d84SDavid Ahern 	    nla_put_u32(skb, RTA_OIF, rt->fib6_nh.nh_dev->ifindex))
4596beb1afacSDavid Ahern 		goto nla_put_failure;
4597beb1afacSDavid Ahern 
45985e670d84SDavid Ahern 	if (rt->fib6_nh.nh_lwtstate &&
45995e670d84SDavid Ahern 	    lwtunnel_fill_encap(skb, rt->fib6_nh.nh_lwtstate) < 0)
4600beb1afacSDavid Ahern 		goto nla_put_failure;
4601beb1afacSDavid Ahern 
4602beb1afacSDavid Ahern 	return 0;
4603beb1afacSDavid Ahern 
4604beb1afacSDavid Ahern nla_put_failure:
4605beb1afacSDavid Ahern 	return -EMSGSIZE;
4606beb1afacSDavid Ahern }
4607beb1afacSDavid Ahern 
46085be083ceSDavid Ahern /* add multipath next hop */
46098d1c802bSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct fib6_info *rt)
4610beb1afacSDavid Ahern {
46115e670d84SDavid Ahern 	const struct net_device *dev = rt->fib6_nh.nh_dev;
4612beb1afacSDavid Ahern 	struct rtnexthop *rtnh;
4613beb1afacSDavid Ahern 	unsigned int flags = 0;
4614beb1afacSDavid Ahern 
4615beb1afacSDavid Ahern 	rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
4616beb1afacSDavid Ahern 	if (!rtnh)
4617beb1afacSDavid Ahern 		goto nla_put_failure;
4618beb1afacSDavid Ahern 
46195e670d84SDavid Ahern 	rtnh->rtnh_hops = rt->fib6_nh.nh_weight - 1;
46205e670d84SDavid Ahern 	rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
4621beb1afacSDavid Ahern 
46225be083ceSDavid Ahern 	if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
4623beb1afacSDavid Ahern 		goto nla_put_failure;
4624beb1afacSDavid Ahern 
4625beb1afacSDavid Ahern 	rtnh->rtnh_flags = flags;
4626beb1afacSDavid Ahern 
4627beb1afacSDavid Ahern 	/* length of rtnetlink header + attributes */
4628beb1afacSDavid Ahern 	rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
4629beb1afacSDavid Ahern 
4630beb1afacSDavid Ahern 	return 0;
4631beb1afacSDavid Ahern 
4632beb1afacSDavid Ahern nla_put_failure:
4633beb1afacSDavid Ahern 	return -EMSGSIZE;
4634339bf98fSThomas Graf }
4635339bf98fSThomas Graf 
4636d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
46378d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
4638d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
463915e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
4640f8cfe2ceSDavid Ahern 			 unsigned int flags)
46411da177e4SLinus Torvalds {
464222d0bd82SXin Long 	struct rt6_info *rt6 = (struct rt6_info *)dst;
464322d0bd82SXin Long 	struct rt6key *rt6_dst, *rt6_src;
464422d0bd82SXin Long 	u32 *pmetrics, table, rt6_flags;
46451da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
464622d0bd82SXin Long 	struct rtmsg *rtm;
4647d4ead6b3SDavid Ahern 	long expires = 0;
46481da177e4SLinus Torvalds 
464915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
465038308473SDavid S. Miller 	if (!nlh)
465126932566SPatrick McHardy 		return -EMSGSIZE;
46522d7202bfSThomas Graf 
465322d0bd82SXin Long 	if (rt6) {
465422d0bd82SXin Long 		rt6_dst = &rt6->rt6i_dst;
465522d0bd82SXin Long 		rt6_src = &rt6->rt6i_src;
465622d0bd82SXin Long 		rt6_flags = rt6->rt6i_flags;
465722d0bd82SXin Long 	} else {
465822d0bd82SXin Long 		rt6_dst = &rt->fib6_dst;
465922d0bd82SXin Long 		rt6_src = &rt->fib6_src;
466022d0bd82SXin Long 		rt6_flags = rt->fib6_flags;
466122d0bd82SXin Long 	}
466222d0bd82SXin Long 
46632d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
46641da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
466522d0bd82SXin Long 	rtm->rtm_dst_len = rt6_dst->plen;
466622d0bd82SXin Long 	rtm->rtm_src_len = rt6_src->plen;
46671da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
466893c2fb25SDavid Ahern 	if (rt->fib6_table)
466993c2fb25SDavid Ahern 		table = rt->fib6_table->tb6_id;
4670c71099acSThomas Graf 	else
46719e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
467297f0082aSKalash Nainwal 	rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
4673c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
4674c78679e8SDavid S. Miller 		goto nla_put_failure;
4675e8478e80SDavid Ahern 
4676e8478e80SDavid Ahern 	rtm->rtm_type = rt->fib6_type;
46771da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
46781da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
467993c2fb25SDavid Ahern 	rtm->rtm_protocol = rt->fib6_protocol;
46801da177e4SLinus Torvalds 
468122d0bd82SXin Long 	if (rt6_flags & RTF_CACHE)
46821da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
46831da177e4SLinus Torvalds 
4684d4ead6b3SDavid Ahern 	if (dest) {
4685d4ead6b3SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, dest))
4686c78679e8SDavid S. Miller 			goto nla_put_failure;
46871da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
46881da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
468922d0bd82SXin Long 		if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
4690c78679e8SDavid S. Miller 			goto nla_put_failure;
46911da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
46921da177e4SLinus Torvalds 	if (src) {
4693930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
4694c78679e8SDavid S. Miller 			goto nla_put_failure;
46951da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
4696c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
469722d0bd82SXin Long 		   nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
4698c78679e8SDavid S. Miller 		goto nla_put_failure;
46991da177e4SLinus Torvalds #endif
47007bc570c8SYOSHIFUJI Hideaki 	if (iif) {
47017bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
470222d0bd82SXin Long 		if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
4703fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
47042cf75070SNikolay Aleksandrov 
47057bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
47067bc570c8SYOSHIFUJI Hideaki 				return 0;
4707fd61c6baSDavid Ahern 			if (err < 0)
47087bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
47097bc570c8SYOSHIFUJI Hideaki 		} else
47107bc570c8SYOSHIFUJI Hideaki #endif
4711c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
4712c78679e8SDavid S. Miller 				goto nla_put_failure;
4713d4ead6b3SDavid Ahern 	} else if (dest) {
47141da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
4715d4ead6b3SDavid Ahern 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
4716930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4717c78679e8SDavid S. Miller 			goto nla_put_failure;
4718c3968a85SDaniel Walter 	}
4719c3968a85SDaniel Walter 
472093c2fb25SDavid Ahern 	if (rt->fib6_prefsrc.plen) {
4721c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
472293c2fb25SDavid Ahern 		saddr_buf = rt->fib6_prefsrc.addr;
4723930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4724c78679e8SDavid S. Miller 			goto nla_put_failure;
47251da177e4SLinus Torvalds 	}
47262d7202bfSThomas Graf 
4727d4ead6b3SDavid Ahern 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4728d4ead6b3SDavid Ahern 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
47292d7202bfSThomas Graf 		goto nla_put_failure;
47302d7202bfSThomas Graf 
473193c2fb25SDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
4732beb1afacSDavid Ahern 		goto nla_put_failure;
4733beb1afacSDavid Ahern 
4734beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
4735beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
4736beb1afacSDavid Ahern 	 */
473722d0bd82SXin Long 	if (rt6) {
473822d0bd82SXin Long 		if (rt6_flags & RTF_GATEWAY &&
473922d0bd82SXin Long 		    nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
474022d0bd82SXin Long 			goto nla_put_failure;
474122d0bd82SXin Long 
474222d0bd82SXin Long 		if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
474322d0bd82SXin Long 			goto nla_put_failure;
474422d0bd82SXin Long 	} else if (rt->fib6_nsiblings) {
47458d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
4746beb1afacSDavid Ahern 		struct nlattr *mp;
4747beb1afacSDavid Ahern 
4748beb1afacSDavid Ahern 		mp = nla_nest_start(skb, RTA_MULTIPATH);
4749beb1afacSDavid Ahern 		if (!mp)
4750beb1afacSDavid Ahern 			goto nla_put_failure;
4751beb1afacSDavid Ahern 
4752beb1afacSDavid Ahern 		if (rt6_add_nexthop(skb, rt) < 0)
4753beb1afacSDavid Ahern 			goto nla_put_failure;
4754beb1afacSDavid Ahern 
4755beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
475693c2fb25SDavid Ahern 					 &rt->fib6_siblings, fib6_siblings) {
4757beb1afacSDavid Ahern 			if (rt6_add_nexthop(skb, sibling) < 0)
475894f826b8SEric Dumazet 				goto nla_put_failure;
475994f826b8SEric Dumazet 		}
47602d7202bfSThomas Graf 
4761beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
4762beb1afacSDavid Ahern 	} else {
47635be083ceSDavid Ahern 		if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
4764c78679e8SDavid S. Miller 			goto nla_put_failure;
4765beb1afacSDavid Ahern 	}
47668253947eSLi Wei 
476722d0bd82SXin Long 	if (rt6_flags & RTF_EXPIRES) {
476814895687SDavid Ahern 		expires = dst ? dst->expires : rt->expires;
476914895687SDavid Ahern 		expires -= jiffies;
477014895687SDavid Ahern 	}
477169cdf8f9SYOSHIFUJI Hideaki 
4772d4ead6b3SDavid Ahern 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
4773e3703b3dSThomas Graf 		goto nla_put_failure;
47741da177e4SLinus Torvalds 
477522d0bd82SXin Long 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
4776c78ba6d6SLubomir Rintel 		goto nla_put_failure;
4777c78ba6d6SLubomir Rintel 
477819e42e45SRoopa Prabhu 
4779053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4780053c095aSJohannes Berg 	return 0;
47812d7202bfSThomas Graf 
47822d7202bfSThomas Graf nla_put_failure:
478326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
478426932566SPatrick McHardy 	return -EMSGSIZE;
47851da177e4SLinus Torvalds }
47861da177e4SLinus Torvalds 
478713e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i,
478813e38901SDavid Ahern 			       const struct net_device *dev)
478913e38901SDavid Ahern {
479013e38901SDavid Ahern 	if (f6i->fib6_nh.nh_dev == dev)
479113e38901SDavid Ahern 		return true;
479213e38901SDavid Ahern 
479313e38901SDavid Ahern 	if (f6i->fib6_nsiblings) {
479413e38901SDavid Ahern 		struct fib6_info *sibling, *next_sibling;
479513e38901SDavid Ahern 
479613e38901SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
479713e38901SDavid Ahern 					 &f6i->fib6_siblings, fib6_siblings) {
479813e38901SDavid Ahern 			if (sibling->fib6_nh.nh_dev == dev)
479913e38901SDavid Ahern 				return true;
480013e38901SDavid Ahern 		}
480113e38901SDavid Ahern 	}
480213e38901SDavid Ahern 
480313e38901SDavid Ahern 	return false;
480413e38901SDavid Ahern }
480513e38901SDavid Ahern 
48068d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg)
48071da177e4SLinus Torvalds {
48081da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
480913e38901SDavid Ahern 	struct fib_dump_filter *filter = &arg->filter;
481013e38901SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
48111f17e2f2SDavid Ahern 	struct net *net = arg->net;
48121f17e2f2SDavid Ahern 
4813421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
48141f17e2f2SDavid Ahern 		return 0;
48151da177e4SLinus Torvalds 
481613e38901SDavid Ahern 	if ((filter->flags & RTM_F_PREFIX) &&
481793c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_PREFIX_RT)) {
4818f8cfe2ceSDavid Ahern 		/* success since this is not a prefix route */
4819f8cfe2ceSDavid Ahern 		return 1;
4820f8cfe2ceSDavid Ahern 	}
482113e38901SDavid Ahern 	if (filter->filter_set) {
482213e38901SDavid Ahern 		if ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
482313e38901SDavid Ahern 		    (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
482413e38901SDavid Ahern 		    (filter->protocol && rt->fib6_protocol != filter->protocol)) {
482513e38901SDavid Ahern 			return 1;
482613e38901SDavid Ahern 		}
482713e38901SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
4828f8cfe2ceSDavid Ahern 	}
48291da177e4SLinus Torvalds 
4830d4ead6b3SDavid Ahern 	return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4831d4ead6b3SDavid Ahern 			     RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
483213e38901SDavid Ahern 			     arg->cb->nlh->nlmsg_seq, flags);
48331da177e4SLinus Torvalds }
48341da177e4SLinus Torvalds 
48350eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
48360eff0a27SJakub Kicinski 					const struct nlmsghdr *nlh,
48370eff0a27SJakub Kicinski 					struct nlattr **tb,
48380eff0a27SJakub Kicinski 					struct netlink_ext_ack *extack)
48390eff0a27SJakub Kicinski {
48400eff0a27SJakub Kicinski 	struct rtmsg *rtm;
48410eff0a27SJakub Kicinski 	int i, err;
48420eff0a27SJakub Kicinski 
48430eff0a27SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
48440eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
48450eff0a27SJakub Kicinski 				   "Invalid header for get route request");
48460eff0a27SJakub Kicinski 		return -EINVAL;
48470eff0a27SJakub Kicinski 	}
48480eff0a27SJakub Kicinski 
48490eff0a27SJakub Kicinski 	if (!netlink_strict_get_check(skb))
48500eff0a27SJakub Kicinski 		return nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
48510eff0a27SJakub Kicinski 				   rtm_ipv6_policy, extack);
48520eff0a27SJakub Kicinski 
48530eff0a27SJakub Kicinski 	rtm = nlmsg_data(nlh);
48540eff0a27SJakub Kicinski 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
48550eff0a27SJakub Kicinski 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
48560eff0a27SJakub Kicinski 	    rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
48570eff0a27SJakub Kicinski 	    rtm->rtm_type) {
48580eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
48590eff0a27SJakub Kicinski 		return -EINVAL;
48600eff0a27SJakub Kicinski 	}
48610eff0a27SJakub Kicinski 	if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
48620eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
48630eff0a27SJakub Kicinski 				   "Invalid flags for get route request");
48640eff0a27SJakub Kicinski 		return -EINVAL;
48650eff0a27SJakub Kicinski 	}
48660eff0a27SJakub Kicinski 
48670eff0a27SJakub Kicinski 	err = nlmsg_parse_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
48680eff0a27SJakub Kicinski 				 rtm_ipv6_policy, extack);
48690eff0a27SJakub Kicinski 	if (err)
48700eff0a27SJakub Kicinski 		return err;
48710eff0a27SJakub Kicinski 
48720eff0a27SJakub Kicinski 	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
48730eff0a27SJakub Kicinski 	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
48740eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
48750eff0a27SJakub Kicinski 		return -EINVAL;
48760eff0a27SJakub Kicinski 	}
48770eff0a27SJakub Kicinski 
48780eff0a27SJakub Kicinski 	for (i = 0; i <= RTA_MAX; i++) {
48790eff0a27SJakub Kicinski 		if (!tb[i])
48800eff0a27SJakub Kicinski 			continue;
48810eff0a27SJakub Kicinski 
48820eff0a27SJakub Kicinski 		switch (i) {
48830eff0a27SJakub Kicinski 		case RTA_SRC:
48840eff0a27SJakub Kicinski 		case RTA_DST:
48850eff0a27SJakub Kicinski 		case RTA_IIF:
48860eff0a27SJakub Kicinski 		case RTA_OIF:
48870eff0a27SJakub Kicinski 		case RTA_MARK:
48880eff0a27SJakub Kicinski 		case RTA_UID:
48890eff0a27SJakub Kicinski 		case RTA_SPORT:
48900eff0a27SJakub Kicinski 		case RTA_DPORT:
48910eff0a27SJakub Kicinski 		case RTA_IP_PROTO:
48920eff0a27SJakub Kicinski 			break;
48930eff0a27SJakub Kicinski 		default:
48940eff0a27SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
48950eff0a27SJakub Kicinski 			return -EINVAL;
48960eff0a27SJakub Kicinski 		}
48970eff0a27SJakub Kicinski 	}
48980eff0a27SJakub Kicinski 
48990eff0a27SJakub Kicinski 	return 0;
49000eff0a27SJakub Kicinski }
49010eff0a27SJakub Kicinski 
4902c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4903c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
49041da177e4SLinus Torvalds {
49053b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
4906ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
490718c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
4908a68886a6SDavid Ahern 	struct fib6_info *from;
490918c3a61cSRoopa Prabhu 	struct dst_entry *dst;
49101da177e4SLinus Torvalds 	struct rt6_info *rt;
4911ab364a6fSThomas Graf 	struct sk_buff *skb;
4912ab364a6fSThomas Graf 	struct rtmsg *rtm;
4913744486d4SMaciej Żenczykowski 	struct flowi6 fl6 = {};
491418c3a61cSRoopa Prabhu 	bool fibmatch;
4915ab364a6fSThomas Graf 
49160eff0a27SJakub Kicinski 	err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
4917ab364a6fSThomas Graf 	if (err < 0)
4918ab364a6fSThomas Graf 		goto errout;
4919ab364a6fSThomas Graf 
4920ab364a6fSThomas Graf 	err = -EINVAL;
492138b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
492238b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
492318c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
4924ab364a6fSThomas Graf 
4925ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
4926ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4927ab364a6fSThomas Graf 			goto errout;
4928ab364a6fSThomas Graf 
49294e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
4930ab364a6fSThomas Graf 	}
4931ab364a6fSThomas Graf 
4932ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
4933ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4934ab364a6fSThomas Graf 			goto errout;
4935ab364a6fSThomas Graf 
49364e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
4937ab364a6fSThomas Graf 	}
4938ab364a6fSThomas Graf 
4939ab364a6fSThomas Graf 	if (tb[RTA_IIF])
4940ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
4941ab364a6fSThomas Graf 
4942ab364a6fSThomas Graf 	if (tb[RTA_OIF])
494372331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
4944ab364a6fSThomas Graf 
49452e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
49462e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
49472e47b291SLorenzo Colitti 
4948622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
4949622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
4950622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
4951622ec2c9SLorenzo Colitti 	else
4952622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4953622ec2c9SLorenzo Colitti 
4954eacb9384SRoopa Prabhu 	if (tb[RTA_SPORT])
4955eacb9384SRoopa Prabhu 		fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
4956eacb9384SRoopa Prabhu 
4957eacb9384SRoopa Prabhu 	if (tb[RTA_DPORT])
4958eacb9384SRoopa Prabhu 		fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
4959eacb9384SRoopa Prabhu 
4960eacb9384SRoopa Prabhu 	if (tb[RTA_IP_PROTO]) {
4961eacb9384SRoopa Prabhu 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
49625e1a99eaSHangbin Liu 						  &fl6.flowi6_proto, AF_INET6,
49635e1a99eaSHangbin Liu 						  extack);
4964eacb9384SRoopa Prabhu 		if (err)
4965eacb9384SRoopa Prabhu 			goto errout;
4966eacb9384SRoopa Prabhu 	}
4967eacb9384SRoopa Prabhu 
4968ab364a6fSThomas Graf 	if (iif) {
4969ab364a6fSThomas Graf 		struct net_device *dev;
497072331bc0SShmulik Ladkani 		int flags = 0;
497172331bc0SShmulik Ladkani 
4972121622dbSFlorian Westphal 		rcu_read_lock();
4973121622dbSFlorian Westphal 
4974121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
4975ab364a6fSThomas Graf 		if (!dev) {
4976121622dbSFlorian Westphal 			rcu_read_unlock();
4977ab364a6fSThomas Graf 			err = -ENODEV;
4978ab364a6fSThomas Graf 			goto errout;
4979ab364a6fSThomas Graf 		}
498072331bc0SShmulik Ladkani 
498172331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
498272331bc0SShmulik Ladkani 
498372331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
498472331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
498572331bc0SShmulik Ladkani 
4986b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
4987121622dbSFlorian Westphal 
4988121622dbSFlorian Westphal 		rcu_read_unlock();
498972331bc0SShmulik Ladkani 	} else {
499072331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
499172331bc0SShmulik Ladkani 
499218c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
499318c3a61cSRoopa Prabhu 	}
499418c3a61cSRoopa Prabhu 
499518c3a61cSRoopa Prabhu 
499618c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
499718c3a61cSRoopa Prabhu 	if (rt->dst.error) {
499818c3a61cSRoopa Prabhu 		err = rt->dst.error;
499918c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
500018c3a61cSRoopa Prabhu 		goto errout;
5001ab364a6fSThomas Graf 	}
50021da177e4SLinus Torvalds 
50039d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
50049d6acb3bSWANG Cong 		err = rt->dst.error;
50059d6acb3bSWANG Cong 		ip6_rt_put(rt);
50069d6acb3bSWANG Cong 		goto errout;
50079d6acb3bSWANG Cong 	}
50089d6acb3bSWANG Cong 
50091da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
501038308473SDavid S. Miller 	if (!skb) {
501194e187c0SAmerigo Wang 		ip6_rt_put(rt);
5012ab364a6fSThomas Graf 		err = -ENOBUFS;
5013ab364a6fSThomas Graf 		goto errout;
5014ab364a6fSThomas Graf 	}
50151da177e4SLinus Torvalds 
5016d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
5017a68886a6SDavid Ahern 
5018a68886a6SDavid Ahern 	rcu_read_lock();
5019a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
5020a68886a6SDavid Ahern 
502118c3a61cSRoopa Prabhu 	if (fibmatch)
5022a68886a6SDavid Ahern 		err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, iif,
502318c3a61cSRoopa Prabhu 				    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
502418c3a61cSRoopa Prabhu 				    nlh->nlmsg_seq, 0);
502518c3a61cSRoopa Prabhu 	else
5026a68886a6SDavid Ahern 		err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
5027a68886a6SDavid Ahern 				    &fl6.saddr, iif, RTM_NEWROUTE,
5028d4ead6b3SDavid Ahern 				    NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
5029d4ead6b3SDavid Ahern 				    0);
5030a68886a6SDavid Ahern 	rcu_read_unlock();
5031a68886a6SDavid Ahern 
50321da177e4SLinus Torvalds 	if (err < 0) {
5033ab364a6fSThomas Graf 		kfree_skb(skb);
5034ab364a6fSThomas Graf 		goto errout;
50351da177e4SLinus Torvalds 	}
50361da177e4SLinus Torvalds 
503715e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
5038ab364a6fSThomas Graf errout:
50391da177e4SLinus Torvalds 	return err;
50401da177e4SLinus Torvalds }
50411da177e4SLinus Torvalds 
50428d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
504337a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
50441da177e4SLinus Torvalds {
50451da177e4SLinus Torvalds 	struct sk_buff *skb;
50465578689aSDaniel Lezcano 	struct net *net = info->nl_net;
5047528c4cebSDenis V. Lunev 	u32 seq;
5048528c4cebSDenis V. Lunev 	int err;
50490d51aa80SJamal Hadi Salim 
5050528c4cebSDenis V. Lunev 	err = -ENOBUFS;
505138308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
505286872cb5SThomas Graf 
505319e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
505438308473SDavid S. Miller 	if (!skb)
505521713ebcSThomas Graf 		goto errout;
50561da177e4SLinus Torvalds 
5057d4ead6b3SDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5058f8cfe2ceSDavid Ahern 			    event, info->portid, seq, nlm_flags);
505926932566SPatrick McHardy 	if (err < 0) {
506026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
506126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
506226932566SPatrick McHardy 		kfree_skb(skb);
506326932566SPatrick McHardy 		goto errout;
506426932566SPatrick McHardy 	}
506515e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
50665578689aSDaniel Lezcano 		    info->nlh, gfp_any());
50671ce85fe4SPablo Neira Ayuso 	return;
506821713ebcSThomas Graf errout:
506921713ebcSThomas Graf 	if (err < 0)
50705578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
50711da177e4SLinus Torvalds }
50721da177e4SLinus Torvalds 
50738ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
5074351638e7SJiri Pirko 				unsigned long event, void *ptr)
50758ed67789SDaniel Lezcano {
5076351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5077c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
50788ed67789SDaniel Lezcano 
5079242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
5080242d3a49SWANG Cong 		return NOTIFY_OK;
5081242d3a49SWANG Cong 
5082242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
5083421842edSDavid Ahern 		net->ipv6.fib6_null_entry->fib6_nh.nh_dev = dev;
5084d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
50858ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
50868ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5087d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
50888ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
5089d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
50908ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
50918ed67789SDaniel Lezcano #endif
509276da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
509376da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
509476da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
509576da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
509676da0704SWANG Cong 		 */
509712d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
5098242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
509912d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
510012d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
5101242d3a49SWANG Cong #endif
51028ed67789SDaniel Lezcano 	}
51038ed67789SDaniel Lezcano 
51048ed67789SDaniel Lezcano 	return NOTIFY_OK;
51058ed67789SDaniel Lezcano }
51068ed67789SDaniel Lezcano 
51071da177e4SLinus Torvalds /*
51081da177e4SLinus Torvalds  *	/proc
51091da177e4SLinus Torvalds  */
51101da177e4SLinus Torvalds 
51111da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
51121da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
51131da177e4SLinus Torvalds {
511469ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
51151da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
511669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
511769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
511881eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
511969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
512069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
5121fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
512269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
51231da177e4SLinus Torvalds 
51241da177e4SLinus Torvalds 	return 0;
51251da177e4SLinus Torvalds }
51261da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
51271da177e4SLinus Torvalds 
51281da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
51291da177e4SLinus Torvalds 
51301da177e4SLinus Torvalds static
5131fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
51321da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
51331da177e4SLinus Torvalds {
5134c486da34SLucian Adrian Grijincu 	struct net *net;
5135c486da34SLucian Adrian Grijincu 	int delay;
5136f0fb9b28SAditya Pakki 	int ret;
5137c486da34SLucian Adrian Grijincu 	if (!write)
5138c486da34SLucian Adrian Grijincu 		return -EINVAL;
5139c486da34SLucian Adrian Grijincu 
5140c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
5141c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
5142f0fb9b28SAditya Pakki 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5143f0fb9b28SAditya Pakki 	if (ret)
5144f0fb9b28SAditya Pakki 		return ret;
5145f0fb9b28SAditya Pakki 
51462ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
51471da177e4SLinus Torvalds 	return 0;
51481da177e4SLinus Torvalds }
51491da177e4SLinus Torvalds 
51507c6bb7d2SDavid Ahern static int zero;
51517c6bb7d2SDavid Ahern static int one = 1;
51527c6bb7d2SDavid Ahern 
5153ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = {
51541da177e4SLinus Torvalds 	{
51551da177e4SLinus Torvalds 		.procname	=	"flush",
51564990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
51571da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
515889c8b3a1SDave Jones 		.mode		=	0200,
51596d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
51601da177e4SLinus Torvalds 	},
51611da177e4SLinus Torvalds 	{
51621da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
51639a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
51641da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51651da177e4SLinus Torvalds 		.mode		=	0644,
51666d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
51671da177e4SLinus Torvalds 	},
51681da177e4SLinus Torvalds 	{
51691da177e4SLinus Torvalds 		.procname	=	"max_size",
51704990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
51711da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51721da177e4SLinus Torvalds 		.mode		=	0644,
51736d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
51741da177e4SLinus Torvalds 	},
51751da177e4SLinus Torvalds 	{
51761da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
51774990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
51781da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51791da177e4SLinus Torvalds 		.mode		=	0644,
51806d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
51811da177e4SLinus Torvalds 	},
51821da177e4SLinus Torvalds 	{
51831da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
51844990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
51851da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51861da177e4SLinus Torvalds 		.mode		=	0644,
51876d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
51881da177e4SLinus Torvalds 	},
51891da177e4SLinus Torvalds 	{
51901da177e4SLinus Torvalds 		.procname	=	"gc_interval",
51914990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
51921da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51931da177e4SLinus Torvalds 		.mode		=	0644,
51946d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
51951da177e4SLinus Torvalds 	},
51961da177e4SLinus Torvalds 	{
51971da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
51984990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
51991da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52001da177e4SLinus Torvalds 		.mode		=	0644,
5201f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
52021da177e4SLinus Torvalds 	},
52031da177e4SLinus Torvalds 	{
52041da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
52054990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
52061da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52071da177e4SLinus Torvalds 		.mode		=	0644,
52086d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
52091da177e4SLinus Torvalds 	},
52101da177e4SLinus Torvalds 	{
52111da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
52124990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
52131da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52141da177e4SLinus Torvalds 		.mode		=	0644,
5215f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
52161da177e4SLinus Torvalds 	},
52171da177e4SLinus Torvalds 	{
52181da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
52194990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
52201da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
52211da177e4SLinus Torvalds 		.mode		=	0644,
52226d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
52231da177e4SLinus Torvalds 	},
52247c6bb7d2SDavid Ahern 	{
52257c6bb7d2SDavid Ahern 		.procname	=	"skip_notify_on_dev_down",
52267c6bb7d2SDavid Ahern 		.data		=	&init_net.ipv6.sysctl.skip_notify_on_dev_down,
52277c6bb7d2SDavid Ahern 		.maxlen		=	sizeof(int),
52287c6bb7d2SDavid Ahern 		.mode		=	0644,
52297c6bb7d2SDavid Ahern 		.proc_handler	=	proc_dointvec,
52307c6bb7d2SDavid Ahern 		.extra1		=	&zero,
52317c6bb7d2SDavid Ahern 		.extra2		=	&one,
52327c6bb7d2SDavid Ahern 	},
5233f8572d8fSEric W. Biederman 	{ }
52341da177e4SLinus Torvalds };
52351da177e4SLinus Torvalds 
52362c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
5237760f2d01SDaniel Lezcano {
5238760f2d01SDaniel Lezcano 	struct ctl_table *table;
5239760f2d01SDaniel Lezcano 
5240760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
5241760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
5242760f2d01SDaniel Lezcano 			GFP_KERNEL);
52435ee09105SYOSHIFUJI Hideaki 
52445ee09105SYOSHIFUJI Hideaki 	if (table) {
52455ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
5246c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
524786393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
52485ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
52495ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
52505ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
52515ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
52525ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
52535ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
52545ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
52559c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
52567c6bb7d2SDavid Ahern 		table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
5257464dc801SEric W. Biederman 
5258464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
5259464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
5260464dc801SEric W. Biederman 			table[0].procname = NULL;
52615ee09105SYOSHIFUJI Hideaki 	}
52625ee09105SYOSHIFUJI Hideaki 
5263760f2d01SDaniel Lezcano 	return table;
5264760f2d01SDaniel Lezcano }
52651da177e4SLinus Torvalds #endif
52661da177e4SLinus Torvalds 
52672c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
5268cdb18761SDaniel Lezcano {
5269633d424bSPavel Emelyanov 	int ret = -ENOMEM;
52708ed67789SDaniel Lezcano 
527186393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
527286393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
5273f2fc6a54SBenjamin Thery 
5274fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5275fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
5276fc66f95cSEric Dumazet 
5277421842edSDavid Ahern 	net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template,
5278421842edSDavid Ahern 					    sizeof(*net->ipv6.fib6_null_entry),
5279421842edSDavid Ahern 					    GFP_KERNEL);
5280421842edSDavid Ahern 	if (!net->ipv6.fib6_null_entry)
5281421842edSDavid Ahern 		goto out_ip6_dst_entries;
5282421842edSDavid Ahern 
52838ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
52848ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
52858ed67789SDaniel Lezcano 					   GFP_KERNEL);
52868ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
5287421842edSDavid Ahern 		goto out_fib6_null_entry;
5288d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
528962fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
529062fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
52918ed67789SDaniel Lezcano 
52928ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5293feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
52948ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
52958ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
52968ed67789SDaniel Lezcano 					       GFP_KERNEL);
529768fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
529868fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
5299d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
530062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
530162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
53028ed67789SDaniel Lezcano 
53038ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
53048ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
53058ed67789SDaniel Lezcano 					       GFP_KERNEL);
530668fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
530768fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
5308d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
530962fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
531062fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
53118ed67789SDaniel Lezcano #endif
53128ed67789SDaniel Lezcano 
5313b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
5314b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
5315b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5316b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5317b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5318b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5319b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5320b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
53217c6bb7d2SDavid Ahern 	net->ipv6.sysctl.skip_notify_on_dev_down = 0;
5322b339a47cSPeter Zijlstra 
53236891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
53246891a346SBenjamin Thery 
53258ed67789SDaniel Lezcano 	ret = 0;
53268ed67789SDaniel Lezcano out:
53278ed67789SDaniel Lezcano 	return ret;
5328f2fc6a54SBenjamin Thery 
532968fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
533068fffc67SPeter Zijlstra out_ip6_prohibit_entry:
533168fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
533268fffc67SPeter Zijlstra out_ip6_null_entry:
533368fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
533468fffc67SPeter Zijlstra #endif
5335421842edSDavid Ahern out_fib6_null_entry:
5336421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
5337fc66f95cSEric Dumazet out_ip6_dst_entries:
5338fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5339f2fc6a54SBenjamin Thery out_ip6_dst_ops:
5340f2fc6a54SBenjamin Thery 	goto out;
5341cdb18761SDaniel Lezcano }
5342cdb18761SDaniel Lezcano 
53432c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
5344cdb18761SDaniel Lezcano {
5345421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
53468ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
53478ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
53488ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
53498ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
53508ed67789SDaniel Lezcano #endif
535141bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5352cdb18761SDaniel Lezcano }
5353cdb18761SDaniel Lezcano 
5354d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
5355d189634eSThomas Graf {
5356d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5357c3506372SChristoph Hellwig 	proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
5358c3506372SChristoph Hellwig 			sizeof(struct ipv6_route_iter));
53593617d949SChristoph Hellwig 	proc_create_net_single("rt6_stats", 0444, net->proc_net,
53603617d949SChristoph Hellwig 			rt6_stats_seq_show, NULL);
5361d189634eSThomas Graf #endif
5362d189634eSThomas Graf 	return 0;
5363d189634eSThomas Graf }
5364d189634eSThomas Graf 
5365d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
5366d189634eSThomas Graf {
5367d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5368ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
5369ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
5370d189634eSThomas Graf #endif
5371d189634eSThomas Graf }
5372d189634eSThomas Graf 
5373cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
5374cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
5375cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
5376cdb18761SDaniel Lezcano };
5377cdb18761SDaniel Lezcano 
5378c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
5379c3426b47SDavid S. Miller {
5380c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5381c3426b47SDavid S. Miller 
5382c3426b47SDavid S. Miller 	if (!bp)
5383c3426b47SDavid S. Miller 		return -ENOMEM;
5384c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
5385c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
5386c3426b47SDavid S. Miller 	return 0;
5387c3426b47SDavid S. Miller }
5388c3426b47SDavid S. Miller 
5389c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
5390c3426b47SDavid S. Miller {
5391c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
5392c3426b47SDavid S. Miller 
5393c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
539456a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
5395c3426b47SDavid S. Miller 	kfree(bp);
5396c3426b47SDavid S. Miller }
5397c3426b47SDavid S. Miller 
53982b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
5399c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
5400c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
5401c3426b47SDavid S. Miller };
5402c3426b47SDavid S. Miller 
5403d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
5404d189634eSThomas Graf 	.init = ip6_route_net_init_late,
5405d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
5406d189634eSThomas Graf };
5407d189634eSThomas Graf 
54088ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
54098ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
5410242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
54118ed67789SDaniel Lezcano };
54128ed67789SDaniel Lezcano 
54132f460933SWANG Cong void __init ip6_route_init_special_entries(void)
54142f460933SWANG Cong {
54152f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
54162f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
54172f460933SWANG Cong 	 * manually for init_net */
5418421842edSDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_nh.nh_dev = init_net.loopback_dev;
54192f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
54202f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
54212f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
54222f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
54232f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
54242f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
54252f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
54262f460933SWANG Cong   #endif
54272f460933SWANG Cong }
54282f460933SWANG Cong 
5429433d49c3SDaniel Lezcano int __init ip6_route_init(void)
54301da177e4SLinus Torvalds {
5431433d49c3SDaniel Lezcano 	int ret;
54328d0b94afSMartin KaFai Lau 	int cpu;
5433433d49c3SDaniel Lezcano 
54349a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
54359a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
54369a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
54379a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
54389a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
5439c19a28e1SFernando Carrijo 		goto out;
544014e50e57SDavid S. Miller 
5441fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
54428ed67789SDaniel Lezcano 	if (ret)
5443bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
5444bdb3289fSDaniel Lezcano 
5445c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5446c3426b47SDavid S. Miller 	if (ret)
5447e8803b6cSDavid S. Miller 		goto out_dst_entries;
54482a0c451aSThomas Graf 
54497e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
54507e52b33bSDavid S. Miller 	if (ret)
54517e52b33bSDavid S. Miller 		goto out_register_inetpeer;
5452c3426b47SDavid S. Miller 
54535dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
54545dc121e9SArnaud Ebalard 
5455e8803b6cSDavid S. Miller 	ret = fib6_init();
5456433d49c3SDaniel Lezcano 	if (ret)
54578ed67789SDaniel Lezcano 		goto out_register_subsys;
5458433d49c3SDaniel Lezcano 
5459433d49c3SDaniel Lezcano 	ret = xfrm6_init();
5460433d49c3SDaniel Lezcano 	if (ret)
5461e8803b6cSDavid S. Miller 		goto out_fib6_init;
5462c35b7e72SDaniel Lezcano 
5463433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
5464433d49c3SDaniel Lezcano 	if (ret)
5465433d49c3SDaniel Lezcano 		goto xfrm6_init;
54667e5449c2SDaniel Lezcano 
5467d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
5468d189634eSThomas Graf 	if (ret)
5469d189634eSThomas Graf 		goto fib6_rules_init;
5470d189634eSThomas Graf 
547116feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
547216feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
547316feebcfSFlorian Westphal 	if (ret < 0)
547416feebcfSFlorian Westphal 		goto out_register_late_subsys;
547516feebcfSFlorian Westphal 
547616feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
547716feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
547816feebcfSFlorian Westphal 	if (ret < 0)
547916feebcfSFlorian Westphal 		goto out_register_late_subsys;
548016feebcfSFlorian Westphal 
548116feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
548216feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
548316feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
548416feebcfSFlorian Westphal 	if (ret < 0)
5485d189634eSThomas Graf 		goto out_register_late_subsys;
5486433d49c3SDaniel Lezcano 
54878ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
5488cdb18761SDaniel Lezcano 	if (ret)
5489d189634eSThomas Graf 		goto out_register_late_subsys;
54908ed67789SDaniel Lezcano 
54918d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
54928d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
54938d0b94afSMartin KaFai Lau 
54948d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
54958d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
54968d0b94afSMartin KaFai Lau 	}
54978d0b94afSMartin KaFai Lau 
5498433d49c3SDaniel Lezcano out:
5499433d49c3SDaniel Lezcano 	return ret;
5500433d49c3SDaniel Lezcano 
5501d189634eSThomas Graf out_register_late_subsys:
550216feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
5503d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5504433d49c3SDaniel Lezcano fib6_rules_init:
5505433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
5506433d49c3SDaniel Lezcano xfrm6_init:
5507433d49c3SDaniel Lezcano 	xfrm6_fini();
55082a0c451aSThomas Graf out_fib6_init:
55092a0c451aSThomas Graf 	fib6_gc_cleanup();
55108ed67789SDaniel Lezcano out_register_subsys:
55118ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
55127e52b33bSDavid S. Miller out_register_inetpeer:
55137e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
5514fc66f95cSEric Dumazet out_dst_entries:
5515fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5516433d49c3SDaniel Lezcano out_kmem_cache:
5517f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
5518433d49c3SDaniel Lezcano 	goto out;
55191da177e4SLinus Torvalds }
55201da177e4SLinus Torvalds 
55211da177e4SLinus Torvalds void ip6_route_cleanup(void)
55221da177e4SLinus Torvalds {
55238ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
5524d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5525101367c2SThomas Graf 	fib6_rules_cleanup();
55261da177e4SLinus Torvalds 	xfrm6_fini();
55271da177e4SLinus Torvalds 	fib6_gc_cleanup();
5528c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
55298ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
553041bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5531f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
55321da177e4SLinus Torvalds }
5533