xref: /openbmc/linux/net/ipv6/route.c (revision 8b9df2657704dd313333a79497dde429f9190caa)
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>
47457c4cbcSEric W. Biederman #include <net/net_namespace.h>
481da177e4SLinus Torvalds #include <net/snmp.h>
491da177e4SLinus Torvalds #include <net/ipv6.h>
501da177e4SLinus Torvalds #include <net/ip6_fib.h>
511da177e4SLinus Torvalds #include <net/ip6_route.h>
521da177e4SLinus Torvalds #include <net/ndisc.h>
531da177e4SLinus Torvalds #include <net/addrconf.h>
541da177e4SLinus Torvalds #include <net/tcp.h>
551da177e4SLinus Torvalds #include <linux/rtnetlink.h>
561da177e4SLinus Torvalds #include <net/dst.h>
571da177e4SLinus Torvalds #include <net/xfrm.h>
588d71740cSTom Tucker #include <net/netevent.h>
5921713ebcSThomas Graf #include <net/netlink.h>
6051ebd318SNicolas Dichtel #include <net/nexthop.h>
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #include <asm/uaccess.h>
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
651da177e4SLinus Torvalds #include <linux/sysctl.h>
661da177e4SLinus Torvalds #endif
671da177e4SLinus Torvalds 
68afc154e9SHannes Frederic Sowa enum rt6_nud_state {
697e980569SJiri Benc 	RT6_NUD_FAIL_HARD = -3,
707e980569SJiri Benc 	RT6_NUD_FAIL_PROBE = -2,
717e980569SJiri Benc 	RT6_NUD_FAIL_DO_RR = -1,
72afc154e9SHannes Frederic Sowa 	RT6_NUD_SUCCEED = 1
73afc154e9SHannes Frederic Sowa };
74afc154e9SHannes Frederic Sowa 
751716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
7621efcfa0SEric Dumazet 				    const struct in6_addr *dest);
771da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
780dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
79ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
801da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
811da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
821da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
831da177e4SLinus Torvalds 				       struct net_device *dev, int how);
84569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
87aad88724SEric Dumazet static int		ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb);
887150aedeSKamala R static int		ip6_pkt_prohibit(struct sk_buff *skb);
89aad88724SEric Dumazet static int		ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb);
901da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
916700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
926700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
936700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
946700c270SDavid S. Miller 					struct sk_buff *skb);
954b32b5adSMartin KaFai Lau static void		rt6_dst_from_metrics_check(struct rt6_info *rt);
9652bd4c0cSNicolas Dichtel static int rt6_score_route(struct rt6_info *rt, int oif, int strict);
971da177e4SLinus Torvalds 
9870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
99efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
100b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
101b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
10295c96174SEric Dumazet 					   unsigned int pref);
103efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
104b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
105b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
10670ceb4f5SYOSHIFUJI Hideaki #endif
10770ceb4f5SYOSHIFUJI Hideaki 
10806582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
10906582540SDavid S. Miller {
11006582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *)dst;
11106582540SDavid S. Miller 
1124b32b5adSMartin KaFai Lau 	if (rt->rt6i_flags & RTF_CACHE)
1134b32b5adSMartin KaFai Lau 		return NULL;
1144b32b5adSMartin KaFai Lau 	else
1153b471175SMartin KaFai Lau 		return dst_cow_metrics_generic(dst, old);
11606582540SDavid S. Miller }
11706582540SDavid S. Miller 
118f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt,
119f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
120f894cbf8SDavid S. Miller 					     const void *daddr)
12139232973SDavid S. Miller {
12239232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
12339232973SDavid S. Miller 
124a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
12539232973SDavid S. Miller 		return (const void *) p;
126f894cbf8SDavid S. Miller 	else if (skb)
127f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
12839232973SDavid S. Miller 	return daddr;
12939232973SDavid S. Miller }
13039232973SDavid S. Miller 
131f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
132f894cbf8SDavid S. Miller 					  struct sk_buff *skb,
133f894cbf8SDavid S. Miller 					  const void *daddr)
134d3aaeb38SDavid S. Miller {
13539232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
13639232973SDavid S. Miller 	struct neighbour *n;
13739232973SDavid S. Miller 
138f894cbf8SDavid S. Miller 	daddr = choose_neigh_daddr(rt, skb, daddr);
1398e022ee6SYOSHIFUJI Hideaki / 吉藤英明 	n = __ipv6_neigh_lookup(dst->dev, daddr);
140f83c7790SDavid S. Miller 	if (n)
141f83c7790SDavid S. Miller 		return n;
142f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
143f83c7790SDavid S. Miller }
144f83c7790SDavid S. Miller 
1459a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1461da177e4SLinus Torvalds 	.family			=	AF_INET6,
1471da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1481da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1491da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1500dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
151ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
15206582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1531da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1541da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1551da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1561da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1571da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1586e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
1591ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
160d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1611da177e4SLinus Torvalds };
1621da177e4SLinus Torvalds 
163ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
164ec831ea7SRoland Dreier {
165618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
166618f9bc7SSteffen Klassert 
167618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
168ec831ea7SRoland Dreier }
169ec831ea7SRoland Dreier 
1706700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
1716700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
17214e50e57SDavid S. Miller {
17314e50e57SDavid S. Miller }
17414e50e57SDavid S. Miller 
1756700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
1766700c270SDavid S. Miller 				      struct sk_buff *skb)
177b587ee3bSDavid S. Miller {
178b587ee3bSDavid S. Miller }
179b587ee3bSDavid S. Miller 
1800972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1810972ddb2SHeld Bernhard 					 unsigned long old)
1820972ddb2SHeld Bernhard {
1830972ddb2SHeld Bernhard 	return NULL;
1840972ddb2SHeld Bernhard }
1850972ddb2SHeld Bernhard 
18614e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
18714e50e57SDavid S. Miller 	.family			=	AF_INET6,
18814e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
18914e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
190ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
191214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
19214e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
193b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
1940972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
195d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
19614e50e57SDavid S. Miller };
19714e50e57SDavid S. Miller 
19862fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
19914edd87dSLi RongQing 	[RTAX_HOPLIMIT - 1] = 0,
20062fa8a84SDavid S. Miller };
20162fa8a84SDavid S. Miller 
202fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
2031da177e4SLinus Torvalds 	.dst = {
2041da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2051da177e4SLinus Torvalds 		.__use		= 1,
2062c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
2071da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2081da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2091da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2101da177e4SLinus Torvalds 	},
2111da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2124f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2131da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2141da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2151da177e4SLinus Torvalds };
2161da177e4SLinus Torvalds 
217101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
218101367c2SThomas Graf 
219fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
220101367c2SThomas Graf 	.dst = {
221101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
222101367c2SThomas Graf 		.__use		= 1,
2232c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
224101367c2SThomas Graf 		.error		= -EACCES,
2259ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2269ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
227101367c2SThomas Graf 	},
228101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2294f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
230101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
231101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
232101367c2SThomas Graf };
233101367c2SThomas Graf 
234fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
235101367c2SThomas Graf 	.dst = {
236101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
237101367c2SThomas Graf 		.__use		= 1,
2382c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
239101367c2SThomas Graf 		.error		= -EINVAL,
240352e512cSHerbert Xu 		.input		= dst_discard,
241aad88724SEric Dumazet 		.output		= dst_discard_sk,
242101367c2SThomas Graf 	},
243101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2444f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
245101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
246101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
247101367c2SThomas Graf };
248101367c2SThomas Graf 
249101367c2SThomas Graf #endif
250101367c2SThomas Graf 
2511da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
25297bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net,
253957c665fSDavid S. Miller 					     struct net_device *dev,
2548b96d22dSDavid S. Miller 					     int flags,
2558b96d22dSDavid S. Miller 					     struct fib6_table *table)
2561da177e4SLinus Torvalds {
25797bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
2586f3118b5SNicolas Dichtel 					0, DST_OBSOLETE_FORCE_CHK, flags);
259cf911662SDavid S. Miller 
26097bab73fSDavid S. Miller 	if (rt) {
2618104891bSSteffen Klassert 		struct dst_entry *dst = &rt->dst;
2628104891bSSteffen Klassert 
2638104891bSSteffen Klassert 		memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
26451ebd318SNicolas Dichtel 		INIT_LIST_HEAD(&rt->rt6i_siblings);
26597bab73fSDavid S. Miller 	}
266cf911662SDavid S. Miller 	return rt;
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2721da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
273ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	struct dst_entry *from = dst->from;
2741da177e4SLinus Torvalds 
2758e2ec639SYan, Zheng 	dst_destroy_metrics_generic(dst);
2768e2ec639SYan, Zheng 
27738308473SDavid S. Miller 	if (idev) {
2781da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2791da177e4SLinus Torvalds 		in6_dev_put(idev);
2801da177e4SLinus Torvalds 	}
2811716a961SGao feng 
282ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst->from = NULL;
283ecd98837SYOSHIFUJI Hideaki / 吉藤英明 	dst_release(from);
284b3419363SDavid S. Miller }
285b3419363SDavid S. Miller 
2861da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
2871da177e4SLinus Torvalds 			   int how)
2881da177e4SLinus Torvalds {
2891da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2901da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2915a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
292c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
2931da177e4SLinus Torvalds 
29497cac082SDavid S. Miller 	if (dev != loopback_dev) {
29597cac082SDavid S. Miller 		if (idev && idev->dev == dev) {
2965a3e55d6SDenis V. Lunev 			struct inet6_dev *loopback_idev =
2975a3e55d6SDenis V. Lunev 				in6_dev_get(loopback_dev);
29838308473SDavid S. Miller 			if (loopback_idev) {
2991da177e4SLinus Torvalds 				rt->rt6i_idev = loopback_idev;
3001da177e4SLinus Torvalds 				in6_dev_put(idev);
3011da177e4SLinus Torvalds 			}
3021da177e4SLinus Torvalds 		}
30397cac082SDavid S. Miller 	}
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
306a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3071da177e4SLinus Torvalds {
3081716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3091716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
310a50feda5SEric Dumazet 			return true;
3111716a961SGao feng 	} else if (rt->dst.from) {
3123fd91fb3SLi RongQing 		return rt6_check_expired((struct rt6_info *) rt->dst.from);
3131716a961SGao feng 	}
314a50feda5SEric Dumazet 	return false;
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
31751ebd318SNicolas Dichtel /* Multipath route selection:
31851ebd318SNicolas Dichtel  *   Hash based function using packet header and flowlabel.
31951ebd318SNicolas Dichtel  * Adapted from fib_info_hashfn()
32051ebd318SNicolas Dichtel  */
32151ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count,
32251ebd318SNicolas Dichtel 			       const struct flowi6 *fl6)
32351ebd318SNicolas Dichtel {
32451ebd318SNicolas Dichtel 	unsigned int val = fl6->flowi6_proto;
32551ebd318SNicolas Dichtel 
326c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->daddr);
327c08977bbSYOSHIFUJI Hideaki / 吉藤英明 	val ^= ipv6_addr_hash(&fl6->saddr);
32851ebd318SNicolas Dichtel 
32951ebd318SNicolas Dichtel 	/* Work only if this not encapsulated */
33051ebd318SNicolas Dichtel 	switch (fl6->flowi6_proto) {
33151ebd318SNicolas Dichtel 	case IPPROTO_UDP:
33251ebd318SNicolas Dichtel 	case IPPROTO_TCP:
33351ebd318SNicolas Dichtel 	case IPPROTO_SCTP:
334b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_sport;
335b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_dport;
33651ebd318SNicolas Dichtel 		break;
33751ebd318SNicolas Dichtel 
33851ebd318SNicolas Dichtel 	case IPPROTO_ICMPV6:
339b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_type;
340b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_code;
34151ebd318SNicolas Dichtel 		break;
34251ebd318SNicolas Dichtel 	}
34351ebd318SNicolas Dichtel 	/* RFC6438 recommands to use flowlabel */
344b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->flowlabel;
34551ebd318SNicolas Dichtel 
34651ebd318SNicolas Dichtel 	/* Perhaps, we need to tune, this function? */
34751ebd318SNicolas Dichtel 	val = val ^ (val >> 7) ^ (val >> 12);
34851ebd318SNicolas Dichtel 	return val % candidate_count;
34951ebd318SNicolas Dichtel }
35051ebd318SNicolas Dichtel 
35151ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
35252bd4c0cSNicolas Dichtel 					     struct flowi6 *fl6, int oif,
35352bd4c0cSNicolas Dichtel 					     int strict)
35451ebd318SNicolas Dichtel {
35551ebd318SNicolas Dichtel 	struct rt6_info *sibling, *next_sibling;
35651ebd318SNicolas Dichtel 	int route_choosen;
35751ebd318SNicolas Dichtel 
35851ebd318SNicolas Dichtel 	route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
35951ebd318SNicolas Dichtel 	/* Don't change the route, if route_choosen == 0
36051ebd318SNicolas Dichtel 	 * (siblings does not include ourself)
36151ebd318SNicolas Dichtel 	 */
36251ebd318SNicolas Dichtel 	if (route_choosen)
36351ebd318SNicolas Dichtel 		list_for_each_entry_safe(sibling, next_sibling,
36451ebd318SNicolas Dichtel 				&match->rt6i_siblings, rt6i_siblings) {
36551ebd318SNicolas Dichtel 			route_choosen--;
36651ebd318SNicolas Dichtel 			if (route_choosen == 0) {
36752bd4c0cSNicolas Dichtel 				if (rt6_score_route(sibling, oif, strict) < 0)
36852bd4c0cSNicolas Dichtel 					break;
36951ebd318SNicolas Dichtel 				match = sibling;
37051ebd318SNicolas Dichtel 				break;
37151ebd318SNicolas Dichtel 			}
37251ebd318SNicolas Dichtel 		}
37351ebd318SNicolas Dichtel 	return match;
37451ebd318SNicolas Dichtel }
37551ebd318SNicolas Dichtel 
3761da177e4SLinus Torvalds /*
377c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
3781da177e4SLinus Torvalds  */
3791da177e4SLinus Torvalds 
3808ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
3818ed67789SDaniel Lezcano 						    struct rt6_info *rt,
382b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
3831da177e4SLinus Torvalds 						    int oif,
384d420895eSYOSHIFUJI Hideaki 						    int flags)
3851da177e4SLinus Torvalds {
3861da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
3871da177e4SLinus Torvalds 	struct rt6_info *sprt;
3881da177e4SLinus Torvalds 
389dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
390dd3abc4eSYOSHIFUJI Hideaki 		goto out;
391dd3abc4eSYOSHIFUJI Hideaki 
392d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
393d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
394dd3abc4eSYOSHIFUJI Hideaki 
395dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
3961da177e4SLinus Torvalds 			if (dev->ifindex == oif)
3971da177e4SLinus Torvalds 				return sprt;
3981da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
39938308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
4001da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
401d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
4021da177e4SLinus Torvalds 						continue;
4031da177e4SLinus Torvalds 					if (local && (!oif ||
4041da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
4051da177e4SLinus Torvalds 						continue;
4061da177e4SLinus Torvalds 				}
4071da177e4SLinus Torvalds 				local = sprt;
4081da177e4SLinus Torvalds 			}
409dd3abc4eSYOSHIFUJI Hideaki 		} else {
410dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
411dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
412dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
413dd3abc4eSYOSHIFUJI Hideaki 		}
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds 
416dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4171da177e4SLinus Torvalds 		if (local)
4181da177e4SLinus Torvalds 			return local;
4191da177e4SLinus Torvalds 
420d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4218ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4221da177e4SLinus Torvalds 	}
423dd3abc4eSYOSHIFUJI Hideaki out:
4241da177e4SLinus Torvalds 	return rt;
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
42727097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
428c2f17e82SHannes Frederic Sowa struct __rt6_probe_work {
429c2f17e82SHannes Frederic Sowa 	struct work_struct work;
430c2f17e82SHannes Frederic Sowa 	struct in6_addr target;
431c2f17e82SHannes Frederic Sowa 	struct net_device *dev;
432c2f17e82SHannes Frederic Sowa };
433c2f17e82SHannes Frederic Sowa 
434c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w)
435c2f17e82SHannes Frederic Sowa {
436c2f17e82SHannes Frederic Sowa 	struct in6_addr mcaddr;
437c2f17e82SHannes Frederic Sowa 	struct __rt6_probe_work *work =
438c2f17e82SHannes Frederic Sowa 		container_of(w, struct __rt6_probe_work, work);
439c2f17e82SHannes Frederic Sowa 
440c2f17e82SHannes Frederic Sowa 	addrconf_addr_solict_mult(&work->target, &mcaddr);
441c2f17e82SHannes Frederic Sowa 	ndisc_send_ns(work->dev, NULL, &work->target, &mcaddr, NULL);
442c2f17e82SHannes Frederic Sowa 	dev_put(work->dev);
443662f5533SMichael Büsch 	kfree(work);
444c2f17e82SHannes Frederic Sowa }
445c2f17e82SHannes Frederic Sowa 
44627097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
44727097255SYOSHIFUJI Hideaki {
448f2c31e32SEric Dumazet 	struct neighbour *neigh;
44927097255SYOSHIFUJI Hideaki 	/*
45027097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
45127097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
45227097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
45327097255SYOSHIFUJI Hideaki 	 *
45427097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
45527097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
45627097255SYOSHIFUJI Hideaki 	 */
4572152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!rt || !(rt->rt6i_flags & RTF_GATEWAY))
458fdd6681dSAmerigo Wang 		return;
4592152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
4602152caeaSYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
4612152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
4622152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_lock(&neigh->lock);
4632152caeaSYOSHIFUJI Hideaki / 吉藤英明 		if (neigh->nud_state & NUD_VALID)
4642152caeaSYOSHIFUJI Hideaki / 吉藤英明 			goto out;
4657ff74a59SYOSHIFUJI Hideaki / 吉藤英明 	}
4662152caeaSYOSHIFUJI Hideaki / 吉藤英明 
4672152caeaSYOSHIFUJI Hideaki / 吉藤英明 	if (!neigh ||
46852e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
469c2f17e82SHannes Frederic Sowa 		struct __rt6_probe_work *work;
47027097255SYOSHIFUJI Hideaki 
471c2f17e82SHannes Frederic Sowa 		work = kmalloc(sizeof(*work), GFP_ATOMIC);
472c2f17e82SHannes Frederic Sowa 
473c2f17e82SHannes Frederic Sowa 		if (neigh && work)
4747e980569SJiri Benc 			__neigh_set_probe_once(neigh);
4752152caeaSYOSHIFUJI Hideaki / 吉藤英明 
476c2f17e82SHannes Frederic Sowa 		if (neigh)
477c2f17e82SHannes Frederic Sowa 			write_unlock(&neigh->lock);
478c2f17e82SHannes Frederic Sowa 
479c2f17e82SHannes Frederic Sowa 		if (work) {
480c2f17e82SHannes Frederic Sowa 			INIT_WORK(&work->work, rt6_probe_deferred);
481c2f17e82SHannes Frederic Sowa 			work->target = rt->rt6i_gateway;
482c2f17e82SHannes Frederic Sowa 			dev_hold(rt->dst.dev);
483c2f17e82SHannes Frederic Sowa 			work->dev = rt->dst.dev;
484c2f17e82SHannes Frederic Sowa 			schedule_work(&work->work);
485c2f17e82SHannes Frederic Sowa 		}
486f2c31e32SEric Dumazet 	} else {
4872152caeaSYOSHIFUJI Hideaki / 吉藤英明 out:
4882152caeaSYOSHIFUJI Hideaki / 吉藤英明 		write_unlock(&neigh->lock);
48927097255SYOSHIFUJI Hideaki 	}
4902152caeaSYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
491f2c31e32SEric Dumazet }
49227097255SYOSHIFUJI Hideaki #else
49327097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
49427097255SYOSHIFUJI Hideaki {
49527097255SYOSHIFUJI Hideaki }
49627097255SYOSHIFUJI Hideaki #endif
49727097255SYOSHIFUJI Hideaki 
4981da177e4SLinus Torvalds /*
499554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5001da177e4SLinus Torvalds  */
501b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
5021da177e4SLinus Torvalds {
503d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
504161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
505554cfb7eSYOSHIFUJI Hideaki 		return 2;
506161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
507161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
508161980f4SDavid S. Miller 		return 1;
509554cfb7eSYOSHIFUJI Hideaki 	return 0;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
512afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt)
5131da177e4SLinus Torvalds {
514f2c31e32SEric Dumazet 	struct neighbour *neigh;
515afc154e9SHannes Frederic Sowa 	enum rt6_nud_state ret = RT6_NUD_FAIL_HARD;
516f2c31e32SEric Dumazet 
5174d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
5184d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
519afc154e9SHannes Frederic Sowa 		return RT6_NUD_SUCCEED;
520145a3621SYOSHIFUJI Hideaki / 吉藤英明 
521145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_lock_bh();
522145a3621SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway);
523145a3621SYOSHIFUJI Hideaki / 吉藤英明 	if (neigh) {
524145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_lock(&neigh->lock);
525554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
526afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
527398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
528a5a81f0bSPaul Marks 		else if (!(neigh->nud_state & NUD_FAILED))
529afc154e9SHannes Frederic Sowa 			ret = RT6_NUD_SUCCEED;
5307e980569SJiri Benc 		else
5317e980569SJiri Benc 			ret = RT6_NUD_FAIL_PROBE;
532398bcbebSYOSHIFUJI Hideaki #endif
533145a3621SYOSHIFUJI Hideaki / 吉藤英明 		read_unlock(&neigh->lock);
534afc154e9SHannes Frederic Sowa 	} else {
535afc154e9SHannes Frederic Sowa 		ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ?
5367e980569SJiri Benc 		      RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR;
537a5a81f0bSPaul Marks 	}
538145a3621SYOSHIFUJI Hideaki / 吉藤英明 	rcu_read_unlock_bh();
539145a3621SYOSHIFUJI Hideaki / 吉藤英明 
540a5a81f0bSPaul Marks 	return ret;
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
543554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
544554cfb7eSYOSHIFUJI Hideaki 			   int strict)
545554cfb7eSYOSHIFUJI Hideaki {
546a5a81f0bSPaul Marks 	int m;
5474d0c5911SYOSHIFUJI Hideaki 
5484d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
54977d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
550afc154e9SHannes Frederic Sowa 		return RT6_NUD_FAIL_HARD;
551ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
552ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
553ebacaaa0SYOSHIFUJI Hideaki #endif
554afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE) {
555afc154e9SHannes Frederic Sowa 		int n = rt6_check_neigh(rt);
556afc154e9SHannes Frederic Sowa 		if (n < 0)
557afc154e9SHannes Frederic Sowa 			return n;
558afc154e9SHannes Frederic Sowa 	}
559554cfb7eSYOSHIFUJI Hideaki 	return m;
560554cfb7eSYOSHIFUJI Hideaki }
561554cfb7eSYOSHIFUJI Hideaki 
562f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
563afc154e9SHannes Frederic Sowa 				   int *mpri, struct rt6_info *match,
564afc154e9SHannes Frederic Sowa 				   bool *do_rr)
565554cfb7eSYOSHIFUJI Hideaki {
566554cfb7eSYOSHIFUJI Hideaki 	int m;
567afc154e9SHannes Frederic Sowa 	bool match_do_rr = false;
568554cfb7eSYOSHIFUJI Hideaki 
569554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
570f11e6659SDavid S. Miller 		goto out;
571554cfb7eSYOSHIFUJI Hideaki 
572554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
5737e980569SJiri Benc 	if (m == RT6_NUD_FAIL_DO_RR) {
574afc154e9SHannes Frederic Sowa 		match_do_rr = true;
575afc154e9SHannes Frederic Sowa 		m = 0; /* lowest valid score */
5767e980569SJiri Benc 	} else if (m == RT6_NUD_FAIL_HARD) {
577f11e6659SDavid S. Miller 		goto out;
5781da177e4SLinus Torvalds 	}
579f11e6659SDavid S. Miller 
580afc154e9SHannes Frederic Sowa 	if (strict & RT6_LOOKUP_F_REACHABLE)
581afc154e9SHannes Frederic Sowa 		rt6_probe(rt);
582afc154e9SHannes Frederic Sowa 
5837e980569SJiri Benc 	/* note that m can be RT6_NUD_FAIL_PROBE at this point */
584afc154e9SHannes Frederic Sowa 	if (m > *mpri) {
585afc154e9SHannes Frederic Sowa 		*do_rr = match_do_rr;
586afc154e9SHannes Frederic Sowa 		*mpri = m;
587afc154e9SHannes Frederic Sowa 		match = rt;
588afc154e9SHannes Frederic Sowa 	}
589f11e6659SDavid S. Miller out:
590f11e6659SDavid S. Miller 	return match;
5911da177e4SLinus Torvalds }
5921da177e4SLinus Torvalds 
593f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
594f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
595afc154e9SHannes Frederic Sowa 				     u32 metric, int oif, int strict,
596afc154e9SHannes Frederic Sowa 				     bool *do_rr)
597f11e6659SDavid S. Miller {
5989fbdcfafSSteffen Klassert 	struct rt6_info *rt, *match, *cont;
599f11e6659SDavid S. Miller 	int mpri = -1;
600f11e6659SDavid S. Miller 
601f11e6659SDavid S. Miller 	match = NULL;
6029fbdcfafSSteffen Klassert 	cont = NULL;
6039fbdcfafSSteffen Klassert 	for (rt = rr_head; rt; rt = rt->dst.rt6_next) {
6049fbdcfafSSteffen Klassert 		if (rt->rt6i_metric != metric) {
6059fbdcfafSSteffen Klassert 			cont = rt;
6069fbdcfafSSteffen Klassert 			break;
6079fbdcfafSSteffen Klassert 		}
6089fbdcfafSSteffen Klassert 
609afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
6109fbdcfafSSteffen Klassert 	}
6119fbdcfafSSteffen Klassert 
6129fbdcfafSSteffen Klassert 	for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) {
6139fbdcfafSSteffen Klassert 		if (rt->rt6i_metric != metric) {
6149fbdcfafSSteffen Klassert 			cont = rt;
6159fbdcfafSSteffen Klassert 			break;
6169fbdcfafSSteffen Klassert 		}
6179fbdcfafSSteffen Klassert 
6189fbdcfafSSteffen Klassert 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
6199fbdcfafSSteffen Klassert 	}
6209fbdcfafSSteffen Klassert 
6219fbdcfafSSteffen Klassert 	if (match || !cont)
6229fbdcfafSSteffen Klassert 		return match;
6239fbdcfafSSteffen Klassert 
6249fbdcfafSSteffen Klassert 	for (rt = cont; rt; rt = rt->dst.rt6_next)
625afc154e9SHannes Frederic Sowa 		match = find_match(rt, oif, strict, &mpri, match, do_rr);
626f11e6659SDavid S. Miller 
627f11e6659SDavid S. Miller 	return match;
628f11e6659SDavid S. Miller }
629f11e6659SDavid S. Miller 
630f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
631f11e6659SDavid S. Miller {
632f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
6338ed67789SDaniel Lezcano 	struct net *net;
634afc154e9SHannes Frederic Sowa 	bool do_rr = false;
635f11e6659SDavid S. Miller 
636f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
637f11e6659SDavid S. Miller 	if (!rt0)
638f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
639f11e6659SDavid S. Miller 
640afc154e9SHannes Frederic Sowa 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict,
641afc154e9SHannes Frederic Sowa 			     &do_rr);
642f11e6659SDavid S. Miller 
643afc154e9SHannes Frederic Sowa 	if (do_rr) {
644d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
645f11e6659SDavid S. Miller 
646554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
647f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
648f11e6659SDavid S. Miller 			next = fn->leaf;
649f11e6659SDavid S. Miller 
650f11e6659SDavid S. Miller 		if (next != rt0)
651f11e6659SDavid S. Miller 			fn->rr_ptr = next;
652554cfb7eSYOSHIFUJI Hideaki 	}
653554cfb7eSYOSHIFUJI Hideaki 
654d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
655a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
6561da177e4SLinus Torvalds }
6571da177e4SLinus Torvalds 
658*8b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt)
659*8b9df265SMartin KaFai Lau {
660*8b9df265SMartin KaFai Lau 	return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY));
661*8b9df265SMartin KaFai Lau }
662*8b9df265SMartin KaFai Lau 
66370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
66470ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
665b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
66670ceb4f5SYOSHIFUJI Hideaki {
667c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
66870ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
66970ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
67070ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
6714bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
67270ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
67370ceb4f5SYOSHIFUJI Hideaki 
67470ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
67570ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
67670ceb4f5SYOSHIFUJI Hideaki 	}
67770ceb4f5SYOSHIFUJI Hideaki 
67870ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
67970ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
68070ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
68170ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
68270ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
68370ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
68470ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
68570ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
68670ceb4f5SYOSHIFUJI Hideaki 		}
68770ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
68870ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
68970ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
69070ceb4f5SYOSHIFUJI Hideaki 		}
69170ceb4f5SYOSHIFUJI Hideaki 	}
69270ceb4f5SYOSHIFUJI Hideaki 
69370ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
69470ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
6953933fc95SJens Rosenboom 		return -EINVAL;
69670ceb4f5SYOSHIFUJI Hideaki 
6974bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
69870ceb4f5SYOSHIFUJI Hideaki 
69970ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
70070ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
70170ceb4f5SYOSHIFUJI Hideaki 	else {
70270ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
70370ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
70470ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
70570ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
70670ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
70770ceb4f5SYOSHIFUJI Hideaki 	}
70870ceb4f5SYOSHIFUJI Hideaki 
709f104a567SDuan Jiong 	if (rinfo->prefix_len == 0)
710f104a567SDuan Jiong 		rt = rt6_get_dflt_router(gwaddr, dev);
711f104a567SDuan Jiong 	else
712f104a567SDuan Jiong 		rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
713f104a567SDuan Jiong 					gwaddr, dev->ifindex);
71470ceb4f5SYOSHIFUJI Hideaki 
71570ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
716e0a1ad73SThomas Graf 		ip6_del_rt(rt);
71770ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
71870ceb4f5SYOSHIFUJI Hideaki 	}
71970ceb4f5SYOSHIFUJI Hideaki 
72070ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
721efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
72270ceb4f5SYOSHIFUJI Hideaki 					pref);
72370ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
72470ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
72570ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
72670ceb4f5SYOSHIFUJI Hideaki 
72770ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
7281716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
7291716a961SGao feng 			rt6_clean_expires(rt);
7301716a961SGao feng 		else
7311716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
7321716a961SGao feng 
73394e187c0SAmerigo Wang 		ip6_rt_put(rt);
73470ceb4f5SYOSHIFUJI Hideaki 	}
73570ceb4f5SYOSHIFUJI Hideaki 	return 0;
73670ceb4f5SYOSHIFUJI Hideaki }
73770ceb4f5SYOSHIFUJI Hideaki #endif
73870ceb4f5SYOSHIFUJI Hideaki 
739a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
740a3c00e46SMartin KaFai Lau 					struct in6_addr *saddr)
741a3c00e46SMartin KaFai Lau {
742a3c00e46SMartin KaFai Lau 	struct fib6_node *pn;
743a3c00e46SMartin KaFai Lau 	while (1) {
744a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_TL_ROOT)
745a3c00e46SMartin KaFai Lau 			return NULL;
746a3c00e46SMartin KaFai Lau 		pn = fn->parent;
747a3c00e46SMartin KaFai Lau 		if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn)
748a3c00e46SMartin KaFai Lau 			fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr);
749a3c00e46SMartin KaFai Lau 		else
750a3c00e46SMartin KaFai Lau 			fn = pn;
751a3c00e46SMartin KaFai Lau 		if (fn->fn_flags & RTN_RTINFO)
752a3c00e46SMartin KaFai Lau 			return fn;
753a3c00e46SMartin KaFai Lau 	}
754a3c00e46SMartin KaFai Lau }
755c71099acSThomas Graf 
7568ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
7578ed67789SDaniel Lezcano 					     struct fib6_table *table,
7584c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
7591da177e4SLinus Torvalds {
7601da177e4SLinus Torvalds 	struct fib6_node *fn;
7611da177e4SLinus Torvalds 	struct rt6_info *rt;
7621da177e4SLinus Torvalds 
763c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7644c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
765c71099acSThomas Graf restart:
766c71099acSThomas Graf 	rt = fn->leaf;
7674c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
76851ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
76952bd4c0cSNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags);
770a3c00e46SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
771a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
772a3c00e46SMartin KaFai Lau 		if (fn)
773a3c00e46SMartin KaFai Lau 			goto restart;
774a3c00e46SMartin KaFai Lau 	}
775d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
776c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7771da177e4SLinus Torvalds 	return rt;
778c71099acSThomas Graf 
779c71099acSThomas Graf }
780c71099acSThomas Graf 
781ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6,
782ea6e574eSFlorian Westphal 				    int flags)
783ea6e574eSFlorian Westphal {
784ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
785ea6e574eSFlorian Westphal }
786ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
787ea6e574eSFlorian Westphal 
7889acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
7899acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
790c71099acSThomas Graf {
7914c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
7924c9483b2SDavid S. Miller 		.flowi6_oif = oif,
7934c9483b2SDavid S. Miller 		.daddr = *daddr,
794c71099acSThomas Graf 	};
795c71099acSThomas Graf 	struct dst_entry *dst;
79677d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
797c71099acSThomas Graf 
798adaa70bbSThomas Graf 	if (saddr) {
7994c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
800adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
801adaa70bbSThomas Graf 	}
802adaa70bbSThomas Graf 
8034c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
804c71099acSThomas Graf 	if (dst->error == 0)
805c71099acSThomas Graf 		return (struct rt6_info *) dst;
806c71099acSThomas Graf 
807c71099acSThomas Graf 	dst_release(dst);
808c71099acSThomas Graf 
8091da177e4SLinus Torvalds 	return NULL;
8101da177e4SLinus Torvalds }
8117159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
8127159039aSYOSHIFUJI Hideaki 
813c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
8141da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
8151da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
8161da177e4SLinus Torvalds    be destroyed.
8171da177e4SLinus Torvalds  */
8181da177e4SLinus Torvalds 
819e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info,
820e715b6d3SFlorian Westphal 			struct mx6_config *mxc)
8211da177e4SLinus Torvalds {
8221da177e4SLinus Torvalds 	int err;
823c71099acSThomas Graf 	struct fib6_table *table;
8241da177e4SLinus Torvalds 
825c71099acSThomas Graf 	table = rt->rt6i_table;
826c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
827e715b6d3SFlorian Westphal 	err = fib6_add(&table->tb6_root, rt, info, mxc);
828c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	return err;
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
83340e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
83440e22e8fSThomas Graf {
835e715b6d3SFlorian Westphal 	struct nl_info info = {	.nl_net = dev_net(rt->dst.dev), };
836e715b6d3SFlorian Westphal 	struct mx6_config mxc = { .mx = NULL, };
837e715b6d3SFlorian Westphal 
838e715b6d3SFlorian Westphal 	return __ip6_ins_rt(rt, &info, &mxc);
83940e22e8fSThomas Graf }
84040e22e8fSThomas Graf 
841*8b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort,
84221efcfa0SEric Dumazet 					   const struct in6_addr *daddr,
843b71d1d42SEric Dumazet 					   const struct in6_addr *saddr)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds 	struct rt6_info *rt;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	/*
8481da177e4SLinus Torvalds 	 *	Clone the route.
8491da177e4SLinus Torvalds 	 */
8501da177e4SLinus Torvalds 
85121efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	if (rt) {
854*8b9df265SMartin KaFai Lau 		rt->rt6i_flags |= RTF_CACHE;
855*8b9df265SMartin KaFai Lau 
856*8b9df265SMartin KaFai Lau 		if (!rt6_is_gw_or_nonexthop(ort)) {
857bb3c3686SDavid S. Miller 			if (ort->rt6i_dst.plen != 128 &&
85821efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
85958c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
8601da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
8611da177e4SLinus Torvalds 			if (rt->rt6i_src.plen && saddr) {
8624e3fd7a0SAlexey Dobriyan 				rt->rt6i_src.addr = *saddr;
8631da177e4SLinus Torvalds 				rt->rt6i_src.plen = 128;
8641da177e4SLinus Torvalds 			}
8651da177e4SLinus Torvalds #endif
86695a9a5baSYOSHIFUJI Hideaki 		}
8671da177e4SLinus Torvalds 	}
86895a9a5baSYOSHIFUJI Hideaki 
869299d9939SYOSHIFUJI Hideaki 	return rt;
870299d9939SYOSHIFUJI Hideaki }
871299d9939SYOSHIFUJI Hideaki 
8728ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
8734c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
8741da177e4SLinus Torvalds {
875367efcb9SMartin KaFai Lau 	struct fib6_node *fn, *saved_fn;
876519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
877c71099acSThomas Graf 	int strict = 0;
8781da177e4SLinus Torvalds 	int attempts = 3;
879519fbd87SYOSHIFUJI Hideaki 	int err;
8801da177e4SLinus Torvalds 
88177d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
882367efcb9SMartin KaFai Lau 	if (net->ipv6.devconf_all->forwarding == 0)
883367efcb9SMartin KaFai Lau 		strict |= RT6_LOOKUP_F_REACHABLE;
8841da177e4SLinus Torvalds 
885a3c00e46SMartin KaFai Lau redo_fib6_lookup_lock:
886c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8871da177e4SLinus Torvalds 
8884c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
889367efcb9SMartin KaFai Lau 	saved_fn = fn;
8901da177e4SLinus Torvalds 
891a3c00e46SMartin KaFai Lau redo_rt6_select:
892367efcb9SMartin KaFai Lau 	rt = rt6_select(fn, oif, strict);
89352bd4c0cSNicolas Dichtel 	if (rt->rt6i_nsiblings)
894367efcb9SMartin KaFai Lau 		rt = rt6_multipath_select(rt, fl6, oif, strict);
895a3c00e46SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
896a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
897a3c00e46SMartin KaFai Lau 		if (fn)
898a3c00e46SMartin KaFai Lau 			goto redo_rt6_select;
899367efcb9SMartin KaFai Lau 		else if (strict & RT6_LOOKUP_F_REACHABLE) {
900367efcb9SMartin KaFai Lau 			/* also consider unreachable route */
901367efcb9SMartin KaFai Lau 			strict &= ~RT6_LOOKUP_F_REACHABLE;
902367efcb9SMartin KaFai Lau 			fn = saved_fn;
903367efcb9SMartin KaFai Lau 			goto redo_rt6_select;
904367efcb9SMartin KaFai Lau 		} else {
905367efcb9SMartin KaFai Lau 			dst_hold(&rt->dst);
906367efcb9SMartin KaFai Lau 			read_unlock_bh(&table->tb6_lock);
907367efcb9SMartin KaFai Lau 			goto out2;
908367efcb9SMartin KaFai Lau 		}
909a3c00e46SMartin KaFai Lau 	}
910a3c00e46SMartin KaFai Lau 
911d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
912c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9131da177e4SLinus Torvalds 
91494c77bb4SMartin KaFai Lau 	if (rt->rt6i_flags & RTF_CACHE)
91594c77bb4SMartin KaFai Lau 		goto out2;
91694c77bb4SMartin KaFai Lau 
917*8b9df265SMartin KaFai Lau 	if (!rt6_is_gw_or_nonexthop(rt) ||
918*8b9df265SMartin KaFai Lau 	    !(rt->dst.flags & DST_HOST) || !(rt->rt6i_flags & RTF_LOCAL))
919*8b9df265SMartin KaFai Lau 		nrt = ip6_rt_cache_alloc(rt, &fl6->daddr, &fl6->saddr);
9207343ff31SDavid S. Miller 	else
9217343ff31SDavid S. Miller 		goto out2;
9221da177e4SLinus Torvalds 
92394e187c0SAmerigo Wang 	ip6_rt_put(rt);
9248ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
9251da177e4SLinus Torvalds 
926d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
927e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
92840e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
929e40cf353SYOSHIFUJI Hideaki 		if (!err)
930e40cf353SYOSHIFUJI Hideaki 			goto out2;
931e40cf353SYOSHIFUJI Hideaki 	}
932e40cf353SYOSHIFUJI Hideaki 
933e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
9341da177e4SLinus Torvalds 		goto out2;
9351da177e4SLinus Torvalds 
936519fbd87SYOSHIFUJI Hideaki 	/*
937c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
938519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
9391da177e4SLinus Torvalds 	 */
94094e187c0SAmerigo Wang 	ip6_rt_put(rt);
941a3c00e46SMartin KaFai Lau 	goto redo_fib6_lookup_lock;
942e40cf353SYOSHIFUJI Hideaki 
9431da177e4SLinus Torvalds out2:
9444b32b5adSMartin KaFai Lau 	rt6_dst_from_metrics_check(rt);
945d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
946d8d1f30bSChangli Gao 	rt->dst.__use++;
947c71099acSThomas Graf 
948c71099acSThomas Graf 	return rt;
949c71099acSThomas Graf }
950c71099acSThomas Graf 
9518ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9524c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9534acad72dSPavel Emelyanov {
9544c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9554acad72dSPavel Emelyanov }
9564acad72dSPavel Emelyanov 
95772331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
95872331bc0SShmulik Ladkani 						struct net_device *dev,
95972331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
96072331bc0SShmulik Ladkani {
96172331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
96272331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
96372331bc0SShmulik Ladkani 
96472331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
96572331bc0SShmulik Ladkani }
96672331bc0SShmulik Ladkani 
967c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
968c71099acSThomas Graf {
969b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
970c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
971adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
9724c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9734c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
9744c9483b2SDavid S. Miller 		.daddr = iph->daddr,
9754c9483b2SDavid S. Miller 		.saddr = iph->saddr,
9766502ca52SYOSHIFUJI Hideaki / 吉藤英明 		.flowlabel = ip6_flowinfo(iph),
9774c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
9784c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
979c71099acSThomas Graf 	};
980adaa70bbSThomas Graf 
98172331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
982c71099acSThomas Graf }
983c71099acSThomas Graf 
9848ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
9854c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
986c71099acSThomas Graf {
9874c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
988c71099acSThomas Graf }
989c71099acSThomas Graf 
9909c7a4f9cSFlorian Westphal struct dst_entry *ip6_route_output(struct net *net, const struct sock *sk,
9914c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
992c71099acSThomas Graf {
993c71099acSThomas Graf 	int flags = 0;
994c71099acSThomas Graf 
9951fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
9964dc27d1cSDavid McCullough 
9974c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
99877d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
999c71099acSThomas Graf 
10004c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
1001adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
10020c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
10030c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
1004adaa70bbSThomas Graf 
10054c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
10061da177e4SLinus Torvalds }
10077159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
10081da177e4SLinus Torvalds 
10092774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
101014e50e57SDavid S. Miller {
10115c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
101214e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
101314e50e57SDavid S. Miller 
1014f5b0a874SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
101514e50e57SDavid S. Miller 	if (rt) {
1016d8d1f30bSChangli Gao 		new = &rt->dst;
101714e50e57SDavid S. Miller 
10188104891bSSteffen Klassert 		memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
10198104891bSSteffen Klassert 
102014e50e57SDavid S. Miller 		new->__use = 1;
1021352e512cSHerbert Xu 		new->input = dst_discard;
1022aad88724SEric Dumazet 		new->output = dst_discard_sk;
102314e50e57SDavid S. Miller 
102421efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
102521efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
102621efcfa0SEric Dumazet 		else
1027defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
102814e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
102914e50e57SDavid S. Miller 		if (rt->rt6i_idev)
103014e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
103114e50e57SDavid S. Miller 
10324e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
10331716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
103414e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
103514e50e57SDavid S. Miller 
103614e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
103714e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
103814e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
103914e50e57SDavid S. Miller #endif
104014e50e57SDavid S. Miller 
104114e50e57SDavid S. Miller 		dst_free(new);
104214e50e57SDavid S. Miller 	}
104314e50e57SDavid S. Miller 
104469ead7afSDavid S. Miller 	dst_release(dst_orig);
104569ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
104614e50e57SDavid S. Miller }
104714e50e57SDavid S. Miller 
10481da177e4SLinus Torvalds /*
10491da177e4SLinus Torvalds  *	Destination cache support functions
10501da177e4SLinus Torvalds  */
10511da177e4SLinus Torvalds 
10524b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt)
10534b32b5adSMartin KaFai Lau {
10544b32b5adSMartin KaFai Lau 	if (rt->dst.from &&
10554b32b5adSMartin KaFai Lau 	    dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from))
10564b32b5adSMartin KaFai Lau 		dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true);
10574b32b5adSMartin KaFai Lau }
10584b32b5adSMartin KaFai Lau 
10591da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10601da177e4SLinus Torvalds {
10611da177e4SLinus Torvalds 	struct rt6_info *rt;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10641da177e4SLinus Torvalds 
10656f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
10666f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
10676f3118b5SNicolas Dichtel 	 * into this function always.
10686f3118b5SNicolas Dichtel 	 */
1069e3bc10bdSHannes Frederic Sowa 	if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie))
10701da177e4SLinus Torvalds 		return NULL;
1071e3bc10bdSHannes Frederic Sowa 
1072e3bc10bdSHannes Frederic Sowa 	if (rt6_check_expired(rt))
1073e3bc10bdSHannes Frederic Sowa 		return NULL;
1074e3bc10bdSHannes Frederic Sowa 
10754b32b5adSMartin KaFai Lau 	rt6_dst_from_metrics_check(rt);
10764b32b5adSMartin KaFai Lau 
1077e3bc10bdSHannes Frederic Sowa 	return dst;
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
10811da177e4SLinus Torvalds {
10821da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	if (rt) {
108554c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
108654c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1087e0a1ad73SThomas Graf 				ip6_del_rt(rt);
108854c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
10891da177e4SLinus Torvalds 			}
109054c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
109154c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
109254c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
109354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
109454c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
109554c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
10991da177e4SLinus Torvalds {
11001da177e4SLinus Torvalds 	struct rt6_info *rt;
11011da177e4SLinus Torvalds 
11023ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
11031da177e4SLinus Torvalds 
1104adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
11051da177e4SLinus Torvalds 	if (rt) {
11061eb4f758SHannes Frederic Sowa 		if (rt->rt6i_flags & RTF_CACHE) {
11071eb4f758SHannes Frederic Sowa 			dst_hold(&rt->dst);
11081eb4f758SHannes Frederic Sowa 			if (ip6_del_rt(rt))
11091eb4f758SHannes Frederic Sowa 				dst_free(&rt->dst);
11101eb4f758SHannes Frederic Sowa 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) {
11111da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
11121da177e4SLinus Torvalds 		}
11131da177e4SLinus Torvalds 	}
11141eb4f758SHannes Frederic Sowa }
11151da177e4SLinus Torvalds 
11166700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
11176700c270SDavid S. Miller 			       struct sk_buff *skb, u32 mtu)
11181da177e4SLinus Torvalds {
11191da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info *)dst;
11201da177e4SLinus Torvalds 
112181aded24SDavid S. Miller 	dst_confirm(dst);
1122653437d0SMartin KaFai Lau 	if (mtu < dst_mtu(dst) && (rt6->rt6i_flags & RTF_CACHE)) {
112381aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
112481aded24SDavid S. Miller 
11251da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
11269d289715SHagen Paul Pfeifer 		if (mtu < IPV6_MIN_MTU)
11271da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
11289d289715SHagen Paul Pfeifer 
11294b32b5adSMartin KaFai Lau 		rt6->rt6i_pmtu = mtu;
113081aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
11311da177e4SLinus Torvalds 	}
11321da177e4SLinus Torvalds }
11331da177e4SLinus Torvalds 
113442ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
113542ae66c8SDavid S. Miller 		     int oif, u32 mark)
113681aded24SDavid S. Miller {
113781aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
113881aded24SDavid S. Miller 	struct dst_entry *dst;
113981aded24SDavid S. Miller 	struct flowi6 fl6;
114081aded24SDavid S. Miller 
114181aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
114281aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
11431b3c61dcSLorenzo Colitti 	fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark);
114481aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
114581aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
11466502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
114781aded24SDavid S. Miller 
114881aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
114981aded24SDavid S. Miller 	if (!dst->error)
11506700c270SDavid S. Miller 		ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
115181aded24SDavid S. Miller 	dst_release(dst);
115281aded24SDavid S. Miller }
115381aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
115481aded24SDavid S. Miller 
115581aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
115681aded24SDavid S. Miller {
115781aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
115881aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
115981aded24SDavid S. Miller }
116081aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
116181aded24SDavid S. Miller 
1162b55b76b2SDuan Jiong /* Handle redirects */
1163b55b76b2SDuan Jiong struct ip6rd_flowi {
1164b55b76b2SDuan Jiong 	struct flowi6 fl6;
1165b55b76b2SDuan Jiong 	struct in6_addr gateway;
1166b55b76b2SDuan Jiong };
1167b55b76b2SDuan Jiong 
1168b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net,
1169b55b76b2SDuan Jiong 					     struct fib6_table *table,
1170b55b76b2SDuan Jiong 					     struct flowi6 *fl6,
1171b55b76b2SDuan Jiong 					     int flags)
1172b55b76b2SDuan Jiong {
1173b55b76b2SDuan Jiong 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1174b55b76b2SDuan Jiong 	struct rt6_info *rt;
1175b55b76b2SDuan Jiong 	struct fib6_node *fn;
1176b55b76b2SDuan Jiong 
1177b55b76b2SDuan Jiong 	/* Get the "current" route for this destination and
1178b55b76b2SDuan Jiong 	 * check if the redirect has come from approriate router.
1179b55b76b2SDuan Jiong 	 *
1180b55b76b2SDuan Jiong 	 * RFC 4861 specifies that redirects should only be
1181b55b76b2SDuan Jiong 	 * accepted if they come from the nexthop to the target.
1182b55b76b2SDuan Jiong 	 * Due to the way the routes are chosen, this notion
1183b55b76b2SDuan Jiong 	 * is a bit fuzzy and one might need to check all possible
1184b55b76b2SDuan Jiong 	 * routes.
1185b55b76b2SDuan Jiong 	 */
1186b55b76b2SDuan Jiong 
1187b55b76b2SDuan Jiong 	read_lock_bh(&table->tb6_lock);
1188b55b76b2SDuan Jiong 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1189b55b76b2SDuan Jiong restart:
1190b55b76b2SDuan Jiong 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1191b55b76b2SDuan Jiong 		if (rt6_check_expired(rt))
1192b55b76b2SDuan Jiong 			continue;
1193b55b76b2SDuan Jiong 		if (rt->dst.error)
1194b55b76b2SDuan Jiong 			break;
1195b55b76b2SDuan Jiong 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1196b55b76b2SDuan Jiong 			continue;
1197b55b76b2SDuan Jiong 		if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1198b55b76b2SDuan Jiong 			continue;
1199b55b76b2SDuan Jiong 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1200b55b76b2SDuan Jiong 			continue;
1201b55b76b2SDuan Jiong 		break;
1202b55b76b2SDuan Jiong 	}
1203b55b76b2SDuan Jiong 
1204b55b76b2SDuan Jiong 	if (!rt)
1205b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
1206b55b76b2SDuan Jiong 	else if (rt->dst.error) {
1207b55b76b2SDuan Jiong 		rt = net->ipv6.ip6_null_entry;
1208b0a1ba59SMartin KaFai Lau 		goto out;
1209b0a1ba59SMartin KaFai Lau 	}
1210b0a1ba59SMartin KaFai Lau 
1211b0a1ba59SMartin KaFai Lau 	if (rt == net->ipv6.ip6_null_entry) {
1212a3c00e46SMartin KaFai Lau 		fn = fib6_backtrack(fn, &fl6->saddr);
1213a3c00e46SMartin KaFai Lau 		if (fn)
1214a3c00e46SMartin KaFai Lau 			goto restart;
1215b55b76b2SDuan Jiong 	}
1216a3c00e46SMartin KaFai Lau 
1217b0a1ba59SMartin KaFai Lau out:
1218b55b76b2SDuan Jiong 	dst_hold(&rt->dst);
1219b55b76b2SDuan Jiong 
1220b55b76b2SDuan Jiong 	read_unlock_bh(&table->tb6_lock);
1221b55b76b2SDuan Jiong 
1222b55b76b2SDuan Jiong 	return rt;
1223b55b76b2SDuan Jiong };
1224b55b76b2SDuan Jiong 
1225b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net,
1226b55b76b2SDuan Jiong 					const struct flowi6 *fl6,
1227b55b76b2SDuan Jiong 					const struct in6_addr *gateway)
1228b55b76b2SDuan Jiong {
1229b55b76b2SDuan Jiong 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1230b55b76b2SDuan Jiong 	struct ip6rd_flowi rdfl;
1231b55b76b2SDuan Jiong 
1232b55b76b2SDuan Jiong 	rdfl.fl6 = *fl6;
1233b55b76b2SDuan Jiong 	rdfl.gateway = *gateway;
1234b55b76b2SDuan Jiong 
1235b55b76b2SDuan Jiong 	return fib6_rule_lookup(net, &rdfl.fl6,
1236b55b76b2SDuan Jiong 				flags, __ip6_route_redirect);
1237b55b76b2SDuan Jiong }
1238b55b76b2SDuan Jiong 
12393a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
12403a5ad2eeSDavid S. Miller {
12413a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
12423a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
12433a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
12443a5ad2eeSDavid S. Miller 
12453a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
1246e374c618SJulian Anastasov 	fl6.flowi6_iif = LOOPBACK_IFINDEX;
12473a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
12483a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
12493a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
12503a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
12516502ca52SYOSHIFUJI Hideaki / 吉藤英明 	fl6.flowlabel = ip6_flowinfo(iph);
12523a5ad2eeSDavid S. Miller 
1253b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr);
12546700c270SDavid S. Miller 	rt6_do_redirect(dst, NULL, skb);
12553a5ad2eeSDavid S. Miller 	dst_release(dst);
12563a5ad2eeSDavid S. Miller }
12573a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
12583a5ad2eeSDavid S. Miller 
1259c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1260c92a59ecSDuan Jiong 			    u32 mark)
1261c92a59ecSDuan Jiong {
1262c92a59ecSDuan Jiong 	const struct ipv6hdr *iph = ipv6_hdr(skb);
1263c92a59ecSDuan Jiong 	const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb);
1264c92a59ecSDuan Jiong 	struct dst_entry *dst;
1265c92a59ecSDuan Jiong 	struct flowi6 fl6;
1266c92a59ecSDuan Jiong 
1267c92a59ecSDuan Jiong 	memset(&fl6, 0, sizeof(fl6));
1268e374c618SJulian Anastasov 	fl6.flowi6_iif = LOOPBACK_IFINDEX;
1269c92a59ecSDuan Jiong 	fl6.flowi6_oif = oif;
1270c92a59ecSDuan Jiong 	fl6.flowi6_mark = mark;
1271c92a59ecSDuan Jiong 	fl6.daddr = msg->dest;
1272c92a59ecSDuan Jiong 	fl6.saddr = iph->daddr;
1273c92a59ecSDuan Jiong 
1274b55b76b2SDuan Jiong 	dst = ip6_route_redirect(net, &fl6, &iph->saddr);
1275c92a59ecSDuan Jiong 	rt6_do_redirect(dst, NULL, skb);
1276c92a59ecSDuan Jiong 	dst_release(dst);
1277c92a59ecSDuan Jiong }
1278c92a59ecSDuan Jiong 
12793a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
12803a5ad2eeSDavid S. Miller {
12813a5ad2eeSDavid S. Miller 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
12823a5ad2eeSDavid S. Miller }
12833a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
12843a5ad2eeSDavid S. Miller 
12850dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
12861da177e4SLinus Torvalds {
12870dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
12880dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
12890dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
12900dbaee3bSDavid S. Miller 
12911da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
12921da177e4SLinus Torvalds 
12935578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
12945578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	/*
12971da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
12981da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
12991da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
13001da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
13011da177e4SLinus Torvalds 	 */
13021da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
13031da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
13041da177e4SLinus Torvalds 	return mtu;
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds 
1307ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1308d33e4553SDavid S. Miller {
13094b32b5adSMartin KaFai Lau 	const struct rt6_info *rt = (const struct rt6_info *)dst;
13104b32b5adSMartin KaFai Lau 	unsigned int mtu = rt->rt6i_pmtu;
1311d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1312618f9bc7SSteffen Klassert 
1313618f9bc7SSteffen Klassert 	if (mtu)
131430f78d8eSEric Dumazet 		goto out;
1315618f9bc7SSteffen Klassert 
13164b32b5adSMartin KaFai Lau 	mtu = dst_metric_raw(dst, RTAX_MTU);
13174b32b5adSMartin KaFai Lau 	if (mtu)
13184b32b5adSMartin KaFai Lau 		goto out;
13194b32b5adSMartin KaFai Lau 
1320618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1321d33e4553SDavid S. Miller 
1322d33e4553SDavid S. Miller 	rcu_read_lock();
1323d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1324d33e4553SDavid S. Miller 	if (idev)
1325d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1326d33e4553SDavid S. Miller 	rcu_read_unlock();
1327d33e4553SDavid S. Miller 
132830f78d8eSEric Dumazet out:
132930f78d8eSEric Dumazet 	return min_t(unsigned int, mtu, IP6_MAX_MTU);
1330d33e4553SDavid S. Miller }
1331d33e4553SDavid S. Miller 
13323b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
13333b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
13345d0bbeebSThomas Graf 
13353b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
133687a11578SDavid S. Miller 				  struct flowi6 *fl6)
13371da177e4SLinus Torvalds {
133887a11578SDavid S. Miller 	struct dst_entry *dst;
13391da177e4SLinus Torvalds 	struct rt6_info *rt;
13401da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1341c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
13421da177e4SLinus Torvalds 
134338308473SDavid S. Miller 	if (unlikely(!idev))
1344122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
13451da177e4SLinus Torvalds 
13468b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
134738308473SDavid S. Miller 	if (unlikely(!rt)) {
13481da177e4SLinus Torvalds 		in6_dev_put(idev);
134987a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
13501da177e4SLinus Torvalds 		goto out;
13511da177e4SLinus Torvalds 	}
13521da177e4SLinus Torvalds 
13538e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
13548e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
1355d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
1356550bab42SJulian Anastasov 	rt->rt6i_gateway  = fl6->daddr;
135787a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
13588e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
13598e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
136014edd87dSLi RongQing 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0);
13611da177e4SLinus Torvalds 
13623b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1363d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1364d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
13653b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
13661da177e4SLinus Torvalds 
13675578689aSDaniel Lezcano 	fib6_force_start_gc(net);
13681da177e4SLinus Torvalds 
136987a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
137087a11578SDavid S. Miller 
13711da177e4SLinus Torvalds out:
137287a11578SDavid S. Miller 	return dst;
13731da177e4SLinus Torvalds }
13741da177e4SLinus Torvalds 
13753d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
13761da177e4SLinus Torvalds {
1377e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
13783d0f24a7SStephen Hemminger 	int more = 0;
13791da177e4SLinus Torvalds 
13803b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
13813b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
13825d0bbeebSThomas Graf 
13831da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
13841da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
13851da177e4SLinus Torvalds 			*pprev = dst->next;
13861da177e4SLinus Torvalds 			dst_free(dst);
13871da177e4SLinus Torvalds 		} else {
13881da177e4SLinus Torvalds 			pprev = &dst->next;
13893d0f24a7SStephen Hemminger 			++more;
13901da177e4SLinus Torvalds 		}
13911da177e4SLinus Torvalds 	}
13921da177e4SLinus Torvalds 
13933b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
13945d0bbeebSThomas Graf 
13953d0f24a7SStephen Hemminger 	return more;
13961da177e4SLinus Torvalds }
13971da177e4SLinus Torvalds 
13981e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
13991e493d19SDavid S. Miller 			    void *arg)
14001e493d19SDavid S. Miller {
14011e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
14021e493d19SDavid S. Miller 
14031e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
14041e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
14051e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
14061e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
14071e493d19SDavid S. Miller 		if (func(rt, arg)) {
14081e493d19SDavid S. Miller 			*pprev = dst->next;
14091e493d19SDavid S. Miller 			dst_free(dst);
14101e493d19SDavid S. Miller 		} else {
14111e493d19SDavid S. Miller 			pprev = &dst->next;
14121e493d19SDavid S. Miller 		}
14131e493d19SDavid S. Miller 	}
14141e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
14151e493d19SDavid S. Miller }
14161e493d19SDavid S. Miller 
1417569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
14181da177e4SLinus Torvalds {
141986393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
14207019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
14217019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
14227019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
14237019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
14247019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1425fc66f95cSEric Dumazet 	int entries;
14261da177e4SLinus Torvalds 
1427fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
142849a18d86SMichal Kubeček 	if (time_after(rt_last_gc + rt_min_interval, jiffies) &&
1429fc66f95cSEric Dumazet 	    entries <= rt_max_size)
14301da177e4SLinus Torvalds 		goto out;
14311da177e4SLinus Torvalds 
14326891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
143314956643SLi RongQing 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true);
1434fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1435fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
14367019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
14371da177e4SLinus Torvalds out:
14387019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1439fc66f95cSEric Dumazet 	return entries > rt_max_size;
14401da177e4SLinus Torvalds }
14411da177e4SLinus Torvalds 
1442e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc,
1443e715b6d3SFlorian Westphal 			       const struct fib6_config *cfg)
1444e715b6d3SFlorian Westphal {
1445e715b6d3SFlorian Westphal 	struct nlattr *nla;
1446e715b6d3SFlorian Westphal 	int remaining;
1447e715b6d3SFlorian Westphal 	u32 *mp;
1448e715b6d3SFlorian Westphal 
144963159f29SIan Morris 	if (!cfg->fc_mx)
1450e715b6d3SFlorian Westphal 		return 0;
1451e715b6d3SFlorian Westphal 
1452e715b6d3SFlorian Westphal 	mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
1453e715b6d3SFlorian Westphal 	if (unlikely(!mp))
1454e715b6d3SFlorian Westphal 		return -ENOMEM;
1455e715b6d3SFlorian Westphal 
1456e715b6d3SFlorian Westphal 	nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1457e715b6d3SFlorian Westphal 		int type = nla_type(nla);
1458e715b6d3SFlorian Westphal 
1459e715b6d3SFlorian Westphal 		if (type) {
1460ea697639SDaniel Borkmann 			u32 val;
1461ea697639SDaniel Borkmann 
1462e715b6d3SFlorian Westphal 			if (unlikely(type > RTAX_MAX))
1463e715b6d3SFlorian Westphal 				goto err;
1464ea697639SDaniel Borkmann 			if (type == RTAX_CC_ALGO) {
1465ea697639SDaniel Borkmann 				char tmp[TCP_CA_NAME_MAX];
1466e715b6d3SFlorian Westphal 
1467ea697639SDaniel Borkmann 				nla_strlcpy(tmp, nla, sizeof(tmp));
1468ea697639SDaniel Borkmann 				val = tcp_ca_get_key_by_name(tmp);
1469ea697639SDaniel Borkmann 				if (val == TCP_CA_UNSPEC)
1470ea697639SDaniel Borkmann 					goto err;
1471ea697639SDaniel Borkmann 			} else {
1472ea697639SDaniel Borkmann 				val = nla_get_u32(nla);
1473ea697639SDaniel Borkmann 			}
1474ea697639SDaniel Borkmann 
1475ea697639SDaniel Borkmann 			mp[type - 1] = val;
1476e715b6d3SFlorian Westphal 			__set_bit(type - 1, mxc->mx_valid);
1477e715b6d3SFlorian Westphal 		}
1478e715b6d3SFlorian Westphal 	}
1479e715b6d3SFlorian Westphal 
1480e715b6d3SFlorian Westphal 	mxc->mx = mp;
1481e715b6d3SFlorian Westphal 
1482e715b6d3SFlorian Westphal 	return 0;
1483e715b6d3SFlorian Westphal  err:
1484e715b6d3SFlorian Westphal 	kfree(mp);
1485e715b6d3SFlorian Westphal 	return -EINVAL;
1486e715b6d3SFlorian Westphal }
14871da177e4SLinus Torvalds 
148886872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
14891da177e4SLinus Torvalds {
14901da177e4SLinus Torvalds 	int err;
14915578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
14921da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
14931da177e4SLinus Torvalds 	struct net_device *dev = NULL;
14941da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1495c71099acSThomas Graf 	struct fib6_table *table;
1496e715b6d3SFlorian Westphal 	struct mx6_config mxc = { .mx = NULL, };
14971da177e4SLinus Torvalds 	int addr_type;
14981da177e4SLinus Torvalds 
149986872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
15001da177e4SLinus Torvalds 		return -EINVAL;
15011da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
150286872cb5SThomas Graf 	if (cfg->fc_src_len)
15031da177e4SLinus Torvalds 		return -EINVAL;
15041da177e4SLinus Torvalds #endif
150586872cb5SThomas Graf 	if (cfg->fc_ifindex) {
15061da177e4SLinus Torvalds 		err = -ENODEV;
15075578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
15081da177e4SLinus Torvalds 		if (!dev)
15091da177e4SLinus Torvalds 			goto out;
15101da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
15111da177e4SLinus Torvalds 		if (!idev)
15121da177e4SLinus Torvalds 			goto out;
15131da177e4SLinus Torvalds 	}
15141da177e4SLinus Torvalds 
151586872cb5SThomas Graf 	if (cfg->fc_metric == 0)
151686872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
15171da177e4SLinus Torvalds 
1518c71099acSThomas Graf 	err = -ENOBUFS;
151938308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1520d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1521d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
152238308473SDavid S. Miller 		if (!table) {
1523f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1524d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1525d71314b4SMatti Vaittinen 		}
1526d71314b4SMatti Vaittinen 	} else {
1527d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1528d71314b4SMatti Vaittinen 	}
152938308473SDavid S. Miller 
153038308473SDavid S. Miller 	if (!table)
1531c71099acSThomas Graf 		goto out;
1532c71099acSThomas Graf 
1533c88507fbSSabrina Dubroca 	rt = ip6_dst_alloc(net, NULL, (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT, table);
15341da177e4SLinus Torvalds 
153538308473SDavid S. Miller 	if (!rt) {
15361da177e4SLinus Torvalds 		err = -ENOMEM;
15371da177e4SLinus Torvalds 		goto out;
15381da177e4SLinus Torvalds 	}
15391da177e4SLinus Torvalds 
15401716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
15411716a961SGao feng 		rt6_set_expires(rt, jiffies +
15421716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
15431716a961SGao feng 	else
15441716a961SGao feng 		rt6_clean_expires(rt);
15451da177e4SLinus Torvalds 
154686872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
154786872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
154886872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
154986872cb5SThomas Graf 
155086872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1553d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1554ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1555ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
15561da177e4SLinus Torvalds 	else
1557d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
15581da177e4SLinus Torvalds 
1559d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
15601da177e4SLinus Torvalds 
156186872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
156286872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
1563afc4eef8SMartin KaFai Lau 	if (rt->rt6i_dst.plen == 128)
156411d53b49SDavid S. Miller 		rt->dst.flags |= DST_HOST;
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
156786872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
156886872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
15691da177e4SLinus Torvalds #endif
15701da177e4SLinus Torvalds 
157186872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
15741da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
15751da177e4SLinus Torvalds 	 */
157686872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
157738308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
157838308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
157938308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
15801da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
15815578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
15821da177e4SLinus Torvalds 			if (dev) {
15831da177e4SLinus Torvalds 				dev_put(dev);
15841da177e4SLinus Torvalds 				in6_dev_put(idev);
15851da177e4SLinus Torvalds 			}
15865578689aSDaniel Lezcano 			dev = net->loopback_dev;
15871da177e4SLinus Torvalds 			dev_hold(dev);
15881da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
15891da177e4SLinus Torvalds 			if (!idev) {
15901da177e4SLinus Torvalds 				err = -ENODEV;
15911da177e4SLinus Torvalds 				goto out;
15921da177e4SLinus Torvalds 			}
15931da177e4SLinus Torvalds 		}
15941da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1595ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
1596ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
1597ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
1598aad88724SEric Dumazet 			rt->dst.output = dst_discard_sk;
15997150aedeSKamala R 			rt->dst.input = dst_discard;
1600ef2c7d7bSNicolas Dichtel 			break;
1601ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
1602ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
16037150aedeSKamala R 			rt->dst.output = ip6_pkt_prohibit_out;
16047150aedeSKamala R 			rt->dst.input = ip6_pkt_prohibit;
1605ef2c7d7bSNicolas Dichtel 			break;
1606b4949ab2SNicolas Dichtel 		case RTN_THROW:
1607ef2c7d7bSNicolas Dichtel 		default:
16087150aedeSKamala R 			rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
16097150aedeSKamala R 					: -ENETUNREACH;
16107150aedeSKamala R 			rt->dst.output = ip6_pkt_discard_out;
16117150aedeSKamala R 			rt->dst.input = ip6_pkt_discard;
1612ef2c7d7bSNicolas Dichtel 			break;
1613ef2c7d7bSNicolas Dichtel 		}
16141da177e4SLinus Torvalds 		goto install_route;
16151da177e4SLinus Torvalds 	}
16161da177e4SLinus Torvalds 
161786872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1618b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
16191da177e4SLinus Torvalds 		int gwa_type;
16201da177e4SLinus Torvalds 
162186872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
162248ed7b26SFlorian Westphal 
162348ed7b26SFlorian Westphal 		/* if gw_addr is local we will fail to detect this in case
162448ed7b26SFlorian Westphal 		 * address is still TENTATIVE (DAD in progress). rt6_lookup()
162548ed7b26SFlorian Westphal 		 * will return already-added prefix route via interface that
162648ed7b26SFlorian Westphal 		 * prefix route was assigned to, which might be non-loopback.
162748ed7b26SFlorian Westphal 		 */
162848ed7b26SFlorian Westphal 		err = -EINVAL;
162948ed7b26SFlorian Westphal 		if (ipv6_chk_addr_and_flags(net, gw_addr, NULL, 0, 0))
163048ed7b26SFlorian Westphal 			goto out;
163148ed7b26SFlorian Westphal 
16324e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
16331da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
16361da177e4SLinus Torvalds 			struct rt6_info *grt;
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
16391da177e4SLinus Torvalds 			   addresses as nexthop address.
16401da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
16411da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
16421da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
16431da177e4SLinus Torvalds 			   some exceptions. --ANK
16441da177e4SLinus Torvalds 			 */
16451da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
16461da177e4SLinus Torvalds 				goto out;
16471da177e4SLinus Torvalds 
16485578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
165138308473SDavid S. Miller 			if (!grt)
16521da177e4SLinus Torvalds 				goto out;
16531da177e4SLinus Torvalds 			if (dev) {
1654d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
165594e187c0SAmerigo Wang 					ip6_rt_put(grt);
16561da177e4SLinus Torvalds 					goto out;
16571da177e4SLinus Torvalds 				}
16581da177e4SLinus Torvalds 			} else {
1659d1918542SDavid S. Miller 				dev = grt->dst.dev;
16601da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
16611da177e4SLinus Torvalds 				dev_hold(dev);
16621da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
16631da177e4SLinus Torvalds 			}
16641da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
16651da177e4SLinus Torvalds 				err = 0;
166694e187c0SAmerigo Wang 			ip6_rt_put(grt);
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 			if (err)
16691da177e4SLinus Torvalds 				goto out;
16701da177e4SLinus Torvalds 		}
16711da177e4SLinus Torvalds 		err = -EINVAL;
167238308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
16731da177e4SLinus Torvalds 			goto out;
16741da177e4SLinus Torvalds 	}
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	err = -ENODEV;
167738308473SDavid S. Miller 	if (!dev)
16781da177e4SLinus Torvalds 		goto out;
16791da177e4SLinus Torvalds 
1680c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1681c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1682c3968a85SDaniel Walter 			err = -EINVAL;
1683c3968a85SDaniel Walter 			goto out;
1684c3968a85SDaniel Walter 		}
16854e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1686c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1687c3968a85SDaniel Walter 	} else
1688c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1689c3968a85SDaniel Walter 
169086872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds install_route:
1693d8d1f30bSChangli Gao 	rt->dst.dev = dev;
16941da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1695c71099acSThomas Graf 	rt->rt6i_table = table;
169663152fc0SDaniel Lezcano 
1697c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
169863152fc0SDaniel Lezcano 
1699e715b6d3SFlorian Westphal 	err = ip6_convert_metrics(&mxc, cfg);
1700e715b6d3SFlorian Westphal 	if (err)
1701e715b6d3SFlorian Westphal 		goto out;
17021da177e4SLinus Torvalds 
1703e715b6d3SFlorian Westphal 	err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc);
1704e715b6d3SFlorian Westphal 
1705e715b6d3SFlorian Westphal 	kfree(mxc.mx);
1706e715b6d3SFlorian Westphal 	return err;
17071da177e4SLinus Torvalds out:
17081da177e4SLinus Torvalds 	if (dev)
17091da177e4SLinus Torvalds 		dev_put(dev);
17101da177e4SLinus Torvalds 	if (idev)
17111da177e4SLinus Torvalds 		in6_dev_put(idev);
17121da177e4SLinus Torvalds 	if (rt)
1713d8d1f30bSChangli Gao 		dst_free(&rt->dst);
17141da177e4SLinus Torvalds 	return err;
17151da177e4SLinus Torvalds }
17161da177e4SLinus Torvalds 
171786872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
17181da177e4SLinus Torvalds {
17191da177e4SLinus Torvalds 	int err;
1720c71099acSThomas Graf 	struct fib6_table *table;
1721d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
17221da177e4SLinus Torvalds 
17236825a26cSGao feng 	if (rt == net->ipv6.ip6_null_entry) {
17246825a26cSGao feng 		err = -ENOENT;
17256825a26cSGao feng 		goto out;
17266825a26cSGao feng 	}
17276c813a72SPatrick McHardy 
1728c71099acSThomas Graf 	table = rt->rt6i_table;
1729c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
173086872cb5SThomas Graf 	err = fib6_del(rt, info);
1731c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
17321da177e4SLinus Torvalds 
17336825a26cSGao feng out:
173494e187c0SAmerigo Wang 	ip6_rt_put(rt);
17351da177e4SLinus Torvalds 	return err;
17361da177e4SLinus Torvalds }
17371da177e4SLinus Torvalds 
1738e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1739e0a1ad73SThomas Graf {
17404d1169c1SDenis V. Lunev 	struct nl_info info = {
1741d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
17424d1169c1SDenis V. Lunev 	};
1743528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1744e0a1ad73SThomas Graf }
1745e0a1ad73SThomas Graf 
174686872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
17471da177e4SLinus Torvalds {
1748c71099acSThomas Graf 	struct fib6_table *table;
17491da177e4SLinus Torvalds 	struct fib6_node *fn;
17501da177e4SLinus Torvalds 	struct rt6_info *rt;
17511da177e4SLinus Torvalds 	int err = -ESRCH;
17521da177e4SLinus Torvalds 
17535578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
175438308473SDavid S. Miller 	if (!table)
1755c71099acSThomas Graf 		return err;
17561da177e4SLinus Torvalds 
1757c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1758c71099acSThomas Graf 
1759c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
176086872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
176186872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds 	if (fn) {
1764d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
17651f56a01fSMartin KaFai Lau 			if ((rt->rt6i_flags & RTF_CACHE) &&
17661f56a01fSMartin KaFai Lau 			    !(cfg->fc_flags & RTF_CACHE))
17671f56a01fSMartin KaFai Lau 				continue;
176886872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1769d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1770d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
17711da177e4SLinus Torvalds 				continue;
177286872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
177386872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
17741da177e4SLinus Torvalds 				continue;
177586872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
17761da177e4SLinus Torvalds 				continue;
1777d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1778c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
17791da177e4SLinus Torvalds 
178086872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
17811da177e4SLinus Torvalds 		}
17821da177e4SLinus Torvalds 	}
1783c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	return err;
17861da177e4SLinus Torvalds }
17871da177e4SLinus Torvalds 
17886700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
1789a6279458SYOSHIFUJI Hideaki {
1790e8599ff4SDavid S. Miller 	struct net *net = dev_net(skb->dev);
1791a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1792e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
1793e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
1794e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
1795e8599ff4SDavid S. Miller 	struct neighbour *neigh;
179671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	struct rd_msg *msg;
17976e157b6aSDavid S. Miller 	int optlen, on_link;
17986e157b6aSDavid S. Miller 	u8 *lladdr;
1799e8599ff4SDavid S. Miller 
180029a3cad5SSimon Horman 	optlen = skb_tail_pointer(skb) - skb_transport_header(skb);
180171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	optlen -= sizeof(*msg);
1802e8599ff4SDavid S. Miller 
1803e8599ff4SDavid S. Miller 	if (optlen < 0) {
18046e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1805e8599ff4SDavid S. Miller 		return;
1806e8599ff4SDavid S. Miller 	}
1807e8599ff4SDavid S. Miller 
180871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	msg = (struct rd_msg *)icmp6_hdr(skb);
1809e8599ff4SDavid S. Miller 
181071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_is_multicast(&msg->dest)) {
18116e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1812e8599ff4SDavid S. Miller 		return;
1813e8599ff4SDavid S. Miller 	}
1814e8599ff4SDavid S. Miller 
18156e157b6aSDavid S. Miller 	on_link = 0;
181671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (ipv6_addr_equal(&msg->dest, &msg->target)) {
1817e8599ff4SDavid S. Miller 		on_link = 1;
181871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	} else if (ipv6_addr_type(&msg->target) !=
1819e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
18206e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1821e8599ff4SDavid S. Miller 		return;
1822e8599ff4SDavid S. Miller 	}
1823e8599ff4SDavid S. Miller 
1824e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
1825e8599ff4SDavid S. Miller 	if (!in6_dev)
1826e8599ff4SDavid S. Miller 		return;
1827e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1828e8599ff4SDavid S. Miller 		return;
1829e8599ff4SDavid S. Miller 
1830e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
1831e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
1832e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
1833e8599ff4SDavid S. Miller 	 */
1834e8599ff4SDavid S. Miller 
183571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) {
1836e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1837e8599ff4SDavid S. Miller 		return;
1838e8599ff4SDavid S. Miller 	}
18396e157b6aSDavid S. Miller 
18406e157b6aSDavid S. Miller 	lladdr = NULL;
1841e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
1842e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1843e8599ff4SDavid S. Miller 					     skb->dev);
1844e8599ff4SDavid S. Miller 		if (!lladdr) {
1845e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1846e8599ff4SDavid S. Miller 			return;
1847e8599ff4SDavid S. Miller 		}
1848e8599ff4SDavid S. Miller 	}
1849e8599ff4SDavid S. Miller 
18506e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
18516e157b6aSDavid S. Miller 	if (rt == net->ipv6.ip6_null_entry) {
18526e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
18536e157b6aSDavid S. Miller 		return;
18546e157b6aSDavid S. Miller 	}
18556e157b6aSDavid S. Miller 
18566e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
18576e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
18586e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
18596e157b6aSDavid S. Miller 	 */
18606e157b6aSDavid S. Miller 	dst_confirm(&rt->dst);
18616e157b6aSDavid S. Miller 
186271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1);
1863e8599ff4SDavid S. Miller 	if (!neigh)
1864e8599ff4SDavid S. Miller 		return;
1865e8599ff4SDavid S. Miller 
18661da177e4SLinus Torvalds 	/*
18671da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
18681da177e4SLinus Torvalds 	 */
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
18711da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
18721da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
18731da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
18741da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
18751da177e4SLinus Torvalds 		     );
18761da177e4SLinus Torvalds 
187771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	nrt = ip6_rt_copy(rt, &msg->dest);
187838308473SDavid S. Miller 	if (!nrt)
18791da177e4SLinus Torvalds 		goto out;
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
18821da177e4SLinus Torvalds 	if (on_link)
18831da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
18841da177e4SLinus Torvalds 
18854e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
18861da177e4SLinus Torvalds 
188740e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
18881da177e4SLinus Torvalds 		goto out;
18891da177e4SLinus Torvalds 
1890d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1891d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
189271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 	netevent.daddr = &msg->dest;
189360592833SYOSHIFUJI Hideaki / 吉藤英明 	netevent.neigh = neigh;
18948d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
18958d71740cSTom Tucker 
18961da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
18976e157b6aSDavid S. Miller 		rt = (struct rt6_info *) dst_clone(&rt->dst);
1898e0a1ad73SThomas Graf 		ip6_del_rt(rt);
18991da177e4SLinus Torvalds 	}
19001da177e4SLinus Torvalds 
19011da177e4SLinus Torvalds out:
1902e8599ff4SDavid S. Miller 	neigh_release(neigh);
19036e157b6aSDavid S. Miller }
19046e157b6aSDavid S. Miller 
19051da177e4SLinus Torvalds /*
19061da177e4SLinus Torvalds  *	Misc support functions
19071da177e4SLinus Torvalds  */
19081da177e4SLinus Torvalds 
19094b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from)
19104b32b5adSMartin KaFai Lau {
19114b32b5adSMartin KaFai Lau 	BUG_ON(from->dst.from);
19124b32b5adSMartin KaFai Lau 
19134b32b5adSMartin KaFai Lau 	rt->rt6i_flags &= ~RTF_EXPIRES;
19144b32b5adSMartin KaFai Lau 	dst_hold(&from->dst);
19154b32b5adSMartin KaFai Lau 	rt->dst.from = &from->dst;
19164b32b5adSMartin KaFai Lau 	dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true);
19174b32b5adSMartin KaFai Lau }
19184b32b5adSMartin KaFai Lau 
19191716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
192021efcfa0SEric Dumazet 				    const struct in6_addr *dest)
19211da177e4SLinus Torvalds {
1922d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
19234b32b5adSMartin KaFai Lau 	struct rt6_info *rt;
19244b32b5adSMartin KaFai Lau 
19254b32b5adSMartin KaFai Lau 	if (ort->rt6i_flags & RTF_CACHE)
19264b32b5adSMartin KaFai Lau 		ort = (struct rt6_info *)ort->dst.from;
19274b32b5adSMartin KaFai Lau 
19284b32b5adSMartin KaFai Lau 	rt = ip6_dst_alloc(net, ort->dst.dev, 0,
19298b96d22dSDavid S. Miller 			   ort->rt6i_table);
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	if (rt) {
1932d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1933d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
19348e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
19351da177e4SLinus Torvalds 
19364e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
19378e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1938d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
19391da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
19401da177e4SLinus Torvalds 		if (rt->rt6i_idev)
19411da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1942d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
19434e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
19441716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
19451716a961SGao feng 		rt6_set_from(rt, ort);
19461da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
19491da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
19501da177e4SLinus Torvalds #endif
19510f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1952c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
19531da177e4SLinus Torvalds 	}
19541da177e4SLinus Torvalds 	return rt;
19551da177e4SLinus Torvalds }
19561da177e4SLinus Torvalds 
195770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1958efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1959b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1960b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
196170ceb4f5SYOSHIFUJI Hideaki {
196270ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
196370ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1964c71099acSThomas Graf 	struct fib6_table *table;
196570ceb4f5SYOSHIFUJI Hideaki 
1966efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
196738308473SDavid S. Miller 	if (!table)
1968c71099acSThomas Graf 		return NULL;
1969c71099acSThomas Graf 
19705744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1971c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0);
197270ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
197370ceb4f5SYOSHIFUJI Hideaki 		goto out;
197470ceb4f5SYOSHIFUJI Hideaki 
1975d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1976d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
197770ceb4f5SYOSHIFUJI Hideaki 			continue;
197870ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
197970ceb4f5SYOSHIFUJI Hideaki 			continue;
198070ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
198170ceb4f5SYOSHIFUJI Hideaki 			continue;
1982d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
198370ceb4f5SYOSHIFUJI Hideaki 		break;
198470ceb4f5SYOSHIFUJI Hideaki 	}
198570ceb4f5SYOSHIFUJI Hideaki out:
19865744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
198770ceb4f5SYOSHIFUJI Hideaki 	return rt;
198870ceb4f5SYOSHIFUJI Hideaki }
198970ceb4f5SYOSHIFUJI Hideaki 
1990efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1991b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1992b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
199395c96174SEric Dumazet 					   unsigned int pref)
199470ceb4f5SYOSHIFUJI Hideaki {
199586872cb5SThomas Graf 	struct fib6_config cfg = {
199686872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1997238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
199886872cb5SThomas Graf 		.fc_ifindex	= ifindex,
199986872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
200086872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
200186872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
200215e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
2003efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
2004efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
200586872cb5SThomas Graf 	};
200670ceb4f5SYOSHIFUJI Hideaki 
20074e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
20084e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
200986872cb5SThomas Graf 
2010e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
2011e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
201286872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
201370ceb4f5SYOSHIFUJI Hideaki 
201486872cb5SThomas Graf 	ip6_route_add(&cfg);
201570ceb4f5SYOSHIFUJI Hideaki 
2016efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
201770ceb4f5SYOSHIFUJI Hideaki }
201870ceb4f5SYOSHIFUJI Hideaki #endif
201970ceb4f5SYOSHIFUJI Hideaki 
2020b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
20211da177e4SLinus Torvalds {
20221da177e4SLinus Torvalds 	struct rt6_info *rt;
2023c71099acSThomas Graf 	struct fib6_table *table;
20241da177e4SLinus Torvalds 
2025c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
202638308473SDavid S. Miller 	if (!table)
2027c71099acSThomas Graf 		return NULL;
20281da177e4SLinus Torvalds 
20295744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
2030d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
2031d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
2032045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
20331da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
20341da177e4SLinus Torvalds 			break;
20351da177e4SLinus Torvalds 	}
20361da177e4SLinus Torvalds 	if (rt)
2037d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
20385744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
20391da177e4SLinus Torvalds 	return rt;
20401da177e4SLinus Torvalds }
20411da177e4SLinus Torvalds 
2042b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
2043ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
2044ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
20451da177e4SLinus Torvalds {
204686872cb5SThomas Graf 	struct fib6_config cfg = {
204786872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
2048238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
204986872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
205086872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
205186872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
205215e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
20535578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
2054c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
205586872cb5SThomas Graf 	};
20561da177e4SLinus Torvalds 
20574e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
20581da177e4SLinus Torvalds 
205986872cb5SThomas Graf 	ip6_route_add(&cfg);
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
20621da177e4SLinus Torvalds }
20631da177e4SLinus Torvalds 
20647b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
20651da177e4SLinus Torvalds {
20661da177e4SLinus Torvalds 	struct rt6_info *rt;
2067c71099acSThomas Graf 	struct fib6_table *table;
2068c71099acSThomas Graf 
2069c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
20707b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
207138308473SDavid S. Miller 	if (!table)
2072c71099acSThomas Graf 		return;
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds restart:
2075c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
2076d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
20773e8b0ac3SLorenzo Colitti 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
20783e8b0ac3SLorenzo Colitti 		    (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
2079d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
2080c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
2081e0a1ad73SThomas Graf 			ip6_del_rt(rt);
20821da177e4SLinus Torvalds 			goto restart;
20831da177e4SLinus Torvalds 		}
20841da177e4SLinus Torvalds 	}
2085c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
20861da177e4SLinus Torvalds }
20871da177e4SLinus Torvalds 
20885578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
20895578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
209086872cb5SThomas Graf 				 struct fib6_config *cfg)
209186872cb5SThomas Graf {
209286872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
209386872cb5SThomas Graf 
209486872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
209586872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
209686872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
209786872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
209886872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
209986872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
210086872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
210186872cb5SThomas Graf 
21025578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
2103f1243c2dSBenjamin Thery 
21044e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
21054e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
21064e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
210786872cb5SThomas Graf }
210886872cb5SThomas Graf 
21095578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
21101da177e4SLinus Torvalds {
211186872cb5SThomas Graf 	struct fib6_config cfg;
21121da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
21131da177e4SLinus Torvalds 	int err;
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds 	switch (cmd) {
21161da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
21171da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
2118af31f412SEric W. Biederman 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
21191da177e4SLinus Torvalds 			return -EPERM;
21201da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
21211da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
21221da177e4SLinus Torvalds 		if (err)
21231da177e4SLinus Torvalds 			return -EFAULT;
21241da177e4SLinus Torvalds 
21255578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
212686872cb5SThomas Graf 
21271da177e4SLinus Torvalds 		rtnl_lock();
21281da177e4SLinus Torvalds 		switch (cmd) {
21291da177e4SLinus Torvalds 		case SIOCADDRT:
213086872cb5SThomas Graf 			err = ip6_route_add(&cfg);
21311da177e4SLinus Torvalds 			break;
21321da177e4SLinus Torvalds 		case SIOCDELRT:
213386872cb5SThomas Graf 			err = ip6_route_del(&cfg);
21341da177e4SLinus Torvalds 			break;
21351da177e4SLinus Torvalds 		default:
21361da177e4SLinus Torvalds 			err = -EINVAL;
21371da177e4SLinus Torvalds 		}
21381da177e4SLinus Torvalds 		rtnl_unlock();
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 		return err;
21413ff50b79SStephen Hemminger 	}
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	return -EINVAL;
21441da177e4SLinus Torvalds }
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds /*
21471da177e4SLinus Torvalds  *	Drop the packet on the floor
21481da177e4SLinus Torvalds  */
21491da177e4SLinus Torvalds 
2150d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
21511da177e4SLinus Torvalds {
2152612f09e8SYOSHIFUJI Hideaki 	int type;
2153adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
2154612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2155612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
21560660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
215745bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
21583bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
21593bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2160612f09e8SYOSHIFUJI Hideaki 			break;
2161612f09e8SYOSHIFUJI Hideaki 		}
2162612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2163612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
21643bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
21653bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2166612f09e8SYOSHIFUJI Hideaki 		break;
2167612f09e8SYOSHIFUJI Hideaki 	}
21683ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
21691da177e4SLinus Torvalds 	kfree_skb(skb);
21701da177e4SLinus Torvalds 	return 0;
21711da177e4SLinus Torvalds }
21721da177e4SLinus Torvalds 
21739ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
21749ce8ade0SThomas Graf {
2175612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
21769ce8ade0SThomas Graf }
21779ce8ade0SThomas Graf 
2178aad88724SEric Dumazet static int ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb)
21791da177e4SLinus Torvalds {
2180adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2181612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
21821da177e4SLinus Torvalds }
21831da177e4SLinus Torvalds 
21849ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
21859ce8ade0SThomas Graf {
2186612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
21879ce8ade0SThomas Graf }
21889ce8ade0SThomas Graf 
2189aad88724SEric Dumazet static int ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb)
21909ce8ade0SThomas Graf {
2191adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2192612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
21939ce8ade0SThomas Graf }
21949ce8ade0SThomas Graf 
21951da177e4SLinus Torvalds /*
21961da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
21971da177e4SLinus Torvalds  */
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
22001da177e4SLinus Torvalds 				    const struct in6_addr *addr,
22018f031519SDavid S. Miller 				    bool anycast)
22021da177e4SLinus Torvalds {
2203c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
2204a3300ef4SHannes Frederic Sowa 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev,
2205a3300ef4SHannes Frederic Sowa 					    DST_NOCOUNT, NULL);
2206a3300ef4SHannes Frederic Sowa 	if (!rt)
22071da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 	in6_dev_hold(idev);
22101da177e4SLinus Torvalds 
221111d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2212d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2213d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
22141da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
221758c4fb86SYOSHIFUJI Hideaki 	if (anycast)
221858c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
221958c4fb86SYOSHIFUJI Hideaki 	else
22201da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
22211da177e4SLinus Torvalds 
2222550bab42SJulian Anastasov 	rt->rt6i_gateway  = *addr;
22234e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
22241da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
22255578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
22261da177e4SLinus Torvalds 
2227d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
22281da177e4SLinus Torvalds 
22291da177e4SLinus Torvalds 	return rt;
22301da177e4SLinus Torvalds }
22311da177e4SLinus Torvalds 
2232c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2233c3968a85SDaniel Walter 			struct rt6_info *rt,
2234b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2235c3968a85SDaniel Walter 			unsigned int prefs,
2236c3968a85SDaniel Walter 			struct in6_addr *saddr)
2237c3968a85SDaniel Walter {
2238e16e888bSMarkus Stenberg 	struct inet6_dev *idev =
2239e16e888bSMarkus Stenberg 		rt ? ip6_dst_idev((struct dst_entry *)rt) : NULL;
2240c3968a85SDaniel Walter 	int err = 0;
2241e16e888bSMarkus Stenberg 	if (rt && rt->rt6i_prefsrc.plen)
22424e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2243c3968a85SDaniel Walter 	else
2244c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2245c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2246c3968a85SDaniel Walter 	return err;
2247c3968a85SDaniel Walter }
2248c3968a85SDaniel Walter 
2249c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2250c3968a85SDaniel Walter struct arg_dev_net_ip {
2251c3968a85SDaniel Walter 	struct net_device *dev;
2252c3968a85SDaniel Walter 	struct net *net;
2253c3968a85SDaniel Walter 	struct in6_addr *addr;
2254c3968a85SDaniel Walter };
2255c3968a85SDaniel Walter 
2256c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2257c3968a85SDaniel Walter {
2258c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2259c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2260c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2261c3968a85SDaniel Walter 
2262d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2263c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2264c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2265c3968a85SDaniel Walter 		/* remove prefsrc entry */
2266c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2267c3968a85SDaniel Walter 	}
2268c3968a85SDaniel Walter 	return 0;
2269c3968a85SDaniel Walter }
2270c3968a85SDaniel Walter 
2271c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2272c3968a85SDaniel Walter {
2273c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2274c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2275c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2276c3968a85SDaniel Walter 		.net = net,
2277c3968a85SDaniel Walter 		.addr = &ifp->addr,
2278c3968a85SDaniel Walter 	};
22790c3584d5SLi RongQing 	fib6_clean_all(net, fib6_remove_prefsrc, &adni);
2280c3968a85SDaniel Walter }
2281c3968a85SDaniel Walter 
2282be7a010dSDuan Jiong #define RTF_RA_ROUTER		(RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY)
2283be7a010dSDuan Jiong #define RTF_CACHE_GATEWAY	(RTF_GATEWAY | RTF_CACHE)
2284be7a010dSDuan Jiong 
2285be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */
2286be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg)
2287be7a010dSDuan Jiong {
2288be7a010dSDuan Jiong 	struct in6_addr *gateway = (struct in6_addr *)arg;
2289be7a010dSDuan Jiong 
2290be7a010dSDuan Jiong 	if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) ||
2291be7a010dSDuan Jiong 	     ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) &&
2292be7a010dSDuan Jiong 	     ipv6_addr_equal(gateway, &rt->rt6i_gateway)) {
2293be7a010dSDuan Jiong 		return -1;
2294be7a010dSDuan Jiong 	}
2295be7a010dSDuan Jiong 	return 0;
2296be7a010dSDuan Jiong }
2297be7a010dSDuan Jiong 
2298be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway)
2299be7a010dSDuan Jiong {
2300be7a010dSDuan Jiong 	fib6_clean_all(net, fib6_clean_tohost, gateway);
2301be7a010dSDuan Jiong }
2302be7a010dSDuan Jiong 
23038ed67789SDaniel Lezcano struct arg_dev_net {
23048ed67789SDaniel Lezcano 	struct net_device *dev;
23058ed67789SDaniel Lezcano 	struct net *net;
23068ed67789SDaniel Lezcano };
23078ed67789SDaniel Lezcano 
23081da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
23091da177e4SLinus Torvalds {
2310bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2311bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
23128ed67789SDaniel Lezcano 
2313d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2314c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
23151da177e4SLinus Torvalds 		return -1;
2316c159d30cSDavid S. Miller 
23171da177e4SLinus Torvalds 	return 0;
23181da177e4SLinus Torvalds }
23191da177e4SLinus Torvalds 
2320f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
23211da177e4SLinus Torvalds {
23228ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
23238ed67789SDaniel Lezcano 		.dev = dev,
23248ed67789SDaniel Lezcano 		.net = net,
23258ed67789SDaniel Lezcano 	};
23268ed67789SDaniel Lezcano 
23270c3584d5SLi RongQing 	fib6_clean_all(net, fib6_ifdown, &adn);
23281e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
23291da177e4SLinus Torvalds }
23301da177e4SLinus Torvalds 
233195c96174SEric Dumazet struct rt6_mtu_change_arg {
23321da177e4SLinus Torvalds 	struct net_device *dev;
233395c96174SEric Dumazet 	unsigned int mtu;
23341da177e4SLinus Torvalds };
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
23371da177e4SLinus Torvalds {
23381da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
23391da177e4SLinus Torvalds 	struct inet6_dev *idev;
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
23421da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
23431da177e4SLinus Torvalds 	   We still use this lock to block changes
23441da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
23451da177e4SLinus Torvalds 	*/
23461da177e4SLinus Torvalds 
23471da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
234838308473SDavid S. Miller 	if (!idev)
23491da177e4SLinus Torvalds 		return 0;
23501da177e4SLinus Torvalds 
23511da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
23521da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
23531da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
23541da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
23551da177e4SLinus Torvalds 	 */
23561da177e4SLinus Torvalds 	/*
23571da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
23581da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
23591da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
23601da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
23611da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
23621da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
23631da177e4SLinus Torvalds 	   PMTU discouvery.
23641da177e4SLinus Torvalds 	 */
2365d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
23664b32b5adSMartin KaFai Lau 	    !dst_metric_locked(&rt->dst, RTAX_MTU)) {
23674b32b5adSMartin KaFai Lau 		if (rt->rt6i_flags & RTF_CACHE) {
23684b32b5adSMartin KaFai Lau 			/* For RTF_CACHE with rt6i_pmtu == 0
23694b32b5adSMartin KaFai Lau 			 * (i.e. a redirected route),
23704b32b5adSMartin KaFai Lau 			 * the metrics of its rt->dst.from has already
23714b32b5adSMartin KaFai Lau 			 * been updated.
23724b32b5adSMartin KaFai Lau 			 */
23734b32b5adSMartin KaFai Lau 			if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu)
23744b32b5adSMartin KaFai Lau 				rt->rt6i_pmtu = arg->mtu;
23754b32b5adSMartin KaFai Lau 		} else if (dst_mtu(&rt->dst) >= arg->mtu ||
2376d8d1f30bSChangli Gao 			   (dst_mtu(&rt->dst) < arg->mtu &&
23774b32b5adSMartin KaFai Lau 			    dst_mtu(&rt->dst) == idev->cnf.mtu6)) {
2378defb3519SDavid S. Miller 			dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2379566cfd8fSSimon Arlott 		}
23804b32b5adSMartin KaFai Lau 	}
23811da177e4SLinus Torvalds 	return 0;
23821da177e4SLinus Torvalds }
23831da177e4SLinus Torvalds 
238495c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
23851da177e4SLinus Torvalds {
2386c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2387c71099acSThomas Graf 		.dev = dev,
2388c71099acSThomas Graf 		.mtu = mtu,
2389c71099acSThomas Graf 	};
23901da177e4SLinus Torvalds 
23910c3584d5SLi RongQing 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg);
23921da177e4SLinus Torvalds }
23931da177e4SLinus Torvalds 
2394ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
23955176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
239686872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2397ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
239886872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
239986872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
240051ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
2401c78ba6d6SLubomir Rintel 	[RTA_PREF]              = { .type = NLA_U8 },
240286872cb5SThomas Graf };
240386872cb5SThomas Graf 
240486872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
240586872cb5SThomas Graf 			      struct fib6_config *cfg)
24061da177e4SLinus Torvalds {
240786872cb5SThomas Graf 	struct rtmsg *rtm;
240886872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
2409c78ba6d6SLubomir Rintel 	unsigned int pref;
241086872cb5SThomas Graf 	int err;
24111da177e4SLinus Torvalds 
241286872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
241386872cb5SThomas Graf 	if (err < 0)
241486872cb5SThomas Graf 		goto errout;
24151da177e4SLinus Torvalds 
241686872cb5SThomas Graf 	err = -EINVAL;
241786872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
241886872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
241986872cb5SThomas Graf 
242086872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
242186872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
242286872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
242386872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
242486872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
2425ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
242686872cb5SThomas Graf 
2427ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
2428ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
2429b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
2430b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
243186872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
243286872cb5SThomas Graf 
2433ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2434ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2435ab79ad14SMaciej Żenczykowski 
24361f56a01fSMartin KaFai Lau 	if (rtm->rtm_flags & RTM_F_CLONED)
24371f56a01fSMartin KaFai Lau 		cfg->fc_flags |= RTF_CACHE;
24381f56a01fSMartin KaFai Lau 
243915e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
244086872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
24413b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
244286872cb5SThomas Graf 
244386872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
244467b61f6cSJiri Benc 		cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
244586872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
24461da177e4SLinus Torvalds 	}
244786872cb5SThomas Graf 
244886872cb5SThomas Graf 	if (tb[RTA_DST]) {
244986872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
245086872cb5SThomas Graf 
245186872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
245286872cb5SThomas Graf 			goto errout;
245386872cb5SThomas Graf 
245486872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
24551da177e4SLinus Torvalds 	}
245686872cb5SThomas Graf 
245786872cb5SThomas Graf 	if (tb[RTA_SRC]) {
245886872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
245986872cb5SThomas Graf 
246086872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
246186872cb5SThomas Graf 			goto errout;
246286872cb5SThomas Graf 
246386872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
24641da177e4SLinus Torvalds 	}
246586872cb5SThomas Graf 
2466c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
246767b61f6cSJiri Benc 		cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
2468c3968a85SDaniel Walter 
246986872cb5SThomas Graf 	if (tb[RTA_OIF])
247086872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
247186872cb5SThomas Graf 
247286872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
247386872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
247486872cb5SThomas Graf 
247586872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
247686872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
247786872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
24781da177e4SLinus Torvalds 	}
247986872cb5SThomas Graf 
248086872cb5SThomas Graf 	if (tb[RTA_TABLE])
248186872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
248286872cb5SThomas Graf 
248351ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
248451ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
248551ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
248651ebd318SNicolas Dichtel 	}
248751ebd318SNicolas Dichtel 
2488c78ba6d6SLubomir Rintel 	if (tb[RTA_PREF]) {
2489c78ba6d6SLubomir Rintel 		pref = nla_get_u8(tb[RTA_PREF]);
2490c78ba6d6SLubomir Rintel 		if (pref != ICMPV6_ROUTER_PREF_LOW &&
2491c78ba6d6SLubomir Rintel 		    pref != ICMPV6_ROUTER_PREF_HIGH)
2492c78ba6d6SLubomir Rintel 			pref = ICMPV6_ROUTER_PREF_MEDIUM;
2493c78ba6d6SLubomir Rintel 		cfg->fc_flags |= RTF_PREF(pref);
2494c78ba6d6SLubomir Rintel 	}
2495c78ba6d6SLubomir Rintel 
249686872cb5SThomas Graf 	err = 0;
249786872cb5SThomas Graf errout:
249886872cb5SThomas Graf 	return err;
24991da177e4SLinus Torvalds }
25001da177e4SLinus Torvalds 
250151ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add)
250251ebd318SNicolas Dichtel {
250351ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
250451ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
250551ebd318SNicolas Dichtel 	int remaining;
250651ebd318SNicolas Dichtel 	int attrlen;
250751ebd318SNicolas Dichtel 	int err = 0, last_err = 0;
250851ebd318SNicolas Dichtel 
250935f1b4e9SMichal Kubeček 	remaining = cfg->fc_mp_len;
251051ebd318SNicolas Dichtel beginning:
251151ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
251251ebd318SNicolas Dichtel 
251351ebd318SNicolas Dichtel 	/* Parse a Multipath Entry */
251451ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
251551ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
251651ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
251751ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
251851ebd318SNicolas Dichtel 
251951ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
252051ebd318SNicolas Dichtel 		if (attrlen > 0) {
252151ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
252251ebd318SNicolas Dichtel 
252351ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
252451ebd318SNicolas Dichtel 			if (nla) {
252567b61f6cSJiri Benc 				r_cfg.fc_gateway = nla_get_in6_addr(nla);
252651ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
252751ebd318SNicolas Dichtel 			}
252851ebd318SNicolas Dichtel 		}
252951ebd318SNicolas Dichtel 		err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
253051ebd318SNicolas Dichtel 		if (err) {
253151ebd318SNicolas Dichtel 			last_err = err;
253251ebd318SNicolas Dichtel 			/* If we are trying to remove a route, do not stop the
253351ebd318SNicolas Dichtel 			 * loop when ip6_route_del() fails (because next hop is
253451ebd318SNicolas Dichtel 			 * already gone), we should try to remove all next hops.
253551ebd318SNicolas Dichtel 			 */
253651ebd318SNicolas Dichtel 			if (add) {
253751ebd318SNicolas Dichtel 				/* If add fails, we should try to delete all
253851ebd318SNicolas Dichtel 				 * next hops that have been already added.
253951ebd318SNicolas Dichtel 				 */
254051ebd318SNicolas Dichtel 				add = 0;
254135f1b4e9SMichal Kubeček 				remaining = cfg->fc_mp_len - remaining;
254251ebd318SNicolas Dichtel 				goto beginning;
254351ebd318SNicolas Dichtel 			}
254451ebd318SNicolas Dichtel 		}
25451a72418bSNicolas Dichtel 		/* Because each route is added like a single route we remove
254627596472SMichal Kubeček 		 * these flags after the first nexthop: if there is a collision,
254727596472SMichal Kubeček 		 * we have already failed to add the first nexthop:
254827596472SMichal Kubeček 		 * fib6_add_rt2node() has rejected it; when replacing, old
254927596472SMichal Kubeček 		 * nexthops have been replaced by first new, the rest should
255027596472SMichal Kubeček 		 * be added to it.
25511a72418bSNicolas Dichtel 		 */
255227596472SMichal Kubeček 		cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL |
255327596472SMichal Kubeček 						     NLM_F_REPLACE);
255451ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
255551ebd318SNicolas Dichtel 	}
255651ebd318SNicolas Dichtel 
255751ebd318SNicolas Dichtel 	return last_err;
255851ebd318SNicolas Dichtel }
255951ebd318SNicolas Dichtel 
2560661d2967SThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
25611da177e4SLinus Torvalds {
256286872cb5SThomas Graf 	struct fib6_config cfg;
256386872cb5SThomas Graf 	int err;
25641da177e4SLinus Torvalds 
256586872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
256686872cb5SThomas Graf 	if (err < 0)
256786872cb5SThomas Graf 		return err;
256886872cb5SThomas Graf 
256951ebd318SNicolas Dichtel 	if (cfg.fc_mp)
257051ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 0);
257151ebd318SNicolas Dichtel 	else
257286872cb5SThomas Graf 		return ip6_route_del(&cfg);
25731da177e4SLinus Torvalds }
25741da177e4SLinus Torvalds 
2575661d2967SThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
25761da177e4SLinus Torvalds {
257786872cb5SThomas Graf 	struct fib6_config cfg;
257886872cb5SThomas Graf 	int err;
25791da177e4SLinus Torvalds 
258086872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
258186872cb5SThomas Graf 	if (err < 0)
258286872cb5SThomas Graf 		return err;
258386872cb5SThomas Graf 
258451ebd318SNicolas Dichtel 	if (cfg.fc_mp)
258551ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 1);
258651ebd318SNicolas Dichtel 	else
258786872cb5SThomas Graf 		return ip6_route_add(&cfg);
25881da177e4SLinus Torvalds }
25891da177e4SLinus Torvalds 
2590339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2591339bf98fSThomas Graf {
2592339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2593339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2594339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2595339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2596339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2597339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2598339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2599339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2600339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
26016a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2602ea697639SDaniel Borkmann 	       + nla_total_size(sizeof(struct rta_cacheinfo))
2603c78ba6d6SLubomir Rintel 	       + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
2604c78ba6d6SLubomir Rintel 	       + nla_total_size(1); /* RTA_PREF */
2605339bf98fSThomas Graf }
2606339bf98fSThomas Graf 
2607191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2608191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
26090d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
261015e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
26117bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
26121da177e4SLinus Torvalds {
26134b32b5adSMartin KaFai Lau 	u32 metrics[RTAX_MAX];
26141da177e4SLinus Torvalds 	struct rtmsg *rtm;
26151da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2616e3703b3dSThomas Graf 	long expires;
26179e762a4aSPatrick McHardy 	u32 table;
26181da177e4SLinus Torvalds 
26191da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
26201da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
26211da177e4SLinus Torvalds 			/* success since this is not a prefix route */
26221da177e4SLinus Torvalds 			return 1;
26231da177e4SLinus Torvalds 		}
26241da177e4SLinus Torvalds 	}
26251da177e4SLinus Torvalds 
262615e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
262738308473SDavid S. Miller 	if (!nlh)
262826932566SPatrick McHardy 		return -EMSGSIZE;
26292d7202bfSThomas Graf 
26302d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
26311da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
26321da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
26331da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
26341da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2635c71099acSThomas Graf 	if (rt->rt6i_table)
26369e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2637c71099acSThomas Graf 	else
26389e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
26399e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2640c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2641c78679e8SDavid S. Miller 		goto nla_put_failure;
2642ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
2643ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
2644ef2c7d7bSNicolas Dichtel 		case -EINVAL:
2645ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
2646ef2c7d7bSNicolas Dichtel 			break;
2647ef2c7d7bSNicolas Dichtel 		case -EACCES:
2648ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
2649ef2c7d7bSNicolas Dichtel 			break;
2650b4949ab2SNicolas Dichtel 		case -EAGAIN:
2651b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
2652b4949ab2SNicolas Dichtel 			break;
2653ef2c7d7bSNicolas Dichtel 		default:
26541da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
2655ef2c7d7bSNicolas Dichtel 			break;
2656ef2c7d7bSNicolas Dichtel 		}
2657ef2c7d7bSNicolas Dichtel 	}
2658ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2659ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2660d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
26611da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
26621da177e4SLinus Torvalds 	else
26631da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
26641da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
26651da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
26661da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
26671da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
26681da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
2669f0396f60SDenis Ovsienko 	else if (rt->rt6i_flags & RTF_ADDRCONF) {
2670f0396f60SDenis Ovsienko 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
26711da177e4SLinus Torvalds 			rtm->rtm_protocol = RTPROT_RA;
2672f0396f60SDenis Ovsienko 		else
2673f0396f60SDenis Ovsienko 			rtm->rtm_protocol = RTPROT_KERNEL;
2674f0396f60SDenis Ovsienko 	}
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
26771da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
26781da177e4SLinus Torvalds 
26791da177e4SLinus Torvalds 	if (dst) {
2680930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_DST, dst))
2681c78679e8SDavid S. Miller 			goto nla_put_failure;
26821da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
26831da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2684930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
2685c78679e8SDavid S. Miller 			goto nla_put_failure;
26861da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
26871da177e4SLinus Torvalds 	if (src) {
2688930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_SRC, src))
2689c78679e8SDavid S. Miller 			goto nla_put_failure;
26901da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2691c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2692930345eaSJiri Benc 		   nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
2693c78679e8SDavid S. Miller 		goto nla_put_failure;
26941da177e4SLinus Torvalds #endif
26957bc570c8SYOSHIFUJI Hideaki 	if (iif) {
26967bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
26977bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
26988229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
26997bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
27007bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
27017bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
27027bc570c8SYOSHIFUJI Hideaki 						return 0;
27037bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
27047bc570c8SYOSHIFUJI Hideaki 				} else {
27057bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
27067bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
27077bc570c8SYOSHIFUJI Hideaki 				}
27087bc570c8SYOSHIFUJI Hideaki 			}
27097bc570c8SYOSHIFUJI Hideaki 		} else
27107bc570c8SYOSHIFUJI Hideaki #endif
2711c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2712c78679e8SDavid S. Miller 				goto nla_put_failure;
27137bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
27141da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2715c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2716930345eaSJiri Benc 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
2717c78679e8SDavid S. Miller 			goto nla_put_failure;
2718c3968a85SDaniel Walter 	}
2719c3968a85SDaniel Walter 
2720c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2721c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
27224e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2723930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
2724c78679e8SDavid S. Miller 			goto nla_put_failure;
27251da177e4SLinus Torvalds 	}
27262d7202bfSThomas Graf 
27274b32b5adSMartin KaFai Lau 	memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics));
27284b32b5adSMartin KaFai Lau 	if (rt->rt6i_pmtu)
27294b32b5adSMartin KaFai Lau 		metrics[RTAX_MTU - 1] = rt->rt6i_pmtu;
27304b32b5adSMartin KaFai Lau 	if (rtnetlink_put_metrics(skb, metrics) < 0)
27312d7202bfSThomas Graf 		goto nla_put_failure;
27322d7202bfSThomas Graf 
2733dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 	if (rt->rt6i_flags & RTF_GATEWAY) {
2734930345eaSJiri Benc 		if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
273594f826b8SEric Dumazet 			goto nla_put_failure;
273694f826b8SEric Dumazet 	}
27372d7202bfSThomas Graf 
2738c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2739c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2740c78679e8SDavid S. Miller 		goto nla_put_failure;
2741c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2742c78679e8SDavid S. Miller 		goto nla_put_failure;
27438253947eSLi Wei 
27448253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
274569cdf8f9SYOSHIFUJI Hideaki 
274687a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2747e3703b3dSThomas Graf 		goto nla_put_failure;
27481da177e4SLinus Torvalds 
2749c78ba6d6SLubomir Rintel 	if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
2750c78ba6d6SLubomir Rintel 		goto nla_put_failure;
2751c78ba6d6SLubomir Rintel 
2752053c095aSJohannes Berg 	nlmsg_end(skb, nlh);
2753053c095aSJohannes Berg 	return 0;
27542d7202bfSThomas Graf 
27552d7202bfSThomas Graf nla_put_failure:
275626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
275726932566SPatrick McHardy 	return -EMSGSIZE;
27581da177e4SLinus Torvalds }
27591da177e4SLinus Torvalds 
27601b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
27611da177e4SLinus Torvalds {
27621da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
27631da177e4SLinus Torvalds 	int prefix;
27641da177e4SLinus Torvalds 
27652d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
27662d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
27671da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
27681da177e4SLinus Torvalds 	} else
27691da177e4SLinus Torvalds 		prefix = 0;
27701da177e4SLinus Torvalds 
2771191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2772191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
277315e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
27747bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
27751da177e4SLinus Torvalds }
27761da177e4SLinus Torvalds 
2777661d2967SThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
27781da177e4SLinus Torvalds {
27793b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2780ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
27811da177e4SLinus Torvalds 	struct rt6_info *rt;
2782ab364a6fSThomas Graf 	struct sk_buff *skb;
2783ab364a6fSThomas Graf 	struct rtmsg *rtm;
27844c9483b2SDavid S. Miller 	struct flowi6 fl6;
278572331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2786ab364a6fSThomas Graf 
2787ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2788ab364a6fSThomas Graf 	if (err < 0)
2789ab364a6fSThomas Graf 		goto errout;
2790ab364a6fSThomas Graf 
2791ab364a6fSThomas Graf 	err = -EINVAL;
27924c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2793ab364a6fSThomas Graf 
2794ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2795ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2796ab364a6fSThomas Graf 			goto errout;
2797ab364a6fSThomas Graf 
27984e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2799ab364a6fSThomas Graf 	}
2800ab364a6fSThomas Graf 
2801ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2802ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2803ab364a6fSThomas Graf 			goto errout;
2804ab364a6fSThomas Graf 
28054e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2806ab364a6fSThomas Graf 	}
2807ab364a6fSThomas Graf 
2808ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2809ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2810ab364a6fSThomas Graf 
2811ab364a6fSThomas Graf 	if (tb[RTA_OIF])
281272331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2813ab364a6fSThomas Graf 
28142e47b291SLorenzo Colitti 	if (tb[RTA_MARK])
28152e47b291SLorenzo Colitti 		fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]);
28162e47b291SLorenzo Colitti 
2817ab364a6fSThomas Graf 	if (iif) {
2818ab364a6fSThomas Graf 		struct net_device *dev;
281972331bc0SShmulik Ladkani 		int flags = 0;
282072331bc0SShmulik Ladkani 
28215578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2822ab364a6fSThomas Graf 		if (!dev) {
2823ab364a6fSThomas Graf 			err = -ENODEV;
2824ab364a6fSThomas Graf 			goto errout;
2825ab364a6fSThomas Graf 		}
282672331bc0SShmulik Ladkani 
282772331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
282872331bc0SShmulik Ladkani 
282972331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
283072331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
283172331bc0SShmulik Ladkani 
283272331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
283372331bc0SShmulik Ladkani 							       flags);
283472331bc0SShmulik Ladkani 	} else {
283572331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
283672331bc0SShmulik Ladkani 
283772331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2838ab364a6fSThomas Graf 	}
28391da177e4SLinus Torvalds 
28401da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
284138308473SDavid S. Miller 	if (!skb) {
284294e187c0SAmerigo Wang 		ip6_rt_put(rt);
2843ab364a6fSThomas Graf 		err = -ENOBUFS;
2844ab364a6fSThomas Graf 		goto errout;
2845ab364a6fSThomas Graf 	}
28461da177e4SLinus Torvalds 
28471da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
28481da177e4SLinus Torvalds 	   through good chunk of routing engine.
28491da177e4SLinus Torvalds 	 */
2850459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
28511da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
28521da177e4SLinus Torvalds 
2853d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
28541da177e4SLinus Torvalds 
28554c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
285615e47304SEric W. Biederman 			    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
28577bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
28581da177e4SLinus Torvalds 	if (err < 0) {
2859ab364a6fSThomas Graf 		kfree_skb(skb);
2860ab364a6fSThomas Graf 		goto errout;
28611da177e4SLinus Torvalds 	}
28621da177e4SLinus Torvalds 
286315e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2864ab364a6fSThomas Graf errout:
28651da177e4SLinus Torvalds 	return err;
28661da177e4SLinus Torvalds }
28671da177e4SLinus Torvalds 
286886872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
28691da177e4SLinus Torvalds {
28701da177e4SLinus Torvalds 	struct sk_buff *skb;
28715578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2872528c4cebSDenis V. Lunev 	u32 seq;
2873528c4cebSDenis V. Lunev 	int err;
28740d51aa80SJamal Hadi Salim 
2875528c4cebSDenis V. Lunev 	err = -ENOBUFS;
287638308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
287786872cb5SThomas Graf 
2878339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
287938308473SDavid S. Miller 	if (!skb)
288021713ebcSThomas Graf 		goto errout;
28811da177e4SLinus Torvalds 
2882191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
288315e47304SEric W. Biederman 				event, info->portid, seq, 0, 0, 0);
288426932566SPatrick McHardy 	if (err < 0) {
288526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
288626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
288726932566SPatrick McHardy 		kfree_skb(skb);
288826932566SPatrick McHardy 		goto errout;
288926932566SPatrick McHardy 	}
289015e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
28915578689aSDaniel Lezcano 		    info->nlh, gfp_any());
28921ce85fe4SPablo Neira Ayuso 	return;
289321713ebcSThomas Graf errout:
289421713ebcSThomas Graf 	if (err < 0)
28955578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
28961da177e4SLinus Torvalds }
28971da177e4SLinus Torvalds 
28988ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
2899351638e7SJiri Pirko 				unsigned long event, void *ptr)
29008ed67789SDaniel Lezcano {
2901351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2902c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
29038ed67789SDaniel Lezcano 
29048ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2905d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
29068ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
29078ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2908d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
29098ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2910d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
29118ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
29128ed67789SDaniel Lezcano #endif
29138ed67789SDaniel Lezcano 	}
29148ed67789SDaniel Lezcano 
29158ed67789SDaniel Lezcano 	return NOTIFY_OK;
29168ed67789SDaniel Lezcano }
29178ed67789SDaniel Lezcano 
29181da177e4SLinus Torvalds /*
29191da177e4SLinus Torvalds  *	/proc
29201da177e4SLinus Torvalds  */
29211da177e4SLinus Torvalds 
29221da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
29231da177e4SLinus Torvalds 
292433120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
292533120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
292633120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
292733120b30SAlexey Dobriyan 	.read		= seq_read,
292833120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
29298d2ca1d7SHannes Frederic Sowa 	.release	= seq_release_net,
293033120b30SAlexey Dobriyan };
293133120b30SAlexey Dobriyan 
29321da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
29331da177e4SLinus Torvalds {
293469ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
29351da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
293669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
293769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
293869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
293969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
294069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2941fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
294269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
29431da177e4SLinus Torvalds 
29441da177e4SLinus Torvalds 	return 0;
29451da177e4SLinus Torvalds }
29461da177e4SLinus Torvalds 
29471da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
29481da177e4SLinus Torvalds {
2949de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
295069ddb805SDaniel Lezcano }
295169ddb805SDaniel Lezcano 
29529a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
29531da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
29541da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
29551da177e4SLinus Torvalds 	.read	 = seq_read,
29561da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2957b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
29581da177e4SLinus Torvalds };
29591da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
29601da177e4SLinus Torvalds 
29611da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
29621da177e4SLinus Torvalds 
29631da177e4SLinus Torvalds static
2964fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write,
29651da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
29661da177e4SLinus Torvalds {
2967c486da34SLucian Adrian Grijincu 	struct net *net;
2968c486da34SLucian Adrian Grijincu 	int delay;
2969c486da34SLucian Adrian Grijincu 	if (!write)
2970c486da34SLucian Adrian Grijincu 		return -EINVAL;
2971c486da34SLucian Adrian Grijincu 
2972c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2973c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
29748d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
29752ac3ac8fSMichal Kubeček 	fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0);
29761da177e4SLinus Torvalds 	return 0;
29771da177e4SLinus Torvalds }
29781da177e4SLinus Torvalds 
2979fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = {
29801da177e4SLinus Torvalds 	{
29811da177e4SLinus Torvalds 		.procname	=	"flush",
29824990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
29831da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
298489c8b3a1SDave Jones 		.mode		=	0200,
29856d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
29861da177e4SLinus Torvalds 	},
29871da177e4SLinus Torvalds 	{
29881da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
29899a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
29901da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29911da177e4SLinus Torvalds 		.mode		=	0644,
29926d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
29931da177e4SLinus Torvalds 	},
29941da177e4SLinus Torvalds 	{
29951da177e4SLinus Torvalds 		.procname	=	"max_size",
29964990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
29971da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29981da177e4SLinus Torvalds 		.mode		=	0644,
29996d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
30001da177e4SLinus Torvalds 	},
30011da177e4SLinus Torvalds 	{
30021da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
30034990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
30041da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30051da177e4SLinus Torvalds 		.mode		=	0644,
30066d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
30071da177e4SLinus Torvalds 	},
30081da177e4SLinus Torvalds 	{
30091da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
30104990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
30111da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30121da177e4SLinus Torvalds 		.mode		=	0644,
30136d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
30141da177e4SLinus Torvalds 	},
30151da177e4SLinus Torvalds 	{
30161da177e4SLinus Torvalds 		.procname	=	"gc_interval",
30174990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
30181da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30191da177e4SLinus Torvalds 		.mode		=	0644,
30206d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
30211da177e4SLinus Torvalds 	},
30221da177e4SLinus Torvalds 	{
30231da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
30244990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
30251da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30261da177e4SLinus Torvalds 		.mode		=	0644,
3027f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
30281da177e4SLinus Torvalds 	},
30291da177e4SLinus Torvalds 	{
30301da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
30314990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
30321da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30331da177e4SLinus Torvalds 		.mode		=	0644,
30346d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
30351da177e4SLinus Torvalds 	},
30361da177e4SLinus Torvalds 	{
30371da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
30384990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
30391da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30401da177e4SLinus Torvalds 		.mode		=	0644,
3041f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
30421da177e4SLinus Torvalds 	},
30431da177e4SLinus Torvalds 	{
30441da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
30454990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
30461da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
30471da177e4SLinus Torvalds 		.mode		=	0644,
30486d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
30491da177e4SLinus Torvalds 	},
3050f8572d8fSEric W. Biederman 	{ }
30511da177e4SLinus Torvalds };
30521da177e4SLinus Torvalds 
30532c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
3054760f2d01SDaniel Lezcano {
3055760f2d01SDaniel Lezcano 	struct ctl_table *table;
3056760f2d01SDaniel Lezcano 
3057760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
3058760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
3059760f2d01SDaniel Lezcano 			GFP_KERNEL);
30605ee09105SYOSHIFUJI Hideaki 
30615ee09105SYOSHIFUJI Hideaki 	if (table) {
30625ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
3063c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
306486393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
30655ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
30665ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
30675ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
30685ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
30695ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
30705ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
30715ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
30729c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
3073464dc801SEric W. Biederman 
3074464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
3075464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
3076464dc801SEric W. Biederman 			table[0].procname = NULL;
30775ee09105SYOSHIFUJI Hideaki 	}
30785ee09105SYOSHIFUJI Hideaki 
3079760f2d01SDaniel Lezcano 	return table;
3080760f2d01SDaniel Lezcano }
30811da177e4SLinus Torvalds #endif
30821da177e4SLinus Torvalds 
30832c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
3084cdb18761SDaniel Lezcano {
3085633d424bSPavel Emelyanov 	int ret = -ENOMEM;
30868ed67789SDaniel Lezcano 
308786393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
308886393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
3089f2fc6a54SBenjamin Thery 
3090fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
3091fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
3092fc66f95cSEric Dumazet 
30938ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
30948ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
30958ed67789SDaniel Lezcano 					   GFP_KERNEL);
30968ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
3097fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
3098d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
30998ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
3100d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
310162fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
310262fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
31038ed67789SDaniel Lezcano 
31048ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
31058ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
31068ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
31078ed67789SDaniel Lezcano 					       GFP_KERNEL);
310868fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
310968fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
3110d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
31118ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
3112d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
311362fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
311462fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
31158ed67789SDaniel Lezcano 
31168ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
31178ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
31188ed67789SDaniel Lezcano 					       GFP_KERNEL);
311968fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
312068fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
3121d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
31228ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
3123d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
312462fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
312562fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
31268ed67789SDaniel Lezcano #endif
31278ed67789SDaniel Lezcano 
3128b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
3129b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
3130b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3131b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3132b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3133b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3134b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3135b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3136b339a47cSPeter Zijlstra 
31376891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
31386891a346SBenjamin Thery 
31398ed67789SDaniel Lezcano 	ret = 0;
31408ed67789SDaniel Lezcano out:
31418ed67789SDaniel Lezcano 	return ret;
3142f2fc6a54SBenjamin Thery 
314368fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
314468fffc67SPeter Zijlstra out_ip6_prohibit_entry:
314568fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
314668fffc67SPeter Zijlstra out_ip6_null_entry:
314768fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
314868fffc67SPeter Zijlstra #endif
3149fc66f95cSEric Dumazet out_ip6_dst_entries:
3150fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3151f2fc6a54SBenjamin Thery out_ip6_dst_ops:
3152f2fc6a54SBenjamin Thery 	goto out;
3153cdb18761SDaniel Lezcano }
3154cdb18761SDaniel Lezcano 
31552c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
3156cdb18761SDaniel Lezcano {
31578ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
31588ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
31598ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
31608ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
31618ed67789SDaniel Lezcano #endif
316241bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3163cdb18761SDaniel Lezcano }
3164cdb18761SDaniel Lezcano 
3165d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
3166d189634eSThomas Graf {
3167d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3168d4beaa66SGao feng 	proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops);
3169d4beaa66SGao feng 	proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops);
3170d189634eSThomas Graf #endif
3171d189634eSThomas Graf 	return 0;
3172d189634eSThomas Graf }
3173d189634eSThomas Graf 
3174d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
3175d189634eSThomas Graf {
3176d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3177ece31ffdSGao feng 	remove_proc_entry("ipv6_route", net->proc_net);
3178ece31ffdSGao feng 	remove_proc_entry("rt6_stats", net->proc_net);
3179d189634eSThomas Graf #endif
3180d189634eSThomas Graf }
3181d189634eSThomas Graf 
3182cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
3183cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
3184cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
3185cdb18761SDaniel Lezcano };
3186cdb18761SDaniel Lezcano 
3187c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
3188c3426b47SDavid S. Miller {
3189c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3190c3426b47SDavid S. Miller 
3191c3426b47SDavid S. Miller 	if (!bp)
3192c3426b47SDavid S. Miller 		return -ENOMEM;
3193c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
3194c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
3195c3426b47SDavid S. Miller 	return 0;
3196c3426b47SDavid S. Miller }
3197c3426b47SDavid S. Miller 
3198c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
3199c3426b47SDavid S. Miller {
3200c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
3201c3426b47SDavid S. Miller 
3202c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
320356a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
3204c3426b47SDavid S. Miller 	kfree(bp);
3205c3426b47SDavid S. Miller }
3206c3426b47SDavid S. Miller 
32072b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
3208c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
3209c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
3210c3426b47SDavid S. Miller };
3211c3426b47SDavid S. Miller 
3212d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
3213d189634eSThomas Graf 	.init = ip6_route_net_init_late,
3214d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
3215d189634eSThomas Graf };
3216d189634eSThomas Graf 
32178ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
32188ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
32198ed67789SDaniel Lezcano 	.priority = 0,
32208ed67789SDaniel Lezcano };
32218ed67789SDaniel Lezcano 
3222433d49c3SDaniel Lezcano int __init ip6_route_init(void)
32231da177e4SLinus Torvalds {
3224433d49c3SDaniel Lezcano 	int ret;
3225433d49c3SDaniel Lezcano 
32269a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
32279a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
32289a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
32299a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
32309a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3231c19a28e1SFernando Carrijo 		goto out;
323214e50e57SDavid S. Miller 
3233fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
32348ed67789SDaniel Lezcano 	if (ret)
3235bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3236bdb3289fSDaniel Lezcano 
3237c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3238c3426b47SDavid S. Miller 	if (ret)
3239e8803b6cSDavid S. Miller 		goto out_dst_entries;
32402a0c451aSThomas Graf 
32417e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
32427e52b33bSDavid S. Miller 	if (ret)
32437e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3244c3426b47SDavid S. Miller 
32455dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
32465dc121e9SArnaud Ebalard 
32478ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
32488ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
32498ed67789SDaniel Lezcano 	 * manually for init_net */
3250d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
32518ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3252bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3253d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
32548ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3255d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
32568ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3257bdb3289fSDaniel Lezcano   #endif
3258e8803b6cSDavid S. Miller 	ret = fib6_init();
3259433d49c3SDaniel Lezcano 	if (ret)
32608ed67789SDaniel Lezcano 		goto out_register_subsys;
3261433d49c3SDaniel Lezcano 
3262433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3263433d49c3SDaniel Lezcano 	if (ret)
3264e8803b6cSDavid S. Miller 		goto out_fib6_init;
3265c35b7e72SDaniel Lezcano 
3266433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3267433d49c3SDaniel Lezcano 	if (ret)
3268433d49c3SDaniel Lezcano 		goto xfrm6_init;
32697e5449c2SDaniel Lezcano 
3270d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3271d189634eSThomas Graf 	if (ret)
3272d189634eSThomas Graf 		goto fib6_rules_init;
3273d189634eSThomas Graf 
3274433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3275c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3276c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3277c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3278d189634eSThomas Graf 		goto out_register_late_subsys;
3279433d49c3SDaniel Lezcano 
32808ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3281cdb18761SDaniel Lezcano 	if (ret)
3282d189634eSThomas Graf 		goto out_register_late_subsys;
32838ed67789SDaniel Lezcano 
3284433d49c3SDaniel Lezcano out:
3285433d49c3SDaniel Lezcano 	return ret;
3286433d49c3SDaniel Lezcano 
3287d189634eSThomas Graf out_register_late_subsys:
3288d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3289433d49c3SDaniel Lezcano fib6_rules_init:
3290433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3291433d49c3SDaniel Lezcano xfrm6_init:
3292433d49c3SDaniel Lezcano 	xfrm6_fini();
32932a0c451aSThomas Graf out_fib6_init:
32942a0c451aSThomas Graf 	fib6_gc_cleanup();
32958ed67789SDaniel Lezcano out_register_subsys:
32968ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
32977e52b33bSDavid S. Miller out_register_inetpeer:
32987e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3299fc66f95cSEric Dumazet out_dst_entries:
3300fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3301433d49c3SDaniel Lezcano out_kmem_cache:
3302f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3303433d49c3SDaniel Lezcano 	goto out;
33041da177e4SLinus Torvalds }
33051da177e4SLinus Torvalds 
33061da177e4SLinus Torvalds void ip6_route_cleanup(void)
33071da177e4SLinus Torvalds {
33088ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3309d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3310101367c2SThomas Graf 	fib6_rules_cleanup();
33111da177e4SLinus Torvalds 	xfrm6_fini();
33121da177e4SLinus Torvalds 	fib6_gc_cleanup();
3313c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
33148ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
331541bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3316f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
33171da177e4SLinus Torvalds }
3318