xref: /openbmc/linux/net/ipv6/route.c (revision 97f0082a0592212fc15d4680f5a4d80f79a1687c)
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))
1043e873e4b9SWei Wang 		return NULL;
1044e873e4b9SWei Wang 
104593531c67SDavid Ahern 	nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
1046dec9b0e2SDavid Ahern 	if (nrt)
1047dec9b0e2SDavid Ahern 		ip6_rt_copy_init(nrt, rt);
1048e873e4b9SWei Wang 	else
1049e873e4b9SWei Wang 		fib6_info_release(rt);
1050dec9b0e2SDavid Ahern 
1051dec9b0e2SDavid Ahern 	return nrt;
1052dec9b0e2SDavid Ahern }
1053dec9b0e2SDavid Ahern 
10548ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
10558ed67789SDaniel Lezcano 					     struct fib6_table *table,
1056b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
1057b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
1058b75cc8f9SDavid Ahern 					     int flags)
10591da177e4SLinus Torvalds {
10608d1c802bSDavid Ahern 	struct fib6_info *f6i;
10611da177e4SLinus Torvalds 	struct fib6_node *fn;
106223fb93a4SDavid Ahern 	struct rt6_info *rt;
10631da177e4SLinus Torvalds 
1064b6cdbc85SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1065b6cdbc85SDavid Ahern 		flags &= ~RT6_LOOKUP_F_IFACE;
1066b6cdbc85SDavid Ahern 
106766f5d6ceSWei Wang 	rcu_read_lock();
10686454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1069c71099acSThomas Graf restart:
107023fb93a4SDavid Ahern 	f6i = rcu_dereference(fn->leaf);
107123fb93a4SDavid Ahern 	if (!f6i) {
107223fb93a4SDavid Ahern 		f6i = net->ipv6.fib6_null_entry;
107366f5d6ceSWei Wang 	} else {
107423fb93a4SDavid Ahern 		f6i = rt6_device_match(net, f6i, &fl6->saddr,
107566f5d6ceSWei Wang 				      fl6->flowi6_oif, flags);
107693c2fb25SDavid Ahern 		if (f6i->fib6_nsiblings && fl6->flowi6_oif == 0)
10773b290a31SDavid Ahern 			f6i = fib6_multipath_select(net, f6i, fl6,
10783b290a31SDavid Ahern 						    fl6->flowi6_oif, skb,
10793b290a31SDavid Ahern 						    flags);
108066f5d6ceSWei Wang 	}
108123fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1082a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1083a3c00e46SMartin KaFai Lau 		if (fn)
1084a3c00e46SMartin KaFai Lau 			goto restart;
1085a3c00e46SMartin KaFai Lau 	}
10862b760fcfSWei Wang 
1087d4bea421SDavid Ahern 	trace_fib6_table_lookup(net, f6i, table, fl6);
1088d4bea421SDavid Ahern 
10894c9483b2SDavid S. Miller 	/* Search through exception table */
109023fb93a4SDavid Ahern 	rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
109123fb93a4SDavid Ahern 	if (rt) {
1092d3843fe5SWei Wang 		if (ip6_hold_safe(net, &rt, true))
1093d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
109423fb93a4SDavid Ahern 	} else if (f6i == net->ipv6.fib6_null_entry) {
1095dec9b0e2SDavid Ahern 		rt = net->ipv6.ip6_null_entry;
1096dec9b0e2SDavid Ahern 		dst_hold(&rt->dst);
109723fb93a4SDavid Ahern 	} else {
109823fb93a4SDavid Ahern 		rt = ip6_create_rt_rcu(f6i);
109923fb93a4SDavid Ahern 		if (!rt) {
110023fb93a4SDavid Ahern 			rt = net->ipv6.ip6_null_entry;
110123fb93a4SDavid Ahern 			dst_hold(&rt->dst);
110223fb93a4SDavid Ahern 		}
1103dec9b0e2SDavid Ahern 	}
1104d3843fe5SWei Wang 
110566f5d6ceSWei Wang 	rcu_read_unlock();
1106b811580dSDavid Ahern 
11071da177e4SLinus Torvalds 	return rt;
1108c71099acSThomas Graf }
1109c71099acSThomas Graf 
1110ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1111b75cc8f9SDavid Ahern 				   const struct sk_buff *skb, int flags)
1112ea6e574eSFlorian Westphal {
1113b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1114ea6e574eSFlorian Westphal }
1115ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
1116ea6e574eSFlorian Westphal 
11179acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1118b75cc8f9SDavid Ahern 			    const struct in6_addr *saddr, int oif,
1119b75cc8f9SDavid Ahern 			    const struct sk_buff *skb, int strict)
1120c71099acSThomas Graf {
11214c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
11224c9483b2SDavid S. Miller 		.flowi6_oif = oif,
11234c9483b2SDavid S. Miller 		.daddr = *daddr,
1124c71099acSThomas Graf 	};
1125c71099acSThomas Graf 	struct dst_entry *dst;
112677d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1127c71099acSThomas Graf 
1128adaa70bbSThomas Graf 	if (saddr) {
11294c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1130adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1131adaa70bbSThomas Graf 	}
1132adaa70bbSThomas Graf 
1133b75cc8f9SDavid Ahern 	dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1134c71099acSThomas Graf 	if (dst->error == 0)
1135c71099acSThomas Graf 		return (struct rt6_info *) dst;
1136c71099acSThomas Graf 
1137c71099acSThomas Graf 	dst_release(dst);
1138c71099acSThomas Graf 
11391da177e4SLinus Torvalds 	return NULL;
11401da177e4SLinus Torvalds }
11417159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
11427159039aSYOSHIFUJI Hideaki 
1143c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
11441cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
11451cfb71eeSWei Wang  * route is released.
11461cfb71eeSWei Wang  * Caller must hold dst before calling it.
11471da177e4SLinus Torvalds  */
11481da177e4SLinus Torvalds 
11498d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1150333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
11511da177e4SLinus Torvalds {
11521da177e4SLinus Torvalds 	int err;
1153c71099acSThomas Graf 	struct fib6_table *table;
11541da177e4SLinus Torvalds 
115593c2fb25SDavid Ahern 	table = rt->fib6_table;
115666f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
1157d4ead6b3SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, extack);
115866f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	return err;
11611da177e4SLinus Torvalds }
11621da177e4SLinus Torvalds 
11638d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt)
116440e22e8fSThomas Graf {
1165afb1d4b5SDavid Ahern 	struct nl_info info = {	.nl_net = net, };
1166e715b6d3SFlorian Westphal 
1167d4ead6b3SDavid Ahern 	return __ip6_ins_rt(rt, &info, NULL);
116840e22e8fSThomas Graf }
116940e22e8fSThomas Graf 
11708d1c802bSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(struct fib6_info *ort,
117121efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
1172b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
11731da177e4SLinus Torvalds {
11744832c30dSDavid Ahern 	struct net_device *dev;
11751da177e4SLinus Torvalds 	struct rt6_info *rt;
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	/*
11781da177e4SLinus Torvalds 	 *	Clone the route.
11791da177e4SLinus Torvalds 	 */
11801da177e4SLinus Torvalds 
1181e873e4b9SWei Wang 	if (!fib6_info_hold_safe(ort))
1182e873e4b9SWei Wang 		return NULL;
1183e873e4b9SWei Wang 
11844832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(ort);
118593531c67SDavid Ahern 	rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1186e873e4b9SWei Wang 	if (!rt) {
1187e873e4b9SWei Wang 		fib6_info_release(ort);
118883a09abdSMartin KaFai Lau 		return NULL;
1189e873e4b9SWei Wang 	}
119083a09abdSMartin KaFai Lau 
119183a09abdSMartin KaFai Lau 	ip6_rt_copy_init(rt, ort);
11928b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
119383a09abdSMartin KaFai Lau 	rt->dst.flags |= DST_HOST;
119483a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
119583a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
11968b9df265SMartin KaFai Lau 
11978b9df265SMartin KaFai Lau 	if (!rt6_is_gw_or_nonexthop(ort)) {
119893c2fb25SDavid Ahern 		if (ort->fib6_dst.plen != 128 &&
119993c2fb25SDavid Ahern 		    ipv6_addr_equal(&ort->fib6_dst.addr, daddr))
120058c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
12011da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
12021da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
12034e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
12041da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
12051da177e4SLinus Torvalds 		}
12061da177e4SLinus Torvalds #endif
120795a9a5baSYOSHIFUJI Hideaki 	}
120895a9a5baSYOSHIFUJI Hideaki 
1209299d9939SYOSHIFUJI Hideaki 	return rt;
1210299d9939SYOSHIFUJI Hideaki }
1211299d9939SYOSHIFUJI Hideaki 
12128d1c802bSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(struct fib6_info *rt)
1213d52d3997SMartin KaFai Lau {
12143b6761d1SDavid Ahern 	unsigned short flags = fib6_info_dst_flags(rt);
12154832c30dSDavid Ahern 	struct net_device *dev;
1216d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1217d52d3997SMartin KaFai Lau 
1218e873e4b9SWei Wang 	if (!fib6_info_hold_safe(rt))
1219e873e4b9SWei Wang 		return NULL;
1220e873e4b9SWei Wang 
12214832c30dSDavid Ahern 	rcu_read_lock();
12224832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(rt);
122393531c67SDavid Ahern 	pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
12244832c30dSDavid Ahern 	rcu_read_unlock();
1225e873e4b9SWei Wang 	if (!pcpu_rt) {
1226e873e4b9SWei Wang 		fib6_info_release(rt);
1227d52d3997SMartin KaFai Lau 		return NULL;
1228e873e4b9SWei Wang 	}
1229d52d3997SMartin KaFai Lau 	ip6_rt_copy_init(pcpu_rt, rt);
1230d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
1231d52d3997SMartin KaFai Lau 	return pcpu_rt;
1232d52d3997SMartin KaFai Lau }
1233d52d3997SMartin KaFai Lau 
123466f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */
12358d1c802bSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(struct fib6_info *rt)
1236d52d3997SMartin KaFai Lau {
1237a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, **p;
1238d52d3997SMartin KaFai Lau 
1239d52d3997SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1240d52d3997SMartin KaFai Lau 	pcpu_rt = *p;
1241d52d3997SMartin KaFai Lau 
1242d4ead6b3SDavid Ahern 	if (pcpu_rt)
1243d4ead6b3SDavid Ahern 		ip6_hold_safe(NULL, &pcpu_rt, false);
1244d3843fe5SWei Wang 
1245a73e4195SMartin KaFai Lau 	return pcpu_rt;
1246a73e4195SMartin KaFai Lau }
1247a73e4195SMartin KaFai Lau 
1248afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net,
12498d1c802bSDavid Ahern 					    struct fib6_info *rt)
1250a73e4195SMartin KaFai Lau {
1251a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1252d52d3997SMartin KaFai Lau 
1253d52d3997SMartin KaFai Lau 	pcpu_rt = ip6_rt_pcpu_alloc(rt);
1254d52d3997SMartin KaFai Lau 	if (!pcpu_rt) {
12559c7370a1SMartin KaFai Lau 		dst_hold(&net->ipv6.ip6_null_entry->dst);
12569c7370a1SMartin KaFai Lau 		return net->ipv6.ip6_null_entry;
1257d52d3997SMartin KaFai Lau 	}
1258d52d3997SMartin KaFai Lau 
1259a94b9367SWei Wang 	dst_hold(&pcpu_rt->dst);
1260a73e4195SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1261d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1262951f788aSEric Dumazet 	BUG_ON(prev);
1263a94b9367SWei Wang 
1264d52d3997SMartin KaFai Lau 	return pcpu_rt;
1265d52d3997SMartin KaFai Lau }
1266d52d3997SMartin KaFai Lau 
126735732d01SWei Wang /* exception hash table implementation
126835732d01SWei Wang  */
126935732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
127035732d01SWei Wang 
127135732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
127235732d01SWei Wang  * Caller must hold rt6_exception_lock
127335732d01SWei Wang  */
127435732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
127535732d01SWei Wang 				 struct rt6_exception *rt6_ex)
127635732d01SWei Wang {
1277f5b51fe8SPaolo Abeni 	struct fib6_info *from;
1278b2427e67SColin Ian King 	struct net *net;
127981eb8447SWei Wang 
128035732d01SWei Wang 	if (!bucket || !rt6_ex)
128135732d01SWei Wang 		return;
1282b2427e67SColin Ian King 
1283b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
1284f5b51fe8SPaolo Abeni 	net->ipv6.rt6_stats->fib_rt_cache--;
1285f5b51fe8SPaolo Abeni 
1286f5b51fe8SPaolo Abeni 	/* purge completely the exception to allow releasing the held resources:
1287f5b51fe8SPaolo Abeni 	 * some [sk] cache may keep the dst around for unlimited time
1288f5b51fe8SPaolo Abeni 	 */
1289f5b51fe8SPaolo Abeni 	from = rcu_dereference_protected(rt6_ex->rt6i->from,
1290f5b51fe8SPaolo Abeni 					 lockdep_is_held(&rt6_exception_lock));
1291f5b51fe8SPaolo Abeni 	rcu_assign_pointer(rt6_ex->rt6i->from, NULL);
1292f5b51fe8SPaolo Abeni 	fib6_info_release(from);
1293f5b51fe8SPaolo Abeni 	dst_dev_put(&rt6_ex->rt6i->dst);
1294f5b51fe8SPaolo Abeni 
129535732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
129677634cc6SDavid Ahern 	dst_release(&rt6_ex->rt6i->dst);
129735732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
129835732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
129935732d01SWei Wang 	bucket->depth--;
130035732d01SWei Wang }
130135732d01SWei Wang 
130235732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
130335732d01SWei Wang  * Caller must hold rt6_exception_lock
130435732d01SWei Wang  */
130535732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
130635732d01SWei Wang {
130735732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
130835732d01SWei Wang 
130935732d01SWei Wang 	if (!bucket)
131035732d01SWei Wang 		return;
131135732d01SWei Wang 
131235732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
131335732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
131435732d01SWei Wang 			oldest = rt6_ex;
131535732d01SWei Wang 	}
131635732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
131735732d01SWei Wang }
131835732d01SWei Wang 
131935732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
132035732d01SWei Wang 			      const struct in6_addr *src)
132135732d01SWei Wang {
132235732d01SWei Wang 	static u32 seed __read_mostly;
132335732d01SWei Wang 	u32 val;
132435732d01SWei Wang 
132535732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
132635732d01SWei Wang 	val = jhash(dst, sizeof(*dst), seed);
132735732d01SWei Wang 
132835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
132935732d01SWei Wang 	if (src)
133035732d01SWei Wang 		val = jhash(src, sizeof(*src), val);
133135732d01SWei Wang #endif
133235732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
133335732d01SWei Wang }
133435732d01SWei Wang 
133535732d01SWei Wang /* Helper function to find the cached rt in the hash table
133635732d01SWei Wang  * and update bucket pointer to point to the bucket for this
133735732d01SWei Wang  * (daddr, saddr) pair
133835732d01SWei Wang  * Caller must hold rt6_exception_lock
133935732d01SWei Wang  */
134035732d01SWei Wang static struct rt6_exception *
134135732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
134235732d01SWei Wang 			      const struct in6_addr *daddr,
134335732d01SWei Wang 			      const struct in6_addr *saddr)
134435732d01SWei Wang {
134535732d01SWei Wang 	struct rt6_exception *rt6_ex;
134635732d01SWei Wang 	u32 hval;
134735732d01SWei Wang 
134835732d01SWei Wang 	if (!(*bucket) || !daddr)
134935732d01SWei Wang 		return NULL;
135035732d01SWei Wang 
135135732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
135235732d01SWei Wang 	*bucket += hval;
135335732d01SWei Wang 
135435732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
135535732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
135635732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
135735732d01SWei Wang 
135835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
135935732d01SWei Wang 		if (matched && saddr)
136035732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
136135732d01SWei Wang #endif
136235732d01SWei Wang 		if (matched)
136335732d01SWei Wang 			return rt6_ex;
136435732d01SWei Wang 	}
136535732d01SWei Wang 	return NULL;
136635732d01SWei Wang }
136735732d01SWei Wang 
136835732d01SWei Wang /* Helper function to find the cached rt in the hash table
136935732d01SWei Wang  * and update bucket pointer to point to the bucket for this
137035732d01SWei Wang  * (daddr, saddr) pair
137135732d01SWei Wang  * Caller must hold rcu_read_lock()
137235732d01SWei Wang  */
137335732d01SWei Wang static struct rt6_exception *
137435732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
137535732d01SWei Wang 			 const struct in6_addr *daddr,
137635732d01SWei Wang 			 const struct in6_addr *saddr)
137735732d01SWei Wang {
137835732d01SWei Wang 	struct rt6_exception *rt6_ex;
137935732d01SWei Wang 	u32 hval;
138035732d01SWei Wang 
138135732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
138235732d01SWei Wang 
138335732d01SWei Wang 	if (!(*bucket) || !daddr)
138435732d01SWei Wang 		return NULL;
138535732d01SWei Wang 
138635732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
138735732d01SWei Wang 	*bucket += hval;
138835732d01SWei Wang 
138935732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
139035732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
139135732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
139235732d01SWei Wang 
139335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
139435732d01SWei Wang 		if (matched && saddr)
139535732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
139635732d01SWei Wang #endif
139735732d01SWei Wang 		if (matched)
139835732d01SWei Wang 			return rt6_ex;
139935732d01SWei Wang 	}
140035732d01SWei Wang 	return NULL;
140135732d01SWei Wang }
140235732d01SWei Wang 
14038d1c802bSDavid Ahern static unsigned int fib6_mtu(const struct fib6_info *rt)
140435732d01SWei Wang {
1405d4ead6b3SDavid Ahern 	unsigned int mtu;
1406d4ead6b3SDavid Ahern 
1407dcd1f572SDavid Ahern 	if (rt->fib6_pmtu) {
1408dcd1f572SDavid Ahern 		mtu = rt->fib6_pmtu;
1409dcd1f572SDavid Ahern 	} else {
1410dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
1411dcd1f572SDavid Ahern 		struct inet6_dev *idev;
1412dcd1f572SDavid Ahern 
1413dcd1f572SDavid Ahern 		rcu_read_lock();
1414dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
1415dcd1f572SDavid Ahern 		mtu = idev->cnf.mtu6;
1416dcd1f572SDavid Ahern 		rcu_read_unlock();
1417dcd1f572SDavid Ahern 	}
1418dcd1f572SDavid Ahern 
1419d4ead6b3SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1420d4ead6b3SDavid Ahern 
1421d4ead6b3SDavid Ahern 	return mtu - lwtunnel_headroom(rt->fib6_nh.nh_lwtstate, mtu);
1422d4ead6b3SDavid Ahern }
1423d4ead6b3SDavid Ahern 
142435732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
14258d1c802bSDavid Ahern 				struct fib6_info *ort)
142635732d01SWei Wang {
14275e670d84SDavid Ahern 	struct net *net = dev_net(nrt->dst.dev);
142835732d01SWei Wang 	struct rt6_exception_bucket *bucket;
142935732d01SWei Wang 	struct in6_addr *src_key = NULL;
143035732d01SWei Wang 	struct rt6_exception *rt6_ex;
143135732d01SWei Wang 	int err = 0;
143235732d01SWei Wang 
143335732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
143435732d01SWei Wang 
143535732d01SWei Wang 	if (ort->exception_bucket_flushed) {
143635732d01SWei Wang 		err = -EINVAL;
143735732d01SWei Wang 		goto out;
143835732d01SWei Wang 	}
143935732d01SWei Wang 
144035732d01SWei Wang 	bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
144135732d01SWei Wang 					lockdep_is_held(&rt6_exception_lock));
144235732d01SWei Wang 	if (!bucket) {
144335732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
144435732d01SWei Wang 				 GFP_ATOMIC);
144535732d01SWei Wang 		if (!bucket) {
144635732d01SWei Wang 			err = -ENOMEM;
144735732d01SWei Wang 			goto out;
144835732d01SWei Wang 		}
144935732d01SWei Wang 		rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
145035732d01SWei Wang 	}
145135732d01SWei Wang 
145235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
145335732d01SWei Wang 	/* rt6i_src.plen != 0 indicates ort is in subtree
145435732d01SWei Wang 	 * and exception table is indexed by a hash of
145535732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
145635732d01SWei Wang 	 * Otherwise, the exception table is indexed by
145735732d01SWei Wang 	 * a hash of only rt6i_dst.
145835732d01SWei Wang 	 */
145993c2fb25SDavid Ahern 	if (ort->fib6_src.plen)
146035732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
146135732d01SWei Wang #endif
1462f5bbe7eeSWei Wang 	/* rt6_mtu_change() might lower mtu on ort.
1463f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
1464f5bbe7eeSWei Wang 	 * is less than ort's mtu value.
1465f5bbe7eeSWei Wang 	 */
1466d4ead6b3SDavid Ahern 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(ort)) {
1467f5bbe7eeSWei Wang 		err = -EINVAL;
1468f5bbe7eeSWei Wang 		goto out;
1469f5bbe7eeSWei Wang 	}
147060006a48SWei Wang 
147135732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
147235732d01SWei Wang 					       src_key);
147335732d01SWei Wang 	if (rt6_ex)
147435732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
147535732d01SWei Wang 
147635732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
147735732d01SWei Wang 	if (!rt6_ex) {
147835732d01SWei Wang 		err = -ENOMEM;
147935732d01SWei Wang 		goto out;
148035732d01SWei Wang 	}
148135732d01SWei Wang 	rt6_ex->rt6i = nrt;
148235732d01SWei Wang 	rt6_ex->stamp = jiffies;
148335732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
148435732d01SWei Wang 	bucket->depth++;
148581eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
148635732d01SWei Wang 
148735732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
148835732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
148935732d01SWei Wang 
149035732d01SWei Wang out:
149135732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
149235732d01SWei Wang 
149335732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1494b886d5f2SPaolo Abeni 	if (!err) {
149593c2fb25SDavid Ahern 		spin_lock_bh(&ort->fib6_table->tb6_lock);
14967aef6859SDavid Ahern 		fib6_update_sernum(net, ort);
149793c2fb25SDavid Ahern 		spin_unlock_bh(&ort->fib6_table->tb6_lock);
1498b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1499b886d5f2SPaolo Abeni 	}
150035732d01SWei Wang 
150135732d01SWei Wang 	return err;
150235732d01SWei Wang }
150335732d01SWei Wang 
15048d1c802bSDavid Ahern void rt6_flush_exceptions(struct fib6_info *rt)
150535732d01SWei Wang {
150635732d01SWei Wang 	struct rt6_exception_bucket *bucket;
150735732d01SWei Wang 	struct rt6_exception *rt6_ex;
150835732d01SWei Wang 	struct hlist_node *tmp;
150935732d01SWei Wang 	int i;
151035732d01SWei Wang 
151135732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
151235732d01SWei Wang 	/* Prevent rt6_insert_exception() to recreate the bucket list */
151335732d01SWei Wang 	rt->exception_bucket_flushed = 1;
151435732d01SWei Wang 
151535732d01SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
151635732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
151735732d01SWei Wang 	if (!bucket)
151835732d01SWei Wang 		goto out;
151935732d01SWei Wang 
152035732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
152135732d01SWei Wang 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
152235732d01SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
152335732d01SWei Wang 		WARN_ON_ONCE(bucket->depth);
152435732d01SWei Wang 		bucket++;
152535732d01SWei Wang 	}
152635732d01SWei Wang 
152735732d01SWei Wang out:
152835732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
152935732d01SWei Wang }
153035732d01SWei Wang 
153135732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
153235732d01SWei Wang  * Caller has to hold rcu_read_lock()
153335732d01SWei Wang  */
15348d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
153535732d01SWei Wang 					   struct in6_addr *daddr,
153635732d01SWei Wang 					   struct in6_addr *saddr)
153735732d01SWei Wang {
153835732d01SWei Wang 	struct rt6_exception_bucket *bucket;
153935732d01SWei Wang 	struct in6_addr *src_key = NULL;
154035732d01SWei Wang 	struct rt6_exception *rt6_ex;
154135732d01SWei Wang 	struct rt6_info *res = NULL;
154235732d01SWei Wang 
154335732d01SWei Wang 	bucket = rcu_dereference(rt->rt6i_exception_bucket);
154435732d01SWei Wang 
154535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
154635732d01SWei Wang 	/* rt6i_src.plen != 0 indicates rt is in subtree
154735732d01SWei Wang 	 * and exception table is indexed by a hash of
154835732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
154935732d01SWei Wang 	 * Otherwise, the exception table is indexed by
155035732d01SWei Wang 	 * a hash of only rt6i_dst.
155135732d01SWei Wang 	 */
155293c2fb25SDavid Ahern 	if (rt->fib6_src.plen)
155335732d01SWei Wang 		src_key = saddr;
155435732d01SWei Wang #endif
155535732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
155635732d01SWei Wang 
155735732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
155835732d01SWei Wang 		res = rt6_ex->rt6i;
155935732d01SWei Wang 
156035732d01SWei Wang 	return res;
156135732d01SWei Wang }
156235732d01SWei Wang 
156335732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
156423fb93a4SDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt)
156535732d01SWei Wang {
156635732d01SWei Wang 	struct rt6_exception_bucket *bucket;
156735732d01SWei Wang 	struct in6_addr *src_key = NULL;
156835732d01SWei Wang 	struct rt6_exception *rt6_ex;
15698a14e46fSDavid Ahern 	struct fib6_info *from;
157035732d01SWei Wang 	int err;
157135732d01SWei Wang 
1572091311deSEric Dumazet 	from = rcu_dereference(rt->from);
157335732d01SWei Wang 	if (!from ||
1574442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
157535732d01SWei Wang 		return -EINVAL;
157635732d01SWei Wang 
157735732d01SWei Wang 	if (!rcu_access_pointer(from->rt6i_exception_bucket))
157835732d01SWei Wang 		return -ENOENT;
157935732d01SWei Wang 
158035732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
158135732d01SWei Wang 	bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
158235732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
158335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
158435732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
158535732d01SWei Wang 	 * and exception table is indexed by a hash of
158635732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
158735732d01SWei Wang 	 * Otherwise, the exception table is indexed by
158835732d01SWei Wang 	 * a hash of only rt6i_dst.
158935732d01SWei Wang 	 */
159093c2fb25SDavid Ahern 	if (from->fib6_src.plen)
159135732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
159235732d01SWei Wang #endif
159335732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
159435732d01SWei Wang 					       &rt->rt6i_dst.addr,
159535732d01SWei Wang 					       src_key);
159635732d01SWei Wang 	if (rt6_ex) {
159735732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
159835732d01SWei Wang 		err = 0;
159935732d01SWei Wang 	} else {
160035732d01SWei Wang 		err = -ENOENT;
160135732d01SWei Wang 	}
160235732d01SWei Wang 
160335732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
160435732d01SWei Wang 	return err;
160535732d01SWei Wang }
160635732d01SWei Wang 
160735732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
160835732d01SWei Wang  * refresh its stamp
160935732d01SWei Wang  */
161035732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
161135732d01SWei Wang {
161235732d01SWei Wang 	struct rt6_exception_bucket *bucket;
161335732d01SWei Wang 	struct in6_addr *src_key = NULL;
161435732d01SWei Wang 	struct rt6_exception *rt6_ex;
1615193f3685SPaolo Abeni 	struct fib6_info *from;
161635732d01SWei Wang 
161735732d01SWei Wang 	rcu_read_lock();
1618193f3685SPaolo Abeni 	from = rcu_dereference(rt->from);
1619193f3685SPaolo Abeni 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1620193f3685SPaolo Abeni 		goto unlock;
1621193f3685SPaolo Abeni 
162235732d01SWei Wang 	bucket = rcu_dereference(from->rt6i_exception_bucket);
162335732d01SWei Wang 
162435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
162535732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
162635732d01SWei Wang 	 * and exception table is indexed by a hash of
162735732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
162835732d01SWei Wang 	 * Otherwise, the exception table is indexed by
162935732d01SWei Wang 	 * a hash of only rt6i_dst.
163035732d01SWei Wang 	 */
163193c2fb25SDavid Ahern 	if (from->fib6_src.plen)
163235732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
163335732d01SWei Wang #endif
163435732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket,
163535732d01SWei Wang 					  &rt->rt6i_dst.addr,
163635732d01SWei Wang 					  src_key);
163735732d01SWei Wang 	if (rt6_ex)
163835732d01SWei Wang 		rt6_ex->stamp = jiffies;
163935732d01SWei Wang 
1640193f3685SPaolo Abeni unlock:
164135732d01SWei Wang 	rcu_read_unlock();
164235732d01SWei Wang }
164335732d01SWei Wang 
1644e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1645e9fa1495SStefano Brivio 					 struct rt6_info *rt, int mtu)
1646e9fa1495SStefano Brivio {
1647e9fa1495SStefano Brivio 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1648e9fa1495SStefano Brivio 	 * lowest MTU in the path: always allow updating the route PMTU to
1649e9fa1495SStefano Brivio 	 * reflect PMTU decreases.
1650e9fa1495SStefano Brivio 	 *
1651e9fa1495SStefano Brivio 	 * If the new MTU is higher, and the route PMTU is equal to the local
1652e9fa1495SStefano Brivio 	 * MTU, this means the old MTU is the lowest in the path, so allow
1653e9fa1495SStefano Brivio 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1654e9fa1495SStefano Brivio 	 * handle this.
1655e9fa1495SStefano Brivio 	 */
1656e9fa1495SStefano Brivio 
1657e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) >= mtu)
1658e9fa1495SStefano Brivio 		return true;
1659e9fa1495SStefano Brivio 
1660e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1661e9fa1495SStefano Brivio 		return true;
1662e9fa1495SStefano Brivio 
1663e9fa1495SStefano Brivio 	return false;
1664e9fa1495SStefano Brivio }
1665e9fa1495SStefano Brivio 
1666e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
16678d1c802bSDavid Ahern 				       struct fib6_info *rt, int mtu)
1668f5bbe7eeSWei Wang {
1669f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
1670f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
1671f5bbe7eeSWei Wang 	int i;
1672f5bbe7eeSWei Wang 
1673f5bbe7eeSWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1674f5bbe7eeSWei Wang 					lockdep_is_held(&rt6_exception_lock));
1675f5bbe7eeSWei Wang 
1676e9fa1495SStefano Brivio 	if (!bucket)
1677e9fa1495SStefano Brivio 		return;
1678e9fa1495SStefano Brivio 
1679f5bbe7eeSWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1680f5bbe7eeSWei Wang 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1681f5bbe7eeSWei Wang 			struct rt6_info *entry = rt6_ex->rt6i;
1682e9fa1495SStefano Brivio 
1683e9fa1495SStefano Brivio 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
1684d4ead6b3SDavid Ahern 			 * route), the metrics of its rt->from have already
1685f5bbe7eeSWei Wang 			 * been updated.
1686f5bbe7eeSWei Wang 			 */
1687d4ead6b3SDavid Ahern 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
1688e9fa1495SStefano Brivio 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
1689d4ead6b3SDavid Ahern 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
1690f5bbe7eeSWei Wang 		}
1691f5bbe7eeSWei Wang 		bucket++;
1692f5bbe7eeSWei Wang 	}
1693f5bbe7eeSWei Wang }
1694f5bbe7eeSWei Wang 
1695b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
1696b16cb459SWei Wang 
16978d1c802bSDavid Ahern static void rt6_exceptions_clean_tohost(struct fib6_info *rt,
1698b16cb459SWei Wang 					struct in6_addr *gateway)
1699b16cb459SWei Wang {
1700b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
1701b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
1702b16cb459SWei Wang 	struct hlist_node *tmp;
1703b16cb459SWei Wang 	int i;
1704b16cb459SWei Wang 
1705b16cb459SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1706b16cb459SWei Wang 		return;
1707b16cb459SWei Wang 
1708b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1709b16cb459SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1710b16cb459SWei Wang 				     lockdep_is_held(&rt6_exception_lock));
1711b16cb459SWei Wang 
1712b16cb459SWei Wang 	if (bucket) {
1713b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1714b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1715b16cb459SWei Wang 						  &bucket->chain, hlist) {
1716b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1717b16cb459SWei Wang 
1718b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1719b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
1720b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
1721b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
1722b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
1723b16cb459SWei Wang 				}
1724b16cb459SWei Wang 			}
1725b16cb459SWei Wang 			bucket++;
1726b16cb459SWei Wang 		}
1727b16cb459SWei Wang 	}
1728b16cb459SWei Wang 
1729b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1730b16cb459SWei Wang }
1731b16cb459SWei Wang 
1732c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1733c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
1734c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
1735c757faa8SWei Wang 				      unsigned long now)
1736c757faa8SWei Wang {
1737c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
1738c757faa8SWei Wang 
17391859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
17401859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
17411859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
17421859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
17431859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
17441859bac0SPaolo Abeni 	 */
174531afeb42SWei Wang 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
174631afeb42SWei Wang 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1747c757faa8SWei Wang 			RT6_TRACE("aging clone %p\n", rt);
1748c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1749c757faa8SWei Wang 			return;
175031afeb42SWei Wang 		}
175131afeb42SWei Wang 	} else if (time_after(jiffies, rt->dst.expires)) {
175231afeb42SWei Wang 		RT6_TRACE("purging expired route %p\n", rt);
175331afeb42SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
175431afeb42SWei Wang 		return;
175531afeb42SWei Wang 	}
175631afeb42SWei Wang 
175731afeb42SWei Wang 	if (rt->rt6i_flags & RTF_GATEWAY) {
1758c757faa8SWei Wang 		struct neighbour *neigh;
1759c757faa8SWei Wang 		__u8 neigh_flags = 0;
1760c757faa8SWei Wang 
17611bfa26ffSEric Dumazet 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
17621bfa26ffSEric Dumazet 		if (neigh)
1763c757faa8SWei Wang 			neigh_flags = neigh->flags;
17641bfa26ffSEric Dumazet 
1765c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
1766c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
1767c757faa8SWei Wang 				  rt);
1768c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1769c757faa8SWei Wang 			return;
1770c757faa8SWei Wang 		}
1771c757faa8SWei Wang 	}
177231afeb42SWei Wang 
1773c757faa8SWei Wang 	gc_args->more++;
1774c757faa8SWei Wang }
1775c757faa8SWei Wang 
17768d1c802bSDavid Ahern void rt6_age_exceptions(struct fib6_info *rt,
1777c757faa8SWei Wang 			struct fib6_gc_args *gc_args,
1778c757faa8SWei Wang 			unsigned long now)
1779c757faa8SWei Wang {
1780c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
1781c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
1782c757faa8SWei Wang 	struct hlist_node *tmp;
1783c757faa8SWei Wang 	int i;
1784c757faa8SWei Wang 
1785c757faa8SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1786c757faa8SWei Wang 		return;
1787c757faa8SWei Wang 
17881bfa26ffSEric Dumazet 	rcu_read_lock_bh();
17891bfa26ffSEric Dumazet 	spin_lock(&rt6_exception_lock);
1790c757faa8SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1791c757faa8SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
1792c757faa8SWei Wang 
1793c757faa8SWei Wang 	if (bucket) {
1794c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1795c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1796c757faa8SWei Wang 						  &bucket->chain, hlist) {
1797c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
1798c757faa8SWei Wang 							  gc_args, now);
1799c757faa8SWei Wang 			}
1800c757faa8SWei Wang 			bucket++;
1801c757faa8SWei Wang 		}
1802c757faa8SWei Wang 	}
18031bfa26ffSEric Dumazet 	spin_unlock(&rt6_exception_lock);
18041bfa26ffSEric Dumazet 	rcu_read_unlock_bh();
1805c757faa8SWei Wang }
1806c757faa8SWei Wang 
18071d053da9SDavid Ahern /* must be called with rcu lock held */
18081d053da9SDavid Ahern struct fib6_info *fib6_table_lookup(struct net *net, struct fib6_table *table,
18091d053da9SDavid Ahern 				    int oif, struct flowi6 *fl6, int strict)
18101da177e4SLinus Torvalds {
1811367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
18128d1c802bSDavid Ahern 	struct fib6_info *f6i;
18131da177e4SLinus Torvalds 
18146454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1815367efcb9SMartin KaFai Lau 	saved_fn = fn;
18161da177e4SLinus Torvalds 
1817ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1818ca254490SDavid Ahern 		oif = 0;
1819ca254490SDavid Ahern 
1820a3c00e46SMartin KaFai Lau redo_rt6_select:
182123fb93a4SDavid Ahern 	f6i = rt6_select(net, fn, oif, strict);
182223fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1823a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1824a3c00e46SMartin KaFai Lau 		if (fn)
1825a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
1826367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
1827367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
1828367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
1829367efcb9SMartin KaFai Lau 			fn = saved_fn;
1830367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
1831367efcb9SMartin KaFai Lau 		}
1832a3c00e46SMartin KaFai Lau 	}
1833a3c00e46SMartin KaFai Lau 
1834d4bea421SDavid Ahern 	trace_fib6_table_lookup(net, f6i, table, fl6);
1835d52d3997SMartin KaFai Lau 
18361d053da9SDavid Ahern 	return f6i;
18371d053da9SDavid Ahern }
18381d053da9SDavid Ahern 
18391d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
18401d053da9SDavid Ahern 			       int oif, struct flowi6 *fl6,
18411d053da9SDavid Ahern 			       const struct sk_buff *skb, int flags)
18421d053da9SDavid Ahern {
18431d053da9SDavid Ahern 	struct fib6_info *f6i;
18441d053da9SDavid Ahern 	struct rt6_info *rt;
18451d053da9SDavid Ahern 	int strict = 0;
18461d053da9SDavid Ahern 
18471d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IFACE;
18481d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
18491d053da9SDavid Ahern 	if (net->ipv6.devconf_all->forwarding == 0)
18501d053da9SDavid Ahern 		strict |= RT6_LOOKUP_F_REACHABLE;
18511d053da9SDavid Ahern 
18521d053da9SDavid Ahern 	rcu_read_lock();
18531d053da9SDavid Ahern 
18541d053da9SDavid Ahern 	f6i = fib6_table_lookup(net, table, oif, fl6, strict);
18551d053da9SDavid Ahern 	if (f6i->fib6_nsiblings)
18561d053da9SDavid Ahern 		f6i = fib6_multipath_select(net, f6i, fl6, oif, skb, strict);
18571d053da9SDavid Ahern 
185823fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1859421842edSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
186066f5d6ceSWei Wang 		rcu_read_unlock();
1861d3843fe5SWei Wang 		dst_hold(&rt->dst);
1862d3843fe5SWei Wang 		return rt;
1863d3843fe5SWei Wang 	}
186423fb93a4SDavid Ahern 
186523fb93a4SDavid Ahern 	/*Search through exception table */
186623fb93a4SDavid Ahern 	rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
186723fb93a4SDavid Ahern 	if (rt) {
1868d4ead6b3SDavid Ahern 		if (ip6_hold_safe(net, &rt, true))
18691da177e4SLinus Torvalds 			dst_use_noref(&rt->dst, jiffies);
1870d4ead6b3SDavid Ahern 
187166f5d6ceSWei Wang 		rcu_read_unlock();
1872d52d3997SMartin KaFai Lau 		return rt;
18733da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
187493c2fb25SDavid Ahern 			    !(f6i->fib6_flags & RTF_GATEWAY))) {
18753da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
18763da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
18773da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
18783da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
18793da59bd9SMartin KaFai Lau 		 */
18803da59bd9SMartin KaFai Lau 		struct rt6_info *uncached_rt;
18813da59bd9SMartin KaFai Lau 
188223fb93a4SDavid Ahern 		uncached_rt = ip6_rt_cache_alloc(f6i, &fl6->daddr, NULL);
1883d52d3997SMartin KaFai Lau 
18844d85cd0cSDavid Ahern 		rcu_read_unlock();
18853da59bd9SMartin KaFai Lau 
18861cfb71eeSWei Wang 		if (uncached_rt) {
18871cfb71eeSWei Wang 			/* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
18881cfb71eeSWei Wang 			 * No need for another dst_hold()
18891cfb71eeSWei Wang 			 */
18908d0b94afSMartin KaFai Lau 			rt6_uncached_list_add(uncached_rt);
189181eb8447SWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
18921cfb71eeSWei Wang 		} else {
18933da59bd9SMartin KaFai Lau 			uncached_rt = net->ipv6.ip6_null_entry;
18943da59bd9SMartin KaFai Lau 			dst_hold(&uncached_rt->dst);
18951cfb71eeSWei Wang 		}
1896b811580dSDavid Ahern 
18973da59bd9SMartin KaFai Lau 		return uncached_rt;
1898d52d3997SMartin KaFai Lau 	} else {
1899d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
1900d52d3997SMartin KaFai Lau 
1901d52d3997SMartin KaFai Lau 		struct rt6_info *pcpu_rt;
1902d52d3997SMartin KaFai Lau 
1903951f788aSEric Dumazet 		local_bh_disable();
190423fb93a4SDavid Ahern 		pcpu_rt = rt6_get_pcpu_route(f6i);
1905d52d3997SMartin KaFai Lau 
190693531c67SDavid Ahern 		if (!pcpu_rt)
190723fb93a4SDavid Ahern 			pcpu_rt = rt6_make_pcpu_route(net, f6i);
190893531c67SDavid Ahern 
1909951f788aSEric Dumazet 		local_bh_enable();
1910951f788aSEric Dumazet 		rcu_read_unlock();
1911d4bea421SDavid Ahern 
1912d52d3997SMartin KaFai Lau 		return pcpu_rt;
1913d52d3997SMartin KaFai Lau 	}
1914c71099acSThomas Graf }
19159ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
1916c71099acSThomas Graf 
1917b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net,
1918b75cc8f9SDavid Ahern 					    struct fib6_table *table,
1919b75cc8f9SDavid Ahern 					    struct flowi6 *fl6,
1920b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
1921b75cc8f9SDavid Ahern 					    int flags)
19224acad72dSPavel Emelyanov {
1923b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
19244acad72dSPavel Emelyanov }
19254acad72dSPavel Emelyanov 
1926d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
192772331bc0SShmulik Ladkani 					 struct net_device *dev,
1928b75cc8f9SDavid Ahern 					 struct flowi6 *fl6,
1929b75cc8f9SDavid Ahern 					 const struct sk_buff *skb,
1930b75cc8f9SDavid Ahern 					 int flags)
193172331bc0SShmulik Ladkani {
193272331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
193372331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
193472331bc0SShmulik Ladkani 
1935b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
193672331bc0SShmulik Ladkani }
1937d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
193872331bc0SShmulik Ladkani 
193923aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
19405e5d6fedSRoopa Prabhu 				  struct flow_keys *keys,
19415e5d6fedSRoopa Prabhu 				  struct flow_keys *flkeys)
194223aebdacSJakub Sitnicki {
194323aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
194423aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
19455e5d6fedSRoopa Prabhu 	struct flow_keys *_flkeys = flkeys;
194623aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
194723aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
194823aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
1949cea67a2dSEric Dumazet 	struct icmp6hdr _icmph;
195023aebdacSJakub Sitnicki 
195123aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
195223aebdacSJakub Sitnicki 		goto out;
195323aebdacSJakub Sitnicki 
1954cea67a2dSEric Dumazet 	icmph = skb_header_pointer(skb, skb_transport_offset(skb),
1955cea67a2dSEric Dumazet 				   sizeof(_icmph), &_icmph);
1956cea67a2dSEric Dumazet 	if (!icmph)
1957cea67a2dSEric Dumazet 		goto out;
1958cea67a2dSEric Dumazet 
195923aebdacSJakub Sitnicki 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
196023aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
196123aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
196223aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
196323aebdacSJakub Sitnicki 		goto out;
196423aebdacSJakub Sitnicki 
196523aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
196623aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
196723aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
196823aebdacSJakub Sitnicki 	if (!inner_iph)
196923aebdacSJakub Sitnicki 		goto out;
197023aebdacSJakub Sitnicki 
197123aebdacSJakub Sitnicki 	key_iph = inner_iph;
19725e5d6fedSRoopa Prabhu 	_flkeys = NULL;
197323aebdacSJakub Sitnicki out:
19745e5d6fedSRoopa Prabhu 	if (_flkeys) {
19755e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
19765e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
19775e5d6fedSRoopa Prabhu 		keys->tags.flow_label = _flkeys->tags.flow_label;
19785e5d6fedSRoopa Prabhu 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
19795e5d6fedSRoopa Prabhu 	} else {
198023aebdacSJakub Sitnicki 		keys->addrs.v6addrs.src = key_iph->saddr;
198123aebdacSJakub Sitnicki 		keys->addrs.v6addrs.dst = key_iph->daddr;
1982fa1be7e0SMichal Kubecek 		keys->tags.flow_label = ip6_flowlabel(key_iph);
198323aebdacSJakub Sitnicki 		keys->basic.ip_proto = key_iph->nexthdr;
198423aebdacSJakub Sitnicki 	}
19855e5d6fedSRoopa Prabhu }
198623aebdacSJakub Sitnicki 
198723aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
1988b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
1989b4bac172SDavid Ahern 		       const struct sk_buff *skb, struct flow_keys *flkeys)
199023aebdacSJakub Sitnicki {
199123aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
19929a2a537aSDavid Ahern 	u32 mhash;
199323aebdacSJakub Sitnicki 
1994bbfa047aSDavid S. Miller 	switch (ip6_multipath_hash_policy(net)) {
1995b4bac172SDavid Ahern 	case 0:
19966f74b6c2SDavid Ahern 		memset(&hash_keys, 0, sizeof(hash_keys));
19976f74b6c2SDavid Ahern 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
19989a2a537aSDavid Ahern 		if (skb) {
19995e5d6fedSRoopa Prabhu 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
20009a2a537aSDavid Ahern 		} else {
20019a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
20029a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2003fa1be7e0SMichal Kubecek 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
20049a2a537aSDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
200523aebdacSJakub Sitnicki 		}
2006b4bac172SDavid Ahern 		break;
2007b4bac172SDavid Ahern 	case 1:
2008b4bac172SDavid Ahern 		if (skb) {
2009b4bac172SDavid Ahern 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
2010b4bac172SDavid Ahern 			struct flow_keys keys;
2011b4bac172SDavid Ahern 
2012b4bac172SDavid Ahern 			/* short-circuit if we already have L4 hash present */
2013b4bac172SDavid Ahern 			if (skb->l4_hash)
2014b4bac172SDavid Ahern 				return skb_get_hash_raw(skb) >> 1;
2015b4bac172SDavid Ahern 
2016b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2017b4bac172SDavid Ahern 
2018b4bac172SDavid Ahern                         if (!flkeys) {
2019b4bac172SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2020b4bac172SDavid Ahern 				flkeys = &keys;
2021b4bac172SDavid Ahern 			}
2022b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2023b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2024b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2025b4bac172SDavid Ahern 			hash_keys.ports.src = flkeys->ports.src;
2026b4bac172SDavid Ahern 			hash_keys.ports.dst = flkeys->ports.dst;
2027b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2028b4bac172SDavid Ahern 		} else {
2029b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2030b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2031b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2032b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2033b4bac172SDavid Ahern 			hash_keys.ports.src = fl6->fl6_sport;
2034b4bac172SDavid Ahern 			hash_keys.ports.dst = fl6->fl6_dport;
2035b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2036b4bac172SDavid Ahern 		}
2037b4bac172SDavid Ahern 		break;
2038b4bac172SDavid Ahern 	}
20399a2a537aSDavid Ahern 	mhash = flow_hash_from_keys(&hash_keys);
204023aebdacSJakub Sitnicki 
20419a2a537aSDavid Ahern 	return mhash >> 1;
204223aebdacSJakub Sitnicki }
204323aebdacSJakub Sitnicki 
2044c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
2045c71099acSThomas Graf {
2046b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2047c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
2048adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2049904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
20504c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
2051e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
20524c9483b2SDavid S. Miller 		.daddr = iph->daddr,
20534c9483b2SDavid S. Miller 		.saddr = iph->saddr,
20546502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
20554c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
20564c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
2057c71099acSThomas Graf 	};
20585e5d6fedSRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2059adaa70bbSThomas Graf 
2060904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
206146fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2062904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
20635e5d6fedSRoopa Prabhu 
20645e5d6fedSRoopa Prabhu 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
20655e5d6fedSRoopa Prabhu 		flkeys = &_flkeys;
20665e5d6fedSRoopa Prabhu 
206723aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2068b4bac172SDavid Ahern 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
206906e9d040SJiri Benc 	skb_dst_drop(skb);
2070b75cc8f9SDavid Ahern 	skb_dst_set(skb,
2071b75cc8f9SDavid Ahern 		    ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
2072c71099acSThomas Graf }
2073c71099acSThomas Graf 
2074b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net,
2075b75cc8f9SDavid Ahern 					     struct fib6_table *table,
2076b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
2077b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2078b75cc8f9SDavid Ahern 					     int flags)
2079c71099acSThomas Graf {
2080b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2081c71099acSThomas Graf }
2082c71099acSThomas Graf 
20836f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
20846f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags)
2085c71099acSThomas Graf {
2086d46a9d67SDavid Ahern 	bool any_src;
2087c71099acSThomas Graf 
20883ede0bbcSRobert Shearman 	if (ipv6_addr_type(&fl6->daddr) &
20893ede0bbcSRobert Shearman 	    (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
20904c1feac5SDavid Ahern 		struct dst_entry *dst;
20914c1feac5SDavid Ahern 
20924c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
2093ca254490SDavid Ahern 		if (dst)
2094ca254490SDavid Ahern 			return dst;
20954c1feac5SDavid Ahern 	}
2096ca254490SDavid Ahern 
20971fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
20984dc27d1cSDavid McCullough 
2099d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
2100741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2101d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
210277d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
2103c71099acSThomas Graf 
2104d46a9d67SDavid Ahern 	if (!any_src)
2105adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
21060c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
21070c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2108adaa70bbSThomas Graf 
2109b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
21101da177e4SLinus Torvalds }
21116f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
21121da177e4SLinus Torvalds 
21132774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
211414e50e57SDavid S. Miller {
21155c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
21161dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
211714e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
211814e50e57SDavid S. Miller 
21191dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
212062cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
212114e50e57SDavid S. Miller 	if (rt) {
21220a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
212381eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
21240a1f5962SMartin KaFai Lau 
2125d8d1f30bSChangli Gao 		new = &rt->dst;
212614e50e57SDavid S. Miller 		new->__use = 1;
2127352e512cSHerbert Xu 		new->input = dst_discard;
2128ede2059dSEric W. Biederman 		new->output = dst_discard_out;
212914e50e57SDavid S. Miller 
2130defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
213114e50e57SDavid S. Miller 
21321dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
21334e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
21340a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
213514e50e57SDavid S. Miller 
213614e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
213714e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
213814e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
213914e50e57SDavid S. Miller #endif
214014e50e57SDavid S. Miller 	}
214114e50e57SDavid S. Miller 
214269ead7afSDavid S. Miller 	dst_release(dst_orig);
214369ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
214414e50e57SDavid S. Miller }
214514e50e57SDavid S. Miller 
21461da177e4SLinus Torvalds /*
21471da177e4SLinus Torvalds  *	Destination cache support functions
21481da177e4SLinus Torvalds  */
21491da177e4SLinus Torvalds 
21508d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie)
21513da59bd9SMartin KaFai Lau {
215236143645SSteffen Klassert 	u32 rt_cookie = 0;
2153c5cff856SWei Wang 
21548ae86971SDavid Ahern 	if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
215593531c67SDavid Ahern 		return false;
215693531c67SDavid Ahern 
215793531c67SDavid Ahern 	if (fib6_check_expired(f6i))
215893531c67SDavid Ahern 		return false;
215993531c67SDavid Ahern 
216093531c67SDavid Ahern 	return true;
216193531c67SDavid Ahern }
216293531c67SDavid Ahern 
2163a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt,
2164a68886a6SDavid Ahern 				   struct fib6_info *from,
2165a68886a6SDavid Ahern 				   u32 cookie)
21663da59bd9SMartin KaFai Lau {
2167c5cff856SWei Wang 	u32 rt_cookie = 0;
2168c5cff856SWei Wang 
2169a68886a6SDavid Ahern 	if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
217093531c67SDavid Ahern 	    rt_cookie != cookie)
21713da59bd9SMartin KaFai Lau 		return NULL;
21723da59bd9SMartin KaFai Lau 
21733da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
21743da59bd9SMartin KaFai Lau 		return NULL;
21753da59bd9SMartin KaFai Lau 
21763da59bd9SMartin KaFai Lau 	return &rt->dst;
21773da59bd9SMartin KaFai Lau }
21783da59bd9SMartin KaFai Lau 
2179a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2180a68886a6SDavid Ahern 					    struct fib6_info *from,
2181a68886a6SDavid Ahern 					    u32 cookie)
21823da59bd9SMartin KaFai Lau {
21835973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
21845973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2185a68886a6SDavid Ahern 	    fib6_check(from, cookie))
21863da59bd9SMartin KaFai Lau 		return &rt->dst;
21873da59bd9SMartin KaFai Lau 	else
21883da59bd9SMartin KaFai Lau 		return NULL;
21893da59bd9SMartin KaFai Lau }
21903da59bd9SMartin KaFai Lau 
21911da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
21921da177e4SLinus Torvalds {
2193a87b7dc9SDavid Ahern 	struct dst_entry *dst_ret;
2194a68886a6SDavid Ahern 	struct fib6_info *from;
21951da177e4SLinus Torvalds 	struct rt6_info *rt;
21961da177e4SLinus Torvalds 
2197a87b7dc9SDavid Ahern 	rt = container_of(dst, struct rt6_info, dst);
2198a87b7dc9SDavid Ahern 
2199a87b7dc9SDavid Ahern 	rcu_read_lock();
22001da177e4SLinus Torvalds 
22016f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
22026f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
22036f3118b5SNicolas Dichtel 	 * into this function always.
22046f3118b5SNicolas Dichtel 	 */
2205e3bc10bdSHannes Frederic Sowa 
2206a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
22074b32b5adSMartin KaFai Lau 
2208a68886a6SDavid Ahern 	if (from && (rt->rt6i_flags & RTF_PCPU ||
2209a68886a6SDavid Ahern 	    unlikely(!list_empty(&rt->rt6i_uncached))))
2210a68886a6SDavid Ahern 		dst_ret = rt6_dst_from_check(rt, from, cookie);
22113da59bd9SMartin KaFai Lau 	else
2212a68886a6SDavid Ahern 		dst_ret = rt6_check(rt, from, cookie);
2213a87b7dc9SDavid Ahern 
2214a87b7dc9SDavid Ahern 	rcu_read_unlock();
2215a87b7dc9SDavid Ahern 
2216a87b7dc9SDavid Ahern 	return dst_ret;
22171da177e4SLinus Torvalds }
22181da177e4SLinus Torvalds 
22191da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
22201da177e4SLinus Torvalds {
22211da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
22221da177e4SLinus Torvalds 
22231da177e4SLinus Torvalds 	if (rt) {
222454c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
2225c3c14da0SDavid Ahern 			rcu_read_lock();
222654c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
222793531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
222854c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
22291da177e4SLinus Torvalds 			}
2230c3c14da0SDavid Ahern 			rcu_read_unlock();
223154c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
223254c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
223354c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
223454c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
223554c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
223654c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
22371da177e4SLinus Torvalds }
22381da177e4SLinus Torvalds 
22391da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
22401da177e4SLinus Torvalds {
22411da177e4SLinus Torvalds 	struct rt6_info *rt;
22421da177e4SLinus Torvalds 
22433ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
22441da177e4SLinus Torvalds 
2245adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
22461da177e4SLinus Torvalds 	if (rt) {
22478a14e46fSDavid Ahern 		rcu_read_lock();
22481eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
224993531c67SDavid Ahern 			rt6_remove_exception_rt(rt);
2250c5cff856SWei Wang 		} else {
2251a68886a6SDavid Ahern 			struct fib6_info *from;
2252c5cff856SWei Wang 			struct fib6_node *fn;
2253c5cff856SWei Wang 
2254a68886a6SDavid Ahern 			from = rcu_dereference(rt->from);
2255a68886a6SDavid Ahern 			if (from) {
2256a68886a6SDavid Ahern 				fn = rcu_dereference(from->fib6_node);
2257c5cff856SWei Wang 				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2258c5cff856SWei Wang 					fn->fn_sernum = -1;
2259a68886a6SDavid Ahern 			}
22601da177e4SLinus Torvalds 		}
22611da177e4SLinus Torvalds 		rcu_read_unlock();
22621da177e4SLinus Torvalds 	}
22631da177e4SLinus Torvalds }
22641da177e4SLinus Torvalds 
22656a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout)
22666a3e030fSDavid Ahern {
2267a68886a6SDavid Ahern 	if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2268a68886a6SDavid Ahern 		struct fib6_info *from;
2269a68886a6SDavid Ahern 
2270a68886a6SDavid Ahern 		rcu_read_lock();
2271a68886a6SDavid Ahern 		from = rcu_dereference(rt0->from);
2272a68886a6SDavid Ahern 		if (from)
2273a68886a6SDavid Ahern 			rt0->dst.expires = from->expires;
2274a68886a6SDavid Ahern 		rcu_read_unlock();
2275a68886a6SDavid Ahern 	}
22766a3e030fSDavid Ahern 
22776a3e030fSDavid Ahern 	dst_set_expires(&rt0->dst, timeout);
22786a3e030fSDavid Ahern 	rt0->rt6i_flags |= RTF_EXPIRES;
22796700c270SDavid S. Miller }
22801da177e4SLinus Torvalds 
228145e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
228245e4fd26SMartin KaFai Lau {
228345e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
228445e4fd26SMartin KaFai Lau 
2285d4ead6b3SDavid Ahern 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
228645e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
228745e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
228845e4fd26SMartin KaFai Lau }
228945e4fd26SMartin KaFai Lau 
22900d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
22910d3f6d29SMartin KaFai Lau {
2292a68886a6SDavid Ahern 	bool from_set;
2293a68886a6SDavid Ahern 
2294a68886a6SDavid Ahern 	rcu_read_lock();
2295a68886a6SDavid Ahern 	from_set = !!rcu_dereference(rt->from);
2296a68886a6SDavid Ahern 	rcu_read_unlock();
2297a68886a6SDavid Ahern 
22980d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
2299a68886a6SDavid Ahern 		(rt->rt6i_flags & RTF_PCPU || from_set);
23000d3f6d29SMartin KaFai Lau }
23010d3f6d29SMartin KaFai Lau 
230245e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
230345e4fd26SMartin KaFai Lau 				 const struct ipv6hdr *iph, u32 mtu)
23041da177e4SLinus Torvalds {
23050dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
23061da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
23071da177e4SLinus Torvalds 
230819bda36cSXin Long 	if (dst_metric_locked(dst, RTAX_MTU))
230919bda36cSXin Long 		return;
231019bda36cSXin Long 
231145e4fd26SMartin KaFai Lau 	if (iph) {
231245e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
231345e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
231445e4fd26SMartin KaFai Lau 	} else if (sk) {
231545e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
231645e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
231745e4fd26SMartin KaFai Lau 	} else {
23180dec879fSJulian Anastasov 		daddr = NULL;
23190dec879fSJulian Anastasov 		saddr = NULL;
23201da177e4SLinus Torvalds 	}
23210dec879fSJulian Anastasov 	dst_confirm_neigh(dst, daddr);
23220dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
23230dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
23240dec879fSJulian Anastasov 		return;
23250dec879fSJulian Anastasov 
23260dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
23270dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
23282b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
23292b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
23302b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
23310dec879fSJulian Anastasov 	} else if (daddr) {
2332a68886a6SDavid Ahern 		struct fib6_info *from;
23330dec879fSJulian Anastasov 		struct rt6_info *nrt6;
23340dec879fSJulian Anastasov 
23354d85cd0cSDavid Ahern 		rcu_read_lock();
2336a68886a6SDavid Ahern 		from = rcu_dereference(rt6->from);
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 	}
418586872cb5SThomas Graf 
418686872cb5SThomas Graf 	if (tb[RTA_DST]) {
418786872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
418886872cb5SThomas Graf 
418986872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
419086872cb5SThomas Graf 			goto errout;
419186872cb5SThomas Graf 
419286872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
41931da177e4SLinus Torvalds 	}
419486872cb5SThomas Graf 
419586872cb5SThomas Graf 	if (tb[RTA_SRC]) {
419686872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
419786872cb5SThomas Graf 
419886872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
419986872cb5SThomas Graf 			goto errout;
420086872cb5SThomas Graf 
420186872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
42021da177e4SLinus Torvalds 	}
420386872cb5SThomas Graf 
4204c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
420567b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4206c3968a85SDaniel Walter 
420786872cb5SThomas Graf 	if (tb[RTA_OIF])
420886872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
420986872cb5SThomas Graf 
421086872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
421186872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
421286872cb5SThomas Graf 
421386872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
421486872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
421586872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
42161da177e4SLinus Torvalds 	}
421786872cb5SThomas Graf 
421886872cb5SThomas Graf 	if (tb[RTA_TABLE])
421986872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
422086872cb5SThomas Graf 
422151ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
422251ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
422351ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
42249ed59592SDavid Ahern 
42259ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4226c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
42279ed59592SDavid Ahern 		if (err < 0)
42289ed59592SDavid Ahern 			goto errout;
422951ebd318SNicolas Dichtel 	}
423051ebd318SNicolas Dichtel 
4231c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
4232c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
4233c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
4234c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
4235c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
4236c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
4237c78ba6d6SLubomir Rintel 	}
4238c78ba6d6SLubomir Rintel 
423919e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
424019e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
424119e42e45SRoopa Prabhu 
42429ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
424319e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
424419e42e45SRoopa Prabhu 
4245c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
42469ed59592SDavid Ahern 		if (err < 0)
42479ed59592SDavid Ahern 			goto errout;
42489ed59592SDavid Ahern 	}
42499ed59592SDavid Ahern 
425032bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
425132bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
425232bc201eSXin Long 
425332bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
425432bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
425532bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
425632bc201eSXin Long 		}
425732bc201eSXin Long 	}
425832bc201eSXin Long 
425986872cb5SThomas Graf 	err = 0;
426086872cb5SThomas Graf errout:
426186872cb5SThomas Graf 	return err;
42621da177e4SLinus Torvalds }
42631da177e4SLinus Torvalds 
42646b9ea5a6SRoopa Prabhu struct rt6_nh {
42658d1c802bSDavid Ahern 	struct fib6_info *fib6_info;
42666b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
42676b9ea5a6SRoopa Prabhu 	struct list_head next;
42686b9ea5a6SRoopa Prabhu };
42696b9ea5a6SRoopa Prabhu 
4270d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net,
4271d4ead6b3SDavid Ahern 				 struct list_head *rt6_nh_list,
42728d1c802bSDavid Ahern 				 struct fib6_info *rt,
42738d1c802bSDavid Ahern 				 struct fib6_config *r_cfg)
42746b9ea5a6SRoopa Prabhu {
42756b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
42766b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
42776b9ea5a6SRoopa Prabhu 
42786b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
42798d1c802bSDavid Ahern 		/* check if fib6_info already exists */
42808d1c802bSDavid Ahern 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
42816b9ea5a6SRoopa Prabhu 			return err;
42826b9ea5a6SRoopa Prabhu 	}
42836b9ea5a6SRoopa Prabhu 
42846b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
42856b9ea5a6SRoopa Prabhu 	if (!nh)
42866b9ea5a6SRoopa Prabhu 		return -ENOMEM;
42878d1c802bSDavid Ahern 	nh->fib6_info = rt;
42886b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
42896b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
42906b9ea5a6SRoopa Prabhu 
42916b9ea5a6SRoopa Prabhu 	return 0;
42926b9ea5a6SRoopa Prabhu }
42936b9ea5a6SRoopa Prabhu 
42948d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt,
42958d1c802bSDavid Ahern 				   struct fib6_info *rt_last,
42963b1137feSDavid Ahern 				   struct nl_info *info,
42973b1137feSDavid Ahern 				   __u16 nlflags)
42983b1137feSDavid Ahern {
42993b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
43003b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
43013b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
43023b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
43033b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
43043b1137feSDavid Ahern 	 */
430593c2fb25SDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
430693c2fb25SDavid Ahern 		rt = list_first_entry(&rt_last->fib6_siblings,
43078d1c802bSDavid Ahern 				      struct fib6_info,
430893c2fb25SDavid Ahern 				      fib6_siblings);
43093b1137feSDavid Ahern 	}
43103b1137feSDavid Ahern 
43113b1137feSDavid Ahern 	if (rt)
43123b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
43133b1137feSDavid Ahern }
43143b1137feSDavid Ahern 
4315333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
4316333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
431751ebd318SNicolas Dichtel {
43188d1c802bSDavid Ahern 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
43193b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
432051ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
432151ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
43228d1c802bSDavid Ahern 	struct fib6_info *rt;
43236b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
43246b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
43253b1137feSDavid Ahern 	__u16 nlflags;
432651ebd318SNicolas Dichtel 	int remaining;
432751ebd318SNicolas Dichtel 	int attrlen;
43286b9ea5a6SRoopa Prabhu 	int err = 1;
43296b9ea5a6SRoopa Prabhu 	int nhn = 0;
43306b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
43316b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
43326b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
433351ebd318SNicolas Dichtel 
43343b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
43353b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
43363b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
43373b1137feSDavid Ahern 
433835f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
433951ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
434051ebd318SNicolas Dichtel 
43416b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
43428d1c802bSDavid Ahern 	 * fib6_info structs per nexthop
43436b9ea5a6SRoopa Prabhu 	 */
434451ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
434551ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
434651ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
434751ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
434851ebd318SNicolas Dichtel 
434951ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
435051ebd318SNicolas Dichtel 		if (attrlen > 0) {
435151ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
435251ebd318SNicolas Dichtel 
435351ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
435451ebd318SNicolas Dichtel 			if (nla) {
435567b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
435651ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
435751ebd318SNicolas Dichtel 			}
435819e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
435919e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
436019e42e45SRoopa Prabhu 			if (nla)
436119e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
436251ebd318SNicolas Dichtel 		}
43636b9ea5a6SRoopa Prabhu 
436468e2ffdeSDavid Ahern 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
4365acb54e3cSDavid Ahern 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
43668c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
43678c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
43688c5b83f0SRoopa Prabhu 			rt = NULL;
43696b9ea5a6SRoopa Prabhu 			goto cleanup;
43708c5b83f0SRoopa Prabhu 		}
4371b5d2d75eSDavid Ahern 		if (!rt6_qualify_for_ecmp(rt)) {
4372b5d2d75eSDavid Ahern 			err = -EINVAL;
4373b5d2d75eSDavid Ahern 			NL_SET_ERR_MSG(extack,
4374b5d2d75eSDavid Ahern 				       "Device only routes can not be added for IPv6 using the multipath API.");
4375b5d2d75eSDavid Ahern 			fib6_info_release(rt);
4376b5d2d75eSDavid Ahern 			goto cleanup;
4377b5d2d75eSDavid Ahern 		}
43786b9ea5a6SRoopa Prabhu 
43795e670d84SDavid Ahern 		rt->fib6_nh.nh_weight = rtnh->rtnh_hops + 1;
4380398958aeSIdo Schimmel 
4381d4ead6b3SDavid Ahern 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4382d4ead6b3SDavid Ahern 					    rt, &r_cfg);
438351ebd318SNicolas Dichtel 		if (err) {
438493531c67SDavid Ahern 			fib6_info_release(rt);
43856b9ea5a6SRoopa Prabhu 			goto cleanup;
438651ebd318SNicolas Dichtel 		}
43876b9ea5a6SRoopa Prabhu 
43886b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
438951ebd318SNicolas Dichtel 	}
43906b9ea5a6SRoopa Prabhu 
43913b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
43923b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
43933b1137feSDavid Ahern 	 * the full route when done
43943b1137feSDavid Ahern 	 */
43953b1137feSDavid Ahern 	info->skip_notify = 1;
43963b1137feSDavid Ahern 
43976b9ea5a6SRoopa Prabhu 	err_nh = NULL;
43986b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
43998d1c802bSDavid Ahern 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
44008d1c802bSDavid Ahern 		fib6_info_release(nh->fib6_info);
44013b1137feSDavid Ahern 
4402f7225172SDavid Ahern 		if (!err) {
4403f7225172SDavid Ahern 			/* save reference to last route successfully inserted */
4404f7225172SDavid Ahern 			rt_last = nh->fib6_info;
4405f7225172SDavid Ahern 
44066b9ea5a6SRoopa Prabhu 			/* save reference to first route for notification */
4407f7225172SDavid Ahern 			if (!rt_notif)
44088d1c802bSDavid Ahern 				rt_notif = nh->fib6_info;
4409f7225172SDavid Ahern 		}
44106b9ea5a6SRoopa Prabhu 
44118d1c802bSDavid Ahern 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
44128d1c802bSDavid Ahern 		nh->fib6_info = NULL;
44136b9ea5a6SRoopa Prabhu 		if (err) {
44146b9ea5a6SRoopa Prabhu 			if (replace && nhn)
4415a5a82d84SJakub Kicinski 				NL_SET_ERR_MSG_MOD(extack,
4416a5a82d84SJakub Kicinski 						   "multipath route replace failed (check consistency of installed routes)");
44176b9ea5a6SRoopa Prabhu 			err_nh = nh;
44186b9ea5a6SRoopa Prabhu 			goto add_errout;
44196b9ea5a6SRoopa Prabhu 		}
44206b9ea5a6SRoopa Prabhu 
44211a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
442227596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
442327596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
442427596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
442527596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
442627596472SMichal Kubeček 		 * be added to it.
44271a72418bSNicolas Dichtel 		 */
442827596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
442927596472SMichal Kubeček 						     NLM_F_REPLACE);
44306b9ea5a6SRoopa Prabhu 		nhn++;
44316b9ea5a6SRoopa Prabhu 	}
44326b9ea5a6SRoopa Prabhu 
44333b1137feSDavid Ahern 	/* success ... tell user about new route */
44343b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
44356b9ea5a6SRoopa Prabhu 	goto cleanup;
44366b9ea5a6SRoopa Prabhu 
44376b9ea5a6SRoopa Prabhu add_errout:
44383b1137feSDavid Ahern 	/* send notification for routes that were added so that
44393b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
44403b1137feSDavid Ahern 	 * coherent
44413b1137feSDavid Ahern 	 */
44423b1137feSDavid Ahern 	if (rt_notif)
44433b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
44443b1137feSDavid Ahern 
44456b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
44466b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
44476b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
44486b9ea5a6SRoopa Prabhu 			break;
4449333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
44506b9ea5a6SRoopa Prabhu 	}
44516b9ea5a6SRoopa Prabhu 
44526b9ea5a6SRoopa Prabhu cleanup:
44536b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
44548d1c802bSDavid Ahern 		if (nh->fib6_info)
44558d1c802bSDavid Ahern 			fib6_info_release(nh->fib6_info);
44566b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
44576b9ea5a6SRoopa Prabhu 		kfree(nh);
44586b9ea5a6SRoopa Prabhu 	}
44596b9ea5a6SRoopa Prabhu 
44606b9ea5a6SRoopa Prabhu 	return err;
44616b9ea5a6SRoopa Prabhu }
44626b9ea5a6SRoopa Prabhu 
4463333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
4464333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
44656b9ea5a6SRoopa Prabhu {
44666b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
44676b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
44686b9ea5a6SRoopa Prabhu 	int remaining;
44696b9ea5a6SRoopa Prabhu 	int attrlen;
44706b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
44716b9ea5a6SRoopa Prabhu 
44726b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
44736b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
44746b9ea5a6SRoopa Prabhu 
44756b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
44766b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
44776b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
44786b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
44796b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
44806b9ea5a6SRoopa Prabhu 
44816b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
44826b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
44836b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
44846b9ea5a6SRoopa Prabhu 
44856b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
44866b9ea5a6SRoopa Prabhu 			if (nla) {
44876b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
44886b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
44896b9ea5a6SRoopa Prabhu 			}
44906b9ea5a6SRoopa Prabhu 		}
4491333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
44926b9ea5a6SRoopa Prabhu 		if (err)
44936b9ea5a6SRoopa Prabhu 			last_err = err;
44946b9ea5a6SRoopa Prabhu 
449551ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
449651ebd318SNicolas Dichtel 	}
449751ebd318SNicolas Dichtel 
449851ebd318SNicolas Dichtel 	return last_err;
449951ebd318SNicolas Dichtel }
450051ebd318SNicolas Dichtel 
4501c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4502c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
45031da177e4SLinus Torvalds {
450486872cb5SThomas Graf 	struct fib6_config cfg;
450586872cb5SThomas Graf 	int err;
45061da177e4SLinus Torvalds 
4507333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
450886872cb5SThomas Graf 	if (err < 0)
450986872cb5SThomas Graf 		return err;
451086872cb5SThomas Graf 
451151ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4512333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
45130ae81335SDavid Ahern 	else {
45140ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
4515333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
45161da177e4SLinus Torvalds 	}
45170ae81335SDavid Ahern }
45181da177e4SLinus Torvalds 
4519c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4520c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
45211da177e4SLinus Torvalds {
452286872cb5SThomas Graf 	struct fib6_config cfg;
452386872cb5SThomas Graf 	int err;
45241da177e4SLinus Torvalds 
4525333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
452686872cb5SThomas Graf 	if (err < 0)
452786872cb5SThomas Graf 		return err;
452886872cb5SThomas Graf 
452951ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4530333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
453151ebd318SNicolas Dichtel 	else
4532acb54e3cSDavid Ahern 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
45331da177e4SLinus Torvalds }
45341da177e4SLinus Torvalds 
45358d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt)
4536339bf98fSThomas Graf {
4537beb1afacSDavid Ahern 	int nexthop_len = 0;
4538beb1afacSDavid Ahern 
453993c2fb25SDavid Ahern 	if (rt->fib6_nsiblings) {
4540beb1afacSDavid Ahern 		nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
4541beb1afacSDavid Ahern 			    + NLA_ALIGN(sizeof(struct rtnexthop))
4542beb1afacSDavid Ahern 			    + nla_total_size(16) /* RTA_GATEWAY */
45435e670d84SDavid Ahern 			    + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate);
4544beb1afacSDavid Ahern 
454593c2fb25SDavid Ahern 		nexthop_len *= rt->fib6_nsiblings;
4546beb1afacSDavid Ahern 	}
4547beb1afacSDavid Ahern 
4548339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
4549339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
4550339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
4551339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
4552339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
4553339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
4554339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
4555339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
4556339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
45576a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
4558ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
4559c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
456019e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
45615e670d84SDavid Ahern 	       + lwtunnel_get_encap_size(rt->fib6_nh.nh_lwtstate)
4562beb1afacSDavid Ahern 	       + nexthop_len;
4563beb1afacSDavid Ahern }
4564beb1afacSDavid Ahern 
45658d1c802bSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct fib6_info *rt,
45665be083ceSDavid Ahern 			    unsigned int *flags, bool skip_oif)
4567beb1afacSDavid Ahern {
45685e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_DEAD)
4569f9d882eaSIdo Schimmel 		*flags |= RTNH_F_DEAD;
4570f9d882eaSIdo Schimmel 
45715e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_LINKDOWN) {
4572beb1afacSDavid Ahern 		*flags |= RTNH_F_LINKDOWN;
4573dcd1f572SDavid Ahern 
4574dcd1f572SDavid Ahern 		rcu_read_lock();
4575dcd1f572SDavid Ahern 		if (fib6_ignore_linkdown(rt))
4576beb1afacSDavid Ahern 			*flags |= RTNH_F_DEAD;
4577dcd1f572SDavid Ahern 		rcu_read_unlock();
4578beb1afacSDavid Ahern 	}
4579beb1afacSDavid Ahern 
458093c2fb25SDavid Ahern 	if (rt->fib6_flags & RTF_GATEWAY) {
45815e670d84SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->fib6_nh.nh_gw) < 0)
4582beb1afacSDavid Ahern 			goto nla_put_failure;
4583beb1afacSDavid Ahern 	}
4584beb1afacSDavid Ahern 
45855e670d84SDavid Ahern 	*flags |= (rt->fib6_nh.nh_flags & RTNH_F_ONLINK);
45865e670d84SDavid Ahern 	if (rt->fib6_nh.nh_flags & RTNH_F_OFFLOAD)
458761e4d01eSIdo Schimmel 		*flags |= RTNH_F_OFFLOAD;
458861e4d01eSIdo Schimmel 
45895be083ceSDavid Ahern 	/* not needed for multipath encoding b/c it has a rtnexthop struct */
45905e670d84SDavid Ahern 	if (!skip_oif && rt->fib6_nh.nh_dev &&
45915e670d84SDavid Ahern 	    nla_put_u32(skb, RTA_OIF, rt->fib6_nh.nh_dev->ifindex))
4592beb1afacSDavid Ahern 		goto nla_put_failure;
4593beb1afacSDavid Ahern 
45945e670d84SDavid Ahern 	if (rt->fib6_nh.nh_lwtstate &&
45955e670d84SDavid Ahern 	    lwtunnel_fill_encap(skb, rt->fib6_nh.nh_lwtstate) < 0)
4596beb1afacSDavid Ahern 		goto nla_put_failure;
4597beb1afacSDavid Ahern 
4598beb1afacSDavid Ahern 	return 0;
4599beb1afacSDavid Ahern 
4600beb1afacSDavid Ahern nla_put_failure:
4601beb1afacSDavid Ahern 	return -EMSGSIZE;
4602beb1afacSDavid Ahern }
4603beb1afacSDavid Ahern 
46045be083ceSDavid Ahern /* add multipath next hop */
46058d1c802bSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct fib6_info *rt)
4606beb1afacSDavid Ahern {
46075e670d84SDavid Ahern 	const struct net_device *dev = rt->fib6_nh.nh_dev;
4608beb1afacSDavid Ahern 	struct rtnexthop *rtnh;
4609beb1afacSDavid Ahern 	unsigned int flags = 0;
4610beb1afacSDavid Ahern 
4611beb1afacSDavid Ahern 	rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
4612beb1afacSDavid Ahern 	if (!rtnh)
4613beb1afacSDavid Ahern 		goto nla_put_failure;
4614beb1afacSDavid Ahern 
46155e670d84SDavid Ahern 	rtnh->rtnh_hops = rt->fib6_nh.nh_weight - 1;
46165e670d84SDavid Ahern 	rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
4617beb1afacSDavid Ahern 
46185be083ceSDavid Ahern 	if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
4619beb1afacSDavid Ahern 		goto nla_put_failure;
4620beb1afacSDavid Ahern 
4621beb1afacSDavid Ahern 	rtnh->rtnh_flags = flags;
4622beb1afacSDavid Ahern 
4623beb1afacSDavid Ahern 	/* length of rtnetlink header + attributes */
4624beb1afacSDavid Ahern 	rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
4625beb1afacSDavid Ahern 
4626beb1afacSDavid Ahern 	return 0;
4627beb1afacSDavid Ahern 
4628beb1afacSDavid Ahern nla_put_failure:
4629beb1afacSDavid Ahern 	return -EMSGSIZE;
4630339bf98fSThomas Graf }
4631339bf98fSThomas Graf 
4632d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
46338d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
4634d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
463515e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
4636f8cfe2ceSDavid Ahern 			 unsigned int flags)
46371da177e4SLinus Torvalds {
463822d0bd82SXin Long 	struct rt6_info *rt6 = (struct rt6_info *)dst;
463922d0bd82SXin Long 	struct rt6key *rt6_dst, *rt6_src;
464022d0bd82SXin Long 	u32 *pmetrics, table, rt6_flags;
46411da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
464222d0bd82SXin Long 	struct rtmsg *rtm;
4643d4ead6b3SDavid Ahern 	long expires = 0;
46441da177e4SLinus Torvalds 
464515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
464638308473SDavid S. Miller 	if (!nlh)
464726932566SPatrick McHardy 		return -EMSGSIZE;
46482d7202bfSThomas Graf 
464922d0bd82SXin Long 	if (rt6) {
465022d0bd82SXin Long 		rt6_dst = &rt6->rt6i_dst;
465122d0bd82SXin Long 		rt6_src = &rt6->rt6i_src;
465222d0bd82SXin Long 		rt6_flags = rt6->rt6i_flags;
465322d0bd82SXin Long 	} else {
465422d0bd82SXin Long 		rt6_dst = &rt->fib6_dst;
465522d0bd82SXin Long 		rt6_src = &rt->fib6_src;
465622d0bd82SXin Long 		rt6_flags = rt->fib6_flags;
465722d0bd82SXin Long 	}
465822d0bd82SXin Long 
46592d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
46601da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
466122d0bd82SXin Long 	rtm->rtm_dst_len = rt6_dst->plen;
466222d0bd82SXin Long 	rtm->rtm_src_len = rt6_src->plen;
46631da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
466493c2fb25SDavid Ahern 	if (rt->fib6_table)
466593c2fb25SDavid Ahern 		table = rt->fib6_table->tb6_id;
4666c71099acSThomas Graf 	else
46679e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
4668*97f0082aSKalash Nainwal 	rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
4669c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
4670c78679e8SDavid S. Miller 		goto nla_put_failure;
4671e8478e80SDavid Ahern 
4672e8478e80SDavid Ahern 	rtm->rtm_type = rt->fib6_type;
46731da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
46741da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
467593c2fb25SDavid Ahern 	rtm->rtm_protocol = rt->fib6_protocol;
46761da177e4SLinus Torvalds 
467722d0bd82SXin Long 	if (rt6_flags & RTF_CACHE)
46781da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
46791da177e4SLinus Torvalds 
4680d4ead6b3SDavid Ahern 	if (dest) {
4681d4ead6b3SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, dest))
4682c78679e8SDavid S. Miller 			goto nla_put_failure;
46831da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
46841da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
468522d0bd82SXin Long 		if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
4686c78679e8SDavid S. Miller 			goto nla_put_failure;
46871da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
46881da177e4SLinus Torvalds 	if (src) {
4689930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
4690c78679e8SDavid S. Miller 			goto nla_put_failure;
46911da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
4692c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
469322d0bd82SXin Long 		   nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
4694c78679e8SDavid S. Miller 		goto nla_put_failure;
46951da177e4SLinus Torvalds #endif
46967bc570c8SYOSHIFUJI Hideaki 	if (iif) {
46977bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
469822d0bd82SXin Long 		if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
4699fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
47002cf75070SNikolay Aleksandrov 
47017bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
47027bc570c8SYOSHIFUJI Hideaki 				return 0;
4703fd61c6baSDavid Ahern 			if (err < 0)
47047bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
47057bc570c8SYOSHIFUJI Hideaki 		} else
47067bc570c8SYOSHIFUJI Hideaki #endif
4707c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
4708c78679e8SDavid S. Miller 				goto nla_put_failure;
4709d4ead6b3SDavid Ahern 	} else if (dest) {
47101da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
4711d4ead6b3SDavid Ahern 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
4712930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4713c78679e8SDavid S. Miller 			goto nla_put_failure;
4714c3968a85SDaniel Walter 	}
4715c3968a85SDaniel Walter 
471693c2fb25SDavid Ahern 	if (rt->fib6_prefsrc.plen) {
4717c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
471893c2fb25SDavid Ahern 		saddr_buf = rt->fib6_prefsrc.addr;
4719930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4720c78679e8SDavid S. Miller 			goto nla_put_failure;
47211da177e4SLinus Torvalds 	}
47222d7202bfSThomas Graf 
4723d4ead6b3SDavid Ahern 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4724d4ead6b3SDavid Ahern 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
47252d7202bfSThomas Graf 		goto nla_put_failure;
47262d7202bfSThomas Graf 
472793c2fb25SDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
4728beb1afacSDavid Ahern 		goto nla_put_failure;
4729beb1afacSDavid Ahern 
4730beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
4731beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
4732beb1afacSDavid Ahern 	 */
473322d0bd82SXin Long 	if (rt6) {
473422d0bd82SXin Long 		if (rt6_flags & RTF_GATEWAY &&
473522d0bd82SXin Long 		    nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
473622d0bd82SXin Long 			goto nla_put_failure;
473722d0bd82SXin Long 
473822d0bd82SXin Long 		if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
473922d0bd82SXin Long 			goto nla_put_failure;
474022d0bd82SXin Long 	} else if (rt->fib6_nsiblings) {
47418d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
4742beb1afacSDavid Ahern 		struct nlattr *mp;
4743beb1afacSDavid Ahern 
4744beb1afacSDavid Ahern 		mp = nla_nest_start(skb, RTA_MULTIPATH);
4745beb1afacSDavid Ahern 		if (!mp)
4746beb1afacSDavid Ahern 			goto nla_put_failure;
4747beb1afacSDavid Ahern 
4748beb1afacSDavid Ahern 		if (rt6_add_nexthop(skb, rt) < 0)
4749beb1afacSDavid Ahern 			goto nla_put_failure;
4750beb1afacSDavid Ahern 
4751beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
475293c2fb25SDavid Ahern 					 &rt->fib6_siblings, fib6_siblings) {
4753beb1afacSDavid Ahern 			if (rt6_add_nexthop(skb, sibling) < 0)
475494f826b8SEric Dumazet 				goto nla_put_failure;
475594f826b8SEric Dumazet 		}
47562d7202bfSThomas Graf 
4757beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
4758beb1afacSDavid Ahern 	} else {
47595be083ceSDavid Ahern 		if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
4760c78679e8SDavid S. Miller 			goto nla_put_failure;
4761beb1afacSDavid Ahern 	}
47628253947eSLi Wei 
476322d0bd82SXin Long 	if (rt6_flags & RTF_EXPIRES) {
476414895687SDavid Ahern 		expires = dst ? dst->expires : rt->expires;
476514895687SDavid Ahern 		expires -= jiffies;
476614895687SDavid Ahern 	}
476769cdf8f9SYOSHIFUJI Hideaki 
4768d4ead6b3SDavid Ahern 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
4769e3703b3dSThomas Graf 		goto nla_put_failure;
47701da177e4SLinus Torvalds 
477122d0bd82SXin Long 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
4772c78ba6d6SLubomir Rintel 		goto nla_put_failure;
4773c78ba6d6SLubomir Rintel 
477419e42e45SRoopa Prabhu 
4775053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4776053c095aSJohannes Berg 	return 0;
47772d7202bfSThomas Graf 
47782d7202bfSThomas Graf nla_put_failure:
477926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
478026932566SPatrick McHardy 	return -EMSGSIZE;
47811da177e4SLinus Torvalds }
47821da177e4SLinus Torvalds 
478313e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i,
478413e38901SDavid Ahern 			       const struct net_device *dev)
478513e38901SDavid Ahern {
478613e38901SDavid Ahern 	if (f6i->fib6_nh.nh_dev == dev)
478713e38901SDavid Ahern 		return true;
478813e38901SDavid Ahern 
478913e38901SDavid Ahern 	if (f6i->fib6_nsiblings) {
479013e38901SDavid Ahern 		struct fib6_info *sibling, *next_sibling;
479113e38901SDavid Ahern 
479213e38901SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
479313e38901SDavid Ahern 					 &f6i->fib6_siblings, fib6_siblings) {
479413e38901SDavid Ahern 			if (sibling->fib6_nh.nh_dev == dev)
479513e38901SDavid Ahern 				return true;
479613e38901SDavid Ahern 		}
479713e38901SDavid Ahern 	}
479813e38901SDavid Ahern 
479913e38901SDavid Ahern 	return false;
480013e38901SDavid Ahern }
480113e38901SDavid Ahern 
48028d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg)
48031da177e4SLinus Torvalds {
48041da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
480513e38901SDavid Ahern 	struct fib_dump_filter *filter = &arg->filter;
480613e38901SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
48071f17e2f2SDavid Ahern 	struct net *net = arg->net;
48081f17e2f2SDavid Ahern 
4809421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
48101f17e2f2SDavid Ahern 		return 0;
48111da177e4SLinus Torvalds 
481213e38901SDavid Ahern 	if ((filter->flags & RTM_F_PREFIX) &&
481393c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_PREFIX_RT)) {
4814f8cfe2ceSDavid Ahern 		/* success since this is not a prefix route */
4815f8cfe2ceSDavid Ahern 		return 1;
4816f8cfe2ceSDavid Ahern 	}
481713e38901SDavid Ahern 	if (filter->filter_set) {
481813e38901SDavid Ahern 		if ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
481913e38901SDavid Ahern 		    (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
482013e38901SDavid Ahern 		    (filter->protocol && rt->fib6_protocol != filter->protocol)) {
482113e38901SDavid Ahern 			return 1;
482213e38901SDavid Ahern 		}
482313e38901SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
4824f8cfe2ceSDavid Ahern 	}
48251da177e4SLinus Torvalds 
4826d4ead6b3SDavid Ahern 	return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4827d4ead6b3SDavid Ahern 			     RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
482813e38901SDavid Ahern 			     arg->cb->nlh->nlmsg_seq, flags);
48291da177e4SLinus Torvalds }
48301da177e4SLinus Torvalds 
4831c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4832c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
48331da177e4SLinus Torvalds {
48343b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
4835ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
483618c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
4837a68886a6SDavid Ahern 	struct fib6_info *from;
483818c3a61cSRoopa Prabhu 	struct dst_entry *dst;
48391da177e4SLinus Torvalds 	struct rt6_info *rt;
4840ab364a6fSThomas Graf 	struct sk_buff *skb;
4841ab364a6fSThomas Graf 	struct rtmsg *rtm;
4842744486d4SMaciej Żenczykowski 	struct flowi6 fl6 = {};
484318c3a61cSRoopa Prabhu 	bool fibmatch;
4844ab364a6fSThomas Graf 
4845fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4846c21ef3e3SDavid Ahern 			  extack);
4847ab364a6fSThomas Graf 	if (err < 0)
4848ab364a6fSThomas Graf 		goto errout;
4849ab364a6fSThomas Graf 
4850ab364a6fSThomas Graf 	err = -EINVAL;
485138b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
485238b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
485318c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
4854ab364a6fSThomas Graf 
4855ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
4856ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4857ab364a6fSThomas Graf 			goto errout;
4858ab364a6fSThomas Graf 
48594e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
4860ab364a6fSThomas Graf 	}
4861ab364a6fSThomas Graf 
4862ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
4863ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4864ab364a6fSThomas Graf 			goto errout;
4865ab364a6fSThomas Graf 
48664e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
4867ab364a6fSThomas Graf 	}
4868ab364a6fSThomas Graf 
4869ab364a6fSThomas Graf 	if (tb[RTA_IIF])
4870ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
4871ab364a6fSThomas Graf 
4872ab364a6fSThomas Graf 	if (tb[RTA_OIF])
487372331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
4874ab364a6fSThomas Graf 
48752e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
48762e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
48772e47b291SLorenzo Colitti 
4878622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
4879622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
4880622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
4881622ec2c9SLorenzo Colitti 	else
4882622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4883622ec2c9SLorenzo Colitti 
4884eacb9384SRoopa Prabhu 	if (tb[RTA_SPORT])
4885eacb9384SRoopa Prabhu 		fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
4886eacb9384SRoopa Prabhu 
4887eacb9384SRoopa Prabhu 	if (tb[RTA_DPORT])
4888eacb9384SRoopa Prabhu 		fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
4889eacb9384SRoopa Prabhu 
4890eacb9384SRoopa Prabhu 	if (tb[RTA_IP_PROTO]) {
4891eacb9384SRoopa Prabhu 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
4892eacb9384SRoopa Prabhu 						  &fl6.flowi6_proto, extack);
4893eacb9384SRoopa Prabhu 		if (err)
4894eacb9384SRoopa Prabhu 			goto errout;
4895eacb9384SRoopa Prabhu 	}
4896eacb9384SRoopa Prabhu 
4897ab364a6fSThomas Graf 	if (iif) {
4898ab364a6fSThomas Graf 		struct net_device *dev;
489972331bc0SShmulik Ladkani 		int flags = 0;
490072331bc0SShmulik Ladkani 
4901121622dbSFlorian Westphal 		rcu_read_lock();
4902121622dbSFlorian Westphal 
4903121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
4904ab364a6fSThomas Graf 		if (!dev) {
4905121622dbSFlorian Westphal 			rcu_read_unlock();
4906ab364a6fSThomas Graf 			err = -ENODEV;
4907ab364a6fSThomas Graf 			goto errout;
4908ab364a6fSThomas Graf 		}
490972331bc0SShmulik Ladkani 
491072331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
491172331bc0SShmulik Ladkani 
491272331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
491372331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
491472331bc0SShmulik Ladkani 
4915b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
4916121622dbSFlorian Westphal 
4917121622dbSFlorian Westphal 		rcu_read_unlock();
491872331bc0SShmulik Ladkani 	} else {
491972331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
492072331bc0SShmulik Ladkani 
492118c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
492218c3a61cSRoopa Prabhu 	}
492318c3a61cSRoopa Prabhu 
492418c3a61cSRoopa Prabhu 
492518c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
492618c3a61cSRoopa Prabhu 	if (rt->dst.error) {
492718c3a61cSRoopa Prabhu 		err = rt->dst.error;
492818c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
492918c3a61cSRoopa Prabhu 		goto errout;
4930ab364a6fSThomas Graf 	}
49311da177e4SLinus Torvalds 
49329d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
49339d6acb3bSWANG Cong 		err = rt->dst.error;
49349d6acb3bSWANG Cong 		ip6_rt_put(rt);
49359d6acb3bSWANG Cong 		goto errout;
49369d6acb3bSWANG Cong 	}
49379d6acb3bSWANG Cong 
49381da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
493938308473SDavid S. Miller 	if (!skb) {
494094e187c0SAmerigo Wang 		ip6_rt_put(rt);
4941ab364a6fSThomas Graf 		err = -ENOBUFS;
4942ab364a6fSThomas Graf 		goto errout;
4943ab364a6fSThomas Graf 	}
49441da177e4SLinus Torvalds 
4945d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
4946a68886a6SDavid Ahern 
4947a68886a6SDavid Ahern 	rcu_read_lock();
4948a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
4949a68886a6SDavid Ahern 
495018c3a61cSRoopa Prabhu 	if (fibmatch)
4951a68886a6SDavid Ahern 		err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, iif,
495218c3a61cSRoopa Prabhu 				    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
495318c3a61cSRoopa Prabhu 				    nlh->nlmsg_seq, 0);
495418c3a61cSRoopa Prabhu 	else
4955a68886a6SDavid Ahern 		err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
4956a68886a6SDavid Ahern 				    &fl6.saddr, iif, RTM_NEWROUTE,
4957d4ead6b3SDavid Ahern 				    NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
4958d4ead6b3SDavid Ahern 				    0);
4959a68886a6SDavid Ahern 	rcu_read_unlock();
4960a68886a6SDavid Ahern 
49611da177e4SLinus Torvalds 	if (err < 0) {
4962ab364a6fSThomas Graf 		kfree_skb(skb);
4963ab364a6fSThomas Graf 		goto errout;
49641da177e4SLinus Torvalds 	}
49651da177e4SLinus Torvalds 
496615e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4967ab364a6fSThomas Graf errout:
49681da177e4SLinus Torvalds 	return err;
49691da177e4SLinus Torvalds }
49701da177e4SLinus Torvalds 
49718d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
497237a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
49731da177e4SLinus Torvalds {
49741da177e4SLinus Torvalds 	struct sk_buff *skb;
49755578689aSDaniel Lezcano 	struct net *net = info->nl_net;
4976528c4cebSDenis V. Lunev 	u32 seq;
4977528c4cebSDenis V. Lunev 	int err;
49780d51aa80SJamal Hadi Salim 
4979528c4cebSDenis V. Lunev 	err = -ENOBUFS;
498038308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
498186872cb5SThomas Graf 
498219e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
498338308473SDavid S. Miller 	if (!skb)
498421713ebcSThomas Graf 		goto errout;
49851da177e4SLinus Torvalds 
4986d4ead6b3SDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
4987f8cfe2ceSDavid Ahern 			    event, info->portid, seq, nlm_flags);
498826932566SPatrick McHardy 	if (err < 0) {
498926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
499026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
499126932566SPatrick McHardy 		kfree_skb(skb);
499226932566SPatrick McHardy 		goto errout;
499326932566SPatrick McHardy 	}
499415e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
49955578689aSDaniel Lezcano 		    info->nlh, gfp_any());
49961ce85fe4SPablo Neira Ayuso 	return;
499721713ebcSThomas Graf errout:
499821713ebcSThomas Graf 	if (err < 0)
49995578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
50001da177e4SLinus Torvalds }
50011da177e4SLinus Torvalds 
50028ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
5003351638e7SJiri Pirko 				unsigned long event, void *ptr)
50048ed67789SDaniel Lezcano {
5005351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5006c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
50078ed67789SDaniel Lezcano 
5008242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
5009242d3a49SWANG Cong 		return NOTIFY_OK;
5010242d3a49SWANG Cong 
5011242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
5012421842edSDavid Ahern 		net->ipv6.fib6_null_entry->fib6_nh.nh_dev = dev;
5013d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
50148ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
50158ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5016d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
50178ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
5018d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
50198ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
50208ed67789SDaniel Lezcano #endif
502176da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
502276da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
502376da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
502476da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
502576da0704SWANG Cong 		 */
502612d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
5027242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
502812d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
502912d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
5030242d3a49SWANG Cong #endif
50318ed67789SDaniel Lezcano 	}
50328ed67789SDaniel Lezcano 
50338ed67789SDaniel Lezcano 	return NOTIFY_OK;
50348ed67789SDaniel Lezcano }
50358ed67789SDaniel Lezcano 
50361da177e4SLinus Torvalds /*
50371da177e4SLinus Torvalds  *	/proc
50381da177e4SLinus Torvalds  */
50391da177e4SLinus Torvalds 
50401da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
50411da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
50421da177e4SLinus Torvalds {
504369ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
50441da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
504569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
504669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
504781eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
504869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
504969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
5050fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
505169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
50521da177e4SLinus Torvalds 
50531da177e4SLinus Torvalds 	return 0;
50541da177e4SLinus Torvalds }
50551da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
50561da177e4SLinus Torvalds 
50571da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
50581da177e4SLinus Torvalds 
50591da177e4SLinus Torvalds static
5060fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
50611da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
50621da177e4SLinus Torvalds {
5063c486da34SLucian Adrian Grijincu 	struct net *net;
5064c486da34SLucian Adrian Grijincu 	int delay;
5065f0fb9b28SAditya Pakki 	int ret;
5066c486da34SLucian Adrian Grijincu 	if (!write)
5067c486da34SLucian Adrian Grijincu 		return -EINVAL;
5068c486da34SLucian Adrian Grijincu 
5069c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
5070c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
5071f0fb9b28SAditya Pakki 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5072f0fb9b28SAditya Pakki 	if (ret)
5073f0fb9b28SAditya Pakki 		return ret;
5074f0fb9b28SAditya Pakki 
50752ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
50761da177e4SLinus Torvalds 	return 0;
50771da177e4SLinus Torvalds }
50781da177e4SLinus Torvalds 
50797c6bb7d2SDavid Ahern static int zero;
50807c6bb7d2SDavid Ahern static int one = 1;
50817c6bb7d2SDavid Ahern 
5082ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = {
50831da177e4SLinus Torvalds 	{
50841da177e4SLinus Torvalds 		.procname	=	"flush",
50854990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
50861da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
508789c8b3a1SDave Jones 		.mode		=	0200,
50886d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
50891da177e4SLinus Torvalds 	},
50901da177e4SLinus Torvalds 	{
50911da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
50929a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
50931da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
50941da177e4SLinus Torvalds 		.mode		=	0644,
50956d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
50961da177e4SLinus Torvalds 	},
50971da177e4SLinus Torvalds 	{
50981da177e4SLinus Torvalds 		.procname	=	"max_size",
50994990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
51001da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51011da177e4SLinus Torvalds 		.mode		=	0644,
51026d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
51031da177e4SLinus Torvalds 	},
51041da177e4SLinus Torvalds 	{
51051da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
51064990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
51071da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51081da177e4SLinus Torvalds 		.mode		=	0644,
51096d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
51101da177e4SLinus Torvalds 	},
51111da177e4SLinus Torvalds 	{
51121da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
51134990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
51141da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51151da177e4SLinus Torvalds 		.mode		=	0644,
51166d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
51171da177e4SLinus Torvalds 	},
51181da177e4SLinus Torvalds 	{
51191da177e4SLinus Torvalds 		.procname	=	"gc_interval",
51204990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
51211da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51221da177e4SLinus Torvalds 		.mode		=	0644,
51236d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
51241da177e4SLinus Torvalds 	},
51251da177e4SLinus Torvalds 	{
51261da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
51274990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
51281da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51291da177e4SLinus Torvalds 		.mode		=	0644,
5130f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
51311da177e4SLinus Torvalds 	},
51321da177e4SLinus Torvalds 	{
51331da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
51344990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
51351da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51361da177e4SLinus Torvalds 		.mode		=	0644,
51376d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
51381da177e4SLinus Torvalds 	},
51391da177e4SLinus Torvalds 	{
51401da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
51414990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
51421da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51431da177e4SLinus Torvalds 		.mode		=	0644,
5144f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
51451da177e4SLinus Torvalds 	},
51461da177e4SLinus Torvalds 	{
51471da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
51484990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
51491da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51501da177e4SLinus Torvalds 		.mode		=	0644,
51516d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
51521da177e4SLinus Torvalds 	},
51537c6bb7d2SDavid Ahern 	{
51547c6bb7d2SDavid Ahern 		.procname	=	"skip_notify_on_dev_down",
51557c6bb7d2SDavid Ahern 		.data		=	&init_net.ipv6.sysctl.skip_notify_on_dev_down,
51567c6bb7d2SDavid Ahern 		.maxlen		=	sizeof(int),
51577c6bb7d2SDavid Ahern 		.mode		=	0644,
51587c6bb7d2SDavid Ahern 		.proc_handler	=	proc_dointvec,
51597c6bb7d2SDavid Ahern 		.extra1		=	&zero,
51607c6bb7d2SDavid Ahern 		.extra2		=	&one,
51617c6bb7d2SDavid Ahern 	},
5162f8572d8fSEric W. Biederman 	{ }
51631da177e4SLinus Torvalds };
51641da177e4SLinus Torvalds 
51652c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
5166760f2d01SDaniel Lezcano {
5167760f2d01SDaniel Lezcano 	struct ctl_table *table;
5168760f2d01SDaniel Lezcano 
5169760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
5170760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
5171760f2d01SDaniel Lezcano 			GFP_KERNEL);
51725ee09105SYOSHIFUJI Hideaki 
51735ee09105SYOSHIFUJI Hideaki 	if (table) {
51745ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
5175c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
517686393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
51775ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
51785ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
51795ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
51805ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
51815ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
51825ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
51835ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
51849c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
51857c6bb7d2SDavid Ahern 		table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
5186464dc801SEric W. Biederman 
5187464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
5188464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
5189464dc801SEric W. Biederman 			table[0].procname = NULL;
51905ee09105SYOSHIFUJI Hideaki 	}
51915ee09105SYOSHIFUJI Hideaki 
5192760f2d01SDaniel Lezcano 	return table;
5193760f2d01SDaniel Lezcano }
51941da177e4SLinus Torvalds #endif
51951da177e4SLinus Torvalds 
51962c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
5197cdb18761SDaniel Lezcano {
5198633d424bSPavel Emelyanov 	int ret = -ENOMEM;
51998ed67789SDaniel Lezcano 
520086393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
520186393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
5202f2fc6a54SBenjamin Thery 
5203fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5204fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
5205fc66f95cSEric Dumazet 
5206421842edSDavid Ahern 	net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template,
5207421842edSDavid Ahern 					    sizeof(*net->ipv6.fib6_null_entry),
5208421842edSDavid Ahern 					    GFP_KERNEL);
5209421842edSDavid Ahern 	if (!net->ipv6.fib6_null_entry)
5210421842edSDavid Ahern 		goto out_ip6_dst_entries;
5211421842edSDavid Ahern 
52128ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
52138ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
52148ed67789SDaniel Lezcano 					   GFP_KERNEL);
52158ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
5216421842edSDavid Ahern 		goto out_fib6_null_entry;
5217d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
521862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
521962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
52208ed67789SDaniel Lezcano 
52218ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5222feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
52238ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
52248ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
52258ed67789SDaniel Lezcano 					       GFP_KERNEL);
522668fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
522768fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
5228d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
522962fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
523062fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
52318ed67789SDaniel Lezcano 
52328ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
52338ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
52348ed67789SDaniel Lezcano 					       GFP_KERNEL);
523568fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
523668fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
5237d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
523862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
523962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
52408ed67789SDaniel Lezcano #endif
52418ed67789SDaniel Lezcano 
5242b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
5243b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
5244b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5245b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5246b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5247b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5248b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5249b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
52507c6bb7d2SDavid Ahern 	net->ipv6.sysctl.skip_notify_on_dev_down = 0;
5251b339a47cSPeter Zijlstra 
52526891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
52536891a346SBenjamin Thery 
52548ed67789SDaniel Lezcano 	ret = 0;
52558ed67789SDaniel Lezcano out:
52568ed67789SDaniel Lezcano 	return ret;
5257f2fc6a54SBenjamin Thery 
525868fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
525968fffc67SPeter Zijlstra out_ip6_prohibit_entry:
526068fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
526168fffc67SPeter Zijlstra out_ip6_null_entry:
526268fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
526368fffc67SPeter Zijlstra #endif
5264421842edSDavid Ahern out_fib6_null_entry:
5265421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
5266fc66f95cSEric Dumazet out_ip6_dst_entries:
5267fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5268f2fc6a54SBenjamin Thery out_ip6_dst_ops:
5269f2fc6a54SBenjamin Thery 	goto out;
5270cdb18761SDaniel Lezcano }
5271cdb18761SDaniel Lezcano 
52722c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
5273cdb18761SDaniel Lezcano {
5274421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
52758ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
52768ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
52778ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
52788ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
52798ed67789SDaniel Lezcano #endif
528041bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5281cdb18761SDaniel Lezcano }
5282cdb18761SDaniel Lezcano 
5283d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
5284d189634eSThomas Graf {
5285d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5286c3506372SChristoph Hellwig 	proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
5287c3506372SChristoph Hellwig 			sizeof(struct ipv6_route_iter));
52883617d949SChristoph Hellwig 	proc_create_net_single("rt6_stats", 0444, net->proc_net,
52893617d949SChristoph Hellwig 			rt6_stats_seq_show, NULL);
5290d189634eSThomas Graf #endif
5291d189634eSThomas Graf 	return 0;
5292d189634eSThomas Graf }
5293d189634eSThomas Graf 
5294d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
5295d189634eSThomas Graf {
5296d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5297ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
5298ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
5299d189634eSThomas Graf #endif
5300d189634eSThomas Graf }
5301d189634eSThomas Graf 
5302cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
5303cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
5304cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
5305cdb18761SDaniel Lezcano };
5306cdb18761SDaniel Lezcano 
5307c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
5308c3426b47SDavid S. Miller {
5309c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5310c3426b47SDavid S. Miller 
5311c3426b47SDavid S. Miller 	if (!bp)
5312c3426b47SDavid S. Miller 		return -ENOMEM;
5313c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
5314c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
5315c3426b47SDavid S. Miller 	return 0;
5316c3426b47SDavid S. Miller }
5317c3426b47SDavid S. Miller 
5318c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
5319c3426b47SDavid S. Miller {
5320c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
5321c3426b47SDavid S. Miller 
5322c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
532356a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
5324c3426b47SDavid S. Miller 	kfree(bp);
5325c3426b47SDavid S. Miller }
5326c3426b47SDavid S. Miller 
53272b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
5328c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
5329c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
5330c3426b47SDavid S. Miller };
5331c3426b47SDavid S. Miller 
5332d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
5333d189634eSThomas Graf 	.init = ip6_route_net_init_late,
5334d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
5335d189634eSThomas Graf };
5336d189634eSThomas Graf 
53378ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
53388ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
5339242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
53408ed67789SDaniel Lezcano };
53418ed67789SDaniel Lezcano 
53422f460933SWANG Cong void __init ip6_route_init_special_entries(void)
53432f460933SWANG Cong {
53442f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
53452f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
53462f460933SWANG Cong 	 * manually for init_net */
5347421842edSDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_nh.nh_dev = init_net.loopback_dev;
53482f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
53492f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
53502f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
53512f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
53522f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
53532f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
53542f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
53552f460933SWANG Cong   #endif
53562f460933SWANG Cong }
53572f460933SWANG Cong 
5358433d49c3SDaniel Lezcano int __init ip6_route_init(void)
53591da177e4SLinus Torvalds {
5360433d49c3SDaniel Lezcano 	int ret;
53618d0b94afSMartin KaFai Lau 	int cpu;
5362433d49c3SDaniel Lezcano 
53639a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
53649a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
53659a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
53669a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
53679a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
5368c19a28e1SFernando Carrijo 		goto out;
536914e50e57SDavid S. Miller 
5370fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
53718ed67789SDaniel Lezcano 	if (ret)
5372bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
5373bdb3289fSDaniel Lezcano 
5374c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5375c3426b47SDavid S. Miller 	if (ret)
5376e8803b6cSDavid S. Miller 		goto out_dst_entries;
53772a0c451aSThomas Graf 
53787e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
53797e52b33bSDavid S. Miller 	if (ret)
53807e52b33bSDavid S. Miller 		goto out_register_inetpeer;
5381c3426b47SDavid S. Miller 
53825dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
53835dc121e9SArnaud Ebalard 
5384e8803b6cSDavid S. Miller 	ret = fib6_init();
5385433d49c3SDaniel Lezcano 	if (ret)
53868ed67789SDaniel Lezcano 		goto out_register_subsys;
5387433d49c3SDaniel Lezcano 
5388433d49c3SDaniel Lezcano 	ret = xfrm6_init();
5389433d49c3SDaniel Lezcano 	if (ret)
5390e8803b6cSDavid S. Miller 		goto out_fib6_init;
5391c35b7e72SDaniel Lezcano 
5392433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
5393433d49c3SDaniel Lezcano 	if (ret)
5394433d49c3SDaniel Lezcano 		goto xfrm6_init;
53957e5449c2SDaniel Lezcano 
5396d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
5397d189634eSThomas Graf 	if (ret)
5398d189634eSThomas Graf 		goto fib6_rules_init;
5399d189634eSThomas Graf 
540016feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
540116feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
540216feebcfSFlorian Westphal 	if (ret < 0)
540316feebcfSFlorian Westphal 		goto out_register_late_subsys;
540416feebcfSFlorian Westphal 
540516feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
540616feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
540716feebcfSFlorian Westphal 	if (ret < 0)
540816feebcfSFlorian Westphal 		goto out_register_late_subsys;
540916feebcfSFlorian Westphal 
541016feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
541116feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
541216feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
541316feebcfSFlorian Westphal 	if (ret < 0)
5414d189634eSThomas Graf 		goto out_register_late_subsys;
5415433d49c3SDaniel Lezcano 
54168ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
5417cdb18761SDaniel Lezcano 	if (ret)
5418d189634eSThomas Graf 		goto out_register_late_subsys;
54198ed67789SDaniel Lezcano 
54208d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
54218d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
54228d0b94afSMartin KaFai Lau 
54238d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
54248d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
54258d0b94afSMartin KaFai Lau 	}
54268d0b94afSMartin KaFai Lau 
5427433d49c3SDaniel Lezcano out:
5428433d49c3SDaniel Lezcano 	return ret;
5429433d49c3SDaniel Lezcano 
5430d189634eSThomas Graf out_register_late_subsys:
543116feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
5432d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5433433d49c3SDaniel Lezcano fib6_rules_init:
5434433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
5435433d49c3SDaniel Lezcano xfrm6_init:
5436433d49c3SDaniel Lezcano 	xfrm6_fini();
54372a0c451aSThomas Graf out_fib6_init:
54382a0c451aSThomas Graf 	fib6_gc_cleanup();
54398ed67789SDaniel Lezcano out_register_subsys:
54408ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
54417e52b33bSDavid S. Miller out_register_inetpeer:
54427e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
5443fc66f95cSEric Dumazet out_dst_entries:
5444fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5445433d49c3SDaniel Lezcano out_kmem_cache:
5446f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
5447433d49c3SDaniel Lezcano 	goto out;
54481da177e4SLinus Torvalds }
54491da177e4SLinus Torvalds 
54501da177e4SLinus Torvalds void ip6_route_cleanup(void)
54511da177e4SLinus Torvalds {
54528ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
5453d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5454101367c2SThomas Graf 	fib6_rules_cleanup();
54551da177e4SLinus Torvalds 	xfrm6_fini();
54561da177e4SLinus Torvalds 	fib6_gc_cleanup();
5457c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
54588ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
545941bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5460f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
54611da177e4SLinus Torvalds }
5462