xref: /openbmc/linux/net/ipv6/route.c (revision 1de178edc76e4ad87d02fcadf73af97f52e87caa)
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 
4778067bb8cSIdo Schimmel 				if (sibling->rt6i_nh_flags & RTNH_F_DEAD)
4788067bb8cSIdo 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 
5048067bb8cSIdo Schimmel 	if (!oif && ipv6_addr_any(saddr) && !(rt->rt6i_nh_flags & RTNH_F_DEAD))
5058067bb8cSIdo 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 
5108067bb8cSIdo Schimmel 		if (sprt->rt6i_nh_flags & RTNH_F_DEAD)
5118067bb8cSIdo Schimmel 			continue;
5128067bb8cSIdo 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 	}
5418067bb8cSIdo Schimmel 
5428067bb8cSIdo 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 
6888067bb8cSIdo Schimmel 	if (rt->rt6i_nh_flags & RTNH_F_DEAD)
6898067bb8cSIdo Schimmel 		goto out;
6908067bb8cSIdo 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) {
1356922c2ac8SIdo Schimmel 		spin_lock_bh(&ort->rt6i_table->tb6_lock);
135735732d01SWei Wang 		fib6_update_sernum(ort);
1358922c2ac8SIdo Schimmel 		spin_unlock_bh(&ort->rt6i_table->tb6_lock);
1359b886d5f2SPaolo Abeni 		fib6_force_start_gc(net);
1360b886d5f2SPaolo Abeni 	}
136135732d01SWei Wang 
136235732d01SWei Wang 	return err;
136335732d01SWei Wang }
136435732d01SWei Wang 
136535732d01SWei Wang void rt6_flush_exceptions(struct rt6_info *rt)
136635732d01SWei Wang {
136735732d01SWei Wang 	struct rt6_exception_bucket *bucket;
136835732d01SWei Wang 	struct rt6_exception *rt6_ex;
136935732d01SWei Wang 	struct hlist_node *tmp;
137035732d01SWei Wang 	int i;
137135732d01SWei Wang 
137235732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
137335732d01SWei Wang 	/* Prevent rt6_insert_exception() to recreate the bucket list */
137435732d01SWei Wang 	rt->exception_bucket_flushed = 1;
137535732d01SWei Wang 
137635732d01SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
137735732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
137835732d01SWei Wang 	if (!bucket)
137935732d01SWei Wang 		goto out;
138035732d01SWei Wang 
138135732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
138235732d01SWei Wang 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
138335732d01SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
138435732d01SWei Wang 		WARN_ON_ONCE(bucket->depth);
138535732d01SWei Wang 		bucket++;
138635732d01SWei Wang 	}
138735732d01SWei Wang 
138835732d01SWei Wang out:
138935732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
139035732d01SWei Wang }
139135732d01SWei Wang 
139235732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
139335732d01SWei Wang  * Caller has to hold rcu_read_lock()
139435732d01SWei Wang  */
139535732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt,
139635732d01SWei Wang 					   struct in6_addr *daddr,
139735732d01SWei Wang 					   struct in6_addr *saddr)
139835732d01SWei Wang {
139935732d01SWei Wang 	struct rt6_exception_bucket *bucket;
140035732d01SWei Wang 	struct in6_addr *src_key = NULL;
140135732d01SWei Wang 	struct rt6_exception *rt6_ex;
140235732d01SWei Wang 	struct rt6_info *res = NULL;
140335732d01SWei Wang 
140435732d01SWei Wang 	bucket = rcu_dereference(rt->rt6i_exception_bucket);
140535732d01SWei Wang 
140635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
140735732d01SWei Wang 	/* rt6i_src.plen != 0 indicates rt is in subtree
140835732d01SWei Wang 	 * and exception table is indexed by a hash of
140935732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
141035732d01SWei Wang 	 * Otherwise, the exception table is indexed by
141135732d01SWei Wang 	 * a hash of only rt6i_dst.
141235732d01SWei Wang 	 */
141335732d01SWei Wang 	if (rt->rt6i_src.plen)
141435732d01SWei Wang 		src_key = saddr;
141535732d01SWei Wang #endif
141635732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
141735732d01SWei Wang 
141835732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
141935732d01SWei Wang 		res = rt6_ex->rt6i;
142035732d01SWei Wang 
142135732d01SWei Wang 	return res;
142235732d01SWei Wang }
142335732d01SWei Wang 
142435732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
142535732d01SWei Wang int rt6_remove_exception_rt(struct rt6_info *rt)
142635732d01SWei Wang {
142735732d01SWei Wang 	struct rt6_exception_bucket *bucket;
14283a2232e9SDavid Miller 	struct rt6_info *from = rt->from;
142935732d01SWei Wang 	struct in6_addr *src_key = NULL;
143035732d01SWei Wang 	struct rt6_exception *rt6_ex;
143135732d01SWei Wang 	int err;
143235732d01SWei Wang 
143335732d01SWei Wang 	if (!from ||
1434442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
143535732d01SWei Wang 		return -EINVAL;
143635732d01SWei Wang 
143735732d01SWei Wang 	if (!rcu_access_pointer(from->rt6i_exception_bucket))
143835732d01SWei Wang 		return -ENOENT;
143935732d01SWei Wang 
144035732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
144135732d01SWei Wang 	bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
144235732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
144335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
144435732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
144535732d01SWei Wang 	 * and exception table is indexed by a hash of
144635732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
144735732d01SWei Wang 	 * Otherwise, the exception table is indexed by
144835732d01SWei Wang 	 * a hash of only rt6i_dst.
144935732d01SWei Wang 	 */
145035732d01SWei Wang 	if (from->rt6i_src.plen)
145135732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
145235732d01SWei Wang #endif
145335732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
145435732d01SWei Wang 					       &rt->rt6i_dst.addr,
145535732d01SWei Wang 					       src_key);
145635732d01SWei Wang 	if (rt6_ex) {
145735732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
145835732d01SWei Wang 		err = 0;
145935732d01SWei Wang 	} else {
146035732d01SWei Wang 		err = -ENOENT;
146135732d01SWei Wang 	}
146235732d01SWei Wang 
146335732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
146435732d01SWei Wang 	return err;
146535732d01SWei Wang }
146635732d01SWei Wang 
146735732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
146835732d01SWei Wang  * refresh its stamp
146935732d01SWei Wang  */
147035732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
147135732d01SWei Wang {
147235732d01SWei Wang 	struct rt6_exception_bucket *bucket;
14733a2232e9SDavid Miller 	struct rt6_info *from = rt->from;
147435732d01SWei Wang 	struct in6_addr *src_key = NULL;
147535732d01SWei Wang 	struct rt6_exception *rt6_ex;
147635732d01SWei Wang 
147735732d01SWei Wang 	if (!from ||
1478442d713bSColin Ian King 	    !(rt->rt6i_flags & RTF_CACHE))
147935732d01SWei Wang 		return;
148035732d01SWei Wang 
148135732d01SWei Wang 	rcu_read_lock();
148235732d01SWei Wang 	bucket = rcu_dereference(from->rt6i_exception_bucket);
148335732d01SWei Wang 
148435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
148535732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
148635732d01SWei Wang 	 * and exception table is indexed by a hash of
148735732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
148835732d01SWei Wang 	 * Otherwise, the exception table is indexed by
148935732d01SWei Wang 	 * a hash of only rt6i_dst.
149035732d01SWei Wang 	 */
149135732d01SWei Wang 	if (from->rt6i_src.plen)
149235732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
149335732d01SWei Wang #endif
149435732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket,
149535732d01SWei Wang 					  &rt->rt6i_dst.addr,
149635732d01SWei Wang 					  src_key);
149735732d01SWei Wang 	if (rt6_ex)
149835732d01SWei Wang 		rt6_ex->stamp = jiffies;
149935732d01SWei Wang 
150035732d01SWei Wang 	rcu_read_unlock();
150135732d01SWei Wang }
150235732d01SWei Wang 
150360006a48SWei Wang static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt)
150460006a48SWei Wang {
150560006a48SWei Wang 	struct rt6_exception_bucket *bucket;
150660006a48SWei Wang 	struct rt6_exception *rt6_ex;
150760006a48SWei Wang 	int i;
150860006a48SWei Wang 
150960006a48SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
151060006a48SWei Wang 					lockdep_is_held(&rt6_exception_lock));
151160006a48SWei Wang 
151260006a48SWei Wang 	if (bucket) {
151360006a48SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
151460006a48SWei Wang 			hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
151560006a48SWei Wang 				rt6_ex->rt6i->rt6i_prefsrc.plen = 0;
151660006a48SWei Wang 			}
151760006a48SWei Wang 			bucket++;
151860006a48SWei Wang 		}
151960006a48SWei Wang 	}
152060006a48SWei Wang }
152160006a48SWei Wang 
1522f5bbe7eeSWei Wang static void rt6_exceptions_update_pmtu(struct rt6_info *rt, int mtu)
1523f5bbe7eeSWei Wang {
1524f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
1525f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
1526f5bbe7eeSWei Wang 	int i;
1527f5bbe7eeSWei Wang 
1528f5bbe7eeSWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1529f5bbe7eeSWei Wang 					lockdep_is_held(&rt6_exception_lock));
1530f5bbe7eeSWei Wang 
1531f5bbe7eeSWei Wang 	if (bucket) {
1532f5bbe7eeSWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1533f5bbe7eeSWei Wang 			hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1534f5bbe7eeSWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1535f5bbe7eeSWei Wang 				/* For RTF_CACHE with rt6i_pmtu == 0
1536f5bbe7eeSWei Wang 				 * (i.e. a redirected route),
1537f5bbe7eeSWei Wang 				 * the metrics of its rt->dst.from has already
1538f5bbe7eeSWei Wang 				 * been updated.
1539f5bbe7eeSWei Wang 				 */
1540f5bbe7eeSWei Wang 				if (entry->rt6i_pmtu && entry->rt6i_pmtu > mtu)
1541f5bbe7eeSWei Wang 					entry->rt6i_pmtu = mtu;
1542f5bbe7eeSWei Wang 			}
1543f5bbe7eeSWei Wang 			bucket++;
1544f5bbe7eeSWei Wang 		}
1545f5bbe7eeSWei Wang 	}
1546f5bbe7eeSWei Wang }
1547f5bbe7eeSWei Wang 
1548b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
1549b16cb459SWei Wang 
1550b16cb459SWei Wang static void rt6_exceptions_clean_tohost(struct rt6_info *rt,
1551b16cb459SWei Wang 					struct in6_addr *gateway)
1552b16cb459SWei Wang {
1553b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
1554b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
1555b16cb459SWei Wang 	struct hlist_node *tmp;
1556b16cb459SWei Wang 	int i;
1557b16cb459SWei Wang 
1558b16cb459SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1559b16cb459SWei Wang 		return;
1560b16cb459SWei Wang 
1561b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1562b16cb459SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1563b16cb459SWei Wang 				     lockdep_is_held(&rt6_exception_lock));
1564b16cb459SWei Wang 
1565b16cb459SWei Wang 	if (bucket) {
1566b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1567b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1568b16cb459SWei Wang 						  &bucket->chain, hlist) {
1569b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1570b16cb459SWei Wang 
1571b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1572b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
1573b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
1574b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
1575b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
1576b16cb459SWei Wang 				}
1577b16cb459SWei Wang 			}
1578b16cb459SWei Wang 			bucket++;
1579b16cb459SWei Wang 		}
1580b16cb459SWei Wang 	}
1581b16cb459SWei Wang 
1582b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1583b16cb459SWei Wang }
1584b16cb459SWei Wang 
1585c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1586c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
1587c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
1588c757faa8SWei Wang 				      unsigned long now)
1589c757faa8SWei Wang {
1590c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
1591c757faa8SWei Wang 
15921859bac0SPaolo Abeni 	/* we are pruning and obsoleting aged-out and non gateway exceptions
15931859bac0SPaolo Abeni 	 * even if others have still references to them, so that on next
15941859bac0SPaolo Abeni 	 * dst_check() such references can be dropped.
15951859bac0SPaolo Abeni 	 * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when
15961859bac0SPaolo Abeni 	 * expired, independently from their aging, as per RFC 8201 section 4
15971859bac0SPaolo Abeni 	 */
15981859bac0SPaolo Abeni 	if (!(rt->rt6i_flags & RTF_EXPIRES) &&
1599c757faa8SWei Wang 	    time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1600c757faa8SWei Wang 		RT6_TRACE("aging clone %p\n", rt);
1601c757faa8SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
1602c757faa8SWei Wang 		return;
1603c757faa8SWei Wang 	} else if (rt->rt6i_flags & RTF_GATEWAY) {
1604c757faa8SWei Wang 		struct neighbour *neigh;
1605c757faa8SWei Wang 		__u8 neigh_flags = 0;
1606c757faa8SWei Wang 
1607c757faa8SWei Wang 		neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1608c757faa8SWei Wang 		if (neigh) {
1609c757faa8SWei Wang 			neigh_flags = neigh->flags;
1610c757faa8SWei Wang 			neigh_release(neigh);
1611c757faa8SWei Wang 		}
1612c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
1613c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
1614c757faa8SWei Wang 				  rt);
1615c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1616c757faa8SWei Wang 			return;
1617c757faa8SWei Wang 		}
16181859bac0SPaolo Abeni 	} else if (__rt6_check_expired(rt)) {
16191859bac0SPaolo Abeni 		RT6_TRACE("purging expired route %p\n", rt);
16201859bac0SPaolo Abeni 		rt6_remove_exception(bucket, rt6_ex);
16211859bac0SPaolo Abeni 		return;
1622c757faa8SWei Wang 	}
1623c757faa8SWei Wang 	gc_args->more++;
1624c757faa8SWei Wang }
1625c757faa8SWei Wang 
1626c757faa8SWei Wang void rt6_age_exceptions(struct rt6_info *rt,
1627c757faa8SWei Wang 			struct fib6_gc_args *gc_args,
1628c757faa8SWei Wang 			unsigned long now)
1629c757faa8SWei Wang {
1630c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
1631c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
1632c757faa8SWei Wang 	struct hlist_node *tmp;
1633c757faa8SWei Wang 	int i;
1634c757faa8SWei Wang 
1635c757faa8SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1636c757faa8SWei Wang 		return;
1637c757faa8SWei Wang 
1638c757faa8SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1639c757faa8SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1640c757faa8SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
1641c757faa8SWei Wang 
1642c757faa8SWei Wang 	if (bucket) {
1643c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1644c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1645c757faa8SWei Wang 						  &bucket->chain, hlist) {
1646c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
1647c757faa8SWei Wang 							  gc_args, now);
1648c757faa8SWei Wang 			}
1649c757faa8SWei Wang 			bucket++;
1650c757faa8SWei Wang 		}
1651c757faa8SWei Wang 	}
1652c757faa8SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1653c757faa8SWei Wang }
1654c757faa8SWei Wang 
16559ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
16569ff74384SDavid Ahern 			       int oif, struct flowi6 *fl6, int flags)
16571da177e4SLinus Torvalds {
1658367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
16592b760fcfSWei Wang 	struct rt6_info *rt, *rt_cache;
1660c71099acSThomas Graf 	int strict = 0;
16611da177e4SLinus Torvalds 
166277d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
1663d5d32e4bSDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
1664367efcb9SMartin KaFai Lau 	if (net->ipv6.devconf_all->forwarding == 0)
1665367efcb9SMartin KaFai Lau 		strict |= RT6_LOOKUP_F_REACHABLE;
16661da177e4SLinus Torvalds 
166766f5d6ceSWei Wang 	rcu_read_lock();
16681da177e4SLinus Torvalds 
16694c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1670367efcb9SMartin KaFai Lau 	saved_fn = fn;
16711da177e4SLinus Torvalds 
1672ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1673ca254490SDavid Ahern 		oif = 0;
1674ca254490SDavid Ahern 
1675a3c00e46SMartin KaFai Lau redo_rt6_select:
16768d1040e8SWei Wang 	rt = rt6_select(net, fn, oif, strict);
167752bd4c0cSNicolas Dichtel 	if (rt->rt6i_nsiblings)
1678367efcb9SMartin KaFai Lau 		rt = rt6_multipath_select(rt, fl6, oif, strict);
1679a3c00e46SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
1680a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1681a3c00e46SMartin KaFai Lau 		if (fn)
1682a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
1683367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
1684367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
1685367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
1686367efcb9SMartin KaFai Lau 			fn = saved_fn;
1687367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
1688367efcb9SMartin KaFai Lau 		}
1689a3c00e46SMartin KaFai Lau 	}
1690a3c00e46SMartin KaFai Lau 
16912b760fcfSWei Wang 	/*Search through exception table */
16922b760fcfSWei Wang 	rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr);
16932b760fcfSWei Wang 	if (rt_cache)
16942b760fcfSWei Wang 		rt = rt_cache;
1695d52d3997SMartin KaFai Lau 
1696d3843fe5SWei Wang 	if (rt == net->ipv6.ip6_null_entry) {
169766f5d6ceSWei Wang 		rcu_read_unlock();
1698d3843fe5SWei Wang 		dst_hold(&rt->dst);
1699b65f164dSPaolo Abeni 		trace_fib6_table_lookup(net, rt, table, fl6);
1700d3843fe5SWei Wang 		return rt;
1701d3843fe5SWei Wang 	} else if (rt->rt6i_flags & RTF_CACHE) {
1702d3843fe5SWei Wang 		if (ip6_hold_safe(net, &rt, true)) {
1703d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
1704d52d3997SMartin KaFai Lau 			rt6_dst_from_metrics_check(rt);
1705d3843fe5SWei Wang 		}
170666f5d6ceSWei Wang 		rcu_read_unlock();
1707b65f164dSPaolo Abeni 		trace_fib6_table_lookup(net, rt, table, fl6);
1708d52d3997SMartin KaFai Lau 		return rt;
17093da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
17103da59bd9SMartin KaFai Lau 			    !(rt->rt6i_flags & RTF_GATEWAY))) {
17113da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
17123da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
17133da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
17143da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
17153da59bd9SMartin KaFai Lau 		 */
1716c71099acSThomas Graf 
17173da59bd9SMartin KaFai Lau 		struct rt6_info *uncached_rt;
17183da59bd9SMartin KaFai Lau 
1719d3843fe5SWei Wang 		if (ip6_hold_safe(net, &rt, true)) {
1720d3843fe5SWei Wang 			dst_use_noref(&rt->dst, jiffies);
1721d3843fe5SWei Wang 		} else {
172266f5d6ceSWei Wang 			rcu_read_unlock();
1723d3843fe5SWei Wang 			uncached_rt = rt;
1724d3843fe5SWei Wang 			goto uncached_rt_out;
1725d3843fe5SWei Wang 		}
172666f5d6ceSWei Wang 		rcu_read_unlock();
1727d52d3997SMartin KaFai Lau 
17283da59bd9SMartin KaFai Lau 		uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL);
17293da59bd9SMartin KaFai Lau 		dst_release(&rt->dst);
17303da59bd9SMartin KaFai Lau 
17311cfb71eeSWei Wang 		if (uncached_rt) {
17321cfb71eeSWei Wang 			/* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
17331cfb71eeSWei Wang 			 * No need for another dst_hold()
17341cfb71eeSWei Wang 			 */
17358d0b94afSMartin KaFai Lau 			rt6_uncached_list_add(uncached_rt);
173681eb8447SWei Wang 			atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
17371cfb71eeSWei Wang 		} else {
17383da59bd9SMartin KaFai Lau 			uncached_rt = net->ipv6.ip6_null_entry;
17393da59bd9SMartin KaFai Lau 			dst_hold(&uncached_rt->dst);
17401cfb71eeSWei Wang 		}
1741b811580dSDavid Ahern 
1742d3843fe5SWei Wang uncached_rt_out:
1743b65f164dSPaolo Abeni 		trace_fib6_table_lookup(net, uncached_rt, table, fl6);
17443da59bd9SMartin KaFai Lau 		return uncached_rt;
17453da59bd9SMartin KaFai Lau 
1746d52d3997SMartin KaFai Lau 	} else {
1747d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
1748d52d3997SMartin KaFai Lau 
1749d52d3997SMartin KaFai Lau 		struct rt6_info *pcpu_rt;
1750d52d3997SMartin KaFai Lau 
1751d3843fe5SWei Wang 		dst_use_noref(&rt->dst, jiffies);
1752951f788aSEric Dumazet 		local_bh_disable();
1753d52d3997SMartin KaFai Lau 		pcpu_rt = rt6_get_pcpu_route(rt);
1754d52d3997SMartin KaFai Lau 
1755951f788aSEric Dumazet 		if (!pcpu_rt) {
1756a94b9367SWei Wang 			/* atomic_inc_not_zero() is needed when using rcu */
1757a94b9367SWei Wang 			if (atomic_inc_not_zero(&rt->rt6i_ref)) {
1758951f788aSEric Dumazet 				/* No dst_hold() on rt is needed because grabbing
1759a94b9367SWei Wang 				 * rt->rt6i_ref makes sure rt can't be released.
17609c7370a1SMartin KaFai Lau 				 */
17619c7370a1SMartin KaFai Lau 				pcpu_rt = rt6_make_pcpu_route(rt);
1762a94b9367SWei Wang 				rt6_release(rt);
1763a94b9367SWei Wang 			} else {
1764a94b9367SWei Wang 				/* rt is already removed from tree */
1765a94b9367SWei Wang 				pcpu_rt = net->ipv6.ip6_null_entry;
1766a94b9367SWei Wang 				dst_hold(&pcpu_rt->dst);
1767a94b9367SWei Wang 			}
17689c7370a1SMartin KaFai Lau 		}
1769951f788aSEric Dumazet 		local_bh_enable();
1770951f788aSEric Dumazet 		rcu_read_unlock();
1771b65f164dSPaolo Abeni 		trace_fib6_table_lookup(net, pcpu_rt, table, fl6);
1772d52d3997SMartin KaFai Lau 		return pcpu_rt;
1773d52d3997SMartin KaFai Lau 	}
1774c71099acSThomas Graf }
17759ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
1776c71099acSThomas Graf 
17778ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
17784c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
17794acad72dSPavel Emelyanov {
17804c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
17814acad72dSPavel Emelyanov }
17824acad72dSPavel Emelyanov 
1783d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
178472331bc0SShmulik Ladkani 					 struct net_device *dev,
178572331bc0SShmulik Ladkani 					 struct flowi6 *fl6, int flags)
178672331bc0SShmulik Ladkani {
178772331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
178872331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
178972331bc0SShmulik Ladkani 
179072331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
179172331bc0SShmulik Ladkani }
1792d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
179372331bc0SShmulik Ladkani 
179423aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
179523aebdacSJakub Sitnicki 				  struct flow_keys *keys)
179623aebdacSJakub Sitnicki {
179723aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
179823aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
179923aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
180023aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
180123aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
180223aebdacSJakub Sitnicki 
180323aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
180423aebdacSJakub Sitnicki 		goto out;
180523aebdacSJakub Sitnicki 
180623aebdacSJakub Sitnicki 	icmph = icmp6_hdr(skb);
180723aebdacSJakub Sitnicki 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
180823aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
180923aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
181023aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
181123aebdacSJakub Sitnicki 		goto out;
181223aebdacSJakub Sitnicki 
181323aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
181423aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
181523aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
181623aebdacSJakub Sitnicki 	if (!inner_iph)
181723aebdacSJakub Sitnicki 		goto out;
181823aebdacSJakub Sitnicki 
181923aebdacSJakub Sitnicki 	key_iph = inner_iph;
182023aebdacSJakub Sitnicki out:
182123aebdacSJakub Sitnicki 	memset(keys, 0, sizeof(*keys));
182223aebdacSJakub Sitnicki 	keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
182323aebdacSJakub Sitnicki 	keys->addrs.v6addrs.src = key_iph->saddr;
182423aebdacSJakub Sitnicki 	keys->addrs.v6addrs.dst = key_iph->daddr;
182523aebdacSJakub Sitnicki 	keys->tags.flow_label = ip6_flowinfo(key_iph);
182623aebdacSJakub Sitnicki 	keys->basic.ip_proto = key_iph->nexthdr;
182723aebdacSJakub Sitnicki }
182823aebdacSJakub Sitnicki 
182923aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
183023aebdacSJakub Sitnicki u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb)
183123aebdacSJakub Sitnicki {
183223aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
183323aebdacSJakub Sitnicki 
183423aebdacSJakub Sitnicki 	if (skb) {
183523aebdacSJakub Sitnicki 		ip6_multipath_l3_keys(skb, &hash_keys);
183623aebdacSJakub Sitnicki 		return flow_hash_from_keys(&hash_keys);
183723aebdacSJakub Sitnicki 	}
183823aebdacSJakub Sitnicki 
183923aebdacSJakub Sitnicki 	return get_hash_from_flowi6(fl6);
184023aebdacSJakub Sitnicki }
184123aebdacSJakub Sitnicki 
1842c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
1843c71099acSThomas Graf {
1844b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
1845c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
1846adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1847904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
18484c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
1849e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
18504c9483b2SDavid S. Miller 		.daddr = iph->daddr,
18514c9483b2SDavid S. Miller 		.saddr = iph->saddr,
18526502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
18534c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
18544c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
1855c71099acSThomas Graf 	};
1856adaa70bbSThomas Graf 
1857904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
185846fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
1859904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
186023aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
186123aebdacSJakub Sitnicki 		fl6.mp_hash = rt6_multipath_hash(&fl6, skb);
186206e9d040SJiri Benc 	skb_dst_drop(skb);
186372331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
1864c71099acSThomas Graf }
1865c71099acSThomas Graf 
18668ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
18674c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
1868c71099acSThomas Graf {
18694c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
1870c71099acSThomas Graf }
1871c71099acSThomas Graf 
18726f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
18736f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags)
1874c71099acSThomas Graf {
1875d46a9d67SDavid Ahern 	bool any_src;
1876c71099acSThomas Graf 
18774c1feac5SDavid Ahern 	if (rt6_need_strict(&fl6->daddr)) {
18784c1feac5SDavid Ahern 		struct dst_entry *dst;
18794c1feac5SDavid Ahern 
18804c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
1881ca254490SDavid Ahern 		if (dst)
1882ca254490SDavid Ahern 			return dst;
18834c1feac5SDavid Ahern 	}
1884ca254490SDavid Ahern 
18851fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
18864dc27d1cSDavid McCullough 
1887d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
1888741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
1889d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
189077d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
1891c71099acSThomas Graf 
1892d46a9d67SDavid Ahern 	if (!any_src)
1893adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
18940c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
18950c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
1896adaa70bbSThomas Graf 
18974c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
18981da177e4SLinus Torvalds }
18996f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
19001da177e4SLinus Torvalds 
19012774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
190214e50e57SDavid S. Miller {
19035c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
19041dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
190514e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
190614e50e57SDavid S. Miller 
19071dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
190862cf27e5SSteffen Klassert 		       DST_OBSOLETE_DEAD, 0);
190914e50e57SDavid S. Miller 	if (rt) {
19100a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
191181eb8447SWei Wang 		atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc);
19120a1f5962SMartin KaFai Lau 
1913d8d1f30bSChangli Gao 		new = &rt->dst;
191414e50e57SDavid S. Miller 		new->__use = 1;
1915352e512cSHerbert Xu 		new->input = dst_discard;
1916ede2059dSEric W. Biederman 		new->output = dst_discard_out;
191714e50e57SDavid S. Miller 
1918defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
191914e50e57SDavid S. Miller 
19201dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
19214e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
19220a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
192314e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
192414e50e57SDavid S. Miller 
192514e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
192614e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
192714e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
192814e50e57SDavid S. Miller #endif
192914e50e57SDavid S. Miller 	}
193014e50e57SDavid S. Miller 
193169ead7afSDavid S. Miller 	dst_release(dst_orig);
193269ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
193314e50e57SDavid S. Miller }
193414e50e57SDavid S. Miller 
19351da177e4SLinus Torvalds /*
19361da177e4SLinus Torvalds  *	Destination cache support functions
19371da177e4SLinus Torvalds  */
19381da177e4SLinus Torvalds 
19394b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt)
19404b32b5adSMartin KaFai Lau {
19413a2232e9SDavid Miller 	if (rt->from &&
19423a2232e9SDavid Miller 	    dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(&rt->from->dst))
19433a2232e9SDavid Miller 		dst_init_metrics(&rt->dst, dst_metrics_ptr(&rt->from->dst), true);
19444b32b5adSMartin KaFai Lau }
19454b32b5adSMartin KaFai Lau 
19463da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie)
19473da59bd9SMartin KaFai Lau {
194836143645SSteffen Klassert 	u32 rt_cookie = 0;
1949c5cff856SWei Wang 
1950c5cff856SWei Wang 	if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie)
19513da59bd9SMartin KaFai Lau 		return NULL;
19523da59bd9SMartin KaFai Lau 
19533da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
19543da59bd9SMartin KaFai Lau 		return NULL;
19553da59bd9SMartin KaFai Lau 
19563da59bd9SMartin KaFai Lau 	return &rt->dst;
19573da59bd9SMartin KaFai Lau }
19583da59bd9SMartin KaFai Lau 
19593da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie)
19603da59bd9SMartin KaFai Lau {
19615973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
19625973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
19633a2232e9SDavid Miller 	    rt6_check(rt->from, cookie))
19643da59bd9SMartin KaFai Lau 		return &rt->dst;
19653da59bd9SMartin KaFai Lau 	else
19663da59bd9SMartin KaFai Lau 		return NULL;
19673da59bd9SMartin KaFai Lau }
19683da59bd9SMartin KaFai Lau 
19691da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
19701da177e4SLinus Torvalds {
19711da177e4SLinus Torvalds 	struct rt6_info *rt;
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
19741da177e4SLinus Torvalds 
19756f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
19766f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
19776f3118b5SNicolas Dichtel 	 * into this function always.
19786f3118b5SNicolas Dichtel 	 */
1979e3bc10bdSHannes Frederic Sowa 
19804b32b5adSMartin KaFai Lau 	rt6_dst_from_metrics_check(rt);
19814b32b5adSMartin KaFai Lau 
198202bcf4e0SMartin KaFai Lau 	if (rt->rt6i_flags & RTF_PCPU ||
19833a2232e9SDavid Miller 	    (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from))
19843da59bd9SMartin KaFai Lau 		return rt6_dst_from_check(rt, cookie);
19853da59bd9SMartin KaFai Lau 	else
19863da59bd9SMartin KaFai Lau 		return rt6_check(rt, cookie);
19871da177e4SLinus Torvalds }
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
19901da177e4SLinus Torvalds {
19911da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds 	if (rt) {
199454c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
199554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1996e0a1ad73SThomas Graf 				ip6_del_rt(rt);
199754c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
19981da177e4SLinus Torvalds 			}
199954c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
200054c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
200154c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
200254c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
200354c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
200454c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
20081da177e4SLinus Torvalds {
20091da177e4SLinus Torvalds 	struct rt6_info *rt;
20101da177e4SLinus Torvalds 
20113ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
20121da177e4SLinus Torvalds 
2013adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
20141da177e4SLinus Torvalds 	if (rt) {
20151eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
2016ad65a2f0SWei Wang 			if (dst_hold_safe(&rt->dst))
20178e3d5be7SMartin KaFai Lau 				ip6_del_rt(rt);
2018c5cff856SWei Wang 		} else {
2019c5cff856SWei Wang 			struct fib6_node *fn;
2020c5cff856SWei Wang 
2021c5cff856SWei Wang 			rcu_read_lock();
2022c5cff856SWei Wang 			fn = rcu_dereference(rt->rt6i_node);
2023c5cff856SWei Wang 			if (fn && (rt->rt6i_flags & RTF_DEFAULT))
2024c5cff856SWei Wang 				fn->fn_sernum = -1;
2025c5cff856SWei Wang 			rcu_read_unlock();
20261da177e4SLinus Torvalds 		}
20271da177e4SLinus Torvalds 	}
20281eb4f758SHannes Frederic Sowa }
20291da177e4SLinus Torvalds 
203045e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
203145e4fd26SMartin KaFai Lau {
203245e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
203345e4fd26SMartin KaFai Lau 
203445e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
203545e4fd26SMartin KaFai Lau 	rt->rt6i_pmtu = mtu;
203645e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
203745e4fd26SMartin KaFai Lau }
203845e4fd26SMartin KaFai Lau 
20390d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
20400d3f6d29SMartin KaFai Lau {
20410d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
20424e587ea7SWei Wang 		(rt->rt6i_flags & RTF_PCPU ||
20434e587ea7SWei Wang 		 rcu_access_pointer(rt->rt6i_node));
20440d3f6d29SMartin KaFai Lau }
20450d3f6d29SMartin KaFai Lau 
204645e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
204745e4fd26SMartin KaFai Lau 				 const struct ipv6hdr *iph, u32 mtu)
20481da177e4SLinus Torvalds {
20490dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
20501da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
20511da177e4SLinus Torvalds 
205245e4fd26SMartin KaFai Lau 	if (rt6->rt6i_flags & RTF_LOCAL)
205345e4fd26SMartin KaFai Lau 		return;
205445e4fd26SMartin KaFai Lau 
205519bda36cSXin Long 	if (dst_metric_locked(dst, RTAX_MTU))
205619bda36cSXin Long 		return;
205719bda36cSXin Long 
205845e4fd26SMartin KaFai Lau 	if (iph) {
205945e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
206045e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
206145e4fd26SMartin KaFai Lau 	} else if (sk) {
206245e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
206345e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
206445e4fd26SMartin KaFai Lau 	} else {
20650dec879fSJulian Anastasov 		daddr = NULL;
20660dec879fSJulian Anastasov 		saddr = NULL;
20671da177e4SLinus Torvalds 	}
20680dec879fSJulian Anastasov 	dst_confirm_neigh(dst, daddr);
20690dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
20700dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
20710dec879fSJulian Anastasov 		return;
20720dec879fSJulian Anastasov 
20730dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
20740dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
20752b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
20762b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
20772b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
20780dec879fSJulian Anastasov 	} else if (daddr) {
20790dec879fSJulian Anastasov 		struct rt6_info *nrt6;
20800dec879fSJulian Anastasov 
208145e4fd26SMartin KaFai Lau 		nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr);
208245e4fd26SMartin KaFai Lau 		if (nrt6) {
208345e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
20842b760fcfSWei Wang 			if (rt6_insert_exception(nrt6, rt6))
20852b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
208645e4fd26SMartin KaFai Lau 		}
208745e4fd26SMartin KaFai Lau 	}
208845e4fd26SMartin KaFai Lau }
208945e4fd26SMartin KaFai Lau 
209045e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
209145e4fd26SMartin KaFai Lau 			       struct sk_buff *skb, u32 mtu)
209245e4fd26SMartin KaFai Lau {
209345e4fd26SMartin KaFai Lau 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
20941da177e4SLinus Torvalds }
20951da177e4SLinus Torvalds 
209642ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2097e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
209881aded24SDavid S. Miller {
209981aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
210081aded24SDavid S. Miller 	struct dst_entry *dst;
210181aded24SDavid S. Miller 	struct flowi6 fl6;
210281aded24SDavid S. Miller 
210381aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
210481aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
21051b3c61dcSLorenzo Colitti 	fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
210681aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
210781aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
21086502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
2109e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = uid;
211081aded24SDavid S. Miller 
211181aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
211281aded24SDavid S. Miller 	if (!dst->error)
211345e4fd26SMartin KaFai Lau 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
211481aded24SDavid S. Miller 	dst_release(dst);
211581aded24SDavid S. Miller }
211681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
211781aded24SDavid S. Miller 
211881aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
211981aded24SDavid S. Miller {
212033c162a9SMartin KaFai Lau 	struct dst_entry *dst;
212133c162a9SMartin KaFai Lau 
212281aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
2123e2d118a1SLorenzo Colitti 			sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid);
212433c162a9SMartin KaFai Lau 
212533c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
212633c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
212733c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
212833c162a9SMartin KaFai Lau 		return;
212933c162a9SMartin KaFai Lau 
213033c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
213133c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
213233c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
213333c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
213481aded24SDavid S. Miller }
213581aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
213681aded24SDavid S. Miller 
2137b55b76b2SDuan Jiong /* Handle redirects */
2138b55b76b2SDuan Jiong struct ip6rd_flowi {
2139b55b76b2SDuan Jiong 	struct flowi6 fl6;
2140b55b76b2SDuan Jiong 	struct in6_addr gateway;
2141b55b76b2SDuan Jiong };
2142b55b76b2SDuan Jiong 
2143b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
2144b55b76b2SDuan Jiong 					     struct fib6_table *table,
2145b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2146b55b76b2SDuan Jiong 					     int flags)
2147b55b76b2SDuan Jiong {
2148b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
21492b760fcfSWei Wang 	struct rt6_info *rt, *rt_cache;
2150b55b76b2SDuan Jiong 	struct fib6_node *fn;
2151b55b76b2SDuan Jiong 
2152b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
215367c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2154b55b76b2SDuan Jiong 	 *
2155b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2156b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2157b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2158b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2159b55b76b2SDuan Jiong 	 * routes.
2160b55b76b2SDuan Jiong 	 */
2161b55b76b2SDuan Jiong 
216266f5d6ceSWei Wang 	rcu_read_lock();
2163b55b76b2SDuan Jiong 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2164b55b76b2SDuan Jiong restart:
216566f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
21668067bb8cSIdo Schimmel 		if (rt->rt6i_nh_flags & RTNH_F_DEAD)
21678067bb8cSIdo Schimmel 			continue;
2168b55b76b2SDuan Jiong 		if (rt6_check_expired(rt))
2169b55b76b2SDuan Jiong 			continue;
2170b55b76b2SDuan Jiong 		if (rt->dst.error)
2171b55b76b2SDuan Jiong 			break;
2172b55b76b2SDuan Jiong 		if (!(rt->rt6i_flags & RTF_GATEWAY))
2173b55b76b2SDuan Jiong 			continue;
2174b55b76b2SDuan Jiong 		if (fl6->flowi6_oif != rt->dst.dev->ifindex)
2175b55b76b2SDuan Jiong 			continue;
21762b760fcfSWei Wang 		/* rt_cache's gateway might be different from its 'parent'
21772b760fcfSWei Wang 		 * in the case of an ip redirect.
21782b760fcfSWei Wang 		 * So we keep searching in the exception table if the gateway
21792b760fcfSWei Wang 		 * is different.
21802b760fcfSWei Wang 		 */
21812b760fcfSWei Wang 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) {
21822b760fcfSWei Wang 			rt_cache = rt6_find_cached_rt(rt,
21832b760fcfSWei Wang 						      &fl6->daddr,
21842b760fcfSWei Wang 						      &fl6->saddr);
21852b760fcfSWei Wang 			if (rt_cache &&
21862b760fcfSWei Wang 			    ipv6_addr_equal(&rdfl->gateway,
21872b760fcfSWei Wang 					    &rt_cache->rt6i_gateway)) {
21882b760fcfSWei Wang 				rt = rt_cache;
21892b760fcfSWei Wang 				break;
21902b760fcfSWei Wang 			}
2191b55b76b2SDuan Jiong 			continue;
21922b760fcfSWei Wang 		}
2193b55b76b2SDuan Jiong 		break;
2194b55b76b2SDuan Jiong 	}
2195b55b76b2SDuan Jiong 
2196b55b76b2SDuan Jiong 	if (!rt)
2197b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
2198b55b76b2SDuan Jiong 	else if (rt->dst.error) {
2199b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
2200b0a1ba59SMartin KaFai Lau 		goto out;
2201b0a1ba59SMartin KaFai Lau 	}
2202b0a1ba59SMartin KaFai Lau 
2203b0a1ba59SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
2204a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2205a3c00e46SMartin KaFai Lau 		if (fn)
2206a3c00e46SMartin KaFai Lau 			goto restart;
2207b55b76b2SDuan Jiong 	}
2208a3c00e46SMartin KaFai Lau 
2209b0a1ba59SMartin KaFai Lau out:
2210d3843fe5SWei Wang 	ip6_hold_safe(net, &rt, true);
2211b55b76b2SDuan Jiong 
221266f5d6ceSWei Wang 	rcu_read_unlock();
2213b55b76b2SDuan Jiong 
2214b65f164dSPaolo Abeni 	trace_fib6_table_lookup(net, rt, table, fl6);
2215b55b76b2SDuan Jiong 	return rt;
2216b55b76b2SDuan Jiong };
2217b55b76b2SDuan Jiong 
2218b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
2219b55b76b2SDuan Jiong 					const struct flowi6 *fl6,
2220b55b76b2SDuan Jiong 					const struct in6_addr *gateway)
2221b55b76b2SDuan Jiong {
2222b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2223b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
2224b55b76b2SDuan Jiong 
2225b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
2226b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
2227b55b76b2SDuan Jiong 
2228b55b76b2SDuan Jiong 	return fib6_rule_lookup(net, &rdfl.fl6,
2229b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
2230b55b76b2SDuan Jiong }
2231b55b76b2SDuan Jiong 
2232e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2233e2d118a1SLorenzo Colitti 		  kuid_t uid)
22343a5ad2eeSDavid S. Miller {
22353a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
22363a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
22373a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
22383a5ad2eeSDavid S. Miller 
22393a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2240e374c618SJulian Anastasov 	fl6.flowi6_iif = LOOPBACK_IFINDEX;
22413a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
22423a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
22433a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
22443a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
22456502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
2246e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = uid;
22473a5ad2eeSDavid S. Miller 
2248b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
22496700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
22503a5ad2eeSDavid S. Miller 	dst_release(dst);
22513a5ad2eeSDavid S. Miller }
22523a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
22533a5ad2eeSDavid S. Miller 
2254c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
2255c92a59ecSDuan Jiong 			    u32 mark)
2256c92a59ecSDuan Jiong {
2257c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2258c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2259c92a59ecSDuan Jiong 	struct dst_entry *dst;
2260c92a59ecSDuan Jiong 	struct flowi6 fl6;
2261c92a59ecSDuan Jiong 
2262c92a59ecSDuan Jiong 	memset(&fl6, 0, sizeof(fl6));
2263e374c618SJulian Anastasov 	fl6.flowi6_iif = LOOPBACK_IFINDEX;
2264c92a59ecSDuan Jiong 	fl6.flowi6_oif = oif;
2265c92a59ecSDuan Jiong 	fl6.flowi6_mark = mark;
2266c92a59ecSDuan Jiong 	fl6.daddr = msg->dest;
2267c92a59ecSDuan Jiong 	fl6.saddr = iph->daddr;
2268e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sock_net_uid(net, NULL);
2269c92a59ecSDuan Jiong 
2270b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &iph->saddr);
2271c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
2272c92a59ecSDuan Jiong 	dst_release(dst);
2273c92a59ecSDuan Jiong }
2274c92a59ecSDuan Jiong 
22753a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
22763a5ad2eeSDavid S. Miller {
2277e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2278e2d118a1SLorenzo Colitti 		     sk->sk_uid);
22793a5ad2eeSDavid S. Miller }
22803a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
22813a5ad2eeSDavid S. Miller 
22820dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
22831da177e4SLinus Torvalds {
22840dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
22850dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
22860dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
22870dbaee3bSDavid S. Miller 
22881da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
22891da177e4SLinus Torvalds 
22905578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
22915578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
22921da177e4SLinus Torvalds 
22931da177e4SLinus Torvalds 	/*
22941da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
22951da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
22961da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
22971da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
22981da177e4SLinus Torvalds 	 */
22991da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
23001da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
23011da177e4SLinus Torvalds 	return mtu;
23021da177e4SLinus Torvalds }
23031da177e4SLinus Torvalds 
2304ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
2305d33e4553SDavid S. Miller {
23064b32b5adSMartin KaFai Lau 	const struct rt6_info *rt = (const struct rt6_info *)dst;
23074b32b5adSMartin KaFai Lau 	unsigned int mtu = rt->rt6i_pmtu;
2308d33e4553SDavid S. Miller 	struct inet6_dev *idev;
2309618f9bc7SSteffen Klassert 
2310618f9bc7SSteffen Klassert 	if (mtu)
231130f78d8eSEric Dumazet 		goto out;
2312618f9bc7SSteffen Klassert 
23134b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
23144b32b5adSMartin KaFai Lau 	if (mtu)
23154b32b5adSMartin KaFai Lau 		goto out;
23164b32b5adSMartin KaFai Lau 
2317618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
2318d33e4553SDavid S. Miller 
2319d33e4553SDavid S. Miller 	rcu_read_lock();
2320d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
2321d33e4553SDavid S. Miller 	if (idev)
2322d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
2323d33e4553SDavid S. Miller 	rcu_read_unlock();
2324d33e4553SDavid S. Miller 
232530f78d8eSEric Dumazet out:
232614972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
232714972cbdSRoopa Prabhu 
232814972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
2329d33e4553SDavid S. Miller }
2330d33e4553SDavid S. Miller 
23313b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
233287a11578SDavid S. Miller 				  struct flowi6 *fl6)
23331da177e4SLinus Torvalds {
233487a11578SDavid S. Miller 	struct dst_entry *dst;
23351da177e4SLinus Torvalds 	struct rt6_info *rt;
23361da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
2337c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
23381da177e4SLinus Torvalds 
233938308473SDavid S. Miller 	if (unlikely(!idev))
2340122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
23411da177e4SLinus Torvalds 
2342ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
234338308473SDavid S. Miller 	if (unlikely(!rt)) {
23441da177e4SLinus Torvalds 		in6_dev_put(idev);
234587a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
23461da177e4SLinus Torvalds 		goto out;
23471da177e4SLinus Torvalds 	}
23481da177e4SLinus Torvalds 
23498e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
2350588753f1SBrendan McGrath 	rt->dst.input = ip6_input;
23518e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
2352550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
235387a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
23548e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
23558e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
235614edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
23571da177e4SLinus Torvalds 
23584c981e28SIdo Schimmel 	/* Add this dst into uncached_list so that rt6_disable_ip() can
2359587fea74SWei Wang 	 * do proper release of the net_device
2360587fea74SWei Wang 	 */
2361587fea74SWei Wang 	rt6_uncached_list_add(rt);
236281eb8447SWei Wang 	atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache);
23631da177e4SLinus Torvalds 
236487a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
236587a11578SDavid S. Miller 
23661da177e4SLinus Torvalds out:
236787a11578SDavid S. Miller 	return dst;
23681da177e4SLinus Torvalds }
23691da177e4SLinus Torvalds 
2370569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
23711da177e4SLinus Torvalds {
237286393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
23737019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
23747019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
23757019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
23767019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
23777019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
2378fc66f95cSEric Dumazet 	int entries;
23791da177e4SLinus Torvalds 
2380fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
238149a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
2382fc66f95cSEric Dumazet 	    entries <= rt_max_size)
23831da177e4SLinus Torvalds 		goto out;
23841da177e4SLinus Torvalds 
23856891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
238614956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
2387fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
2388fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
23897019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
23901da177e4SLinus Torvalds out:
23917019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
2392fc66f95cSEric Dumazet 	return entries > rt_max_size;
23931da177e4SLinus Torvalds }
23941da177e4SLinus Torvalds 
2395e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc,
2396e715b6d3SFlorian Westphal 			       const struct fib6_config *cfg)
2397e715b6d3SFlorian Westphal {
23986670e152SStephen Hemminger 	struct net *net = cfg->fc_nlinfo.nl_net;
2399c3a8d947SDaniel Borkmann 	bool ecn_ca = false;
2400e715b6d3SFlorian Westphal 	struct nlattr *nla;
2401e715b6d3SFlorian Westphal 	int remaining;
2402e715b6d3SFlorian Westphal 	u32 *mp;
2403e715b6d3SFlorian Westphal 
240463159f29SIan Morris 	if (!cfg->fc_mx)
2405e715b6d3SFlorian Westphal 		return 0;
2406e715b6d3SFlorian Westphal 
2407e715b6d3SFlorian Westphal 	mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
2408e715b6d3SFlorian Westphal 	if (unlikely(!mp))
2409e715b6d3SFlorian Westphal 		return -ENOMEM;
2410e715b6d3SFlorian Westphal 
2411e715b6d3SFlorian Westphal 	nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
2412e715b6d3SFlorian Westphal 		int type = nla_type(nla);
2413ea697639SDaniel Borkmann 		u32 val;
2414ea697639SDaniel Borkmann 
24151bb14807SDaniel Borkmann 		if (!type)
24161bb14807SDaniel Borkmann 			continue;
2417e715b6d3SFlorian Westphal 		if (unlikely(type > RTAX_MAX))
2418e715b6d3SFlorian Westphal 			goto err;
24191bb14807SDaniel Borkmann 
2420ea697639SDaniel Borkmann 		if (type == RTAX_CC_ALGO) {
2421ea697639SDaniel Borkmann 			char tmp[TCP_CA_NAME_MAX];
2422e715b6d3SFlorian Westphal 
2423ea697639SDaniel Borkmann 			nla_strlcpy(tmp, nla, sizeof(tmp));
24246670e152SStephen Hemminger 			val = tcp_ca_get_key_by_name(net, tmp, &ecn_ca);
2425ea697639SDaniel Borkmann 			if (val == TCP_CA_UNSPEC)
2426ea697639SDaniel Borkmann 				goto err;
2427ea697639SDaniel Borkmann 		} else {
2428ea697639SDaniel Borkmann 			val = nla_get_u32(nla);
2429ea697639SDaniel Borkmann 		}
2430626abd59SPaolo Abeni 		if (type == RTAX_HOPLIMIT && val > 255)
2431626abd59SPaolo Abeni 			val = 255;
2432b8d3e416SDaniel Borkmann 		if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
2433b8d3e416SDaniel Borkmann 			goto err;
2434ea697639SDaniel Borkmann 
2435ea697639SDaniel Borkmann 		mp[type - 1] = val;
2436e715b6d3SFlorian Westphal 		__set_bit(type - 1, mxc->mx_valid);
2437e715b6d3SFlorian Westphal 	}
2438e715b6d3SFlorian Westphal 
2439c3a8d947SDaniel Borkmann 	if (ecn_ca) {
2440c3a8d947SDaniel Borkmann 		__set_bit(RTAX_FEATURES - 1, mxc->mx_valid);
2441c3a8d947SDaniel Borkmann 		mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
2442c3a8d947SDaniel Borkmann 	}
2443e715b6d3SFlorian Westphal 
2444c3a8d947SDaniel Borkmann 	mxc->mx = mp;
2445e715b6d3SFlorian Westphal 	return 0;
2446e715b6d3SFlorian Westphal  err:
2447e715b6d3SFlorian Westphal 	kfree(mp);
2448e715b6d3SFlorian Westphal 	return -EINVAL;
2449e715b6d3SFlorian Westphal }
24501da177e4SLinus Torvalds 
24518c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net,
24528c14586fSDavid Ahern 					    struct fib6_config *cfg,
24538c14586fSDavid Ahern 					    const struct in6_addr *gw_addr)
24548c14586fSDavid Ahern {
24558c14586fSDavid Ahern 	struct flowi6 fl6 = {
24568c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
24578c14586fSDavid Ahern 		.daddr = *gw_addr,
24588c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
24598c14586fSDavid Ahern 	};
24608c14586fSDavid Ahern 	struct fib6_table *table;
24618c14586fSDavid Ahern 	struct rt6_info *rt;
2462d5d32e4bSDavid Ahern 	int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE;
24638c14586fSDavid Ahern 
24648c14586fSDavid Ahern 	table = fib6_get_table(net, cfg->fc_table);
24658c14586fSDavid Ahern 	if (!table)
24668c14586fSDavid Ahern 		return NULL;
24678c14586fSDavid Ahern 
24688c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
24698c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
24708c14586fSDavid Ahern 
24718c14586fSDavid Ahern 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags);
24728c14586fSDavid Ahern 
24738c14586fSDavid Ahern 	/* if table lookup failed, fall back to full lookup */
24748c14586fSDavid Ahern 	if (rt == net->ipv6.ip6_null_entry) {
24758c14586fSDavid Ahern 		ip6_rt_put(rt);
24768c14586fSDavid Ahern 		rt = NULL;
24778c14586fSDavid Ahern 	}
24788c14586fSDavid Ahern 
24798c14586fSDavid Ahern 	return rt;
24808c14586fSDavid Ahern }
24818c14586fSDavid Ahern 
2482333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg,
2483333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
24841da177e4SLinus Torvalds {
24855578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
24861da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
24871da177e4SLinus Torvalds 	struct net_device *dev = NULL;
24881da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
2489c71099acSThomas Graf 	struct fib6_table *table;
24901da177e4SLinus Torvalds 	int addr_type;
24918c5b83f0SRoopa Prabhu 	int err = -EINVAL;
24921da177e4SLinus Torvalds 
2493557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
2494d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
2495d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
2496557c44beSDavid Ahern 		goto out;
2497d5d531cbSDavid Ahern 	}
2498557c44beSDavid Ahern 
24992ea2352eSWei Wang 	/* RTF_CACHE is an internal flag; can not be set by userspace */
25002ea2352eSWei Wang 	if (cfg->fc_flags & RTF_CACHE) {
25012ea2352eSWei Wang 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE");
25022ea2352eSWei Wang 		goto out;
25032ea2352eSWei Wang 	}
25042ea2352eSWei Wang 
2505d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
2506d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
25078c5b83f0SRoopa Prabhu 		goto out;
2508d5d531cbSDavid Ahern 	}
2509d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
2510d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
2511d5d531cbSDavid Ahern 		goto out;
2512d5d531cbSDavid Ahern 	}
25131da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
2514d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
2515d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
2516d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
25178c5b83f0SRoopa Prabhu 		goto out;
2518d5d531cbSDavid Ahern 	}
25191da177e4SLinus Torvalds #endif
252086872cb5SThomas Graf 	if (cfg->fc_ifindex) {
25211da177e4SLinus Torvalds 		err = -ENODEV;
25225578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
25231da177e4SLinus Torvalds 		if (!dev)
25241da177e4SLinus Torvalds 			goto out;
25251da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
25261da177e4SLinus Torvalds 		if (!idev)
25271da177e4SLinus Torvalds 			goto out;
25281da177e4SLinus Torvalds 	}
25291da177e4SLinus Torvalds 
253086872cb5SThomas Graf 	if (cfg->fc_metric == 0)
253186872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
25321da177e4SLinus Torvalds 
2533c71099acSThomas Graf 	err = -ENOBUFS;
253438308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
2535d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
2536d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
253738308473SDavid S. Miller 		if (!table) {
2538f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
2539d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
2540d71314b4SMatti Vaittinen 		}
2541d71314b4SMatti Vaittinen 	} else {
2542d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
2543d71314b4SMatti Vaittinen 	}
254438308473SDavid S. Miller 
254538308473SDavid S. Miller 	if (!table)
2546c71099acSThomas Graf 		goto out;
2547c71099acSThomas Graf 
2548ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, NULL,
2549ad706862SMartin KaFai Lau 			   (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT);
25501da177e4SLinus Torvalds 
255138308473SDavid S. Miller 	if (!rt) {
25521da177e4SLinus Torvalds 		err = -ENOMEM;
25531da177e4SLinus Torvalds 		goto out;
25541da177e4SLinus Torvalds 	}
25551da177e4SLinus Torvalds 
25561716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
25571716a961SGao feng 		rt6_set_expires(rt, jiffies +
25581716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
25591716a961SGao feng 	else
25601716a961SGao feng 		rt6_clean_expires(rt);
25611da177e4SLinus Torvalds 
256286872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
256386872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
256486872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
256586872cb5SThomas Graf 
256686872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
25671da177e4SLinus Torvalds 
25681da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
2569d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
2570ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
2571ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
25721da177e4SLinus Torvalds 	else
2573d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
25741da177e4SLinus Torvalds 
2575d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
25761da177e4SLinus Torvalds 
257719e42e45SRoopa Prabhu 	if (cfg->fc_encap) {
257819e42e45SRoopa Prabhu 		struct lwtunnel_state *lwtstate;
257919e42e45SRoopa Prabhu 
258030357d7dSDavid Ahern 		err = lwtunnel_build_state(cfg->fc_encap_type,
2581127eb7cdSTom Herbert 					   cfg->fc_encap, AF_INET6, cfg,
25829ae28727SDavid Ahern 					   &lwtstate, extack);
258319e42e45SRoopa Prabhu 		if (err)
258419e42e45SRoopa Prabhu 			goto out;
258561adedf3SJiri Benc 		rt->dst.lwtstate = lwtstate_get(lwtstate);
258661adedf3SJiri Benc 		if (lwtunnel_output_redirect(rt->dst.lwtstate)) {
258761adedf3SJiri Benc 			rt->dst.lwtstate->orig_output = rt->dst.output;
258861adedf3SJiri Benc 			rt->dst.output = lwtunnel_output;
258919e42e45SRoopa Prabhu 		}
259061adedf3SJiri Benc 		if (lwtunnel_input_redirect(rt->dst.lwtstate)) {
259161adedf3SJiri Benc 			rt->dst.lwtstate->orig_input = rt->dst.input;
259261adedf3SJiri Benc 			rt->dst.input = lwtunnel_input;
259325368623STom Herbert 		}
259425368623STom Herbert 	}
259519e42e45SRoopa Prabhu 
259686872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
259786872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
2598afc4eef8SMartin KaFai Lau 	if (rt->rt6i_dst.plen == 128)
259911d53b49SDavid S. Miller 		rt->dst.flags |= DST_HOST;
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
260286872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
260386872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
26041da177e4SLinus Torvalds #endif
26051da177e4SLinus Torvalds 
260686872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
26071da177e4SLinus Torvalds 
26081da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
26091da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
26101da177e4SLinus Torvalds 	 */
261186872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
261238308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
261338308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
261438308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
26151da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
26165578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
26171da177e4SLinus Torvalds 			if (dev) {
26181da177e4SLinus Torvalds 				dev_put(dev);
26191da177e4SLinus Torvalds 				in6_dev_put(idev);
26201da177e4SLinus Torvalds 			}
26215578689aSDaniel Lezcano 			dev = net->loopback_dev;
26221da177e4SLinus Torvalds 			dev_hold(dev);
26231da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
26241da177e4SLinus Torvalds 			if (!idev) {
26251da177e4SLinus Torvalds 				err = -ENODEV;
26261da177e4SLinus Torvalds 				goto out;
26271da177e4SLinus Torvalds 			}
26281da177e4SLinus Torvalds 		}
26291da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
2630ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
2631ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
2632ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
2633ede2059dSEric W. Biederman 			rt->dst.output = dst_discard_out;
26347150aedeSKamala R 			rt->dst.input = dst_discard;
2635ef2c7d7bSNicolas Dichtel 			break;
2636ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
2637ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
26387150aedeSKamala R 			rt->dst.output = ip6_pkt_prohibit_out;
26397150aedeSKamala R 			rt->dst.input = ip6_pkt_prohibit;
2640ef2c7d7bSNicolas Dichtel 			break;
2641b4949ab2SNicolas Dichtel 		case RTN_THROW:
26420315e382SNikola Forró 		case RTN_UNREACHABLE:
2643ef2c7d7bSNicolas Dichtel 		default:
26447150aedeSKamala R 			rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
26450315e382SNikola Forró 					: (cfg->fc_type == RTN_UNREACHABLE)
26460315e382SNikola Forró 					? -EHOSTUNREACH : -ENETUNREACH;
26477150aedeSKamala R 			rt->dst.output = ip6_pkt_discard_out;
26487150aedeSKamala R 			rt->dst.input = ip6_pkt_discard;
2649ef2c7d7bSNicolas Dichtel 			break;
2650ef2c7d7bSNicolas Dichtel 		}
26511da177e4SLinus Torvalds 		goto install_route;
26521da177e4SLinus Torvalds 	}
26531da177e4SLinus Torvalds 
265486872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
2655b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
26561da177e4SLinus Torvalds 		int gwa_type;
26571da177e4SLinus Torvalds 
265886872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
2659330567b7SFlorian Westphal 		gwa_type = ipv6_addr_type(gw_addr);
266048ed7b26SFlorian Westphal 
266148ed7b26SFlorian Westphal 		/* if gw_addr is local we will fail to detect this in case
266248ed7b26SFlorian Westphal 		 * address is still TENTATIVE (DAD in progress). rt6_lookup()
266348ed7b26SFlorian Westphal 		 * will return already-added prefix route via interface that
266448ed7b26SFlorian Westphal 		 * prefix route was assigned to, which might be non-loopback.
266548ed7b26SFlorian Westphal 		 */
266648ed7b26SFlorian Westphal 		err = -EINVAL;
2667330567b7SFlorian Westphal 		if (ipv6_chk_addr_and_flags(net, gw_addr,
2668330567b7SFlorian Westphal 					    gwa_type & IPV6_ADDR_LINKLOCAL ?
2669d5d531cbSDavid Ahern 					    dev : NULL, 0, 0)) {
2670d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
267148ed7b26SFlorian Westphal 			goto out;
2672d5d531cbSDavid Ahern 		}
26734e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
26741da177e4SLinus Torvalds 
26751da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
26768c14586fSDavid Ahern 			struct rt6_info *grt = NULL;
26771da177e4SLinus Torvalds 
26781da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
26791da177e4SLinus Torvalds 			   addresses as nexthop address.
26801da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
26811da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
26821da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
26831da177e4SLinus Torvalds 			   some exceptions. --ANK
268496d5822cSErik Nordmark 			   We allow IPv4-mapped nexthops to support RFC4798-type
268596d5822cSErik Nordmark 			   addressing
26861da177e4SLinus Torvalds 			 */
268796d5822cSErik Nordmark 			if (!(gwa_type & (IPV6_ADDR_UNICAST |
2688d5d531cbSDavid Ahern 					  IPV6_ADDR_MAPPED))) {
2689d5d531cbSDavid Ahern 				NL_SET_ERR_MSG(extack,
2690d5d531cbSDavid Ahern 					       "Invalid gateway address");
26911da177e4SLinus Torvalds 				goto out;
2692d5d531cbSDavid Ahern 			}
26931da177e4SLinus Torvalds 
2694a435a07fSVincent Bernat 			if (cfg->fc_table) {
26958c14586fSDavid Ahern 				grt = ip6_nh_lookup_table(net, cfg, gw_addr);
26968c14586fSDavid Ahern 
2697a435a07fSVincent Bernat 				if (grt) {
2698a435a07fSVincent Bernat 					if (grt->rt6i_flags & RTF_GATEWAY ||
2699a435a07fSVincent Bernat 					    (dev && dev != grt->dst.dev)) {
2700a435a07fSVincent Bernat 						ip6_rt_put(grt);
2701a435a07fSVincent Bernat 						grt = NULL;
2702a435a07fSVincent Bernat 					}
2703a435a07fSVincent Bernat 				}
2704a435a07fSVincent Bernat 			}
2705a435a07fSVincent Bernat 
27068c14586fSDavid Ahern 			if (!grt)
27078c14586fSDavid Ahern 				grt = rt6_lookup(net, gw_addr, NULL,
27088c14586fSDavid Ahern 						 cfg->fc_ifindex, 1);
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
271138308473SDavid S. Miller 			if (!grt)
27121da177e4SLinus Torvalds 				goto out;
27131da177e4SLinus Torvalds 			if (dev) {
2714d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
271594e187c0SAmerigo Wang 					ip6_rt_put(grt);
27161da177e4SLinus Torvalds 					goto out;
27171da177e4SLinus Torvalds 				}
27181da177e4SLinus Torvalds 			} else {
2719d1918542SDavid S. Miller 				dev = grt->dst.dev;
27201da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
27211da177e4SLinus Torvalds 				dev_hold(dev);
27221da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
27231da177e4SLinus Torvalds 			}
27241da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
27251da177e4SLinus Torvalds 				err = 0;
272694e187c0SAmerigo Wang 			ip6_rt_put(grt);
27271da177e4SLinus Torvalds 
27281da177e4SLinus Torvalds 			if (err)
27291da177e4SLinus Torvalds 				goto out;
27301da177e4SLinus Torvalds 		}
27311da177e4SLinus Torvalds 		err = -EINVAL;
2732d5d531cbSDavid Ahern 		if (!dev) {
2733d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Egress device not specified");
27341da177e4SLinus Torvalds 			goto out;
2735d5d531cbSDavid Ahern 		} else if (dev->flags & IFF_LOOPBACK) {
2736d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack,
2737d5d531cbSDavid Ahern 				       "Egress device can not be loopback device for this route");
2738d5d531cbSDavid Ahern 			goto out;
2739d5d531cbSDavid Ahern 		}
27401da177e4SLinus Torvalds 	}
27411da177e4SLinus Torvalds 
27421da177e4SLinus Torvalds 	err = -ENODEV;
274338308473SDavid S. Miller 	if (!dev)
27441da177e4SLinus Torvalds 		goto out;
27451da177e4SLinus Torvalds 
2746c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
2747c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
2748d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
2749c3968a85SDaniel Walter 			err = -EINVAL;
2750c3968a85SDaniel Walter 			goto out;
2751c3968a85SDaniel Walter 		}
27524e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
2753c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
2754c3968a85SDaniel Walter 	} else
2755c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2756c3968a85SDaniel Walter 
275786872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
27581da177e4SLinus Torvalds 
27591da177e4SLinus Torvalds install_route:
27605609b80aSIdo Schimmel 	if (!(rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
27615609b80aSIdo Schimmel 	    !netif_carrier_ok(dev))
27625609b80aSIdo Schimmel 		rt->rt6i_nh_flags |= RTNH_F_LINKDOWN;
2763d8d1f30bSChangli Gao 	rt->dst.dev = dev;
27641da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2765c71099acSThomas Graf 	rt->rt6i_table = table;
276663152fc0SDaniel Lezcano 
2767c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
276863152fc0SDaniel Lezcano 
27698c5b83f0SRoopa Prabhu 	return rt;
27701da177e4SLinus Torvalds out:
27711da177e4SLinus Torvalds 	if (dev)
27721da177e4SLinus Torvalds 		dev_put(dev);
27731da177e4SLinus Torvalds 	if (idev)
27741da177e4SLinus Torvalds 		in6_dev_put(idev);
2775587fea74SWei Wang 	if (rt)
2776587fea74SWei Wang 		dst_release_immediate(&rt->dst);
27776b9ea5a6SRoopa Prabhu 
27788c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
27796b9ea5a6SRoopa Prabhu }
27806b9ea5a6SRoopa Prabhu 
2781333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg,
2782333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
27836b9ea5a6SRoopa Prabhu {
27846b9ea5a6SRoopa Prabhu 	struct mx6_config mxc = { .mx = NULL, };
27858c5b83f0SRoopa Prabhu 	struct rt6_info *rt;
27866b9ea5a6SRoopa Prabhu 	int err;
27876b9ea5a6SRoopa Prabhu 
2788333c4301SDavid Ahern 	rt = ip6_route_info_create(cfg, extack);
27898c5b83f0SRoopa Prabhu 	if (IS_ERR(rt)) {
27908c5b83f0SRoopa Prabhu 		err = PTR_ERR(rt);
27918c5b83f0SRoopa Prabhu 		rt = NULL;
27926b9ea5a6SRoopa Prabhu 		goto out;
27938c5b83f0SRoopa Prabhu 	}
27946b9ea5a6SRoopa Prabhu 
27956b9ea5a6SRoopa Prabhu 	err = ip6_convert_metrics(&mxc, cfg);
27966b9ea5a6SRoopa Prabhu 	if (err)
27976b9ea5a6SRoopa Prabhu 		goto out;
27986b9ea5a6SRoopa Prabhu 
2799333c4301SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack);
28006b9ea5a6SRoopa Prabhu 
28016b9ea5a6SRoopa Prabhu 	kfree(mxc.mx);
28026b9ea5a6SRoopa Prabhu 
28036b9ea5a6SRoopa Prabhu 	return err;
28046b9ea5a6SRoopa Prabhu out:
2805587fea74SWei Wang 	if (rt)
2806587fea74SWei Wang 		dst_release_immediate(&rt->dst);
28076b9ea5a6SRoopa Prabhu 
28081da177e4SLinus Torvalds 	return err;
28091da177e4SLinus Torvalds }
28101da177e4SLinus Torvalds 
281186872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
28121da177e4SLinus Torvalds {
28131da177e4SLinus Torvalds 	int err;
2814c71099acSThomas Graf 	struct fib6_table *table;
2815d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
28161da177e4SLinus Torvalds 
2817a4c2fd7fSWei Wang 	if (rt == net->ipv6.ip6_null_entry) {
28186825a26cSGao feng 		err = -ENOENT;
28196825a26cSGao feng 		goto out;
28206825a26cSGao feng 	}
28216c813a72SPatrick McHardy 
2822c71099acSThomas Graf 	table = rt->rt6i_table;
282366f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
282486872cb5SThomas Graf 	err = fib6_del(rt, info);
282566f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
28261da177e4SLinus Torvalds 
28276825a26cSGao feng out:
282894e187c0SAmerigo Wang 	ip6_rt_put(rt);
28291da177e4SLinus Torvalds 	return err;
28301da177e4SLinus Torvalds }
28311da177e4SLinus Torvalds 
2832e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
2833e0a1ad73SThomas Graf {
28344d1169c1SDenis V. Lunev 	struct nl_info info = {
2835d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
28364d1169c1SDenis V. Lunev 	};
2837528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
2838e0a1ad73SThomas Graf }
2839e0a1ad73SThomas Graf 
28400ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg)
28410ae81335SDavid Ahern {
28420ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
2843e3330039SWANG Cong 	struct net *net = info->nl_net;
284416a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
28450ae81335SDavid Ahern 	struct fib6_table *table;
2846e3330039SWANG Cong 	int err = -ENOENT;
28470ae81335SDavid Ahern 
2848e3330039SWANG Cong 	if (rt == net->ipv6.ip6_null_entry)
2849e3330039SWANG Cong 		goto out_put;
28500ae81335SDavid Ahern 	table = rt->rt6i_table;
285166f5d6ceSWei Wang 	spin_lock_bh(&table->tb6_lock);
28520ae81335SDavid Ahern 
28530ae81335SDavid Ahern 	if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) {
28540ae81335SDavid Ahern 		struct rt6_info *sibling, *next_sibling;
28550ae81335SDavid Ahern 
285616a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
285716a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
285816a16cd3SDavid Ahern 		if (skb) {
285916a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
286016a16cd3SDavid Ahern 
2861e3330039SWANG Cong 			if (rt6_fill_node(net, skb, rt,
286216a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
286316a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
286416a16cd3SDavid Ahern 				kfree_skb(skb);
286516a16cd3SDavid Ahern 				skb = NULL;
286616a16cd3SDavid Ahern 			} else
286716a16cd3SDavid Ahern 				info->skip_notify = 1;
286816a16cd3SDavid Ahern 		}
286916a16cd3SDavid Ahern 
28700ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
28710ae81335SDavid Ahern 					 &rt->rt6i_siblings,
28720ae81335SDavid Ahern 					 rt6i_siblings) {
28730ae81335SDavid Ahern 			err = fib6_del(sibling, info);
28740ae81335SDavid Ahern 			if (err)
2875e3330039SWANG Cong 				goto out_unlock;
28760ae81335SDavid Ahern 		}
28770ae81335SDavid Ahern 	}
28780ae81335SDavid Ahern 
28790ae81335SDavid Ahern 	err = fib6_del(rt, info);
2880e3330039SWANG Cong out_unlock:
288166f5d6ceSWei Wang 	spin_unlock_bh(&table->tb6_lock);
2882e3330039SWANG Cong out_put:
28830ae81335SDavid Ahern 	ip6_rt_put(rt);
288416a16cd3SDavid Ahern 
288516a16cd3SDavid Ahern 	if (skb) {
2886e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
288716a16cd3SDavid Ahern 			    info->nlh, gfp_any());
288816a16cd3SDavid Ahern 	}
28890ae81335SDavid Ahern 	return err;
28900ae81335SDavid Ahern }
28910ae81335SDavid Ahern 
2892333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
2893333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
28941da177e4SLinus Torvalds {
28952b760fcfSWei Wang 	struct rt6_info *rt, *rt_cache;
2896c71099acSThomas Graf 	struct fib6_table *table;
28971da177e4SLinus Torvalds 	struct fib6_node *fn;
28981da177e4SLinus Torvalds 	int err = -ESRCH;
28991da177e4SLinus Torvalds 
29005578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
2901d5d531cbSDavid Ahern 	if (!table) {
2902d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
2903c71099acSThomas Graf 		return err;
2904d5d531cbSDavid Ahern 	}
29051da177e4SLinus Torvalds 
290666f5d6ceSWei Wang 	rcu_read_lock();
2907c71099acSThomas Graf 
2908c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
290986872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
291038fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
29112b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
29121da177e4SLinus Torvalds 
29131da177e4SLinus Torvalds 	if (fn) {
291466f5d6ceSWei Wang 		for_each_fib6_node_rt_rcu(fn) {
29152b760fcfSWei Wang 			if (cfg->fc_flags & RTF_CACHE) {
29162b760fcfSWei Wang 				rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
29172b760fcfSWei Wang 							      &cfg->fc_src);
29182b760fcfSWei Wang 				if (!rt_cache)
29191f56a01fSMartin KaFai Lau 					continue;
29202b760fcfSWei Wang 				rt = rt_cache;
29212b760fcfSWei Wang 			}
292286872cb5SThomas Graf 			if (cfg->fc_ifindex &&
2923d1918542SDavid S. Miller 			    (!rt->dst.dev ||
2924d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
29251da177e4SLinus Torvalds 				continue;
292686872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
292786872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
29281da177e4SLinus Torvalds 				continue;
292986872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
29301da177e4SLinus Torvalds 				continue;
2931c2ed1880SMantas M 			if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol)
2932c2ed1880SMantas M 				continue;
2933d3843fe5SWei Wang 			if (!dst_hold_safe(&rt->dst))
2934d3843fe5SWei Wang 				break;
293566f5d6ceSWei Wang 			rcu_read_unlock();
29361da177e4SLinus Torvalds 
29370ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
29380ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
293986872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
29400ae81335SDavid Ahern 
29410ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
29421da177e4SLinus Torvalds 		}
29431da177e4SLinus Torvalds 	}
294466f5d6ceSWei Wang 	rcu_read_unlock();
29451da177e4SLinus Torvalds 
29461da177e4SLinus Torvalds 	return err;
29471da177e4SLinus Torvalds }
29481da177e4SLinus Torvalds 
29496700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
2950a6279458SYOSHIFUJI Hideaki {
2951a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
2952e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
2953e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
2954e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
2955e8599ff4SDavid S. Miller 	struct neighbour *neigh;
295671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
29576e157b6aSDavid S. Miller 	int optlen, on_link;
29586e157b6aSDavid S. Miller 	u8 *lladdr;
2959e8599ff4SDavid S. Miller 
296029a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
296171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
2962e8599ff4SDavid S. Miller 
2963e8599ff4SDavid S. Miller 	if (optlen < 0) {
29646e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
2965e8599ff4SDavid S. Miller 		return;
2966e8599ff4SDavid S. Miller 	}
2967e8599ff4SDavid S. Miller 
296871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
2969e8599ff4SDavid S. Miller 
297071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
29716e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
2972e8599ff4SDavid S. Miller 		return;
2973e8599ff4SDavid S. Miller 	}
2974e8599ff4SDavid S. Miller 
29756e157b6aSDavid S. Miller 	on_link = 0;
297671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
2977e8599ff4SDavid S. Miller 		on_link = 1;
297871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
2979e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
29806e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
2981e8599ff4SDavid S. Miller 		return;
2982e8599ff4SDavid S. Miller 	}
2983e8599ff4SDavid S. Miller 
2984e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
2985e8599ff4SDavid S. Miller 	if (!in6_dev)
2986e8599ff4SDavid S. Miller 		return;
2987e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
2988e8599ff4SDavid S. Miller 		return;
2989e8599ff4SDavid S. Miller 
2990e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
2991e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
2992e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
2993e8599ff4SDavid S. Miller 	 */
2994e8599ff4SDavid S. Miller 
2995f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
2996e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
2997e8599ff4SDavid S. Miller 		return;
2998e8599ff4SDavid S. Miller 	}
29996e157b6aSDavid S. Miller 
30006e157b6aSDavid S. Miller 	lladdr = NULL;
3001e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
3002e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
3003e8599ff4SDavid S. Miller 					     skb->dev);
3004e8599ff4SDavid S. Miller 		if (!lladdr) {
3005e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
3006e8599ff4SDavid S. Miller 			return;
3007e8599ff4SDavid S. Miller 		}
3008e8599ff4SDavid S. Miller 	}
3009e8599ff4SDavid S. Miller 
30106e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
3011ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
30126e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
30136e157b6aSDavid S. Miller 		return;
30146e157b6aSDavid S. Miller 	}
30156e157b6aSDavid S. Miller 
30166e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
30176e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
30186e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
30196e157b6aSDavid S. Miller 	 */
30200dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
30216e157b6aSDavid S. Miller 
302271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
3023e8599ff4SDavid S. Miller 	if (!neigh)
3024e8599ff4SDavid S. Miller 		return;
3025e8599ff4SDavid S. Miller 
30261da177e4SLinus Torvalds 	/*
30271da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
30281da177e4SLinus Torvalds 	 */
30291da177e4SLinus Torvalds 
3030f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
30311da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
30321da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
30331da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
3034f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
3035f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
30361da177e4SLinus Torvalds 
303783a09abdSMartin KaFai Lau 	nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL);
303838308473SDavid S. Miller 	if (!nrt)
30391da177e4SLinus Torvalds 		goto out;
30401da177e4SLinus Torvalds 
30411da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
30421da177e4SLinus Torvalds 	if (on_link)
30431da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
30441da177e4SLinus Torvalds 
3045b91d5329SXin Long 	nrt->rt6i_protocol = RTPROT_REDIRECT;
30464e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
30471da177e4SLinus Torvalds 
30482b760fcfSWei Wang 	/* No need to remove rt from the exception table if rt is
30492b760fcfSWei Wang 	 * a cached route because rt6_insert_exception() will
30502b760fcfSWei Wang 	 * takes care of it
30512b760fcfSWei Wang 	 */
30522b760fcfSWei Wang 	if (rt6_insert_exception(nrt, rt)) {
30532b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
30542b760fcfSWei Wang 		goto out;
30552b760fcfSWei Wang 	}
30561da177e4SLinus Torvalds 
3057d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
3058d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
305971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
306060592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
30618d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
30628d71740cSTom Tucker 
30631da177e4SLinus Torvalds out:
3064e8599ff4SDavid S. Miller 	neigh_release(neigh);
30656e157b6aSDavid S. Miller }
30666e157b6aSDavid S. Miller 
30671da177e4SLinus Torvalds /*
30681da177e4SLinus Torvalds  *	Misc support functions
30691da177e4SLinus Torvalds  */
30701da177e4SLinus Torvalds 
30714b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
30724b32b5adSMartin KaFai Lau {
30733a2232e9SDavid Miller 	BUG_ON(from->from);
30744b32b5adSMartin KaFai Lau 
30754b32b5adSMartin KaFai Lau 	rt->rt6i_flags &= ~RTF_EXPIRES;
30764b32b5adSMartin KaFai Lau 	dst_hold(&from->dst);
30773a2232e9SDavid Miller 	rt->from = from;
30784b32b5adSMartin KaFai Lau 	dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
30794b32b5adSMartin KaFai Lau }
30804b32b5adSMartin KaFai Lau 
308183a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort)
30821da177e4SLinus Torvalds {
3083d8d1f30bSChangli Gao 	rt->dst.input = ort->dst.input;
3084d8d1f30bSChangli Gao 	rt->dst.output = ort->dst.output;
308583a09abdSMartin KaFai Lau 	rt->rt6i_dst = ort->rt6i_dst;
3086d8d1f30bSChangli Gao 	rt->dst.error = ort->dst.error;
30871da177e4SLinus Torvalds 	rt->rt6i_idev = ort->rt6i_idev;
30881da177e4SLinus Torvalds 	if (rt->rt6i_idev)
30891da177e4SLinus Torvalds 		in6_dev_hold(rt->rt6i_idev);
3090d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
30914e3fd7a0SAlexey Dobriyan 	rt->rt6i_gateway = ort->rt6i_gateway;
30921716a961SGao feng 	rt->rt6i_flags = ort->rt6i_flags;
30931716a961SGao feng 	rt6_set_from(rt, ort);
309483a09abdSMartin KaFai Lau 	rt->rt6i_metric = ort->rt6i_metric;
30951da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
309683a09abdSMartin KaFai Lau 	rt->rt6i_src = ort->rt6i_src;
30971da177e4SLinus Torvalds #endif
309883a09abdSMartin KaFai Lau 	rt->rt6i_prefsrc = ort->rt6i_prefsrc;
3099c71099acSThomas Graf 	rt->rt6i_table = ort->rt6i_table;
310061adedf3SJiri Benc 	rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate);
31011da177e4SLinus Torvalds }
31021da177e4SLinus Torvalds 
310370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
3104efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
3105b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3106830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3107830218c1SDavid Ahern 					   struct net_device *dev)
310870ceb4f5SYOSHIFUJI Hideaki {
3109830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3110830218c1SDavid Ahern 	int ifindex = dev->ifindex;
311170ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
311270ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
3113c71099acSThomas Graf 	struct fib6_table *table;
311470ceb4f5SYOSHIFUJI Hideaki 
3115830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
311638308473SDavid S. Miller 	if (!table)
3117c71099acSThomas Graf 		return NULL;
3118c71099acSThomas Graf 
311966f5d6ceSWei Wang 	rcu_read_lock();
312038fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
312170ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
312270ceb4f5SYOSHIFUJI Hideaki 		goto out;
312370ceb4f5SYOSHIFUJI Hideaki 
312466f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(fn) {
3125d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
312670ceb4f5SYOSHIFUJI Hideaki 			continue;
312770ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
312870ceb4f5SYOSHIFUJI Hideaki 			continue;
312970ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
313070ceb4f5SYOSHIFUJI Hideaki 			continue;
3131d3843fe5SWei Wang 		ip6_hold_safe(NULL, &rt, false);
313270ceb4f5SYOSHIFUJI Hideaki 		break;
313370ceb4f5SYOSHIFUJI Hideaki 	}
313470ceb4f5SYOSHIFUJI Hideaki out:
313566f5d6ceSWei Wang 	rcu_read_unlock();
313670ceb4f5SYOSHIFUJI Hideaki 	return rt;
313770ceb4f5SYOSHIFUJI Hideaki }
313870ceb4f5SYOSHIFUJI Hideaki 
3139efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
3140b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3141830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3142830218c1SDavid Ahern 					   struct net_device *dev,
314395c96174SEric Dumazet 					   unsigned int pref)
314470ceb4f5SYOSHIFUJI Hideaki {
314586872cb5SThomas Graf 	struct fib6_config cfg = {
3146238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
3147830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
314886872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
314986872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
315086872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
3151b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
315215e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
3153efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3154efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
315586872cb5SThomas Graf 	};
315670ceb4f5SYOSHIFUJI Hideaki 
3157830218c1SDavid Ahern 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
31584e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
31594e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
316086872cb5SThomas Graf 
3161e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
3162e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
316386872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
316470ceb4f5SYOSHIFUJI Hideaki 
3165333c4301SDavid Ahern 	ip6_route_add(&cfg, NULL);
316670ceb4f5SYOSHIFUJI Hideaki 
3167830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
316870ceb4f5SYOSHIFUJI Hideaki }
316970ceb4f5SYOSHIFUJI Hideaki #endif
317070ceb4f5SYOSHIFUJI Hideaki 
3171b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
31721da177e4SLinus Torvalds {
3173830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
31741da177e4SLinus Torvalds 	struct rt6_info *rt;
3175c71099acSThomas Graf 	struct fib6_table *table;
31761da177e4SLinus Torvalds 
3177830218c1SDavid Ahern 	table = fib6_get_table(dev_net(dev), tb_id);
317838308473SDavid S. Miller 	if (!table)
3179c71099acSThomas Graf 		return NULL;
31801da177e4SLinus Torvalds 
318166f5d6ceSWei Wang 	rcu_read_lock();
318266f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
3183d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
3184045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
31851da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
31861da177e4SLinus Torvalds 			break;
31871da177e4SLinus Torvalds 	}
31881da177e4SLinus Torvalds 	if (rt)
3189d3843fe5SWei Wang 		ip6_hold_safe(NULL, &rt, false);
319066f5d6ceSWei Wang 	rcu_read_unlock();
31911da177e4SLinus Torvalds 	return rt;
31921da177e4SLinus Torvalds }
31931da177e4SLinus Torvalds 
3194b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
3195ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
3196ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
31971da177e4SLinus Torvalds {
319886872cb5SThomas Graf 	struct fib6_config cfg = {
3199ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
3200238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
320186872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
320286872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
320386872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
3204b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
320515e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
32065578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3207c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
320886872cb5SThomas Graf 	};
32091da177e4SLinus Torvalds 
32104e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
32111da177e4SLinus Torvalds 
3212333c4301SDavid Ahern 	if (!ip6_route_add(&cfg, NULL)) {
3213830218c1SDavid Ahern 		struct fib6_table *table;
3214830218c1SDavid Ahern 
3215830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
3216830218c1SDavid Ahern 		if (table)
3217830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3218830218c1SDavid Ahern 	}
32191da177e4SLinus Torvalds 
32201da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
32211da177e4SLinus Torvalds }
32221da177e4SLinus Torvalds 
3223830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table)
32241da177e4SLinus Torvalds {
32251da177e4SLinus Torvalds 	struct rt6_info *rt;
32261da177e4SLinus Torvalds 
32271da177e4SLinus Torvalds restart:
322866f5d6ceSWei Wang 	rcu_read_lock();
322966f5d6ceSWei Wang 	for_each_fib6_node_rt_rcu(&table->tb6_root) {
32303e8b0ac3SLorenzo Colitti 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
32313e8b0ac3SLorenzo Colitti 		    (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
3232d3843fe5SWei Wang 			if (dst_hold_safe(&rt->dst)) {
323366f5d6ceSWei Wang 				rcu_read_unlock();
3234e0a1ad73SThomas Graf 				ip6_del_rt(rt);
3235d3843fe5SWei Wang 			} else {
323666f5d6ceSWei Wang 				rcu_read_unlock();
3237d3843fe5SWei Wang 			}
32381da177e4SLinus Torvalds 			goto restart;
32391da177e4SLinus Torvalds 		}
32401da177e4SLinus Torvalds 	}
324166f5d6ceSWei Wang 	rcu_read_unlock();
3242830218c1SDavid Ahern 
3243830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3244830218c1SDavid Ahern }
3245830218c1SDavid Ahern 
3246830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
3247830218c1SDavid Ahern {
3248830218c1SDavid Ahern 	struct fib6_table *table;
3249830218c1SDavid Ahern 	struct hlist_head *head;
3250830218c1SDavid Ahern 	unsigned int h;
3251830218c1SDavid Ahern 
3252830218c1SDavid Ahern 	rcu_read_lock();
3253830218c1SDavid Ahern 
3254830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3255830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
3256830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3257830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3258830218c1SDavid Ahern 				__rt6_purge_dflt_routers(table);
3259830218c1SDavid Ahern 		}
3260830218c1SDavid Ahern 	}
3261830218c1SDavid Ahern 
3262830218c1SDavid Ahern 	rcu_read_unlock();
32631da177e4SLinus Torvalds }
32641da177e4SLinus Torvalds 
32655578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
32665578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
326786872cb5SThomas Graf 				 struct fib6_config *cfg)
326886872cb5SThomas Graf {
326986872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
327086872cb5SThomas Graf 
3271ca254490SDavid Ahern 	cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
3272ca254490SDavid Ahern 			 : RT6_TABLE_MAIN;
327386872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
327486872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
327586872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
327686872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
327786872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
327886872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
327986872cb5SThomas Graf 
32805578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
3281f1243c2dSBenjamin Thery 
32824e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
32834e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
32844e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
328586872cb5SThomas Graf }
328686872cb5SThomas Graf 
32875578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
32881da177e4SLinus Torvalds {
328986872cb5SThomas Graf 	struct fib6_config cfg;
32901da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
32911da177e4SLinus Torvalds 	int err;
32921da177e4SLinus Torvalds 
32931da177e4SLinus Torvalds 	switch (cmd) {
32941da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
32951da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
3296af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
32971da177e4SLinus Torvalds 			return -EPERM;
32981da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
32991da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
33001da177e4SLinus Torvalds 		if (err)
33011da177e4SLinus Torvalds 			return -EFAULT;
33021da177e4SLinus Torvalds 
33035578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
330486872cb5SThomas Graf 
33051da177e4SLinus Torvalds 		rtnl_lock();
33061da177e4SLinus Torvalds 		switch (cmd) {
33071da177e4SLinus Torvalds 		case SIOCADDRT:
3308333c4301SDavid Ahern 			err = ip6_route_add(&cfg, NULL);
33091da177e4SLinus Torvalds 			break;
33101da177e4SLinus Torvalds 		case SIOCDELRT:
3311333c4301SDavid Ahern 			err = ip6_route_del(&cfg, NULL);
33121da177e4SLinus Torvalds 			break;
33131da177e4SLinus Torvalds 		default:
33141da177e4SLinus Torvalds 			err = -EINVAL;
33151da177e4SLinus Torvalds 		}
33161da177e4SLinus Torvalds 		rtnl_unlock();
33171da177e4SLinus Torvalds 
33181da177e4SLinus Torvalds 		return err;
33193ff50b79SStephen Hemminger 	}
33201da177e4SLinus Torvalds 
33211da177e4SLinus Torvalds 	return -EINVAL;
33221da177e4SLinus Torvalds }
33231da177e4SLinus Torvalds 
33241da177e4SLinus Torvalds /*
33251da177e4SLinus Torvalds  *	Drop the packet on the floor
33261da177e4SLinus Torvalds  */
33271da177e4SLinus Torvalds 
3328d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
33291da177e4SLinus Torvalds {
3330612f09e8SYOSHIFUJI Hideaki 	int type;
3331adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
3332612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
3333612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
33340660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
333545bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
33363bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
33373bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
3338612f09e8SYOSHIFUJI Hideaki 			break;
3339612f09e8SYOSHIFUJI Hideaki 		}
3340612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
3341612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
33423bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
33433bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
3344612f09e8SYOSHIFUJI Hideaki 		break;
3345612f09e8SYOSHIFUJI Hideaki 	}
33463ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
33471da177e4SLinus Torvalds 	kfree_skb(skb);
33481da177e4SLinus Torvalds 	return 0;
33491da177e4SLinus Torvalds }
33501da177e4SLinus Torvalds 
33519ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
33529ce8ade0SThomas Graf {
3353612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
33549ce8ade0SThomas Graf }
33559ce8ade0SThomas Graf 
3356ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
33571da177e4SLinus Torvalds {
3358adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3359612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
33601da177e4SLinus Torvalds }
33611da177e4SLinus Torvalds 
33629ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
33639ce8ade0SThomas Graf {
3364612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
33659ce8ade0SThomas Graf }
33669ce8ade0SThomas Graf 
3367ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
33689ce8ade0SThomas Graf {
3369adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3370612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
33719ce8ade0SThomas Graf }
33729ce8ade0SThomas Graf 
33731da177e4SLinus Torvalds /*
33741da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
33751da177e4SLinus Torvalds  */
33761da177e4SLinus Torvalds 
33771da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
33781da177e4SLinus Torvalds 				    const struct in6_addr *addr,
33798f031519SDavid S. Miller 				    bool anycast)
33801da177e4SLinus Torvalds {
3381ca254490SDavid Ahern 	u32 tb_id;
3382c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
33834832c30dSDavid Ahern 	struct net_device *dev = idev->dev;
33845f02ce24SDavid Ahern 	struct rt6_info *rt;
33855f02ce24SDavid Ahern 
33865f02ce24SDavid Ahern 	rt = ip6_dst_alloc(net, dev, DST_NOCOUNT);
3387a3300ef4SHannes Frederic Sowa 	if (!rt)
33881da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
33891da177e4SLinus Torvalds 
33901da177e4SLinus Torvalds 	in6_dev_hold(idev);
33911da177e4SLinus Torvalds 
339211d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
3393d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
3394d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
33951da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
33961da177e4SLinus Torvalds 
339794b5e0f9SDavid Ahern 	rt->rt6i_protocol = RTPROT_KERNEL;
33981da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
339958c4fb86SYOSHIFUJI Hideaki 	if (anycast)
340058c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
340158c4fb86SYOSHIFUJI Hideaki 	else
34021da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
34031da177e4SLinus Torvalds 
3404550bab42SJulian Anastasov 	rt->rt6i_gateway  = *addr;
34054e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
34061da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
3407ca254490SDavid Ahern 	tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
3408ca254490SDavid Ahern 	rt->rt6i_table = fib6_get_table(net, tb_id);
34091da177e4SLinus Torvalds 
34101da177e4SLinus Torvalds 	return rt;
34111da177e4SLinus Torvalds }
34121da177e4SLinus Torvalds 
3413c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
3414c3968a85SDaniel Walter struct arg_dev_net_ip {
3415c3968a85SDaniel Walter 	struct net_device *dev;
3416c3968a85SDaniel Walter 	struct net *net;
3417c3968a85SDaniel Walter 	struct in6_addr *addr;
3418c3968a85SDaniel Walter };
3419c3968a85SDaniel Walter 
3420c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
3421c3968a85SDaniel Walter {
3422c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3423c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3424c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3425c3968a85SDaniel Walter 
3426d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
3427c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
3428c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
342960006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
3430c3968a85SDaniel Walter 		/* remove prefsrc entry */
3431c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
343260006a48SWei Wang 		/* need to update cache as well */
343360006a48SWei Wang 		rt6_exceptions_remove_prefsrc(rt);
343460006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
3435c3968a85SDaniel Walter 	}
3436c3968a85SDaniel Walter 	return 0;
3437c3968a85SDaniel Walter }
3438c3968a85SDaniel Walter 
3439c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3440c3968a85SDaniel Walter {
3441c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
3442c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
3443c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
3444c3968a85SDaniel Walter 		.net = net,
3445c3968a85SDaniel Walter 		.addr = &ifp->addr,
3446c3968a85SDaniel Walter 	};
34470c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
3448c3968a85SDaniel Walter }
3449c3968a85SDaniel Walter 
3450be7a010dSDuan Jiong #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
3451be7a010dSDuan Jiong 
3452be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
3453be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
3454be7a010dSDuan Jiong {
3455be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
3456be7a010dSDuan Jiong 
34572b760fcfSWei Wang 	if (((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
3458be7a010dSDuan Jiong 	    ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
3459be7a010dSDuan Jiong 		return -1;
3460be7a010dSDuan Jiong 	}
3461b16cb459SWei Wang 
3462b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
3463b16cb459SWei Wang 	 * This is needed because cached route may have a different
3464b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
3465b16cb459SWei Wang 	 */
3466b16cb459SWei Wang 	rt6_exceptions_clean_tohost(rt, gateway);
3467b16cb459SWei Wang 
3468be7a010dSDuan Jiong 	return 0;
3469be7a010dSDuan Jiong }
3470be7a010dSDuan Jiong 
3471be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3472be7a010dSDuan Jiong {
3473be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
3474be7a010dSDuan Jiong }
3475be7a010dSDuan Jiong 
34762127d95aSIdo Schimmel struct arg_netdev_event {
34772127d95aSIdo Schimmel 	const struct net_device *dev;
34784c981e28SIdo Schimmel 	union {
34792127d95aSIdo Schimmel 		unsigned int nh_flags;
34804c981e28SIdo Schimmel 		unsigned long event;
34814c981e28SIdo Schimmel 	};
34822127d95aSIdo Schimmel };
34832127d95aSIdo Schimmel 
34842127d95aSIdo Schimmel static int fib6_ifup(struct rt6_info *rt, void *p_arg)
34852127d95aSIdo Schimmel {
34862127d95aSIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
34872127d95aSIdo Schimmel 	const struct net *net = dev_net(arg->dev);
34882127d95aSIdo Schimmel 
3489*1de178edSIdo Schimmel 	if (rt != net->ipv6.ip6_null_entry && rt->dst.dev == arg->dev) {
34902127d95aSIdo Schimmel 		rt->rt6i_nh_flags &= ~arg->nh_flags;
3491*1de178edSIdo Schimmel 		fib6_update_sernum_upto_root(dev_net(rt->dst.dev), rt);
3492*1de178edSIdo Schimmel 	}
34932127d95aSIdo Schimmel 
34942127d95aSIdo Schimmel 	return 0;
34952127d95aSIdo Schimmel }
34962127d95aSIdo Schimmel 
34972127d95aSIdo Schimmel void rt6_sync_up(struct net_device *dev, unsigned int nh_flags)
34982127d95aSIdo Schimmel {
34992127d95aSIdo Schimmel 	struct arg_netdev_event arg = {
35002127d95aSIdo Schimmel 		.dev = dev,
35012127d95aSIdo Schimmel 		.nh_flags = nh_flags,
35022127d95aSIdo Schimmel 	};
35032127d95aSIdo Schimmel 
35042127d95aSIdo Schimmel 	if (nh_flags & RTNH_F_DEAD && netif_carrier_ok(dev))
35052127d95aSIdo Schimmel 		arg.nh_flags |= RTNH_F_LINKDOWN;
35062127d95aSIdo Schimmel 
35072127d95aSIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifup, &arg);
35082127d95aSIdo Schimmel }
35092127d95aSIdo Schimmel 
3510*1de178edSIdo Schimmel static bool rt6_multipath_uses_dev(const struct rt6_info *rt,
3511*1de178edSIdo Schimmel 				   const struct net_device *dev)
3512*1de178edSIdo Schimmel {
3513*1de178edSIdo Schimmel 	struct rt6_info *iter;
3514*1de178edSIdo Schimmel 
3515*1de178edSIdo Schimmel 	if (rt->dst.dev == dev)
3516*1de178edSIdo Schimmel 		return true;
3517*1de178edSIdo Schimmel 	list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings)
3518*1de178edSIdo Schimmel 		if (iter->dst.dev == dev)
3519*1de178edSIdo Schimmel 			return true;
3520*1de178edSIdo Schimmel 
3521*1de178edSIdo Schimmel 	return false;
3522*1de178edSIdo Schimmel }
3523*1de178edSIdo Schimmel 
3524*1de178edSIdo Schimmel static void rt6_multipath_flush(struct rt6_info *rt)
3525*1de178edSIdo Schimmel {
3526*1de178edSIdo Schimmel 	struct rt6_info *iter;
3527*1de178edSIdo Schimmel 
3528*1de178edSIdo Schimmel 	rt->should_flush = 1;
3529*1de178edSIdo Schimmel 	list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings)
3530*1de178edSIdo Schimmel 		iter->should_flush = 1;
3531*1de178edSIdo Schimmel }
3532*1de178edSIdo Schimmel 
3533*1de178edSIdo Schimmel static unsigned int rt6_multipath_dead_count(const struct rt6_info *rt,
3534*1de178edSIdo Schimmel 					     const struct net_device *down_dev)
3535*1de178edSIdo Schimmel {
3536*1de178edSIdo Schimmel 	struct rt6_info *iter;
3537*1de178edSIdo Schimmel 	unsigned int dead = 0;
3538*1de178edSIdo Schimmel 
3539*1de178edSIdo Schimmel 	if (rt->dst.dev == down_dev || rt->rt6i_nh_flags & RTNH_F_DEAD)
3540*1de178edSIdo Schimmel 		dead++;
3541*1de178edSIdo Schimmel 	list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings)
3542*1de178edSIdo Schimmel 		if (iter->dst.dev == down_dev ||
3543*1de178edSIdo Schimmel 		    iter->rt6i_nh_flags & RTNH_F_DEAD)
3544*1de178edSIdo Schimmel 			dead++;
3545*1de178edSIdo Schimmel 
3546*1de178edSIdo Schimmel 	return dead;
3547*1de178edSIdo Schimmel }
3548*1de178edSIdo Schimmel 
3549*1de178edSIdo Schimmel static void rt6_multipath_nh_flags_set(struct rt6_info *rt,
3550*1de178edSIdo Schimmel 				       const struct net_device *dev,
3551*1de178edSIdo Schimmel 				       unsigned int nh_flags)
3552*1de178edSIdo Schimmel {
3553*1de178edSIdo Schimmel 	struct rt6_info *iter;
3554*1de178edSIdo Schimmel 
3555*1de178edSIdo Schimmel 	if (rt->dst.dev == dev)
3556*1de178edSIdo Schimmel 		rt->rt6i_nh_flags |= nh_flags;
3557*1de178edSIdo Schimmel 	list_for_each_entry(iter, &rt->rt6i_siblings, rt6i_siblings)
3558*1de178edSIdo Schimmel 		if (iter->dst.dev == dev)
3559*1de178edSIdo Schimmel 			iter->rt6i_nh_flags |= nh_flags;
3560*1de178edSIdo Schimmel }
3561*1de178edSIdo Schimmel 
3562a1a22c12SDavid Ahern /* called with write lock held for table with rt */
35634c981e28SIdo Schimmel static int fib6_ifdown(struct rt6_info *rt, void *p_arg)
35641da177e4SLinus Torvalds {
35654c981e28SIdo Schimmel 	const struct arg_netdev_event *arg = p_arg;
35664c981e28SIdo Schimmel 	const struct net_device *dev = arg->dev;
35674c981e28SIdo Schimmel 	const struct net *net = dev_net(dev);
35688ed67789SDaniel Lezcano 
3569*1de178edSIdo Schimmel 	if (rt == net->ipv6.ip6_null_entry)
357027c6fa73SIdo Schimmel 		return 0;
357127c6fa73SIdo Schimmel 
357227c6fa73SIdo Schimmel 	switch (arg->event) {
357327c6fa73SIdo Schimmel 	case NETDEV_UNREGISTER:
3574*1de178edSIdo Schimmel 		return rt->dst.dev == dev ? -1 : 0;
357527c6fa73SIdo Schimmel 	case NETDEV_DOWN:
3576*1de178edSIdo Schimmel 		if (rt->should_flush)
357727c6fa73SIdo Schimmel 			return -1;
3578*1de178edSIdo Schimmel 		if (!rt->rt6i_nsiblings)
3579*1de178edSIdo Schimmel 			return rt->dst.dev == dev ? -1 : 0;
3580*1de178edSIdo Schimmel 		if (rt6_multipath_uses_dev(rt, dev)) {
3581*1de178edSIdo Schimmel 			unsigned int count;
3582*1de178edSIdo Schimmel 
3583*1de178edSIdo Schimmel 			count = rt6_multipath_dead_count(rt, dev);
3584*1de178edSIdo Schimmel 			if (rt->rt6i_nsiblings + 1 == count) {
3585*1de178edSIdo Schimmel 				rt6_multipath_flush(rt);
3586*1de178edSIdo Schimmel 				return -1;
3587*1de178edSIdo Schimmel 			}
3588*1de178edSIdo Schimmel 			rt6_multipath_nh_flags_set(rt, dev, RTNH_F_DEAD |
3589*1de178edSIdo Schimmel 						   RTNH_F_LINKDOWN);
3590*1de178edSIdo Schimmel 			fib6_update_sernum(rt);
3591*1de178edSIdo Schimmel 		}
3592*1de178edSIdo Schimmel 		return -2;
359327c6fa73SIdo Schimmel 	case NETDEV_CHANGE:
3594*1de178edSIdo Schimmel 		if (rt->dst.dev != dev ||
3595*1de178edSIdo Schimmel 		    rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST))
359627c6fa73SIdo Schimmel 			break;
359727c6fa73SIdo Schimmel 		rt->rt6i_nh_flags |= RTNH_F_LINKDOWN;
359827c6fa73SIdo Schimmel 		break;
35992b241361SIdo Schimmel 	}
3600c159d30cSDavid S. Miller 
36011da177e4SLinus Torvalds 	return 0;
36021da177e4SLinus Torvalds }
36031da177e4SLinus Torvalds 
360427c6fa73SIdo Schimmel void rt6_sync_down_dev(struct net_device *dev, unsigned long event)
36051da177e4SLinus Torvalds {
36064c981e28SIdo Schimmel 	struct arg_netdev_event arg = {
36078ed67789SDaniel Lezcano 		.dev = dev,
36084c981e28SIdo Schimmel 		.event = event,
36098ed67789SDaniel Lezcano 	};
36108ed67789SDaniel Lezcano 
36114c981e28SIdo Schimmel 	fib6_clean_all(dev_net(dev), fib6_ifdown, &arg);
36124c981e28SIdo Schimmel }
36134c981e28SIdo Schimmel 
36144c981e28SIdo Schimmel void rt6_disable_ip(struct net_device *dev, unsigned long event)
36154c981e28SIdo Schimmel {
36164c981e28SIdo Schimmel 	rt6_sync_down_dev(dev, event);
36174c981e28SIdo Schimmel 	rt6_uncached_list_flush_dev(dev_net(dev), dev);
36184c981e28SIdo Schimmel 	neigh_ifdown(&nd_tbl, dev);
36191da177e4SLinus Torvalds }
36201da177e4SLinus Torvalds 
362195c96174SEric Dumazet struct rt6_mtu_change_arg {
36221da177e4SLinus Torvalds 	struct net_device *dev;
362395c96174SEric Dumazet 	unsigned int mtu;
36241da177e4SLinus Torvalds };
36251da177e4SLinus Torvalds 
36261da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
36271da177e4SLinus Torvalds {
36281da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
36291da177e4SLinus Torvalds 	struct inet6_dev *idev;
36301da177e4SLinus Torvalds 
36311da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
36321da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
36331da177e4SLinus Torvalds 	   We still use this lock to block changes
36341da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
36351da177e4SLinus Torvalds 	*/
36361da177e4SLinus Torvalds 
36371da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
363838308473SDavid S. Miller 	if (!idev)
36391da177e4SLinus Torvalds 		return 0;
36401da177e4SLinus Torvalds 
36411da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
36421da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
36431da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
36441da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
36451da177e4SLinus Torvalds 	 */
36461da177e4SLinus Torvalds 	/*
36471da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
36481da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
36491da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
36501da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
36511da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
36521da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
365367c408cfSAlexander Alemayhu 	   PMTU discovery.
36541da177e4SLinus Torvalds 	 */
3655d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
3656fb56be83SMaciej Żenczykowski 	    dst_metric_raw(&rt->dst, RTAX_MTU) &&
36574b32b5adSMartin KaFai Lau 	    !dst_metric_locked(&rt->dst, RTAX_MTU)) {
3658f5bbe7eeSWei Wang 		spin_lock_bh(&rt6_exception_lock);
36592b760fcfSWei Wang 		if (dst_mtu(&rt->dst) >= arg->mtu ||
3660d8d1f30bSChangli Gao 		    (dst_mtu(&rt->dst) < arg->mtu &&
36614b32b5adSMartin KaFai Lau 		     dst_mtu(&rt->dst) == idev->cnf.mtu6)) {
3662defb3519SDavid S. Miller 			dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
3663566cfd8fSSimon Arlott 		}
3664f5bbe7eeSWei Wang 		rt6_exceptions_update_pmtu(rt, arg->mtu);
3665f5bbe7eeSWei Wang 		spin_unlock_bh(&rt6_exception_lock);
36664b32b5adSMartin KaFai Lau 	}
36671da177e4SLinus Torvalds 	return 0;
36681da177e4SLinus Torvalds }
36691da177e4SLinus Torvalds 
367095c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
36711da177e4SLinus Torvalds {
3672c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
3673c71099acSThomas Graf 		.dev = dev,
3674c71099acSThomas Graf 		.mtu = mtu,
3675c71099acSThomas Graf 	};
36761da177e4SLinus Torvalds 
36770c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
36781da177e4SLinus Torvalds }
36791da177e4SLinus Torvalds 
3680ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
36815176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
368286872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
3683ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
368486872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
368586872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
368651ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
3687c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
368819e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
368919e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
369032bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
3691622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
36923b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
369386872cb5SThomas Graf };
369486872cb5SThomas Graf 
369586872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
3696333c4301SDavid Ahern 			      struct fib6_config *cfg,
3697333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
36981da177e4SLinus Torvalds {
369986872cb5SThomas Graf 	struct rtmsg *rtm;
370086872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
3701c78ba6d6SLubomir Rintel 	unsigned int pref;
370286872cb5SThomas Graf 	int err;
37031da177e4SLinus Torvalds 
3704fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
3705fceb6435SJohannes Berg 			  NULL);
370686872cb5SThomas Graf 	if (err < 0)
370786872cb5SThomas Graf 		goto errout;
37081da177e4SLinus Torvalds 
370986872cb5SThomas Graf 	err = -EINVAL;
371086872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
371186872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
371286872cb5SThomas Graf 
371386872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
371486872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
371586872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
371686872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
371786872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
3718ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
371986872cb5SThomas Graf 
3720ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
3721ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
3722b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
3723b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
372486872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
372586872cb5SThomas Graf 
3726ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
3727ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
3728ab79ad14SMaciej Żenczykowski 
37291f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
37301f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
37311f56a01fSMartin KaFai Lau 
373215e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
373386872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
37343b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
373586872cb5SThomas Graf 
373686872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
373767b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
373886872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
37391da177e4SLinus Torvalds 	}
374086872cb5SThomas Graf 
374186872cb5SThomas Graf 	if (tb[RTA_DST]) {
374286872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
374386872cb5SThomas Graf 
374486872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
374586872cb5SThomas Graf 			goto errout;
374686872cb5SThomas Graf 
374786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
37481da177e4SLinus Torvalds 	}
374986872cb5SThomas Graf 
375086872cb5SThomas Graf 	if (tb[RTA_SRC]) {
375186872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
375286872cb5SThomas Graf 
375386872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
375486872cb5SThomas Graf 			goto errout;
375586872cb5SThomas Graf 
375686872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
37571da177e4SLinus Torvalds 	}
375886872cb5SThomas Graf 
3759c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
376067b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
3761c3968a85SDaniel Walter 
376286872cb5SThomas Graf 	if (tb[RTA_OIF])
376386872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
376486872cb5SThomas Graf 
376586872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
376686872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
376786872cb5SThomas Graf 
376886872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
376986872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
377086872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
37711da177e4SLinus Torvalds 	}
377286872cb5SThomas Graf 
377386872cb5SThomas Graf 	if (tb[RTA_TABLE])
377486872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
377586872cb5SThomas Graf 
377651ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
377751ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
377851ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
37799ed59592SDavid Ahern 
37809ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
3781c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
37829ed59592SDavid Ahern 		if (err < 0)
37839ed59592SDavid Ahern 			goto errout;
378451ebd318SNicolas Dichtel 	}
378551ebd318SNicolas Dichtel 
3786c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
3787c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
3788c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
3789c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
3790c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
3791c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
3792c78ba6d6SLubomir Rintel 	}
3793c78ba6d6SLubomir Rintel 
379419e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
379519e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
379619e42e45SRoopa Prabhu 
37979ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
379819e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
379919e42e45SRoopa Prabhu 
3800c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
38019ed59592SDavid Ahern 		if (err < 0)
38029ed59592SDavid Ahern 			goto errout;
38039ed59592SDavid Ahern 	}
38049ed59592SDavid Ahern 
380532bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
380632bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
380732bc201eSXin Long 
380832bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
380932bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
381032bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
381132bc201eSXin Long 		}
381232bc201eSXin Long 	}
381332bc201eSXin Long 
381486872cb5SThomas Graf 	err = 0;
381586872cb5SThomas Graf errout:
381686872cb5SThomas Graf 	return err;
38171da177e4SLinus Torvalds }
38181da177e4SLinus Torvalds 
38196b9ea5a6SRoopa Prabhu struct rt6_nh {
38206b9ea5a6SRoopa Prabhu 	struct rt6_info *rt6_info;
38216b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
38226b9ea5a6SRoopa Prabhu 	struct mx6_config mxc;
38236b9ea5a6SRoopa Prabhu 	struct list_head next;
38246b9ea5a6SRoopa Prabhu };
38256b9ea5a6SRoopa Prabhu 
38266b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
38276b9ea5a6SRoopa Prabhu {
38286b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
38296b9ea5a6SRoopa Prabhu 
38306b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
38317d4d5065SDavid Ahern 		pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n",
38326b9ea5a6SRoopa Prabhu 		        &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
38336b9ea5a6SRoopa Prabhu 		        nh->r_cfg.fc_ifindex);
38346b9ea5a6SRoopa Prabhu 	}
38356b9ea5a6SRoopa Prabhu }
38366b9ea5a6SRoopa Prabhu 
38376b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list,
38386b9ea5a6SRoopa Prabhu 				 struct rt6_info *rt, struct fib6_config *r_cfg)
38396b9ea5a6SRoopa Prabhu {
38406b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
38416b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
38426b9ea5a6SRoopa Prabhu 
38436b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
38446b9ea5a6SRoopa Prabhu 		/* check if rt6_info already exists */
3845f06b7549SDavid Ahern 		if (rt6_duplicate_nexthop(nh->rt6_info, rt))
38466b9ea5a6SRoopa Prabhu 			return err;
38476b9ea5a6SRoopa Prabhu 	}
38486b9ea5a6SRoopa Prabhu 
38496b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
38506b9ea5a6SRoopa Prabhu 	if (!nh)
38516b9ea5a6SRoopa Prabhu 		return -ENOMEM;
38526b9ea5a6SRoopa Prabhu 	nh->rt6_info = rt;
38536b9ea5a6SRoopa Prabhu 	err = ip6_convert_metrics(&nh->mxc, r_cfg);
38546b9ea5a6SRoopa Prabhu 	if (err) {
38556b9ea5a6SRoopa Prabhu 		kfree(nh);
38566b9ea5a6SRoopa Prabhu 		return err;
38576b9ea5a6SRoopa Prabhu 	}
38586b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
38596b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
38606b9ea5a6SRoopa Prabhu 
38616b9ea5a6SRoopa Prabhu 	return 0;
38626b9ea5a6SRoopa Prabhu }
38636b9ea5a6SRoopa Prabhu 
38643b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt,
38653b1137feSDavid Ahern 				   struct rt6_info *rt_last,
38663b1137feSDavid Ahern 				   struct nl_info *info,
38673b1137feSDavid Ahern 				   __u16 nlflags)
38683b1137feSDavid Ahern {
38693b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
38703b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
38713b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
38723b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
38733b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
38743b1137feSDavid Ahern 	 */
38753b1137feSDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) {
38763b1137feSDavid Ahern 		rt = list_first_entry(&rt_last->rt6i_siblings,
38773b1137feSDavid Ahern 				      struct rt6_info,
38783b1137feSDavid Ahern 				      rt6i_siblings);
38793b1137feSDavid Ahern 	}
38803b1137feSDavid Ahern 
38813b1137feSDavid Ahern 	if (rt)
38823b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
38833b1137feSDavid Ahern }
38843b1137feSDavid Ahern 
3885333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
3886333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
388751ebd318SNicolas Dichtel {
38883b1137feSDavid Ahern 	struct rt6_info *rt_notif = NULL, *rt_last = NULL;
38893b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
389051ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
389151ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
38926b9ea5a6SRoopa Prabhu 	struct rt6_info *rt;
38936b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
38946b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
38953b1137feSDavid Ahern 	__u16 nlflags;
389651ebd318SNicolas Dichtel 	int remaining;
389751ebd318SNicolas Dichtel 	int attrlen;
38986b9ea5a6SRoopa Prabhu 	int err = 1;
38996b9ea5a6SRoopa Prabhu 	int nhn = 0;
39006b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
39016b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
39026b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
390351ebd318SNicolas Dichtel 
39043b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
39053b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
39063b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
39073b1137feSDavid Ahern 
390835f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
390951ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
391051ebd318SNicolas Dichtel 
39116b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
39126b9ea5a6SRoopa Prabhu 	 * rt6_info structs per nexthop
39136b9ea5a6SRoopa Prabhu 	 */
391451ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
391551ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
391651ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
391751ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
391851ebd318SNicolas Dichtel 
391951ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
392051ebd318SNicolas Dichtel 		if (attrlen > 0) {
392151ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
392251ebd318SNicolas Dichtel 
392351ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
392451ebd318SNicolas Dichtel 			if (nla) {
392567b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
392651ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
392751ebd318SNicolas Dichtel 			}
392819e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
392919e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
393019e42e45SRoopa Prabhu 			if (nla)
393119e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
393251ebd318SNicolas Dichtel 		}
39336b9ea5a6SRoopa Prabhu 
3934333c4301SDavid Ahern 		rt = ip6_route_info_create(&r_cfg, extack);
39358c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
39368c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
39378c5b83f0SRoopa Prabhu 			rt = NULL;
39386b9ea5a6SRoopa Prabhu 			goto cleanup;
39398c5b83f0SRoopa Prabhu 		}
39406b9ea5a6SRoopa Prabhu 
39416b9ea5a6SRoopa Prabhu 		err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg);
394251ebd318SNicolas Dichtel 		if (err) {
3943587fea74SWei Wang 			dst_release_immediate(&rt->dst);
39446b9ea5a6SRoopa Prabhu 			goto cleanup;
394551ebd318SNicolas Dichtel 		}
39466b9ea5a6SRoopa Prabhu 
39476b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
394851ebd318SNicolas Dichtel 	}
39496b9ea5a6SRoopa Prabhu 
39503b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
39513b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
39523b1137feSDavid Ahern 	 * the full route when done
39533b1137feSDavid Ahern 	 */
39543b1137feSDavid Ahern 	info->skip_notify = 1;
39553b1137feSDavid Ahern 
39566b9ea5a6SRoopa Prabhu 	err_nh = NULL;
39576b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
39583b1137feSDavid Ahern 		rt_last = nh->rt6_info;
3959333c4301SDavid Ahern 		err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack);
39603b1137feSDavid Ahern 		/* save reference to first route for notification */
39613b1137feSDavid Ahern 		if (!rt_notif && !err)
39623b1137feSDavid Ahern 			rt_notif = nh->rt6_info;
39633b1137feSDavid Ahern 
39646b9ea5a6SRoopa Prabhu 		/* nh->rt6_info is used or freed at this point, reset to NULL*/
39656b9ea5a6SRoopa Prabhu 		nh->rt6_info = NULL;
39666b9ea5a6SRoopa Prabhu 		if (err) {
39676b9ea5a6SRoopa Prabhu 			if (replace && nhn)
39686b9ea5a6SRoopa Prabhu 				ip6_print_replace_route_err(&rt6_nh_list);
39696b9ea5a6SRoopa Prabhu 			err_nh = nh;
39706b9ea5a6SRoopa Prabhu 			goto add_errout;
39716b9ea5a6SRoopa Prabhu 		}
39726b9ea5a6SRoopa Prabhu 
39731a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
397427596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
397527596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
397627596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
397727596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
397827596472SMichal Kubeček 		 * be added to it.
39791a72418bSNicolas Dichtel 		 */
398027596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
398127596472SMichal Kubeček 						     NLM_F_REPLACE);
39826b9ea5a6SRoopa Prabhu 		nhn++;
39836b9ea5a6SRoopa Prabhu 	}
39846b9ea5a6SRoopa Prabhu 
39853b1137feSDavid Ahern 	/* success ... tell user about new route */
39863b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
39876b9ea5a6SRoopa Prabhu 	goto cleanup;
39886b9ea5a6SRoopa Prabhu 
39896b9ea5a6SRoopa Prabhu add_errout:
39903b1137feSDavid Ahern 	/* send notification for routes that were added so that
39913b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
39923b1137feSDavid Ahern 	 * coherent
39933b1137feSDavid Ahern 	 */
39943b1137feSDavid Ahern 	if (rt_notif)
39953b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
39963b1137feSDavid Ahern 
39976b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
39986b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
39996b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
40006b9ea5a6SRoopa Prabhu 			break;
4001333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
40026b9ea5a6SRoopa Prabhu 	}
40036b9ea5a6SRoopa Prabhu 
40046b9ea5a6SRoopa Prabhu cleanup:
40056b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
4006587fea74SWei Wang 		if (nh->rt6_info)
4007587fea74SWei Wang 			dst_release_immediate(&nh->rt6_info->dst);
40086b9ea5a6SRoopa Prabhu 		kfree(nh->mxc.mx);
40096b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
40106b9ea5a6SRoopa Prabhu 		kfree(nh);
40116b9ea5a6SRoopa Prabhu 	}
40126b9ea5a6SRoopa Prabhu 
40136b9ea5a6SRoopa Prabhu 	return err;
40146b9ea5a6SRoopa Prabhu }
40156b9ea5a6SRoopa Prabhu 
4016333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
4017333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
40186b9ea5a6SRoopa Prabhu {
40196b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
40206b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
40216b9ea5a6SRoopa Prabhu 	int remaining;
40226b9ea5a6SRoopa Prabhu 	int attrlen;
40236b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
40246b9ea5a6SRoopa Prabhu 
40256b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
40266b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
40276b9ea5a6SRoopa Prabhu 
40286b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
40296b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
40306b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
40316b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
40326b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
40336b9ea5a6SRoopa Prabhu 
40346b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
40356b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
40366b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
40376b9ea5a6SRoopa Prabhu 
40386b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
40396b9ea5a6SRoopa Prabhu 			if (nla) {
40406b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
40416b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
40426b9ea5a6SRoopa Prabhu 			}
40436b9ea5a6SRoopa Prabhu 		}
4044333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
40456b9ea5a6SRoopa Prabhu 		if (err)
40466b9ea5a6SRoopa Prabhu 			last_err = err;
40476b9ea5a6SRoopa Prabhu 
404851ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
404951ebd318SNicolas Dichtel 	}
405051ebd318SNicolas Dichtel 
405151ebd318SNicolas Dichtel 	return last_err;
405251ebd318SNicolas Dichtel }
405351ebd318SNicolas Dichtel 
4054c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4055c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
40561da177e4SLinus Torvalds {
405786872cb5SThomas Graf 	struct fib6_config cfg;
405886872cb5SThomas Graf 	int err;
40591da177e4SLinus Torvalds 
4060333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
406186872cb5SThomas Graf 	if (err < 0)
406286872cb5SThomas Graf 		return err;
406386872cb5SThomas Graf 
406451ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4065333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
40660ae81335SDavid Ahern 	else {
40670ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
4068333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
40691da177e4SLinus Torvalds 	}
40700ae81335SDavid Ahern }
40711da177e4SLinus Torvalds 
4072c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
4073c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
40741da177e4SLinus Torvalds {
407586872cb5SThomas Graf 	struct fib6_config cfg;
407686872cb5SThomas Graf 	int err;
40771da177e4SLinus Torvalds 
4078333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
407986872cb5SThomas Graf 	if (err < 0)
408086872cb5SThomas Graf 		return err;
408186872cb5SThomas Graf 
408251ebd318SNicolas Dichtel 	if (cfg.fc_mp)
4083333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
408451ebd318SNicolas Dichtel 	else
4085333c4301SDavid Ahern 		return ip6_route_add(&cfg, extack);
40861da177e4SLinus Torvalds }
40871da177e4SLinus Torvalds 
4088beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt)
4089339bf98fSThomas Graf {
4090beb1afacSDavid Ahern 	int nexthop_len = 0;
4091beb1afacSDavid Ahern 
4092beb1afacSDavid Ahern 	if (rt->rt6i_nsiblings) {
4093beb1afacSDavid Ahern 		nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
4094beb1afacSDavid Ahern 			    + NLA_ALIGN(sizeof(struct rtnexthop))
4095beb1afacSDavid Ahern 			    + nla_total_size(16) /* RTA_GATEWAY */
4096beb1afacSDavid Ahern 			    + lwtunnel_get_encap_size(rt->dst.lwtstate);
4097beb1afacSDavid Ahern 
4098beb1afacSDavid Ahern 		nexthop_len *= rt->rt6i_nsiblings;
4099beb1afacSDavid Ahern 	}
4100beb1afacSDavid Ahern 
4101339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
4102339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
4103339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
4104339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
4105339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
4106339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
4107339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
4108339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
4109339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
41106a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
4111ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
4112c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
411319e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
4114beb1afacSDavid Ahern 	       + lwtunnel_get_encap_size(rt->dst.lwtstate)
4115beb1afacSDavid Ahern 	       + nexthop_len;
4116beb1afacSDavid Ahern }
4117beb1afacSDavid Ahern 
4118beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt,
41195be083ceSDavid Ahern 			    unsigned int *flags, bool skip_oif)
4120beb1afacSDavid Ahern {
4121f9d882eaSIdo Schimmel 	if (rt->rt6i_nh_flags & RTNH_F_DEAD)
4122f9d882eaSIdo Schimmel 		*flags |= RTNH_F_DEAD;
4123f9d882eaSIdo Schimmel 
412444c9f2f2SIdo Schimmel 	if (rt->rt6i_nh_flags & RTNH_F_LINKDOWN) {
4125beb1afacSDavid Ahern 		*flags |= RTNH_F_LINKDOWN;
4126beb1afacSDavid Ahern 		if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown)
4127beb1afacSDavid Ahern 			*flags |= RTNH_F_DEAD;
4128beb1afacSDavid Ahern 	}
4129beb1afacSDavid Ahern 
4130beb1afacSDavid Ahern 	if (rt->rt6i_flags & RTF_GATEWAY) {
4131beb1afacSDavid Ahern 		if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
4132beb1afacSDavid Ahern 			goto nla_put_failure;
4133beb1afacSDavid Ahern 	}
4134beb1afacSDavid Ahern 
4135fe400799SIdo Schimmel 	if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD)
413661e4d01eSIdo Schimmel 		*flags |= RTNH_F_OFFLOAD;
413761e4d01eSIdo Schimmel 
41385be083ceSDavid Ahern 	/* not needed for multipath encoding b/c it has a rtnexthop struct */
41395be083ceSDavid Ahern 	if (!skip_oif && rt->dst.dev &&
4140beb1afacSDavid Ahern 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
4141beb1afacSDavid Ahern 		goto nla_put_failure;
4142beb1afacSDavid Ahern 
4143beb1afacSDavid Ahern 	if (rt->dst.lwtstate &&
4144beb1afacSDavid Ahern 	    lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0)
4145beb1afacSDavid Ahern 		goto nla_put_failure;
4146beb1afacSDavid Ahern 
4147beb1afacSDavid Ahern 	return 0;
4148beb1afacSDavid Ahern 
4149beb1afacSDavid Ahern nla_put_failure:
4150beb1afacSDavid Ahern 	return -EMSGSIZE;
4151beb1afacSDavid Ahern }
4152beb1afacSDavid Ahern 
41535be083ceSDavid Ahern /* add multipath next hop */
4154beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt)
4155beb1afacSDavid Ahern {
4156beb1afacSDavid Ahern 	struct rtnexthop *rtnh;
4157beb1afacSDavid Ahern 	unsigned int flags = 0;
4158beb1afacSDavid Ahern 
4159beb1afacSDavid Ahern 	rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
4160beb1afacSDavid Ahern 	if (!rtnh)
4161beb1afacSDavid Ahern 		goto nla_put_failure;
4162beb1afacSDavid Ahern 
4163beb1afacSDavid Ahern 	rtnh->rtnh_hops = 0;
4164beb1afacSDavid Ahern 	rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0;
4165beb1afacSDavid Ahern 
41665be083ceSDavid Ahern 	if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
4167beb1afacSDavid Ahern 		goto nla_put_failure;
4168beb1afacSDavid Ahern 
4169beb1afacSDavid Ahern 	rtnh->rtnh_flags = flags;
4170beb1afacSDavid Ahern 
4171beb1afacSDavid Ahern 	/* length of rtnetlink header + attributes */
4172beb1afacSDavid Ahern 	rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
4173beb1afacSDavid Ahern 
4174beb1afacSDavid Ahern 	return 0;
4175beb1afacSDavid Ahern 
4176beb1afacSDavid Ahern nla_put_failure:
4177beb1afacSDavid Ahern 	return -EMSGSIZE;
4178339bf98fSThomas Graf }
4179339bf98fSThomas Graf 
4180191cd582SBrian Haley static int rt6_fill_node(struct net *net,
4181191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
41820d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
418315e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
4184f8cfe2ceSDavid Ahern 			 unsigned int flags)
41851da177e4SLinus Torvalds {
41864b32b5adSMartin KaFai Lau 	u32 metrics[RTAX_MAX];
41871da177e4SLinus Torvalds 	struct rtmsg *rtm;
41881da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
4189e3703b3dSThomas Graf 	long expires;
41909e762a4aSPatrick McHardy 	u32 table;
41911da177e4SLinus Torvalds 
419215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
419338308473SDavid S. Miller 	if (!nlh)
419426932566SPatrick McHardy 		return -EMSGSIZE;
41952d7202bfSThomas Graf 
41962d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
41971da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
41981da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
41991da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
42001da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
4201c71099acSThomas Graf 	if (rt->rt6i_table)
42029e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
4203c71099acSThomas Graf 	else
42049e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
42059e762a4aSPatrick McHardy 	rtm->rtm_table = table;
4206c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
4207c78679e8SDavid S. Miller 		goto nla_put_failure;
4208ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
4209ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
4210ef2c7d7bSNicolas Dichtel 		case -EINVAL:
4211ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
4212ef2c7d7bSNicolas Dichtel 			break;
4213ef2c7d7bSNicolas Dichtel 		case -EACCES:
4214ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
4215ef2c7d7bSNicolas Dichtel 			break;
4216b4949ab2SNicolas Dichtel 		case -EAGAIN:
4217b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
4218b4949ab2SNicolas Dichtel 			break;
4219ef2c7d7bSNicolas Dichtel 		default:
42201da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
4221ef2c7d7bSNicolas Dichtel 			break;
4222ef2c7d7bSNicolas Dichtel 		}
4223ef2c7d7bSNicolas Dichtel 	}
4224ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
4225ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
42264ee39733SDavid Ahern 	else if (rt->rt6i_flags & RTF_ANYCAST)
42274ee39733SDavid Ahern 		rtm->rtm_type = RTN_ANYCAST;
4228d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
42291da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
42301da177e4SLinus Torvalds 	else
42311da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
42321da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
42331da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
42341da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
42351da177e4SLinus Torvalds 
42361da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
42371da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
42381da177e4SLinus Torvalds 
42391da177e4SLinus Torvalds 	if (dst) {
4240930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_DST, dst))
4241c78679e8SDavid S. Miller 			goto nla_put_failure;
42421da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
42431da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
4244930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
4245c78679e8SDavid S. Miller 			goto nla_put_failure;
42461da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
42471da177e4SLinus Torvalds 	if (src) {
4248930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
4249c78679e8SDavid S. Miller 			goto nla_put_failure;
42501da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
4251c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
4252930345eaSJiri Benc 		   nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
4253c78679e8SDavid S. Miller 		goto nla_put_failure;
42541da177e4SLinus Torvalds #endif
42557bc570c8SYOSHIFUJI Hideaki 	if (iif) {
42567bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
42577bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
4258fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
42592cf75070SNikolay Aleksandrov 
42607bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
42617bc570c8SYOSHIFUJI Hideaki 				return 0;
4262fd61c6baSDavid Ahern 			if (err < 0)
42637bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
42647bc570c8SYOSHIFUJI Hideaki 		} else
42657bc570c8SYOSHIFUJI Hideaki #endif
4266c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
4267c78679e8SDavid S. Miller 				goto nla_put_failure;
42687bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
42691da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
4270c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
4271930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4272c78679e8SDavid S. Miller 			goto nla_put_failure;
4273c3968a85SDaniel Walter 	}
4274c3968a85SDaniel Walter 
4275c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
4276c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
42774e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
4278930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4279c78679e8SDavid S. Miller 			goto nla_put_failure;
42801da177e4SLinus Torvalds 	}
42812d7202bfSThomas Graf 
42824b32b5adSMartin KaFai Lau 	memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
42834b32b5adSMartin KaFai Lau 	if (rt->rt6i_pmtu)
42844b32b5adSMartin KaFai Lau 		metrics[RTAX_MTU - 1] = rt->rt6i_pmtu;
42854b32b5adSMartin KaFai Lau 	if (rtnetlink_put_metrics(skb, metrics) < 0)
42862d7202bfSThomas Graf 		goto nla_put_failure;
42872d7202bfSThomas Graf 
4288beb1afacSDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
4289beb1afacSDavid Ahern 		goto nla_put_failure;
4290beb1afacSDavid Ahern 
4291beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
4292beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
4293beb1afacSDavid Ahern 	 */
4294beb1afacSDavid Ahern 	if (rt->rt6i_nsiblings) {
4295beb1afacSDavid Ahern 		struct rt6_info *sibling, *next_sibling;
4296beb1afacSDavid Ahern 		struct nlattr *mp;
4297beb1afacSDavid Ahern 
4298beb1afacSDavid Ahern 		mp = nla_nest_start(skb, RTA_MULTIPATH);
4299beb1afacSDavid Ahern 		if (!mp)
4300beb1afacSDavid Ahern 			goto nla_put_failure;
4301beb1afacSDavid Ahern 
4302beb1afacSDavid Ahern 		if (rt6_add_nexthop(skb, rt) < 0)
4303beb1afacSDavid Ahern 			goto nla_put_failure;
4304beb1afacSDavid Ahern 
4305beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
4306beb1afacSDavid Ahern 					 &rt->rt6i_siblings, rt6i_siblings) {
4307beb1afacSDavid Ahern 			if (rt6_add_nexthop(skb, sibling) < 0)
430894f826b8SEric Dumazet 				goto nla_put_failure;
430994f826b8SEric Dumazet 		}
43102d7202bfSThomas Graf 
4311beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
4312beb1afacSDavid Ahern 	} else {
43135be083ceSDavid Ahern 		if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
4314c78679e8SDavid S. Miller 			goto nla_put_failure;
4315beb1afacSDavid Ahern 	}
43168253947eSLi Wei 
43178253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
431869cdf8f9SYOSHIFUJI Hideaki 
431987a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
4320e3703b3dSThomas Graf 		goto nla_put_failure;
43211da177e4SLinus Torvalds 
4322c78ba6d6SLubomir Rintel 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
4323c78ba6d6SLubomir Rintel 		goto nla_put_failure;
4324c78ba6d6SLubomir Rintel 
432519e42e45SRoopa Prabhu 
4326053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4327053c095aSJohannes Berg 	return 0;
43282d7202bfSThomas Graf 
43292d7202bfSThomas Graf nla_put_failure:
433026932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
433126932566SPatrick McHardy 	return -EMSGSIZE;
43321da177e4SLinus Torvalds }
43331da177e4SLinus Torvalds 
43341b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
43351da177e4SLinus Torvalds {
43361da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
43371f17e2f2SDavid Ahern 	struct net *net = arg->net;
43381f17e2f2SDavid Ahern 
43391f17e2f2SDavid Ahern 	if (rt == net->ipv6.ip6_null_entry)
43401f17e2f2SDavid Ahern 		return 0;
43411da177e4SLinus Torvalds 
43422d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
43432d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
4344f8cfe2ceSDavid Ahern 
4345f8cfe2ceSDavid Ahern 		/* user wants prefix routes only */
4346f8cfe2ceSDavid Ahern 		if (rtm->rtm_flags & RTM_F_PREFIX &&
4347f8cfe2ceSDavid Ahern 		    !(rt->rt6i_flags & RTF_PREFIX_RT)) {
4348f8cfe2ceSDavid Ahern 			/* success since this is not a prefix route */
4349f8cfe2ceSDavid Ahern 			return 1;
4350f8cfe2ceSDavid Ahern 		}
4351f8cfe2ceSDavid Ahern 	}
43521da177e4SLinus Torvalds 
43531f17e2f2SDavid Ahern 	return rt6_fill_node(net,
4354191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
435515e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
4356f8cfe2ceSDavid Ahern 		     NLM_F_MULTI);
43571da177e4SLinus Torvalds }
43581da177e4SLinus Torvalds 
4359c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4360c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
43611da177e4SLinus Torvalds {
43623b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
4363ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
436418c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
436518c3a61cSRoopa Prabhu 	struct dst_entry *dst;
43661da177e4SLinus Torvalds 	struct rt6_info *rt;
4367ab364a6fSThomas Graf 	struct sk_buff *skb;
4368ab364a6fSThomas Graf 	struct rtmsg *rtm;
43694c9483b2SDavid S. Miller 	struct flowi6 fl6;
437018c3a61cSRoopa Prabhu 	bool fibmatch;
4371ab364a6fSThomas Graf 
4372fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4373c21ef3e3SDavid Ahern 			  extack);
4374ab364a6fSThomas Graf 	if (err < 0)
4375ab364a6fSThomas Graf 		goto errout;
4376ab364a6fSThomas Graf 
4377ab364a6fSThomas Graf 	err = -EINVAL;
43784c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
437938b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
438038b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
438118c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
4382ab364a6fSThomas Graf 
4383ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
4384ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4385ab364a6fSThomas Graf 			goto errout;
4386ab364a6fSThomas Graf 
43874e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
4388ab364a6fSThomas Graf 	}
4389ab364a6fSThomas Graf 
4390ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
4391ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4392ab364a6fSThomas Graf 			goto errout;
4393ab364a6fSThomas Graf 
43944e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
4395ab364a6fSThomas Graf 	}
4396ab364a6fSThomas Graf 
4397ab364a6fSThomas Graf 	if (tb[RTA_IIF])
4398ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
4399ab364a6fSThomas Graf 
4400ab364a6fSThomas Graf 	if (tb[RTA_OIF])
440172331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
4402ab364a6fSThomas Graf 
44032e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
44042e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
44052e47b291SLorenzo Colitti 
4406622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
4407622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
4408622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
4409622ec2c9SLorenzo Colitti 	else
4410622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4411622ec2c9SLorenzo Colitti 
4412ab364a6fSThomas Graf 	if (iif) {
4413ab364a6fSThomas Graf 		struct net_device *dev;
441472331bc0SShmulik Ladkani 		int flags = 0;
441572331bc0SShmulik Ladkani 
4416121622dbSFlorian Westphal 		rcu_read_lock();
4417121622dbSFlorian Westphal 
4418121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
4419ab364a6fSThomas Graf 		if (!dev) {
4420121622dbSFlorian Westphal 			rcu_read_unlock();
4421ab364a6fSThomas Graf 			err = -ENODEV;
4422ab364a6fSThomas Graf 			goto errout;
4423ab364a6fSThomas Graf 		}
442472331bc0SShmulik Ladkani 
442572331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
442672331bc0SShmulik Ladkani 
442772331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
442872331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
442972331bc0SShmulik Ladkani 
443018c3a61cSRoopa Prabhu 		dst = ip6_route_input_lookup(net, dev, &fl6, flags);
4431121622dbSFlorian Westphal 
4432121622dbSFlorian Westphal 		rcu_read_unlock();
443372331bc0SShmulik Ladkani 	} else {
443472331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
443572331bc0SShmulik Ladkani 
443618c3a61cSRoopa Prabhu 		dst = ip6_route_output(net, NULL, &fl6);
443718c3a61cSRoopa Prabhu 	}
443818c3a61cSRoopa Prabhu 
443918c3a61cSRoopa Prabhu 
444018c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
444118c3a61cSRoopa Prabhu 	if (rt->dst.error) {
444218c3a61cSRoopa Prabhu 		err = rt->dst.error;
444318c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
444418c3a61cSRoopa Prabhu 		goto errout;
4445ab364a6fSThomas Graf 	}
44461da177e4SLinus Torvalds 
44479d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
44489d6acb3bSWANG Cong 		err = rt->dst.error;
44499d6acb3bSWANG Cong 		ip6_rt_put(rt);
44509d6acb3bSWANG Cong 		goto errout;
44519d6acb3bSWANG Cong 	}
44529d6acb3bSWANG Cong 
4453fba961abSDavid S. Miller 	if (fibmatch && rt->from) {
4454fba961abSDavid S. Miller 		struct rt6_info *ort = rt->from;
445558acfd71SIdo Schimmel 
445658acfd71SIdo Schimmel 		dst_hold(&ort->dst);
445758acfd71SIdo Schimmel 		ip6_rt_put(rt);
445858acfd71SIdo Schimmel 		rt = ort;
445958acfd71SIdo Schimmel 	}
446058acfd71SIdo Schimmel 
44611da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
446238308473SDavid S. Miller 	if (!skb) {
446394e187c0SAmerigo Wang 		ip6_rt_put(rt);
4464ab364a6fSThomas Graf 		err = -ENOBUFS;
4465ab364a6fSThomas Graf 		goto errout;
4466ab364a6fSThomas Graf 	}
44671da177e4SLinus Torvalds 
4468d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
446918c3a61cSRoopa Prabhu 	if (fibmatch)
447018c3a61cSRoopa Prabhu 		err = rt6_fill_node(net, skb, rt, NULL, NULL, iif,
447118c3a61cSRoopa Prabhu 				    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
447218c3a61cSRoopa Prabhu 				    nlh->nlmsg_seq, 0);
447318c3a61cSRoopa Prabhu 	else
44744c9483b2SDavid S. Miller 		err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
447515e47304SEric W. Biederman 				    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
4476f8cfe2ceSDavid Ahern 				    nlh->nlmsg_seq, 0);
44771da177e4SLinus Torvalds 	if (err < 0) {
4478ab364a6fSThomas Graf 		kfree_skb(skb);
4479ab364a6fSThomas Graf 		goto errout;
44801da177e4SLinus Torvalds 	}
44811da177e4SLinus Torvalds 
448215e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4483ab364a6fSThomas Graf errout:
44841da177e4SLinus Torvalds 	return err;
44851da177e4SLinus Torvalds }
44861da177e4SLinus Torvalds 
448737a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
448837a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
44891da177e4SLinus Torvalds {
44901da177e4SLinus Torvalds 	struct sk_buff *skb;
44915578689aSDaniel Lezcano 	struct net *net = info->nl_net;
4492528c4cebSDenis V. Lunev 	u32 seq;
4493528c4cebSDenis V. Lunev 	int err;
44940d51aa80SJamal Hadi Salim 
4495528c4cebSDenis V. Lunev 	err = -ENOBUFS;
449638308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
449786872cb5SThomas Graf 
449819e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
449938308473SDavid S. Miller 	if (!skb)
450021713ebcSThomas Graf 		goto errout;
45011da177e4SLinus Torvalds 
4502191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
4503f8cfe2ceSDavid Ahern 				event, info->portid, seq, nlm_flags);
450426932566SPatrick McHardy 	if (err < 0) {
450526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
450626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
450726932566SPatrick McHardy 		kfree_skb(skb);
450826932566SPatrick McHardy 		goto errout;
450926932566SPatrick McHardy 	}
451015e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
45115578689aSDaniel Lezcano 		    info->nlh, gfp_any());
45121ce85fe4SPablo Neira Ayuso 	return;
451321713ebcSThomas Graf errout:
451421713ebcSThomas Graf 	if (err < 0)
45155578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
45161da177e4SLinus Torvalds }
45171da177e4SLinus Torvalds 
45188ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
4519351638e7SJiri Pirko 				unsigned long event, void *ptr)
45208ed67789SDaniel Lezcano {
4521351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4522c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
45238ed67789SDaniel Lezcano 
4524242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
4525242d3a49SWANG Cong 		return NOTIFY_OK;
4526242d3a49SWANG Cong 
4527242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
4528d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
45298ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
45308ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
4531d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
45328ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
4533d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
45348ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
45358ed67789SDaniel Lezcano #endif
453676da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
453776da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
453876da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
453976da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
454076da0704SWANG Cong 		 */
454112d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
4542242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
454312d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
454412d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
4545242d3a49SWANG Cong #endif
45468ed67789SDaniel Lezcano 	}
45478ed67789SDaniel Lezcano 
45488ed67789SDaniel Lezcano 	return NOTIFY_OK;
45498ed67789SDaniel Lezcano }
45508ed67789SDaniel Lezcano 
45511da177e4SLinus Torvalds /*
45521da177e4SLinus Torvalds  *	/proc
45531da177e4SLinus Torvalds  */
45541da177e4SLinus Torvalds 
45551da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
45561da177e4SLinus Torvalds 
455733120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
455833120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
455933120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
456033120b30SAlexey Dobriyan 	.read		= seq_read,
456133120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
45628d2ca1d7SHannes Frederic Sowa 	.release	= seq_release_net,
456333120b30SAlexey Dobriyan };
456433120b30SAlexey Dobriyan 
45651da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
45661da177e4SLinus Torvalds {
456769ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
45681da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
456969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
457069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
457181eb8447SWei Wang 		   atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc),
457269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
457369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
4574fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
457569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
45761da177e4SLinus Torvalds 
45771da177e4SLinus Torvalds 	return 0;
45781da177e4SLinus Torvalds }
45791da177e4SLinus Torvalds 
45801da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
45811da177e4SLinus Torvalds {
4582de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
458369ddb805SDaniel Lezcano }
458469ddb805SDaniel Lezcano 
45859a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
45861da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
45871da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
45881da177e4SLinus Torvalds 	.read	 = seq_read,
45891da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
4590b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
45911da177e4SLinus Torvalds };
45921da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
45931da177e4SLinus Torvalds 
45941da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
45951da177e4SLinus Torvalds 
45961da177e4SLinus Torvalds static
4597fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
45981da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
45991da177e4SLinus Torvalds {
4600c486da34SLucian Adrian Grijincu 	struct net *net;
4601c486da34SLucian Adrian Grijincu 	int delay;
4602c486da34SLucian Adrian Grijincu 	if (!write)
4603c486da34SLucian Adrian Grijincu 		return -EINVAL;
4604c486da34SLucian Adrian Grijincu 
4605c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
4606c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
46078d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
46082ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
46091da177e4SLinus Torvalds 	return 0;
46101da177e4SLinus Torvalds }
46111da177e4SLinus Torvalds 
4612fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = {
46131da177e4SLinus Torvalds 	{
46141da177e4SLinus Torvalds 		.procname	=	"flush",
46154990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
46161da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
461789c8b3a1SDave Jones 		.mode		=	0200,
46186d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
46191da177e4SLinus Torvalds 	},
46201da177e4SLinus Torvalds 	{
46211da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
46229a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
46231da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
46241da177e4SLinus Torvalds 		.mode		=	0644,
46256d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
46261da177e4SLinus Torvalds 	},
46271da177e4SLinus Torvalds 	{
46281da177e4SLinus Torvalds 		.procname	=	"max_size",
46294990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
46301da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
46311da177e4SLinus Torvalds 		.mode		=	0644,
46326d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
46331da177e4SLinus Torvalds 	},
46341da177e4SLinus Torvalds 	{
46351da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
46364990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
46371da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
46381da177e4SLinus Torvalds 		.mode		=	0644,
46396d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
46401da177e4SLinus Torvalds 	},
46411da177e4SLinus Torvalds 	{
46421da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
46434990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
46441da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
46451da177e4SLinus Torvalds 		.mode		=	0644,
46466d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
46471da177e4SLinus Torvalds 	},
46481da177e4SLinus Torvalds 	{
46491da177e4SLinus Torvalds 		.procname	=	"gc_interval",
46504990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
46511da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
46521da177e4SLinus Torvalds 		.mode		=	0644,
46536d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
46541da177e4SLinus Torvalds 	},
46551da177e4SLinus Torvalds 	{
46561da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
46574990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
46581da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
46591da177e4SLinus Torvalds 		.mode		=	0644,
4660f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
46611da177e4SLinus Torvalds 	},
46621da177e4SLinus Torvalds 	{
46631da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
46644990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
46651da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
46661da177e4SLinus Torvalds 		.mode		=	0644,
46676d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
46681da177e4SLinus Torvalds 	},
46691da177e4SLinus Torvalds 	{
46701da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
46714990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
46721da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
46731da177e4SLinus Torvalds 		.mode		=	0644,
4674f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
46751da177e4SLinus Torvalds 	},
46761da177e4SLinus Torvalds 	{
46771da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
46784990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
46791da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
46801da177e4SLinus Torvalds 		.mode		=	0644,
46816d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
46821da177e4SLinus Torvalds 	},
4683f8572d8fSEric W. Biederman 	{ }
46841da177e4SLinus Torvalds };
46851da177e4SLinus Torvalds 
46862c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
4687760f2d01SDaniel Lezcano {
4688760f2d01SDaniel Lezcano 	struct ctl_table *table;
4689760f2d01SDaniel Lezcano 
4690760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
4691760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
4692760f2d01SDaniel Lezcano 			GFP_KERNEL);
46935ee09105SYOSHIFUJI Hideaki 
46945ee09105SYOSHIFUJI Hideaki 	if (table) {
46955ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
4696c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
469786393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
46985ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
46995ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
47005ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
47015ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
47025ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
47035ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
47045ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
47059c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
4706464dc801SEric W. Biederman 
4707464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
4708464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
4709464dc801SEric W. Biederman 			table[0].procname = NULL;
47105ee09105SYOSHIFUJI Hideaki 	}
47115ee09105SYOSHIFUJI Hideaki 
4712760f2d01SDaniel Lezcano 	return table;
4713760f2d01SDaniel Lezcano }
47141da177e4SLinus Torvalds #endif
47151da177e4SLinus Torvalds 
47162c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
4717cdb18761SDaniel Lezcano {
4718633d424bSPavel Emelyanov 	int ret = -ENOMEM;
47198ed67789SDaniel Lezcano 
472086393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
472186393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
4722f2fc6a54SBenjamin Thery 
4723fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
4724fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
4725fc66f95cSEric Dumazet 
47268ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
47278ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
47288ed67789SDaniel Lezcano 					   GFP_KERNEL);
47298ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
4730fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
4731d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
473262fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
473362fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
47348ed67789SDaniel Lezcano 
47358ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
4736feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
47378ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
47388ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
47398ed67789SDaniel Lezcano 					       GFP_KERNEL);
474068fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
474168fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
4742d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
474362fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
474462fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
47458ed67789SDaniel Lezcano 
47468ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
47478ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
47488ed67789SDaniel Lezcano 					       GFP_KERNEL);
474968fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
475068fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
4751d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
475262fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
475362fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
47548ed67789SDaniel Lezcano #endif
47558ed67789SDaniel Lezcano 
4756b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
4757b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
4758b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
4759b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
4760b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
4761b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
4762b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
4763b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
4764b339a47cSPeter Zijlstra 
47656891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
47666891a346SBenjamin Thery 
47678ed67789SDaniel Lezcano 	ret = 0;
47688ed67789SDaniel Lezcano out:
47698ed67789SDaniel Lezcano 	return ret;
4770f2fc6a54SBenjamin Thery 
477168fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
477268fffc67SPeter Zijlstra out_ip6_prohibit_entry:
477368fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
477468fffc67SPeter Zijlstra out_ip6_null_entry:
477568fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
477668fffc67SPeter Zijlstra #endif
4777fc66f95cSEric Dumazet out_ip6_dst_entries:
4778fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
4779f2fc6a54SBenjamin Thery out_ip6_dst_ops:
4780f2fc6a54SBenjamin Thery 	goto out;
4781cdb18761SDaniel Lezcano }
4782cdb18761SDaniel Lezcano 
47832c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
4784cdb18761SDaniel Lezcano {
47858ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
47868ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
47878ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
47888ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
47898ed67789SDaniel Lezcano #endif
479041bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
4791cdb18761SDaniel Lezcano }
4792cdb18761SDaniel Lezcano 
4793d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
4794d189634eSThomas Graf {
4795d189634eSThomas Graf #ifdef CONFIG_PROC_FS
4796d4beaa66SGao feng 	proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
4797d4beaa66SGao feng 	proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
4798d189634eSThomas Graf #endif
4799d189634eSThomas Graf 	return 0;
4800d189634eSThomas Graf }
4801d189634eSThomas Graf 
4802d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
4803d189634eSThomas Graf {
4804d189634eSThomas Graf #ifdef CONFIG_PROC_FS
4805ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
4806ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
4807d189634eSThomas Graf #endif
4808d189634eSThomas Graf }
4809d189634eSThomas Graf 
4810cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
4811cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
4812cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
4813cdb18761SDaniel Lezcano };
4814cdb18761SDaniel Lezcano 
4815c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
4816c3426b47SDavid S. Miller {
4817c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
4818c3426b47SDavid S. Miller 
4819c3426b47SDavid S. Miller 	if (!bp)
4820c3426b47SDavid S. Miller 		return -ENOMEM;
4821c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
4822c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
4823c3426b47SDavid S. Miller 	return 0;
4824c3426b47SDavid S. Miller }
4825c3426b47SDavid S. Miller 
4826c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
4827c3426b47SDavid S. Miller {
4828c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
4829c3426b47SDavid S. Miller 
4830c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
483156a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
4832c3426b47SDavid S. Miller 	kfree(bp);
4833c3426b47SDavid S. Miller }
4834c3426b47SDavid S. Miller 
48352b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
4836c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
4837c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
4838c3426b47SDavid S. Miller };
4839c3426b47SDavid S. Miller 
4840d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
4841d189634eSThomas Graf 	.init = ip6_route_net_init_late,
4842d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
4843d189634eSThomas Graf };
4844d189634eSThomas Graf 
48458ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
48468ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
4847242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
48488ed67789SDaniel Lezcano };
48498ed67789SDaniel Lezcano 
48502f460933SWANG Cong void __init ip6_route_init_special_entries(void)
48512f460933SWANG Cong {
48522f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
48532f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
48542f460933SWANG Cong 	 * manually for init_net */
48552f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
48562f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
48572f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
48582f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
48592f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
48602f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
48612f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
48622f460933SWANG Cong   #endif
48632f460933SWANG Cong }
48642f460933SWANG Cong 
4865433d49c3SDaniel Lezcano int __init ip6_route_init(void)
48661da177e4SLinus Torvalds {
4867433d49c3SDaniel Lezcano 	int ret;
48688d0b94afSMartin KaFai Lau 	int cpu;
4869433d49c3SDaniel Lezcano 
48709a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
48719a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
48729a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
48739a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
48749a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
4875c19a28e1SFernando Carrijo 		goto out;
487614e50e57SDavid S. Miller 
4877fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
48788ed67789SDaniel Lezcano 	if (ret)
4879bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
4880bdb3289fSDaniel Lezcano 
4881c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
4882c3426b47SDavid S. Miller 	if (ret)
4883e8803b6cSDavid S. Miller 		goto out_dst_entries;
48842a0c451aSThomas Graf 
48857e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
48867e52b33bSDavid S. Miller 	if (ret)
48877e52b33bSDavid S. Miller 		goto out_register_inetpeer;
4888c3426b47SDavid S. Miller 
48895dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
48905dc121e9SArnaud Ebalard 
4891e8803b6cSDavid S. Miller 	ret = fib6_init();
4892433d49c3SDaniel Lezcano 	if (ret)
48938ed67789SDaniel Lezcano 		goto out_register_subsys;
4894433d49c3SDaniel Lezcano 
4895433d49c3SDaniel Lezcano 	ret = xfrm6_init();
4896433d49c3SDaniel Lezcano 	if (ret)
4897e8803b6cSDavid S. Miller 		goto out_fib6_init;
4898c35b7e72SDaniel Lezcano 
4899433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
4900433d49c3SDaniel Lezcano 	if (ret)
4901433d49c3SDaniel Lezcano 		goto xfrm6_init;
49027e5449c2SDaniel Lezcano 
4903d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
4904d189634eSThomas Graf 	if (ret)
4905d189634eSThomas Graf 		goto fib6_rules_init;
4906d189634eSThomas Graf 
490716feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE,
490816feebcfSFlorian Westphal 				   inet6_rtm_newroute, NULL, 0);
490916feebcfSFlorian Westphal 	if (ret < 0)
491016feebcfSFlorian Westphal 		goto out_register_late_subsys;
491116feebcfSFlorian Westphal 
491216feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE,
491316feebcfSFlorian Westphal 				   inet6_rtm_delroute, NULL, 0);
491416feebcfSFlorian Westphal 	if (ret < 0)
491516feebcfSFlorian Westphal 		goto out_register_late_subsys;
491616feebcfSFlorian Westphal 
491716feebcfSFlorian Westphal 	ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE,
491816feebcfSFlorian Westphal 				   inet6_rtm_getroute, NULL,
491916feebcfSFlorian Westphal 				   RTNL_FLAG_DOIT_UNLOCKED);
492016feebcfSFlorian Westphal 	if (ret < 0)
4921d189634eSThomas Graf 		goto out_register_late_subsys;
4922433d49c3SDaniel Lezcano 
49238ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
4924cdb18761SDaniel Lezcano 	if (ret)
4925d189634eSThomas Graf 		goto out_register_late_subsys;
49268ed67789SDaniel Lezcano 
49278d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
49288d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
49298d0b94afSMartin KaFai Lau 
49308d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
49318d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
49328d0b94afSMartin KaFai Lau 	}
49338d0b94afSMartin KaFai Lau 
4934433d49c3SDaniel Lezcano out:
4935433d49c3SDaniel Lezcano 	return ret;
4936433d49c3SDaniel Lezcano 
4937d189634eSThomas Graf out_register_late_subsys:
493816feebcfSFlorian Westphal 	rtnl_unregister_all(PF_INET6);
4939d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
4940433d49c3SDaniel Lezcano fib6_rules_init:
4941433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
4942433d49c3SDaniel Lezcano xfrm6_init:
4943433d49c3SDaniel Lezcano 	xfrm6_fini();
49442a0c451aSThomas Graf out_fib6_init:
49452a0c451aSThomas Graf 	fib6_gc_cleanup();
49468ed67789SDaniel Lezcano out_register_subsys:
49478ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
49487e52b33bSDavid S. Miller out_register_inetpeer:
49497e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
4950fc66f95cSEric Dumazet out_dst_entries:
4951fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
4952433d49c3SDaniel Lezcano out_kmem_cache:
4953f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
4954433d49c3SDaniel Lezcano 	goto out;
49551da177e4SLinus Torvalds }
49561da177e4SLinus Torvalds 
49571da177e4SLinus Torvalds void ip6_route_cleanup(void)
49581da177e4SLinus Torvalds {
49598ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
4960d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
4961101367c2SThomas Graf 	fib6_rules_cleanup();
49621da177e4SLinus Torvalds 	xfrm6_fini();
49631da177e4SLinus Torvalds 	fib6_gc_cleanup();
4964c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
49658ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
496641bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
4967f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
49681da177e4SLinus Torvalds }
4969