xref: /openbmc/linux/net/ipv6/route.c (revision b3ce5ae1fb6ba45c70e7c4d144182d38f0b0aef7)
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>
6051ebd318SNicolas Dichtel #include <net/nexthop.h>
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #include <asm/uaccess.h>
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
651da177e4SLinus Torvalds #include <linux/sysctl.h>
661da177e4SLinus Torvalds #endif
671da177e4SLinus Torvalds 
681716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
6921efcfa0SEric Dumazet 				    const struct in6_addr *dest);
701da177e4SLinus Torvalds static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie);
710dbaee3bSDavid S. Miller static unsigned int	 ip6_default_advmss(const struct dst_entry *dst);
72ebb762f2SSteffen Klassert static unsigned int	 ip6_mtu(const struct dst_entry *dst);
731da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *);
741da177e4SLinus Torvalds static void		ip6_dst_destroy(struct dst_entry *);
751da177e4SLinus Torvalds static void		ip6_dst_ifdown(struct dst_entry *,
761da177e4SLinus Torvalds 				       struct net_device *dev, int how);
77569d3645SDaniel Lezcano static int		 ip6_dst_gc(struct dst_ops *ops);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static int		ip6_pkt_discard(struct sk_buff *skb);
801da177e4SLinus Torvalds static int		ip6_pkt_discard_out(struct sk_buff *skb);
811da177e4SLinus Torvalds static void		ip6_link_failure(struct sk_buff *skb);
826700c270SDavid S. Miller static void		ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
836700c270SDavid S. Miller 					   struct sk_buff *skb, u32 mtu);
846700c270SDavid S. Miller static void		rt6_do_redirect(struct dst_entry *dst, struct sock *sk,
856700c270SDavid S. Miller 					struct sk_buff *skb);
861da177e4SLinus Torvalds 
8770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
88efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
89b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
90b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
9195c96174SEric Dumazet 					   unsigned int pref);
92efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
93b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
94b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex);
9570ceb4f5SYOSHIFUJI Hideaki #endif
9670ceb4f5SYOSHIFUJI Hideaki 
9706582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
9806582540SDavid S. Miller {
9906582540SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
10006582540SDavid S. Miller 	struct inet_peer *peer;
10106582540SDavid S. Miller 	u32 *p = NULL;
10206582540SDavid S. Miller 
1038e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
1048e2ec639SYan, Zheng 		return NULL;
1058e2ec639SYan, Zheng 
106fbfe95a4SDavid S. Miller 	peer = rt6_get_peer_create(rt);
10706582540SDavid S. Miller 	if (peer) {
10806582540SDavid S. Miller 		u32 *old_p = __DST_METRICS_PTR(old);
10906582540SDavid S. Miller 		unsigned long prev, new;
11006582540SDavid S. Miller 
11106582540SDavid S. Miller 		p = peer->metrics;
11206582540SDavid S. Miller 		if (inet_metrics_new(peer))
11306582540SDavid S. Miller 			memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
11406582540SDavid S. Miller 
11506582540SDavid S. Miller 		new = (unsigned long) p;
11606582540SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
11706582540SDavid S. Miller 
11806582540SDavid S. Miller 		if (prev != old) {
11906582540SDavid S. Miller 			p = __DST_METRICS_PTR(prev);
12006582540SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
12106582540SDavid S. Miller 				p = NULL;
12206582540SDavid S. Miller 		}
12306582540SDavid S. Miller 	}
12406582540SDavid S. Miller 	return p;
12506582540SDavid S. Miller }
12606582540SDavid S. Miller 
127f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt,
128f894cbf8SDavid S. Miller 					     struct sk_buff *skb,
129f894cbf8SDavid S. Miller 					     const void *daddr)
13039232973SDavid S. Miller {
13139232973SDavid S. Miller 	struct in6_addr *p = &rt->rt6i_gateway;
13239232973SDavid S. Miller 
133a7563f34SDavid S. Miller 	if (!ipv6_addr_any(p))
13439232973SDavid S. Miller 		return (const void *) p;
135f894cbf8SDavid S. Miller 	else if (skb)
136f894cbf8SDavid S. Miller 		return &ipv6_hdr(skb)->daddr;
13739232973SDavid S. Miller 	return daddr;
13839232973SDavid S. Miller }
13939232973SDavid S. Miller 
140f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
141f894cbf8SDavid S. Miller 					  struct sk_buff *skb,
142f894cbf8SDavid S. Miller 					  const void *daddr)
143d3aaeb38SDavid S. Miller {
14439232973SDavid S. Miller 	struct rt6_info *rt = (struct rt6_info *) dst;
14539232973SDavid S. Miller 	struct neighbour *n;
14639232973SDavid S. Miller 
147f894cbf8SDavid S. Miller 	daddr = choose_neigh_daddr(rt, skb, daddr);
14839232973SDavid S. Miller 	n = __ipv6_neigh_lookup(&nd_tbl, dst->dev, daddr);
149f83c7790SDavid S. Miller 	if (n)
150f83c7790SDavid S. Miller 		return n;
151f83c7790SDavid S. Miller 	return neigh_create(&nd_tbl, daddr, dst->dev);
152f83c7790SDavid S. Miller }
153f83c7790SDavid S. Miller 
1548ade06c6SDavid S. Miller static int rt6_bind_neighbour(struct rt6_info *rt, struct net_device *dev)
155f83c7790SDavid S. Miller {
1568ade06c6SDavid S. Miller 	struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dev, &rt->rt6i_gateway);
1578ade06c6SDavid S. Miller 	if (!n) {
1588ade06c6SDavid S. Miller 		n = neigh_create(&nd_tbl, &rt->rt6i_gateway, dev);
159f83c7790SDavid S. Miller 		if (IS_ERR(n))
160f83c7790SDavid S. Miller 			return PTR_ERR(n);
1618ade06c6SDavid S. Miller 	}
16297cac082SDavid S. Miller 	rt->n = n;
163f83c7790SDavid S. Miller 
164f83c7790SDavid S. Miller 	return 0;
165d3aaeb38SDavid S. Miller }
166d3aaeb38SDavid S. Miller 
1679a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = {
1681da177e4SLinus Torvalds 	.family			=	AF_INET6,
16909640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
1701da177e4SLinus Torvalds 	.gc			=	ip6_dst_gc,
1711da177e4SLinus Torvalds 	.gc_thresh		=	1024,
1721da177e4SLinus Torvalds 	.check			=	ip6_dst_check,
1730dbaee3bSDavid S. Miller 	.default_advmss		=	ip6_default_advmss,
174ebb762f2SSteffen Klassert 	.mtu			=	ip6_mtu,
17506582540SDavid S. Miller 	.cow_metrics		=	ipv6_cow_metrics,
1761da177e4SLinus Torvalds 	.destroy		=	ip6_dst_destroy,
1771da177e4SLinus Torvalds 	.ifdown			=	ip6_dst_ifdown,
1781da177e4SLinus Torvalds 	.negative_advice	=	ip6_negative_advice,
1791da177e4SLinus Torvalds 	.link_failure		=	ip6_link_failure,
1801da177e4SLinus Torvalds 	.update_pmtu		=	ip6_rt_update_pmtu,
1816e157b6aSDavid S. Miller 	.redirect		=	rt6_do_redirect,
1821ac06e03SHerbert Xu 	.local_out		=	__ip6_local_out,
183d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
1841da177e4SLinus Torvalds };
1851da177e4SLinus Torvalds 
186ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst)
187ec831ea7SRoland Dreier {
188618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
189618f9bc7SSteffen Klassert 
190618f9bc7SSteffen Klassert 	return mtu ? : dst->dev->mtu;
191ec831ea7SRoland Dreier }
192ec831ea7SRoland Dreier 
1936700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
1946700c270SDavid S. Miller 					 struct sk_buff *skb, u32 mtu)
19514e50e57SDavid S. Miller {
19614e50e57SDavid S. Miller }
19714e50e57SDavid S. Miller 
1986700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
1996700c270SDavid S. Miller 				      struct sk_buff *skb)
200b587ee3bSDavid S. Miller {
201b587ee3bSDavid S. Miller }
202b587ee3bSDavid S. Miller 
2030972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
2040972ddb2SHeld Bernhard 					 unsigned long old)
2050972ddb2SHeld Bernhard {
2060972ddb2SHeld Bernhard 	return NULL;
2070972ddb2SHeld Bernhard }
2080972ddb2SHeld Bernhard 
20914e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = {
21014e50e57SDavid S. Miller 	.family			=	AF_INET6,
21109640e63SHarvey Harrison 	.protocol		=	cpu_to_be16(ETH_P_IPV6),
21214e50e57SDavid S. Miller 	.destroy		=	ip6_dst_destroy,
21314e50e57SDavid S. Miller 	.check			=	ip6_dst_check,
214ebb762f2SSteffen Klassert 	.mtu			=	ip6_blackhole_mtu,
215214f45c9SEric Dumazet 	.default_advmss		=	ip6_default_advmss,
21614e50e57SDavid S. Miller 	.update_pmtu		=	ip6_rt_blackhole_update_pmtu,
217b587ee3bSDavid S. Miller 	.redirect		=	ip6_rt_blackhole_redirect,
2180972ddb2SHeld Bernhard 	.cow_metrics		=	ip6_rt_blackhole_cow_metrics,
219d3aaeb38SDavid S. Miller 	.neigh_lookup		=	ip6_neigh_lookup,
22014e50e57SDavid S. Miller };
22114e50e57SDavid S. Miller 
22262fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = {
22362fa8a84SDavid S. Miller 	[RTAX_HOPLIMIT - 1] = 255,
22462fa8a84SDavid S. Miller };
22562fa8a84SDavid S. Miller 
226fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = {
2271da177e4SLinus Torvalds 	.dst = {
2281da177e4SLinus Torvalds 		.__refcnt	= ATOMIC_INIT(1),
2291da177e4SLinus Torvalds 		.__use		= 1,
2302c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
2311da177e4SLinus Torvalds 		.error		= -ENETUNREACH,
2321da177e4SLinus Torvalds 		.input		= ip6_pkt_discard,
2331da177e4SLinus Torvalds 		.output		= ip6_pkt_discard_out,
2341da177e4SLinus Torvalds 	},
2351da177e4SLinus Torvalds 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2364f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
2371da177e4SLinus Torvalds 	.rt6i_metric	= ~(u32) 0,
2381da177e4SLinus Torvalds 	.rt6i_ref	= ATOMIC_INIT(1),
2391da177e4SLinus Torvalds };
2401da177e4SLinus Torvalds 
241101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES
242101367c2SThomas Graf 
2436723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb);
2446723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb);
2456723ab54SDavid S. Miller 
246fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = {
247101367c2SThomas Graf 	.dst = {
248101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
249101367c2SThomas Graf 		.__use		= 1,
2502c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
251101367c2SThomas Graf 		.error		= -EACCES,
2529ce8ade0SThomas Graf 		.input		= ip6_pkt_prohibit,
2539ce8ade0SThomas Graf 		.output		= ip6_pkt_prohibit_out,
254101367c2SThomas Graf 	},
255101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2564f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
257101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
258101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
259101367c2SThomas Graf };
260101367c2SThomas Graf 
261fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = {
262101367c2SThomas Graf 	.dst = {
263101367c2SThomas Graf 		.__refcnt	= ATOMIC_INIT(1),
264101367c2SThomas Graf 		.__use		= 1,
2652c20cbd7SNicolas Dichtel 		.obsolete	= DST_OBSOLETE_FORCE_CHK,
266101367c2SThomas Graf 		.error		= -EINVAL,
267352e512cSHerbert Xu 		.input		= dst_discard,
268352e512cSHerbert Xu 		.output		= dst_discard,
269101367c2SThomas Graf 	},
270101367c2SThomas Graf 	.rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP),
2714f724279SJean-Mickael Guerin 	.rt6i_protocol  = RTPROT_KERNEL,
272101367c2SThomas Graf 	.rt6i_metric	= ~(u32) 0,
273101367c2SThomas Graf 	.rt6i_ref	= ATOMIC_INIT(1),
274101367c2SThomas Graf };
275101367c2SThomas Graf 
276101367c2SThomas Graf #endif
277101367c2SThomas Graf 
2781da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */
27997bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net,
280957c665fSDavid S. Miller 					     struct net_device *dev,
2818b96d22dSDavid S. Miller 					     int flags,
2828b96d22dSDavid S. Miller 					     struct fib6_table *table)
2831da177e4SLinus Torvalds {
28497bab73fSDavid S. Miller 	struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev,
2856f3118b5SNicolas Dichtel 					0, DST_OBSOLETE_FORCE_CHK, flags);
286cf911662SDavid S. Miller 
28797bab73fSDavid S. Miller 	if (rt) {
2888104891bSSteffen Klassert 		struct dst_entry *dst = &rt->dst;
2898104891bSSteffen Klassert 
2908104891bSSteffen Klassert 		memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst));
2918b96d22dSDavid S. Miller 		rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers);
2926f3118b5SNicolas Dichtel 		rt->rt6i_genid = rt_genid(net);
29351ebd318SNicolas Dichtel 		INIT_LIST_HEAD(&rt->rt6i_siblings);
29451ebd318SNicolas Dichtel 		rt->rt6i_nsiblings = 0;
29597bab73fSDavid S. Miller 	}
296cf911662SDavid S. Miller 	return rt;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3021da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3031da177e4SLinus Torvalds 
30497cac082SDavid S. Miller 	if (rt->n)
30597cac082SDavid S. Miller 		neigh_release(rt->n);
30697cac082SDavid S. Miller 
3078e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST))
3088e2ec639SYan, Zheng 		dst_destroy_metrics_generic(dst);
3098e2ec639SYan, Zheng 
31038308473SDavid S. Miller 	if (idev) {
3111da177e4SLinus Torvalds 		rt->rt6i_idev = NULL;
3121da177e4SLinus Torvalds 		in6_dev_put(idev);
3131da177e4SLinus Torvalds 	}
3141716a961SGao feng 
3151716a961SGao feng 	if (!(rt->rt6i_flags & RTF_EXPIRES) && dst->from)
3161716a961SGao feng 		dst_release(dst->from);
3171716a961SGao feng 
31897bab73fSDavid S. Miller 	if (rt6_has_peer(rt)) {
31997bab73fSDavid S. Miller 		struct inet_peer *peer = rt6_peer_ptr(rt);
320b3419363SDavid S. Miller 		inet_putpeer(peer);
321b3419363SDavid S. Miller 	}
322b3419363SDavid S. Miller }
323b3419363SDavid S. Miller 
3246431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0);
3256431cbc2SDavid S. Miller 
3266431cbc2SDavid S. Miller static u32 rt6_peer_genid(void)
3276431cbc2SDavid S. Miller {
3286431cbc2SDavid S. Miller 	return atomic_read(&__rt6_peer_genid);
3296431cbc2SDavid S. Miller }
3306431cbc2SDavid S. Miller 
331b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create)
332b3419363SDavid S. Miller {
33397bab73fSDavid S. Miller 	struct inet_peer_base *base;
334b3419363SDavid S. Miller 	struct inet_peer *peer;
335b3419363SDavid S. Miller 
33697bab73fSDavid S. Miller 	base = inetpeer_base_ptr(rt->_rt6i_peer);
33797bab73fSDavid S. Miller 	if (!base)
33897bab73fSDavid S. Miller 		return;
33997bab73fSDavid S. Miller 
34097bab73fSDavid S. Miller 	peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create);
3417b34ca2aSDavid S. Miller 	if (peer) {
34297bab73fSDavid S. Miller 		if (!rt6_set_peer(rt, peer))
343b3419363SDavid S. Miller 			inet_putpeer(peer);
3446431cbc2SDavid S. Miller 		else
3456431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
3461da177e4SLinus Torvalds 	}
3477b34ca2aSDavid S. Miller }
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
3501da177e4SLinus Torvalds 			   int how)
3511da177e4SLinus Torvalds {
3521da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
3531da177e4SLinus Torvalds 	struct inet6_dev *idev = rt->rt6i_idev;
3545a3e55d6SDenis V. Lunev 	struct net_device *loopback_dev =
355c346dca1SYOSHIFUJI Hideaki 		dev_net(dev)->loopback_dev;
3561da177e4SLinus Torvalds 
35797cac082SDavid S. Miller 	if (dev != loopback_dev) {
35897cac082SDavid S. Miller 		if (idev && idev->dev == dev) {
3595a3e55d6SDenis V. Lunev 			struct inet6_dev *loopback_idev =
3605a3e55d6SDenis V. Lunev 				in6_dev_get(loopback_dev);
36138308473SDavid S. Miller 			if (loopback_idev) {
3621da177e4SLinus Torvalds 				rt->rt6i_idev = loopback_idev;
3631da177e4SLinus Torvalds 				in6_dev_put(idev);
3641da177e4SLinus Torvalds 			}
3651da177e4SLinus Torvalds 		}
36697cac082SDavid S. Miller 		if (rt->n && rt->n->dev == dev) {
36797cac082SDavid S. Miller 			rt->n->dev = loopback_dev;
36897cac082SDavid S. Miller 			dev_hold(loopback_dev);
36997cac082SDavid S. Miller 			dev_put(dev);
37097cac082SDavid S. Miller 		}
37197cac082SDavid S. Miller 	}
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
374a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt)
3751da177e4SLinus Torvalds {
3761716a961SGao feng 	if (rt->rt6i_flags & RTF_EXPIRES) {
3771716a961SGao feng 		if (time_after(jiffies, rt->dst.expires))
378a50feda5SEric Dumazet 			return true;
3791716a961SGao feng 	} else if (rt->dst.from) {
3803fd91fb3SLi RongQing 		return rt6_check_expired((struct rt6_info *) rt->dst.from);
3811716a961SGao feng 	}
382a50feda5SEric Dumazet 	return false;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
385a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr)
386c71099acSThomas Graf {
387a02cec21SEric Dumazet 	return ipv6_addr_type(daddr) &
388a02cec21SEric Dumazet 		(IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
389c71099acSThomas Graf }
390c71099acSThomas Graf 
39151ebd318SNicolas Dichtel /* Multipath route selection:
39251ebd318SNicolas Dichtel  *   Hash based function using packet header and flowlabel.
39351ebd318SNicolas Dichtel  * Adapted from fib_info_hashfn()
39451ebd318SNicolas Dichtel  */
39551ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count,
39651ebd318SNicolas Dichtel 			       const struct flowi6 *fl6)
39751ebd318SNicolas Dichtel {
39851ebd318SNicolas Dichtel 	unsigned int val = fl6->flowi6_proto;
39951ebd318SNicolas Dichtel 
400*b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->daddr.s6_addr32[0];
401*b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->daddr.s6_addr32[1];
402*b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->daddr.s6_addr32[2];
403*b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->daddr.s6_addr32[3];
40451ebd318SNicolas Dichtel 
405*b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->saddr.s6_addr32[0];
406*b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->saddr.s6_addr32[1];
407*b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->saddr.s6_addr32[2];
408*b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->saddr.s6_addr32[3];
40951ebd318SNicolas Dichtel 
41051ebd318SNicolas Dichtel 	/* Work only if this not encapsulated */
41151ebd318SNicolas Dichtel 	switch (fl6->flowi6_proto) {
41251ebd318SNicolas Dichtel 	case IPPROTO_UDP:
41351ebd318SNicolas Dichtel 	case IPPROTO_TCP:
41451ebd318SNicolas Dichtel 	case IPPROTO_SCTP:
415*b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_sport;
416*b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_dport;
41751ebd318SNicolas Dichtel 		break;
41851ebd318SNicolas Dichtel 
41951ebd318SNicolas Dichtel 	case IPPROTO_ICMPV6:
420*b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_type;
421*b3ce5ae1SNicolas Dichtel 		val ^= (__force u16)fl6->fl6_icmp_code;
42251ebd318SNicolas Dichtel 		break;
42351ebd318SNicolas Dichtel 	}
42451ebd318SNicolas Dichtel 	/* RFC6438 recommands to use flowlabel */
425*b3ce5ae1SNicolas Dichtel 	val ^= (__force u32)fl6->flowlabel;
42651ebd318SNicolas Dichtel 
42751ebd318SNicolas Dichtel 	/* Perhaps, we need to tune, this function? */
42851ebd318SNicolas Dichtel 	val = val ^ (val >> 7) ^ (val >> 12);
42951ebd318SNicolas Dichtel 	return val % candidate_count;
43051ebd318SNicolas Dichtel }
43151ebd318SNicolas Dichtel 
43251ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match,
43351ebd318SNicolas Dichtel 					     struct flowi6 *fl6)
43451ebd318SNicolas Dichtel {
43551ebd318SNicolas Dichtel 	struct rt6_info *sibling, *next_sibling;
43651ebd318SNicolas Dichtel 	int route_choosen;
43751ebd318SNicolas Dichtel 
43851ebd318SNicolas Dichtel 	route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6);
43951ebd318SNicolas Dichtel 	/* Don't change the route, if route_choosen == 0
44051ebd318SNicolas Dichtel 	 * (siblings does not include ourself)
44151ebd318SNicolas Dichtel 	 */
44251ebd318SNicolas Dichtel 	if (route_choosen)
44351ebd318SNicolas Dichtel 		list_for_each_entry_safe(sibling, next_sibling,
44451ebd318SNicolas Dichtel 				&match->rt6i_siblings, rt6i_siblings) {
44551ebd318SNicolas Dichtel 			route_choosen--;
44651ebd318SNicolas Dichtel 			if (route_choosen == 0) {
44751ebd318SNicolas Dichtel 				match = sibling;
44851ebd318SNicolas Dichtel 				break;
44951ebd318SNicolas Dichtel 			}
45051ebd318SNicolas Dichtel 		}
45151ebd318SNicolas Dichtel 	return match;
45251ebd318SNicolas Dichtel }
45351ebd318SNicolas Dichtel 
4541da177e4SLinus Torvalds /*
455c71099acSThomas Graf  *	Route lookup. Any table->tb6_lock is implied.
4561da177e4SLinus Torvalds  */
4571da177e4SLinus Torvalds 
4588ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net,
4598ed67789SDaniel Lezcano 						    struct rt6_info *rt,
460b71d1d42SEric Dumazet 						    const struct in6_addr *saddr,
4611da177e4SLinus Torvalds 						    int oif,
462d420895eSYOSHIFUJI Hideaki 						    int flags)
4631da177e4SLinus Torvalds {
4641da177e4SLinus Torvalds 	struct rt6_info *local = NULL;
4651da177e4SLinus Torvalds 	struct rt6_info *sprt;
4661da177e4SLinus Torvalds 
467dd3abc4eSYOSHIFUJI Hideaki 	if (!oif && ipv6_addr_any(saddr))
468dd3abc4eSYOSHIFUJI Hideaki 		goto out;
469dd3abc4eSYOSHIFUJI Hideaki 
470d8d1f30bSChangli Gao 	for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
471d1918542SDavid S. Miller 		struct net_device *dev = sprt->dst.dev;
472dd3abc4eSYOSHIFUJI Hideaki 
473dd3abc4eSYOSHIFUJI Hideaki 		if (oif) {
4741da177e4SLinus Torvalds 			if (dev->ifindex == oif)
4751da177e4SLinus Torvalds 				return sprt;
4761da177e4SLinus Torvalds 			if (dev->flags & IFF_LOOPBACK) {
47738308473SDavid S. Miller 				if (!sprt->rt6i_idev ||
4781da177e4SLinus Torvalds 				    sprt->rt6i_idev->dev->ifindex != oif) {
479d420895eSYOSHIFUJI Hideaki 					if (flags & RT6_LOOKUP_F_IFACE && oif)
4801da177e4SLinus Torvalds 						continue;
4811da177e4SLinus Torvalds 					if (local && (!oif ||
4821da177e4SLinus Torvalds 						      local->rt6i_idev->dev->ifindex == oif))
4831da177e4SLinus Torvalds 						continue;
4841da177e4SLinus Torvalds 				}
4851da177e4SLinus Torvalds 				local = sprt;
4861da177e4SLinus Torvalds 			}
487dd3abc4eSYOSHIFUJI Hideaki 		} else {
488dd3abc4eSYOSHIFUJI Hideaki 			if (ipv6_chk_addr(net, saddr, dev,
489dd3abc4eSYOSHIFUJI Hideaki 					  flags & RT6_LOOKUP_F_IFACE))
490dd3abc4eSYOSHIFUJI Hideaki 				return sprt;
491dd3abc4eSYOSHIFUJI Hideaki 		}
4921da177e4SLinus Torvalds 	}
4931da177e4SLinus Torvalds 
494dd3abc4eSYOSHIFUJI Hideaki 	if (oif) {
4951da177e4SLinus Torvalds 		if (local)
4961da177e4SLinus Torvalds 			return local;
4971da177e4SLinus Torvalds 
498d420895eSYOSHIFUJI Hideaki 		if (flags & RT6_LOOKUP_F_IFACE)
4998ed67789SDaniel Lezcano 			return net->ipv6.ip6_null_entry;
5001da177e4SLinus Torvalds 	}
501dd3abc4eSYOSHIFUJI Hideaki out:
5021da177e4SLinus Torvalds 	return rt;
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
50527097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
50627097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt)
50727097255SYOSHIFUJI Hideaki {
508f2c31e32SEric Dumazet 	struct neighbour *neigh;
50927097255SYOSHIFUJI Hideaki 	/*
51027097255SYOSHIFUJI Hideaki 	 * Okay, this does not seem to be appropriate
51127097255SYOSHIFUJI Hideaki 	 * for now, however, we need to check if it
51227097255SYOSHIFUJI Hideaki 	 * is really so; aka Router Reachability Probing.
51327097255SYOSHIFUJI Hideaki 	 *
51427097255SYOSHIFUJI Hideaki 	 * Router Reachability Probe MUST be rate-limited
51527097255SYOSHIFUJI Hideaki 	 * to no more than one per minute.
51627097255SYOSHIFUJI Hideaki 	 */
51797cac082SDavid S. Miller 	neigh = rt ? rt->n : NULL;
51827097255SYOSHIFUJI Hideaki 	if (!neigh || (neigh->nud_state & NUD_VALID))
519fdd6681dSAmerigo Wang 		return;
52027097255SYOSHIFUJI Hideaki 	read_lock_bh(&neigh->lock);
52127097255SYOSHIFUJI Hideaki 	if (!(neigh->nud_state & NUD_VALID) &&
52252e16356SYOSHIFUJI Hideaki 	    time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
52327097255SYOSHIFUJI Hideaki 		struct in6_addr mcaddr;
52427097255SYOSHIFUJI Hideaki 		struct in6_addr *target;
52527097255SYOSHIFUJI Hideaki 
52627097255SYOSHIFUJI Hideaki 		neigh->updated = jiffies;
52727097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
52827097255SYOSHIFUJI Hideaki 
52927097255SYOSHIFUJI Hideaki 		target = (struct in6_addr *)&neigh->primary_key;
53027097255SYOSHIFUJI Hideaki 		addrconf_addr_solict_mult(target, &mcaddr);
531d1918542SDavid S. Miller 		ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL);
532f2c31e32SEric Dumazet 	} else {
53327097255SYOSHIFUJI Hideaki 		read_unlock_bh(&neigh->lock);
53427097255SYOSHIFUJI Hideaki 	}
535f2c31e32SEric Dumazet }
53627097255SYOSHIFUJI Hideaki #else
53727097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt)
53827097255SYOSHIFUJI Hideaki {
53927097255SYOSHIFUJI Hideaki }
54027097255SYOSHIFUJI Hideaki #endif
54127097255SYOSHIFUJI Hideaki 
5421da177e4SLinus Torvalds /*
543554cfb7eSYOSHIFUJI Hideaki  * Default Router Selection (RFC 2461 6.3.6)
5441da177e4SLinus Torvalds  */
545b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif)
5461da177e4SLinus Torvalds {
547d1918542SDavid S. Miller 	struct net_device *dev = rt->dst.dev;
548161980f4SDavid S. Miller 	if (!oif || dev->ifindex == oif)
549554cfb7eSYOSHIFUJI Hideaki 		return 2;
550161980f4SDavid S. Miller 	if ((dev->flags & IFF_LOOPBACK) &&
551161980f4SDavid S. Miller 	    rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
552161980f4SDavid S. Miller 		return 1;
553554cfb7eSYOSHIFUJI Hideaki 	return 0;
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
556b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt)
5571da177e4SLinus Torvalds {
558f2c31e32SEric Dumazet 	struct neighbour *neigh;
559398bcbebSYOSHIFUJI Hideaki 	int m;
560f2c31e32SEric Dumazet 
56197cac082SDavid S. Miller 	neigh = rt->n;
5624d0c5911SYOSHIFUJI Hideaki 	if (rt->rt6i_flags & RTF_NONEXTHOP ||
5634d0c5911SYOSHIFUJI Hideaki 	    !(rt->rt6i_flags & RTF_GATEWAY))
5644d0c5911SYOSHIFUJI Hideaki 		m = 1;
5654d0c5911SYOSHIFUJI Hideaki 	else if (neigh) {
5661da177e4SLinus Torvalds 		read_lock_bh(&neigh->lock);
567554cfb7eSYOSHIFUJI Hideaki 		if (neigh->nud_state & NUD_VALID)
5684d0c5911SYOSHIFUJI Hideaki 			m = 2;
569398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
570398bcbebSYOSHIFUJI Hideaki 		else if (neigh->nud_state & NUD_FAILED)
571398bcbebSYOSHIFUJI Hideaki 			m = 0;
572398bcbebSYOSHIFUJI Hideaki #endif
573398bcbebSYOSHIFUJI Hideaki 		else
574ea73ee23SYOSHIFUJI Hideaki 			m = 1;
5751da177e4SLinus Torvalds 		read_unlock_bh(&neigh->lock);
576398bcbebSYOSHIFUJI Hideaki 	} else
577398bcbebSYOSHIFUJI Hideaki 		m = 0;
578554cfb7eSYOSHIFUJI Hideaki 	return m;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
581554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif,
582554cfb7eSYOSHIFUJI Hideaki 			   int strict)
583554cfb7eSYOSHIFUJI Hideaki {
5844d0c5911SYOSHIFUJI Hideaki 	int m, n;
5854d0c5911SYOSHIFUJI Hideaki 
5864d0c5911SYOSHIFUJI Hideaki 	m = rt6_check_dev(rt, oif);
58777d16f45SYOSHIFUJI Hideaki 	if (!m && (strict & RT6_LOOKUP_F_IFACE))
588554cfb7eSYOSHIFUJI Hideaki 		return -1;
589ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
590ebacaaa0SYOSHIFUJI Hideaki 	m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
591ebacaaa0SYOSHIFUJI Hideaki #endif
5924d0c5911SYOSHIFUJI Hideaki 	n = rt6_check_neigh(rt);
593557e92efSYOSHIFUJI Hideaki 	if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
594554cfb7eSYOSHIFUJI Hideaki 		return -1;
595554cfb7eSYOSHIFUJI Hideaki 	return m;
596554cfb7eSYOSHIFUJI Hideaki }
597554cfb7eSYOSHIFUJI Hideaki 
598f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
599f11e6659SDavid S. Miller 				   int *mpri, struct rt6_info *match)
600554cfb7eSYOSHIFUJI Hideaki {
601554cfb7eSYOSHIFUJI Hideaki 	int m;
602554cfb7eSYOSHIFUJI Hideaki 
603554cfb7eSYOSHIFUJI Hideaki 	if (rt6_check_expired(rt))
604f11e6659SDavid S. Miller 		goto out;
605554cfb7eSYOSHIFUJI Hideaki 
606554cfb7eSYOSHIFUJI Hideaki 	m = rt6_score_route(rt, oif, strict);
607554cfb7eSYOSHIFUJI Hideaki 	if (m < 0)
608f11e6659SDavid S. Miller 		goto out;
609554cfb7eSYOSHIFUJI Hideaki 
610f11e6659SDavid S. Miller 	if (m > *mpri) {
611ea659e07SYOSHIFUJI Hideaki 		if (strict & RT6_LOOKUP_F_REACHABLE)
61227097255SYOSHIFUJI Hideaki 			rt6_probe(match);
613f11e6659SDavid S. Miller 		*mpri = m;
614554cfb7eSYOSHIFUJI Hideaki 		match = rt;
615ea659e07SYOSHIFUJI Hideaki 	} else if (strict & RT6_LOOKUP_F_REACHABLE) {
61627097255SYOSHIFUJI Hideaki 		rt6_probe(rt);
6171da177e4SLinus Torvalds 	}
618f11e6659SDavid S. Miller 
619f11e6659SDavid S. Miller out:
620f11e6659SDavid S. Miller 	return match;
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
623f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
624f11e6659SDavid S. Miller 				     struct rt6_info *rr_head,
625f11e6659SDavid S. Miller 				     u32 metric, int oif, int strict)
626f11e6659SDavid S. Miller {
627f11e6659SDavid S. Miller 	struct rt6_info *rt, *match;
628f11e6659SDavid S. Miller 	int mpri = -1;
629f11e6659SDavid S. Miller 
630f11e6659SDavid S. Miller 	match = NULL;
631f11e6659SDavid S. Miller 	for (rt = rr_head; rt && rt->rt6i_metric == metric;
632d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
633f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
634f11e6659SDavid S. Miller 	for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
635d8d1f30bSChangli Gao 	     rt = rt->dst.rt6_next)
636f11e6659SDavid S. Miller 		match = find_match(rt, oif, strict, &mpri, match);
637f11e6659SDavid S. Miller 
638f11e6659SDavid S. Miller 	return match;
639f11e6659SDavid S. Miller }
640f11e6659SDavid S. Miller 
641f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
642f11e6659SDavid S. Miller {
643f11e6659SDavid S. Miller 	struct rt6_info *match, *rt0;
6448ed67789SDaniel Lezcano 	struct net *net;
645f11e6659SDavid S. Miller 
646f11e6659SDavid S. Miller 	rt0 = fn->rr_ptr;
647f11e6659SDavid S. Miller 	if (!rt0)
648f11e6659SDavid S. Miller 		fn->rr_ptr = rt0 = fn->leaf;
649f11e6659SDavid S. Miller 
650f11e6659SDavid S. Miller 	match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
651f11e6659SDavid S. Miller 
652554cfb7eSYOSHIFUJI Hideaki 	if (!match &&
653f11e6659SDavid S. Miller 	    (strict & RT6_LOOKUP_F_REACHABLE)) {
654d8d1f30bSChangli Gao 		struct rt6_info *next = rt0->dst.rt6_next;
655f11e6659SDavid S. Miller 
656554cfb7eSYOSHIFUJI Hideaki 		/* no entries matched; do round-robin */
657f11e6659SDavid S. Miller 		if (!next || next->rt6i_metric != rt0->rt6i_metric)
658f11e6659SDavid S. Miller 			next = fn->leaf;
659f11e6659SDavid S. Miller 
660f11e6659SDavid S. Miller 		if (next != rt0)
661f11e6659SDavid S. Miller 			fn->rr_ptr = next;
662554cfb7eSYOSHIFUJI Hideaki 	}
663554cfb7eSYOSHIFUJI Hideaki 
664d1918542SDavid S. Miller 	net = dev_net(rt0->dst.dev);
665a02cec21SEric Dumazet 	return match ? match : net->ipv6.ip6_null_entry;
6661da177e4SLinus Torvalds }
6671da177e4SLinus Torvalds 
66870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
66970ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
670b71d1d42SEric Dumazet 		  const struct in6_addr *gwaddr)
67170ceb4f5SYOSHIFUJI Hideaki {
672c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
67370ceb4f5SYOSHIFUJI Hideaki 	struct route_info *rinfo = (struct route_info *) opt;
67470ceb4f5SYOSHIFUJI Hideaki 	struct in6_addr prefix_buf, *prefix;
67570ceb4f5SYOSHIFUJI Hideaki 	unsigned int pref;
6764bed72e4SYOSHIFUJI Hideaki 	unsigned long lifetime;
67770ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt;
67870ceb4f5SYOSHIFUJI Hideaki 
67970ceb4f5SYOSHIFUJI Hideaki 	if (len < sizeof(struct route_info)) {
68070ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
68170ceb4f5SYOSHIFUJI Hideaki 	}
68270ceb4f5SYOSHIFUJI Hideaki 
68370ceb4f5SYOSHIFUJI Hideaki 	/* Sanity check for prefix_len and length */
68470ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length > 3) {
68570ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
68670ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 128) {
68770ceb4f5SYOSHIFUJI Hideaki 		return -EINVAL;
68870ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 64) {
68970ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 2) {
69070ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
69170ceb4f5SYOSHIFUJI Hideaki 		}
69270ceb4f5SYOSHIFUJI Hideaki 	} else if (rinfo->prefix_len > 0) {
69370ceb4f5SYOSHIFUJI Hideaki 		if (rinfo->length < 1) {
69470ceb4f5SYOSHIFUJI Hideaki 			return -EINVAL;
69570ceb4f5SYOSHIFUJI Hideaki 		}
69670ceb4f5SYOSHIFUJI Hideaki 	}
69770ceb4f5SYOSHIFUJI Hideaki 
69870ceb4f5SYOSHIFUJI Hideaki 	pref = rinfo->route_pref;
69970ceb4f5SYOSHIFUJI Hideaki 	if (pref == ICMPV6_ROUTER_PREF_INVALID)
7003933fc95SJens Rosenboom 		return -EINVAL;
70170ceb4f5SYOSHIFUJI Hideaki 
7024bed72e4SYOSHIFUJI Hideaki 	lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
70370ceb4f5SYOSHIFUJI Hideaki 
70470ceb4f5SYOSHIFUJI Hideaki 	if (rinfo->length == 3)
70570ceb4f5SYOSHIFUJI Hideaki 		prefix = (struct in6_addr *)rinfo->prefix;
70670ceb4f5SYOSHIFUJI Hideaki 	else {
70770ceb4f5SYOSHIFUJI Hideaki 		/* this function is safe */
70870ceb4f5SYOSHIFUJI Hideaki 		ipv6_addr_prefix(&prefix_buf,
70970ceb4f5SYOSHIFUJI Hideaki 				 (struct in6_addr *)rinfo->prefix,
71070ceb4f5SYOSHIFUJI Hideaki 				 rinfo->prefix_len);
71170ceb4f5SYOSHIFUJI Hideaki 		prefix = &prefix_buf;
71270ceb4f5SYOSHIFUJI Hideaki 	}
71370ceb4f5SYOSHIFUJI Hideaki 
714efa2cea0SDaniel Lezcano 	rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
715efa2cea0SDaniel Lezcano 				dev->ifindex);
71670ceb4f5SYOSHIFUJI Hideaki 
71770ceb4f5SYOSHIFUJI Hideaki 	if (rt && !lifetime) {
718e0a1ad73SThomas Graf 		ip6_del_rt(rt);
71970ceb4f5SYOSHIFUJI Hideaki 		rt = NULL;
72070ceb4f5SYOSHIFUJI Hideaki 	}
72170ceb4f5SYOSHIFUJI Hideaki 
72270ceb4f5SYOSHIFUJI Hideaki 	if (!rt && lifetime)
723efa2cea0SDaniel Lezcano 		rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
72470ceb4f5SYOSHIFUJI Hideaki 					pref);
72570ceb4f5SYOSHIFUJI Hideaki 	else if (rt)
72670ceb4f5SYOSHIFUJI Hideaki 		rt->rt6i_flags = RTF_ROUTEINFO |
72770ceb4f5SYOSHIFUJI Hideaki 				 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
72870ceb4f5SYOSHIFUJI Hideaki 
72970ceb4f5SYOSHIFUJI Hideaki 	if (rt) {
7301716a961SGao feng 		if (!addrconf_finite_timeout(lifetime))
7311716a961SGao feng 			rt6_clean_expires(rt);
7321716a961SGao feng 		else
7331716a961SGao feng 			rt6_set_expires(rt, jiffies + HZ * lifetime);
7341716a961SGao feng 
735d8d1f30bSChangli Gao 		dst_release(&rt->dst);
73670ceb4f5SYOSHIFUJI Hideaki 	}
73770ceb4f5SYOSHIFUJI Hideaki 	return 0;
73870ceb4f5SYOSHIFUJI Hideaki }
73970ceb4f5SYOSHIFUJI Hideaki #endif
74070ceb4f5SYOSHIFUJI Hideaki 
7418ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr)			\
742982f56f3SYOSHIFUJI Hideaki do { \
7438ed67789SDaniel Lezcano 	if (rt == __net->ipv6.ip6_null_entry) {	\
744982f56f3SYOSHIFUJI Hideaki 		struct fib6_node *pn; \
745e0eda7bbSVille Nuorvala 		while (1) { \
746982f56f3SYOSHIFUJI Hideaki 			if (fn->fn_flags & RTN_TL_ROOT) \
747c71099acSThomas Graf 				goto out; \
748982f56f3SYOSHIFUJI Hideaki 			pn = fn->parent; \
749982f56f3SYOSHIFUJI Hideaki 			if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
7508bce65b9SKim Nordlund 				fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
751982f56f3SYOSHIFUJI Hideaki 			else \
752982f56f3SYOSHIFUJI Hideaki 				fn = pn; \
753c71099acSThomas Graf 			if (fn->fn_flags & RTN_RTINFO) \
754c71099acSThomas Graf 				goto restart; \
755c71099acSThomas Graf 		} \
756982f56f3SYOSHIFUJI Hideaki 	} \
757982f56f3SYOSHIFUJI Hideaki } while (0)
758c71099acSThomas Graf 
7598ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net,
7608ed67789SDaniel Lezcano 					     struct fib6_table *table,
7614c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	struct fib6_node *fn;
7641da177e4SLinus Torvalds 	struct rt6_info *rt;
7651da177e4SLinus Torvalds 
766c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
7674c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
768c71099acSThomas Graf restart:
769c71099acSThomas Graf 	rt = fn->leaf;
7704c9483b2SDavid S. Miller 	rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags);
77151ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0)
77251ebd318SNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6);
7734c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
774c71099acSThomas Graf out:
775d8d1f30bSChangli Gao 	dst_use(&rt->dst, jiffies);
776c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
7771da177e4SLinus Torvalds 	return rt;
778c71099acSThomas Graf 
779c71099acSThomas Graf }
780c71099acSThomas Graf 
781ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
782ea6e574eSFlorian Westphal 				    int flags)
783ea6e574eSFlorian Westphal {
784ea6e574eSFlorian Westphal 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
785ea6e574eSFlorian Westphal }
786ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup);
787ea6e574eSFlorian Westphal 
7889acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
7899acd9f3aSYOSHIFUJI Hideaki 			    const struct in6_addr *saddr, int oif, int strict)
790c71099acSThomas Graf {
7914c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
7924c9483b2SDavid S. Miller 		.flowi6_oif = oif,
7934c9483b2SDavid S. Miller 		.daddr = *daddr,
794c71099acSThomas Graf 	};
795c71099acSThomas Graf 	struct dst_entry *dst;
79677d16f45SYOSHIFUJI Hideaki 	int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
797c71099acSThomas Graf 
798adaa70bbSThomas Graf 	if (saddr) {
7994c9483b2SDavid S. Miller 		memcpy(&fl6.saddr, saddr, sizeof(*saddr));
800adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
801adaa70bbSThomas Graf 	}
802adaa70bbSThomas Graf 
8034c9483b2SDavid S. Miller 	dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup);
804c71099acSThomas Graf 	if (dst->error == 0)
805c71099acSThomas Graf 		return (struct rt6_info *) dst;
806c71099acSThomas Graf 
807c71099acSThomas Graf 	dst_release(dst);
808c71099acSThomas Graf 
8091da177e4SLinus Torvalds 	return NULL;
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
8127159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup);
8137159039aSYOSHIFUJI Hideaki 
814c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock.
8151da177e4SLinus Torvalds    It takes new route entry, the addition fails by any reason the
8161da177e4SLinus Torvalds    route is freed. In any case, if caller does not hold it, it may
8171da177e4SLinus Torvalds    be destroyed.
8181da177e4SLinus Torvalds  */
8191da177e4SLinus Torvalds 
82086872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
8211da177e4SLinus Torvalds {
8221da177e4SLinus Torvalds 	int err;
823c71099acSThomas Graf 	struct fib6_table *table;
8241da177e4SLinus Torvalds 
825c71099acSThomas Graf 	table = rt->rt6i_table;
826c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
82786872cb5SThomas Graf 	err = fib6_add(&table->tb6_root, rt, info);
828c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	return err;
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
83340e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt)
83440e22e8fSThomas Graf {
8354d1169c1SDenis V. Lunev 	struct nl_info info = {
836d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
8374d1169c1SDenis V. Lunev 	};
838528c4cebSDenis V. Lunev 	return __ip6_ins_rt(rt, &info);
83940e22e8fSThomas Graf }
84040e22e8fSThomas Graf 
8411716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
84221efcfa0SEric Dumazet 				      const struct in6_addr *daddr,
843b71d1d42SEric Dumazet 				      const struct in6_addr *saddr)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds 	struct rt6_info *rt;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	/*
8481da177e4SLinus Torvalds 	 *	Clone the route.
8491da177e4SLinus Torvalds 	 */
8501da177e4SLinus Torvalds 
85121efcfa0SEric Dumazet 	rt = ip6_rt_copy(ort, daddr);
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	if (rt) {
85414deae41SDavid S. Miller 		int attempts = !in_softirq();
85514deae41SDavid S. Miller 
85658c4fb86SYOSHIFUJI Hideaki 		if (!(rt->rt6i_flags & RTF_GATEWAY)) {
857bb3c3686SDavid S. Miller 			if (ort->rt6i_dst.plen != 128 &&
85821efcfa0SEric Dumazet 			    ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
85958c4fb86SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_ANYCAST;
8604e3fd7a0SAlexey Dobriyan 			rt->rt6i_gateway = *daddr;
86158c4fb86SYOSHIFUJI Hideaki 		}
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_CACHE;
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
8661da177e4SLinus Torvalds 		if (rt->rt6i_src.plen && saddr) {
8674e3fd7a0SAlexey Dobriyan 			rt->rt6i_src.addr = *saddr;
8681da177e4SLinus Torvalds 			rt->rt6i_src.plen = 128;
8691da177e4SLinus Torvalds 		}
8701da177e4SLinus Torvalds #endif
8711da177e4SLinus Torvalds 
87214deae41SDavid S. Miller 	retry:
8738ade06c6SDavid S. Miller 		if (rt6_bind_neighbour(rt, rt->dst.dev)) {
874d1918542SDavid S. Miller 			struct net *net = dev_net(rt->dst.dev);
87514deae41SDavid S. Miller 			int saved_rt_min_interval =
87614deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval;
87714deae41SDavid S. Miller 			int saved_rt_elasticity =
87814deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity;
87914deae41SDavid S. Miller 
88014deae41SDavid S. Miller 			if (attempts-- > 0) {
88114deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
88214deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
88314deae41SDavid S. Miller 
88486393e52SAlexey Dobriyan 				ip6_dst_gc(&net->ipv6.ip6_dst_ops);
88514deae41SDavid S. Miller 
88614deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_elasticity =
88714deae41SDavid S. Miller 					saved_rt_elasticity;
88814deae41SDavid S. Miller 				net->ipv6.sysctl.ip6_rt_gc_min_interval =
88914deae41SDavid S. Miller 					saved_rt_min_interval;
89014deae41SDavid S. Miller 				goto retry;
89114deae41SDavid S. Miller 			}
89214deae41SDavid S. Miller 
893f3213831SJoe Perches 			net_warn_ratelimited("Neighbour table overflow\n");
894d8d1f30bSChangli Gao 			dst_free(&rt->dst);
89514deae41SDavid S. Miller 			return NULL;
89614deae41SDavid S. Miller 		}
89795a9a5baSYOSHIFUJI Hideaki 	}
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	return rt;
9001da177e4SLinus Torvalds }
90195a9a5baSYOSHIFUJI Hideaki 
90221efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort,
90321efcfa0SEric Dumazet 					const struct in6_addr *daddr)
904299d9939SYOSHIFUJI Hideaki {
90521efcfa0SEric Dumazet 	struct rt6_info *rt = ip6_rt_copy(ort, daddr);
90621efcfa0SEric Dumazet 
907299d9939SYOSHIFUJI Hideaki 	if (rt) {
908299d9939SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_CACHE;
90997cac082SDavid S. Miller 		rt->n = neigh_clone(ort->n);
910299d9939SYOSHIFUJI Hideaki 	}
911299d9939SYOSHIFUJI Hideaki 	return rt;
912299d9939SYOSHIFUJI Hideaki }
913299d9939SYOSHIFUJI Hideaki 
9148ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
9154c9483b2SDavid S. Miller 				      struct flowi6 *fl6, int flags)
9161da177e4SLinus Torvalds {
9171da177e4SLinus Torvalds 	struct fib6_node *fn;
918519fbd87SYOSHIFUJI Hideaki 	struct rt6_info *rt, *nrt;
919c71099acSThomas Graf 	int strict = 0;
9201da177e4SLinus Torvalds 	int attempts = 3;
921519fbd87SYOSHIFUJI Hideaki 	int err;
92253b7997fSYOSHIFUJI Hideaki 	int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
9231da177e4SLinus Torvalds 
92477d16f45SYOSHIFUJI Hideaki 	strict |= flags & RT6_LOOKUP_F_IFACE;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds relookup:
927c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
9281da177e4SLinus Torvalds 
9298238dd06SYOSHIFUJI Hideaki restart_2:
9304c9483b2SDavid S. Miller 	fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr);
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds restart:
9334acad72dSPavel Emelyanov 	rt = rt6_select(fn, oif, strict | reachable);
93451ebd318SNicolas Dichtel 	if (rt->rt6i_nsiblings && oif == 0)
93551ebd318SNicolas Dichtel 		rt = rt6_multipath_select(rt, fl6);
9364c9483b2SDavid S. Miller 	BACKTRACK(net, &fl6->saddr);
9378ed67789SDaniel Lezcano 	if (rt == net->ipv6.ip6_null_entry ||
9388238dd06SYOSHIFUJI Hideaki 	    rt->rt6i_flags & RTF_CACHE)
9391da177e4SLinus Torvalds 		goto out;
9401da177e4SLinus Torvalds 
941d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
942c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9431da177e4SLinus Torvalds 
94497cac082SDavid S. Miller 	if (!rt->n && !(rt->rt6i_flags & RTF_NONEXTHOP))
9454c9483b2SDavid S. Miller 		nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr);
9467343ff31SDavid S. Miller 	else if (!(rt->dst.flags & DST_HOST))
9474c9483b2SDavid S. Miller 		nrt = rt6_alloc_clone(rt, &fl6->daddr);
9487343ff31SDavid S. Miller 	else
9497343ff31SDavid S. Miller 		goto out2;
9501da177e4SLinus Torvalds 
951d8d1f30bSChangli Gao 	dst_release(&rt->dst);
9528ed67789SDaniel Lezcano 	rt = nrt ? : net->ipv6.ip6_null_entry;
9531da177e4SLinus Torvalds 
954d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
955e40cf353SYOSHIFUJI Hideaki 	if (nrt) {
95640e22e8fSThomas Graf 		err = ip6_ins_rt(nrt);
957e40cf353SYOSHIFUJI Hideaki 		if (!err)
958e40cf353SYOSHIFUJI Hideaki 			goto out2;
959e40cf353SYOSHIFUJI Hideaki 	}
960e40cf353SYOSHIFUJI Hideaki 
961e40cf353SYOSHIFUJI Hideaki 	if (--attempts <= 0)
9621da177e4SLinus Torvalds 		goto out2;
9631da177e4SLinus Torvalds 
964519fbd87SYOSHIFUJI Hideaki 	/*
965c71099acSThomas Graf 	 * Race condition! In the gap, when table->tb6_lock was
966519fbd87SYOSHIFUJI Hideaki 	 * released someone could insert this route.  Relookup.
9671da177e4SLinus Torvalds 	 */
968d8d1f30bSChangli Gao 	dst_release(&rt->dst);
9691da177e4SLinus Torvalds 	goto relookup;
970e40cf353SYOSHIFUJI Hideaki 
971519fbd87SYOSHIFUJI Hideaki out:
9728238dd06SYOSHIFUJI Hideaki 	if (reachable) {
9738238dd06SYOSHIFUJI Hideaki 		reachable = 0;
9748238dd06SYOSHIFUJI Hideaki 		goto restart_2;
9758238dd06SYOSHIFUJI Hideaki 	}
976d8d1f30bSChangli Gao 	dst_hold(&rt->dst);
977c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
9781da177e4SLinus Torvalds out2:
979d8d1f30bSChangli Gao 	rt->dst.lastuse = jiffies;
980d8d1f30bSChangli Gao 	rt->dst.__use++;
981c71099acSThomas Graf 
982c71099acSThomas Graf 	return rt;
983c71099acSThomas Graf }
984c71099acSThomas Graf 
9858ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
9864c9483b2SDavid S. Miller 					    struct flowi6 *fl6, int flags)
9874acad72dSPavel Emelyanov {
9884c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags);
9894acad72dSPavel Emelyanov }
9904acad72dSPavel Emelyanov 
99172331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net,
99272331bc0SShmulik Ladkani 						struct net_device *dev,
99372331bc0SShmulik Ladkani 						struct flowi6 *fl6, int flags)
99472331bc0SShmulik Ladkani {
99572331bc0SShmulik Ladkani 	if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG)
99672331bc0SShmulik Ladkani 		flags |= RT6_LOOKUP_F_IFACE;
99772331bc0SShmulik Ladkani 
99872331bc0SShmulik Ladkani 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input);
99972331bc0SShmulik Ladkani }
100072331bc0SShmulik Ladkani 
1001c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb)
1002c71099acSThomas Graf {
1003b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
1004c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(skb->dev);
1005adaa70bbSThomas Graf 	int flags = RT6_LOOKUP_F_HAS_SADDR;
10064c9483b2SDavid S. Miller 	struct flowi6 fl6 = {
10074c9483b2SDavid S. Miller 		.flowi6_iif = skb->dev->ifindex,
10084c9483b2SDavid S. Miller 		.daddr = iph->daddr,
10094c9483b2SDavid S. Miller 		.saddr = iph->saddr,
10104c9483b2SDavid S. Miller 		.flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
10114c9483b2SDavid S. Miller 		.flowi6_mark = skb->mark,
10124c9483b2SDavid S. Miller 		.flowi6_proto = iph->nexthdr,
1013c71099acSThomas Graf 	};
1014adaa70bbSThomas Graf 
101572331bc0SShmulik Ladkani 	skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags));
1016c71099acSThomas Graf }
1017c71099acSThomas Graf 
10188ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
10194c9483b2SDavid S. Miller 					     struct flowi6 *fl6, int flags)
1020c71099acSThomas Graf {
10214c9483b2SDavid S. Miller 	return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags);
1022c71099acSThomas Graf }
1023c71099acSThomas Graf 
10249c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk,
10254c9483b2SDavid S. Miller 				    struct flowi6 *fl6)
1026c71099acSThomas Graf {
1027c71099acSThomas Graf 	int flags = 0;
1028c71099acSThomas Graf 
10291fb9489bSPavel Emelyanov 	fl6->flowi6_iif = LOOPBACK_IFINDEX;
10304dc27d1cSDavid McCullough 
10314c9483b2SDavid S. Miller 	if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr))
103277d16f45SYOSHIFUJI Hideaki 		flags |= RT6_LOOKUP_F_IFACE;
1033c71099acSThomas Graf 
10344c9483b2SDavid S. Miller 	if (!ipv6_addr_any(&fl6->saddr))
1035adaa70bbSThomas Graf 		flags |= RT6_LOOKUP_F_HAS_SADDR;
10360c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 	else if (sk)
10370c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 		flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
1038adaa70bbSThomas Graf 
10394c9483b2SDavid S. Miller 	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output);
10401da177e4SLinus Torvalds }
10411da177e4SLinus Torvalds 
10427159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output);
10431da177e4SLinus Torvalds 
10442774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig)
104514e50e57SDavid S. Miller {
10465c1e6aa3SDavid S. Miller 	struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig;
104714e50e57SDavid S. Miller 	struct dst_entry *new = NULL;
104814e50e57SDavid S. Miller 
1049f5b0a874SDavid S. Miller 	rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0);
105014e50e57SDavid S. Miller 	if (rt) {
1051d8d1f30bSChangli Gao 		new = &rt->dst;
105214e50e57SDavid S. Miller 
10538104891bSSteffen Klassert 		memset(new + 1, 0, sizeof(*rt) - sizeof(*new));
10548104891bSSteffen Klassert 		rt6_init_peer(rt, net->ipv6.peers);
10558104891bSSteffen Klassert 
105614e50e57SDavid S. Miller 		new->__use = 1;
1057352e512cSHerbert Xu 		new->input = dst_discard;
1058352e512cSHerbert Xu 		new->output = dst_discard;
105914e50e57SDavid S. Miller 
106021efcfa0SEric Dumazet 		if (dst_metrics_read_only(&ort->dst))
106121efcfa0SEric Dumazet 			new->_metrics = ort->dst._metrics;
106221efcfa0SEric Dumazet 		else
1063defb3519SDavid S. Miller 			dst_copy_metrics(new, &ort->dst);
106414e50e57SDavid S. Miller 		rt->rt6i_idev = ort->rt6i_idev;
106514e50e57SDavid S. Miller 		if (rt->rt6i_idev)
106614e50e57SDavid S. Miller 			in6_dev_hold(rt->rt6i_idev);
106714e50e57SDavid S. Miller 
10684e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
10691716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
10701716a961SGao feng 		rt6_clean_expires(rt);
107114e50e57SDavid S. Miller 		rt->rt6i_metric = 0;
107214e50e57SDavid S. Miller 
107314e50e57SDavid S. Miller 		memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
107414e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
107514e50e57SDavid S. Miller 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
107614e50e57SDavid S. Miller #endif
107714e50e57SDavid S. Miller 
107814e50e57SDavid S. Miller 		dst_free(new);
107914e50e57SDavid S. Miller 	}
108014e50e57SDavid S. Miller 
108169ead7afSDavid S. Miller 	dst_release(dst_orig);
108269ead7afSDavid S. Miller 	return new ? new : ERR_PTR(-ENOMEM);
108314e50e57SDavid S. Miller }
108414e50e57SDavid S. Miller 
10851da177e4SLinus Torvalds /*
10861da177e4SLinus Torvalds  *	Destination cache support functions
10871da177e4SLinus Torvalds  */
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
10901da177e4SLinus Torvalds {
10911da177e4SLinus Torvalds 	struct rt6_info *rt;
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	rt = (struct rt6_info *) dst;
10941da177e4SLinus Torvalds 
10956f3118b5SNicolas Dichtel 	/* All IPV6 dsts are created with ->obsolete set to the value
10966f3118b5SNicolas Dichtel 	 * DST_OBSOLETE_FORCE_CHK which forces validation calls down
10976f3118b5SNicolas Dichtel 	 * into this function always.
10986f3118b5SNicolas Dichtel 	 */
10996f3118b5SNicolas Dichtel 	if (rt->rt6i_genid != rt_genid(dev_net(rt->dst.dev)))
11006f3118b5SNicolas Dichtel 		return NULL;
11016f3118b5SNicolas Dichtel 
11026431cbc2SDavid S. Miller 	if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) {
11036431cbc2SDavid S. Miller 		if (rt->rt6i_peer_genid != rt6_peer_genid()) {
110497bab73fSDavid S. Miller 			if (!rt6_has_peer(rt))
11056431cbc2SDavid S. Miller 				rt6_bind_peer(rt, 0);
11066431cbc2SDavid S. Miller 			rt->rt6i_peer_genid = rt6_peer_genid();
11076431cbc2SDavid S. Miller 		}
11081da177e4SLinus Torvalds 		return dst;
11096431cbc2SDavid S. Miller 	}
11101da177e4SLinus Torvalds 	return NULL;
11111da177e4SLinus Torvalds }
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
11141da177e4SLinus Torvalds {
11151da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *) dst;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	if (rt) {
111854c1a859SYOSHIFUJI Hideaki / 吉藤英明 		if (rt->rt6i_flags & RTF_CACHE) {
111954c1a859SYOSHIFUJI Hideaki / 吉藤英明 			if (rt6_check_expired(rt)) {
1120e0a1ad73SThomas Graf 				ip6_del_rt(rt);
112154c1a859SYOSHIFUJI Hideaki / 吉藤英明 				dst = NULL;
11221da177e4SLinus Torvalds 			}
112354c1a859SYOSHIFUJI Hideaki / 吉藤英明 		} else {
112454c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst_release(dst);
112554c1a859SYOSHIFUJI Hideaki / 吉藤英明 			dst = NULL;
112654c1a859SYOSHIFUJI Hideaki / 吉藤英明 		}
112754c1a859SYOSHIFUJI Hideaki / 吉藤英明 	}
112854c1a859SYOSHIFUJI Hideaki / 吉藤英明 	return dst;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb)
11321da177e4SLinus Torvalds {
11331da177e4SLinus Torvalds 	struct rt6_info *rt;
11341da177e4SLinus Torvalds 
11353ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
11361da177e4SLinus Torvalds 
1137adf30907SEric Dumazet 	rt = (struct rt6_info *) skb_dst(skb);
11381da177e4SLinus Torvalds 	if (rt) {
11391716a961SGao feng 		if (rt->rt6i_flags & RTF_CACHE)
11401716a961SGao feng 			rt6_update_expires(rt, 0);
11411716a961SGao feng 		else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
11421da177e4SLinus Torvalds 			rt->rt6i_node->fn_sernum = -1;
11431da177e4SLinus Torvalds 	}
11441da177e4SLinus Torvalds }
11451da177e4SLinus Torvalds 
11466700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
11476700c270SDavid S. Miller 			       struct sk_buff *skb, u32 mtu)
11481da177e4SLinus Torvalds {
11491da177e4SLinus Torvalds 	struct rt6_info *rt6 = (struct rt6_info*)dst;
11501da177e4SLinus Torvalds 
115181aded24SDavid S. Miller 	dst_confirm(dst);
11521da177e4SLinus Torvalds 	if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
115381aded24SDavid S. Miller 		struct net *net = dev_net(dst->dev);
115481aded24SDavid S. Miller 
11551da177e4SLinus Torvalds 		rt6->rt6i_flags |= RTF_MODIFIED;
11561da177e4SLinus Torvalds 		if (mtu < IPV6_MIN_MTU) {
1157defb3519SDavid S. Miller 			u32 features = dst_metric(dst, RTAX_FEATURES);
11581da177e4SLinus Torvalds 			mtu = IPV6_MIN_MTU;
1159defb3519SDavid S. Miller 			features |= RTAX_FEATURE_ALLFRAG;
1160defb3519SDavid S. Miller 			dst_metric_set(dst, RTAX_FEATURES, features);
11611da177e4SLinus Torvalds 		}
1162defb3519SDavid S. Miller 		dst_metric_set(dst, RTAX_MTU, mtu);
116381aded24SDavid S. Miller 		rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires);
11641da177e4SLinus Torvalds 	}
11651da177e4SLinus Torvalds }
11661da177e4SLinus Torvalds 
116742ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
116842ae66c8SDavid S. Miller 		     int oif, u32 mark)
116981aded24SDavid S. Miller {
117081aded24SDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
117181aded24SDavid S. Miller 	struct dst_entry *dst;
117281aded24SDavid S. Miller 	struct flowi6 fl6;
117381aded24SDavid S. Miller 
117481aded24SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
117581aded24SDavid S. Miller 	fl6.flowi6_oif = oif;
117681aded24SDavid S. Miller 	fl6.flowi6_mark = mark;
11773e12939aSDavid S. Miller 	fl6.flowi6_flags = 0;
117881aded24SDavid S. Miller 	fl6.daddr = iph->daddr;
117981aded24SDavid S. Miller 	fl6.saddr = iph->saddr;
118081aded24SDavid S. Miller 	fl6.flowlabel = (*(__be32 *) iph) & IPV6_FLOWINFO_MASK;
118181aded24SDavid S. Miller 
118281aded24SDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
118381aded24SDavid S. Miller 	if (!dst->error)
11846700c270SDavid S. Miller 		ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu));
118581aded24SDavid S. Miller 	dst_release(dst);
118681aded24SDavid S. Miller }
118781aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu);
118881aded24SDavid S. Miller 
118981aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)
119081aded24SDavid S. Miller {
119181aded24SDavid S. Miller 	ip6_update_pmtu(skb, sock_net(sk), mtu,
119281aded24SDavid S. Miller 			sk->sk_bound_dev_if, sk->sk_mark);
119381aded24SDavid S. Miller }
119481aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu);
119581aded24SDavid S. Miller 
11963a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
11973a5ad2eeSDavid S. Miller {
11983a5ad2eeSDavid S. Miller 	const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;
11993a5ad2eeSDavid S. Miller 	struct dst_entry *dst;
12003a5ad2eeSDavid S. Miller 	struct flowi6 fl6;
12013a5ad2eeSDavid S. Miller 
12023a5ad2eeSDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
12033a5ad2eeSDavid S. Miller 	fl6.flowi6_oif = oif;
12043a5ad2eeSDavid S. Miller 	fl6.flowi6_mark = mark;
12053a5ad2eeSDavid S. Miller 	fl6.flowi6_flags = 0;
12063a5ad2eeSDavid S. Miller 	fl6.daddr = iph->daddr;
12073a5ad2eeSDavid S. Miller 	fl6.saddr = iph->saddr;
12083a5ad2eeSDavid S. Miller 	fl6.flowlabel = (*(__be32 *) iph) & IPV6_FLOWINFO_MASK;
12093a5ad2eeSDavid S. Miller 
12103a5ad2eeSDavid S. Miller 	dst = ip6_route_output(net, NULL, &fl6);
12113a5ad2eeSDavid S. Miller 	if (!dst->error)
12126700c270SDavid S. Miller 		rt6_do_redirect(dst, NULL, skb);
12133a5ad2eeSDavid S. Miller 	dst_release(dst);
12143a5ad2eeSDavid S. Miller }
12153a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect);
12163a5ad2eeSDavid S. Miller 
12173a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk)
12183a5ad2eeSDavid S. Miller {
12193a5ad2eeSDavid S. Miller 	ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark);
12203a5ad2eeSDavid S. Miller }
12213a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect);
12223a5ad2eeSDavid S. Miller 
12230dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst)
12241da177e4SLinus Torvalds {
12250dbaee3bSDavid S. Miller 	struct net_device *dev = dst->dev;
12260dbaee3bSDavid S. Miller 	unsigned int mtu = dst_mtu(dst);
12270dbaee3bSDavid S. Miller 	struct net *net = dev_net(dev);
12280dbaee3bSDavid S. Miller 
12291da177e4SLinus Torvalds 	mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
12301da177e4SLinus Torvalds 
12315578689aSDaniel Lezcano 	if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
12325578689aSDaniel Lezcano 		mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds 	/*
12351da177e4SLinus Torvalds 	 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
12361da177e4SLinus Torvalds 	 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
12371da177e4SLinus Torvalds 	 * IPV6_MAXPLEN is also valid and means: "any MSS,
12381da177e4SLinus Torvalds 	 * rely only on pmtu discovery"
12391da177e4SLinus Torvalds 	 */
12401da177e4SLinus Torvalds 	if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
12411da177e4SLinus Torvalds 		mtu = IPV6_MAXPLEN;
12421da177e4SLinus Torvalds 	return mtu;
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds 
1245ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst)
1246d33e4553SDavid S. Miller {
1247d33e4553SDavid S. Miller 	struct inet6_dev *idev;
1248618f9bc7SSteffen Klassert 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
1249618f9bc7SSteffen Klassert 
1250618f9bc7SSteffen Klassert 	if (mtu)
1251618f9bc7SSteffen Klassert 		return mtu;
1252618f9bc7SSteffen Klassert 
1253618f9bc7SSteffen Klassert 	mtu = IPV6_MIN_MTU;
1254d33e4553SDavid S. Miller 
1255d33e4553SDavid S. Miller 	rcu_read_lock();
1256d33e4553SDavid S. Miller 	idev = __in6_dev_get(dst->dev);
1257d33e4553SDavid S. Miller 	if (idev)
1258d33e4553SDavid S. Miller 		mtu = idev->cnf.mtu6;
1259d33e4553SDavid S. Miller 	rcu_read_unlock();
1260d33e4553SDavid S. Miller 
1261d33e4553SDavid S. Miller 	return mtu;
1262d33e4553SDavid S. Miller }
1263d33e4553SDavid S. Miller 
12643b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list;
12653b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock);
12665d0bbeebSThomas Graf 
12673b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
12681da177e4SLinus Torvalds 				  struct neighbour *neigh,
126987a11578SDavid S. Miller 				  struct flowi6 *fl6)
12701da177e4SLinus Torvalds {
127187a11578SDavid S. Miller 	struct dst_entry *dst;
12721da177e4SLinus Torvalds 	struct rt6_info *rt;
12731da177e4SLinus Torvalds 	struct inet6_dev *idev = in6_dev_get(dev);
1274c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
12751da177e4SLinus Torvalds 
127638308473SDavid S. Miller 	if (unlikely(!idev))
1277122bdf67SEric Dumazet 		return ERR_PTR(-ENODEV);
12781da177e4SLinus Torvalds 
12798b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, dev, 0, NULL);
128038308473SDavid S. Miller 	if (unlikely(!rt)) {
12811da177e4SLinus Torvalds 		in6_dev_put(idev);
128287a11578SDavid S. Miller 		dst = ERR_PTR(-ENOMEM);
12831da177e4SLinus Torvalds 		goto out;
12841da177e4SLinus Torvalds 	}
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	if (neigh)
12871da177e4SLinus Torvalds 		neigh_hold(neigh);
128814deae41SDavid S. Miller 	else {
1289f894cbf8SDavid S. Miller 		neigh = ip6_neigh_lookup(&rt->dst, NULL, &fl6->daddr);
1290b43faac6SDavid S. Miller 		if (IS_ERR(neigh)) {
1291252c3d84SRongQing.Li 			in6_dev_put(idev);
1292b43faac6SDavid S. Miller 			dst_free(&rt->dst);
1293b43faac6SDavid S. Miller 			return ERR_CAST(neigh);
1294b43faac6SDavid S. Miller 		}
129514deae41SDavid S. Miller 	}
12961da177e4SLinus Torvalds 
12978e2ec639SYan, Zheng 	rt->dst.flags |= DST_HOST;
12988e2ec639SYan, Zheng 	rt->dst.output  = ip6_output;
129997cac082SDavid S. Miller 	rt->n = neigh;
1300d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
130187a11578SDavid S. Miller 	rt->rt6i_dst.addr = fl6->daddr;
13028e2ec639SYan, Zheng 	rt->rt6i_dst.plen = 128;
13038e2ec639SYan, Zheng 	rt->rt6i_idev     = idev;
13047011687fSGao feng 	dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
13051da177e4SLinus Torvalds 
13063b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
1307d8d1f30bSChangli Gao 	rt->dst.next = icmp6_dst_gc_list;
1308d8d1f30bSChangli Gao 	icmp6_dst_gc_list = &rt->dst;
13093b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
13101da177e4SLinus Torvalds 
13115578689aSDaniel Lezcano 	fib6_force_start_gc(net);
13121da177e4SLinus Torvalds 
131387a11578SDavid S. Miller 	dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0);
131487a11578SDavid S. Miller 
13151da177e4SLinus Torvalds out:
131687a11578SDavid S. Miller 	return dst;
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
13193d0f24a7SStephen Hemminger int icmp6_dst_gc(void)
13201da177e4SLinus Torvalds {
1321e9476e95SHagen Paul Pfeifer 	struct dst_entry *dst, **pprev;
13223d0f24a7SStephen Hemminger 	int more = 0;
13231da177e4SLinus Torvalds 
13243b00944cSYOSHIFUJI Hideaki 	spin_lock_bh(&icmp6_dst_lock);
13253b00944cSYOSHIFUJI Hideaki 	pprev = &icmp6_dst_gc_list;
13265d0bbeebSThomas Graf 
13271da177e4SLinus Torvalds 	while ((dst = *pprev) != NULL) {
13281da177e4SLinus Torvalds 		if (!atomic_read(&dst->__refcnt)) {
13291da177e4SLinus Torvalds 			*pprev = dst->next;
13301da177e4SLinus Torvalds 			dst_free(dst);
13311da177e4SLinus Torvalds 		} else {
13321da177e4SLinus Torvalds 			pprev = &dst->next;
13333d0f24a7SStephen Hemminger 			++more;
13341da177e4SLinus Torvalds 		}
13351da177e4SLinus Torvalds 	}
13361da177e4SLinus Torvalds 
13373b00944cSYOSHIFUJI Hideaki 	spin_unlock_bh(&icmp6_dst_lock);
13385d0bbeebSThomas Graf 
13393d0f24a7SStephen Hemminger 	return more;
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds 
13421e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
13431e493d19SDavid S. Miller 			    void *arg)
13441e493d19SDavid S. Miller {
13451e493d19SDavid S. Miller 	struct dst_entry *dst, **pprev;
13461e493d19SDavid S. Miller 
13471e493d19SDavid S. Miller 	spin_lock_bh(&icmp6_dst_lock);
13481e493d19SDavid S. Miller 	pprev = &icmp6_dst_gc_list;
13491e493d19SDavid S. Miller 	while ((dst = *pprev) != NULL) {
13501e493d19SDavid S. Miller 		struct rt6_info *rt = (struct rt6_info *) dst;
13511e493d19SDavid S. Miller 		if (func(rt, arg)) {
13521e493d19SDavid S. Miller 			*pprev = dst->next;
13531e493d19SDavid S. Miller 			dst_free(dst);
13541e493d19SDavid S. Miller 		} else {
13551e493d19SDavid S. Miller 			pprev = &dst->next;
13561e493d19SDavid S. Miller 		}
13571e493d19SDavid S. Miller 	}
13581e493d19SDavid S. Miller 	spin_unlock_bh(&icmp6_dst_lock);
13591e493d19SDavid S. Miller }
13601e493d19SDavid S. Miller 
1361569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops)
13621da177e4SLinus Torvalds {
13631da177e4SLinus Torvalds 	unsigned long now = jiffies;
136486393e52SAlexey Dobriyan 	struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
13657019b78eSDaniel Lezcano 	int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
13667019b78eSDaniel Lezcano 	int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
13677019b78eSDaniel Lezcano 	int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
13687019b78eSDaniel Lezcano 	int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
13697019b78eSDaniel Lezcano 	unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1370fc66f95cSEric Dumazet 	int entries;
13711da177e4SLinus Torvalds 
1372fc66f95cSEric Dumazet 	entries = dst_entries_get_fast(ops);
13737019b78eSDaniel Lezcano 	if (time_after(rt_last_gc + rt_min_interval, now) &&
1374fc66f95cSEric Dumazet 	    entries <= rt_max_size)
13751da177e4SLinus Torvalds 		goto out;
13761da177e4SLinus Torvalds 
13776891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire++;
13786891a346SBenjamin Thery 	fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
13796891a346SBenjamin Thery 	net->ipv6.ip6_rt_last_gc = now;
1380fc66f95cSEric Dumazet 	entries = dst_entries_get_slow(ops);
1381fc66f95cSEric Dumazet 	if (entries < ops->gc_thresh)
13827019b78eSDaniel Lezcano 		net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
13831da177e4SLinus Torvalds out:
13847019b78eSDaniel Lezcano 	net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1385fc66f95cSEric Dumazet 	return entries > rt_max_size;
13861da177e4SLinus Torvalds }
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree,
13891da177e4SLinus Torvalds    but results in cleaner routing tables.
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds    Remove it only when all the things will work!
13921da177e4SLinus Torvalds  */
13931da177e4SLinus Torvalds 
13946b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst)
13951da177e4SLinus Torvalds {
13965170ae82SDavid S. Miller 	int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1397a02e4b7dSDavid S. Miller 	if (hoplimit == 0) {
13986b75d090SYOSHIFUJI Hideaki 		struct net_device *dev = dst->dev;
1399c68f24ccSEric Dumazet 		struct inet6_dev *idev;
1400c68f24ccSEric Dumazet 
1401c68f24ccSEric Dumazet 		rcu_read_lock();
1402c68f24ccSEric Dumazet 		idev = __in6_dev_get(dev);
1403c68f24ccSEric Dumazet 		if (idev)
14041da177e4SLinus Torvalds 			hoplimit = idev->cnf.hop_limit;
1405c68f24ccSEric Dumazet 		else
140653b7997fSYOSHIFUJI Hideaki 			hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1407c68f24ccSEric Dumazet 		rcu_read_unlock();
14081da177e4SLinus Torvalds 	}
14091da177e4SLinus Torvalds 	return hoplimit;
14101da177e4SLinus Torvalds }
1411abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit);
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds /*
14141da177e4SLinus Torvalds  *
14151da177e4SLinus Torvalds  */
14161da177e4SLinus Torvalds 
141786872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg)
14181da177e4SLinus Torvalds {
14191da177e4SLinus Torvalds 	int err;
14205578689aSDaniel Lezcano 	struct net *net = cfg->fc_nlinfo.nl_net;
14211da177e4SLinus Torvalds 	struct rt6_info *rt = NULL;
14221da177e4SLinus Torvalds 	struct net_device *dev = NULL;
14231da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
1424c71099acSThomas Graf 	struct fib6_table *table;
14251da177e4SLinus Torvalds 	int addr_type;
14261da177e4SLinus Torvalds 
142786872cb5SThomas Graf 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
14281da177e4SLinus Torvalds 		return -EINVAL;
14291da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES
143086872cb5SThomas Graf 	if (cfg->fc_src_len)
14311da177e4SLinus Torvalds 		return -EINVAL;
14321da177e4SLinus Torvalds #endif
143386872cb5SThomas Graf 	if (cfg->fc_ifindex) {
14341da177e4SLinus Torvalds 		err = -ENODEV;
14355578689aSDaniel Lezcano 		dev = dev_get_by_index(net, cfg->fc_ifindex);
14361da177e4SLinus Torvalds 		if (!dev)
14371da177e4SLinus Torvalds 			goto out;
14381da177e4SLinus Torvalds 		idev = in6_dev_get(dev);
14391da177e4SLinus Torvalds 		if (!idev)
14401da177e4SLinus Torvalds 			goto out;
14411da177e4SLinus Torvalds 	}
14421da177e4SLinus Torvalds 
144386872cb5SThomas Graf 	if (cfg->fc_metric == 0)
144486872cb5SThomas Graf 		cfg->fc_metric = IP6_RT_PRIO_USER;
14451da177e4SLinus Torvalds 
1446c71099acSThomas Graf 	err = -ENOBUFS;
144738308473SDavid S. Miller 	if (cfg->fc_nlinfo.nlh &&
1448d71314b4SMatti Vaittinen 	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
1449d71314b4SMatti Vaittinen 		table = fib6_get_table(net, cfg->fc_table);
145038308473SDavid S. Miller 		if (!table) {
1451f3213831SJoe Perches 			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
1452d71314b4SMatti Vaittinen 			table = fib6_new_table(net, cfg->fc_table);
1453d71314b4SMatti Vaittinen 		}
1454d71314b4SMatti Vaittinen 	} else {
1455d71314b4SMatti Vaittinen 		table = fib6_new_table(net, cfg->fc_table);
1456d71314b4SMatti Vaittinen 	}
145738308473SDavid S. Miller 
145838308473SDavid S. Miller 	if (!table)
1459c71099acSThomas Graf 		goto out;
1460c71099acSThomas Graf 
14618b96d22dSDavid S. Miller 	rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table);
14621da177e4SLinus Torvalds 
146338308473SDavid S. Miller 	if (!rt) {
14641da177e4SLinus Torvalds 		err = -ENOMEM;
14651da177e4SLinus Torvalds 		goto out;
14661da177e4SLinus Torvalds 	}
14671da177e4SLinus Torvalds 
14681716a961SGao feng 	if (cfg->fc_flags & RTF_EXPIRES)
14691716a961SGao feng 		rt6_set_expires(rt, jiffies +
14701716a961SGao feng 				clock_t_to_jiffies(cfg->fc_expires));
14711716a961SGao feng 	else
14721716a961SGao feng 		rt6_clean_expires(rt);
14731da177e4SLinus Torvalds 
147486872cb5SThomas Graf 	if (cfg->fc_protocol == RTPROT_UNSPEC)
147586872cb5SThomas Graf 		cfg->fc_protocol = RTPROT_BOOT;
147686872cb5SThomas Graf 	rt->rt6i_protocol = cfg->fc_protocol;
147786872cb5SThomas Graf 
147886872cb5SThomas Graf 	addr_type = ipv6_addr_type(&cfg->fc_dst);
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 	if (addr_type & IPV6_ADDR_MULTICAST)
1481d8d1f30bSChangli Gao 		rt->dst.input = ip6_mc_input;
1482ab79ad14SMaciej Żenczykowski 	else if (cfg->fc_flags & RTF_LOCAL)
1483ab79ad14SMaciej Żenczykowski 		rt->dst.input = ip6_input;
14841da177e4SLinus Torvalds 	else
1485d8d1f30bSChangli Gao 		rt->dst.input = ip6_forward;
14861da177e4SLinus Torvalds 
1487d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
14881da177e4SLinus Torvalds 
148986872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
149086872cb5SThomas Graf 	rt->rt6i_dst.plen = cfg->fc_dst_len;
14911da177e4SLinus Torvalds 	if (rt->rt6i_dst.plen == 128)
149211d53b49SDavid S. Miller 	       rt->dst.flags |= DST_HOST;
14931da177e4SLinus Torvalds 
14948e2ec639SYan, Zheng 	if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) {
14958e2ec639SYan, Zheng 		u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
14968e2ec639SYan, Zheng 		if (!metrics) {
14978e2ec639SYan, Zheng 			err = -ENOMEM;
14988e2ec639SYan, Zheng 			goto out;
14998e2ec639SYan, Zheng 		}
15008e2ec639SYan, Zheng 		dst_init_metrics(&rt->dst, metrics, 0);
15018e2ec639SYan, Zheng 	}
15021da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
150386872cb5SThomas Graf 	ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
150486872cb5SThomas Graf 	rt->rt6i_src.plen = cfg->fc_src_len;
15051da177e4SLinus Torvalds #endif
15061da177e4SLinus Torvalds 
150786872cb5SThomas Graf 	rt->rt6i_metric = cfg->fc_metric;
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 	/* We cannot add true routes via loopback here,
15101da177e4SLinus Torvalds 	   they would result in kernel looping; promote them to reject routes
15111da177e4SLinus Torvalds 	 */
151286872cb5SThomas Graf 	if ((cfg->fc_flags & RTF_REJECT) ||
151338308473SDavid S. Miller 	    (dev && (dev->flags & IFF_LOOPBACK) &&
151438308473SDavid S. Miller 	     !(addr_type & IPV6_ADDR_LOOPBACK) &&
151538308473SDavid S. Miller 	     !(cfg->fc_flags & RTF_LOCAL))) {
15161da177e4SLinus Torvalds 		/* hold loopback dev/idev if we haven't done so. */
15175578689aSDaniel Lezcano 		if (dev != net->loopback_dev) {
15181da177e4SLinus Torvalds 			if (dev) {
15191da177e4SLinus Torvalds 				dev_put(dev);
15201da177e4SLinus Torvalds 				in6_dev_put(idev);
15211da177e4SLinus Torvalds 			}
15225578689aSDaniel Lezcano 			dev = net->loopback_dev;
15231da177e4SLinus Torvalds 			dev_hold(dev);
15241da177e4SLinus Torvalds 			idev = in6_dev_get(dev);
15251da177e4SLinus Torvalds 			if (!idev) {
15261da177e4SLinus Torvalds 				err = -ENODEV;
15271da177e4SLinus Torvalds 				goto out;
15281da177e4SLinus Torvalds 			}
15291da177e4SLinus Torvalds 		}
1530d8d1f30bSChangli Gao 		rt->dst.output = ip6_pkt_discard_out;
1531d8d1f30bSChangli Gao 		rt->dst.input = ip6_pkt_discard;
15321da177e4SLinus Torvalds 		rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1533ef2c7d7bSNicolas Dichtel 		switch (cfg->fc_type) {
1534ef2c7d7bSNicolas Dichtel 		case RTN_BLACKHOLE:
1535ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EINVAL;
1536ef2c7d7bSNicolas Dichtel 			break;
1537ef2c7d7bSNicolas Dichtel 		case RTN_PROHIBIT:
1538ef2c7d7bSNicolas Dichtel 			rt->dst.error = -EACCES;
1539ef2c7d7bSNicolas Dichtel 			break;
1540b4949ab2SNicolas Dichtel 		case RTN_THROW:
1541b4949ab2SNicolas Dichtel 			rt->dst.error = -EAGAIN;
1542b4949ab2SNicolas Dichtel 			break;
1543ef2c7d7bSNicolas Dichtel 		default:
1544ef2c7d7bSNicolas Dichtel 			rt->dst.error = -ENETUNREACH;
1545ef2c7d7bSNicolas Dichtel 			break;
1546ef2c7d7bSNicolas Dichtel 		}
15471da177e4SLinus Torvalds 		goto install_route;
15481da177e4SLinus Torvalds 	}
15491da177e4SLinus Torvalds 
155086872cb5SThomas Graf 	if (cfg->fc_flags & RTF_GATEWAY) {
1551b71d1d42SEric Dumazet 		const struct in6_addr *gw_addr;
15521da177e4SLinus Torvalds 		int gwa_type;
15531da177e4SLinus Torvalds 
155486872cb5SThomas Graf 		gw_addr = &cfg->fc_gateway;
15554e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = *gw_addr;
15561da177e4SLinus Torvalds 		gwa_type = ipv6_addr_type(gw_addr);
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 		if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
15591da177e4SLinus Torvalds 			struct rt6_info *grt;
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 			/* IPv6 strictly inhibits using not link-local
15621da177e4SLinus Torvalds 			   addresses as nexthop address.
15631da177e4SLinus Torvalds 			   Otherwise, router will not able to send redirects.
15641da177e4SLinus Torvalds 			   It is very good, but in some (rare!) circumstances
15651da177e4SLinus Torvalds 			   (SIT, PtP, NBMA NOARP links) it is handy to allow
15661da177e4SLinus Torvalds 			   some exceptions. --ANK
15671da177e4SLinus Torvalds 			 */
15681da177e4SLinus Torvalds 			err = -EINVAL;
15691da177e4SLinus Torvalds 			if (!(gwa_type & IPV6_ADDR_UNICAST))
15701da177e4SLinus Torvalds 				goto out;
15711da177e4SLinus Torvalds 
15725578689aSDaniel Lezcano 			grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 			err = -EHOSTUNREACH;
157538308473SDavid S. Miller 			if (!grt)
15761da177e4SLinus Torvalds 				goto out;
15771da177e4SLinus Torvalds 			if (dev) {
1578d1918542SDavid S. Miller 				if (dev != grt->dst.dev) {
1579d8d1f30bSChangli Gao 					dst_release(&grt->dst);
15801da177e4SLinus Torvalds 					goto out;
15811da177e4SLinus Torvalds 				}
15821da177e4SLinus Torvalds 			} else {
1583d1918542SDavid S. Miller 				dev = grt->dst.dev;
15841da177e4SLinus Torvalds 				idev = grt->rt6i_idev;
15851da177e4SLinus Torvalds 				dev_hold(dev);
15861da177e4SLinus Torvalds 				in6_dev_hold(grt->rt6i_idev);
15871da177e4SLinus Torvalds 			}
15881da177e4SLinus Torvalds 			if (!(grt->rt6i_flags & RTF_GATEWAY))
15891da177e4SLinus Torvalds 				err = 0;
1590d8d1f30bSChangli Gao 			dst_release(&grt->dst);
15911da177e4SLinus Torvalds 
15921da177e4SLinus Torvalds 			if (err)
15931da177e4SLinus Torvalds 				goto out;
15941da177e4SLinus Torvalds 		}
15951da177e4SLinus Torvalds 		err = -EINVAL;
159638308473SDavid S. Miller 		if (!dev || (dev->flags & IFF_LOOPBACK))
15971da177e4SLinus Torvalds 			goto out;
15981da177e4SLinus Torvalds 	}
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds 	err = -ENODEV;
160138308473SDavid S. Miller 	if (!dev)
16021da177e4SLinus Torvalds 		goto out;
16031da177e4SLinus Torvalds 
1604c3968a85SDaniel Walter 	if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
1605c3968a85SDaniel Walter 		if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
1606c3968a85SDaniel Walter 			err = -EINVAL;
1607c3968a85SDaniel Walter 			goto out;
1608c3968a85SDaniel Walter 		}
16094e3fd7a0SAlexey Dobriyan 		rt->rt6i_prefsrc.addr = cfg->fc_prefsrc;
1610c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 128;
1611c3968a85SDaniel Walter 	} else
1612c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
1613c3968a85SDaniel Walter 
161486872cb5SThomas Graf 	if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
16158ade06c6SDavid S. Miller 		err = rt6_bind_neighbour(rt, dev);
1616f83c7790SDavid S. Miller 		if (err)
16171da177e4SLinus Torvalds 			goto out;
16181da177e4SLinus Torvalds 	}
16191da177e4SLinus Torvalds 
162086872cb5SThomas Graf 	rt->rt6i_flags = cfg->fc_flags;
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds install_route:
162386872cb5SThomas Graf 	if (cfg->fc_mx) {
162486872cb5SThomas Graf 		struct nlattr *nla;
162586872cb5SThomas Graf 		int remaining;
16261da177e4SLinus Torvalds 
162786872cb5SThomas Graf 		nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
16288f4c1f9bSThomas Graf 			int type = nla_type(nla);
162986872cb5SThomas Graf 
163086872cb5SThomas Graf 			if (type) {
163186872cb5SThomas Graf 				if (type > RTAX_MAX) {
16321da177e4SLinus Torvalds 					err = -EINVAL;
16331da177e4SLinus Torvalds 					goto out;
16341da177e4SLinus Torvalds 				}
163586872cb5SThomas Graf 
1636defb3519SDavid S. Miller 				dst_metric_set(&rt->dst, type, nla_get_u32(nla));
16371da177e4SLinus Torvalds 			}
16381da177e4SLinus Torvalds 		}
16391da177e4SLinus Torvalds 	}
16401da177e4SLinus Torvalds 
1641d8d1f30bSChangli Gao 	rt->dst.dev = dev;
16421da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
1643c71099acSThomas Graf 	rt->rt6i_table = table;
164463152fc0SDaniel Lezcano 
1645c346dca1SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = dev_net(dev);
164663152fc0SDaniel Lezcano 
164786872cb5SThomas Graf 	return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds out:
16501da177e4SLinus Torvalds 	if (dev)
16511da177e4SLinus Torvalds 		dev_put(dev);
16521da177e4SLinus Torvalds 	if (idev)
16531da177e4SLinus Torvalds 		in6_dev_put(idev);
16541da177e4SLinus Torvalds 	if (rt)
1655d8d1f30bSChangli Gao 		dst_free(&rt->dst);
16561da177e4SLinus Torvalds 	return err;
16571da177e4SLinus Torvalds }
16581da177e4SLinus Torvalds 
165986872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
16601da177e4SLinus Torvalds {
16611da177e4SLinus Torvalds 	int err;
1662c71099acSThomas Graf 	struct fib6_table *table;
1663d1918542SDavid S. Miller 	struct net *net = dev_net(rt->dst.dev);
16641da177e4SLinus Torvalds 
16656825a26cSGao feng 	if (rt == net->ipv6.ip6_null_entry) {
16666825a26cSGao feng 		err = -ENOENT;
16676825a26cSGao feng 		goto out;
16686825a26cSGao feng 	}
16696c813a72SPatrick McHardy 
1670c71099acSThomas Graf 	table = rt->rt6i_table;
1671c71099acSThomas Graf 	write_lock_bh(&table->tb6_lock);
167286872cb5SThomas Graf 	err = fib6_del(rt, info);
1673c71099acSThomas Graf 	write_unlock_bh(&table->tb6_lock);
16741da177e4SLinus Torvalds 
16756825a26cSGao feng out:
16766825a26cSGao feng 	dst_release(&rt->dst);
16771da177e4SLinus Torvalds 	return err;
16781da177e4SLinus Torvalds }
16791da177e4SLinus Torvalds 
1680e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt)
1681e0a1ad73SThomas Graf {
16824d1169c1SDenis V. Lunev 	struct nl_info info = {
1683d1918542SDavid S. Miller 		.nl_net = dev_net(rt->dst.dev),
16844d1169c1SDenis V. Lunev 	};
1685528c4cebSDenis V. Lunev 	return __ip6_del_rt(rt, &info);
1686e0a1ad73SThomas Graf }
1687e0a1ad73SThomas Graf 
168886872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg)
16891da177e4SLinus Torvalds {
1690c71099acSThomas Graf 	struct fib6_table *table;
16911da177e4SLinus Torvalds 	struct fib6_node *fn;
16921da177e4SLinus Torvalds 	struct rt6_info *rt;
16931da177e4SLinus Torvalds 	int err = -ESRCH;
16941da177e4SLinus Torvalds 
16955578689aSDaniel Lezcano 	table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
169638308473SDavid S. Miller 	if (!table)
1697c71099acSThomas Graf 		return err;
16981da177e4SLinus Torvalds 
1699c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
1700c71099acSThomas Graf 
1701c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root,
170286872cb5SThomas Graf 			 &cfg->fc_dst, cfg->fc_dst_len,
170386872cb5SThomas Graf 			 &cfg->fc_src, cfg->fc_src_len);
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	if (fn) {
1706d8d1f30bSChangli Gao 		for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
170786872cb5SThomas Graf 			if (cfg->fc_ifindex &&
1708d1918542SDavid S. Miller 			    (!rt->dst.dev ||
1709d1918542SDavid S. Miller 			     rt->dst.dev->ifindex != cfg->fc_ifindex))
17101da177e4SLinus Torvalds 				continue;
171186872cb5SThomas Graf 			if (cfg->fc_flags & RTF_GATEWAY &&
171286872cb5SThomas Graf 			    !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
17131da177e4SLinus Torvalds 				continue;
171486872cb5SThomas Graf 			if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
17151da177e4SLinus Torvalds 				continue;
1716d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
1717c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
17181da177e4SLinus Torvalds 
171986872cb5SThomas Graf 			return __ip6_del_rt(rt, &cfg->fc_nlinfo);
17201da177e4SLinus Torvalds 		}
17211da177e4SLinus Torvalds 	}
1722c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 	return err;
17251da177e4SLinus Torvalds }
17261da177e4SLinus Torvalds 
17276700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb)
1728a6279458SYOSHIFUJI Hideaki {
1729e8599ff4SDavid S. Miller 	struct net *net = dev_net(skb->dev);
1730a6279458SYOSHIFUJI Hideaki 	struct netevent_redirect netevent;
1731e8599ff4SDavid S. Miller 	struct rt6_info *rt, *nrt = NULL;
1732e8599ff4SDavid S. Miller 	const struct in6_addr *target;
1733e8599ff4SDavid S. Miller 	struct ndisc_options ndopts;
17346e157b6aSDavid S. Miller 	const struct in6_addr *dest;
17356e157b6aSDavid S. Miller 	struct neighbour *old_neigh;
1736e8599ff4SDavid S. Miller 	struct inet6_dev *in6_dev;
1737e8599ff4SDavid S. Miller 	struct neighbour *neigh;
1738e8599ff4SDavid S. Miller 	struct icmp6hdr *icmph;
17396e157b6aSDavid S. Miller 	int optlen, on_link;
17406e157b6aSDavid S. Miller 	u8 *lladdr;
1741e8599ff4SDavid S. Miller 
1742e8599ff4SDavid S. Miller 	optlen = skb->tail - skb->transport_header;
1743e8599ff4SDavid S. Miller 	optlen -= sizeof(struct icmp6hdr) + 2 * sizeof(struct in6_addr);
1744e8599ff4SDavid S. Miller 
1745e8599ff4SDavid S. Miller 	if (optlen < 0) {
17466e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: packet too short\n");
1747e8599ff4SDavid S. Miller 		return;
1748e8599ff4SDavid S. Miller 	}
1749e8599ff4SDavid S. Miller 
1750e8599ff4SDavid S. Miller 	icmph = icmp6_hdr(skb);
1751e8599ff4SDavid S. Miller 	target = (const struct in6_addr *) (icmph + 1);
1752e8599ff4SDavid S. Miller 	dest = target + 1;
1753e8599ff4SDavid S. Miller 
1754e8599ff4SDavid S. Miller 	if (ipv6_addr_is_multicast(dest)) {
17556e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n");
1756e8599ff4SDavid S. Miller 		return;
1757e8599ff4SDavid S. Miller 	}
1758e8599ff4SDavid S. Miller 
17596e157b6aSDavid S. Miller 	on_link = 0;
1760e8599ff4SDavid S. Miller 	if (ipv6_addr_equal(dest, target)) {
1761e8599ff4SDavid S. Miller 		on_link = 1;
1762e8599ff4SDavid S. Miller 	} else if (ipv6_addr_type(target) !=
1763e8599ff4SDavid S. Miller 		   (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) {
17646e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n");
1765e8599ff4SDavid S. Miller 		return;
1766e8599ff4SDavid S. Miller 	}
1767e8599ff4SDavid S. Miller 
1768e8599ff4SDavid S. Miller 	in6_dev = __in6_dev_get(skb->dev);
1769e8599ff4SDavid S. Miller 	if (!in6_dev)
1770e8599ff4SDavid S. Miller 		return;
1771e8599ff4SDavid S. Miller 	if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects)
1772e8599ff4SDavid S. Miller 		return;
1773e8599ff4SDavid S. Miller 
1774e8599ff4SDavid S. Miller 	/* RFC2461 8.1:
1775e8599ff4SDavid S. Miller 	 *	The IP source address of the Redirect MUST be the same as the current
1776e8599ff4SDavid S. Miller 	 *	first-hop router for the specified ICMP Destination Address.
1777e8599ff4SDavid S. Miller 	 */
1778e8599ff4SDavid S. Miller 
1779e8599ff4SDavid S. Miller 	if (!ndisc_parse_options((u8*)(dest + 1), optlen, &ndopts)) {
1780e8599ff4SDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: invalid ND options\n");
1781e8599ff4SDavid S. Miller 		return;
1782e8599ff4SDavid S. Miller 	}
17836e157b6aSDavid S. Miller 
17846e157b6aSDavid S. Miller 	lladdr = NULL;
1785e8599ff4SDavid S. Miller 	if (ndopts.nd_opts_tgt_lladdr) {
1786e8599ff4SDavid S. Miller 		lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr,
1787e8599ff4SDavid S. Miller 					     skb->dev);
1788e8599ff4SDavid S. Miller 		if (!lladdr) {
1789e8599ff4SDavid S. Miller 			net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n");
1790e8599ff4SDavid S. Miller 			return;
1791e8599ff4SDavid S. Miller 		}
1792e8599ff4SDavid S. Miller 	}
1793e8599ff4SDavid S. Miller 
17946e157b6aSDavid S. Miller 	rt = (struct rt6_info *) dst;
17956e157b6aSDavid S. Miller 	if (rt == net->ipv6.ip6_null_entry) {
17966e157b6aSDavid S. Miller 		net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n");
17976e157b6aSDavid S. Miller 		return;
17986e157b6aSDavid S. Miller 	}
17996e157b6aSDavid S. Miller 
18006e157b6aSDavid S. Miller 	/* Redirect received -> path was valid.
18016e157b6aSDavid S. Miller 	 * Look, redirects are sent only in response to data packets,
18026e157b6aSDavid S. Miller 	 * so that this nexthop apparently is reachable. --ANK
18036e157b6aSDavid S. Miller 	 */
18046e157b6aSDavid S. Miller 	dst_confirm(&rt->dst);
18056e157b6aSDavid S. Miller 
1806e8599ff4SDavid S. Miller 	neigh = __neigh_lookup(&nd_tbl, target, skb->dev, 1);
1807e8599ff4SDavid S. Miller 	if (!neigh)
1808e8599ff4SDavid S. Miller 		return;
1809e8599ff4SDavid S. Miller 
18106e157b6aSDavid S. Miller 	/* Duplicate redirect: silently ignore. */
18116e157b6aSDavid S. Miller 	old_neigh = rt->n;
18126e157b6aSDavid S. Miller 	if (neigh == old_neigh)
1813a6279458SYOSHIFUJI Hideaki 		goto out;
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 	/*
18161da177e4SLinus Torvalds 	 *	We have finally decided to accept it.
18171da177e4SLinus Torvalds 	 */
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds 	neigh_update(neigh, lladdr, NUD_STALE,
18201da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_WEAK_OVERRIDE|
18211da177e4SLinus Torvalds 		     NEIGH_UPDATE_F_OVERRIDE|
18221da177e4SLinus Torvalds 		     (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
18231da177e4SLinus Torvalds 				     NEIGH_UPDATE_F_ISROUTER))
18241da177e4SLinus Torvalds 		     );
18251da177e4SLinus Torvalds 
182621efcfa0SEric Dumazet 	nrt = ip6_rt_copy(rt, dest);
182738308473SDavid S. Miller 	if (!nrt)
18281da177e4SLinus Torvalds 		goto out;
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 	nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
18311da177e4SLinus Torvalds 	if (on_link)
18321da177e4SLinus Torvalds 		nrt->rt6i_flags &= ~RTF_GATEWAY;
18331da177e4SLinus Torvalds 
18344e3fd7a0SAlexey Dobriyan 	nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key;
183597cac082SDavid S. Miller 	nrt->n = neigh_clone(neigh);
18361da177e4SLinus Torvalds 
183740e22e8fSThomas Graf 	if (ip6_ins_rt(nrt))
18381da177e4SLinus Torvalds 		goto out;
18391da177e4SLinus Torvalds 
1840d8d1f30bSChangli Gao 	netevent.old = &rt->dst;
18411d248b1cSDavid S. Miller 	netevent.old_neigh = old_neigh;
1842d8d1f30bSChangli Gao 	netevent.new = &nrt->dst;
18431d248b1cSDavid S. Miller 	netevent.new_neigh = neigh;
18441d248b1cSDavid S. Miller 	netevent.daddr = dest;
18458d71740cSTom Tucker 	call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
18468d71740cSTom Tucker 
18471da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE) {
18486e157b6aSDavid S. Miller 		rt = (struct rt6_info *) dst_clone(&rt->dst);
1849e0a1ad73SThomas Graf 		ip6_del_rt(rt);
18501da177e4SLinus Torvalds 	}
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds out:
1853e8599ff4SDavid S. Miller 	neigh_release(neigh);
18546e157b6aSDavid S. Miller }
18556e157b6aSDavid S. Miller 
18561da177e4SLinus Torvalds /*
18571da177e4SLinus Torvalds  *	Misc support functions
18581da177e4SLinus Torvalds  */
18591da177e4SLinus Torvalds 
18601716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
186121efcfa0SEric Dumazet 				    const struct in6_addr *dest)
18621da177e4SLinus Torvalds {
1863d1918542SDavid S. Miller 	struct net *net = dev_net(ort->dst.dev);
18648b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0,
18658b96d22dSDavid S. Miller 					    ort->rt6i_table);
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	if (rt) {
1868d8d1f30bSChangli Gao 		rt->dst.input = ort->dst.input;
1869d8d1f30bSChangli Gao 		rt->dst.output = ort->dst.output;
18708e2ec639SYan, Zheng 		rt->dst.flags |= DST_HOST;
18711da177e4SLinus Torvalds 
18724e3fd7a0SAlexey Dobriyan 		rt->rt6i_dst.addr = *dest;
18738e2ec639SYan, Zheng 		rt->rt6i_dst.plen = 128;
1874defb3519SDavid S. Miller 		dst_copy_metrics(&rt->dst, &ort->dst);
1875d8d1f30bSChangli Gao 		rt->dst.error = ort->dst.error;
18761da177e4SLinus Torvalds 		rt->rt6i_idev = ort->rt6i_idev;
18771da177e4SLinus Torvalds 		if (rt->rt6i_idev)
18781da177e4SLinus Torvalds 			in6_dev_hold(rt->rt6i_idev);
1879d8d1f30bSChangli Gao 		rt->dst.lastuse = jiffies;
18801da177e4SLinus Torvalds 
18814e3fd7a0SAlexey Dobriyan 		rt->rt6i_gateway = ort->rt6i_gateway;
18821716a961SGao feng 		rt->rt6i_flags = ort->rt6i_flags;
18831716a961SGao feng 		if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ==
18841716a961SGao feng 		    (RTF_DEFAULT | RTF_ADDRCONF))
18851716a961SGao feng 			rt6_set_from(rt, ort);
18861716a961SGao feng 		else
18871716a961SGao feng 			rt6_clean_expires(rt);
18881da177e4SLinus Torvalds 		rt->rt6i_metric = 0;
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
18911da177e4SLinus Torvalds 		memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
18921da177e4SLinus Torvalds #endif
18930f6c6392SFlorian Westphal 		memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key));
1894c71099acSThomas Graf 		rt->rt6i_table = ort->rt6i_table;
18951da177e4SLinus Torvalds 	}
18961da177e4SLinus Torvalds 	return rt;
18971da177e4SLinus Torvalds }
18981da177e4SLinus Torvalds 
189970ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
1900efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net,
1901b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1902b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex)
190370ceb4f5SYOSHIFUJI Hideaki {
190470ceb4f5SYOSHIFUJI Hideaki 	struct fib6_node *fn;
190570ceb4f5SYOSHIFUJI Hideaki 	struct rt6_info *rt = NULL;
1906c71099acSThomas Graf 	struct fib6_table *table;
190770ceb4f5SYOSHIFUJI Hideaki 
1908efa2cea0SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_INFO);
190938308473SDavid S. Miller 	if (!table)
1910c71099acSThomas Graf 		return NULL;
1911c71099acSThomas Graf 
19125744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1913c71099acSThomas Graf 	fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
191470ceb4f5SYOSHIFUJI Hideaki 	if (!fn)
191570ceb4f5SYOSHIFUJI Hideaki 		goto out;
191670ceb4f5SYOSHIFUJI Hideaki 
1917d8d1f30bSChangli Gao 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1918d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != ifindex)
191970ceb4f5SYOSHIFUJI Hideaki 			continue;
192070ceb4f5SYOSHIFUJI Hideaki 		if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
192170ceb4f5SYOSHIFUJI Hideaki 			continue;
192270ceb4f5SYOSHIFUJI Hideaki 		if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
192370ceb4f5SYOSHIFUJI Hideaki 			continue;
1924d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
192570ceb4f5SYOSHIFUJI Hideaki 		break;
192670ceb4f5SYOSHIFUJI Hideaki 	}
192770ceb4f5SYOSHIFUJI Hideaki out:
19285744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
192970ceb4f5SYOSHIFUJI Hideaki 	return rt;
193070ceb4f5SYOSHIFUJI Hideaki }
193170ceb4f5SYOSHIFUJI Hideaki 
1932efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net,
1933b71d1d42SEric Dumazet 					   const struct in6_addr *prefix, int prefixlen,
1934b71d1d42SEric Dumazet 					   const struct in6_addr *gwaddr, int ifindex,
193595c96174SEric Dumazet 					   unsigned int pref)
193670ceb4f5SYOSHIFUJI Hideaki {
193786872cb5SThomas Graf 	struct fib6_config cfg = {
193886872cb5SThomas Graf 		.fc_table	= RT6_TABLE_INFO,
1939238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
194086872cb5SThomas Graf 		.fc_ifindex	= ifindex,
194186872cb5SThomas Graf 		.fc_dst_len	= prefixlen,
194286872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
194386872cb5SThomas Graf 				  RTF_UP | RTF_PREF(pref),
194415e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
1945efa2cea0SDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1946efa2cea0SDaniel Lezcano 		.fc_nlinfo.nl_net = net,
194786872cb5SThomas Graf 	};
194870ceb4f5SYOSHIFUJI Hideaki 
19494e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *prefix;
19504e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
195186872cb5SThomas Graf 
1952e317da96SYOSHIFUJI Hideaki 	/* We should treat it as a default route if prefix length is 0. */
1953e317da96SYOSHIFUJI Hideaki 	if (!prefixlen)
195486872cb5SThomas Graf 		cfg.fc_flags |= RTF_DEFAULT;
195570ceb4f5SYOSHIFUJI Hideaki 
195686872cb5SThomas Graf 	ip6_route_add(&cfg);
195770ceb4f5SYOSHIFUJI Hideaki 
1958efa2cea0SDaniel Lezcano 	return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
195970ceb4f5SYOSHIFUJI Hideaki }
196070ceb4f5SYOSHIFUJI Hideaki #endif
196170ceb4f5SYOSHIFUJI Hideaki 
1962b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev)
19631da177e4SLinus Torvalds {
19641da177e4SLinus Torvalds 	struct rt6_info *rt;
1965c71099acSThomas Graf 	struct fib6_table *table;
19661da177e4SLinus Torvalds 
1967c346dca1SYOSHIFUJI Hideaki 	table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
196838308473SDavid S. Miller 	if (!table)
1969c71099acSThomas Graf 		return NULL;
19701da177e4SLinus Torvalds 
19715744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
1972d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1973d1918542SDavid S. Miller 		if (dev == rt->dst.dev &&
1974045927ffSYOSHIFUJI Hideaki 		    ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
19751da177e4SLinus Torvalds 		    ipv6_addr_equal(&rt->rt6i_gateway, addr))
19761da177e4SLinus Torvalds 			break;
19771da177e4SLinus Torvalds 	}
19781da177e4SLinus Torvalds 	if (rt)
1979d8d1f30bSChangli Gao 		dst_hold(&rt->dst);
19805744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
19811da177e4SLinus Torvalds 	return rt;
19821da177e4SLinus Torvalds }
19831da177e4SLinus Torvalds 
1984b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr,
1985ebacaaa0SYOSHIFUJI Hideaki 				     struct net_device *dev,
1986ebacaaa0SYOSHIFUJI Hideaki 				     unsigned int pref)
19871da177e4SLinus Torvalds {
198886872cb5SThomas Graf 	struct fib6_config cfg = {
198986872cb5SThomas Graf 		.fc_table	= RT6_TABLE_DFLT,
1990238fc7eaSRami Rosen 		.fc_metric	= IP6_RT_PRIO_USER,
199186872cb5SThomas Graf 		.fc_ifindex	= dev->ifindex,
199286872cb5SThomas Graf 		.fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
199386872cb5SThomas Graf 				  RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
199415e47304SEric W. Biederman 		.fc_nlinfo.portid = 0,
19955578689aSDaniel Lezcano 		.fc_nlinfo.nlh = NULL,
1996c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
199786872cb5SThomas Graf 	};
19981da177e4SLinus Torvalds 
19994e3fd7a0SAlexey Dobriyan 	cfg.fc_gateway = *gwaddr;
20001da177e4SLinus Torvalds 
200186872cb5SThomas Graf 	ip6_route_add(&cfg);
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 	return rt6_get_dflt_router(gwaddr, dev);
20041da177e4SLinus Torvalds }
20051da177e4SLinus Torvalds 
20067b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net)
20071da177e4SLinus Torvalds {
20081da177e4SLinus Torvalds 	struct rt6_info *rt;
2009c71099acSThomas Graf 	struct fib6_table *table;
2010c71099acSThomas Graf 
2011c71099acSThomas Graf 	/* NOTE: Keep consistent with rt6_get_dflt_router */
20127b4da532SDaniel Lezcano 	table = fib6_get_table(net, RT6_TABLE_DFLT);
201338308473SDavid S. Miller 	if (!table)
2014c71099acSThomas Graf 		return;
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds restart:
2017c71099acSThomas Graf 	read_lock_bh(&table->tb6_lock);
2018d8d1f30bSChangli Gao 	for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
20191da177e4SLinus Torvalds 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
2020d8d1f30bSChangli Gao 			dst_hold(&rt->dst);
2021c71099acSThomas Graf 			read_unlock_bh(&table->tb6_lock);
2022e0a1ad73SThomas Graf 			ip6_del_rt(rt);
20231da177e4SLinus Torvalds 			goto restart;
20241da177e4SLinus Torvalds 		}
20251da177e4SLinus Torvalds 	}
2026c71099acSThomas Graf 	read_unlock_bh(&table->tb6_lock);
20271da177e4SLinus Torvalds }
20281da177e4SLinus Torvalds 
20295578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net,
20305578689aSDaniel Lezcano 				 struct in6_rtmsg *rtmsg,
203186872cb5SThomas Graf 				 struct fib6_config *cfg)
203286872cb5SThomas Graf {
203386872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
203486872cb5SThomas Graf 
203586872cb5SThomas Graf 	cfg->fc_table = RT6_TABLE_MAIN;
203686872cb5SThomas Graf 	cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
203786872cb5SThomas Graf 	cfg->fc_metric = rtmsg->rtmsg_metric;
203886872cb5SThomas Graf 	cfg->fc_expires = rtmsg->rtmsg_info;
203986872cb5SThomas Graf 	cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
204086872cb5SThomas Graf 	cfg->fc_src_len = rtmsg->rtmsg_src_len;
204186872cb5SThomas Graf 	cfg->fc_flags = rtmsg->rtmsg_flags;
204286872cb5SThomas Graf 
20435578689aSDaniel Lezcano 	cfg->fc_nlinfo.nl_net = net;
2044f1243c2dSBenjamin Thery 
20454e3fd7a0SAlexey Dobriyan 	cfg->fc_dst = rtmsg->rtmsg_dst;
20464e3fd7a0SAlexey Dobriyan 	cfg->fc_src = rtmsg->rtmsg_src;
20474e3fd7a0SAlexey Dobriyan 	cfg->fc_gateway = rtmsg->rtmsg_gateway;
204886872cb5SThomas Graf }
204986872cb5SThomas Graf 
20505578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
20511da177e4SLinus Torvalds {
205286872cb5SThomas Graf 	struct fib6_config cfg;
20531da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
20541da177e4SLinus Torvalds 	int err;
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	switch(cmd) {
20571da177e4SLinus Torvalds 	case SIOCADDRT:		/* Add a route */
20581da177e4SLinus Torvalds 	case SIOCDELRT:		/* Delete a route */
20591da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
20601da177e4SLinus Torvalds 			return -EPERM;
20611da177e4SLinus Torvalds 		err = copy_from_user(&rtmsg, arg,
20621da177e4SLinus Torvalds 				     sizeof(struct in6_rtmsg));
20631da177e4SLinus Torvalds 		if (err)
20641da177e4SLinus Torvalds 			return -EFAULT;
20651da177e4SLinus Torvalds 
20665578689aSDaniel Lezcano 		rtmsg_to_fib6_config(net, &rtmsg, &cfg);
206786872cb5SThomas Graf 
20681da177e4SLinus Torvalds 		rtnl_lock();
20691da177e4SLinus Torvalds 		switch (cmd) {
20701da177e4SLinus Torvalds 		case SIOCADDRT:
207186872cb5SThomas Graf 			err = ip6_route_add(&cfg);
20721da177e4SLinus Torvalds 			break;
20731da177e4SLinus Torvalds 		case SIOCDELRT:
207486872cb5SThomas Graf 			err = ip6_route_del(&cfg);
20751da177e4SLinus Torvalds 			break;
20761da177e4SLinus Torvalds 		default:
20771da177e4SLinus Torvalds 			err = -EINVAL;
20781da177e4SLinus Torvalds 		}
20791da177e4SLinus Torvalds 		rtnl_unlock();
20801da177e4SLinus Torvalds 
20811da177e4SLinus Torvalds 		return err;
20823ff50b79SStephen Hemminger 	}
20831da177e4SLinus Torvalds 
20841da177e4SLinus Torvalds 	return -EINVAL;
20851da177e4SLinus Torvalds }
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds /*
20881da177e4SLinus Torvalds  *	Drop the packet on the floor
20891da177e4SLinus Torvalds  */
20901da177e4SLinus Torvalds 
2091d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
20921da177e4SLinus Torvalds {
2093612f09e8SYOSHIFUJI Hideaki 	int type;
2094adf30907SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
2095612f09e8SYOSHIFUJI Hideaki 	switch (ipstats_mib_noroutes) {
2096612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_INNOROUTES:
20970660e03fSArnaldo Carvalho de Melo 		type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
209845bb0060SUlrich Weber 		if (type == IPV6_ADDR_ANY) {
20993bd653c8SDenis V. Lunev 			IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
21003bd653c8SDenis V. Lunev 				      IPSTATS_MIB_INADDRERRORS);
2101612f09e8SYOSHIFUJI Hideaki 			break;
2102612f09e8SYOSHIFUJI Hideaki 		}
2103612f09e8SYOSHIFUJI Hideaki 		/* FALLTHROUGH */
2104612f09e8SYOSHIFUJI Hideaki 	case IPSTATS_MIB_OUTNOROUTES:
21053bd653c8SDenis V. Lunev 		IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
21063bd653c8SDenis V. Lunev 			      ipstats_mib_noroutes);
2107612f09e8SYOSHIFUJI Hideaki 		break;
2108612f09e8SYOSHIFUJI Hideaki 	}
21093ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
21101da177e4SLinus Torvalds 	kfree_skb(skb);
21111da177e4SLinus Torvalds 	return 0;
21121da177e4SLinus Torvalds }
21131da177e4SLinus Torvalds 
21149ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb)
21159ce8ade0SThomas Graf {
2116612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
21179ce8ade0SThomas Graf }
21189ce8ade0SThomas Graf 
211920380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb)
21201da177e4SLinus Torvalds {
2121adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2122612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
21231da177e4SLinus Torvalds }
21241da177e4SLinus Torvalds 
21256723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES
21266723ab54SDavid S. Miller 
21279ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb)
21289ce8ade0SThomas Graf {
2129612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
21309ce8ade0SThomas Graf }
21319ce8ade0SThomas Graf 
21329ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb)
21339ce8ade0SThomas Graf {
2134adf30907SEric Dumazet 	skb->dev = skb_dst(skb)->dev;
2135612f09e8SYOSHIFUJI Hideaki 	return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
21369ce8ade0SThomas Graf }
21379ce8ade0SThomas Graf 
21386723ab54SDavid S. Miller #endif
21396723ab54SDavid S. Miller 
21401da177e4SLinus Torvalds /*
21411da177e4SLinus Torvalds  *	Allocate a dst for local (unicast / anycast) address.
21421da177e4SLinus Torvalds  */
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
21451da177e4SLinus Torvalds 				    const struct in6_addr *addr,
21468f031519SDavid S. Miller 				    bool anycast)
21471da177e4SLinus Torvalds {
2148c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
21498b96d22dSDavid S. Miller 	struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL);
2150f83c7790SDavid S. Miller 	int err;
21511da177e4SLinus Torvalds 
215238308473SDavid S. Miller 	if (!rt) {
2153f3213831SJoe Perches 		net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
21541da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
215540385653SBen Greear 	}
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds 	in6_dev_hold(idev);
21581da177e4SLinus Torvalds 
215911d53b49SDavid S. Miller 	rt->dst.flags |= DST_HOST;
2160d8d1f30bSChangli Gao 	rt->dst.input = ip6_input;
2161d8d1f30bSChangli Gao 	rt->dst.output = ip6_output;
21621da177e4SLinus Torvalds 	rt->rt6i_idev = idev;
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds 	rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
216558c4fb86SYOSHIFUJI Hideaki 	if (anycast)
216658c4fb86SYOSHIFUJI Hideaki 		rt->rt6i_flags |= RTF_ANYCAST;
216758c4fb86SYOSHIFUJI Hideaki 	else
21681da177e4SLinus Torvalds 		rt->rt6i_flags |= RTF_LOCAL;
21698ade06c6SDavid S. Miller 	err = rt6_bind_neighbour(rt, rt->dst.dev);
2170f83c7790SDavid S. Miller 	if (err) {
2171d8d1f30bSChangli Gao 		dst_free(&rt->dst);
2172f83c7790SDavid S. Miller 		return ERR_PTR(err);
21731da177e4SLinus Torvalds 	}
21741da177e4SLinus Torvalds 
21754e3fd7a0SAlexey Dobriyan 	rt->rt6i_dst.addr = *addr;
21761da177e4SLinus Torvalds 	rt->rt6i_dst.plen = 128;
21775578689aSDaniel Lezcano 	rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
21781da177e4SLinus Torvalds 
2179d8d1f30bSChangli Gao 	atomic_set(&rt->dst.__refcnt, 1);
21801da177e4SLinus Torvalds 
21811da177e4SLinus Torvalds 	return rt;
21821da177e4SLinus Torvalds }
21831da177e4SLinus Torvalds 
2184c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net,
2185c3968a85SDaniel Walter 			struct rt6_info *rt,
2186b71d1d42SEric Dumazet 			const struct in6_addr *daddr,
2187c3968a85SDaniel Walter 			unsigned int prefs,
2188c3968a85SDaniel Walter 			struct in6_addr *saddr)
2189c3968a85SDaniel Walter {
2190c3968a85SDaniel Walter 	struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt);
2191c3968a85SDaniel Walter 	int err = 0;
2192c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen)
21934e3fd7a0SAlexey Dobriyan 		*saddr = rt->rt6i_prefsrc.addr;
2194c3968a85SDaniel Walter 	else
2195c3968a85SDaniel Walter 		err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2196c3968a85SDaniel Walter 					 daddr, prefs, saddr);
2197c3968a85SDaniel Walter 	return err;
2198c3968a85SDaniel Walter }
2199c3968a85SDaniel Walter 
2200c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */
2201c3968a85SDaniel Walter struct arg_dev_net_ip {
2202c3968a85SDaniel Walter 	struct net_device *dev;
2203c3968a85SDaniel Walter 	struct net *net;
2204c3968a85SDaniel Walter 	struct in6_addr *addr;
2205c3968a85SDaniel Walter };
2206c3968a85SDaniel Walter 
2207c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg)
2208c3968a85SDaniel Walter {
2209c3968a85SDaniel Walter 	struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev;
2210c3968a85SDaniel Walter 	struct net *net = ((struct arg_dev_net_ip *)arg)->net;
2211c3968a85SDaniel Walter 	struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr;
2212c3968a85SDaniel Walter 
2213d1918542SDavid S. Miller 	if (((void *)rt->dst.dev == dev || !dev) &&
2214c3968a85SDaniel Walter 	    rt != net->ipv6.ip6_null_entry &&
2215c3968a85SDaniel Walter 	    ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) {
2216c3968a85SDaniel Walter 		/* remove prefsrc entry */
2217c3968a85SDaniel Walter 		rt->rt6i_prefsrc.plen = 0;
2218c3968a85SDaniel Walter 	}
2219c3968a85SDaniel Walter 	return 0;
2220c3968a85SDaniel Walter }
2221c3968a85SDaniel Walter 
2222c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp)
2223c3968a85SDaniel Walter {
2224c3968a85SDaniel Walter 	struct net *net = dev_net(ifp->idev->dev);
2225c3968a85SDaniel Walter 	struct arg_dev_net_ip adni = {
2226c3968a85SDaniel Walter 		.dev = ifp->idev->dev,
2227c3968a85SDaniel Walter 		.net = net,
2228c3968a85SDaniel Walter 		.addr = &ifp->addr,
2229c3968a85SDaniel Walter 	};
2230c3968a85SDaniel Walter 	fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni);
2231c3968a85SDaniel Walter }
2232c3968a85SDaniel Walter 
22338ed67789SDaniel Lezcano struct arg_dev_net {
22348ed67789SDaniel Lezcano 	struct net_device *dev;
22358ed67789SDaniel Lezcano 	struct net *net;
22368ed67789SDaniel Lezcano };
22378ed67789SDaniel Lezcano 
22381da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg)
22391da177e4SLinus Torvalds {
2240bc3ef660Sstephen hemminger 	const struct arg_dev_net *adn = arg;
2241bc3ef660Sstephen hemminger 	const struct net_device *dev = adn->dev;
22428ed67789SDaniel Lezcano 
2243d1918542SDavid S. Miller 	if ((rt->dst.dev == dev || !dev) &&
2244c159d30cSDavid S. Miller 	    rt != adn->net->ipv6.ip6_null_entry)
22451da177e4SLinus Torvalds 		return -1;
2246c159d30cSDavid S. Miller 
22471da177e4SLinus Torvalds 	return 0;
22481da177e4SLinus Torvalds }
22491da177e4SLinus Torvalds 
2250f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev)
22511da177e4SLinus Torvalds {
22528ed67789SDaniel Lezcano 	struct arg_dev_net adn = {
22538ed67789SDaniel Lezcano 		.dev = dev,
22548ed67789SDaniel Lezcano 		.net = net,
22558ed67789SDaniel Lezcano 	};
22568ed67789SDaniel Lezcano 
22578ed67789SDaniel Lezcano 	fib6_clean_all(net, fib6_ifdown, 0, &adn);
22581e493d19SDavid S. Miller 	icmp6_clean_all(fib6_ifdown, &adn);
22591da177e4SLinus Torvalds }
22601da177e4SLinus Torvalds 
226195c96174SEric Dumazet struct rt6_mtu_change_arg {
22621da177e4SLinus Torvalds 	struct net_device *dev;
226395c96174SEric Dumazet 	unsigned int mtu;
22641da177e4SLinus Torvalds };
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
22671da177e4SLinus Torvalds {
22681da177e4SLinus Torvalds 	struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
22691da177e4SLinus Torvalds 	struct inet6_dev *idev;
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds 	/* In IPv6 pmtu discovery is not optional,
22721da177e4SLinus Torvalds 	   so that RTAX_MTU lock cannot disable it.
22731da177e4SLinus Torvalds 	   We still use this lock to block changes
22741da177e4SLinus Torvalds 	   caused by addrconf/ndisc.
22751da177e4SLinus Torvalds 	*/
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds 	idev = __in6_dev_get(arg->dev);
227838308473SDavid S. Miller 	if (!idev)
22791da177e4SLinus Torvalds 		return 0;
22801da177e4SLinus Torvalds 
22811da177e4SLinus Torvalds 	/* For administrative MTU increase, there is no way to discover
22821da177e4SLinus Torvalds 	   IPv6 PMTU increase, so PMTU increase should be updated here.
22831da177e4SLinus Torvalds 	   Since RFC 1981 doesn't include administrative MTU increase
22841da177e4SLinus Torvalds 	   update PMTU increase is a MUST. (i.e. jumbo frame)
22851da177e4SLinus Torvalds 	 */
22861da177e4SLinus Torvalds 	/*
22871da177e4SLinus Torvalds 	   If new MTU is less than route PMTU, this new MTU will be the
22881da177e4SLinus Torvalds 	   lowest MTU in the path, update the route PMTU to reflect PMTU
22891da177e4SLinus Torvalds 	   decreases; if new MTU is greater than route PMTU, and the
22901da177e4SLinus Torvalds 	   old MTU is the lowest MTU in the path, update the route PMTU
22911da177e4SLinus Torvalds 	   to reflect the increase. In this case if the other nodes' MTU
22921da177e4SLinus Torvalds 	   also have the lowest MTU, TOO BIG MESSAGE will be lead to
22931da177e4SLinus Torvalds 	   PMTU discouvery.
22941da177e4SLinus Torvalds 	 */
2295d1918542SDavid S. Miller 	if (rt->dst.dev == arg->dev &&
2296d8d1f30bSChangli Gao 	    !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2297d8d1f30bSChangli Gao 	    (dst_mtu(&rt->dst) >= arg->mtu ||
2298d8d1f30bSChangli Gao 	     (dst_mtu(&rt->dst) < arg->mtu &&
2299d8d1f30bSChangli Gao 	      dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2300defb3519SDavid S. Miller 		dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2301566cfd8fSSimon Arlott 	}
23021da177e4SLinus Torvalds 	return 0;
23031da177e4SLinus Torvalds }
23041da177e4SLinus Torvalds 
230595c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu)
23061da177e4SLinus Torvalds {
2307c71099acSThomas Graf 	struct rt6_mtu_change_arg arg = {
2308c71099acSThomas Graf 		.dev = dev,
2309c71099acSThomas Graf 		.mtu = mtu,
2310c71099acSThomas Graf 	};
23111da177e4SLinus Torvalds 
2312c346dca1SYOSHIFUJI Hideaki 	fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
23131da177e4SLinus Torvalds }
23141da177e4SLinus Torvalds 
2315ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
23165176f91eSThomas Graf 	[RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) },
231786872cb5SThomas Graf 	[RTA_OIF]               = { .type = NLA_U32 },
2318ab364a6fSThomas Graf 	[RTA_IIF]		= { .type = NLA_U32 },
231986872cb5SThomas Graf 	[RTA_PRIORITY]          = { .type = NLA_U32 },
232086872cb5SThomas Graf 	[RTA_METRICS]           = { .type = NLA_NESTED },
232151ebd318SNicolas Dichtel 	[RTA_MULTIPATH]		= { .len = sizeof(struct rtnexthop) },
232286872cb5SThomas Graf };
232386872cb5SThomas Graf 
232486872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
232586872cb5SThomas Graf 			      struct fib6_config *cfg)
23261da177e4SLinus Torvalds {
232786872cb5SThomas Graf 	struct rtmsg *rtm;
232886872cb5SThomas Graf 	struct nlattr *tb[RTA_MAX+1];
232986872cb5SThomas Graf 	int err;
23301da177e4SLinus Torvalds 
233186872cb5SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
233286872cb5SThomas Graf 	if (err < 0)
233386872cb5SThomas Graf 		goto errout;
23341da177e4SLinus Torvalds 
233586872cb5SThomas Graf 	err = -EINVAL;
233686872cb5SThomas Graf 	rtm = nlmsg_data(nlh);
233786872cb5SThomas Graf 	memset(cfg, 0, sizeof(*cfg));
233886872cb5SThomas Graf 
233986872cb5SThomas Graf 	cfg->fc_table = rtm->rtm_table;
234086872cb5SThomas Graf 	cfg->fc_dst_len = rtm->rtm_dst_len;
234186872cb5SThomas Graf 	cfg->fc_src_len = rtm->rtm_src_len;
234286872cb5SThomas Graf 	cfg->fc_flags = RTF_UP;
234386872cb5SThomas Graf 	cfg->fc_protocol = rtm->rtm_protocol;
2344ef2c7d7bSNicolas Dichtel 	cfg->fc_type = rtm->rtm_type;
234586872cb5SThomas Graf 
2346ef2c7d7bSNicolas Dichtel 	if (rtm->rtm_type == RTN_UNREACHABLE ||
2347ef2c7d7bSNicolas Dichtel 	    rtm->rtm_type == RTN_BLACKHOLE ||
2348b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_PROHIBIT ||
2349b4949ab2SNicolas Dichtel 	    rtm->rtm_type == RTN_THROW)
235086872cb5SThomas Graf 		cfg->fc_flags |= RTF_REJECT;
235186872cb5SThomas Graf 
2352ab79ad14SMaciej Żenczykowski 	if (rtm->rtm_type == RTN_LOCAL)
2353ab79ad14SMaciej Żenczykowski 		cfg->fc_flags |= RTF_LOCAL;
2354ab79ad14SMaciej Żenczykowski 
235515e47304SEric W. Biederman 	cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
235686872cb5SThomas Graf 	cfg->fc_nlinfo.nlh = nlh;
23573b1e0a65SYOSHIFUJI Hideaki 	cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
235886872cb5SThomas Graf 
235986872cb5SThomas Graf 	if (tb[RTA_GATEWAY]) {
236086872cb5SThomas Graf 		nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
236186872cb5SThomas Graf 		cfg->fc_flags |= RTF_GATEWAY;
23621da177e4SLinus Torvalds 	}
236386872cb5SThomas Graf 
236486872cb5SThomas Graf 	if (tb[RTA_DST]) {
236586872cb5SThomas Graf 		int plen = (rtm->rtm_dst_len + 7) >> 3;
236686872cb5SThomas Graf 
236786872cb5SThomas Graf 		if (nla_len(tb[RTA_DST]) < plen)
236886872cb5SThomas Graf 			goto errout;
236986872cb5SThomas Graf 
237086872cb5SThomas Graf 		nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
23711da177e4SLinus Torvalds 	}
237286872cb5SThomas Graf 
237386872cb5SThomas Graf 	if (tb[RTA_SRC]) {
237486872cb5SThomas Graf 		int plen = (rtm->rtm_src_len + 7) >> 3;
237586872cb5SThomas Graf 
237686872cb5SThomas Graf 		if (nla_len(tb[RTA_SRC]) < plen)
237786872cb5SThomas Graf 			goto errout;
237886872cb5SThomas Graf 
237986872cb5SThomas Graf 		nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
23801da177e4SLinus Torvalds 	}
238186872cb5SThomas Graf 
2382c3968a85SDaniel Walter 	if (tb[RTA_PREFSRC])
2383c3968a85SDaniel Walter 		nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16);
2384c3968a85SDaniel Walter 
238586872cb5SThomas Graf 	if (tb[RTA_OIF])
238686872cb5SThomas Graf 		cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
238786872cb5SThomas Graf 
238886872cb5SThomas Graf 	if (tb[RTA_PRIORITY])
238986872cb5SThomas Graf 		cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
239086872cb5SThomas Graf 
239186872cb5SThomas Graf 	if (tb[RTA_METRICS]) {
239286872cb5SThomas Graf 		cfg->fc_mx = nla_data(tb[RTA_METRICS]);
239386872cb5SThomas Graf 		cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
23941da177e4SLinus Torvalds 	}
239586872cb5SThomas Graf 
239686872cb5SThomas Graf 	if (tb[RTA_TABLE])
239786872cb5SThomas Graf 		cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
239886872cb5SThomas Graf 
239951ebd318SNicolas Dichtel 	if (tb[RTA_MULTIPATH]) {
240051ebd318SNicolas Dichtel 		cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]);
240151ebd318SNicolas Dichtel 		cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
240251ebd318SNicolas Dichtel 	}
240351ebd318SNicolas Dichtel 
240486872cb5SThomas Graf 	err = 0;
240586872cb5SThomas Graf errout:
240686872cb5SThomas Graf 	return err;
24071da177e4SLinus Torvalds }
24081da177e4SLinus Torvalds 
240951ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add)
241051ebd318SNicolas Dichtel {
241151ebd318SNicolas Dichtel 	struct fib6_config r_cfg;
241251ebd318SNicolas Dichtel 	struct rtnexthop *rtnh;
241351ebd318SNicolas Dichtel 	int remaining;
241451ebd318SNicolas Dichtel 	int attrlen;
241551ebd318SNicolas Dichtel 	int err = 0, last_err = 0;
241651ebd318SNicolas Dichtel 
241751ebd318SNicolas Dichtel beginning:
241851ebd318SNicolas Dichtel 	rtnh = (struct rtnexthop *)cfg->fc_mp;
241951ebd318SNicolas Dichtel 	remaining = cfg->fc_mp_len;
242051ebd318SNicolas Dichtel 
242151ebd318SNicolas Dichtel 	/* Parse a Multipath Entry */
242251ebd318SNicolas Dichtel 	while (rtnh_ok(rtnh, remaining)) {
242351ebd318SNicolas Dichtel 		memcpy(&r_cfg, cfg, sizeof(*cfg));
242451ebd318SNicolas Dichtel 		if (rtnh->rtnh_ifindex)
242551ebd318SNicolas Dichtel 			r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
242651ebd318SNicolas Dichtel 
242751ebd318SNicolas Dichtel 		attrlen = rtnh_attrlen(rtnh);
242851ebd318SNicolas Dichtel 		if (attrlen > 0) {
242951ebd318SNicolas Dichtel 			struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
243051ebd318SNicolas Dichtel 
243151ebd318SNicolas Dichtel 			nla = nla_find(attrs, attrlen, RTA_GATEWAY);
243251ebd318SNicolas Dichtel 			if (nla) {
243351ebd318SNicolas Dichtel 				nla_memcpy(&r_cfg.fc_gateway, nla, 16);
243451ebd318SNicolas Dichtel 				r_cfg.fc_flags |= RTF_GATEWAY;
243551ebd318SNicolas Dichtel 			}
243651ebd318SNicolas Dichtel 		}
243751ebd318SNicolas Dichtel 		err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg);
243851ebd318SNicolas Dichtel 		if (err) {
243951ebd318SNicolas Dichtel 			last_err = err;
244051ebd318SNicolas Dichtel 			/* If we are trying to remove a route, do not stop the
244151ebd318SNicolas Dichtel 			 * loop when ip6_route_del() fails (because next hop is
244251ebd318SNicolas Dichtel 			 * already gone), we should try to remove all next hops.
244351ebd318SNicolas Dichtel 			 */
244451ebd318SNicolas Dichtel 			if (add) {
244551ebd318SNicolas Dichtel 				/* If add fails, we should try to delete all
244651ebd318SNicolas Dichtel 				 * next hops that have been already added.
244751ebd318SNicolas Dichtel 				 */
244851ebd318SNicolas Dichtel 				add = 0;
244951ebd318SNicolas Dichtel 				goto beginning;
245051ebd318SNicolas Dichtel 			}
245151ebd318SNicolas Dichtel 		}
245251ebd318SNicolas Dichtel 		rtnh = rtnh_next(rtnh, &remaining);
245351ebd318SNicolas Dichtel 	}
245451ebd318SNicolas Dichtel 
245551ebd318SNicolas Dichtel 	return last_err;
245651ebd318SNicolas Dichtel }
245751ebd318SNicolas Dichtel 
2458c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
24591da177e4SLinus Torvalds {
246086872cb5SThomas Graf 	struct fib6_config cfg;
246186872cb5SThomas Graf 	int err;
24621da177e4SLinus Torvalds 
246386872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
246486872cb5SThomas Graf 	if (err < 0)
246586872cb5SThomas Graf 		return err;
246686872cb5SThomas Graf 
246751ebd318SNicolas Dichtel 	if (cfg.fc_mp)
246851ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 0);
246951ebd318SNicolas Dichtel 	else
247086872cb5SThomas Graf 		return ip6_route_del(&cfg);
24711da177e4SLinus Torvalds }
24721da177e4SLinus Torvalds 
2473c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
24741da177e4SLinus Torvalds {
247586872cb5SThomas Graf 	struct fib6_config cfg;
247686872cb5SThomas Graf 	int err;
24771da177e4SLinus Torvalds 
247886872cb5SThomas Graf 	err = rtm_to_fib6_config(skb, nlh, &cfg);
247986872cb5SThomas Graf 	if (err < 0)
248086872cb5SThomas Graf 		return err;
248186872cb5SThomas Graf 
248251ebd318SNicolas Dichtel 	if (cfg.fc_mp)
248351ebd318SNicolas Dichtel 		return ip6_route_multipath(&cfg, 1);
248451ebd318SNicolas Dichtel 	else
248586872cb5SThomas Graf 		return ip6_route_add(&cfg);
24861da177e4SLinus Torvalds }
24871da177e4SLinus Torvalds 
2488339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void)
2489339bf98fSThomas Graf {
2490339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct rtmsg))
2491339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_SRC */
2492339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_DST */
2493339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_GATEWAY */
2494339bf98fSThomas Graf 	       + nla_total_size(16) /* RTA_PREFSRC */
2495339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_TABLE */
2496339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_IIF */
2497339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_OIF */
2498339bf98fSThomas Graf 	       + nla_total_size(4) /* RTA_PRIORITY */
24996a2b9ce0SNoriaki TAKAMIYA 	       + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2500339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct rta_cacheinfo));
2501339bf98fSThomas Graf }
2502339bf98fSThomas Graf 
2503191cd582SBrian Haley static int rt6_fill_node(struct net *net,
2504191cd582SBrian Haley 			 struct sk_buff *skb, struct rt6_info *rt,
25050d51aa80SJamal Hadi Salim 			 struct in6_addr *dst, struct in6_addr *src,
250615e47304SEric W. Biederman 			 int iif, int type, u32 portid, u32 seq,
25077bc570c8SYOSHIFUJI Hideaki 			 int prefix, int nowait, unsigned int flags)
25081da177e4SLinus Torvalds {
25091da177e4SLinus Torvalds 	struct rtmsg *rtm;
25101da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
2511e3703b3dSThomas Graf 	long expires;
25129e762a4aSPatrick McHardy 	u32 table;
2513f2c31e32SEric Dumazet 	struct neighbour *n;
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds 	if (prefix) {	/* user wants prefix routes only */
25161da177e4SLinus Torvalds 		if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
25171da177e4SLinus Torvalds 			/* success since this is not a prefix route */
25181da177e4SLinus Torvalds 			return 1;
25191da177e4SLinus Torvalds 		}
25201da177e4SLinus Torvalds 	}
25211da177e4SLinus Torvalds 
252215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags);
252338308473SDavid S. Miller 	if (!nlh)
252426932566SPatrick McHardy 		return -EMSGSIZE;
25252d7202bfSThomas Graf 
25262d7202bfSThomas Graf 	rtm = nlmsg_data(nlh);
25271da177e4SLinus Torvalds 	rtm->rtm_family = AF_INET6;
25281da177e4SLinus Torvalds 	rtm->rtm_dst_len = rt->rt6i_dst.plen;
25291da177e4SLinus Torvalds 	rtm->rtm_src_len = rt->rt6i_src.plen;
25301da177e4SLinus Torvalds 	rtm->rtm_tos = 0;
2531c71099acSThomas Graf 	if (rt->rt6i_table)
25329e762a4aSPatrick McHardy 		table = rt->rt6i_table->tb6_id;
2533c71099acSThomas Graf 	else
25349e762a4aSPatrick McHardy 		table = RT6_TABLE_UNSPEC;
25359e762a4aSPatrick McHardy 	rtm->rtm_table = table;
2536c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_TABLE, table))
2537c78679e8SDavid S. Miller 		goto nla_put_failure;
2538ef2c7d7bSNicolas Dichtel 	if (rt->rt6i_flags & RTF_REJECT) {
2539ef2c7d7bSNicolas Dichtel 		switch (rt->dst.error) {
2540ef2c7d7bSNicolas Dichtel 		case -EINVAL:
2541ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_BLACKHOLE;
2542ef2c7d7bSNicolas Dichtel 			break;
2543ef2c7d7bSNicolas Dichtel 		case -EACCES:
2544ef2c7d7bSNicolas Dichtel 			rtm->rtm_type = RTN_PROHIBIT;
2545ef2c7d7bSNicolas Dichtel 			break;
2546b4949ab2SNicolas Dichtel 		case -EAGAIN:
2547b4949ab2SNicolas Dichtel 			rtm->rtm_type = RTN_THROW;
2548b4949ab2SNicolas Dichtel 			break;
2549ef2c7d7bSNicolas Dichtel 		default:
25501da177e4SLinus Torvalds 			rtm->rtm_type = RTN_UNREACHABLE;
2551ef2c7d7bSNicolas Dichtel 			break;
2552ef2c7d7bSNicolas Dichtel 		}
2553ef2c7d7bSNicolas Dichtel 	}
2554ab79ad14SMaciej Żenczykowski 	else if (rt->rt6i_flags & RTF_LOCAL)
2555ab79ad14SMaciej Żenczykowski 		rtm->rtm_type = RTN_LOCAL;
2556d1918542SDavid S. Miller 	else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK))
25571da177e4SLinus Torvalds 		rtm->rtm_type = RTN_LOCAL;
25581da177e4SLinus Torvalds 	else
25591da177e4SLinus Torvalds 		rtm->rtm_type = RTN_UNICAST;
25601da177e4SLinus Torvalds 	rtm->rtm_flags = 0;
25611da177e4SLinus Torvalds 	rtm->rtm_scope = RT_SCOPE_UNIVERSE;
25621da177e4SLinus Torvalds 	rtm->rtm_protocol = rt->rt6i_protocol;
25631da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_DYNAMIC)
25641da177e4SLinus Torvalds 		rtm->rtm_protocol = RTPROT_REDIRECT;
2565f0396f60SDenis Ovsienko 	else if (rt->rt6i_flags & RTF_ADDRCONF) {
2566f0396f60SDenis Ovsienko 		if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO))
25671da177e4SLinus Torvalds 			rtm->rtm_protocol = RTPROT_RA;
2568f0396f60SDenis Ovsienko 		else
2569f0396f60SDenis Ovsienko 			rtm->rtm_protocol = RTPROT_KERNEL;
2570f0396f60SDenis Ovsienko 	}
25711da177e4SLinus Torvalds 
25721da177e4SLinus Torvalds 	if (rt->rt6i_flags & RTF_CACHE)
25731da177e4SLinus Torvalds 		rtm->rtm_flags |= RTM_F_CLONED;
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds 	if (dst) {
2576c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, dst))
2577c78679e8SDavid S. Miller 			goto nla_put_failure;
25781da177e4SLinus Torvalds 		rtm->rtm_dst_len = 128;
25791da177e4SLinus Torvalds 	} else if (rtm->rtm_dst_len)
2580c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr))
2581c78679e8SDavid S. Miller 			goto nla_put_failure;
25821da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
25831da177e4SLinus Torvalds 	if (src) {
2584c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_SRC, 16, src))
2585c78679e8SDavid S. Miller 			goto nla_put_failure;
25861da177e4SLinus Torvalds 		rtm->rtm_src_len = 128;
2587c78679e8SDavid S. Miller 	} else if (rtm->rtm_src_len &&
2588c78679e8SDavid S. Miller 		   nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr))
2589c78679e8SDavid S. Miller 		goto nla_put_failure;
25901da177e4SLinus Torvalds #endif
25917bc570c8SYOSHIFUJI Hideaki 	if (iif) {
25927bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
25937bc570c8SYOSHIFUJI Hideaki 		if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
25948229efdaSBenjamin Thery 			int err = ip6mr_get_route(net, skb, rtm, nowait);
25957bc570c8SYOSHIFUJI Hideaki 			if (err <= 0) {
25967bc570c8SYOSHIFUJI Hideaki 				if (!nowait) {
25977bc570c8SYOSHIFUJI Hideaki 					if (err == 0)
25987bc570c8SYOSHIFUJI Hideaki 						return 0;
25997bc570c8SYOSHIFUJI Hideaki 					goto nla_put_failure;
26007bc570c8SYOSHIFUJI Hideaki 				} else {
26017bc570c8SYOSHIFUJI Hideaki 					if (err == -EMSGSIZE)
26027bc570c8SYOSHIFUJI Hideaki 						goto nla_put_failure;
26037bc570c8SYOSHIFUJI Hideaki 				}
26047bc570c8SYOSHIFUJI Hideaki 			}
26057bc570c8SYOSHIFUJI Hideaki 		} else
26067bc570c8SYOSHIFUJI Hideaki #endif
2607c78679e8SDavid S. Miller 			if (nla_put_u32(skb, RTA_IIF, iif))
2608c78679e8SDavid S. Miller 				goto nla_put_failure;
26097bc570c8SYOSHIFUJI Hideaki 	} else if (dst) {
26101da177e4SLinus Torvalds 		struct in6_addr saddr_buf;
2611c78679e8SDavid S. Miller 		if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2612c78679e8SDavid S. Miller 		    nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2613c78679e8SDavid S. Miller 			goto nla_put_failure;
2614c3968a85SDaniel Walter 	}
2615c3968a85SDaniel Walter 
2616c3968a85SDaniel Walter 	if (rt->rt6i_prefsrc.plen) {
2617c3968a85SDaniel Walter 		struct in6_addr saddr_buf;
26184e3fd7a0SAlexey Dobriyan 		saddr_buf = rt->rt6i_prefsrc.addr;
2619c78679e8SDavid S. Miller 		if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf))
2620c78679e8SDavid S. Miller 			goto nla_put_failure;
26211da177e4SLinus Torvalds 	}
26222d7202bfSThomas Graf 
2623defb3519SDavid S. Miller 	if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
26242d7202bfSThomas Graf 		goto nla_put_failure;
26252d7202bfSThomas Graf 
262697cac082SDavid S. Miller 	n = rt->n;
262794f826b8SEric Dumazet 	if (n) {
2628fdd6681dSAmerigo Wang 		if (nla_put(skb, RTA_GATEWAY, 16, &n->primary_key) < 0)
262994f826b8SEric Dumazet 			goto nla_put_failure;
263094f826b8SEric Dumazet 	}
26312d7202bfSThomas Graf 
2632c78679e8SDavid S. Miller 	if (rt->dst.dev &&
2633c78679e8SDavid S. Miller 	    nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex))
2634c78679e8SDavid S. Miller 		goto nla_put_failure;
2635c78679e8SDavid S. Miller 	if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric))
2636c78679e8SDavid S. Miller 		goto nla_put_failure;
26378253947eSLi Wei 
26388253947eSLi Wei 	expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0;
263969cdf8f9SYOSHIFUJI Hideaki 
264087a50699SDavid S. Miller 	if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2641e3703b3dSThomas Graf 		goto nla_put_failure;
26421da177e4SLinus Torvalds 
26432d7202bfSThomas Graf 	return nlmsg_end(skb, nlh);
26442d7202bfSThomas Graf 
26452d7202bfSThomas Graf nla_put_failure:
264626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
264726932566SPatrick McHardy 	return -EMSGSIZE;
26481da177e4SLinus Torvalds }
26491da177e4SLinus Torvalds 
26501b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg)
26511da177e4SLinus Torvalds {
26521da177e4SLinus Torvalds 	struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
26531da177e4SLinus Torvalds 	int prefix;
26541da177e4SLinus Torvalds 
26552d7202bfSThomas Graf 	if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
26562d7202bfSThomas Graf 		struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
26571da177e4SLinus Torvalds 		prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
26581da177e4SLinus Torvalds 	} else
26591da177e4SLinus Torvalds 		prefix = 0;
26601da177e4SLinus Torvalds 
2661191cd582SBrian Haley 	return rt6_fill_node(arg->net,
2662191cd582SBrian Haley 		     arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
266315e47304SEric W. Biederman 		     NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq,
26647bc570c8SYOSHIFUJI Hideaki 		     prefix, 0, NLM_F_MULTI);
26651da177e4SLinus Torvalds }
26661da177e4SLinus Torvalds 
2667c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
26681da177e4SLinus Torvalds {
26693b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
2670ab364a6fSThomas Graf 	struct nlattr *tb[RTA_MAX+1];
26711da177e4SLinus Torvalds 	struct rt6_info *rt;
2672ab364a6fSThomas Graf 	struct sk_buff *skb;
2673ab364a6fSThomas Graf 	struct rtmsg *rtm;
26744c9483b2SDavid S. Miller 	struct flowi6 fl6;
267572331bc0SShmulik Ladkani 	int err, iif = 0, oif = 0;
2676ab364a6fSThomas Graf 
2677ab364a6fSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2678ab364a6fSThomas Graf 	if (err < 0)
2679ab364a6fSThomas Graf 		goto errout;
2680ab364a6fSThomas Graf 
2681ab364a6fSThomas Graf 	err = -EINVAL;
26824c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
2683ab364a6fSThomas Graf 
2684ab364a6fSThomas Graf 	if (tb[RTA_SRC]) {
2685ab364a6fSThomas Graf 		if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2686ab364a6fSThomas Graf 			goto errout;
2687ab364a6fSThomas Graf 
26884e3fd7a0SAlexey Dobriyan 		fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]);
2689ab364a6fSThomas Graf 	}
2690ab364a6fSThomas Graf 
2691ab364a6fSThomas Graf 	if (tb[RTA_DST]) {
2692ab364a6fSThomas Graf 		if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2693ab364a6fSThomas Graf 			goto errout;
2694ab364a6fSThomas Graf 
26954e3fd7a0SAlexey Dobriyan 		fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]);
2696ab364a6fSThomas Graf 	}
2697ab364a6fSThomas Graf 
2698ab364a6fSThomas Graf 	if (tb[RTA_IIF])
2699ab364a6fSThomas Graf 		iif = nla_get_u32(tb[RTA_IIF]);
2700ab364a6fSThomas Graf 
2701ab364a6fSThomas Graf 	if (tb[RTA_OIF])
270272331bc0SShmulik Ladkani 		oif = nla_get_u32(tb[RTA_OIF]);
2703ab364a6fSThomas Graf 
2704ab364a6fSThomas Graf 	if (iif) {
2705ab364a6fSThomas Graf 		struct net_device *dev;
270672331bc0SShmulik Ladkani 		int flags = 0;
270772331bc0SShmulik Ladkani 
27085578689aSDaniel Lezcano 		dev = __dev_get_by_index(net, iif);
2709ab364a6fSThomas Graf 		if (!dev) {
2710ab364a6fSThomas Graf 			err = -ENODEV;
2711ab364a6fSThomas Graf 			goto errout;
2712ab364a6fSThomas Graf 		}
271372331bc0SShmulik Ladkani 
271472331bc0SShmulik Ladkani 		fl6.flowi6_iif = iif;
271572331bc0SShmulik Ladkani 
271672331bc0SShmulik Ladkani 		if (!ipv6_addr_any(&fl6.saddr))
271772331bc0SShmulik Ladkani 			flags |= RT6_LOOKUP_F_HAS_SADDR;
271872331bc0SShmulik Ladkani 
271972331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6,
272072331bc0SShmulik Ladkani 							       flags);
272172331bc0SShmulik Ladkani 	} else {
272272331bc0SShmulik Ladkani 		fl6.flowi6_oif = oif;
272372331bc0SShmulik Ladkani 
272472331bc0SShmulik Ladkani 		rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6);
2725ab364a6fSThomas Graf 	}
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
272838308473SDavid S. Miller 	if (!skb) {
27292173bff5SShmulik Ladkani 		dst_release(&rt->dst);
2730ab364a6fSThomas Graf 		err = -ENOBUFS;
2731ab364a6fSThomas Graf 		goto errout;
2732ab364a6fSThomas Graf 	}
27331da177e4SLinus Torvalds 
27341da177e4SLinus Torvalds 	/* Reserve room for dummy headers, this skb can pass
27351da177e4SLinus Torvalds 	   through good chunk of routing engine.
27361da177e4SLinus Torvalds 	 */
2737459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
27381da177e4SLinus Torvalds 	skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
27391da177e4SLinus Torvalds 
2740d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
27411da177e4SLinus Torvalds 
27424c9483b2SDavid S. Miller 	err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif,
274315e47304SEric W. Biederman 			    RTM_NEWROUTE, NETLINK_CB(in_skb).portid,
27447bc570c8SYOSHIFUJI Hideaki 			    nlh->nlmsg_seq, 0, 0, 0);
27451da177e4SLinus Torvalds 	if (err < 0) {
2746ab364a6fSThomas Graf 		kfree_skb(skb);
2747ab364a6fSThomas Graf 		goto errout;
27481da177e4SLinus Torvalds 	}
27491da177e4SLinus Torvalds 
275015e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2751ab364a6fSThomas Graf errout:
27521da177e4SLinus Torvalds 	return err;
27531da177e4SLinus Torvalds }
27541da177e4SLinus Torvalds 
275586872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
27561da177e4SLinus Torvalds {
27571da177e4SLinus Torvalds 	struct sk_buff *skb;
27585578689aSDaniel Lezcano 	struct net *net = info->nl_net;
2759528c4cebSDenis V. Lunev 	u32 seq;
2760528c4cebSDenis V. Lunev 	int err;
27610d51aa80SJamal Hadi Salim 
2762528c4cebSDenis V. Lunev 	err = -ENOBUFS;
276338308473SDavid S. Miller 	seq = info->nlh ? info->nlh->nlmsg_seq : 0;
276486872cb5SThomas Graf 
2765339bf98fSThomas Graf 	skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
276638308473SDavid S. Miller 	if (!skb)
276721713ebcSThomas Graf 		goto errout;
27681da177e4SLinus Torvalds 
2769191cd582SBrian Haley 	err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
277015e47304SEric W. Biederman 				event, info->portid, seq, 0, 0, 0);
277126932566SPatrick McHardy 	if (err < 0) {
277226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
277326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
277426932566SPatrick McHardy 		kfree_skb(skb);
277526932566SPatrick McHardy 		goto errout;
277626932566SPatrick McHardy 	}
277715e47304SEric W. Biederman 	rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE,
27785578689aSDaniel Lezcano 		    info->nlh, gfp_any());
27791ce85fe4SPablo Neira Ayuso 	return;
278021713ebcSThomas Graf errout:
278121713ebcSThomas Graf 	if (err < 0)
27825578689aSDaniel Lezcano 		rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
27831da177e4SLinus Torvalds }
27841da177e4SLinus Torvalds 
27858ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this,
27868ed67789SDaniel Lezcano 				unsigned long event, void *data)
27878ed67789SDaniel Lezcano {
27888ed67789SDaniel Lezcano 	struct net_device *dev = (struct net_device *)data;
2789c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
27908ed67789SDaniel Lezcano 
27918ed67789SDaniel Lezcano 	if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2792d8d1f30bSChangli Gao 		net->ipv6.ip6_null_entry->dst.dev = dev;
27938ed67789SDaniel Lezcano 		net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
27948ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2795d8d1f30bSChangli Gao 		net->ipv6.ip6_prohibit_entry->dst.dev = dev;
27968ed67789SDaniel Lezcano 		net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2797d8d1f30bSChangli Gao 		net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
27988ed67789SDaniel Lezcano 		net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
27998ed67789SDaniel Lezcano #endif
28008ed67789SDaniel Lezcano 	}
28018ed67789SDaniel Lezcano 
28028ed67789SDaniel Lezcano 	return NOTIFY_OK;
28038ed67789SDaniel Lezcano }
28048ed67789SDaniel Lezcano 
28051da177e4SLinus Torvalds /*
28061da177e4SLinus Torvalds  *	/proc
28071da177e4SLinus Torvalds  */
28081da177e4SLinus Torvalds 
28091da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
28101da177e4SLinus Torvalds 
28111da177e4SLinus Torvalds struct rt6_proc_arg
28121da177e4SLinus Torvalds {
28131da177e4SLinus Torvalds 	char *buffer;
28141da177e4SLinus Torvalds 	int offset;
28151da177e4SLinus Torvalds 	int length;
28161da177e4SLinus Torvalds 	int skip;
28171da177e4SLinus Torvalds 	int len;
28181da177e4SLinus Torvalds };
28191da177e4SLinus Torvalds 
28201da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg)
28211da177e4SLinus Torvalds {
282233120b30SAlexey Dobriyan 	struct seq_file *m = p_arg;
282369cce1d1SDavid S. Miller 	struct neighbour *n;
28241da177e4SLinus Torvalds 
28254b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
28261da177e4SLinus Torvalds 
28271da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES
28284b7a4274SHarvey Harrison 	seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
28291da177e4SLinus Torvalds #else
283033120b30SAlexey Dobriyan 	seq_puts(m, "00000000000000000000000000000000 00 ");
28311da177e4SLinus Torvalds #endif
283297cac082SDavid S. Miller 	n = rt->n;
283369cce1d1SDavid S. Miller 	if (n) {
283469cce1d1SDavid S. Miller 		seq_printf(m, "%pi6", n->primary_key);
28351da177e4SLinus Torvalds 	} else {
283633120b30SAlexey Dobriyan 		seq_puts(m, "00000000000000000000000000000000");
28371da177e4SLinus Torvalds 	}
283833120b30SAlexey Dobriyan 	seq_printf(m, " %08x %08x %08x %08x %8s\n",
2839d8d1f30bSChangli Gao 		   rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2840d8d1f30bSChangli Gao 		   rt->dst.__use, rt->rt6i_flags,
2841d1918542SDavid S. Miller 		   rt->dst.dev ? rt->dst.dev->name : "");
28421da177e4SLinus Torvalds 	return 0;
28431da177e4SLinus Torvalds }
28441da177e4SLinus Torvalds 
284533120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v)
28461da177e4SLinus Torvalds {
2847f3db4851SDaniel Lezcano 	struct net *net = (struct net *)m->private;
284832b293a5SJosh Hunt 	fib6_clean_all_ro(net, rt6_info_route, 0, m);
284933120b30SAlexey Dobriyan 	return 0;
28501da177e4SLinus Torvalds }
28511da177e4SLinus Torvalds 
285233120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file)
285333120b30SAlexey Dobriyan {
2854de05c557SPavel Emelyanov 	return single_open_net(inode, file, ipv6_route_show);
2855f3db4851SDaniel Lezcano }
2856f3db4851SDaniel Lezcano 
285733120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = {
285833120b30SAlexey Dobriyan 	.owner		= THIS_MODULE,
285933120b30SAlexey Dobriyan 	.open		= ipv6_route_open,
286033120b30SAlexey Dobriyan 	.read		= seq_read,
286133120b30SAlexey Dobriyan 	.llseek		= seq_lseek,
2862b6fcbdb4SPavel Emelyanov 	.release	= single_release_net,
286333120b30SAlexey Dobriyan };
286433120b30SAlexey Dobriyan 
28651da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v)
28661da177e4SLinus Torvalds {
286769ddb805SDaniel Lezcano 	struct net *net = (struct net *)seq->private;
28681da177e4SLinus Torvalds 	seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
286969ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_nodes,
287069ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_route_nodes,
287169ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_alloc,
287269ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_entries,
287369ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_rt_cache,
2874fc66f95cSEric Dumazet 		   dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
287569ddb805SDaniel Lezcano 		   net->ipv6.rt6_stats->fib_discarded_routes);
28761da177e4SLinus Torvalds 
28771da177e4SLinus Torvalds 	return 0;
28781da177e4SLinus Torvalds }
28791da177e4SLinus Torvalds 
28801da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file)
28811da177e4SLinus Torvalds {
2882de05c557SPavel Emelyanov 	return single_open_net(inode, file, rt6_stats_seq_show);
288369ddb805SDaniel Lezcano }
288469ddb805SDaniel Lezcano 
28859a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = {
28861da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
28871da177e4SLinus Torvalds 	.open	 = rt6_stats_seq_open,
28881da177e4SLinus Torvalds 	.read	 = seq_read,
28891da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
2890b6fcbdb4SPavel Emelyanov 	.release = single_release_net,
28911da177e4SLinus Torvalds };
28921da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
28931da177e4SLinus Torvalds 
28941da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
28951da177e4SLinus Torvalds 
28961da177e4SLinus Torvalds static
28978d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
28981da177e4SLinus Torvalds 			      void __user *buffer, size_t *lenp, loff_t *ppos)
28991da177e4SLinus Torvalds {
2900c486da34SLucian Adrian Grijincu 	struct net *net;
2901c486da34SLucian Adrian Grijincu 	int delay;
2902c486da34SLucian Adrian Grijincu 	if (!write)
2903c486da34SLucian Adrian Grijincu 		return -EINVAL;
2904c486da34SLucian Adrian Grijincu 
2905c486da34SLucian Adrian Grijincu 	net = (struct net *)ctl->extra1;
2906c486da34SLucian Adrian Grijincu 	delay = net->ipv6.sysctl.flush_delay;
29078d65af78SAlexey Dobriyan 	proc_dointvec(ctl, write, buffer, lenp, ppos);
29085b7c931dSDaniel Lezcano 	fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
29091da177e4SLinus Torvalds 	return 0;
29101da177e4SLinus Torvalds }
29111da177e4SLinus Torvalds 
2912760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = {
29131da177e4SLinus Torvalds 	{
29141da177e4SLinus Torvalds 		.procname	=	"flush",
29154990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.flush_delay,
29161da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
291789c8b3a1SDave Jones 		.mode		=	0200,
29186d9f239aSAlexey Dobriyan 		.proc_handler	=	ipv6_sysctl_rtcache_flush
29191da177e4SLinus Torvalds 	},
29201da177e4SLinus Torvalds 	{
29211da177e4SLinus Torvalds 		.procname	=	"gc_thresh",
29229a7ec3a9SDaniel Lezcano 		.data		=	&ip6_dst_ops_template.gc_thresh,
29231da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29241da177e4SLinus Torvalds 		.mode		=	0644,
29256d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
29261da177e4SLinus Torvalds 	},
29271da177e4SLinus Torvalds 	{
29281da177e4SLinus Torvalds 		.procname	=	"max_size",
29294990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_max_size,
29301da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29311da177e4SLinus Torvalds 		.mode		=	0644,
29326d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec,
29331da177e4SLinus Torvalds 	},
29341da177e4SLinus Torvalds 	{
29351da177e4SLinus Torvalds 		.procname	=	"gc_min_interval",
29364990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
29371da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29381da177e4SLinus Torvalds 		.mode		=	0644,
29396d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29401da177e4SLinus Torvalds 	},
29411da177e4SLinus Torvalds 	{
29421da177e4SLinus Torvalds 		.procname	=	"gc_timeout",
29434990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout,
29441da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29451da177e4SLinus Torvalds 		.mode		=	0644,
29466d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29471da177e4SLinus Torvalds 	},
29481da177e4SLinus Torvalds 	{
29491da177e4SLinus Torvalds 		.procname	=	"gc_interval",
29504990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval,
29511da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29521da177e4SLinus Torvalds 		.mode		=	0644,
29536d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29541da177e4SLinus Torvalds 	},
29551da177e4SLinus Torvalds 	{
29561da177e4SLinus Torvalds 		.procname	=	"gc_elasticity",
29574990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
29581da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29591da177e4SLinus Torvalds 		.mode		=	0644,
2960f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
29611da177e4SLinus Torvalds 	},
29621da177e4SLinus Torvalds 	{
29631da177e4SLinus Torvalds 		.procname	=	"mtu_expires",
29644990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires,
29651da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29661da177e4SLinus Torvalds 		.mode		=	0644,
29676d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_jiffies,
29681da177e4SLinus Torvalds 	},
29691da177e4SLinus Torvalds 	{
29701da177e4SLinus Torvalds 		.procname	=	"min_adv_mss",
29714990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss,
29721da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29731da177e4SLinus Torvalds 		.mode		=	0644,
2974f3d3f616SMin Zhang 		.proc_handler	=	proc_dointvec,
29751da177e4SLinus Torvalds 	},
29761da177e4SLinus Torvalds 	{
29771da177e4SLinus Torvalds 		.procname	=	"gc_min_interval_ms",
29784990509fSDaniel Lezcano 		.data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
29791da177e4SLinus Torvalds 		.maxlen		=	sizeof(int),
29801da177e4SLinus Torvalds 		.mode		=	0644,
29816d9f239aSAlexey Dobriyan 		.proc_handler	=	proc_dointvec_ms_jiffies,
29821da177e4SLinus Torvalds 	},
2983f8572d8fSEric W. Biederman 	{ }
29841da177e4SLinus Torvalds };
29851da177e4SLinus Torvalds 
29862c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2987760f2d01SDaniel Lezcano {
2988760f2d01SDaniel Lezcano 	struct ctl_table *table;
2989760f2d01SDaniel Lezcano 
2990760f2d01SDaniel Lezcano 	table = kmemdup(ipv6_route_table_template,
2991760f2d01SDaniel Lezcano 			sizeof(ipv6_route_table_template),
2992760f2d01SDaniel Lezcano 			GFP_KERNEL);
29935ee09105SYOSHIFUJI Hideaki 
29945ee09105SYOSHIFUJI Hideaki 	if (table) {
29955ee09105SYOSHIFUJI Hideaki 		table[0].data = &net->ipv6.sysctl.flush_delay;
2996c486da34SLucian Adrian Grijincu 		table[0].extra1 = net;
299786393e52SAlexey Dobriyan 		table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
29985ee09105SYOSHIFUJI Hideaki 		table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
29995ee09105SYOSHIFUJI Hideaki 		table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
30005ee09105SYOSHIFUJI Hideaki 		table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
30015ee09105SYOSHIFUJI Hideaki 		table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
30025ee09105SYOSHIFUJI Hideaki 		table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
30035ee09105SYOSHIFUJI Hideaki 		table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
30045ee09105SYOSHIFUJI Hideaki 		table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
30059c69fabeSAlexey Dobriyan 		table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
30065ee09105SYOSHIFUJI Hideaki 	}
30075ee09105SYOSHIFUJI Hideaki 
3008760f2d01SDaniel Lezcano 	return table;
3009760f2d01SDaniel Lezcano }
30101da177e4SLinus Torvalds #endif
30111da177e4SLinus Torvalds 
30122c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net)
3013cdb18761SDaniel Lezcano {
3014633d424bSPavel Emelyanov 	int ret = -ENOMEM;
30158ed67789SDaniel Lezcano 
301686393e52SAlexey Dobriyan 	memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
301786393e52SAlexey Dobriyan 	       sizeof(net->ipv6.ip6_dst_ops));
3018f2fc6a54SBenjamin Thery 
3019fc66f95cSEric Dumazet 	if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
3020fc66f95cSEric Dumazet 		goto out_ip6_dst_ops;
3021fc66f95cSEric Dumazet 
30228ed67789SDaniel Lezcano 	net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
30238ed67789SDaniel Lezcano 					   sizeof(*net->ipv6.ip6_null_entry),
30248ed67789SDaniel Lezcano 					   GFP_KERNEL);
30258ed67789SDaniel Lezcano 	if (!net->ipv6.ip6_null_entry)
3026fc66f95cSEric Dumazet 		goto out_ip6_dst_entries;
3027d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.path =
30288ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_null_entry;
3029d8d1f30bSChangli Gao 	net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
303062fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
303162fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
30328ed67789SDaniel Lezcano 
30338ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
30348ed67789SDaniel Lezcano 	net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
30358ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_prohibit_entry),
30368ed67789SDaniel Lezcano 					       GFP_KERNEL);
303768fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_prohibit_entry)
303868fffc67SPeter Zijlstra 		goto out_ip6_null_entry;
3039d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.path =
30408ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_prohibit_entry;
3041d8d1f30bSChangli Gao 	net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
304262fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
304362fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
30448ed67789SDaniel Lezcano 
30458ed67789SDaniel Lezcano 	net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
30468ed67789SDaniel Lezcano 					       sizeof(*net->ipv6.ip6_blk_hole_entry),
30478ed67789SDaniel Lezcano 					       GFP_KERNEL);
304868fffc67SPeter Zijlstra 	if (!net->ipv6.ip6_blk_hole_entry)
304968fffc67SPeter Zijlstra 		goto out_ip6_prohibit_entry;
3050d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.path =
30518ed67789SDaniel Lezcano 		(struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
3052d8d1f30bSChangli Gao 	net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
305362fa8a84SDavid S. Miller 	dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
305462fa8a84SDavid S. Miller 			 ip6_template_metrics, true);
30558ed67789SDaniel Lezcano #endif
30568ed67789SDaniel Lezcano 
3057b339a47cSPeter Zijlstra 	net->ipv6.sysctl.flush_delay = 0;
3058b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_max_size = 4096;
3059b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
3060b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
3061b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
3062b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
3063b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
3064b339a47cSPeter Zijlstra 	net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
3065b339a47cSPeter Zijlstra 
30666891a346SBenjamin Thery 	net->ipv6.ip6_rt_gc_expire = 30*HZ;
30676891a346SBenjamin Thery 
30688ed67789SDaniel Lezcano 	ret = 0;
30698ed67789SDaniel Lezcano out:
30708ed67789SDaniel Lezcano 	return ret;
3071f2fc6a54SBenjamin Thery 
307268fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES
307368fffc67SPeter Zijlstra out_ip6_prohibit_entry:
307468fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_prohibit_entry);
307568fffc67SPeter Zijlstra out_ip6_null_entry:
307668fffc67SPeter Zijlstra 	kfree(net->ipv6.ip6_null_entry);
307768fffc67SPeter Zijlstra #endif
3078fc66f95cSEric Dumazet out_ip6_dst_entries:
3079fc66f95cSEric Dumazet 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3080f2fc6a54SBenjamin Thery out_ip6_dst_ops:
3081f2fc6a54SBenjamin Thery 	goto out;
3082cdb18761SDaniel Lezcano }
3083cdb18761SDaniel Lezcano 
30842c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net)
3085cdb18761SDaniel Lezcano {
30868ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_null_entry);
30878ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES
30888ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_prohibit_entry);
30898ed67789SDaniel Lezcano 	kfree(net->ipv6.ip6_blk_hole_entry);
30908ed67789SDaniel Lezcano #endif
309141bb78b4SXiaotian Feng 	dst_entries_destroy(&net->ipv6.ip6_dst_ops);
3092cdb18761SDaniel Lezcano }
3093cdb18761SDaniel Lezcano 
3094d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net)
3095d189634eSThomas Graf {
3096d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3097d189634eSThomas Graf 	proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
3098d189634eSThomas Graf 	proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
3099d189634eSThomas Graf #endif
3100d189634eSThomas Graf 	return 0;
3101d189634eSThomas Graf }
3102d189634eSThomas Graf 
3103d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net)
3104d189634eSThomas Graf {
3105d189634eSThomas Graf #ifdef CONFIG_PROC_FS
3106d189634eSThomas Graf 	proc_net_remove(net, "ipv6_route");
3107d189634eSThomas Graf 	proc_net_remove(net, "rt6_stats");
3108d189634eSThomas Graf #endif
3109d189634eSThomas Graf }
3110d189634eSThomas Graf 
3111cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = {
3112cdb18761SDaniel Lezcano 	.init = ip6_route_net_init,
3113cdb18761SDaniel Lezcano 	.exit = ip6_route_net_exit,
3114cdb18761SDaniel Lezcano };
3115cdb18761SDaniel Lezcano 
3116c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net)
3117c3426b47SDavid S. Miller {
3118c3426b47SDavid S. Miller 	struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL);
3119c3426b47SDavid S. Miller 
3120c3426b47SDavid S. Miller 	if (!bp)
3121c3426b47SDavid S. Miller 		return -ENOMEM;
3122c3426b47SDavid S. Miller 	inet_peer_base_init(bp);
3123c3426b47SDavid S. Miller 	net->ipv6.peers = bp;
3124c3426b47SDavid S. Miller 	return 0;
3125c3426b47SDavid S. Miller }
3126c3426b47SDavid S. Miller 
3127c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net)
3128c3426b47SDavid S. Miller {
3129c3426b47SDavid S. Miller 	struct inet_peer_base *bp = net->ipv6.peers;
3130c3426b47SDavid S. Miller 
3131c3426b47SDavid S. Miller 	net->ipv6.peers = NULL;
313256a6b248SDavid S. Miller 	inetpeer_invalidate_tree(bp);
3133c3426b47SDavid S. Miller 	kfree(bp);
3134c3426b47SDavid S. Miller }
3135c3426b47SDavid S. Miller 
31362b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = {
3137c3426b47SDavid S. Miller 	.init	=	ipv6_inetpeer_init,
3138c3426b47SDavid S. Miller 	.exit	=	ipv6_inetpeer_exit,
3139c3426b47SDavid S. Miller };
3140c3426b47SDavid S. Miller 
3141d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = {
3142d189634eSThomas Graf 	.init = ip6_route_net_init_late,
3143d189634eSThomas Graf 	.exit = ip6_route_net_exit_late,
3144d189634eSThomas Graf };
3145d189634eSThomas Graf 
31468ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = {
31478ed67789SDaniel Lezcano 	.notifier_call = ip6_route_dev_notify,
31488ed67789SDaniel Lezcano 	.priority = 0,
31498ed67789SDaniel Lezcano };
31508ed67789SDaniel Lezcano 
3151433d49c3SDaniel Lezcano int __init ip6_route_init(void)
31521da177e4SLinus Torvalds {
3153433d49c3SDaniel Lezcano 	int ret;
3154433d49c3SDaniel Lezcano 
31559a7ec3a9SDaniel Lezcano 	ret = -ENOMEM;
31569a7ec3a9SDaniel Lezcano 	ip6_dst_ops_template.kmem_cachep =
31579a7ec3a9SDaniel Lezcano 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
31589a7ec3a9SDaniel Lezcano 				  SLAB_HWCACHE_ALIGN, NULL);
31599a7ec3a9SDaniel Lezcano 	if (!ip6_dst_ops_template.kmem_cachep)
3160c19a28e1SFernando Carrijo 		goto out;
316114e50e57SDavid S. Miller 
3162fc66f95cSEric Dumazet 	ret = dst_entries_init(&ip6_dst_blackhole_ops);
31638ed67789SDaniel Lezcano 	if (ret)
3164bdb3289fSDaniel Lezcano 		goto out_kmem_cache;
3165bdb3289fSDaniel Lezcano 
3166c3426b47SDavid S. Miller 	ret = register_pernet_subsys(&ipv6_inetpeer_ops);
3167c3426b47SDavid S. Miller 	if (ret)
3168e8803b6cSDavid S. Miller 		goto out_dst_entries;
31692a0c451aSThomas Graf 
31707e52b33bSDavid S. Miller 	ret = register_pernet_subsys(&ip6_route_net_ops);
31717e52b33bSDavid S. Miller 	if (ret)
31727e52b33bSDavid S. Miller 		goto out_register_inetpeer;
3173c3426b47SDavid S. Miller 
31745dc121e9SArnaud Ebalard 	ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
31755dc121e9SArnaud Ebalard 
31768ed67789SDaniel Lezcano 	/* Registering of the loopback is done before this portion of code,
31778ed67789SDaniel Lezcano 	 * the loopback reference in rt6_info will not be taken, do it
31788ed67789SDaniel Lezcano 	 * manually for init_net */
3179d8d1f30bSChangli Gao 	init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
31808ed67789SDaniel Lezcano 	init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3181bdb3289fSDaniel Lezcano   #ifdef CONFIG_IPV6_MULTIPLE_TABLES
3182d8d1f30bSChangli Gao 	init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
31838ed67789SDaniel Lezcano 	init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3184d8d1f30bSChangli Gao 	init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
31858ed67789SDaniel Lezcano 	init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
3186bdb3289fSDaniel Lezcano   #endif
3187e8803b6cSDavid S. Miller 	ret = fib6_init();
3188433d49c3SDaniel Lezcano 	if (ret)
31898ed67789SDaniel Lezcano 		goto out_register_subsys;
3190433d49c3SDaniel Lezcano 
3191433d49c3SDaniel Lezcano 	ret = xfrm6_init();
3192433d49c3SDaniel Lezcano 	if (ret)
3193e8803b6cSDavid S. Miller 		goto out_fib6_init;
3194c35b7e72SDaniel Lezcano 
3195433d49c3SDaniel Lezcano 	ret = fib6_rules_init();
3196433d49c3SDaniel Lezcano 	if (ret)
3197433d49c3SDaniel Lezcano 		goto xfrm6_init;
31987e5449c2SDaniel Lezcano 
3199d189634eSThomas Graf 	ret = register_pernet_subsys(&ip6_route_net_late_ops);
3200d189634eSThomas Graf 	if (ret)
3201d189634eSThomas Graf 		goto fib6_rules_init;
3202d189634eSThomas Graf 
3203433d49c3SDaniel Lezcano 	ret = -ENOBUFS;
3204c7ac8679SGreg Rose 	if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) ||
3205c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) ||
3206c7ac8679SGreg Rose 	    __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL))
3207d189634eSThomas Graf 		goto out_register_late_subsys;
3208433d49c3SDaniel Lezcano 
32098ed67789SDaniel Lezcano 	ret = register_netdevice_notifier(&ip6_route_dev_notifier);
3210cdb18761SDaniel Lezcano 	if (ret)
3211d189634eSThomas Graf 		goto out_register_late_subsys;
32128ed67789SDaniel Lezcano 
3213433d49c3SDaniel Lezcano out:
3214433d49c3SDaniel Lezcano 	return ret;
3215433d49c3SDaniel Lezcano 
3216d189634eSThomas Graf out_register_late_subsys:
3217d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3218433d49c3SDaniel Lezcano fib6_rules_init:
3219433d49c3SDaniel Lezcano 	fib6_rules_cleanup();
3220433d49c3SDaniel Lezcano xfrm6_init:
3221433d49c3SDaniel Lezcano 	xfrm6_fini();
32222a0c451aSThomas Graf out_fib6_init:
32232a0c451aSThomas Graf 	fib6_gc_cleanup();
32248ed67789SDaniel Lezcano out_register_subsys:
32258ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
32267e52b33bSDavid S. Miller out_register_inetpeer:
32277e52b33bSDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
3228fc66f95cSEric Dumazet out_dst_entries:
3229fc66f95cSEric Dumazet 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3230433d49c3SDaniel Lezcano out_kmem_cache:
3231f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
3232433d49c3SDaniel Lezcano 	goto out;
32331da177e4SLinus Torvalds }
32341da177e4SLinus Torvalds 
32351da177e4SLinus Torvalds void ip6_route_cleanup(void)
32361da177e4SLinus Torvalds {
32378ed67789SDaniel Lezcano 	unregister_netdevice_notifier(&ip6_route_dev_notifier);
3238d189634eSThomas Graf 	unregister_pernet_subsys(&ip6_route_net_late_ops);
3239101367c2SThomas Graf 	fib6_rules_cleanup();
32401da177e4SLinus Torvalds 	xfrm6_fini();
32411da177e4SLinus Torvalds 	fib6_gc_cleanup();
3242c3426b47SDavid S. Miller 	unregister_pernet_subsys(&ipv6_inetpeer_ops);
32438ed67789SDaniel Lezcano 	unregister_pernet_subsys(&ip6_route_net_ops);
324441bb78b4SXiaotian Feng 	dst_entries_destroy(&ip6_dst_blackhole_ops);
3245f2fc6a54SBenjamin Thery 	kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
32461da177e4SLinus Torvalds }
3247