xref: /openbmc/linux/net/ipv6/route.c (revision 2a0c451ade8e1783c5d453948289e4a978d417c9)
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 
27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
28f3213831SJoe Perches 
294fc268d2SRandy Dunlap #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/errno.h>
31bc3b2d7fSPaul Gortmaker #include <linux/export.h>
321da177e4SLinus Torvalds #include <linux/types.h>
331da177e4SLinus Torvalds #include <linux/times.h>
341da177e4SLinus Torvalds #include <linux/socket.h>
351da177e4SLinus Torvalds #include <linux/sockios.h>
361da177e4SLinus Torvalds #include <linux/net.h>
371da177e4SLinus Torvalds #include <linux/route.h>
381da177e4SLinus Torvalds #include <linux/netdevice.h>
391da177e4SLinus Torvalds #include <linux/in6.h>
407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
411da177e4SLinus Torvalds #include <linux/init.h>
421da177e4SLinus Torvalds #include <linux/if_arp.h>
431da177e4SLinus Torvalds #include <linux/proc_fs.h>
441da177e4SLinus Torvalds #include <linux/seq_file.h>
455b7c931dSDaniel Lezcano #include <linux/nsproxy.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
47457c4cbcSEric W. Biederman #include <net/net_namespace.h>
481da177e4SLinus Torvalds #include <net/snmp.h>
491da177e4SLinus Torvalds #include <net/ipv6.h>
501da177e4SLinus Torvalds #include <net/ip6_fib.h>
511da177e4SLinus Torvalds #include <net/ip6_route.h>
521da177e4SLinus Torvalds #include <net/ndisc.h>
531da177e4SLinus Torvalds #include <net/addrconf.h>
541da177e4SLinus Torvalds #include <net/tcp.h>
551da177e4SLinus Torvalds #include <linux/rtnetlink.h>
561da177e4SLinus Torvalds #include <net/dst.h>
571da177e4SLinus Torvalds #include <net/xfrm.h>
588d71740cSTom Tucker #include <net/netevent.h>
5921713ebcSThomas Graf #include <net/netlink.h>
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #include <asm/uaccess.h>
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
641da177e4SLinus Torvalds #include <linux/sysctl.h>
651da177e4SLinus Torvalds #endif
661da177e4SLinus Torvalds 
671716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
6821efcfa0SEric Dumazet 				    const struct in6_addr *dest);
691da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
700dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
71ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
721da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
731da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
741da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
751da177e4SLinus Torvalds 				       struct net_device *dev, int how);
76569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
791da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
801da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
811da177e4SLinus Torvalds static void		ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
821da177e4SLinus Torvalds 
8370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
84efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
85b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
86b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
8795c96174SEric Dumazet 					   unsigned int pref);
88efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
89b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
90b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
9170ceb4f5SYOSHIFUJI Hideaki #endif
9270ceb4f5SYOSHIFUJI Hideaki 
9306582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
9406582540SDavid S. Miller {
9506582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
9606582540SDavid S. Miller 	struct inet_peer *peer;
9706582540SDavid S. Miller 	u32 *p = NULL;
9806582540SDavid S. Miller 
998e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1008e2ec639SYan, Zheng 		return NULL;
1018e2ec639SYan, Zheng 
10206582540SDavid S. Miller 	if (!rt->rt6i_peer)
10306582540SDavid S. Miller 		rt6_bind_peer(rt, 1);
10406582540SDavid S. Miller 
10506582540SDavid S. Miller 	peer = rt->rt6i_peer;
10606582540SDavid S. Miller 	if (peer) {
10706582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
10806582540SDavid S. Miller 		unsigned long prev, new;
10906582540SDavid S. Miller 
11006582540SDavid S. Miller 		p = peer->metrics;
11106582540SDavid S. Miller 		if (inet_metrics_new(peer))
11206582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
11306582540SDavid S. Miller 
11406582540SDavid S. Miller 		new = (unsigned long) p;
11506582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
11606582540SDavid S. Miller 
11706582540SDavid S. Miller 		if (prev != old) {
11806582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
11906582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
12006582540SDavid S. Miller 				p = NULL;
12106582540SDavid S. Miller 		}
12206582540SDavid S. Miller 	}
12306582540SDavid S. Miller 	return p;
12406582540SDavid S. Miller }
12506582540SDavid S. Miller 
12639232973SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, const void *daddr)
12739232973SDavid S. Miller {
12839232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
12939232973SDavid S. Miller 
130a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
13139232973SDavid S. Miller 		return (const void *) p;
13239232973SDavid S. Miller 	return daddr;
13339232973SDavid S. Miller }
13439232973SDavid S. Miller 
135d3aaeb38SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, const void *daddr)
136d3aaeb38SDavid S. Miller {
13739232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
13839232973SDavid S. Miller 	struct neighbour *n;
13939232973SDavid S. Miller 
14039232973SDavid S. Miller 	daddr = choose_neigh_daddr(rt, daddr);
14139232973SDavid S. Miller 	n = __ipv6_neigh_lookup(&nd_tbl, dst->dev, daddr);
142f83c7790SDavid S. Miller 	if (n)
143f83c7790SDavid S. Miller 		return n;
144f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
145f83c7790SDavid S. Miller }
146f83c7790SDavid S. Miller 
1478ade06c6SDavid S. Miller static int rt6_bind_neighbour(struct rt6_info *rt, struct net_device *dev)
148f83c7790SDavid S. Miller {
1498ade06c6SDavid S. Miller 	struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dev, &rt->rt6i_gateway);
1508ade06c6SDavid S. Miller 	if (!n) {
1518ade06c6SDavid S. Miller 		n = neigh_create(&nd_tbl, &rt->rt6i_gateway, dev);
152f83c7790SDavid S. Miller 		if (IS_ERR(n))
153f83c7790SDavid S. Miller 			return PTR_ERR(n);
1548ade06c6SDavid S. Miller 	}
155f83c7790SDavid S. Miller 	dst_set_neighbour(&rt->dst, n);
156f83c7790SDavid S. Miller 
157f83c7790SDavid S. Miller 	return 0;
158d3aaeb38SDavid S. Miller }
159d3aaeb38SDavid S. Miller 
1609a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1611da177e4SLinus Torvalds 	.family			=	AF_INET6,
16209640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1631da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1641da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1651da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1660dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
167ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
16806582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1691da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1701da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1711da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1721da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1731da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1741ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
175d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1761da177e4SLinus Torvalds };
1771da177e4SLinus Torvalds 
178ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
179ec831ea7SRoland Dreier {
180618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
181618f9bc7SSteffen Klassert 
182618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
183ec831ea7SRoland Dreier }
184ec831ea7SRoland Dreier 
18514e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
18614e50e57SDavid S. Miller {
18714e50e57SDavid S. Miller }
18814e50e57SDavid S. Miller 
1890972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1900972ddb2SHeld Bernhard 					 unsigned long old)
1910972ddb2SHeld Bernhard {
1920972ddb2SHeld Bernhard 	return NULL;
1930972ddb2SHeld Bernhard }
1940972ddb2SHeld Bernhard 
19514e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
19614e50e57SDavid S. Miller 	.family			=	AF_INET6,
19709640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
19814e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
19914e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
200ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
201214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
20214e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
2030972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
204d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
20514e50e57SDavid S. Miller };
20614e50e57SDavid S. Miller 
20762fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
20862fa8a84SDavid S. Miller 	[RTAX_HOPLIMIT - 1] = 255,
20962fa8a84SDavid S. Miller };
21062fa8a84SDavid S. Miller 
211bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = {
2121da177e4SLinus Torvalds 	.dst = {
2131da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2141da177e4SLinus Torvalds 		.__use		= 1,
2151da177e4SLinus Torvalds 		.obsolete	= -1,
2161da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2171da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2181da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2191da177e4SLinus Torvalds 	},
2201da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2214f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2221da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2231da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2241da177e4SLinus Torvalds };
2251da177e4SLinus Torvalds 
226101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
227101367c2SThomas Graf 
2286723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
2296723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
2306723ab54SDavid S. Miller 
231280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = {
232101367c2SThomas Graf 	.dst = {
233101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
234101367c2SThomas Graf 		.__use		= 1,
235101367c2SThomas Graf 		.obsolete	= -1,
236101367c2SThomas Graf 		.error		= -EACCES,
2379ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2389ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
239101367c2SThomas Graf 	},
240101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2414f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
242101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
243101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
244101367c2SThomas Graf };
245101367c2SThomas Graf 
246bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = {
247101367c2SThomas Graf 	.dst = {
248101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
249101367c2SThomas Graf 		.__use		= 1,
250101367c2SThomas Graf 		.obsolete	= -1,
251101367c2SThomas Graf 		.error		= -EINVAL,
252352e512cSHerbert Xu 		.input		= dst_discard,
253352e512cSHerbert Xu 		.output		= dst_discard,
254101367c2SThomas Graf 	},
255101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2564f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
257101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
258101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
259101367c2SThomas Graf };
260101367c2SThomas Graf 
261101367c2SThomas Graf #endif
262101367c2SThomas Graf 
2631da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
2645c1e6aa3SDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops,
265957c665fSDavid S. Miller 					     struct net_device *dev,
266957c665fSDavid S. Miller 					     int flags)
2671da177e4SLinus Torvalds {
268957c665fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, flags);
269cf911662SDavid S. Miller 
27038308473SDavid S. Miller 	if (rt)
271fbe58186SMadalin Bucur 		memset(&rt->rt6i_table, 0,
272fbe58186SMadalin Bucur 		       sizeof(*rt) - sizeof(struct dst_entry));
273cf911662SDavid S. Miller 
274cf911662SDavid S. Miller 	return rt;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2801da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
281b3419363SDavid S. Miller 	struct inet_peer *peer = rt->rt6i_peer;
2821da177e4SLinus Torvalds 
2838e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
2848e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
2858e2ec639SYan, Zheng 
28638308473SDavid S. Miller 	if (idev) {
2871da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
2881da177e4SLinus Torvalds 		in6_dev_put(idev);
2891da177e4SLinus Torvalds 	}
2901716a961SGao feng 
2911716a961SGao feng 	if (!(rt->rt6i_flags & RTF_EXPIRES) && dst->from)
2921716a961SGao feng 		dst_release(dst->from);
2931716a961SGao feng 
294b3419363SDavid S. Miller 	if (peer) {
295b3419363SDavid S. Miller 		rt->rt6i_peer = NULL;
296b3419363SDavid S. Miller 		inet_putpeer(peer);
297b3419363SDavid S. Miller 	}
298b3419363SDavid S. Miller }
299b3419363SDavid S. Miller 
3006431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
3016431cbc2SDavid S. Miller 
3026431cbc2SDavid S. Miller static u32 rt6_peer_genid(void)
3036431cbc2SDavid S. Miller {
3046431cbc2SDavid S. Miller 	return atomic_read(&__rt6_peer_genid);
3056431cbc2SDavid S. Miller }
3066431cbc2SDavid S. Miller 
307b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
308b3419363SDavid S. Miller {
309b3419363SDavid S. Miller 	struct inet_peer *peer;
310b3419363SDavid S. Miller 
311b3419363SDavid S. Miller 	peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
312b3419363SDavid S. Miller 	if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
313b3419363SDavid S. Miller 		inet_putpeer(peer);
3146431cbc2SDavid S. Miller 	else
3156431cbc2SDavid S. Miller 		rt->rt6i_peer_genid = rt6_peer_genid();
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3191da177e4SLinus Torvalds 			   int how)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3221da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3235a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
324c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3251da177e4SLinus Torvalds 
32638308473SDavid S. Miller 	if (dev != loopback_dev && idev && idev->dev == dev) {
3275a3e55d6SDenis V. Lunev 		struct inet6_dev *loopback_idev =
3285a3e55d6SDenis V. Lunev 			in6_dev_get(loopback_dev);
32938308473SDavid S. Miller 		if (loopback_idev) {
3301da177e4SLinus Torvalds 			rt->rt6i_idev = loopback_idev;
3311da177e4SLinus Torvalds 			in6_dev_put(idev);
3321da177e4SLinus Torvalds 		}
3331da177e4SLinus Torvalds 	}
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
336a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3371da177e4SLinus Torvalds {
3381716a961SGao feng 	struct rt6_info *ort = NULL;
3391716a961SGao feng 
3401716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3411716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
342a50feda5SEric Dumazet 			return true;
3431716a961SGao feng 	} else if (rt->dst.from) {
3441716a961SGao feng 		ort = (struct rt6_info *) rt->dst.from;
3451716a961SGao feng 		return (ort->rt6i_flags & RTF_EXPIRES) &&
3461716a961SGao feng 			time_after(jiffies, ort->dst.expires);
3471716a961SGao feng 	}
348a50feda5SEric Dumazet 	return false;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
351a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr)
352c71099acSThomas Graf {
353a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
354a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
355c71099acSThomas Graf }
356c71099acSThomas Graf 
3571da177e4SLinus Torvalds /*
358c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
3591da177e4SLinus Torvalds  */
3601da177e4SLinus Torvalds 
3618ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
3628ed67789SDaniel Lezcano 						    struct rt6_info *rt,
363b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
3641da177e4SLinus Torvalds 						    int oif,
365d420895eSYOSHIFUJI Hideaki 						    int flags)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
3681da177e4SLinus Torvalds 	struct rt6_info *sprt;
3691da177e4SLinus Torvalds 
370dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
371dd3abc4eSYOSHIFUJI Hideaki 		goto out;
372dd3abc4eSYOSHIFUJI Hideaki 
373d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
374d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
375dd3abc4eSYOSHIFUJI Hideaki 
376dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
3771da177e4SLinus Torvalds 			if (dev->ifindex == oif)
3781da177e4SLinus Torvalds 				return sprt;
3791da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
38038308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
3811da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
382d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
3831da177e4SLinus Torvalds 						continue;
3841da177e4SLinus Torvalds 					if (local && (!oif ||
3851da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
3861da177e4SLinus Torvalds 						continue;
3871da177e4SLinus Torvalds 				}
3881da177e4SLinus Torvalds 				local = sprt;
3891da177e4SLinus Torvalds 			}
390dd3abc4eSYOSHIFUJI Hideaki 		} else {
391dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
392dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
393dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
394dd3abc4eSYOSHIFUJI Hideaki 		}
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 
397dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
3981da177e4SLinus Torvalds 		if (local)
3991da177e4SLinus Torvalds 			return local;
4001da177e4SLinus Torvalds 
401d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4028ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4031da177e4SLinus Torvalds 	}
404dd3abc4eSYOSHIFUJI Hideaki out:
4051da177e4SLinus Torvalds 	return rt;
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
40827097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
40927097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
41027097255SYOSHIFUJI Hideaki {
411f2c31e32SEric Dumazet 	struct neighbour *neigh;
41227097255SYOSHIFUJI Hideaki 	/*
41327097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
41427097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
41527097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
41627097255SYOSHIFUJI Hideaki 	 *
41727097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
41827097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
41927097255SYOSHIFUJI Hideaki 	 */
420f2c31e32SEric Dumazet 	rcu_read_lock();
42127217455SDavid Miller 	neigh = rt ? dst_get_neighbour_noref(&rt->dst) : NULL;
42227097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
423f2c31e32SEric Dumazet 		goto out;
42427097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
42527097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
42652e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
42727097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
42827097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
42927097255SYOSHIFUJI Hideaki 
43027097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
43127097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
43227097255SYOSHIFUJI Hideaki 
43327097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
43427097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
435d1918542SDavid S. Miller 		ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
436f2c31e32SEric Dumazet 	} else {
43727097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
43827097255SYOSHIFUJI Hideaki 	}
439f2c31e32SEric Dumazet out:
440f2c31e32SEric Dumazet 	rcu_read_unlock();
441f2c31e32SEric Dumazet }
44227097255SYOSHIFUJI Hideaki #else
44327097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
44427097255SYOSHIFUJI Hideaki {
44527097255SYOSHIFUJI Hideaki }
44627097255SYOSHIFUJI Hideaki #endif
44727097255SYOSHIFUJI Hideaki 
4481da177e4SLinus Torvalds /*
449554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
4501da177e4SLinus Torvalds  */
451b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
4521da177e4SLinus Torvalds {
453d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
454161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
455554cfb7eSYOSHIFUJI Hideaki 		return 2;
456161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
457161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
458161980f4SDavid S. Miller 		return 1;
459554cfb7eSYOSHIFUJI Hideaki 	return 0;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
462b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
4631da177e4SLinus Torvalds {
464f2c31e32SEric Dumazet 	struct neighbour *neigh;
465398bcbebSYOSHIFUJI Hideaki 	int m;
466f2c31e32SEric Dumazet 
467f2c31e32SEric Dumazet 	rcu_read_lock();
46827217455SDavid Miller 	neigh = dst_get_neighbour_noref(&rt->dst);
4694d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
4704d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
4714d0c5911SYOSHIFUJI Hideaki 		m = 1;
4724d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
4731da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
474554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
4754d0c5911SYOSHIFUJI Hideaki 			m = 2;
476398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
477398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
478398bcbebSYOSHIFUJI Hideaki 			m = 0;
479398bcbebSYOSHIFUJI Hideaki #endif
480398bcbebSYOSHIFUJI Hideaki 		else
481ea73ee23SYOSHIFUJI Hideaki 			m = 1;
4821da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
483398bcbebSYOSHIFUJI Hideaki 	} else
484398bcbebSYOSHIFUJI Hideaki 		m = 0;
485f2c31e32SEric Dumazet 	rcu_read_unlock();
486554cfb7eSYOSHIFUJI Hideaki 	return m;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
489554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
490554cfb7eSYOSHIFUJI Hideaki 			   int strict)
491554cfb7eSYOSHIFUJI Hideaki {
4924d0c5911SYOSHIFUJI Hideaki 	int m, n;
4934d0c5911SYOSHIFUJI Hideaki 
4944d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
49577d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
496554cfb7eSYOSHIFUJI Hideaki 		return -1;
497ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
498ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
499ebacaaa0SYOSHIFUJI Hideaki #endif
5004d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
501557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
502554cfb7eSYOSHIFUJI Hideaki 		return -1;
503554cfb7eSYOSHIFUJI Hideaki 	return m;
504554cfb7eSYOSHIFUJI Hideaki }
505554cfb7eSYOSHIFUJI Hideaki 
506f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
507f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
508554cfb7eSYOSHIFUJI Hideaki {
509554cfb7eSYOSHIFUJI Hideaki 	int m;
510554cfb7eSYOSHIFUJI Hideaki 
511554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
512f11e6659SDavid S. Miller 		goto out;
513554cfb7eSYOSHIFUJI Hideaki 
514554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
515554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
516f11e6659SDavid S. Miller 		goto out;
517554cfb7eSYOSHIFUJI Hideaki 
518f11e6659SDavid S. Miller 	if (m > *mpri) {
519ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
52027097255SYOSHIFUJI Hideaki 			rt6_probe(match);
521f11e6659SDavid S. Miller 		*mpri = m;
522554cfb7eSYOSHIFUJI Hideaki 		match = rt;
523ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
52427097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
5251da177e4SLinus Torvalds 	}
526f11e6659SDavid S. Miller 
527f11e6659SDavid S. Miller out:
528f11e6659SDavid S. Miller 	return match;
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
531f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
532f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
533f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
534f11e6659SDavid S. Miller {
535f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
536f11e6659SDavid S. Miller 	int mpri = -1;
537f11e6659SDavid S. Miller 
538f11e6659SDavid S. Miller 	match = NULL;
539f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
540d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
541f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
542f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
543d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
544f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
545f11e6659SDavid S. Miller 
546f11e6659SDavid S. Miller 	return match;
547f11e6659SDavid S. Miller }
548f11e6659SDavid S. Miller 
549f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
550f11e6659SDavid S. Miller {
551f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
5528ed67789SDaniel Lezcano 	struct net *net;
553f11e6659SDavid S. Miller 
554f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
555f11e6659SDavid S. Miller 	if (!rt0)
556f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
557f11e6659SDavid S. Miller 
558f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
559f11e6659SDavid S. Miller 
560554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
561f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
562d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
563f11e6659SDavid S. Miller 
564554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
565f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
566f11e6659SDavid S. Miller 			next = fn->leaf;
567f11e6659SDavid S. Miller 
568f11e6659SDavid S. Miller 		if (next != rt0)
569f11e6659SDavid S. Miller 			fn->rr_ptr = next;
570554cfb7eSYOSHIFUJI Hideaki 	}
571554cfb7eSYOSHIFUJI Hideaki 
572d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
573a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
5741da177e4SLinus Torvalds }
5751da177e4SLinus Torvalds 
57670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
57770ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
578b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
57970ceb4f5SYOSHIFUJI Hideaki {
580c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
58170ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
58270ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
58370ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
5844bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
58570ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
58670ceb4f5SYOSHIFUJI Hideaki 
58770ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
58870ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
58970ceb4f5SYOSHIFUJI Hideaki 	}
59070ceb4f5SYOSHIFUJI Hideaki 
59170ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
59270ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
59370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
59470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
59570ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
59670ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
59770ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
59870ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
59970ceb4f5SYOSHIFUJI Hideaki 		}
60070ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
60170ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
60270ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
60370ceb4f5SYOSHIFUJI Hideaki 		}
60470ceb4f5SYOSHIFUJI Hideaki 	}
60570ceb4f5SYOSHIFUJI Hideaki 
60670ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
60770ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
6083933fc95SJens Rosenboom 		return -EINVAL;
60970ceb4f5SYOSHIFUJI Hideaki 
6104bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
61170ceb4f5SYOSHIFUJI Hideaki 
61270ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
61370ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
61470ceb4f5SYOSHIFUJI Hideaki 	else {
61570ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
61670ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
61770ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
61870ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
61970ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
62070ceb4f5SYOSHIFUJI Hideaki 	}
62170ceb4f5SYOSHIFUJI Hideaki 
622efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
623efa2cea0SDaniel Lezcano 				dev->ifindex);
62470ceb4f5SYOSHIFUJI Hideaki 
62570ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
626e0a1ad73SThomas Graf 		ip6_del_rt(rt);
62770ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
62870ceb4f5SYOSHIFUJI Hideaki 	}
62970ceb4f5SYOSHIFUJI Hideaki 
63070ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
631efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
63270ceb4f5SYOSHIFUJI Hideaki 					pref);
63370ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
63470ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
63570ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
63670ceb4f5SYOSHIFUJI Hideaki 
63770ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
6381716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
6391716a961SGao feng 			rt6_clean_expires(rt);
6401716a961SGao feng 		else
6411716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
6421716a961SGao feng 
643d8d1f30bSChangli Gao 		dst_release(&rt->dst);
64470ceb4f5SYOSHIFUJI Hideaki 	}
64570ceb4f5SYOSHIFUJI Hideaki 	return 0;
64670ceb4f5SYOSHIFUJI Hideaki }
64770ceb4f5SYOSHIFUJI Hideaki #endif
64870ceb4f5SYOSHIFUJI Hideaki 
6498ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
650982f56f3SYOSHIFUJI Hideaki do { \
6518ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
652982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
653e0eda7bbSVille Nuorvala 		while (1) { \
654982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
655c71099acSThomas Graf 				goto out; \
656982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
657982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
6588bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
659982f56f3SYOSHIFUJI Hideaki 			else \
660982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
661c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
662c71099acSThomas Graf 				goto restart; \
663c71099acSThomas Graf 		} \
664982f56f3SYOSHIFUJI Hideaki 	} \
665982f56f3SYOSHIFUJI Hideaki } while (0)
666c71099acSThomas Graf 
6678ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
6688ed67789SDaniel Lezcano 					     struct fib6_table *table,
6694c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
6701da177e4SLinus Torvalds {
6711da177e4SLinus Torvalds 	struct fib6_node *fn;
6721da177e4SLinus Torvalds 	struct rt6_info *rt;
6731da177e4SLinus Torvalds 
674c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
6754c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
676c71099acSThomas Graf restart:
677c71099acSThomas Graf 	rt = fn->leaf;
6784c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
6794c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
680c71099acSThomas Graf out:
681d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
682c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
6831da177e4SLinus Torvalds 	return rt;
684c71099acSThomas Graf 
685c71099acSThomas Graf }
686c71099acSThomas Graf 
687ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
688ea6e574eSFlorian Westphal 				    int flags)
689ea6e574eSFlorian Westphal {
690ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
691ea6e574eSFlorian Westphal }
692ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
693ea6e574eSFlorian Westphal 
6949acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
6959acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
696c71099acSThomas Graf {
6974c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
6984c9483b2SDavid S. Miller 		.flowi6_oif = oif,
6994c9483b2SDavid S. Miller 		.daddr = *daddr,
700c71099acSThomas Graf 	};
701c71099acSThomas Graf 	struct dst_entry *dst;
70277d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
703c71099acSThomas Graf 
704adaa70bbSThomas Graf 	if (saddr) {
7054c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
706adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
707adaa70bbSThomas Graf 	}
708adaa70bbSThomas Graf 
7094c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
710c71099acSThomas Graf 	if (dst->error == 0)
711c71099acSThomas Graf 		return (struct rt6_info *) dst;
712c71099acSThomas Graf 
713c71099acSThomas Graf 	dst_release(dst);
714c71099acSThomas Graf 
7151da177e4SLinus Torvalds 	return NULL;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7187159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
7197159039aSYOSHIFUJI Hideaki 
720c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
7211da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
7221da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
7231da177e4SLinus Torvalds    be destroyed.
7241da177e4SLinus Torvalds  */
7251da177e4SLinus Torvalds 
72686872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
7271da177e4SLinus Torvalds {
7281da177e4SLinus Torvalds 	int err;
729c71099acSThomas Graf 	struct fib6_table *table;
7301da177e4SLinus Torvalds 
731c71099acSThomas Graf 	table = rt->rt6i_table;
732c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
73386872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
734c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	return err;
7371da177e4SLinus Torvalds }
7381da177e4SLinus Torvalds 
73940e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
74040e22e8fSThomas Graf {
7414d1169c1SDenis V. Lunev 	struct nl_info info = {
742d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
7434d1169c1SDenis V. Lunev 	};
744528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
74540e22e8fSThomas Graf }
74640e22e8fSThomas Graf 
7471716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
74821efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
749b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	struct rt6_info *rt;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	/*
7541da177e4SLinus Torvalds 	 *	Clone the route.
7551da177e4SLinus Torvalds 	 */
7561da177e4SLinus Torvalds 
75721efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	if (rt) {
76014deae41SDavid S. Miller 		int attempts = !in_softirq();
76114deae41SDavid S. Miller 
76258c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags & RTF_GATEWAY)) {
763bb3c3686SDavid S. Miller 			if (ort->rt6i_dst.plen != 128 &&
76421efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
76558c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
7664e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = *daddr;
76758c4fb86SYOSHIFUJI Hideaki 		}
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
7721da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
7734e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
7741da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
7751da177e4SLinus Torvalds 		}
7761da177e4SLinus Torvalds #endif
7771da177e4SLinus Torvalds 
77814deae41SDavid S. Miller 	retry:
7798ade06c6SDavid S. Miller 		if (rt6_bind_neighbour(rt, rt->dst.dev)) {
780d1918542SDavid S. Miller 			struct net *net = dev_net(rt->dst.dev);
78114deae41SDavid S. Miller 			int saved_rt_min_interval =
78214deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval;
78314deae41SDavid S. Miller 			int saved_rt_elasticity =
78414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity;
78514deae41SDavid S. Miller 
78614deae41SDavid S. Miller 			if (attempts-- > 0) {
78714deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
78814deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
78914deae41SDavid S. Miller 
79086393e52SAlexey Dobriyan 				ip6_dst_gc(&net->ipv6.ip6_dst_ops);
79114deae41SDavid S. Miller 
79214deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity =
79314deae41SDavid S. Miller 					saved_rt_elasticity;
79414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval =
79514deae41SDavid S. Miller 					saved_rt_min_interval;
79614deae41SDavid S. Miller 				goto retry;
79714deae41SDavid S. Miller 			}
79814deae41SDavid S. Miller 
799f3213831SJoe Perches 			net_warn_ratelimited("Neighbour table overflow\n");
800d8d1f30bSChangli Gao 			dst_free(&rt->dst);
80114deae41SDavid S. Miller 			return NULL;
80214deae41SDavid S. Miller 		}
80395a9a5baSYOSHIFUJI Hideaki 	}
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	return rt;
8061da177e4SLinus Torvalds }
80795a9a5baSYOSHIFUJI Hideaki 
80821efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
80921efcfa0SEric Dumazet 					const struct in6_addr *daddr)
810299d9939SYOSHIFUJI Hideaki {
81121efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
81221efcfa0SEric Dumazet 
813299d9939SYOSHIFUJI Hideaki 	if (rt) {
814299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
81527217455SDavid Miller 		dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_noref_raw(&ort->dst)));
816299d9939SYOSHIFUJI Hideaki 	}
817299d9939SYOSHIFUJI Hideaki 	return rt;
818299d9939SYOSHIFUJI Hideaki }
819299d9939SYOSHIFUJI Hideaki 
8208ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
8214c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	struct fib6_node *fn;
824519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
825c71099acSThomas Graf 	int strict = 0;
8261da177e4SLinus Torvalds 	int attempts = 3;
827519fbd87SYOSHIFUJI Hideaki 	int err;
82853b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
8291da177e4SLinus Torvalds 
83077d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds relookup:
833c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8341da177e4SLinus Torvalds 
8358238dd06SYOSHIFUJI Hideaki restart_2:
8364c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds restart:
8394acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
8408ed67789SDaniel Lezcano 
8414c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
8428ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
8438238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
8441da177e4SLinus Torvalds 		goto out;
8451da177e4SLinus Torvalds 
846d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
847c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8481da177e4SLinus Torvalds 
84927217455SDavid Miller 	if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
8504c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
8517343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
8524c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
8537343ff31SDavid S. Miller 	else
8547343ff31SDavid S. Miller 		goto out2;
8551da177e4SLinus Torvalds 
856d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8578ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
8581da177e4SLinus Torvalds 
859d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
860e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
86140e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
862e40cf353SYOSHIFUJI Hideaki 		if (!err)
863e40cf353SYOSHIFUJI Hideaki 			goto out2;
864e40cf353SYOSHIFUJI Hideaki 	}
865e40cf353SYOSHIFUJI Hideaki 
866e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
8671da177e4SLinus Torvalds 		goto out2;
8681da177e4SLinus Torvalds 
869519fbd87SYOSHIFUJI Hideaki 	/*
870c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
871519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
8721da177e4SLinus Torvalds 	 */
873d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8741da177e4SLinus Torvalds 	goto relookup;
875e40cf353SYOSHIFUJI Hideaki 
876519fbd87SYOSHIFUJI Hideaki out:
8778238dd06SYOSHIFUJI Hideaki 	if (reachable) {
8788238dd06SYOSHIFUJI Hideaki 		reachable = 0;
8798238dd06SYOSHIFUJI Hideaki 		goto restart_2;
8808238dd06SYOSHIFUJI Hideaki 	}
881d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
882c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8831da177e4SLinus Torvalds out2:
884d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
885d8d1f30bSChangli Gao 	rt->dst.__use++;
886c71099acSThomas Graf 
887c71099acSThomas Graf 	return rt;
888c71099acSThomas Graf }
889c71099acSThomas Graf 
8908ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
8914c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
8924acad72dSPavel Emelyanov {
8934c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
8944acad72dSPavel Emelyanov }
8954acad72dSPavel Emelyanov 
89672331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
89772331bc0SShmulik Ladkani 						struct net_device *dev,
89872331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
89972331bc0SShmulik Ladkani {
90072331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
90172331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
90272331bc0SShmulik Ladkani 
90372331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
90472331bc0SShmulik Ladkani }
90572331bc0SShmulik Ladkani 
906c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
907c71099acSThomas Graf {
908b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
909c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
910adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
9114c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9124c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
9134c9483b2SDavid S. Miller 		.daddr = iph->daddr,
9144c9483b2SDavid S. Miller 		.saddr = iph->saddr,
9154c9483b2SDavid S. Miller 		.flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
9164c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
9174c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
918c71099acSThomas Graf 	};
919adaa70bbSThomas Graf 
92072331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
921c71099acSThomas Graf }
922c71099acSThomas Graf 
9238ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
9244c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
925c71099acSThomas Graf {
9264c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
927c71099acSThomas Graf }
928c71099acSThomas Graf 
9299c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
9304c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
931c71099acSThomas Graf {
932c71099acSThomas Graf 	int flags = 0;
933c71099acSThomas Graf 
9344c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
93577d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
936c71099acSThomas Graf 
9374c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
938adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
9390c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
9400c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
941adaa70bbSThomas Graf 
9424c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
9431da177e4SLinus Torvalds }
9441da177e4SLinus Torvalds 
9457159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
9461da177e4SLinus Torvalds 
9472774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
94814e50e57SDavid S. Miller {
9495c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
95014e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
95114e50e57SDavid S. Miller 
9525c1e6aa3SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
95314e50e57SDavid S. Miller 	if (rt) {
954cf911662SDavid S. Miller 		memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry));
955cf911662SDavid S. Miller 
956d8d1f30bSChangli Gao 		new = &rt->dst;
95714e50e57SDavid S. Miller 
95814e50e57SDavid S. Miller 		new->__use = 1;
959352e512cSHerbert Xu 		new->input = dst_discard;
960352e512cSHerbert Xu 		new->output = dst_discard;
96114e50e57SDavid S. Miller 
96221efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
96321efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
96421efcfa0SEric Dumazet 		else
965defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
96614e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
96714e50e57SDavid S. Miller 		if (rt->rt6i_idev)
96814e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
96914e50e57SDavid S. Miller 
9704e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
9711716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
9721716a961SGao feng 		rt6_clean_expires(rt);
97314e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
97414e50e57SDavid S. Miller 
97514e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
97614e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
97714e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
97814e50e57SDavid S. Miller #endif
97914e50e57SDavid S. Miller 
98014e50e57SDavid S. Miller 		dst_free(new);
98114e50e57SDavid S. Miller 	}
98214e50e57SDavid S. Miller 
98369ead7afSDavid S. Miller 	dst_release(dst_orig);
98469ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
98514e50e57SDavid S. Miller }
98614e50e57SDavid S. Miller 
9871da177e4SLinus Torvalds /*
9881da177e4SLinus Torvalds  *	Destination cache support functions
9891da177e4SLinus Torvalds  */
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
9921da177e4SLinus Torvalds {
9931da177e4SLinus Torvalds 	struct rt6_info *rt;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
9961da177e4SLinus Torvalds 
9976431cbc2SDavid S. Miller 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
9986431cbc2SDavid S. Miller 		if (rt->rt6i_peer_genid != rt6_peer_genid()) {
9996431cbc2SDavid S. Miller 			if (!rt->rt6i_peer)
10006431cbc2SDavid S. Miller 				rt6_bind_peer(rt, 0);
10016431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
10026431cbc2SDavid S. Miller 		}
10031da177e4SLinus Torvalds 		return dst;
10046431cbc2SDavid S. Miller 	}
10051da177e4SLinus Torvalds 	return NULL;
10061da177e4SLinus Torvalds }
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
10091da177e4SLinus Torvalds {
10101da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	if (rt) {
101354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
101454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1015e0a1ad73SThomas Graf 				ip6_del_rt(rt);
101654c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
10171da177e4SLinus Torvalds 			}
101854c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
101954c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
102054c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
102154c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
102254c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
102354c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
10271da177e4SLinus Torvalds {
10281da177e4SLinus Torvalds 	struct rt6_info *rt;
10291da177e4SLinus Torvalds 
10303ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
10311da177e4SLinus Torvalds 
1032adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
10331da177e4SLinus Torvalds 	if (rt) {
10341716a961SGao feng 		if (rt->rt6i_flags & RTF_CACHE)
10351716a961SGao feng 			rt6_update_expires(rt, 0);
10361716a961SGao feng 		else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
10371da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
10381da177e4SLinus Torvalds 	}
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
10421da177e4SLinus Torvalds {
10431da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
10461da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
10471da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1048defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
10491da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1050defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1051defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
10521da177e4SLinus Torvalds 		}
1053defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
10541da177e4SLinus Torvalds 	}
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
10570dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
10581da177e4SLinus Torvalds {
10590dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
10600dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
10610dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
10620dbaee3bSDavid S. Miller 
10631da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
10641da177e4SLinus Torvalds 
10655578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
10665578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	/*
10691da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
10701da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
10711da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
10721da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
10731da177e4SLinus Torvalds 	 */
10741da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
10751da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
10761da177e4SLinus Torvalds 	return mtu;
10771da177e4SLinus Torvalds }
10781da177e4SLinus Torvalds 
1079ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1080d33e4553SDavid S. Miller {
1081d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1082618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1083618f9bc7SSteffen Klassert 
1084618f9bc7SSteffen Klassert 	if (mtu)
1085618f9bc7SSteffen Klassert 		return mtu;
1086618f9bc7SSteffen Klassert 
1087618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1088d33e4553SDavid S. Miller 
1089d33e4553SDavid S. Miller 	rcu_read_lock();
1090d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1091d33e4553SDavid S. Miller 	if (idev)
1092d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1093d33e4553SDavid S. Miller 	rcu_read_unlock();
1094d33e4553SDavid S. Miller 
1095d33e4553SDavid S. Miller 	return mtu;
1096d33e4553SDavid S. Miller }
1097d33e4553SDavid S. Miller 
10983b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
10993b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
11005d0bbeebSThomas Graf 
11013b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
11021da177e4SLinus Torvalds 				  struct neighbour *neigh,
110387a11578SDavid S. Miller 				  struct flowi6 *fl6)
11041da177e4SLinus Torvalds {
110587a11578SDavid S. Miller 	struct dst_entry *dst;
11061da177e4SLinus Torvalds 	struct rt6_info *rt;
11071da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1108c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
11091da177e4SLinus Torvalds 
111038308473SDavid S. Miller 	if (unlikely(!idev))
1111122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
11121da177e4SLinus Torvalds 
1113957c665fSDavid S. Miller 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev, 0);
111438308473SDavid S. Miller 	if (unlikely(!rt)) {
11151da177e4SLinus Torvalds 		in6_dev_put(idev);
111687a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
11171da177e4SLinus Torvalds 		goto out;
11181da177e4SLinus Torvalds 	}
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	if (neigh)
11211da177e4SLinus Torvalds 		neigh_hold(neigh);
112214deae41SDavid S. Miller 	else {
1123f83c7790SDavid S. Miller 		neigh = ip6_neigh_lookup(&rt->dst, &fl6->daddr);
1124b43faac6SDavid S. Miller 		if (IS_ERR(neigh)) {
1125252c3d84SRongQing.Li 			in6_dev_put(idev);
1126b43faac6SDavid S. Miller 			dst_free(&rt->dst);
1127b43faac6SDavid S. Miller 			return ERR_CAST(neigh);
1128b43faac6SDavid S. Miller 		}
112914deae41SDavid S. Miller 	}
11301da177e4SLinus Torvalds 
11318e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
11328e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
113369cce1d1SDavid S. Miller 	dst_set_neighbour(&rt->dst, neigh);
1134d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
113587a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
11368e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
11378e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
11387011687fSGao feng 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
11391da177e4SLinus Torvalds 
11403b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1141d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1142d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
11433b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
11441da177e4SLinus Torvalds 
11455578689aSDaniel Lezcano 	fib6_force_start_gc(net);
11461da177e4SLinus Torvalds 
114787a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
114887a11578SDavid S. Miller 
11491da177e4SLinus Torvalds out:
115087a11578SDavid S. Miller 	return dst;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
11533d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
11541da177e4SLinus Torvalds {
1155e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
11563d0f24a7SStephen Hemminger 	int more = 0;
11571da177e4SLinus Torvalds 
11583b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
11593b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
11605d0bbeebSThomas Graf 
11611da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
11621da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
11631da177e4SLinus Torvalds 			*pprev = dst->next;
11641da177e4SLinus Torvalds 			dst_free(dst);
11651da177e4SLinus Torvalds 		} else {
11661da177e4SLinus Torvalds 			pprev = &dst->next;
11673d0f24a7SStephen Hemminger 			++more;
11681da177e4SLinus Torvalds 		}
11691da177e4SLinus Torvalds 	}
11701da177e4SLinus Torvalds 
11713b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
11725d0bbeebSThomas Graf 
11733d0f24a7SStephen Hemminger 	return more;
11741da177e4SLinus Torvalds }
11751da177e4SLinus Torvalds 
11761e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
11771e493d19SDavid S. Miller 			    void *arg)
11781e493d19SDavid S. Miller {
11791e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
11801e493d19SDavid S. Miller 
11811e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
11821e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
11831e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
11841e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
11851e493d19SDavid S. Miller 		if (func(rt, arg)) {
11861e493d19SDavid S. Miller 			*pprev = dst->next;
11871e493d19SDavid S. Miller 			dst_free(dst);
11881e493d19SDavid S. Miller 		} else {
11891e493d19SDavid S. Miller 			pprev = &dst->next;
11901e493d19SDavid S. Miller 		}
11911e493d19SDavid S. Miller 	}
11921e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
11931e493d19SDavid S. Miller }
11941e493d19SDavid S. Miller 
1195569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
11961da177e4SLinus Torvalds {
11971da177e4SLinus Torvalds 	unsigned long now = jiffies;
119886393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
11997019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
12007019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
12017019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
12027019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
12037019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1204fc66f95cSEric Dumazet 	int entries;
12051da177e4SLinus Torvalds 
1206fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
12077019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1208fc66f95cSEric Dumazet 	    entries <= rt_max_size)
12091da177e4SLinus Torvalds 		goto out;
12101da177e4SLinus Torvalds 
12116891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
12126891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
12136891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1214fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1215fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
12167019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
12171da177e4SLinus Torvalds out:
12187019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1219fc66f95cSEric Dumazet 	return entries > rt_max_size;
12201da177e4SLinus Torvalds }
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
12231da177e4SLinus Torvalds    but results in cleaner routing tables.
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds    Remove it only when all the things will work!
12261da177e4SLinus Torvalds  */
12271da177e4SLinus Torvalds 
12286b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
12291da177e4SLinus Torvalds {
12305170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1231a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
12326b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1233c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1234c68f24ccSEric Dumazet 
1235c68f24ccSEric Dumazet 		rcu_read_lock();
1236c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1237c68f24ccSEric Dumazet 		if (idev)
12381da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1239c68f24ccSEric Dumazet 		else
124053b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1241c68f24ccSEric Dumazet 		rcu_read_unlock();
12421da177e4SLinus Torvalds 	}
12431da177e4SLinus Torvalds 	return hoplimit;
12441da177e4SLinus Torvalds }
1245abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds /*
12481da177e4SLinus Torvalds  *
12491da177e4SLinus Torvalds  */
12501da177e4SLinus Torvalds 
125186872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
12521da177e4SLinus Torvalds {
12531da177e4SLinus Torvalds 	int err;
12545578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
12551da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
12561da177e4SLinus Torvalds 	struct net_device *dev = NULL;
12571da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1258c71099acSThomas Graf 	struct fib6_table *table;
12591da177e4SLinus Torvalds 	int addr_type;
12601da177e4SLinus Torvalds 
126186872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
12621da177e4SLinus Torvalds 		return -EINVAL;
12631da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
126486872cb5SThomas Graf 	if (cfg->fc_src_len)
12651da177e4SLinus Torvalds 		return -EINVAL;
12661da177e4SLinus Torvalds #endif
126786872cb5SThomas Graf 	if (cfg->fc_ifindex) {
12681da177e4SLinus Torvalds 		err = -ENODEV;
12695578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
12701da177e4SLinus Torvalds 		if (!dev)
12711da177e4SLinus Torvalds 			goto out;
12721da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
12731da177e4SLinus Torvalds 		if (!idev)
12741da177e4SLinus Torvalds 			goto out;
12751da177e4SLinus Torvalds 	}
12761da177e4SLinus Torvalds 
127786872cb5SThomas Graf 	if (cfg->fc_metric == 0)
127886872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
12791da177e4SLinus Torvalds 
1280c71099acSThomas Graf 	err = -ENOBUFS;
128138308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1282d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1283d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
128438308473SDavid S. Miller 		if (!table) {
1285f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1286d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1287d71314b4SMatti Vaittinen 		}
1288d71314b4SMatti Vaittinen 	} else {
1289d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1290d71314b4SMatti Vaittinen 	}
129138308473SDavid S. Miller 
129238308473SDavid S. Miller 	if (!table)
1293c71099acSThomas Graf 		goto out;
1294c71099acSThomas Graf 
1295957c665fSDavid S. Miller 	rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT);
12961da177e4SLinus Torvalds 
129738308473SDavid S. Miller 	if (!rt) {
12981da177e4SLinus Torvalds 		err = -ENOMEM;
12991da177e4SLinus Torvalds 		goto out;
13001da177e4SLinus Torvalds 	}
13011da177e4SLinus Torvalds 
1302d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
13031716a961SGao feng 
13041716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
13051716a961SGao feng 		rt6_set_expires(rt, jiffies +
13061716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
13071716a961SGao feng 	else
13081716a961SGao feng 		rt6_clean_expires(rt);
13091da177e4SLinus Torvalds 
131086872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
131186872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
131286872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
131386872cb5SThomas Graf 
131486872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1317d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1318ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1319ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
13201da177e4SLinus Torvalds 	else
1321d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
13221da177e4SLinus Torvalds 
1323d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
13241da177e4SLinus Torvalds 
132586872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
132686872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
13271da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
132811d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
13291da177e4SLinus Torvalds 
13308e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
13318e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
13328e2ec639SYan, Zheng 		if (!metrics) {
13338e2ec639SYan, Zheng 			err = -ENOMEM;
13348e2ec639SYan, Zheng 			goto out;
13358e2ec639SYan, Zheng 		}
13368e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
13378e2ec639SYan, Zheng 	}
13381da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
133986872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
134086872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
13411da177e4SLinus Torvalds #endif
13421da177e4SLinus Torvalds 
134386872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
13461da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
13471da177e4SLinus Torvalds 	 */
134886872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
134938308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
135038308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
135138308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
13521da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
13535578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
13541da177e4SLinus Torvalds 			if (dev) {
13551da177e4SLinus Torvalds 				dev_put(dev);
13561da177e4SLinus Torvalds 				in6_dev_put(idev);
13571da177e4SLinus Torvalds 			}
13585578689aSDaniel Lezcano 			dev = net->loopback_dev;
13591da177e4SLinus Torvalds 			dev_hold(dev);
13601da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
13611da177e4SLinus Torvalds 			if (!idev) {
13621da177e4SLinus Torvalds 				err = -ENODEV;
13631da177e4SLinus Torvalds 				goto out;
13641da177e4SLinus Torvalds 			}
13651da177e4SLinus Torvalds 		}
1366d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1367d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
1368d8d1f30bSChangli Gao 		rt->dst.error = -ENETUNREACH;
13691da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
13701da177e4SLinus Torvalds 		goto install_route;
13711da177e4SLinus Torvalds 	}
13721da177e4SLinus Torvalds 
137386872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1374b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
13751da177e4SLinus Torvalds 		int gwa_type;
13761da177e4SLinus Torvalds 
137786872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
13784e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
13791da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
13821da177e4SLinus Torvalds 			struct rt6_info *grt;
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
13851da177e4SLinus Torvalds 			   addresses as nexthop address.
13861da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
13871da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
13881da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
13891da177e4SLinus Torvalds 			   some exceptions. --ANK
13901da177e4SLinus Torvalds 			 */
13911da177e4SLinus Torvalds 			err = -EINVAL;
13921da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
13931da177e4SLinus Torvalds 				goto out;
13941da177e4SLinus Torvalds 
13955578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
139838308473SDavid S. Miller 			if (!grt)
13991da177e4SLinus Torvalds 				goto out;
14001da177e4SLinus Torvalds 			if (dev) {
1401d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
1402d8d1f30bSChangli Gao 					dst_release(&grt->dst);
14031da177e4SLinus Torvalds 					goto out;
14041da177e4SLinus Torvalds 				}
14051da177e4SLinus Torvalds 			} else {
1406d1918542SDavid S. Miller 				dev = grt->dst.dev;
14071da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
14081da177e4SLinus Torvalds 				dev_hold(dev);
14091da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
14101da177e4SLinus Torvalds 			}
14111da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
14121da177e4SLinus Torvalds 				err = 0;
1413d8d1f30bSChangli Gao 			dst_release(&grt->dst);
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 			if (err)
14161da177e4SLinus Torvalds 				goto out;
14171da177e4SLinus Torvalds 		}
14181da177e4SLinus Torvalds 		err = -EINVAL;
141938308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
14201da177e4SLinus Torvalds 			goto out;
14211da177e4SLinus Torvalds 	}
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 	err = -ENODEV;
142438308473SDavid S. Miller 	if (!dev)
14251da177e4SLinus Torvalds 		goto out;
14261da177e4SLinus Torvalds 
1427c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1428c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1429c3968a85SDaniel Walter 			err = -EINVAL;
1430c3968a85SDaniel Walter 			goto out;
1431c3968a85SDaniel Walter 		}
14324e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1433c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1434c3968a85SDaniel Walter 	} else
1435c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1436c3968a85SDaniel Walter 
143786872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
14388ade06c6SDavid S. Miller 		err = rt6_bind_neighbour(rt, dev);
1439f83c7790SDavid S. Miller 		if (err)
14401da177e4SLinus Torvalds 			goto out;
14411da177e4SLinus Torvalds 	}
14421da177e4SLinus Torvalds 
144386872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds install_route:
144686872cb5SThomas Graf 	if (cfg->fc_mx) {
144786872cb5SThomas Graf 		struct nlattr *nla;
144886872cb5SThomas Graf 		int remaining;
14491da177e4SLinus Torvalds 
145086872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
14518f4c1f9bSThomas Graf 			int type = nla_type(nla);
145286872cb5SThomas Graf 
145386872cb5SThomas Graf 			if (type) {
145486872cb5SThomas Graf 				if (type > RTAX_MAX) {
14551da177e4SLinus Torvalds 					err = -EINVAL;
14561da177e4SLinus Torvalds 					goto out;
14571da177e4SLinus Torvalds 				}
145886872cb5SThomas Graf 
1459defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
14601da177e4SLinus Torvalds 			}
14611da177e4SLinus Torvalds 		}
14621da177e4SLinus Torvalds 	}
14631da177e4SLinus Torvalds 
1464d8d1f30bSChangli Gao 	rt->dst.dev = dev;
14651da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1466c71099acSThomas Graf 	rt->rt6i_table = table;
146763152fc0SDaniel Lezcano 
1468c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
146963152fc0SDaniel Lezcano 
147086872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds out:
14731da177e4SLinus Torvalds 	if (dev)
14741da177e4SLinus Torvalds 		dev_put(dev);
14751da177e4SLinus Torvalds 	if (idev)
14761da177e4SLinus Torvalds 		in6_dev_put(idev);
14771da177e4SLinus Torvalds 	if (rt)
1478d8d1f30bSChangli Gao 		dst_free(&rt->dst);
14791da177e4SLinus Torvalds 	return err;
14801da177e4SLinus Torvalds }
14811da177e4SLinus Torvalds 
148286872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
14831da177e4SLinus Torvalds {
14841da177e4SLinus Torvalds 	int err;
1485c71099acSThomas Graf 	struct fib6_table *table;
1486d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
14871da177e4SLinus Torvalds 
14888ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry)
14896c813a72SPatrick McHardy 		return -ENOENT;
14906c813a72SPatrick McHardy 
1491c71099acSThomas Graf 	table = rt->rt6i_table;
1492c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
14931da177e4SLinus Torvalds 
149486872cb5SThomas Graf 	err = fib6_del(rt, info);
1495d8d1f30bSChangli Gao 	dst_release(&rt->dst);
14961da177e4SLinus Torvalds 
1497c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 	return err;
15001da177e4SLinus Torvalds }
15011da177e4SLinus Torvalds 
1502e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1503e0a1ad73SThomas Graf {
15044d1169c1SDenis V. Lunev 	struct nl_info info = {
1505d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
15064d1169c1SDenis V. Lunev 	};
1507528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1508e0a1ad73SThomas Graf }
1509e0a1ad73SThomas Graf 
151086872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
15111da177e4SLinus Torvalds {
1512c71099acSThomas Graf 	struct fib6_table *table;
15131da177e4SLinus Torvalds 	struct fib6_node *fn;
15141da177e4SLinus Torvalds 	struct rt6_info *rt;
15151da177e4SLinus Torvalds 	int err = -ESRCH;
15161da177e4SLinus Torvalds 
15175578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
151838308473SDavid S. Miller 	if (!table)
1519c71099acSThomas Graf 		return err;
15201da177e4SLinus Torvalds 
1521c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1522c71099acSThomas Graf 
1523c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
152486872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
152586872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	if (fn) {
1528d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
152986872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1530d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1531d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
15321da177e4SLinus Torvalds 				continue;
153386872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
153486872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
15351da177e4SLinus Torvalds 				continue;
153686872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
15371da177e4SLinus Torvalds 				continue;
1538d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1539c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
15401da177e4SLinus Torvalds 
154186872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
15421da177e4SLinus Torvalds 		}
15431da177e4SLinus Torvalds 	}
1544c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	return err;
15471da177e4SLinus Torvalds }
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds /*
15501da177e4SLinus Torvalds  *	Handle redirects
15511da177e4SLinus Torvalds  */
1552a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi {
15534c9483b2SDavid S. Miller 	struct flowi6 fl6;
1554a6279458SYOSHIFUJI Hideaki 	struct in6_addr gateway;
1555a6279458SYOSHIFUJI Hideaki };
15561da177e4SLinus Torvalds 
15578ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net,
15588ed67789SDaniel Lezcano 					     struct fib6_table *table,
15594c9483b2SDavid S. Miller 					     struct flowi6 *fl6,
1560a6279458SYOSHIFUJI Hideaki 					     int flags)
1561a6279458SYOSHIFUJI Hideaki {
15624c9483b2SDavid S. Miller 	struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6;
1563a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt;
1564a6279458SYOSHIFUJI Hideaki 	struct fib6_node *fn;
1565c71099acSThomas Graf 
1566e843b9e1SYOSHIFUJI Hideaki 	/*
1567e843b9e1SYOSHIFUJI Hideaki 	 * Get the "current" route for this destination and
1568e843b9e1SYOSHIFUJI Hideaki 	 * check if the redirect has come from approriate router.
1569e843b9e1SYOSHIFUJI Hideaki 	 *
1570e843b9e1SYOSHIFUJI Hideaki 	 * RFC 2461 specifies that redirects should only be
1571e843b9e1SYOSHIFUJI Hideaki 	 * accepted if they come from the nexthop to the target.
1572e843b9e1SYOSHIFUJI Hideaki 	 * Due to the way the routes are chosen, this notion
1573e843b9e1SYOSHIFUJI Hideaki 	 * is a bit fuzzy and one might need to check all possible
1574e843b9e1SYOSHIFUJI Hideaki 	 * routes.
1575e843b9e1SYOSHIFUJI Hideaki 	 */
15761da177e4SLinus Torvalds 
1577c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
15784c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
1579e843b9e1SYOSHIFUJI Hideaki restart:
1580d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
15811da177e4SLinus Torvalds 		/*
15821da177e4SLinus Torvalds 		 * Current route is on-link; redirect is always invalid.
15831da177e4SLinus Torvalds 		 *
15841da177e4SLinus Torvalds 		 * Seems, previous statement is not true. It could
15851da177e4SLinus Torvalds 		 * be node, which looks for us as on-link (f.e. proxy ndisc)
15861da177e4SLinus Torvalds 		 * But then router serving it might decide, that we should
15871da177e4SLinus Torvalds 		 * know truth 8)8) --ANK (980726).
15881da177e4SLinus Torvalds 		 */
1589e843b9e1SYOSHIFUJI Hideaki 		if (rt6_check_expired(rt))
1590e843b9e1SYOSHIFUJI Hideaki 			continue;
15911da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_GATEWAY))
1592e843b9e1SYOSHIFUJI Hideaki 			continue;
1593d1918542SDavid S. Miller 		if (fl6->flowi6_oif != rt->dst.dev->ifindex)
1594e843b9e1SYOSHIFUJI Hideaki 			continue;
1595a6279458SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1596e843b9e1SYOSHIFUJI Hideaki 			continue;
1597e843b9e1SYOSHIFUJI Hideaki 		break;
1598e843b9e1SYOSHIFUJI Hideaki 	}
1599a6279458SYOSHIFUJI Hideaki 
1600cb15d9c2SYOSHIFUJI Hideaki 	if (!rt)
16018ed67789SDaniel Lezcano 		rt = net->ipv6.ip6_null_entry;
16024c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
1603cb15d9c2SYOSHIFUJI Hideaki out:
1604d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
1605a6279458SYOSHIFUJI Hideaki 
1606c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
16071da177e4SLinus Torvalds 
1608a6279458SYOSHIFUJI Hideaki 	return rt;
1609a6279458SYOSHIFUJI Hideaki };
1610a6279458SYOSHIFUJI Hideaki 
1611b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest,
1612b71d1d42SEric Dumazet 					   const struct in6_addr *src,
1613b71d1d42SEric Dumazet 					   const struct in6_addr *gateway,
1614a6279458SYOSHIFUJI Hideaki 					   struct net_device *dev)
1615a6279458SYOSHIFUJI Hideaki {
1616adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
1617c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
1618a6279458SYOSHIFUJI Hideaki 	struct ip6rd_flowi rdfl = {
16194c9483b2SDavid S. Miller 		.fl6 = {
16204c9483b2SDavid S. Miller 			.flowi6_oif = dev->ifindex,
16214c9483b2SDavid S. Miller 			.daddr = *dest,
16224c9483b2SDavid S. Miller 			.saddr = *src,
1623a6279458SYOSHIFUJI Hideaki 		},
1624a6279458SYOSHIFUJI Hideaki 	};
1625adaa70bbSThomas Graf 
16264e3fd7a0SAlexey Dobriyan 	rdfl.gateway = *gateway;
162786c36ce4SBrian Haley 
1628adaa70bbSThomas Graf 	if (rt6_need_strict(dest))
1629adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_IFACE;
1630a6279458SYOSHIFUJI Hideaki 
16314c9483b2SDavid S. Miller 	return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6,
163258f09b78SDaniel Lezcano 						   flags, __ip6_route_redirect);
1633a6279458SYOSHIFUJI Hideaki }
1634a6279458SYOSHIFUJI Hideaki 
1635b71d1d42SEric Dumazet void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src,
1636b71d1d42SEric Dumazet 		  const struct in6_addr *saddr,
1637a6279458SYOSHIFUJI Hideaki 		  struct neighbour *neigh, u8 *lladdr, int on_link)
1638a6279458SYOSHIFUJI Hideaki {
1639a6279458SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt = NULL;
1640a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1641c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(neigh->dev);
1642a6279458SYOSHIFUJI Hideaki 
1643a6279458SYOSHIFUJI Hideaki 	rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1644a6279458SYOSHIFUJI Hideaki 
16458ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry) {
1646e87cc472SJoe Perches 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
1647a6279458SYOSHIFUJI Hideaki 		goto out;
16481da177e4SLinus Torvalds 	}
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	/*
16511da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
16521da177e4SLinus Torvalds 	 */
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
16551da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
16561da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
16571da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
16581da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
16591da177e4SLinus Torvalds 		     );
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 	/*
16621da177e4SLinus Torvalds 	 * Redirect received -> path was valid.
16631da177e4SLinus Torvalds 	 * Look, redirects are sent only in response to data packets,
16641da177e4SLinus Torvalds 	 * so that this nexthop apparently is reachable. --ANK
16651da177e4SLinus Torvalds 	 */
1666d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 	/* Duplicate redirect: silently ignore. */
166927217455SDavid Miller 	if (neigh == dst_get_neighbour_noref_raw(&rt->dst))
16701da177e4SLinus Torvalds 		goto out;
16711da177e4SLinus Torvalds 
167221efcfa0SEric Dumazet 	nrt = ip6_rt_copy(rt, dest);
167338308473SDavid S. Miller 	if (!nrt)
16741da177e4SLinus Torvalds 		goto out;
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
16771da177e4SLinus Torvalds 	if (on_link)
16781da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
16791da177e4SLinus Torvalds 
16804e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
168169cce1d1SDavid S. Miller 	dst_set_neighbour(&nrt->dst, neigh_clone(neigh));
16821da177e4SLinus Torvalds 
168340e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
16841da177e4SLinus Torvalds 		goto out;
16851da177e4SLinus Torvalds 
1686d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
1687d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
16888d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
16898d71740cSTom Tucker 
16901da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
1691e0a1ad73SThomas Graf 		ip6_del_rt(rt);
16921da177e4SLinus Torvalds 		return;
16931da177e4SLinus Torvalds 	}
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds out:
1696d8d1f30bSChangli Gao 	dst_release(&rt->dst);
16971da177e4SLinus Torvalds }
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds /*
17001da177e4SLinus Torvalds  *	Handle ICMP "packet too big" messages
17011da177e4SLinus Torvalds  *	i.e. Path MTU discovery
17021da177e4SLinus Torvalds  */
17031da177e4SLinus Torvalds 
1704b71d1d42SEric Dumazet static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr,
1705ae878ae2SMaciej Żenczykowski 			     struct net *net, u32 pmtu, int ifindex)
17061da177e4SLinus Torvalds {
17071da177e4SLinus Torvalds 	struct rt6_info *rt, *nrt;
17081da177e4SLinus Torvalds 	int allfrag = 0;
1709d3052b55SAndrey Vagin again:
1710ae878ae2SMaciej Żenczykowski 	rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
171138308473SDavid S. Miller 	if (!rt)
17121da177e4SLinus Torvalds 		return;
17131da177e4SLinus Torvalds 
1714d3052b55SAndrey Vagin 	if (rt6_check_expired(rt)) {
1715d3052b55SAndrey Vagin 		ip6_del_rt(rt);
1716d3052b55SAndrey Vagin 		goto again;
1717d3052b55SAndrey Vagin 	}
1718d3052b55SAndrey Vagin 
1719d8d1f30bSChangli Gao 	if (pmtu >= dst_mtu(&rt->dst))
17201da177e4SLinus Torvalds 		goto out;
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	if (pmtu < IPV6_MIN_MTU) {
17231da177e4SLinus Torvalds 		/*
17241da177e4SLinus Torvalds 		 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
17251da177e4SLinus Torvalds 		 * MTU (1280) and a fragment header should always be included
17261da177e4SLinus Torvalds 		 * after a node receiving Too Big message reporting PMTU is
17271da177e4SLinus Torvalds 		 * less than the IPv6 Minimum Link MTU.
17281da177e4SLinus Torvalds 		 */
17291da177e4SLinus Torvalds 		pmtu = IPV6_MIN_MTU;
17301da177e4SLinus Torvalds 		allfrag = 1;
17311da177e4SLinus Torvalds 	}
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	/* New mtu received -> path was valid.
17341da177e4SLinus Torvalds 	   They are sent only in response to data packets,
17351da177e4SLinus Torvalds 	   so that this nexthop apparently is reachable. --ANK
17361da177e4SLinus Torvalds 	 */
1737d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
17381da177e4SLinus Torvalds 
17391da177e4SLinus Torvalds 	/* Host route. If it is static, it would be better
17401da177e4SLinus Torvalds 	   not to override it, but add new one, so that
17411da177e4SLinus Torvalds 	   when cache entry will expire old pmtu
17421da177e4SLinus Torvalds 	   would return automatically.
17431da177e4SLinus Torvalds 	 */
17441da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
1745defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1746defb3519SDavid S. Miller 		if (allfrag) {
1747defb3519SDavid S. Miller 			u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1748defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1749defb3519SDavid S. Miller 			dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1750defb3519SDavid S. Miller 		}
17511716a961SGao feng 		rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires);
17521716a961SGao feng 		rt->rt6i_flags |= RTF_MODIFIED;
17531da177e4SLinus Torvalds 		goto out;
17541da177e4SLinus Torvalds 	}
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	/* Network route.
17571da177e4SLinus Torvalds 	   Two cases are possible:
17581da177e4SLinus Torvalds 	   1. It is connected route. Action: COW
17591da177e4SLinus Torvalds 	   2. It is gatewayed route or NONEXTHOP route. Action: clone it.
17601da177e4SLinus Torvalds 	 */
176127217455SDavid Miller 	if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP))
1762a1e78363SYOSHIFUJI Hideaki 		nrt = rt6_alloc_cow(rt, daddr, saddr);
1763d5315b50SYOSHIFUJI Hideaki 	else
1764d5315b50SYOSHIFUJI Hideaki 		nrt = rt6_alloc_clone(rt, daddr);
1765a1e78363SYOSHIFUJI Hideaki 
1766d5315b50SYOSHIFUJI Hideaki 	if (nrt) {
1767defb3519SDavid S. Miller 		dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1768defb3519SDavid S. Miller 		if (allfrag) {
1769defb3519SDavid S. Miller 			u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1770defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1771defb3519SDavid S. Miller 			dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1772defb3519SDavid S. Miller 		}
1773a1e78363SYOSHIFUJI Hideaki 
17741da177e4SLinus Torvalds 		/* According to RFC 1981, detecting PMTU increase shouldn't be
1775a1e78363SYOSHIFUJI Hideaki 		 * happened within 5 mins, the recommended timer is 10 mins.
1776a1e78363SYOSHIFUJI Hideaki 		 * Here this route expiration time is set to ip6_rt_mtu_expires
1777a1e78363SYOSHIFUJI Hideaki 		 * which is 10 mins. After 10 mins the decreased pmtu is expired
1778a1e78363SYOSHIFUJI Hideaki 		 * and detecting PMTU increase will be automatically happened.
17791da177e4SLinus Torvalds 		 */
17801716a961SGao feng 		rt6_update_expires(nrt, net->ipv6.sysctl.ip6_rt_mtu_expires);
17811716a961SGao feng 		nrt->rt6i_flags |= RTF_DYNAMIC;
178240e22e8fSThomas Graf 		ip6_ins_rt(nrt);
17831da177e4SLinus Torvalds 	}
17841da177e4SLinus Torvalds out:
1785d8d1f30bSChangli Gao 	dst_release(&rt->dst);
17861da177e4SLinus Torvalds }
17871da177e4SLinus Torvalds 
1788b71d1d42SEric Dumazet void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr,
1789ae878ae2SMaciej Żenczykowski 			struct net_device *dev, u32 pmtu)
1790ae878ae2SMaciej Żenczykowski {
1791ae878ae2SMaciej Żenczykowski 	struct net *net = dev_net(dev);
1792ae878ae2SMaciej Żenczykowski 
1793ae878ae2SMaciej Żenczykowski 	/*
1794ae878ae2SMaciej Żenczykowski 	 * RFC 1981 states that a node "MUST reduce the size of the packets it
1795ae878ae2SMaciej Żenczykowski 	 * is sending along the path" that caused the Packet Too Big message.
1796ae878ae2SMaciej Żenczykowski 	 * Since it's not possible in the general case to determine which
1797ae878ae2SMaciej Żenczykowski 	 * interface was used to send the original packet, we update the MTU
1798ae878ae2SMaciej Żenczykowski 	 * on the interface that will be used to send future packets. We also
1799ae878ae2SMaciej Żenczykowski 	 * update the MTU on the interface that received the Packet Too Big in
1800ae878ae2SMaciej Żenczykowski 	 * case the original packet was forced out that interface with
1801ae878ae2SMaciej Żenczykowski 	 * SO_BINDTODEVICE or similar. This is the next best thing to the
1802ae878ae2SMaciej Żenczykowski 	 * correct behaviour, which would be to update the MTU on all
1803ae878ae2SMaciej Żenczykowski 	 * interfaces.
1804ae878ae2SMaciej Żenczykowski 	 */
1805ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1806ae878ae2SMaciej Żenczykowski 	rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1807ae878ae2SMaciej Żenczykowski }
1808ae878ae2SMaciej Żenczykowski 
18091da177e4SLinus Torvalds /*
18101da177e4SLinus Torvalds  *	Misc support functions
18111da177e4SLinus Torvalds  */
18121da177e4SLinus Torvalds 
18131716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
181421efcfa0SEric Dumazet 				    const struct in6_addr *dest)
18151da177e4SLinus Torvalds {
1816d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
18175c1e6aa3SDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
1818957c665fSDavid S. Miller 					    ort->dst.dev, 0);
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds 	if (rt) {
1821d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1822d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
18238e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
18241da177e4SLinus Torvalds 
18254e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
18268e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1827defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1828d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
18291da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
18301da177e4SLinus Torvalds 		if (rt->rt6i_idev)
18311da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1832d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
18331da177e4SLinus Torvalds 
18344e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
18351716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
18361716a961SGao feng 		if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
18371716a961SGao feng 		    (RTF_DEFAULT | RTF_ADDRCONF))
18381716a961SGao feng 			rt6_set_from(rt, ort);
18391716a961SGao feng 		else
18401716a961SGao feng 			rt6_clean_expires(rt);
18411da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
18441da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
18451da177e4SLinus Torvalds #endif
18460f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1847c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
18481da177e4SLinus Torvalds 	}
18491da177e4SLinus Torvalds 	return rt;
18501da177e4SLinus Torvalds }
18511da177e4SLinus Torvalds 
185270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1853efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1854b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1855b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
185670ceb4f5SYOSHIFUJI Hideaki {
185770ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
185870ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1859c71099acSThomas Graf 	struct fib6_table *table;
186070ceb4f5SYOSHIFUJI Hideaki 
1861efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
186238308473SDavid S. Miller 	if (!table)
1863c71099acSThomas Graf 		return NULL;
1864c71099acSThomas Graf 
1865c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1866c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
186770ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
186870ceb4f5SYOSHIFUJI Hideaki 		goto out;
186970ceb4f5SYOSHIFUJI Hideaki 
1870d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1871d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
187270ceb4f5SYOSHIFUJI Hideaki 			continue;
187370ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
187470ceb4f5SYOSHIFUJI Hideaki 			continue;
187570ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
187670ceb4f5SYOSHIFUJI Hideaki 			continue;
1877d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
187870ceb4f5SYOSHIFUJI Hideaki 		break;
187970ceb4f5SYOSHIFUJI Hideaki 	}
188070ceb4f5SYOSHIFUJI Hideaki out:
1881c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
188270ceb4f5SYOSHIFUJI Hideaki 	return rt;
188370ceb4f5SYOSHIFUJI Hideaki }
188470ceb4f5SYOSHIFUJI Hideaki 
1885efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1886b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1887b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
188895c96174SEric Dumazet 					   unsigned int pref)
188970ceb4f5SYOSHIFUJI Hideaki {
189086872cb5SThomas Graf 	struct fib6_config cfg = {
189186872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1892238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
189386872cb5SThomas Graf 		.fc_ifindex	= ifindex,
189486872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
189586872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
189686872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
1897efa2cea0SDaniel Lezcano 		.fc_nlinfo.pid = 0,
1898efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1899efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
190086872cb5SThomas Graf 	};
190170ceb4f5SYOSHIFUJI Hideaki 
19024e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
19034e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
190486872cb5SThomas Graf 
1905e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1906e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
190786872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
190870ceb4f5SYOSHIFUJI Hideaki 
190986872cb5SThomas Graf 	ip6_route_add(&cfg);
191070ceb4f5SYOSHIFUJI Hideaki 
1911efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
191270ceb4f5SYOSHIFUJI Hideaki }
191370ceb4f5SYOSHIFUJI Hideaki #endif
191470ceb4f5SYOSHIFUJI Hideaki 
1915b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
19161da177e4SLinus Torvalds {
19171da177e4SLinus Torvalds 	struct rt6_info *rt;
1918c71099acSThomas Graf 	struct fib6_table *table;
19191da177e4SLinus Torvalds 
1920c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
192138308473SDavid S. Miller 	if (!table)
1922c71099acSThomas Graf 		return NULL;
19231da177e4SLinus Torvalds 
1924c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1925d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1926d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
1927045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
19281da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
19291da177e4SLinus Torvalds 			break;
19301da177e4SLinus Torvalds 	}
19311da177e4SLinus Torvalds 	if (rt)
1932d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
1933c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
19341da177e4SLinus Torvalds 	return rt;
19351da177e4SLinus Torvalds }
19361da177e4SLinus Torvalds 
1937b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1938ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1939ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
19401da177e4SLinus Torvalds {
194186872cb5SThomas Graf 	struct fib6_config cfg = {
194286872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1943238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
194486872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
194586872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
194686872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
19475578689aSDaniel Lezcano 		.fc_nlinfo.pid = 0,
19485578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1949c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
195086872cb5SThomas Graf 	};
19511da177e4SLinus Torvalds 
19524e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
19531da177e4SLinus Torvalds 
195486872cb5SThomas Graf 	ip6_route_add(&cfg);
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
19571da177e4SLinus Torvalds }
19581da177e4SLinus Torvalds 
19597b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
19601da177e4SLinus Torvalds {
19611da177e4SLinus Torvalds 	struct rt6_info *rt;
1962c71099acSThomas Graf 	struct fib6_table *table;
1963c71099acSThomas Graf 
1964c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
19657b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
196638308473SDavid S. Miller 	if (!table)
1967c71099acSThomas Graf 		return;
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds restart:
1970c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1971d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
19721da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1973d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1974c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1975e0a1ad73SThomas Graf 			ip6_del_rt(rt);
19761da177e4SLinus Torvalds 			goto restart;
19771da177e4SLinus Torvalds 		}
19781da177e4SLinus Torvalds 	}
1979c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
19801da177e4SLinus Torvalds }
19811da177e4SLinus Torvalds 
19825578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
19835578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
198486872cb5SThomas Graf 				 struct fib6_config *cfg)
198586872cb5SThomas Graf {
198686872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
198786872cb5SThomas Graf 
198886872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
198986872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
199086872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
199186872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
199286872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
199386872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
199486872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
199586872cb5SThomas Graf 
19965578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1997f1243c2dSBenjamin Thery 
19984e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
19994e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
20004e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
200186872cb5SThomas Graf }
200286872cb5SThomas Graf 
20035578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
20041da177e4SLinus Torvalds {
200586872cb5SThomas Graf 	struct fib6_config cfg;
20061da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
20071da177e4SLinus Torvalds 	int err;
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds 	switch(cmd) {
20101da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
20111da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
20121da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
20131da177e4SLinus Torvalds 			return -EPERM;
20141da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
20151da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
20161da177e4SLinus Torvalds 		if (err)
20171da177e4SLinus Torvalds 			return -EFAULT;
20181da177e4SLinus Torvalds 
20195578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
202086872cb5SThomas Graf 
20211da177e4SLinus Torvalds 		rtnl_lock();
20221da177e4SLinus Torvalds 		switch (cmd) {
20231da177e4SLinus Torvalds 		case SIOCADDRT:
202486872cb5SThomas Graf 			err = ip6_route_add(&cfg);
20251da177e4SLinus Torvalds 			break;
20261da177e4SLinus Torvalds 		case SIOCDELRT:
202786872cb5SThomas Graf 			err = ip6_route_del(&cfg);
20281da177e4SLinus Torvalds 			break;
20291da177e4SLinus Torvalds 		default:
20301da177e4SLinus Torvalds 			err = -EINVAL;
20311da177e4SLinus Torvalds 		}
20321da177e4SLinus Torvalds 		rtnl_unlock();
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds 		return err;
20353ff50b79SStephen Hemminger 	}
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds 	return -EINVAL;
20381da177e4SLinus Torvalds }
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds /*
20411da177e4SLinus Torvalds  *	Drop the packet on the floor
20421da177e4SLinus Torvalds  */
20431da177e4SLinus Torvalds 
2044d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
20451da177e4SLinus Torvalds {
2046612f09e8SYOSHIFUJI Hideaki 	int type;
2047adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
2048612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2049612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
20500660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
205145bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
20523bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20533bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2054612f09e8SYOSHIFUJI Hideaki 			break;
2055612f09e8SYOSHIFUJI Hideaki 		}
2056612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2057612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
20583bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20593bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2060612f09e8SYOSHIFUJI Hideaki 		break;
2061612f09e8SYOSHIFUJI Hideaki 	}
20623ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
20631da177e4SLinus Torvalds 	kfree_skb(skb);
20641da177e4SLinus Torvalds 	return 0;
20651da177e4SLinus Torvalds }
20661da177e4SLinus Torvalds 
20679ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
20689ce8ade0SThomas Graf {
2069612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
20709ce8ade0SThomas Graf }
20719ce8ade0SThomas Graf 
207220380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
20731da177e4SLinus Torvalds {
2074adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2075612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
20761da177e4SLinus Torvalds }
20771da177e4SLinus Torvalds 
20786723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
20796723ab54SDavid S. Miller 
20809ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
20819ce8ade0SThomas Graf {
2082612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
20839ce8ade0SThomas Graf }
20849ce8ade0SThomas Graf 
20859ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
20869ce8ade0SThomas Graf {
2087adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2088612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
20899ce8ade0SThomas Graf }
20909ce8ade0SThomas Graf 
20916723ab54SDavid S. Miller #endif
20926723ab54SDavid S. Miller 
20931da177e4SLinus Torvalds /*
20941da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
20951da177e4SLinus Torvalds  */
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
20981da177e4SLinus Torvalds 				    const struct in6_addr *addr,
20998f031519SDavid S. Miller 				    bool anycast)
21001da177e4SLinus Torvalds {
2101c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
21025c1e6aa3SDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops,
2103957c665fSDavid S. Miller 					    net->loopback_dev, 0);
2104f83c7790SDavid S. Miller 	int err;
21051da177e4SLinus Torvalds 
210638308473SDavid S. Miller 	if (!rt) {
2107f3213831SJoe Perches 		net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
21081da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
210940385653SBen Greear 	}
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds 	in6_dev_hold(idev);
21121da177e4SLinus Torvalds 
211311d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2114d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2115d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
21161da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2117d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
21181da177e4SLinus Torvalds 
21191da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
212058c4fb86SYOSHIFUJI Hideaki 	if (anycast)
212158c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
212258c4fb86SYOSHIFUJI Hideaki 	else
21231da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
21248ade06c6SDavid S. Miller 	err = rt6_bind_neighbour(rt, rt->dst.dev);
2125f83c7790SDavid S. Miller 	if (err) {
2126d8d1f30bSChangli Gao 		dst_free(&rt->dst);
2127f83c7790SDavid S. Miller 		return ERR_PTR(err);
21281da177e4SLinus Torvalds 	}
21291da177e4SLinus Torvalds 
21304e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
21311da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
21325578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
21331da177e4SLinus Torvalds 
2134d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds 	return rt;
21371da177e4SLinus Torvalds }
21381da177e4SLinus Torvalds 
2139c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2140c3968a85SDaniel Walter 			struct rt6_info *rt,
2141b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2142c3968a85SDaniel Walter 			unsigned int prefs,
2143c3968a85SDaniel Walter 			struct in6_addr *saddr)
2144c3968a85SDaniel Walter {
2145c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2146c3968a85SDaniel Walter 	int err = 0;
2147c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
21484e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2149c3968a85SDaniel Walter 	else
2150c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2151c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2152c3968a85SDaniel Walter 	return err;
2153c3968a85SDaniel Walter }
2154c3968a85SDaniel Walter 
2155c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2156c3968a85SDaniel Walter struct arg_dev_net_ip {
2157c3968a85SDaniel Walter 	struct net_device *dev;
2158c3968a85SDaniel Walter 	struct net *net;
2159c3968a85SDaniel Walter 	struct in6_addr *addr;
2160c3968a85SDaniel Walter };
2161c3968a85SDaniel Walter 
2162c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2163c3968a85SDaniel Walter {
2164c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2165c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2166c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2167c3968a85SDaniel Walter 
2168d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2169c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2170c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2171c3968a85SDaniel Walter 		/* remove prefsrc entry */
2172c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2173c3968a85SDaniel Walter 	}
2174c3968a85SDaniel Walter 	return 0;
2175c3968a85SDaniel Walter }
2176c3968a85SDaniel Walter 
2177c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2178c3968a85SDaniel Walter {
2179c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2180c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2181c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2182c3968a85SDaniel Walter 		.net = net,
2183c3968a85SDaniel Walter 		.addr = &ifp->addr,
2184c3968a85SDaniel Walter 	};
2185c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2186c3968a85SDaniel Walter }
2187c3968a85SDaniel Walter 
21888ed67789SDaniel Lezcano struct arg_dev_net {
21898ed67789SDaniel Lezcano 	struct net_device *dev;
21908ed67789SDaniel Lezcano 	struct net *net;
21918ed67789SDaniel Lezcano };
21928ed67789SDaniel Lezcano 
21931da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
21941da177e4SLinus Torvalds {
2195bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2196bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
21978ed67789SDaniel Lezcano 
2198d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2199c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
22001da177e4SLinus Torvalds 		return -1;
2201c159d30cSDavid S. Miller 
22021da177e4SLinus Torvalds 	return 0;
22031da177e4SLinus Torvalds }
22041da177e4SLinus Torvalds 
2205f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
22061da177e4SLinus Torvalds {
22078ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
22088ed67789SDaniel Lezcano 		.dev = dev,
22098ed67789SDaniel Lezcano 		.net = net,
22108ed67789SDaniel Lezcano 	};
22118ed67789SDaniel Lezcano 
22128ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
22131e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
22141da177e4SLinus Torvalds }
22151da177e4SLinus Torvalds 
221695c96174SEric Dumazet struct rt6_mtu_change_arg {
22171da177e4SLinus Torvalds 	struct net_device *dev;
221895c96174SEric Dumazet 	unsigned int mtu;
22191da177e4SLinus Torvalds };
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
22221da177e4SLinus Torvalds {
22231da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
22241da177e4SLinus Torvalds 	struct inet6_dev *idev;
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
22271da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
22281da177e4SLinus Torvalds 	   We still use this lock to block changes
22291da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
22301da177e4SLinus Torvalds 	*/
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
223338308473SDavid S. Miller 	if (!idev)
22341da177e4SLinus Torvalds 		return 0;
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
22371da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
22381da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
22391da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
22401da177e4SLinus Torvalds 	 */
22411da177e4SLinus Torvalds 	/*
22421da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
22431da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
22441da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
22451da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
22461da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
22471da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
22481da177e4SLinus Torvalds 	   PMTU discouvery.
22491da177e4SLinus Torvalds 	 */
2250d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2251d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2252d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2253d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2254d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2255defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2256566cfd8fSSimon Arlott 	}
22571da177e4SLinus Torvalds 	return 0;
22581da177e4SLinus Torvalds }
22591da177e4SLinus Torvalds 
226095c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
22611da177e4SLinus Torvalds {
2262c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2263c71099acSThomas Graf 		.dev = dev,
2264c71099acSThomas Graf 		.mtu = mtu,
2265c71099acSThomas Graf 	};
22661da177e4SLinus Torvalds 
2267c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
22681da177e4SLinus Torvalds }
22691da177e4SLinus Torvalds 
2270ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
22715176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
227286872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2273ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
227486872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
227586872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
227686872cb5SThomas Graf };
227786872cb5SThomas Graf 
227886872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
227986872cb5SThomas Graf 			      struct fib6_config *cfg)
22801da177e4SLinus Torvalds {
228186872cb5SThomas Graf 	struct rtmsg *rtm;
228286872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
228386872cb5SThomas Graf 	int err;
22841da177e4SLinus Torvalds 
228586872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
228686872cb5SThomas Graf 	if (err < 0)
228786872cb5SThomas Graf 		goto errout;
22881da177e4SLinus Torvalds 
228986872cb5SThomas Graf 	err = -EINVAL;
229086872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
229186872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
229286872cb5SThomas Graf 
229386872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
229486872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
229586872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
229686872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
229786872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
229886872cb5SThomas Graf 
229986872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
230086872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
230186872cb5SThomas Graf 
2302ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2303ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2304ab79ad14SMaciej Żenczykowski 
230586872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
230686872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
23073b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
230886872cb5SThomas Graf 
230986872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
231086872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
231186872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
23121da177e4SLinus Torvalds 	}
231386872cb5SThomas Graf 
231486872cb5SThomas Graf 	if (tb[RTA_DST]) {
231586872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
231686872cb5SThomas Graf 
231786872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
231886872cb5SThomas Graf 			goto errout;
231986872cb5SThomas Graf 
232086872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
23211da177e4SLinus Torvalds 	}
232286872cb5SThomas Graf 
232386872cb5SThomas Graf 	if (tb[RTA_SRC]) {
232486872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
232586872cb5SThomas Graf 
232686872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
232786872cb5SThomas Graf 			goto errout;
232886872cb5SThomas Graf 
232986872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
23301da177e4SLinus Torvalds 	}
233186872cb5SThomas Graf 
2332c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2333c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2334c3968a85SDaniel Walter 
233586872cb5SThomas Graf 	if (tb[RTA_OIF])
233686872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
233786872cb5SThomas Graf 
233886872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
233986872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
234086872cb5SThomas Graf 
234186872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
234286872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
234386872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
23441da177e4SLinus Torvalds 	}
234586872cb5SThomas Graf 
234686872cb5SThomas Graf 	if (tb[RTA_TABLE])
234786872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
234886872cb5SThomas Graf 
234986872cb5SThomas Graf 	err = 0;
235086872cb5SThomas Graf errout:
235186872cb5SThomas Graf 	return err;
23521da177e4SLinus Torvalds }
23531da177e4SLinus Torvalds 
2354c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23551da177e4SLinus Torvalds {
235686872cb5SThomas Graf 	struct fib6_config cfg;
235786872cb5SThomas Graf 	int err;
23581da177e4SLinus Torvalds 
235986872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
236086872cb5SThomas Graf 	if (err < 0)
236186872cb5SThomas Graf 		return err;
236286872cb5SThomas Graf 
236386872cb5SThomas Graf 	return ip6_route_del(&cfg);
23641da177e4SLinus Torvalds }
23651da177e4SLinus Torvalds 
2366c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23671da177e4SLinus Torvalds {
236886872cb5SThomas Graf 	struct fib6_config cfg;
236986872cb5SThomas Graf 	int err;
23701da177e4SLinus Torvalds 
237186872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
237286872cb5SThomas Graf 	if (err < 0)
237386872cb5SThomas Graf 		return err;
237486872cb5SThomas Graf 
237586872cb5SThomas Graf 	return ip6_route_add(&cfg);
23761da177e4SLinus Torvalds }
23771da177e4SLinus Torvalds 
2378339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2379339bf98fSThomas Graf {
2380339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2381339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2382339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2383339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2384339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2385339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2386339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2387339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2388339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
23896a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2390339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2391339bf98fSThomas Graf }
2392339bf98fSThomas Graf 
2393191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2394191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
23950d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
23960d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
23977bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
23981da177e4SLinus Torvalds {
2399346f870bSDavid S. Miller 	const struct inet_peer *peer;
24001da177e4SLinus Torvalds 	struct rtmsg *rtm;
24011da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2402e3703b3dSThomas Graf 	long expires;
24039e762a4aSPatrick McHardy 	u32 table;
2404f2c31e32SEric Dumazet 	struct neighbour *n;
2405346f870bSDavid S. Miller 	u32 ts, tsage;
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
24081da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
24091da177e4SLinus Torvalds 			/* success since this is not a prefix route */
24101da177e4SLinus Torvalds 			return 1;
24111da177e4SLinus Torvalds 		}
24121da177e4SLinus Torvalds 	}
24131da177e4SLinus Torvalds 
24142d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
241538308473SDavid S. Miller 	if (!nlh)
241626932566SPatrick McHardy 		return -EMSGSIZE;
24172d7202bfSThomas Graf 
24182d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
24191da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
24201da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
24211da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
24221da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2423c71099acSThomas Graf 	if (rt->rt6i_table)
24249e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2425c71099acSThomas Graf 	else
24269e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
24279e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2428c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2429c78679e8SDavid S. Miller 		goto nla_put_failure;
24301da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_REJECT)
24311da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
2432ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2433ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2434d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
24351da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
24361da177e4SLinus Torvalds 	else
24371da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
24381da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
24391da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
24401da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
24411da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
24421da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
24431da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
24441da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
24451da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_DEFAULT)
24461da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
24491da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 	if (dst) {
2452c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2453c78679e8SDavid S. Miller 			goto nla_put_failure;
24541da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
24551da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2456c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2457c78679e8SDavid S. Miller 			goto nla_put_failure;
24581da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
24591da177e4SLinus Torvalds 	if (src) {
2460c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2461c78679e8SDavid S. Miller 			goto nla_put_failure;
24621da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2463c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2464c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2465c78679e8SDavid S. Miller 		goto nla_put_failure;
24661da177e4SLinus Torvalds #endif
24677bc570c8SYOSHIFUJI Hideaki 	if (iif) {
24687bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
24697bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
24708229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
24717bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
24727bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
24737bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
24747bc570c8SYOSHIFUJI Hideaki 						return 0;
24757bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
24767bc570c8SYOSHIFUJI Hideaki 				} else {
24777bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
24787bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
24797bc570c8SYOSHIFUJI Hideaki 				}
24807bc570c8SYOSHIFUJI Hideaki 			}
24817bc570c8SYOSHIFUJI Hideaki 		} else
24827bc570c8SYOSHIFUJI Hideaki #endif
2483c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2484c78679e8SDavid S. Miller 				goto nla_put_failure;
24857bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
24861da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2487c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2488c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2489c78679e8SDavid S. Miller 			goto nla_put_failure;
2490c3968a85SDaniel Walter 	}
2491c3968a85SDaniel Walter 
2492c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2493c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
24944e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2495c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2496c78679e8SDavid S. Miller 			goto nla_put_failure;
24971da177e4SLinus Torvalds 	}
24982d7202bfSThomas Graf 
2499defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
25002d7202bfSThomas Graf 		goto nla_put_failure;
25012d7202bfSThomas Graf 
2502f2c31e32SEric Dumazet 	rcu_read_lock();
250327217455SDavid Miller 	n = dst_get_neighbour_noref(&rt->dst);
250494f826b8SEric Dumazet 	if (n) {
250594f826b8SEric Dumazet 		if (nla_put(skb, RTA_GATEWAY, 16, &n->primary_key) < 0) {
250694f826b8SEric Dumazet 			rcu_read_unlock();
250794f826b8SEric Dumazet 			goto nla_put_failure;
250894f826b8SEric Dumazet 		}
250994f826b8SEric Dumazet 	}
2510f2c31e32SEric Dumazet 	rcu_read_unlock();
25112d7202bfSThomas Graf 
2512c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2513c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2514c78679e8SDavid S. Miller 		goto nla_put_failure;
2515c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2516c78679e8SDavid S. Miller 		goto nla_put_failure;
251736e3deaeSYOSHIFUJI Hideaki 	if (!(rt->rt6i_flags & RTF_EXPIRES))
251836e3deaeSYOSHIFUJI Hideaki 		expires = 0;
2519d1918542SDavid S. Miller 	else if (rt->dst.expires - jiffies < INT_MAX)
2520d1918542SDavid S. Miller 		expires = rt->dst.expires - jiffies;
252136e3deaeSYOSHIFUJI Hideaki 	else
252236e3deaeSYOSHIFUJI Hideaki 		expires = INT_MAX;
252369cdf8f9SYOSHIFUJI Hideaki 
2524346f870bSDavid S. Miller 	peer = rt->rt6i_peer;
2525346f870bSDavid S. Miller 	ts = tsage = 0;
2526346f870bSDavid S. Miller 	if (peer && peer->tcp_ts_stamp) {
2527346f870bSDavid S. Miller 		ts = peer->tcp_ts;
2528346f870bSDavid S. Miller 		tsage = get_seconds() - peer->tcp_ts_stamp;
2529346f870bSDavid S. Miller 	}
2530346f870bSDavid S. Miller 
2531346f870bSDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, ts, tsage,
2532d8d1f30bSChangli Gao 			       expires, rt->dst.error) < 0)
2533e3703b3dSThomas Graf 		goto nla_put_failure;
25341da177e4SLinus Torvalds 
25352d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
25362d7202bfSThomas Graf 
25372d7202bfSThomas Graf nla_put_failure:
253826932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
253926932566SPatrick McHardy 	return -EMSGSIZE;
25401da177e4SLinus Torvalds }
25411da177e4SLinus Torvalds 
25421b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
25431da177e4SLinus Torvalds {
25441da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
25451da177e4SLinus Torvalds 	int prefix;
25461da177e4SLinus Torvalds 
25472d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
25482d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
25491da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
25501da177e4SLinus Torvalds 	} else
25511da177e4SLinus Torvalds 		prefix = 0;
25521da177e4SLinus Torvalds 
2553191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2554191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
25551da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
25567bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
25571da177e4SLinus Torvalds }
25581da177e4SLinus Torvalds 
2559c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
25601da177e4SLinus Torvalds {
25613b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2562ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
25631da177e4SLinus Torvalds 	struct rt6_info *rt;
2564ab364a6fSThomas Graf 	struct sk_buff *skb;
2565ab364a6fSThomas Graf 	struct rtmsg *rtm;
25664c9483b2SDavid S. Miller 	struct flowi6 fl6;
256772331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2568ab364a6fSThomas Graf 
2569ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2570ab364a6fSThomas Graf 	if (err < 0)
2571ab364a6fSThomas Graf 		goto errout;
2572ab364a6fSThomas Graf 
2573ab364a6fSThomas Graf 	err = -EINVAL;
25744c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2575ab364a6fSThomas Graf 
2576ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2577ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2578ab364a6fSThomas Graf 			goto errout;
2579ab364a6fSThomas Graf 
25804e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2581ab364a6fSThomas Graf 	}
2582ab364a6fSThomas Graf 
2583ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2584ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2585ab364a6fSThomas Graf 			goto errout;
2586ab364a6fSThomas Graf 
25874e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2588ab364a6fSThomas Graf 	}
2589ab364a6fSThomas Graf 
2590ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2591ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2592ab364a6fSThomas Graf 
2593ab364a6fSThomas Graf 	if (tb[RTA_OIF])
259472331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2595ab364a6fSThomas Graf 
2596ab364a6fSThomas Graf 	if (iif) {
2597ab364a6fSThomas Graf 		struct net_device *dev;
259872331bc0SShmulik Ladkani 		int flags = 0;
259972331bc0SShmulik Ladkani 
26005578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2601ab364a6fSThomas Graf 		if (!dev) {
2602ab364a6fSThomas Graf 			err = -ENODEV;
2603ab364a6fSThomas Graf 			goto errout;
2604ab364a6fSThomas Graf 		}
260572331bc0SShmulik Ladkani 
260672331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
260772331bc0SShmulik Ladkani 
260872331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
260972331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
261072331bc0SShmulik Ladkani 
261172331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
261272331bc0SShmulik Ladkani 							       flags);
261372331bc0SShmulik Ladkani 	} else {
261472331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
261572331bc0SShmulik Ladkani 
261672331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2617ab364a6fSThomas Graf 	}
26181da177e4SLinus Torvalds 
26191da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
262038308473SDavid S. Miller 	if (!skb) {
26212173bff5SShmulik Ladkani 		dst_release(&rt->dst);
2622ab364a6fSThomas Graf 		err = -ENOBUFS;
2623ab364a6fSThomas Graf 		goto errout;
2624ab364a6fSThomas Graf 	}
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
26271da177e4SLinus Torvalds 	   through good chunk of routing engine.
26281da177e4SLinus Torvalds 	 */
2629459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
26301da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
26311da177e4SLinus Torvalds 
2632d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
26331da177e4SLinus Torvalds 
26344c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
26351da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
26367bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
26371da177e4SLinus Torvalds 	if (err < 0) {
2638ab364a6fSThomas Graf 		kfree_skb(skb);
2639ab364a6fSThomas Graf 		goto errout;
26401da177e4SLinus Torvalds 	}
26411da177e4SLinus Torvalds 
26425578689aSDaniel Lezcano 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2643ab364a6fSThomas Graf errout:
26441da177e4SLinus Torvalds 	return err;
26451da177e4SLinus Torvalds }
26461da177e4SLinus Torvalds 
264786872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
26481da177e4SLinus Torvalds {
26491da177e4SLinus Torvalds 	struct sk_buff *skb;
26505578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2651528c4cebSDenis V. Lunev 	u32 seq;
2652528c4cebSDenis V. Lunev 	int err;
26530d51aa80SJamal Hadi Salim 
2654528c4cebSDenis V. Lunev 	err = -ENOBUFS;
265538308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
265686872cb5SThomas Graf 
2657339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
265838308473SDavid S. Miller 	if (!skb)
265921713ebcSThomas Graf 		goto errout;
26601da177e4SLinus Torvalds 
2661191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
26627bc570c8SYOSHIFUJI Hideaki 				event, info->pid, seq, 0, 0, 0);
266326932566SPatrick McHardy 	if (err < 0) {
266426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
266526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
266626932566SPatrick McHardy 		kfree_skb(skb);
266726932566SPatrick McHardy 		goto errout;
266826932566SPatrick McHardy 	}
26691ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
26705578689aSDaniel Lezcano 		    info->nlh, gfp_any());
26711ce85fe4SPablo Neira Ayuso 	return;
267221713ebcSThomas Graf errout:
267321713ebcSThomas Graf 	if (err < 0)
26745578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
26751da177e4SLinus Torvalds }
26761da177e4SLinus Torvalds 
26778ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
26788ed67789SDaniel Lezcano 				unsigned long event, void *data)
26798ed67789SDaniel Lezcano {
26808ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2681c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26828ed67789SDaniel Lezcano 
26838ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2684d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
26858ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
26868ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2687d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
26888ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2689d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
26908ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
26918ed67789SDaniel Lezcano #endif
26928ed67789SDaniel Lezcano 	}
26938ed67789SDaniel Lezcano 
26948ed67789SDaniel Lezcano 	return NOTIFY_OK;
26958ed67789SDaniel Lezcano }
26968ed67789SDaniel Lezcano 
26971da177e4SLinus Torvalds /*
26981da177e4SLinus Torvalds  *	/proc
26991da177e4SLinus Torvalds  */
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
27021da177e4SLinus Torvalds 
27031da177e4SLinus Torvalds struct rt6_proc_arg
27041da177e4SLinus Torvalds {
27051da177e4SLinus Torvalds 	char *buffer;
27061da177e4SLinus Torvalds 	int offset;
27071da177e4SLinus Torvalds 	int length;
27081da177e4SLinus Torvalds 	int skip;
27091da177e4SLinus Torvalds 	int len;
27101da177e4SLinus Torvalds };
27111da177e4SLinus Torvalds 
27121da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
27131da177e4SLinus Torvalds {
271433120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
271569cce1d1SDavid S. Miller 	struct neighbour *n;
27161da177e4SLinus Torvalds 
27174b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
27204b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
27211da177e4SLinus Torvalds #else
272233120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
27231da177e4SLinus Torvalds #endif
2724f2c31e32SEric Dumazet 	rcu_read_lock();
272527217455SDavid Miller 	n = dst_get_neighbour_noref(&rt->dst);
272669cce1d1SDavid S. Miller 	if (n) {
272769cce1d1SDavid S. Miller 		seq_printf(m, "%pi6", n->primary_key);
27281da177e4SLinus Torvalds 	} else {
272933120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
27301da177e4SLinus Torvalds 	}
2731f2c31e32SEric Dumazet 	rcu_read_unlock();
273233120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2733d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2734d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
2735d1918542SDavid S. Miller 		   rt->dst.dev ? rt->dst.dev->name : "");
27361da177e4SLinus Torvalds 	return 0;
27371da177e4SLinus Torvalds }
27381da177e4SLinus Torvalds 
273933120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
27401da177e4SLinus Torvalds {
2741f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
274232b293a5SJosh Hunt 	fib6_clean_all_ro(net, rt6_info_route, 0, m);
274333120b30SAlexey Dobriyan 	return 0;
27441da177e4SLinus Torvalds }
27451da177e4SLinus Torvalds 
274633120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
274733120b30SAlexey Dobriyan {
2748de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2749f3db4851SDaniel Lezcano }
2750f3db4851SDaniel Lezcano 
275133120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
275233120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
275333120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
275433120b30SAlexey Dobriyan 	.read		= seq_read,
275533120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2756b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
275733120b30SAlexey Dobriyan };
275833120b30SAlexey Dobriyan 
27591da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
27601da177e4SLinus Torvalds {
276169ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
27621da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
276369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
276469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
276569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
276669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
276769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2768fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
276969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
27701da177e4SLinus Torvalds 
27711da177e4SLinus Torvalds 	return 0;
27721da177e4SLinus Torvalds }
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
27751da177e4SLinus Torvalds {
2776de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
277769ddb805SDaniel Lezcano }
277869ddb805SDaniel Lezcano 
27799a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
27801da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
27811da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
27821da177e4SLinus Torvalds 	.read	 = seq_read,
27831da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2784b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
27851da177e4SLinus Torvalds };
27861da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
27871da177e4SLinus Torvalds 
27881da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
27891da177e4SLinus Torvalds 
27901da177e4SLinus Torvalds static
27918d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
27921da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
27931da177e4SLinus Torvalds {
2794c486da34SLucian Adrian Grijincu 	struct net *net;
2795c486da34SLucian Adrian Grijincu 	int delay;
2796c486da34SLucian Adrian Grijincu 	if (!write)
2797c486da34SLucian Adrian Grijincu 		return -EINVAL;
2798c486da34SLucian Adrian Grijincu 
2799c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2800c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
28018d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
28025b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
28031da177e4SLinus Torvalds 	return 0;
28041da177e4SLinus Torvalds }
28051da177e4SLinus Torvalds 
2806760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
28071da177e4SLinus Torvalds 	{
28081da177e4SLinus Torvalds 		.procname	=	"flush",
28094990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
28101da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
281189c8b3a1SDave Jones 		.mode		=	0200,
28126d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
28131da177e4SLinus Torvalds 	},
28141da177e4SLinus Torvalds 	{
28151da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
28169a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
28171da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28181da177e4SLinus Torvalds 		.mode		=	0644,
28196d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28201da177e4SLinus Torvalds 	},
28211da177e4SLinus Torvalds 	{
28221da177e4SLinus Torvalds 		.procname	=	"max_size",
28234990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
28241da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28251da177e4SLinus Torvalds 		.mode		=	0644,
28266d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
28271da177e4SLinus Torvalds 	},
28281da177e4SLinus Torvalds 	{
28291da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
28304990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28311da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28321da177e4SLinus Torvalds 		.mode		=	0644,
28336d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28341da177e4SLinus Torvalds 	},
28351da177e4SLinus Torvalds 	{
28361da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
28374990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
28381da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28391da177e4SLinus Torvalds 		.mode		=	0644,
28406d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28411da177e4SLinus Torvalds 	},
28421da177e4SLinus Torvalds 	{
28431da177e4SLinus Torvalds 		.procname	=	"gc_interval",
28444990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
28451da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28461da177e4SLinus Torvalds 		.mode		=	0644,
28476d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28481da177e4SLinus Torvalds 	},
28491da177e4SLinus Torvalds 	{
28501da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
28514990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
28521da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28531da177e4SLinus Torvalds 		.mode		=	0644,
2854f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28551da177e4SLinus Torvalds 	},
28561da177e4SLinus Torvalds 	{
28571da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
28584990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
28591da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28601da177e4SLinus Torvalds 		.mode		=	0644,
28616d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28621da177e4SLinus Torvalds 	},
28631da177e4SLinus Torvalds 	{
28641da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
28654990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
28661da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28671da177e4SLinus Torvalds 		.mode		=	0644,
2868f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28691da177e4SLinus Torvalds 	},
28701da177e4SLinus Torvalds 	{
28711da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
28724990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28731da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28741da177e4SLinus Torvalds 		.mode		=	0644,
28756d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
28761da177e4SLinus Torvalds 	},
2877f8572d8fSEric W. Biederman 	{ }
28781da177e4SLinus Torvalds };
28791da177e4SLinus Torvalds 
28802c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2881760f2d01SDaniel Lezcano {
2882760f2d01SDaniel Lezcano 	struct ctl_table *table;
2883760f2d01SDaniel Lezcano 
2884760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2885760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2886760f2d01SDaniel Lezcano 			GFP_KERNEL);
28875ee09105SYOSHIFUJI Hideaki 
28885ee09105SYOSHIFUJI Hideaki 	if (table) {
28895ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2890c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
289186393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
28925ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
28935ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28945ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
28955ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
28965ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
28975ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
28985ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
28999c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
29005ee09105SYOSHIFUJI Hideaki 	}
29015ee09105SYOSHIFUJI Hideaki 
2902760f2d01SDaniel Lezcano 	return table;
2903760f2d01SDaniel Lezcano }
29041da177e4SLinus Torvalds #endif
29051da177e4SLinus Torvalds 
29062c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2907cdb18761SDaniel Lezcano {
2908633d424bSPavel Emelyanov 	int ret = -ENOMEM;
29098ed67789SDaniel Lezcano 
291086393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
291186393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2912f2fc6a54SBenjamin Thery 
2913fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2914fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2915fc66f95cSEric Dumazet 
29168ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
29178ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
29188ed67789SDaniel Lezcano 					   GFP_KERNEL);
29198ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2920fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2921d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
29228ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2923d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
292462fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
292562fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29268ed67789SDaniel Lezcano 
29278ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29288ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
29298ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
29308ed67789SDaniel Lezcano 					       GFP_KERNEL);
293168fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
293268fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2933d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
29348ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2935d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
293662fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
293762fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29388ed67789SDaniel Lezcano 
29398ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
29408ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
29418ed67789SDaniel Lezcano 					       GFP_KERNEL);
294268fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
294368fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2944d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
29458ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2946d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
294762fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
294862fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29498ed67789SDaniel Lezcano #endif
29508ed67789SDaniel Lezcano 
2951b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2952b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2953b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2954b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2955b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2956b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2957b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2958b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2959b339a47cSPeter Zijlstra 
2960cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2961cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2962cdb18761SDaniel Lezcano 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2963cdb18761SDaniel Lezcano #endif
29646891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
29656891a346SBenjamin Thery 
29668ed67789SDaniel Lezcano 	ret = 0;
29678ed67789SDaniel Lezcano out:
29688ed67789SDaniel Lezcano 	return ret;
2969f2fc6a54SBenjamin Thery 
297068fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
297168fffc67SPeter Zijlstra out_ip6_prohibit_entry:
297268fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
297368fffc67SPeter Zijlstra out_ip6_null_entry:
297468fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
297568fffc67SPeter Zijlstra #endif
2976fc66f95cSEric Dumazet out_ip6_dst_entries:
2977fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2978f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2979f2fc6a54SBenjamin Thery 	goto out;
2980cdb18761SDaniel Lezcano }
2981cdb18761SDaniel Lezcano 
29822c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2983cdb18761SDaniel Lezcano {
2984cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS
2985cdb18761SDaniel Lezcano 	proc_net_remove(net, "ipv6_route");
2986cdb18761SDaniel Lezcano 	proc_net_remove(net, "rt6_stats");
2987cdb18761SDaniel Lezcano #endif
29888ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
29898ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29908ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
29918ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
29928ed67789SDaniel Lezcano #endif
299341bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2994cdb18761SDaniel Lezcano }
2995cdb18761SDaniel Lezcano 
2996cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
2997cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
2998cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
2999cdb18761SDaniel Lezcano };
3000cdb18761SDaniel Lezcano 
30018ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
30028ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
30038ed67789SDaniel Lezcano 	.priority = 0,
30048ed67789SDaniel Lezcano };
30058ed67789SDaniel Lezcano 
3006433d49c3SDaniel Lezcano int __init ip6_route_init(void)
30071da177e4SLinus Torvalds {
3008433d49c3SDaniel Lezcano 	int ret;
3009433d49c3SDaniel Lezcano 
30109a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
30119a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
30129a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
30139a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
30149a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3015c19a28e1SFernando Carrijo 		goto out;
301614e50e57SDavid S. Miller 
3017fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
30188ed67789SDaniel Lezcano 	if (ret)
3019bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3020bdb3289fSDaniel Lezcano 
3021*2a0c451aSThomas Graf 	ret = fib6_init();
3022fc66f95cSEric Dumazet 	if (ret)
3023fc66f95cSEric Dumazet 		goto out_dst_entries;
3024fc66f95cSEric Dumazet 
3025*2a0c451aSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_ops);
3026*2a0c451aSThomas Graf 	if (ret)
3027*2a0c451aSThomas Graf 		goto out_fib6_init;
3028*2a0c451aSThomas Graf 
30295dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
30305dc121e9SArnaud Ebalard 
30318ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
30328ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
30338ed67789SDaniel Lezcano 	 * manually for init_net */
3034d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
30358ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3036bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3037d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
30388ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3039d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
30408ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3041bdb3289fSDaniel Lezcano   #endif
3042*2a0c451aSThomas Graf 	ret = fib6_init_late();
3043433d49c3SDaniel Lezcano 	if (ret)
30448ed67789SDaniel Lezcano 		goto out_register_subsys;
3045433d49c3SDaniel Lezcano 
3046433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3047433d49c3SDaniel Lezcano 	if (ret)
3048*2a0c451aSThomas Graf 		goto out_fib6_init_late;
3049c35b7e72SDaniel Lezcano 
3050433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3051433d49c3SDaniel Lezcano 	if (ret)
3052433d49c3SDaniel Lezcano 		goto xfrm6_init;
30537e5449c2SDaniel Lezcano 
3054433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3055c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3056c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3057c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3058433d49c3SDaniel Lezcano 		goto fib6_rules_init;
3059433d49c3SDaniel Lezcano 
30608ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3061cdb18761SDaniel Lezcano 	if (ret)
3062cdb18761SDaniel Lezcano 		goto fib6_rules_init;
30638ed67789SDaniel Lezcano 
3064433d49c3SDaniel Lezcano out:
3065433d49c3SDaniel Lezcano 	return ret;
3066433d49c3SDaniel Lezcano 
3067433d49c3SDaniel Lezcano fib6_rules_init:
3068433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3069433d49c3SDaniel Lezcano xfrm6_init:
3070433d49c3SDaniel Lezcano 	xfrm6_fini();
3071*2a0c451aSThomas Graf out_fib6_init_late:
3072*2a0c451aSThomas Graf 	fib6_cleanup_late();
30738ed67789SDaniel Lezcano out_register_subsys:
30748ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
3075*2a0c451aSThomas Graf out_fib6_init:
3076*2a0c451aSThomas Graf 	fib6_gc_cleanup();
3077fc66f95cSEric Dumazet out_dst_entries:
3078fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3079433d49c3SDaniel Lezcano out_kmem_cache:
3080f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3081433d49c3SDaniel Lezcano 	goto out;
30821da177e4SLinus Torvalds }
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds void ip6_route_cleanup(void)
30851da177e4SLinus Torvalds {
30868ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3087101367c2SThomas Graf 	fib6_rules_cleanup();
30881da177e4SLinus Torvalds 	xfrm6_fini();
30891da177e4SLinus Torvalds 	fib6_gc_cleanup();
30908ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
309141bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3092f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
30931da177e4SLinus Torvalds }
3094