xref: /openbmc/linux/net/ipv6/route.c (revision 8104891b86b212de77063660c0c062b427526fa6)
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);
826e157b6aSDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sk_buff *skb);
831da177e4SLinus Torvalds 
8470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
85efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
86b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
87b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
8895c96174SEric Dumazet 					   unsigned int pref);
89efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
90b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
91b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
9270ceb4f5SYOSHIFUJI Hideaki #endif
9370ceb4f5SYOSHIFUJI Hideaki 
9406582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
9506582540SDavid S. Miller {
9606582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
9706582540SDavid S. Miller 	struct inet_peer *peer;
9806582540SDavid S. Miller 	u32 *p = NULL;
9906582540SDavid S. Miller 
1008e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1018e2ec639SYan, Zheng 		return NULL;
1028e2ec639SYan, Zheng 
103fbfe95a4SDavid S. Miller 	peer = rt6_get_peer_create(rt);
10406582540SDavid S. Miller 	if (peer) {
10506582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
10606582540SDavid S. Miller 		unsigned long prev, new;
10706582540SDavid S. Miller 
10806582540SDavid S. Miller 		p = peer->metrics;
10906582540SDavid S. Miller 		if (inet_metrics_new(peer))
11006582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
11106582540SDavid S. Miller 
11206582540SDavid S. Miller 		new = (unsigned long) p;
11306582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
11406582540SDavid S. Miller 
11506582540SDavid S. Miller 		if (prev != old) {
11606582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
11706582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
11806582540SDavid S. Miller 				p = NULL;
11906582540SDavid S. Miller 		}
12006582540SDavid S. Miller 	}
12106582540SDavid S. Miller 	return p;
12206582540SDavid S. Miller }
12306582540SDavid S. Miller 
124f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt,
125f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
126f894cbf8SDavid S. Miller 					     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;
132f894cbf8SDavid S. Miller 	else if (skb)
133f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
13439232973SDavid S. Miller 	return daddr;
13539232973SDavid S. Miller }
13639232973SDavid S. Miller 
137f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
138f894cbf8SDavid S. Miller 					  struct sk_buff *skb,
139f894cbf8SDavid S. Miller 					  const void *daddr)
140d3aaeb38SDavid S. Miller {
14139232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
14239232973SDavid S. Miller 	struct neighbour *n;
14339232973SDavid S. Miller 
144f894cbf8SDavid S. Miller 	daddr = choose_neigh_daddr(rt, skb, daddr);
14539232973SDavid S. Miller 	n = __ipv6_neigh_lookup(&nd_tbl, dst->dev, daddr);
146f83c7790SDavid S. Miller 	if (n)
147f83c7790SDavid S. Miller 		return n;
148f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
149f83c7790SDavid S. Miller }
150f83c7790SDavid S. Miller 
1518ade06c6SDavid S. Miller static int rt6_bind_neighbour(struct rt6_info *rt, struct net_device *dev)
152f83c7790SDavid S. Miller {
1538ade06c6SDavid S. Miller 	struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dev, &rt->rt6i_gateway);
1548ade06c6SDavid S. Miller 	if (!n) {
1558ade06c6SDavid S. Miller 		n = neigh_create(&nd_tbl, &rt->rt6i_gateway, dev);
156f83c7790SDavid S. Miller 		if (IS_ERR(n))
157f83c7790SDavid S. Miller 			return PTR_ERR(n);
1588ade06c6SDavid S. Miller 	}
15997cac082SDavid S. Miller 	rt->n = n;
160f83c7790SDavid S. Miller 
161f83c7790SDavid S. Miller 	return 0;
162d3aaeb38SDavid S. Miller }
163d3aaeb38SDavid S. Miller 
1649a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1651da177e4SLinus Torvalds 	.family			=	AF_INET6,
16609640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1671da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1681da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1691da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1700dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
171ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
17206582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1731da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1741da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1751da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1761da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1771da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1786e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
1791ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
180d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1811da177e4SLinus Torvalds };
1821da177e4SLinus Torvalds 
183ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
184ec831ea7SRoland Dreier {
185618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
186618f9bc7SSteffen Klassert 
187618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
188ec831ea7SRoland Dreier }
189ec831ea7SRoland Dreier 
19014e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
19114e50e57SDavid S. Miller {
19214e50e57SDavid S. Miller }
19314e50e57SDavid S. Miller 
194b587ee3bSDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sk_buff *skb)
195b587ee3bSDavid S. Miller {
196b587ee3bSDavid S. Miller }
197b587ee3bSDavid S. Miller 
1980972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
1990972ddb2SHeld Bernhard 					 unsigned long old)
2000972ddb2SHeld Bernhard {
2010972ddb2SHeld Bernhard 	return NULL;
2020972ddb2SHeld Bernhard }
2030972ddb2SHeld Bernhard 
20414e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
20514e50e57SDavid S. Miller 	.family			=	AF_INET6,
20609640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
20714e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
20814e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
209ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
210214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
21114e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
212b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2130972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
214d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
21514e50e57SDavid S. Miller };
21614e50e57SDavid S. Miller 
21762fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
21862fa8a84SDavid S. Miller 	[RTAX_HOPLIMIT - 1] = 255,
21962fa8a84SDavid S. Miller };
22062fa8a84SDavid S. Miller 
221bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = {
2221da177e4SLinus Torvalds 	.dst = {
2231da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2241da177e4SLinus Torvalds 		.__use		= 1,
2251da177e4SLinus Torvalds 		.obsolete	= -1,
2261da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2271da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2281da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2291da177e4SLinus Torvalds 	},
2301da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2314f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2321da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2331da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2341da177e4SLinus Torvalds };
2351da177e4SLinus Torvalds 
236101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
237101367c2SThomas Graf 
2386723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
2396723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
2406723ab54SDavid S. Miller 
241280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = {
242101367c2SThomas Graf 	.dst = {
243101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
244101367c2SThomas Graf 		.__use		= 1,
245101367c2SThomas Graf 		.obsolete	= -1,
246101367c2SThomas Graf 		.error		= -EACCES,
2479ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2489ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
249101367c2SThomas Graf 	},
250101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2514f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
252101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
253101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
254101367c2SThomas Graf };
255101367c2SThomas Graf 
256bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = {
257101367c2SThomas Graf 	.dst = {
258101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
259101367c2SThomas Graf 		.__use		= 1,
260101367c2SThomas Graf 		.obsolete	= -1,
261101367c2SThomas Graf 		.error		= -EINVAL,
262352e512cSHerbert Xu 		.input		= dst_discard,
263352e512cSHerbert Xu 		.output		= dst_discard,
264101367c2SThomas Graf 	},
265101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2664f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
267101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
268101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
269101367c2SThomas Graf };
270101367c2SThomas Graf 
271101367c2SThomas Graf #endif
272101367c2SThomas Graf 
2731da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
27497bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net,
275957c665fSDavid S. Miller 					     struct net_device *dev,
2768b96d22dSDavid S. Miller 					     int flags,
2778b96d22dSDavid S. Miller 					     struct fib6_table *table)
2781da177e4SLinus Torvalds {
27997bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
28097bab73fSDavid S. Miller 					0, 0, flags);
281cf911662SDavid S. Miller 
28297bab73fSDavid S. Miller 	if (rt) {
283*8104891bSSteffen Klassert 		struct dst_entry *dst = &rt->dst;
284*8104891bSSteffen Klassert 
285*8104891bSSteffen Klassert 		memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
2868b96d22dSDavid S. Miller 		rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
28797bab73fSDavid S. Miller 	}
288cf911662SDavid S. Miller 	return rt;
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
2941da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
2951da177e4SLinus Torvalds 
29697cac082SDavid S. Miller 	if (rt->n)
29797cac082SDavid S. Miller 		neigh_release(rt->n);
29897cac082SDavid S. Miller 
2998e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
3008e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
3018e2ec639SYan, Zheng 
30238308473SDavid S. Miller 	if (idev) {
3031da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3041da177e4SLinus Torvalds 		in6_dev_put(idev);
3051da177e4SLinus Torvalds 	}
3061716a961SGao feng 
3071716a961SGao feng 	if (!(rt->rt6i_flags & RTF_EXPIRES) && dst->from)
3081716a961SGao feng 		dst_release(dst->from);
3091716a961SGao feng 
31097bab73fSDavid S. Miller 	if (rt6_has_peer(rt)) {
31197bab73fSDavid S. Miller 		struct inet_peer *peer = rt6_peer_ptr(rt);
312b3419363SDavid S. Miller 		inet_putpeer(peer);
313b3419363SDavid S. Miller 	}
314b3419363SDavid S. Miller }
315b3419363SDavid S. Miller 
3166431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
3176431cbc2SDavid S. Miller 
3186431cbc2SDavid S. Miller static u32 rt6_peer_genid(void)
3196431cbc2SDavid S. Miller {
3206431cbc2SDavid S. Miller 	return atomic_read(&__rt6_peer_genid);
3216431cbc2SDavid S. Miller }
3226431cbc2SDavid S. Miller 
323b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
324b3419363SDavid S. Miller {
32597bab73fSDavid S. Miller 	struct inet_peer_base *base;
326b3419363SDavid S. Miller 	struct inet_peer *peer;
327b3419363SDavid S. Miller 
32897bab73fSDavid S. Miller 	base = inetpeer_base_ptr(rt->_rt6i_peer);
32997bab73fSDavid S. Miller 	if (!base)
33097bab73fSDavid S. Miller 		return;
33197bab73fSDavid S. Miller 
33297bab73fSDavid S. Miller 	peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
3337b34ca2aSDavid S. Miller 	if (peer) {
33497bab73fSDavid S. Miller 		if (!rt6_set_peer(rt, peer))
335b3419363SDavid S. Miller 			inet_putpeer(peer);
3366431cbc2SDavid S. Miller 		else
3376431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
3381da177e4SLinus Torvalds 	}
3397b34ca2aSDavid S. Miller }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3421da177e4SLinus Torvalds 			   int how)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3451da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3465a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
347c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3481da177e4SLinus Torvalds 
34997cac082SDavid S. Miller 	if (dev != loopback_dev) {
35097cac082SDavid S. Miller 		if (idev && idev->dev == dev) {
3515a3e55d6SDenis V. Lunev 			struct inet6_dev *loopback_idev =
3525a3e55d6SDenis V. Lunev 				in6_dev_get(loopback_dev);
35338308473SDavid S. Miller 			if (loopback_idev) {
3541da177e4SLinus Torvalds 				rt->rt6i_idev = loopback_idev;
3551da177e4SLinus Torvalds 				in6_dev_put(idev);
3561da177e4SLinus Torvalds 			}
3571da177e4SLinus Torvalds 		}
35897cac082SDavid S. Miller 		if (rt->n && rt->n->dev == dev) {
35997cac082SDavid S. Miller 			rt->n->dev = loopback_dev;
36097cac082SDavid S. Miller 			dev_hold(loopback_dev);
36197cac082SDavid S. Miller 			dev_put(dev);
36297cac082SDavid S. Miller 		}
36397cac082SDavid S. Miller 	}
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
366a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3671da177e4SLinus Torvalds {
3681716a961SGao feng 	struct rt6_info *ort = NULL;
3691716a961SGao feng 
3701716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3711716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
372a50feda5SEric Dumazet 			return true;
3731716a961SGao feng 	} else if (rt->dst.from) {
3741716a961SGao feng 		ort = (struct rt6_info *) rt->dst.from;
3751716a961SGao feng 		return (ort->rt6i_flags & RTF_EXPIRES) &&
3761716a961SGao feng 			time_after(jiffies, ort->dst.expires);
3771716a961SGao feng 	}
378a50feda5SEric Dumazet 	return false;
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds 
381a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr)
382c71099acSThomas Graf {
383a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
384a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
385c71099acSThomas Graf }
386c71099acSThomas Graf 
3871da177e4SLinus Torvalds /*
388c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
3891da177e4SLinus Torvalds  */
3901da177e4SLinus Torvalds 
3918ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
3928ed67789SDaniel Lezcano 						    struct rt6_info *rt,
393b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
3941da177e4SLinus Torvalds 						    int oif,
395d420895eSYOSHIFUJI Hideaki 						    int flags)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
3981da177e4SLinus Torvalds 	struct rt6_info *sprt;
3991da177e4SLinus Torvalds 
400dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
401dd3abc4eSYOSHIFUJI Hideaki 		goto out;
402dd3abc4eSYOSHIFUJI Hideaki 
403d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
404d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
405dd3abc4eSYOSHIFUJI Hideaki 
406dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4071da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4081da177e4SLinus Torvalds 				return sprt;
4091da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
41038308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
4111da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
412d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
4131da177e4SLinus Torvalds 						continue;
4141da177e4SLinus Torvalds 					if (local && (!oif ||
4151da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
4161da177e4SLinus Torvalds 						continue;
4171da177e4SLinus Torvalds 				}
4181da177e4SLinus Torvalds 				local = sprt;
4191da177e4SLinus Torvalds 			}
420dd3abc4eSYOSHIFUJI Hideaki 		} else {
421dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
422dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
423dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
424dd3abc4eSYOSHIFUJI Hideaki 		}
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
427dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4281da177e4SLinus Torvalds 		if (local)
4291da177e4SLinus Torvalds 			return local;
4301da177e4SLinus Torvalds 
431d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4328ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
4331da177e4SLinus Torvalds 	}
434dd3abc4eSYOSHIFUJI Hideaki out:
4351da177e4SLinus Torvalds 	return rt;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
43827097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
43927097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
44027097255SYOSHIFUJI Hideaki {
441f2c31e32SEric Dumazet 	struct neighbour *neigh;
44227097255SYOSHIFUJI Hideaki 	/*
44327097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
44427097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
44527097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
44627097255SYOSHIFUJI Hideaki 	 *
44727097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
44827097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
44927097255SYOSHIFUJI Hideaki 	 */
450f2c31e32SEric Dumazet 	rcu_read_lock();
45197cac082SDavid S. Miller 	neigh = rt ? rt->n : NULL;
45227097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
453f2c31e32SEric Dumazet 		goto out;
45427097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
45527097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
45652e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
45727097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
45827097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
45927097255SYOSHIFUJI Hideaki 
46027097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
46127097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
46227097255SYOSHIFUJI Hideaki 
46327097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
46427097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
465d1918542SDavid S. Miller 		ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
466f2c31e32SEric Dumazet 	} else {
46727097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
46827097255SYOSHIFUJI Hideaki 	}
469f2c31e32SEric Dumazet out:
470f2c31e32SEric Dumazet 	rcu_read_unlock();
471f2c31e32SEric Dumazet }
47227097255SYOSHIFUJI Hideaki #else
47327097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
47427097255SYOSHIFUJI Hideaki {
47527097255SYOSHIFUJI Hideaki }
47627097255SYOSHIFUJI Hideaki #endif
47727097255SYOSHIFUJI Hideaki 
4781da177e4SLinus Torvalds /*
479554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
4801da177e4SLinus Torvalds  */
481b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
4821da177e4SLinus Torvalds {
483d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
484161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
485554cfb7eSYOSHIFUJI Hideaki 		return 2;
486161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
487161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
488161980f4SDavid S. Miller 		return 1;
489554cfb7eSYOSHIFUJI Hideaki 	return 0;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
492b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
4931da177e4SLinus Torvalds {
494f2c31e32SEric Dumazet 	struct neighbour *neigh;
495398bcbebSYOSHIFUJI Hideaki 	int m;
496f2c31e32SEric Dumazet 
497f2c31e32SEric Dumazet 	rcu_read_lock();
49897cac082SDavid S. Miller 	neigh = rt->n;
4994d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
5004d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
5014d0c5911SYOSHIFUJI Hideaki 		m = 1;
5024d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
5031da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
504554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
5054d0c5911SYOSHIFUJI Hideaki 			m = 2;
506398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
507398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
508398bcbebSYOSHIFUJI Hideaki 			m = 0;
509398bcbebSYOSHIFUJI Hideaki #endif
510398bcbebSYOSHIFUJI Hideaki 		else
511ea73ee23SYOSHIFUJI Hideaki 			m = 1;
5121da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
513398bcbebSYOSHIFUJI Hideaki 	} else
514398bcbebSYOSHIFUJI Hideaki 		m = 0;
515f2c31e32SEric Dumazet 	rcu_read_unlock();
516554cfb7eSYOSHIFUJI Hideaki 	return m;
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
519554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
520554cfb7eSYOSHIFUJI Hideaki 			   int strict)
521554cfb7eSYOSHIFUJI Hideaki {
5224d0c5911SYOSHIFUJI Hideaki 	int m, n;
5234d0c5911SYOSHIFUJI Hideaki 
5244d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
52577d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
526554cfb7eSYOSHIFUJI Hideaki 		return -1;
527ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
528ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
529ebacaaa0SYOSHIFUJI Hideaki #endif
5304d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
531557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
532554cfb7eSYOSHIFUJI Hideaki 		return -1;
533554cfb7eSYOSHIFUJI Hideaki 	return m;
534554cfb7eSYOSHIFUJI Hideaki }
535554cfb7eSYOSHIFUJI Hideaki 
536f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
537f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
538554cfb7eSYOSHIFUJI Hideaki {
539554cfb7eSYOSHIFUJI Hideaki 	int m;
540554cfb7eSYOSHIFUJI Hideaki 
541554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
542f11e6659SDavid S. Miller 		goto out;
543554cfb7eSYOSHIFUJI Hideaki 
544554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
545554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
546f11e6659SDavid S. Miller 		goto out;
547554cfb7eSYOSHIFUJI Hideaki 
548f11e6659SDavid S. Miller 	if (m > *mpri) {
549ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
55027097255SYOSHIFUJI Hideaki 			rt6_probe(match);
551f11e6659SDavid S. Miller 		*mpri = m;
552554cfb7eSYOSHIFUJI Hideaki 		match = rt;
553ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
55427097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
5551da177e4SLinus Torvalds 	}
556f11e6659SDavid S. Miller 
557f11e6659SDavid S. Miller out:
558f11e6659SDavid S. Miller 	return match;
5591da177e4SLinus Torvalds }
5601da177e4SLinus Torvalds 
561f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
562f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
563f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
564f11e6659SDavid S. Miller {
565f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
566f11e6659SDavid S. Miller 	int mpri = -1;
567f11e6659SDavid S. Miller 
568f11e6659SDavid S. Miller 	match = NULL;
569f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
570d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
571f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
572f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
573d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
574f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
575f11e6659SDavid S. Miller 
576f11e6659SDavid S. Miller 	return match;
577f11e6659SDavid S. Miller }
578f11e6659SDavid S. Miller 
579f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
580f11e6659SDavid S. Miller {
581f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
5828ed67789SDaniel Lezcano 	struct net *net;
583f11e6659SDavid S. Miller 
584f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
585f11e6659SDavid S. Miller 	if (!rt0)
586f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
587f11e6659SDavid S. Miller 
588f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
589f11e6659SDavid S. Miller 
590554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
591f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
592d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
593f11e6659SDavid S. Miller 
594554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
595f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
596f11e6659SDavid S. Miller 			next = fn->leaf;
597f11e6659SDavid S. Miller 
598f11e6659SDavid S. Miller 		if (next != rt0)
599f11e6659SDavid S. Miller 			fn->rr_ptr = next;
600554cfb7eSYOSHIFUJI Hideaki 	}
601554cfb7eSYOSHIFUJI Hideaki 
602d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
603a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
60670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
60770ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
608b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
60970ceb4f5SYOSHIFUJI Hideaki {
610c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
61170ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
61270ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
61370ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
6144bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
61570ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
61670ceb4f5SYOSHIFUJI Hideaki 
61770ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
61870ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
61970ceb4f5SYOSHIFUJI Hideaki 	}
62070ceb4f5SYOSHIFUJI Hideaki 
62170ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
62270ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
62370ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
62470ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
62570ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
62670ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
62770ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
62870ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
62970ceb4f5SYOSHIFUJI Hideaki 		}
63070ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
63170ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
63270ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
63370ceb4f5SYOSHIFUJI Hideaki 		}
63470ceb4f5SYOSHIFUJI Hideaki 	}
63570ceb4f5SYOSHIFUJI Hideaki 
63670ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
63770ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
6383933fc95SJens Rosenboom 		return -EINVAL;
63970ceb4f5SYOSHIFUJI Hideaki 
6404bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
64170ceb4f5SYOSHIFUJI Hideaki 
64270ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
64370ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
64470ceb4f5SYOSHIFUJI Hideaki 	else {
64570ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
64670ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
64770ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
64870ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
64970ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
65070ceb4f5SYOSHIFUJI Hideaki 	}
65170ceb4f5SYOSHIFUJI Hideaki 
652efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
653efa2cea0SDaniel Lezcano 				dev->ifindex);
65470ceb4f5SYOSHIFUJI Hideaki 
65570ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
656e0a1ad73SThomas Graf 		ip6_del_rt(rt);
65770ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
65870ceb4f5SYOSHIFUJI Hideaki 	}
65970ceb4f5SYOSHIFUJI Hideaki 
66070ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
661efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
66270ceb4f5SYOSHIFUJI Hideaki 					pref);
66370ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
66470ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
66570ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
66670ceb4f5SYOSHIFUJI Hideaki 
66770ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
6681716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
6691716a961SGao feng 			rt6_clean_expires(rt);
6701716a961SGao feng 		else
6711716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
6721716a961SGao feng 
673d8d1f30bSChangli Gao 		dst_release(&rt->dst);
67470ceb4f5SYOSHIFUJI Hideaki 	}
67570ceb4f5SYOSHIFUJI Hideaki 	return 0;
67670ceb4f5SYOSHIFUJI Hideaki }
67770ceb4f5SYOSHIFUJI Hideaki #endif
67870ceb4f5SYOSHIFUJI Hideaki 
6798ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
680982f56f3SYOSHIFUJI Hideaki do { \
6818ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
682982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
683e0eda7bbSVille Nuorvala 		while (1) { \
684982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
685c71099acSThomas Graf 				goto out; \
686982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
687982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
6888bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
689982f56f3SYOSHIFUJI Hideaki 			else \
690982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
691c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
692c71099acSThomas Graf 				goto restart; \
693c71099acSThomas Graf 		} \
694982f56f3SYOSHIFUJI Hideaki 	} \
695982f56f3SYOSHIFUJI Hideaki } while (0)
696c71099acSThomas Graf 
6978ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
6988ed67789SDaniel Lezcano 					     struct fib6_table *table,
6994c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	struct fib6_node *fn;
7021da177e4SLinus Torvalds 	struct rt6_info *rt;
7031da177e4SLinus Torvalds 
704c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7054c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
706c71099acSThomas Graf restart:
707c71099acSThomas Graf 	rt = fn->leaf;
7084c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
7094c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
710c71099acSThomas Graf out:
711d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
712c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7131da177e4SLinus Torvalds 	return rt;
714c71099acSThomas Graf 
715c71099acSThomas Graf }
716c71099acSThomas Graf 
717ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
718ea6e574eSFlorian Westphal 				    int flags)
719ea6e574eSFlorian Westphal {
720ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
721ea6e574eSFlorian Westphal }
722ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
723ea6e574eSFlorian Westphal 
7249acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
7259acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
726c71099acSThomas Graf {
7274c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
7284c9483b2SDavid S. Miller 		.flowi6_oif = oif,
7294c9483b2SDavid S. Miller 		.daddr = *daddr,
730c71099acSThomas Graf 	};
731c71099acSThomas Graf 	struct dst_entry *dst;
73277d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
733c71099acSThomas Graf 
734adaa70bbSThomas Graf 	if (saddr) {
7354c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
736adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
737adaa70bbSThomas Graf 	}
738adaa70bbSThomas Graf 
7394c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
740c71099acSThomas Graf 	if (dst->error == 0)
741c71099acSThomas Graf 		return (struct rt6_info *) dst;
742c71099acSThomas Graf 
743c71099acSThomas Graf 	dst_release(dst);
744c71099acSThomas Graf 
7451da177e4SLinus Torvalds 	return NULL;
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds 
7487159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
7497159039aSYOSHIFUJI Hideaki 
750c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
7511da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
7521da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
7531da177e4SLinus Torvalds    be destroyed.
7541da177e4SLinus Torvalds  */
7551da177e4SLinus Torvalds 
75686872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	int err;
759c71099acSThomas Graf 	struct fib6_table *table;
7601da177e4SLinus Torvalds 
761c71099acSThomas Graf 	table = rt->rt6i_table;
762c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
76386872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
764c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	return err;
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
76940e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
77040e22e8fSThomas Graf {
7714d1169c1SDenis V. Lunev 	struct nl_info info = {
772d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
7734d1169c1SDenis V. Lunev 	};
774528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
77540e22e8fSThomas Graf }
77640e22e8fSThomas Graf 
7771716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
77821efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
779b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
7801da177e4SLinus Torvalds {
7811da177e4SLinus Torvalds 	struct rt6_info *rt;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	/*
7841da177e4SLinus Torvalds 	 *	Clone the route.
7851da177e4SLinus Torvalds 	 */
7861da177e4SLinus Torvalds 
78721efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	if (rt) {
79014deae41SDavid S. Miller 		int attempts = !in_softirq();
79114deae41SDavid S. Miller 
79258c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags & RTF_GATEWAY)) {
793bb3c3686SDavid S. Miller 			if (ort->rt6i_dst.plen != 128 &&
79421efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
79558c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
7964e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = *daddr;
79758c4fb86SYOSHIFUJI Hideaki 		}
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
8021da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
8034e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
8041da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
8051da177e4SLinus Torvalds 		}
8061da177e4SLinus Torvalds #endif
8071da177e4SLinus Torvalds 
80814deae41SDavid S. Miller 	retry:
8098ade06c6SDavid S. Miller 		if (rt6_bind_neighbour(rt, rt->dst.dev)) {
810d1918542SDavid S. Miller 			struct net *net = dev_net(rt->dst.dev);
81114deae41SDavid S. Miller 			int saved_rt_min_interval =
81214deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval;
81314deae41SDavid S. Miller 			int saved_rt_elasticity =
81414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity;
81514deae41SDavid S. Miller 
81614deae41SDavid S. Miller 			if (attempts-- > 0) {
81714deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
81814deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
81914deae41SDavid S. Miller 
82086393e52SAlexey Dobriyan 				ip6_dst_gc(&net->ipv6.ip6_dst_ops);
82114deae41SDavid S. Miller 
82214deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity =
82314deae41SDavid S. Miller 					saved_rt_elasticity;
82414deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval =
82514deae41SDavid S. Miller 					saved_rt_min_interval;
82614deae41SDavid S. Miller 				goto retry;
82714deae41SDavid S. Miller 			}
82814deae41SDavid S. Miller 
829f3213831SJoe Perches 			net_warn_ratelimited("Neighbour table overflow\n");
830d8d1f30bSChangli Gao 			dst_free(&rt->dst);
83114deae41SDavid S. Miller 			return NULL;
83214deae41SDavid S. Miller 		}
83395a9a5baSYOSHIFUJI Hideaki 	}
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	return rt;
8361da177e4SLinus Torvalds }
83795a9a5baSYOSHIFUJI Hideaki 
83821efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
83921efcfa0SEric Dumazet 					const struct in6_addr *daddr)
840299d9939SYOSHIFUJI Hideaki {
84121efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
84221efcfa0SEric Dumazet 
843299d9939SYOSHIFUJI Hideaki 	if (rt) {
844299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
84597cac082SDavid S. Miller 		rt->n = neigh_clone(ort->n);
846299d9939SYOSHIFUJI Hideaki 	}
847299d9939SYOSHIFUJI Hideaki 	return rt;
848299d9939SYOSHIFUJI Hideaki }
849299d9939SYOSHIFUJI Hideaki 
8508ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
8514c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
8521da177e4SLinus Torvalds {
8531da177e4SLinus Torvalds 	struct fib6_node *fn;
854519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
855c71099acSThomas Graf 	int strict = 0;
8561da177e4SLinus Torvalds 	int attempts = 3;
857519fbd87SYOSHIFUJI Hideaki 	int err;
85853b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
8591da177e4SLinus Torvalds 
86077d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds relookup:
863c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
8641da177e4SLinus Torvalds 
8658238dd06SYOSHIFUJI Hideaki restart_2:
8664c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds restart:
8694acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
8708ed67789SDaniel Lezcano 
8714c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
8728ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
8738238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
8741da177e4SLinus Torvalds 		goto out;
8751da177e4SLinus Torvalds 
876d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
877c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
8781da177e4SLinus Torvalds 
87997cac082SDavid S. Miller 	if (!rt->n && !(rt->rt6i_flags & RTF_NONEXTHOP))
8804c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
8817343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
8824c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
8837343ff31SDavid S. Miller 	else
8847343ff31SDavid S. Miller 		goto out2;
8851da177e4SLinus Torvalds 
886d8d1f30bSChangli Gao 	dst_release(&rt->dst);
8878ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
8881da177e4SLinus Torvalds 
889d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
890e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
89140e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
892e40cf353SYOSHIFUJI Hideaki 		if (!err)
893e40cf353SYOSHIFUJI Hideaki 			goto out2;
894e40cf353SYOSHIFUJI Hideaki 	}
895e40cf353SYOSHIFUJI Hideaki 
896e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
8971da177e4SLinus Torvalds 		goto out2;
8981da177e4SLinus Torvalds 
899519fbd87SYOSHIFUJI Hideaki 	/*
900c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
901519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
9021da177e4SLinus Torvalds 	 */
903d8d1f30bSChangli Gao 	dst_release(&rt->dst);
9041da177e4SLinus Torvalds 	goto relookup;
905e40cf353SYOSHIFUJI Hideaki 
906519fbd87SYOSHIFUJI Hideaki out:
9078238dd06SYOSHIFUJI Hideaki 	if (reachable) {
9088238dd06SYOSHIFUJI Hideaki 		reachable = 0;
9098238dd06SYOSHIFUJI Hideaki 		goto restart_2;
9108238dd06SYOSHIFUJI Hideaki 	}
911d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
912c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9131da177e4SLinus Torvalds out2:
914d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
915d8d1f30bSChangli Gao 	rt->dst.__use++;
916c71099acSThomas Graf 
917c71099acSThomas Graf 	return rt;
918c71099acSThomas Graf }
919c71099acSThomas Graf 
9208ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9214c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9224acad72dSPavel Emelyanov {
9234c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9244acad72dSPavel Emelyanov }
9254acad72dSPavel Emelyanov 
92672331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
92772331bc0SShmulik Ladkani 						struct net_device *dev,
92872331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
92972331bc0SShmulik Ladkani {
93072331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
93172331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
93272331bc0SShmulik Ladkani 
93372331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
93472331bc0SShmulik Ladkani }
93572331bc0SShmulik Ladkani 
936c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
937c71099acSThomas Graf {
938b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
939c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
940adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
9414c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
9424c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
9434c9483b2SDavid S. Miller 		.daddr = iph->daddr,
9444c9483b2SDavid S. Miller 		.saddr = iph->saddr,
9454c9483b2SDavid S. Miller 		.flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
9464c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
9474c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
948c71099acSThomas Graf 	};
949adaa70bbSThomas Graf 
95072331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
951c71099acSThomas Graf }
952c71099acSThomas Graf 
9538ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
9544c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
955c71099acSThomas Graf {
9564c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
957c71099acSThomas Graf }
958c71099acSThomas Graf 
9599c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
9604c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
961c71099acSThomas Graf {
962c71099acSThomas Graf 	int flags = 0;
963c71099acSThomas Graf 
9644dc27d1cSDavid McCullough 	fl6->flowi6_iif = net->loopback_dev->ifindex;
9654dc27d1cSDavid McCullough 
9664c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
96777d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
968c71099acSThomas Graf 
9694c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
970adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
9710c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
9720c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
973adaa70bbSThomas Graf 
9744c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
9777159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
9781da177e4SLinus Torvalds 
9792774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
98014e50e57SDavid S. Miller {
9815c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
98214e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
98314e50e57SDavid S. Miller 
9845c1e6aa3SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0);
98514e50e57SDavid S. Miller 	if (rt) {
986d8d1f30bSChangli Gao 		new = &rt->dst;
98714e50e57SDavid S. Miller 
988*8104891bSSteffen Klassert 		memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
989*8104891bSSteffen Klassert 		rt6_init_peer(rt, net->ipv6.peers);
990*8104891bSSteffen Klassert 
99114e50e57SDavid S. Miller 		new->__use = 1;
992352e512cSHerbert Xu 		new->input = dst_discard;
993352e512cSHerbert Xu 		new->output = dst_discard;
99414e50e57SDavid S. Miller 
99521efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
99621efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
99721efcfa0SEric Dumazet 		else
998defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
99914e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
100014e50e57SDavid S. Miller 		if (rt->rt6i_idev)
100114e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
100214e50e57SDavid S. Miller 
10034e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
10041716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
10051716a961SGao feng 		rt6_clean_expires(rt);
100614e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
100714e50e57SDavid S. Miller 
100814e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
100914e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
101014e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
101114e50e57SDavid S. Miller #endif
101214e50e57SDavid S. Miller 
101314e50e57SDavid S. Miller 		dst_free(new);
101414e50e57SDavid S. Miller 	}
101514e50e57SDavid S. Miller 
101669ead7afSDavid S. Miller 	dst_release(dst_orig);
101769ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
101814e50e57SDavid S. Miller }
101914e50e57SDavid S. Miller 
10201da177e4SLinus Torvalds /*
10211da177e4SLinus Torvalds  *	Destination cache support functions
10221da177e4SLinus Torvalds  */
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10251da177e4SLinus Torvalds {
10261da177e4SLinus Torvalds 	struct rt6_info *rt;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10291da177e4SLinus Torvalds 
10306431cbc2SDavid S. Miller 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
10316431cbc2SDavid S. Miller 		if (rt->rt6i_peer_genid != rt6_peer_genid()) {
103297bab73fSDavid S. Miller 			if (!rt6_has_peer(rt))
10336431cbc2SDavid S. Miller 				rt6_bind_peer(rt, 0);
10346431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
10356431cbc2SDavid S. Miller 		}
10361da177e4SLinus Torvalds 		return dst;
10376431cbc2SDavid S. Miller 	}
10381da177e4SLinus Torvalds 	return NULL;
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
10421da177e4SLinus Torvalds {
10431da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	if (rt) {
104654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
104754c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1048e0a1ad73SThomas Graf 				ip6_del_rt(rt);
104954c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
10501da177e4SLinus Torvalds 			}
105154c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
105254c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
105354c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
105454c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
105554c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
105654c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
10571da177e4SLinus Torvalds }
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
10601da177e4SLinus Torvalds {
10611da177e4SLinus Torvalds 	struct rt6_info *rt;
10621da177e4SLinus Torvalds 
10633ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
10641da177e4SLinus Torvalds 
1065adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
10661da177e4SLinus Torvalds 	if (rt) {
10671716a961SGao feng 		if (rt->rt6i_flags & RTF_CACHE)
10681716a961SGao feng 			rt6_update_expires(rt, 0);
10691716a961SGao feng 		else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
10701da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
10711da177e4SLinus Torvalds 	}
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
10771da177e4SLinus Torvalds 
107881aded24SDavid S. Miller 	dst_confirm(dst);
10791da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
108081aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
108181aded24SDavid S. Miller 
10821da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
10831da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1084defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
10851da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1086defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1087defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
10881da177e4SLinus Torvalds 		}
1089defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
109081aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
10911da177e4SLinus Torvalds 	}
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
109442ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
109542ae66c8SDavid S. Miller 		     int oif, u32 mark)
109681aded24SDavid S. Miller {
109781aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
109881aded24SDavid S. Miller 	struct dst_entry *dst;
109981aded24SDavid S. Miller 	struct flowi6 fl6;
110081aded24SDavid S. Miller 
110181aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
110281aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
110381aded24SDavid S. Miller 	fl6.flowi6_mark = mark;
11043e12939aSDavid S. Miller 	fl6.flowi6_flags = 0;
110581aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
110681aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
110781aded24SDavid S. Miller 	fl6.flowlabel = (*(__be32 *) iph) & IPV6_FLOWINFO_MASK;
110881aded24SDavid S. Miller 
110981aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
111081aded24SDavid S. Miller 	if (!dst->error)
111181aded24SDavid S. Miller 		ip6_rt_update_pmtu(dst, ntohl(mtu));
111281aded24SDavid S. Miller 	dst_release(dst);
111381aded24SDavid S. Miller }
111481aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
111581aded24SDavid S. Miller 
111681aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
111781aded24SDavid S. Miller {
111881aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
111981aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
112081aded24SDavid S. Miller }
112181aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
112281aded24SDavid S. Miller 
11233a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
11243a5ad2eeSDavid S. Miller {
11253a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
11263a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
11273a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
11283a5ad2eeSDavid S. Miller 
11293a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
11303a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
11313a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
11323a5ad2eeSDavid S. Miller 	fl6.flowi6_flags = 0;
11333a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
11343a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
11353a5ad2eeSDavid S. Miller 	fl6.flowlabel = (*(__be32 *) iph) & IPV6_FLOWINFO_MASK;
11363a5ad2eeSDavid S. Miller 
11373a5ad2eeSDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
11383a5ad2eeSDavid S. Miller 	if (!dst->error)
11393a5ad2eeSDavid S. Miller 		rt6_do_redirect(dst, skb);
11403a5ad2eeSDavid S. Miller 	dst_release(dst);
11413a5ad2eeSDavid S. Miller }
11423a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
11433a5ad2eeSDavid S. Miller 
11443a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
11453a5ad2eeSDavid S. Miller {
11463a5ad2eeSDavid S. Miller 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
11473a5ad2eeSDavid S. Miller }
11483a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
11493a5ad2eeSDavid S. Miller 
11500dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
11511da177e4SLinus Torvalds {
11520dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
11530dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
11540dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
11550dbaee3bSDavid S. Miller 
11561da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
11571da177e4SLinus Torvalds 
11585578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
11595578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	/*
11621da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
11631da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
11641da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
11651da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
11661da177e4SLinus Torvalds 	 */
11671da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
11681da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
11691da177e4SLinus Torvalds 	return mtu;
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds 
1172ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1173d33e4553SDavid S. Miller {
1174d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1175618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1176618f9bc7SSteffen Klassert 
1177618f9bc7SSteffen Klassert 	if (mtu)
1178618f9bc7SSteffen Klassert 		return mtu;
1179618f9bc7SSteffen Klassert 
1180618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1181d33e4553SDavid S. Miller 
1182d33e4553SDavid S. Miller 	rcu_read_lock();
1183d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1184d33e4553SDavid S. Miller 	if (idev)
1185d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1186d33e4553SDavid S. Miller 	rcu_read_unlock();
1187d33e4553SDavid S. Miller 
1188d33e4553SDavid S. Miller 	return mtu;
1189d33e4553SDavid S. Miller }
1190d33e4553SDavid S. Miller 
11913b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
11923b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
11935d0bbeebSThomas Graf 
11943b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
11951da177e4SLinus Torvalds 				  struct neighbour *neigh,
119687a11578SDavid S. Miller 				  struct flowi6 *fl6)
11971da177e4SLinus Torvalds {
119887a11578SDavid S. Miller 	struct dst_entry *dst;
11991da177e4SLinus Torvalds 	struct rt6_info *rt;
12001da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1201c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
12021da177e4SLinus Torvalds 
120338308473SDavid S. Miller 	if (unlikely(!idev))
1204122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
12051da177e4SLinus Torvalds 
12068b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
120738308473SDavid S. Miller 	if (unlikely(!rt)) {
12081da177e4SLinus Torvalds 		in6_dev_put(idev);
120987a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
12101da177e4SLinus Torvalds 		goto out;
12111da177e4SLinus Torvalds 	}
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	if (neigh)
12141da177e4SLinus Torvalds 		neigh_hold(neigh);
121514deae41SDavid S. Miller 	else {
1216f894cbf8SDavid S. Miller 		neigh = ip6_neigh_lookup(&rt->dst, NULL, &fl6->daddr);
1217b43faac6SDavid S. Miller 		if (IS_ERR(neigh)) {
1218252c3d84SRongQing.Li 			in6_dev_put(idev);
1219b43faac6SDavid S. Miller 			dst_free(&rt->dst);
1220b43faac6SDavid S. Miller 			return ERR_CAST(neigh);
1221b43faac6SDavid S. Miller 		}
122214deae41SDavid S. Miller 	}
12231da177e4SLinus Torvalds 
12248e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
12258e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
122697cac082SDavid S. Miller 	rt->n = neigh;
1227d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
122887a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
12298e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
12308e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
12317011687fSGao feng 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
12321da177e4SLinus Torvalds 
12333b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1234d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1235d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
12363b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12371da177e4SLinus Torvalds 
12385578689aSDaniel Lezcano 	fib6_force_start_gc(net);
12391da177e4SLinus Torvalds 
124087a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
124187a11578SDavid S. Miller 
12421da177e4SLinus Torvalds out:
124387a11578SDavid S. Miller 	return dst;
12441da177e4SLinus Torvalds }
12451da177e4SLinus Torvalds 
12463d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
12471da177e4SLinus Torvalds {
1248e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
12493d0f24a7SStephen Hemminger 	int more = 0;
12501da177e4SLinus Torvalds 
12513b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
12523b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
12535d0bbeebSThomas Graf 
12541da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
12551da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
12561da177e4SLinus Torvalds 			*pprev = dst->next;
12571da177e4SLinus Torvalds 			dst_free(dst);
12581da177e4SLinus Torvalds 		} else {
12591da177e4SLinus Torvalds 			pprev = &dst->next;
12603d0f24a7SStephen Hemminger 			++more;
12611da177e4SLinus Torvalds 		}
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds 
12643b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
12655d0bbeebSThomas Graf 
12663d0f24a7SStephen Hemminger 	return more;
12671da177e4SLinus Torvalds }
12681da177e4SLinus Torvalds 
12691e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
12701e493d19SDavid S. Miller 			    void *arg)
12711e493d19SDavid S. Miller {
12721e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
12731e493d19SDavid S. Miller 
12741e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
12751e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
12761e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
12771e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
12781e493d19SDavid S. Miller 		if (func(rt, arg)) {
12791e493d19SDavid S. Miller 			*pprev = dst->next;
12801e493d19SDavid S. Miller 			dst_free(dst);
12811e493d19SDavid S. Miller 		} else {
12821e493d19SDavid S. Miller 			pprev = &dst->next;
12831e493d19SDavid S. Miller 		}
12841e493d19SDavid S. Miller 	}
12851e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
12861e493d19SDavid S. Miller }
12871e493d19SDavid S. Miller 
1288569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
12891da177e4SLinus Torvalds {
12901da177e4SLinus Torvalds 	unsigned long now = jiffies;
129186393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
12927019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
12937019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
12947019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
12957019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
12967019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1297fc66f95cSEric Dumazet 	int entries;
12981da177e4SLinus Torvalds 
1299fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
13007019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1301fc66f95cSEric Dumazet 	    entries <= rt_max_size)
13021da177e4SLinus Torvalds 		goto out;
13031da177e4SLinus Torvalds 
13046891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
13056891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
13066891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1307fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1308fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
13097019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
13101da177e4SLinus Torvalds out:
13117019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1312fc66f95cSEric Dumazet 	return entries > rt_max_size;
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
13161da177e4SLinus Torvalds    but results in cleaner routing tables.
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds    Remove it only when all the things will work!
13191da177e4SLinus Torvalds  */
13201da177e4SLinus Torvalds 
13216b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
13221da177e4SLinus Torvalds {
13235170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1324a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
13256b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1326c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1327c68f24ccSEric Dumazet 
1328c68f24ccSEric Dumazet 		rcu_read_lock();
1329c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1330c68f24ccSEric Dumazet 		if (idev)
13311da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1332c68f24ccSEric Dumazet 		else
133353b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1334c68f24ccSEric Dumazet 		rcu_read_unlock();
13351da177e4SLinus Torvalds 	}
13361da177e4SLinus Torvalds 	return hoplimit;
13371da177e4SLinus Torvalds }
1338abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds /*
13411da177e4SLinus Torvalds  *
13421da177e4SLinus Torvalds  */
13431da177e4SLinus Torvalds 
134486872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
13451da177e4SLinus Torvalds {
13461da177e4SLinus Torvalds 	int err;
13475578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
13481da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
13491da177e4SLinus Torvalds 	struct net_device *dev = NULL;
13501da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1351c71099acSThomas Graf 	struct fib6_table *table;
13521da177e4SLinus Torvalds 	int addr_type;
13531da177e4SLinus Torvalds 
135486872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
13551da177e4SLinus Torvalds 		return -EINVAL;
13561da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
135786872cb5SThomas Graf 	if (cfg->fc_src_len)
13581da177e4SLinus Torvalds 		return -EINVAL;
13591da177e4SLinus Torvalds #endif
136086872cb5SThomas Graf 	if (cfg->fc_ifindex) {
13611da177e4SLinus Torvalds 		err = -ENODEV;
13625578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
13631da177e4SLinus Torvalds 		if (!dev)
13641da177e4SLinus Torvalds 			goto out;
13651da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
13661da177e4SLinus Torvalds 		if (!idev)
13671da177e4SLinus Torvalds 			goto out;
13681da177e4SLinus Torvalds 	}
13691da177e4SLinus Torvalds 
137086872cb5SThomas Graf 	if (cfg->fc_metric == 0)
137186872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
13721da177e4SLinus Torvalds 
1373c71099acSThomas Graf 	err = -ENOBUFS;
137438308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1375d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1376d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
137738308473SDavid S. Miller 		if (!table) {
1378f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1379d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1380d71314b4SMatti Vaittinen 		}
1381d71314b4SMatti Vaittinen 	} else {
1382d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1383d71314b4SMatti Vaittinen 	}
138438308473SDavid S. Miller 
138538308473SDavid S. Miller 	if (!table)
1386c71099acSThomas Graf 		goto out;
1387c71099acSThomas Graf 
13888b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
13891da177e4SLinus Torvalds 
139038308473SDavid S. Miller 	if (!rt) {
13911da177e4SLinus Torvalds 		err = -ENOMEM;
13921da177e4SLinus Torvalds 		goto out;
13931da177e4SLinus Torvalds 	}
13941da177e4SLinus Torvalds 
1395d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
13961716a961SGao feng 
13971716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
13981716a961SGao feng 		rt6_set_expires(rt, jiffies +
13991716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
14001716a961SGao feng 	else
14011716a961SGao feng 		rt6_clean_expires(rt);
14021da177e4SLinus Torvalds 
140386872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
140486872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
140586872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
140686872cb5SThomas Graf 
140786872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1410d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1411ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1412ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
14131da177e4SLinus Torvalds 	else
1414d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
14151da177e4SLinus Torvalds 
1416d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
14171da177e4SLinus Torvalds 
141886872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
141986872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
14201da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
142111d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
14221da177e4SLinus Torvalds 
14238e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
14248e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
14258e2ec639SYan, Zheng 		if (!metrics) {
14268e2ec639SYan, Zheng 			err = -ENOMEM;
14278e2ec639SYan, Zheng 			goto out;
14288e2ec639SYan, Zheng 		}
14298e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
14308e2ec639SYan, Zheng 	}
14311da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
143286872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
143386872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
14341da177e4SLinus Torvalds #endif
14351da177e4SLinus Torvalds 
143686872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
14391da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
14401da177e4SLinus Torvalds 	 */
144186872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
144238308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
144338308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
144438308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
14451da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
14465578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
14471da177e4SLinus Torvalds 			if (dev) {
14481da177e4SLinus Torvalds 				dev_put(dev);
14491da177e4SLinus Torvalds 				in6_dev_put(idev);
14501da177e4SLinus Torvalds 			}
14515578689aSDaniel Lezcano 			dev = net->loopback_dev;
14521da177e4SLinus Torvalds 			dev_hold(dev);
14531da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
14541da177e4SLinus Torvalds 			if (!idev) {
14551da177e4SLinus Torvalds 				err = -ENODEV;
14561da177e4SLinus Torvalds 				goto out;
14571da177e4SLinus Torvalds 			}
14581da177e4SLinus Torvalds 		}
1459d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1460d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
1461d8d1f30bSChangli Gao 		rt->dst.error = -ENETUNREACH;
14621da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
14631da177e4SLinus Torvalds 		goto install_route;
14641da177e4SLinus Torvalds 	}
14651da177e4SLinus Torvalds 
146686872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1467b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
14681da177e4SLinus Torvalds 		int gwa_type;
14691da177e4SLinus Torvalds 
147086872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
14714e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
14721da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
14731da177e4SLinus Torvalds 
14741da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
14751da177e4SLinus Torvalds 			struct rt6_info *grt;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
14781da177e4SLinus Torvalds 			   addresses as nexthop address.
14791da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
14801da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
14811da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
14821da177e4SLinus Torvalds 			   some exceptions. --ANK
14831da177e4SLinus Torvalds 			 */
14841da177e4SLinus Torvalds 			err = -EINVAL;
14851da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
14861da177e4SLinus Torvalds 				goto out;
14871da177e4SLinus Torvalds 
14885578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
149138308473SDavid S. Miller 			if (!grt)
14921da177e4SLinus Torvalds 				goto out;
14931da177e4SLinus Torvalds 			if (dev) {
1494d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
1495d8d1f30bSChangli Gao 					dst_release(&grt->dst);
14961da177e4SLinus Torvalds 					goto out;
14971da177e4SLinus Torvalds 				}
14981da177e4SLinus Torvalds 			} else {
1499d1918542SDavid S. Miller 				dev = grt->dst.dev;
15001da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
15011da177e4SLinus Torvalds 				dev_hold(dev);
15021da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
15031da177e4SLinus Torvalds 			}
15041da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
15051da177e4SLinus Torvalds 				err = 0;
1506d8d1f30bSChangli Gao 			dst_release(&grt->dst);
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 			if (err)
15091da177e4SLinus Torvalds 				goto out;
15101da177e4SLinus Torvalds 		}
15111da177e4SLinus Torvalds 		err = -EINVAL;
151238308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
15131da177e4SLinus Torvalds 			goto out;
15141da177e4SLinus Torvalds 	}
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds 	err = -ENODEV;
151738308473SDavid S. Miller 	if (!dev)
15181da177e4SLinus Torvalds 		goto out;
15191da177e4SLinus Torvalds 
1520c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1521c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1522c3968a85SDaniel Walter 			err = -EINVAL;
1523c3968a85SDaniel Walter 			goto out;
1524c3968a85SDaniel Walter 		}
15254e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1526c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1527c3968a85SDaniel Walter 	} else
1528c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1529c3968a85SDaniel Walter 
153086872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
15318ade06c6SDavid S. Miller 		err = rt6_bind_neighbour(rt, dev);
1532f83c7790SDavid S. Miller 		if (err)
15331da177e4SLinus Torvalds 			goto out;
15341da177e4SLinus Torvalds 	}
15351da177e4SLinus Torvalds 
153686872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds install_route:
153986872cb5SThomas Graf 	if (cfg->fc_mx) {
154086872cb5SThomas Graf 		struct nlattr *nla;
154186872cb5SThomas Graf 		int remaining;
15421da177e4SLinus Torvalds 
154386872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
15448f4c1f9bSThomas Graf 			int type = nla_type(nla);
154586872cb5SThomas Graf 
154686872cb5SThomas Graf 			if (type) {
154786872cb5SThomas Graf 				if (type > RTAX_MAX) {
15481da177e4SLinus Torvalds 					err = -EINVAL;
15491da177e4SLinus Torvalds 					goto out;
15501da177e4SLinus Torvalds 				}
155186872cb5SThomas Graf 
1552defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
15531da177e4SLinus Torvalds 			}
15541da177e4SLinus Torvalds 		}
15551da177e4SLinus Torvalds 	}
15561da177e4SLinus Torvalds 
1557d8d1f30bSChangli Gao 	rt->dst.dev = dev;
15581da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1559c71099acSThomas Graf 	rt->rt6i_table = table;
156063152fc0SDaniel Lezcano 
1561c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
156263152fc0SDaniel Lezcano 
156386872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds out:
15661da177e4SLinus Torvalds 	if (dev)
15671da177e4SLinus Torvalds 		dev_put(dev);
15681da177e4SLinus Torvalds 	if (idev)
15691da177e4SLinus Torvalds 		in6_dev_put(idev);
15701da177e4SLinus Torvalds 	if (rt)
1571d8d1f30bSChangli Gao 		dst_free(&rt->dst);
15721da177e4SLinus Torvalds 	return err;
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
157586872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
15761da177e4SLinus Torvalds {
15771da177e4SLinus Torvalds 	int err;
1578c71099acSThomas Graf 	struct fib6_table *table;
1579d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
15801da177e4SLinus Torvalds 
15818ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry)
15826c813a72SPatrick McHardy 		return -ENOENT;
15836c813a72SPatrick McHardy 
1584c71099acSThomas Graf 	table = rt->rt6i_table;
1585c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
15861da177e4SLinus Torvalds 
158786872cb5SThomas Graf 	err = fib6_del(rt, info);
1588d8d1f30bSChangli Gao 	dst_release(&rt->dst);
15891da177e4SLinus Torvalds 
1590c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
15911da177e4SLinus Torvalds 
15921da177e4SLinus Torvalds 	return err;
15931da177e4SLinus Torvalds }
15941da177e4SLinus Torvalds 
1595e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1596e0a1ad73SThomas Graf {
15974d1169c1SDenis V. Lunev 	struct nl_info info = {
1598d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
15994d1169c1SDenis V. Lunev 	};
1600528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1601e0a1ad73SThomas Graf }
1602e0a1ad73SThomas Graf 
160386872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
16041da177e4SLinus Torvalds {
1605c71099acSThomas Graf 	struct fib6_table *table;
16061da177e4SLinus Torvalds 	struct fib6_node *fn;
16071da177e4SLinus Torvalds 	struct rt6_info *rt;
16081da177e4SLinus Torvalds 	int err = -ESRCH;
16091da177e4SLinus Torvalds 
16105578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
161138308473SDavid S. Miller 	if (!table)
1612c71099acSThomas Graf 		return err;
16131da177e4SLinus Torvalds 
1614c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1615c71099acSThomas Graf 
1616c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
161786872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
161886872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 	if (fn) {
1621d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
162286872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1623d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1624d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
16251da177e4SLinus Torvalds 				continue;
162686872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
162786872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
16281da177e4SLinus Torvalds 				continue;
162986872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
16301da177e4SLinus Torvalds 				continue;
1631d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1632c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
16331da177e4SLinus Torvalds 
163486872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
16351da177e4SLinus Torvalds 		}
16361da177e4SLinus Torvalds 	}
1637c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	return err;
16401da177e4SLinus Torvalds }
16411da177e4SLinus Torvalds 
16426e157b6aSDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sk_buff *skb)
1643a6279458SYOSHIFUJI Hideaki {
1644e8599ff4SDavid S. Miller 	struct net *net = dev_net(skb->dev);
1645a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1646e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
1647e8599ff4SDavid S. Miller 	const struct in6_addr *target;
1648e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
16496e157b6aSDavid S. Miller 	const struct in6_addr *dest;
16506e157b6aSDavid S. Miller 	struct neighbour *old_neigh;
1651e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
1652e8599ff4SDavid S. Miller 	struct neighbour *neigh;
1653e8599ff4SDavid S. Miller 	struct icmp6hdr *icmph;
16546e157b6aSDavid S. Miller 	int optlen, on_link;
16556e157b6aSDavid S. Miller 	u8 *lladdr;
1656e8599ff4SDavid S. Miller 
1657e8599ff4SDavid S. Miller 	optlen = skb->tail - skb->transport_header;
1658e8599ff4SDavid S. Miller 	optlen -= sizeof(struct icmp6hdr) + 2 * sizeof(struct in6_addr);
1659e8599ff4SDavid S. Miller 
1660e8599ff4SDavid S. Miller 	if (optlen < 0) {
16616e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1662e8599ff4SDavid S. Miller 		return;
1663e8599ff4SDavid S. Miller 	}
1664e8599ff4SDavid S. Miller 
1665e8599ff4SDavid S. Miller 	icmph = icmp6_hdr(skb);
1666e8599ff4SDavid S. Miller 	target = (const struct in6_addr *) (icmph + 1);
1667e8599ff4SDavid S. Miller 	dest = target + 1;
1668e8599ff4SDavid S. Miller 
1669e8599ff4SDavid S. Miller 	if (ipv6_addr_is_multicast(dest)) {
16706e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1671e8599ff4SDavid S. Miller 		return;
1672e8599ff4SDavid S. Miller 	}
1673e8599ff4SDavid S. Miller 
16746e157b6aSDavid S. Miller 	on_link = 0;
1675e8599ff4SDavid S. Miller 	if (ipv6_addr_equal(dest, target)) {
1676e8599ff4SDavid S. Miller 		on_link = 1;
1677e8599ff4SDavid S. Miller 	} else if (ipv6_addr_type(target) !=
1678e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
16796e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1680e8599ff4SDavid S. Miller 		return;
1681e8599ff4SDavid S. Miller 	}
1682e8599ff4SDavid S. Miller 
1683e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
1684e8599ff4SDavid S. Miller 	if (!in6_dev)
1685e8599ff4SDavid S. Miller 		return;
1686e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1687e8599ff4SDavid S. Miller 		return;
1688e8599ff4SDavid S. Miller 
1689e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
1690e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
1691e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
1692e8599ff4SDavid S. Miller 	 */
1693e8599ff4SDavid S. Miller 
1694e8599ff4SDavid S. Miller 	if (!ndisc_parse_options((u8*)(dest + 1), optlen, &ndopts)) {
1695e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1696e8599ff4SDavid S. Miller 		return;
1697e8599ff4SDavid S. Miller 	}
16986e157b6aSDavid S. Miller 
16996e157b6aSDavid S. Miller 	lladdr = NULL;
1700e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
1701e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1702e8599ff4SDavid S. Miller 					     skb->dev);
1703e8599ff4SDavid S. Miller 		if (!lladdr) {
1704e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1705e8599ff4SDavid S. Miller 			return;
1706e8599ff4SDavid S. Miller 		}
1707e8599ff4SDavid S. Miller 	}
1708e8599ff4SDavid S. Miller 
17096e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
17106e157b6aSDavid S. Miller 	if (rt == net->ipv6.ip6_null_entry) {
17116e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
17126e157b6aSDavid S. Miller 		return;
17136e157b6aSDavid S. Miller 	}
17146e157b6aSDavid S. Miller 
17156e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
17166e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
17176e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
17186e157b6aSDavid S. Miller 	 */
17196e157b6aSDavid S. Miller 	dst_confirm(&rt->dst);
17206e157b6aSDavid S. Miller 
1721e8599ff4SDavid S. Miller 	neigh = __neigh_lookup(&nd_tbl, target, skb->dev, 1);
1722e8599ff4SDavid S. Miller 	if (!neigh)
1723e8599ff4SDavid S. Miller 		return;
1724e8599ff4SDavid S. Miller 
17256e157b6aSDavid S. Miller 	/* Duplicate redirect: silently ignore. */
17266e157b6aSDavid S. Miller 	old_neigh = rt->n;
17276e157b6aSDavid S. Miller 	if (neigh == old_neigh)
1728a6279458SYOSHIFUJI Hideaki 		goto out;
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 	/*
17311da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
17321da177e4SLinus Torvalds 	 */
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
17351da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
17361da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
17371da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
17381da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
17391da177e4SLinus Torvalds 		     );
17401da177e4SLinus Torvalds 
174121efcfa0SEric Dumazet 	nrt = ip6_rt_copy(rt, dest);
174238308473SDavid S. Miller 	if (!nrt)
17431da177e4SLinus Torvalds 		goto out;
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
17461da177e4SLinus Torvalds 	if (on_link)
17471da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
17481da177e4SLinus Torvalds 
17494e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
175097cac082SDavid S. Miller 	nrt->n = neigh_clone(neigh);
17511da177e4SLinus Torvalds 
175240e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
17531da177e4SLinus Torvalds 		goto out;
17541da177e4SLinus Torvalds 
1755d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
17561d248b1cSDavid S. Miller 	netevent.old_neigh = old_neigh;
1757d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
17581d248b1cSDavid S. Miller 	netevent.new_neigh = neigh;
17591d248b1cSDavid S. Miller 	netevent.daddr = dest;
17608d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
17618d71740cSTom Tucker 
17621da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
17636e157b6aSDavid S. Miller 		rt = (struct rt6_info *) dst_clone(&rt->dst);
1764e0a1ad73SThomas Graf 		ip6_del_rt(rt);
17651da177e4SLinus Torvalds 	}
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds out:
1768e8599ff4SDavid S. Miller 	neigh_release(neigh);
17696e157b6aSDavid S. Miller }
17706e157b6aSDavid S. Miller 
17711da177e4SLinus Torvalds /*
17721da177e4SLinus Torvalds  *	Misc support functions
17731da177e4SLinus Torvalds  */
17741da177e4SLinus Torvalds 
17751716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
177621efcfa0SEric Dumazet 				    const struct in6_addr *dest)
17771da177e4SLinus Torvalds {
1778d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
17798b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
17808b96d22dSDavid S. Miller 					    ort->rt6i_table);
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds 	if (rt) {
1783d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1784d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
17858e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
17861da177e4SLinus Torvalds 
17874e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
17888e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1789defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1790d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
17911da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
17921da177e4SLinus Torvalds 		if (rt->rt6i_idev)
17931da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1794d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
17951da177e4SLinus Torvalds 
17964e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
17971716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
17981716a961SGao feng 		if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
17991716a961SGao feng 		    (RTF_DEFAULT | RTF_ADDRCONF))
18001716a961SGao feng 			rt6_set_from(rt, ort);
18011716a961SGao feng 		else
18021716a961SGao feng 			rt6_clean_expires(rt);
18031da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
18061da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
18071da177e4SLinus Torvalds #endif
18080f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1809c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
18101da177e4SLinus Torvalds 	}
18111da177e4SLinus Torvalds 	return rt;
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds 
181470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1815efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1816b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1817b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
181870ceb4f5SYOSHIFUJI Hideaki {
181970ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
182070ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1821c71099acSThomas Graf 	struct fib6_table *table;
182270ceb4f5SYOSHIFUJI Hideaki 
1823efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
182438308473SDavid S. Miller 	if (!table)
1825c71099acSThomas Graf 		return NULL;
1826c71099acSThomas Graf 
1827c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1828c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
182970ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
183070ceb4f5SYOSHIFUJI Hideaki 		goto out;
183170ceb4f5SYOSHIFUJI Hideaki 
1832d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1833d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
183470ceb4f5SYOSHIFUJI Hideaki 			continue;
183570ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
183670ceb4f5SYOSHIFUJI Hideaki 			continue;
183770ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
183870ceb4f5SYOSHIFUJI Hideaki 			continue;
1839d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
184070ceb4f5SYOSHIFUJI Hideaki 		break;
184170ceb4f5SYOSHIFUJI Hideaki 	}
184270ceb4f5SYOSHIFUJI Hideaki out:
1843c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
184470ceb4f5SYOSHIFUJI Hideaki 	return rt;
184570ceb4f5SYOSHIFUJI Hideaki }
184670ceb4f5SYOSHIFUJI Hideaki 
1847efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1848b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1849b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
185095c96174SEric Dumazet 					   unsigned int pref)
185170ceb4f5SYOSHIFUJI Hideaki {
185286872cb5SThomas Graf 	struct fib6_config cfg = {
185386872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1854238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
185586872cb5SThomas Graf 		.fc_ifindex	= ifindex,
185686872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
185786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
185886872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
1859efa2cea0SDaniel Lezcano 		.fc_nlinfo.pid = 0,
1860efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1861efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
186286872cb5SThomas Graf 	};
186370ceb4f5SYOSHIFUJI Hideaki 
18644e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
18654e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
186686872cb5SThomas Graf 
1867e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1868e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
186986872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
187070ceb4f5SYOSHIFUJI Hideaki 
187186872cb5SThomas Graf 	ip6_route_add(&cfg);
187270ceb4f5SYOSHIFUJI Hideaki 
1873efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
187470ceb4f5SYOSHIFUJI Hideaki }
187570ceb4f5SYOSHIFUJI Hideaki #endif
187670ceb4f5SYOSHIFUJI Hideaki 
1877b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
18781da177e4SLinus Torvalds {
18791da177e4SLinus Torvalds 	struct rt6_info *rt;
1880c71099acSThomas Graf 	struct fib6_table *table;
18811da177e4SLinus Torvalds 
1882c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
188338308473SDavid S. Miller 	if (!table)
1884c71099acSThomas Graf 		return NULL;
18851da177e4SLinus Torvalds 
1886c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
1887d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1888d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
1889045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
18901da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
18911da177e4SLinus Torvalds 			break;
18921da177e4SLinus Torvalds 	}
18931da177e4SLinus Torvalds 	if (rt)
1894d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
1895c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
18961da177e4SLinus Torvalds 	return rt;
18971da177e4SLinus Torvalds }
18981da177e4SLinus Torvalds 
1899b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1900ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1901ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
19021da177e4SLinus Torvalds {
190386872cb5SThomas Graf 	struct fib6_config cfg = {
190486872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1905238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
190686872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
190786872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
190886872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
19095578689aSDaniel Lezcano 		.fc_nlinfo.pid = 0,
19105578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1911c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
191286872cb5SThomas Graf 	};
19131da177e4SLinus Torvalds 
19144e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
19151da177e4SLinus Torvalds 
191686872cb5SThomas Graf 	ip6_route_add(&cfg);
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
19217b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
19221da177e4SLinus Torvalds {
19231da177e4SLinus Torvalds 	struct rt6_info *rt;
1924c71099acSThomas Graf 	struct fib6_table *table;
1925c71099acSThomas Graf 
1926c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
19277b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
192838308473SDavid S. Miller 	if (!table)
1929c71099acSThomas Graf 		return;
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds restart:
1932c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1933d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
19341da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1935d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1936c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
1937e0a1ad73SThomas Graf 			ip6_del_rt(rt);
19381da177e4SLinus Torvalds 			goto restart;
19391da177e4SLinus Torvalds 		}
19401da177e4SLinus Torvalds 	}
1941c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
19421da177e4SLinus Torvalds }
19431da177e4SLinus Torvalds 
19445578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
19455578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
194686872cb5SThomas Graf 				 struct fib6_config *cfg)
194786872cb5SThomas Graf {
194886872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
194986872cb5SThomas Graf 
195086872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
195186872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
195286872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
195386872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
195486872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
195586872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
195686872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
195786872cb5SThomas Graf 
19585578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
1959f1243c2dSBenjamin Thery 
19604e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
19614e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
19624e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
196386872cb5SThomas Graf }
196486872cb5SThomas Graf 
19655578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
19661da177e4SLinus Torvalds {
196786872cb5SThomas Graf 	struct fib6_config cfg;
19681da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
19691da177e4SLinus Torvalds 	int err;
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	switch(cmd) {
19721da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
19731da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
19741da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
19751da177e4SLinus Torvalds 			return -EPERM;
19761da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
19771da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
19781da177e4SLinus Torvalds 		if (err)
19791da177e4SLinus Torvalds 			return -EFAULT;
19801da177e4SLinus Torvalds 
19815578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
198286872cb5SThomas Graf 
19831da177e4SLinus Torvalds 		rtnl_lock();
19841da177e4SLinus Torvalds 		switch (cmd) {
19851da177e4SLinus Torvalds 		case SIOCADDRT:
198686872cb5SThomas Graf 			err = ip6_route_add(&cfg);
19871da177e4SLinus Torvalds 			break;
19881da177e4SLinus Torvalds 		case SIOCDELRT:
198986872cb5SThomas Graf 			err = ip6_route_del(&cfg);
19901da177e4SLinus Torvalds 			break;
19911da177e4SLinus Torvalds 		default:
19921da177e4SLinus Torvalds 			err = -EINVAL;
19931da177e4SLinus Torvalds 		}
19941da177e4SLinus Torvalds 		rtnl_unlock();
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 		return err;
19973ff50b79SStephen Hemminger 	}
19981da177e4SLinus Torvalds 
19991da177e4SLinus Torvalds 	return -EINVAL;
20001da177e4SLinus Torvalds }
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds /*
20031da177e4SLinus Torvalds  *	Drop the packet on the floor
20041da177e4SLinus Torvalds  */
20051da177e4SLinus Torvalds 
2006d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
20071da177e4SLinus Torvalds {
2008612f09e8SYOSHIFUJI Hideaki 	int type;
2009adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
2010612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2011612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
20120660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
201345bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
20143bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20153bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2016612f09e8SYOSHIFUJI Hideaki 			break;
2017612f09e8SYOSHIFUJI Hideaki 		}
2018612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2019612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
20203bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
20213bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2022612f09e8SYOSHIFUJI Hideaki 		break;
2023612f09e8SYOSHIFUJI Hideaki 	}
20243ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
20251da177e4SLinus Torvalds 	kfree_skb(skb);
20261da177e4SLinus Torvalds 	return 0;
20271da177e4SLinus Torvalds }
20281da177e4SLinus Torvalds 
20299ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
20309ce8ade0SThomas Graf {
2031612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
20329ce8ade0SThomas Graf }
20339ce8ade0SThomas Graf 
203420380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
20351da177e4SLinus Torvalds {
2036adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2037612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
20381da177e4SLinus Torvalds }
20391da177e4SLinus Torvalds 
20406723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
20416723ab54SDavid S. Miller 
20429ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
20439ce8ade0SThomas Graf {
2044612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
20459ce8ade0SThomas Graf }
20469ce8ade0SThomas Graf 
20479ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
20489ce8ade0SThomas Graf {
2049adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2050612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
20519ce8ade0SThomas Graf }
20529ce8ade0SThomas Graf 
20536723ab54SDavid S. Miller #endif
20546723ab54SDavid S. Miller 
20551da177e4SLinus Torvalds /*
20561da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
20571da177e4SLinus Torvalds  */
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
20601da177e4SLinus Torvalds 				    const struct in6_addr *addr,
20618f031519SDavid S. Miller 				    bool anycast)
20621da177e4SLinus Torvalds {
2063c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
20648b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
2065f83c7790SDavid S. Miller 	int err;
20661da177e4SLinus Torvalds 
206738308473SDavid S. Miller 	if (!rt) {
2068f3213831SJoe Perches 		net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
20691da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
207040385653SBen Greear 	}
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 	in6_dev_hold(idev);
20731da177e4SLinus Torvalds 
207411d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2075d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2076d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
20771da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
2078d8d1f30bSChangli Gao 	rt->dst.obsolete = -1;
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
208158c4fb86SYOSHIFUJI Hideaki 	if (anycast)
208258c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
208358c4fb86SYOSHIFUJI Hideaki 	else
20841da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
20858ade06c6SDavid S. Miller 	err = rt6_bind_neighbour(rt, rt->dst.dev);
2086f83c7790SDavid S. Miller 	if (err) {
2087d8d1f30bSChangli Gao 		dst_free(&rt->dst);
2088f83c7790SDavid S. Miller 		return ERR_PTR(err);
20891da177e4SLinus Torvalds 	}
20901da177e4SLinus Torvalds 
20914e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
20921da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
20935578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
20941da177e4SLinus Torvalds 
2095d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds 	return rt;
20981da177e4SLinus Torvalds }
20991da177e4SLinus Torvalds 
2100c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2101c3968a85SDaniel Walter 			struct rt6_info *rt,
2102b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2103c3968a85SDaniel Walter 			unsigned int prefs,
2104c3968a85SDaniel Walter 			struct in6_addr *saddr)
2105c3968a85SDaniel Walter {
2106c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2107c3968a85SDaniel Walter 	int err = 0;
2108c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
21094e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2110c3968a85SDaniel Walter 	else
2111c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2112c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2113c3968a85SDaniel Walter 	return err;
2114c3968a85SDaniel Walter }
2115c3968a85SDaniel Walter 
2116c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2117c3968a85SDaniel Walter struct arg_dev_net_ip {
2118c3968a85SDaniel Walter 	struct net_device *dev;
2119c3968a85SDaniel Walter 	struct net *net;
2120c3968a85SDaniel Walter 	struct in6_addr *addr;
2121c3968a85SDaniel Walter };
2122c3968a85SDaniel Walter 
2123c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2124c3968a85SDaniel Walter {
2125c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2126c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2127c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2128c3968a85SDaniel Walter 
2129d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2130c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2131c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2132c3968a85SDaniel Walter 		/* remove prefsrc entry */
2133c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2134c3968a85SDaniel Walter 	}
2135c3968a85SDaniel Walter 	return 0;
2136c3968a85SDaniel Walter }
2137c3968a85SDaniel Walter 
2138c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2139c3968a85SDaniel Walter {
2140c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2141c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2142c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2143c3968a85SDaniel Walter 		.net = net,
2144c3968a85SDaniel Walter 		.addr = &ifp->addr,
2145c3968a85SDaniel Walter 	};
2146c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2147c3968a85SDaniel Walter }
2148c3968a85SDaniel Walter 
21498ed67789SDaniel Lezcano struct arg_dev_net {
21508ed67789SDaniel Lezcano 	struct net_device *dev;
21518ed67789SDaniel Lezcano 	struct net *net;
21528ed67789SDaniel Lezcano };
21538ed67789SDaniel Lezcano 
21541da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
21551da177e4SLinus Torvalds {
2156bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2157bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
21588ed67789SDaniel Lezcano 
2159d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2160c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
21611da177e4SLinus Torvalds 		return -1;
2162c159d30cSDavid S. Miller 
21631da177e4SLinus Torvalds 	return 0;
21641da177e4SLinus Torvalds }
21651da177e4SLinus Torvalds 
2166f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
21671da177e4SLinus Torvalds {
21688ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
21698ed67789SDaniel Lezcano 		.dev = dev,
21708ed67789SDaniel Lezcano 		.net = net,
21718ed67789SDaniel Lezcano 	};
21728ed67789SDaniel Lezcano 
21738ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
21741e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
21751da177e4SLinus Torvalds }
21761da177e4SLinus Torvalds 
217795c96174SEric Dumazet struct rt6_mtu_change_arg {
21781da177e4SLinus Torvalds 	struct net_device *dev;
217995c96174SEric Dumazet 	unsigned int mtu;
21801da177e4SLinus Torvalds };
21811da177e4SLinus Torvalds 
21821da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
21831da177e4SLinus Torvalds {
21841da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
21851da177e4SLinus Torvalds 	struct inet6_dev *idev;
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
21881da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
21891da177e4SLinus Torvalds 	   We still use this lock to block changes
21901da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
21911da177e4SLinus Torvalds 	*/
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
219438308473SDavid S. Miller 	if (!idev)
21951da177e4SLinus Torvalds 		return 0;
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
21981da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
21991da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
22001da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
22011da177e4SLinus Torvalds 	 */
22021da177e4SLinus Torvalds 	/*
22031da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
22041da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
22051da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
22061da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
22071da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
22081da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
22091da177e4SLinus Torvalds 	   PMTU discouvery.
22101da177e4SLinus Torvalds 	 */
2211d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2212d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2213d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2214d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2215d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2216defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2217566cfd8fSSimon Arlott 	}
22181da177e4SLinus Torvalds 	return 0;
22191da177e4SLinus Torvalds }
22201da177e4SLinus Torvalds 
222195c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
22221da177e4SLinus Torvalds {
2223c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2224c71099acSThomas Graf 		.dev = dev,
2225c71099acSThomas Graf 		.mtu = mtu,
2226c71099acSThomas Graf 	};
22271da177e4SLinus Torvalds 
2228c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
22291da177e4SLinus Torvalds }
22301da177e4SLinus Torvalds 
2231ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
22325176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
223386872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2234ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
223586872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
223686872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
223786872cb5SThomas Graf };
223886872cb5SThomas Graf 
223986872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
224086872cb5SThomas Graf 			      struct fib6_config *cfg)
22411da177e4SLinus Torvalds {
224286872cb5SThomas Graf 	struct rtmsg *rtm;
224386872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
224486872cb5SThomas Graf 	int err;
22451da177e4SLinus Torvalds 
224686872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
224786872cb5SThomas Graf 	if (err < 0)
224886872cb5SThomas Graf 		goto errout;
22491da177e4SLinus Torvalds 
225086872cb5SThomas Graf 	err = -EINVAL;
225186872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
225286872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
225386872cb5SThomas Graf 
225486872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
225586872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
225686872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
225786872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
225886872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
225986872cb5SThomas Graf 
226086872cb5SThomas Graf 	if (rtm->rtm_type == RTN_UNREACHABLE)
226186872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
226286872cb5SThomas Graf 
2263ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2264ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2265ab79ad14SMaciej Żenczykowski 
226686872cb5SThomas Graf 	cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
226786872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
22683b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
226986872cb5SThomas Graf 
227086872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
227186872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
227286872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
22731da177e4SLinus Torvalds 	}
227486872cb5SThomas Graf 
227586872cb5SThomas Graf 	if (tb[RTA_DST]) {
227686872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
227786872cb5SThomas Graf 
227886872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
227986872cb5SThomas Graf 			goto errout;
228086872cb5SThomas Graf 
228186872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
22821da177e4SLinus Torvalds 	}
228386872cb5SThomas Graf 
228486872cb5SThomas Graf 	if (tb[RTA_SRC]) {
228586872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
228686872cb5SThomas Graf 
228786872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
228886872cb5SThomas Graf 			goto errout;
228986872cb5SThomas Graf 
229086872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
22911da177e4SLinus Torvalds 	}
229286872cb5SThomas Graf 
2293c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2294c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2295c3968a85SDaniel Walter 
229686872cb5SThomas Graf 	if (tb[RTA_OIF])
229786872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
229886872cb5SThomas Graf 
229986872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
230086872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
230186872cb5SThomas Graf 
230286872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
230386872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
230486872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
23051da177e4SLinus Torvalds 	}
230686872cb5SThomas Graf 
230786872cb5SThomas Graf 	if (tb[RTA_TABLE])
230886872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
230986872cb5SThomas Graf 
231086872cb5SThomas Graf 	err = 0;
231186872cb5SThomas Graf errout:
231286872cb5SThomas Graf 	return err;
23131da177e4SLinus Torvalds }
23141da177e4SLinus Torvalds 
2315c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23161da177e4SLinus Torvalds {
231786872cb5SThomas Graf 	struct fib6_config cfg;
231886872cb5SThomas Graf 	int err;
23191da177e4SLinus Torvalds 
232086872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
232186872cb5SThomas Graf 	if (err < 0)
232286872cb5SThomas Graf 		return err;
232386872cb5SThomas Graf 
232486872cb5SThomas Graf 	return ip6_route_del(&cfg);
23251da177e4SLinus Torvalds }
23261da177e4SLinus Torvalds 
2327c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
23281da177e4SLinus Torvalds {
232986872cb5SThomas Graf 	struct fib6_config cfg;
233086872cb5SThomas Graf 	int err;
23311da177e4SLinus Torvalds 
233286872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
233386872cb5SThomas Graf 	if (err < 0)
233486872cb5SThomas Graf 		return err;
233586872cb5SThomas Graf 
233686872cb5SThomas Graf 	return ip6_route_add(&cfg);
23371da177e4SLinus Torvalds }
23381da177e4SLinus Torvalds 
2339339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2340339bf98fSThomas Graf {
2341339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2342339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2343339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2344339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2345339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2346339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2347339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2348339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2349339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
23506a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2351339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2352339bf98fSThomas Graf }
2353339bf98fSThomas Graf 
2354191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2355191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
23560d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
23570d51aa80SJamal Hadi Salim 			 int iif, int type, u32 pid, u32 seq,
23587bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
23591da177e4SLinus Torvalds {
23601da177e4SLinus Torvalds 	struct rtmsg *rtm;
23611da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2362e3703b3dSThomas Graf 	long expires;
23639e762a4aSPatrick McHardy 	u32 table;
2364f2c31e32SEric Dumazet 	struct neighbour *n;
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
23671da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
23681da177e4SLinus Torvalds 			/* success since this is not a prefix route */
23691da177e4SLinus Torvalds 			return 1;
23701da177e4SLinus Torvalds 		}
23711da177e4SLinus Torvalds 	}
23721da177e4SLinus Torvalds 
23732d7202bfSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
237438308473SDavid S. Miller 	if (!nlh)
237526932566SPatrick McHardy 		return -EMSGSIZE;
23762d7202bfSThomas Graf 
23772d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
23781da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
23791da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
23801da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
23811da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2382c71099acSThomas Graf 	if (rt->rt6i_table)
23839e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2384c71099acSThomas Graf 	else
23859e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
23869e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2387c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2388c78679e8SDavid S. Miller 		goto nla_put_failure;
23891da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_REJECT)
23901da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNREACHABLE;
2391ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2392ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2393d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
23941da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
23951da177e4SLinus Torvalds 	else
23961da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
23971da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
23981da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
23991da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
24001da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
24011da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
24021da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_ADDRCONF)
24031da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_KERNEL;
24041da177e4SLinus Torvalds 	else if (rt->rt6i_flags & RTF_DEFAULT)
24051da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_RA;
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
24081da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds 	if (dst) {
2411c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2412c78679e8SDavid S. Miller 			goto nla_put_failure;
24131da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
24141da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2415c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2416c78679e8SDavid S. Miller 			goto nla_put_failure;
24171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
24181da177e4SLinus Torvalds 	if (src) {
2419c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2420c78679e8SDavid S. Miller 			goto nla_put_failure;
24211da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2422c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2423c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2424c78679e8SDavid S. Miller 		goto nla_put_failure;
24251da177e4SLinus Torvalds #endif
24267bc570c8SYOSHIFUJI Hideaki 	if (iif) {
24277bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
24287bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
24298229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
24307bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
24317bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
24327bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
24337bc570c8SYOSHIFUJI Hideaki 						return 0;
24347bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
24357bc570c8SYOSHIFUJI Hideaki 				} else {
24367bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
24377bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
24387bc570c8SYOSHIFUJI Hideaki 				}
24397bc570c8SYOSHIFUJI Hideaki 			}
24407bc570c8SYOSHIFUJI Hideaki 		} else
24417bc570c8SYOSHIFUJI Hideaki #endif
2442c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2443c78679e8SDavid S. Miller 				goto nla_put_failure;
24447bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
24451da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2446c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2447c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2448c78679e8SDavid S. Miller 			goto nla_put_failure;
2449c3968a85SDaniel Walter 	}
2450c3968a85SDaniel Walter 
2451c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2452c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
24534e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2454c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2455c78679e8SDavid S. Miller 			goto nla_put_failure;
24561da177e4SLinus Torvalds 	}
24572d7202bfSThomas Graf 
2458defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
24592d7202bfSThomas Graf 		goto nla_put_failure;
24602d7202bfSThomas Graf 
2461f2c31e32SEric Dumazet 	rcu_read_lock();
246297cac082SDavid S. Miller 	n = rt->n;
246394f826b8SEric Dumazet 	if (n) {
246494f826b8SEric Dumazet 		if (nla_put(skb, RTA_GATEWAY, 16, &n->primary_key) < 0) {
246594f826b8SEric Dumazet 			rcu_read_unlock();
246694f826b8SEric Dumazet 			goto nla_put_failure;
246794f826b8SEric Dumazet 		}
246894f826b8SEric Dumazet 	}
2469f2c31e32SEric Dumazet 	rcu_read_unlock();
24702d7202bfSThomas Graf 
2471c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2472c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2473c78679e8SDavid S. Miller 		goto nla_put_failure;
2474c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2475c78679e8SDavid S. Miller 		goto nla_put_failure;
247636e3deaeSYOSHIFUJI Hideaki 	if (!(rt->rt6i_flags & RTF_EXPIRES))
247736e3deaeSYOSHIFUJI Hideaki 		expires = 0;
2478d1918542SDavid S. Miller 	else if (rt->dst.expires - jiffies < INT_MAX)
2479d1918542SDavid S. Miller 		expires = rt->dst.expires - jiffies;
248036e3deaeSYOSHIFUJI Hideaki 	else
248136e3deaeSYOSHIFUJI Hideaki 		expires = INT_MAX;
248269cdf8f9SYOSHIFUJI Hideaki 
248387a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2484e3703b3dSThomas Graf 		goto nla_put_failure;
24851da177e4SLinus Torvalds 
24862d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
24872d7202bfSThomas Graf 
24882d7202bfSThomas Graf nla_put_failure:
248926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
249026932566SPatrick McHardy 	return -EMSGSIZE;
24911da177e4SLinus Torvalds }
24921da177e4SLinus Torvalds 
24931b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
24941da177e4SLinus Torvalds {
24951da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
24961da177e4SLinus Torvalds 	int prefix;
24971da177e4SLinus Torvalds 
24982d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
24992d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
25001da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
25011da177e4SLinus Torvalds 	} else
25021da177e4SLinus Torvalds 		prefix = 0;
25031da177e4SLinus Torvalds 
2504191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2505191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
25061da177e4SLinus Torvalds 		     NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
25077bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
25081da177e4SLinus Torvalds }
25091da177e4SLinus Torvalds 
2510c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
25111da177e4SLinus Torvalds {
25123b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2513ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
25141da177e4SLinus Torvalds 	struct rt6_info *rt;
2515ab364a6fSThomas Graf 	struct sk_buff *skb;
2516ab364a6fSThomas Graf 	struct rtmsg *rtm;
25174c9483b2SDavid S. Miller 	struct flowi6 fl6;
251872331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2519ab364a6fSThomas Graf 
2520ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2521ab364a6fSThomas Graf 	if (err < 0)
2522ab364a6fSThomas Graf 		goto errout;
2523ab364a6fSThomas Graf 
2524ab364a6fSThomas Graf 	err = -EINVAL;
25254c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2526ab364a6fSThomas Graf 
2527ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2528ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2529ab364a6fSThomas Graf 			goto errout;
2530ab364a6fSThomas Graf 
25314e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2532ab364a6fSThomas Graf 	}
2533ab364a6fSThomas Graf 
2534ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2535ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2536ab364a6fSThomas Graf 			goto errout;
2537ab364a6fSThomas Graf 
25384e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2539ab364a6fSThomas Graf 	}
2540ab364a6fSThomas Graf 
2541ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2542ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2543ab364a6fSThomas Graf 
2544ab364a6fSThomas Graf 	if (tb[RTA_OIF])
254572331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2546ab364a6fSThomas Graf 
2547ab364a6fSThomas Graf 	if (iif) {
2548ab364a6fSThomas Graf 		struct net_device *dev;
254972331bc0SShmulik Ladkani 		int flags = 0;
255072331bc0SShmulik Ladkani 
25515578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2552ab364a6fSThomas Graf 		if (!dev) {
2553ab364a6fSThomas Graf 			err = -ENODEV;
2554ab364a6fSThomas Graf 			goto errout;
2555ab364a6fSThomas Graf 		}
255672331bc0SShmulik Ladkani 
255772331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
255872331bc0SShmulik Ladkani 
255972331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
256072331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
256172331bc0SShmulik Ladkani 
256272331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
256372331bc0SShmulik Ladkani 							       flags);
256472331bc0SShmulik Ladkani 	} else {
256572331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
256672331bc0SShmulik Ladkani 
256772331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2568ab364a6fSThomas Graf 	}
25691da177e4SLinus Torvalds 
25701da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
257138308473SDavid S. Miller 	if (!skb) {
25722173bff5SShmulik Ladkani 		dst_release(&rt->dst);
2573ab364a6fSThomas Graf 		err = -ENOBUFS;
2574ab364a6fSThomas Graf 		goto errout;
2575ab364a6fSThomas Graf 	}
25761da177e4SLinus Torvalds 
25771da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
25781da177e4SLinus Torvalds 	   through good chunk of routing engine.
25791da177e4SLinus Torvalds 	 */
2580459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
25811da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
25821da177e4SLinus Torvalds 
2583d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
25841da177e4SLinus Torvalds 
25854c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
25861da177e4SLinus Torvalds 			    RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
25877bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
25881da177e4SLinus Torvalds 	if (err < 0) {
2589ab364a6fSThomas Graf 		kfree_skb(skb);
2590ab364a6fSThomas Graf 		goto errout;
25911da177e4SLinus Torvalds 	}
25921da177e4SLinus Torvalds 
25935578689aSDaniel Lezcano 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2594ab364a6fSThomas Graf errout:
25951da177e4SLinus Torvalds 	return err;
25961da177e4SLinus Torvalds }
25971da177e4SLinus Torvalds 
259886872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
25991da177e4SLinus Torvalds {
26001da177e4SLinus Torvalds 	struct sk_buff *skb;
26015578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2602528c4cebSDenis V. Lunev 	u32 seq;
2603528c4cebSDenis V. Lunev 	int err;
26040d51aa80SJamal Hadi Salim 
2605528c4cebSDenis V. Lunev 	err = -ENOBUFS;
260638308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
260786872cb5SThomas Graf 
2608339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
260938308473SDavid S. Miller 	if (!skb)
261021713ebcSThomas Graf 		goto errout;
26111da177e4SLinus Torvalds 
2612191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
26137bc570c8SYOSHIFUJI Hideaki 				event, info->pid, seq, 0, 0, 0);
261426932566SPatrick McHardy 	if (err < 0) {
261526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
261626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
261726932566SPatrick McHardy 		kfree_skb(skb);
261826932566SPatrick McHardy 		goto errout;
261926932566SPatrick McHardy 	}
26201ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
26215578689aSDaniel Lezcano 		    info->nlh, gfp_any());
26221ce85fe4SPablo Neira Ayuso 	return;
262321713ebcSThomas Graf errout:
262421713ebcSThomas Graf 	if (err < 0)
26255578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
26261da177e4SLinus Torvalds }
26271da177e4SLinus Torvalds 
26288ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
26298ed67789SDaniel Lezcano 				unsigned long event, void *data)
26308ed67789SDaniel Lezcano {
26318ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2632c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26338ed67789SDaniel Lezcano 
26348ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2635d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
26368ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
26378ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2638d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
26398ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2640d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
26418ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
26428ed67789SDaniel Lezcano #endif
26438ed67789SDaniel Lezcano 	}
26448ed67789SDaniel Lezcano 
26458ed67789SDaniel Lezcano 	return NOTIFY_OK;
26468ed67789SDaniel Lezcano }
26478ed67789SDaniel Lezcano 
26481da177e4SLinus Torvalds /*
26491da177e4SLinus Torvalds  *	/proc
26501da177e4SLinus Torvalds  */
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds struct rt6_proc_arg
26551da177e4SLinus Torvalds {
26561da177e4SLinus Torvalds 	char *buffer;
26571da177e4SLinus Torvalds 	int offset;
26581da177e4SLinus Torvalds 	int length;
26591da177e4SLinus Torvalds 	int skip;
26601da177e4SLinus Torvalds 	int len;
26611da177e4SLinus Torvalds };
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
26641da177e4SLinus Torvalds {
266533120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
266669cce1d1SDavid S. Miller 	struct neighbour *n;
26671da177e4SLinus Torvalds 
26684b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
26691da177e4SLinus Torvalds 
26701da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
26714b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
26721da177e4SLinus Torvalds #else
267333120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
26741da177e4SLinus Torvalds #endif
2675f2c31e32SEric Dumazet 	rcu_read_lock();
267697cac082SDavid S. Miller 	n = rt->n;
267769cce1d1SDavid S. Miller 	if (n) {
267869cce1d1SDavid S. Miller 		seq_printf(m, "%pi6", n->primary_key);
26791da177e4SLinus Torvalds 	} else {
268033120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
26811da177e4SLinus Torvalds 	}
2682f2c31e32SEric Dumazet 	rcu_read_unlock();
268333120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2684d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2685d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
2686d1918542SDavid S. Miller 		   rt->dst.dev ? rt->dst.dev->name : "");
26871da177e4SLinus Torvalds 	return 0;
26881da177e4SLinus Torvalds }
26891da177e4SLinus Torvalds 
269033120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
26911da177e4SLinus Torvalds {
2692f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
269332b293a5SJosh Hunt 	fib6_clean_all_ro(net, rt6_info_route, 0, m);
269433120b30SAlexey Dobriyan 	return 0;
26951da177e4SLinus Torvalds }
26961da177e4SLinus Torvalds 
269733120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
269833120b30SAlexey Dobriyan {
2699de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2700f3db4851SDaniel Lezcano }
2701f3db4851SDaniel Lezcano 
270233120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
270333120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
270433120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
270533120b30SAlexey Dobriyan 	.read		= seq_read,
270633120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2707b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
270833120b30SAlexey Dobriyan };
270933120b30SAlexey Dobriyan 
27101da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
27111da177e4SLinus Torvalds {
271269ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
27131da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
271469ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
271569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
271669ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
271769ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
271869ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2719fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
272069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
27211da177e4SLinus Torvalds 
27221da177e4SLinus Torvalds 	return 0;
27231da177e4SLinus Torvalds }
27241da177e4SLinus Torvalds 
27251da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
27261da177e4SLinus Torvalds {
2727de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
272869ddb805SDaniel Lezcano }
272969ddb805SDaniel Lezcano 
27309a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
27311da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
27321da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
27331da177e4SLinus Torvalds 	.read	 = seq_read,
27341da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2735b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
27361da177e4SLinus Torvalds };
27371da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
27381da177e4SLinus Torvalds 
27391da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
27401da177e4SLinus Torvalds 
27411da177e4SLinus Torvalds static
27428d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
27431da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
27441da177e4SLinus Torvalds {
2745c486da34SLucian Adrian Grijincu 	struct net *net;
2746c486da34SLucian Adrian Grijincu 	int delay;
2747c486da34SLucian Adrian Grijincu 	if (!write)
2748c486da34SLucian Adrian Grijincu 		return -EINVAL;
2749c486da34SLucian Adrian Grijincu 
2750c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2751c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
27528d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
27535b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
27541da177e4SLinus Torvalds 	return 0;
27551da177e4SLinus Torvalds }
27561da177e4SLinus Torvalds 
2757760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
27581da177e4SLinus Torvalds 	{
27591da177e4SLinus Torvalds 		.procname	=	"flush",
27604990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
27611da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
276289c8b3a1SDave Jones 		.mode		=	0200,
27636d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
27641da177e4SLinus Torvalds 	},
27651da177e4SLinus Torvalds 	{
27661da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
27679a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
27681da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27691da177e4SLinus Torvalds 		.mode		=	0644,
27706d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27711da177e4SLinus Torvalds 	},
27721da177e4SLinus Torvalds 	{
27731da177e4SLinus Torvalds 		.procname	=	"max_size",
27744990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
27751da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27761da177e4SLinus Torvalds 		.mode		=	0644,
27776d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
27781da177e4SLinus Torvalds 	},
27791da177e4SLinus Torvalds 	{
27801da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
27814990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
27821da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27831da177e4SLinus Torvalds 		.mode		=	0644,
27846d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27851da177e4SLinus Torvalds 	},
27861da177e4SLinus Torvalds 	{
27871da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
27884990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
27891da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27901da177e4SLinus Torvalds 		.mode		=	0644,
27916d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27921da177e4SLinus Torvalds 	},
27931da177e4SLinus Torvalds 	{
27941da177e4SLinus Torvalds 		.procname	=	"gc_interval",
27954990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
27961da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
27971da177e4SLinus Torvalds 		.mode		=	0644,
27986d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
27991da177e4SLinus Torvalds 	},
28001da177e4SLinus Torvalds 	{
28011da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
28024990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
28031da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28041da177e4SLinus Torvalds 		.mode		=	0644,
2805f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28061da177e4SLinus Torvalds 	},
28071da177e4SLinus Torvalds 	{
28081da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
28094990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
28101da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28111da177e4SLinus Torvalds 		.mode		=	0644,
28126d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
28131da177e4SLinus Torvalds 	},
28141da177e4SLinus Torvalds 	{
28151da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
28164990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
28171da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28181da177e4SLinus Torvalds 		.mode		=	0644,
2819f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
28201da177e4SLinus Torvalds 	},
28211da177e4SLinus Torvalds 	{
28221da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
28234990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
28241da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
28251da177e4SLinus Torvalds 		.mode		=	0644,
28266d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
28271da177e4SLinus Torvalds 	},
2828f8572d8fSEric W. Biederman 	{ }
28291da177e4SLinus Torvalds };
28301da177e4SLinus Torvalds 
28312c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2832760f2d01SDaniel Lezcano {
2833760f2d01SDaniel Lezcano 	struct ctl_table *table;
2834760f2d01SDaniel Lezcano 
2835760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2836760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2837760f2d01SDaniel Lezcano 			GFP_KERNEL);
28385ee09105SYOSHIFUJI Hideaki 
28395ee09105SYOSHIFUJI Hideaki 	if (table) {
28405ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2841c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
284286393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
28435ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
28445ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28455ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
28465ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
28475ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
28485ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
28495ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
28509c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
28515ee09105SYOSHIFUJI Hideaki 	}
28525ee09105SYOSHIFUJI Hideaki 
2853760f2d01SDaniel Lezcano 	return table;
2854760f2d01SDaniel Lezcano }
28551da177e4SLinus Torvalds #endif
28561da177e4SLinus Torvalds 
28572c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
2858cdb18761SDaniel Lezcano {
2859633d424bSPavel Emelyanov 	int ret = -ENOMEM;
28608ed67789SDaniel Lezcano 
286186393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
286286393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
2863f2fc6a54SBenjamin Thery 
2864fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2865fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
2866fc66f95cSEric Dumazet 
28678ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
28688ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
28698ed67789SDaniel Lezcano 					   GFP_KERNEL);
28708ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
2871fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
2872d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
28738ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
2874d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
287562fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
287662fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28778ed67789SDaniel Lezcano 
28788ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
28798ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
28808ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
28818ed67789SDaniel Lezcano 					       GFP_KERNEL);
288268fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
288368fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
2884d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
28858ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2886d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
288762fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
288862fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
28898ed67789SDaniel Lezcano 
28908ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
28918ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
28928ed67789SDaniel Lezcano 					       GFP_KERNEL);
289368fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
289468fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
2895d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
28968ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2897d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
289862fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
289962fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
29008ed67789SDaniel Lezcano #endif
29018ed67789SDaniel Lezcano 
2902b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
2903b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
2904b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2905b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2906b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2907b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2908b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2909b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2910b339a47cSPeter Zijlstra 
29116891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
29126891a346SBenjamin Thery 
29138ed67789SDaniel Lezcano 	ret = 0;
29148ed67789SDaniel Lezcano out:
29158ed67789SDaniel Lezcano 	return ret;
2916f2fc6a54SBenjamin Thery 
291768fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
291868fffc67SPeter Zijlstra out_ip6_prohibit_entry:
291968fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
292068fffc67SPeter Zijlstra out_ip6_null_entry:
292168fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
292268fffc67SPeter Zijlstra #endif
2923fc66f95cSEric Dumazet out_ip6_dst_entries:
2924fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2925f2fc6a54SBenjamin Thery out_ip6_dst_ops:
2926f2fc6a54SBenjamin Thery 	goto out;
2927cdb18761SDaniel Lezcano }
2928cdb18761SDaniel Lezcano 
29292c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
2930cdb18761SDaniel Lezcano {
29318ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
29328ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
29338ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
29348ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
29358ed67789SDaniel Lezcano #endif
293641bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2937cdb18761SDaniel Lezcano }
2938cdb18761SDaniel Lezcano 
2939d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
2940d189634eSThomas Graf {
2941d189634eSThomas Graf #ifdef CONFIG_PROC_FS
2942d189634eSThomas Graf 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2943d189634eSThomas Graf 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2944d189634eSThomas Graf #endif
2945d189634eSThomas Graf 	return 0;
2946d189634eSThomas Graf }
2947d189634eSThomas Graf 
2948d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
2949d189634eSThomas Graf {
2950d189634eSThomas Graf #ifdef CONFIG_PROC_FS
2951d189634eSThomas Graf 	proc_net_remove(net, "ipv6_route");
2952d189634eSThomas Graf 	proc_net_remove(net, "rt6_stats");
2953d189634eSThomas Graf #endif
2954d189634eSThomas Graf }
2955d189634eSThomas Graf 
2956cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
2957cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
2958cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
2959cdb18761SDaniel Lezcano };
2960cdb18761SDaniel Lezcano 
2961c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
2962c3426b47SDavid S. Miller {
2963c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
2964c3426b47SDavid S. Miller 
2965c3426b47SDavid S. Miller 	if (!bp)
2966c3426b47SDavid S. Miller 		return -ENOMEM;
2967c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
2968c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
2969c3426b47SDavid S. Miller 	return 0;
2970c3426b47SDavid S. Miller }
2971c3426b47SDavid S. Miller 
2972c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
2973c3426b47SDavid S. Miller {
2974c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
2975c3426b47SDavid S. Miller 
2976c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
297756a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
2978c3426b47SDavid S. Miller 	kfree(bp);
2979c3426b47SDavid S. Miller }
2980c3426b47SDavid S. Miller 
29812b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
2982c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
2983c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
2984c3426b47SDavid S. Miller };
2985c3426b47SDavid S. Miller 
2986d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
2987d189634eSThomas Graf 	.init = ip6_route_net_init_late,
2988d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
2989d189634eSThomas Graf };
2990d189634eSThomas Graf 
29918ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
29928ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
29938ed67789SDaniel Lezcano 	.priority = 0,
29948ed67789SDaniel Lezcano };
29958ed67789SDaniel Lezcano 
2996433d49c3SDaniel Lezcano int __init ip6_route_init(void)
29971da177e4SLinus Torvalds {
2998433d49c3SDaniel Lezcano 	int ret;
2999433d49c3SDaniel Lezcano 
30009a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
30019a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
30029a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
30039a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
30049a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3005c19a28e1SFernando Carrijo 		goto out;
300614e50e57SDavid S. Miller 
3007fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
30088ed67789SDaniel Lezcano 	if (ret)
3009bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3010bdb3289fSDaniel Lezcano 
3011c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3012c3426b47SDavid S. Miller 	if (ret)
3013e8803b6cSDavid S. Miller 		goto out_dst_entries;
30142a0c451aSThomas Graf 
30157e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
30167e52b33bSDavid S. Miller 	if (ret)
30177e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3018c3426b47SDavid S. Miller 
30195dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
30205dc121e9SArnaud Ebalard 
30218ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
30228ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
30238ed67789SDaniel Lezcano 	 * manually for init_net */
3024d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
30258ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3026bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3027d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
30288ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3029d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
30308ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3031bdb3289fSDaniel Lezcano   #endif
3032e8803b6cSDavid S. Miller 	ret = fib6_init();
3033433d49c3SDaniel Lezcano 	if (ret)
30348ed67789SDaniel Lezcano 		goto out_register_subsys;
3035433d49c3SDaniel Lezcano 
3036433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3037433d49c3SDaniel Lezcano 	if (ret)
3038e8803b6cSDavid S. Miller 		goto out_fib6_init;
3039c35b7e72SDaniel Lezcano 
3040433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3041433d49c3SDaniel Lezcano 	if (ret)
3042433d49c3SDaniel Lezcano 		goto xfrm6_init;
30437e5449c2SDaniel Lezcano 
3044d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3045d189634eSThomas Graf 	if (ret)
3046d189634eSThomas Graf 		goto fib6_rules_init;
3047d189634eSThomas Graf 
3048433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3049c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3050c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3051c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3052d189634eSThomas Graf 		goto out_register_late_subsys;
3053433d49c3SDaniel Lezcano 
30548ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3055cdb18761SDaniel Lezcano 	if (ret)
3056d189634eSThomas Graf 		goto out_register_late_subsys;
30578ed67789SDaniel Lezcano 
3058433d49c3SDaniel Lezcano out:
3059433d49c3SDaniel Lezcano 	return ret;
3060433d49c3SDaniel Lezcano 
3061d189634eSThomas Graf out_register_late_subsys:
3062d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3063433d49c3SDaniel Lezcano fib6_rules_init:
3064433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3065433d49c3SDaniel Lezcano xfrm6_init:
3066433d49c3SDaniel Lezcano 	xfrm6_fini();
30672a0c451aSThomas Graf out_fib6_init:
30682a0c451aSThomas Graf 	fib6_gc_cleanup();
30698ed67789SDaniel Lezcano out_register_subsys:
30708ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
30717e52b33bSDavid S. Miller out_register_inetpeer:
30727e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3073fc66f95cSEric Dumazet out_dst_entries:
3074fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3075433d49c3SDaniel Lezcano out_kmem_cache:
3076f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3077433d49c3SDaniel Lezcano 	goto out;
30781da177e4SLinus Torvalds }
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds void ip6_route_cleanup(void)
30811da177e4SLinus Torvalds {
30828ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3083d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3084101367c2SThomas Graf 	fib6_rules_cleanup();
30851da177e4SLinus Torvalds 	xfrm6_fini();
30861da177e4SLinus Torvalds 	fib6_gc_cleanup();
3087c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
30888ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
308941bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3090f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
30911da177e4SLinus Torvalds }
3092