xref: /openbmc/linux/net/ipv6/route.c (revision 4dc27d1cf3b3027b9ce654221c559e88b2f41b33)
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>
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #include <asm/uaccess.h>
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
641da177e4SLinus Torvalds #include <linux/sysctl.h>
651da177e4SLinus Torvalds #endif
661da177e4SLinus Torvalds 
671716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
6821efcfa0SEric Dumazet 				    const struct in6_addr *dest);
691da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
700dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
71ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
721da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
731da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
741da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
751da177e4SLinus Torvalds 				       struct net_device *dev, int how);
76569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
791da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
801da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
811da177e4SLinus Torvalds static void		ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
821da177e4SLinus Torvalds 
8370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
84efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
85b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
86b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
8795c96174SEric Dumazet 					   unsigned int pref);
88efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
89b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
90b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
9170ceb4f5SYOSHIFUJI Hideaki #endif
9270ceb4f5SYOSHIFUJI Hideaki 
9306582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
9406582540SDavid S. Miller {
9506582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
9606582540SDavid S. Miller 	struct inet_peer *peer;
9706582540SDavid S. Miller 	u32 *p = NULL;
9806582540SDavid S. Miller 
998e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1008e2ec639SYan, Zheng 		return NULL;
1018e2ec639SYan, Zheng 
102fbfe95a4SDavid S. Miller 	peer = rt6_get_peer_create(rt);
10306582540SDavid S. Miller 	if (peer) {
10406582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
10506582540SDavid S. Miller 		unsigned long prev, new;
10606582540SDavid S. Miller 
10706582540SDavid S. Miller 		p = peer->metrics;
10806582540SDavid S. Miller 		if (inet_metrics_new(peer))
10906582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
11006582540SDavid S. Miller 
11106582540SDavid S. Miller 		new = (unsigned long) p;
11206582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
11306582540SDavid S. Miller 
11406582540SDavid S. Miller 		if (prev != old) {
11506582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
11606582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
11706582540SDavid S. Miller 				p = NULL;
11806582540SDavid S. Miller 		}
11906582540SDavid S. Miller 	}
12006582540SDavid S. Miller 	return p;
12106582540SDavid S. Miller }
12206582540SDavid S. Miller 
12339232973SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, const void *daddr)
12439232973SDavid S. Miller {
12539232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
12639232973SDavid S. Miller 
127a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
12839232973SDavid S. Miller 		return (const void *) p;
12939232973SDavid S. Miller 	return daddr;
13039232973SDavid S. Miller }
13139232973SDavid S. Miller 
132d3aaeb38SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, const void *daddr)
133d3aaeb38SDavid S. Miller {
13439232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
13539232973SDavid S. Miller 	struct neighbour *n;
13639232973SDavid S. Miller 
13739232973SDavid S. Miller 	daddr = choose_neigh_daddr(rt, daddr);
13839232973SDavid S. Miller 	n = __ipv6_neigh_lookup(&nd_tbl, dst->dev, daddr);
139f83c7790SDavid S. Miller 	if (n)
140f83c7790SDavid S. Miller 		return n;
141f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
142f83c7790SDavid S. Miller }
143f83c7790SDavid S. Miller 
1448ade06c6SDavid S. Miller static int rt6_bind_neighbour(struct rt6_info *rt, struct net_device *dev)
145f83c7790SDavid S. Miller {
1468ade06c6SDavid S. Miller 	struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dev, &rt->rt6i_gateway);
1478ade06c6SDavid S. Miller 	if (!n) {
1488ade06c6SDavid S. Miller 		n = neigh_create(&nd_tbl, &rt->rt6i_gateway, dev);
149f83c7790SDavid S. Miller 		if (IS_ERR(n))
150f83c7790SDavid S. Miller 			return PTR_ERR(n);
1518ade06c6SDavid S. Miller 	}
152f83c7790SDavid S. Miller 	dst_set_neighbour(&rt->dst, n);
153f83c7790SDavid S. Miller 
154f83c7790SDavid S. Miller 	return 0;
155d3aaeb38SDavid S. Miller }
156d3aaeb38SDavid S. Miller 
1579a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1581da177e4SLinus Torvalds 	.family			=	AF_INET6,
15909640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1601da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1611da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1621da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1630dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
164ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
16506582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1661da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1671da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1681da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1691da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1701da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1711ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
172d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1731da177e4SLinus Torvalds };
1741da177e4SLinus Torvalds 
175ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
176ec831ea7SRoland Dreier {
177618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
178618f9bc7SSteffen Klassert 
179618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
180ec831ea7SRoland Dreier }
181ec831ea7SRoland Dreier 
18214e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
18314e50e57SDavid S. Miller {
18414e50e57SDavid S. Miller }
18514e50e57SDavid S. Miller 
1860972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1870972ddb2SHeld Bernhard 					 unsigned long old)
1880972ddb2SHeld Bernhard {
1890972ddb2SHeld Bernhard 	return NULL;
1900972ddb2SHeld Bernhard }
1910972ddb2SHeld Bernhard 
19214e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
19314e50e57SDavid S. Miller 	.family			=	AF_INET6,
19409640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
19514e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
19614e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
197ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
198214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
19914e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
2000972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
201d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
20214e50e57SDavid S. Miller };
20314e50e57SDavid S. Miller 
20462fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
20562fa8a84SDavid S. Miller 	[RTAX_HOPLIMIT - 1] = 255,
20662fa8a84SDavid S. Miller };
20762fa8a84SDavid S. Miller 
208bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = {
2091da177e4SLinus Torvalds 	.dst = {
2101da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2111da177e4SLinus Torvalds 		.__use		= 1,
2121da177e4SLinus Torvalds 		.obsolete	= -1,
2131da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2141da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2151da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2161da177e4SLinus Torvalds 	},
2171da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2184f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2191da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2201da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2211da177e4SLinus Torvalds };
2221da177e4SLinus Torvalds 
223101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
224101367c2SThomas Graf 
2256723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
2266723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
2276723ab54SDavid S. Miller 
228280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = {
229101367c2SThomas Graf 	.dst = {
230101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
231101367c2SThomas Graf 		.__use		= 1,
232101367c2SThomas Graf 		.obsolete	= -1,
233101367c2SThomas Graf 		.error		= -EACCES,
2349ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2359ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
236101367c2SThomas Graf 	},
237101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2384f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
239101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
240101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
241101367c2SThomas Graf };
242101367c2SThomas Graf 
243bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = {
244101367c2SThomas Graf 	.dst = {
245101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
246101367c2SThomas Graf 		.__use		= 1,
247101367c2SThomas Graf 		.obsolete	= -1,
248101367c2SThomas Graf 		.error		= -EINVAL,
249352e512cSHerbert Xu 		.input		= dst_discard,
250352e512cSHerbert Xu 		.output		= dst_discard,
251101367c2SThomas Graf 	},
252101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2534f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
254101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
255101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
256101367c2SThomas Graf };
257101367c2SThomas Graf 
258101367c2SThomas Graf #endif
259101367c2SThomas Graf 
2601da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
26197bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net,
262957c665fSDavid S. Miller 					     struct net_device *dev,
2638b96d22dSDavid S. Miller 					     int flags,
2648b96d22dSDavid S. Miller 					     struct fib6_table *table)
2651da177e4SLinus Torvalds {
26697bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
26797bab73fSDavid S. Miller 					0, 0, flags);
268cf911662SDavid S. Miller 
26997bab73fSDavid S. Miller 	if (rt) {
270fbe58186SMadalin Bucur 		memset(&rt->rt6i_table, 0,
271fbe58186SMadalin Bucur 		       sizeof(*rt) - sizeof(struct dst_entry));
2728b96d22dSDavid S. Miller 		rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
27397bab73fSDavid S. Miller 	}
274cf911662SDavid S. Miller 	return rt;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2801da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2811da177e4SLinus Torvalds 
2828e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
2838e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
2848e2ec639SYan, Zheng 
28538308473SDavid S. Miller 	if (idev) {
2861da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2871da177e4SLinus Torvalds 		in6_dev_put(idev);
2881da177e4SLinus Torvalds 	}
2891716a961SGao feng 
2901716a961SGao feng 	if (!(rt->rt6i_flags & RTF_EXPIRES) && dst->from)
2911716a961SGao feng 		dst_release(dst->from);
2921716a961SGao feng 
29397bab73fSDavid S. Miller 	if (rt6_has_peer(rt)) {
29497bab73fSDavid S. Miller 		struct inet_peer *peer = rt6_peer_ptr(rt);
295b3419363SDavid S. Miller 		inet_putpeer(peer);
296b3419363SDavid S. Miller 	}
297b3419363SDavid S. Miller }
298b3419363SDavid S. Miller 
2996431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
3006431cbc2SDavid S. Miller 
3016431cbc2SDavid S. Miller static u32 rt6_peer_genid(void)
3026431cbc2SDavid S. Miller {
3036431cbc2SDavid S. Miller 	return atomic_read(&__rt6_peer_genid);
3046431cbc2SDavid S. Miller }
3056431cbc2SDavid S. Miller 
306b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
307b3419363SDavid S. Miller {
30897bab73fSDavid S. Miller 	struct inet_peer_base *base;
309b3419363SDavid S. Miller 	struct inet_peer *peer;
310b3419363SDavid S. Miller 
31197bab73fSDavid S. Miller 	base = inetpeer_base_ptr(rt->_rt6i_peer);
31297bab73fSDavid S. Miller 	if (!base)
31397bab73fSDavid S. Miller 		return;
31497bab73fSDavid S. Miller 
31597bab73fSDavid S. Miller 	peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
3167b34ca2aSDavid S. Miller 	if (peer) {
31797bab73fSDavid S. Miller 		if (!rt6_set_peer(rt, peer))
318b3419363SDavid S. Miller 			inet_putpeer(peer);
3196431cbc2SDavid S. Miller 		else
3206431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
3211da177e4SLinus Torvalds 	}
3227b34ca2aSDavid S. Miller }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3251da177e4SLinus Torvalds 			   int how)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3281da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3295a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
330c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3311da177e4SLinus Torvalds 
33238308473SDavid S. Miller 	if (dev != loopback_dev && idev && idev->dev == dev) {
3335a3e55d6SDenis V. Lunev 		struct inet6_dev *loopback_idev =
3345a3e55d6SDenis V. Lunev 			in6_dev_get(loopback_dev);
33538308473SDavid S. Miller 		if (loopback_idev) {
3361da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
3371da177e4SLinus Torvalds 			in6_dev_put(idev);
3381da177e4SLinus Torvalds 		}
3391da177e4SLinus Torvalds 	}
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
342a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3431da177e4SLinus Torvalds {
3441716a961SGao feng 	struct rt6_info *ort = NULL;
3451716a961SGao feng 
3461716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3471716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
348a50feda5SEric Dumazet 			return true;
3491716a961SGao feng 	} else if (rt->dst.from) {
3501716a961SGao feng 		ort = (struct rt6_info *) rt->dst.from;
3511716a961SGao feng 		return (ort->rt6i_flags & RTF_EXPIRES) &&
3521716a961SGao feng 			time_after(jiffies, ort->dst.expires);
3531716a961SGao feng 	}
354a50feda5SEric Dumazet 	return false;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
357a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr)
358c71099acSThomas Graf {
359a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
360a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
361c71099acSThomas Graf }
362c71099acSThomas Graf 
3631da177e4SLinus Torvalds /*
364c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
3651da177e4SLinus Torvalds  */
3661da177e4SLinus Torvalds 
3678ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
3688ed67789SDaniel Lezcano 						    struct rt6_info *rt,
369b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
3701da177e4SLinus Torvalds 						    int oif,
371d420895eSYOSHIFUJI Hideaki 						    int flags)
3721da177e4SLinus Torvalds {
3731da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
3741da177e4SLinus Torvalds 	struct rt6_info *sprt;
3751da177e4SLinus Torvalds 
376dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
377dd3abc4eSYOSHIFUJI Hideaki 		goto out;
378dd3abc4eSYOSHIFUJI Hideaki 
379d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
380d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
381dd3abc4eSYOSHIFUJI Hideaki 
382dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
3831da177e4SLinus Torvalds 			if (dev->ifindex == oif)
3841da177e4SLinus Torvalds 				return sprt;
3851da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
38638308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
3871da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
388d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
3891da177e4SLinus Torvalds 						continue;
3901da177e4SLinus Torvalds 					if (local && (!oif ||
3911da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
3921da177e4SLinus Torvalds 						continue;
3931da177e4SLinus Torvalds 				}
3941da177e4SLinus Torvalds 				local = sprt;
3951da177e4SLinus Torvalds 			}
396dd3abc4eSYOSHIFUJI Hideaki 		} else {
397dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
398dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
399dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
400dd3abc4eSYOSHIFUJI Hideaki 		}
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 
403dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4041da177e4SLinus Torvalds 		if (local)
4051da177e4SLinus Torvalds 			return local;
4061da177e4SLinus Torvalds 
407d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4088ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4091da177e4SLinus Torvalds 	}
410dd3abc4eSYOSHIFUJI Hideaki out:
4111da177e4SLinus Torvalds 	return rt;
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
41427097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
41527097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
41627097255SYOSHIFUJI Hideaki {
417f2c31e32SEric Dumazet 	struct neighbour *neigh;
41827097255SYOSHIFUJI Hideaki 	/*
41927097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
42027097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
42127097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
42227097255SYOSHIFUJI Hideaki 	 *
42327097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
42427097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
42527097255SYOSHIFUJI Hideaki 	 */
426f2c31e32SEric Dumazet 	rcu_read_lock();
42727217455SDavid Miller 	neigh = rt ? dst_get_neighbour_noref(&rt->dst) : NULL;
42827097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
429f2c31e32SEric Dumazet 		goto out;
43027097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
43127097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
43252e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
43327097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
43427097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
43527097255SYOSHIFUJI Hideaki 
43627097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
43727097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
43827097255SYOSHIFUJI Hideaki 
43927097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
44027097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
441d1918542SDavid S. Miller 		ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
442f2c31e32SEric Dumazet 	} else {
44327097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
44427097255SYOSHIFUJI Hideaki 	}
445f2c31e32SEric Dumazet out:
446f2c31e32SEric Dumazet 	rcu_read_unlock();
447f2c31e32SEric Dumazet }
44827097255SYOSHIFUJI Hideaki #else
44927097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
45027097255SYOSHIFUJI Hideaki {
45127097255SYOSHIFUJI Hideaki }
45227097255SYOSHIFUJI Hideaki #endif
45327097255SYOSHIFUJI Hideaki 
4541da177e4SLinus Torvalds /*
455554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
4561da177e4SLinus Torvalds  */
457b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
4581da177e4SLinus Torvalds {
459d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
460161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
461554cfb7eSYOSHIFUJI Hideaki 		return 2;
462161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
463161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
464161980f4SDavid S. Miller 		return 1;
465554cfb7eSYOSHIFUJI Hideaki 	return 0;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
468b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
4691da177e4SLinus Torvalds {
470f2c31e32SEric Dumazet 	struct neighbour *neigh;
471398bcbebSYOSHIFUJI Hideaki 	int m;
472f2c31e32SEric Dumazet 
473f2c31e32SEric Dumazet 	rcu_read_lock();
47427217455SDavid Miller 	neigh = dst_get_neighbour_noref(&rt->dst);
4754d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
4764d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
4774d0c5911SYOSHIFUJI Hideaki 		m = 1;
4784d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
4791da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
480554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
4814d0c5911SYOSHIFUJI Hideaki 			m = 2;
482398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
483398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
484398bcbebSYOSHIFUJI Hideaki 			m = 0;
485398bcbebSYOSHIFUJI Hideaki #endif
486398bcbebSYOSHIFUJI Hideaki 		else
487ea73ee23SYOSHIFUJI Hideaki 			m = 1;
4881da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
489398bcbebSYOSHIFUJI Hideaki 	} else
490398bcbebSYOSHIFUJI Hideaki 		m = 0;
491f2c31e32SEric Dumazet 	rcu_read_unlock();
492554cfb7eSYOSHIFUJI Hideaki 	return m;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
495554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
496554cfb7eSYOSHIFUJI Hideaki 			   int strict)
497554cfb7eSYOSHIFUJI Hideaki {
4984d0c5911SYOSHIFUJI Hideaki 	int m, n;
4994d0c5911SYOSHIFUJI Hideaki 
5004d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
50177d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
502554cfb7eSYOSHIFUJI Hideaki 		return -1;
503ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
504ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
505ebacaaa0SYOSHIFUJI Hideaki #endif
5064d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
507557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
508554cfb7eSYOSHIFUJI Hideaki 		return -1;
509554cfb7eSYOSHIFUJI Hideaki 	return m;
510554cfb7eSYOSHIFUJI Hideaki }
511554cfb7eSYOSHIFUJI Hideaki 
512f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
513f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
514554cfb7eSYOSHIFUJI Hideaki {
515554cfb7eSYOSHIFUJI Hideaki 	int m;
516554cfb7eSYOSHIFUJI Hideaki 
517554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
518f11e6659SDavid S. Miller 		goto out;
519554cfb7eSYOSHIFUJI Hideaki 
520554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
521554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
522f11e6659SDavid S. Miller 		goto out;
523554cfb7eSYOSHIFUJI Hideaki 
524f11e6659SDavid S. Miller 	if (m > *mpri) {
525ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
52627097255SYOSHIFUJI Hideaki 			rt6_probe(match);
527f11e6659SDavid S. Miller 		*mpri = m;
528554cfb7eSYOSHIFUJI Hideaki 		match = rt;
529ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
53027097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
5311da177e4SLinus Torvalds 	}
532f11e6659SDavid S. Miller 
533f11e6659SDavid S. Miller out:
534f11e6659SDavid S. Miller 	return match;
5351da177e4SLinus Torvalds }
5361da177e4SLinus Torvalds 
537f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
538f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
539f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
540f11e6659SDavid S. Miller {
541f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
542f11e6659SDavid S. Miller 	int mpri = -1;
543f11e6659SDavid S. Miller 
544f11e6659SDavid S. Miller 	match = NULL;
545f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
546d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
547f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
548f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
549d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
550f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
551f11e6659SDavid S. Miller 
552f11e6659SDavid S. Miller 	return match;
553f11e6659SDavid S. Miller }
554f11e6659SDavid S. Miller 
555f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
556f11e6659SDavid S. Miller {
557f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
5588ed67789SDaniel Lezcano 	struct net *net;
559f11e6659SDavid S. Miller 
560f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
561f11e6659SDavid S. Miller 	if (!rt0)
562f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
563f11e6659SDavid S. Miller 
564f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
565f11e6659SDavid S. Miller 
566554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
567f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
568d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
569f11e6659SDavid S. Miller 
570554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
571f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
572f11e6659SDavid S. Miller 			next = fn->leaf;
573f11e6659SDavid S. Miller 
574f11e6659SDavid S. Miller 		if (next != rt0)
575f11e6659SDavid S. Miller 			fn->rr_ptr = next;
576554cfb7eSYOSHIFUJI Hideaki 	}
577554cfb7eSYOSHIFUJI Hideaki 
578d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
579a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
58270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
58370ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
584b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
58570ceb4f5SYOSHIFUJI Hideaki {
586c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
58770ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
58870ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
58970ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
5904bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
59170ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
59270ceb4f5SYOSHIFUJI Hideaki 
59370ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
59470ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
59570ceb4f5SYOSHIFUJI Hideaki 	}
59670ceb4f5SYOSHIFUJI Hideaki 
59770ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
59870ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
59970ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
60070ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
60170ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
60270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
60370ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
60470ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
60570ceb4f5SYOSHIFUJI Hideaki 		}
60670ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
60770ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
60870ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
60970ceb4f5SYOSHIFUJI Hideaki 		}
61070ceb4f5SYOSHIFUJI Hideaki 	}
61170ceb4f5SYOSHIFUJI Hideaki 
61270ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
61370ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
6143933fc95SJens Rosenboom 		return -EINVAL;
61570ceb4f5SYOSHIFUJI Hideaki 
6164bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
61770ceb4f5SYOSHIFUJI Hideaki 
61870ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
61970ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
62070ceb4f5SYOSHIFUJI Hideaki 	else {
62170ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
62270ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
62370ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
62470ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
62570ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
62670ceb4f5SYOSHIFUJI Hideaki 	}
62770ceb4f5SYOSHIFUJI Hideaki 
628efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
629efa2cea0SDaniel Lezcano 				dev->ifindex);
63070ceb4f5SYOSHIFUJI Hideaki 
63170ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
632e0a1ad73SThomas Graf 		ip6_del_rt(rt);
63370ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
63470ceb4f5SYOSHIFUJI Hideaki 	}
63570ceb4f5SYOSHIFUJI Hideaki 
63670ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
637efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
63870ceb4f5SYOSHIFUJI Hideaki 					pref);
63970ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
64070ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
64170ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
64270ceb4f5SYOSHIFUJI Hideaki 
64370ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
6441716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
6451716a961SGao feng 			rt6_clean_expires(rt);
6461716a961SGao feng 		else
6471716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
6481716a961SGao feng 
649d8d1f30bSChangli Gao 		dst_release(&rt->dst);
65070ceb4f5SYOSHIFUJI Hideaki 	}
65170ceb4f5SYOSHIFUJI Hideaki 	return 0;
65270ceb4f5SYOSHIFUJI Hideaki }
65370ceb4f5SYOSHIFUJI Hideaki #endif
65470ceb4f5SYOSHIFUJI Hideaki 
6558ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
656982f56f3SYOSHIFUJI Hideaki do { \
6578ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
658982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
659e0eda7bbSVille Nuorvala 		while (1) { \
660982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
661c71099acSThomas Graf 				goto out; \
662982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
663982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
6648bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
665982f56f3SYOSHIFUJI Hideaki 			else \
666982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
667c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
668c71099acSThomas Graf 				goto restart; \
669c71099acSThomas Graf 		} \
670982f56f3SYOSHIFUJI Hideaki 	} \
671982f56f3SYOSHIFUJI Hideaki } while (0)
672c71099acSThomas Graf 
6738ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
6748ed67789SDaniel Lezcano 					     struct fib6_table *table,
6754c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
6761da177e4SLinus Torvalds {
6771da177e4SLinus Torvalds 	struct fib6_node *fn;
6781da177e4SLinus Torvalds 	struct rt6_info *rt;
6791da177e4SLinus Torvalds 
680c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
6814c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
682c71099acSThomas Graf restart:
683c71099acSThomas Graf 	rt = fn->leaf;
6844c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
6854c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
686c71099acSThomas Graf out:
687d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
688c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
6891da177e4SLinus Torvalds 	return rt;
690c71099acSThomas Graf 
691c71099acSThomas Graf }
692c71099acSThomas Graf 
693ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
694ea6e574eSFlorian Westphal 				    int flags)
695ea6e574eSFlorian Westphal {
696ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
697ea6e574eSFlorian Westphal }
698ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
699ea6e574eSFlorian Westphal 
7009acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
7019acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
702c71099acSThomas Graf {
7034c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
7044c9483b2SDavid S. Miller 		.flowi6_oif = oif,
7054c9483b2SDavid S. Miller 		.daddr = *daddr,
706c71099acSThomas Graf 	};
707c71099acSThomas Graf 	struct dst_entry *dst;
70877d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
709c71099acSThomas Graf 
710adaa70bbSThomas Graf 	if (saddr) {
7114c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
712adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
713adaa70bbSThomas Graf 	}
714adaa70bbSThomas Graf 
7154c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
716c71099acSThomas Graf 	if (dst->error == 0)
717c71099acSThomas Graf 		return (struct rt6_info *) dst;
718c71099acSThomas Graf 
719c71099acSThomas Graf 	dst_release(dst);
720c71099acSThomas Graf 
7211da177e4SLinus Torvalds 	return NULL;
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
7247159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
7257159039aSYOSHIFUJI Hideaki 
726c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
7271da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
7281da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
7291da177e4SLinus Torvalds    be destroyed.
7301da177e4SLinus Torvalds  */
7311da177e4SLinus Torvalds 
73286872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	int err;
735c71099acSThomas Graf 	struct fib6_table *table;
7361da177e4SLinus Torvalds 
737c71099acSThomas Graf 	table = rt->rt6i_table;
738c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
73986872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
740c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	return err;
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
74540e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
74640e22e8fSThomas Graf {
7474d1169c1SDenis V. Lunev 	struct nl_info info = {
748d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
7494d1169c1SDenis V. Lunev 	};
750528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
75140e22e8fSThomas Graf }
75240e22e8fSThomas Graf 
7531716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
75421efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
755b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
7561da177e4SLinus Torvalds {
7571da177e4SLinus Torvalds 	struct rt6_info *rt;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	/*
7601da177e4SLinus Torvalds 	 *	Clone the route.
7611da177e4SLinus Torvalds 	 */
7621da177e4SLinus Torvalds 
76321efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	if (rt) {
76614deae41SDavid S. Miller 		int attempts = !in_softirq();
76714deae41SDavid S. Miller 
76858c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags & RTF_GATEWAY)) {
769bb3c3686SDavid S. Miller 			if (ort->rt6i_dst.plen != 128 &&
77021efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
77158c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
7724e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = *daddr;
77358c4fb86SYOSHIFUJI Hideaki 		}
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
7781da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
7794e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
7801da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
7811da177e4SLinus Torvalds 		}
7821da177e4SLinus Torvalds #endif
7831da177e4SLinus Torvalds 
78414deae41SDavid S. Miller 	retry:
7858ade06c6SDavid S. Miller 		if (rt6_bind_neighbour(rt, rt->dst.dev)) {
786d1918542SDavid S. Miller 			struct net *net = dev_net(rt->dst.dev);
78714deae41SDavid S. Miller 			int saved_rt_min_interval =
78814deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval;
78914deae41SDavid S. Miller 			int saved_rt_elasticity =
79014deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity;
79114deae41SDavid S. Miller 
79214deae41SDavid S. Miller 			if (attempts-- > 0) {
79314deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
79414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
79514deae41SDavid S. Miller 
79686393e52SAlexey Dobriyan 				ip6_dst_gc(&net->ipv6.ip6_dst_ops);
79714deae41SDavid S. Miller 
79814deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity =
79914deae41SDavid S. Miller 					saved_rt_elasticity;
80014deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval =
80114deae41SDavid S. Miller 					saved_rt_min_interval;
80214deae41SDavid S. Miller 				goto retry;
80314deae41SDavid S. Miller 			}
80414deae41SDavid S. Miller 
805f3213831SJoe Perches 			net_warn_ratelimited("Neighbour table overflow\n");
806d8d1f30bSChangli Gao 			dst_free(&rt->dst);
80714deae41SDavid S. Miller 			return NULL;
80814deae41SDavid S. Miller 		}
80995a9a5baSYOSHIFUJI Hideaki 	}
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	return rt;
8121da177e4SLinus Torvalds }
81395a9a5baSYOSHIFUJI Hideaki 
81421efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
81521efcfa0SEric Dumazet 					const struct in6_addr *daddr)
816299d9939SYOSHIFUJI Hideaki {
81721efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
81821efcfa0SEric Dumazet 
819299d9939SYOSHIFUJI Hideaki 	if (rt) {
820299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
82127217455SDavid Miller 		dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_noref_raw(&ort->dst)));
822299d9939SYOSHIFUJI Hideaki 	}
823299d9939SYOSHIFUJI Hideaki 	return rt;
824299d9939SYOSHIFUJI Hideaki }
825299d9939SYOSHIFUJI Hideaki 
8268ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
8274c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	struct fib6_node *fn;
830519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
831c71099acSThomas Graf 	int strict = 0;
8321da177e4SLinus Torvalds 	int attempts = 3;
833519fbd87SYOSHIFUJI Hideaki 	int err;
83453b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
8351da177e4SLinus Torvalds 
83677d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds relookup:
839c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8401da177e4SLinus Torvalds 
8418238dd06SYOSHIFUJI Hideaki restart_2:
8424c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds restart:
8454acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
8468ed67789SDaniel Lezcano 
8474c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
8488ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
8498238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
8501da177e4SLinus Torvalds 		goto out;
8511da177e4SLinus Torvalds 
852d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
853c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8541da177e4SLinus Torvalds 
85527217455SDavid Miller 	if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
8564c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
8577343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
8584c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
8597343ff31SDavid S. Miller 	else
8607343ff31SDavid S. Miller 		goto out2;
8611da177e4SLinus Torvalds 
862d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8638ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
8641da177e4SLinus Torvalds 
865d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
866e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
86740e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
868e40cf353SYOSHIFUJI Hideaki 		if (!err)
869e40cf353SYOSHIFUJI Hideaki 			goto out2;
870e40cf353SYOSHIFUJI Hideaki 	}
871e40cf353SYOSHIFUJI Hideaki 
872e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
8731da177e4SLinus Torvalds 		goto out2;
8741da177e4SLinus Torvalds 
875519fbd87SYOSHIFUJI Hideaki 	/*
876c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
877519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
8781da177e4SLinus Torvalds 	 */
879d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8801da177e4SLinus Torvalds 	goto relookup;
881e40cf353SYOSHIFUJI Hideaki 
882519fbd87SYOSHIFUJI Hideaki out:
8838238dd06SYOSHIFUJI Hideaki 	if (reachable) {
8848238dd06SYOSHIFUJI Hideaki 		reachable = 0;
8858238dd06SYOSHIFUJI Hideaki 		goto restart_2;
8868238dd06SYOSHIFUJI Hideaki 	}
887d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
888c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8891da177e4SLinus Torvalds out2:
890d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
891d8d1f30bSChangli Gao 	rt->dst.__use++;
892c71099acSThomas Graf 
893c71099acSThomas Graf 	return rt;
894c71099acSThomas Graf }
895c71099acSThomas Graf 
8968ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
8974c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
8984acad72dSPavel Emelyanov {
8994c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9004acad72dSPavel Emelyanov }
9014acad72dSPavel Emelyanov 
90272331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
90372331bc0SShmulik Ladkani 						struct net_device *dev,
90472331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
90572331bc0SShmulik Ladkani {
90672331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
90772331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
90872331bc0SShmulik Ladkani 
90972331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
91072331bc0SShmulik Ladkani }
91172331bc0SShmulik Ladkani 
912c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
913c71099acSThomas Graf {
914b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
915c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
916adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
9174c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9184c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
9194c9483b2SDavid S. Miller 		.daddr = iph->daddr,
9204c9483b2SDavid S. Miller 		.saddr = iph->saddr,
9214c9483b2SDavid S. Miller 		.flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
9224c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
9234c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
924c71099acSThomas Graf 	};
925adaa70bbSThomas Graf 
92672331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
927c71099acSThomas Graf }
928c71099acSThomas Graf 
9298ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
9304c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
931c71099acSThomas Graf {
9324c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
933c71099acSThomas Graf }
934c71099acSThomas Graf 
9359c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
9364c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
937c71099acSThomas Graf {
938c71099acSThomas Graf 	int flags = 0;
939c71099acSThomas Graf 
940*4dc27d1cSDavid McCullough 	fl6->flowi6_iif = net->loopback_dev->ifindex;
941*4dc27d1cSDavid McCullough 
9424c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
94377d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
944c71099acSThomas Graf 
9454c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
946adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
9470c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
9480c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
949adaa70bbSThomas Graf 
9504c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
9511da177e4SLinus Torvalds }
9521da177e4SLinus Torvalds 
9537159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
9541da177e4SLinus Torvalds 
9552774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
95614e50e57SDavid S. Miller {
9575c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
95814e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
95914e50e57SDavid S. Miller 
9605c1e6aa3SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
96114e50e57SDavid S. Miller 	if (rt) {
962cf911662SDavid S. Miller 		memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
96397bab73fSDavid S. Miller 		rt6_init_peer(rt, net->ipv6.peers);
964cf911662SDavid S. Miller 
965d8d1f30bSChangli Gao 		new = &rt->dst;
96614e50e57SDavid S. Miller 
96714e50e57SDavid S. Miller 		new->__use = 1;
968352e512cSHerbert Xu 		new->input = dst_discard;
969352e512cSHerbert Xu 		new->output = dst_discard;
97014e50e57SDavid S. Miller 
97121efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
97221efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
97321efcfa0SEric Dumazet 		else
974defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
97514e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
97614e50e57SDavid S. Miller 		if (rt->rt6i_idev)
97714e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
97814e50e57SDavid S. Miller 
9794e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
9801716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
9811716a961SGao feng 		rt6_clean_expires(rt);
98214e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
98314e50e57SDavid S. Miller 
98414e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
98514e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
98614e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
98714e50e57SDavid S. Miller #endif
98814e50e57SDavid S. Miller 
98914e50e57SDavid S. Miller 		dst_free(new);
99014e50e57SDavid S. Miller 	}
99114e50e57SDavid S. Miller 
99269ead7afSDavid S. Miller 	dst_release(dst_orig);
99369ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
99414e50e57SDavid S. Miller }
99514e50e57SDavid S. Miller 
9961da177e4SLinus Torvalds /*
9971da177e4SLinus Torvalds  *	Destination cache support functions
9981da177e4SLinus Torvalds  */
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10011da177e4SLinus Torvalds {
10021da177e4SLinus Torvalds 	struct rt6_info *rt;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10051da177e4SLinus Torvalds 
10066431cbc2SDavid S. Miller 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
10076431cbc2SDavid S. Miller 		if (rt->rt6i_peer_genid != rt6_peer_genid()) {
100897bab73fSDavid S. Miller 			if (!rt6_has_peer(rt))
10096431cbc2SDavid S. Miller 				rt6_bind_peer(rt, 0);
10106431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
10116431cbc2SDavid S. Miller 		}
10121da177e4SLinus Torvalds 		return dst;
10136431cbc2SDavid S. Miller 	}
10141da177e4SLinus Torvalds 	return NULL;
10151da177e4SLinus Torvalds }
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
10181da177e4SLinus Torvalds {
10191da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	if (rt) {
102254c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
102354c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1024e0a1ad73SThomas Graf 				ip6_del_rt(rt);
102554c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
10261da177e4SLinus Torvalds 			}
102754c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
102854c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
102954c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
103054c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
103154c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
103254c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	struct rt6_info *rt;
10381da177e4SLinus Torvalds 
10393ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
10401da177e4SLinus Torvalds 
1041adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
10421da177e4SLinus Torvalds 	if (rt) {
10431716a961SGao feng 		if (rt->rt6i_flags & RTF_CACHE)
10441716a961SGao feng 			rt6_update_expires(rt, 0);
10451716a961SGao feng 		else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
10461da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
10471da177e4SLinus Torvalds 	}
10481da177e4SLinus Torvalds }
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
10511da177e4SLinus Torvalds {
10521da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
10531da177e4SLinus Torvalds 
105481aded24SDavid S. Miller 	dst_confirm(dst);
10551da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
105681aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
105781aded24SDavid S. Miller 
10581da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
10591da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1060defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
10611da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1062defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1063defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
10641da177e4SLinus Torvalds 		}
1065defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
106681aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
10671da177e4SLinus Torvalds 	}
10681da177e4SLinus Torvalds }
10691da177e4SLinus Torvalds 
107042ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
107142ae66c8SDavid S. Miller 		     int oif, u32 mark)
107281aded24SDavid S. Miller {
107381aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
107481aded24SDavid S. Miller 	struct dst_entry *dst;
107581aded24SDavid S. Miller 	struct flowi6 fl6;
107681aded24SDavid S. Miller 
107781aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
107881aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
107981aded24SDavid S. Miller 	fl6.flowi6_mark = mark;
108081aded24SDavid S. Miller 	fl6.flowi6_flags = FLOWI_FLAG_PRECOW_METRICS;
108181aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
108281aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
108381aded24SDavid S. Miller 	fl6.flowlabel = (*(__be32 *) iph) & IPV6_FLOWINFO_MASK;
108481aded24SDavid S. Miller 
108581aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
108681aded24SDavid S. Miller 	if (!dst->error)
108781aded24SDavid S. Miller 		ip6_rt_update_pmtu(dst, ntohl(mtu));
108881aded24SDavid S. Miller 	dst_release(dst);
108981aded24SDavid S. Miller }
109081aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
109181aded24SDavid S. Miller 
109281aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
109381aded24SDavid S. Miller {
109481aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
109581aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
109681aded24SDavid S. Miller }
109781aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
109881aded24SDavid S. Miller 
10990dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
11001da177e4SLinus Torvalds {
11010dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
11020dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
11030dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
11040dbaee3bSDavid S. Miller 
11051da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
11061da177e4SLinus Torvalds 
11075578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
11085578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	/*
11111da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
11121da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
11131da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
11141da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
11151da177e4SLinus Torvalds 	 */
11161da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
11171da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
11181da177e4SLinus Torvalds 	return mtu;
11191da177e4SLinus Torvalds }
11201da177e4SLinus Torvalds 
1121ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1122d33e4553SDavid S. Miller {
1123d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1124618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1125618f9bc7SSteffen Klassert 
1126618f9bc7SSteffen Klassert 	if (mtu)
1127618f9bc7SSteffen Klassert 		return mtu;
1128618f9bc7SSteffen Klassert 
1129618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1130d33e4553SDavid S. Miller 
1131d33e4553SDavid S. Miller 	rcu_read_lock();
1132d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1133d33e4553SDavid S. Miller 	if (idev)
1134d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1135d33e4553SDavid S. Miller 	rcu_read_unlock();
1136d33e4553SDavid S. Miller 
1137d33e4553SDavid S. Miller 	return mtu;
1138d33e4553SDavid S. Miller }
1139d33e4553SDavid S. Miller 
11403b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
11413b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
11425d0bbeebSThomas Graf 
11433b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
11441da177e4SLinus Torvalds 				  struct neighbour *neigh,
114587a11578SDavid S. Miller 				  struct flowi6 *fl6)
11461da177e4SLinus Torvalds {
114787a11578SDavid S. Miller 	struct dst_entry *dst;
11481da177e4SLinus Torvalds 	struct rt6_info *rt;
11491da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1150c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
11511da177e4SLinus Torvalds 
115238308473SDavid S. Miller 	if (unlikely(!idev))
1153122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
11541da177e4SLinus Torvalds 
11558b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
115638308473SDavid S. Miller 	if (unlikely(!rt)) {
11571da177e4SLinus Torvalds 		in6_dev_put(idev);
115887a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
11591da177e4SLinus Torvalds 		goto out;
11601da177e4SLinus Torvalds 	}
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	if (neigh)
11631da177e4SLinus Torvalds 		neigh_hold(neigh);
116414deae41SDavid S. Miller 	else {
1165f83c7790SDavid S. Miller 		neigh = ip6_neigh_lookup(&rt->dst, &fl6->daddr);
1166b43faac6SDavid S. Miller 		if (IS_ERR(neigh)) {
1167252c3d84SRongQing.Li 			in6_dev_put(idev);
1168b43faac6SDavid S. Miller 			dst_free(&rt->dst);
1169b43faac6SDavid S. Miller 			return ERR_CAST(neigh);
1170b43faac6SDavid S. Miller 		}
117114deae41SDavid S. Miller 	}
11721da177e4SLinus Torvalds 
11738e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
11748e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
117569cce1d1SDavid S. Miller 	dst_set_neighbour(&rt->dst, neigh);
1176d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
117787a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
11788e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
11798e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
11807011687fSGao feng 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
11811da177e4SLinus Torvalds 
11823b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1183d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1184d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
11853b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
11861da177e4SLinus Torvalds 
11875578689aSDaniel Lezcano 	fib6_force_start_gc(net);
11881da177e4SLinus Torvalds 
118987a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
119087a11578SDavid S. Miller 
11911da177e4SLinus Torvalds out:
119287a11578SDavid S. Miller 	return dst;
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds 
11953d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
11961da177e4SLinus Torvalds {
1197e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
11983d0f24a7SStephen Hemminger 	int more = 0;
11991da177e4SLinus Torvalds 
12003b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
12013b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
12025d0bbeebSThomas Graf 
12031da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
12041da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
12051da177e4SLinus Torvalds 			*pprev = dst->next;
12061da177e4SLinus Torvalds 			dst_free(dst);
12071da177e4SLinus Torvalds 		} else {
12081da177e4SLinus Torvalds 			pprev = &dst->next;
12093d0f24a7SStephen Hemminger 			++more;
12101da177e4SLinus Torvalds 		}
12111da177e4SLinus Torvalds 	}
12121da177e4SLinus Torvalds 
12133b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12145d0bbeebSThomas Graf 
12153d0f24a7SStephen Hemminger 	return more;
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
12181e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
12191e493d19SDavid S. Miller 			    void *arg)
12201e493d19SDavid S. Miller {
12211e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
12221e493d19SDavid S. Miller 
12231e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
12241e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
12251e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
12261e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
12271e493d19SDavid S. Miller 		if (func(rt, arg)) {
12281e493d19SDavid S. Miller 			*pprev = dst->next;
12291e493d19SDavid S. Miller 			dst_free(dst);
12301e493d19SDavid S. Miller 		} else {
12311e493d19SDavid S. Miller 			pprev = &dst->next;
12321e493d19SDavid S. Miller 		}
12331e493d19SDavid S. Miller 	}
12341e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
12351e493d19SDavid S. Miller }
12361e493d19SDavid S. Miller 
1237569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
12381da177e4SLinus Torvalds {
12391da177e4SLinus Torvalds 	unsigned long now = jiffies;
124086393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
12417019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
12427019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
12437019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
12447019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
12457019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1246fc66f95cSEric Dumazet 	int entries;
12471da177e4SLinus Torvalds 
1248fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
12497019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1250fc66f95cSEric Dumazet 	    entries <= rt_max_size)
12511da177e4SLinus Torvalds 		goto out;
12521da177e4SLinus Torvalds 
12536891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
12546891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
12556891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1256fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1257fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
12587019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
12591da177e4SLinus Torvalds out:
12607019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1261fc66f95cSEric Dumazet 	return entries > rt_max_size;
12621da177e4SLinus Torvalds }
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
12651da177e4SLinus Torvalds    but results in cleaner routing tables.
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds    Remove it only when all the things will work!
12681da177e4SLinus Torvalds  */
12691da177e4SLinus Torvalds 
12706b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
12711da177e4SLinus Torvalds {
12725170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1273a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
12746b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1275c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1276c68f24ccSEric Dumazet 
1277c68f24ccSEric Dumazet 		rcu_read_lock();
1278c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1279c68f24ccSEric Dumazet 		if (idev)
12801da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1281c68f24ccSEric Dumazet 		else
128253b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1283c68f24ccSEric Dumazet 		rcu_read_unlock();
12841da177e4SLinus Torvalds 	}
12851da177e4SLinus Torvalds 	return hoplimit;
12861da177e4SLinus Torvalds }
1287abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds /*
12901da177e4SLinus Torvalds  *
12911da177e4SLinus Torvalds  */
12921da177e4SLinus Torvalds 
129386872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
12941da177e4SLinus Torvalds {
12951da177e4SLinus Torvalds 	int err;
12965578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
12971da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
12981da177e4SLinus Torvalds 	struct net_device *dev = NULL;
12991da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1300c71099acSThomas Graf 	struct fib6_table *table;
13011da177e4SLinus Torvalds 	int addr_type;
13021da177e4SLinus Torvalds 
130386872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
13041da177e4SLinus Torvalds 		return -EINVAL;
13051da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
130686872cb5SThomas Graf 	if (cfg->fc_src_len)
13071da177e4SLinus Torvalds 		return -EINVAL;
13081da177e4SLinus Torvalds #endif
130986872cb5SThomas Graf 	if (cfg->fc_ifindex) {
13101da177e4SLinus Torvalds 		err = -ENODEV;
13115578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
13121da177e4SLinus Torvalds 		if (!dev)
13131da177e4SLinus Torvalds 			goto out;
13141da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
13151da177e4SLinus Torvalds 		if (!idev)
13161da177e4SLinus Torvalds 			goto out;
13171da177e4SLinus Torvalds 	}
13181da177e4SLinus Torvalds 
131986872cb5SThomas Graf 	if (cfg->fc_metric == 0)
132086872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
13211da177e4SLinus Torvalds 
1322c71099acSThomas Graf 	err = -ENOBUFS;
132338308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1324d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1325d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
132638308473SDavid S. Miller 		if (!table) {
1327f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1328d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1329d71314b4SMatti Vaittinen 		}
1330d71314b4SMatti Vaittinen 	} else {
1331d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1332d71314b4SMatti Vaittinen 	}
133338308473SDavid S. Miller 
133438308473SDavid S. Miller 	if (!table)
1335c71099acSThomas Graf 		goto out;
1336c71099acSThomas Graf 
13378b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
13381da177e4SLinus Torvalds 
133938308473SDavid S. Miller 	if (!rt) {
13401da177e4SLinus Torvalds 		err = -ENOMEM;
13411da177e4SLinus Torvalds 		goto out;
13421da177e4SLinus Torvalds 	}
13431da177e4SLinus Torvalds 
1344d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
13451716a961SGao feng 
13461716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
13471716a961SGao feng 		rt6_set_expires(rt, jiffies +
13481716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
13491716a961SGao feng 	else
13501716a961SGao feng 		rt6_clean_expires(rt);
13511da177e4SLinus Torvalds 
135286872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
135386872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
135486872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
135586872cb5SThomas Graf 
135686872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1359d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1360ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1361ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
13621da177e4SLinus Torvalds 	else
1363d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
13641da177e4SLinus Torvalds 
1365d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
13661da177e4SLinus Torvalds 
136786872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
136886872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
13691da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
137011d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
13711da177e4SLinus Torvalds 
13728e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
13738e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
13748e2ec639SYan, Zheng 		if (!metrics) {
13758e2ec639SYan, Zheng 			err = -ENOMEM;
13768e2ec639SYan, Zheng 			goto out;
13778e2ec639SYan, Zheng 		}
13788e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
13798e2ec639SYan, Zheng 	}
13801da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
138186872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
138286872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
13831da177e4SLinus Torvalds #endif
13841da177e4SLinus Torvalds 
138586872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
13881da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
13891da177e4SLinus Torvalds 	 */
139086872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
139138308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
139238308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
139338308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
13941da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
13955578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
13961da177e4SLinus Torvalds 			if (dev) {
13971da177e4SLinus Torvalds 				dev_put(dev);
13981da177e4SLinus Torvalds 				in6_dev_put(idev);
13991da177e4SLinus Torvalds 			}
14005578689aSDaniel Lezcano 			dev = net->loopback_dev;
14011da177e4SLinus Torvalds 			dev_hold(dev);
14021da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
14031da177e4SLinus Torvalds 			if (!idev) {
14041da177e4SLinus Torvalds 				err = -ENODEV;
14051da177e4SLinus Torvalds 				goto out;
14061da177e4SLinus Torvalds 			}
14071da177e4SLinus Torvalds 		}
1408d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1409d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
1410d8d1f30bSChangli Gao 		rt->dst.error = -ENETUNREACH;
14111da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
14121da177e4SLinus Torvalds 		goto install_route;
14131da177e4SLinus Torvalds 	}
14141da177e4SLinus Torvalds 
141586872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1416b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
14171da177e4SLinus Torvalds 		int gwa_type;
14181da177e4SLinus Torvalds 
141986872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
14204e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
14211da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
14241da177e4SLinus Torvalds 			struct rt6_info *grt;
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
14271da177e4SLinus Torvalds 			   addresses as nexthop address.
14281da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
14291da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
14301da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
14311da177e4SLinus Torvalds 			   some exceptions. --ANK
14321da177e4SLinus Torvalds 			 */
14331da177e4SLinus Torvalds 			err = -EINVAL;
14341da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
14351da177e4SLinus Torvalds 				goto out;
14361da177e4SLinus Torvalds 
14375578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
144038308473SDavid S. Miller 			if (!grt)
14411da177e4SLinus Torvalds 				goto out;
14421da177e4SLinus Torvalds 			if (dev) {
1443d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
1444d8d1f30bSChangli Gao 					dst_release(&grt->dst);
14451da177e4SLinus Torvalds 					goto out;
14461da177e4SLinus Torvalds 				}
14471da177e4SLinus Torvalds 			} else {
1448d1918542SDavid S. Miller 				dev = grt->dst.dev;
14491da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
14501da177e4SLinus Torvalds 				dev_hold(dev);
14511da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
14521da177e4SLinus Torvalds 			}
14531da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
14541da177e4SLinus Torvalds 				err = 0;
1455d8d1f30bSChangli Gao 			dst_release(&grt->dst);
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 			if (err)
14581da177e4SLinus Torvalds 				goto out;
14591da177e4SLinus Torvalds 		}
14601da177e4SLinus Torvalds 		err = -EINVAL;
146138308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
14621da177e4SLinus Torvalds 			goto out;
14631da177e4SLinus Torvalds 	}
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	err = -ENODEV;
146638308473SDavid S. Miller 	if (!dev)
14671da177e4SLinus Torvalds 		goto out;
14681da177e4SLinus Torvalds 
1469c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1470c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1471c3968a85SDaniel Walter 			err = -EINVAL;
1472c3968a85SDaniel Walter 			goto out;
1473c3968a85SDaniel Walter 		}
14744e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1475c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1476c3968a85SDaniel Walter 	} else
1477c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1478c3968a85SDaniel Walter 
147986872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
14808ade06c6SDavid S. Miller 		err = rt6_bind_neighbour(rt, dev);
1481f83c7790SDavid S. Miller 		if (err)
14821da177e4SLinus Torvalds 			goto out;
14831da177e4SLinus Torvalds 	}
14841da177e4SLinus Torvalds 
148586872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds install_route:
148886872cb5SThomas Graf 	if (cfg->fc_mx) {
148986872cb5SThomas Graf 		struct nlattr *nla;
149086872cb5SThomas Graf 		int remaining;
14911da177e4SLinus Torvalds 
149286872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
14938f4c1f9bSThomas Graf 			int type = nla_type(nla);
149486872cb5SThomas Graf 
149586872cb5SThomas Graf 			if (type) {
149686872cb5SThomas Graf 				if (type > RTAX_MAX) {
14971da177e4SLinus Torvalds 					err = -EINVAL;
14981da177e4SLinus Torvalds 					goto out;
14991da177e4SLinus Torvalds 				}
150086872cb5SThomas Graf 
1501defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
15021da177e4SLinus Torvalds 			}
15031da177e4SLinus Torvalds 		}
15041da177e4SLinus Torvalds 	}
15051da177e4SLinus Torvalds 
1506d8d1f30bSChangli Gao 	rt->dst.dev = dev;
15071da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1508c71099acSThomas Graf 	rt->rt6i_table = table;
150963152fc0SDaniel Lezcano 
1510c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
151163152fc0SDaniel Lezcano 
151286872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds out:
15151da177e4SLinus Torvalds 	if (dev)
15161da177e4SLinus Torvalds 		dev_put(dev);
15171da177e4SLinus Torvalds 	if (idev)
15181da177e4SLinus Torvalds 		in6_dev_put(idev);
15191da177e4SLinus Torvalds 	if (rt)
1520d8d1f30bSChangli Gao 		dst_free(&rt->dst);
15211da177e4SLinus Torvalds 	return err;
15221da177e4SLinus Torvalds }
15231da177e4SLinus Torvalds 
152486872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
15251da177e4SLinus Torvalds {
15261da177e4SLinus Torvalds 	int err;
1527c71099acSThomas Graf 	struct fib6_table *table;
1528d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
15291da177e4SLinus Torvalds 
15308ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry)
15316c813a72SPatrick McHardy 		return -ENOENT;
15326c813a72SPatrick McHardy 
1533c71099acSThomas Graf 	table = rt->rt6i_table;
1534c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
15351da177e4SLinus Torvalds 
153686872cb5SThomas Graf 	err = fib6_del(rt, info);
1537d8d1f30bSChangli Gao 	dst_release(&rt->dst);
15381da177e4SLinus Torvalds 
1539c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 	return err;
15421da177e4SLinus Torvalds }
15431da177e4SLinus Torvalds 
1544e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1545e0a1ad73SThomas Graf {
15464d1169c1SDenis V. Lunev 	struct nl_info info = {
1547d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
15484d1169c1SDenis V. Lunev 	};
1549528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1550e0a1ad73SThomas Graf }
1551e0a1ad73SThomas Graf 
155286872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
15531da177e4SLinus Torvalds {
1554c71099acSThomas Graf 	struct fib6_table *table;
15551da177e4SLinus Torvalds 	struct fib6_node *fn;
15561da177e4SLinus Torvalds 	struct rt6_info *rt;
15571da177e4SLinus Torvalds 	int err = -ESRCH;
15581da177e4SLinus Torvalds 
15595578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
156038308473SDavid S. Miller 	if (!table)
1561c71099acSThomas Graf 		return err;
15621da177e4SLinus Torvalds 
1563c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1564c71099acSThomas Graf 
1565c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
156686872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
156786872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	if (fn) {
1570d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
157186872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1572d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1573d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
15741da177e4SLinus Torvalds 				continue;
157586872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
157686872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
15771da177e4SLinus Torvalds 				continue;
157886872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
15791da177e4SLinus Torvalds 				continue;
1580d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1581c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
15821da177e4SLinus Torvalds 
158386872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
15841da177e4SLinus Torvalds 		}
15851da177e4SLinus Torvalds 	}
1586c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds 	return err;
15891da177e4SLinus Torvalds }
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds /*
15921da177e4SLinus Torvalds  *	Handle redirects
15931da177e4SLinus Torvalds  */
1594a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi {
15954c9483b2SDavid S. Miller 	struct flowi6 fl6;
1596a6279458SYOSHIFUJI Hideaki 	struct in6_addr gateway;
1597a6279458SYOSHIFUJI Hideaki };
15981da177e4SLinus Torvalds 
15998ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net,
16008ed67789SDaniel Lezcano 					     struct fib6_table *table,
16014c9483b2SDavid S. Miller 					     struct flowi6 *fl6,
1602a6279458SYOSHIFUJI Hideaki 					     int flags)
1603a6279458SYOSHIFUJI Hideaki {
16044c9483b2SDavid S. Miller 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1605a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt;
1606a6279458SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1607c71099acSThomas Graf 
1608e843b9e1SYOSHIFUJI Hideaki 	/*
1609e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1610e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1611e843b9e1SYOSHIFUJI Hideaki 	 *
1612e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1613e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1614e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1615e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1616e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1617e843b9e1SYOSHIFUJI Hideaki 	 */
16181da177e4SLinus Torvalds 
1619c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
16204c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1621e843b9e1SYOSHIFUJI Hideaki restart:
1622d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
16231da177e4SLinus Torvalds 		/*
16241da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
16251da177e4SLinus Torvalds 		 *
16261da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
16271da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
16281da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
16291da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
16301da177e4SLinus Torvalds 		 */
1631e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1632e843b9e1SYOSHIFUJI Hideaki 			continue;
16331da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1634e843b9e1SYOSHIFUJI Hideaki 			continue;
1635d1918542SDavid S. Miller 		if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1636e843b9e1SYOSHIFUJI Hideaki 			continue;
1637a6279458SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1638e843b9e1SYOSHIFUJI Hideaki 			continue;
1639e843b9e1SYOSHIFUJI Hideaki 		break;
1640e843b9e1SYOSHIFUJI Hideaki 	}
1641a6279458SYOSHIFUJI Hideaki 
1642cb15d9c2SYOSHIFUJI Hideaki 	if (!rt)
16438ed67789SDaniel Lezcano 		rt = net->ipv6.ip6_null_entry;
16444c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
1645cb15d9c2SYOSHIFUJI Hideaki out:
1646d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
1647a6279458SYOSHIFUJI Hideaki 
1648c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
16491da177e4SLinus Torvalds 
1650a6279458SYOSHIFUJI Hideaki 	return rt;
1651a6279458SYOSHIFUJI Hideaki };
1652a6279458SYOSHIFUJI Hideaki 
1653b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1654b71d1d42SEric Dumazet 					   const struct in6_addr *src,
1655b71d1d42SEric Dumazet 					   const struct in6_addr *gateway,
1656a6279458SYOSHIFUJI Hideaki 					   struct net_device *dev)
1657a6279458SYOSHIFUJI Hideaki {
1658adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1659c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
1660a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi rdfl = {
16614c9483b2SDavid S. Miller 		.fl6 = {
16624c9483b2SDavid S. Miller 			.flowi6_oif = dev->ifindex,
16634c9483b2SDavid S. Miller 			.daddr = *dest,
16644c9483b2SDavid S. Miller 			.saddr = *src,
1665a6279458SYOSHIFUJI Hideaki 		},
1666a6279458SYOSHIFUJI Hideaki 	};
1667adaa70bbSThomas Graf 
16684e3fd7a0SAlexey Dobriyan 	rdfl.gateway = *gateway;
166986c36ce4SBrian Haley 
1670adaa70bbSThomas Graf 	if (rt6_need_strict(dest))
1671adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
1672a6279458SYOSHIFUJI Hideaki 
16734c9483b2SDavid S. Miller 	return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
167458f09b78SDaniel Lezcano 						   flags, __ip6_route_redirect);
1675a6279458SYOSHIFUJI Hideaki }
1676a6279458SYOSHIFUJI Hideaki 
1677b71d1d42SEric Dumazet void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1678b71d1d42SEric Dumazet 		  const struct in6_addr *saddr,
1679a6279458SYOSHIFUJI Hideaki 		  struct neighbour *neigh, u8 *lladdr, int on_link)
1680a6279458SYOSHIFUJI Hideaki {
1681a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt = NULL;
1682a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1683c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(neigh->dev);
1684a6279458SYOSHIFUJI Hideaki 
1685a6279458SYOSHIFUJI Hideaki 	rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1686a6279458SYOSHIFUJI Hideaki 
16878ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry) {
1688e87cc472SJoe Perches 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
1689a6279458SYOSHIFUJI Hideaki 		goto out;
16901da177e4SLinus Torvalds 	}
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	/*
16931da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
16941da177e4SLinus Torvalds 	 */
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
16971da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
16981da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
16991da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
17001da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
17011da177e4SLinus Torvalds 		     );
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds 	/*
17041da177e4SLinus Torvalds 	 * Redirect received -> path was valid.
17051da177e4SLinus Torvalds 	 * Look, redirects are sent only in response to data packets,
17061da177e4SLinus Torvalds 	 * so that this nexthop apparently is reachable. --ANK
17071da177e4SLinus Torvalds 	 */
1708d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds 	/* Duplicate redirect: silently ignore. */
171127217455SDavid Miller 	if (neigh == dst_get_neighbour_noref_raw(&rt->dst))
17121da177e4SLinus Torvalds 		goto out;
17131da177e4SLinus Torvalds 
171421efcfa0SEric Dumazet 	nrt = ip6_rt_copy(rt, dest);
171538308473SDavid S. Miller 	if (!nrt)
17161da177e4SLinus Torvalds 		goto out;
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
17191da177e4SLinus Torvalds 	if (on_link)
17201da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
17211da177e4SLinus Torvalds 
17224e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
172369cce1d1SDavid S. Miller 	dst_set_neighbour(&nrt->dst, neigh_clone(neigh));
17241da177e4SLinus Torvalds 
172540e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
17261da177e4SLinus Torvalds 		goto out;
17271da177e4SLinus Torvalds 
1728d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1729d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
17308d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
17318d71740cSTom Tucker 
17321da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
1733e0a1ad73SThomas Graf 		ip6_del_rt(rt);
17341da177e4SLinus Torvalds 		return;
17351da177e4SLinus Torvalds 	}
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds out:
1738d8d1f30bSChangli Gao 	dst_release(&rt->dst);
17391da177e4SLinus Torvalds }
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds /*
17421da177e4SLinus Torvalds  *	Misc support functions
17431da177e4SLinus Torvalds  */
17441da177e4SLinus Torvalds 
17451716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
174621efcfa0SEric Dumazet 				    const struct in6_addr *dest)
17471da177e4SLinus Torvalds {
1748d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
17498b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
17508b96d22dSDavid S. Miller 					    ort->rt6i_table);
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	if (rt) {
1753d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1754d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
17558e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
17561da177e4SLinus Torvalds 
17574e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
17588e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1759defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1760d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
17611da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
17621da177e4SLinus Torvalds 		if (rt->rt6i_idev)
17631da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1764d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
17651da177e4SLinus Torvalds 
17664e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
17671716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
17681716a961SGao feng 		if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
17691716a961SGao feng 		    (RTF_DEFAULT | RTF_ADDRCONF))
17701716a961SGao feng 			rt6_set_from(rt, ort);
17711716a961SGao feng 		else
17721716a961SGao feng 			rt6_clean_expires(rt);
17731da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
17761da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
17771da177e4SLinus Torvalds #endif
17780f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1779c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
17801da177e4SLinus Torvalds 	}
17811da177e4SLinus Torvalds 	return rt;
17821da177e4SLinus Torvalds }
17831da177e4SLinus Torvalds 
178470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1785efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1786b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1787b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
178870ceb4f5SYOSHIFUJI Hideaki {
178970ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
179070ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1791c71099acSThomas Graf 	struct fib6_table *table;
179270ceb4f5SYOSHIFUJI Hideaki 
1793efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
179438308473SDavid S. Miller 	if (!table)
1795c71099acSThomas Graf 		return NULL;
1796c71099acSThomas Graf 
1797c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1798c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
179970ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
180070ceb4f5SYOSHIFUJI Hideaki 		goto out;
180170ceb4f5SYOSHIFUJI Hideaki 
1802d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1803d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
180470ceb4f5SYOSHIFUJI Hideaki 			continue;
180570ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
180670ceb4f5SYOSHIFUJI Hideaki 			continue;
180770ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
180870ceb4f5SYOSHIFUJI Hideaki 			continue;
1809d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
181070ceb4f5SYOSHIFUJI Hideaki 		break;
181170ceb4f5SYOSHIFUJI Hideaki 	}
181270ceb4f5SYOSHIFUJI Hideaki out:
1813c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
181470ceb4f5SYOSHIFUJI Hideaki 	return rt;
181570ceb4f5SYOSHIFUJI Hideaki }
181670ceb4f5SYOSHIFUJI Hideaki 
1817efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1818b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1819b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
182095c96174SEric Dumazet 					   unsigned int pref)
182170ceb4f5SYOSHIFUJI Hideaki {
182286872cb5SThomas Graf 	struct fib6_config cfg = {
182386872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1824238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
182586872cb5SThomas Graf 		.fc_ifindex	= ifindex,
182686872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
182786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
182886872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
1829efa2cea0SDaniel Lezcano 		.fc_nlinfo.pid = 0,
1830efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1831efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
183286872cb5SThomas Graf 	};
183370ceb4f5SYOSHIFUJI Hideaki 
18344e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
18354e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
183686872cb5SThomas Graf 
1837e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1838e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
183986872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
184070ceb4f5SYOSHIFUJI Hideaki 
184186872cb5SThomas Graf 	ip6_route_add(&cfg);
184270ceb4f5SYOSHIFUJI Hideaki 
1843efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
184470ceb4f5SYOSHIFUJI Hideaki }
184570ceb4f5SYOSHIFUJI Hideaki #endif
184670ceb4f5SYOSHIFUJI Hideaki 
1847b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
18481da177e4SLinus Torvalds {
18491da177e4SLinus Torvalds 	struct rt6_info *rt;
1850c71099acSThomas Graf 	struct fib6_table *table;
18511da177e4SLinus Torvalds 
1852c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
185338308473SDavid S. Miller 	if (!table)
1854c71099acSThomas Graf 		return NULL;
18551da177e4SLinus Torvalds 
1856c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1857d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1858d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
1859045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
18601da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
18611da177e4SLinus Torvalds 			break;
18621da177e4SLinus Torvalds 	}
18631da177e4SLinus Torvalds 	if (rt)
1864d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
1865c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
18661da177e4SLinus Torvalds 	return rt;
18671da177e4SLinus Torvalds }
18681da177e4SLinus Torvalds 
1869b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1870ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1871ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
18721da177e4SLinus Torvalds {
187386872cb5SThomas Graf 	struct fib6_config cfg = {
187486872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1875238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
187686872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
187786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
187886872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
18795578689aSDaniel Lezcano 		.fc_nlinfo.pid = 0,
18805578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1881c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
188286872cb5SThomas Graf 	};
18831da177e4SLinus Torvalds 
18844e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
18851da177e4SLinus Torvalds 
188686872cb5SThomas Graf 	ip6_route_add(&cfg);
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
18891da177e4SLinus Torvalds }
18901da177e4SLinus Torvalds 
18917b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
18921da177e4SLinus Torvalds {
18931da177e4SLinus Torvalds 	struct rt6_info *rt;
1894c71099acSThomas Graf 	struct fib6_table *table;
1895c71099acSThomas Graf 
1896c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
18977b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
189838308473SDavid S. Miller 	if (!table)
1899c71099acSThomas Graf 		return;
19001da177e4SLinus Torvalds 
19011da177e4SLinus Torvalds restart:
1902c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1903d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
19041da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1905d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1906c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1907e0a1ad73SThomas Graf 			ip6_del_rt(rt);
19081da177e4SLinus Torvalds 			goto restart;
19091da177e4SLinus Torvalds 		}
19101da177e4SLinus Torvalds 	}
1911c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
19121da177e4SLinus Torvalds }
19131da177e4SLinus Torvalds 
19145578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
19155578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
191686872cb5SThomas Graf 				 struct fib6_config *cfg)
191786872cb5SThomas Graf {
191886872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
191986872cb5SThomas Graf 
192086872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
192186872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
192286872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
192386872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
192486872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
192586872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
192686872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
192786872cb5SThomas Graf 
19285578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1929f1243c2dSBenjamin Thery 
19304e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
19314e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
19324e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
193386872cb5SThomas Graf }
193486872cb5SThomas Graf 
19355578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
19361da177e4SLinus Torvalds {
193786872cb5SThomas Graf 	struct fib6_config cfg;
19381da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
19391da177e4SLinus Torvalds 	int err;
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds 	switch(cmd) {
19421da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
19431da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
19441da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
19451da177e4SLinus Torvalds 			return -EPERM;
19461da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
19471da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
19481da177e4SLinus Torvalds 		if (err)
19491da177e4SLinus Torvalds 			return -EFAULT;
19501da177e4SLinus Torvalds 
19515578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
195286872cb5SThomas Graf 
19531da177e4SLinus Torvalds 		rtnl_lock();
19541da177e4SLinus Torvalds 		switch (cmd) {
19551da177e4SLinus Torvalds 		case SIOCADDRT:
195686872cb5SThomas Graf 			err = ip6_route_add(&cfg);
19571da177e4SLinus Torvalds 			break;
19581da177e4SLinus Torvalds 		case SIOCDELRT:
195986872cb5SThomas Graf 			err = ip6_route_del(&cfg);
19601da177e4SLinus Torvalds 			break;
19611da177e4SLinus Torvalds 		default:
19621da177e4SLinus Torvalds 			err = -EINVAL;
19631da177e4SLinus Torvalds 		}
19641da177e4SLinus Torvalds 		rtnl_unlock();
19651da177e4SLinus Torvalds 
19661da177e4SLinus Torvalds 		return err;
19673ff50b79SStephen Hemminger 	}
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds 	return -EINVAL;
19701da177e4SLinus Torvalds }
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds /*
19731da177e4SLinus Torvalds  *	Drop the packet on the floor
19741da177e4SLinus Torvalds  */
19751da177e4SLinus Torvalds 
1976d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
19771da177e4SLinus Torvalds {
1978612f09e8SYOSHIFUJI Hideaki 	int type;
1979adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
1980612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
1981612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
19820660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
198345bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
19843bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
19853bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
1986612f09e8SYOSHIFUJI Hideaki 			break;
1987612f09e8SYOSHIFUJI Hideaki 		}
1988612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
1989612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
19903bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
19913bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
1992612f09e8SYOSHIFUJI Hideaki 		break;
1993612f09e8SYOSHIFUJI Hideaki 	}
19943ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
19951da177e4SLinus Torvalds 	kfree_skb(skb);
19961da177e4SLinus Torvalds 	return 0;
19971da177e4SLinus Torvalds }
19981da177e4SLinus Torvalds 
19999ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
20009ce8ade0SThomas Graf {
2001612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
20029ce8ade0SThomas Graf }
20039ce8ade0SThomas Graf 
200420380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
20051da177e4SLinus Torvalds {
2006adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2007612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
20081da177e4SLinus Torvalds }
20091da177e4SLinus Torvalds 
20106723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
20116723ab54SDavid S. Miller 
20129ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
20139ce8ade0SThomas Graf {
2014612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
20159ce8ade0SThomas Graf }
20169ce8ade0SThomas Graf 
20179ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
20189ce8ade0SThomas Graf {
2019adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2020612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
20219ce8ade0SThomas Graf }
20229ce8ade0SThomas Graf 
20236723ab54SDavid S. Miller #endif
20246723ab54SDavid S. Miller 
20251da177e4SLinus Torvalds /*
20261da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
20271da177e4SLinus Torvalds  */
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
20301da177e4SLinus Torvalds 				    const struct in6_addr *addr,
20318f031519SDavid S. Miller 				    bool anycast)
20321da177e4SLinus Torvalds {
2033c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
20348b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
2035f83c7790SDavid S. Miller 	int err;
20361da177e4SLinus Torvalds 
203738308473SDavid S. Miller 	if (!rt) {
2038f3213831SJoe Perches 		net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
20391da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
204040385653SBen Greear 	}
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 	in6_dev_hold(idev);
20431da177e4SLinus Torvalds 
204411d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2045d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2046d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
20471da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2048d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
205158c4fb86SYOSHIFUJI Hideaki 	if (anycast)
205258c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
205358c4fb86SYOSHIFUJI Hideaki 	else
20541da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
20558ade06c6SDavid S. Miller 	err = rt6_bind_neighbour(rt, rt->dst.dev);
2056f83c7790SDavid S. Miller 	if (err) {
2057d8d1f30bSChangli Gao 		dst_free(&rt->dst);
2058f83c7790SDavid S. Miller 		return ERR_PTR(err);
20591da177e4SLinus Torvalds 	}
20601da177e4SLinus Torvalds 
20614e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
20621da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
20635578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
20641da177e4SLinus Torvalds 
2065d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds 	return rt;
20681da177e4SLinus Torvalds }
20691da177e4SLinus Torvalds 
2070c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2071c3968a85SDaniel Walter 			struct rt6_info *rt,
2072b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2073c3968a85SDaniel Walter 			unsigned int prefs,
2074c3968a85SDaniel Walter 			struct in6_addr *saddr)
2075c3968a85SDaniel Walter {
2076c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2077c3968a85SDaniel Walter 	int err = 0;
2078c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
20794e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2080c3968a85SDaniel Walter 	else
2081c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2082c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2083c3968a85SDaniel Walter 	return err;
2084c3968a85SDaniel Walter }
2085c3968a85SDaniel Walter 
2086c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2087c3968a85SDaniel Walter struct arg_dev_net_ip {
2088c3968a85SDaniel Walter 	struct net_device *dev;
2089c3968a85SDaniel Walter 	struct net *net;
2090c3968a85SDaniel Walter 	struct in6_addr *addr;
2091c3968a85SDaniel Walter };
2092c3968a85SDaniel Walter 
2093c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2094c3968a85SDaniel Walter {
2095c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2096c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2097c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2098c3968a85SDaniel Walter 
2099d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2100c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2101c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2102c3968a85SDaniel Walter 		/* remove prefsrc entry */
2103c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2104c3968a85SDaniel Walter 	}
2105c3968a85SDaniel Walter 	return 0;
2106c3968a85SDaniel Walter }
2107c3968a85SDaniel Walter 
2108c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2109c3968a85SDaniel Walter {
2110c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2111c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2112c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2113c3968a85SDaniel Walter 		.net = net,
2114c3968a85SDaniel Walter 		.addr = &ifp->addr,
2115c3968a85SDaniel Walter 	};
2116c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2117c3968a85SDaniel Walter }
2118c3968a85SDaniel Walter 
21198ed67789SDaniel Lezcano struct arg_dev_net {
21208ed67789SDaniel Lezcano 	struct net_device *dev;
21218ed67789SDaniel Lezcano 	struct net *net;
21228ed67789SDaniel Lezcano };
21238ed67789SDaniel Lezcano 
21241da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
21251da177e4SLinus Torvalds {
2126bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2127bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
21288ed67789SDaniel Lezcano 
2129d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2130c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
21311da177e4SLinus Torvalds 		return -1;
2132c159d30cSDavid S. Miller 
21331da177e4SLinus Torvalds 	return 0;
21341da177e4SLinus Torvalds }
21351da177e4SLinus Torvalds 
2136f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
21371da177e4SLinus Torvalds {
21388ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
21398ed67789SDaniel Lezcano 		.dev = dev,
21408ed67789SDaniel Lezcano 		.net = net,
21418ed67789SDaniel Lezcano 	};
21428ed67789SDaniel Lezcano 
21438ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
21441e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
21451da177e4SLinus Torvalds }
21461da177e4SLinus Torvalds 
214795c96174SEric Dumazet struct rt6_mtu_change_arg {
21481da177e4SLinus Torvalds 	struct net_device *dev;
214995c96174SEric Dumazet 	unsigned int mtu;
21501da177e4SLinus Torvalds };
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
21531da177e4SLinus Torvalds {
21541da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
21551da177e4SLinus Torvalds 	struct inet6_dev *idev;
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
21581da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
21591da177e4SLinus Torvalds 	   We still use this lock to block changes
21601da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
21611da177e4SLinus Torvalds 	*/
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
216438308473SDavid S. Miller 	if (!idev)
21651da177e4SLinus Torvalds 		return 0;
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
21681da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
21691da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
21701da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
21711da177e4SLinus Torvalds 	 */
21721da177e4SLinus Torvalds 	/*
21731da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
21741da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
21751da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
21761da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
21771da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
21781da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
21791da177e4SLinus Torvalds 	   PMTU discouvery.
21801da177e4SLinus Torvalds 	 */
2181d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2182d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2183d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2184d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2185d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2186defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2187566cfd8fSSimon Arlott 	}
21881da177e4SLinus Torvalds 	return 0;
21891da177e4SLinus Torvalds }
21901da177e4SLinus Torvalds 
219195c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
21921da177e4SLinus Torvalds {
2193c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2194c71099acSThomas Graf 		.dev = dev,
2195c71099acSThomas Graf 		.mtu = mtu,
2196c71099acSThomas Graf 	};
21971da177e4SLinus Torvalds 
2198c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
21991da177e4SLinus Torvalds }
22001da177e4SLinus Torvalds 
2201ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
22025176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
220386872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2204ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
220586872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
220686872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
220786872cb5SThomas Graf };
220886872cb5SThomas Graf 
220986872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
221086872cb5SThomas Graf 			      struct fib6_config *cfg)
22111da177e4SLinus Torvalds {
221286872cb5SThomas Graf 	struct rtmsg *rtm;
221386872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
221486872cb5SThomas Graf 	int err;
22151da177e4SLinus Torvalds 
221686872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
221786872cb5SThomas Graf 	if (err < 0)
221886872cb5SThomas Graf 		goto errout;
22191da177e4SLinus Torvalds 
222086872cb5SThomas Graf 	err = -EINVAL;
222186872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
222286872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
222386872cb5SThomas Graf 
222486872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
222586872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
222686872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
222786872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
222886872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
222986872cb5SThomas Graf 
223086872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
223186872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
223286872cb5SThomas Graf 
2233ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2234ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2235ab79ad14SMaciej Żenczykowski 
223686872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
223786872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
22383b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
223986872cb5SThomas Graf 
224086872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
224186872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
224286872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
22431da177e4SLinus Torvalds 	}
224486872cb5SThomas Graf 
224586872cb5SThomas Graf 	if (tb[RTA_DST]) {
224686872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
224786872cb5SThomas Graf 
224886872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
224986872cb5SThomas Graf 			goto errout;
225086872cb5SThomas Graf 
225186872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
22521da177e4SLinus Torvalds 	}
225386872cb5SThomas Graf 
225486872cb5SThomas Graf 	if (tb[RTA_SRC]) {
225586872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
225686872cb5SThomas Graf 
225786872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
225886872cb5SThomas Graf 			goto errout;
225986872cb5SThomas Graf 
226086872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
22611da177e4SLinus Torvalds 	}
226286872cb5SThomas Graf 
2263c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2264c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2265c3968a85SDaniel Walter 
226686872cb5SThomas Graf 	if (tb[RTA_OIF])
226786872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
226886872cb5SThomas Graf 
226986872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
227086872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
227186872cb5SThomas Graf 
227286872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
227386872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
227486872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
22751da177e4SLinus Torvalds 	}
227686872cb5SThomas Graf 
227786872cb5SThomas Graf 	if (tb[RTA_TABLE])
227886872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
227986872cb5SThomas Graf 
228086872cb5SThomas Graf 	err = 0;
228186872cb5SThomas Graf errout:
228286872cb5SThomas Graf 	return err;
22831da177e4SLinus Torvalds }
22841da177e4SLinus Torvalds 
2285c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
22861da177e4SLinus Torvalds {
228786872cb5SThomas Graf 	struct fib6_config cfg;
228886872cb5SThomas Graf 	int err;
22891da177e4SLinus Torvalds 
229086872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
229186872cb5SThomas Graf 	if (err < 0)
229286872cb5SThomas Graf 		return err;
229386872cb5SThomas Graf 
229486872cb5SThomas Graf 	return ip6_route_del(&cfg);
22951da177e4SLinus Torvalds }
22961da177e4SLinus Torvalds 
2297c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
22981da177e4SLinus Torvalds {
229986872cb5SThomas Graf 	struct fib6_config cfg;
230086872cb5SThomas Graf 	int err;
23011da177e4SLinus Torvalds 
230286872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
230386872cb5SThomas Graf 	if (err < 0)
230486872cb5SThomas Graf 		return err;
230586872cb5SThomas Graf 
230686872cb5SThomas Graf 	return ip6_route_add(&cfg);
23071da177e4SLinus Torvalds }
23081da177e4SLinus Torvalds 
2309339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2310339bf98fSThomas Graf {
2311339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2312339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2313339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2314339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2315339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2316339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2317339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2318339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2319339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
23206a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2321339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2322339bf98fSThomas Graf }
2323339bf98fSThomas Graf 
2324191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2325191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
23260d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
23270d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
23287bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
23291da177e4SLinus Torvalds {
2330346f870bSDavid S. Miller 	const struct inet_peer *peer;
23311da177e4SLinus Torvalds 	struct rtmsg *rtm;
23321da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2333e3703b3dSThomas Graf 	long expires;
23349e762a4aSPatrick McHardy 	u32 table;
2335f2c31e32SEric Dumazet 	struct neighbour *n;
2336346f870bSDavid S. Miller 	u32 ts, tsage;
23371da177e4SLinus Torvalds 
23381da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
23391da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
23401da177e4SLinus Torvalds 			/* success since this is not a prefix route */
23411da177e4SLinus Torvalds 			return 1;
23421da177e4SLinus Torvalds 		}
23431da177e4SLinus Torvalds 	}
23441da177e4SLinus Torvalds 
23452d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
234638308473SDavid S. Miller 	if (!nlh)
234726932566SPatrick McHardy 		return -EMSGSIZE;
23482d7202bfSThomas Graf 
23492d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
23501da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
23511da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
23521da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
23531da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2354c71099acSThomas Graf 	if (rt->rt6i_table)
23559e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2356c71099acSThomas Graf 	else
23579e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
23589e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2359c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2360c78679e8SDavid S. Miller 		goto nla_put_failure;
23611da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_REJECT)
23621da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
2363ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2364ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2365d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
23661da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
23671da177e4SLinus Torvalds 	else
23681da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
23691da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
23701da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
23711da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
23721da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
23731da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
23741da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
23751da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
23761da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_DEFAULT)
23771da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
23801da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
23811da177e4SLinus Torvalds 
23821da177e4SLinus Torvalds 	if (dst) {
2383c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2384c78679e8SDavid S. Miller 			goto nla_put_failure;
23851da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
23861da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2387c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2388c78679e8SDavid S. Miller 			goto nla_put_failure;
23891da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
23901da177e4SLinus Torvalds 	if (src) {
2391c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2392c78679e8SDavid S. Miller 			goto nla_put_failure;
23931da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2394c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2395c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2396c78679e8SDavid S. Miller 		goto nla_put_failure;
23971da177e4SLinus Torvalds #endif
23987bc570c8SYOSHIFUJI Hideaki 	if (iif) {
23997bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
24007bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
24018229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
24027bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
24037bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
24047bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
24057bc570c8SYOSHIFUJI Hideaki 						return 0;
24067bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
24077bc570c8SYOSHIFUJI Hideaki 				} else {
24087bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
24097bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
24107bc570c8SYOSHIFUJI Hideaki 				}
24117bc570c8SYOSHIFUJI Hideaki 			}
24127bc570c8SYOSHIFUJI Hideaki 		} else
24137bc570c8SYOSHIFUJI Hideaki #endif
2414c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2415c78679e8SDavid S. Miller 				goto nla_put_failure;
24167bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
24171da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2418c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2419c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2420c78679e8SDavid S. Miller 			goto nla_put_failure;
2421c3968a85SDaniel Walter 	}
2422c3968a85SDaniel Walter 
2423c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2424c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
24254e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2426c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2427c78679e8SDavid S. Miller 			goto nla_put_failure;
24281da177e4SLinus Torvalds 	}
24292d7202bfSThomas Graf 
2430defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
24312d7202bfSThomas Graf 		goto nla_put_failure;
24322d7202bfSThomas Graf 
2433f2c31e32SEric Dumazet 	rcu_read_lock();
243427217455SDavid Miller 	n = dst_get_neighbour_noref(&rt->dst);
243594f826b8SEric Dumazet 	if (n) {
243694f826b8SEric Dumazet 		if (nla_put(skb, RTA_GATEWAY, 16, &n->primary_key) < 0) {
243794f826b8SEric Dumazet 			rcu_read_unlock();
243894f826b8SEric Dumazet 			goto nla_put_failure;
243994f826b8SEric Dumazet 		}
244094f826b8SEric Dumazet 	}
2441f2c31e32SEric Dumazet 	rcu_read_unlock();
24422d7202bfSThomas Graf 
2443c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2444c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2445c78679e8SDavid S. Miller 		goto nla_put_failure;
2446c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2447c78679e8SDavid S. Miller 		goto nla_put_failure;
244836e3deaeSYOSHIFUJI Hideaki 	if (!(rt->rt6i_flags & RTF_EXPIRES))
244936e3deaeSYOSHIFUJI Hideaki 		expires = 0;
2450d1918542SDavid S. Miller 	else if (rt->dst.expires - jiffies < INT_MAX)
2451d1918542SDavid S. Miller 		expires = rt->dst.expires - jiffies;
245236e3deaeSYOSHIFUJI Hideaki 	else
245336e3deaeSYOSHIFUJI Hideaki 		expires = INT_MAX;
245469cdf8f9SYOSHIFUJI Hideaki 
245597bab73fSDavid S. Miller 	peer = NULL;
245697bab73fSDavid S. Miller 	if (rt6_has_peer(rt))
245797bab73fSDavid S. Miller 		peer = rt6_peer_ptr(rt);
2458346f870bSDavid S. Miller 	ts = tsage = 0;
2459346f870bSDavid S. Miller 	if (peer && peer->tcp_ts_stamp) {
2460346f870bSDavid S. Miller 		ts = peer->tcp_ts;
2461346f870bSDavid S. Miller 		tsage = get_seconds() - peer->tcp_ts_stamp;
2462346f870bSDavid S. Miller 	}
2463346f870bSDavid S. Miller 
2464346f870bSDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, ts, tsage,
2465d8d1f30bSChangli Gao 			       expires, rt->dst.error) < 0)
2466e3703b3dSThomas Graf 		goto nla_put_failure;
24671da177e4SLinus Torvalds 
24682d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
24692d7202bfSThomas Graf 
24702d7202bfSThomas Graf nla_put_failure:
247126932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
247226932566SPatrick McHardy 	return -EMSGSIZE;
24731da177e4SLinus Torvalds }
24741da177e4SLinus Torvalds 
24751b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
24761da177e4SLinus Torvalds {
24771da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
24781da177e4SLinus Torvalds 	int prefix;
24791da177e4SLinus Torvalds 
24802d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
24812d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
24821da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
24831da177e4SLinus Torvalds 	} else
24841da177e4SLinus Torvalds 		prefix = 0;
24851da177e4SLinus Torvalds 
2486191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2487191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
24881da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
24897bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
24901da177e4SLinus Torvalds }
24911da177e4SLinus Torvalds 
2492c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
24931da177e4SLinus Torvalds {
24943b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2495ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
24961da177e4SLinus Torvalds 	struct rt6_info *rt;
2497ab364a6fSThomas Graf 	struct sk_buff *skb;
2498ab364a6fSThomas Graf 	struct rtmsg *rtm;
24994c9483b2SDavid S. Miller 	struct flowi6 fl6;
250072331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2501ab364a6fSThomas Graf 
2502ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2503ab364a6fSThomas Graf 	if (err < 0)
2504ab364a6fSThomas Graf 		goto errout;
2505ab364a6fSThomas Graf 
2506ab364a6fSThomas Graf 	err = -EINVAL;
25074c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2508ab364a6fSThomas Graf 
2509ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2510ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2511ab364a6fSThomas Graf 			goto errout;
2512ab364a6fSThomas Graf 
25134e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2514ab364a6fSThomas Graf 	}
2515ab364a6fSThomas Graf 
2516ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2517ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2518ab364a6fSThomas Graf 			goto errout;
2519ab364a6fSThomas Graf 
25204e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2521ab364a6fSThomas Graf 	}
2522ab364a6fSThomas Graf 
2523ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2524ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2525ab364a6fSThomas Graf 
2526ab364a6fSThomas Graf 	if (tb[RTA_OIF])
252772331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2528ab364a6fSThomas Graf 
2529ab364a6fSThomas Graf 	if (iif) {
2530ab364a6fSThomas Graf 		struct net_device *dev;
253172331bc0SShmulik Ladkani 		int flags = 0;
253272331bc0SShmulik Ladkani 
25335578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2534ab364a6fSThomas Graf 		if (!dev) {
2535ab364a6fSThomas Graf 			err = -ENODEV;
2536ab364a6fSThomas Graf 			goto errout;
2537ab364a6fSThomas Graf 		}
253872331bc0SShmulik Ladkani 
253972331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
254072331bc0SShmulik Ladkani 
254172331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
254272331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
254372331bc0SShmulik Ladkani 
254472331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
254572331bc0SShmulik Ladkani 							       flags);
254672331bc0SShmulik Ladkani 	} else {
254772331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
254872331bc0SShmulik Ladkani 
254972331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2550ab364a6fSThomas Graf 	}
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
255338308473SDavid S. Miller 	if (!skb) {
25542173bff5SShmulik Ladkani 		dst_release(&rt->dst);
2555ab364a6fSThomas Graf 		err = -ENOBUFS;
2556ab364a6fSThomas Graf 		goto errout;
2557ab364a6fSThomas Graf 	}
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
25601da177e4SLinus Torvalds 	   through good chunk of routing engine.
25611da177e4SLinus Torvalds 	 */
2562459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
25631da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
25641da177e4SLinus Torvalds 
2565d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
25661da177e4SLinus Torvalds 
25674c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
25681da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
25697bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
25701da177e4SLinus Torvalds 	if (err < 0) {
2571ab364a6fSThomas Graf 		kfree_skb(skb);
2572ab364a6fSThomas Graf 		goto errout;
25731da177e4SLinus Torvalds 	}
25741da177e4SLinus Torvalds 
25755578689aSDaniel Lezcano 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2576ab364a6fSThomas Graf errout:
25771da177e4SLinus Torvalds 	return err;
25781da177e4SLinus Torvalds }
25791da177e4SLinus Torvalds 
258086872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
25811da177e4SLinus Torvalds {
25821da177e4SLinus Torvalds 	struct sk_buff *skb;
25835578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2584528c4cebSDenis V. Lunev 	u32 seq;
2585528c4cebSDenis V. Lunev 	int err;
25860d51aa80SJamal Hadi Salim 
2587528c4cebSDenis V. Lunev 	err = -ENOBUFS;
258838308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
258986872cb5SThomas Graf 
2590339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
259138308473SDavid S. Miller 	if (!skb)
259221713ebcSThomas Graf 		goto errout;
25931da177e4SLinus Torvalds 
2594191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
25957bc570c8SYOSHIFUJI Hideaki 				event, info->pid, seq, 0, 0, 0);
259626932566SPatrick McHardy 	if (err < 0) {
259726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
259826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
259926932566SPatrick McHardy 		kfree_skb(skb);
260026932566SPatrick McHardy 		goto errout;
260126932566SPatrick McHardy 	}
26021ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
26035578689aSDaniel Lezcano 		    info->nlh, gfp_any());
26041ce85fe4SPablo Neira Ayuso 	return;
260521713ebcSThomas Graf errout:
260621713ebcSThomas Graf 	if (err < 0)
26075578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
26081da177e4SLinus Torvalds }
26091da177e4SLinus Torvalds 
26108ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
26118ed67789SDaniel Lezcano 				unsigned long event, void *data)
26128ed67789SDaniel Lezcano {
26138ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2614c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26158ed67789SDaniel Lezcano 
26168ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2617d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
26188ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
26198ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2620d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
26218ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2622d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
26238ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
26248ed67789SDaniel Lezcano #endif
26258ed67789SDaniel Lezcano 	}
26268ed67789SDaniel Lezcano 
26278ed67789SDaniel Lezcano 	return NOTIFY_OK;
26288ed67789SDaniel Lezcano }
26298ed67789SDaniel Lezcano 
26301da177e4SLinus Torvalds /*
26311da177e4SLinus Torvalds  *	/proc
26321da177e4SLinus Torvalds  */
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
26351da177e4SLinus Torvalds 
26361da177e4SLinus Torvalds struct rt6_proc_arg
26371da177e4SLinus Torvalds {
26381da177e4SLinus Torvalds 	char *buffer;
26391da177e4SLinus Torvalds 	int offset;
26401da177e4SLinus Torvalds 	int length;
26411da177e4SLinus Torvalds 	int skip;
26421da177e4SLinus Torvalds 	int len;
26431da177e4SLinus Torvalds };
26441da177e4SLinus Torvalds 
26451da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
26461da177e4SLinus Torvalds {
264733120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
264869cce1d1SDavid S. Miller 	struct neighbour *n;
26491da177e4SLinus Torvalds 
26504b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
26534b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
26541da177e4SLinus Torvalds #else
265533120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
26561da177e4SLinus Torvalds #endif
2657f2c31e32SEric Dumazet 	rcu_read_lock();
265827217455SDavid Miller 	n = dst_get_neighbour_noref(&rt->dst);
265969cce1d1SDavid S. Miller 	if (n) {
266069cce1d1SDavid S. Miller 		seq_printf(m, "%pi6", n->primary_key);
26611da177e4SLinus Torvalds 	} else {
266233120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
26631da177e4SLinus Torvalds 	}
2664f2c31e32SEric Dumazet 	rcu_read_unlock();
266533120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2666d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2667d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
2668d1918542SDavid S. Miller 		   rt->dst.dev ? rt->dst.dev->name : "");
26691da177e4SLinus Torvalds 	return 0;
26701da177e4SLinus Torvalds }
26711da177e4SLinus Torvalds 
267233120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
26731da177e4SLinus Torvalds {
2674f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
267532b293a5SJosh Hunt 	fib6_clean_all_ro(net, rt6_info_route, 0, m);
267633120b30SAlexey Dobriyan 	return 0;
26771da177e4SLinus Torvalds }
26781da177e4SLinus Torvalds 
267933120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
268033120b30SAlexey Dobriyan {
2681de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2682f3db4851SDaniel Lezcano }
2683f3db4851SDaniel Lezcano 
268433120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
268533120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
268633120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
268733120b30SAlexey Dobriyan 	.read		= seq_read,
268833120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2689b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
269033120b30SAlexey Dobriyan };
269133120b30SAlexey Dobriyan 
26921da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
26931da177e4SLinus Torvalds {
269469ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
26951da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
269669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
269769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
269869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
269969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
270069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2701fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
270269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds 	return 0;
27051da177e4SLinus Torvalds }
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
27081da177e4SLinus Torvalds {
2709de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
271069ddb805SDaniel Lezcano }
271169ddb805SDaniel Lezcano 
27129a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
27131da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
27141da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
27151da177e4SLinus Torvalds 	.read	 = seq_read,
27161da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2717b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
27181da177e4SLinus Torvalds };
27191da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
27201da177e4SLinus Torvalds 
27211da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds static
27248d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
27251da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
27261da177e4SLinus Torvalds {
2727c486da34SLucian Adrian Grijincu 	struct net *net;
2728c486da34SLucian Adrian Grijincu 	int delay;
2729c486da34SLucian Adrian Grijincu 	if (!write)
2730c486da34SLucian Adrian Grijincu 		return -EINVAL;
2731c486da34SLucian Adrian Grijincu 
2732c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2733c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
27348d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
27355b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
27361da177e4SLinus Torvalds 	return 0;
27371da177e4SLinus Torvalds }
27381da177e4SLinus Torvalds 
2739760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
27401da177e4SLinus Torvalds 	{
27411da177e4SLinus Torvalds 		.procname	=	"flush",
27424990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
27431da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
274489c8b3a1SDave Jones 		.mode		=	0200,
27456d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
27461da177e4SLinus Torvalds 	},
27471da177e4SLinus Torvalds 	{
27481da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
27499a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
27501da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27511da177e4SLinus Torvalds 		.mode		=	0644,
27526d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27531da177e4SLinus Torvalds 	},
27541da177e4SLinus Torvalds 	{
27551da177e4SLinus Torvalds 		.procname	=	"max_size",
27564990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
27571da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27581da177e4SLinus Torvalds 		.mode		=	0644,
27596d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27601da177e4SLinus Torvalds 	},
27611da177e4SLinus Torvalds 	{
27621da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
27634990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
27641da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27651da177e4SLinus Torvalds 		.mode		=	0644,
27666d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27671da177e4SLinus Torvalds 	},
27681da177e4SLinus Torvalds 	{
27691da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
27704990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
27711da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27721da177e4SLinus Torvalds 		.mode		=	0644,
27736d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27741da177e4SLinus Torvalds 	},
27751da177e4SLinus Torvalds 	{
27761da177e4SLinus Torvalds 		.procname	=	"gc_interval",
27774990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
27781da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27791da177e4SLinus Torvalds 		.mode		=	0644,
27806d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27811da177e4SLinus Torvalds 	},
27821da177e4SLinus Torvalds 	{
27831da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
27844990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
27851da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27861da177e4SLinus Torvalds 		.mode		=	0644,
2787f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
27881da177e4SLinus Torvalds 	},
27891da177e4SLinus Torvalds 	{
27901da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
27914990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
27921da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27931da177e4SLinus Torvalds 		.mode		=	0644,
27946d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27951da177e4SLinus Torvalds 	},
27961da177e4SLinus Torvalds 	{
27971da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
27984990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
27991da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28001da177e4SLinus Torvalds 		.mode		=	0644,
2801f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28021da177e4SLinus Torvalds 	},
28031da177e4SLinus Torvalds 	{
28041da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
28054990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28061da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28071da177e4SLinus Torvalds 		.mode		=	0644,
28086d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
28091da177e4SLinus Torvalds 	},
2810f8572d8fSEric W. Biederman 	{ }
28111da177e4SLinus Torvalds };
28121da177e4SLinus Torvalds 
28132c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2814760f2d01SDaniel Lezcano {
2815760f2d01SDaniel Lezcano 	struct ctl_table *table;
2816760f2d01SDaniel Lezcano 
2817760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2818760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2819760f2d01SDaniel Lezcano 			GFP_KERNEL);
28205ee09105SYOSHIFUJI Hideaki 
28215ee09105SYOSHIFUJI Hideaki 	if (table) {
28225ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2823c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
282486393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
28255ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
28265ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28275ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
28285ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
28295ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
28305ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
28315ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
28329c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28335ee09105SYOSHIFUJI Hideaki 	}
28345ee09105SYOSHIFUJI Hideaki 
2835760f2d01SDaniel Lezcano 	return table;
2836760f2d01SDaniel Lezcano }
28371da177e4SLinus Torvalds #endif
28381da177e4SLinus Torvalds 
28392c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2840cdb18761SDaniel Lezcano {
2841633d424bSPavel Emelyanov 	int ret = -ENOMEM;
28428ed67789SDaniel Lezcano 
284386393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
284486393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2845f2fc6a54SBenjamin Thery 
2846fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2847fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2848fc66f95cSEric Dumazet 
28498ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
28508ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
28518ed67789SDaniel Lezcano 					   GFP_KERNEL);
28528ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2853fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2854d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
28558ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2856d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
285762fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
285862fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28598ed67789SDaniel Lezcano 
28608ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
28618ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
28628ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
28638ed67789SDaniel Lezcano 					       GFP_KERNEL);
286468fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
286568fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2866d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
28678ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2868d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
286962fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
287062fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28718ed67789SDaniel Lezcano 
28728ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
28738ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
28748ed67789SDaniel Lezcano 					       GFP_KERNEL);
287568fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
287668fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2877d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
28788ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2879d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
288062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
288162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28828ed67789SDaniel Lezcano #endif
28838ed67789SDaniel Lezcano 
2884b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2885b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2886b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2887b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2888b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2889b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2890b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2891b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2892b339a47cSPeter Zijlstra 
28936891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
28946891a346SBenjamin Thery 
28958ed67789SDaniel Lezcano 	ret = 0;
28968ed67789SDaniel Lezcano out:
28978ed67789SDaniel Lezcano 	return ret;
2898f2fc6a54SBenjamin Thery 
289968fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
290068fffc67SPeter Zijlstra out_ip6_prohibit_entry:
290168fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
290268fffc67SPeter Zijlstra out_ip6_null_entry:
290368fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
290468fffc67SPeter Zijlstra #endif
2905fc66f95cSEric Dumazet out_ip6_dst_entries:
2906fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2907f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2908f2fc6a54SBenjamin Thery 	goto out;
2909cdb18761SDaniel Lezcano }
2910cdb18761SDaniel Lezcano 
29112c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2912cdb18761SDaniel Lezcano {
29138ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
29148ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29158ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
29168ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
29178ed67789SDaniel Lezcano #endif
291841bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2919cdb18761SDaniel Lezcano }
2920cdb18761SDaniel Lezcano 
2921d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
2922d189634eSThomas Graf {
2923d189634eSThomas Graf #ifdef CONFIG_PROC_FS
2924d189634eSThomas Graf 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2925d189634eSThomas Graf 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2926d189634eSThomas Graf #endif
2927d189634eSThomas Graf 	return 0;
2928d189634eSThomas Graf }
2929d189634eSThomas Graf 
2930d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
2931d189634eSThomas Graf {
2932d189634eSThomas Graf #ifdef CONFIG_PROC_FS
2933d189634eSThomas Graf 	proc_net_remove(net, "ipv6_route");
2934d189634eSThomas Graf 	proc_net_remove(net, "rt6_stats");
2935d189634eSThomas Graf #endif
2936d189634eSThomas Graf }
2937d189634eSThomas Graf 
2938cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
2939cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
2940cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
2941cdb18761SDaniel Lezcano };
2942cdb18761SDaniel Lezcano 
2943c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
2944c3426b47SDavid S. Miller {
2945c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
2946c3426b47SDavid S. Miller 
2947c3426b47SDavid S. Miller 	if (!bp)
2948c3426b47SDavid S. Miller 		return -ENOMEM;
2949c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
2950c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
2951c3426b47SDavid S. Miller 	return 0;
2952c3426b47SDavid S. Miller }
2953c3426b47SDavid S. Miller 
2954c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
2955c3426b47SDavid S. Miller {
2956c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
2957c3426b47SDavid S. Miller 
2958c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
295956a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
2960c3426b47SDavid S. Miller 	kfree(bp);
2961c3426b47SDavid S. Miller }
2962c3426b47SDavid S. Miller 
29632b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
2964c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
2965c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
2966c3426b47SDavid S. Miller };
2967c3426b47SDavid S. Miller 
2968d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
2969d189634eSThomas Graf 	.init = ip6_route_net_init_late,
2970d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
2971d189634eSThomas Graf };
2972d189634eSThomas Graf 
29738ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
29748ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
29758ed67789SDaniel Lezcano 	.priority = 0,
29768ed67789SDaniel Lezcano };
29778ed67789SDaniel Lezcano 
2978433d49c3SDaniel Lezcano int __init ip6_route_init(void)
29791da177e4SLinus Torvalds {
2980433d49c3SDaniel Lezcano 	int ret;
2981433d49c3SDaniel Lezcano 
29829a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
29839a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
29849a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
29859a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
29869a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
2987c19a28e1SFernando Carrijo 		goto out;
298814e50e57SDavid S. Miller 
2989fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
29908ed67789SDaniel Lezcano 	if (ret)
2991bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
2992bdb3289fSDaniel Lezcano 
2993c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
2994c3426b47SDavid S. Miller 	if (ret)
2995e8803b6cSDavid S. Miller 		goto out_dst_entries;
29962a0c451aSThomas Graf 
29977e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
29987e52b33bSDavid S. Miller 	if (ret)
29997e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3000c3426b47SDavid S. Miller 
30015dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
30025dc121e9SArnaud Ebalard 
30038ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
30048ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
30058ed67789SDaniel Lezcano 	 * manually for init_net */
3006d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
30078ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3008bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3009d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
30108ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3011d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
30128ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3013bdb3289fSDaniel Lezcano   #endif
3014e8803b6cSDavid S. Miller 	ret = fib6_init();
3015433d49c3SDaniel Lezcano 	if (ret)
30168ed67789SDaniel Lezcano 		goto out_register_subsys;
3017433d49c3SDaniel Lezcano 
3018433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3019433d49c3SDaniel Lezcano 	if (ret)
3020e8803b6cSDavid S. Miller 		goto out_fib6_init;
3021c35b7e72SDaniel Lezcano 
3022433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3023433d49c3SDaniel Lezcano 	if (ret)
3024433d49c3SDaniel Lezcano 		goto xfrm6_init;
30257e5449c2SDaniel Lezcano 
3026d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3027d189634eSThomas Graf 	if (ret)
3028d189634eSThomas Graf 		goto fib6_rules_init;
3029d189634eSThomas Graf 
3030433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3031c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3032c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3033c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3034d189634eSThomas Graf 		goto out_register_late_subsys;
3035433d49c3SDaniel Lezcano 
30368ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3037cdb18761SDaniel Lezcano 	if (ret)
3038d189634eSThomas Graf 		goto out_register_late_subsys;
30398ed67789SDaniel Lezcano 
3040433d49c3SDaniel Lezcano out:
3041433d49c3SDaniel Lezcano 	return ret;
3042433d49c3SDaniel Lezcano 
3043d189634eSThomas Graf out_register_late_subsys:
3044d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3045433d49c3SDaniel Lezcano fib6_rules_init:
3046433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3047433d49c3SDaniel Lezcano xfrm6_init:
3048433d49c3SDaniel Lezcano 	xfrm6_fini();
30492a0c451aSThomas Graf out_fib6_init:
30502a0c451aSThomas Graf 	fib6_gc_cleanup();
30518ed67789SDaniel Lezcano out_register_subsys:
30528ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
30537e52b33bSDavid S. Miller out_register_inetpeer:
30547e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3055fc66f95cSEric Dumazet out_dst_entries:
3056fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3057433d49c3SDaniel Lezcano out_kmem_cache:
3058f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3059433d49c3SDaniel Lezcano 	goto out;
30601da177e4SLinus Torvalds }
30611da177e4SLinus Torvalds 
30621da177e4SLinus Torvalds void ip6_route_cleanup(void)
30631da177e4SLinus Torvalds {
30648ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3065d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3066101367c2SThomas Graf 	fib6_rules_cleanup();
30671da177e4SLinus Torvalds 	xfrm6_fini();
30681da177e4SLinus Torvalds 	fib6_gc_cleanup();
3069c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
30708ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
307141bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3072f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
30731da177e4SLinus Torvalds }
3074