xref: /openbmc/linux/net/ipv6/route.c (revision 4e3fd7a06dc20b2d8ec6892233ad2012968fe7b6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Linux INET6 implementation
31da177e4SLinus Torvalds  *	FIB front-end.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
91da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
101da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
111da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds /*	Changes:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI
171da177e4SLinus Torvalds  *		reworked default router selection.
181da177e4SLinus Torvalds  *		- respect outgoing interface
191da177e4SLinus Torvalds  *		- select from (probably) reachable routers (i.e.
201da177e4SLinus Torvalds  *		routers in REACHABLE, STALE, DELAY or PROBE states).
211da177e4SLinus Torvalds  *		- always select the same router if it is (probably)
221da177e4SLinus Torvalds  *		reachable.  otherwise, round-robin the list.
23c0bece9fSYOSHIFUJI Hideaki  *	Ville Nuorvala
24c0bece9fSYOSHIFUJI Hideaki  *		Fixed routing subtrees.
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
274fc268d2SRandy Dunlap #include <linux/capability.h>
281da177e4SLinus Torvalds #include <linux/errno.h>
29bc3b2d7fSPaul Gortmaker #include <linux/export.h>
301da177e4SLinus Torvalds #include <linux/types.h>
311da177e4SLinus Torvalds #include <linux/times.h>
321da177e4SLinus Torvalds #include <linux/socket.h>
331da177e4SLinus Torvalds #include <linux/sockios.h>
341da177e4SLinus Torvalds #include <linux/net.h>
351da177e4SLinus Torvalds #include <linux/route.h>
361da177e4SLinus Torvalds #include <linux/netdevice.h>
371da177e4SLinus Torvalds #include <linux/in6.h>
387bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
391da177e4SLinus Torvalds #include <linux/init.h>
401da177e4SLinus Torvalds #include <linux/if_arp.h>
411da177e4SLinus Torvalds #include <linux/proc_fs.h>
421da177e4SLinus Torvalds #include <linux/seq_file.h>
435b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
445a0e3ad6STejun Heo #include <linux/slab.h>
45457c4cbcSEric W. Biederman #include <net/net_namespace.h>
461da177e4SLinus Torvalds #include <net/snmp.h>
471da177e4SLinus Torvalds #include <net/ipv6.h>
481da177e4SLinus Torvalds #include <net/ip6_fib.h>
491da177e4SLinus Torvalds #include <net/ip6_route.h>
501da177e4SLinus Torvalds #include <net/ndisc.h>
511da177e4SLinus Torvalds #include <net/addrconf.h>
521da177e4SLinus Torvalds #include <net/tcp.h>
531da177e4SLinus Torvalds #include <linux/rtnetlink.h>
541da177e4SLinus Torvalds #include <net/dst.h>
551da177e4SLinus Torvalds #include <net/xfrm.h>
568d71740cSTom Tucker #include <net/netevent.h>
5721713ebcSThomas Graf #include <net/netlink.h>
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #include <asm/uaccess.h>
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
621da177e4SLinus Torvalds #include <linux/sysctl.h>
631da177e4SLinus Torvalds #endif
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* Set to 3 to get tracing. */
661da177e4SLinus Torvalds #define RT6_DEBUG 2
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #if RT6_DEBUG >= 3
691da177e4SLinus Torvalds #define RDBG(x) printk x
701da177e4SLinus Torvalds #define RT6_TRACE(x...) printk(KERN_DEBUG x)
711da177e4SLinus Torvalds #else
721da177e4SLinus Torvalds #define RDBG(x)
731da177e4SLinus Torvalds #define RT6_TRACE(x...) do { ; } while (0)
741da177e4SLinus Torvalds #endif
751da177e4SLinus Torvalds 
7621efcfa0SEric Dumazet static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort,
7721efcfa0SEric Dumazet 				    const struct in6_addr *dest);
781da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
790dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
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;
732*4e3fd7a0SAlexey Dobriyan 			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) {
739*4e3fd7a0SAlexey Dobriyan 			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 
935*4e3fd7a0SAlexey Dobriyan 		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);
1090*4e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
10918e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
10928e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
10937011687fSGao feng 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
10941da177e4SLinus Torvalds 
10953b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1096d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1097d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
10983b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
10991da177e4SLinus Torvalds 
11005578689aSDaniel Lezcano 	fib6_force_start_gc(net);
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds out:
1103d8d1f30bSChangli Gao 	return &rt->dst;
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds 
11063d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
11071da177e4SLinus Torvalds {
1108e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
11093d0f24a7SStephen Hemminger 	int more = 0;
11101da177e4SLinus Torvalds 
11113b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
11123b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
11135d0bbeebSThomas Graf 
11141da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
11151da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
11161da177e4SLinus Torvalds 			*pprev = dst->next;
11171da177e4SLinus Torvalds 			dst_free(dst);
11181da177e4SLinus Torvalds 		} else {
11191da177e4SLinus Torvalds 			pprev = &dst->next;
11203d0f24a7SStephen Hemminger 			++more;
11211da177e4SLinus Torvalds 		}
11221da177e4SLinus Torvalds 	}
11231da177e4SLinus Torvalds 
11243b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
11255d0bbeebSThomas Graf 
11263d0f24a7SStephen Hemminger 	return more;
11271da177e4SLinus Torvalds }
11281da177e4SLinus Torvalds 
11291e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
11301e493d19SDavid S. Miller 			    void *arg)
11311e493d19SDavid S. Miller {
11321e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
11331e493d19SDavid S. Miller 
11341e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
11351e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
11361e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
11371e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
11381e493d19SDavid S. Miller 		if (func(rt, arg)) {
11391e493d19SDavid S. Miller 			*pprev = dst->next;
11401e493d19SDavid S. Miller 			dst_free(dst);
11411e493d19SDavid S. Miller 		} else {
11421e493d19SDavid S. Miller 			pprev = &dst->next;
11431e493d19SDavid S. Miller 		}
11441e493d19SDavid S. Miller 	}
11451e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
11461e493d19SDavid S. Miller }
11471e493d19SDavid S. Miller 
1148569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
11491da177e4SLinus Torvalds {
11501da177e4SLinus Torvalds 	unsigned long now = jiffies;
115186393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
11527019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
11537019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
11547019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
11557019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
11567019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1157fc66f95cSEric Dumazet 	int entries;
11581da177e4SLinus Torvalds 
1159fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
11607019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1161fc66f95cSEric Dumazet 	    entries <= rt_max_size)
11621da177e4SLinus Torvalds 		goto out;
11631da177e4SLinus Torvalds 
11646891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
11656891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
11666891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1167fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1168fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
11697019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
11701da177e4SLinus Torvalds out:
11717019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1172fc66f95cSEric Dumazet 	return entries > rt_max_size;
11731da177e4SLinus Torvalds }
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
11761da177e4SLinus Torvalds    but results in cleaner routing tables.
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds    Remove it only when all the things will work!
11791da177e4SLinus Torvalds  */
11801da177e4SLinus Torvalds 
11816b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
11821da177e4SLinus Torvalds {
11835170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1184a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
11856b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1186c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1187c68f24ccSEric Dumazet 
1188c68f24ccSEric Dumazet 		rcu_read_lock();
1189c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1190c68f24ccSEric Dumazet 		if (idev)
11911da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1192c68f24ccSEric Dumazet 		else
119353b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1194c68f24ccSEric Dumazet 		rcu_read_unlock();
11951da177e4SLinus Torvalds 	}
11961da177e4SLinus Torvalds 	return hoplimit;
11971da177e4SLinus Torvalds }
1198abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds /*
12011da177e4SLinus Torvalds  *
12021da177e4SLinus Torvalds  */
12031da177e4SLinus Torvalds 
120486872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
12051da177e4SLinus Torvalds {
12061da177e4SLinus Torvalds 	int err;
12075578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
12081da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
12091da177e4SLinus Torvalds 	struct net_device *dev = NULL;
12101da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1211c71099acSThomas Graf 	struct fib6_table *table;
12121da177e4SLinus Torvalds 	int addr_type;
12131da177e4SLinus Torvalds 
121486872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
12151da177e4SLinus Torvalds 		return -EINVAL;
12161da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
121786872cb5SThomas Graf 	if (cfg->fc_src_len)
12181da177e4SLinus Torvalds 		return -EINVAL;
12191da177e4SLinus Torvalds #endif
122086872cb5SThomas Graf 	if (cfg->fc_ifindex) {
12211da177e4SLinus Torvalds 		err = -ENODEV;
12225578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
12231da177e4SLinus Torvalds 		if (!dev)
12241da177e4SLinus Torvalds 			goto out;
12251da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
12261da177e4SLinus Torvalds 		if (!idev)
12271da177e4SLinus Torvalds 			goto out;
12281da177e4SLinus Torvalds 	}
12291da177e4SLinus Torvalds 
123086872cb5SThomas Graf 	if (cfg->fc_metric == 0)
123186872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
12321da177e4SLinus Torvalds 
1233c71099acSThomas Graf 	err = -ENOBUFS;
1234d71314b4SMatti Vaittinen 	if (NULL != cfg->fc_nlinfo.nlh &&
1235d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags&NLM_F_CREATE)) {
1236d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
1237d71314b4SMatti Vaittinen 		if (table == NULL) {
1238d71314b4SMatti Vaittinen 			printk(KERN_WARNING "IPv6: NLM_F_CREATE should be specified when creating new route\n");
1239d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1240d71314b4SMatti Vaittinen 		}
1241d71314b4SMatti Vaittinen 	} else {
1242d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1243d71314b4SMatti Vaittinen 	}
1244d71314b4SMatti Vaittinen 	if (table == NULL) {
1245c71099acSThomas Graf 		goto out;
1246c71099acSThomas Graf 	}
1247c71099acSThomas Graf 
1248957c665fSDavid S. Miller 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT);
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	if (rt == NULL) {
12511da177e4SLinus Torvalds 		err = -ENOMEM;
12521da177e4SLinus Torvalds 		goto out;
12531da177e4SLinus Torvalds 	}
12541da177e4SLinus Torvalds 
1255d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
12566f704992SYOSHIFUJI Hideaki 	rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
12576f704992SYOSHIFUJI Hideaki 				jiffies + clock_t_to_jiffies(cfg->fc_expires) :
12586f704992SYOSHIFUJI Hideaki 				0;
12591da177e4SLinus Torvalds 
126086872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
126186872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
126286872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
126386872cb5SThomas Graf 
126486872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1267d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1268ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1269ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
12701da177e4SLinus Torvalds 	else
1271d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
12721da177e4SLinus Torvalds 
1273d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
12741da177e4SLinus Torvalds 
127586872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
127686872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
12771da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
127811d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
12791da177e4SLinus Torvalds 
12808e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
12818e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
12828e2ec639SYan, Zheng 		if (!metrics) {
12838e2ec639SYan, Zheng 			err = -ENOMEM;
12848e2ec639SYan, Zheng 			goto out;
12858e2ec639SYan, Zheng 		}
12868e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
12878e2ec639SYan, Zheng 	}
12881da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
128986872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
129086872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
12911da177e4SLinus Torvalds #endif
12921da177e4SLinus Torvalds 
129386872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
12961da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
12971da177e4SLinus Torvalds 	 */
129886872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
1299ab79ad14SMaciej Żenczykowski 	    (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1300ab79ad14SMaciej Żenczykowski 					      && !(cfg->fc_flags&RTF_LOCAL))) {
13011da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
13025578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
13031da177e4SLinus Torvalds 			if (dev) {
13041da177e4SLinus Torvalds 				dev_put(dev);
13051da177e4SLinus Torvalds 				in6_dev_put(idev);
13061da177e4SLinus Torvalds 			}
13075578689aSDaniel Lezcano 			dev = net->loopback_dev;
13081da177e4SLinus Torvalds 			dev_hold(dev);
13091da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
13101da177e4SLinus Torvalds 			if (!idev) {
13111da177e4SLinus Torvalds 				err = -ENODEV;
13121da177e4SLinus Torvalds 				goto out;
13131da177e4SLinus Torvalds 			}
13141da177e4SLinus Torvalds 		}
1315d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1316d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
1317d8d1f30bSChangli Gao 		rt->dst.error = -ENETUNREACH;
13181da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
13191da177e4SLinus Torvalds 		goto install_route;
13201da177e4SLinus Torvalds 	}
13211da177e4SLinus Torvalds 
132286872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1323b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
13241da177e4SLinus Torvalds 		int gwa_type;
13251da177e4SLinus Torvalds 
132686872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
1327*4e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
13281da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
13311da177e4SLinus Torvalds 			struct rt6_info *grt;
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
13341da177e4SLinus Torvalds 			   addresses as nexthop address.
13351da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
13361da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
13371da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
13381da177e4SLinus Torvalds 			   some exceptions. --ANK
13391da177e4SLinus Torvalds 			 */
13401da177e4SLinus Torvalds 			err = -EINVAL;
13411da177e4SLinus Torvalds 			if (!(gwa_type&IPV6_ADDR_UNICAST))
13421da177e4SLinus Torvalds 				goto out;
13431da177e4SLinus Torvalds 
13445578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
13471da177e4SLinus Torvalds 			if (grt == NULL)
13481da177e4SLinus Torvalds 				goto out;
13491da177e4SLinus Torvalds 			if (dev) {
13501da177e4SLinus Torvalds 				if (dev != grt->rt6i_dev) {
1351d8d1f30bSChangli Gao 					dst_release(&grt->dst);
13521da177e4SLinus Torvalds 					goto out;
13531da177e4SLinus Torvalds 				}
13541da177e4SLinus Torvalds 			} else {
13551da177e4SLinus Torvalds 				dev = grt->rt6i_dev;
13561da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
13571da177e4SLinus Torvalds 				dev_hold(dev);
13581da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
13591da177e4SLinus Torvalds 			}
13601da177e4SLinus Torvalds 			if (!(grt->rt6i_flags&RTF_GATEWAY))
13611da177e4SLinus Torvalds 				err = 0;
1362d8d1f30bSChangli Gao 			dst_release(&grt->dst);
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 			if (err)
13651da177e4SLinus Torvalds 				goto out;
13661da177e4SLinus Torvalds 		}
13671da177e4SLinus Torvalds 		err = -EINVAL;
13681da177e4SLinus Torvalds 		if (dev == NULL || (dev->flags&IFF_LOOPBACK))
13691da177e4SLinus Torvalds 			goto out;
13701da177e4SLinus Torvalds 	}
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 	err = -ENODEV;
13731da177e4SLinus Torvalds 	if (dev == NULL)
13741da177e4SLinus Torvalds 		goto out;
13751da177e4SLinus Torvalds 
1376c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1377c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1378c3968a85SDaniel Walter 			err = -EINVAL;
1379c3968a85SDaniel Walter 			goto out;
1380c3968a85SDaniel Walter 		}
1381*4e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1382c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1383c3968a85SDaniel Walter 	} else
1384c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1385c3968a85SDaniel Walter 
138686872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
138769cce1d1SDavid S. Miller 		struct neighbour *n = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
138869cce1d1SDavid S. Miller 		if (IS_ERR(n)) {
138969cce1d1SDavid S. Miller 			err = PTR_ERR(n);
13901da177e4SLinus Torvalds 			goto out;
13911da177e4SLinus Torvalds 		}
139269cce1d1SDavid S. Miller 		dst_set_neighbour(&rt->dst, n);
13931da177e4SLinus Torvalds 	}
13941da177e4SLinus Torvalds 
139586872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds install_route:
139886872cb5SThomas Graf 	if (cfg->fc_mx) {
139986872cb5SThomas Graf 		struct nlattr *nla;
140086872cb5SThomas Graf 		int remaining;
14011da177e4SLinus Torvalds 
140286872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
14038f4c1f9bSThomas Graf 			int type = nla_type(nla);
140486872cb5SThomas Graf 
140586872cb5SThomas Graf 			if (type) {
140686872cb5SThomas Graf 				if (type > RTAX_MAX) {
14071da177e4SLinus Torvalds 					err = -EINVAL;
14081da177e4SLinus Torvalds 					goto out;
14091da177e4SLinus Torvalds 				}
141086872cb5SThomas Graf 
1411defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
14121da177e4SLinus Torvalds 			}
14131da177e4SLinus Torvalds 		}
14141da177e4SLinus Torvalds 	}
14151da177e4SLinus Torvalds 
1416d8d1f30bSChangli Gao 	rt->dst.dev = dev;
14171da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1418c71099acSThomas Graf 	rt->rt6i_table = table;
141963152fc0SDaniel Lezcano 
1420c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
142163152fc0SDaniel Lezcano 
142286872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds out:
14251da177e4SLinus Torvalds 	if (dev)
14261da177e4SLinus Torvalds 		dev_put(dev);
14271da177e4SLinus Torvalds 	if (idev)
14281da177e4SLinus Torvalds 		in6_dev_put(idev);
14291da177e4SLinus Torvalds 	if (rt)
1430d8d1f30bSChangli Gao 		dst_free(&rt->dst);
14311da177e4SLinus Torvalds 	return err;
14321da177e4SLinus Torvalds }
14331da177e4SLinus Torvalds 
143486872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
14351da177e4SLinus Torvalds {
14361da177e4SLinus Torvalds 	int err;
1437c71099acSThomas Graf 	struct fib6_table *table;
1438c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(rt->rt6i_dev);
14391da177e4SLinus Torvalds 
14408ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry)
14416c813a72SPatrick McHardy 		return -ENOENT;
14426c813a72SPatrick McHardy 
1443c71099acSThomas Graf 	table = rt->rt6i_table;
1444c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
14451da177e4SLinus Torvalds 
144686872cb5SThomas Graf 	err = fib6_del(rt, info);
1447d8d1f30bSChangli Gao 	dst_release(&rt->dst);
14481da177e4SLinus Torvalds 
1449c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	return err;
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds 
1454e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1455e0a1ad73SThomas Graf {
14564d1169c1SDenis V. Lunev 	struct nl_info info = {
1457c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
14584d1169c1SDenis V. Lunev 	};
1459528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1460e0a1ad73SThomas Graf }
1461e0a1ad73SThomas Graf 
146286872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
14631da177e4SLinus Torvalds {
1464c71099acSThomas Graf 	struct fib6_table *table;
14651da177e4SLinus Torvalds 	struct fib6_node *fn;
14661da177e4SLinus Torvalds 	struct rt6_info *rt;
14671da177e4SLinus Torvalds 	int err = -ESRCH;
14681da177e4SLinus Torvalds 
14695578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
1470c71099acSThomas Graf 	if (table == NULL)
1471c71099acSThomas Graf 		return err;
14721da177e4SLinus Torvalds 
1473c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1474c71099acSThomas Graf 
1475c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
147686872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
147786872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 	if (fn) {
1480d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
148186872cb5SThomas Graf 			if (cfg->fc_ifindex &&
14821da177e4SLinus Torvalds 			    (rt->rt6i_dev == NULL ||
148386872cb5SThomas Graf 			     rt->rt6i_dev->ifindex != cfg->fc_ifindex))
14841da177e4SLinus Torvalds 				continue;
148586872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
148686872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
14871da177e4SLinus Torvalds 				continue;
148886872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
14891da177e4SLinus Torvalds 				continue;
1490d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1491c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
14921da177e4SLinus Torvalds 
149386872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
14941da177e4SLinus Torvalds 		}
14951da177e4SLinus Torvalds 	}
1496c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	return err;
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds /*
15021da177e4SLinus Torvalds  *	Handle redirects
15031da177e4SLinus Torvalds  */
1504a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi {
15054c9483b2SDavid S. Miller 	struct flowi6 fl6;
1506a6279458SYOSHIFUJI Hideaki 	struct in6_addr gateway;
1507a6279458SYOSHIFUJI Hideaki };
15081da177e4SLinus Torvalds 
15098ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net,
15108ed67789SDaniel Lezcano 					     struct fib6_table *table,
15114c9483b2SDavid S. Miller 					     struct flowi6 *fl6,
1512a6279458SYOSHIFUJI Hideaki 					     int flags)
1513a6279458SYOSHIFUJI Hideaki {
15144c9483b2SDavid S. Miller 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1515a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt;
1516a6279458SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1517c71099acSThomas Graf 
1518e843b9e1SYOSHIFUJI Hideaki 	/*
1519e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1520e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1521e843b9e1SYOSHIFUJI Hideaki 	 *
1522e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1523e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1524e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1525e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1526e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1527e843b9e1SYOSHIFUJI Hideaki 	 */
15281da177e4SLinus Torvalds 
1529c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
15304c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1531e843b9e1SYOSHIFUJI Hideaki restart:
1532d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
15331da177e4SLinus Torvalds 		/*
15341da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
15351da177e4SLinus Torvalds 		 *
15361da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
15371da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
15381da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
15391da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
15401da177e4SLinus Torvalds 		 */
1541e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1542e843b9e1SYOSHIFUJI Hideaki 			continue;
15431da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1544e843b9e1SYOSHIFUJI Hideaki 			continue;
15454c9483b2SDavid S. Miller 		if (fl6->flowi6_oif != rt->rt6i_dev->ifindex)
1546e843b9e1SYOSHIFUJI Hideaki 			continue;
1547a6279458SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1548e843b9e1SYOSHIFUJI Hideaki 			continue;
1549e843b9e1SYOSHIFUJI Hideaki 		break;
1550e843b9e1SYOSHIFUJI Hideaki 	}
1551a6279458SYOSHIFUJI Hideaki 
1552cb15d9c2SYOSHIFUJI Hideaki 	if (!rt)
15538ed67789SDaniel Lezcano 		rt = net->ipv6.ip6_null_entry;
15544c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
1555cb15d9c2SYOSHIFUJI Hideaki out:
1556d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
1557a6279458SYOSHIFUJI Hideaki 
1558c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
15591da177e4SLinus Torvalds 
1560a6279458SYOSHIFUJI Hideaki 	return rt;
1561a6279458SYOSHIFUJI Hideaki };
1562a6279458SYOSHIFUJI Hideaki 
1563b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1564b71d1d42SEric Dumazet 					   const struct in6_addr *src,
1565b71d1d42SEric Dumazet 					   const struct in6_addr *gateway,
1566a6279458SYOSHIFUJI Hideaki 					   struct net_device *dev)
1567a6279458SYOSHIFUJI Hideaki {
1568adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1569c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
1570a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi rdfl = {
15714c9483b2SDavid S. Miller 		.fl6 = {
15724c9483b2SDavid S. Miller 			.flowi6_oif = dev->ifindex,
15734c9483b2SDavid S. Miller 			.daddr = *dest,
15744c9483b2SDavid S. Miller 			.saddr = *src,
1575a6279458SYOSHIFUJI Hideaki 		},
1576a6279458SYOSHIFUJI Hideaki 	};
1577adaa70bbSThomas Graf 
1578*4e3fd7a0SAlexey Dobriyan 	rdfl.gateway = *gateway;
157986c36ce4SBrian Haley 
1580adaa70bbSThomas Graf 	if (rt6_need_strict(dest))
1581adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
1582a6279458SYOSHIFUJI Hideaki 
15834c9483b2SDavid S. Miller 	return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
158458f09b78SDaniel Lezcano 						   flags, __ip6_route_redirect);
1585a6279458SYOSHIFUJI Hideaki }
1586a6279458SYOSHIFUJI Hideaki 
1587b71d1d42SEric Dumazet void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1588b71d1d42SEric Dumazet 		  const struct in6_addr *saddr,
1589a6279458SYOSHIFUJI Hideaki 		  struct neighbour *neigh, u8 *lladdr, int on_link)
1590a6279458SYOSHIFUJI Hideaki {
1591a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt = NULL;
1592a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1593c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(neigh->dev);
1594a6279458SYOSHIFUJI Hideaki 
1595a6279458SYOSHIFUJI Hideaki 	rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1596a6279458SYOSHIFUJI Hideaki 
15978ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry) {
15981da177e4SLinus Torvalds 		if (net_ratelimit())
15991da177e4SLinus Torvalds 			printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
16001da177e4SLinus Torvalds 			       "for redirect target\n");
1601a6279458SYOSHIFUJI Hideaki 		goto out;
16021da177e4SLinus Torvalds 	}
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	/*
16051da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
16061da177e4SLinus Torvalds 	 */
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
16091da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
16101da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
16111da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
16121da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
16131da177e4SLinus Torvalds 		     );
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds 	/*
16161da177e4SLinus Torvalds 	 * Redirect received -> path was valid.
16171da177e4SLinus Torvalds 	 * Look, redirects are sent only in response to data packets,
16181da177e4SLinus Torvalds 	 * so that this nexthop apparently is reachable. --ANK
16191da177e4SLinus Torvalds 	 */
1620d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 	/* Duplicate redirect: silently ignore. */
1623f2c31e32SEric Dumazet 	if (neigh == dst_get_neighbour_raw(&rt->dst))
16241da177e4SLinus Torvalds 		goto out;
16251da177e4SLinus Torvalds 
162621efcfa0SEric Dumazet 	nrt = ip6_rt_copy(rt, dest);
16271da177e4SLinus Torvalds 	if (nrt == NULL)
16281da177e4SLinus Torvalds 		goto out;
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
16311da177e4SLinus Torvalds 	if (on_link)
16321da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
16331da177e4SLinus Torvalds 
1634*4e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
163569cce1d1SDavid S. Miller 	dst_set_neighbour(&nrt->dst, neigh_clone(neigh));
16361da177e4SLinus Torvalds 
163740e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
16381da177e4SLinus Torvalds 		goto out;
16391da177e4SLinus Torvalds 
1640d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1641d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
16428d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
16438d71740cSTom Tucker 
16441da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE) {
1645e0a1ad73SThomas Graf 		ip6_del_rt(rt);
16461da177e4SLinus Torvalds 		return;
16471da177e4SLinus Torvalds 	}
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds out:
1650d8d1f30bSChangli Gao 	dst_release(&rt->dst);
16511da177e4SLinus Torvalds }
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds /*
16541da177e4SLinus Torvalds  *	Handle ICMP "packet too big" messages
16551da177e4SLinus Torvalds  *	i.e. Path MTU discovery
16561da177e4SLinus Torvalds  */
16571da177e4SLinus Torvalds 
1658b71d1d42SEric Dumazet static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
1659ae878ae2SMaciej Żenczykowski 			     struct net *net, u32 pmtu, int ifindex)
16601da177e4SLinus Torvalds {
16611da177e4SLinus Torvalds 	struct rt6_info *rt, *nrt;
16621da177e4SLinus Torvalds 	int allfrag = 0;
1663d3052b55SAndrey Vagin again:
1664ae878ae2SMaciej Żenczykowski 	rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
16651da177e4SLinus Torvalds 	if (rt == NULL)
16661da177e4SLinus Torvalds 		return;
16671da177e4SLinus Torvalds 
1668d3052b55SAndrey Vagin 	if (rt6_check_expired(rt)) {
1669d3052b55SAndrey Vagin 		ip6_del_rt(rt);
1670d3052b55SAndrey Vagin 		goto again;
1671d3052b55SAndrey Vagin 	}
1672d3052b55SAndrey Vagin 
1673d8d1f30bSChangli Gao 	if (pmtu >= dst_mtu(&rt->dst))
16741da177e4SLinus Torvalds 		goto out;
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	if (pmtu < IPV6_MIN_MTU) {
16771da177e4SLinus Torvalds 		/*
16781da177e4SLinus Torvalds 		 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
16791da177e4SLinus Torvalds 		 * MTU (1280) and a fragment header should always be included
16801da177e4SLinus Torvalds 		 * after a node receiving Too Big message reporting PMTU is
16811da177e4SLinus Torvalds 		 * less than the IPv6 Minimum Link MTU.
16821da177e4SLinus Torvalds 		 */
16831da177e4SLinus Torvalds 		pmtu = IPV6_MIN_MTU;
16841da177e4SLinus Torvalds 		allfrag = 1;
16851da177e4SLinus Torvalds 	}
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds 	/* New mtu received -> path was valid.
16881da177e4SLinus Torvalds 	   They are sent only in response to data packets,
16891da177e4SLinus Torvalds 	   so that this nexthop apparently is reachable. --ANK
16901da177e4SLinus Torvalds 	 */
1691d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds 	/* Host route. If it is static, it would be better
16941da177e4SLinus Torvalds 	   not to override it, but add new one, so that
16951da177e4SLinus Torvalds 	   when cache entry will expire old pmtu
16961da177e4SLinus Torvalds 	   would return automatically.
16971da177e4SLinus Torvalds 	 */
16981da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
1699defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1700defb3519SDavid S. Miller 		if (allfrag) {
1701defb3519SDavid S. Miller 			u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1702defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1703defb3519SDavid S. Miller 			dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1704defb3519SDavid S. Miller 		}
1705d8d1f30bSChangli Gao 		dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
17061da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
17071da177e4SLinus Torvalds 		goto out;
17081da177e4SLinus Torvalds 	}
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds 	/* Network route.
17111da177e4SLinus Torvalds 	   Two cases are possible:
17121da177e4SLinus Torvalds 	   1. It is connected route. Action: COW
17131da177e4SLinus Torvalds 	   2. It is gatewayed route or NONEXTHOP route. Action: clone it.
17141da177e4SLinus Torvalds 	 */
1715f2c31e32SEric Dumazet 	if (!dst_get_neighbour_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
1716a1e78363SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, daddr, saddr);
1717d5315b50SYOSHIFUJI Hideaki 	else
1718d5315b50SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, daddr);
1719a1e78363SYOSHIFUJI Hideaki 
1720d5315b50SYOSHIFUJI Hideaki 	if (nrt) {
1721defb3519SDavid S. Miller 		dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1722defb3519SDavid S. Miller 		if (allfrag) {
1723defb3519SDavid S. Miller 			u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1724defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1725defb3519SDavid S. Miller 			dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1726defb3519SDavid S. Miller 		}
1727a1e78363SYOSHIFUJI Hideaki 
17281da177e4SLinus Torvalds 		/* According to RFC 1981, detecting PMTU increase shouldn't be
1729a1e78363SYOSHIFUJI Hideaki 		 * happened within 5 mins, the recommended timer is 10 mins.
1730a1e78363SYOSHIFUJI Hideaki 		 * Here this route expiration time is set to ip6_rt_mtu_expires
1731a1e78363SYOSHIFUJI Hideaki 		 * which is 10 mins. After 10 mins the decreased pmtu is expired
1732a1e78363SYOSHIFUJI Hideaki 		 * and detecting PMTU increase will be automatically happened.
17331da177e4SLinus Torvalds 		 */
1734d8d1f30bSChangli Gao 		dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
17351da177e4SLinus Torvalds 		nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1736a1e78363SYOSHIFUJI Hideaki 
173740e22e8fSThomas Graf 		ip6_ins_rt(nrt);
17381da177e4SLinus Torvalds 	}
17391da177e4SLinus Torvalds out:
1740d8d1f30bSChangli Gao 	dst_release(&rt->dst);
17411da177e4SLinus Torvalds }
17421da177e4SLinus Torvalds 
1743b71d1d42SEric Dumazet void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
1744ae878ae2SMaciej Żenczykowski 			struct net_device *dev, u32 pmtu)
1745ae878ae2SMaciej Żenczykowski {
1746ae878ae2SMaciej Żenczykowski 	struct net *net = dev_net(dev);
1747ae878ae2SMaciej Żenczykowski 
1748ae878ae2SMaciej Żenczykowski 	/*
1749ae878ae2SMaciej Żenczykowski 	 * RFC 1981 states that a node "MUST reduce the size of the packets it
1750ae878ae2SMaciej Żenczykowski 	 * is sending along the path" that caused the Packet Too Big message.
1751ae878ae2SMaciej Żenczykowski 	 * Since it's not possible in the general case to determine which
1752ae878ae2SMaciej Żenczykowski 	 * interface was used to send the original packet, we update the MTU
1753ae878ae2SMaciej Żenczykowski 	 * on the interface that will be used to send future packets. We also
1754ae878ae2SMaciej Żenczykowski 	 * update the MTU on the interface that received the Packet Too Big in
1755ae878ae2SMaciej Żenczykowski 	 * case the original packet was forced out that interface with
1756ae878ae2SMaciej Żenczykowski 	 * SO_BINDTODEVICE or similar. This is the next best thing to the
1757ae878ae2SMaciej Żenczykowski 	 * correct behaviour, which would be to update the MTU on all
1758ae878ae2SMaciej Żenczykowski 	 * interfaces.
1759ae878ae2SMaciej Żenczykowski 	 */
1760ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1761ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1762ae878ae2SMaciej Żenczykowski }
1763ae878ae2SMaciej Żenczykowski 
17641da177e4SLinus Torvalds /*
17651da177e4SLinus Torvalds  *	Misc support functions
17661da177e4SLinus Torvalds  */
17671da177e4SLinus Torvalds 
176821efcfa0SEric Dumazet static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort,
176921efcfa0SEric Dumazet 				    const struct in6_addr *dest)
17701da177e4SLinus Torvalds {
1771c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ort->rt6i_dev);
17725c1e6aa3SDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
1773957c665fSDavid S. Miller 					    ort->dst.dev, 0);
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds 	if (rt) {
1776d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1777d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
17788e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
17791da177e4SLinus Torvalds 
1780*4e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
17818e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1782defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1783d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
17841da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
17851da177e4SLinus Torvalds 		if (rt->rt6i_idev)
17861da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1787d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
17881da177e4SLinus Torvalds 		rt->rt6i_expires = 0;
17891da177e4SLinus Torvalds 
1790*4e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
17911da177e4SLinus Torvalds 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
17921da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
17951da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
17961da177e4SLinus Torvalds #endif
17970f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1798c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
17991da177e4SLinus Torvalds 	}
18001da177e4SLinus Torvalds 	return rt;
18011da177e4SLinus Torvalds }
18021da177e4SLinus Torvalds 
180370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1804efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1805b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1806b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
180770ceb4f5SYOSHIFUJI Hideaki {
180870ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
180970ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1810c71099acSThomas Graf 	struct fib6_table *table;
181170ceb4f5SYOSHIFUJI Hideaki 
1812efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
1813c71099acSThomas Graf 	if (table == NULL)
1814c71099acSThomas Graf 		return NULL;
1815c71099acSThomas Graf 
1816c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1817c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
181870ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
181970ceb4f5SYOSHIFUJI Hideaki 		goto out;
182070ceb4f5SYOSHIFUJI Hideaki 
1821d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
182270ceb4f5SYOSHIFUJI Hideaki 		if (rt->rt6i_dev->ifindex != ifindex)
182370ceb4f5SYOSHIFUJI Hideaki 			continue;
182470ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
182570ceb4f5SYOSHIFUJI Hideaki 			continue;
182670ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
182770ceb4f5SYOSHIFUJI Hideaki 			continue;
1828d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
182970ceb4f5SYOSHIFUJI Hideaki 		break;
183070ceb4f5SYOSHIFUJI Hideaki 	}
183170ceb4f5SYOSHIFUJI Hideaki out:
1832c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
183370ceb4f5SYOSHIFUJI Hideaki 	return rt;
183470ceb4f5SYOSHIFUJI Hideaki }
183570ceb4f5SYOSHIFUJI Hideaki 
1836efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1837b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1838b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
183970ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref)
184070ceb4f5SYOSHIFUJI Hideaki {
184186872cb5SThomas Graf 	struct fib6_config cfg = {
184286872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1843238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
184486872cb5SThomas Graf 		.fc_ifindex	= ifindex,
184586872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
184686872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
184786872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
1848efa2cea0SDaniel Lezcano 		.fc_nlinfo.pid = 0,
1849efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1850efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
185186872cb5SThomas Graf 	};
185270ceb4f5SYOSHIFUJI Hideaki 
1853*4e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
1854*4e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
185586872cb5SThomas Graf 
1856e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1857e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
185886872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
185970ceb4f5SYOSHIFUJI Hideaki 
186086872cb5SThomas Graf 	ip6_route_add(&cfg);
186170ceb4f5SYOSHIFUJI Hideaki 
1862efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
186370ceb4f5SYOSHIFUJI Hideaki }
186470ceb4f5SYOSHIFUJI Hideaki #endif
186570ceb4f5SYOSHIFUJI Hideaki 
1866b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
18671da177e4SLinus Torvalds {
18681da177e4SLinus Torvalds 	struct rt6_info *rt;
1869c71099acSThomas Graf 	struct fib6_table *table;
18701da177e4SLinus Torvalds 
1871c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
1872c71099acSThomas Graf 	if (table == NULL)
1873c71099acSThomas Graf 		return NULL;
18741da177e4SLinus Torvalds 
1875c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1876d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
18771da177e4SLinus Torvalds 		if (dev == rt->rt6i_dev &&
1878045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
18791da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
18801da177e4SLinus Torvalds 			break;
18811da177e4SLinus Torvalds 	}
18821da177e4SLinus Torvalds 	if (rt)
1883d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
1884c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
18851da177e4SLinus Torvalds 	return rt;
18861da177e4SLinus Torvalds }
18871da177e4SLinus Torvalds 
1888b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1889ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1890ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
18911da177e4SLinus Torvalds {
189286872cb5SThomas Graf 	struct fib6_config cfg = {
189386872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1894238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
189586872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
189686872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
189786872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
18985578689aSDaniel Lezcano 		.fc_nlinfo.pid = 0,
18995578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1900c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
190186872cb5SThomas Graf 	};
19021da177e4SLinus Torvalds 
1903*4e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
19041da177e4SLinus Torvalds 
190586872cb5SThomas Graf 	ip6_route_add(&cfg);
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
19081da177e4SLinus Torvalds }
19091da177e4SLinus Torvalds 
19107b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
19111da177e4SLinus Torvalds {
19121da177e4SLinus Torvalds 	struct rt6_info *rt;
1913c71099acSThomas Graf 	struct fib6_table *table;
1914c71099acSThomas Graf 
1915c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
19167b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
1917c71099acSThomas Graf 	if (table == NULL)
1918c71099acSThomas Graf 		return;
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds restart:
1921c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1922d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
19231da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1924d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1925c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1926e0a1ad73SThomas Graf 			ip6_del_rt(rt);
19271da177e4SLinus Torvalds 			goto restart;
19281da177e4SLinus Torvalds 		}
19291da177e4SLinus Torvalds 	}
1930c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
19311da177e4SLinus Torvalds }
19321da177e4SLinus Torvalds 
19335578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
19345578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
193586872cb5SThomas Graf 				 struct fib6_config *cfg)
193686872cb5SThomas Graf {
193786872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
193886872cb5SThomas Graf 
193986872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
194086872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
194186872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
194286872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
194386872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
194486872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
194586872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
194686872cb5SThomas Graf 
19475578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1948f1243c2dSBenjamin Thery 
1949*4e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
1950*4e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
1951*4e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
195286872cb5SThomas Graf }
195386872cb5SThomas Graf 
19545578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
19551da177e4SLinus Torvalds {
195686872cb5SThomas Graf 	struct fib6_config cfg;
19571da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
19581da177e4SLinus Torvalds 	int err;
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds 	switch(cmd) {
19611da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
19621da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
19631da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
19641da177e4SLinus Torvalds 			return -EPERM;
19651da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
19661da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
19671da177e4SLinus Torvalds 		if (err)
19681da177e4SLinus Torvalds 			return -EFAULT;
19691da177e4SLinus Torvalds 
19705578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
197186872cb5SThomas Graf 
19721da177e4SLinus Torvalds 		rtnl_lock();
19731da177e4SLinus Torvalds 		switch (cmd) {
19741da177e4SLinus Torvalds 		case SIOCADDRT:
197586872cb5SThomas Graf 			err = ip6_route_add(&cfg);
19761da177e4SLinus Torvalds 			break;
19771da177e4SLinus Torvalds 		case SIOCDELRT:
197886872cb5SThomas Graf 			err = ip6_route_del(&cfg);
19791da177e4SLinus Torvalds 			break;
19801da177e4SLinus Torvalds 		default:
19811da177e4SLinus Torvalds 			err = -EINVAL;
19821da177e4SLinus Torvalds 		}
19831da177e4SLinus Torvalds 		rtnl_unlock();
19841da177e4SLinus Torvalds 
19851da177e4SLinus Torvalds 		return err;
19863ff50b79SStephen Hemminger 	}
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds 	return -EINVAL;
19891da177e4SLinus Torvalds }
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds /*
19921da177e4SLinus Torvalds  *	Drop the packet on the floor
19931da177e4SLinus Torvalds  */
19941da177e4SLinus Torvalds 
1995d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
19961da177e4SLinus Torvalds {
1997612f09e8SYOSHIFUJI Hideaki 	int type;
1998adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
1999612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2000612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
20010660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
200245bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
20033bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20043bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2005612f09e8SYOSHIFUJI Hideaki 			break;
2006612f09e8SYOSHIFUJI Hideaki 		}
2007612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2008612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
20093bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20103bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2011612f09e8SYOSHIFUJI Hideaki 		break;
2012612f09e8SYOSHIFUJI Hideaki 	}
20133ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
20141da177e4SLinus Torvalds 	kfree_skb(skb);
20151da177e4SLinus Torvalds 	return 0;
20161da177e4SLinus Torvalds }
20171da177e4SLinus Torvalds 
20189ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
20199ce8ade0SThomas Graf {
2020612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
20219ce8ade0SThomas Graf }
20229ce8ade0SThomas Graf 
202320380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
20241da177e4SLinus Torvalds {
2025adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2026612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
20271da177e4SLinus Torvalds }
20281da177e4SLinus Torvalds 
20296723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
20306723ab54SDavid S. Miller 
20319ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
20329ce8ade0SThomas Graf {
2033612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
20349ce8ade0SThomas Graf }
20359ce8ade0SThomas Graf 
20369ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
20379ce8ade0SThomas Graf {
2038adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2039612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
20409ce8ade0SThomas Graf }
20419ce8ade0SThomas Graf 
20426723ab54SDavid S. Miller #endif
20436723ab54SDavid S. Miller 
20441da177e4SLinus Torvalds /*
20451da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
20461da177e4SLinus Torvalds  */
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
20491da177e4SLinus Torvalds 				    const struct in6_addr *addr,
20501da177e4SLinus Torvalds 				    int anycast)
20511da177e4SLinus Torvalds {
2052c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
20535c1e6aa3SDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
2054957c665fSDavid S. Miller 					    net->loopback_dev, 0);
205514deae41SDavid S. Miller 	struct neighbour *neigh;
20561da177e4SLinus Torvalds 
205740385653SBen Greear 	if (rt == NULL) {
205840385653SBen Greear 		if (net_ratelimit())
205940385653SBen Greear 			pr_warning("IPv6:  Maximum number of routes reached,"
206040385653SBen Greear 				   " consider increasing route/max_size.\n");
20611da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
206240385653SBen Greear 	}
20631da177e4SLinus Torvalds 
20641da177e4SLinus Torvalds 	in6_dev_hold(idev);
20651da177e4SLinus Torvalds 
206611d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2067d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2068d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
20691da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2070d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
207358c4fb86SYOSHIFUJI Hideaki 	if (anycast)
207458c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
207558c4fb86SYOSHIFUJI Hideaki 	else
20761da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
207714deae41SDavid S. Miller 	neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
207814deae41SDavid S. Miller 	if (IS_ERR(neigh)) {
2079d8d1f30bSChangli Gao 		dst_free(&rt->dst);
208014deae41SDavid S. Miller 
208129546a64SDavid S. Miller 		return ERR_CAST(neigh);
20821da177e4SLinus Torvalds 	}
208369cce1d1SDavid S. Miller 	dst_set_neighbour(&rt->dst, neigh);
20841da177e4SLinus Torvalds 
2085*4e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
20861da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
20875578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
20881da177e4SLinus Torvalds 
2089d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds 	return rt;
20921da177e4SLinus Torvalds }
20931da177e4SLinus Torvalds 
2094c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2095c3968a85SDaniel Walter 			struct rt6_info *rt,
2096b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2097c3968a85SDaniel Walter 			unsigned int prefs,
2098c3968a85SDaniel Walter 			struct in6_addr *saddr)
2099c3968a85SDaniel Walter {
2100c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2101c3968a85SDaniel Walter 	int err = 0;
2102c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
2103*4e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2104c3968a85SDaniel Walter 	else
2105c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2106c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2107c3968a85SDaniel Walter 	return err;
2108c3968a85SDaniel Walter }
2109c3968a85SDaniel Walter 
2110c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2111c3968a85SDaniel Walter struct arg_dev_net_ip {
2112c3968a85SDaniel Walter 	struct net_device *dev;
2113c3968a85SDaniel Walter 	struct net *net;
2114c3968a85SDaniel Walter 	struct in6_addr *addr;
2115c3968a85SDaniel Walter };
2116c3968a85SDaniel Walter 
2117c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2118c3968a85SDaniel Walter {
2119c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2120c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2121c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2122c3968a85SDaniel Walter 
2123c3968a85SDaniel Walter 	if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
2124c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2125c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2126c3968a85SDaniel Walter 		/* remove prefsrc entry */
2127c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2128c3968a85SDaniel Walter 	}
2129c3968a85SDaniel Walter 	return 0;
2130c3968a85SDaniel Walter }
2131c3968a85SDaniel Walter 
2132c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2133c3968a85SDaniel Walter {
2134c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2135c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2136c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2137c3968a85SDaniel Walter 		.net = net,
2138c3968a85SDaniel Walter 		.addr = &ifp->addr,
2139c3968a85SDaniel Walter 	};
2140c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2141c3968a85SDaniel Walter }
2142c3968a85SDaniel Walter 
21438ed67789SDaniel Lezcano struct arg_dev_net {
21448ed67789SDaniel Lezcano 	struct net_device *dev;
21458ed67789SDaniel Lezcano 	struct net *net;
21468ed67789SDaniel Lezcano };
21478ed67789SDaniel Lezcano 
21481da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
21491da177e4SLinus Torvalds {
2150bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2151bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
21528ed67789SDaniel Lezcano 
2153bc3ef660Sstephen hemminger 	if ((rt->rt6i_dev == dev || dev == NULL) &&
2154bc3ef660Sstephen hemminger 	    rt != adn->net->ipv6.ip6_null_entry) {
21551da177e4SLinus Torvalds 		RT6_TRACE("deleted by ifdown %p\n", rt);
21561da177e4SLinus Torvalds 		return -1;
21571da177e4SLinus Torvalds 	}
21581da177e4SLinus Torvalds 	return 0;
21591da177e4SLinus Torvalds }
21601da177e4SLinus Torvalds 
2161f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
21621da177e4SLinus Torvalds {
21638ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
21648ed67789SDaniel Lezcano 		.dev = dev,
21658ed67789SDaniel Lezcano 		.net = net,
21668ed67789SDaniel Lezcano 	};
21678ed67789SDaniel Lezcano 
21688ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
21691e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
21701da177e4SLinus Torvalds }
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds struct rt6_mtu_change_arg
21731da177e4SLinus Torvalds {
21741da177e4SLinus Torvalds 	struct net_device *dev;
21751da177e4SLinus Torvalds 	unsigned mtu;
21761da177e4SLinus Torvalds };
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
21791da177e4SLinus Torvalds {
21801da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
21811da177e4SLinus Torvalds 	struct inet6_dev *idev;
21821da177e4SLinus Torvalds 
21831da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
21841da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
21851da177e4SLinus Torvalds 	   We still use this lock to block changes
21861da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
21871da177e4SLinus Torvalds 	*/
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
21901da177e4SLinus Torvalds 	if (idev == NULL)
21911da177e4SLinus Torvalds 		return 0;
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
21941da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
21951da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
21961da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
21971da177e4SLinus Torvalds 	 */
21981da177e4SLinus Torvalds 	/*
21991da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
22001da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
22011da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
22021da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
22031da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
22041da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
22051da177e4SLinus Torvalds 	   PMTU discouvery.
22061da177e4SLinus Torvalds 	 */
22071da177e4SLinus Torvalds 	if (rt->rt6i_dev == arg->dev &&
2208d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2209d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2210d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2211d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2212defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2213566cfd8fSSimon Arlott 	}
22141da177e4SLinus Torvalds 	return 0;
22151da177e4SLinus Torvalds }
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu)
22181da177e4SLinus Torvalds {
2219c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2220c71099acSThomas Graf 		.dev = dev,
2221c71099acSThomas Graf 		.mtu = mtu,
2222c71099acSThomas Graf 	};
22231da177e4SLinus Torvalds 
2224c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
22251da177e4SLinus Torvalds }
22261da177e4SLinus Torvalds 
2227ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
22285176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
222986872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2230ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
223186872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
223286872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
223386872cb5SThomas Graf };
223486872cb5SThomas Graf 
223586872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
223686872cb5SThomas Graf 			      struct fib6_config *cfg)
22371da177e4SLinus Torvalds {
223886872cb5SThomas Graf 	struct rtmsg *rtm;
223986872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
224086872cb5SThomas Graf 	int err;
22411da177e4SLinus Torvalds 
224286872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
224386872cb5SThomas Graf 	if (err < 0)
224486872cb5SThomas Graf 		goto errout;
22451da177e4SLinus Torvalds 
224686872cb5SThomas Graf 	err = -EINVAL;
224786872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
224886872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
224986872cb5SThomas Graf 
225086872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
225186872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
225286872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
225386872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
225486872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
225586872cb5SThomas Graf 
225686872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
225786872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
225886872cb5SThomas Graf 
2259ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2260ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2261ab79ad14SMaciej Żenczykowski 
226286872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
226386872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
22643b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
226586872cb5SThomas Graf 
226686872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
226786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
226886872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
22691da177e4SLinus Torvalds 	}
227086872cb5SThomas Graf 
227186872cb5SThomas Graf 	if (tb[RTA_DST]) {
227286872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
227386872cb5SThomas Graf 
227486872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
227586872cb5SThomas Graf 			goto errout;
227686872cb5SThomas Graf 
227786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
22781da177e4SLinus Torvalds 	}
227986872cb5SThomas Graf 
228086872cb5SThomas Graf 	if (tb[RTA_SRC]) {
228186872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
228286872cb5SThomas Graf 
228386872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
228486872cb5SThomas Graf 			goto errout;
228586872cb5SThomas Graf 
228686872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
22871da177e4SLinus Torvalds 	}
228886872cb5SThomas Graf 
2289c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2290c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2291c3968a85SDaniel Walter 
229286872cb5SThomas Graf 	if (tb[RTA_OIF])
229386872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
229486872cb5SThomas Graf 
229586872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
229686872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
229786872cb5SThomas Graf 
229886872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
229986872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
230086872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
23011da177e4SLinus Torvalds 	}
230286872cb5SThomas Graf 
230386872cb5SThomas Graf 	if (tb[RTA_TABLE])
230486872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
230586872cb5SThomas Graf 
230686872cb5SThomas Graf 	err = 0;
230786872cb5SThomas Graf errout:
230886872cb5SThomas Graf 	return err;
23091da177e4SLinus Torvalds }
23101da177e4SLinus Torvalds 
2311c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23121da177e4SLinus Torvalds {
231386872cb5SThomas Graf 	struct fib6_config cfg;
231486872cb5SThomas Graf 	int err;
23151da177e4SLinus Torvalds 
231686872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
231786872cb5SThomas Graf 	if (err < 0)
231886872cb5SThomas Graf 		return err;
231986872cb5SThomas Graf 
232086872cb5SThomas Graf 	return ip6_route_del(&cfg);
23211da177e4SLinus Torvalds }
23221da177e4SLinus Torvalds 
2323c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23241da177e4SLinus Torvalds {
232586872cb5SThomas Graf 	struct fib6_config cfg;
232686872cb5SThomas Graf 	int err;
23271da177e4SLinus Torvalds 
232886872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
232986872cb5SThomas Graf 	if (err < 0)
233086872cb5SThomas Graf 		return err;
233186872cb5SThomas Graf 
233286872cb5SThomas Graf 	return ip6_route_add(&cfg);
23331da177e4SLinus Torvalds }
23341da177e4SLinus Torvalds 
2335339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2336339bf98fSThomas Graf {
2337339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2338339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2339339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2340339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2341339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2342339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2343339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2344339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2345339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
23466a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2347339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2348339bf98fSThomas Graf }
2349339bf98fSThomas Graf 
2350191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2351191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
23520d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
23530d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
23547bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
23551da177e4SLinus Torvalds {
23561da177e4SLinus Torvalds 	struct rtmsg *rtm;
23571da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2358e3703b3dSThomas Graf 	long expires;
23599e762a4aSPatrick McHardy 	u32 table;
2360f2c31e32SEric Dumazet 	struct neighbour *n;
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
23631da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
23641da177e4SLinus Torvalds 			/* success since this is not a prefix route */
23651da177e4SLinus Torvalds 			return 1;
23661da177e4SLinus Torvalds 		}
23671da177e4SLinus Torvalds 	}
23681da177e4SLinus Torvalds 
23692d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
23702d7202bfSThomas Graf 	if (nlh == NULL)
237126932566SPatrick McHardy 		return -EMSGSIZE;
23722d7202bfSThomas Graf 
23732d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
23741da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
23751da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
23761da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
23771da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2378c71099acSThomas Graf 	if (rt->rt6i_table)
23799e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2380c71099acSThomas Graf 	else
23819e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
23829e762a4aSPatrick McHardy 	rtm->rtm_table = table;
23832d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_TABLE, table);
23841da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_REJECT)
23851da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
2386ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags&RTF_LOCAL)
2387ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
23881da177e4SLinus Torvalds 	else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
23891da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
23901da177e4SLinus Torvalds 	else
23911da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
23921da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
23931da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
23941da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
23951da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_DYNAMIC)
23961da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
23971da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
23981da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
23991da177e4SLinus Torvalds 	else if (rt->rt6i_flags&RTF_DEFAULT)
24001da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
24011da177e4SLinus Torvalds 
24021da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE)
24031da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
24041da177e4SLinus Torvalds 
24051da177e4SLinus Torvalds 	if (dst) {
24062d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, dst);
24071da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
24081da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
24092d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
24101da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
24111da177e4SLinus Torvalds 	if (src) {
24122d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, src);
24131da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
24141da177e4SLinus Torvalds 	} else if (rtm->rtm_src_len)
24152d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
24161da177e4SLinus Torvalds #endif
24177bc570c8SYOSHIFUJI Hideaki 	if (iif) {
24187bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
24197bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
24208229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
24217bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
24227bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
24237bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
24247bc570c8SYOSHIFUJI Hideaki 						return 0;
24257bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
24267bc570c8SYOSHIFUJI Hideaki 				} else {
24277bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
24287bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
24297bc570c8SYOSHIFUJI Hideaki 				}
24307bc570c8SYOSHIFUJI Hideaki 			}
24317bc570c8SYOSHIFUJI Hideaki 		} else
24327bc570c8SYOSHIFUJI Hideaki #endif
24332d7202bfSThomas Graf 			NLA_PUT_U32(skb, RTA_IIF, iif);
24347bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
24351da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2436c3968a85SDaniel Walter 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0)
2437c3968a85SDaniel Walter 			NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2438c3968a85SDaniel Walter 	}
2439c3968a85SDaniel Walter 
2440c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2441c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
2442*4e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
24432d7202bfSThomas Graf 		NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
24441da177e4SLinus Torvalds 	}
24452d7202bfSThomas Graf 
2446defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
24472d7202bfSThomas Graf 		goto nla_put_failure;
24482d7202bfSThomas Graf 
2449f2c31e32SEric Dumazet 	rcu_read_lock();
2450f2c31e32SEric Dumazet 	n = dst_get_neighbour(&rt->dst);
2451f2c31e32SEric Dumazet 	if (n)
2452f2c31e32SEric Dumazet 		NLA_PUT(skb, RTA_GATEWAY, 16, &n->primary_key);
2453f2c31e32SEric Dumazet 	rcu_read_unlock();
24542d7202bfSThomas Graf 
2455d8d1f30bSChangli Gao 	if (rt->dst.dev)
24562d7202bfSThomas Graf 		NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
24572d7202bfSThomas Graf 
24582d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
2459e3703b3dSThomas Graf 
246036e3deaeSYOSHIFUJI Hideaki 	if (!(rt->rt6i_flags & RTF_EXPIRES))
246136e3deaeSYOSHIFUJI Hideaki 		expires = 0;
246236e3deaeSYOSHIFUJI Hideaki 	else if (rt->rt6i_expires - jiffies < INT_MAX)
246336e3deaeSYOSHIFUJI Hideaki 		expires = rt->rt6i_expires - jiffies;
246436e3deaeSYOSHIFUJI Hideaki 	else
246536e3deaeSYOSHIFUJI Hideaki 		expires = INT_MAX;
246669cdf8f9SYOSHIFUJI Hideaki 
2467d8d1f30bSChangli Gao 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2468d8d1f30bSChangli Gao 			       expires, rt->dst.error) < 0)
2469e3703b3dSThomas Graf 		goto nla_put_failure;
24701da177e4SLinus Torvalds 
24712d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
24722d7202bfSThomas Graf 
24732d7202bfSThomas Graf nla_put_failure:
247426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
247526932566SPatrick McHardy 	return -EMSGSIZE;
24761da177e4SLinus Torvalds }
24771da177e4SLinus Torvalds 
24781b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
24791da177e4SLinus Torvalds {
24801da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
24811da177e4SLinus Torvalds 	int prefix;
24821da177e4SLinus Torvalds 
24832d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
24842d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
24851da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
24861da177e4SLinus Torvalds 	} else
24871da177e4SLinus Torvalds 		prefix = 0;
24881da177e4SLinus Torvalds 
2489191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2490191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
24911da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
24927bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
24931da177e4SLinus Torvalds }
24941da177e4SLinus Torvalds 
2495c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
24961da177e4SLinus Torvalds {
24973b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2498ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
24991da177e4SLinus Torvalds 	struct rt6_info *rt;
2500ab364a6fSThomas Graf 	struct sk_buff *skb;
2501ab364a6fSThomas Graf 	struct rtmsg *rtm;
25024c9483b2SDavid S. Miller 	struct flowi6 fl6;
2503ab364a6fSThomas Graf 	int err, iif = 0;
2504ab364a6fSThomas Graf 
2505ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2506ab364a6fSThomas Graf 	if (err < 0)
2507ab364a6fSThomas Graf 		goto errout;
2508ab364a6fSThomas Graf 
2509ab364a6fSThomas Graf 	err = -EINVAL;
25104c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2511ab364a6fSThomas Graf 
2512ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2513ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2514ab364a6fSThomas Graf 			goto errout;
2515ab364a6fSThomas Graf 
2516*4e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2517ab364a6fSThomas Graf 	}
2518ab364a6fSThomas Graf 
2519ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2520ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2521ab364a6fSThomas Graf 			goto errout;
2522ab364a6fSThomas Graf 
2523*4e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2524ab364a6fSThomas Graf 	}
2525ab364a6fSThomas Graf 
2526ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2527ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2528ab364a6fSThomas Graf 
2529ab364a6fSThomas Graf 	if (tb[RTA_OIF])
25304c9483b2SDavid S. Miller 		fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]);
2531ab364a6fSThomas Graf 
2532ab364a6fSThomas Graf 	if (iif) {
2533ab364a6fSThomas Graf 		struct net_device *dev;
25345578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2535ab364a6fSThomas Graf 		if (!dev) {
2536ab364a6fSThomas Graf 			err = -ENODEV;
2537ab364a6fSThomas Graf 			goto errout;
2538ab364a6fSThomas Graf 		}
2539ab364a6fSThomas Graf 	}
25401da177e4SLinus Torvalds 
25411da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2542ab364a6fSThomas Graf 	if (skb == NULL) {
2543ab364a6fSThomas Graf 		err = -ENOBUFS;
2544ab364a6fSThomas Graf 		goto errout;
2545ab364a6fSThomas Graf 	}
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
25481da177e4SLinus Torvalds 	   through good chunk of routing engine.
25491da177e4SLinus Torvalds 	 */
2550459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
25511da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
25521da177e4SLinus Torvalds 
25534c9483b2SDavid S. Miller 	rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6);
2554d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
25551da177e4SLinus Torvalds 
25564c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
25571da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
25587bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
25591da177e4SLinus Torvalds 	if (err < 0) {
2560ab364a6fSThomas Graf 		kfree_skb(skb);
2561ab364a6fSThomas Graf 		goto errout;
25621da177e4SLinus Torvalds 	}
25631da177e4SLinus Torvalds 
25645578689aSDaniel Lezcano 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2565ab364a6fSThomas Graf errout:
25661da177e4SLinus Torvalds 	return err;
25671da177e4SLinus Torvalds }
25681da177e4SLinus Torvalds 
256986872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
25701da177e4SLinus Torvalds {
25711da177e4SLinus Torvalds 	struct sk_buff *skb;
25725578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2573528c4cebSDenis V. Lunev 	u32 seq;
2574528c4cebSDenis V. Lunev 	int err;
25750d51aa80SJamal Hadi Salim 
2576528c4cebSDenis V. Lunev 	err = -ENOBUFS;
2577528c4cebSDenis V. Lunev 	seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
257886872cb5SThomas Graf 
2579339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
258021713ebcSThomas Graf 	if (skb == NULL)
258121713ebcSThomas Graf 		goto errout;
25821da177e4SLinus Torvalds 
2583191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
25847bc570c8SYOSHIFUJI Hideaki 				event, info->pid, seq, 0, 0, 0);
258526932566SPatrick McHardy 	if (err < 0) {
258626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
258726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
258826932566SPatrick McHardy 		kfree_skb(skb);
258926932566SPatrick McHardy 		goto errout;
259026932566SPatrick McHardy 	}
25911ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
25925578689aSDaniel Lezcano 		    info->nlh, gfp_any());
25931ce85fe4SPablo Neira Ayuso 	return;
259421713ebcSThomas Graf errout:
259521713ebcSThomas Graf 	if (err < 0)
25965578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
25971da177e4SLinus Torvalds }
25981da177e4SLinus Torvalds 
25998ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
26008ed67789SDaniel Lezcano 				unsigned long event, void *data)
26018ed67789SDaniel Lezcano {
26028ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2603c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26048ed67789SDaniel Lezcano 
26058ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2606d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
26078ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
26088ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2609d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
26108ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2611d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
26128ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
26138ed67789SDaniel Lezcano #endif
26148ed67789SDaniel Lezcano 	}
26158ed67789SDaniel Lezcano 
26168ed67789SDaniel Lezcano 	return NOTIFY_OK;
26178ed67789SDaniel Lezcano }
26188ed67789SDaniel Lezcano 
26191da177e4SLinus Torvalds /*
26201da177e4SLinus Torvalds  *	/proc
26211da177e4SLinus Torvalds  */
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds struct rt6_proc_arg
26261da177e4SLinus Torvalds {
26271da177e4SLinus Torvalds 	char *buffer;
26281da177e4SLinus Torvalds 	int offset;
26291da177e4SLinus Torvalds 	int length;
26301da177e4SLinus Torvalds 	int skip;
26311da177e4SLinus Torvalds 	int len;
26321da177e4SLinus Torvalds };
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
26351da177e4SLinus Torvalds {
263633120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
263769cce1d1SDavid S. Miller 	struct neighbour *n;
26381da177e4SLinus Torvalds 
26394b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
26424b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
26431da177e4SLinus Torvalds #else
264433120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
26451da177e4SLinus Torvalds #endif
2646f2c31e32SEric Dumazet 	rcu_read_lock();
264769cce1d1SDavid S. Miller 	n = dst_get_neighbour(&rt->dst);
264869cce1d1SDavid S. Miller 	if (n) {
264969cce1d1SDavid S. Miller 		seq_printf(m, "%pi6", n->primary_key);
26501da177e4SLinus Torvalds 	} else {
265133120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
26521da177e4SLinus Torvalds 	}
2653f2c31e32SEric Dumazet 	rcu_read_unlock();
265433120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2655d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2656d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
26571da177e4SLinus Torvalds 		   rt->rt6i_dev ? rt->rt6i_dev->name : "");
26581da177e4SLinus Torvalds 	return 0;
26591da177e4SLinus Torvalds }
26601da177e4SLinus Torvalds 
266133120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
26621da177e4SLinus Torvalds {
2663f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
2664f3db4851SDaniel Lezcano 	fib6_clean_all(net, rt6_info_route, 0, m);
266533120b30SAlexey Dobriyan 	return 0;
26661da177e4SLinus Torvalds }
26671da177e4SLinus Torvalds 
266833120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
266933120b30SAlexey Dobriyan {
2670de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2671f3db4851SDaniel Lezcano }
2672f3db4851SDaniel Lezcano 
267333120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
267433120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
267533120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
267633120b30SAlexey Dobriyan 	.read		= seq_read,
267733120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2678b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
267933120b30SAlexey Dobriyan };
268033120b30SAlexey Dobriyan 
26811da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
26821da177e4SLinus Torvalds {
268369ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
26841da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
268569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
268669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
268769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
268869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
268969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2690fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
269169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
26921da177e4SLinus Torvalds 
26931da177e4SLinus Torvalds 	return 0;
26941da177e4SLinus Torvalds }
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
26971da177e4SLinus Torvalds {
2698de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
269969ddb805SDaniel Lezcano }
270069ddb805SDaniel Lezcano 
27019a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
27021da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
27031da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
27041da177e4SLinus Torvalds 	.read	 = seq_read,
27051da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2706b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
27071da177e4SLinus Torvalds };
27081da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
27111da177e4SLinus Torvalds 
27121da177e4SLinus Torvalds static
27138d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
27141da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
27151da177e4SLinus Torvalds {
2716c486da34SLucian Adrian Grijincu 	struct net *net;
2717c486da34SLucian Adrian Grijincu 	int delay;
2718c486da34SLucian Adrian Grijincu 	if (!write)
2719c486da34SLucian Adrian Grijincu 		return -EINVAL;
2720c486da34SLucian Adrian Grijincu 
2721c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2722c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
27238d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
27245b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
27251da177e4SLinus Torvalds 	return 0;
27261da177e4SLinus Torvalds }
27271da177e4SLinus Torvalds 
2728760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
27291da177e4SLinus Torvalds 	{
27301da177e4SLinus Torvalds 		.procname	=	"flush",
27314990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
27321da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
273389c8b3a1SDave Jones 		.mode		=	0200,
27346d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
27351da177e4SLinus Torvalds 	},
27361da177e4SLinus Torvalds 	{
27371da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
27389a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
27391da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27401da177e4SLinus Torvalds 		.mode		=	0644,
27416d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27421da177e4SLinus Torvalds 	},
27431da177e4SLinus Torvalds 	{
27441da177e4SLinus Torvalds 		.procname	=	"max_size",
27454990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
27461da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27471da177e4SLinus Torvalds 		.mode		=	0644,
27486d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27491da177e4SLinus Torvalds 	},
27501da177e4SLinus Torvalds 	{
27511da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
27524990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
27531da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27541da177e4SLinus Torvalds 		.mode		=	0644,
27556d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27561da177e4SLinus Torvalds 	},
27571da177e4SLinus Torvalds 	{
27581da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
27594990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
27601da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27611da177e4SLinus Torvalds 		.mode		=	0644,
27626d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27631da177e4SLinus Torvalds 	},
27641da177e4SLinus Torvalds 	{
27651da177e4SLinus Torvalds 		.procname	=	"gc_interval",
27664990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
27671da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27681da177e4SLinus Torvalds 		.mode		=	0644,
27696d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27701da177e4SLinus Torvalds 	},
27711da177e4SLinus Torvalds 	{
27721da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
27734990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
27741da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27751da177e4SLinus Torvalds 		.mode		=	0644,
2776f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
27771da177e4SLinus Torvalds 	},
27781da177e4SLinus Torvalds 	{
27791da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
27804990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
27811da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27821da177e4SLinus Torvalds 		.mode		=	0644,
27836d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27841da177e4SLinus Torvalds 	},
27851da177e4SLinus Torvalds 	{
27861da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
27874990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
27881da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27891da177e4SLinus Torvalds 		.mode		=	0644,
2790f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
27911da177e4SLinus Torvalds 	},
27921da177e4SLinus Torvalds 	{
27931da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
27944990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
27951da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27961da177e4SLinus Torvalds 		.mode		=	0644,
27976d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
27981da177e4SLinus Torvalds 	},
2799f8572d8fSEric W. Biederman 	{ }
28001da177e4SLinus Torvalds };
28011da177e4SLinus Torvalds 
28022c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2803760f2d01SDaniel Lezcano {
2804760f2d01SDaniel Lezcano 	struct ctl_table *table;
2805760f2d01SDaniel Lezcano 
2806760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2807760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2808760f2d01SDaniel Lezcano 			GFP_KERNEL);
28095ee09105SYOSHIFUJI Hideaki 
28105ee09105SYOSHIFUJI Hideaki 	if (table) {
28115ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2812c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
281386393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
28145ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
28155ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28165ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
28175ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
28185ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
28195ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
28205ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
28219c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28225ee09105SYOSHIFUJI Hideaki 	}
28235ee09105SYOSHIFUJI Hideaki 
2824760f2d01SDaniel Lezcano 	return table;
2825760f2d01SDaniel Lezcano }
28261da177e4SLinus Torvalds #endif
28271da177e4SLinus Torvalds 
28282c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2829cdb18761SDaniel Lezcano {
2830633d424bSPavel Emelyanov 	int ret = -ENOMEM;
28318ed67789SDaniel Lezcano 
283286393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
283386393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2834f2fc6a54SBenjamin Thery 
2835fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2836fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2837fc66f95cSEric Dumazet 
28388ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
28398ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
28408ed67789SDaniel Lezcano 					   GFP_KERNEL);
28418ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2842fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2843d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
28448ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2845d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
284662fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
284762fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28488ed67789SDaniel Lezcano 
28498ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
28508ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
28518ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
28528ed67789SDaniel Lezcano 					       GFP_KERNEL);
285368fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
285468fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2855d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
28568ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2857d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
285862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
285962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28608ed67789SDaniel Lezcano 
28618ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
28628ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
28638ed67789SDaniel Lezcano 					       GFP_KERNEL);
286468fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
286568fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2866d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
28678ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2868d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
286962fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
287062fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28718ed67789SDaniel Lezcano #endif
28728ed67789SDaniel Lezcano 
2873b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2874b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2875b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2876b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2877b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2878b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2879b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2880b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2881b339a47cSPeter Zijlstra 
2882cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2883cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2884cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2885cdb18761SDaniel Lezcano #endif
28866891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
28876891a346SBenjamin Thery 
28888ed67789SDaniel Lezcano 	ret = 0;
28898ed67789SDaniel Lezcano out:
28908ed67789SDaniel Lezcano 	return ret;
2891f2fc6a54SBenjamin Thery 
289268fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
289368fffc67SPeter Zijlstra out_ip6_prohibit_entry:
289468fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
289568fffc67SPeter Zijlstra out_ip6_null_entry:
289668fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
289768fffc67SPeter Zijlstra #endif
2898fc66f95cSEric Dumazet out_ip6_dst_entries:
2899fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2900f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2901f2fc6a54SBenjamin Thery 	goto out;
2902cdb18761SDaniel Lezcano }
2903cdb18761SDaniel Lezcano 
29042c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2905cdb18761SDaniel Lezcano {
2906cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2907cdb18761SDaniel Lezcano 	proc_net_remove(net, "ipv6_route");
2908cdb18761SDaniel Lezcano 	proc_net_remove(net, "rt6_stats");
2909cdb18761SDaniel Lezcano #endif
29108ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
29118ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29128ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
29138ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
29148ed67789SDaniel Lezcano #endif
291541bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2916cdb18761SDaniel Lezcano }
2917cdb18761SDaniel Lezcano 
2918cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
2919cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
2920cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
2921cdb18761SDaniel Lezcano };
2922cdb18761SDaniel Lezcano 
29238ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
29248ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
29258ed67789SDaniel Lezcano 	.priority = 0,
29268ed67789SDaniel Lezcano };
29278ed67789SDaniel Lezcano 
2928433d49c3SDaniel Lezcano int __init ip6_route_init(void)
29291da177e4SLinus Torvalds {
2930433d49c3SDaniel Lezcano 	int ret;
2931433d49c3SDaniel Lezcano 
29329a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
29339a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
29349a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
29359a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
29369a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
2937c19a28e1SFernando Carrijo 		goto out;
293814e50e57SDavid S. Miller 
2939fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
29408ed67789SDaniel Lezcano 	if (ret)
2941bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
2942bdb3289fSDaniel Lezcano 
2943fc66f95cSEric Dumazet 	ret = register_pernet_subsys(&ip6_route_net_ops);
2944fc66f95cSEric Dumazet 	if (ret)
2945fc66f95cSEric Dumazet 		goto out_dst_entries;
2946fc66f95cSEric Dumazet 
29475dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
29485dc121e9SArnaud Ebalard 
29498ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
29508ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
29518ed67789SDaniel Lezcano 	 * manually for init_net */
2952d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
29538ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2954bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2955d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
29568ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2957d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
29588ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2959bdb3289fSDaniel Lezcano   #endif
2960433d49c3SDaniel Lezcano 	ret = fib6_init();
2961433d49c3SDaniel Lezcano 	if (ret)
29628ed67789SDaniel Lezcano 		goto out_register_subsys;
2963433d49c3SDaniel Lezcano 
2964433d49c3SDaniel Lezcano 	ret = xfrm6_init();
2965433d49c3SDaniel Lezcano 	if (ret)
2966cdb18761SDaniel Lezcano 		goto out_fib6_init;
2967c35b7e72SDaniel Lezcano 
2968433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
2969433d49c3SDaniel Lezcano 	if (ret)
2970433d49c3SDaniel Lezcano 		goto xfrm6_init;
29717e5449c2SDaniel Lezcano 
2972433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
2973c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
2974c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
2975c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
2976433d49c3SDaniel Lezcano 		goto fib6_rules_init;
2977433d49c3SDaniel Lezcano 
29788ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
2979cdb18761SDaniel Lezcano 	if (ret)
2980cdb18761SDaniel Lezcano 		goto fib6_rules_init;
29818ed67789SDaniel Lezcano 
2982433d49c3SDaniel Lezcano out:
2983433d49c3SDaniel Lezcano 	return ret;
2984433d49c3SDaniel Lezcano 
2985433d49c3SDaniel Lezcano fib6_rules_init:
2986433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
2987433d49c3SDaniel Lezcano xfrm6_init:
2988433d49c3SDaniel Lezcano 	xfrm6_fini();
2989433d49c3SDaniel Lezcano out_fib6_init:
2990433d49c3SDaniel Lezcano 	fib6_gc_cleanup();
29918ed67789SDaniel Lezcano out_register_subsys:
29928ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
2993fc66f95cSEric Dumazet out_dst_entries:
2994fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
2995433d49c3SDaniel Lezcano out_kmem_cache:
2996f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
2997433d49c3SDaniel Lezcano 	goto out;
29981da177e4SLinus Torvalds }
29991da177e4SLinus Torvalds 
30001da177e4SLinus Torvalds void ip6_route_cleanup(void)
30011da177e4SLinus Torvalds {
30028ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3003101367c2SThomas Graf 	fib6_rules_cleanup();
30041da177e4SLinus Torvalds 	xfrm6_fini();
30051da177e4SLinus Torvalds 	fib6_gc_cleanup();
30068ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
300741bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3008f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
30091da177e4SLinus Torvalds }
3010