xref: /openbmc/linux/net/ipv6/route.c (revision 702cea56852c6e57e997890ae8202e5385c63691)
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);
105*702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
106*702cea56SDavid Ahern 			   int strict);
1078d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt);
108d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
1098d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
110d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
11116a16cd3SDavid Ahern 			 int iif, int type, u32 portid, u32 seq,
11216a16cd3SDavid Ahern 			 unsigned int flags);
1138d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
11435732d01SWei Wang 					   struct in6_addr *daddr,
11535732d01SWei Wang 					   struct in6_addr *saddr);
1161da177e4SLinus Torvalds 
11770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1188d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
119b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
120830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
121830218c1SDavid Ahern 					   struct net_device *dev,
12295c96174SEric Dumazet 					   unsigned int pref);
1238d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
124b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
125830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
126830218c1SDavid Ahern 					   struct net_device *dev);
12770ceb4f5SYOSHIFUJI Hideaki #endif
12870ceb4f5SYOSHIFUJI Hideaki 
1298d0b94afSMartin KaFai Lau struct uncached_list {
1308d0b94afSMartin KaFai Lau 	spinlock_t		lock;
1318d0b94afSMartin KaFai Lau 	struct list_head	head;
1328d0b94afSMartin KaFai Lau };
1338d0b94afSMartin KaFai Lau 
1348d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
1358d0b94afSMartin KaFai Lau 
136510c321bSXin Long void rt6_uncached_list_add(struct rt6_info *rt)
1378d0b94afSMartin KaFai Lau {
1388d0b94afSMartin KaFai Lau 	struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
1398d0b94afSMartin KaFai Lau 
1408d0b94afSMartin KaFai Lau 	rt->rt6i_uncached_list = ul;
1418d0b94afSMartin KaFai Lau 
1428d0b94afSMartin KaFai Lau 	spin_lock_bh(&ul->lock);
1438d0b94afSMartin KaFai Lau 	list_add_tail(&rt->rt6i_uncached, &ul->head);
1448d0b94afSMartin KaFai Lau 	spin_unlock_bh(&ul->lock);
1458d0b94afSMartin KaFai Lau }
1468d0b94afSMartin KaFai Lau 
147510c321bSXin Long void rt6_uncached_list_del(struct rt6_info *rt)
1488d0b94afSMartin KaFai Lau {
1498d0b94afSMartin KaFai Lau 	if (!list_empty(&rt->rt6i_uncached)) {
1508d0b94afSMartin KaFai Lau 		struct uncached_list *ul = rt->rt6i_uncached_list;
15181eb8447SWei Wang 		struct net *net = dev_net(rt->dst.dev);
1528d0b94afSMartin KaFai Lau 
1538d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1548d0b94afSMartin KaFai Lau 		list_del(&rt->rt6i_uncached);
15581eb8447SWei Wang 		atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
1568d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1578d0b94afSMartin KaFai Lau 	}
1588d0b94afSMartin KaFai Lau }
1598d0b94afSMartin KaFai Lau 
1608d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
1618d0b94afSMartin KaFai Lau {
1628d0b94afSMartin KaFai Lau 	struct net_device *loopback_dev = net->loopback_dev;
1638d0b94afSMartin KaFai Lau 	int cpu;
1648d0b94afSMartin KaFai Lau 
165e332bc67SEric W. Biederman 	if (dev == loopback_dev)
166e332bc67SEric W. Biederman 		return;
167e332bc67SEric W. Biederman 
1688d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
1698d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
1708d0b94afSMartin KaFai Lau 		struct rt6_info *rt;
1718d0b94afSMartin KaFai Lau 
1728d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1738d0b94afSMartin KaFai Lau 		list_for_each_entry(rt, &ul->head, rt6i_uncached) {
1748d0b94afSMartin KaFai Lau 			struct inet6_dev *rt_idev = rt->rt6i_idev;
1758d0b94afSMartin KaFai Lau 			struct net_device *rt_dev = rt->dst.dev;
1768d0b94afSMartin KaFai Lau 
177e332bc67SEric W. Biederman 			if (rt_idev->dev == dev) {
1788d0b94afSMartin KaFai Lau 				rt->rt6i_idev = in6_dev_get(loopback_dev);
1798d0b94afSMartin KaFai Lau 				in6_dev_put(rt_idev);
1808d0b94afSMartin KaFai Lau 			}
1818d0b94afSMartin KaFai Lau 
182e332bc67SEric W. Biederman 			if (rt_dev == dev) {
1838d0b94afSMartin KaFai Lau 				rt->dst.dev = loopback_dev;
1848d0b94afSMartin KaFai Lau 				dev_hold(rt->dst.dev);
1858d0b94afSMartin KaFai Lau 				dev_put(rt_dev);
1868d0b94afSMartin KaFai Lau 			}
1878d0b94afSMartin KaFai Lau 		}
1888d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1898d0b94afSMartin KaFai Lau 	}
1908d0b94afSMartin KaFai Lau }
1918d0b94afSMartin KaFai Lau 
192f8a1b43bSDavid Ahern static inline const void *choose_neigh_daddr(const struct in6_addr *p,
193f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
194f894cbf8SDavid S. Miller 					     const void *daddr)
19539232973SDavid S. Miller {
196a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
19739232973SDavid S. Miller 		return (const void *) p;
198f894cbf8SDavid S. Miller 	else if (skb)
199f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
20039232973SDavid S. Miller 	return daddr;
20139232973SDavid S. Miller }
20239232973SDavid S. Miller 
203f8a1b43bSDavid Ahern struct neighbour *ip6_neigh_lookup(const struct in6_addr *gw,
204f8a1b43bSDavid Ahern 				   struct net_device *dev,
205f894cbf8SDavid S. Miller 				   struct sk_buff *skb,
206f894cbf8SDavid S. Miller 				   const void *daddr)
207d3aaeb38SDavid S. Miller {
20839232973SDavid S. Miller 	struct neighbour *n;
20939232973SDavid S. Miller 
210f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(gw, skb, daddr);
211f8a1b43bSDavid Ahern 	n = __ipv6_neigh_lookup(dev, daddr);
212f83c7790SDavid S. Miller 	if (n)
213f83c7790SDavid S. Miller 		return n;
2147adf3246SStefano Brivio 
2157adf3246SStefano Brivio 	n = neigh_create(&nd_tbl, daddr, dev);
2167adf3246SStefano Brivio 	return IS_ERR(n) ? NULL : n;
217f8a1b43bSDavid Ahern }
218f8a1b43bSDavid Ahern 
219f8a1b43bSDavid Ahern static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,
220f8a1b43bSDavid Ahern 					      struct sk_buff *skb,
221f8a1b43bSDavid Ahern 					      const void *daddr)
222f8a1b43bSDavid Ahern {
223f8a1b43bSDavid Ahern 	const struct rt6_info *rt = container_of(dst, struct rt6_info, dst);
224f8a1b43bSDavid Ahern 
225f8a1b43bSDavid Ahern 	return ip6_neigh_lookup(&rt->rt6i_gateway, dst->dev, skb, daddr);
226f83c7790SDavid S. Miller }
227f83c7790SDavid S. Miller 
22863fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
22963fca65dSJulian Anastasov {
23063fca65dSJulian Anastasov 	struct net_device *dev = dst->dev;
23163fca65dSJulian Anastasov 	struct rt6_info *rt = (struct rt6_info *)dst;
23263fca65dSJulian Anastasov 
233f8a1b43bSDavid Ahern 	daddr = choose_neigh_daddr(&rt->rt6i_gateway, NULL, daddr);
23463fca65dSJulian Anastasov 	if (!daddr)
23563fca65dSJulian Anastasov 		return;
23663fca65dSJulian Anastasov 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
23763fca65dSJulian Anastasov 		return;
23863fca65dSJulian Anastasov 	if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
23963fca65dSJulian Anastasov 		return;
24063fca65dSJulian Anastasov 	__ipv6_confirm_neigh(dev, daddr);
24163fca65dSJulian Anastasov }
24263fca65dSJulian Anastasov 
2439a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
2441da177e4SLinus Torvalds 	.family			=	AF_INET6,
2451da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
2461da177e4SLinus Torvalds 	.gc_thresh		=	1024,
2471da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
2480dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
249ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
250d4ead6b3SDavid Ahern 	.cow_metrics		=	dst_cow_metrics_generic,
2511da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
2521da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
2531da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
2541da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
2551da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
2566e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
2579f8955ccSEric W. Biederman 	.local_out		=	__ip6_local_out,
258f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
25963fca65dSJulian Anastasov 	.confirm_neigh		=	ip6_confirm_neigh,
2601da177e4SLinus Torvalds };
2611da177e4SLinus Torvalds 
262ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
263ec831ea7SRoland Dreier {
264618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
265618f9bc7SSteffen Klassert 
266618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
267ec831ea7SRoland Dreier }
268ec831ea7SRoland Dreier 
2696700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
2706700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
27114e50e57SDavid S. Miller {
27214e50e57SDavid S. Miller }
27314e50e57SDavid S. Miller 
2746700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
2756700c270SDavid S. Miller 				      struct sk_buff *skb)
276b587ee3bSDavid S. Miller {
277b587ee3bSDavid S. Miller }
278b587ee3bSDavid S. Miller 
27914e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
28014e50e57SDavid S. Miller 	.family			=	AF_INET6,
28114e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
28214e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
283ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
284214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
28514e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
286b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2870a1f5962SMartin KaFai Lau 	.cow_metrics		=	dst_cow_metrics_generic,
288f8a1b43bSDavid Ahern 	.neigh_lookup		=	ip6_dst_neigh_lookup,
28914e50e57SDavid S. Miller };
29014e50e57SDavid S. Miller 
29162fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
29214edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
29362fa8a84SDavid S. Miller };
29462fa8a84SDavid S. Miller 
2958d1c802bSDavid Ahern static const struct fib6_info fib6_null_entry_template = {
29693c2fb25SDavid Ahern 	.fib6_flags	= (RTF_REJECT | RTF_NONEXTHOP),
29793c2fb25SDavid Ahern 	.fib6_protocol  = RTPROT_KERNEL,
29893c2fb25SDavid Ahern 	.fib6_metric	= ~(u32)0,
29993c2fb25SDavid Ahern 	.fib6_ref	= ATOMIC_INIT(1),
300421842edSDavid Ahern 	.fib6_type	= RTN_UNREACHABLE,
301421842edSDavid Ahern 	.fib6_metrics	= (struct dst_metrics *)&dst_default_metrics,
302421842edSDavid Ahern };
303421842edSDavid Ahern 
304fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
3051da177e4SLinus Torvalds 	.dst = {
3061da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
3071da177e4SLinus Torvalds 		.__use		= 1,
3082c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
3091da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
3101da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
3111da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
3121da177e4SLinus Torvalds 	},
3131da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3141da177e4SLinus Torvalds };
3151da177e4SLinus Torvalds 
316101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
317101367c2SThomas Graf 
318fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
319101367c2SThomas Graf 	.dst = {
320101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
321101367c2SThomas Graf 		.__use		= 1,
3222c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
323101367c2SThomas Graf 		.error		= -EACCES,
3249ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
3259ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
326101367c2SThomas Graf 	},
327101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
328101367c2SThomas Graf };
329101367c2SThomas Graf 
330fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
331101367c2SThomas Graf 	.dst = {
332101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
333101367c2SThomas Graf 		.__use		= 1,
3342c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
335101367c2SThomas Graf 		.error		= -EINVAL,
336352e512cSHerbert Xu 		.input		= dst_discard,
337ede2059dSEric W. Biederman 		.output		= dst_discard_out,
338101367c2SThomas Graf 	},
339101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
340101367c2SThomas Graf };
341101367c2SThomas Graf 
342101367c2SThomas Graf #endif
343101367c2SThomas Graf 
344ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt)
345ebfa45f0SMartin KaFai Lau {
346ebfa45f0SMartin KaFai Lau 	struct dst_entry *dst = &rt->dst;
347ebfa45f0SMartin KaFai Lau 
348ebfa45f0SMartin KaFai Lau 	memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
349ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_uncached);
350ebfa45f0SMartin KaFai Lau }
351ebfa45f0SMartin KaFai Lau 
3521da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
35393531c67SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
354ad706862SMartin KaFai Lau 			       int flags)
3551da177e4SLinus Torvalds {
35697bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
357b2a9c0edSWei Wang 					1, DST_OBSOLETE_FORCE_CHK, flags);
358cf911662SDavid S. Miller 
35981eb8447SWei Wang 	if (rt) {
360ebfa45f0SMartin KaFai Lau 		rt6_info_init(rt);
36181eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
36281eb8447SWei Wang 	}
3638104891bSSteffen Klassert 
364cf911662SDavid S. Miller 	return rt;
3651da177e4SLinus Torvalds }
3669ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc);
367d52d3997SMartin KaFai Lau 
3681da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
371a68886a6SDavid Ahern 	struct fib6_info *from;
3728d0b94afSMartin KaFai Lau 	struct inet6_dev *idev;
3731da177e4SLinus Torvalds 
3741620a336SDavid Ahern 	ip_dst_metrics_put(dst);
3758d0b94afSMartin KaFai Lau 	rt6_uncached_list_del(rt);
3768d0b94afSMartin KaFai Lau 
3778d0b94afSMartin KaFai Lau 	idev = rt->rt6i_idev;
37838308473SDavid S. Miller 	if (idev) {
3791da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3801da177e4SLinus Torvalds 		in6_dev_put(idev);
3811da177e4SLinus Torvalds 	}
3821716a961SGao feng 
383a68886a6SDavid Ahern 	rcu_read_lock();
384a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
385a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, NULL);
38693531c67SDavid Ahern 	fib6_info_release(from);
387a68886a6SDavid Ahern 	rcu_read_unlock();
388b3419363SDavid S. Miller }
389b3419363SDavid S. Miller 
3901da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3911da177e4SLinus Torvalds 			   int how)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3941da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3955a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
396c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3971da177e4SLinus Torvalds 
398e5645f51SWei Wang 	if (idev && idev->dev != loopback_dev) {
399e5645f51SWei Wang 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
40038308473SDavid S. Miller 		if (loopback_idev) {
4011da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
4021da177e4SLinus Torvalds 			in6_dev_put(idev);
4031da177e4SLinus Torvalds 		}
4041da177e4SLinus Torvalds 	}
40597cac082SDavid S. Miller }
4061da177e4SLinus Torvalds 
4075973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt)
4085973fb1eSMartin KaFai Lau {
4095973fb1eSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_EXPIRES)
4105973fb1eSMartin KaFai Lau 		return time_after(jiffies, rt->dst.expires);
4115973fb1eSMartin KaFai Lau 	else
4125973fb1eSMartin KaFai Lau 		return false;
4135973fb1eSMartin KaFai Lau }
4145973fb1eSMartin KaFai Lau 
415a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
4161da177e4SLinus Torvalds {
417a68886a6SDavid Ahern 	struct fib6_info *from;
418a68886a6SDavid Ahern 
419a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
420a68886a6SDavid Ahern 
4211716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
4221716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
423a50feda5SEric Dumazet 			return true;
424a68886a6SDavid Ahern 	} else if (from) {
4251e2ea8adSXin Long 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
426a68886a6SDavid Ahern 			fib6_check_expired(from);
4271716a961SGao feng 	}
428a50feda5SEric Dumazet 	return false;
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4313b290a31SDavid Ahern struct fib6_info *fib6_multipath_select(const struct net *net,
4328d1c802bSDavid Ahern 					struct fib6_info *match,
43352bd4c0cSNicolas Dichtel 					struct flowi6 *fl6, int oif,
434b75cc8f9SDavid Ahern 					const struct sk_buff *skb,
43552bd4c0cSNicolas Dichtel 					int strict)
43651ebd318SNicolas Dichtel {
4378d1c802bSDavid Ahern 	struct fib6_info *sibling, *next_sibling;
43851ebd318SNicolas Dichtel 
439b673d6ccSJakub Sitnicki 	/* We might have already computed the hash for ICMPv6 errors. In such
440b673d6ccSJakub Sitnicki 	 * case it will always be non-zero. Otherwise now is the time to do it.
441b673d6ccSJakub Sitnicki 	 */
442b673d6ccSJakub Sitnicki 	if (!fl6->mp_hash)
443b4bac172SDavid Ahern 		fl6->mp_hash = rt6_multipath_hash(net, fl6, skb, NULL);
444b673d6ccSJakub Sitnicki 
445ad1601aeSDavid Ahern 	if (fl6->mp_hash <= atomic_read(&match->fib6_nh.fib_nh_upper_bound))
4463d709f69SIdo Schimmel 		return match;
447bbfcd776SIdo Schimmel 
44893c2fb25SDavid Ahern 	list_for_each_entry_safe(sibling, next_sibling, &match->fib6_siblings,
44993c2fb25SDavid Ahern 				 fib6_siblings) {
450*702cea56SDavid Ahern 		const struct fib6_nh *nh = &sibling->fib6_nh;
4515e670d84SDavid Ahern 		int nh_upper_bound;
4525e670d84SDavid Ahern 
453*702cea56SDavid Ahern 		nh_upper_bound = atomic_read(&nh->fib_nh_upper_bound);
4545e670d84SDavid Ahern 		if (fl6->mp_hash > nh_upper_bound)
4553d709f69SIdo Schimmel 			continue;
456*702cea56SDavid Ahern 		if (rt6_score_route(nh, sibling->fib6_flags, oif, strict) < 0)
45752bd4c0cSNicolas Dichtel 			break;
45851ebd318SNicolas Dichtel 		match = sibling;
45951ebd318SNicolas Dichtel 		break;
46051ebd318SNicolas Dichtel 	}
4613d709f69SIdo Schimmel 
46251ebd318SNicolas Dichtel 	return match;
46351ebd318SNicolas Dichtel }
46451ebd318SNicolas Dichtel 
4651da177e4SLinus Torvalds /*
46666f5d6ceSWei Wang  *	Route lookup. rcu_read_lock() should be held.
4671da177e4SLinus Torvalds  */
4681da177e4SLinus Torvalds 
4698d1c802bSDavid Ahern static inline struct fib6_info *rt6_device_match(struct net *net,
4708d1c802bSDavid Ahern 						 struct fib6_info *rt,
471b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4721da177e4SLinus Torvalds 						    int oif,
473d420895eSYOSHIFUJI Hideaki 						    int flags)
4741da177e4SLinus Torvalds {
4758d1c802bSDavid Ahern 	struct fib6_info *sprt;
4761da177e4SLinus Torvalds 
4775e670d84SDavid Ahern 	if (!oif && ipv6_addr_any(saddr) &&
478ad1601aeSDavid Ahern 	    !(rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD))
4798067bb8cSIdo Schimmel 		return rt;
480dd3abc4eSYOSHIFUJI Hideaki 
4818fb11a9aSDavid Ahern 	for (sprt = rt; sprt; sprt = rcu_dereference(sprt->fib6_next)) {
482ad1601aeSDavid Ahern 		const struct net_device *dev = sprt->fib6_nh.fib_nh_dev;
483dd3abc4eSYOSHIFUJI Hideaki 
484ad1601aeSDavid Ahern 		if (sprt->fib6_nh.fib_nh_flags & RTNH_F_DEAD)
4858067bb8cSIdo Schimmel 			continue;
4868067bb8cSIdo Schimmel 
487dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4881da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4891da177e4SLinus Torvalds 				return sprt;
490dd3abc4eSYOSHIFUJI Hideaki 		} else {
491dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
492dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
493dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
494dd3abc4eSYOSHIFUJI Hideaki 		}
4951da177e4SLinus Torvalds 	}
4961da177e4SLinus Torvalds 
497eea68cd3SDavid Ahern 	if (oif && flags & RT6_LOOKUP_F_IFACE)
498421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
4991da177e4SLinus Torvalds 
500ad1601aeSDavid Ahern 	return rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD ? net->ipv6.fib6_null_entry : rt;
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds 
50327097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
504c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
505c2f17e82SHannes Frederic Sowa 	struct work_struct work;
506c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
507c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
508c2f17e82SHannes Frederic Sowa };
509c2f17e82SHannes Frederic Sowa 
510c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
511c2f17e82SHannes Frederic Sowa {
512c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
513c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
514c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
515c2f17e82SHannes Frederic Sowa 
516c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
517adc176c5SErik Nordmark 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
518c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
519662f5533SMichael Büsch 	kfree(work);
520c2f17e82SHannes Frederic Sowa }
521c2f17e82SHannes Frederic Sowa 
522cc3a86c8SDavid Ahern static void rt6_probe(struct fib6_nh *fib6_nh)
52327097255SYOSHIFUJI Hideaki {
524f547fac6SSabrina Dubroca 	struct __rt6_probe_work *work = NULL;
5255e670d84SDavid Ahern 	const struct in6_addr *nh_gw;
526f2c31e32SEric Dumazet 	struct neighbour *neigh;
5275e670d84SDavid Ahern 	struct net_device *dev;
528f547fac6SSabrina Dubroca 	struct inet6_dev *idev;
5295e670d84SDavid Ahern 
53027097255SYOSHIFUJI Hideaki 	/*
53127097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
53227097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
53327097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
53427097255SYOSHIFUJI Hideaki 	 *
53527097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
53627097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
53727097255SYOSHIFUJI Hideaki 	 */
538cc3a86c8SDavid Ahern 	if (fib6_nh->fib_nh_gw_family)
539fdd6681dSAmerigo Wang 		return;
5405e670d84SDavid Ahern 
541cc3a86c8SDavid Ahern 	nh_gw = &fib6_nh->fib_nh_gw6;
542cc3a86c8SDavid Ahern 	dev = fib6_nh->fib_nh_dev;
5432152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
544f547fac6SSabrina Dubroca 	idev = __in6_dev_get(dev);
5455e670d84SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(dev, nh_gw);
5462152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
5478d6c31bfSMartin KaFai Lau 		if (neigh->nud_state & NUD_VALID)
5488d6c31bfSMartin KaFai Lau 			goto out;
5498d6c31bfSMartin KaFai Lau 
5502152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
551990edb42SMartin KaFai Lau 		if (!(neigh->nud_state & NUD_VALID) &&
552990edb42SMartin KaFai Lau 		    time_after(jiffies,
553dcd1f572SDavid Ahern 			       neigh->updated + idev->cnf.rtr_probe_interval)) {
554c2f17e82SHannes Frederic Sowa 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
555990edb42SMartin KaFai Lau 			if (work)
5567e980569SJiri Benc 				__neigh_set_probe_once(neigh);
557990edb42SMartin KaFai Lau 		}
558c2f17e82SHannes Frederic Sowa 		write_unlock(&neigh->lock);
559cc3a86c8SDavid Ahern 	} else if (time_after(jiffies, fib6_nh->last_probe +
560f547fac6SSabrina Dubroca 				       idev->cnf.rtr_probe_interval)) {
561990edb42SMartin KaFai Lau 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
562990edb42SMartin KaFai Lau 	}
563c2f17e82SHannes Frederic Sowa 
564c2f17e82SHannes Frederic Sowa 	if (work) {
565cc3a86c8SDavid Ahern 		fib6_nh->last_probe = jiffies;
566c2f17e82SHannes Frederic Sowa 		INIT_WORK(&work->work, rt6_probe_deferred);
5675e670d84SDavid Ahern 		work->target = *nh_gw;
5685e670d84SDavid Ahern 		dev_hold(dev);
5695e670d84SDavid Ahern 		work->dev = dev;
570c2f17e82SHannes Frederic Sowa 		schedule_work(&work->work);
571c2f17e82SHannes Frederic Sowa 	}
572990edb42SMartin KaFai Lau 
5738d6c31bfSMartin KaFai Lau out:
5742152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
575f2c31e32SEric Dumazet }
57627097255SYOSHIFUJI Hideaki #else
577cc3a86c8SDavid Ahern static inline void rt6_probe(struct fib6_nh *fib6_nh)
57827097255SYOSHIFUJI Hideaki {
57927097255SYOSHIFUJI Hideaki }
58027097255SYOSHIFUJI Hideaki #endif
58127097255SYOSHIFUJI Hideaki 
5821da177e4SLinus Torvalds /*
583554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5841da177e4SLinus Torvalds  */
5851ba9a895SDavid Ahern static enum rt6_nud_state rt6_check_neigh(const struct fib6_nh *fib6_nh)
5861da177e4SLinus Torvalds {
587afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
5885e670d84SDavid Ahern 	struct neighbour *neigh;
589f2c31e32SEric Dumazet 
590145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
5911ba9a895SDavid Ahern 	neigh = __ipv6_neigh_lookup_noref(fib6_nh->fib_nh_dev,
5921ba9a895SDavid Ahern 					  &fib6_nh->fib_nh_gw6);
593145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
594145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
595554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
596afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
597398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
598a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
599afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
6007e980569SJiri Benc 		else
6017e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
602398bcbebSYOSHIFUJI Hideaki #endif
603145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
604afc154e9SHannes Frederic Sowa 	} else {
605afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
6067e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
607a5a81f0bSPaul Marks 	}
608145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
609145a3621SYOSHIFUJI Hideaki / 吉藤英明 
610a5a81f0bSPaul Marks 	return ret;
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
613*702cea56SDavid Ahern static int rt6_score_route(const struct fib6_nh *nh, u32 fib6_flags, int oif,
614*702cea56SDavid Ahern 			   int strict)
615554cfb7eSYOSHIFUJI Hideaki {
6166e1809a5SDavid Ahern 	int m = 0;
6174d0c5911SYOSHIFUJI Hideaki 
6186e1809a5SDavid Ahern 	if (!oif || nh->fib_nh_dev->ifindex == oif)
6196e1809a5SDavid Ahern 		m = 2;
6206e1809a5SDavid Ahern 
62177d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
622afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
623ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
624*702cea56SDavid Ahern 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(fib6_flags)) << 2;
625ebacaaa0SYOSHIFUJI Hideaki #endif
6261ba9a895SDavid Ahern 	if ((strict & RT6_LOOKUP_F_REACHABLE) &&
627*702cea56SDavid Ahern 	    !(fib6_flags & RTF_NONEXTHOP) && nh->fib_nh_gw_family) {
6281ba9a895SDavid Ahern 		int n = rt6_check_neigh(nh);
629afc154e9SHannes Frederic Sowa 		if (n < 0)
630afc154e9SHannes Frederic Sowa 			return n;
631afc154e9SHannes Frederic Sowa 	}
632554cfb7eSYOSHIFUJI Hideaki 	return m;
633554cfb7eSYOSHIFUJI Hideaki }
634554cfb7eSYOSHIFUJI Hideaki 
6358d1c802bSDavid Ahern static struct fib6_info *find_match(struct fib6_info *rt, int oif, int strict,
6368d1c802bSDavid Ahern 				   int *mpri, struct fib6_info *match,
637afc154e9SHannes Frederic Sowa 				   bool *do_rr)
638554cfb7eSYOSHIFUJI Hideaki {
639554cfb7eSYOSHIFUJI Hideaki 	int m;
640afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
64135103d11SAndy Gospodarek 
642ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD)
6438067bb8cSIdo Schimmel 		goto out;
6448067bb8cSIdo Schimmel 
645ad1601aeSDavid Ahern 	if (ip6_ignore_linkdown(rt->fib6_nh.fib_nh_dev) &&
646ad1601aeSDavid Ahern 	    rt->fib6_nh.fib_nh_flags & RTNH_F_LINKDOWN &&
647d5d32e4bSDavid Ahern 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
64835103d11SAndy Gospodarek 		goto out;
649554cfb7eSYOSHIFUJI Hideaki 
65014895687SDavid Ahern 	if (fib6_check_expired(rt))
651f11e6659SDavid S. Miller 		goto out;
652554cfb7eSYOSHIFUJI Hideaki 
653*702cea56SDavid Ahern 	m = rt6_score_route(&rt->fib6_nh, rt->fib6_flags, oif, strict);
6547e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
655afc154e9SHannes Frederic Sowa 		match_do_rr = true;
656afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
6577e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
658f11e6659SDavid S. Miller 		goto out;
6591da177e4SLinus Torvalds 	}
660f11e6659SDavid S. Miller 
661afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
662cc3a86c8SDavid Ahern 		rt6_probe(&rt->fib6_nh);
663afc154e9SHannes Frederic Sowa 
6647e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
665afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
666afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
667afc154e9SHannes Frederic Sowa 		*mpri = m;
668afc154e9SHannes Frederic Sowa 		match = rt;
669afc154e9SHannes Frederic Sowa 	}
670f11e6659SDavid S. Miller out:
671f11e6659SDavid S. Miller 	return match;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
6748d1c802bSDavid Ahern static struct fib6_info *find_rr_leaf(struct fib6_node *fn,
6758d1c802bSDavid Ahern 				     struct fib6_info *leaf,
6768d1c802bSDavid Ahern 				     struct fib6_info *rr_head,
677afc154e9SHannes Frederic Sowa 				     u32 metric, int oif, int strict,
678afc154e9SHannes Frederic Sowa 				     bool *do_rr)
679f11e6659SDavid S. Miller {
6808d1c802bSDavid Ahern 	struct fib6_info *rt, *match, *cont;
681f11e6659SDavid S. Miller 	int mpri = -1;
682f11e6659SDavid S. Miller 
683f11e6659SDavid S. Miller 	match = NULL;
6849fbdcfafSSteffen Klassert 	cont = NULL;
6858fb11a9aSDavid Ahern 	for (rt = rr_head; rt; rt = rcu_dereference(rt->fib6_next)) {
68693c2fb25SDavid Ahern 		if (rt->fib6_metric != metric) {
6879fbdcfafSSteffen Klassert 			cont = rt;
6889fbdcfafSSteffen Klassert 			break;
6899fbdcfafSSteffen Klassert 		}
6909fbdcfafSSteffen Klassert 
691afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
6929fbdcfafSSteffen Klassert 	}
6939fbdcfafSSteffen Klassert 
69466f5d6ceSWei Wang 	for (rt = leaf; rt && rt != rr_head;
6958fb11a9aSDavid Ahern 	     rt = rcu_dereference(rt->fib6_next)) {
69693c2fb25SDavid Ahern 		if (rt->fib6_metric != metric) {
6979fbdcfafSSteffen Klassert 			cont = rt;
6989fbdcfafSSteffen Klassert 			break;
6999fbdcfafSSteffen Klassert 		}
7009fbdcfafSSteffen Klassert 
7019fbdcfafSSteffen Klassert 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
7029fbdcfafSSteffen Klassert 	}
7039fbdcfafSSteffen Klassert 
7049fbdcfafSSteffen Klassert 	if (match || !cont)
7059fbdcfafSSteffen Klassert 		return match;
7069fbdcfafSSteffen Klassert 
7078fb11a9aSDavid Ahern 	for (rt = cont; rt; rt = rcu_dereference(rt->fib6_next))
708afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
709f11e6659SDavid S. Miller 
710f11e6659SDavid S. Miller 	return match;
711f11e6659SDavid S. Miller }
712f11e6659SDavid S. Miller 
7138d1c802bSDavid Ahern static struct fib6_info *rt6_select(struct net *net, struct fib6_node *fn,
7148d1040e8SWei Wang 				   int oif, int strict)
715f11e6659SDavid S. Miller {
7168d1c802bSDavid Ahern 	struct fib6_info *leaf = rcu_dereference(fn->leaf);
7178d1c802bSDavid Ahern 	struct fib6_info *match, *rt0;
718afc154e9SHannes Frederic Sowa 	bool do_rr = false;
71917ecf590SWei Wang 	int key_plen;
720f11e6659SDavid S. Miller 
721421842edSDavid Ahern 	if (!leaf || leaf == net->ipv6.fib6_null_entry)
722421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
7238d1040e8SWei Wang 
72466f5d6ceSWei Wang 	rt0 = rcu_dereference(fn->rr_ptr);
725f11e6659SDavid S. Miller 	if (!rt0)
72666f5d6ceSWei Wang 		rt0 = leaf;
727f11e6659SDavid S. Miller 
72817ecf590SWei Wang 	/* Double check to make sure fn is not an intermediate node
72917ecf590SWei Wang 	 * and fn->leaf does not points to its child's leaf
73017ecf590SWei Wang 	 * (This might happen if all routes under fn are deleted from
73117ecf590SWei Wang 	 * the tree and fib6_repair_tree() is called on the node.)
73217ecf590SWei Wang 	 */
73393c2fb25SDavid Ahern 	key_plen = rt0->fib6_dst.plen;
73417ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES
73593c2fb25SDavid Ahern 	if (rt0->fib6_src.plen)
73693c2fb25SDavid Ahern 		key_plen = rt0->fib6_src.plen;
73717ecf590SWei Wang #endif
73817ecf590SWei Wang 	if (fn->fn_bit != key_plen)
739421842edSDavid Ahern 		return net->ipv6.fib6_null_entry;
74017ecf590SWei Wang 
74193c2fb25SDavid Ahern 	match = find_rr_leaf(fn, leaf, rt0, rt0->fib6_metric, oif, strict,
742afc154e9SHannes Frederic Sowa 			     &do_rr);
743f11e6659SDavid S. Miller 
744afc154e9SHannes Frederic Sowa 	if (do_rr) {
7458fb11a9aSDavid Ahern 		struct fib6_info *next = rcu_dereference(rt0->fib6_next);
746f11e6659SDavid S. Miller 
747554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
74893c2fb25SDavid Ahern 		if (!next || next->fib6_metric != rt0->fib6_metric)
7498d1040e8SWei Wang 			next = leaf;
750f11e6659SDavid S. Miller 
75166f5d6ceSWei Wang 		if (next != rt0) {
75293c2fb25SDavid Ahern 			spin_lock_bh(&leaf->fib6_table->tb6_lock);
75366f5d6ceSWei Wang 			/* make sure next is not being deleted from the tree */
75493c2fb25SDavid Ahern 			if (next->fib6_node)
75566f5d6ceSWei Wang 				rcu_assign_pointer(fn->rr_ptr, next);
75693c2fb25SDavid Ahern 			spin_unlock_bh(&leaf->fib6_table->tb6_lock);
75766f5d6ceSWei Wang 		}
758554cfb7eSYOSHIFUJI Hideaki 	}
759554cfb7eSYOSHIFUJI Hideaki 
760421842edSDavid Ahern 	return match ? match : net->ipv6.fib6_null_entry;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
7638d1c802bSDavid Ahern static bool rt6_is_gw_or_nonexthop(const struct fib6_info *rt)
7648b9df265SMartin KaFai Lau {
765bdf00467SDavid Ahern 	return (rt->fib6_flags & RTF_NONEXTHOP) || rt->fib6_nh.fib_nh_gw_family;
7668b9df265SMartin KaFai Lau }
7678b9df265SMartin KaFai Lau 
76870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
76970ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
770b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
77170ceb4f5SYOSHIFUJI Hideaki {
772c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
77370ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
77470ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
77570ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
7764bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
7778d1c802bSDavid Ahern 	struct fib6_info *rt;
77870ceb4f5SYOSHIFUJI Hideaki 
77970ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
78070ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
78170ceb4f5SYOSHIFUJI Hideaki 	}
78270ceb4f5SYOSHIFUJI Hideaki 
78370ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
78470ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
78570ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
78670ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
78770ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
78870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
78970ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
79070ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
79170ceb4f5SYOSHIFUJI Hideaki 		}
79270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
79370ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
79470ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
79570ceb4f5SYOSHIFUJI Hideaki 		}
79670ceb4f5SYOSHIFUJI Hideaki 	}
79770ceb4f5SYOSHIFUJI Hideaki 
79870ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
79970ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
8003933fc95SJens Rosenboom 		return -EINVAL;
80170ceb4f5SYOSHIFUJI Hideaki 
8024bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
80370ceb4f5SYOSHIFUJI Hideaki 
80470ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
80570ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
80670ceb4f5SYOSHIFUJI Hideaki 	else {
80770ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
80870ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
80970ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
81070ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
81170ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
81270ceb4f5SYOSHIFUJI Hideaki 	}
81370ceb4f5SYOSHIFUJI Hideaki 
814f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
815afb1d4b5SDavid Ahern 		rt = rt6_get_dflt_router(net, gwaddr, dev);
816f104a567SDuan Jiong 	else
817f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
818830218c1SDavid Ahern 					gwaddr, dev);
81970ceb4f5SYOSHIFUJI Hideaki 
82070ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
821afb1d4b5SDavid Ahern 		ip6_del_rt(net, rt);
82270ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
82370ceb4f5SYOSHIFUJI Hideaki 	}
82470ceb4f5SYOSHIFUJI Hideaki 
82570ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
826830218c1SDavid Ahern 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
827830218c1SDavid Ahern 					dev, pref);
82870ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
82993c2fb25SDavid Ahern 		rt->fib6_flags = RTF_ROUTEINFO |
83093c2fb25SDavid Ahern 				 (rt->fib6_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
83170ceb4f5SYOSHIFUJI Hideaki 
83270ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
8331716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
83414895687SDavid Ahern 			fib6_clean_expires(rt);
8351716a961SGao feng 		else
83614895687SDavid Ahern 			fib6_set_expires(rt, jiffies + HZ * lifetime);
8371716a961SGao feng 
83893531c67SDavid Ahern 		fib6_info_release(rt);
83970ceb4f5SYOSHIFUJI Hideaki 	}
84070ceb4f5SYOSHIFUJI Hideaki 	return 0;
84170ceb4f5SYOSHIFUJI Hideaki }
84270ceb4f5SYOSHIFUJI Hideaki #endif
84370ceb4f5SYOSHIFUJI Hideaki 
844ae90d867SDavid Ahern /*
845ae90d867SDavid Ahern  *	Misc support functions
846ae90d867SDavid Ahern  */
847ae90d867SDavid Ahern 
848ae90d867SDavid Ahern /* called with rcu_lock held */
8498d1c802bSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct fib6_info *rt)
850ae90d867SDavid Ahern {
851ad1601aeSDavid Ahern 	struct net_device *dev = rt->fib6_nh.fib_nh_dev;
852ae90d867SDavid Ahern 
85393c2fb25SDavid Ahern 	if (rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) {
854ae90d867SDavid Ahern 		/* for copies of local routes, dst->dev needs to be the
855ae90d867SDavid Ahern 		 * device if it is a master device, the master device if
856ae90d867SDavid Ahern 		 * device is enslaved, and the loopback as the default
857ae90d867SDavid Ahern 		 */
858ae90d867SDavid Ahern 		if (netif_is_l3_slave(dev) &&
85993c2fb25SDavid Ahern 		    !rt6_need_strict(&rt->fib6_dst.addr))
860ae90d867SDavid Ahern 			dev = l3mdev_master_dev_rcu(dev);
861ae90d867SDavid Ahern 		else if (!netif_is_l3_master(dev))
862ae90d867SDavid Ahern 			dev = dev_net(dev)->loopback_dev;
863ae90d867SDavid Ahern 		/* last case is netif_is_l3_master(dev) is true in which
864ae90d867SDavid Ahern 		 * case we want dev returned to be dev
865ae90d867SDavid Ahern 		 */
866ae90d867SDavid Ahern 	}
867ae90d867SDavid Ahern 
868ae90d867SDavid Ahern 	return dev;
869ae90d867SDavid Ahern }
870ae90d867SDavid Ahern 
8716edb3c96SDavid Ahern static const int fib6_prop[RTN_MAX + 1] = {
8726edb3c96SDavid Ahern 	[RTN_UNSPEC]	= 0,
8736edb3c96SDavid Ahern 	[RTN_UNICAST]	= 0,
8746edb3c96SDavid Ahern 	[RTN_LOCAL]	= 0,
8756edb3c96SDavid Ahern 	[RTN_BROADCAST]	= 0,
8766edb3c96SDavid Ahern 	[RTN_ANYCAST]	= 0,
8776edb3c96SDavid Ahern 	[RTN_MULTICAST]	= 0,
8786edb3c96SDavid Ahern 	[RTN_BLACKHOLE]	= -EINVAL,
8796edb3c96SDavid Ahern 	[RTN_UNREACHABLE] = -EHOSTUNREACH,
8806edb3c96SDavid Ahern 	[RTN_PROHIBIT]	= -EACCES,
8816edb3c96SDavid Ahern 	[RTN_THROW]	= -EAGAIN,
8826edb3c96SDavid Ahern 	[RTN_NAT]	= -EINVAL,
8836edb3c96SDavid Ahern 	[RTN_XRESOLVE]	= -EINVAL,
8846edb3c96SDavid Ahern };
8856edb3c96SDavid Ahern 
8866edb3c96SDavid Ahern static int ip6_rt_type_to_error(u8 fib6_type)
8876edb3c96SDavid Ahern {
8886edb3c96SDavid Ahern 	return fib6_prop[fib6_type];
8896edb3c96SDavid Ahern }
8906edb3c96SDavid Ahern 
8918d1c802bSDavid Ahern static unsigned short fib6_info_dst_flags(struct fib6_info *rt)
8923b6761d1SDavid Ahern {
8933b6761d1SDavid Ahern 	unsigned short flags = 0;
8943b6761d1SDavid Ahern 
8953b6761d1SDavid Ahern 	if (rt->dst_nocount)
8963b6761d1SDavid Ahern 		flags |= DST_NOCOUNT;
8973b6761d1SDavid Ahern 	if (rt->dst_nopolicy)
8983b6761d1SDavid Ahern 		flags |= DST_NOPOLICY;
8993b6761d1SDavid Ahern 	if (rt->dst_host)
9003b6761d1SDavid Ahern 		flags |= DST_HOST;
9013b6761d1SDavid Ahern 
9023b6761d1SDavid Ahern 	return flags;
9033b6761d1SDavid Ahern }
9043b6761d1SDavid Ahern 
9058d1c802bSDavid Ahern static void ip6_rt_init_dst_reject(struct rt6_info *rt, struct fib6_info *ort)
9066edb3c96SDavid Ahern {
9076edb3c96SDavid Ahern 	rt->dst.error = ip6_rt_type_to_error(ort->fib6_type);
9086edb3c96SDavid Ahern 
9096edb3c96SDavid Ahern 	switch (ort->fib6_type) {
9106edb3c96SDavid Ahern 	case RTN_BLACKHOLE:
9116edb3c96SDavid Ahern 		rt->dst.output = dst_discard_out;
9126edb3c96SDavid Ahern 		rt->dst.input = dst_discard;
9136edb3c96SDavid Ahern 		break;
9146edb3c96SDavid Ahern 	case RTN_PROHIBIT:
9156edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_prohibit_out;
9166edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_prohibit;
9176edb3c96SDavid Ahern 		break;
9186edb3c96SDavid Ahern 	case RTN_THROW:
9196edb3c96SDavid Ahern 	case RTN_UNREACHABLE:
9206edb3c96SDavid Ahern 	default:
9216edb3c96SDavid Ahern 		rt->dst.output = ip6_pkt_discard_out;
9226edb3c96SDavid Ahern 		rt->dst.input = ip6_pkt_discard;
9236edb3c96SDavid Ahern 		break;
9246edb3c96SDavid Ahern 	}
9256edb3c96SDavid Ahern }
9266edb3c96SDavid Ahern 
9278d1c802bSDavid Ahern static void ip6_rt_init_dst(struct rt6_info *rt, struct fib6_info *ort)
9286edb3c96SDavid Ahern {
92993c2fb25SDavid Ahern 	if (ort->fib6_flags & RTF_REJECT) {
9306edb3c96SDavid Ahern 		ip6_rt_init_dst_reject(rt, ort);
9316edb3c96SDavid Ahern 		return;
9326edb3c96SDavid Ahern 	}
9336edb3c96SDavid Ahern 
9346edb3c96SDavid Ahern 	rt->dst.error = 0;
9356edb3c96SDavid Ahern 	rt->dst.output = ip6_output;
9366edb3c96SDavid Ahern 
937d23c4b63SHangbin Liu 	if (ort->fib6_type == RTN_LOCAL || ort->fib6_type == RTN_ANYCAST) {
9386edb3c96SDavid Ahern 		rt->dst.input = ip6_input;
93993c2fb25SDavid Ahern 	} else if (ipv6_addr_type(&ort->fib6_dst.addr) & IPV6_ADDR_MULTICAST) {
9406edb3c96SDavid Ahern 		rt->dst.input = ip6_mc_input;
9416edb3c96SDavid Ahern 	} else {
9426edb3c96SDavid Ahern 		rt->dst.input = ip6_forward;
9436edb3c96SDavid Ahern 	}
9446edb3c96SDavid Ahern 
945ad1601aeSDavid Ahern 	if (ort->fib6_nh.fib_nh_lws) {
946ad1601aeSDavid Ahern 		rt->dst.lwtstate = lwtstate_get(ort->fib6_nh.fib_nh_lws);
9476edb3c96SDavid Ahern 		lwtunnel_set_redirect(&rt->dst);
9486edb3c96SDavid Ahern 	}
9496edb3c96SDavid Ahern 
9506edb3c96SDavid Ahern 	rt->dst.lastuse = jiffies;
9516edb3c96SDavid Ahern }
9526edb3c96SDavid Ahern 
953e873e4b9SWei Wang /* Caller must already hold reference to @from */
9548d1c802bSDavid Ahern static void rt6_set_from(struct rt6_info *rt, struct fib6_info *from)
955ae90d867SDavid Ahern {
956ae90d867SDavid Ahern 	rt->rt6i_flags &= ~RTF_EXPIRES;
957a68886a6SDavid Ahern 	rcu_assign_pointer(rt->from, from);
958e1255ed4SDavid Ahern 	ip_dst_init_metrics(&rt->dst, from->fib6_metrics);
959ae90d867SDavid Ahern }
960ae90d867SDavid Ahern 
961e873e4b9SWei Wang /* Caller must already hold reference to @ort */
9628d1c802bSDavid Ahern static void ip6_rt_copy_init(struct rt6_info *rt, struct fib6_info *ort)
963ae90d867SDavid Ahern {
964dcd1f572SDavid Ahern 	struct net_device *dev = fib6_info_nh_dev(ort);
965dcd1f572SDavid Ahern 
9666edb3c96SDavid Ahern 	ip6_rt_init_dst(rt, ort);
9676edb3c96SDavid Ahern 
96893c2fb25SDavid Ahern 	rt->rt6i_dst = ort->fib6_dst;
969dcd1f572SDavid Ahern 	rt->rt6i_idev = dev ? in6_dev_get(dev) : NULL;
97093c2fb25SDavid Ahern 	rt->rt6i_flags = ort->fib6_flags;
971bdf00467SDavid Ahern 	if (ort->fib6_nh.fib_nh_gw_family) {
972ad1601aeSDavid Ahern 		rt->rt6i_gateway = ort->fib6_nh.fib_nh_gw6;
9732b2450caSDavid Ahern 		rt->rt6i_flags |= RTF_GATEWAY;
9742b2450caSDavid Ahern 	}
975ae90d867SDavid Ahern 	rt6_set_from(rt, ort);
976ae90d867SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
97793c2fb25SDavid Ahern 	rt->rt6i_src = ort->fib6_src;
978ae90d867SDavid Ahern #endif
979ae90d867SDavid Ahern }
980ae90d867SDavid Ahern 
981a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
982a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
983a3c00e46SMartin KaFai Lau {
98466f5d6ceSWei Wang 	struct fib6_node *pn, *sn;
985a3c00e46SMartin KaFai Lau 	while (1) {
986a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
987a3c00e46SMartin KaFai Lau 			return NULL;
98866f5d6ceSWei Wang 		pn = rcu_dereference(fn->parent);
98966f5d6ceSWei Wang 		sn = FIB6_SUBTREE(pn);
99066f5d6ceSWei Wang 		if (sn && sn != fn)
9916454743bSDavid Ahern 			fn = fib6_node_lookup(sn, NULL, saddr);
992a3c00e46SMartin KaFai Lau 		else
993a3c00e46SMartin KaFai Lau 			fn = pn;
994a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
995a3c00e46SMartin KaFai Lau 			return fn;
996a3c00e46SMartin KaFai Lau 	}
997a3c00e46SMartin KaFai Lau }
998c71099acSThomas Graf 
99910585b43SDavid Ahern static bool ip6_hold_safe(struct net *net, struct rt6_info **prt)
1000d3843fe5SWei Wang {
1001d3843fe5SWei Wang 	struct rt6_info *rt = *prt;
1002d3843fe5SWei Wang 
1003d3843fe5SWei Wang 	if (dst_hold_safe(&rt->dst))
1004d3843fe5SWei Wang 		return true;
100510585b43SDavid Ahern 	if (net) {
1006d3843fe5SWei Wang 		rt = net->ipv6.ip6_null_entry;
1007d3843fe5SWei Wang 		dst_hold(&rt->dst);
1008d3843fe5SWei Wang 	} else {
1009d3843fe5SWei Wang 		rt = NULL;
1010d3843fe5SWei Wang 	}
1011d3843fe5SWei Wang 	*prt = rt;
1012d3843fe5SWei Wang 	return false;
1013d3843fe5SWei Wang }
1014d3843fe5SWei Wang 
1015dec9b0e2SDavid Ahern /* called with rcu_lock held */
10168d1c802bSDavid Ahern static struct rt6_info *ip6_create_rt_rcu(struct fib6_info *rt)
1017dec9b0e2SDavid Ahern {
10183b6761d1SDavid Ahern 	unsigned short flags = fib6_info_dst_flags(rt);
1019ad1601aeSDavid Ahern 	struct net_device *dev = rt->fib6_nh.fib_nh_dev;
1020dec9b0e2SDavid Ahern 	struct rt6_info *nrt;
1021dec9b0e2SDavid Ahern 
1022e873e4b9SWei Wang 	if (!fib6_info_hold_safe(rt))
10231c87e79aSXin Long 		goto fallback;
1024e873e4b9SWei Wang 
102593531c67SDavid Ahern 	nrt = ip6_dst_alloc(dev_net(dev), dev, flags);
10261c87e79aSXin Long 	if (!nrt) {
1027e873e4b9SWei Wang 		fib6_info_release(rt);
10281c87e79aSXin Long 		goto fallback;
10291c87e79aSXin Long 	}
1030dec9b0e2SDavid Ahern 
10311c87e79aSXin Long 	ip6_rt_copy_init(nrt, rt);
10321c87e79aSXin Long 	return nrt;
10331c87e79aSXin Long 
10341c87e79aSXin Long fallback:
10351c87e79aSXin Long 	nrt = dev_net(dev)->ipv6.ip6_null_entry;
10361c87e79aSXin Long 	dst_hold(&nrt->dst);
1037dec9b0e2SDavid Ahern 	return nrt;
1038dec9b0e2SDavid Ahern }
1039dec9b0e2SDavid Ahern 
10408ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
10418ed67789SDaniel Lezcano 					     struct fib6_table *table,
1042b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
1043b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
1044b75cc8f9SDavid Ahern 					     int flags)
10451da177e4SLinus Torvalds {
10468d1c802bSDavid Ahern 	struct fib6_info *f6i;
10471da177e4SLinus Torvalds 	struct fib6_node *fn;
104823fb93a4SDavid Ahern 	struct rt6_info *rt;
10491da177e4SLinus Torvalds 
1050b6cdbc85SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1051b6cdbc85SDavid Ahern 		flags &= ~RT6_LOOKUP_F_IFACE;
1052b6cdbc85SDavid Ahern 
105366f5d6ceSWei Wang 	rcu_read_lock();
10546454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1055c71099acSThomas Graf restart:
105623fb93a4SDavid Ahern 	f6i = rcu_dereference(fn->leaf);
105723fb93a4SDavid Ahern 	if (!f6i) {
105823fb93a4SDavid Ahern 		f6i = net->ipv6.fib6_null_entry;
105966f5d6ceSWei Wang 	} else {
106023fb93a4SDavid Ahern 		f6i = rt6_device_match(net, f6i, &fl6->saddr,
106166f5d6ceSWei Wang 				      fl6->flowi6_oif, flags);
106293c2fb25SDavid Ahern 		if (f6i->fib6_nsiblings && fl6->flowi6_oif == 0)
10633b290a31SDavid Ahern 			f6i = fib6_multipath_select(net, f6i, fl6,
10643b290a31SDavid Ahern 						    fl6->flowi6_oif, skb,
10653b290a31SDavid Ahern 						    flags);
106666f5d6ceSWei Wang 	}
106723fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1068a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1069a3c00e46SMartin KaFai Lau 		if (fn)
1070a3c00e46SMartin KaFai Lau 			goto restart;
1071a3c00e46SMartin KaFai Lau 	}
10722b760fcfSWei Wang 
1073d4bea421SDavid Ahern 	trace_fib6_table_lookup(net, f6i, table, fl6);
1074d4bea421SDavid Ahern 
10754c9483b2SDavid S. Miller 	/* Search through exception table */
107623fb93a4SDavid Ahern 	rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
107723fb93a4SDavid Ahern 	if (rt) {
107810585b43SDavid Ahern 		if (ip6_hold_safe(net, &rt))
1079d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
108023fb93a4SDavid Ahern 	} else if (f6i == net->ipv6.fib6_null_entry) {
1081dec9b0e2SDavid Ahern 		rt = net->ipv6.ip6_null_entry;
1082dec9b0e2SDavid Ahern 		dst_hold(&rt->dst);
108323fb93a4SDavid Ahern 	} else {
108423fb93a4SDavid Ahern 		rt = ip6_create_rt_rcu(f6i);
1085dec9b0e2SDavid Ahern 	}
1086d3843fe5SWei Wang 
108766f5d6ceSWei Wang 	rcu_read_unlock();
1088b811580dSDavid Ahern 
10891da177e4SLinus Torvalds 	return rt;
1090c71099acSThomas Graf }
1091c71099acSThomas Graf 
1092ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
1093b75cc8f9SDavid Ahern 				   const struct sk_buff *skb, int flags)
1094ea6e574eSFlorian Westphal {
1095b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_lookup);
1096ea6e574eSFlorian Westphal }
1097ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
1098ea6e574eSFlorian Westphal 
10999acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
1100b75cc8f9SDavid Ahern 			    const struct in6_addr *saddr, int oif,
1101b75cc8f9SDavid Ahern 			    const struct sk_buff *skb, int strict)
1102c71099acSThomas Graf {
11034c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
11044c9483b2SDavid S. Miller 		.flowi6_oif = oif,
11054c9483b2SDavid S. Miller 		.daddr = *daddr,
1106c71099acSThomas Graf 	};
1107c71099acSThomas Graf 	struct dst_entry *dst;
110877d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
1109c71099acSThomas Graf 
1110adaa70bbSThomas Graf 	if (saddr) {
11114c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
1112adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1113adaa70bbSThomas Graf 	}
1114adaa70bbSThomas Graf 
1115b75cc8f9SDavid Ahern 	dst = fib6_rule_lookup(net, &fl6, skb, flags, ip6_pol_route_lookup);
1116c71099acSThomas Graf 	if (dst->error == 0)
1117c71099acSThomas Graf 		return (struct rt6_info *) dst;
1118c71099acSThomas Graf 
1119c71099acSThomas Graf 	dst_release(dst);
1120c71099acSThomas Graf 
11211da177e4SLinus Torvalds 	return NULL;
11221da177e4SLinus Torvalds }
11237159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
11247159039aSYOSHIFUJI Hideaki 
1125c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
11261cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
11271cfb71eeSWei Wang  * route is released.
11281cfb71eeSWei Wang  * Caller must hold dst before calling it.
11291da177e4SLinus Torvalds  */
11301da177e4SLinus Torvalds 
11318d1c802bSDavid Ahern static int __ip6_ins_rt(struct fib6_info *rt, struct nl_info *info,
1132333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
11331da177e4SLinus Torvalds {
11341da177e4SLinus Torvalds 	int err;
1135c71099acSThomas Graf 	struct fib6_table *table;
11361da177e4SLinus Torvalds 
113793c2fb25SDavid Ahern 	table = rt->fib6_table;
113866f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
1139d4ead6b3SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, extack);
114066f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	return err;
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
11458d1c802bSDavid Ahern int ip6_ins_rt(struct net *net, struct fib6_info *rt)
114640e22e8fSThomas Graf {
1147afb1d4b5SDavid Ahern 	struct nl_info info = {	.nl_net = net, };
1148e715b6d3SFlorian Westphal 
1149d4ead6b3SDavid Ahern 	return __ip6_ins_rt(rt, &info, NULL);
115040e22e8fSThomas Graf }
115140e22e8fSThomas Graf 
11528d1c802bSDavid Ahern static struct rt6_info *ip6_rt_cache_alloc(struct fib6_info *ort,
115321efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
1154b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
11551da177e4SLinus Torvalds {
11564832c30dSDavid Ahern 	struct net_device *dev;
11571da177e4SLinus Torvalds 	struct rt6_info *rt;
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	/*
11601da177e4SLinus Torvalds 	 *	Clone the route.
11611da177e4SLinus Torvalds 	 */
11621da177e4SLinus Torvalds 
1163e873e4b9SWei Wang 	if (!fib6_info_hold_safe(ort))
1164e873e4b9SWei Wang 		return NULL;
1165e873e4b9SWei Wang 
11664832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(ort);
116793531c67SDavid Ahern 	rt = ip6_dst_alloc(dev_net(dev), dev, 0);
1168e873e4b9SWei Wang 	if (!rt) {
1169e873e4b9SWei Wang 		fib6_info_release(ort);
117083a09abdSMartin KaFai Lau 		return NULL;
1171e873e4b9SWei Wang 	}
117283a09abdSMartin KaFai Lau 
117383a09abdSMartin KaFai Lau 	ip6_rt_copy_init(rt, ort);
11748b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
117583a09abdSMartin KaFai Lau 	rt->dst.flags |= DST_HOST;
117683a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
117783a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
11788b9df265SMartin KaFai Lau 
11798b9df265SMartin KaFai Lau 	if (!rt6_is_gw_or_nonexthop(ort)) {
118093c2fb25SDavid Ahern 		if (ort->fib6_dst.plen != 128 &&
118193c2fb25SDavid Ahern 		    ipv6_addr_equal(&ort->fib6_dst.addr, daddr))
118258c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
11831da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
11841da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
11854e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
11861da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
11871da177e4SLinus Torvalds 		}
11881da177e4SLinus Torvalds #endif
118995a9a5baSYOSHIFUJI Hideaki 	}
119095a9a5baSYOSHIFUJI Hideaki 
1191299d9939SYOSHIFUJI Hideaki 	return rt;
1192299d9939SYOSHIFUJI Hideaki }
1193299d9939SYOSHIFUJI Hideaki 
11948d1c802bSDavid Ahern static struct rt6_info *ip6_rt_pcpu_alloc(struct fib6_info *rt)
1195d52d3997SMartin KaFai Lau {
11963b6761d1SDavid Ahern 	unsigned short flags = fib6_info_dst_flags(rt);
11974832c30dSDavid Ahern 	struct net_device *dev;
1198d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1199d52d3997SMartin KaFai Lau 
1200e873e4b9SWei Wang 	if (!fib6_info_hold_safe(rt))
1201e873e4b9SWei Wang 		return NULL;
1202e873e4b9SWei Wang 
12034832c30dSDavid Ahern 	rcu_read_lock();
12044832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(rt);
120593531c67SDavid Ahern 	pcpu_rt = ip6_dst_alloc(dev_net(dev), dev, flags);
12064832c30dSDavid Ahern 	rcu_read_unlock();
1207e873e4b9SWei Wang 	if (!pcpu_rt) {
1208e873e4b9SWei Wang 		fib6_info_release(rt);
1209d52d3997SMartin KaFai Lau 		return NULL;
1210e873e4b9SWei Wang 	}
1211d52d3997SMartin KaFai Lau 	ip6_rt_copy_init(pcpu_rt, rt);
1212d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
1213d52d3997SMartin KaFai Lau 	return pcpu_rt;
1214d52d3997SMartin KaFai Lau }
1215d52d3997SMartin KaFai Lau 
121666f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */
12178d1c802bSDavid Ahern static struct rt6_info *rt6_get_pcpu_route(struct fib6_info *rt)
1218d52d3997SMartin KaFai Lau {
1219a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, **p;
1220d52d3997SMartin KaFai Lau 
1221d52d3997SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1222d52d3997SMartin KaFai Lau 	pcpu_rt = *p;
1223d52d3997SMartin KaFai Lau 
1224d4ead6b3SDavid Ahern 	if (pcpu_rt)
122510585b43SDavid Ahern 		ip6_hold_safe(NULL, &pcpu_rt);
1226d3843fe5SWei Wang 
1227a73e4195SMartin KaFai Lau 	return pcpu_rt;
1228a73e4195SMartin KaFai Lau }
1229a73e4195SMartin KaFai Lau 
1230afb1d4b5SDavid Ahern static struct rt6_info *rt6_make_pcpu_route(struct net *net,
12318d1c802bSDavid Ahern 					    struct fib6_info *rt)
1232a73e4195SMartin KaFai Lau {
1233a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1234d52d3997SMartin KaFai Lau 
1235d52d3997SMartin KaFai Lau 	pcpu_rt = ip6_rt_pcpu_alloc(rt);
1236d52d3997SMartin KaFai Lau 	if (!pcpu_rt) {
12379c7370a1SMartin KaFai Lau 		dst_hold(&net->ipv6.ip6_null_entry->dst);
12389c7370a1SMartin KaFai Lau 		return net->ipv6.ip6_null_entry;
1239d52d3997SMartin KaFai Lau 	}
1240d52d3997SMartin KaFai Lau 
1241a94b9367SWei Wang 	dst_hold(&pcpu_rt->dst);
1242a73e4195SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1243d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1244951f788aSEric Dumazet 	BUG_ON(prev);
1245a94b9367SWei Wang 
1246d52d3997SMartin KaFai Lau 	return pcpu_rt;
1247d52d3997SMartin KaFai Lau }
1248d52d3997SMartin KaFai Lau 
124935732d01SWei Wang /* exception hash table implementation
125035732d01SWei Wang  */
125135732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
125235732d01SWei Wang 
125335732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
125435732d01SWei Wang  * Caller must hold rt6_exception_lock
125535732d01SWei Wang  */
125635732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
125735732d01SWei Wang 				 struct rt6_exception *rt6_ex)
125835732d01SWei Wang {
1259f5b51fe8SPaolo Abeni 	struct fib6_info *from;
1260b2427e67SColin Ian King 	struct net *net;
126181eb8447SWei Wang 
126235732d01SWei Wang 	if (!bucket || !rt6_ex)
126335732d01SWei Wang 		return;
1264b2427e67SColin Ian King 
1265b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
1266f5b51fe8SPaolo Abeni 	net->ipv6.rt6_stats->fib_rt_cache--;
1267f5b51fe8SPaolo Abeni 
1268f5b51fe8SPaolo Abeni 	/* purge completely the exception to allow releasing the held resources:
1269f5b51fe8SPaolo Abeni 	 * some [sk] cache may keep the dst around for unlimited time
1270f5b51fe8SPaolo Abeni 	 */
1271f5b51fe8SPaolo Abeni 	from = rcu_dereference_protected(rt6_ex->rt6i->from,
1272f5b51fe8SPaolo Abeni 					 lockdep_is_held(&rt6_exception_lock));
1273f5b51fe8SPaolo Abeni 	rcu_assign_pointer(rt6_ex->rt6i->from, NULL);
1274f5b51fe8SPaolo Abeni 	fib6_info_release(from);
1275f5b51fe8SPaolo Abeni 	dst_dev_put(&rt6_ex->rt6i->dst);
1276f5b51fe8SPaolo Abeni 
127735732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
127877634cc6SDavid Ahern 	dst_release(&rt6_ex->rt6i->dst);
127935732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
128035732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
128135732d01SWei Wang 	bucket->depth--;
128235732d01SWei Wang }
128335732d01SWei Wang 
128435732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
128535732d01SWei Wang  * Caller must hold rt6_exception_lock
128635732d01SWei Wang  */
128735732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
128835732d01SWei Wang {
128935732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
129035732d01SWei Wang 
129135732d01SWei Wang 	if (!bucket)
129235732d01SWei Wang 		return;
129335732d01SWei Wang 
129435732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
129535732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
129635732d01SWei Wang 			oldest = rt6_ex;
129735732d01SWei Wang 	}
129835732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
129935732d01SWei Wang }
130035732d01SWei Wang 
130135732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
130235732d01SWei Wang 			      const struct in6_addr *src)
130335732d01SWei Wang {
130435732d01SWei Wang 	static u32 seed __read_mostly;
130535732d01SWei Wang 	u32 val;
130635732d01SWei Wang 
130735732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
130835732d01SWei Wang 	val = jhash(dst, sizeof(*dst), seed);
130935732d01SWei Wang 
131035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
131135732d01SWei Wang 	if (src)
131235732d01SWei Wang 		val = jhash(src, sizeof(*src), val);
131335732d01SWei Wang #endif
131435732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
131535732d01SWei Wang }
131635732d01SWei Wang 
131735732d01SWei Wang /* Helper function to find the cached rt in the hash table
131835732d01SWei Wang  * and update bucket pointer to point to the bucket for this
131935732d01SWei Wang  * (daddr, saddr) pair
132035732d01SWei Wang  * Caller must hold rt6_exception_lock
132135732d01SWei Wang  */
132235732d01SWei Wang static struct rt6_exception *
132335732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
132435732d01SWei Wang 			      const struct in6_addr *daddr,
132535732d01SWei Wang 			      const struct in6_addr *saddr)
132635732d01SWei Wang {
132735732d01SWei Wang 	struct rt6_exception *rt6_ex;
132835732d01SWei Wang 	u32 hval;
132935732d01SWei Wang 
133035732d01SWei Wang 	if (!(*bucket) || !daddr)
133135732d01SWei Wang 		return NULL;
133235732d01SWei Wang 
133335732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
133435732d01SWei Wang 	*bucket += hval;
133535732d01SWei Wang 
133635732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
133735732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
133835732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
133935732d01SWei Wang 
134035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
134135732d01SWei Wang 		if (matched && saddr)
134235732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
134335732d01SWei Wang #endif
134435732d01SWei Wang 		if (matched)
134535732d01SWei Wang 			return rt6_ex;
134635732d01SWei Wang 	}
134735732d01SWei Wang 	return NULL;
134835732d01SWei Wang }
134935732d01SWei Wang 
135035732d01SWei Wang /* Helper function to find the cached rt in the hash table
135135732d01SWei Wang  * and update bucket pointer to point to the bucket for this
135235732d01SWei Wang  * (daddr, saddr) pair
135335732d01SWei Wang  * Caller must hold rcu_read_lock()
135435732d01SWei Wang  */
135535732d01SWei Wang static struct rt6_exception *
135635732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
135735732d01SWei Wang 			 const struct in6_addr *daddr,
135835732d01SWei Wang 			 const struct in6_addr *saddr)
135935732d01SWei Wang {
136035732d01SWei Wang 	struct rt6_exception *rt6_ex;
136135732d01SWei Wang 	u32 hval;
136235732d01SWei Wang 
136335732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
136435732d01SWei Wang 
136535732d01SWei Wang 	if (!(*bucket) || !daddr)
136635732d01SWei Wang 		return NULL;
136735732d01SWei Wang 
136835732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
136935732d01SWei Wang 	*bucket += hval;
137035732d01SWei Wang 
137135732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
137235732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
137335732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
137435732d01SWei Wang 
137535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
137635732d01SWei Wang 		if (matched && saddr)
137735732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
137835732d01SWei Wang #endif
137935732d01SWei Wang 		if (matched)
138035732d01SWei Wang 			return rt6_ex;
138135732d01SWei Wang 	}
138235732d01SWei Wang 	return NULL;
138335732d01SWei Wang }
138435732d01SWei Wang 
13858d1c802bSDavid Ahern static unsigned int fib6_mtu(const struct fib6_info *rt)
138635732d01SWei Wang {
1387d4ead6b3SDavid Ahern 	unsigned int mtu;
1388d4ead6b3SDavid Ahern 
1389dcd1f572SDavid Ahern 	if (rt->fib6_pmtu) {
1390dcd1f572SDavid Ahern 		mtu = rt->fib6_pmtu;
1391dcd1f572SDavid Ahern 	} else {
1392dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
1393dcd1f572SDavid Ahern 		struct inet6_dev *idev;
1394dcd1f572SDavid Ahern 
1395dcd1f572SDavid Ahern 		rcu_read_lock();
1396dcd1f572SDavid Ahern 		idev = __in6_dev_get(dev);
1397dcd1f572SDavid Ahern 		mtu = idev->cnf.mtu6;
1398dcd1f572SDavid Ahern 		rcu_read_unlock();
1399dcd1f572SDavid Ahern 	}
1400dcd1f572SDavid Ahern 
1401d4ead6b3SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
1402d4ead6b3SDavid Ahern 
1403ad1601aeSDavid Ahern 	return mtu - lwtunnel_headroom(rt->fib6_nh.fib_nh_lws, mtu);
1404d4ead6b3SDavid Ahern }
1405d4ead6b3SDavid Ahern 
140635732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
14078d1c802bSDavid Ahern 				struct fib6_info *ort)
140835732d01SWei Wang {
14095e670d84SDavid Ahern 	struct net *net = dev_net(nrt->dst.dev);
141035732d01SWei Wang 	struct rt6_exception_bucket *bucket;
141135732d01SWei Wang 	struct in6_addr *src_key = NULL;
141235732d01SWei Wang 	struct rt6_exception *rt6_ex;
141335732d01SWei Wang 	int err = 0;
141435732d01SWei Wang 
141535732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
141635732d01SWei Wang 
141735732d01SWei Wang 	if (ort->exception_bucket_flushed) {
141835732d01SWei Wang 		err = -EINVAL;
141935732d01SWei Wang 		goto out;
142035732d01SWei Wang 	}
142135732d01SWei Wang 
142235732d01SWei Wang 	bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
142335732d01SWei Wang 					lockdep_is_held(&rt6_exception_lock));
142435732d01SWei Wang 	if (!bucket) {
142535732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
142635732d01SWei Wang 				 GFP_ATOMIC);
142735732d01SWei Wang 		if (!bucket) {
142835732d01SWei Wang 			err = -ENOMEM;
142935732d01SWei Wang 			goto out;
143035732d01SWei Wang 		}
143135732d01SWei Wang 		rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
143235732d01SWei Wang 	}
143335732d01SWei Wang 
143435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
143535732d01SWei Wang 	/* rt6i_src.plen != 0 indicates ort is in subtree
143635732d01SWei Wang 	 * and exception table is indexed by a hash of
143735732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
143835732d01SWei Wang 	 * Otherwise, the exception table is indexed by
143935732d01SWei Wang 	 * a hash of only rt6i_dst.
144035732d01SWei Wang 	 */
144193c2fb25SDavid Ahern 	if (ort->fib6_src.plen)
144235732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
144335732d01SWei Wang #endif
1444f5bbe7eeSWei Wang 	/* rt6_mtu_change() might lower mtu on ort.
1445f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
1446f5bbe7eeSWei Wang 	 * is less than ort's mtu value.
1447f5bbe7eeSWei Wang 	 */
1448d4ead6b3SDavid Ahern 	if (dst_metric_raw(&nrt->dst, RTAX_MTU) >= fib6_mtu(ort)) {
1449f5bbe7eeSWei Wang 		err = -EINVAL;
1450f5bbe7eeSWei Wang 		goto out;
1451f5bbe7eeSWei Wang 	}
145260006a48SWei Wang 
145335732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
145435732d01SWei Wang 					       src_key);
145535732d01SWei Wang 	if (rt6_ex)
145635732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
145735732d01SWei Wang 
145835732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
145935732d01SWei Wang 	if (!rt6_ex) {
146035732d01SWei Wang 		err = -ENOMEM;
146135732d01SWei Wang 		goto out;
146235732d01SWei Wang 	}
146335732d01SWei Wang 	rt6_ex->rt6i = nrt;
146435732d01SWei Wang 	rt6_ex->stamp = jiffies;
146535732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
146635732d01SWei Wang 	bucket->depth++;
146781eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
146835732d01SWei Wang 
146935732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
147035732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
147135732d01SWei Wang 
147235732d01SWei Wang out:
147335732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
147435732d01SWei Wang 
147535732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1476b886d5f2SPaolo Abeni 	if (!err) {
147793c2fb25SDavid Ahern 		spin_lock_bh(&ort->fib6_table->tb6_lock);
14787aef6859SDavid Ahern 		fib6_update_sernum(net, ort);
147993c2fb25SDavid Ahern 		spin_unlock_bh(&ort->fib6_table->tb6_lock);
1480b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1481b886d5f2SPaolo Abeni 	}
148235732d01SWei Wang 
148335732d01SWei Wang 	return err;
148435732d01SWei Wang }
148535732d01SWei Wang 
14868d1c802bSDavid Ahern void rt6_flush_exceptions(struct fib6_info *rt)
148735732d01SWei Wang {
148835732d01SWei Wang 	struct rt6_exception_bucket *bucket;
148935732d01SWei Wang 	struct rt6_exception *rt6_ex;
149035732d01SWei Wang 	struct hlist_node *tmp;
149135732d01SWei Wang 	int i;
149235732d01SWei Wang 
149335732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
149435732d01SWei Wang 	/* Prevent rt6_insert_exception() to recreate the bucket list */
149535732d01SWei Wang 	rt->exception_bucket_flushed = 1;
149635732d01SWei Wang 
149735732d01SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
149835732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
149935732d01SWei Wang 	if (!bucket)
150035732d01SWei Wang 		goto out;
150135732d01SWei Wang 
150235732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
150335732d01SWei Wang 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
150435732d01SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
150535732d01SWei Wang 		WARN_ON_ONCE(bucket->depth);
150635732d01SWei Wang 		bucket++;
150735732d01SWei Wang 	}
150835732d01SWei Wang 
150935732d01SWei Wang out:
151035732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
151135732d01SWei Wang }
151235732d01SWei Wang 
151335732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
151435732d01SWei Wang  * Caller has to hold rcu_read_lock()
151535732d01SWei Wang  */
15168d1c802bSDavid Ahern static struct rt6_info *rt6_find_cached_rt(struct fib6_info *rt,
151735732d01SWei Wang 					   struct in6_addr *daddr,
151835732d01SWei Wang 					   struct in6_addr *saddr)
151935732d01SWei Wang {
152035732d01SWei Wang 	struct rt6_exception_bucket *bucket;
152135732d01SWei Wang 	struct in6_addr *src_key = NULL;
152235732d01SWei Wang 	struct rt6_exception *rt6_ex;
152335732d01SWei Wang 	struct rt6_info *res = NULL;
152435732d01SWei Wang 
152535732d01SWei Wang 	bucket = rcu_dereference(rt->rt6i_exception_bucket);
152635732d01SWei Wang 
152735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
152835732d01SWei Wang 	/* rt6i_src.plen != 0 indicates rt is in subtree
152935732d01SWei Wang 	 * and exception table is indexed by a hash of
153035732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
153135732d01SWei Wang 	 * Otherwise, the exception table is indexed by
153235732d01SWei Wang 	 * a hash of only rt6i_dst.
153335732d01SWei Wang 	 */
153493c2fb25SDavid Ahern 	if (rt->fib6_src.plen)
153535732d01SWei Wang 		src_key = saddr;
153635732d01SWei Wang #endif
153735732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
153835732d01SWei Wang 
153935732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
154035732d01SWei Wang 		res = rt6_ex->rt6i;
154135732d01SWei Wang 
154235732d01SWei Wang 	return res;
154335732d01SWei Wang }
154435732d01SWei Wang 
154535732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
154623fb93a4SDavid Ahern static int rt6_remove_exception_rt(struct rt6_info *rt)
154735732d01SWei Wang {
154835732d01SWei Wang 	struct rt6_exception_bucket *bucket;
154935732d01SWei Wang 	struct in6_addr *src_key = NULL;
155035732d01SWei Wang 	struct rt6_exception *rt6_ex;
15518a14e46fSDavid Ahern 	struct fib6_info *from;
155235732d01SWei Wang 	int err;
155335732d01SWei Wang 
1554091311deSEric Dumazet 	from = rcu_dereference(rt->from);
155535732d01SWei Wang 	if (!from ||
1556442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
155735732d01SWei Wang 		return -EINVAL;
155835732d01SWei Wang 
155935732d01SWei Wang 	if (!rcu_access_pointer(from->rt6i_exception_bucket))
156035732d01SWei Wang 		return -ENOENT;
156135732d01SWei Wang 
156235732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
156335732d01SWei Wang 	bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
156435732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
156535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
156635732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
156735732d01SWei Wang 	 * and exception table is indexed by a hash of
156835732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
156935732d01SWei Wang 	 * Otherwise, the exception table is indexed by
157035732d01SWei Wang 	 * a hash of only rt6i_dst.
157135732d01SWei Wang 	 */
157293c2fb25SDavid Ahern 	if (from->fib6_src.plen)
157335732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
157435732d01SWei Wang #endif
157535732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
157635732d01SWei Wang 					       &rt->rt6i_dst.addr,
157735732d01SWei Wang 					       src_key);
157835732d01SWei Wang 	if (rt6_ex) {
157935732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
158035732d01SWei Wang 		err = 0;
158135732d01SWei Wang 	} else {
158235732d01SWei Wang 		err = -ENOENT;
158335732d01SWei Wang 	}
158435732d01SWei Wang 
158535732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
158635732d01SWei Wang 	return err;
158735732d01SWei Wang }
158835732d01SWei Wang 
158935732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
159035732d01SWei Wang  * refresh its stamp
159135732d01SWei Wang  */
159235732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
159335732d01SWei Wang {
159435732d01SWei Wang 	struct rt6_exception_bucket *bucket;
159535732d01SWei Wang 	struct in6_addr *src_key = NULL;
159635732d01SWei Wang 	struct rt6_exception *rt6_ex;
1597193f3685SPaolo Abeni 	struct fib6_info *from;
159835732d01SWei Wang 
159935732d01SWei Wang 	rcu_read_lock();
1600193f3685SPaolo Abeni 	from = rcu_dereference(rt->from);
1601193f3685SPaolo Abeni 	if (!from || !(rt->rt6i_flags & RTF_CACHE))
1602193f3685SPaolo Abeni 		goto unlock;
1603193f3685SPaolo Abeni 
160435732d01SWei Wang 	bucket = rcu_dereference(from->rt6i_exception_bucket);
160535732d01SWei Wang 
160635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
160735732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
160835732d01SWei Wang 	 * and exception table is indexed by a hash of
160935732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
161035732d01SWei Wang 	 * Otherwise, the exception table is indexed by
161135732d01SWei Wang 	 * a hash of only rt6i_dst.
161235732d01SWei Wang 	 */
161393c2fb25SDavid Ahern 	if (from->fib6_src.plen)
161435732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
161535732d01SWei Wang #endif
161635732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket,
161735732d01SWei Wang 					  &rt->rt6i_dst.addr,
161835732d01SWei Wang 					  src_key);
161935732d01SWei Wang 	if (rt6_ex)
162035732d01SWei Wang 		rt6_ex->stamp = jiffies;
162135732d01SWei Wang 
1622193f3685SPaolo Abeni unlock:
162335732d01SWei Wang 	rcu_read_unlock();
162435732d01SWei Wang }
162535732d01SWei Wang 
1626e9fa1495SStefano Brivio static bool rt6_mtu_change_route_allowed(struct inet6_dev *idev,
1627e9fa1495SStefano Brivio 					 struct rt6_info *rt, int mtu)
1628e9fa1495SStefano Brivio {
1629e9fa1495SStefano Brivio 	/* If the new MTU is lower than the route PMTU, this new MTU will be the
1630e9fa1495SStefano Brivio 	 * lowest MTU in the path: always allow updating the route PMTU to
1631e9fa1495SStefano Brivio 	 * reflect PMTU decreases.
1632e9fa1495SStefano Brivio 	 *
1633e9fa1495SStefano Brivio 	 * If the new MTU is higher, and the route PMTU is equal to the local
1634e9fa1495SStefano Brivio 	 * MTU, this means the old MTU is the lowest in the path, so allow
1635e9fa1495SStefano Brivio 	 * updating it: if other nodes now have lower MTUs, PMTU discovery will
1636e9fa1495SStefano Brivio 	 * handle this.
1637e9fa1495SStefano Brivio 	 */
1638e9fa1495SStefano Brivio 
1639e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) >= mtu)
1640e9fa1495SStefano Brivio 		return true;
1641e9fa1495SStefano Brivio 
1642e9fa1495SStefano Brivio 	if (dst_mtu(&rt->dst) == idev->cnf.mtu6)
1643e9fa1495SStefano Brivio 		return true;
1644e9fa1495SStefano Brivio 
1645e9fa1495SStefano Brivio 	return false;
1646e9fa1495SStefano Brivio }
1647e9fa1495SStefano Brivio 
1648e9fa1495SStefano Brivio static void rt6_exceptions_update_pmtu(struct inet6_dev *idev,
16498d1c802bSDavid Ahern 				       struct fib6_info *rt, int mtu)
1650f5bbe7eeSWei Wang {
1651f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
1652f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
1653f5bbe7eeSWei Wang 	int i;
1654f5bbe7eeSWei Wang 
1655f5bbe7eeSWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1656f5bbe7eeSWei Wang 					lockdep_is_held(&rt6_exception_lock));
1657f5bbe7eeSWei Wang 
1658e9fa1495SStefano Brivio 	if (!bucket)
1659e9fa1495SStefano Brivio 		return;
1660e9fa1495SStefano Brivio 
1661f5bbe7eeSWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1662f5bbe7eeSWei Wang 		hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1663f5bbe7eeSWei Wang 			struct rt6_info *entry = rt6_ex->rt6i;
1664e9fa1495SStefano Brivio 
1665e9fa1495SStefano Brivio 			/* For RTF_CACHE with rt6i_pmtu == 0 (i.e. a redirected
1666d4ead6b3SDavid Ahern 			 * route), the metrics of its rt->from have already
1667f5bbe7eeSWei Wang 			 * been updated.
1668f5bbe7eeSWei Wang 			 */
1669d4ead6b3SDavid Ahern 			if (dst_metric_raw(&entry->dst, RTAX_MTU) &&
1670e9fa1495SStefano Brivio 			    rt6_mtu_change_route_allowed(idev, entry, mtu))
1671d4ead6b3SDavid Ahern 				dst_metric_set(&entry->dst, RTAX_MTU, mtu);
1672f5bbe7eeSWei Wang 		}
1673f5bbe7eeSWei Wang 		bucket++;
1674f5bbe7eeSWei Wang 	}
1675f5bbe7eeSWei Wang }
1676f5bbe7eeSWei Wang 
1677b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
1678b16cb459SWei Wang 
16798d1c802bSDavid Ahern static void rt6_exceptions_clean_tohost(struct fib6_info *rt,
1680b16cb459SWei Wang 					struct in6_addr *gateway)
1681b16cb459SWei Wang {
1682b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
1683b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
1684b16cb459SWei Wang 	struct hlist_node *tmp;
1685b16cb459SWei Wang 	int i;
1686b16cb459SWei Wang 
1687b16cb459SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1688b16cb459SWei Wang 		return;
1689b16cb459SWei Wang 
1690b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1691b16cb459SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1692b16cb459SWei Wang 				     lockdep_is_held(&rt6_exception_lock));
1693b16cb459SWei Wang 
1694b16cb459SWei Wang 	if (bucket) {
1695b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1696b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1697b16cb459SWei Wang 						  &bucket->chain, hlist) {
1698b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1699b16cb459SWei Wang 
1700b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1701b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
1702b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
1703b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
1704b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
1705b16cb459SWei Wang 				}
1706b16cb459SWei Wang 			}
1707b16cb459SWei Wang 			bucket++;
1708b16cb459SWei Wang 		}
1709b16cb459SWei Wang 	}
1710b16cb459SWei Wang 
1711b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1712b16cb459SWei Wang }
1713b16cb459SWei Wang 
1714c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1715c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
1716c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
1717c757faa8SWei Wang 				      unsigned long now)
1718c757faa8SWei Wang {
1719c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
1720c757faa8SWei Wang 
17211859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
17221859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
17231859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
17241859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
17251859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
17261859bac0SPaolo Abeni 	 */
172731afeb42SWei Wang 	if (!(rt->rt6i_flags & RTF_EXPIRES)) {
172831afeb42SWei Wang 		if (time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1729c757faa8SWei Wang 			RT6_TRACE("aging clone %p\n", rt);
1730c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1731c757faa8SWei Wang 			return;
173231afeb42SWei Wang 		}
173331afeb42SWei Wang 	} else if (time_after(jiffies, rt->dst.expires)) {
173431afeb42SWei Wang 		RT6_TRACE("purging expired route %p\n", rt);
173531afeb42SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
173631afeb42SWei Wang 		return;
173731afeb42SWei Wang 	}
173831afeb42SWei Wang 
173931afeb42SWei Wang 	if (rt->rt6i_flags & RTF_GATEWAY) {
1740c757faa8SWei Wang 		struct neighbour *neigh;
1741c757faa8SWei Wang 		__u8 neigh_flags = 0;
1742c757faa8SWei Wang 
17431bfa26ffSEric Dumazet 		neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
17441bfa26ffSEric Dumazet 		if (neigh)
1745c757faa8SWei Wang 			neigh_flags = neigh->flags;
17461bfa26ffSEric Dumazet 
1747c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
1748c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
1749c757faa8SWei Wang 				  rt);
1750c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1751c757faa8SWei Wang 			return;
1752c757faa8SWei Wang 		}
1753c757faa8SWei Wang 	}
175431afeb42SWei Wang 
1755c757faa8SWei Wang 	gc_args->more++;
1756c757faa8SWei Wang }
1757c757faa8SWei Wang 
17588d1c802bSDavid Ahern void rt6_age_exceptions(struct fib6_info *rt,
1759c757faa8SWei Wang 			struct fib6_gc_args *gc_args,
1760c757faa8SWei Wang 			unsigned long now)
1761c757faa8SWei Wang {
1762c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
1763c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
1764c757faa8SWei Wang 	struct hlist_node *tmp;
1765c757faa8SWei Wang 	int i;
1766c757faa8SWei Wang 
1767c757faa8SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1768c757faa8SWei Wang 		return;
1769c757faa8SWei Wang 
17701bfa26ffSEric Dumazet 	rcu_read_lock_bh();
17711bfa26ffSEric Dumazet 	spin_lock(&rt6_exception_lock);
1772c757faa8SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1773c757faa8SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
1774c757faa8SWei Wang 
1775c757faa8SWei Wang 	if (bucket) {
1776c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1777c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1778c757faa8SWei Wang 						  &bucket->chain, hlist) {
1779c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
1780c757faa8SWei Wang 							  gc_args, now);
1781c757faa8SWei Wang 			}
1782c757faa8SWei Wang 			bucket++;
1783c757faa8SWei Wang 		}
1784c757faa8SWei Wang 	}
17851bfa26ffSEric Dumazet 	spin_unlock(&rt6_exception_lock);
17861bfa26ffSEric Dumazet 	rcu_read_unlock_bh();
1787c757faa8SWei Wang }
1788c757faa8SWei Wang 
17891d053da9SDavid Ahern /* must be called with rcu lock held */
17901d053da9SDavid Ahern struct fib6_info *fib6_table_lookup(struct net *net, struct fib6_table *table,
17911d053da9SDavid Ahern 				    int oif, struct flowi6 *fl6, int strict)
17921da177e4SLinus Torvalds {
1793367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
17948d1c802bSDavid Ahern 	struct fib6_info *f6i;
17951da177e4SLinus Torvalds 
17966454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1797367efcb9SMartin KaFai Lau 	saved_fn = fn;
17981da177e4SLinus Torvalds 
1799ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1800ca254490SDavid Ahern 		oif = 0;
1801ca254490SDavid Ahern 
1802a3c00e46SMartin KaFai Lau redo_rt6_select:
180323fb93a4SDavid Ahern 	f6i = rt6_select(net, fn, oif, strict);
180423fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1805a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1806a3c00e46SMartin KaFai Lau 		if (fn)
1807a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
1808367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
1809367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
1810367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
1811367efcb9SMartin KaFai Lau 			fn = saved_fn;
1812367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
1813367efcb9SMartin KaFai Lau 		}
1814a3c00e46SMartin KaFai Lau 	}
1815a3c00e46SMartin KaFai Lau 
1816d4bea421SDavid Ahern 	trace_fib6_table_lookup(net, f6i, table, fl6);
1817d52d3997SMartin KaFai Lau 
18181d053da9SDavid Ahern 	return f6i;
18191d053da9SDavid Ahern }
18201d053da9SDavid Ahern 
18211d053da9SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
18221d053da9SDavid Ahern 			       int oif, struct flowi6 *fl6,
18231d053da9SDavid Ahern 			       const struct sk_buff *skb, int flags)
18241d053da9SDavid Ahern {
18251d053da9SDavid Ahern 	struct fib6_info *f6i;
18261d053da9SDavid Ahern 	struct rt6_info *rt;
18271d053da9SDavid Ahern 	int strict = 0;
18281d053da9SDavid Ahern 
18291d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IFACE;
18301d053da9SDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
18311d053da9SDavid Ahern 	if (net->ipv6.devconf_all->forwarding == 0)
18321d053da9SDavid Ahern 		strict |= RT6_LOOKUP_F_REACHABLE;
18331d053da9SDavid Ahern 
18341d053da9SDavid Ahern 	rcu_read_lock();
18351d053da9SDavid Ahern 
18361d053da9SDavid Ahern 	f6i = fib6_table_lookup(net, table, oif, fl6, strict);
18371d053da9SDavid Ahern 	if (f6i->fib6_nsiblings)
18381d053da9SDavid Ahern 		f6i = fib6_multipath_select(net, f6i, fl6, oif, skb, strict);
18391d053da9SDavid Ahern 
184023fb93a4SDavid Ahern 	if (f6i == net->ipv6.fib6_null_entry) {
1841421842edSDavid Ahern 		rt = net->ipv6.ip6_null_entry;
184266f5d6ceSWei Wang 		rcu_read_unlock();
1843d3843fe5SWei Wang 		dst_hold(&rt->dst);
1844d3843fe5SWei Wang 		return rt;
1845d3843fe5SWei Wang 	}
184623fb93a4SDavid Ahern 
184723fb93a4SDavid Ahern 	/*Search through exception table */
184823fb93a4SDavid Ahern 	rt = rt6_find_cached_rt(f6i, &fl6->daddr, &fl6->saddr);
184923fb93a4SDavid Ahern 	if (rt) {
185010585b43SDavid Ahern 		if (ip6_hold_safe(net, &rt))
18511da177e4SLinus Torvalds 			dst_use_noref(&rt->dst, jiffies);
1852d4ead6b3SDavid Ahern 
185366f5d6ceSWei Wang 		rcu_read_unlock();
1854d52d3997SMartin KaFai Lau 		return rt;
18553da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
1856bdf00467SDavid Ahern 			    !f6i->fib6_nh.fib_nh_gw_family)) {
18573da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
18583da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
18593da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
18603da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
18613da59bd9SMartin KaFai Lau 		 */
18623da59bd9SMartin KaFai Lau 		struct rt6_info *uncached_rt;
18633da59bd9SMartin KaFai Lau 
186423fb93a4SDavid Ahern 		uncached_rt = ip6_rt_cache_alloc(f6i, &fl6->daddr, NULL);
1865d52d3997SMartin KaFai Lau 
18664d85cd0cSDavid Ahern 		rcu_read_unlock();
18673da59bd9SMartin KaFai Lau 
18681cfb71eeSWei Wang 		if (uncached_rt) {
18691cfb71eeSWei Wang 			/* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
18701cfb71eeSWei Wang 			 * No need for another dst_hold()
18711cfb71eeSWei Wang 			 */
18728d0b94afSMartin KaFai Lau 			rt6_uncached_list_add(uncached_rt);
187381eb8447SWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
18741cfb71eeSWei Wang 		} else {
18753da59bd9SMartin KaFai Lau 			uncached_rt = net->ipv6.ip6_null_entry;
18763da59bd9SMartin KaFai Lau 			dst_hold(&uncached_rt->dst);
18771cfb71eeSWei Wang 		}
1878b811580dSDavid Ahern 
18793da59bd9SMartin KaFai Lau 		return uncached_rt;
1880d52d3997SMartin KaFai Lau 	} else {
1881d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
1882d52d3997SMartin KaFai Lau 
1883d52d3997SMartin KaFai Lau 		struct rt6_info *pcpu_rt;
1884d52d3997SMartin KaFai Lau 
1885951f788aSEric Dumazet 		local_bh_disable();
188623fb93a4SDavid Ahern 		pcpu_rt = rt6_get_pcpu_route(f6i);
1887d52d3997SMartin KaFai Lau 
188893531c67SDavid Ahern 		if (!pcpu_rt)
188923fb93a4SDavid Ahern 			pcpu_rt = rt6_make_pcpu_route(net, f6i);
189093531c67SDavid Ahern 
1891951f788aSEric Dumazet 		local_bh_enable();
1892951f788aSEric Dumazet 		rcu_read_unlock();
1893d4bea421SDavid Ahern 
1894d52d3997SMartin KaFai Lau 		return pcpu_rt;
1895d52d3997SMartin KaFai Lau 	}
1896c71099acSThomas Graf }
18979ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
1898c71099acSThomas Graf 
1899b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_input(struct net *net,
1900b75cc8f9SDavid Ahern 					    struct fib6_table *table,
1901b75cc8f9SDavid Ahern 					    struct flowi6 *fl6,
1902b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
1903b75cc8f9SDavid Ahern 					    int flags)
19044acad72dSPavel Emelyanov {
1905b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, skb, flags);
19064acad72dSPavel Emelyanov }
19074acad72dSPavel Emelyanov 
1908d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
190972331bc0SShmulik Ladkani 					 struct net_device *dev,
1910b75cc8f9SDavid Ahern 					 struct flowi6 *fl6,
1911b75cc8f9SDavid Ahern 					 const struct sk_buff *skb,
1912b75cc8f9SDavid Ahern 					 int flags)
191372331bc0SShmulik Ladkani {
191472331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
191572331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
191672331bc0SShmulik Ladkani 
1917b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, skb, flags, ip6_pol_route_input);
191872331bc0SShmulik Ladkani }
1919d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
192072331bc0SShmulik Ladkani 
192123aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
19225e5d6fedSRoopa Prabhu 				  struct flow_keys *keys,
19235e5d6fedSRoopa Prabhu 				  struct flow_keys *flkeys)
192423aebdacSJakub Sitnicki {
192523aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
192623aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
19275e5d6fedSRoopa Prabhu 	struct flow_keys *_flkeys = flkeys;
192823aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
192923aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
193023aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
1931cea67a2dSEric Dumazet 	struct icmp6hdr _icmph;
193223aebdacSJakub Sitnicki 
193323aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
193423aebdacSJakub Sitnicki 		goto out;
193523aebdacSJakub Sitnicki 
1936cea67a2dSEric Dumazet 	icmph = skb_header_pointer(skb, skb_transport_offset(skb),
1937cea67a2dSEric Dumazet 				   sizeof(_icmph), &_icmph);
1938cea67a2dSEric Dumazet 	if (!icmph)
1939cea67a2dSEric Dumazet 		goto out;
1940cea67a2dSEric Dumazet 
194123aebdacSJakub Sitnicki 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
194223aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
194323aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
194423aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
194523aebdacSJakub Sitnicki 		goto out;
194623aebdacSJakub Sitnicki 
194723aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
194823aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
194923aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
195023aebdacSJakub Sitnicki 	if (!inner_iph)
195123aebdacSJakub Sitnicki 		goto out;
195223aebdacSJakub Sitnicki 
195323aebdacSJakub Sitnicki 	key_iph = inner_iph;
19545e5d6fedSRoopa Prabhu 	_flkeys = NULL;
195523aebdacSJakub Sitnicki out:
19565e5d6fedSRoopa Prabhu 	if (_flkeys) {
19575e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.src = _flkeys->addrs.v6addrs.src;
19585e5d6fedSRoopa Prabhu 		keys->addrs.v6addrs.dst = _flkeys->addrs.v6addrs.dst;
19595e5d6fedSRoopa Prabhu 		keys->tags.flow_label = _flkeys->tags.flow_label;
19605e5d6fedSRoopa Prabhu 		keys->basic.ip_proto = _flkeys->basic.ip_proto;
19615e5d6fedSRoopa Prabhu 	} else {
196223aebdacSJakub Sitnicki 		keys->addrs.v6addrs.src = key_iph->saddr;
196323aebdacSJakub Sitnicki 		keys->addrs.v6addrs.dst = key_iph->daddr;
1964fa1be7e0SMichal Kubecek 		keys->tags.flow_label = ip6_flowlabel(key_iph);
196523aebdacSJakub Sitnicki 		keys->basic.ip_proto = key_iph->nexthdr;
196623aebdacSJakub Sitnicki 	}
19675e5d6fedSRoopa Prabhu }
196823aebdacSJakub Sitnicki 
196923aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
1970b4bac172SDavid Ahern u32 rt6_multipath_hash(const struct net *net, const struct flowi6 *fl6,
1971b4bac172SDavid Ahern 		       const struct sk_buff *skb, struct flow_keys *flkeys)
197223aebdacSJakub Sitnicki {
197323aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
19749a2a537aSDavid Ahern 	u32 mhash;
197523aebdacSJakub Sitnicki 
1976bbfa047aSDavid S. Miller 	switch (ip6_multipath_hash_policy(net)) {
1977b4bac172SDavid Ahern 	case 0:
19786f74b6c2SDavid Ahern 		memset(&hash_keys, 0, sizeof(hash_keys));
19796f74b6c2SDavid Ahern 		hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
19809a2a537aSDavid Ahern 		if (skb) {
19815e5d6fedSRoopa Prabhu 			ip6_multipath_l3_keys(skb, &hash_keys, flkeys);
19829a2a537aSDavid Ahern 		} else {
19839a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
19849a2a537aSDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
1985fa1be7e0SMichal Kubecek 			hash_keys.tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6);
19869a2a537aSDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
198723aebdacSJakub Sitnicki 		}
1988b4bac172SDavid Ahern 		break;
1989b4bac172SDavid Ahern 	case 1:
1990b4bac172SDavid Ahern 		if (skb) {
1991b4bac172SDavid Ahern 			unsigned int flag = FLOW_DISSECTOR_F_STOP_AT_ENCAP;
1992b4bac172SDavid Ahern 			struct flow_keys keys;
1993b4bac172SDavid Ahern 
1994b4bac172SDavid Ahern 			/* short-circuit if we already have L4 hash present */
1995b4bac172SDavid Ahern 			if (skb->l4_hash)
1996b4bac172SDavid Ahern 				return skb_get_hash_raw(skb) >> 1;
1997b4bac172SDavid Ahern 
1998b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
1999b4bac172SDavid Ahern 
2000b4bac172SDavid Ahern                         if (!flkeys) {
2001b4bac172SDavid Ahern 				skb_flow_dissect_flow_keys(skb, &keys, flag);
2002b4bac172SDavid Ahern 				flkeys = &keys;
2003b4bac172SDavid Ahern 			}
2004b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2005b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = flkeys->addrs.v6addrs.src;
2006b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = flkeys->addrs.v6addrs.dst;
2007b4bac172SDavid Ahern 			hash_keys.ports.src = flkeys->ports.src;
2008b4bac172SDavid Ahern 			hash_keys.ports.dst = flkeys->ports.dst;
2009b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = flkeys->basic.ip_proto;
2010b4bac172SDavid Ahern 		} else {
2011b4bac172SDavid Ahern 			memset(&hash_keys, 0, sizeof(hash_keys));
2012b4bac172SDavid Ahern 			hash_keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2013b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.src = fl6->saddr;
2014b4bac172SDavid Ahern 			hash_keys.addrs.v6addrs.dst = fl6->daddr;
2015b4bac172SDavid Ahern 			hash_keys.ports.src = fl6->fl6_sport;
2016b4bac172SDavid Ahern 			hash_keys.ports.dst = fl6->fl6_dport;
2017b4bac172SDavid Ahern 			hash_keys.basic.ip_proto = fl6->flowi6_proto;
2018b4bac172SDavid Ahern 		}
2019b4bac172SDavid Ahern 		break;
2020b4bac172SDavid Ahern 	}
20219a2a537aSDavid Ahern 	mhash = flow_hash_from_keys(&hash_keys);
202223aebdacSJakub Sitnicki 
20239a2a537aSDavid Ahern 	return mhash >> 1;
202423aebdacSJakub Sitnicki }
202523aebdacSJakub Sitnicki 
2026c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
2027c71099acSThomas Graf {
2028b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2029c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
2030adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2031904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
20324c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
2033e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
20344c9483b2SDavid S. Miller 		.daddr = iph->daddr,
20354c9483b2SDavid S. Miller 		.saddr = iph->saddr,
20366502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
20374c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
20384c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
2039c71099acSThomas Graf 	};
20405e5d6fedSRoopa Prabhu 	struct flow_keys *flkeys = NULL, _flkeys;
2041adaa70bbSThomas Graf 
2042904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
204346fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
2044904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
20455e5d6fedSRoopa Prabhu 
20465e5d6fedSRoopa Prabhu 	if (fib6_rules_early_flow_dissect(net, skb, &fl6, &_flkeys))
20475e5d6fedSRoopa Prabhu 		flkeys = &_flkeys;
20485e5d6fedSRoopa Prabhu 
204923aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
2050b4bac172SDavid Ahern 		fl6.mp_hash = rt6_multipath_hash(net, &fl6, skb, flkeys);
205106e9d040SJiri Benc 	skb_dst_drop(skb);
2052b75cc8f9SDavid Ahern 	skb_dst_set(skb,
2053b75cc8f9SDavid Ahern 		    ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags));
2054c71099acSThomas Graf }
2055c71099acSThomas Graf 
2056b75cc8f9SDavid Ahern static struct rt6_info *ip6_pol_route_output(struct net *net,
2057b75cc8f9SDavid Ahern 					     struct fib6_table *table,
2058b75cc8f9SDavid Ahern 					     struct flowi6 *fl6,
2059b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2060b75cc8f9SDavid Ahern 					     int flags)
2061c71099acSThomas Graf {
2062b75cc8f9SDavid Ahern 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, skb, flags);
2063c71099acSThomas Graf }
2064c71099acSThomas Graf 
20656f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
20666f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags)
2067c71099acSThomas Graf {
2068d46a9d67SDavid Ahern 	bool any_src;
2069c71099acSThomas Graf 
20703ede0bbcSRobert Shearman 	if (ipv6_addr_type(&fl6->daddr) &
20713ede0bbcSRobert Shearman 	    (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL)) {
20724c1feac5SDavid Ahern 		struct dst_entry *dst;
20734c1feac5SDavid Ahern 
20744c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
2075ca254490SDavid Ahern 		if (dst)
2076ca254490SDavid Ahern 			return dst;
20774c1feac5SDavid Ahern 	}
2078ca254490SDavid Ahern 
20791fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
20804dc27d1cSDavid McCullough 
2081d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
2082741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
2083d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
208477d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
2085c71099acSThomas Graf 
2086d46a9d67SDavid Ahern 	if (!any_src)
2087adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
20880c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
20890c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
2090adaa70bbSThomas Graf 
2091b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, fl6, NULL, flags, ip6_pol_route_output);
20921da177e4SLinus Torvalds }
20936f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
20941da177e4SLinus Torvalds 
20952774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
209614e50e57SDavid S. Miller {
20975c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
20981dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
209914e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
210014e50e57SDavid S. Miller 
21011dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
210262cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
210314e50e57SDavid S. Miller 	if (rt) {
21040a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
210581eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
21060a1f5962SMartin KaFai Lau 
2107d8d1f30bSChangli Gao 		new = &rt->dst;
210814e50e57SDavid S. Miller 		new->__use = 1;
2109352e512cSHerbert Xu 		new->input = dst_discard;
2110ede2059dSEric W. Biederman 		new->output = dst_discard_out;
211114e50e57SDavid S. Miller 
2112defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
211314e50e57SDavid S. Miller 
21141dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
21154e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
21160a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
211714e50e57SDavid S. Miller 
211814e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
211914e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
212014e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
212114e50e57SDavid S. Miller #endif
212214e50e57SDavid S. Miller 	}
212314e50e57SDavid S. Miller 
212469ead7afSDavid S. Miller 	dst_release(dst_orig);
212569ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
212614e50e57SDavid S. Miller }
212714e50e57SDavid S. Miller 
21281da177e4SLinus Torvalds /*
21291da177e4SLinus Torvalds  *	Destination cache support functions
21301da177e4SLinus Torvalds  */
21311da177e4SLinus Torvalds 
21328d1c802bSDavid Ahern static bool fib6_check(struct fib6_info *f6i, u32 cookie)
21333da59bd9SMartin KaFai Lau {
213436143645SSteffen Klassert 	u32 rt_cookie = 0;
2135c5cff856SWei Wang 
21368ae86971SDavid Ahern 	if (!fib6_get_cookie_safe(f6i, &rt_cookie) || rt_cookie != cookie)
213793531c67SDavid Ahern 		return false;
213893531c67SDavid Ahern 
213993531c67SDavid Ahern 	if (fib6_check_expired(f6i))
214093531c67SDavid Ahern 		return false;
214193531c67SDavid Ahern 
214293531c67SDavid Ahern 	return true;
214393531c67SDavid Ahern }
214493531c67SDavid Ahern 
2145a68886a6SDavid Ahern static struct dst_entry *rt6_check(struct rt6_info *rt,
2146a68886a6SDavid Ahern 				   struct fib6_info *from,
2147a68886a6SDavid Ahern 				   u32 cookie)
21483da59bd9SMartin KaFai Lau {
2149c5cff856SWei Wang 	u32 rt_cookie = 0;
2150c5cff856SWei Wang 
2151a68886a6SDavid Ahern 	if ((from && !fib6_get_cookie_safe(from, &rt_cookie)) ||
215293531c67SDavid Ahern 	    rt_cookie != cookie)
21533da59bd9SMartin KaFai Lau 		return NULL;
21543da59bd9SMartin KaFai Lau 
21553da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
21563da59bd9SMartin KaFai Lau 		return NULL;
21573da59bd9SMartin KaFai Lau 
21583da59bd9SMartin KaFai Lau 	return &rt->dst;
21593da59bd9SMartin KaFai Lau }
21603da59bd9SMartin KaFai Lau 
2161a68886a6SDavid Ahern static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt,
2162a68886a6SDavid Ahern 					    struct fib6_info *from,
2163a68886a6SDavid Ahern 					    u32 cookie)
21643da59bd9SMartin KaFai Lau {
21655973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
21665973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
2167a68886a6SDavid Ahern 	    fib6_check(from, cookie))
21683da59bd9SMartin KaFai Lau 		return &rt->dst;
21693da59bd9SMartin KaFai Lau 	else
21703da59bd9SMartin KaFai Lau 		return NULL;
21713da59bd9SMartin KaFai Lau }
21723da59bd9SMartin KaFai Lau 
21731da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
21741da177e4SLinus Torvalds {
2175a87b7dc9SDavid Ahern 	struct dst_entry *dst_ret;
2176a68886a6SDavid Ahern 	struct fib6_info *from;
21771da177e4SLinus Torvalds 	struct rt6_info *rt;
21781da177e4SLinus Torvalds 
2179a87b7dc9SDavid Ahern 	rt = container_of(dst, struct rt6_info, dst);
2180a87b7dc9SDavid Ahern 
2181a87b7dc9SDavid Ahern 	rcu_read_lock();
21821da177e4SLinus Torvalds 
21836f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
21846f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
21856f3118b5SNicolas Dichtel 	 * into this function always.
21866f3118b5SNicolas Dichtel 	 */
2187e3bc10bdSHannes Frederic Sowa 
2188a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
21894b32b5adSMartin KaFai Lau 
2190a68886a6SDavid Ahern 	if (from && (rt->rt6i_flags & RTF_PCPU ||
2191a68886a6SDavid Ahern 	    unlikely(!list_empty(&rt->rt6i_uncached))))
2192a68886a6SDavid Ahern 		dst_ret = rt6_dst_from_check(rt, from, cookie);
21933da59bd9SMartin KaFai Lau 	else
2194a68886a6SDavid Ahern 		dst_ret = rt6_check(rt, from, cookie);
2195a87b7dc9SDavid Ahern 
2196a87b7dc9SDavid Ahern 	rcu_read_unlock();
2197a87b7dc9SDavid Ahern 
2198a87b7dc9SDavid Ahern 	return dst_ret;
21991da177e4SLinus Torvalds }
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
22021da177e4SLinus Torvalds {
22031da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
22041da177e4SLinus Torvalds 
22051da177e4SLinus Torvalds 	if (rt) {
220654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
2207c3c14da0SDavid Ahern 			rcu_read_lock();
220854c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
220993531c67SDavid Ahern 				rt6_remove_exception_rt(rt);
221054c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
22111da177e4SLinus Torvalds 			}
2212c3c14da0SDavid Ahern 			rcu_read_unlock();
221354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
221454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
221554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
221654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
221754c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
221854c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
22221da177e4SLinus Torvalds {
22231da177e4SLinus Torvalds 	struct rt6_info *rt;
22241da177e4SLinus Torvalds 
22253ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
22261da177e4SLinus Torvalds 
2227adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
22281da177e4SLinus Torvalds 	if (rt) {
22298a14e46fSDavid Ahern 		rcu_read_lock();
22301eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
223193531c67SDavid Ahern 			rt6_remove_exception_rt(rt);
2232c5cff856SWei Wang 		} else {
2233a68886a6SDavid Ahern 			struct fib6_info *from;
2234c5cff856SWei Wang 			struct fib6_node *fn;
2235c5cff856SWei Wang 
2236a68886a6SDavid Ahern 			from = rcu_dereference(rt->from);
2237a68886a6SDavid Ahern 			if (from) {
2238a68886a6SDavid Ahern 				fn = rcu_dereference(from->fib6_node);
2239c5cff856SWei Wang 				if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2240c5cff856SWei Wang 					fn->fn_sernum = -1;
2241a68886a6SDavid Ahern 			}
22421da177e4SLinus Torvalds 		}
22431da177e4SLinus Torvalds 		rcu_read_unlock();
22441da177e4SLinus Torvalds 	}
22451da177e4SLinus Torvalds }
22461da177e4SLinus Torvalds 
22476a3e030fSDavid Ahern static void rt6_update_expires(struct rt6_info *rt0, int timeout)
22486a3e030fSDavid Ahern {
2249a68886a6SDavid Ahern 	if (!(rt0->rt6i_flags & RTF_EXPIRES)) {
2250a68886a6SDavid Ahern 		struct fib6_info *from;
2251a68886a6SDavid Ahern 
2252a68886a6SDavid Ahern 		rcu_read_lock();
2253a68886a6SDavid Ahern 		from = rcu_dereference(rt0->from);
2254a68886a6SDavid Ahern 		if (from)
2255a68886a6SDavid Ahern 			rt0->dst.expires = from->expires;
2256a68886a6SDavid Ahern 		rcu_read_unlock();
2257a68886a6SDavid Ahern 	}
22586a3e030fSDavid Ahern 
22596a3e030fSDavid Ahern 	dst_set_expires(&rt0->dst, timeout);
22606a3e030fSDavid Ahern 	rt0->rt6i_flags |= RTF_EXPIRES;
22616700c270SDavid S. Miller }
22621da177e4SLinus Torvalds 
226345e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
226445e4fd26SMartin KaFai Lau {
226545e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
226645e4fd26SMartin KaFai Lau 
2267d4ead6b3SDavid Ahern 	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
226845e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
226945e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
227045e4fd26SMartin KaFai Lau }
227145e4fd26SMartin KaFai Lau 
22720d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
22730d3f6d29SMartin KaFai Lau {
22740d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
22751490ed2aSPaolo Abeni 		(rt->rt6i_flags & RTF_PCPU || rcu_access_pointer(rt->from));
22760d3f6d29SMartin KaFai Lau }
22770d3f6d29SMartin KaFai Lau 
227845e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
227945e4fd26SMartin KaFai Lau 				 const struct ipv6hdr *iph, u32 mtu)
22801da177e4SLinus Torvalds {
22810dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
22821da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
22831da177e4SLinus Torvalds 
228419bda36cSXin Long 	if (dst_metric_locked(dst, RTAX_MTU))
228519bda36cSXin Long 		return;
228619bda36cSXin Long 
228745e4fd26SMartin KaFai Lau 	if (iph) {
228845e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
228945e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
229045e4fd26SMartin KaFai Lau 	} else if (sk) {
229145e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
229245e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
229345e4fd26SMartin KaFai Lau 	} else {
22940dec879fSJulian Anastasov 		daddr = NULL;
22950dec879fSJulian Anastasov 		saddr = NULL;
22961da177e4SLinus Torvalds 	}
22970dec879fSJulian Anastasov 	dst_confirm_neigh(dst, daddr);
22980dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
22990dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
23000dec879fSJulian Anastasov 		return;
23010dec879fSJulian Anastasov 
23020dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
23030dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
23042b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
23052b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
23062b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
23070dec879fSJulian Anastasov 	} else if (daddr) {
2308a68886a6SDavid Ahern 		struct fib6_info *from;
23090dec879fSJulian Anastasov 		struct rt6_info *nrt6;
23100dec879fSJulian Anastasov 
23114d85cd0cSDavid Ahern 		rcu_read_lock();
2312a68886a6SDavid Ahern 		from = rcu_dereference(rt6->from);
2313a68886a6SDavid Ahern 		nrt6 = ip6_rt_cache_alloc(from, daddr, saddr);
231445e4fd26SMartin KaFai Lau 		if (nrt6) {
231545e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
2316a68886a6SDavid Ahern 			if (rt6_insert_exception(nrt6, from))
23172b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
231845e4fd26SMartin KaFai Lau 		}
2319a68886a6SDavid Ahern 		rcu_read_unlock();
232045e4fd26SMartin KaFai Lau 	}
232145e4fd26SMartin KaFai Lau }
232245e4fd26SMartin KaFai Lau 
232345e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
232445e4fd26SMartin KaFai Lau 			       struct sk_buff *skb, u32 mtu)
232545e4fd26SMartin KaFai Lau {
232645e4fd26SMartin KaFai Lau 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
23271da177e4SLinus Torvalds }
23281da177e4SLinus Torvalds 
232942ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2330e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
233181aded24SDavid S. Miller {
233281aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
233381aded24SDavid S. Miller 	struct dst_entry *dst;
2334dc92095dSMaciej Żenczykowski 	struct flowi6 fl6 = {
2335dc92095dSMaciej Żenczykowski 		.flowi6_oif = oif,
2336dc92095dSMaciej Żenczykowski 		.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark),
2337dc92095dSMaciej Żenczykowski 		.daddr = iph->daddr,
2338dc92095dSMaciej Żenczykowski 		.saddr = iph->saddr,
2339dc92095dSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
2340dc92095dSMaciej Żenczykowski 		.flowi6_uid = uid,
2341dc92095dSMaciej Żenczykowski 	};
234281aded24SDavid S. Miller 
234381aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
234481aded24SDavid S. Miller 	if (!dst->error)
234545e4fd26SMartin KaFai Lau 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
234681aded24SDavid S. Miller 	dst_release(dst);
234781aded24SDavid S. Miller }
234881aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
234981aded24SDavid S. Miller 
235081aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
235181aded24SDavid S. Miller {
23527ddacfa5SDavid Ahern 	int oif = sk->sk_bound_dev_if;
235333c162a9SMartin KaFai Lau 	struct dst_entry *dst;
235433c162a9SMartin KaFai Lau 
23557ddacfa5SDavid Ahern 	if (!oif && skb->dev)
23567ddacfa5SDavid Ahern 		oif = l3mdev_master_ifindex(skb->dev);
23577ddacfa5SDavid Ahern 
23587ddacfa5SDavid Ahern 	ip6_update_pmtu(skb, sock_net(sk), mtu, oif, sk->sk_mark, sk->sk_uid);
235933c162a9SMartin KaFai Lau 
236033c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
236133c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
236233c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
236333c162a9SMartin KaFai Lau 		return;
236433c162a9SMartin KaFai Lau 
236533c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
236633c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
236733c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
236833c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
236981aded24SDavid S. Miller }
237081aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
237181aded24SDavid S. Miller 
23727d6850f7SAlexey Kodanev void ip6_sk_dst_store_flow(struct sock *sk, struct dst_entry *dst,
23737d6850f7SAlexey Kodanev 			   const struct flowi6 *fl6)
23747d6850f7SAlexey Kodanev {
23757d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
23767d6850f7SAlexey Kodanev 	struct ipv6_pinfo *np = inet6_sk(sk);
23777d6850f7SAlexey Kodanev #endif
23787d6850f7SAlexey Kodanev 
23797d6850f7SAlexey Kodanev 	ip6_dst_store(sk, dst,
23807d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->daddr, &sk->sk_v6_daddr) ?
23817d6850f7SAlexey Kodanev 		      &sk->sk_v6_daddr : NULL,
23827d6850f7SAlexey Kodanev #ifdef CONFIG_IPV6_SUBTREES
23837d6850f7SAlexey Kodanev 		      ipv6_addr_equal(&fl6->saddr, &np->saddr) ?
23847d6850f7SAlexey Kodanev 		      &np->saddr :
23857d6850f7SAlexey Kodanev #endif
23867d6850f7SAlexey Kodanev 		      NULL);
23877d6850f7SAlexey Kodanev }
23887d6850f7SAlexey Kodanev 
2389b55b76b2SDuan Jiong /* Handle redirects */
2390b55b76b2SDuan Jiong struct ip6rd_flowi {
2391b55b76b2SDuan Jiong 	struct flowi6 fl6;
2392b55b76b2SDuan Jiong 	struct in6_addr gateway;
2393b55b76b2SDuan Jiong };
2394b55b76b2SDuan Jiong 
2395b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
2396b55b76b2SDuan Jiong 					     struct fib6_table *table,
2397b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2398b75cc8f9SDavid Ahern 					     const struct sk_buff *skb,
2399b55b76b2SDuan Jiong 					     int flags)
2400b55b76b2SDuan Jiong {
2401b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
240223fb93a4SDavid Ahern 	struct rt6_info *ret = NULL, *rt_cache;
24038d1c802bSDavid Ahern 	struct fib6_info *rt;
2404b55b76b2SDuan Jiong 	struct fib6_node *fn;
2405b55b76b2SDuan Jiong 
2406b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
240767c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2408b55b76b2SDuan Jiong 	 *
2409b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2410b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2411b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2412b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2413b55b76b2SDuan Jiong 	 * routes.
2414b55b76b2SDuan Jiong 	 */
2415b55b76b2SDuan Jiong 
241666f5d6ceSWei Wang 	rcu_read_lock();
24176454743bSDavid Ahern 	fn = fib6_node_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2418b55b76b2SDuan Jiong restart:
241966f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
2420ad1601aeSDavid Ahern 		if (rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD)
24218067bb8cSIdo Schimmel 			continue;
242214895687SDavid Ahern 		if (fib6_check_expired(rt))
2423b55b76b2SDuan Jiong 			continue;
242493c2fb25SDavid Ahern 		if (rt->fib6_flags & RTF_REJECT)
2425b55b76b2SDuan Jiong 			break;
2426bdf00467SDavid Ahern 		if (!rt->fib6_nh.fib_nh_gw_family)
2427b55b76b2SDuan Jiong 			continue;
2428ad1601aeSDavid Ahern 		if (fl6->flowi6_oif != rt->fib6_nh.fib_nh_dev->ifindex)
2429b55b76b2SDuan Jiong 			continue;
24302b760fcfSWei Wang 		/* rt_cache's gateway might be different from its 'parent'
24312b760fcfSWei Wang 		 * in the case of an ip redirect.
24322b760fcfSWei Wang 		 * So we keep searching in the exception table if the gateway
24332b760fcfSWei Wang 		 * is different.
24342b760fcfSWei Wang 		 */
2435ad1601aeSDavid Ahern 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->fib6_nh.fib_nh_gw6)) {
24362b760fcfSWei Wang 			rt_cache = rt6_find_cached_rt(rt,
24372b760fcfSWei Wang 						      &fl6->daddr,
24382b760fcfSWei Wang 						      &fl6->saddr);
24392b760fcfSWei Wang 			if (rt_cache &&
24402b760fcfSWei Wang 			    ipv6_addr_equal(&rdfl->gateway,
24412b760fcfSWei Wang 					    &rt_cache->rt6i_gateway)) {
244223fb93a4SDavid Ahern 				ret = rt_cache;
24432b760fcfSWei Wang 				break;
24442b760fcfSWei Wang 			}
2445b55b76b2SDuan Jiong 			continue;
24462b760fcfSWei Wang 		}
2447b55b76b2SDuan Jiong 		break;
2448b55b76b2SDuan Jiong 	}
2449b55b76b2SDuan Jiong 
2450b55b76b2SDuan Jiong 	if (!rt)
2451421842edSDavid Ahern 		rt = net->ipv6.fib6_null_entry;
245293c2fb25SDavid Ahern 	else if (rt->fib6_flags & RTF_REJECT) {
245323fb93a4SDavid Ahern 		ret = net->ipv6.ip6_null_entry;
2454b0a1ba59SMartin KaFai Lau 		goto out;
2455b0a1ba59SMartin KaFai Lau 	}
2456b0a1ba59SMartin KaFai Lau 
2457421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
2458a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2459a3c00e46SMartin KaFai Lau 		if (fn)
2460a3c00e46SMartin KaFai Lau 			goto restart;
2461b55b76b2SDuan Jiong 	}
2462a3c00e46SMartin KaFai Lau 
2463b0a1ba59SMartin KaFai Lau out:
246423fb93a4SDavid Ahern 	if (ret)
246510585b43SDavid Ahern 		ip6_hold_safe(net, &ret);
246623fb93a4SDavid Ahern 	else
246723fb93a4SDavid Ahern 		ret = ip6_create_rt_rcu(rt);
2468b55b76b2SDuan Jiong 
246966f5d6ceSWei Wang 	rcu_read_unlock();
2470b55b76b2SDuan Jiong 
2471b65f164dSPaolo Abeni 	trace_fib6_table_lookup(net, rt, table, fl6);
247223fb93a4SDavid Ahern 	return ret;
2473b55b76b2SDuan Jiong };
2474b55b76b2SDuan Jiong 
2475b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
2476b55b76b2SDuan Jiong 					    const struct flowi6 *fl6,
2477b75cc8f9SDavid Ahern 					    const struct sk_buff *skb,
2478b55b76b2SDuan Jiong 					    const struct in6_addr *gateway)
2479b55b76b2SDuan Jiong {
2480b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2481b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
2482b55b76b2SDuan Jiong 
2483b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
2484b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
2485b55b76b2SDuan Jiong 
2486b75cc8f9SDavid Ahern 	return fib6_rule_lookup(net, &rdfl.fl6, skb,
2487b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
2488b55b76b2SDuan Jiong }
2489b55b76b2SDuan Jiong 
2490e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2491e2d118a1SLorenzo Colitti 		  kuid_t uid)
24923a5ad2eeSDavid S. Miller {
24933a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
24943a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
24951f7f10acSMaciej Żenczykowski 	struct flowi6 fl6 = {
24961f7f10acSMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
24971f7f10acSMaciej Żenczykowski 		.flowi6_oif = oif,
24981f7f10acSMaciej Żenczykowski 		.flowi6_mark = mark,
24991f7f10acSMaciej Żenczykowski 		.daddr = iph->daddr,
25001f7f10acSMaciej Żenczykowski 		.saddr = iph->saddr,
25011f7f10acSMaciej Żenczykowski 		.flowlabel = ip6_flowinfo(iph),
25021f7f10acSMaciej Żenczykowski 		.flowi6_uid = uid,
25031f7f10acSMaciej Żenczykowski 	};
25043a5ad2eeSDavid S. Miller 
2505b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &ipv6_hdr(skb)->saddr);
25066700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
25073a5ad2eeSDavid S. Miller 	dst_release(dst);
25083a5ad2eeSDavid S. Miller }
25093a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
25103a5ad2eeSDavid S. Miller 
2511d456336dSMaciej Żenczykowski void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif)
2512c92a59ecSDuan Jiong {
2513c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2514c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2515c92a59ecSDuan Jiong 	struct dst_entry *dst;
25160b26fb17SMaciej Żenczykowski 	struct flowi6 fl6 = {
25170b26fb17SMaciej Żenczykowski 		.flowi6_iif = LOOPBACK_IFINDEX,
25180b26fb17SMaciej Żenczykowski 		.flowi6_oif = oif,
25190b26fb17SMaciej Żenczykowski 		.daddr = msg->dest,
25200b26fb17SMaciej Żenczykowski 		.saddr = iph->daddr,
25210b26fb17SMaciej Żenczykowski 		.flowi6_uid = sock_net_uid(net, NULL),
25220b26fb17SMaciej Żenczykowski 	};
2523c92a59ecSDuan Jiong 
2524b75cc8f9SDavid Ahern 	dst = ip6_route_redirect(net, &fl6, skb, &iph->saddr);
2525c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
2526c92a59ecSDuan Jiong 	dst_release(dst);
2527c92a59ecSDuan Jiong }
2528c92a59ecSDuan Jiong 
25293a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
25303a5ad2eeSDavid S. Miller {
2531e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2532e2d118a1SLorenzo Colitti 		     sk->sk_uid);
25333a5ad2eeSDavid S. Miller }
25343a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
25353a5ad2eeSDavid S. Miller 
25360dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
25371da177e4SLinus Torvalds {
25380dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
25390dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
25400dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
25410dbaee3bSDavid S. Miller 
25421da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
25431da177e4SLinus Torvalds 
25445578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
25455578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds 	/*
25481da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
25491da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
25501da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
25511da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
25521da177e4SLinus Torvalds 	 */
25531da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
25541da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
25551da177e4SLinus Torvalds 	return mtu;
25561da177e4SLinus Torvalds }
25571da177e4SLinus Torvalds 
2558ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
2559d33e4553SDavid S. Miller {
2560d33e4553SDavid S. Miller 	struct inet6_dev *idev;
2561d4ead6b3SDavid Ahern 	unsigned int mtu;
2562618f9bc7SSteffen Klassert 
25634b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
25644b32b5adSMartin KaFai Lau 	if (mtu)
25654b32b5adSMartin KaFai Lau 		goto out;
25664b32b5adSMartin KaFai Lau 
2567618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
2568d33e4553SDavid S. Miller 
2569d33e4553SDavid S. Miller 	rcu_read_lock();
2570d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
2571d33e4553SDavid S. Miller 	if (idev)
2572d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
2573d33e4553SDavid S. Miller 	rcu_read_unlock();
2574d33e4553SDavid S. Miller 
257530f78d8eSEric Dumazet out:
257614972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
257714972cbdSRoopa Prabhu 
257814972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
2579d33e4553SDavid S. Miller }
2580d33e4553SDavid S. Miller 
2581901731b8SDavid Ahern /* MTU selection:
2582901731b8SDavid Ahern  * 1. mtu on route is locked - use it
2583901731b8SDavid Ahern  * 2. mtu from nexthop exception
2584901731b8SDavid Ahern  * 3. mtu from egress device
2585901731b8SDavid Ahern  *
2586901731b8SDavid Ahern  * based on ip6_dst_mtu_forward and exception logic of
2587901731b8SDavid Ahern  * rt6_find_cached_rt; called with rcu_read_lock
2588901731b8SDavid Ahern  */
2589901731b8SDavid Ahern u32 ip6_mtu_from_fib6(struct fib6_info *f6i, struct in6_addr *daddr,
2590901731b8SDavid Ahern 		      struct in6_addr *saddr)
2591901731b8SDavid Ahern {
2592901731b8SDavid Ahern 	struct rt6_exception_bucket *bucket;
2593901731b8SDavid Ahern 	struct rt6_exception *rt6_ex;
2594901731b8SDavid Ahern 	struct in6_addr *src_key;
2595901731b8SDavid Ahern 	struct inet6_dev *idev;
2596901731b8SDavid Ahern 	u32 mtu = 0;
2597901731b8SDavid Ahern 
2598901731b8SDavid Ahern 	if (unlikely(fib6_metric_locked(f6i, RTAX_MTU))) {
2599901731b8SDavid Ahern 		mtu = f6i->fib6_pmtu;
2600901731b8SDavid Ahern 		if (mtu)
2601901731b8SDavid Ahern 			goto out;
2602901731b8SDavid Ahern 	}
2603901731b8SDavid Ahern 
2604901731b8SDavid Ahern 	src_key = NULL;
2605901731b8SDavid Ahern #ifdef CONFIG_IPV6_SUBTREES
2606901731b8SDavid Ahern 	if (f6i->fib6_src.plen)
2607901731b8SDavid Ahern 		src_key = saddr;
2608901731b8SDavid Ahern #endif
2609901731b8SDavid Ahern 
2610901731b8SDavid Ahern 	bucket = rcu_dereference(f6i->rt6i_exception_bucket);
2611901731b8SDavid Ahern 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
2612901731b8SDavid Ahern 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
2613901731b8SDavid Ahern 		mtu = dst_metric_raw(&rt6_ex->rt6i->dst, RTAX_MTU);
2614901731b8SDavid Ahern 
2615901731b8SDavid Ahern 	if (likely(!mtu)) {
2616901731b8SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(f6i);
2617901731b8SDavid Ahern 
2618901731b8SDavid Ahern 		mtu = IPV6_MIN_MTU;
2619901731b8SDavid Ahern 		idev = __in6_dev_get(dev);
2620901731b8SDavid Ahern 		if (idev && idev->cnf.mtu6 > mtu)
2621901731b8SDavid Ahern 			mtu = idev->cnf.mtu6;
2622901731b8SDavid Ahern 	}
2623901731b8SDavid Ahern 
2624901731b8SDavid Ahern 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
2625901731b8SDavid Ahern out:
2626901731b8SDavid Ahern 	return mtu - lwtunnel_headroom(fib6_info_nh_lwt(f6i), mtu);
2627901731b8SDavid Ahern }
2628901731b8SDavid Ahern 
26293b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
263087a11578SDavid S. Miller 				  struct flowi6 *fl6)
26311da177e4SLinus Torvalds {
263287a11578SDavid S. Miller 	struct dst_entry *dst;
26331da177e4SLinus Torvalds 	struct rt6_info *rt;
26341da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
2635c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26361da177e4SLinus Torvalds 
263738308473SDavid S. Miller 	if (unlikely(!idev))
2638122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
26391da177e4SLinus Torvalds 
2640ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
264138308473SDavid S. Miller 	if (unlikely(!rt)) {
26421da177e4SLinus Torvalds 		in6_dev_put(idev);
264387a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
26441da177e4SLinus Torvalds 		goto out;
26451da177e4SLinus Torvalds 	}
26461da177e4SLinus Torvalds 
26478e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
2648588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
26498e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
2650550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
265187a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
26528e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
26538e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
265414edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
26551da177e4SLinus Torvalds 
26564c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
2657587fea74SWei Wang 	 * do proper release of the net_device
2658587fea74SWei Wang 	 */
2659587fea74SWei Wang 	rt6_uncached_list_add(rt);
266081eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
26611da177e4SLinus Torvalds 
266287a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
266387a11578SDavid S. Miller 
26641da177e4SLinus Torvalds out:
266587a11578SDavid S. Miller 	return dst;
26661da177e4SLinus Torvalds }
26671da177e4SLinus Torvalds 
2668569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
26691da177e4SLinus Torvalds {
267086393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
26717019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
26727019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
26737019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
26747019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
26757019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
2676fc66f95cSEric Dumazet 	int entries;
26771da177e4SLinus Torvalds 
2678fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
267949a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
2680fc66f95cSEric Dumazet 	    entries <= rt_max_size)
26811da177e4SLinus Torvalds 		goto out;
26821da177e4SLinus Torvalds 
26836891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
268414956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
2685fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
2686fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
26877019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
26881da177e4SLinus Torvalds out:
26897019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
2690fc66f95cSEric Dumazet 	return entries > rt_max_size;
26911da177e4SLinus Torvalds }
26921da177e4SLinus Torvalds 
26938c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net,
26948c14586fSDavid Ahern 					    struct fib6_config *cfg,
2695f4797b33SDavid Ahern 					    const struct in6_addr *gw_addr,
2696f4797b33SDavid Ahern 					    u32 tbid, int flags)
26978c14586fSDavid Ahern {
26988c14586fSDavid Ahern 	struct flowi6 fl6 = {
26998c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
27008c14586fSDavid Ahern 		.daddr = *gw_addr,
27018c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
27028c14586fSDavid Ahern 	};
27038c14586fSDavid Ahern 	struct fib6_table *table;
27048c14586fSDavid Ahern 	struct rt6_info *rt;
27058c14586fSDavid Ahern 
2706f4797b33SDavid Ahern 	table = fib6_get_table(net, tbid);
27078c14586fSDavid Ahern 	if (!table)
27088c14586fSDavid Ahern 		return NULL;
27098c14586fSDavid Ahern 
27108c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
27118c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
27128c14586fSDavid Ahern 
2713f4797b33SDavid Ahern 	flags |= RT6_LOOKUP_F_IGNORE_LINKSTATE;
2714b75cc8f9SDavid Ahern 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, NULL, flags);
27158c14586fSDavid Ahern 
27168c14586fSDavid Ahern 	/* if table lookup failed, fall back to full lookup */
27178c14586fSDavid Ahern 	if (rt == net->ipv6.ip6_null_entry) {
27188c14586fSDavid Ahern 		ip6_rt_put(rt);
27198c14586fSDavid Ahern 		rt = NULL;
27208c14586fSDavid Ahern 	}
27218c14586fSDavid Ahern 
27228c14586fSDavid Ahern 	return rt;
27238c14586fSDavid Ahern }
27248c14586fSDavid Ahern 
2725fc1e64e1SDavid Ahern static int ip6_route_check_nh_onlink(struct net *net,
2726fc1e64e1SDavid Ahern 				     struct fib6_config *cfg,
27279fbb704cSDavid Ahern 				     const struct net_device *dev,
2728fc1e64e1SDavid Ahern 				     struct netlink_ext_ack *extack)
2729fc1e64e1SDavid Ahern {
273044750f84SDavid Ahern 	u32 tbid = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
2731fc1e64e1SDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
2732fc1e64e1SDavid Ahern 	u32 flags = RTF_LOCAL | RTF_ANYCAST | RTF_REJECT;
2733bf1dc8baSPaolo Abeni 	struct fib6_info *from;
2734fc1e64e1SDavid Ahern 	struct rt6_info *grt;
2735fc1e64e1SDavid Ahern 	int err;
2736fc1e64e1SDavid Ahern 
2737fc1e64e1SDavid Ahern 	err = 0;
2738fc1e64e1SDavid Ahern 	grt = ip6_nh_lookup_table(net, cfg, gw_addr, tbid, 0);
2739fc1e64e1SDavid Ahern 	if (grt) {
2740bf1dc8baSPaolo Abeni 		rcu_read_lock();
2741bf1dc8baSPaolo Abeni 		from = rcu_dereference(grt->from);
274258e354c0SDavid Ahern 		if (!grt->dst.error &&
27434ed591c8SDavid Ahern 		    /* ignore match if it is the default route */
2744bf1dc8baSPaolo Abeni 		    from && !ipv6_addr_any(&from->fib6_dst.addr) &&
274558e354c0SDavid Ahern 		    (grt->rt6i_flags & flags || dev != grt->dst.dev)) {
274644750f84SDavid Ahern 			NL_SET_ERR_MSG(extack,
274744750f84SDavid Ahern 				       "Nexthop has invalid gateway or device mismatch");
2748fc1e64e1SDavid Ahern 			err = -EINVAL;
2749fc1e64e1SDavid Ahern 		}
2750bf1dc8baSPaolo Abeni 		rcu_read_unlock();
2751fc1e64e1SDavid Ahern 
2752fc1e64e1SDavid Ahern 		ip6_rt_put(grt);
2753fc1e64e1SDavid Ahern 	}
2754fc1e64e1SDavid Ahern 
2755fc1e64e1SDavid Ahern 	return err;
2756fc1e64e1SDavid Ahern }
2757fc1e64e1SDavid Ahern 
27581edce99fSDavid Ahern static int ip6_route_check_nh(struct net *net,
27591edce99fSDavid Ahern 			      struct fib6_config *cfg,
27601edce99fSDavid Ahern 			      struct net_device **_dev,
27611edce99fSDavid Ahern 			      struct inet6_dev **idev)
27621edce99fSDavid Ahern {
27631edce99fSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
27641edce99fSDavid Ahern 	struct net_device *dev = _dev ? *_dev : NULL;
27651edce99fSDavid Ahern 	struct rt6_info *grt = NULL;
27661edce99fSDavid Ahern 	int err = -EHOSTUNREACH;
27671edce99fSDavid Ahern 
27681edce99fSDavid Ahern 	if (cfg->fc_table) {
2769f4797b33SDavid Ahern 		int flags = RT6_LOOKUP_F_IFACE;
2770f4797b33SDavid Ahern 
2771f4797b33SDavid Ahern 		grt = ip6_nh_lookup_table(net, cfg, gw_addr,
2772f4797b33SDavid Ahern 					  cfg->fc_table, flags);
27731edce99fSDavid Ahern 		if (grt) {
27741edce99fSDavid Ahern 			if (grt->rt6i_flags & RTF_GATEWAY ||
27751edce99fSDavid Ahern 			    (dev && dev != grt->dst.dev)) {
27761edce99fSDavid Ahern 				ip6_rt_put(grt);
27771edce99fSDavid Ahern 				grt = NULL;
27781edce99fSDavid Ahern 			}
27791edce99fSDavid Ahern 		}
27801edce99fSDavid Ahern 	}
27811edce99fSDavid Ahern 
27821edce99fSDavid Ahern 	if (!grt)
2783b75cc8f9SDavid Ahern 		grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, NULL, 1);
27841edce99fSDavid Ahern 
27851edce99fSDavid Ahern 	if (!grt)
27861edce99fSDavid Ahern 		goto out;
27871edce99fSDavid Ahern 
27881edce99fSDavid Ahern 	if (dev) {
27891edce99fSDavid Ahern 		if (dev != grt->dst.dev) {
27901edce99fSDavid Ahern 			ip6_rt_put(grt);
27911edce99fSDavid Ahern 			goto out;
27921edce99fSDavid Ahern 		}
27931edce99fSDavid Ahern 	} else {
27941edce99fSDavid Ahern 		*_dev = dev = grt->dst.dev;
27951edce99fSDavid Ahern 		*idev = grt->rt6i_idev;
27961edce99fSDavid Ahern 		dev_hold(dev);
27971edce99fSDavid Ahern 		in6_dev_hold(grt->rt6i_idev);
27981edce99fSDavid Ahern 	}
27991edce99fSDavid Ahern 
28001edce99fSDavid Ahern 	if (!(grt->rt6i_flags & RTF_GATEWAY))
28011edce99fSDavid Ahern 		err = 0;
28021edce99fSDavid Ahern 
28031edce99fSDavid Ahern 	ip6_rt_put(grt);
28041edce99fSDavid Ahern 
28051edce99fSDavid Ahern out:
28061edce99fSDavid Ahern 	return err;
28071edce99fSDavid Ahern }
28081edce99fSDavid Ahern 
28099fbb704cSDavid Ahern static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
28109fbb704cSDavid Ahern 			   struct net_device **_dev, struct inet6_dev **idev,
28119fbb704cSDavid Ahern 			   struct netlink_ext_ack *extack)
28129fbb704cSDavid Ahern {
28139fbb704cSDavid Ahern 	const struct in6_addr *gw_addr = &cfg->fc_gateway;
28149fbb704cSDavid Ahern 	int gwa_type = ipv6_addr_type(gw_addr);
2815232378e8SDavid Ahern 	bool skip_dev = gwa_type & IPV6_ADDR_LINKLOCAL ? false : true;
28169fbb704cSDavid Ahern 	const struct net_device *dev = *_dev;
2817232378e8SDavid Ahern 	bool need_addr_check = !dev;
28189fbb704cSDavid Ahern 	int err = -EINVAL;
28199fbb704cSDavid Ahern 
28209fbb704cSDavid Ahern 	/* if gw_addr is local we will fail to detect this in case
28219fbb704cSDavid Ahern 	 * address is still TENTATIVE (DAD in progress). rt6_lookup()
28229fbb704cSDavid Ahern 	 * will return already-added prefix route via interface that
28239fbb704cSDavid Ahern 	 * prefix route was assigned to, which might be non-loopback.
28249fbb704cSDavid Ahern 	 */
2825232378e8SDavid Ahern 	if (dev &&
2826232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2827232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
28289fbb704cSDavid Ahern 		goto out;
28299fbb704cSDavid Ahern 	}
28309fbb704cSDavid Ahern 
28319fbb704cSDavid Ahern 	if (gwa_type != (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST)) {
28329fbb704cSDavid Ahern 		/* IPv6 strictly inhibits using not link-local
28339fbb704cSDavid Ahern 		 * addresses as nexthop address.
28349fbb704cSDavid Ahern 		 * Otherwise, router will not able to send redirects.
28359fbb704cSDavid Ahern 		 * It is very good, but in some (rare!) circumstances
28369fbb704cSDavid Ahern 		 * (SIT, PtP, NBMA NOARP links) it is handy to allow
28379fbb704cSDavid Ahern 		 * some exceptions. --ANK
28389fbb704cSDavid Ahern 		 * We allow IPv4-mapped nexthops to support RFC4798-type
28399fbb704cSDavid Ahern 		 * addressing
28409fbb704cSDavid Ahern 		 */
28419fbb704cSDavid Ahern 		if (!(gwa_type & (IPV6_ADDR_UNICAST | IPV6_ADDR_MAPPED))) {
28429fbb704cSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
28439fbb704cSDavid Ahern 			goto out;
28449fbb704cSDavid Ahern 		}
28459fbb704cSDavid Ahern 
28469fbb704cSDavid Ahern 		if (cfg->fc_flags & RTNH_F_ONLINK)
28479fbb704cSDavid Ahern 			err = ip6_route_check_nh_onlink(net, cfg, dev, extack);
28489fbb704cSDavid Ahern 		else
28499fbb704cSDavid Ahern 			err = ip6_route_check_nh(net, cfg, _dev, idev);
28509fbb704cSDavid Ahern 
28519fbb704cSDavid Ahern 		if (err)
28529fbb704cSDavid Ahern 			goto out;
28539fbb704cSDavid Ahern 	}
28549fbb704cSDavid Ahern 
28559fbb704cSDavid Ahern 	/* reload in case device was changed */
28569fbb704cSDavid Ahern 	dev = *_dev;
28579fbb704cSDavid Ahern 
28589fbb704cSDavid Ahern 	err = -EINVAL;
28599fbb704cSDavid Ahern 	if (!dev) {
28609fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack, "Egress device not specified");
28619fbb704cSDavid Ahern 		goto out;
28629fbb704cSDavid Ahern 	} else if (dev->flags & IFF_LOOPBACK) {
28639fbb704cSDavid Ahern 		NL_SET_ERR_MSG(extack,
28649fbb704cSDavid Ahern 			       "Egress device can not be loopback device for this route");
28659fbb704cSDavid Ahern 		goto out;
28669fbb704cSDavid Ahern 	}
2867232378e8SDavid Ahern 
2868232378e8SDavid Ahern 	/* if we did not check gw_addr above, do so now that the
2869232378e8SDavid Ahern 	 * egress device has been resolved.
2870232378e8SDavid Ahern 	 */
2871232378e8SDavid Ahern 	if (need_addr_check &&
2872232378e8SDavid Ahern 	    ipv6_chk_addr_and_flags(net, gw_addr, dev, skip_dev, 0, 0)) {
2873232378e8SDavid Ahern 		NL_SET_ERR_MSG(extack, "Gateway can not be a local address");
2874232378e8SDavid Ahern 		goto out;
2875232378e8SDavid Ahern 	}
2876232378e8SDavid Ahern 
28779fbb704cSDavid Ahern 	err = 0;
28789fbb704cSDavid Ahern out:
28799fbb704cSDavid Ahern 	return err;
28809fbb704cSDavid Ahern }
28819fbb704cSDavid Ahern 
288283c44251SDavid Ahern static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
288383c44251SDavid Ahern {
288483c44251SDavid Ahern 	if ((flags & RTF_REJECT) ||
288583c44251SDavid Ahern 	    (dev && (dev->flags & IFF_LOOPBACK) &&
288683c44251SDavid Ahern 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
288783c44251SDavid Ahern 	     !(flags & RTF_LOCAL)))
288883c44251SDavid Ahern 		return true;
288983c44251SDavid Ahern 
289083c44251SDavid Ahern 	return false;
289183c44251SDavid Ahern }
289283c44251SDavid Ahern 
289383c44251SDavid Ahern int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
289483c44251SDavid Ahern 		 struct fib6_config *cfg, gfp_t gfp_flags,
289583c44251SDavid Ahern 		 struct netlink_ext_ack *extack)
289683c44251SDavid Ahern {
289783c44251SDavid Ahern 	struct net_device *dev = NULL;
289883c44251SDavid Ahern 	struct inet6_dev *idev = NULL;
289983c44251SDavid Ahern 	int addr_type;
290083c44251SDavid Ahern 	int err;
290183c44251SDavid Ahern 
2902f1741730SDavid Ahern 	fib6_nh->fib_nh_family = AF_INET6;
2903f1741730SDavid Ahern 
290483c44251SDavid Ahern 	err = -ENODEV;
290583c44251SDavid Ahern 	if (cfg->fc_ifindex) {
290683c44251SDavid Ahern 		dev = dev_get_by_index(net, cfg->fc_ifindex);
290783c44251SDavid Ahern 		if (!dev)
290883c44251SDavid Ahern 			goto out;
290983c44251SDavid Ahern 		idev = in6_dev_get(dev);
291083c44251SDavid Ahern 		if (!idev)
291183c44251SDavid Ahern 			goto out;
291283c44251SDavid Ahern 	}
291383c44251SDavid Ahern 
291483c44251SDavid Ahern 	if (cfg->fc_flags & RTNH_F_ONLINK) {
291583c44251SDavid Ahern 		if (!dev) {
291683c44251SDavid Ahern 			NL_SET_ERR_MSG(extack,
291783c44251SDavid Ahern 				       "Nexthop device required for onlink");
291883c44251SDavid Ahern 			goto out;
291983c44251SDavid Ahern 		}
292083c44251SDavid Ahern 
292183c44251SDavid Ahern 		if (!(dev->flags & IFF_UP)) {
292283c44251SDavid Ahern 			NL_SET_ERR_MSG(extack, "Nexthop device is not up");
292383c44251SDavid Ahern 			err = -ENETDOWN;
292483c44251SDavid Ahern 			goto out;
292583c44251SDavid Ahern 		}
292683c44251SDavid Ahern 
2927ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_ONLINK;
292883c44251SDavid Ahern 	}
292983c44251SDavid Ahern 
2930ad1601aeSDavid Ahern 	fib6_nh->fib_nh_weight = 1;
293183c44251SDavid Ahern 
293283c44251SDavid Ahern 	/* We cannot add true routes via loopback here,
293383c44251SDavid Ahern 	 * they would result in kernel looping; promote them to reject routes
293483c44251SDavid Ahern 	 */
293583c44251SDavid Ahern 	addr_type = ipv6_addr_type(&cfg->fc_dst);
293683c44251SDavid Ahern 	if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
293783c44251SDavid Ahern 		/* hold loopback dev/idev if we haven't done so. */
293883c44251SDavid Ahern 		if (dev != net->loopback_dev) {
293983c44251SDavid Ahern 			if (dev) {
294083c44251SDavid Ahern 				dev_put(dev);
294183c44251SDavid Ahern 				in6_dev_put(idev);
294283c44251SDavid Ahern 			}
294383c44251SDavid Ahern 			dev = net->loopback_dev;
294483c44251SDavid Ahern 			dev_hold(dev);
294583c44251SDavid Ahern 			idev = in6_dev_get(dev);
294683c44251SDavid Ahern 			if (!idev) {
294783c44251SDavid Ahern 				err = -ENODEV;
294883c44251SDavid Ahern 				goto out;
294983c44251SDavid Ahern 			}
295083c44251SDavid Ahern 		}
295183c44251SDavid Ahern 		goto set_dev;
295283c44251SDavid Ahern 	}
295383c44251SDavid Ahern 
295483c44251SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY) {
295583c44251SDavid Ahern 		err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
295683c44251SDavid Ahern 		if (err)
295783c44251SDavid Ahern 			goto out;
295883c44251SDavid Ahern 
2959ad1601aeSDavid Ahern 		fib6_nh->fib_nh_gw6 = cfg->fc_gateway;
2960bdf00467SDavid Ahern 		fib6_nh->fib_nh_gw_family = AF_INET6;
296183c44251SDavid Ahern 	}
296283c44251SDavid Ahern 
296383c44251SDavid Ahern 	err = -ENODEV;
296483c44251SDavid Ahern 	if (!dev)
296583c44251SDavid Ahern 		goto out;
296683c44251SDavid Ahern 
296783c44251SDavid Ahern 	if (idev->cnf.disable_ipv6) {
296883c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
296983c44251SDavid Ahern 		err = -EACCES;
297083c44251SDavid Ahern 		goto out;
297183c44251SDavid Ahern 	}
297283c44251SDavid Ahern 
297383c44251SDavid Ahern 	if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
297483c44251SDavid Ahern 		NL_SET_ERR_MSG(extack, "Nexthop device is not up");
297583c44251SDavid Ahern 		err = -ENETDOWN;
297683c44251SDavid Ahern 		goto out;
297783c44251SDavid Ahern 	}
297883c44251SDavid Ahern 
297983c44251SDavid Ahern 	if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
298083c44251SDavid Ahern 	    !netif_carrier_ok(dev))
2981ad1601aeSDavid Ahern 		fib6_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
298283c44251SDavid Ahern 
2983979e276eSDavid Ahern 	err = fib_nh_common_init(&fib6_nh->nh_common, cfg->fc_encap,
2984979e276eSDavid Ahern 				 cfg->fc_encap_type, cfg, gfp_flags, extack);
2985979e276eSDavid Ahern 	if (err)
2986979e276eSDavid Ahern 		goto out;
298783c44251SDavid Ahern set_dev:
2988ad1601aeSDavid Ahern 	fib6_nh->fib_nh_dev = dev;
2989f1741730SDavid Ahern 	fib6_nh->fib_nh_oif = dev->ifindex;
299083c44251SDavid Ahern 	err = 0;
299183c44251SDavid Ahern out:
299283c44251SDavid Ahern 	if (idev)
299383c44251SDavid Ahern 		in6_dev_put(idev);
299483c44251SDavid Ahern 
299583c44251SDavid Ahern 	if (err) {
2996ad1601aeSDavid Ahern 		lwtstate_put(fib6_nh->fib_nh_lws);
2997ad1601aeSDavid Ahern 		fib6_nh->fib_nh_lws = NULL;
299883c44251SDavid Ahern 		if (dev)
299983c44251SDavid Ahern 			dev_put(dev);
300083c44251SDavid Ahern 	}
300183c44251SDavid Ahern 
300283c44251SDavid Ahern 	return err;
300383c44251SDavid Ahern }
300483c44251SDavid Ahern 
3005dac7d0f2SDavid Ahern void fib6_nh_release(struct fib6_nh *fib6_nh)
3006dac7d0f2SDavid Ahern {
3007979e276eSDavid Ahern 	fib_nh_common_release(&fib6_nh->nh_common);
3008dac7d0f2SDavid Ahern }
3009dac7d0f2SDavid Ahern 
30108d1c802bSDavid Ahern static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
3011acb54e3cSDavid Ahern 					      gfp_t gfp_flags,
3012333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
30131da177e4SLinus Torvalds {
30145578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
30158d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3016c71099acSThomas Graf 	struct fib6_table *table;
30178c5b83f0SRoopa Prabhu 	int err = -EINVAL;
301883c44251SDavid Ahern 	int addr_type;
30191da177e4SLinus Torvalds 
3020557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
3021d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
3022d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
3023557c44beSDavid Ahern 		goto out;
3024d5d531cbSDavid Ahern 	}
3025557c44beSDavid Ahern 
30262ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
30272ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
30282ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
30292ea2352eSWei Wang 		goto out;
30302ea2352eSWei Wang 	}
30312ea2352eSWei Wang 
3032e8478e80SDavid Ahern 	if (cfg->fc_type > RTN_MAX) {
3033e8478e80SDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid route type");
3034e8478e80SDavid Ahern 		goto out;
3035e8478e80SDavid Ahern 	}
3036e8478e80SDavid Ahern 
3037d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
3038d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
30398c5b83f0SRoopa Prabhu 		goto out;
3040d5d531cbSDavid Ahern 	}
3041d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
3042d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
3043d5d531cbSDavid Ahern 		goto out;
3044d5d531cbSDavid Ahern 	}
30451da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
3046d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
3047d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
3048d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
30498c5b83f0SRoopa Prabhu 		goto out;
3050d5d531cbSDavid Ahern 	}
30511da177e4SLinus Torvalds #endif
3052fc1e64e1SDavid Ahern 
3053c71099acSThomas Graf 	err = -ENOBUFS;
305438308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
3055d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
3056d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
305738308473SDavid S. Miller 		if (!table) {
3058f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
3059d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
3060d71314b4SMatti Vaittinen 		}
3061d71314b4SMatti Vaittinen 	} else {
3062d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
3063d71314b4SMatti Vaittinen 	}
306438308473SDavid S. Miller 
306538308473SDavid S. Miller 	if (!table)
3066c71099acSThomas Graf 		goto out;
3067c71099acSThomas Graf 
30681da177e4SLinus Torvalds 	err = -ENOMEM;
306993531c67SDavid Ahern 	rt = fib6_info_alloc(gfp_flags);
307093531c67SDavid Ahern 	if (!rt)
30711da177e4SLinus Torvalds 		goto out;
307293531c67SDavid Ahern 
3073d7e774f3SDavid Ahern 	rt->fib6_metrics = ip_fib_metrics_init(net, cfg->fc_mx, cfg->fc_mx_len,
3074d7e774f3SDavid Ahern 					       extack);
3075767a2217SDavid Ahern 	if (IS_ERR(rt->fib6_metrics)) {
3076767a2217SDavid Ahern 		err = PTR_ERR(rt->fib6_metrics);
3077fda21d46SEric Dumazet 		/* Do not leave garbage there. */
3078fda21d46SEric Dumazet 		rt->fib6_metrics = (struct dst_metrics *)&dst_default_metrics;
3079767a2217SDavid Ahern 		goto out;
3080767a2217SDavid Ahern 	}
3081767a2217SDavid Ahern 
308293531c67SDavid Ahern 	if (cfg->fc_flags & RTF_ADDRCONF)
308393531c67SDavid Ahern 		rt->dst_nocount = true;
30841da177e4SLinus Torvalds 
30851716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
308614895687SDavid Ahern 		fib6_set_expires(rt, jiffies +
30871716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
30881716a961SGao feng 	else
308914895687SDavid Ahern 		fib6_clean_expires(rt);
30901da177e4SLinus Torvalds 
309186872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
309286872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
309393c2fb25SDavid Ahern 	rt->fib6_protocol = cfg->fc_protocol;
309486872cb5SThomas Graf 
309583c44251SDavid Ahern 	rt->fib6_table = table;
309683c44251SDavid Ahern 	rt->fib6_metric = cfg->fc_metric;
309783c44251SDavid Ahern 	rt->fib6_type = cfg->fc_type;
30982b2450caSDavid Ahern 	rt->fib6_flags = cfg->fc_flags & ~RTF_GATEWAY;
309919e42e45SRoopa Prabhu 
310093c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
310193c2fb25SDavid Ahern 	rt->fib6_dst.plen = cfg->fc_dst_len;
310293c2fb25SDavid Ahern 	if (rt->fib6_dst.plen == 128)
31033b6761d1SDavid Ahern 		rt->dst_host = true;
31041da177e4SLinus Torvalds 
31051da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
310693c2fb25SDavid Ahern 	ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
310793c2fb25SDavid Ahern 	rt->fib6_src.plen = cfg->fc_src_len;
31081da177e4SLinus Torvalds #endif
310983c44251SDavid Ahern 	err = fib6_nh_init(net, &rt->fib6_nh, cfg, gfp_flags, extack);
31101da177e4SLinus Torvalds 	if (err)
31111da177e4SLinus Torvalds 		goto out;
31129fbb704cSDavid Ahern 
311383c44251SDavid Ahern 	/* We cannot add true routes via loopback here,
311483c44251SDavid Ahern 	 * they would result in kernel looping; promote them to reject routes
311583c44251SDavid Ahern 	 */
311683c44251SDavid Ahern 	addr_type = ipv6_addr_type(&cfg->fc_dst);
3117ad1601aeSDavid Ahern 	if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh.fib_nh_dev, addr_type))
311883c44251SDavid Ahern 		rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
3119955ec4cbSDavid Ahern 
3120c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
312183c44251SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
312283c44251SDavid Ahern 
3123c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
3124d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
3125c3968a85SDaniel Walter 			err = -EINVAL;
3126c3968a85SDaniel Walter 			goto out;
3127c3968a85SDaniel Walter 		}
312893c2fb25SDavid Ahern 		rt->fib6_prefsrc.addr = cfg->fc_prefsrc;
312993c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 128;
3130c3968a85SDaniel Walter 	} else
313193c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
3132c3968a85SDaniel Walter 
31338c5b83f0SRoopa Prabhu 	return rt;
31341da177e4SLinus Torvalds out:
313593531c67SDavid Ahern 	fib6_info_release(rt);
31368c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
31376b9ea5a6SRoopa Prabhu }
31386b9ea5a6SRoopa Prabhu 
3139acb54e3cSDavid Ahern int ip6_route_add(struct fib6_config *cfg, gfp_t gfp_flags,
3140333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
31416b9ea5a6SRoopa Prabhu {
31428d1c802bSDavid Ahern 	struct fib6_info *rt;
31436b9ea5a6SRoopa Prabhu 	int err;
31446b9ea5a6SRoopa Prabhu 
3145acb54e3cSDavid Ahern 	rt = ip6_route_info_create(cfg, gfp_flags, extack);
3146d4ead6b3SDavid Ahern 	if (IS_ERR(rt))
3147d4ead6b3SDavid Ahern 		return PTR_ERR(rt);
31486b9ea5a6SRoopa Prabhu 
3149d4ead6b3SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, extack);
315093531c67SDavid Ahern 	fib6_info_release(rt);
31516b9ea5a6SRoopa Prabhu 
31521da177e4SLinus Torvalds 	return err;
31531da177e4SLinus Torvalds }
31541da177e4SLinus Torvalds 
31558d1c802bSDavid Ahern static int __ip6_del_rt(struct fib6_info *rt, struct nl_info *info)
31561da177e4SLinus Torvalds {
3157afb1d4b5SDavid Ahern 	struct net *net = info->nl_net;
3158c71099acSThomas Graf 	struct fib6_table *table;
3159afb1d4b5SDavid Ahern 	int err;
31601da177e4SLinus Torvalds 
3161421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry) {
31626825a26cSGao feng 		err = -ENOENT;
31636825a26cSGao feng 		goto out;
31646825a26cSGao feng 	}
31656c813a72SPatrick McHardy 
316693c2fb25SDavid Ahern 	table = rt->fib6_table;
316766f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
316886872cb5SThomas Graf 	err = fib6_del(rt, info);
316966f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
31701da177e4SLinus Torvalds 
31716825a26cSGao feng out:
317293531c67SDavid Ahern 	fib6_info_release(rt);
31731da177e4SLinus Torvalds 	return err;
31741da177e4SLinus Torvalds }
31751da177e4SLinus Torvalds 
31768d1c802bSDavid Ahern int ip6_del_rt(struct net *net, struct fib6_info *rt)
3177e0a1ad73SThomas Graf {
3178afb1d4b5SDavid Ahern 	struct nl_info info = { .nl_net = net };
3179afb1d4b5SDavid Ahern 
3180528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
3181e0a1ad73SThomas Graf }
3182e0a1ad73SThomas Graf 
31838d1c802bSDavid Ahern static int __ip6_del_rt_siblings(struct fib6_info *rt, struct fib6_config *cfg)
31840ae81335SDavid Ahern {
31850ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
3186e3330039SWANG Cong 	struct net *net = info->nl_net;
318716a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
31880ae81335SDavid Ahern 	struct fib6_table *table;
3189e3330039SWANG Cong 	int err = -ENOENT;
31900ae81335SDavid Ahern 
3191421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
3192e3330039SWANG Cong 		goto out_put;
319393c2fb25SDavid Ahern 	table = rt->fib6_table;
319466f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
31950ae81335SDavid Ahern 
319693c2fb25SDavid Ahern 	if (rt->fib6_nsiblings && cfg->fc_delete_all_nh) {
31978d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
31980ae81335SDavid Ahern 
319916a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
320016a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
320116a16cd3SDavid Ahern 		if (skb) {
320216a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
320316a16cd3SDavid Ahern 
3204d4ead6b3SDavid Ahern 			if (rt6_fill_node(net, skb, rt, NULL,
320516a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
320616a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
320716a16cd3SDavid Ahern 				kfree_skb(skb);
320816a16cd3SDavid Ahern 				skb = NULL;
320916a16cd3SDavid Ahern 			} else
321016a16cd3SDavid Ahern 				info->skip_notify = 1;
321116a16cd3SDavid Ahern 		}
321216a16cd3SDavid Ahern 
32130ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
321493c2fb25SDavid Ahern 					 &rt->fib6_siblings,
321593c2fb25SDavid Ahern 					 fib6_siblings) {
32160ae81335SDavid Ahern 			err = fib6_del(sibling, info);
32170ae81335SDavid Ahern 			if (err)
3218e3330039SWANG Cong 				goto out_unlock;
32190ae81335SDavid Ahern 		}
32200ae81335SDavid Ahern 	}
32210ae81335SDavid Ahern 
32220ae81335SDavid Ahern 	err = fib6_del(rt, info);
3223e3330039SWANG Cong out_unlock:
322466f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
3225e3330039SWANG Cong out_put:
322693531c67SDavid Ahern 	fib6_info_release(rt);
322716a16cd3SDavid Ahern 
322816a16cd3SDavid Ahern 	if (skb) {
3229e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
323016a16cd3SDavid Ahern 			    info->nlh, gfp_any());
323116a16cd3SDavid Ahern 	}
32320ae81335SDavid Ahern 	return err;
32330ae81335SDavid Ahern }
32340ae81335SDavid Ahern 
323523fb93a4SDavid Ahern static int ip6_del_cached_rt(struct rt6_info *rt, struct fib6_config *cfg)
323623fb93a4SDavid Ahern {
323723fb93a4SDavid Ahern 	int rc = -ESRCH;
323823fb93a4SDavid Ahern 
323923fb93a4SDavid Ahern 	if (cfg->fc_ifindex && rt->dst.dev->ifindex != cfg->fc_ifindex)
324023fb93a4SDavid Ahern 		goto out;
324123fb93a4SDavid Ahern 
324223fb93a4SDavid Ahern 	if (cfg->fc_flags & RTF_GATEWAY &&
324323fb93a4SDavid Ahern 	    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
324423fb93a4SDavid Ahern 		goto out;
3245761f6026SXin Long 
324623fb93a4SDavid Ahern 	rc = rt6_remove_exception_rt(rt);
324723fb93a4SDavid Ahern out:
324823fb93a4SDavid Ahern 	return rc;
324923fb93a4SDavid Ahern }
325023fb93a4SDavid Ahern 
3251333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
3252333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
32531da177e4SLinus Torvalds {
32548d1c802bSDavid Ahern 	struct rt6_info *rt_cache;
3255c71099acSThomas Graf 	struct fib6_table *table;
32568d1c802bSDavid Ahern 	struct fib6_info *rt;
32571da177e4SLinus Torvalds 	struct fib6_node *fn;
32581da177e4SLinus Torvalds 	int err = -ESRCH;
32591da177e4SLinus Torvalds 
32605578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
3261d5d531cbSDavid Ahern 	if (!table) {
3262d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
3263c71099acSThomas Graf 		return err;
3264d5d531cbSDavid Ahern 	}
32651da177e4SLinus Torvalds 
326666f5d6ceSWei Wang 	rcu_read_lock();
3267c71099acSThomas Graf 
3268c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
326986872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
327038fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
32712b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
32721da177e4SLinus Torvalds 
32731da177e4SLinus Torvalds 	if (fn) {
327466f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
3275ad1601aeSDavid Ahern 			struct fib6_nh *nh;
3276ad1601aeSDavid Ahern 
32772b760fcfSWei Wang 			if (cfg->fc_flags & RTF_CACHE) {
327823fb93a4SDavid Ahern 				int rc;
327923fb93a4SDavid Ahern 
32802b760fcfSWei Wang 				rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
32812b760fcfSWei Wang 							      &cfg->fc_src);
328223fb93a4SDavid Ahern 				if (rt_cache) {
328323fb93a4SDavid Ahern 					rc = ip6_del_cached_rt(rt_cache, cfg);
32849e575010SEric Dumazet 					if (rc != -ESRCH) {
32859e575010SEric Dumazet 						rcu_read_unlock();
328623fb93a4SDavid Ahern 						return rc;
328723fb93a4SDavid Ahern 					}
32889e575010SEric Dumazet 				}
32891f56a01fSMartin KaFai Lau 				continue;
32902b760fcfSWei Wang 			}
3291ad1601aeSDavid Ahern 
3292ad1601aeSDavid Ahern 			nh = &rt->fib6_nh;
329386872cb5SThomas Graf 			if (cfg->fc_ifindex &&
3294ad1601aeSDavid Ahern 			    (!nh->fib_nh_dev ||
3295ad1601aeSDavid Ahern 			     nh->fib_nh_dev->ifindex != cfg->fc_ifindex))
32961da177e4SLinus Torvalds 				continue;
329786872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
3298ad1601aeSDavid Ahern 			    !ipv6_addr_equal(&cfg->fc_gateway, &nh->fib_nh_gw6))
32991da177e4SLinus Torvalds 				continue;
330093c2fb25SDavid Ahern 			if (cfg->fc_metric && cfg->fc_metric != rt->fib6_metric)
33011da177e4SLinus Torvalds 				continue;
330293c2fb25SDavid Ahern 			if (cfg->fc_protocol && cfg->fc_protocol != rt->fib6_protocol)
3303c2ed1880SMantas M 				continue;
3304e873e4b9SWei Wang 			if (!fib6_info_hold_safe(rt))
3305e873e4b9SWei Wang 				continue;
330666f5d6ceSWei Wang 			rcu_read_unlock();
33071da177e4SLinus Torvalds 
33080ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
33090ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
331086872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
33110ae81335SDavid Ahern 
33120ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
33131da177e4SLinus Torvalds 		}
33141da177e4SLinus Torvalds 	}
331566f5d6ceSWei Wang 	rcu_read_unlock();
33161da177e4SLinus Torvalds 
33171da177e4SLinus Torvalds 	return err;
33181da177e4SLinus Torvalds }
33191da177e4SLinus Torvalds 
33206700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
3321a6279458SYOSHIFUJI Hideaki {
3322a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
3323e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
3324e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
3325e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
3326e8599ff4SDavid S. Miller 	struct neighbour *neigh;
3327a68886a6SDavid Ahern 	struct fib6_info *from;
332871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
33296e157b6aSDavid S. Miller 	int optlen, on_link;
33306e157b6aSDavid S. Miller 	u8 *lladdr;
3331e8599ff4SDavid S. Miller 
333229a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
333371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
3334e8599ff4SDavid S. Miller 
3335e8599ff4SDavid S. Miller 	if (optlen < 0) {
33366e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
3337e8599ff4SDavid S. Miller 		return;
3338e8599ff4SDavid S. Miller 	}
3339e8599ff4SDavid S. Miller 
334071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
3341e8599ff4SDavid S. Miller 
334271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
33436e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
3344e8599ff4SDavid S. Miller 		return;
3345e8599ff4SDavid S. Miller 	}
3346e8599ff4SDavid S. Miller 
33476e157b6aSDavid S. Miller 	on_link = 0;
334871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
3349e8599ff4SDavid S. Miller 		on_link = 1;
335071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
3351e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
33526e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
3353e8599ff4SDavid S. Miller 		return;
3354e8599ff4SDavid S. Miller 	}
3355e8599ff4SDavid S. Miller 
3356e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
3357e8599ff4SDavid S. Miller 	if (!in6_dev)
3358e8599ff4SDavid S. Miller 		return;
3359e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
3360e8599ff4SDavid S. Miller 		return;
3361e8599ff4SDavid S. Miller 
3362e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
3363e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
3364e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
3365e8599ff4SDavid S. Miller 	 */
3366e8599ff4SDavid S. Miller 
3367f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
3368e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
3369e8599ff4SDavid S. Miller 		return;
3370e8599ff4SDavid S. Miller 	}
33716e157b6aSDavid S. Miller 
33726e157b6aSDavid S. Miller 	lladdr = NULL;
3373e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
3374e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3375e8599ff4SDavid S. Miller 					     skb->dev);
3376e8599ff4SDavid S. Miller 		if (!lladdr) {
3377e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3378e8599ff4SDavid S. Miller 			return;
3379e8599ff4SDavid S. Miller 		}
3380e8599ff4SDavid S. Miller 	}
3381e8599ff4SDavid S. Miller 
33826e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
3383ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
33846e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
33856e157b6aSDavid S. Miller 		return;
33866e157b6aSDavid S. Miller 	}
33876e157b6aSDavid S. Miller 
33886e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
33896e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
33906e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
33916e157b6aSDavid S. Miller 	 */
33920dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
33936e157b6aSDavid S. Miller 
339471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
3395e8599ff4SDavid S. Miller 	if (!neigh)
3396e8599ff4SDavid S. Miller 		return;
3397e8599ff4SDavid S. Miller 
33981da177e4SLinus Torvalds 	/*
33991da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
34001da177e4SLinus Torvalds 	 */
34011da177e4SLinus Torvalds 
3402f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
34031da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
34041da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
34051da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
3406f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
3407f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
34081da177e4SLinus Torvalds 
34094d85cd0cSDavid Ahern 	rcu_read_lock();
3410a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
3411e873e4b9SWei Wang 	/* This fib6_info_hold() is safe here because we hold reference to rt
3412e873e4b9SWei Wang 	 * and rt already holds reference to fib6_info.
3413e873e4b9SWei Wang 	 */
34148a14e46fSDavid Ahern 	fib6_info_hold(from);
34154d85cd0cSDavid Ahern 	rcu_read_unlock();
34168a14e46fSDavid Ahern 
34178a14e46fSDavid Ahern 	nrt = ip6_rt_cache_alloc(from, &msg->dest, NULL);
341838308473SDavid S. Miller 	if (!nrt)
34191da177e4SLinus Torvalds 		goto out;
34201da177e4SLinus Torvalds 
34211da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
34221da177e4SLinus Torvalds 	if (on_link)
34231da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
34241da177e4SLinus Torvalds 
34254e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
34261da177e4SLinus Torvalds 
34272b760fcfSWei Wang 	/* No need to remove rt from the exception table if rt is
34282b760fcfSWei Wang 	 * a cached route because rt6_insert_exception() will
34292b760fcfSWei Wang 	 * takes care of it
34302b760fcfSWei Wang 	 */
34318a14e46fSDavid Ahern 	if (rt6_insert_exception(nrt, from)) {
34322b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
34332b760fcfSWei Wang 		goto out;
34342b760fcfSWei Wang 	}
34351da177e4SLinus Torvalds 
3436d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
3437d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
343871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
343960592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
34408d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
34418d71740cSTom Tucker 
34421da177e4SLinus Torvalds out:
34438a14e46fSDavid Ahern 	fib6_info_release(from);
3444e8599ff4SDavid S. Miller 	neigh_release(neigh);
34456e157b6aSDavid S. Miller }
34466e157b6aSDavid S. Miller 
344770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
34488d1c802bSDavid Ahern static struct fib6_info *rt6_get_route_info(struct net *net,
3449b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3450830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3451830218c1SDavid Ahern 					   struct net_device *dev)
345270ceb4f5SYOSHIFUJI Hideaki {
3453830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3454830218c1SDavid Ahern 	int ifindex = dev->ifindex;
345570ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
34568d1c802bSDavid Ahern 	struct fib6_info *rt = NULL;
3457c71099acSThomas Graf 	struct fib6_table *table;
345870ceb4f5SYOSHIFUJI Hideaki 
3459830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
346038308473SDavid S. Miller 	if (!table)
3461c71099acSThomas Graf 		return NULL;
3462c71099acSThomas Graf 
346366f5d6ceSWei Wang 	rcu_read_lock();
346438fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
346570ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
346670ceb4f5SYOSHIFUJI Hideaki 		goto out;
346770ceb4f5SYOSHIFUJI Hideaki 
346866f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
3469ad1601aeSDavid Ahern 		if (rt->fib6_nh.fib_nh_dev->ifindex != ifindex)
347070ceb4f5SYOSHIFUJI Hideaki 			continue;
34712b2450caSDavid Ahern 		if (!(rt->fib6_flags & RTF_ROUTEINFO) ||
3472bdf00467SDavid Ahern 		    !rt->fib6_nh.fib_nh_gw_family)
347370ceb4f5SYOSHIFUJI Hideaki 			continue;
3474ad1601aeSDavid Ahern 		if (!ipv6_addr_equal(&rt->fib6_nh.fib_nh_gw6, gwaddr))
347570ceb4f5SYOSHIFUJI Hideaki 			continue;
3476e873e4b9SWei Wang 		if (!fib6_info_hold_safe(rt))
3477e873e4b9SWei Wang 			continue;
347870ceb4f5SYOSHIFUJI Hideaki 		break;
347970ceb4f5SYOSHIFUJI Hideaki 	}
348070ceb4f5SYOSHIFUJI Hideaki out:
348166f5d6ceSWei Wang 	rcu_read_unlock();
348270ceb4f5SYOSHIFUJI Hideaki 	return rt;
348370ceb4f5SYOSHIFUJI Hideaki }
348470ceb4f5SYOSHIFUJI Hideaki 
34858d1c802bSDavid Ahern static struct fib6_info *rt6_add_route_info(struct net *net,
3486b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3487830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3488830218c1SDavid Ahern 					   struct net_device *dev,
348995c96174SEric Dumazet 					   unsigned int pref)
349070ceb4f5SYOSHIFUJI Hideaki {
349186872cb5SThomas Graf 	struct fib6_config cfg = {
3492238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
3493830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
349486872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
349586872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
349686872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
3497b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3498e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
349915e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
3500efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3501efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
350286872cb5SThomas Graf 	};
350370ceb4f5SYOSHIFUJI Hideaki 
3504830218c1SDavid Ahern 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
35054e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
35064e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
350786872cb5SThomas Graf 
3508e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
3509e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
351086872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
351170ceb4f5SYOSHIFUJI Hideaki 
3512acb54e3cSDavid Ahern 	ip6_route_add(&cfg, GFP_ATOMIC, NULL);
351370ceb4f5SYOSHIFUJI Hideaki 
3514830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
351570ceb4f5SYOSHIFUJI Hideaki }
351670ceb4f5SYOSHIFUJI Hideaki #endif
351770ceb4f5SYOSHIFUJI Hideaki 
35188d1c802bSDavid Ahern struct fib6_info *rt6_get_dflt_router(struct net *net,
3519afb1d4b5SDavid Ahern 				     const struct in6_addr *addr,
3520afb1d4b5SDavid Ahern 				     struct net_device *dev)
35211da177e4SLinus Torvalds {
3522830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
35238d1c802bSDavid Ahern 	struct fib6_info *rt;
3524c71099acSThomas Graf 	struct fib6_table *table;
35251da177e4SLinus Torvalds 
3526afb1d4b5SDavid Ahern 	table = fib6_get_table(net, tb_id);
352738308473SDavid S. Miller 	if (!table)
3528c71099acSThomas Graf 		return NULL;
35291da177e4SLinus Torvalds 
353066f5d6ceSWei Wang 	rcu_read_lock();
353166f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
3532ad1601aeSDavid Ahern 		struct fib6_nh *nh = &rt->fib6_nh;
3533ad1601aeSDavid Ahern 
3534ad1601aeSDavid Ahern 		if (dev == nh->fib_nh_dev &&
353593c2fb25SDavid Ahern 		    ((rt->fib6_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
3536ad1601aeSDavid Ahern 		    ipv6_addr_equal(&nh->fib_nh_gw6, addr))
35371da177e4SLinus Torvalds 			break;
35381da177e4SLinus Torvalds 	}
3539e873e4b9SWei Wang 	if (rt && !fib6_info_hold_safe(rt))
3540e873e4b9SWei Wang 		rt = NULL;
354166f5d6ceSWei Wang 	rcu_read_unlock();
35421da177e4SLinus Torvalds 	return rt;
35431da177e4SLinus Torvalds }
35441da177e4SLinus Torvalds 
35458d1c802bSDavid Ahern struct fib6_info *rt6_add_dflt_router(struct net *net,
3546afb1d4b5SDavid Ahern 				     const struct in6_addr *gwaddr,
3547ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
3548ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
35491da177e4SLinus Torvalds {
355086872cb5SThomas Graf 	struct fib6_config cfg = {
3551ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
3552238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
355386872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
355486872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
355586872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
3556b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
3557e8478e80SDavid Ahern 		.fc_type = RTN_UNICAST,
355815e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
35595578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3560afb1d4b5SDavid Ahern 		.fc_nlinfo.nl_net = net,
356186872cb5SThomas Graf 	};
35621da177e4SLinus Torvalds 
35634e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
35641da177e4SLinus Torvalds 
3565acb54e3cSDavid Ahern 	if (!ip6_route_add(&cfg, GFP_ATOMIC, NULL)) {
3566830218c1SDavid Ahern 		struct fib6_table *table;
3567830218c1SDavid Ahern 
3568830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
3569830218c1SDavid Ahern 		if (table)
3570830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3571830218c1SDavid Ahern 	}
35721da177e4SLinus Torvalds 
3573afb1d4b5SDavid Ahern 	return rt6_get_dflt_router(net, gwaddr, dev);
35741da177e4SLinus Torvalds }
35751da177e4SLinus Torvalds 
3576afb1d4b5SDavid Ahern static void __rt6_purge_dflt_routers(struct net *net,
3577afb1d4b5SDavid Ahern 				     struct fib6_table *table)
35781da177e4SLinus Torvalds {
35798d1c802bSDavid Ahern 	struct fib6_info *rt;
35801da177e4SLinus Torvalds 
35811da177e4SLinus Torvalds restart:
358266f5d6ceSWei Wang 	rcu_read_lock();
358366f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
3584dcd1f572SDavid Ahern 		struct net_device *dev = fib6_info_nh_dev(rt);
3585dcd1f572SDavid Ahern 		struct inet6_dev *idev = dev ? __in6_dev_get(dev) : NULL;
3586dcd1f572SDavid Ahern 
358793c2fb25SDavid Ahern 		if (rt->fib6_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
3588e873e4b9SWei Wang 		    (!idev || idev->cnf.accept_ra != 2) &&
3589e873e4b9SWei Wang 		    fib6_info_hold_safe(rt)) {
359066f5d6ceSWei Wang 			rcu_read_unlock();
3591afb1d4b5SDavid Ahern 			ip6_del_rt(net, rt);
35921da177e4SLinus Torvalds 			goto restart;
35931da177e4SLinus Torvalds 		}
35941da177e4SLinus Torvalds 	}
359566f5d6ceSWei Wang 	rcu_read_unlock();
3596830218c1SDavid Ahern 
3597830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3598830218c1SDavid Ahern }
3599830218c1SDavid Ahern 
3600830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
3601830218c1SDavid Ahern {
3602830218c1SDavid Ahern 	struct fib6_table *table;
3603830218c1SDavid Ahern 	struct hlist_head *head;
3604830218c1SDavid Ahern 	unsigned int h;
3605830218c1SDavid Ahern 
3606830218c1SDavid Ahern 	rcu_read_lock();
3607830218c1SDavid Ahern 
3608830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3609830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
3610830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3611830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3612afb1d4b5SDavid Ahern 				__rt6_purge_dflt_routers(net, table);
3613830218c1SDavid Ahern 		}
3614830218c1SDavid Ahern 	}
3615830218c1SDavid Ahern 
3616830218c1SDavid Ahern 	rcu_read_unlock();
36171da177e4SLinus Torvalds }
36181da177e4SLinus Torvalds 
36195578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
36205578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
362186872cb5SThomas Graf 				 struct fib6_config *cfg)
362286872cb5SThomas Graf {
36238823a3acSMaciej Żenczykowski 	*cfg = (struct fib6_config){
36248823a3acSMaciej Żenczykowski 		.fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
36258823a3acSMaciej Żenczykowski 			 : RT6_TABLE_MAIN,
36268823a3acSMaciej Żenczykowski 		.fc_ifindex = rtmsg->rtmsg_ifindex,
362767f69513SDavid Ahern 		.fc_metric = rtmsg->rtmsg_metric ? : IP6_RT_PRIO_USER,
36288823a3acSMaciej Żenczykowski 		.fc_expires = rtmsg->rtmsg_info,
36298823a3acSMaciej Żenczykowski 		.fc_dst_len = rtmsg->rtmsg_dst_len,
36308823a3acSMaciej Żenczykowski 		.fc_src_len = rtmsg->rtmsg_src_len,
36318823a3acSMaciej Żenczykowski 		.fc_flags = rtmsg->rtmsg_flags,
36328823a3acSMaciej Żenczykowski 		.fc_type = rtmsg->rtmsg_type,
363386872cb5SThomas Graf 
36348823a3acSMaciej Żenczykowski 		.fc_nlinfo.nl_net = net,
363586872cb5SThomas Graf 
36368823a3acSMaciej Żenczykowski 		.fc_dst = rtmsg->rtmsg_dst,
36378823a3acSMaciej Żenczykowski 		.fc_src = rtmsg->rtmsg_src,
36388823a3acSMaciej Żenczykowski 		.fc_gateway = rtmsg->rtmsg_gateway,
36398823a3acSMaciej Żenczykowski 	};
364086872cb5SThomas Graf }
364186872cb5SThomas Graf 
36425578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
36431da177e4SLinus Torvalds {
364486872cb5SThomas Graf 	struct fib6_config cfg;
36451da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
36461da177e4SLinus Torvalds 	int err;
36471da177e4SLinus Torvalds 
36481da177e4SLinus Torvalds 	switch (cmd) {
36491da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
36501da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
3651af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
36521da177e4SLinus Torvalds 			return -EPERM;
36531da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
36541da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
36551da177e4SLinus Torvalds 		if (err)
36561da177e4SLinus Torvalds 			return -EFAULT;
36571da177e4SLinus Torvalds 
36585578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
365986872cb5SThomas Graf 
36601da177e4SLinus Torvalds 		rtnl_lock();
36611da177e4SLinus Torvalds 		switch (cmd) {
36621da177e4SLinus Torvalds 		case SIOCADDRT:
3663acb54e3cSDavid Ahern 			err = ip6_route_add(&cfg, GFP_KERNEL, NULL);
36641da177e4SLinus Torvalds 			break;
36651da177e4SLinus Torvalds 		case SIOCDELRT:
3666333c4301SDavid Ahern 			err = ip6_route_del(&cfg, NULL);
36671da177e4SLinus Torvalds 			break;
36681da177e4SLinus Torvalds 		default:
36691da177e4SLinus Torvalds 			err = -EINVAL;
36701da177e4SLinus Torvalds 		}
36711da177e4SLinus Torvalds 		rtnl_unlock();
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds 		return err;
36743ff50b79SStephen Hemminger 	}
36751da177e4SLinus Torvalds 
36761da177e4SLinus Torvalds 	return -EINVAL;
36771da177e4SLinus Torvalds }
36781da177e4SLinus Torvalds 
36791da177e4SLinus Torvalds /*
36801da177e4SLinus Torvalds  *	Drop the packet on the floor
36811da177e4SLinus Torvalds  */
36821da177e4SLinus Torvalds 
3683d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
36841da177e4SLinus Torvalds {
3685612f09e8SYOSHIFUJI Hideaki 	int type;
3686adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
3687612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
3688612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
36890660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
369045bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
3691bdb7cc64SStephen Suryaputra 			IP6_INC_STATS(dev_net(dst->dev),
3692bdb7cc64SStephen Suryaputra 				      __in6_dev_get_safely(skb->dev),
36933bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
3694612f09e8SYOSHIFUJI Hideaki 			break;
3695612f09e8SYOSHIFUJI Hideaki 		}
3696612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
3697612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
36983bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
36993bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
3700612f09e8SYOSHIFUJI Hideaki 		break;
3701612f09e8SYOSHIFUJI Hideaki 	}
37023ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
37031da177e4SLinus Torvalds 	kfree_skb(skb);
37041da177e4SLinus Torvalds 	return 0;
37051da177e4SLinus Torvalds }
37061da177e4SLinus Torvalds 
37079ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
37089ce8ade0SThomas Graf {
3709612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
37109ce8ade0SThomas Graf }
37119ce8ade0SThomas Graf 
3712ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
37131da177e4SLinus Torvalds {
3714adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3715612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
37161da177e4SLinus Torvalds }
37171da177e4SLinus Torvalds 
37189ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
37199ce8ade0SThomas Graf {
3720612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
37219ce8ade0SThomas Graf }
37229ce8ade0SThomas Graf 
3723ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
37249ce8ade0SThomas Graf {
3725adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3726612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
37279ce8ade0SThomas Graf }
37289ce8ade0SThomas Graf 
37291da177e4SLinus Torvalds /*
37301da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
37311da177e4SLinus Torvalds  */
37321da177e4SLinus Torvalds 
3733360a9887SDavid Ahern struct fib6_info *addrconf_f6i_alloc(struct net *net,
3734afb1d4b5SDavid Ahern 				     struct inet6_dev *idev,
37351da177e4SLinus Torvalds 				     const struct in6_addr *addr,
3736acb54e3cSDavid Ahern 				     bool anycast, gfp_t gfp_flags)
37371da177e4SLinus Torvalds {
3738c7a1ce39SDavid Ahern 	struct fib6_config cfg = {
3739c7a1ce39SDavid Ahern 		.fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
3740c7a1ce39SDavid Ahern 		.fc_ifindex = idev->dev->ifindex,
3741c7a1ce39SDavid Ahern 		.fc_flags = RTF_UP | RTF_ADDRCONF | RTF_NONEXTHOP,
3742c7a1ce39SDavid Ahern 		.fc_dst = *addr,
3743c7a1ce39SDavid Ahern 		.fc_dst_len = 128,
3744c7a1ce39SDavid Ahern 		.fc_protocol = RTPROT_KERNEL,
3745c7a1ce39SDavid Ahern 		.fc_nlinfo.nl_net = net,
3746c7a1ce39SDavid Ahern 		.fc_ignore_dev_down = true,
3747c7a1ce39SDavid Ahern 	};
37485f02ce24SDavid Ahern 
3749e8478e80SDavid Ahern 	if (anycast) {
3750c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_ANYCAST;
3751c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_ANYCAST;
3752e8478e80SDavid Ahern 	} else {
3753c7a1ce39SDavid Ahern 		cfg.fc_type = RTN_LOCAL;
3754c7a1ce39SDavid Ahern 		cfg.fc_flags |= RTF_LOCAL;
3755e8478e80SDavid Ahern 	}
37561da177e4SLinus Torvalds 
3757c7a1ce39SDavid Ahern 	return ip6_route_info_create(&cfg, gfp_flags, NULL);
37581da177e4SLinus Torvalds }
37591da177e4SLinus Torvalds 
3760c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
3761c3968a85SDaniel Walter struct arg_dev_net_ip {
3762c3968a85SDaniel Walter 	struct net_device *dev;
3763c3968a85SDaniel Walter 	struct net *net;
3764c3968a85SDaniel Walter 	struct in6_addr *addr;
3765c3968a85SDaniel Walter };
3766c3968a85SDaniel Walter 
37678d1c802bSDavid Ahern static int fib6_remove_prefsrc(struct fib6_info *rt, void *arg)
3768c3968a85SDaniel Walter {
3769c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3770c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3771c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3772c3968a85SDaniel Walter 
3773ad1601aeSDavid Ahern 	if (((void *)rt->fib6_nh.fib_nh_dev == dev || !dev) &&
3774421842edSDavid Ahern 	    rt != net->ipv6.fib6_null_entry &&
377593c2fb25SDavid Ahern 	    ipv6_addr_equal(addr, &rt->fib6_prefsrc.addr)) {
377660006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
3777c3968a85SDaniel Walter 		/* remove prefsrc entry */
377893c2fb25SDavid Ahern 		rt->fib6_prefsrc.plen = 0;
377960006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
3780c3968a85SDaniel Walter 	}
3781c3968a85SDaniel Walter 	return 0;
3782c3968a85SDaniel Walter }
3783c3968a85SDaniel Walter 
3784c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3785c3968a85SDaniel Walter {
3786c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
3787c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
3788c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
3789c3968a85SDaniel Walter 		.net = net,
3790c3968a85SDaniel Walter 		.addr = &ifp->addr,
3791c3968a85SDaniel Walter 	};
37920c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
3793c3968a85SDaniel Walter }
3794c3968a85SDaniel Walter 
37952b2450caSDavid Ahern #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT)
3796be7a010dSDuan Jiong 
3797be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
37988d1c802bSDavid Ahern static int fib6_clean_tohost(struct fib6_info *rt, void *arg)
3799be7a010dSDuan Jiong {
3800be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
3801be7a010dSDuan Jiong 
380293c2fb25SDavid Ahern 	if (((rt->fib6_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
3803bdf00467SDavid Ahern 	    rt->fib6_nh.fib_nh_gw_family &&
3804ad1601aeSDavid Ahern 	    ipv6_addr_equal(gateway, &rt->fib6_nh.fib_nh_gw6)) {
3805be7a010dSDuan Jiong 		return -1;
3806be7a010dSDuan Jiong 	}
3807b16cb459SWei Wang 
3808b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
3809b16cb459SWei Wang 	 * This is needed because cached route may have a different
3810b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
3811b16cb459SWei Wang 	 */
3812b16cb459SWei Wang 	rt6_exceptions_clean_tohost(rt, gateway);
3813b16cb459SWei Wang 
3814be7a010dSDuan Jiong 	return 0;
3815be7a010dSDuan Jiong }
3816be7a010dSDuan Jiong 
3817be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3818be7a010dSDuan Jiong {
3819be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
3820be7a010dSDuan Jiong }
3821be7a010dSDuan Jiong 
38222127d95aSIdo Schimmel struct arg_netdev_event {
38232127d95aSIdo Schimmel 	const struct net_device *dev;
38244c981e28SIdo Schimmel 	union {
38252127d95aSIdo Schimmel 		unsigned int nh_flags;
38264c981e28SIdo Schimmel 		unsigned long event;
38274c981e28SIdo Schimmel 	};
38282127d95aSIdo Schimmel };
38292127d95aSIdo Schimmel 
38308d1c802bSDavid Ahern static struct fib6_info *rt6_multipath_first_sibling(const struct fib6_info *rt)
3831d7dedee1SIdo Schimmel {
38328d1c802bSDavid Ahern 	struct fib6_info *iter;
3833d7dedee1SIdo Schimmel 	struct fib6_node *fn;
3834d7dedee1SIdo Schimmel 
383593c2fb25SDavid Ahern 	fn = rcu_dereference_protected(rt->fib6_node,
383693c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3837d7dedee1SIdo Schimmel 	iter = rcu_dereference_protected(fn->leaf,
383893c2fb25SDavid Ahern 			lockdep_is_held(&rt->fib6_table->tb6_lock));
3839d7dedee1SIdo Schimmel 	while (iter) {
384093c2fb25SDavid Ahern 		if (iter->fib6_metric == rt->fib6_metric &&
384133bd5ac5SDavid Ahern 		    rt6_qualify_for_ecmp(iter))
3842d7dedee1SIdo Schimmel 			return iter;
38438fb11a9aSDavid Ahern 		iter = rcu_dereference_protected(iter->fib6_next,
384493c2fb25SDavid Ahern 				lockdep_is_held(&rt->fib6_table->tb6_lock));
3845d7dedee1SIdo Schimmel 	}
3846d7dedee1SIdo Schimmel 
3847d7dedee1SIdo Schimmel 	return NULL;
3848d7dedee1SIdo Schimmel }
3849d7dedee1SIdo Schimmel 
38508d1c802bSDavid Ahern static bool rt6_is_dead(const struct fib6_info *rt)
3851d7dedee1SIdo Schimmel {
3852ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD ||
3853ad1601aeSDavid Ahern 	    (rt->fib6_nh.fib_nh_flags & RTNH_F_LINKDOWN &&
3854ad1601aeSDavid Ahern 	     ip6_ignore_linkdown(rt->fib6_nh.fib_nh_dev)))
3855d7dedee1SIdo Schimmel 		return true;
3856d7dedee1SIdo Schimmel 
3857d7dedee1SIdo Schimmel 	return false;
3858d7dedee1SIdo Schimmel }
3859d7dedee1SIdo Schimmel 
38608d1c802bSDavid Ahern static int rt6_multipath_total_weight(const struct fib6_info *rt)
3861d7dedee1SIdo Schimmel {
38628d1c802bSDavid Ahern 	struct fib6_info *iter;
3863d7dedee1SIdo Schimmel 	int total = 0;
3864d7dedee1SIdo Schimmel 
3865d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt))
3866ad1601aeSDavid Ahern 		total += rt->fib6_nh.fib_nh_weight;
3867d7dedee1SIdo Schimmel 
386893c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings) {
3869d7dedee1SIdo Schimmel 		if (!rt6_is_dead(iter))
3870ad1601aeSDavid Ahern 			total += iter->fib6_nh.fib_nh_weight;
3871d7dedee1SIdo Schimmel 	}
3872d7dedee1SIdo Schimmel 
3873d7dedee1SIdo Schimmel 	return total;
3874d7dedee1SIdo Schimmel }
3875d7dedee1SIdo Schimmel 
38768d1c802bSDavid Ahern static void rt6_upper_bound_set(struct fib6_info *rt, int *weight, int total)
3877d7dedee1SIdo Schimmel {
3878d7dedee1SIdo Schimmel 	int upper_bound = -1;
3879d7dedee1SIdo Schimmel 
3880d7dedee1SIdo Schimmel 	if (!rt6_is_dead(rt)) {
3881ad1601aeSDavid Ahern 		*weight += rt->fib6_nh.fib_nh_weight;
3882d7dedee1SIdo Schimmel 		upper_bound = DIV_ROUND_CLOSEST_ULL((u64) (*weight) << 31,
3883d7dedee1SIdo Schimmel 						    total) - 1;
3884d7dedee1SIdo Schimmel 	}
3885ad1601aeSDavid Ahern 	atomic_set(&rt->fib6_nh.fib_nh_upper_bound, upper_bound);
3886d7dedee1SIdo Schimmel }
3887d7dedee1SIdo Schimmel 
38888d1c802bSDavid Ahern static void rt6_multipath_upper_bound_set(struct fib6_info *rt, int total)
3889d7dedee1SIdo Schimmel {
38908d1c802bSDavid Ahern 	struct fib6_info *iter;
3891d7dedee1SIdo Schimmel 	int weight = 0;
3892d7dedee1SIdo Schimmel 
3893d7dedee1SIdo Schimmel 	rt6_upper_bound_set(rt, &weight, total);
3894d7dedee1SIdo Schimmel 
389593c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
3896d7dedee1SIdo Schimmel 		rt6_upper_bound_set(iter, &weight, total);
3897d7dedee1SIdo Schimmel }
3898d7dedee1SIdo Schimmel 
38998d1c802bSDavid Ahern void rt6_multipath_rebalance(struct fib6_info *rt)
3900d7dedee1SIdo Schimmel {
39018d1c802bSDavid Ahern 	struct fib6_info *first;
3902d7dedee1SIdo Schimmel 	int total;
3903d7dedee1SIdo Schimmel 
3904d7dedee1SIdo Schimmel 	/* In case the entire multipath route was marked for flushing,
3905d7dedee1SIdo Schimmel 	 * then there is no need to rebalance upon the removal of every
3906d7dedee1SIdo Schimmel 	 * sibling route.
3907d7dedee1SIdo Schimmel 	 */
390893c2fb25SDavid Ahern 	if (!rt->fib6_nsiblings || rt->should_flush)
3909d7dedee1SIdo Schimmel 		return;
3910d7dedee1SIdo Schimmel 
3911d7dedee1SIdo Schimmel 	/* During lookup routes are evaluated in order, so we need to
3912d7dedee1SIdo Schimmel 	 * make sure upper bounds are assigned from the first sibling
3913d7dedee1SIdo Schimmel 	 * onwards.
3914d7dedee1SIdo Schimmel 	 */
3915d7dedee1SIdo Schimmel 	first = rt6_multipath_first_sibling(rt);
3916d7dedee1SIdo Schimmel 	if (WARN_ON_ONCE(!first))
3917d7dedee1SIdo Schimmel 		return;
3918d7dedee1SIdo Schimmel 
3919d7dedee1SIdo Schimmel 	total = rt6_multipath_total_weight(first);
3920d7dedee1SIdo Schimmel 	rt6_multipath_upper_bound_set(first, total);
3921d7dedee1SIdo Schimmel }
3922d7dedee1SIdo Schimmel 
39238d1c802bSDavid Ahern static int fib6_ifup(struct fib6_info *rt, void *p_arg)
39242127d95aSIdo Schimmel {
39252127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
39267aef6859SDavid Ahern 	struct net *net = dev_net(arg->dev);
39272127d95aSIdo Schimmel 
3928ad1601aeSDavid Ahern 	if (rt != net->ipv6.fib6_null_entry &&
3929ad1601aeSDavid Ahern 	    rt->fib6_nh.fib_nh_dev == arg->dev) {
3930ad1601aeSDavid Ahern 		rt->fib6_nh.fib_nh_flags &= ~arg->nh_flags;
39317aef6859SDavid Ahern 		fib6_update_sernum_upto_root(net, rt);
3932d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
39331de178edSIdo Schimmel 	}
39342127d95aSIdo Schimmel 
39352127d95aSIdo Schimmel 	return 0;
39362127d95aSIdo Schimmel }
39372127d95aSIdo Schimmel 
39382127d95aSIdo Schimmel void rt6_sync_up(struct net_device *dev, unsigned int nh_flags)
39392127d95aSIdo Schimmel {
39402127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
39412127d95aSIdo Schimmel 		.dev = dev,
39426802f3adSIdo Schimmel 		{
39432127d95aSIdo Schimmel 			.nh_flags = nh_flags,
39446802f3adSIdo Schimmel 		},
39452127d95aSIdo Schimmel 	};
39462127d95aSIdo Schimmel 
39472127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
39482127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
39492127d95aSIdo Schimmel 
39502127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
39512127d95aSIdo Schimmel }
39522127d95aSIdo Schimmel 
39538d1c802bSDavid Ahern static bool rt6_multipath_uses_dev(const struct fib6_info *rt,
39541de178edSIdo Schimmel 				   const struct net_device *dev)
39551de178edSIdo Schimmel {
39568d1c802bSDavid Ahern 	struct fib6_info *iter;
39571de178edSIdo Schimmel 
3958ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_dev == dev)
39591de178edSIdo Schimmel 		return true;
396093c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
3961ad1601aeSDavid Ahern 		if (iter->fib6_nh.fib_nh_dev == dev)
39621de178edSIdo Schimmel 			return true;
39631de178edSIdo Schimmel 
39641de178edSIdo Schimmel 	return false;
39651de178edSIdo Schimmel }
39661de178edSIdo Schimmel 
39678d1c802bSDavid Ahern static void rt6_multipath_flush(struct fib6_info *rt)
39681de178edSIdo Schimmel {
39698d1c802bSDavid Ahern 	struct fib6_info *iter;
39701de178edSIdo Schimmel 
39711de178edSIdo Schimmel 	rt->should_flush = 1;
397293c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
39731de178edSIdo Schimmel 		iter->should_flush = 1;
39741de178edSIdo Schimmel }
39751de178edSIdo Schimmel 
39768d1c802bSDavid Ahern static unsigned int rt6_multipath_dead_count(const struct fib6_info *rt,
39771de178edSIdo Schimmel 					     const struct net_device *down_dev)
39781de178edSIdo Schimmel {
39798d1c802bSDavid Ahern 	struct fib6_info *iter;
39801de178edSIdo Schimmel 	unsigned int dead = 0;
39811de178edSIdo Schimmel 
3982ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_dev == down_dev ||
3983ad1601aeSDavid Ahern 	    rt->fib6_nh.fib_nh_flags & RTNH_F_DEAD)
39841de178edSIdo Schimmel 		dead++;
398593c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
3986ad1601aeSDavid Ahern 		if (iter->fib6_nh.fib_nh_dev == down_dev ||
3987ad1601aeSDavid Ahern 		    iter->fib6_nh.fib_nh_flags & RTNH_F_DEAD)
39881de178edSIdo Schimmel 			dead++;
39891de178edSIdo Schimmel 
39901de178edSIdo Schimmel 	return dead;
39911de178edSIdo Schimmel }
39921de178edSIdo Schimmel 
39938d1c802bSDavid Ahern static void rt6_multipath_nh_flags_set(struct fib6_info *rt,
39941de178edSIdo Schimmel 				       const struct net_device *dev,
39951de178edSIdo Schimmel 				       unsigned int nh_flags)
39961de178edSIdo Schimmel {
39978d1c802bSDavid Ahern 	struct fib6_info *iter;
39981de178edSIdo Schimmel 
3999ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_dev == dev)
4000ad1601aeSDavid Ahern 		rt->fib6_nh.fib_nh_flags |= nh_flags;
400193c2fb25SDavid Ahern 	list_for_each_entry(iter, &rt->fib6_siblings, fib6_siblings)
4002ad1601aeSDavid Ahern 		if (iter->fib6_nh.fib_nh_dev == dev)
4003ad1601aeSDavid Ahern 			iter->fib6_nh.fib_nh_flags |= nh_flags;
40041de178edSIdo Schimmel }
40051de178edSIdo Schimmel 
4006a1a22c12SDavid Ahern /* called with write lock held for table with rt */
40078d1c802bSDavid Ahern static int fib6_ifdown(struct fib6_info *rt, void *p_arg)
40081da177e4SLinus Torvalds {
40094c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
40104c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
40117aef6859SDavid Ahern 	struct net *net = dev_net(dev);
40128ed67789SDaniel Lezcano 
4013421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
401427c6fa73SIdo Schimmel 		return 0;
401527c6fa73SIdo Schimmel 
401627c6fa73SIdo Schimmel 	switch (arg->event) {
401727c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
4018ad1601aeSDavid Ahern 		return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0;
401927c6fa73SIdo Schimmel 	case NETDEV_DOWN:
40201de178edSIdo Schimmel 		if (rt->should_flush)
402127c6fa73SIdo Schimmel 			return -1;
402293c2fb25SDavid Ahern 		if (!rt->fib6_nsiblings)
4023ad1601aeSDavid Ahern 			return rt->fib6_nh.fib_nh_dev == dev ? -1 : 0;
40241de178edSIdo Schimmel 		if (rt6_multipath_uses_dev(rt, dev)) {
40251de178edSIdo Schimmel 			unsigned int count;
40261de178edSIdo Schimmel 
40271de178edSIdo Schimmel 			count = rt6_multipath_dead_count(rt, dev);
402893c2fb25SDavid Ahern 			if (rt->fib6_nsiblings + 1 == count) {
40291de178edSIdo Schimmel 				rt6_multipath_flush(rt);
40301de178edSIdo Schimmel 				return -1;
40311de178edSIdo Schimmel 			}
40321de178edSIdo Schimmel 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
40331de178edSIdo Schimmel 						   RTNH_F_LINKDOWN);
40347aef6859SDavid Ahern 			fib6_update_sernum(net, rt);
4035d7dedee1SIdo Schimmel 			rt6_multipath_rebalance(rt);
40361de178edSIdo Schimmel 		}
40371de178edSIdo Schimmel 		return -2;
403827c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
4039ad1601aeSDavid Ahern 		if (rt->fib6_nh.fib_nh_dev != dev ||
404093c2fb25SDavid Ahern 		    rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST))
404127c6fa73SIdo Schimmel 			break;
4042ad1601aeSDavid Ahern 		rt->fib6_nh.fib_nh_flags |= RTNH_F_LINKDOWN;
4043d7dedee1SIdo Schimmel 		rt6_multipath_rebalance(rt);
404427c6fa73SIdo Schimmel 		break;
40452b241361SIdo Schimmel 	}
4046c159d30cSDavid S. Miller 
40471da177e4SLinus Torvalds 	return 0;
40481da177e4SLinus Torvalds }
40491da177e4SLinus Torvalds 
405027c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
40511da177e4SLinus Torvalds {
40524c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
40538ed67789SDaniel Lezcano 		.dev = dev,
40546802f3adSIdo Schimmel 		{
40554c981e28SIdo Schimmel 			.event = event,
40566802f3adSIdo Schimmel 		},
40578ed67789SDaniel Lezcano 	};
40587c6bb7d2SDavid Ahern 	struct net *net = dev_net(dev);
40598ed67789SDaniel Lezcano 
40607c6bb7d2SDavid Ahern 	if (net->ipv6.sysctl.skip_notify_on_dev_down)
40617c6bb7d2SDavid Ahern 		fib6_clean_all_skip_notify(net, fib6_ifdown, &arg);
40627c6bb7d2SDavid Ahern 	else
40637c6bb7d2SDavid Ahern 		fib6_clean_all(net, fib6_ifdown, &arg);
40644c981e28SIdo Schimmel }
40654c981e28SIdo Schimmel 
40664c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
40674c981e28SIdo Schimmel {
40684c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
40694c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
40704c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
40711da177e4SLinus Torvalds }
40721da177e4SLinus Torvalds 
407395c96174SEric Dumazet struct rt6_mtu_change_arg {
40741da177e4SLinus Torvalds 	struct net_device *dev;
407595c96174SEric Dumazet 	unsigned int mtu;
40761da177e4SLinus Torvalds };
40771da177e4SLinus Torvalds 
40788d1c802bSDavid Ahern static int rt6_mtu_change_route(struct fib6_info *rt, void *p_arg)
40791da177e4SLinus Torvalds {
40801da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
40811da177e4SLinus Torvalds 	struct inet6_dev *idev;
40821da177e4SLinus Torvalds 
40831da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
40841da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
40851da177e4SLinus Torvalds 	   We still use this lock to block changes
40861da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
40871da177e4SLinus Torvalds 	*/
40881da177e4SLinus Torvalds 
40891da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
409038308473SDavid S. Miller 	if (!idev)
40911da177e4SLinus Torvalds 		return 0;
40921da177e4SLinus Torvalds 
40931da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
40941da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
40951da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
40961da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
40971da177e4SLinus Torvalds 	 */
4098ad1601aeSDavid Ahern 	if (rt->fib6_nh.fib_nh_dev == arg->dev &&
4099d4ead6b3SDavid Ahern 	    !fib6_metric_locked(rt, RTAX_MTU)) {
4100d4ead6b3SDavid Ahern 		u32 mtu = rt->fib6_pmtu;
4101d4ead6b3SDavid Ahern 
4102d4ead6b3SDavid Ahern 		if (mtu >= arg->mtu ||
4103d4ead6b3SDavid Ahern 		    (mtu < arg->mtu && mtu == idev->cnf.mtu6))
4104d4ead6b3SDavid Ahern 			fib6_metric_set(rt, RTAX_MTU, arg->mtu);
4105d4ead6b3SDavid Ahern 
4106f5bbe7eeSWei Wang 		spin_lock_bh(&rt6_exception_lock);
4107e9fa1495SStefano Brivio 		rt6_exceptions_update_pmtu(idev, rt, arg->mtu);
4108f5bbe7eeSWei Wang 		spin_unlock_bh(&rt6_exception_lock);
41094b32b5adSMartin KaFai Lau 	}
41101da177e4SLinus Torvalds 	return 0;
41111da177e4SLinus Torvalds }
41121da177e4SLinus Torvalds 
411395c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
41141da177e4SLinus Torvalds {
4115c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
4116c71099acSThomas Graf 		.dev = dev,
4117c71099acSThomas Graf 		.mtu = mtu,
4118c71099acSThomas Graf 	};
41191da177e4SLinus Torvalds 
41200c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
41211da177e4SLinus Torvalds }
41221da177e4SLinus Torvalds 
4123ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
41245176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
4125aa8f8778SEric Dumazet 	[RTA_PREFSRC]		= { .len = sizeof(struct in6_addr) },
412686872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
4127ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
412886872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
412986872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
413051ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
4131c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
413219e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
413319e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
413432bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
4135622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
41363b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
4137aa8f8778SEric Dumazet 	[RTA_TABLE]		= { .type = NLA_U32 },
4138eacb9384SRoopa Prabhu 	[RTA_IP_PROTO]		= { .type = NLA_U8 },
4139eacb9384SRoopa Prabhu 	[RTA_SPORT]		= { .type = NLA_U16 },
4140eacb9384SRoopa Prabhu 	[RTA_DPORT]		= { .type = NLA_U16 },
414186872cb5SThomas Graf };
414286872cb5SThomas Graf 
414386872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
4144333c4301SDavid Ahern 			      struct fib6_config *cfg,
4145333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
41461da177e4SLinus Torvalds {
414786872cb5SThomas Graf 	struct rtmsg *rtm;
414886872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
4149c78ba6d6SLubomir Rintel 	unsigned int pref;
415086872cb5SThomas Graf 	int err;
41511da177e4SLinus Torvalds 
4152fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4153dac9c979SDavid Ahern 			  extack);
415486872cb5SThomas Graf 	if (err < 0)
415586872cb5SThomas Graf 		goto errout;
41561da177e4SLinus Torvalds 
415786872cb5SThomas Graf 	err = -EINVAL;
415886872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
415986872cb5SThomas Graf 
416084db8407SMaciej Żenczykowski 	*cfg = (struct fib6_config){
416184db8407SMaciej Żenczykowski 		.fc_table = rtm->rtm_table,
416284db8407SMaciej Żenczykowski 		.fc_dst_len = rtm->rtm_dst_len,
416384db8407SMaciej Żenczykowski 		.fc_src_len = rtm->rtm_src_len,
416484db8407SMaciej Żenczykowski 		.fc_flags = RTF_UP,
416584db8407SMaciej Żenczykowski 		.fc_protocol = rtm->rtm_protocol,
416684db8407SMaciej Żenczykowski 		.fc_type = rtm->rtm_type,
416784db8407SMaciej Żenczykowski 
416884db8407SMaciej Żenczykowski 		.fc_nlinfo.portid = NETLINK_CB(skb).portid,
416984db8407SMaciej Żenczykowski 		.fc_nlinfo.nlh = nlh,
417084db8407SMaciej Żenczykowski 		.fc_nlinfo.nl_net = sock_net(skb->sk),
417184db8407SMaciej Żenczykowski 	};
417286872cb5SThomas Graf 
4173ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
4174ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
4175b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
4176b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
417786872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
417886872cb5SThomas Graf 
4179ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
4180ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
4181ab79ad14SMaciej Żenczykowski 
41821f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
41831f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
41841f56a01fSMartin KaFai Lau 
4185fc1e64e1SDavid Ahern 	cfg->fc_flags |= (rtm->rtm_flags & RTNH_F_ONLINK);
4186fc1e64e1SDavid Ahern 
418786872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
418867b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
418986872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
41901da177e4SLinus Torvalds 	}
4191e3818541SDavid Ahern 	if (tb[RTA_VIA]) {
4192e3818541SDavid Ahern 		NL_SET_ERR_MSG(extack, "IPv6 does not support RTA_VIA attribute");
4193e3818541SDavid Ahern 		goto errout;
4194e3818541SDavid Ahern 	}
419586872cb5SThomas Graf 
419686872cb5SThomas Graf 	if (tb[RTA_DST]) {
419786872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
419886872cb5SThomas Graf 
419986872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
420086872cb5SThomas Graf 			goto errout;
420186872cb5SThomas Graf 
420286872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
42031da177e4SLinus Torvalds 	}
420486872cb5SThomas Graf 
420586872cb5SThomas Graf 	if (tb[RTA_SRC]) {
420686872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
420786872cb5SThomas Graf 
420886872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
420986872cb5SThomas Graf 			goto errout;
421086872cb5SThomas Graf 
421186872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
42121da177e4SLinus Torvalds 	}
421386872cb5SThomas Graf 
4214c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
421567b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
4216c3968a85SDaniel Walter 
421786872cb5SThomas Graf 	if (tb[RTA_OIF])
421886872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
421986872cb5SThomas Graf 
422086872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
422186872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
422286872cb5SThomas Graf 
422386872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
422486872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
422586872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
42261da177e4SLinus Torvalds 	}
422786872cb5SThomas Graf 
422886872cb5SThomas Graf 	if (tb[RTA_TABLE])
422986872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
423086872cb5SThomas Graf 
423151ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
423251ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
423351ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
42349ed59592SDavid Ahern 
42359ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
4236c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
42379ed59592SDavid Ahern 		if (err < 0)
42389ed59592SDavid Ahern 			goto errout;
423951ebd318SNicolas Dichtel 	}
424051ebd318SNicolas Dichtel 
4241c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
4242c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
4243c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
4244c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
4245c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
4246c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
4247c78ba6d6SLubomir Rintel 	}
4248c78ba6d6SLubomir Rintel 
424919e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
425019e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
425119e42e45SRoopa Prabhu 
42529ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
425319e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
425419e42e45SRoopa Prabhu 
4255c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
42569ed59592SDavid Ahern 		if (err < 0)
42579ed59592SDavid Ahern 			goto errout;
42589ed59592SDavid Ahern 	}
42599ed59592SDavid Ahern 
426032bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
426132bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
426232bc201eSXin Long 
426332bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
426432bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
426532bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
426632bc201eSXin Long 		}
426732bc201eSXin Long 	}
426832bc201eSXin Long 
426986872cb5SThomas Graf 	err = 0;
427086872cb5SThomas Graf errout:
427186872cb5SThomas Graf 	return err;
42721da177e4SLinus Torvalds }
42731da177e4SLinus Torvalds 
42746b9ea5a6SRoopa Prabhu struct rt6_nh {
42758d1c802bSDavid Ahern 	struct fib6_info *fib6_info;
42766b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
42776b9ea5a6SRoopa Prabhu 	struct list_head next;
42786b9ea5a6SRoopa Prabhu };
42796b9ea5a6SRoopa Prabhu 
4280d4ead6b3SDavid Ahern static int ip6_route_info_append(struct net *net,
4281d4ead6b3SDavid Ahern 				 struct list_head *rt6_nh_list,
42828d1c802bSDavid Ahern 				 struct fib6_info *rt,
42838d1c802bSDavid Ahern 				 struct fib6_config *r_cfg)
42846b9ea5a6SRoopa Prabhu {
42856b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
42866b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
42876b9ea5a6SRoopa Prabhu 
42886b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
42898d1c802bSDavid Ahern 		/* check if fib6_info already exists */
42908d1c802bSDavid Ahern 		if (rt6_duplicate_nexthop(nh->fib6_info, rt))
42916b9ea5a6SRoopa Prabhu 			return err;
42926b9ea5a6SRoopa Prabhu 	}
42936b9ea5a6SRoopa Prabhu 
42946b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
42956b9ea5a6SRoopa Prabhu 	if (!nh)
42966b9ea5a6SRoopa Prabhu 		return -ENOMEM;
42978d1c802bSDavid Ahern 	nh->fib6_info = rt;
42986b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
42996b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
43006b9ea5a6SRoopa Prabhu 
43016b9ea5a6SRoopa Prabhu 	return 0;
43026b9ea5a6SRoopa Prabhu }
43036b9ea5a6SRoopa Prabhu 
43048d1c802bSDavid Ahern static void ip6_route_mpath_notify(struct fib6_info *rt,
43058d1c802bSDavid Ahern 				   struct fib6_info *rt_last,
43063b1137feSDavid Ahern 				   struct nl_info *info,
43073b1137feSDavid Ahern 				   __u16 nlflags)
43083b1137feSDavid Ahern {
43093b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
43103b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
43113b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
43123b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
43133b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
43143b1137feSDavid Ahern 	 */
431593c2fb25SDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->fib6_nsiblings) {
431693c2fb25SDavid Ahern 		rt = list_first_entry(&rt_last->fib6_siblings,
43178d1c802bSDavid Ahern 				      struct fib6_info,
431893c2fb25SDavid Ahern 				      fib6_siblings);
43193b1137feSDavid Ahern 	}
43203b1137feSDavid Ahern 
43213b1137feSDavid Ahern 	if (rt)
43223b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
43233b1137feSDavid Ahern }
43243b1137feSDavid Ahern 
4325333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
4326333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
432751ebd318SNicolas Dichtel {
43288d1c802bSDavid Ahern 	struct fib6_info *rt_notif = NULL, *rt_last = NULL;
43293b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
433051ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
433151ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
43328d1c802bSDavid Ahern 	struct fib6_info *rt;
43336b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
43346b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
43353b1137feSDavid Ahern 	__u16 nlflags;
433651ebd318SNicolas Dichtel 	int remaining;
433751ebd318SNicolas Dichtel 	int attrlen;
43386b9ea5a6SRoopa Prabhu 	int err = 1;
43396b9ea5a6SRoopa Prabhu 	int nhn = 0;
43406b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
43416b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
43426b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
434351ebd318SNicolas Dichtel 
43443b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
43453b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
43463b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
43473b1137feSDavid Ahern 
434835f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
434951ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
435051ebd318SNicolas Dichtel 
43516b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
43528d1c802bSDavid Ahern 	 * fib6_info structs per nexthop
43536b9ea5a6SRoopa Prabhu 	 */
435451ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
435551ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
435651ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
435751ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
435851ebd318SNicolas Dichtel 
435951ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
436051ebd318SNicolas Dichtel 		if (attrlen > 0) {
436151ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
436251ebd318SNicolas Dichtel 
436351ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
436451ebd318SNicolas Dichtel 			if (nla) {
436567b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
436651ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
436751ebd318SNicolas Dichtel 			}
436819e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
436919e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
437019e42e45SRoopa Prabhu 			if (nla)
437119e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
437251ebd318SNicolas Dichtel 		}
43736b9ea5a6SRoopa Prabhu 
437468e2ffdeSDavid Ahern 		r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
4375acb54e3cSDavid Ahern 		rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
43768c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
43778c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
43788c5b83f0SRoopa Prabhu 			rt = NULL;
43796b9ea5a6SRoopa Prabhu 			goto cleanup;
43808c5b83f0SRoopa Prabhu 		}
4381b5d2d75eSDavid Ahern 		if (!rt6_qualify_for_ecmp(rt)) {
4382b5d2d75eSDavid Ahern 			err = -EINVAL;
4383b5d2d75eSDavid Ahern 			NL_SET_ERR_MSG(extack,
4384b5d2d75eSDavid Ahern 				       "Device only routes can not be added for IPv6 using the multipath API.");
4385b5d2d75eSDavid Ahern 			fib6_info_release(rt);
4386b5d2d75eSDavid Ahern 			goto cleanup;
4387b5d2d75eSDavid Ahern 		}
43886b9ea5a6SRoopa Prabhu 
4389ad1601aeSDavid Ahern 		rt->fib6_nh.fib_nh_weight = rtnh->rtnh_hops + 1;
4390398958aeSIdo Schimmel 
4391d4ead6b3SDavid Ahern 		err = ip6_route_info_append(info->nl_net, &rt6_nh_list,
4392d4ead6b3SDavid Ahern 					    rt, &r_cfg);
439351ebd318SNicolas Dichtel 		if (err) {
439493531c67SDavid Ahern 			fib6_info_release(rt);
43956b9ea5a6SRoopa Prabhu 			goto cleanup;
439651ebd318SNicolas Dichtel 		}
43976b9ea5a6SRoopa Prabhu 
43986b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
439951ebd318SNicolas Dichtel 	}
44006b9ea5a6SRoopa Prabhu 
44013b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
44023b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
44033b1137feSDavid Ahern 	 * the full route when done
44043b1137feSDavid Ahern 	 */
44053b1137feSDavid Ahern 	info->skip_notify = 1;
44063b1137feSDavid Ahern 
44076b9ea5a6SRoopa Prabhu 	err_nh = NULL;
44086b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
44098d1c802bSDavid Ahern 		err = __ip6_ins_rt(nh->fib6_info, info, extack);
44108d1c802bSDavid Ahern 		fib6_info_release(nh->fib6_info);
44113b1137feSDavid Ahern 
4412f7225172SDavid Ahern 		if (!err) {
4413f7225172SDavid Ahern 			/* save reference to last route successfully inserted */
4414f7225172SDavid Ahern 			rt_last = nh->fib6_info;
4415f7225172SDavid Ahern 
44166b9ea5a6SRoopa Prabhu 			/* save reference to first route for notification */
4417f7225172SDavid Ahern 			if (!rt_notif)
44188d1c802bSDavid Ahern 				rt_notif = nh->fib6_info;
4419f7225172SDavid Ahern 		}
44206b9ea5a6SRoopa Prabhu 
44218d1c802bSDavid Ahern 		/* nh->fib6_info is used or freed at this point, reset to NULL*/
44228d1c802bSDavid Ahern 		nh->fib6_info = NULL;
44236b9ea5a6SRoopa Prabhu 		if (err) {
44246b9ea5a6SRoopa Prabhu 			if (replace && nhn)
4425a5a82d84SJakub Kicinski 				NL_SET_ERR_MSG_MOD(extack,
4426a5a82d84SJakub Kicinski 						   "multipath route replace failed (check consistency of installed routes)");
44276b9ea5a6SRoopa Prabhu 			err_nh = nh;
44286b9ea5a6SRoopa Prabhu 			goto add_errout;
44296b9ea5a6SRoopa Prabhu 		}
44306b9ea5a6SRoopa Prabhu 
44311a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
443227596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
443327596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
443427596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
443527596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
443627596472SMichal Kubeček 		 * be added to it.
44371a72418bSNicolas Dichtel 		 */
443827596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
443927596472SMichal Kubeček 						     NLM_F_REPLACE);
44406b9ea5a6SRoopa Prabhu 		nhn++;
44416b9ea5a6SRoopa Prabhu 	}
44426b9ea5a6SRoopa Prabhu 
44433b1137feSDavid Ahern 	/* success ... tell user about new route */
44443b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
44456b9ea5a6SRoopa Prabhu 	goto cleanup;
44466b9ea5a6SRoopa Prabhu 
44476b9ea5a6SRoopa Prabhu add_errout:
44483b1137feSDavid Ahern 	/* send notification for routes that were added so that
44493b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
44503b1137feSDavid Ahern 	 * coherent
44513b1137feSDavid Ahern 	 */
44523b1137feSDavid Ahern 	if (rt_notif)
44533b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
44543b1137feSDavid Ahern 
44556b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
44566b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
44576b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
44586b9ea5a6SRoopa Prabhu 			break;
4459333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
44606b9ea5a6SRoopa Prabhu 	}
44616b9ea5a6SRoopa Prabhu 
44626b9ea5a6SRoopa Prabhu cleanup:
44636b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
44648d1c802bSDavid Ahern 		if (nh->fib6_info)
44658d1c802bSDavid Ahern 			fib6_info_release(nh->fib6_info);
44666b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
44676b9ea5a6SRoopa Prabhu 		kfree(nh);
44686b9ea5a6SRoopa Prabhu 	}
44696b9ea5a6SRoopa Prabhu 
44706b9ea5a6SRoopa Prabhu 	return err;
44716b9ea5a6SRoopa Prabhu }
44726b9ea5a6SRoopa Prabhu 
4473333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
4474333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
44756b9ea5a6SRoopa Prabhu {
44766b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
44776b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
44786b9ea5a6SRoopa Prabhu 	int remaining;
44796b9ea5a6SRoopa Prabhu 	int attrlen;
44806b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
44816b9ea5a6SRoopa Prabhu 
44826b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
44836b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
44846b9ea5a6SRoopa Prabhu 
44856b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
44866b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
44876b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
44886b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
44896b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
44906b9ea5a6SRoopa Prabhu 
44916b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
44926b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
44936b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
44946b9ea5a6SRoopa Prabhu 
44956b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
44966b9ea5a6SRoopa Prabhu 			if (nla) {
44976b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
44986b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
44996b9ea5a6SRoopa Prabhu 			}
45006b9ea5a6SRoopa Prabhu 		}
4501333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
45026b9ea5a6SRoopa Prabhu 		if (err)
45036b9ea5a6SRoopa Prabhu 			last_err = err;
45046b9ea5a6SRoopa Prabhu 
450551ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
450651ebd318SNicolas Dichtel 	}
450751ebd318SNicolas Dichtel 
450851ebd318SNicolas Dichtel 	return last_err;
450951ebd318SNicolas Dichtel }
451051ebd318SNicolas Dichtel 
4511c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4512c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
45131da177e4SLinus Torvalds {
451486872cb5SThomas Graf 	struct fib6_config cfg;
451586872cb5SThomas Graf 	int err;
45161da177e4SLinus Torvalds 
4517333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
451886872cb5SThomas Graf 	if (err < 0)
451986872cb5SThomas Graf 		return err;
452086872cb5SThomas Graf 
452151ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4522333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
45230ae81335SDavid Ahern 	else {
45240ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
4525333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
45261da177e4SLinus Torvalds 	}
45270ae81335SDavid Ahern }
45281da177e4SLinus Torvalds 
4529c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4530c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
45311da177e4SLinus Torvalds {
453286872cb5SThomas Graf 	struct fib6_config cfg;
453386872cb5SThomas Graf 	int err;
45341da177e4SLinus Torvalds 
4535333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
453686872cb5SThomas Graf 	if (err < 0)
453786872cb5SThomas Graf 		return err;
453886872cb5SThomas Graf 
453967f69513SDavid Ahern 	if (cfg.fc_metric == 0)
454067f69513SDavid Ahern 		cfg.fc_metric = IP6_RT_PRIO_USER;
454167f69513SDavid Ahern 
454251ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4543333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
454451ebd318SNicolas Dichtel 	else
4545acb54e3cSDavid Ahern 		return ip6_route_add(&cfg, GFP_KERNEL, extack);
45461da177e4SLinus Torvalds }
45471da177e4SLinus Torvalds 
45488d1c802bSDavid Ahern static size_t rt6_nlmsg_size(struct fib6_info *rt)
4549339bf98fSThomas Graf {
4550beb1afacSDavid Ahern 	int nexthop_len = 0;
4551beb1afacSDavid Ahern 
455293c2fb25SDavid Ahern 	if (rt->fib6_nsiblings) {
4553beb1afacSDavid Ahern 		nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
4554beb1afacSDavid Ahern 			    + NLA_ALIGN(sizeof(struct rtnexthop))
4555beb1afacSDavid Ahern 			    + nla_total_size(16) /* RTA_GATEWAY */
4556ad1601aeSDavid Ahern 			    + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws);
4557beb1afacSDavid Ahern 
455893c2fb25SDavid Ahern 		nexthop_len *= rt->fib6_nsiblings;
4559beb1afacSDavid Ahern 	}
4560beb1afacSDavid Ahern 
4561339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
4562339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
4563339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
4564339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
4565339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
4566339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
4567339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
4568339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
4569339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
45706a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
4571ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
4572c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
457319e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
4574ad1601aeSDavid Ahern 	       + lwtunnel_get_encap_size(rt->fib6_nh.fib_nh_lws)
4575beb1afacSDavid Ahern 	       + nexthop_len;
4576beb1afacSDavid Ahern }
4577beb1afacSDavid Ahern 
4578d4ead6b3SDavid Ahern static int rt6_fill_node(struct net *net, struct sk_buff *skb,
45798d1c802bSDavid Ahern 			 struct fib6_info *rt, struct dst_entry *dst,
4580d4ead6b3SDavid Ahern 			 struct in6_addr *dest, struct in6_addr *src,
458115e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
4582f8cfe2ceSDavid Ahern 			 unsigned int flags)
45831da177e4SLinus Torvalds {
458422d0bd82SXin Long 	struct rt6_info *rt6 = (struct rt6_info *)dst;
458522d0bd82SXin Long 	struct rt6key *rt6_dst, *rt6_src;
458622d0bd82SXin Long 	u32 *pmetrics, table, rt6_flags;
45871da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
458822d0bd82SXin Long 	struct rtmsg *rtm;
4589d4ead6b3SDavid Ahern 	long expires = 0;
45901da177e4SLinus Torvalds 
459115e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
459238308473SDavid S. Miller 	if (!nlh)
459326932566SPatrick McHardy 		return -EMSGSIZE;
45942d7202bfSThomas Graf 
459522d0bd82SXin Long 	if (rt6) {
459622d0bd82SXin Long 		rt6_dst = &rt6->rt6i_dst;
459722d0bd82SXin Long 		rt6_src = &rt6->rt6i_src;
459822d0bd82SXin Long 		rt6_flags = rt6->rt6i_flags;
459922d0bd82SXin Long 	} else {
460022d0bd82SXin Long 		rt6_dst = &rt->fib6_dst;
460122d0bd82SXin Long 		rt6_src = &rt->fib6_src;
460222d0bd82SXin Long 		rt6_flags = rt->fib6_flags;
460322d0bd82SXin Long 	}
460422d0bd82SXin Long 
46052d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
46061da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
460722d0bd82SXin Long 	rtm->rtm_dst_len = rt6_dst->plen;
460822d0bd82SXin Long 	rtm->rtm_src_len = rt6_src->plen;
46091da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
461093c2fb25SDavid Ahern 	if (rt->fib6_table)
461193c2fb25SDavid Ahern 		table = rt->fib6_table->tb6_id;
4612c71099acSThomas Graf 	else
46139e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
461497f0082aSKalash Nainwal 	rtm->rtm_table = table < 256 ? table : RT_TABLE_COMPAT;
4615c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
4616c78679e8SDavid S. Miller 		goto nla_put_failure;
4617e8478e80SDavid Ahern 
4618e8478e80SDavid Ahern 	rtm->rtm_type = rt->fib6_type;
46191da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
46201da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
462193c2fb25SDavid Ahern 	rtm->rtm_protocol = rt->fib6_protocol;
46221da177e4SLinus Torvalds 
462322d0bd82SXin Long 	if (rt6_flags & RTF_CACHE)
46241da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
46251da177e4SLinus Torvalds 
4626d4ead6b3SDavid Ahern 	if (dest) {
4627d4ead6b3SDavid Ahern 		if (nla_put_in6_addr(skb, RTA_DST, dest))
4628c78679e8SDavid S. Miller 			goto nla_put_failure;
46291da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
46301da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
463122d0bd82SXin Long 		if (nla_put_in6_addr(skb, RTA_DST, &rt6_dst->addr))
4632c78679e8SDavid S. Miller 			goto nla_put_failure;
46331da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
46341da177e4SLinus Torvalds 	if (src) {
4635930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
4636c78679e8SDavid S. Miller 			goto nla_put_failure;
46371da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
4638c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
463922d0bd82SXin Long 		   nla_put_in6_addr(skb, RTA_SRC, &rt6_src->addr))
4640c78679e8SDavid S. Miller 		goto nla_put_failure;
46411da177e4SLinus Torvalds #endif
46427bc570c8SYOSHIFUJI Hideaki 	if (iif) {
46437bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
464422d0bd82SXin Long 		if (ipv6_addr_is_multicast(&rt6_dst->addr)) {
4645fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
46462cf75070SNikolay Aleksandrov 
46477bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
46487bc570c8SYOSHIFUJI Hideaki 				return 0;
4649fd61c6baSDavid Ahern 			if (err < 0)
46507bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
46517bc570c8SYOSHIFUJI Hideaki 		} else
46527bc570c8SYOSHIFUJI Hideaki #endif
4653c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
4654c78679e8SDavid S. Miller 				goto nla_put_failure;
4655d4ead6b3SDavid Ahern 	} else if (dest) {
46561da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
4657d4ead6b3SDavid Ahern 		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
4658930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4659c78679e8SDavid S. Miller 			goto nla_put_failure;
4660c3968a85SDaniel Walter 	}
4661c3968a85SDaniel Walter 
466293c2fb25SDavid Ahern 	if (rt->fib6_prefsrc.plen) {
4663c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
466493c2fb25SDavid Ahern 		saddr_buf = rt->fib6_prefsrc.addr;
4665930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4666c78679e8SDavid S. Miller 			goto nla_put_failure;
46671da177e4SLinus Torvalds 	}
46682d7202bfSThomas Graf 
4669d4ead6b3SDavid Ahern 	pmetrics = dst ? dst_metrics_ptr(dst) : rt->fib6_metrics->metrics;
4670d4ead6b3SDavid Ahern 	if (rtnetlink_put_metrics(skb, pmetrics) < 0)
46712d7202bfSThomas Graf 		goto nla_put_failure;
46722d7202bfSThomas Graf 
467393c2fb25SDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->fib6_metric))
4674beb1afacSDavid Ahern 		goto nla_put_failure;
4675beb1afacSDavid Ahern 
4676beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
4677beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
4678beb1afacSDavid Ahern 	 */
467922d0bd82SXin Long 	if (rt6) {
468022d0bd82SXin Long 		if (rt6_flags & RTF_GATEWAY &&
468122d0bd82SXin Long 		    nla_put_in6_addr(skb, RTA_GATEWAY, &rt6->rt6i_gateway))
468222d0bd82SXin Long 			goto nla_put_failure;
468322d0bd82SXin Long 
468422d0bd82SXin Long 		if (dst->dev && nla_put_u32(skb, RTA_OIF, dst->dev->ifindex))
468522d0bd82SXin Long 			goto nla_put_failure;
468622d0bd82SXin Long 	} else if (rt->fib6_nsiblings) {
46878d1c802bSDavid Ahern 		struct fib6_info *sibling, *next_sibling;
4688beb1afacSDavid Ahern 		struct nlattr *mp;
4689beb1afacSDavid Ahern 
4690beb1afacSDavid Ahern 		mp = nla_nest_start(skb, RTA_MULTIPATH);
4691beb1afacSDavid Ahern 		if (!mp)
4692beb1afacSDavid Ahern 			goto nla_put_failure;
4693beb1afacSDavid Ahern 
4694c0a72077SDavid Ahern 		if (fib_add_nexthop(skb, &rt->fib6_nh.nh_common,
4695c0a72077SDavid Ahern 				    rt->fib6_nh.fib_nh_weight) < 0)
4696beb1afacSDavid Ahern 			goto nla_put_failure;
4697beb1afacSDavid Ahern 
4698beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
469993c2fb25SDavid Ahern 					 &rt->fib6_siblings, fib6_siblings) {
4700c0a72077SDavid Ahern 			if (fib_add_nexthop(skb, &sibling->fib6_nh.nh_common,
4701c0a72077SDavid Ahern 					    sibling->fib6_nh.fib_nh_weight) < 0)
470294f826b8SEric Dumazet 				goto nla_put_failure;
470394f826b8SEric Dumazet 		}
47042d7202bfSThomas Graf 
4705beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
4706beb1afacSDavid Ahern 	} else {
4707c0a72077SDavid Ahern 		if (fib_nexthop_info(skb, &rt->fib6_nh.nh_common,
4708c0a72077SDavid Ahern 				     &rtm->rtm_flags, false) < 0)
4709c78679e8SDavid S. Miller 			goto nla_put_failure;
4710beb1afacSDavid Ahern 	}
47118253947eSLi Wei 
471222d0bd82SXin Long 	if (rt6_flags & RTF_EXPIRES) {
471314895687SDavid Ahern 		expires = dst ? dst->expires : rt->expires;
471414895687SDavid Ahern 		expires -= jiffies;
471514895687SDavid Ahern 	}
471669cdf8f9SYOSHIFUJI Hideaki 
4717d4ead6b3SDavid Ahern 	if (rtnl_put_cacheinfo(skb, dst, 0, expires, dst ? dst->error : 0) < 0)
4718e3703b3dSThomas Graf 		goto nla_put_failure;
47191da177e4SLinus Torvalds 
472022d0bd82SXin Long 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt6_flags)))
4721c78ba6d6SLubomir Rintel 		goto nla_put_failure;
4722c78ba6d6SLubomir Rintel 
472319e42e45SRoopa Prabhu 
4724053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4725053c095aSJohannes Berg 	return 0;
47262d7202bfSThomas Graf 
47272d7202bfSThomas Graf nla_put_failure:
472826932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
472926932566SPatrick McHardy 	return -EMSGSIZE;
47301da177e4SLinus Torvalds }
47311da177e4SLinus Torvalds 
473213e38901SDavid Ahern static bool fib6_info_uses_dev(const struct fib6_info *f6i,
473313e38901SDavid Ahern 			       const struct net_device *dev)
473413e38901SDavid Ahern {
4735ad1601aeSDavid Ahern 	if (f6i->fib6_nh.fib_nh_dev == dev)
473613e38901SDavid Ahern 		return true;
473713e38901SDavid Ahern 
473813e38901SDavid Ahern 	if (f6i->fib6_nsiblings) {
473913e38901SDavid Ahern 		struct fib6_info *sibling, *next_sibling;
474013e38901SDavid Ahern 
474113e38901SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
474213e38901SDavid Ahern 					 &f6i->fib6_siblings, fib6_siblings) {
4743ad1601aeSDavid Ahern 			if (sibling->fib6_nh.fib_nh_dev == dev)
474413e38901SDavid Ahern 				return true;
474513e38901SDavid Ahern 		}
474613e38901SDavid Ahern 	}
474713e38901SDavid Ahern 
474813e38901SDavid Ahern 	return false;
474913e38901SDavid Ahern }
475013e38901SDavid Ahern 
47518d1c802bSDavid Ahern int rt6_dump_route(struct fib6_info *rt, void *p_arg)
47521da177e4SLinus Torvalds {
47531da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
475413e38901SDavid Ahern 	struct fib_dump_filter *filter = &arg->filter;
475513e38901SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
47561f17e2f2SDavid Ahern 	struct net *net = arg->net;
47571f17e2f2SDavid Ahern 
4758421842edSDavid Ahern 	if (rt == net->ipv6.fib6_null_entry)
47591f17e2f2SDavid Ahern 		return 0;
47601da177e4SLinus Torvalds 
476113e38901SDavid Ahern 	if ((filter->flags & RTM_F_PREFIX) &&
476293c2fb25SDavid Ahern 	    !(rt->fib6_flags & RTF_PREFIX_RT)) {
4763f8cfe2ceSDavid Ahern 		/* success since this is not a prefix route */
4764f8cfe2ceSDavid Ahern 		return 1;
4765f8cfe2ceSDavid Ahern 	}
476613e38901SDavid Ahern 	if (filter->filter_set) {
476713e38901SDavid Ahern 		if ((filter->rt_type && rt->fib6_type != filter->rt_type) ||
476813e38901SDavid Ahern 		    (filter->dev && !fib6_info_uses_dev(rt, filter->dev)) ||
476913e38901SDavid Ahern 		    (filter->protocol && rt->fib6_protocol != filter->protocol)) {
477013e38901SDavid Ahern 			return 1;
477113e38901SDavid Ahern 		}
477213e38901SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
4773f8cfe2ceSDavid Ahern 	}
47741da177e4SLinus Torvalds 
4775d4ead6b3SDavid Ahern 	return rt6_fill_node(net, arg->skb, rt, NULL, NULL, NULL, 0,
4776d4ead6b3SDavid Ahern 			     RTM_NEWROUTE, NETLINK_CB(arg->cb->skb).portid,
477713e38901SDavid Ahern 			     arg->cb->nlh->nlmsg_seq, flags);
47781da177e4SLinus Torvalds }
47791da177e4SLinus Torvalds 
47800eff0a27SJakub Kicinski static int inet6_rtm_valid_getroute_req(struct sk_buff *skb,
47810eff0a27SJakub Kicinski 					const struct nlmsghdr *nlh,
47820eff0a27SJakub Kicinski 					struct nlattr **tb,
47830eff0a27SJakub Kicinski 					struct netlink_ext_ack *extack)
47840eff0a27SJakub Kicinski {
47850eff0a27SJakub Kicinski 	struct rtmsg *rtm;
47860eff0a27SJakub Kicinski 	int i, err;
47870eff0a27SJakub Kicinski 
47880eff0a27SJakub Kicinski 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
47890eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
47900eff0a27SJakub Kicinski 				   "Invalid header for get route request");
47910eff0a27SJakub Kicinski 		return -EINVAL;
47920eff0a27SJakub Kicinski 	}
47930eff0a27SJakub Kicinski 
47940eff0a27SJakub Kicinski 	if (!netlink_strict_get_check(skb))
47950eff0a27SJakub Kicinski 		return nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
47960eff0a27SJakub Kicinski 				   rtm_ipv6_policy, extack);
47970eff0a27SJakub Kicinski 
47980eff0a27SJakub Kicinski 	rtm = nlmsg_data(nlh);
47990eff0a27SJakub Kicinski 	if ((rtm->rtm_src_len && rtm->rtm_src_len != 128) ||
48000eff0a27SJakub Kicinski 	    (rtm->rtm_dst_len && rtm->rtm_dst_len != 128) ||
48010eff0a27SJakub Kicinski 	    rtm->rtm_table || rtm->rtm_protocol || rtm->rtm_scope ||
48020eff0a27SJakub Kicinski 	    rtm->rtm_type) {
48030eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get route request");
48040eff0a27SJakub Kicinski 		return -EINVAL;
48050eff0a27SJakub Kicinski 	}
48060eff0a27SJakub Kicinski 	if (rtm->rtm_flags & ~RTM_F_FIB_MATCH) {
48070eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack,
48080eff0a27SJakub Kicinski 				   "Invalid flags for get route request");
48090eff0a27SJakub Kicinski 		return -EINVAL;
48100eff0a27SJakub Kicinski 	}
48110eff0a27SJakub Kicinski 
48120eff0a27SJakub Kicinski 	err = nlmsg_parse_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
48130eff0a27SJakub Kicinski 				 rtm_ipv6_policy, extack);
48140eff0a27SJakub Kicinski 	if (err)
48150eff0a27SJakub Kicinski 		return err;
48160eff0a27SJakub Kicinski 
48170eff0a27SJakub Kicinski 	if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
48180eff0a27SJakub Kicinski 	    (tb[RTA_DST] && !rtm->rtm_dst_len)) {
48190eff0a27SJakub Kicinski 		NL_SET_ERR_MSG_MOD(extack, "rtm_src_len and rtm_dst_len must be 128 for IPv6");
48200eff0a27SJakub Kicinski 		return -EINVAL;
48210eff0a27SJakub Kicinski 	}
48220eff0a27SJakub Kicinski 
48230eff0a27SJakub Kicinski 	for (i = 0; i <= RTA_MAX; i++) {
48240eff0a27SJakub Kicinski 		if (!tb[i])
48250eff0a27SJakub Kicinski 			continue;
48260eff0a27SJakub Kicinski 
48270eff0a27SJakub Kicinski 		switch (i) {
48280eff0a27SJakub Kicinski 		case RTA_SRC:
48290eff0a27SJakub Kicinski 		case RTA_DST:
48300eff0a27SJakub Kicinski 		case RTA_IIF:
48310eff0a27SJakub Kicinski 		case RTA_OIF:
48320eff0a27SJakub Kicinski 		case RTA_MARK:
48330eff0a27SJakub Kicinski 		case RTA_UID:
48340eff0a27SJakub Kicinski 		case RTA_SPORT:
48350eff0a27SJakub Kicinski 		case RTA_DPORT:
48360eff0a27SJakub Kicinski 		case RTA_IP_PROTO:
48370eff0a27SJakub Kicinski 			break;
48380eff0a27SJakub Kicinski 		default:
48390eff0a27SJakub Kicinski 			NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get route request");
48400eff0a27SJakub Kicinski 			return -EINVAL;
48410eff0a27SJakub Kicinski 		}
48420eff0a27SJakub Kicinski 	}
48430eff0a27SJakub Kicinski 
48440eff0a27SJakub Kicinski 	return 0;
48450eff0a27SJakub Kicinski }
48460eff0a27SJakub Kicinski 
4847c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4848c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
48491da177e4SLinus Torvalds {
48503b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
4851ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
485218c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
4853a68886a6SDavid Ahern 	struct fib6_info *from;
485418c3a61cSRoopa Prabhu 	struct dst_entry *dst;
48551da177e4SLinus Torvalds 	struct rt6_info *rt;
4856ab364a6fSThomas Graf 	struct sk_buff *skb;
4857ab364a6fSThomas Graf 	struct rtmsg *rtm;
4858744486d4SMaciej Żenczykowski 	struct flowi6 fl6 = {};
485918c3a61cSRoopa Prabhu 	bool fibmatch;
4860ab364a6fSThomas Graf 
48610eff0a27SJakub Kicinski 	err = inet6_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
4862ab364a6fSThomas Graf 	if (err < 0)
4863ab364a6fSThomas Graf 		goto errout;
4864ab364a6fSThomas Graf 
4865ab364a6fSThomas Graf 	err = -EINVAL;
486638b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
486738b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
486818c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
4869ab364a6fSThomas Graf 
4870ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
4871ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4872ab364a6fSThomas Graf 			goto errout;
4873ab364a6fSThomas Graf 
48744e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
4875ab364a6fSThomas Graf 	}
4876ab364a6fSThomas Graf 
4877ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
4878ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4879ab364a6fSThomas Graf 			goto errout;
4880ab364a6fSThomas Graf 
48814e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
4882ab364a6fSThomas Graf 	}
4883ab364a6fSThomas Graf 
4884ab364a6fSThomas Graf 	if (tb[RTA_IIF])
4885ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
4886ab364a6fSThomas Graf 
4887ab364a6fSThomas Graf 	if (tb[RTA_OIF])
488872331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
4889ab364a6fSThomas Graf 
48902e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
48912e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
48922e47b291SLorenzo Colitti 
4893622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
4894622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
4895622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
4896622ec2c9SLorenzo Colitti 	else
4897622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4898622ec2c9SLorenzo Colitti 
4899eacb9384SRoopa Prabhu 	if (tb[RTA_SPORT])
4900eacb9384SRoopa Prabhu 		fl6.fl6_sport = nla_get_be16(tb[RTA_SPORT]);
4901eacb9384SRoopa Prabhu 
4902eacb9384SRoopa Prabhu 	if (tb[RTA_DPORT])
4903eacb9384SRoopa Prabhu 		fl6.fl6_dport = nla_get_be16(tb[RTA_DPORT]);
4904eacb9384SRoopa Prabhu 
4905eacb9384SRoopa Prabhu 	if (tb[RTA_IP_PROTO]) {
4906eacb9384SRoopa Prabhu 		err = rtm_getroute_parse_ip_proto(tb[RTA_IP_PROTO],
49075e1a99eaSHangbin Liu 						  &fl6.flowi6_proto, AF_INET6,
49085e1a99eaSHangbin Liu 						  extack);
4909eacb9384SRoopa Prabhu 		if (err)
4910eacb9384SRoopa Prabhu 			goto errout;
4911eacb9384SRoopa Prabhu 	}
4912eacb9384SRoopa Prabhu 
4913ab364a6fSThomas Graf 	if (iif) {
4914ab364a6fSThomas Graf 		struct net_device *dev;
491572331bc0SShmulik Ladkani 		int flags = 0;
491672331bc0SShmulik Ladkani 
4917121622dbSFlorian Westphal 		rcu_read_lock();
4918121622dbSFlorian Westphal 
4919121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
4920ab364a6fSThomas Graf 		if (!dev) {
4921121622dbSFlorian Westphal 			rcu_read_unlock();
4922ab364a6fSThomas Graf 			err = -ENODEV;
4923ab364a6fSThomas Graf 			goto errout;
4924ab364a6fSThomas Graf 		}
492572331bc0SShmulik Ladkani 
492672331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
492772331bc0SShmulik Ladkani 
492872331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
492972331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
493072331bc0SShmulik Ladkani 
4931b75cc8f9SDavid Ahern 		dst = ip6_route_input_lookup(net, dev, &fl6, NULL, flags);
4932121622dbSFlorian Westphal 
4933121622dbSFlorian Westphal 		rcu_read_unlock();
493472331bc0SShmulik Ladkani 	} else {
493572331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
493672331bc0SShmulik Ladkani 
493718c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
493818c3a61cSRoopa Prabhu 	}
493918c3a61cSRoopa Prabhu 
494018c3a61cSRoopa Prabhu 
494118c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
494218c3a61cSRoopa Prabhu 	if (rt->dst.error) {
494318c3a61cSRoopa Prabhu 		err = rt->dst.error;
494418c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
494518c3a61cSRoopa Prabhu 		goto errout;
4946ab364a6fSThomas Graf 	}
49471da177e4SLinus Torvalds 
49489d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
49499d6acb3bSWANG Cong 		err = rt->dst.error;
49509d6acb3bSWANG Cong 		ip6_rt_put(rt);
49519d6acb3bSWANG Cong 		goto errout;
49529d6acb3bSWANG Cong 	}
49539d6acb3bSWANG Cong 
49541da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
495538308473SDavid S. Miller 	if (!skb) {
495694e187c0SAmerigo Wang 		ip6_rt_put(rt);
4957ab364a6fSThomas Graf 		err = -ENOBUFS;
4958ab364a6fSThomas Graf 		goto errout;
4959ab364a6fSThomas Graf 	}
49601da177e4SLinus Torvalds 
4961d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
4962a68886a6SDavid Ahern 
4963a68886a6SDavid Ahern 	rcu_read_lock();
4964a68886a6SDavid Ahern 	from = rcu_dereference(rt->from);
4965a68886a6SDavid Ahern 
496618c3a61cSRoopa Prabhu 	if (fibmatch)
4967a68886a6SDavid Ahern 		err = rt6_fill_node(net, skb, from, NULL, NULL, NULL, iif,
496818c3a61cSRoopa Prabhu 				    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
496918c3a61cSRoopa Prabhu 				    nlh->nlmsg_seq, 0);
497018c3a61cSRoopa Prabhu 	else
4971a68886a6SDavid Ahern 		err = rt6_fill_node(net, skb, from, dst, &fl6.daddr,
4972a68886a6SDavid Ahern 				    &fl6.saddr, iif, RTM_NEWROUTE,
4973d4ead6b3SDavid Ahern 				    NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
4974d4ead6b3SDavid Ahern 				    0);
4975a68886a6SDavid Ahern 	rcu_read_unlock();
4976a68886a6SDavid Ahern 
49771da177e4SLinus Torvalds 	if (err < 0) {
4978ab364a6fSThomas Graf 		kfree_skb(skb);
4979ab364a6fSThomas Graf 		goto errout;
49801da177e4SLinus Torvalds 	}
49811da177e4SLinus Torvalds 
498215e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4983ab364a6fSThomas Graf errout:
49841da177e4SLinus Torvalds 	return err;
49851da177e4SLinus Torvalds }
49861da177e4SLinus Torvalds 
49878d1c802bSDavid Ahern void inet6_rt_notify(int event, struct fib6_info *rt, struct nl_info *info,
498837a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
49891da177e4SLinus Torvalds {
49901da177e4SLinus Torvalds 	struct sk_buff *skb;
49915578689aSDaniel Lezcano 	struct net *net = info->nl_net;
4992528c4cebSDenis V. Lunev 	u32 seq;
4993528c4cebSDenis V. Lunev 	int err;
49940d51aa80SJamal Hadi Salim 
4995528c4cebSDenis V. Lunev 	err = -ENOBUFS;
499638308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
499786872cb5SThomas Graf 
499819e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
499938308473SDavid S. Miller 	if (!skb)
500021713ebcSThomas Graf 		goto errout;
50011da177e4SLinus Torvalds 
5002d4ead6b3SDavid Ahern 	err = rt6_fill_node(net, skb, rt, NULL, NULL, NULL, 0,
5003f8cfe2ceSDavid Ahern 			    event, info->portid, seq, nlm_flags);
500426932566SPatrick McHardy 	if (err < 0) {
500526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
500626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
500726932566SPatrick McHardy 		kfree_skb(skb);
500826932566SPatrick McHardy 		goto errout;
500926932566SPatrick McHardy 	}
501015e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
50115578689aSDaniel Lezcano 		    info->nlh, gfp_any());
50121ce85fe4SPablo Neira Ayuso 	return;
501321713ebcSThomas Graf errout:
501421713ebcSThomas Graf 	if (err < 0)
50155578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
50161da177e4SLinus Torvalds }
50171da177e4SLinus Torvalds 
50188ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
5019351638e7SJiri Pirko 				unsigned long event, void *ptr)
50208ed67789SDaniel Lezcano {
5021351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5022c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
50238ed67789SDaniel Lezcano 
5024242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
5025242d3a49SWANG Cong 		return NOTIFY_OK;
5026242d3a49SWANG Cong 
5027242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
5028ad1601aeSDavid Ahern 		net->ipv6.fib6_null_entry->fib6_nh.fib_nh_dev = dev;
5029d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
50308ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
50318ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5032d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
50338ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
5034d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
50358ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
50368ed67789SDaniel Lezcano #endif
503776da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
503876da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
503976da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
504076da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
504176da0704SWANG Cong 		 */
504212d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
5043242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
504412d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
504512d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
5046242d3a49SWANG Cong #endif
50478ed67789SDaniel Lezcano 	}
50488ed67789SDaniel Lezcano 
50498ed67789SDaniel Lezcano 	return NOTIFY_OK;
50508ed67789SDaniel Lezcano }
50518ed67789SDaniel Lezcano 
50521da177e4SLinus Torvalds /*
50531da177e4SLinus Torvalds  *	/proc
50541da177e4SLinus Torvalds  */
50551da177e4SLinus Torvalds 
50561da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
50571da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
50581da177e4SLinus Torvalds {
505969ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
50601da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
506169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
506269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
506381eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
506469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
506569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
5066fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
506769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
50681da177e4SLinus Torvalds 
50691da177e4SLinus Torvalds 	return 0;
50701da177e4SLinus Torvalds }
50711da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
50721da177e4SLinus Torvalds 
50731da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
50741da177e4SLinus Torvalds 
50751da177e4SLinus Torvalds static
5076fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
50771da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
50781da177e4SLinus Torvalds {
5079c486da34SLucian Adrian Grijincu 	struct net *net;
5080c486da34SLucian Adrian Grijincu 	int delay;
5081f0fb9b28SAditya Pakki 	int ret;
5082c486da34SLucian Adrian Grijincu 	if (!write)
5083c486da34SLucian Adrian Grijincu 		return -EINVAL;
5084c486da34SLucian Adrian Grijincu 
5085c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
5086c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
5087f0fb9b28SAditya Pakki 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5088f0fb9b28SAditya Pakki 	if (ret)
5089f0fb9b28SAditya Pakki 		return ret;
5090f0fb9b28SAditya Pakki 
50912ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
50921da177e4SLinus Torvalds 	return 0;
50931da177e4SLinus Torvalds }
50941da177e4SLinus Torvalds 
50957c6bb7d2SDavid Ahern static int zero;
50967c6bb7d2SDavid Ahern static int one = 1;
50977c6bb7d2SDavid Ahern 
5098ed792e28SDavid Ahern static struct ctl_table ipv6_route_table_template[] = {
50991da177e4SLinus Torvalds 	{
51001da177e4SLinus Torvalds 		.procname	=	"flush",
51014990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
51021da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
510389c8b3a1SDave Jones 		.mode		=	0200,
51046d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
51051da177e4SLinus Torvalds 	},
51061da177e4SLinus Torvalds 	{
51071da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
51089a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
51091da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51101da177e4SLinus Torvalds 		.mode		=	0644,
51116d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
51121da177e4SLinus Torvalds 	},
51131da177e4SLinus Torvalds 	{
51141da177e4SLinus Torvalds 		.procname	=	"max_size",
51154990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
51161da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51171da177e4SLinus Torvalds 		.mode		=	0644,
51186d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
51191da177e4SLinus Torvalds 	},
51201da177e4SLinus Torvalds 	{
51211da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
51224990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
51231da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51241da177e4SLinus Torvalds 		.mode		=	0644,
51256d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
51261da177e4SLinus Torvalds 	},
51271da177e4SLinus Torvalds 	{
51281da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
51294990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
51301da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51311da177e4SLinus Torvalds 		.mode		=	0644,
51326d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
51331da177e4SLinus Torvalds 	},
51341da177e4SLinus Torvalds 	{
51351da177e4SLinus Torvalds 		.procname	=	"gc_interval",
51364990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
51371da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51381da177e4SLinus Torvalds 		.mode		=	0644,
51396d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
51401da177e4SLinus Torvalds 	},
51411da177e4SLinus Torvalds 	{
51421da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
51434990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
51441da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51451da177e4SLinus Torvalds 		.mode		=	0644,
5146f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
51471da177e4SLinus Torvalds 	},
51481da177e4SLinus Torvalds 	{
51491da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
51504990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
51511da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51521da177e4SLinus Torvalds 		.mode		=	0644,
51536d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
51541da177e4SLinus Torvalds 	},
51551da177e4SLinus Torvalds 	{
51561da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
51574990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
51581da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51591da177e4SLinus Torvalds 		.mode		=	0644,
5160f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
51611da177e4SLinus Torvalds 	},
51621da177e4SLinus Torvalds 	{
51631da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
51644990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
51651da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
51661da177e4SLinus Torvalds 		.mode		=	0644,
51676d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
51681da177e4SLinus Torvalds 	},
51697c6bb7d2SDavid Ahern 	{
51707c6bb7d2SDavid Ahern 		.procname	=	"skip_notify_on_dev_down",
51717c6bb7d2SDavid Ahern 		.data		=	&init_net.ipv6.sysctl.skip_notify_on_dev_down,
51727c6bb7d2SDavid Ahern 		.maxlen		=	sizeof(int),
51737c6bb7d2SDavid Ahern 		.mode		=	0644,
51747c6bb7d2SDavid Ahern 		.proc_handler	=	proc_dointvec,
51757c6bb7d2SDavid Ahern 		.extra1		=	&zero,
51767c6bb7d2SDavid Ahern 		.extra2		=	&one,
51777c6bb7d2SDavid Ahern 	},
5178f8572d8fSEric W. Biederman 	{ }
51791da177e4SLinus Torvalds };
51801da177e4SLinus Torvalds 
51812c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
5182760f2d01SDaniel Lezcano {
5183760f2d01SDaniel Lezcano 	struct ctl_table *table;
5184760f2d01SDaniel Lezcano 
5185760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
5186760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
5187760f2d01SDaniel Lezcano 			GFP_KERNEL);
51885ee09105SYOSHIFUJI Hideaki 
51895ee09105SYOSHIFUJI Hideaki 	if (table) {
51905ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
5191c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
519286393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
51935ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
51945ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
51955ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
51965ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
51975ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
51985ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
51995ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
52009c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
52017c6bb7d2SDavid Ahern 		table[10].data = &net->ipv6.sysctl.skip_notify_on_dev_down;
5202464dc801SEric W. Biederman 
5203464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
5204464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
5205464dc801SEric W. Biederman 			table[0].procname = NULL;
52065ee09105SYOSHIFUJI Hideaki 	}
52075ee09105SYOSHIFUJI Hideaki 
5208760f2d01SDaniel Lezcano 	return table;
5209760f2d01SDaniel Lezcano }
52101da177e4SLinus Torvalds #endif
52111da177e4SLinus Torvalds 
52122c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
5213cdb18761SDaniel Lezcano {
5214633d424bSPavel Emelyanov 	int ret = -ENOMEM;
52158ed67789SDaniel Lezcano 
521686393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
521786393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
5218f2fc6a54SBenjamin Thery 
5219fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
5220fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
5221fc66f95cSEric Dumazet 
5222421842edSDavid Ahern 	net->ipv6.fib6_null_entry = kmemdup(&fib6_null_entry_template,
5223421842edSDavid Ahern 					    sizeof(*net->ipv6.fib6_null_entry),
5224421842edSDavid Ahern 					    GFP_KERNEL);
5225421842edSDavid Ahern 	if (!net->ipv6.fib6_null_entry)
5226421842edSDavid Ahern 		goto out_ip6_dst_entries;
5227421842edSDavid Ahern 
52288ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
52298ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
52308ed67789SDaniel Lezcano 					   GFP_KERNEL);
52318ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
5232421842edSDavid Ahern 		goto out_fib6_null_entry;
5233d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
523462fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
523562fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
52368ed67789SDaniel Lezcano 
52378ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
5238feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
52398ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
52408ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
52418ed67789SDaniel Lezcano 					       GFP_KERNEL);
524268fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
524368fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
5244d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
524562fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
524662fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
52478ed67789SDaniel Lezcano 
52488ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
52498ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
52508ed67789SDaniel Lezcano 					       GFP_KERNEL);
525168fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
525268fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
5253d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
525462fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
525562fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
52568ed67789SDaniel Lezcano #endif
52578ed67789SDaniel Lezcano 
5258b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
5259b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
5260b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
5261b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
5262b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
5263b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
5264b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
5265b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
52667c6bb7d2SDavid Ahern 	net->ipv6.sysctl.skip_notify_on_dev_down = 0;
5267b339a47cSPeter Zijlstra 
52686891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
52696891a346SBenjamin Thery 
52708ed67789SDaniel Lezcano 	ret = 0;
52718ed67789SDaniel Lezcano out:
52728ed67789SDaniel Lezcano 	return ret;
5273f2fc6a54SBenjamin Thery 
527468fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
527568fffc67SPeter Zijlstra out_ip6_prohibit_entry:
527668fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
527768fffc67SPeter Zijlstra out_ip6_null_entry:
527868fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
527968fffc67SPeter Zijlstra #endif
5280421842edSDavid Ahern out_fib6_null_entry:
5281421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
5282fc66f95cSEric Dumazet out_ip6_dst_entries:
5283fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5284f2fc6a54SBenjamin Thery out_ip6_dst_ops:
5285f2fc6a54SBenjamin Thery 	goto out;
5286cdb18761SDaniel Lezcano }
5287cdb18761SDaniel Lezcano 
52882c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
5289cdb18761SDaniel Lezcano {
5290421842edSDavid Ahern 	kfree(net->ipv6.fib6_null_entry);
52918ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
52928ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
52938ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
52948ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
52958ed67789SDaniel Lezcano #endif
529641bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
5297cdb18761SDaniel Lezcano }
5298cdb18761SDaniel Lezcano 
5299d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
5300d189634eSThomas Graf {
5301d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5302c3506372SChristoph Hellwig 	proc_create_net("ipv6_route", 0, net->proc_net, &ipv6_route_seq_ops,
5303c3506372SChristoph Hellwig 			sizeof(struct ipv6_route_iter));
53043617d949SChristoph Hellwig 	proc_create_net_single("rt6_stats", 0444, net->proc_net,
53053617d949SChristoph Hellwig 			rt6_stats_seq_show, NULL);
5306d189634eSThomas Graf #endif
5307d189634eSThomas Graf 	return 0;
5308d189634eSThomas Graf }
5309d189634eSThomas Graf 
5310d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
5311d189634eSThomas Graf {
5312d189634eSThomas Graf #ifdef CONFIG_PROC_FS
5313ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
5314ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
5315d189634eSThomas Graf #endif
5316d189634eSThomas Graf }
5317d189634eSThomas Graf 
5318cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
5319cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
5320cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
5321cdb18761SDaniel Lezcano };
5322cdb18761SDaniel Lezcano 
5323c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
5324c3426b47SDavid S. Miller {
5325c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
5326c3426b47SDavid S. Miller 
5327c3426b47SDavid S. Miller 	if (!bp)
5328c3426b47SDavid S. Miller 		return -ENOMEM;
5329c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
5330c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
5331c3426b47SDavid S. Miller 	return 0;
5332c3426b47SDavid S. Miller }
5333c3426b47SDavid S. Miller 
5334c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
5335c3426b47SDavid S. Miller {
5336c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
5337c3426b47SDavid S. Miller 
5338c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
533956a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
5340c3426b47SDavid S. Miller 	kfree(bp);
5341c3426b47SDavid S. Miller }
5342c3426b47SDavid S. Miller 
53432b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
5344c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
5345c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
5346c3426b47SDavid S. Miller };
5347c3426b47SDavid S. Miller 
5348d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
5349d189634eSThomas Graf 	.init = ip6_route_net_init_late,
5350d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
5351d189634eSThomas Graf };
5352d189634eSThomas Graf 
53538ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
53548ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
5355242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
53568ed67789SDaniel Lezcano };
53578ed67789SDaniel Lezcano 
53582f460933SWANG Cong void __init ip6_route_init_special_entries(void)
53592f460933SWANG Cong {
53602f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
53612f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
53622f460933SWANG Cong 	 * manually for init_net */
5363ad1601aeSDavid Ahern 	init_net.ipv6.fib6_null_entry->fib6_nh.fib_nh_dev = init_net.loopback_dev;
53642f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
53652f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
53662f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
53672f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
53682f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
53692f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
53702f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
53712f460933SWANG Cong   #endif
53722f460933SWANG Cong }
53732f460933SWANG Cong 
5374433d49c3SDaniel Lezcano int __init ip6_route_init(void)
53751da177e4SLinus Torvalds {
5376433d49c3SDaniel Lezcano 	int ret;
53778d0b94afSMartin KaFai Lau 	int cpu;
5378433d49c3SDaniel Lezcano 
53799a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
53809a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
53819a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
53829a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
53839a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
5384c19a28e1SFernando Carrijo 		goto out;
538514e50e57SDavid S. Miller 
5386fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
53878ed67789SDaniel Lezcano 	if (ret)
5388bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
5389bdb3289fSDaniel Lezcano 
5390c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
5391c3426b47SDavid S. Miller 	if (ret)
5392e8803b6cSDavid S. Miller 		goto out_dst_entries;
53932a0c451aSThomas Graf 
53947e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
53957e52b33bSDavid S. Miller 	if (ret)
53967e52b33bSDavid S. Miller 		goto out_register_inetpeer;
5397c3426b47SDavid S. Miller 
53985dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
53995dc121e9SArnaud Ebalard 
5400e8803b6cSDavid S. Miller 	ret = fib6_init();
5401433d49c3SDaniel Lezcano 	if (ret)
54028ed67789SDaniel Lezcano 		goto out_register_subsys;
5403433d49c3SDaniel Lezcano 
5404433d49c3SDaniel Lezcano 	ret = xfrm6_init();
5405433d49c3SDaniel Lezcano 	if (ret)
5406e8803b6cSDavid S. Miller 		goto out_fib6_init;
5407c35b7e72SDaniel Lezcano 
5408433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
5409433d49c3SDaniel Lezcano 	if (ret)
5410433d49c3SDaniel Lezcano 		goto xfrm6_init;
54117e5449c2SDaniel Lezcano 
5412d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
5413d189634eSThomas Graf 	if (ret)
5414d189634eSThomas Graf 		goto fib6_rules_init;
5415d189634eSThomas Graf 
541616feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
541716feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
541816feebcfSFlorian Westphal 	if (ret < 0)
541916feebcfSFlorian Westphal 		goto out_register_late_subsys;
542016feebcfSFlorian Westphal 
542116feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
542216feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
542316feebcfSFlorian Westphal 	if (ret < 0)
542416feebcfSFlorian Westphal 		goto out_register_late_subsys;
542516feebcfSFlorian Westphal 
542616feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
542716feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
542816feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
542916feebcfSFlorian Westphal 	if (ret < 0)
5430d189634eSThomas Graf 		goto out_register_late_subsys;
5431433d49c3SDaniel Lezcano 
54328ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
5433cdb18761SDaniel Lezcano 	if (ret)
5434d189634eSThomas Graf 		goto out_register_late_subsys;
54358ed67789SDaniel Lezcano 
54368d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
54378d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
54388d0b94afSMartin KaFai Lau 
54398d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
54408d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
54418d0b94afSMartin KaFai Lau 	}
54428d0b94afSMartin KaFai Lau 
5443433d49c3SDaniel Lezcano out:
5444433d49c3SDaniel Lezcano 	return ret;
5445433d49c3SDaniel Lezcano 
5446d189634eSThomas Graf out_register_late_subsys:
544716feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
5448d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5449433d49c3SDaniel Lezcano fib6_rules_init:
5450433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
5451433d49c3SDaniel Lezcano xfrm6_init:
5452433d49c3SDaniel Lezcano 	xfrm6_fini();
54532a0c451aSThomas Graf out_fib6_init:
54542a0c451aSThomas Graf 	fib6_gc_cleanup();
54558ed67789SDaniel Lezcano out_register_subsys:
54568ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
54577e52b33bSDavid S. Miller out_register_inetpeer:
54587e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
5459fc66f95cSEric Dumazet out_dst_entries:
5460fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5461433d49c3SDaniel Lezcano out_kmem_cache:
5462f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
5463433d49c3SDaniel Lezcano 	goto out;
54641da177e4SLinus Torvalds }
54651da177e4SLinus Torvalds 
54661da177e4SLinus Torvalds void ip6_route_cleanup(void)
54671da177e4SLinus Torvalds {
54688ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
5469d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
5470101367c2SThomas Graf 	fib6_rules_cleanup();
54711da177e4SLinus Torvalds 	xfrm6_fini();
54721da177e4SLinus Torvalds 	fib6_gc_cleanup();
5473c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
54748ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
547541bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
5476f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
54771da177e4SLinus Torvalds }
5478