xref: /openbmc/linux/net/ipv6/route.c (revision cf91166223772ef4a2ed98b9874958bf6a2470df)
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 
751da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
761da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
770dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
78d33e4553SDavid S. Miller static unsigned int	 ip6_default_mtu(const struct dst_entry *dst);
791da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
801da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
811da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
821da177e4SLinus Torvalds 				       struct net_device *dev, int how);
83569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
861da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
871da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
881da177e4SLinus Torvalds static void		ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
891da177e4SLinus Torvalds 
9070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
91efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
92b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
93b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
9470ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref);
95efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
96b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
97b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
9870ceb4f5SYOSHIFUJI Hideaki #endif
9970ceb4f5SYOSHIFUJI Hideaki 
10006582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
10106582540SDavid S. Miller {
10206582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
10306582540SDavid S. Miller 	struct inet_peer *peer;
10406582540SDavid S. Miller 	u32 *p = NULL;
10506582540SDavid S. Miller 
10606582540SDavid S. Miller 	if (!rt->rt6i_peer)
10706582540SDavid S. Miller 		rt6_bind_peer(rt, 1);
10806582540SDavid S. Miller 
10906582540SDavid S. Miller 	peer = rt->rt6i_peer;
11006582540SDavid S. Miller 	if (peer) {
11106582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
11206582540SDavid S. Miller 		unsigned long prev, new;
11306582540SDavid S. Miller 
11406582540SDavid S. Miller 		p = peer->metrics;
11506582540SDavid S. Miller 		if (inet_metrics_new(peer))
11606582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
11706582540SDavid S. Miller 
11806582540SDavid S. Miller 		new = (unsigned long) p;
11906582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
12006582540SDavid S. Miller 
12106582540SDavid S. Miller 		if (prev != old) {
12206582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
12306582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
12406582540SDavid S. Miller 				p = NULL;
12506582540SDavid S. Miller 		}
12606582540SDavid S. Miller 	}
12706582540SDavid S. Miller 	return p;
12806582540SDavid S. Miller }
12906582540SDavid S. Miller 
1309a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1311da177e4SLinus Torvalds 	.family			=	AF_INET6,
13209640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1331da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1341da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1351da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1360dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
137d33e4553SDavid S. Miller 	.default_mtu		=	ip6_default_mtu,
13806582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1391da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1401da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1411da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1421da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1431da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1441ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
1451da177e4SLinus Torvalds };
1461da177e4SLinus Torvalds 
147ec831ea7SRoland Dreier static unsigned int ip6_blackhole_default_mtu(const struct dst_entry *dst)
148ec831ea7SRoland Dreier {
149ec831ea7SRoland Dreier 	return 0;
150ec831ea7SRoland Dreier }
151ec831ea7SRoland Dreier 
15214e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
15314e50e57SDavid S. Miller {
15414e50e57SDavid S. Miller }
15514e50e57SDavid S. Miller 
1560972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1570972ddb2SHeld Bernhard 					 unsigned long old)
1580972ddb2SHeld Bernhard {
1590972ddb2SHeld Bernhard 	return NULL;
1600972ddb2SHeld Bernhard }
1610972ddb2SHeld Bernhard 
16214e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
16314e50e57SDavid S. Miller 	.family			=	AF_INET6,
16409640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
16514e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
16614e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
167ec831ea7SRoland Dreier 	.default_mtu		=	ip6_blackhole_default_mtu,
168214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
16914e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
1700972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
17114e50e57SDavid S. Miller };
17214e50e57SDavid S. Miller 
17362fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
17462fa8a84SDavid S. Miller 	[RTAX_HOPLIMIT - 1] = 255,
17562fa8a84SDavid S. Miller };
17662fa8a84SDavid S. Miller 
177bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = {
1781da177e4SLinus Torvalds 	.dst = {
1791da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
1801da177e4SLinus Torvalds 		.__use		= 1,
1811da177e4SLinus Torvalds 		.obsolete	= -1,
1821da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
1831da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
1841da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
1851da177e4SLinus Torvalds 	},
1861da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
1874f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
1881da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
1891da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
1901da177e4SLinus Torvalds };
1911da177e4SLinus Torvalds 
192101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
193101367c2SThomas Graf 
1946723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
1956723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
1966723ab54SDavid S. Miller 
197280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = {
198101367c2SThomas Graf 	.dst = {
199101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
200101367c2SThomas Graf 		.__use		= 1,
201101367c2SThomas Graf 		.obsolete	= -1,
202101367c2SThomas Graf 		.error		= -EACCES,
2039ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2049ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
205101367c2SThomas Graf 	},
206101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2074f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
208101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
209101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
210101367c2SThomas Graf };
211101367c2SThomas Graf 
212bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = {
213101367c2SThomas Graf 	.dst = {
214101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
215101367c2SThomas Graf 		.__use		= 1,
216101367c2SThomas Graf 		.obsolete	= -1,
217101367c2SThomas Graf 		.error		= -EINVAL,
218352e512cSHerbert Xu 		.input		= dst_discard,
219352e512cSHerbert Xu 		.output		= dst_discard,
220101367c2SThomas Graf 	},
221101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2224f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
223101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
224101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
225101367c2SThomas Graf };
226101367c2SThomas Graf 
227101367c2SThomas Graf #endif
228101367c2SThomas Graf 
2291da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
2305c1e6aa3SDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops,
2315c1e6aa3SDavid S. Miller 					     struct net_device *dev)
2321da177e4SLinus Torvalds {
233*cf911662SDavid S. Miller 	struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, 0);
234*cf911662SDavid S. Miller 
235*cf911662SDavid S. Miller 	memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
236*cf911662SDavid S. Miller 
237*cf911662SDavid S. Miller 	return rt;
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2431da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
244b3419363SDavid S. Miller 	struct inet_peer *peer = rt->rt6i_peer;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	if (idev != NULL) {
2471da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2481da177e4SLinus Torvalds 		in6_dev_put(idev);
2491da177e4SLinus Torvalds 	}
250b3419363SDavid S. Miller 	if (peer) {
251b3419363SDavid S. Miller 		rt->rt6i_peer = NULL;
252b3419363SDavid S. Miller 		inet_putpeer(peer);
253b3419363SDavid S. Miller 	}
254b3419363SDavid S. Miller }
255b3419363SDavid S. Miller 
2566431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
2576431cbc2SDavid S. Miller 
2586431cbc2SDavid S. Miller static u32 rt6_peer_genid(void)
2596431cbc2SDavid S. Miller {
2606431cbc2SDavid S. Miller 	return atomic_read(&__rt6_peer_genid);
2616431cbc2SDavid S. Miller }
2626431cbc2SDavid S. Miller 
263b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
264b3419363SDavid S. Miller {
265b3419363SDavid S. Miller 	struct inet_peer *peer;
266b3419363SDavid S. Miller 
267b3419363SDavid S. Miller 	peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
268b3419363SDavid S. Miller 	if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
269b3419363SDavid S. Miller 		inet_putpeer(peer);
2706431cbc2SDavid S. Miller 	else
2716431cbc2SDavid S. Miller 		rt->rt6i_peer_genid = rt6_peer_genid();
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
2751da177e4SLinus Torvalds 			   int how)
2761da177e4SLinus Torvalds {
2771da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2781da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2795a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
280c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
2811da177e4SLinus Torvalds 
2825a3e55d6SDenis V. Lunev 	if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
2835a3e55d6SDenis V. Lunev 		struct inet6_dev *loopback_idev =
2845a3e55d6SDenis V. Lunev 			in6_dev_get(loopback_dev);
2851da177e4SLinus Torvalds 		if (loopback_idev != NULL) {
2861da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
2871da177e4SLinus Torvalds 			in6_dev_put(idev);
2881da177e4SLinus Torvalds 		}
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds static __inline__ int rt6_check_expired(const struct rt6_info *rt)
2931da177e4SLinus Torvalds {
294a02cec21SEric Dumazet 	return (rt->rt6i_flags & RTF_EXPIRES) &&
295a02cec21SEric Dumazet 		time_after(jiffies, rt->rt6i_expires);
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
298b71d1d42SEric Dumazet static inline int rt6_need_strict(const struct in6_addr *daddr)
299c71099acSThomas Graf {
300a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
301a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
302c71099acSThomas Graf }
303c71099acSThomas Graf 
3041da177e4SLinus Torvalds /*
305c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
3061da177e4SLinus Torvalds  */
3071da177e4SLinus Torvalds 
3088ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
3098ed67789SDaniel Lezcano 						    struct rt6_info *rt,
310b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
3111da177e4SLinus Torvalds 						    int oif,
312d420895eSYOSHIFUJI Hideaki 						    int flags)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
3151da177e4SLinus Torvalds 	struct rt6_info *sprt;
3161da177e4SLinus Torvalds 
317dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
318dd3abc4eSYOSHIFUJI Hideaki 		goto out;
319dd3abc4eSYOSHIFUJI Hideaki 
320d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
3211da177e4SLinus Torvalds 		struct net_device *dev = sprt->rt6i_dev;
322dd3abc4eSYOSHIFUJI Hideaki 
323dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
3241da177e4SLinus Torvalds 			if (dev->ifindex == oif)
3251da177e4SLinus Torvalds 				return sprt;
3261da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
3271da177e4SLinus Torvalds 				if (sprt->rt6i_idev == NULL ||
3281da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
329d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
3301da177e4SLinus Torvalds 						continue;
3311da177e4SLinus Torvalds 					if (local && (!oif ||
3321da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
3331da177e4SLinus Torvalds 						continue;
3341da177e4SLinus Torvalds 				}
3351da177e4SLinus Torvalds 				local = sprt;
3361da177e4SLinus Torvalds 			}
337dd3abc4eSYOSHIFUJI Hideaki 		} else {
338dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
339dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
340dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
341dd3abc4eSYOSHIFUJI Hideaki 		}
3421da177e4SLinus Torvalds 	}
3431da177e4SLinus Torvalds 
344dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
3451da177e4SLinus Torvalds 		if (local)
3461da177e4SLinus Torvalds 			return local;
3471da177e4SLinus Torvalds 
348d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
3498ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
3501da177e4SLinus Torvalds 	}
351dd3abc4eSYOSHIFUJI Hideaki out:
3521da177e4SLinus Torvalds 	return rt;
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
35527097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
35627097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
35727097255SYOSHIFUJI Hideaki {
35827097255SYOSHIFUJI Hideaki 	struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
35927097255SYOSHIFUJI Hideaki 	/*
36027097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
36127097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
36227097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
36327097255SYOSHIFUJI Hideaki 	 *
36427097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
36527097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
36627097255SYOSHIFUJI Hideaki 	 */
36727097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
36827097255SYOSHIFUJI Hideaki 		return;
36927097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
37027097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
37152e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
37227097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
37327097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
37427097255SYOSHIFUJI Hideaki 
37527097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
37627097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
37727097255SYOSHIFUJI Hideaki 
37827097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
37927097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
38027097255SYOSHIFUJI Hideaki 		ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
38127097255SYOSHIFUJI Hideaki 	} else
38227097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
38327097255SYOSHIFUJI Hideaki }
38427097255SYOSHIFUJI Hideaki #else
38527097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
38627097255SYOSHIFUJI Hideaki {
38727097255SYOSHIFUJI Hideaki }
38827097255SYOSHIFUJI Hideaki #endif
38927097255SYOSHIFUJI Hideaki 
3901da177e4SLinus Torvalds /*
391554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
3921da177e4SLinus Torvalds  */
393b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
3941da177e4SLinus Torvalds {
395554cfb7eSYOSHIFUJI Hideaki 	struct net_device *dev = rt->rt6i_dev;
396161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
397554cfb7eSYOSHIFUJI Hideaki 		return 2;
398161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
399161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
400161980f4SDavid S. Miller 		return 1;
401554cfb7eSYOSHIFUJI Hideaki 	return 0;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
404b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
4051da177e4SLinus Torvalds {
406554cfb7eSYOSHIFUJI Hideaki 	struct neighbour *neigh = rt->rt6i_nexthop;
407398bcbebSYOSHIFUJI Hideaki 	int m;
4084d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
4094d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
4104d0c5911SYOSHIFUJI Hideaki 		m = 1;
4114d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
4121da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
413554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
4144d0c5911SYOSHIFUJI Hideaki 			m = 2;
415398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
416398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
417398bcbebSYOSHIFUJI Hideaki 			m = 0;
418398bcbebSYOSHIFUJI Hideaki #endif
419398bcbebSYOSHIFUJI Hideaki 		else
420ea73ee23SYOSHIFUJI Hideaki 			m = 1;
4211da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
422398bcbebSYOSHIFUJI Hideaki 	} else
423398bcbebSYOSHIFUJI Hideaki 		m = 0;
424554cfb7eSYOSHIFUJI Hideaki 	return m;
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
427554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
428554cfb7eSYOSHIFUJI Hideaki 			   int strict)
429554cfb7eSYOSHIFUJI Hideaki {
4304d0c5911SYOSHIFUJI Hideaki 	int m, n;
4314d0c5911SYOSHIFUJI Hideaki 
4324d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
43377d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
434554cfb7eSYOSHIFUJI Hideaki 		return -1;
435ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
436ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
437ebacaaa0SYOSHIFUJI Hideaki #endif
4384d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
439557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
440554cfb7eSYOSHIFUJI Hideaki 		return -1;
441554cfb7eSYOSHIFUJI Hideaki 	return m;
442554cfb7eSYOSHIFUJI Hideaki }
443554cfb7eSYOSHIFUJI Hideaki 
444f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
445f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
446554cfb7eSYOSHIFUJI Hideaki {
447554cfb7eSYOSHIFUJI Hideaki 	int m;
448554cfb7eSYOSHIFUJI Hideaki 
449554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
450f11e6659SDavid S. Miller 		goto out;
451554cfb7eSYOSHIFUJI Hideaki 
452554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
453554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
454f11e6659SDavid S. Miller 		goto out;
455554cfb7eSYOSHIFUJI Hideaki 
456f11e6659SDavid S. Miller 	if (m > *mpri) {
457ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
45827097255SYOSHIFUJI Hideaki 			rt6_probe(match);
459f11e6659SDavid S. Miller 		*mpri = m;
460554cfb7eSYOSHIFUJI Hideaki 		match = rt;
461ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
46227097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
4631da177e4SLinus Torvalds 	}
464f11e6659SDavid S. Miller 
465f11e6659SDavid S. Miller out:
466f11e6659SDavid S. Miller 	return match;
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
469f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
470f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
471f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
472f11e6659SDavid S. Miller {
473f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
474f11e6659SDavid S. Miller 	int mpri = -1;
475f11e6659SDavid S. Miller 
476f11e6659SDavid S. Miller 	match = NULL;
477f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
478d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
479f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
480f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
481d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
482f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
483f11e6659SDavid S. Miller 
484f11e6659SDavid S. Miller 	return match;
485f11e6659SDavid S. Miller }
486f11e6659SDavid S. Miller 
487f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
488f11e6659SDavid S. Miller {
489f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
4908ed67789SDaniel Lezcano 	struct net *net;
491f11e6659SDavid S. Miller 
492f11e6659SDavid S. Miller 	RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
4930dc47877SHarvey Harrison 		  __func__, fn->leaf, oif);
494f11e6659SDavid S. Miller 
495f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
496f11e6659SDavid S. Miller 	if (!rt0)
497f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
498f11e6659SDavid S. Miller 
499f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
500f11e6659SDavid S. Miller 
501554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
502f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
503d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
504f11e6659SDavid S. Miller 
505554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
506f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
507f11e6659SDavid S. Miller 			next = fn->leaf;
508f11e6659SDavid S. Miller 
509f11e6659SDavid S. Miller 		if (next != rt0)
510f11e6659SDavid S. Miller 			fn->rr_ptr = next;
511554cfb7eSYOSHIFUJI Hideaki 	}
512554cfb7eSYOSHIFUJI Hideaki 
513f11e6659SDavid S. Miller 	RT6_TRACE("%s() => %p\n",
5140dc47877SHarvey Harrison 		  __func__, match);
515554cfb7eSYOSHIFUJI Hideaki 
516c346dca1SYOSHIFUJI Hideaki 	net = dev_net(rt0->rt6i_dev);
517a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds 
52070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
52170ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
522b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
52370ceb4f5SYOSHIFUJI Hideaki {
524c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
52570ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
52670ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
52770ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
5284bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
52970ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
53070ceb4f5SYOSHIFUJI Hideaki 
53170ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
53270ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
53370ceb4f5SYOSHIFUJI Hideaki 	}
53470ceb4f5SYOSHIFUJI Hideaki 
53570ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
53670ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
53770ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
53870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
53970ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
54070ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
54170ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
54270ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
54370ceb4f5SYOSHIFUJI Hideaki 		}
54470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
54570ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
54670ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
54770ceb4f5SYOSHIFUJI Hideaki 		}
54870ceb4f5SYOSHIFUJI Hideaki 	}
54970ceb4f5SYOSHIFUJI Hideaki 
55070ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
55170ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
5523933fc95SJens Rosenboom 		return -EINVAL;
55370ceb4f5SYOSHIFUJI Hideaki 
5544bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
55570ceb4f5SYOSHIFUJI Hideaki 
55670ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
55770ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
55870ceb4f5SYOSHIFUJI Hideaki 	else {
55970ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
56070ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
56170ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
56270ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
56370ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
56470ceb4f5SYOSHIFUJI Hideaki 	}
56570ceb4f5SYOSHIFUJI Hideaki 
566efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
567efa2cea0SDaniel Lezcano 				dev->ifindex);
56870ceb4f5SYOSHIFUJI Hideaki 
56970ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
570e0a1ad73SThomas Graf 		ip6_del_rt(rt);
57170ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
57270ceb4f5SYOSHIFUJI Hideaki 	}
57370ceb4f5SYOSHIFUJI Hideaki 
57470ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
575efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
57670ceb4f5SYOSHIFUJI Hideaki 					pref);
57770ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
57870ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
57970ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
58070ceb4f5SYOSHIFUJI Hideaki 
58170ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
5824bed72e4SYOSHIFUJI Hideaki 		if (!addrconf_finite_timeout(lifetime)) {
58370ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags &= ~RTF_EXPIRES;
58470ceb4f5SYOSHIFUJI Hideaki 		} else {
58570ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_expires = jiffies + HZ * lifetime;
58670ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_EXPIRES;
58770ceb4f5SYOSHIFUJI Hideaki 		}
588d8d1f30bSChangli Gao 		dst_release(&rt->dst);
58970ceb4f5SYOSHIFUJI Hideaki 	}
59070ceb4f5SYOSHIFUJI Hideaki 	return 0;
59170ceb4f5SYOSHIFUJI Hideaki }
59270ceb4f5SYOSHIFUJI Hideaki #endif
59370ceb4f5SYOSHIFUJI Hideaki 
5948ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
595982f56f3SYOSHIFUJI Hideaki do { \
5968ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
597982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
598e0eda7bbSVille Nuorvala 		while (1) { \
599982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
600c71099acSThomas Graf 				goto out; \
601982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
602982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
6038bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
604982f56f3SYOSHIFUJI Hideaki 			else \
605982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
606c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
607c71099acSThomas Graf 				goto restart; \
608c71099acSThomas Graf 		} \
609982f56f3SYOSHIFUJI Hideaki 	} \
610982f56f3SYOSHIFUJI Hideaki } while(0)
611c71099acSThomas Graf 
6128ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
6138ed67789SDaniel Lezcano 					     struct fib6_table *table,
6144c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
6151da177e4SLinus Torvalds {
6161da177e4SLinus Torvalds 	struct fib6_node *fn;
6171da177e4SLinus Torvalds 	struct rt6_info *rt;
6181da177e4SLinus Torvalds 
619c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
6204c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
621c71099acSThomas Graf restart:
622c71099acSThomas Graf 	rt = fn->leaf;
6234c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
6244c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
625c71099acSThomas Graf out:
626d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
627c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
6281da177e4SLinus Torvalds 	return rt;
629c71099acSThomas Graf 
630c71099acSThomas Graf }
631c71099acSThomas Graf 
6329acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
6339acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
634c71099acSThomas Graf {
6354c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
6364c9483b2SDavid S. Miller 		.flowi6_oif = oif,
6374c9483b2SDavid S. Miller 		.daddr = *daddr,
638c71099acSThomas Graf 	};
639c71099acSThomas Graf 	struct dst_entry *dst;
64077d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
641c71099acSThomas Graf 
642adaa70bbSThomas Graf 	if (saddr) {
6434c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
644adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
645adaa70bbSThomas Graf 	}
646adaa70bbSThomas Graf 
6474c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
648c71099acSThomas Graf 	if (dst->error == 0)
649c71099acSThomas Graf 		return (struct rt6_info *) dst;
650c71099acSThomas Graf 
651c71099acSThomas Graf 	dst_release(dst);
652c71099acSThomas Graf 
6531da177e4SLinus Torvalds 	return NULL;
6541da177e4SLinus Torvalds }
6551da177e4SLinus Torvalds 
6567159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
6577159039aSYOSHIFUJI Hideaki 
658c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
6591da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
6601da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
6611da177e4SLinus Torvalds    be destroyed.
6621da177e4SLinus Torvalds  */
6631da177e4SLinus Torvalds 
66486872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
6651da177e4SLinus Torvalds {
6661da177e4SLinus Torvalds 	int err;
667c71099acSThomas Graf 	struct fib6_table *table;
6681da177e4SLinus Torvalds 
669c71099acSThomas Graf 	table = rt->rt6i_table;
670c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
67186872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
672c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	return err;
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
67740e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
67840e22e8fSThomas Graf {
6794d1169c1SDenis V. Lunev 	struct nl_info info = {
680c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
6814d1169c1SDenis V. Lunev 	};
682528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
68340e22e8fSThomas Graf }
68440e22e8fSThomas Graf 
685b71d1d42SEric Dumazet static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, const struct in6_addr *daddr,
686b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	struct rt6_info *rt;
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	/*
6911da177e4SLinus Torvalds 	 *	Clone the route.
6921da177e4SLinus Torvalds 	 */
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	rt = ip6_rt_copy(ort);
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	if (rt) {
69714deae41SDavid S. Miller 		struct neighbour *neigh;
69814deae41SDavid S. Miller 		int attempts = !in_softirq();
69914deae41SDavid S. Miller 
70058c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags&RTF_GATEWAY)) {
70158c4fb86SYOSHIFUJI Hideaki 			if (rt->rt6i_dst.plen != 128 &&
70258c4fb86SYOSHIFUJI Hideaki 			    ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
70358c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
7041da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_gateway, daddr);
70558c4fb86SYOSHIFUJI Hideaki 		}
7061da177e4SLinus Torvalds 
70758c4fb86SYOSHIFUJI Hideaki 		ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
7081da177e4SLinus Torvalds 		rt->rt6i_dst.plen = 128;
7091da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
710d8d1f30bSChangli Gao 		rt->dst.flags |= DST_HOST;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
7131da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
7141da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
7151da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
7161da177e4SLinus Torvalds 		}
7171da177e4SLinus Torvalds #endif
7181da177e4SLinus Torvalds 
71914deae41SDavid S. Miller 	retry:
72014deae41SDavid S. Miller 		neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
72114deae41SDavid S. Miller 		if (IS_ERR(neigh)) {
72214deae41SDavid S. Miller 			struct net *net = dev_net(rt->rt6i_dev);
72314deae41SDavid S. Miller 			int saved_rt_min_interval =
72414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval;
72514deae41SDavid S. Miller 			int saved_rt_elasticity =
72614deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity;
72714deae41SDavid S. Miller 
72814deae41SDavid S. Miller 			if (attempts-- > 0) {
72914deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
73014deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
73114deae41SDavid S. Miller 
73286393e52SAlexey Dobriyan 				ip6_dst_gc(&net->ipv6.ip6_dst_ops);
73314deae41SDavid S. Miller 
73414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity =
73514deae41SDavid S. Miller 					saved_rt_elasticity;
73614deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval =
73714deae41SDavid S. Miller 					saved_rt_min_interval;
73814deae41SDavid S. Miller 				goto retry;
73914deae41SDavid S. Miller 			}
74014deae41SDavid S. Miller 
74114deae41SDavid S. Miller 			if (net_ratelimit())
74214deae41SDavid S. Miller 				printk(KERN_WARNING
7437e1b33e5SUlrich Weber 				       "ipv6: Neighbour table overflow.\n");
744d8d1f30bSChangli Gao 			dst_free(&rt->dst);
74514deae41SDavid S. Miller 			return NULL;
74614deae41SDavid S. Miller 		}
74714deae41SDavid S. Miller 		rt->rt6i_nexthop = neigh;
7481da177e4SLinus Torvalds 
74995a9a5baSYOSHIFUJI Hideaki 	}
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	return rt;
7521da177e4SLinus Torvalds }
75395a9a5baSYOSHIFUJI Hideaki 
754b71d1d42SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, const struct in6_addr *daddr)
755299d9939SYOSHIFUJI Hideaki {
756299d9939SYOSHIFUJI Hideaki 	struct rt6_info *rt = ip6_rt_copy(ort);
757299d9939SYOSHIFUJI Hideaki 	if (rt) {
758299d9939SYOSHIFUJI Hideaki 		ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
759299d9939SYOSHIFUJI Hideaki 		rt->rt6i_dst.plen = 128;
760299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
761d8d1f30bSChangli Gao 		rt->dst.flags |= DST_HOST;
762299d9939SYOSHIFUJI Hideaki 		rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
763299d9939SYOSHIFUJI Hideaki 	}
764299d9939SYOSHIFUJI Hideaki 	return rt;
765299d9939SYOSHIFUJI Hideaki }
766299d9939SYOSHIFUJI Hideaki 
7678ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
7684c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
7691da177e4SLinus Torvalds {
7701da177e4SLinus Torvalds 	struct fib6_node *fn;
771519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
772c71099acSThomas Graf 	int strict = 0;
7731da177e4SLinus Torvalds 	int attempts = 3;
774519fbd87SYOSHIFUJI Hideaki 	int err;
77553b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
7761da177e4SLinus Torvalds 
77777d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds relookup:
780c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7811da177e4SLinus Torvalds 
7828238dd06SYOSHIFUJI Hideaki restart_2:
7834c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds restart:
7864acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
7878ed67789SDaniel Lezcano 
7884c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
7898ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
7908238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
7911da177e4SLinus Torvalds 		goto out;
7921da177e4SLinus Torvalds 
793d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
794c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7951da177e4SLinus Torvalds 
796519fbd87SYOSHIFUJI Hideaki 	if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
7974c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
7987343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
7994c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
8007343ff31SDavid S. Miller 	else
8017343ff31SDavid S. Miller 		goto out2;
8021da177e4SLinus Torvalds 
803d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8048ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
8051da177e4SLinus Torvalds 
806d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
807e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
80840e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
809e40cf353SYOSHIFUJI Hideaki 		if (!err)
810e40cf353SYOSHIFUJI Hideaki 			goto out2;
811e40cf353SYOSHIFUJI Hideaki 	}
812e40cf353SYOSHIFUJI Hideaki 
813e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
8141da177e4SLinus Torvalds 		goto out2;
8151da177e4SLinus Torvalds 
816519fbd87SYOSHIFUJI Hideaki 	/*
817c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
818519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
8191da177e4SLinus Torvalds 	 */
820d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8211da177e4SLinus Torvalds 	goto relookup;
822e40cf353SYOSHIFUJI Hideaki 
823519fbd87SYOSHIFUJI Hideaki out:
8248238dd06SYOSHIFUJI Hideaki 	if (reachable) {
8258238dd06SYOSHIFUJI Hideaki 		reachable = 0;
8268238dd06SYOSHIFUJI Hideaki 		goto restart_2;
8278238dd06SYOSHIFUJI Hideaki 	}
828d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
829c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8301da177e4SLinus Torvalds out2:
831d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
832d8d1f30bSChangli Gao 	rt->dst.__use++;
833c71099acSThomas Graf 
834c71099acSThomas Graf 	return rt;
835c71099acSThomas Graf }
836c71099acSThomas Graf 
8378ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
8384c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
8394acad72dSPavel Emelyanov {
8404c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
8414acad72dSPavel Emelyanov }
8424acad72dSPavel Emelyanov 
843c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
844c71099acSThomas Graf {
845b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
846c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
847adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
8484c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
8494c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
8504c9483b2SDavid S. Miller 		.daddr = iph->daddr,
8514c9483b2SDavid S. Miller 		.saddr = iph->saddr,
8524c9483b2SDavid S. Miller 		.flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
8534c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
8544c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
855c71099acSThomas Graf 	};
856adaa70bbSThomas Graf 
8571d6e55f1SThomas Goff 	if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
858adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
859c71099acSThomas Graf 
8604c9483b2SDavid S. Miller 	skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input));
861c71099acSThomas Graf }
862c71099acSThomas Graf 
8638ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
8644c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
865c71099acSThomas Graf {
8664c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
867c71099acSThomas Graf }
868c71099acSThomas Graf 
8699c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
8704c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
871c71099acSThomas Graf {
872c71099acSThomas Graf 	int flags = 0;
873c71099acSThomas Graf 
8744c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
87577d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
876c71099acSThomas Graf 
8774c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
878adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
8790c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
8800c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
881adaa70bbSThomas Graf 
8824c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
8857159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
8861da177e4SLinus Torvalds 
8872774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
88814e50e57SDavid S. Miller {
8895c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
89014e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
89114e50e57SDavid S. Miller 
8925c1e6aa3SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
89314e50e57SDavid S. Miller 	if (rt) {
894*cf911662SDavid S. Miller 		memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
895*cf911662SDavid S. Miller 
896d8d1f30bSChangli Gao 		new = &rt->dst;
89714e50e57SDavid S. Miller 
89814e50e57SDavid S. Miller 		new->__use = 1;
899352e512cSHerbert Xu 		new->input = dst_discard;
900352e512cSHerbert Xu 		new->output = dst_discard;
90114e50e57SDavid S. Miller 
902defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
90314e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
90414e50e57SDavid S. Miller 		if (rt->rt6i_idev)
90514e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
90614e50e57SDavid S. Miller 		rt->rt6i_expires = 0;
90714e50e57SDavid S. Miller 
90814e50e57SDavid S. Miller 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
90914e50e57SDavid S. Miller 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
91014e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
91114e50e57SDavid S. Miller 
91214e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
91314e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
91414e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
91514e50e57SDavid S. Miller #endif
91614e50e57SDavid S. Miller 
91714e50e57SDavid S. Miller 		dst_free(new);
91814e50e57SDavid S. Miller 	}
91914e50e57SDavid S. Miller 
92069ead7afSDavid S. Miller 	dst_release(dst_orig);
92169ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
92214e50e57SDavid S. Miller }
92314e50e57SDavid S. Miller 
9241da177e4SLinus Torvalds /*
9251da177e4SLinus Torvalds  *	Destination cache support functions
9261da177e4SLinus Torvalds  */
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
9291da177e4SLinus Torvalds {
9301da177e4SLinus Torvalds 	struct rt6_info *rt;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
9331da177e4SLinus Torvalds 
9346431cbc2SDavid S. Miller 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
9356431cbc2SDavid S. Miller 		if (rt->rt6i_peer_genid != rt6_peer_genid()) {
9366431cbc2SDavid S. Miller 			if (!rt->rt6i_peer)
9376431cbc2SDavid S. Miller 				rt6_bind_peer(rt, 0);
9386431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
9396431cbc2SDavid S. Miller 		}
9401da177e4SLinus Torvalds 		return dst;
9416431cbc2SDavid S. Miller 	}
9421da177e4SLinus Torvalds 	return NULL;
9431da177e4SLinus Torvalds }
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
9461da177e4SLinus Torvalds {
9471da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	if (rt) {
95054c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
95154c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
952e0a1ad73SThomas Graf 				ip6_del_rt(rt);
95354c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
9541da177e4SLinus Torvalds 			}
95554c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
95654c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
95754c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
95854c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
95954c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
96054c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
9641da177e4SLinus Torvalds {
9651da177e4SLinus Torvalds 	struct rt6_info *rt;
9661da177e4SLinus Torvalds 
9673ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
9681da177e4SLinus Torvalds 
969adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
9701da177e4SLinus Torvalds 	if (rt) {
9711da177e4SLinus Torvalds 		if (rt->rt6i_flags&RTF_CACHE) {
972d8d1f30bSChangli Gao 			dst_set_expires(&rt->dst, 0);
9731da177e4SLinus Torvalds 			rt->rt6i_flags |= RTF_EXPIRES;
9741da177e4SLinus Torvalds 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
9751da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
9761da177e4SLinus Torvalds 	}
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
9841da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
9851da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
986defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
9871da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
988defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
989defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
9901da177e4SLinus Torvalds 		}
991defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
9921da177e4SLinus Torvalds 	}
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
9950dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
9961da177e4SLinus Torvalds {
9970dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
9980dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
9990dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
10000dbaee3bSDavid S. Miller 
10011da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
10021da177e4SLinus Torvalds 
10035578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
10045578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	/*
10071da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
10081da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
10091da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
10101da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
10111da177e4SLinus Torvalds 	 */
10121da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
10131da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
10141da177e4SLinus Torvalds 	return mtu;
10151da177e4SLinus Torvalds }
10161da177e4SLinus Torvalds 
1017d33e4553SDavid S. Miller static unsigned int ip6_default_mtu(const struct dst_entry *dst)
1018d33e4553SDavid S. Miller {
1019d33e4553SDavid S. Miller 	unsigned int mtu = IPV6_MIN_MTU;
1020d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1021d33e4553SDavid S. Miller 
1022d33e4553SDavid S. Miller 	rcu_read_lock();
1023d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1024d33e4553SDavid S. Miller 	if (idev)
1025d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1026d33e4553SDavid S. Miller 	rcu_read_unlock();
1027d33e4553SDavid S. Miller 
1028d33e4553SDavid S. Miller 	return mtu;
1029d33e4553SDavid S. Miller }
1030d33e4553SDavid S. Miller 
10313b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
10323b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
10335d0bbeebSThomas Graf 
10343b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
10351da177e4SLinus Torvalds 				  struct neighbour *neigh,
10369acd9f3aSYOSHIFUJI Hideaki 				  const struct in6_addr *addr)
10371da177e4SLinus Torvalds {
10381da177e4SLinus Torvalds 	struct rt6_info *rt;
10391da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1040c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	if (unlikely(idev == NULL))
10431da177e4SLinus Torvalds 		return NULL;
10441da177e4SLinus Torvalds 
10455c1e6aa3SDavid S. Miller 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev);
10461da177e4SLinus Torvalds 	if (unlikely(rt == NULL)) {
10471da177e4SLinus Torvalds 		in6_dev_put(idev);
10481da177e4SLinus Torvalds 		goto out;
10491da177e4SLinus Torvalds 	}
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	if (neigh)
10521da177e4SLinus Torvalds 		neigh_hold(neigh);
105314deae41SDavid S. Miller 	else {
10541da177e4SLinus Torvalds 		neigh = ndisc_get_neigh(dev, addr);
105514deae41SDavid S. Miller 		if (IS_ERR(neigh))
105614deae41SDavid S. Miller 			neigh = NULL;
105714deae41SDavid S. Miller 	}
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	rt->rt6i_idev     = idev;
10601da177e4SLinus Torvalds 	rt->rt6i_nexthop  = neigh;
1061d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
1062defb3519SDavid S. Miller 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
1063d8d1f30bSChangli Gao 	rt->dst.output  = ip6_output;
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds #if 0	/* there's no chance to use these for ndisc */
1066d8d1f30bSChangli Gao 	rt->dst.flags   = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
10671da177e4SLinus Torvalds 				? DST_HOST
10681da177e4SLinus Torvalds 				: 0;
10691da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
10701da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
10711da177e4SLinus Torvalds #endif
10721da177e4SLinus Torvalds 
10733b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1074d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1075d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
10763b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
10771da177e4SLinus Torvalds 
10785578689aSDaniel Lezcano 	fib6_force_start_gc(net);
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds out:
1081d8d1f30bSChangli Gao 	return &rt->dst;
10821da177e4SLinus Torvalds }
10831da177e4SLinus Torvalds 
10843d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
10851da177e4SLinus Torvalds {
1086e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
10873d0f24a7SStephen Hemminger 	int more = 0;
10881da177e4SLinus Torvalds 
10893b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
10903b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
10915d0bbeebSThomas Graf 
10921da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
10931da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
10941da177e4SLinus Torvalds 			*pprev = dst->next;
10951da177e4SLinus Torvalds 			dst_free(dst);
10961da177e4SLinus Torvalds 		} else {
10971da177e4SLinus Torvalds 			pprev = &dst->next;
10983d0f24a7SStephen Hemminger 			++more;
10991da177e4SLinus Torvalds 		}
11001da177e4SLinus Torvalds 	}
11011da177e4SLinus Torvalds 
11023b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
11035d0bbeebSThomas Graf 
11043d0f24a7SStephen Hemminger 	return more;
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11071e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
11081e493d19SDavid S. Miller 			    void *arg)
11091e493d19SDavid S. Miller {
11101e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
11111e493d19SDavid S. Miller 
11121e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
11131e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
11141e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
11151e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
11161e493d19SDavid S. Miller 		if (func(rt, arg)) {
11171e493d19SDavid S. Miller 			*pprev = dst->next;
11181e493d19SDavid S. Miller 			dst_free(dst);
11191e493d19SDavid S. Miller 		} else {
11201e493d19SDavid S. Miller 			pprev = &dst->next;
11211e493d19SDavid S. Miller 		}
11221e493d19SDavid S. Miller 	}
11231e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
11241e493d19SDavid S. Miller }
11251e493d19SDavid S. Miller 
1126569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
11271da177e4SLinus Torvalds {
11281da177e4SLinus Torvalds 	unsigned long now = jiffies;
112986393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
11307019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
11317019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
11327019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
11337019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
11347019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1135fc66f95cSEric Dumazet 	int entries;
11361da177e4SLinus Torvalds 
1137fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
11387019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1139fc66f95cSEric Dumazet 	    entries <= rt_max_size)
11401da177e4SLinus Torvalds 		goto out;
11411da177e4SLinus Torvalds 
11426891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
11436891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
11446891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1145fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1146fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
11477019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
11481da177e4SLinus Torvalds out:
11497019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1150fc66f95cSEric Dumazet 	return entries > rt_max_size;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
11541da177e4SLinus Torvalds    but results in cleaner routing tables.
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds    Remove it only when all the things will work!
11571da177e4SLinus Torvalds  */
11581da177e4SLinus Torvalds 
11596b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
11601da177e4SLinus Torvalds {
11615170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1162a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
11636b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1164c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1165c68f24ccSEric Dumazet 
1166c68f24ccSEric Dumazet 		rcu_read_lock();
1167c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1168c68f24ccSEric Dumazet 		if (idev)
11691da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1170c68f24ccSEric Dumazet 		else
117153b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1172c68f24ccSEric Dumazet 		rcu_read_unlock();
11731da177e4SLinus Torvalds 	}
11741da177e4SLinus Torvalds 	return hoplimit;
11751da177e4SLinus Torvalds }
1176abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds /*
11791da177e4SLinus Torvalds  *
11801da177e4SLinus Torvalds  */
11811da177e4SLinus Torvalds 
118286872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
11831da177e4SLinus Torvalds {
11841da177e4SLinus Torvalds 	int err;
11855578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
11861da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
11871da177e4SLinus Torvalds 	struct net_device *dev = NULL;
11881da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1189c71099acSThomas Graf 	struct fib6_table *table;
11901da177e4SLinus Torvalds 	int addr_type;
11911da177e4SLinus Torvalds 
119286872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
11931da177e4SLinus Torvalds 		return -EINVAL;
11941da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
119586872cb5SThomas Graf 	if (cfg->fc_src_len)
11961da177e4SLinus Torvalds 		return -EINVAL;
11971da177e4SLinus Torvalds #endif
119886872cb5SThomas Graf 	if (cfg->fc_ifindex) {
11991da177e4SLinus Torvalds 		err = -ENODEV;
12005578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
12011da177e4SLinus Torvalds 		if (!dev)
12021da177e4SLinus Torvalds 			goto out;
12031da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
12041da177e4SLinus Torvalds 		if (!idev)
12051da177e4SLinus Torvalds 			goto out;
12061da177e4SLinus Torvalds 	}
12071da177e4SLinus Torvalds 
120886872cb5SThomas Graf 	if (cfg->fc_metric == 0)
120986872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
12101da177e4SLinus Torvalds 
12115578689aSDaniel Lezcano 	table = fib6_new_table(net, cfg->fc_table);
1212c71099acSThomas Graf 	if (table == NULL) {
1213c71099acSThomas Graf 		err = -ENOBUFS;
1214c71099acSThomas Graf 		goto out;
1215c71099acSThomas Graf 	}
1216c71099acSThomas Graf 
12175c1e6aa3SDavid S. Miller 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL);
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 	if (rt == NULL) {
12201da177e4SLinus Torvalds 		err = -ENOMEM;
12211da177e4SLinus Torvalds 		goto out;
12221da177e4SLinus Torvalds 	}
12231da177e4SLinus Torvalds 
1224d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
12256f704992SYOSHIFUJI Hideaki 	rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
12266f704992SYOSHIFUJI Hideaki 				jiffies + clock_t_to_jiffies(cfg->fc_expires) :
12276f704992SYOSHIFUJI Hideaki 				0;
12281da177e4SLinus Torvalds 
122986872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
123086872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
123186872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
123286872cb5SThomas Graf 
123386872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1236d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1237ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1238ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
12391da177e4SLinus Torvalds 	else
1240d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
12411da177e4SLinus Torvalds 
1242d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
12431da177e4SLinus Torvalds 
124486872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
124586872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
12461da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
1247d8d1f30bSChangli Gao 	       rt->dst.flags = DST_HOST;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
125086872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
125186872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
12521da177e4SLinus Torvalds #endif
12531da177e4SLinus Torvalds 
125486872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
12571da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
12581da177e4SLinus Torvalds 	 */
125986872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
1260ab79ad14SMaciej Żenczykowski 	    (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1261ab79ad14SMaciej Żenczykowski 					      && !(cfg->fc_flags&RTF_LOCAL))) {
12621da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
12635578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
12641da177e4SLinus Torvalds 			if (dev) {
12651da177e4SLinus Torvalds 				dev_put(dev);
12661da177e4SLinus Torvalds 				in6_dev_put(idev);
12671da177e4SLinus Torvalds 			}
12685578689aSDaniel Lezcano 			dev = net->loopback_dev;
12691da177e4SLinus Torvalds 			dev_hold(dev);
12701da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
12711da177e4SLinus Torvalds 			if (!idev) {
12721da177e4SLinus Torvalds 				err = -ENODEV;
12731da177e4SLinus Torvalds 				goto out;
12741da177e4SLinus Torvalds 			}
12751da177e4SLinus Torvalds 		}
1276d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1277d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
1278d8d1f30bSChangli Gao 		rt->dst.error = -ENETUNREACH;
12791da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
12801da177e4SLinus Torvalds 		goto install_route;
12811da177e4SLinus Torvalds 	}
12821da177e4SLinus Torvalds 
128386872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1284b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
12851da177e4SLinus Torvalds 		int gwa_type;
12861da177e4SLinus Torvalds 
128786872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
128886872cb5SThomas Graf 		ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
12891da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
12921da177e4SLinus Torvalds 			struct rt6_info *grt;
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
12951da177e4SLinus Torvalds 			   addresses as nexthop address.
12961da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
12971da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
12981da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
12991da177e4SLinus Torvalds 			   some exceptions. --ANK
13001da177e4SLinus Torvalds 			 */
13011da177e4SLinus Torvalds 			err = -EINVAL;
13021da177e4SLinus Torvalds 			if (!(gwa_type&IPV6_ADDR_UNICAST))
13031da177e4SLinus Torvalds 				goto out;
13041da177e4SLinus Torvalds 
13055578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
13081da177e4SLinus Torvalds 			if (grt == NULL)
13091da177e4SLinus Torvalds 				goto out;
13101da177e4SLinus Torvalds 			if (dev) {
13111da177e4SLinus Torvalds 				if (dev != grt->rt6i_dev) {
1312d8d1f30bSChangli Gao 					dst_release(&grt->dst);
13131da177e4SLinus Torvalds 					goto out;
13141da177e4SLinus Torvalds 				}
13151da177e4SLinus Torvalds 			} else {
13161da177e4SLinus Torvalds 				dev = grt->rt6i_dev;
13171da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
13181da177e4SLinus Torvalds 				dev_hold(dev);
13191da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
13201da177e4SLinus Torvalds 			}
13211da177e4SLinus Torvalds 			if (!(grt->rt6i_flags&RTF_GATEWAY))
13221da177e4SLinus Torvalds 				err = 0;
1323d8d1f30bSChangli Gao 			dst_release(&grt->dst);
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 			if (err)
13261da177e4SLinus Torvalds 				goto out;
13271da177e4SLinus Torvalds 		}
13281da177e4SLinus Torvalds 		err = -EINVAL;
13291da177e4SLinus Torvalds 		if (dev == NULL || (dev->flags&IFF_LOOPBACK))
13301da177e4SLinus Torvalds 			goto out;
13311da177e4SLinus Torvalds 	}
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 	err = -ENODEV;
13341da177e4SLinus Torvalds 	if (dev == NULL)
13351da177e4SLinus Torvalds 		goto out;
13361da177e4SLinus Torvalds 
1337c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1338c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1339c3968a85SDaniel Walter 			err = -EINVAL;
1340c3968a85SDaniel Walter 			goto out;
1341c3968a85SDaniel Walter 		}
1342c3968a85SDaniel Walter 		ipv6_addr_copy(&rt->rt6i_prefsrc.addr, &cfg->fc_prefsrc);
1343c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1344c3968a85SDaniel Walter 	} else
1345c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1346c3968a85SDaniel Walter 
134786872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
13481da177e4SLinus Torvalds 		rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
13491da177e4SLinus Torvalds 		if (IS_ERR(rt->rt6i_nexthop)) {
13501da177e4SLinus Torvalds 			err = PTR_ERR(rt->rt6i_nexthop);
13511da177e4SLinus Torvalds 			rt->rt6i_nexthop = NULL;
13521da177e4SLinus Torvalds 			goto out;
13531da177e4SLinus Torvalds 		}
13541da177e4SLinus Torvalds 	}
13551da177e4SLinus Torvalds 
135686872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds install_route:
135986872cb5SThomas Graf 	if (cfg->fc_mx) {
136086872cb5SThomas Graf 		struct nlattr *nla;
136186872cb5SThomas Graf 		int remaining;
13621da177e4SLinus Torvalds 
136386872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
13648f4c1f9bSThomas Graf 			int type = nla_type(nla);
136586872cb5SThomas Graf 
136686872cb5SThomas Graf 			if (type) {
136786872cb5SThomas Graf 				if (type > RTAX_MAX) {
13681da177e4SLinus Torvalds 					err = -EINVAL;
13691da177e4SLinus Torvalds 					goto out;
13701da177e4SLinus Torvalds 				}
137186872cb5SThomas Graf 
1372defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
13731da177e4SLinus Torvalds 			}
13741da177e4SLinus Torvalds 		}
13751da177e4SLinus Torvalds 	}
13761da177e4SLinus Torvalds 
1377d8d1f30bSChangli Gao 	rt->dst.dev = dev;
13781da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1379c71099acSThomas Graf 	rt->rt6i_table = table;
138063152fc0SDaniel Lezcano 
1381c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
138263152fc0SDaniel Lezcano 
138386872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds out:
13861da177e4SLinus Torvalds 	if (dev)
13871da177e4SLinus Torvalds 		dev_put(dev);
13881da177e4SLinus Torvalds 	if (idev)
13891da177e4SLinus Torvalds 		in6_dev_put(idev);
13901da177e4SLinus Torvalds 	if (rt)
1391d8d1f30bSChangli Gao 		dst_free(&rt->dst);
13921da177e4SLinus Torvalds 	return err;
13931da177e4SLinus Torvalds }
13941da177e4SLinus Torvalds 
139586872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
13961da177e4SLinus Torvalds {
13971da177e4SLinus Torvalds 	int err;
1398c71099acSThomas Graf 	struct fib6_table *table;
1399c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(rt->rt6i_dev);
14001da177e4SLinus Torvalds 
14018ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry)
14026c813a72SPatrick McHardy 		return -ENOENT;
14036c813a72SPatrick McHardy 
1404c71099acSThomas Graf 	table = rt->rt6i_table;
1405c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
14061da177e4SLinus Torvalds 
140786872cb5SThomas Graf 	err = fib6_del(rt, info);
1408d8d1f30bSChangli Gao 	dst_release(&rt->dst);
14091da177e4SLinus Torvalds 
1410c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 	return err;
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
1415e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1416e0a1ad73SThomas Graf {
14174d1169c1SDenis V. Lunev 	struct nl_info info = {
1418c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
14194d1169c1SDenis V. Lunev 	};
1420528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1421e0a1ad73SThomas Graf }
1422e0a1ad73SThomas Graf 
142386872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
14241da177e4SLinus Torvalds {
1425c71099acSThomas Graf 	struct fib6_table *table;
14261da177e4SLinus Torvalds 	struct fib6_node *fn;
14271da177e4SLinus Torvalds 	struct rt6_info *rt;
14281da177e4SLinus Torvalds 	int err = -ESRCH;
14291da177e4SLinus Torvalds 
14305578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
1431c71099acSThomas Graf 	if (table == NULL)
1432c71099acSThomas Graf 		return err;
14331da177e4SLinus Torvalds 
1434c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1435c71099acSThomas Graf 
1436c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
143786872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
143886872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	if (fn) {
1441d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
144286872cb5SThomas Graf 			if (cfg->fc_ifindex &&
14431da177e4SLinus Torvalds 			    (rt->rt6i_dev == NULL ||
144486872cb5SThomas Graf 			     rt->rt6i_dev->ifindex != cfg->fc_ifindex))
14451da177e4SLinus Torvalds 				continue;
144686872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
144786872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
14481da177e4SLinus Torvalds 				continue;
144986872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
14501da177e4SLinus Torvalds 				continue;
1451d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1452c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
14531da177e4SLinus Torvalds 
145486872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
14551da177e4SLinus Torvalds 		}
14561da177e4SLinus Torvalds 	}
1457c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 	return err;
14601da177e4SLinus Torvalds }
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds /*
14631da177e4SLinus Torvalds  *	Handle redirects
14641da177e4SLinus Torvalds  */
1465a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi {
14664c9483b2SDavid S. Miller 	struct flowi6 fl6;
1467a6279458SYOSHIFUJI Hideaki 	struct in6_addr gateway;
1468a6279458SYOSHIFUJI Hideaki };
14691da177e4SLinus Torvalds 
14708ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net,
14718ed67789SDaniel Lezcano 					     struct fib6_table *table,
14724c9483b2SDavid S. Miller 					     struct flowi6 *fl6,
1473a6279458SYOSHIFUJI Hideaki 					     int flags)
1474a6279458SYOSHIFUJI Hideaki {
14754c9483b2SDavid S. Miller 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1476a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt;
1477a6279458SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1478c71099acSThomas Graf 
1479e843b9e1SYOSHIFUJI Hideaki 	/*
1480e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1481e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1482e843b9e1SYOSHIFUJI Hideaki 	 *
1483e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1484e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1485e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1486e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1487e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1488e843b9e1SYOSHIFUJI Hideaki 	 */
14891da177e4SLinus Torvalds 
1490c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
14914c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1492e843b9e1SYOSHIFUJI Hideaki restart:
1493d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
14941da177e4SLinus Torvalds 		/*
14951da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
14961da177e4SLinus Torvalds 		 *
14971da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
14981da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
14991da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
15001da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
15011da177e4SLinus Torvalds 		 */
1502e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1503e843b9e1SYOSHIFUJI Hideaki 			continue;
15041da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1505e843b9e1SYOSHIFUJI Hideaki 			continue;
15064c9483b2SDavid S. Miller 		if (fl6->flowi6_oif != rt->rt6i_dev->ifindex)
1507e843b9e1SYOSHIFUJI Hideaki 			continue;
1508a6279458SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1509e843b9e1SYOSHIFUJI Hideaki 			continue;
1510e843b9e1SYOSHIFUJI Hideaki 		break;
1511e843b9e1SYOSHIFUJI Hideaki 	}
1512a6279458SYOSHIFUJI Hideaki 
1513cb15d9c2SYOSHIFUJI Hideaki 	if (!rt)
15148ed67789SDaniel Lezcano 		rt = net->ipv6.ip6_null_entry;
15154c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
1516cb15d9c2SYOSHIFUJI Hideaki out:
1517d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
1518a6279458SYOSHIFUJI Hideaki 
1519c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
15201da177e4SLinus Torvalds 
1521a6279458SYOSHIFUJI Hideaki 	return rt;
1522a6279458SYOSHIFUJI Hideaki };
1523a6279458SYOSHIFUJI Hideaki 
1524b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1525b71d1d42SEric Dumazet 					   const struct in6_addr *src,
1526b71d1d42SEric Dumazet 					   const struct in6_addr *gateway,
1527a6279458SYOSHIFUJI Hideaki 					   struct net_device *dev)
1528a6279458SYOSHIFUJI Hideaki {
1529adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1530c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
1531a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi rdfl = {
15324c9483b2SDavid S. Miller 		.fl6 = {
15334c9483b2SDavid S. Miller 			.flowi6_oif = dev->ifindex,
15344c9483b2SDavid S. Miller 			.daddr = *dest,
15354c9483b2SDavid S. Miller 			.saddr = *src,
1536a6279458SYOSHIFUJI Hideaki 		},
1537a6279458SYOSHIFUJI Hideaki 	};
1538adaa70bbSThomas Graf 
153986c36ce4SBrian Haley 	ipv6_addr_copy(&rdfl.gateway, gateway);
154086c36ce4SBrian Haley 
1541adaa70bbSThomas Graf 	if (rt6_need_strict(dest))
1542adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
1543a6279458SYOSHIFUJI Hideaki 
15444c9483b2SDavid S. Miller 	return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
154558f09b78SDaniel Lezcano 						   flags, __ip6_route_redirect);
1546a6279458SYOSHIFUJI Hideaki }
1547a6279458SYOSHIFUJI Hideaki 
1548b71d1d42SEric Dumazet void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1549b71d1d42SEric Dumazet 		  const struct in6_addr *saddr,
1550a6279458SYOSHIFUJI Hideaki 		  struct neighbour *neigh, u8 *lladdr, int on_link)
1551a6279458SYOSHIFUJI Hideaki {
1552a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt = NULL;
1553a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1554c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(neigh->dev);
1555a6279458SYOSHIFUJI Hideaki 
1556a6279458SYOSHIFUJI Hideaki 	rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1557a6279458SYOSHIFUJI Hideaki 
15588ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry) {
15591da177e4SLinus Torvalds 		if (net_ratelimit())
15601da177e4SLinus Torvalds 			printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
15611da177e4SLinus Torvalds 			       "for redirect target\n");
1562a6279458SYOSHIFUJI Hideaki 		goto out;
15631da177e4SLinus Torvalds 	}
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 	/*
15661da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
15671da177e4SLinus Torvalds 	 */
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
15701da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
15711da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
15721da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
15731da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
15741da177e4SLinus Torvalds 		     );
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	/*
15771da177e4SLinus Torvalds 	 * Redirect received -> path was valid.
15781da177e4SLinus Torvalds 	 * Look, redirects are sent only in response to data packets,
15791da177e4SLinus Torvalds 	 * so that this nexthop apparently is reachable. --ANK
15801da177e4SLinus Torvalds 	 */
1581d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	/* Duplicate redirect: silently ignore. */
1584d8d1f30bSChangli Gao 	if (neigh == rt->dst.neighbour)
15851da177e4SLinus Torvalds 		goto out;
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 	nrt = ip6_rt_copy(rt);
15881da177e4SLinus Torvalds 	if (nrt == NULL)
15891da177e4SLinus Torvalds 		goto out;
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
15921da177e4SLinus Torvalds 	if (on_link)
15931da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
15961da177e4SLinus Torvalds 	nrt->rt6i_dst.plen = 128;
1597d8d1f30bSChangli Gao 	nrt->dst.flags |= DST_HOST;
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
16001da177e4SLinus Torvalds 	nrt->rt6i_nexthop = neigh_clone(neigh);
16011da177e4SLinus Torvalds 
160240e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
16031da177e4SLinus Torvalds 		goto out;
16041da177e4SLinus Torvalds 
1605d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1606d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
16078d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
16088d71740cSTom Tucker 
16091da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE) {
1610e0a1ad73SThomas Graf 		ip6_del_rt(rt);
16111da177e4SLinus Torvalds 		return;
16121da177e4SLinus Torvalds 	}
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds out:
1615d8d1f30bSChangli Gao 	dst_release(&rt->dst);
16161da177e4SLinus Torvalds }
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds /*
16191da177e4SLinus Torvalds  *	Handle ICMP "packet too big" messages
16201da177e4SLinus Torvalds  *	i.e. Path MTU discovery
16211da177e4SLinus Torvalds  */
16221da177e4SLinus Torvalds 
1623b71d1d42SEric Dumazet static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
1624ae878ae2SMaciej Żenczykowski 			     struct net *net, u32 pmtu, int ifindex)
16251da177e4SLinus Torvalds {
16261da177e4SLinus Torvalds 	struct rt6_info *rt, *nrt;
16271da177e4SLinus Torvalds 	int allfrag = 0;
1628d3052b55SAndrey Vagin again:
1629ae878ae2SMaciej Żenczykowski 	rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
16301da177e4SLinus Torvalds 	if (rt == NULL)
16311da177e4SLinus Torvalds 		return;
16321da177e4SLinus Torvalds 
1633d3052b55SAndrey Vagin 	if (rt6_check_expired(rt)) {
1634d3052b55SAndrey Vagin 		ip6_del_rt(rt);
1635d3052b55SAndrey Vagin 		goto again;
1636d3052b55SAndrey Vagin 	}
1637d3052b55SAndrey Vagin 
1638d8d1f30bSChangli Gao 	if (pmtu >= dst_mtu(&rt->dst))
16391da177e4SLinus Torvalds 		goto out;
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	if (pmtu < IPV6_MIN_MTU) {
16421da177e4SLinus Torvalds 		/*
16431da177e4SLinus Torvalds 		 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
16441da177e4SLinus Torvalds 		 * MTU (1280) and a fragment header should always be included
16451da177e4SLinus Torvalds 		 * after a node receiving Too Big message reporting PMTU is
16461da177e4SLinus Torvalds 		 * less than the IPv6 Minimum Link MTU.
16471da177e4SLinus Torvalds 		 */
16481da177e4SLinus Torvalds 		pmtu = IPV6_MIN_MTU;
16491da177e4SLinus Torvalds 		allfrag = 1;
16501da177e4SLinus Torvalds 	}
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	/* New mtu received -> path was valid.
16531da177e4SLinus Torvalds 	   They are sent only in response to data packets,
16541da177e4SLinus Torvalds 	   so that this nexthop apparently is reachable. --ANK
16551da177e4SLinus Torvalds 	 */
1656d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds 	/* Host route. If it is static, it would be better
16591da177e4SLinus Torvalds 	   not to override it, but add new one, so that
16601da177e4SLinus Torvalds 	   when cache entry will expire old pmtu
16611da177e4SLinus Torvalds 	   would return automatically.
16621da177e4SLinus Torvalds 	 */
16631da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
1664defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1665defb3519SDavid S. Miller 		if (allfrag) {
1666defb3519SDavid S. Miller 			u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1667defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1668defb3519SDavid S. Miller 			dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1669defb3519SDavid S. Miller 		}
1670d8d1f30bSChangli Gao 		dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
16711da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
16721da177e4SLinus Torvalds 		goto out;
16731da177e4SLinus Torvalds 	}
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds 	/* Network route.
16761da177e4SLinus Torvalds 	   Two cases are possible:
16771da177e4SLinus Torvalds 	   1. It is connected route. Action: COW
16781da177e4SLinus Torvalds 	   2. It is gatewayed route or NONEXTHOP route. Action: clone it.
16791da177e4SLinus Torvalds 	 */
1680d5315b50SYOSHIFUJI Hideaki 	if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
1681a1e78363SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, daddr, saddr);
1682d5315b50SYOSHIFUJI Hideaki 	else
1683d5315b50SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, daddr);
1684a1e78363SYOSHIFUJI Hideaki 
1685d5315b50SYOSHIFUJI Hideaki 	if (nrt) {
1686defb3519SDavid S. Miller 		dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1687defb3519SDavid S. Miller 		if (allfrag) {
1688defb3519SDavid S. Miller 			u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1689defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1690defb3519SDavid S. Miller 			dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1691defb3519SDavid S. Miller 		}
1692a1e78363SYOSHIFUJI Hideaki 
16931da177e4SLinus Torvalds 		/* According to RFC 1981, detecting PMTU increase shouldn't be
1694a1e78363SYOSHIFUJI Hideaki 		 * happened within 5 mins, the recommended timer is 10 mins.
1695a1e78363SYOSHIFUJI Hideaki 		 * Here this route expiration time is set to ip6_rt_mtu_expires
1696a1e78363SYOSHIFUJI Hideaki 		 * which is 10 mins. After 10 mins the decreased pmtu is expired
1697a1e78363SYOSHIFUJI Hideaki 		 * and detecting PMTU increase will be automatically happened.
16981da177e4SLinus Torvalds 		 */
1699d8d1f30bSChangli Gao 		dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
17001da177e4SLinus Torvalds 		nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1701a1e78363SYOSHIFUJI Hideaki 
170240e22e8fSThomas Graf 		ip6_ins_rt(nrt);
17031da177e4SLinus Torvalds 	}
17041da177e4SLinus Torvalds out:
1705d8d1f30bSChangli Gao 	dst_release(&rt->dst);
17061da177e4SLinus Torvalds }
17071da177e4SLinus Torvalds 
1708b71d1d42SEric Dumazet void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
1709ae878ae2SMaciej Żenczykowski 			struct net_device *dev, u32 pmtu)
1710ae878ae2SMaciej Żenczykowski {
1711ae878ae2SMaciej Żenczykowski 	struct net *net = dev_net(dev);
1712ae878ae2SMaciej Żenczykowski 
1713ae878ae2SMaciej Żenczykowski 	/*
1714ae878ae2SMaciej Żenczykowski 	 * RFC 1981 states that a node "MUST reduce the size of the packets it
1715ae878ae2SMaciej Żenczykowski 	 * is sending along the path" that caused the Packet Too Big message.
1716ae878ae2SMaciej Żenczykowski 	 * Since it's not possible in the general case to determine which
1717ae878ae2SMaciej Żenczykowski 	 * interface was used to send the original packet, we update the MTU
1718ae878ae2SMaciej Żenczykowski 	 * on the interface that will be used to send future packets. We also
1719ae878ae2SMaciej Żenczykowski 	 * update the MTU on the interface that received the Packet Too Big in
1720ae878ae2SMaciej Żenczykowski 	 * case the original packet was forced out that interface with
1721ae878ae2SMaciej Żenczykowski 	 * SO_BINDTODEVICE or similar. This is the next best thing to the
1722ae878ae2SMaciej Żenczykowski 	 * correct behaviour, which would be to update the MTU on all
1723ae878ae2SMaciej Żenczykowski 	 * interfaces.
1724ae878ae2SMaciej Żenczykowski 	 */
1725ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1726ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1727ae878ae2SMaciej Żenczykowski }
1728ae878ae2SMaciej Żenczykowski 
17291da177e4SLinus Torvalds /*
17301da177e4SLinus Torvalds  *	Misc support functions
17311da177e4SLinus Torvalds  */
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
17341da177e4SLinus Torvalds {
1735c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ort->rt6i_dev);
17365c1e6aa3SDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
17375c1e6aa3SDavid S. Miller 					    ort->dst.dev);
17381da177e4SLinus Torvalds 
17391da177e4SLinus Torvalds 	if (rt) {
1740d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1741d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
17421da177e4SLinus Torvalds 
1743defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1744d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
17451da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
17461da177e4SLinus Torvalds 		if (rt->rt6i_idev)
17471da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1748d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
17491da177e4SLinus Torvalds 		rt->rt6i_expires = 0;
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
17521da177e4SLinus Torvalds 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
17531da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
17561da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
17571da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
17581da177e4SLinus Torvalds #endif
1759c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
17601da177e4SLinus Torvalds 	}
17611da177e4SLinus Torvalds 	return rt;
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds 
176470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1765efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1766b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1767b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
176870ceb4f5SYOSHIFUJI Hideaki {
176970ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
177070ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1771c71099acSThomas Graf 	struct fib6_table *table;
177270ceb4f5SYOSHIFUJI Hideaki 
1773efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
1774c71099acSThomas Graf 	if (table == NULL)
1775c71099acSThomas Graf 		return NULL;
1776c71099acSThomas Graf 
1777c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1778c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
177970ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
178070ceb4f5SYOSHIFUJI Hideaki 		goto out;
178170ceb4f5SYOSHIFUJI Hideaki 
1782d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
178370ceb4f5SYOSHIFUJI Hideaki 		if (rt->rt6i_dev->ifindex != ifindex)
178470ceb4f5SYOSHIFUJI Hideaki 			continue;
178570ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
178670ceb4f5SYOSHIFUJI Hideaki 			continue;
178770ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
178870ceb4f5SYOSHIFUJI Hideaki 			continue;
1789d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
179070ceb4f5SYOSHIFUJI Hideaki 		break;
179170ceb4f5SYOSHIFUJI Hideaki 	}
179270ceb4f5SYOSHIFUJI Hideaki out:
1793c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
179470ceb4f5SYOSHIFUJI Hideaki 	return rt;
179570ceb4f5SYOSHIFUJI Hideaki }
179670ceb4f5SYOSHIFUJI Hideaki 
1797efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1798b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1799b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
180070ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref)
180170ceb4f5SYOSHIFUJI Hideaki {
180286872cb5SThomas Graf 	struct fib6_config cfg = {
180386872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1804238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
180586872cb5SThomas Graf 		.fc_ifindex	= ifindex,
180686872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
180786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
180886872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
1809efa2cea0SDaniel Lezcano 		.fc_nlinfo.pid = 0,
1810efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1811efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
181286872cb5SThomas Graf 	};
181370ceb4f5SYOSHIFUJI Hideaki 
181486872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_dst, prefix);
181586872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
181686872cb5SThomas Graf 
1817e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1818e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
181986872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
182070ceb4f5SYOSHIFUJI Hideaki 
182186872cb5SThomas Graf 	ip6_route_add(&cfg);
182270ceb4f5SYOSHIFUJI Hideaki 
1823efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
182470ceb4f5SYOSHIFUJI Hideaki }
182570ceb4f5SYOSHIFUJI Hideaki #endif
182670ceb4f5SYOSHIFUJI Hideaki 
1827b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
18281da177e4SLinus Torvalds {
18291da177e4SLinus Torvalds 	struct rt6_info *rt;
1830c71099acSThomas Graf 	struct fib6_table *table;
18311da177e4SLinus Torvalds 
1832c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
1833c71099acSThomas Graf 	if (table == NULL)
1834c71099acSThomas Graf 		return NULL;
18351da177e4SLinus Torvalds 
1836c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1837d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
18381da177e4SLinus Torvalds 		if (dev == rt->rt6i_dev &&
1839045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
18401da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
18411da177e4SLinus Torvalds 			break;
18421da177e4SLinus Torvalds 	}
18431da177e4SLinus Torvalds 	if (rt)
1844d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
1845c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
18461da177e4SLinus Torvalds 	return rt;
18471da177e4SLinus Torvalds }
18481da177e4SLinus Torvalds 
1849b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1850ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1851ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
18521da177e4SLinus Torvalds {
185386872cb5SThomas Graf 	struct fib6_config cfg = {
185486872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1855238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
185686872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
185786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
185886872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
18595578689aSDaniel Lezcano 		.fc_nlinfo.pid = 0,
18605578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1861c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
186286872cb5SThomas Graf 	};
18631da177e4SLinus Torvalds 
186486872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
18651da177e4SLinus Torvalds 
186686872cb5SThomas Graf 	ip6_route_add(&cfg);
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
18691da177e4SLinus Torvalds }
18701da177e4SLinus Torvalds 
18717b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
18721da177e4SLinus Torvalds {
18731da177e4SLinus Torvalds 	struct rt6_info *rt;
1874c71099acSThomas Graf 	struct fib6_table *table;
1875c71099acSThomas Graf 
1876c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
18777b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
1878c71099acSThomas Graf 	if (table == NULL)
1879c71099acSThomas Graf 		return;
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds restart:
1882c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1883d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
18841da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1885d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1886c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1887e0a1ad73SThomas Graf 			ip6_del_rt(rt);
18881da177e4SLinus Torvalds 			goto restart;
18891da177e4SLinus Torvalds 		}
18901da177e4SLinus Torvalds 	}
1891c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
18921da177e4SLinus Torvalds }
18931da177e4SLinus Torvalds 
18945578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
18955578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
189686872cb5SThomas Graf 				 struct fib6_config *cfg)
189786872cb5SThomas Graf {
189886872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
189986872cb5SThomas Graf 
190086872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
190186872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
190286872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
190386872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
190486872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
190586872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
190686872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
190786872cb5SThomas Graf 
19085578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1909f1243c2dSBenjamin Thery 
191086872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
191186872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
191286872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
191386872cb5SThomas Graf }
191486872cb5SThomas Graf 
19155578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
19161da177e4SLinus Torvalds {
191786872cb5SThomas Graf 	struct fib6_config cfg;
19181da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
19191da177e4SLinus Torvalds 	int err;
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 	switch(cmd) {
19221da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
19231da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
19241da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
19251da177e4SLinus Torvalds 			return -EPERM;
19261da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
19271da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
19281da177e4SLinus Torvalds 		if (err)
19291da177e4SLinus Torvalds 			return -EFAULT;
19301da177e4SLinus Torvalds 
19315578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
193286872cb5SThomas Graf 
19331da177e4SLinus Torvalds 		rtnl_lock();
19341da177e4SLinus Torvalds 		switch (cmd) {
19351da177e4SLinus Torvalds 		case SIOCADDRT:
193686872cb5SThomas Graf 			err = ip6_route_add(&cfg);
19371da177e4SLinus Torvalds 			break;
19381da177e4SLinus Torvalds 		case SIOCDELRT:
193986872cb5SThomas Graf 			err = ip6_route_del(&cfg);
19401da177e4SLinus Torvalds 			break;
19411da177e4SLinus Torvalds 		default:
19421da177e4SLinus Torvalds 			err = -EINVAL;
19431da177e4SLinus Torvalds 		}
19441da177e4SLinus Torvalds 		rtnl_unlock();
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds 		return err;
19473ff50b79SStephen Hemminger 	}
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	return -EINVAL;
19501da177e4SLinus Torvalds }
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds /*
19531da177e4SLinus Torvalds  *	Drop the packet on the floor
19541da177e4SLinus Torvalds  */
19551da177e4SLinus Torvalds 
1956d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
19571da177e4SLinus Torvalds {
1958612f09e8SYOSHIFUJI Hideaki 	int type;
1959adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
1960612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
1961612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
19620660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
196345bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
19643bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
19653bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
1966612f09e8SYOSHIFUJI Hideaki 			break;
1967612f09e8SYOSHIFUJI Hideaki 		}
1968612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
1969612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
19703bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
19713bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
1972612f09e8SYOSHIFUJI Hideaki 		break;
1973612f09e8SYOSHIFUJI Hideaki 	}
19743ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
19751da177e4SLinus Torvalds 	kfree_skb(skb);
19761da177e4SLinus Torvalds 	return 0;
19771da177e4SLinus Torvalds }
19781da177e4SLinus Torvalds 
19799ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
19809ce8ade0SThomas Graf {
1981612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
19829ce8ade0SThomas Graf }
19839ce8ade0SThomas Graf 
198420380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
19851da177e4SLinus Torvalds {
1986adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
1987612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
19881da177e4SLinus Torvalds }
19891da177e4SLinus Torvalds 
19906723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
19916723ab54SDavid S. Miller 
19929ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
19939ce8ade0SThomas Graf {
1994612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
19959ce8ade0SThomas Graf }
19969ce8ade0SThomas Graf 
19979ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
19989ce8ade0SThomas Graf {
1999adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2000612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
20019ce8ade0SThomas Graf }
20029ce8ade0SThomas Graf 
20036723ab54SDavid S. Miller #endif
20046723ab54SDavid S. Miller 
20051da177e4SLinus Torvalds /*
20061da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
20071da177e4SLinus Torvalds  */
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
20101da177e4SLinus Torvalds 				    const struct in6_addr *addr,
20111da177e4SLinus Torvalds 				    int anycast)
20121da177e4SLinus Torvalds {
2013c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
20145c1e6aa3SDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
20155c1e6aa3SDavid S. Miller 					    net->loopback_dev);
201614deae41SDavid S. Miller 	struct neighbour *neigh;
20171da177e4SLinus Torvalds 
201840385653SBen Greear 	if (rt == NULL) {
201940385653SBen Greear 		if (net_ratelimit())
202040385653SBen Greear 			pr_warning("IPv6:  Maximum number of routes reached,"
202140385653SBen Greear 				   " consider increasing route/max_size.\n");
20221da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
202340385653SBen Greear 	}
20241da177e4SLinus Torvalds 
20251da177e4SLinus Torvalds 	in6_dev_hold(idev);
20261da177e4SLinus Torvalds 
2027d8d1f30bSChangli Gao 	rt->dst.flags = DST_HOST;
2028d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2029d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
20301da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2031d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
203458c4fb86SYOSHIFUJI Hideaki 	if (anycast)
203558c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
203658c4fb86SYOSHIFUJI Hideaki 	else
20371da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
203814deae41SDavid S. Miller 	neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
203914deae41SDavid S. Miller 	if (IS_ERR(neigh)) {
2040d8d1f30bSChangli Gao 		dst_free(&rt->dst);
204114deae41SDavid S. Miller 
204229546a64SDavid S. Miller 		return ERR_CAST(neigh);
20431da177e4SLinus Torvalds 	}
204414deae41SDavid S. Miller 	rt->rt6i_nexthop = neigh;
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
20471da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
20485578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
20491da177e4SLinus Torvalds 
2050d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 	return rt;
20531da177e4SLinus Torvalds }
20541da177e4SLinus Torvalds 
2055c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2056c3968a85SDaniel Walter 			struct rt6_info *rt,
2057b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2058c3968a85SDaniel Walter 			unsigned int prefs,
2059c3968a85SDaniel Walter 			struct in6_addr *saddr)
2060c3968a85SDaniel Walter {
2061c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2062c3968a85SDaniel Walter 	int err = 0;
2063c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
2064c3968a85SDaniel Walter 		ipv6_addr_copy(saddr, &rt->rt6i_prefsrc.addr);
2065c3968a85SDaniel Walter 	else
2066c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2067c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2068c3968a85SDaniel Walter 	return err;
2069c3968a85SDaniel Walter }
2070c3968a85SDaniel Walter 
2071c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2072c3968a85SDaniel Walter struct arg_dev_net_ip {
2073c3968a85SDaniel Walter 	struct net_device *dev;
2074c3968a85SDaniel Walter 	struct net *net;
2075c3968a85SDaniel Walter 	struct in6_addr *addr;
2076c3968a85SDaniel Walter };
2077c3968a85SDaniel Walter 
2078c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2079c3968a85SDaniel Walter {
2080c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2081c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2082c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2083c3968a85SDaniel Walter 
2084c3968a85SDaniel Walter 	if (((void *)rt->rt6i_dev == dev || dev == NULL) &&
2085c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2086c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2087c3968a85SDaniel Walter 		/* remove prefsrc entry */
2088c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2089c3968a85SDaniel Walter 	}
2090c3968a85SDaniel Walter 	return 0;
2091c3968a85SDaniel Walter }
2092c3968a85SDaniel Walter 
2093c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2094c3968a85SDaniel Walter {
2095c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2096c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2097c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2098c3968a85SDaniel Walter 		.net = net,
2099c3968a85SDaniel Walter 		.addr = &ifp->addr,
2100c3968a85SDaniel Walter 	};
2101c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2102c3968a85SDaniel Walter }
2103c3968a85SDaniel Walter 
21048ed67789SDaniel Lezcano struct arg_dev_net {
21058ed67789SDaniel Lezcano 	struct net_device *dev;
21068ed67789SDaniel Lezcano 	struct net *net;
21078ed67789SDaniel Lezcano };
21088ed67789SDaniel Lezcano 
21091da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
21101da177e4SLinus Torvalds {
2111bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2112bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
21138ed67789SDaniel Lezcano 
2114bc3ef660Sstephen hemminger 	if ((rt->rt6i_dev == dev || dev == NULL) &&
2115bc3ef660Sstephen hemminger 	    rt != adn->net->ipv6.ip6_null_entry) {
21161da177e4SLinus Torvalds 		RT6_TRACE("deleted by ifdown %p\n", rt);
21171da177e4SLinus Torvalds 		return -1;
21181da177e4SLinus Torvalds 	}
21191da177e4SLinus Torvalds 	return 0;
21201da177e4SLinus Torvalds }
21211da177e4SLinus Torvalds 
2122f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
21231da177e4SLinus Torvalds {
21248ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
21258ed67789SDaniel Lezcano 		.dev = dev,
21268ed67789SDaniel Lezcano 		.net = net,
21278ed67789SDaniel Lezcano 	};
21288ed67789SDaniel Lezcano 
21298ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
21301e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
21311da177e4SLinus Torvalds }
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds struct rt6_mtu_change_arg
21341da177e4SLinus Torvalds {
21351da177e4SLinus Torvalds 	struct net_device *dev;
21361da177e4SLinus Torvalds 	unsigned mtu;
21371da177e4SLinus Torvalds };
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
21401da177e4SLinus Torvalds {
21411da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
21421da177e4SLinus Torvalds 	struct inet6_dev *idev;
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
21451da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
21461da177e4SLinus Torvalds 	   We still use this lock to block changes
21471da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
21481da177e4SLinus Torvalds 	*/
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
21511da177e4SLinus Torvalds 	if (idev == NULL)
21521da177e4SLinus Torvalds 		return 0;
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
21551da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
21561da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
21571da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
21581da177e4SLinus Torvalds 	 */
21591da177e4SLinus Torvalds 	/*
21601da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
21611da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
21621da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
21631da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
21641da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
21651da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
21661da177e4SLinus Torvalds 	   PMTU discouvery.
21671da177e4SLinus Torvalds 	 */
21681da177e4SLinus Torvalds 	if (rt->rt6i_dev == arg->dev &&
2169d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2170d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2171d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2172d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2173defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2174566cfd8fSSimon Arlott 	}
21751da177e4SLinus Torvalds 	return 0;
21761da177e4SLinus Torvalds }
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu)
21791da177e4SLinus Torvalds {
2180c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2181c71099acSThomas Graf 		.dev = dev,
2182c71099acSThomas Graf 		.mtu = mtu,
2183c71099acSThomas Graf 	};
21841da177e4SLinus Torvalds 
2185c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
21861da177e4SLinus Torvalds }
21871da177e4SLinus Torvalds 
2188ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
21895176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
219086872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2191ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
219286872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
219386872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
219486872cb5SThomas Graf };
219586872cb5SThomas Graf 
219686872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
219786872cb5SThomas Graf 			      struct fib6_config *cfg)
21981da177e4SLinus Torvalds {
219986872cb5SThomas Graf 	struct rtmsg *rtm;
220086872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
220186872cb5SThomas Graf 	int err;
22021da177e4SLinus Torvalds 
220386872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
220486872cb5SThomas Graf 	if (err < 0)
220586872cb5SThomas Graf 		goto errout;
22061da177e4SLinus Torvalds 
220786872cb5SThomas Graf 	err = -EINVAL;
220886872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
220986872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
221086872cb5SThomas Graf 
221186872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
221286872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
221386872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
221486872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
221586872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
221686872cb5SThomas Graf 
221786872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
221886872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
221986872cb5SThomas Graf 
2220ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2221ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2222ab79ad14SMaciej Żenczykowski 
222386872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
222486872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
22253b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
222686872cb5SThomas Graf 
222786872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
222886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
222986872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
22301da177e4SLinus Torvalds 	}
223186872cb5SThomas Graf 
223286872cb5SThomas Graf 	if (tb[RTA_DST]) {
223386872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
223486872cb5SThomas Graf 
223586872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
223686872cb5SThomas Graf 			goto errout;
223786872cb5SThomas Graf 
223886872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
22391da177e4SLinus Torvalds 	}
224086872cb5SThomas Graf 
224186872cb5SThomas Graf 	if (tb[RTA_SRC]) {
224286872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
224386872cb5SThomas Graf 
224486872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
224586872cb5SThomas Graf 			goto errout;
224686872cb5SThomas Graf 
224786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
22481da177e4SLinus Torvalds 	}
224986872cb5SThomas Graf 
2250c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2251c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2252c3968a85SDaniel Walter 
225386872cb5SThomas Graf 	if (tb[RTA_OIF])
225486872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
225586872cb5SThomas Graf 
225686872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
225786872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
225886872cb5SThomas Graf 
225986872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
226086872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
226186872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
22621da177e4SLinus Torvalds 	}
226386872cb5SThomas Graf 
226486872cb5SThomas Graf 	if (tb[RTA_TABLE])
226586872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
226686872cb5SThomas Graf 
226786872cb5SThomas Graf 	err = 0;
226886872cb5SThomas Graf errout:
226986872cb5SThomas Graf 	return err;
22701da177e4SLinus Torvalds }
22711da177e4SLinus Torvalds 
2272c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
22731da177e4SLinus Torvalds {
227486872cb5SThomas Graf 	struct fib6_config cfg;
227586872cb5SThomas Graf 	int err;
22761da177e4SLinus Torvalds 
227786872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
227886872cb5SThomas Graf 	if (err < 0)
227986872cb5SThomas Graf 		return err;
228086872cb5SThomas Graf 
228186872cb5SThomas Graf 	return ip6_route_del(&cfg);
22821da177e4SLinus Torvalds }
22831da177e4SLinus Torvalds 
2284c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
22851da177e4SLinus Torvalds {
228686872cb5SThomas Graf 	struct fib6_config cfg;
228786872cb5SThomas Graf 	int err;
22881da177e4SLinus Torvalds 
228986872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
229086872cb5SThomas Graf 	if (err < 0)
229186872cb5SThomas Graf 		return err;
229286872cb5SThomas Graf 
229386872cb5SThomas Graf 	return ip6_route_add(&cfg);
22941da177e4SLinus Torvalds }
22951da177e4SLinus Torvalds 
2296339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2297339bf98fSThomas Graf {
2298339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2299339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2300339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2301339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2302339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2303339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2304339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2305339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2306339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
23076a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2308339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2309339bf98fSThomas Graf }
2310339bf98fSThomas Graf 
2311191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2312191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
23130d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
23140d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
23157bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
23161da177e4SLinus Torvalds {
23171da177e4SLinus Torvalds 	struct rtmsg *rtm;
23181da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2319e3703b3dSThomas Graf 	long expires;
23209e762a4aSPatrick McHardy 	u32 table;
23211da177e4SLinus Torvalds 
23221da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
23231da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
23241da177e4SLinus Torvalds 			/* success since this is not a prefix route */
23251da177e4SLinus Torvalds 			return 1;
23261da177e4SLinus Torvalds 		}
23271da177e4SLinus Torvalds 	}
23281da177e4SLinus Torvalds 
23292d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
23302d7202bfSThomas Graf 	if (nlh == NULL)
233126932566SPatrick McHardy 		return -EMSGSIZE;
23322d7202bfSThomas Graf 
23332d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
23341da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
23351da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
23361da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
23371da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2338c71099acSThomas Graf 	if (rt->rt6i_table)
23399e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2340c71099acSThomas Graf 	else
23419e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
23429e762a4aSPatrick McHardy 	rtm->rtm_table = table;
23432d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_TABLE, table);
23441da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_REJECT)
23451da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
2346ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags&RTF_LOCAL)
2347ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
23481da177e4SLinus Torvalds 	else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
23491da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
23501da177e4SLinus Torvalds 	else
23511da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
23521da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
23531da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
23541da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
23551da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_DYNAMIC)
23561da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
23571da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
23581da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
23591da177e4SLinus Torvalds 	else if (rt->rt6i_flags&RTF_DEFAULT)
23601da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE)
23631da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds 	if (dst) {
23662d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, dst);
23671da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
23681da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
23692d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
23701da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
23711da177e4SLinus Torvalds 	if (src) {
23722d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, src);
23731da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
23741da177e4SLinus Torvalds 	} else if (rtm->rtm_src_len)
23752d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
23761da177e4SLinus Torvalds #endif
23777bc570c8SYOSHIFUJI Hideaki 	if (iif) {
23787bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
23797bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
23808229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
23817bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
23827bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
23837bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
23847bc570c8SYOSHIFUJI Hideaki 						return 0;
23857bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
23867bc570c8SYOSHIFUJI Hideaki 				} else {
23877bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
23887bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
23897bc570c8SYOSHIFUJI Hideaki 				}
23907bc570c8SYOSHIFUJI Hideaki 			}
23917bc570c8SYOSHIFUJI Hideaki 		} else
23927bc570c8SYOSHIFUJI Hideaki #endif
23932d7202bfSThomas Graf 			NLA_PUT_U32(skb, RTA_IIF, iif);
23947bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
23951da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2396c3968a85SDaniel Walter 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0)
2397c3968a85SDaniel Walter 			NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2398c3968a85SDaniel Walter 	}
2399c3968a85SDaniel Walter 
2400c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2401c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
2402c3968a85SDaniel Walter 		ipv6_addr_copy(&saddr_buf, &rt->rt6i_prefsrc.addr);
24032d7202bfSThomas Graf 		NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
24041da177e4SLinus Torvalds 	}
24052d7202bfSThomas Graf 
2406defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
24072d7202bfSThomas Graf 		goto nla_put_failure;
24082d7202bfSThomas Graf 
2409d8d1f30bSChangli Gao 	if (rt->dst.neighbour)
2410d8d1f30bSChangli Gao 		NLA_PUT(skb, RTA_GATEWAY, 16, &rt->dst.neighbour->primary_key);
24112d7202bfSThomas Graf 
2412d8d1f30bSChangli Gao 	if (rt->dst.dev)
24132d7202bfSThomas Graf 		NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
24142d7202bfSThomas Graf 
24152d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
2416e3703b3dSThomas Graf 
241736e3deaeSYOSHIFUJI Hideaki 	if (!(rt->rt6i_flags & RTF_EXPIRES))
241836e3deaeSYOSHIFUJI Hideaki 		expires = 0;
241936e3deaeSYOSHIFUJI Hideaki 	else if (rt->rt6i_expires - jiffies < INT_MAX)
242036e3deaeSYOSHIFUJI Hideaki 		expires = rt->rt6i_expires - jiffies;
242136e3deaeSYOSHIFUJI Hideaki 	else
242236e3deaeSYOSHIFUJI Hideaki 		expires = INT_MAX;
242369cdf8f9SYOSHIFUJI Hideaki 
2424d8d1f30bSChangli Gao 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2425d8d1f30bSChangli Gao 			       expires, rt->dst.error) < 0)
2426e3703b3dSThomas Graf 		goto nla_put_failure;
24271da177e4SLinus Torvalds 
24282d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
24292d7202bfSThomas Graf 
24302d7202bfSThomas Graf nla_put_failure:
243126932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
243226932566SPatrick McHardy 	return -EMSGSIZE;
24331da177e4SLinus Torvalds }
24341da177e4SLinus Torvalds 
24351b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
24361da177e4SLinus Torvalds {
24371da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
24381da177e4SLinus Torvalds 	int prefix;
24391da177e4SLinus Torvalds 
24402d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
24412d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
24421da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
24431da177e4SLinus Torvalds 	} else
24441da177e4SLinus Torvalds 		prefix = 0;
24451da177e4SLinus Torvalds 
2446191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2447191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
24481da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
24497bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
24501da177e4SLinus Torvalds }
24511da177e4SLinus Torvalds 
2452c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
24531da177e4SLinus Torvalds {
24543b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2455ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
24561da177e4SLinus Torvalds 	struct rt6_info *rt;
2457ab364a6fSThomas Graf 	struct sk_buff *skb;
2458ab364a6fSThomas Graf 	struct rtmsg *rtm;
24594c9483b2SDavid S. Miller 	struct flowi6 fl6;
2460ab364a6fSThomas Graf 	int err, iif = 0;
2461ab364a6fSThomas Graf 
2462ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2463ab364a6fSThomas Graf 	if (err < 0)
2464ab364a6fSThomas Graf 		goto errout;
2465ab364a6fSThomas Graf 
2466ab364a6fSThomas Graf 	err = -EINVAL;
24674c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2468ab364a6fSThomas Graf 
2469ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2470ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2471ab364a6fSThomas Graf 			goto errout;
2472ab364a6fSThomas Graf 
24734c9483b2SDavid S. Miller 		ipv6_addr_copy(&fl6.saddr, nla_data(tb[RTA_SRC]));
2474ab364a6fSThomas Graf 	}
2475ab364a6fSThomas Graf 
2476ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2477ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2478ab364a6fSThomas Graf 			goto errout;
2479ab364a6fSThomas Graf 
24804c9483b2SDavid S. Miller 		ipv6_addr_copy(&fl6.daddr, nla_data(tb[RTA_DST]));
2481ab364a6fSThomas Graf 	}
2482ab364a6fSThomas Graf 
2483ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2484ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2485ab364a6fSThomas Graf 
2486ab364a6fSThomas Graf 	if (tb[RTA_OIF])
24874c9483b2SDavid S. Miller 		fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]);
2488ab364a6fSThomas Graf 
2489ab364a6fSThomas Graf 	if (iif) {
2490ab364a6fSThomas Graf 		struct net_device *dev;
24915578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2492ab364a6fSThomas Graf 		if (!dev) {
2493ab364a6fSThomas Graf 			err = -ENODEV;
2494ab364a6fSThomas Graf 			goto errout;
2495ab364a6fSThomas Graf 		}
2496ab364a6fSThomas Graf 	}
24971da177e4SLinus Torvalds 
24981da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2499ab364a6fSThomas Graf 	if (skb == NULL) {
2500ab364a6fSThomas Graf 		err = -ENOBUFS;
2501ab364a6fSThomas Graf 		goto errout;
2502ab364a6fSThomas Graf 	}
25031da177e4SLinus Torvalds 
25041da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
25051da177e4SLinus Torvalds 	   through good chunk of routing engine.
25061da177e4SLinus Torvalds 	 */
2507459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
25081da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
25091da177e4SLinus Torvalds 
25104c9483b2SDavid S. Miller 	rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6);
2511d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
25121da177e4SLinus Torvalds 
25134c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
25141da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
25157bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
25161da177e4SLinus Torvalds 	if (err < 0) {
2517ab364a6fSThomas Graf 		kfree_skb(skb);
2518ab364a6fSThomas Graf 		goto errout;
25191da177e4SLinus Torvalds 	}
25201da177e4SLinus Torvalds 
25215578689aSDaniel Lezcano 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2522ab364a6fSThomas Graf errout:
25231da177e4SLinus Torvalds 	return err;
25241da177e4SLinus Torvalds }
25251da177e4SLinus Torvalds 
252686872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
25271da177e4SLinus Torvalds {
25281da177e4SLinus Torvalds 	struct sk_buff *skb;
25295578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2530528c4cebSDenis V. Lunev 	u32 seq;
2531528c4cebSDenis V. Lunev 	int err;
25320d51aa80SJamal Hadi Salim 
2533528c4cebSDenis V. Lunev 	err = -ENOBUFS;
2534528c4cebSDenis V. Lunev 	seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
253586872cb5SThomas Graf 
2536339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
253721713ebcSThomas Graf 	if (skb == NULL)
253821713ebcSThomas Graf 		goto errout;
25391da177e4SLinus Torvalds 
2540191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
25417bc570c8SYOSHIFUJI Hideaki 				event, info->pid, seq, 0, 0, 0);
254226932566SPatrick McHardy 	if (err < 0) {
254326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
254426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
254526932566SPatrick McHardy 		kfree_skb(skb);
254626932566SPatrick McHardy 		goto errout;
254726932566SPatrick McHardy 	}
25481ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
25495578689aSDaniel Lezcano 		    info->nlh, gfp_any());
25501ce85fe4SPablo Neira Ayuso 	return;
255121713ebcSThomas Graf errout:
255221713ebcSThomas Graf 	if (err < 0)
25535578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
25541da177e4SLinus Torvalds }
25551da177e4SLinus Torvalds 
25568ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
25578ed67789SDaniel Lezcano 				unsigned long event, void *data)
25588ed67789SDaniel Lezcano {
25598ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2560c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
25618ed67789SDaniel Lezcano 
25628ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2563d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
25648ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
25658ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2566d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
25678ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2568d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
25698ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
25708ed67789SDaniel Lezcano #endif
25718ed67789SDaniel Lezcano 	}
25728ed67789SDaniel Lezcano 
25738ed67789SDaniel Lezcano 	return NOTIFY_OK;
25748ed67789SDaniel Lezcano }
25758ed67789SDaniel Lezcano 
25761da177e4SLinus Torvalds /*
25771da177e4SLinus Torvalds  *	/proc
25781da177e4SLinus Torvalds  */
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds struct rt6_proc_arg
25831da177e4SLinus Torvalds {
25841da177e4SLinus Torvalds 	char *buffer;
25851da177e4SLinus Torvalds 	int offset;
25861da177e4SLinus Torvalds 	int length;
25871da177e4SLinus Torvalds 	int skip;
25881da177e4SLinus Torvalds 	int len;
25891da177e4SLinus Torvalds };
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
25921da177e4SLinus Torvalds {
259333120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
25941da177e4SLinus Torvalds 
25954b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
25984b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
25991da177e4SLinus Torvalds #else
260033120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
26011da177e4SLinus Torvalds #endif
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds 	if (rt->rt6i_nexthop) {
26044b7a4274SHarvey Harrison 		seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key);
26051da177e4SLinus Torvalds 	} else {
260633120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
26071da177e4SLinus Torvalds 	}
260833120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2609d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2610d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
26111da177e4SLinus Torvalds 		   rt->rt6i_dev ? rt->rt6i_dev->name : "");
26121da177e4SLinus Torvalds 	return 0;
26131da177e4SLinus Torvalds }
26141da177e4SLinus Torvalds 
261533120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
26161da177e4SLinus Torvalds {
2617f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
2618f3db4851SDaniel Lezcano 	fib6_clean_all(net, rt6_info_route, 0, m);
261933120b30SAlexey Dobriyan 	return 0;
26201da177e4SLinus Torvalds }
26211da177e4SLinus Torvalds 
262233120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
262333120b30SAlexey Dobriyan {
2624de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2625f3db4851SDaniel Lezcano }
2626f3db4851SDaniel Lezcano 
262733120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
262833120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
262933120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
263033120b30SAlexey Dobriyan 	.read		= seq_read,
263133120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2632b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
263333120b30SAlexey Dobriyan };
263433120b30SAlexey Dobriyan 
26351da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
26361da177e4SLinus Torvalds {
263769ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
26381da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
263969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
264069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
264169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
264269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
264369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2644fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
264569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
26461da177e4SLinus Torvalds 
26471da177e4SLinus Torvalds 	return 0;
26481da177e4SLinus Torvalds }
26491da177e4SLinus Torvalds 
26501da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
26511da177e4SLinus Torvalds {
2652de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
265369ddb805SDaniel Lezcano }
265469ddb805SDaniel Lezcano 
26559a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
26561da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
26571da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
26581da177e4SLinus Torvalds 	.read	 = seq_read,
26591da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2660b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
26611da177e4SLinus Torvalds };
26621da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
26631da177e4SLinus Torvalds 
26641da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds static
26678d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
26681da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
26691da177e4SLinus Torvalds {
2670c486da34SLucian Adrian Grijincu 	struct net *net;
2671c486da34SLucian Adrian Grijincu 	int delay;
2672c486da34SLucian Adrian Grijincu 	if (!write)
2673c486da34SLucian Adrian Grijincu 		return -EINVAL;
2674c486da34SLucian Adrian Grijincu 
2675c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2676c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
26778d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
26785b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
26791da177e4SLinus Torvalds 	return 0;
26801da177e4SLinus Torvalds }
26811da177e4SLinus Torvalds 
2682760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
26831da177e4SLinus Torvalds 	{
26841da177e4SLinus Torvalds 		.procname	=	"flush",
26854990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
26861da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
268789c8b3a1SDave Jones 		.mode		=	0200,
26886d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
26891da177e4SLinus Torvalds 	},
26901da177e4SLinus Torvalds 	{
26911da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
26929a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
26931da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26941da177e4SLinus Torvalds 		.mode		=	0644,
26956d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
26961da177e4SLinus Torvalds 	},
26971da177e4SLinus Torvalds 	{
26981da177e4SLinus Torvalds 		.procname	=	"max_size",
26994990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
27001da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27011da177e4SLinus Torvalds 		.mode		=	0644,
27026d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27031da177e4SLinus Torvalds 	},
27041da177e4SLinus Torvalds 	{
27051da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
27064990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
27071da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27081da177e4SLinus Torvalds 		.mode		=	0644,
27096d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27101da177e4SLinus Torvalds 	},
27111da177e4SLinus Torvalds 	{
27121da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
27134990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
27141da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27151da177e4SLinus Torvalds 		.mode		=	0644,
27166d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27171da177e4SLinus Torvalds 	},
27181da177e4SLinus Torvalds 	{
27191da177e4SLinus Torvalds 		.procname	=	"gc_interval",
27204990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
27211da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27221da177e4SLinus Torvalds 		.mode		=	0644,
27236d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27241da177e4SLinus Torvalds 	},
27251da177e4SLinus Torvalds 	{
27261da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
27274990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
27281da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27291da177e4SLinus Torvalds 		.mode		=	0644,
2730f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
27311da177e4SLinus Torvalds 	},
27321da177e4SLinus Torvalds 	{
27331da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
27344990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
27351da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27361da177e4SLinus Torvalds 		.mode		=	0644,
27376d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27381da177e4SLinus Torvalds 	},
27391da177e4SLinus Torvalds 	{
27401da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
27414990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
27421da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27431da177e4SLinus Torvalds 		.mode		=	0644,
2744f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
27451da177e4SLinus Torvalds 	},
27461da177e4SLinus Torvalds 	{
27471da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
27484990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
27491da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27501da177e4SLinus Torvalds 		.mode		=	0644,
27516d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
27521da177e4SLinus Torvalds 	},
2753f8572d8fSEric W. Biederman 	{ }
27541da177e4SLinus Torvalds };
27551da177e4SLinus Torvalds 
27562c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2757760f2d01SDaniel Lezcano {
2758760f2d01SDaniel Lezcano 	struct ctl_table *table;
2759760f2d01SDaniel Lezcano 
2760760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2761760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2762760f2d01SDaniel Lezcano 			GFP_KERNEL);
27635ee09105SYOSHIFUJI Hideaki 
27645ee09105SYOSHIFUJI Hideaki 	if (table) {
27655ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2766c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
276786393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
27685ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
27695ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
27705ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
27715ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
27725ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
27735ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
27745ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
27759c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
27765ee09105SYOSHIFUJI Hideaki 	}
27775ee09105SYOSHIFUJI Hideaki 
2778760f2d01SDaniel Lezcano 	return table;
2779760f2d01SDaniel Lezcano }
27801da177e4SLinus Torvalds #endif
27811da177e4SLinus Torvalds 
27822c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2783cdb18761SDaniel Lezcano {
2784633d424bSPavel Emelyanov 	int ret = -ENOMEM;
27858ed67789SDaniel Lezcano 
278686393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
278786393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2788f2fc6a54SBenjamin Thery 
2789fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2790fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2791fc66f95cSEric Dumazet 
27928ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
27938ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
27948ed67789SDaniel Lezcano 					   GFP_KERNEL);
27958ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2796fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2797d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
27988ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2799d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
280062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
280162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28028ed67789SDaniel Lezcano 
28038ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
28048ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
28058ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
28068ed67789SDaniel Lezcano 					       GFP_KERNEL);
280768fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
280868fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2809d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
28108ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2811d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
281262fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
281362fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28148ed67789SDaniel Lezcano 
28158ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
28168ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
28178ed67789SDaniel Lezcano 					       GFP_KERNEL);
281868fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
281968fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2820d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
28218ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2822d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
282362fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
282462fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28258ed67789SDaniel Lezcano #endif
28268ed67789SDaniel Lezcano 
2827b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2828b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2829b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2830b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2831b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2832b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2833b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2834b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2835b339a47cSPeter Zijlstra 
2836cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2837cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2838cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2839cdb18761SDaniel Lezcano #endif
28406891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
28416891a346SBenjamin Thery 
28428ed67789SDaniel Lezcano 	ret = 0;
28438ed67789SDaniel Lezcano out:
28448ed67789SDaniel Lezcano 	return ret;
2845f2fc6a54SBenjamin Thery 
284668fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
284768fffc67SPeter Zijlstra out_ip6_prohibit_entry:
284868fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
284968fffc67SPeter Zijlstra out_ip6_null_entry:
285068fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
285168fffc67SPeter Zijlstra #endif
2852fc66f95cSEric Dumazet out_ip6_dst_entries:
2853fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2854f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2855f2fc6a54SBenjamin Thery 	goto out;
2856cdb18761SDaniel Lezcano }
2857cdb18761SDaniel Lezcano 
28582c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2859cdb18761SDaniel Lezcano {
2860cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2861cdb18761SDaniel Lezcano 	proc_net_remove(net, "ipv6_route");
2862cdb18761SDaniel Lezcano 	proc_net_remove(net, "rt6_stats");
2863cdb18761SDaniel Lezcano #endif
28648ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
28658ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
28668ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
28678ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
28688ed67789SDaniel Lezcano #endif
286941bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2870cdb18761SDaniel Lezcano }
2871cdb18761SDaniel Lezcano 
2872cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
2873cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
2874cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
2875cdb18761SDaniel Lezcano };
2876cdb18761SDaniel Lezcano 
28778ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
28788ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
28798ed67789SDaniel Lezcano 	.priority = 0,
28808ed67789SDaniel Lezcano };
28818ed67789SDaniel Lezcano 
2882433d49c3SDaniel Lezcano int __init ip6_route_init(void)
28831da177e4SLinus Torvalds {
2884433d49c3SDaniel Lezcano 	int ret;
2885433d49c3SDaniel Lezcano 
28869a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
28879a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
28889a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
28899a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
28909a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
2891c19a28e1SFernando Carrijo 		goto out;
289214e50e57SDavid S. Miller 
2893fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
28948ed67789SDaniel Lezcano 	if (ret)
2895bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
2896bdb3289fSDaniel Lezcano 
2897fc66f95cSEric Dumazet 	ret = register_pernet_subsys(&ip6_route_net_ops);
2898fc66f95cSEric Dumazet 	if (ret)
2899fc66f95cSEric Dumazet 		goto out_dst_entries;
2900fc66f95cSEric Dumazet 
29015dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
29025dc121e9SArnaud Ebalard 
29038ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
29048ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
29058ed67789SDaniel Lezcano 	 * manually for init_net */
2906d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
29078ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2908bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2909d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
29108ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2911d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
29128ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2913bdb3289fSDaniel Lezcano   #endif
2914433d49c3SDaniel Lezcano 	ret = fib6_init();
2915433d49c3SDaniel Lezcano 	if (ret)
29168ed67789SDaniel Lezcano 		goto out_register_subsys;
2917433d49c3SDaniel Lezcano 
2918433d49c3SDaniel Lezcano 	ret = xfrm6_init();
2919433d49c3SDaniel Lezcano 	if (ret)
2920cdb18761SDaniel Lezcano 		goto out_fib6_init;
2921c35b7e72SDaniel Lezcano 
2922433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
2923433d49c3SDaniel Lezcano 	if (ret)
2924433d49c3SDaniel Lezcano 		goto xfrm6_init;
29257e5449c2SDaniel Lezcano 
2926433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
2927433d49c3SDaniel Lezcano 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2928433d49c3SDaniel Lezcano 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2929433d49c3SDaniel Lezcano 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2930433d49c3SDaniel Lezcano 		goto fib6_rules_init;
2931433d49c3SDaniel Lezcano 
29328ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
2933cdb18761SDaniel Lezcano 	if (ret)
2934cdb18761SDaniel Lezcano 		goto fib6_rules_init;
29358ed67789SDaniel Lezcano 
2936433d49c3SDaniel Lezcano out:
2937433d49c3SDaniel Lezcano 	return ret;
2938433d49c3SDaniel Lezcano 
2939433d49c3SDaniel Lezcano fib6_rules_init:
2940433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
2941433d49c3SDaniel Lezcano xfrm6_init:
2942433d49c3SDaniel Lezcano 	xfrm6_fini();
2943433d49c3SDaniel Lezcano out_fib6_init:
2944433d49c3SDaniel Lezcano 	fib6_gc_cleanup();
29458ed67789SDaniel Lezcano out_register_subsys:
29468ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
2947fc66f95cSEric Dumazet out_dst_entries:
2948fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
2949433d49c3SDaniel Lezcano out_kmem_cache:
2950f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
2951433d49c3SDaniel Lezcano 	goto out;
29521da177e4SLinus Torvalds }
29531da177e4SLinus Torvalds 
29541da177e4SLinus Torvalds void ip6_route_cleanup(void)
29551da177e4SLinus Torvalds {
29568ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
2957101367c2SThomas Graf 	fib6_rules_cleanup();
29581da177e4SLinus Torvalds 	xfrm6_fini();
29591da177e4SLinus Torvalds 	fib6_gc_cleanup();
29608ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
296141bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
2962f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
29631da177e4SLinus Torvalds }
2964