xref: /openbmc/linux/net/ipv6/route.c (revision fbe58186901155c0cb5398dd343337be0c456c04)
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>
291da177e4SLinus Torvalds #include <linux/types.h>
301da177e4SLinus Torvalds #include <linux/times.h>
311da177e4SLinus Torvalds #include <linux/socket.h>
321da177e4SLinus Torvalds #include <linux/sockios.h>
331da177e4SLinus Torvalds #include <linux/net.h>
341da177e4SLinus Torvalds #include <linux/route.h>
351da177e4SLinus Torvalds #include <linux/netdevice.h>
361da177e4SLinus Torvalds #include <linux/in6.h>
377bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
381da177e4SLinus Torvalds #include <linux/init.h>
391da177e4SLinus Torvalds #include <linux/if_arp.h>
401da177e4SLinus Torvalds #include <linux/proc_fs.h>
411da177e4SLinus Torvalds #include <linux/seq_file.h>
425b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
435a0e3ad6STejun Heo #include <linux/slab.h>
44457c4cbcSEric W. Biederman #include <net/net_namespace.h>
451da177e4SLinus Torvalds #include <net/snmp.h>
461da177e4SLinus Torvalds #include <net/ipv6.h>
471da177e4SLinus Torvalds #include <net/ip6_fib.h>
481da177e4SLinus Torvalds #include <net/ip6_route.h>
491da177e4SLinus Torvalds #include <net/ndisc.h>
501da177e4SLinus Torvalds #include <net/addrconf.h>
511da177e4SLinus Torvalds #include <net/tcp.h>
521da177e4SLinus Torvalds #include <linux/rtnetlink.h>
531da177e4SLinus Torvalds #include <net/dst.h>
541da177e4SLinus Torvalds #include <net/xfrm.h>
558d71740cSTom Tucker #include <net/netevent.h>
5621713ebcSThomas Graf #include <net/netlink.h>
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds #include <asm/uaccess.h>
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
611da177e4SLinus Torvalds #include <linux/sysctl.h>
621da177e4SLinus Torvalds #endif
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /* Set to 3 to get tracing. */
651da177e4SLinus Torvalds #define RT6_DEBUG 2
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #if RT6_DEBUG >= 3
681da177e4SLinus Torvalds #define RDBG(x) printk x
691da177e4SLinus Torvalds #define RT6_TRACE(x...) printk(KERN_DEBUG x)
701da177e4SLinus Torvalds #else
711da177e4SLinus Torvalds #define RDBG(x)
721da177e4SLinus Torvalds #define RT6_TRACE(x...) do { ; } while (0)
731da177e4SLinus Torvalds #endif
741da177e4SLinus Torvalds 
7521efcfa0SEric Dumazet static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort,
7621efcfa0SEric Dumazet 				    const struct in6_addr *dest);
771da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
780dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
79d33e4553SDavid S. Miller static unsigned int	 ip6_default_mtu(const struct dst_entry *dst);
801da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
811da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
821da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
831da177e4SLinus Torvalds 				       struct net_device *dev, int how);
84569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
871da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
881da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
891da177e4SLinus Torvalds static void		ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
901da177e4SLinus Torvalds 
9170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
92efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
93b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
94b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
9570ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref);
96efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
97b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
98b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
9970ceb4f5SYOSHIFUJI Hideaki #endif
10070ceb4f5SYOSHIFUJI Hideaki 
10106582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
10206582540SDavid S. Miller {
10306582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
10406582540SDavid S. Miller 	struct inet_peer *peer;
10506582540SDavid S. Miller 	u32 *p = NULL;
10606582540SDavid S. Miller 
1078e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1088e2ec639SYan, Zheng 		return NULL;
1098e2ec639SYan, Zheng 
11006582540SDavid S. Miller 	if (!rt->rt6i_peer)
11106582540SDavid S. Miller 		rt6_bind_peer(rt, 1);
11206582540SDavid S. Miller 
11306582540SDavid S. Miller 	peer = rt->rt6i_peer;
11406582540SDavid S. Miller 	if (peer) {
11506582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
11606582540SDavid S. Miller 		unsigned long prev, new;
11706582540SDavid S. Miller 
11806582540SDavid S. Miller 		p = peer->metrics;
11906582540SDavid S. Miller 		if (inet_metrics_new(peer))
12006582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
12106582540SDavid S. Miller 
12206582540SDavid S. Miller 		new = (unsigned long) p;
12306582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
12406582540SDavid S. Miller 
12506582540SDavid S. Miller 		if (prev != old) {
12606582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
12706582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
12806582540SDavid S. Miller 				p = NULL;
12906582540SDavid S. Miller 		}
13006582540SDavid S. Miller 	}
13106582540SDavid S. Miller 	return p;
13206582540SDavid S. Miller }
13306582540SDavid S. Miller 
134d3aaeb38SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, const void *daddr)
135d3aaeb38SDavid S. Miller {
136d3aaeb38SDavid S. Miller 	return __neigh_lookup_errno(&nd_tbl, daddr, dst->dev);
137d3aaeb38SDavid S. Miller }
138d3aaeb38SDavid S. Miller 
1399a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1401da177e4SLinus Torvalds 	.family			=	AF_INET6,
14109640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1421da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1431da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1441da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1450dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
146d33e4553SDavid S. Miller 	.default_mtu		=	ip6_default_mtu,
14706582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1481da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1491da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1501da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1511da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1521da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1531ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
154d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1551da177e4SLinus Torvalds };
1561da177e4SLinus Torvalds 
157ec831ea7SRoland Dreier static unsigned int ip6_blackhole_default_mtu(const struct dst_entry *dst)
158ec831ea7SRoland Dreier {
159ec831ea7SRoland Dreier 	return 0;
160ec831ea7SRoland Dreier }
161ec831ea7SRoland Dreier 
16214e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
16314e50e57SDavid S. Miller {
16414e50e57SDavid S. Miller }
16514e50e57SDavid S. Miller 
1660972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1670972ddb2SHeld Bernhard 					 unsigned long old)
1680972ddb2SHeld Bernhard {
1690972ddb2SHeld Bernhard 	return NULL;
1700972ddb2SHeld Bernhard }
1710972ddb2SHeld Bernhard 
17214e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
17314e50e57SDavid S. Miller 	.family			=	AF_INET6,
17409640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
17514e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
17614e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
177ec831ea7SRoland Dreier 	.default_mtu		=	ip6_blackhole_default_mtu,
178214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
17914e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
1800972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
181d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
18214e50e57SDavid S. Miller };
18314e50e57SDavid S. Miller 
18462fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
18562fa8a84SDavid S. Miller 	[RTAX_HOPLIMIT - 1] = 255,
18662fa8a84SDavid S. Miller };
18762fa8a84SDavid S. Miller 
188bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = {
1891da177e4SLinus Torvalds 	.dst = {
1901da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
1911da177e4SLinus Torvalds 		.__use		= 1,
1921da177e4SLinus Torvalds 		.obsolete	= -1,
1931da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
1941da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
1951da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
1961da177e4SLinus Torvalds 	},
1971da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
1984f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
1991da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2001da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2011da177e4SLinus Torvalds };
2021da177e4SLinus Torvalds 
203101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
204101367c2SThomas Graf 
2056723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
2066723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
2076723ab54SDavid S. Miller 
208280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = {
209101367c2SThomas Graf 	.dst = {
210101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
211101367c2SThomas Graf 		.__use		= 1,
212101367c2SThomas Graf 		.obsolete	= -1,
213101367c2SThomas Graf 		.error		= -EACCES,
2149ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2159ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
216101367c2SThomas Graf 	},
217101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2184f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
219101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
220101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
221101367c2SThomas Graf };
222101367c2SThomas Graf 
223bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = {
224101367c2SThomas Graf 	.dst = {
225101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
226101367c2SThomas Graf 		.__use		= 1,
227101367c2SThomas Graf 		.obsolete	= -1,
228101367c2SThomas Graf 		.error		= -EINVAL,
229352e512cSHerbert Xu 		.input		= dst_discard,
230352e512cSHerbert Xu 		.output		= dst_discard,
231101367c2SThomas Graf 	},
232101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2334f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
234101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
235101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
236101367c2SThomas Graf };
237101367c2SThomas Graf 
238101367c2SThomas Graf #endif
239101367c2SThomas Graf 
2401da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
2415c1e6aa3SDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops,
242957c665fSDavid S. Miller 					     struct net_device *dev,
243957c665fSDavid S. Miller 					     int flags)
2441da177e4SLinus Torvalds {
245957c665fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, flags);
246cf911662SDavid S. Miller 
247*fbe58186SMadalin Bucur 	if (rt != NULL)
248*fbe58186SMadalin Bucur 		memset(&rt->rt6i_table, 0,
249*fbe58186SMadalin Bucur 			sizeof(*rt) - sizeof(struct dst_entry));
250cf911662SDavid S. Miller 
251cf911662SDavid S. Miller 	return rt;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2571da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
258b3419363SDavid S. Miller 	struct inet_peer *peer = rt->rt6i_peer;
2591da177e4SLinus Torvalds 
2608e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
2618e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
2628e2ec639SYan, Zheng 
2631da177e4SLinus Torvalds 	if (idev != NULL) {
2641da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2651da177e4SLinus Torvalds 		in6_dev_put(idev);
2661da177e4SLinus Torvalds 	}
267b3419363SDavid S. Miller 	if (peer) {
268b3419363SDavid S. Miller 		rt->rt6i_peer = NULL;
269b3419363SDavid S. Miller 		inet_putpeer(peer);
270b3419363SDavid S. Miller 	}
271b3419363SDavid S. Miller }
272b3419363SDavid S. Miller 
2736431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
2746431cbc2SDavid S. Miller 
2756431cbc2SDavid S. Miller static u32 rt6_peer_genid(void)
2766431cbc2SDavid S. Miller {
2776431cbc2SDavid S. Miller 	return atomic_read(&__rt6_peer_genid);
2786431cbc2SDavid S. Miller }
2796431cbc2SDavid S. Miller 
280b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
281b3419363SDavid S. Miller {
282b3419363SDavid S. Miller 	struct inet_peer *peer;
283b3419363SDavid S. Miller 
284b3419363SDavid S. Miller 	peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
285b3419363SDavid S. Miller 	if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
286b3419363SDavid S. Miller 		inet_putpeer(peer);
2876431cbc2SDavid S. Miller 	else
2886431cbc2SDavid S. Miller 		rt->rt6i_peer_genid = rt6_peer_genid();
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
2921da177e4SLinus Torvalds 			   int how)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2951da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2965a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
297c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
2981da177e4SLinus Torvalds 
2995a3e55d6SDenis V. Lunev 	if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
3005a3e55d6SDenis V. Lunev 		struct inet6_dev *loopback_idev =
3015a3e55d6SDenis V. Lunev 			in6_dev_get(loopback_dev);
3021da177e4SLinus Torvalds 		if (loopback_idev != NULL) {
3031da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
3041da177e4SLinus Torvalds 			in6_dev_put(idev);
3051da177e4SLinus Torvalds 		}
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds static __inline__ int rt6_check_expired(const struct rt6_info *rt)
3101da177e4SLinus Torvalds {
311a02cec21SEric Dumazet 	return (rt->rt6i_flags & RTF_EXPIRES) &&
312a02cec21SEric Dumazet 		time_after(jiffies, rt->rt6i_expires);
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
315b71d1d42SEric Dumazet static inline int rt6_need_strict(const struct in6_addr *daddr)
316c71099acSThomas Graf {
317a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
318a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
319c71099acSThomas Graf }
320c71099acSThomas Graf 
3211da177e4SLinus Torvalds /*
322c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
3231da177e4SLinus Torvalds  */
3241da177e4SLinus Torvalds 
3258ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
3268ed67789SDaniel Lezcano 						    struct rt6_info *rt,
327b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
3281da177e4SLinus Torvalds 						    int oif,
329d420895eSYOSHIFUJI Hideaki 						    int flags)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
3321da177e4SLinus Torvalds 	struct rt6_info *sprt;
3331da177e4SLinus Torvalds 
334dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
335dd3abc4eSYOSHIFUJI Hideaki 		goto out;
336dd3abc4eSYOSHIFUJI Hideaki 
337d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
3381da177e4SLinus Torvalds 		struct net_device *dev = sprt->rt6i_dev;
339dd3abc4eSYOSHIFUJI Hideaki 
340dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
3411da177e4SLinus Torvalds 			if (dev->ifindex == oif)
3421da177e4SLinus Torvalds 				return sprt;
3431da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
3441da177e4SLinus Torvalds 				if (sprt->rt6i_idev == NULL ||
3451da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
346d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
3471da177e4SLinus Torvalds 						continue;
3481da177e4SLinus Torvalds 					if (local && (!oif ||
3491da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
3501da177e4SLinus Torvalds 						continue;
3511da177e4SLinus Torvalds 				}
3521da177e4SLinus Torvalds 				local = sprt;
3531da177e4SLinus Torvalds 			}
354dd3abc4eSYOSHIFUJI Hideaki 		} else {
355dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
356dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
357dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
358dd3abc4eSYOSHIFUJI Hideaki 		}
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 
361dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
3621da177e4SLinus Torvalds 		if (local)
3631da177e4SLinus Torvalds 			return local;
3641da177e4SLinus Torvalds 
365d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
3668ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
3671da177e4SLinus Torvalds 	}
368dd3abc4eSYOSHIFUJI Hideaki out:
3691da177e4SLinus Torvalds 	return rt;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
37227097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
37327097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
37427097255SYOSHIFUJI Hideaki {
375f2c31e32SEric Dumazet 	struct neighbour *neigh;
37627097255SYOSHIFUJI Hideaki 	/*
37727097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
37827097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
37927097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
38027097255SYOSHIFUJI Hideaki 	 *
38127097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
38227097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
38327097255SYOSHIFUJI Hideaki 	 */
384f2c31e32SEric Dumazet 	rcu_read_lock();
385f2c31e32SEric Dumazet 	neigh = rt ? dst_get_neighbour(&rt->dst) : NULL;
38627097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
387f2c31e32SEric Dumazet 		goto out;
38827097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
38927097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
39052e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
39127097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
39227097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
39327097255SYOSHIFUJI Hideaki 
39427097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
39527097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
39627097255SYOSHIFUJI Hideaki 
39727097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
39827097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
39927097255SYOSHIFUJI Hideaki 		ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
400f2c31e32SEric Dumazet 	} else {
40127097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
40227097255SYOSHIFUJI Hideaki 	}
403f2c31e32SEric Dumazet out:
404f2c31e32SEric Dumazet 	rcu_read_unlock();
405f2c31e32SEric Dumazet }
40627097255SYOSHIFUJI Hideaki #else
40727097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
40827097255SYOSHIFUJI Hideaki {
40927097255SYOSHIFUJI Hideaki }
41027097255SYOSHIFUJI Hideaki #endif
41127097255SYOSHIFUJI Hideaki 
4121da177e4SLinus Torvalds /*
413554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
4141da177e4SLinus Torvalds  */
415b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
4161da177e4SLinus Torvalds {
417554cfb7eSYOSHIFUJI Hideaki 	struct net_device *dev = rt->rt6i_dev;
418161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
419554cfb7eSYOSHIFUJI Hideaki 		return 2;
420161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
421161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
422161980f4SDavid S. Miller 		return 1;
423554cfb7eSYOSHIFUJI Hideaki 	return 0;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
426b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
4271da177e4SLinus Torvalds {
428f2c31e32SEric Dumazet 	struct neighbour *neigh;
429398bcbebSYOSHIFUJI Hideaki 	int m;
430f2c31e32SEric Dumazet 
431f2c31e32SEric Dumazet 	rcu_read_lock();
432f2c31e32SEric Dumazet 	neigh = dst_get_neighbour(&rt->dst);
4334d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
4344d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
4354d0c5911SYOSHIFUJI Hideaki 		m = 1;
4364d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
4371da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
438554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
4394d0c5911SYOSHIFUJI Hideaki 			m = 2;
440398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
441398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
442398bcbebSYOSHIFUJI Hideaki 			m = 0;
443398bcbebSYOSHIFUJI Hideaki #endif
444398bcbebSYOSHIFUJI Hideaki 		else
445ea73ee23SYOSHIFUJI Hideaki 			m = 1;
4461da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
447398bcbebSYOSHIFUJI Hideaki 	} else
448398bcbebSYOSHIFUJI Hideaki 		m = 0;
449f2c31e32SEric Dumazet 	rcu_read_unlock();
450554cfb7eSYOSHIFUJI Hideaki 	return m;
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
453554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
454554cfb7eSYOSHIFUJI Hideaki 			   int strict)
455554cfb7eSYOSHIFUJI Hideaki {
4564d0c5911SYOSHIFUJI Hideaki 	int m, n;
4574d0c5911SYOSHIFUJI Hideaki 
4584d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
45977d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
460554cfb7eSYOSHIFUJI Hideaki 		return -1;
461ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
462ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
463ebacaaa0SYOSHIFUJI Hideaki #endif
4644d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
465557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
466554cfb7eSYOSHIFUJI Hideaki 		return -1;
467554cfb7eSYOSHIFUJI Hideaki 	return m;
468554cfb7eSYOSHIFUJI Hideaki }
469554cfb7eSYOSHIFUJI Hideaki 
470f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
471f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
472554cfb7eSYOSHIFUJI Hideaki {
473554cfb7eSYOSHIFUJI Hideaki 	int m;
474554cfb7eSYOSHIFUJI Hideaki 
475554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
476f11e6659SDavid S. Miller 		goto out;
477554cfb7eSYOSHIFUJI Hideaki 
478554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
479554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
480f11e6659SDavid S. Miller 		goto out;
481554cfb7eSYOSHIFUJI Hideaki 
482f11e6659SDavid S. Miller 	if (m > *mpri) {
483ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
48427097255SYOSHIFUJI Hideaki 			rt6_probe(match);
485f11e6659SDavid S. Miller 		*mpri = m;
486554cfb7eSYOSHIFUJI Hideaki 		match = rt;
487ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
48827097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
4891da177e4SLinus Torvalds 	}
490f11e6659SDavid S. Miller 
491f11e6659SDavid S. Miller out:
492f11e6659SDavid S. Miller 	return match;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
495f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
496f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
497f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
498f11e6659SDavid S. Miller {
499f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
500f11e6659SDavid S. Miller 	int mpri = -1;
501f11e6659SDavid S. Miller 
502f11e6659SDavid S. Miller 	match = NULL;
503f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
504d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
505f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
506f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
507d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
508f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
509f11e6659SDavid S. Miller 
510f11e6659SDavid S. Miller 	return match;
511f11e6659SDavid S. Miller }
512f11e6659SDavid S. Miller 
513f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
514f11e6659SDavid S. Miller {
515f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
5168ed67789SDaniel Lezcano 	struct net *net;
517f11e6659SDavid S. Miller 
518f11e6659SDavid S. Miller 	RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
5190dc47877SHarvey Harrison 		  __func__, fn->leaf, oif);
520f11e6659SDavid S. Miller 
521f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
522f11e6659SDavid S. Miller 	if (!rt0)
523f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
524f11e6659SDavid S. Miller 
525f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
526f11e6659SDavid S. Miller 
527554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
528f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
529d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
530f11e6659SDavid S. Miller 
531554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
532f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
533f11e6659SDavid S. Miller 			next = fn->leaf;
534f11e6659SDavid S. Miller 
535f11e6659SDavid S. Miller 		if (next != rt0)
536f11e6659SDavid S. Miller 			fn->rr_ptr = next;
537554cfb7eSYOSHIFUJI Hideaki 	}
538554cfb7eSYOSHIFUJI Hideaki 
539f11e6659SDavid S. Miller 	RT6_TRACE("%s() => %p\n",
5400dc47877SHarvey Harrison 		  __func__, match);
541554cfb7eSYOSHIFUJI Hideaki 
542c346dca1SYOSHIFUJI Hideaki 	net = dev_net(rt0->rt6i_dev);
543a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds 
54670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
54770ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
548b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
54970ceb4f5SYOSHIFUJI Hideaki {
550c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
55170ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
55270ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
55370ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
5544bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
55570ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
55670ceb4f5SYOSHIFUJI Hideaki 
55770ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
55870ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
55970ceb4f5SYOSHIFUJI Hideaki 	}
56070ceb4f5SYOSHIFUJI Hideaki 
56170ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
56270ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
56370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
56470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
56570ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
56670ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
56770ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
56870ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
56970ceb4f5SYOSHIFUJI Hideaki 		}
57070ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
57170ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
57270ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
57370ceb4f5SYOSHIFUJI Hideaki 		}
57470ceb4f5SYOSHIFUJI Hideaki 	}
57570ceb4f5SYOSHIFUJI Hideaki 
57670ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
57770ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
5783933fc95SJens Rosenboom 		return -EINVAL;
57970ceb4f5SYOSHIFUJI Hideaki 
5804bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
58170ceb4f5SYOSHIFUJI Hideaki 
58270ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
58370ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
58470ceb4f5SYOSHIFUJI Hideaki 	else {
58570ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
58670ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
58770ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
58870ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
58970ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
59070ceb4f5SYOSHIFUJI Hideaki 	}
59170ceb4f5SYOSHIFUJI Hideaki 
592efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
593efa2cea0SDaniel Lezcano 				dev->ifindex);
59470ceb4f5SYOSHIFUJI Hideaki 
59570ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
596e0a1ad73SThomas Graf 		ip6_del_rt(rt);
59770ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
59870ceb4f5SYOSHIFUJI Hideaki 	}
59970ceb4f5SYOSHIFUJI Hideaki 
60070ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
601efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
60270ceb4f5SYOSHIFUJI Hideaki 					pref);
60370ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
60470ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
60570ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
60670ceb4f5SYOSHIFUJI Hideaki 
60770ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
6084bed72e4SYOSHIFUJI Hideaki 		if (!addrconf_finite_timeout(lifetime)) {
60970ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags &= ~RTF_EXPIRES;
61070ceb4f5SYOSHIFUJI Hideaki 		} else {
61170ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_expires = jiffies + HZ * lifetime;
61270ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_EXPIRES;
61370ceb4f5SYOSHIFUJI Hideaki 		}
614d8d1f30bSChangli Gao 		dst_release(&rt->dst);
61570ceb4f5SYOSHIFUJI Hideaki 	}
61670ceb4f5SYOSHIFUJI Hideaki 	return 0;
61770ceb4f5SYOSHIFUJI Hideaki }
61870ceb4f5SYOSHIFUJI Hideaki #endif
61970ceb4f5SYOSHIFUJI Hideaki 
6208ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
621982f56f3SYOSHIFUJI Hideaki do { \
6228ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
623982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
624e0eda7bbSVille Nuorvala 		while (1) { \
625982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
626c71099acSThomas Graf 				goto out; \
627982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
628982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
6298bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
630982f56f3SYOSHIFUJI Hideaki 			else \
631982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
632c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
633c71099acSThomas Graf 				goto restart; \
634c71099acSThomas Graf 		} \
635982f56f3SYOSHIFUJI Hideaki 	} \
636982f56f3SYOSHIFUJI Hideaki } while(0)
637c71099acSThomas Graf 
6388ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
6398ed67789SDaniel Lezcano 					     struct fib6_table *table,
6404c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
6411da177e4SLinus Torvalds {
6421da177e4SLinus Torvalds 	struct fib6_node *fn;
6431da177e4SLinus Torvalds 	struct rt6_info *rt;
6441da177e4SLinus Torvalds 
645c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
6464c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
647c71099acSThomas Graf restart:
648c71099acSThomas Graf 	rt = fn->leaf;
6494c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
6504c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
651c71099acSThomas Graf out:
652d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
653c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
6541da177e4SLinus Torvalds 	return rt;
655c71099acSThomas Graf 
656c71099acSThomas Graf }
657c71099acSThomas Graf 
6589acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
6599acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
660c71099acSThomas Graf {
6614c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
6624c9483b2SDavid S. Miller 		.flowi6_oif = oif,
6634c9483b2SDavid S. Miller 		.daddr = *daddr,
664c71099acSThomas Graf 	};
665c71099acSThomas Graf 	struct dst_entry *dst;
66677d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
667c71099acSThomas Graf 
668adaa70bbSThomas Graf 	if (saddr) {
6694c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
670adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
671adaa70bbSThomas Graf 	}
672adaa70bbSThomas Graf 
6734c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
674c71099acSThomas Graf 	if (dst->error == 0)
675c71099acSThomas Graf 		return (struct rt6_info *) dst;
676c71099acSThomas Graf 
677c71099acSThomas Graf 	dst_release(dst);
678c71099acSThomas Graf 
6791da177e4SLinus Torvalds 	return NULL;
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
6827159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
6837159039aSYOSHIFUJI Hideaki 
684c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
6851da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
6861da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
6871da177e4SLinus Torvalds    be destroyed.
6881da177e4SLinus Torvalds  */
6891da177e4SLinus Torvalds 
69086872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
6911da177e4SLinus Torvalds {
6921da177e4SLinus Torvalds 	int err;
693c71099acSThomas Graf 	struct fib6_table *table;
6941da177e4SLinus Torvalds 
695c71099acSThomas Graf 	table = rt->rt6i_table;
696c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
69786872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
698c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	return err;
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
70340e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
70440e22e8fSThomas Graf {
7054d1169c1SDenis V. Lunev 	struct nl_info info = {
706c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
7074d1169c1SDenis V. Lunev 	};
708528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
70940e22e8fSThomas Graf }
71040e22e8fSThomas Graf 
71121efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_cow(const struct rt6_info *ort,
71221efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
713b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	struct rt6_info *rt;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	/*
7181da177e4SLinus Torvalds 	 *	Clone the route.
7191da177e4SLinus Torvalds 	 */
7201da177e4SLinus Torvalds 
72121efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	if (rt) {
72414deae41SDavid S. Miller 		struct neighbour *neigh;
72514deae41SDavid S. Miller 		int attempts = !in_softirq();
72614deae41SDavid S. Miller 
72758c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags&RTF_GATEWAY)) {
72858c4fb86SYOSHIFUJI Hideaki 			if (rt->rt6i_dst.plen != 128 &&
72921efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
73058c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
7311da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_gateway, daddr);
73258c4fb86SYOSHIFUJI Hideaki 		}
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
7371da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
7381da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
7391da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
7401da177e4SLinus Torvalds 		}
7411da177e4SLinus Torvalds #endif
7421da177e4SLinus Torvalds 
74314deae41SDavid S. Miller 	retry:
74414deae41SDavid S. Miller 		neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
74514deae41SDavid S. Miller 		if (IS_ERR(neigh)) {
74614deae41SDavid S. Miller 			struct net *net = dev_net(rt->rt6i_dev);
74714deae41SDavid S. Miller 			int saved_rt_min_interval =
74814deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval;
74914deae41SDavid S. Miller 			int saved_rt_elasticity =
75014deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity;
75114deae41SDavid S. Miller 
75214deae41SDavid S. Miller 			if (attempts-- > 0) {
75314deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
75414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
75514deae41SDavid S. Miller 
75686393e52SAlexey Dobriyan 				ip6_dst_gc(&net->ipv6.ip6_dst_ops);
75714deae41SDavid S. Miller 
75814deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity =
75914deae41SDavid S. Miller 					saved_rt_elasticity;
76014deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval =
76114deae41SDavid S. Miller 					saved_rt_min_interval;
76214deae41SDavid S. Miller 				goto retry;
76314deae41SDavid S. Miller 			}
76414deae41SDavid S. Miller 
76514deae41SDavid S. Miller 			if (net_ratelimit())
76614deae41SDavid S. Miller 				printk(KERN_WARNING
7677e1b33e5SUlrich Weber 				       "ipv6: Neighbour table overflow.\n");
768d8d1f30bSChangli Gao 			dst_free(&rt->dst);
76914deae41SDavid S. Miller 			return NULL;
77014deae41SDavid S. Miller 		}
77169cce1d1SDavid S. Miller 		dst_set_neighbour(&rt->dst, neigh);
7721da177e4SLinus Torvalds 
77395a9a5baSYOSHIFUJI Hideaki 	}
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	return rt;
7761da177e4SLinus Torvalds }
77795a9a5baSYOSHIFUJI Hideaki 
77821efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
77921efcfa0SEric Dumazet 					const struct in6_addr *daddr)
780299d9939SYOSHIFUJI Hideaki {
78121efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
78221efcfa0SEric Dumazet 
783299d9939SYOSHIFUJI Hideaki 	if (rt) {
784299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
785f2c31e32SEric Dumazet 		dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_raw(&ort->dst)));
786299d9939SYOSHIFUJI Hideaki 	}
787299d9939SYOSHIFUJI Hideaki 	return rt;
788299d9939SYOSHIFUJI Hideaki }
789299d9939SYOSHIFUJI Hideaki 
7908ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
7914c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
7921da177e4SLinus Torvalds {
7931da177e4SLinus Torvalds 	struct fib6_node *fn;
794519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
795c71099acSThomas Graf 	int strict = 0;
7961da177e4SLinus Torvalds 	int attempts = 3;
797519fbd87SYOSHIFUJI Hideaki 	int err;
79853b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
7991da177e4SLinus Torvalds 
80077d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds relookup:
803c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8041da177e4SLinus Torvalds 
8058238dd06SYOSHIFUJI Hideaki restart_2:
8064c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds restart:
8094acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
8108ed67789SDaniel Lezcano 
8114c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
8128ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
8138238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
8141da177e4SLinus Torvalds 		goto out;
8151da177e4SLinus Torvalds 
816d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
817c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8181da177e4SLinus Torvalds 
819f2c31e32SEric Dumazet 	if (!dst_get_neighbour_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
8204c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
8217343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
8224c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
8237343ff31SDavid S. Miller 	else
8247343ff31SDavid S. Miller 		goto out2;
8251da177e4SLinus Torvalds 
826d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8278ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
8281da177e4SLinus Torvalds 
829d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
830e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
83140e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
832e40cf353SYOSHIFUJI Hideaki 		if (!err)
833e40cf353SYOSHIFUJI Hideaki 			goto out2;
834e40cf353SYOSHIFUJI Hideaki 	}
835e40cf353SYOSHIFUJI Hideaki 
836e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
8371da177e4SLinus Torvalds 		goto out2;
8381da177e4SLinus Torvalds 
839519fbd87SYOSHIFUJI Hideaki 	/*
840c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
841519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
8421da177e4SLinus Torvalds 	 */
843d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8441da177e4SLinus Torvalds 	goto relookup;
845e40cf353SYOSHIFUJI Hideaki 
846519fbd87SYOSHIFUJI Hideaki out:
8478238dd06SYOSHIFUJI Hideaki 	if (reachable) {
8488238dd06SYOSHIFUJI Hideaki 		reachable = 0;
8498238dd06SYOSHIFUJI Hideaki 		goto restart_2;
8508238dd06SYOSHIFUJI Hideaki 	}
851d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
852c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8531da177e4SLinus Torvalds out2:
854d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
855d8d1f30bSChangli Gao 	rt->dst.__use++;
856c71099acSThomas Graf 
857c71099acSThomas Graf 	return rt;
858c71099acSThomas Graf }
859c71099acSThomas Graf 
8608ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
8614c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
8624acad72dSPavel Emelyanov {
8634c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
8644acad72dSPavel Emelyanov }
8654acad72dSPavel Emelyanov 
866c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
867c71099acSThomas Graf {
868b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
869c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
870adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
8714c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
8724c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
8734c9483b2SDavid S. Miller 		.daddr = iph->daddr,
8744c9483b2SDavid S. Miller 		.saddr = iph->saddr,
8754c9483b2SDavid S. Miller 		.flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
8764c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
8774c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
878c71099acSThomas Graf 	};
879adaa70bbSThomas Graf 
8801d6e55f1SThomas Goff 	if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
881adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
882c71099acSThomas Graf 
8834c9483b2SDavid S. Miller 	skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input));
884c71099acSThomas Graf }
885c71099acSThomas Graf 
8868ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
8874c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
888c71099acSThomas Graf {
8894c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
890c71099acSThomas Graf }
891c71099acSThomas Graf 
8929c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
8934c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
894c71099acSThomas Graf {
895c71099acSThomas Graf 	int flags = 0;
896c71099acSThomas Graf 
8974c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
89877d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
899c71099acSThomas Graf 
9004c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
901adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
9020c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
9030c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
904adaa70bbSThomas Graf 
9054c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
9087159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
9091da177e4SLinus Torvalds 
9102774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
91114e50e57SDavid S. Miller {
9125c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
91314e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
91414e50e57SDavid S. Miller 
9155c1e6aa3SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
91614e50e57SDavid S. Miller 	if (rt) {
917cf911662SDavid S. Miller 		memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
918cf911662SDavid S. Miller 
919d8d1f30bSChangli Gao 		new = &rt->dst;
92014e50e57SDavid S. Miller 
92114e50e57SDavid S. Miller 		new->__use = 1;
922352e512cSHerbert Xu 		new->input = dst_discard;
923352e512cSHerbert Xu 		new->output = dst_discard;
92414e50e57SDavid S. Miller 
92521efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
92621efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
92721efcfa0SEric Dumazet 		else
928defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
92914e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
93014e50e57SDavid S. Miller 		if (rt->rt6i_idev)
93114e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
93214e50e57SDavid S. Miller 		rt->rt6i_expires = 0;
93314e50e57SDavid S. Miller 
93414e50e57SDavid S. Miller 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
93514e50e57SDavid S. Miller 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
93614e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
93714e50e57SDavid S. Miller 
93814e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
93914e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
94014e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
94114e50e57SDavid S. Miller #endif
94214e50e57SDavid S. Miller 
94314e50e57SDavid S. Miller 		dst_free(new);
94414e50e57SDavid S. Miller 	}
94514e50e57SDavid S. Miller 
94669ead7afSDavid S. Miller 	dst_release(dst_orig);
94769ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
94814e50e57SDavid S. Miller }
94914e50e57SDavid S. Miller 
9501da177e4SLinus Torvalds /*
9511da177e4SLinus Torvalds  *	Destination cache support functions
9521da177e4SLinus Torvalds  */
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
9551da177e4SLinus Torvalds {
9561da177e4SLinus Torvalds 	struct rt6_info *rt;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
9591da177e4SLinus Torvalds 
9606431cbc2SDavid S. Miller 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
9616431cbc2SDavid S. Miller 		if (rt->rt6i_peer_genid != rt6_peer_genid()) {
9626431cbc2SDavid S. Miller 			if (!rt->rt6i_peer)
9636431cbc2SDavid S. Miller 				rt6_bind_peer(rt, 0);
9646431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
9656431cbc2SDavid S. Miller 		}
9661da177e4SLinus Torvalds 		return dst;
9676431cbc2SDavid S. Miller 	}
9681da177e4SLinus Torvalds 	return NULL;
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
9721da177e4SLinus Torvalds {
9731da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 	if (rt) {
97654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
97754c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
978e0a1ad73SThomas Graf 				ip6_del_rt(rt);
97954c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
9801da177e4SLinus Torvalds 			}
98154c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
98254c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
98354c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
98454c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
98554c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
98654c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
9871da177e4SLinus Torvalds }
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
9901da177e4SLinus Torvalds {
9911da177e4SLinus Torvalds 	struct rt6_info *rt;
9921da177e4SLinus Torvalds 
9933ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
9941da177e4SLinus Torvalds 
995adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
9961da177e4SLinus Torvalds 	if (rt) {
9971da177e4SLinus Torvalds 		if (rt->rt6i_flags&RTF_CACHE) {
998d8d1f30bSChangli Gao 			dst_set_expires(&rt->dst, 0);
9991da177e4SLinus Torvalds 			rt->rt6i_flags |= RTF_EXPIRES;
10001da177e4SLinus Torvalds 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
10011da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
10061da177e4SLinus Torvalds {
10071da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
10101da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
10111da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1012defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
10131da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1014defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1015defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
10161da177e4SLinus Torvalds 		}
1017defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
10181da177e4SLinus Torvalds 	}
10191da177e4SLinus Torvalds }
10201da177e4SLinus Torvalds 
10210dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
10221da177e4SLinus Torvalds {
10230dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
10240dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
10250dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
10260dbaee3bSDavid S. Miller 
10271da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
10281da177e4SLinus Torvalds 
10295578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
10305578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	/*
10331da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
10341da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
10351da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
10361da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
10371da177e4SLinus Torvalds 	 */
10381da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
10391da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
10401da177e4SLinus Torvalds 	return mtu;
10411da177e4SLinus Torvalds }
10421da177e4SLinus Torvalds 
1043d33e4553SDavid S. Miller static unsigned int ip6_default_mtu(const struct dst_entry *dst)
1044d33e4553SDavid S. Miller {
1045d33e4553SDavid S. Miller 	unsigned int mtu = IPV6_MIN_MTU;
1046d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1047d33e4553SDavid S. Miller 
1048d33e4553SDavid S. Miller 	rcu_read_lock();
1049d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1050d33e4553SDavid S. Miller 	if (idev)
1051d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1052d33e4553SDavid S. Miller 	rcu_read_unlock();
1053d33e4553SDavid S. Miller 
1054d33e4553SDavid S. Miller 	return mtu;
1055d33e4553SDavid S. Miller }
1056d33e4553SDavid S. Miller 
10573b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
10583b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
10595d0bbeebSThomas Graf 
10603b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
10611da177e4SLinus Torvalds 				  struct neighbour *neigh,
10629acd9f3aSYOSHIFUJI Hideaki 				  const struct in6_addr *addr)
10631da177e4SLinus Torvalds {
10641da177e4SLinus Torvalds 	struct rt6_info *rt;
10651da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1066c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	if (unlikely(idev == NULL))
10691da177e4SLinus Torvalds 		return NULL;
10701da177e4SLinus Torvalds 
1071957c665fSDavid S. Miller 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev, 0);
10721da177e4SLinus Torvalds 	if (unlikely(rt == NULL)) {
10731da177e4SLinus Torvalds 		in6_dev_put(idev);
10741da177e4SLinus Torvalds 		goto out;
10751da177e4SLinus Torvalds 	}
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	if (neigh)
10781da177e4SLinus Torvalds 		neigh_hold(neigh);
107914deae41SDavid S. Miller 	else {
10801da177e4SLinus Torvalds 		neigh = ndisc_get_neigh(dev, addr);
108114deae41SDavid S. Miller 		if (IS_ERR(neigh))
108214deae41SDavid S. Miller 			neigh = NULL;
108314deae41SDavid S. Miller 	}
10841da177e4SLinus Torvalds 
10858e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
10868e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
108769cce1d1SDavid S. Miller 	dst_set_neighbour(&rt->dst, neigh);
1088d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
1089defb3519SDavid S. Miller 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
10908e2ec639SYan, Zheng 
10918e2ec639SYan, Zheng 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
10928e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
10938e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
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 
12335578689aSDaniel Lezcano 	table = fib6_new_table(net, cfg->fc_table);
1234c71099acSThomas Graf 	if (table == NULL) {
1235c71099acSThomas Graf 		err = -ENOBUFS;
1236c71099acSThomas Graf 		goto out;
1237c71099acSThomas Graf 	}
1238c71099acSThomas Graf 
1239957c665fSDavid S. Miller 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT);
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 	if (rt == NULL) {
12421da177e4SLinus Torvalds 		err = -ENOMEM;
12431da177e4SLinus Torvalds 		goto out;
12441da177e4SLinus Torvalds 	}
12451da177e4SLinus Torvalds 
1246d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
12476f704992SYOSHIFUJI Hideaki 	rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
12486f704992SYOSHIFUJI Hideaki 				jiffies + clock_t_to_jiffies(cfg->fc_expires) :
12496f704992SYOSHIFUJI Hideaki 				0;
12501da177e4SLinus Torvalds 
125186872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
125286872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
125386872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
125486872cb5SThomas Graf 
125586872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1258d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1259ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1260ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
12611da177e4SLinus Torvalds 	else
1262d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
12631da177e4SLinus Torvalds 
1264d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
12651da177e4SLinus Torvalds 
126686872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
126786872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
12681da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
126911d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
12701da177e4SLinus Torvalds 
12718e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
12728e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
12738e2ec639SYan, Zheng 		if (!metrics) {
12748e2ec639SYan, Zheng 			err = -ENOMEM;
12758e2ec639SYan, Zheng 			goto out;
12768e2ec639SYan, Zheng 		}
12778e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
12788e2ec639SYan, Zheng 	}
12791da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
128086872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
128186872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
12821da177e4SLinus Torvalds #endif
12831da177e4SLinus Torvalds 
128486872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
12871da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
12881da177e4SLinus Torvalds 	 */
128986872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
1290ab79ad14SMaciej Żenczykowski 	    (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1291ab79ad14SMaciej Żenczykowski 					      && !(cfg->fc_flags&RTF_LOCAL))) {
12921da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
12935578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
12941da177e4SLinus Torvalds 			if (dev) {
12951da177e4SLinus Torvalds 				dev_put(dev);
12961da177e4SLinus Torvalds 				in6_dev_put(idev);
12971da177e4SLinus Torvalds 			}
12985578689aSDaniel Lezcano 			dev = net->loopback_dev;
12991da177e4SLinus Torvalds 			dev_hold(dev);
13001da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
13011da177e4SLinus Torvalds 			if (!idev) {
13021da177e4SLinus Torvalds 				err = -ENODEV;
13031da177e4SLinus Torvalds 				goto out;
13041da177e4SLinus Torvalds 			}
13051da177e4SLinus Torvalds 		}
1306d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1307d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
1308d8d1f30bSChangli Gao 		rt->dst.error = -ENETUNREACH;
13091da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
13101da177e4SLinus Torvalds 		goto install_route;
13111da177e4SLinus Torvalds 	}
13121da177e4SLinus Torvalds 
131386872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1314b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
13151da177e4SLinus Torvalds 		int gwa_type;
13161da177e4SLinus Torvalds 
131786872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
131886872cb5SThomas Graf 		ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
13191da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
13221da177e4SLinus Torvalds 			struct rt6_info *grt;
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
13251da177e4SLinus Torvalds 			   addresses as nexthop address.
13261da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
13271da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
13281da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
13291da177e4SLinus Torvalds 			   some exceptions. --ANK
13301da177e4SLinus Torvalds 			 */
13311da177e4SLinus Torvalds 			err = -EINVAL;
13321da177e4SLinus Torvalds 			if (!(gwa_type&IPV6_ADDR_UNICAST))
13331da177e4SLinus Torvalds 				goto out;
13341da177e4SLinus Torvalds 
13355578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
13381da177e4SLinus Torvalds 			if (grt == NULL)
13391da177e4SLinus Torvalds 				goto out;
13401da177e4SLinus Torvalds 			if (dev) {
13411da177e4SLinus Torvalds 				if (dev != grt->rt6i_dev) {
1342d8d1f30bSChangli Gao 					dst_release(&grt->dst);
13431da177e4SLinus Torvalds 					goto out;
13441da177e4SLinus Torvalds 				}
13451da177e4SLinus Torvalds 			} else {
13461da177e4SLinus Torvalds 				dev = grt->rt6i_dev;
13471da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
13481da177e4SLinus Torvalds 				dev_hold(dev);
13491da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
13501da177e4SLinus Torvalds 			}
13511da177e4SLinus Torvalds 			if (!(grt->rt6i_flags&RTF_GATEWAY))
13521da177e4SLinus Torvalds 				err = 0;
1353d8d1f30bSChangli Gao 			dst_release(&grt->dst);
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 			if (err)
13561da177e4SLinus Torvalds 				goto out;
13571da177e4SLinus Torvalds 		}
13581da177e4SLinus Torvalds 		err = -EINVAL;
13591da177e4SLinus Torvalds 		if (dev == NULL || (dev->flags&IFF_LOOPBACK))
13601da177e4SLinus Torvalds 			goto out;
13611da177e4SLinus Torvalds 	}
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	err = -ENODEV;
13641da177e4SLinus Torvalds 	if (dev == NULL)
13651da177e4SLinus Torvalds 		goto out;
13661da177e4SLinus Torvalds 
1367c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1368c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1369c3968a85SDaniel Walter 			err = -EINVAL;
1370c3968a85SDaniel Walter 			goto out;
1371c3968a85SDaniel Walter 		}
1372c3968a85SDaniel Walter 		ipv6_addr_copy(&rt->rt6i_prefsrc.addr, &cfg->fc_prefsrc);
1373c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1374c3968a85SDaniel Walter 	} else
1375c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1376c3968a85SDaniel Walter 
137786872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
137869cce1d1SDavid S. Miller 		struct neighbour *n = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
137969cce1d1SDavid S. Miller 		if (IS_ERR(n)) {
138069cce1d1SDavid S. Miller 			err = PTR_ERR(n);
13811da177e4SLinus Torvalds 			goto out;
13821da177e4SLinus Torvalds 		}
138369cce1d1SDavid S. Miller 		dst_set_neighbour(&rt->dst, n);
13841da177e4SLinus Torvalds 	}
13851da177e4SLinus Torvalds 
138686872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds install_route:
138986872cb5SThomas Graf 	if (cfg->fc_mx) {
139086872cb5SThomas Graf 		struct nlattr *nla;
139186872cb5SThomas Graf 		int remaining;
13921da177e4SLinus Torvalds 
139386872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
13948f4c1f9bSThomas Graf 			int type = nla_type(nla);
139586872cb5SThomas Graf 
139686872cb5SThomas Graf 			if (type) {
139786872cb5SThomas Graf 				if (type > RTAX_MAX) {
13981da177e4SLinus Torvalds 					err = -EINVAL;
13991da177e4SLinus Torvalds 					goto out;
14001da177e4SLinus Torvalds 				}
140186872cb5SThomas Graf 
1402defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
14031da177e4SLinus Torvalds 			}
14041da177e4SLinus Torvalds 		}
14051da177e4SLinus Torvalds 	}
14061da177e4SLinus Torvalds 
1407d8d1f30bSChangli Gao 	rt->dst.dev = dev;
14081da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1409c71099acSThomas Graf 	rt->rt6i_table = table;
141063152fc0SDaniel Lezcano 
1411c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
141263152fc0SDaniel Lezcano 
141386872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds out:
14161da177e4SLinus Torvalds 	if (dev)
14171da177e4SLinus Torvalds 		dev_put(dev);
14181da177e4SLinus Torvalds 	if (idev)
14191da177e4SLinus Torvalds 		in6_dev_put(idev);
14201da177e4SLinus Torvalds 	if (rt)
1421d8d1f30bSChangli Gao 		dst_free(&rt->dst);
14221da177e4SLinus Torvalds 	return err;
14231da177e4SLinus Torvalds }
14241da177e4SLinus Torvalds 
142586872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
14261da177e4SLinus Torvalds {
14271da177e4SLinus Torvalds 	int err;
1428c71099acSThomas Graf 	struct fib6_table *table;
1429c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(rt->rt6i_dev);
14301da177e4SLinus Torvalds 
14318ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry)
14326c813a72SPatrick McHardy 		return -ENOENT;
14336c813a72SPatrick McHardy 
1434c71099acSThomas Graf 	table = rt->rt6i_table;
1435c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
14361da177e4SLinus Torvalds 
143786872cb5SThomas Graf 	err = fib6_del(rt, info);
1438d8d1f30bSChangli Gao 	dst_release(&rt->dst);
14391da177e4SLinus Torvalds 
1440c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 	return err;
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
1445e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1446e0a1ad73SThomas Graf {
14474d1169c1SDenis V. Lunev 	struct nl_info info = {
1448c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
14494d1169c1SDenis V. Lunev 	};
1450528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1451e0a1ad73SThomas Graf }
1452e0a1ad73SThomas Graf 
145386872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
14541da177e4SLinus Torvalds {
1455c71099acSThomas Graf 	struct fib6_table *table;
14561da177e4SLinus Torvalds 	struct fib6_node *fn;
14571da177e4SLinus Torvalds 	struct rt6_info *rt;
14581da177e4SLinus Torvalds 	int err = -ESRCH;
14591da177e4SLinus Torvalds 
14605578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
1461c71099acSThomas Graf 	if (table == NULL)
1462c71099acSThomas Graf 		return err;
14631da177e4SLinus Torvalds 
1464c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1465c71099acSThomas Graf 
1466c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
146786872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
146886872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds 	if (fn) {
1471d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
147286872cb5SThomas Graf 			if (cfg->fc_ifindex &&
14731da177e4SLinus Torvalds 			    (rt->rt6i_dev == NULL ||
147486872cb5SThomas Graf 			     rt->rt6i_dev->ifindex != cfg->fc_ifindex))
14751da177e4SLinus Torvalds 				continue;
147686872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
147786872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
14781da177e4SLinus Torvalds 				continue;
147986872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
14801da177e4SLinus Torvalds 				continue;
1481d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1482c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
14831da177e4SLinus Torvalds 
148486872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
14851da177e4SLinus Torvalds 		}
14861da177e4SLinus Torvalds 	}
1487c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 	return err;
14901da177e4SLinus Torvalds }
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds /*
14931da177e4SLinus Torvalds  *	Handle redirects
14941da177e4SLinus Torvalds  */
1495a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi {
14964c9483b2SDavid S. Miller 	struct flowi6 fl6;
1497a6279458SYOSHIFUJI Hideaki 	struct in6_addr gateway;
1498a6279458SYOSHIFUJI Hideaki };
14991da177e4SLinus Torvalds 
15008ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net,
15018ed67789SDaniel Lezcano 					     struct fib6_table *table,
15024c9483b2SDavid S. Miller 					     struct flowi6 *fl6,
1503a6279458SYOSHIFUJI Hideaki 					     int flags)
1504a6279458SYOSHIFUJI Hideaki {
15054c9483b2SDavid S. Miller 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1506a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt;
1507a6279458SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1508c71099acSThomas Graf 
1509e843b9e1SYOSHIFUJI Hideaki 	/*
1510e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1511e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1512e843b9e1SYOSHIFUJI Hideaki 	 *
1513e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1514e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1515e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1516e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1517e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1518e843b9e1SYOSHIFUJI Hideaki 	 */
15191da177e4SLinus Torvalds 
1520c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
15214c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1522e843b9e1SYOSHIFUJI Hideaki restart:
1523d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
15241da177e4SLinus Torvalds 		/*
15251da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
15261da177e4SLinus Torvalds 		 *
15271da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
15281da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
15291da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
15301da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
15311da177e4SLinus Torvalds 		 */
1532e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1533e843b9e1SYOSHIFUJI Hideaki 			continue;
15341da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1535e843b9e1SYOSHIFUJI Hideaki 			continue;
15364c9483b2SDavid S. Miller 		if (fl6->flowi6_oif != rt->rt6i_dev->ifindex)
1537e843b9e1SYOSHIFUJI Hideaki 			continue;
1538a6279458SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1539e843b9e1SYOSHIFUJI Hideaki 			continue;
1540e843b9e1SYOSHIFUJI Hideaki 		break;
1541e843b9e1SYOSHIFUJI Hideaki 	}
1542a6279458SYOSHIFUJI Hideaki 
1543cb15d9c2SYOSHIFUJI Hideaki 	if (!rt)
15448ed67789SDaniel Lezcano 		rt = net->ipv6.ip6_null_entry;
15454c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
1546cb15d9c2SYOSHIFUJI Hideaki out:
1547d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
1548a6279458SYOSHIFUJI Hideaki 
1549c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
15501da177e4SLinus Torvalds 
1551a6279458SYOSHIFUJI Hideaki 	return rt;
1552a6279458SYOSHIFUJI Hideaki };
1553a6279458SYOSHIFUJI Hideaki 
1554b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1555b71d1d42SEric Dumazet 					   const struct in6_addr *src,
1556b71d1d42SEric Dumazet 					   const struct in6_addr *gateway,
1557a6279458SYOSHIFUJI Hideaki 					   struct net_device *dev)
1558a6279458SYOSHIFUJI Hideaki {
1559adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1560c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
1561a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi rdfl = {
15624c9483b2SDavid S. Miller 		.fl6 = {
15634c9483b2SDavid S. Miller 			.flowi6_oif = dev->ifindex,
15644c9483b2SDavid S. Miller 			.daddr = *dest,
15654c9483b2SDavid S. Miller 			.saddr = *src,
1566a6279458SYOSHIFUJI Hideaki 		},
1567a6279458SYOSHIFUJI Hideaki 	};
1568adaa70bbSThomas Graf 
156986c36ce4SBrian Haley 	ipv6_addr_copy(&rdfl.gateway, gateway);
157086c36ce4SBrian Haley 
1571adaa70bbSThomas Graf 	if (rt6_need_strict(dest))
1572adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
1573a6279458SYOSHIFUJI Hideaki 
15744c9483b2SDavid S. Miller 	return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
157558f09b78SDaniel Lezcano 						   flags, __ip6_route_redirect);
1576a6279458SYOSHIFUJI Hideaki }
1577a6279458SYOSHIFUJI Hideaki 
1578b71d1d42SEric Dumazet void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1579b71d1d42SEric Dumazet 		  const struct in6_addr *saddr,
1580a6279458SYOSHIFUJI Hideaki 		  struct neighbour *neigh, u8 *lladdr, int on_link)
1581a6279458SYOSHIFUJI Hideaki {
1582a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt = NULL;
1583a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1584c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(neigh->dev);
1585a6279458SYOSHIFUJI Hideaki 
1586a6279458SYOSHIFUJI Hideaki 	rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1587a6279458SYOSHIFUJI Hideaki 
15888ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry) {
15891da177e4SLinus Torvalds 		if (net_ratelimit())
15901da177e4SLinus Torvalds 			printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
15911da177e4SLinus Torvalds 			       "for redirect target\n");
1592a6279458SYOSHIFUJI Hideaki 		goto out;
15931da177e4SLinus Torvalds 	}
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 	/*
15961da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
15971da177e4SLinus Torvalds 	 */
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
16001da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
16011da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
16021da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
16031da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
16041da177e4SLinus Torvalds 		     );
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	/*
16071da177e4SLinus Torvalds 	 * Redirect received -> path was valid.
16081da177e4SLinus Torvalds 	 * Look, redirects are sent only in response to data packets,
16091da177e4SLinus Torvalds 	 * so that this nexthop apparently is reachable. --ANK
16101da177e4SLinus Torvalds 	 */
1611d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds 	/* Duplicate redirect: silently ignore. */
1614f2c31e32SEric Dumazet 	if (neigh == dst_get_neighbour_raw(&rt->dst))
16151da177e4SLinus Torvalds 		goto out;
16161da177e4SLinus Torvalds 
161721efcfa0SEric Dumazet 	nrt = ip6_rt_copy(rt, dest);
16181da177e4SLinus Torvalds 	if (nrt == NULL)
16191da177e4SLinus Torvalds 		goto out;
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
16221da177e4SLinus Torvalds 	if (on_link)
16231da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
162669cce1d1SDavid S. Miller 	dst_set_neighbour(&nrt->dst, neigh_clone(neigh));
16271da177e4SLinus Torvalds 
162840e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
16291da177e4SLinus Torvalds 		goto out;
16301da177e4SLinus Torvalds 
1631d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1632d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
16338d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
16348d71740cSTom Tucker 
16351da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE) {
1636e0a1ad73SThomas Graf 		ip6_del_rt(rt);
16371da177e4SLinus Torvalds 		return;
16381da177e4SLinus Torvalds 	}
16391da177e4SLinus Torvalds 
16401da177e4SLinus Torvalds out:
1641d8d1f30bSChangli Gao 	dst_release(&rt->dst);
16421da177e4SLinus Torvalds }
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds /*
16451da177e4SLinus Torvalds  *	Handle ICMP "packet too big" messages
16461da177e4SLinus Torvalds  *	i.e. Path MTU discovery
16471da177e4SLinus Torvalds  */
16481da177e4SLinus Torvalds 
1649b71d1d42SEric Dumazet static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
1650ae878ae2SMaciej Żenczykowski 			     struct net *net, u32 pmtu, int ifindex)
16511da177e4SLinus Torvalds {
16521da177e4SLinus Torvalds 	struct rt6_info *rt, *nrt;
16531da177e4SLinus Torvalds 	int allfrag = 0;
1654d3052b55SAndrey Vagin again:
1655ae878ae2SMaciej Żenczykowski 	rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
16561da177e4SLinus Torvalds 	if (rt == NULL)
16571da177e4SLinus Torvalds 		return;
16581da177e4SLinus Torvalds 
1659d3052b55SAndrey Vagin 	if (rt6_check_expired(rt)) {
1660d3052b55SAndrey Vagin 		ip6_del_rt(rt);
1661d3052b55SAndrey Vagin 		goto again;
1662d3052b55SAndrey Vagin 	}
1663d3052b55SAndrey Vagin 
1664d8d1f30bSChangli Gao 	if (pmtu >= dst_mtu(&rt->dst))
16651da177e4SLinus Torvalds 		goto out;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	if (pmtu < IPV6_MIN_MTU) {
16681da177e4SLinus Torvalds 		/*
16691da177e4SLinus Torvalds 		 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
16701da177e4SLinus Torvalds 		 * MTU (1280) and a fragment header should always be included
16711da177e4SLinus Torvalds 		 * after a node receiving Too Big message reporting PMTU is
16721da177e4SLinus Torvalds 		 * less than the IPv6 Minimum Link MTU.
16731da177e4SLinus Torvalds 		 */
16741da177e4SLinus Torvalds 		pmtu = IPV6_MIN_MTU;
16751da177e4SLinus Torvalds 		allfrag = 1;
16761da177e4SLinus Torvalds 	}
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 	/* New mtu received -> path was valid.
16791da177e4SLinus Torvalds 	   They are sent only in response to data packets,
16801da177e4SLinus Torvalds 	   so that this nexthop apparently is reachable. --ANK
16811da177e4SLinus Torvalds 	 */
1682d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds 	/* Host route. If it is static, it would be better
16851da177e4SLinus Torvalds 	   not to override it, but add new one, so that
16861da177e4SLinus Torvalds 	   when cache entry will expire old pmtu
16871da177e4SLinus Torvalds 	   would return automatically.
16881da177e4SLinus Torvalds 	 */
16891da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
1690defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1691defb3519SDavid S. Miller 		if (allfrag) {
1692defb3519SDavid S. Miller 			u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1693defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1694defb3519SDavid S. Miller 			dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1695defb3519SDavid S. Miller 		}
1696d8d1f30bSChangli Gao 		dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
16971da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
16981da177e4SLinus Torvalds 		goto out;
16991da177e4SLinus Torvalds 	}
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	/* Network route.
17021da177e4SLinus Torvalds 	   Two cases are possible:
17031da177e4SLinus Torvalds 	   1. It is connected route. Action: COW
17041da177e4SLinus Torvalds 	   2. It is gatewayed route or NONEXTHOP route. Action: clone it.
17051da177e4SLinus Torvalds 	 */
1706f2c31e32SEric Dumazet 	if (!dst_get_neighbour_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
1707a1e78363SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, daddr, saddr);
1708d5315b50SYOSHIFUJI Hideaki 	else
1709d5315b50SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, daddr);
1710a1e78363SYOSHIFUJI Hideaki 
1711d5315b50SYOSHIFUJI Hideaki 	if (nrt) {
1712defb3519SDavid S. Miller 		dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1713defb3519SDavid S. Miller 		if (allfrag) {
1714defb3519SDavid S. Miller 			u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1715defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1716defb3519SDavid S. Miller 			dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1717defb3519SDavid S. Miller 		}
1718a1e78363SYOSHIFUJI Hideaki 
17191da177e4SLinus Torvalds 		/* According to RFC 1981, detecting PMTU increase shouldn't be
1720a1e78363SYOSHIFUJI Hideaki 		 * happened within 5 mins, the recommended timer is 10 mins.
1721a1e78363SYOSHIFUJI Hideaki 		 * Here this route expiration time is set to ip6_rt_mtu_expires
1722a1e78363SYOSHIFUJI Hideaki 		 * which is 10 mins. After 10 mins the decreased pmtu is expired
1723a1e78363SYOSHIFUJI Hideaki 		 * and detecting PMTU increase will be automatically happened.
17241da177e4SLinus Torvalds 		 */
1725d8d1f30bSChangli Gao 		dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
17261da177e4SLinus Torvalds 		nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1727a1e78363SYOSHIFUJI Hideaki 
172840e22e8fSThomas Graf 		ip6_ins_rt(nrt);
17291da177e4SLinus Torvalds 	}
17301da177e4SLinus Torvalds out:
1731d8d1f30bSChangli Gao 	dst_release(&rt->dst);
17321da177e4SLinus Torvalds }
17331da177e4SLinus Torvalds 
1734b71d1d42SEric Dumazet void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
1735ae878ae2SMaciej Żenczykowski 			struct net_device *dev, u32 pmtu)
1736ae878ae2SMaciej Żenczykowski {
1737ae878ae2SMaciej Żenczykowski 	struct net *net = dev_net(dev);
1738ae878ae2SMaciej Żenczykowski 
1739ae878ae2SMaciej Żenczykowski 	/*
1740ae878ae2SMaciej Żenczykowski 	 * RFC 1981 states that a node "MUST reduce the size of the packets it
1741ae878ae2SMaciej Żenczykowski 	 * is sending along the path" that caused the Packet Too Big message.
1742ae878ae2SMaciej Żenczykowski 	 * Since it's not possible in the general case to determine which
1743ae878ae2SMaciej Żenczykowski 	 * interface was used to send the original packet, we update the MTU
1744ae878ae2SMaciej Żenczykowski 	 * on the interface that will be used to send future packets. We also
1745ae878ae2SMaciej Żenczykowski 	 * update the MTU on the interface that received the Packet Too Big in
1746ae878ae2SMaciej Żenczykowski 	 * case the original packet was forced out that interface with
1747ae878ae2SMaciej Żenczykowski 	 * SO_BINDTODEVICE or similar. This is the next best thing to the
1748ae878ae2SMaciej Żenczykowski 	 * correct behaviour, which would be to update the MTU on all
1749ae878ae2SMaciej Żenczykowski 	 * interfaces.
1750ae878ae2SMaciej Żenczykowski 	 */
1751ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1752ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1753ae878ae2SMaciej Żenczykowski }
1754ae878ae2SMaciej Żenczykowski 
17551da177e4SLinus Torvalds /*
17561da177e4SLinus Torvalds  *	Misc support functions
17571da177e4SLinus Torvalds  */
17581da177e4SLinus Torvalds 
175921efcfa0SEric Dumazet static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort,
176021efcfa0SEric Dumazet 				    const struct in6_addr *dest)
17611da177e4SLinus Torvalds {
1762c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ort->rt6i_dev);
17635c1e6aa3SDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
1764957c665fSDavid S. Miller 					    ort->dst.dev, 0);
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds 	if (rt) {
1767d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1768d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
17698e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
17701da177e4SLinus Torvalds 
177121efcfa0SEric Dumazet 		ipv6_addr_copy(&rt->rt6i_dst.addr, dest);
17728e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1773defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1774d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
17751da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
17761da177e4SLinus Torvalds 		if (rt->rt6i_idev)
17771da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1778d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
17791da177e4SLinus Torvalds 		rt->rt6i_expires = 0;
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
17821da177e4SLinus Torvalds 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
17831da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
17861da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
17871da177e4SLinus Torvalds #endif
17880f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1789c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
17901da177e4SLinus Torvalds 	}
17911da177e4SLinus Torvalds 	return rt;
17921da177e4SLinus Torvalds }
17931da177e4SLinus Torvalds 
179470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1795efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1796b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1797b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
179870ceb4f5SYOSHIFUJI Hideaki {
179970ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
180070ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1801c71099acSThomas Graf 	struct fib6_table *table;
180270ceb4f5SYOSHIFUJI Hideaki 
1803efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
1804c71099acSThomas Graf 	if (table == NULL)
1805c71099acSThomas Graf 		return NULL;
1806c71099acSThomas Graf 
1807c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1808c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
180970ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
181070ceb4f5SYOSHIFUJI Hideaki 		goto out;
181170ceb4f5SYOSHIFUJI Hideaki 
1812d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
181370ceb4f5SYOSHIFUJI Hideaki 		if (rt->rt6i_dev->ifindex != ifindex)
181470ceb4f5SYOSHIFUJI Hideaki 			continue;
181570ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
181670ceb4f5SYOSHIFUJI Hideaki 			continue;
181770ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
181870ceb4f5SYOSHIFUJI Hideaki 			continue;
1819d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
182070ceb4f5SYOSHIFUJI Hideaki 		break;
182170ceb4f5SYOSHIFUJI Hideaki 	}
182270ceb4f5SYOSHIFUJI Hideaki out:
1823c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
182470ceb4f5SYOSHIFUJI Hideaki 	return rt;
182570ceb4f5SYOSHIFUJI Hideaki }
182670ceb4f5SYOSHIFUJI Hideaki 
1827efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1828b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1829b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
183070ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref)
183170ceb4f5SYOSHIFUJI Hideaki {
183286872cb5SThomas Graf 	struct fib6_config cfg = {
183386872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1834238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
183586872cb5SThomas Graf 		.fc_ifindex	= ifindex,
183686872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
183786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
183886872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
1839efa2cea0SDaniel Lezcano 		.fc_nlinfo.pid = 0,
1840efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1841efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
184286872cb5SThomas Graf 	};
184370ceb4f5SYOSHIFUJI Hideaki 
184486872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_dst, prefix);
184586872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
184686872cb5SThomas Graf 
1847e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1848e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
184986872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
185070ceb4f5SYOSHIFUJI Hideaki 
185186872cb5SThomas Graf 	ip6_route_add(&cfg);
185270ceb4f5SYOSHIFUJI Hideaki 
1853efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
185470ceb4f5SYOSHIFUJI Hideaki }
185570ceb4f5SYOSHIFUJI Hideaki #endif
185670ceb4f5SYOSHIFUJI Hideaki 
1857b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
18581da177e4SLinus Torvalds {
18591da177e4SLinus Torvalds 	struct rt6_info *rt;
1860c71099acSThomas Graf 	struct fib6_table *table;
18611da177e4SLinus Torvalds 
1862c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
1863c71099acSThomas Graf 	if (table == NULL)
1864c71099acSThomas Graf 		return NULL;
18651da177e4SLinus Torvalds 
1866c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1867d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
18681da177e4SLinus Torvalds 		if (dev == rt->rt6i_dev &&
1869045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
18701da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
18711da177e4SLinus Torvalds 			break;
18721da177e4SLinus Torvalds 	}
18731da177e4SLinus Torvalds 	if (rt)
1874d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
1875c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
18761da177e4SLinus Torvalds 	return rt;
18771da177e4SLinus Torvalds }
18781da177e4SLinus Torvalds 
1879b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1880ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1881ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
18821da177e4SLinus Torvalds {
188386872cb5SThomas Graf 	struct fib6_config cfg = {
188486872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1885238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
188686872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
188786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
188886872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
18895578689aSDaniel Lezcano 		.fc_nlinfo.pid = 0,
18905578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1891c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
189286872cb5SThomas Graf 	};
18931da177e4SLinus Torvalds 
189486872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
18951da177e4SLinus Torvalds 
189686872cb5SThomas Graf 	ip6_route_add(&cfg);
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
18991da177e4SLinus Torvalds }
19001da177e4SLinus Torvalds 
19017b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
19021da177e4SLinus Torvalds {
19031da177e4SLinus Torvalds 	struct rt6_info *rt;
1904c71099acSThomas Graf 	struct fib6_table *table;
1905c71099acSThomas Graf 
1906c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
19077b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
1908c71099acSThomas Graf 	if (table == NULL)
1909c71099acSThomas Graf 		return;
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds restart:
1912c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1913d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
19141da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1915d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1916c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1917e0a1ad73SThomas Graf 			ip6_del_rt(rt);
19181da177e4SLinus Torvalds 			goto restart;
19191da177e4SLinus Torvalds 		}
19201da177e4SLinus Torvalds 	}
1921c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
19221da177e4SLinus Torvalds }
19231da177e4SLinus Torvalds 
19245578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
19255578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
192686872cb5SThomas Graf 				 struct fib6_config *cfg)
192786872cb5SThomas Graf {
192886872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
192986872cb5SThomas Graf 
193086872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
193186872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
193286872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
193386872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
193486872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
193586872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
193686872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
193786872cb5SThomas Graf 
19385578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1939f1243c2dSBenjamin Thery 
194086872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
194186872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
194286872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
194386872cb5SThomas Graf }
194486872cb5SThomas Graf 
19455578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
19461da177e4SLinus Torvalds {
194786872cb5SThomas Graf 	struct fib6_config cfg;
19481da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
19491da177e4SLinus Torvalds 	int err;
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	switch(cmd) {
19521da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
19531da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
19541da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
19551da177e4SLinus Torvalds 			return -EPERM;
19561da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
19571da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
19581da177e4SLinus Torvalds 		if (err)
19591da177e4SLinus Torvalds 			return -EFAULT;
19601da177e4SLinus Torvalds 
19615578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
196286872cb5SThomas Graf 
19631da177e4SLinus Torvalds 		rtnl_lock();
19641da177e4SLinus Torvalds 		switch (cmd) {
19651da177e4SLinus Torvalds 		case SIOCADDRT:
196686872cb5SThomas Graf 			err = ip6_route_add(&cfg);
19671da177e4SLinus Torvalds 			break;
19681da177e4SLinus Torvalds 		case SIOCDELRT:
196986872cb5SThomas Graf 			err = ip6_route_del(&cfg);
19701da177e4SLinus Torvalds 			break;
19711da177e4SLinus Torvalds 		default:
19721da177e4SLinus Torvalds 			err = -EINVAL;
19731da177e4SLinus Torvalds 		}
19741da177e4SLinus Torvalds 		rtnl_unlock();
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 		return err;
19773ff50b79SStephen Hemminger 	}
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 	return -EINVAL;
19801da177e4SLinus Torvalds }
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds /*
19831da177e4SLinus Torvalds  *	Drop the packet on the floor
19841da177e4SLinus Torvalds  */
19851da177e4SLinus Torvalds 
1986d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
19871da177e4SLinus Torvalds {
1988612f09e8SYOSHIFUJI Hideaki 	int type;
1989adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
1990612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
1991612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
19920660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
199345bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
19943bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
19953bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
1996612f09e8SYOSHIFUJI Hideaki 			break;
1997612f09e8SYOSHIFUJI Hideaki 		}
1998612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
1999612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
20003bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20013bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2002612f09e8SYOSHIFUJI Hideaki 		break;
2003612f09e8SYOSHIFUJI Hideaki 	}
20043ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
20051da177e4SLinus Torvalds 	kfree_skb(skb);
20061da177e4SLinus Torvalds 	return 0;
20071da177e4SLinus Torvalds }
20081da177e4SLinus Torvalds 
20099ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
20109ce8ade0SThomas Graf {
2011612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
20129ce8ade0SThomas Graf }
20139ce8ade0SThomas Graf 
201420380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
20151da177e4SLinus Torvalds {
2016adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2017612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
20181da177e4SLinus Torvalds }
20191da177e4SLinus Torvalds 
20206723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
20216723ab54SDavid S. Miller 
20229ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
20239ce8ade0SThomas Graf {
2024612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
20259ce8ade0SThomas Graf }
20269ce8ade0SThomas Graf 
20279ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
20289ce8ade0SThomas Graf {
2029adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2030612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
20319ce8ade0SThomas Graf }
20329ce8ade0SThomas Graf 
20336723ab54SDavid S. Miller #endif
20346723ab54SDavid S. Miller 
20351da177e4SLinus Torvalds /*
20361da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
20371da177e4SLinus Torvalds  */
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
20401da177e4SLinus Torvalds 				    const struct in6_addr *addr,
20411da177e4SLinus Torvalds 				    int anycast)
20421da177e4SLinus Torvalds {
2043c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
20445c1e6aa3SDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
2045957c665fSDavid S. Miller 					    net->loopback_dev, 0);
204614deae41SDavid S. Miller 	struct neighbour *neigh;
20471da177e4SLinus Torvalds 
204840385653SBen Greear 	if (rt == NULL) {
204940385653SBen Greear 		if (net_ratelimit())
205040385653SBen Greear 			pr_warning("IPv6:  Maximum number of routes reached,"
205140385653SBen Greear 				   " consider increasing route/max_size.\n");
20521da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
205340385653SBen Greear 	}
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	in6_dev_hold(idev);
20561da177e4SLinus Torvalds 
205711d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2058d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2059d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
20601da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2061d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
206458c4fb86SYOSHIFUJI Hideaki 	if (anycast)
206558c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
206658c4fb86SYOSHIFUJI Hideaki 	else
20671da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
206814deae41SDavid S. Miller 	neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
206914deae41SDavid S. Miller 	if (IS_ERR(neigh)) {
2070d8d1f30bSChangli Gao 		dst_free(&rt->dst);
207114deae41SDavid S. Miller 
207229546a64SDavid S. Miller 		return ERR_CAST(neigh);
20731da177e4SLinus Torvalds 	}
207469cce1d1SDavid S. Miller 	dst_set_neighbour(&rt->dst, neigh);
20751da177e4SLinus Torvalds 
20761da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
20771da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
20785578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
20791da177e4SLinus Torvalds 
2080d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 	return rt;
20831da177e4SLinus Torvalds }
20841da177e4SLinus Torvalds 
2085c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2086c3968a85SDaniel Walter 			struct rt6_info *rt,
2087b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2088c3968a85SDaniel Walter 			unsigned int prefs,
2089c3968a85SDaniel Walter 			struct in6_addr *saddr)
2090c3968a85SDaniel Walter {
2091c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2092c3968a85SDaniel Walter 	int err = 0;
2093c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
2094c3968a85SDaniel Walter 		ipv6_addr_copy(saddr, &rt->rt6i_prefsrc.addr);
2095c3968a85SDaniel Walter 	else
2096c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2097c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2098c3968a85SDaniel Walter 	return err;
2099c3968a85SDaniel Walter }
2100c3968a85SDaniel Walter 
2101c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2102c3968a85SDaniel Walter struct arg_dev_net_ip {
2103c3968a85SDaniel Walter 	struct net_device *dev;
2104c3968a85SDaniel Walter 	struct net *net;
2105c3968a85SDaniel Walter 	struct in6_addr *addr;
2106c3968a85SDaniel Walter };
2107c3968a85SDaniel Walter 
2108c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2109c3968a85SDaniel Walter {
2110c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2111c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2112c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2113c3968a85SDaniel Walter 
2114c3968a85SDaniel Walter 	if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
2115c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2116c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2117c3968a85SDaniel Walter 		/* remove prefsrc entry */
2118c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2119c3968a85SDaniel Walter 	}
2120c3968a85SDaniel Walter 	return 0;
2121c3968a85SDaniel Walter }
2122c3968a85SDaniel Walter 
2123c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2124c3968a85SDaniel Walter {
2125c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2126c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2127c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2128c3968a85SDaniel Walter 		.net = net,
2129c3968a85SDaniel Walter 		.addr = &ifp->addr,
2130c3968a85SDaniel Walter 	};
2131c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2132c3968a85SDaniel Walter }
2133c3968a85SDaniel Walter 
21348ed67789SDaniel Lezcano struct arg_dev_net {
21358ed67789SDaniel Lezcano 	struct net_device *dev;
21368ed67789SDaniel Lezcano 	struct net *net;
21378ed67789SDaniel Lezcano };
21388ed67789SDaniel Lezcano 
21391da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
21401da177e4SLinus Torvalds {
2141bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2142bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
21438ed67789SDaniel Lezcano 
2144bc3ef660Sstephen hemminger 	if ((rt->rt6i_dev == dev || dev == NULL) &&
2145bc3ef660Sstephen hemminger 	    rt != adn->net->ipv6.ip6_null_entry) {
21461da177e4SLinus Torvalds 		RT6_TRACE("deleted by ifdown %p\n", rt);
21471da177e4SLinus Torvalds 		return -1;
21481da177e4SLinus Torvalds 	}
21491da177e4SLinus Torvalds 	return 0;
21501da177e4SLinus Torvalds }
21511da177e4SLinus Torvalds 
2152f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
21531da177e4SLinus Torvalds {
21548ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
21558ed67789SDaniel Lezcano 		.dev = dev,
21568ed67789SDaniel Lezcano 		.net = net,
21578ed67789SDaniel Lezcano 	};
21588ed67789SDaniel Lezcano 
21598ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
21601e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
21611da177e4SLinus Torvalds }
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds struct rt6_mtu_change_arg
21641da177e4SLinus Torvalds {
21651da177e4SLinus Torvalds 	struct net_device *dev;
21661da177e4SLinus Torvalds 	unsigned mtu;
21671da177e4SLinus Torvalds };
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
21701da177e4SLinus Torvalds {
21711da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
21721da177e4SLinus Torvalds 	struct inet6_dev *idev;
21731da177e4SLinus Torvalds 
21741da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
21751da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
21761da177e4SLinus Torvalds 	   We still use this lock to block changes
21771da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
21781da177e4SLinus Torvalds 	*/
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
21811da177e4SLinus Torvalds 	if (idev == NULL)
21821da177e4SLinus Torvalds 		return 0;
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
21851da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
21861da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
21871da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
21881da177e4SLinus Torvalds 	 */
21891da177e4SLinus Torvalds 	/*
21901da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
21911da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
21921da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
21931da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
21941da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
21951da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
21961da177e4SLinus Torvalds 	   PMTU discouvery.
21971da177e4SLinus Torvalds 	 */
21981da177e4SLinus Torvalds 	if (rt->rt6i_dev == arg->dev &&
2199d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2200d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2201d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2202d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2203defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2204566cfd8fSSimon Arlott 	}
22051da177e4SLinus Torvalds 	return 0;
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu)
22091da177e4SLinus Torvalds {
2210c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2211c71099acSThomas Graf 		.dev = dev,
2212c71099acSThomas Graf 		.mtu = mtu,
2213c71099acSThomas Graf 	};
22141da177e4SLinus Torvalds 
2215c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
22161da177e4SLinus Torvalds }
22171da177e4SLinus Torvalds 
2218ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
22195176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
222086872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2221ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
222286872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
222386872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
222486872cb5SThomas Graf };
222586872cb5SThomas Graf 
222686872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
222786872cb5SThomas Graf 			      struct fib6_config *cfg)
22281da177e4SLinus Torvalds {
222986872cb5SThomas Graf 	struct rtmsg *rtm;
223086872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
223186872cb5SThomas Graf 	int err;
22321da177e4SLinus Torvalds 
223386872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
223486872cb5SThomas Graf 	if (err < 0)
223586872cb5SThomas Graf 		goto errout;
22361da177e4SLinus Torvalds 
223786872cb5SThomas Graf 	err = -EINVAL;
223886872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
223986872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
224086872cb5SThomas Graf 
224186872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
224286872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
224386872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
224486872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
224586872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
224686872cb5SThomas Graf 
224786872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
224886872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
224986872cb5SThomas Graf 
2250ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2251ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2252ab79ad14SMaciej Żenczykowski 
225386872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
225486872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
22553b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
225686872cb5SThomas Graf 
225786872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
225886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
225986872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
22601da177e4SLinus Torvalds 	}
226186872cb5SThomas Graf 
226286872cb5SThomas Graf 	if (tb[RTA_DST]) {
226386872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
226486872cb5SThomas Graf 
226586872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
226686872cb5SThomas Graf 			goto errout;
226786872cb5SThomas Graf 
226886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
22691da177e4SLinus Torvalds 	}
227086872cb5SThomas Graf 
227186872cb5SThomas Graf 	if (tb[RTA_SRC]) {
227286872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
227386872cb5SThomas Graf 
227486872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
227586872cb5SThomas Graf 			goto errout;
227686872cb5SThomas Graf 
227786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
22781da177e4SLinus Torvalds 	}
227986872cb5SThomas Graf 
2280c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2281c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2282c3968a85SDaniel Walter 
228386872cb5SThomas Graf 	if (tb[RTA_OIF])
228486872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
228586872cb5SThomas Graf 
228686872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
228786872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
228886872cb5SThomas Graf 
228986872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
229086872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
229186872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
22921da177e4SLinus Torvalds 	}
229386872cb5SThomas Graf 
229486872cb5SThomas Graf 	if (tb[RTA_TABLE])
229586872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
229686872cb5SThomas Graf 
229786872cb5SThomas Graf 	err = 0;
229886872cb5SThomas Graf errout:
229986872cb5SThomas Graf 	return err;
23001da177e4SLinus Torvalds }
23011da177e4SLinus Torvalds 
2302c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23031da177e4SLinus Torvalds {
230486872cb5SThomas Graf 	struct fib6_config cfg;
230586872cb5SThomas Graf 	int err;
23061da177e4SLinus Torvalds 
230786872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
230886872cb5SThomas Graf 	if (err < 0)
230986872cb5SThomas Graf 		return err;
231086872cb5SThomas Graf 
231186872cb5SThomas Graf 	return ip6_route_del(&cfg);
23121da177e4SLinus Torvalds }
23131da177e4SLinus Torvalds 
2314c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23151da177e4SLinus Torvalds {
231686872cb5SThomas Graf 	struct fib6_config cfg;
231786872cb5SThomas Graf 	int err;
23181da177e4SLinus Torvalds 
231986872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
232086872cb5SThomas Graf 	if (err < 0)
232186872cb5SThomas Graf 		return err;
232286872cb5SThomas Graf 
232386872cb5SThomas Graf 	return ip6_route_add(&cfg);
23241da177e4SLinus Torvalds }
23251da177e4SLinus Torvalds 
2326339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2327339bf98fSThomas Graf {
2328339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2329339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2330339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2331339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2332339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2333339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2334339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2335339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2336339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
23376a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2338339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2339339bf98fSThomas Graf }
2340339bf98fSThomas Graf 
2341191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2342191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
23430d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
23440d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
23457bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
23461da177e4SLinus Torvalds {
23471da177e4SLinus Torvalds 	struct rtmsg *rtm;
23481da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2349e3703b3dSThomas Graf 	long expires;
23509e762a4aSPatrick McHardy 	u32 table;
2351f2c31e32SEric Dumazet 	struct neighbour *n;
23521da177e4SLinus Torvalds 
23531da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
23541da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
23551da177e4SLinus Torvalds 			/* success since this is not a prefix route */
23561da177e4SLinus Torvalds 			return 1;
23571da177e4SLinus Torvalds 		}
23581da177e4SLinus Torvalds 	}
23591da177e4SLinus Torvalds 
23602d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
23612d7202bfSThomas Graf 	if (nlh == NULL)
236226932566SPatrick McHardy 		return -EMSGSIZE;
23632d7202bfSThomas Graf 
23642d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
23651da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
23661da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
23671da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
23681da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2369c71099acSThomas Graf 	if (rt->rt6i_table)
23709e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2371c71099acSThomas Graf 	else
23729e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
23739e762a4aSPatrick McHardy 	rtm->rtm_table = table;
23742d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_TABLE, table);
23751da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_REJECT)
23761da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
2377ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags&RTF_LOCAL)
2378ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
23791da177e4SLinus Torvalds 	else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
23801da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
23811da177e4SLinus Torvalds 	else
23821da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
23831da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
23841da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
23851da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
23861da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_DYNAMIC)
23871da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
23881da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
23891da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
23901da177e4SLinus Torvalds 	else if (rt->rt6i_flags&RTF_DEFAULT)
23911da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE)
23941da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds 	if (dst) {
23972d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, dst);
23981da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
23991da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
24002d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
24011da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
24021da177e4SLinus Torvalds 	if (src) {
24032d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, src);
24041da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
24051da177e4SLinus Torvalds 	} else if (rtm->rtm_src_len)
24062d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
24071da177e4SLinus Torvalds #endif
24087bc570c8SYOSHIFUJI Hideaki 	if (iif) {
24097bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
24107bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
24118229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
24127bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
24137bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
24147bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
24157bc570c8SYOSHIFUJI Hideaki 						return 0;
24167bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
24177bc570c8SYOSHIFUJI Hideaki 				} else {
24187bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
24197bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
24207bc570c8SYOSHIFUJI Hideaki 				}
24217bc570c8SYOSHIFUJI Hideaki 			}
24227bc570c8SYOSHIFUJI Hideaki 		} else
24237bc570c8SYOSHIFUJI Hideaki #endif
24242d7202bfSThomas Graf 			NLA_PUT_U32(skb, RTA_IIF, iif);
24257bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
24261da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2427c3968a85SDaniel Walter 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0)
2428c3968a85SDaniel Walter 			NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2429c3968a85SDaniel Walter 	}
2430c3968a85SDaniel Walter 
2431c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2432c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
2433c3968a85SDaniel Walter 		ipv6_addr_copy(&saddr_buf, &rt->rt6i_prefsrc.addr);
24342d7202bfSThomas Graf 		NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
24351da177e4SLinus Torvalds 	}
24362d7202bfSThomas Graf 
2437defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
24382d7202bfSThomas Graf 		goto nla_put_failure;
24392d7202bfSThomas Graf 
2440f2c31e32SEric Dumazet 	rcu_read_lock();
2441f2c31e32SEric Dumazet 	n = dst_get_neighbour(&rt->dst);
2442f2c31e32SEric Dumazet 	if (n)
2443f2c31e32SEric Dumazet 		NLA_PUT(skb, RTA_GATEWAY, 16, &n->primary_key);
2444f2c31e32SEric Dumazet 	rcu_read_unlock();
24452d7202bfSThomas Graf 
2446d8d1f30bSChangli Gao 	if (rt->dst.dev)
24472d7202bfSThomas Graf 		NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
24482d7202bfSThomas Graf 
24492d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
2450e3703b3dSThomas Graf 
245136e3deaeSYOSHIFUJI Hideaki 	if (!(rt->rt6i_flags & RTF_EXPIRES))
245236e3deaeSYOSHIFUJI Hideaki 		expires = 0;
245336e3deaeSYOSHIFUJI Hideaki 	else if (rt->rt6i_expires - jiffies < INT_MAX)
245436e3deaeSYOSHIFUJI Hideaki 		expires = rt->rt6i_expires - jiffies;
245536e3deaeSYOSHIFUJI Hideaki 	else
245636e3deaeSYOSHIFUJI Hideaki 		expires = INT_MAX;
245769cdf8f9SYOSHIFUJI Hideaki 
2458d8d1f30bSChangli Gao 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2459d8d1f30bSChangli Gao 			       expires, rt->dst.error) < 0)
2460e3703b3dSThomas Graf 		goto nla_put_failure;
24611da177e4SLinus Torvalds 
24622d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
24632d7202bfSThomas Graf 
24642d7202bfSThomas Graf nla_put_failure:
246526932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
246626932566SPatrick McHardy 	return -EMSGSIZE;
24671da177e4SLinus Torvalds }
24681da177e4SLinus Torvalds 
24691b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
24701da177e4SLinus Torvalds {
24711da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
24721da177e4SLinus Torvalds 	int prefix;
24731da177e4SLinus Torvalds 
24742d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
24752d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
24761da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
24771da177e4SLinus Torvalds 	} else
24781da177e4SLinus Torvalds 		prefix = 0;
24791da177e4SLinus Torvalds 
2480191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2481191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
24821da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
24837bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
24841da177e4SLinus Torvalds }
24851da177e4SLinus Torvalds 
2486c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
24871da177e4SLinus Torvalds {
24883b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2489ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
24901da177e4SLinus Torvalds 	struct rt6_info *rt;
2491ab364a6fSThomas Graf 	struct sk_buff *skb;
2492ab364a6fSThomas Graf 	struct rtmsg *rtm;
24934c9483b2SDavid S. Miller 	struct flowi6 fl6;
2494ab364a6fSThomas Graf 	int err, iif = 0;
2495ab364a6fSThomas Graf 
2496ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2497ab364a6fSThomas Graf 	if (err < 0)
2498ab364a6fSThomas Graf 		goto errout;
2499ab364a6fSThomas Graf 
2500ab364a6fSThomas Graf 	err = -EINVAL;
25014c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2502ab364a6fSThomas Graf 
2503ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2504ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2505ab364a6fSThomas Graf 			goto errout;
2506ab364a6fSThomas Graf 
25074c9483b2SDavid S. Miller 		ipv6_addr_copy(&fl6.saddr, nla_data(tb[RTA_SRC]));
2508ab364a6fSThomas Graf 	}
2509ab364a6fSThomas Graf 
2510ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2511ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2512ab364a6fSThomas Graf 			goto errout;
2513ab364a6fSThomas Graf 
25144c9483b2SDavid S. Miller 		ipv6_addr_copy(&fl6.daddr, nla_data(tb[RTA_DST]));
2515ab364a6fSThomas Graf 	}
2516ab364a6fSThomas Graf 
2517ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2518ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2519ab364a6fSThomas Graf 
2520ab364a6fSThomas Graf 	if (tb[RTA_OIF])
25214c9483b2SDavid S. Miller 		fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]);
2522ab364a6fSThomas Graf 
2523ab364a6fSThomas Graf 	if (iif) {
2524ab364a6fSThomas Graf 		struct net_device *dev;
25255578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2526ab364a6fSThomas Graf 		if (!dev) {
2527ab364a6fSThomas Graf 			err = -ENODEV;
2528ab364a6fSThomas Graf 			goto errout;
2529ab364a6fSThomas Graf 		}
2530ab364a6fSThomas Graf 	}
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2533ab364a6fSThomas Graf 	if (skb == NULL) {
2534ab364a6fSThomas Graf 		err = -ENOBUFS;
2535ab364a6fSThomas Graf 		goto errout;
2536ab364a6fSThomas Graf 	}
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
25391da177e4SLinus Torvalds 	   through good chunk of routing engine.
25401da177e4SLinus Torvalds 	 */
2541459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
25421da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
25431da177e4SLinus Torvalds 
25444c9483b2SDavid S. Miller 	rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6);
2545d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
25461da177e4SLinus Torvalds 
25474c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
25481da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
25497bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
25501da177e4SLinus Torvalds 	if (err < 0) {
2551ab364a6fSThomas Graf 		kfree_skb(skb);
2552ab364a6fSThomas Graf 		goto errout;
25531da177e4SLinus Torvalds 	}
25541da177e4SLinus Torvalds 
25555578689aSDaniel Lezcano 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2556ab364a6fSThomas Graf errout:
25571da177e4SLinus Torvalds 	return err;
25581da177e4SLinus Torvalds }
25591da177e4SLinus Torvalds 
256086872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
25611da177e4SLinus Torvalds {
25621da177e4SLinus Torvalds 	struct sk_buff *skb;
25635578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2564528c4cebSDenis V. Lunev 	u32 seq;
2565528c4cebSDenis V. Lunev 	int err;
25660d51aa80SJamal Hadi Salim 
2567528c4cebSDenis V. Lunev 	err = -ENOBUFS;
2568528c4cebSDenis V. Lunev 	seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
256986872cb5SThomas Graf 
2570339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
257121713ebcSThomas Graf 	if (skb == NULL)
257221713ebcSThomas Graf 		goto errout;
25731da177e4SLinus Torvalds 
2574191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
25757bc570c8SYOSHIFUJI Hideaki 				event, info->pid, seq, 0, 0, 0);
257626932566SPatrick McHardy 	if (err < 0) {
257726932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
257826932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
257926932566SPatrick McHardy 		kfree_skb(skb);
258026932566SPatrick McHardy 		goto errout;
258126932566SPatrick McHardy 	}
25821ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
25835578689aSDaniel Lezcano 		    info->nlh, gfp_any());
25841ce85fe4SPablo Neira Ayuso 	return;
258521713ebcSThomas Graf errout:
258621713ebcSThomas Graf 	if (err < 0)
25875578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
25881da177e4SLinus Torvalds }
25891da177e4SLinus Torvalds 
25908ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
25918ed67789SDaniel Lezcano 				unsigned long event, void *data)
25928ed67789SDaniel Lezcano {
25938ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2594c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
25958ed67789SDaniel Lezcano 
25968ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2597d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
25988ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
25998ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2600d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
26018ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2602d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
26038ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
26048ed67789SDaniel Lezcano #endif
26058ed67789SDaniel Lezcano 	}
26068ed67789SDaniel Lezcano 
26078ed67789SDaniel Lezcano 	return NOTIFY_OK;
26088ed67789SDaniel Lezcano }
26098ed67789SDaniel Lezcano 
26101da177e4SLinus Torvalds /*
26111da177e4SLinus Torvalds  *	/proc
26121da177e4SLinus Torvalds  */
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
26151da177e4SLinus Torvalds 
26161da177e4SLinus Torvalds struct rt6_proc_arg
26171da177e4SLinus Torvalds {
26181da177e4SLinus Torvalds 	char *buffer;
26191da177e4SLinus Torvalds 	int offset;
26201da177e4SLinus Torvalds 	int length;
26211da177e4SLinus Torvalds 	int skip;
26221da177e4SLinus Torvalds 	int len;
26231da177e4SLinus Torvalds };
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
26261da177e4SLinus Torvalds {
262733120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
262869cce1d1SDavid S. Miller 	struct neighbour *n;
26291da177e4SLinus Torvalds 
26304b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
26311da177e4SLinus Torvalds 
26321da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
26334b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
26341da177e4SLinus Torvalds #else
263533120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
26361da177e4SLinus Torvalds #endif
2637f2c31e32SEric Dumazet 	rcu_read_lock();
263869cce1d1SDavid S. Miller 	n = dst_get_neighbour(&rt->dst);
263969cce1d1SDavid S. Miller 	if (n) {
264069cce1d1SDavid S. Miller 		seq_printf(m, "%pi6", n->primary_key);
26411da177e4SLinus Torvalds 	} else {
264233120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
26431da177e4SLinus Torvalds 	}
2644f2c31e32SEric Dumazet 	rcu_read_unlock();
264533120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2646d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2647d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
26481da177e4SLinus Torvalds 		   rt->rt6i_dev ? rt->rt6i_dev->name : "");
26491da177e4SLinus Torvalds 	return 0;
26501da177e4SLinus Torvalds }
26511da177e4SLinus Torvalds 
265233120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
26531da177e4SLinus Torvalds {
2654f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
2655f3db4851SDaniel Lezcano 	fib6_clean_all(net, rt6_info_route, 0, m);
265633120b30SAlexey Dobriyan 	return 0;
26571da177e4SLinus Torvalds }
26581da177e4SLinus Torvalds 
265933120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
266033120b30SAlexey Dobriyan {
2661de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2662f3db4851SDaniel Lezcano }
2663f3db4851SDaniel Lezcano 
266433120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
266533120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
266633120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
266733120b30SAlexey Dobriyan 	.read		= seq_read,
266833120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2669b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
267033120b30SAlexey Dobriyan };
267133120b30SAlexey Dobriyan 
26721da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
26731da177e4SLinus Torvalds {
267469ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
26751da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
267669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
267769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
267869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
267969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
268069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2681fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
268269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds 	return 0;
26851da177e4SLinus Torvalds }
26861da177e4SLinus Torvalds 
26871da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
26881da177e4SLinus Torvalds {
2689de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
269069ddb805SDaniel Lezcano }
269169ddb805SDaniel Lezcano 
26929a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
26931da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
26941da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
26951da177e4SLinus Torvalds 	.read	 = seq_read,
26961da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2697b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
26981da177e4SLinus Torvalds };
26991da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
27021da177e4SLinus Torvalds 
27031da177e4SLinus Torvalds static
27048d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
27051da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
27061da177e4SLinus Torvalds {
2707c486da34SLucian Adrian Grijincu 	struct net *net;
2708c486da34SLucian Adrian Grijincu 	int delay;
2709c486da34SLucian Adrian Grijincu 	if (!write)
2710c486da34SLucian Adrian Grijincu 		return -EINVAL;
2711c486da34SLucian Adrian Grijincu 
2712c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2713c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
27148d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
27155b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
27161da177e4SLinus Torvalds 	return 0;
27171da177e4SLinus Torvalds }
27181da177e4SLinus Torvalds 
2719760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
27201da177e4SLinus Torvalds 	{
27211da177e4SLinus Torvalds 		.procname	=	"flush",
27224990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
27231da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
272489c8b3a1SDave Jones 		.mode		=	0200,
27256d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
27261da177e4SLinus Torvalds 	},
27271da177e4SLinus Torvalds 	{
27281da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
27299a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
27301da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27311da177e4SLinus Torvalds 		.mode		=	0644,
27326d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27331da177e4SLinus Torvalds 	},
27341da177e4SLinus Torvalds 	{
27351da177e4SLinus Torvalds 		.procname	=	"max_size",
27364990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
27371da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27381da177e4SLinus Torvalds 		.mode		=	0644,
27396d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27401da177e4SLinus Torvalds 	},
27411da177e4SLinus Torvalds 	{
27421da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
27434990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
27441da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27451da177e4SLinus Torvalds 		.mode		=	0644,
27466d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27471da177e4SLinus Torvalds 	},
27481da177e4SLinus Torvalds 	{
27491da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
27504990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
27511da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27521da177e4SLinus Torvalds 		.mode		=	0644,
27536d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27541da177e4SLinus Torvalds 	},
27551da177e4SLinus Torvalds 	{
27561da177e4SLinus Torvalds 		.procname	=	"gc_interval",
27574990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
27581da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27591da177e4SLinus Torvalds 		.mode		=	0644,
27606d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27611da177e4SLinus Torvalds 	},
27621da177e4SLinus Torvalds 	{
27631da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
27644990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
27651da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27661da177e4SLinus Torvalds 		.mode		=	0644,
2767f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
27681da177e4SLinus Torvalds 	},
27691da177e4SLinus Torvalds 	{
27701da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
27714990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
27721da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27731da177e4SLinus Torvalds 		.mode		=	0644,
27746d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27751da177e4SLinus Torvalds 	},
27761da177e4SLinus Torvalds 	{
27771da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
27784990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
27791da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27801da177e4SLinus Torvalds 		.mode		=	0644,
2781f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
27821da177e4SLinus Torvalds 	},
27831da177e4SLinus Torvalds 	{
27841da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
27854990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
27861da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27871da177e4SLinus Torvalds 		.mode		=	0644,
27886d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
27891da177e4SLinus Torvalds 	},
2790f8572d8fSEric W. Biederman 	{ }
27911da177e4SLinus Torvalds };
27921da177e4SLinus Torvalds 
27932c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2794760f2d01SDaniel Lezcano {
2795760f2d01SDaniel Lezcano 	struct ctl_table *table;
2796760f2d01SDaniel Lezcano 
2797760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2798760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2799760f2d01SDaniel Lezcano 			GFP_KERNEL);
28005ee09105SYOSHIFUJI Hideaki 
28015ee09105SYOSHIFUJI Hideaki 	if (table) {
28025ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2803c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
280486393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
28055ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
28065ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28075ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
28085ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
28095ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
28105ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
28115ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
28129c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28135ee09105SYOSHIFUJI Hideaki 	}
28145ee09105SYOSHIFUJI Hideaki 
2815760f2d01SDaniel Lezcano 	return table;
2816760f2d01SDaniel Lezcano }
28171da177e4SLinus Torvalds #endif
28181da177e4SLinus Torvalds 
28192c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2820cdb18761SDaniel Lezcano {
2821633d424bSPavel Emelyanov 	int ret = -ENOMEM;
28228ed67789SDaniel Lezcano 
282386393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
282486393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2825f2fc6a54SBenjamin Thery 
2826fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2827fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2828fc66f95cSEric Dumazet 
28298ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
28308ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
28318ed67789SDaniel Lezcano 					   GFP_KERNEL);
28328ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2833fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2834d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
28358ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2836d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
283762fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
283862fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28398ed67789SDaniel Lezcano 
28408ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
28418ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
28428ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
28438ed67789SDaniel Lezcano 					       GFP_KERNEL);
284468fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
284568fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2846d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
28478ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2848d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
284962fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
285062fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28518ed67789SDaniel Lezcano 
28528ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
28538ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
28548ed67789SDaniel Lezcano 					       GFP_KERNEL);
285568fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
285668fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2857d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
28588ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2859d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
286062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
286162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28628ed67789SDaniel Lezcano #endif
28638ed67789SDaniel Lezcano 
2864b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2865b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2866b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2867b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2868b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2869b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2870b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2871b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2872b339a47cSPeter Zijlstra 
2873cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2874cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2875cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2876cdb18761SDaniel Lezcano #endif
28776891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
28786891a346SBenjamin Thery 
28798ed67789SDaniel Lezcano 	ret = 0;
28808ed67789SDaniel Lezcano out:
28818ed67789SDaniel Lezcano 	return ret;
2882f2fc6a54SBenjamin Thery 
288368fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
288468fffc67SPeter Zijlstra out_ip6_prohibit_entry:
288568fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
288668fffc67SPeter Zijlstra out_ip6_null_entry:
288768fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
288868fffc67SPeter Zijlstra #endif
2889fc66f95cSEric Dumazet out_ip6_dst_entries:
2890fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2891f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2892f2fc6a54SBenjamin Thery 	goto out;
2893cdb18761SDaniel Lezcano }
2894cdb18761SDaniel Lezcano 
28952c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2896cdb18761SDaniel Lezcano {
2897cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2898cdb18761SDaniel Lezcano 	proc_net_remove(net, "ipv6_route");
2899cdb18761SDaniel Lezcano 	proc_net_remove(net, "rt6_stats");
2900cdb18761SDaniel Lezcano #endif
29018ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
29028ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29038ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
29048ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
29058ed67789SDaniel Lezcano #endif
290641bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2907cdb18761SDaniel Lezcano }
2908cdb18761SDaniel Lezcano 
2909cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
2910cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
2911cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
2912cdb18761SDaniel Lezcano };
2913cdb18761SDaniel Lezcano 
29148ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
29158ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
29168ed67789SDaniel Lezcano 	.priority = 0,
29178ed67789SDaniel Lezcano };
29188ed67789SDaniel Lezcano 
2919433d49c3SDaniel Lezcano int __init ip6_route_init(void)
29201da177e4SLinus Torvalds {
2921433d49c3SDaniel Lezcano 	int ret;
2922433d49c3SDaniel Lezcano 
29239a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
29249a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
29259a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
29269a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
29279a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
2928c19a28e1SFernando Carrijo 		goto out;
292914e50e57SDavid S. Miller 
2930fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
29318ed67789SDaniel Lezcano 	if (ret)
2932bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
2933bdb3289fSDaniel Lezcano 
2934fc66f95cSEric Dumazet 	ret = register_pernet_subsys(&ip6_route_net_ops);
2935fc66f95cSEric Dumazet 	if (ret)
2936fc66f95cSEric Dumazet 		goto out_dst_entries;
2937fc66f95cSEric Dumazet 
29385dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
29395dc121e9SArnaud Ebalard 
29408ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
29418ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
29428ed67789SDaniel Lezcano 	 * manually for init_net */
2943d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
29448ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2945bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2946d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
29478ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2948d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
29498ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2950bdb3289fSDaniel Lezcano   #endif
2951433d49c3SDaniel Lezcano 	ret = fib6_init();
2952433d49c3SDaniel Lezcano 	if (ret)
29538ed67789SDaniel Lezcano 		goto out_register_subsys;
2954433d49c3SDaniel Lezcano 
2955433d49c3SDaniel Lezcano 	ret = xfrm6_init();
2956433d49c3SDaniel Lezcano 	if (ret)
2957cdb18761SDaniel Lezcano 		goto out_fib6_init;
2958c35b7e72SDaniel Lezcano 
2959433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
2960433d49c3SDaniel Lezcano 	if (ret)
2961433d49c3SDaniel Lezcano 		goto xfrm6_init;
29627e5449c2SDaniel Lezcano 
2963433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
2964c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
2965c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
2966c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
2967433d49c3SDaniel Lezcano 		goto fib6_rules_init;
2968433d49c3SDaniel Lezcano 
29698ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
2970cdb18761SDaniel Lezcano 	if (ret)
2971cdb18761SDaniel Lezcano 		goto fib6_rules_init;
29728ed67789SDaniel Lezcano 
2973433d49c3SDaniel Lezcano out:
2974433d49c3SDaniel Lezcano 	return ret;
2975433d49c3SDaniel Lezcano 
2976433d49c3SDaniel Lezcano fib6_rules_init:
2977433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
2978433d49c3SDaniel Lezcano xfrm6_init:
2979433d49c3SDaniel Lezcano 	xfrm6_fini();
2980433d49c3SDaniel Lezcano out_fib6_init:
2981433d49c3SDaniel Lezcano 	fib6_gc_cleanup();
29828ed67789SDaniel Lezcano out_register_subsys:
29838ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
2984fc66f95cSEric Dumazet out_dst_entries:
2985fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
2986433d49c3SDaniel Lezcano out_kmem_cache:
2987f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
2988433d49c3SDaniel Lezcano 	goto out;
29891da177e4SLinus Torvalds }
29901da177e4SLinus Torvalds 
29911da177e4SLinus Torvalds void ip6_route_cleanup(void)
29921da177e4SLinus Torvalds {
29938ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
2994101367c2SThomas Graf 	fib6_rules_cleanup();
29951da177e4SLinus Torvalds 	xfrm6_fini();
29961da177e4SLinus Torvalds 	fib6_gc_cleanup();
29978ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
299841bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
2999f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
30001da177e4SLinus Torvalds }
3001