xref: /openbmc/linux/net/ipv6/route.c (revision bc3b2d7fb9b014d75ebb79ba371a763dbab5e8cf)
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>
29*bc3b2d7fSPaul 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);
80d33e4553SDavid S. Miller static unsigned int	 ip6_default_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,
147d33e4553SDavid S. Miller 	.default_mtu		=	ip6_default_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 
158ec831ea7SRoland Dreier static unsigned int ip6_blackhole_default_mtu(const struct dst_entry *dst)
159ec831ea7SRoland Dreier {
160ec831ea7SRoland Dreier 	return 0;
161ec831ea7SRoland Dreier }
162ec831ea7SRoland Dreier 
16314e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
16414e50e57SDavid S. Miller {
16514e50e57SDavid S. Miller }
16614e50e57SDavid S. Miller 
1670972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1680972ddb2SHeld Bernhard 					 unsigned long old)
1690972ddb2SHeld Bernhard {
1700972ddb2SHeld Bernhard 	return NULL;
1710972ddb2SHeld Bernhard }
1720972ddb2SHeld Bernhard 
17314e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
17414e50e57SDavid S. Miller 	.family			=	AF_INET6,
17509640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
17614e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
17714e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
178ec831ea7SRoland Dreier 	.default_mtu		=	ip6_blackhole_default_mtu,
179214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
18014e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
1810972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
182d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
18314e50e57SDavid S. Miller };
18414e50e57SDavid S. Miller 
18562fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
18662fa8a84SDavid S. Miller 	[RTAX_HOPLIMIT - 1] = 255,
18762fa8a84SDavid S. Miller };
18862fa8a84SDavid S. Miller 
189bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = {
1901da177e4SLinus Torvalds 	.dst = {
1911da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
1921da177e4SLinus Torvalds 		.__use		= 1,
1931da177e4SLinus Torvalds 		.obsolete	= -1,
1941da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
1951da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
1961da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
1971da177e4SLinus Torvalds 	},
1981da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
1994f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2001da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2011da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2021da177e4SLinus Torvalds };
2031da177e4SLinus Torvalds 
204101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
205101367c2SThomas Graf 
2066723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
2076723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
2086723ab54SDavid S. Miller 
209280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = {
210101367c2SThomas Graf 	.dst = {
211101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
212101367c2SThomas Graf 		.__use		= 1,
213101367c2SThomas Graf 		.obsolete	= -1,
214101367c2SThomas Graf 		.error		= -EACCES,
2159ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2169ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
217101367c2SThomas Graf 	},
218101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2194f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
220101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
221101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
222101367c2SThomas Graf };
223101367c2SThomas Graf 
224bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = {
225101367c2SThomas Graf 	.dst = {
226101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
227101367c2SThomas Graf 		.__use		= 1,
228101367c2SThomas Graf 		.obsolete	= -1,
229101367c2SThomas Graf 		.error		= -EINVAL,
230352e512cSHerbert Xu 		.input		= dst_discard,
231352e512cSHerbert Xu 		.output		= dst_discard,
232101367c2SThomas Graf 	},
233101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2344f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
235101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
236101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
237101367c2SThomas Graf };
238101367c2SThomas Graf 
239101367c2SThomas Graf #endif
240101367c2SThomas Graf 
2411da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
2425c1e6aa3SDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops,
243957c665fSDavid S. Miller 					     struct net_device *dev,
244957c665fSDavid S. Miller 					     int flags)
2451da177e4SLinus Torvalds {
246957c665fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, flags);
247cf911662SDavid S. Miller 
248fbe58186SMadalin Bucur 	if (rt != NULL)
249fbe58186SMadalin Bucur 		memset(&rt->rt6i_table, 0,
250fbe58186SMadalin Bucur 			sizeof(*rt) - sizeof(struct dst_entry));
251cf911662SDavid S. Miller 
252cf911662SDavid S. Miller 	return rt;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2561da177e4SLinus Torvalds {
2571da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2581da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
259b3419363SDavid S. Miller 	struct inet_peer *peer = rt->rt6i_peer;
2601da177e4SLinus Torvalds 
2618e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
2628e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
2638e2ec639SYan, Zheng 
2641da177e4SLinus Torvalds 	if (idev != NULL) {
2651da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2661da177e4SLinus Torvalds 		in6_dev_put(idev);
2671da177e4SLinus Torvalds 	}
268b3419363SDavid S. Miller 	if (peer) {
269b3419363SDavid S. Miller 		rt->rt6i_peer = NULL;
270b3419363SDavid S. Miller 		inet_putpeer(peer);
271b3419363SDavid S. Miller 	}
272b3419363SDavid S. Miller }
273b3419363SDavid S. Miller 
2746431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
2756431cbc2SDavid S. Miller 
2766431cbc2SDavid S. Miller static u32 rt6_peer_genid(void)
2776431cbc2SDavid S. Miller {
2786431cbc2SDavid S. Miller 	return atomic_read(&__rt6_peer_genid);
2796431cbc2SDavid S. Miller }
2806431cbc2SDavid S. Miller 
281b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
282b3419363SDavid S. Miller {
283b3419363SDavid S. Miller 	struct inet_peer *peer;
284b3419363SDavid S. Miller 
285b3419363SDavid S. Miller 	peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
286b3419363SDavid S. Miller 	if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
287b3419363SDavid S. Miller 		inet_putpeer(peer);
2886431cbc2SDavid S. Miller 	else
2896431cbc2SDavid S. Miller 		rt->rt6i_peer_genid = rt6_peer_genid();
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
2931da177e4SLinus Torvalds 			   int how)
2941da177e4SLinus Torvalds {
2951da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2961da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2975a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
298c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
2991da177e4SLinus Torvalds 
3005a3e55d6SDenis V. Lunev 	if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
3015a3e55d6SDenis V. Lunev 		struct inet6_dev *loopback_idev =
3025a3e55d6SDenis V. Lunev 			in6_dev_get(loopback_dev);
3031da177e4SLinus Torvalds 		if (loopback_idev != NULL) {
3041da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
3051da177e4SLinus Torvalds 			in6_dev_put(idev);
3061da177e4SLinus Torvalds 		}
3071da177e4SLinus Torvalds 	}
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds static __inline__ int rt6_check_expired(const struct rt6_info *rt)
3111da177e4SLinus Torvalds {
312a02cec21SEric Dumazet 	return (rt->rt6i_flags & RTF_EXPIRES) &&
313a02cec21SEric Dumazet 		time_after(jiffies, rt->rt6i_expires);
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
316b71d1d42SEric Dumazet static inline int rt6_need_strict(const struct in6_addr *daddr)
317c71099acSThomas Graf {
318a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
319a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
320c71099acSThomas Graf }
321c71099acSThomas Graf 
3221da177e4SLinus Torvalds /*
323c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
3241da177e4SLinus Torvalds  */
3251da177e4SLinus Torvalds 
3268ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
3278ed67789SDaniel Lezcano 						    struct rt6_info *rt,
328b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
3291da177e4SLinus Torvalds 						    int oif,
330d420895eSYOSHIFUJI Hideaki 						    int flags)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
3331da177e4SLinus Torvalds 	struct rt6_info *sprt;
3341da177e4SLinus Torvalds 
335dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
336dd3abc4eSYOSHIFUJI Hideaki 		goto out;
337dd3abc4eSYOSHIFUJI Hideaki 
338d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
3391da177e4SLinus Torvalds 		struct net_device *dev = sprt->rt6i_dev;
340dd3abc4eSYOSHIFUJI Hideaki 
341dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
3421da177e4SLinus Torvalds 			if (dev->ifindex == oif)
3431da177e4SLinus Torvalds 				return sprt;
3441da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
3451da177e4SLinus Torvalds 				if (sprt->rt6i_idev == NULL ||
3461da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
347d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
3481da177e4SLinus Torvalds 						continue;
3491da177e4SLinus Torvalds 					if (local && (!oif ||
3501da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
3511da177e4SLinus Torvalds 						continue;
3521da177e4SLinus Torvalds 				}
3531da177e4SLinus Torvalds 				local = sprt;
3541da177e4SLinus Torvalds 			}
355dd3abc4eSYOSHIFUJI Hideaki 		} else {
356dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
357dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
358dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
359dd3abc4eSYOSHIFUJI Hideaki 		}
3601da177e4SLinus Torvalds 	}
3611da177e4SLinus Torvalds 
362dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
3631da177e4SLinus Torvalds 		if (local)
3641da177e4SLinus Torvalds 			return local;
3651da177e4SLinus Torvalds 
366d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
3678ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
3681da177e4SLinus Torvalds 	}
369dd3abc4eSYOSHIFUJI Hideaki out:
3701da177e4SLinus Torvalds 	return rt;
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
37327097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
37427097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
37527097255SYOSHIFUJI Hideaki {
376f2c31e32SEric Dumazet 	struct neighbour *neigh;
37727097255SYOSHIFUJI Hideaki 	/*
37827097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
37927097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
38027097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
38127097255SYOSHIFUJI Hideaki 	 *
38227097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
38327097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
38427097255SYOSHIFUJI Hideaki 	 */
385f2c31e32SEric Dumazet 	rcu_read_lock();
386f2c31e32SEric Dumazet 	neigh = rt ? dst_get_neighbour(&rt->dst) : NULL;
38727097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
388f2c31e32SEric Dumazet 		goto out;
38927097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
39027097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
39152e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
39227097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
39327097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
39427097255SYOSHIFUJI Hideaki 
39527097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
39627097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
39727097255SYOSHIFUJI Hideaki 
39827097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
39927097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
40027097255SYOSHIFUJI Hideaki 		ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
401f2c31e32SEric Dumazet 	} else {
40227097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
40327097255SYOSHIFUJI Hideaki 	}
404f2c31e32SEric Dumazet out:
405f2c31e32SEric Dumazet 	rcu_read_unlock();
406f2c31e32SEric Dumazet }
40727097255SYOSHIFUJI Hideaki #else
40827097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
40927097255SYOSHIFUJI Hideaki {
41027097255SYOSHIFUJI Hideaki }
41127097255SYOSHIFUJI Hideaki #endif
41227097255SYOSHIFUJI Hideaki 
4131da177e4SLinus Torvalds /*
414554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
4151da177e4SLinus Torvalds  */
416b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
4171da177e4SLinus Torvalds {
418554cfb7eSYOSHIFUJI Hideaki 	struct net_device *dev = rt->rt6i_dev;
419161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
420554cfb7eSYOSHIFUJI Hideaki 		return 2;
421161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
422161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
423161980f4SDavid S. Miller 		return 1;
424554cfb7eSYOSHIFUJI Hideaki 	return 0;
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
427b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
4281da177e4SLinus Torvalds {
429f2c31e32SEric Dumazet 	struct neighbour *neigh;
430398bcbebSYOSHIFUJI Hideaki 	int m;
431f2c31e32SEric Dumazet 
432f2c31e32SEric Dumazet 	rcu_read_lock();
433f2c31e32SEric Dumazet 	neigh = dst_get_neighbour(&rt->dst);
4344d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
4354d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
4364d0c5911SYOSHIFUJI Hideaki 		m = 1;
4374d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
4381da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
439554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
4404d0c5911SYOSHIFUJI Hideaki 			m = 2;
441398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
442398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
443398bcbebSYOSHIFUJI Hideaki 			m = 0;
444398bcbebSYOSHIFUJI Hideaki #endif
445398bcbebSYOSHIFUJI Hideaki 		else
446ea73ee23SYOSHIFUJI Hideaki 			m = 1;
4471da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
448398bcbebSYOSHIFUJI Hideaki 	} else
449398bcbebSYOSHIFUJI Hideaki 		m = 0;
450f2c31e32SEric Dumazet 	rcu_read_unlock();
451554cfb7eSYOSHIFUJI Hideaki 	return m;
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
454554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
455554cfb7eSYOSHIFUJI Hideaki 			   int strict)
456554cfb7eSYOSHIFUJI Hideaki {
4574d0c5911SYOSHIFUJI Hideaki 	int m, n;
4584d0c5911SYOSHIFUJI Hideaki 
4594d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
46077d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
461554cfb7eSYOSHIFUJI Hideaki 		return -1;
462ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
463ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
464ebacaaa0SYOSHIFUJI Hideaki #endif
4654d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
466557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
467554cfb7eSYOSHIFUJI Hideaki 		return -1;
468554cfb7eSYOSHIFUJI Hideaki 	return m;
469554cfb7eSYOSHIFUJI Hideaki }
470554cfb7eSYOSHIFUJI Hideaki 
471f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
472f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
473554cfb7eSYOSHIFUJI Hideaki {
474554cfb7eSYOSHIFUJI Hideaki 	int m;
475554cfb7eSYOSHIFUJI Hideaki 
476554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
477f11e6659SDavid S. Miller 		goto out;
478554cfb7eSYOSHIFUJI Hideaki 
479554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
480554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
481f11e6659SDavid S. Miller 		goto out;
482554cfb7eSYOSHIFUJI Hideaki 
483f11e6659SDavid S. Miller 	if (m > *mpri) {
484ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
48527097255SYOSHIFUJI Hideaki 			rt6_probe(match);
486f11e6659SDavid S. Miller 		*mpri = m;
487554cfb7eSYOSHIFUJI Hideaki 		match = rt;
488ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
48927097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
4901da177e4SLinus Torvalds 	}
491f11e6659SDavid S. Miller 
492f11e6659SDavid S. Miller out:
493f11e6659SDavid S. Miller 	return match;
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
496f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
497f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
498f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
499f11e6659SDavid S. Miller {
500f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
501f11e6659SDavid S. Miller 	int mpri = -1;
502f11e6659SDavid S. Miller 
503f11e6659SDavid S. Miller 	match = NULL;
504f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
505d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
506f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
507f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
508d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
509f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
510f11e6659SDavid S. Miller 
511f11e6659SDavid S. Miller 	return match;
512f11e6659SDavid S. Miller }
513f11e6659SDavid S. Miller 
514f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
515f11e6659SDavid S. Miller {
516f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
5178ed67789SDaniel Lezcano 	struct net *net;
518f11e6659SDavid S. Miller 
519f11e6659SDavid S. Miller 	RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
5200dc47877SHarvey Harrison 		  __func__, fn->leaf, oif);
521f11e6659SDavid S. Miller 
522f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
523f11e6659SDavid S. Miller 	if (!rt0)
524f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
525f11e6659SDavid S. Miller 
526f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
527f11e6659SDavid S. Miller 
528554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
529f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
530d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
531f11e6659SDavid S. Miller 
532554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
533f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
534f11e6659SDavid S. Miller 			next = fn->leaf;
535f11e6659SDavid S. Miller 
536f11e6659SDavid S. Miller 		if (next != rt0)
537f11e6659SDavid S. Miller 			fn->rr_ptr = next;
538554cfb7eSYOSHIFUJI Hideaki 	}
539554cfb7eSYOSHIFUJI Hideaki 
540f11e6659SDavid S. Miller 	RT6_TRACE("%s() => %p\n",
5410dc47877SHarvey Harrison 		  __func__, match);
542554cfb7eSYOSHIFUJI Hideaki 
543c346dca1SYOSHIFUJI Hideaki 	net = dev_net(rt0->rt6i_dev);
544a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
54770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
54870ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
549b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
55070ceb4f5SYOSHIFUJI Hideaki {
551c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
55270ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
55370ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
55470ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
5554bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
55670ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
55770ceb4f5SYOSHIFUJI Hideaki 
55870ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
55970ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
56070ceb4f5SYOSHIFUJI Hideaki 	}
56170ceb4f5SYOSHIFUJI Hideaki 
56270ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
56370ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
56470ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
56570ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
56670ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
56770ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
56870ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
56970ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
57070ceb4f5SYOSHIFUJI Hideaki 		}
57170ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
57270ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
57370ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
57470ceb4f5SYOSHIFUJI Hideaki 		}
57570ceb4f5SYOSHIFUJI Hideaki 	}
57670ceb4f5SYOSHIFUJI Hideaki 
57770ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
57870ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
5793933fc95SJens Rosenboom 		return -EINVAL;
58070ceb4f5SYOSHIFUJI Hideaki 
5814bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
58270ceb4f5SYOSHIFUJI Hideaki 
58370ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
58470ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
58570ceb4f5SYOSHIFUJI Hideaki 	else {
58670ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
58770ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
58870ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
58970ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
59070ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
59170ceb4f5SYOSHIFUJI Hideaki 	}
59270ceb4f5SYOSHIFUJI Hideaki 
593efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
594efa2cea0SDaniel Lezcano 				dev->ifindex);
59570ceb4f5SYOSHIFUJI Hideaki 
59670ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
597e0a1ad73SThomas Graf 		ip6_del_rt(rt);
59870ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
59970ceb4f5SYOSHIFUJI Hideaki 	}
60070ceb4f5SYOSHIFUJI Hideaki 
60170ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
602efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
60370ceb4f5SYOSHIFUJI Hideaki 					pref);
60470ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
60570ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
60670ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
60770ceb4f5SYOSHIFUJI Hideaki 
60870ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
6094bed72e4SYOSHIFUJI Hideaki 		if (!addrconf_finite_timeout(lifetime)) {
61070ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags &= ~RTF_EXPIRES;
61170ceb4f5SYOSHIFUJI Hideaki 		} else {
61270ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_expires = jiffies + HZ * lifetime;
61370ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_EXPIRES;
61470ceb4f5SYOSHIFUJI Hideaki 		}
615d8d1f30bSChangli Gao 		dst_release(&rt->dst);
61670ceb4f5SYOSHIFUJI Hideaki 	}
61770ceb4f5SYOSHIFUJI Hideaki 	return 0;
61870ceb4f5SYOSHIFUJI Hideaki }
61970ceb4f5SYOSHIFUJI Hideaki #endif
62070ceb4f5SYOSHIFUJI Hideaki 
6218ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
622982f56f3SYOSHIFUJI Hideaki do { \
6238ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
624982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
625e0eda7bbSVille Nuorvala 		while (1) { \
626982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
627c71099acSThomas Graf 				goto out; \
628982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
629982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
6308bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
631982f56f3SYOSHIFUJI Hideaki 			else \
632982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
633c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
634c71099acSThomas Graf 				goto restart; \
635c71099acSThomas Graf 		} \
636982f56f3SYOSHIFUJI Hideaki 	} \
637982f56f3SYOSHIFUJI Hideaki } while(0)
638c71099acSThomas Graf 
6398ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
6408ed67789SDaniel Lezcano 					     struct fib6_table *table,
6414c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
6421da177e4SLinus Torvalds {
6431da177e4SLinus Torvalds 	struct fib6_node *fn;
6441da177e4SLinus Torvalds 	struct rt6_info *rt;
6451da177e4SLinus Torvalds 
646c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
6474c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
648c71099acSThomas Graf restart:
649c71099acSThomas Graf 	rt = fn->leaf;
6504c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
6514c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
652c71099acSThomas Graf out:
653d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
654c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
6551da177e4SLinus Torvalds 	return rt;
656c71099acSThomas Graf 
657c71099acSThomas Graf }
658c71099acSThomas Graf 
6599acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
6609acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
661c71099acSThomas Graf {
6624c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
6634c9483b2SDavid S. Miller 		.flowi6_oif = oif,
6644c9483b2SDavid S. Miller 		.daddr = *daddr,
665c71099acSThomas Graf 	};
666c71099acSThomas Graf 	struct dst_entry *dst;
66777d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
668c71099acSThomas Graf 
669adaa70bbSThomas Graf 	if (saddr) {
6704c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
671adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
672adaa70bbSThomas Graf 	}
673adaa70bbSThomas Graf 
6744c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
675c71099acSThomas Graf 	if (dst->error == 0)
676c71099acSThomas Graf 		return (struct rt6_info *) dst;
677c71099acSThomas Graf 
678c71099acSThomas Graf 	dst_release(dst);
679c71099acSThomas Graf 
6801da177e4SLinus Torvalds 	return NULL;
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
6837159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
6847159039aSYOSHIFUJI Hideaki 
685c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
6861da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
6871da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
6881da177e4SLinus Torvalds    be destroyed.
6891da177e4SLinus Torvalds  */
6901da177e4SLinus Torvalds 
69186872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
6921da177e4SLinus Torvalds {
6931da177e4SLinus Torvalds 	int err;
694c71099acSThomas Graf 	struct fib6_table *table;
6951da177e4SLinus Torvalds 
696c71099acSThomas Graf 	table = rt->rt6i_table;
697c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
69886872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
699c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	return err;
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
70440e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
70540e22e8fSThomas Graf {
7064d1169c1SDenis V. Lunev 	struct nl_info info = {
707c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
7084d1169c1SDenis V. Lunev 	};
709528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
71040e22e8fSThomas Graf }
71140e22e8fSThomas Graf 
71221efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_cow(const struct rt6_info *ort,
71321efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
714b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
7151da177e4SLinus Torvalds {
7161da177e4SLinus Torvalds 	struct rt6_info *rt;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	/*
7191da177e4SLinus Torvalds 	 *	Clone the route.
7201da177e4SLinus Torvalds 	 */
7211da177e4SLinus Torvalds 
72221efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	if (rt) {
72514deae41SDavid S. Miller 		struct neighbour *neigh;
72614deae41SDavid S. Miller 		int attempts = !in_softirq();
72714deae41SDavid S. Miller 
72858c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags&RTF_GATEWAY)) {
72958c4fb86SYOSHIFUJI Hideaki 			if (rt->rt6i_dst.plen != 128 &&
73021efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
73158c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
7321da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_gateway, daddr);
73358c4fb86SYOSHIFUJI Hideaki 		}
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
7381da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
7391da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
7401da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
7411da177e4SLinus Torvalds 		}
7421da177e4SLinus Torvalds #endif
7431da177e4SLinus Torvalds 
74414deae41SDavid S. Miller 	retry:
74514deae41SDavid S. Miller 		neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
74614deae41SDavid S. Miller 		if (IS_ERR(neigh)) {
74714deae41SDavid S. Miller 			struct net *net = dev_net(rt->rt6i_dev);
74814deae41SDavid S. Miller 			int saved_rt_min_interval =
74914deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval;
75014deae41SDavid S. Miller 			int saved_rt_elasticity =
75114deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity;
75214deae41SDavid S. Miller 
75314deae41SDavid S. Miller 			if (attempts-- > 0) {
75414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
75514deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
75614deae41SDavid S. Miller 
75786393e52SAlexey Dobriyan 				ip6_dst_gc(&net->ipv6.ip6_dst_ops);
75814deae41SDavid S. Miller 
75914deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity =
76014deae41SDavid S. Miller 					saved_rt_elasticity;
76114deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval =
76214deae41SDavid S. Miller 					saved_rt_min_interval;
76314deae41SDavid S. Miller 				goto retry;
76414deae41SDavid S. Miller 			}
76514deae41SDavid S. Miller 
76614deae41SDavid S. Miller 			if (net_ratelimit())
76714deae41SDavid S. Miller 				printk(KERN_WARNING
7687e1b33e5SUlrich Weber 				       "ipv6: Neighbour table overflow.\n");
769d8d1f30bSChangli Gao 			dst_free(&rt->dst);
77014deae41SDavid S. Miller 			return NULL;
77114deae41SDavid S. Miller 		}
77269cce1d1SDavid S. Miller 		dst_set_neighbour(&rt->dst, neigh);
7731da177e4SLinus Torvalds 
77495a9a5baSYOSHIFUJI Hideaki 	}
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	return rt;
7771da177e4SLinus Torvalds }
77895a9a5baSYOSHIFUJI Hideaki 
77921efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
78021efcfa0SEric Dumazet 					const struct in6_addr *daddr)
781299d9939SYOSHIFUJI Hideaki {
78221efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
78321efcfa0SEric Dumazet 
784299d9939SYOSHIFUJI Hideaki 	if (rt) {
785299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
786f2c31e32SEric Dumazet 		dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_raw(&ort->dst)));
787299d9939SYOSHIFUJI Hideaki 	}
788299d9939SYOSHIFUJI Hideaki 	return rt;
789299d9939SYOSHIFUJI Hideaki }
790299d9939SYOSHIFUJI Hideaki 
7918ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
7924c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
7931da177e4SLinus Torvalds {
7941da177e4SLinus Torvalds 	struct fib6_node *fn;
795519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
796c71099acSThomas Graf 	int strict = 0;
7971da177e4SLinus Torvalds 	int attempts = 3;
798519fbd87SYOSHIFUJI Hideaki 	int err;
79953b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
8001da177e4SLinus Torvalds 
80177d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds relookup:
804c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8051da177e4SLinus Torvalds 
8068238dd06SYOSHIFUJI Hideaki restart_2:
8074c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds restart:
8104acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
8118ed67789SDaniel Lezcano 
8124c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
8138ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
8148238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
8151da177e4SLinus Torvalds 		goto out;
8161da177e4SLinus Torvalds 
817d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
818c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8191da177e4SLinus Torvalds 
820f2c31e32SEric Dumazet 	if (!dst_get_neighbour_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
8214c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
8227343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
8234c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
8247343ff31SDavid S. Miller 	else
8257343ff31SDavid S. Miller 		goto out2;
8261da177e4SLinus Torvalds 
827d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8288ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
8291da177e4SLinus Torvalds 
830d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
831e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
83240e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
833e40cf353SYOSHIFUJI Hideaki 		if (!err)
834e40cf353SYOSHIFUJI Hideaki 			goto out2;
835e40cf353SYOSHIFUJI Hideaki 	}
836e40cf353SYOSHIFUJI Hideaki 
837e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
8381da177e4SLinus Torvalds 		goto out2;
8391da177e4SLinus Torvalds 
840519fbd87SYOSHIFUJI Hideaki 	/*
841c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
842519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
8431da177e4SLinus Torvalds 	 */
844d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8451da177e4SLinus Torvalds 	goto relookup;
846e40cf353SYOSHIFUJI Hideaki 
847519fbd87SYOSHIFUJI Hideaki out:
8488238dd06SYOSHIFUJI Hideaki 	if (reachable) {
8498238dd06SYOSHIFUJI Hideaki 		reachable = 0;
8508238dd06SYOSHIFUJI Hideaki 		goto restart_2;
8518238dd06SYOSHIFUJI Hideaki 	}
852d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
853c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8541da177e4SLinus Torvalds out2:
855d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
856d8d1f30bSChangli Gao 	rt->dst.__use++;
857c71099acSThomas Graf 
858c71099acSThomas Graf 	return rt;
859c71099acSThomas Graf }
860c71099acSThomas Graf 
8618ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
8624c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
8634acad72dSPavel Emelyanov {
8644c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
8654acad72dSPavel Emelyanov }
8664acad72dSPavel Emelyanov 
867c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
868c71099acSThomas Graf {
869b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
870c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
871adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
8724c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
8734c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
8744c9483b2SDavid S. Miller 		.daddr = iph->daddr,
8754c9483b2SDavid S. Miller 		.saddr = iph->saddr,
8764c9483b2SDavid S. Miller 		.flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
8774c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
8784c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
879c71099acSThomas Graf 	};
880adaa70bbSThomas Graf 
8811d6e55f1SThomas Goff 	if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
882adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
883c71099acSThomas Graf 
8844c9483b2SDavid S. Miller 	skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input));
885c71099acSThomas Graf }
886c71099acSThomas Graf 
8878ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
8884c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
889c71099acSThomas Graf {
8904c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
891c71099acSThomas Graf }
892c71099acSThomas Graf 
8939c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
8944c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
895c71099acSThomas Graf {
896c71099acSThomas Graf 	int flags = 0;
897c71099acSThomas Graf 
8984c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
89977d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
900c71099acSThomas Graf 
9014c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
902adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
9030c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
9040c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
905adaa70bbSThomas Graf 
9064c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
9097159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
9101da177e4SLinus Torvalds 
9112774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
91214e50e57SDavid S. Miller {
9135c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
91414e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
91514e50e57SDavid S. Miller 
9165c1e6aa3SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
91714e50e57SDavid S. Miller 	if (rt) {
918cf911662SDavid S. Miller 		memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
919cf911662SDavid S. Miller 
920d8d1f30bSChangli Gao 		new = &rt->dst;
92114e50e57SDavid S. Miller 
92214e50e57SDavid S. Miller 		new->__use = 1;
923352e512cSHerbert Xu 		new->input = dst_discard;
924352e512cSHerbert Xu 		new->output = dst_discard;
92514e50e57SDavid S. Miller 
92621efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
92721efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
92821efcfa0SEric Dumazet 		else
929defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
93014e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
93114e50e57SDavid S. Miller 		if (rt->rt6i_idev)
93214e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
93314e50e57SDavid S. Miller 		rt->rt6i_expires = 0;
93414e50e57SDavid S. Miller 
93514e50e57SDavid S. Miller 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
93614e50e57SDavid S. Miller 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
93714e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
93814e50e57SDavid S. Miller 
93914e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
94014e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
94114e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
94214e50e57SDavid S. Miller #endif
94314e50e57SDavid S. Miller 
94414e50e57SDavid S. Miller 		dst_free(new);
94514e50e57SDavid S. Miller 	}
94614e50e57SDavid S. Miller 
94769ead7afSDavid S. Miller 	dst_release(dst_orig);
94869ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
94914e50e57SDavid S. Miller }
95014e50e57SDavid S. Miller 
9511da177e4SLinus Torvalds /*
9521da177e4SLinus Torvalds  *	Destination cache support functions
9531da177e4SLinus Torvalds  */
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
9561da177e4SLinus Torvalds {
9571da177e4SLinus Torvalds 	struct rt6_info *rt;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
9601da177e4SLinus Torvalds 
9616431cbc2SDavid S. Miller 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
9626431cbc2SDavid S. Miller 		if (rt->rt6i_peer_genid != rt6_peer_genid()) {
9636431cbc2SDavid S. Miller 			if (!rt->rt6i_peer)
9646431cbc2SDavid S. Miller 				rt6_bind_peer(rt, 0);
9656431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
9666431cbc2SDavid S. Miller 		}
9671da177e4SLinus Torvalds 		return dst;
9686431cbc2SDavid S. Miller 	}
9691da177e4SLinus Torvalds 	return NULL;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
9731da177e4SLinus Torvalds {
9741da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	if (rt) {
97754c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
97854c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
979e0a1ad73SThomas Graf 				ip6_del_rt(rt);
98054c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
9811da177e4SLinus Torvalds 			}
98254c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
98354c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
98454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
98554c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
98654c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
98754c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
9911da177e4SLinus Torvalds {
9921da177e4SLinus Torvalds 	struct rt6_info *rt;
9931da177e4SLinus Torvalds 
9943ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
9951da177e4SLinus Torvalds 
996adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
9971da177e4SLinus Torvalds 	if (rt) {
9981da177e4SLinus Torvalds 		if (rt->rt6i_flags&RTF_CACHE) {
999d8d1f30bSChangli Gao 			dst_set_expires(&rt->dst, 0);
10001da177e4SLinus Torvalds 			rt->rt6i_flags |= RTF_EXPIRES;
10011da177e4SLinus Torvalds 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
10021da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
10031da177e4SLinus Torvalds 	}
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
10071da177e4SLinus Torvalds {
10081da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
10111da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
10121da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1013defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
10141da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1015defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1016defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
10171da177e4SLinus Torvalds 		}
1018defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
10191da177e4SLinus Torvalds 	}
10201da177e4SLinus Torvalds }
10211da177e4SLinus Torvalds 
10220dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
10231da177e4SLinus Torvalds {
10240dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
10250dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
10260dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
10270dbaee3bSDavid S. Miller 
10281da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
10291da177e4SLinus Torvalds 
10305578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
10315578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	/*
10341da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
10351da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
10361da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
10371da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
10381da177e4SLinus Torvalds 	 */
10391da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
10401da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
10411da177e4SLinus Torvalds 	return mtu;
10421da177e4SLinus Torvalds }
10431da177e4SLinus Torvalds 
1044d33e4553SDavid S. Miller static unsigned int ip6_default_mtu(const struct dst_entry *dst)
1045d33e4553SDavid S. Miller {
1046d33e4553SDavid S. Miller 	unsigned int mtu = IPV6_MIN_MTU;
1047d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1048d33e4553SDavid S. Miller 
1049d33e4553SDavid S. Miller 	rcu_read_lock();
1050d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1051d33e4553SDavid S. Miller 	if (idev)
1052d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1053d33e4553SDavid S. Miller 	rcu_read_unlock();
1054d33e4553SDavid S. Miller 
1055d33e4553SDavid S. Miller 	return mtu;
1056d33e4553SDavid S. Miller }
1057d33e4553SDavid S. Miller 
10583b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
10593b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
10605d0bbeebSThomas Graf 
10613b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
10621da177e4SLinus Torvalds 				  struct neighbour *neigh,
10639acd9f3aSYOSHIFUJI Hideaki 				  const struct in6_addr *addr)
10641da177e4SLinus Torvalds {
10651da177e4SLinus Torvalds 	struct rt6_info *rt;
10661da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1067c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	if (unlikely(idev == NULL))
10701da177e4SLinus Torvalds 		return NULL;
10711da177e4SLinus Torvalds 
1072957c665fSDavid S. Miller 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev, 0);
10731da177e4SLinus Torvalds 	if (unlikely(rt == NULL)) {
10741da177e4SLinus Torvalds 		in6_dev_put(idev);
10751da177e4SLinus Torvalds 		goto out;
10761da177e4SLinus Torvalds 	}
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	if (neigh)
10791da177e4SLinus Torvalds 		neigh_hold(neigh);
108014deae41SDavid S. Miller 	else {
10811da177e4SLinus Torvalds 		neigh = ndisc_get_neigh(dev, addr);
108214deae41SDavid S. Miller 		if (IS_ERR(neigh))
108314deae41SDavid S. Miller 			neigh = NULL;
108414deae41SDavid S. Miller 	}
10851da177e4SLinus Torvalds 
10868e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
10878e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
108869cce1d1SDavid S. Miller 	dst_set_neighbour(&rt->dst, neigh);
1089d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
1090defb3519SDavid S. Miller 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
10918e2ec639SYan, Zheng 
10928e2ec639SYan, Zheng 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
10938e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
10948e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
10951da177e4SLinus Torvalds 
10963b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1097d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1098d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
10993b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
11001da177e4SLinus Torvalds 
11015578689aSDaniel Lezcano 	fib6_force_start_gc(net);
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds out:
1104d8d1f30bSChangli Gao 	return &rt->dst;
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11073d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
11081da177e4SLinus Torvalds {
1109e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
11103d0f24a7SStephen Hemminger 	int more = 0;
11111da177e4SLinus Torvalds 
11123b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
11133b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
11145d0bbeebSThomas Graf 
11151da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
11161da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
11171da177e4SLinus Torvalds 			*pprev = dst->next;
11181da177e4SLinus Torvalds 			dst_free(dst);
11191da177e4SLinus Torvalds 		} else {
11201da177e4SLinus Torvalds 			pprev = &dst->next;
11213d0f24a7SStephen Hemminger 			++more;
11221da177e4SLinus Torvalds 		}
11231da177e4SLinus Torvalds 	}
11241da177e4SLinus Torvalds 
11253b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
11265d0bbeebSThomas Graf 
11273d0f24a7SStephen Hemminger 	return more;
11281da177e4SLinus Torvalds }
11291da177e4SLinus Torvalds 
11301e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
11311e493d19SDavid S. Miller 			    void *arg)
11321e493d19SDavid S. Miller {
11331e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
11341e493d19SDavid S. Miller 
11351e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
11361e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
11371e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
11381e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
11391e493d19SDavid S. Miller 		if (func(rt, arg)) {
11401e493d19SDavid S. Miller 			*pprev = dst->next;
11411e493d19SDavid S. Miller 			dst_free(dst);
11421e493d19SDavid S. Miller 		} else {
11431e493d19SDavid S. Miller 			pprev = &dst->next;
11441e493d19SDavid S. Miller 		}
11451e493d19SDavid S. Miller 	}
11461e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
11471e493d19SDavid S. Miller }
11481e493d19SDavid S. Miller 
1149569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
11501da177e4SLinus Torvalds {
11511da177e4SLinus Torvalds 	unsigned long now = jiffies;
115286393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
11537019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
11547019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
11557019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
11567019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
11577019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1158fc66f95cSEric Dumazet 	int entries;
11591da177e4SLinus Torvalds 
1160fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
11617019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1162fc66f95cSEric Dumazet 	    entries <= rt_max_size)
11631da177e4SLinus Torvalds 		goto out;
11641da177e4SLinus Torvalds 
11656891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
11666891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
11676891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1168fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1169fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
11707019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
11711da177e4SLinus Torvalds out:
11727019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1173fc66f95cSEric Dumazet 	return entries > rt_max_size;
11741da177e4SLinus Torvalds }
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
11771da177e4SLinus Torvalds    but results in cleaner routing tables.
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds    Remove it only when all the things will work!
11801da177e4SLinus Torvalds  */
11811da177e4SLinus Torvalds 
11826b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
11831da177e4SLinus Torvalds {
11845170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1185a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
11866b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1187c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1188c68f24ccSEric Dumazet 
1189c68f24ccSEric Dumazet 		rcu_read_lock();
1190c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1191c68f24ccSEric Dumazet 		if (idev)
11921da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1193c68f24ccSEric Dumazet 		else
119453b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1195c68f24ccSEric Dumazet 		rcu_read_unlock();
11961da177e4SLinus Torvalds 	}
11971da177e4SLinus Torvalds 	return hoplimit;
11981da177e4SLinus Torvalds }
1199abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds /*
12021da177e4SLinus Torvalds  *
12031da177e4SLinus Torvalds  */
12041da177e4SLinus Torvalds 
120586872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
12061da177e4SLinus Torvalds {
12071da177e4SLinus Torvalds 	int err;
12085578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
12091da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
12101da177e4SLinus Torvalds 	struct net_device *dev = NULL;
12111da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1212c71099acSThomas Graf 	struct fib6_table *table;
12131da177e4SLinus Torvalds 	int addr_type;
12141da177e4SLinus Torvalds 
121586872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
12161da177e4SLinus Torvalds 		return -EINVAL;
12171da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
121886872cb5SThomas Graf 	if (cfg->fc_src_len)
12191da177e4SLinus Torvalds 		return -EINVAL;
12201da177e4SLinus Torvalds #endif
122186872cb5SThomas Graf 	if (cfg->fc_ifindex) {
12221da177e4SLinus Torvalds 		err = -ENODEV;
12235578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
12241da177e4SLinus Torvalds 		if (!dev)
12251da177e4SLinus Torvalds 			goto out;
12261da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
12271da177e4SLinus Torvalds 		if (!idev)
12281da177e4SLinus Torvalds 			goto out;
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 
123186872cb5SThomas Graf 	if (cfg->fc_metric == 0)
123286872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
12331da177e4SLinus Torvalds 
12345578689aSDaniel Lezcano 	table = fib6_new_table(net, cfg->fc_table);
1235c71099acSThomas Graf 	if (table == NULL) {
1236c71099acSThomas Graf 		err = -ENOBUFS;
1237c71099acSThomas Graf 		goto out;
1238c71099acSThomas Graf 	}
1239c71099acSThomas Graf 
1240957c665fSDavid S. Miller 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT);
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds 	if (rt == NULL) {
12431da177e4SLinus Torvalds 		err = -ENOMEM;
12441da177e4SLinus Torvalds 		goto out;
12451da177e4SLinus Torvalds 	}
12461da177e4SLinus Torvalds 
1247d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
12486f704992SYOSHIFUJI Hideaki 	rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
12496f704992SYOSHIFUJI Hideaki 				jiffies + clock_t_to_jiffies(cfg->fc_expires) :
12506f704992SYOSHIFUJI Hideaki 				0;
12511da177e4SLinus Torvalds 
125286872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
125386872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
125486872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
125586872cb5SThomas Graf 
125686872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1259d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1260ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1261ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
12621da177e4SLinus Torvalds 	else
1263d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
12641da177e4SLinus Torvalds 
1265d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
12661da177e4SLinus Torvalds 
126786872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
126886872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
12691da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
127011d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
12711da177e4SLinus Torvalds 
12728e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
12738e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
12748e2ec639SYan, Zheng 		if (!metrics) {
12758e2ec639SYan, Zheng 			err = -ENOMEM;
12768e2ec639SYan, Zheng 			goto out;
12778e2ec639SYan, Zheng 		}
12788e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
12798e2ec639SYan, Zheng 	}
12801da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
128186872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
128286872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
12831da177e4SLinus Torvalds #endif
12841da177e4SLinus Torvalds 
128586872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
12881da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
12891da177e4SLinus Torvalds 	 */
129086872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
1291ab79ad14SMaciej Żenczykowski 	    (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1292ab79ad14SMaciej Żenczykowski 					      && !(cfg->fc_flags&RTF_LOCAL))) {
12931da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
12945578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
12951da177e4SLinus Torvalds 			if (dev) {
12961da177e4SLinus Torvalds 				dev_put(dev);
12971da177e4SLinus Torvalds 				in6_dev_put(idev);
12981da177e4SLinus Torvalds 			}
12995578689aSDaniel Lezcano 			dev = net->loopback_dev;
13001da177e4SLinus Torvalds 			dev_hold(dev);
13011da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
13021da177e4SLinus Torvalds 			if (!idev) {
13031da177e4SLinus Torvalds 				err = -ENODEV;
13041da177e4SLinus Torvalds 				goto out;
13051da177e4SLinus Torvalds 			}
13061da177e4SLinus Torvalds 		}
1307d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1308d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
1309d8d1f30bSChangli Gao 		rt->dst.error = -ENETUNREACH;
13101da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
13111da177e4SLinus Torvalds 		goto install_route;
13121da177e4SLinus Torvalds 	}
13131da177e4SLinus Torvalds 
131486872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1315b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
13161da177e4SLinus Torvalds 		int gwa_type;
13171da177e4SLinus Torvalds 
131886872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
131986872cb5SThomas Graf 		ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
13201da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
13231da177e4SLinus Torvalds 			struct rt6_info *grt;
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
13261da177e4SLinus Torvalds 			   addresses as nexthop address.
13271da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
13281da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
13291da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
13301da177e4SLinus Torvalds 			   some exceptions. --ANK
13311da177e4SLinus Torvalds 			 */
13321da177e4SLinus Torvalds 			err = -EINVAL;
13331da177e4SLinus Torvalds 			if (!(gwa_type&IPV6_ADDR_UNICAST))
13341da177e4SLinus Torvalds 				goto out;
13351da177e4SLinus Torvalds 
13365578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
13391da177e4SLinus Torvalds 			if (grt == NULL)
13401da177e4SLinus Torvalds 				goto out;
13411da177e4SLinus Torvalds 			if (dev) {
13421da177e4SLinus Torvalds 				if (dev != grt->rt6i_dev) {
1343d8d1f30bSChangli Gao 					dst_release(&grt->dst);
13441da177e4SLinus Torvalds 					goto out;
13451da177e4SLinus Torvalds 				}
13461da177e4SLinus Torvalds 			} else {
13471da177e4SLinus Torvalds 				dev = grt->rt6i_dev;
13481da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
13491da177e4SLinus Torvalds 				dev_hold(dev);
13501da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
13511da177e4SLinus Torvalds 			}
13521da177e4SLinus Torvalds 			if (!(grt->rt6i_flags&RTF_GATEWAY))
13531da177e4SLinus Torvalds 				err = 0;
1354d8d1f30bSChangli Gao 			dst_release(&grt->dst);
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 			if (err)
13571da177e4SLinus Torvalds 				goto out;
13581da177e4SLinus Torvalds 		}
13591da177e4SLinus Torvalds 		err = -EINVAL;
13601da177e4SLinus Torvalds 		if (dev == NULL || (dev->flags&IFF_LOOPBACK))
13611da177e4SLinus Torvalds 			goto out;
13621da177e4SLinus Torvalds 	}
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	err = -ENODEV;
13651da177e4SLinus Torvalds 	if (dev == NULL)
13661da177e4SLinus Torvalds 		goto out;
13671da177e4SLinus Torvalds 
1368c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1369c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1370c3968a85SDaniel Walter 			err = -EINVAL;
1371c3968a85SDaniel Walter 			goto out;
1372c3968a85SDaniel Walter 		}
1373c3968a85SDaniel Walter 		ipv6_addr_copy(&rt->rt6i_prefsrc.addr, &cfg->fc_prefsrc);
1374c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1375c3968a85SDaniel Walter 	} else
1376c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1377c3968a85SDaniel Walter 
137886872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
137969cce1d1SDavid S. Miller 		struct neighbour *n = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
138069cce1d1SDavid S. Miller 		if (IS_ERR(n)) {
138169cce1d1SDavid S. Miller 			err = PTR_ERR(n);
13821da177e4SLinus Torvalds 			goto out;
13831da177e4SLinus Torvalds 		}
138469cce1d1SDavid S. Miller 		dst_set_neighbour(&rt->dst, n);
13851da177e4SLinus Torvalds 	}
13861da177e4SLinus Torvalds 
138786872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds install_route:
139086872cb5SThomas Graf 	if (cfg->fc_mx) {
139186872cb5SThomas Graf 		struct nlattr *nla;
139286872cb5SThomas Graf 		int remaining;
13931da177e4SLinus Torvalds 
139486872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
13958f4c1f9bSThomas Graf 			int type = nla_type(nla);
139686872cb5SThomas Graf 
139786872cb5SThomas Graf 			if (type) {
139886872cb5SThomas Graf 				if (type > RTAX_MAX) {
13991da177e4SLinus Torvalds 					err = -EINVAL;
14001da177e4SLinus Torvalds 					goto out;
14011da177e4SLinus Torvalds 				}
140286872cb5SThomas Graf 
1403defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
14041da177e4SLinus Torvalds 			}
14051da177e4SLinus Torvalds 		}
14061da177e4SLinus Torvalds 	}
14071da177e4SLinus Torvalds 
1408d8d1f30bSChangli Gao 	rt->dst.dev = dev;
14091da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1410c71099acSThomas Graf 	rt->rt6i_table = table;
141163152fc0SDaniel Lezcano 
1412c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
141363152fc0SDaniel Lezcano 
141486872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds out:
14171da177e4SLinus Torvalds 	if (dev)
14181da177e4SLinus Torvalds 		dev_put(dev);
14191da177e4SLinus Torvalds 	if (idev)
14201da177e4SLinus Torvalds 		in6_dev_put(idev);
14211da177e4SLinus Torvalds 	if (rt)
1422d8d1f30bSChangli Gao 		dst_free(&rt->dst);
14231da177e4SLinus Torvalds 	return err;
14241da177e4SLinus Torvalds }
14251da177e4SLinus Torvalds 
142686872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
14271da177e4SLinus Torvalds {
14281da177e4SLinus Torvalds 	int err;
1429c71099acSThomas Graf 	struct fib6_table *table;
1430c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(rt->rt6i_dev);
14311da177e4SLinus Torvalds 
14328ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry)
14336c813a72SPatrick McHardy 		return -ENOENT;
14346c813a72SPatrick McHardy 
1435c71099acSThomas Graf 	table = rt->rt6i_table;
1436c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
14371da177e4SLinus Torvalds 
143886872cb5SThomas Graf 	err = fib6_del(rt, info);
1439d8d1f30bSChangli Gao 	dst_release(&rt->dst);
14401da177e4SLinus Torvalds 
1441c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds 	return err;
14441da177e4SLinus Torvalds }
14451da177e4SLinus Torvalds 
1446e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1447e0a1ad73SThomas Graf {
14484d1169c1SDenis V. Lunev 	struct nl_info info = {
1449c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
14504d1169c1SDenis V. Lunev 	};
1451528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1452e0a1ad73SThomas Graf }
1453e0a1ad73SThomas Graf 
145486872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
14551da177e4SLinus Torvalds {
1456c71099acSThomas Graf 	struct fib6_table *table;
14571da177e4SLinus Torvalds 	struct fib6_node *fn;
14581da177e4SLinus Torvalds 	struct rt6_info *rt;
14591da177e4SLinus Torvalds 	int err = -ESRCH;
14601da177e4SLinus Torvalds 
14615578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
1462c71099acSThomas Graf 	if (table == NULL)
1463c71099acSThomas Graf 		return err;
14641da177e4SLinus Torvalds 
1465c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1466c71099acSThomas Graf 
1467c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
146886872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
146986872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 	if (fn) {
1472d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
147386872cb5SThomas Graf 			if (cfg->fc_ifindex &&
14741da177e4SLinus Torvalds 			    (rt->rt6i_dev == NULL ||
147586872cb5SThomas Graf 			     rt->rt6i_dev->ifindex != cfg->fc_ifindex))
14761da177e4SLinus Torvalds 				continue;
147786872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
147886872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
14791da177e4SLinus Torvalds 				continue;
148086872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
14811da177e4SLinus Torvalds 				continue;
1482d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1483c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
14841da177e4SLinus Torvalds 
148586872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
14861da177e4SLinus Torvalds 		}
14871da177e4SLinus Torvalds 	}
1488c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 	return err;
14911da177e4SLinus Torvalds }
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds /*
14941da177e4SLinus Torvalds  *	Handle redirects
14951da177e4SLinus Torvalds  */
1496a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi {
14974c9483b2SDavid S. Miller 	struct flowi6 fl6;
1498a6279458SYOSHIFUJI Hideaki 	struct in6_addr gateway;
1499a6279458SYOSHIFUJI Hideaki };
15001da177e4SLinus Torvalds 
15018ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net,
15028ed67789SDaniel Lezcano 					     struct fib6_table *table,
15034c9483b2SDavid S. Miller 					     struct flowi6 *fl6,
1504a6279458SYOSHIFUJI Hideaki 					     int flags)
1505a6279458SYOSHIFUJI Hideaki {
15064c9483b2SDavid S. Miller 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1507a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt;
1508a6279458SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1509c71099acSThomas Graf 
1510e843b9e1SYOSHIFUJI Hideaki 	/*
1511e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1512e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1513e843b9e1SYOSHIFUJI Hideaki 	 *
1514e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1515e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1516e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1517e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1518e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1519e843b9e1SYOSHIFUJI Hideaki 	 */
15201da177e4SLinus Torvalds 
1521c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
15224c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1523e843b9e1SYOSHIFUJI Hideaki restart:
1524d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
15251da177e4SLinus Torvalds 		/*
15261da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
15271da177e4SLinus Torvalds 		 *
15281da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
15291da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
15301da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
15311da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
15321da177e4SLinus Torvalds 		 */
1533e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1534e843b9e1SYOSHIFUJI Hideaki 			continue;
15351da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1536e843b9e1SYOSHIFUJI Hideaki 			continue;
15374c9483b2SDavid S. Miller 		if (fl6->flowi6_oif != rt->rt6i_dev->ifindex)
1538e843b9e1SYOSHIFUJI Hideaki 			continue;
1539a6279458SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1540e843b9e1SYOSHIFUJI Hideaki 			continue;
1541e843b9e1SYOSHIFUJI Hideaki 		break;
1542e843b9e1SYOSHIFUJI Hideaki 	}
1543a6279458SYOSHIFUJI Hideaki 
1544cb15d9c2SYOSHIFUJI Hideaki 	if (!rt)
15458ed67789SDaniel Lezcano 		rt = net->ipv6.ip6_null_entry;
15464c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
1547cb15d9c2SYOSHIFUJI Hideaki out:
1548d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
1549a6279458SYOSHIFUJI Hideaki 
1550c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
15511da177e4SLinus Torvalds 
1552a6279458SYOSHIFUJI Hideaki 	return rt;
1553a6279458SYOSHIFUJI Hideaki };
1554a6279458SYOSHIFUJI Hideaki 
1555b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1556b71d1d42SEric Dumazet 					   const struct in6_addr *src,
1557b71d1d42SEric Dumazet 					   const struct in6_addr *gateway,
1558a6279458SYOSHIFUJI Hideaki 					   struct net_device *dev)
1559a6279458SYOSHIFUJI Hideaki {
1560adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1561c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
1562a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi rdfl = {
15634c9483b2SDavid S. Miller 		.fl6 = {
15644c9483b2SDavid S. Miller 			.flowi6_oif = dev->ifindex,
15654c9483b2SDavid S. Miller 			.daddr = *dest,
15664c9483b2SDavid S. Miller 			.saddr = *src,
1567a6279458SYOSHIFUJI Hideaki 		},
1568a6279458SYOSHIFUJI Hideaki 	};
1569adaa70bbSThomas Graf 
157086c36ce4SBrian Haley 	ipv6_addr_copy(&rdfl.gateway, gateway);
157186c36ce4SBrian Haley 
1572adaa70bbSThomas Graf 	if (rt6_need_strict(dest))
1573adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
1574a6279458SYOSHIFUJI Hideaki 
15754c9483b2SDavid S. Miller 	return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
157658f09b78SDaniel Lezcano 						   flags, __ip6_route_redirect);
1577a6279458SYOSHIFUJI Hideaki }
1578a6279458SYOSHIFUJI Hideaki 
1579b71d1d42SEric Dumazet void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1580b71d1d42SEric Dumazet 		  const struct in6_addr *saddr,
1581a6279458SYOSHIFUJI Hideaki 		  struct neighbour *neigh, u8 *lladdr, int on_link)
1582a6279458SYOSHIFUJI Hideaki {
1583a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt = NULL;
1584a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1585c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(neigh->dev);
1586a6279458SYOSHIFUJI Hideaki 
1587a6279458SYOSHIFUJI Hideaki 	rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1588a6279458SYOSHIFUJI Hideaki 
15898ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry) {
15901da177e4SLinus Torvalds 		if (net_ratelimit())
15911da177e4SLinus Torvalds 			printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
15921da177e4SLinus Torvalds 			       "for redirect target\n");
1593a6279458SYOSHIFUJI Hideaki 		goto out;
15941da177e4SLinus Torvalds 	}
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 	/*
15971da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
15981da177e4SLinus Torvalds 	 */
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
16011da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
16021da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
16031da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
16041da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
16051da177e4SLinus Torvalds 		     );
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 	/*
16081da177e4SLinus Torvalds 	 * Redirect received -> path was valid.
16091da177e4SLinus Torvalds 	 * Look, redirects are sent only in response to data packets,
16101da177e4SLinus Torvalds 	 * so that this nexthop apparently is reachable. --ANK
16111da177e4SLinus Torvalds 	 */
1612d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 	/* Duplicate redirect: silently ignore. */
1615f2c31e32SEric Dumazet 	if (neigh == dst_get_neighbour_raw(&rt->dst))
16161da177e4SLinus Torvalds 		goto out;
16171da177e4SLinus Torvalds 
161821efcfa0SEric Dumazet 	nrt = ip6_rt_copy(rt, dest);
16191da177e4SLinus Torvalds 	if (nrt == NULL)
16201da177e4SLinus Torvalds 		goto out;
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
16231da177e4SLinus Torvalds 	if (on_link)
16241da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
162769cce1d1SDavid S. Miller 	dst_set_neighbour(&nrt->dst, neigh_clone(neigh));
16281da177e4SLinus Torvalds 
162940e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
16301da177e4SLinus Torvalds 		goto out;
16311da177e4SLinus Torvalds 
1632d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1633d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
16348d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
16358d71740cSTom Tucker 
16361da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE) {
1637e0a1ad73SThomas Graf 		ip6_del_rt(rt);
16381da177e4SLinus Torvalds 		return;
16391da177e4SLinus Torvalds 	}
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds out:
1642d8d1f30bSChangli Gao 	dst_release(&rt->dst);
16431da177e4SLinus Torvalds }
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds /*
16461da177e4SLinus Torvalds  *	Handle ICMP "packet too big" messages
16471da177e4SLinus Torvalds  *	i.e. Path MTU discovery
16481da177e4SLinus Torvalds  */
16491da177e4SLinus Torvalds 
1650b71d1d42SEric Dumazet static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
1651ae878ae2SMaciej Żenczykowski 			     struct net *net, u32 pmtu, int ifindex)
16521da177e4SLinus Torvalds {
16531da177e4SLinus Torvalds 	struct rt6_info *rt, *nrt;
16541da177e4SLinus Torvalds 	int allfrag = 0;
1655d3052b55SAndrey Vagin again:
1656ae878ae2SMaciej Żenczykowski 	rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
16571da177e4SLinus Torvalds 	if (rt == NULL)
16581da177e4SLinus Torvalds 		return;
16591da177e4SLinus Torvalds 
1660d3052b55SAndrey Vagin 	if (rt6_check_expired(rt)) {
1661d3052b55SAndrey Vagin 		ip6_del_rt(rt);
1662d3052b55SAndrey Vagin 		goto again;
1663d3052b55SAndrey Vagin 	}
1664d3052b55SAndrey Vagin 
1665d8d1f30bSChangli Gao 	if (pmtu >= dst_mtu(&rt->dst))
16661da177e4SLinus Torvalds 		goto out;
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 	if (pmtu < IPV6_MIN_MTU) {
16691da177e4SLinus Torvalds 		/*
16701da177e4SLinus Torvalds 		 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
16711da177e4SLinus Torvalds 		 * MTU (1280) and a fragment header should always be included
16721da177e4SLinus Torvalds 		 * after a node receiving Too Big message reporting PMTU is
16731da177e4SLinus Torvalds 		 * less than the IPv6 Minimum Link MTU.
16741da177e4SLinus Torvalds 		 */
16751da177e4SLinus Torvalds 		pmtu = IPV6_MIN_MTU;
16761da177e4SLinus Torvalds 		allfrag = 1;
16771da177e4SLinus Torvalds 	}
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 	/* New mtu received -> path was valid.
16801da177e4SLinus Torvalds 	   They are sent only in response to data packets,
16811da177e4SLinus Torvalds 	   so that this nexthop apparently is reachable. --ANK
16821da177e4SLinus Torvalds 	 */
1683d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds 	/* Host route. If it is static, it would be better
16861da177e4SLinus Torvalds 	   not to override it, but add new one, so that
16871da177e4SLinus Torvalds 	   when cache entry will expire old pmtu
16881da177e4SLinus Torvalds 	   would return automatically.
16891da177e4SLinus Torvalds 	 */
16901da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
1691defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1692defb3519SDavid S. Miller 		if (allfrag) {
1693defb3519SDavid S. Miller 			u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1694defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1695defb3519SDavid S. Miller 			dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1696defb3519SDavid S. Miller 		}
1697d8d1f30bSChangli Gao 		dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
16981da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
16991da177e4SLinus Torvalds 		goto out;
17001da177e4SLinus Torvalds 	}
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 	/* Network route.
17031da177e4SLinus Torvalds 	   Two cases are possible:
17041da177e4SLinus Torvalds 	   1. It is connected route. Action: COW
17051da177e4SLinus Torvalds 	   2. It is gatewayed route or NONEXTHOP route. Action: clone it.
17061da177e4SLinus Torvalds 	 */
1707f2c31e32SEric Dumazet 	if (!dst_get_neighbour_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
1708a1e78363SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, daddr, saddr);
1709d5315b50SYOSHIFUJI Hideaki 	else
1710d5315b50SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, daddr);
1711a1e78363SYOSHIFUJI Hideaki 
1712d5315b50SYOSHIFUJI Hideaki 	if (nrt) {
1713defb3519SDavid S. Miller 		dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1714defb3519SDavid S. Miller 		if (allfrag) {
1715defb3519SDavid S. Miller 			u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1716defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1717defb3519SDavid S. Miller 			dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1718defb3519SDavid S. Miller 		}
1719a1e78363SYOSHIFUJI Hideaki 
17201da177e4SLinus Torvalds 		/* According to RFC 1981, detecting PMTU increase shouldn't be
1721a1e78363SYOSHIFUJI Hideaki 		 * happened within 5 mins, the recommended timer is 10 mins.
1722a1e78363SYOSHIFUJI Hideaki 		 * Here this route expiration time is set to ip6_rt_mtu_expires
1723a1e78363SYOSHIFUJI Hideaki 		 * which is 10 mins. After 10 mins the decreased pmtu is expired
1724a1e78363SYOSHIFUJI Hideaki 		 * and detecting PMTU increase will be automatically happened.
17251da177e4SLinus Torvalds 		 */
1726d8d1f30bSChangli Gao 		dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
17271da177e4SLinus Torvalds 		nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1728a1e78363SYOSHIFUJI Hideaki 
172940e22e8fSThomas Graf 		ip6_ins_rt(nrt);
17301da177e4SLinus Torvalds 	}
17311da177e4SLinus Torvalds out:
1732d8d1f30bSChangli Gao 	dst_release(&rt->dst);
17331da177e4SLinus Torvalds }
17341da177e4SLinus Torvalds 
1735b71d1d42SEric Dumazet void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
1736ae878ae2SMaciej Żenczykowski 			struct net_device *dev, u32 pmtu)
1737ae878ae2SMaciej Żenczykowski {
1738ae878ae2SMaciej Żenczykowski 	struct net *net = dev_net(dev);
1739ae878ae2SMaciej Żenczykowski 
1740ae878ae2SMaciej Żenczykowski 	/*
1741ae878ae2SMaciej Żenczykowski 	 * RFC 1981 states that a node "MUST reduce the size of the packets it
1742ae878ae2SMaciej Żenczykowski 	 * is sending along the path" that caused the Packet Too Big message.
1743ae878ae2SMaciej Żenczykowski 	 * Since it's not possible in the general case to determine which
1744ae878ae2SMaciej Żenczykowski 	 * interface was used to send the original packet, we update the MTU
1745ae878ae2SMaciej Żenczykowski 	 * on the interface that will be used to send future packets. We also
1746ae878ae2SMaciej Żenczykowski 	 * update the MTU on the interface that received the Packet Too Big in
1747ae878ae2SMaciej Żenczykowski 	 * case the original packet was forced out that interface with
1748ae878ae2SMaciej Żenczykowski 	 * SO_BINDTODEVICE or similar. This is the next best thing to the
1749ae878ae2SMaciej Żenczykowski 	 * correct behaviour, which would be to update the MTU on all
1750ae878ae2SMaciej Żenczykowski 	 * interfaces.
1751ae878ae2SMaciej Żenczykowski 	 */
1752ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1753ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1754ae878ae2SMaciej Żenczykowski }
1755ae878ae2SMaciej Żenczykowski 
17561da177e4SLinus Torvalds /*
17571da177e4SLinus Torvalds  *	Misc support functions
17581da177e4SLinus Torvalds  */
17591da177e4SLinus Torvalds 
176021efcfa0SEric Dumazet static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort,
176121efcfa0SEric Dumazet 				    const struct in6_addr *dest)
17621da177e4SLinus Torvalds {
1763c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ort->rt6i_dev);
17645c1e6aa3SDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
1765957c665fSDavid S. Miller 					    ort->dst.dev, 0);
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds 	if (rt) {
1768d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1769d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
17708e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
17711da177e4SLinus Torvalds 
177221efcfa0SEric Dumazet 		ipv6_addr_copy(&rt->rt6i_dst.addr, dest);
17738e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1774defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1775d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
17761da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
17771da177e4SLinus Torvalds 		if (rt->rt6i_idev)
17781da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1779d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
17801da177e4SLinus Torvalds 		rt->rt6i_expires = 0;
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
17831da177e4SLinus Torvalds 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
17841da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
17871da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
17881da177e4SLinus Torvalds #endif
17890f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1790c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
17911da177e4SLinus Torvalds 	}
17921da177e4SLinus Torvalds 	return rt;
17931da177e4SLinus Torvalds }
17941da177e4SLinus Torvalds 
179570ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1796efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1797b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1798b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
179970ceb4f5SYOSHIFUJI Hideaki {
180070ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
180170ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1802c71099acSThomas Graf 	struct fib6_table *table;
180370ceb4f5SYOSHIFUJI Hideaki 
1804efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
1805c71099acSThomas Graf 	if (table == NULL)
1806c71099acSThomas Graf 		return NULL;
1807c71099acSThomas Graf 
1808c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1809c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
181070ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
181170ceb4f5SYOSHIFUJI Hideaki 		goto out;
181270ceb4f5SYOSHIFUJI Hideaki 
1813d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
181470ceb4f5SYOSHIFUJI Hideaki 		if (rt->rt6i_dev->ifindex != ifindex)
181570ceb4f5SYOSHIFUJI Hideaki 			continue;
181670ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
181770ceb4f5SYOSHIFUJI Hideaki 			continue;
181870ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
181970ceb4f5SYOSHIFUJI Hideaki 			continue;
1820d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
182170ceb4f5SYOSHIFUJI Hideaki 		break;
182270ceb4f5SYOSHIFUJI Hideaki 	}
182370ceb4f5SYOSHIFUJI Hideaki out:
1824c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
182570ceb4f5SYOSHIFUJI Hideaki 	return rt;
182670ceb4f5SYOSHIFUJI Hideaki }
182770ceb4f5SYOSHIFUJI Hideaki 
1828efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1829b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1830b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
183170ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref)
183270ceb4f5SYOSHIFUJI Hideaki {
183386872cb5SThomas Graf 	struct fib6_config cfg = {
183486872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1835238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
183686872cb5SThomas Graf 		.fc_ifindex	= ifindex,
183786872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
183886872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
183986872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
1840efa2cea0SDaniel Lezcano 		.fc_nlinfo.pid = 0,
1841efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1842efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
184386872cb5SThomas Graf 	};
184470ceb4f5SYOSHIFUJI Hideaki 
184586872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_dst, prefix);
184686872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
184786872cb5SThomas Graf 
1848e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1849e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
185086872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
185170ceb4f5SYOSHIFUJI Hideaki 
185286872cb5SThomas Graf 	ip6_route_add(&cfg);
185370ceb4f5SYOSHIFUJI Hideaki 
1854efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
185570ceb4f5SYOSHIFUJI Hideaki }
185670ceb4f5SYOSHIFUJI Hideaki #endif
185770ceb4f5SYOSHIFUJI Hideaki 
1858b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
18591da177e4SLinus Torvalds {
18601da177e4SLinus Torvalds 	struct rt6_info *rt;
1861c71099acSThomas Graf 	struct fib6_table *table;
18621da177e4SLinus Torvalds 
1863c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
1864c71099acSThomas Graf 	if (table == NULL)
1865c71099acSThomas Graf 		return NULL;
18661da177e4SLinus Torvalds 
1867c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1868d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
18691da177e4SLinus Torvalds 		if (dev == rt->rt6i_dev &&
1870045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
18711da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
18721da177e4SLinus Torvalds 			break;
18731da177e4SLinus Torvalds 	}
18741da177e4SLinus Torvalds 	if (rt)
1875d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
1876c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
18771da177e4SLinus Torvalds 	return rt;
18781da177e4SLinus Torvalds }
18791da177e4SLinus Torvalds 
1880b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1881ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1882ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
18831da177e4SLinus Torvalds {
188486872cb5SThomas Graf 	struct fib6_config cfg = {
188586872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1886238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
188786872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
188886872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
188986872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
18905578689aSDaniel Lezcano 		.fc_nlinfo.pid = 0,
18915578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1892c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
189386872cb5SThomas Graf 	};
18941da177e4SLinus Torvalds 
189586872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
18961da177e4SLinus Torvalds 
189786872cb5SThomas Graf 	ip6_route_add(&cfg);
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
19001da177e4SLinus Torvalds }
19011da177e4SLinus Torvalds 
19027b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
19031da177e4SLinus Torvalds {
19041da177e4SLinus Torvalds 	struct rt6_info *rt;
1905c71099acSThomas Graf 	struct fib6_table *table;
1906c71099acSThomas Graf 
1907c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
19087b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
1909c71099acSThomas Graf 	if (table == NULL)
1910c71099acSThomas Graf 		return;
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds restart:
1913c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1914d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
19151da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1916d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1917c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1918e0a1ad73SThomas Graf 			ip6_del_rt(rt);
19191da177e4SLinus Torvalds 			goto restart;
19201da177e4SLinus Torvalds 		}
19211da177e4SLinus Torvalds 	}
1922c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
19231da177e4SLinus Torvalds }
19241da177e4SLinus Torvalds 
19255578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
19265578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
192786872cb5SThomas Graf 				 struct fib6_config *cfg)
192886872cb5SThomas Graf {
192986872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
193086872cb5SThomas Graf 
193186872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
193286872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
193386872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
193486872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
193586872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
193686872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
193786872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
193886872cb5SThomas Graf 
19395578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1940f1243c2dSBenjamin Thery 
194186872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
194286872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
194386872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
194486872cb5SThomas Graf }
194586872cb5SThomas Graf 
19465578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
19471da177e4SLinus Torvalds {
194886872cb5SThomas Graf 	struct fib6_config cfg;
19491da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
19501da177e4SLinus Torvalds 	int err;
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 	switch(cmd) {
19531da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
19541da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
19551da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
19561da177e4SLinus Torvalds 			return -EPERM;
19571da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
19581da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
19591da177e4SLinus Torvalds 		if (err)
19601da177e4SLinus Torvalds 			return -EFAULT;
19611da177e4SLinus Torvalds 
19625578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
196386872cb5SThomas Graf 
19641da177e4SLinus Torvalds 		rtnl_lock();
19651da177e4SLinus Torvalds 		switch (cmd) {
19661da177e4SLinus Torvalds 		case SIOCADDRT:
196786872cb5SThomas Graf 			err = ip6_route_add(&cfg);
19681da177e4SLinus Torvalds 			break;
19691da177e4SLinus Torvalds 		case SIOCDELRT:
197086872cb5SThomas Graf 			err = ip6_route_del(&cfg);
19711da177e4SLinus Torvalds 			break;
19721da177e4SLinus Torvalds 		default:
19731da177e4SLinus Torvalds 			err = -EINVAL;
19741da177e4SLinus Torvalds 		}
19751da177e4SLinus Torvalds 		rtnl_unlock();
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds 		return err;
19783ff50b79SStephen Hemminger 	}
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds 	return -EINVAL;
19811da177e4SLinus Torvalds }
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds /*
19841da177e4SLinus Torvalds  *	Drop the packet on the floor
19851da177e4SLinus Torvalds  */
19861da177e4SLinus Torvalds 
1987d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
19881da177e4SLinus Torvalds {
1989612f09e8SYOSHIFUJI Hideaki 	int type;
1990adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
1991612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
1992612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
19930660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
199445bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
19953bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
19963bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
1997612f09e8SYOSHIFUJI Hideaki 			break;
1998612f09e8SYOSHIFUJI Hideaki 		}
1999612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2000612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
20013bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20023bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2003612f09e8SYOSHIFUJI Hideaki 		break;
2004612f09e8SYOSHIFUJI Hideaki 	}
20053ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
20061da177e4SLinus Torvalds 	kfree_skb(skb);
20071da177e4SLinus Torvalds 	return 0;
20081da177e4SLinus Torvalds }
20091da177e4SLinus Torvalds 
20109ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
20119ce8ade0SThomas Graf {
2012612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
20139ce8ade0SThomas Graf }
20149ce8ade0SThomas Graf 
201520380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
20161da177e4SLinus Torvalds {
2017adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2018612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds 
20216723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
20226723ab54SDavid S. Miller 
20239ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
20249ce8ade0SThomas Graf {
2025612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
20269ce8ade0SThomas Graf }
20279ce8ade0SThomas Graf 
20289ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
20299ce8ade0SThomas Graf {
2030adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2031612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
20329ce8ade0SThomas Graf }
20339ce8ade0SThomas Graf 
20346723ab54SDavid S. Miller #endif
20356723ab54SDavid S. Miller 
20361da177e4SLinus Torvalds /*
20371da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
20381da177e4SLinus Torvalds  */
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
20411da177e4SLinus Torvalds 				    const struct in6_addr *addr,
20421da177e4SLinus Torvalds 				    int anycast)
20431da177e4SLinus Torvalds {
2044c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
20455c1e6aa3SDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
2046957c665fSDavid S. Miller 					    net->loopback_dev, 0);
204714deae41SDavid S. Miller 	struct neighbour *neigh;
20481da177e4SLinus Torvalds 
204940385653SBen Greear 	if (rt == NULL) {
205040385653SBen Greear 		if (net_ratelimit())
205140385653SBen Greear 			pr_warning("IPv6:  Maximum number of routes reached,"
205240385653SBen Greear 				   " consider increasing route/max_size.\n");
20531da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
205440385653SBen Greear 	}
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	in6_dev_hold(idev);
20571da177e4SLinus Torvalds 
205811d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2059d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2060d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
20611da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2062d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
20631da177e4SLinus Torvalds 
20641da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
206558c4fb86SYOSHIFUJI Hideaki 	if (anycast)
206658c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
206758c4fb86SYOSHIFUJI Hideaki 	else
20681da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
206914deae41SDavid S. Miller 	neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
207014deae41SDavid S. Miller 	if (IS_ERR(neigh)) {
2071d8d1f30bSChangli Gao 		dst_free(&rt->dst);
207214deae41SDavid S. Miller 
207329546a64SDavid S. Miller 		return ERR_CAST(neigh);
20741da177e4SLinus Torvalds 	}
207569cce1d1SDavid S. Miller 	dst_set_neighbour(&rt->dst, neigh);
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
20781da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
20795578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
20801da177e4SLinus Torvalds 
2081d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds 	return rt;
20841da177e4SLinus Torvalds }
20851da177e4SLinus Torvalds 
2086c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2087c3968a85SDaniel Walter 			struct rt6_info *rt,
2088b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2089c3968a85SDaniel Walter 			unsigned int prefs,
2090c3968a85SDaniel Walter 			struct in6_addr *saddr)
2091c3968a85SDaniel Walter {
2092c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2093c3968a85SDaniel Walter 	int err = 0;
2094c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
2095c3968a85SDaniel Walter 		ipv6_addr_copy(saddr, &rt->rt6i_prefsrc.addr);
2096c3968a85SDaniel Walter 	else
2097c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2098c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2099c3968a85SDaniel Walter 	return err;
2100c3968a85SDaniel Walter }
2101c3968a85SDaniel Walter 
2102c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2103c3968a85SDaniel Walter struct arg_dev_net_ip {
2104c3968a85SDaniel Walter 	struct net_device *dev;
2105c3968a85SDaniel Walter 	struct net *net;
2106c3968a85SDaniel Walter 	struct in6_addr *addr;
2107c3968a85SDaniel Walter };
2108c3968a85SDaniel Walter 
2109c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2110c3968a85SDaniel Walter {
2111c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2112c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2113c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2114c3968a85SDaniel Walter 
2115c3968a85SDaniel Walter 	if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
2116c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2117c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2118c3968a85SDaniel Walter 		/* remove prefsrc entry */
2119c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2120c3968a85SDaniel Walter 	}
2121c3968a85SDaniel Walter 	return 0;
2122c3968a85SDaniel Walter }
2123c3968a85SDaniel Walter 
2124c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2125c3968a85SDaniel Walter {
2126c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2127c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2128c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2129c3968a85SDaniel Walter 		.net = net,
2130c3968a85SDaniel Walter 		.addr = &ifp->addr,
2131c3968a85SDaniel Walter 	};
2132c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2133c3968a85SDaniel Walter }
2134c3968a85SDaniel Walter 
21358ed67789SDaniel Lezcano struct arg_dev_net {
21368ed67789SDaniel Lezcano 	struct net_device *dev;
21378ed67789SDaniel Lezcano 	struct net *net;
21388ed67789SDaniel Lezcano };
21398ed67789SDaniel Lezcano 
21401da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
21411da177e4SLinus Torvalds {
2142bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2143bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
21448ed67789SDaniel Lezcano 
2145bc3ef660Sstephen hemminger 	if ((rt->rt6i_dev == dev || dev == NULL) &&
2146bc3ef660Sstephen hemminger 	    rt != adn->net->ipv6.ip6_null_entry) {
21471da177e4SLinus Torvalds 		RT6_TRACE("deleted by ifdown %p\n", rt);
21481da177e4SLinus Torvalds 		return -1;
21491da177e4SLinus Torvalds 	}
21501da177e4SLinus Torvalds 	return 0;
21511da177e4SLinus Torvalds }
21521da177e4SLinus Torvalds 
2153f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
21541da177e4SLinus Torvalds {
21558ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
21568ed67789SDaniel Lezcano 		.dev = dev,
21578ed67789SDaniel Lezcano 		.net = net,
21588ed67789SDaniel Lezcano 	};
21598ed67789SDaniel Lezcano 
21608ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
21611e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
21621da177e4SLinus Torvalds }
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds struct rt6_mtu_change_arg
21651da177e4SLinus Torvalds {
21661da177e4SLinus Torvalds 	struct net_device *dev;
21671da177e4SLinus Torvalds 	unsigned mtu;
21681da177e4SLinus Torvalds };
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
21711da177e4SLinus Torvalds {
21721da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
21731da177e4SLinus Torvalds 	struct inet6_dev *idev;
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
21761da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
21771da177e4SLinus Torvalds 	   We still use this lock to block changes
21781da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
21791da177e4SLinus Torvalds 	*/
21801da177e4SLinus Torvalds 
21811da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
21821da177e4SLinus Torvalds 	if (idev == NULL)
21831da177e4SLinus Torvalds 		return 0;
21841da177e4SLinus Torvalds 
21851da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
21861da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
21871da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
21881da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
21891da177e4SLinus Torvalds 	 */
21901da177e4SLinus Torvalds 	/*
21911da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
21921da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
21931da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
21941da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
21951da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
21961da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
21971da177e4SLinus Torvalds 	   PMTU discouvery.
21981da177e4SLinus Torvalds 	 */
21991da177e4SLinus Torvalds 	if (rt->rt6i_dev == arg->dev &&
2200d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2201d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2202d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2203d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2204defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2205566cfd8fSSimon Arlott 	}
22061da177e4SLinus Torvalds 	return 0;
22071da177e4SLinus Torvalds }
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu)
22101da177e4SLinus Torvalds {
2211c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2212c71099acSThomas Graf 		.dev = dev,
2213c71099acSThomas Graf 		.mtu = mtu,
2214c71099acSThomas Graf 	};
22151da177e4SLinus Torvalds 
2216c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
22171da177e4SLinus Torvalds }
22181da177e4SLinus Torvalds 
2219ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
22205176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
222186872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2222ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
222386872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
222486872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
222586872cb5SThomas Graf };
222686872cb5SThomas Graf 
222786872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
222886872cb5SThomas Graf 			      struct fib6_config *cfg)
22291da177e4SLinus Torvalds {
223086872cb5SThomas Graf 	struct rtmsg *rtm;
223186872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
223286872cb5SThomas Graf 	int err;
22331da177e4SLinus Torvalds 
223486872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
223586872cb5SThomas Graf 	if (err < 0)
223686872cb5SThomas Graf 		goto errout;
22371da177e4SLinus Torvalds 
223886872cb5SThomas Graf 	err = -EINVAL;
223986872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
224086872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
224186872cb5SThomas Graf 
224286872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
224386872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
224486872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
224586872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
224686872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
224786872cb5SThomas Graf 
224886872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
224986872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
225086872cb5SThomas Graf 
2251ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2252ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2253ab79ad14SMaciej Żenczykowski 
225486872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
225586872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
22563b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
225786872cb5SThomas Graf 
225886872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
225986872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
226086872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
22611da177e4SLinus Torvalds 	}
226286872cb5SThomas Graf 
226386872cb5SThomas Graf 	if (tb[RTA_DST]) {
226486872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
226586872cb5SThomas Graf 
226686872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
226786872cb5SThomas Graf 			goto errout;
226886872cb5SThomas Graf 
226986872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
22701da177e4SLinus Torvalds 	}
227186872cb5SThomas Graf 
227286872cb5SThomas Graf 	if (tb[RTA_SRC]) {
227386872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
227486872cb5SThomas Graf 
227586872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
227686872cb5SThomas Graf 			goto errout;
227786872cb5SThomas Graf 
227886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
22791da177e4SLinus Torvalds 	}
228086872cb5SThomas Graf 
2281c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2282c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2283c3968a85SDaniel Walter 
228486872cb5SThomas Graf 	if (tb[RTA_OIF])
228586872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
228686872cb5SThomas Graf 
228786872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
228886872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
228986872cb5SThomas Graf 
229086872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
229186872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
229286872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
22931da177e4SLinus Torvalds 	}
229486872cb5SThomas Graf 
229586872cb5SThomas Graf 	if (tb[RTA_TABLE])
229686872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
229786872cb5SThomas Graf 
229886872cb5SThomas Graf 	err = 0;
229986872cb5SThomas Graf errout:
230086872cb5SThomas Graf 	return err;
23011da177e4SLinus Torvalds }
23021da177e4SLinus Torvalds 
2303c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23041da177e4SLinus Torvalds {
230586872cb5SThomas Graf 	struct fib6_config cfg;
230686872cb5SThomas Graf 	int err;
23071da177e4SLinus Torvalds 
230886872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
230986872cb5SThomas Graf 	if (err < 0)
231086872cb5SThomas Graf 		return err;
231186872cb5SThomas Graf 
231286872cb5SThomas Graf 	return ip6_route_del(&cfg);
23131da177e4SLinus Torvalds }
23141da177e4SLinus Torvalds 
2315c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23161da177e4SLinus Torvalds {
231786872cb5SThomas Graf 	struct fib6_config cfg;
231886872cb5SThomas Graf 	int err;
23191da177e4SLinus Torvalds 
232086872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
232186872cb5SThomas Graf 	if (err < 0)
232286872cb5SThomas Graf 		return err;
232386872cb5SThomas Graf 
232486872cb5SThomas Graf 	return ip6_route_add(&cfg);
23251da177e4SLinus Torvalds }
23261da177e4SLinus Torvalds 
2327339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2328339bf98fSThomas Graf {
2329339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2330339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2331339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2332339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2333339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2334339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2335339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2336339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2337339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
23386a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2339339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2340339bf98fSThomas Graf }
2341339bf98fSThomas Graf 
2342191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2343191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
23440d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
23450d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
23467bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
23471da177e4SLinus Torvalds {
23481da177e4SLinus Torvalds 	struct rtmsg *rtm;
23491da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2350e3703b3dSThomas Graf 	long expires;
23519e762a4aSPatrick McHardy 	u32 table;
2352f2c31e32SEric Dumazet 	struct neighbour *n;
23531da177e4SLinus Torvalds 
23541da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
23551da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
23561da177e4SLinus Torvalds 			/* success since this is not a prefix route */
23571da177e4SLinus Torvalds 			return 1;
23581da177e4SLinus Torvalds 		}
23591da177e4SLinus Torvalds 	}
23601da177e4SLinus Torvalds 
23612d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
23622d7202bfSThomas Graf 	if (nlh == NULL)
236326932566SPatrick McHardy 		return -EMSGSIZE;
23642d7202bfSThomas Graf 
23652d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
23661da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
23671da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
23681da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
23691da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2370c71099acSThomas Graf 	if (rt->rt6i_table)
23719e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2372c71099acSThomas Graf 	else
23739e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
23749e762a4aSPatrick McHardy 	rtm->rtm_table = table;
23752d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_TABLE, table);
23761da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_REJECT)
23771da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
2378ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags&RTF_LOCAL)
2379ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
23801da177e4SLinus Torvalds 	else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
23811da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
23821da177e4SLinus Torvalds 	else
23831da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
23841da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
23851da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
23861da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
23871da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_DYNAMIC)
23881da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
23891da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
23901da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
23911da177e4SLinus Torvalds 	else if (rt->rt6i_flags&RTF_DEFAULT)
23921da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE)
23951da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
23961da177e4SLinus Torvalds 
23971da177e4SLinus Torvalds 	if (dst) {
23982d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, dst);
23991da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
24001da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
24012d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
24021da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
24031da177e4SLinus Torvalds 	if (src) {
24042d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, src);
24051da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
24061da177e4SLinus Torvalds 	} else if (rtm->rtm_src_len)
24072d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
24081da177e4SLinus Torvalds #endif
24097bc570c8SYOSHIFUJI Hideaki 	if (iif) {
24107bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
24117bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
24128229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
24137bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
24147bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
24157bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
24167bc570c8SYOSHIFUJI Hideaki 						return 0;
24177bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
24187bc570c8SYOSHIFUJI Hideaki 				} else {
24197bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
24207bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
24217bc570c8SYOSHIFUJI Hideaki 				}
24227bc570c8SYOSHIFUJI Hideaki 			}
24237bc570c8SYOSHIFUJI Hideaki 		} else
24247bc570c8SYOSHIFUJI Hideaki #endif
24252d7202bfSThomas Graf 			NLA_PUT_U32(skb, RTA_IIF, iif);
24267bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
24271da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2428c3968a85SDaniel Walter 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0)
2429c3968a85SDaniel Walter 			NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2430c3968a85SDaniel Walter 	}
2431c3968a85SDaniel Walter 
2432c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2433c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
2434c3968a85SDaniel Walter 		ipv6_addr_copy(&saddr_buf, &rt->rt6i_prefsrc.addr);
24352d7202bfSThomas Graf 		NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
24361da177e4SLinus Torvalds 	}
24372d7202bfSThomas Graf 
2438defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
24392d7202bfSThomas Graf 		goto nla_put_failure;
24402d7202bfSThomas Graf 
2441f2c31e32SEric Dumazet 	rcu_read_lock();
2442f2c31e32SEric Dumazet 	n = dst_get_neighbour(&rt->dst);
2443f2c31e32SEric Dumazet 	if (n)
2444f2c31e32SEric Dumazet 		NLA_PUT(skb, RTA_GATEWAY, 16, &n->primary_key);
2445f2c31e32SEric Dumazet 	rcu_read_unlock();
24462d7202bfSThomas Graf 
2447d8d1f30bSChangli Gao 	if (rt->dst.dev)
24482d7202bfSThomas Graf 		NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
24492d7202bfSThomas Graf 
24502d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
2451e3703b3dSThomas Graf 
245236e3deaeSYOSHIFUJI Hideaki 	if (!(rt->rt6i_flags & RTF_EXPIRES))
245336e3deaeSYOSHIFUJI Hideaki 		expires = 0;
245436e3deaeSYOSHIFUJI Hideaki 	else if (rt->rt6i_expires - jiffies < INT_MAX)
245536e3deaeSYOSHIFUJI Hideaki 		expires = rt->rt6i_expires - jiffies;
245636e3deaeSYOSHIFUJI Hideaki 	else
245736e3deaeSYOSHIFUJI Hideaki 		expires = INT_MAX;
245869cdf8f9SYOSHIFUJI Hideaki 
2459d8d1f30bSChangli Gao 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2460d8d1f30bSChangli Gao 			       expires, rt->dst.error) < 0)
2461e3703b3dSThomas Graf 		goto nla_put_failure;
24621da177e4SLinus Torvalds 
24632d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
24642d7202bfSThomas Graf 
24652d7202bfSThomas Graf nla_put_failure:
246626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
246726932566SPatrick McHardy 	return -EMSGSIZE;
24681da177e4SLinus Torvalds }
24691da177e4SLinus Torvalds 
24701b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
24711da177e4SLinus Torvalds {
24721da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
24731da177e4SLinus Torvalds 	int prefix;
24741da177e4SLinus Torvalds 
24752d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
24762d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
24771da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
24781da177e4SLinus Torvalds 	} else
24791da177e4SLinus Torvalds 		prefix = 0;
24801da177e4SLinus Torvalds 
2481191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2482191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
24831da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
24847bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
24851da177e4SLinus Torvalds }
24861da177e4SLinus Torvalds 
2487c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
24881da177e4SLinus Torvalds {
24893b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2490ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
24911da177e4SLinus Torvalds 	struct rt6_info *rt;
2492ab364a6fSThomas Graf 	struct sk_buff *skb;
2493ab364a6fSThomas Graf 	struct rtmsg *rtm;
24944c9483b2SDavid S. Miller 	struct flowi6 fl6;
2495ab364a6fSThomas Graf 	int err, iif = 0;
2496ab364a6fSThomas Graf 
2497ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2498ab364a6fSThomas Graf 	if (err < 0)
2499ab364a6fSThomas Graf 		goto errout;
2500ab364a6fSThomas Graf 
2501ab364a6fSThomas Graf 	err = -EINVAL;
25024c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2503ab364a6fSThomas Graf 
2504ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2505ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2506ab364a6fSThomas Graf 			goto errout;
2507ab364a6fSThomas Graf 
25084c9483b2SDavid S. Miller 		ipv6_addr_copy(&fl6.saddr, nla_data(tb[RTA_SRC]));
2509ab364a6fSThomas Graf 	}
2510ab364a6fSThomas Graf 
2511ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2512ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2513ab364a6fSThomas Graf 			goto errout;
2514ab364a6fSThomas Graf 
25154c9483b2SDavid S. Miller 		ipv6_addr_copy(&fl6.daddr, nla_data(tb[RTA_DST]));
2516ab364a6fSThomas Graf 	}
2517ab364a6fSThomas Graf 
2518ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2519ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2520ab364a6fSThomas Graf 
2521ab364a6fSThomas Graf 	if (tb[RTA_OIF])
25224c9483b2SDavid S. Miller 		fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]);
2523ab364a6fSThomas Graf 
2524ab364a6fSThomas Graf 	if (iif) {
2525ab364a6fSThomas Graf 		struct net_device *dev;
25265578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2527ab364a6fSThomas Graf 		if (!dev) {
2528ab364a6fSThomas Graf 			err = -ENODEV;
2529ab364a6fSThomas Graf 			goto errout;
2530ab364a6fSThomas Graf 		}
2531ab364a6fSThomas Graf 	}
25321da177e4SLinus Torvalds 
25331da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2534ab364a6fSThomas Graf 	if (skb == NULL) {
2535ab364a6fSThomas Graf 		err = -ENOBUFS;
2536ab364a6fSThomas Graf 		goto errout;
2537ab364a6fSThomas Graf 	}
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
25401da177e4SLinus Torvalds 	   through good chunk of routing engine.
25411da177e4SLinus Torvalds 	 */
2542459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
25431da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
25441da177e4SLinus Torvalds 
25454c9483b2SDavid S. Miller 	rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6);
2546d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
25471da177e4SLinus Torvalds 
25484c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
25491da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
25507bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
25511da177e4SLinus Torvalds 	if (err < 0) {
2552ab364a6fSThomas Graf 		kfree_skb(skb);
2553ab364a6fSThomas Graf 		goto errout;
25541da177e4SLinus Torvalds 	}
25551da177e4SLinus Torvalds 
25565578689aSDaniel Lezcano 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2557ab364a6fSThomas Graf errout:
25581da177e4SLinus Torvalds 	return err;
25591da177e4SLinus Torvalds }
25601da177e4SLinus Torvalds 
256186872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
25621da177e4SLinus Torvalds {
25631da177e4SLinus Torvalds 	struct sk_buff *skb;
25645578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2565528c4cebSDenis V. Lunev 	u32 seq;
2566528c4cebSDenis V. Lunev 	int err;
25670d51aa80SJamal Hadi Salim 
2568528c4cebSDenis V. Lunev 	err = -ENOBUFS;
2569528c4cebSDenis V. Lunev 	seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
257086872cb5SThomas Graf 
2571339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
257221713ebcSThomas Graf 	if (skb == NULL)
257321713ebcSThomas Graf 		goto errout;
25741da177e4SLinus Torvalds 
2575191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
25767bc570c8SYOSHIFUJI Hideaki 				event, info->pid, seq, 0, 0, 0);
257726932566SPatrick McHardy 	if (err < 0) {
257826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
257926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
258026932566SPatrick McHardy 		kfree_skb(skb);
258126932566SPatrick McHardy 		goto errout;
258226932566SPatrick McHardy 	}
25831ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
25845578689aSDaniel Lezcano 		    info->nlh, gfp_any());
25851ce85fe4SPablo Neira Ayuso 	return;
258621713ebcSThomas Graf errout:
258721713ebcSThomas Graf 	if (err < 0)
25885578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
25891da177e4SLinus Torvalds }
25901da177e4SLinus Torvalds 
25918ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
25928ed67789SDaniel Lezcano 				unsigned long event, void *data)
25938ed67789SDaniel Lezcano {
25948ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2595c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
25968ed67789SDaniel Lezcano 
25978ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2598d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
25998ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
26008ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2601d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
26028ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2603d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
26048ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
26058ed67789SDaniel Lezcano #endif
26068ed67789SDaniel Lezcano 	}
26078ed67789SDaniel Lezcano 
26088ed67789SDaniel Lezcano 	return NOTIFY_OK;
26098ed67789SDaniel Lezcano }
26108ed67789SDaniel Lezcano 
26111da177e4SLinus Torvalds /*
26121da177e4SLinus Torvalds  *	/proc
26131da177e4SLinus Torvalds  */
26141da177e4SLinus Torvalds 
26151da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
26161da177e4SLinus Torvalds 
26171da177e4SLinus Torvalds struct rt6_proc_arg
26181da177e4SLinus Torvalds {
26191da177e4SLinus Torvalds 	char *buffer;
26201da177e4SLinus Torvalds 	int offset;
26211da177e4SLinus Torvalds 	int length;
26221da177e4SLinus Torvalds 	int skip;
26231da177e4SLinus Torvalds 	int len;
26241da177e4SLinus Torvalds };
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
26271da177e4SLinus Torvalds {
262833120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
262969cce1d1SDavid S. Miller 	struct neighbour *n;
26301da177e4SLinus Torvalds 
26314b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
26321da177e4SLinus Torvalds 
26331da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
26344b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
26351da177e4SLinus Torvalds #else
263633120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
26371da177e4SLinus Torvalds #endif
2638f2c31e32SEric Dumazet 	rcu_read_lock();
263969cce1d1SDavid S. Miller 	n = dst_get_neighbour(&rt->dst);
264069cce1d1SDavid S. Miller 	if (n) {
264169cce1d1SDavid S. Miller 		seq_printf(m, "%pi6", n->primary_key);
26421da177e4SLinus Torvalds 	} else {
264333120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
26441da177e4SLinus Torvalds 	}
2645f2c31e32SEric Dumazet 	rcu_read_unlock();
264633120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2647d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2648d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
26491da177e4SLinus Torvalds 		   rt->rt6i_dev ? rt->rt6i_dev->name : "");
26501da177e4SLinus Torvalds 	return 0;
26511da177e4SLinus Torvalds }
26521da177e4SLinus Torvalds 
265333120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
26541da177e4SLinus Torvalds {
2655f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
2656f3db4851SDaniel Lezcano 	fib6_clean_all(net, rt6_info_route, 0, m);
265733120b30SAlexey Dobriyan 	return 0;
26581da177e4SLinus Torvalds }
26591da177e4SLinus Torvalds 
266033120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
266133120b30SAlexey Dobriyan {
2662de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2663f3db4851SDaniel Lezcano }
2664f3db4851SDaniel Lezcano 
266533120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
266633120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
266733120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
266833120b30SAlexey Dobriyan 	.read		= seq_read,
266933120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2670b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
267133120b30SAlexey Dobriyan };
267233120b30SAlexey Dobriyan 
26731da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
26741da177e4SLinus Torvalds {
267569ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
26761da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
267769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
267869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
267969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
268069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
268169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2682fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
268369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
26841da177e4SLinus Torvalds 
26851da177e4SLinus Torvalds 	return 0;
26861da177e4SLinus Torvalds }
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
26891da177e4SLinus Torvalds {
2690de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
269169ddb805SDaniel Lezcano }
269269ddb805SDaniel Lezcano 
26939a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
26941da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
26951da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
26961da177e4SLinus Torvalds 	.read	 = seq_read,
26971da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2698b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
26991da177e4SLinus Torvalds };
27001da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds static
27058d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
27061da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
27071da177e4SLinus Torvalds {
2708c486da34SLucian Adrian Grijincu 	struct net *net;
2709c486da34SLucian Adrian Grijincu 	int delay;
2710c486da34SLucian Adrian Grijincu 	if (!write)
2711c486da34SLucian Adrian Grijincu 		return -EINVAL;
2712c486da34SLucian Adrian Grijincu 
2713c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2714c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
27158d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
27165b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
27171da177e4SLinus Torvalds 	return 0;
27181da177e4SLinus Torvalds }
27191da177e4SLinus Torvalds 
2720760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
27211da177e4SLinus Torvalds 	{
27221da177e4SLinus Torvalds 		.procname	=	"flush",
27234990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
27241da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
272589c8b3a1SDave Jones 		.mode		=	0200,
27266d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
27271da177e4SLinus Torvalds 	},
27281da177e4SLinus Torvalds 	{
27291da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
27309a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
27311da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27321da177e4SLinus Torvalds 		.mode		=	0644,
27336d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27341da177e4SLinus Torvalds 	},
27351da177e4SLinus Torvalds 	{
27361da177e4SLinus Torvalds 		.procname	=	"max_size",
27374990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
27381da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27391da177e4SLinus Torvalds 		.mode		=	0644,
27406d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27411da177e4SLinus Torvalds 	},
27421da177e4SLinus Torvalds 	{
27431da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
27444990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
27451da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27461da177e4SLinus Torvalds 		.mode		=	0644,
27476d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27481da177e4SLinus Torvalds 	},
27491da177e4SLinus Torvalds 	{
27501da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
27514990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
27521da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27531da177e4SLinus Torvalds 		.mode		=	0644,
27546d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27551da177e4SLinus Torvalds 	},
27561da177e4SLinus Torvalds 	{
27571da177e4SLinus Torvalds 		.procname	=	"gc_interval",
27584990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
27591da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27601da177e4SLinus Torvalds 		.mode		=	0644,
27616d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27621da177e4SLinus Torvalds 	},
27631da177e4SLinus Torvalds 	{
27641da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
27654990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
27661da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27671da177e4SLinus Torvalds 		.mode		=	0644,
2768f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
27691da177e4SLinus Torvalds 	},
27701da177e4SLinus Torvalds 	{
27711da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
27724990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
27731da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27741da177e4SLinus Torvalds 		.mode		=	0644,
27756d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27761da177e4SLinus Torvalds 	},
27771da177e4SLinus Torvalds 	{
27781da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
27794990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
27801da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27811da177e4SLinus Torvalds 		.mode		=	0644,
2782f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
27831da177e4SLinus Torvalds 	},
27841da177e4SLinus Torvalds 	{
27851da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
27864990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
27871da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27881da177e4SLinus Torvalds 		.mode		=	0644,
27896d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
27901da177e4SLinus Torvalds 	},
2791f8572d8fSEric W. Biederman 	{ }
27921da177e4SLinus Torvalds };
27931da177e4SLinus Torvalds 
27942c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2795760f2d01SDaniel Lezcano {
2796760f2d01SDaniel Lezcano 	struct ctl_table *table;
2797760f2d01SDaniel Lezcano 
2798760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2799760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2800760f2d01SDaniel Lezcano 			GFP_KERNEL);
28015ee09105SYOSHIFUJI Hideaki 
28025ee09105SYOSHIFUJI Hideaki 	if (table) {
28035ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2804c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
280586393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
28065ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
28075ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28085ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
28095ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
28105ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
28115ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
28125ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
28139c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28145ee09105SYOSHIFUJI Hideaki 	}
28155ee09105SYOSHIFUJI Hideaki 
2816760f2d01SDaniel Lezcano 	return table;
2817760f2d01SDaniel Lezcano }
28181da177e4SLinus Torvalds #endif
28191da177e4SLinus Torvalds 
28202c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2821cdb18761SDaniel Lezcano {
2822633d424bSPavel Emelyanov 	int ret = -ENOMEM;
28238ed67789SDaniel Lezcano 
282486393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
282586393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2826f2fc6a54SBenjamin Thery 
2827fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2828fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2829fc66f95cSEric Dumazet 
28308ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
28318ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
28328ed67789SDaniel Lezcano 					   GFP_KERNEL);
28338ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2834fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2835d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
28368ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2837d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
283862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
283962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28408ed67789SDaniel Lezcano 
28418ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
28428ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
28438ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
28448ed67789SDaniel Lezcano 					       GFP_KERNEL);
284568fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
284668fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2847d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
28488ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2849d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
285062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
285162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28528ed67789SDaniel Lezcano 
28538ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
28548ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
28558ed67789SDaniel Lezcano 					       GFP_KERNEL);
285668fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
285768fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2858d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
28598ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2860d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
286162fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
286262fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28638ed67789SDaniel Lezcano #endif
28648ed67789SDaniel Lezcano 
2865b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2866b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2867b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2868b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2869b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2870b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2871b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2872b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2873b339a47cSPeter Zijlstra 
2874cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2875cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2876cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2877cdb18761SDaniel Lezcano #endif
28786891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
28796891a346SBenjamin Thery 
28808ed67789SDaniel Lezcano 	ret = 0;
28818ed67789SDaniel Lezcano out:
28828ed67789SDaniel Lezcano 	return ret;
2883f2fc6a54SBenjamin Thery 
288468fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
288568fffc67SPeter Zijlstra out_ip6_prohibit_entry:
288668fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
288768fffc67SPeter Zijlstra out_ip6_null_entry:
288868fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
288968fffc67SPeter Zijlstra #endif
2890fc66f95cSEric Dumazet out_ip6_dst_entries:
2891fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2892f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2893f2fc6a54SBenjamin Thery 	goto out;
2894cdb18761SDaniel Lezcano }
2895cdb18761SDaniel Lezcano 
28962c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2897cdb18761SDaniel Lezcano {
2898cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2899cdb18761SDaniel Lezcano 	proc_net_remove(net, "ipv6_route");
2900cdb18761SDaniel Lezcano 	proc_net_remove(net, "rt6_stats");
2901cdb18761SDaniel Lezcano #endif
29028ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
29038ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29048ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
29058ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
29068ed67789SDaniel Lezcano #endif
290741bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2908cdb18761SDaniel Lezcano }
2909cdb18761SDaniel Lezcano 
2910cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
2911cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
2912cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
2913cdb18761SDaniel Lezcano };
2914cdb18761SDaniel Lezcano 
29158ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
29168ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
29178ed67789SDaniel Lezcano 	.priority = 0,
29188ed67789SDaniel Lezcano };
29198ed67789SDaniel Lezcano 
2920433d49c3SDaniel Lezcano int __init ip6_route_init(void)
29211da177e4SLinus Torvalds {
2922433d49c3SDaniel Lezcano 	int ret;
2923433d49c3SDaniel Lezcano 
29249a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
29259a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
29269a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
29279a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
29289a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
2929c19a28e1SFernando Carrijo 		goto out;
293014e50e57SDavid S. Miller 
2931fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
29328ed67789SDaniel Lezcano 	if (ret)
2933bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
2934bdb3289fSDaniel Lezcano 
2935fc66f95cSEric Dumazet 	ret = register_pernet_subsys(&ip6_route_net_ops);
2936fc66f95cSEric Dumazet 	if (ret)
2937fc66f95cSEric Dumazet 		goto out_dst_entries;
2938fc66f95cSEric Dumazet 
29395dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
29405dc121e9SArnaud Ebalard 
29418ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
29428ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
29438ed67789SDaniel Lezcano 	 * manually for init_net */
2944d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
29458ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2946bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2947d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
29488ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2949d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
29508ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2951bdb3289fSDaniel Lezcano   #endif
2952433d49c3SDaniel Lezcano 	ret = fib6_init();
2953433d49c3SDaniel Lezcano 	if (ret)
29548ed67789SDaniel Lezcano 		goto out_register_subsys;
2955433d49c3SDaniel Lezcano 
2956433d49c3SDaniel Lezcano 	ret = xfrm6_init();
2957433d49c3SDaniel Lezcano 	if (ret)
2958cdb18761SDaniel Lezcano 		goto out_fib6_init;
2959c35b7e72SDaniel Lezcano 
2960433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
2961433d49c3SDaniel Lezcano 	if (ret)
2962433d49c3SDaniel Lezcano 		goto xfrm6_init;
29637e5449c2SDaniel Lezcano 
2964433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
2965c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
2966c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
2967c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
2968433d49c3SDaniel Lezcano 		goto fib6_rules_init;
2969433d49c3SDaniel Lezcano 
29708ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
2971cdb18761SDaniel Lezcano 	if (ret)
2972cdb18761SDaniel Lezcano 		goto fib6_rules_init;
29738ed67789SDaniel Lezcano 
2974433d49c3SDaniel Lezcano out:
2975433d49c3SDaniel Lezcano 	return ret;
2976433d49c3SDaniel Lezcano 
2977433d49c3SDaniel Lezcano fib6_rules_init:
2978433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
2979433d49c3SDaniel Lezcano xfrm6_init:
2980433d49c3SDaniel Lezcano 	xfrm6_fini();
2981433d49c3SDaniel Lezcano out_fib6_init:
2982433d49c3SDaniel Lezcano 	fib6_gc_cleanup();
29838ed67789SDaniel Lezcano out_register_subsys:
29848ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
2985fc66f95cSEric Dumazet out_dst_entries:
2986fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
2987433d49c3SDaniel Lezcano out_kmem_cache:
2988f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
2989433d49c3SDaniel Lezcano 	goto out;
29901da177e4SLinus Torvalds }
29911da177e4SLinus Torvalds 
29921da177e4SLinus Torvalds void ip6_route_cleanup(void)
29931da177e4SLinus Torvalds {
29948ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
2995101367c2SThomas Graf 	fib6_rules_cleanup();
29961da177e4SLinus Torvalds 	xfrm6_fini();
29971da177e4SLinus Torvalds 	fib6_gc_cleanup();
29988ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
299941bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3000f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
30011da177e4SLinus Torvalds }
3002