xref: /openbmc/linux/net/ipv6/route.c (revision 8067bb8c1d3599e137dee445d65b64db90ebc6f5)
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>
66b811580dSDavid Ahern #include <trace/events/fib6.h>
671da177e4SLinus Torvalds 
687c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
711da177e4SLinus Torvalds #include <linux/sysctl.h>
721da177e4SLinus Torvalds #endif
731da177e4SLinus Torvalds 
74afc154e9SHannes Frederic Sowa enum rt6_nud_state {
757e980569SJiri Benc 	RT6_NUD_FAIL_HARD = -3,
767e980569SJiri Benc 	RT6_NUD_FAIL_PROBE = -2,
777e980569SJiri Benc 	RT6_NUD_FAIL_DO_RR = -1,
78afc154e9SHannes Frederic Sowa 	RT6_NUD_SUCCEED = 1
79afc154e9SHannes Frederic Sowa };
80afc154e9SHannes Frederic Sowa 
8183a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort);
821da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
830dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
84ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
851da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
861da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
871da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
881da177e4SLinus Torvalds 				       struct net_device *dev, int how);
89569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
92ede2059dSEric W. Biederman static int		ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
937150aedeSKamala R static int		ip6_pkt_prohibit(struct sk_buff *skb);
94ede2059dSEric W. Biederman static int		ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
951da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
966700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
976700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
986700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
996700c270SDavid S. Miller 					struct sk_buff *skb);
1004b32b5adSMartin KaFai Lau static void		rt6_dst_from_metrics_check(struct rt6_info *rt);
10152bd4c0cSNicolas Dichtel static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
10216a16cd3SDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt);
10316a16cd3SDavid Ahern static int rt6_fill_node(struct net *net,
10416a16cd3SDavid Ahern 			 struct sk_buff *skb, struct rt6_info *rt,
10516a16cd3SDavid Ahern 			 struct in6_addr *dst, struct in6_addr *src,
10616a16cd3SDavid Ahern 			 int iif, int type, u32 portid, u32 seq,
10716a16cd3SDavid Ahern 			 unsigned int flags);
10835732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt,
10935732d01SWei Wang 					   struct in6_addr *daddr,
11035732d01SWei Wang 					   struct in6_addr *saddr);
1111da177e4SLinus Torvalds 
11270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
113efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
114b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
115830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
116830218c1SDavid Ahern 					   struct net_device *dev,
11795c96174SEric Dumazet 					   unsigned int pref);
118efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_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);
12270ceb4f5SYOSHIFUJI Hideaki #endif
12370ceb4f5SYOSHIFUJI Hideaki 
1248d0b94afSMartin KaFai Lau struct uncached_list {
1258d0b94afSMartin KaFai Lau 	spinlock_t		lock;
1268d0b94afSMartin KaFai Lau 	struct list_head	head;
1278d0b94afSMartin KaFai Lau };
1288d0b94afSMartin KaFai Lau 
1298d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list);
1308d0b94afSMartin KaFai Lau 
1318d0b94afSMartin KaFai Lau static void rt6_uncached_list_add(struct rt6_info *rt)
1328d0b94afSMartin KaFai Lau {
1338d0b94afSMartin KaFai Lau 	struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list);
1348d0b94afSMartin KaFai Lau 
1358d0b94afSMartin KaFai Lau 	rt->rt6i_uncached_list = ul;
1368d0b94afSMartin KaFai Lau 
1378d0b94afSMartin KaFai Lau 	spin_lock_bh(&ul->lock);
1388d0b94afSMartin KaFai Lau 	list_add_tail(&rt->rt6i_uncached, &ul->head);
1398d0b94afSMartin KaFai Lau 	spin_unlock_bh(&ul->lock);
1408d0b94afSMartin KaFai Lau }
1418d0b94afSMartin KaFai Lau 
1428d0b94afSMartin KaFai Lau static void rt6_uncached_list_del(struct rt6_info *rt)
1438d0b94afSMartin KaFai Lau {
1448d0b94afSMartin KaFai Lau 	if (!list_empty(&rt->rt6i_uncached)) {
1458d0b94afSMartin KaFai Lau 		struct uncached_list *ul = rt->rt6i_uncached_list;
14681eb8447SWei Wang 		struct net *net = dev_net(rt->dst.dev);
1478d0b94afSMartin KaFai Lau 
1488d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1498d0b94afSMartin KaFai Lau 		list_del(&rt->rt6i_uncached);
15081eb8447SWei Wang 		atomic_dec(&net->ipv6.rt6_stats->fib_rt_uncache);
1518d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1528d0b94afSMartin KaFai Lau 	}
1538d0b94afSMartin KaFai Lau }
1548d0b94afSMartin KaFai Lau 
1558d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
1568d0b94afSMartin KaFai Lau {
1578d0b94afSMartin KaFai Lau 	struct net_device *loopback_dev = net->loopback_dev;
1588d0b94afSMartin KaFai Lau 	int cpu;
1598d0b94afSMartin KaFai Lau 
160e332bc67SEric W. Biederman 	if (dev == loopback_dev)
161e332bc67SEric W. Biederman 		return;
162e332bc67SEric W. Biederman 
1638d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
1648d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
1658d0b94afSMartin KaFai Lau 		struct rt6_info *rt;
1668d0b94afSMartin KaFai Lau 
1678d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1688d0b94afSMartin KaFai Lau 		list_for_each_entry(rt, &ul->head, rt6i_uncached) {
1698d0b94afSMartin KaFai Lau 			struct inet6_dev *rt_idev = rt->rt6i_idev;
1708d0b94afSMartin KaFai Lau 			struct net_device *rt_dev = rt->dst.dev;
1718d0b94afSMartin KaFai Lau 
172e332bc67SEric W. Biederman 			if (rt_idev->dev == dev) {
1738d0b94afSMartin KaFai Lau 				rt->rt6i_idev = in6_dev_get(loopback_dev);
1748d0b94afSMartin KaFai Lau 				in6_dev_put(rt_idev);
1758d0b94afSMartin KaFai Lau 			}
1768d0b94afSMartin KaFai Lau 
177e332bc67SEric W. Biederman 			if (rt_dev == dev) {
1788d0b94afSMartin KaFai Lau 				rt->dst.dev = loopback_dev;
1798d0b94afSMartin KaFai Lau 				dev_hold(rt->dst.dev);
1808d0b94afSMartin KaFai Lau 				dev_put(rt_dev);
1818d0b94afSMartin KaFai Lau 			}
1828d0b94afSMartin KaFai Lau 		}
1838d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1848d0b94afSMartin KaFai Lau 	}
1858d0b94afSMartin KaFai Lau }
1868d0b94afSMartin KaFai Lau 
187d52d3997SMartin KaFai Lau static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt)
188d52d3997SMartin KaFai Lau {
1893a2232e9SDavid Miller 	return dst_metrics_write_ptr(&rt->from->dst);
190d52d3997SMartin KaFai Lau }
191d52d3997SMartin KaFai Lau 
19206582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
19306582540SDavid S. Miller {
19406582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *)dst;
19506582540SDavid S. Miller 
196d52d3997SMartin KaFai Lau 	if (rt->rt6i_flags & RTF_PCPU)
197d52d3997SMartin KaFai Lau 		return rt6_pcpu_cow_metrics(rt);
198d52d3997SMartin KaFai Lau 	else if (rt->rt6i_flags & RTF_CACHE)
1994b32b5adSMartin KaFai Lau 		return NULL;
2004b32b5adSMartin KaFai Lau 	else
2013b471175SMartin KaFai Lau 		return dst_cow_metrics_generic(dst, old);
20206582540SDavid S. Miller }
20306582540SDavid S. Miller 
204f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt,
205f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
206f894cbf8SDavid S. Miller 					     const void *daddr)
20739232973SDavid S. Miller {
20839232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
20939232973SDavid S. Miller 
210a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
21139232973SDavid S. Miller 		return (const void *) p;
212f894cbf8SDavid S. Miller 	else if (skb)
213f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
21439232973SDavid S. Miller 	return daddr;
21539232973SDavid S. Miller }
21639232973SDavid S. Miller 
217f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
218f894cbf8SDavid S. Miller 					  struct sk_buff *skb,
219f894cbf8SDavid S. Miller 					  const void *daddr)
220d3aaeb38SDavid S. Miller {
22139232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
22239232973SDavid S. Miller 	struct neighbour *n;
22339232973SDavid S. Miller 
224f894cbf8SDavid S. Miller 	daddr = choose_neigh_daddr(rt, skb, daddr);
2258e022ee6SYOSHIFUJI Hideaki / 吉藤英明 	n = __ipv6_neigh_lookup(dst->dev, daddr);
226f83c7790SDavid S. Miller 	if (n)
227f83c7790SDavid S. Miller 		return n;
228f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
229f83c7790SDavid S. Miller }
230f83c7790SDavid S. Miller 
23163fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
23263fca65dSJulian Anastasov {
23363fca65dSJulian Anastasov 	struct net_device *dev = dst->dev;
23463fca65dSJulian Anastasov 	struct rt6_info *rt = (struct rt6_info *)dst;
23563fca65dSJulian Anastasov 
23663fca65dSJulian Anastasov 	daddr = choose_neigh_daddr(rt, NULL, daddr);
23763fca65dSJulian Anastasov 	if (!daddr)
23863fca65dSJulian Anastasov 		return;
23963fca65dSJulian Anastasov 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
24063fca65dSJulian Anastasov 		return;
24163fca65dSJulian Anastasov 	if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
24263fca65dSJulian Anastasov 		return;
24363fca65dSJulian Anastasov 	__ipv6_confirm_neigh(dev, daddr);
24463fca65dSJulian Anastasov }
24563fca65dSJulian Anastasov 
2469a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
2471da177e4SLinus Torvalds 	.family			=	AF_INET6,
2481da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
2491da177e4SLinus Torvalds 	.gc_thresh		=	1024,
2501da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
2510dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
252ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
25306582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
2541da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
2551da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
2561da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
2571da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
2581da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
2596e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
2609f8955ccSEric W. Biederman 	.local_out		=	__ip6_local_out,
261d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
26263fca65dSJulian Anastasov 	.confirm_neigh		=	ip6_confirm_neigh,
2631da177e4SLinus Torvalds };
2641da177e4SLinus Torvalds 
265ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
266ec831ea7SRoland Dreier {
267618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
268618f9bc7SSteffen Klassert 
269618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
270ec831ea7SRoland Dreier }
271ec831ea7SRoland Dreier 
2726700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
2736700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
27414e50e57SDavid S. Miller {
27514e50e57SDavid S. Miller }
27614e50e57SDavid S. Miller 
2776700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
2786700c270SDavid S. Miller 				      struct sk_buff *skb)
279b587ee3bSDavid S. Miller {
280b587ee3bSDavid S. Miller }
281b587ee3bSDavid S. Miller 
28214e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
28314e50e57SDavid S. Miller 	.family			=	AF_INET6,
28414e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
28514e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
286ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
287214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
28814e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
289b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2900a1f5962SMartin KaFai Lau 	.cow_metrics		=	dst_cow_metrics_generic,
291d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
29214e50e57SDavid S. Miller };
29314e50e57SDavid S. Miller 
29462fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
29514edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
29662fa8a84SDavid S. Miller };
29762fa8a84SDavid S. Miller 
298fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
2991da177e4SLinus Torvalds 	.dst = {
3001da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
3011da177e4SLinus Torvalds 		.__use		= 1,
3022c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
3031da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
3041da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
3051da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
3061da177e4SLinus Torvalds 	},
3071da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3084f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
3091da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
3101da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
3111da177e4SLinus Torvalds };
3121da177e4SLinus Torvalds 
313101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
314101367c2SThomas Graf 
315fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
316101367c2SThomas Graf 	.dst = {
317101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
318101367c2SThomas Graf 		.__use		= 1,
3192c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
320101367c2SThomas Graf 		.error		= -EACCES,
3219ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
3229ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
323101367c2SThomas Graf 	},
324101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3254f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
326101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
327101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
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),
3404f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
341101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
342101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
343101367c2SThomas Graf };
344101367c2SThomas Graf 
345101367c2SThomas Graf #endif
346101367c2SThomas Graf 
347ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt)
348ebfa45f0SMartin KaFai Lau {
349ebfa45f0SMartin KaFai Lau 	struct dst_entry *dst = &rt->dst;
350ebfa45f0SMartin KaFai Lau 
351ebfa45f0SMartin KaFai Lau 	memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
352ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_siblings);
353ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_uncached);
354ebfa45f0SMartin KaFai Lau }
355ebfa45f0SMartin KaFai Lau 
3561da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
357d52d3997SMartin KaFai Lau static struct rt6_info *__ip6_dst_alloc(struct net *net,
358957c665fSDavid S. Miller 					struct net_device *dev,
359ad706862SMartin KaFai Lau 					int flags)
3601da177e4SLinus Torvalds {
36197bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
362b2a9c0edSWei Wang 					1, DST_OBSOLETE_FORCE_CHK, flags);
363cf911662SDavid S. Miller 
36481eb8447SWei Wang 	if (rt) {
365ebfa45f0SMartin KaFai Lau 		rt6_info_init(rt);
36681eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
36781eb8447SWei Wang 	}
3688104891bSSteffen Klassert 
369cf911662SDavid S. Miller 	return rt;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3729ab179d8SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net,
373d52d3997SMartin KaFai Lau 			       struct net_device *dev,
374ad706862SMartin KaFai Lau 			       int flags)
375d52d3997SMartin KaFai Lau {
376ad706862SMartin KaFai Lau 	struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags);
377d52d3997SMartin KaFai Lau 
378d52d3997SMartin KaFai Lau 	if (rt) {
379d52d3997SMartin KaFai Lau 		rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC);
380bfd8e5a4SEric Dumazet 		if (!rt->rt6i_pcpu) {
381587fea74SWei Wang 			dst_release_immediate(&rt->dst);
382d52d3997SMartin KaFai Lau 			return NULL;
383d52d3997SMartin KaFai Lau 		}
384d52d3997SMartin KaFai Lau 	}
385d52d3997SMartin KaFai Lau 
386d52d3997SMartin KaFai Lau 	return rt;
387d52d3997SMartin KaFai Lau }
3889ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc);
389d52d3997SMartin KaFai Lau 
3901da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3911da177e4SLinus Torvalds {
3921da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
39335732d01SWei Wang 	struct rt6_exception_bucket *bucket;
3943a2232e9SDavid Miller 	struct rt6_info *from = rt->from;
3958d0b94afSMartin KaFai Lau 	struct inet6_dev *idev;
3961da177e4SLinus Torvalds 
3978e2ec639SYan, Zheng 	dst_destroy_metrics_generic(dst);
398d52d3997SMartin KaFai Lau 	free_percpu(rt->rt6i_pcpu);
3998d0b94afSMartin KaFai Lau 	rt6_uncached_list_del(rt);
4008d0b94afSMartin KaFai Lau 
4018d0b94afSMartin KaFai Lau 	idev = rt->rt6i_idev;
40238308473SDavid S. Miller 	if (idev) {
4031da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
4041da177e4SLinus Torvalds 		in6_dev_put(idev);
4051da177e4SLinus Torvalds 	}
40635732d01SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1);
40735732d01SWei Wang 	if (bucket) {
40835732d01SWei Wang 		rt->rt6i_exception_bucket = NULL;
40935732d01SWei Wang 		kfree(bucket);
41035732d01SWei Wang 	}
4111716a961SGao feng 
4123a2232e9SDavid Miller 	rt->from = NULL;
4133a2232e9SDavid Miller 	dst_release(&from->dst);
414b3419363SDavid S. Miller }
415b3419363SDavid S. Miller 
4161da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
4171da177e4SLinus Torvalds 			   int how)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
4201da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
4215a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
422c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
4231da177e4SLinus Torvalds 
424e5645f51SWei Wang 	if (idev && idev->dev != loopback_dev) {
425e5645f51SWei Wang 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
42638308473SDavid S. Miller 		if (loopback_idev) {
4271da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
4281da177e4SLinus Torvalds 			in6_dev_put(idev);
4291da177e4SLinus Torvalds 		}
4301da177e4SLinus Torvalds 	}
43197cac082SDavid S. Miller }
4321da177e4SLinus Torvalds 
4335973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt)
4345973fb1eSMartin KaFai Lau {
4355973fb1eSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_EXPIRES)
4365973fb1eSMartin KaFai Lau 		return time_after(jiffies, rt->dst.expires);
4375973fb1eSMartin KaFai Lau 	else
4385973fb1eSMartin KaFai Lau 		return false;
4395973fb1eSMartin KaFai Lau }
4405973fb1eSMartin KaFai Lau 
441a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
4421da177e4SLinus Torvalds {
4431716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
4441716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
445a50feda5SEric Dumazet 			return true;
4463a2232e9SDavid Miller 	} else if (rt->from) {
4471e2ea8adSXin Long 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
4483a2232e9SDavid Miller 			rt6_check_expired(rt->from);
4491716a961SGao feng 	}
450a50feda5SEric Dumazet 	return false;
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
45351ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
45452bd4c0cSNicolas Dichtel 					     struct flowi6 *fl6, int oif,
45552bd4c0cSNicolas Dichtel 					     int strict)
45651ebd318SNicolas Dichtel {
45751ebd318SNicolas Dichtel 	struct rt6_info *sibling, *next_sibling;
45851ebd318SNicolas Dichtel 	int route_choosen;
45951ebd318SNicolas Dichtel 
460b673d6ccSJakub Sitnicki 	/* We might have already computed the hash for ICMPv6 errors. In such
461b673d6ccSJakub Sitnicki 	 * case it will always be non-zero. Otherwise now is the time to do it.
462b673d6ccSJakub Sitnicki 	 */
463b673d6ccSJakub Sitnicki 	if (!fl6->mp_hash)
464b673d6ccSJakub Sitnicki 		fl6->mp_hash = rt6_multipath_hash(fl6, NULL);
465b673d6ccSJakub Sitnicki 
466b673d6ccSJakub Sitnicki 	route_choosen = fl6->mp_hash % (match->rt6i_nsiblings + 1);
46751ebd318SNicolas Dichtel 	/* Don't change the route, if route_choosen == 0
46851ebd318SNicolas Dichtel 	 * (siblings does not include ourself)
46951ebd318SNicolas Dichtel 	 */
47051ebd318SNicolas Dichtel 	if (route_choosen)
47151ebd318SNicolas Dichtel 		list_for_each_entry_safe(sibling, next_sibling,
47251ebd318SNicolas Dichtel 				&match->rt6i_siblings, rt6i_siblings) {
47351ebd318SNicolas Dichtel 			route_choosen--;
47451ebd318SNicolas Dichtel 			if (route_choosen == 0) {
475bbfcd776SIdo Schimmel 				struct inet6_dev *idev = sibling->rt6i_idev;
476bbfcd776SIdo Schimmel 
477*8067bb8cSIdo Schimmel 				if (sibling->rt6i_nh_flags & RTNH_F_DEAD)
478*8067bb8cSIdo Schimmel 					break;
47914c5206cSIdo Schimmel 				if (sibling->rt6i_nh_flags & RTNH_F_LINKDOWN &&
480bbfcd776SIdo Schimmel 				    idev->cnf.ignore_routes_with_linkdown)
481bbfcd776SIdo Schimmel 					break;
48252bd4c0cSNicolas Dichtel 				if (rt6_score_route(sibling, oif, strict) < 0)
48352bd4c0cSNicolas Dichtel 					break;
48451ebd318SNicolas Dichtel 				match = sibling;
48551ebd318SNicolas Dichtel 				break;
48651ebd318SNicolas Dichtel 			}
48751ebd318SNicolas Dichtel 		}
48851ebd318SNicolas Dichtel 	return match;
48951ebd318SNicolas Dichtel }
49051ebd318SNicolas Dichtel 
4911da177e4SLinus Torvalds /*
49266f5d6ceSWei Wang  *	Route lookup. rcu_read_lock() should be held.
4931da177e4SLinus Torvalds  */
4941da177e4SLinus Torvalds 
4958ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
4968ed67789SDaniel Lezcano 						    struct rt6_info *rt,
497b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4981da177e4SLinus Torvalds 						    int oif,
499d420895eSYOSHIFUJI Hideaki 						    int flags)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
5021da177e4SLinus Torvalds 	struct rt6_info *sprt;
5031da177e4SLinus Torvalds 
504*8067bb8cSIdo Schimmel 	if (!oif && ipv6_addr_any(saddr) && !(rt->rt6i_nh_flags & RTNH_F_DEAD))
505*8067bb8cSIdo Schimmel 		return rt;
506dd3abc4eSYOSHIFUJI Hideaki 
507071fb37eSDavid Miller 	for (sprt = rt; sprt; sprt = rcu_dereference(sprt->rt6_next)) {
508d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
509dd3abc4eSYOSHIFUJI Hideaki 
510*8067bb8cSIdo Schimmel 		if (sprt->rt6i_nh_flags & RTNH_F_DEAD)
511*8067bb8cSIdo Schimmel 			continue;
512*8067bb8cSIdo Schimmel 
513dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
5141da177e4SLinus Torvalds 			if (dev->ifindex == oif)
5151da177e4SLinus Torvalds 				return sprt;
5161da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
51738308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
5181da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
51917fb0b2bSDavid Ahern 					if (flags & RT6_LOOKUP_F_IFACE)
5201da177e4SLinus Torvalds 						continue;
52117fb0b2bSDavid Ahern 					if (local &&
52217fb0b2bSDavid Ahern 					    local->rt6i_idev->dev->ifindex == oif)
5231da177e4SLinus Torvalds 						continue;
5241da177e4SLinus Torvalds 				}
5251da177e4SLinus Torvalds 				local = sprt;
5261da177e4SLinus Torvalds 			}
527dd3abc4eSYOSHIFUJI Hideaki 		} else {
528dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
529dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
530dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
531dd3abc4eSYOSHIFUJI Hideaki 		}
5321da177e4SLinus Torvalds 	}
5331da177e4SLinus Torvalds 
534dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
5351da177e4SLinus Torvalds 		if (local)
5361da177e4SLinus Torvalds 			return local;
5371da177e4SLinus Torvalds 
538d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
5398ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
5401da177e4SLinus Torvalds 	}
541*8067bb8cSIdo Schimmel 
542*8067bb8cSIdo Schimmel 	return rt->rt6i_nh_flags & RTNH_F_DEAD ? net->ipv6.ip6_null_entry : rt;
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
54527097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
546c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
547c2f17e82SHannes Frederic Sowa 	struct work_struct work;
548c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
549c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
550c2f17e82SHannes Frederic Sowa };
551c2f17e82SHannes Frederic Sowa 
552c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
553c2f17e82SHannes Frederic Sowa {
554c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
555c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
556c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
557c2f17e82SHannes Frederic Sowa 
558c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
559adc176c5SErik Nordmark 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
560c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
561662f5533SMichael Büsch 	kfree(work);
562c2f17e82SHannes Frederic Sowa }
563c2f17e82SHannes Frederic Sowa 
56427097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
56527097255SYOSHIFUJI Hideaki {
566990edb42SMartin KaFai Lau 	struct __rt6_probe_work *work;
567f2c31e32SEric Dumazet 	struct neighbour *neigh;
56827097255SYOSHIFUJI Hideaki 	/*
56927097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
57027097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
57127097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
57227097255SYOSHIFUJI Hideaki 	 *
57327097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
57427097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
57527097255SYOSHIFUJI Hideaki 	 */
5762152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
577fdd6681dSAmerigo Wang 		return;
5782152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
5792152caeaSYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
5802152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
5818d6c31bfSMartin KaFai Lau 		if (neigh->nud_state & NUD_VALID)
5828d6c31bfSMartin KaFai Lau 			goto out;
5838d6c31bfSMartin KaFai Lau 
584990edb42SMartin KaFai Lau 		work = NULL;
5852152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
586990edb42SMartin KaFai Lau 		if (!(neigh->nud_state & NUD_VALID) &&
587990edb42SMartin KaFai Lau 		    time_after(jiffies,
588990edb42SMartin KaFai Lau 			       neigh->updated +
589990edb42SMartin KaFai Lau 			       rt->rt6i_idev->cnf.rtr_probe_interval)) {
590c2f17e82SHannes Frederic Sowa 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
591990edb42SMartin KaFai Lau 			if (work)
5927e980569SJiri Benc 				__neigh_set_probe_once(neigh);
593990edb42SMartin KaFai Lau 		}
594c2f17e82SHannes Frederic Sowa 		write_unlock(&neigh->lock);
595990edb42SMartin KaFai Lau 	} else {
596990edb42SMartin KaFai Lau 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
597990edb42SMartin KaFai Lau 	}
598c2f17e82SHannes Frederic Sowa 
599c2f17e82SHannes Frederic Sowa 	if (work) {
600c2f17e82SHannes Frederic Sowa 		INIT_WORK(&work->work, rt6_probe_deferred);
601c2f17e82SHannes Frederic Sowa 		work->target = rt->rt6i_gateway;
602c2f17e82SHannes Frederic Sowa 		dev_hold(rt->dst.dev);
603c2f17e82SHannes Frederic Sowa 		work->dev = rt->dst.dev;
604c2f17e82SHannes Frederic Sowa 		schedule_work(&work->work);
605c2f17e82SHannes Frederic Sowa 	}
606990edb42SMartin KaFai Lau 
6078d6c31bfSMartin KaFai Lau out:
6082152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
609f2c31e32SEric Dumazet }
61027097255SYOSHIFUJI Hideaki #else
61127097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
61227097255SYOSHIFUJI Hideaki {
61327097255SYOSHIFUJI Hideaki }
61427097255SYOSHIFUJI Hideaki #endif
61527097255SYOSHIFUJI Hideaki 
6161da177e4SLinus Torvalds /*
617554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
6181da177e4SLinus Torvalds  */
619b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
6201da177e4SLinus Torvalds {
621d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
622161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
623554cfb7eSYOSHIFUJI Hideaki 		return 2;
624161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
625161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
626161980f4SDavid S. Miller 		return 1;
627554cfb7eSYOSHIFUJI Hideaki 	return 0;
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
630afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
6311da177e4SLinus Torvalds {
632f2c31e32SEric Dumazet 	struct neighbour *neigh;
633afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
634f2c31e32SEric Dumazet 
6354d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
6364d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
637afc154e9SHannes Frederic Sowa 		return RT6_NUD_SUCCEED;
638145a3621SYOSHIFUJI Hideaki / 吉藤英明 
639145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
640145a3621SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
641145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
642145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
643554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
644afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
645398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
646a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
647afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
6487e980569SJiri Benc 		else
6497e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
650398bcbebSYOSHIFUJI Hideaki #endif
651145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
652afc154e9SHannes Frederic Sowa 	} else {
653afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
6547e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
655a5a81f0bSPaul Marks 	}
656145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
657145a3621SYOSHIFUJI Hideaki / 吉藤英明 
658a5a81f0bSPaul Marks 	return ret;
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
661554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
662554cfb7eSYOSHIFUJI Hideaki 			   int strict)
663554cfb7eSYOSHIFUJI Hideaki {
664a5a81f0bSPaul Marks 	int m;
6654d0c5911SYOSHIFUJI Hideaki 
6664d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
66777d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
668afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
669ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
670ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
671ebacaaa0SYOSHIFUJI Hideaki #endif
672afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE) {
673afc154e9SHannes Frederic Sowa 		int n = rt6_check_neigh(rt);
674afc154e9SHannes Frederic Sowa 		if (n < 0)
675afc154e9SHannes Frederic Sowa 			return n;
676afc154e9SHannes Frederic Sowa 	}
677554cfb7eSYOSHIFUJI Hideaki 	return m;
678554cfb7eSYOSHIFUJI Hideaki }
679554cfb7eSYOSHIFUJI Hideaki 
680f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
681afc154e9SHannes Frederic Sowa 				   int *mpri, struct rt6_info *match,
682afc154e9SHannes Frederic Sowa 				   bool *do_rr)
683554cfb7eSYOSHIFUJI Hideaki {
684554cfb7eSYOSHIFUJI Hideaki 	int m;
685afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
68635103d11SAndy Gospodarek 	struct inet6_dev *idev = rt->rt6i_idev;
68735103d11SAndy Gospodarek 
688*8067bb8cSIdo Schimmel 	if (rt->rt6i_nh_flags & RTNH_F_DEAD)
689*8067bb8cSIdo Schimmel 		goto out;
690*8067bb8cSIdo Schimmel 
69114c5206cSIdo Schimmel 	if (idev->cnf.ignore_routes_with_linkdown &&
69214c5206cSIdo Schimmel 	    rt->rt6i_nh_flags & RTNH_F_LINKDOWN &&
693d5d32e4bSDavid Ahern 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
69435103d11SAndy Gospodarek 		goto out;
695554cfb7eSYOSHIFUJI Hideaki 
696554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
697f11e6659SDavid S. Miller 		goto out;
698554cfb7eSYOSHIFUJI Hideaki 
699554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
7007e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
701afc154e9SHannes Frederic Sowa 		match_do_rr = true;
702afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
7037e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
704f11e6659SDavid S. Miller 		goto out;
7051da177e4SLinus Torvalds 	}
706f11e6659SDavid S. Miller 
707afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
708afc154e9SHannes Frederic Sowa 		rt6_probe(rt);
709afc154e9SHannes Frederic Sowa 
7107e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
711afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
712afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
713afc154e9SHannes Frederic Sowa 		*mpri = m;
714afc154e9SHannes Frederic Sowa 		match = rt;
715afc154e9SHannes Frederic Sowa 	}
716f11e6659SDavid S. Miller out:
717f11e6659SDavid S. Miller 	return match;
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
720f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
7218d1040e8SWei Wang 				     struct rt6_info *leaf,
722f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
723afc154e9SHannes Frederic Sowa 				     u32 metric, int oif, int strict,
724afc154e9SHannes Frederic Sowa 				     bool *do_rr)
725f11e6659SDavid S. Miller {
7269fbdcfafSSteffen Klassert 	struct rt6_info *rt, *match, *cont;
727f11e6659SDavid S. Miller 	int mpri = -1;
728f11e6659SDavid S. Miller 
729f11e6659SDavid S. Miller 	match = NULL;
7309fbdcfafSSteffen Klassert 	cont = NULL;
731071fb37eSDavid Miller 	for (rt = rr_head; rt; rt = rcu_dereference(rt->rt6_next)) {
7329fbdcfafSSteffen Klassert 		if (rt->rt6i_metric != metric) {
7339fbdcfafSSteffen Klassert 			cont = rt;
7349fbdcfafSSteffen Klassert 			break;
7359fbdcfafSSteffen Klassert 		}
7369fbdcfafSSteffen Klassert 
737afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
7389fbdcfafSSteffen Klassert 	}
7399fbdcfafSSteffen Klassert 
74066f5d6ceSWei Wang 	for (rt = leaf; rt && rt != rr_head;
741071fb37eSDavid Miller 	     rt = rcu_dereference(rt->rt6_next)) {
7429fbdcfafSSteffen Klassert 		if (rt->rt6i_metric != metric) {
7439fbdcfafSSteffen Klassert 			cont = rt;
7449fbdcfafSSteffen Klassert 			break;
7459fbdcfafSSteffen Klassert 		}
7469fbdcfafSSteffen Klassert 
7479fbdcfafSSteffen Klassert 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
7489fbdcfafSSteffen Klassert 	}
7499fbdcfafSSteffen Klassert 
7509fbdcfafSSteffen Klassert 	if (match || !cont)
7519fbdcfafSSteffen Klassert 		return match;
7529fbdcfafSSteffen Klassert 
753071fb37eSDavid Miller 	for (rt = cont; rt; rt = rcu_dereference(rt->rt6_next))
754afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
755f11e6659SDavid S. Miller 
756f11e6659SDavid S. Miller 	return match;
757f11e6659SDavid S. Miller }
758f11e6659SDavid S. Miller 
7598d1040e8SWei Wang static struct rt6_info *rt6_select(struct net *net, struct fib6_node *fn,
7608d1040e8SWei Wang 				   int oif, int strict)
761f11e6659SDavid S. Miller {
76266f5d6ceSWei Wang 	struct rt6_info *leaf = rcu_dereference(fn->leaf);
763f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
764afc154e9SHannes Frederic Sowa 	bool do_rr = false;
76517ecf590SWei Wang 	int key_plen;
766f11e6659SDavid S. Miller 
76787b1af8dSWei Wang 	if (!leaf || leaf == net->ipv6.ip6_null_entry)
7688d1040e8SWei Wang 		return net->ipv6.ip6_null_entry;
7698d1040e8SWei Wang 
77066f5d6ceSWei Wang 	rt0 = rcu_dereference(fn->rr_ptr);
771f11e6659SDavid S. Miller 	if (!rt0)
77266f5d6ceSWei Wang 		rt0 = leaf;
773f11e6659SDavid S. Miller 
77417ecf590SWei Wang 	/* Double check to make sure fn is not an intermediate node
77517ecf590SWei Wang 	 * and fn->leaf does not points to its child's leaf
77617ecf590SWei Wang 	 * (This might happen if all routes under fn are deleted from
77717ecf590SWei Wang 	 * the tree and fib6_repair_tree() is called on the node.)
77817ecf590SWei Wang 	 */
77917ecf590SWei Wang 	key_plen = rt0->rt6i_dst.plen;
78017ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES
78117ecf590SWei Wang 	if (rt0->rt6i_src.plen)
78217ecf590SWei Wang 		key_plen = rt0->rt6i_src.plen;
78317ecf590SWei Wang #endif
78417ecf590SWei Wang 	if (fn->fn_bit != key_plen)
78517ecf590SWei Wang 		return net->ipv6.ip6_null_entry;
78617ecf590SWei Wang 
7878d1040e8SWei Wang 	match = find_rr_leaf(fn, leaf, rt0, rt0->rt6i_metric, oif, strict,
788afc154e9SHannes Frederic Sowa 			     &do_rr);
789f11e6659SDavid S. Miller 
790afc154e9SHannes Frederic Sowa 	if (do_rr) {
791071fb37eSDavid Miller 		struct rt6_info *next = rcu_dereference(rt0->rt6_next);
792f11e6659SDavid S. Miller 
793554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
794f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
7958d1040e8SWei Wang 			next = leaf;
796f11e6659SDavid S. Miller 
79766f5d6ceSWei Wang 		if (next != rt0) {
79866f5d6ceSWei Wang 			spin_lock_bh(&leaf->rt6i_table->tb6_lock);
79966f5d6ceSWei Wang 			/* make sure next is not being deleted from the tree */
80066f5d6ceSWei Wang 			if (next->rt6i_node)
80166f5d6ceSWei Wang 				rcu_assign_pointer(fn->rr_ptr, next);
80266f5d6ceSWei Wang 			spin_unlock_bh(&leaf->rt6i_table->tb6_lock);
80366f5d6ceSWei Wang 		}
804554cfb7eSYOSHIFUJI Hideaki 	}
805554cfb7eSYOSHIFUJI Hideaki 
806a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
8071da177e4SLinus Torvalds }
8081da177e4SLinus Torvalds 
8098b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt)
8108b9df265SMartin KaFai Lau {
8118b9df265SMartin KaFai Lau 	return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
8128b9df265SMartin KaFai Lau }
8138b9df265SMartin KaFai Lau 
81470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
81570ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
816b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
81770ceb4f5SYOSHIFUJI Hideaki {
818c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
81970ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
82070ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
82170ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
8224bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
82370ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
82470ceb4f5SYOSHIFUJI Hideaki 
82570ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
82670ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
82770ceb4f5SYOSHIFUJI Hideaki 	}
82870ceb4f5SYOSHIFUJI Hideaki 
82970ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
83070ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
83170ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
83270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
83370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
83470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
83570ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
83670ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
83770ceb4f5SYOSHIFUJI Hideaki 		}
83870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
83970ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
84070ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
84170ceb4f5SYOSHIFUJI Hideaki 		}
84270ceb4f5SYOSHIFUJI Hideaki 	}
84370ceb4f5SYOSHIFUJI Hideaki 
84470ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
84570ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
8463933fc95SJens Rosenboom 		return -EINVAL;
84770ceb4f5SYOSHIFUJI Hideaki 
8484bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
84970ceb4f5SYOSHIFUJI Hideaki 
85070ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
85170ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
85270ceb4f5SYOSHIFUJI Hideaki 	else {
85370ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
85470ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
85570ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
85670ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
85770ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
85870ceb4f5SYOSHIFUJI Hideaki 	}
85970ceb4f5SYOSHIFUJI Hideaki 
860f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
861f104a567SDuan Jiong 		rt = rt6_get_dflt_router(gwaddr, dev);
862f104a567SDuan Jiong 	else
863f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
864830218c1SDavid Ahern 					gwaddr, dev);
86570ceb4f5SYOSHIFUJI Hideaki 
86670ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
867e0a1ad73SThomas Graf 		ip6_del_rt(rt);
86870ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
86970ceb4f5SYOSHIFUJI Hideaki 	}
87070ceb4f5SYOSHIFUJI Hideaki 
87170ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
872830218c1SDavid Ahern 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
873830218c1SDavid Ahern 					dev, pref);
87470ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
87570ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
87670ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
87770ceb4f5SYOSHIFUJI Hideaki 
87870ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
8791716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
8801716a961SGao feng 			rt6_clean_expires(rt);
8811716a961SGao feng 		else
8821716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
8831716a961SGao feng 
88494e187c0SAmerigo Wang 		ip6_rt_put(rt);
88570ceb4f5SYOSHIFUJI Hideaki 	}
88670ceb4f5SYOSHIFUJI Hideaki 	return 0;
88770ceb4f5SYOSHIFUJI Hideaki }
88870ceb4f5SYOSHIFUJI Hideaki #endif
88970ceb4f5SYOSHIFUJI Hideaki 
890a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
891a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
892a3c00e46SMartin KaFai Lau {
89366f5d6ceSWei Wang 	struct fib6_node *pn, *sn;
894a3c00e46SMartin KaFai Lau 	while (1) {
895a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
896a3c00e46SMartin KaFai Lau 			return NULL;
89766f5d6ceSWei Wang 		pn = rcu_dereference(fn->parent);
89866f5d6ceSWei Wang 		sn = FIB6_SUBTREE(pn);
89966f5d6ceSWei Wang 		if (sn && sn != fn)
90066f5d6ceSWei Wang 			fn = fib6_lookup(sn, NULL, saddr);
901a3c00e46SMartin KaFai Lau 		else
902a3c00e46SMartin KaFai Lau 			fn = pn;
903a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
904a3c00e46SMartin KaFai Lau 			return fn;
905a3c00e46SMartin KaFai Lau 	}
906a3c00e46SMartin KaFai Lau }
907c71099acSThomas Graf 
908d3843fe5SWei Wang static bool ip6_hold_safe(struct net *net, struct rt6_info **prt,
909d3843fe5SWei Wang 			  bool null_fallback)
910d3843fe5SWei Wang {
911d3843fe5SWei Wang 	struct rt6_info *rt = *prt;
912d3843fe5SWei Wang 
913d3843fe5SWei Wang 	if (dst_hold_safe(&rt->dst))
914d3843fe5SWei Wang 		return true;
915d3843fe5SWei Wang 	if (null_fallback) {
916d3843fe5SWei Wang 		rt = net->ipv6.ip6_null_entry;
917d3843fe5SWei Wang 		dst_hold(&rt->dst);
918d3843fe5SWei Wang 	} else {
919d3843fe5SWei Wang 		rt = NULL;
920d3843fe5SWei Wang 	}
921d3843fe5SWei Wang 	*prt = rt;
922d3843fe5SWei Wang 	return false;
923d3843fe5SWei Wang }
924d3843fe5SWei Wang 
9258ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
9268ed67789SDaniel Lezcano 					     struct fib6_table *table,
9274c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
9281da177e4SLinus Torvalds {
9292b760fcfSWei Wang 	struct rt6_info *rt, *rt_cache;
9301da177e4SLinus Torvalds 	struct fib6_node *fn;
9311da177e4SLinus Torvalds 
93266f5d6ceSWei Wang 	rcu_read_lock();
9334c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
934c71099acSThomas Graf restart:
93566f5d6ceSWei Wang 	rt = rcu_dereference(fn->leaf);
93666f5d6ceSWei Wang 	if (!rt) {
93766f5d6ceSWei Wang 		rt = net->ipv6.ip6_null_entry;
93866f5d6ceSWei Wang 	} else {
93966f5d6ceSWei Wang 		rt = rt6_device_match(net, rt, &fl6->saddr,
94066f5d6ceSWei Wang 				      fl6->flowi6_oif, flags);
94151ebd318SNicolas Dichtel 		if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
94266f5d6ceSWei Wang 			rt = rt6_multipath_select(rt, fl6,
94366f5d6ceSWei Wang 						  fl6->flowi6_oif, flags);
94466f5d6ceSWei Wang 	}
945a3c00e46SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
946a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
947a3c00e46SMartin KaFai Lau 		if (fn)
948a3c00e46SMartin KaFai Lau 			goto restart;
949a3c00e46SMartin KaFai Lau 	}
9502b760fcfSWei Wang 	/* Search through exception table */
9512b760fcfSWei Wang 	rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr);
9522b760fcfSWei Wang 	if (rt_cache)
9532b760fcfSWei Wang 		rt = rt_cache;
9542b760fcfSWei Wang 
955d3843fe5SWei Wang 	if (ip6_hold_safe(net, &rt, true))
956d3843fe5SWei Wang 		dst_use_noref(&rt->dst, jiffies);
957d3843fe5SWei Wang 
95866f5d6ceSWei Wang 	rcu_read_unlock();
959b811580dSDavid Ahern 
960b65f164dSPaolo Abeni 	trace_fib6_table_lookup(net, rt, table, fl6);
961b811580dSDavid Ahern 
9621da177e4SLinus Torvalds 	return rt;
963c71099acSThomas Graf 
964c71099acSThomas Graf }
965c71099acSThomas Graf 
966ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
967ea6e574eSFlorian Westphal 				    int flags)
968ea6e574eSFlorian Westphal {
969ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
970ea6e574eSFlorian Westphal }
971ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
972ea6e574eSFlorian Westphal 
9739acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
9749acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
975c71099acSThomas Graf {
9764c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9774c9483b2SDavid S. Miller 		.flowi6_oif = oif,
9784c9483b2SDavid S. Miller 		.daddr = *daddr,
979c71099acSThomas Graf 	};
980c71099acSThomas Graf 	struct dst_entry *dst;
98177d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
982c71099acSThomas Graf 
983adaa70bbSThomas Graf 	if (saddr) {
9844c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
985adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
986adaa70bbSThomas Graf 	}
987adaa70bbSThomas Graf 
9884c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
989c71099acSThomas Graf 	if (dst->error == 0)
990c71099acSThomas Graf 		return (struct rt6_info *) dst;
991c71099acSThomas Graf 
992c71099acSThomas Graf 	dst_release(dst);
993c71099acSThomas Graf 
9941da177e4SLinus Torvalds 	return NULL;
9951da177e4SLinus Torvalds }
9967159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
9977159039aSYOSHIFUJI Hideaki 
998c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
9991cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
10001cfb71eeSWei Wang  * route is released.
10011cfb71eeSWei Wang  * Caller must hold dst before calling it.
10021da177e4SLinus Torvalds  */
10031da177e4SLinus Torvalds 
1004e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
1005333c4301SDavid Ahern 			struct mx6_config *mxc,
1006333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
10071da177e4SLinus Torvalds {
10081da177e4SLinus Torvalds 	int err;
1009c71099acSThomas Graf 	struct fib6_table *table;
10101da177e4SLinus Torvalds 
1011c71099acSThomas Graf 	table = rt->rt6i_table;
101266f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
1013333c4301SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, mxc, extack);
101466f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	return err;
10171da177e4SLinus Torvalds }
10181da177e4SLinus Torvalds 
101940e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
102040e22e8fSThomas Graf {
1021e715b6d3SFlorian Westphal 	struct nl_info info = {	.nl_net = dev_net(rt->dst.dev), };
1022e715b6d3SFlorian Westphal 	struct mx6_config mxc = { .mx = NULL, };
1023e715b6d3SFlorian Westphal 
10241cfb71eeSWei Wang 	/* Hold dst to account for the reference from the fib6 tree */
10251cfb71eeSWei Wang 	dst_hold(&rt->dst);
1026333c4301SDavid Ahern 	return __ip6_ins_rt(rt, &info, &mxc, NULL);
102740e22e8fSThomas Graf }
102840e22e8fSThomas Graf 
10294832c30dSDavid Ahern /* called with rcu_lock held */
10304832c30dSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt)
10314832c30dSDavid Ahern {
10324832c30dSDavid Ahern 	struct net_device *dev = rt->dst.dev;
10334832c30dSDavid Ahern 
103498d11291SDavid Ahern 	if (rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) {
10354832c30dSDavid Ahern 		/* for copies of local routes, dst->dev needs to be the
10364832c30dSDavid Ahern 		 * device if it is a master device, the master device if
10374832c30dSDavid Ahern 		 * device is enslaved, and the loopback as the default
10384832c30dSDavid Ahern 		 */
10394832c30dSDavid Ahern 		if (netif_is_l3_slave(dev) &&
10404832c30dSDavid Ahern 		    !rt6_need_strict(&rt->rt6i_dst.addr))
10414832c30dSDavid Ahern 			dev = l3mdev_master_dev_rcu(dev);
10424832c30dSDavid Ahern 		else if (!netif_is_l3_master(dev))
10434832c30dSDavid Ahern 			dev = dev_net(dev)->loopback_dev;
10444832c30dSDavid Ahern 		/* last case is netif_is_l3_master(dev) is true in which
10454832c30dSDavid Ahern 		 * case we want dev returned to be dev
10464832c30dSDavid Ahern 		 */
10474832c30dSDavid Ahern 	}
10484832c30dSDavid Ahern 
10494832c30dSDavid Ahern 	return dev;
10504832c30dSDavid Ahern }
10514832c30dSDavid Ahern 
10528b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort,
105321efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
1054b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
10551da177e4SLinus Torvalds {
10564832c30dSDavid Ahern 	struct net_device *dev;
10571da177e4SLinus Torvalds 	struct rt6_info *rt;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	/*
10601da177e4SLinus Torvalds 	 *	Clone the route.
10611da177e4SLinus Torvalds 	 */
10621da177e4SLinus Torvalds 
1063d52d3997SMartin KaFai Lau 	if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
10643a2232e9SDavid Miller 		ort = ort->from;
10651da177e4SLinus Torvalds 
10664832c30dSDavid Ahern 	rcu_read_lock();
10674832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(ort);
10684832c30dSDavid Ahern 	rt = __ip6_dst_alloc(dev_net(dev), dev, 0);
10694832c30dSDavid Ahern 	rcu_read_unlock();
107083a09abdSMartin KaFai Lau 	if (!rt)
107183a09abdSMartin KaFai Lau 		return NULL;
107283a09abdSMartin KaFai Lau 
107383a09abdSMartin KaFai Lau 	ip6_rt_copy_init(rt, ort);
10748b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
107583a09abdSMartin KaFai Lau 	rt->rt6i_metric = 0;
107683a09abdSMartin KaFai Lau 	rt->dst.flags |= DST_HOST;
107783a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
107883a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
10798b9df265SMartin KaFai Lau 
10808b9df265SMartin KaFai Lau 	if (!rt6_is_gw_or_nonexthop(ort)) {
1081bb3c3686SDavid S. Miller 		if (ort->rt6i_dst.plen != 128 &&
108221efcfa0SEric Dumazet 		    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
108358c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
10841da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
10851da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
10864e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
10871da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
10881da177e4SLinus Torvalds 		}
10891da177e4SLinus Torvalds #endif
109095a9a5baSYOSHIFUJI Hideaki 	}
109195a9a5baSYOSHIFUJI Hideaki 
1092299d9939SYOSHIFUJI Hideaki 	return rt;
1093299d9939SYOSHIFUJI Hideaki }
1094299d9939SYOSHIFUJI Hideaki 
1095d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt)
1096d52d3997SMartin KaFai Lau {
10974832c30dSDavid Ahern 	struct net_device *dev;
1098d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1099d52d3997SMartin KaFai Lau 
11004832c30dSDavid Ahern 	rcu_read_lock();
11014832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(rt);
11024832c30dSDavid Ahern 	pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags);
11034832c30dSDavid Ahern 	rcu_read_unlock();
1104d52d3997SMartin KaFai Lau 	if (!pcpu_rt)
1105d52d3997SMartin KaFai Lau 		return NULL;
1106d52d3997SMartin KaFai Lau 	ip6_rt_copy_init(pcpu_rt, rt);
1107d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_protocol = rt->rt6i_protocol;
1108d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
1109d52d3997SMartin KaFai Lau 	return pcpu_rt;
1110d52d3997SMartin KaFai Lau }
1111d52d3997SMartin KaFai Lau 
111266f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */
1113d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt)
1114d52d3997SMartin KaFai Lau {
1115a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, **p;
1116d52d3997SMartin KaFai Lau 
1117d52d3997SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1118d52d3997SMartin KaFai Lau 	pcpu_rt = *p;
1119d52d3997SMartin KaFai Lau 
1120d3843fe5SWei Wang 	if (pcpu_rt && ip6_hold_safe(NULL, &pcpu_rt, false))
1121a73e4195SMartin KaFai Lau 		rt6_dst_from_metrics_check(pcpu_rt);
1122d3843fe5SWei Wang 
1123a73e4195SMartin KaFai Lau 	return pcpu_rt;
1124a73e4195SMartin KaFai Lau }
1125a73e4195SMartin KaFai Lau 
1126a73e4195SMartin KaFai Lau static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt)
1127a73e4195SMartin KaFai Lau {
1128a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1129d52d3997SMartin KaFai Lau 
1130d52d3997SMartin KaFai Lau 	pcpu_rt = ip6_rt_pcpu_alloc(rt);
1131d52d3997SMartin KaFai Lau 	if (!pcpu_rt) {
1132d52d3997SMartin KaFai Lau 		struct net *net = dev_net(rt->dst.dev);
1133d52d3997SMartin KaFai Lau 
11349c7370a1SMartin KaFai Lau 		dst_hold(&net->ipv6.ip6_null_entry->dst);
11359c7370a1SMartin KaFai Lau 		return net->ipv6.ip6_null_entry;
1136d52d3997SMartin KaFai Lau 	}
1137d52d3997SMartin KaFai Lau 
1138a94b9367SWei Wang 	dst_hold(&pcpu_rt->dst);
1139a73e4195SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1140d52d3997SMartin KaFai Lau 	prev = cmpxchg(p, NULL, pcpu_rt);
1141951f788aSEric Dumazet 	BUG_ON(prev);
1142a94b9367SWei Wang 
1143d52d3997SMartin KaFai Lau 	rt6_dst_from_metrics_check(pcpu_rt);
1144d52d3997SMartin KaFai Lau 	return pcpu_rt;
1145d52d3997SMartin KaFai Lau }
1146d52d3997SMartin KaFai Lau 
114735732d01SWei Wang /* exception hash table implementation
114835732d01SWei Wang  */
114935732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
115035732d01SWei Wang 
115135732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
115235732d01SWei Wang  * Caller must hold rt6_exception_lock
115335732d01SWei Wang  */
115435732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
115535732d01SWei Wang 				 struct rt6_exception *rt6_ex)
115635732d01SWei Wang {
1157b2427e67SColin Ian King 	struct net *net;
115881eb8447SWei Wang 
115935732d01SWei Wang 	if (!bucket || !rt6_ex)
116035732d01SWei Wang 		return;
1161b2427e67SColin Ian King 
1162b2427e67SColin Ian King 	net = dev_net(rt6_ex->rt6i->dst.dev);
116335732d01SWei Wang 	rt6_ex->rt6i->rt6i_node = NULL;
116435732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
116535732d01SWei Wang 	rt6_release(rt6_ex->rt6i);
116635732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
116735732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
116835732d01SWei Wang 	bucket->depth--;
116981eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache--;
117035732d01SWei Wang }
117135732d01SWei Wang 
117235732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
117335732d01SWei Wang  * Caller must hold rt6_exception_lock
117435732d01SWei Wang  */
117535732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
117635732d01SWei Wang {
117735732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
117835732d01SWei Wang 
117935732d01SWei Wang 	if (!bucket)
118035732d01SWei Wang 		return;
118135732d01SWei Wang 
118235732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
118335732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
118435732d01SWei Wang 			oldest = rt6_ex;
118535732d01SWei Wang 	}
118635732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
118735732d01SWei Wang }
118835732d01SWei Wang 
118935732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
119035732d01SWei Wang 			      const struct in6_addr *src)
119135732d01SWei Wang {
119235732d01SWei Wang 	static u32 seed __read_mostly;
119335732d01SWei Wang 	u32 val;
119435732d01SWei Wang 
119535732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
119635732d01SWei Wang 	val = jhash(dst, sizeof(*dst), seed);
119735732d01SWei Wang 
119835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
119935732d01SWei Wang 	if (src)
120035732d01SWei Wang 		val = jhash(src, sizeof(*src), val);
120135732d01SWei Wang #endif
120235732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
120335732d01SWei Wang }
120435732d01SWei Wang 
120535732d01SWei Wang /* Helper function to find the cached rt in the hash table
120635732d01SWei Wang  * and update bucket pointer to point to the bucket for this
120735732d01SWei Wang  * (daddr, saddr) pair
120835732d01SWei Wang  * Caller must hold rt6_exception_lock
120935732d01SWei Wang  */
121035732d01SWei Wang static struct rt6_exception *
121135732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
121235732d01SWei Wang 			      const struct in6_addr *daddr,
121335732d01SWei Wang 			      const struct in6_addr *saddr)
121435732d01SWei Wang {
121535732d01SWei Wang 	struct rt6_exception *rt6_ex;
121635732d01SWei Wang 	u32 hval;
121735732d01SWei Wang 
121835732d01SWei Wang 	if (!(*bucket) || !daddr)
121935732d01SWei Wang 		return NULL;
122035732d01SWei Wang 
122135732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
122235732d01SWei Wang 	*bucket += hval;
122335732d01SWei Wang 
122435732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
122535732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
122635732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
122735732d01SWei Wang 
122835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
122935732d01SWei Wang 		if (matched && saddr)
123035732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
123135732d01SWei Wang #endif
123235732d01SWei Wang 		if (matched)
123335732d01SWei Wang 			return rt6_ex;
123435732d01SWei Wang 	}
123535732d01SWei Wang 	return NULL;
123635732d01SWei Wang }
123735732d01SWei Wang 
123835732d01SWei Wang /* Helper function to find the cached rt in the hash table
123935732d01SWei Wang  * and update bucket pointer to point to the bucket for this
124035732d01SWei Wang  * (daddr, saddr) pair
124135732d01SWei Wang  * Caller must hold rcu_read_lock()
124235732d01SWei Wang  */
124335732d01SWei Wang static struct rt6_exception *
124435732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
124535732d01SWei Wang 			 const struct in6_addr *daddr,
124635732d01SWei Wang 			 const struct in6_addr *saddr)
124735732d01SWei Wang {
124835732d01SWei Wang 	struct rt6_exception *rt6_ex;
124935732d01SWei Wang 	u32 hval;
125035732d01SWei Wang 
125135732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
125235732d01SWei Wang 
125335732d01SWei Wang 	if (!(*bucket) || !daddr)
125435732d01SWei Wang 		return NULL;
125535732d01SWei Wang 
125635732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
125735732d01SWei Wang 	*bucket += hval;
125835732d01SWei Wang 
125935732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
126035732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
126135732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
126235732d01SWei Wang 
126335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
126435732d01SWei Wang 		if (matched && saddr)
126535732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
126635732d01SWei Wang #endif
126735732d01SWei Wang 		if (matched)
126835732d01SWei Wang 			return rt6_ex;
126935732d01SWei Wang 	}
127035732d01SWei Wang 	return NULL;
127135732d01SWei Wang }
127235732d01SWei Wang 
127335732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
127435732d01SWei Wang 				struct rt6_info *ort)
127535732d01SWei Wang {
127681eb8447SWei Wang 	struct net *net = dev_net(ort->dst.dev);
127735732d01SWei Wang 	struct rt6_exception_bucket *bucket;
127835732d01SWei Wang 	struct in6_addr *src_key = NULL;
127935732d01SWei Wang 	struct rt6_exception *rt6_ex;
128035732d01SWei Wang 	int err = 0;
128135732d01SWei Wang 
128235732d01SWei Wang 	/* ort can't be a cache or pcpu route */
128335732d01SWei Wang 	if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
12843a2232e9SDavid Miller 		ort = ort->from;
128535732d01SWei Wang 	WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU));
128635732d01SWei Wang 
128735732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
128835732d01SWei Wang 
128935732d01SWei Wang 	if (ort->exception_bucket_flushed) {
129035732d01SWei Wang 		err = -EINVAL;
129135732d01SWei Wang 		goto out;
129235732d01SWei Wang 	}
129335732d01SWei Wang 
129435732d01SWei Wang 	bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
129535732d01SWei Wang 					lockdep_is_held(&rt6_exception_lock));
129635732d01SWei Wang 	if (!bucket) {
129735732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
129835732d01SWei Wang 				 GFP_ATOMIC);
129935732d01SWei Wang 		if (!bucket) {
130035732d01SWei Wang 			err = -ENOMEM;
130135732d01SWei Wang 			goto out;
130235732d01SWei Wang 		}
130335732d01SWei Wang 		rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
130435732d01SWei Wang 	}
130535732d01SWei Wang 
130635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
130735732d01SWei Wang 	/* rt6i_src.plen != 0 indicates ort is in subtree
130835732d01SWei Wang 	 * and exception table is indexed by a hash of
130935732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
131035732d01SWei Wang 	 * Otherwise, the exception table is indexed by
131135732d01SWei Wang 	 * a hash of only rt6i_dst.
131235732d01SWei Wang 	 */
131335732d01SWei Wang 	if (ort->rt6i_src.plen)
131435732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
131535732d01SWei Wang #endif
131660006a48SWei Wang 
131760006a48SWei Wang 	/* Update rt6i_prefsrc as it could be changed
131860006a48SWei Wang 	 * in rt6_remove_prefsrc()
131960006a48SWei Wang 	 */
132060006a48SWei Wang 	nrt->rt6i_prefsrc = ort->rt6i_prefsrc;
1321f5bbe7eeSWei Wang 	/* rt6_mtu_change() might lower mtu on ort.
1322f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
1323f5bbe7eeSWei Wang 	 * is less than ort's mtu value.
1324f5bbe7eeSWei Wang 	 */
1325f5bbe7eeSWei Wang 	if (nrt->rt6i_pmtu >= dst_mtu(&ort->dst)) {
1326f5bbe7eeSWei Wang 		err = -EINVAL;
1327f5bbe7eeSWei Wang 		goto out;
1328f5bbe7eeSWei Wang 	}
132960006a48SWei Wang 
133035732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
133135732d01SWei Wang 					       src_key);
133235732d01SWei Wang 	if (rt6_ex)
133335732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
133435732d01SWei Wang 
133535732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
133635732d01SWei Wang 	if (!rt6_ex) {
133735732d01SWei Wang 		err = -ENOMEM;
133835732d01SWei Wang 		goto out;
133935732d01SWei Wang 	}
134035732d01SWei Wang 	rt6_ex->rt6i = nrt;
134135732d01SWei Wang 	rt6_ex->stamp = jiffies;
134235732d01SWei Wang 	atomic_inc(&nrt->rt6i_ref);
134335732d01SWei Wang 	nrt->rt6i_node = ort->rt6i_node;
134435732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
134535732d01SWei Wang 	bucket->depth++;
134681eb8447SWei Wang 	net->ipv6.rt6_stats->fib_rt_cache++;
134735732d01SWei Wang 
134835732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
134935732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
135035732d01SWei Wang 
135135732d01SWei Wang out:
135235732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
135335732d01SWei Wang 
135435732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
1355b886d5f2SPaolo Abeni 	if (!err) {
135635732d01SWei Wang 		fib6_update_sernum(ort);
1357b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1358b886d5f2SPaolo Abeni 	}
135935732d01SWei Wang 
136035732d01SWei Wang 	return err;
136135732d01SWei Wang }
136235732d01SWei Wang 
136335732d01SWei Wang void rt6_flush_exceptions(struct rt6_info *rt)
136435732d01SWei Wang {
136535732d01SWei Wang 	struct rt6_exception_bucket *bucket;
136635732d01SWei Wang 	struct rt6_exception *rt6_ex;
136735732d01SWei Wang 	struct hlist_node *tmp;
136835732d01SWei Wang 	int i;
136935732d01SWei Wang 
137035732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
137135732d01SWei Wang 	/* Prevent rt6_insert_exception() to recreate the bucket list */
137235732d01SWei Wang 	rt->exception_bucket_flushed = 1;
137335732d01SWei Wang 
137435732d01SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
137535732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
137635732d01SWei Wang 	if (!bucket)
137735732d01SWei Wang 		goto out;
137835732d01SWei Wang 
137935732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
138035732d01SWei Wang 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
138135732d01SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
138235732d01SWei Wang 		WARN_ON_ONCE(bucket->depth);
138335732d01SWei Wang 		bucket++;
138435732d01SWei Wang 	}
138535732d01SWei Wang 
138635732d01SWei Wang out:
138735732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
138835732d01SWei Wang }
138935732d01SWei Wang 
139035732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
139135732d01SWei Wang  * Caller has to hold rcu_read_lock()
139235732d01SWei Wang  */
139335732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt,
139435732d01SWei Wang 					   struct in6_addr *daddr,
139535732d01SWei Wang 					   struct in6_addr *saddr)
139635732d01SWei Wang {
139735732d01SWei Wang 	struct rt6_exception_bucket *bucket;
139835732d01SWei Wang 	struct in6_addr *src_key = NULL;
139935732d01SWei Wang 	struct rt6_exception *rt6_ex;
140035732d01SWei Wang 	struct rt6_info *res = NULL;
140135732d01SWei Wang 
140235732d01SWei Wang 	bucket = rcu_dereference(rt->rt6i_exception_bucket);
140335732d01SWei Wang 
140435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
140535732d01SWei Wang 	/* rt6i_src.plen != 0 indicates rt is in subtree
140635732d01SWei Wang 	 * and exception table is indexed by a hash of
140735732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
140835732d01SWei Wang 	 * Otherwise, the exception table is indexed by
140935732d01SWei Wang 	 * a hash of only rt6i_dst.
141035732d01SWei Wang 	 */
141135732d01SWei Wang 	if (rt->rt6i_src.plen)
141235732d01SWei Wang 		src_key = saddr;
141335732d01SWei Wang #endif
141435732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
141535732d01SWei Wang 
141635732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
141735732d01SWei Wang 		res = rt6_ex->rt6i;
141835732d01SWei Wang 
141935732d01SWei Wang 	return res;
142035732d01SWei Wang }
142135732d01SWei Wang 
142235732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
142335732d01SWei Wang int rt6_remove_exception_rt(struct rt6_info *rt)
142435732d01SWei Wang {
142535732d01SWei Wang 	struct rt6_exception_bucket *bucket;
14263a2232e9SDavid Miller 	struct rt6_info *from = rt->from;
142735732d01SWei Wang 	struct in6_addr *src_key = NULL;
142835732d01SWei Wang 	struct rt6_exception *rt6_ex;
142935732d01SWei Wang 	int err;
143035732d01SWei Wang 
143135732d01SWei Wang 	if (!from ||
1432442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
143335732d01SWei Wang 		return -EINVAL;
143435732d01SWei Wang 
143535732d01SWei Wang 	if (!rcu_access_pointer(from->rt6i_exception_bucket))
143635732d01SWei Wang 		return -ENOENT;
143735732d01SWei Wang 
143835732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
143935732d01SWei Wang 	bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
144035732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
144135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
144235732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
144335732d01SWei Wang 	 * and exception table is indexed by a hash of
144435732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
144535732d01SWei Wang 	 * Otherwise, the exception table is indexed by
144635732d01SWei Wang 	 * a hash of only rt6i_dst.
144735732d01SWei Wang 	 */
144835732d01SWei Wang 	if (from->rt6i_src.plen)
144935732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
145035732d01SWei Wang #endif
145135732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
145235732d01SWei Wang 					       &rt->rt6i_dst.addr,
145335732d01SWei Wang 					       src_key);
145435732d01SWei Wang 	if (rt6_ex) {
145535732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
145635732d01SWei Wang 		err = 0;
145735732d01SWei Wang 	} else {
145835732d01SWei Wang 		err = -ENOENT;
145935732d01SWei Wang 	}
146035732d01SWei Wang 
146135732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
146235732d01SWei Wang 	return err;
146335732d01SWei Wang }
146435732d01SWei Wang 
146535732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
146635732d01SWei Wang  * refresh its stamp
146735732d01SWei Wang  */
146835732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
146935732d01SWei Wang {
147035732d01SWei Wang 	struct rt6_exception_bucket *bucket;
14713a2232e9SDavid Miller 	struct rt6_info *from = rt->from;
147235732d01SWei Wang 	struct in6_addr *src_key = NULL;
147335732d01SWei Wang 	struct rt6_exception *rt6_ex;
147435732d01SWei Wang 
147535732d01SWei Wang 	if (!from ||
1476442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
147735732d01SWei Wang 		return;
147835732d01SWei Wang 
147935732d01SWei Wang 	rcu_read_lock();
148035732d01SWei Wang 	bucket = rcu_dereference(from->rt6i_exception_bucket);
148135732d01SWei Wang 
148235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
148335732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
148435732d01SWei Wang 	 * and exception table is indexed by a hash of
148535732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
148635732d01SWei Wang 	 * Otherwise, the exception table is indexed by
148735732d01SWei Wang 	 * a hash of only rt6i_dst.
148835732d01SWei Wang 	 */
148935732d01SWei Wang 	if (from->rt6i_src.plen)
149035732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
149135732d01SWei Wang #endif
149235732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket,
149335732d01SWei Wang 					  &rt->rt6i_dst.addr,
149435732d01SWei Wang 					  src_key);
149535732d01SWei Wang 	if (rt6_ex)
149635732d01SWei Wang 		rt6_ex->stamp = jiffies;
149735732d01SWei Wang 
149835732d01SWei Wang 	rcu_read_unlock();
149935732d01SWei Wang }
150035732d01SWei Wang 
150160006a48SWei Wang static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt)
150260006a48SWei Wang {
150360006a48SWei Wang 	struct rt6_exception_bucket *bucket;
150460006a48SWei Wang 	struct rt6_exception *rt6_ex;
150560006a48SWei Wang 	int i;
150660006a48SWei Wang 
150760006a48SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
150860006a48SWei Wang 					lockdep_is_held(&rt6_exception_lock));
150960006a48SWei Wang 
151060006a48SWei Wang 	if (bucket) {
151160006a48SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
151260006a48SWei Wang 			hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
151360006a48SWei Wang 				rt6_ex->rt6i->rt6i_prefsrc.plen = 0;
151460006a48SWei Wang 			}
151560006a48SWei Wang 			bucket++;
151660006a48SWei Wang 		}
151760006a48SWei Wang 	}
151860006a48SWei Wang }
151960006a48SWei Wang 
1520f5bbe7eeSWei Wang static void rt6_exceptions_update_pmtu(struct rt6_info *rt, int mtu)
1521f5bbe7eeSWei Wang {
1522f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
1523f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
1524f5bbe7eeSWei Wang 	int i;
1525f5bbe7eeSWei Wang 
1526f5bbe7eeSWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1527f5bbe7eeSWei Wang 					lockdep_is_held(&rt6_exception_lock));
1528f5bbe7eeSWei Wang 
1529f5bbe7eeSWei Wang 	if (bucket) {
1530f5bbe7eeSWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1531f5bbe7eeSWei Wang 			hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1532f5bbe7eeSWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1533f5bbe7eeSWei Wang 				/* For RTF_CACHE with rt6i_pmtu == 0
1534f5bbe7eeSWei Wang 				 * (i.e. a redirected route),
1535f5bbe7eeSWei Wang 				 * the metrics of its rt->dst.from has already
1536f5bbe7eeSWei Wang 				 * been updated.
1537f5bbe7eeSWei Wang 				 */
1538f5bbe7eeSWei Wang 				if (entry->rt6i_pmtu && entry->rt6i_pmtu > mtu)
1539f5bbe7eeSWei Wang 					entry->rt6i_pmtu = mtu;
1540f5bbe7eeSWei Wang 			}
1541f5bbe7eeSWei Wang 			bucket++;
1542f5bbe7eeSWei Wang 		}
1543f5bbe7eeSWei Wang 	}
1544f5bbe7eeSWei Wang }
1545f5bbe7eeSWei Wang 
1546b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
1547b16cb459SWei Wang 
1548b16cb459SWei Wang static void rt6_exceptions_clean_tohost(struct rt6_info *rt,
1549b16cb459SWei Wang 					struct in6_addr *gateway)
1550b16cb459SWei Wang {
1551b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
1552b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
1553b16cb459SWei Wang 	struct hlist_node *tmp;
1554b16cb459SWei Wang 	int i;
1555b16cb459SWei Wang 
1556b16cb459SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1557b16cb459SWei Wang 		return;
1558b16cb459SWei Wang 
1559b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1560b16cb459SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1561b16cb459SWei Wang 				     lockdep_is_held(&rt6_exception_lock));
1562b16cb459SWei Wang 
1563b16cb459SWei Wang 	if (bucket) {
1564b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1565b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1566b16cb459SWei Wang 						  &bucket->chain, hlist) {
1567b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1568b16cb459SWei Wang 
1569b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1570b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
1571b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
1572b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
1573b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
1574b16cb459SWei Wang 				}
1575b16cb459SWei Wang 			}
1576b16cb459SWei Wang 			bucket++;
1577b16cb459SWei Wang 		}
1578b16cb459SWei Wang 	}
1579b16cb459SWei Wang 
1580b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1581b16cb459SWei Wang }
1582b16cb459SWei Wang 
1583c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1584c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
1585c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
1586c757faa8SWei Wang 				      unsigned long now)
1587c757faa8SWei Wang {
1588c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
1589c757faa8SWei Wang 
15901859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
15911859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
15921859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
15931859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
15941859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
15951859bac0SPaolo Abeni 	 */
15961859bac0SPaolo Abeni 	if (!(rt->rt6i_flags & RTF_EXPIRES) &&
1597c757faa8SWei Wang 	    time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1598c757faa8SWei Wang 		RT6_TRACE("aging clone %p\n", rt);
1599c757faa8SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
1600c757faa8SWei Wang 		return;
1601c757faa8SWei Wang 	} else if (rt->rt6i_flags & RTF_GATEWAY) {
1602c757faa8SWei Wang 		struct neighbour *neigh;
1603c757faa8SWei Wang 		__u8 neigh_flags = 0;
1604c757faa8SWei Wang 
1605c757faa8SWei Wang 		neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1606c757faa8SWei Wang 		if (neigh) {
1607c757faa8SWei Wang 			neigh_flags = neigh->flags;
1608c757faa8SWei Wang 			neigh_release(neigh);
1609c757faa8SWei Wang 		}
1610c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
1611c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
1612c757faa8SWei Wang 				  rt);
1613c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1614c757faa8SWei Wang 			return;
1615c757faa8SWei Wang 		}
16161859bac0SPaolo Abeni 	} else if (__rt6_check_expired(rt)) {
16171859bac0SPaolo Abeni 		RT6_TRACE("purging expired route %p\n", rt);
16181859bac0SPaolo Abeni 		rt6_remove_exception(bucket, rt6_ex);
16191859bac0SPaolo Abeni 		return;
1620c757faa8SWei Wang 	}
1621c757faa8SWei Wang 	gc_args->more++;
1622c757faa8SWei Wang }
1623c757faa8SWei Wang 
1624c757faa8SWei Wang void rt6_age_exceptions(struct rt6_info *rt,
1625c757faa8SWei Wang 			struct fib6_gc_args *gc_args,
1626c757faa8SWei Wang 			unsigned long now)
1627c757faa8SWei Wang {
1628c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
1629c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
1630c757faa8SWei Wang 	struct hlist_node *tmp;
1631c757faa8SWei Wang 	int i;
1632c757faa8SWei Wang 
1633c757faa8SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1634c757faa8SWei Wang 		return;
1635c757faa8SWei Wang 
1636c757faa8SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1637c757faa8SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1638c757faa8SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
1639c757faa8SWei Wang 
1640c757faa8SWei Wang 	if (bucket) {
1641c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1642c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1643c757faa8SWei Wang 						  &bucket->chain, hlist) {
1644c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
1645c757faa8SWei Wang 							  gc_args, now);
1646c757faa8SWei Wang 			}
1647c757faa8SWei Wang 			bucket++;
1648c757faa8SWei Wang 		}
1649c757faa8SWei Wang 	}
1650c757faa8SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1651c757faa8SWei Wang }
1652c757faa8SWei Wang 
16539ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
16549ff74384SDavid Ahern 			       int oif, struct flowi6 *fl6, int flags)
16551da177e4SLinus Torvalds {
1656367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
16572b760fcfSWei Wang 	struct rt6_info *rt, *rt_cache;
1658c71099acSThomas Graf 	int strict = 0;
16591da177e4SLinus Torvalds 
166077d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
1661d5d32e4bSDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
1662367efcb9SMartin KaFai Lau 	if (net->ipv6.devconf_all->forwarding == 0)
1663367efcb9SMartin KaFai Lau 		strict |= RT6_LOOKUP_F_REACHABLE;
16641da177e4SLinus Torvalds 
166566f5d6ceSWei Wang 	rcu_read_lock();
16661da177e4SLinus Torvalds 
16674c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1668367efcb9SMartin KaFai Lau 	saved_fn = fn;
16691da177e4SLinus Torvalds 
1670ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1671ca254490SDavid Ahern 		oif = 0;
1672ca254490SDavid Ahern 
1673a3c00e46SMartin KaFai Lau redo_rt6_select:
16748d1040e8SWei Wang 	rt = rt6_select(net, fn, oif, strict);
167552bd4c0cSNicolas Dichtel 	if (rt->rt6i_nsiblings)
1676367efcb9SMartin KaFai Lau 		rt = rt6_multipath_select(rt, fl6, oif, strict);
1677a3c00e46SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
1678a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1679a3c00e46SMartin KaFai Lau 		if (fn)
1680a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
1681367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
1682367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
1683367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
1684367efcb9SMartin KaFai Lau 			fn = saved_fn;
1685367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
1686367efcb9SMartin KaFai Lau 		}
1687a3c00e46SMartin KaFai Lau 	}
1688a3c00e46SMartin KaFai Lau 
16892b760fcfSWei Wang 	/*Search through exception table */
16902b760fcfSWei Wang 	rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr);
16912b760fcfSWei Wang 	if (rt_cache)
16922b760fcfSWei Wang 		rt = rt_cache;
1693d52d3997SMartin KaFai Lau 
1694d3843fe5SWei Wang 	if (rt == net->ipv6.ip6_null_entry) {
169566f5d6ceSWei Wang 		rcu_read_unlock();
1696d3843fe5SWei Wang 		dst_hold(&rt->dst);
1697b65f164dSPaolo Abeni 		trace_fib6_table_lookup(net, rt, table, fl6);
1698d3843fe5SWei Wang 		return rt;
1699d3843fe5SWei Wang 	} else if (rt->rt6i_flags & RTF_CACHE) {
1700d3843fe5SWei Wang 		if (ip6_hold_safe(net, &rt, true)) {
1701d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
1702d52d3997SMartin KaFai Lau 			rt6_dst_from_metrics_check(rt);
1703d3843fe5SWei Wang 		}
170466f5d6ceSWei Wang 		rcu_read_unlock();
1705b65f164dSPaolo Abeni 		trace_fib6_table_lookup(net, rt, table, fl6);
1706d52d3997SMartin KaFai Lau 		return rt;
17073da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
17083da59bd9SMartin KaFai Lau 			    !(rt->rt6i_flags & RTF_GATEWAY))) {
17093da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
17103da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
17113da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
17123da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
17133da59bd9SMartin KaFai Lau 		 */
1714c71099acSThomas Graf 
17153da59bd9SMartin KaFai Lau 		struct rt6_info *uncached_rt;
17163da59bd9SMartin KaFai Lau 
1717d3843fe5SWei Wang 		if (ip6_hold_safe(net, &rt, true)) {
1718d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
1719d3843fe5SWei Wang 		} else {
172066f5d6ceSWei Wang 			rcu_read_unlock();
1721d3843fe5SWei Wang 			uncached_rt = rt;
1722d3843fe5SWei Wang 			goto uncached_rt_out;
1723d3843fe5SWei Wang 		}
172466f5d6ceSWei Wang 		rcu_read_unlock();
1725d52d3997SMartin KaFai Lau 
17263da59bd9SMartin KaFai Lau 		uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL);
17273da59bd9SMartin KaFai Lau 		dst_release(&rt->dst);
17283da59bd9SMartin KaFai Lau 
17291cfb71eeSWei Wang 		if (uncached_rt) {
17301cfb71eeSWei Wang 			/* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
17311cfb71eeSWei Wang 			 * No need for another dst_hold()
17321cfb71eeSWei Wang 			 */
17338d0b94afSMartin KaFai Lau 			rt6_uncached_list_add(uncached_rt);
173481eb8447SWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
17351cfb71eeSWei Wang 		} else {
17363da59bd9SMartin KaFai Lau 			uncached_rt = net->ipv6.ip6_null_entry;
17373da59bd9SMartin KaFai Lau 			dst_hold(&uncached_rt->dst);
17381cfb71eeSWei Wang 		}
1739b811580dSDavid Ahern 
1740d3843fe5SWei Wang uncached_rt_out:
1741b65f164dSPaolo Abeni 		trace_fib6_table_lookup(net, uncached_rt, table, fl6);
17423da59bd9SMartin KaFai Lau 		return uncached_rt;
17433da59bd9SMartin KaFai Lau 
1744d52d3997SMartin KaFai Lau 	} else {
1745d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
1746d52d3997SMartin KaFai Lau 
1747d52d3997SMartin KaFai Lau 		struct rt6_info *pcpu_rt;
1748d52d3997SMartin KaFai Lau 
1749d3843fe5SWei Wang 		dst_use_noref(&rt->dst, jiffies);
1750951f788aSEric Dumazet 		local_bh_disable();
1751d52d3997SMartin KaFai Lau 		pcpu_rt = rt6_get_pcpu_route(rt);
1752d52d3997SMartin KaFai Lau 
1753951f788aSEric Dumazet 		if (!pcpu_rt) {
1754a94b9367SWei Wang 			/* atomic_inc_not_zero() is needed when using rcu */
1755a94b9367SWei Wang 			if (atomic_inc_not_zero(&rt->rt6i_ref)) {
1756951f788aSEric Dumazet 				/* No dst_hold() on rt is needed because grabbing
1757a94b9367SWei Wang 				 * rt->rt6i_ref makes sure rt can't be released.
17589c7370a1SMartin KaFai Lau 				 */
17599c7370a1SMartin KaFai Lau 				pcpu_rt = rt6_make_pcpu_route(rt);
1760a94b9367SWei Wang 				rt6_release(rt);
1761a94b9367SWei Wang 			} else {
1762a94b9367SWei Wang 				/* rt is already removed from tree */
1763a94b9367SWei Wang 				pcpu_rt = net->ipv6.ip6_null_entry;
1764a94b9367SWei Wang 				dst_hold(&pcpu_rt->dst);
1765a94b9367SWei Wang 			}
17669c7370a1SMartin KaFai Lau 		}
1767951f788aSEric Dumazet 		local_bh_enable();
1768951f788aSEric Dumazet 		rcu_read_unlock();
1769b65f164dSPaolo Abeni 		trace_fib6_table_lookup(net, pcpu_rt, table, fl6);
1770d52d3997SMartin KaFai Lau 		return pcpu_rt;
1771d52d3997SMartin KaFai Lau 	}
1772c71099acSThomas Graf }
17739ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
1774c71099acSThomas Graf 
17758ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
17764c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
17774acad72dSPavel Emelyanov {
17784c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
17794acad72dSPavel Emelyanov }
17804acad72dSPavel Emelyanov 
1781d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
178272331bc0SShmulik Ladkani 					 struct net_device *dev,
178372331bc0SShmulik Ladkani 					 struct flowi6 *fl6, int flags)
178472331bc0SShmulik Ladkani {
178572331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
178672331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
178772331bc0SShmulik Ladkani 
178872331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
178972331bc0SShmulik Ladkani }
1790d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
179172331bc0SShmulik Ladkani 
179223aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
179323aebdacSJakub Sitnicki 				  struct flow_keys *keys)
179423aebdacSJakub Sitnicki {
179523aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
179623aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
179723aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
179823aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
179923aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
180023aebdacSJakub Sitnicki 
180123aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
180223aebdacSJakub Sitnicki 		goto out;
180323aebdacSJakub Sitnicki 
180423aebdacSJakub Sitnicki 	icmph = icmp6_hdr(skb);
180523aebdacSJakub Sitnicki 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
180623aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
180723aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
180823aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
180923aebdacSJakub Sitnicki 		goto out;
181023aebdacSJakub Sitnicki 
181123aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
181223aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
181323aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
181423aebdacSJakub Sitnicki 	if (!inner_iph)
181523aebdacSJakub Sitnicki 		goto out;
181623aebdacSJakub Sitnicki 
181723aebdacSJakub Sitnicki 	key_iph = inner_iph;
181823aebdacSJakub Sitnicki out:
181923aebdacSJakub Sitnicki 	memset(keys, 0, sizeof(*keys));
182023aebdacSJakub Sitnicki 	keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
182123aebdacSJakub Sitnicki 	keys->addrs.v6addrs.src = key_iph->saddr;
182223aebdacSJakub Sitnicki 	keys->addrs.v6addrs.dst = key_iph->daddr;
182323aebdacSJakub Sitnicki 	keys->tags.flow_label = ip6_flowinfo(key_iph);
182423aebdacSJakub Sitnicki 	keys->basic.ip_proto = key_iph->nexthdr;
182523aebdacSJakub Sitnicki }
182623aebdacSJakub Sitnicki 
182723aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
182823aebdacSJakub Sitnicki u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb)
182923aebdacSJakub Sitnicki {
183023aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
183123aebdacSJakub Sitnicki 
183223aebdacSJakub Sitnicki 	if (skb) {
183323aebdacSJakub Sitnicki 		ip6_multipath_l3_keys(skb, &hash_keys);
183423aebdacSJakub Sitnicki 		return flow_hash_from_keys(&hash_keys);
183523aebdacSJakub Sitnicki 	}
183623aebdacSJakub Sitnicki 
183723aebdacSJakub Sitnicki 	return get_hash_from_flowi6(fl6);
183823aebdacSJakub Sitnicki }
183923aebdacSJakub Sitnicki 
1840c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
1841c71099acSThomas Graf {
1842b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
1843c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
1844adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1845904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
18464c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
1847e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
18484c9483b2SDavid S. Miller 		.daddr = iph->daddr,
18494c9483b2SDavid S. Miller 		.saddr = iph->saddr,
18506502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
18514c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
18524c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
1853c71099acSThomas Graf 	};
1854adaa70bbSThomas Graf 
1855904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
185646fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
1857904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
185823aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
185923aebdacSJakub Sitnicki 		fl6.mp_hash = rt6_multipath_hash(&fl6, skb);
186006e9d040SJiri Benc 	skb_dst_drop(skb);
186172331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
1862c71099acSThomas Graf }
1863c71099acSThomas Graf 
18648ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
18654c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
1866c71099acSThomas Graf {
18674c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
1868c71099acSThomas Graf }
1869c71099acSThomas Graf 
18706f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
18716f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags)
1872c71099acSThomas Graf {
1873d46a9d67SDavid Ahern 	bool any_src;
1874c71099acSThomas Graf 
18754c1feac5SDavid Ahern 	if (rt6_need_strict(&fl6->daddr)) {
18764c1feac5SDavid Ahern 		struct dst_entry *dst;
18774c1feac5SDavid Ahern 
18784c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
1879ca254490SDavid Ahern 		if (dst)
1880ca254490SDavid Ahern 			return dst;
18814c1feac5SDavid Ahern 	}
1882ca254490SDavid Ahern 
18831fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
18844dc27d1cSDavid McCullough 
1885d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
1886741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
1887d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
188877d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
1889c71099acSThomas Graf 
1890d46a9d67SDavid Ahern 	if (!any_src)
1891adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
18920c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
18930c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
1894adaa70bbSThomas Graf 
18954c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
18961da177e4SLinus Torvalds }
18976f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
18981da177e4SLinus Torvalds 
18992774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
190014e50e57SDavid S. Miller {
19015c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
19021dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
190314e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
190414e50e57SDavid S. Miller 
19051dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
190662cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
190714e50e57SDavid S. Miller 	if (rt) {
19080a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
190981eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
19100a1f5962SMartin KaFai Lau 
1911d8d1f30bSChangli Gao 		new = &rt->dst;
191214e50e57SDavid S. Miller 		new->__use = 1;
1913352e512cSHerbert Xu 		new->input = dst_discard;
1914ede2059dSEric W. Biederman 		new->output = dst_discard_out;
191514e50e57SDavid S. Miller 
1916defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
191714e50e57SDavid S. Miller 
19181dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
19194e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
19200a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
192114e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
192214e50e57SDavid S. Miller 
192314e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
192414e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
192514e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
192614e50e57SDavid S. Miller #endif
192714e50e57SDavid S. Miller 	}
192814e50e57SDavid S. Miller 
192969ead7afSDavid S. Miller 	dst_release(dst_orig);
193069ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
193114e50e57SDavid S. Miller }
193214e50e57SDavid S. Miller 
19331da177e4SLinus Torvalds /*
19341da177e4SLinus Torvalds  *	Destination cache support functions
19351da177e4SLinus Torvalds  */
19361da177e4SLinus Torvalds 
19374b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt)
19384b32b5adSMartin KaFai Lau {
19393a2232e9SDavid Miller 	if (rt->from &&
19403a2232e9SDavid Miller 	    dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(&rt->from->dst))
19413a2232e9SDavid Miller 		dst_init_metrics(&rt->dst, dst_metrics_ptr(&rt->from->dst), true);
19424b32b5adSMartin KaFai Lau }
19434b32b5adSMartin KaFai Lau 
19443da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie)
19453da59bd9SMartin KaFai Lau {
194636143645SSteffen Klassert 	u32 rt_cookie = 0;
1947c5cff856SWei Wang 
1948c5cff856SWei Wang 	if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie)
19493da59bd9SMartin KaFai Lau 		return NULL;
19503da59bd9SMartin KaFai Lau 
19513da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
19523da59bd9SMartin KaFai Lau 		return NULL;
19533da59bd9SMartin KaFai Lau 
19543da59bd9SMartin KaFai Lau 	return &rt->dst;
19553da59bd9SMartin KaFai Lau }
19563da59bd9SMartin KaFai Lau 
19573da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie)
19583da59bd9SMartin KaFai Lau {
19595973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
19605973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
19613a2232e9SDavid Miller 	    rt6_check(rt->from, cookie))
19623da59bd9SMartin KaFai Lau 		return &rt->dst;
19633da59bd9SMartin KaFai Lau 	else
19643da59bd9SMartin KaFai Lau 		return NULL;
19653da59bd9SMartin KaFai Lau }
19663da59bd9SMartin KaFai Lau 
19671da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
19681da177e4SLinus Torvalds {
19691da177e4SLinus Torvalds 	struct rt6_info *rt;
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
19721da177e4SLinus Torvalds 
19736f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
19746f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
19756f3118b5SNicolas Dichtel 	 * into this function always.
19766f3118b5SNicolas Dichtel 	 */
1977e3bc10bdSHannes Frederic Sowa 
19784b32b5adSMartin KaFai Lau 	rt6_dst_from_metrics_check(rt);
19794b32b5adSMartin KaFai Lau 
198002bcf4e0SMartin KaFai Lau 	if (rt->rt6i_flags & RTF_PCPU ||
19813a2232e9SDavid Miller 	    (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from))
19823da59bd9SMartin KaFai Lau 		return rt6_dst_from_check(rt, cookie);
19833da59bd9SMartin KaFai Lau 	else
19843da59bd9SMartin KaFai Lau 		return rt6_check(rt, cookie);
19851da177e4SLinus Torvalds }
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
19881da177e4SLinus Torvalds {
19891da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds 	if (rt) {
199254c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
199354c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1994e0a1ad73SThomas Graf 				ip6_del_rt(rt);
199554c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
19961da177e4SLinus Torvalds 			}
199754c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
199854c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
199954c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
200054c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
200154c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
200254c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
20031da177e4SLinus Torvalds }
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
20061da177e4SLinus Torvalds {
20071da177e4SLinus Torvalds 	struct rt6_info *rt;
20081da177e4SLinus Torvalds 
20093ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
20101da177e4SLinus Torvalds 
2011adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
20121da177e4SLinus Torvalds 	if (rt) {
20131eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
2014ad65a2f0SWei Wang 			if (dst_hold_safe(&rt->dst))
20158e3d5be7SMartin KaFai Lau 				ip6_del_rt(rt);
2016c5cff856SWei Wang 		} else {
2017c5cff856SWei Wang 			struct fib6_node *fn;
2018c5cff856SWei Wang 
2019c5cff856SWei Wang 			rcu_read_lock();
2020c5cff856SWei Wang 			fn = rcu_dereference(rt->rt6i_node);
2021c5cff856SWei Wang 			if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2022c5cff856SWei Wang 				fn->fn_sernum = -1;
2023c5cff856SWei Wang 			rcu_read_unlock();
20241da177e4SLinus Torvalds 		}
20251da177e4SLinus Torvalds 	}
20261eb4f758SHannes Frederic Sowa }
20271da177e4SLinus Torvalds 
202845e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
202945e4fd26SMartin KaFai Lau {
203045e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
203145e4fd26SMartin KaFai Lau 
203245e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
203345e4fd26SMartin KaFai Lau 	rt->rt6i_pmtu = mtu;
203445e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
203545e4fd26SMartin KaFai Lau }
203645e4fd26SMartin KaFai Lau 
20370d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
20380d3f6d29SMartin KaFai Lau {
20390d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
20404e587ea7SWei Wang 		(rt->rt6i_flags & RTF_PCPU ||
20414e587ea7SWei Wang 		 rcu_access_pointer(rt->rt6i_node));
20420d3f6d29SMartin KaFai Lau }
20430d3f6d29SMartin KaFai Lau 
204445e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
204545e4fd26SMartin KaFai Lau 				 const struct ipv6hdr *iph, u32 mtu)
20461da177e4SLinus Torvalds {
20470dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
20481da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
20491da177e4SLinus Torvalds 
205045e4fd26SMartin KaFai Lau 	if (rt6->rt6i_flags & RTF_LOCAL)
205145e4fd26SMartin KaFai Lau 		return;
205245e4fd26SMartin KaFai Lau 
205319bda36cSXin Long 	if (dst_metric_locked(dst, RTAX_MTU))
205419bda36cSXin Long 		return;
205519bda36cSXin Long 
205645e4fd26SMartin KaFai Lau 	if (iph) {
205745e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
205845e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
205945e4fd26SMartin KaFai Lau 	} else if (sk) {
206045e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
206145e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
206245e4fd26SMartin KaFai Lau 	} else {
20630dec879fSJulian Anastasov 		daddr = NULL;
20640dec879fSJulian Anastasov 		saddr = NULL;
20651da177e4SLinus Torvalds 	}
20660dec879fSJulian Anastasov 	dst_confirm_neigh(dst, daddr);
20670dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
20680dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
20690dec879fSJulian Anastasov 		return;
20700dec879fSJulian Anastasov 
20710dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
20720dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
20732b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
20742b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
20752b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
20760dec879fSJulian Anastasov 	} else if (daddr) {
20770dec879fSJulian Anastasov 		struct rt6_info *nrt6;
20780dec879fSJulian Anastasov 
207945e4fd26SMartin KaFai Lau 		nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr);
208045e4fd26SMartin KaFai Lau 		if (nrt6) {
208145e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
20822b760fcfSWei Wang 			if (rt6_insert_exception(nrt6, rt6))
20832b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
208445e4fd26SMartin KaFai Lau 		}
208545e4fd26SMartin KaFai Lau 	}
208645e4fd26SMartin KaFai Lau }
208745e4fd26SMartin KaFai Lau 
208845e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
208945e4fd26SMartin KaFai Lau 			       struct sk_buff *skb, u32 mtu)
209045e4fd26SMartin KaFai Lau {
209145e4fd26SMartin KaFai Lau 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
20921da177e4SLinus Torvalds }
20931da177e4SLinus Torvalds 
209442ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2095e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
209681aded24SDavid S. Miller {
209781aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
209881aded24SDavid S. Miller 	struct dst_entry *dst;
209981aded24SDavid S. Miller 	struct flowi6 fl6;
210081aded24SDavid S. Miller 
210181aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
210281aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
21031b3c61dcSLorenzo Colitti 	fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
210481aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
210581aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
21066502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
2107e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = uid;
210881aded24SDavid S. Miller 
210981aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
211081aded24SDavid S. Miller 	if (!dst->error)
211145e4fd26SMartin KaFai Lau 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
211281aded24SDavid S. Miller 	dst_release(dst);
211381aded24SDavid S. Miller }
211481aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
211581aded24SDavid S. Miller 
211681aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
211781aded24SDavid S. Miller {
211833c162a9SMartin KaFai Lau 	struct dst_entry *dst;
211933c162a9SMartin KaFai Lau 
212081aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
2121e2d118a1SLorenzo Colitti 			sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid);
212233c162a9SMartin KaFai Lau 
212333c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
212433c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
212533c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
212633c162a9SMartin KaFai Lau 		return;
212733c162a9SMartin KaFai Lau 
212833c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
212933c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
213033c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
213133c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
213281aded24SDavid S. Miller }
213381aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
213481aded24SDavid S. Miller 
2135b55b76b2SDuan Jiong /* Handle redirects */
2136b55b76b2SDuan Jiong struct ip6rd_flowi {
2137b55b76b2SDuan Jiong 	struct flowi6 fl6;
2138b55b76b2SDuan Jiong 	struct in6_addr gateway;
2139b55b76b2SDuan Jiong };
2140b55b76b2SDuan Jiong 
2141b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
2142b55b76b2SDuan Jiong 					     struct fib6_table *table,
2143b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2144b55b76b2SDuan Jiong 					     int flags)
2145b55b76b2SDuan Jiong {
2146b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
21472b760fcfSWei Wang 	struct rt6_info *rt, *rt_cache;
2148b55b76b2SDuan Jiong 	struct fib6_node *fn;
2149b55b76b2SDuan Jiong 
2150b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
215167c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2152b55b76b2SDuan Jiong 	 *
2153b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2154b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2155b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2156b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2157b55b76b2SDuan Jiong 	 * routes.
2158b55b76b2SDuan Jiong 	 */
2159b55b76b2SDuan Jiong 
216066f5d6ceSWei Wang 	rcu_read_lock();
2161b55b76b2SDuan Jiong 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2162b55b76b2SDuan Jiong restart:
216366f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
2164*8067bb8cSIdo Schimmel 		if (rt->rt6i_nh_flags & RTNH_F_DEAD)
2165*8067bb8cSIdo Schimmel 			continue;
2166b55b76b2SDuan Jiong 		if (rt6_check_expired(rt))
2167b55b76b2SDuan Jiong 			continue;
2168b55b76b2SDuan Jiong 		if (rt->dst.error)
2169b55b76b2SDuan Jiong 			break;
2170b55b76b2SDuan Jiong 		if (!(rt->rt6i_flags & RTF_GATEWAY))
2171b55b76b2SDuan Jiong 			continue;
2172b55b76b2SDuan Jiong 		if (fl6->flowi6_oif != rt->dst.dev->ifindex)
2173b55b76b2SDuan Jiong 			continue;
21742b760fcfSWei Wang 		/* rt_cache's gateway might be different from its 'parent'
21752b760fcfSWei Wang 		 * in the case of an ip redirect.
21762b760fcfSWei Wang 		 * So we keep searching in the exception table if the gateway
21772b760fcfSWei Wang 		 * is different.
21782b760fcfSWei Wang 		 */
21792b760fcfSWei Wang 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) {
21802b760fcfSWei Wang 			rt_cache = rt6_find_cached_rt(rt,
21812b760fcfSWei Wang 						      &fl6->daddr,
21822b760fcfSWei Wang 						      &fl6->saddr);
21832b760fcfSWei Wang 			if (rt_cache &&
21842b760fcfSWei Wang 			    ipv6_addr_equal(&rdfl->gateway,
21852b760fcfSWei Wang 					    &rt_cache->rt6i_gateway)) {
21862b760fcfSWei Wang 				rt = rt_cache;
21872b760fcfSWei Wang 				break;
21882b760fcfSWei Wang 			}
2189b55b76b2SDuan Jiong 			continue;
21902b760fcfSWei Wang 		}
2191b55b76b2SDuan Jiong 		break;
2192b55b76b2SDuan Jiong 	}
2193b55b76b2SDuan Jiong 
2194b55b76b2SDuan Jiong 	if (!rt)
2195b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
2196b55b76b2SDuan Jiong 	else if (rt->dst.error) {
2197b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
2198b0a1ba59SMartin KaFai Lau 		goto out;
2199b0a1ba59SMartin KaFai Lau 	}
2200b0a1ba59SMartin KaFai Lau 
2201b0a1ba59SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
2202a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2203a3c00e46SMartin KaFai Lau 		if (fn)
2204a3c00e46SMartin KaFai Lau 			goto restart;
2205b55b76b2SDuan Jiong 	}
2206a3c00e46SMartin KaFai Lau 
2207b0a1ba59SMartin KaFai Lau out:
2208d3843fe5SWei Wang 	ip6_hold_safe(net, &rt, true);
2209b55b76b2SDuan Jiong 
221066f5d6ceSWei Wang 	rcu_read_unlock();
2211b55b76b2SDuan Jiong 
2212b65f164dSPaolo Abeni 	trace_fib6_table_lookup(net, rt, table, fl6);
2213b55b76b2SDuan Jiong 	return rt;
2214b55b76b2SDuan Jiong };
2215b55b76b2SDuan Jiong 
2216b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
2217b55b76b2SDuan Jiong 					const struct flowi6 *fl6,
2218b55b76b2SDuan Jiong 					const struct in6_addr *gateway)
2219b55b76b2SDuan Jiong {
2220b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2221b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
2222b55b76b2SDuan Jiong 
2223b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
2224b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
2225b55b76b2SDuan Jiong 
2226b55b76b2SDuan Jiong 	return fib6_rule_lookup(net, &rdfl.fl6,
2227b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
2228b55b76b2SDuan Jiong }
2229b55b76b2SDuan Jiong 
2230e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2231e2d118a1SLorenzo Colitti 		  kuid_t uid)
22323a5ad2eeSDavid S. Miller {
22333a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
22343a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
22353a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
22363a5ad2eeSDavid S. Miller 
22373a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2238e374c618SJulian Anastasov 	fl6.flowi6_iif = LOOPBACK_IFINDEX;
22393a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
22403a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
22413a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
22423a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
22436502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
2244e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = uid;
22453a5ad2eeSDavid S. Miller 
2246b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
22476700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
22483a5ad2eeSDavid S. Miller 	dst_release(dst);
22493a5ad2eeSDavid S. Miller }
22503a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
22513a5ad2eeSDavid S. Miller 
2252c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
2253c92a59ecSDuan Jiong 			    u32 mark)
2254c92a59ecSDuan Jiong {
2255c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2256c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2257c92a59ecSDuan Jiong 	struct dst_entry *dst;
2258c92a59ecSDuan Jiong 	struct flowi6 fl6;
2259c92a59ecSDuan Jiong 
2260c92a59ecSDuan Jiong 	memset(&fl6, 0, sizeof(fl6));
2261e374c618SJulian Anastasov 	fl6.flowi6_iif = LOOPBACK_IFINDEX;
2262c92a59ecSDuan Jiong 	fl6.flowi6_oif = oif;
2263c92a59ecSDuan Jiong 	fl6.flowi6_mark = mark;
2264c92a59ecSDuan Jiong 	fl6.daddr = msg->dest;
2265c92a59ecSDuan Jiong 	fl6.saddr = iph->daddr;
2266e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sock_net_uid(net, NULL);
2267c92a59ecSDuan Jiong 
2268b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &iph->saddr);
2269c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
2270c92a59ecSDuan Jiong 	dst_release(dst);
2271c92a59ecSDuan Jiong }
2272c92a59ecSDuan Jiong 
22733a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
22743a5ad2eeSDavid S. Miller {
2275e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2276e2d118a1SLorenzo Colitti 		     sk->sk_uid);
22773a5ad2eeSDavid S. Miller }
22783a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
22793a5ad2eeSDavid S. Miller 
22800dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
22811da177e4SLinus Torvalds {
22820dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
22830dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
22840dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
22850dbaee3bSDavid S. Miller 
22861da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
22871da177e4SLinus Torvalds 
22885578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
22895578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
22901da177e4SLinus Torvalds 
22911da177e4SLinus Torvalds 	/*
22921da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
22931da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
22941da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
22951da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
22961da177e4SLinus Torvalds 	 */
22971da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
22981da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
22991da177e4SLinus Torvalds 	return mtu;
23001da177e4SLinus Torvalds }
23011da177e4SLinus Torvalds 
2302ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
2303d33e4553SDavid S. Miller {
23044b32b5adSMartin KaFai Lau 	const struct rt6_info *rt = (const struct rt6_info *)dst;
23054b32b5adSMartin KaFai Lau 	unsigned int mtu = rt->rt6i_pmtu;
2306d33e4553SDavid S. Miller 	struct inet6_dev *idev;
2307618f9bc7SSteffen Klassert 
2308618f9bc7SSteffen Klassert 	if (mtu)
230930f78d8eSEric Dumazet 		goto out;
2310618f9bc7SSteffen Klassert 
23114b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
23124b32b5adSMartin KaFai Lau 	if (mtu)
23134b32b5adSMartin KaFai Lau 		goto out;
23144b32b5adSMartin KaFai Lau 
2315618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
2316d33e4553SDavid S. Miller 
2317d33e4553SDavid S. Miller 	rcu_read_lock();
2318d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
2319d33e4553SDavid S. Miller 	if (idev)
2320d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
2321d33e4553SDavid S. Miller 	rcu_read_unlock();
2322d33e4553SDavid S. Miller 
232330f78d8eSEric Dumazet out:
232414972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
232514972cbdSRoopa Prabhu 
232614972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
2327d33e4553SDavid S. Miller }
2328d33e4553SDavid S. Miller 
23293b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
233087a11578SDavid S. Miller 				  struct flowi6 *fl6)
23311da177e4SLinus Torvalds {
233287a11578SDavid S. Miller 	struct dst_entry *dst;
23331da177e4SLinus Torvalds 	struct rt6_info *rt;
23341da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
2335c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
23361da177e4SLinus Torvalds 
233738308473SDavid S. Miller 	if (unlikely(!idev))
2338122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
23391da177e4SLinus Torvalds 
2340ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
234138308473SDavid S. Miller 	if (unlikely(!rt)) {
23421da177e4SLinus Torvalds 		in6_dev_put(idev);
234387a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
23441da177e4SLinus Torvalds 		goto out;
23451da177e4SLinus Torvalds 	}
23461da177e4SLinus Torvalds 
23478e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
2348588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
23498e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
2350550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
235187a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
23528e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
23538e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
235414edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
23551da177e4SLinus Torvalds 
23564c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
2357587fea74SWei Wang 	 * do proper release of the net_device
2358587fea74SWei Wang 	 */
2359587fea74SWei Wang 	rt6_uncached_list_add(rt);
236081eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
23611da177e4SLinus Torvalds 
236287a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
236387a11578SDavid S. Miller 
23641da177e4SLinus Torvalds out:
236587a11578SDavid S. Miller 	return dst;
23661da177e4SLinus Torvalds }
23671da177e4SLinus Torvalds 
2368569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
23691da177e4SLinus Torvalds {
237086393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
23717019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
23727019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
23737019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
23747019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
23757019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
2376fc66f95cSEric Dumazet 	int entries;
23771da177e4SLinus Torvalds 
2378fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
237949a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
2380fc66f95cSEric Dumazet 	    entries <= rt_max_size)
23811da177e4SLinus Torvalds 		goto out;
23821da177e4SLinus Torvalds 
23836891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
238414956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
2385fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
2386fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
23877019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
23881da177e4SLinus Torvalds out:
23897019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
2390fc66f95cSEric Dumazet 	return entries > rt_max_size;
23911da177e4SLinus Torvalds }
23921da177e4SLinus Torvalds 
2393e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc,
2394e715b6d3SFlorian Westphal 			       const struct fib6_config *cfg)
2395e715b6d3SFlorian Westphal {
23966670e152SStephen Hemminger 	struct net *net = cfg->fc_nlinfo.nl_net;
2397c3a8d947SDaniel Borkmann 	bool ecn_ca = false;
2398e715b6d3SFlorian Westphal 	struct nlattr *nla;
2399e715b6d3SFlorian Westphal 	int remaining;
2400e715b6d3SFlorian Westphal 	u32 *mp;
2401e715b6d3SFlorian Westphal 
240263159f29SIan Morris 	if (!cfg->fc_mx)
2403e715b6d3SFlorian Westphal 		return 0;
2404e715b6d3SFlorian Westphal 
2405e715b6d3SFlorian Westphal 	mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
2406e715b6d3SFlorian Westphal 	if (unlikely(!mp))
2407e715b6d3SFlorian Westphal 		return -ENOMEM;
2408e715b6d3SFlorian Westphal 
2409e715b6d3SFlorian Westphal 	nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
2410e715b6d3SFlorian Westphal 		int type = nla_type(nla);
2411ea697639SDaniel Borkmann 		u32 val;
2412ea697639SDaniel Borkmann 
24131bb14807SDaniel Borkmann 		if (!type)
24141bb14807SDaniel Borkmann 			continue;
2415e715b6d3SFlorian Westphal 		if (unlikely(type > RTAX_MAX))
2416e715b6d3SFlorian Westphal 			goto err;
24171bb14807SDaniel Borkmann 
2418ea697639SDaniel Borkmann 		if (type == RTAX_CC_ALGO) {
2419ea697639SDaniel Borkmann 			char tmp[TCP_CA_NAME_MAX];
2420e715b6d3SFlorian Westphal 
2421ea697639SDaniel Borkmann 			nla_strlcpy(tmp, nla, sizeof(tmp));
24226670e152SStephen Hemminger 			val = tcp_ca_get_key_by_name(net, tmp, &ecn_ca);
2423ea697639SDaniel Borkmann 			if (val == TCP_CA_UNSPEC)
2424ea697639SDaniel Borkmann 				goto err;
2425ea697639SDaniel Borkmann 		} else {
2426ea697639SDaniel Borkmann 			val = nla_get_u32(nla);
2427ea697639SDaniel Borkmann 		}
2428626abd59SPaolo Abeni 		if (type == RTAX_HOPLIMIT && val > 255)
2429626abd59SPaolo Abeni 			val = 255;
2430b8d3e416SDaniel Borkmann 		if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
2431b8d3e416SDaniel Borkmann 			goto err;
2432ea697639SDaniel Borkmann 
2433ea697639SDaniel Borkmann 		mp[type - 1] = val;
2434e715b6d3SFlorian Westphal 		__set_bit(type - 1, mxc->mx_valid);
2435e715b6d3SFlorian Westphal 	}
2436e715b6d3SFlorian Westphal 
2437c3a8d947SDaniel Borkmann 	if (ecn_ca) {
2438c3a8d947SDaniel Borkmann 		__set_bit(RTAX_FEATURES - 1, mxc->mx_valid);
2439c3a8d947SDaniel Borkmann 		mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
2440c3a8d947SDaniel Borkmann 	}
2441e715b6d3SFlorian Westphal 
2442c3a8d947SDaniel Borkmann 	mxc->mx = mp;
2443e715b6d3SFlorian Westphal 	return 0;
2444e715b6d3SFlorian Westphal  err:
2445e715b6d3SFlorian Westphal 	kfree(mp);
2446e715b6d3SFlorian Westphal 	return -EINVAL;
2447e715b6d3SFlorian Westphal }
24481da177e4SLinus Torvalds 
24498c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net,
24508c14586fSDavid Ahern 					    struct fib6_config *cfg,
24518c14586fSDavid Ahern 					    const struct in6_addr *gw_addr)
24528c14586fSDavid Ahern {
24538c14586fSDavid Ahern 	struct flowi6 fl6 = {
24548c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
24558c14586fSDavid Ahern 		.daddr = *gw_addr,
24568c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
24578c14586fSDavid Ahern 	};
24588c14586fSDavid Ahern 	struct fib6_table *table;
24598c14586fSDavid Ahern 	struct rt6_info *rt;
2460d5d32e4bSDavid Ahern 	int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE;
24618c14586fSDavid Ahern 
24628c14586fSDavid Ahern 	table = fib6_get_table(net, cfg->fc_table);
24638c14586fSDavid Ahern 	if (!table)
24648c14586fSDavid Ahern 		return NULL;
24658c14586fSDavid Ahern 
24668c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
24678c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
24688c14586fSDavid Ahern 
24698c14586fSDavid Ahern 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags);
24708c14586fSDavid Ahern 
24718c14586fSDavid Ahern 	/* if table lookup failed, fall back to full lookup */
24728c14586fSDavid Ahern 	if (rt == net->ipv6.ip6_null_entry) {
24738c14586fSDavid Ahern 		ip6_rt_put(rt);
24748c14586fSDavid Ahern 		rt = NULL;
24758c14586fSDavid Ahern 	}
24768c14586fSDavid Ahern 
24778c14586fSDavid Ahern 	return rt;
24788c14586fSDavid Ahern }
24798c14586fSDavid Ahern 
2480333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg,
2481333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
24821da177e4SLinus Torvalds {
24835578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
24841da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
24851da177e4SLinus Torvalds 	struct net_device *dev = NULL;
24861da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
2487c71099acSThomas Graf 	struct fib6_table *table;
24881da177e4SLinus Torvalds 	int addr_type;
24898c5b83f0SRoopa Prabhu 	int err = -EINVAL;
24901da177e4SLinus Torvalds 
2491557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
2492d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
2493d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
2494557c44beSDavid Ahern 		goto out;
2495d5d531cbSDavid Ahern 	}
2496557c44beSDavid Ahern 
24972ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
24982ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
24992ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
25002ea2352eSWei Wang 		goto out;
25012ea2352eSWei Wang 	}
25022ea2352eSWei Wang 
2503d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
2504d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
25058c5b83f0SRoopa Prabhu 		goto out;
2506d5d531cbSDavid Ahern 	}
2507d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
2508d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
2509d5d531cbSDavid Ahern 		goto out;
2510d5d531cbSDavid Ahern 	}
25111da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
2512d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
2513d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
2514d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
25158c5b83f0SRoopa Prabhu 		goto out;
2516d5d531cbSDavid Ahern 	}
25171da177e4SLinus Torvalds #endif
251886872cb5SThomas Graf 	if (cfg->fc_ifindex) {
25191da177e4SLinus Torvalds 		err = -ENODEV;
25205578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
25211da177e4SLinus Torvalds 		if (!dev)
25221da177e4SLinus Torvalds 			goto out;
25231da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
25241da177e4SLinus Torvalds 		if (!idev)
25251da177e4SLinus Torvalds 			goto out;
25261da177e4SLinus Torvalds 	}
25271da177e4SLinus Torvalds 
252886872cb5SThomas Graf 	if (cfg->fc_metric == 0)
252986872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
25301da177e4SLinus Torvalds 
2531c71099acSThomas Graf 	err = -ENOBUFS;
253238308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
2533d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
2534d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
253538308473SDavid S. Miller 		if (!table) {
2536f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
2537d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
2538d71314b4SMatti Vaittinen 		}
2539d71314b4SMatti Vaittinen 	} else {
2540d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
2541d71314b4SMatti Vaittinen 	}
254238308473SDavid S. Miller 
254338308473SDavid S. Miller 	if (!table)
2544c71099acSThomas Graf 		goto out;
2545c71099acSThomas Graf 
2546ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, NULL,
2547ad706862SMartin KaFai Lau 			   (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT);
25481da177e4SLinus Torvalds 
254938308473SDavid S. Miller 	if (!rt) {
25501da177e4SLinus Torvalds 		err = -ENOMEM;
25511da177e4SLinus Torvalds 		goto out;
25521da177e4SLinus Torvalds 	}
25531da177e4SLinus Torvalds 
25541716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
25551716a961SGao feng 		rt6_set_expires(rt, jiffies +
25561716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
25571716a961SGao feng 	else
25581716a961SGao feng 		rt6_clean_expires(rt);
25591da177e4SLinus Torvalds 
256086872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
256186872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
256286872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
256386872cb5SThomas Graf 
256486872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
25651da177e4SLinus Torvalds 
25661da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
2567d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
2568ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
2569ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
25701da177e4SLinus Torvalds 	else
2571d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
25721da177e4SLinus Torvalds 
2573d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
25741da177e4SLinus Torvalds 
257519e42e45SRoopa Prabhu 	if (cfg->fc_encap) {
257619e42e45SRoopa Prabhu 		struct lwtunnel_state *lwtstate;
257719e42e45SRoopa Prabhu 
257830357d7dSDavid Ahern 		err = lwtunnel_build_state(cfg->fc_encap_type,
2579127eb7cdSTom Herbert 					   cfg->fc_encap, AF_INET6, cfg,
25809ae28727SDavid Ahern 					   &lwtstate, extack);
258119e42e45SRoopa Prabhu 		if (err)
258219e42e45SRoopa Prabhu 			goto out;
258361adedf3SJiri Benc 		rt->dst.lwtstate = lwtstate_get(lwtstate);
258461adedf3SJiri Benc 		if (lwtunnel_output_redirect(rt->dst.lwtstate)) {
258561adedf3SJiri Benc 			rt->dst.lwtstate->orig_output = rt->dst.output;
258661adedf3SJiri Benc 			rt->dst.output = lwtunnel_output;
258719e42e45SRoopa Prabhu 		}
258861adedf3SJiri Benc 		if (lwtunnel_input_redirect(rt->dst.lwtstate)) {
258961adedf3SJiri Benc 			rt->dst.lwtstate->orig_input = rt->dst.input;
259061adedf3SJiri Benc 			rt->dst.input = lwtunnel_input;
259125368623STom Herbert 		}
259225368623STom Herbert 	}
259319e42e45SRoopa Prabhu 
259486872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
259586872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
2596afc4eef8SMartin KaFai Lau 	if (rt->rt6i_dst.plen == 128)
259711d53b49SDavid S. Miller 		rt->dst.flags |= DST_HOST;
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
260086872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
260186872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
26021da177e4SLinus Torvalds #endif
26031da177e4SLinus Torvalds 
260486872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
26051da177e4SLinus Torvalds 
26061da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
26071da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
26081da177e4SLinus Torvalds 	 */
260986872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
261038308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
261138308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
261238308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
26131da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
26145578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
26151da177e4SLinus Torvalds 			if (dev) {
26161da177e4SLinus Torvalds 				dev_put(dev);
26171da177e4SLinus Torvalds 				in6_dev_put(idev);
26181da177e4SLinus Torvalds 			}
26195578689aSDaniel Lezcano 			dev = net->loopback_dev;
26201da177e4SLinus Torvalds 			dev_hold(dev);
26211da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
26221da177e4SLinus Torvalds 			if (!idev) {
26231da177e4SLinus Torvalds 				err = -ENODEV;
26241da177e4SLinus Torvalds 				goto out;
26251da177e4SLinus Torvalds 			}
26261da177e4SLinus Torvalds 		}
26271da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
2628ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
2629ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
2630ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
2631ede2059dSEric W. Biederman 			rt->dst.output = dst_discard_out;
26327150aedeSKamala R 			rt->dst.input = dst_discard;
2633ef2c7d7bSNicolas Dichtel 			break;
2634ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
2635ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
26367150aedeSKamala R 			rt->dst.output = ip6_pkt_prohibit_out;
26377150aedeSKamala R 			rt->dst.input = ip6_pkt_prohibit;
2638ef2c7d7bSNicolas Dichtel 			break;
2639b4949ab2SNicolas Dichtel 		case RTN_THROW:
26400315e382SNikola Forró 		case RTN_UNREACHABLE:
2641ef2c7d7bSNicolas Dichtel 		default:
26427150aedeSKamala R 			rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
26430315e382SNikola Forró 					: (cfg->fc_type == RTN_UNREACHABLE)
26440315e382SNikola Forró 					? -EHOSTUNREACH : -ENETUNREACH;
26457150aedeSKamala R 			rt->dst.output = ip6_pkt_discard_out;
26467150aedeSKamala R 			rt->dst.input = ip6_pkt_discard;
2647ef2c7d7bSNicolas Dichtel 			break;
2648ef2c7d7bSNicolas Dichtel 		}
26491da177e4SLinus Torvalds 		goto install_route;
26501da177e4SLinus Torvalds 	}
26511da177e4SLinus Torvalds 
265286872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
2653b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
26541da177e4SLinus Torvalds 		int gwa_type;
26551da177e4SLinus Torvalds 
265686872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
2657330567b7SFlorian Westphal 		gwa_type = ipv6_addr_type(gw_addr);
265848ed7b26SFlorian Westphal 
265948ed7b26SFlorian Westphal 		/* if gw_addr is local we will fail to detect this in case
266048ed7b26SFlorian Westphal 		 * address is still TENTATIVE (DAD in progress). rt6_lookup()
266148ed7b26SFlorian Westphal 		 * will return already-added prefix route via interface that
266248ed7b26SFlorian Westphal 		 * prefix route was assigned to, which might be non-loopback.
266348ed7b26SFlorian Westphal 		 */
266448ed7b26SFlorian Westphal 		err = -EINVAL;
2665330567b7SFlorian Westphal 		if (ipv6_chk_addr_and_flags(net, gw_addr,
2666330567b7SFlorian Westphal 					    gwa_type & IPV6_ADDR_LINKLOCAL ?
2667d5d531cbSDavid Ahern 					    dev : NULL, 0, 0)) {
2668d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
266948ed7b26SFlorian Westphal 			goto out;
2670d5d531cbSDavid Ahern 		}
26714e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
26721da177e4SLinus Torvalds 
26731da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
26748c14586fSDavid Ahern 			struct rt6_info *grt = NULL;
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
26771da177e4SLinus Torvalds 			   addresses as nexthop address.
26781da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
26791da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
26801da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
26811da177e4SLinus Torvalds 			   some exceptions. --ANK
268296d5822cSErik Nordmark 			   We allow IPv4-mapped nexthops to support RFC4798-type
268396d5822cSErik Nordmark 			   addressing
26841da177e4SLinus Torvalds 			 */
268596d5822cSErik Nordmark 			if (!(gwa_type & (IPV6_ADDR_UNICAST |
2686d5d531cbSDavid Ahern 					  IPV6_ADDR_MAPPED))) {
2687d5d531cbSDavid Ahern 				NL_SET_ERR_MSG(extack,
2688d5d531cbSDavid Ahern 					       "Invalid gateway address");
26891da177e4SLinus Torvalds 				goto out;
2690d5d531cbSDavid Ahern 			}
26911da177e4SLinus Torvalds 
2692a435a07fSVincent Bernat 			if (cfg->fc_table) {
26938c14586fSDavid Ahern 				grt = ip6_nh_lookup_table(net, cfg, gw_addr);
26948c14586fSDavid Ahern 
2695a435a07fSVincent Bernat 				if (grt) {
2696a435a07fSVincent Bernat 					if (grt->rt6i_flags & RTF_GATEWAY ||
2697a435a07fSVincent Bernat 					    (dev && dev != grt->dst.dev)) {
2698a435a07fSVincent Bernat 						ip6_rt_put(grt);
2699a435a07fSVincent Bernat 						grt = NULL;
2700a435a07fSVincent Bernat 					}
2701a435a07fSVincent Bernat 				}
2702a435a07fSVincent Bernat 			}
2703a435a07fSVincent Bernat 
27048c14586fSDavid Ahern 			if (!grt)
27058c14586fSDavid Ahern 				grt = rt6_lookup(net, gw_addr, NULL,
27068c14586fSDavid Ahern 						 cfg->fc_ifindex, 1);
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
270938308473SDavid S. Miller 			if (!grt)
27101da177e4SLinus Torvalds 				goto out;
27111da177e4SLinus Torvalds 			if (dev) {
2712d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
271394e187c0SAmerigo Wang 					ip6_rt_put(grt);
27141da177e4SLinus Torvalds 					goto out;
27151da177e4SLinus Torvalds 				}
27161da177e4SLinus Torvalds 			} else {
2717d1918542SDavid S. Miller 				dev = grt->dst.dev;
27181da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
27191da177e4SLinus Torvalds 				dev_hold(dev);
27201da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
27211da177e4SLinus Torvalds 			}
27221da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
27231da177e4SLinus Torvalds 				err = 0;
272494e187c0SAmerigo Wang 			ip6_rt_put(grt);
27251da177e4SLinus Torvalds 
27261da177e4SLinus Torvalds 			if (err)
27271da177e4SLinus Torvalds 				goto out;
27281da177e4SLinus Torvalds 		}
27291da177e4SLinus Torvalds 		err = -EINVAL;
2730d5d531cbSDavid Ahern 		if (!dev) {
2731d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Egress device not specified");
27321da177e4SLinus Torvalds 			goto out;
2733d5d531cbSDavid Ahern 		} else if (dev->flags & IFF_LOOPBACK) {
2734d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack,
2735d5d531cbSDavid Ahern 				       "Egress device can not be loopback device for this route");
2736d5d531cbSDavid Ahern 			goto out;
2737d5d531cbSDavid Ahern 		}
27381da177e4SLinus Torvalds 	}
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds 	err = -ENODEV;
274138308473SDavid S. Miller 	if (!dev)
27421da177e4SLinus Torvalds 		goto out;
27431da177e4SLinus Torvalds 
2744c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
2745c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
2746d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
2747c3968a85SDaniel Walter 			err = -EINVAL;
2748c3968a85SDaniel Walter 			goto out;
2749c3968a85SDaniel Walter 		}
27504e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
2751c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
2752c3968a85SDaniel Walter 	} else
2753c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2754c3968a85SDaniel Walter 
275586872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
27561da177e4SLinus Torvalds 
27571da177e4SLinus Torvalds install_route:
27585609b80aSIdo Schimmel 	if (!(rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
27595609b80aSIdo Schimmel 	    !netif_carrier_ok(dev))
27605609b80aSIdo Schimmel 		rt->rt6i_nh_flags |= RTNH_F_LINKDOWN;
2761d8d1f30bSChangli Gao 	rt->dst.dev = dev;
27621da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2763c71099acSThomas Graf 	rt->rt6i_table = table;
276463152fc0SDaniel Lezcano 
2765c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
276663152fc0SDaniel Lezcano 
27678c5b83f0SRoopa Prabhu 	return rt;
27681da177e4SLinus Torvalds out:
27691da177e4SLinus Torvalds 	if (dev)
27701da177e4SLinus Torvalds 		dev_put(dev);
27711da177e4SLinus Torvalds 	if (idev)
27721da177e4SLinus Torvalds 		in6_dev_put(idev);
2773587fea74SWei Wang 	if (rt)
2774587fea74SWei Wang 		dst_release_immediate(&rt->dst);
27756b9ea5a6SRoopa Prabhu 
27768c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
27776b9ea5a6SRoopa Prabhu }
27786b9ea5a6SRoopa Prabhu 
2779333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg,
2780333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
27816b9ea5a6SRoopa Prabhu {
27826b9ea5a6SRoopa Prabhu 	struct mx6_config mxc = { .mx = NULL, };
27838c5b83f0SRoopa Prabhu 	struct rt6_info *rt;
27846b9ea5a6SRoopa Prabhu 	int err;
27856b9ea5a6SRoopa Prabhu 
2786333c4301SDavid Ahern 	rt = ip6_route_info_create(cfg, extack);
27878c5b83f0SRoopa Prabhu 	if (IS_ERR(rt)) {
27888c5b83f0SRoopa Prabhu 		err = PTR_ERR(rt);
27898c5b83f0SRoopa Prabhu 		rt = NULL;
27906b9ea5a6SRoopa Prabhu 		goto out;
27918c5b83f0SRoopa Prabhu 	}
27926b9ea5a6SRoopa Prabhu 
27936b9ea5a6SRoopa Prabhu 	err = ip6_convert_metrics(&mxc, cfg);
27946b9ea5a6SRoopa Prabhu 	if (err)
27956b9ea5a6SRoopa Prabhu 		goto out;
27966b9ea5a6SRoopa Prabhu 
2797333c4301SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack);
27986b9ea5a6SRoopa Prabhu 
27996b9ea5a6SRoopa Prabhu 	kfree(mxc.mx);
28006b9ea5a6SRoopa Prabhu 
28016b9ea5a6SRoopa Prabhu 	return err;
28026b9ea5a6SRoopa Prabhu out:
2803587fea74SWei Wang 	if (rt)
2804587fea74SWei Wang 		dst_release_immediate(&rt->dst);
28056b9ea5a6SRoopa Prabhu 
28061da177e4SLinus Torvalds 	return err;
28071da177e4SLinus Torvalds }
28081da177e4SLinus Torvalds 
280986872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
28101da177e4SLinus Torvalds {
28111da177e4SLinus Torvalds 	int err;
2812c71099acSThomas Graf 	struct fib6_table *table;
2813d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
28141da177e4SLinus Torvalds 
2815a4c2fd7fSWei Wang 	if (rt == net->ipv6.ip6_null_entry) {
28166825a26cSGao feng 		err = -ENOENT;
28176825a26cSGao feng 		goto out;
28186825a26cSGao feng 	}
28196c813a72SPatrick McHardy 
2820c71099acSThomas Graf 	table = rt->rt6i_table;
282166f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
282286872cb5SThomas Graf 	err = fib6_del(rt, info);
282366f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
28241da177e4SLinus Torvalds 
28256825a26cSGao feng out:
282694e187c0SAmerigo Wang 	ip6_rt_put(rt);
28271da177e4SLinus Torvalds 	return err;
28281da177e4SLinus Torvalds }
28291da177e4SLinus Torvalds 
2830e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
2831e0a1ad73SThomas Graf {
28324d1169c1SDenis V. Lunev 	struct nl_info info = {
2833d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
28344d1169c1SDenis V. Lunev 	};
2835528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
2836e0a1ad73SThomas Graf }
2837e0a1ad73SThomas Graf 
28380ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg)
28390ae81335SDavid Ahern {
28400ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
2841e3330039SWANG Cong 	struct net *net = info->nl_net;
284216a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
28430ae81335SDavid Ahern 	struct fib6_table *table;
2844e3330039SWANG Cong 	int err = -ENOENT;
28450ae81335SDavid Ahern 
2846e3330039SWANG Cong 	if (rt == net->ipv6.ip6_null_entry)
2847e3330039SWANG Cong 		goto out_put;
28480ae81335SDavid Ahern 	table = rt->rt6i_table;
284966f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
28500ae81335SDavid Ahern 
28510ae81335SDavid Ahern 	if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) {
28520ae81335SDavid Ahern 		struct rt6_info *sibling, *next_sibling;
28530ae81335SDavid Ahern 
285416a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
285516a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
285616a16cd3SDavid Ahern 		if (skb) {
285716a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
285816a16cd3SDavid Ahern 
2859e3330039SWANG Cong 			if (rt6_fill_node(net, skb, rt,
286016a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
286116a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
286216a16cd3SDavid Ahern 				kfree_skb(skb);
286316a16cd3SDavid Ahern 				skb = NULL;
286416a16cd3SDavid Ahern 			} else
286516a16cd3SDavid Ahern 				info->skip_notify = 1;
286616a16cd3SDavid Ahern 		}
286716a16cd3SDavid Ahern 
28680ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
28690ae81335SDavid Ahern 					 &rt->rt6i_siblings,
28700ae81335SDavid Ahern 					 rt6i_siblings) {
28710ae81335SDavid Ahern 			err = fib6_del(sibling, info);
28720ae81335SDavid Ahern 			if (err)
2873e3330039SWANG Cong 				goto out_unlock;
28740ae81335SDavid Ahern 		}
28750ae81335SDavid Ahern 	}
28760ae81335SDavid Ahern 
28770ae81335SDavid Ahern 	err = fib6_del(rt, info);
2878e3330039SWANG Cong out_unlock:
287966f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
2880e3330039SWANG Cong out_put:
28810ae81335SDavid Ahern 	ip6_rt_put(rt);
288216a16cd3SDavid Ahern 
288316a16cd3SDavid Ahern 	if (skb) {
2884e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
288516a16cd3SDavid Ahern 			    info->nlh, gfp_any());
288616a16cd3SDavid Ahern 	}
28870ae81335SDavid Ahern 	return err;
28880ae81335SDavid Ahern }
28890ae81335SDavid Ahern 
2890333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
2891333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
28921da177e4SLinus Torvalds {
28932b760fcfSWei Wang 	struct rt6_info *rt, *rt_cache;
2894c71099acSThomas Graf 	struct fib6_table *table;
28951da177e4SLinus Torvalds 	struct fib6_node *fn;
28961da177e4SLinus Torvalds 	int err = -ESRCH;
28971da177e4SLinus Torvalds 
28985578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
2899d5d531cbSDavid Ahern 	if (!table) {
2900d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
2901c71099acSThomas Graf 		return err;
2902d5d531cbSDavid Ahern 	}
29031da177e4SLinus Torvalds 
290466f5d6ceSWei Wang 	rcu_read_lock();
2905c71099acSThomas Graf 
2906c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
290786872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
290838fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
29092b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
29101da177e4SLinus Torvalds 
29111da177e4SLinus Torvalds 	if (fn) {
291266f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
29132b760fcfSWei Wang 			if (cfg->fc_flags & RTF_CACHE) {
29142b760fcfSWei Wang 				rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
29152b760fcfSWei Wang 							      &cfg->fc_src);
29162b760fcfSWei Wang 				if (!rt_cache)
29171f56a01fSMartin KaFai Lau 					continue;
29182b760fcfSWei Wang 				rt = rt_cache;
29192b760fcfSWei Wang 			}
292086872cb5SThomas Graf 			if (cfg->fc_ifindex &&
2921d1918542SDavid S. Miller 			    (!rt->dst.dev ||
2922d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
29231da177e4SLinus Torvalds 				continue;
292486872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
292586872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
29261da177e4SLinus Torvalds 				continue;
292786872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
29281da177e4SLinus Torvalds 				continue;
2929c2ed1880SMantas M 			if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol)
2930c2ed1880SMantas M 				continue;
2931d3843fe5SWei Wang 			if (!dst_hold_safe(&rt->dst))
2932d3843fe5SWei Wang 				break;
293366f5d6ceSWei Wang 			rcu_read_unlock();
29341da177e4SLinus Torvalds 
29350ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
29360ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
293786872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
29380ae81335SDavid Ahern 
29390ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
29401da177e4SLinus Torvalds 		}
29411da177e4SLinus Torvalds 	}
294266f5d6ceSWei Wang 	rcu_read_unlock();
29431da177e4SLinus Torvalds 
29441da177e4SLinus Torvalds 	return err;
29451da177e4SLinus Torvalds }
29461da177e4SLinus Torvalds 
29476700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
2948a6279458SYOSHIFUJI Hideaki {
2949a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
2950e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
2951e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
2952e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
2953e8599ff4SDavid S. Miller 	struct neighbour *neigh;
295471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
29556e157b6aSDavid S. Miller 	int optlen, on_link;
29566e157b6aSDavid S. Miller 	u8 *lladdr;
2957e8599ff4SDavid S. Miller 
295829a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
295971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
2960e8599ff4SDavid S. Miller 
2961e8599ff4SDavid S. Miller 	if (optlen < 0) {
29626e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
2963e8599ff4SDavid S. Miller 		return;
2964e8599ff4SDavid S. Miller 	}
2965e8599ff4SDavid S. Miller 
296671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
2967e8599ff4SDavid S. Miller 
296871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
29696e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
2970e8599ff4SDavid S. Miller 		return;
2971e8599ff4SDavid S. Miller 	}
2972e8599ff4SDavid S. Miller 
29736e157b6aSDavid S. Miller 	on_link = 0;
297471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
2975e8599ff4SDavid S. Miller 		on_link = 1;
297671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
2977e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
29786e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
2979e8599ff4SDavid S. Miller 		return;
2980e8599ff4SDavid S. Miller 	}
2981e8599ff4SDavid S. Miller 
2982e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
2983e8599ff4SDavid S. Miller 	if (!in6_dev)
2984e8599ff4SDavid S. Miller 		return;
2985e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
2986e8599ff4SDavid S. Miller 		return;
2987e8599ff4SDavid S. Miller 
2988e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
2989e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
2990e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
2991e8599ff4SDavid S. Miller 	 */
2992e8599ff4SDavid S. Miller 
2993f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
2994e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
2995e8599ff4SDavid S. Miller 		return;
2996e8599ff4SDavid S. Miller 	}
29976e157b6aSDavid S. Miller 
29986e157b6aSDavid S. Miller 	lladdr = NULL;
2999e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
3000e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3001e8599ff4SDavid S. Miller 					     skb->dev);
3002e8599ff4SDavid S. Miller 		if (!lladdr) {
3003e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3004e8599ff4SDavid S. Miller 			return;
3005e8599ff4SDavid S. Miller 		}
3006e8599ff4SDavid S. Miller 	}
3007e8599ff4SDavid S. Miller 
30086e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
3009ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
30106e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
30116e157b6aSDavid S. Miller 		return;
30126e157b6aSDavid S. Miller 	}
30136e157b6aSDavid S. Miller 
30146e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
30156e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
30166e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
30176e157b6aSDavid S. Miller 	 */
30180dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
30196e157b6aSDavid S. Miller 
302071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
3021e8599ff4SDavid S. Miller 	if (!neigh)
3022e8599ff4SDavid S. Miller 		return;
3023e8599ff4SDavid S. Miller 
30241da177e4SLinus Torvalds 	/*
30251da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
30261da177e4SLinus Torvalds 	 */
30271da177e4SLinus Torvalds 
3028f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
30291da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
30301da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
30311da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
3032f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
3033f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
30341da177e4SLinus Torvalds 
303583a09abdSMartin KaFai Lau 	nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL);
303638308473SDavid S. Miller 	if (!nrt)
30371da177e4SLinus Torvalds 		goto out;
30381da177e4SLinus Torvalds 
30391da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
30401da177e4SLinus Torvalds 	if (on_link)
30411da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
30421da177e4SLinus Torvalds 
3043b91d5329SXin Long 	nrt->rt6i_protocol = RTPROT_REDIRECT;
30444e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
30451da177e4SLinus Torvalds 
30462b760fcfSWei Wang 	/* No need to remove rt from the exception table if rt is
30472b760fcfSWei Wang 	 * a cached route because rt6_insert_exception() will
30482b760fcfSWei Wang 	 * takes care of it
30492b760fcfSWei Wang 	 */
30502b760fcfSWei Wang 	if (rt6_insert_exception(nrt, rt)) {
30512b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
30522b760fcfSWei Wang 		goto out;
30532b760fcfSWei Wang 	}
30541da177e4SLinus Torvalds 
3055d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
3056d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
305771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
305860592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
30598d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
30608d71740cSTom Tucker 
30611da177e4SLinus Torvalds out:
3062e8599ff4SDavid S. Miller 	neigh_release(neigh);
30636e157b6aSDavid S. Miller }
30646e157b6aSDavid S. Miller 
30651da177e4SLinus Torvalds /*
30661da177e4SLinus Torvalds  *	Misc support functions
30671da177e4SLinus Torvalds  */
30681da177e4SLinus Torvalds 
30694b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
30704b32b5adSMartin KaFai Lau {
30713a2232e9SDavid Miller 	BUG_ON(from->from);
30724b32b5adSMartin KaFai Lau 
30734b32b5adSMartin KaFai Lau 	rt->rt6i_flags &= ~RTF_EXPIRES;
30744b32b5adSMartin KaFai Lau 	dst_hold(&from->dst);
30753a2232e9SDavid Miller 	rt->from = from;
30764b32b5adSMartin KaFai Lau 	dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
30774b32b5adSMartin KaFai Lau }
30784b32b5adSMartin KaFai Lau 
307983a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort)
30801da177e4SLinus Torvalds {
3081d8d1f30bSChangli Gao 	rt->dst.input = ort->dst.input;
3082d8d1f30bSChangli Gao 	rt->dst.output = ort->dst.output;
308383a09abdSMartin KaFai Lau 	rt->rt6i_dst = ort->rt6i_dst;
3084d8d1f30bSChangli Gao 	rt->dst.error = ort->dst.error;
30851da177e4SLinus Torvalds 	rt->rt6i_idev = ort->rt6i_idev;
30861da177e4SLinus Torvalds 	if (rt->rt6i_idev)
30871da177e4SLinus Torvalds 		in6_dev_hold(rt->rt6i_idev);
3088d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
30894e3fd7a0SAlexey Dobriyan 	rt->rt6i_gateway = ort->rt6i_gateway;
30901716a961SGao feng 	rt->rt6i_flags = ort->rt6i_flags;
30911716a961SGao feng 	rt6_set_from(rt, ort);
309283a09abdSMartin KaFai Lau 	rt->rt6i_metric = ort->rt6i_metric;
30931da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
309483a09abdSMartin KaFai Lau 	rt->rt6i_src = ort->rt6i_src;
30951da177e4SLinus Torvalds #endif
309683a09abdSMartin KaFai Lau 	rt->rt6i_prefsrc = ort->rt6i_prefsrc;
3097c71099acSThomas Graf 	rt->rt6i_table = ort->rt6i_table;
309861adedf3SJiri Benc 	rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate);
30991da177e4SLinus Torvalds }
31001da177e4SLinus Torvalds 
310170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
3102efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
3103b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3104830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3105830218c1SDavid Ahern 					   struct net_device *dev)
310670ceb4f5SYOSHIFUJI Hideaki {
3107830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3108830218c1SDavid Ahern 	int ifindex = dev->ifindex;
310970ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
311070ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
3111c71099acSThomas Graf 	struct fib6_table *table;
311270ceb4f5SYOSHIFUJI Hideaki 
3113830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
311438308473SDavid S. Miller 	if (!table)
3115c71099acSThomas Graf 		return NULL;
3116c71099acSThomas Graf 
311766f5d6ceSWei Wang 	rcu_read_lock();
311838fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
311970ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
312070ceb4f5SYOSHIFUJI Hideaki 		goto out;
312170ceb4f5SYOSHIFUJI Hideaki 
312266f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
3123d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
312470ceb4f5SYOSHIFUJI Hideaki 			continue;
312570ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
312670ceb4f5SYOSHIFUJI Hideaki 			continue;
312770ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
312870ceb4f5SYOSHIFUJI Hideaki 			continue;
3129d3843fe5SWei Wang 		ip6_hold_safe(NULL, &rt, false);
313070ceb4f5SYOSHIFUJI Hideaki 		break;
313170ceb4f5SYOSHIFUJI Hideaki 	}
313270ceb4f5SYOSHIFUJI Hideaki out:
313366f5d6ceSWei Wang 	rcu_read_unlock();
313470ceb4f5SYOSHIFUJI Hideaki 	return rt;
313570ceb4f5SYOSHIFUJI Hideaki }
313670ceb4f5SYOSHIFUJI Hideaki 
3137efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
3138b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3139830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3140830218c1SDavid Ahern 					   struct net_device *dev,
314195c96174SEric Dumazet 					   unsigned int pref)
314270ceb4f5SYOSHIFUJI Hideaki {
314386872cb5SThomas Graf 	struct fib6_config cfg = {
3144238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
3145830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
314686872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
314786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
314886872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
3149b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
315015e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
3151efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3152efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
315386872cb5SThomas Graf 	};
315470ceb4f5SYOSHIFUJI Hideaki 
3155830218c1SDavid Ahern 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
31564e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
31574e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
315886872cb5SThomas Graf 
3159e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
3160e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
316186872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
316270ceb4f5SYOSHIFUJI Hideaki 
3163333c4301SDavid Ahern 	ip6_route_add(&cfg, NULL);
316470ceb4f5SYOSHIFUJI Hideaki 
3165830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
316670ceb4f5SYOSHIFUJI Hideaki }
316770ceb4f5SYOSHIFUJI Hideaki #endif
316870ceb4f5SYOSHIFUJI Hideaki 
3169b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
31701da177e4SLinus Torvalds {
3171830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
31721da177e4SLinus Torvalds 	struct rt6_info *rt;
3173c71099acSThomas Graf 	struct fib6_table *table;
31741da177e4SLinus Torvalds 
3175830218c1SDavid Ahern 	table = fib6_get_table(dev_net(dev), tb_id);
317638308473SDavid S. Miller 	if (!table)
3177c71099acSThomas Graf 		return NULL;
31781da177e4SLinus Torvalds 
317966f5d6ceSWei Wang 	rcu_read_lock();
318066f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
3181d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
3182045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
31831da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
31841da177e4SLinus Torvalds 			break;
31851da177e4SLinus Torvalds 	}
31861da177e4SLinus Torvalds 	if (rt)
3187d3843fe5SWei Wang 		ip6_hold_safe(NULL, &rt, false);
318866f5d6ceSWei Wang 	rcu_read_unlock();
31891da177e4SLinus Torvalds 	return rt;
31901da177e4SLinus Torvalds }
31911da177e4SLinus Torvalds 
3192b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
3193ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
3194ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
31951da177e4SLinus Torvalds {
319686872cb5SThomas Graf 	struct fib6_config cfg = {
3197ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
3198238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
319986872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
320086872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
320186872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
3202b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
320315e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
32045578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3205c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
320686872cb5SThomas Graf 	};
32071da177e4SLinus Torvalds 
32084e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
32091da177e4SLinus Torvalds 
3210333c4301SDavid Ahern 	if (!ip6_route_add(&cfg, NULL)) {
3211830218c1SDavid Ahern 		struct fib6_table *table;
3212830218c1SDavid Ahern 
3213830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
3214830218c1SDavid Ahern 		if (table)
3215830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3216830218c1SDavid Ahern 	}
32171da177e4SLinus Torvalds 
32181da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
32191da177e4SLinus Torvalds }
32201da177e4SLinus Torvalds 
3221830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table)
32221da177e4SLinus Torvalds {
32231da177e4SLinus Torvalds 	struct rt6_info *rt;
32241da177e4SLinus Torvalds 
32251da177e4SLinus Torvalds restart:
322666f5d6ceSWei Wang 	rcu_read_lock();
322766f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
32283e8b0ac3SLorenzo Colitti 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
32293e8b0ac3SLorenzo Colitti 		    (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
3230d3843fe5SWei Wang 			if (dst_hold_safe(&rt->dst)) {
323166f5d6ceSWei Wang 				rcu_read_unlock();
3232e0a1ad73SThomas Graf 				ip6_del_rt(rt);
3233d3843fe5SWei Wang 			} else {
323466f5d6ceSWei Wang 				rcu_read_unlock();
3235d3843fe5SWei Wang 			}
32361da177e4SLinus Torvalds 			goto restart;
32371da177e4SLinus Torvalds 		}
32381da177e4SLinus Torvalds 	}
323966f5d6ceSWei Wang 	rcu_read_unlock();
3240830218c1SDavid Ahern 
3241830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3242830218c1SDavid Ahern }
3243830218c1SDavid Ahern 
3244830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
3245830218c1SDavid Ahern {
3246830218c1SDavid Ahern 	struct fib6_table *table;
3247830218c1SDavid Ahern 	struct hlist_head *head;
3248830218c1SDavid Ahern 	unsigned int h;
3249830218c1SDavid Ahern 
3250830218c1SDavid Ahern 	rcu_read_lock();
3251830218c1SDavid Ahern 
3252830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3253830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
3254830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3255830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3256830218c1SDavid Ahern 				__rt6_purge_dflt_routers(table);
3257830218c1SDavid Ahern 		}
3258830218c1SDavid Ahern 	}
3259830218c1SDavid Ahern 
3260830218c1SDavid Ahern 	rcu_read_unlock();
32611da177e4SLinus Torvalds }
32621da177e4SLinus Torvalds 
32635578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
32645578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
326586872cb5SThomas Graf 				 struct fib6_config *cfg)
326686872cb5SThomas Graf {
326786872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
326886872cb5SThomas Graf 
3269ca254490SDavid Ahern 	cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
3270ca254490SDavid Ahern 			 : RT6_TABLE_MAIN;
327186872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
327286872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
327386872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
327486872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
327586872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
327686872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
327786872cb5SThomas Graf 
32785578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
3279f1243c2dSBenjamin Thery 
32804e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
32814e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
32824e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
328386872cb5SThomas Graf }
328486872cb5SThomas Graf 
32855578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
32861da177e4SLinus Torvalds {
328786872cb5SThomas Graf 	struct fib6_config cfg;
32881da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
32891da177e4SLinus Torvalds 	int err;
32901da177e4SLinus Torvalds 
32911da177e4SLinus Torvalds 	switch (cmd) {
32921da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
32931da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
3294af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
32951da177e4SLinus Torvalds 			return -EPERM;
32961da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
32971da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
32981da177e4SLinus Torvalds 		if (err)
32991da177e4SLinus Torvalds 			return -EFAULT;
33001da177e4SLinus Torvalds 
33015578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
330286872cb5SThomas Graf 
33031da177e4SLinus Torvalds 		rtnl_lock();
33041da177e4SLinus Torvalds 		switch (cmd) {
33051da177e4SLinus Torvalds 		case SIOCADDRT:
3306333c4301SDavid Ahern 			err = ip6_route_add(&cfg, NULL);
33071da177e4SLinus Torvalds 			break;
33081da177e4SLinus Torvalds 		case SIOCDELRT:
3309333c4301SDavid Ahern 			err = ip6_route_del(&cfg, NULL);
33101da177e4SLinus Torvalds 			break;
33111da177e4SLinus Torvalds 		default:
33121da177e4SLinus Torvalds 			err = -EINVAL;
33131da177e4SLinus Torvalds 		}
33141da177e4SLinus Torvalds 		rtnl_unlock();
33151da177e4SLinus Torvalds 
33161da177e4SLinus Torvalds 		return err;
33173ff50b79SStephen Hemminger 	}
33181da177e4SLinus Torvalds 
33191da177e4SLinus Torvalds 	return -EINVAL;
33201da177e4SLinus Torvalds }
33211da177e4SLinus Torvalds 
33221da177e4SLinus Torvalds /*
33231da177e4SLinus Torvalds  *	Drop the packet on the floor
33241da177e4SLinus Torvalds  */
33251da177e4SLinus Torvalds 
3326d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
33271da177e4SLinus Torvalds {
3328612f09e8SYOSHIFUJI Hideaki 	int type;
3329adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
3330612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
3331612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
33320660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
333345bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
33343bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
33353bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
3336612f09e8SYOSHIFUJI Hideaki 			break;
3337612f09e8SYOSHIFUJI Hideaki 		}
3338612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
3339612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
33403bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
33413bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
3342612f09e8SYOSHIFUJI Hideaki 		break;
3343612f09e8SYOSHIFUJI Hideaki 	}
33443ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
33451da177e4SLinus Torvalds 	kfree_skb(skb);
33461da177e4SLinus Torvalds 	return 0;
33471da177e4SLinus Torvalds }
33481da177e4SLinus Torvalds 
33499ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
33509ce8ade0SThomas Graf {
3351612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
33529ce8ade0SThomas Graf }
33539ce8ade0SThomas Graf 
3354ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
33551da177e4SLinus Torvalds {
3356adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3357612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
33581da177e4SLinus Torvalds }
33591da177e4SLinus Torvalds 
33609ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
33619ce8ade0SThomas Graf {
3362612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
33639ce8ade0SThomas Graf }
33649ce8ade0SThomas Graf 
3365ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
33669ce8ade0SThomas Graf {
3367adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3368612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
33699ce8ade0SThomas Graf }
33709ce8ade0SThomas Graf 
33711da177e4SLinus Torvalds /*
33721da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
33731da177e4SLinus Torvalds  */
33741da177e4SLinus Torvalds 
33751da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
33761da177e4SLinus Torvalds 				    const struct in6_addr *addr,
33778f031519SDavid S. Miller 				    bool anycast)
33781da177e4SLinus Torvalds {
3379ca254490SDavid Ahern 	u32 tb_id;
3380c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
33814832c30dSDavid Ahern 	struct net_device *dev = idev->dev;
33825f02ce24SDavid Ahern 	struct rt6_info *rt;
33835f02ce24SDavid Ahern 
33845f02ce24SDavid Ahern 	rt = ip6_dst_alloc(net, dev, DST_NOCOUNT);
3385a3300ef4SHannes Frederic Sowa 	if (!rt)
33861da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
33871da177e4SLinus Torvalds 
33881da177e4SLinus Torvalds 	in6_dev_hold(idev);
33891da177e4SLinus Torvalds 
339011d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
3391d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
3392d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
33931da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
33941da177e4SLinus Torvalds 
339594b5e0f9SDavid Ahern 	rt->rt6i_protocol = RTPROT_KERNEL;
33961da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
339758c4fb86SYOSHIFUJI Hideaki 	if (anycast)
339858c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
339958c4fb86SYOSHIFUJI Hideaki 	else
34001da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
34011da177e4SLinus Torvalds 
3402550bab42SJulian Anastasov 	rt->rt6i_gateway  = *addr;
34034e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
34041da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
3405ca254490SDavid Ahern 	tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
3406ca254490SDavid Ahern 	rt->rt6i_table = fib6_get_table(net, tb_id);
34071da177e4SLinus Torvalds 
34081da177e4SLinus Torvalds 	return rt;
34091da177e4SLinus Torvalds }
34101da177e4SLinus Torvalds 
3411c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
3412c3968a85SDaniel Walter struct arg_dev_net_ip {
3413c3968a85SDaniel Walter 	struct net_device *dev;
3414c3968a85SDaniel Walter 	struct net *net;
3415c3968a85SDaniel Walter 	struct in6_addr *addr;
3416c3968a85SDaniel Walter };
3417c3968a85SDaniel Walter 
3418c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
3419c3968a85SDaniel Walter {
3420c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3421c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3422c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3423c3968a85SDaniel Walter 
3424d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
3425c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
3426c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
342760006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
3428c3968a85SDaniel Walter 		/* remove prefsrc entry */
3429c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
343060006a48SWei Wang 		/* need to update cache as well */
343160006a48SWei Wang 		rt6_exceptions_remove_prefsrc(rt);
343260006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
3433c3968a85SDaniel Walter 	}
3434c3968a85SDaniel Walter 	return 0;
3435c3968a85SDaniel Walter }
3436c3968a85SDaniel Walter 
3437c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3438c3968a85SDaniel Walter {
3439c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
3440c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
3441c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
3442c3968a85SDaniel Walter 		.net = net,
3443c3968a85SDaniel Walter 		.addr = &ifp->addr,
3444c3968a85SDaniel Walter 	};
34450c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
3446c3968a85SDaniel Walter }
3447c3968a85SDaniel Walter 
3448be7a010dSDuan Jiong #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
3449be7a010dSDuan Jiong 
3450be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
3451be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
3452be7a010dSDuan Jiong {
3453be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
3454be7a010dSDuan Jiong 
34552b760fcfSWei Wang 	if (((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
3456be7a010dSDuan Jiong 	    ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
3457be7a010dSDuan Jiong 		return -1;
3458be7a010dSDuan Jiong 	}
3459b16cb459SWei Wang 
3460b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
3461b16cb459SWei Wang 	 * This is needed because cached route may have a different
3462b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
3463b16cb459SWei Wang 	 */
3464b16cb459SWei Wang 	rt6_exceptions_clean_tohost(rt, gateway);
3465b16cb459SWei Wang 
3466be7a010dSDuan Jiong 	return 0;
3467be7a010dSDuan Jiong }
3468be7a010dSDuan Jiong 
3469be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3470be7a010dSDuan Jiong {
3471be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
3472be7a010dSDuan Jiong }
3473be7a010dSDuan Jiong 
34742127d95aSIdo Schimmel struct arg_netdev_event {
34752127d95aSIdo Schimmel 	const struct net_device *dev;
34764c981e28SIdo Schimmel 	union {
34772127d95aSIdo Schimmel 		unsigned int nh_flags;
34784c981e28SIdo Schimmel 		unsigned long event;
34794c981e28SIdo Schimmel 	};
34802127d95aSIdo Schimmel };
34812127d95aSIdo Schimmel 
34822127d95aSIdo Schimmel static int fib6_ifup(struct rt6_info *rt, void *p_arg)
34832127d95aSIdo Schimmel {
34842127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
34852127d95aSIdo Schimmel 	const struct net *net = dev_net(arg->dev);
34862127d95aSIdo Schimmel 
34872127d95aSIdo Schimmel 	if (rt != net->ipv6.ip6_null_entry && rt->dst.dev == arg->dev)
34882127d95aSIdo Schimmel 		rt->rt6i_nh_flags &= ~arg->nh_flags;
34892127d95aSIdo Schimmel 
34902127d95aSIdo Schimmel 	return 0;
34912127d95aSIdo Schimmel }
34922127d95aSIdo Schimmel 
34932127d95aSIdo Schimmel void rt6_sync_up(struct net_device *dev, unsigned int nh_flags)
34942127d95aSIdo Schimmel {
34952127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
34962127d95aSIdo Schimmel 		.dev = dev,
34972127d95aSIdo Schimmel 		.nh_flags = nh_flags,
34982127d95aSIdo Schimmel 	};
34992127d95aSIdo Schimmel 
35002127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
35012127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
35022127d95aSIdo Schimmel 
35032127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
35042127d95aSIdo Schimmel }
35052127d95aSIdo Schimmel 
3506a1a22c12SDavid Ahern /* called with write lock held for table with rt */
35074c981e28SIdo Schimmel static int fib6_ifdown(struct rt6_info *rt, void *p_arg)
35081da177e4SLinus Torvalds {
35094c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
35104c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
35114c981e28SIdo Schimmel 	const struct net *net = dev_net(dev);
35128ed67789SDaniel Lezcano 
351327c6fa73SIdo Schimmel 	if (rt->dst.dev != dev || rt == net->ipv6.ip6_null_entry)
351427c6fa73SIdo Schimmel 		return 0;
351527c6fa73SIdo Schimmel 
351627c6fa73SIdo Schimmel 	switch (arg->event) {
351727c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
35181da177e4SLinus Torvalds 		return -1;
351927c6fa73SIdo Schimmel 	case NETDEV_DOWN:
352027c6fa73SIdo Schimmel 		if (rt->rt6i_nsiblings == 0 ||
352127c6fa73SIdo Schimmel 		    !rt->rt6i_idev->cnf.ignore_routes_with_linkdown)
352227c6fa73SIdo Schimmel 			return -1;
352327c6fa73SIdo Schimmel 		rt->rt6i_nh_flags |= RTNH_F_DEAD;
352427c6fa73SIdo Schimmel 		/* fall through */
352527c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
352627c6fa73SIdo Schimmel 		if (rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST))
352727c6fa73SIdo Schimmel 			break;
352827c6fa73SIdo Schimmel 		rt->rt6i_nh_flags |= RTNH_F_LINKDOWN;
352927c6fa73SIdo Schimmel 		break;
35302b241361SIdo Schimmel 	}
3531c159d30cSDavid S. Miller 
35321da177e4SLinus Torvalds 	return 0;
35331da177e4SLinus Torvalds }
35341da177e4SLinus Torvalds 
353527c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
35361da177e4SLinus Torvalds {
35374c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
35388ed67789SDaniel Lezcano 		.dev = dev,
35394c981e28SIdo Schimmel 		.event = event,
35408ed67789SDaniel Lezcano 	};
35418ed67789SDaniel Lezcano 
35424c981e28SIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifdown, &arg);
35434c981e28SIdo Schimmel }
35444c981e28SIdo Schimmel 
35454c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
35464c981e28SIdo Schimmel {
35474c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
35484c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
35494c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
35501da177e4SLinus Torvalds }
35511da177e4SLinus Torvalds 
355295c96174SEric Dumazet struct rt6_mtu_change_arg {
35531da177e4SLinus Torvalds 	struct net_device *dev;
355495c96174SEric Dumazet 	unsigned int mtu;
35551da177e4SLinus Torvalds };
35561da177e4SLinus Torvalds 
35571da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
35581da177e4SLinus Torvalds {
35591da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
35601da177e4SLinus Torvalds 	struct inet6_dev *idev;
35611da177e4SLinus Torvalds 
35621da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
35631da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
35641da177e4SLinus Torvalds 	   We still use this lock to block changes
35651da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
35661da177e4SLinus Torvalds 	*/
35671da177e4SLinus Torvalds 
35681da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
356938308473SDavid S. Miller 	if (!idev)
35701da177e4SLinus Torvalds 		return 0;
35711da177e4SLinus Torvalds 
35721da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
35731da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
35741da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
35751da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
35761da177e4SLinus Torvalds 	 */
35771da177e4SLinus Torvalds 	/*
35781da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
35791da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
35801da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
35811da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
35821da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
35831da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
358467c408cfSAlexander Alemayhu 	   PMTU discovery.
35851da177e4SLinus Torvalds 	 */
3586d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
3587fb56be83SMaciej Żenczykowski 	    dst_metric_raw(&rt->dst, RTAX_MTU) &&
35884b32b5adSMartin KaFai Lau 	    !dst_metric_locked(&rt->dst, RTAX_MTU)) {
3589f5bbe7eeSWei Wang 		spin_lock_bh(&rt6_exception_lock);
35902b760fcfSWei Wang 		if (dst_mtu(&rt->dst) >= arg->mtu ||
3591d8d1f30bSChangli Gao 		    (dst_mtu(&rt->dst) < arg->mtu &&
35924b32b5adSMartin KaFai Lau 		     dst_mtu(&rt->dst) == idev->cnf.mtu6)) {
3593defb3519SDavid S. Miller 			dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
3594566cfd8fSSimon Arlott 		}
3595f5bbe7eeSWei Wang 		rt6_exceptions_update_pmtu(rt, arg->mtu);
3596f5bbe7eeSWei Wang 		spin_unlock_bh(&rt6_exception_lock);
35974b32b5adSMartin KaFai Lau 	}
35981da177e4SLinus Torvalds 	return 0;
35991da177e4SLinus Torvalds }
36001da177e4SLinus Torvalds 
360195c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
36021da177e4SLinus Torvalds {
3603c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
3604c71099acSThomas Graf 		.dev = dev,
3605c71099acSThomas Graf 		.mtu = mtu,
3606c71099acSThomas Graf 	};
36071da177e4SLinus Torvalds 
36080c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
36091da177e4SLinus Torvalds }
36101da177e4SLinus Torvalds 
3611ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
36125176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
361386872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
3614ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
361586872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
361686872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
361751ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
3618c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
361919e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
362019e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
362132bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
3622622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
36233b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
362486872cb5SThomas Graf };
362586872cb5SThomas Graf 
362686872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
3627333c4301SDavid Ahern 			      struct fib6_config *cfg,
3628333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
36291da177e4SLinus Torvalds {
363086872cb5SThomas Graf 	struct rtmsg *rtm;
363186872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
3632c78ba6d6SLubomir Rintel 	unsigned int pref;
363386872cb5SThomas Graf 	int err;
36341da177e4SLinus Torvalds 
3635fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
3636fceb6435SJohannes Berg 			  NULL);
363786872cb5SThomas Graf 	if (err < 0)
363886872cb5SThomas Graf 		goto errout;
36391da177e4SLinus Torvalds 
364086872cb5SThomas Graf 	err = -EINVAL;
364186872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
364286872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
364386872cb5SThomas Graf 
364486872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
364586872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
364686872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
364786872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
364886872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
3649ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
365086872cb5SThomas Graf 
3651ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
3652ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
3653b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
3654b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
365586872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
365686872cb5SThomas Graf 
3657ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
3658ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
3659ab79ad14SMaciej Żenczykowski 
36601f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
36611f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
36621f56a01fSMartin KaFai Lau 
366315e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
366486872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
36653b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
366686872cb5SThomas Graf 
366786872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
366867b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
366986872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
36701da177e4SLinus Torvalds 	}
367186872cb5SThomas Graf 
367286872cb5SThomas Graf 	if (tb[RTA_DST]) {
367386872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
367486872cb5SThomas Graf 
367586872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
367686872cb5SThomas Graf 			goto errout;
367786872cb5SThomas Graf 
367886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
36791da177e4SLinus Torvalds 	}
368086872cb5SThomas Graf 
368186872cb5SThomas Graf 	if (tb[RTA_SRC]) {
368286872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
368386872cb5SThomas Graf 
368486872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
368586872cb5SThomas Graf 			goto errout;
368686872cb5SThomas Graf 
368786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
36881da177e4SLinus Torvalds 	}
368986872cb5SThomas Graf 
3690c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
369167b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
3692c3968a85SDaniel Walter 
369386872cb5SThomas Graf 	if (tb[RTA_OIF])
369486872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
369586872cb5SThomas Graf 
369686872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
369786872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
369886872cb5SThomas Graf 
369986872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
370086872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
370186872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
37021da177e4SLinus Torvalds 	}
370386872cb5SThomas Graf 
370486872cb5SThomas Graf 	if (tb[RTA_TABLE])
370586872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
370686872cb5SThomas Graf 
370751ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
370851ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
370951ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
37109ed59592SDavid Ahern 
37119ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
3712c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
37139ed59592SDavid Ahern 		if (err < 0)
37149ed59592SDavid Ahern 			goto errout;
371551ebd318SNicolas Dichtel 	}
371651ebd318SNicolas Dichtel 
3717c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
3718c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
3719c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
3720c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
3721c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
3722c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
3723c78ba6d6SLubomir Rintel 	}
3724c78ba6d6SLubomir Rintel 
372519e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
372619e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
372719e42e45SRoopa Prabhu 
37289ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
372919e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
373019e42e45SRoopa Prabhu 
3731c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
37329ed59592SDavid Ahern 		if (err < 0)
37339ed59592SDavid Ahern 			goto errout;
37349ed59592SDavid Ahern 	}
37359ed59592SDavid Ahern 
373632bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
373732bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
373832bc201eSXin Long 
373932bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
374032bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
374132bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
374232bc201eSXin Long 		}
374332bc201eSXin Long 	}
374432bc201eSXin Long 
374586872cb5SThomas Graf 	err = 0;
374686872cb5SThomas Graf errout:
374786872cb5SThomas Graf 	return err;
37481da177e4SLinus Torvalds }
37491da177e4SLinus Torvalds 
37506b9ea5a6SRoopa Prabhu struct rt6_nh {
37516b9ea5a6SRoopa Prabhu 	struct rt6_info *rt6_info;
37526b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
37536b9ea5a6SRoopa Prabhu 	struct mx6_config mxc;
37546b9ea5a6SRoopa Prabhu 	struct list_head next;
37556b9ea5a6SRoopa Prabhu };
37566b9ea5a6SRoopa Prabhu 
37576b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
37586b9ea5a6SRoopa Prabhu {
37596b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
37606b9ea5a6SRoopa Prabhu 
37616b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
37627d4d5065SDavid Ahern 		pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n",
37636b9ea5a6SRoopa Prabhu 		        &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
37646b9ea5a6SRoopa Prabhu 		        nh->r_cfg.fc_ifindex);
37656b9ea5a6SRoopa Prabhu 	}
37666b9ea5a6SRoopa Prabhu }
37676b9ea5a6SRoopa Prabhu 
37686b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list,
37696b9ea5a6SRoopa Prabhu 				 struct rt6_info *rt, struct fib6_config *r_cfg)
37706b9ea5a6SRoopa Prabhu {
37716b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
37726b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
37736b9ea5a6SRoopa Prabhu 
37746b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
37756b9ea5a6SRoopa Prabhu 		/* check if rt6_info already exists */
3776f06b7549SDavid Ahern 		if (rt6_duplicate_nexthop(nh->rt6_info, rt))
37776b9ea5a6SRoopa Prabhu 			return err;
37786b9ea5a6SRoopa Prabhu 	}
37796b9ea5a6SRoopa Prabhu 
37806b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
37816b9ea5a6SRoopa Prabhu 	if (!nh)
37826b9ea5a6SRoopa Prabhu 		return -ENOMEM;
37836b9ea5a6SRoopa Prabhu 	nh->rt6_info = rt;
37846b9ea5a6SRoopa Prabhu 	err = ip6_convert_metrics(&nh->mxc, r_cfg);
37856b9ea5a6SRoopa Prabhu 	if (err) {
37866b9ea5a6SRoopa Prabhu 		kfree(nh);
37876b9ea5a6SRoopa Prabhu 		return err;
37886b9ea5a6SRoopa Prabhu 	}
37896b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
37906b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
37916b9ea5a6SRoopa Prabhu 
37926b9ea5a6SRoopa Prabhu 	return 0;
37936b9ea5a6SRoopa Prabhu }
37946b9ea5a6SRoopa Prabhu 
37953b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt,
37963b1137feSDavid Ahern 				   struct rt6_info *rt_last,
37973b1137feSDavid Ahern 				   struct nl_info *info,
37983b1137feSDavid Ahern 				   __u16 nlflags)
37993b1137feSDavid Ahern {
38003b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
38013b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
38023b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
38033b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
38043b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
38053b1137feSDavid Ahern 	 */
38063b1137feSDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) {
38073b1137feSDavid Ahern 		rt = list_first_entry(&rt_last->rt6i_siblings,
38083b1137feSDavid Ahern 				      struct rt6_info,
38093b1137feSDavid Ahern 				      rt6i_siblings);
38103b1137feSDavid Ahern 	}
38113b1137feSDavid Ahern 
38123b1137feSDavid Ahern 	if (rt)
38133b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
38143b1137feSDavid Ahern }
38153b1137feSDavid Ahern 
3816333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
3817333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
381851ebd318SNicolas Dichtel {
38193b1137feSDavid Ahern 	struct rt6_info *rt_notif = NULL, *rt_last = NULL;
38203b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
382151ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
382251ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
38236b9ea5a6SRoopa Prabhu 	struct rt6_info *rt;
38246b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
38256b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
38263b1137feSDavid Ahern 	__u16 nlflags;
382751ebd318SNicolas Dichtel 	int remaining;
382851ebd318SNicolas Dichtel 	int attrlen;
38296b9ea5a6SRoopa Prabhu 	int err = 1;
38306b9ea5a6SRoopa Prabhu 	int nhn = 0;
38316b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
38326b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
38336b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
383451ebd318SNicolas Dichtel 
38353b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
38363b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
38373b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
38383b1137feSDavid Ahern 
383935f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
384051ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
384151ebd318SNicolas Dichtel 
38426b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
38436b9ea5a6SRoopa Prabhu 	 * rt6_info structs per nexthop
38446b9ea5a6SRoopa Prabhu 	 */
384551ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
384651ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
384751ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
384851ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
384951ebd318SNicolas Dichtel 
385051ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
385151ebd318SNicolas Dichtel 		if (attrlen > 0) {
385251ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
385351ebd318SNicolas Dichtel 
385451ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
385551ebd318SNicolas Dichtel 			if (nla) {
385667b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
385751ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
385851ebd318SNicolas Dichtel 			}
385919e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
386019e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
386119e42e45SRoopa Prabhu 			if (nla)
386219e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
386351ebd318SNicolas Dichtel 		}
38646b9ea5a6SRoopa Prabhu 
3865333c4301SDavid Ahern 		rt = ip6_route_info_create(&r_cfg, extack);
38668c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
38678c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
38688c5b83f0SRoopa Prabhu 			rt = NULL;
38696b9ea5a6SRoopa Prabhu 			goto cleanup;
38708c5b83f0SRoopa Prabhu 		}
38716b9ea5a6SRoopa Prabhu 
38726b9ea5a6SRoopa Prabhu 		err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg);
387351ebd318SNicolas Dichtel 		if (err) {
3874587fea74SWei Wang 			dst_release_immediate(&rt->dst);
38756b9ea5a6SRoopa Prabhu 			goto cleanup;
387651ebd318SNicolas Dichtel 		}
38776b9ea5a6SRoopa Prabhu 
38786b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
387951ebd318SNicolas Dichtel 	}
38806b9ea5a6SRoopa Prabhu 
38813b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
38823b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
38833b1137feSDavid Ahern 	 * the full route when done
38843b1137feSDavid Ahern 	 */
38853b1137feSDavid Ahern 	info->skip_notify = 1;
38863b1137feSDavid Ahern 
38876b9ea5a6SRoopa Prabhu 	err_nh = NULL;
38886b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
38893b1137feSDavid Ahern 		rt_last = nh->rt6_info;
3890333c4301SDavid Ahern 		err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack);
38913b1137feSDavid Ahern 		/* save reference to first route for notification */
38923b1137feSDavid Ahern 		if (!rt_notif && !err)
38933b1137feSDavid Ahern 			rt_notif = nh->rt6_info;
38943b1137feSDavid Ahern 
38956b9ea5a6SRoopa Prabhu 		/* nh->rt6_info is used or freed at this point, reset to NULL*/
38966b9ea5a6SRoopa Prabhu 		nh->rt6_info = NULL;
38976b9ea5a6SRoopa Prabhu 		if (err) {
38986b9ea5a6SRoopa Prabhu 			if (replace && nhn)
38996b9ea5a6SRoopa Prabhu 				ip6_print_replace_route_err(&rt6_nh_list);
39006b9ea5a6SRoopa Prabhu 			err_nh = nh;
39016b9ea5a6SRoopa Prabhu 			goto add_errout;
39026b9ea5a6SRoopa Prabhu 		}
39036b9ea5a6SRoopa Prabhu 
39041a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
390527596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
390627596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
390727596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
390827596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
390927596472SMichal Kubeček 		 * be added to it.
39101a72418bSNicolas Dichtel 		 */
391127596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
391227596472SMichal Kubeček 						     NLM_F_REPLACE);
39136b9ea5a6SRoopa Prabhu 		nhn++;
39146b9ea5a6SRoopa Prabhu 	}
39156b9ea5a6SRoopa Prabhu 
39163b1137feSDavid Ahern 	/* success ... tell user about new route */
39173b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
39186b9ea5a6SRoopa Prabhu 	goto cleanup;
39196b9ea5a6SRoopa Prabhu 
39206b9ea5a6SRoopa Prabhu add_errout:
39213b1137feSDavid Ahern 	/* send notification for routes that were added so that
39223b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
39233b1137feSDavid Ahern 	 * coherent
39243b1137feSDavid Ahern 	 */
39253b1137feSDavid Ahern 	if (rt_notif)
39263b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
39273b1137feSDavid Ahern 
39286b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
39296b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
39306b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
39316b9ea5a6SRoopa Prabhu 			break;
3932333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
39336b9ea5a6SRoopa Prabhu 	}
39346b9ea5a6SRoopa Prabhu 
39356b9ea5a6SRoopa Prabhu cleanup:
39366b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
3937587fea74SWei Wang 		if (nh->rt6_info)
3938587fea74SWei Wang 			dst_release_immediate(&nh->rt6_info->dst);
39396b9ea5a6SRoopa Prabhu 		kfree(nh->mxc.mx);
39406b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
39416b9ea5a6SRoopa Prabhu 		kfree(nh);
39426b9ea5a6SRoopa Prabhu 	}
39436b9ea5a6SRoopa Prabhu 
39446b9ea5a6SRoopa Prabhu 	return err;
39456b9ea5a6SRoopa Prabhu }
39466b9ea5a6SRoopa Prabhu 
3947333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
3948333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
39496b9ea5a6SRoopa Prabhu {
39506b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
39516b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
39526b9ea5a6SRoopa Prabhu 	int remaining;
39536b9ea5a6SRoopa Prabhu 	int attrlen;
39546b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
39556b9ea5a6SRoopa Prabhu 
39566b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
39576b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
39586b9ea5a6SRoopa Prabhu 
39596b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
39606b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
39616b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
39626b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
39636b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
39646b9ea5a6SRoopa Prabhu 
39656b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
39666b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
39676b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
39686b9ea5a6SRoopa Prabhu 
39696b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
39706b9ea5a6SRoopa Prabhu 			if (nla) {
39716b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
39726b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
39736b9ea5a6SRoopa Prabhu 			}
39746b9ea5a6SRoopa Prabhu 		}
3975333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
39766b9ea5a6SRoopa Prabhu 		if (err)
39776b9ea5a6SRoopa Prabhu 			last_err = err;
39786b9ea5a6SRoopa Prabhu 
397951ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
398051ebd318SNicolas Dichtel 	}
398151ebd318SNicolas Dichtel 
398251ebd318SNicolas Dichtel 	return last_err;
398351ebd318SNicolas Dichtel }
398451ebd318SNicolas Dichtel 
3985c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
3986c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
39871da177e4SLinus Torvalds {
398886872cb5SThomas Graf 	struct fib6_config cfg;
398986872cb5SThomas Graf 	int err;
39901da177e4SLinus Torvalds 
3991333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
399286872cb5SThomas Graf 	if (err < 0)
399386872cb5SThomas Graf 		return err;
399486872cb5SThomas Graf 
399551ebd318SNicolas Dichtel 	if (cfg.fc_mp)
3996333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
39970ae81335SDavid Ahern 	else {
39980ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
3999333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
40001da177e4SLinus Torvalds 	}
40010ae81335SDavid Ahern }
40021da177e4SLinus Torvalds 
4003c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4004c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
40051da177e4SLinus Torvalds {
400686872cb5SThomas Graf 	struct fib6_config cfg;
400786872cb5SThomas Graf 	int err;
40081da177e4SLinus Torvalds 
4009333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
401086872cb5SThomas Graf 	if (err < 0)
401186872cb5SThomas Graf 		return err;
401286872cb5SThomas Graf 
401351ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4014333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
401551ebd318SNicolas Dichtel 	else
4016333c4301SDavid Ahern 		return ip6_route_add(&cfg, extack);
40171da177e4SLinus Torvalds }
40181da177e4SLinus Torvalds 
4019beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt)
4020339bf98fSThomas Graf {
4021beb1afacSDavid Ahern 	int nexthop_len = 0;
4022beb1afacSDavid Ahern 
4023beb1afacSDavid Ahern 	if (rt->rt6i_nsiblings) {
4024beb1afacSDavid Ahern 		nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
4025beb1afacSDavid Ahern 			    + NLA_ALIGN(sizeof(struct rtnexthop))
4026beb1afacSDavid Ahern 			    + nla_total_size(16) /* RTA_GATEWAY */
4027beb1afacSDavid Ahern 			    + lwtunnel_get_encap_size(rt->dst.lwtstate);
4028beb1afacSDavid Ahern 
4029beb1afacSDavid Ahern 		nexthop_len *= rt->rt6i_nsiblings;
4030beb1afacSDavid Ahern 	}
4031beb1afacSDavid Ahern 
4032339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
4033339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
4034339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
4035339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
4036339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
4037339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
4038339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
4039339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
4040339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
40416a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
4042ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
4043c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
404419e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
4045beb1afacSDavid Ahern 	       + lwtunnel_get_encap_size(rt->dst.lwtstate)
4046beb1afacSDavid Ahern 	       + nexthop_len;
4047beb1afacSDavid Ahern }
4048beb1afacSDavid Ahern 
4049beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt,
40505be083ceSDavid Ahern 			    unsigned int *flags, bool skip_oif)
4051beb1afacSDavid Ahern {
405244c9f2f2SIdo Schimmel 	if (rt->rt6i_nh_flags & RTNH_F_LINKDOWN) {
4053beb1afacSDavid Ahern 		*flags |= RTNH_F_LINKDOWN;
4054beb1afacSDavid Ahern 		if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown)
4055beb1afacSDavid Ahern 			*flags |= RTNH_F_DEAD;
4056beb1afacSDavid Ahern 	}
4057beb1afacSDavid Ahern 
4058beb1afacSDavid Ahern 	if (rt->rt6i_flags & RTF_GATEWAY) {
4059beb1afacSDavid Ahern 		if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
4060beb1afacSDavid Ahern 			goto nla_put_failure;
4061beb1afacSDavid Ahern 	}
4062beb1afacSDavid Ahern 
4063fe400799SIdo Schimmel 	if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD)
406461e4d01eSIdo Schimmel 		*flags |= RTNH_F_OFFLOAD;
406561e4d01eSIdo Schimmel 
40665be083ceSDavid Ahern 	/* not needed for multipath encoding b/c it has a rtnexthop struct */
40675be083ceSDavid Ahern 	if (!skip_oif && rt->dst.dev &&
4068beb1afacSDavid Ahern 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
4069beb1afacSDavid Ahern 		goto nla_put_failure;
4070beb1afacSDavid Ahern 
4071beb1afacSDavid Ahern 	if (rt->dst.lwtstate &&
4072beb1afacSDavid Ahern 	    lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0)
4073beb1afacSDavid Ahern 		goto nla_put_failure;
4074beb1afacSDavid Ahern 
4075beb1afacSDavid Ahern 	return 0;
4076beb1afacSDavid Ahern 
4077beb1afacSDavid Ahern nla_put_failure:
4078beb1afacSDavid Ahern 	return -EMSGSIZE;
4079beb1afacSDavid Ahern }
4080beb1afacSDavid Ahern 
40815be083ceSDavid Ahern /* add multipath next hop */
4082beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt)
4083beb1afacSDavid Ahern {
4084beb1afacSDavid Ahern 	struct rtnexthop *rtnh;
4085beb1afacSDavid Ahern 	unsigned int flags = 0;
4086beb1afacSDavid Ahern 
4087beb1afacSDavid Ahern 	rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
4088beb1afacSDavid Ahern 	if (!rtnh)
4089beb1afacSDavid Ahern 		goto nla_put_failure;
4090beb1afacSDavid Ahern 
4091beb1afacSDavid Ahern 	rtnh->rtnh_hops = 0;
4092beb1afacSDavid Ahern 	rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0;
4093beb1afacSDavid Ahern 
40945be083ceSDavid Ahern 	if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
4095beb1afacSDavid Ahern 		goto nla_put_failure;
4096beb1afacSDavid Ahern 
4097beb1afacSDavid Ahern 	rtnh->rtnh_flags = flags;
4098beb1afacSDavid Ahern 
4099beb1afacSDavid Ahern 	/* length of rtnetlink header + attributes */
4100beb1afacSDavid Ahern 	rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
4101beb1afacSDavid Ahern 
4102beb1afacSDavid Ahern 	return 0;
4103beb1afacSDavid Ahern 
4104beb1afacSDavid Ahern nla_put_failure:
4105beb1afacSDavid Ahern 	return -EMSGSIZE;
4106339bf98fSThomas Graf }
4107339bf98fSThomas Graf 
4108191cd582SBrian Haley static int rt6_fill_node(struct net *net,
4109191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
41100d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
411115e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
4112f8cfe2ceSDavid Ahern 			 unsigned int flags)
41131da177e4SLinus Torvalds {
41144b32b5adSMartin KaFai Lau 	u32 metrics[RTAX_MAX];
41151da177e4SLinus Torvalds 	struct rtmsg *rtm;
41161da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
4117e3703b3dSThomas Graf 	long expires;
41189e762a4aSPatrick McHardy 	u32 table;
41191da177e4SLinus Torvalds 
412015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
412138308473SDavid S. Miller 	if (!nlh)
412226932566SPatrick McHardy 		return -EMSGSIZE;
41232d7202bfSThomas Graf 
41242d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
41251da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
41261da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
41271da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
41281da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
4129c71099acSThomas Graf 	if (rt->rt6i_table)
41309e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
4131c71099acSThomas Graf 	else
41329e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
41339e762a4aSPatrick McHardy 	rtm->rtm_table = table;
4134c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
4135c78679e8SDavid S. Miller 		goto nla_put_failure;
4136ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
4137ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
4138ef2c7d7bSNicolas Dichtel 		case -EINVAL:
4139ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
4140ef2c7d7bSNicolas Dichtel 			break;
4141ef2c7d7bSNicolas Dichtel 		case -EACCES:
4142ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
4143ef2c7d7bSNicolas Dichtel 			break;
4144b4949ab2SNicolas Dichtel 		case -EAGAIN:
4145b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
4146b4949ab2SNicolas Dichtel 			break;
4147ef2c7d7bSNicolas Dichtel 		default:
41481da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
4149ef2c7d7bSNicolas Dichtel 			break;
4150ef2c7d7bSNicolas Dichtel 		}
4151ef2c7d7bSNicolas Dichtel 	}
4152ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
4153ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
41544ee39733SDavid Ahern 	else if (rt->rt6i_flags & RTF_ANYCAST)
41554ee39733SDavid Ahern 		rtm->rtm_type = RTN_ANYCAST;
4156d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
41571da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
41581da177e4SLinus Torvalds 	else
41591da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
41601da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
41611da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
41621da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
41631da177e4SLinus Torvalds 
41641da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
41651da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
41661da177e4SLinus Torvalds 
41671da177e4SLinus Torvalds 	if (dst) {
4168930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_DST, dst))
4169c78679e8SDavid S. Miller 			goto nla_put_failure;
41701da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
41711da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
4172930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
4173c78679e8SDavid S. Miller 			goto nla_put_failure;
41741da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
41751da177e4SLinus Torvalds 	if (src) {
4176930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
4177c78679e8SDavid S. Miller 			goto nla_put_failure;
41781da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
4179c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
4180930345eaSJiri Benc 		   nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
4181c78679e8SDavid S. Miller 		goto nla_put_failure;
41821da177e4SLinus Torvalds #endif
41837bc570c8SYOSHIFUJI Hideaki 	if (iif) {
41847bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
41857bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
4186fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
41872cf75070SNikolay Aleksandrov 
41887bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
41897bc570c8SYOSHIFUJI Hideaki 				return 0;
4190fd61c6baSDavid Ahern 			if (err < 0)
41917bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
41927bc570c8SYOSHIFUJI Hideaki 		} else
41937bc570c8SYOSHIFUJI Hideaki #endif
4194c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
4195c78679e8SDavid S. Miller 				goto nla_put_failure;
41967bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
41971da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
4198c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
4199930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4200c78679e8SDavid S. Miller 			goto nla_put_failure;
4201c3968a85SDaniel Walter 	}
4202c3968a85SDaniel Walter 
4203c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
4204c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
42054e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
4206930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4207c78679e8SDavid S. Miller 			goto nla_put_failure;
42081da177e4SLinus Torvalds 	}
42092d7202bfSThomas Graf 
42104b32b5adSMartin KaFai Lau 	memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
42114b32b5adSMartin KaFai Lau 	if (rt->rt6i_pmtu)
42124b32b5adSMartin KaFai Lau 		metrics[RTAX_MTU - 1] = rt->rt6i_pmtu;
42134b32b5adSMartin KaFai Lau 	if (rtnetlink_put_metrics(skb, metrics) < 0)
42142d7202bfSThomas Graf 		goto nla_put_failure;
42152d7202bfSThomas Graf 
4216beb1afacSDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
4217beb1afacSDavid Ahern 		goto nla_put_failure;
4218beb1afacSDavid Ahern 
4219beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
4220beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
4221beb1afacSDavid Ahern 	 */
4222beb1afacSDavid Ahern 	if (rt->rt6i_nsiblings) {
4223beb1afacSDavid Ahern 		struct rt6_info *sibling, *next_sibling;
4224beb1afacSDavid Ahern 		struct nlattr *mp;
4225beb1afacSDavid Ahern 
4226beb1afacSDavid Ahern 		mp = nla_nest_start(skb, RTA_MULTIPATH);
4227beb1afacSDavid Ahern 		if (!mp)
4228beb1afacSDavid Ahern 			goto nla_put_failure;
4229beb1afacSDavid Ahern 
4230beb1afacSDavid Ahern 		if (rt6_add_nexthop(skb, rt) < 0)
4231beb1afacSDavid Ahern 			goto nla_put_failure;
4232beb1afacSDavid Ahern 
4233beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
4234beb1afacSDavid Ahern 					 &rt->rt6i_siblings, rt6i_siblings) {
4235beb1afacSDavid Ahern 			if (rt6_add_nexthop(skb, sibling) < 0)
423694f826b8SEric Dumazet 				goto nla_put_failure;
423794f826b8SEric Dumazet 		}
42382d7202bfSThomas Graf 
4239beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
4240beb1afacSDavid Ahern 	} else {
42415be083ceSDavid Ahern 		if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
4242c78679e8SDavid S. Miller 			goto nla_put_failure;
4243beb1afacSDavid Ahern 	}
42448253947eSLi Wei 
42458253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
424669cdf8f9SYOSHIFUJI Hideaki 
424787a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
4248e3703b3dSThomas Graf 		goto nla_put_failure;
42491da177e4SLinus Torvalds 
4250c78ba6d6SLubomir Rintel 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
4251c78ba6d6SLubomir Rintel 		goto nla_put_failure;
4252c78ba6d6SLubomir Rintel 
425319e42e45SRoopa Prabhu 
4254053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4255053c095aSJohannes Berg 	return 0;
42562d7202bfSThomas Graf 
42572d7202bfSThomas Graf nla_put_failure:
425826932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
425926932566SPatrick McHardy 	return -EMSGSIZE;
42601da177e4SLinus Torvalds }
42611da177e4SLinus Torvalds 
42621b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
42631da177e4SLinus Torvalds {
42641da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
42651f17e2f2SDavid Ahern 	struct net *net = arg->net;
42661f17e2f2SDavid Ahern 
42671f17e2f2SDavid Ahern 	if (rt == net->ipv6.ip6_null_entry)
42681f17e2f2SDavid Ahern 		return 0;
42691da177e4SLinus Torvalds 
42702d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
42712d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
4272f8cfe2ceSDavid Ahern 
4273f8cfe2ceSDavid Ahern 		/* user wants prefix routes only */
4274f8cfe2ceSDavid Ahern 		if (rtm->rtm_flags & RTM_F_PREFIX &&
4275f8cfe2ceSDavid Ahern 		    !(rt->rt6i_flags & RTF_PREFIX_RT)) {
4276f8cfe2ceSDavid Ahern 			/* success since this is not a prefix route */
4277f8cfe2ceSDavid Ahern 			return 1;
4278f8cfe2ceSDavid Ahern 		}
4279f8cfe2ceSDavid Ahern 	}
42801da177e4SLinus Torvalds 
42811f17e2f2SDavid Ahern 	return rt6_fill_node(net,
4282191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
428315e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
4284f8cfe2ceSDavid Ahern 		     NLM_F_MULTI);
42851da177e4SLinus Torvalds }
42861da177e4SLinus Torvalds 
4287c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4288c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
42891da177e4SLinus Torvalds {
42903b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
4291ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
429218c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
429318c3a61cSRoopa Prabhu 	struct dst_entry *dst;
42941da177e4SLinus Torvalds 	struct rt6_info *rt;
4295ab364a6fSThomas Graf 	struct sk_buff *skb;
4296ab364a6fSThomas Graf 	struct rtmsg *rtm;
42974c9483b2SDavid S. Miller 	struct flowi6 fl6;
429818c3a61cSRoopa Prabhu 	bool fibmatch;
4299ab364a6fSThomas Graf 
4300fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4301c21ef3e3SDavid Ahern 			  extack);
4302ab364a6fSThomas Graf 	if (err < 0)
4303ab364a6fSThomas Graf 		goto errout;
4304ab364a6fSThomas Graf 
4305ab364a6fSThomas Graf 	err = -EINVAL;
43064c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
430738b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
430838b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
430918c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
4310ab364a6fSThomas Graf 
4311ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
4312ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4313ab364a6fSThomas Graf 			goto errout;
4314ab364a6fSThomas Graf 
43154e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
4316ab364a6fSThomas Graf 	}
4317ab364a6fSThomas Graf 
4318ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
4319ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4320ab364a6fSThomas Graf 			goto errout;
4321ab364a6fSThomas Graf 
43224e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
4323ab364a6fSThomas Graf 	}
4324ab364a6fSThomas Graf 
4325ab364a6fSThomas Graf 	if (tb[RTA_IIF])
4326ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
4327ab364a6fSThomas Graf 
4328ab364a6fSThomas Graf 	if (tb[RTA_OIF])
432972331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
4330ab364a6fSThomas Graf 
43312e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
43322e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
43332e47b291SLorenzo Colitti 
4334622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
4335622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
4336622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
4337622ec2c9SLorenzo Colitti 	else
4338622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4339622ec2c9SLorenzo Colitti 
4340ab364a6fSThomas Graf 	if (iif) {
4341ab364a6fSThomas Graf 		struct net_device *dev;
434272331bc0SShmulik Ladkani 		int flags = 0;
434372331bc0SShmulik Ladkani 
4344121622dbSFlorian Westphal 		rcu_read_lock();
4345121622dbSFlorian Westphal 
4346121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
4347ab364a6fSThomas Graf 		if (!dev) {
4348121622dbSFlorian Westphal 			rcu_read_unlock();
4349ab364a6fSThomas Graf 			err = -ENODEV;
4350ab364a6fSThomas Graf 			goto errout;
4351ab364a6fSThomas Graf 		}
435272331bc0SShmulik Ladkani 
435372331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
435472331bc0SShmulik Ladkani 
435572331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
435672331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
435772331bc0SShmulik Ladkani 
435818c3a61cSRoopa Prabhu 		dst = ip6_route_input_lookup(net, dev, &fl6, flags);
4359121622dbSFlorian Westphal 
4360121622dbSFlorian Westphal 		rcu_read_unlock();
436172331bc0SShmulik Ladkani 	} else {
436272331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
436372331bc0SShmulik Ladkani 
436418c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
436518c3a61cSRoopa Prabhu 	}
436618c3a61cSRoopa Prabhu 
436718c3a61cSRoopa Prabhu 
436818c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
436918c3a61cSRoopa Prabhu 	if (rt->dst.error) {
437018c3a61cSRoopa Prabhu 		err = rt->dst.error;
437118c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
437218c3a61cSRoopa Prabhu 		goto errout;
4373ab364a6fSThomas Graf 	}
43741da177e4SLinus Torvalds 
43759d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
43769d6acb3bSWANG Cong 		err = rt->dst.error;
43779d6acb3bSWANG Cong 		ip6_rt_put(rt);
43789d6acb3bSWANG Cong 		goto errout;
43799d6acb3bSWANG Cong 	}
43809d6acb3bSWANG Cong 
4381fba961abSDavid S. Miller 	if (fibmatch && rt->from) {
4382fba961abSDavid S. Miller 		struct rt6_info *ort = rt->from;
438358acfd71SIdo Schimmel 
438458acfd71SIdo Schimmel 		dst_hold(&ort->dst);
438558acfd71SIdo Schimmel 		ip6_rt_put(rt);
438658acfd71SIdo Schimmel 		rt = ort;
438758acfd71SIdo Schimmel 	}
438858acfd71SIdo Schimmel 
43891da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
439038308473SDavid S. Miller 	if (!skb) {
439194e187c0SAmerigo Wang 		ip6_rt_put(rt);
4392ab364a6fSThomas Graf 		err = -ENOBUFS;
4393ab364a6fSThomas Graf 		goto errout;
4394ab364a6fSThomas Graf 	}
43951da177e4SLinus Torvalds 
4396d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
439718c3a61cSRoopa Prabhu 	if (fibmatch)
439818c3a61cSRoopa Prabhu 		err = rt6_fill_node(net, skb, rt, NULL, NULL, iif,
439918c3a61cSRoopa Prabhu 				    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
440018c3a61cSRoopa Prabhu 				    nlh->nlmsg_seq, 0);
440118c3a61cSRoopa Prabhu 	else
44024c9483b2SDavid S. Miller 		err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
440315e47304SEric W. Biederman 				    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
4404f8cfe2ceSDavid Ahern 				    nlh->nlmsg_seq, 0);
44051da177e4SLinus Torvalds 	if (err < 0) {
4406ab364a6fSThomas Graf 		kfree_skb(skb);
4407ab364a6fSThomas Graf 		goto errout;
44081da177e4SLinus Torvalds 	}
44091da177e4SLinus Torvalds 
441015e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4411ab364a6fSThomas Graf errout:
44121da177e4SLinus Torvalds 	return err;
44131da177e4SLinus Torvalds }
44141da177e4SLinus Torvalds 
441537a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
441637a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
44171da177e4SLinus Torvalds {
44181da177e4SLinus Torvalds 	struct sk_buff *skb;
44195578689aSDaniel Lezcano 	struct net *net = info->nl_net;
4420528c4cebSDenis V. Lunev 	u32 seq;
4421528c4cebSDenis V. Lunev 	int err;
44220d51aa80SJamal Hadi Salim 
4423528c4cebSDenis V. Lunev 	err = -ENOBUFS;
442438308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
442586872cb5SThomas Graf 
442619e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
442738308473SDavid S. Miller 	if (!skb)
442821713ebcSThomas Graf 		goto errout;
44291da177e4SLinus Torvalds 
4430191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
4431f8cfe2ceSDavid Ahern 				event, info->portid, seq, nlm_flags);
443226932566SPatrick McHardy 	if (err < 0) {
443326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
443426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
443526932566SPatrick McHardy 		kfree_skb(skb);
443626932566SPatrick McHardy 		goto errout;
443726932566SPatrick McHardy 	}
443815e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
44395578689aSDaniel Lezcano 		    info->nlh, gfp_any());
44401ce85fe4SPablo Neira Ayuso 	return;
444121713ebcSThomas Graf errout:
444221713ebcSThomas Graf 	if (err < 0)
44435578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
44441da177e4SLinus Torvalds }
44451da177e4SLinus Torvalds 
44468ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
4447351638e7SJiri Pirko 				unsigned long event, void *ptr)
44488ed67789SDaniel Lezcano {
4449351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4450c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
44518ed67789SDaniel Lezcano 
4452242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
4453242d3a49SWANG Cong 		return NOTIFY_OK;
4454242d3a49SWANG Cong 
4455242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
4456d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
44578ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
44588ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
4459d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
44608ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
4461d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
44628ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
44638ed67789SDaniel Lezcano #endif
446476da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
446576da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
446676da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
446776da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
446876da0704SWANG Cong 		 */
446912d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
4470242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
447112d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
447212d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
4473242d3a49SWANG Cong #endif
44748ed67789SDaniel Lezcano 	}
44758ed67789SDaniel Lezcano 
44768ed67789SDaniel Lezcano 	return NOTIFY_OK;
44778ed67789SDaniel Lezcano }
44788ed67789SDaniel Lezcano 
44791da177e4SLinus Torvalds /*
44801da177e4SLinus Torvalds  *	/proc
44811da177e4SLinus Torvalds  */
44821da177e4SLinus Torvalds 
44831da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
44841da177e4SLinus Torvalds 
448533120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
448633120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
448733120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
448833120b30SAlexey Dobriyan 	.read		= seq_read,
448933120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
44908d2ca1d7SHannes Frederic Sowa 	.release	= seq_release_net,
449133120b30SAlexey Dobriyan };
449233120b30SAlexey Dobriyan 
44931da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
44941da177e4SLinus Torvalds {
449569ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
44961da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
449769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
449869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
449981eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
450069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
450169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
4502fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
450369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
45041da177e4SLinus Torvalds 
45051da177e4SLinus Torvalds 	return 0;
45061da177e4SLinus Torvalds }
45071da177e4SLinus Torvalds 
45081da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
45091da177e4SLinus Torvalds {
4510de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
451169ddb805SDaniel Lezcano }
451269ddb805SDaniel Lezcano 
45139a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
45141da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
45151da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
45161da177e4SLinus Torvalds 	.read	 = seq_read,
45171da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
4518b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
45191da177e4SLinus Torvalds };
45201da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
45211da177e4SLinus Torvalds 
45221da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
45231da177e4SLinus Torvalds 
45241da177e4SLinus Torvalds static
4525fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
45261da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
45271da177e4SLinus Torvalds {
4528c486da34SLucian Adrian Grijincu 	struct net *net;
4529c486da34SLucian Adrian Grijincu 	int delay;
4530c486da34SLucian Adrian Grijincu 	if (!write)
4531c486da34SLucian Adrian Grijincu 		return -EINVAL;
4532c486da34SLucian Adrian Grijincu 
4533c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
4534c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
45358d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
45362ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
45371da177e4SLinus Torvalds 	return 0;
45381da177e4SLinus Torvalds }
45391da177e4SLinus Torvalds 
4540fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = {
45411da177e4SLinus Torvalds 	{
45421da177e4SLinus Torvalds 		.procname	=	"flush",
45434990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
45441da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
454589c8b3a1SDave Jones 		.mode		=	0200,
45466d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
45471da177e4SLinus Torvalds 	},
45481da177e4SLinus Torvalds 	{
45491da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
45509a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
45511da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
45521da177e4SLinus Torvalds 		.mode		=	0644,
45536d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
45541da177e4SLinus Torvalds 	},
45551da177e4SLinus Torvalds 	{
45561da177e4SLinus Torvalds 		.procname	=	"max_size",
45574990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
45581da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
45591da177e4SLinus Torvalds 		.mode		=	0644,
45606d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
45611da177e4SLinus Torvalds 	},
45621da177e4SLinus Torvalds 	{
45631da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
45644990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
45651da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
45661da177e4SLinus Torvalds 		.mode		=	0644,
45676d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
45681da177e4SLinus Torvalds 	},
45691da177e4SLinus Torvalds 	{
45701da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
45714990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
45721da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
45731da177e4SLinus Torvalds 		.mode		=	0644,
45746d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
45751da177e4SLinus Torvalds 	},
45761da177e4SLinus Torvalds 	{
45771da177e4SLinus Torvalds 		.procname	=	"gc_interval",
45784990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
45791da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
45801da177e4SLinus Torvalds 		.mode		=	0644,
45816d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
45821da177e4SLinus Torvalds 	},
45831da177e4SLinus Torvalds 	{
45841da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
45854990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
45861da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
45871da177e4SLinus Torvalds 		.mode		=	0644,
4588f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
45891da177e4SLinus Torvalds 	},
45901da177e4SLinus Torvalds 	{
45911da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
45924990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
45931da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
45941da177e4SLinus Torvalds 		.mode		=	0644,
45956d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
45961da177e4SLinus Torvalds 	},
45971da177e4SLinus Torvalds 	{
45981da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
45994990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
46001da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
46011da177e4SLinus Torvalds 		.mode		=	0644,
4602f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
46031da177e4SLinus Torvalds 	},
46041da177e4SLinus Torvalds 	{
46051da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
46064990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
46071da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
46081da177e4SLinus Torvalds 		.mode		=	0644,
46096d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
46101da177e4SLinus Torvalds 	},
4611f8572d8fSEric W. Biederman 	{ }
46121da177e4SLinus Torvalds };
46131da177e4SLinus Torvalds 
46142c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
4615760f2d01SDaniel Lezcano {
4616760f2d01SDaniel Lezcano 	struct ctl_table *table;
4617760f2d01SDaniel Lezcano 
4618760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
4619760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
4620760f2d01SDaniel Lezcano 			GFP_KERNEL);
46215ee09105SYOSHIFUJI Hideaki 
46225ee09105SYOSHIFUJI Hideaki 	if (table) {
46235ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
4624c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
462586393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
46265ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
46275ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
46285ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
46295ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
46305ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
46315ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
46325ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
46339c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
4634464dc801SEric W. Biederman 
4635464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
4636464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
4637464dc801SEric W. Biederman 			table[0].procname = NULL;
46385ee09105SYOSHIFUJI Hideaki 	}
46395ee09105SYOSHIFUJI Hideaki 
4640760f2d01SDaniel Lezcano 	return table;
4641760f2d01SDaniel Lezcano }
46421da177e4SLinus Torvalds #endif
46431da177e4SLinus Torvalds 
46442c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
4645cdb18761SDaniel Lezcano {
4646633d424bSPavel Emelyanov 	int ret = -ENOMEM;
46478ed67789SDaniel Lezcano 
464886393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
464986393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
4650f2fc6a54SBenjamin Thery 
4651fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
4652fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
4653fc66f95cSEric Dumazet 
46548ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
46558ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
46568ed67789SDaniel Lezcano 					   GFP_KERNEL);
46578ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
4658fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
4659d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
466062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
466162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
46628ed67789SDaniel Lezcano 
46638ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
4664feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
46658ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
46668ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
46678ed67789SDaniel Lezcano 					       GFP_KERNEL);
466868fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
466968fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
4670d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
467162fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
467262fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
46738ed67789SDaniel Lezcano 
46748ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
46758ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
46768ed67789SDaniel Lezcano 					       GFP_KERNEL);
467768fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
467868fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
4679d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
468062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
468162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
46828ed67789SDaniel Lezcano #endif
46838ed67789SDaniel Lezcano 
4684b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
4685b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
4686b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
4687b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
4688b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
4689b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
4690b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
4691b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
4692b339a47cSPeter Zijlstra 
46936891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
46946891a346SBenjamin Thery 
46958ed67789SDaniel Lezcano 	ret = 0;
46968ed67789SDaniel Lezcano out:
46978ed67789SDaniel Lezcano 	return ret;
4698f2fc6a54SBenjamin Thery 
469968fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
470068fffc67SPeter Zijlstra out_ip6_prohibit_entry:
470168fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
470268fffc67SPeter Zijlstra out_ip6_null_entry:
470368fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
470468fffc67SPeter Zijlstra #endif
4705fc66f95cSEric Dumazet out_ip6_dst_entries:
4706fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
4707f2fc6a54SBenjamin Thery out_ip6_dst_ops:
4708f2fc6a54SBenjamin Thery 	goto out;
4709cdb18761SDaniel Lezcano }
4710cdb18761SDaniel Lezcano 
47112c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
4712cdb18761SDaniel Lezcano {
47138ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
47148ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
47158ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
47168ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
47178ed67789SDaniel Lezcano #endif
471841bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
4719cdb18761SDaniel Lezcano }
4720cdb18761SDaniel Lezcano 
4721d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
4722d189634eSThomas Graf {
4723d189634eSThomas Graf #ifdef CONFIG_PROC_FS
4724d4beaa66SGao feng 	proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
4725d4beaa66SGao feng 	proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
4726d189634eSThomas Graf #endif
4727d189634eSThomas Graf 	return 0;
4728d189634eSThomas Graf }
4729d189634eSThomas Graf 
4730d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
4731d189634eSThomas Graf {
4732d189634eSThomas Graf #ifdef CONFIG_PROC_FS
4733ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
4734ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
4735d189634eSThomas Graf #endif
4736d189634eSThomas Graf }
4737d189634eSThomas Graf 
4738cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
4739cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
4740cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
4741cdb18761SDaniel Lezcano };
4742cdb18761SDaniel Lezcano 
4743c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
4744c3426b47SDavid S. Miller {
4745c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
4746c3426b47SDavid S. Miller 
4747c3426b47SDavid S. Miller 	if (!bp)
4748c3426b47SDavid S. Miller 		return -ENOMEM;
4749c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
4750c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
4751c3426b47SDavid S. Miller 	return 0;
4752c3426b47SDavid S. Miller }
4753c3426b47SDavid S. Miller 
4754c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
4755c3426b47SDavid S. Miller {
4756c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
4757c3426b47SDavid S. Miller 
4758c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
475956a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
4760c3426b47SDavid S. Miller 	kfree(bp);
4761c3426b47SDavid S. Miller }
4762c3426b47SDavid S. Miller 
47632b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
4764c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
4765c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
4766c3426b47SDavid S. Miller };
4767c3426b47SDavid S. Miller 
4768d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
4769d189634eSThomas Graf 	.init = ip6_route_net_init_late,
4770d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
4771d189634eSThomas Graf };
4772d189634eSThomas Graf 
47738ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
47748ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
4775242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
47768ed67789SDaniel Lezcano };
47778ed67789SDaniel Lezcano 
47782f460933SWANG Cong void __init ip6_route_init_special_entries(void)
47792f460933SWANG Cong {
47802f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
47812f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
47822f460933SWANG Cong 	 * manually for init_net */
47832f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
47842f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
47852f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
47862f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
47872f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
47882f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
47892f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
47902f460933SWANG Cong   #endif
47912f460933SWANG Cong }
47922f460933SWANG Cong 
4793433d49c3SDaniel Lezcano int __init ip6_route_init(void)
47941da177e4SLinus Torvalds {
4795433d49c3SDaniel Lezcano 	int ret;
47968d0b94afSMartin KaFai Lau 	int cpu;
4797433d49c3SDaniel Lezcano 
47989a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
47999a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
48009a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
48019a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
48029a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
4803c19a28e1SFernando Carrijo 		goto out;
480414e50e57SDavid S. Miller 
4805fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
48068ed67789SDaniel Lezcano 	if (ret)
4807bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
4808bdb3289fSDaniel Lezcano 
4809c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
4810c3426b47SDavid S. Miller 	if (ret)
4811e8803b6cSDavid S. Miller 		goto out_dst_entries;
48122a0c451aSThomas Graf 
48137e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
48147e52b33bSDavid S. Miller 	if (ret)
48157e52b33bSDavid S. Miller 		goto out_register_inetpeer;
4816c3426b47SDavid S. Miller 
48175dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
48185dc121e9SArnaud Ebalard 
4819e8803b6cSDavid S. Miller 	ret = fib6_init();
4820433d49c3SDaniel Lezcano 	if (ret)
48218ed67789SDaniel Lezcano 		goto out_register_subsys;
4822433d49c3SDaniel Lezcano 
4823433d49c3SDaniel Lezcano 	ret = xfrm6_init();
4824433d49c3SDaniel Lezcano 	if (ret)
4825e8803b6cSDavid S. Miller 		goto out_fib6_init;
4826c35b7e72SDaniel Lezcano 
4827433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
4828433d49c3SDaniel Lezcano 	if (ret)
4829433d49c3SDaniel Lezcano 		goto xfrm6_init;
48307e5449c2SDaniel Lezcano 
4831d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
4832d189634eSThomas Graf 	if (ret)
4833d189634eSThomas Graf 		goto fib6_rules_init;
4834d189634eSThomas Graf 
483516feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
483616feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
483716feebcfSFlorian Westphal 	if (ret < 0)
483816feebcfSFlorian Westphal 		goto out_register_late_subsys;
483916feebcfSFlorian Westphal 
484016feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
484116feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
484216feebcfSFlorian Westphal 	if (ret < 0)
484316feebcfSFlorian Westphal 		goto out_register_late_subsys;
484416feebcfSFlorian Westphal 
484516feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
484616feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
484716feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
484816feebcfSFlorian Westphal 	if (ret < 0)
4849d189634eSThomas Graf 		goto out_register_late_subsys;
4850433d49c3SDaniel Lezcano 
48518ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
4852cdb18761SDaniel Lezcano 	if (ret)
4853d189634eSThomas Graf 		goto out_register_late_subsys;
48548ed67789SDaniel Lezcano 
48558d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
48568d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
48578d0b94afSMartin KaFai Lau 
48588d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
48598d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
48608d0b94afSMartin KaFai Lau 	}
48618d0b94afSMartin KaFai Lau 
4862433d49c3SDaniel Lezcano out:
4863433d49c3SDaniel Lezcano 	return ret;
4864433d49c3SDaniel Lezcano 
4865d189634eSThomas Graf out_register_late_subsys:
486616feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
4867d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
4868433d49c3SDaniel Lezcano fib6_rules_init:
4869433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
4870433d49c3SDaniel Lezcano xfrm6_init:
4871433d49c3SDaniel Lezcano 	xfrm6_fini();
48722a0c451aSThomas Graf out_fib6_init:
48732a0c451aSThomas Graf 	fib6_gc_cleanup();
48748ed67789SDaniel Lezcano out_register_subsys:
48758ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
48767e52b33bSDavid S. Miller out_register_inetpeer:
48777e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
4878fc66f95cSEric Dumazet out_dst_entries:
4879fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
4880433d49c3SDaniel Lezcano out_kmem_cache:
4881f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
4882433d49c3SDaniel Lezcano 	goto out;
48831da177e4SLinus Torvalds }
48841da177e4SLinus Torvalds 
48851da177e4SLinus Torvalds void ip6_route_cleanup(void)
48861da177e4SLinus Torvalds {
48878ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
4888d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
4889101367c2SThomas Graf 	fib6_rules_cleanup();
48901da177e4SLinus Torvalds 	xfrm6_fini();
48911da177e4SLinus Torvalds 	fib6_gc_cleanup();
4892c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
48938ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
489441bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
4895f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
48961da177e4SLinus Torvalds }
4897