xref: /openbmc/linux/net/ipv6/route.c (revision 2b760fcf5cfb34e8610df56d83745b2b74ae1379)
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;
1468d0b94afSMartin KaFai Lau 
1478d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1488d0b94afSMartin KaFai Lau 		list_del(&rt->rt6i_uncached);
1498d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1508d0b94afSMartin KaFai Lau 	}
1518d0b94afSMartin KaFai Lau }
1528d0b94afSMartin KaFai Lau 
1538d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev)
1548d0b94afSMartin KaFai Lau {
1558d0b94afSMartin KaFai Lau 	struct net_device *loopback_dev = net->loopback_dev;
1568d0b94afSMartin KaFai Lau 	int cpu;
1578d0b94afSMartin KaFai Lau 
158e332bc67SEric W. Biederman 	if (dev == loopback_dev)
159e332bc67SEric W. Biederman 		return;
160e332bc67SEric W. Biederman 
1618d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
1628d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
1638d0b94afSMartin KaFai Lau 		struct rt6_info *rt;
1648d0b94afSMartin KaFai Lau 
1658d0b94afSMartin KaFai Lau 		spin_lock_bh(&ul->lock);
1668d0b94afSMartin KaFai Lau 		list_for_each_entry(rt, &ul->head, rt6i_uncached) {
1678d0b94afSMartin KaFai Lau 			struct inet6_dev *rt_idev = rt->rt6i_idev;
1688d0b94afSMartin KaFai Lau 			struct net_device *rt_dev = rt->dst.dev;
1698d0b94afSMartin KaFai Lau 
170e332bc67SEric W. Biederman 			if (rt_idev->dev == dev) {
1718d0b94afSMartin KaFai Lau 				rt->rt6i_idev = in6_dev_get(loopback_dev);
1728d0b94afSMartin KaFai Lau 				in6_dev_put(rt_idev);
1738d0b94afSMartin KaFai Lau 			}
1748d0b94afSMartin KaFai Lau 
175e332bc67SEric W. Biederman 			if (rt_dev == dev) {
1768d0b94afSMartin KaFai Lau 				rt->dst.dev = loopback_dev;
1778d0b94afSMartin KaFai Lau 				dev_hold(rt->dst.dev);
1788d0b94afSMartin KaFai Lau 				dev_put(rt_dev);
1798d0b94afSMartin KaFai Lau 			}
1808d0b94afSMartin KaFai Lau 		}
1818d0b94afSMartin KaFai Lau 		spin_unlock_bh(&ul->lock);
1828d0b94afSMartin KaFai Lau 	}
1838d0b94afSMartin KaFai Lau }
1848d0b94afSMartin KaFai Lau 
185d52d3997SMartin KaFai Lau static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt)
186d52d3997SMartin KaFai Lau {
187d52d3997SMartin KaFai Lau 	return dst_metrics_write_ptr(rt->dst.from);
188d52d3997SMartin KaFai Lau }
189d52d3997SMartin KaFai Lau 
19006582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
19106582540SDavid S. Miller {
19206582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *)dst;
19306582540SDavid S. Miller 
194d52d3997SMartin KaFai Lau 	if (rt->rt6i_flags & RTF_PCPU)
195d52d3997SMartin KaFai Lau 		return rt6_pcpu_cow_metrics(rt);
196d52d3997SMartin KaFai Lau 	else if (rt->rt6i_flags & RTF_CACHE)
1974b32b5adSMartin KaFai Lau 		return NULL;
1984b32b5adSMartin KaFai Lau 	else
1993b471175SMartin KaFai Lau 		return dst_cow_metrics_generic(dst, old);
20006582540SDavid S. Miller }
20106582540SDavid S. Miller 
202f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt,
203f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
204f894cbf8SDavid S. Miller 					     const void *daddr)
20539232973SDavid S. Miller {
20639232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
20739232973SDavid S. Miller 
208a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
20939232973SDavid S. Miller 		return (const void *) p;
210f894cbf8SDavid S. Miller 	else if (skb)
211f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
21239232973SDavid S. Miller 	return daddr;
21339232973SDavid S. Miller }
21439232973SDavid S. Miller 
215f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
216f894cbf8SDavid S. Miller 					  struct sk_buff *skb,
217f894cbf8SDavid S. Miller 					  const void *daddr)
218d3aaeb38SDavid S. Miller {
21939232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
22039232973SDavid S. Miller 	struct neighbour *n;
22139232973SDavid S. Miller 
222f894cbf8SDavid S. Miller 	daddr = choose_neigh_daddr(rt, skb, daddr);
2238e022ee6SYOSHIFUJI Hideaki / 吉藤英明 	n = __ipv6_neigh_lookup(dst->dev, daddr);
224f83c7790SDavid S. Miller 	if (n)
225f83c7790SDavid S. Miller 		return n;
226f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
227f83c7790SDavid S. Miller }
228f83c7790SDavid S. Miller 
22963fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr)
23063fca65dSJulian Anastasov {
23163fca65dSJulian Anastasov 	struct net_device *dev = dst->dev;
23263fca65dSJulian Anastasov 	struct rt6_info *rt = (struct rt6_info *)dst;
23363fca65dSJulian Anastasov 
23463fca65dSJulian Anastasov 	daddr = choose_neigh_daddr(rt, NULL, daddr);
23563fca65dSJulian Anastasov 	if (!daddr)
23663fca65dSJulian Anastasov 		return;
23763fca65dSJulian Anastasov 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
23863fca65dSJulian Anastasov 		return;
23963fca65dSJulian Anastasov 	if (ipv6_addr_is_multicast((const struct in6_addr *)daddr))
24063fca65dSJulian Anastasov 		return;
24163fca65dSJulian Anastasov 	__ipv6_confirm_neigh(dev, daddr);
24263fca65dSJulian Anastasov }
24363fca65dSJulian Anastasov 
2449a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
2451da177e4SLinus Torvalds 	.family			=	AF_INET6,
2461da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
2471da177e4SLinus Torvalds 	.gc_thresh		=	1024,
2481da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
2490dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
250ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
25106582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
2521da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
2531da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
2541da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
2551da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
2561da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
2576e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
2589f8955ccSEric W. Biederman 	.local_out		=	__ip6_local_out,
259d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
26063fca65dSJulian Anastasov 	.confirm_neigh		=	ip6_confirm_neigh,
2611da177e4SLinus Torvalds };
2621da177e4SLinus Torvalds 
263ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
264ec831ea7SRoland Dreier {
265618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
266618f9bc7SSteffen Klassert 
267618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
268ec831ea7SRoland Dreier }
269ec831ea7SRoland Dreier 
2706700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
2716700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
27214e50e57SDavid S. Miller {
27314e50e57SDavid S. Miller }
27414e50e57SDavid S. Miller 
2756700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
2766700c270SDavid S. Miller 				      struct sk_buff *skb)
277b587ee3bSDavid S. Miller {
278b587ee3bSDavid S. Miller }
279b587ee3bSDavid S. Miller 
28014e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
28114e50e57SDavid S. Miller 	.family			=	AF_INET6,
28214e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
28314e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
284ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
285214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
28614e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
287b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2880a1f5962SMartin KaFai Lau 	.cow_metrics		=	dst_cow_metrics_generic,
289d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
29014e50e57SDavid S. Miller };
29114e50e57SDavid S. Miller 
29262fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
29314edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
29462fa8a84SDavid S. Miller };
29562fa8a84SDavid S. Miller 
296fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
2971da177e4SLinus Torvalds 	.dst = {
2981da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2991da177e4SLinus Torvalds 		.__use		= 1,
3002c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
3011da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
3021da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
3031da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
3041da177e4SLinus Torvalds 	},
3051da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3064f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
3071da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
3081da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
3091da177e4SLinus Torvalds };
3101da177e4SLinus Torvalds 
311101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
312101367c2SThomas Graf 
313fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
314101367c2SThomas Graf 	.dst = {
315101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
316101367c2SThomas Graf 		.__use		= 1,
3172c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
318101367c2SThomas Graf 		.error		= -EACCES,
3199ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
3209ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
321101367c2SThomas Graf 	},
322101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3234f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
324101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
325101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
326101367c2SThomas Graf };
327101367c2SThomas Graf 
328fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
329101367c2SThomas Graf 	.dst = {
330101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
331101367c2SThomas Graf 		.__use		= 1,
3322c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
333101367c2SThomas Graf 		.error		= -EINVAL,
334352e512cSHerbert Xu 		.input		= dst_discard,
335ede2059dSEric W. Biederman 		.output		= dst_discard_out,
336101367c2SThomas Graf 	},
337101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
3384f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
339101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
340101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
341101367c2SThomas Graf };
342101367c2SThomas Graf 
343101367c2SThomas Graf #endif
344101367c2SThomas Graf 
345ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt)
346ebfa45f0SMartin KaFai Lau {
347ebfa45f0SMartin KaFai Lau 	struct dst_entry *dst = &rt->dst;
348ebfa45f0SMartin KaFai Lau 
349ebfa45f0SMartin KaFai Lau 	memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
350ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_siblings);
351ebfa45f0SMartin KaFai Lau 	INIT_LIST_HEAD(&rt->rt6i_uncached);
352ebfa45f0SMartin KaFai Lau }
353ebfa45f0SMartin KaFai Lau 
3541da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
355d52d3997SMartin KaFai Lau static struct rt6_info *__ip6_dst_alloc(struct net *net,
356957c665fSDavid S. Miller 					struct net_device *dev,
357ad706862SMartin KaFai Lau 					int flags)
3581da177e4SLinus Torvalds {
35997bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
360b2a9c0edSWei Wang 					1, DST_OBSOLETE_FORCE_CHK, flags);
361cf911662SDavid S. Miller 
362ebfa45f0SMartin KaFai Lau 	if (rt)
363ebfa45f0SMartin KaFai Lau 		rt6_info_init(rt);
3648104891bSSteffen Klassert 
365cf911662SDavid S. Miller 	return rt;
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3689ab179d8SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net,
369d52d3997SMartin KaFai Lau 			       struct net_device *dev,
370ad706862SMartin KaFai Lau 			       int flags)
371d52d3997SMartin KaFai Lau {
372ad706862SMartin KaFai Lau 	struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags);
373d52d3997SMartin KaFai Lau 
374d52d3997SMartin KaFai Lau 	if (rt) {
375d52d3997SMartin KaFai Lau 		rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC);
376d52d3997SMartin KaFai Lau 		if (rt->rt6i_pcpu) {
377d52d3997SMartin KaFai Lau 			int cpu;
378d52d3997SMartin KaFai Lau 
379d52d3997SMartin KaFai Lau 			for_each_possible_cpu(cpu) {
380d52d3997SMartin KaFai Lau 				struct rt6_info **p;
381d52d3997SMartin KaFai Lau 
382d52d3997SMartin KaFai Lau 				p = per_cpu_ptr(rt->rt6i_pcpu, cpu);
383d52d3997SMartin KaFai Lau 				/* no one shares rt */
384d52d3997SMartin KaFai Lau 				*p =  NULL;
385d52d3997SMartin KaFai Lau 			}
386d52d3997SMartin KaFai Lau 		} else {
387587fea74SWei Wang 			dst_release_immediate(&rt->dst);
388d52d3997SMartin KaFai Lau 			return NULL;
389d52d3997SMartin KaFai Lau 		}
390d52d3997SMartin KaFai Lau 	}
391d52d3997SMartin KaFai Lau 
392d52d3997SMartin KaFai Lau 	return rt;
393d52d3997SMartin KaFai Lau }
3949ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc);
395d52d3997SMartin KaFai Lau 
3961da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3971da177e4SLinus Torvalds {
3981da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
39935732d01SWei Wang 	struct rt6_exception_bucket *bucket;
400ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	struct dst_entry *from = dst->from;
4018d0b94afSMartin KaFai Lau 	struct inet6_dev *idev;
4021da177e4SLinus Torvalds 
4038e2ec639SYan, Zheng 	dst_destroy_metrics_generic(dst);
404d52d3997SMartin KaFai Lau 	free_percpu(rt->rt6i_pcpu);
4058d0b94afSMartin KaFai Lau 	rt6_uncached_list_del(rt);
4068d0b94afSMartin KaFai Lau 
4078d0b94afSMartin KaFai Lau 	idev = rt->rt6i_idev;
40838308473SDavid S. Miller 	if (idev) {
4091da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
4101da177e4SLinus Torvalds 		in6_dev_put(idev);
4111da177e4SLinus Torvalds 	}
41235732d01SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1);
41335732d01SWei Wang 	if (bucket) {
41435732d01SWei Wang 		rt->rt6i_exception_bucket = NULL;
41535732d01SWei Wang 		kfree(bucket);
41635732d01SWei Wang 	}
4171716a961SGao feng 
418ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst->from = NULL;
419ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst_release(from);
420b3419363SDavid S. Miller }
421b3419363SDavid S. Miller 
4221da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
4231da177e4SLinus Torvalds 			   int how)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
4261da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
4275a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
428c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
4291da177e4SLinus Torvalds 
430e5645f51SWei Wang 	if (idev && idev->dev != loopback_dev) {
431e5645f51SWei Wang 		struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev);
43238308473SDavid S. Miller 		if (loopback_idev) {
4331da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
4341da177e4SLinus Torvalds 			in6_dev_put(idev);
4351da177e4SLinus Torvalds 		}
4361da177e4SLinus Torvalds 	}
43797cac082SDavid S. Miller }
4381da177e4SLinus Torvalds 
4395973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt)
4405973fb1eSMartin KaFai Lau {
4415973fb1eSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_EXPIRES)
4425973fb1eSMartin KaFai Lau 		return time_after(jiffies, rt->dst.expires);
4435973fb1eSMartin KaFai Lau 	else
4445973fb1eSMartin KaFai Lau 		return false;
4455973fb1eSMartin KaFai Lau }
4465973fb1eSMartin KaFai Lau 
447a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
4481da177e4SLinus Torvalds {
4491716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
4501716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
451a50feda5SEric Dumazet 			return true;
4521716a961SGao feng 	} else if (rt->dst.from) {
4531e2ea8adSXin Long 		return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK ||
4541e2ea8adSXin Long 		       rt6_check_expired((struct rt6_info *)rt->dst.from);
4551716a961SGao feng 	}
456a50feda5SEric Dumazet 	return false;
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
45951ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
46052bd4c0cSNicolas Dichtel 					     struct flowi6 *fl6, int oif,
46152bd4c0cSNicolas Dichtel 					     int strict)
46251ebd318SNicolas Dichtel {
46351ebd318SNicolas Dichtel 	struct rt6_info *sibling, *next_sibling;
46451ebd318SNicolas Dichtel 	int route_choosen;
46551ebd318SNicolas Dichtel 
466b673d6ccSJakub Sitnicki 	/* We might have already computed the hash for ICMPv6 errors. In such
467b673d6ccSJakub Sitnicki 	 * case it will always be non-zero. Otherwise now is the time to do it.
468b673d6ccSJakub Sitnicki 	 */
469b673d6ccSJakub Sitnicki 	if (!fl6->mp_hash)
470b673d6ccSJakub Sitnicki 		fl6->mp_hash = rt6_multipath_hash(fl6, NULL);
471b673d6ccSJakub Sitnicki 
472b673d6ccSJakub Sitnicki 	route_choosen = fl6->mp_hash % (match->rt6i_nsiblings + 1);
47351ebd318SNicolas Dichtel 	/* Don't change the route, if route_choosen == 0
47451ebd318SNicolas Dichtel 	 * (siblings does not include ourself)
47551ebd318SNicolas Dichtel 	 */
47651ebd318SNicolas Dichtel 	if (route_choosen)
47751ebd318SNicolas Dichtel 		list_for_each_entry_safe(sibling, next_sibling,
47851ebd318SNicolas Dichtel 				&match->rt6i_siblings, rt6i_siblings) {
47951ebd318SNicolas Dichtel 			route_choosen--;
48051ebd318SNicolas Dichtel 			if (route_choosen == 0) {
48152bd4c0cSNicolas Dichtel 				if (rt6_score_route(sibling, oif, strict) < 0)
48252bd4c0cSNicolas Dichtel 					break;
48351ebd318SNicolas Dichtel 				match = sibling;
48451ebd318SNicolas Dichtel 				break;
48551ebd318SNicolas Dichtel 			}
48651ebd318SNicolas Dichtel 		}
48751ebd318SNicolas Dichtel 	return match;
48851ebd318SNicolas Dichtel }
48951ebd318SNicolas Dichtel 
4901da177e4SLinus Torvalds /*
491c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
4921da177e4SLinus Torvalds  */
4931da177e4SLinus Torvalds 
4948ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
4958ed67789SDaniel Lezcano 						    struct rt6_info *rt,
496b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4971da177e4SLinus Torvalds 						    int oif,
498d420895eSYOSHIFUJI Hideaki 						    int flags)
4991da177e4SLinus Torvalds {
5001da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
5011da177e4SLinus Torvalds 	struct rt6_info *sprt;
5021da177e4SLinus Torvalds 
503dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
504dd3abc4eSYOSHIFUJI Hideaki 		goto out;
505dd3abc4eSYOSHIFUJI Hideaki 
506d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
507d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
508dd3abc4eSYOSHIFUJI Hideaki 
509dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
5101da177e4SLinus Torvalds 			if (dev->ifindex == oif)
5111da177e4SLinus Torvalds 				return sprt;
5121da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
51338308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
5141da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
51517fb0b2bSDavid Ahern 					if (flags & RT6_LOOKUP_F_IFACE)
5161da177e4SLinus Torvalds 						continue;
51717fb0b2bSDavid Ahern 					if (local &&
51817fb0b2bSDavid Ahern 					    local->rt6i_idev->dev->ifindex == oif)
5191da177e4SLinus Torvalds 						continue;
5201da177e4SLinus Torvalds 				}
5211da177e4SLinus Torvalds 				local = sprt;
5221da177e4SLinus Torvalds 			}
523dd3abc4eSYOSHIFUJI Hideaki 		} else {
524dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
525dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
526dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
527dd3abc4eSYOSHIFUJI Hideaki 		}
5281da177e4SLinus Torvalds 	}
5291da177e4SLinus Torvalds 
530dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
5311da177e4SLinus Torvalds 		if (local)
5321da177e4SLinus Torvalds 			return local;
5331da177e4SLinus Torvalds 
534d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
5358ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
5361da177e4SLinus Torvalds 	}
537dd3abc4eSYOSHIFUJI Hideaki out:
5381da177e4SLinus Torvalds 	return rt;
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
54127097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
542c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
543c2f17e82SHannes Frederic Sowa 	struct work_struct work;
544c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
545c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
546c2f17e82SHannes Frederic Sowa };
547c2f17e82SHannes Frederic Sowa 
548c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
549c2f17e82SHannes Frederic Sowa {
550c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
551c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
552c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
553c2f17e82SHannes Frederic Sowa 
554c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
555adc176c5SErik Nordmark 	ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0);
556c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
557662f5533SMichael Büsch 	kfree(work);
558c2f17e82SHannes Frederic Sowa }
559c2f17e82SHannes Frederic Sowa 
56027097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
56127097255SYOSHIFUJI Hideaki {
562990edb42SMartin KaFai Lau 	struct __rt6_probe_work *work;
563f2c31e32SEric Dumazet 	struct neighbour *neigh;
56427097255SYOSHIFUJI Hideaki 	/*
56527097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
56627097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
56727097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
56827097255SYOSHIFUJI Hideaki 	 *
56927097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
57027097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
57127097255SYOSHIFUJI Hideaki 	 */
5722152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
573fdd6681dSAmerigo Wang 		return;
5742152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
5752152caeaSYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
5762152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
5778d6c31bfSMartin KaFai Lau 		if (neigh->nud_state & NUD_VALID)
5788d6c31bfSMartin KaFai Lau 			goto out;
5798d6c31bfSMartin KaFai Lau 
580990edb42SMartin KaFai Lau 		work = NULL;
5812152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
582990edb42SMartin KaFai Lau 		if (!(neigh->nud_state & NUD_VALID) &&
583990edb42SMartin KaFai Lau 		    time_after(jiffies,
584990edb42SMartin KaFai Lau 			       neigh->updated +
585990edb42SMartin KaFai Lau 			       rt->rt6i_idev->cnf.rtr_probe_interval)) {
586c2f17e82SHannes Frederic Sowa 			work = kmalloc(sizeof(*work), GFP_ATOMIC);
587990edb42SMartin KaFai Lau 			if (work)
5887e980569SJiri Benc 				__neigh_set_probe_once(neigh);
589990edb42SMartin KaFai Lau 		}
590c2f17e82SHannes Frederic Sowa 		write_unlock(&neigh->lock);
591990edb42SMartin KaFai Lau 	} else {
592990edb42SMartin KaFai Lau 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
593990edb42SMartin KaFai Lau 	}
594c2f17e82SHannes Frederic Sowa 
595c2f17e82SHannes Frederic Sowa 	if (work) {
596c2f17e82SHannes Frederic Sowa 		INIT_WORK(&work->work, rt6_probe_deferred);
597c2f17e82SHannes Frederic Sowa 		work->target = rt->rt6i_gateway;
598c2f17e82SHannes Frederic Sowa 		dev_hold(rt->dst.dev);
599c2f17e82SHannes Frederic Sowa 		work->dev = rt->dst.dev;
600c2f17e82SHannes Frederic Sowa 		schedule_work(&work->work);
601c2f17e82SHannes Frederic Sowa 	}
602990edb42SMartin KaFai Lau 
6038d6c31bfSMartin KaFai Lau out:
6042152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
605f2c31e32SEric Dumazet }
60627097255SYOSHIFUJI Hideaki #else
60727097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
60827097255SYOSHIFUJI Hideaki {
60927097255SYOSHIFUJI Hideaki }
61027097255SYOSHIFUJI Hideaki #endif
61127097255SYOSHIFUJI Hideaki 
6121da177e4SLinus Torvalds /*
613554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
6141da177e4SLinus Torvalds  */
615b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
6161da177e4SLinus Torvalds {
617d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
618161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
619554cfb7eSYOSHIFUJI Hideaki 		return 2;
620161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
621161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
622161980f4SDavid S. Miller 		return 1;
623554cfb7eSYOSHIFUJI Hideaki 	return 0;
6241da177e4SLinus Torvalds }
6251da177e4SLinus Torvalds 
626afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
6271da177e4SLinus Torvalds {
628f2c31e32SEric Dumazet 	struct neighbour *neigh;
629afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
630f2c31e32SEric Dumazet 
6314d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
6324d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
633afc154e9SHannes Frederic Sowa 		return RT6_NUD_SUCCEED;
634145a3621SYOSHIFUJI Hideaki / 吉藤英明 
635145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
636145a3621SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
637145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
638145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
639554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
640afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
641398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
642a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
643afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
6447e980569SJiri Benc 		else
6457e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
646398bcbebSYOSHIFUJI Hideaki #endif
647145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
648afc154e9SHannes Frederic Sowa 	} else {
649afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
6507e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
651a5a81f0bSPaul Marks 	}
652145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
653145a3621SYOSHIFUJI Hideaki / 吉藤英明 
654a5a81f0bSPaul Marks 	return ret;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
657554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
658554cfb7eSYOSHIFUJI Hideaki 			   int strict)
659554cfb7eSYOSHIFUJI Hideaki {
660a5a81f0bSPaul Marks 	int m;
6614d0c5911SYOSHIFUJI Hideaki 
6624d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
66377d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
664afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
665ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
666ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
667ebacaaa0SYOSHIFUJI Hideaki #endif
668afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE) {
669afc154e9SHannes Frederic Sowa 		int n = rt6_check_neigh(rt);
670afc154e9SHannes Frederic Sowa 		if (n < 0)
671afc154e9SHannes Frederic Sowa 			return n;
672afc154e9SHannes Frederic Sowa 	}
673554cfb7eSYOSHIFUJI Hideaki 	return m;
674554cfb7eSYOSHIFUJI Hideaki }
675554cfb7eSYOSHIFUJI Hideaki 
676f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
677afc154e9SHannes Frederic Sowa 				   int *mpri, struct rt6_info *match,
678afc154e9SHannes Frederic Sowa 				   bool *do_rr)
679554cfb7eSYOSHIFUJI Hideaki {
680554cfb7eSYOSHIFUJI Hideaki 	int m;
681afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
68235103d11SAndy Gospodarek 	struct inet6_dev *idev = rt->rt6i_idev;
68335103d11SAndy Gospodarek 	struct net_device *dev = rt->dst.dev;
68435103d11SAndy Gospodarek 
68535103d11SAndy Gospodarek 	if (dev && !netif_carrier_ok(dev) &&
686d5d32e4bSDavid Ahern 	    idev->cnf.ignore_routes_with_linkdown &&
687d5d32e4bSDavid Ahern 	    !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE))
68835103d11SAndy Gospodarek 		goto out;
689554cfb7eSYOSHIFUJI Hideaki 
690554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
691f11e6659SDavid S. Miller 		goto out;
692554cfb7eSYOSHIFUJI Hideaki 
693554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
6947e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
695afc154e9SHannes Frederic Sowa 		match_do_rr = true;
696afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
6977e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
698f11e6659SDavid S. Miller 		goto out;
6991da177e4SLinus Torvalds 	}
700f11e6659SDavid S. Miller 
701afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
702afc154e9SHannes Frederic Sowa 		rt6_probe(rt);
703afc154e9SHannes Frederic Sowa 
7047e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
705afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
706afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
707afc154e9SHannes Frederic Sowa 		*mpri = m;
708afc154e9SHannes Frederic Sowa 		match = rt;
709afc154e9SHannes Frederic Sowa 	}
710f11e6659SDavid S. Miller out:
711f11e6659SDavid S. Miller 	return match;
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
714f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
715f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
716afc154e9SHannes Frederic Sowa 				     u32 metric, int oif, int strict,
717afc154e9SHannes Frederic Sowa 				     bool *do_rr)
718f11e6659SDavid S. Miller {
7199fbdcfafSSteffen Klassert 	struct rt6_info *rt, *match, *cont;
720f11e6659SDavid S. Miller 	int mpri = -1;
721f11e6659SDavid S. Miller 
722f11e6659SDavid S. Miller 	match = NULL;
7239fbdcfafSSteffen Klassert 	cont = NULL;
7249fbdcfafSSteffen Klassert 	for (rt = rr_head; rt; rt = rt->dst.rt6_next) {
7259fbdcfafSSteffen Klassert 		if (rt->rt6i_metric != metric) {
7269fbdcfafSSteffen Klassert 			cont = rt;
7279fbdcfafSSteffen Klassert 			break;
7289fbdcfafSSteffen Klassert 		}
7299fbdcfafSSteffen Klassert 
730afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
7319fbdcfafSSteffen Klassert 	}
7329fbdcfafSSteffen Klassert 
7339fbdcfafSSteffen Klassert 	for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) {
7349fbdcfafSSteffen Klassert 		if (rt->rt6i_metric != metric) {
7359fbdcfafSSteffen Klassert 			cont = rt;
7369fbdcfafSSteffen Klassert 			break;
7379fbdcfafSSteffen Klassert 		}
7389fbdcfafSSteffen Klassert 
7399fbdcfafSSteffen Klassert 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
7409fbdcfafSSteffen Klassert 	}
7419fbdcfafSSteffen Klassert 
7429fbdcfafSSteffen Klassert 	if (match || !cont)
7439fbdcfafSSteffen Klassert 		return match;
7449fbdcfafSSteffen Klassert 
7459fbdcfafSSteffen Klassert 	for (rt = cont; rt; rt = rt->dst.rt6_next)
746afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
747f11e6659SDavid S. Miller 
748f11e6659SDavid S. Miller 	return match;
749f11e6659SDavid S. Miller }
750f11e6659SDavid S. Miller 
751f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
752f11e6659SDavid S. Miller {
753f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
7548ed67789SDaniel Lezcano 	struct net *net;
755afc154e9SHannes Frederic Sowa 	bool do_rr = false;
756f11e6659SDavid S. Miller 
757f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
758f11e6659SDavid S. Miller 	if (!rt0)
759f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
760f11e6659SDavid S. Miller 
761afc154e9SHannes Frederic Sowa 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
762afc154e9SHannes Frederic Sowa 			     &do_rr);
763f11e6659SDavid S. Miller 
764afc154e9SHannes Frederic Sowa 	if (do_rr) {
765d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
766f11e6659SDavid S. Miller 
767554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
768f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
769f11e6659SDavid S. Miller 			next = fn->leaf;
770f11e6659SDavid S. Miller 
771f11e6659SDavid S. Miller 		if (next != rt0)
772f11e6659SDavid S. Miller 			fn->rr_ptr = next;
773554cfb7eSYOSHIFUJI Hideaki 	}
774554cfb7eSYOSHIFUJI Hideaki 
775d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
776a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds 
7798b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt)
7808b9df265SMartin KaFai Lau {
7818b9df265SMartin KaFai Lau 	return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
7828b9df265SMartin KaFai Lau }
7838b9df265SMartin KaFai Lau 
78470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
78570ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
786b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
78770ceb4f5SYOSHIFUJI Hideaki {
788c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
78970ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
79070ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
79170ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
7924bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
79370ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
79470ceb4f5SYOSHIFUJI Hideaki 
79570ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
79670ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
79770ceb4f5SYOSHIFUJI Hideaki 	}
79870ceb4f5SYOSHIFUJI Hideaki 
79970ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
80070ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
80170ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
80270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
80370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
80470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
80570ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
80670ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
80770ceb4f5SYOSHIFUJI Hideaki 		}
80870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
80970ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
81070ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
81170ceb4f5SYOSHIFUJI Hideaki 		}
81270ceb4f5SYOSHIFUJI Hideaki 	}
81370ceb4f5SYOSHIFUJI Hideaki 
81470ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
81570ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
8163933fc95SJens Rosenboom 		return -EINVAL;
81770ceb4f5SYOSHIFUJI Hideaki 
8184bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
81970ceb4f5SYOSHIFUJI Hideaki 
82070ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
82170ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
82270ceb4f5SYOSHIFUJI Hideaki 	else {
82370ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
82470ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
82570ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
82670ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
82770ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
82870ceb4f5SYOSHIFUJI Hideaki 	}
82970ceb4f5SYOSHIFUJI Hideaki 
830f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
831f104a567SDuan Jiong 		rt = rt6_get_dflt_router(gwaddr, dev);
832f104a567SDuan Jiong 	else
833f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
834830218c1SDavid Ahern 					gwaddr, dev);
83570ceb4f5SYOSHIFUJI Hideaki 
83670ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
837e0a1ad73SThomas Graf 		ip6_del_rt(rt);
83870ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
83970ceb4f5SYOSHIFUJI Hideaki 	}
84070ceb4f5SYOSHIFUJI Hideaki 
84170ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
842830218c1SDavid Ahern 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr,
843830218c1SDavid Ahern 					dev, pref);
84470ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
84570ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
84670ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
84770ceb4f5SYOSHIFUJI Hideaki 
84870ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
8491716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
8501716a961SGao feng 			rt6_clean_expires(rt);
8511716a961SGao feng 		else
8521716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
8531716a961SGao feng 
85494e187c0SAmerigo Wang 		ip6_rt_put(rt);
85570ceb4f5SYOSHIFUJI Hideaki 	}
85670ceb4f5SYOSHIFUJI Hideaki 	return 0;
85770ceb4f5SYOSHIFUJI Hideaki }
85870ceb4f5SYOSHIFUJI Hideaki #endif
85970ceb4f5SYOSHIFUJI Hideaki 
860a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
861a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
862a3c00e46SMartin KaFai Lau {
863a3c00e46SMartin KaFai Lau 	struct fib6_node *pn;
864a3c00e46SMartin KaFai Lau 	while (1) {
865a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
866a3c00e46SMartin KaFai Lau 			return NULL;
867a3c00e46SMartin KaFai Lau 		pn = fn->parent;
868a3c00e46SMartin KaFai Lau 		if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn)
869a3c00e46SMartin KaFai Lau 			fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr);
870a3c00e46SMartin KaFai Lau 		else
871a3c00e46SMartin KaFai Lau 			fn = pn;
872a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
873a3c00e46SMartin KaFai Lau 			return fn;
874a3c00e46SMartin KaFai Lau 	}
875a3c00e46SMartin KaFai Lau }
876c71099acSThomas Graf 
8778ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
8788ed67789SDaniel Lezcano 					     struct fib6_table *table,
8794c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
8801da177e4SLinus Torvalds {
881*2b760fcfSWei Wang 	struct rt6_info *rt, *rt_cache;
8821da177e4SLinus Torvalds 	struct fib6_node *fn;
8831da177e4SLinus Torvalds 
884c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8854c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
886c71099acSThomas Graf restart:
887c71099acSThomas Graf 	rt = fn->leaf;
8884c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
88951ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
89052bd4c0cSNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
891a3c00e46SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
892a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
893a3c00e46SMartin KaFai Lau 		if (fn)
894a3c00e46SMartin KaFai Lau 			goto restart;
895a3c00e46SMartin KaFai Lau 	}
896*2b760fcfSWei Wang 	/* Search through exception table */
897*2b760fcfSWei Wang 	rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr);
898*2b760fcfSWei Wang 	if (rt_cache)
899*2b760fcfSWei Wang 		rt = rt_cache;
900*2b760fcfSWei Wang 
901d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
902c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
903b811580dSDavid Ahern 
904b811580dSDavid Ahern 	trace_fib6_table_lookup(net, rt, table->tb6_id, fl6);
905b811580dSDavid Ahern 
9061da177e4SLinus Torvalds 	return rt;
907c71099acSThomas Graf 
908c71099acSThomas Graf }
909c71099acSThomas Graf 
910ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
911ea6e574eSFlorian Westphal 				    int flags)
912ea6e574eSFlorian Westphal {
913ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
914ea6e574eSFlorian Westphal }
915ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
916ea6e574eSFlorian Westphal 
9179acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
9189acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
919c71099acSThomas Graf {
9204c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9214c9483b2SDavid S. Miller 		.flowi6_oif = oif,
9224c9483b2SDavid S. Miller 		.daddr = *daddr,
923c71099acSThomas Graf 	};
924c71099acSThomas Graf 	struct dst_entry *dst;
92577d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
926c71099acSThomas Graf 
927adaa70bbSThomas Graf 	if (saddr) {
9284c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
929adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
930adaa70bbSThomas Graf 	}
931adaa70bbSThomas Graf 
9324c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
933c71099acSThomas Graf 	if (dst->error == 0)
934c71099acSThomas Graf 		return (struct rt6_info *) dst;
935c71099acSThomas Graf 
936c71099acSThomas Graf 	dst_release(dst);
937c71099acSThomas Graf 
9381da177e4SLinus Torvalds 	return NULL;
9391da177e4SLinus Torvalds }
9407159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
9417159039aSYOSHIFUJI Hideaki 
942c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
9431cfb71eeSWei Wang  * It takes new route entry, the addition fails by any reason the
9441cfb71eeSWei Wang  * route is released.
9451cfb71eeSWei Wang  * Caller must hold dst before calling it.
9461da177e4SLinus Torvalds  */
9471da177e4SLinus Torvalds 
948e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
949333c4301SDavid Ahern 			struct mx6_config *mxc,
950333c4301SDavid Ahern 			struct netlink_ext_ack *extack)
9511da177e4SLinus Torvalds {
9521da177e4SLinus Torvalds 	int err;
953c71099acSThomas Graf 	struct fib6_table *table;
9541da177e4SLinus Torvalds 
955c71099acSThomas Graf 	table = rt->rt6i_table;
956c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
957333c4301SDavid Ahern 	err = fib6_add(&table->tb6_root, rt, info, mxc, extack);
958c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	return err;
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
96340e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
96440e22e8fSThomas Graf {
965e715b6d3SFlorian Westphal 	struct nl_info info = {	.nl_net = dev_net(rt->dst.dev), };
966e715b6d3SFlorian Westphal 	struct mx6_config mxc = { .mx = NULL, };
967e715b6d3SFlorian Westphal 
9681cfb71eeSWei Wang 	/* Hold dst to account for the reference from the fib6 tree */
9691cfb71eeSWei Wang 	dst_hold(&rt->dst);
970333c4301SDavid Ahern 	return __ip6_ins_rt(rt, &info, &mxc, NULL);
97140e22e8fSThomas Graf }
97240e22e8fSThomas Graf 
9734832c30dSDavid Ahern /* called with rcu_lock held */
9744832c30dSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt)
9754832c30dSDavid Ahern {
9764832c30dSDavid Ahern 	struct net_device *dev = rt->dst.dev;
9774832c30dSDavid Ahern 
9784832c30dSDavid Ahern 	if (rt->rt6i_flags & RTF_LOCAL) {
9794832c30dSDavid Ahern 		/* for copies of local routes, dst->dev needs to be the
9804832c30dSDavid Ahern 		 * device if it is a master device, the master device if
9814832c30dSDavid Ahern 		 * device is enslaved, and the loopback as the default
9824832c30dSDavid Ahern 		 */
9834832c30dSDavid Ahern 		if (netif_is_l3_slave(dev) &&
9844832c30dSDavid Ahern 		    !rt6_need_strict(&rt->rt6i_dst.addr))
9854832c30dSDavid Ahern 			dev = l3mdev_master_dev_rcu(dev);
9864832c30dSDavid Ahern 		else if (!netif_is_l3_master(dev))
9874832c30dSDavid Ahern 			dev = dev_net(dev)->loopback_dev;
9884832c30dSDavid Ahern 		/* last case is netif_is_l3_master(dev) is true in which
9894832c30dSDavid Ahern 		 * case we want dev returned to be dev
9904832c30dSDavid Ahern 		 */
9914832c30dSDavid Ahern 	}
9924832c30dSDavid Ahern 
9934832c30dSDavid Ahern 	return dev;
9944832c30dSDavid Ahern }
9954832c30dSDavid Ahern 
9968b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort,
99721efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
998b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
9991da177e4SLinus Torvalds {
10004832c30dSDavid Ahern 	struct net_device *dev;
10011da177e4SLinus Torvalds 	struct rt6_info *rt;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	/*
10041da177e4SLinus Torvalds 	 *	Clone the route.
10051da177e4SLinus Torvalds 	 */
10061da177e4SLinus Torvalds 
1007d52d3997SMartin KaFai Lau 	if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
100883a09abdSMartin KaFai Lau 		ort = (struct rt6_info *)ort->dst.from;
10091da177e4SLinus Torvalds 
10104832c30dSDavid Ahern 	rcu_read_lock();
10114832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(ort);
10124832c30dSDavid Ahern 	rt = __ip6_dst_alloc(dev_net(dev), dev, 0);
10134832c30dSDavid Ahern 	rcu_read_unlock();
101483a09abdSMartin KaFai Lau 	if (!rt)
101583a09abdSMartin KaFai Lau 		return NULL;
101683a09abdSMartin KaFai Lau 
101783a09abdSMartin KaFai Lau 	ip6_rt_copy_init(rt, ort);
10188b9df265SMartin KaFai Lau 	rt->rt6i_flags |= RTF_CACHE;
101983a09abdSMartin KaFai Lau 	rt->rt6i_metric = 0;
102083a09abdSMartin KaFai Lau 	rt->dst.flags |= DST_HOST;
102183a09abdSMartin KaFai Lau 	rt->rt6i_dst.addr = *daddr;
102283a09abdSMartin KaFai Lau 	rt->rt6i_dst.plen = 128;
10238b9df265SMartin KaFai Lau 
10248b9df265SMartin KaFai Lau 	if (!rt6_is_gw_or_nonexthop(ort)) {
1025bb3c3686SDavid S. Miller 		if (ort->rt6i_dst.plen != 128 &&
102621efcfa0SEric Dumazet 		    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
102758c4fb86SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_ANYCAST;
10281da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
10291da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
10304e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
10311da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
10321da177e4SLinus Torvalds 		}
10331da177e4SLinus Torvalds #endif
103495a9a5baSYOSHIFUJI Hideaki 	}
103595a9a5baSYOSHIFUJI Hideaki 
1036299d9939SYOSHIFUJI Hideaki 	return rt;
1037299d9939SYOSHIFUJI Hideaki }
1038299d9939SYOSHIFUJI Hideaki 
1039d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt)
1040d52d3997SMartin KaFai Lau {
10414832c30dSDavid Ahern 	struct net_device *dev;
1042d52d3997SMartin KaFai Lau 	struct rt6_info *pcpu_rt;
1043d52d3997SMartin KaFai Lau 
10444832c30dSDavid Ahern 	rcu_read_lock();
10454832c30dSDavid Ahern 	dev = ip6_rt_get_dev_rcu(rt);
10464832c30dSDavid Ahern 	pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags);
10474832c30dSDavid Ahern 	rcu_read_unlock();
1048d52d3997SMartin KaFai Lau 	if (!pcpu_rt)
1049d52d3997SMartin KaFai Lau 		return NULL;
1050d52d3997SMartin KaFai Lau 	ip6_rt_copy_init(pcpu_rt, rt);
1051d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_protocol = rt->rt6i_protocol;
1052d52d3997SMartin KaFai Lau 	pcpu_rt->rt6i_flags |= RTF_PCPU;
1053d52d3997SMartin KaFai Lau 	return pcpu_rt;
1054d52d3997SMartin KaFai Lau }
1055d52d3997SMartin KaFai Lau 
1056d52d3997SMartin KaFai Lau /* It should be called with read_lock_bh(&tb6_lock) acquired */
1057d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt)
1058d52d3997SMartin KaFai Lau {
1059a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, **p;
1060d52d3997SMartin KaFai Lau 
1061d52d3997SMartin KaFai Lau 	p = this_cpu_ptr(rt->rt6i_pcpu);
1062d52d3997SMartin KaFai Lau 	pcpu_rt = *p;
1063d52d3997SMartin KaFai Lau 
1064a73e4195SMartin KaFai Lau 	if (pcpu_rt) {
1065a73e4195SMartin KaFai Lau 		dst_hold(&pcpu_rt->dst);
1066a73e4195SMartin KaFai Lau 		rt6_dst_from_metrics_check(pcpu_rt);
1067a73e4195SMartin KaFai Lau 	}
1068a73e4195SMartin KaFai Lau 	return pcpu_rt;
1069a73e4195SMartin KaFai Lau }
1070a73e4195SMartin KaFai Lau 
1071a73e4195SMartin KaFai Lau static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt)
1072a73e4195SMartin KaFai Lau {
10739c7370a1SMartin KaFai Lau 	struct fib6_table *table = rt->rt6i_table;
1074a73e4195SMartin KaFai Lau 	struct rt6_info *pcpu_rt, *prev, **p;
1075d52d3997SMartin KaFai Lau 
1076d52d3997SMartin KaFai Lau 	pcpu_rt = ip6_rt_pcpu_alloc(rt);
1077d52d3997SMartin KaFai Lau 	if (!pcpu_rt) {
1078d52d3997SMartin KaFai Lau 		struct net *net = dev_net(rt->dst.dev);
1079d52d3997SMartin KaFai Lau 
10809c7370a1SMartin KaFai Lau 		dst_hold(&net->ipv6.ip6_null_entry->dst);
10819c7370a1SMartin KaFai Lau 		return net->ipv6.ip6_null_entry;
1082d52d3997SMartin KaFai Lau 	}
1083d52d3997SMartin KaFai Lau 
10849c7370a1SMartin KaFai Lau 	read_lock_bh(&table->tb6_lock);
10859c7370a1SMartin KaFai Lau 	if (rt->rt6i_pcpu) {
1086a73e4195SMartin KaFai Lau 		p = this_cpu_ptr(rt->rt6i_pcpu);
1087d52d3997SMartin KaFai Lau 		prev = cmpxchg(p, NULL, pcpu_rt);
1088d52d3997SMartin KaFai Lau 		if (prev) {
1089d52d3997SMartin KaFai Lau 			/* If someone did it before us, return prev instead */
1090587fea74SWei Wang 			dst_release_immediate(&pcpu_rt->dst);
1091d52d3997SMartin KaFai Lau 			pcpu_rt = prev;
1092d52d3997SMartin KaFai Lau 		}
10939c7370a1SMartin KaFai Lau 	} else {
10949c7370a1SMartin KaFai Lau 		/* rt has been removed from the fib6 tree
10959c7370a1SMartin KaFai Lau 		 * before we have a chance to acquire the read_lock.
10969c7370a1SMartin KaFai Lau 		 * In this case, don't brother to create a pcpu rt
10979c7370a1SMartin KaFai Lau 		 * since rt is going away anyway.  The next
10989c7370a1SMartin KaFai Lau 		 * dst_check() will trigger a re-lookup.
10999c7370a1SMartin KaFai Lau 		 */
1100587fea74SWei Wang 		dst_release_immediate(&pcpu_rt->dst);
11019c7370a1SMartin KaFai Lau 		pcpu_rt = rt;
11029c7370a1SMartin KaFai Lau 	}
1103d52d3997SMartin KaFai Lau 	dst_hold(&pcpu_rt->dst);
1104d52d3997SMartin KaFai Lau 	rt6_dst_from_metrics_check(pcpu_rt);
11059c7370a1SMartin KaFai Lau 	read_unlock_bh(&table->tb6_lock);
1106d52d3997SMartin KaFai Lau 	return pcpu_rt;
1107d52d3997SMartin KaFai Lau }
1108d52d3997SMartin KaFai Lau 
110935732d01SWei Wang /* exception hash table implementation
111035732d01SWei Wang  */
111135732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock);
111235732d01SWei Wang 
111335732d01SWei Wang /* Remove rt6_ex from hash table and free the memory
111435732d01SWei Wang  * Caller must hold rt6_exception_lock
111535732d01SWei Wang  */
111635732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket,
111735732d01SWei Wang 				 struct rt6_exception *rt6_ex)
111835732d01SWei Wang {
111935732d01SWei Wang 	if (!bucket || !rt6_ex)
112035732d01SWei Wang 		return;
112135732d01SWei Wang 	rt6_ex->rt6i->rt6i_node = NULL;
112235732d01SWei Wang 	hlist_del_rcu(&rt6_ex->hlist);
112335732d01SWei Wang 	rt6_release(rt6_ex->rt6i);
112435732d01SWei Wang 	kfree_rcu(rt6_ex, rcu);
112535732d01SWei Wang 	WARN_ON_ONCE(!bucket->depth);
112635732d01SWei Wang 	bucket->depth--;
112735732d01SWei Wang }
112835732d01SWei Wang 
112935732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory
113035732d01SWei Wang  * Caller must hold rt6_exception_lock
113135732d01SWei Wang  */
113235732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket)
113335732d01SWei Wang {
113435732d01SWei Wang 	struct rt6_exception *rt6_ex, *oldest = NULL;
113535732d01SWei Wang 
113635732d01SWei Wang 	if (!bucket)
113735732d01SWei Wang 		return;
113835732d01SWei Wang 
113935732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
114035732d01SWei Wang 		if (!oldest || time_before(rt6_ex->stamp, oldest->stamp))
114135732d01SWei Wang 			oldest = rt6_ex;
114235732d01SWei Wang 	}
114335732d01SWei Wang 	rt6_remove_exception(bucket, oldest);
114435732d01SWei Wang }
114535732d01SWei Wang 
114635732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst,
114735732d01SWei Wang 			      const struct in6_addr *src)
114835732d01SWei Wang {
114935732d01SWei Wang 	static u32 seed __read_mostly;
115035732d01SWei Wang 	u32 val;
115135732d01SWei Wang 
115235732d01SWei Wang 	net_get_random_once(&seed, sizeof(seed));
115335732d01SWei Wang 	val = jhash(dst, sizeof(*dst), seed);
115435732d01SWei Wang 
115535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
115635732d01SWei Wang 	if (src)
115735732d01SWei Wang 		val = jhash(src, sizeof(*src), val);
115835732d01SWei Wang #endif
115935732d01SWei Wang 	return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT);
116035732d01SWei Wang }
116135732d01SWei Wang 
116235732d01SWei Wang /* Helper function to find the cached rt in the hash table
116335732d01SWei Wang  * and update bucket pointer to point to the bucket for this
116435732d01SWei Wang  * (daddr, saddr) pair
116535732d01SWei Wang  * Caller must hold rt6_exception_lock
116635732d01SWei Wang  */
116735732d01SWei Wang static struct rt6_exception *
116835732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket,
116935732d01SWei Wang 			      const struct in6_addr *daddr,
117035732d01SWei Wang 			      const struct in6_addr *saddr)
117135732d01SWei Wang {
117235732d01SWei Wang 	struct rt6_exception *rt6_ex;
117335732d01SWei Wang 	u32 hval;
117435732d01SWei Wang 
117535732d01SWei Wang 	if (!(*bucket) || !daddr)
117635732d01SWei Wang 		return NULL;
117735732d01SWei Wang 
117835732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
117935732d01SWei Wang 	*bucket += hval;
118035732d01SWei Wang 
118135732d01SWei Wang 	hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) {
118235732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
118335732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
118435732d01SWei Wang 
118535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
118635732d01SWei Wang 		if (matched && saddr)
118735732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
118835732d01SWei Wang #endif
118935732d01SWei Wang 		if (matched)
119035732d01SWei Wang 			return rt6_ex;
119135732d01SWei Wang 	}
119235732d01SWei Wang 	return NULL;
119335732d01SWei Wang }
119435732d01SWei Wang 
119535732d01SWei Wang /* Helper function to find the cached rt in the hash table
119635732d01SWei Wang  * and update bucket pointer to point to the bucket for this
119735732d01SWei Wang  * (daddr, saddr) pair
119835732d01SWei Wang  * Caller must hold rcu_read_lock()
119935732d01SWei Wang  */
120035732d01SWei Wang static struct rt6_exception *
120135732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket,
120235732d01SWei Wang 			 const struct in6_addr *daddr,
120335732d01SWei Wang 			 const struct in6_addr *saddr)
120435732d01SWei Wang {
120535732d01SWei Wang 	struct rt6_exception *rt6_ex;
120635732d01SWei Wang 	u32 hval;
120735732d01SWei Wang 
120835732d01SWei Wang 	WARN_ON_ONCE(!rcu_read_lock_held());
120935732d01SWei Wang 
121035732d01SWei Wang 	if (!(*bucket) || !daddr)
121135732d01SWei Wang 		return NULL;
121235732d01SWei Wang 
121335732d01SWei Wang 	hval = rt6_exception_hash(daddr, saddr);
121435732d01SWei Wang 	*bucket += hval;
121535732d01SWei Wang 
121635732d01SWei Wang 	hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) {
121735732d01SWei Wang 		struct rt6_info *rt6 = rt6_ex->rt6i;
121835732d01SWei Wang 		bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr);
121935732d01SWei Wang 
122035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
122135732d01SWei Wang 		if (matched && saddr)
122235732d01SWei Wang 			matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr);
122335732d01SWei Wang #endif
122435732d01SWei Wang 		if (matched)
122535732d01SWei Wang 			return rt6_ex;
122635732d01SWei Wang 	}
122735732d01SWei Wang 	return NULL;
122835732d01SWei Wang }
122935732d01SWei Wang 
123035732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt,
123135732d01SWei Wang 				struct rt6_info *ort)
123235732d01SWei Wang {
123335732d01SWei Wang 	struct rt6_exception_bucket *bucket;
123435732d01SWei Wang 	struct in6_addr *src_key = NULL;
123535732d01SWei Wang 	struct rt6_exception *rt6_ex;
123635732d01SWei Wang 	int err = 0;
123735732d01SWei Wang 
123835732d01SWei Wang 	/* ort can't be a cache or pcpu route */
123935732d01SWei Wang 	if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU))
124035732d01SWei Wang 		ort = (struct rt6_info *)ort->dst.from;
124135732d01SWei Wang 	WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU));
124235732d01SWei Wang 
124335732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
124435732d01SWei Wang 
124535732d01SWei Wang 	if (ort->exception_bucket_flushed) {
124635732d01SWei Wang 		err = -EINVAL;
124735732d01SWei Wang 		goto out;
124835732d01SWei Wang 	}
124935732d01SWei Wang 
125035732d01SWei Wang 	bucket = rcu_dereference_protected(ort->rt6i_exception_bucket,
125135732d01SWei Wang 					lockdep_is_held(&rt6_exception_lock));
125235732d01SWei Wang 	if (!bucket) {
125335732d01SWei Wang 		bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket),
125435732d01SWei Wang 				 GFP_ATOMIC);
125535732d01SWei Wang 		if (!bucket) {
125635732d01SWei Wang 			err = -ENOMEM;
125735732d01SWei Wang 			goto out;
125835732d01SWei Wang 		}
125935732d01SWei Wang 		rcu_assign_pointer(ort->rt6i_exception_bucket, bucket);
126035732d01SWei Wang 	}
126135732d01SWei Wang 
126235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
126335732d01SWei Wang 	/* rt6i_src.plen != 0 indicates ort is in subtree
126435732d01SWei Wang 	 * and exception table is indexed by a hash of
126535732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
126635732d01SWei Wang 	 * Otherwise, the exception table is indexed by
126735732d01SWei Wang 	 * a hash of only rt6i_dst.
126835732d01SWei Wang 	 */
126935732d01SWei Wang 	if (ort->rt6i_src.plen)
127035732d01SWei Wang 		src_key = &nrt->rt6i_src.addr;
127135732d01SWei Wang #endif
127260006a48SWei Wang 
127360006a48SWei Wang 	/* Update rt6i_prefsrc as it could be changed
127460006a48SWei Wang 	 * in rt6_remove_prefsrc()
127560006a48SWei Wang 	 */
127660006a48SWei Wang 	nrt->rt6i_prefsrc = ort->rt6i_prefsrc;
1277f5bbe7eeSWei Wang 	/* rt6_mtu_change() might lower mtu on ort.
1278f5bbe7eeSWei Wang 	 * Only insert this exception route if its mtu
1279f5bbe7eeSWei Wang 	 * is less than ort's mtu value.
1280f5bbe7eeSWei Wang 	 */
1281f5bbe7eeSWei Wang 	if (nrt->rt6i_pmtu >= dst_mtu(&ort->dst)) {
1282f5bbe7eeSWei Wang 		err = -EINVAL;
1283f5bbe7eeSWei Wang 		goto out;
1284f5bbe7eeSWei Wang 	}
128560006a48SWei Wang 
128635732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr,
128735732d01SWei Wang 					       src_key);
128835732d01SWei Wang 	if (rt6_ex)
128935732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
129035732d01SWei Wang 
129135732d01SWei Wang 	rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC);
129235732d01SWei Wang 	if (!rt6_ex) {
129335732d01SWei Wang 		err = -ENOMEM;
129435732d01SWei Wang 		goto out;
129535732d01SWei Wang 	}
129635732d01SWei Wang 	rt6_ex->rt6i = nrt;
129735732d01SWei Wang 	rt6_ex->stamp = jiffies;
129835732d01SWei Wang 	atomic_inc(&nrt->rt6i_ref);
129935732d01SWei Wang 	nrt->rt6i_node = ort->rt6i_node;
130035732d01SWei Wang 	hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain);
130135732d01SWei Wang 	bucket->depth++;
130235732d01SWei Wang 
130335732d01SWei Wang 	if (bucket->depth > FIB6_MAX_DEPTH)
130435732d01SWei Wang 		rt6_exception_remove_oldest(bucket);
130535732d01SWei Wang 
130635732d01SWei Wang out:
130735732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
130835732d01SWei Wang 
130935732d01SWei Wang 	/* Update fn->fn_sernum to invalidate all cached dst */
131035732d01SWei Wang 	if (!err)
131135732d01SWei Wang 		fib6_update_sernum(ort);
131235732d01SWei Wang 
131335732d01SWei Wang 	return err;
131435732d01SWei Wang }
131535732d01SWei Wang 
131635732d01SWei Wang void rt6_flush_exceptions(struct rt6_info *rt)
131735732d01SWei Wang {
131835732d01SWei Wang 	struct rt6_exception_bucket *bucket;
131935732d01SWei Wang 	struct rt6_exception *rt6_ex;
132035732d01SWei Wang 	struct hlist_node *tmp;
132135732d01SWei Wang 	int i;
132235732d01SWei Wang 
132335732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
132435732d01SWei Wang 	/* Prevent rt6_insert_exception() to recreate the bucket list */
132535732d01SWei Wang 	rt->exception_bucket_flushed = 1;
132635732d01SWei Wang 
132735732d01SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
132835732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
132935732d01SWei Wang 	if (!bucket)
133035732d01SWei Wang 		goto out;
133135732d01SWei Wang 
133235732d01SWei Wang 	for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
133335732d01SWei Wang 		hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist)
133435732d01SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
133535732d01SWei Wang 		WARN_ON_ONCE(bucket->depth);
133635732d01SWei Wang 		bucket++;
133735732d01SWei Wang 	}
133835732d01SWei Wang 
133935732d01SWei Wang out:
134035732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
134135732d01SWei Wang }
134235732d01SWei Wang 
134335732d01SWei Wang /* Find cached rt in the hash table inside passed in rt
134435732d01SWei Wang  * Caller has to hold rcu_read_lock()
134535732d01SWei Wang  */
134635732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt,
134735732d01SWei Wang 					   struct in6_addr *daddr,
134835732d01SWei Wang 					   struct in6_addr *saddr)
134935732d01SWei Wang {
135035732d01SWei Wang 	struct rt6_exception_bucket *bucket;
135135732d01SWei Wang 	struct in6_addr *src_key = NULL;
135235732d01SWei Wang 	struct rt6_exception *rt6_ex;
135335732d01SWei Wang 	struct rt6_info *res = NULL;
135435732d01SWei Wang 
135535732d01SWei Wang 	bucket = rcu_dereference(rt->rt6i_exception_bucket);
135635732d01SWei Wang 
135735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
135835732d01SWei Wang 	/* rt6i_src.plen != 0 indicates rt is in subtree
135935732d01SWei Wang 	 * and exception table is indexed by a hash of
136035732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
136135732d01SWei Wang 	 * Otherwise, the exception table is indexed by
136235732d01SWei Wang 	 * a hash of only rt6i_dst.
136335732d01SWei Wang 	 */
136435732d01SWei Wang 	if (rt->rt6i_src.plen)
136535732d01SWei Wang 		src_key = saddr;
136635732d01SWei Wang #endif
136735732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key);
136835732d01SWei Wang 
136935732d01SWei Wang 	if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i))
137035732d01SWei Wang 		res = rt6_ex->rt6i;
137135732d01SWei Wang 
137235732d01SWei Wang 	return res;
137335732d01SWei Wang }
137435732d01SWei Wang 
137535732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */
137635732d01SWei Wang int rt6_remove_exception_rt(struct rt6_info *rt)
137735732d01SWei Wang {
137835732d01SWei Wang 	struct rt6_info *from = (struct rt6_info *)rt->dst.from;
137935732d01SWei Wang 	struct rt6_exception_bucket *bucket;
138035732d01SWei Wang 	struct in6_addr *src_key = NULL;
138135732d01SWei Wang 	struct rt6_exception *rt6_ex;
138235732d01SWei Wang 	int err;
138335732d01SWei Wang 
138435732d01SWei Wang 	if (!from ||
138535732d01SWei Wang 	    !(rt->rt6i_flags | RTF_CACHE))
138635732d01SWei Wang 		return -EINVAL;
138735732d01SWei Wang 
138835732d01SWei Wang 	if (!rcu_access_pointer(from->rt6i_exception_bucket))
138935732d01SWei Wang 		return -ENOENT;
139035732d01SWei Wang 
139135732d01SWei Wang 	spin_lock_bh(&rt6_exception_lock);
139235732d01SWei Wang 	bucket = rcu_dereference_protected(from->rt6i_exception_bucket,
139335732d01SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
139435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
139535732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
139635732d01SWei Wang 	 * and exception table is indexed by a hash of
139735732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
139835732d01SWei Wang 	 * Otherwise, the exception table is indexed by
139935732d01SWei Wang 	 * a hash of only rt6i_dst.
140035732d01SWei Wang 	 */
140135732d01SWei Wang 	if (from->rt6i_src.plen)
140235732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
140335732d01SWei Wang #endif
140435732d01SWei Wang 	rt6_ex = __rt6_find_exception_spinlock(&bucket,
140535732d01SWei Wang 					       &rt->rt6i_dst.addr,
140635732d01SWei Wang 					       src_key);
140735732d01SWei Wang 	if (rt6_ex) {
140835732d01SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
140935732d01SWei Wang 		err = 0;
141035732d01SWei Wang 	} else {
141135732d01SWei Wang 		err = -ENOENT;
141235732d01SWei Wang 	}
141335732d01SWei Wang 
141435732d01SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
141535732d01SWei Wang 	return err;
141635732d01SWei Wang }
141735732d01SWei Wang 
141835732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and
141935732d01SWei Wang  * refresh its stamp
142035732d01SWei Wang  */
142135732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt)
142235732d01SWei Wang {
142335732d01SWei Wang 	struct rt6_info *from = (struct rt6_info *)rt->dst.from;
142435732d01SWei Wang 	struct rt6_exception_bucket *bucket;
142535732d01SWei Wang 	struct in6_addr *src_key = NULL;
142635732d01SWei Wang 	struct rt6_exception *rt6_ex;
142735732d01SWei Wang 
142835732d01SWei Wang 	if (!from ||
142935732d01SWei Wang 	    !(rt->rt6i_flags | RTF_CACHE))
143035732d01SWei Wang 		return;
143135732d01SWei Wang 
143235732d01SWei Wang 	rcu_read_lock();
143335732d01SWei Wang 	bucket = rcu_dereference(from->rt6i_exception_bucket);
143435732d01SWei Wang 
143535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES
143635732d01SWei Wang 	/* rt6i_src.plen != 0 indicates 'from' is in subtree
143735732d01SWei Wang 	 * and exception table is indexed by a hash of
143835732d01SWei Wang 	 * both rt6i_dst and rt6i_src.
143935732d01SWei Wang 	 * Otherwise, the exception table is indexed by
144035732d01SWei Wang 	 * a hash of only rt6i_dst.
144135732d01SWei Wang 	 */
144235732d01SWei Wang 	if (from->rt6i_src.plen)
144335732d01SWei Wang 		src_key = &rt->rt6i_src.addr;
144435732d01SWei Wang #endif
144535732d01SWei Wang 	rt6_ex = __rt6_find_exception_rcu(&bucket,
144635732d01SWei Wang 					  &rt->rt6i_dst.addr,
144735732d01SWei Wang 					  src_key);
144835732d01SWei Wang 	if (rt6_ex)
144935732d01SWei Wang 		rt6_ex->stamp = jiffies;
145035732d01SWei Wang 
145135732d01SWei Wang 	rcu_read_unlock();
145235732d01SWei Wang }
145335732d01SWei Wang 
145460006a48SWei Wang static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt)
145560006a48SWei Wang {
145660006a48SWei Wang 	struct rt6_exception_bucket *bucket;
145760006a48SWei Wang 	struct rt6_exception *rt6_ex;
145860006a48SWei Wang 	int i;
145960006a48SWei Wang 
146060006a48SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
146160006a48SWei Wang 					lockdep_is_held(&rt6_exception_lock));
146260006a48SWei Wang 
146360006a48SWei Wang 	if (bucket) {
146460006a48SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
146560006a48SWei Wang 			hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
146660006a48SWei Wang 				rt6_ex->rt6i->rt6i_prefsrc.plen = 0;
146760006a48SWei Wang 			}
146860006a48SWei Wang 			bucket++;
146960006a48SWei Wang 		}
147060006a48SWei Wang 	}
147160006a48SWei Wang }
147260006a48SWei Wang 
1473f5bbe7eeSWei Wang static void rt6_exceptions_update_pmtu(struct rt6_info *rt, int mtu)
1474f5bbe7eeSWei Wang {
1475f5bbe7eeSWei Wang 	struct rt6_exception_bucket *bucket;
1476f5bbe7eeSWei Wang 	struct rt6_exception *rt6_ex;
1477f5bbe7eeSWei Wang 	int i;
1478f5bbe7eeSWei Wang 
1479f5bbe7eeSWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1480f5bbe7eeSWei Wang 					lockdep_is_held(&rt6_exception_lock));
1481f5bbe7eeSWei Wang 
1482f5bbe7eeSWei Wang 	if (bucket) {
1483f5bbe7eeSWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1484f5bbe7eeSWei Wang 			hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) {
1485f5bbe7eeSWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1486f5bbe7eeSWei Wang 				/* For RTF_CACHE with rt6i_pmtu == 0
1487f5bbe7eeSWei Wang 				 * (i.e. a redirected route),
1488f5bbe7eeSWei Wang 				 * the metrics of its rt->dst.from has already
1489f5bbe7eeSWei Wang 				 * been updated.
1490f5bbe7eeSWei Wang 				 */
1491f5bbe7eeSWei Wang 				if (entry->rt6i_pmtu && entry->rt6i_pmtu > mtu)
1492f5bbe7eeSWei Wang 					entry->rt6i_pmtu = mtu;
1493f5bbe7eeSWei Wang 			}
1494f5bbe7eeSWei Wang 			bucket++;
1495f5bbe7eeSWei Wang 		}
1496f5bbe7eeSWei Wang 	}
1497f5bbe7eeSWei Wang }
1498f5bbe7eeSWei Wang 
1499b16cb459SWei Wang #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
1500b16cb459SWei Wang 
1501b16cb459SWei Wang static void rt6_exceptions_clean_tohost(struct rt6_info *rt,
1502b16cb459SWei Wang 					struct in6_addr *gateway)
1503b16cb459SWei Wang {
1504b16cb459SWei Wang 	struct rt6_exception_bucket *bucket;
1505b16cb459SWei Wang 	struct rt6_exception *rt6_ex;
1506b16cb459SWei Wang 	struct hlist_node *tmp;
1507b16cb459SWei Wang 	int i;
1508b16cb459SWei Wang 
1509b16cb459SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1510b16cb459SWei Wang 		return;
1511b16cb459SWei Wang 
1512b16cb459SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1513b16cb459SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1514b16cb459SWei Wang 				     lockdep_is_held(&rt6_exception_lock));
1515b16cb459SWei Wang 
1516b16cb459SWei Wang 	if (bucket) {
1517b16cb459SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1518b16cb459SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1519b16cb459SWei Wang 						  &bucket->chain, hlist) {
1520b16cb459SWei Wang 				struct rt6_info *entry = rt6_ex->rt6i;
1521b16cb459SWei Wang 
1522b16cb459SWei Wang 				if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) ==
1523b16cb459SWei Wang 				    RTF_CACHE_GATEWAY &&
1524b16cb459SWei Wang 				    ipv6_addr_equal(gateway,
1525b16cb459SWei Wang 						    &entry->rt6i_gateway)) {
1526b16cb459SWei Wang 					rt6_remove_exception(bucket, rt6_ex);
1527b16cb459SWei Wang 				}
1528b16cb459SWei Wang 			}
1529b16cb459SWei Wang 			bucket++;
1530b16cb459SWei Wang 		}
1531b16cb459SWei Wang 	}
1532b16cb459SWei Wang 
1533b16cb459SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1534b16cb459SWei Wang }
1535b16cb459SWei Wang 
1536c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket,
1537c757faa8SWei Wang 				      struct rt6_exception *rt6_ex,
1538c757faa8SWei Wang 				      struct fib6_gc_args *gc_args,
1539c757faa8SWei Wang 				      unsigned long now)
1540c757faa8SWei Wang {
1541c757faa8SWei Wang 	struct rt6_info *rt = rt6_ex->rt6i;
1542c757faa8SWei Wang 
1543c757faa8SWei Wang 	if (atomic_read(&rt->dst.__refcnt) == 1 &&
1544c757faa8SWei Wang 	    time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) {
1545c757faa8SWei Wang 		RT6_TRACE("aging clone %p\n", rt);
1546c757faa8SWei Wang 		rt6_remove_exception(bucket, rt6_ex);
1547c757faa8SWei Wang 		return;
1548c757faa8SWei Wang 	} else if (rt->rt6i_flags & RTF_GATEWAY) {
1549c757faa8SWei Wang 		struct neighbour *neigh;
1550c757faa8SWei Wang 		__u8 neigh_flags = 0;
1551c757faa8SWei Wang 
1552c757faa8SWei Wang 		neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway);
1553c757faa8SWei Wang 		if (neigh) {
1554c757faa8SWei Wang 			neigh_flags = neigh->flags;
1555c757faa8SWei Wang 			neigh_release(neigh);
1556c757faa8SWei Wang 		}
1557c757faa8SWei Wang 		if (!(neigh_flags & NTF_ROUTER)) {
1558c757faa8SWei Wang 			RT6_TRACE("purging route %p via non-router but gateway\n",
1559c757faa8SWei Wang 				  rt);
1560c757faa8SWei Wang 			rt6_remove_exception(bucket, rt6_ex);
1561c757faa8SWei Wang 			return;
1562c757faa8SWei Wang 		}
1563c757faa8SWei Wang 	}
1564c757faa8SWei Wang 	gc_args->more++;
1565c757faa8SWei Wang }
1566c757faa8SWei Wang 
1567c757faa8SWei Wang void rt6_age_exceptions(struct rt6_info *rt,
1568c757faa8SWei Wang 			struct fib6_gc_args *gc_args,
1569c757faa8SWei Wang 			unsigned long now)
1570c757faa8SWei Wang {
1571c757faa8SWei Wang 	struct rt6_exception_bucket *bucket;
1572c757faa8SWei Wang 	struct rt6_exception *rt6_ex;
1573c757faa8SWei Wang 	struct hlist_node *tmp;
1574c757faa8SWei Wang 	int i;
1575c757faa8SWei Wang 
1576c757faa8SWei Wang 	if (!rcu_access_pointer(rt->rt6i_exception_bucket))
1577c757faa8SWei Wang 		return;
1578c757faa8SWei Wang 
1579c757faa8SWei Wang 	spin_lock_bh(&rt6_exception_lock);
1580c757faa8SWei Wang 	bucket = rcu_dereference_protected(rt->rt6i_exception_bucket,
1581c757faa8SWei Wang 				    lockdep_is_held(&rt6_exception_lock));
1582c757faa8SWei Wang 
1583c757faa8SWei Wang 	if (bucket) {
1584c757faa8SWei Wang 		for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) {
1585c757faa8SWei Wang 			hlist_for_each_entry_safe(rt6_ex, tmp,
1586c757faa8SWei Wang 						  &bucket->chain, hlist) {
1587c757faa8SWei Wang 				rt6_age_examine_exception(bucket, rt6_ex,
1588c757faa8SWei Wang 							  gc_args, now);
1589c757faa8SWei Wang 			}
1590c757faa8SWei Wang 			bucket++;
1591c757faa8SWei Wang 		}
1592c757faa8SWei Wang 	}
1593c757faa8SWei Wang 	spin_unlock_bh(&rt6_exception_lock);
1594c757faa8SWei Wang }
1595c757faa8SWei Wang 
15969ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
15979ff74384SDavid Ahern 			       int oif, struct flowi6 *fl6, int flags)
15981da177e4SLinus Torvalds {
1599367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
1600*2b760fcfSWei Wang 	struct rt6_info *rt, *rt_cache;
1601c71099acSThomas Graf 	int strict = 0;
16021da177e4SLinus Torvalds 
160377d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
1604d5d32e4bSDavid Ahern 	strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE;
1605367efcb9SMartin KaFai Lau 	if (net->ipv6.devconf_all->forwarding == 0)
1606367efcb9SMartin KaFai Lau 		strict |= RT6_LOOKUP_F_REACHABLE;
16071da177e4SLinus Torvalds 
1608c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
16091da177e4SLinus Torvalds 
16104c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1611367efcb9SMartin KaFai Lau 	saved_fn = fn;
16121da177e4SLinus Torvalds 
1613ca254490SDavid Ahern 	if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF)
1614ca254490SDavid Ahern 		oif = 0;
1615ca254490SDavid Ahern 
1616a3c00e46SMartin KaFai Lau redo_rt6_select:
1617367efcb9SMartin KaFai Lau 	rt = rt6_select(fn, oif, strict);
161852bd4c0cSNicolas Dichtel 	if (rt->rt6i_nsiblings)
1619367efcb9SMartin KaFai Lau 		rt = rt6_multipath_select(rt, fl6, oif, strict);
1620a3c00e46SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
1621a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1622a3c00e46SMartin KaFai Lau 		if (fn)
1623a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
1624367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
1625367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
1626367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
1627367efcb9SMartin KaFai Lau 			fn = saved_fn;
1628367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
1629367efcb9SMartin KaFai Lau 		}
1630a3c00e46SMartin KaFai Lau 	}
1631a3c00e46SMartin KaFai Lau 
1632*2b760fcfSWei Wang 	/*Search through exception table */
1633*2b760fcfSWei Wang 	rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr);
1634*2b760fcfSWei Wang 	if (rt_cache)
1635*2b760fcfSWei Wang 		rt = rt_cache;
1636d52d3997SMartin KaFai Lau 
1637d52d3997SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) {
16383da59bd9SMartin KaFai Lau 		dst_use(&rt->dst, jiffies);
1639c71099acSThomas Graf 		read_unlock_bh(&table->tb6_lock);
16401da177e4SLinus Torvalds 
1641d52d3997SMartin KaFai Lau 		rt6_dst_from_metrics_check(rt);
1642b811580dSDavid Ahern 
1643b811580dSDavid Ahern 		trace_fib6_table_lookup(net, rt, table->tb6_id, fl6);
1644d52d3997SMartin KaFai Lau 		return rt;
16453da59bd9SMartin KaFai Lau 	} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
16463da59bd9SMartin KaFai Lau 			    !(rt->rt6i_flags & RTF_GATEWAY))) {
16473da59bd9SMartin KaFai Lau 		/* Create a RTF_CACHE clone which will not be
16483da59bd9SMartin KaFai Lau 		 * owned by the fib6 tree.  It is for the special case where
16493da59bd9SMartin KaFai Lau 		 * the daddr in the skb during the neighbor look-up is different
16503da59bd9SMartin KaFai Lau 		 * from the fl6->daddr used to look-up route here.
16513da59bd9SMartin KaFai Lau 		 */
1652c71099acSThomas Graf 
16533da59bd9SMartin KaFai Lau 		struct rt6_info *uncached_rt;
16543da59bd9SMartin KaFai Lau 
1655d52d3997SMartin KaFai Lau 		dst_use(&rt->dst, jiffies);
1656d52d3997SMartin KaFai Lau 		read_unlock_bh(&table->tb6_lock);
1657d52d3997SMartin KaFai Lau 
16583da59bd9SMartin KaFai Lau 		uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL);
16593da59bd9SMartin KaFai Lau 		dst_release(&rt->dst);
16603da59bd9SMartin KaFai Lau 
16611cfb71eeSWei Wang 		if (uncached_rt) {
16621cfb71eeSWei Wang 			/* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc()
16631cfb71eeSWei Wang 			 * No need for another dst_hold()
16641cfb71eeSWei Wang 			 */
16658d0b94afSMartin KaFai Lau 			rt6_uncached_list_add(uncached_rt);
16661cfb71eeSWei Wang 		} else {
16673da59bd9SMartin KaFai Lau 			uncached_rt = net->ipv6.ip6_null_entry;
16683da59bd9SMartin KaFai Lau 			dst_hold(&uncached_rt->dst);
16691cfb71eeSWei Wang 		}
1670b811580dSDavid Ahern 
1671b811580dSDavid Ahern 		trace_fib6_table_lookup(net, uncached_rt, table->tb6_id, fl6);
16723da59bd9SMartin KaFai Lau 		return uncached_rt;
16733da59bd9SMartin KaFai Lau 
1674d52d3997SMartin KaFai Lau 	} else {
1675d52d3997SMartin KaFai Lau 		/* Get a percpu copy */
1676d52d3997SMartin KaFai Lau 
1677d52d3997SMartin KaFai Lau 		struct rt6_info *pcpu_rt;
1678d52d3997SMartin KaFai Lau 
1679d52d3997SMartin KaFai Lau 		rt->dst.lastuse = jiffies;
1680d52d3997SMartin KaFai Lau 		rt->dst.__use++;
1681d52d3997SMartin KaFai Lau 		pcpu_rt = rt6_get_pcpu_route(rt);
1682d52d3997SMartin KaFai Lau 
16839c7370a1SMartin KaFai Lau 		if (pcpu_rt) {
1684d52d3997SMartin KaFai Lau 			read_unlock_bh(&table->tb6_lock);
16859c7370a1SMartin KaFai Lau 		} else {
16869c7370a1SMartin KaFai Lau 			/* We have to do the read_unlock first
16879c7370a1SMartin KaFai Lau 			 * because rt6_make_pcpu_route() may trigger
16889c7370a1SMartin KaFai Lau 			 * ip6_dst_gc() which will take the write_lock.
16899c7370a1SMartin KaFai Lau 			 */
16909c7370a1SMartin KaFai Lau 			dst_hold(&rt->dst);
16919c7370a1SMartin KaFai Lau 			read_unlock_bh(&table->tb6_lock);
16929c7370a1SMartin KaFai Lau 			pcpu_rt = rt6_make_pcpu_route(rt);
16939c7370a1SMartin KaFai Lau 			dst_release(&rt->dst);
16949c7370a1SMartin KaFai Lau 		}
1695d52d3997SMartin KaFai Lau 
1696b811580dSDavid Ahern 		trace_fib6_table_lookup(net, pcpu_rt, table->tb6_id, fl6);
1697d52d3997SMartin KaFai Lau 		return pcpu_rt;
16989c7370a1SMartin KaFai Lau 
1699d52d3997SMartin KaFai Lau 	}
1700c71099acSThomas Graf }
17019ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route);
1702c71099acSThomas Graf 
17038ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
17044c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
17054acad72dSPavel Emelyanov {
17064c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
17074acad72dSPavel Emelyanov }
17084acad72dSPavel Emelyanov 
1709d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net,
171072331bc0SShmulik Ladkani 					 struct net_device *dev,
171172331bc0SShmulik Ladkani 					 struct flowi6 *fl6, int flags)
171272331bc0SShmulik Ladkani {
171372331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
171472331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
171572331bc0SShmulik Ladkani 
171672331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
171772331bc0SShmulik Ladkani }
1718d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup);
171972331bc0SShmulik Ladkani 
172023aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb,
172123aebdacSJakub Sitnicki 				  struct flow_keys *keys)
172223aebdacSJakub Sitnicki {
172323aebdacSJakub Sitnicki 	const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
172423aebdacSJakub Sitnicki 	const struct ipv6hdr *key_iph = outer_iph;
172523aebdacSJakub Sitnicki 	const struct ipv6hdr *inner_iph;
172623aebdacSJakub Sitnicki 	const struct icmp6hdr *icmph;
172723aebdacSJakub Sitnicki 	struct ipv6hdr _inner_iph;
172823aebdacSJakub Sitnicki 
172923aebdacSJakub Sitnicki 	if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6))
173023aebdacSJakub Sitnicki 		goto out;
173123aebdacSJakub Sitnicki 
173223aebdacSJakub Sitnicki 	icmph = icmp6_hdr(skb);
173323aebdacSJakub Sitnicki 	if (icmph->icmp6_type != ICMPV6_DEST_UNREACH &&
173423aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PKT_TOOBIG &&
173523aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_TIME_EXCEED &&
173623aebdacSJakub Sitnicki 	    icmph->icmp6_type != ICMPV6_PARAMPROB)
173723aebdacSJakub Sitnicki 		goto out;
173823aebdacSJakub Sitnicki 
173923aebdacSJakub Sitnicki 	inner_iph = skb_header_pointer(skb,
174023aebdacSJakub Sitnicki 				       skb_transport_offset(skb) + sizeof(*icmph),
174123aebdacSJakub Sitnicki 				       sizeof(_inner_iph), &_inner_iph);
174223aebdacSJakub Sitnicki 	if (!inner_iph)
174323aebdacSJakub Sitnicki 		goto out;
174423aebdacSJakub Sitnicki 
174523aebdacSJakub Sitnicki 	key_iph = inner_iph;
174623aebdacSJakub Sitnicki out:
174723aebdacSJakub Sitnicki 	memset(keys, 0, sizeof(*keys));
174823aebdacSJakub Sitnicki 	keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
174923aebdacSJakub Sitnicki 	keys->addrs.v6addrs.src = key_iph->saddr;
175023aebdacSJakub Sitnicki 	keys->addrs.v6addrs.dst = key_iph->daddr;
175123aebdacSJakub Sitnicki 	keys->tags.flow_label = ip6_flowinfo(key_iph);
175223aebdacSJakub Sitnicki 	keys->basic.ip_proto = key_iph->nexthdr;
175323aebdacSJakub Sitnicki }
175423aebdacSJakub Sitnicki 
175523aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */
175623aebdacSJakub Sitnicki u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb)
175723aebdacSJakub Sitnicki {
175823aebdacSJakub Sitnicki 	struct flow_keys hash_keys;
175923aebdacSJakub Sitnicki 
176023aebdacSJakub Sitnicki 	if (skb) {
176123aebdacSJakub Sitnicki 		ip6_multipath_l3_keys(skb, &hash_keys);
176223aebdacSJakub Sitnicki 		return flow_hash_from_keys(&hash_keys);
176323aebdacSJakub Sitnicki 	}
176423aebdacSJakub Sitnicki 
176523aebdacSJakub Sitnicki 	return get_hash_from_flowi6(fl6);
176623aebdacSJakub Sitnicki }
176723aebdacSJakub Sitnicki 
1768c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
1769c71099acSThomas Graf {
1770b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
1771c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
1772adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1773904af04dSJiri Benc 	struct ip_tunnel_info *tun_info;
17744c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
1775e0d56fddSDavid Ahern 		.flowi6_iif = skb->dev->ifindex,
17764c9483b2SDavid S. Miller 		.daddr = iph->daddr,
17774c9483b2SDavid S. Miller 		.saddr = iph->saddr,
17786502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
17794c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
17804c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
1781c71099acSThomas Graf 	};
1782adaa70bbSThomas Graf 
1783904af04dSJiri Benc 	tun_info = skb_tunnel_info(skb);
178446fa062aSJiri Benc 	if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX))
1785904af04dSJiri Benc 		fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id;
178623aebdacSJakub Sitnicki 	if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6))
178723aebdacSJakub Sitnicki 		fl6.mp_hash = rt6_multipath_hash(&fl6, skb);
178806e9d040SJiri Benc 	skb_dst_drop(skb);
178972331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
1790c71099acSThomas Graf }
1791c71099acSThomas Graf 
17928ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
17934c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
1794c71099acSThomas Graf {
17954c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
1796c71099acSThomas Graf }
1797c71099acSThomas Graf 
17986f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk,
17996f21c96aSPaolo Abeni 					 struct flowi6 *fl6, int flags)
1800c71099acSThomas Graf {
1801d46a9d67SDavid Ahern 	bool any_src;
1802c71099acSThomas Graf 
18034c1feac5SDavid Ahern 	if (rt6_need_strict(&fl6->daddr)) {
18044c1feac5SDavid Ahern 		struct dst_entry *dst;
18054c1feac5SDavid Ahern 
18064c1feac5SDavid Ahern 		dst = l3mdev_link_scope_lookup(net, fl6);
1807ca254490SDavid Ahern 		if (dst)
1808ca254490SDavid Ahern 			return dst;
18094c1feac5SDavid Ahern 	}
1810ca254490SDavid Ahern 
18111fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
18124dc27d1cSDavid McCullough 
1813d46a9d67SDavid Ahern 	any_src = ipv6_addr_any(&fl6->saddr);
1814741a11d9SDavid Ahern 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) ||
1815d46a9d67SDavid Ahern 	    (fl6->flowi6_oif && any_src))
181677d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
1817c71099acSThomas Graf 
1818d46a9d67SDavid Ahern 	if (!any_src)
1819adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
18200c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
18210c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
1822adaa70bbSThomas Graf 
18234c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
18241da177e4SLinus Torvalds }
18256f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags);
18261da177e4SLinus Torvalds 
18272774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
182814e50e57SDavid S. Miller {
18295c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
18301dbe3252SWei Wang 	struct net_device *loopback_dev = net->loopback_dev;
183114e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
183214e50e57SDavid S. Miller 
18331dbe3252SWei Wang 	rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1,
1834b2a9c0edSWei Wang 		       DST_OBSOLETE_NONE, 0);
183514e50e57SDavid S. Miller 	if (rt) {
18360a1f5962SMartin KaFai Lau 		rt6_info_init(rt);
18370a1f5962SMartin KaFai Lau 
1838d8d1f30bSChangli Gao 		new = &rt->dst;
183914e50e57SDavid S. Miller 		new->__use = 1;
1840352e512cSHerbert Xu 		new->input = dst_discard;
1841ede2059dSEric W. Biederman 		new->output = dst_discard_out;
184214e50e57SDavid S. Miller 
1843defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
184414e50e57SDavid S. Miller 
18451dbe3252SWei Wang 		rt->rt6i_idev = in6_dev_get(loopback_dev);
18464e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
18470a1f5962SMartin KaFai Lau 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU;
184814e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
184914e50e57SDavid S. Miller 
185014e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
185114e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
185214e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
185314e50e57SDavid S. Miller #endif
185414e50e57SDavid S. Miller 	}
185514e50e57SDavid S. Miller 
185669ead7afSDavid S. Miller 	dst_release(dst_orig);
185769ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
185814e50e57SDavid S. Miller }
185914e50e57SDavid S. Miller 
18601da177e4SLinus Torvalds /*
18611da177e4SLinus Torvalds  *	Destination cache support functions
18621da177e4SLinus Torvalds  */
18631da177e4SLinus Torvalds 
18644b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt)
18654b32b5adSMartin KaFai Lau {
18664b32b5adSMartin KaFai Lau 	if (rt->dst.from &&
18674b32b5adSMartin KaFai Lau 	    dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from))
18684b32b5adSMartin KaFai Lau 		dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true);
18694b32b5adSMartin KaFai Lau }
18704b32b5adSMartin KaFai Lau 
18713da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie)
18723da59bd9SMartin KaFai Lau {
187336143645SSteffen Klassert 	u32 rt_cookie = 0;
1874c5cff856SWei Wang 
1875c5cff856SWei Wang 	if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie)
18763da59bd9SMartin KaFai Lau 		return NULL;
18773da59bd9SMartin KaFai Lau 
18783da59bd9SMartin KaFai Lau 	if (rt6_check_expired(rt))
18793da59bd9SMartin KaFai Lau 		return NULL;
18803da59bd9SMartin KaFai Lau 
18813da59bd9SMartin KaFai Lau 	return &rt->dst;
18823da59bd9SMartin KaFai Lau }
18833da59bd9SMartin KaFai Lau 
18843da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie)
18853da59bd9SMartin KaFai Lau {
18865973fb1eSMartin KaFai Lau 	if (!__rt6_check_expired(rt) &&
18875973fb1eSMartin KaFai Lau 	    rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK &&
18883da59bd9SMartin KaFai Lau 	    rt6_check((struct rt6_info *)(rt->dst.from), cookie))
18893da59bd9SMartin KaFai Lau 		return &rt->dst;
18903da59bd9SMartin KaFai Lau 	else
18913da59bd9SMartin KaFai Lau 		return NULL;
18923da59bd9SMartin KaFai Lau }
18933da59bd9SMartin KaFai Lau 
18941da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
18951da177e4SLinus Torvalds {
18961da177e4SLinus Torvalds 	struct rt6_info *rt;
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
18991da177e4SLinus Torvalds 
19006f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
19016f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
19026f3118b5SNicolas Dichtel 	 * into this function always.
19036f3118b5SNicolas Dichtel 	 */
1904e3bc10bdSHannes Frederic Sowa 
19054b32b5adSMartin KaFai Lau 	rt6_dst_from_metrics_check(rt);
19064b32b5adSMartin KaFai Lau 
190702bcf4e0SMartin KaFai Lau 	if (rt->rt6i_flags & RTF_PCPU ||
1908a4c2fd7fSWei Wang 	    (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from))
19093da59bd9SMartin KaFai Lau 		return rt6_dst_from_check(rt, cookie);
19103da59bd9SMartin KaFai Lau 	else
19113da59bd9SMartin KaFai Lau 		return rt6_check(rt, cookie);
19121da177e4SLinus Torvalds }
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
19151da177e4SLinus Torvalds {
19161da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	if (rt) {
191954c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
192054c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1921e0a1ad73SThomas Graf 				ip6_del_rt(rt);
192254c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
19231da177e4SLinus Torvalds 			}
192454c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
192554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
192654c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
192754c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
192854c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
192954c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
19301da177e4SLinus Torvalds }
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
19331da177e4SLinus Torvalds {
19341da177e4SLinus Torvalds 	struct rt6_info *rt;
19351da177e4SLinus Torvalds 
19363ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
19371da177e4SLinus Torvalds 
1938adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
19391da177e4SLinus Torvalds 	if (rt) {
19401eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
1941ad65a2f0SWei Wang 			if (dst_hold_safe(&rt->dst))
19428e3d5be7SMartin KaFai Lau 				ip6_del_rt(rt);
1943c5cff856SWei Wang 		} else {
1944c5cff856SWei Wang 			struct fib6_node *fn;
1945c5cff856SWei Wang 
1946c5cff856SWei Wang 			rcu_read_lock();
1947c5cff856SWei Wang 			fn = rcu_dereference(rt->rt6i_node);
1948c5cff856SWei Wang 			if (fn && (rt->rt6i_flags & RTF_DEFAULT))
1949c5cff856SWei Wang 				fn->fn_sernum = -1;
1950c5cff856SWei Wang 			rcu_read_unlock();
19511da177e4SLinus Torvalds 		}
19521da177e4SLinus Torvalds 	}
19531eb4f758SHannes Frederic Sowa }
19541da177e4SLinus Torvalds 
195545e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
195645e4fd26SMartin KaFai Lau {
195745e4fd26SMartin KaFai Lau 	struct net *net = dev_net(rt->dst.dev);
195845e4fd26SMartin KaFai Lau 
195945e4fd26SMartin KaFai Lau 	rt->rt6i_flags |= RTF_MODIFIED;
196045e4fd26SMartin KaFai Lau 	rt->rt6i_pmtu = mtu;
196145e4fd26SMartin KaFai Lau 	rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
196245e4fd26SMartin KaFai Lau }
196345e4fd26SMartin KaFai Lau 
19640d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
19650d3f6d29SMartin KaFai Lau {
19660d3f6d29SMartin KaFai Lau 	return !(rt->rt6i_flags & RTF_CACHE) &&
19674e587ea7SWei Wang 		(rt->rt6i_flags & RTF_PCPU ||
19684e587ea7SWei Wang 		 rcu_access_pointer(rt->rt6i_node));
19690d3f6d29SMartin KaFai Lau }
19700d3f6d29SMartin KaFai Lau 
197145e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
197245e4fd26SMartin KaFai Lau 				 const struct ipv6hdr *iph, u32 mtu)
19731da177e4SLinus Torvalds {
19740dec879fSJulian Anastasov 	const struct in6_addr *daddr, *saddr;
19751da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
19761da177e4SLinus Torvalds 
197745e4fd26SMartin KaFai Lau 	if (rt6->rt6i_flags & RTF_LOCAL)
197845e4fd26SMartin KaFai Lau 		return;
197945e4fd26SMartin KaFai Lau 
198019bda36cSXin Long 	if (dst_metric_locked(dst, RTAX_MTU))
198119bda36cSXin Long 		return;
198219bda36cSXin Long 
198345e4fd26SMartin KaFai Lau 	if (iph) {
198445e4fd26SMartin KaFai Lau 		daddr = &iph->daddr;
198545e4fd26SMartin KaFai Lau 		saddr = &iph->saddr;
198645e4fd26SMartin KaFai Lau 	} else if (sk) {
198745e4fd26SMartin KaFai Lau 		daddr = &sk->sk_v6_daddr;
198845e4fd26SMartin KaFai Lau 		saddr = &inet6_sk(sk)->saddr;
198945e4fd26SMartin KaFai Lau 	} else {
19900dec879fSJulian Anastasov 		daddr = NULL;
19910dec879fSJulian Anastasov 		saddr = NULL;
19921da177e4SLinus Torvalds 	}
19930dec879fSJulian Anastasov 	dst_confirm_neigh(dst, daddr);
19940dec879fSJulian Anastasov 	mtu = max_t(u32, mtu, IPV6_MIN_MTU);
19950dec879fSJulian Anastasov 	if (mtu >= dst_mtu(dst))
19960dec879fSJulian Anastasov 		return;
19970dec879fSJulian Anastasov 
19980dec879fSJulian Anastasov 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
19990dec879fSJulian Anastasov 		rt6_do_update_pmtu(rt6, mtu);
2000*2b760fcfSWei Wang 		/* update rt6_ex->stamp for cache */
2001*2b760fcfSWei Wang 		if (rt6->rt6i_flags & RTF_CACHE)
2002*2b760fcfSWei Wang 			rt6_update_exception_stamp_rt(rt6);
20030dec879fSJulian Anastasov 	} else if (daddr) {
20040dec879fSJulian Anastasov 		struct rt6_info *nrt6;
20050dec879fSJulian Anastasov 
200645e4fd26SMartin KaFai Lau 		nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr);
200745e4fd26SMartin KaFai Lau 		if (nrt6) {
200845e4fd26SMartin KaFai Lau 			rt6_do_update_pmtu(nrt6, mtu);
2009*2b760fcfSWei Wang 			if (rt6_insert_exception(nrt6, rt6))
2010*2b760fcfSWei Wang 				dst_release_immediate(&nrt6->dst);
201145e4fd26SMartin KaFai Lau 		}
201245e4fd26SMartin KaFai Lau 	}
201345e4fd26SMartin KaFai Lau }
201445e4fd26SMartin KaFai Lau 
201545e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
201645e4fd26SMartin KaFai Lau 			       struct sk_buff *skb, u32 mtu)
201745e4fd26SMartin KaFai Lau {
201845e4fd26SMartin KaFai Lau 	__ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu);
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds 
202142ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
2022e2d118a1SLorenzo Colitti 		     int oif, u32 mark, kuid_t uid)
202381aded24SDavid S. Miller {
202481aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
202581aded24SDavid S. Miller 	struct dst_entry *dst;
202681aded24SDavid S. Miller 	struct flowi6 fl6;
202781aded24SDavid S. Miller 
202881aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
202981aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
20301b3c61dcSLorenzo Colitti 	fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
203181aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
203281aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
20336502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
2034e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = uid;
203581aded24SDavid S. Miller 
203681aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
203781aded24SDavid S. Miller 	if (!dst->error)
203845e4fd26SMartin KaFai Lau 		__ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu));
203981aded24SDavid S. Miller 	dst_release(dst);
204081aded24SDavid S. Miller }
204181aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
204281aded24SDavid S. Miller 
204381aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
204481aded24SDavid S. Miller {
204533c162a9SMartin KaFai Lau 	struct dst_entry *dst;
204633c162a9SMartin KaFai Lau 
204781aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
2048e2d118a1SLorenzo Colitti 			sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid);
204933c162a9SMartin KaFai Lau 
205033c162a9SMartin KaFai Lau 	dst = __sk_dst_get(sk);
205133c162a9SMartin KaFai Lau 	if (!dst || !dst->obsolete ||
205233c162a9SMartin KaFai Lau 	    dst->ops->check(dst, inet6_sk(sk)->dst_cookie))
205333c162a9SMartin KaFai Lau 		return;
205433c162a9SMartin KaFai Lau 
205533c162a9SMartin KaFai Lau 	bh_lock_sock(sk);
205633c162a9SMartin KaFai Lau 	if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
205733c162a9SMartin KaFai Lau 		ip6_datagram_dst_update(sk, false);
205833c162a9SMartin KaFai Lau 	bh_unlock_sock(sk);
205981aded24SDavid S. Miller }
206081aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
206181aded24SDavid S. Miller 
2062b55b76b2SDuan Jiong /* Handle redirects */
2063b55b76b2SDuan Jiong struct ip6rd_flowi {
2064b55b76b2SDuan Jiong 	struct flowi6 fl6;
2065b55b76b2SDuan Jiong 	struct in6_addr gateway;
2066b55b76b2SDuan Jiong };
2067b55b76b2SDuan Jiong 
2068b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
2069b55b76b2SDuan Jiong 					     struct fib6_table *table,
2070b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
2071b55b76b2SDuan Jiong 					     int flags)
2072b55b76b2SDuan Jiong {
2073b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
2074*2b760fcfSWei Wang 	struct rt6_info *rt, *rt_cache;
2075b55b76b2SDuan Jiong 	struct fib6_node *fn;
2076b55b76b2SDuan Jiong 
2077b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
207867c408cfSAlexander Alemayhu 	 * check if the redirect has come from appropriate router.
2079b55b76b2SDuan Jiong 	 *
2080b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
2081b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
2082b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
2083b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
2084b55b76b2SDuan Jiong 	 * routes.
2085b55b76b2SDuan Jiong 	 */
2086b55b76b2SDuan Jiong 
2087b55b76b2SDuan Jiong 	read_lock_bh(&table->tb6_lock);
2088b55b76b2SDuan Jiong 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
2089b55b76b2SDuan Jiong restart:
2090b55b76b2SDuan Jiong 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2091b55b76b2SDuan Jiong 		if (rt6_check_expired(rt))
2092b55b76b2SDuan Jiong 			continue;
2093b55b76b2SDuan Jiong 		if (rt->dst.error)
2094b55b76b2SDuan Jiong 			break;
2095b55b76b2SDuan Jiong 		if (!(rt->rt6i_flags & RTF_GATEWAY))
2096b55b76b2SDuan Jiong 			continue;
2097b55b76b2SDuan Jiong 		if (fl6->flowi6_oif != rt->dst.dev->ifindex)
2098b55b76b2SDuan Jiong 			continue;
2099*2b760fcfSWei Wang 		/* rt_cache's gateway might be different from its 'parent'
2100*2b760fcfSWei Wang 		 * in the case of an ip redirect.
2101*2b760fcfSWei Wang 		 * So we keep searching in the exception table if the gateway
2102*2b760fcfSWei Wang 		 * is different.
2103*2b760fcfSWei Wang 		 */
2104*2b760fcfSWei Wang 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) {
2105*2b760fcfSWei Wang 			rt_cache = rt6_find_cached_rt(rt,
2106*2b760fcfSWei Wang 						      &fl6->daddr,
2107*2b760fcfSWei Wang 						      &fl6->saddr);
2108*2b760fcfSWei Wang 			if (rt_cache &&
2109*2b760fcfSWei Wang 			    ipv6_addr_equal(&rdfl->gateway,
2110*2b760fcfSWei Wang 					    &rt_cache->rt6i_gateway)) {
2111*2b760fcfSWei Wang 				rt = rt_cache;
2112*2b760fcfSWei Wang 				break;
2113*2b760fcfSWei Wang 			}
2114b55b76b2SDuan Jiong 			continue;
2115*2b760fcfSWei Wang 		}
2116b55b76b2SDuan Jiong 		break;
2117b55b76b2SDuan Jiong 	}
2118b55b76b2SDuan Jiong 
2119b55b76b2SDuan Jiong 	if (!rt)
2120b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
2121b55b76b2SDuan Jiong 	else if (rt->dst.error) {
2122b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
2123b0a1ba59SMartin KaFai Lau 		goto out;
2124b0a1ba59SMartin KaFai Lau 	}
2125b0a1ba59SMartin KaFai Lau 
2126b0a1ba59SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
2127a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
2128a3c00e46SMartin KaFai Lau 		if (fn)
2129a3c00e46SMartin KaFai Lau 			goto restart;
2130b55b76b2SDuan Jiong 	}
2131a3c00e46SMartin KaFai Lau 
2132b0a1ba59SMartin KaFai Lau out:
2133b55b76b2SDuan Jiong 	dst_hold(&rt->dst);
2134b55b76b2SDuan Jiong 
2135b55b76b2SDuan Jiong 	read_unlock_bh(&table->tb6_lock);
2136b55b76b2SDuan Jiong 
2137b811580dSDavid Ahern 	trace_fib6_table_lookup(net, rt, table->tb6_id, fl6);
2138b55b76b2SDuan Jiong 	return rt;
2139b55b76b2SDuan Jiong };
2140b55b76b2SDuan Jiong 
2141b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
2142b55b76b2SDuan Jiong 					const struct flowi6 *fl6,
2143b55b76b2SDuan Jiong 					const struct in6_addr *gateway)
2144b55b76b2SDuan Jiong {
2145b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
2146b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
2147b55b76b2SDuan Jiong 
2148b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
2149b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
2150b55b76b2SDuan Jiong 
2151b55b76b2SDuan Jiong 	return fib6_rule_lookup(net, &rdfl.fl6,
2152b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
2153b55b76b2SDuan Jiong }
2154b55b76b2SDuan Jiong 
2155e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark,
2156e2d118a1SLorenzo Colitti 		  kuid_t uid)
21573a5ad2eeSDavid S. Miller {
21583a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
21593a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
21603a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
21613a5ad2eeSDavid S. Miller 
21623a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2163e374c618SJulian Anastasov 	fl6.flowi6_iif = LOOPBACK_IFINDEX;
21643a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
21653a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
21663a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
21673a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
21686502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
2169e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = uid;
21703a5ad2eeSDavid S. Miller 
2171b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
21726700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
21733a5ad2eeSDavid S. Miller 	dst_release(dst);
21743a5ad2eeSDavid S. Miller }
21753a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
21763a5ad2eeSDavid S. Miller 
2177c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
2178c92a59ecSDuan Jiong 			    u32 mark)
2179c92a59ecSDuan Jiong {
2180c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
2181c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
2182c92a59ecSDuan Jiong 	struct dst_entry *dst;
2183c92a59ecSDuan Jiong 	struct flowi6 fl6;
2184c92a59ecSDuan Jiong 
2185c92a59ecSDuan Jiong 	memset(&fl6, 0, sizeof(fl6));
2186e374c618SJulian Anastasov 	fl6.flowi6_iif = LOOPBACK_IFINDEX;
2187c92a59ecSDuan Jiong 	fl6.flowi6_oif = oif;
2188c92a59ecSDuan Jiong 	fl6.flowi6_mark = mark;
2189c92a59ecSDuan Jiong 	fl6.daddr = msg->dest;
2190c92a59ecSDuan Jiong 	fl6.saddr = iph->daddr;
2191e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sock_net_uid(net, NULL);
2192c92a59ecSDuan Jiong 
2193b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &iph->saddr);
2194c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
2195c92a59ecSDuan Jiong 	dst_release(dst);
2196c92a59ecSDuan Jiong }
2197c92a59ecSDuan Jiong 
21983a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
21993a5ad2eeSDavid S. Miller {
2200e2d118a1SLorenzo Colitti 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark,
2201e2d118a1SLorenzo Colitti 		     sk->sk_uid);
22023a5ad2eeSDavid S. Miller }
22033a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
22043a5ad2eeSDavid S. Miller 
22050dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
22061da177e4SLinus Torvalds {
22070dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
22080dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
22090dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
22100dbaee3bSDavid S. Miller 
22111da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
22121da177e4SLinus Torvalds 
22135578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
22145578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 	/*
22171da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
22181da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
22191da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
22201da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
22211da177e4SLinus Torvalds 	 */
22221da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
22231da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
22241da177e4SLinus Torvalds 	return mtu;
22251da177e4SLinus Torvalds }
22261da177e4SLinus Torvalds 
2227ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
2228d33e4553SDavid S. Miller {
22294b32b5adSMartin KaFai Lau 	const struct rt6_info *rt = (const struct rt6_info *)dst;
22304b32b5adSMartin KaFai Lau 	unsigned int mtu = rt->rt6i_pmtu;
2231d33e4553SDavid S. Miller 	struct inet6_dev *idev;
2232618f9bc7SSteffen Klassert 
2233618f9bc7SSteffen Klassert 	if (mtu)
223430f78d8eSEric Dumazet 		goto out;
2235618f9bc7SSteffen Klassert 
22364b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
22374b32b5adSMartin KaFai Lau 	if (mtu)
22384b32b5adSMartin KaFai Lau 		goto out;
22394b32b5adSMartin KaFai Lau 
2240618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
2241d33e4553SDavid S. Miller 
2242d33e4553SDavid S. Miller 	rcu_read_lock();
2243d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
2244d33e4553SDavid S. Miller 	if (idev)
2245d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
2246d33e4553SDavid S. Miller 	rcu_read_unlock();
2247d33e4553SDavid S. Miller 
224830f78d8eSEric Dumazet out:
224914972cbdSRoopa Prabhu 	mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
225014972cbdSRoopa Prabhu 
225114972cbdSRoopa Prabhu 	return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
2252d33e4553SDavid S. Miller }
2253d33e4553SDavid S. Miller 
22543b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
225587a11578SDavid S. Miller 				  struct flowi6 *fl6)
22561da177e4SLinus Torvalds {
225787a11578SDavid S. Miller 	struct dst_entry *dst;
22581da177e4SLinus Torvalds 	struct rt6_info *rt;
22591da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
2260c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
22611da177e4SLinus Torvalds 
226238308473SDavid S. Miller 	if (unlikely(!idev))
2263122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
22641da177e4SLinus Torvalds 
2265ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, dev, 0);
226638308473SDavid S. Miller 	if (unlikely(!rt)) {
22671da177e4SLinus Torvalds 		in6_dev_put(idev);
226887a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
22691da177e4SLinus Torvalds 		goto out;
22701da177e4SLinus Torvalds 	}
22711da177e4SLinus Torvalds 
22728e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
22738e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
2274550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
227587a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
22768e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
22778e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
227814edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
22791da177e4SLinus Torvalds 
2280587fea74SWei Wang 	/* Add this dst into uncached_list so that rt6_ifdown() can
2281587fea74SWei Wang 	 * do proper release of the net_device
2282587fea74SWei Wang 	 */
2283587fea74SWei Wang 	rt6_uncached_list_add(rt);
22841da177e4SLinus Torvalds 
228587a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
228687a11578SDavid S. Miller 
22871da177e4SLinus Torvalds out:
228887a11578SDavid S. Miller 	return dst;
22891da177e4SLinus Torvalds }
22901da177e4SLinus Torvalds 
2291569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
22921da177e4SLinus Torvalds {
229386393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
22947019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
22957019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
22967019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
22977019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
22987019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
2299fc66f95cSEric Dumazet 	int entries;
23001da177e4SLinus Torvalds 
2301fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
230249a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
2303fc66f95cSEric Dumazet 	    entries <= rt_max_size)
23041da177e4SLinus Torvalds 		goto out;
23051da177e4SLinus Torvalds 
23066891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
230714956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
2308fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
2309fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
23107019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
23111da177e4SLinus Torvalds out:
23127019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
2313fc66f95cSEric Dumazet 	return entries > rt_max_size;
23141da177e4SLinus Torvalds }
23151da177e4SLinus Torvalds 
2316e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc,
2317e715b6d3SFlorian Westphal 			       const struct fib6_config *cfg)
2318e715b6d3SFlorian Westphal {
2319c3a8d947SDaniel Borkmann 	bool ecn_ca = false;
2320e715b6d3SFlorian Westphal 	struct nlattr *nla;
2321e715b6d3SFlorian Westphal 	int remaining;
2322e715b6d3SFlorian Westphal 	u32 *mp;
2323e715b6d3SFlorian Westphal 
232463159f29SIan Morris 	if (!cfg->fc_mx)
2325e715b6d3SFlorian Westphal 		return 0;
2326e715b6d3SFlorian Westphal 
2327e715b6d3SFlorian Westphal 	mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
2328e715b6d3SFlorian Westphal 	if (unlikely(!mp))
2329e715b6d3SFlorian Westphal 		return -ENOMEM;
2330e715b6d3SFlorian Westphal 
2331e715b6d3SFlorian Westphal 	nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
2332e715b6d3SFlorian Westphal 		int type = nla_type(nla);
2333ea697639SDaniel Borkmann 		u32 val;
2334ea697639SDaniel Borkmann 
23351bb14807SDaniel Borkmann 		if (!type)
23361bb14807SDaniel Borkmann 			continue;
2337e715b6d3SFlorian Westphal 		if (unlikely(type > RTAX_MAX))
2338e715b6d3SFlorian Westphal 			goto err;
23391bb14807SDaniel Borkmann 
2340ea697639SDaniel Borkmann 		if (type == RTAX_CC_ALGO) {
2341ea697639SDaniel Borkmann 			char tmp[TCP_CA_NAME_MAX];
2342e715b6d3SFlorian Westphal 
2343ea697639SDaniel Borkmann 			nla_strlcpy(tmp, nla, sizeof(tmp));
2344c3a8d947SDaniel Borkmann 			val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
2345ea697639SDaniel Borkmann 			if (val == TCP_CA_UNSPEC)
2346ea697639SDaniel Borkmann 				goto err;
2347ea697639SDaniel Borkmann 		} else {
2348ea697639SDaniel Borkmann 			val = nla_get_u32(nla);
2349ea697639SDaniel Borkmann 		}
2350626abd59SPaolo Abeni 		if (type == RTAX_HOPLIMIT && val > 255)
2351626abd59SPaolo Abeni 			val = 255;
2352b8d3e416SDaniel Borkmann 		if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
2353b8d3e416SDaniel Borkmann 			goto err;
2354ea697639SDaniel Borkmann 
2355ea697639SDaniel Borkmann 		mp[type - 1] = val;
2356e715b6d3SFlorian Westphal 		__set_bit(type - 1, mxc->mx_valid);
2357e715b6d3SFlorian Westphal 	}
2358e715b6d3SFlorian Westphal 
2359c3a8d947SDaniel Borkmann 	if (ecn_ca) {
2360c3a8d947SDaniel Borkmann 		__set_bit(RTAX_FEATURES - 1, mxc->mx_valid);
2361c3a8d947SDaniel Borkmann 		mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
2362c3a8d947SDaniel Borkmann 	}
2363e715b6d3SFlorian Westphal 
2364c3a8d947SDaniel Borkmann 	mxc->mx = mp;
2365e715b6d3SFlorian Westphal 	return 0;
2366e715b6d3SFlorian Westphal  err:
2367e715b6d3SFlorian Westphal 	kfree(mp);
2368e715b6d3SFlorian Westphal 	return -EINVAL;
2369e715b6d3SFlorian Westphal }
23701da177e4SLinus Torvalds 
23718c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net,
23728c14586fSDavid Ahern 					    struct fib6_config *cfg,
23738c14586fSDavid Ahern 					    const struct in6_addr *gw_addr)
23748c14586fSDavid Ahern {
23758c14586fSDavid Ahern 	struct flowi6 fl6 = {
23768c14586fSDavid Ahern 		.flowi6_oif = cfg->fc_ifindex,
23778c14586fSDavid Ahern 		.daddr = *gw_addr,
23788c14586fSDavid Ahern 		.saddr = cfg->fc_prefsrc,
23798c14586fSDavid Ahern 	};
23808c14586fSDavid Ahern 	struct fib6_table *table;
23818c14586fSDavid Ahern 	struct rt6_info *rt;
2382d5d32e4bSDavid Ahern 	int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE;
23838c14586fSDavid Ahern 
23848c14586fSDavid Ahern 	table = fib6_get_table(net, cfg->fc_table);
23858c14586fSDavid Ahern 	if (!table)
23868c14586fSDavid Ahern 		return NULL;
23878c14586fSDavid Ahern 
23888c14586fSDavid Ahern 	if (!ipv6_addr_any(&cfg->fc_prefsrc))
23898c14586fSDavid Ahern 		flags |= RT6_LOOKUP_F_HAS_SADDR;
23908c14586fSDavid Ahern 
23918c14586fSDavid Ahern 	rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags);
23928c14586fSDavid Ahern 
23938c14586fSDavid Ahern 	/* if table lookup failed, fall back to full lookup */
23948c14586fSDavid Ahern 	if (rt == net->ipv6.ip6_null_entry) {
23958c14586fSDavid Ahern 		ip6_rt_put(rt);
23968c14586fSDavid Ahern 		rt = NULL;
23978c14586fSDavid Ahern 	}
23988c14586fSDavid Ahern 
23998c14586fSDavid Ahern 	return rt;
24008c14586fSDavid Ahern }
24018c14586fSDavid Ahern 
2402333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg,
2403333c4301SDavid Ahern 					      struct netlink_ext_ack *extack)
24041da177e4SLinus Torvalds {
24055578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
24061da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
24071da177e4SLinus Torvalds 	struct net_device *dev = NULL;
24081da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
2409c71099acSThomas Graf 	struct fib6_table *table;
24101da177e4SLinus Torvalds 	int addr_type;
24118c5b83f0SRoopa Prabhu 	int err = -EINVAL;
24121da177e4SLinus Torvalds 
2413557c44beSDavid Ahern 	/* RTF_PCPU is an internal flag; can not be set by userspace */
2414d5d531cbSDavid Ahern 	if (cfg->fc_flags & RTF_PCPU) {
2415d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU");
2416557c44beSDavid Ahern 		goto out;
2417d5d531cbSDavid Ahern 	}
2418557c44beSDavid Ahern 
2419d5d531cbSDavid Ahern 	if (cfg->fc_dst_len > 128) {
2420d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid prefix length");
24218c5b83f0SRoopa Prabhu 		goto out;
2422d5d531cbSDavid Ahern 	}
2423d5d531cbSDavid Ahern 	if (cfg->fc_src_len > 128) {
2424d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "Invalid source address length");
2425d5d531cbSDavid Ahern 		goto out;
2426d5d531cbSDavid Ahern 	}
24271da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
2428d5d531cbSDavid Ahern 	if (cfg->fc_src_len) {
2429d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack,
2430d5d531cbSDavid Ahern 			       "Specifying source address requires IPV6_SUBTREES to be enabled");
24318c5b83f0SRoopa Prabhu 		goto out;
2432d5d531cbSDavid Ahern 	}
24331da177e4SLinus Torvalds #endif
243486872cb5SThomas Graf 	if (cfg->fc_ifindex) {
24351da177e4SLinus Torvalds 		err = -ENODEV;
24365578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
24371da177e4SLinus Torvalds 		if (!dev)
24381da177e4SLinus Torvalds 			goto out;
24391da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
24401da177e4SLinus Torvalds 		if (!idev)
24411da177e4SLinus Torvalds 			goto out;
24421da177e4SLinus Torvalds 	}
24431da177e4SLinus Torvalds 
244486872cb5SThomas Graf 	if (cfg->fc_metric == 0)
244586872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
24461da177e4SLinus Torvalds 
2447c71099acSThomas Graf 	err = -ENOBUFS;
244838308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
2449d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
2450d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
245138308473SDavid S. Miller 		if (!table) {
2452f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
2453d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
2454d71314b4SMatti Vaittinen 		}
2455d71314b4SMatti Vaittinen 	} else {
2456d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
2457d71314b4SMatti Vaittinen 	}
245838308473SDavid S. Miller 
245938308473SDavid S. Miller 	if (!table)
2460c71099acSThomas Graf 		goto out;
2461c71099acSThomas Graf 
2462ad706862SMartin KaFai Lau 	rt = ip6_dst_alloc(net, NULL,
2463ad706862SMartin KaFai Lau 			   (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT);
24641da177e4SLinus Torvalds 
246538308473SDavid S. Miller 	if (!rt) {
24661da177e4SLinus Torvalds 		err = -ENOMEM;
24671da177e4SLinus Torvalds 		goto out;
24681da177e4SLinus Torvalds 	}
24691da177e4SLinus Torvalds 
24701716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
24711716a961SGao feng 		rt6_set_expires(rt, jiffies +
24721716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
24731716a961SGao feng 	else
24741716a961SGao feng 		rt6_clean_expires(rt);
24751da177e4SLinus Torvalds 
247686872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
247786872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
247886872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
247986872cb5SThomas Graf 
248086872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
2483d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
2484ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
2485ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
24861da177e4SLinus Torvalds 	else
2487d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
24881da177e4SLinus Torvalds 
2489d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
24901da177e4SLinus Torvalds 
249119e42e45SRoopa Prabhu 	if (cfg->fc_encap) {
249219e42e45SRoopa Prabhu 		struct lwtunnel_state *lwtstate;
249319e42e45SRoopa Prabhu 
249430357d7dSDavid Ahern 		err = lwtunnel_build_state(cfg->fc_encap_type,
2495127eb7cdSTom Herbert 					   cfg->fc_encap, AF_INET6, cfg,
24969ae28727SDavid Ahern 					   &lwtstate, extack);
249719e42e45SRoopa Prabhu 		if (err)
249819e42e45SRoopa Prabhu 			goto out;
249961adedf3SJiri Benc 		rt->dst.lwtstate = lwtstate_get(lwtstate);
250061adedf3SJiri Benc 		if (lwtunnel_output_redirect(rt->dst.lwtstate)) {
250161adedf3SJiri Benc 			rt->dst.lwtstate->orig_output = rt->dst.output;
250261adedf3SJiri Benc 			rt->dst.output = lwtunnel_output;
250319e42e45SRoopa Prabhu 		}
250461adedf3SJiri Benc 		if (lwtunnel_input_redirect(rt->dst.lwtstate)) {
250561adedf3SJiri Benc 			rt->dst.lwtstate->orig_input = rt->dst.input;
250661adedf3SJiri Benc 			rt->dst.input = lwtunnel_input;
250725368623STom Herbert 		}
250825368623STom Herbert 	}
250919e42e45SRoopa Prabhu 
251086872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
251186872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
2512afc4eef8SMartin KaFai Lau 	if (rt->rt6i_dst.plen == 128)
251311d53b49SDavid S. Miller 		rt->dst.flags |= DST_HOST;
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
251686872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
251786872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
25181da177e4SLinus Torvalds #endif
25191da177e4SLinus Torvalds 
252086872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
25211da177e4SLinus Torvalds 
25221da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
25231da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
25241da177e4SLinus Torvalds 	 */
252586872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
252638308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
252738308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
252838308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
25291da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
25305578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
25311da177e4SLinus Torvalds 			if (dev) {
25321da177e4SLinus Torvalds 				dev_put(dev);
25331da177e4SLinus Torvalds 				in6_dev_put(idev);
25341da177e4SLinus Torvalds 			}
25355578689aSDaniel Lezcano 			dev = net->loopback_dev;
25361da177e4SLinus Torvalds 			dev_hold(dev);
25371da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
25381da177e4SLinus Torvalds 			if (!idev) {
25391da177e4SLinus Torvalds 				err = -ENODEV;
25401da177e4SLinus Torvalds 				goto out;
25411da177e4SLinus Torvalds 			}
25421da177e4SLinus Torvalds 		}
25431da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
2544ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
2545ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
2546ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
2547ede2059dSEric W. Biederman 			rt->dst.output = dst_discard_out;
25487150aedeSKamala R 			rt->dst.input = dst_discard;
2549ef2c7d7bSNicolas Dichtel 			break;
2550ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
2551ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
25527150aedeSKamala R 			rt->dst.output = ip6_pkt_prohibit_out;
25537150aedeSKamala R 			rt->dst.input = ip6_pkt_prohibit;
2554ef2c7d7bSNicolas Dichtel 			break;
2555b4949ab2SNicolas Dichtel 		case RTN_THROW:
25560315e382SNikola Forró 		case RTN_UNREACHABLE:
2557ef2c7d7bSNicolas Dichtel 		default:
25587150aedeSKamala R 			rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
25590315e382SNikola Forró 					: (cfg->fc_type == RTN_UNREACHABLE)
25600315e382SNikola Forró 					? -EHOSTUNREACH : -ENETUNREACH;
25617150aedeSKamala R 			rt->dst.output = ip6_pkt_discard_out;
25627150aedeSKamala R 			rt->dst.input = ip6_pkt_discard;
2563ef2c7d7bSNicolas Dichtel 			break;
2564ef2c7d7bSNicolas Dichtel 		}
25651da177e4SLinus Torvalds 		goto install_route;
25661da177e4SLinus Torvalds 	}
25671da177e4SLinus Torvalds 
256886872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
2569b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
25701da177e4SLinus Torvalds 		int gwa_type;
25711da177e4SLinus Torvalds 
257286872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
2573330567b7SFlorian Westphal 		gwa_type = ipv6_addr_type(gw_addr);
257448ed7b26SFlorian Westphal 
257548ed7b26SFlorian Westphal 		/* if gw_addr is local we will fail to detect this in case
257648ed7b26SFlorian Westphal 		 * address is still TENTATIVE (DAD in progress). rt6_lookup()
257748ed7b26SFlorian Westphal 		 * will return already-added prefix route via interface that
257848ed7b26SFlorian Westphal 		 * prefix route was assigned to, which might be non-loopback.
257948ed7b26SFlorian Westphal 		 */
258048ed7b26SFlorian Westphal 		err = -EINVAL;
2581330567b7SFlorian Westphal 		if (ipv6_chk_addr_and_flags(net, gw_addr,
2582330567b7SFlorian Westphal 					    gwa_type & IPV6_ADDR_LINKLOCAL ?
2583d5d531cbSDavid Ahern 					    dev : NULL, 0, 0)) {
2584d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid gateway address");
258548ed7b26SFlorian Westphal 			goto out;
2586d5d531cbSDavid Ahern 		}
25874e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
25881da177e4SLinus Torvalds 
25891da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
25908c14586fSDavid Ahern 			struct rt6_info *grt = NULL;
25911da177e4SLinus Torvalds 
25921da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
25931da177e4SLinus Torvalds 			   addresses as nexthop address.
25941da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
25951da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
25961da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
25971da177e4SLinus Torvalds 			   some exceptions. --ANK
259896d5822cSErik Nordmark 			   We allow IPv4-mapped nexthops to support RFC4798-type
259996d5822cSErik Nordmark 			   addressing
26001da177e4SLinus Torvalds 			 */
260196d5822cSErik Nordmark 			if (!(gwa_type & (IPV6_ADDR_UNICAST |
2602d5d531cbSDavid Ahern 					  IPV6_ADDR_MAPPED))) {
2603d5d531cbSDavid Ahern 				NL_SET_ERR_MSG(extack,
2604d5d531cbSDavid Ahern 					       "Invalid gateway address");
26051da177e4SLinus Torvalds 				goto out;
2606d5d531cbSDavid Ahern 			}
26071da177e4SLinus Torvalds 
2608a435a07fSVincent Bernat 			if (cfg->fc_table) {
26098c14586fSDavid Ahern 				grt = ip6_nh_lookup_table(net, cfg, gw_addr);
26108c14586fSDavid Ahern 
2611a435a07fSVincent Bernat 				if (grt) {
2612a435a07fSVincent Bernat 					if (grt->rt6i_flags & RTF_GATEWAY ||
2613a435a07fSVincent Bernat 					    (dev && dev != grt->dst.dev)) {
2614a435a07fSVincent Bernat 						ip6_rt_put(grt);
2615a435a07fSVincent Bernat 						grt = NULL;
2616a435a07fSVincent Bernat 					}
2617a435a07fSVincent Bernat 				}
2618a435a07fSVincent Bernat 			}
2619a435a07fSVincent Bernat 
26208c14586fSDavid Ahern 			if (!grt)
26218c14586fSDavid Ahern 				grt = rt6_lookup(net, gw_addr, NULL,
26228c14586fSDavid Ahern 						 cfg->fc_ifindex, 1);
26231da177e4SLinus Torvalds 
26241da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
262538308473SDavid S. Miller 			if (!grt)
26261da177e4SLinus Torvalds 				goto out;
26271da177e4SLinus Torvalds 			if (dev) {
2628d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
262994e187c0SAmerigo Wang 					ip6_rt_put(grt);
26301da177e4SLinus Torvalds 					goto out;
26311da177e4SLinus Torvalds 				}
26321da177e4SLinus Torvalds 			} else {
2633d1918542SDavid S. Miller 				dev = grt->dst.dev;
26341da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
26351da177e4SLinus Torvalds 				dev_hold(dev);
26361da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
26371da177e4SLinus Torvalds 			}
26381da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
26391da177e4SLinus Torvalds 				err = 0;
264094e187c0SAmerigo Wang 			ip6_rt_put(grt);
26411da177e4SLinus Torvalds 
26421da177e4SLinus Torvalds 			if (err)
26431da177e4SLinus Torvalds 				goto out;
26441da177e4SLinus Torvalds 		}
26451da177e4SLinus Torvalds 		err = -EINVAL;
2646d5d531cbSDavid Ahern 		if (!dev) {
2647d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Egress device not specified");
26481da177e4SLinus Torvalds 			goto out;
2649d5d531cbSDavid Ahern 		} else if (dev->flags & IFF_LOOPBACK) {
2650d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack,
2651d5d531cbSDavid Ahern 				       "Egress device can not be loopback device for this route");
2652d5d531cbSDavid Ahern 			goto out;
2653d5d531cbSDavid Ahern 		}
26541da177e4SLinus Torvalds 	}
26551da177e4SLinus Torvalds 
26561da177e4SLinus Torvalds 	err = -ENODEV;
265738308473SDavid S. Miller 	if (!dev)
26581da177e4SLinus Torvalds 		goto out;
26591da177e4SLinus Torvalds 
2660c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
2661c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
2662d5d531cbSDavid Ahern 			NL_SET_ERR_MSG(extack, "Invalid source address");
2663c3968a85SDaniel Walter 			err = -EINVAL;
2664c3968a85SDaniel Walter 			goto out;
2665c3968a85SDaniel Walter 		}
26664e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
2667c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
2668c3968a85SDaniel Walter 	} else
2669c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2670c3968a85SDaniel Walter 
267186872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
26721da177e4SLinus Torvalds 
26731da177e4SLinus Torvalds install_route:
2674d8d1f30bSChangli Gao 	rt->dst.dev = dev;
26751da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2676c71099acSThomas Graf 	rt->rt6i_table = table;
267763152fc0SDaniel Lezcano 
2678c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
267963152fc0SDaniel Lezcano 
26808c5b83f0SRoopa Prabhu 	return rt;
26811da177e4SLinus Torvalds out:
26821da177e4SLinus Torvalds 	if (dev)
26831da177e4SLinus Torvalds 		dev_put(dev);
26841da177e4SLinus Torvalds 	if (idev)
26851da177e4SLinus Torvalds 		in6_dev_put(idev);
2686587fea74SWei Wang 	if (rt)
2687587fea74SWei Wang 		dst_release_immediate(&rt->dst);
26886b9ea5a6SRoopa Prabhu 
26898c5b83f0SRoopa Prabhu 	return ERR_PTR(err);
26906b9ea5a6SRoopa Prabhu }
26916b9ea5a6SRoopa Prabhu 
2692333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg,
2693333c4301SDavid Ahern 		  struct netlink_ext_ack *extack)
26946b9ea5a6SRoopa Prabhu {
26956b9ea5a6SRoopa Prabhu 	struct mx6_config mxc = { .mx = NULL, };
26968c5b83f0SRoopa Prabhu 	struct rt6_info *rt;
26976b9ea5a6SRoopa Prabhu 	int err;
26986b9ea5a6SRoopa Prabhu 
2699333c4301SDavid Ahern 	rt = ip6_route_info_create(cfg, extack);
27008c5b83f0SRoopa Prabhu 	if (IS_ERR(rt)) {
27018c5b83f0SRoopa Prabhu 		err = PTR_ERR(rt);
27028c5b83f0SRoopa Prabhu 		rt = NULL;
27036b9ea5a6SRoopa Prabhu 		goto out;
27048c5b83f0SRoopa Prabhu 	}
27056b9ea5a6SRoopa Prabhu 
27066b9ea5a6SRoopa Prabhu 	err = ip6_convert_metrics(&mxc, cfg);
27076b9ea5a6SRoopa Prabhu 	if (err)
27086b9ea5a6SRoopa Prabhu 		goto out;
27096b9ea5a6SRoopa Prabhu 
2710333c4301SDavid Ahern 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack);
27116b9ea5a6SRoopa Prabhu 
27126b9ea5a6SRoopa Prabhu 	kfree(mxc.mx);
27136b9ea5a6SRoopa Prabhu 
27146b9ea5a6SRoopa Prabhu 	return err;
27156b9ea5a6SRoopa Prabhu out:
2716587fea74SWei Wang 	if (rt)
2717587fea74SWei Wang 		dst_release_immediate(&rt->dst);
27186b9ea5a6SRoopa Prabhu 
27191da177e4SLinus Torvalds 	return err;
27201da177e4SLinus Torvalds }
27211da177e4SLinus Torvalds 
272286872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
27231da177e4SLinus Torvalds {
27241da177e4SLinus Torvalds 	int err;
2725c71099acSThomas Graf 	struct fib6_table *table;
2726d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
27271da177e4SLinus Torvalds 
2728a4c2fd7fSWei Wang 	if (rt == net->ipv6.ip6_null_entry) {
27296825a26cSGao feng 		err = -ENOENT;
27306825a26cSGao feng 		goto out;
27316825a26cSGao feng 	}
27326c813a72SPatrick McHardy 
2733c71099acSThomas Graf 	table = rt->rt6i_table;
2734c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
273586872cb5SThomas Graf 	err = fib6_del(rt, info);
2736c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
27371da177e4SLinus Torvalds 
27386825a26cSGao feng out:
273994e187c0SAmerigo Wang 	ip6_rt_put(rt);
27401da177e4SLinus Torvalds 	return err;
27411da177e4SLinus Torvalds }
27421da177e4SLinus Torvalds 
2743e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
2744e0a1ad73SThomas Graf {
27454d1169c1SDenis V. Lunev 	struct nl_info info = {
2746d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
27474d1169c1SDenis V. Lunev 	};
2748528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
2749e0a1ad73SThomas Graf }
2750e0a1ad73SThomas Graf 
27510ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg)
27520ae81335SDavid Ahern {
27530ae81335SDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
2754e3330039SWANG Cong 	struct net *net = info->nl_net;
275516a16cd3SDavid Ahern 	struct sk_buff *skb = NULL;
27560ae81335SDavid Ahern 	struct fib6_table *table;
2757e3330039SWANG Cong 	int err = -ENOENT;
27580ae81335SDavid Ahern 
2759e3330039SWANG Cong 	if (rt == net->ipv6.ip6_null_entry)
2760e3330039SWANG Cong 		goto out_put;
27610ae81335SDavid Ahern 	table = rt->rt6i_table;
27620ae81335SDavid Ahern 	write_lock_bh(&table->tb6_lock);
27630ae81335SDavid Ahern 
27640ae81335SDavid Ahern 	if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) {
27650ae81335SDavid Ahern 		struct rt6_info *sibling, *next_sibling;
27660ae81335SDavid Ahern 
276716a16cd3SDavid Ahern 		/* prefer to send a single notification with all hops */
276816a16cd3SDavid Ahern 		skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
276916a16cd3SDavid Ahern 		if (skb) {
277016a16cd3SDavid Ahern 			u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
277116a16cd3SDavid Ahern 
2772e3330039SWANG Cong 			if (rt6_fill_node(net, skb, rt,
277316a16cd3SDavid Ahern 					  NULL, NULL, 0, RTM_DELROUTE,
277416a16cd3SDavid Ahern 					  info->portid, seq, 0) < 0) {
277516a16cd3SDavid Ahern 				kfree_skb(skb);
277616a16cd3SDavid Ahern 				skb = NULL;
277716a16cd3SDavid Ahern 			} else
277816a16cd3SDavid Ahern 				info->skip_notify = 1;
277916a16cd3SDavid Ahern 		}
278016a16cd3SDavid Ahern 
27810ae81335SDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
27820ae81335SDavid Ahern 					 &rt->rt6i_siblings,
27830ae81335SDavid Ahern 					 rt6i_siblings) {
27840ae81335SDavid Ahern 			err = fib6_del(sibling, info);
27850ae81335SDavid Ahern 			if (err)
2786e3330039SWANG Cong 				goto out_unlock;
27870ae81335SDavid Ahern 		}
27880ae81335SDavid Ahern 	}
27890ae81335SDavid Ahern 
27900ae81335SDavid Ahern 	err = fib6_del(rt, info);
2791e3330039SWANG Cong out_unlock:
27920ae81335SDavid Ahern 	write_unlock_bh(&table->tb6_lock);
2793e3330039SWANG Cong out_put:
27940ae81335SDavid Ahern 	ip6_rt_put(rt);
279516a16cd3SDavid Ahern 
279616a16cd3SDavid Ahern 	if (skb) {
2797e3330039SWANG Cong 		rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
279816a16cd3SDavid Ahern 			    info->nlh, gfp_any());
279916a16cd3SDavid Ahern 	}
28000ae81335SDavid Ahern 	return err;
28010ae81335SDavid Ahern }
28020ae81335SDavid Ahern 
2803333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg,
2804333c4301SDavid Ahern 			 struct netlink_ext_ack *extack)
28051da177e4SLinus Torvalds {
2806*2b760fcfSWei Wang 	struct rt6_info *rt, *rt_cache;
2807c71099acSThomas Graf 	struct fib6_table *table;
28081da177e4SLinus Torvalds 	struct fib6_node *fn;
28091da177e4SLinus Torvalds 	int err = -ESRCH;
28101da177e4SLinus Torvalds 
28115578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
2812d5d531cbSDavid Ahern 	if (!table) {
2813d5d531cbSDavid Ahern 		NL_SET_ERR_MSG(extack, "FIB table does not exist");
2814c71099acSThomas Graf 		return err;
2815d5d531cbSDavid Ahern 	}
28161da177e4SLinus Torvalds 
2817c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
2818c71099acSThomas Graf 
2819c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
282086872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
282138fbeeeeSWei Wang 			 &cfg->fc_src, cfg->fc_src_len,
2822*2b760fcfSWei Wang 			 !(cfg->fc_flags & RTF_CACHE));
28231da177e4SLinus Torvalds 
28241da177e4SLinus Torvalds 	if (fn) {
2825d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
2826*2b760fcfSWei Wang 			if (cfg->fc_flags & RTF_CACHE) {
2827*2b760fcfSWei Wang 				rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst,
2828*2b760fcfSWei Wang 							      &cfg->fc_src);
2829*2b760fcfSWei Wang 				if (!rt_cache)
28301f56a01fSMartin KaFai Lau 					continue;
2831*2b760fcfSWei Wang 				rt = rt_cache;
2832*2b760fcfSWei Wang 			}
283386872cb5SThomas Graf 			if (cfg->fc_ifindex &&
2834d1918542SDavid S. Miller 			    (!rt->dst.dev ||
2835d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
28361da177e4SLinus Torvalds 				continue;
283786872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
283886872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
28391da177e4SLinus Torvalds 				continue;
284086872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
28411da177e4SLinus Torvalds 				continue;
2842c2ed1880SMantas M 			if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol)
2843c2ed1880SMantas M 				continue;
2844d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
2845c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
28461da177e4SLinus Torvalds 
28470ae81335SDavid Ahern 			/* if gateway was specified only delete the one hop */
28480ae81335SDavid Ahern 			if (cfg->fc_flags & RTF_GATEWAY)
284986872cb5SThomas Graf 				return __ip6_del_rt(rt, &cfg->fc_nlinfo);
28500ae81335SDavid Ahern 
28510ae81335SDavid Ahern 			return __ip6_del_rt_siblings(rt, cfg);
28521da177e4SLinus Torvalds 		}
28531da177e4SLinus Torvalds 	}
2854c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
28551da177e4SLinus Torvalds 
28561da177e4SLinus Torvalds 	return err;
28571da177e4SLinus Torvalds }
28581da177e4SLinus Torvalds 
28596700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
2860a6279458SYOSHIFUJI Hideaki {
2861a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
2862e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
2863e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
2864e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
2865e8599ff4SDavid S. Miller 	struct neighbour *neigh;
286671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
28676e157b6aSDavid S. Miller 	int optlen, on_link;
28686e157b6aSDavid S. Miller 	u8 *lladdr;
2869e8599ff4SDavid S. Miller 
287029a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
287171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
2872e8599ff4SDavid S. Miller 
2873e8599ff4SDavid S. Miller 	if (optlen < 0) {
28746e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
2875e8599ff4SDavid S. Miller 		return;
2876e8599ff4SDavid S. Miller 	}
2877e8599ff4SDavid S. Miller 
287871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
2879e8599ff4SDavid S. Miller 
288071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
28816e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
2882e8599ff4SDavid S. Miller 		return;
2883e8599ff4SDavid S. Miller 	}
2884e8599ff4SDavid S. Miller 
28856e157b6aSDavid S. Miller 	on_link = 0;
288671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
2887e8599ff4SDavid S. Miller 		on_link = 1;
288871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
2889e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
28906e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
2891e8599ff4SDavid S. Miller 		return;
2892e8599ff4SDavid S. Miller 	}
2893e8599ff4SDavid S. Miller 
2894e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
2895e8599ff4SDavid S. Miller 	if (!in6_dev)
2896e8599ff4SDavid S. Miller 		return;
2897e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
2898e8599ff4SDavid S. Miller 		return;
2899e8599ff4SDavid S. Miller 
2900e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
2901e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
2902e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
2903e8599ff4SDavid S. Miller 	 */
2904e8599ff4SDavid S. Miller 
2905f997c55cSAlexander Aring 	if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) {
2906e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
2907e8599ff4SDavid S. Miller 		return;
2908e8599ff4SDavid S. Miller 	}
29096e157b6aSDavid S. Miller 
29106e157b6aSDavid S. Miller 	lladdr = NULL;
2911e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
2912e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
2913e8599ff4SDavid S. Miller 					     skb->dev);
2914e8599ff4SDavid S. Miller 		if (!lladdr) {
2915e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
2916e8599ff4SDavid S. Miller 			return;
2917e8599ff4SDavid S. Miller 		}
2918e8599ff4SDavid S. Miller 	}
2919e8599ff4SDavid S. Miller 
29206e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
2921ec13ad1dSMatthias Schiffer 	if (rt->rt6i_flags & RTF_REJECT) {
29226e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
29236e157b6aSDavid S. Miller 		return;
29246e157b6aSDavid S. Miller 	}
29256e157b6aSDavid S. Miller 
29266e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
29276e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
29286e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
29296e157b6aSDavid S. Miller 	 */
29300dec879fSJulian Anastasov 	dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr);
29316e157b6aSDavid S. Miller 
293271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
2933e8599ff4SDavid S. Miller 	if (!neigh)
2934e8599ff4SDavid S. Miller 		return;
2935e8599ff4SDavid S. Miller 
29361da177e4SLinus Torvalds 	/*
29371da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
29381da177e4SLinus Torvalds 	 */
29391da177e4SLinus Torvalds 
2940f997c55cSAlexander Aring 	ndisc_update(skb->dev, neigh, lladdr, NUD_STALE,
29411da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
29421da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
29431da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
2944f997c55cSAlexander Aring 				     NEIGH_UPDATE_F_ISROUTER)),
2945f997c55cSAlexander Aring 		     NDISC_REDIRECT, &ndopts);
29461da177e4SLinus Torvalds 
294783a09abdSMartin KaFai Lau 	nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL);
294838308473SDavid S. Miller 	if (!nrt)
29491da177e4SLinus Torvalds 		goto out;
29501da177e4SLinus Torvalds 
29511da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
29521da177e4SLinus Torvalds 	if (on_link)
29531da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
29541da177e4SLinus Torvalds 
2955b91d5329SXin Long 	nrt->rt6i_protocol = RTPROT_REDIRECT;
29564e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
29571da177e4SLinus Torvalds 
2958*2b760fcfSWei Wang 	/* No need to remove rt from the exception table if rt is
2959*2b760fcfSWei Wang 	 * a cached route because rt6_insert_exception() will
2960*2b760fcfSWei Wang 	 * takes care of it
2961*2b760fcfSWei Wang 	 */
2962*2b760fcfSWei Wang 	if (rt6_insert_exception(nrt, rt)) {
2963*2b760fcfSWei Wang 		dst_release_immediate(&nrt->dst);
2964*2b760fcfSWei Wang 		goto out;
2965*2b760fcfSWei Wang 	}
29661da177e4SLinus Torvalds 
2967d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
2968d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
296971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
297060592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
29718d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
29728d71740cSTom Tucker 
29731da177e4SLinus Torvalds out:
2974e8599ff4SDavid S. Miller 	neigh_release(neigh);
29756e157b6aSDavid S. Miller }
29766e157b6aSDavid S. Miller 
29771da177e4SLinus Torvalds /*
29781da177e4SLinus Torvalds  *	Misc support functions
29791da177e4SLinus Torvalds  */
29801da177e4SLinus Torvalds 
29814b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
29824b32b5adSMartin KaFai Lau {
29834b32b5adSMartin KaFai Lau 	BUG_ON(from->dst.from);
29844b32b5adSMartin KaFai Lau 
29854b32b5adSMartin KaFai Lau 	rt->rt6i_flags &= ~RTF_EXPIRES;
29864b32b5adSMartin KaFai Lau 	dst_hold(&from->dst);
29874b32b5adSMartin KaFai Lau 	rt->dst.from = &from->dst;
29884b32b5adSMartin KaFai Lau 	dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
29894b32b5adSMartin KaFai Lau }
29904b32b5adSMartin KaFai Lau 
299183a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort)
29921da177e4SLinus Torvalds {
2993d8d1f30bSChangli Gao 	rt->dst.input = ort->dst.input;
2994d8d1f30bSChangli Gao 	rt->dst.output = ort->dst.output;
299583a09abdSMartin KaFai Lau 	rt->rt6i_dst = ort->rt6i_dst;
2996d8d1f30bSChangli Gao 	rt->dst.error = ort->dst.error;
29971da177e4SLinus Torvalds 	rt->rt6i_idev = ort->rt6i_idev;
29981da177e4SLinus Torvalds 	if (rt->rt6i_idev)
29991da177e4SLinus Torvalds 		in6_dev_hold(rt->rt6i_idev);
3000d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
30014e3fd7a0SAlexey Dobriyan 	rt->rt6i_gateway = ort->rt6i_gateway;
30021716a961SGao feng 	rt->rt6i_flags = ort->rt6i_flags;
30031716a961SGao feng 	rt6_set_from(rt, ort);
300483a09abdSMartin KaFai Lau 	rt->rt6i_metric = ort->rt6i_metric;
30051da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
300683a09abdSMartin KaFai Lau 	rt->rt6i_src = ort->rt6i_src;
30071da177e4SLinus Torvalds #endif
300883a09abdSMartin KaFai Lau 	rt->rt6i_prefsrc = ort->rt6i_prefsrc;
3009c71099acSThomas Graf 	rt->rt6i_table = ort->rt6i_table;
301061adedf3SJiri Benc 	rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate);
30111da177e4SLinus Torvalds }
30121da177e4SLinus Torvalds 
301370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
3014efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
3015b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3016830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3017830218c1SDavid Ahern 					   struct net_device *dev)
301870ceb4f5SYOSHIFUJI Hideaki {
3019830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO;
3020830218c1SDavid Ahern 	int ifindex = dev->ifindex;
302170ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
302270ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
3023c71099acSThomas Graf 	struct fib6_table *table;
302470ceb4f5SYOSHIFUJI Hideaki 
3025830218c1SDavid Ahern 	table = fib6_get_table(net, tb_id);
302638308473SDavid S. Miller 	if (!table)
3027c71099acSThomas Graf 		return NULL;
3028c71099acSThomas Graf 
30295744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
303038fbeeeeSWei Wang 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true);
303170ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
303270ceb4f5SYOSHIFUJI Hideaki 		goto out;
303370ceb4f5SYOSHIFUJI Hideaki 
3034d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
3035d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
303670ceb4f5SYOSHIFUJI Hideaki 			continue;
303770ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
303870ceb4f5SYOSHIFUJI Hideaki 			continue;
303970ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
304070ceb4f5SYOSHIFUJI Hideaki 			continue;
3041d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
304270ceb4f5SYOSHIFUJI Hideaki 		break;
304370ceb4f5SYOSHIFUJI Hideaki 	}
304470ceb4f5SYOSHIFUJI Hideaki out:
30455744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
304670ceb4f5SYOSHIFUJI Hideaki 	return rt;
304770ceb4f5SYOSHIFUJI Hideaki }
304870ceb4f5SYOSHIFUJI Hideaki 
3049efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
3050b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
3051830218c1SDavid Ahern 					   const struct in6_addr *gwaddr,
3052830218c1SDavid Ahern 					   struct net_device *dev,
305395c96174SEric Dumazet 					   unsigned int pref)
305470ceb4f5SYOSHIFUJI Hideaki {
305586872cb5SThomas Graf 	struct fib6_config cfg = {
3056238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
3057830218c1SDavid Ahern 		.fc_ifindex	= dev->ifindex,
305886872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
305986872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
306086872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
3061b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
306215e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
3063efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3064efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
306586872cb5SThomas Graf 	};
306670ceb4f5SYOSHIFUJI Hideaki 
3067830218c1SDavid Ahern 	cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO,
30684e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
30694e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
307086872cb5SThomas Graf 
3071e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
3072e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
307386872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
307470ceb4f5SYOSHIFUJI Hideaki 
3075333c4301SDavid Ahern 	ip6_route_add(&cfg, NULL);
307670ceb4f5SYOSHIFUJI Hideaki 
3077830218c1SDavid Ahern 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev);
307870ceb4f5SYOSHIFUJI Hideaki }
307970ceb4f5SYOSHIFUJI Hideaki #endif
308070ceb4f5SYOSHIFUJI Hideaki 
3081b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
30821da177e4SLinus Torvalds {
3083830218c1SDavid Ahern 	u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT;
30841da177e4SLinus Torvalds 	struct rt6_info *rt;
3085c71099acSThomas Graf 	struct fib6_table *table;
30861da177e4SLinus Torvalds 
3087830218c1SDavid Ahern 	table = fib6_get_table(dev_net(dev), tb_id);
308838308473SDavid S. Miller 	if (!table)
3089c71099acSThomas Graf 		return NULL;
30901da177e4SLinus Torvalds 
30915744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
3092d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
3093d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
3094045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
30951da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
30961da177e4SLinus Torvalds 			break;
30971da177e4SLinus Torvalds 	}
30981da177e4SLinus Torvalds 	if (rt)
3099d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
31005744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
31011da177e4SLinus Torvalds 	return rt;
31021da177e4SLinus Torvalds }
31031da177e4SLinus Torvalds 
3104b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
3105ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
3106ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
31071da177e4SLinus Torvalds {
310886872cb5SThomas Graf 	struct fib6_config cfg = {
3109ca254490SDavid Ahern 		.fc_table	= l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT,
3110238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
311186872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
311286872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
311386872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
3114b91d5329SXin Long 		.fc_protocol = RTPROT_RA,
311515e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
31165578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
3117c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
311886872cb5SThomas Graf 	};
31191da177e4SLinus Torvalds 
31204e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
31211da177e4SLinus Torvalds 
3122333c4301SDavid Ahern 	if (!ip6_route_add(&cfg, NULL)) {
3123830218c1SDavid Ahern 		struct fib6_table *table;
3124830218c1SDavid Ahern 
3125830218c1SDavid Ahern 		table = fib6_get_table(dev_net(dev), cfg.fc_table);
3126830218c1SDavid Ahern 		if (table)
3127830218c1SDavid Ahern 			table->flags |= RT6_TABLE_HAS_DFLT_ROUTER;
3128830218c1SDavid Ahern 	}
31291da177e4SLinus Torvalds 
31301da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
31311da177e4SLinus Torvalds }
31321da177e4SLinus Torvalds 
3133830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table)
31341da177e4SLinus Torvalds {
31351da177e4SLinus Torvalds 	struct rt6_info *rt;
31361da177e4SLinus Torvalds 
31371da177e4SLinus Torvalds restart:
3138c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
3139d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
31403e8b0ac3SLorenzo Colitti 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
31413e8b0ac3SLorenzo Colitti 		    (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
3142d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
3143c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
3144e0a1ad73SThomas Graf 			ip6_del_rt(rt);
31451da177e4SLinus Torvalds 			goto restart;
31461da177e4SLinus Torvalds 		}
31471da177e4SLinus Torvalds 	}
3148c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
3149830218c1SDavid Ahern 
3150830218c1SDavid Ahern 	table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER;
3151830218c1SDavid Ahern }
3152830218c1SDavid Ahern 
3153830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net)
3154830218c1SDavid Ahern {
3155830218c1SDavid Ahern 	struct fib6_table *table;
3156830218c1SDavid Ahern 	struct hlist_head *head;
3157830218c1SDavid Ahern 	unsigned int h;
3158830218c1SDavid Ahern 
3159830218c1SDavid Ahern 	rcu_read_lock();
3160830218c1SDavid Ahern 
3161830218c1SDavid Ahern 	for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
3162830218c1SDavid Ahern 		head = &net->ipv6.fib_table_hash[h];
3163830218c1SDavid Ahern 		hlist_for_each_entry_rcu(table, head, tb6_hlist) {
3164830218c1SDavid Ahern 			if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER)
3165830218c1SDavid Ahern 				__rt6_purge_dflt_routers(table);
3166830218c1SDavid Ahern 		}
3167830218c1SDavid Ahern 	}
3168830218c1SDavid Ahern 
3169830218c1SDavid Ahern 	rcu_read_unlock();
31701da177e4SLinus Torvalds }
31711da177e4SLinus Torvalds 
31725578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
31735578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
317486872cb5SThomas Graf 				 struct fib6_config *cfg)
317586872cb5SThomas Graf {
317686872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
317786872cb5SThomas Graf 
3178ca254490SDavid Ahern 	cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ?
3179ca254490SDavid Ahern 			 : RT6_TABLE_MAIN;
318086872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
318186872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
318286872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
318386872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
318486872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
318586872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
318686872cb5SThomas Graf 
31875578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
3188f1243c2dSBenjamin Thery 
31894e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
31904e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
31914e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
319286872cb5SThomas Graf }
319386872cb5SThomas Graf 
31945578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
31951da177e4SLinus Torvalds {
319686872cb5SThomas Graf 	struct fib6_config cfg;
31971da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
31981da177e4SLinus Torvalds 	int err;
31991da177e4SLinus Torvalds 
32001da177e4SLinus Torvalds 	switch (cmd) {
32011da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
32021da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
3203af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
32041da177e4SLinus Torvalds 			return -EPERM;
32051da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
32061da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
32071da177e4SLinus Torvalds 		if (err)
32081da177e4SLinus Torvalds 			return -EFAULT;
32091da177e4SLinus Torvalds 
32105578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
321186872cb5SThomas Graf 
32121da177e4SLinus Torvalds 		rtnl_lock();
32131da177e4SLinus Torvalds 		switch (cmd) {
32141da177e4SLinus Torvalds 		case SIOCADDRT:
3215333c4301SDavid Ahern 			err = ip6_route_add(&cfg, NULL);
32161da177e4SLinus Torvalds 			break;
32171da177e4SLinus Torvalds 		case SIOCDELRT:
3218333c4301SDavid Ahern 			err = ip6_route_del(&cfg, NULL);
32191da177e4SLinus Torvalds 			break;
32201da177e4SLinus Torvalds 		default:
32211da177e4SLinus Torvalds 			err = -EINVAL;
32221da177e4SLinus Torvalds 		}
32231da177e4SLinus Torvalds 		rtnl_unlock();
32241da177e4SLinus Torvalds 
32251da177e4SLinus Torvalds 		return err;
32263ff50b79SStephen Hemminger 	}
32271da177e4SLinus Torvalds 
32281da177e4SLinus Torvalds 	return -EINVAL;
32291da177e4SLinus Torvalds }
32301da177e4SLinus Torvalds 
32311da177e4SLinus Torvalds /*
32321da177e4SLinus Torvalds  *	Drop the packet on the floor
32331da177e4SLinus Torvalds  */
32341da177e4SLinus Torvalds 
3235d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
32361da177e4SLinus Torvalds {
3237612f09e8SYOSHIFUJI Hideaki 	int type;
3238adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
3239612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
3240612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
32410660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
324245bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
32433bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
32443bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
3245612f09e8SYOSHIFUJI Hideaki 			break;
3246612f09e8SYOSHIFUJI Hideaki 		}
3247612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
3248612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
32493bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
32503bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
3251612f09e8SYOSHIFUJI Hideaki 		break;
3252612f09e8SYOSHIFUJI Hideaki 	}
32533ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
32541da177e4SLinus Torvalds 	kfree_skb(skb);
32551da177e4SLinus Torvalds 	return 0;
32561da177e4SLinus Torvalds }
32571da177e4SLinus Torvalds 
32589ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
32599ce8ade0SThomas Graf {
3260612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
32619ce8ade0SThomas Graf }
32629ce8ade0SThomas Graf 
3263ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
32641da177e4SLinus Torvalds {
3265adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3266612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
32671da177e4SLinus Torvalds }
32681da177e4SLinus Torvalds 
32699ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
32709ce8ade0SThomas Graf {
3271612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
32729ce8ade0SThomas Graf }
32739ce8ade0SThomas Graf 
3274ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb)
32759ce8ade0SThomas Graf {
3276adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
3277612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
32789ce8ade0SThomas Graf }
32799ce8ade0SThomas Graf 
32801da177e4SLinus Torvalds /*
32811da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
32821da177e4SLinus Torvalds  */
32831da177e4SLinus Torvalds 
32841da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
32851da177e4SLinus Torvalds 				    const struct in6_addr *addr,
32868f031519SDavid S. Miller 				    bool anycast)
32871da177e4SLinus Torvalds {
3288ca254490SDavid Ahern 	u32 tb_id;
3289c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
32904832c30dSDavid Ahern 	struct net_device *dev = idev->dev;
32915f02ce24SDavid Ahern 	struct rt6_info *rt;
32925f02ce24SDavid Ahern 
32935f02ce24SDavid Ahern 	rt = ip6_dst_alloc(net, dev, DST_NOCOUNT);
3294a3300ef4SHannes Frederic Sowa 	if (!rt)
32951da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
32961da177e4SLinus Torvalds 
32971da177e4SLinus Torvalds 	in6_dev_hold(idev);
32981da177e4SLinus Torvalds 
329911d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
3300d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
3301d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
33021da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
33031da177e4SLinus Torvalds 
330494b5e0f9SDavid Ahern 	rt->rt6i_protocol = RTPROT_KERNEL;
33051da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
330658c4fb86SYOSHIFUJI Hideaki 	if (anycast)
330758c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
330858c4fb86SYOSHIFUJI Hideaki 	else
33091da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
33101da177e4SLinus Torvalds 
3311550bab42SJulian Anastasov 	rt->rt6i_gateway  = *addr;
33124e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
33131da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
3314ca254490SDavid Ahern 	tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL;
3315ca254490SDavid Ahern 	rt->rt6i_table = fib6_get_table(net, tb_id);
33161da177e4SLinus Torvalds 
33171da177e4SLinus Torvalds 	return rt;
33181da177e4SLinus Torvalds }
33191da177e4SLinus Torvalds 
3320c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
3321c3968a85SDaniel Walter struct arg_dev_net_ip {
3322c3968a85SDaniel Walter 	struct net_device *dev;
3323c3968a85SDaniel Walter 	struct net *net;
3324c3968a85SDaniel Walter 	struct in6_addr *addr;
3325c3968a85SDaniel Walter };
3326c3968a85SDaniel Walter 
3327c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
3328c3968a85SDaniel Walter {
3329c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
3330c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
3331c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
3332c3968a85SDaniel Walter 
3333d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
3334c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
3335c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
333660006a48SWei Wang 		spin_lock_bh(&rt6_exception_lock);
3337c3968a85SDaniel Walter 		/* remove prefsrc entry */
3338c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
333960006a48SWei Wang 		/* need to update cache as well */
334060006a48SWei Wang 		rt6_exceptions_remove_prefsrc(rt);
334160006a48SWei Wang 		spin_unlock_bh(&rt6_exception_lock);
3342c3968a85SDaniel Walter 	}
3343c3968a85SDaniel Walter 	return 0;
3344c3968a85SDaniel Walter }
3345c3968a85SDaniel Walter 
3346c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
3347c3968a85SDaniel Walter {
3348c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
3349c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
3350c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
3351c3968a85SDaniel Walter 		.net = net,
3352c3968a85SDaniel Walter 		.addr = &ifp->addr,
3353c3968a85SDaniel Walter 	};
33540c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
3355c3968a85SDaniel Walter }
3356c3968a85SDaniel Walter 
3357be7a010dSDuan Jiong #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
3358be7a010dSDuan Jiong 
3359be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
3360be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
3361be7a010dSDuan Jiong {
3362be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
3363be7a010dSDuan Jiong 
3364*2b760fcfSWei Wang 	if (((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) &&
3365be7a010dSDuan Jiong 	    ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
3366be7a010dSDuan Jiong 		return -1;
3367be7a010dSDuan Jiong 	}
3368b16cb459SWei Wang 
3369b16cb459SWei Wang 	/* Further clean up cached routes in exception table.
3370b16cb459SWei Wang 	 * This is needed because cached route may have a different
3371b16cb459SWei Wang 	 * gateway than its 'parent' in the case of an ip redirect.
3372b16cb459SWei Wang 	 */
3373b16cb459SWei Wang 	rt6_exceptions_clean_tohost(rt, gateway);
3374b16cb459SWei Wang 
3375be7a010dSDuan Jiong 	return 0;
3376be7a010dSDuan Jiong }
3377be7a010dSDuan Jiong 
3378be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
3379be7a010dSDuan Jiong {
3380be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
3381be7a010dSDuan Jiong }
3382be7a010dSDuan Jiong 
33838ed67789SDaniel Lezcano struct arg_dev_net {
33848ed67789SDaniel Lezcano 	struct net_device *dev;
33858ed67789SDaniel Lezcano 	struct net *net;
33868ed67789SDaniel Lezcano };
33878ed67789SDaniel Lezcano 
3388a1a22c12SDavid Ahern /* called with write lock held for table with rt */
33891da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
33901da177e4SLinus Torvalds {
3391bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
3392bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
33938ed67789SDaniel Lezcano 
3394d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
3395a1a22c12SDavid Ahern 	    rt != adn->net->ipv6.ip6_null_entry &&
3396a1a22c12SDavid Ahern 	    (rt->rt6i_nsiblings == 0 ||
33978397ed36SDavid Ahern 	     (dev && netdev_unregistering(dev)) ||
3398a1a22c12SDavid Ahern 	     !rt->rt6i_idev->cnf.ignore_routes_with_linkdown))
33991da177e4SLinus Torvalds 		return -1;
3400c159d30cSDavid S. Miller 
34011da177e4SLinus Torvalds 	return 0;
34021da177e4SLinus Torvalds }
34031da177e4SLinus Torvalds 
3404f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
34051da177e4SLinus Torvalds {
34068ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
34078ed67789SDaniel Lezcano 		.dev = dev,
34088ed67789SDaniel Lezcano 		.net = net,
34098ed67789SDaniel Lezcano 	};
34108ed67789SDaniel Lezcano 
34110c3584d5SLi RongQing 	fib6_clean_all(net, fib6_ifdown, &adn);
3412e332bc67SEric W. Biederman 	if (dev)
34138d0b94afSMartin KaFai Lau 		rt6_uncached_list_flush_dev(net, dev);
34141da177e4SLinus Torvalds }
34151da177e4SLinus Torvalds 
341695c96174SEric Dumazet struct rt6_mtu_change_arg {
34171da177e4SLinus Torvalds 	struct net_device *dev;
341895c96174SEric Dumazet 	unsigned int mtu;
34191da177e4SLinus Torvalds };
34201da177e4SLinus Torvalds 
34211da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
34221da177e4SLinus Torvalds {
34231da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
34241da177e4SLinus Torvalds 	struct inet6_dev *idev;
34251da177e4SLinus Torvalds 
34261da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
34271da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
34281da177e4SLinus Torvalds 	   We still use this lock to block changes
34291da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
34301da177e4SLinus Torvalds 	*/
34311da177e4SLinus Torvalds 
34321da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
343338308473SDavid S. Miller 	if (!idev)
34341da177e4SLinus Torvalds 		return 0;
34351da177e4SLinus Torvalds 
34361da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
34371da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
34381da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
34391da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
34401da177e4SLinus Torvalds 	 */
34411da177e4SLinus Torvalds 	/*
34421da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
34431da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
34441da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
34451da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
34461da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
34471da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
344867c408cfSAlexander Alemayhu 	   PMTU discovery.
34491da177e4SLinus Torvalds 	 */
3450d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
3451fb56be83SMaciej Żenczykowski 	    dst_metric_raw(&rt->dst, RTAX_MTU) &&
34524b32b5adSMartin KaFai Lau 	    !dst_metric_locked(&rt->dst, RTAX_MTU)) {
3453f5bbe7eeSWei Wang 		spin_lock_bh(&rt6_exception_lock);
3454*2b760fcfSWei Wang 		if (dst_mtu(&rt->dst) >= arg->mtu ||
3455d8d1f30bSChangli Gao 		    (dst_mtu(&rt->dst) < arg->mtu &&
34564b32b5adSMartin KaFai Lau 		     dst_mtu(&rt->dst) == idev->cnf.mtu6)) {
3457defb3519SDavid S. Miller 			dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
3458566cfd8fSSimon Arlott 		}
3459f5bbe7eeSWei Wang 		rt6_exceptions_update_pmtu(rt, arg->mtu);
3460f5bbe7eeSWei Wang 		spin_unlock_bh(&rt6_exception_lock);
34614b32b5adSMartin KaFai Lau 	}
34621da177e4SLinus Torvalds 	return 0;
34631da177e4SLinus Torvalds }
34641da177e4SLinus Torvalds 
346595c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
34661da177e4SLinus Torvalds {
3467c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
3468c71099acSThomas Graf 		.dev = dev,
3469c71099acSThomas Graf 		.mtu = mtu,
3470c71099acSThomas Graf 	};
34711da177e4SLinus Torvalds 
34720c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
34731da177e4SLinus Torvalds }
34741da177e4SLinus Torvalds 
3475ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
34765176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
347786872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
3478ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
347986872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
348086872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
348151ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
3482c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
348319e42e45SRoopa Prabhu 	[RTA_ENCAP_TYPE]	= { .type = NLA_U16 },
348419e42e45SRoopa Prabhu 	[RTA_ENCAP]		= { .type = NLA_NESTED },
348532bc201eSXin Long 	[RTA_EXPIRES]		= { .type = NLA_U32 },
3486622ec2c9SLorenzo Colitti 	[RTA_UID]		= { .type = NLA_U32 },
34873b45a410SLiping Zhang 	[RTA_MARK]		= { .type = NLA_U32 },
348886872cb5SThomas Graf };
348986872cb5SThomas Graf 
349086872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
3491333c4301SDavid Ahern 			      struct fib6_config *cfg,
3492333c4301SDavid Ahern 			      struct netlink_ext_ack *extack)
34931da177e4SLinus Torvalds {
349486872cb5SThomas Graf 	struct rtmsg *rtm;
349586872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
3496c78ba6d6SLubomir Rintel 	unsigned int pref;
349786872cb5SThomas Graf 	int err;
34981da177e4SLinus Torvalds 
3499fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
3500fceb6435SJohannes Berg 			  NULL);
350186872cb5SThomas Graf 	if (err < 0)
350286872cb5SThomas Graf 		goto errout;
35031da177e4SLinus Torvalds 
350486872cb5SThomas Graf 	err = -EINVAL;
350586872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
350686872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
350786872cb5SThomas Graf 
350886872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
350986872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
351086872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
351186872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
351286872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
3513ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
351486872cb5SThomas Graf 
3515ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
3516ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
3517b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
3518b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
351986872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
352086872cb5SThomas Graf 
3521ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
3522ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
3523ab79ad14SMaciej Żenczykowski 
35241f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
35251f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
35261f56a01fSMartin KaFai Lau 
352715e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
352886872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
35293b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
353086872cb5SThomas Graf 
353186872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
353267b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
353386872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
35341da177e4SLinus Torvalds 	}
353586872cb5SThomas Graf 
353686872cb5SThomas Graf 	if (tb[RTA_DST]) {
353786872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
353886872cb5SThomas Graf 
353986872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
354086872cb5SThomas Graf 			goto errout;
354186872cb5SThomas Graf 
354286872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
35431da177e4SLinus Torvalds 	}
354486872cb5SThomas Graf 
354586872cb5SThomas Graf 	if (tb[RTA_SRC]) {
354686872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
354786872cb5SThomas Graf 
354886872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
354986872cb5SThomas Graf 			goto errout;
355086872cb5SThomas Graf 
355186872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
35521da177e4SLinus Torvalds 	}
355386872cb5SThomas Graf 
3554c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
355567b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
3556c3968a85SDaniel Walter 
355786872cb5SThomas Graf 	if (tb[RTA_OIF])
355886872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
355986872cb5SThomas Graf 
356086872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
356186872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
356286872cb5SThomas Graf 
356386872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
356486872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
356586872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
35661da177e4SLinus Torvalds 	}
356786872cb5SThomas Graf 
356886872cb5SThomas Graf 	if (tb[RTA_TABLE])
356986872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
357086872cb5SThomas Graf 
357151ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
357251ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
357351ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
35749ed59592SDavid Ahern 
35759ed59592SDavid Ahern 		err = lwtunnel_valid_encap_type_attr(cfg->fc_mp,
3576c255bd68SDavid Ahern 						     cfg->fc_mp_len, extack);
35779ed59592SDavid Ahern 		if (err < 0)
35789ed59592SDavid Ahern 			goto errout;
357951ebd318SNicolas Dichtel 	}
358051ebd318SNicolas Dichtel 
3581c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
3582c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
3583c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
3584c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
3585c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
3586c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
3587c78ba6d6SLubomir Rintel 	}
3588c78ba6d6SLubomir Rintel 
358919e42e45SRoopa Prabhu 	if (tb[RTA_ENCAP])
359019e42e45SRoopa Prabhu 		cfg->fc_encap = tb[RTA_ENCAP];
359119e42e45SRoopa Prabhu 
35929ed59592SDavid Ahern 	if (tb[RTA_ENCAP_TYPE]) {
359319e42e45SRoopa Prabhu 		cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]);
359419e42e45SRoopa Prabhu 
3595c255bd68SDavid Ahern 		err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack);
35969ed59592SDavid Ahern 		if (err < 0)
35979ed59592SDavid Ahern 			goto errout;
35989ed59592SDavid Ahern 	}
35999ed59592SDavid Ahern 
360032bc201eSXin Long 	if (tb[RTA_EXPIRES]) {
360132bc201eSXin Long 		unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ);
360232bc201eSXin Long 
360332bc201eSXin Long 		if (addrconf_finite_timeout(timeout)) {
360432bc201eSXin Long 			cfg->fc_expires = jiffies_to_clock_t(timeout * HZ);
360532bc201eSXin Long 			cfg->fc_flags |= RTF_EXPIRES;
360632bc201eSXin Long 		}
360732bc201eSXin Long 	}
360832bc201eSXin Long 
360986872cb5SThomas Graf 	err = 0;
361086872cb5SThomas Graf errout:
361186872cb5SThomas Graf 	return err;
36121da177e4SLinus Torvalds }
36131da177e4SLinus Torvalds 
36146b9ea5a6SRoopa Prabhu struct rt6_nh {
36156b9ea5a6SRoopa Prabhu 	struct rt6_info *rt6_info;
36166b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
36176b9ea5a6SRoopa Prabhu 	struct mx6_config mxc;
36186b9ea5a6SRoopa Prabhu 	struct list_head next;
36196b9ea5a6SRoopa Prabhu };
36206b9ea5a6SRoopa Prabhu 
36216b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list)
36226b9ea5a6SRoopa Prabhu {
36236b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
36246b9ea5a6SRoopa Prabhu 
36256b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
36267d4d5065SDavid Ahern 		pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n",
36276b9ea5a6SRoopa Prabhu 		        &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway,
36286b9ea5a6SRoopa Prabhu 		        nh->r_cfg.fc_ifindex);
36296b9ea5a6SRoopa Prabhu 	}
36306b9ea5a6SRoopa Prabhu }
36316b9ea5a6SRoopa Prabhu 
36326b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list,
36336b9ea5a6SRoopa Prabhu 				 struct rt6_info *rt, struct fib6_config *r_cfg)
36346b9ea5a6SRoopa Prabhu {
36356b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh;
36366b9ea5a6SRoopa Prabhu 	int err = -EEXIST;
36376b9ea5a6SRoopa Prabhu 
36386b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, rt6_nh_list, next) {
36396b9ea5a6SRoopa Prabhu 		/* check if rt6_info already exists */
3640f06b7549SDavid Ahern 		if (rt6_duplicate_nexthop(nh->rt6_info, rt))
36416b9ea5a6SRoopa Prabhu 			return err;
36426b9ea5a6SRoopa Prabhu 	}
36436b9ea5a6SRoopa Prabhu 
36446b9ea5a6SRoopa Prabhu 	nh = kzalloc(sizeof(*nh), GFP_KERNEL);
36456b9ea5a6SRoopa Prabhu 	if (!nh)
36466b9ea5a6SRoopa Prabhu 		return -ENOMEM;
36476b9ea5a6SRoopa Prabhu 	nh->rt6_info = rt;
36486b9ea5a6SRoopa Prabhu 	err = ip6_convert_metrics(&nh->mxc, r_cfg);
36496b9ea5a6SRoopa Prabhu 	if (err) {
36506b9ea5a6SRoopa Prabhu 		kfree(nh);
36516b9ea5a6SRoopa Prabhu 		return err;
36526b9ea5a6SRoopa Prabhu 	}
36536b9ea5a6SRoopa Prabhu 	memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
36546b9ea5a6SRoopa Prabhu 	list_add_tail(&nh->next, rt6_nh_list);
36556b9ea5a6SRoopa Prabhu 
36566b9ea5a6SRoopa Prabhu 	return 0;
36576b9ea5a6SRoopa Prabhu }
36586b9ea5a6SRoopa Prabhu 
36593b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt,
36603b1137feSDavid Ahern 				   struct rt6_info *rt_last,
36613b1137feSDavid Ahern 				   struct nl_info *info,
36623b1137feSDavid Ahern 				   __u16 nlflags)
36633b1137feSDavid Ahern {
36643b1137feSDavid Ahern 	/* if this is an APPEND route, then rt points to the first route
36653b1137feSDavid Ahern 	 * inserted and rt_last points to last route inserted. Userspace
36663b1137feSDavid Ahern 	 * wants a consistent dump of the route which starts at the first
36673b1137feSDavid Ahern 	 * nexthop. Since sibling routes are always added at the end of
36683b1137feSDavid Ahern 	 * the list, find the first sibling of the last route appended
36693b1137feSDavid Ahern 	 */
36703b1137feSDavid Ahern 	if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) {
36713b1137feSDavid Ahern 		rt = list_first_entry(&rt_last->rt6i_siblings,
36723b1137feSDavid Ahern 				      struct rt6_info,
36733b1137feSDavid Ahern 				      rt6i_siblings);
36743b1137feSDavid Ahern 	}
36753b1137feSDavid Ahern 
36763b1137feSDavid Ahern 	if (rt)
36773b1137feSDavid Ahern 		inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
36783b1137feSDavid Ahern }
36793b1137feSDavid Ahern 
3680333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg,
3681333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
368251ebd318SNicolas Dichtel {
36833b1137feSDavid Ahern 	struct rt6_info *rt_notif = NULL, *rt_last = NULL;
36843b1137feSDavid Ahern 	struct nl_info *info = &cfg->fc_nlinfo;
368551ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
368651ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
36876b9ea5a6SRoopa Prabhu 	struct rt6_info *rt;
36886b9ea5a6SRoopa Prabhu 	struct rt6_nh *err_nh;
36896b9ea5a6SRoopa Prabhu 	struct rt6_nh *nh, *nh_safe;
36903b1137feSDavid Ahern 	__u16 nlflags;
369151ebd318SNicolas Dichtel 	int remaining;
369251ebd318SNicolas Dichtel 	int attrlen;
36936b9ea5a6SRoopa Prabhu 	int err = 1;
36946b9ea5a6SRoopa Prabhu 	int nhn = 0;
36956b9ea5a6SRoopa Prabhu 	int replace = (cfg->fc_nlinfo.nlh &&
36966b9ea5a6SRoopa Prabhu 		       (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
36976b9ea5a6SRoopa Prabhu 	LIST_HEAD(rt6_nh_list);
369851ebd318SNicolas Dichtel 
36993b1137feSDavid Ahern 	nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
37003b1137feSDavid Ahern 	if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
37013b1137feSDavid Ahern 		nlflags |= NLM_F_APPEND;
37023b1137feSDavid Ahern 
370335f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
370451ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
370551ebd318SNicolas Dichtel 
37066b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry and build a list (rt6_nh_list) of
37076b9ea5a6SRoopa Prabhu 	 * rt6_info structs per nexthop
37086b9ea5a6SRoopa Prabhu 	 */
370951ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
371051ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
371151ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
371251ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
371351ebd318SNicolas Dichtel 
371451ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
371551ebd318SNicolas Dichtel 		if (attrlen > 0) {
371651ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
371751ebd318SNicolas Dichtel 
371851ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
371951ebd318SNicolas Dichtel 			if (nla) {
372067b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
372151ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
372251ebd318SNicolas Dichtel 			}
372319e42e45SRoopa Prabhu 			r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
372419e42e45SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
372519e42e45SRoopa Prabhu 			if (nla)
372619e42e45SRoopa Prabhu 				r_cfg.fc_encap_type = nla_get_u16(nla);
372751ebd318SNicolas Dichtel 		}
37286b9ea5a6SRoopa Prabhu 
3729333c4301SDavid Ahern 		rt = ip6_route_info_create(&r_cfg, extack);
37308c5b83f0SRoopa Prabhu 		if (IS_ERR(rt)) {
37318c5b83f0SRoopa Prabhu 			err = PTR_ERR(rt);
37328c5b83f0SRoopa Prabhu 			rt = NULL;
37336b9ea5a6SRoopa Prabhu 			goto cleanup;
37348c5b83f0SRoopa Prabhu 		}
37356b9ea5a6SRoopa Prabhu 
37366b9ea5a6SRoopa Prabhu 		err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg);
373751ebd318SNicolas Dichtel 		if (err) {
3738587fea74SWei Wang 			dst_release_immediate(&rt->dst);
37396b9ea5a6SRoopa Prabhu 			goto cleanup;
374051ebd318SNicolas Dichtel 		}
37416b9ea5a6SRoopa Prabhu 
37426b9ea5a6SRoopa Prabhu 		rtnh = rtnh_next(rtnh, &remaining);
374351ebd318SNicolas Dichtel 	}
37446b9ea5a6SRoopa Prabhu 
37453b1137feSDavid Ahern 	/* for add and replace send one notification with all nexthops.
37463b1137feSDavid Ahern 	 * Skip the notification in fib6_add_rt2node and send one with
37473b1137feSDavid Ahern 	 * the full route when done
37483b1137feSDavid Ahern 	 */
37493b1137feSDavid Ahern 	info->skip_notify = 1;
37503b1137feSDavid Ahern 
37516b9ea5a6SRoopa Prabhu 	err_nh = NULL;
37526b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
37533b1137feSDavid Ahern 		rt_last = nh->rt6_info;
3754333c4301SDavid Ahern 		err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack);
37553b1137feSDavid Ahern 		/* save reference to first route for notification */
37563b1137feSDavid Ahern 		if (!rt_notif && !err)
37573b1137feSDavid Ahern 			rt_notif = nh->rt6_info;
37583b1137feSDavid Ahern 
37596b9ea5a6SRoopa Prabhu 		/* nh->rt6_info is used or freed at this point, reset to NULL*/
37606b9ea5a6SRoopa Prabhu 		nh->rt6_info = NULL;
37616b9ea5a6SRoopa Prabhu 		if (err) {
37626b9ea5a6SRoopa Prabhu 			if (replace && nhn)
37636b9ea5a6SRoopa Prabhu 				ip6_print_replace_route_err(&rt6_nh_list);
37646b9ea5a6SRoopa Prabhu 			err_nh = nh;
37656b9ea5a6SRoopa Prabhu 			goto add_errout;
37666b9ea5a6SRoopa Prabhu 		}
37676b9ea5a6SRoopa Prabhu 
37681a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
376927596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
377027596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
377127596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
377227596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
377327596472SMichal Kubeček 		 * be added to it.
37741a72418bSNicolas Dichtel 		 */
377527596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
377627596472SMichal Kubeček 						     NLM_F_REPLACE);
37776b9ea5a6SRoopa Prabhu 		nhn++;
37786b9ea5a6SRoopa Prabhu 	}
37796b9ea5a6SRoopa Prabhu 
37803b1137feSDavid Ahern 	/* success ... tell user about new route */
37813b1137feSDavid Ahern 	ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
37826b9ea5a6SRoopa Prabhu 	goto cleanup;
37836b9ea5a6SRoopa Prabhu 
37846b9ea5a6SRoopa Prabhu add_errout:
37853b1137feSDavid Ahern 	/* send notification for routes that were added so that
37863b1137feSDavid Ahern 	 * the delete notifications sent by ip6_route_del are
37873b1137feSDavid Ahern 	 * coherent
37883b1137feSDavid Ahern 	 */
37893b1137feSDavid Ahern 	if (rt_notif)
37903b1137feSDavid Ahern 		ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags);
37913b1137feSDavid Ahern 
37926b9ea5a6SRoopa Prabhu 	/* Delete routes that were already added */
37936b9ea5a6SRoopa Prabhu 	list_for_each_entry(nh, &rt6_nh_list, next) {
37946b9ea5a6SRoopa Prabhu 		if (err_nh == nh)
37956b9ea5a6SRoopa Prabhu 			break;
3796333c4301SDavid Ahern 		ip6_route_del(&nh->r_cfg, extack);
37976b9ea5a6SRoopa Prabhu 	}
37986b9ea5a6SRoopa Prabhu 
37996b9ea5a6SRoopa Prabhu cleanup:
38006b9ea5a6SRoopa Prabhu 	list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) {
3801587fea74SWei Wang 		if (nh->rt6_info)
3802587fea74SWei Wang 			dst_release_immediate(&nh->rt6_info->dst);
38036b9ea5a6SRoopa Prabhu 		kfree(nh->mxc.mx);
38046b9ea5a6SRoopa Prabhu 		list_del(&nh->next);
38056b9ea5a6SRoopa Prabhu 		kfree(nh);
38066b9ea5a6SRoopa Prabhu 	}
38076b9ea5a6SRoopa Prabhu 
38086b9ea5a6SRoopa Prabhu 	return err;
38096b9ea5a6SRoopa Prabhu }
38106b9ea5a6SRoopa Prabhu 
3811333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg,
3812333c4301SDavid Ahern 				   struct netlink_ext_ack *extack)
38136b9ea5a6SRoopa Prabhu {
38146b9ea5a6SRoopa Prabhu 	struct fib6_config r_cfg;
38156b9ea5a6SRoopa Prabhu 	struct rtnexthop *rtnh;
38166b9ea5a6SRoopa Prabhu 	int remaining;
38176b9ea5a6SRoopa Prabhu 	int attrlen;
38186b9ea5a6SRoopa Prabhu 	int err = 1, last_err = 0;
38196b9ea5a6SRoopa Prabhu 
38206b9ea5a6SRoopa Prabhu 	remaining = cfg->fc_mp_len;
38216b9ea5a6SRoopa Prabhu 	rtnh = (struct rtnexthop *)cfg->fc_mp;
38226b9ea5a6SRoopa Prabhu 
38236b9ea5a6SRoopa Prabhu 	/* Parse a Multipath Entry */
38246b9ea5a6SRoopa Prabhu 	while (rtnh_ok(rtnh, remaining)) {
38256b9ea5a6SRoopa Prabhu 		memcpy(&r_cfg, cfg, sizeof(*cfg));
38266b9ea5a6SRoopa Prabhu 		if (rtnh->rtnh_ifindex)
38276b9ea5a6SRoopa Prabhu 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
38286b9ea5a6SRoopa Prabhu 
38296b9ea5a6SRoopa Prabhu 		attrlen = rtnh_attrlen(rtnh);
38306b9ea5a6SRoopa Prabhu 		if (attrlen > 0) {
38316b9ea5a6SRoopa Prabhu 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
38326b9ea5a6SRoopa Prabhu 
38336b9ea5a6SRoopa Prabhu 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
38346b9ea5a6SRoopa Prabhu 			if (nla) {
38356b9ea5a6SRoopa Prabhu 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
38366b9ea5a6SRoopa Prabhu 				r_cfg.fc_flags |= RTF_GATEWAY;
38376b9ea5a6SRoopa Prabhu 			}
38386b9ea5a6SRoopa Prabhu 		}
3839333c4301SDavid Ahern 		err = ip6_route_del(&r_cfg, extack);
38406b9ea5a6SRoopa Prabhu 		if (err)
38416b9ea5a6SRoopa Prabhu 			last_err = err;
38426b9ea5a6SRoopa Prabhu 
384351ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
384451ebd318SNicolas Dichtel 	}
384551ebd318SNicolas Dichtel 
384651ebd318SNicolas Dichtel 	return last_err;
384751ebd318SNicolas Dichtel }
384851ebd318SNicolas Dichtel 
3849c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
3850c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
38511da177e4SLinus Torvalds {
385286872cb5SThomas Graf 	struct fib6_config cfg;
385386872cb5SThomas Graf 	int err;
38541da177e4SLinus Torvalds 
3855333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
385686872cb5SThomas Graf 	if (err < 0)
385786872cb5SThomas Graf 		return err;
385886872cb5SThomas Graf 
385951ebd318SNicolas Dichtel 	if (cfg.fc_mp)
3860333c4301SDavid Ahern 		return ip6_route_multipath_del(&cfg, extack);
38610ae81335SDavid Ahern 	else {
38620ae81335SDavid Ahern 		cfg.fc_delete_all_nh = 1;
3863333c4301SDavid Ahern 		return ip6_route_del(&cfg, extack);
38641da177e4SLinus Torvalds 	}
38650ae81335SDavid Ahern }
38661da177e4SLinus Torvalds 
3867c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
3868c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
38691da177e4SLinus Torvalds {
387086872cb5SThomas Graf 	struct fib6_config cfg;
387186872cb5SThomas Graf 	int err;
38721da177e4SLinus Torvalds 
3873333c4301SDavid Ahern 	err = rtm_to_fib6_config(skb, nlh, &cfg, extack);
387486872cb5SThomas Graf 	if (err < 0)
387586872cb5SThomas Graf 		return err;
387686872cb5SThomas Graf 
387751ebd318SNicolas Dichtel 	if (cfg.fc_mp)
3878333c4301SDavid Ahern 		return ip6_route_multipath_add(&cfg, extack);
387951ebd318SNicolas Dichtel 	else
3880333c4301SDavid Ahern 		return ip6_route_add(&cfg, extack);
38811da177e4SLinus Torvalds }
38821da177e4SLinus Torvalds 
3883beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt)
3884339bf98fSThomas Graf {
3885beb1afacSDavid Ahern 	int nexthop_len = 0;
3886beb1afacSDavid Ahern 
3887beb1afacSDavid Ahern 	if (rt->rt6i_nsiblings) {
3888beb1afacSDavid Ahern 		nexthop_len = nla_total_size(0)	 /* RTA_MULTIPATH */
3889beb1afacSDavid Ahern 			    + NLA_ALIGN(sizeof(struct rtnexthop))
3890beb1afacSDavid Ahern 			    + nla_total_size(16) /* RTA_GATEWAY */
3891beb1afacSDavid Ahern 			    + lwtunnel_get_encap_size(rt->dst.lwtstate);
3892beb1afacSDavid Ahern 
3893beb1afacSDavid Ahern 		nexthop_len *= rt->rt6i_nsiblings;
3894beb1afacSDavid Ahern 	}
3895beb1afacSDavid Ahern 
3896339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
3897339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
3898339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
3899339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
3900339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
3901339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
3902339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
3903339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
3904339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
39056a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
3906ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
3907c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
390819e42e45SRoopa Prabhu 	       + nla_total_size(1) /* RTA_PREF */
3909beb1afacSDavid Ahern 	       + lwtunnel_get_encap_size(rt->dst.lwtstate)
3910beb1afacSDavid Ahern 	       + nexthop_len;
3911beb1afacSDavid Ahern }
3912beb1afacSDavid Ahern 
3913beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt,
39145be083ceSDavid Ahern 			    unsigned int *flags, bool skip_oif)
3915beb1afacSDavid Ahern {
3916beb1afacSDavid Ahern 	if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) {
3917beb1afacSDavid Ahern 		*flags |= RTNH_F_LINKDOWN;
3918beb1afacSDavid Ahern 		if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown)
3919beb1afacSDavid Ahern 			*flags |= RTNH_F_DEAD;
3920beb1afacSDavid Ahern 	}
3921beb1afacSDavid Ahern 
3922beb1afacSDavid Ahern 	if (rt->rt6i_flags & RTF_GATEWAY) {
3923beb1afacSDavid Ahern 		if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
3924beb1afacSDavid Ahern 			goto nla_put_failure;
3925beb1afacSDavid Ahern 	}
3926beb1afacSDavid Ahern 
3927fe400799SIdo Schimmel 	if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD)
392861e4d01eSIdo Schimmel 		*flags |= RTNH_F_OFFLOAD;
392961e4d01eSIdo Schimmel 
39305be083ceSDavid Ahern 	/* not needed for multipath encoding b/c it has a rtnexthop struct */
39315be083ceSDavid Ahern 	if (!skip_oif && rt->dst.dev &&
3932beb1afacSDavid Ahern 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
3933beb1afacSDavid Ahern 		goto nla_put_failure;
3934beb1afacSDavid Ahern 
3935beb1afacSDavid Ahern 	if (rt->dst.lwtstate &&
3936beb1afacSDavid Ahern 	    lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0)
3937beb1afacSDavid Ahern 		goto nla_put_failure;
3938beb1afacSDavid Ahern 
3939beb1afacSDavid Ahern 	return 0;
3940beb1afacSDavid Ahern 
3941beb1afacSDavid Ahern nla_put_failure:
3942beb1afacSDavid Ahern 	return -EMSGSIZE;
3943beb1afacSDavid Ahern }
3944beb1afacSDavid Ahern 
39455be083ceSDavid Ahern /* add multipath next hop */
3946beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt)
3947beb1afacSDavid Ahern {
3948beb1afacSDavid Ahern 	struct rtnexthop *rtnh;
3949beb1afacSDavid Ahern 	unsigned int flags = 0;
3950beb1afacSDavid Ahern 
3951beb1afacSDavid Ahern 	rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
3952beb1afacSDavid Ahern 	if (!rtnh)
3953beb1afacSDavid Ahern 		goto nla_put_failure;
3954beb1afacSDavid Ahern 
3955beb1afacSDavid Ahern 	rtnh->rtnh_hops = 0;
3956beb1afacSDavid Ahern 	rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0;
3957beb1afacSDavid Ahern 
39585be083ceSDavid Ahern 	if (rt6_nexthop_info(skb, rt, &flags, true) < 0)
3959beb1afacSDavid Ahern 		goto nla_put_failure;
3960beb1afacSDavid Ahern 
3961beb1afacSDavid Ahern 	rtnh->rtnh_flags = flags;
3962beb1afacSDavid Ahern 
3963beb1afacSDavid Ahern 	/* length of rtnetlink header + attributes */
3964beb1afacSDavid Ahern 	rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
3965beb1afacSDavid Ahern 
3966beb1afacSDavid Ahern 	return 0;
3967beb1afacSDavid Ahern 
3968beb1afacSDavid Ahern nla_put_failure:
3969beb1afacSDavid Ahern 	return -EMSGSIZE;
3970339bf98fSThomas Graf }
3971339bf98fSThomas Graf 
3972191cd582SBrian Haley static int rt6_fill_node(struct net *net,
3973191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
39740d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
397515e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
3976f8cfe2ceSDavid Ahern 			 unsigned int flags)
39771da177e4SLinus Torvalds {
39784b32b5adSMartin KaFai Lau 	u32 metrics[RTAX_MAX];
39791da177e4SLinus Torvalds 	struct rtmsg *rtm;
39801da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
3981e3703b3dSThomas Graf 	long expires;
39829e762a4aSPatrick McHardy 	u32 table;
39831da177e4SLinus Torvalds 
398415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
398538308473SDavid S. Miller 	if (!nlh)
398626932566SPatrick McHardy 		return -EMSGSIZE;
39872d7202bfSThomas Graf 
39882d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
39891da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
39901da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
39911da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
39921da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
3993c71099acSThomas Graf 	if (rt->rt6i_table)
39949e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
3995c71099acSThomas Graf 	else
39969e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
39979e762a4aSPatrick McHardy 	rtm->rtm_table = table;
3998c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
3999c78679e8SDavid S. Miller 		goto nla_put_failure;
4000ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
4001ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
4002ef2c7d7bSNicolas Dichtel 		case -EINVAL:
4003ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
4004ef2c7d7bSNicolas Dichtel 			break;
4005ef2c7d7bSNicolas Dichtel 		case -EACCES:
4006ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
4007ef2c7d7bSNicolas Dichtel 			break;
4008b4949ab2SNicolas Dichtel 		case -EAGAIN:
4009b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
4010b4949ab2SNicolas Dichtel 			break;
4011ef2c7d7bSNicolas Dichtel 		default:
40121da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
4013ef2c7d7bSNicolas Dichtel 			break;
4014ef2c7d7bSNicolas Dichtel 		}
4015ef2c7d7bSNicolas Dichtel 	}
4016ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
4017ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
40184ee39733SDavid Ahern 	else if (rt->rt6i_flags & RTF_ANYCAST)
40194ee39733SDavid Ahern 		rtm->rtm_type = RTN_ANYCAST;
4020d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
40211da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
40221da177e4SLinus Torvalds 	else
40231da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
40241da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
40251da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
40261da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
40271da177e4SLinus Torvalds 
40281da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
40291da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
40301da177e4SLinus Torvalds 
40311da177e4SLinus Torvalds 	if (dst) {
4032930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_DST, dst))
4033c78679e8SDavid S. Miller 			goto nla_put_failure;
40341da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
40351da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
4036930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
4037c78679e8SDavid S. Miller 			goto nla_put_failure;
40381da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
40391da177e4SLinus Torvalds 	if (src) {
4040930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
4041c78679e8SDavid S. Miller 			goto nla_put_failure;
40421da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
4043c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
4044930345eaSJiri Benc 		   nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
4045c78679e8SDavid S. Miller 		goto nla_put_failure;
40461da177e4SLinus Torvalds #endif
40477bc570c8SYOSHIFUJI Hideaki 	if (iif) {
40487bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
40497bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
4050fd61c6baSDavid Ahern 			int err = ip6mr_get_route(net, skb, rtm, portid);
40512cf75070SNikolay Aleksandrov 
40527bc570c8SYOSHIFUJI Hideaki 			if (err == 0)
40537bc570c8SYOSHIFUJI Hideaki 				return 0;
4054fd61c6baSDavid Ahern 			if (err < 0)
40557bc570c8SYOSHIFUJI Hideaki 				goto nla_put_failure;
40567bc570c8SYOSHIFUJI Hideaki 		} else
40577bc570c8SYOSHIFUJI Hideaki #endif
4058c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
4059c78679e8SDavid S. Miller 				goto nla_put_failure;
40607bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
40611da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
4062c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
4063930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4064c78679e8SDavid S. Miller 			goto nla_put_failure;
4065c3968a85SDaniel Walter 	}
4066c3968a85SDaniel Walter 
4067c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
4068c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
40694e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
4070930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
4071c78679e8SDavid S. Miller 			goto nla_put_failure;
40721da177e4SLinus Torvalds 	}
40732d7202bfSThomas Graf 
40744b32b5adSMartin KaFai Lau 	memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
40754b32b5adSMartin KaFai Lau 	if (rt->rt6i_pmtu)
40764b32b5adSMartin KaFai Lau 		metrics[RTAX_MTU - 1] = rt->rt6i_pmtu;
40774b32b5adSMartin KaFai Lau 	if (rtnetlink_put_metrics(skb, metrics) < 0)
40782d7202bfSThomas Graf 		goto nla_put_failure;
40792d7202bfSThomas Graf 
4080beb1afacSDavid Ahern 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
4081beb1afacSDavid Ahern 		goto nla_put_failure;
4082beb1afacSDavid Ahern 
4083beb1afacSDavid Ahern 	/* For multipath routes, walk the siblings list and add
4084beb1afacSDavid Ahern 	 * each as a nexthop within RTA_MULTIPATH.
4085beb1afacSDavid Ahern 	 */
4086beb1afacSDavid Ahern 	if (rt->rt6i_nsiblings) {
4087beb1afacSDavid Ahern 		struct rt6_info *sibling, *next_sibling;
4088beb1afacSDavid Ahern 		struct nlattr *mp;
4089beb1afacSDavid Ahern 
4090beb1afacSDavid Ahern 		mp = nla_nest_start(skb, RTA_MULTIPATH);
4091beb1afacSDavid Ahern 		if (!mp)
4092beb1afacSDavid Ahern 			goto nla_put_failure;
4093beb1afacSDavid Ahern 
4094beb1afacSDavid Ahern 		if (rt6_add_nexthop(skb, rt) < 0)
4095beb1afacSDavid Ahern 			goto nla_put_failure;
4096beb1afacSDavid Ahern 
4097beb1afacSDavid Ahern 		list_for_each_entry_safe(sibling, next_sibling,
4098beb1afacSDavid Ahern 					 &rt->rt6i_siblings, rt6i_siblings) {
4099beb1afacSDavid Ahern 			if (rt6_add_nexthop(skb, sibling) < 0)
410094f826b8SEric Dumazet 				goto nla_put_failure;
410194f826b8SEric Dumazet 		}
41022d7202bfSThomas Graf 
4103beb1afacSDavid Ahern 		nla_nest_end(skb, mp);
4104beb1afacSDavid Ahern 	} else {
41055be083ceSDavid Ahern 		if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0)
4106c78679e8SDavid S. Miller 			goto nla_put_failure;
4107beb1afacSDavid Ahern 	}
41088253947eSLi Wei 
41098253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
411069cdf8f9SYOSHIFUJI Hideaki 
411187a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
4112e3703b3dSThomas Graf 		goto nla_put_failure;
41131da177e4SLinus Torvalds 
4114c78ba6d6SLubomir Rintel 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
4115c78ba6d6SLubomir Rintel 		goto nla_put_failure;
4116c78ba6d6SLubomir Rintel 
411719e42e45SRoopa Prabhu 
4118053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
4119053c095aSJohannes Berg 	return 0;
41202d7202bfSThomas Graf 
41212d7202bfSThomas Graf nla_put_failure:
412226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
412326932566SPatrick McHardy 	return -EMSGSIZE;
41241da177e4SLinus Torvalds }
41251da177e4SLinus Torvalds 
41261b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
41271da177e4SLinus Torvalds {
41281da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
41291f17e2f2SDavid Ahern 	struct net *net = arg->net;
41301f17e2f2SDavid Ahern 
41311f17e2f2SDavid Ahern 	if (rt == net->ipv6.ip6_null_entry)
41321f17e2f2SDavid Ahern 		return 0;
41331da177e4SLinus Torvalds 
41342d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
41352d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
4136f8cfe2ceSDavid Ahern 
4137f8cfe2ceSDavid Ahern 		/* user wants prefix routes only */
4138f8cfe2ceSDavid Ahern 		if (rtm->rtm_flags & RTM_F_PREFIX &&
4139f8cfe2ceSDavid Ahern 		    !(rt->rt6i_flags & RTF_PREFIX_RT)) {
4140f8cfe2ceSDavid Ahern 			/* success since this is not a prefix route */
4141f8cfe2ceSDavid Ahern 			return 1;
4142f8cfe2ceSDavid Ahern 		}
4143f8cfe2ceSDavid Ahern 	}
41441da177e4SLinus Torvalds 
41451f17e2f2SDavid Ahern 	return rt6_fill_node(net,
4146191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
414715e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
4148f8cfe2ceSDavid Ahern 		     NLM_F_MULTI);
41491da177e4SLinus Torvalds }
41501da177e4SLinus Torvalds 
4151c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4152c21ef3e3SDavid Ahern 			      struct netlink_ext_ack *extack)
41531da177e4SLinus Torvalds {
41543b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
4155ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
415618c3a61cSRoopa Prabhu 	int err, iif = 0, oif = 0;
415718c3a61cSRoopa Prabhu 	struct dst_entry *dst;
41581da177e4SLinus Torvalds 	struct rt6_info *rt;
4159ab364a6fSThomas Graf 	struct sk_buff *skb;
4160ab364a6fSThomas Graf 	struct rtmsg *rtm;
41614c9483b2SDavid S. Miller 	struct flowi6 fl6;
416218c3a61cSRoopa Prabhu 	bool fibmatch;
4163ab364a6fSThomas Graf 
4164fceb6435SJohannes Berg 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy,
4165c21ef3e3SDavid Ahern 			  extack);
4166ab364a6fSThomas Graf 	if (err < 0)
4167ab364a6fSThomas Graf 		goto errout;
4168ab364a6fSThomas Graf 
4169ab364a6fSThomas Graf 	err = -EINVAL;
41704c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
417138b7097bSHannes Frederic Sowa 	rtm = nlmsg_data(nlh);
417238b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0);
417318c3a61cSRoopa Prabhu 	fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH);
4174ab364a6fSThomas Graf 
4175ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
4176ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
4177ab364a6fSThomas Graf 			goto errout;
4178ab364a6fSThomas Graf 
41794e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
4180ab364a6fSThomas Graf 	}
4181ab364a6fSThomas Graf 
4182ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
4183ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
4184ab364a6fSThomas Graf 			goto errout;
4185ab364a6fSThomas Graf 
41864e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
4187ab364a6fSThomas Graf 	}
4188ab364a6fSThomas Graf 
4189ab364a6fSThomas Graf 	if (tb[RTA_IIF])
4190ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
4191ab364a6fSThomas Graf 
4192ab364a6fSThomas Graf 	if (tb[RTA_OIF])
419372331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
4194ab364a6fSThomas Graf 
41952e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
41962e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
41972e47b291SLorenzo Colitti 
4198622ec2c9SLorenzo Colitti 	if (tb[RTA_UID])
4199622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = make_kuid(current_user_ns(),
4200622ec2c9SLorenzo Colitti 					   nla_get_u32(tb[RTA_UID]));
4201622ec2c9SLorenzo Colitti 	else
4202622ec2c9SLorenzo Colitti 		fl6.flowi6_uid = iif ? INVALID_UID : current_uid();
4203622ec2c9SLorenzo Colitti 
4204ab364a6fSThomas Graf 	if (iif) {
4205ab364a6fSThomas Graf 		struct net_device *dev;
420672331bc0SShmulik Ladkani 		int flags = 0;
420772331bc0SShmulik Ladkani 
4208121622dbSFlorian Westphal 		rcu_read_lock();
4209121622dbSFlorian Westphal 
4210121622dbSFlorian Westphal 		dev = dev_get_by_index_rcu(net, iif);
4211ab364a6fSThomas Graf 		if (!dev) {
4212121622dbSFlorian Westphal 			rcu_read_unlock();
4213ab364a6fSThomas Graf 			err = -ENODEV;
4214ab364a6fSThomas Graf 			goto errout;
4215ab364a6fSThomas Graf 		}
421672331bc0SShmulik Ladkani 
421772331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
421872331bc0SShmulik Ladkani 
421972331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
422072331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
422172331bc0SShmulik Ladkani 
422218c3a61cSRoopa Prabhu 		if (!fibmatch)
422318c3a61cSRoopa Prabhu 			dst = ip6_route_input_lookup(net, dev, &fl6, flags);
4224401481e0SArnd Bergmann 		else
4225401481e0SArnd Bergmann 			dst = ip6_route_lookup(net, &fl6, 0);
4226121622dbSFlorian Westphal 
4227121622dbSFlorian Westphal 		rcu_read_unlock();
422872331bc0SShmulik Ladkani 	} else {
422972331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
423072331bc0SShmulik Ladkani 
423118c3a61cSRoopa Prabhu 		if (!fibmatch)
423218c3a61cSRoopa Prabhu 			dst = ip6_route_output(net, NULL, &fl6);
4233401481e0SArnd Bergmann 		else
4234401481e0SArnd Bergmann 			dst = ip6_route_lookup(net, &fl6, 0);
423518c3a61cSRoopa Prabhu 	}
423618c3a61cSRoopa Prabhu 
423718c3a61cSRoopa Prabhu 
423818c3a61cSRoopa Prabhu 	rt = container_of(dst, struct rt6_info, dst);
423918c3a61cSRoopa Prabhu 	if (rt->dst.error) {
424018c3a61cSRoopa Prabhu 		err = rt->dst.error;
424118c3a61cSRoopa Prabhu 		ip6_rt_put(rt);
424218c3a61cSRoopa Prabhu 		goto errout;
4243ab364a6fSThomas Graf 	}
42441da177e4SLinus Torvalds 
42459d6acb3bSWANG Cong 	if (rt == net->ipv6.ip6_null_entry) {
42469d6acb3bSWANG Cong 		err = rt->dst.error;
42479d6acb3bSWANG Cong 		ip6_rt_put(rt);
42489d6acb3bSWANG Cong 		goto errout;
42499d6acb3bSWANG Cong 	}
42509d6acb3bSWANG Cong 
42511da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
425238308473SDavid S. Miller 	if (!skb) {
425394e187c0SAmerigo Wang 		ip6_rt_put(rt);
4254ab364a6fSThomas Graf 		err = -ENOBUFS;
4255ab364a6fSThomas Graf 		goto errout;
4256ab364a6fSThomas Graf 	}
42571da177e4SLinus Torvalds 
4258d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
425918c3a61cSRoopa Prabhu 	if (fibmatch)
426018c3a61cSRoopa Prabhu 		err = rt6_fill_node(net, skb, rt, NULL, NULL, iif,
426118c3a61cSRoopa Prabhu 				    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
426218c3a61cSRoopa Prabhu 				    nlh->nlmsg_seq, 0);
426318c3a61cSRoopa Prabhu 	else
42644c9483b2SDavid S. Miller 		err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
426515e47304SEric W. Biederman 				    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
4266f8cfe2ceSDavid Ahern 				    nlh->nlmsg_seq, 0);
42671da177e4SLinus Torvalds 	if (err < 0) {
4268ab364a6fSThomas Graf 		kfree_skb(skb);
4269ab364a6fSThomas Graf 		goto errout;
42701da177e4SLinus Torvalds 	}
42711da177e4SLinus Torvalds 
427215e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4273ab364a6fSThomas Graf errout:
42741da177e4SLinus Torvalds 	return err;
42751da177e4SLinus Torvalds }
42761da177e4SLinus Torvalds 
427737a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info,
427837a1d361SRoopa Prabhu 		     unsigned int nlm_flags)
42791da177e4SLinus Torvalds {
42801da177e4SLinus Torvalds 	struct sk_buff *skb;
42815578689aSDaniel Lezcano 	struct net *net = info->nl_net;
4282528c4cebSDenis V. Lunev 	u32 seq;
4283528c4cebSDenis V. Lunev 	int err;
42840d51aa80SJamal Hadi Salim 
4285528c4cebSDenis V. Lunev 	err = -ENOBUFS;
428638308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
428786872cb5SThomas Graf 
428819e42e45SRoopa Prabhu 	skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any());
428938308473SDavid S. Miller 	if (!skb)
429021713ebcSThomas Graf 		goto errout;
42911da177e4SLinus Torvalds 
4292191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
4293f8cfe2ceSDavid Ahern 				event, info->portid, seq, nlm_flags);
429426932566SPatrick McHardy 	if (err < 0) {
429526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
429626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
429726932566SPatrick McHardy 		kfree_skb(skb);
429826932566SPatrick McHardy 		goto errout;
429926932566SPatrick McHardy 	}
430015e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
43015578689aSDaniel Lezcano 		    info->nlh, gfp_any());
43021ce85fe4SPablo Neira Ayuso 	return;
430321713ebcSThomas Graf errout:
430421713ebcSThomas Graf 	if (err < 0)
43055578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
43061da177e4SLinus Torvalds }
43071da177e4SLinus Torvalds 
43088ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
4309351638e7SJiri Pirko 				unsigned long event, void *ptr)
43108ed67789SDaniel Lezcano {
4311351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4312c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
43138ed67789SDaniel Lezcano 
4314242d3a49SWANG Cong 	if (!(dev->flags & IFF_LOOPBACK))
4315242d3a49SWANG Cong 		return NOTIFY_OK;
4316242d3a49SWANG Cong 
4317242d3a49SWANG Cong 	if (event == NETDEV_REGISTER) {
4318d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
43198ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
43208ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
4321d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
43228ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
4323d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
43248ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
43258ed67789SDaniel Lezcano #endif
432676da0704SWANG Cong 	 } else if (event == NETDEV_UNREGISTER &&
432776da0704SWANG Cong 		    dev->reg_state != NETREG_UNREGISTERED) {
432876da0704SWANG Cong 		/* NETDEV_UNREGISTER could be fired for multiple times by
432976da0704SWANG Cong 		 * netdev_wait_allrefs(). Make sure we only call this once.
433076da0704SWANG Cong 		 */
433112d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
4332242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES
433312d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
433412d94a80SEric Dumazet 		in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
4335242d3a49SWANG Cong #endif
43368ed67789SDaniel Lezcano 	}
43378ed67789SDaniel Lezcano 
43388ed67789SDaniel Lezcano 	return NOTIFY_OK;
43398ed67789SDaniel Lezcano }
43408ed67789SDaniel Lezcano 
43411da177e4SLinus Torvalds /*
43421da177e4SLinus Torvalds  *	/proc
43431da177e4SLinus Torvalds  */
43441da177e4SLinus Torvalds 
43451da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
43461da177e4SLinus Torvalds 
434733120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
434833120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
434933120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
435033120b30SAlexey Dobriyan 	.read		= seq_read,
435133120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
43528d2ca1d7SHannes Frederic Sowa 	.release	= seq_release_net,
435333120b30SAlexey Dobriyan };
435433120b30SAlexey Dobriyan 
43551da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
43561da177e4SLinus Torvalds {
435769ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
43581da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
435969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
436069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
436169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
436269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
436369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
4364fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
436569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
43661da177e4SLinus Torvalds 
43671da177e4SLinus Torvalds 	return 0;
43681da177e4SLinus Torvalds }
43691da177e4SLinus Torvalds 
43701da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
43711da177e4SLinus Torvalds {
4372de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
437369ddb805SDaniel Lezcano }
437469ddb805SDaniel Lezcano 
43759a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
43761da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
43771da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
43781da177e4SLinus Torvalds 	.read	 = seq_read,
43791da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
4380b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
43811da177e4SLinus Torvalds };
43821da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
43831da177e4SLinus Torvalds 
43841da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
43851da177e4SLinus Torvalds 
43861da177e4SLinus Torvalds static
4387fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
43881da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
43891da177e4SLinus Torvalds {
4390c486da34SLucian Adrian Grijincu 	struct net *net;
4391c486da34SLucian Adrian Grijincu 	int delay;
4392c486da34SLucian Adrian Grijincu 	if (!write)
4393c486da34SLucian Adrian Grijincu 		return -EINVAL;
4394c486da34SLucian Adrian Grijincu 
4395c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
4396c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
43978d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
43982ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
43991da177e4SLinus Torvalds 	return 0;
44001da177e4SLinus Torvalds }
44011da177e4SLinus Torvalds 
4402fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = {
44031da177e4SLinus Torvalds 	{
44041da177e4SLinus Torvalds 		.procname	=	"flush",
44054990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
44061da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
440789c8b3a1SDave Jones 		.mode		=	0200,
44086d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
44091da177e4SLinus Torvalds 	},
44101da177e4SLinus Torvalds 	{
44111da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
44129a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
44131da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
44141da177e4SLinus Torvalds 		.mode		=	0644,
44156d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
44161da177e4SLinus Torvalds 	},
44171da177e4SLinus Torvalds 	{
44181da177e4SLinus Torvalds 		.procname	=	"max_size",
44194990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
44201da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
44211da177e4SLinus Torvalds 		.mode		=	0644,
44226d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
44231da177e4SLinus Torvalds 	},
44241da177e4SLinus Torvalds 	{
44251da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
44264990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
44271da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
44281da177e4SLinus Torvalds 		.mode		=	0644,
44296d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
44301da177e4SLinus Torvalds 	},
44311da177e4SLinus Torvalds 	{
44321da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
44334990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
44341da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
44351da177e4SLinus Torvalds 		.mode		=	0644,
44366d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
44371da177e4SLinus Torvalds 	},
44381da177e4SLinus Torvalds 	{
44391da177e4SLinus Torvalds 		.procname	=	"gc_interval",
44404990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
44411da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
44421da177e4SLinus Torvalds 		.mode		=	0644,
44436d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
44441da177e4SLinus Torvalds 	},
44451da177e4SLinus Torvalds 	{
44461da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
44474990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
44481da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
44491da177e4SLinus Torvalds 		.mode		=	0644,
4450f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
44511da177e4SLinus Torvalds 	},
44521da177e4SLinus Torvalds 	{
44531da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
44544990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
44551da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
44561da177e4SLinus Torvalds 		.mode		=	0644,
44576d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
44581da177e4SLinus Torvalds 	},
44591da177e4SLinus Torvalds 	{
44601da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
44614990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
44621da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
44631da177e4SLinus Torvalds 		.mode		=	0644,
4464f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
44651da177e4SLinus Torvalds 	},
44661da177e4SLinus Torvalds 	{
44671da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
44684990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
44691da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
44701da177e4SLinus Torvalds 		.mode		=	0644,
44716d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
44721da177e4SLinus Torvalds 	},
4473f8572d8fSEric W. Biederman 	{ }
44741da177e4SLinus Torvalds };
44751da177e4SLinus Torvalds 
44762c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
4477760f2d01SDaniel Lezcano {
4478760f2d01SDaniel Lezcano 	struct ctl_table *table;
4479760f2d01SDaniel Lezcano 
4480760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
4481760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
4482760f2d01SDaniel Lezcano 			GFP_KERNEL);
44835ee09105SYOSHIFUJI Hideaki 
44845ee09105SYOSHIFUJI Hideaki 	if (table) {
44855ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
4486c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
448786393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
44885ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
44895ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
44905ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
44915ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
44925ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
44935ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
44945ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
44959c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
4496464dc801SEric W. Biederman 
4497464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
4498464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
4499464dc801SEric W. Biederman 			table[0].procname = NULL;
45005ee09105SYOSHIFUJI Hideaki 	}
45015ee09105SYOSHIFUJI Hideaki 
4502760f2d01SDaniel Lezcano 	return table;
4503760f2d01SDaniel Lezcano }
45041da177e4SLinus Torvalds #endif
45051da177e4SLinus Torvalds 
45062c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
4507cdb18761SDaniel Lezcano {
4508633d424bSPavel Emelyanov 	int ret = -ENOMEM;
45098ed67789SDaniel Lezcano 
451086393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
451186393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
4512f2fc6a54SBenjamin Thery 
4513fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
4514fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
4515fc66f95cSEric Dumazet 
45168ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
45178ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
45188ed67789SDaniel Lezcano 					   GFP_KERNEL);
45198ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
4520fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
4521d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
45228ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
4523d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
452462fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
452562fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
45268ed67789SDaniel Lezcano 
45278ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
4528feca7d8cSVincent Bernat 	net->ipv6.fib6_has_custom_rules = false;
45298ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
45308ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
45318ed67789SDaniel Lezcano 					       GFP_KERNEL);
453268fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
453368fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
4534d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
45358ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
4536d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
453762fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
453862fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
45398ed67789SDaniel Lezcano 
45408ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
45418ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
45428ed67789SDaniel Lezcano 					       GFP_KERNEL);
454368fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
454468fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
4545d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
45468ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
4547d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
454862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
454962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
45508ed67789SDaniel Lezcano #endif
45518ed67789SDaniel Lezcano 
4552b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
4553b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
4554b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
4555b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
4556b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
4557b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
4558b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
4559b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
4560b339a47cSPeter Zijlstra 
45616891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
45626891a346SBenjamin Thery 
45638ed67789SDaniel Lezcano 	ret = 0;
45648ed67789SDaniel Lezcano out:
45658ed67789SDaniel Lezcano 	return ret;
4566f2fc6a54SBenjamin Thery 
456768fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
456868fffc67SPeter Zijlstra out_ip6_prohibit_entry:
456968fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
457068fffc67SPeter Zijlstra out_ip6_null_entry:
457168fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
457268fffc67SPeter Zijlstra #endif
4573fc66f95cSEric Dumazet out_ip6_dst_entries:
4574fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
4575f2fc6a54SBenjamin Thery out_ip6_dst_ops:
4576f2fc6a54SBenjamin Thery 	goto out;
4577cdb18761SDaniel Lezcano }
4578cdb18761SDaniel Lezcano 
45792c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
4580cdb18761SDaniel Lezcano {
45818ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
45828ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
45838ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
45848ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
45858ed67789SDaniel Lezcano #endif
458641bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
4587cdb18761SDaniel Lezcano }
4588cdb18761SDaniel Lezcano 
4589d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
4590d189634eSThomas Graf {
4591d189634eSThomas Graf #ifdef CONFIG_PROC_FS
4592d4beaa66SGao feng 	proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
4593d4beaa66SGao feng 	proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
4594d189634eSThomas Graf #endif
4595d189634eSThomas Graf 	return 0;
4596d189634eSThomas Graf }
4597d189634eSThomas Graf 
4598d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
4599d189634eSThomas Graf {
4600d189634eSThomas Graf #ifdef CONFIG_PROC_FS
4601ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
4602ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
4603d189634eSThomas Graf #endif
4604d189634eSThomas Graf }
4605d189634eSThomas Graf 
4606cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
4607cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
4608cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
4609cdb18761SDaniel Lezcano };
4610cdb18761SDaniel Lezcano 
4611c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
4612c3426b47SDavid S. Miller {
4613c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
4614c3426b47SDavid S. Miller 
4615c3426b47SDavid S. Miller 	if (!bp)
4616c3426b47SDavid S. Miller 		return -ENOMEM;
4617c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
4618c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
4619c3426b47SDavid S. Miller 	return 0;
4620c3426b47SDavid S. Miller }
4621c3426b47SDavid S. Miller 
4622c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
4623c3426b47SDavid S. Miller {
4624c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
4625c3426b47SDavid S. Miller 
4626c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
462756a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
4628c3426b47SDavid S. Miller 	kfree(bp);
4629c3426b47SDavid S. Miller }
4630c3426b47SDavid S. Miller 
46312b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
4632c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
4633c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
4634c3426b47SDavid S. Miller };
4635c3426b47SDavid S. Miller 
4636d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
4637d189634eSThomas Graf 	.init = ip6_route_net_init_late,
4638d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
4639d189634eSThomas Graf };
4640d189634eSThomas Graf 
46418ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
46428ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
4643242d3a49SWANG Cong 	.priority = ADDRCONF_NOTIFY_PRIORITY - 10,
46448ed67789SDaniel Lezcano };
46458ed67789SDaniel Lezcano 
46462f460933SWANG Cong void __init ip6_route_init_special_entries(void)
46472f460933SWANG Cong {
46482f460933SWANG Cong 	/* Registering of the loopback is done before this portion of code,
46492f460933SWANG Cong 	 * the loopback reference in rt6_info will not be taken, do it
46502f460933SWANG Cong 	 * manually for init_net */
46512f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
46522f460933SWANG Cong 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
46532f460933SWANG Cong   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
46542f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
46552f460933SWANG Cong 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
46562f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
46572f460933SWANG Cong 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
46582f460933SWANG Cong   #endif
46592f460933SWANG Cong }
46602f460933SWANG Cong 
4661433d49c3SDaniel Lezcano int __init ip6_route_init(void)
46621da177e4SLinus Torvalds {
4663433d49c3SDaniel Lezcano 	int ret;
46648d0b94afSMartin KaFai Lau 	int cpu;
4665433d49c3SDaniel Lezcano 
46669a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
46679a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
46689a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
46699a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
46709a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
4671c19a28e1SFernando Carrijo 		goto out;
467214e50e57SDavid S. Miller 
4673fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
46748ed67789SDaniel Lezcano 	if (ret)
4675bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
4676bdb3289fSDaniel Lezcano 
4677c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
4678c3426b47SDavid S. Miller 	if (ret)
4679e8803b6cSDavid S. Miller 		goto out_dst_entries;
46802a0c451aSThomas Graf 
46817e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
46827e52b33bSDavid S. Miller 	if (ret)
46837e52b33bSDavid S. Miller 		goto out_register_inetpeer;
4684c3426b47SDavid S. Miller 
46855dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
46865dc121e9SArnaud Ebalard 
4687e8803b6cSDavid S. Miller 	ret = fib6_init();
4688433d49c3SDaniel Lezcano 	if (ret)
46898ed67789SDaniel Lezcano 		goto out_register_subsys;
4690433d49c3SDaniel Lezcano 
4691433d49c3SDaniel Lezcano 	ret = xfrm6_init();
4692433d49c3SDaniel Lezcano 	if (ret)
4693e8803b6cSDavid S. Miller 		goto out_fib6_init;
4694c35b7e72SDaniel Lezcano 
4695433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
4696433d49c3SDaniel Lezcano 	if (ret)
4697433d49c3SDaniel Lezcano 		goto xfrm6_init;
46987e5449c2SDaniel Lezcano 
4699d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
4700d189634eSThomas Graf 	if (ret)
4701d189634eSThomas Graf 		goto fib6_rules_init;
4702d189634eSThomas Graf 
4703433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
4704b97bac64SFlorian Westphal 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, 0) ||
4705b97bac64SFlorian Westphal 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, 0) ||
4706e3a22b7fSFlorian Westphal 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL,
4707e3a22b7fSFlorian Westphal 			    RTNL_FLAG_DOIT_UNLOCKED))
4708d189634eSThomas Graf 		goto out_register_late_subsys;
4709433d49c3SDaniel Lezcano 
47108ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
4711cdb18761SDaniel Lezcano 	if (ret)
4712d189634eSThomas Graf 		goto out_register_late_subsys;
47138ed67789SDaniel Lezcano 
47148d0b94afSMartin KaFai Lau 	for_each_possible_cpu(cpu) {
47158d0b94afSMartin KaFai Lau 		struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu);
47168d0b94afSMartin KaFai Lau 
47178d0b94afSMartin KaFai Lau 		INIT_LIST_HEAD(&ul->head);
47188d0b94afSMartin KaFai Lau 		spin_lock_init(&ul->lock);
47198d0b94afSMartin KaFai Lau 	}
47208d0b94afSMartin KaFai Lau 
4721433d49c3SDaniel Lezcano out:
4722433d49c3SDaniel Lezcano 	return ret;
4723433d49c3SDaniel Lezcano 
4724d189634eSThomas Graf out_register_late_subsys:
4725d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
4726433d49c3SDaniel Lezcano fib6_rules_init:
4727433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
4728433d49c3SDaniel Lezcano xfrm6_init:
4729433d49c3SDaniel Lezcano 	xfrm6_fini();
47302a0c451aSThomas Graf out_fib6_init:
47312a0c451aSThomas Graf 	fib6_gc_cleanup();
47328ed67789SDaniel Lezcano out_register_subsys:
47338ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
47347e52b33bSDavid S. Miller out_register_inetpeer:
47357e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
4736fc66f95cSEric Dumazet out_dst_entries:
4737fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
4738433d49c3SDaniel Lezcano out_kmem_cache:
4739f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
4740433d49c3SDaniel Lezcano 	goto out;
47411da177e4SLinus Torvalds }
47421da177e4SLinus Torvalds 
47431da177e4SLinus Torvalds void ip6_route_cleanup(void)
47441da177e4SLinus Torvalds {
47458ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
4746d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
4747101367c2SThomas Graf 	fib6_rules_cleanup();
47481da177e4SLinus Torvalds 	xfrm6_fini();
47491da177e4SLinus Torvalds 	fib6_gc_cleanup();
4750c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
47518ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
475241bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
4753f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
47541da177e4SLinus Torvalds }
4755