xref: /openbmc/linux/net/ipv6/route.c (revision 2774c131b1d19920b4587db1cfbd6f0750ad1f15)
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,
92efa2cea0SDaniel Lezcano 					   struct in6_addr *prefix, int prefixlen,
9370ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex,
9470ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref);
95efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
96efa2cea0SDaniel Lezcano 					   struct in6_addr *prefix, int prefixlen,
9770ceb4f5SYOSHIFUJI Hideaki 					   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 
15614e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
15714e50e57SDavid S. Miller 	.family			=	AF_INET6,
15809640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
15914e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
16014e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
161ec831ea7SRoland Dreier 	.default_mtu		=	ip6_blackhole_default_mtu,
162214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
16314e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
16414e50e57SDavid S. Miller };
16514e50e57SDavid S. Miller 
16662fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
16762fa8a84SDavid S. Miller 	[RTAX_HOPLIMIT - 1] = 255,
16862fa8a84SDavid S. Miller };
16962fa8a84SDavid S. Miller 
170bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = {
1711da177e4SLinus Torvalds 	.dst = {
1721da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
1731da177e4SLinus Torvalds 		.__use		= 1,
1741da177e4SLinus Torvalds 		.obsolete	= -1,
1751da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
1761da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
1771da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
1781da177e4SLinus Torvalds 	},
1791da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
1804f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
1811da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
1821da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
1831da177e4SLinus Torvalds };
1841da177e4SLinus Torvalds 
185101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
186101367c2SThomas Graf 
1876723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
1886723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
1896723ab54SDavid S. Miller 
190280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = {
191101367c2SThomas Graf 	.dst = {
192101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
193101367c2SThomas Graf 		.__use		= 1,
194101367c2SThomas Graf 		.obsolete	= -1,
195101367c2SThomas Graf 		.error		= -EACCES,
1969ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
1979ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
198101367c2SThomas Graf 	},
199101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2004f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
201101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
202101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
203101367c2SThomas Graf };
204101367c2SThomas Graf 
205bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = {
206101367c2SThomas Graf 	.dst = {
207101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
208101367c2SThomas Graf 		.__use		= 1,
209101367c2SThomas Graf 		.obsolete	= -1,
210101367c2SThomas Graf 		.error		= -EINVAL,
211352e512cSHerbert Xu 		.input		= dst_discard,
212352e512cSHerbert Xu 		.output		= dst_discard,
213101367c2SThomas Graf 	},
214101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2154f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
216101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
217101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
218101367c2SThomas Graf };
219101367c2SThomas Graf 
220101367c2SThomas Graf #endif
221101367c2SThomas Graf 
2221da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
223f2fc6a54SBenjamin Thery static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops)
2241da177e4SLinus Torvalds {
2253c7bd1a1SDavid S. Miller 	return (struct rt6_info *)dst_alloc(ops, 0);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2311da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
232b3419363SDavid S. Miller 	struct inet_peer *peer = rt->rt6i_peer;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	if (idev != NULL) {
2351da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2361da177e4SLinus Torvalds 		in6_dev_put(idev);
2371da177e4SLinus Torvalds 	}
238b3419363SDavid S. Miller 	if (peer) {
239b3419363SDavid S. Miller 		rt->rt6i_peer = NULL;
240b3419363SDavid S. Miller 		inet_putpeer(peer);
241b3419363SDavid S. Miller 	}
242b3419363SDavid S. Miller }
243b3419363SDavid S. Miller 
2446431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
2456431cbc2SDavid S. Miller 
2466431cbc2SDavid S. Miller static u32 rt6_peer_genid(void)
2476431cbc2SDavid S. Miller {
2486431cbc2SDavid S. Miller 	return atomic_read(&__rt6_peer_genid);
2496431cbc2SDavid S. Miller }
2506431cbc2SDavid S. Miller 
251b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
252b3419363SDavid S. Miller {
253b3419363SDavid S. Miller 	struct inet_peer *peer;
254b3419363SDavid S. Miller 
255b3419363SDavid S. Miller 	peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
256b3419363SDavid S. Miller 	if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
257b3419363SDavid S. Miller 		inet_putpeer(peer);
2586431cbc2SDavid S. Miller 	else
2596431cbc2SDavid S. Miller 		rt->rt6i_peer_genid = rt6_peer_genid();
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
2631da177e4SLinus Torvalds 			   int how)
2641da177e4SLinus Torvalds {
2651da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2661da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2675a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
268c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
2691da177e4SLinus Torvalds 
2705a3e55d6SDenis V. Lunev 	if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
2715a3e55d6SDenis V. Lunev 		struct inet6_dev *loopback_idev =
2725a3e55d6SDenis V. Lunev 			in6_dev_get(loopback_dev);
2731da177e4SLinus Torvalds 		if (loopback_idev != NULL) {
2741da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
2751da177e4SLinus Torvalds 			in6_dev_put(idev);
2761da177e4SLinus Torvalds 		}
2771da177e4SLinus Torvalds 	}
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds static __inline__ int rt6_check_expired(const struct rt6_info *rt)
2811da177e4SLinus Torvalds {
282a02cec21SEric Dumazet 	return (rt->rt6i_flags & RTF_EXPIRES) &&
283a02cec21SEric Dumazet 		time_after(jiffies, rt->rt6i_expires);
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
286c71099acSThomas Graf static inline int rt6_need_strict(struct in6_addr *daddr)
287c71099acSThomas Graf {
288a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
289a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
290c71099acSThomas Graf }
291c71099acSThomas Graf 
2921da177e4SLinus Torvalds /*
293c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
2941da177e4SLinus Torvalds  */
2951da177e4SLinus Torvalds 
2968ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
2978ed67789SDaniel Lezcano 						    struct rt6_info *rt,
298dd3abc4eSYOSHIFUJI Hideaki 						    struct in6_addr *saddr,
2991da177e4SLinus Torvalds 						    int oif,
300d420895eSYOSHIFUJI Hideaki 						    int flags)
3011da177e4SLinus Torvalds {
3021da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
3031da177e4SLinus Torvalds 	struct rt6_info *sprt;
3041da177e4SLinus Torvalds 
305dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
306dd3abc4eSYOSHIFUJI Hideaki 		goto out;
307dd3abc4eSYOSHIFUJI Hideaki 
308d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
3091da177e4SLinus Torvalds 		struct net_device *dev = sprt->rt6i_dev;
310dd3abc4eSYOSHIFUJI Hideaki 
311dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
3121da177e4SLinus Torvalds 			if (dev->ifindex == oif)
3131da177e4SLinus Torvalds 				return sprt;
3141da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
3151da177e4SLinus Torvalds 				if (sprt->rt6i_idev == NULL ||
3161da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
317d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
3181da177e4SLinus Torvalds 						continue;
3191da177e4SLinus Torvalds 					if (local && (!oif ||
3201da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
3211da177e4SLinus Torvalds 						continue;
3221da177e4SLinus Torvalds 				}
3231da177e4SLinus Torvalds 				local = sprt;
3241da177e4SLinus Torvalds 			}
325dd3abc4eSYOSHIFUJI Hideaki 		} else {
326dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
327dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
328dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
329dd3abc4eSYOSHIFUJI Hideaki 		}
3301da177e4SLinus Torvalds 	}
3311da177e4SLinus Torvalds 
332dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
3331da177e4SLinus Torvalds 		if (local)
3341da177e4SLinus Torvalds 			return local;
3351da177e4SLinus Torvalds 
336d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
3378ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
3381da177e4SLinus Torvalds 	}
339dd3abc4eSYOSHIFUJI Hideaki out:
3401da177e4SLinus Torvalds 	return rt;
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
34327097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
34427097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
34527097255SYOSHIFUJI Hideaki {
34627097255SYOSHIFUJI Hideaki 	struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
34727097255SYOSHIFUJI Hideaki 	/*
34827097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
34927097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
35027097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
35127097255SYOSHIFUJI Hideaki 	 *
35227097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
35327097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
35427097255SYOSHIFUJI Hideaki 	 */
35527097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
35627097255SYOSHIFUJI Hideaki 		return;
35727097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
35827097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
35952e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
36027097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
36127097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
36227097255SYOSHIFUJI Hideaki 
36327097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
36427097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
36527097255SYOSHIFUJI Hideaki 
36627097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
36727097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
36827097255SYOSHIFUJI Hideaki 		ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
36927097255SYOSHIFUJI Hideaki 	} else
37027097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
37127097255SYOSHIFUJI Hideaki }
37227097255SYOSHIFUJI Hideaki #else
37327097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
37427097255SYOSHIFUJI Hideaki {
37527097255SYOSHIFUJI Hideaki }
37627097255SYOSHIFUJI Hideaki #endif
37727097255SYOSHIFUJI Hideaki 
3781da177e4SLinus Torvalds /*
379554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
3801da177e4SLinus Torvalds  */
381b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
3821da177e4SLinus Torvalds {
383554cfb7eSYOSHIFUJI Hideaki 	struct net_device *dev = rt->rt6i_dev;
384161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
385554cfb7eSYOSHIFUJI Hideaki 		return 2;
386161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
387161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
388161980f4SDavid S. Miller 		return 1;
389554cfb7eSYOSHIFUJI Hideaki 	return 0;
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
392b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
3931da177e4SLinus Torvalds {
394554cfb7eSYOSHIFUJI Hideaki 	struct neighbour *neigh = rt->rt6i_nexthop;
395398bcbebSYOSHIFUJI Hideaki 	int m;
3964d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
3974d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
3984d0c5911SYOSHIFUJI Hideaki 		m = 1;
3994d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
4001da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
401554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
4024d0c5911SYOSHIFUJI Hideaki 			m = 2;
403398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
404398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
405398bcbebSYOSHIFUJI Hideaki 			m = 0;
406398bcbebSYOSHIFUJI Hideaki #endif
407398bcbebSYOSHIFUJI Hideaki 		else
408ea73ee23SYOSHIFUJI Hideaki 			m = 1;
4091da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
410398bcbebSYOSHIFUJI Hideaki 	} else
411398bcbebSYOSHIFUJI Hideaki 		m = 0;
412554cfb7eSYOSHIFUJI Hideaki 	return m;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
415554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
416554cfb7eSYOSHIFUJI Hideaki 			   int strict)
417554cfb7eSYOSHIFUJI Hideaki {
4184d0c5911SYOSHIFUJI Hideaki 	int m, n;
4194d0c5911SYOSHIFUJI Hideaki 
4204d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
42177d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
422554cfb7eSYOSHIFUJI Hideaki 		return -1;
423ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
424ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
425ebacaaa0SYOSHIFUJI Hideaki #endif
4264d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
427557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
428554cfb7eSYOSHIFUJI Hideaki 		return -1;
429554cfb7eSYOSHIFUJI Hideaki 	return m;
430554cfb7eSYOSHIFUJI Hideaki }
431554cfb7eSYOSHIFUJI Hideaki 
432f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
433f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
434554cfb7eSYOSHIFUJI Hideaki {
435554cfb7eSYOSHIFUJI Hideaki 	int m;
436554cfb7eSYOSHIFUJI Hideaki 
437554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
438f11e6659SDavid S. Miller 		goto out;
439554cfb7eSYOSHIFUJI Hideaki 
440554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
441554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
442f11e6659SDavid S. Miller 		goto out;
443554cfb7eSYOSHIFUJI Hideaki 
444f11e6659SDavid S. Miller 	if (m > *mpri) {
445ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
44627097255SYOSHIFUJI Hideaki 			rt6_probe(match);
447f11e6659SDavid S. Miller 		*mpri = m;
448554cfb7eSYOSHIFUJI Hideaki 		match = rt;
449ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
45027097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
4511da177e4SLinus Torvalds 	}
452f11e6659SDavid S. Miller 
453f11e6659SDavid S. Miller out:
454f11e6659SDavid S. Miller 	return match;
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds 
457f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
458f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
459f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
460f11e6659SDavid S. Miller {
461f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
462f11e6659SDavid S. Miller 	int mpri = -1;
463f11e6659SDavid S. Miller 
464f11e6659SDavid S. Miller 	match = NULL;
465f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
466d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
467f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
468f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
469d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
470f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
471f11e6659SDavid S. Miller 
472f11e6659SDavid S. Miller 	return match;
473f11e6659SDavid S. Miller }
474f11e6659SDavid S. Miller 
475f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
476f11e6659SDavid S. Miller {
477f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
4788ed67789SDaniel Lezcano 	struct net *net;
479f11e6659SDavid S. Miller 
480f11e6659SDavid S. Miller 	RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
4810dc47877SHarvey Harrison 		  __func__, fn->leaf, oif);
482f11e6659SDavid S. Miller 
483f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
484f11e6659SDavid S. Miller 	if (!rt0)
485f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
486f11e6659SDavid S. Miller 
487f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
488f11e6659SDavid S. Miller 
489554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
490f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
491d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
492f11e6659SDavid S. Miller 
493554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
494f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
495f11e6659SDavid S. Miller 			next = fn->leaf;
496f11e6659SDavid S. Miller 
497f11e6659SDavid S. Miller 		if (next != rt0)
498f11e6659SDavid S. Miller 			fn->rr_ptr = next;
499554cfb7eSYOSHIFUJI Hideaki 	}
500554cfb7eSYOSHIFUJI Hideaki 
501f11e6659SDavid S. Miller 	RT6_TRACE("%s() => %p\n",
5020dc47877SHarvey Harrison 		  __func__, match);
503554cfb7eSYOSHIFUJI Hideaki 
504c346dca1SYOSHIFUJI Hideaki 	net = dev_net(rt0->rt6i_dev);
505a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
50870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
50970ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
51070ceb4f5SYOSHIFUJI Hideaki 		  struct in6_addr *gwaddr)
51170ceb4f5SYOSHIFUJI Hideaki {
512c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
51370ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
51470ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
51570ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
5164bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
51770ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
51870ceb4f5SYOSHIFUJI Hideaki 
51970ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
52070ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
52170ceb4f5SYOSHIFUJI Hideaki 	}
52270ceb4f5SYOSHIFUJI Hideaki 
52370ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
52470ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
52570ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
52670ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
52770ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
52870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
52970ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
53070ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
53170ceb4f5SYOSHIFUJI Hideaki 		}
53270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
53370ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
53470ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
53570ceb4f5SYOSHIFUJI Hideaki 		}
53670ceb4f5SYOSHIFUJI Hideaki 	}
53770ceb4f5SYOSHIFUJI Hideaki 
53870ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
53970ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
5403933fc95SJens Rosenboom 		return -EINVAL;
54170ceb4f5SYOSHIFUJI Hideaki 
5424bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
54370ceb4f5SYOSHIFUJI Hideaki 
54470ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
54570ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
54670ceb4f5SYOSHIFUJI Hideaki 	else {
54770ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
54870ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
54970ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
55070ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
55170ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
55270ceb4f5SYOSHIFUJI Hideaki 	}
55370ceb4f5SYOSHIFUJI Hideaki 
554efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
555efa2cea0SDaniel Lezcano 				dev->ifindex);
55670ceb4f5SYOSHIFUJI Hideaki 
55770ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
558e0a1ad73SThomas Graf 		ip6_del_rt(rt);
55970ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
56070ceb4f5SYOSHIFUJI Hideaki 	}
56170ceb4f5SYOSHIFUJI Hideaki 
56270ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
563efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
56470ceb4f5SYOSHIFUJI Hideaki 					pref);
56570ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
56670ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
56770ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
56870ceb4f5SYOSHIFUJI Hideaki 
56970ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
5704bed72e4SYOSHIFUJI Hideaki 		if (!addrconf_finite_timeout(lifetime)) {
57170ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags &= ~RTF_EXPIRES;
57270ceb4f5SYOSHIFUJI Hideaki 		} else {
57370ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_expires = jiffies + HZ * lifetime;
57470ceb4f5SYOSHIFUJI Hideaki 			rt->rt6i_flags |= RTF_EXPIRES;
57570ceb4f5SYOSHIFUJI Hideaki 		}
576d8d1f30bSChangli Gao 		dst_release(&rt->dst);
57770ceb4f5SYOSHIFUJI Hideaki 	}
57870ceb4f5SYOSHIFUJI Hideaki 	return 0;
57970ceb4f5SYOSHIFUJI Hideaki }
58070ceb4f5SYOSHIFUJI Hideaki #endif
58170ceb4f5SYOSHIFUJI Hideaki 
5828ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
583982f56f3SYOSHIFUJI Hideaki do { \
5848ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
585982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
586e0eda7bbSVille Nuorvala 		while (1) { \
587982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
588c71099acSThomas Graf 				goto out; \
589982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
590982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
5918bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
592982f56f3SYOSHIFUJI Hideaki 			else \
593982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
594c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
595c71099acSThomas Graf 				goto restart; \
596c71099acSThomas Graf 		} \
597982f56f3SYOSHIFUJI Hideaki 	} \
598982f56f3SYOSHIFUJI Hideaki } while(0)
599c71099acSThomas Graf 
6008ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
6018ed67789SDaniel Lezcano 					     struct fib6_table *table,
602c71099acSThomas Graf 					     struct flowi *fl, int flags)
6031da177e4SLinus Torvalds {
6041da177e4SLinus Torvalds 	struct fib6_node *fn;
6051da177e4SLinus Torvalds 	struct rt6_info *rt;
6061da177e4SLinus Torvalds 
607c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
608c71099acSThomas Graf 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
609c71099acSThomas Graf restart:
610c71099acSThomas Graf 	rt = fn->leaf;
611dd3abc4eSYOSHIFUJI Hideaki 	rt = rt6_device_match(net, rt, &fl->fl6_src, fl->oif, flags);
6128ed67789SDaniel Lezcano 	BACKTRACK(net, &fl->fl6_src);
613c71099acSThomas Graf out:
614d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
615c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
6161da177e4SLinus Torvalds 	return rt;
617c71099acSThomas Graf 
618c71099acSThomas Graf }
619c71099acSThomas Graf 
6209acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
6219acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
622c71099acSThomas Graf {
623c71099acSThomas Graf 	struct flowi fl = {
624c71099acSThomas Graf 		.oif = oif,
6255811662bSChangli Gao 		.fl6_dst = *daddr,
626c71099acSThomas Graf 	};
627c71099acSThomas Graf 	struct dst_entry *dst;
62877d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
629c71099acSThomas Graf 
630adaa70bbSThomas Graf 	if (saddr) {
631adaa70bbSThomas Graf 		memcpy(&fl.fl6_src, saddr, sizeof(*saddr));
632adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
633adaa70bbSThomas Graf 	}
634adaa70bbSThomas Graf 
635606a2b48SDaniel Lezcano 	dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_lookup);
636c71099acSThomas Graf 	if (dst->error == 0)
637c71099acSThomas Graf 		return (struct rt6_info *) dst;
638c71099acSThomas Graf 
639c71099acSThomas Graf 	dst_release(dst);
640c71099acSThomas Graf 
6411da177e4SLinus Torvalds 	return NULL;
6421da177e4SLinus Torvalds }
6431da177e4SLinus Torvalds 
6447159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
6457159039aSYOSHIFUJI Hideaki 
646c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
6471da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
6481da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
6491da177e4SLinus Torvalds    be destroyed.
6501da177e4SLinus Torvalds  */
6511da177e4SLinus Torvalds 
65286872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
6531da177e4SLinus Torvalds {
6541da177e4SLinus Torvalds 	int err;
655c71099acSThomas Graf 	struct fib6_table *table;
6561da177e4SLinus Torvalds 
657c71099acSThomas Graf 	table = rt->rt6i_table;
658c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
65986872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
660c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	return err;
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
66540e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
66640e22e8fSThomas Graf {
6674d1169c1SDenis V. Lunev 	struct nl_info info = {
668c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
6694d1169c1SDenis V. Lunev 	};
670528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
67140e22e8fSThomas Graf }
67240e22e8fSThomas Graf 
67395a9a5baSYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
67495a9a5baSYOSHIFUJI Hideaki 				      struct in6_addr *saddr)
6751da177e4SLinus Torvalds {
6761da177e4SLinus Torvalds 	struct rt6_info *rt;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	/*
6791da177e4SLinus Torvalds 	 *	Clone the route.
6801da177e4SLinus Torvalds 	 */
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	rt = ip6_rt_copy(ort);
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	if (rt) {
68514deae41SDavid S. Miller 		struct neighbour *neigh;
68614deae41SDavid S. Miller 		int attempts = !in_softirq();
68714deae41SDavid S. Miller 
68858c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags&RTF_GATEWAY)) {
68958c4fb86SYOSHIFUJI Hideaki 			if (rt->rt6i_dst.plen != 128 &&
69058c4fb86SYOSHIFUJI Hideaki 			    ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
69158c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
6921da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_gateway, daddr);
69358c4fb86SYOSHIFUJI Hideaki 		}
6941da177e4SLinus Torvalds 
69558c4fb86SYOSHIFUJI Hideaki 		ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
6961da177e4SLinus Torvalds 		rt->rt6i_dst.plen = 128;
6971da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
698d8d1f30bSChangli Gao 		rt->dst.flags |= DST_HOST;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
7011da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
7021da177e4SLinus Torvalds 			ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
7031da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
7041da177e4SLinus Torvalds 		}
7051da177e4SLinus Torvalds #endif
7061da177e4SLinus Torvalds 
70714deae41SDavid S. Miller 	retry:
70814deae41SDavid S. Miller 		neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
70914deae41SDavid S. Miller 		if (IS_ERR(neigh)) {
71014deae41SDavid S. Miller 			struct net *net = dev_net(rt->rt6i_dev);
71114deae41SDavid S. Miller 			int saved_rt_min_interval =
71214deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval;
71314deae41SDavid S. Miller 			int saved_rt_elasticity =
71414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity;
71514deae41SDavid S. Miller 
71614deae41SDavid S. Miller 			if (attempts-- > 0) {
71714deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
71814deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
71914deae41SDavid S. Miller 
72086393e52SAlexey Dobriyan 				ip6_dst_gc(&net->ipv6.ip6_dst_ops);
72114deae41SDavid S. Miller 
72214deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity =
72314deae41SDavid S. Miller 					saved_rt_elasticity;
72414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval =
72514deae41SDavid S. Miller 					saved_rt_min_interval;
72614deae41SDavid S. Miller 				goto retry;
72714deae41SDavid S. Miller 			}
72814deae41SDavid S. Miller 
72914deae41SDavid S. Miller 			if (net_ratelimit())
73014deae41SDavid S. Miller 				printk(KERN_WARNING
7317e1b33e5SUlrich Weber 				       "ipv6: Neighbour table overflow.\n");
732d8d1f30bSChangli Gao 			dst_free(&rt->dst);
73314deae41SDavid S. Miller 			return NULL;
73414deae41SDavid S. Miller 		}
73514deae41SDavid S. Miller 		rt->rt6i_nexthop = neigh;
7361da177e4SLinus Torvalds 
73795a9a5baSYOSHIFUJI Hideaki 	}
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	return rt;
7401da177e4SLinus Torvalds }
74195a9a5baSYOSHIFUJI Hideaki 
742299d9939SYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
743299d9939SYOSHIFUJI Hideaki {
744299d9939SYOSHIFUJI Hideaki 	struct rt6_info *rt = ip6_rt_copy(ort);
745299d9939SYOSHIFUJI Hideaki 	if (rt) {
746299d9939SYOSHIFUJI Hideaki 		ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
747299d9939SYOSHIFUJI Hideaki 		rt->rt6i_dst.plen = 128;
748299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
749d8d1f30bSChangli Gao 		rt->dst.flags |= DST_HOST;
750299d9939SYOSHIFUJI Hideaki 		rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
751299d9939SYOSHIFUJI Hideaki 	}
752299d9939SYOSHIFUJI Hideaki 	return rt;
753299d9939SYOSHIFUJI Hideaki }
754299d9939SYOSHIFUJI Hideaki 
7558ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
7568ce11e6aSAdrian Bunk 				      struct flowi *fl, int flags)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	struct fib6_node *fn;
759519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
760c71099acSThomas Graf 	int strict = 0;
7611da177e4SLinus Torvalds 	int attempts = 3;
762519fbd87SYOSHIFUJI Hideaki 	int err;
76353b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
7641da177e4SLinus Torvalds 
76577d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds relookup:
768c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7691da177e4SLinus Torvalds 
7708238dd06SYOSHIFUJI Hideaki restart_2:
771c71099acSThomas Graf 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds restart:
7744acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
7758ed67789SDaniel Lezcano 
7768ed67789SDaniel Lezcano 	BACKTRACK(net, &fl->fl6_src);
7778ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
7788238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
7791da177e4SLinus Torvalds 		goto out;
7801da177e4SLinus Torvalds 
781d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
782c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7831da177e4SLinus Torvalds 
784519fbd87SYOSHIFUJI Hideaki 	if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
785e40cf353SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
786d80bc0fdSDavid S. Miller 	else
787519fbd87SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
7881da177e4SLinus Torvalds 
789d8d1f30bSChangli Gao 	dst_release(&rt->dst);
7908ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
7911da177e4SLinus Torvalds 
792d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
793e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
79440e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
795e40cf353SYOSHIFUJI Hideaki 		if (!err)
796e40cf353SYOSHIFUJI Hideaki 			goto out2;
797e40cf353SYOSHIFUJI Hideaki 	}
798e40cf353SYOSHIFUJI Hideaki 
799e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
8001da177e4SLinus Torvalds 		goto out2;
8011da177e4SLinus Torvalds 
802519fbd87SYOSHIFUJI Hideaki 	/*
803c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
804519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
8051da177e4SLinus Torvalds 	 */
806d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8071da177e4SLinus Torvalds 	goto relookup;
808e40cf353SYOSHIFUJI Hideaki 
809519fbd87SYOSHIFUJI Hideaki out:
8108238dd06SYOSHIFUJI Hideaki 	if (reachable) {
8118238dd06SYOSHIFUJI Hideaki 		reachable = 0;
8128238dd06SYOSHIFUJI Hideaki 		goto restart_2;
8138238dd06SYOSHIFUJI Hideaki 	}
814d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
815c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8161da177e4SLinus Torvalds out2:
817d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
818d8d1f30bSChangli Gao 	rt->dst.__use++;
819c71099acSThomas Graf 
820c71099acSThomas Graf 	return rt;
821c71099acSThomas Graf }
822c71099acSThomas Graf 
8238ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
8244acad72dSPavel Emelyanov 					    struct flowi *fl, int flags)
8254acad72dSPavel Emelyanov {
8268ed67789SDaniel Lezcano 	return ip6_pol_route(net, table, fl->iif, fl, flags);
8274acad72dSPavel Emelyanov }
8284acad72dSPavel Emelyanov 
829c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
830c71099acSThomas Graf {
8310660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *iph = ipv6_hdr(skb);
832c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
833adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
834c71099acSThomas Graf 	struct flowi fl = {
835c71099acSThomas Graf 		.iif = skb->dev->ifindex,
8365811662bSChangli Gao 		.fl6_dst = iph->daddr,
8375811662bSChangli Gao 		.fl6_src = iph->saddr,
8385811662bSChangli Gao 		.fl6_flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
83947dcf0cbSThomas Graf 		.mark = skb->mark,
840c71099acSThomas Graf 		.proto = iph->nexthdr,
841c71099acSThomas Graf 	};
842adaa70bbSThomas Graf 
8431d6e55f1SThomas Goff 	if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
844adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
845c71099acSThomas Graf 
846adf30907SEric Dumazet 	skb_dst_set(skb, fib6_rule_lookup(net, &fl, flags, ip6_pol_route_input));
847c71099acSThomas Graf }
848c71099acSThomas Graf 
8498ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
850c71099acSThomas Graf 					     struct flowi *fl, int flags)
851c71099acSThomas Graf {
8528ed67789SDaniel Lezcano 	return ip6_pol_route(net, table, fl->oif, fl, flags);
853c71099acSThomas Graf }
854c71099acSThomas Graf 
8554591db4fSDaniel Lezcano struct dst_entry * ip6_route_output(struct net *net, struct sock *sk,
8564591db4fSDaniel Lezcano 				    struct flowi *fl)
857c71099acSThomas Graf {
858c71099acSThomas Graf 	int flags = 0;
859c71099acSThomas Graf 
8606057fd78SBrian Haley 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl->fl6_dst))
86177d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
862c71099acSThomas Graf 
863adaa70bbSThomas Graf 	if (!ipv6_addr_any(&fl->fl6_src))
864adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
8650c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
8660c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
867adaa70bbSThomas Graf 
8684591db4fSDaniel Lezcano 	return fib6_rule_lookup(net, fl, flags, ip6_pol_route_output);
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds 
8717159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
8721da177e4SLinus Torvalds 
873*2774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
87414e50e57SDavid S. Miller {
87569ead7afSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&ip6_dst_blackhole_ops, 1);
87669ead7afSDavid S. Miller 	struct rt6_info *ort = (struct rt6_info *) dst_orig;
87714e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
87814e50e57SDavid S. Miller 
87914e50e57SDavid S. Miller 	if (rt) {
880d8d1f30bSChangli Gao 		new = &rt->dst;
88114e50e57SDavid S. Miller 
88214e50e57SDavid S. Miller 		new->__use = 1;
883352e512cSHerbert Xu 		new->input = dst_discard;
884352e512cSHerbert Xu 		new->output = dst_discard;
88514e50e57SDavid S. Miller 
886defb3519SDavid S. Miller 		dst_copy_metrics(new, &ort->dst);
887d8d1f30bSChangli Gao 		new->dev = ort->dst.dev;
88814e50e57SDavid S. Miller 		if (new->dev)
88914e50e57SDavid S. Miller 			dev_hold(new->dev);
89014e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
89114e50e57SDavid S. Miller 		if (rt->rt6i_idev)
89214e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
89314e50e57SDavid S. Miller 		rt->rt6i_expires = 0;
89414e50e57SDavid S. Miller 
89514e50e57SDavid S. Miller 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
89614e50e57SDavid S. Miller 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
89714e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
89814e50e57SDavid S. Miller 
89914e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
90014e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
90114e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
90214e50e57SDavid S. Miller #endif
90314e50e57SDavid S. Miller 
90414e50e57SDavid S. Miller 		dst_free(new);
90514e50e57SDavid S. Miller 	}
90614e50e57SDavid S. Miller 
90769ead7afSDavid S. Miller 	dst_release(dst_orig);
90869ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
90914e50e57SDavid S. Miller }
91014e50e57SDavid S. Miller 
9111da177e4SLinus Torvalds /*
9121da177e4SLinus Torvalds  *	Destination cache support functions
9131da177e4SLinus Torvalds  */
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
9161da177e4SLinus Torvalds {
9171da177e4SLinus Torvalds 	struct rt6_info *rt;
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
9201da177e4SLinus Torvalds 
9216431cbc2SDavid S. Miller 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
9226431cbc2SDavid S. Miller 		if (rt->rt6i_peer_genid != rt6_peer_genid()) {
9236431cbc2SDavid S. Miller 			if (!rt->rt6i_peer)
9246431cbc2SDavid S. Miller 				rt6_bind_peer(rt, 0);
9256431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
9266431cbc2SDavid S. Miller 		}
9271da177e4SLinus Torvalds 		return dst;
9286431cbc2SDavid S. Miller 	}
9291da177e4SLinus Torvalds 	return NULL;
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
9331da177e4SLinus Torvalds {
9341da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	if (rt) {
93754c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
93854c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
939e0a1ad73SThomas Graf 				ip6_del_rt(rt);
94054c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
9411da177e4SLinus Torvalds 			}
94254c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
94354c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
94454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
94554c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
94654c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
94754c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
9511da177e4SLinus Torvalds {
9521da177e4SLinus Torvalds 	struct rt6_info *rt;
9531da177e4SLinus Torvalds 
9543ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
9551da177e4SLinus Torvalds 
956adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
9571da177e4SLinus Torvalds 	if (rt) {
9581da177e4SLinus Torvalds 		if (rt->rt6i_flags&RTF_CACHE) {
959d8d1f30bSChangli Gao 			dst_set_expires(&rt->dst, 0);
9601da177e4SLinus Torvalds 			rt->rt6i_flags |= RTF_EXPIRES;
9611da177e4SLinus Torvalds 		} else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
9621da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds }
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
9671da177e4SLinus Torvalds {
9681da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
9711da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
9721da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
973defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
9741da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
975defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
976defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
9771da177e4SLinus Torvalds 		}
978defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
9791da177e4SLinus Torvalds 	}
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
9820dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
9831da177e4SLinus Torvalds {
9840dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
9850dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
9860dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
9870dbaee3bSDavid S. Miller 
9881da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
9891da177e4SLinus Torvalds 
9905578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
9915578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	/*
9941da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
9951da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
9961da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
9971da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
9981da177e4SLinus Torvalds 	 */
9991da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
10001da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
10011da177e4SLinus Torvalds 	return mtu;
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds 
1004d33e4553SDavid S. Miller static unsigned int ip6_default_mtu(const struct dst_entry *dst)
1005d33e4553SDavid S. Miller {
1006d33e4553SDavid S. Miller 	unsigned int mtu = IPV6_MIN_MTU;
1007d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1008d33e4553SDavid S. Miller 
1009d33e4553SDavid S. Miller 	rcu_read_lock();
1010d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1011d33e4553SDavid S. Miller 	if (idev)
1012d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1013d33e4553SDavid S. Miller 	rcu_read_unlock();
1014d33e4553SDavid S. Miller 
1015d33e4553SDavid S. Miller 	return mtu;
1016d33e4553SDavid S. Miller }
1017d33e4553SDavid S. Miller 
10183b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
10193b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
10205d0bbeebSThomas Graf 
10213b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
10221da177e4SLinus Torvalds 				  struct neighbour *neigh,
10239acd9f3aSYOSHIFUJI Hideaki 				  const struct in6_addr *addr)
10241da177e4SLinus Torvalds {
10251da177e4SLinus Torvalds 	struct rt6_info *rt;
10261da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1027c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	if (unlikely(idev == NULL))
10301da177e4SLinus Torvalds 		return NULL;
10311da177e4SLinus Torvalds 
103286393e52SAlexey Dobriyan 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
10331da177e4SLinus Torvalds 	if (unlikely(rt == NULL)) {
10341da177e4SLinus Torvalds 		in6_dev_put(idev);
10351da177e4SLinus Torvalds 		goto out;
10361da177e4SLinus Torvalds 	}
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	dev_hold(dev);
10391da177e4SLinus Torvalds 	if (neigh)
10401da177e4SLinus Torvalds 		neigh_hold(neigh);
104114deae41SDavid S. Miller 	else {
10421da177e4SLinus Torvalds 		neigh = ndisc_get_neigh(dev, addr);
104314deae41SDavid S. Miller 		if (IS_ERR(neigh))
104414deae41SDavid S. Miller 			neigh = NULL;
104514deae41SDavid S. Miller 	}
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	rt->rt6i_dev	  = dev;
10481da177e4SLinus Torvalds 	rt->rt6i_idev     = idev;
10491da177e4SLinus Torvalds 	rt->rt6i_nexthop  = neigh;
1050d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
1051defb3519SDavid S. Miller 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
1052d8d1f30bSChangli Gao 	rt->dst.output  = ip6_output;
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds #if 0	/* there's no chance to use these for ndisc */
1055d8d1f30bSChangli Gao 	rt->dst.flags   = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
10561da177e4SLinus Torvalds 				? DST_HOST
10571da177e4SLinus Torvalds 				: 0;
10581da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
10591da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
10601da177e4SLinus Torvalds #endif
10611da177e4SLinus Torvalds 
10623b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1063d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1064d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
10653b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
10661da177e4SLinus Torvalds 
10675578689aSDaniel Lezcano 	fib6_force_start_gc(net);
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds out:
1070d8d1f30bSChangli Gao 	return &rt->dst;
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
10733d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
10741da177e4SLinus Torvalds {
1075e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
10763d0f24a7SStephen Hemminger 	int more = 0;
10771da177e4SLinus Torvalds 
10783b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
10793b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
10805d0bbeebSThomas Graf 
10811da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
10821da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
10831da177e4SLinus Torvalds 			*pprev = dst->next;
10841da177e4SLinus Torvalds 			dst_free(dst);
10851da177e4SLinus Torvalds 		} else {
10861da177e4SLinus Torvalds 			pprev = &dst->next;
10873d0f24a7SStephen Hemminger 			++more;
10881da177e4SLinus Torvalds 		}
10891da177e4SLinus Torvalds 	}
10901da177e4SLinus Torvalds 
10913b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
10925d0bbeebSThomas Graf 
10933d0f24a7SStephen Hemminger 	return more;
10941da177e4SLinus Torvalds }
10951da177e4SLinus Torvalds 
10961e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
10971e493d19SDavid S. Miller 			    void *arg)
10981e493d19SDavid S. Miller {
10991e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
11001e493d19SDavid S. Miller 
11011e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
11021e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
11031e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
11041e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
11051e493d19SDavid S. Miller 		if (func(rt, arg)) {
11061e493d19SDavid S. Miller 			*pprev = dst->next;
11071e493d19SDavid S. Miller 			dst_free(dst);
11081e493d19SDavid S. Miller 		} else {
11091e493d19SDavid S. Miller 			pprev = &dst->next;
11101e493d19SDavid S. Miller 		}
11111e493d19SDavid S. Miller 	}
11121e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
11131e493d19SDavid S. Miller }
11141e493d19SDavid S. Miller 
1115569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
11161da177e4SLinus Torvalds {
11171da177e4SLinus Torvalds 	unsigned long now = jiffies;
111886393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
11197019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
11207019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
11217019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
11227019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
11237019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1124fc66f95cSEric Dumazet 	int entries;
11251da177e4SLinus Torvalds 
1126fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
11277019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1128fc66f95cSEric Dumazet 	    entries <= rt_max_size)
11291da177e4SLinus Torvalds 		goto out;
11301da177e4SLinus Torvalds 
11316891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
11326891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
11336891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1134fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1135fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
11367019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
11371da177e4SLinus Torvalds out:
11387019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1139fc66f95cSEric Dumazet 	return entries > rt_max_size;
11401da177e4SLinus Torvalds }
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
11431da177e4SLinus Torvalds    but results in cleaner routing tables.
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds    Remove it only when all the things will work!
11461da177e4SLinus Torvalds  */
11471da177e4SLinus Torvalds 
11486b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
11491da177e4SLinus Torvalds {
11505170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1151a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
11526b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1153c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1154c68f24ccSEric Dumazet 
1155c68f24ccSEric Dumazet 		rcu_read_lock();
1156c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1157c68f24ccSEric Dumazet 		if (idev)
11581da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1159c68f24ccSEric Dumazet 		else
116053b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1161c68f24ccSEric Dumazet 		rcu_read_unlock();
11621da177e4SLinus Torvalds 	}
11631da177e4SLinus Torvalds 	return hoplimit;
11641da177e4SLinus Torvalds }
1165abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds /*
11681da177e4SLinus Torvalds  *
11691da177e4SLinus Torvalds  */
11701da177e4SLinus Torvalds 
117186872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
11721da177e4SLinus Torvalds {
11731da177e4SLinus Torvalds 	int err;
11745578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
11751da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
11761da177e4SLinus Torvalds 	struct net_device *dev = NULL;
11771da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1178c71099acSThomas Graf 	struct fib6_table *table;
11791da177e4SLinus Torvalds 	int addr_type;
11801da177e4SLinus Torvalds 
118186872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
11821da177e4SLinus Torvalds 		return -EINVAL;
11831da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
118486872cb5SThomas Graf 	if (cfg->fc_src_len)
11851da177e4SLinus Torvalds 		return -EINVAL;
11861da177e4SLinus Torvalds #endif
118786872cb5SThomas Graf 	if (cfg->fc_ifindex) {
11881da177e4SLinus Torvalds 		err = -ENODEV;
11895578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
11901da177e4SLinus Torvalds 		if (!dev)
11911da177e4SLinus Torvalds 			goto out;
11921da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
11931da177e4SLinus Torvalds 		if (!idev)
11941da177e4SLinus Torvalds 			goto out;
11951da177e4SLinus Torvalds 	}
11961da177e4SLinus Torvalds 
119786872cb5SThomas Graf 	if (cfg->fc_metric == 0)
119886872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
11991da177e4SLinus Torvalds 
12005578689aSDaniel Lezcano 	table = fib6_new_table(net, cfg->fc_table);
1201c71099acSThomas Graf 	if (table == NULL) {
1202c71099acSThomas Graf 		err = -ENOBUFS;
1203c71099acSThomas Graf 		goto out;
1204c71099acSThomas Graf 	}
1205c71099acSThomas Graf 
120686393e52SAlexey Dobriyan 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	if (rt == NULL) {
12091da177e4SLinus Torvalds 		err = -ENOMEM;
12101da177e4SLinus Torvalds 		goto out;
12111da177e4SLinus Torvalds 	}
12121da177e4SLinus Torvalds 
1213d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
12146f704992SYOSHIFUJI Hideaki 	rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
12156f704992SYOSHIFUJI Hideaki 				jiffies + clock_t_to_jiffies(cfg->fc_expires) :
12166f704992SYOSHIFUJI Hideaki 				0;
12171da177e4SLinus Torvalds 
121886872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
121986872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
122086872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
122186872cb5SThomas Graf 
122286872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1225d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1226ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1227ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
12281da177e4SLinus Torvalds 	else
1229d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
12301da177e4SLinus Torvalds 
1231d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
12321da177e4SLinus Torvalds 
123386872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
123486872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
12351da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
1236d8d1f30bSChangli Gao 	       rt->dst.flags = DST_HOST;
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
123986872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
124086872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
12411da177e4SLinus Torvalds #endif
12421da177e4SLinus Torvalds 
124386872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
12461da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
12471da177e4SLinus Torvalds 	 */
124886872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
1249ab79ad14SMaciej Żenczykowski 	    (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1250ab79ad14SMaciej Żenczykowski 					      && !(cfg->fc_flags&RTF_LOCAL))) {
12511da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
12525578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
12531da177e4SLinus Torvalds 			if (dev) {
12541da177e4SLinus Torvalds 				dev_put(dev);
12551da177e4SLinus Torvalds 				in6_dev_put(idev);
12561da177e4SLinus Torvalds 			}
12575578689aSDaniel Lezcano 			dev = net->loopback_dev;
12581da177e4SLinus Torvalds 			dev_hold(dev);
12591da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
12601da177e4SLinus Torvalds 			if (!idev) {
12611da177e4SLinus Torvalds 				err = -ENODEV;
12621da177e4SLinus Torvalds 				goto out;
12631da177e4SLinus Torvalds 			}
12641da177e4SLinus Torvalds 		}
1265d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1266d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
1267d8d1f30bSChangli Gao 		rt->dst.error = -ENETUNREACH;
12681da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
12691da177e4SLinus Torvalds 		goto install_route;
12701da177e4SLinus Torvalds 	}
12711da177e4SLinus Torvalds 
127286872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
12731da177e4SLinus Torvalds 		struct in6_addr *gw_addr;
12741da177e4SLinus Torvalds 		int gwa_type;
12751da177e4SLinus Torvalds 
127686872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
127786872cb5SThomas Graf 		ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
12781da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
12811da177e4SLinus Torvalds 			struct rt6_info *grt;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
12841da177e4SLinus Torvalds 			   addresses as nexthop address.
12851da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
12861da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
12871da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
12881da177e4SLinus Torvalds 			   some exceptions. --ANK
12891da177e4SLinus Torvalds 			 */
12901da177e4SLinus Torvalds 			err = -EINVAL;
12911da177e4SLinus Torvalds 			if (!(gwa_type&IPV6_ADDR_UNICAST))
12921da177e4SLinus Torvalds 				goto out;
12931da177e4SLinus Torvalds 
12945578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
12971da177e4SLinus Torvalds 			if (grt == NULL)
12981da177e4SLinus Torvalds 				goto out;
12991da177e4SLinus Torvalds 			if (dev) {
13001da177e4SLinus Torvalds 				if (dev != grt->rt6i_dev) {
1301d8d1f30bSChangli Gao 					dst_release(&grt->dst);
13021da177e4SLinus Torvalds 					goto out;
13031da177e4SLinus Torvalds 				}
13041da177e4SLinus Torvalds 			} else {
13051da177e4SLinus Torvalds 				dev = grt->rt6i_dev;
13061da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
13071da177e4SLinus Torvalds 				dev_hold(dev);
13081da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
13091da177e4SLinus Torvalds 			}
13101da177e4SLinus Torvalds 			if (!(grt->rt6i_flags&RTF_GATEWAY))
13111da177e4SLinus Torvalds 				err = 0;
1312d8d1f30bSChangli Gao 			dst_release(&grt->dst);
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 			if (err)
13151da177e4SLinus Torvalds 				goto out;
13161da177e4SLinus Torvalds 		}
13171da177e4SLinus Torvalds 		err = -EINVAL;
13181da177e4SLinus Torvalds 		if (dev == NULL || (dev->flags&IFF_LOOPBACK))
13191da177e4SLinus Torvalds 			goto out;
13201da177e4SLinus Torvalds 	}
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	err = -ENODEV;
13231da177e4SLinus Torvalds 	if (dev == NULL)
13241da177e4SLinus Torvalds 		goto out;
13251da177e4SLinus Torvalds 
132686872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
13271da177e4SLinus Torvalds 		rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
13281da177e4SLinus Torvalds 		if (IS_ERR(rt->rt6i_nexthop)) {
13291da177e4SLinus Torvalds 			err = PTR_ERR(rt->rt6i_nexthop);
13301da177e4SLinus Torvalds 			rt->rt6i_nexthop = NULL;
13311da177e4SLinus Torvalds 			goto out;
13321da177e4SLinus Torvalds 		}
13331da177e4SLinus Torvalds 	}
13341da177e4SLinus Torvalds 
133586872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds install_route:
133886872cb5SThomas Graf 	if (cfg->fc_mx) {
133986872cb5SThomas Graf 		struct nlattr *nla;
134086872cb5SThomas Graf 		int remaining;
13411da177e4SLinus Torvalds 
134286872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
13438f4c1f9bSThomas Graf 			int type = nla_type(nla);
134486872cb5SThomas Graf 
134586872cb5SThomas Graf 			if (type) {
134686872cb5SThomas Graf 				if (type > RTAX_MAX) {
13471da177e4SLinus Torvalds 					err = -EINVAL;
13481da177e4SLinus Torvalds 					goto out;
13491da177e4SLinus Torvalds 				}
135086872cb5SThomas Graf 
1351defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
13521da177e4SLinus Torvalds 			}
13531da177e4SLinus Torvalds 		}
13541da177e4SLinus Torvalds 	}
13551da177e4SLinus Torvalds 
1356d8d1f30bSChangli Gao 	rt->dst.dev = dev;
13571da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1358c71099acSThomas Graf 	rt->rt6i_table = table;
135963152fc0SDaniel Lezcano 
1360c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
136163152fc0SDaniel Lezcano 
136286872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds out:
13651da177e4SLinus Torvalds 	if (dev)
13661da177e4SLinus Torvalds 		dev_put(dev);
13671da177e4SLinus Torvalds 	if (idev)
13681da177e4SLinus Torvalds 		in6_dev_put(idev);
13691da177e4SLinus Torvalds 	if (rt)
1370d8d1f30bSChangli Gao 		dst_free(&rt->dst);
13711da177e4SLinus Torvalds 	return err;
13721da177e4SLinus Torvalds }
13731da177e4SLinus Torvalds 
137486872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
13751da177e4SLinus Torvalds {
13761da177e4SLinus Torvalds 	int err;
1377c71099acSThomas Graf 	struct fib6_table *table;
1378c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(rt->rt6i_dev);
13791da177e4SLinus Torvalds 
13808ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry)
13816c813a72SPatrick McHardy 		return -ENOENT;
13826c813a72SPatrick McHardy 
1383c71099acSThomas Graf 	table = rt->rt6i_table;
1384c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
13851da177e4SLinus Torvalds 
138686872cb5SThomas Graf 	err = fib6_del(rt, info);
1387d8d1f30bSChangli Gao 	dst_release(&rt->dst);
13881da177e4SLinus Torvalds 
1389c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 	return err;
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds 
1394e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1395e0a1ad73SThomas Graf {
13964d1169c1SDenis V. Lunev 	struct nl_info info = {
1397c346dca1SYOSHIFUJI Hideaki 		.nl_net = dev_net(rt->rt6i_dev),
13984d1169c1SDenis V. Lunev 	};
1399528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1400e0a1ad73SThomas Graf }
1401e0a1ad73SThomas Graf 
140286872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
14031da177e4SLinus Torvalds {
1404c71099acSThomas Graf 	struct fib6_table *table;
14051da177e4SLinus Torvalds 	struct fib6_node *fn;
14061da177e4SLinus Torvalds 	struct rt6_info *rt;
14071da177e4SLinus Torvalds 	int err = -ESRCH;
14081da177e4SLinus Torvalds 
14095578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
1410c71099acSThomas Graf 	if (table == NULL)
1411c71099acSThomas Graf 		return err;
14121da177e4SLinus Torvalds 
1413c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1414c71099acSThomas Graf 
1415c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
141686872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
141786872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 	if (fn) {
1420d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
142186872cb5SThomas Graf 			if (cfg->fc_ifindex &&
14221da177e4SLinus Torvalds 			    (rt->rt6i_dev == NULL ||
142386872cb5SThomas Graf 			     rt->rt6i_dev->ifindex != cfg->fc_ifindex))
14241da177e4SLinus Torvalds 				continue;
142586872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
142686872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
14271da177e4SLinus Torvalds 				continue;
142886872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
14291da177e4SLinus Torvalds 				continue;
1430d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1431c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
14321da177e4SLinus Torvalds 
143386872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
14341da177e4SLinus Torvalds 		}
14351da177e4SLinus Torvalds 	}
1436c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	return err;
14391da177e4SLinus Torvalds }
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds /*
14421da177e4SLinus Torvalds  *	Handle redirects
14431da177e4SLinus Torvalds  */
1444a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi {
1445a6279458SYOSHIFUJI Hideaki 	struct flowi fl;
1446a6279458SYOSHIFUJI Hideaki 	struct in6_addr gateway;
1447a6279458SYOSHIFUJI Hideaki };
14481da177e4SLinus Torvalds 
14498ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net,
14508ed67789SDaniel Lezcano 					     struct fib6_table *table,
1451a6279458SYOSHIFUJI Hideaki 					     struct flowi *fl,
1452a6279458SYOSHIFUJI Hideaki 					     int flags)
1453a6279458SYOSHIFUJI Hideaki {
1454a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl;
1455a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt;
1456a6279458SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1457c71099acSThomas Graf 
1458e843b9e1SYOSHIFUJI Hideaki 	/*
1459e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1460e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1461e843b9e1SYOSHIFUJI Hideaki 	 *
1462e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1463e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1464e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1465e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1466e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1467e843b9e1SYOSHIFUJI Hideaki 	 */
14681da177e4SLinus Torvalds 
1469c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1470a6279458SYOSHIFUJI Hideaki 	fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
1471e843b9e1SYOSHIFUJI Hideaki restart:
1472d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
14731da177e4SLinus Torvalds 		/*
14741da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
14751da177e4SLinus Torvalds 		 *
14761da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
14771da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
14781da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
14791da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
14801da177e4SLinus Torvalds 		 */
1481e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1482e843b9e1SYOSHIFUJI Hideaki 			continue;
14831da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1484e843b9e1SYOSHIFUJI Hideaki 			continue;
1485a6279458SYOSHIFUJI Hideaki 		if (fl->oif != rt->rt6i_dev->ifindex)
1486e843b9e1SYOSHIFUJI Hideaki 			continue;
1487a6279458SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1488e843b9e1SYOSHIFUJI Hideaki 			continue;
1489e843b9e1SYOSHIFUJI Hideaki 		break;
1490e843b9e1SYOSHIFUJI Hideaki 	}
1491a6279458SYOSHIFUJI Hideaki 
1492cb15d9c2SYOSHIFUJI Hideaki 	if (!rt)
14938ed67789SDaniel Lezcano 		rt = net->ipv6.ip6_null_entry;
14948ed67789SDaniel Lezcano 	BACKTRACK(net, &fl->fl6_src);
1495cb15d9c2SYOSHIFUJI Hideaki out:
1496d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
1497a6279458SYOSHIFUJI Hideaki 
1498c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
14991da177e4SLinus Torvalds 
1500a6279458SYOSHIFUJI Hideaki 	return rt;
1501a6279458SYOSHIFUJI Hideaki };
1502a6279458SYOSHIFUJI Hideaki 
1503a6279458SYOSHIFUJI Hideaki static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
1504a6279458SYOSHIFUJI Hideaki 					   struct in6_addr *src,
1505a6279458SYOSHIFUJI Hideaki 					   struct in6_addr *gateway,
1506a6279458SYOSHIFUJI Hideaki 					   struct net_device *dev)
1507a6279458SYOSHIFUJI Hideaki {
1508adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1509c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
1510a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi rdfl = {
1511a6279458SYOSHIFUJI Hideaki 		.fl = {
1512a6279458SYOSHIFUJI Hideaki 			.oif = dev->ifindex,
15135811662bSChangli Gao 			.fl6_dst = *dest,
15145811662bSChangli Gao 			.fl6_src = *src,
1515a6279458SYOSHIFUJI Hideaki 		},
1516a6279458SYOSHIFUJI Hideaki 	};
1517adaa70bbSThomas Graf 
151886c36ce4SBrian Haley 	ipv6_addr_copy(&rdfl.gateway, gateway);
151986c36ce4SBrian Haley 
1520adaa70bbSThomas Graf 	if (rt6_need_strict(dest))
1521adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
1522a6279458SYOSHIFUJI Hideaki 
15235578689aSDaniel Lezcano 	return (struct rt6_info *)fib6_rule_lookup(net, (struct flowi *)&rdfl,
152458f09b78SDaniel Lezcano 						   flags, __ip6_route_redirect);
1525a6279458SYOSHIFUJI Hideaki }
1526a6279458SYOSHIFUJI Hideaki 
1527a6279458SYOSHIFUJI Hideaki void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
1528a6279458SYOSHIFUJI Hideaki 		  struct in6_addr *saddr,
1529a6279458SYOSHIFUJI Hideaki 		  struct neighbour *neigh, u8 *lladdr, int on_link)
1530a6279458SYOSHIFUJI Hideaki {
1531a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt = NULL;
1532a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1533c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(neigh->dev);
1534a6279458SYOSHIFUJI Hideaki 
1535a6279458SYOSHIFUJI Hideaki 	rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1536a6279458SYOSHIFUJI Hideaki 
15378ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry) {
15381da177e4SLinus Torvalds 		if (net_ratelimit())
15391da177e4SLinus Torvalds 			printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
15401da177e4SLinus Torvalds 			       "for redirect target\n");
1541a6279458SYOSHIFUJI Hideaki 		goto out;
15421da177e4SLinus Torvalds 	}
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	/*
15451da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
15461da177e4SLinus Torvalds 	 */
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
15491da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
15501da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
15511da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
15521da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
15531da177e4SLinus Torvalds 		     );
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	/*
15561da177e4SLinus Torvalds 	 * Redirect received -> path was valid.
15571da177e4SLinus Torvalds 	 * Look, redirects are sent only in response to data packets,
15581da177e4SLinus Torvalds 	 * so that this nexthop apparently is reachable. --ANK
15591da177e4SLinus Torvalds 	 */
1560d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 	/* Duplicate redirect: silently ignore. */
1563d8d1f30bSChangli Gao 	if (neigh == rt->dst.neighbour)
15641da177e4SLinus Torvalds 		goto out;
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	nrt = ip6_rt_copy(rt);
15671da177e4SLinus Torvalds 	if (nrt == NULL)
15681da177e4SLinus Torvalds 		goto out;
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
15711da177e4SLinus Torvalds 	if (on_link)
15721da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
15751da177e4SLinus Torvalds 	nrt->rt6i_dst.plen = 128;
1576d8d1f30bSChangli Gao 	nrt->dst.flags |= DST_HOST;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
15791da177e4SLinus Torvalds 	nrt->rt6i_nexthop = neigh_clone(neigh);
15801da177e4SLinus Torvalds 
158140e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
15821da177e4SLinus Torvalds 		goto out;
15831da177e4SLinus Torvalds 
1584d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1585d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
15868d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
15878d71740cSTom Tucker 
15881da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE) {
1589e0a1ad73SThomas Graf 		ip6_del_rt(rt);
15901da177e4SLinus Torvalds 		return;
15911da177e4SLinus Torvalds 	}
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds out:
1594d8d1f30bSChangli Gao 	dst_release(&rt->dst);
15951da177e4SLinus Torvalds }
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds /*
15981da177e4SLinus Torvalds  *	Handle ICMP "packet too big" messages
15991da177e4SLinus Torvalds  *	i.e. Path MTU discovery
16001da177e4SLinus Torvalds  */
16011da177e4SLinus Torvalds 
1602ae878ae2SMaciej Żenczykowski static void rt6_do_pmtu_disc(struct in6_addr *daddr, struct in6_addr *saddr,
1603ae878ae2SMaciej Żenczykowski 			     struct net *net, u32 pmtu, int ifindex)
16041da177e4SLinus Torvalds {
16051da177e4SLinus Torvalds 	struct rt6_info *rt, *nrt;
16061da177e4SLinus Torvalds 	int allfrag = 0;
1607d3052b55SAndrey Vagin again:
1608ae878ae2SMaciej Żenczykowski 	rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
16091da177e4SLinus Torvalds 	if (rt == NULL)
16101da177e4SLinus Torvalds 		return;
16111da177e4SLinus Torvalds 
1612d3052b55SAndrey Vagin 	if (rt6_check_expired(rt)) {
1613d3052b55SAndrey Vagin 		ip6_del_rt(rt);
1614d3052b55SAndrey Vagin 		goto again;
1615d3052b55SAndrey Vagin 	}
1616d3052b55SAndrey Vagin 
1617d8d1f30bSChangli Gao 	if (pmtu >= dst_mtu(&rt->dst))
16181da177e4SLinus Torvalds 		goto out;
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 	if (pmtu < IPV6_MIN_MTU) {
16211da177e4SLinus Torvalds 		/*
16221da177e4SLinus Torvalds 		 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
16231da177e4SLinus Torvalds 		 * MTU (1280) and a fragment header should always be included
16241da177e4SLinus Torvalds 		 * after a node receiving Too Big message reporting PMTU is
16251da177e4SLinus Torvalds 		 * less than the IPv6 Minimum Link MTU.
16261da177e4SLinus Torvalds 		 */
16271da177e4SLinus Torvalds 		pmtu = IPV6_MIN_MTU;
16281da177e4SLinus Torvalds 		allfrag = 1;
16291da177e4SLinus Torvalds 	}
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 	/* New mtu received -> path was valid.
16321da177e4SLinus Torvalds 	   They are sent only in response to data packets,
16331da177e4SLinus Torvalds 	   so that this nexthop apparently is reachable. --ANK
16341da177e4SLinus Torvalds 	 */
1635d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds 	/* Host route. If it is static, it would be better
16381da177e4SLinus Torvalds 	   not to override it, but add new one, so that
16391da177e4SLinus Torvalds 	   when cache entry will expire old pmtu
16401da177e4SLinus Torvalds 	   would return automatically.
16411da177e4SLinus Torvalds 	 */
16421da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
1643defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1644defb3519SDavid S. Miller 		if (allfrag) {
1645defb3519SDavid S. Miller 			u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1646defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1647defb3519SDavid S. Miller 			dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1648defb3519SDavid S. Miller 		}
1649d8d1f30bSChangli Gao 		dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
16501da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
16511da177e4SLinus Torvalds 		goto out;
16521da177e4SLinus Torvalds 	}
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	/* Network route.
16551da177e4SLinus Torvalds 	   Two cases are possible:
16561da177e4SLinus Torvalds 	   1. It is connected route. Action: COW
16571da177e4SLinus Torvalds 	   2. It is gatewayed route or NONEXTHOP route. Action: clone it.
16581da177e4SLinus Torvalds 	 */
1659d5315b50SYOSHIFUJI Hideaki 	if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
1660a1e78363SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, daddr, saddr);
1661d5315b50SYOSHIFUJI Hideaki 	else
1662d5315b50SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, daddr);
1663a1e78363SYOSHIFUJI Hideaki 
1664d5315b50SYOSHIFUJI Hideaki 	if (nrt) {
1665defb3519SDavid S. Miller 		dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1666defb3519SDavid S. Miller 		if (allfrag) {
1667defb3519SDavid S. Miller 			u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1668defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1669defb3519SDavid S. Miller 			dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1670defb3519SDavid S. Miller 		}
1671a1e78363SYOSHIFUJI Hideaki 
16721da177e4SLinus Torvalds 		/* According to RFC 1981, detecting PMTU increase shouldn't be
1673a1e78363SYOSHIFUJI Hideaki 		 * happened within 5 mins, the recommended timer is 10 mins.
1674a1e78363SYOSHIFUJI Hideaki 		 * Here this route expiration time is set to ip6_rt_mtu_expires
1675a1e78363SYOSHIFUJI Hideaki 		 * which is 10 mins. After 10 mins the decreased pmtu is expired
1676a1e78363SYOSHIFUJI Hideaki 		 * and detecting PMTU increase will be automatically happened.
16771da177e4SLinus Torvalds 		 */
1678d8d1f30bSChangli Gao 		dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
16791da177e4SLinus Torvalds 		nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1680a1e78363SYOSHIFUJI Hideaki 
168140e22e8fSThomas Graf 		ip6_ins_rt(nrt);
16821da177e4SLinus Torvalds 	}
16831da177e4SLinus Torvalds out:
1684d8d1f30bSChangli Gao 	dst_release(&rt->dst);
16851da177e4SLinus Torvalds }
16861da177e4SLinus Torvalds 
1687ae878ae2SMaciej Żenczykowski void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
1688ae878ae2SMaciej Żenczykowski 			struct net_device *dev, u32 pmtu)
1689ae878ae2SMaciej Żenczykowski {
1690ae878ae2SMaciej Żenczykowski 	struct net *net = dev_net(dev);
1691ae878ae2SMaciej Żenczykowski 
1692ae878ae2SMaciej Żenczykowski 	/*
1693ae878ae2SMaciej Żenczykowski 	 * RFC 1981 states that a node "MUST reduce the size of the packets it
1694ae878ae2SMaciej Żenczykowski 	 * is sending along the path" that caused the Packet Too Big message.
1695ae878ae2SMaciej Żenczykowski 	 * Since it's not possible in the general case to determine which
1696ae878ae2SMaciej Żenczykowski 	 * interface was used to send the original packet, we update the MTU
1697ae878ae2SMaciej Żenczykowski 	 * on the interface that will be used to send future packets. We also
1698ae878ae2SMaciej Żenczykowski 	 * update the MTU on the interface that received the Packet Too Big in
1699ae878ae2SMaciej Żenczykowski 	 * case the original packet was forced out that interface with
1700ae878ae2SMaciej Żenczykowski 	 * SO_BINDTODEVICE or similar. This is the next best thing to the
1701ae878ae2SMaciej Żenczykowski 	 * correct behaviour, which would be to update the MTU on all
1702ae878ae2SMaciej Żenczykowski 	 * interfaces.
1703ae878ae2SMaciej Żenczykowski 	 */
1704ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1705ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1706ae878ae2SMaciej Żenczykowski }
1707ae878ae2SMaciej Żenczykowski 
17081da177e4SLinus Torvalds /*
17091da177e4SLinus Torvalds  *	Misc support functions
17101da177e4SLinus Torvalds  */
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
17131da177e4SLinus Torvalds {
1714c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ort->rt6i_dev);
171586393e52SAlexey Dobriyan 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds 	if (rt) {
1718d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1719d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
17201da177e4SLinus Torvalds 
1721defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1722d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
1723d8d1f30bSChangli Gao 		rt->dst.dev = ort->dst.dev;
1724d8d1f30bSChangli Gao 		if (rt->dst.dev)
1725d8d1f30bSChangli Gao 			dev_hold(rt->dst.dev);
17261da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
17271da177e4SLinus Torvalds 		if (rt->rt6i_idev)
17281da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1729d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
17301da177e4SLinus Torvalds 		rt->rt6i_expires = 0;
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds 		ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
17331da177e4SLinus Torvalds 		rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
17341da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
17371da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
17381da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
17391da177e4SLinus Torvalds #endif
1740c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
17411da177e4SLinus Torvalds 	}
17421da177e4SLinus Torvalds 	return rt;
17431da177e4SLinus Torvalds }
17441da177e4SLinus Torvalds 
174570ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1746efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1747efa2cea0SDaniel Lezcano 					   struct in6_addr *prefix, int prefixlen,
174870ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex)
174970ceb4f5SYOSHIFUJI Hideaki {
175070ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
175170ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1752c71099acSThomas Graf 	struct fib6_table *table;
175370ceb4f5SYOSHIFUJI Hideaki 
1754efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
1755c71099acSThomas Graf 	if (table == NULL)
1756c71099acSThomas Graf 		return NULL;
1757c71099acSThomas Graf 
1758c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1759c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
176070ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
176170ceb4f5SYOSHIFUJI Hideaki 		goto out;
176270ceb4f5SYOSHIFUJI Hideaki 
1763d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
176470ceb4f5SYOSHIFUJI Hideaki 		if (rt->rt6i_dev->ifindex != ifindex)
176570ceb4f5SYOSHIFUJI Hideaki 			continue;
176670ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
176770ceb4f5SYOSHIFUJI Hideaki 			continue;
176870ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
176970ceb4f5SYOSHIFUJI Hideaki 			continue;
1770d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
177170ceb4f5SYOSHIFUJI Hideaki 		break;
177270ceb4f5SYOSHIFUJI Hideaki 	}
177370ceb4f5SYOSHIFUJI Hideaki out:
1774c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
177570ceb4f5SYOSHIFUJI Hideaki 	return rt;
177670ceb4f5SYOSHIFUJI Hideaki }
177770ceb4f5SYOSHIFUJI Hideaki 
1778efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1779efa2cea0SDaniel Lezcano 					   struct in6_addr *prefix, int prefixlen,
178070ceb4f5SYOSHIFUJI Hideaki 					   struct in6_addr *gwaddr, int ifindex,
178170ceb4f5SYOSHIFUJI Hideaki 					   unsigned pref)
178270ceb4f5SYOSHIFUJI Hideaki {
178386872cb5SThomas Graf 	struct fib6_config cfg = {
178486872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1785238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
178686872cb5SThomas Graf 		.fc_ifindex	= ifindex,
178786872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
178886872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
178986872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
1790efa2cea0SDaniel Lezcano 		.fc_nlinfo.pid = 0,
1791efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1792efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
179386872cb5SThomas Graf 	};
179470ceb4f5SYOSHIFUJI Hideaki 
179586872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_dst, prefix);
179686872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
179786872cb5SThomas Graf 
1798e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1799e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
180086872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
180170ceb4f5SYOSHIFUJI Hideaki 
180286872cb5SThomas Graf 	ip6_route_add(&cfg);
180370ceb4f5SYOSHIFUJI Hideaki 
1804efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
180570ceb4f5SYOSHIFUJI Hideaki }
180670ceb4f5SYOSHIFUJI Hideaki #endif
180770ceb4f5SYOSHIFUJI Hideaki 
18081da177e4SLinus Torvalds struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
18091da177e4SLinus Torvalds {
18101da177e4SLinus Torvalds 	struct rt6_info *rt;
1811c71099acSThomas Graf 	struct fib6_table *table;
18121da177e4SLinus Torvalds 
1813c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
1814c71099acSThomas Graf 	if (table == NULL)
1815c71099acSThomas Graf 		return NULL;
18161da177e4SLinus Torvalds 
1817c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1818d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
18191da177e4SLinus Torvalds 		if (dev == rt->rt6i_dev &&
1820045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
18211da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
18221da177e4SLinus Torvalds 			break;
18231da177e4SLinus Torvalds 	}
18241da177e4SLinus Torvalds 	if (rt)
1825d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
1826c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
18271da177e4SLinus Torvalds 	return rt;
18281da177e4SLinus Torvalds }
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
1831ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1832ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
18331da177e4SLinus Torvalds {
183486872cb5SThomas Graf 	struct fib6_config cfg = {
183586872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1836238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
183786872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
183886872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
183986872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
18405578689aSDaniel Lezcano 		.fc_nlinfo.pid = 0,
18415578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1842c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
184386872cb5SThomas Graf 	};
18441da177e4SLinus Torvalds 
184586872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
18461da177e4SLinus Torvalds 
184786872cb5SThomas Graf 	ip6_route_add(&cfg);
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
18501da177e4SLinus Torvalds }
18511da177e4SLinus Torvalds 
18527b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
18531da177e4SLinus Torvalds {
18541da177e4SLinus Torvalds 	struct rt6_info *rt;
1855c71099acSThomas Graf 	struct fib6_table *table;
1856c71099acSThomas Graf 
1857c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
18587b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
1859c71099acSThomas Graf 	if (table == NULL)
1860c71099acSThomas Graf 		return;
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds restart:
1863c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1864d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
18651da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1866d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1867c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1868e0a1ad73SThomas Graf 			ip6_del_rt(rt);
18691da177e4SLinus Torvalds 			goto restart;
18701da177e4SLinus Torvalds 		}
18711da177e4SLinus Torvalds 	}
1872c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
18731da177e4SLinus Torvalds }
18741da177e4SLinus Torvalds 
18755578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
18765578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
187786872cb5SThomas Graf 				 struct fib6_config *cfg)
187886872cb5SThomas Graf {
187986872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
188086872cb5SThomas Graf 
188186872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
188286872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
188386872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
188486872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
188586872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
188686872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
188786872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
188886872cb5SThomas Graf 
18895578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1890f1243c2dSBenjamin Thery 
189186872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
189286872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
189386872cb5SThomas Graf 	ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
189486872cb5SThomas Graf }
189586872cb5SThomas Graf 
18965578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
18971da177e4SLinus Torvalds {
189886872cb5SThomas Graf 	struct fib6_config cfg;
18991da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
19001da177e4SLinus Torvalds 	int err;
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds 	switch(cmd) {
19031da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
19041da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
19051da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
19061da177e4SLinus Torvalds 			return -EPERM;
19071da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
19081da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
19091da177e4SLinus Torvalds 		if (err)
19101da177e4SLinus Torvalds 			return -EFAULT;
19111da177e4SLinus Torvalds 
19125578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
191386872cb5SThomas Graf 
19141da177e4SLinus Torvalds 		rtnl_lock();
19151da177e4SLinus Torvalds 		switch (cmd) {
19161da177e4SLinus Torvalds 		case SIOCADDRT:
191786872cb5SThomas Graf 			err = ip6_route_add(&cfg);
19181da177e4SLinus Torvalds 			break;
19191da177e4SLinus Torvalds 		case SIOCDELRT:
192086872cb5SThomas Graf 			err = ip6_route_del(&cfg);
19211da177e4SLinus Torvalds 			break;
19221da177e4SLinus Torvalds 		default:
19231da177e4SLinus Torvalds 			err = -EINVAL;
19241da177e4SLinus Torvalds 		}
19251da177e4SLinus Torvalds 		rtnl_unlock();
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds 		return err;
19283ff50b79SStephen Hemminger 	}
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 	return -EINVAL;
19311da177e4SLinus Torvalds }
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds /*
19341da177e4SLinus Torvalds  *	Drop the packet on the floor
19351da177e4SLinus Torvalds  */
19361da177e4SLinus Torvalds 
1937d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
19381da177e4SLinus Torvalds {
1939612f09e8SYOSHIFUJI Hideaki 	int type;
1940adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
1941612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
1942612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
19430660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
194445bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
19453bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
19463bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
1947612f09e8SYOSHIFUJI Hideaki 			break;
1948612f09e8SYOSHIFUJI Hideaki 		}
1949612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
1950612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
19513bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
19523bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
1953612f09e8SYOSHIFUJI Hideaki 		break;
1954612f09e8SYOSHIFUJI Hideaki 	}
19553ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
19561da177e4SLinus Torvalds 	kfree_skb(skb);
19571da177e4SLinus Torvalds 	return 0;
19581da177e4SLinus Torvalds }
19591da177e4SLinus Torvalds 
19609ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
19619ce8ade0SThomas Graf {
1962612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
19639ce8ade0SThomas Graf }
19649ce8ade0SThomas Graf 
196520380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
19661da177e4SLinus Torvalds {
1967adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
1968612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
19691da177e4SLinus Torvalds }
19701da177e4SLinus Torvalds 
19716723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
19726723ab54SDavid S. Miller 
19739ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
19749ce8ade0SThomas Graf {
1975612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
19769ce8ade0SThomas Graf }
19779ce8ade0SThomas Graf 
19789ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
19799ce8ade0SThomas Graf {
1980adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
1981612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
19829ce8ade0SThomas Graf }
19839ce8ade0SThomas Graf 
19846723ab54SDavid S. Miller #endif
19856723ab54SDavid S. Miller 
19861da177e4SLinus Torvalds /*
19871da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
19881da177e4SLinus Torvalds  */
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
19911da177e4SLinus Torvalds 				    const struct in6_addr *addr,
19921da177e4SLinus Torvalds 				    int anycast)
19931da177e4SLinus Torvalds {
1994c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
199586393e52SAlexey Dobriyan 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
199614deae41SDavid S. Miller 	struct neighbour *neigh;
19971da177e4SLinus Torvalds 
199840385653SBen Greear 	if (rt == NULL) {
199940385653SBen Greear 		if (net_ratelimit())
200040385653SBen Greear 			pr_warning("IPv6:  Maximum number of routes reached,"
200140385653SBen Greear 				   " consider increasing route/max_size.\n");
20021da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
200340385653SBen Greear 	}
20041da177e4SLinus Torvalds 
20055578689aSDaniel Lezcano 	dev_hold(net->loopback_dev);
20061da177e4SLinus Torvalds 	in6_dev_hold(idev);
20071da177e4SLinus Torvalds 
2008d8d1f30bSChangli Gao 	rt->dst.flags = DST_HOST;
2009d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2010d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
20115578689aSDaniel Lezcano 	rt->rt6i_dev = net->loopback_dev;
20121da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2013defb3519SDavid S. Miller 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, -1);
2014d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
201758c4fb86SYOSHIFUJI Hideaki 	if (anycast)
201858c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
201958c4fb86SYOSHIFUJI Hideaki 	else
20201da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
202114deae41SDavid S. Miller 	neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
202214deae41SDavid S. Miller 	if (IS_ERR(neigh)) {
2023d8d1f30bSChangli Gao 		dst_free(&rt->dst);
202414deae41SDavid S. Miller 
202514deae41SDavid S. Miller 		/* We are casting this because that is the return
202614deae41SDavid S. Miller 		 * value type.  But an errno encoded pointer is the
202714deae41SDavid S. Miller 		 * same regardless of the underlying pointer type,
202814deae41SDavid S. Miller 		 * and that's what we are returning.  So this is OK.
202914deae41SDavid S. Miller 		 */
203014deae41SDavid S. Miller 		return (struct rt6_info *) neigh;
20311da177e4SLinus Torvalds 	}
203214deae41SDavid S. Miller 	rt->rt6i_nexthop = neigh;
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 	ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
20351da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
20365578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
20371da177e4SLinus Torvalds 
2038d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	return rt;
20411da177e4SLinus Torvalds }
20421da177e4SLinus Torvalds 
20438ed67789SDaniel Lezcano struct arg_dev_net {
20448ed67789SDaniel Lezcano 	struct net_device *dev;
20458ed67789SDaniel Lezcano 	struct net *net;
20468ed67789SDaniel Lezcano };
20478ed67789SDaniel Lezcano 
20481da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
20491da177e4SLinus Torvalds {
2050bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2051bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
20528ed67789SDaniel Lezcano 
2053bc3ef660Sstephen hemminger 	if ((rt->rt6i_dev == dev || dev == NULL) &&
2054bc3ef660Sstephen hemminger 	    rt != adn->net->ipv6.ip6_null_entry) {
20551da177e4SLinus Torvalds 		RT6_TRACE("deleted by ifdown %p\n", rt);
20561da177e4SLinus Torvalds 		return -1;
20571da177e4SLinus Torvalds 	}
20581da177e4SLinus Torvalds 	return 0;
20591da177e4SLinus Torvalds }
20601da177e4SLinus Torvalds 
2061f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
20621da177e4SLinus Torvalds {
20638ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
20648ed67789SDaniel Lezcano 		.dev = dev,
20658ed67789SDaniel Lezcano 		.net = net,
20668ed67789SDaniel Lezcano 	};
20678ed67789SDaniel Lezcano 
20688ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
20691e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
20701da177e4SLinus Torvalds }
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds struct rt6_mtu_change_arg
20731da177e4SLinus Torvalds {
20741da177e4SLinus Torvalds 	struct net_device *dev;
20751da177e4SLinus Torvalds 	unsigned mtu;
20761da177e4SLinus Torvalds };
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
20791da177e4SLinus Torvalds {
20801da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
20811da177e4SLinus Torvalds 	struct inet6_dev *idev;
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
20841da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
20851da177e4SLinus Torvalds 	   We still use this lock to block changes
20861da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
20871da177e4SLinus Torvalds 	*/
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
20901da177e4SLinus Torvalds 	if (idev == NULL)
20911da177e4SLinus Torvalds 		return 0;
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
20941da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
20951da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
20961da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
20971da177e4SLinus Torvalds 	 */
20981da177e4SLinus Torvalds 	/*
20991da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
21001da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
21011da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
21021da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
21031da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
21041da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
21051da177e4SLinus Torvalds 	   PMTU discouvery.
21061da177e4SLinus Torvalds 	 */
21071da177e4SLinus Torvalds 	if (rt->rt6i_dev == arg->dev &&
2108d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2109d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2110d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2111d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2112defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2113566cfd8fSSimon Arlott 	}
21141da177e4SLinus Torvalds 	return 0;
21151da177e4SLinus Torvalds }
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu)
21181da177e4SLinus Torvalds {
2119c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2120c71099acSThomas Graf 		.dev = dev,
2121c71099acSThomas Graf 		.mtu = mtu,
2122c71099acSThomas Graf 	};
21231da177e4SLinus Torvalds 
2124c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
21251da177e4SLinus Torvalds }
21261da177e4SLinus Torvalds 
2127ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
21285176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
212986872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2130ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
213186872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
213286872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
213386872cb5SThomas Graf };
213486872cb5SThomas Graf 
213586872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
213686872cb5SThomas Graf 			      struct fib6_config *cfg)
21371da177e4SLinus Torvalds {
213886872cb5SThomas Graf 	struct rtmsg *rtm;
213986872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
214086872cb5SThomas Graf 	int err;
21411da177e4SLinus Torvalds 
214286872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
214386872cb5SThomas Graf 	if (err < 0)
214486872cb5SThomas Graf 		goto errout;
21451da177e4SLinus Torvalds 
214686872cb5SThomas Graf 	err = -EINVAL;
214786872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
214886872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
214986872cb5SThomas Graf 
215086872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
215186872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
215286872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
215386872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
215486872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
215586872cb5SThomas Graf 
215686872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
215786872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
215886872cb5SThomas Graf 
2159ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2160ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2161ab79ad14SMaciej Żenczykowski 
216286872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
216386872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
21643b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
216586872cb5SThomas Graf 
216686872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
216786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
216886872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
21691da177e4SLinus Torvalds 	}
217086872cb5SThomas Graf 
217186872cb5SThomas Graf 	if (tb[RTA_DST]) {
217286872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
217386872cb5SThomas Graf 
217486872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
217586872cb5SThomas Graf 			goto errout;
217686872cb5SThomas Graf 
217786872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
21781da177e4SLinus Torvalds 	}
217986872cb5SThomas Graf 
218086872cb5SThomas Graf 	if (tb[RTA_SRC]) {
218186872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
218286872cb5SThomas Graf 
218386872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
218486872cb5SThomas Graf 			goto errout;
218586872cb5SThomas Graf 
218686872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
21871da177e4SLinus Torvalds 	}
218886872cb5SThomas Graf 
218986872cb5SThomas Graf 	if (tb[RTA_OIF])
219086872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
219186872cb5SThomas Graf 
219286872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
219386872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
219486872cb5SThomas Graf 
219586872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
219686872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
219786872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
21981da177e4SLinus Torvalds 	}
219986872cb5SThomas Graf 
220086872cb5SThomas Graf 	if (tb[RTA_TABLE])
220186872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
220286872cb5SThomas Graf 
220386872cb5SThomas Graf 	err = 0;
220486872cb5SThomas Graf errout:
220586872cb5SThomas Graf 	return err;
22061da177e4SLinus Torvalds }
22071da177e4SLinus Torvalds 
2208c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
22091da177e4SLinus Torvalds {
221086872cb5SThomas Graf 	struct fib6_config cfg;
221186872cb5SThomas Graf 	int err;
22121da177e4SLinus Torvalds 
221386872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
221486872cb5SThomas Graf 	if (err < 0)
221586872cb5SThomas Graf 		return err;
221686872cb5SThomas Graf 
221786872cb5SThomas Graf 	return ip6_route_del(&cfg);
22181da177e4SLinus Torvalds }
22191da177e4SLinus Torvalds 
2220c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
22211da177e4SLinus Torvalds {
222286872cb5SThomas Graf 	struct fib6_config cfg;
222386872cb5SThomas Graf 	int err;
22241da177e4SLinus Torvalds 
222586872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
222686872cb5SThomas Graf 	if (err < 0)
222786872cb5SThomas Graf 		return err;
222886872cb5SThomas Graf 
222986872cb5SThomas Graf 	return ip6_route_add(&cfg);
22301da177e4SLinus Torvalds }
22311da177e4SLinus Torvalds 
2232339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2233339bf98fSThomas Graf {
2234339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2235339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2236339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2237339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2238339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2239339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2240339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2241339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2242339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
22436a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2244339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2245339bf98fSThomas Graf }
2246339bf98fSThomas Graf 
2247191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2248191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
22490d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
22500d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
22517bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
22521da177e4SLinus Torvalds {
22531da177e4SLinus Torvalds 	struct rtmsg *rtm;
22541da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2255e3703b3dSThomas Graf 	long expires;
22569e762a4aSPatrick McHardy 	u32 table;
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
22591da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
22601da177e4SLinus Torvalds 			/* success since this is not a prefix route */
22611da177e4SLinus Torvalds 			return 1;
22621da177e4SLinus Torvalds 		}
22631da177e4SLinus Torvalds 	}
22641da177e4SLinus Torvalds 
22652d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
22662d7202bfSThomas Graf 	if (nlh == NULL)
226726932566SPatrick McHardy 		return -EMSGSIZE;
22682d7202bfSThomas Graf 
22692d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
22701da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
22711da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
22721da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
22731da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2274c71099acSThomas Graf 	if (rt->rt6i_table)
22759e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2276c71099acSThomas Graf 	else
22779e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
22789e762a4aSPatrick McHardy 	rtm->rtm_table = table;
22792d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_TABLE, table);
22801da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_REJECT)
22811da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
2282ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags&RTF_LOCAL)
2283ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
22841da177e4SLinus Torvalds 	else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
22851da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
22861da177e4SLinus Torvalds 	else
22871da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
22881da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
22891da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
22901da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
22911da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_DYNAMIC)
22921da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
22931da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
22941da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
22951da177e4SLinus Torvalds 	else if (rt->rt6i_flags&RTF_DEFAULT)
22961da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds 	if (rt->rt6i_flags&RTF_CACHE)
22991da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds 	if (dst) {
23022d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, dst);
23031da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
23041da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
23052d7202bfSThomas Graf 		NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
23061da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
23071da177e4SLinus Torvalds 	if (src) {
23082d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, src);
23091da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
23101da177e4SLinus Torvalds 	} else if (rtm->rtm_src_len)
23112d7202bfSThomas Graf 		NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
23121da177e4SLinus Torvalds #endif
23137bc570c8SYOSHIFUJI Hideaki 	if (iif) {
23147bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
23157bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
23168229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
23177bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
23187bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
23197bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
23207bc570c8SYOSHIFUJI Hideaki 						return 0;
23217bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
23227bc570c8SYOSHIFUJI Hideaki 				} else {
23237bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
23247bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
23257bc570c8SYOSHIFUJI Hideaki 				}
23267bc570c8SYOSHIFUJI Hideaki 			}
23277bc570c8SYOSHIFUJI Hideaki 		} else
23287bc570c8SYOSHIFUJI Hideaki #endif
23292d7202bfSThomas Graf 			NLA_PUT_U32(skb, RTA_IIF, iif);
23307bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
2331d8d1f30bSChangli Gao 		struct inet6_dev *idev = ip6_dst_idev(&rt->dst);
23321da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2333191cd582SBrian Haley 		if (ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
23347cbca67cSYOSHIFUJI Hideaki 				       dst, 0, &saddr_buf) == 0)
23352d7202bfSThomas Graf 			NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
23361da177e4SLinus Torvalds 	}
23372d7202bfSThomas Graf 
2338defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
23392d7202bfSThomas Graf 		goto nla_put_failure;
23402d7202bfSThomas Graf 
2341d8d1f30bSChangli Gao 	if (rt->dst.neighbour)
2342d8d1f30bSChangli Gao 		NLA_PUT(skb, RTA_GATEWAY, 16, &rt->dst.neighbour->primary_key);
23432d7202bfSThomas Graf 
2344d8d1f30bSChangli Gao 	if (rt->dst.dev)
23452d7202bfSThomas Graf 		NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
23462d7202bfSThomas Graf 
23472d7202bfSThomas Graf 	NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
2348e3703b3dSThomas Graf 
234936e3deaeSYOSHIFUJI Hideaki 	if (!(rt->rt6i_flags & RTF_EXPIRES))
235036e3deaeSYOSHIFUJI Hideaki 		expires = 0;
235136e3deaeSYOSHIFUJI Hideaki 	else if (rt->rt6i_expires - jiffies < INT_MAX)
235236e3deaeSYOSHIFUJI Hideaki 		expires = rt->rt6i_expires - jiffies;
235336e3deaeSYOSHIFUJI Hideaki 	else
235436e3deaeSYOSHIFUJI Hideaki 		expires = INT_MAX;
235569cdf8f9SYOSHIFUJI Hideaki 
2356d8d1f30bSChangli Gao 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2357d8d1f30bSChangli Gao 			       expires, rt->dst.error) < 0)
2358e3703b3dSThomas Graf 		goto nla_put_failure;
23591da177e4SLinus Torvalds 
23602d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
23612d7202bfSThomas Graf 
23622d7202bfSThomas Graf nla_put_failure:
236326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
236426932566SPatrick McHardy 	return -EMSGSIZE;
23651da177e4SLinus Torvalds }
23661da177e4SLinus Torvalds 
23671b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
23681da177e4SLinus Torvalds {
23691da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
23701da177e4SLinus Torvalds 	int prefix;
23711da177e4SLinus Torvalds 
23722d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
23732d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
23741da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
23751da177e4SLinus Torvalds 	} else
23761da177e4SLinus Torvalds 		prefix = 0;
23771da177e4SLinus Torvalds 
2378191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2379191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
23801da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
23817bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
23821da177e4SLinus Torvalds }
23831da177e4SLinus Torvalds 
2384c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
23851da177e4SLinus Torvalds {
23863b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2387ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
23881da177e4SLinus Torvalds 	struct rt6_info *rt;
2389ab364a6fSThomas Graf 	struct sk_buff *skb;
2390ab364a6fSThomas Graf 	struct rtmsg *rtm;
2391ab364a6fSThomas Graf 	struct flowi fl;
2392ab364a6fSThomas Graf 	int err, iif = 0;
2393ab364a6fSThomas Graf 
2394ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2395ab364a6fSThomas Graf 	if (err < 0)
2396ab364a6fSThomas Graf 		goto errout;
2397ab364a6fSThomas Graf 
2398ab364a6fSThomas Graf 	err = -EINVAL;
2399ab364a6fSThomas Graf 	memset(&fl, 0, sizeof(fl));
2400ab364a6fSThomas Graf 
2401ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2402ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2403ab364a6fSThomas Graf 			goto errout;
2404ab364a6fSThomas Graf 
2405ab364a6fSThomas Graf 		ipv6_addr_copy(&fl.fl6_src, nla_data(tb[RTA_SRC]));
2406ab364a6fSThomas Graf 	}
2407ab364a6fSThomas Graf 
2408ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2409ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2410ab364a6fSThomas Graf 			goto errout;
2411ab364a6fSThomas Graf 
2412ab364a6fSThomas Graf 		ipv6_addr_copy(&fl.fl6_dst, nla_data(tb[RTA_DST]));
2413ab364a6fSThomas Graf 	}
2414ab364a6fSThomas Graf 
2415ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2416ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2417ab364a6fSThomas Graf 
2418ab364a6fSThomas Graf 	if (tb[RTA_OIF])
2419ab364a6fSThomas Graf 		fl.oif = nla_get_u32(tb[RTA_OIF]);
2420ab364a6fSThomas Graf 
2421ab364a6fSThomas Graf 	if (iif) {
2422ab364a6fSThomas Graf 		struct net_device *dev;
24235578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2424ab364a6fSThomas Graf 		if (!dev) {
2425ab364a6fSThomas Graf 			err = -ENODEV;
2426ab364a6fSThomas Graf 			goto errout;
2427ab364a6fSThomas Graf 		}
2428ab364a6fSThomas Graf 	}
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2431ab364a6fSThomas Graf 	if (skb == NULL) {
2432ab364a6fSThomas Graf 		err = -ENOBUFS;
2433ab364a6fSThomas Graf 		goto errout;
2434ab364a6fSThomas Graf 	}
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
24371da177e4SLinus Torvalds 	   through good chunk of routing engine.
24381da177e4SLinus Torvalds 	 */
2439459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
24401da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
24411da177e4SLinus Torvalds 
24428a3edd80SDaniel Lezcano 	rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl);
2443d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
24441da177e4SLinus Torvalds 
2445191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, &fl.fl6_dst, &fl.fl6_src, iif,
24461da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
24477bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
24481da177e4SLinus Torvalds 	if (err < 0) {
2449ab364a6fSThomas Graf 		kfree_skb(skb);
2450ab364a6fSThomas Graf 		goto errout;
24511da177e4SLinus Torvalds 	}
24521da177e4SLinus Torvalds 
24535578689aSDaniel Lezcano 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2454ab364a6fSThomas Graf errout:
24551da177e4SLinus Torvalds 	return err;
24561da177e4SLinus Torvalds }
24571da177e4SLinus Torvalds 
245886872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
24591da177e4SLinus Torvalds {
24601da177e4SLinus Torvalds 	struct sk_buff *skb;
24615578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2462528c4cebSDenis V. Lunev 	u32 seq;
2463528c4cebSDenis V. Lunev 	int err;
24640d51aa80SJamal Hadi Salim 
2465528c4cebSDenis V. Lunev 	err = -ENOBUFS;
2466528c4cebSDenis V. Lunev 	seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
246786872cb5SThomas Graf 
2468339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
246921713ebcSThomas Graf 	if (skb == NULL)
247021713ebcSThomas Graf 		goto errout;
24711da177e4SLinus Torvalds 
2472191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
24737bc570c8SYOSHIFUJI Hideaki 				event, info->pid, seq, 0, 0, 0);
247426932566SPatrick McHardy 	if (err < 0) {
247526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
247626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
247726932566SPatrick McHardy 		kfree_skb(skb);
247826932566SPatrick McHardy 		goto errout;
247926932566SPatrick McHardy 	}
24801ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
24815578689aSDaniel Lezcano 		    info->nlh, gfp_any());
24821ce85fe4SPablo Neira Ayuso 	return;
248321713ebcSThomas Graf errout:
248421713ebcSThomas Graf 	if (err < 0)
24855578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
24861da177e4SLinus Torvalds }
24871da177e4SLinus Torvalds 
24888ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
24898ed67789SDaniel Lezcano 				unsigned long event, void *data)
24908ed67789SDaniel Lezcano {
24918ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2492c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
24938ed67789SDaniel Lezcano 
24948ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2495d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
24968ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
24978ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2498d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
24998ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2500d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
25018ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
25028ed67789SDaniel Lezcano #endif
25038ed67789SDaniel Lezcano 	}
25048ed67789SDaniel Lezcano 
25058ed67789SDaniel Lezcano 	return NOTIFY_OK;
25068ed67789SDaniel Lezcano }
25078ed67789SDaniel Lezcano 
25081da177e4SLinus Torvalds /*
25091da177e4SLinus Torvalds  *	/proc
25101da177e4SLinus Torvalds  */
25111da177e4SLinus Torvalds 
25121da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
25131da177e4SLinus Torvalds 
25141da177e4SLinus Torvalds struct rt6_proc_arg
25151da177e4SLinus Torvalds {
25161da177e4SLinus Torvalds 	char *buffer;
25171da177e4SLinus Torvalds 	int offset;
25181da177e4SLinus Torvalds 	int length;
25191da177e4SLinus Torvalds 	int skip;
25201da177e4SLinus Torvalds 	int len;
25211da177e4SLinus Torvalds };
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
25241da177e4SLinus Torvalds {
252533120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
25261da177e4SLinus Torvalds 
25274b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
25281da177e4SLinus Torvalds 
25291da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
25304b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
25311da177e4SLinus Torvalds #else
253233120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
25331da177e4SLinus Torvalds #endif
25341da177e4SLinus Torvalds 
25351da177e4SLinus Torvalds 	if (rt->rt6i_nexthop) {
25364b7a4274SHarvey Harrison 		seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key);
25371da177e4SLinus Torvalds 	} else {
253833120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
25391da177e4SLinus Torvalds 	}
254033120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2541d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2542d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
25431da177e4SLinus Torvalds 		   rt->rt6i_dev ? rt->rt6i_dev->name : "");
25441da177e4SLinus Torvalds 	return 0;
25451da177e4SLinus Torvalds }
25461da177e4SLinus Torvalds 
254733120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
25481da177e4SLinus Torvalds {
2549f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
2550f3db4851SDaniel Lezcano 	fib6_clean_all(net, rt6_info_route, 0, m);
255133120b30SAlexey Dobriyan 	return 0;
25521da177e4SLinus Torvalds }
25531da177e4SLinus Torvalds 
255433120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
255533120b30SAlexey Dobriyan {
2556de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2557f3db4851SDaniel Lezcano }
2558f3db4851SDaniel Lezcano 
255933120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
256033120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
256133120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
256233120b30SAlexey Dobriyan 	.read		= seq_read,
256333120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2564b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
256533120b30SAlexey Dobriyan };
256633120b30SAlexey Dobriyan 
25671da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
25681da177e4SLinus Torvalds {
256969ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
25701da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
257169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
257269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
257369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
257469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
257569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2576fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
257769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
25781da177e4SLinus Torvalds 
25791da177e4SLinus Torvalds 	return 0;
25801da177e4SLinus Torvalds }
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
25831da177e4SLinus Torvalds {
2584de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
258569ddb805SDaniel Lezcano }
258669ddb805SDaniel Lezcano 
25879a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
25881da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
25891da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
25901da177e4SLinus Torvalds 	.read	 = seq_read,
25911da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2592b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
25931da177e4SLinus Torvalds };
25941da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
25951da177e4SLinus Torvalds 
25961da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds static
25998d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
26001da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
26011da177e4SLinus Torvalds {
26025b7c931dSDaniel Lezcano 	struct net *net = current->nsproxy->net_ns;
26035b7c931dSDaniel Lezcano 	int delay = net->ipv6.sysctl.flush_delay;
26041da177e4SLinus Torvalds 	if (write) {
26058d65af78SAlexey Dobriyan 		proc_dointvec(ctl, write, buffer, lenp, ppos);
26065b7c931dSDaniel Lezcano 		fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
26071da177e4SLinus Torvalds 		return 0;
26081da177e4SLinus Torvalds 	} else
26091da177e4SLinus Torvalds 		return -EINVAL;
26101da177e4SLinus Torvalds }
26111da177e4SLinus Torvalds 
2612760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
26131da177e4SLinus Torvalds 	{
26141da177e4SLinus Torvalds 		.procname	=	"flush",
26154990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
26161da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
261789c8b3a1SDave Jones 		.mode		=	0200,
26186d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
26191da177e4SLinus Torvalds 	},
26201da177e4SLinus Torvalds 	{
26211da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
26229a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
26231da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26241da177e4SLinus Torvalds 		.mode		=	0644,
26256d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
26261da177e4SLinus Torvalds 	},
26271da177e4SLinus Torvalds 	{
26281da177e4SLinus Torvalds 		.procname	=	"max_size",
26294990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
26301da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26311da177e4SLinus Torvalds 		.mode		=	0644,
26326d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
26331da177e4SLinus Torvalds 	},
26341da177e4SLinus Torvalds 	{
26351da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
26364990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
26371da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26381da177e4SLinus Torvalds 		.mode		=	0644,
26396d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
26401da177e4SLinus Torvalds 	},
26411da177e4SLinus Torvalds 	{
26421da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
26434990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
26441da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26451da177e4SLinus Torvalds 		.mode		=	0644,
26466d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
26471da177e4SLinus Torvalds 	},
26481da177e4SLinus Torvalds 	{
26491da177e4SLinus Torvalds 		.procname	=	"gc_interval",
26504990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
26511da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26521da177e4SLinus Torvalds 		.mode		=	0644,
26536d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
26541da177e4SLinus Torvalds 	},
26551da177e4SLinus Torvalds 	{
26561da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
26574990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
26581da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26591da177e4SLinus Torvalds 		.mode		=	0644,
2660f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
26611da177e4SLinus Torvalds 	},
26621da177e4SLinus Torvalds 	{
26631da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
26644990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
26651da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26661da177e4SLinus Torvalds 		.mode		=	0644,
26676d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
26681da177e4SLinus Torvalds 	},
26691da177e4SLinus Torvalds 	{
26701da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
26714990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
26721da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26731da177e4SLinus Torvalds 		.mode		=	0644,
2674f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
26751da177e4SLinus Torvalds 	},
26761da177e4SLinus Torvalds 	{
26771da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
26784990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
26791da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
26801da177e4SLinus Torvalds 		.mode		=	0644,
26816d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
26821da177e4SLinus Torvalds 	},
2683f8572d8fSEric W. Biederman 	{ }
26841da177e4SLinus Torvalds };
26851da177e4SLinus Torvalds 
26862c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2687760f2d01SDaniel Lezcano {
2688760f2d01SDaniel Lezcano 	struct ctl_table *table;
2689760f2d01SDaniel Lezcano 
2690760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2691760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2692760f2d01SDaniel Lezcano 			GFP_KERNEL);
26935ee09105SYOSHIFUJI Hideaki 
26945ee09105SYOSHIFUJI Hideaki 	if (table) {
26955ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
269686393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
26975ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
26985ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
26995ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
27005ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
27015ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
27025ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
27035ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
27049c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
27055ee09105SYOSHIFUJI Hideaki 	}
27065ee09105SYOSHIFUJI Hideaki 
2707760f2d01SDaniel Lezcano 	return table;
2708760f2d01SDaniel Lezcano }
27091da177e4SLinus Torvalds #endif
27101da177e4SLinus Torvalds 
27112c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2712cdb18761SDaniel Lezcano {
2713633d424bSPavel Emelyanov 	int ret = -ENOMEM;
27148ed67789SDaniel Lezcano 
271586393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
271686393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2717f2fc6a54SBenjamin Thery 
2718fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2719fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2720fc66f95cSEric Dumazet 
27218ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
27228ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
27238ed67789SDaniel Lezcano 					   GFP_KERNEL);
27248ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2725fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2726d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
27278ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2728d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
272962fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
273062fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
27318ed67789SDaniel Lezcano 
27328ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
27338ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
27348ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
27358ed67789SDaniel Lezcano 					       GFP_KERNEL);
273668fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
273768fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2738d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
27398ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2740d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
274162fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
274262fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
27438ed67789SDaniel Lezcano 
27448ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
27458ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
27468ed67789SDaniel Lezcano 					       GFP_KERNEL);
274768fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
274868fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2749d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
27508ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2751d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
275262fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
275362fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
27548ed67789SDaniel Lezcano #endif
27558ed67789SDaniel Lezcano 
2756b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2757b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2758b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2759b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2760b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2761b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2762b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2763b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2764b339a47cSPeter Zijlstra 
2765cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2766cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2767cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2768cdb18761SDaniel Lezcano #endif
27696891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
27706891a346SBenjamin Thery 
27718ed67789SDaniel Lezcano 	ret = 0;
27728ed67789SDaniel Lezcano out:
27738ed67789SDaniel Lezcano 	return ret;
2774f2fc6a54SBenjamin Thery 
277568fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
277668fffc67SPeter Zijlstra out_ip6_prohibit_entry:
277768fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
277868fffc67SPeter Zijlstra out_ip6_null_entry:
277968fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
278068fffc67SPeter Zijlstra #endif
2781fc66f95cSEric Dumazet out_ip6_dst_entries:
2782fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2783f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2784f2fc6a54SBenjamin Thery 	goto out;
2785cdb18761SDaniel Lezcano }
2786cdb18761SDaniel Lezcano 
27872c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2788cdb18761SDaniel Lezcano {
2789cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2790cdb18761SDaniel Lezcano 	proc_net_remove(net, "ipv6_route");
2791cdb18761SDaniel Lezcano 	proc_net_remove(net, "rt6_stats");
2792cdb18761SDaniel Lezcano #endif
27938ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
27948ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
27958ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
27968ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
27978ed67789SDaniel Lezcano #endif
279841bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2799cdb18761SDaniel Lezcano }
2800cdb18761SDaniel Lezcano 
2801cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
2802cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
2803cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
2804cdb18761SDaniel Lezcano };
2805cdb18761SDaniel Lezcano 
28068ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
28078ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
28088ed67789SDaniel Lezcano 	.priority = 0,
28098ed67789SDaniel Lezcano };
28108ed67789SDaniel Lezcano 
2811433d49c3SDaniel Lezcano int __init ip6_route_init(void)
28121da177e4SLinus Torvalds {
2813433d49c3SDaniel Lezcano 	int ret;
2814433d49c3SDaniel Lezcano 
28159a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
28169a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
28179a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
28189a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
28199a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
2820c19a28e1SFernando Carrijo 		goto out;
282114e50e57SDavid S. Miller 
2822fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
28238ed67789SDaniel Lezcano 	if (ret)
2824bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
2825bdb3289fSDaniel Lezcano 
2826fc66f95cSEric Dumazet 	ret = register_pernet_subsys(&ip6_route_net_ops);
2827fc66f95cSEric Dumazet 	if (ret)
2828fc66f95cSEric Dumazet 		goto out_dst_entries;
2829fc66f95cSEric Dumazet 
28305dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
28315dc121e9SArnaud Ebalard 
28328ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
28338ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
28348ed67789SDaniel Lezcano 	 * manually for init_net */
2835d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
28368ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2837bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2838d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
28398ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2840d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
28418ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2842bdb3289fSDaniel Lezcano   #endif
2843433d49c3SDaniel Lezcano 	ret = fib6_init();
2844433d49c3SDaniel Lezcano 	if (ret)
28458ed67789SDaniel Lezcano 		goto out_register_subsys;
2846433d49c3SDaniel Lezcano 
2847433d49c3SDaniel Lezcano 	ret = xfrm6_init();
2848433d49c3SDaniel Lezcano 	if (ret)
2849cdb18761SDaniel Lezcano 		goto out_fib6_init;
2850c35b7e72SDaniel Lezcano 
2851433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
2852433d49c3SDaniel Lezcano 	if (ret)
2853433d49c3SDaniel Lezcano 		goto xfrm6_init;
28547e5449c2SDaniel Lezcano 
2855433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
2856433d49c3SDaniel Lezcano 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2857433d49c3SDaniel Lezcano 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2858433d49c3SDaniel Lezcano 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2859433d49c3SDaniel Lezcano 		goto fib6_rules_init;
2860433d49c3SDaniel Lezcano 
28618ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
2862cdb18761SDaniel Lezcano 	if (ret)
2863cdb18761SDaniel Lezcano 		goto fib6_rules_init;
28648ed67789SDaniel Lezcano 
2865433d49c3SDaniel Lezcano out:
2866433d49c3SDaniel Lezcano 	return ret;
2867433d49c3SDaniel Lezcano 
2868433d49c3SDaniel Lezcano fib6_rules_init:
2869433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
2870433d49c3SDaniel Lezcano xfrm6_init:
2871433d49c3SDaniel Lezcano 	xfrm6_fini();
2872433d49c3SDaniel Lezcano out_fib6_init:
2873433d49c3SDaniel Lezcano 	fib6_gc_cleanup();
28748ed67789SDaniel Lezcano out_register_subsys:
28758ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
2876fc66f95cSEric Dumazet out_dst_entries:
2877fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
2878433d49c3SDaniel Lezcano out_kmem_cache:
2879f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
2880433d49c3SDaniel Lezcano 	goto out;
28811da177e4SLinus Torvalds }
28821da177e4SLinus Torvalds 
28831da177e4SLinus Torvalds void ip6_route_cleanup(void)
28841da177e4SLinus Torvalds {
28858ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
2886101367c2SThomas Graf 	fib6_rules_cleanup();
28871da177e4SLinus Torvalds 	xfrm6_fini();
28881da177e4SLinus Torvalds 	fib6_gc_cleanup();
28898ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
289041bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
2891f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
28921da177e4SLinus Torvalds }
2893