xref: /openbmc/linux/net/ipv6/route.c (revision 3830847396fa6d7f9a5fec0ca9819c47ac8a64e8)
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 
274fc268d2SRandy Dunlap #include <linux/capability.h>
281da177e4SLinus Torvalds #include <linux/errno.h>
29bc3b2d7fSPaul Gortmaker #include <linux/export.h>
301da177e4SLinus Torvalds #include <linux/types.h>
311da177e4SLinus Torvalds #include <linux/times.h>
321da177e4SLinus Torvalds #include <linux/socket.h>
331da177e4SLinus Torvalds #include <linux/sockios.h>
341da177e4SLinus Torvalds #include <linux/net.h>
351da177e4SLinus Torvalds #include <linux/route.h>
361da177e4SLinus Torvalds #include <linux/netdevice.h>
371da177e4SLinus Torvalds #include <linux/in6.h>
387bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
391da177e4SLinus Torvalds #include <linux/init.h>
401da177e4SLinus Torvalds #include <linux/if_arp.h>
411da177e4SLinus Torvalds #include <linux/proc_fs.h>
421da177e4SLinus Torvalds #include <linux/seq_file.h>
435b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
445a0e3ad6STejun Heo #include <linux/slab.h>
45457c4cbcSEric W. Biederman #include <net/net_namespace.h>
461da177e4SLinus Torvalds #include <net/snmp.h>
471da177e4SLinus Torvalds #include <net/ipv6.h>
481da177e4SLinus Torvalds #include <net/ip6_fib.h>
491da177e4SLinus Torvalds #include <net/ip6_route.h>
501da177e4SLinus Torvalds #include <net/ndisc.h>
511da177e4SLinus Torvalds #include <net/addrconf.h>
521da177e4SLinus Torvalds #include <net/tcp.h>
531da177e4SLinus Torvalds #include <linux/rtnetlink.h>
541da177e4SLinus Torvalds #include <net/dst.h>
551da177e4SLinus Torvalds #include <net/xfrm.h>
568d71740cSTom Tucker #include <net/netevent.h>
5721713ebcSThomas Graf #include <net/netlink.h>
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #include <asm/uaccess.h>
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
621da177e4SLinus Torvalds #include <linux/sysctl.h>
631da177e4SLinus Torvalds #endif
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* Set to 3 to get tracing. */
661da177e4SLinus Torvalds #define RT6_DEBUG 2
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #if RT6_DEBUG >= 3
691da177e4SLinus Torvalds #define RDBG(x) printk x
701da177e4SLinus Torvalds #define RT6_TRACE(x...) printk(KERN_DEBUG x)
711da177e4SLinus Torvalds #else
721da177e4SLinus Torvalds #define RDBG(x)
731da177e4SLinus Torvalds #define RT6_TRACE(x...) do { ; } while (0)
741da177e4SLinus Torvalds #endif
751da177e4SLinus Torvalds 
7621efcfa0SEric Dumazet static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort,
7721efcfa0SEric Dumazet 				    const struct in6_addr *dest);
781da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
790dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
80ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
811da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
821da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
831da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
841da177e4SLinus Torvalds 				       struct net_device *dev, int how);
85569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
881da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
891da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
901da177e4SLinus Torvalds static void		ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
911da177e4SLinus Torvalds 
9270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
93efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
94b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
95b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
9670ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref);
97efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
98b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
99b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
10070ceb4f5SYOSHIFUJI Hideaki #endif
10170ceb4f5SYOSHIFUJI Hideaki 
10206582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
10306582540SDavid S. Miller {
10406582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
10506582540SDavid S. Miller 	struct inet_peer *peer;
10606582540SDavid S. Miller 	u32 *p = NULL;
10706582540SDavid S. Miller 
1088e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1098e2ec639SYan, Zheng 		return NULL;
1108e2ec639SYan, Zheng 
11106582540SDavid S. Miller 	if (!rt->rt6i_peer)
11206582540SDavid S. Miller 		rt6_bind_peer(rt, 1);
11306582540SDavid S. Miller 
11406582540SDavid S. Miller 	peer = rt->rt6i_peer;
11506582540SDavid S. Miller 	if (peer) {
11606582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
11706582540SDavid S. Miller 		unsigned long prev, new;
11806582540SDavid S. Miller 
11906582540SDavid S. Miller 		p = peer->metrics;
12006582540SDavid S. Miller 		if (inet_metrics_new(peer))
12106582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
12206582540SDavid S. Miller 
12306582540SDavid S. Miller 		new = (unsigned long) p;
12406582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
12506582540SDavid S. Miller 
12606582540SDavid S. Miller 		if (prev != old) {
12706582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
12806582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
12906582540SDavid S. Miller 				p = NULL;
13006582540SDavid S. Miller 		}
13106582540SDavid S. Miller 	}
13206582540SDavid S. Miller 	return p;
13306582540SDavid S. Miller }
13406582540SDavid S. Miller 
135d3aaeb38SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, const void *daddr)
136d3aaeb38SDavid S. Miller {
137d3aaeb38SDavid S. Miller 	return __neigh_lookup_errno(&nd_tbl, daddr, dst->dev);
138d3aaeb38SDavid S. Miller }
139d3aaeb38SDavid S. Miller 
1409a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1411da177e4SLinus Torvalds 	.family			=	AF_INET6,
14209640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1431da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1441da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1451da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1460dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
147ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
14806582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1491da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1501da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1511da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1521da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1531da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1541ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
155d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1561da177e4SLinus Torvalds };
1571da177e4SLinus Torvalds 
158ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
159ec831ea7SRoland Dreier {
160618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
161618f9bc7SSteffen Klassert 
162618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
163ec831ea7SRoland Dreier }
164ec831ea7SRoland Dreier 
16514e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
16614e50e57SDavid S. Miller {
16714e50e57SDavid S. Miller }
16814e50e57SDavid S. Miller 
1690972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1700972ddb2SHeld Bernhard 					 unsigned long old)
1710972ddb2SHeld Bernhard {
1720972ddb2SHeld Bernhard 	return NULL;
1730972ddb2SHeld Bernhard }
1740972ddb2SHeld Bernhard 
17514e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
17614e50e57SDavid S. Miller 	.family			=	AF_INET6,
17709640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
17814e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
17914e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
180ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
181214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
18214e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
1830972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
184d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
18514e50e57SDavid S. Miller };
18614e50e57SDavid S. Miller 
18762fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
18862fa8a84SDavid S. Miller 	[RTAX_HOPLIMIT - 1] = 255,
18962fa8a84SDavid S. Miller };
19062fa8a84SDavid S. Miller 
191bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = {
1921da177e4SLinus Torvalds 	.dst = {
1931da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
1941da177e4SLinus Torvalds 		.__use		= 1,
1951da177e4SLinus Torvalds 		.obsolete	= -1,
1961da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
1971da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
1981da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
1991da177e4SLinus Torvalds 	},
2001da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2014f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2021da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2031da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2041da177e4SLinus Torvalds };
2051da177e4SLinus Torvalds 
206101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
207101367c2SThomas Graf 
2086723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
2096723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
2106723ab54SDavid S. Miller 
211280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = {
212101367c2SThomas Graf 	.dst = {
213101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
214101367c2SThomas Graf 		.__use		= 1,
215101367c2SThomas Graf 		.obsolete	= -1,
216101367c2SThomas Graf 		.error		= -EACCES,
2179ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2189ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
219101367c2SThomas Graf 	},
220101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2214f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
222101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
223101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
224101367c2SThomas Graf };
225101367c2SThomas Graf 
226bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = {
227101367c2SThomas Graf 	.dst = {
228101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
229101367c2SThomas Graf 		.__use		= 1,
230101367c2SThomas Graf 		.obsolete	= -1,
231101367c2SThomas Graf 		.error		= -EINVAL,
232352e512cSHerbert Xu 		.input		= dst_discard,
233352e512cSHerbert Xu 		.output		= dst_discard,
234101367c2SThomas Graf 	},
235101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2364f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
237101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
238101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
239101367c2SThomas Graf };
240101367c2SThomas Graf 
241101367c2SThomas Graf #endif
242101367c2SThomas Graf 
2431da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
2445c1e6aa3SDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops,
245957c665fSDavid S. Miller 					     struct net_device *dev,
246957c665fSDavid S. Miller 					     int flags)
2471da177e4SLinus Torvalds {
248957c665fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, flags);
249cf911662SDavid S. Miller 
250*38308473SDavid S. Miller 	if (rt)
251fbe58186SMadalin Bucur 		memset(&rt->rt6i_table, 0,
252fbe58186SMadalin Bucur 		       sizeof(*rt) - sizeof(struct dst_entry));
253cf911662SDavid S. Miller 
254cf911662SDavid S. Miller 	return rt;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2601da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
261b3419363SDavid S. Miller 	struct inet_peer *peer = rt->rt6i_peer;
2621da177e4SLinus Torvalds 
2638e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
2648e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
2658e2ec639SYan, Zheng 
266*38308473SDavid S. Miller 	if (idev) {
2671da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2681da177e4SLinus Torvalds 		in6_dev_put(idev);
2691da177e4SLinus Torvalds 	}
270b3419363SDavid S. Miller 	if (peer) {
271b3419363SDavid S. Miller 		rt->rt6i_peer = NULL;
272b3419363SDavid S. Miller 		inet_putpeer(peer);
273b3419363SDavid S. Miller 	}
274b3419363SDavid S. Miller }
275b3419363SDavid S. Miller 
2766431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
2776431cbc2SDavid S. Miller 
2786431cbc2SDavid S. Miller static u32 rt6_peer_genid(void)
2796431cbc2SDavid S. Miller {
2806431cbc2SDavid S. Miller 	return atomic_read(&__rt6_peer_genid);
2816431cbc2SDavid S. Miller }
2826431cbc2SDavid S. Miller 
283b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
284b3419363SDavid S. Miller {
285b3419363SDavid S. Miller 	struct inet_peer *peer;
286b3419363SDavid S. Miller 
287b3419363SDavid S. Miller 	peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
288b3419363SDavid S. Miller 	if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
289b3419363SDavid S. Miller 		inet_putpeer(peer);
2906431cbc2SDavid S. Miller 	else
2916431cbc2SDavid S. Miller 		rt->rt6i_peer_genid = rt6_peer_genid();
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
2951da177e4SLinus Torvalds 			   int how)
2961da177e4SLinus Torvalds {
2971da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2981da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2995a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
300c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3011da177e4SLinus Torvalds 
302*38308473SDavid S. Miller 	if (dev != loopback_dev && idev && idev->dev == dev) {
3035a3e55d6SDenis V. Lunev 		struct inet6_dev *loopback_idev =
3045a3e55d6SDenis V. Lunev 			in6_dev_get(loopback_dev);
305*38308473SDavid S. Miller 		if (loopback_idev) {
3061da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
3071da177e4SLinus Torvalds 			in6_dev_put(idev);
3081da177e4SLinus Torvalds 		}
3091da177e4SLinus Torvalds 	}
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds static __inline__ int rt6_check_expired(const struct rt6_info *rt)
3131da177e4SLinus Torvalds {
314a02cec21SEric Dumazet 	return (rt->rt6i_flags & RTF_EXPIRES) &&
315a02cec21SEric Dumazet 		time_after(jiffies, rt->rt6i_expires);
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
318b71d1d42SEric Dumazet static inline int rt6_need_strict(const struct in6_addr *daddr)
319c71099acSThomas Graf {
320a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
321a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
322c71099acSThomas Graf }
323c71099acSThomas Graf 
3241da177e4SLinus Torvalds /*
325c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
3261da177e4SLinus Torvalds  */
3271da177e4SLinus Torvalds 
3288ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
3298ed67789SDaniel Lezcano 						    struct rt6_info *rt,
330b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
3311da177e4SLinus Torvalds 						    int oif,
332d420895eSYOSHIFUJI Hideaki 						    int flags)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
3351da177e4SLinus Torvalds 	struct rt6_info *sprt;
3361da177e4SLinus Torvalds 
337dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
338dd3abc4eSYOSHIFUJI Hideaki 		goto out;
339dd3abc4eSYOSHIFUJI Hideaki 
340d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
3411da177e4SLinus Torvalds 		struct net_device *dev = sprt->rt6i_dev;
342dd3abc4eSYOSHIFUJI Hideaki 
343dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
3441da177e4SLinus Torvalds 			if (dev->ifindex == oif)
3451da177e4SLinus Torvalds 				return sprt;
3461da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
347*38308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
3481da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
349d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
3501da177e4SLinus Torvalds 						continue;
3511da177e4SLinus Torvalds 					if (local && (!oif ||
3521da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
3531da177e4SLinus Torvalds 						continue;
3541da177e4SLinus Torvalds 				}
3551da177e4SLinus Torvalds 				local = sprt;
3561da177e4SLinus Torvalds 			}
357dd3abc4eSYOSHIFUJI Hideaki 		} else {
358dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
359dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
360dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
361dd3abc4eSYOSHIFUJI Hideaki 		}
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds 
364dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
3651da177e4SLinus Torvalds 		if (local)
3661da177e4SLinus Torvalds 			return local;
3671da177e4SLinus Torvalds 
368d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
3698ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
3701da177e4SLinus Torvalds 	}
371dd3abc4eSYOSHIFUJI Hideaki out:
3721da177e4SLinus Torvalds 	return rt;
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
37527097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
37627097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
37727097255SYOSHIFUJI Hideaki {
378f2c31e32SEric Dumazet 	struct neighbour *neigh;
37927097255SYOSHIFUJI Hideaki 	/*
38027097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
38127097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
38227097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
38327097255SYOSHIFUJI Hideaki 	 *
38427097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
38527097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
38627097255SYOSHIFUJI Hideaki 	 */
387f2c31e32SEric Dumazet 	rcu_read_lock();
388f2c31e32SEric Dumazet 	neigh = rt ? dst_get_neighbour(&rt->dst) : NULL;
38927097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
390f2c31e32SEric Dumazet 		goto out;
39127097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
39227097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
39352e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
39427097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
39527097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
39627097255SYOSHIFUJI Hideaki 
39727097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
39827097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
39927097255SYOSHIFUJI Hideaki 
40027097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
40127097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
40227097255SYOSHIFUJI Hideaki 		ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
403f2c31e32SEric Dumazet 	} else {
40427097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
40527097255SYOSHIFUJI Hideaki 	}
406f2c31e32SEric Dumazet out:
407f2c31e32SEric Dumazet 	rcu_read_unlock();
408f2c31e32SEric Dumazet }
40927097255SYOSHIFUJI Hideaki #else
41027097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
41127097255SYOSHIFUJI Hideaki {
41227097255SYOSHIFUJI Hideaki }
41327097255SYOSHIFUJI Hideaki #endif
41427097255SYOSHIFUJI Hideaki 
4151da177e4SLinus Torvalds /*
416554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
4171da177e4SLinus Torvalds  */
418b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
4191da177e4SLinus Torvalds {
420554cfb7eSYOSHIFUJI Hideaki 	struct net_device *dev = rt->rt6i_dev;
421161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
422554cfb7eSYOSHIFUJI Hideaki 		return 2;
423161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
424161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
425161980f4SDavid S. Miller 		return 1;
426554cfb7eSYOSHIFUJI Hideaki 	return 0;
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
429b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
4301da177e4SLinus Torvalds {
431f2c31e32SEric Dumazet 	struct neighbour *neigh;
432398bcbebSYOSHIFUJI Hideaki 	int m;
433f2c31e32SEric Dumazet 
434f2c31e32SEric Dumazet 	rcu_read_lock();
435f2c31e32SEric Dumazet 	neigh = dst_get_neighbour(&rt->dst);
4364d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
4374d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
4384d0c5911SYOSHIFUJI Hideaki 		m = 1;
4394d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
4401da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
441554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
4424d0c5911SYOSHIFUJI Hideaki 			m = 2;
443398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
444398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
445398bcbebSYOSHIFUJI Hideaki 			m = 0;
446398bcbebSYOSHIFUJI Hideaki #endif
447398bcbebSYOSHIFUJI Hideaki 		else
448ea73ee23SYOSHIFUJI Hideaki 			m = 1;
4491da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
450398bcbebSYOSHIFUJI Hideaki 	} else
451398bcbebSYOSHIFUJI Hideaki 		m = 0;
452f2c31e32SEric Dumazet 	rcu_read_unlock();
453554cfb7eSYOSHIFUJI Hideaki 	return m;
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
456554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
457554cfb7eSYOSHIFUJI Hideaki 			   int strict)
458554cfb7eSYOSHIFUJI Hideaki {
4594d0c5911SYOSHIFUJI Hideaki 	int m, n;
4604d0c5911SYOSHIFUJI Hideaki 
4614d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
46277d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
463554cfb7eSYOSHIFUJI Hideaki 		return -1;
464ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
465ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
466ebacaaa0SYOSHIFUJI Hideaki #endif
4674d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
468557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
469554cfb7eSYOSHIFUJI Hideaki 		return -1;
470554cfb7eSYOSHIFUJI Hideaki 	return m;
471554cfb7eSYOSHIFUJI Hideaki }
472554cfb7eSYOSHIFUJI Hideaki 
473f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
474f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
475554cfb7eSYOSHIFUJI Hideaki {
476554cfb7eSYOSHIFUJI Hideaki 	int m;
477554cfb7eSYOSHIFUJI Hideaki 
478554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
479f11e6659SDavid S. Miller 		goto out;
480554cfb7eSYOSHIFUJI Hideaki 
481554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
482554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
483f11e6659SDavid S. Miller 		goto out;
484554cfb7eSYOSHIFUJI Hideaki 
485f11e6659SDavid S. Miller 	if (m > *mpri) {
486ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
48727097255SYOSHIFUJI Hideaki 			rt6_probe(match);
488f11e6659SDavid S. Miller 		*mpri = m;
489554cfb7eSYOSHIFUJI Hideaki 		match = rt;
490ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
49127097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
4921da177e4SLinus Torvalds 	}
493f11e6659SDavid S. Miller 
494f11e6659SDavid S. Miller out:
495f11e6659SDavid S. Miller 	return match;
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
498f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
499f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
500f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
501f11e6659SDavid S. Miller {
502f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
503f11e6659SDavid S. Miller 	int mpri = -1;
504f11e6659SDavid S. Miller 
505f11e6659SDavid S. Miller 	match = NULL;
506f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
507d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
508f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
509f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
510d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
511f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
512f11e6659SDavid S. Miller 
513f11e6659SDavid S. Miller 	return match;
514f11e6659SDavid S. Miller }
515f11e6659SDavid S. Miller 
516f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
517f11e6659SDavid S. Miller {
518f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
5198ed67789SDaniel Lezcano 	struct net *net;
520f11e6659SDavid S. Miller 
521f11e6659SDavid S. Miller 	RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
5220dc47877SHarvey Harrison 		  __func__, fn->leaf, oif);
523f11e6659SDavid S. Miller 
524f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
525f11e6659SDavid S. Miller 	if (!rt0)
526f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
527f11e6659SDavid S. Miller 
528f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
529f11e6659SDavid S. Miller 
530554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
531f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
532d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
533f11e6659SDavid S. Miller 
534554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
535f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
536f11e6659SDavid S. Miller 			next = fn->leaf;
537f11e6659SDavid S. Miller 
538f11e6659SDavid S. Miller 		if (next != rt0)
539f11e6659SDavid S. Miller 			fn->rr_ptr = next;
540554cfb7eSYOSHIFUJI Hideaki 	}
541554cfb7eSYOSHIFUJI Hideaki 
542f11e6659SDavid S. Miller 	RT6_TRACE("%s() => %p\n",
5430dc47877SHarvey Harrison 		  __func__, match);
544554cfb7eSYOSHIFUJI Hideaki 
545c346dca1SYOSHIFUJI Hideaki 	net = dev_net(rt0->rt6i_dev);
546a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
54970ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
55070ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
551b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
55270ceb4f5SYOSHIFUJI Hideaki {
553c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
55470ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
55570ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
55670ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
5574bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
55870ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
55970ceb4f5SYOSHIFUJI Hideaki 
56070ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
56170ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
56270ceb4f5SYOSHIFUJI Hideaki 	}
56370ceb4f5SYOSHIFUJI Hideaki 
56470ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
56570ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
56670ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
56770ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
56870ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
56970ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
57070ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
57170ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
57270ceb4f5SYOSHIFUJI Hideaki 		}
57370ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
57470ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
57570ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
57670ceb4f5SYOSHIFUJI Hideaki 		}
57770ceb4f5SYOSHIFUJI Hideaki 	}
57870ceb4f5SYOSHIFUJI Hideaki 
57970ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
58070ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
5813933fc95SJens Rosenboom 		return -EINVAL;
58270ceb4f5SYOSHIFUJI Hideaki 
5834bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
58470ceb4f5SYOSHIFUJI Hideaki 
58570ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
58670ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
58770ceb4f5SYOSHIFUJI Hideaki 	else {
58870ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
58970ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
59070ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
59170ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
59270ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
59370ceb4f5SYOSHIFUJI Hideaki 	}
59470ceb4f5SYOSHIFUJI Hideaki 
595efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
596efa2cea0SDaniel Lezcano 				dev->ifindex);
59770ceb4f5SYOSHIFUJI Hideaki 
59870ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
599e0a1ad73SThomas Graf 		ip6_del_rt(rt);
60070ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
60170ceb4f5SYOSHIFUJI Hideaki 	}
60270ceb4f5SYOSHIFUJI Hideaki 
60370ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
604efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
60570ceb4f5SYOSHIFUJI Hideaki 					pref);
60670ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
60770ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
60870ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
60970ceb4f5SYOSHIFUJI Hideaki 
61070ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
6114bed72e4SYOSHIFUJI Hideaki 		if (!addrconf_finite_timeout(lifetime)) {
61270ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags &= ~RTF_EXPIRES;
61370ceb4f5SYOSHIFUJI Hideaki 		} else {
61470ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_expires = jiffies + HZ * lifetime;
61570ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_EXPIRES;
61670ceb4f5SYOSHIFUJI Hideaki 		}
617d8d1f30bSChangli Gao 		dst_release(&rt->dst);
61870ceb4f5SYOSHIFUJI Hideaki 	}
61970ceb4f5SYOSHIFUJI Hideaki 	return 0;
62070ceb4f5SYOSHIFUJI Hideaki }
62170ceb4f5SYOSHIFUJI Hideaki #endif
62270ceb4f5SYOSHIFUJI Hideaki 
6238ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
624982f56f3SYOSHIFUJI Hideaki do { \
6258ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
626982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
627e0eda7bbSVille Nuorvala 		while (1) { \
628982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
629c71099acSThomas Graf 				goto out; \
630982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
631982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
6328bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
633982f56f3SYOSHIFUJI Hideaki 			else \
634982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
635c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
636c71099acSThomas Graf 				goto restart; \
637c71099acSThomas Graf 		} \
638982f56f3SYOSHIFUJI Hideaki 	} \
639982f56f3SYOSHIFUJI Hideaki } while (0)
640c71099acSThomas Graf 
6418ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
6428ed67789SDaniel Lezcano 					     struct fib6_table *table,
6434c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
6441da177e4SLinus Torvalds {
6451da177e4SLinus Torvalds 	struct fib6_node *fn;
6461da177e4SLinus Torvalds 	struct rt6_info *rt;
6471da177e4SLinus Torvalds 
648c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
6494c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
650c71099acSThomas Graf restart:
651c71099acSThomas Graf 	rt = fn->leaf;
6524c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
6534c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
654c71099acSThomas Graf out:
655d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
656c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
6571da177e4SLinus Torvalds 	return rt;
658c71099acSThomas Graf 
659c71099acSThomas Graf }
660c71099acSThomas Graf 
6619acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
6629acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
663c71099acSThomas Graf {
6644c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
6654c9483b2SDavid S. Miller 		.flowi6_oif = oif,
6664c9483b2SDavid S. Miller 		.daddr = *daddr,
667c71099acSThomas Graf 	};
668c71099acSThomas Graf 	struct dst_entry *dst;
66977d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
670c71099acSThomas Graf 
671adaa70bbSThomas Graf 	if (saddr) {
6724c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
673adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
674adaa70bbSThomas Graf 	}
675adaa70bbSThomas Graf 
6764c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
677c71099acSThomas Graf 	if (dst->error == 0)
678c71099acSThomas Graf 		return (struct rt6_info *) dst;
679c71099acSThomas Graf 
680c71099acSThomas Graf 	dst_release(dst);
681c71099acSThomas Graf 
6821da177e4SLinus Torvalds 	return NULL;
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6857159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
6867159039aSYOSHIFUJI Hideaki 
687c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
6881da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
6891da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
6901da177e4SLinus Torvalds    be destroyed.
6911da177e4SLinus Torvalds  */
6921da177e4SLinus Torvalds 
69386872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
6941da177e4SLinus Torvalds {
6951da177e4SLinus Torvalds 	int err;
696c71099acSThomas Graf 	struct fib6_table *table;
6971da177e4SLinus Torvalds 
698c71099acSThomas Graf 	table = rt->rt6i_table;
699c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
70086872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
701c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	return err;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
70640e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
70740e22e8fSThomas Graf {
7084d1169c1SDenis V. Lunev 	struct nl_info info = {
709c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
7104d1169c1SDenis V. Lunev 	};
711528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
71240e22e8fSThomas Graf }
71340e22e8fSThomas Graf 
71421efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_cow(const struct rt6_info *ort,
71521efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
716b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
7171da177e4SLinus Torvalds {
7181da177e4SLinus Torvalds 	struct rt6_info *rt;
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	/*
7211da177e4SLinus Torvalds 	 *	Clone the route.
7221da177e4SLinus Torvalds 	 */
7231da177e4SLinus Torvalds 
72421efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	if (rt) {
72714deae41SDavid S. Miller 		struct neighbour *neigh;
72814deae41SDavid S. Miller 		int attempts = !in_softirq();
72914deae41SDavid S. Miller 
73058c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags & RTF_GATEWAY)) {
73158c4fb86SYOSHIFUJI Hideaki 			if (rt->rt6i_dst.plen != 128 &&
73221efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
73358c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
7344e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = *daddr;
73558c4fb86SYOSHIFUJI Hideaki 		}
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
7401da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
7414e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
7421da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
7431da177e4SLinus Torvalds 		}
7441da177e4SLinus Torvalds #endif
7451da177e4SLinus Torvalds 
74614deae41SDavid S. Miller 	retry:
74714deae41SDavid S. Miller 		neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
74814deae41SDavid S. Miller 		if (IS_ERR(neigh)) {
74914deae41SDavid S. Miller 			struct net *net = dev_net(rt->rt6i_dev);
75014deae41SDavid S. Miller 			int saved_rt_min_interval =
75114deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval;
75214deae41SDavid S. Miller 			int saved_rt_elasticity =
75314deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity;
75414deae41SDavid S. Miller 
75514deae41SDavid S. Miller 			if (attempts-- > 0) {
75614deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
75714deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
75814deae41SDavid S. Miller 
75986393e52SAlexey Dobriyan 				ip6_dst_gc(&net->ipv6.ip6_dst_ops);
76014deae41SDavid S. Miller 
76114deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity =
76214deae41SDavid S. Miller 					saved_rt_elasticity;
76314deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval =
76414deae41SDavid S. Miller 					saved_rt_min_interval;
76514deae41SDavid S. Miller 				goto retry;
76614deae41SDavid S. Miller 			}
76714deae41SDavid S. Miller 
76814deae41SDavid S. Miller 			if (net_ratelimit())
76914deae41SDavid S. Miller 				printk(KERN_WARNING
7707e1b33e5SUlrich Weber 				       "ipv6: Neighbour table overflow.\n");
771d8d1f30bSChangli Gao 			dst_free(&rt->dst);
77214deae41SDavid S. Miller 			return NULL;
77314deae41SDavid S. Miller 		}
77469cce1d1SDavid S. Miller 		dst_set_neighbour(&rt->dst, neigh);
7751da177e4SLinus Torvalds 
77695a9a5baSYOSHIFUJI Hideaki 	}
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	return rt;
7791da177e4SLinus Torvalds }
78095a9a5baSYOSHIFUJI Hideaki 
78121efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
78221efcfa0SEric Dumazet 					const struct in6_addr *daddr)
783299d9939SYOSHIFUJI Hideaki {
78421efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
78521efcfa0SEric Dumazet 
786299d9939SYOSHIFUJI Hideaki 	if (rt) {
787299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
788f2c31e32SEric Dumazet 		dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_raw(&ort->dst)));
789299d9939SYOSHIFUJI Hideaki 	}
790299d9939SYOSHIFUJI Hideaki 	return rt;
791299d9939SYOSHIFUJI Hideaki }
792299d9939SYOSHIFUJI Hideaki 
7938ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
7944c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
7951da177e4SLinus Torvalds {
7961da177e4SLinus Torvalds 	struct fib6_node *fn;
797519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
798c71099acSThomas Graf 	int strict = 0;
7991da177e4SLinus Torvalds 	int attempts = 3;
800519fbd87SYOSHIFUJI Hideaki 	int err;
80153b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
8021da177e4SLinus Torvalds 
80377d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds relookup:
806c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8071da177e4SLinus Torvalds 
8088238dd06SYOSHIFUJI Hideaki restart_2:
8094c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds restart:
8124acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
8138ed67789SDaniel Lezcano 
8144c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
8158ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
8168238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
8171da177e4SLinus Torvalds 		goto out;
8181da177e4SLinus Torvalds 
819d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
820c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8211da177e4SLinus Torvalds 
822f2c31e32SEric Dumazet 	if (!dst_get_neighbour_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
8234c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
8247343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
8254c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
8267343ff31SDavid S. Miller 	else
8277343ff31SDavid S. Miller 		goto out2;
8281da177e4SLinus Torvalds 
829d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8308ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
8311da177e4SLinus Torvalds 
832d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
833e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
83440e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
835e40cf353SYOSHIFUJI Hideaki 		if (!err)
836e40cf353SYOSHIFUJI Hideaki 			goto out2;
837e40cf353SYOSHIFUJI Hideaki 	}
838e40cf353SYOSHIFUJI Hideaki 
839e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
8401da177e4SLinus Torvalds 		goto out2;
8411da177e4SLinus Torvalds 
842519fbd87SYOSHIFUJI Hideaki 	/*
843c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
844519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
8451da177e4SLinus Torvalds 	 */
846d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8471da177e4SLinus Torvalds 	goto relookup;
848e40cf353SYOSHIFUJI Hideaki 
849519fbd87SYOSHIFUJI Hideaki out:
8508238dd06SYOSHIFUJI Hideaki 	if (reachable) {
8518238dd06SYOSHIFUJI Hideaki 		reachable = 0;
8528238dd06SYOSHIFUJI Hideaki 		goto restart_2;
8538238dd06SYOSHIFUJI Hideaki 	}
854d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
855c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8561da177e4SLinus Torvalds out2:
857d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
858d8d1f30bSChangli Gao 	rt->dst.__use++;
859c71099acSThomas Graf 
860c71099acSThomas Graf 	return rt;
861c71099acSThomas Graf }
862c71099acSThomas Graf 
8638ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
8644c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
8654acad72dSPavel Emelyanov {
8664c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
8674acad72dSPavel Emelyanov }
8684acad72dSPavel Emelyanov 
869c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
870c71099acSThomas Graf {
871b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
872c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
873adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
8744c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
8754c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
8764c9483b2SDavid S. Miller 		.daddr = iph->daddr,
8774c9483b2SDavid S. Miller 		.saddr = iph->saddr,
8784c9483b2SDavid S. Miller 		.flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
8794c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
8804c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
881c71099acSThomas Graf 	};
882adaa70bbSThomas Graf 
8831d6e55f1SThomas Goff 	if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
884adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
885c71099acSThomas Graf 
8864c9483b2SDavid S. Miller 	skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input));
887c71099acSThomas Graf }
888c71099acSThomas Graf 
8898ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
8904c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
891c71099acSThomas Graf {
8924c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
893c71099acSThomas Graf }
894c71099acSThomas Graf 
8959c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
8964c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
897c71099acSThomas Graf {
898c71099acSThomas Graf 	int flags = 0;
899c71099acSThomas Graf 
9004c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
90177d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
902c71099acSThomas Graf 
9034c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
904adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
9050c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
9060c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
907adaa70bbSThomas Graf 
9084c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds 
9117159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
9121da177e4SLinus Torvalds 
9132774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
91414e50e57SDavid S. Miller {
9155c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
91614e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
91714e50e57SDavid S. Miller 
9185c1e6aa3SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
91914e50e57SDavid S. Miller 	if (rt) {
920cf911662SDavid S. Miller 		memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
921cf911662SDavid S. Miller 
922d8d1f30bSChangli Gao 		new = &rt->dst;
92314e50e57SDavid S. Miller 
92414e50e57SDavid S. Miller 		new->__use = 1;
925352e512cSHerbert Xu 		new->input = dst_discard;
926352e512cSHerbert Xu 		new->output = dst_discard;
92714e50e57SDavid S. Miller 
92821efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
92921efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
93021efcfa0SEric Dumazet 		else
931defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
93214e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
93314e50e57SDavid S. Miller 		if (rt->rt6i_idev)
93414e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
93514e50e57SDavid S. Miller 		rt->rt6i_expires = 0;
93614e50e57SDavid S. Miller 
9374e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
93814e50e57SDavid S. Miller 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
93914e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
94014e50e57SDavid S. Miller 
94114e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
94214e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
94314e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
94414e50e57SDavid S. Miller #endif
94514e50e57SDavid S. Miller 
94614e50e57SDavid S. Miller 		dst_free(new);
94714e50e57SDavid S. Miller 	}
94814e50e57SDavid S. Miller 
94969ead7afSDavid S. Miller 	dst_release(dst_orig);
95069ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
95114e50e57SDavid S. Miller }
95214e50e57SDavid S. Miller 
9531da177e4SLinus Torvalds /*
9541da177e4SLinus Torvalds  *	Destination cache support functions
9551da177e4SLinus Torvalds  */
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
9581da177e4SLinus Torvalds {
9591da177e4SLinus Torvalds 	struct rt6_info *rt;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
9621da177e4SLinus Torvalds 
9636431cbc2SDavid S. Miller 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
9646431cbc2SDavid S. Miller 		if (rt->rt6i_peer_genid != rt6_peer_genid()) {
9656431cbc2SDavid S. Miller 			if (!rt->rt6i_peer)
9666431cbc2SDavid S. Miller 				rt6_bind_peer(rt, 0);
9676431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
9686431cbc2SDavid S. Miller 		}
9691da177e4SLinus Torvalds 		return dst;
9706431cbc2SDavid S. Miller 	}
9711da177e4SLinus Torvalds 	return NULL;
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
9751da177e4SLinus Torvalds {
9761da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	if (rt) {
97954c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
98054c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
981e0a1ad73SThomas Graf 				ip6_del_rt(rt);
98254c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
9831da177e4SLinus Torvalds 			}
98454c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
98554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
98654c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
98754c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
98854c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
98954c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
9901da177e4SLinus Torvalds }
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
9931da177e4SLinus Torvalds {
9941da177e4SLinus Torvalds 	struct rt6_info *rt;
9951da177e4SLinus Torvalds 
9963ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
9971da177e4SLinus Torvalds 
998adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
9991da177e4SLinus Torvalds 	if (rt) {
10001da177e4SLinus Torvalds 		if (rt->rt6i_flags & RTF_CACHE) {
1001d8d1f30bSChangli Gao 			dst_set_expires(&rt->dst, 0);
10021da177e4SLinus Torvalds 			rt->rt6i_flags |= RTF_EXPIRES;
10031da177e4SLinus Torvalds 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
10041da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
10051da177e4SLinus Torvalds 	}
10061da177e4SLinus Torvalds }
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
10091da177e4SLinus Torvalds {
10101da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
10131da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
10141da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1015defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
10161da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1017defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1018defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
10191da177e4SLinus Torvalds 		}
1020defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
10211da177e4SLinus Torvalds 	}
10221da177e4SLinus Torvalds }
10231da177e4SLinus Torvalds 
10240dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
10251da177e4SLinus Torvalds {
10260dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
10270dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
10280dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
10290dbaee3bSDavid S. Miller 
10301da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
10311da177e4SLinus Torvalds 
10325578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
10335578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	/*
10361da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
10371da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
10381da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
10391da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
10401da177e4SLinus Torvalds 	 */
10411da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
10421da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
10431da177e4SLinus Torvalds 	return mtu;
10441da177e4SLinus Torvalds }
10451da177e4SLinus Torvalds 
1046ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1047d33e4553SDavid S. Miller {
1048d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1049618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1050618f9bc7SSteffen Klassert 
1051618f9bc7SSteffen Klassert 	if (mtu)
1052618f9bc7SSteffen Klassert 		return mtu;
1053618f9bc7SSteffen Klassert 
1054618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1055d33e4553SDavid S. Miller 
1056d33e4553SDavid S. Miller 	rcu_read_lock();
1057d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1058d33e4553SDavid S. Miller 	if (idev)
1059d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1060d33e4553SDavid S. Miller 	rcu_read_unlock();
1061d33e4553SDavid S. Miller 
1062d33e4553SDavid S. Miller 	return mtu;
1063d33e4553SDavid S. Miller }
1064d33e4553SDavid S. Miller 
10653b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
10663b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
10675d0bbeebSThomas Graf 
10683b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
10691da177e4SLinus Torvalds 				  struct neighbour *neigh,
10709acd9f3aSYOSHIFUJI Hideaki 				  const struct in6_addr *addr)
10711da177e4SLinus Torvalds {
10721da177e4SLinus Torvalds 	struct rt6_info *rt;
10731da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1074c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
10751da177e4SLinus Torvalds 
1076*38308473SDavid S. Miller 	if (unlikely(!idev))
10771da177e4SLinus Torvalds 		return NULL;
10781da177e4SLinus Torvalds 
1079957c665fSDavid S. Miller 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev, 0);
1080*38308473SDavid S. Miller 	if (unlikely(!rt)) {
10811da177e4SLinus Torvalds 		in6_dev_put(idev);
10821da177e4SLinus Torvalds 		goto out;
10831da177e4SLinus Torvalds 	}
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	if (neigh)
10861da177e4SLinus Torvalds 		neigh_hold(neigh);
108714deae41SDavid S. Miller 	else {
10881da177e4SLinus Torvalds 		neigh = ndisc_get_neigh(dev, addr);
108914deae41SDavid S. Miller 		if (IS_ERR(neigh))
109014deae41SDavid S. Miller 			neigh = NULL;
109114deae41SDavid S. Miller 	}
10921da177e4SLinus Torvalds 
10938e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
10948e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
109569cce1d1SDavid S. Miller 	dst_set_neighbour(&rt->dst, neigh);
1096d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
10974e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
10988e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
10998e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
11007011687fSGao feng 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
11011da177e4SLinus Torvalds 
11023b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1103d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1104d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
11053b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
11061da177e4SLinus Torvalds 
11075578689aSDaniel Lezcano 	fib6_force_start_gc(net);
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds out:
1110d8d1f30bSChangli Gao 	return &rt->dst;
11111da177e4SLinus Torvalds }
11121da177e4SLinus Torvalds 
11133d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
11141da177e4SLinus Torvalds {
1115e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
11163d0f24a7SStephen Hemminger 	int more = 0;
11171da177e4SLinus Torvalds 
11183b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
11193b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
11205d0bbeebSThomas Graf 
11211da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
11221da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
11231da177e4SLinus Torvalds 			*pprev = dst->next;
11241da177e4SLinus Torvalds 			dst_free(dst);
11251da177e4SLinus Torvalds 		} else {
11261da177e4SLinus Torvalds 			pprev = &dst->next;
11273d0f24a7SStephen Hemminger 			++more;
11281da177e4SLinus Torvalds 		}
11291da177e4SLinus Torvalds 	}
11301da177e4SLinus Torvalds 
11313b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
11325d0bbeebSThomas Graf 
11333d0f24a7SStephen Hemminger 	return more;
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds 
11361e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
11371e493d19SDavid S. Miller 			    void *arg)
11381e493d19SDavid S. Miller {
11391e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
11401e493d19SDavid S. Miller 
11411e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
11421e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
11431e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
11441e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
11451e493d19SDavid S. Miller 		if (func(rt, arg)) {
11461e493d19SDavid S. Miller 			*pprev = dst->next;
11471e493d19SDavid S. Miller 			dst_free(dst);
11481e493d19SDavid S. Miller 		} else {
11491e493d19SDavid S. Miller 			pprev = &dst->next;
11501e493d19SDavid S. Miller 		}
11511e493d19SDavid S. Miller 	}
11521e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
11531e493d19SDavid S. Miller }
11541e493d19SDavid S. Miller 
1155569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
11561da177e4SLinus Torvalds {
11571da177e4SLinus Torvalds 	unsigned long now = jiffies;
115886393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
11597019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
11607019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
11617019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
11627019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
11637019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1164fc66f95cSEric Dumazet 	int entries;
11651da177e4SLinus Torvalds 
1166fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
11677019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1168fc66f95cSEric Dumazet 	    entries <= rt_max_size)
11691da177e4SLinus Torvalds 		goto out;
11701da177e4SLinus Torvalds 
11716891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
11726891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
11736891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1174fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1175fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
11767019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
11771da177e4SLinus Torvalds out:
11787019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1179fc66f95cSEric Dumazet 	return entries > rt_max_size;
11801da177e4SLinus Torvalds }
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
11831da177e4SLinus Torvalds    but results in cleaner routing tables.
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds    Remove it only when all the things will work!
11861da177e4SLinus Torvalds  */
11871da177e4SLinus Torvalds 
11886b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
11891da177e4SLinus Torvalds {
11905170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1191a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
11926b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1193c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1194c68f24ccSEric Dumazet 
1195c68f24ccSEric Dumazet 		rcu_read_lock();
1196c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1197c68f24ccSEric Dumazet 		if (idev)
11981da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1199c68f24ccSEric Dumazet 		else
120053b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1201c68f24ccSEric Dumazet 		rcu_read_unlock();
12021da177e4SLinus Torvalds 	}
12031da177e4SLinus Torvalds 	return hoplimit;
12041da177e4SLinus Torvalds }
1205abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds /*
12081da177e4SLinus Torvalds  *
12091da177e4SLinus Torvalds  */
12101da177e4SLinus Torvalds 
121186872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
12121da177e4SLinus Torvalds {
12131da177e4SLinus Torvalds 	int err;
12145578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
12151da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
12161da177e4SLinus Torvalds 	struct net_device *dev = NULL;
12171da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1218c71099acSThomas Graf 	struct fib6_table *table;
12191da177e4SLinus Torvalds 	int addr_type;
12201da177e4SLinus Torvalds 
122186872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
12221da177e4SLinus Torvalds 		return -EINVAL;
12231da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
122486872cb5SThomas Graf 	if (cfg->fc_src_len)
12251da177e4SLinus Torvalds 		return -EINVAL;
12261da177e4SLinus Torvalds #endif
122786872cb5SThomas Graf 	if (cfg->fc_ifindex) {
12281da177e4SLinus Torvalds 		err = -ENODEV;
12295578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
12301da177e4SLinus Torvalds 		if (!dev)
12311da177e4SLinus Torvalds 			goto out;
12321da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
12331da177e4SLinus Torvalds 		if (!idev)
12341da177e4SLinus Torvalds 			goto out;
12351da177e4SLinus Torvalds 	}
12361da177e4SLinus Torvalds 
123786872cb5SThomas Graf 	if (cfg->fc_metric == 0)
123886872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
12391da177e4SLinus Torvalds 
1240c71099acSThomas Graf 	err = -ENOBUFS;
1241*38308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1242d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1243d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
1244*38308473SDavid S. Miller 		if (!table) {
1245d71314b4SMatti Vaittinen 			printk(KERN_WARNING "IPv6: NLM_F_CREATE should be specified when creating new route\n");
1246d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1247d71314b4SMatti Vaittinen 		}
1248d71314b4SMatti Vaittinen 	} else {
1249d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1250d71314b4SMatti Vaittinen 	}
1251*38308473SDavid S. Miller 
1252*38308473SDavid S. Miller 	if (!table)
1253c71099acSThomas Graf 		goto out;
1254c71099acSThomas Graf 
1255957c665fSDavid S. Miller 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT);
12561da177e4SLinus Torvalds 
1257*38308473SDavid S. Miller 	if (!rt) {
12581da177e4SLinus Torvalds 		err = -ENOMEM;
12591da177e4SLinus Torvalds 		goto out;
12601da177e4SLinus Torvalds 	}
12611da177e4SLinus Torvalds 
1262d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
12636f704992SYOSHIFUJI Hideaki 	rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
12646f704992SYOSHIFUJI Hideaki 				jiffies + clock_t_to_jiffies(cfg->fc_expires) :
12656f704992SYOSHIFUJI Hideaki 				0;
12661da177e4SLinus Torvalds 
126786872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
126886872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
126986872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
127086872cb5SThomas Graf 
127186872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1274d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1275ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1276ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
12771da177e4SLinus Torvalds 	else
1278d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
12791da177e4SLinus Torvalds 
1280d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
12811da177e4SLinus Torvalds 
128286872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
128386872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
12841da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
128511d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
12861da177e4SLinus Torvalds 
12878e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
12888e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
12898e2ec639SYan, Zheng 		if (!metrics) {
12908e2ec639SYan, Zheng 			err = -ENOMEM;
12918e2ec639SYan, Zheng 			goto out;
12928e2ec639SYan, Zheng 		}
12938e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
12948e2ec639SYan, Zheng 	}
12951da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
129686872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
129786872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
12981da177e4SLinus Torvalds #endif
12991da177e4SLinus Torvalds 
130086872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
13031da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
13041da177e4SLinus Torvalds 	 */
130586872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
1306*38308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
1307*38308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
1308*38308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
13091da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
13105578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
13111da177e4SLinus Torvalds 			if (dev) {
13121da177e4SLinus Torvalds 				dev_put(dev);
13131da177e4SLinus Torvalds 				in6_dev_put(idev);
13141da177e4SLinus Torvalds 			}
13155578689aSDaniel Lezcano 			dev = net->loopback_dev;
13161da177e4SLinus Torvalds 			dev_hold(dev);
13171da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
13181da177e4SLinus Torvalds 			if (!idev) {
13191da177e4SLinus Torvalds 				err = -ENODEV;
13201da177e4SLinus Torvalds 				goto out;
13211da177e4SLinus Torvalds 			}
13221da177e4SLinus Torvalds 		}
1323d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1324d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
1325d8d1f30bSChangli Gao 		rt->dst.error = -ENETUNREACH;
13261da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
13271da177e4SLinus Torvalds 		goto install_route;
13281da177e4SLinus Torvalds 	}
13291da177e4SLinus Torvalds 
133086872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1331b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
13321da177e4SLinus Torvalds 		int gwa_type;
13331da177e4SLinus Torvalds 
133486872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
13354e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
13361da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
13391da177e4SLinus Torvalds 			struct rt6_info *grt;
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
13421da177e4SLinus Torvalds 			   addresses as nexthop address.
13431da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
13441da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
13451da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
13461da177e4SLinus Torvalds 			   some exceptions. --ANK
13471da177e4SLinus Torvalds 			 */
13481da177e4SLinus Torvalds 			err = -EINVAL;
13491da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
13501da177e4SLinus Torvalds 				goto out;
13511da177e4SLinus Torvalds 
13525578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
1355*38308473SDavid S. Miller 			if (!grt)
13561da177e4SLinus Torvalds 				goto out;
13571da177e4SLinus Torvalds 			if (dev) {
13581da177e4SLinus Torvalds 				if (dev != grt->rt6i_dev) {
1359d8d1f30bSChangli Gao 					dst_release(&grt->dst);
13601da177e4SLinus Torvalds 					goto out;
13611da177e4SLinus Torvalds 				}
13621da177e4SLinus Torvalds 			} else {
13631da177e4SLinus Torvalds 				dev = grt->rt6i_dev;
13641da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
13651da177e4SLinus Torvalds 				dev_hold(dev);
13661da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
13671da177e4SLinus Torvalds 			}
13681da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
13691da177e4SLinus Torvalds 				err = 0;
1370d8d1f30bSChangli Gao 			dst_release(&grt->dst);
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 			if (err)
13731da177e4SLinus Torvalds 				goto out;
13741da177e4SLinus Torvalds 		}
13751da177e4SLinus Torvalds 		err = -EINVAL;
1376*38308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
13771da177e4SLinus Torvalds 			goto out;
13781da177e4SLinus Torvalds 	}
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	err = -ENODEV;
1381*38308473SDavid S. Miller 	if (!dev)
13821da177e4SLinus Torvalds 		goto out;
13831da177e4SLinus Torvalds 
1384c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1385c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1386c3968a85SDaniel Walter 			err = -EINVAL;
1387c3968a85SDaniel Walter 			goto out;
1388c3968a85SDaniel Walter 		}
13894e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1390c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1391c3968a85SDaniel Walter 	} else
1392c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1393c3968a85SDaniel Walter 
139486872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
139569cce1d1SDavid S. Miller 		struct neighbour *n = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
139669cce1d1SDavid S. Miller 		if (IS_ERR(n)) {
139769cce1d1SDavid S. Miller 			err = PTR_ERR(n);
13981da177e4SLinus Torvalds 			goto out;
13991da177e4SLinus Torvalds 		}
140069cce1d1SDavid S. Miller 		dst_set_neighbour(&rt->dst, n);
14011da177e4SLinus Torvalds 	}
14021da177e4SLinus Torvalds 
140386872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds install_route:
140686872cb5SThomas Graf 	if (cfg->fc_mx) {
140786872cb5SThomas Graf 		struct nlattr *nla;
140886872cb5SThomas Graf 		int remaining;
14091da177e4SLinus Torvalds 
141086872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
14118f4c1f9bSThomas Graf 			int type = nla_type(nla);
141286872cb5SThomas Graf 
141386872cb5SThomas Graf 			if (type) {
141486872cb5SThomas Graf 				if (type > RTAX_MAX) {
14151da177e4SLinus Torvalds 					err = -EINVAL;
14161da177e4SLinus Torvalds 					goto out;
14171da177e4SLinus Torvalds 				}
141886872cb5SThomas Graf 
1419defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
14201da177e4SLinus Torvalds 			}
14211da177e4SLinus Torvalds 		}
14221da177e4SLinus Torvalds 	}
14231da177e4SLinus Torvalds 
1424d8d1f30bSChangli Gao 	rt->dst.dev = dev;
14251da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1426c71099acSThomas Graf 	rt->rt6i_table = table;
142763152fc0SDaniel Lezcano 
1428c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
142963152fc0SDaniel Lezcano 
143086872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds out:
14331da177e4SLinus Torvalds 	if (dev)
14341da177e4SLinus Torvalds 		dev_put(dev);
14351da177e4SLinus Torvalds 	if (idev)
14361da177e4SLinus Torvalds 		in6_dev_put(idev);
14371da177e4SLinus Torvalds 	if (rt)
1438d8d1f30bSChangli Gao 		dst_free(&rt->dst);
14391da177e4SLinus Torvalds 	return err;
14401da177e4SLinus Torvalds }
14411da177e4SLinus Torvalds 
144286872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
14431da177e4SLinus Torvalds {
14441da177e4SLinus Torvalds 	int err;
1445c71099acSThomas Graf 	struct fib6_table *table;
1446c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(rt->rt6i_dev);
14471da177e4SLinus Torvalds 
14488ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry)
14496c813a72SPatrick McHardy 		return -ENOENT;
14506c813a72SPatrick McHardy 
1451c71099acSThomas Graf 	table = rt->rt6i_table;
1452c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
14531da177e4SLinus Torvalds 
145486872cb5SThomas Graf 	err = fib6_del(rt, info);
1455d8d1f30bSChangli Gao 	dst_release(&rt->dst);
14561da177e4SLinus Torvalds 
1457c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	return err;
14601da177e4SLinus Torvalds }
14611da177e4SLinus Torvalds 
1462e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1463e0a1ad73SThomas Graf {
14644d1169c1SDenis V. Lunev 	struct nl_info info = {
1465c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
14664d1169c1SDenis V. Lunev 	};
1467528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1468e0a1ad73SThomas Graf }
1469e0a1ad73SThomas Graf 
147086872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
14711da177e4SLinus Torvalds {
1472c71099acSThomas Graf 	struct fib6_table *table;
14731da177e4SLinus Torvalds 	struct fib6_node *fn;
14741da177e4SLinus Torvalds 	struct rt6_info *rt;
14751da177e4SLinus Torvalds 	int err = -ESRCH;
14761da177e4SLinus Torvalds 
14775578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
1478*38308473SDavid S. Miller 	if (!table)
1479c71099acSThomas Graf 		return err;
14801da177e4SLinus Torvalds 
1481c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1482c71099acSThomas Graf 
1483c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
148486872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
148586872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds 	if (fn) {
1488d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
148986872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1490*38308473SDavid S. Miller 			    (!rt->rt6i_dev ||
149186872cb5SThomas Graf 			     rt->rt6i_dev->ifindex != cfg->fc_ifindex))
14921da177e4SLinus Torvalds 				continue;
149386872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
149486872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
14951da177e4SLinus Torvalds 				continue;
149686872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
14971da177e4SLinus Torvalds 				continue;
1498d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1499c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
15001da177e4SLinus Torvalds 
150186872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
15021da177e4SLinus Torvalds 		}
15031da177e4SLinus Torvalds 	}
1504c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 	return err;
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds /*
15101da177e4SLinus Torvalds  *	Handle redirects
15111da177e4SLinus Torvalds  */
1512a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi {
15134c9483b2SDavid S. Miller 	struct flowi6 fl6;
1514a6279458SYOSHIFUJI Hideaki 	struct in6_addr gateway;
1515a6279458SYOSHIFUJI Hideaki };
15161da177e4SLinus Torvalds 
15178ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net,
15188ed67789SDaniel Lezcano 					     struct fib6_table *table,
15194c9483b2SDavid S. Miller 					     struct flowi6 *fl6,
1520a6279458SYOSHIFUJI Hideaki 					     int flags)
1521a6279458SYOSHIFUJI Hideaki {
15224c9483b2SDavid S. Miller 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1523a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt;
1524a6279458SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1525c71099acSThomas Graf 
1526e843b9e1SYOSHIFUJI Hideaki 	/*
1527e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1528e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1529e843b9e1SYOSHIFUJI Hideaki 	 *
1530e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1531e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1532e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1533e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1534e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1535e843b9e1SYOSHIFUJI Hideaki 	 */
15361da177e4SLinus Torvalds 
1537c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
15384c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1539e843b9e1SYOSHIFUJI Hideaki restart:
1540d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
15411da177e4SLinus Torvalds 		/*
15421da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
15431da177e4SLinus Torvalds 		 *
15441da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
15451da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
15461da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
15471da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
15481da177e4SLinus Torvalds 		 */
1549e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1550e843b9e1SYOSHIFUJI Hideaki 			continue;
15511da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1552e843b9e1SYOSHIFUJI Hideaki 			continue;
15534c9483b2SDavid S. Miller 		if (fl6->flowi6_oif != rt->rt6i_dev->ifindex)
1554e843b9e1SYOSHIFUJI Hideaki 			continue;
1555a6279458SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1556e843b9e1SYOSHIFUJI Hideaki 			continue;
1557e843b9e1SYOSHIFUJI Hideaki 		break;
1558e843b9e1SYOSHIFUJI Hideaki 	}
1559a6279458SYOSHIFUJI Hideaki 
1560cb15d9c2SYOSHIFUJI Hideaki 	if (!rt)
15618ed67789SDaniel Lezcano 		rt = net->ipv6.ip6_null_entry;
15624c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
1563cb15d9c2SYOSHIFUJI Hideaki out:
1564d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
1565a6279458SYOSHIFUJI Hideaki 
1566c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
15671da177e4SLinus Torvalds 
1568a6279458SYOSHIFUJI Hideaki 	return rt;
1569a6279458SYOSHIFUJI Hideaki };
1570a6279458SYOSHIFUJI Hideaki 
1571b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1572b71d1d42SEric Dumazet 					   const struct in6_addr *src,
1573b71d1d42SEric Dumazet 					   const struct in6_addr *gateway,
1574a6279458SYOSHIFUJI Hideaki 					   struct net_device *dev)
1575a6279458SYOSHIFUJI Hideaki {
1576adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1577c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
1578a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi rdfl = {
15794c9483b2SDavid S. Miller 		.fl6 = {
15804c9483b2SDavid S. Miller 			.flowi6_oif = dev->ifindex,
15814c9483b2SDavid S. Miller 			.daddr = *dest,
15824c9483b2SDavid S. Miller 			.saddr = *src,
1583a6279458SYOSHIFUJI Hideaki 		},
1584a6279458SYOSHIFUJI Hideaki 	};
1585adaa70bbSThomas Graf 
15864e3fd7a0SAlexey Dobriyan 	rdfl.gateway = *gateway;
158786c36ce4SBrian Haley 
1588adaa70bbSThomas Graf 	if (rt6_need_strict(dest))
1589adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
1590a6279458SYOSHIFUJI Hideaki 
15914c9483b2SDavid S. Miller 	return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
159258f09b78SDaniel Lezcano 						   flags, __ip6_route_redirect);
1593a6279458SYOSHIFUJI Hideaki }
1594a6279458SYOSHIFUJI Hideaki 
1595b71d1d42SEric Dumazet void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1596b71d1d42SEric Dumazet 		  const struct in6_addr *saddr,
1597a6279458SYOSHIFUJI Hideaki 		  struct neighbour *neigh, u8 *lladdr, int on_link)
1598a6279458SYOSHIFUJI Hideaki {
1599a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt = NULL;
1600a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1601c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(neigh->dev);
1602a6279458SYOSHIFUJI Hideaki 
1603a6279458SYOSHIFUJI Hideaki 	rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1604a6279458SYOSHIFUJI Hideaki 
16058ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry) {
16061da177e4SLinus Torvalds 		if (net_ratelimit())
16071da177e4SLinus Torvalds 			printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
16081da177e4SLinus Torvalds 			       "for redirect target\n");
1609a6279458SYOSHIFUJI Hideaki 		goto out;
16101da177e4SLinus Torvalds 	}
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	/*
16131da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
16141da177e4SLinus Torvalds 	 */
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
16171da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
16181da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
16191da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
16201da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
16211da177e4SLinus Torvalds 		     );
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 	/*
16241da177e4SLinus Torvalds 	 * Redirect received -> path was valid.
16251da177e4SLinus Torvalds 	 * Look, redirects are sent only in response to data packets,
16261da177e4SLinus Torvalds 	 * so that this nexthop apparently is reachable. --ANK
16271da177e4SLinus Torvalds 	 */
1628d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 	/* Duplicate redirect: silently ignore. */
1631f2c31e32SEric Dumazet 	if (neigh == dst_get_neighbour_raw(&rt->dst))
16321da177e4SLinus Torvalds 		goto out;
16331da177e4SLinus Torvalds 
163421efcfa0SEric Dumazet 	nrt = ip6_rt_copy(rt, dest);
1635*38308473SDavid S. Miller 	if (!nrt)
16361da177e4SLinus Torvalds 		goto out;
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
16391da177e4SLinus Torvalds 	if (on_link)
16401da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
16411da177e4SLinus Torvalds 
16424e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
164369cce1d1SDavid S. Miller 	dst_set_neighbour(&nrt->dst, neigh_clone(neigh));
16441da177e4SLinus Torvalds 
164540e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
16461da177e4SLinus Torvalds 		goto out;
16471da177e4SLinus Torvalds 
1648d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1649d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
16508d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
16518d71740cSTom Tucker 
16521da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
1653e0a1ad73SThomas Graf 		ip6_del_rt(rt);
16541da177e4SLinus Torvalds 		return;
16551da177e4SLinus Torvalds 	}
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds out:
1658d8d1f30bSChangli Gao 	dst_release(&rt->dst);
16591da177e4SLinus Torvalds }
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds /*
16621da177e4SLinus Torvalds  *	Handle ICMP "packet too big" messages
16631da177e4SLinus Torvalds  *	i.e. Path MTU discovery
16641da177e4SLinus Torvalds  */
16651da177e4SLinus Torvalds 
1666b71d1d42SEric Dumazet static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
1667ae878ae2SMaciej Żenczykowski 			     struct net *net, u32 pmtu, int ifindex)
16681da177e4SLinus Torvalds {
16691da177e4SLinus Torvalds 	struct rt6_info *rt, *nrt;
16701da177e4SLinus Torvalds 	int allfrag = 0;
1671d3052b55SAndrey Vagin again:
1672ae878ae2SMaciej Żenczykowski 	rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
1673*38308473SDavid S. Miller 	if (!rt)
16741da177e4SLinus Torvalds 		return;
16751da177e4SLinus Torvalds 
1676d3052b55SAndrey Vagin 	if (rt6_check_expired(rt)) {
1677d3052b55SAndrey Vagin 		ip6_del_rt(rt);
1678d3052b55SAndrey Vagin 		goto again;
1679d3052b55SAndrey Vagin 	}
1680d3052b55SAndrey Vagin 
1681d8d1f30bSChangli Gao 	if (pmtu >= dst_mtu(&rt->dst))
16821da177e4SLinus Torvalds 		goto out;
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds 	if (pmtu < IPV6_MIN_MTU) {
16851da177e4SLinus Torvalds 		/*
16861da177e4SLinus Torvalds 		 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
16871da177e4SLinus Torvalds 		 * MTU (1280) and a fragment header should always be included
16881da177e4SLinus Torvalds 		 * after a node receiving Too Big message reporting PMTU is
16891da177e4SLinus Torvalds 		 * less than the IPv6 Minimum Link MTU.
16901da177e4SLinus Torvalds 		 */
16911da177e4SLinus Torvalds 		pmtu = IPV6_MIN_MTU;
16921da177e4SLinus Torvalds 		allfrag = 1;
16931da177e4SLinus Torvalds 	}
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 	/* New mtu received -> path was valid.
16961da177e4SLinus Torvalds 	   They are sent only in response to data packets,
16971da177e4SLinus Torvalds 	   so that this nexthop apparently is reachable. --ANK
16981da177e4SLinus Torvalds 	 */
1699d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	/* Host route. If it is static, it would be better
17021da177e4SLinus Torvalds 	   not to override it, but add new one, so that
17031da177e4SLinus Torvalds 	   when cache entry will expire old pmtu
17041da177e4SLinus Torvalds 	   would return automatically.
17051da177e4SLinus Torvalds 	 */
17061da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
1707defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1708defb3519SDavid S. Miller 		if (allfrag) {
1709defb3519SDavid S. Miller 			u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1710defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1711defb3519SDavid S. Miller 			dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1712defb3519SDavid S. Miller 		}
1713d8d1f30bSChangli Gao 		dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
17141da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
17151da177e4SLinus Torvalds 		goto out;
17161da177e4SLinus Torvalds 	}
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	/* Network route.
17191da177e4SLinus Torvalds 	   Two cases are possible:
17201da177e4SLinus Torvalds 	   1. It is connected route. Action: COW
17211da177e4SLinus Torvalds 	   2. It is gatewayed route or NONEXTHOP route. Action: clone it.
17221da177e4SLinus Torvalds 	 */
1723f2c31e32SEric Dumazet 	if (!dst_get_neighbour_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
1724a1e78363SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, daddr, saddr);
1725d5315b50SYOSHIFUJI Hideaki 	else
1726d5315b50SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, daddr);
1727a1e78363SYOSHIFUJI Hideaki 
1728d5315b50SYOSHIFUJI Hideaki 	if (nrt) {
1729defb3519SDavid S. Miller 		dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1730defb3519SDavid S. Miller 		if (allfrag) {
1731defb3519SDavid S. Miller 			u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1732defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1733defb3519SDavid S. Miller 			dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1734defb3519SDavid S. Miller 		}
1735a1e78363SYOSHIFUJI Hideaki 
17361da177e4SLinus Torvalds 		/* According to RFC 1981, detecting PMTU increase shouldn't be
1737a1e78363SYOSHIFUJI Hideaki 		 * happened within 5 mins, the recommended timer is 10 mins.
1738a1e78363SYOSHIFUJI Hideaki 		 * Here this route expiration time is set to ip6_rt_mtu_expires
1739a1e78363SYOSHIFUJI Hideaki 		 * which is 10 mins. After 10 mins the decreased pmtu is expired
1740a1e78363SYOSHIFUJI Hideaki 		 * and detecting PMTU increase will be automatically happened.
17411da177e4SLinus Torvalds 		 */
1742d8d1f30bSChangli Gao 		dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
17431da177e4SLinus Torvalds 		nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1744a1e78363SYOSHIFUJI Hideaki 
174540e22e8fSThomas Graf 		ip6_ins_rt(nrt);
17461da177e4SLinus Torvalds 	}
17471da177e4SLinus Torvalds out:
1748d8d1f30bSChangli Gao 	dst_release(&rt->dst);
17491da177e4SLinus Torvalds }
17501da177e4SLinus Torvalds 
1751b71d1d42SEric Dumazet void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
1752ae878ae2SMaciej Żenczykowski 			struct net_device *dev, u32 pmtu)
1753ae878ae2SMaciej Żenczykowski {
1754ae878ae2SMaciej Żenczykowski 	struct net *net = dev_net(dev);
1755ae878ae2SMaciej Żenczykowski 
1756ae878ae2SMaciej Żenczykowski 	/*
1757ae878ae2SMaciej Żenczykowski 	 * RFC 1981 states that a node "MUST reduce the size of the packets it
1758ae878ae2SMaciej Żenczykowski 	 * is sending along the path" that caused the Packet Too Big message.
1759ae878ae2SMaciej Żenczykowski 	 * Since it's not possible in the general case to determine which
1760ae878ae2SMaciej Żenczykowski 	 * interface was used to send the original packet, we update the MTU
1761ae878ae2SMaciej Żenczykowski 	 * on the interface that will be used to send future packets. We also
1762ae878ae2SMaciej Żenczykowski 	 * update the MTU on the interface that received the Packet Too Big in
1763ae878ae2SMaciej Żenczykowski 	 * case the original packet was forced out that interface with
1764ae878ae2SMaciej Żenczykowski 	 * SO_BINDTODEVICE or similar. This is the next best thing to the
1765ae878ae2SMaciej Żenczykowski 	 * correct behaviour, which would be to update the MTU on all
1766ae878ae2SMaciej Żenczykowski 	 * interfaces.
1767ae878ae2SMaciej Żenczykowski 	 */
1768ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1769ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1770ae878ae2SMaciej Żenczykowski }
1771ae878ae2SMaciej Żenczykowski 
17721da177e4SLinus Torvalds /*
17731da177e4SLinus Torvalds  *	Misc support functions
17741da177e4SLinus Torvalds  */
17751da177e4SLinus Torvalds 
177621efcfa0SEric Dumazet static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort,
177721efcfa0SEric Dumazet 				    const struct in6_addr *dest)
17781da177e4SLinus Torvalds {
1779c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ort->rt6i_dev);
17805c1e6aa3SDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
1781957c665fSDavid S. Miller 					    ort->dst.dev, 0);
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	if (rt) {
1784d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1785d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
17868e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
17871da177e4SLinus Torvalds 
17884e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
17898e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1790defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1791d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
17921da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
17931da177e4SLinus Torvalds 		if (rt->rt6i_idev)
17941da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1795d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
17961da177e4SLinus Torvalds 		rt->rt6i_expires = 0;
17971da177e4SLinus Torvalds 
17984e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
17991da177e4SLinus Torvalds 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
18001da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
18031da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
18041da177e4SLinus Torvalds #endif
18050f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1806c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
18071da177e4SLinus Torvalds 	}
18081da177e4SLinus Torvalds 	return rt;
18091da177e4SLinus Torvalds }
18101da177e4SLinus Torvalds 
181170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1812efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1813b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1814b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
181570ceb4f5SYOSHIFUJI Hideaki {
181670ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
181770ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1818c71099acSThomas Graf 	struct fib6_table *table;
181970ceb4f5SYOSHIFUJI Hideaki 
1820efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
1821*38308473SDavid S. Miller 	if (!table)
1822c71099acSThomas Graf 		return NULL;
1823c71099acSThomas Graf 
1824c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1825c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
182670ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
182770ceb4f5SYOSHIFUJI Hideaki 		goto out;
182870ceb4f5SYOSHIFUJI Hideaki 
1829d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
183070ceb4f5SYOSHIFUJI Hideaki 		if (rt->rt6i_dev->ifindex != ifindex)
183170ceb4f5SYOSHIFUJI Hideaki 			continue;
183270ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
183370ceb4f5SYOSHIFUJI Hideaki 			continue;
183470ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
183570ceb4f5SYOSHIFUJI Hideaki 			continue;
1836d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
183770ceb4f5SYOSHIFUJI Hideaki 		break;
183870ceb4f5SYOSHIFUJI Hideaki 	}
183970ceb4f5SYOSHIFUJI Hideaki out:
1840c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
184170ceb4f5SYOSHIFUJI Hideaki 	return rt;
184270ceb4f5SYOSHIFUJI Hideaki }
184370ceb4f5SYOSHIFUJI Hideaki 
1844efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1845b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1846b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
184770ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref)
184870ceb4f5SYOSHIFUJI Hideaki {
184986872cb5SThomas Graf 	struct fib6_config cfg = {
185086872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1851238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
185286872cb5SThomas Graf 		.fc_ifindex	= ifindex,
185386872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
185486872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
185586872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
1856efa2cea0SDaniel Lezcano 		.fc_nlinfo.pid = 0,
1857efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1858efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
185986872cb5SThomas Graf 	};
186070ceb4f5SYOSHIFUJI Hideaki 
18614e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
18624e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
186386872cb5SThomas Graf 
1864e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1865e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
186686872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
186770ceb4f5SYOSHIFUJI Hideaki 
186886872cb5SThomas Graf 	ip6_route_add(&cfg);
186970ceb4f5SYOSHIFUJI Hideaki 
1870efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
187170ceb4f5SYOSHIFUJI Hideaki }
187270ceb4f5SYOSHIFUJI Hideaki #endif
187370ceb4f5SYOSHIFUJI Hideaki 
1874b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
18751da177e4SLinus Torvalds {
18761da177e4SLinus Torvalds 	struct rt6_info *rt;
1877c71099acSThomas Graf 	struct fib6_table *table;
18781da177e4SLinus Torvalds 
1879c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
1880*38308473SDavid S. Miller 	if (!table)
1881c71099acSThomas Graf 		return NULL;
18821da177e4SLinus Torvalds 
1883c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1884d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
18851da177e4SLinus Torvalds 		if (dev == rt->rt6i_dev &&
1886045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
18871da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
18881da177e4SLinus Torvalds 			break;
18891da177e4SLinus Torvalds 	}
18901da177e4SLinus Torvalds 	if (rt)
1891d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
1892c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
18931da177e4SLinus Torvalds 	return rt;
18941da177e4SLinus Torvalds }
18951da177e4SLinus Torvalds 
1896b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1897ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1898ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
18991da177e4SLinus Torvalds {
190086872cb5SThomas Graf 	struct fib6_config cfg = {
190186872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1902238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
190386872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
190486872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
190586872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
19065578689aSDaniel Lezcano 		.fc_nlinfo.pid = 0,
19075578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1908c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
190986872cb5SThomas Graf 	};
19101da177e4SLinus Torvalds 
19114e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
19121da177e4SLinus Torvalds 
191386872cb5SThomas Graf 	ip6_route_add(&cfg);
19141da177e4SLinus Torvalds 
19151da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
19161da177e4SLinus Torvalds }
19171da177e4SLinus Torvalds 
19187b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
19191da177e4SLinus Torvalds {
19201da177e4SLinus Torvalds 	struct rt6_info *rt;
1921c71099acSThomas Graf 	struct fib6_table *table;
1922c71099acSThomas Graf 
1923c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
19247b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
1925*38308473SDavid S. Miller 	if (!table)
1926c71099acSThomas Graf 		return;
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds restart:
1929c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1930d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
19311da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1932d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1933c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1934e0a1ad73SThomas Graf 			ip6_del_rt(rt);
19351da177e4SLinus Torvalds 			goto restart;
19361da177e4SLinus Torvalds 		}
19371da177e4SLinus Torvalds 	}
1938c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
19391da177e4SLinus Torvalds }
19401da177e4SLinus Torvalds 
19415578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
19425578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
194386872cb5SThomas Graf 				 struct fib6_config *cfg)
194486872cb5SThomas Graf {
194586872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
194686872cb5SThomas Graf 
194786872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
194886872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
194986872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
195086872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
195186872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
195286872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
195386872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
195486872cb5SThomas Graf 
19555578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1956f1243c2dSBenjamin Thery 
19574e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
19584e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
19594e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
196086872cb5SThomas Graf }
196186872cb5SThomas Graf 
19625578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
19631da177e4SLinus Torvalds {
196486872cb5SThomas Graf 	struct fib6_config cfg;
19651da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
19661da177e4SLinus Torvalds 	int err;
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds 	switch(cmd) {
19691da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
19701da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
19711da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
19721da177e4SLinus Torvalds 			return -EPERM;
19731da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
19741da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
19751da177e4SLinus Torvalds 		if (err)
19761da177e4SLinus Torvalds 			return -EFAULT;
19771da177e4SLinus Torvalds 
19785578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
197986872cb5SThomas Graf 
19801da177e4SLinus Torvalds 		rtnl_lock();
19811da177e4SLinus Torvalds 		switch (cmd) {
19821da177e4SLinus Torvalds 		case SIOCADDRT:
198386872cb5SThomas Graf 			err = ip6_route_add(&cfg);
19841da177e4SLinus Torvalds 			break;
19851da177e4SLinus Torvalds 		case SIOCDELRT:
198686872cb5SThomas Graf 			err = ip6_route_del(&cfg);
19871da177e4SLinus Torvalds 			break;
19881da177e4SLinus Torvalds 		default:
19891da177e4SLinus Torvalds 			err = -EINVAL;
19901da177e4SLinus Torvalds 		}
19911da177e4SLinus Torvalds 		rtnl_unlock();
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds 		return err;
19943ff50b79SStephen Hemminger 	}
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 	return -EINVAL;
19971da177e4SLinus Torvalds }
19981da177e4SLinus Torvalds 
19991da177e4SLinus Torvalds /*
20001da177e4SLinus Torvalds  *	Drop the packet on the floor
20011da177e4SLinus Torvalds  */
20021da177e4SLinus Torvalds 
2003d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
20041da177e4SLinus Torvalds {
2005612f09e8SYOSHIFUJI Hideaki 	int type;
2006adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
2007612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2008612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
20090660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
201045bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
20113bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20123bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2013612f09e8SYOSHIFUJI Hideaki 			break;
2014612f09e8SYOSHIFUJI Hideaki 		}
2015612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2016612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
20173bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20183bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2019612f09e8SYOSHIFUJI Hideaki 		break;
2020612f09e8SYOSHIFUJI Hideaki 	}
20213ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
20221da177e4SLinus Torvalds 	kfree_skb(skb);
20231da177e4SLinus Torvalds 	return 0;
20241da177e4SLinus Torvalds }
20251da177e4SLinus Torvalds 
20269ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
20279ce8ade0SThomas Graf {
2028612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
20299ce8ade0SThomas Graf }
20309ce8ade0SThomas Graf 
203120380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
20321da177e4SLinus Torvalds {
2033adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2034612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
20351da177e4SLinus Torvalds }
20361da177e4SLinus Torvalds 
20376723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
20386723ab54SDavid S. Miller 
20399ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
20409ce8ade0SThomas Graf {
2041612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
20429ce8ade0SThomas Graf }
20439ce8ade0SThomas Graf 
20449ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
20459ce8ade0SThomas Graf {
2046adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2047612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
20489ce8ade0SThomas Graf }
20499ce8ade0SThomas Graf 
20506723ab54SDavid S. Miller #endif
20516723ab54SDavid S. Miller 
20521da177e4SLinus Torvalds /*
20531da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
20541da177e4SLinus Torvalds  */
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
20571da177e4SLinus Torvalds 				    const struct in6_addr *addr,
20581da177e4SLinus Torvalds 				    int anycast)
20591da177e4SLinus Torvalds {
2060c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
20615c1e6aa3SDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
2062957c665fSDavid S. Miller 					    net->loopback_dev, 0);
206314deae41SDavid S. Miller 	struct neighbour *neigh;
20641da177e4SLinus Torvalds 
2065*38308473SDavid S. Miller 	if (!rt) {
206640385653SBen Greear 		if (net_ratelimit())
206740385653SBen Greear 			pr_warning("IPv6:  Maximum number of routes reached,"
206840385653SBen Greear 				   " consider increasing route/max_size.\n");
20691da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
207040385653SBen Greear 	}
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 	in6_dev_hold(idev);
20731da177e4SLinus Torvalds 
207411d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2075d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2076d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
20771da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2078d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
208158c4fb86SYOSHIFUJI Hideaki 	if (anycast)
208258c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
208358c4fb86SYOSHIFUJI Hideaki 	else
20841da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
208514deae41SDavid S. Miller 	neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
208614deae41SDavid S. Miller 	if (IS_ERR(neigh)) {
2087d8d1f30bSChangli Gao 		dst_free(&rt->dst);
208814deae41SDavid S. Miller 
208929546a64SDavid S. Miller 		return ERR_CAST(neigh);
20901da177e4SLinus Torvalds 	}
209169cce1d1SDavid S. Miller 	dst_set_neighbour(&rt->dst, neigh);
20921da177e4SLinus Torvalds 
20934e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
20941da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
20955578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
20961da177e4SLinus Torvalds 
2097d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds 	return rt;
21001da177e4SLinus Torvalds }
21011da177e4SLinus Torvalds 
2102c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2103c3968a85SDaniel Walter 			struct rt6_info *rt,
2104b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2105c3968a85SDaniel Walter 			unsigned int prefs,
2106c3968a85SDaniel Walter 			struct in6_addr *saddr)
2107c3968a85SDaniel Walter {
2108c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2109c3968a85SDaniel Walter 	int err = 0;
2110c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
21114e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2112c3968a85SDaniel Walter 	else
2113c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2114c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2115c3968a85SDaniel Walter 	return err;
2116c3968a85SDaniel Walter }
2117c3968a85SDaniel Walter 
2118c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2119c3968a85SDaniel Walter struct arg_dev_net_ip {
2120c3968a85SDaniel Walter 	struct net_device *dev;
2121c3968a85SDaniel Walter 	struct net *net;
2122c3968a85SDaniel Walter 	struct in6_addr *addr;
2123c3968a85SDaniel Walter };
2124c3968a85SDaniel Walter 
2125c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2126c3968a85SDaniel Walter {
2127c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2128c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2129c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2130c3968a85SDaniel Walter 
2131*38308473SDavid S. Miller 	if (((void *)rt->rt6i_dev == dev || !dev) &&
2132c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2133c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2134c3968a85SDaniel Walter 		/* remove prefsrc entry */
2135c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2136c3968a85SDaniel Walter 	}
2137c3968a85SDaniel Walter 	return 0;
2138c3968a85SDaniel Walter }
2139c3968a85SDaniel Walter 
2140c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2141c3968a85SDaniel Walter {
2142c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2143c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2144c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2145c3968a85SDaniel Walter 		.net = net,
2146c3968a85SDaniel Walter 		.addr = &ifp->addr,
2147c3968a85SDaniel Walter 	};
2148c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2149c3968a85SDaniel Walter }
2150c3968a85SDaniel Walter 
21518ed67789SDaniel Lezcano struct arg_dev_net {
21528ed67789SDaniel Lezcano 	struct net_device *dev;
21538ed67789SDaniel Lezcano 	struct net *net;
21548ed67789SDaniel Lezcano };
21558ed67789SDaniel Lezcano 
21561da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
21571da177e4SLinus Torvalds {
2158bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2159bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
21608ed67789SDaniel Lezcano 
2161*38308473SDavid S. Miller 	if ((rt->rt6i_dev == dev || !dev) &&
2162bc3ef660Sstephen hemminger 	    rt != adn->net->ipv6.ip6_null_entry) {
21631da177e4SLinus Torvalds 		RT6_TRACE("deleted by ifdown %p\n", rt);
21641da177e4SLinus Torvalds 		return -1;
21651da177e4SLinus Torvalds 	}
21661da177e4SLinus Torvalds 	return 0;
21671da177e4SLinus Torvalds }
21681da177e4SLinus Torvalds 
2169f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
21701da177e4SLinus Torvalds {
21718ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
21728ed67789SDaniel Lezcano 		.dev = dev,
21738ed67789SDaniel Lezcano 		.net = net,
21748ed67789SDaniel Lezcano 	};
21758ed67789SDaniel Lezcano 
21768ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
21771e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
21781da177e4SLinus Torvalds }
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds struct rt6_mtu_change_arg
21811da177e4SLinus Torvalds {
21821da177e4SLinus Torvalds 	struct net_device *dev;
21831da177e4SLinus Torvalds 	unsigned mtu;
21841da177e4SLinus Torvalds };
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
21871da177e4SLinus Torvalds {
21881da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
21891da177e4SLinus Torvalds 	struct inet6_dev *idev;
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
21921da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
21931da177e4SLinus Torvalds 	   We still use this lock to block changes
21941da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
21951da177e4SLinus Torvalds 	*/
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
2198*38308473SDavid S. Miller 	if (!idev)
21991da177e4SLinus Torvalds 		return 0;
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
22021da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
22031da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
22041da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
22051da177e4SLinus Torvalds 	 */
22061da177e4SLinus Torvalds 	/*
22071da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
22081da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
22091da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
22101da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
22111da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
22121da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
22131da177e4SLinus Torvalds 	   PMTU discouvery.
22141da177e4SLinus Torvalds 	 */
22151da177e4SLinus Torvalds 	if (rt->rt6i_dev == arg->dev &&
2216d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2217d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2218d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2219d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2220defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2221566cfd8fSSimon Arlott 	}
22221da177e4SLinus Torvalds 	return 0;
22231da177e4SLinus Torvalds }
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu)
22261da177e4SLinus Torvalds {
2227c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2228c71099acSThomas Graf 		.dev = dev,
2229c71099acSThomas Graf 		.mtu = mtu,
2230c71099acSThomas Graf 	};
22311da177e4SLinus Torvalds 
2232c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
22331da177e4SLinus Torvalds }
22341da177e4SLinus Torvalds 
2235ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
22365176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
223786872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2238ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
223986872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
224086872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
224186872cb5SThomas Graf };
224286872cb5SThomas Graf 
224386872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
224486872cb5SThomas Graf 			      struct fib6_config *cfg)
22451da177e4SLinus Torvalds {
224686872cb5SThomas Graf 	struct rtmsg *rtm;
224786872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
224886872cb5SThomas Graf 	int err;
22491da177e4SLinus Torvalds 
225086872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
225186872cb5SThomas Graf 	if (err < 0)
225286872cb5SThomas Graf 		goto errout;
22531da177e4SLinus Torvalds 
225486872cb5SThomas Graf 	err = -EINVAL;
225586872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
225686872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
225786872cb5SThomas Graf 
225886872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
225986872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
226086872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
226186872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
226286872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
226386872cb5SThomas Graf 
226486872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
226586872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
226686872cb5SThomas Graf 
2267ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2268ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2269ab79ad14SMaciej Żenczykowski 
227086872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
227186872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
22723b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
227386872cb5SThomas Graf 
227486872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
227586872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
227686872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
22771da177e4SLinus Torvalds 	}
227886872cb5SThomas Graf 
227986872cb5SThomas Graf 	if (tb[RTA_DST]) {
228086872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
228186872cb5SThomas Graf 
228286872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
228386872cb5SThomas Graf 			goto errout;
228486872cb5SThomas Graf 
228586872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
22861da177e4SLinus Torvalds 	}
228786872cb5SThomas Graf 
228886872cb5SThomas Graf 	if (tb[RTA_SRC]) {
228986872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
229086872cb5SThomas Graf 
229186872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
229286872cb5SThomas Graf 			goto errout;
229386872cb5SThomas Graf 
229486872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
22951da177e4SLinus Torvalds 	}
229686872cb5SThomas Graf 
2297c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2298c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2299c3968a85SDaniel Walter 
230086872cb5SThomas Graf 	if (tb[RTA_OIF])
230186872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
230286872cb5SThomas Graf 
230386872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
230486872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
230586872cb5SThomas Graf 
230686872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
230786872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
230886872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
23091da177e4SLinus Torvalds 	}
231086872cb5SThomas Graf 
231186872cb5SThomas Graf 	if (tb[RTA_TABLE])
231286872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
231386872cb5SThomas Graf 
231486872cb5SThomas Graf 	err = 0;
231586872cb5SThomas Graf errout:
231686872cb5SThomas Graf 	return err;
23171da177e4SLinus Torvalds }
23181da177e4SLinus Torvalds 
2319c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23201da177e4SLinus Torvalds {
232186872cb5SThomas Graf 	struct fib6_config cfg;
232286872cb5SThomas Graf 	int err;
23231da177e4SLinus Torvalds 
232486872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
232586872cb5SThomas Graf 	if (err < 0)
232686872cb5SThomas Graf 		return err;
232786872cb5SThomas Graf 
232886872cb5SThomas Graf 	return ip6_route_del(&cfg);
23291da177e4SLinus Torvalds }
23301da177e4SLinus Torvalds 
2331c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23321da177e4SLinus Torvalds {
233386872cb5SThomas Graf 	struct fib6_config cfg;
233486872cb5SThomas Graf 	int err;
23351da177e4SLinus Torvalds 
233686872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
233786872cb5SThomas Graf 	if (err < 0)
233886872cb5SThomas Graf 		return err;
233986872cb5SThomas Graf 
234086872cb5SThomas Graf 	return ip6_route_add(&cfg);
23411da177e4SLinus Torvalds }
23421da177e4SLinus Torvalds 
2343339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2344339bf98fSThomas Graf {
2345339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2346339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2347339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2348339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2349339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2350339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2351339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2352339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2353339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
23546a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2355339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2356339bf98fSThomas Graf }
2357339bf98fSThomas Graf 
2358191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2359191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
23600d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
23610d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
23627bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
23631da177e4SLinus Torvalds {
23641da177e4SLinus Torvalds 	struct rtmsg *rtm;
23651da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2366e3703b3dSThomas Graf 	long expires;
23679e762a4aSPatrick McHardy 	u32 table;
2368f2c31e32SEric Dumazet 	struct neighbour *n;
23691da177e4SLinus Torvalds 
23701da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
23711da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
23721da177e4SLinus Torvalds 			/* success since this is not a prefix route */
23731da177e4SLinus Torvalds 			return 1;
23741da177e4SLinus Torvalds 		}
23751da177e4SLinus Torvalds 	}
23761da177e4SLinus Torvalds 
23772d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
2378*38308473SDavid S. Miller 	if (!nlh)
237926932566SPatrick McHardy 		return -EMSGSIZE;
23802d7202bfSThomas Graf 
23812d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
23821da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
23831da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
23841da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
23851da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2386c71099acSThomas Graf 	if (rt->rt6i_table)
23879e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2388c71099acSThomas Graf 	else
23899e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
23909e762a4aSPatrick McHardy 	rtm->rtm_table = table;
23912d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_TABLE, table);
23921da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_REJECT)
23931da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
2394ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2395ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
23961da177e4SLinus Torvalds 	else if (rt->rt6i_dev && (rt->rt6i_dev->flags & IFF_LOOPBACK))
23971da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
23981da177e4SLinus Torvalds 	else
23991da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
24001da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
24011da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
24021da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
24031da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
24041da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
24051da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
24061da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
24071da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_DEFAULT)
24081da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
24111da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds 	if (dst) {
24142d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, dst);
24151da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
24161da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
24172d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
24181da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
24191da177e4SLinus Torvalds 	if (src) {
24202d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, src);
24211da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
24221da177e4SLinus Torvalds 	} else if (rtm->rtm_src_len)
24232d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
24241da177e4SLinus Torvalds #endif
24257bc570c8SYOSHIFUJI Hideaki 	if (iif) {
24267bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
24277bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
24288229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
24297bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
24307bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
24317bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
24327bc570c8SYOSHIFUJI Hideaki 						return 0;
24337bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
24347bc570c8SYOSHIFUJI Hideaki 				} else {
24357bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
24367bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
24377bc570c8SYOSHIFUJI Hideaki 				}
24387bc570c8SYOSHIFUJI Hideaki 			}
24397bc570c8SYOSHIFUJI Hideaki 		} else
24407bc570c8SYOSHIFUJI Hideaki #endif
24412d7202bfSThomas Graf 			NLA_PUT_U32(skb, RTA_IIF, iif);
24427bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
24431da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2444c3968a85SDaniel Walter 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0)
2445c3968a85SDaniel Walter 			NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2446c3968a85SDaniel Walter 	}
2447c3968a85SDaniel Walter 
2448c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2449c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
24504e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
24512d7202bfSThomas Graf 		NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
24521da177e4SLinus Torvalds 	}
24532d7202bfSThomas Graf 
2454defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
24552d7202bfSThomas Graf 		goto nla_put_failure;
24562d7202bfSThomas Graf 
2457f2c31e32SEric Dumazet 	rcu_read_lock();
2458f2c31e32SEric Dumazet 	n = dst_get_neighbour(&rt->dst);
2459f2c31e32SEric Dumazet 	if (n)
2460f2c31e32SEric Dumazet 		NLA_PUT(skb, RTA_GATEWAY, 16, &n->primary_key);
2461f2c31e32SEric Dumazet 	rcu_read_unlock();
24622d7202bfSThomas Graf 
2463d8d1f30bSChangli Gao 	if (rt->dst.dev)
24642d7202bfSThomas Graf 		NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
24652d7202bfSThomas Graf 
24662d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
2467e3703b3dSThomas Graf 
246836e3deaeSYOSHIFUJI Hideaki 	if (!(rt->rt6i_flags & RTF_EXPIRES))
246936e3deaeSYOSHIFUJI Hideaki 		expires = 0;
247036e3deaeSYOSHIFUJI Hideaki 	else if (rt->rt6i_expires - jiffies < INT_MAX)
247136e3deaeSYOSHIFUJI Hideaki 		expires = rt->rt6i_expires - jiffies;
247236e3deaeSYOSHIFUJI Hideaki 	else
247336e3deaeSYOSHIFUJI Hideaki 		expires = INT_MAX;
247469cdf8f9SYOSHIFUJI Hideaki 
2475d8d1f30bSChangli Gao 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2476d8d1f30bSChangli Gao 			       expires, rt->dst.error) < 0)
2477e3703b3dSThomas Graf 		goto nla_put_failure;
24781da177e4SLinus Torvalds 
24792d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
24802d7202bfSThomas Graf 
24812d7202bfSThomas Graf nla_put_failure:
248226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
248326932566SPatrick McHardy 	return -EMSGSIZE;
24841da177e4SLinus Torvalds }
24851da177e4SLinus Torvalds 
24861b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
24871da177e4SLinus Torvalds {
24881da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
24891da177e4SLinus Torvalds 	int prefix;
24901da177e4SLinus Torvalds 
24912d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
24922d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
24931da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
24941da177e4SLinus Torvalds 	} else
24951da177e4SLinus Torvalds 		prefix = 0;
24961da177e4SLinus Torvalds 
2497191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2498191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
24991da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
25007bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
25011da177e4SLinus Torvalds }
25021da177e4SLinus Torvalds 
2503c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
25041da177e4SLinus Torvalds {
25053b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2506ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
25071da177e4SLinus Torvalds 	struct rt6_info *rt;
2508ab364a6fSThomas Graf 	struct sk_buff *skb;
2509ab364a6fSThomas Graf 	struct rtmsg *rtm;
25104c9483b2SDavid S. Miller 	struct flowi6 fl6;
2511ab364a6fSThomas Graf 	int err, iif = 0;
2512ab364a6fSThomas Graf 
2513ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2514ab364a6fSThomas Graf 	if (err < 0)
2515ab364a6fSThomas Graf 		goto errout;
2516ab364a6fSThomas Graf 
2517ab364a6fSThomas Graf 	err = -EINVAL;
25184c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2519ab364a6fSThomas Graf 
2520ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2521ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2522ab364a6fSThomas Graf 			goto errout;
2523ab364a6fSThomas Graf 
25244e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2525ab364a6fSThomas Graf 	}
2526ab364a6fSThomas Graf 
2527ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2528ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2529ab364a6fSThomas Graf 			goto errout;
2530ab364a6fSThomas Graf 
25314e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2532ab364a6fSThomas Graf 	}
2533ab364a6fSThomas Graf 
2534ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2535ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2536ab364a6fSThomas Graf 
2537ab364a6fSThomas Graf 	if (tb[RTA_OIF])
25384c9483b2SDavid S. Miller 		fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]);
2539ab364a6fSThomas Graf 
2540ab364a6fSThomas Graf 	if (iif) {
2541ab364a6fSThomas Graf 		struct net_device *dev;
25425578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2543ab364a6fSThomas Graf 		if (!dev) {
2544ab364a6fSThomas Graf 			err = -ENODEV;
2545ab364a6fSThomas Graf 			goto errout;
2546ab364a6fSThomas Graf 		}
2547ab364a6fSThomas Graf 	}
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2550*38308473SDavid S. Miller 	if (!skb) {
2551ab364a6fSThomas Graf 		err = -ENOBUFS;
2552ab364a6fSThomas Graf 		goto errout;
2553ab364a6fSThomas Graf 	}
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
25561da177e4SLinus Torvalds 	   through good chunk of routing engine.
25571da177e4SLinus Torvalds 	 */
2558459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
25591da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
25601da177e4SLinus Torvalds 
25614c9483b2SDavid S. Miller 	rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6);
2562d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
25631da177e4SLinus Torvalds 
25644c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
25651da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
25667bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
25671da177e4SLinus Torvalds 	if (err < 0) {
2568ab364a6fSThomas Graf 		kfree_skb(skb);
2569ab364a6fSThomas Graf 		goto errout;
25701da177e4SLinus Torvalds 	}
25711da177e4SLinus Torvalds 
25725578689aSDaniel Lezcano 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2573ab364a6fSThomas Graf errout:
25741da177e4SLinus Torvalds 	return err;
25751da177e4SLinus Torvalds }
25761da177e4SLinus Torvalds 
257786872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
25781da177e4SLinus Torvalds {
25791da177e4SLinus Torvalds 	struct sk_buff *skb;
25805578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2581528c4cebSDenis V. Lunev 	u32 seq;
2582528c4cebSDenis V. Lunev 	int err;
25830d51aa80SJamal Hadi Salim 
2584528c4cebSDenis V. Lunev 	err = -ENOBUFS;
2585*38308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
258686872cb5SThomas Graf 
2587339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
2588*38308473SDavid S. Miller 	if (!skb)
258921713ebcSThomas Graf 		goto errout;
25901da177e4SLinus Torvalds 
2591191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
25927bc570c8SYOSHIFUJI Hideaki 				event, info->pid, seq, 0, 0, 0);
259326932566SPatrick McHardy 	if (err < 0) {
259426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
259526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
259626932566SPatrick McHardy 		kfree_skb(skb);
259726932566SPatrick McHardy 		goto errout;
259826932566SPatrick McHardy 	}
25991ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
26005578689aSDaniel Lezcano 		    info->nlh, gfp_any());
26011ce85fe4SPablo Neira Ayuso 	return;
260221713ebcSThomas Graf errout:
260321713ebcSThomas Graf 	if (err < 0)
26045578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
26051da177e4SLinus Torvalds }
26061da177e4SLinus Torvalds 
26078ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
26088ed67789SDaniel Lezcano 				unsigned long event, void *data)
26098ed67789SDaniel Lezcano {
26108ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2611c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26128ed67789SDaniel Lezcano 
26138ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2614d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
26158ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
26168ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2617d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
26188ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2619d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
26208ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
26218ed67789SDaniel Lezcano #endif
26228ed67789SDaniel Lezcano 	}
26238ed67789SDaniel Lezcano 
26248ed67789SDaniel Lezcano 	return NOTIFY_OK;
26258ed67789SDaniel Lezcano }
26268ed67789SDaniel Lezcano 
26271da177e4SLinus Torvalds /*
26281da177e4SLinus Torvalds  *	/proc
26291da177e4SLinus Torvalds  */
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
26321da177e4SLinus Torvalds 
26331da177e4SLinus Torvalds struct rt6_proc_arg
26341da177e4SLinus Torvalds {
26351da177e4SLinus Torvalds 	char *buffer;
26361da177e4SLinus Torvalds 	int offset;
26371da177e4SLinus Torvalds 	int length;
26381da177e4SLinus Torvalds 	int skip;
26391da177e4SLinus Torvalds 	int len;
26401da177e4SLinus Torvalds };
26411da177e4SLinus Torvalds 
26421da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
26431da177e4SLinus Torvalds {
264433120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
264569cce1d1SDavid S. Miller 	struct neighbour *n;
26461da177e4SLinus Torvalds 
26474b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
26481da177e4SLinus Torvalds 
26491da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
26504b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
26511da177e4SLinus Torvalds #else
265233120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
26531da177e4SLinus Torvalds #endif
2654f2c31e32SEric Dumazet 	rcu_read_lock();
265569cce1d1SDavid S. Miller 	n = dst_get_neighbour(&rt->dst);
265669cce1d1SDavid S. Miller 	if (n) {
265769cce1d1SDavid S. Miller 		seq_printf(m, "%pi6", n->primary_key);
26581da177e4SLinus Torvalds 	} else {
265933120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
26601da177e4SLinus Torvalds 	}
2661f2c31e32SEric Dumazet 	rcu_read_unlock();
266233120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2663d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2664d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
26651da177e4SLinus Torvalds 		   rt->rt6i_dev ? rt->rt6i_dev->name : "");
26661da177e4SLinus Torvalds 	return 0;
26671da177e4SLinus Torvalds }
26681da177e4SLinus Torvalds 
266933120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
26701da177e4SLinus Torvalds {
2671f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
2672f3db4851SDaniel Lezcano 	fib6_clean_all(net, rt6_info_route, 0, m);
267333120b30SAlexey Dobriyan 	return 0;
26741da177e4SLinus Torvalds }
26751da177e4SLinus Torvalds 
267633120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
267733120b30SAlexey Dobriyan {
2678de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2679f3db4851SDaniel Lezcano }
2680f3db4851SDaniel Lezcano 
268133120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
268233120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
268333120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
268433120b30SAlexey Dobriyan 	.read		= seq_read,
268533120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2686b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
268733120b30SAlexey Dobriyan };
268833120b30SAlexey Dobriyan 
26891da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
26901da177e4SLinus Torvalds {
269169ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
26921da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
269369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
269469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
269569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
269669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
269769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2698fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
269969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds 	return 0;
27021da177e4SLinus Torvalds }
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
27051da177e4SLinus Torvalds {
2706de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
270769ddb805SDaniel Lezcano }
270869ddb805SDaniel Lezcano 
27099a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
27101da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
27111da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
27121da177e4SLinus Torvalds 	.read	 = seq_read,
27131da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2714b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
27151da177e4SLinus Torvalds };
27161da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
27191da177e4SLinus Torvalds 
27201da177e4SLinus Torvalds static
27218d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
27221da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
27231da177e4SLinus Torvalds {
2724c486da34SLucian Adrian Grijincu 	struct net *net;
2725c486da34SLucian Adrian Grijincu 	int delay;
2726c486da34SLucian Adrian Grijincu 	if (!write)
2727c486da34SLucian Adrian Grijincu 		return -EINVAL;
2728c486da34SLucian Adrian Grijincu 
2729c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2730c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
27318d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
27325b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
27331da177e4SLinus Torvalds 	return 0;
27341da177e4SLinus Torvalds }
27351da177e4SLinus Torvalds 
2736760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
27371da177e4SLinus Torvalds 	{
27381da177e4SLinus Torvalds 		.procname	=	"flush",
27394990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
27401da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
274189c8b3a1SDave Jones 		.mode		=	0200,
27426d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
27431da177e4SLinus Torvalds 	},
27441da177e4SLinus Torvalds 	{
27451da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
27469a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
27471da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27481da177e4SLinus Torvalds 		.mode		=	0644,
27496d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27501da177e4SLinus Torvalds 	},
27511da177e4SLinus Torvalds 	{
27521da177e4SLinus Torvalds 		.procname	=	"max_size",
27534990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
27541da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27551da177e4SLinus Torvalds 		.mode		=	0644,
27566d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27571da177e4SLinus Torvalds 	},
27581da177e4SLinus Torvalds 	{
27591da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
27604990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
27611da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27621da177e4SLinus Torvalds 		.mode		=	0644,
27636d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27641da177e4SLinus Torvalds 	},
27651da177e4SLinus Torvalds 	{
27661da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
27674990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
27681da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27691da177e4SLinus Torvalds 		.mode		=	0644,
27706d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27711da177e4SLinus Torvalds 	},
27721da177e4SLinus Torvalds 	{
27731da177e4SLinus Torvalds 		.procname	=	"gc_interval",
27744990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
27751da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27761da177e4SLinus Torvalds 		.mode		=	0644,
27776d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27781da177e4SLinus Torvalds 	},
27791da177e4SLinus Torvalds 	{
27801da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
27814990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
27821da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27831da177e4SLinus Torvalds 		.mode		=	0644,
2784f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
27851da177e4SLinus Torvalds 	},
27861da177e4SLinus Torvalds 	{
27871da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
27884990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
27891da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27901da177e4SLinus Torvalds 		.mode		=	0644,
27916d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27921da177e4SLinus Torvalds 	},
27931da177e4SLinus Torvalds 	{
27941da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
27954990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
27961da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27971da177e4SLinus Torvalds 		.mode		=	0644,
2798f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
27991da177e4SLinus Torvalds 	},
28001da177e4SLinus Torvalds 	{
28011da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
28024990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28031da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28041da177e4SLinus Torvalds 		.mode		=	0644,
28056d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
28061da177e4SLinus Torvalds 	},
2807f8572d8fSEric W. Biederman 	{ }
28081da177e4SLinus Torvalds };
28091da177e4SLinus Torvalds 
28102c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2811760f2d01SDaniel Lezcano {
2812760f2d01SDaniel Lezcano 	struct ctl_table *table;
2813760f2d01SDaniel Lezcano 
2814760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2815760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2816760f2d01SDaniel Lezcano 			GFP_KERNEL);
28175ee09105SYOSHIFUJI Hideaki 
28185ee09105SYOSHIFUJI Hideaki 	if (table) {
28195ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2820c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
282186393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
28225ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
28235ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28245ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
28255ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
28265ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
28275ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
28285ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
28299c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28305ee09105SYOSHIFUJI Hideaki 	}
28315ee09105SYOSHIFUJI Hideaki 
2832760f2d01SDaniel Lezcano 	return table;
2833760f2d01SDaniel Lezcano }
28341da177e4SLinus Torvalds #endif
28351da177e4SLinus Torvalds 
28362c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2837cdb18761SDaniel Lezcano {
2838633d424bSPavel Emelyanov 	int ret = -ENOMEM;
28398ed67789SDaniel Lezcano 
284086393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
284186393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2842f2fc6a54SBenjamin Thery 
2843fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2844fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2845fc66f95cSEric Dumazet 
28468ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
28478ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
28488ed67789SDaniel Lezcano 					   GFP_KERNEL);
28498ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2850fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2851d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
28528ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2853d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
285462fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
285562fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28568ed67789SDaniel Lezcano 
28578ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
28588ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
28598ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
28608ed67789SDaniel Lezcano 					       GFP_KERNEL);
286168fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
286268fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2863d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
28648ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2865d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
286662fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
286762fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28688ed67789SDaniel Lezcano 
28698ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
28708ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
28718ed67789SDaniel Lezcano 					       GFP_KERNEL);
287268fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
287368fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2874d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
28758ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2876d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
287762fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
287862fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28798ed67789SDaniel Lezcano #endif
28808ed67789SDaniel Lezcano 
2881b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2882b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2883b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2884b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2885b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2886b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2887b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2888b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2889b339a47cSPeter Zijlstra 
2890cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2891cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2892cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2893cdb18761SDaniel Lezcano #endif
28946891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
28956891a346SBenjamin Thery 
28968ed67789SDaniel Lezcano 	ret = 0;
28978ed67789SDaniel Lezcano out:
28988ed67789SDaniel Lezcano 	return ret;
2899f2fc6a54SBenjamin Thery 
290068fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
290168fffc67SPeter Zijlstra out_ip6_prohibit_entry:
290268fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
290368fffc67SPeter Zijlstra out_ip6_null_entry:
290468fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
290568fffc67SPeter Zijlstra #endif
2906fc66f95cSEric Dumazet out_ip6_dst_entries:
2907fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2908f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2909f2fc6a54SBenjamin Thery 	goto out;
2910cdb18761SDaniel Lezcano }
2911cdb18761SDaniel Lezcano 
29122c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2913cdb18761SDaniel Lezcano {
2914cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2915cdb18761SDaniel Lezcano 	proc_net_remove(net, "ipv6_route");
2916cdb18761SDaniel Lezcano 	proc_net_remove(net, "rt6_stats");
2917cdb18761SDaniel Lezcano #endif
29188ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
29198ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29208ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
29218ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
29228ed67789SDaniel Lezcano #endif
292341bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2924cdb18761SDaniel Lezcano }
2925cdb18761SDaniel Lezcano 
2926cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
2927cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
2928cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
2929cdb18761SDaniel Lezcano };
2930cdb18761SDaniel Lezcano 
29318ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
29328ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
29338ed67789SDaniel Lezcano 	.priority = 0,
29348ed67789SDaniel Lezcano };
29358ed67789SDaniel Lezcano 
2936433d49c3SDaniel Lezcano int __init ip6_route_init(void)
29371da177e4SLinus Torvalds {
2938433d49c3SDaniel Lezcano 	int ret;
2939433d49c3SDaniel Lezcano 
29409a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
29419a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
29429a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
29439a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
29449a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
2945c19a28e1SFernando Carrijo 		goto out;
294614e50e57SDavid S. Miller 
2947fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
29488ed67789SDaniel Lezcano 	if (ret)
2949bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
2950bdb3289fSDaniel Lezcano 
2951fc66f95cSEric Dumazet 	ret = register_pernet_subsys(&ip6_route_net_ops);
2952fc66f95cSEric Dumazet 	if (ret)
2953fc66f95cSEric Dumazet 		goto out_dst_entries;
2954fc66f95cSEric Dumazet 
29555dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
29565dc121e9SArnaud Ebalard 
29578ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
29588ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
29598ed67789SDaniel Lezcano 	 * manually for init_net */
2960d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
29618ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2962bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2963d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
29648ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2965d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
29668ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2967bdb3289fSDaniel Lezcano   #endif
2968433d49c3SDaniel Lezcano 	ret = fib6_init();
2969433d49c3SDaniel Lezcano 	if (ret)
29708ed67789SDaniel Lezcano 		goto out_register_subsys;
2971433d49c3SDaniel Lezcano 
2972433d49c3SDaniel Lezcano 	ret = xfrm6_init();
2973433d49c3SDaniel Lezcano 	if (ret)
2974cdb18761SDaniel Lezcano 		goto out_fib6_init;
2975c35b7e72SDaniel Lezcano 
2976433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
2977433d49c3SDaniel Lezcano 	if (ret)
2978433d49c3SDaniel Lezcano 		goto xfrm6_init;
29797e5449c2SDaniel Lezcano 
2980433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
2981c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
2982c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
2983c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
2984433d49c3SDaniel Lezcano 		goto fib6_rules_init;
2985433d49c3SDaniel Lezcano 
29868ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
2987cdb18761SDaniel Lezcano 	if (ret)
2988cdb18761SDaniel Lezcano 		goto fib6_rules_init;
29898ed67789SDaniel Lezcano 
2990433d49c3SDaniel Lezcano out:
2991433d49c3SDaniel Lezcano 	return ret;
2992433d49c3SDaniel Lezcano 
2993433d49c3SDaniel Lezcano fib6_rules_init:
2994433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
2995433d49c3SDaniel Lezcano xfrm6_init:
2996433d49c3SDaniel Lezcano 	xfrm6_fini();
2997433d49c3SDaniel Lezcano out_fib6_init:
2998433d49c3SDaniel Lezcano 	fib6_gc_cleanup();
29998ed67789SDaniel Lezcano out_register_subsys:
30008ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
3001fc66f95cSEric Dumazet out_dst_entries:
3002fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3003433d49c3SDaniel Lezcano out_kmem_cache:
3004f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3005433d49c3SDaniel Lezcano 	goto out;
30061da177e4SLinus Torvalds }
30071da177e4SLinus Torvalds 
30081da177e4SLinus Torvalds void ip6_route_cleanup(void)
30091da177e4SLinus Torvalds {
30108ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3011101367c2SThomas Graf 	fib6_rules_cleanup();
30121da177e4SLinus Torvalds 	xfrm6_fini();
30131da177e4SLinus Torvalds 	fib6_gc_cleanup();
30148ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
301541bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3016f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
30171da177e4SLinus Torvalds }
3018